1/******************************************************************************
2** This file is an amalgamation of many separate C source files from SQLite
3** version 3.8.11.1.  By combining all the individual C code files into this
4** single large file, the entire code can be compiled as a single translation
5** unit.  This allows many compilers to do optimizations that would not be
6** possible if the files were compiled separately.  Performance improvements
7** of 5% or more are commonly seen when SQLite is compiled as a single
8** translation unit.
9**
10** This file is all you need to compile SQLite.  To use SQLite in other
11** programs, you need this file and the "sqlite3.h" header file that defines
12** the programming interface to the SQLite library.  (If you do not have
13** the "sqlite3.h" header file at hand, you will find a copy embedded within
14** the text of this file.  Search for "Begin file sqlite3.h" to find the start
15** of the embedded sqlite3.h header file.) Additional code files may be needed
16** if you want a wrapper to interface SQLite with your choice of programming
17** language. The code for the "sqlite3" command-line shell is also in a
18** separate file. This file contains only code for the core SQLite library.
19*/
20#define SQLITE_CORE 1
21#define SQLITE_AMALGAMATION 1
22#ifndef SQLITE_PRIVATE
23# define SQLITE_PRIVATE static
24#endif
25/************** Begin file sqliteInt.h ***************************************/
26/*
27** 2001 September 15
28**
29** The author disclaims copyright to this source code.  In place of
30** a legal notice, here is a blessing:
31**
32**    May you do good and not evil.
33**    May you find forgiveness for yourself and forgive others.
34**    May you share freely, never taking more than you give.
35**
36*************************************************************************
37** Internal interface definitions for SQLite.
38**
39*/
40#ifndef _SQLITEINT_H_
41#define _SQLITEINT_H_
42
43/*
44** Include the header file used to customize the compiler options for MSVC.
45** This should be done first so that it can successfully prevent spurious
46** compiler warnings due to subsequent content in this file and other files
47** that are included by this file.
48*/
49/************** Include msvc.h in the middle of sqliteInt.h ******************/
50/************** Begin file msvc.h ********************************************/
51/*
52** 2015 January 12
53**
54** The author disclaims copyright to this source code.  In place of
55** a legal notice, here is a blessing:
56**
57**    May you do good and not evil.
58**    May you find forgiveness for yourself and forgive others.
59**    May you share freely, never taking more than you give.
60**
61******************************************************************************
62**
63** This file contains code that is specific to MSVC.
64*/
65#ifndef _MSVC_H_
66#define _MSVC_H_
67
68#if defined(_MSC_VER)
69#pragma warning(disable : 4054)
70#pragma warning(disable : 4055)
71#pragma warning(disable : 4100)
72#pragma warning(disable : 4127)
73#pragma warning(disable : 4130)
74#pragma warning(disable : 4152)
75#pragma warning(disable : 4189)
76#pragma warning(disable : 4206)
77#pragma warning(disable : 4210)
78#pragma warning(disable : 4232)
79#pragma warning(disable : 4244)
80#pragma warning(disable : 4305)
81#pragma warning(disable : 4306)
82#pragma warning(disable : 4702)
83#pragma warning(disable : 4706)
84#endif /* defined(_MSC_VER) */
85
86#endif /* _MSVC_H_ */
87
88/************** End of msvc.h ************************************************/
89/************** Continuing where we left off in sqliteInt.h ******************/
90
91/*
92** Special setup for VxWorks
93*/
94/************** Include vxworks.h in the middle of sqliteInt.h ***************/
95/************** Begin file vxworks.h *****************************************/
96/*
97** 2015-03-02
98**
99** The author disclaims copyright to this source code.  In place of
100** a legal notice, here is a blessing:
101**
102**    May you do good and not evil.
103**    May you find forgiveness for yourself and forgive others.
104**    May you share freely, never taking more than you give.
105**
106******************************************************************************
107**
108** This file contains code that is specific to Wind River's VxWorks
109*/
110#if defined(__RTP__) || defined(_WRS_KERNEL)
111/* This is VxWorks.  Set up things specially for that OS
112*/
113#include <vxWorks.h>
114#include <pthread.h>  /* amalgamator: dontcache */
115#define OS_VXWORKS 1
116#define SQLITE_OS_OTHER 0
117#define SQLITE_HOMEGROWN_RECURSIVE_MUTEX 1
118#define SQLITE_OMIT_LOAD_EXTENSION 1
119#define SQLITE_ENABLE_LOCKING_STYLE 0
120#define HAVE_UTIME 1
121#else
122/* This is not VxWorks. */
123#define OS_VXWORKS 0
124#endif /* defined(_WRS_KERNEL) */
125
126/************** End of vxworks.h *********************************************/
127/************** Continuing where we left off in sqliteInt.h ******************/
128
129/*
130** These #defines should enable >2GB file support on POSIX if the
131** underlying operating system supports it.  If the OS lacks
132** large file support, or if the OS is windows, these should be no-ops.
133**
134** Ticket #2739:  The _LARGEFILE_SOURCE macro must appear before any
135** system #includes.  Hence, this block of code must be the very first
136** code in all source files.
137**
138** Large file support can be disabled using the -DSQLITE_DISABLE_LFS switch
139** on the compiler command line.  This is necessary if you are compiling
140** on a recent machine (ex: Red Hat 7.2) but you want your code to work
141** on an older machine (ex: Red Hat 6.0).  If you compile on Red Hat 7.2
142** without this option, LFS is enable.  But LFS does not exist in the kernel
143** in Red Hat 6.0, so the code won't work.  Hence, for maximum binary
144** portability you should omit LFS.
145**
146** The previous paragraph was written in 2005.  (This paragraph is written
147** on 2008-11-28.) These days, all Linux kernels support large files, so
148** you should probably leave LFS enabled.  But some embedded platforms might
149** lack LFS in which case the SQLITE_DISABLE_LFS macro might still be useful.
150**
151** Similar is true for Mac OS X.  LFS is only supported on Mac OS X 9 and later.
152*/
153#ifndef SQLITE_DISABLE_LFS
154# define _LARGE_FILE       1
155# ifndef _FILE_OFFSET_BITS
156#   define _FILE_OFFSET_BITS 64
157# endif
158# define _LARGEFILE_SOURCE 1
159#endif
160
161/* What version of GCC is being used.  0 means GCC is not being used */
162#ifdef __GNUC__
163# define GCC_VERSION (__GNUC__*1000000+__GNUC_MINOR__*1000+__GNUC_PATCHLEVEL__)
164#else
165# define GCC_VERSION 0
166#endif
167
168/* Needed for various definitions... */
169#if defined(__GNUC__) && !defined(_GNU_SOURCE)
170# define _GNU_SOURCE
171#endif
172
173#if defined(__OpenBSD__) && !defined(_BSD_SOURCE)
174# define _BSD_SOURCE
175#endif
176
177/*
178** For MinGW, check to see if we can include the header file containing its
179** version information, among other things.  Normally, this internal MinGW
180** header file would [only] be included automatically by other MinGW header
181** files; however, the contained version information is now required by this
182** header file to work around binary compatibility issues (see below) and
183** this is the only known way to reliably obtain it.  This entire #if block
184** would be completely unnecessary if there was any other way of detecting
185** MinGW via their preprocessor (e.g. if they customized their GCC to define
186** some MinGW-specific macros).  When compiling for MinGW, either the
187** _HAVE_MINGW_H or _HAVE__MINGW_H (note the extra underscore) macro must be
188** defined; otherwise, detection of conditions specific to MinGW will be
189** disabled.
190*/
191#if defined(_HAVE_MINGW_H)
192# include "mingw.h"
193#elif defined(_HAVE__MINGW_H)
194# include "_mingw.h"
195#endif
196
197/*
198** For MinGW version 4.x (and higher), check to see if the _USE_32BIT_TIME_T
199** define is required to maintain binary compatibility with the MSVC runtime
200** library in use (e.g. for Windows XP).
201*/
202#if !defined(_USE_32BIT_TIME_T) && !defined(_USE_64BIT_TIME_T) && \
203    defined(_WIN32) && !defined(_WIN64) && \
204    defined(__MINGW_MAJOR_VERSION) && __MINGW_MAJOR_VERSION >= 4 && \
205    defined(__MSVCRT__)
206# define _USE_32BIT_TIME_T
207#endif
208
209/* The public SQLite interface.  The _FILE_OFFSET_BITS macro must appear
210** first in QNX.  Also, the _USE_32BIT_TIME_T macro must appear first for
211** MinGW.
212*/
213/************** Include sqlite3.h in the middle of sqliteInt.h ***************/
214/************** Begin file sqlite3.h *****************************************/
215/*
216** 2001 September 15
217**
218** The author disclaims copyright to this source code.  In place of
219** a legal notice, here is a blessing:
220**
221**    May you do good and not evil.
222**    May you find forgiveness for yourself and forgive others.
223**    May you share freely, never taking more than you give.
224**
225*************************************************************************
226** This header file defines the interface that the SQLite library
227** presents to client programs.  If a C-function, structure, datatype,
228** or constant definition does not appear in this file, then it is
229** not a published API of SQLite, is subject to change without
230** notice, and should not be referenced by programs that use SQLite.
231**
232** Some of the definitions that are in this file are marked as
233** "experimental".  Experimental interfaces are normally new
234** features recently added to SQLite.  We do not anticipate changes
235** to experimental interfaces but reserve the right to make minor changes
236** if experience from use "in the wild" suggest such changes are prudent.
237**
238** The official C-language API documentation for SQLite is derived
239** from comments in this file.  This file is the authoritative source
240** on how SQLite interfaces are supposed to operate.
241**
242** The name of this file under configuration management is "sqlite.h.in".
243** The makefile makes some minor changes to this file (such as inserting
244** the version number) and changes its name to "sqlite3.h" as
245** part of the build process.
246*/
247#ifndef _SQLITE3_H_
248#define _SQLITE3_H_
249#include <stdarg.h>     /* Needed for the definition of va_list */
250
251/*
252** Make sure we can call this stuff from C++.
253*/
254#if 0
255extern "C" {
256#endif
257
258
259/*
260** Provide the ability to override linkage features of the interface.
261*/
262#ifndef SQLITE_EXTERN
263# define SQLITE_EXTERN extern
264#endif
265#ifndef SQLITE_API
266# define SQLITE_API
267#endif
268#ifndef SQLITE_CDECL
269# define SQLITE_CDECL
270#endif
271#ifndef SQLITE_STDCALL
272# define SQLITE_STDCALL
273#endif
274
275/*
276** These no-op macros are used in front of interfaces to mark those
277** interfaces as either deprecated or experimental.  New applications
278** should not use deprecated interfaces - they are supported for backwards
279** compatibility only.  Application writers should be aware that
280** experimental interfaces are subject to change in point releases.
281**
282** These macros used to resolve to various kinds of compiler magic that
283** would generate warning messages when they were used.  But that
284** compiler magic ended up generating such a flurry of bug reports
285** that we have taken it all out and gone back to using simple
286** noop macros.
287*/
288#define SQLITE_DEPRECATED
289#define SQLITE_EXPERIMENTAL
290
291/*
292** Ensure these symbols were not defined by some previous header file.
293*/
294#ifdef SQLITE_VERSION
295# undef SQLITE_VERSION
296#endif
297#ifdef SQLITE_VERSION_NUMBER
298# undef SQLITE_VERSION_NUMBER
299#endif
300
301/*
302** CAPI3REF: Compile-Time Library Version Numbers
303**
304** ^(The [SQLITE_VERSION] C preprocessor macro in the sqlite3.h header
305** evaluates to a string literal that is the SQLite version in the
306** format "X.Y.Z" where X is the major version number (always 3 for
307** SQLite3) and Y is the minor version number and Z is the release number.)^
308** ^(The [SQLITE_VERSION_NUMBER] C preprocessor macro resolves to an integer
309** with the value (X*1000000 + Y*1000 + Z) where X, Y, and Z are the same
310** numbers used in [SQLITE_VERSION].)^
311** The SQLITE_VERSION_NUMBER for any given release of SQLite will also
312** be larger than the release from which it is derived.  Either Y will
313** be held constant and Z will be incremented or else Y will be incremented
314** and Z will be reset to zero.
315**
316** Since version 3.6.18, SQLite source code has been stored in the
317** <a href="http://www.fossil-scm.org/">Fossil configuration management
318** system</a>.  ^The SQLITE_SOURCE_ID macro evaluates to
319** a string which identifies a particular check-in of SQLite
320** within its configuration management system.  ^The SQLITE_SOURCE_ID
321** string contains the date and time of the check-in (UTC) and an SHA1
322** hash of the entire source tree.
323**
324** See also: [sqlite3_libversion()],
325** [sqlite3_libversion_number()], [sqlite3_sourceid()],
326** [sqlite_version()] and [sqlite_source_id()].
327*/
328#define SQLITE_VERSION        "3.8.11.1"
329#define SQLITE_VERSION_NUMBER 3008011
330#define SQLITE_SOURCE_ID      "2015-07-29 20:00:57 cf538e2783e468bbc25e7cb2a9ee64d3e0e80b2f"
331
332/*
333** CAPI3REF: Run-Time Library Version Numbers
334** KEYWORDS: sqlite3_version, sqlite3_sourceid
335**
336** These interfaces provide the same information as the [SQLITE_VERSION],
337** [SQLITE_VERSION_NUMBER], and [SQLITE_SOURCE_ID] C preprocessor macros
338** but are associated with the library instead of the header file.  ^(Cautious
339** programmers might include assert() statements in their application to
340** verify that values returned by these interfaces match the macros in
341** the header, and thus insure that the application is
342** compiled with matching library and header files.
343**
344** <blockquote><pre>
345** assert( sqlite3_libversion_number()==SQLITE_VERSION_NUMBER );
346** assert( strcmp(sqlite3_sourceid(),SQLITE_SOURCE_ID)==0 );
347** assert( strcmp(sqlite3_libversion(),SQLITE_VERSION)==0 );
348** </pre></blockquote>)^
349**
350** ^The sqlite3_version[] string constant contains the text of [SQLITE_VERSION]
351** macro.  ^The sqlite3_libversion() function returns a pointer to the
352** to the sqlite3_version[] string constant.  The sqlite3_libversion()
353** function is provided for use in DLLs since DLL users usually do not have
354** direct access to string constants within the DLL.  ^The
355** sqlite3_libversion_number() function returns an integer equal to
356** [SQLITE_VERSION_NUMBER].  ^The sqlite3_sourceid() function returns
357** a pointer to a string constant whose value is the same as the
358** [SQLITE_SOURCE_ID] C preprocessor macro.
359**
360** See also: [sqlite_version()] and [sqlite_source_id()].
361*/
362SQLITE_API const char sqlite3_version[] = SQLITE_VERSION;
363SQLITE_API const char *SQLITE_STDCALL sqlite3_libversion(void);
364SQLITE_API const char *SQLITE_STDCALL sqlite3_sourceid(void);
365SQLITE_API int SQLITE_STDCALL sqlite3_libversion_number(void);
366
367/*
368** CAPI3REF: Run-Time Library Compilation Options Diagnostics
369**
370** ^The sqlite3_compileoption_used() function returns 0 or 1
371** indicating whether the specified option was defined at
372** compile time.  ^The SQLITE_ prefix may be omitted from the
373** option name passed to sqlite3_compileoption_used().
374**
375** ^The sqlite3_compileoption_get() function allows iterating
376** over the list of options that were defined at compile time by
377** returning the N-th compile time option string.  ^If N is out of range,
378** sqlite3_compileoption_get() returns a NULL pointer.  ^The SQLITE_
379** prefix is omitted from any strings returned by
380** sqlite3_compileoption_get().
381**
382** ^Support for the diagnostic functions sqlite3_compileoption_used()
383** and sqlite3_compileoption_get() may be omitted by specifying the
384** [SQLITE_OMIT_COMPILEOPTION_DIAGS] option at compile time.
385**
386** See also: SQL functions [sqlite_compileoption_used()] and
387** [sqlite_compileoption_get()] and the [compile_options pragma].
388*/
389#ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
390SQLITE_API int SQLITE_STDCALL sqlite3_compileoption_used(const char *zOptName);
391SQLITE_API const char *SQLITE_STDCALL sqlite3_compileoption_get(int N);
392#endif
393
394/*
395** CAPI3REF: Test To See If The Library Is Threadsafe
396**
397** ^The sqlite3_threadsafe() function returns zero if and only if
398** SQLite was compiled with mutexing code omitted due to the
399** [SQLITE_THREADSAFE] compile-time option being set to 0.
400**
401** SQLite can be compiled with or without mutexes.  When
402** the [SQLITE_THREADSAFE] C preprocessor macro is 1 or 2, mutexes
403** are enabled and SQLite is threadsafe.  When the
404** [SQLITE_THREADSAFE] macro is 0,
405** the mutexes are omitted.  Without the mutexes, it is not safe
406** to use SQLite concurrently from more than one thread.
407**
408** Enabling mutexes incurs a measurable performance penalty.
409** So if speed is of utmost importance, it makes sense to disable
410** the mutexes.  But for maximum safety, mutexes should be enabled.
411** ^The default behavior is for mutexes to be enabled.
412**
413** This interface can be used by an application to make sure that the
414** version of SQLite that it is linking against was compiled with
415** the desired setting of the [SQLITE_THREADSAFE] macro.
416**
417** This interface only reports on the compile-time mutex setting
418** of the [SQLITE_THREADSAFE] flag.  If SQLite is compiled with
419** SQLITE_THREADSAFE=1 or =2 then mutexes are enabled by default but
420** can be fully or partially disabled using a call to [sqlite3_config()]
421** with the verbs [SQLITE_CONFIG_SINGLETHREAD], [SQLITE_CONFIG_MULTITHREAD],
422** or [SQLITE_CONFIG_SERIALIZED].  ^(The return value of the
423** sqlite3_threadsafe() function shows only the compile-time setting of
424** thread safety, not any run-time changes to that setting made by
425** sqlite3_config(). In other words, the return value from sqlite3_threadsafe()
426** is unchanged by calls to sqlite3_config().)^
427**
428** See the [threading mode] documentation for additional information.
429*/
430SQLITE_API int SQLITE_STDCALL sqlite3_threadsafe(void);
431
432/*
433** CAPI3REF: Database Connection Handle
434** KEYWORDS: {database connection} {database connections}
435**
436** Each open SQLite database is represented by a pointer to an instance of
437** the opaque structure named "sqlite3".  It is useful to think of an sqlite3
438** pointer as an object.  The [sqlite3_open()], [sqlite3_open16()], and
439** [sqlite3_open_v2()] interfaces are its constructors, and [sqlite3_close()]
440** and [sqlite3_close_v2()] are its destructors.  There are many other
441** interfaces (such as
442** [sqlite3_prepare_v2()], [sqlite3_create_function()], and
443** [sqlite3_busy_timeout()] to name but three) that are methods on an
444** sqlite3 object.
445*/
446typedef struct sqlite3 sqlite3;
447
448/*
449** CAPI3REF: 64-Bit Integer Types
450** KEYWORDS: sqlite_int64 sqlite_uint64
451**
452** Because there is no cross-platform way to specify 64-bit integer types
453** SQLite includes typedefs for 64-bit signed and unsigned integers.
454**
455** The sqlite3_int64 and sqlite3_uint64 are the preferred type definitions.
456** The sqlite_int64 and sqlite_uint64 types are supported for backwards
457** compatibility only.
458**
459** ^The sqlite3_int64 and sqlite_int64 types can store integer values
460** between -9223372036854775808 and +9223372036854775807 inclusive.  ^The
461** sqlite3_uint64 and sqlite_uint64 types can store integer values
462** between 0 and +18446744073709551615 inclusive.
463*/
464#ifdef SQLITE_INT64_TYPE
465  typedef SQLITE_INT64_TYPE sqlite_int64;
466  typedef unsigned SQLITE_INT64_TYPE sqlite_uint64;
467#elif defined(_MSC_VER) || defined(__BORLANDC__)
468  typedef __int64 sqlite_int64;
469  typedef unsigned __int64 sqlite_uint64;
470#else
471  typedef long long int sqlite_int64;
472  typedef unsigned long long int sqlite_uint64;
473#endif
474typedef sqlite_int64 sqlite3_int64;
475typedef sqlite_uint64 sqlite3_uint64;
476
477/*
478** If compiling for a processor that lacks floating point support,
479** substitute integer for floating-point.
480*/
481#ifdef SQLITE_OMIT_FLOATING_POINT
482# define double sqlite3_int64
483#endif
484
485/*
486** CAPI3REF: Closing A Database Connection
487** DESTRUCTOR: sqlite3
488**
489** ^The sqlite3_close() and sqlite3_close_v2() routines are destructors
490** for the [sqlite3] object.
491** ^Calls to sqlite3_close() and sqlite3_close_v2() return [SQLITE_OK] if
492** the [sqlite3] object is successfully destroyed and all associated
493** resources are deallocated.
494**
495** ^If the database connection is associated with unfinalized prepared
496** statements or unfinished sqlite3_backup objects then sqlite3_close()
497** will leave the database connection open and return [SQLITE_BUSY].
498** ^If sqlite3_close_v2() is called with unfinalized prepared statements
499** and/or unfinished sqlite3_backups, then the database connection becomes
500** an unusable "zombie" which will automatically be deallocated when the
501** last prepared statement is finalized or the last sqlite3_backup is
502** finished.  The sqlite3_close_v2() interface is intended for use with
503** host languages that are garbage collected, and where the order in which
504** destructors are called is arbitrary.
505**
506** Applications should [sqlite3_finalize | finalize] all [prepared statements],
507** [sqlite3_blob_close | close] all [BLOB handles], and
508** [sqlite3_backup_finish | finish] all [sqlite3_backup] objects associated
509** with the [sqlite3] object prior to attempting to close the object.  ^If
510** sqlite3_close_v2() is called on a [database connection] that still has
511** outstanding [prepared statements], [BLOB handles], and/or
512** [sqlite3_backup] objects then it returns [SQLITE_OK] and the deallocation
513** of resources is deferred until all [prepared statements], [BLOB handles],
514** and [sqlite3_backup] objects are also destroyed.
515**
516** ^If an [sqlite3] object is destroyed while a transaction is open,
517** the transaction is automatically rolled back.
518**
519** The C parameter to [sqlite3_close(C)] and [sqlite3_close_v2(C)]
520** must be either a NULL
521** pointer or an [sqlite3] object pointer obtained
522** from [sqlite3_open()], [sqlite3_open16()], or
523** [sqlite3_open_v2()], and not previously closed.
524** ^Calling sqlite3_close() or sqlite3_close_v2() with a NULL pointer
525** argument is a harmless no-op.
526*/
527SQLITE_API int SQLITE_STDCALL sqlite3_close(sqlite3*);
528SQLITE_API int SQLITE_STDCALL sqlite3_close_v2(sqlite3*);
529
530/*
531** The type for a callback function.
532** This is legacy and deprecated.  It is included for historical
533** compatibility and is not documented.
534*/
535typedef int (*sqlite3_callback)(void*,int,char**, char**);
536
537/*
538** CAPI3REF: One-Step Query Execution Interface
539** METHOD: sqlite3
540**
541** The sqlite3_exec() interface is a convenience wrapper around
542** [sqlite3_prepare_v2()], [sqlite3_step()], and [sqlite3_finalize()],
543** that allows an application to run multiple statements of SQL
544** without having to use a lot of C code.
545**
546** ^The sqlite3_exec() interface runs zero or more UTF-8 encoded,
547** semicolon-separate SQL statements passed into its 2nd argument,
548** in the context of the [database connection] passed in as its 1st
549** argument.  ^If the callback function of the 3rd argument to
550** sqlite3_exec() is not NULL, then it is invoked for each result row
551** coming out of the evaluated SQL statements.  ^The 4th argument to
552** sqlite3_exec() is relayed through to the 1st argument of each
553** callback invocation.  ^If the callback pointer to sqlite3_exec()
554** is NULL, then no callback is ever invoked and result rows are
555** ignored.
556**
557** ^If an error occurs while evaluating the SQL statements passed into
558** sqlite3_exec(), then execution of the current statement stops and
559** subsequent statements are skipped.  ^If the 5th parameter to sqlite3_exec()
560** is not NULL then any error message is written into memory obtained
561** from [sqlite3_malloc()] and passed back through the 5th parameter.
562** To avoid memory leaks, the application should invoke [sqlite3_free()]
563** on error message strings returned through the 5th parameter of
564** of sqlite3_exec() after the error message string is no longer needed.
565** ^If the 5th parameter to sqlite3_exec() is not NULL and no errors
566** occur, then sqlite3_exec() sets the pointer in its 5th parameter to
567** NULL before returning.
568**
569** ^If an sqlite3_exec() callback returns non-zero, the sqlite3_exec()
570** routine returns SQLITE_ABORT without invoking the callback again and
571** without running any subsequent SQL statements.
572**
573** ^The 2nd argument to the sqlite3_exec() callback function is the
574** number of columns in the result.  ^The 3rd argument to the sqlite3_exec()
575** callback is an array of pointers to strings obtained as if from
576** [sqlite3_column_text()], one for each column.  ^If an element of a
577** result row is NULL then the corresponding string pointer for the
578** sqlite3_exec() callback is a NULL pointer.  ^The 4th argument to the
579** sqlite3_exec() callback is an array of pointers to strings where each
580** entry represents the name of corresponding result column as obtained
581** from [sqlite3_column_name()].
582**
583** ^If the 2nd parameter to sqlite3_exec() is a NULL pointer, a pointer
584** to an empty string, or a pointer that contains only whitespace and/or
585** SQL comments, then no SQL statements are evaluated and the database
586** is not changed.
587**
588** Restrictions:
589**
590** <ul>
591** <li> The application must insure that the 1st parameter to sqlite3_exec()
592**      is a valid and open [database connection].
593** <li> The application must not close the [database connection] specified by
594**      the 1st parameter to sqlite3_exec() while sqlite3_exec() is running.
595** <li> The application must not modify the SQL statement text passed into
596**      the 2nd parameter of sqlite3_exec() while sqlite3_exec() is running.
597** </ul>
598*/
599SQLITE_API int SQLITE_STDCALL sqlite3_exec(
600  sqlite3*,                                  /* An open database */
601  const char *sql,                           /* SQL to be evaluated */
602  int (*callback)(void*,int,char**,char**),  /* Callback function */
603  void *,                                    /* 1st argument to callback */
604  char **errmsg                              /* Error msg written here */
605);
606
607/*
608** CAPI3REF: Result Codes
609** KEYWORDS: {result code definitions}
610**
611** Many SQLite functions return an integer result code from the set shown
612** here in order to indicate success or failure.
613**
614** New error codes may be added in future versions of SQLite.
615**
616** See also: [extended result code definitions]
617*/
618#define SQLITE_OK           0   /* Successful result */
619/* beginning-of-error-codes */
620#define SQLITE_ERROR        1   /* SQL error or missing database */
621#define SQLITE_INTERNAL     2   /* Internal logic error in SQLite */
622#define SQLITE_PERM         3   /* Access permission denied */
623#define SQLITE_ABORT        4   /* Callback routine requested an abort */
624#define SQLITE_BUSY         5   /* The database file is locked */
625#define SQLITE_LOCKED       6   /* A table in the database is locked */
626#define SQLITE_NOMEM        7   /* A malloc() failed */
627#define SQLITE_READONLY     8   /* Attempt to write a readonly database */
628#define SQLITE_INTERRUPT    9   /* Operation terminated by sqlite3_interrupt()*/
629#define SQLITE_IOERR       10   /* Some kind of disk I/O error occurred */
630#define SQLITE_CORRUPT     11   /* The database disk image is malformed */
631#define SQLITE_NOTFOUND    12   /* Unknown opcode in sqlite3_file_control() */
632#define SQLITE_FULL        13   /* Insertion failed because database is full */
633#define SQLITE_CANTOPEN    14   /* Unable to open the database file */
634#define SQLITE_PROTOCOL    15   /* Database lock protocol error */
635#define SQLITE_EMPTY       16   /* Database is empty */
636#define SQLITE_SCHEMA      17   /* The database schema changed */
637#define SQLITE_TOOBIG      18   /* String or BLOB exceeds size limit */
638#define SQLITE_CONSTRAINT  19   /* Abort due to constraint violation */
639#define SQLITE_MISMATCH    20   /* Data type mismatch */
640#define SQLITE_MISUSE      21   /* Library used incorrectly */
641#define SQLITE_NOLFS       22   /* Uses OS features not supported on host */
642#define SQLITE_AUTH        23   /* Authorization denied */
643#define SQLITE_FORMAT      24   /* Auxiliary database format error */
644#define SQLITE_RANGE       25   /* 2nd parameter to sqlite3_bind out of range */
645#define SQLITE_NOTADB      26   /* File opened that is not a database file */
646#define SQLITE_NOTICE      27   /* Notifications from sqlite3_log() */
647#define SQLITE_WARNING     28   /* Warnings from sqlite3_log() */
648#define SQLITE_ROW         100  /* sqlite3_step() has another row ready */
649#define SQLITE_DONE        101  /* sqlite3_step() has finished executing */
650/* end-of-error-codes */
651
652/*
653** CAPI3REF: Extended Result Codes
654** KEYWORDS: {extended result code definitions}
655**
656** In its default configuration, SQLite API routines return one of 30 integer
657** [result codes].  However, experience has shown that many of
658** these result codes are too coarse-grained.  They do not provide as
659** much information about problems as programmers might like.  In an effort to
660** address this, newer versions of SQLite (version 3.3.8 and later) include
661** support for additional result codes that provide more detailed information
662** about errors. These [extended result codes] are enabled or disabled
663** on a per database connection basis using the
664** [sqlite3_extended_result_codes()] API.  Or, the extended code for
665** the most recent error can be obtained using
666** [sqlite3_extended_errcode()].
667*/
668#define SQLITE_IOERR_READ              (SQLITE_IOERR | (1<<8))
669#define SQLITE_IOERR_SHORT_READ        (SQLITE_IOERR | (2<<8))
670#define SQLITE_IOERR_WRITE             (SQLITE_IOERR | (3<<8))
671#define SQLITE_IOERR_FSYNC             (SQLITE_IOERR | (4<<8))
672#define SQLITE_IOERR_DIR_FSYNC         (SQLITE_IOERR | (5<<8))
673#define SQLITE_IOERR_TRUNCATE          (SQLITE_IOERR | (6<<8))
674#define SQLITE_IOERR_FSTAT             (SQLITE_IOERR | (7<<8))
675#define SQLITE_IOERR_UNLOCK            (SQLITE_IOERR | (8<<8))
676#define SQLITE_IOERR_RDLOCK            (SQLITE_IOERR | (9<<8))
677#define SQLITE_IOERR_DELETE            (SQLITE_IOERR | (10<<8))
678#define SQLITE_IOERR_BLOCKED           (SQLITE_IOERR | (11<<8))
679#define SQLITE_IOERR_NOMEM             (SQLITE_IOERR | (12<<8))
680#define SQLITE_IOERR_ACCESS            (SQLITE_IOERR | (13<<8))
681#define SQLITE_IOERR_CHECKRESERVEDLOCK (SQLITE_IOERR | (14<<8))
682#define SQLITE_IOERR_LOCK              (SQLITE_IOERR | (15<<8))
683#define SQLITE_IOERR_CLOSE             (SQLITE_IOERR | (16<<8))
684#define SQLITE_IOERR_DIR_CLOSE         (SQLITE_IOERR | (17<<8))
685#define SQLITE_IOERR_SHMOPEN           (SQLITE_IOERR | (18<<8))
686#define SQLITE_IOERR_SHMSIZE           (SQLITE_IOERR | (19<<8))
687#define SQLITE_IOERR_SHMLOCK           (SQLITE_IOERR | (20<<8))
688#define SQLITE_IOERR_SHMMAP            (SQLITE_IOERR | (21<<8))
689#define SQLITE_IOERR_SEEK              (SQLITE_IOERR | (22<<8))
690#define SQLITE_IOERR_DELETE_NOENT      (SQLITE_IOERR | (23<<8))
691#define SQLITE_IOERR_MMAP              (SQLITE_IOERR | (24<<8))
692#define SQLITE_IOERR_GETTEMPPATH       (SQLITE_IOERR | (25<<8))
693#define SQLITE_IOERR_CONVPATH          (SQLITE_IOERR | (26<<8))
694#define SQLITE_LOCKED_SHAREDCACHE      (SQLITE_LOCKED |  (1<<8))
695#define SQLITE_BUSY_RECOVERY           (SQLITE_BUSY   |  (1<<8))
696#define SQLITE_BUSY_SNAPSHOT           (SQLITE_BUSY   |  (2<<8))
697#define SQLITE_CANTOPEN_NOTEMPDIR      (SQLITE_CANTOPEN | (1<<8))
698#define SQLITE_CANTOPEN_ISDIR          (SQLITE_CANTOPEN | (2<<8))
699#define SQLITE_CANTOPEN_FULLPATH       (SQLITE_CANTOPEN | (3<<8))
700#define SQLITE_CANTOPEN_CONVPATH       (SQLITE_CANTOPEN | (4<<8))
701#define SQLITE_CORRUPT_VTAB            (SQLITE_CORRUPT | (1<<8))
702#define SQLITE_READONLY_RECOVERY       (SQLITE_READONLY | (1<<8))
703#define SQLITE_READONLY_CANTLOCK       (SQLITE_READONLY | (2<<8))
704#define SQLITE_READONLY_ROLLBACK       (SQLITE_READONLY | (3<<8))
705#define SQLITE_READONLY_DBMOVED        (SQLITE_READONLY | (4<<8))
706#define SQLITE_ABORT_ROLLBACK          (SQLITE_ABORT | (2<<8))
707#define SQLITE_CONSTRAINT_CHECK        (SQLITE_CONSTRAINT | (1<<8))
708#define SQLITE_CONSTRAINT_COMMITHOOK   (SQLITE_CONSTRAINT | (2<<8))
709#define SQLITE_CONSTRAINT_FOREIGNKEY   (SQLITE_CONSTRAINT | (3<<8))
710#define SQLITE_CONSTRAINT_FUNCTION     (SQLITE_CONSTRAINT | (4<<8))
711#define SQLITE_CONSTRAINT_NOTNULL      (SQLITE_CONSTRAINT | (5<<8))
712#define SQLITE_CONSTRAINT_PRIMARYKEY   (SQLITE_CONSTRAINT | (6<<8))
713#define SQLITE_CONSTRAINT_TRIGGER      (SQLITE_CONSTRAINT | (7<<8))
714#define SQLITE_CONSTRAINT_UNIQUE       (SQLITE_CONSTRAINT | (8<<8))
715#define SQLITE_CONSTRAINT_VTAB         (SQLITE_CONSTRAINT | (9<<8))
716#define SQLITE_CONSTRAINT_ROWID        (SQLITE_CONSTRAINT |(10<<8))
717#define SQLITE_NOTICE_RECOVER_WAL      (SQLITE_NOTICE | (1<<8))
718#define SQLITE_NOTICE_RECOVER_ROLLBACK (SQLITE_NOTICE | (2<<8))
719#define SQLITE_WARNING_AUTOINDEX       (SQLITE_WARNING | (1<<8))
720#define SQLITE_AUTH_USER               (SQLITE_AUTH | (1<<8))
721
722/*
723** CAPI3REF: Flags For File Open Operations
724**
725** These bit values are intended for use in the
726** 3rd parameter to the [sqlite3_open_v2()] interface and
727** in the 4th parameter to the [sqlite3_vfs.xOpen] method.
728*/
729#define SQLITE_OPEN_READONLY         0x00000001  /* Ok for sqlite3_open_v2() */
730#define SQLITE_OPEN_READWRITE        0x00000002  /* Ok for sqlite3_open_v2() */
731#define SQLITE_OPEN_CREATE           0x00000004  /* Ok for sqlite3_open_v2() */
732#define SQLITE_OPEN_DELETEONCLOSE    0x00000008  /* VFS only */
733#define SQLITE_OPEN_EXCLUSIVE        0x00000010  /* VFS only */
734#define SQLITE_OPEN_AUTOPROXY        0x00000020  /* VFS only */
735#define SQLITE_OPEN_URI              0x00000040  /* Ok for sqlite3_open_v2() */
736#define SQLITE_OPEN_MEMORY           0x00000080  /* Ok for sqlite3_open_v2() */
737#define SQLITE_OPEN_MAIN_DB          0x00000100  /* VFS only */
738#define SQLITE_OPEN_TEMP_DB          0x00000200  /* VFS only */
739#define SQLITE_OPEN_TRANSIENT_DB     0x00000400  /* VFS only */
740#define SQLITE_OPEN_MAIN_JOURNAL     0x00000800  /* VFS only */
741#define SQLITE_OPEN_TEMP_JOURNAL     0x00001000  /* VFS only */
742#define SQLITE_OPEN_SUBJOURNAL       0x00002000  /* VFS only */
743#define SQLITE_OPEN_MASTER_JOURNAL   0x00004000  /* VFS only */
744#define SQLITE_OPEN_NOMUTEX          0x00008000  /* Ok for sqlite3_open_v2() */
745#define SQLITE_OPEN_FULLMUTEX        0x00010000  /* Ok for sqlite3_open_v2() */
746#define SQLITE_OPEN_SHAREDCACHE      0x00020000  /* Ok for sqlite3_open_v2() */
747#define SQLITE_OPEN_PRIVATECACHE     0x00040000  /* Ok for sqlite3_open_v2() */
748#define SQLITE_OPEN_WAL              0x00080000  /* VFS only */
749
750/* Reserved:                         0x00F00000 */
751
752/*
753** CAPI3REF: Device Characteristics
754**
755** The xDeviceCharacteristics method of the [sqlite3_io_methods]
756** object returns an integer which is a vector of these
757** bit values expressing I/O characteristics of the mass storage
758** device that holds the file that the [sqlite3_io_methods]
759** refers to.
760**
761** The SQLITE_IOCAP_ATOMIC property means that all writes of
762** any size are atomic.  The SQLITE_IOCAP_ATOMICnnn values
763** mean that writes of blocks that are nnn bytes in size and
764** are aligned to an address which is an integer multiple of
765** nnn are atomic.  The SQLITE_IOCAP_SAFE_APPEND value means
766** that when data is appended to a file, the data is appended
767** first then the size of the file is extended, never the other
768** way around.  The SQLITE_IOCAP_SEQUENTIAL property means that
769** information is written to disk in the same order as calls
770** to xWrite().  The SQLITE_IOCAP_POWERSAFE_OVERWRITE property means that
771** after reboot following a crash or power loss, the only bytes in a
772** file that were written at the application level might have changed
773** and that adjacent bytes, even bytes within the same sector are
774** guaranteed to be unchanged.  The SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN
775** flag indicate that a file cannot be deleted when open.  The
776** SQLITE_IOCAP_IMMUTABLE flag indicates that the file is on
777** read-only media and cannot be changed even by processes with
778** elevated privileges.
779*/
780#define SQLITE_IOCAP_ATOMIC                 0x00000001
781#define SQLITE_IOCAP_ATOMIC512              0x00000002
782#define SQLITE_IOCAP_ATOMIC1K               0x00000004
783#define SQLITE_IOCAP_ATOMIC2K               0x00000008
784#define SQLITE_IOCAP_ATOMIC4K               0x00000010
785#define SQLITE_IOCAP_ATOMIC8K               0x00000020
786#define SQLITE_IOCAP_ATOMIC16K              0x00000040
787#define SQLITE_IOCAP_ATOMIC32K              0x00000080
788#define SQLITE_IOCAP_ATOMIC64K              0x00000100
789#define SQLITE_IOCAP_SAFE_APPEND            0x00000200
790#define SQLITE_IOCAP_SEQUENTIAL             0x00000400
791#define SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN  0x00000800
792#define SQLITE_IOCAP_POWERSAFE_OVERWRITE    0x00001000
793#define SQLITE_IOCAP_IMMUTABLE              0x00002000
794
795/*
796** CAPI3REF: File Locking Levels
797**
798** SQLite uses one of these integer values as the second
799** argument to calls it makes to the xLock() and xUnlock() methods
800** of an [sqlite3_io_methods] object.
801*/
802#define SQLITE_LOCK_NONE          0
803#define SQLITE_LOCK_SHARED        1
804#define SQLITE_LOCK_RESERVED      2
805#define SQLITE_LOCK_PENDING       3
806#define SQLITE_LOCK_EXCLUSIVE     4
807
808/*
809** CAPI3REF: Synchronization Type Flags
810**
811** When SQLite invokes the xSync() method of an
812** [sqlite3_io_methods] object it uses a combination of
813** these integer values as the second argument.
814**
815** When the SQLITE_SYNC_DATAONLY flag is used, it means that the
816** sync operation only needs to flush data to mass storage.  Inode
817** information need not be flushed. If the lower four bits of the flag
818** equal SQLITE_SYNC_NORMAL, that means to use normal fsync() semantics.
819** If the lower four bits equal SQLITE_SYNC_FULL, that means
820** to use Mac OS X style fullsync instead of fsync().
821**
822** Do not confuse the SQLITE_SYNC_NORMAL and SQLITE_SYNC_FULL flags
823** with the [PRAGMA synchronous]=NORMAL and [PRAGMA synchronous]=FULL
824** settings.  The [synchronous pragma] determines when calls to the
825** xSync VFS method occur and applies uniformly across all platforms.
826** The SQLITE_SYNC_NORMAL and SQLITE_SYNC_FULL flags determine how
827** energetic or rigorous or forceful the sync operations are and
828** only make a difference on Mac OSX for the default SQLite code.
829** (Third-party VFS implementations might also make the distinction
830** between SQLITE_SYNC_NORMAL and SQLITE_SYNC_FULL, but among the
831** operating systems natively supported by SQLite, only Mac OSX
832** cares about the difference.)
833*/
834#define SQLITE_SYNC_NORMAL        0x00002
835#define SQLITE_SYNC_FULL          0x00003
836#define SQLITE_SYNC_DATAONLY      0x00010
837
838/*
839** CAPI3REF: OS Interface Open File Handle
840**
841** An [sqlite3_file] object represents an open file in the
842** [sqlite3_vfs | OS interface layer].  Individual OS interface
843** implementations will
844** want to subclass this object by appending additional fields
845** for their own use.  The pMethods entry is a pointer to an
846** [sqlite3_io_methods] object that defines methods for performing
847** I/O operations on the open file.
848*/
849typedef struct sqlite3_file sqlite3_file;
850struct sqlite3_file {
851  const struct sqlite3_io_methods *pMethods;  /* Methods for an open file */
852};
853
854/*
855** CAPI3REF: OS Interface File Virtual Methods Object
856**
857** Every file opened by the [sqlite3_vfs.xOpen] method populates an
858** [sqlite3_file] object (or, more commonly, a subclass of the
859** [sqlite3_file] object) with a pointer to an instance of this object.
860** This object defines the methods used to perform various operations
861** against the open file represented by the [sqlite3_file] object.
862**
863** If the [sqlite3_vfs.xOpen] method sets the sqlite3_file.pMethods element
864** to a non-NULL pointer, then the sqlite3_io_methods.xClose method
865** may be invoked even if the [sqlite3_vfs.xOpen] reported that it failed.  The
866** only way to prevent a call to xClose following a failed [sqlite3_vfs.xOpen]
867** is for the [sqlite3_vfs.xOpen] to set the sqlite3_file.pMethods element
868** to NULL.
869**
870** The flags argument to xSync may be one of [SQLITE_SYNC_NORMAL] or
871** [SQLITE_SYNC_FULL].  The first choice is the normal fsync().
872** The second choice is a Mac OS X style fullsync.  The [SQLITE_SYNC_DATAONLY]
873** flag may be ORed in to indicate that only the data of the file
874** and not its inode needs to be synced.
875**
876** The integer values to xLock() and xUnlock() are one of
877** <ul>
878** <li> [SQLITE_LOCK_NONE],
879** <li> [SQLITE_LOCK_SHARED],
880** <li> [SQLITE_LOCK_RESERVED],
881** <li> [SQLITE_LOCK_PENDING], or
882** <li> [SQLITE_LOCK_EXCLUSIVE].
883** </ul>
884** xLock() increases the lock. xUnlock() decreases the lock.
885** The xCheckReservedLock() method checks whether any database connection,
886** either in this process or in some other process, is holding a RESERVED,
887** PENDING, or EXCLUSIVE lock on the file.  It returns true
888** if such a lock exists and false otherwise.
889**
890** The xFileControl() method is a generic interface that allows custom
891** VFS implementations to directly control an open file using the
892** [sqlite3_file_control()] interface.  The second "op" argument is an
893** integer opcode.  The third argument is a generic pointer intended to
894** point to a structure that may contain arguments or space in which to
895** write return values.  Potential uses for xFileControl() might be
896** functions to enable blocking locks with timeouts, to change the
897** locking strategy (for example to use dot-file locks), to inquire
898** about the status of a lock, or to break stale locks.  The SQLite
899** core reserves all opcodes less than 100 for its own use.
900** A [file control opcodes | list of opcodes] less than 100 is available.
901** Applications that define a custom xFileControl method should use opcodes
902** greater than 100 to avoid conflicts.  VFS implementations should
903** return [SQLITE_NOTFOUND] for file control opcodes that they do not
904** recognize.
905**
906** The xSectorSize() method returns the sector size of the
907** device that underlies the file.  The sector size is the
908** minimum write that can be performed without disturbing
909** other bytes in the file.  The xDeviceCharacteristics()
910** method returns a bit vector describing behaviors of the
911** underlying device:
912**
913** <ul>
914** <li> [SQLITE_IOCAP_ATOMIC]
915** <li> [SQLITE_IOCAP_ATOMIC512]
916** <li> [SQLITE_IOCAP_ATOMIC1K]
917** <li> [SQLITE_IOCAP_ATOMIC2K]
918** <li> [SQLITE_IOCAP_ATOMIC4K]
919** <li> [SQLITE_IOCAP_ATOMIC8K]
920** <li> [SQLITE_IOCAP_ATOMIC16K]
921** <li> [SQLITE_IOCAP_ATOMIC32K]
922** <li> [SQLITE_IOCAP_ATOMIC64K]
923** <li> [SQLITE_IOCAP_SAFE_APPEND]
924** <li> [SQLITE_IOCAP_SEQUENTIAL]
925** </ul>
926**
927** The SQLITE_IOCAP_ATOMIC property means that all writes of
928** any size are atomic.  The SQLITE_IOCAP_ATOMICnnn values
929** mean that writes of blocks that are nnn bytes in size and
930** are aligned to an address which is an integer multiple of
931** nnn are atomic.  The SQLITE_IOCAP_SAFE_APPEND value means
932** that when data is appended to a file, the data is appended
933** first then the size of the file is extended, never the other
934** way around.  The SQLITE_IOCAP_SEQUENTIAL property means that
935** information is written to disk in the same order as calls
936** to xWrite().
937**
938** If xRead() returns SQLITE_IOERR_SHORT_READ it must also fill
939** in the unread portions of the buffer with zeros.  A VFS that
940** fails to zero-fill short reads might seem to work.  However,
941** failure to zero-fill short reads will eventually lead to
942** database corruption.
943*/
944typedef struct sqlite3_io_methods sqlite3_io_methods;
945struct sqlite3_io_methods {
946  int iVersion;
947  int (*xClose)(sqlite3_file*);
948  int (*xRead)(sqlite3_file*, void*, int iAmt, sqlite3_int64 iOfst);
949  int (*xWrite)(sqlite3_file*, const void*, int iAmt, sqlite3_int64 iOfst);
950  int (*xTruncate)(sqlite3_file*, sqlite3_int64 size);
951  int (*xSync)(sqlite3_file*, int flags);
952  int (*xFileSize)(sqlite3_file*, sqlite3_int64 *pSize);
953  int (*xLock)(sqlite3_file*, int);
954  int (*xUnlock)(sqlite3_file*, int);
955  int (*xCheckReservedLock)(sqlite3_file*, int *pResOut);
956  int (*xFileControl)(sqlite3_file*, int op, void *pArg);
957  int (*xSectorSize)(sqlite3_file*);
958  int (*xDeviceCharacteristics)(sqlite3_file*);
959  /* Methods above are valid for version 1 */
960  int (*xShmMap)(sqlite3_file*, int iPg, int pgsz, int, void volatile**);
961  int (*xShmLock)(sqlite3_file*, int offset, int n, int flags);
962  void (*xShmBarrier)(sqlite3_file*);
963  int (*xShmUnmap)(sqlite3_file*, int deleteFlag);
964  /* Methods above are valid for version 2 */
965  int (*xFetch)(sqlite3_file*, sqlite3_int64 iOfst, int iAmt, void **pp);
966  int (*xUnfetch)(sqlite3_file*, sqlite3_int64 iOfst, void *p);
967  /* Methods above are valid for version 3 */
968  /* Additional methods may be added in future releases */
969};
970
971/*
972** CAPI3REF: Standard File Control Opcodes
973** KEYWORDS: {file control opcodes} {file control opcode}
974**
975** These integer constants are opcodes for the xFileControl method
976** of the [sqlite3_io_methods] object and for the [sqlite3_file_control()]
977** interface.
978**
979** <ul>
980** <li>[[SQLITE_FCNTL_LOCKSTATE]]
981** The [SQLITE_FCNTL_LOCKSTATE] opcode is used for debugging.  This
982** opcode causes the xFileControl method to write the current state of
983** the lock (one of [SQLITE_LOCK_NONE], [SQLITE_LOCK_SHARED],
984** [SQLITE_LOCK_RESERVED], [SQLITE_LOCK_PENDING], or [SQLITE_LOCK_EXCLUSIVE])
985** into an integer that the pArg argument points to. This capability
986** is used during testing and is only available when the SQLITE_TEST
987** compile-time option is used.
988**
989** <li>[[SQLITE_FCNTL_SIZE_HINT]]
990** The [SQLITE_FCNTL_SIZE_HINT] opcode is used by SQLite to give the VFS
991** layer a hint of how large the database file will grow to be during the
992** current transaction.  This hint is not guaranteed to be accurate but it
993** is often close.  The underlying VFS might choose to preallocate database
994** file space based on this hint in order to help writes to the database
995** file run faster.
996**
997** <li>[[SQLITE_FCNTL_CHUNK_SIZE]]
998** The [SQLITE_FCNTL_CHUNK_SIZE] opcode is used to request that the VFS
999** extends and truncates the database file in chunks of a size specified
1000** by the user. The fourth argument to [sqlite3_file_control()] should
1001** point to an integer (type int) containing the new chunk-size to use
1002** for the nominated database. Allocating database file space in large
1003** chunks (say 1MB at a time), may reduce file-system fragmentation and
1004** improve performance on some systems.
1005**
1006** <li>[[SQLITE_FCNTL_FILE_POINTER]]
1007** The [SQLITE_FCNTL_FILE_POINTER] opcode is used to obtain a pointer
1008** to the [sqlite3_file] object associated with a particular database
1009** connection.  See the [sqlite3_file_control()] documentation for
1010** additional information.
1011**
1012** <li>[[SQLITE_FCNTL_SYNC_OMITTED]]
1013** No longer in use.
1014**
1015** <li>[[SQLITE_FCNTL_SYNC]]
1016** The [SQLITE_FCNTL_SYNC] opcode is generated internally by SQLite and
1017** sent to the VFS immediately before the xSync method is invoked on a
1018** database file descriptor. Or, if the xSync method is not invoked
1019** because the user has configured SQLite with
1020** [PRAGMA synchronous | PRAGMA synchronous=OFF] it is invoked in place
1021** of the xSync method. In most cases, the pointer argument passed with
1022** this file-control is NULL. However, if the database file is being synced
1023** as part of a multi-database commit, the argument points to a nul-terminated
1024** string containing the transactions master-journal file name. VFSes that
1025** do not need this signal should silently ignore this opcode. Applications
1026** should not call [sqlite3_file_control()] with this opcode as doing so may
1027** disrupt the operation of the specialized VFSes that do require it.
1028**
1029** <li>[[SQLITE_FCNTL_COMMIT_PHASETWO]]
1030** The [SQLITE_FCNTL_COMMIT_PHASETWO] opcode is generated internally by SQLite
1031** and sent to the VFS after a transaction has been committed immediately
1032** but before the database is unlocked. VFSes that do not need this signal
1033** should silently ignore this opcode. Applications should not call
1034** [sqlite3_file_control()] with this opcode as doing so may disrupt the
1035** operation of the specialized VFSes that do require it.
1036**
1037** <li>[[SQLITE_FCNTL_WIN32_AV_RETRY]]
1038** ^The [SQLITE_FCNTL_WIN32_AV_RETRY] opcode is used to configure automatic
1039** retry counts and intervals for certain disk I/O operations for the
1040** windows [VFS] in order to provide robustness in the presence of
1041** anti-virus programs.  By default, the windows VFS will retry file read,
1042** file write, and file delete operations up to 10 times, with a delay
1043** of 25 milliseconds before the first retry and with the delay increasing
1044** by an additional 25 milliseconds with each subsequent retry.  This
1045** opcode allows these two values (10 retries and 25 milliseconds of delay)
1046** to be adjusted.  The values are changed for all database connections
1047** within the same process.  The argument is a pointer to an array of two
1048** integers where the first integer i the new retry count and the second
1049** integer is the delay.  If either integer is negative, then the setting
1050** is not changed but instead the prior value of that setting is written
1051** into the array entry, allowing the current retry settings to be
1052** interrogated.  The zDbName parameter is ignored.
1053**
1054** <li>[[SQLITE_FCNTL_PERSIST_WAL]]
1055** ^The [SQLITE_FCNTL_PERSIST_WAL] opcode is used to set or query the
1056** persistent [WAL | Write Ahead Log] setting.  By default, the auxiliary
1057** write ahead log and shared memory files used for transaction control
1058** are automatically deleted when the latest connection to the database
1059** closes.  Setting persistent WAL mode causes those files to persist after
1060** close.  Persisting the files is useful when other processes that do not
1061** have write permission on the directory containing the database file want
1062** to read the database file, as the WAL and shared memory files must exist
1063** in order for the database to be readable.  The fourth parameter to
1064** [sqlite3_file_control()] for this opcode should be a pointer to an integer.
1065** That integer is 0 to disable persistent WAL mode or 1 to enable persistent
1066** WAL mode.  If the integer is -1, then it is overwritten with the current
1067** WAL persistence setting.
1068**
1069** <li>[[SQLITE_FCNTL_POWERSAFE_OVERWRITE]]
1070** ^The [SQLITE_FCNTL_POWERSAFE_OVERWRITE] opcode is used to set or query the
1071** persistent "powersafe-overwrite" or "PSOW" setting.  The PSOW setting
1072** determines the [SQLITE_IOCAP_POWERSAFE_OVERWRITE] bit of the
1073** xDeviceCharacteristics methods. The fourth parameter to
1074** [sqlite3_file_control()] for this opcode should be a pointer to an integer.
1075** That integer is 0 to disable zero-damage mode or 1 to enable zero-damage
1076** mode.  If the integer is -1, then it is overwritten with the current
1077** zero-damage mode setting.
1078**
1079** <li>[[SQLITE_FCNTL_OVERWRITE]]
1080** ^The [SQLITE_FCNTL_OVERWRITE] opcode is invoked by SQLite after opening
1081** a write transaction to indicate that, unless it is rolled back for some
1082** reason, the entire database file will be overwritten by the current
1083** transaction. This is used by VACUUM operations.
1084**
1085** <li>[[SQLITE_FCNTL_VFSNAME]]
1086** ^The [SQLITE_FCNTL_VFSNAME] opcode can be used to obtain the names of
1087** all [VFSes] in the VFS stack.  The names are of all VFS shims and the
1088** final bottom-level VFS are written into memory obtained from
1089** [sqlite3_malloc()] and the result is stored in the char* variable
1090** that the fourth parameter of [sqlite3_file_control()] points to.
1091** The caller is responsible for freeing the memory when done.  As with
1092** all file-control actions, there is no guarantee that this will actually
1093** do anything.  Callers should initialize the char* variable to a NULL
1094** pointer in case this file-control is not implemented.  This file-control
1095** is intended for diagnostic use only.
1096**
1097** <li>[[SQLITE_FCNTL_PRAGMA]]
1098** ^Whenever a [PRAGMA] statement is parsed, an [SQLITE_FCNTL_PRAGMA]
1099** file control is sent to the open [sqlite3_file] object corresponding
1100** to the database file to which the pragma statement refers. ^The argument
1101** to the [SQLITE_FCNTL_PRAGMA] file control is an array of
1102** pointers to strings (char**) in which the second element of the array
1103** is the name of the pragma and the third element is the argument to the
1104** pragma or NULL if the pragma has no argument.  ^The handler for an
1105** [SQLITE_FCNTL_PRAGMA] file control can optionally make the first element
1106** of the char** argument point to a string obtained from [sqlite3_mprintf()]
1107** or the equivalent and that string will become the result of the pragma or
1108** the error message if the pragma fails. ^If the
1109** [SQLITE_FCNTL_PRAGMA] file control returns [SQLITE_NOTFOUND], then normal
1110** [PRAGMA] processing continues.  ^If the [SQLITE_FCNTL_PRAGMA]
1111** file control returns [SQLITE_OK], then the parser assumes that the
1112** VFS has handled the PRAGMA itself and the parser generates a no-op
1113** prepared statement if result string is NULL, or that returns a copy
1114** of the result string if the string is non-NULL.
1115** ^If the [SQLITE_FCNTL_PRAGMA] file control returns
1116** any result code other than [SQLITE_OK] or [SQLITE_NOTFOUND], that means
1117** that the VFS encountered an error while handling the [PRAGMA] and the
1118** compilation of the PRAGMA fails with an error.  ^The [SQLITE_FCNTL_PRAGMA]
1119** file control occurs at the beginning of pragma statement analysis and so
1120** it is able to override built-in [PRAGMA] statements.
1121**
1122** <li>[[SQLITE_FCNTL_BUSYHANDLER]]
1123** ^The [SQLITE_FCNTL_BUSYHANDLER]
1124** file-control may be invoked by SQLite on the database file handle
1125** shortly after it is opened in order to provide a custom VFS with access
1126** to the connections busy-handler callback. The argument is of type (void **)
1127** - an array of two (void *) values. The first (void *) actually points
1128** to a function of type (int (*)(void *)). In order to invoke the connections
1129** busy-handler, this function should be invoked with the second (void *) in
1130** the array as the only argument. If it returns non-zero, then the operation
1131** should be retried. If it returns zero, the custom VFS should abandon the
1132** current operation.
1133**
1134** <li>[[SQLITE_FCNTL_TEMPFILENAME]]
1135** ^Application can invoke the [SQLITE_FCNTL_TEMPFILENAME] file-control
1136** to have SQLite generate a
1137** temporary filename using the same algorithm that is followed to generate
1138** temporary filenames for TEMP tables and other internal uses.  The
1139** argument should be a char** which will be filled with the filename
1140** written into memory obtained from [sqlite3_malloc()].  The caller should
1141** invoke [sqlite3_free()] on the result to avoid a memory leak.
1142**
1143** <li>[[SQLITE_FCNTL_MMAP_SIZE]]
1144** The [SQLITE_FCNTL_MMAP_SIZE] file control is used to query or set the
1145** maximum number of bytes that will be used for memory-mapped I/O.
1146** The argument is a pointer to a value of type sqlite3_int64 that
1147** is an advisory maximum number of bytes in the file to memory map.  The
1148** pointer is overwritten with the old value.  The limit is not changed if
1149** the value originally pointed to is negative, and so the current limit
1150** can be queried by passing in a pointer to a negative number.  This
1151** file-control is used internally to implement [PRAGMA mmap_size].
1152**
1153** <li>[[SQLITE_FCNTL_TRACE]]
1154** The [SQLITE_FCNTL_TRACE] file control provides advisory information
1155** to the VFS about what the higher layers of the SQLite stack are doing.
1156** This file control is used by some VFS activity tracing [shims].
1157** The argument is a zero-terminated string.  Higher layers in the
1158** SQLite stack may generate instances of this file control if
1159** the [SQLITE_USE_FCNTL_TRACE] compile-time option is enabled.
1160**
1161** <li>[[SQLITE_FCNTL_HAS_MOVED]]
1162** The [SQLITE_FCNTL_HAS_MOVED] file control interprets its argument as a
1163** pointer to an integer and it writes a boolean into that integer depending
1164** on whether or not the file has been renamed, moved, or deleted since it
1165** was first opened.
1166**
1167** <li>[[SQLITE_FCNTL_WIN32_SET_HANDLE]]
1168** The [SQLITE_FCNTL_WIN32_SET_HANDLE] opcode is used for debugging.  This
1169** opcode causes the xFileControl method to swap the file handle with the one
1170** pointed to by the pArg argument.  This capability is used during testing
1171** and only needs to be supported when SQLITE_TEST is defined.
1172**
1173** <li>[[SQLITE_FCNTL_WAL_BLOCK]]
1174** The [SQLITE_FCNTL_WAL_BLOCK] is a signal to the VFS layer that it might
1175** be advantageous to block on the next WAL lock if the lock is not immediately
1176** available.  The WAL subsystem issues this signal during rare
1177** circumstances in order to fix a problem with priority inversion.
1178** Applications should <em>not</em> use this file-control.
1179**
1180** <li>[[SQLITE_FCNTL_ZIPVFS]]
1181** The [SQLITE_FCNTL_ZIPVFS] opcode is implemented by zipvfs only. All other
1182** VFS should return SQLITE_NOTFOUND for this opcode.
1183**
1184** <li>[[SQLITE_FCNTL_RBU]]
1185** The [SQLITE_FCNTL_RBU] opcode is implemented by the special VFS used by
1186** the RBU extension only.  All other VFS should return SQLITE_NOTFOUND for
1187** this opcode.
1188** </ul>
1189*/
1190#define SQLITE_FCNTL_LOCKSTATE               1
1191#define SQLITE_FCNTL_GET_LOCKPROXYFILE       2
1192#define SQLITE_FCNTL_SET_LOCKPROXYFILE       3
1193#define SQLITE_FCNTL_LAST_ERRNO              4
1194#define SQLITE_FCNTL_SIZE_HINT               5
1195#define SQLITE_FCNTL_CHUNK_SIZE              6
1196#define SQLITE_FCNTL_FILE_POINTER            7
1197#define SQLITE_FCNTL_SYNC_OMITTED            8
1198#define SQLITE_FCNTL_WIN32_AV_RETRY          9
1199#define SQLITE_FCNTL_PERSIST_WAL            10
1200#define SQLITE_FCNTL_OVERWRITE              11
1201#define SQLITE_FCNTL_VFSNAME                12
1202#define SQLITE_FCNTL_POWERSAFE_OVERWRITE    13
1203#define SQLITE_FCNTL_PRAGMA                 14
1204#define SQLITE_FCNTL_BUSYHANDLER            15
1205#define SQLITE_FCNTL_TEMPFILENAME           16
1206#define SQLITE_FCNTL_MMAP_SIZE              18
1207#define SQLITE_FCNTL_TRACE                  19
1208#define SQLITE_FCNTL_HAS_MOVED              20
1209#define SQLITE_FCNTL_SYNC                   21
1210#define SQLITE_FCNTL_COMMIT_PHASETWO        22
1211#define SQLITE_FCNTL_WIN32_SET_HANDLE       23
1212#define SQLITE_FCNTL_WAL_BLOCK              24
1213#define SQLITE_FCNTL_ZIPVFS                 25
1214#define SQLITE_FCNTL_RBU                    26
1215
1216/* deprecated names */
1217#define SQLITE_GET_LOCKPROXYFILE      SQLITE_FCNTL_GET_LOCKPROXYFILE
1218#define SQLITE_SET_LOCKPROXYFILE      SQLITE_FCNTL_SET_LOCKPROXYFILE
1219#define SQLITE_LAST_ERRNO             SQLITE_FCNTL_LAST_ERRNO
1220
1221
1222/*
1223** CAPI3REF: Mutex Handle
1224**
1225** The mutex module within SQLite defines [sqlite3_mutex] to be an
1226** abstract type for a mutex object.  The SQLite core never looks
1227** at the internal representation of an [sqlite3_mutex].  It only
1228** deals with pointers to the [sqlite3_mutex] object.
1229**
1230** Mutexes are created using [sqlite3_mutex_alloc()].
1231*/
1232typedef struct sqlite3_mutex sqlite3_mutex;
1233
1234/*
1235** CAPI3REF: OS Interface Object
1236**
1237** An instance of the sqlite3_vfs object defines the interface between
1238** the SQLite core and the underlying operating system.  The "vfs"
1239** in the name of the object stands for "virtual file system".  See
1240** the [VFS | VFS documentation] for further information.
1241**
1242** The value of the iVersion field is initially 1 but may be larger in
1243** future versions of SQLite.  Additional fields may be appended to this
1244** object when the iVersion value is increased.  Note that the structure
1245** of the sqlite3_vfs object changes in the transaction between
1246** SQLite version 3.5.9 and 3.6.0 and yet the iVersion field was not
1247** modified.
1248**
1249** The szOsFile field is the size of the subclassed [sqlite3_file]
1250** structure used by this VFS.  mxPathname is the maximum length of
1251** a pathname in this VFS.
1252**
1253** Registered sqlite3_vfs objects are kept on a linked list formed by
1254** the pNext pointer.  The [sqlite3_vfs_register()]
1255** and [sqlite3_vfs_unregister()] interfaces manage this list
1256** in a thread-safe way.  The [sqlite3_vfs_find()] interface
1257** searches the list.  Neither the application code nor the VFS
1258** implementation should use the pNext pointer.
1259**
1260** The pNext field is the only field in the sqlite3_vfs
1261** structure that SQLite will ever modify.  SQLite will only access
1262** or modify this field while holding a particular static mutex.
1263** The application should never modify anything within the sqlite3_vfs
1264** object once the object has been registered.
1265**
1266** The zName field holds the name of the VFS module.  The name must
1267** be unique across all VFS modules.
1268**
1269** [[sqlite3_vfs.xOpen]]
1270** ^SQLite guarantees that the zFilename parameter to xOpen
1271** is either a NULL pointer or string obtained
1272** from xFullPathname() with an optional suffix added.
1273** ^If a suffix is added to the zFilename parameter, it will
1274** consist of a single "-" character followed by no more than
1275** 11 alphanumeric and/or "-" characters.
1276** ^SQLite further guarantees that
1277** the string will be valid and unchanged until xClose() is
1278** called. Because of the previous sentence,
1279** the [sqlite3_file] can safely store a pointer to the
1280** filename if it needs to remember the filename for some reason.
1281** If the zFilename parameter to xOpen is a NULL pointer then xOpen
1282** must invent its own temporary name for the file.  ^Whenever the
1283** xFilename parameter is NULL it will also be the case that the
1284** flags parameter will include [SQLITE_OPEN_DELETEONCLOSE].
1285**
1286** The flags argument to xOpen() includes all bits set in
1287** the flags argument to [sqlite3_open_v2()].  Or if [sqlite3_open()]
1288** or [sqlite3_open16()] is used, then flags includes at least
1289** [SQLITE_OPEN_READWRITE] | [SQLITE_OPEN_CREATE].
1290** If xOpen() opens a file read-only then it sets *pOutFlags to
1291** include [SQLITE_OPEN_READONLY].  Other bits in *pOutFlags may be set.
1292**
1293** ^(SQLite will also add one of the following flags to the xOpen()
1294** call, depending on the object being opened:
1295**
1296** <ul>
1297** <li>  [SQLITE_OPEN_MAIN_DB]
1298** <li>  [SQLITE_OPEN_MAIN_JOURNAL]
1299** <li>  [SQLITE_OPEN_TEMP_DB]
1300** <li>  [SQLITE_OPEN_TEMP_JOURNAL]
1301** <li>  [SQLITE_OPEN_TRANSIENT_DB]
1302** <li>  [SQLITE_OPEN_SUBJOURNAL]
1303** <li>  [SQLITE_OPEN_MASTER_JOURNAL]
1304** <li>  [SQLITE_OPEN_WAL]
1305** </ul>)^
1306**
1307** The file I/O implementation can use the object type flags to
1308** change the way it deals with files.  For example, an application
1309** that does not care about crash recovery or rollback might make
1310** the open of a journal file a no-op.  Writes to this journal would
1311** also be no-ops, and any attempt to read the journal would return
1312** SQLITE_IOERR.  Or the implementation might recognize that a database
1313** file will be doing page-aligned sector reads and writes in a random
1314** order and set up its I/O subsystem accordingly.
1315**
1316** SQLite might also add one of the following flags to the xOpen method:
1317**
1318** <ul>
1319** <li> [SQLITE_OPEN_DELETEONCLOSE]
1320** <li> [SQLITE_OPEN_EXCLUSIVE]
1321** </ul>
1322**
1323** The [SQLITE_OPEN_DELETEONCLOSE] flag means the file should be
1324** deleted when it is closed.  ^The [SQLITE_OPEN_DELETEONCLOSE]
1325** will be set for TEMP databases and their journals, transient
1326** databases, and subjournals.
1327**
1328** ^The [SQLITE_OPEN_EXCLUSIVE] flag is always used in conjunction
1329** with the [SQLITE_OPEN_CREATE] flag, which are both directly
1330** analogous to the O_EXCL and O_CREAT flags of the POSIX open()
1331** API.  The SQLITE_OPEN_EXCLUSIVE flag, when paired with the
1332** SQLITE_OPEN_CREATE, is used to indicate that file should always
1333** be created, and that it is an error if it already exists.
1334** It is <i>not</i> used to indicate the file should be opened
1335** for exclusive access.
1336**
1337** ^At least szOsFile bytes of memory are allocated by SQLite
1338** to hold the  [sqlite3_file] structure passed as the third
1339** argument to xOpen.  The xOpen method does not have to
1340** allocate the structure; it should just fill it in.  Note that
1341** the xOpen method must set the sqlite3_file.pMethods to either
1342** a valid [sqlite3_io_methods] object or to NULL.  xOpen must do
1343** this even if the open fails.  SQLite expects that the sqlite3_file.pMethods
1344** element will be valid after xOpen returns regardless of the success
1345** or failure of the xOpen call.
1346**
1347** [[sqlite3_vfs.xAccess]]
1348** ^The flags argument to xAccess() may be [SQLITE_ACCESS_EXISTS]
1349** to test for the existence of a file, or [SQLITE_ACCESS_READWRITE] to
1350** test whether a file is readable and writable, or [SQLITE_ACCESS_READ]
1351** to test whether a file is at least readable.   The file can be a
1352** directory.
1353**
1354** ^SQLite will always allocate at least mxPathname+1 bytes for the
1355** output buffer xFullPathname.  The exact size of the output buffer
1356** is also passed as a parameter to both  methods. If the output buffer
1357** is not large enough, [SQLITE_CANTOPEN] should be returned. Since this is
1358** handled as a fatal error by SQLite, vfs implementations should endeavor
1359** to prevent this by setting mxPathname to a sufficiently large value.
1360**
1361** The xRandomness(), xSleep(), xCurrentTime(), and xCurrentTimeInt64()
1362** interfaces are not strictly a part of the filesystem, but they are
1363** included in the VFS structure for completeness.
1364** The xRandomness() function attempts to return nBytes bytes
1365** of good-quality randomness into zOut.  The return value is
1366** the actual number of bytes of randomness obtained.
1367** The xSleep() method causes the calling thread to sleep for at
1368** least the number of microseconds given.  ^The xCurrentTime()
1369** method returns a Julian Day Number for the current date and time as
1370** a floating point value.
1371** ^The xCurrentTimeInt64() method returns, as an integer, the Julian
1372** Day Number multiplied by 86400000 (the number of milliseconds in
1373** a 24-hour day).
1374** ^SQLite will use the xCurrentTimeInt64() method to get the current
1375** date and time if that method is available (if iVersion is 2 or
1376** greater and the function pointer is not NULL) and will fall back
1377** to xCurrentTime() if xCurrentTimeInt64() is unavailable.
1378**
1379** ^The xSetSystemCall(), xGetSystemCall(), and xNestSystemCall() interfaces
1380** are not used by the SQLite core.  These optional interfaces are provided
1381** by some VFSes to facilitate testing of the VFS code. By overriding
1382** system calls with functions under its control, a test program can
1383** simulate faults and error conditions that would otherwise be difficult
1384** or impossible to induce.  The set of system calls that can be overridden
1385** varies from one VFS to another, and from one version of the same VFS to the
1386** next.  Applications that use these interfaces must be prepared for any
1387** or all of these interfaces to be NULL or for their behavior to change
1388** from one release to the next.  Applications must not attempt to access
1389** any of these methods if the iVersion of the VFS is less than 3.
1390*/
1391typedef struct sqlite3_vfs sqlite3_vfs;
1392typedef void (*sqlite3_syscall_ptr)(void);
1393struct sqlite3_vfs {
1394  int iVersion;            /* Structure version number (currently 3) */
1395  int szOsFile;            /* Size of subclassed sqlite3_file */
1396  int mxPathname;          /* Maximum file pathname length */
1397  sqlite3_vfs *pNext;      /* Next registered VFS */
1398  const char *zName;       /* Name of this virtual file system */
1399  void *pAppData;          /* Pointer to application-specific data */
1400  int (*xOpen)(sqlite3_vfs*, const char *zName, sqlite3_file*,
1401               int flags, int *pOutFlags);
1402  int (*xDelete)(sqlite3_vfs*, const char *zName, int syncDir);
1403  int (*xAccess)(sqlite3_vfs*, const char *zName, int flags, int *pResOut);
1404  int (*xFullPathname)(sqlite3_vfs*, const char *zName, int nOut, char *zOut);
1405  void *(*xDlOpen)(sqlite3_vfs*, const char *zFilename);
1406  void (*xDlError)(sqlite3_vfs*, int nByte, char *zErrMsg);
1407  void (*(*xDlSym)(sqlite3_vfs*,void*, const char *zSymbol))(void);
1408  void (*xDlClose)(sqlite3_vfs*, void*);
1409  int (*xRandomness)(sqlite3_vfs*, int nByte, char *zOut);
1410  int (*xSleep)(sqlite3_vfs*, int microseconds);
1411  int (*xCurrentTime)(sqlite3_vfs*, double*);
1412  int (*xGetLastError)(sqlite3_vfs*, int, char *);
1413  /*
1414  ** The methods above are in version 1 of the sqlite_vfs object
1415  ** definition.  Those that follow are added in version 2 or later
1416  */
1417  int (*xCurrentTimeInt64)(sqlite3_vfs*, sqlite3_int64*);
1418  /*
1419  ** The methods above are in versions 1 and 2 of the sqlite_vfs object.
1420  ** Those below are for version 3 and greater.
1421  */
1422  int (*xSetSystemCall)(sqlite3_vfs*, const char *zName, sqlite3_syscall_ptr);
1423  sqlite3_syscall_ptr (*xGetSystemCall)(sqlite3_vfs*, const char *zName);
1424  const char *(*xNextSystemCall)(sqlite3_vfs*, const char *zName);
1425  /*
1426  ** The methods above are in versions 1 through 3 of the sqlite_vfs object.
1427  ** New fields may be appended in figure versions.  The iVersion
1428  ** value will increment whenever this happens.
1429  */
1430};
1431
1432/*
1433** CAPI3REF: Flags for the xAccess VFS method
1434**
1435** These integer constants can be used as the third parameter to
1436** the xAccess method of an [sqlite3_vfs] object.  They determine
1437** what kind of permissions the xAccess method is looking for.
1438** With SQLITE_ACCESS_EXISTS, the xAccess method
1439** simply checks whether the file exists.
1440** With SQLITE_ACCESS_READWRITE, the xAccess method
1441** checks whether the named directory is both readable and writable
1442** (in other words, if files can be added, removed, and renamed within
1443** the directory).
1444** The SQLITE_ACCESS_READWRITE constant is currently used only by the
1445** [temp_store_directory pragma], though this could change in a future
1446** release of SQLite.
1447** With SQLITE_ACCESS_READ, the xAccess method
1448** checks whether the file is readable.  The SQLITE_ACCESS_READ constant is
1449** currently unused, though it might be used in a future release of
1450** SQLite.
1451*/
1452#define SQLITE_ACCESS_EXISTS    0
1453#define SQLITE_ACCESS_READWRITE 1   /* Used by PRAGMA temp_store_directory */
1454#define SQLITE_ACCESS_READ      2   /* Unused */
1455
1456/*
1457** CAPI3REF: Flags for the xShmLock VFS method
1458**
1459** These integer constants define the various locking operations
1460** allowed by the xShmLock method of [sqlite3_io_methods].  The
1461** following are the only legal combinations of flags to the
1462** xShmLock method:
1463**
1464** <ul>
1465** <li>  SQLITE_SHM_LOCK | SQLITE_SHM_SHARED
1466** <li>  SQLITE_SHM_LOCK | SQLITE_SHM_EXCLUSIVE
1467** <li>  SQLITE_SHM_UNLOCK | SQLITE_SHM_SHARED
1468** <li>  SQLITE_SHM_UNLOCK | SQLITE_SHM_EXCLUSIVE
1469** </ul>
1470**
1471** When unlocking, the same SHARED or EXCLUSIVE flag must be supplied as
1472** was given on the corresponding lock.
1473**
1474** The xShmLock method can transition between unlocked and SHARED or
1475** between unlocked and EXCLUSIVE.  It cannot transition between SHARED
1476** and EXCLUSIVE.
1477*/
1478#define SQLITE_SHM_UNLOCK       1
1479#define SQLITE_SHM_LOCK         2
1480#define SQLITE_SHM_SHARED       4
1481#define SQLITE_SHM_EXCLUSIVE    8
1482
1483/*
1484** CAPI3REF: Maximum xShmLock index
1485**
1486** The xShmLock method on [sqlite3_io_methods] may use values
1487** between 0 and this upper bound as its "offset" argument.
1488** The SQLite core will never attempt to acquire or release a
1489** lock outside of this range
1490*/
1491#define SQLITE_SHM_NLOCK        8
1492
1493
1494/*
1495** CAPI3REF: Initialize The SQLite Library
1496**
1497** ^The sqlite3_initialize() routine initializes the
1498** SQLite library.  ^The sqlite3_shutdown() routine
1499** deallocates any resources that were allocated by sqlite3_initialize().
1500** These routines are designed to aid in process initialization and
1501** shutdown on embedded systems.  Workstation applications using
1502** SQLite normally do not need to invoke either of these routines.
1503**
1504** A call to sqlite3_initialize() is an "effective" call if it is
1505** the first time sqlite3_initialize() is invoked during the lifetime of
1506** the process, or if it is the first time sqlite3_initialize() is invoked
1507** following a call to sqlite3_shutdown().  ^(Only an effective call
1508** of sqlite3_initialize() does any initialization.  All other calls
1509** are harmless no-ops.)^
1510**
1511** A call to sqlite3_shutdown() is an "effective" call if it is the first
1512** call to sqlite3_shutdown() since the last sqlite3_initialize().  ^(Only
1513** an effective call to sqlite3_shutdown() does any deinitialization.
1514** All other valid calls to sqlite3_shutdown() are harmless no-ops.)^
1515**
1516** The sqlite3_initialize() interface is threadsafe, but sqlite3_shutdown()
1517** is not.  The sqlite3_shutdown() interface must only be called from a
1518** single thread.  All open [database connections] must be closed and all
1519** other SQLite resources must be deallocated prior to invoking
1520** sqlite3_shutdown().
1521**
1522** Among other things, ^sqlite3_initialize() will invoke
1523** sqlite3_os_init().  Similarly, ^sqlite3_shutdown()
1524** will invoke sqlite3_os_end().
1525**
1526** ^The sqlite3_initialize() routine returns [SQLITE_OK] on success.
1527** ^If for some reason, sqlite3_initialize() is unable to initialize
1528** the library (perhaps it is unable to allocate a needed resource such
1529** as a mutex) it returns an [error code] other than [SQLITE_OK].
1530**
1531** ^The sqlite3_initialize() routine is called internally by many other
1532** SQLite interfaces so that an application usually does not need to
1533** invoke sqlite3_initialize() directly.  For example, [sqlite3_open()]
1534** calls sqlite3_initialize() so the SQLite library will be automatically
1535** initialized when [sqlite3_open()] is called if it has not be initialized
1536** already.  ^However, if SQLite is compiled with the [SQLITE_OMIT_AUTOINIT]
1537** compile-time option, then the automatic calls to sqlite3_initialize()
1538** are omitted and the application must call sqlite3_initialize() directly
1539** prior to using any other SQLite interface.  For maximum portability,
1540** it is recommended that applications always invoke sqlite3_initialize()
1541** directly prior to using any other SQLite interface.  Future releases
1542** of SQLite may require this.  In other words, the behavior exhibited
1543** when SQLite is compiled with [SQLITE_OMIT_AUTOINIT] might become the
1544** default behavior in some future release of SQLite.
1545**
1546** The sqlite3_os_init() routine does operating-system specific
1547** initialization of the SQLite library.  The sqlite3_os_end()
1548** routine undoes the effect of sqlite3_os_init().  Typical tasks
1549** performed by these routines include allocation or deallocation
1550** of static resources, initialization of global variables,
1551** setting up a default [sqlite3_vfs] module, or setting up
1552** a default configuration using [sqlite3_config()].
1553**
1554** The application should never invoke either sqlite3_os_init()
1555** or sqlite3_os_end() directly.  The application should only invoke
1556** sqlite3_initialize() and sqlite3_shutdown().  The sqlite3_os_init()
1557** interface is called automatically by sqlite3_initialize() and
1558** sqlite3_os_end() is called by sqlite3_shutdown().  Appropriate
1559** implementations for sqlite3_os_init() and sqlite3_os_end()
1560** are built into SQLite when it is compiled for Unix, Windows, or OS/2.
1561** When [custom builds | built for other platforms]
1562** (using the [SQLITE_OS_OTHER=1] compile-time
1563** option) the application must supply a suitable implementation for
1564** sqlite3_os_init() and sqlite3_os_end().  An application-supplied
1565** implementation of sqlite3_os_init() or sqlite3_os_end()
1566** must return [SQLITE_OK] on success and some other [error code] upon
1567** failure.
1568*/
1569SQLITE_API int SQLITE_STDCALL sqlite3_initialize(void);
1570SQLITE_API int SQLITE_STDCALL sqlite3_shutdown(void);
1571SQLITE_API int SQLITE_STDCALL sqlite3_os_init(void);
1572SQLITE_API int SQLITE_STDCALL sqlite3_os_end(void);
1573
1574/*
1575** CAPI3REF: Configuring The SQLite Library
1576**
1577** The sqlite3_config() interface is used to make global configuration
1578** changes to SQLite in order to tune SQLite to the specific needs of
1579** the application.  The default configuration is recommended for most
1580** applications and so this routine is usually not necessary.  It is
1581** provided to support rare applications with unusual needs.
1582**
1583** The sqlite3_config() interface is not threadsafe.  The application
1584** must insure that no other SQLite interfaces are invoked by other
1585** threads while sqlite3_config() is running.  Furthermore, sqlite3_config()
1586** may only be invoked prior to library initialization using
1587** [sqlite3_initialize()] or after shutdown by [sqlite3_shutdown()].
1588** ^If sqlite3_config() is called after [sqlite3_initialize()] and before
1589** [sqlite3_shutdown()] then it will return SQLITE_MISUSE.
1590** Note, however, that ^sqlite3_config() can be called as part of the
1591** implementation of an application-defined [sqlite3_os_init()].
1592**
1593** The first argument to sqlite3_config() is an integer
1594** [configuration option] that determines
1595** what property of SQLite is to be configured.  Subsequent arguments
1596** vary depending on the [configuration option]
1597** in the first argument.
1598**
1599** ^When a configuration option is set, sqlite3_config() returns [SQLITE_OK].
1600** ^If the option is unknown or SQLite is unable to set the option
1601** then this routine returns a non-zero [error code].
1602*/
1603SQLITE_API int SQLITE_CDECL sqlite3_config(int, ...);
1604
1605/*
1606** CAPI3REF: Configure database connections
1607** METHOD: sqlite3
1608**
1609** The sqlite3_db_config() interface is used to make configuration
1610** changes to a [database connection].  The interface is similar to
1611** [sqlite3_config()] except that the changes apply to a single
1612** [database connection] (specified in the first argument).
1613**
1614** The second argument to sqlite3_db_config(D,V,...)  is the
1615** [SQLITE_DBCONFIG_LOOKASIDE | configuration verb] - an integer code
1616** that indicates what aspect of the [database connection] is being configured.
1617** Subsequent arguments vary depending on the configuration verb.
1618**
1619** ^Calls to sqlite3_db_config() return SQLITE_OK if and only if
1620** the call is considered successful.
1621*/
1622SQLITE_API int SQLITE_CDECL sqlite3_db_config(sqlite3*, int op, ...);
1623
1624/*
1625** CAPI3REF: Memory Allocation Routines
1626**
1627** An instance of this object defines the interface between SQLite
1628** and low-level memory allocation routines.
1629**
1630** This object is used in only one place in the SQLite interface.
1631** A pointer to an instance of this object is the argument to
1632** [sqlite3_config()] when the configuration option is
1633** [SQLITE_CONFIG_MALLOC] or [SQLITE_CONFIG_GETMALLOC].
1634** By creating an instance of this object
1635** and passing it to [sqlite3_config]([SQLITE_CONFIG_MALLOC])
1636** during configuration, an application can specify an alternative
1637** memory allocation subsystem for SQLite to use for all of its
1638** dynamic memory needs.
1639**
1640** Note that SQLite comes with several [built-in memory allocators]
1641** that are perfectly adequate for the overwhelming majority of applications
1642** and that this object is only useful to a tiny minority of applications
1643** with specialized memory allocation requirements.  This object is
1644** also used during testing of SQLite in order to specify an alternative
1645** memory allocator that simulates memory out-of-memory conditions in
1646** order to verify that SQLite recovers gracefully from such
1647** conditions.
1648**
1649** The xMalloc, xRealloc, and xFree methods must work like the
1650** malloc(), realloc() and free() functions from the standard C library.
1651** ^SQLite guarantees that the second argument to
1652** xRealloc is always a value returned by a prior call to xRoundup.
1653**
1654** xSize should return the allocated size of a memory allocation
1655** previously obtained from xMalloc or xRealloc.  The allocated size
1656** is always at least as big as the requested size but may be larger.
1657**
1658** The xRoundup method returns what would be the allocated size of
1659** a memory allocation given a particular requested size.  Most memory
1660** allocators round up memory allocations at least to the next multiple
1661** of 8.  Some allocators round up to a larger multiple or to a power of 2.
1662** Every memory allocation request coming in through [sqlite3_malloc()]
1663** or [sqlite3_realloc()] first calls xRoundup.  If xRoundup returns 0,
1664** that causes the corresponding memory allocation to fail.
1665**
1666** The xInit method initializes the memory allocator.  For example,
1667** it might allocate any require mutexes or initialize internal data
1668** structures.  The xShutdown method is invoked (indirectly) by
1669** [sqlite3_shutdown()] and should deallocate any resources acquired
1670** by xInit.  The pAppData pointer is used as the only parameter to
1671** xInit and xShutdown.
1672**
1673** SQLite holds the [SQLITE_MUTEX_STATIC_MASTER] mutex when it invokes
1674** the xInit method, so the xInit method need not be threadsafe.  The
1675** xShutdown method is only called from [sqlite3_shutdown()] so it does
1676** not need to be threadsafe either.  For all other methods, SQLite
1677** holds the [SQLITE_MUTEX_STATIC_MEM] mutex as long as the
1678** [SQLITE_CONFIG_MEMSTATUS] configuration option is turned on (which
1679** it is by default) and so the methods are automatically serialized.
1680** However, if [SQLITE_CONFIG_MEMSTATUS] is disabled, then the other
1681** methods must be threadsafe or else make their own arrangements for
1682** serialization.
1683**
1684** SQLite will never invoke xInit() more than once without an intervening
1685** call to xShutdown().
1686*/
1687typedef struct sqlite3_mem_methods sqlite3_mem_methods;
1688struct sqlite3_mem_methods {
1689  void *(*xMalloc)(int);         /* Memory allocation function */
1690  void (*xFree)(void*);          /* Free a prior allocation */
1691  void *(*xRealloc)(void*,int);  /* Resize an allocation */
1692  int (*xSize)(void*);           /* Return the size of an allocation */
1693  int (*xRoundup)(int);          /* Round up request size to allocation size */
1694  int (*xInit)(void*);           /* Initialize the memory allocator */
1695  void (*xShutdown)(void*);      /* Deinitialize the memory allocator */
1696  void *pAppData;                /* Argument to xInit() and xShutdown() */
1697};
1698
1699/*
1700** CAPI3REF: Configuration Options
1701** KEYWORDS: {configuration option}
1702**
1703** These constants are the available integer configuration options that
1704** can be passed as the first argument to the [sqlite3_config()] interface.
1705**
1706** New configuration options may be added in future releases of SQLite.
1707** Existing configuration options might be discontinued.  Applications
1708** should check the return code from [sqlite3_config()] to make sure that
1709** the call worked.  The [sqlite3_config()] interface will return a
1710** non-zero [error code] if a discontinued or unsupported configuration option
1711** is invoked.
1712**
1713** <dl>
1714** [[SQLITE_CONFIG_SINGLETHREAD]] <dt>SQLITE_CONFIG_SINGLETHREAD</dt>
1715** <dd>There are no arguments to this option.  ^This option sets the
1716** [threading mode] to Single-thread.  In other words, it disables
1717** all mutexing and puts SQLite into a mode where it can only be used
1718** by a single thread.   ^If SQLite is compiled with
1719** the [SQLITE_THREADSAFE | SQLITE_THREADSAFE=0] compile-time option then
1720** it is not possible to change the [threading mode] from its default
1721** value of Single-thread and so [sqlite3_config()] will return
1722** [SQLITE_ERROR] if called with the SQLITE_CONFIG_SINGLETHREAD
1723** configuration option.</dd>
1724**
1725** [[SQLITE_CONFIG_MULTITHREAD]] <dt>SQLITE_CONFIG_MULTITHREAD</dt>
1726** <dd>There are no arguments to this option.  ^This option sets the
1727** [threading mode] to Multi-thread.  In other words, it disables
1728** mutexing on [database connection] and [prepared statement] objects.
1729** The application is responsible for serializing access to
1730** [database connections] and [prepared statements].  But other mutexes
1731** are enabled so that SQLite will be safe to use in a multi-threaded
1732** environment as long as no two threads attempt to use the same
1733** [database connection] at the same time.  ^If SQLite is compiled with
1734** the [SQLITE_THREADSAFE | SQLITE_THREADSAFE=0] compile-time option then
1735** it is not possible to set the Multi-thread [threading mode] and
1736** [sqlite3_config()] will return [SQLITE_ERROR] if called with the
1737** SQLITE_CONFIG_MULTITHREAD configuration option.</dd>
1738**
1739** [[SQLITE_CONFIG_SERIALIZED]] <dt>SQLITE_CONFIG_SERIALIZED</dt>
1740** <dd>There are no arguments to this option.  ^This option sets the
1741** [threading mode] to Serialized. In other words, this option enables
1742** all mutexes including the recursive
1743** mutexes on [database connection] and [prepared statement] objects.
1744** In this mode (which is the default when SQLite is compiled with
1745** [SQLITE_THREADSAFE=1]) the SQLite library will itself serialize access
1746** to [database connections] and [prepared statements] so that the
1747** application is free to use the same [database connection] or the
1748** same [prepared statement] in different threads at the same time.
1749** ^If SQLite is compiled with
1750** the [SQLITE_THREADSAFE | SQLITE_THREADSAFE=0] compile-time option then
1751** it is not possible to set the Serialized [threading mode] and
1752** [sqlite3_config()] will return [SQLITE_ERROR] if called with the
1753** SQLITE_CONFIG_SERIALIZED configuration option.</dd>
1754**
1755** [[SQLITE_CONFIG_MALLOC]] <dt>SQLITE_CONFIG_MALLOC</dt>
1756** <dd> ^(The SQLITE_CONFIG_MALLOC option takes a single argument which is
1757** a pointer to an instance of the [sqlite3_mem_methods] structure.
1758** The argument specifies
1759** alternative low-level memory allocation routines to be used in place of
1760** the memory allocation routines built into SQLite.)^ ^SQLite makes
1761** its own private copy of the content of the [sqlite3_mem_methods] structure
1762** before the [sqlite3_config()] call returns.</dd>
1763**
1764** [[SQLITE_CONFIG_GETMALLOC]] <dt>SQLITE_CONFIG_GETMALLOC</dt>
1765** <dd> ^(The SQLITE_CONFIG_GETMALLOC option takes a single argument which
1766** is a pointer to an instance of the [sqlite3_mem_methods] structure.
1767** The [sqlite3_mem_methods]
1768** structure is filled with the currently defined memory allocation routines.)^
1769** This option can be used to overload the default memory allocation
1770** routines with a wrapper that simulations memory allocation failure or
1771** tracks memory usage, for example. </dd>
1772**
1773** [[SQLITE_CONFIG_MEMSTATUS]] <dt>SQLITE_CONFIG_MEMSTATUS</dt>
1774** <dd> ^The SQLITE_CONFIG_MEMSTATUS option takes single argument of type int,
1775** interpreted as a boolean, which enables or disables the collection of
1776** memory allocation statistics. ^(When memory allocation statistics are
1777** disabled, the following SQLite interfaces become non-operational:
1778**   <ul>
1779**   <li> [sqlite3_memory_used()]
1780**   <li> [sqlite3_memory_highwater()]
1781**   <li> [sqlite3_soft_heap_limit64()]
1782**   <li> [sqlite3_status64()]
1783**   </ul>)^
1784** ^Memory allocation statistics are enabled by default unless SQLite is
1785** compiled with [SQLITE_DEFAULT_MEMSTATUS]=0 in which case memory
1786** allocation statistics are disabled by default.
1787** </dd>
1788**
1789** [[SQLITE_CONFIG_SCRATCH]] <dt>SQLITE_CONFIG_SCRATCH</dt>
1790** <dd> ^The SQLITE_CONFIG_SCRATCH option specifies a static memory buffer
1791** that SQLite can use for scratch memory.  ^(There are three arguments
1792** to SQLITE_CONFIG_SCRATCH:  A pointer an 8-byte
1793** aligned memory buffer from which the scratch allocations will be
1794** drawn, the size of each scratch allocation (sz),
1795** and the maximum number of scratch allocations (N).)^
1796** The first argument must be a pointer to an 8-byte aligned buffer
1797** of at least sz*N bytes of memory.
1798** ^SQLite will not use more than one scratch buffers per thread.
1799** ^SQLite will never request a scratch buffer that is more than 6
1800** times the database page size.
1801** ^If SQLite needs needs additional
1802** scratch memory beyond what is provided by this configuration option, then
1803** [sqlite3_malloc()] will be used to obtain the memory needed.<p>
1804** ^When the application provides any amount of scratch memory using
1805** SQLITE_CONFIG_SCRATCH, SQLite avoids unnecessary large
1806** [sqlite3_malloc|heap allocations].
1807** This can help [Robson proof|prevent memory allocation failures] due to heap
1808** fragmentation in low-memory embedded systems.
1809** </dd>
1810**
1811** [[SQLITE_CONFIG_PAGECACHE]] <dt>SQLITE_CONFIG_PAGECACHE</dt>
1812** <dd> ^The SQLITE_CONFIG_PAGECACHE option specifies a static memory buffer
1813** that SQLite can use for the database page cache with the default page
1814** cache implementation.
1815** This configuration should not be used if an application-define page
1816** cache implementation is loaded using the [SQLITE_CONFIG_PCACHE2]
1817** configuration option.
1818** ^There are three arguments to SQLITE_CONFIG_PAGECACHE: A pointer to
1819** 8-byte aligned
1820** memory, the size of each page buffer (sz), and the number of pages (N).
1821** The sz argument should be the size of the largest database page
1822** (a power of two between 512 and 65536) plus some extra bytes for each
1823** page header.  ^The number of extra bytes needed by the page header
1824** can be determined using the [SQLITE_CONFIG_PCACHE_HDRSZ] option
1825** to [sqlite3_config()].
1826** ^It is harmless, apart from the wasted memory,
1827** for the sz parameter to be larger than necessary.  The first
1828** argument should pointer to an 8-byte aligned block of memory that
1829** is at least sz*N bytes of memory, otherwise subsequent behavior is
1830** undefined.
1831** ^SQLite will use the memory provided by the first argument to satisfy its
1832** memory needs for the first N pages that it adds to cache.  ^If additional
1833** page cache memory is needed beyond what is provided by this option, then
1834** SQLite goes to [sqlite3_malloc()] for the additional storage space.</dd>
1835**
1836** [[SQLITE_CONFIG_HEAP]] <dt>SQLITE_CONFIG_HEAP</dt>
1837** <dd> ^The SQLITE_CONFIG_HEAP option specifies a static memory buffer
1838** that SQLite will use for all of its dynamic memory allocation needs
1839** beyond those provided for by [SQLITE_CONFIG_SCRATCH] and
1840** [SQLITE_CONFIG_PAGECACHE].
1841** ^The SQLITE_CONFIG_HEAP option is only available if SQLite is compiled
1842** with either [SQLITE_ENABLE_MEMSYS3] or [SQLITE_ENABLE_MEMSYS5] and returns
1843** [SQLITE_ERROR] if invoked otherwise.
1844** ^There are three arguments to SQLITE_CONFIG_HEAP:
1845** An 8-byte aligned pointer to the memory,
1846** the number of bytes in the memory buffer, and the minimum allocation size.
1847** ^If the first pointer (the memory pointer) is NULL, then SQLite reverts
1848** to using its default memory allocator (the system malloc() implementation),
1849** undoing any prior invocation of [SQLITE_CONFIG_MALLOC].  ^If the
1850** memory pointer is not NULL then the alternative memory
1851** allocator is engaged to handle all of SQLites memory allocation needs.
1852** The first pointer (the memory pointer) must be aligned to an 8-byte
1853** boundary or subsequent behavior of SQLite will be undefined.
1854** The minimum allocation size is capped at 2**12. Reasonable values
1855** for the minimum allocation size are 2**5 through 2**8.</dd>
1856**
1857** [[SQLITE_CONFIG_MUTEX]] <dt>SQLITE_CONFIG_MUTEX</dt>
1858** <dd> ^(The SQLITE_CONFIG_MUTEX option takes a single argument which is a
1859** pointer to an instance of the [sqlite3_mutex_methods] structure.
1860** The argument specifies alternative low-level mutex routines to be used
1861** in place the mutex routines built into SQLite.)^  ^SQLite makes a copy of
1862** the content of the [sqlite3_mutex_methods] structure before the call to
1863** [sqlite3_config()] returns. ^If SQLite is compiled with
1864** the [SQLITE_THREADSAFE | SQLITE_THREADSAFE=0] compile-time option then
1865** the entire mutexing subsystem is omitted from the build and hence calls to
1866** [sqlite3_config()] with the SQLITE_CONFIG_MUTEX configuration option will
1867** return [SQLITE_ERROR].</dd>
1868**
1869** [[SQLITE_CONFIG_GETMUTEX]] <dt>SQLITE_CONFIG_GETMUTEX</dt>
1870** <dd> ^(The SQLITE_CONFIG_GETMUTEX option takes a single argument which
1871** is a pointer to an instance of the [sqlite3_mutex_methods] structure.  The
1872** [sqlite3_mutex_methods]
1873** structure is filled with the currently defined mutex routines.)^
1874** This option can be used to overload the default mutex allocation
1875** routines with a wrapper used to track mutex usage for performance
1876** profiling or testing, for example.   ^If SQLite is compiled with
1877** the [SQLITE_THREADSAFE | SQLITE_THREADSAFE=0] compile-time option then
1878** the entire mutexing subsystem is omitted from the build and hence calls to
1879** [sqlite3_config()] with the SQLITE_CONFIG_GETMUTEX configuration option will
1880** return [SQLITE_ERROR].</dd>
1881**
1882** [[SQLITE_CONFIG_LOOKASIDE]] <dt>SQLITE_CONFIG_LOOKASIDE</dt>
1883** <dd> ^(The SQLITE_CONFIG_LOOKASIDE option takes two arguments that determine
1884** the default size of lookaside memory on each [database connection].
1885** The first argument is the
1886** size of each lookaside buffer slot and the second is the number of
1887** slots allocated to each database connection.)^  ^(SQLITE_CONFIG_LOOKASIDE
1888** sets the <i>default</i> lookaside size. The [SQLITE_DBCONFIG_LOOKASIDE]
1889** option to [sqlite3_db_config()] can be used to change the lookaside
1890** configuration on individual connections.)^ </dd>
1891**
1892** [[SQLITE_CONFIG_PCACHE2]] <dt>SQLITE_CONFIG_PCACHE2</dt>
1893** <dd> ^(The SQLITE_CONFIG_PCACHE2 option takes a single argument which is
1894** a pointer to an [sqlite3_pcache_methods2] object.  This object specifies
1895** the interface to a custom page cache implementation.)^
1896** ^SQLite makes a copy of the [sqlite3_pcache_methods2] object.</dd>
1897**
1898** [[SQLITE_CONFIG_GETPCACHE2]] <dt>SQLITE_CONFIG_GETPCACHE2</dt>
1899** <dd> ^(The SQLITE_CONFIG_GETPCACHE2 option takes a single argument which
1900** is a pointer to an [sqlite3_pcache_methods2] object.  SQLite copies of
1901** the current page cache implementation into that object.)^ </dd>
1902**
1903** [[SQLITE_CONFIG_LOG]] <dt>SQLITE_CONFIG_LOG</dt>
1904** <dd> The SQLITE_CONFIG_LOG option is used to configure the SQLite
1905** global [error log].
1906** (^The SQLITE_CONFIG_LOG option takes two arguments: a pointer to a
1907** function with a call signature of void(*)(void*,int,const char*),
1908** and a pointer to void. ^If the function pointer is not NULL, it is
1909** invoked by [sqlite3_log()] to process each logging event.  ^If the
1910** function pointer is NULL, the [sqlite3_log()] interface becomes a no-op.
1911** ^The void pointer that is the second argument to SQLITE_CONFIG_LOG is
1912** passed through as the first parameter to the application-defined logger
1913** function whenever that function is invoked.  ^The second parameter to
1914** the logger function is a copy of the first parameter to the corresponding
1915** [sqlite3_log()] call and is intended to be a [result code] or an
1916** [extended result code].  ^The third parameter passed to the logger is
1917** log message after formatting via [sqlite3_snprintf()].
1918** The SQLite logging interface is not reentrant; the logger function
1919** supplied by the application must not invoke any SQLite interface.
1920** In a multi-threaded application, the application-defined logger
1921** function must be threadsafe. </dd>
1922**
1923** [[SQLITE_CONFIG_URI]] <dt>SQLITE_CONFIG_URI
1924** <dd>^(The SQLITE_CONFIG_URI option takes a single argument of type int.
1925** If non-zero, then URI handling is globally enabled. If the parameter is zero,
1926** then URI handling is globally disabled.)^ ^If URI handling is globally
1927** enabled, all filenames passed to [sqlite3_open()], [sqlite3_open_v2()],
1928** [sqlite3_open16()] or
1929** specified as part of [ATTACH] commands are interpreted as URIs, regardless
1930** of whether or not the [SQLITE_OPEN_URI] flag is set when the database
1931** connection is opened. ^If it is globally disabled, filenames are
1932** only interpreted as URIs if the SQLITE_OPEN_URI flag is set when the
1933** database connection is opened. ^(By default, URI handling is globally
1934** disabled. The default value may be changed by compiling with the
1935** [SQLITE_USE_URI] symbol defined.)^
1936**
1937** [[SQLITE_CONFIG_COVERING_INDEX_SCAN]] <dt>SQLITE_CONFIG_COVERING_INDEX_SCAN
1938** <dd>^The SQLITE_CONFIG_COVERING_INDEX_SCAN option takes a single integer
1939** argument which is interpreted as a boolean in order to enable or disable
1940** the use of covering indices for full table scans in the query optimizer.
1941** ^The default setting is determined
1942** by the [SQLITE_ALLOW_COVERING_INDEX_SCAN] compile-time option, or is "on"
1943** if that compile-time option is omitted.
1944** The ability to disable the use of covering indices for full table scans
1945** is because some incorrectly coded legacy applications might malfunction
1946** when the optimization is enabled.  Providing the ability to
1947** disable the optimization allows the older, buggy application code to work
1948** without change even with newer versions of SQLite.
1949**
1950** [[SQLITE_CONFIG_PCACHE]] [[SQLITE_CONFIG_GETPCACHE]]
1951** <dt>SQLITE_CONFIG_PCACHE and SQLITE_CONFIG_GETPCACHE
1952** <dd> These options are obsolete and should not be used by new code.
1953** They are retained for backwards compatibility but are now no-ops.
1954** </dd>
1955**
1956** [[SQLITE_CONFIG_SQLLOG]]
1957** <dt>SQLITE_CONFIG_SQLLOG
1958** <dd>This option is only available if sqlite is compiled with the
1959** [SQLITE_ENABLE_SQLLOG] pre-processor macro defined. The first argument should
1960** be a pointer to a function of type void(*)(void*,sqlite3*,const char*, int).
1961** The second should be of type (void*). The callback is invoked by the library
1962** in three separate circumstances, identified by the value passed as the
1963** fourth parameter. If the fourth parameter is 0, then the database connection
1964** passed as the second argument has just been opened. The third argument
1965** points to a buffer containing the name of the main database file. If the
1966** fourth parameter is 1, then the SQL statement that the third parameter
1967** points to has just been executed. Or, if the fourth parameter is 2, then
1968** the connection being passed as the second parameter is being closed. The
1969** third parameter is passed NULL In this case.  An example of using this
1970** configuration option can be seen in the "test_sqllog.c" source file in
1971** the canonical SQLite source tree.</dd>
1972**
1973** [[SQLITE_CONFIG_MMAP_SIZE]]
1974** <dt>SQLITE_CONFIG_MMAP_SIZE
1975** <dd>^SQLITE_CONFIG_MMAP_SIZE takes two 64-bit integer (sqlite3_int64) values
1976** that are the default mmap size limit (the default setting for
1977** [PRAGMA mmap_size]) and the maximum allowed mmap size limit.
1978** ^The default setting can be overridden by each database connection using
1979** either the [PRAGMA mmap_size] command, or by using the
1980** [SQLITE_FCNTL_MMAP_SIZE] file control.  ^(The maximum allowed mmap size
1981** will be silently truncated if necessary so that it does not exceed the
1982** compile-time maximum mmap size set by the
1983** [SQLITE_MAX_MMAP_SIZE] compile-time option.)^
1984** ^If either argument to this option is negative, then that argument is
1985** changed to its compile-time default.
1986**
1987** [[SQLITE_CONFIG_WIN32_HEAPSIZE]]
1988** <dt>SQLITE_CONFIG_WIN32_HEAPSIZE
1989** <dd>^The SQLITE_CONFIG_WIN32_HEAPSIZE option is only available if SQLite is
1990** compiled for Windows with the [SQLITE_WIN32_MALLOC] pre-processor macro
1991** defined. ^SQLITE_CONFIG_WIN32_HEAPSIZE takes a 32-bit unsigned integer value
1992** that specifies the maximum size of the created heap.
1993**
1994** [[SQLITE_CONFIG_PCACHE_HDRSZ]]
1995** <dt>SQLITE_CONFIG_PCACHE_HDRSZ
1996** <dd>^The SQLITE_CONFIG_PCACHE_HDRSZ option takes a single parameter which
1997** is a pointer to an integer and writes into that integer the number of extra
1998** bytes per page required for each page in [SQLITE_CONFIG_PAGECACHE].
1999** The amount of extra space required can change depending on the compiler,
2000** target platform, and SQLite version.
2001**
2002** [[SQLITE_CONFIG_PMASZ]]
2003** <dt>SQLITE_CONFIG_PMASZ
2004** <dd>^The SQLITE_CONFIG_PMASZ option takes a single parameter which
2005** is an unsigned integer and sets the "Minimum PMA Size" for the multithreaded
2006** sorter to that integer.  The default minimum PMA Size is set by the
2007** [SQLITE_SORTER_PMASZ] compile-time option.  New threads are launched
2008** to help with sort operations when multithreaded sorting
2009** is enabled (using the [PRAGMA threads] command) and the amount of content
2010** to be sorted exceeds the page size times the minimum of the
2011** [PRAGMA cache_size] setting and this value.
2012** </dl>
2013*/
2014#define SQLITE_CONFIG_SINGLETHREAD  1  /* nil */
2015#define SQLITE_CONFIG_MULTITHREAD   2  /* nil */
2016#define SQLITE_CONFIG_SERIALIZED    3  /* nil */
2017#define SQLITE_CONFIG_MALLOC        4  /* sqlite3_mem_methods* */
2018#define SQLITE_CONFIG_GETMALLOC     5  /* sqlite3_mem_methods* */
2019#define SQLITE_CONFIG_SCRATCH       6  /* void*, int sz, int N */
2020#define SQLITE_CONFIG_PAGECACHE     7  /* void*, int sz, int N */
2021#define SQLITE_CONFIG_HEAP          8  /* void*, int nByte, int min */
2022#define SQLITE_CONFIG_MEMSTATUS     9  /* boolean */
2023#define SQLITE_CONFIG_MUTEX        10  /* sqlite3_mutex_methods* */
2024#define SQLITE_CONFIG_GETMUTEX     11  /* sqlite3_mutex_methods* */
2025/* previously SQLITE_CONFIG_CHUNKALLOC 12 which is now unused. */
2026#define SQLITE_CONFIG_LOOKASIDE    13  /* int int */
2027#define SQLITE_CONFIG_PCACHE       14  /* no-op */
2028#define SQLITE_CONFIG_GETPCACHE    15  /* no-op */
2029#define SQLITE_CONFIG_LOG          16  /* xFunc, void* */
2030#define SQLITE_CONFIG_URI          17  /* int */
2031#define SQLITE_CONFIG_PCACHE2      18  /* sqlite3_pcache_methods2* */
2032#define SQLITE_CONFIG_GETPCACHE2   19  /* sqlite3_pcache_methods2* */
2033#define SQLITE_CONFIG_COVERING_INDEX_SCAN 20  /* int */
2034#define SQLITE_CONFIG_SQLLOG       21  /* xSqllog, void* */
2035#define SQLITE_CONFIG_MMAP_SIZE    22  /* sqlite3_int64, sqlite3_int64 */
2036#define SQLITE_CONFIG_WIN32_HEAPSIZE      23  /* int nByte */
2037#define SQLITE_CONFIG_PCACHE_HDRSZ        24  /* int *psz */
2038#define SQLITE_CONFIG_PMASZ               25  /* unsigned int szPma */
2039
2040/*
2041** CAPI3REF: Database Connection Configuration Options
2042**
2043** These constants are the available integer configuration options that
2044** can be passed as the second argument to the [sqlite3_db_config()] interface.
2045**
2046** New configuration options may be added in future releases of SQLite.
2047** Existing configuration options might be discontinued.  Applications
2048** should check the return code from [sqlite3_db_config()] to make sure that
2049** the call worked.  ^The [sqlite3_db_config()] interface will return a
2050** non-zero [error code] if a discontinued or unsupported configuration option
2051** is invoked.
2052**
2053** <dl>
2054** <dt>SQLITE_DBCONFIG_LOOKASIDE</dt>
2055** <dd> ^This option takes three additional arguments that determine the
2056** [lookaside memory allocator] configuration for the [database connection].
2057** ^The first argument (the third parameter to [sqlite3_db_config()] is a
2058** pointer to a memory buffer to use for lookaside memory.
2059** ^The first argument after the SQLITE_DBCONFIG_LOOKASIDE verb
2060** may be NULL in which case SQLite will allocate the
2061** lookaside buffer itself using [sqlite3_malloc()]. ^The second argument is the
2062** size of each lookaside buffer slot.  ^The third argument is the number of
2063** slots.  The size of the buffer in the first argument must be greater than
2064** or equal to the product of the second and third arguments.  The buffer
2065** must be aligned to an 8-byte boundary.  ^If the second argument to
2066** SQLITE_DBCONFIG_LOOKASIDE is not a multiple of 8, it is internally
2067** rounded down to the next smaller multiple of 8.  ^(The lookaside memory
2068** configuration for a database connection can only be changed when that
2069** connection is not currently using lookaside memory, or in other words
2070** when the "current value" returned by
2071** [sqlite3_db_status](D,[SQLITE_CONFIG_LOOKASIDE],...) is zero.
2072** Any attempt to change the lookaside memory configuration when lookaside
2073** memory is in use leaves the configuration unchanged and returns
2074** [SQLITE_BUSY].)^</dd>
2075**
2076** <dt>SQLITE_DBCONFIG_ENABLE_FKEY</dt>
2077** <dd> ^This option is used to enable or disable the enforcement of
2078** [foreign key constraints].  There should be two additional arguments.
2079** The first argument is an integer which is 0 to disable FK enforcement,
2080** positive to enable FK enforcement or negative to leave FK enforcement
2081** unchanged.  The second parameter is a pointer to an integer into which
2082** is written 0 or 1 to indicate whether FK enforcement is off or on
2083** following this call.  The second parameter may be a NULL pointer, in
2084** which case the FK enforcement setting is not reported back. </dd>
2085**
2086** <dt>SQLITE_DBCONFIG_ENABLE_TRIGGER</dt>
2087** <dd> ^This option is used to enable or disable [CREATE TRIGGER | triggers].
2088** There should be two additional arguments.
2089** The first argument is an integer which is 0 to disable triggers,
2090** positive to enable triggers or negative to leave the setting unchanged.
2091** The second parameter is a pointer to an integer into which
2092** is written 0 or 1 to indicate whether triggers are disabled or enabled
2093** following this call.  The second parameter may be a NULL pointer, in
2094** which case the trigger setting is not reported back. </dd>
2095**
2096** </dl>
2097*/
2098#define SQLITE_DBCONFIG_LOOKASIDE       1001  /* void* int int */
2099#define SQLITE_DBCONFIG_ENABLE_FKEY     1002  /* int int* */
2100#define SQLITE_DBCONFIG_ENABLE_TRIGGER  1003  /* int int* */
2101
2102
2103/*
2104** CAPI3REF: Enable Or Disable Extended Result Codes
2105** METHOD: sqlite3
2106**
2107** ^The sqlite3_extended_result_codes() routine enables or disables the
2108** [extended result codes] feature of SQLite. ^The extended result
2109** codes are disabled by default for historical compatibility.
2110*/
2111SQLITE_API int SQLITE_STDCALL sqlite3_extended_result_codes(sqlite3*, int onoff);
2112
2113/*
2114** CAPI3REF: Last Insert Rowid
2115** METHOD: sqlite3
2116**
2117** ^Each entry in most SQLite tables (except for [WITHOUT ROWID] tables)
2118** has a unique 64-bit signed
2119** integer key called the [ROWID | "rowid"]. ^The rowid is always available
2120** as an undeclared column named ROWID, OID, or _ROWID_ as long as those
2121** names are not also used by explicitly declared columns. ^If
2122** the table has a column of type [INTEGER PRIMARY KEY] then that column
2123** is another alias for the rowid.
2124**
2125** ^The sqlite3_last_insert_rowid(D) interface returns the [rowid] of the
2126** most recent successful [INSERT] into a rowid table or [virtual table]
2127** on database connection D.
2128** ^Inserts into [WITHOUT ROWID] tables are not recorded.
2129** ^If no successful [INSERT]s into rowid tables
2130** have ever occurred on the database connection D,
2131** then sqlite3_last_insert_rowid(D) returns zero.
2132**
2133** ^(If an [INSERT] occurs within a trigger or within a [virtual table]
2134** method, then this routine will return the [rowid] of the inserted
2135** row as long as the trigger or virtual table method is running.
2136** But once the trigger or virtual table method ends, the value returned
2137** by this routine reverts to what it was before the trigger or virtual
2138** table method began.)^
2139**
2140** ^An [INSERT] that fails due to a constraint violation is not a
2141** successful [INSERT] and does not change the value returned by this
2142** routine.  ^Thus INSERT OR FAIL, INSERT OR IGNORE, INSERT OR ROLLBACK,
2143** and INSERT OR ABORT make no changes to the return value of this
2144** routine when their insertion fails.  ^(When INSERT OR REPLACE
2145** encounters a constraint violation, it does not fail.  The
2146** INSERT continues to completion after deleting rows that caused
2147** the constraint problem so INSERT OR REPLACE will always change
2148** the return value of this interface.)^
2149**
2150** ^For the purposes of this routine, an [INSERT] is considered to
2151** be successful even if it is subsequently rolled back.
2152**
2153** This function is accessible to SQL statements via the
2154** [last_insert_rowid() SQL function].
2155**
2156** If a separate thread performs a new [INSERT] on the same
2157** database connection while the [sqlite3_last_insert_rowid()]
2158** function is running and thus changes the last insert [rowid],
2159** then the value returned by [sqlite3_last_insert_rowid()] is
2160** unpredictable and might not equal either the old or the new
2161** last insert [rowid].
2162*/
2163SQLITE_API sqlite3_int64 SQLITE_STDCALL sqlite3_last_insert_rowid(sqlite3*);
2164
2165/*
2166** CAPI3REF: Count The Number Of Rows Modified
2167** METHOD: sqlite3
2168**
2169** ^This function returns the number of rows modified, inserted or
2170** deleted by the most recently completed INSERT, UPDATE or DELETE
2171** statement on the database connection specified by the only parameter.
2172** ^Executing any other type of SQL statement does not modify the value
2173** returned by this function.
2174**
2175** ^Only changes made directly by the INSERT, UPDATE or DELETE statement are
2176** considered - auxiliary changes caused by [CREATE TRIGGER | triggers],
2177** [foreign key actions] or [REPLACE] constraint resolution are not counted.
2178**
2179** Changes to a view that are intercepted by
2180** [INSTEAD OF trigger | INSTEAD OF triggers] are not counted. ^The value
2181** returned by sqlite3_changes() immediately after an INSERT, UPDATE or
2182** DELETE statement run on a view is always zero. Only changes made to real
2183** tables are counted.
2184**
2185** Things are more complicated if the sqlite3_changes() function is
2186** executed while a trigger program is running. This may happen if the
2187** program uses the [changes() SQL function], or if some other callback
2188** function invokes sqlite3_changes() directly. Essentially:
2189**
2190** <ul>
2191**   <li> ^(Before entering a trigger program the value returned by
2192**        sqlite3_changes() function is saved. After the trigger program
2193**        has finished, the original value is restored.)^
2194**
2195**   <li> ^(Within a trigger program each INSERT, UPDATE and DELETE
2196**        statement sets the value returned by sqlite3_changes()
2197**        upon completion as normal. Of course, this value will not include
2198**        any changes performed by sub-triggers, as the sqlite3_changes()
2199**        value will be saved and restored after each sub-trigger has run.)^
2200** </ul>
2201**
2202** ^This means that if the changes() SQL function (or similar) is used
2203** by the first INSERT, UPDATE or DELETE statement within a trigger, it
2204** returns the value as set when the calling statement began executing.
2205** ^If it is used by the second or subsequent such statement within a trigger
2206** program, the value returned reflects the number of rows modified by the
2207** previous INSERT, UPDATE or DELETE statement within the same trigger.
2208**
2209** See also the [sqlite3_total_changes()] interface, the
2210** [count_changes pragma], and the [changes() SQL function].
2211**
2212** If a separate thread makes changes on the same database connection
2213** while [sqlite3_changes()] is running then the value returned
2214** is unpredictable and not meaningful.
2215*/
2216SQLITE_API int SQLITE_STDCALL sqlite3_changes(sqlite3*);
2217
2218/*
2219** CAPI3REF: Total Number Of Rows Modified
2220** METHOD: sqlite3
2221**
2222** ^This function returns the total number of rows inserted, modified or
2223** deleted by all [INSERT], [UPDATE] or [DELETE] statements completed
2224** since the database connection was opened, including those executed as
2225** part of trigger programs. ^Executing any other type of SQL statement
2226** does not affect the value returned by sqlite3_total_changes().
2227**
2228** ^Changes made as part of [foreign key actions] are included in the
2229** count, but those made as part of REPLACE constraint resolution are
2230** not. ^Changes to a view that are intercepted by INSTEAD OF triggers
2231** are not counted.
2232**
2233** See also the [sqlite3_changes()] interface, the
2234** [count_changes pragma], and the [total_changes() SQL function].
2235**
2236** If a separate thread makes changes on the same database connection
2237** while [sqlite3_total_changes()] is running then the value
2238** returned is unpredictable and not meaningful.
2239*/
2240SQLITE_API int SQLITE_STDCALL sqlite3_total_changes(sqlite3*);
2241
2242/*
2243** CAPI3REF: Interrupt A Long-Running Query
2244** METHOD: sqlite3
2245**
2246** ^This function causes any pending database operation to abort and
2247** return at its earliest opportunity. This routine is typically
2248** called in response to a user action such as pressing "Cancel"
2249** or Ctrl-C where the user wants a long query operation to halt
2250** immediately.
2251**
2252** ^It is safe to call this routine from a thread different from the
2253** thread that is currently running the database operation.  But it
2254** is not safe to call this routine with a [database connection] that
2255** is closed or might close before sqlite3_interrupt() returns.
2256**
2257** ^If an SQL operation is very nearly finished at the time when
2258** sqlite3_interrupt() is called, then it might not have an opportunity
2259** to be interrupted and might continue to completion.
2260**
2261** ^An SQL operation that is interrupted will return [SQLITE_INTERRUPT].
2262** ^If the interrupted SQL operation is an INSERT, UPDATE, or DELETE
2263** that is inside an explicit transaction, then the entire transaction
2264** will be rolled back automatically.
2265**
2266** ^The sqlite3_interrupt(D) call is in effect until all currently running
2267** SQL statements on [database connection] D complete.  ^Any new SQL statements
2268** that are started after the sqlite3_interrupt() call and before the
2269** running statements reaches zero are interrupted as if they had been
2270** running prior to the sqlite3_interrupt() call.  ^New SQL statements
2271** that are started after the running statement count reaches zero are
2272** not effected by the sqlite3_interrupt().
2273** ^A call to sqlite3_interrupt(D) that occurs when there are no running
2274** SQL statements is a no-op and has no effect on SQL statements
2275** that are started after the sqlite3_interrupt() call returns.
2276**
2277** If the database connection closes while [sqlite3_interrupt()]
2278** is running then bad things will likely happen.
2279*/
2280SQLITE_API void SQLITE_STDCALL sqlite3_interrupt(sqlite3*);
2281
2282/*
2283** CAPI3REF: Determine If An SQL Statement Is Complete
2284**
2285** These routines are useful during command-line input to determine if the
2286** currently entered text seems to form a complete SQL statement or
2287** if additional input is needed before sending the text into
2288** SQLite for parsing.  ^These routines return 1 if the input string
2289** appears to be a complete SQL statement.  ^A statement is judged to be
2290** complete if it ends with a semicolon token and is not a prefix of a
2291** well-formed CREATE TRIGGER statement.  ^Semicolons that are embedded within
2292** string literals or quoted identifier names or comments are not
2293** independent tokens (they are part of the token in which they are
2294** embedded) and thus do not count as a statement terminator.  ^Whitespace
2295** and comments that follow the final semicolon are ignored.
2296**
2297** ^These routines return 0 if the statement is incomplete.  ^If a
2298** memory allocation fails, then SQLITE_NOMEM is returned.
2299**
2300** ^These routines do not parse the SQL statements thus
2301** will not detect syntactically incorrect SQL.
2302**
2303** ^(If SQLite has not been initialized using [sqlite3_initialize()] prior
2304** to invoking sqlite3_complete16() then sqlite3_initialize() is invoked
2305** automatically by sqlite3_complete16().  If that initialization fails,
2306** then the return value from sqlite3_complete16() will be non-zero
2307** regardless of whether or not the input SQL is complete.)^
2308**
2309** The input to [sqlite3_complete()] must be a zero-terminated
2310** UTF-8 string.
2311**
2312** The input to [sqlite3_complete16()] must be a zero-terminated
2313** UTF-16 string in native byte order.
2314*/
2315SQLITE_API int SQLITE_STDCALL sqlite3_complete(const char *sql);
2316SQLITE_API int SQLITE_STDCALL sqlite3_complete16(const void *sql);
2317
2318/*
2319** CAPI3REF: Register A Callback To Handle SQLITE_BUSY Errors
2320** KEYWORDS: {busy-handler callback} {busy handler}
2321** METHOD: sqlite3
2322**
2323** ^The sqlite3_busy_handler(D,X,P) routine sets a callback function X
2324** that might be invoked with argument P whenever
2325** an attempt is made to access a database table associated with
2326** [database connection] D when another thread
2327** or process has the table locked.
2328** The sqlite3_busy_handler() interface is used to implement
2329** [sqlite3_busy_timeout()] and [PRAGMA busy_timeout].
2330**
2331** ^If the busy callback is NULL, then [SQLITE_BUSY]
2332** is returned immediately upon encountering the lock.  ^If the busy callback
2333** is not NULL, then the callback might be invoked with two arguments.
2334**
2335** ^The first argument to the busy handler is a copy of the void* pointer which
2336** is the third argument to sqlite3_busy_handler().  ^The second argument to
2337** the busy handler callback is the number of times that the busy handler has
2338** been invoked previously for the same locking event.  ^If the
2339** busy callback returns 0, then no additional attempts are made to
2340** access the database and [SQLITE_BUSY] is returned
2341** to the application.
2342** ^If the callback returns non-zero, then another attempt
2343** is made to access the database and the cycle repeats.
2344**
2345** The presence of a busy handler does not guarantee that it will be invoked
2346** when there is lock contention. ^If SQLite determines that invoking the busy
2347** handler could result in a deadlock, it will go ahead and return [SQLITE_BUSY]
2348** to the application instead of invoking the
2349** busy handler.
2350** Consider a scenario where one process is holding a read lock that
2351** it is trying to promote to a reserved lock and
2352** a second process is holding a reserved lock that it is trying
2353** to promote to an exclusive lock.  The first process cannot proceed
2354** because it is blocked by the second and the second process cannot
2355** proceed because it is blocked by the first.  If both processes
2356** invoke the busy handlers, neither will make any progress.  Therefore,
2357** SQLite returns [SQLITE_BUSY] for the first process, hoping that this
2358** will induce the first process to release its read lock and allow
2359** the second process to proceed.
2360**
2361** ^The default busy callback is NULL.
2362**
2363** ^(There can only be a single busy handler defined for each
2364** [database connection].  Setting a new busy handler clears any
2365** previously set handler.)^  ^Note that calling [sqlite3_busy_timeout()]
2366** or evaluating [PRAGMA busy_timeout=N] will change the
2367** busy handler and thus clear any previously set busy handler.
2368**
2369** The busy callback should not take any actions which modify the
2370** database connection that invoked the busy handler.  In other words,
2371** the busy handler is not reentrant.  Any such actions
2372** result in undefined behavior.
2373**
2374** A busy handler must not close the database connection
2375** or [prepared statement] that invoked the busy handler.
2376*/
2377SQLITE_API int SQLITE_STDCALL sqlite3_busy_handler(sqlite3*, int(*)(void*,int), void*);
2378
2379/*
2380** CAPI3REF: Set A Busy Timeout
2381** METHOD: sqlite3
2382**
2383** ^This routine sets a [sqlite3_busy_handler | busy handler] that sleeps
2384** for a specified amount of time when a table is locked.  ^The handler
2385** will sleep multiple times until at least "ms" milliseconds of sleeping
2386** have accumulated.  ^After at least "ms" milliseconds of sleeping,
2387** the handler returns 0 which causes [sqlite3_step()] to return
2388** [SQLITE_BUSY].
2389**
2390** ^Calling this routine with an argument less than or equal to zero
2391** turns off all busy handlers.
2392**
2393** ^(There can only be a single busy handler for a particular
2394** [database connection] at any given moment.  If another busy handler
2395** was defined  (using [sqlite3_busy_handler()]) prior to calling
2396** this routine, that other busy handler is cleared.)^
2397**
2398** See also:  [PRAGMA busy_timeout]
2399*/
2400SQLITE_API int SQLITE_STDCALL sqlite3_busy_timeout(sqlite3*, int ms);
2401
2402/*
2403** CAPI3REF: Convenience Routines For Running Queries
2404** METHOD: sqlite3
2405**
2406** This is a legacy interface that is preserved for backwards compatibility.
2407** Use of this interface is not recommended.
2408**
2409** Definition: A <b>result table</b> is memory data structure created by the
2410** [sqlite3_get_table()] interface.  A result table records the
2411** complete query results from one or more queries.
2412**
2413** The table conceptually has a number of rows and columns.  But
2414** these numbers are not part of the result table itself.  These
2415** numbers are obtained separately.  Let N be the number of rows
2416** and M be the number of columns.
2417**
2418** A result table is an array of pointers to zero-terminated UTF-8 strings.
2419** There are (N+1)*M elements in the array.  The first M pointers point
2420** to zero-terminated strings that  contain the names of the columns.
2421** The remaining entries all point to query results.  NULL values result
2422** in NULL pointers.  All other values are in their UTF-8 zero-terminated
2423** string representation as returned by [sqlite3_column_text()].
2424**
2425** A result table might consist of one or more memory allocations.
2426** It is not safe to pass a result table directly to [sqlite3_free()].
2427** A result table should be deallocated using [sqlite3_free_table()].
2428**
2429** ^(As an example of the result table format, suppose a query result
2430** is as follows:
2431**
2432** <blockquote><pre>
2433**        Name        | Age
2434**        -----------------------
2435**        Alice       | 43
2436**        Bob         | 28
2437**        Cindy       | 21
2438** </pre></blockquote>
2439**
2440** There are two column (M==2) and three rows (N==3).  Thus the
2441** result table has 8 entries.  Suppose the result table is stored
2442** in an array names azResult.  Then azResult holds this content:
2443**
2444** <blockquote><pre>
2445**        azResult&#91;0] = "Name";
2446**        azResult&#91;1] = "Age";
2447**        azResult&#91;2] = "Alice";
2448**        azResult&#91;3] = "43";
2449**        azResult&#91;4] = "Bob";
2450**        azResult&#91;5] = "28";
2451**        azResult&#91;6] = "Cindy";
2452**        azResult&#91;7] = "21";
2453** </pre></blockquote>)^
2454**
2455** ^The sqlite3_get_table() function evaluates one or more
2456** semicolon-separated SQL statements in the zero-terminated UTF-8
2457** string of its 2nd parameter and returns a result table to the
2458** pointer given in its 3rd parameter.
2459**
2460** After the application has finished with the result from sqlite3_get_table(),
2461** it must pass the result table pointer to sqlite3_free_table() in order to
2462** release the memory that was malloced.  Because of the way the
2463** [sqlite3_malloc()] happens within sqlite3_get_table(), the calling
2464** function must not try to call [sqlite3_free()] directly.  Only
2465** [sqlite3_free_table()] is able to release the memory properly and safely.
2466**
2467** The sqlite3_get_table() interface is implemented as a wrapper around
2468** [sqlite3_exec()].  The sqlite3_get_table() routine does not have access
2469** to any internal data structures of SQLite.  It uses only the public
2470** interface defined here.  As a consequence, errors that occur in the
2471** wrapper layer outside of the internal [sqlite3_exec()] call are not
2472** reflected in subsequent calls to [sqlite3_errcode()] or
2473** [sqlite3_errmsg()].
2474*/
2475SQLITE_API int SQLITE_STDCALL sqlite3_get_table(
2476  sqlite3 *db,          /* An open database */
2477  const char *zSql,     /* SQL to be evaluated */
2478  char ***pazResult,    /* Results of the query */
2479  int *pnRow,           /* Number of result rows written here */
2480  int *pnColumn,        /* Number of result columns written here */
2481  char **pzErrmsg       /* Error msg written here */
2482);
2483SQLITE_API void SQLITE_STDCALL sqlite3_free_table(char **result);
2484
2485/*
2486** CAPI3REF: Formatted String Printing Functions
2487**
2488** These routines are work-alikes of the "printf()" family of functions
2489** from the standard C library.
2490** These routines understand most of the common K&R formatting options,
2491** plus some additional non-standard formats, detailed below.
2492** Note that some of the more obscure formatting options from recent
2493** C-library standards are omitted from this implementation.
2494**
2495** ^The sqlite3_mprintf() and sqlite3_vmprintf() routines write their
2496** results into memory obtained from [sqlite3_malloc()].
2497** The strings returned by these two routines should be
2498** released by [sqlite3_free()].  ^Both routines return a
2499** NULL pointer if [sqlite3_malloc()] is unable to allocate enough
2500** memory to hold the resulting string.
2501**
2502** ^(The sqlite3_snprintf() routine is similar to "snprintf()" from
2503** the standard C library.  The result is written into the
2504** buffer supplied as the second parameter whose size is given by
2505** the first parameter. Note that the order of the
2506** first two parameters is reversed from snprintf().)^  This is an
2507** historical accident that cannot be fixed without breaking
2508** backwards compatibility.  ^(Note also that sqlite3_snprintf()
2509** returns a pointer to its buffer instead of the number of
2510** characters actually written into the buffer.)^  We admit that
2511** the number of characters written would be a more useful return
2512** value but we cannot change the implementation of sqlite3_snprintf()
2513** now without breaking compatibility.
2514**
2515** ^As long as the buffer size is greater than zero, sqlite3_snprintf()
2516** guarantees that the buffer is always zero-terminated.  ^The first
2517** parameter "n" is the total size of the buffer, including space for
2518** the zero terminator.  So the longest string that can be completely
2519** written will be n-1 characters.
2520**
2521** ^The sqlite3_vsnprintf() routine is a varargs version of sqlite3_snprintf().
2522**
2523** These routines all implement some additional formatting
2524** options that are useful for constructing SQL statements.
2525** All of the usual printf() formatting options apply.  In addition, there
2526** is are "%q", "%Q", "%w" and "%z" options.
2527**
2528** ^(The %q option works like %s in that it substitutes a nul-terminated
2529** string from the argument list.  But %q also doubles every '\'' character.
2530** %q is designed for use inside a string literal.)^  By doubling each '\''
2531** character it escapes that character and allows it to be inserted into
2532** the string.
2533**
2534** For example, assume the string variable zText contains text as follows:
2535**
2536** <blockquote><pre>
2537**  char *zText = "It's a happy day!";
2538** </pre></blockquote>
2539**
2540** One can use this text in an SQL statement as follows:
2541**
2542** <blockquote><pre>
2543**  char *zSQL = sqlite3_mprintf("INSERT INTO table VALUES('%q')", zText);
2544**  sqlite3_exec(db, zSQL, 0, 0, 0);
2545**  sqlite3_free(zSQL);
2546** </pre></blockquote>
2547**
2548** Because the %q format string is used, the '\'' character in zText
2549** is escaped and the SQL generated is as follows:
2550**
2551** <blockquote><pre>
2552**  INSERT INTO table1 VALUES('It''s a happy day!')
2553** </pre></blockquote>
2554**
2555** This is correct.  Had we used %s instead of %q, the generated SQL
2556** would have looked like this:
2557**
2558** <blockquote><pre>
2559**  INSERT INTO table1 VALUES('It's a happy day!');
2560** </pre></blockquote>
2561**
2562** This second example is an SQL syntax error.  As a general rule you should
2563** always use %q instead of %s when inserting text into a string literal.
2564**
2565** ^(The %Q option works like %q except it also adds single quotes around
2566** the outside of the total string.  Additionally, if the parameter in the
2567** argument list is a NULL pointer, %Q substitutes the text "NULL" (without
2568** single quotes).)^  So, for example, one could say:
2569**
2570** <blockquote><pre>
2571**  char *zSQL = sqlite3_mprintf("INSERT INTO table VALUES(%Q)", zText);
2572**  sqlite3_exec(db, zSQL, 0, 0, 0);
2573**  sqlite3_free(zSQL);
2574** </pre></blockquote>
2575**
2576** The code above will render a correct SQL statement in the zSQL
2577** variable even if the zText variable is a NULL pointer.
2578**
2579** ^(The "%w" formatting option is like "%q" except that it expects to
2580** be contained within double-quotes instead of single quotes, and it
2581** escapes the double-quote character instead of the single-quote
2582** character.)^  The "%w" formatting option is intended for safely inserting
2583** table and column names into a constructed SQL statement.
2584**
2585** ^(The "%z" formatting option works like "%s" but with the
2586** addition that after the string has been read and copied into
2587** the result, [sqlite3_free()] is called on the input string.)^
2588*/
2589SQLITE_API char *SQLITE_CDECL sqlite3_mprintf(const char*,...);
2590SQLITE_API char *SQLITE_STDCALL sqlite3_vmprintf(const char*, va_list);
2591SQLITE_API char *SQLITE_CDECL sqlite3_snprintf(int,char*,const char*, ...);
2592SQLITE_API char *SQLITE_STDCALL sqlite3_vsnprintf(int,char*,const char*, va_list);
2593
2594/*
2595** CAPI3REF: Memory Allocation Subsystem
2596**
2597** The SQLite core uses these three routines for all of its own
2598** internal memory allocation needs. "Core" in the previous sentence
2599** does not include operating-system specific VFS implementation.  The
2600** Windows VFS uses native malloc() and free() for some operations.
2601**
2602** ^The sqlite3_malloc() routine returns a pointer to a block
2603** of memory at least N bytes in length, where N is the parameter.
2604** ^If sqlite3_malloc() is unable to obtain sufficient free
2605** memory, it returns a NULL pointer.  ^If the parameter N to
2606** sqlite3_malloc() is zero or negative then sqlite3_malloc() returns
2607** a NULL pointer.
2608**
2609** ^The sqlite3_malloc64(N) routine works just like
2610** sqlite3_malloc(N) except that N is an unsigned 64-bit integer instead
2611** of a signed 32-bit integer.
2612**
2613** ^Calling sqlite3_free() with a pointer previously returned
2614** by sqlite3_malloc() or sqlite3_realloc() releases that memory so
2615** that it might be reused.  ^The sqlite3_free() routine is
2616** a no-op if is called with a NULL pointer.  Passing a NULL pointer
2617** to sqlite3_free() is harmless.  After being freed, memory
2618** should neither be read nor written.  Even reading previously freed
2619** memory might result in a segmentation fault or other severe error.
2620** Memory corruption, a segmentation fault, or other severe error
2621** might result if sqlite3_free() is called with a non-NULL pointer that
2622** was not obtained from sqlite3_malloc() or sqlite3_realloc().
2623**
2624** ^The sqlite3_realloc(X,N) interface attempts to resize a
2625** prior memory allocation X to be at least N bytes.
2626** ^If the X parameter to sqlite3_realloc(X,N)
2627** is a NULL pointer then its behavior is identical to calling
2628** sqlite3_malloc(N).
2629** ^If the N parameter to sqlite3_realloc(X,N) is zero or
2630** negative then the behavior is exactly the same as calling
2631** sqlite3_free(X).
2632** ^sqlite3_realloc(X,N) returns a pointer to a memory allocation
2633** of at least N bytes in size or NULL if insufficient memory is available.
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(X,N) and the prior allocation is freed.
2637** ^If sqlite3_realloc(X,N) returns NULL and N is positive, then the
2638** prior allocation is not freed.
2639**
2640** ^The sqlite3_realloc64(X,N) interfaces works the same as
2641** sqlite3_realloc(X,N) except that N is a 64-bit unsigned integer instead
2642** of a 32-bit signed integer.
2643**
2644** ^If X is a memory allocation previously obtained from sqlite3_malloc(),
2645** sqlite3_malloc64(), sqlite3_realloc(), or sqlite3_realloc64(), then
2646** sqlite3_msize(X) returns the size of that memory allocation in bytes.
2647** ^The value returned by sqlite3_msize(X) might be larger than the number
2648** of bytes requested when X was allocated.  ^If X is a NULL pointer then
2649** sqlite3_msize(X) returns zero.  If X points to something that is not
2650** the beginning of memory allocation, or if it points to a formerly
2651** valid memory allocation that has now been freed, then the behavior
2652** of sqlite3_msize(X) is undefined and possibly harmful.
2653**
2654** ^The memory returned by sqlite3_malloc(), sqlite3_realloc(),
2655** sqlite3_malloc64(), and sqlite3_realloc64()
2656** is always aligned to at least an 8 byte boundary, or to a
2657** 4 byte boundary if the [SQLITE_4_BYTE_ALIGNED_MALLOC] compile-time
2658** option is used.
2659**
2660** In SQLite version 3.5.0 and 3.5.1, it was possible to define
2661** the SQLITE_OMIT_MEMORY_ALLOCATION which would cause the built-in
2662** implementation of these routines to be omitted.  That capability
2663** is no longer provided.  Only built-in memory allocators can be used.
2664**
2665** Prior to SQLite version 3.7.10, the Windows OS interface layer called
2666** the system malloc() and free() directly when converting
2667** filenames between the UTF-8 encoding used by SQLite
2668** and whatever filename encoding is used by the particular Windows
2669** installation.  Memory allocation errors were detected, but
2670** they were reported back as [SQLITE_CANTOPEN] or
2671** [SQLITE_IOERR] rather than [SQLITE_NOMEM].
2672**
2673** The pointer arguments to [sqlite3_free()] and [sqlite3_realloc()]
2674** must be either NULL or else pointers obtained from a prior
2675** invocation of [sqlite3_malloc()] or [sqlite3_realloc()] that have
2676** not yet been released.
2677**
2678** The application must not read or write any part of
2679** a block of memory after it has been released using
2680** [sqlite3_free()] or [sqlite3_realloc()].
2681*/
2682SQLITE_API void *SQLITE_STDCALL sqlite3_malloc(int);
2683SQLITE_API void *SQLITE_STDCALL sqlite3_malloc64(sqlite3_uint64);
2684SQLITE_API void *SQLITE_STDCALL sqlite3_realloc(void*, int);
2685SQLITE_API void *SQLITE_STDCALL sqlite3_realloc64(void*, sqlite3_uint64);
2686SQLITE_API void SQLITE_STDCALL sqlite3_free(void*);
2687SQLITE_API sqlite3_uint64 SQLITE_STDCALL sqlite3_msize(void*);
2688
2689/*
2690** CAPI3REF: Memory Allocator Statistics
2691**
2692** SQLite provides these two interfaces for reporting on the status
2693** of the [sqlite3_malloc()], [sqlite3_free()], and [sqlite3_realloc()]
2694** routines, which form the built-in memory allocation subsystem.
2695**
2696** ^The [sqlite3_memory_used()] routine returns the number of bytes
2697** of memory currently outstanding (malloced but not freed).
2698** ^The [sqlite3_memory_highwater()] routine returns the maximum
2699** value of [sqlite3_memory_used()] since the high-water mark
2700** was last reset.  ^The values returned by [sqlite3_memory_used()] and
2701** [sqlite3_memory_highwater()] include any overhead
2702** added by SQLite in its implementation of [sqlite3_malloc()],
2703** but not overhead added by the any underlying system library
2704** routines that [sqlite3_malloc()] may call.
2705**
2706** ^The memory high-water mark is reset to the current value of
2707** [sqlite3_memory_used()] if and only if the parameter to
2708** [sqlite3_memory_highwater()] is true.  ^The value returned
2709** by [sqlite3_memory_highwater(1)] is the high-water mark
2710** prior to the reset.
2711*/
2712SQLITE_API sqlite3_int64 SQLITE_STDCALL sqlite3_memory_used(void);
2713SQLITE_API sqlite3_int64 SQLITE_STDCALL sqlite3_memory_highwater(int resetFlag);
2714
2715/*
2716** CAPI3REF: Pseudo-Random Number Generator
2717**
2718** SQLite contains a high-quality pseudo-random number generator (PRNG) used to
2719** select random [ROWID | ROWIDs] when inserting new records into a table that
2720** already uses the largest possible [ROWID].  The PRNG is also used for
2721** the build-in random() and randomblob() SQL functions.  This interface allows
2722** applications to access the same PRNG for other purposes.
2723**
2724** ^A call to this routine stores N bytes of randomness into buffer P.
2725** ^The P parameter can be a NULL pointer.
2726**
2727** ^If this routine has not been previously called or if the previous
2728** call had N less than one or a NULL pointer for P, then the PRNG is
2729** seeded using randomness obtained from the xRandomness method of
2730** the default [sqlite3_vfs] object.
2731** ^If the previous call to this routine had an N of 1 or more and a
2732** non-NULL P then the pseudo-randomness is generated
2733** internally and without recourse to the [sqlite3_vfs] xRandomness
2734** method.
2735*/
2736SQLITE_API void SQLITE_STDCALL sqlite3_randomness(int N, void *P);
2737
2738/*
2739** CAPI3REF: Compile-Time Authorization Callbacks
2740** METHOD: sqlite3
2741**
2742** ^This routine registers an authorizer callback with a particular
2743** [database connection], supplied in the first argument.
2744** ^The authorizer callback is invoked as SQL statements are being compiled
2745** by [sqlite3_prepare()] or its variants [sqlite3_prepare_v2()],
2746** [sqlite3_prepare16()] and [sqlite3_prepare16_v2()].  ^At various
2747** points during the compilation process, as logic is being created
2748** to perform various actions, the authorizer callback is invoked to
2749** see if those actions are allowed.  ^The authorizer callback should
2750** return [SQLITE_OK] to allow the action, [SQLITE_IGNORE] to disallow the
2751** specific action but allow the SQL statement to continue to be
2752** compiled, or [SQLITE_DENY] to cause the entire SQL statement to be
2753** rejected with an error.  ^If the authorizer callback returns
2754** any value other than [SQLITE_IGNORE], [SQLITE_OK], or [SQLITE_DENY]
2755** then the [sqlite3_prepare_v2()] or equivalent call that triggered
2756** the authorizer will fail with an error message.
2757**
2758** When the callback returns [SQLITE_OK], that means the operation
2759** requested is ok.  ^When the callback returns [SQLITE_DENY], the
2760** [sqlite3_prepare_v2()] or equivalent call that triggered the
2761** authorizer will fail with an error message explaining that
2762** access is denied.
2763**
2764** ^The first parameter to the authorizer callback is a copy of the third
2765** parameter to the sqlite3_set_authorizer() interface. ^The second parameter
2766** to the callback is an integer [SQLITE_COPY | action code] that specifies
2767** the particular action to be authorized. ^The third through sixth parameters
2768** to the callback are zero-terminated strings that contain additional
2769** details about the action to be authorized.
2770**
2771** ^If the action code is [SQLITE_READ]
2772** and the callback returns [SQLITE_IGNORE] then the
2773** [prepared statement] statement is constructed to substitute
2774** a NULL value in place of the table column that would have
2775** been read if [SQLITE_OK] had been returned.  The [SQLITE_IGNORE]
2776** return can be used to deny an untrusted user access to individual
2777** columns of a table.
2778** ^If the action code is [SQLITE_DELETE] and the callback returns
2779** [SQLITE_IGNORE] then the [DELETE] operation proceeds but the
2780** [truncate optimization] is disabled and all rows are deleted individually.
2781**
2782** An authorizer is used when [sqlite3_prepare | preparing]
2783** SQL statements from an untrusted source, to ensure that the SQL statements
2784** do not try to access data they are not allowed to see, or that they do not
2785** try to execute malicious statements that damage the database.  For
2786** example, an application may allow a user to enter arbitrary
2787** SQL queries for evaluation by a database.  But the application does
2788** not want the user to be able to make arbitrary changes to the
2789** database.  An authorizer could then be put in place while the
2790** user-entered SQL is being [sqlite3_prepare | prepared] that
2791** disallows everything except [SELECT] statements.
2792**
2793** Applications that need to process SQL from untrusted sources
2794** might also consider lowering resource limits using [sqlite3_limit()]
2795** and limiting database size using the [max_page_count] [PRAGMA]
2796** in addition to using an authorizer.
2797**
2798** ^(Only a single authorizer can be in place on a database connection
2799** at a time.  Each call to sqlite3_set_authorizer overrides the
2800** previous call.)^  ^Disable the authorizer by installing a NULL callback.
2801** The authorizer is disabled by default.
2802**
2803** The authorizer callback must not do anything that will modify
2804** the database connection that invoked the authorizer callback.
2805** Note that [sqlite3_prepare_v2()] and [sqlite3_step()] both modify their
2806** database connections for the meaning of "modify" in this paragraph.
2807**
2808** ^When [sqlite3_prepare_v2()] is used to prepare a statement, the
2809** statement might be re-prepared during [sqlite3_step()] due to a
2810** schema change.  Hence, the application should ensure that the
2811** correct authorizer callback remains in place during the [sqlite3_step()].
2812**
2813** ^Note that the authorizer callback is invoked only during
2814** [sqlite3_prepare()] or its variants.  Authorization is not
2815** performed during statement evaluation in [sqlite3_step()], unless
2816** as stated in the previous paragraph, sqlite3_step() invokes
2817** sqlite3_prepare_v2() to reprepare a statement after a schema change.
2818*/
2819SQLITE_API int SQLITE_STDCALL sqlite3_set_authorizer(
2820  sqlite3*,
2821  int (*xAuth)(void*,int,const char*,const char*,const char*,const char*),
2822  void *pUserData
2823);
2824
2825/*
2826** CAPI3REF: Authorizer Return Codes
2827**
2828** The [sqlite3_set_authorizer | authorizer callback function] must
2829** return either [SQLITE_OK] or one of these two constants in order
2830** to signal SQLite whether or not the action is permitted.  See the
2831** [sqlite3_set_authorizer | authorizer documentation] for additional
2832** information.
2833**
2834** Note that SQLITE_IGNORE is also used as a [conflict resolution mode]
2835** returned from the [sqlite3_vtab_on_conflict()] interface.
2836*/
2837#define SQLITE_DENY   1   /* Abort the SQL statement with an error */
2838#define SQLITE_IGNORE 2   /* Don't allow access, but don't generate an error */
2839
2840/*
2841** CAPI3REF: Authorizer Action Codes
2842**
2843** The [sqlite3_set_authorizer()] interface registers a callback function
2844** that is invoked to authorize certain SQL statement actions.  The
2845** second parameter to the callback is an integer code that specifies
2846** what action is being authorized.  These are the integer action codes that
2847** the authorizer callback may be passed.
2848**
2849** These action code values signify what kind of operation is to be
2850** authorized.  The 3rd and 4th parameters to the authorization
2851** callback function will be parameters or NULL depending on which of these
2852** codes is used as the second parameter.  ^(The 5th parameter to the
2853** authorizer callback is the name of the database ("main", "temp",
2854** etc.) if applicable.)^  ^The 6th parameter to the authorizer callback
2855** is the name of the inner-most trigger or view that is responsible for
2856** the access attempt or NULL if this access attempt is directly from
2857** top-level SQL code.
2858*/
2859/******************************************* 3rd ************ 4th ***********/
2860#define SQLITE_CREATE_INDEX          1   /* Index Name      Table Name      */
2861#define SQLITE_CREATE_TABLE          2   /* Table Name      NULL            */
2862#define SQLITE_CREATE_TEMP_INDEX     3   /* Index Name      Table Name      */
2863#define SQLITE_CREATE_TEMP_TABLE     4   /* Table Name      NULL            */
2864#define SQLITE_CREATE_TEMP_TRIGGER   5   /* Trigger Name    Table Name      */
2865#define SQLITE_CREATE_TEMP_VIEW      6   /* View Name       NULL            */
2866#define SQLITE_CREATE_TRIGGER        7   /* Trigger Name    Table Name      */
2867#define SQLITE_CREATE_VIEW           8   /* View Name       NULL            */
2868#define SQLITE_DELETE                9   /* Table Name      NULL            */
2869#define SQLITE_DROP_INDEX           10   /* Index Name      Table Name      */
2870#define SQLITE_DROP_TABLE           11   /* Table Name      NULL            */
2871#define SQLITE_DROP_TEMP_INDEX      12   /* Index Name      Table Name      */
2872#define SQLITE_DROP_TEMP_TABLE      13   /* Table Name      NULL            */
2873#define SQLITE_DROP_TEMP_TRIGGER    14   /* Trigger Name    Table Name      */
2874#define SQLITE_DROP_TEMP_VIEW       15   /* View Name       NULL            */
2875#define SQLITE_DROP_TRIGGER         16   /* Trigger Name    Table Name      */
2876#define SQLITE_DROP_VIEW            17   /* View Name       NULL            */
2877#define SQLITE_INSERT               18   /* Table Name      NULL            */
2878#define SQLITE_PRAGMA               19   /* Pragma Name     1st arg or NULL */
2879#define SQLITE_READ                 20   /* Table Name      Column Name     */
2880#define SQLITE_SELECT               21   /* NULL            NULL            */
2881#define SQLITE_TRANSACTION          22   /* Operation       NULL            */
2882#define SQLITE_UPDATE               23   /* Table Name      Column Name     */
2883#define SQLITE_ATTACH               24   /* Filename        NULL            */
2884#define SQLITE_DETACH               25   /* Database Name   NULL            */
2885#define SQLITE_ALTER_TABLE          26   /* Database Name   Table Name      */
2886#define SQLITE_REINDEX              27   /* Index Name      NULL            */
2887#define SQLITE_ANALYZE              28   /* Table Name      NULL            */
2888#define SQLITE_CREATE_VTABLE        29   /* Table Name      Module Name     */
2889#define SQLITE_DROP_VTABLE          30   /* Table Name      Module Name     */
2890#define SQLITE_FUNCTION             31   /* NULL            Function Name   */
2891#define SQLITE_SAVEPOINT            32   /* Operation       Savepoint Name  */
2892#define SQLITE_COPY                  0   /* No longer used */
2893#define SQLITE_RECURSIVE            33   /* NULL            NULL            */
2894
2895/*
2896** CAPI3REF: Tracing And Profiling Functions
2897** METHOD: sqlite3
2898**
2899** These routines register callback functions that can be used for
2900** tracing and profiling the execution of SQL statements.
2901**
2902** ^The callback function registered by sqlite3_trace() is invoked at
2903** various times when an SQL statement is being run by [sqlite3_step()].
2904** ^The sqlite3_trace() callback is invoked with a UTF-8 rendering of the
2905** SQL statement text as the statement first begins executing.
2906** ^(Additional sqlite3_trace() callbacks might occur
2907** as each triggered subprogram is entered.  The callbacks for triggers
2908** contain a UTF-8 SQL comment that identifies the trigger.)^
2909**
2910** The [SQLITE_TRACE_SIZE_LIMIT] compile-time option can be used to limit
2911** the length of [bound parameter] expansion in the output of sqlite3_trace().
2912**
2913** ^The callback function registered by sqlite3_profile() is invoked
2914** as each SQL statement finishes.  ^The profile callback contains
2915** the original statement text and an estimate of wall-clock time
2916** of how long that statement took to run.  ^The profile callback
2917** time is in units of nanoseconds, however the current implementation
2918** is only capable of millisecond resolution so the six least significant
2919** digits in the time are meaningless.  Future versions of SQLite
2920** might provide greater resolution on the profiler callback.  The
2921** sqlite3_profile() function is considered experimental and is
2922** subject to change in future versions of SQLite.
2923*/
2924SQLITE_API void *SQLITE_STDCALL sqlite3_trace(sqlite3*, void(*xTrace)(void*,const char*), void*);
2925SQLITE_API SQLITE_EXPERIMENTAL void *SQLITE_STDCALL sqlite3_profile(sqlite3*,
2926   void(*xProfile)(void*,const char*,sqlite3_uint64), void*);
2927
2928/*
2929** CAPI3REF: Query Progress Callbacks
2930** METHOD: sqlite3
2931**
2932** ^The sqlite3_progress_handler(D,N,X,P) interface causes the callback
2933** function X to be invoked periodically during long running calls to
2934** [sqlite3_exec()], [sqlite3_step()] and [sqlite3_get_table()] for
2935** database connection D.  An example use for this
2936** interface is to keep a GUI updated during a large query.
2937**
2938** ^The parameter P is passed through as the only parameter to the
2939** callback function X.  ^The parameter N is the approximate number of
2940** [virtual machine instructions] that are evaluated between successive
2941** invocations of the callback X.  ^If N is less than one then the progress
2942** handler is disabled.
2943**
2944** ^Only a single progress handler may be defined at one time per
2945** [database connection]; setting a new progress handler cancels the
2946** old one.  ^Setting parameter X to NULL disables the progress handler.
2947** ^The progress handler is also disabled by setting N to a value less
2948** than 1.
2949**
2950** ^If the progress callback returns non-zero, the operation is
2951** interrupted.  This feature can be used to implement a
2952** "Cancel" button on a GUI progress dialog box.
2953**
2954** The progress handler callback must not do anything that will modify
2955** the database connection that invoked the progress handler.
2956** Note that [sqlite3_prepare_v2()] and [sqlite3_step()] both modify their
2957** database connections for the meaning of "modify" in this paragraph.
2958**
2959*/
2960SQLITE_API void SQLITE_STDCALL sqlite3_progress_handler(sqlite3*, int, int(*)(void*), void*);
2961
2962/*
2963** CAPI3REF: Opening A New Database Connection
2964** CONSTRUCTOR: sqlite3
2965**
2966** ^These routines open an SQLite database file as specified by the
2967** filename argument. ^The filename argument is interpreted as UTF-8 for
2968** sqlite3_open() and sqlite3_open_v2() and as UTF-16 in the native byte
2969** order for sqlite3_open16(). ^(A [database connection] handle is usually
2970** returned in *ppDb, even if an error occurs.  The only exception is that
2971** if SQLite is unable to allocate memory to hold the [sqlite3] object,
2972** a NULL will be written into *ppDb instead of a pointer to the [sqlite3]
2973** object.)^ ^(If the database is opened (and/or created) successfully, then
2974** [SQLITE_OK] is returned.  Otherwise an [error code] is returned.)^ ^The
2975** [sqlite3_errmsg()] or [sqlite3_errmsg16()] routines can be used to obtain
2976** an English language description of the error following a failure of any
2977** of the sqlite3_open() routines.
2978**
2979** ^The default encoding will be UTF-8 for databases created using
2980** sqlite3_open() or sqlite3_open_v2().  ^The default encoding for databases
2981** created using sqlite3_open16() will be UTF-16 in the native byte order.
2982**
2983** Whether or not an error occurs when it is opened, resources
2984** associated with the [database connection] handle should be released by
2985** passing it to [sqlite3_close()] when it is no longer required.
2986**
2987** The sqlite3_open_v2() interface works like sqlite3_open()
2988** except that it accepts two additional parameters for additional control
2989** over the new database connection.  ^(The flags parameter to
2990** sqlite3_open_v2() can take one of
2991** the following three values, optionally combined with the
2992** [SQLITE_OPEN_NOMUTEX], [SQLITE_OPEN_FULLMUTEX], [SQLITE_OPEN_SHAREDCACHE],
2993** [SQLITE_OPEN_PRIVATECACHE], and/or [SQLITE_OPEN_URI] flags:)^
2994**
2995** <dl>
2996** ^(<dt>[SQLITE_OPEN_READONLY]</dt>
2997** <dd>The database is opened in read-only mode.  If the database does not
2998** already exist, an error is returned.</dd>)^
2999**
3000** ^(<dt>[SQLITE_OPEN_READWRITE]</dt>
3001** <dd>The database is opened for reading and writing if possible, or reading
3002** only if the file is write protected by the operating system.  In either
3003** case the database must already exist, otherwise an error is returned.</dd>)^
3004**
3005** ^(<dt>[SQLITE_OPEN_READWRITE] | [SQLITE_OPEN_CREATE]</dt>
3006** <dd>The database is opened for reading and writing, and is created if
3007** it does not already exist. This is the behavior that is always used for
3008** sqlite3_open() and sqlite3_open16().</dd>)^
3009** </dl>
3010**
3011** If the 3rd parameter to sqlite3_open_v2() is not one of the
3012** combinations shown above optionally combined with other
3013** [SQLITE_OPEN_READONLY | SQLITE_OPEN_* bits]
3014** then the behavior is undefined.
3015**
3016** ^If the [SQLITE_OPEN_NOMUTEX] flag is set, then the database connection
3017** opens in the multi-thread [threading mode] as long as the single-thread
3018** mode has not been set at compile-time or start-time.  ^If the
3019** [SQLITE_OPEN_FULLMUTEX] flag is set then the database connection opens
3020** in the serialized [threading mode] unless single-thread was
3021** previously selected at compile-time or start-time.
3022** ^The [SQLITE_OPEN_SHAREDCACHE] flag causes the database connection to be
3023** eligible to use [shared cache mode], regardless of whether or not shared
3024** cache is enabled using [sqlite3_enable_shared_cache()].  ^The
3025** [SQLITE_OPEN_PRIVATECACHE] flag causes the database connection to not
3026** participate in [shared cache mode] even if it is enabled.
3027**
3028** ^The fourth parameter to sqlite3_open_v2() is the name of the
3029** [sqlite3_vfs] object that defines the operating system interface that
3030** the new database connection should use.  ^If the fourth parameter is
3031** a NULL pointer then the default [sqlite3_vfs] object is used.
3032**
3033** ^If the filename is ":memory:", then a private, temporary in-memory database
3034** is created for the connection.  ^This in-memory database will vanish when
3035** the database connection is closed.  Future versions of SQLite might
3036** make use of additional special filenames that begin with the ":" character.
3037** It is recommended that when a database filename actually does begin with
3038** a ":" character you should prefix the filename with a pathname such as
3039** "./" to avoid ambiguity.
3040**
3041** ^If the filename is an empty string, then a private, temporary
3042** on-disk database will be created.  ^This private database will be
3043** automatically deleted as soon as the database connection is closed.
3044**
3045** [[URI filenames in sqlite3_open()]] <h3>URI Filenames</h3>
3046**
3047** ^If [URI filename] interpretation is enabled, and the filename argument
3048** begins with "file:", then the filename is interpreted as a URI. ^URI
3049** filename interpretation is enabled if the [SQLITE_OPEN_URI] flag is
3050** set in the fourth argument to sqlite3_open_v2(), or if it has
3051** been enabled globally using the [SQLITE_CONFIG_URI] option with the
3052** [sqlite3_config()] method or by the [SQLITE_USE_URI] compile-time option.
3053** As of SQLite version 3.7.7, URI filename interpretation is turned off
3054** by default, but future releases of SQLite might enable URI filename
3055** interpretation by default.  See "[URI filenames]" for additional
3056** information.
3057**
3058** URI filenames are parsed according to RFC 3986. ^If the URI contains an
3059** authority, then it must be either an empty string or the string
3060** "localhost". ^If the authority is not an empty string or "localhost", an
3061** error is returned to the caller. ^The fragment component of a URI, if
3062** present, is ignored.
3063**
3064** ^SQLite uses the path component of the URI as the name of the disk file
3065** which contains the database. ^If the path begins with a '/' character,
3066** then it is interpreted as an absolute path. ^If the path does not begin
3067** with a '/' (meaning that the authority section is omitted from the URI)
3068** then the path is interpreted as a relative path.
3069** ^(On windows, the first component of an absolute path
3070** is a drive specification (e.g. "C:").)^
3071**
3072** [[core URI query parameters]]
3073** The query component of a URI may contain parameters that are interpreted
3074** either by SQLite itself, or by a [VFS | custom VFS implementation].
3075** SQLite and its built-in [VFSes] interpret the
3076** following query parameters:
3077**
3078** <ul>
3079**   <li> <b>vfs</b>: ^The "vfs" parameter may be used to specify the name of
3080**     a VFS object that provides the operating system interface that should
3081**     be used to access the database file on disk. ^If this option is set to
3082**     an empty string the default VFS object is used. ^Specifying an unknown
3083**     VFS is an error. ^If sqlite3_open_v2() is used and the vfs option is
3084**     present, then the VFS specified by the option takes precedence over
3085**     the value passed as the fourth parameter to sqlite3_open_v2().
3086**
3087**   <li> <b>mode</b>: ^(The mode parameter may be set to either "ro", "rw",
3088**     "rwc", or "memory". Attempting to set it to any other value is
3089**     an error)^.
3090**     ^If "ro" is specified, then the database is opened for read-only
3091**     access, just as if the [SQLITE_OPEN_READONLY] flag had been set in the
3092**     third argument to sqlite3_open_v2(). ^If the mode option is set to
3093**     "rw", then the database is opened for read-write (but not create)
3094**     access, as if SQLITE_OPEN_READWRITE (but not SQLITE_OPEN_CREATE) had
3095**     been set. ^Value "rwc" is equivalent to setting both
3096**     SQLITE_OPEN_READWRITE and SQLITE_OPEN_CREATE.  ^If the mode option is
3097**     set to "memory" then a pure [in-memory database] that never reads
3098**     or writes from disk is used. ^It is an error to specify a value for
3099**     the mode parameter that is less restrictive than that specified by
3100**     the flags passed in the third parameter to sqlite3_open_v2().
3101**
3102**   <li> <b>cache</b>: ^The cache parameter may be set to either "shared" or
3103**     "private". ^Setting it to "shared" is equivalent to setting the
3104**     SQLITE_OPEN_SHAREDCACHE bit in the flags argument passed to
3105**     sqlite3_open_v2(). ^Setting the cache parameter to "private" is
3106**     equivalent to setting the SQLITE_OPEN_PRIVATECACHE bit.
3107**     ^If sqlite3_open_v2() is used and the "cache" parameter is present in
3108**     a URI filename, its value overrides any behavior requested by setting
3109**     SQLITE_OPEN_PRIVATECACHE or SQLITE_OPEN_SHAREDCACHE flag.
3110**
3111**  <li> <b>psow</b>: ^The psow parameter indicates whether or not the
3112**     [powersafe overwrite] property does or does not apply to the
3113**     storage media on which the database file resides.
3114**
3115**  <li> <b>nolock</b>: ^The nolock parameter is a boolean query parameter
3116**     which if set disables file locking in rollback journal modes.  This
3117**     is useful for accessing a database on a filesystem that does not
3118**     support locking.  Caution:  Database corruption might result if two
3119**     or more processes write to the same database and any one of those
3120**     processes uses nolock=1.
3121**
3122**  <li> <b>immutable</b>: ^The immutable parameter is a boolean query
3123**     parameter that indicates that the database file is stored on
3124**     read-only media.  ^When immutable is set, SQLite assumes that the
3125**     database file cannot be changed, even by a process with higher
3126**     privilege, and so the database is opened read-only and all locking
3127**     and change detection is disabled.  Caution: Setting the immutable
3128**     property on a database file that does in fact change can result
3129**     in incorrect query results and/or [SQLITE_CORRUPT] errors.
3130**     See also: [SQLITE_IOCAP_IMMUTABLE].
3131**
3132** </ul>
3133**
3134** ^Specifying an unknown parameter in the query component of a URI is not an
3135** error.  Future versions of SQLite might understand additional query
3136** parameters.  See "[query parameters with special meaning to SQLite]" for
3137** additional information.
3138**
3139** [[URI filename examples]] <h3>URI filename examples</h3>
3140**
3141** <table border="1" align=center cellpadding=5>
3142** <tr><th> URI filenames <th> Results
3143** <tr><td> file:data.db <td>
3144**          Open the file "data.db" in the current directory.
3145** <tr><td> file:/home/fred/data.db<br>
3146**          file:///home/fred/data.db <br>
3147**          file://localhost/home/fred/data.db <br> <td>
3148**          Open the database file "/home/fred/data.db".
3149** <tr><td> file://darkstar/home/fred/data.db <td>
3150**          An error. "darkstar" is not a recognized authority.
3151** <tr><td style="white-space:nowrap">
3152**          file:///C:/Documents%20and%20Settings/fred/Desktop/data.db
3153**     <td> Windows only: Open the file "data.db" on fred's desktop on drive
3154**          C:. Note that the %20 escaping in this example is not strictly
3155**          necessary - space characters can be used literally
3156**          in URI filenames.
3157** <tr><td> file:data.db?mode=ro&cache=private <td>
3158**          Open file "data.db" in the current directory for read-only access.
3159**          Regardless of whether or not shared-cache mode is enabled by
3160**          default, use a private cache.
3161** <tr><td> file:/home/fred/data.db?vfs=unix-dotfile <td>
3162**          Open file "/home/fred/data.db". Use the special VFS "unix-dotfile"
3163**          that uses dot-files in place of posix advisory locking.
3164** <tr><td> file:data.db?mode=readonly <td>
3165**          An error. "readonly" is not a valid option for the "mode" parameter.
3166** </table>
3167**
3168** ^URI hexadecimal escape sequences (%HH) are supported within the path and
3169** query components of a URI. A hexadecimal escape sequence consists of a
3170** percent sign - "%" - followed by exactly two hexadecimal digits
3171** specifying an octet value. ^Before the path or query components of a
3172** URI filename are interpreted, they are encoded using UTF-8 and all
3173** hexadecimal escape sequences replaced by a single byte containing the
3174** corresponding octet. If this process generates an invalid UTF-8 encoding,
3175** the results are undefined.
3176**
3177** <b>Note to Windows users:</b>  The encoding used for the filename argument
3178** of sqlite3_open() and sqlite3_open_v2() must be UTF-8, not whatever
3179** codepage is currently defined.  Filenames containing international
3180** characters must be converted to UTF-8 prior to passing them into
3181** sqlite3_open() or sqlite3_open_v2().
3182**
3183** <b>Note to Windows Runtime users:</b>  The temporary directory must be set
3184** prior to calling sqlite3_open() or sqlite3_open_v2().  Otherwise, various
3185** features that require the use of temporary files may fail.
3186**
3187** See also: [sqlite3_temp_directory]
3188*/
3189SQLITE_API int SQLITE_STDCALL sqlite3_open(
3190  const char *filename,   /* Database filename (UTF-8) */
3191  sqlite3 **ppDb          /* OUT: SQLite db handle */
3192);
3193SQLITE_API int SQLITE_STDCALL sqlite3_open16(
3194  const void *filename,   /* Database filename (UTF-16) */
3195  sqlite3 **ppDb          /* OUT: SQLite db handle */
3196);
3197SQLITE_API int SQLITE_STDCALL sqlite3_open_v2(
3198  const char *filename,   /* Database filename (UTF-8) */
3199  sqlite3 **ppDb,         /* OUT: SQLite db handle */
3200  int flags,              /* Flags */
3201  const char *zVfs        /* Name of VFS module to use */
3202);
3203
3204/*
3205** CAPI3REF: Obtain Values For URI Parameters
3206**
3207** These are utility routines, useful to VFS implementations, that check
3208** to see if a database file was a URI that contained a specific query
3209** parameter, and if so obtains the value of that query parameter.
3210**
3211** If F is the database filename pointer passed into the xOpen() method of
3212** a VFS implementation when the flags parameter to xOpen() has one or
3213** more of the [SQLITE_OPEN_URI] or [SQLITE_OPEN_MAIN_DB] bits set and
3214** P is the name of the query parameter, then
3215** sqlite3_uri_parameter(F,P) returns the value of the P
3216** parameter if it exists or a NULL pointer if P does not appear as a
3217** query parameter on F.  If P is a query parameter of F
3218** has no explicit value, then sqlite3_uri_parameter(F,P) returns
3219** a pointer to an empty string.
3220**
3221** The sqlite3_uri_boolean(F,P,B) routine assumes that P is a boolean
3222** parameter and returns true (1) or false (0) according to the value
3223** of P.  The sqlite3_uri_boolean(F,P,B) routine returns true (1) if the
3224** value of query parameter P is one of "yes", "true", or "on" in any
3225** case or if the value begins with a non-zero number.  The
3226** sqlite3_uri_boolean(F,P,B) routines returns false (0) if the value of
3227** query parameter P is one of "no", "false", or "off" in any case or
3228** if the value begins with a numeric zero.  If P is not a query
3229** parameter on F or if the value of P is does not match any of the
3230** above, then sqlite3_uri_boolean(F,P,B) returns (B!=0).
3231**
3232** The sqlite3_uri_int64(F,P,D) routine converts the value of P into a
3233** 64-bit signed integer and returns that integer, or D if P does not
3234** exist.  If the value of P is something other than an integer, then
3235** zero is returned.
3236**
3237** If F is a NULL pointer, then sqlite3_uri_parameter(F,P) returns NULL and
3238** sqlite3_uri_boolean(F,P,B) returns B.  If F is not a NULL pointer and
3239** is not a database file pathname pointer that SQLite passed into the xOpen
3240** VFS method, then the behavior of this routine is undefined and probably
3241** undesirable.
3242*/
3243SQLITE_API const char *SQLITE_STDCALL sqlite3_uri_parameter(const char *zFilename, const char *zParam);
3244SQLITE_API int SQLITE_STDCALL sqlite3_uri_boolean(const char *zFile, const char *zParam, int bDefault);
3245SQLITE_API sqlite3_int64 SQLITE_STDCALL sqlite3_uri_int64(const char*, const char*, sqlite3_int64);
3246
3247
3248/*
3249** CAPI3REF: Error Codes And Messages
3250** METHOD: sqlite3
3251**
3252** ^If the most recent sqlite3_* API call associated with
3253** [database connection] D failed, then the sqlite3_errcode(D) interface
3254** returns the numeric [result code] or [extended result code] for that
3255** API call.
3256** If the most recent API call was successful,
3257** then the return value from sqlite3_errcode() is undefined.
3258** ^The sqlite3_extended_errcode()
3259** interface is the same except that it always returns the
3260** [extended result code] even when extended result codes are
3261** disabled.
3262**
3263** ^The sqlite3_errmsg() and sqlite3_errmsg16() return English-language
3264** text that describes the error, as either UTF-8 or UTF-16 respectively.
3265** ^(Memory to hold the error message string is managed internally.
3266** The application does not need to worry about freeing the result.
3267** However, the error string might be overwritten or deallocated by
3268** subsequent calls to other SQLite interface functions.)^
3269**
3270** ^The sqlite3_errstr() interface returns the English-language text
3271** that describes the [result code], as UTF-8.
3272** ^(Memory to hold the error message string is managed internally
3273** and must not be freed by the application)^.
3274**
3275** When the serialized [threading mode] is in use, it might be the
3276** case that a second error occurs on a separate thread in between
3277** the time of the first error and the call to these interfaces.
3278** When that happens, the second error will be reported since these
3279** interfaces always report the most recent result.  To avoid
3280** this, each thread can obtain exclusive use of the [database connection] D
3281** by invoking [sqlite3_mutex_enter]([sqlite3_db_mutex](D)) before beginning
3282** to use D and invoking [sqlite3_mutex_leave]([sqlite3_db_mutex](D)) after
3283** all calls to the interfaces listed here are completed.
3284**
3285** If an interface fails with SQLITE_MISUSE, that means the interface
3286** was invoked incorrectly by the application.  In that case, the
3287** error code and message may or may not be set.
3288*/
3289SQLITE_API int SQLITE_STDCALL sqlite3_errcode(sqlite3 *db);
3290SQLITE_API int SQLITE_STDCALL sqlite3_extended_errcode(sqlite3 *db);
3291SQLITE_API const char *SQLITE_STDCALL sqlite3_errmsg(sqlite3*);
3292SQLITE_API const void *SQLITE_STDCALL sqlite3_errmsg16(sqlite3*);
3293SQLITE_API const char *SQLITE_STDCALL sqlite3_errstr(int);
3294
3295/*
3296** CAPI3REF: Prepared Statement Object
3297** KEYWORDS: {prepared statement} {prepared statements}
3298**
3299** An instance of this object represents a single SQL statement that
3300** has been compiled into binary form and is ready to be evaluated.
3301**
3302** Think of each SQL statement as a separate computer program.  The
3303** original SQL text is source code.  A prepared statement object
3304** is the compiled object code.  All SQL must be converted into a
3305** prepared statement before it can be run.
3306**
3307** The life-cycle of a prepared statement object usually goes like this:
3308**
3309** <ol>
3310** <li> Create the prepared statement object using [sqlite3_prepare_v2()].
3311** <li> Bind values to [parameters] using the sqlite3_bind_*()
3312**      interfaces.
3313** <li> Run the SQL by calling [sqlite3_step()] one or more times.
3314** <li> Reset the prepared statement using [sqlite3_reset()] then go back
3315**      to step 2.  Do this zero or more times.
3316** <li> Destroy the object using [sqlite3_finalize()].
3317** </ol>
3318*/
3319typedef struct sqlite3_stmt sqlite3_stmt;
3320
3321/*
3322** CAPI3REF: Run-time Limits
3323** METHOD: sqlite3
3324**
3325** ^(This interface allows the size of various constructs to be limited
3326** on a connection by connection basis.  The first parameter is the
3327** [database connection] whose limit is to be set or queried.  The
3328** second parameter is one of the [limit categories] that define a
3329** class of constructs to be size limited.  The third parameter is the
3330** new limit for that construct.)^
3331**
3332** ^If the new limit is a negative number, the limit is unchanged.
3333** ^(For each limit category SQLITE_LIMIT_<i>NAME</i> there is a
3334** [limits | hard upper bound]
3335** set at compile-time by a C preprocessor macro called
3336** [limits | SQLITE_MAX_<i>NAME</i>].
3337** (The "_LIMIT_" in the name is changed to "_MAX_".))^
3338** ^Attempts to increase a limit above its hard upper bound are
3339** silently truncated to the hard upper bound.
3340**
3341** ^Regardless of whether or not the limit was changed, the
3342** [sqlite3_limit()] interface returns the prior value of the limit.
3343** ^Hence, to find the current value of a limit without changing it,
3344** simply invoke this interface with the third parameter set to -1.
3345**
3346** Run-time limits are intended for use in applications that manage
3347** both their own internal database and also databases that are controlled
3348** by untrusted external sources.  An example application might be a
3349** web browser that has its own databases for storing history and
3350** separate databases controlled by JavaScript applications downloaded
3351** off the Internet.  The internal databases can be given the
3352** large, default limits.  Databases managed by external sources can
3353** be given much smaller limits designed to prevent a denial of service
3354** attack.  Developers might also want to use the [sqlite3_set_authorizer()]
3355** interface to further control untrusted SQL.  The size of the database
3356** created by an untrusted script can be contained using the
3357** [max_page_count] [PRAGMA].
3358**
3359** New run-time limit categories may be added in future releases.
3360*/
3361SQLITE_API int SQLITE_STDCALL sqlite3_limit(sqlite3*, int id, int newVal);
3362
3363/*
3364** CAPI3REF: Run-Time Limit Categories
3365** KEYWORDS: {limit category} {*limit categories}
3366**
3367** These constants define various performance limits
3368** that can be lowered at run-time using [sqlite3_limit()].
3369** The synopsis of the meanings of the various limits is shown below.
3370** Additional information is available at [limits | Limits in SQLite].
3371**
3372** <dl>
3373** [[SQLITE_LIMIT_LENGTH]] ^(<dt>SQLITE_LIMIT_LENGTH</dt>
3374** <dd>The maximum size of any string or BLOB or table row, in bytes.<dd>)^
3375**
3376** [[SQLITE_LIMIT_SQL_LENGTH]] ^(<dt>SQLITE_LIMIT_SQL_LENGTH</dt>
3377** <dd>The maximum length of an SQL statement, in bytes.</dd>)^
3378**
3379** [[SQLITE_LIMIT_COLUMN]] ^(<dt>SQLITE_LIMIT_COLUMN</dt>
3380** <dd>The maximum number of columns in a table definition or in the
3381** result set of a [SELECT] or the maximum number of columns in an index
3382** or in an ORDER BY or GROUP BY clause.</dd>)^
3383**
3384** [[SQLITE_LIMIT_EXPR_DEPTH]] ^(<dt>SQLITE_LIMIT_EXPR_DEPTH</dt>
3385** <dd>The maximum depth of the parse tree on any expression.</dd>)^
3386**
3387** [[SQLITE_LIMIT_COMPOUND_SELECT]] ^(<dt>SQLITE_LIMIT_COMPOUND_SELECT</dt>
3388** <dd>The maximum number of terms in a compound SELECT statement.</dd>)^
3389**
3390** [[SQLITE_LIMIT_VDBE_OP]] ^(<dt>SQLITE_LIMIT_VDBE_OP</dt>
3391** <dd>The maximum number of instructions in a virtual machine program
3392** used to implement an SQL statement.  This limit is not currently
3393** enforced, though that might be added in some future release of
3394** SQLite.</dd>)^
3395**
3396** [[SQLITE_LIMIT_FUNCTION_ARG]] ^(<dt>SQLITE_LIMIT_FUNCTION_ARG</dt>
3397** <dd>The maximum number of arguments on a function.</dd>)^
3398**
3399** [[SQLITE_LIMIT_ATTACHED]] ^(<dt>SQLITE_LIMIT_ATTACHED</dt>
3400** <dd>The maximum number of [ATTACH | attached databases].)^</dd>
3401**
3402** [[SQLITE_LIMIT_LIKE_PATTERN_LENGTH]]
3403** ^(<dt>SQLITE_LIMIT_LIKE_PATTERN_LENGTH</dt>
3404** <dd>The maximum length of the pattern argument to the [LIKE] or
3405** [GLOB] operators.</dd>)^
3406**
3407** [[SQLITE_LIMIT_VARIABLE_NUMBER]]
3408** ^(<dt>SQLITE_LIMIT_VARIABLE_NUMBER</dt>
3409** <dd>The maximum index number of any [parameter] in an SQL statement.)^
3410**
3411** [[SQLITE_LIMIT_TRIGGER_DEPTH]] ^(<dt>SQLITE_LIMIT_TRIGGER_DEPTH</dt>
3412** <dd>The maximum depth of recursion for triggers.</dd>)^
3413**
3414** [[SQLITE_LIMIT_WORKER_THREADS]] ^(<dt>SQLITE_LIMIT_WORKER_THREADS</dt>
3415** <dd>The maximum number of auxiliary worker threads that a single
3416** [prepared statement] may start.</dd>)^
3417** </dl>
3418*/
3419#define SQLITE_LIMIT_LENGTH                    0
3420#define SQLITE_LIMIT_SQL_LENGTH                1
3421#define SQLITE_LIMIT_COLUMN                    2
3422#define SQLITE_LIMIT_EXPR_DEPTH                3
3423#define SQLITE_LIMIT_COMPOUND_SELECT           4
3424#define SQLITE_LIMIT_VDBE_OP                   5
3425#define SQLITE_LIMIT_FUNCTION_ARG              6
3426#define SQLITE_LIMIT_ATTACHED                  7
3427#define SQLITE_LIMIT_LIKE_PATTERN_LENGTH       8
3428#define SQLITE_LIMIT_VARIABLE_NUMBER           9
3429#define SQLITE_LIMIT_TRIGGER_DEPTH            10
3430#define SQLITE_LIMIT_WORKER_THREADS           11
3431
3432/*
3433** CAPI3REF: Compiling An SQL Statement
3434** KEYWORDS: {SQL statement compiler}
3435** METHOD: sqlite3
3436** CONSTRUCTOR: sqlite3_stmt
3437**
3438** To execute an SQL query, it must first be compiled into a byte-code
3439** program using one of these routines.
3440**
3441** The first argument, "db", is a [database connection] obtained from a
3442** prior successful call to [sqlite3_open()], [sqlite3_open_v2()] or
3443** [sqlite3_open16()].  The database connection must not have been closed.
3444**
3445** The second argument, "zSql", is the statement to be compiled, encoded
3446** as either UTF-8 or UTF-16.  The sqlite3_prepare() and sqlite3_prepare_v2()
3447** interfaces use UTF-8, and sqlite3_prepare16() and sqlite3_prepare16_v2()
3448** use UTF-16.
3449**
3450** ^If the nByte argument is negative, then zSql is read up to the
3451** first zero terminator. ^If nByte is positive, then it is the
3452** number of bytes read from zSql.  ^If nByte is zero, then no prepared
3453** statement is generated.
3454** If the caller knows that the supplied string is nul-terminated, then
3455** there is a small performance advantage to passing an nByte parameter that
3456** is the number of bytes in the input string <i>including</i>
3457** the nul-terminator.
3458**
3459** ^If pzTail is not NULL then *pzTail is made to point to the first byte
3460** past the end of the first SQL statement in zSql.  These routines only
3461** compile the first statement in zSql, so *pzTail is left pointing to
3462** what remains uncompiled.
3463**
3464** ^*ppStmt is left pointing to a compiled [prepared statement] that can be
3465** executed using [sqlite3_step()].  ^If there is an error, *ppStmt is set
3466** to NULL.  ^If the input text contains no SQL (if the input is an empty
3467** string or a comment) then *ppStmt is set to NULL.
3468** The calling procedure is responsible for deleting the compiled
3469** SQL statement using [sqlite3_finalize()] after it has finished with it.
3470** ppStmt may not be NULL.
3471**
3472** ^On success, the sqlite3_prepare() family of routines return [SQLITE_OK];
3473** otherwise an [error code] is returned.
3474**
3475** The sqlite3_prepare_v2() and sqlite3_prepare16_v2() interfaces are
3476** recommended for all new programs. The two older interfaces are retained
3477** for backwards compatibility, but their use is discouraged.
3478** ^In the "v2" interfaces, the prepared statement
3479** that is returned (the [sqlite3_stmt] object) contains a copy of the
3480** original SQL text. This causes the [sqlite3_step()] interface to
3481** behave differently in three ways:
3482**
3483** <ol>
3484** <li>
3485** ^If the database schema changes, instead of returning [SQLITE_SCHEMA] as it
3486** always used to do, [sqlite3_step()] will automatically recompile the SQL
3487** statement and try to run it again. As many as [SQLITE_MAX_SCHEMA_RETRY]
3488** retries will occur before sqlite3_step() gives up and returns an error.
3489** </li>
3490**
3491** <li>
3492** ^When an error occurs, [sqlite3_step()] will return one of the detailed
3493** [error codes] or [extended error codes].  ^The legacy behavior was that
3494** [sqlite3_step()] would only return a generic [SQLITE_ERROR] result code
3495** and the application would have to make a second call to [sqlite3_reset()]
3496** in order to find the underlying cause of the problem. With the "v2" prepare
3497** interfaces, the underlying reason for the error is returned immediately.
3498** </li>
3499**
3500** <li>
3501** ^If the specific value bound to [parameter | host parameter] in the
3502** WHERE clause might influence the choice of query plan for a statement,
3503** then the statement will be automatically recompiled, as if there had been
3504** a schema change, on the first  [sqlite3_step()] call following any change
3505** to the [sqlite3_bind_text | bindings] of that [parameter].
3506** ^The specific value of WHERE-clause [parameter] might influence the
3507** choice of query plan if the parameter is the left-hand side of a [LIKE]
3508** or [GLOB] operator or if the parameter is compared to an indexed column
3509** and the [SQLITE_ENABLE_STAT3] compile-time option is enabled.
3510** </li>
3511** </ol>
3512*/
3513SQLITE_API int SQLITE_STDCALL sqlite3_prepare(
3514  sqlite3 *db,            /* Database handle */
3515  const char *zSql,       /* SQL statement, UTF-8 encoded */
3516  int nByte,              /* Maximum length of zSql in bytes. */
3517  sqlite3_stmt **ppStmt,  /* OUT: Statement handle */
3518  const char **pzTail     /* OUT: Pointer to unused portion of zSql */
3519);
3520SQLITE_API int SQLITE_STDCALL sqlite3_prepare_v2(
3521  sqlite3 *db,            /* Database handle */
3522  const char *zSql,       /* SQL statement, UTF-8 encoded */
3523  int nByte,              /* Maximum length of zSql in bytes. */
3524  sqlite3_stmt **ppStmt,  /* OUT: Statement handle */
3525  const char **pzTail     /* OUT: Pointer to unused portion of zSql */
3526);
3527SQLITE_API int SQLITE_STDCALL sqlite3_prepare16(
3528  sqlite3 *db,            /* Database handle */
3529  const void *zSql,       /* SQL statement, UTF-16 encoded */
3530  int nByte,              /* Maximum length of zSql in bytes. */
3531  sqlite3_stmt **ppStmt,  /* OUT: Statement handle */
3532  const void **pzTail     /* OUT: Pointer to unused portion of zSql */
3533);
3534SQLITE_API int SQLITE_STDCALL sqlite3_prepare16_v2(
3535  sqlite3 *db,            /* Database handle */
3536  const void *zSql,       /* SQL statement, UTF-16 encoded */
3537  int nByte,              /* Maximum length of zSql in bytes. */
3538  sqlite3_stmt **ppStmt,  /* OUT: Statement handle */
3539  const void **pzTail     /* OUT: Pointer to unused portion of zSql */
3540);
3541
3542/*
3543** CAPI3REF: Retrieving Statement SQL
3544** METHOD: sqlite3_stmt
3545**
3546** ^This interface can be used to retrieve a saved copy of the original
3547** SQL text used to create a [prepared statement] if that statement was
3548** compiled using either [sqlite3_prepare_v2()] or [sqlite3_prepare16_v2()].
3549*/
3550SQLITE_API const char *SQLITE_STDCALL sqlite3_sql(sqlite3_stmt *pStmt);
3551
3552/*
3553** CAPI3REF: Determine If An SQL Statement Writes The Database
3554** METHOD: sqlite3_stmt
3555**
3556** ^The sqlite3_stmt_readonly(X) interface returns true (non-zero) if
3557** and only if the [prepared statement] X makes no direct changes to
3558** the content of the database file.
3559**
3560** Note that [application-defined SQL functions] or
3561** [virtual tables] might change the database indirectly as a side effect.
3562** ^(For example, if an application defines a function "eval()" that
3563** calls [sqlite3_exec()], then the following SQL statement would
3564** change the database file through side-effects:
3565**
3566** <blockquote><pre>
3567**    SELECT eval('DELETE FROM t1') FROM t2;
3568** </pre></blockquote>
3569**
3570** But because the [SELECT] statement does not change the database file
3571** directly, sqlite3_stmt_readonly() would still return true.)^
3572**
3573** ^Transaction control statements such as [BEGIN], [COMMIT], [ROLLBACK],
3574** [SAVEPOINT], and [RELEASE] cause sqlite3_stmt_readonly() to return true,
3575** since the statements themselves do not actually modify the database but
3576** rather they control the timing of when other statements modify the
3577** database.  ^The [ATTACH] and [DETACH] statements also cause
3578** sqlite3_stmt_readonly() to return true since, while those statements
3579** change the configuration of a database connection, they do not make
3580** changes to the content of the database files on disk.
3581*/
3582SQLITE_API int SQLITE_STDCALL sqlite3_stmt_readonly(sqlite3_stmt *pStmt);
3583
3584/*
3585** CAPI3REF: Determine If A Prepared Statement Has Been Reset
3586** METHOD: sqlite3_stmt
3587**
3588** ^The sqlite3_stmt_busy(S) interface returns true (non-zero) if the
3589** [prepared statement] S has been stepped at least once using
3590** [sqlite3_step(S)] but has not run to completion and/or has not
3591** been reset using [sqlite3_reset(S)].  ^The sqlite3_stmt_busy(S)
3592** interface returns false if S is a NULL pointer.  If S is not a
3593** NULL pointer and is not a pointer to a valid [prepared statement]
3594** object, then the behavior is undefined and probably undesirable.
3595**
3596** This interface can be used in combination [sqlite3_next_stmt()]
3597** to locate all prepared statements associated with a database
3598** connection that are in need of being reset.  This can be used,
3599** for example, in diagnostic routines to search for prepared
3600** statements that are holding a transaction open.
3601*/
3602SQLITE_API int SQLITE_STDCALL sqlite3_stmt_busy(sqlite3_stmt*);
3603
3604/*
3605** CAPI3REF: Dynamically Typed Value Object
3606** KEYWORDS: {protected sqlite3_value} {unprotected sqlite3_value}
3607**
3608** SQLite uses the sqlite3_value object to represent all values
3609** that can be stored in a database table. SQLite uses dynamic typing
3610** for the values it stores.  ^Values stored in sqlite3_value objects
3611** can be integers, floating point values, strings, BLOBs, or NULL.
3612**
3613** An sqlite3_value object may be either "protected" or "unprotected".
3614** Some interfaces require a protected sqlite3_value.  Other interfaces
3615** will accept either a protected or an unprotected sqlite3_value.
3616** Every interface that accepts sqlite3_value arguments specifies
3617** whether or not it requires a protected sqlite3_value.  The
3618** [sqlite3_value_dup()] interface can be used to construct a new
3619** protected sqlite3_value from an unprotected sqlite3_value.
3620**
3621** The terms "protected" and "unprotected" refer to whether or not
3622** a mutex is held.  An internal mutex is held for a protected
3623** sqlite3_value object but no mutex is held for an unprotected
3624** sqlite3_value object.  If SQLite is compiled to be single-threaded
3625** (with [SQLITE_THREADSAFE=0] and with [sqlite3_threadsafe()] returning 0)
3626** or if SQLite is run in one of reduced mutex modes
3627** [SQLITE_CONFIG_SINGLETHREAD] or [SQLITE_CONFIG_MULTITHREAD]
3628** then there is no distinction between protected and unprotected
3629** sqlite3_value objects and they can be used interchangeably.  However,
3630** for maximum code portability it is recommended that applications
3631** still make the distinction between protected and unprotected
3632** sqlite3_value objects even when not strictly required.
3633**
3634** ^The sqlite3_value objects that are passed as parameters into the
3635** implementation of [application-defined SQL functions] are protected.
3636** ^The sqlite3_value object returned by
3637** [sqlite3_column_value()] is unprotected.
3638** Unprotected sqlite3_value objects may only be used with
3639** [sqlite3_result_value()] and [sqlite3_bind_value()].
3640** The [sqlite3_value_blob | sqlite3_value_type()] family of
3641** interfaces require protected sqlite3_value objects.
3642*/
3643typedef struct Mem sqlite3_value;
3644
3645/*
3646** CAPI3REF: SQL Function Context Object
3647**
3648** The context in which an SQL function executes is stored in an
3649** sqlite3_context object.  ^A pointer to an sqlite3_context object
3650** is always first parameter to [application-defined SQL functions].
3651** The application-defined SQL function implementation will pass this
3652** pointer through into calls to [sqlite3_result_int | sqlite3_result()],
3653** [sqlite3_aggregate_context()], [sqlite3_user_data()],
3654** [sqlite3_context_db_handle()], [sqlite3_get_auxdata()],
3655** and/or [sqlite3_set_auxdata()].
3656*/
3657typedef struct sqlite3_context sqlite3_context;
3658
3659/*
3660** CAPI3REF: Binding Values To Prepared Statements
3661** KEYWORDS: {host parameter} {host parameters} {host parameter name}
3662** KEYWORDS: {SQL parameter} {SQL parameters} {parameter binding}
3663** METHOD: sqlite3_stmt
3664**
3665** ^(In the SQL statement text input to [sqlite3_prepare_v2()] and its variants,
3666** literals may be replaced by a [parameter] that matches one of following
3667** templates:
3668**
3669** <ul>
3670** <li>  ?
3671** <li>  ?NNN
3672** <li>  :VVV
3673** <li>  @VVV
3674** <li>  $VVV
3675** </ul>
3676**
3677** In the templates above, NNN represents an integer literal,
3678** and VVV represents an alphanumeric identifier.)^  ^The values of these
3679** parameters (also called "host parameter names" or "SQL parameters")
3680** can be set using the sqlite3_bind_*() routines defined here.
3681**
3682** ^The first argument to the sqlite3_bind_*() routines is always
3683** a pointer to the [sqlite3_stmt] object returned from
3684** [sqlite3_prepare_v2()] or its variants.
3685**
3686** ^The second argument is the index of the SQL parameter to be set.
3687** ^The leftmost SQL parameter has an index of 1.  ^When the same named
3688** SQL parameter is used more than once, second and subsequent
3689** occurrences have the same index as the first occurrence.
3690** ^The index for named parameters can be looked up using the
3691** [sqlite3_bind_parameter_index()] API if desired.  ^The index
3692** for "?NNN" parameters is the value of NNN.
3693** ^The NNN value must be between 1 and the [sqlite3_limit()]
3694** parameter [SQLITE_LIMIT_VARIABLE_NUMBER] (default value: 999).
3695**
3696** ^The third argument is the value to bind to the parameter.
3697** ^If the third parameter to sqlite3_bind_text() or sqlite3_bind_text16()
3698** or sqlite3_bind_blob() is a NULL pointer then the fourth parameter
3699** is ignored and the end result is the same as sqlite3_bind_null().
3700**
3701** ^(In those routines that have a fourth argument, its value is the
3702** number of bytes in the parameter.  To be clear: the value is the
3703** number of <u>bytes</u> in the value, not the number of characters.)^
3704** ^If the fourth parameter to sqlite3_bind_text() or sqlite3_bind_text16()
3705** is negative, then the length of the string is
3706** the number of bytes up to the first zero terminator.
3707** If the fourth parameter to sqlite3_bind_blob() is negative, then
3708** the behavior is undefined.
3709** If a non-negative fourth parameter is provided to sqlite3_bind_text()
3710** or sqlite3_bind_text16() or sqlite3_bind_text64() then
3711** that parameter must be the byte offset
3712** where the NUL terminator would occur assuming the string were NUL
3713** terminated.  If any NUL characters occur at byte offsets less than
3714** the value of the fourth parameter then the resulting string value will
3715** contain embedded NULs.  The result of expressions involving strings
3716** with embedded NULs is undefined.
3717**
3718** ^The fifth argument to the BLOB and string binding interfaces
3719** is a destructor used to dispose of the BLOB or
3720** string after SQLite has finished with it.  ^The destructor is called
3721** to dispose of the BLOB or string even if the call to bind API fails.
3722** ^If the fifth argument is
3723** the special value [SQLITE_STATIC], then SQLite assumes that the
3724** information is in static, unmanaged space and does not need to be freed.
3725** ^If the fifth argument has the value [SQLITE_TRANSIENT], then
3726** SQLite makes its own private copy of the data immediately, before
3727** the sqlite3_bind_*() routine returns.
3728**
3729** ^The sixth argument to sqlite3_bind_text64() must be one of
3730** [SQLITE_UTF8], [SQLITE_UTF16], [SQLITE_UTF16BE], or [SQLITE_UTF16LE]
3731** to specify the encoding of the text in the third parameter.  If
3732** the sixth argument to sqlite3_bind_text64() is not one of the
3733** allowed values shown above, or if the text encoding is different
3734** from the encoding specified by the sixth parameter, then the behavior
3735** is undefined.
3736**
3737** ^The sqlite3_bind_zeroblob() routine binds a BLOB of length N that
3738** is filled with zeroes.  ^A zeroblob uses a fixed amount of memory
3739** (just an integer to hold its size) while it is being processed.
3740** Zeroblobs are intended to serve as placeholders for BLOBs whose
3741** content is later written using
3742** [sqlite3_blob_open | incremental BLOB I/O] routines.
3743** ^A negative value for the zeroblob results in a zero-length BLOB.
3744**
3745** ^If any of the sqlite3_bind_*() routines are called with a NULL pointer
3746** for the [prepared statement] or with a prepared statement for which
3747** [sqlite3_step()] has been called more recently than [sqlite3_reset()],
3748** then the call will return [SQLITE_MISUSE].  If any sqlite3_bind_()
3749** routine is passed a [prepared statement] that has been finalized, the
3750** result is undefined and probably harmful.
3751**
3752** ^Bindings are not cleared by the [sqlite3_reset()] routine.
3753** ^Unbound parameters are interpreted as NULL.
3754**
3755** ^The sqlite3_bind_* routines return [SQLITE_OK] on success or an
3756** [error code] if anything goes wrong.
3757** ^[SQLITE_TOOBIG] might be returned if the size of a string or BLOB
3758** exceeds limits imposed by [sqlite3_limit]([SQLITE_LIMIT_LENGTH]) or
3759** [SQLITE_MAX_LENGTH].
3760** ^[SQLITE_RANGE] is returned if the parameter
3761** index is out of range.  ^[SQLITE_NOMEM] is returned if malloc() fails.
3762**
3763** See also: [sqlite3_bind_parameter_count()],
3764** [sqlite3_bind_parameter_name()], and [sqlite3_bind_parameter_index()].
3765*/
3766SQLITE_API int SQLITE_STDCALL sqlite3_bind_blob(sqlite3_stmt*, int, const void*, int n, void(*)(void*));
3767SQLITE_API int SQLITE_STDCALL sqlite3_bind_blob64(sqlite3_stmt*, int, const void*, sqlite3_uint64,
3768                        void(*)(void*));
3769SQLITE_API int SQLITE_STDCALL sqlite3_bind_double(sqlite3_stmt*, int, double);
3770SQLITE_API int SQLITE_STDCALL sqlite3_bind_int(sqlite3_stmt*, int, int);
3771SQLITE_API int SQLITE_STDCALL sqlite3_bind_int64(sqlite3_stmt*, int, sqlite3_int64);
3772SQLITE_API int SQLITE_STDCALL sqlite3_bind_null(sqlite3_stmt*, int);
3773SQLITE_API int SQLITE_STDCALL sqlite3_bind_text(sqlite3_stmt*,int,const char*,int,void(*)(void*));
3774SQLITE_API int SQLITE_STDCALL sqlite3_bind_text16(sqlite3_stmt*, int, const void*, int, void(*)(void*));
3775SQLITE_API int SQLITE_STDCALL sqlite3_bind_text64(sqlite3_stmt*, int, const char*, sqlite3_uint64,
3776                         void(*)(void*), unsigned char encoding);
3777SQLITE_API int SQLITE_STDCALL sqlite3_bind_value(sqlite3_stmt*, int, const sqlite3_value*);
3778SQLITE_API int SQLITE_STDCALL sqlite3_bind_zeroblob(sqlite3_stmt*, int, int n);
3779SQLITE_API int SQLITE_STDCALL sqlite3_bind_zeroblob64(sqlite3_stmt*, int, sqlite3_uint64);
3780
3781/*
3782** CAPI3REF: Number Of SQL Parameters
3783** METHOD: sqlite3_stmt
3784**
3785** ^This routine can be used to find the number of [SQL parameters]
3786** in a [prepared statement].  SQL parameters are tokens of the
3787** form "?", "?NNN", ":AAA", "$AAA", or "@AAA" that serve as
3788** placeholders for values that are [sqlite3_bind_blob | bound]
3789** to the parameters at a later time.
3790**
3791** ^(This routine actually returns the index of the largest (rightmost)
3792** parameter. For all forms except ?NNN, this will correspond to the
3793** number of unique parameters.  If parameters of the ?NNN form are used,
3794** there may be gaps in the list.)^
3795**
3796** See also: [sqlite3_bind_blob|sqlite3_bind()],
3797** [sqlite3_bind_parameter_name()], and
3798** [sqlite3_bind_parameter_index()].
3799*/
3800SQLITE_API int SQLITE_STDCALL sqlite3_bind_parameter_count(sqlite3_stmt*);
3801
3802/*
3803** CAPI3REF: Name Of A Host Parameter
3804** METHOD: sqlite3_stmt
3805**
3806** ^The sqlite3_bind_parameter_name(P,N) interface returns
3807** the name of the N-th [SQL parameter] in the [prepared statement] P.
3808** ^(SQL parameters of the form "?NNN" or ":AAA" or "@AAA" or "$AAA"
3809** have a name which is the string "?NNN" or ":AAA" or "@AAA" or "$AAA"
3810** respectively.
3811** In other words, the initial ":" or "$" or "@" or "?"
3812** is included as part of the name.)^
3813** ^Parameters of the form "?" without a following integer have no name
3814** and are referred to as "nameless" or "anonymous parameters".
3815**
3816** ^The first host parameter has an index of 1, not 0.
3817**
3818** ^If the value N is out of range or if the N-th parameter is
3819** nameless, then NULL is returned.  ^The returned string is
3820** always in UTF-8 encoding even if the named parameter was
3821** originally specified as UTF-16 in [sqlite3_prepare16()] or
3822** [sqlite3_prepare16_v2()].
3823**
3824** See also: [sqlite3_bind_blob|sqlite3_bind()],
3825** [sqlite3_bind_parameter_count()], and
3826** [sqlite3_bind_parameter_index()].
3827*/
3828SQLITE_API const char *SQLITE_STDCALL sqlite3_bind_parameter_name(sqlite3_stmt*, int);
3829
3830/*
3831** CAPI3REF: Index Of A Parameter With A Given Name
3832** METHOD: sqlite3_stmt
3833**
3834** ^Return the index of an SQL parameter given its name.  ^The
3835** index value returned is suitable for use as the second
3836** parameter to [sqlite3_bind_blob|sqlite3_bind()].  ^A zero
3837** is returned if no matching parameter is found.  ^The parameter
3838** name must be given in UTF-8 even if the original statement
3839** was prepared from UTF-16 text using [sqlite3_prepare16_v2()].
3840**
3841** See also: [sqlite3_bind_blob|sqlite3_bind()],
3842** [sqlite3_bind_parameter_count()], and
3843** [sqlite3_bind_parameter_index()].
3844*/
3845SQLITE_API int SQLITE_STDCALL sqlite3_bind_parameter_index(sqlite3_stmt*, const char *zName);
3846
3847/*
3848** CAPI3REF: Reset All Bindings On A Prepared Statement
3849** METHOD: sqlite3_stmt
3850**
3851** ^Contrary to the intuition of many, [sqlite3_reset()] does not reset
3852** the [sqlite3_bind_blob | bindings] on a [prepared statement].
3853** ^Use this routine to reset all host parameters to NULL.
3854*/
3855SQLITE_API int SQLITE_STDCALL sqlite3_clear_bindings(sqlite3_stmt*);
3856
3857/*
3858** CAPI3REF: Number Of Columns In A Result Set
3859** METHOD: sqlite3_stmt
3860**
3861** ^Return the number of columns in the result set returned by the
3862** [prepared statement]. ^This routine returns 0 if pStmt is an SQL
3863** statement that does not return data (for example an [UPDATE]).
3864**
3865** See also: [sqlite3_data_count()]
3866*/
3867SQLITE_API int SQLITE_STDCALL sqlite3_column_count(sqlite3_stmt *pStmt);
3868
3869/*
3870** CAPI3REF: Column Names In A Result Set
3871** METHOD: sqlite3_stmt
3872**
3873** ^These routines return the name assigned to a particular column
3874** in the result set of a [SELECT] statement.  ^The sqlite3_column_name()
3875** interface returns a pointer to a zero-terminated UTF-8 string
3876** and sqlite3_column_name16() returns a pointer to a zero-terminated
3877** UTF-16 string.  ^The first parameter is the [prepared statement]
3878** that implements the [SELECT] statement. ^The second parameter is the
3879** column number.  ^The leftmost column is number 0.
3880**
3881** ^The returned string pointer is valid until either the [prepared statement]
3882** is destroyed by [sqlite3_finalize()] or until the statement is automatically
3883** reprepared by the first call to [sqlite3_step()] for a particular run
3884** or until the next call to
3885** sqlite3_column_name() or sqlite3_column_name16() on the same column.
3886**
3887** ^If sqlite3_malloc() fails during the processing of either routine
3888** (for example during a conversion from UTF-8 to UTF-16) then a
3889** NULL pointer is returned.
3890**
3891** ^The name of a result column is the value of the "AS" clause for
3892** that column, if there is an AS clause.  If there is no AS clause
3893** then the name of the column is unspecified and may change from
3894** one release of SQLite to the next.
3895*/
3896SQLITE_API const char *SQLITE_STDCALL sqlite3_column_name(sqlite3_stmt*, int N);
3897SQLITE_API const void *SQLITE_STDCALL sqlite3_column_name16(sqlite3_stmt*, int N);
3898
3899/*
3900** CAPI3REF: Source Of Data In A Query Result
3901** METHOD: sqlite3_stmt
3902**
3903** ^These routines provide a means to determine the database, table, and
3904** table column that is the origin of a particular result column in
3905** [SELECT] statement.
3906** ^The name of the database or table or column can be returned as
3907** either a UTF-8 or UTF-16 string.  ^The _database_ routines return
3908** the database name, the _table_ routines return the table name, and
3909** the origin_ routines return the column name.
3910** ^The returned string is valid until the [prepared statement] is destroyed
3911** using [sqlite3_finalize()] or until the statement is automatically
3912** reprepared by the first call to [sqlite3_step()] for a particular run
3913** or until the same information is requested
3914** again in a different encoding.
3915**
3916** ^The names returned are the original un-aliased names of the
3917** database, table, and column.
3918**
3919** ^The first argument to these interfaces is a [prepared statement].
3920** ^These functions return information about the Nth result column returned by
3921** the statement, where N is the second function argument.
3922** ^The left-most column is column 0 for these routines.
3923**
3924** ^If the Nth column returned by the statement is an expression or
3925** subquery and is not a column value, then all of these functions return
3926** NULL.  ^These routine might also return NULL if a memory allocation error
3927** occurs.  ^Otherwise, they return the name of the attached database, table,
3928** or column that query result column was extracted from.
3929**
3930** ^As with all other SQLite APIs, those whose names end with "16" return
3931** UTF-16 encoded strings and the other functions return UTF-8.
3932**
3933** ^These APIs are only available if the library was compiled with the
3934** [SQLITE_ENABLE_COLUMN_METADATA] C-preprocessor symbol.
3935**
3936** If two or more threads call one or more of these routines against the same
3937** prepared statement and column at the same time then the results are
3938** undefined.
3939**
3940** If two or more threads call one or more
3941** [sqlite3_column_database_name | column metadata interfaces]
3942** for the same [prepared statement] and result column
3943** at the same time then the results are undefined.
3944*/
3945SQLITE_API const char *SQLITE_STDCALL sqlite3_column_database_name(sqlite3_stmt*,int);
3946SQLITE_API const void *SQLITE_STDCALL sqlite3_column_database_name16(sqlite3_stmt*,int);
3947SQLITE_API const char *SQLITE_STDCALL sqlite3_column_table_name(sqlite3_stmt*,int);
3948SQLITE_API const void *SQLITE_STDCALL sqlite3_column_table_name16(sqlite3_stmt*,int);
3949SQLITE_API const char *SQLITE_STDCALL sqlite3_column_origin_name(sqlite3_stmt*,int);
3950SQLITE_API const void *SQLITE_STDCALL sqlite3_column_origin_name16(sqlite3_stmt*,int);
3951
3952/*
3953** CAPI3REF: Declared Datatype Of A Query Result
3954** METHOD: sqlite3_stmt
3955**
3956** ^(The first parameter is a [prepared statement].
3957** If this statement is a [SELECT] statement and the Nth column of the
3958** returned result set of that [SELECT] is a table column (not an
3959** expression or subquery) then the declared type of the table
3960** column is returned.)^  ^If the Nth column of the result set is an
3961** expression or subquery, then a NULL pointer is returned.
3962** ^The returned string is always UTF-8 encoded.
3963**
3964** ^(For example, given the database schema:
3965**
3966** CREATE TABLE t1(c1 VARIANT);
3967**
3968** and the following statement to be compiled:
3969**
3970** SELECT c1 + 1, c1 FROM t1;
3971**
3972** this routine would return the string "VARIANT" for the second result
3973** column (i==1), and a NULL pointer for the first result column (i==0).)^
3974**
3975** ^SQLite uses dynamic run-time typing.  ^So just because a column
3976** is declared to contain a particular type does not mean that the
3977** data stored in that column is of the declared type.  SQLite is
3978** strongly typed, but the typing is dynamic not static.  ^Type
3979** is associated with individual values, not with the containers
3980** used to hold those values.
3981*/
3982SQLITE_API const char *SQLITE_STDCALL sqlite3_column_decltype(sqlite3_stmt*,int);
3983SQLITE_API const void *SQLITE_STDCALL sqlite3_column_decltype16(sqlite3_stmt*,int);
3984
3985/*
3986** CAPI3REF: Evaluate An SQL Statement
3987** METHOD: sqlite3_stmt
3988**
3989** After a [prepared statement] has been prepared using either
3990** [sqlite3_prepare_v2()] or [sqlite3_prepare16_v2()] or one of the legacy
3991** interfaces [sqlite3_prepare()] or [sqlite3_prepare16()], this function
3992** must be called one or more times to evaluate the statement.
3993**
3994** The details of the behavior of the sqlite3_step() interface depend
3995** on whether the statement was prepared using the newer "v2" interface
3996** [sqlite3_prepare_v2()] and [sqlite3_prepare16_v2()] or the older legacy
3997** interface [sqlite3_prepare()] and [sqlite3_prepare16()].  The use of the
3998** new "v2" interface is recommended for new applications but the legacy
3999** interface will continue to be supported.
4000**
4001** ^In the legacy interface, the return value will be either [SQLITE_BUSY],
4002** [SQLITE_DONE], [SQLITE_ROW], [SQLITE_ERROR], or [SQLITE_MISUSE].
4003** ^With the "v2" interface, any of the other [result codes] or
4004** [extended result codes] might be returned as well.
4005**
4006** ^[SQLITE_BUSY] means that the database engine was unable to acquire the
4007** database locks it needs to do its job.  ^If the statement is a [COMMIT]
4008** or occurs outside of an explicit transaction, then you can retry the
4009** statement.  If the statement is not a [COMMIT] and occurs within an
4010** explicit transaction then you should rollback the transaction before
4011** continuing.
4012**
4013** ^[SQLITE_DONE] means that the statement has finished executing
4014** successfully.  sqlite3_step() should not be called again on this virtual
4015** machine without first calling [sqlite3_reset()] to reset the virtual
4016** machine back to its initial state.
4017**
4018** ^If the SQL statement being executed returns any data, then [SQLITE_ROW]
4019** is returned each time a new row of data is ready for processing by the
4020** caller. The values may be accessed using the [column access functions].
4021** sqlite3_step() is called again to retrieve the next row of data.
4022**
4023** ^[SQLITE_ERROR] means that a run-time error (such as a constraint
4024** violation) has occurred.  sqlite3_step() should not be called again on
4025** the VM. More information may be found by calling [sqlite3_errmsg()].
4026** ^With the legacy interface, a more specific error code (for example,
4027** [SQLITE_INTERRUPT], [SQLITE_SCHEMA], [SQLITE_CORRUPT], and so forth)
4028** can be obtained by calling [sqlite3_reset()] on the
4029** [prepared statement].  ^In the "v2" interface,
4030** the more specific error code is returned directly by sqlite3_step().
4031**
4032** [SQLITE_MISUSE] means that the this routine was called inappropriately.
4033** Perhaps it was called on a [prepared statement] that has
4034** already been [sqlite3_finalize | finalized] or on one that had
4035** previously returned [SQLITE_ERROR] or [SQLITE_DONE].  Or it could
4036** be the case that the same database connection is being used by two or
4037** more threads at the same moment in time.
4038**
4039** For all versions of SQLite up to and including 3.6.23.1, a call to
4040** [sqlite3_reset()] was required after sqlite3_step() returned anything
4041** other than [SQLITE_ROW] before any subsequent invocation of
4042** sqlite3_step().  Failure to reset the prepared statement using
4043** [sqlite3_reset()] would result in an [SQLITE_MISUSE] return from
4044** sqlite3_step().  But after version 3.6.23.1, sqlite3_step() began
4045** calling [sqlite3_reset()] automatically in this circumstance rather
4046** than returning [SQLITE_MISUSE].  This is not considered a compatibility
4047** break because any application that ever receives an SQLITE_MISUSE error
4048** is broken by definition.  The [SQLITE_OMIT_AUTORESET] compile-time option
4049** can be used to restore the legacy behavior.
4050**
4051** <b>Goofy Interface Alert:</b> In the legacy interface, the sqlite3_step()
4052** API always returns a generic error code, [SQLITE_ERROR], following any
4053** error other than [SQLITE_BUSY] and [SQLITE_MISUSE].  You must call
4054** [sqlite3_reset()] or [sqlite3_finalize()] in order to find one of the
4055** specific [error codes] that better describes the error.
4056** We admit that this is a goofy design.  The problem has been fixed
4057** with the "v2" interface.  If you prepare all of your SQL statements
4058** using either [sqlite3_prepare_v2()] or [sqlite3_prepare16_v2()] instead
4059** of the legacy [sqlite3_prepare()] and [sqlite3_prepare16()] interfaces,
4060** then the more specific [error codes] are returned directly
4061** by sqlite3_step().  The use of the "v2" interface is recommended.
4062*/
4063SQLITE_API int SQLITE_STDCALL sqlite3_step(sqlite3_stmt*);
4064
4065/*
4066** CAPI3REF: Number of columns in a result set
4067** METHOD: sqlite3_stmt
4068**
4069** ^The sqlite3_data_count(P) interface returns the number of columns in the
4070** current row of the result set of [prepared statement] P.
4071** ^If prepared statement P does not have results ready to return
4072** (via calls to the [sqlite3_column_int | sqlite3_column_*()] of
4073** interfaces) then sqlite3_data_count(P) returns 0.
4074** ^The sqlite3_data_count(P) routine also returns 0 if P is a NULL pointer.
4075** ^The sqlite3_data_count(P) routine returns 0 if the previous call to
4076** [sqlite3_step](P) returned [SQLITE_DONE].  ^The sqlite3_data_count(P)
4077** will return non-zero if previous call to [sqlite3_step](P) returned
4078** [SQLITE_ROW], except in the case of the [PRAGMA incremental_vacuum]
4079** where it always returns zero since each step of that multi-step
4080** pragma returns 0 columns of data.
4081**
4082** See also: [sqlite3_column_count()]
4083*/
4084SQLITE_API int SQLITE_STDCALL sqlite3_data_count(sqlite3_stmt *pStmt);
4085
4086/*
4087** CAPI3REF: Fundamental Datatypes
4088** KEYWORDS: SQLITE_TEXT
4089**
4090** ^(Every value in SQLite has one of five fundamental datatypes:
4091**
4092** <ul>
4093** <li> 64-bit signed integer
4094** <li> 64-bit IEEE floating point number
4095** <li> string
4096** <li> BLOB
4097** <li> NULL
4098** </ul>)^
4099**
4100** These constants are codes for each of those types.
4101**
4102** Note that the SQLITE_TEXT constant was also used in SQLite version 2
4103** for a completely different meaning.  Software that links against both
4104** SQLite version 2 and SQLite version 3 should use SQLITE3_TEXT, not
4105** SQLITE_TEXT.
4106*/
4107#define SQLITE_INTEGER  1
4108#define SQLITE_FLOAT    2
4109#define SQLITE_BLOB     4
4110#define SQLITE_NULL     5
4111#ifdef SQLITE_TEXT
4112# undef SQLITE_TEXT
4113#else
4114# define SQLITE_TEXT     3
4115#endif
4116#define SQLITE3_TEXT     3
4117
4118/*
4119** CAPI3REF: Result Values From A Query
4120** KEYWORDS: {column access functions}
4121** METHOD: sqlite3_stmt
4122**
4123** ^These routines return information about a single column of the current
4124** result row of a query.  ^In every case the first argument is a pointer
4125** to the [prepared statement] that is being evaluated (the [sqlite3_stmt*]
4126** that was returned from [sqlite3_prepare_v2()] or one of its variants)
4127** and the second argument is the index of the column for which information
4128** should be returned. ^The leftmost column of the result set has the index 0.
4129** ^The number of columns in the result can be determined using
4130** [sqlite3_column_count()].
4131**
4132** If the SQL statement does not currently point to a valid row, or if the
4133** column index is out of range, the result is undefined.
4134** These routines may only be called when the most recent call to
4135** [sqlite3_step()] has returned [SQLITE_ROW] and neither
4136** [sqlite3_reset()] nor [sqlite3_finalize()] have been called subsequently.
4137** If any of these routines are called after [sqlite3_reset()] or
4138** [sqlite3_finalize()] or after [sqlite3_step()] has returned
4139** something other than [SQLITE_ROW], the results are undefined.
4140** If [sqlite3_step()] or [sqlite3_reset()] or [sqlite3_finalize()]
4141** are called from a different thread while any of these routines
4142** are pending, then the results are undefined.
4143**
4144** ^The sqlite3_column_type() routine returns the
4145** [SQLITE_INTEGER | datatype code] for the initial data type
4146** of the result column.  ^The returned value is one of [SQLITE_INTEGER],
4147** [SQLITE_FLOAT], [SQLITE_TEXT], [SQLITE_BLOB], or [SQLITE_NULL].  The value
4148** returned by sqlite3_column_type() is only meaningful if no type
4149** conversions have occurred as described below.  After a type conversion,
4150** the value returned by sqlite3_column_type() is undefined.  Future
4151** versions of SQLite may change the behavior of sqlite3_column_type()
4152** following a type conversion.
4153**
4154** ^If the result is a BLOB or UTF-8 string then the sqlite3_column_bytes()
4155** routine returns the number of bytes in that BLOB or string.
4156** ^If the result is a UTF-16 string, then sqlite3_column_bytes() converts
4157** the string to UTF-8 and then returns the number of bytes.
4158** ^If the result is a numeric value then sqlite3_column_bytes() uses
4159** [sqlite3_snprintf()] to convert that value to a UTF-8 string and returns
4160** the number of bytes in that string.
4161** ^If the result is NULL, then sqlite3_column_bytes() returns zero.
4162**
4163** ^If the result is a BLOB or UTF-16 string then the sqlite3_column_bytes16()
4164** routine returns the number of bytes in that BLOB or string.
4165** ^If the result is a UTF-8 string, then sqlite3_column_bytes16() converts
4166** the string to UTF-16 and then returns the number of bytes.
4167** ^If the result is a numeric value then sqlite3_column_bytes16() uses
4168** [sqlite3_snprintf()] to convert that value to a UTF-16 string and returns
4169** the number of bytes in that string.
4170** ^If the result is NULL, then sqlite3_column_bytes16() returns zero.
4171**
4172** ^The values returned by [sqlite3_column_bytes()] and
4173** [sqlite3_column_bytes16()] do not include the zero terminators at the end
4174** of the string.  ^For clarity: the values returned by
4175** [sqlite3_column_bytes()] and [sqlite3_column_bytes16()] are the number of
4176** bytes in the string, not the number of characters.
4177**
4178** ^Strings returned by sqlite3_column_text() and sqlite3_column_text16(),
4179** even empty strings, are always zero-terminated.  ^The return
4180** value from sqlite3_column_blob() for a zero-length BLOB is a NULL pointer.
4181**
4182** <b>Warning:</b> ^The object returned by [sqlite3_column_value()] is an
4183** [unprotected sqlite3_value] object.  In a multithreaded environment,
4184** an unprotected sqlite3_value object may only be used safely with
4185** [sqlite3_bind_value()] and [sqlite3_result_value()].
4186** If the [unprotected sqlite3_value] object returned by
4187** [sqlite3_column_value()] is used in any other way, including calls
4188** to routines like [sqlite3_value_int()], [sqlite3_value_text()],
4189** or [sqlite3_value_bytes()], the behavior is not threadsafe.
4190**
4191** These routines attempt to convert the value where appropriate.  ^For
4192** example, if the internal representation is FLOAT and a text result
4193** is requested, [sqlite3_snprintf()] is used internally to perform the
4194** conversion automatically.  ^(The following table details the conversions
4195** that are applied:
4196**
4197** <blockquote>
4198** <table border="1">
4199** <tr><th> Internal<br>Type <th> Requested<br>Type <th>  Conversion
4200**
4201** <tr><td>  NULL    <td> INTEGER   <td> Result is 0
4202** <tr><td>  NULL    <td>  FLOAT    <td> Result is 0.0
4203** <tr><td>  NULL    <td>   TEXT    <td> Result is a NULL pointer
4204** <tr><td>  NULL    <td>   BLOB    <td> Result is a NULL pointer
4205** <tr><td> INTEGER  <td>  FLOAT    <td> Convert from integer to float
4206** <tr><td> INTEGER  <td>   TEXT    <td> ASCII rendering of the integer
4207** <tr><td> INTEGER  <td>   BLOB    <td> Same as INTEGER->TEXT
4208** <tr><td>  FLOAT   <td> INTEGER   <td> [CAST] to INTEGER
4209** <tr><td>  FLOAT   <td>   TEXT    <td> ASCII rendering of the float
4210** <tr><td>  FLOAT   <td>   BLOB    <td> [CAST] to BLOB
4211** <tr><td>  TEXT    <td> INTEGER   <td> [CAST] to INTEGER
4212** <tr><td>  TEXT    <td>  FLOAT    <td> [CAST] to REAL
4213** <tr><td>  TEXT    <td>   BLOB    <td> No change
4214** <tr><td>  BLOB    <td> INTEGER   <td> [CAST] to INTEGER
4215** <tr><td>  BLOB    <td>  FLOAT    <td> [CAST] to REAL
4216** <tr><td>  BLOB    <td>   TEXT    <td> Add a zero terminator if needed
4217** </table>
4218** </blockquote>)^
4219**
4220** Note that when type conversions occur, pointers returned by prior
4221** calls to sqlite3_column_blob(), sqlite3_column_text(), and/or
4222** sqlite3_column_text16() may be invalidated.
4223** Type conversions and pointer invalidations might occur
4224** in the following cases:
4225**
4226** <ul>
4227** <li> The initial content is a BLOB and sqlite3_column_text() or
4228**      sqlite3_column_text16() is called.  A zero-terminator might
4229**      need to be added to the string.</li>
4230** <li> The initial content is UTF-8 text and sqlite3_column_bytes16() or
4231**      sqlite3_column_text16() is called.  The content must be converted
4232**      to UTF-16.</li>
4233** <li> The initial content is UTF-16 text and sqlite3_column_bytes() or
4234**      sqlite3_column_text() is called.  The content must be converted
4235**      to UTF-8.</li>
4236** </ul>
4237**
4238** ^Conversions between UTF-16be and UTF-16le are always done in place and do
4239** not invalidate a prior pointer, though of course the content of the buffer
4240** that the prior pointer references will have been modified.  Other kinds
4241** of conversion are done in place when it is possible, but sometimes they
4242** are not possible and in those cases prior pointers are invalidated.
4243**
4244** The safest policy is to invoke these routines
4245** in one of the following ways:
4246**
4247** <ul>
4248**  <li>sqlite3_column_text() followed by sqlite3_column_bytes()</li>
4249**  <li>sqlite3_column_blob() followed by sqlite3_column_bytes()</li>
4250**  <li>sqlite3_column_text16() followed by sqlite3_column_bytes16()</li>
4251** </ul>
4252**
4253** In other words, you should call sqlite3_column_text(),
4254** sqlite3_column_blob(), or sqlite3_column_text16() first to force the result
4255** into the desired format, then invoke sqlite3_column_bytes() or
4256** sqlite3_column_bytes16() to find the size of the result.  Do not mix calls
4257** to sqlite3_column_text() or sqlite3_column_blob() with calls to
4258** sqlite3_column_bytes16(), and do not mix calls to sqlite3_column_text16()
4259** with calls to sqlite3_column_bytes().
4260**
4261** ^The pointers returned are valid until a type conversion occurs as
4262** described above, or until [sqlite3_step()] or [sqlite3_reset()] or
4263** [sqlite3_finalize()] is called.  ^The memory space used to hold strings
4264** and BLOBs is freed automatically.  Do <em>not</em> pass the pointers returned
4265** from [sqlite3_column_blob()], [sqlite3_column_text()], etc. into
4266** [sqlite3_free()].
4267**
4268** ^(If a memory allocation error occurs during the evaluation of any
4269** of these routines, a default value is returned.  The default value
4270** is either the integer 0, the floating point number 0.0, or a NULL
4271** pointer.  Subsequent calls to [sqlite3_errcode()] will return
4272** [SQLITE_NOMEM].)^
4273*/
4274SQLITE_API const void *SQLITE_STDCALL sqlite3_column_blob(sqlite3_stmt*, int iCol);
4275SQLITE_API int SQLITE_STDCALL sqlite3_column_bytes(sqlite3_stmt*, int iCol);
4276SQLITE_API int SQLITE_STDCALL sqlite3_column_bytes16(sqlite3_stmt*, int iCol);
4277SQLITE_API double SQLITE_STDCALL sqlite3_column_double(sqlite3_stmt*, int iCol);
4278SQLITE_API int SQLITE_STDCALL sqlite3_column_int(sqlite3_stmt*, int iCol);
4279SQLITE_API sqlite3_int64 SQLITE_STDCALL sqlite3_column_int64(sqlite3_stmt*, int iCol);
4280SQLITE_API const unsigned char *SQLITE_STDCALL sqlite3_column_text(sqlite3_stmt*, int iCol);
4281SQLITE_API const void *SQLITE_STDCALL sqlite3_column_text16(sqlite3_stmt*, int iCol);
4282SQLITE_API int SQLITE_STDCALL sqlite3_column_type(sqlite3_stmt*, int iCol);
4283SQLITE_API sqlite3_value *SQLITE_STDCALL sqlite3_column_value(sqlite3_stmt*, int iCol);
4284
4285/*
4286** CAPI3REF: Destroy A Prepared Statement Object
4287** DESTRUCTOR: sqlite3_stmt
4288**
4289** ^The sqlite3_finalize() function is called to delete a [prepared statement].
4290** ^If the most recent evaluation of the statement encountered no errors
4291** or if the statement is never been evaluated, then sqlite3_finalize() returns
4292** SQLITE_OK.  ^If the most recent evaluation of statement S failed, then
4293** sqlite3_finalize(S) returns the appropriate [error code] or
4294** [extended error code].
4295**
4296** ^The sqlite3_finalize(S) routine can be called at any point during
4297** the life cycle of [prepared statement] S:
4298** before statement S is ever evaluated, after
4299** one or more calls to [sqlite3_reset()], or after any call
4300** to [sqlite3_step()] regardless of whether or not the statement has
4301** completed execution.
4302**
4303** ^Invoking sqlite3_finalize() on a NULL pointer is a harmless no-op.
4304**
4305** The application must finalize every [prepared statement] in order to avoid
4306** resource leaks.  It is a grievous error for the application to try to use
4307** a prepared statement after it has been finalized.  Any use of a prepared
4308** statement after it has been finalized can result in undefined and
4309** undesirable behavior such as segfaults and heap corruption.
4310*/
4311SQLITE_API int SQLITE_STDCALL sqlite3_finalize(sqlite3_stmt *pStmt);
4312
4313/*
4314** CAPI3REF: Reset A Prepared Statement Object
4315** METHOD: sqlite3_stmt
4316**
4317** The sqlite3_reset() function is called to reset a [prepared statement]
4318** object back to its initial state, ready to be re-executed.
4319** ^Any SQL statement variables that had values bound to them using
4320** the [sqlite3_bind_blob | sqlite3_bind_*() API] retain their values.
4321** Use [sqlite3_clear_bindings()] to reset the bindings.
4322**
4323** ^The [sqlite3_reset(S)] interface resets the [prepared statement] S
4324** back to the beginning of its program.
4325**
4326** ^If the most recent call to [sqlite3_step(S)] for the
4327** [prepared statement] S returned [SQLITE_ROW] or [SQLITE_DONE],
4328** or if [sqlite3_step(S)] has never before been called on S,
4329** then [sqlite3_reset(S)] returns [SQLITE_OK].
4330**
4331** ^If the most recent call to [sqlite3_step(S)] for the
4332** [prepared statement] S indicated an error, then
4333** [sqlite3_reset(S)] returns an appropriate [error code].
4334**
4335** ^The [sqlite3_reset(S)] interface does not change the values
4336** of any [sqlite3_bind_blob|bindings] on the [prepared statement] S.
4337*/
4338SQLITE_API int SQLITE_STDCALL sqlite3_reset(sqlite3_stmt *pStmt);
4339
4340/*
4341** CAPI3REF: Create Or Redefine SQL Functions
4342** KEYWORDS: {function creation routines}
4343** KEYWORDS: {application-defined SQL function}
4344** KEYWORDS: {application-defined SQL functions}
4345** METHOD: sqlite3
4346**
4347** ^These functions (collectively known as "function creation routines")
4348** are used to add SQL functions or aggregates or to redefine the behavior
4349** of existing SQL functions or aggregates.  The only differences between
4350** these routines are the text encoding expected for
4351** the second parameter (the name of the function being created)
4352** and the presence or absence of a destructor callback for
4353** the application data pointer.
4354**
4355** ^The first parameter is the [database connection] to which the SQL
4356** function is to be added.  ^If an application uses more than one database
4357** connection then application-defined SQL functions must be added
4358** to each database connection separately.
4359**
4360** ^The second parameter is the name of the SQL function to be created or
4361** redefined.  ^The length of the name is limited to 255 bytes in a UTF-8
4362** representation, exclusive of the zero-terminator.  ^Note that the name
4363** length limit is in UTF-8 bytes, not characters nor UTF-16 bytes.
4364** ^Any attempt to create a function with a longer name
4365** will result in [SQLITE_MISUSE] being returned.
4366**
4367** ^The third parameter (nArg)
4368** is the number of arguments that the SQL function or
4369** aggregate takes. ^If this parameter is -1, then the SQL function or
4370** aggregate may take any number of arguments between 0 and the limit
4371** set by [sqlite3_limit]([SQLITE_LIMIT_FUNCTION_ARG]).  If the third
4372** parameter is less than -1 or greater than 127 then the behavior is
4373** undefined.
4374**
4375** ^The fourth parameter, eTextRep, specifies what
4376** [SQLITE_UTF8 | text encoding] this SQL function prefers for
4377** its parameters.  The application should set this parameter to
4378** [SQLITE_UTF16LE] if the function implementation invokes
4379** [sqlite3_value_text16le()] on an input, or [SQLITE_UTF16BE] if the
4380** implementation invokes [sqlite3_value_text16be()] on an input, or
4381** [SQLITE_UTF16] if [sqlite3_value_text16()] is used, or [SQLITE_UTF8]
4382** otherwise.  ^The same SQL function may be registered multiple times using
4383** different preferred text encodings, with different implementations for
4384** each encoding.
4385** ^When multiple implementations of the same function are available, SQLite
4386** will pick the one that involves the least amount of data conversion.
4387**
4388** ^The fourth parameter may optionally be ORed with [SQLITE_DETERMINISTIC]
4389** to signal that the function will always return the same result given
4390** the same inputs within a single SQL statement.  Most SQL functions are
4391** deterministic.  The built-in [random()] SQL function is an example of a
4392** function that is not deterministic.  The SQLite query planner is able to
4393** perform additional optimizations on deterministic functions, so use
4394** of the [SQLITE_DETERMINISTIC] flag is recommended where possible.
4395**
4396** ^(The fifth parameter is an arbitrary pointer.  The implementation of the
4397** function can gain access to this pointer using [sqlite3_user_data()].)^
4398**
4399** ^The sixth, seventh and eighth parameters, xFunc, xStep and xFinal, are
4400** pointers to C-language functions that implement the SQL function or
4401** aggregate. ^A scalar SQL function requires an implementation of the xFunc
4402** callback only; NULL pointers must be passed as the xStep and xFinal
4403** parameters. ^An aggregate SQL function requires an implementation of xStep
4404** and xFinal and NULL pointer must be passed for xFunc. ^To delete an existing
4405** SQL function or aggregate, pass NULL pointers for all three function
4406** callbacks.
4407**
4408** ^(If the ninth parameter to sqlite3_create_function_v2() is not NULL,
4409** then it is destructor for the application data pointer.
4410** The destructor is invoked when the function is deleted, either by being
4411** overloaded or when the database connection closes.)^
4412** ^The destructor is also invoked if the call to
4413** sqlite3_create_function_v2() fails.
4414** ^When the destructor callback of the tenth parameter is invoked, it
4415** is passed a single argument which is a copy of the application data
4416** pointer which was the fifth parameter to sqlite3_create_function_v2().
4417**
4418** ^It is permitted to register multiple implementations of the same
4419** functions with the same name but with either differing numbers of
4420** arguments or differing preferred text encodings.  ^SQLite will use
4421** the implementation that most closely matches the way in which the
4422** SQL function is used.  ^A function implementation with a non-negative
4423** nArg parameter is a better match than a function implementation with
4424** a negative nArg.  ^A function where the preferred text encoding
4425** matches the database encoding is a better
4426** match than a function where the encoding is different.
4427** ^A function where the encoding difference is between UTF16le and UTF16be
4428** is a closer match than a function where the encoding difference is
4429** between UTF8 and UTF16.
4430**
4431** ^Built-in functions may be overloaded by new application-defined functions.
4432**
4433** ^An application-defined function is permitted to call other
4434** SQLite interfaces.  However, such calls must not
4435** close the database connection nor finalize or reset the prepared
4436** statement in which the function is running.
4437*/
4438SQLITE_API int SQLITE_STDCALL sqlite3_create_function(
4439  sqlite3 *db,
4440  const char *zFunctionName,
4441  int nArg,
4442  int eTextRep,
4443  void *pApp,
4444  void (*xFunc)(sqlite3_context*,int,sqlite3_value**),
4445  void (*xStep)(sqlite3_context*,int,sqlite3_value**),
4446  void (*xFinal)(sqlite3_context*)
4447);
4448SQLITE_API int SQLITE_STDCALL sqlite3_create_function16(
4449  sqlite3 *db,
4450  const void *zFunctionName,
4451  int nArg,
4452  int eTextRep,
4453  void *pApp,
4454  void (*xFunc)(sqlite3_context*,int,sqlite3_value**),
4455  void (*xStep)(sqlite3_context*,int,sqlite3_value**),
4456  void (*xFinal)(sqlite3_context*)
4457);
4458SQLITE_API int SQLITE_STDCALL sqlite3_create_function_v2(
4459  sqlite3 *db,
4460  const char *zFunctionName,
4461  int nArg,
4462  int eTextRep,
4463  void *pApp,
4464  void (*xFunc)(sqlite3_context*,int,sqlite3_value**),
4465  void (*xStep)(sqlite3_context*,int,sqlite3_value**),
4466  void (*xFinal)(sqlite3_context*),
4467  void(*xDestroy)(void*)
4468);
4469
4470/*
4471** CAPI3REF: Text Encodings
4472**
4473** These constant define integer codes that represent the various
4474** text encodings supported by SQLite.
4475*/
4476#define SQLITE_UTF8           1    /* IMP: R-37514-35566 */
4477#define SQLITE_UTF16LE        2    /* IMP: R-03371-37637 */
4478#define SQLITE_UTF16BE        3    /* IMP: R-51971-34154 */
4479#define SQLITE_UTF16          4    /* Use native byte order */
4480#define SQLITE_ANY            5    /* Deprecated */
4481#define SQLITE_UTF16_ALIGNED  8    /* sqlite3_create_collation only */
4482
4483/*
4484** CAPI3REF: Function Flags
4485**
4486** These constants may be ORed together with the
4487** [SQLITE_UTF8 | preferred text encoding] as the fourth argument
4488** to [sqlite3_create_function()], [sqlite3_create_function16()], or
4489** [sqlite3_create_function_v2()].
4490*/
4491#define SQLITE_DETERMINISTIC    0x800
4492
4493/*
4494** CAPI3REF: Deprecated Functions
4495** DEPRECATED
4496**
4497** These functions are [deprecated].  In order to maintain
4498** backwards compatibility with older code, these functions continue
4499** to be supported.  However, new applications should avoid
4500** the use of these functions.  To encourage programmers to avoid
4501** these functions, we will not explain what they do.
4502*/
4503#ifndef SQLITE_OMIT_DEPRECATED
4504SQLITE_API SQLITE_DEPRECATED int SQLITE_STDCALL sqlite3_aggregate_count(sqlite3_context*);
4505SQLITE_API SQLITE_DEPRECATED int SQLITE_STDCALL sqlite3_expired(sqlite3_stmt*);
4506SQLITE_API SQLITE_DEPRECATED int SQLITE_STDCALL sqlite3_transfer_bindings(sqlite3_stmt*, sqlite3_stmt*);
4507SQLITE_API SQLITE_DEPRECATED int SQLITE_STDCALL sqlite3_global_recover(void);
4508SQLITE_API SQLITE_DEPRECATED void SQLITE_STDCALL sqlite3_thread_cleanup(void);
4509SQLITE_API SQLITE_DEPRECATED int SQLITE_STDCALL sqlite3_memory_alarm(void(*)(void*,sqlite3_int64,int),
4510                      void*,sqlite3_int64);
4511#endif
4512
4513/*
4514** CAPI3REF: Obtaining SQL Values
4515** METHOD: sqlite3_value
4516**
4517** The C-language implementation of SQL functions and aggregates uses
4518** this set of interface routines to access the parameter values on
4519** the function or aggregate.
4520**
4521** The xFunc (for scalar functions) or xStep (for aggregates) parameters
4522** to [sqlite3_create_function()] and [sqlite3_create_function16()]
4523** define callbacks that implement the SQL functions and aggregates.
4524** The 3rd parameter to these callbacks is an array of pointers to
4525** [protected sqlite3_value] objects.  There is one [sqlite3_value] object for
4526** each parameter to the SQL function.  These routines are used to
4527** extract values from the [sqlite3_value] objects.
4528**
4529** These routines work only with [protected sqlite3_value] objects.
4530** Any attempt to use these routines on an [unprotected sqlite3_value]
4531** object results in undefined behavior.
4532**
4533** ^These routines work just like the corresponding [column access functions]
4534** except that these routines take a single [protected sqlite3_value] object
4535** pointer instead of a [sqlite3_stmt*] pointer and an integer column number.
4536**
4537** ^The sqlite3_value_text16() interface extracts a UTF-16 string
4538** in the native byte-order of the host machine.  ^The
4539** sqlite3_value_text16be() and sqlite3_value_text16le() interfaces
4540** extract UTF-16 strings as big-endian and little-endian respectively.
4541**
4542** ^(The sqlite3_value_numeric_type() interface attempts to apply
4543** numeric affinity to the value.  This means that an attempt is
4544** made to convert the value to an integer or floating point.  If
4545** such a conversion is possible without loss of information (in other
4546** words, if the value is a string that looks like a number)
4547** then the conversion is performed.  Otherwise no conversion occurs.
4548** The [SQLITE_INTEGER | datatype] after conversion is returned.)^
4549**
4550** Please pay particular attention to the fact that the pointer returned
4551** from [sqlite3_value_blob()], [sqlite3_value_text()], or
4552** [sqlite3_value_text16()] can be invalidated by a subsequent call to
4553** [sqlite3_value_bytes()], [sqlite3_value_bytes16()], [sqlite3_value_text()],
4554** or [sqlite3_value_text16()].
4555**
4556** These routines must be called from the same thread as
4557** the SQL function that supplied the [sqlite3_value*] parameters.
4558*/
4559SQLITE_API const void *SQLITE_STDCALL sqlite3_value_blob(sqlite3_value*);
4560SQLITE_API int SQLITE_STDCALL sqlite3_value_bytes(sqlite3_value*);
4561SQLITE_API int SQLITE_STDCALL sqlite3_value_bytes16(sqlite3_value*);
4562SQLITE_API double SQLITE_STDCALL sqlite3_value_double(sqlite3_value*);
4563SQLITE_API int SQLITE_STDCALL sqlite3_value_int(sqlite3_value*);
4564SQLITE_API sqlite3_int64 SQLITE_STDCALL sqlite3_value_int64(sqlite3_value*);
4565SQLITE_API const unsigned char *SQLITE_STDCALL sqlite3_value_text(sqlite3_value*);
4566SQLITE_API const void *SQLITE_STDCALL sqlite3_value_text16(sqlite3_value*);
4567SQLITE_API const void *SQLITE_STDCALL sqlite3_value_text16le(sqlite3_value*);
4568SQLITE_API const void *SQLITE_STDCALL sqlite3_value_text16be(sqlite3_value*);
4569SQLITE_API int SQLITE_STDCALL sqlite3_value_type(sqlite3_value*);
4570SQLITE_API int SQLITE_STDCALL sqlite3_value_numeric_type(sqlite3_value*);
4571
4572/*
4573** CAPI3REF: Copy And Free SQL Values
4574** METHOD: sqlite3_value
4575**
4576** ^The sqlite3_value_dup(V) interface makes a copy of the [sqlite3_value]
4577** object D and returns a pointer to that copy.  ^The [sqlite3_value] returned
4578** is a [protected sqlite3_value] object even if the input is not.
4579** ^The sqlite3_value_dup(V) interface returns NULL if V is NULL or if a
4580** memory allocation fails.
4581**
4582** ^The sqlite3_value_free(V) interface frees an [sqlite3_value] object
4583** previously obtained from [sqlite3_value_dup()].  ^If V is a NULL pointer
4584** then sqlite3_value_free(V) is a harmless no-op.
4585*/
4586SQLITE_API SQLITE_EXPERIMENTAL sqlite3_value *SQLITE_STDCALL sqlite3_value_dup(const sqlite3_value*);
4587SQLITE_API SQLITE_EXPERIMENTAL void SQLITE_STDCALL sqlite3_value_free(sqlite3_value*);
4588
4589/*
4590** CAPI3REF: Obtain Aggregate Function Context
4591** METHOD: sqlite3_context
4592**
4593** Implementations of aggregate SQL functions use this
4594** routine to allocate memory for storing their state.
4595**
4596** ^The first time the sqlite3_aggregate_context(C,N) routine is called
4597** for a particular aggregate function, SQLite
4598** allocates N of memory, zeroes out that memory, and returns a pointer
4599** to the new memory. ^On second and subsequent calls to
4600** sqlite3_aggregate_context() for the same aggregate function instance,
4601** the same buffer is returned.  Sqlite3_aggregate_context() is normally
4602** called once for each invocation of the xStep callback and then one
4603** last time when the xFinal callback is invoked.  ^(When no rows match
4604** an aggregate query, the xStep() callback of the aggregate function
4605** implementation is never called and xFinal() is called exactly once.
4606** In those cases, sqlite3_aggregate_context() might be called for the
4607** first time from within xFinal().)^
4608**
4609** ^The sqlite3_aggregate_context(C,N) routine returns a NULL pointer
4610** when first called if N is less than or equal to zero or if a memory
4611** allocate error occurs.
4612**
4613** ^(The amount of space allocated by sqlite3_aggregate_context(C,N) is
4614** determined by the N parameter on first successful call.  Changing the
4615** value of N in subsequent call to sqlite3_aggregate_context() within
4616** the same aggregate function instance will not resize the memory
4617** allocation.)^  Within the xFinal callback, it is customary to set
4618** N=0 in calls to sqlite3_aggregate_context(C,N) so that no
4619** pointless memory allocations occur.
4620**
4621** ^SQLite automatically frees the memory allocated by
4622** sqlite3_aggregate_context() when the aggregate query concludes.
4623**
4624** The first parameter must be a copy of the
4625** [sqlite3_context | SQL function context] that is the first parameter
4626** to the xStep or xFinal callback routine that implements the aggregate
4627** function.
4628**
4629** This routine must be called from the same thread in which
4630** the aggregate SQL function is running.
4631*/
4632SQLITE_API void *SQLITE_STDCALL sqlite3_aggregate_context(sqlite3_context*, int nBytes);
4633
4634/*
4635** CAPI3REF: User Data For Functions
4636** METHOD: sqlite3_context
4637**
4638** ^The sqlite3_user_data() interface returns a copy of
4639** the pointer that was the pUserData parameter (the 5th parameter)
4640** of the [sqlite3_create_function()]
4641** and [sqlite3_create_function16()] routines that originally
4642** registered the application defined function.
4643**
4644** This routine must be called from the same thread in which
4645** the application-defined function is running.
4646*/
4647SQLITE_API void *SQLITE_STDCALL sqlite3_user_data(sqlite3_context*);
4648
4649/*
4650** CAPI3REF: Database Connection For Functions
4651** METHOD: sqlite3_context
4652**
4653** ^The sqlite3_context_db_handle() interface returns a copy of
4654** the pointer to the [database connection] (the 1st parameter)
4655** of the [sqlite3_create_function()]
4656** and [sqlite3_create_function16()] routines that originally
4657** registered the application defined function.
4658*/
4659SQLITE_API sqlite3 *SQLITE_STDCALL sqlite3_context_db_handle(sqlite3_context*);
4660
4661/*
4662** CAPI3REF: Function Auxiliary Data
4663** METHOD: sqlite3_context
4664**
4665** These functions may be used by (non-aggregate) SQL functions to
4666** associate metadata with argument values. If the same value is passed to
4667** multiple invocations of the same SQL function during query execution, under
4668** some circumstances the associated metadata may be preserved.  An example
4669** of where this might be useful is in a regular-expression matching
4670** function. The compiled version of the regular expression can be stored as
4671** metadata associated with the pattern string.
4672** Then as long as the pattern string remains the same,
4673** the compiled regular expression can be reused on multiple
4674** invocations of the same function.
4675**
4676** ^The sqlite3_get_auxdata() interface returns a pointer to the metadata
4677** associated by the sqlite3_set_auxdata() function with the Nth argument
4678** value to the application-defined function. ^If there is no metadata
4679** associated with the function argument, this sqlite3_get_auxdata() interface
4680** returns a NULL pointer.
4681**
4682** ^The sqlite3_set_auxdata(C,N,P,X) interface saves P as metadata for the N-th
4683** argument of the application-defined function.  ^Subsequent
4684** calls to sqlite3_get_auxdata(C,N) return P from the most recent
4685** sqlite3_set_auxdata(C,N,P,X) call if the metadata is still valid or
4686** NULL if the metadata has been discarded.
4687** ^After each call to sqlite3_set_auxdata(C,N,P,X) where X is not NULL,
4688** SQLite will invoke the destructor function X with parameter P exactly
4689** once, when the metadata is discarded.
4690** SQLite is free to discard the metadata at any time, including: <ul>
4691** <li> when the corresponding function parameter changes, or
4692** <li> when [sqlite3_reset()] or [sqlite3_finalize()] is called for the
4693**      SQL statement, or
4694** <li> when sqlite3_set_auxdata() is invoked again on the same parameter, or
4695** <li> during the original sqlite3_set_auxdata() call when a memory
4696**      allocation error occurs. </ul>)^
4697**
4698** Note the last bullet in particular.  The destructor X in
4699** sqlite3_set_auxdata(C,N,P,X) might be called immediately, before the
4700** sqlite3_set_auxdata() interface even returns.  Hence sqlite3_set_auxdata()
4701** should be called near the end of the function implementation and the
4702** function implementation should not make any use of P after
4703** sqlite3_set_auxdata() has been called.
4704**
4705** ^(In practice, metadata is preserved between function calls for
4706** function parameters that are compile-time constants, including literal
4707** values and [parameters] and expressions composed from the same.)^
4708**
4709** These routines must be called from the same thread in which
4710** the SQL function is running.
4711*/
4712SQLITE_API void *SQLITE_STDCALL sqlite3_get_auxdata(sqlite3_context*, int N);
4713SQLITE_API void SQLITE_STDCALL sqlite3_set_auxdata(sqlite3_context*, int N, void*, void (*)(void*));
4714
4715
4716/*
4717** CAPI3REF: Constants Defining Special Destructor Behavior
4718**
4719** These are special values for the destructor that is passed in as the
4720** final argument to routines like [sqlite3_result_blob()].  ^If the destructor
4721** argument is SQLITE_STATIC, it means that the content pointer is constant
4722** and will never change.  It does not need to be destroyed.  ^The
4723** SQLITE_TRANSIENT value means that the content will likely change in
4724** the near future and that SQLite should make its own private copy of
4725** the content before returning.
4726**
4727** The typedef is necessary to work around problems in certain
4728** C++ compilers.
4729*/
4730typedef void (*sqlite3_destructor_type)(void*);
4731#define SQLITE_STATIC      ((sqlite3_destructor_type)0)
4732#define SQLITE_TRANSIENT   ((sqlite3_destructor_type)-1)
4733
4734/*
4735** CAPI3REF: Setting The Result Of An SQL Function
4736** METHOD: sqlite3_context
4737**
4738** These routines are used by the xFunc or xFinal callbacks that
4739** implement SQL functions and aggregates.  See
4740** [sqlite3_create_function()] and [sqlite3_create_function16()]
4741** for additional information.
4742**
4743** These functions work very much like the [parameter binding] family of
4744** functions used to bind values to host parameters in prepared statements.
4745** Refer to the [SQL parameter] documentation for additional information.
4746**
4747** ^The sqlite3_result_blob() interface sets the result from
4748** an application-defined function to be the BLOB whose content is pointed
4749** to by the second parameter and which is N bytes long where N is the
4750** third parameter.
4751**
4752** ^The sqlite3_result_zeroblob(C,N) and sqlite3_result_zeroblob64(C,N)
4753** interfaces set the result of the application-defined function to be
4754** a BLOB containing all zero bytes and N bytes in size.
4755**
4756** ^The sqlite3_result_double() interface sets the result from
4757** an application-defined function to be a floating point value specified
4758** by its 2nd argument.
4759**
4760** ^The sqlite3_result_error() and sqlite3_result_error16() functions
4761** cause the implemented SQL function to throw an exception.
4762** ^SQLite uses the string pointed to by the
4763** 2nd parameter of sqlite3_result_error() or sqlite3_result_error16()
4764** as the text of an error message.  ^SQLite interprets the error
4765** message string from sqlite3_result_error() as UTF-8. ^SQLite
4766** interprets the string from sqlite3_result_error16() as UTF-16 in native
4767** byte order.  ^If the third parameter to sqlite3_result_error()
4768** or sqlite3_result_error16() is negative then SQLite takes as the error
4769** message all text up through the first zero character.
4770** ^If the third parameter to sqlite3_result_error() or
4771** sqlite3_result_error16() is non-negative then SQLite takes that many
4772** bytes (not characters) from the 2nd parameter as the error message.
4773** ^The sqlite3_result_error() and sqlite3_result_error16()
4774** routines make a private copy of the error message text before
4775** they return.  Hence, the calling function can deallocate or
4776** modify the text after they return without harm.
4777** ^The sqlite3_result_error_code() function changes the error code
4778** returned by SQLite as a result of an error in a function.  ^By default,
4779** the error code is SQLITE_ERROR.  ^A subsequent call to sqlite3_result_error()
4780** or sqlite3_result_error16() resets the error code to SQLITE_ERROR.
4781**
4782** ^The sqlite3_result_error_toobig() interface causes SQLite to throw an
4783** error indicating that a string or BLOB is too long to represent.
4784**
4785** ^The sqlite3_result_error_nomem() interface causes SQLite to throw an
4786** error indicating that a memory allocation failed.
4787**
4788** ^The sqlite3_result_int() interface sets the return value
4789** of the application-defined function to be the 32-bit signed integer
4790** value given in the 2nd argument.
4791** ^The sqlite3_result_int64() interface sets the return value
4792** of the application-defined function to be the 64-bit signed integer
4793** value given in the 2nd argument.
4794**
4795** ^The sqlite3_result_null() interface sets the return value
4796** of the application-defined function to be NULL.
4797**
4798** ^The sqlite3_result_text(), sqlite3_result_text16(),
4799** sqlite3_result_text16le(), and sqlite3_result_text16be() interfaces
4800** set the return value of the application-defined function to be
4801** a text string which is represented as UTF-8, UTF-16 native byte order,
4802** UTF-16 little endian, or UTF-16 big endian, respectively.
4803** ^The sqlite3_result_text64() interface sets the return value of an
4804** application-defined function to be a text string in an encoding
4805** specified by the fifth (and last) parameter, which must be one
4806** of [SQLITE_UTF8], [SQLITE_UTF16], [SQLITE_UTF16BE], or [SQLITE_UTF16LE].
4807** ^SQLite takes the text result from the application from
4808** the 2nd parameter of the sqlite3_result_text* interfaces.
4809** ^If the 3rd parameter to the sqlite3_result_text* interfaces
4810** is negative, then SQLite takes result text from the 2nd parameter
4811** through the first zero character.
4812** ^If the 3rd parameter to the sqlite3_result_text* interfaces
4813** is non-negative, then as many bytes (not characters) of the text
4814** pointed to by the 2nd parameter are taken as the application-defined
4815** function result.  If the 3rd parameter is non-negative, then it
4816** must be the byte offset into the string where the NUL terminator would
4817** appear if the string where NUL terminated.  If any NUL characters occur
4818** in the string at a byte offset that is less than the value of the 3rd
4819** parameter, then the resulting string will contain embedded NULs and the
4820** result of expressions operating on strings with embedded NULs is undefined.
4821** ^If the 4th parameter to the sqlite3_result_text* interfaces
4822** or sqlite3_result_blob is a non-NULL pointer, then SQLite calls that
4823** function as the destructor on the text or BLOB result when it has
4824** finished using that result.
4825** ^If the 4th parameter to the sqlite3_result_text* interfaces or to
4826** sqlite3_result_blob is the special constant SQLITE_STATIC, then SQLite
4827** assumes that the text or BLOB result is in constant space and does not
4828** copy the content of the parameter nor call a destructor on the content
4829** when it has finished using that result.
4830** ^If the 4th parameter to the sqlite3_result_text* interfaces
4831** or sqlite3_result_blob is the special constant SQLITE_TRANSIENT
4832** then SQLite makes a copy of the result into space obtained from
4833** from [sqlite3_malloc()] before it returns.
4834**
4835** ^The sqlite3_result_value() interface sets the result of
4836** the application-defined function to be a copy of the
4837** [unprotected sqlite3_value] object specified by the 2nd parameter.  ^The
4838** sqlite3_result_value() interface makes a copy of the [sqlite3_value]
4839** so that the [sqlite3_value] specified in the parameter may change or
4840** be deallocated after sqlite3_result_value() returns without harm.
4841** ^A [protected sqlite3_value] object may always be used where an
4842** [unprotected sqlite3_value] object is required, so either
4843** kind of [sqlite3_value] object can be used with this interface.
4844**
4845** If these routines are called from within the different thread
4846** than the one containing the application-defined function that received
4847** the [sqlite3_context] pointer, the results are undefined.
4848*/
4849SQLITE_API void SQLITE_STDCALL sqlite3_result_blob(sqlite3_context*, const void*, int, void(*)(void*));
4850SQLITE_API void SQLITE_STDCALL sqlite3_result_blob64(sqlite3_context*,const void*,
4851                           sqlite3_uint64,void(*)(void*));
4852SQLITE_API void SQLITE_STDCALL sqlite3_result_double(sqlite3_context*, double);
4853SQLITE_API void SQLITE_STDCALL sqlite3_result_error(sqlite3_context*, const char*, int);
4854SQLITE_API void SQLITE_STDCALL sqlite3_result_error16(sqlite3_context*, const void*, int);
4855SQLITE_API void SQLITE_STDCALL sqlite3_result_error_toobig(sqlite3_context*);
4856SQLITE_API void SQLITE_STDCALL sqlite3_result_error_nomem(sqlite3_context*);
4857SQLITE_API void SQLITE_STDCALL sqlite3_result_error_code(sqlite3_context*, int);
4858SQLITE_API void SQLITE_STDCALL sqlite3_result_int(sqlite3_context*, int);
4859SQLITE_API void SQLITE_STDCALL sqlite3_result_int64(sqlite3_context*, sqlite3_int64);
4860SQLITE_API void SQLITE_STDCALL sqlite3_result_null(sqlite3_context*);
4861SQLITE_API void SQLITE_STDCALL sqlite3_result_text(sqlite3_context*, const char*, int, void(*)(void*));
4862SQLITE_API void SQLITE_STDCALL sqlite3_result_text64(sqlite3_context*, const char*,sqlite3_uint64,
4863                           void(*)(void*), unsigned char encoding);
4864SQLITE_API void SQLITE_STDCALL sqlite3_result_text16(sqlite3_context*, const void*, int, void(*)(void*));
4865SQLITE_API void SQLITE_STDCALL sqlite3_result_text16le(sqlite3_context*, const void*, int,void(*)(void*));
4866SQLITE_API void SQLITE_STDCALL sqlite3_result_text16be(sqlite3_context*, const void*, int,void(*)(void*));
4867SQLITE_API void SQLITE_STDCALL sqlite3_result_value(sqlite3_context*, sqlite3_value*);
4868SQLITE_API void SQLITE_STDCALL sqlite3_result_zeroblob(sqlite3_context*, int n);
4869SQLITE_API int SQLITE_STDCALL sqlite3_result_zeroblob64(sqlite3_context*, sqlite3_uint64 n);
4870
4871/*
4872** CAPI3REF: Define New Collating Sequences
4873** METHOD: sqlite3
4874**
4875** ^These functions add, remove, or modify a [collation] associated
4876** with the [database connection] specified as the first argument.
4877**
4878** ^The name of the collation is a UTF-8 string
4879** for sqlite3_create_collation() and sqlite3_create_collation_v2()
4880** and a UTF-16 string in native byte order for sqlite3_create_collation16().
4881** ^Collation names that compare equal according to [sqlite3_strnicmp()] are
4882** considered to be the same name.
4883**
4884** ^(The third argument (eTextRep) must be one of the constants:
4885** <ul>
4886** <li> [SQLITE_UTF8],
4887** <li> [SQLITE_UTF16LE],
4888** <li> [SQLITE_UTF16BE],
4889** <li> [SQLITE_UTF16], or
4890** <li> [SQLITE_UTF16_ALIGNED].
4891** </ul>)^
4892** ^The eTextRep argument determines the encoding of strings passed
4893** to the collating function callback, xCallback.
4894** ^The [SQLITE_UTF16] and [SQLITE_UTF16_ALIGNED] values for eTextRep
4895** force strings to be UTF16 with native byte order.
4896** ^The [SQLITE_UTF16_ALIGNED] value for eTextRep forces strings to begin
4897** on an even byte address.
4898**
4899** ^The fourth argument, pArg, is an application data pointer that is passed
4900** through as the first argument to the collating function callback.
4901**
4902** ^The fifth argument, xCallback, is a pointer to the collating function.
4903** ^Multiple collating functions can be registered using the same name but
4904** with different eTextRep parameters and SQLite will use whichever
4905** function requires the least amount of data transformation.
4906** ^If the xCallback argument is NULL then the collating function is
4907** deleted.  ^When all collating functions having the same name are deleted,
4908** that collation is no longer usable.
4909**
4910** ^The collating function callback is invoked with a copy of the pArg
4911** application data pointer and with two strings in the encoding specified
4912** by the eTextRep argument.  The collating function must return an
4913** integer that is negative, zero, or positive
4914** if the first string is less than, equal to, or greater than the second,
4915** respectively.  A collating function must always return the same answer
4916** given the same inputs.  If two or more collating functions are registered
4917** to the same collation name (using different eTextRep values) then all
4918** must give an equivalent answer when invoked with equivalent strings.
4919** The collating function must obey the following properties for all
4920** strings A, B, and C:
4921**
4922** <ol>
4923** <li> If A==B then B==A.
4924** <li> If A==B and B==C then A==C.
4925** <li> If A&lt;B THEN B&gt;A.
4926** <li> If A&lt;B and B&lt;C then A&lt;C.
4927** </ol>
4928**
4929** If a collating function fails any of the above constraints and that
4930** collating function is  registered and used, then the behavior of SQLite
4931** is undefined.
4932**
4933** ^The sqlite3_create_collation_v2() works like sqlite3_create_collation()
4934** with the addition that the xDestroy callback is invoked on pArg when
4935** the collating function is deleted.
4936** ^Collating functions are deleted when they are overridden by later
4937** calls to the collation creation functions or when the
4938** [database connection] is closed using [sqlite3_close()].
4939**
4940** ^The xDestroy callback is <u>not</u> called if the
4941** sqlite3_create_collation_v2() function fails.  Applications that invoke
4942** sqlite3_create_collation_v2() with a non-NULL xDestroy argument should
4943** check the return code and dispose of the application data pointer
4944** themselves rather than expecting SQLite to deal with it for them.
4945** This is different from every other SQLite interface.  The inconsistency
4946** is unfortunate but cannot be changed without breaking backwards
4947** compatibility.
4948**
4949** See also:  [sqlite3_collation_needed()] and [sqlite3_collation_needed16()].
4950*/
4951SQLITE_API int SQLITE_STDCALL sqlite3_create_collation(
4952  sqlite3*,
4953  const char *zName,
4954  int eTextRep,
4955  void *pArg,
4956  int(*xCompare)(void*,int,const void*,int,const void*)
4957);
4958SQLITE_API int SQLITE_STDCALL sqlite3_create_collation_v2(
4959  sqlite3*,
4960  const char *zName,
4961  int eTextRep,
4962  void *pArg,
4963  int(*xCompare)(void*,int,const void*,int,const void*),
4964  void(*xDestroy)(void*)
4965);
4966SQLITE_API int SQLITE_STDCALL sqlite3_create_collation16(
4967  sqlite3*,
4968  const void *zName,
4969  int eTextRep,
4970  void *pArg,
4971  int(*xCompare)(void*,int,const void*,int,const void*)
4972);
4973
4974/*
4975** CAPI3REF: Collation Needed Callbacks
4976** METHOD: sqlite3
4977**
4978** ^To avoid having to register all collation sequences before a database
4979** can be used, a single callback function may be registered with the
4980** [database connection] to be invoked whenever an undefined collation
4981** sequence is required.
4982**
4983** ^If the function is registered using the sqlite3_collation_needed() API,
4984** then it is passed the names of undefined collation sequences as strings
4985** encoded in UTF-8. ^If sqlite3_collation_needed16() is used,
4986** the names are passed as UTF-16 in machine native byte order.
4987** ^A call to either function replaces the existing collation-needed callback.
4988**
4989** ^(When the callback is invoked, the first argument passed is a copy
4990** of the second argument to sqlite3_collation_needed() or
4991** sqlite3_collation_needed16().  The second argument is the database
4992** connection.  The third argument is one of [SQLITE_UTF8], [SQLITE_UTF16BE],
4993** or [SQLITE_UTF16LE], indicating the most desirable form of the collation
4994** sequence function required.  The fourth parameter is the name of the
4995** required collation sequence.)^
4996**
4997** The callback function should register the desired collation using
4998** [sqlite3_create_collation()], [sqlite3_create_collation16()], or
4999** [sqlite3_create_collation_v2()].
5000*/
5001SQLITE_API int SQLITE_STDCALL sqlite3_collation_needed(
5002  sqlite3*,
5003  void*,
5004  void(*)(void*,sqlite3*,int eTextRep,const char*)
5005);
5006SQLITE_API int SQLITE_STDCALL sqlite3_collation_needed16(
5007  sqlite3*,
5008  void*,
5009  void(*)(void*,sqlite3*,int eTextRep,const void*)
5010);
5011
5012#ifdef SQLITE_HAS_CODEC
5013/*
5014** Specify the key for an encrypted database.  This routine should be
5015** called right after sqlite3_open().
5016**
5017** The code to implement this API is not available in the public release
5018** of SQLite.
5019*/
5020SQLITE_API int SQLITE_STDCALL sqlite3_key(
5021  sqlite3 *db,                   /* Database to be rekeyed */
5022  const void *pKey, int nKey     /* The key */
5023);
5024SQLITE_API int SQLITE_STDCALL sqlite3_key_v2(
5025  sqlite3 *db,                   /* Database to be rekeyed */
5026  const char *zDbName,           /* Name of the database */
5027  const void *pKey, int nKey     /* The key */
5028);
5029
5030/*
5031** Change the key on an open database.  If the current database is not
5032** encrypted, this routine will encrypt it.  If pNew==0 or nNew==0, the
5033** database is decrypted.
5034**
5035** The code to implement this API is not available in the public release
5036** of SQLite.
5037*/
5038SQLITE_API int SQLITE_STDCALL sqlite3_rekey(
5039  sqlite3 *db,                   /* Database to be rekeyed */
5040  const void *pKey, int nKey     /* The new key */
5041);
5042SQLITE_API int SQLITE_STDCALL sqlite3_rekey_v2(
5043  sqlite3 *db,                   /* Database to be rekeyed */
5044  const char *zDbName,           /* Name of the database */
5045  const void *pKey, int nKey     /* The new key */
5046);
5047
5048/*
5049** Specify the activation key for a SEE database.  Unless
5050** activated, none of the SEE routines will work.
5051*/
5052SQLITE_API void SQLITE_STDCALL sqlite3_activate_see(
5053  const char *zPassPhrase        /* Activation phrase */
5054);
5055#endif
5056
5057#ifdef SQLITE_ENABLE_CEROD
5058/*
5059** Specify the activation key for a CEROD database.  Unless
5060** activated, none of the CEROD routines will work.
5061*/
5062SQLITE_API void SQLITE_STDCALL sqlite3_activate_cerod(
5063  const char *zPassPhrase        /* Activation phrase */
5064);
5065#endif
5066
5067/*
5068** CAPI3REF: Suspend Execution For A Short Time
5069**
5070** The sqlite3_sleep() function causes the current thread to suspend execution
5071** for at least a number of milliseconds specified in its parameter.
5072**
5073** If the operating system does not support sleep requests with
5074** millisecond time resolution, then the time will be rounded up to
5075** the nearest second. The number of milliseconds of sleep actually
5076** requested from the operating system is returned.
5077**
5078** ^SQLite implements this interface by calling the xSleep()
5079** method of the default [sqlite3_vfs] object.  If the xSleep() method
5080** of the default VFS is not implemented correctly, or not implemented at
5081** all, then the behavior of sqlite3_sleep() may deviate from the description
5082** in the previous paragraphs.
5083*/
5084SQLITE_API int SQLITE_STDCALL sqlite3_sleep(int);
5085
5086/*
5087** CAPI3REF: Name Of The Folder Holding Temporary Files
5088**
5089** ^(If this global variable is made to point to a string which is
5090** the name of a folder (a.k.a. directory), then all temporary files
5091** created by SQLite when using a built-in [sqlite3_vfs | VFS]
5092** will be placed in that directory.)^  ^If this variable
5093** is a NULL pointer, then SQLite performs a search for an appropriate
5094** temporary file directory.
5095**
5096** Applications are strongly discouraged from using this global variable.
5097** It is required to set a temporary folder on Windows Runtime (WinRT).
5098** But for all other platforms, it is highly recommended that applications
5099** neither read nor write this variable.  This global variable is a relic
5100** that exists for backwards compatibility of legacy applications and should
5101** be avoided in new projects.
5102**
5103** It is not safe to read or modify this variable in more than one
5104** thread at a time.  It is not safe to read or modify this variable
5105** if a [database connection] is being used at the same time in a separate
5106** thread.
5107** It is intended that this variable be set once
5108** as part of process initialization and before any SQLite interface
5109** routines have been called and that this variable remain unchanged
5110** thereafter.
5111**
5112** ^The [temp_store_directory pragma] may modify this variable and cause
5113** it to point to memory obtained from [sqlite3_malloc].  ^Furthermore,
5114** the [temp_store_directory pragma] always assumes that any string
5115** that this variable points to is held in memory obtained from
5116** [sqlite3_malloc] and the pragma may attempt to free that memory
5117** using [sqlite3_free].
5118** Hence, if this variable is modified directly, either it should be
5119** made NULL or made to point to memory obtained from [sqlite3_malloc]
5120** or else the use of the [temp_store_directory pragma] should be avoided.
5121** Except when requested by the [temp_store_directory pragma], SQLite
5122** does not free the memory that sqlite3_temp_directory points to.  If
5123** the application wants that memory to be freed, it must do
5124** so itself, taking care to only do so after all [database connection]
5125** objects have been destroyed.
5126**
5127** <b>Note to Windows Runtime users:</b>  The temporary directory must be set
5128** prior to calling [sqlite3_open] or [sqlite3_open_v2].  Otherwise, various
5129** features that require the use of temporary files may fail.  Here is an
5130** example of how to do this using C++ with the Windows Runtime:
5131**
5132** <blockquote><pre>
5133** LPCWSTR zPath = Windows::Storage::ApplicationData::Current->
5134** &nbsp;     TemporaryFolder->Path->Data();
5135** char zPathBuf&#91;MAX_PATH + 1&#93;;
5136** memset(zPathBuf, 0, sizeof(zPathBuf));
5137** WideCharToMultiByte(CP_UTF8, 0, zPath, -1, zPathBuf, sizeof(zPathBuf),
5138** &nbsp;     NULL, NULL);
5139** sqlite3_temp_directory = sqlite3_mprintf("%s", zPathBuf);
5140** </pre></blockquote>
5141*/
5142SQLITE_API char *sqlite3_temp_directory;
5143
5144/*
5145** CAPI3REF: Name Of The Folder Holding Database Files
5146**
5147** ^(If this global variable is made to point to a string which is
5148** the name of a folder (a.k.a. directory), then all database files
5149** specified with a relative pathname and created or accessed by
5150** SQLite when using a built-in windows [sqlite3_vfs | VFS] will be assumed
5151** to be relative to that directory.)^ ^If this variable is a NULL
5152** pointer, then SQLite assumes that all database files specified
5153** with a relative pathname are relative to the current directory
5154** for the process.  Only the windows VFS makes use of this global
5155** variable; it is ignored by the unix VFS.
5156**
5157** Changing the value of this variable while a database connection is
5158** open can result in a corrupt database.
5159**
5160** It is not safe to read or modify this variable in more than one
5161** thread at a time.  It is not safe to read or modify this variable
5162** if a [database connection] is being used at the same time in a separate
5163** thread.
5164** It is intended that this variable be set once
5165** as part of process initialization and before any SQLite interface
5166** routines have been called and that this variable remain unchanged
5167** thereafter.
5168**
5169** ^The [data_store_directory pragma] may modify this variable and cause
5170** it to point to memory obtained from [sqlite3_malloc].  ^Furthermore,
5171** the [data_store_directory pragma] always assumes that any string
5172** that this variable points to is held in memory obtained from
5173** [sqlite3_malloc] and the pragma may attempt to free that memory
5174** using [sqlite3_free].
5175** Hence, if this variable is modified directly, either it should be
5176** made NULL or made to point to memory obtained from [sqlite3_malloc]
5177** or else the use of the [data_store_directory pragma] should be avoided.
5178*/
5179SQLITE_API char *sqlite3_data_directory;
5180
5181/*
5182** CAPI3REF: Test For Auto-Commit Mode
5183** KEYWORDS: {autocommit mode}
5184** METHOD: sqlite3
5185**
5186** ^The sqlite3_get_autocommit() interface returns non-zero or
5187** zero if the given database connection is or is not in autocommit mode,
5188** respectively.  ^Autocommit mode is on by default.
5189** ^Autocommit mode is disabled by a [BEGIN] statement.
5190** ^Autocommit mode is re-enabled by a [COMMIT] or [ROLLBACK].
5191**
5192** If certain kinds of errors occur on a statement within a multi-statement
5193** transaction (errors including [SQLITE_FULL], [SQLITE_IOERR],
5194** [SQLITE_NOMEM], [SQLITE_BUSY], and [SQLITE_INTERRUPT]) then the
5195** transaction might be rolled back automatically.  The only way to
5196** find out whether SQLite automatically rolled back the transaction after
5197** an error is to use this function.
5198**
5199** If another thread changes the autocommit status of the database
5200** connection while this routine is running, then the return value
5201** is undefined.
5202*/
5203SQLITE_API int SQLITE_STDCALL sqlite3_get_autocommit(sqlite3*);
5204
5205/*
5206** CAPI3REF: Find The Database Handle Of A Prepared Statement
5207** METHOD: sqlite3_stmt
5208**
5209** ^The sqlite3_db_handle interface returns the [database connection] handle
5210** to which a [prepared statement] belongs.  ^The [database connection]
5211** returned by sqlite3_db_handle is the same [database connection]
5212** that was the first argument
5213** to the [sqlite3_prepare_v2()] call (or its variants) that was used to
5214** create the statement in the first place.
5215*/
5216SQLITE_API sqlite3 *SQLITE_STDCALL sqlite3_db_handle(sqlite3_stmt*);
5217
5218/*
5219** CAPI3REF: Return The Filename For A Database Connection
5220** METHOD: sqlite3
5221**
5222** ^The sqlite3_db_filename(D,N) interface returns a pointer to a filename
5223** associated with database N of connection D.  ^The main database file
5224** has the name "main".  If there is no attached database N on the database
5225** connection D, or if database N is a temporary or in-memory database, then
5226** a NULL pointer is returned.
5227**
5228** ^The filename returned by this function is the output of the
5229** xFullPathname method of the [VFS].  ^In other words, the filename
5230** will be an absolute pathname, even if the filename used
5231** to open the database originally was a URI or relative pathname.
5232*/
5233SQLITE_API const char *SQLITE_STDCALL sqlite3_db_filename(sqlite3 *db, const char *zDbName);
5234
5235/*
5236** CAPI3REF: Determine if a database is read-only
5237** METHOD: sqlite3
5238**
5239** ^The sqlite3_db_readonly(D,N) interface returns 1 if the database N
5240** of connection D is read-only, 0 if it is read/write, or -1 if N is not
5241** the name of a database on connection D.
5242*/
5243SQLITE_API int SQLITE_STDCALL sqlite3_db_readonly(sqlite3 *db, const char *zDbName);
5244
5245/*
5246** CAPI3REF: Find the next prepared statement
5247** METHOD: sqlite3
5248**
5249** ^This interface returns a pointer to the next [prepared statement] after
5250** pStmt associated with the [database connection] pDb.  ^If pStmt is NULL
5251** then this interface returns a pointer to the first prepared statement
5252** associated with the database connection pDb.  ^If no prepared statement
5253** satisfies the conditions of this routine, it returns NULL.
5254**
5255** The [database connection] pointer D in a call to
5256** [sqlite3_next_stmt(D,S)] must refer to an open database
5257** connection and in particular must not be a NULL pointer.
5258*/
5259SQLITE_API sqlite3_stmt *SQLITE_STDCALL sqlite3_next_stmt(sqlite3 *pDb, sqlite3_stmt *pStmt);
5260
5261/*
5262** CAPI3REF: Commit And Rollback Notification Callbacks
5263** METHOD: sqlite3
5264**
5265** ^The sqlite3_commit_hook() interface registers a callback
5266** function to be invoked whenever a transaction is [COMMIT | committed].
5267** ^Any callback set by a previous call to sqlite3_commit_hook()
5268** for the same database connection is overridden.
5269** ^The sqlite3_rollback_hook() interface registers a callback
5270** function to be invoked whenever a transaction is [ROLLBACK | rolled back].
5271** ^Any callback set by a previous call to sqlite3_rollback_hook()
5272** for the same database connection is overridden.
5273** ^The pArg argument is passed through to the callback.
5274** ^If the callback on a commit hook function returns non-zero,
5275** then the commit is converted into a rollback.
5276**
5277** ^The sqlite3_commit_hook(D,C,P) and sqlite3_rollback_hook(D,C,P) functions
5278** return the P argument from the previous call of the same function
5279** on the same [database connection] D, or NULL for
5280** the first call for each function on D.
5281**
5282** The commit and rollback hook callbacks are not reentrant.
5283** The callback implementation must not do anything that will modify
5284** the database connection that invoked the callback.  Any actions
5285** to modify the database connection must be deferred until after the
5286** completion of the [sqlite3_step()] call that triggered the commit
5287** or rollback hook in the first place.
5288** Note that running any other SQL statements, including SELECT statements,
5289** or merely calling [sqlite3_prepare_v2()] and [sqlite3_step()] will modify
5290** the database connections for the meaning of "modify" in this paragraph.
5291**
5292** ^Registering a NULL function disables the callback.
5293**
5294** ^When the commit hook callback routine returns zero, the [COMMIT]
5295** operation is allowed to continue normally.  ^If the commit hook
5296** returns non-zero, then the [COMMIT] is converted into a [ROLLBACK].
5297** ^The rollback hook is invoked on a rollback that results from a commit
5298** hook returning non-zero, just as it would be with any other rollback.
5299**
5300** ^For the purposes of this API, a transaction is said to have been
5301** rolled back if an explicit "ROLLBACK" statement is executed, or
5302** an error or constraint causes an implicit rollback to occur.
5303** ^The rollback callback is not invoked if a transaction is
5304** automatically rolled back because the database connection is closed.
5305**
5306** See also the [sqlite3_update_hook()] interface.
5307*/
5308SQLITE_API void *SQLITE_STDCALL sqlite3_commit_hook(sqlite3*, int(*)(void*), void*);
5309SQLITE_API void *SQLITE_STDCALL sqlite3_rollback_hook(sqlite3*, void(*)(void *), void*);
5310
5311/*
5312** CAPI3REF: Data Change Notification Callbacks
5313** METHOD: sqlite3
5314**
5315** ^The sqlite3_update_hook() interface registers a callback function
5316** with the [database connection] identified by the first argument
5317** to be invoked whenever a row is updated, inserted or deleted in
5318** a rowid table.
5319** ^Any callback set by a previous call to this function
5320** for the same database connection is overridden.
5321**
5322** ^The second argument is a pointer to the function to invoke when a
5323** row is updated, inserted or deleted in a rowid table.
5324** ^The first argument to the callback is a copy of the third argument
5325** to sqlite3_update_hook().
5326** ^The second callback argument is one of [SQLITE_INSERT], [SQLITE_DELETE],
5327** or [SQLITE_UPDATE], depending on the operation that caused the callback
5328** to be invoked.
5329** ^The third and fourth arguments to the callback contain pointers to the
5330** database and table name containing the affected row.
5331** ^The final callback parameter is the [rowid] of the row.
5332** ^In the case of an update, this is the [rowid] after the update takes place.
5333**
5334** ^(The update hook is not invoked when internal system tables are
5335** modified (i.e. sqlite_master and sqlite_sequence).)^
5336** ^The update hook is not invoked when [WITHOUT ROWID] tables are modified.
5337**
5338** ^In the current implementation, the update hook
5339** is not invoked when duplication rows are deleted because of an
5340** [ON CONFLICT | ON CONFLICT REPLACE] clause.  ^Nor is the update hook
5341** invoked when rows are deleted using the [truncate optimization].
5342** The exceptions defined in this paragraph might change in a future
5343** release of SQLite.
5344**
5345** The update hook implementation must not do anything that will modify
5346** the database connection that invoked the update hook.  Any actions
5347** to modify the database connection must be deferred until after the
5348** completion of the [sqlite3_step()] call that triggered the update hook.
5349** Note that [sqlite3_prepare_v2()] and [sqlite3_step()] both modify their
5350** database connections for the meaning of "modify" in this paragraph.
5351**
5352** ^The sqlite3_update_hook(D,C,P) function
5353** returns the P argument from the previous call
5354** on the same [database connection] D, or NULL for
5355** the first call on D.
5356**
5357** See also the [sqlite3_commit_hook()] and [sqlite3_rollback_hook()]
5358** interfaces.
5359*/
5360SQLITE_API void *SQLITE_STDCALL sqlite3_update_hook(
5361  sqlite3*,
5362  void(*)(void *,int ,char const *,char const *,sqlite3_int64),
5363  void*
5364);
5365
5366/*
5367** CAPI3REF: Enable Or Disable Shared Pager Cache
5368**
5369** ^(This routine enables or disables the sharing of the database cache
5370** and schema data structures between [database connection | connections]
5371** to the same database. Sharing is enabled if the argument is true
5372** and disabled if the argument is false.)^
5373**
5374** ^Cache sharing is enabled and disabled for an entire process.
5375** This is a change as of SQLite version 3.5.0. In prior versions of SQLite,
5376** sharing was enabled or disabled for each thread separately.
5377**
5378** ^(The cache sharing mode set by this interface effects all subsequent
5379** calls to [sqlite3_open()], [sqlite3_open_v2()], and [sqlite3_open16()].
5380** Existing database connections continue use the sharing mode
5381** that was in effect at the time they were opened.)^
5382**
5383** ^(This routine returns [SQLITE_OK] if shared cache was enabled or disabled
5384** successfully.  An [error code] is returned otherwise.)^
5385**
5386** ^Shared cache is disabled by default. But this might change in
5387** future releases of SQLite.  Applications that care about shared
5388** cache setting should set it explicitly.
5389**
5390** Note: This method is disabled on MacOS X 10.7 and iOS version 5.0
5391** and will always return SQLITE_MISUSE. On those systems,
5392** shared cache mode should be enabled per-database connection via
5393** [sqlite3_open_v2()] with [SQLITE_OPEN_SHAREDCACHE].
5394**
5395** This interface is threadsafe on processors where writing a
5396** 32-bit integer is atomic.
5397**
5398** See Also:  [SQLite Shared-Cache Mode]
5399*/
5400SQLITE_API int SQLITE_STDCALL sqlite3_enable_shared_cache(int);
5401
5402/*
5403** CAPI3REF: Attempt To Free Heap Memory
5404**
5405** ^The sqlite3_release_memory() interface attempts to free N bytes
5406** of heap memory by deallocating non-essential memory allocations
5407** held by the database library.   Memory used to cache database
5408** pages to improve performance is an example of non-essential memory.
5409** ^sqlite3_release_memory() returns the number of bytes actually freed,
5410** which might be more or less than the amount requested.
5411** ^The sqlite3_release_memory() routine is a no-op returning zero
5412** if SQLite is not compiled with [SQLITE_ENABLE_MEMORY_MANAGEMENT].
5413**
5414** See also: [sqlite3_db_release_memory()]
5415*/
5416SQLITE_API int SQLITE_STDCALL sqlite3_release_memory(int);
5417
5418/*
5419** CAPI3REF: Free Memory Used By A Database Connection
5420** METHOD: sqlite3
5421**
5422** ^The sqlite3_db_release_memory(D) interface attempts to free as much heap
5423** memory as possible from database connection D. Unlike the
5424** [sqlite3_release_memory()] interface, this interface is in effect even
5425** when the [SQLITE_ENABLE_MEMORY_MANAGEMENT] compile-time option is
5426** omitted.
5427**
5428** See also: [sqlite3_release_memory()]
5429*/
5430SQLITE_API int SQLITE_STDCALL sqlite3_db_release_memory(sqlite3*);
5431
5432/*
5433** CAPI3REF: Impose A Limit On Heap Size
5434**
5435** ^The sqlite3_soft_heap_limit64() interface sets and/or queries the
5436** soft limit on the amount of heap memory that may be allocated by SQLite.
5437** ^SQLite strives to keep heap memory utilization below the soft heap
5438** limit by reducing the number of pages held in the page cache
5439** as heap memory usages approaches the limit.
5440** ^The soft heap limit is "soft" because even though SQLite strives to stay
5441** below the limit, it will exceed the limit rather than generate
5442** an [SQLITE_NOMEM] error.  In other words, the soft heap limit
5443** is advisory only.
5444**
5445** ^The return value from sqlite3_soft_heap_limit64() is the size of
5446** the soft heap limit prior to the call, or negative in the case of an
5447** error.  ^If the argument N is negative
5448** then no change is made to the soft heap limit.  Hence, the current
5449** size of the soft heap limit can be determined by invoking
5450** sqlite3_soft_heap_limit64() with a negative argument.
5451**
5452** ^If the argument N is zero then the soft heap limit is disabled.
5453**
5454** ^(The soft heap limit is not enforced in the current implementation
5455** if one or more of following conditions are true:
5456**
5457** <ul>
5458** <li> The soft heap limit is set to zero.
5459** <li> Memory accounting is disabled using a combination of the
5460**      [sqlite3_config]([SQLITE_CONFIG_MEMSTATUS],...) start-time option and
5461**      the [SQLITE_DEFAULT_MEMSTATUS] compile-time option.
5462** <li> An alternative page cache implementation is specified using
5463**      [sqlite3_config]([SQLITE_CONFIG_PCACHE2],...).
5464** <li> The page cache allocates from its own memory pool supplied
5465**      by [sqlite3_config]([SQLITE_CONFIG_PAGECACHE],...) rather than
5466**      from the heap.
5467** </ul>)^
5468**
5469** Beginning with SQLite version 3.7.3, the soft heap limit is enforced
5470** regardless of whether or not the [SQLITE_ENABLE_MEMORY_MANAGEMENT]
5471** compile-time option is invoked.  With [SQLITE_ENABLE_MEMORY_MANAGEMENT],
5472** the soft heap limit is enforced on every memory allocation.  Without
5473** [SQLITE_ENABLE_MEMORY_MANAGEMENT], the soft heap limit is only enforced
5474** when memory is allocated by the page cache.  Testing suggests that because
5475** the page cache is the predominate memory user in SQLite, most
5476** applications will achieve adequate soft heap limit enforcement without
5477** the use of [SQLITE_ENABLE_MEMORY_MANAGEMENT].
5478**
5479** The circumstances under which SQLite will enforce the soft heap limit may
5480** changes in future releases of SQLite.
5481*/
5482SQLITE_API sqlite3_int64 SQLITE_STDCALL sqlite3_soft_heap_limit64(sqlite3_int64 N);
5483
5484/*
5485** CAPI3REF: Deprecated Soft Heap Limit Interface
5486** DEPRECATED
5487**
5488** This is a deprecated version of the [sqlite3_soft_heap_limit64()]
5489** interface.  This routine is provided for historical compatibility
5490** only.  All new applications should use the
5491** [sqlite3_soft_heap_limit64()] interface rather than this one.
5492*/
5493SQLITE_API SQLITE_DEPRECATED void SQLITE_STDCALL sqlite3_soft_heap_limit(int N);
5494
5495
5496/*
5497** CAPI3REF: Extract Metadata About A Column Of A Table
5498** METHOD: sqlite3
5499**
5500** ^(The sqlite3_table_column_metadata(X,D,T,C,....) routine returns
5501** information about column C of table T in database D
5502** on [database connection] X.)^  ^The sqlite3_table_column_metadata()
5503** interface returns SQLITE_OK and fills in the non-NULL pointers in
5504** the final five arguments with appropriate values if the specified
5505** column exists.  ^The sqlite3_table_column_metadata() interface returns
5506** SQLITE_ERROR and if the specified column does not exist.
5507** ^If the column-name parameter to sqlite3_table_column_metadata() is a
5508** NULL pointer, then this routine simply checks for the existance of the
5509** table and returns SQLITE_OK if the table exists and SQLITE_ERROR if it
5510** does not.
5511**
5512** ^The column is identified by the second, third and fourth parameters to
5513** this function. ^(The second parameter is either the name of the database
5514** (i.e. "main", "temp", or an attached database) containing the specified
5515** table or NULL.)^ ^If it is NULL, then all attached databases are searched
5516** for the table using the same algorithm used by the database engine to
5517** resolve unqualified table references.
5518**
5519** ^The third and fourth parameters to this function are the table and column
5520** name of the desired column, respectively.
5521**
5522** ^Metadata is returned by writing to the memory locations passed as the 5th
5523** and subsequent parameters to this function. ^Any of these arguments may be
5524** NULL, in which case the corresponding element of metadata is omitted.
5525**
5526** ^(<blockquote>
5527** <table border="1">
5528** <tr><th> Parameter <th> Output<br>Type <th>  Description
5529**
5530** <tr><td> 5th <td> const char* <td> Data type
5531** <tr><td> 6th <td> const char* <td> Name of default collation sequence
5532** <tr><td> 7th <td> int         <td> True if column has a NOT NULL constraint
5533** <tr><td> 8th <td> int         <td> True if column is part of the PRIMARY KEY
5534** <tr><td> 9th <td> int         <td> True if column is [AUTOINCREMENT]
5535** </table>
5536** </blockquote>)^
5537**
5538** ^The memory pointed to by the character pointers returned for the
5539** declaration type and collation sequence is valid until the next
5540** call to any SQLite API function.
5541**
5542** ^If the specified table is actually a view, an [error code] is returned.
5543**
5544** ^If the specified column is "rowid", "oid" or "_rowid_" and the table
5545** is not a [WITHOUT ROWID] table and an
5546** [INTEGER PRIMARY KEY] column has been explicitly declared, then the output
5547** parameters are set for the explicitly declared column. ^(If there is no
5548** [INTEGER PRIMARY KEY] column, then the outputs
5549** for the [rowid] are set as follows:
5550**
5551** <pre>
5552**     data type: "INTEGER"
5553**     collation sequence: "BINARY"
5554**     not null: 0
5555**     primary key: 1
5556**     auto increment: 0
5557** </pre>)^
5558**
5559** ^This function causes all database schemas to be read from disk and
5560** parsed, if that has not already been done, and returns an error if
5561** any errors are encountered while loading the schema.
5562*/
5563SQLITE_API int SQLITE_STDCALL sqlite3_table_column_metadata(
5564  sqlite3 *db,                /* Connection handle */
5565  const char *zDbName,        /* Database name or NULL */
5566  const char *zTableName,     /* Table name */
5567  const char *zColumnName,    /* Column name */
5568  char const **pzDataType,    /* OUTPUT: Declared data type */
5569  char const **pzCollSeq,     /* OUTPUT: Collation sequence name */
5570  int *pNotNull,              /* OUTPUT: True if NOT NULL constraint exists */
5571  int *pPrimaryKey,           /* OUTPUT: True if column part of PK */
5572  int *pAutoinc               /* OUTPUT: True if column is auto-increment */
5573);
5574
5575/*
5576** CAPI3REF: Load An Extension
5577** METHOD: sqlite3
5578**
5579** ^This interface loads an SQLite extension library from the named file.
5580**
5581** ^The sqlite3_load_extension() interface attempts to load an
5582** [SQLite extension] library contained in the file zFile.  If
5583** the file cannot be loaded directly, attempts are made to load
5584** with various operating-system specific extensions added.
5585** So for example, if "samplelib" cannot be loaded, then names like
5586** "samplelib.so" or "samplelib.dylib" or "samplelib.dll" might
5587** be tried also.
5588**
5589** ^The entry point is zProc.
5590** ^(zProc may be 0, in which case SQLite will try to come up with an
5591** entry point name on its own.  It first tries "sqlite3_extension_init".
5592** If that does not work, it constructs a name "sqlite3_X_init" where the
5593** X is consists of the lower-case equivalent of all ASCII alphabetic
5594** characters in the filename from the last "/" to the first following
5595** "." and omitting any initial "lib".)^
5596** ^The sqlite3_load_extension() interface returns
5597** [SQLITE_OK] on success and [SQLITE_ERROR] if something goes wrong.
5598** ^If an error occurs and pzErrMsg is not 0, then the
5599** [sqlite3_load_extension()] interface shall attempt to
5600** fill *pzErrMsg with error message text stored in memory
5601** obtained from [sqlite3_malloc()]. The calling function
5602** should free this memory by calling [sqlite3_free()].
5603**
5604** ^Extension loading must be enabled using
5605** [sqlite3_enable_load_extension()] prior to calling this API,
5606** otherwise an error will be returned.
5607**
5608** See also the [load_extension() SQL function].
5609*/
5610SQLITE_API int SQLITE_STDCALL sqlite3_load_extension(
5611  sqlite3 *db,          /* Load the extension into this database connection */
5612  const char *zFile,    /* Name of the shared library containing extension */
5613  const char *zProc,    /* Entry point.  Derived from zFile if 0 */
5614  char **pzErrMsg       /* Put error message here if not 0 */
5615);
5616
5617/*
5618** CAPI3REF: Enable Or Disable Extension Loading
5619** METHOD: sqlite3
5620**
5621** ^So as not to open security holes in older applications that are
5622** unprepared to deal with [extension loading], and as a means of disabling
5623** [extension loading] while evaluating user-entered SQL, the following API
5624** is provided to turn the [sqlite3_load_extension()] mechanism on and off.
5625**
5626** ^Extension loading is off by default.
5627** ^Call the sqlite3_enable_load_extension() routine with onoff==1
5628** to turn extension loading on and call it with onoff==0 to turn
5629** it back off again.
5630*/
5631SQLITE_API int SQLITE_STDCALL sqlite3_enable_load_extension(sqlite3 *db, int onoff);
5632
5633/*
5634** CAPI3REF: Automatically Load Statically Linked Extensions
5635**
5636** ^This interface causes the xEntryPoint() function to be invoked for
5637** each new [database connection] that is created.  The idea here is that
5638** xEntryPoint() is the entry point for a statically linked [SQLite extension]
5639** that is to be automatically loaded into all new database connections.
5640**
5641** ^(Even though the function prototype shows that xEntryPoint() takes
5642** no arguments and returns void, SQLite invokes xEntryPoint() with three
5643** arguments and expects and integer result as if the signature of the
5644** entry point where as follows:
5645**
5646** <blockquote><pre>
5647** &nbsp;  int xEntryPoint(
5648** &nbsp;    sqlite3 *db,
5649** &nbsp;    const char **pzErrMsg,
5650** &nbsp;    const struct sqlite3_api_routines *pThunk
5651** &nbsp;  );
5652** </pre></blockquote>)^
5653**
5654** If the xEntryPoint routine encounters an error, it should make *pzErrMsg
5655** point to an appropriate error message (obtained from [sqlite3_mprintf()])
5656** and return an appropriate [error code].  ^SQLite ensures that *pzErrMsg
5657** is NULL before calling the xEntryPoint().  ^SQLite will invoke
5658** [sqlite3_free()] on *pzErrMsg after xEntryPoint() returns.  ^If any
5659** xEntryPoint() returns an error, the [sqlite3_open()], [sqlite3_open16()],
5660** or [sqlite3_open_v2()] call that provoked the xEntryPoint() will fail.
5661**
5662** ^Calling sqlite3_auto_extension(X) with an entry point X that is already
5663** on the list of automatic extensions is a harmless no-op. ^No entry point
5664** will be called more than once for each database connection that is opened.
5665**
5666** See also: [sqlite3_reset_auto_extension()]
5667** and [sqlite3_cancel_auto_extension()]
5668*/
5669SQLITE_API int SQLITE_STDCALL sqlite3_auto_extension(void (*xEntryPoint)(void));
5670
5671/*
5672** CAPI3REF: Cancel Automatic Extension Loading
5673**
5674** ^The [sqlite3_cancel_auto_extension(X)] interface unregisters the
5675** initialization routine X that was registered using a prior call to
5676** [sqlite3_auto_extension(X)].  ^The [sqlite3_cancel_auto_extension(X)]
5677** routine returns 1 if initialization routine X was successfully
5678** unregistered and it returns 0 if X was not on the list of initialization
5679** routines.
5680*/
5681SQLITE_API int SQLITE_STDCALL sqlite3_cancel_auto_extension(void (*xEntryPoint)(void));
5682
5683/*
5684** CAPI3REF: Reset Automatic Extension Loading
5685**
5686** ^This interface disables all automatic extensions previously
5687** registered using [sqlite3_auto_extension()].
5688*/
5689SQLITE_API void SQLITE_STDCALL sqlite3_reset_auto_extension(void);
5690
5691/*
5692** The interface to the virtual-table mechanism is currently considered
5693** to be experimental.  The interface might change in incompatible ways.
5694** If this is a problem for you, do not use the interface at this time.
5695**
5696** When the virtual-table mechanism stabilizes, we will declare the
5697** interface fixed, support it indefinitely, and remove this comment.
5698*/
5699
5700/*
5701** Structures used by the virtual table interface
5702*/
5703typedef struct sqlite3_vtab sqlite3_vtab;
5704typedef struct sqlite3_index_info sqlite3_index_info;
5705typedef struct sqlite3_vtab_cursor sqlite3_vtab_cursor;
5706typedef struct sqlite3_module sqlite3_module;
5707
5708/*
5709** CAPI3REF: Virtual Table Object
5710** KEYWORDS: sqlite3_module {virtual table module}
5711**
5712** This structure, sometimes called a "virtual table module",
5713** defines the implementation of a [virtual tables].
5714** This structure consists mostly of methods for the module.
5715**
5716** ^A virtual table module is created by filling in a persistent
5717** instance of this structure and passing a pointer to that instance
5718** to [sqlite3_create_module()] or [sqlite3_create_module_v2()].
5719** ^The registration remains valid until it is replaced by a different
5720** module or until the [database connection] closes.  The content
5721** of this structure must not change while it is registered with
5722** any database connection.
5723*/
5724struct sqlite3_module {
5725  int iVersion;
5726  int (*xCreate)(sqlite3*, void *pAux,
5727               int argc, const char *const*argv,
5728               sqlite3_vtab **ppVTab, char**);
5729  int (*xConnect)(sqlite3*, void *pAux,
5730               int argc, const char *const*argv,
5731               sqlite3_vtab **ppVTab, char**);
5732  int (*xBestIndex)(sqlite3_vtab *pVTab, sqlite3_index_info*);
5733  int (*xDisconnect)(sqlite3_vtab *pVTab);
5734  int (*xDestroy)(sqlite3_vtab *pVTab);
5735  int (*xOpen)(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor);
5736  int (*xClose)(sqlite3_vtab_cursor*);
5737  int (*xFilter)(sqlite3_vtab_cursor*, int idxNum, const char *idxStr,
5738                int argc, sqlite3_value **argv);
5739  int (*xNext)(sqlite3_vtab_cursor*);
5740  int (*xEof)(sqlite3_vtab_cursor*);
5741  int (*xColumn)(sqlite3_vtab_cursor*, sqlite3_context*, int);
5742  int (*xRowid)(sqlite3_vtab_cursor*, sqlite3_int64 *pRowid);
5743  int (*xUpdate)(sqlite3_vtab *, int, sqlite3_value **, sqlite3_int64 *);
5744  int (*xBegin)(sqlite3_vtab *pVTab);
5745  int (*xSync)(sqlite3_vtab *pVTab);
5746  int (*xCommit)(sqlite3_vtab *pVTab);
5747  int (*xRollback)(sqlite3_vtab *pVTab);
5748  int (*xFindFunction)(sqlite3_vtab *pVtab, int nArg, const char *zName,
5749                       void (**pxFunc)(sqlite3_context*,int,sqlite3_value**),
5750                       void **ppArg);
5751  int (*xRename)(sqlite3_vtab *pVtab, const char *zNew);
5752  /* The methods above are in version 1 of the sqlite_module object. Those
5753  ** below are for version 2 and greater. */
5754  int (*xSavepoint)(sqlite3_vtab *pVTab, int);
5755  int (*xRelease)(sqlite3_vtab *pVTab, int);
5756  int (*xRollbackTo)(sqlite3_vtab *pVTab, int);
5757};
5758
5759/*
5760** CAPI3REF: Virtual Table Indexing Information
5761** KEYWORDS: sqlite3_index_info
5762**
5763** The sqlite3_index_info structure and its substructures is used as part
5764** of the [virtual table] interface to
5765** pass information into and receive the reply from the [xBestIndex]
5766** method of a [virtual table module].  The fields under **Inputs** are the
5767** inputs to xBestIndex and are read-only.  xBestIndex inserts its
5768** results into the **Outputs** fields.
5769**
5770** ^(The aConstraint[] array records WHERE clause constraints of the form:
5771**
5772** <blockquote>column OP expr</blockquote>
5773**
5774** where OP is =, &lt;, &lt;=, &gt;, or &gt;=.)^  ^(The particular operator is
5775** stored in aConstraint[].op using one of the
5776** [SQLITE_INDEX_CONSTRAINT_EQ | SQLITE_INDEX_CONSTRAINT_ values].)^
5777** ^(The index of the column is stored in
5778** aConstraint[].iColumn.)^  ^(aConstraint[].usable is TRUE if the
5779** expr on the right-hand side can be evaluated (and thus the constraint
5780** is usable) and false if it cannot.)^
5781**
5782** ^The optimizer automatically inverts terms of the form "expr OP column"
5783** and makes other simplifications to the WHERE clause in an attempt to
5784** get as many WHERE clause terms into the form shown above as possible.
5785** ^The aConstraint[] array only reports WHERE clause terms that are
5786** relevant to the particular virtual table being queried.
5787**
5788** ^Information about the ORDER BY clause is stored in aOrderBy[].
5789** ^Each term of aOrderBy records a column of the ORDER BY clause.
5790**
5791** The [xBestIndex] method must fill aConstraintUsage[] with information
5792** about what parameters to pass to xFilter.  ^If argvIndex>0 then
5793** the right-hand side of the corresponding aConstraint[] is evaluated
5794** and becomes the argvIndex-th entry in argv.  ^(If aConstraintUsage[].omit
5795** is true, then the constraint is assumed to be fully handled by the
5796** virtual table and is not checked again by SQLite.)^
5797**
5798** ^The idxNum and idxPtr values are recorded and passed into the
5799** [xFilter] method.
5800** ^[sqlite3_free()] is used to free idxPtr if and only if
5801** needToFreeIdxPtr is true.
5802**
5803** ^The orderByConsumed means that output from [xFilter]/[xNext] will occur in
5804** the correct order to satisfy the ORDER BY clause so that no separate
5805** sorting step is required.
5806**
5807** ^The estimatedCost value is an estimate of the cost of a particular
5808** strategy. A cost of N indicates that the cost of the strategy is similar
5809** to a linear scan of an SQLite table with N rows. A cost of log(N)
5810** indicates that the expense of the operation is similar to that of a
5811** binary search on a unique indexed field of an SQLite table with N rows.
5812**
5813** ^The estimatedRows value is an estimate of the number of rows that
5814** will be returned by the strategy.
5815**
5816** IMPORTANT: The estimatedRows field was added to the sqlite3_index_info
5817** structure for SQLite version 3.8.2. If a virtual table extension is
5818** used with an SQLite version earlier than 3.8.2, the results of attempting
5819** to read or write the estimatedRows field are undefined (but are likely
5820** to included crashing the application). The estimatedRows field should
5821** therefore only be used if [sqlite3_libversion_number()] returns a
5822** value greater than or equal to 3008002.
5823*/
5824struct sqlite3_index_info {
5825  /* Inputs */
5826  int nConstraint;           /* Number of entries in aConstraint */
5827  struct sqlite3_index_constraint {
5828     int iColumn;              /* Column on left-hand side of constraint */
5829     unsigned char op;         /* Constraint operator */
5830     unsigned char usable;     /* True if this constraint is usable */
5831     int iTermOffset;          /* Used internally - xBestIndex should ignore */
5832  } *aConstraint;            /* Table of WHERE clause constraints */
5833  int nOrderBy;              /* Number of terms in the ORDER BY clause */
5834  struct sqlite3_index_orderby {
5835     int iColumn;              /* Column number */
5836     unsigned char desc;       /* True for DESC.  False for ASC. */
5837  } *aOrderBy;               /* The ORDER BY clause */
5838  /* Outputs */
5839  struct sqlite3_index_constraint_usage {
5840    int argvIndex;           /* if >0, constraint is part of argv to xFilter */
5841    unsigned char omit;      /* Do not code a test for this constraint */
5842  } *aConstraintUsage;
5843  int idxNum;                /* Number used to identify the index */
5844  char *idxStr;              /* String, possibly obtained from sqlite3_malloc */
5845  int needToFreeIdxStr;      /* Free idxStr using sqlite3_free() if true */
5846  int orderByConsumed;       /* True if output is already ordered */
5847  double estimatedCost;           /* Estimated cost of using this index */
5848  /* Fields below are only available in SQLite 3.8.2 and later */
5849  sqlite3_int64 estimatedRows;    /* Estimated number of rows returned */
5850};
5851
5852/*
5853** CAPI3REF: Virtual Table Constraint Operator Codes
5854**
5855** These macros defined the allowed values for the
5856** [sqlite3_index_info].aConstraint[].op field.  Each value represents
5857** an operator that is part of a constraint term in the wHERE clause of
5858** a query that uses a [virtual table].
5859*/
5860#define SQLITE_INDEX_CONSTRAINT_EQ    2
5861#define SQLITE_INDEX_CONSTRAINT_GT    4
5862#define SQLITE_INDEX_CONSTRAINT_LE    8
5863#define SQLITE_INDEX_CONSTRAINT_LT    16
5864#define SQLITE_INDEX_CONSTRAINT_GE    32
5865#define SQLITE_INDEX_CONSTRAINT_MATCH 64
5866
5867/*
5868** CAPI3REF: Register A Virtual Table Implementation
5869** METHOD: sqlite3
5870**
5871** ^These routines are used to register a new [virtual table module] name.
5872** ^Module names must be registered before
5873** creating a new [virtual table] using the module and before using a
5874** preexisting [virtual table] for the module.
5875**
5876** ^The module name is registered on the [database connection] specified
5877** by the first parameter.  ^The name of the module is given by the
5878** second parameter.  ^The third parameter is a pointer to
5879** the implementation of the [virtual table module].   ^The fourth
5880** parameter is an arbitrary client data pointer that is passed through
5881** into the [xCreate] and [xConnect] methods of the virtual table module
5882** when a new virtual table is be being created or reinitialized.
5883**
5884** ^The sqlite3_create_module_v2() interface has a fifth parameter which
5885** is a pointer to a destructor for the pClientData.  ^SQLite will
5886** invoke the destructor function (if it is not NULL) when SQLite
5887** no longer needs the pClientData pointer.  ^The destructor will also
5888** be invoked if the call to sqlite3_create_module_v2() fails.
5889** ^The sqlite3_create_module()
5890** interface is equivalent to sqlite3_create_module_v2() with a NULL
5891** destructor.
5892*/
5893SQLITE_API int SQLITE_STDCALL sqlite3_create_module(
5894  sqlite3 *db,               /* SQLite connection to register module with */
5895  const char *zName,         /* Name of the module */
5896  const sqlite3_module *p,   /* Methods for the module */
5897  void *pClientData          /* Client data for xCreate/xConnect */
5898);
5899SQLITE_API int SQLITE_STDCALL sqlite3_create_module_v2(
5900  sqlite3 *db,               /* SQLite connection to register module with */
5901  const char *zName,         /* Name of the module */
5902  const sqlite3_module *p,   /* Methods for the module */
5903  void *pClientData,         /* Client data for xCreate/xConnect */
5904  void(*xDestroy)(void*)     /* Module destructor function */
5905);
5906
5907/*
5908** CAPI3REF: Virtual Table Instance Object
5909** KEYWORDS: sqlite3_vtab
5910**
5911** Every [virtual table module] implementation uses a subclass
5912** of this object to describe a particular instance
5913** of the [virtual table].  Each subclass will
5914** be tailored to the specific needs of the module implementation.
5915** The purpose of this superclass is to define certain fields that are
5916** common to all module implementations.
5917**
5918** ^Virtual tables methods can set an error message by assigning a
5919** string obtained from [sqlite3_mprintf()] to zErrMsg.  The method should
5920** take care that any prior string is freed by a call to [sqlite3_free()]
5921** prior to assigning a new string to zErrMsg.  ^After the error message
5922** is delivered up to the client application, the string will be automatically
5923** freed by sqlite3_free() and the zErrMsg field will be zeroed.
5924*/
5925struct sqlite3_vtab {
5926  const sqlite3_module *pModule;  /* The module for this virtual table */
5927  int nRef;                       /* Number of open cursors */
5928  char *zErrMsg;                  /* Error message from sqlite3_mprintf() */
5929  /* Virtual table implementations will typically add additional fields */
5930};
5931
5932/*
5933** CAPI3REF: Virtual Table Cursor Object
5934** KEYWORDS: sqlite3_vtab_cursor {virtual table cursor}
5935**
5936** Every [virtual table module] implementation uses a subclass of the
5937** following structure to describe cursors that point into the
5938** [virtual table] and are used
5939** to loop through the virtual table.  Cursors are created using the
5940** [sqlite3_module.xOpen | xOpen] method of the module and are destroyed
5941** by the [sqlite3_module.xClose | xClose] method.  Cursors are used
5942** by the [xFilter], [xNext], [xEof], [xColumn], and [xRowid] methods
5943** of the module.  Each module implementation will define
5944** the content of a cursor structure to suit its own needs.
5945**
5946** This superclass exists in order to define fields of the cursor that
5947** are common to all implementations.
5948*/
5949struct sqlite3_vtab_cursor {
5950  sqlite3_vtab *pVtab;      /* Virtual table of this cursor */
5951  /* Virtual table implementations will typically add additional fields */
5952};
5953
5954/*
5955** CAPI3REF: Declare The Schema Of A Virtual Table
5956**
5957** ^The [xCreate] and [xConnect] methods of a
5958** [virtual table module] call this interface
5959** to declare the format (the names and datatypes of the columns) of
5960** the virtual tables they implement.
5961*/
5962SQLITE_API int SQLITE_STDCALL sqlite3_declare_vtab(sqlite3*, const char *zSQL);
5963
5964/*
5965** CAPI3REF: Overload A Function For A Virtual Table
5966** METHOD: sqlite3
5967**
5968** ^(Virtual tables can provide alternative implementations of functions
5969** using the [xFindFunction] method of the [virtual table module].
5970** But global versions of those functions
5971** must exist in order to be overloaded.)^
5972**
5973** ^(This API makes sure a global version of a function with a particular
5974** name and number of parameters exists.  If no such function exists
5975** before this API is called, a new function is created.)^  ^The implementation
5976** of the new function always causes an exception to be thrown.  So
5977** the new function is not good for anything by itself.  Its only
5978** purpose is to be a placeholder function that can be overloaded
5979** by a [virtual table].
5980*/
5981SQLITE_API int SQLITE_STDCALL sqlite3_overload_function(sqlite3*, const char *zFuncName, int nArg);
5982
5983/*
5984** The interface to the virtual-table mechanism defined above (back up
5985** to a comment remarkably similar to this one) is currently considered
5986** to be experimental.  The interface might change in incompatible ways.
5987** If this is a problem for you, do not use the interface at this time.
5988**
5989** When the virtual-table mechanism stabilizes, we will declare the
5990** interface fixed, support it indefinitely, and remove this comment.
5991*/
5992
5993/*
5994** CAPI3REF: A Handle To An Open BLOB
5995** KEYWORDS: {BLOB handle} {BLOB handles}
5996**
5997** An instance of this object represents an open BLOB on which
5998** [sqlite3_blob_open | incremental BLOB I/O] can be performed.
5999** ^Objects of this type are created by [sqlite3_blob_open()]
6000** and destroyed by [sqlite3_blob_close()].
6001** ^The [sqlite3_blob_read()] and [sqlite3_blob_write()] interfaces
6002** can be used to read or write small subsections of the BLOB.
6003** ^The [sqlite3_blob_bytes()] interface returns the size of the BLOB in bytes.
6004*/
6005typedef struct sqlite3_blob sqlite3_blob;
6006
6007/*
6008** CAPI3REF: Open A BLOB For Incremental I/O
6009** METHOD: sqlite3
6010** CONSTRUCTOR: sqlite3_blob
6011**
6012** ^(This interfaces opens a [BLOB handle | handle] to the BLOB located
6013** in row iRow, column zColumn, table zTable in database zDb;
6014** in other words, the same BLOB that would be selected by:
6015**
6016** <pre>
6017**     SELECT zColumn FROM zDb.zTable WHERE [rowid] = iRow;
6018** </pre>)^
6019**
6020** ^(Parameter zDb is not the filename that contains the database, but
6021** rather the symbolic name of the database. For attached databases, this is
6022** the name that appears after the AS keyword in the [ATTACH] statement.
6023** For the main database file, the database name is "main". For TEMP
6024** tables, the database name is "temp".)^
6025**
6026** ^If the flags parameter is non-zero, then the BLOB is opened for read
6027** and write access. ^If the flags parameter is zero, the BLOB is opened for
6028** read-only access.
6029**
6030** ^(On success, [SQLITE_OK] is returned and the new [BLOB handle] is stored
6031** in *ppBlob. Otherwise an [error code] is returned and, unless the error
6032** code is SQLITE_MISUSE, *ppBlob is set to NULL.)^ ^This means that, provided
6033** the API is not misused, it is always safe to call [sqlite3_blob_close()]
6034** on *ppBlob after this function it returns.
6035**
6036** This function fails with SQLITE_ERROR if any of the following are true:
6037** <ul>
6038**   <li> ^(Database zDb does not exist)^,
6039**   <li> ^(Table zTable does not exist within database zDb)^,
6040**   <li> ^(Table zTable is a WITHOUT ROWID table)^,
6041**   <li> ^(Column zColumn does not exist)^,
6042**   <li> ^(Row iRow is not present in the table)^,
6043**   <li> ^(The specified column of row iRow contains a value that is not
6044**         a TEXT or BLOB value)^,
6045**   <li> ^(Column zColumn is part of an index, PRIMARY KEY or UNIQUE
6046**         constraint and the blob is being opened for read/write access)^,
6047**   <li> ^([foreign key constraints | Foreign key constraints] are enabled,
6048**         column zColumn is part of a [child key] definition and the blob is
6049**         being opened for read/write access)^.
6050** </ul>
6051**
6052** ^Unless it returns SQLITE_MISUSE, this function sets the
6053** [database connection] error code and message accessible via
6054** [sqlite3_errcode()] and [sqlite3_errmsg()] and related functions.
6055**
6056**
6057** ^(If the row that a BLOB handle points to is modified by an
6058** [UPDATE], [DELETE], or by [ON CONFLICT] side-effects
6059** then the BLOB handle is marked as "expired".
6060** This is true if any column of the row is changed, even a column
6061** other than the one the BLOB handle is open on.)^
6062** ^Calls to [sqlite3_blob_read()] and [sqlite3_blob_write()] for
6063** an expired BLOB handle fail with a return code of [SQLITE_ABORT].
6064** ^(Changes written into a BLOB prior to the BLOB expiring are not
6065** rolled back by the expiration of the BLOB.  Such changes will eventually
6066** commit if the transaction continues to completion.)^
6067**
6068** ^Use the [sqlite3_blob_bytes()] interface to determine the size of
6069** the opened blob.  ^The size of a blob may not be changed by this
6070** interface.  Use the [UPDATE] SQL command to change the size of a
6071** blob.
6072**
6073** ^The [sqlite3_bind_zeroblob()] and [sqlite3_result_zeroblob()] interfaces
6074** and the built-in [zeroblob] SQL function may be used to create a
6075** zero-filled blob to read or write using the incremental-blob interface.
6076**
6077** To avoid a resource leak, every open [BLOB handle] should eventually
6078** be released by a call to [sqlite3_blob_close()].
6079*/
6080SQLITE_API int SQLITE_STDCALL sqlite3_blob_open(
6081  sqlite3*,
6082  const char *zDb,
6083  const char *zTable,
6084  const char *zColumn,
6085  sqlite3_int64 iRow,
6086  int flags,
6087  sqlite3_blob **ppBlob
6088);
6089
6090/*
6091** CAPI3REF: Move a BLOB Handle to a New Row
6092** METHOD: sqlite3_blob
6093**
6094** ^This function is used to move an existing blob handle so that it points
6095** to a different row of the same database table. ^The new row is identified
6096** by the rowid value passed as the second argument. Only the row can be
6097** changed. ^The database, table and column on which the blob handle is open
6098** remain the same. Moving an existing blob handle to a new row can be
6099** faster than closing the existing handle and opening a new one.
6100**
6101** ^(The new row must meet the same criteria as for [sqlite3_blob_open()] -
6102** it must exist and there must be either a blob or text value stored in
6103** the nominated column.)^ ^If the new row is not present in the table, or if
6104** it does not contain a blob or text value, or if another error occurs, an
6105** SQLite error code is returned and the blob handle is considered aborted.
6106** ^All subsequent calls to [sqlite3_blob_read()], [sqlite3_blob_write()] or
6107** [sqlite3_blob_reopen()] on an aborted blob handle immediately return
6108** SQLITE_ABORT. ^Calling [sqlite3_blob_bytes()] on an aborted blob handle
6109** always returns zero.
6110**
6111** ^This function sets the database handle error code and message.
6112*/
6113SQLITE_API int SQLITE_STDCALL sqlite3_blob_reopen(sqlite3_blob *, sqlite3_int64);
6114
6115/*
6116** CAPI3REF: Close A BLOB Handle
6117** DESTRUCTOR: sqlite3_blob
6118**
6119** ^This function closes an open [BLOB handle]. ^(The BLOB handle is closed
6120** unconditionally.  Even if this routine returns an error code, the
6121** handle is still closed.)^
6122**
6123** ^If the blob handle being closed was opened for read-write access, and if
6124** the database is in auto-commit mode and there are no other open read-write
6125** blob handles or active write statements, the current transaction is
6126** committed. ^If an error occurs while committing the transaction, an error
6127** code is returned and the transaction rolled back.
6128**
6129** Calling this function with an argument that is not a NULL pointer or an
6130** open blob handle results in undefined behaviour. ^Calling this routine
6131** with a null pointer (such as would be returned by a failed call to
6132** [sqlite3_blob_open()]) is a harmless no-op. ^Otherwise, if this function
6133** is passed a valid open blob handle, the values returned by the
6134** sqlite3_errcode() and sqlite3_errmsg() functions are set before returning.
6135*/
6136SQLITE_API int SQLITE_STDCALL sqlite3_blob_close(sqlite3_blob *);
6137
6138/*
6139** CAPI3REF: Return The Size Of An Open BLOB
6140** METHOD: sqlite3_blob
6141**
6142** ^Returns the size in bytes of the BLOB accessible via the
6143** successfully opened [BLOB handle] in its only argument.  ^The
6144** incremental blob I/O routines can only read or overwriting existing
6145** blob content; they cannot change the size of a blob.
6146**
6147** This routine only works on a [BLOB handle] which has been created
6148** by a prior successful call to [sqlite3_blob_open()] and which has not
6149** been closed by [sqlite3_blob_close()].  Passing any other pointer in
6150** to this routine results in undefined and probably undesirable behavior.
6151*/
6152SQLITE_API int SQLITE_STDCALL sqlite3_blob_bytes(sqlite3_blob *);
6153
6154/*
6155** CAPI3REF: Read Data From A BLOB Incrementally
6156** METHOD: sqlite3_blob
6157**
6158** ^(This function is used to read data from an open [BLOB handle] into a
6159** caller-supplied buffer. N bytes of data are copied into buffer Z
6160** from the open BLOB, starting at offset iOffset.)^
6161**
6162** ^If offset iOffset is less than N bytes from the end of the BLOB,
6163** [SQLITE_ERROR] is returned and no data is read.  ^If N or iOffset is
6164** less than zero, [SQLITE_ERROR] is returned and no data is read.
6165** ^The size of the blob (and hence the maximum value of N+iOffset)
6166** can be determined using the [sqlite3_blob_bytes()] interface.
6167**
6168** ^An attempt to read from an expired [BLOB handle] fails with an
6169** error code of [SQLITE_ABORT].
6170**
6171** ^(On success, sqlite3_blob_read() returns SQLITE_OK.
6172** Otherwise, an [error code] or an [extended error code] is returned.)^
6173**
6174** This routine only works on a [BLOB handle] which has been created
6175** by a prior successful call to [sqlite3_blob_open()] and which has not
6176** been closed by [sqlite3_blob_close()].  Passing any other pointer in
6177** to this routine results in undefined and probably undesirable behavior.
6178**
6179** See also: [sqlite3_blob_write()].
6180*/
6181SQLITE_API int SQLITE_STDCALL sqlite3_blob_read(sqlite3_blob *, void *Z, int N, int iOffset);
6182
6183/*
6184** CAPI3REF: Write Data Into A BLOB Incrementally
6185** METHOD: sqlite3_blob
6186**
6187** ^(This function is used to write data into an open [BLOB handle] from a
6188** caller-supplied buffer. N bytes of data are copied from the buffer Z
6189** into the open BLOB, starting at offset iOffset.)^
6190**
6191** ^(On success, sqlite3_blob_write() returns SQLITE_OK.
6192** Otherwise, an  [error code] or an [extended error code] is returned.)^
6193** ^Unless SQLITE_MISUSE is returned, this function sets the
6194** [database connection] error code and message accessible via
6195** [sqlite3_errcode()] and [sqlite3_errmsg()] and related functions.
6196**
6197** ^If the [BLOB handle] passed as the first argument was not opened for
6198** writing (the flags parameter to [sqlite3_blob_open()] was zero),
6199** this function returns [SQLITE_READONLY].
6200**
6201** This function may only modify the contents of the BLOB; it is
6202** not possible to increase the size of a BLOB using this API.
6203** ^If offset iOffset is less than N bytes from the end of the BLOB,
6204** [SQLITE_ERROR] is returned and no data is written. The size of the
6205** BLOB (and hence the maximum value of N+iOffset) can be determined
6206** using the [sqlite3_blob_bytes()] interface. ^If N or iOffset are less
6207** than zero [SQLITE_ERROR] is returned and no data is written.
6208**
6209** ^An attempt to write to an expired [BLOB handle] fails with an
6210** error code of [SQLITE_ABORT].  ^Writes to the BLOB that occurred
6211** before the [BLOB handle] expired are not rolled back by the
6212** expiration of the handle, though of course those changes might
6213** have been overwritten by the statement that expired the BLOB handle
6214** or by other independent statements.
6215**
6216** This routine only works on a [BLOB handle] which has been created
6217** by a prior successful call to [sqlite3_blob_open()] and which has not
6218** been closed by [sqlite3_blob_close()].  Passing any other pointer in
6219** to this routine results in undefined and probably undesirable behavior.
6220**
6221** See also: [sqlite3_blob_read()].
6222*/
6223SQLITE_API int SQLITE_STDCALL sqlite3_blob_write(sqlite3_blob *, const void *z, int n, int iOffset);
6224
6225/*
6226** CAPI3REF: Virtual File System Objects
6227**
6228** A virtual filesystem (VFS) is an [sqlite3_vfs] object
6229** that SQLite uses to interact
6230** with the underlying operating system.  Most SQLite builds come with a
6231** single default VFS that is appropriate for the host computer.
6232** New VFSes can be registered and existing VFSes can be unregistered.
6233** The following interfaces are provided.
6234**
6235** ^The sqlite3_vfs_find() interface returns a pointer to a VFS given its name.
6236** ^Names are case sensitive.
6237** ^Names are zero-terminated UTF-8 strings.
6238** ^If there is no match, a NULL pointer is returned.
6239** ^If zVfsName is NULL then the default VFS is returned.
6240**
6241** ^New VFSes are registered with sqlite3_vfs_register().
6242** ^Each new VFS becomes the default VFS if the makeDflt flag is set.
6243** ^The same VFS can be registered multiple times without injury.
6244** ^To make an existing VFS into the default VFS, register it again
6245** with the makeDflt flag set.  If two different VFSes with the
6246** same name are registered, the behavior is undefined.  If a
6247** VFS is registered with a name that is NULL or an empty string,
6248** then the behavior is undefined.
6249**
6250** ^Unregister a VFS with the sqlite3_vfs_unregister() interface.
6251** ^(If the default VFS is unregistered, another VFS is chosen as
6252** the default.  The choice for the new VFS is arbitrary.)^
6253*/
6254SQLITE_API sqlite3_vfs *SQLITE_STDCALL sqlite3_vfs_find(const char *zVfsName);
6255SQLITE_API int SQLITE_STDCALL sqlite3_vfs_register(sqlite3_vfs*, int makeDflt);
6256SQLITE_API int SQLITE_STDCALL sqlite3_vfs_unregister(sqlite3_vfs*);
6257
6258/*
6259** CAPI3REF: Mutexes
6260**
6261** The SQLite core uses these routines for thread
6262** synchronization. Though they are intended for internal
6263** use by SQLite, code that links against SQLite is
6264** permitted to use any of these routines.
6265**
6266** The SQLite source code contains multiple implementations
6267** of these mutex routines.  An appropriate implementation
6268** is selected automatically at compile-time.  The following
6269** implementations are available in the SQLite core:
6270**
6271** <ul>
6272** <li>   SQLITE_MUTEX_PTHREADS
6273** <li>   SQLITE_MUTEX_W32
6274** <li>   SQLITE_MUTEX_NOOP
6275** </ul>
6276**
6277** The SQLITE_MUTEX_NOOP implementation is a set of routines
6278** that does no real locking and is appropriate for use in
6279** a single-threaded application.  The SQLITE_MUTEX_PTHREADS and
6280** SQLITE_MUTEX_W32 implementations are appropriate for use on Unix
6281** and Windows.
6282**
6283** If SQLite is compiled with the SQLITE_MUTEX_APPDEF preprocessor
6284** macro defined (with "-DSQLITE_MUTEX_APPDEF=1"), then no mutex
6285** implementation is included with the library. In this case the
6286** application must supply a custom mutex implementation using the
6287** [SQLITE_CONFIG_MUTEX] option of the sqlite3_config() function
6288** before calling sqlite3_initialize() or any other public sqlite3_
6289** function that calls sqlite3_initialize().
6290**
6291** ^The sqlite3_mutex_alloc() routine allocates a new
6292** mutex and returns a pointer to it. ^The sqlite3_mutex_alloc()
6293** routine returns NULL if it is unable to allocate the requested
6294** mutex.  The argument to sqlite3_mutex_alloc() must one of these
6295** integer constants:
6296**
6297** <ul>
6298** <li>  SQLITE_MUTEX_FAST
6299** <li>  SQLITE_MUTEX_RECURSIVE
6300** <li>  SQLITE_MUTEX_STATIC_MASTER
6301** <li>  SQLITE_MUTEX_STATIC_MEM
6302** <li>  SQLITE_MUTEX_STATIC_OPEN
6303** <li>  SQLITE_MUTEX_STATIC_PRNG
6304** <li>  SQLITE_MUTEX_STATIC_LRU
6305** <li>  SQLITE_MUTEX_STATIC_PMEM
6306** <li>  SQLITE_MUTEX_STATIC_APP1
6307** <li>  SQLITE_MUTEX_STATIC_APP2
6308** <li>  SQLITE_MUTEX_STATIC_APP3
6309** </ul>
6310**
6311** ^The first two constants (SQLITE_MUTEX_FAST and SQLITE_MUTEX_RECURSIVE)
6312** cause sqlite3_mutex_alloc() to create
6313** a new mutex.  ^The new mutex is recursive when SQLITE_MUTEX_RECURSIVE
6314** is used but not necessarily so when SQLITE_MUTEX_FAST is used.
6315** The mutex implementation does not need to make a distinction
6316** between SQLITE_MUTEX_RECURSIVE and SQLITE_MUTEX_FAST if it does
6317** not want to.  SQLite will only request a recursive mutex in
6318** cases where it really needs one.  If a faster non-recursive mutex
6319** implementation is available on the host platform, the mutex subsystem
6320** might return such a mutex in response to SQLITE_MUTEX_FAST.
6321**
6322** ^The other allowed parameters to sqlite3_mutex_alloc() (anything other
6323** than SQLITE_MUTEX_FAST and SQLITE_MUTEX_RECURSIVE) each return
6324** a pointer to a static preexisting mutex.  ^Nine static mutexes are
6325** used by the current version of SQLite.  Future versions of SQLite
6326** may add additional static mutexes.  Static mutexes are for internal
6327** use by SQLite only.  Applications that use SQLite mutexes should
6328** use only the dynamic mutexes returned by SQLITE_MUTEX_FAST or
6329** SQLITE_MUTEX_RECURSIVE.
6330**
6331** ^Note that if one of the dynamic mutex parameters (SQLITE_MUTEX_FAST
6332** or SQLITE_MUTEX_RECURSIVE) is used then sqlite3_mutex_alloc()
6333** returns a different mutex on every call.  ^For the static
6334** mutex types, the same mutex is returned on every call that has
6335** the same type number.
6336**
6337** ^The sqlite3_mutex_free() routine deallocates a previously
6338** allocated dynamic mutex.  Attempting to deallocate a static
6339** mutex results in undefined behavior.
6340**
6341** ^The sqlite3_mutex_enter() and sqlite3_mutex_try() routines attempt
6342** to enter a mutex.  ^If another thread is already within the mutex,
6343** sqlite3_mutex_enter() will block and sqlite3_mutex_try() will return
6344** SQLITE_BUSY.  ^The sqlite3_mutex_try() interface returns [SQLITE_OK]
6345** upon successful entry.  ^(Mutexes created using
6346** SQLITE_MUTEX_RECURSIVE can be entered multiple times by the same thread.
6347** In such cases, the
6348** mutex must be exited an equal number of times before another thread
6349** can enter.)^  If the same thread tries to enter any mutex other
6350** than an SQLITE_MUTEX_RECURSIVE more than once, the behavior is undefined.
6351**
6352** ^(Some systems (for example, Windows 95) do not support the operation
6353** implemented by sqlite3_mutex_try().  On those systems, sqlite3_mutex_try()
6354** will always return SQLITE_BUSY. The SQLite core only ever uses
6355** sqlite3_mutex_try() as an optimization so this is acceptable
6356** behavior.)^
6357**
6358** ^The sqlite3_mutex_leave() routine exits a mutex that was
6359** previously entered by the same thread.   The behavior
6360** is undefined if the mutex is not currently entered by the
6361** calling thread or is not currently allocated.
6362**
6363** ^If the argument to sqlite3_mutex_enter(), sqlite3_mutex_try(), or
6364** sqlite3_mutex_leave() is a NULL pointer, then all three routines
6365** behave as no-ops.
6366**
6367** See also: [sqlite3_mutex_held()] and [sqlite3_mutex_notheld()].
6368*/
6369SQLITE_API sqlite3_mutex *SQLITE_STDCALL sqlite3_mutex_alloc(int);
6370SQLITE_API void SQLITE_STDCALL sqlite3_mutex_free(sqlite3_mutex*);
6371SQLITE_API void SQLITE_STDCALL sqlite3_mutex_enter(sqlite3_mutex*);
6372SQLITE_API int SQLITE_STDCALL sqlite3_mutex_try(sqlite3_mutex*);
6373SQLITE_API void SQLITE_STDCALL sqlite3_mutex_leave(sqlite3_mutex*);
6374
6375/*
6376** CAPI3REF: Mutex Methods Object
6377**
6378** An instance of this structure defines the low-level routines
6379** used to allocate and use mutexes.
6380**
6381** Usually, the default mutex implementations provided by SQLite are
6382** sufficient, however the application has the option of substituting a custom
6383** implementation for specialized deployments or systems for which SQLite
6384** does not provide a suitable implementation. In this case, the application
6385** creates and populates an instance of this structure to pass
6386** to sqlite3_config() along with the [SQLITE_CONFIG_MUTEX] option.
6387** Additionally, an instance of this structure can be used as an
6388** output variable when querying the system for the current mutex
6389** implementation, using the [SQLITE_CONFIG_GETMUTEX] option.
6390**
6391** ^The xMutexInit method defined by this structure is invoked as
6392** part of system initialization by the sqlite3_initialize() function.
6393** ^The xMutexInit routine is called by SQLite exactly once for each
6394** effective call to [sqlite3_initialize()].
6395**
6396** ^The xMutexEnd method defined by this structure is invoked as
6397** part of system shutdown by the sqlite3_shutdown() function. The
6398** implementation of this method is expected to release all outstanding
6399** resources obtained by the mutex methods implementation, especially
6400** those obtained by the xMutexInit method.  ^The xMutexEnd()
6401** interface is invoked exactly once for each call to [sqlite3_shutdown()].
6402**
6403** ^(The remaining seven methods defined by this structure (xMutexAlloc,
6404** xMutexFree, xMutexEnter, xMutexTry, xMutexLeave, xMutexHeld and
6405** xMutexNotheld) implement the following interfaces (respectively):
6406**
6407** <ul>
6408**   <li>  [sqlite3_mutex_alloc()] </li>
6409**   <li>  [sqlite3_mutex_free()] </li>
6410**   <li>  [sqlite3_mutex_enter()] </li>
6411**   <li>  [sqlite3_mutex_try()] </li>
6412**   <li>  [sqlite3_mutex_leave()] </li>
6413**   <li>  [sqlite3_mutex_held()] </li>
6414**   <li>  [sqlite3_mutex_notheld()] </li>
6415** </ul>)^
6416**
6417** The only difference is that the public sqlite3_XXX functions enumerated
6418** above silently ignore any invocations that pass a NULL pointer instead
6419** of a valid mutex handle. The implementations of the methods defined
6420** by this structure are not required to handle this case, the results
6421** of passing a NULL pointer instead of a valid mutex handle are undefined
6422** (i.e. it is acceptable to provide an implementation that segfaults if
6423** it is passed a NULL pointer).
6424**
6425** The xMutexInit() method must be threadsafe.  It must be harmless to
6426** invoke xMutexInit() multiple times within the same process and without
6427** intervening calls to xMutexEnd().  Second and subsequent calls to
6428** xMutexInit() must be no-ops.
6429**
6430** xMutexInit() must not use SQLite memory allocation ([sqlite3_malloc()]
6431** and its associates).  Similarly, xMutexAlloc() must not use SQLite memory
6432** allocation for a static mutex.  ^However xMutexAlloc() may use SQLite
6433** memory allocation for a fast or recursive mutex.
6434**
6435** ^SQLite will invoke the xMutexEnd() method when [sqlite3_shutdown()] is
6436** called, but only if the prior call to xMutexInit returned SQLITE_OK.
6437** If xMutexInit fails in any way, it is expected to clean up after itself
6438** prior to returning.
6439*/
6440typedef struct sqlite3_mutex_methods sqlite3_mutex_methods;
6441struct sqlite3_mutex_methods {
6442  int (*xMutexInit)(void);
6443  int (*xMutexEnd)(void);
6444  sqlite3_mutex *(*xMutexAlloc)(int);
6445  void (*xMutexFree)(sqlite3_mutex *);
6446  void (*xMutexEnter)(sqlite3_mutex *);
6447  int (*xMutexTry)(sqlite3_mutex *);
6448  void (*xMutexLeave)(sqlite3_mutex *);
6449  int (*xMutexHeld)(sqlite3_mutex *);
6450  int (*xMutexNotheld)(sqlite3_mutex *);
6451};
6452
6453/*
6454** CAPI3REF: Mutex Verification Routines
6455**
6456** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routines
6457** are intended for use inside assert() statements.  The SQLite core
6458** never uses these routines except inside an assert() and applications
6459** are advised to follow the lead of the core.  The SQLite core only
6460** provides implementations for these routines when it is compiled
6461** with the SQLITE_DEBUG flag.  External mutex implementations
6462** are only required to provide these routines if SQLITE_DEBUG is
6463** defined and if NDEBUG is not defined.
6464**
6465** These routines should return true if the mutex in their argument
6466** is held or not held, respectively, by the calling thread.
6467**
6468** The implementation is not required to provide versions of these
6469** routines that actually work. If the implementation does not provide working
6470** versions of these routines, it should at least provide stubs that always
6471** return true so that one does not get spurious assertion failures.
6472**
6473** If the argument to sqlite3_mutex_held() is a NULL pointer then
6474** the routine should return 1.   This seems counter-intuitive since
6475** clearly the mutex cannot be held if it does not exist.  But
6476** the reason the mutex does not exist is because the build is not
6477** using mutexes.  And we do not want the assert() containing the
6478** call to sqlite3_mutex_held() to fail, so a non-zero return is
6479** the appropriate thing to do.  The sqlite3_mutex_notheld()
6480** interface should also return 1 when given a NULL pointer.
6481*/
6482#ifndef NDEBUG
6483SQLITE_API int SQLITE_STDCALL sqlite3_mutex_held(sqlite3_mutex*);
6484SQLITE_API int SQLITE_STDCALL sqlite3_mutex_notheld(sqlite3_mutex*);
6485#endif
6486
6487/*
6488** CAPI3REF: Mutex Types
6489**
6490** The [sqlite3_mutex_alloc()] interface takes a single argument
6491** which is one of these integer constants.
6492**
6493** The set of static mutexes may change from one SQLite release to the
6494** next.  Applications that override the built-in mutex logic must be
6495** prepared to accommodate additional static mutexes.
6496*/
6497#define SQLITE_MUTEX_FAST             0
6498#define SQLITE_MUTEX_RECURSIVE        1
6499#define SQLITE_MUTEX_STATIC_MASTER    2
6500#define SQLITE_MUTEX_STATIC_MEM       3  /* sqlite3_malloc() */
6501#define SQLITE_MUTEX_STATIC_MEM2      4  /* NOT USED */
6502#define SQLITE_MUTEX_STATIC_OPEN      4  /* sqlite3BtreeOpen() */
6503#define SQLITE_MUTEX_STATIC_PRNG      5  /* sqlite3_random() */
6504#define SQLITE_MUTEX_STATIC_LRU       6  /* lru page list */
6505#define SQLITE_MUTEX_STATIC_LRU2      7  /* NOT USED */
6506#define SQLITE_MUTEX_STATIC_PMEM      7  /* sqlite3PageMalloc() */
6507#define SQLITE_MUTEX_STATIC_APP1      8  /* For use by application */
6508#define SQLITE_MUTEX_STATIC_APP2      9  /* For use by application */
6509#define SQLITE_MUTEX_STATIC_APP3     10  /* For use by application */
6510#define SQLITE_MUTEX_STATIC_VFS1     11  /* For use by built-in VFS */
6511#define SQLITE_MUTEX_STATIC_VFS2     12  /* For use by extension VFS */
6512#define SQLITE_MUTEX_STATIC_VFS3     13  /* For use by application VFS */
6513
6514/*
6515** CAPI3REF: Retrieve the mutex for a database connection
6516** METHOD: sqlite3
6517**
6518** ^This interface returns a pointer the [sqlite3_mutex] object that
6519** serializes access to the [database connection] given in the argument
6520** when the [threading mode] is Serialized.
6521** ^If the [threading mode] is Single-thread or Multi-thread then this
6522** routine returns a NULL pointer.
6523*/
6524SQLITE_API sqlite3_mutex *SQLITE_STDCALL sqlite3_db_mutex(sqlite3*);
6525
6526/*
6527** CAPI3REF: Low-Level Control Of Database Files
6528** METHOD: sqlite3
6529**
6530** ^The [sqlite3_file_control()] interface makes a direct call to the
6531** xFileControl method for the [sqlite3_io_methods] object associated
6532** with a particular database identified by the second argument. ^The
6533** name of the database is "main" for the main database or "temp" for the
6534** TEMP database, or the name that appears after the AS keyword for
6535** databases that are added using the [ATTACH] SQL command.
6536** ^A NULL pointer can be used in place of "main" to refer to the
6537** main database file.
6538** ^The third and fourth parameters to this routine
6539** are passed directly through to the second and third parameters of
6540** the xFileControl method.  ^The return value of the xFileControl
6541** method becomes the return value of this routine.
6542**
6543** ^The SQLITE_FCNTL_FILE_POINTER value for the op parameter causes
6544** a pointer to the underlying [sqlite3_file] object to be written into
6545** the space pointed to by the 4th parameter.  ^The SQLITE_FCNTL_FILE_POINTER
6546** case is a short-circuit path which does not actually invoke the
6547** underlying sqlite3_io_methods.xFileControl method.
6548**
6549** ^If the second parameter (zDbName) does not match the name of any
6550** open database file, then SQLITE_ERROR is returned.  ^This error
6551** code is not remembered and will not be recalled by [sqlite3_errcode()]
6552** or [sqlite3_errmsg()].  The underlying xFileControl method might
6553** also return SQLITE_ERROR.  There is no way to distinguish between
6554** an incorrect zDbName and an SQLITE_ERROR return from the underlying
6555** xFileControl method.
6556**
6557** See also: [SQLITE_FCNTL_LOCKSTATE]
6558*/
6559SQLITE_API int SQLITE_STDCALL sqlite3_file_control(sqlite3*, const char *zDbName, int op, void*);
6560
6561/*
6562** CAPI3REF: Testing Interface
6563**
6564** ^The sqlite3_test_control() interface is used to read out internal
6565** state of SQLite and to inject faults into SQLite for testing
6566** purposes.  ^The first parameter is an operation code that determines
6567** the number, meaning, and operation of all subsequent parameters.
6568**
6569** This interface is not for use by applications.  It exists solely
6570** for verifying the correct operation of the SQLite library.  Depending
6571** on how the SQLite library is compiled, this interface might not exist.
6572**
6573** The details of the operation codes, their meanings, the parameters
6574** they take, and what they do are all subject to change without notice.
6575** Unlike most of the SQLite API, this function is not guaranteed to
6576** operate consistently from one release to the next.
6577*/
6578SQLITE_API int SQLITE_CDECL sqlite3_test_control(int op, ...);
6579
6580/*
6581** CAPI3REF: Testing Interface Operation Codes
6582**
6583** These constants are the valid operation code parameters used
6584** as the first argument to [sqlite3_test_control()].
6585**
6586** These parameters and their meanings are subject to change
6587** without notice.  These values are for testing purposes only.
6588** Applications should not use any of these parameters or the
6589** [sqlite3_test_control()] interface.
6590*/
6591#define SQLITE_TESTCTRL_FIRST                    5
6592#define SQLITE_TESTCTRL_PRNG_SAVE                5
6593#define SQLITE_TESTCTRL_PRNG_RESTORE             6
6594#define SQLITE_TESTCTRL_PRNG_RESET               7
6595#define SQLITE_TESTCTRL_BITVEC_TEST              8
6596#define SQLITE_TESTCTRL_FAULT_INSTALL            9
6597#define SQLITE_TESTCTRL_BENIGN_MALLOC_HOOKS     10
6598#define SQLITE_TESTCTRL_PENDING_BYTE            11
6599#define SQLITE_TESTCTRL_ASSERT                  12
6600#define SQLITE_TESTCTRL_ALWAYS                  13
6601#define SQLITE_TESTCTRL_RESERVE                 14
6602#define SQLITE_TESTCTRL_OPTIMIZATIONS           15
6603#define SQLITE_TESTCTRL_ISKEYWORD               16
6604#define SQLITE_TESTCTRL_SCRATCHMALLOC           17
6605#define SQLITE_TESTCTRL_LOCALTIME_FAULT         18
6606#define SQLITE_TESTCTRL_EXPLAIN_STMT            19  /* NOT USED */
6607#define SQLITE_TESTCTRL_NEVER_CORRUPT           20
6608#define SQLITE_TESTCTRL_VDBE_COVERAGE           21
6609#define SQLITE_TESTCTRL_BYTEORDER               22
6610#define SQLITE_TESTCTRL_ISINIT                  23
6611#define SQLITE_TESTCTRL_SORTER_MMAP             24
6612#define SQLITE_TESTCTRL_IMPOSTER                25
6613#define SQLITE_TESTCTRL_LAST                    25
6614
6615/*
6616** CAPI3REF: SQLite Runtime Status
6617**
6618** ^These interfaces are used to retrieve runtime status information
6619** about the performance of SQLite, and optionally to reset various
6620** highwater marks.  ^The first argument is an integer code for
6621** the specific parameter to measure.  ^(Recognized integer codes
6622** are of the form [status parameters | SQLITE_STATUS_...].)^
6623** ^The current value of the parameter is returned into *pCurrent.
6624** ^The highest recorded value is returned in *pHighwater.  ^If the
6625** resetFlag is true, then the highest record value is reset after
6626** *pHighwater is written.  ^(Some parameters do not record the highest
6627** value.  For those parameters
6628** nothing is written into *pHighwater and the resetFlag is ignored.)^
6629** ^(Other parameters record only the highwater mark and not the current
6630** value.  For these latter parameters nothing is written into *pCurrent.)^
6631**
6632** ^The sqlite3_status() and sqlite3_status64() routines return
6633** SQLITE_OK on success and a non-zero [error code] on failure.
6634**
6635** If either the current value or the highwater mark is too large to
6636** be represented by a 32-bit integer, then the values returned by
6637** sqlite3_status() are undefined.
6638**
6639** See also: [sqlite3_db_status()]
6640*/
6641SQLITE_API int SQLITE_STDCALL sqlite3_status(int op, int *pCurrent, int *pHighwater, int resetFlag);
6642SQLITE_API int SQLITE_STDCALL sqlite3_status64(
6643  int op,
6644  sqlite3_int64 *pCurrent,
6645  sqlite3_int64 *pHighwater,
6646  int resetFlag
6647);
6648
6649
6650/*
6651** CAPI3REF: Status Parameters
6652** KEYWORDS: {status parameters}
6653**
6654** These integer constants designate various run-time status parameters
6655** that can be returned by [sqlite3_status()].
6656**
6657** <dl>
6658** [[SQLITE_STATUS_MEMORY_USED]] ^(<dt>SQLITE_STATUS_MEMORY_USED</dt>
6659** <dd>This parameter is the current amount of memory checked out
6660** using [sqlite3_malloc()], either directly or indirectly.  The
6661** figure includes calls made to [sqlite3_malloc()] by the application
6662** and internal memory usage by the SQLite library.  Scratch memory
6663** controlled by [SQLITE_CONFIG_SCRATCH] and auxiliary page-cache
6664** memory controlled by [SQLITE_CONFIG_PAGECACHE] is not included in
6665** this parameter.  The amount returned is the sum of the allocation
6666** sizes as reported by the xSize method in [sqlite3_mem_methods].</dd>)^
6667**
6668** [[SQLITE_STATUS_MALLOC_SIZE]] ^(<dt>SQLITE_STATUS_MALLOC_SIZE</dt>
6669** <dd>This parameter records the largest memory allocation request
6670** handed to [sqlite3_malloc()] or [sqlite3_realloc()] (or their
6671** internal equivalents).  Only the value returned in the
6672** *pHighwater parameter to [sqlite3_status()] is of interest.
6673** The value written into the *pCurrent parameter is undefined.</dd>)^
6674**
6675** [[SQLITE_STATUS_MALLOC_COUNT]] ^(<dt>SQLITE_STATUS_MALLOC_COUNT</dt>
6676** <dd>This parameter records the number of separate memory allocations
6677** currently checked out.</dd>)^
6678**
6679** [[SQLITE_STATUS_PAGECACHE_USED]] ^(<dt>SQLITE_STATUS_PAGECACHE_USED</dt>
6680** <dd>This parameter returns the number of pages used out of the
6681** [pagecache memory allocator] that was configured using
6682** [SQLITE_CONFIG_PAGECACHE].  The
6683** value returned is in pages, not in bytes.</dd>)^
6684**
6685** [[SQLITE_STATUS_PAGECACHE_OVERFLOW]]
6686** ^(<dt>SQLITE_STATUS_PAGECACHE_OVERFLOW</dt>
6687** <dd>This parameter returns the number of bytes of page cache
6688** allocation which could not be satisfied by the [SQLITE_CONFIG_PAGECACHE]
6689** buffer and where forced to overflow to [sqlite3_malloc()].  The
6690** returned value includes allocations that overflowed because they
6691** where too large (they were larger than the "sz" parameter to
6692** [SQLITE_CONFIG_PAGECACHE]) and allocations that overflowed because
6693** no space was left in the page cache.</dd>)^
6694**
6695** [[SQLITE_STATUS_PAGECACHE_SIZE]] ^(<dt>SQLITE_STATUS_PAGECACHE_SIZE</dt>
6696** <dd>This parameter records the largest memory allocation request
6697** handed to [pagecache memory allocator].  Only the value returned in the
6698** *pHighwater parameter to [sqlite3_status()] is of interest.
6699** The value written into the *pCurrent parameter is undefined.</dd>)^
6700**
6701** [[SQLITE_STATUS_SCRATCH_USED]] ^(<dt>SQLITE_STATUS_SCRATCH_USED</dt>
6702** <dd>This parameter returns the number of allocations used out of the
6703** [scratch memory allocator] configured using
6704** [SQLITE_CONFIG_SCRATCH].  The value returned is in allocations, not
6705** in bytes.  Since a single thread may only have one scratch allocation
6706** outstanding at time, this parameter also reports the number of threads
6707** using scratch memory at the same time.</dd>)^
6708**
6709** [[SQLITE_STATUS_SCRATCH_OVERFLOW]] ^(<dt>SQLITE_STATUS_SCRATCH_OVERFLOW</dt>
6710** <dd>This parameter returns the number of bytes of scratch memory
6711** allocation which could not be satisfied by the [SQLITE_CONFIG_SCRATCH]
6712** buffer and where forced to overflow to [sqlite3_malloc()].  The values
6713** returned include overflows because the requested allocation was too
6714** larger (that is, because the requested allocation was larger than the
6715** "sz" parameter to [SQLITE_CONFIG_SCRATCH]) and because no scratch buffer
6716** slots were available.
6717** </dd>)^
6718**
6719** [[SQLITE_STATUS_SCRATCH_SIZE]] ^(<dt>SQLITE_STATUS_SCRATCH_SIZE</dt>
6720** <dd>This parameter records the largest memory allocation request
6721** handed to [scratch memory allocator].  Only the value returned in the
6722** *pHighwater parameter to [sqlite3_status()] is of interest.
6723** The value written into the *pCurrent parameter is undefined.</dd>)^
6724**
6725** [[SQLITE_STATUS_PARSER_STACK]] ^(<dt>SQLITE_STATUS_PARSER_STACK</dt>
6726** <dd>This parameter records the deepest parser stack.  It is only
6727** meaningful if SQLite is compiled with [YYTRACKMAXSTACKDEPTH].</dd>)^
6728** </dl>
6729**
6730** New status parameters may be added from time to time.
6731*/
6732#define SQLITE_STATUS_MEMORY_USED          0
6733#define SQLITE_STATUS_PAGECACHE_USED       1
6734#define SQLITE_STATUS_PAGECACHE_OVERFLOW   2
6735#define SQLITE_STATUS_SCRATCH_USED         3
6736#define SQLITE_STATUS_SCRATCH_OVERFLOW     4
6737#define SQLITE_STATUS_MALLOC_SIZE          5
6738#define SQLITE_STATUS_PARSER_STACK         6
6739#define SQLITE_STATUS_PAGECACHE_SIZE       7
6740#define SQLITE_STATUS_SCRATCH_SIZE         8
6741#define SQLITE_STATUS_MALLOC_COUNT         9
6742
6743/*
6744** CAPI3REF: Database Connection Status
6745** METHOD: sqlite3
6746**
6747** ^This interface is used to retrieve runtime status information
6748** about a single [database connection].  ^The first argument is the
6749** database connection object to be interrogated.  ^The second argument
6750** is an integer constant, taken from the set of
6751** [SQLITE_DBSTATUS options], that
6752** determines the parameter to interrogate.  The set of
6753** [SQLITE_DBSTATUS options] is likely
6754** to grow in future releases of SQLite.
6755**
6756** ^The current value of the requested parameter is written into *pCur
6757** and the highest instantaneous value is written into *pHiwtr.  ^If
6758** the resetFlg is true, then the highest instantaneous value is
6759** reset back down to the current value.
6760**
6761** ^The sqlite3_db_status() routine returns SQLITE_OK on success and a
6762** non-zero [error code] on failure.
6763**
6764** See also: [sqlite3_status()] and [sqlite3_stmt_status()].
6765*/
6766SQLITE_API int SQLITE_STDCALL sqlite3_db_status(sqlite3*, int op, int *pCur, int *pHiwtr, int resetFlg);
6767
6768/*
6769** CAPI3REF: Status Parameters for database connections
6770** KEYWORDS: {SQLITE_DBSTATUS options}
6771**
6772** These constants are the available integer "verbs" that can be passed as
6773** the second argument to the [sqlite3_db_status()] interface.
6774**
6775** New verbs may be added in future releases of SQLite. Existing verbs
6776** might be discontinued. Applications should check the return code from
6777** [sqlite3_db_status()] to make sure that the call worked.
6778** The [sqlite3_db_status()] interface will return a non-zero error code
6779** if a discontinued or unsupported verb is invoked.
6780**
6781** <dl>
6782** [[SQLITE_DBSTATUS_LOOKASIDE_USED]] ^(<dt>SQLITE_DBSTATUS_LOOKASIDE_USED</dt>
6783** <dd>This parameter returns the number of lookaside memory slots currently
6784** checked out.</dd>)^
6785**
6786** [[SQLITE_DBSTATUS_LOOKASIDE_HIT]] ^(<dt>SQLITE_DBSTATUS_LOOKASIDE_HIT</dt>
6787** <dd>This parameter returns the number malloc attempts that were
6788** satisfied using lookaside memory. Only the high-water value is meaningful;
6789** the current value is always zero.)^
6790**
6791** [[SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE]]
6792** ^(<dt>SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE</dt>
6793** <dd>This parameter returns the number malloc attempts that might have
6794** been satisfied using lookaside memory but failed due to the amount of
6795** memory requested being larger than the lookaside slot size.
6796** Only the high-water value is meaningful;
6797** the current value is always zero.)^
6798**
6799** [[SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL]]
6800** ^(<dt>SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL</dt>
6801** <dd>This parameter returns the number malloc attempts that might have
6802** been satisfied using lookaside memory but failed due to all lookaside
6803** memory already being in use.
6804** Only the high-water value is meaningful;
6805** the current value is always zero.)^
6806**
6807** [[SQLITE_DBSTATUS_CACHE_USED]] ^(<dt>SQLITE_DBSTATUS_CACHE_USED</dt>
6808** <dd>This parameter returns the approximate number of bytes of heap
6809** memory used by all pager caches associated with the database connection.)^
6810** ^The highwater mark associated with SQLITE_DBSTATUS_CACHE_USED is always 0.
6811**
6812** [[SQLITE_DBSTATUS_SCHEMA_USED]] ^(<dt>SQLITE_DBSTATUS_SCHEMA_USED</dt>
6813** <dd>This parameter returns the approximate number of bytes of heap
6814** memory used to store the schema for all databases associated
6815** with the connection - main, temp, and any [ATTACH]-ed databases.)^
6816** ^The full amount of memory used by the schemas is reported, even if the
6817** schema memory is shared with other database connections due to
6818** [shared cache mode] being enabled.
6819** ^The highwater mark associated with SQLITE_DBSTATUS_SCHEMA_USED is always 0.
6820**
6821** [[SQLITE_DBSTATUS_STMT_USED]] ^(<dt>SQLITE_DBSTATUS_STMT_USED</dt>
6822** <dd>This parameter returns the approximate number of bytes of heap
6823** and lookaside memory used by all prepared statements associated with
6824** the database connection.)^
6825** ^The highwater mark associated with SQLITE_DBSTATUS_STMT_USED is always 0.
6826** </dd>
6827**
6828** [[SQLITE_DBSTATUS_CACHE_HIT]] ^(<dt>SQLITE_DBSTATUS_CACHE_HIT</dt>
6829** <dd>This parameter returns the number of pager cache hits that have
6830** occurred.)^ ^The highwater mark associated with SQLITE_DBSTATUS_CACHE_HIT
6831** is always 0.
6832** </dd>
6833**
6834** [[SQLITE_DBSTATUS_CACHE_MISS]] ^(<dt>SQLITE_DBSTATUS_CACHE_MISS</dt>
6835** <dd>This parameter returns the number of pager cache misses that have
6836** occurred.)^ ^The highwater mark associated with SQLITE_DBSTATUS_CACHE_MISS
6837** is always 0.
6838** </dd>
6839**
6840** [[SQLITE_DBSTATUS_CACHE_WRITE]] ^(<dt>SQLITE_DBSTATUS_CACHE_WRITE</dt>
6841** <dd>This parameter returns the number of dirty cache entries that have
6842** been written to disk. Specifically, the number of pages written to the
6843** wal file in wal mode databases, or the number of pages written to the
6844** database file in rollback mode databases. Any pages written as part of
6845** transaction rollback or database recovery operations are not included.
6846** If an IO or other error occurs while writing a page to disk, the effect
6847** on subsequent SQLITE_DBSTATUS_CACHE_WRITE requests is undefined.)^ ^The
6848** highwater mark associated with SQLITE_DBSTATUS_CACHE_WRITE is always 0.
6849** </dd>
6850**
6851** [[SQLITE_DBSTATUS_DEFERRED_FKS]] ^(<dt>SQLITE_DBSTATUS_DEFERRED_FKS</dt>
6852** <dd>This parameter returns zero for the current value if and only if
6853** all foreign key constraints (deferred or immediate) have been
6854** resolved.)^  ^The highwater mark is always 0.
6855** </dd>
6856** </dl>
6857*/
6858#define SQLITE_DBSTATUS_LOOKASIDE_USED       0
6859#define SQLITE_DBSTATUS_CACHE_USED           1
6860#define SQLITE_DBSTATUS_SCHEMA_USED          2
6861#define SQLITE_DBSTATUS_STMT_USED            3
6862#define SQLITE_DBSTATUS_LOOKASIDE_HIT        4
6863#define SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE  5
6864#define SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL  6
6865#define SQLITE_DBSTATUS_CACHE_HIT            7
6866#define SQLITE_DBSTATUS_CACHE_MISS           8
6867#define SQLITE_DBSTATUS_CACHE_WRITE          9
6868#define SQLITE_DBSTATUS_DEFERRED_FKS        10
6869#define SQLITE_DBSTATUS_MAX                 10   /* Largest defined DBSTATUS */
6870
6871
6872/*
6873** CAPI3REF: Prepared Statement Status
6874** METHOD: sqlite3_stmt
6875**
6876** ^(Each prepared statement maintains various
6877** [SQLITE_STMTSTATUS counters] that measure the number
6878** of times it has performed specific operations.)^  These counters can
6879** be used to monitor the performance characteristics of the prepared
6880** statements.  For example, if the number of table steps greatly exceeds
6881** the number of table searches or result rows, that would tend to indicate
6882** that the prepared statement is using a full table scan rather than
6883** an index.
6884**
6885** ^(This interface is used to retrieve and reset counter values from
6886** a [prepared statement].  The first argument is the prepared statement
6887** object to be interrogated.  The second argument
6888** is an integer code for a specific [SQLITE_STMTSTATUS counter]
6889** to be interrogated.)^
6890** ^The current value of the requested counter is returned.
6891** ^If the resetFlg is true, then the counter is reset to zero after this
6892** interface call returns.
6893**
6894** See also: [sqlite3_status()] and [sqlite3_db_status()].
6895*/
6896SQLITE_API int SQLITE_STDCALL sqlite3_stmt_status(sqlite3_stmt*, int op,int resetFlg);
6897
6898/*
6899** CAPI3REF: Status Parameters for prepared statements
6900** KEYWORDS: {SQLITE_STMTSTATUS counter} {SQLITE_STMTSTATUS counters}
6901**
6902** These preprocessor macros define integer codes that name counter
6903** values associated with the [sqlite3_stmt_status()] interface.
6904** The meanings of the various counters are as follows:
6905**
6906** <dl>
6907** [[SQLITE_STMTSTATUS_FULLSCAN_STEP]] <dt>SQLITE_STMTSTATUS_FULLSCAN_STEP</dt>
6908** <dd>^This is the number of times that SQLite has stepped forward in
6909** a table as part of a full table scan.  Large numbers for this counter
6910** may indicate opportunities for performance improvement through
6911** careful use of indices.</dd>
6912**
6913** [[SQLITE_STMTSTATUS_SORT]] <dt>SQLITE_STMTSTATUS_SORT</dt>
6914** <dd>^This is the number of sort operations that have occurred.
6915** A non-zero value in this counter may indicate an opportunity to
6916** improvement performance through careful use of indices.</dd>
6917**
6918** [[SQLITE_STMTSTATUS_AUTOINDEX]] <dt>SQLITE_STMTSTATUS_AUTOINDEX</dt>
6919** <dd>^This is the number of rows inserted into transient indices that
6920** were created automatically in order to help joins run faster.
6921** A non-zero value in this counter may indicate an opportunity to
6922** improvement performance by adding permanent indices that do not
6923** need to be reinitialized each time the statement is run.</dd>
6924**
6925** [[SQLITE_STMTSTATUS_VM_STEP]] <dt>SQLITE_STMTSTATUS_VM_STEP</dt>
6926** <dd>^This is the number of virtual machine operations executed
6927** by the prepared statement if that number is less than or equal
6928** to 2147483647.  The number of virtual machine operations can be
6929** used as a proxy for the total work done by the prepared statement.
6930** If the number of virtual machine operations exceeds 2147483647
6931** then the value returned by this statement status code is undefined.
6932** </dd>
6933** </dl>
6934*/
6935#define SQLITE_STMTSTATUS_FULLSCAN_STEP     1
6936#define SQLITE_STMTSTATUS_SORT              2
6937#define SQLITE_STMTSTATUS_AUTOINDEX         3
6938#define SQLITE_STMTSTATUS_VM_STEP           4
6939
6940/*
6941** CAPI3REF: Custom Page Cache Object
6942**
6943** The sqlite3_pcache type is opaque.  It is implemented by
6944** the pluggable module.  The SQLite core has no knowledge of
6945** its size or internal structure and never deals with the
6946** sqlite3_pcache object except by holding and passing pointers
6947** to the object.
6948**
6949** See [sqlite3_pcache_methods2] for additional information.
6950*/
6951typedef struct sqlite3_pcache sqlite3_pcache;
6952
6953/*
6954** CAPI3REF: Custom Page Cache Object
6955**
6956** The sqlite3_pcache_page object represents a single page in the
6957** page cache.  The page cache will allocate instances of this
6958** object.  Various methods of the page cache use pointers to instances
6959** of this object as parameters or as their return value.
6960**
6961** See [sqlite3_pcache_methods2] for additional information.
6962*/
6963typedef struct sqlite3_pcache_page sqlite3_pcache_page;
6964struct sqlite3_pcache_page {
6965  void *pBuf;        /* The content of the page */
6966  void *pExtra;      /* Extra information associated with the page */
6967};
6968
6969/*
6970** CAPI3REF: Application Defined Page Cache.
6971** KEYWORDS: {page cache}
6972**
6973** ^(The [sqlite3_config]([SQLITE_CONFIG_PCACHE2], ...) interface can
6974** register an alternative page cache implementation by passing in an
6975** instance of the sqlite3_pcache_methods2 structure.)^
6976** In many applications, most of the heap memory allocated by
6977** SQLite is used for the page cache.
6978** By implementing a
6979** custom page cache using this API, an application can better control
6980** the amount of memory consumed by SQLite, the way in which
6981** that memory is allocated and released, and the policies used to
6982** determine exactly which parts of a database file are cached and for
6983** how long.
6984**
6985** The alternative page cache mechanism is an
6986** extreme measure that is only needed by the most demanding applications.
6987** The built-in page cache is recommended for most uses.
6988**
6989** ^(The contents of the sqlite3_pcache_methods2 structure are copied to an
6990** internal buffer by SQLite within the call to [sqlite3_config].  Hence
6991** the application may discard the parameter after the call to
6992** [sqlite3_config()] returns.)^
6993**
6994** [[the xInit() page cache method]]
6995** ^(The xInit() method is called once for each effective
6996** call to [sqlite3_initialize()])^
6997** (usually only once during the lifetime of the process). ^(The xInit()
6998** method is passed a copy of the sqlite3_pcache_methods2.pArg value.)^
6999** The intent of the xInit() method is to set up global data structures
7000** required by the custom page cache implementation.
7001** ^(If the xInit() method is NULL, then the
7002** built-in default page cache is used instead of the application defined
7003** page cache.)^
7004**
7005** [[the xShutdown() page cache method]]
7006** ^The xShutdown() method is called by [sqlite3_shutdown()].
7007** It can be used to clean up
7008** any outstanding resources before process shutdown, if required.
7009** ^The xShutdown() method may be NULL.
7010**
7011** ^SQLite automatically serializes calls to the xInit method,
7012** so the xInit method need not be threadsafe.  ^The
7013** xShutdown method is only called from [sqlite3_shutdown()] so it does
7014** not need to be threadsafe either.  All other methods must be threadsafe
7015** in multithreaded applications.
7016**
7017** ^SQLite will never invoke xInit() more than once without an intervening
7018** call to xShutdown().
7019**
7020** [[the xCreate() page cache methods]]
7021** ^SQLite invokes the xCreate() method to construct a new cache instance.
7022** SQLite will typically create one cache instance for each open database file,
7023** though this is not guaranteed. ^The
7024** first parameter, szPage, is the size in bytes of the pages that must
7025** be allocated by the cache.  ^szPage will always a power of two.  ^The
7026** second parameter szExtra is a number of bytes of extra storage
7027** associated with each page cache entry.  ^The szExtra parameter will
7028** a number less than 250.  SQLite will use the
7029** extra szExtra bytes on each page to store metadata about the underlying
7030** database page on disk.  The value passed into szExtra depends
7031** on the SQLite version, the target platform, and how SQLite was compiled.
7032** ^The third argument to xCreate(), bPurgeable, is true if the cache being
7033** created will be used to cache database pages of a file stored on disk, or
7034** false if it is used for an in-memory database. The cache implementation
7035** does not have to do anything special based with the value of bPurgeable;
7036** it is purely advisory.  ^On a cache where bPurgeable is false, SQLite will
7037** never invoke xUnpin() except to deliberately delete a page.
7038** ^In other words, calls to xUnpin() on a cache with bPurgeable set to
7039** false will always have the "discard" flag set to true.
7040** ^Hence, a cache created with bPurgeable false will
7041** never contain any unpinned pages.
7042**
7043** [[the xCachesize() page cache method]]
7044** ^(The xCachesize() method may be called at any time by SQLite to set the
7045** suggested maximum cache-size (number of pages stored by) the cache
7046** instance passed as the first argument. This is the value configured using
7047** the SQLite "[PRAGMA cache_size]" command.)^  As with the bPurgeable
7048** parameter, the implementation is not required to do anything with this
7049** value; it is advisory only.
7050**
7051** [[the xPagecount() page cache methods]]
7052** The xPagecount() method must return the number of pages currently
7053** stored in the cache, both pinned and unpinned.
7054**
7055** [[the xFetch() page cache methods]]
7056** The xFetch() method locates a page in the cache and returns a pointer to
7057** an sqlite3_pcache_page object associated with that page, or a NULL pointer.
7058** The pBuf element of the returned sqlite3_pcache_page object will be a
7059** pointer to a buffer of szPage bytes used to store the content of a
7060** single database page.  The pExtra element of sqlite3_pcache_page will be
7061** a pointer to the szExtra bytes of extra storage that SQLite has requested
7062** for each entry in the page cache.
7063**
7064** The page to be fetched is determined by the key. ^The minimum key value
7065** is 1.  After it has been retrieved using xFetch, the page is considered
7066** to be "pinned".
7067**
7068** If the requested page is already in the page cache, then the page cache
7069** implementation must return a pointer to the page buffer with its content
7070** intact.  If the requested page is not already in the cache, then the
7071** cache implementation should use the value of the createFlag
7072** parameter to help it determined what action to take:
7073**
7074** <table border=1 width=85% align=center>
7075** <tr><th> createFlag <th> Behavior when page is not already in cache
7076** <tr><td> 0 <td> Do not allocate a new page.  Return NULL.
7077** <tr><td> 1 <td> Allocate a new page if it easy and convenient to do so.
7078**                 Otherwise return NULL.
7079** <tr><td> 2 <td> Make every effort to allocate a new page.  Only return
7080**                 NULL if allocating a new page is effectively impossible.
7081** </table>
7082**
7083** ^(SQLite will normally invoke xFetch() with a createFlag of 0 or 1.  SQLite
7084** will only use a createFlag of 2 after a prior call with a createFlag of 1
7085** failed.)^  In between the to xFetch() calls, SQLite may
7086** attempt to unpin one or more cache pages by spilling the content of
7087** pinned pages to disk and synching the operating system disk cache.
7088**
7089** [[the xUnpin() page cache method]]
7090** ^xUnpin() is called by SQLite with a pointer to a currently pinned page
7091** as its second argument.  If the third parameter, discard, is non-zero,
7092** then the page must be evicted from the cache.
7093** ^If the discard parameter is
7094** zero, then the page may be discarded or retained at the discretion of
7095** page cache implementation. ^The page cache implementation
7096** may choose to evict unpinned pages at any time.
7097**
7098** The cache must not perform any reference counting. A single
7099** call to xUnpin() unpins the page regardless of the number of prior calls
7100** to xFetch().
7101**
7102** [[the xRekey() page cache methods]]
7103** The xRekey() method is used to change the key value associated with the
7104** page passed as the second argument. If the cache
7105** previously contains an entry associated with newKey, it must be
7106** discarded. ^Any prior cache entry associated with newKey is guaranteed not
7107** to be pinned.
7108**
7109** When SQLite calls the xTruncate() method, the cache must discard all
7110** existing cache entries with page numbers (keys) greater than or equal
7111** to the value of the iLimit parameter passed to xTruncate(). If any
7112** of these pages are pinned, they are implicitly unpinned, meaning that
7113** they can be safely discarded.
7114**
7115** [[the xDestroy() page cache method]]
7116** ^The xDestroy() method is used to delete a cache allocated by xCreate().
7117** All resources associated with the specified cache should be freed. ^After
7118** calling the xDestroy() method, SQLite considers the [sqlite3_pcache*]
7119** handle invalid, and will not use it with any other sqlite3_pcache_methods2
7120** functions.
7121**
7122** [[the xShrink() page cache method]]
7123** ^SQLite invokes the xShrink() method when it wants the page cache to
7124** free up as much of heap memory as possible.  The page cache implementation
7125** is not obligated to free any memory, but well-behaved implementations should
7126** do their best.
7127*/
7128typedef struct sqlite3_pcache_methods2 sqlite3_pcache_methods2;
7129struct sqlite3_pcache_methods2 {
7130  int iVersion;
7131  void *pArg;
7132  int (*xInit)(void*);
7133  void (*xShutdown)(void*);
7134  sqlite3_pcache *(*xCreate)(int szPage, int szExtra, int bPurgeable);
7135  void (*xCachesize)(sqlite3_pcache*, int nCachesize);
7136  int (*xPagecount)(sqlite3_pcache*);
7137  sqlite3_pcache_page *(*xFetch)(sqlite3_pcache*, unsigned key, int createFlag);
7138  void (*xUnpin)(sqlite3_pcache*, sqlite3_pcache_page*, int discard);
7139  void (*xRekey)(sqlite3_pcache*, sqlite3_pcache_page*,
7140      unsigned oldKey, unsigned newKey);
7141  void (*xTruncate)(sqlite3_pcache*, unsigned iLimit);
7142  void (*xDestroy)(sqlite3_pcache*);
7143  void (*xShrink)(sqlite3_pcache*);
7144};
7145
7146/*
7147** This is the obsolete pcache_methods object that has now been replaced
7148** by sqlite3_pcache_methods2.  This object is not used by SQLite.  It is
7149** retained in the header file for backwards compatibility only.
7150*/
7151typedef struct sqlite3_pcache_methods sqlite3_pcache_methods;
7152struct sqlite3_pcache_methods {
7153  void *pArg;
7154  int (*xInit)(void*);
7155  void (*xShutdown)(void*);
7156  sqlite3_pcache *(*xCreate)(int szPage, int bPurgeable);
7157  void (*xCachesize)(sqlite3_pcache*, int nCachesize);
7158  int (*xPagecount)(sqlite3_pcache*);
7159  void *(*xFetch)(sqlite3_pcache*, unsigned key, int createFlag);
7160  void (*xUnpin)(sqlite3_pcache*, void*, int discard);
7161  void (*xRekey)(sqlite3_pcache*, void*, unsigned oldKey, unsigned newKey);
7162  void (*xTruncate)(sqlite3_pcache*, unsigned iLimit);
7163  void (*xDestroy)(sqlite3_pcache*);
7164};
7165
7166
7167/*
7168** CAPI3REF: Online Backup Object
7169**
7170** The sqlite3_backup object records state information about an ongoing
7171** online backup operation.  ^The sqlite3_backup object is created by
7172** a call to [sqlite3_backup_init()] and is destroyed by a call to
7173** [sqlite3_backup_finish()].
7174**
7175** See Also: [Using the SQLite Online Backup API]
7176*/
7177typedef struct sqlite3_backup sqlite3_backup;
7178
7179/*
7180** CAPI3REF: Online Backup API.
7181**
7182** The backup API copies the content of one database into another.
7183** It is useful either for creating backups of databases or
7184** for copying in-memory databases to or from persistent files.
7185**
7186** See Also: [Using the SQLite Online Backup API]
7187**
7188** ^SQLite holds a write transaction open on the destination database file
7189** for the duration of the backup operation.
7190** ^The source database is read-locked only while it is being read;
7191** it is not locked continuously for the entire backup operation.
7192** ^Thus, the backup may be performed on a live source database without
7193** preventing other database connections from
7194** reading or writing to the source database while the backup is underway.
7195**
7196** ^(To perform a backup operation:
7197**   <ol>
7198**     <li><b>sqlite3_backup_init()</b> is called once to initialize the
7199**         backup,
7200**     <li><b>sqlite3_backup_step()</b> is called one or more times to transfer
7201**         the data between the two databases, and finally
7202**     <li><b>sqlite3_backup_finish()</b> is called to release all resources
7203**         associated with the backup operation.
7204**   </ol>)^
7205** There should be exactly one call to sqlite3_backup_finish() for each
7206** successful call to sqlite3_backup_init().
7207**
7208** [[sqlite3_backup_init()]] <b>sqlite3_backup_init()</b>
7209**
7210** ^The D and N arguments to sqlite3_backup_init(D,N,S,M) are the
7211** [database connection] associated with the destination database
7212** and the database name, respectively.
7213** ^The database name is "main" for the main database, "temp" for the
7214** temporary database, or the name specified after the AS keyword in
7215** an [ATTACH] statement for an attached database.
7216** ^The S and M arguments passed to
7217** sqlite3_backup_init(D,N,S,M) identify the [database connection]
7218** and database name of the source database, respectively.
7219** ^The source and destination [database connections] (parameters S and D)
7220** must be different or else sqlite3_backup_init(D,N,S,M) will fail with
7221** an error.
7222**
7223** ^A call to sqlite3_backup_init() will fail, returning SQLITE_ERROR, if
7224** there is already a read or read-write transaction open on the
7225** destination database.
7226**
7227** ^If an error occurs within sqlite3_backup_init(D,N,S,M), then NULL is
7228** returned and an error code and error message are stored in the
7229** destination [database connection] D.
7230** ^The error code and message for the failed call to sqlite3_backup_init()
7231** can be retrieved using the [sqlite3_errcode()], [sqlite3_errmsg()], and/or
7232** [sqlite3_errmsg16()] functions.
7233** ^A successful call to sqlite3_backup_init() returns a pointer to an
7234** [sqlite3_backup] object.
7235** ^The [sqlite3_backup] object may be used with the sqlite3_backup_step() and
7236** sqlite3_backup_finish() functions to perform the specified backup
7237** operation.
7238**
7239** [[sqlite3_backup_step()]] <b>sqlite3_backup_step()</b>
7240**
7241** ^Function sqlite3_backup_step(B,N) will copy up to N pages between
7242** the source and destination databases specified by [sqlite3_backup] object B.
7243** ^If N is negative, all remaining source pages are copied.
7244** ^If sqlite3_backup_step(B,N) successfully copies N pages and there
7245** are still more pages to be copied, then the function returns [SQLITE_OK].
7246** ^If sqlite3_backup_step(B,N) successfully finishes copying all pages
7247** from source to destination, then it returns [SQLITE_DONE].
7248** ^If an error occurs while running sqlite3_backup_step(B,N),
7249** then an [error code] is returned. ^As well as [SQLITE_OK] and
7250** [SQLITE_DONE], a call to sqlite3_backup_step() may return [SQLITE_READONLY],
7251** [SQLITE_NOMEM], [SQLITE_BUSY], [SQLITE_LOCKED], or an
7252** [SQLITE_IOERR_ACCESS | SQLITE_IOERR_XXX] extended error code.
7253**
7254** ^(The sqlite3_backup_step() might return [SQLITE_READONLY] if
7255** <ol>
7256** <li> the destination database was opened read-only, or
7257** <li> the destination database is using write-ahead-log journaling
7258** and the destination and source page sizes differ, or
7259** <li> the destination database is an in-memory database and the
7260** destination and source page sizes differ.
7261** </ol>)^
7262**
7263** ^If sqlite3_backup_step() cannot obtain a required file-system lock, then
7264** the [sqlite3_busy_handler | busy-handler function]
7265** is invoked (if one is specified). ^If the
7266** busy-handler returns non-zero before the lock is available, then
7267** [SQLITE_BUSY] is returned to the caller. ^In this case the call to
7268** sqlite3_backup_step() can be retried later. ^If the source
7269** [database connection]
7270** is being used to write to the source database when sqlite3_backup_step()
7271** is called, then [SQLITE_LOCKED] is returned immediately. ^Again, in this
7272** case the call to sqlite3_backup_step() can be retried later on. ^(If
7273** [SQLITE_IOERR_ACCESS | SQLITE_IOERR_XXX], [SQLITE_NOMEM], or
7274** [SQLITE_READONLY] is returned, then
7275** there is no point in retrying the call to sqlite3_backup_step(). These
7276** errors are considered fatal.)^  The application must accept
7277** that the backup operation has failed and pass the backup operation handle
7278** to the sqlite3_backup_finish() to release associated resources.
7279**
7280** ^The first call to sqlite3_backup_step() obtains an exclusive lock
7281** on the destination file. ^The exclusive lock is not released until either
7282** sqlite3_backup_finish() is called or the backup operation is complete
7283** and sqlite3_backup_step() returns [SQLITE_DONE].  ^Every call to
7284** sqlite3_backup_step() obtains a [shared lock] on the source database that
7285** lasts for the duration of the sqlite3_backup_step() call.
7286** ^Because the source database is not locked between calls to
7287** sqlite3_backup_step(), the source database may be modified mid-way
7288** through the backup process.  ^If the source database is modified by an
7289** external process or via a database connection other than the one being
7290** used by the backup operation, then the backup will be automatically
7291** restarted by the next call to sqlite3_backup_step(). ^If the source
7292** database is modified by the using the same database connection as is used
7293** by the backup operation, then the backup database is automatically
7294** updated at the same time.
7295**
7296** [[sqlite3_backup_finish()]] <b>sqlite3_backup_finish()</b>
7297**
7298** When sqlite3_backup_step() has returned [SQLITE_DONE], or when the
7299** application wishes to abandon the backup operation, the application
7300** should destroy the [sqlite3_backup] by passing it to sqlite3_backup_finish().
7301** ^The sqlite3_backup_finish() interfaces releases all
7302** resources associated with the [sqlite3_backup] object.
7303** ^If sqlite3_backup_step() has not yet returned [SQLITE_DONE], then any
7304** active write-transaction on the destination database is rolled back.
7305** The [sqlite3_backup] object is invalid
7306** and may not be used following a call to sqlite3_backup_finish().
7307**
7308** ^The value returned by sqlite3_backup_finish is [SQLITE_OK] if no
7309** sqlite3_backup_step() errors occurred, regardless or whether or not
7310** sqlite3_backup_step() completed.
7311** ^If an out-of-memory condition or IO error occurred during any prior
7312** sqlite3_backup_step() call on the same [sqlite3_backup] object, then
7313** sqlite3_backup_finish() returns the corresponding [error code].
7314**
7315** ^A return of [SQLITE_BUSY] or [SQLITE_LOCKED] from sqlite3_backup_step()
7316** is not a permanent error and does not affect the return value of
7317** sqlite3_backup_finish().
7318**
7319** [[sqlite3_backup_remaining()]] [[sqlite3_backup_pagecount()]]
7320** <b>sqlite3_backup_remaining() and sqlite3_backup_pagecount()</b>
7321**
7322** ^The sqlite3_backup_remaining() routine returns the number of pages still
7323** to be backed up at the conclusion of the most recent sqlite3_backup_step().
7324** ^The sqlite3_backup_pagecount() routine returns the total number of pages
7325** in the source database at the conclusion of the most recent
7326** sqlite3_backup_step().
7327** ^(The values returned by these functions are only updated by
7328** sqlite3_backup_step(). If the source database is modified in a way that
7329** changes the size of the source database or the number of pages remaining,
7330** those changes are not reflected in the output of sqlite3_backup_pagecount()
7331** and sqlite3_backup_remaining() until after the next
7332** sqlite3_backup_step().)^
7333**
7334** <b>Concurrent Usage of Database Handles</b>
7335**
7336** ^The source [database connection] may be used by the application for other
7337** purposes while a backup operation is underway or being initialized.
7338** ^If SQLite is compiled and configured to support threadsafe database
7339** connections, then the source database connection may be used concurrently
7340** from within other threads.
7341**
7342** However, the application must guarantee that the destination
7343** [database connection] is not passed to any other API (by any thread) after
7344** sqlite3_backup_init() is called and before the corresponding call to
7345** sqlite3_backup_finish().  SQLite does not currently check to see
7346** if the application incorrectly accesses the destination [database connection]
7347** and so no error code is reported, but the operations may malfunction
7348** nevertheless.  Use of the destination database connection while a
7349** backup is in progress might also also cause a mutex deadlock.
7350**
7351** If running in [shared cache mode], the application must
7352** guarantee that the shared cache used by the destination database
7353** is not accessed while the backup is running. In practice this means
7354** that the application must guarantee that the disk file being
7355** backed up to is not accessed by any connection within the process,
7356** not just the specific connection that was passed to sqlite3_backup_init().
7357**
7358** The [sqlite3_backup] object itself is partially threadsafe. Multiple
7359** threads may safely make multiple concurrent calls to sqlite3_backup_step().
7360** However, the sqlite3_backup_remaining() and sqlite3_backup_pagecount()
7361** APIs are not strictly speaking threadsafe. If they are invoked at the
7362** same time as another thread is invoking sqlite3_backup_step() it is
7363** possible that they return invalid values.
7364*/
7365SQLITE_API sqlite3_backup *SQLITE_STDCALL sqlite3_backup_init(
7366  sqlite3 *pDest,                        /* Destination database handle */
7367  const char *zDestName,                 /* Destination database name */
7368  sqlite3 *pSource,                      /* Source database handle */
7369  const char *zSourceName                /* Source database name */
7370);
7371SQLITE_API int SQLITE_STDCALL sqlite3_backup_step(sqlite3_backup *p, int nPage);
7372SQLITE_API int SQLITE_STDCALL sqlite3_backup_finish(sqlite3_backup *p);
7373SQLITE_API int SQLITE_STDCALL sqlite3_backup_remaining(sqlite3_backup *p);
7374SQLITE_API int SQLITE_STDCALL sqlite3_backup_pagecount(sqlite3_backup *p);
7375
7376/*
7377** CAPI3REF: Unlock Notification
7378** METHOD: sqlite3
7379**
7380** ^When running in shared-cache mode, a database operation may fail with
7381** an [SQLITE_LOCKED] error if the required locks on the shared-cache or
7382** individual tables within the shared-cache cannot be obtained. See
7383** [SQLite Shared-Cache Mode] for a description of shared-cache locking.
7384** ^This API may be used to register a callback that SQLite will invoke
7385** when the connection currently holding the required lock relinquishes it.
7386** ^This API is only available if the library was compiled with the
7387** [SQLITE_ENABLE_UNLOCK_NOTIFY] C-preprocessor symbol defined.
7388**
7389** See Also: [Using the SQLite Unlock Notification Feature].
7390**
7391** ^Shared-cache locks are released when a database connection concludes
7392** its current transaction, either by committing it or rolling it back.
7393**
7394** ^When a connection (known as the blocked connection) fails to obtain a
7395** shared-cache lock and SQLITE_LOCKED is returned to the caller, the
7396** identity of the database connection (the blocking connection) that
7397** has locked the required resource is stored internally. ^After an
7398** application receives an SQLITE_LOCKED error, it may call the
7399** sqlite3_unlock_notify() method with the blocked connection handle as
7400** the first argument to register for a callback that will be invoked
7401** when the blocking connections current transaction is concluded. ^The
7402** callback is invoked from within the [sqlite3_step] or [sqlite3_close]
7403** call that concludes the blocking connections transaction.
7404**
7405** ^(If sqlite3_unlock_notify() is called in a multi-threaded application,
7406** there is a chance that the blocking connection will have already
7407** concluded its transaction by the time sqlite3_unlock_notify() is invoked.
7408** If this happens, then the specified callback is invoked immediately,
7409** from within the call to sqlite3_unlock_notify().)^
7410**
7411** ^If the blocked connection is attempting to obtain a write-lock on a
7412** shared-cache table, and more than one other connection currently holds
7413** a read-lock on the same table, then SQLite arbitrarily selects one of
7414** the other connections to use as the blocking connection.
7415**
7416** ^(There may be at most one unlock-notify callback registered by a
7417** blocked connection. If sqlite3_unlock_notify() is called when the
7418** blocked connection already has a registered unlock-notify callback,
7419** then the new callback replaces the old.)^ ^If sqlite3_unlock_notify() is
7420** called with a NULL pointer as its second argument, then any existing
7421** unlock-notify callback is canceled. ^The blocked connections
7422** unlock-notify callback may also be canceled by closing the blocked
7423** connection using [sqlite3_close()].
7424**
7425** The unlock-notify callback is not reentrant. If an application invokes
7426** any sqlite3_xxx API functions from within an unlock-notify callback, a
7427** crash or deadlock may be the result.
7428**
7429** ^Unless deadlock is detected (see below), sqlite3_unlock_notify() always
7430** returns SQLITE_OK.
7431**
7432** <b>Callback Invocation Details</b>
7433**
7434** When an unlock-notify callback is registered, the application provides a
7435** single void* pointer that is passed to the callback when it is invoked.
7436** However, the signature of the callback function allows SQLite to pass
7437** it an array of void* context pointers. The first argument passed to
7438** an unlock-notify callback is a pointer to an array of void* pointers,
7439** and the second is the number of entries in the array.
7440**
7441** When a blocking connections transaction is concluded, there may be
7442** more than one blocked connection that has registered for an unlock-notify
7443** callback. ^If two or more such blocked connections have specified the
7444** same callback function, then instead of invoking the callback function
7445** multiple times, it is invoked once with the set of void* context pointers
7446** specified by the blocked connections bundled together into an array.
7447** This gives the application an opportunity to prioritize any actions
7448** related to the set of unblocked database connections.
7449**
7450** <b>Deadlock Detection</b>
7451**
7452** Assuming that after registering for an unlock-notify callback a
7453** database waits for the callback to be issued before taking any further
7454** action (a reasonable assumption), then using this API may cause the
7455** application to deadlock. For example, if connection X is waiting for
7456** connection Y's transaction to be concluded, and similarly connection
7457** Y is waiting on connection X's transaction, then neither connection
7458** will proceed and the system may remain deadlocked indefinitely.
7459**
7460** To avoid this scenario, the sqlite3_unlock_notify() performs deadlock
7461** detection. ^If a given call to sqlite3_unlock_notify() would put the
7462** system in a deadlocked state, then SQLITE_LOCKED is returned and no
7463** unlock-notify callback is registered. The system is said to be in
7464** a deadlocked state if connection A has registered for an unlock-notify
7465** callback on the conclusion of connection B's transaction, and connection
7466** B has itself registered for an unlock-notify callback when connection
7467** A's transaction is concluded. ^Indirect deadlock is also detected, so
7468** the system is also considered to be deadlocked if connection B has
7469** registered for an unlock-notify callback on the conclusion of connection
7470** C's transaction, where connection C is waiting on connection A. ^Any
7471** number of levels of indirection are allowed.
7472**
7473** <b>The "DROP TABLE" Exception</b>
7474**
7475** When a call to [sqlite3_step()] returns SQLITE_LOCKED, it is almost
7476** always appropriate to call sqlite3_unlock_notify(). There is however,
7477** one exception. When executing a "DROP TABLE" or "DROP INDEX" statement,
7478** SQLite checks if there are any currently executing SELECT statements
7479** that belong to the same connection. If there are, SQLITE_LOCKED is
7480** returned. In this case there is no "blocking connection", so invoking
7481** sqlite3_unlock_notify() results in the unlock-notify callback being
7482** invoked immediately. If the application then re-attempts the "DROP TABLE"
7483** or "DROP INDEX" query, an infinite loop might be the result.
7484**
7485** One way around this problem is to check the extended error code returned
7486** by an sqlite3_step() call. ^(If there is a blocking connection, then the
7487** extended error code is set to SQLITE_LOCKED_SHAREDCACHE. Otherwise, in
7488** the special "DROP TABLE/INDEX" case, the extended error code is just
7489** SQLITE_LOCKED.)^
7490*/
7491SQLITE_API int SQLITE_STDCALL sqlite3_unlock_notify(
7492  sqlite3 *pBlocked,                          /* Waiting connection */
7493  void (*xNotify)(void **apArg, int nArg),    /* Callback function to invoke */
7494  void *pNotifyArg                            /* Argument to pass to xNotify */
7495);
7496
7497
7498/*
7499** CAPI3REF: String Comparison
7500**
7501** ^The [sqlite3_stricmp()] and [sqlite3_strnicmp()] APIs allow applications
7502** and extensions to compare the contents of two buffers containing UTF-8
7503** strings in a case-independent fashion, using the same definition of "case
7504** independence" that SQLite uses internally when comparing identifiers.
7505*/
7506SQLITE_API int SQLITE_STDCALL sqlite3_stricmp(const char *, const char *);
7507SQLITE_API int SQLITE_STDCALL sqlite3_strnicmp(const char *, const char *, int);
7508
7509/*
7510** CAPI3REF: String Globbing
7511*
7512** ^The [sqlite3_strglob(P,X)] interface returns zero if string X matches
7513** the glob pattern P, and it returns non-zero if string X does not match
7514** the glob pattern P.  ^The definition of glob pattern matching used in
7515** [sqlite3_strglob(P,X)] is the same as for the "X GLOB P" operator in the
7516** SQL dialect used by SQLite.  ^The sqlite3_strglob(P,X) function is case
7517** sensitive.
7518**
7519** Note that this routine returns zero on a match and non-zero if the strings
7520** do not match, the same as [sqlite3_stricmp()] and [sqlite3_strnicmp()].
7521*/
7522SQLITE_API int SQLITE_STDCALL sqlite3_strglob(const char *zGlob, const char *zStr);
7523
7524/*
7525** CAPI3REF: Error Logging Interface
7526**
7527** ^The [sqlite3_log()] interface writes a message into the [error log]
7528** established by the [SQLITE_CONFIG_LOG] option to [sqlite3_config()].
7529** ^If logging is enabled, the zFormat string and subsequent arguments are
7530** used with [sqlite3_snprintf()] to generate the final output string.
7531**
7532** The sqlite3_log() interface is intended for use by extensions such as
7533** virtual tables, collating functions, and SQL functions.  While there is
7534** nothing to prevent an application from calling sqlite3_log(), doing so
7535** is considered bad form.
7536**
7537** The zFormat string must not be NULL.
7538**
7539** To avoid deadlocks and other threading problems, the sqlite3_log() routine
7540** will not use dynamically allocated memory.  The log message is stored in
7541** a fixed-length buffer on the stack.  If the log message is longer than
7542** a few hundred characters, it will be truncated to the length of the
7543** buffer.
7544*/
7545SQLITE_API void SQLITE_CDECL sqlite3_log(int iErrCode, const char *zFormat, ...);
7546
7547/*
7548** CAPI3REF: Write-Ahead Log Commit Hook
7549** METHOD: sqlite3
7550**
7551** ^The [sqlite3_wal_hook()] function is used to register a callback that
7552** is invoked each time data is committed to a database in wal mode.
7553**
7554** ^(The callback is invoked by SQLite after the commit has taken place and
7555** the associated write-lock on the database released)^, so the implementation
7556** may read, write or [checkpoint] the database as required.
7557**
7558** ^The first parameter passed to the callback function when it is invoked
7559** is a copy of the third parameter passed to sqlite3_wal_hook() when
7560** registering the callback. ^The second is a copy of the database handle.
7561** ^The third parameter is the name of the database that was written to -
7562** either "main" or the name of an [ATTACH]-ed database. ^The fourth parameter
7563** is the number of pages currently in the write-ahead log file,
7564** including those that were just committed.
7565**
7566** The callback function should normally return [SQLITE_OK].  ^If an error
7567** code is returned, that error will propagate back up through the
7568** SQLite code base to cause the statement that provoked the callback
7569** to report an error, though the commit will have still occurred. If the
7570** callback returns [SQLITE_ROW] or [SQLITE_DONE], or if it returns a value
7571** that does not correspond to any valid SQLite error code, the results
7572** are undefined.
7573**
7574** A single database handle may have at most a single write-ahead log callback
7575** registered at one time. ^Calling [sqlite3_wal_hook()] replaces any
7576** previously registered write-ahead log callback. ^Note that the
7577** [sqlite3_wal_autocheckpoint()] interface and the
7578** [wal_autocheckpoint pragma] both invoke [sqlite3_wal_hook()] and will
7579** those overwrite any prior [sqlite3_wal_hook()] settings.
7580*/
7581SQLITE_API void *SQLITE_STDCALL sqlite3_wal_hook(
7582  sqlite3*,
7583  int(*)(void *,sqlite3*,const char*,int),
7584  void*
7585);
7586
7587/*
7588** CAPI3REF: Configure an auto-checkpoint
7589** METHOD: sqlite3
7590**
7591** ^The [sqlite3_wal_autocheckpoint(D,N)] is a wrapper around
7592** [sqlite3_wal_hook()] that causes any database on [database connection] D
7593** to automatically [checkpoint]
7594** after committing a transaction if there are N or
7595** more frames in the [write-ahead log] file.  ^Passing zero or
7596** a negative value as the nFrame parameter disables automatic
7597** checkpoints entirely.
7598**
7599** ^The callback registered by this function replaces any existing callback
7600** registered using [sqlite3_wal_hook()].  ^Likewise, registering a callback
7601** using [sqlite3_wal_hook()] disables the automatic checkpoint mechanism
7602** configured by this function.
7603**
7604** ^The [wal_autocheckpoint pragma] can be used to invoke this interface
7605** from SQL.
7606**
7607** ^Checkpoints initiated by this mechanism are
7608** [sqlite3_wal_checkpoint_v2|PASSIVE].
7609**
7610** ^Every new [database connection] defaults to having the auto-checkpoint
7611** enabled with a threshold of 1000 or [SQLITE_DEFAULT_WAL_AUTOCHECKPOINT]
7612** pages.  The use of this interface
7613** is only necessary if the default setting is found to be suboptimal
7614** for a particular application.
7615*/
7616SQLITE_API int SQLITE_STDCALL sqlite3_wal_autocheckpoint(sqlite3 *db, int N);
7617
7618/*
7619** CAPI3REF: Checkpoint a database
7620** METHOD: sqlite3
7621**
7622** ^(The sqlite3_wal_checkpoint(D,X) is equivalent to
7623** [sqlite3_wal_checkpoint_v2](D,X,[SQLITE_CHECKPOINT_PASSIVE],0,0).)^
7624**
7625** In brief, sqlite3_wal_checkpoint(D,X) causes the content in the
7626** [write-ahead log] for database X on [database connection] D to be
7627** transferred into the database file and for the write-ahead log to
7628** be reset.  See the [checkpointing] documentation for addition
7629** information.
7630**
7631** This interface used to be the only way to cause a checkpoint to
7632** occur.  But then the newer and more powerful [sqlite3_wal_checkpoint_v2()]
7633** interface was added.  This interface is retained for backwards
7634** compatibility and as a convenience for applications that need to manually
7635** start a callback but which do not need the full power (and corresponding
7636** complication) of [sqlite3_wal_checkpoint_v2()].
7637*/
7638SQLITE_API int SQLITE_STDCALL sqlite3_wal_checkpoint(sqlite3 *db, const char *zDb);
7639
7640/*
7641** CAPI3REF: Checkpoint a database
7642** METHOD: sqlite3
7643**
7644** ^(The sqlite3_wal_checkpoint_v2(D,X,M,L,C) interface runs a checkpoint
7645** operation on database X of [database connection] D in mode M.  Status
7646** information is written back into integers pointed to by L and C.)^
7647** ^(The M parameter must be a valid [checkpoint mode]:)^
7648**
7649** <dl>
7650** <dt>SQLITE_CHECKPOINT_PASSIVE<dd>
7651**   ^Checkpoint as many frames as possible without waiting for any database
7652**   readers or writers to finish, then sync the database file if all frames
7653**   in the log were checkpointed. ^The [busy-handler callback]
7654**   is never invoked in the SQLITE_CHECKPOINT_PASSIVE mode.
7655**   ^On the other hand, passive mode might leave the checkpoint unfinished
7656**   if there are concurrent readers or writers.
7657**
7658** <dt>SQLITE_CHECKPOINT_FULL<dd>
7659**   ^This mode blocks (it invokes the
7660**   [sqlite3_busy_handler|busy-handler callback]) until there is no
7661**   database writer and all readers are reading from the most recent database
7662**   snapshot. ^It then checkpoints all frames in the log file and syncs the
7663**   database file. ^This mode blocks new database writers while it is pending,
7664**   but new database readers are allowed to continue unimpeded.
7665**
7666** <dt>SQLITE_CHECKPOINT_RESTART<dd>
7667**   ^This mode works the same way as SQLITE_CHECKPOINT_FULL with the addition
7668**   that after checkpointing the log file it blocks (calls the
7669**   [busy-handler callback])
7670**   until all readers are reading from the database file only. ^This ensures
7671**   that the next writer will restart the log file from the beginning.
7672**   ^Like SQLITE_CHECKPOINT_FULL, this mode blocks new
7673**   database writer attempts while it is pending, but does not impede readers.
7674**
7675** <dt>SQLITE_CHECKPOINT_TRUNCATE<dd>
7676**   ^This mode works the same way as SQLITE_CHECKPOINT_RESTART with the
7677**   addition that it also truncates the log file to zero bytes just prior
7678**   to a successful return.
7679** </dl>
7680**
7681** ^If pnLog is not NULL, then *pnLog is set to the total number of frames in
7682** the log file or to -1 if the checkpoint could not run because
7683** of an error or because the database is not in [WAL mode]. ^If pnCkpt is not
7684** NULL,then *pnCkpt is set to the total number of checkpointed frames in the
7685** log file (including any that were already checkpointed before the function
7686** was called) or to -1 if the checkpoint could not run due to an error or
7687** because the database is not in WAL mode. ^Note that upon successful
7688** completion of an SQLITE_CHECKPOINT_TRUNCATE, the log file will have been
7689** truncated to zero bytes and so both *pnLog and *pnCkpt will be set to zero.
7690**
7691** ^All calls obtain an exclusive "checkpoint" lock on the database file. ^If
7692** any other process is running a checkpoint operation at the same time, the
7693** lock cannot be obtained and SQLITE_BUSY is returned. ^Even if there is a
7694** busy-handler configured, it will not be invoked in this case.
7695**
7696** ^The SQLITE_CHECKPOINT_FULL, RESTART and TRUNCATE modes also obtain the
7697** exclusive "writer" lock on the database file. ^If the writer lock cannot be
7698** obtained immediately, and a busy-handler is configured, it is invoked and
7699** the writer lock retried until either the busy-handler returns 0 or the lock
7700** is successfully obtained. ^The busy-handler is also invoked while waiting for
7701** database readers as described above. ^If the busy-handler returns 0 before
7702** the writer lock is obtained or while waiting for database readers, the
7703** checkpoint operation proceeds from that point in the same way as
7704** SQLITE_CHECKPOINT_PASSIVE - checkpointing as many frames as possible
7705** without blocking any further. ^SQLITE_BUSY is returned in this case.
7706**
7707** ^If parameter zDb is NULL or points to a zero length string, then the
7708** specified operation is attempted on all WAL databases [attached] to
7709** [database connection] db.  In this case the
7710** values written to output parameters *pnLog and *pnCkpt are undefined. ^If
7711** an SQLITE_BUSY error is encountered when processing one or more of the
7712** attached WAL databases, the operation is still attempted on any remaining
7713** attached databases and SQLITE_BUSY is returned at the end. ^If any other
7714** error occurs while processing an attached database, processing is abandoned
7715** and the error code is returned to the caller immediately. ^If no error
7716** (SQLITE_BUSY or otherwise) is encountered while processing the attached
7717** databases, SQLITE_OK is returned.
7718**
7719** ^If database zDb is the name of an attached database that is not in WAL
7720** mode, SQLITE_OK is returned and both *pnLog and *pnCkpt set to -1. ^If
7721** zDb is not NULL (or a zero length string) and is not the name of any
7722** attached database, SQLITE_ERROR is returned to the caller.
7723**
7724** ^Unless it returns SQLITE_MISUSE,
7725** the sqlite3_wal_checkpoint_v2() interface
7726** sets the error information that is queried by
7727** [sqlite3_errcode()] and [sqlite3_errmsg()].
7728**
7729** ^The [PRAGMA wal_checkpoint] command can be used to invoke this interface
7730** from SQL.
7731*/
7732SQLITE_API int SQLITE_STDCALL sqlite3_wal_checkpoint_v2(
7733  sqlite3 *db,                    /* Database handle */
7734  const char *zDb,                /* Name of attached database (or NULL) */
7735  int eMode,                      /* SQLITE_CHECKPOINT_* value */
7736  int *pnLog,                     /* OUT: Size of WAL log in frames */
7737  int *pnCkpt                     /* OUT: Total number of frames checkpointed */
7738);
7739
7740/*
7741** CAPI3REF: Checkpoint Mode Values
7742** KEYWORDS: {checkpoint mode}
7743**
7744** These constants define all valid values for the "checkpoint mode" passed
7745** as the third parameter to the [sqlite3_wal_checkpoint_v2()] interface.
7746** See the [sqlite3_wal_checkpoint_v2()] documentation for details on the
7747** meaning of each of these checkpoint modes.
7748*/
7749#define SQLITE_CHECKPOINT_PASSIVE  0  /* Do as much as possible w/o blocking */
7750#define SQLITE_CHECKPOINT_FULL     1  /* Wait for writers, then checkpoint */
7751#define SQLITE_CHECKPOINT_RESTART  2  /* Like FULL but wait for for readers */
7752#define SQLITE_CHECKPOINT_TRUNCATE 3  /* Like RESTART but also truncate WAL */
7753
7754/*
7755** CAPI3REF: Virtual Table Interface Configuration
7756**
7757** This function may be called by either the [xConnect] or [xCreate] method
7758** of a [virtual table] implementation to configure
7759** various facets of the virtual table interface.
7760**
7761** If this interface is invoked outside the context of an xConnect or
7762** xCreate virtual table method then the behavior is undefined.
7763**
7764** At present, there is only one option that may be configured using
7765** this function. (See [SQLITE_VTAB_CONSTRAINT_SUPPORT].)  Further options
7766** may be added in the future.
7767*/
7768SQLITE_API int SQLITE_CDECL sqlite3_vtab_config(sqlite3*, int op, ...);
7769
7770/*
7771** CAPI3REF: Virtual Table Configuration Options
7772**
7773** These macros define the various options to the
7774** [sqlite3_vtab_config()] interface that [virtual table] implementations
7775** can use to customize and optimize their behavior.
7776**
7777** <dl>
7778** <dt>SQLITE_VTAB_CONSTRAINT_SUPPORT
7779** <dd>Calls of the form
7780** [sqlite3_vtab_config](db,SQLITE_VTAB_CONSTRAINT_SUPPORT,X) are supported,
7781** where X is an integer.  If X is zero, then the [virtual table] whose
7782** [xCreate] or [xConnect] method invoked [sqlite3_vtab_config()] does not
7783** support constraints.  In this configuration (which is the default) if
7784** a call to the [xUpdate] method returns [SQLITE_CONSTRAINT], then the entire
7785** statement is rolled back as if [ON CONFLICT | OR ABORT] had been
7786** specified as part of the users SQL statement, regardless of the actual
7787** ON CONFLICT mode specified.
7788**
7789** If X is non-zero, then the virtual table implementation guarantees
7790** that if [xUpdate] returns [SQLITE_CONSTRAINT], it will do so before
7791** any modifications to internal or persistent data structures have been made.
7792** If the [ON CONFLICT] mode is ABORT, FAIL, IGNORE or ROLLBACK, SQLite
7793** is able to roll back a statement or database transaction, and abandon
7794** or continue processing the current SQL statement as appropriate.
7795** If the ON CONFLICT mode is REPLACE and the [xUpdate] method returns
7796** [SQLITE_CONSTRAINT], SQLite handles this as if the ON CONFLICT mode
7797** had been ABORT.
7798**
7799** Virtual table implementations that are required to handle OR REPLACE
7800** must do so within the [xUpdate] method. If a call to the
7801** [sqlite3_vtab_on_conflict()] function indicates that the current ON
7802** CONFLICT policy is REPLACE, the virtual table implementation should
7803** silently replace the appropriate rows within the xUpdate callback and
7804** return SQLITE_OK. Or, if this is not possible, it may return
7805** SQLITE_CONSTRAINT, in which case SQLite falls back to OR ABORT
7806** constraint handling.
7807** </dl>
7808*/
7809#define SQLITE_VTAB_CONSTRAINT_SUPPORT 1
7810
7811/*
7812** CAPI3REF: Determine The Virtual Table Conflict Policy
7813**
7814** This function may only be called from within a call to the [xUpdate] method
7815** of a [virtual table] implementation for an INSERT or UPDATE operation. ^The
7816** value returned is one of [SQLITE_ROLLBACK], [SQLITE_IGNORE], [SQLITE_FAIL],
7817** [SQLITE_ABORT], or [SQLITE_REPLACE], according to the [ON CONFLICT] mode
7818** of the SQL statement that triggered the call to the [xUpdate] method of the
7819** [virtual table].
7820*/
7821SQLITE_API int SQLITE_STDCALL sqlite3_vtab_on_conflict(sqlite3 *);
7822
7823/*
7824** CAPI3REF: Conflict resolution modes
7825** KEYWORDS: {conflict resolution mode}
7826**
7827** These constants are returned by [sqlite3_vtab_on_conflict()] to
7828** inform a [virtual table] implementation what the [ON CONFLICT] mode
7829** is for the SQL statement being evaluated.
7830**
7831** Note that the [SQLITE_IGNORE] constant is also used as a potential
7832** return value from the [sqlite3_set_authorizer()] callback and that
7833** [SQLITE_ABORT] is also a [result code].
7834*/
7835#define SQLITE_ROLLBACK 1
7836/* #define SQLITE_IGNORE 2 // Also used by sqlite3_authorizer() callback */
7837#define SQLITE_FAIL     3
7838/* #define SQLITE_ABORT 4  // Also an error code */
7839#define SQLITE_REPLACE  5
7840
7841/*
7842** CAPI3REF: Prepared Statement Scan Status Opcodes
7843** KEYWORDS: {scanstatus options}
7844**
7845** The following constants can be used for the T parameter to the
7846** [sqlite3_stmt_scanstatus(S,X,T,V)] interface.  Each constant designates a
7847** different metric for sqlite3_stmt_scanstatus() to return.
7848**
7849** When the value returned to V is a string, space to hold that string is
7850** managed by the prepared statement S and will be automatically freed when
7851** S is finalized.
7852**
7853** <dl>
7854** [[SQLITE_SCANSTAT_NLOOP]] <dt>SQLITE_SCANSTAT_NLOOP</dt>
7855** <dd>^The [sqlite3_int64] variable pointed to by the T parameter will be
7856** set to the total number of times that the X-th loop has run.</dd>
7857**
7858** [[SQLITE_SCANSTAT_NVISIT]] <dt>SQLITE_SCANSTAT_NVISIT</dt>
7859** <dd>^The [sqlite3_int64] variable pointed to by the T parameter will be set
7860** to the total number of rows examined by all iterations of the X-th loop.</dd>
7861**
7862** [[SQLITE_SCANSTAT_EST]] <dt>SQLITE_SCANSTAT_EST</dt>
7863** <dd>^The "double" variable pointed to by the T parameter will be set to the
7864** query planner's estimate for the average number of rows output from each
7865** iteration of the X-th loop.  If the query planner's estimates was accurate,
7866** then this value will approximate the quotient NVISIT/NLOOP and the
7867** product of this value for all prior loops with the same SELECTID will
7868** be the NLOOP value for the current loop.
7869**
7870** [[SQLITE_SCANSTAT_NAME]] <dt>SQLITE_SCANSTAT_NAME</dt>
7871** <dd>^The "const char *" variable pointed to by the T parameter will be set
7872** to a zero-terminated UTF-8 string containing the name of the index or table
7873** used for the X-th loop.
7874**
7875** [[SQLITE_SCANSTAT_EXPLAIN]] <dt>SQLITE_SCANSTAT_EXPLAIN</dt>
7876** <dd>^The "const char *" variable pointed to by the T parameter will be set
7877** to a zero-terminated UTF-8 string containing the [EXPLAIN QUERY PLAN]
7878** description for the X-th loop.
7879**
7880** [[SQLITE_SCANSTAT_SELECTID]] <dt>SQLITE_SCANSTAT_SELECT</dt>
7881** <dd>^The "int" variable pointed to by the T parameter will be set to the
7882** "select-id" for the X-th loop.  The select-id identifies which query or
7883** subquery the loop is part of.  The main query has a select-id of zero.
7884** The select-id is the same value as is output in the first column
7885** of an [EXPLAIN QUERY PLAN] query.
7886** </dl>
7887*/
7888#define SQLITE_SCANSTAT_NLOOP    0
7889#define SQLITE_SCANSTAT_NVISIT   1
7890#define SQLITE_SCANSTAT_EST      2
7891#define SQLITE_SCANSTAT_NAME     3
7892#define SQLITE_SCANSTAT_EXPLAIN  4
7893#define SQLITE_SCANSTAT_SELECTID 5
7894
7895/*
7896** CAPI3REF: Prepared Statement Scan Status
7897** METHOD: sqlite3_stmt
7898**
7899** This interface returns information about the predicted and measured
7900** performance for pStmt.  Advanced applications can use this
7901** interface to compare the predicted and the measured performance and
7902** issue warnings and/or rerun [ANALYZE] if discrepancies are found.
7903**
7904** Since this interface is expected to be rarely used, it is only
7905** available if SQLite is compiled using the [SQLITE_ENABLE_STMT_SCANSTATUS]
7906** compile-time option.
7907**
7908** The "iScanStatusOp" parameter determines which status information to return.
7909** The "iScanStatusOp" must be one of the [scanstatus options] or the behavior
7910** of this interface is undefined.
7911** ^The requested measurement is written into a variable pointed to by
7912** the "pOut" parameter.
7913** Parameter "idx" identifies the specific loop to retrieve statistics for.
7914** Loops are numbered starting from zero. ^If idx is out of range - less than
7915** zero or greater than or equal to the total number of loops used to implement
7916** the statement - a non-zero value is returned and the variable that pOut
7917** points to is unchanged.
7918**
7919** ^Statistics might not be available for all loops in all statements. ^In cases
7920** where there exist loops with no available statistics, this function behaves
7921** as if the loop did not exist - it returns non-zero and leave the variable
7922** that pOut points to unchanged.
7923**
7924** See also: [sqlite3_stmt_scanstatus_reset()]
7925*/
7926SQLITE_API int SQLITE_STDCALL sqlite3_stmt_scanstatus(
7927  sqlite3_stmt *pStmt,      /* Prepared statement for which info desired */
7928  int idx,                  /* Index of loop to report on */
7929  int iScanStatusOp,        /* Information desired.  SQLITE_SCANSTAT_* */
7930  void *pOut                /* Result written here */
7931);
7932
7933/*
7934** CAPI3REF: Zero Scan-Status Counters
7935** METHOD: sqlite3_stmt
7936**
7937** ^Zero all [sqlite3_stmt_scanstatus()] related event counters.
7938**
7939** This API is only available if the library is built with pre-processor
7940** symbol [SQLITE_ENABLE_STMT_SCANSTATUS] defined.
7941*/
7942SQLITE_API void SQLITE_STDCALL sqlite3_stmt_scanstatus_reset(sqlite3_stmt*);
7943
7944
7945/*
7946** Undo the hack that converts floating point types to integer for
7947** builds on processors without floating point support.
7948*/
7949#ifdef SQLITE_OMIT_FLOATING_POINT
7950# undef double
7951#endif
7952
7953#if 0
7954}  /* End of the 'extern "C"' block */
7955#endif
7956#endif /* _SQLITE3_H_ */
7957
7958/*
7959** 2010 August 30
7960**
7961** The author disclaims copyright to this source code.  In place of
7962** a legal notice, here is a blessing:
7963**
7964**    May you do good and not evil.
7965**    May you find forgiveness for yourself and forgive others.
7966**    May you share freely, never taking more than you give.
7967**
7968*************************************************************************
7969*/
7970
7971#ifndef _SQLITE3RTREE_H_
7972#define _SQLITE3RTREE_H_
7973
7974
7975#if 0
7976extern "C" {
7977#endif
7978
7979typedef struct sqlite3_rtree_geometry sqlite3_rtree_geometry;
7980typedef struct sqlite3_rtree_query_info sqlite3_rtree_query_info;
7981
7982/* The double-precision datatype used by RTree depends on the
7983** SQLITE_RTREE_INT_ONLY compile-time option.
7984*/
7985#ifdef SQLITE_RTREE_INT_ONLY
7986  typedef sqlite3_int64 sqlite3_rtree_dbl;
7987#else
7988  typedef double sqlite3_rtree_dbl;
7989#endif
7990
7991/*
7992** Register a geometry callback named zGeom that can be used as part of an
7993** R-Tree geometry query as follows:
7994**
7995**   SELECT ... FROM <rtree> WHERE <rtree col> MATCH $zGeom(... params ...)
7996*/
7997SQLITE_API int SQLITE_STDCALL sqlite3_rtree_geometry_callback(
7998  sqlite3 *db,
7999  const char *zGeom,
8000  int (*xGeom)(sqlite3_rtree_geometry*, int, sqlite3_rtree_dbl*,int*),
8001  void *pContext
8002);
8003
8004
8005/*
8006** A pointer to a structure of the following type is passed as the first
8007** argument to callbacks registered using rtree_geometry_callback().
8008*/
8009struct sqlite3_rtree_geometry {
8010  void *pContext;                 /* Copy of pContext passed to s_r_g_c() */
8011  int nParam;                     /* Size of array aParam[] */
8012  sqlite3_rtree_dbl *aParam;      /* Parameters passed to SQL geom function */
8013  void *pUser;                    /* Callback implementation user data */
8014  void (*xDelUser)(void *);       /* Called by SQLite to clean up pUser */
8015};
8016
8017/*
8018** Register a 2nd-generation geometry callback named zScore that can be
8019** used as part of an R-Tree geometry query as follows:
8020**
8021**   SELECT ... FROM <rtree> WHERE <rtree col> MATCH $zQueryFunc(... params ...)
8022*/
8023SQLITE_API int SQLITE_STDCALL sqlite3_rtree_query_callback(
8024  sqlite3 *db,
8025  const char *zQueryFunc,
8026  int (*xQueryFunc)(sqlite3_rtree_query_info*),
8027  void *pContext,
8028  void (*xDestructor)(void*)
8029);
8030
8031
8032/*
8033** A pointer to a structure of the following type is passed as the
8034** argument to scored geometry callback registered using
8035** sqlite3_rtree_query_callback().
8036**
8037** Note that the first 5 fields of this structure are identical to
8038** sqlite3_rtree_geometry.  This structure is a subclass of
8039** sqlite3_rtree_geometry.
8040*/
8041struct sqlite3_rtree_query_info {
8042  void *pContext;                   /* pContext from when function registered */
8043  int nParam;                       /* Number of function parameters */
8044  sqlite3_rtree_dbl *aParam;        /* value of function parameters */
8045  void *pUser;                      /* callback can use this, if desired */
8046  void (*xDelUser)(void*);          /* function to free pUser */
8047  sqlite3_rtree_dbl *aCoord;        /* Coordinates of node or entry to check */
8048  unsigned int *anQueue;            /* Number of pending entries in the queue */
8049  int nCoord;                       /* Number of coordinates */
8050  int iLevel;                       /* Level of current node or entry */
8051  int mxLevel;                      /* The largest iLevel value in the tree */
8052  sqlite3_int64 iRowid;             /* Rowid for current entry */
8053  sqlite3_rtree_dbl rParentScore;   /* Score of parent node */
8054  int eParentWithin;                /* Visibility of parent node */
8055  int eWithin;                      /* OUT: Visiblity */
8056  sqlite3_rtree_dbl rScore;         /* OUT: Write the score here */
8057  /* The following fields are only available in 3.8.11 and later */
8058  sqlite3_value **apSqlParam;       /* Original SQL values of parameters */
8059};
8060
8061/*
8062** Allowed values for sqlite3_rtree_query.eWithin and .eParentWithin.
8063*/
8064#define NOT_WITHIN       0   /* Object completely outside of query region */
8065#define PARTLY_WITHIN    1   /* Object partially overlaps query region */
8066#define FULLY_WITHIN     2   /* Object fully contained within query region */
8067
8068
8069#if 0
8070}  /* end of the 'extern "C"' block */
8071#endif
8072
8073#endif  /* ifndef _SQLITE3RTREE_H_ */
8074
8075
8076/************** End of sqlite3.h *********************************************/
8077/************** Continuing where we left off in sqliteInt.h ******************/
8078
8079/*
8080** Include the configuration header output by 'configure' if we're using the
8081** autoconf-based build
8082*/
8083#ifdef _HAVE_SQLITE_CONFIG_H
8084#include "config.h"
8085#endif
8086
8087/************** Include sqliteLimit.h in the middle of sqliteInt.h ***********/
8088/************** Begin file sqliteLimit.h *************************************/
8089/*
8090** 2007 May 7
8091**
8092** The author disclaims copyright to this source code.  In place of
8093** a legal notice, here is a blessing:
8094**
8095**    May you do good and not evil.
8096**    May you find forgiveness for yourself and forgive others.
8097**    May you share freely, never taking more than you give.
8098**
8099*************************************************************************
8100**
8101** This file defines various limits of what SQLite can process.
8102*/
8103
8104/*
8105** The maximum length of a TEXT or BLOB in bytes.   This also
8106** limits the size of a row in a table or index.
8107**
8108** The hard limit is the ability of a 32-bit signed integer
8109** to count the size: 2^31-1 or 2147483647.
8110*/
8111#ifndef SQLITE_MAX_LENGTH
8112# define SQLITE_MAX_LENGTH 1000000000
8113#endif
8114
8115/*
8116** This is the maximum number of
8117**
8118**    * Columns in a table
8119**    * Columns in an index
8120**    * Columns in a view
8121**    * Terms in the SET clause of an UPDATE statement
8122**    * Terms in the result set of a SELECT statement
8123**    * Terms in the GROUP BY or ORDER BY clauses of a SELECT statement.
8124**    * Terms in the VALUES clause of an INSERT statement
8125**
8126** The hard upper limit here is 32676.  Most database people will
8127** tell you that in a well-normalized database, you usually should
8128** not have more than a dozen or so columns in any table.  And if
8129** that is the case, there is no point in having more than a few
8130** dozen values in any of the other situations described above.
8131*/
8132#ifndef SQLITE_MAX_COLUMN
8133# define SQLITE_MAX_COLUMN 2000
8134#endif
8135
8136/*
8137** The maximum length of a single SQL statement in bytes.
8138**
8139** It used to be the case that setting this value to zero would
8140** turn the limit off.  That is no longer true.  It is not possible
8141** to turn this limit off.
8142*/
8143#ifndef SQLITE_MAX_SQL_LENGTH
8144# define SQLITE_MAX_SQL_LENGTH 1000000000
8145#endif
8146
8147/*
8148** The maximum depth of an expression tree. This is limited to
8149** some extent by SQLITE_MAX_SQL_LENGTH. But sometime you might
8150** want to place more severe limits on the complexity of an
8151** expression.
8152**
8153** A value of 0 used to mean that the limit was not enforced.
8154** But that is no longer true.  The limit is now strictly enforced
8155** at all times.
8156*/
8157#ifndef SQLITE_MAX_EXPR_DEPTH
8158# define SQLITE_MAX_EXPR_DEPTH 1000
8159#endif
8160
8161/*
8162** The maximum number of terms in a compound SELECT statement.
8163** The code generator for compound SELECT statements does one
8164** level of recursion for each term.  A stack overflow can result
8165** if the number of terms is too large.  In practice, most SQL
8166** never has more than 3 or 4 terms.  Use a value of 0 to disable
8167** any limit on the number of terms in a compount SELECT.
8168*/
8169#ifndef SQLITE_MAX_COMPOUND_SELECT
8170# define SQLITE_MAX_COMPOUND_SELECT 500
8171#endif
8172
8173/*
8174** The maximum number of opcodes in a VDBE program.
8175** Not currently enforced.
8176*/
8177#ifndef SQLITE_MAX_VDBE_OP
8178# define SQLITE_MAX_VDBE_OP 25000
8179#endif
8180
8181/*
8182** The maximum number of arguments to an SQL function.
8183*/
8184#ifndef SQLITE_MAX_FUNCTION_ARG
8185# define SQLITE_MAX_FUNCTION_ARG 127
8186#endif
8187
8188/*
8189** The suggested maximum number of in-memory pages to use for
8190** the main database table and for temporary tables.
8191**
8192** IMPLEMENTATION-OF: R-31093-59126 The default suggested cache size
8193** is 2000 pages.
8194** IMPLEMENTATION-OF: R-48205-43578 The default suggested cache size can be
8195** altered using the SQLITE_DEFAULT_CACHE_SIZE compile-time options.
8196*/
8197#ifndef SQLITE_DEFAULT_CACHE_SIZE
8198# define SQLITE_DEFAULT_CACHE_SIZE  2000
8199#endif
8200
8201/*
8202** The default number of frames to accumulate in the log file before
8203** checkpointing the database in WAL mode.
8204*/
8205#ifndef SQLITE_DEFAULT_WAL_AUTOCHECKPOINT
8206# define SQLITE_DEFAULT_WAL_AUTOCHECKPOINT  1000
8207#endif
8208
8209/*
8210** The maximum number of attached databases.  This must be between 0
8211** and 62.  The upper bound on 62 is because a 64-bit integer bitmap
8212** is used internally to track attached databases.
8213*/
8214#ifndef SQLITE_MAX_ATTACHED
8215# define SQLITE_MAX_ATTACHED 10
8216#endif
8217
8218
8219/*
8220** The maximum value of a ?nnn wildcard that the parser will accept.
8221*/
8222#ifndef SQLITE_MAX_VARIABLE_NUMBER
8223# define SQLITE_MAX_VARIABLE_NUMBER 999
8224#endif
8225
8226/* Maximum page size.  The upper bound on this value is 65536.  This a limit
8227** imposed by the use of 16-bit offsets within each page.
8228**
8229** Earlier versions of SQLite allowed the user to change this value at
8230** compile time. This is no longer permitted, on the grounds that it creates
8231** a library that is technically incompatible with an SQLite library
8232** compiled with a different limit. If a process operating on a database
8233** with a page-size of 65536 bytes crashes, then an instance of SQLite
8234** compiled with the default page-size limit will not be able to rollback
8235** the aborted transaction. This could lead to database corruption.
8236*/
8237#ifdef SQLITE_MAX_PAGE_SIZE
8238# undef SQLITE_MAX_PAGE_SIZE
8239#endif
8240#define SQLITE_MAX_PAGE_SIZE 65536
8241
8242
8243/*
8244** The default size of a database page.
8245*/
8246#ifndef SQLITE_DEFAULT_PAGE_SIZE
8247# define SQLITE_DEFAULT_PAGE_SIZE 1024
8248#endif
8249#if SQLITE_DEFAULT_PAGE_SIZE>SQLITE_MAX_PAGE_SIZE
8250# undef SQLITE_DEFAULT_PAGE_SIZE
8251# define SQLITE_DEFAULT_PAGE_SIZE SQLITE_MAX_PAGE_SIZE
8252#endif
8253
8254/*
8255** Ordinarily, if no value is explicitly provided, SQLite creates databases
8256** with page size SQLITE_DEFAULT_PAGE_SIZE. However, based on certain
8257** device characteristics (sector-size and atomic write() support),
8258** SQLite may choose a larger value. This constant is the maximum value
8259** SQLite will choose on its own.
8260*/
8261#ifndef SQLITE_MAX_DEFAULT_PAGE_SIZE
8262# define SQLITE_MAX_DEFAULT_PAGE_SIZE 8192
8263#endif
8264#if SQLITE_MAX_DEFAULT_PAGE_SIZE>SQLITE_MAX_PAGE_SIZE
8265# undef SQLITE_MAX_DEFAULT_PAGE_SIZE
8266# define SQLITE_MAX_DEFAULT_PAGE_SIZE SQLITE_MAX_PAGE_SIZE
8267#endif
8268
8269
8270/*
8271** Maximum number of pages in one database file.
8272**
8273** This is really just the default value for the max_page_count pragma.
8274** This value can be lowered (or raised) at run-time using that the
8275** max_page_count macro.
8276*/
8277#ifndef SQLITE_MAX_PAGE_COUNT
8278# define SQLITE_MAX_PAGE_COUNT 1073741823
8279#endif
8280
8281/*
8282** Maximum length (in bytes) of the pattern in a LIKE or GLOB
8283** operator.
8284*/
8285#ifndef SQLITE_MAX_LIKE_PATTERN_LENGTH
8286# define SQLITE_MAX_LIKE_PATTERN_LENGTH 50000
8287#endif
8288
8289/*
8290** Maximum depth of recursion for triggers.
8291**
8292** A value of 1 means that a trigger program will not be able to itself
8293** fire any triggers. A value of 0 means that no trigger programs at all
8294** may be executed.
8295*/
8296#ifndef SQLITE_MAX_TRIGGER_DEPTH
8297# define SQLITE_MAX_TRIGGER_DEPTH 1000
8298#endif
8299
8300/************** End of sqliteLimit.h *****************************************/
8301/************** Continuing where we left off in sqliteInt.h ******************/
8302
8303/* Disable nuisance warnings on Borland compilers */
8304#if defined(__BORLANDC__)
8305#pragma warn -rch /* unreachable code */
8306#pragma warn -ccc /* Condition is always true or false */
8307#pragma warn -aus /* Assigned value is never used */
8308#pragma warn -csu /* Comparing signed and unsigned */
8309#pragma warn -spa /* Suspicious pointer arithmetic */
8310#endif
8311
8312/*
8313** Include standard header files as necessary
8314*/
8315#ifdef HAVE_STDINT_H
8316#include <stdint.h>
8317#endif
8318#ifdef HAVE_INTTYPES_H
8319#include <inttypes.h>
8320#endif
8321
8322/*
8323** The following macros are used to cast pointers to integers and
8324** integers to pointers.  The way you do this varies from one compiler
8325** to the next, so we have developed the following set of #if statements
8326** to generate appropriate macros for a wide range of compilers.
8327**
8328** The correct "ANSI" way to do this is to use the intptr_t type.
8329** Unfortunately, that typedef is not available on all compilers, or
8330** if it is available, it requires an #include of specific headers
8331** that vary from one machine to the next.
8332**
8333** Ticket #3860:  The llvm-gcc-4.2 compiler from Apple chokes on
8334** the ((void*)&((char*)0)[X]) construct.  But MSVC chokes on ((void*)(X)).
8335** So we have to define the macros in different ways depending on the
8336** compiler.
8337*/
8338#if defined(__PTRDIFF_TYPE__)  /* This case should work for GCC */
8339# define SQLITE_INT_TO_PTR(X)  ((void*)(__PTRDIFF_TYPE__)(X))
8340# define SQLITE_PTR_TO_INT(X)  ((int)(__PTRDIFF_TYPE__)(X))
8341#elif !defined(__GNUC__)       /* Works for compilers other than LLVM */
8342# define SQLITE_INT_TO_PTR(X)  ((void*)&((char*)0)[X])
8343# define SQLITE_PTR_TO_INT(X)  ((int)(((char*)X)-(char*)0))
8344#elif defined(HAVE_STDINT_H)   /* Use this case if we have ANSI headers */
8345# define SQLITE_INT_TO_PTR(X)  ((void*)(intptr_t)(X))
8346# define SQLITE_PTR_TO_INT(X)  ((int)(intptr_t)(X))
8347#else                          /* Generates a warning - but it always works */
8348# define SQLITE_INT_TO_PTR(X)  ((void*)(X))
8349# define SQLITE_PTR_TO_INT(X)  ((int)(X))
8350#endif
8351
8352/*
8353** A macro to hint to the compiler that a function should not be
8354** inlined.
8355*/
8356#if defined(__GNUC__)
8357#  define SQLITE_NOINLINE  __attribute__((noinline))
8358#elif defined(_MSC_VER) && _MSC_VER>=1310
8359#  define SQLITE_NOINLINE  __declspec(noinline)
8360#else
8361#  define SQLITE_NOINLINE
8362#endif
8363
8364/*
8365** Make sure that the compiler intrinsics we desire are enabled when
8366** compiling with an appropriate version of MSVC.
8367*/
8368#if defined(_MSC_VER) && _MSC_VER>=1300
8369#  if !defined(_WIN32_WCE)
8370#    include <intrin.h>
8371#    pragma intrinsic(_byteswap_ushort)
8372#    pragma intrinsic(_byteswap_ulong)
8373#  else
8374#    include <cmnintrin.h>
8375#  endif
8376#endif
8377
8378/*
8379** The SQLITE_THREADSAFE macro must be defined as 0, 1, or 2.
8380** 0 means mutexes are permanently disable and the library is never
8381** threadsafe.  1 means the library is serialized which is the highest
8382** level of threadsafety.  2 means the library is multithreaded - multiple
8383** threads can use SQLite as long as no two threads try to use the same
8384** database connection at the same time.
8385**
8386** Older versions of SQLite used an optional THREADSAFE macro.
8387** We support that for legacy.
8388*/
8389#if !defined(SQLITE_THREADSAFE)
8390# if defined(THREADSAFE)
8391#   define SQLITE_THREADSAFE THREADSAFE
8392# else
8393#   define SQLITE_THREADSAFE 1 /* IMP: R-07272-22309 */
8394# endif
8395#endif
8396
8397/*
8398** Powersafe overwrite is on by default.  But can be turned off using
8399** the -DSQLITE_POWERSAFE_OVERWRITE=0 command-line option.
8400*/
8401#ifndef SQLITE_POWERSAFE_OVERWRITE
8402# define SQLITE_POWERSAFE_OVERWRITE 1
8403#endif
8404
8405/*
8406** EVIDENCE-OF: R-25715-37072 Memory allocation statistics are enabled by
8407** default unless SQLite is compiled with SQLITE_DEFAULT_MEMSTATUS=0 in
8408** which case memory allocation statistics are disabled by default.
8409*/
8410#if !defined(SQLITE_DEFAULT_MEMSTATUS)
8411# define SQLITE_DEFAULT_MEMSTATUS 1
8412#endif
8413
8414/*
8415** Exactly one of the following macros must be defined in order to
8416** specify which memory allocation subsystem to use.
8417**
8418**     SQLITE_SYSTEM_MALLOC          // Use normal system malloc()
8419**     SQLITE_WIN32_MALLOC           // Use Win32 native heap API
8420**     SQLITE_ZERO_MALLOC            // Use a stub allocator that always fails
8421**     SQLITE_MEMDEBUG               // Debugging version of system malloc()
8422**
8423** On Windows, if the SQLITE_WIN32_MALLOC_VALIDATE macro is defined and the
8424** assert() macro is enabled, each call into the Win32 native heap subsystem
8425** will cause HeapValidate to be called.  If heap validation should fail, an
8426** assertion will be triggered.
8427**
8428** If none of the above are defined, then set SQLITE_SYSTEM_MALLOC as
8429** the default.
8430*/
8431#if defined(SQLITE_SYSTEM_MALLOC) \
8432  + defined(SQLITE_WIN32_MALLOC) \
8433  + defined(SQLITE_ZERO_MALLOC) \
8434  + defined(SQLITE_MEMDEBUG)>1
8435# error "Two or more of the following compile-time configuration options\
8436 are defined but at most one is allowed:\
8437 SQLITE_SYSTEM_MALLOC, SQLITE_WIN32_MALLOC, SQLITE_MEMDEBUG,\
8438 SQLITE_ZERO_MALLOC"
8439#endif
8440#if defined(SQLITE_SYSTEM_MALLOC) \
8441  + defined(SQLITE_WIN32_MALLOC) \
8442  + defined(SQLITE_ZERO_MALLOC) \
8443  + defined(SQLITE_MEMDEBUG)==0
8444# define SQLITE_SYSTEM_MALLOC 1
8445#endif
8446
8447/*
8448** If SQLITE_MALLOC_SOFT_LIMIT is not zero, then try to keep the
8449** sizes of memory allocations below this value where possible.
8450*/
8451#if !defined(SQLITE_MALLOC_SOFT_LIMIT)
8452# define SQLITE_MALLOC_SOFT_LIMIT 1024
8453#endif
8454
8455/*
8456** We need to define _XOPEN_SOURCE as follows in order to enable
8457** recursive mutexes on most Unix systems and fchmod() on OpenBSD.
8458** But _XOPEN_SOURCE define causes problems for Mac OS X, so omit
8459** it.
8460*/
8461#if !defined(_XOPEN_SOURCE) && !defined(__DARWIN__) && !defined(__APPLE__)
8462#  define _XOPEN_SOURCE 600
8463#endif
8464
8465/*
8466** NDEBUG and SQLITE_DEBUG are opposites.  It should always be true that
8467** defined(NDEBUG)==!defined(SQLITE_DEBUG).  If this is not currently true,
8468** make it true by defining or undefining NDEBUG.
8469**
8470** Setting NDEBUG makes the code smaller and faster by disabling the
8471** assert() statements in the code.  So we want the default action
8472** to be for NDEBUG to be set and NDEBUG to be undefined only if SQLITE_DEBUG
8473** is set.  Thus NDEBUG becomes an opt-in rather than an opt-out
8474** feature.
8475*/
8476#if !defined(NDEBUG) && !defined(SQLITE_DEBUG)
8477# define NDEBUG 1
8478#endif
8479#if defined(NDEBUG) && defined(SQLITE_DEBUG)
8480# undef NDEBUG
8481#endif
8482
8483/*
8484** Enable SQLITE_ENABLE_EXPLAIN_COMMENTS if SQLITE_DEBUG is turned on.
8485*/
8486#if !defined(SQLITE_ENABLE_EXPLAIN_COMMENTS) && defined(SQLITE_DEBUG)
8487# define SQLITE_ENABLE_EXPLAIN_COMMENTS 1
8488#endif
8489
8490/*
8491** The testcase() macro is used to aid in coverage testing.  When
8492** doing coverage testing, the condition inside the argument to
8493** testcase() must be evaluated both true and false in order to
8494** get full branch coverage.  The testcase() macro is inserted
8495** to help ensure adequate test coverage in places where simple
8496** condition/decision coverage is inadequate.  For example, testcase()
8497** can be used to make sure boundary values are tested.  For
8498** bitmask tests, testcase() can be used to make sure each bit
8499** is significant and used at least once.  On switch statements
8500** where multiple cases go to the same block of code, testcase()
8501** can insure that all cases are evaluated.
8502**
8503*/
8504#ifdef SQLITE_COVERAGE_TEST
8505SQLITE_PRIVATE   void sqlite3Coverage(int);
8506# define testcase(X)  if( X ){ sqlite3Coverage(__LINE__); }
8507#else
8508# define testcase(X)
8509#endif
8510
8511/*
8512** The TESTONLY macro is used to enclose variable declarations or
8513** other bits of code that are needed to support the arguments
8514** within testcase() and assert() macros.
8515*/
8516#if !defined(NDEBUG) || defined(SQLITE_COVERAGE_TEST)
8517# define TESTONLY(X)  X
8518#else
8519# define TESTONLY(X)
8520#endif
8521
8522/*
8523** Sometimes we need a small amount of code such as a variable initialization
8524** to setup for a later assert() statement.  We do not want this code to
8525** appear when assert() is disabled.  The following macro is therefore
8526** used to contain that setup code.  The "VVA" acronym stands for
8527** "Verification, Validation, and Accreditation".  In other words, the
8528** code within VVA_ONLY() will only run during verification processes.
8529*/
8530#ifndef NDEBUG
8531# define VVA_ONLY(X)  X
8532#else
8533# define VVA_ONLY(X)
8534#endif
8535
8536/*
8537** The ALWAYS and NEVER macros surround boolean expressions which
8538** are intended to always be true or false, respectively.  Such
8539** expressions could be omitted from the code completely.  But they
8540** are included in a few cases in order to enhance the resilience
8541** of SQLite to unexpected behavior - to make the code "self-healing"
8542** or "ductile" rather than being "brittle" and crashing at the first
8543** hint of unplanned behavior.
8544**
8545** In other words, ALWAYS and NEVER are added for defensive code.
8546**
8547** When doing coverage testing ALWAYS and NEVER are hard-coded to
8548** be true and false so that the unreachable code they specify will
8549** not be counted as untested code.
8550*/
8551#if defined(SQLITE_COVERAGE_TEST)
8552# define ALWAYS(X)      (1)
8553# define NEVER(X)       (0)
8554#elif !defined(NDEBUG)
8555# define ALWAYS(X)      ((X)?1:(assert(0),0))
8556# define NEVER(X)       ((X)?(assert(0),1):0)
8557#else
8558# define ALWAYS(X)      (X)
8559# define NEVER(X)       (X)
8560#endif
8561
8562/*
8563** Declarations used for tracing the operating system interfaces.
8564*/
8565#if defined(SQLITE_FORCE_OS_TRACE) || defined(SQLITE_TEST) || \
8566    (defined(SQLITE_DEBUG) && SQLITE_OS_WIN)
8567  extern int sqlite3OSTrace;
8568# define OSTRACE(X)          if( sqlite3OSTrace ) sqlite3DebugPrintf X
8569# define SQLITE_HAVE_OS_TRACE
8570#else
8571# define OSTRACE(X)
8572# undef  SQLITE_HAVE_OS_TRACE
8573#endif
8574
8575/*
8576** Is the sqlite3ErrName() function needed in the build?  Currently,
8577** it is needed by "mutex_w32.c" (when debugging), "os_win.c" (when
8578** OSTRACE is enabled), and by several "test*.c" files (which are
8579** compiled using SQLITE_TEST).
8580*/
8581#if defined(SQLITE_HAVE_OS_TRACE) || defined(SQLITE_TEST) || \
8582    (defined(SQLITE_DEBUG) && SQLITE_OS_WIN)
8583# define SQLITE_NEED_ERR_NAME
8584#else
8585# undef  SQLITE_NEED_ERR_NAME
8586#endif
8587
8588/*
8589** Return true (non-zero) if the input is an integer that is too large
8590** to fit in 32-bits.  This macro is used inside of various testcase()
8591** macros to verify that we have tested SQLite for large-file support.
8592*/
8593#define IS_BIG_INT(X)  (((X)&~(i64)0xffffffff)!=0)
8594
8595/*
8596** The macro unlikely() is a hint that surrounds a boolean
8597** expression that is usually false.  Macro likely() surrounds
8598** a boolean expression that is usually true.  These hints could,
8599** in theory, be used by the compiler to generate better code, but
8600** currently they are just comments for human readers.
8601*/
8602#define likely(X)    (X)
8603#define unlikely(X)  (X)
8604
8605/************** Include hash.h in the middle of sqliteInt.h ******************/
8606/************** Begin file hash.h ********************************************/
8607/*
8608** 2001 September 22
8609**
8610** The author disclaims copyright to this source code.  In place of
8611** a legal notice, here is a blessing:
8612**
8613**    May you do good and not evil.
8614**    May you find forgiveness for yourself and forgive others.
8615**    May you share freely, never taking more than you give.
8616**
8617*************************************************************************
8618** This is the header file for the generic hash-table implementation
8619** used in SQLite.
8620*/
8621#ifndef _SQLITE_HASH_H_
8622#define _SQLITE_HASH_H_
8623
8624/* Forward declarations of structures. */
8625typedef struct Hash Hash;
8626typedef struct HashElem HashElem;
8627
8628/* A complete hash table is an instance of the following structure.
8629** The internals of this structure are intended to be opaque -- client
8630** code should not attempt to access or modify the fields of this structure
8631** directly.  Change this structure only by using the routines below.
8632** However, some of the "procedures" and "functions" for modifying and
8633** accessing this structure are really macros, so we can't really make
8634** this structure opaque.
8635**
8636** All elements of the hash table are on a single doubly-linked list.
8637** Hash.first points to the head of this list.
8638**
8639** There are Hash.htsize buckets.  Each bucket points to a spot in
8640** the global doubly-linked list.  The contents of the bucket are the
8641** element pointed to plus the next _ht.count-1 elements in the list.
8642**
8643** Hash.htsize and Hash.ht may be zero.  In that case lookup is done
8644** by a linear search of the global list.  For small tables, the
8645** Hash.ht table is never allocated because if there are few elements
8646** in the table, it is faster to do a linear search than to manage
8647** the hash table.
8648*/
8649struct Hash {
8650  unsigned int htsize;      /* Number of buckets in the hash table */
8651  unsigned int count;       /* Number of entries in this table */
8652  HashElem *first;          /* The first element of the array */
8653  struct _ht {              /* the hash table */
8654    int count;                 /* Number of entries with this hash */
8655    HashElem *chain;           /* Pointer to first entry with this hash */
8656  } *ht;
8657};
8658
8659/* Each element in the hash table is an instance of the following
8660** structure.  All elements are stored on a single doubly-linked list.
8661**
8662** Again, this structure is intended to be opaque, but it can't really
8663** be opaque because it is used by macros.
8664*/
8665struct HashElem {
8666  HashElem *next, *prev;       /* Next and previous elements in the table */
8667  void *data;                  /* Data associated with this element */
8668  const char *pKey;            /* Key associated with this element */
8669};
8670
8671/*
8672** Access routines.  To delete, insert a NULL pointer.
8673*/
8674SQLITE_PRIVATE void sqlite3HashInit(Hash*);
8675SQLITE_PRIVATE void *sqlite3HashInsert(Hash*, const char *pKey, void *pData);
8676SQLITE_PRIVATE void *sqlite3HashFind(const Hash*, const char *pKey);
8677SQLITE_PRIVATE void sqlite3HashClear(Hash*);
8678
8679/*
8680** Macros for looping over all elements of a hash table.  The idiom is
8681** like this:
8682**
8683**   Hash h;
8684**   HashElem *p;
8685**   ...
8686**   for(p=sqliteHashFirst(&h); p; p=sqliteHashNext(p)){
8687**     SomeStructure *pData = sqliteHashData(p);
8688**     // do something with pData
8689**   }
8690*/
8691#define sqliteHashFirst(H)  ((H)->first)
8692#define sqliteHashNext(E)   ((E)->next)
8693#define sqliteHashData(E)   ((E)->data)
8694/* #define sqliteHashKey(E)    ((E)->pKey) // NOT USED */
8695/* #define sqliteHashKeysize(E) ((E)->nKey)  // NOT USED */
8696
8697/*
8698** Number of entries in a hash table
8699*/
8700/* #define sqliteHashCount(H)  ((H)->count) // NOT USED */
8701
8702#endif /* _SQLITE_HASH_H_ */
8703
8704/************** End of hash.h ************************************************/
8705/************** Continuing where we left off in sqliteInt.h ******************/
8706/************** Include parse.h in the middle of sqliteInt.h *****************/
8707/************** Begin file parse.h *******************************************/
8708#define TK_SEMI                             1
8709#define TK_EXPLAIN                          2
8710#define TK_QUERY                            3
8711#define TK_PLAN                             4
8712#define TK_BEGIN                            5
8713#define TK_TRANSACTION                      6
8714#define TK_DEFERRED                         7
8715#define TK_IMMEDIATE                        8
8716#define TK_EXCLUSIVE                        9
8717#define TK_COMMIT                          10
8718#define TK_END                             11
8719#define TK_ROLLBACK                        12
8720#define TK_SAVEPOINT                       13
8721#define TK_RELEASE                         14
8722#define TK_TO                              15
8723#define TK_TABLE                           16
8724#define TK_CREATE                          17
8725#define TK_IF                              18
8726#define TK_NOT                             19
8727#define TK_EXISTS                          20
8728#define TK_TEMP                            21
8729#define TK_LP                              22
8730#define TK_RP                              23
8731#define TK_AS                              24
8732#define TK_WITHOUT                         25
8733#define TK_COMMA                           26
8734#define TK_ID                              27
8735#define TK_INDEXED                         28
8736#define TK_ABORT                           29
8737#define TK_ACTION                          30
8738#define TK_AFTER                           31
8739#define TK_ANALYZE                         32
8740#define TK_ASC                             33
8741#define TK_ATTACH                          34
8742#define TK_BEFORE                          35
8743#define TK_BY                              36
8744#define TK_CASCADE                         37
8745#define TK_CAST                            38
8746#define TK_COLUMNKW                        39
8747#define TK_CONFLICT                        40
8748#define TK_DATABASE                        41
8749#define TK_DESC                            42
8750#define TK_DETACH                          43
8751#define TK_EACH                            44
8752#define TK_FAIL                            45
8753#define TK_FOR                             46
8754#define TK_IGNORE                          47
8755#define TK_INITIALLY                       48
8756#define TK_INSTEAD                         49
8757#define TK_LIKE_KW                         50
8758#define TK_MATCH                           51
8759#define TK_NO                              52
8760#define TK_KEY                             53
8761#define TK_OF                              54
8762#define TK_OFFSET                          55
8763#define TK_PRAGMA                          56
8764#define TK_RAISE                           57
8765#define TK_RECURSIVE                       58
8766#define TK_REPLACE                         59
8767#define TK_RESTRICT                        60
8768#define TK_ROW                             61
8769#define TK_TRIGGER                         62
8770#define TK_VACUUM                          63
8771#define TK_VIEW                            64
8772#define TK_VIRTUAL                         65
8773#define TK_WITH                            66
8774#define TK_REINDEX                         67
8775#define TK_RENAME                          68
8776#define TK_CTIME_KW                        69
8777#define TK_ANY                             70
8778#define TK_OR                              71
8779#define TK_AND                             72
8780#define TK_IS                              73
8781#define TK_BETWEEN                         74
8782#define TK_IN                              75
8783#define TK_ISNULL                          76
8784#define TK_NOTNULL                         77
8785#define TK_NE                              78
8786#define TK_EQ                              79
8787#define TK_GT                              80
8788#define TK_LE                              81
8789#define TK_LT                              82
8790#define TK_GE                              83
8791#define TK_ESCAPE                          84
8792#define TK_BITAND                          85
8793#define TK_BITOR                           86
8794#define TK_LSHIFT                          87
8795#define TK_RSHIFT                          88
8796#define TK_PLUS                            89
8797#define TK_MINUS                           90
8798#define TK_STAR                            91
8799#define TK_SLASH                           92
8800#define TK_REM                             93
8801#define TK_CONCAT                          94
8802#define TK_COLLATE                         95
8803#define TK_BITNOT                          96
8804#define TK_STRING                          97
8805#define TK_JOIN_KW                         98
8806#define TK_CONSTRAINT                      99
8807#define TK_DEFAULT                        100
8808#define TK_NULL                           101
8809#define TK_PRIMARY                        102
8810#define TK_UNIQUE                         103
8811#define TK_CHECK                          104
8812#define TK_REFERENCES                     105
8813#define TK_AUTOINCR                       106
8814#define TK_ON                             107
8815#define TK_INSERT                         108
8816#define TK_DELETE                         109
8817#define TK_UPDATE                         110
8818#define TK_SET                            111
8819#define TK_DEFERRABLE                     112
8820#define TK_FOREIGN                        113
8821#define TK_DROP                           114
8822#define TK_UNION                          115
8823#define TK_ALL                            116
8824#define TK_EXCEPT                         117
8825#define TK_INTERSECT                      118
8826#define TK_SELECT                         119
8827#define TK_VALUES                         120
8828#define TK_DISTINCT                       121
8829#define TK_DOT                            122
8830#define TK_FROM                           123
8831#define TK_JOIN                           124
8832#define TK_USING                          125
8833#define TK_ORDER                          126
8834#define TK_GROUP                          127
8835#define TK_HAVING                         128
8836#define TK_LIMIT                          129
8837#define TK_WHERE                          130
8838#define TK_INTO                           131
8839#define TK_INTEGER                        132
8840#define TK_FLOAT                          133
8841#define TK_BLOB                           134
8842#define TK_VARIABLE                       135
8843#define TK_CASE                           136
8844#define TK_WHEN                           137
8845#define TK_THEN                           138
8846#define TK_ELSE                           139
8847#define TK_INDEX                          140
8848#define TK_ALTER                          141
8849#define TK_ADD                            142
8850#define TK_TO_TEXT                        143
8851#define TK_TO_BLOB                        144
8852#define TK_TO_NUMERIC                     145
8853#define TK_TO_INT                         146
8854#define TK_TO_REAL                        147
8855#define TK_ISNOT                          148
8856#define TK_END_OF_FILE                    149
8857#define TK_ILLEGAL                        150
8858#define TK_SPACE                          151
8859#define TK_UNCLOSED_STRING                152
8860#define TK_FUNCTION                       153
8861#define TK_COLUMN                         154
8862#define TK_AGG_FUNCTION                   155
8863#define TK_AGG_COLUMN                     156
8864#define TK_UMINUS                         157
8865#define TK_UPLUS                          158
8866#define TK_REGISTER                       159
8867
8868/************** End of parse.h ***********************************************/
8869/************** Continuing where we left off in sqliteInt.h ******************/
8870#include <stdio.h>
8871#include <stdlib.h>
8872#include <string.h>
8873#include <assert.h>
8874#include <stddef.h>
8875
8876/*
8877** If compiling for a processor that lacks floating point support,
8878** substitute integer for floating-point
8879*/
8880#ifdef SQLITE_OMIT_FLOATING_POINT
8881# define double sqlite_int64
8882# define float sqlite_int64
8883# define LONGDOUBLE_TYPE sqlite_int64
8884# ifndef SQLITE_BIG_DBL
8885#   define SQLITE_BIG_DBL (((sqlite3_int64)1)<<50)
8886# endif
8887# define SQLITE_OMIT_DATETIME_FUNCS 1
8888# define SQLITE_OMIT_TRACE 1
8889# undef SQLITE_MIXED_ENDIAN_64BIT_FLOAT
8890# undef SQLITE_HAVE_ISNAN
8891#endif
8892#ifndef SQLITE_BIG_DBL
8893# define SQLITE_BIG_DBL (1e99)
8894#endif
8895
8896/*
8897** OMIT_TEMPDB is set to 1 if SQLITE_OMIT_TEMPDB is defined, or 0
8898** afterward. Having this macro allows us to cause the C compiler
8899** to omit code used by TEMP tables without messy #ifndef statements.
8900*/
8901#ifdef SQLITE_OMIT_TEMPDB
8902#define OMIT_TEMPDB 1
8903#else
8904#define OMIT_TEMPDB 0
8905#endif
8906
8907/*
8908** The "file format" number is an integer that is incremented whenever
8909** the VDBE-level file format changes.  The following macros define the
8910** the default file format for new databases and the maximum file format
8911** that the library can read.
8912*/
8913#define SQLITE_MAX_FILE_FORMAT 4
8914#ifndef SQLITE_DEFAULT_FILE_FORMAT
8915# define SQLITE_DEFAULT_FILE_FORMAT 4
8916#endif
8917
8918/*
8919** Determine whether triggers are recursive by default.  This can be
8920** changed at run-time using a pragma.
8921*/
8922#ifndef SQLITE_DEFAULT_RECURSIVE_TRIGGERS
8923# define SQLITE_DEFAULT_RECURSIVE_TRIGGERS 0
8924#endif
8925
8926/*
8927** Provide a default value for SQLITE_TEMP_STORE in case it is not specified
8928** on the command-line
8929*/
8930#ifndef SQLITE_TEMP_STORE
8931# define SQLITE_TEMP_STORE 1
8932# define SQLITE_TEMP_STORE_xc 1  /* Exclude from ctime.c */
8933#endif
8934
8935/*
8936** If no value has been provided for SQLITE_MAX_WORKER_THREADS, or if
8937** SQLITE_TEMP_STORE is set to 3 (never use temporary files), set it
8938** to zero.
8939*/
8940#if SQLITE_TEMP_STORE==3 || SQLITE_THREADSAFE==0
8941# undef SQLITE_MAX_WORKER_THREADS
8942# define SQLITE_MAX_WORKER_THREADS 0
8943#endif
8944#ifndef SQLITE_MAX_WORKER_THREADS
8945# define SQLITE_MAX_WORKER_THREADS 8
8946#endif
8947#ifndef SQLITE_DEFAULT_WORKER_THREADS
8948# define SQLITE_DEFAULT_WORKER_THREADS 0
8949#endif
8950#if SQLITE_DEFAULT_WORKER_THREADS>SQLITE_MAX_WORKER_THREADS
8951# undef SQLITE_MAX_WORKER_THREADS
8952# define SQLITE_MAX_WORKER_THREADS SQLITE_DEFAULT_WORKER_THREADS
8953#endif
8954
8955/*
8956** The default initial allocation for the pagecache when using separate
8957** pagecaches for each database connection.  A positive number is the
8958** number of pages.  A negative number N translations means that a buffer
8959** of -1024*N bytes is allocated and used for as many pages as it will hold.
8960*/
8961#ifndef SQLITE_DEFAULT_PCACHE_INITSZ
8962# define SQLITE_DEFAULT_PCACHE_INITSZ 100
8963#endif
8964
8965
8966/*
8967** GCC does not define the offsetof() macro so we'll have to do it
8968** ourselves.
8969*/
8970#ifndef offsetof
8971#define offsetof(STRUCTURE,FIELD) ((int)((char*)&((STRUCTURE*)0)->FIELD))
8972#endif
8973
8974/*
8975** Macros to compute minimum and maximum of two numbers.
8976*/
8977#define MIN(A,B) ((A)<(B)?(A):(B))
8978#define MAX(A,B) ((A)>(B)?(A):(B))
8979
8980/*
8981** Swap two objects of type TYPE.
8982*/
8983#define SWAP(TYPE,A,B) {TYPE t=A; A=B; B=t;}
8984
8985/*
8986** Check to see if this machine uses EBCDIC.  (Yes, believe it or
8987** not, there are still machines out there that use EBCDIC.)
8988*/
8989#if 'A' == '\301'
8990# define SQLITE_EBCDIC 1
8991#else
8992# define SQLITE_ASCII 1
8993#endif
8994
8995/*
8996** Integers of known sizes.  These typedefs might change for architectures
8997** where the sizes very.  Preprocessor macros are available so that the
8998** types can be conveniently redefined at compile-type.  Like this:
8999**
9000**         cc '-DUINTPTR_TYPE=long long int' ...
9001*/
9002#ifndef UINT32_TYPE
9003# ifdef HAVE_UINT32_T
9004#  define UINT32_TYPE uint32_t
9005# else
9006#  define UINT32_TYPE unsigned int
9007# endif
9008#endif
9009#ifndef UINT16_TYPE
9010# ifdef HAVE_UINT16_T
9011#  define UINT16_TYPE uint16_t
9012# else
9013#  define UINT16_TYPE unsigned short int
9014# endif
9015#endif
9016#ifndef INT16_TYPE
9017# ifdef HAVE_INT16_T
9018#  define INT16_TYPE int16_t
9019# else
9020#  define INT16_TYPE short int
9021# endif
9022#endif
9023#ifndef UINT8_TYPE
9024# ifdef HAVE_UINT8_T
9025#  define UINT8_TYPE uint8_t
9026# else
9027#  define UINT8_TYPE unsigned char
9028# endif
9029#endif
9030#ifndef INT8_TYPE
9031# ifdef HAVE_INT8_T
9032#  define INT8_TYPE int8_t
9033# else
9034#  define INT8_TYPE signed char
9035# endif
9036#endif
9037#ifndef LONGDOUBLE_TYPE
9038# define LONGDOUBLE_TYPE long double
9039#endif
9040typedef sqlite_int64 i64;          /* 8-byte signed integer */
9041typedef sqlite_uint64 u64;         /* 8-byte unsigned integer */
9042typedef UINT32_TYPE u32;           /* 4-byte unsigned integer */
9043typedef UINT16_TYPE u16;           /* 2-byte unsigned integer */
9044typedef INT16_TYPE i16;            /* 2-byte signed integer */
9045typedef UINT8_TYPE u8;             /* 1-byte unsigned integer */
9046typedef INT8_TYPE i8;              /* 1-byte signed integer */
9047
9048/*
9049** SQLITE_MAX_U32 is a u64 constant that is the maximum u64 value
9050** that can be stored in a u32 without loss of data.  The value
9051** is 0x00000000ffffffff.  But because of quirks of some compilers, we
9052** have to specify the value in the less intuitive manner shown:
9053*/
9054#define SQLITE_MAX_U32  ((((u64)1)<<32)-1)
9055
9056/*
9057** The datatype used to store estimates of the number of rows in a
9058** table or index.  This is an unsigned integer type.  For 99.9% of
9059** the world, a 32-bit integer is sufficient.  But a 64-bit integer
9060** can be used at compile-time if desired.
9061*/
9062#ifdef SQLITE_64BIT_STATS
9063 typedef u64 tRowcnt;    /* 64-bit only if requested at compile-time */
9064#else
9065 typedef u32 tRowcnt;    /* 32-bit is the default */
9066#endif
9067
9068/*
9069** Estimated quantities used for query planning are stored as 16-bit
9070** logarithms.  For quantity X, the value stored is 10*log2(X).  This
9071** gives a possible range of values of approximately 1.0e986 to 1e-986.
9072** But the allowed values are "grainy".  Not every value is representable.
9073** For example, quantities 16 and 17 are both represented by a LogEst
9074** of 40.  However, since LogEst quantities are suppose to be estimates,
9075** not exact values, this imprecision is not a problem.
9076**
9077** "LogEst" is short for "Logarithmic Estimate".
9078**
9079** Examples:
9080**      1 -> 0              20 -> 43          10000 -> 132
9081**      2 -> 10             25 -> 46          25000 -> 146
9082**      3 -> 16            100 -> 66        1000000 -> 199
9083**      4 -> 20           1000 -> 99        1048576 -> 200
9084**     10 -> 33           1024 -> 100    4294967296 -> 320
9085**
9086** The LogEst can be negative to indicate fractional values.
9087** Examples:
9088**
9089**    0.5 -> -10           0.1 -> -33        0.0625 -> -40
9090*/
9091typedef INT16_TYPE LogEst;
9092
9093/*
9094** Set the SQLITE_PTRSIZE macro to the number of bytes in a pointer
9095*/
9096#ifndef SQLITE_PTRSIZE
9097# if defined(__SIZEOF_POINTER__)
9098#   define SQLITE_PTRSIZE __SIZEOF_POINTER__
9099# elif defined(i386)     || defined(__i386__)   || defined(_M_IX86) ||    \
9100       defined(_M_ARM)   || defined(__arm__)    || defined(__x86)
9101#   define SQLITE_PTRSIZE 4
9102# else
9103#   define SQLITE_PTRSIZE 8
9104# endif
9105#endif
9106
9107/*
9108** Macros to determine whether the machine is big or little endian,
9109** and whether or not that determination is run-time or compile-time.
9110**
9111** For best performance, an attempt is made to guess at the byte-order
9112** using C-preprocessor macros.  If that is unsuccessful, or if
9113** -DSQLITE_RUNTIME_BYTEORDER=1 is set, then byte-order is determined
9114** at run-time.
9115*/
9116#ifdef SQLITE_AMALGAMATION
9117SQLITE_PRIVATE const int sqlite3one = 1;
9118#else
9119SQLITE_PRIVATE const int sqlite3one;
9120#endif
9121#if (defined(i386)     || defined(__i386__)   || defined(_M_IX86) ||    \
9122     defined(__x86_64) || defined(__x86_64__) || defined(_M_X64)  ||    \
9123     defined(_M_AMD64) || defined(_M_ARM)     || defined(__x86)   ||    \
9124     defined(__arm__)) && !defined(SQLITE_RUNTIME_BYTEORDER)
9125# define SQLITE_BYTEORDER    1234
9126# define SQLITE_BIGENDIAN    0
9127# define SQLITE_LITTLEENDIAN 1
9128# define SQLITE_UTF16NATIVE  SQLITE_UTF16LE
9129#endif
9130#if (defined(sparc)    || defined(__ppc__))  \
9131    && !defined(SQLITE_RUNTIME_BYTEORDER)
9132# define SQLITE_BYTEORDER    4321
9133# define SQLITE_BIGENDIAN    1
9134# define SQLITE_LITTLEENDIAN 0
9135# define SQLITE_UTF16NATIVE  SQLITE_UTF16BE
9136#endif
9137#if !defined(SQLITE_BYTEORDER)
9138# define SQLITE_BYTEORDER    0     /* 0 means "unknown at compile-time" */
9139# define SQLITE_BIGENDIAN    (*(char *)(&sqlite3one)==0)
9140# define SQLITE_LITTLEENDIAN (*(char *)(&sqlite3one)==1)
9141# define SQLITE_UTF16NATIVE  (SQLITE_BIGENDIAN?SQLITE_UTF16BE:SQLITE_UTF16LE)
9142#endif
9143
9144/*
9145** Constants for the largest and smallest possible 64-bit signed integers.
9146** These macros are designed to work correctly on both 32-bit and 64-bit
9147** compilers.
9148*/
9149#define LARGEST_INT64  (0xffffffff|(((i64)0x7fffffff)<<32))
9150#define SMALLEST_INT64 (((i64)-1) - LARGEST_INT64)
9151
9152/*
9153** Round up a number to the next larger multiple of 8.  This is used
9154** to force 8-byte alignment on 64-bit architectures.
9155*/
9156#define ROUND8(x)     (((x)+7)&~7)
9157
9158/*
9159** Round down to the nearest multiple of 8
9160*/
9161#define ROUNDDOWN8(x) ((x)&~7)
9162
9163/*
9164** Assert that the pointer X is aligned to an 8-byte boundary.  This
9165** macro is used only within assert() to verify that the code gets
9166** all alignment restrictions correct.
9167**
9168** Except, if SQLITE_4_BYTE_ALIGNED_MALLOC is defined, then the
9169** underlying malloc() implementation might return us 4-byte aligned
9170** pointers.  In that case, only verify 4-byte alignment.
9171*/
9172#ifdef SQLITE_4_BYTE_ALIGNED_MALLOC
9173# define EIGHT_BYTE_ALIGNMENT(X)   ((((char*)(X) - (char*)0)&3)==0)
9174#else
9175# define EIGHT_BYTE_ALIGNMENT(X)   ((((char*)(X) - (char*)0)&7)==0)
9176#endif
9177
9178/*
9179** Disable MMAP on platforms where it is known to not work
9180*/
9181#if defined(__OpenBSD__) || defined(__QNXNTO__)
9182# undef SQLITE_MAX_MMAP_SIZE
9183# define SQLITE_MAX_MMAP_SIZE 0
9184#endif
9185
9186/*
9187** Default maximum size of memory used by memory-mapped I/O in the VFS
9188*/
9189#ifdef __APPLE__
9190# include <TargetConditionals.h>
9191# if TARGET_OS_IPHONE
9192#   undef SQLITE_MAX_MMAP_SIZE
9193#   define SQLITE_MAX_MMAP_SIZE 0
9194# endif
9195#endif
9196#ifndef SQLITE_MAX_MMAP_SIZE
9197# if defined(__linux__) \
9198  || defined(_WIN32) \
9199  || (defined(__APPLE__) && defined(__MACH__)) \
9200  || defined(__sun) \
9201  || defined(__FreeBSD__) \
9202  || defined(__DragonFly__)
9203#   define SQLITE_MAX_MMAP_SIZE 0x7fff0000  /* 2147418112 */
9204# else
9205#   define SQLITE_MAX_MMAP_SIZE 0
9206# endif
9207# define SQLITE_MAX_MMAP_SIZE_xc 1 /* exclude from ctime.c */
9208#endif
9209
9210/*
9211** The default MMAP_SIZE is zero on all platforms.  Or, even if a larger
9212** default MMAP_SIZE is specified at compile-time, make sure that it does
9213** not exceed the maximum mmap size.
9214*/
9215#ifndef SQLITE_DEFAULT_MMAP_SIZE
9216# define SQLITE_DEFAULT_MMAP_SIZE 0
9217# define SQLITE_DEFAULT_MMAP_SIZE_xc 1  /* Exclude from ctime.c */
9218#endif
9219#if SQLITE_DEFAULT_MMAP_SIZE>SQLITE_MAX_MMAP_SIZE
9220# undef SQLITE_DEFAULT_MMAP_SIZE
9221# define SQLITE_DEFAULT_MMAP_SIZE SQLITE_MAX_MMAP_SIZE
9222#endif
9223
9224/*
9225** Only one of SQLITE_ENABLE_STAT3 or SQLITE_ENABLE_STAT4 can be defined.
9226** Priority is given to SQLITE_ENABLE_STAT4.  If either are defined, also
9227** define SQLITE_ENABLE_STAT3_OR_STAT4
9228*/
9229#ifdef SQLITE_ENABLE_STAT4
9230# undef SQLITE_ENABLE_STAT3
9231# define SQLITE_ENABLE_STAT3_OR_STAT4 1
9232#elif SQLITE_ENABLE_STAT3
9233# define SQLITE_ENABLE_STAT3_OR_STAT4 1
9234#elif SQLITE_ENABLE_STAT3_OR_STAT4
9235# undef SQLITE_ENABLE_STAT3_OR_STAT4
9236#endif
9237
9238/*
9239** SELECTTRACE_ENABLED will be either 1 or 0 depending on whether or not
9240** the Select query generator tracing logic is turned on.
9241*/
9242#if defined(SQLITE_DEBUG) || defined(SQLITE_ENABLE_SELECTTRACE)
9243# define SELECTTRACE_ENABLED 1
9244#else
9245# define SELECTTRACE_ENABLED 0
9246#endif
9247
9248/*
9249** An instance of the following structure is used to store the busy-handler
9250** callback for a given sqlite handle.
9251**
9252** The sqlite.busyHandler member of the sqlite struct contains the busy
9253** callback for the database handle. Each pager opened via the sqlite
9254** handle is passed a pointer to sqlite.busyHandler. The busy-handler
9255** callback is currently invoked only from within pager.c.
9256*/
9257typedef struct BusyHandler BusyHandler;
9258struct BusyHandler {
9259  int (*xFunc)(void *,int);  /* The busy callback */
9260  void *pArg;                /* First arg to busy callback */
9261  int nBusy;                 /* Incremented with each busy call */
9262};
9263
9264/*
9265** Name of the master database table.  The master database table
9266** is a special table that holds the names and attributes of all
9267** user tables and indices.
9268*/
9269#define MASTER_NAME       "sqlite_master"
9270#define TEMP_MASTER_NAME  "sqlite_temp_master"
9271
9272/*
9273** The root-page of the master database table.
9274*/
9275#define MASTER_ROOT       1
9276
9277/*
9278** The name of the schema table.
9279*/
9280#define SCHEMA_TABLE(x)  ((!OMIT_TEMPDB)&&(x==1)?TEMP_MASTER_NAME:MASTER_NAME)
9281
9282/*
9283** A convenience macro that returns the number of elements in
9284** an array.
9285*/
9286#define ArraySize(X)    ((int)(sizeof(X)/sizeof(X[0])))
9287
9288/*
9289** Determine if the argument is a power of two
9290*/
9291#define IsPowerOfTwo(X) (((X)&((X)-1))==0)
9292
9293/*
9294** The following value as a destructor means to use sqlite3DbFree().
9295** The sqlite3DbFree() routine requires two parameters instead of the
9296** one parameter that destructors normally want.  So we have to introduce
9297** this magic value that the code knows to handle differently.  Any
9298** pointer will work here as long as it is distinct from SQLITE_STATIC
9299** and SQLITE_TRANSIENT.
9300*/
9301#define SQLITE_DYNAMIC   ((sqlite3_destructor_type)sqlite3MallocSize)
9302
9303/*
9304** When SQLITE_OMIT_WSD is defined, it means that the target platform does
9305** not support Writable Static Data (WSD) such as global and static variables.
9306** All variables must either be on the stack or dynamically allocated from
9307** the heap.  When WSD is unsupported, the variable declarations scattered
9308** throughout the SQLite code must become constants instead.  The SQLITE_WSD
9309** macro is used for this purpose.  And instead of referencing the variable
9310** directly, we use its constant as a key to lookup the run-time allocated
9311** buffer that holds real variable.  The constant is also the initializer
9312** for the run-time allocated buffer.
9313**
9314** In the usual case where WSD is supported, the SQLITE_WSD and GLOBAL
9315** macros become no-ops and have zero performance impact.
9316*/
9317#ifdef SQLITE_OMIT_WSD
9318  #define SQLITE_WSD const
9319  #define GLOBAL(t,v) (*(t*)sqlite3_wsd_find((void*)&(v), sizeof(v)))
9320  #define sqlite3GlobalConfig GLOBAL(struct Sqlite3Config, sqlite3Config)
9321SQLITE_API int SQLITE_STDCALL sqlite3_wsd_init(int N, int J);
9322SQLITE_API void *SQLITE_STDCALL sqlite3_wsd_find(void *K, int L);
9323#else
9324  #define SQLITE_WSD
9325  #define GLOBAL(t,v) v
9326  #define sqlite3GlobalConfig sqlite3Config
9327#endif
9328
9329/*
9330** The following macros are used to suppress compiler warnings and to
9331** make it clear to human readers when a function parameter is deliberately
9332** left unused within the body of a function. This usually happens when
9333** a function is called via a function pointer. For example the
9334** implementation of an SQL aggregate step callback may not use the
9335** parameter indicating the number of arguments passed to the aggregate,
9336** if it knows that this is enforced elsewhere.
9337**
9338** When a function parameter is not used at all within the body of a function,
9339** it is generally named "NotUsed" or "NotUsed2" to make things even clearer.
9340** However, these macros may also be used to suppress warnings related to
9341** parameters that may or may not be used depending on compilation options.
9342** For example those parameters only used in assert() statements. In these
9343** cases the parameters are named as per the usual conventions.
9344*/
9345#define UNUSED_PARAMETER(x) (void)(x)
9346#define UNUSED_PARAMETER2(x,y) UNUSED_PARAMETER(x),UNUSED_PARAMETER(y)
9347
9348/*
9349** Forward references to structures
9350*/
9351typedef struct AggInfo AggInfo;
9352typedef struct AuthContext AuthContext;
9353typedef struct AutoincInfo AutoincInfo;
9354typedef struct Bitvec Bitvec;
9355typedef struct CollSeq CollSeq;
9356typedef struct Column Column;
9357typedef struct Db Db;
9358typedef struct Schema Schema;
9359typedef struct Expr Expr;
9360typedef struct ExprList ExprList;
9361typedef struct ExprSpan ExprSpan;
9362typedef struct FKey FKey;
9363typedef struct FuncDestructor FuncDestructor;
9364typedef struct FuncDef FuncDef;
9365typedef struct FuncDefHash FuncDefHash;
9366typedef struct IdList IdList;
9367typedef struct Index Index;
9368typedef struct IndexSample IndexSample;
9369typedef struct KeyClass KeyClass;
9370typedef struct KeyInfo KeyInfo;
9371typedef struct Lookaside Lookaside;
9372typedef struct LookasideSlot LookasideSlot;
9373typedef struct Module Module;
9374typedef struct NameContext NameContext;
9375typedef struct Parse Parse;
9376typedef struct PrintfArguments PrintfArguments;
9377typedef struct RowSet RowSet;
9378typedef struct Savepoint Savepoint;
9379typedef struct Select Select;
9380typedef struct SQLiteThread SQLiteThread;
9381typedef struct SelectDest SelectDest;
9382typedef struct SrcList SrcList;
9383typedef struct StrAccum StrAccum;
9384typedef struct Table Table;
9385typedef struct TableLock TableLock;
9386typedef struct Token Token;
9387typedef struct TreeView TreeView;
9388typedef struct Trigger Trigger;
9389typedef struct TriggerPrg TriggerPrg;
9390typedef struct TriggerStep TriggerStep;
9391typedef struct UnpackedRecord UnpackedRecord;
9392typedef struct VTable VTable;
9393typedef struct VtabCtx VtabCtx;
9394typedef struct Walker Walker;
9395typedef struct WhereInfo WhereInfo;
9396typedef struct With With;
9397
9398/*
9399** Defer sourcing vdbe.h and btree.h until after the "u8" and
9400** "BusyHandler" typedefs. vdbe.h also requires a few of the opaque
9401** pointer types (i.e. FuncDef) defined above.
9402*/
9403/************** Include btree.h in the middle of sqliteInt.h *****************/
9404/************** Begin file btree.h *******************************************/
9405/*
9406** 2001 September 15
9407**
9408** The author disclaims copyright to this source code.  In place of
9409** a legal notice, here is a blessing:
9410**
9411**    May you do good and not evil.
9412**    May you find forgiveness for yourself and forgive others.
9413**    May you share freely, never taking more than you give.
9414**
9415*************************************************************************
9416** This header file defines the interface that the sqlite B-Tree file
9417** subsystem.  See comments in the source code for a detailed description
9418** of what each interface routine does.
9419*/
9420#ifndef _BTREE_H_
9421#define _BTREE_H_
9422
9423/* TODO: This definition is just included so other modules compile. It
9424** needs to be revisited.
9425*/
9426#define SQLITE_N_BTREE_META 16
9427
9428/*
9429** If defined as non-zero, auto-vacuum is enabled by default. Otherwise
9430** it must be turned on for each database using "PRAGMA auto_vacuum = 1".
9431*/
9432#ifndef SQLITE_DEFAULT_AUTOVACUUM
9433  #define SQLITE_DEFAULT_AUTOVACUUM 0
9434#endif
9435
9436#define BTREE_AUTOVACUUM_NONE 0        /* Do not do auto-vacuum */
9437#define BTREE_AUTOVACUUM_FULL 1        /* Do full auto-vacuum */
9438#define BTREE_AUTOVACUUM_INCR 2        /* Incremental vacuum */
9439
9440/*
9441** Forward declarations of structure
9442*/
9443typedef struct Btree Btree;
9444typedef struct BtCursor BtCursor;
9445typedef struct BtShared BtShared;
9446
9447
9448SQLITE_PRIVATE int sqlite3BtreeOpen(
9449  sqlite3_vfs *pVfs,       /* VFS to use with this b-tree */
9450  const char *zFilename,   /* Name of database file to open */
9451  sqlite3 *db,             /* Associated database connection */
9452  Btree **ppBtree,         /* Return open Btree* here */
9453  int flags,               /* Flags */
9454  int vfsFlags             /* Flags passed through to VFS open */
9455);
9456
9457/* The flags parameter to sqlite3BtreeOpen can be the bitwise or of the
9458** following values.
9459**
9460** NOTE:  These values must match the corresponding PAGER_ values in
9461** pager.h.
9462*/
9463#define BTREE_OMIT_JOURNAL  1  /* Do not create or use a rollback journal */
9464#define BTREE_MEMORY        2  /* This is an in-memory DB */
9465#define BTREE_SINGLE        4  /* The file contains at most 1 b-tree */
9466#define BTREE_UNORDERED     8  /* Use of a hash implementation is OK */
9467
9468SQLITE_PRIVATE int sqlite3BtreeClose(Btree*);
9469SQLITE_PRIVATE int sqlite3BtreeSetCacheSize(Btree*,int);
9470#if SQLITE_MAX_MMAP_SIZE>0
9471SQLITE_PRIVATE   int sqlite3BtreeSetMmapLimit(Btree*,sqlite3_int64);
9472#endif
9473SQLITE_PRIVATE int sqlite3BtreeSetPagerFlags(Btree*,unsigned);
9474SQLITE_PRIVATE int sqlite3BtreeSyncDisabled(Btree*);
9475SQLITE_PRIVATE int sqlite3BtreeSetPageSize(Btree *p, int nPagesize, int nReserve, int eFix);
9476SQLITE_PRIVATE int sqlite3BtreeGetPageSize(Btree*);
9477SQLITE_PRIVATE int sqlite3BtreeMaxPageCount(Btree*,int);
9478SQLITE_PRIVATE u32 sqlite3BtreeLastPage(Btree*);
9479SQLITE_PRIVATE int sqlite3BtreeSecureDelete(Btree*,int);
9480SQLITE_PRIVATE int sqlite3BtreeGetOptimalReserve(Btree*);
9481SQLITE_PRIVATE int sqlite3BtreeGetReserveNoMutex(Btree *p);
9482SQLITE_PRIVATE int sqlite3BtreeSetAutoVacuum(Btree *, int);
9483SQLITE_PRIVATE int sqlite3BtreeGetAutoVacuum(Btree *);
9484SQLITE_PRIVATE int sqlite3BtreeBeginTrans(Btree*,int);
9485SQLITE_PRIVATE int sqlite3BtreeCommitPhaseOne(Btree*, const char *zMaster);
9486SQLITE_PRIVATE int sqlite3BtreeCommitPhaseTwo(Btree*, int);
9487SQLITE_PRIVATE int sqlite3BtreeCommit(Btree*);
9488SQLITE_PRIVATE int sqlite3BtreeRollback(Btree*,int,int);
9489SQLITE_PRIVATE int sqlite3BtreeBeginStmt(Btree*,int);
9490SQLITE_PRIVATE int sqlite3BtreeCreateTable(Btree*, int*, int flags);
9491SQLITE_PRIVATE int sqlite3BtreeIsInTrans(Btree*);
9492SQLITE_PRIVATE int sqlite3BtreeIsInReadTrans(Btree*);
9493SQLITE_PRIVATE int sqlite3BtreeIsInBackup(Btree*);
9494SQLITE_PRIVATE void *sqlite3BtreeSchema(Btree *, int, void(*)(void *));
9495SQLITE_PRIVATE int sqlite3BtreeSchemaLocked(Btree *pBtree);
9496SQLITE_PRIVATE int sqlite3BtreeLockTable(Btree *pBtree, int iTab, u8 isWriteLock);
9497SQLITE_PRIVATE int sqlite3BtreeSavepoint(Btree *, int, int);
9498
9499SQLITE_PRIVATE const char *sqlite3BtreeGetFilename(Btree *);
9500SQLITE_PRIVATE const char *sqlite3BtreeGetJournalname(Btree *);
9501SQLITE_PRIVATE int sqlite3BtreeCopyFile(Btree *, Btree *);
9502
9503SQLITE_PRIVATE int sqlite3BtreeIncrVacuum(Btree *);
9504
9505/* The flags parameter to sqlite3BtreeCreateTable can be the bitwise OR
9506** of the flags shown below.
9507**
9508** Every SQLite table must have either BTREE_INTKEY or BTREE_BLOBKEY set.
9509** With BTREE_INTKEY, the table key is a 64-bit integer and arbitrary data
9510** is stored in the leaves.  (BTREE_INTKEY is used for SQL tables.)  With
9511** BTREE_BLOBKEY, the key is an arbitrary BLOB and no content is stored
9512** anywhere - the key is the content.  (BTREE_BLOBKEY is used for SQL
9513** indices.)
9514*/
9515#define BTREE_INTKEY     1    /* Table has only 64-bit signed integer keys */
9516#define BTREE_BLOBKEY    2    /* Table has keys only - no data */
9517
9518SQLITE_PRIVATE int sqlite3BtreeDropTable(Btree*, int, int*);
9519SQLITE_PRIVATE int sqlite3BtreeClearTable(Btree*, int, int*);
9520SQLITE_PRIVATE int sqlite3BtreeClearTableOfCursor(BtCursor*);
9521SQLITE_PRIVATE int sqlite3BtreeTripAllCursors(Btree*, int, int);
9522
9523SQLITE_PRIVATE void sqlite3BtreeGetMeta(Btree *pBtree, int idx, u32 *pValue);
9524SQLITE_PRIVATE int sqlite3BtreeUpdateMeta(Btree*, int idx, u32 value);
9525
9526SQLITE_PRIVATE int sqlite3BtreeNewDb(Btree *p);
9527
9528/*
9529** The second parameter to sqlite3BtreeGetMeta or sqlite3BtreeUpdateMeta
9530** should be one of the following values. The integer values are assigned
9531** to constants so that the offset of the corresponding field in an
9532** SQLite database header may be found using the following formula:
9533**
9534**   offset = 36 + (idx * 4)
9535**
9536** For example, the free-page-count field is located at byte offset 36 of
9537** the database file header. The incr-vacuum-flag field is located at
9538** byte offset 64 (== 36+4*7).
9539**
9540** The BTREE_DATA_VERSION value is not really a value stored in the header.
9541** It is a read-only number computed by the pager.  But we merge it with
9542** the header value access routines since its access pattern is the same.
9543** Call it a "virtual meta value".
9544*/
9545#define BTREE_FREE_PAGE_COUNT     0
9546#define BTREE_SCHEMA_VERSION      1
9547#define BTREE_FILE_FORMAT         2
9548#define BTREE_DEFAULT_CACHE_SIZE  3
9549#define BTREE_LARGEST_ROOT_PAGE   4
9550#define BTREE_TEXT_ENCODING       5
9551#define BTREE_USER_VERSION        6
9552#define BTREE_INCR_VACUUM         7
9553#define BTREE_APPLICATION_ID      8
9554#define BTREE_DATA_VERSION        15  /* A virtual meta-value */
9555
9556/*
9557** Values that may be OR'd together to form the second argument of an
9558** sqlite3BtreeCursorHints() call.
9559**
9560** The BTREE_BULKLOAD flag is set on index cursors when the index is going
9561** to be filled with content that is already in sorted order.
9562**
9563** The BTREE_SEEK_EQ flag is set on cursors that will get OP_SeekGE or
9564** OP_SeekLE opcodes for a range search, but where the range of entries
9565** selected will all have the same key.  In other words, the cursor will
9566** be used only for equality key searches.
9567**
9568*/
9569#define BTREE_BULKLOAD 0x00000001  /* Used to full index in sorted order */
9570#define BTREE_SEEK_EQ  0x00000002  /* EQ seeks only - no range seeks */
9571
9572SQLITE_PRIVATE int sqlite3BtreeCursor(
9573  Btree*,                              /* BTree containing table to open */
9574  int iTable,                          /* Index of root page */
9575  int wrFlag,                          /* 1 for writing.  0 for read-only */
9576  struct KeyInfo*,                     /* First argument to compare function */
9577  BtCursor *pCursor                    /* Space to write cursor structure */
9578);
9579SQLITE_PRIVATE int sqlite3BtreeCursorSize(void);
9580SQLITE_PRIVATE void sqlite3BtreeCursorZero(BtCursor*);
9581
9582SQLITE_PRIVATE int sqlite3BtreeCloseCursor(BtCursor*);
9583SQLITE_PRIVATE int sqlite3BtreeMovetoUnpacked(
9584  BtCursor*,
9585  UnpackedRecord *pUnKey,
9586  i64 intKey,
9587  int bias,
9588  int *pRes
9589);
9590SQLITE_PRIVATE int sqlite3BtreeCursorHasMoved(BtCursor*);
9591SQLITE_PRIVATE int sqlite3BtreeCursorRestore(BtCursor*, int*);
9592SQLITE_PRIVATE int sqlite3BtreeDelete(BtCursor*);
9593SQLITE_PRIVATE int sqlite3BtreeInsert(BtCursor*, const void *pKey, i64 nKey,
9594                                  const void *pData, int nData,
9595                                  int nZero, int bias, int seekResult);
9596SQLITE_PRIVATE int sqlite3BtreeFirst(BtCursor*, int *pRes);
9597SQLITE_PRIVATE int sqlite3BtreeLast(BtCursor*, int *pRes);
9598SQLITE_PRIVATE int sqlite3BtreeNext(BtCursor*, int *pRes);
9599SQLITE_PRIVATE int sqlite3BtreeEof(BtCursor*);
9600SQLITE_PRIVATE int sqlite3BtreePrevious(BtCursor*, int *pRes);
9601SQLITE_PRIVATE int sqlite3BtreeKeySize(BtCursor*, i64 *pSize);
9602SQLITE_PRIVATE int sqlite3BtreeKey(BtCursor*, u32 offset, u32 amt, void*);
9603SQLITE_PRIVATE const void *sqlite3BtreeKeyFetch(BtCursor*, u32 *pAmt);
9604SQLITE_PRIVATE const void *sqlite3BtreeDataFetch(BtCursor*, u32 *pAmt);
9605SQLITE_PRIVATE int sqlite3BtreeDataSize(BtCursor*, u32 *pSize);
9606SQLITE_PRIVATE int sqlite3BtreeData(BtCursor*, u32 offset, u32 amt, void*);
9607
9608SQLITE_PRIVATE char *sqlite3BtreeIntegrityCheck(Btree*, int *aRoot, int nRoot, int, int*);
9609SQLITE_PRIVATE struct Pager *sqlite3BtreePager(Btree*);
9610
9611SQLITE_PRIVATE int sqlite3BtreePutData(BtCursor*, u32 offset, u32 amt, void*);
9612SQLITE_PRIVATE void sqlite3BtreeIncrblobCursor(BtCursor *);
9613SQLITE_PRIVATE void sqlite3BtreeClearCursor(BtCursor *);
9614SQLITE_PRIVATE int sqlite3BtreeSetVersion(Btree *pBt, int iVersion);
9615SQLITE_PRIVATE void sqlite3BtreeCursorHints(BtCursor *, unsigned int mask);
9616#ifdef SQLITE_DEBUG
9617SQLITE_PRIVATE int sqlite3BtreeCursorHasHint(BtCursor*, unsigned int mask);
9618#endif
9619SQLITE_PRIVATE int sqlite3BtreeIsReadonly(Btree *pBt);
9620SQLITE_PRIVATE int sqlite3HeaderSizeBtree(void);
9621
9622#ifndef NDEBUG
9623SQLITE_PRIVATE int sqlite3BtreeCursorIsValid(BtCursor*);
9624#endif
9625
9626#ifndef SQLITE_OMIT_BTREECOUNT
9627SQLITE_PRIVATE int sqlite3BtreeCount(BtCursor *, i64 *);
9628#endif
9629
9630#ifdef SQLITE_TEST
9631SQLITE_PRIVATE int sqlite3BtreeCursorInfo(BtCursor*, int*, int);
9632SQLITE_PRIVATE void sqlite3BtreeCursorList(Btree*);
9633#endif
9634
9635#ifndef SQLITE_OMIT_WAL
9636SQLITE_PRIVATE   int sqlite3BtreeCheckpoint(Btree*, int, int *, int *);
9637#endif
9638
9639/*
9640** If we are not using shared cache, then there is no need to
9641** use mutexes to access the BtShared structures.  So make the
9642** Enter and Leave procedures no-ops.
9643*/
9644#ifndef SQLITE_OMIT_SHARED_CACHE
9645SQLITE_PRIVATE   void sqlite3BtreeEnter(Btree*);
9646SQLITE_PRIVATE   void sqlite3BtreeEnterAll(sqlite3*);
9647#else
9648# define sqlite3BtreeEnter(X)
9649# define sqlite3BtreeEnterAll(X)
9650#endif
9651
9652#if !defined(SQLITE_OMIT_SHARED_CACHE) && SQLITE_THREADSAFE
9653SQLITE_PRIVATE   int sqlite3BtreeSharable(Btree*);
9654SQLITE_PRIVATE   void sqlite3BtreeLeave(Btree*);
9655SQLITE_PRIVATE   void sqlite3BtreeEnterCursor(BtCursor*);
9656SQLITE_PRIVATE   void sqlite3BtreeLeaveCursor(BtCursor*);
9657SQLITE_PRIVATE   void sqlite3BtreeLeaveAll(sqlite3*);
9658#ifndef NDEBUG
9659  /* These routines are used inside assert() statements only. */
9660SQLITE_PRIVATE   int sqlite3BtreeHoldsMutex(Btree*);
9661SQLITE_PRIVATE   int sqlite3BtreeHoldsAllMutexes(sqlite3*);
9662SQLITE_PRIVATE   int sqlite3SchemaMutexHeld(sqlite3*,int,Schema*);
9663#endif
9664#else
9665
9666# define sqlite3BtreeSharable(X) 0
9667# define sqlite3BtreeLeave(X)
9668# define sqlite3BtreeEnterCursor(X)
9669# define sqlite3BtreeLeaveCursor(X)
9670# define sqlite3BtreeLeaveAll(X)
9671
9672# define sqlite3BtreeHoldsMutex(X) 1
9673# define sqlite3BtreeHoldsAllMutexes(X) 1
9674# define sqlite3SchemaMutexHeld(X,Y,Z) 1
9675#endif
9676
9677
9678#endif /* _BTREE_H_ */
9679
9680/************** End of btree.h ***********************************************/
9681/************** Continuing where we left off in sqliteInt.h ******************/
9682/************** Include vdbe.h in the middle of sqliteInt.h ******************/
9683/************** Begin file vdbe.h ********************************************/
9684/*
9685** 2001 September 15
9686**
9687** The author disclaims copyright to this source code.  In place of
9688** a legal notice, here is a blessing:
9689**
9690**    May you do good and not evil.
9691**    May you find forgiveness for yourself and forgive others.
9692**    May you share freely, never taking more than you give.
9693**
9694*************************************************************************
9695** Header file for the Virtual DataBase Engine (VDBE)
9696**
9697** This header defines the interface to the virtual database engine
9698** or VDBE.  The VDBE implements an abstract machine that runs a
9699** simple program to access and modify the underlying database.
9700*/
9701#ifndef _SQLITE_VDBE_H_
9702#define _SQLITE_VDBE_H_
9703/* #include <stdio.h> */
9704
9705/*
9706** A single VDBE is an opaque structure named "Vdbe".  Only routines
9707** in the source file sqliteVdbe.c are allowed to see the insides
9708** of this structure.
9709*/
9710typedef struct Vdbe Vdbe;
9711
9712/*
9713** The names of the following types declared in vdbeInt.h are required
9714** for the VdbeOp definition.
9715*/
9716typedef struct Mem Mem;
9717typedef struct SubProgram SubProgram;
9718
9719/*
9720** A single instruction of the virtual machine has an opcode
9721** and as many as three operands.  The instruction is recorded
9722** as an instance of the following structure:
9723*/
9724struct VdbeOp {
9725  u8 opcode;          /* What operation to perform */
9726  signed char p4type; /* One of the P4_xxx constants for p4 */
9727  u8 opflags;         /* Mask of the OPFLG_* flags in opcodes.h */
9728  u8 p5;              /* Fifth parameter is an unsigned character */
9729  int p1;             /* First operand */
9730  int p2;             /* Second parameter (often the jump destination) */
9731  int p3;             /* The third parameter */
9732  union p4union {     /* fourth parameter */
9733    int i;                 /* Integer value if p4type==P4_INT32 */
9734    void *p;               /* Generic pointer */
9735    char *z;               /* Pointer to data for string (char array) types */
9736    i64 *pI64;             /* Used when p4type is P4_INT64 */
9737    double *pReal;         /* Used when p4type is P4_REAL */
9738    FuncDef *pFunc;        /* Used when p4type is P4_FUNCDEF */
9739    sqlite3_context *pCtx; /* Used when p4type is P4_FUNCCTX */
9740    CollSeq *pColl;        /* Used when p4type is P4_COLLSEQ */
9741    Mem *pMem;             /* Used when p4type is P4_MEM */
9742    VTable *pVtab;         /* Used when p4type is P4_VTAB */
9743    KeyInfo *pKeyInfo;     /* Used when p4type is P4_KEYINFO */
9744    int *ai;               /* Used when p4type is P4_INTARRAY */
9745    SubProgram *pProgram;  /* Used when p4type is P4_SUBPROGRAM */
9746    int (*xAdvance)(BtCursor *, int *);
9747  } p4;
9748#ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
9749  char *zComment;          /* Comment to improve readability */
9750#endif
9751#ifdef VDBE_PROFILE
9752  u32 cnt;                 /* Number of times this instruction was executed */
9753  u64 cycles;              /* Total time spent executing this instruction */
9754#endif
9755#ifdef SQLITE_VDBE_COVERAGE
9756  int iSrcLine;            /* Source-code line that generated this opcode */
9757#endif
9758};
9759typedef struct VdbeOp VdbeOp;
9760
9761
9762/*
9763** A sub-routine used to implement a trigger program.
9764*/
9765struct SubProgram {
9766  VdbeOp *aOp;                  /* Array of opcodes for sub-program */
9767  int nOp;                      /* Elements in aOp[] */
9768  int nMem;                     /* Number of memory cells required */
9769  int nCsr;                     /* Number of cursors required */
9770  int nOnce;                    /* Number of OP_Once instructions */
9771  void *token;                  /* id that may be used to recursive triggers */
9772  SubProgram *pNext;            /* Next sub-program already visited */
9773};
9774
9775/*
9776** A smaller version of VdbeOp used for the VdbeAddOpList() function because
9777** it takes up less space.
9778*/
9779struct VdbeOpList {
9780  u8 opcode;          /* What operation to perform */
9781  signed char p1;     /* First operand */
9782  signed char p2;     /* Second parameter (often the jump destination) */
9783  signed char p3;     /* Third parameter */
9784};
9785typedef struct VdbeOpList VdbeOpList;
9786
9787/*
9788** Allowed values of VdbeOp.p4type
9789*/
9790#define P4_NOTUSED    0   /* The P4 parameter is not used */
9791#define P4_DYNAMIC  (-1)  /* Pointer to a string obtained from sqliteMalloc() */
9792#define P4_STATIC   (-2)  /* Pointer to a static string */
9793#define P4_COLLSEQ  (-4)  /* P4 is a pointer to a CollSeq structure */
9794#define P4_FUNCDEF  (-5)  /* P4 is a pointer to a FuncDef structure */
9795#define P4_KEYINFO  (-6)  /* P4 is a pointer to a KeyInfo structure */
9796#define P4_MEM      (-8)  /* P4 is a pointer to a Mem*    structure */
9797#define P4_TRANSIENT  0   /* P4 is a pointer to a transient string */
9798#define P4_VTAB     (-10) /* P4 is a pointer to an sqlite3_vtab structure */
9799#define P4_MPRINTF  (-11) /* P4 is a string obtained from sqlite3_mprintf() */
9800#define P4_REAL     (-12) /* P4 is a 64-bit floating point value */
9801#define P4_INT64    (-13) /* P4 is a 64-bit signed integer */
9802#define P4_INT32    (-14) /* P4 is a 32-bit signed integer */
9803#define P4_INTARRAY (-15) /* P4 is a vector of 32-bit integers */
9804#define P4_SUBPROGRAM  (-18) /* P4 is a pointer to a SubProgram structure */
9805#define P4_ADVANCE  (-19) /* P4 is a pointer to BtreeNext() or BtreePrev() */
9806#define P4_FUNCCTX  (-20) /* P4 is a pointer to an sqlite3_context object */
9807
9808/* Error message codes for OP_Halt */
9809#define P5_ConstraintNotNull 1
9810#define P5_ConstraintUnique  2
9811#define P5_ConstraintCheck   3
9812#define P5_ConstraintFK      4
9813
9814/*
9815** The Vdbe.aColName array contains 5n Mem structures, where n is the
9816** number of columns of data returned by the statement.
9817*/
9818#define COLNAME_NAME     0
9819#define COLNAME_DECLTYPE 1
9820#define COLNAME_DATABASE 2
9821#define COLNAME_TABLE    3
9822#define COLNAME_COLUMN   4
9823#ifdef SQLITE_ENABLE_COLUMN_METADATA
9824# define COLNAME_N        5      /* Number of COLNAME_xxx symbols */
9825#else
9826# ifdef SQLITE_OMIT_DECLTYPE
9827#   define COLNAME_N      1      /* Store only the name */
9828# else
9829#   define COLNAME_N      2      /* Store the name and decltype */
9830# endif
9831#endif
9832
9833/*
9834** The following macro converts a relative address in the p2 field
9835** of a VdbeOp structure into a negative number so that
9836** sqlite3VdbeAddOpList() knows that the address is relative.  Calling
9837** the macro again restores the address.
9838*/
9839#define ADDR(X)  (-1-(X))
9840
9841/*
9842** The makefile scans the vdbe.c source file and creates the "opcodes.h"
9843** header file that defines a number for each opcode used by the VDBE.
9844*/
9845/************** Include opcodes.h in the middle of vdbe.h ********************/
9846/************** Begin file opcodes.h *****************************************/
9847/* Automatically generated.  Do not edit */
9848/* See the mkopcodeh.awk script for details */
9849#define OP_Savepoint       1
9850#define OP_AutoCommit      2
9851#define OP_Transaction     3
9852#define OP_SorterNext      4
9853#define OP_PrevIfOpen      5
9854#define OP_NextIfOpen      6
9855#define OP_Prev            7
9856#define OP_Next            8
9857#define OP_Checkpoint      9
9858#define OP_JournalMode    10
9859#define OP_Vacuum         11
9860#define OP_VFilter        12 /* synopsis: iplan=r[P3] zplan='P4'           */
9861#define OP_VUpdate        13 /* synopsis: data=r[P3@P2]                    */
9862#define OP_Goto           14
9863#define OP_Gosub          15
9864#define OP_Return         16
9865#define OP_InitCoroutine  17
9866#define OP_EndCoroutine   18
9867#define OP_Not            19 /* same as TK_NOT, synopsis: r[P2]= !r[P1]    */
9868#define OP_Yield          20
9869#define OP_HaltIfNull     21 /* synopsis: if r[P3]=null halt               */
9870#define OP_Halt           22
9871#define OP_Integer        23 /* synopsis: r[P2]=P1                         */
9872#define OP_Int64          24 /* synopsis: r[P2]=P4                         */
9873#define OP_String         25 /* synopsis: r[P2]='P4' (len=P1)              */
9874#define OP_Null           26 /* synopsis: r[P2..P3]=NULL                   */
9875#define OP_SoftNull       27 /* synopsis: r[P1]=NULL                       */
9876#define OP_Blob           28 /* synopsis: r[P2]=P4 (len=P1)                */
9877#define OP_Variable       29 /* synopsis: r[P2]=parameter(P1,P4)           */
9878#define OP_Move           30 /* synopsis: r[P2@P3]=r[P1@P3]                */
9879#define OP_Copy           31 /* synopsis: r[P2@P3+1]=r[P1@P3+1]            */
9880#define OP_SCopy          32 /* synopsis: r[P2]=r[P1]                      */
9881#define OP_ResultRow      33 /* synopsis: output=r[P1@P2]                  */
9882#define OP_CollSeq        34
9883#define OP_Function0      35 /* synopsis: r[P3]=func(r[P2@P5])             */
9884#define OP_Function       36 /* synopsis: r[P3]=func(r[P2@P5])             */
9885#define OP_AddImm         37 /* synopsis: r[P1]=r[P1]+P2                   */
9886#define OP_MustBeInt      38
9887#define OP_RealAffinity   39
9888#define OP_Cast           40 /* synopsis: affinity(r[P1])                  */
9889#define OP_Permutation    41
9890#define OP_Compare        42 /* synopsis: r[P1@P3] <-> r[P2@P3]            */
9891#define OP_Jump           43
9892#define OP_Once           44
9893#define OP_If             45
9894#define OP_IfNot          46
9895#define OP_Column         47 /* synopsis: r[P3]=PX                         */
9896#define OP_Affinity       48 /* synopsis: affinity(r[P1@P2])               */
9897#define OP_MakeRecord     49 /* synopsis: r[P3]=mkrec(r[P1@P2])            */
9898#define OP_Count          50 /* synopsis: r[P2]=count()                    */
9899#define OP_ReadCookie     51
9900#define OP_SetCookie      52
9901#define OP_ReopenIdx      53 /* synopsis: root=P2 iDb=P3                   */
9902#define OP_OpenRead       54 /* synopsis: root=P2 iDb=P3                   */
9903#define OP_OpenWrite      55 /* synopsis: root=P2 iDb=P3                   */
9904#define OP_OpenAutoindex  56 /* synopsis: nColumn=P2                       */
9905#define OP_OpenEphemeral  57 /* synopsis: nColumn=P2                       */
9906#define OP_SorterOpen     58
9907#define OP_SequenceTest   59 /* synopsis: if( cursor[P1].ctr++ ) pc = P2   */
9908#define OP_OpenPseudo     60 /* synopsis: P3 columns in r[P2]              */
9909#define OP_Close          61
9910#define OP_ColumnsUsed    62
9911#define OP_SeekLT         63 /* synopsis: key=r[P3@P4]                     */
9912#define OP_SeekLE         64 /* synopsis: key=r[P3@P4]                     */
9913#define OP_SeekGE         65 /* synopsis: key=r[P3@P4]                     */
9914#define OP_SeekGT         66 /* synopsis: key=r[P3@P4]                     */
9915#define OP_Seek           67 /* synopsis: intkey=r[P2]                     */
9916#define OP_NoConflict     68 /* synopsis: key=r[P3@P4]                     */
9917#define OP_NotFound       69 /* synopsis: key=r[P3@P4]                     */
9918#define OP_Found          70 /* synopsis: key=r[P3@P4]                     */
9919#define OP_Or             71 /* same as TK_OR, synopsis: r[P3]=(r[P1] || r[P2]) */
9920#define OP_And            72 /* same as TK_AND, synopsis: r[P3]=(r[P1] && r[P2]) */
9921#define OP_NotExists      73 /* synopsis: intkey=r[P3]                     */
9922#define OP_Sequence       74 /* synopsis: r[P2]=cursor[P1].ctr++           */
9923#define OP_NewRowid       75 /* synopsis: r[P2]=rowid                      */
9924#define OP_IsNull         76 /* same as TK_ISNULL, synopsis: if r[P1]==NULL goto P2 */
9925#define OP_NotNull        77 /* same as TK_NOTNULL, synopsis: if r[P1]!=NULL goto P2 */
9926#define OP_Ne             78 /* same as TK_NE, synopsis: if r[P1]!=r[P3] goto P2 */
9927#define OP_Eq             79 /* same as TK_EQ, synopsis: if r[P1]==r[P3] goto P2 */
9928#define OP_Gt             80 /* same as TK_GT, synopsis: if r[P1]>r[P3] goto P2 */
9929#define OP_Le             81 /* same as TK_LE, synopsis: if r[P1]<=r[P3] goto P2 */
9930#define OP_Lt             82 /* same as TK_LT, synopsis: if r[P1]<r[P3] goto P2 */
9931#define OP_Ge             83 /* same as TK_GE, synopsis: if r[P1]>=r[P3] goto P2 */
9932#define OP_Insert         84 /* synopsis: intkey=r[P3] data=r[P2]          */
9933#define OP_BitAnd         85 /* same as TK_BITAND, synopsis: r[P3]=r[P1]&r[P2] */
9934#define OP_BitOr          86 /* same as TK_BITOR, synopsis: r[P3]=r[P1]|r[P2] */
9935#define OP_ShiftLeft      87 /* same as TK_LSHIFT, synopsis: r[P3]=r[P2]<<r[P1] */
9936#define OP_ShiftRight     88 /* same as TK_RSHIFT, synopsis: r[P3]=r[P2]>>r[P1] */
9937#define OP_Add            89 /* same as TK_PLUS, synopsis: r[P3]=r[P1]+r[P2] */
9938#define OP_Subtract       90 /* same as TK_MINUS, synopsis: r[P3]=r[P2]-r[P1] */
9939#define OP_Multiply       91 /* same as TK_STAR, synopsis: r[P3]=r[P1]*r[P2] */
9940#define OP_Divide         92 /* same as TK_SLASH, synopsis: r[P3]=r[P2]/r[P1] */
9941#define OP_Remainder      93 /* same as TK_REM, synopsis: r[P3]=r[P2]%r[P1] */
9942#define OP_Concat         94 /* same as TK_CONCAT, synopsis: r[P3]=r[P2]+r[P1] */
9943#define OP_InsertInt      95 /* synopsis: intkey=P3 data=r[P2]             */
9944#define OP_BitNot         96 /* same as TK_BITNOT, synopsis: r[P1]= ~r[P1] */
9945#define OP_String8        97 /* same as TK_STRING, synopsis: r[P2]='P4'    */
9946#define OP_Delete         98
9947#define OP_ResetCount     99
9948#define OP_SorterCompare 100 /* synopsis: if key(P1)!=trim(r[P3],P4) goto P2 */
9949#define OP_SorterData    101 /* synopsis: r[P2]=data                       */
9950#define OP_RowKey        102 /* synopsis: r[P2]=key                        */
9951#define OP_RowData       103 /* synopsis: r[P2]=data                       */
9952#define OP_Rowid         104 /* synopsis: r[P2]=rowid                      */
9953#define OP_NullRow       105
9954#define OP_Last          106
9955#define OP_SorterSort    107
9956#define OP_Sort          108
9957#define OP_Rewind        109
9958#define OP_SorterInsert  110
9959#define OP_IdxInsert     111 /* synopsis: key=r[P2]                        */
9960#define OP_IdxDelete     112 /* synopsis: key=r[P2@P3]                     */
9961#define OP_IdxRowid      113 /* synopsis: r[P2]=rowid                      */
9962#define OP_IdxLE         114 /* synopsis: key=r[P3@P4]                     */
9963#define OP_IdxGT         115 /* synopsis: key=r[P3@P4]                     */
9964#define OP_IdxLT         116 /* synopsis: key=r[P3@P4]                     */
9965#define OP_IdxGE         117 /* synopsis: key=r[P3@P4]                     */
9966#define OP_Destroy       118
9967#define OP_Clear         119
9968#define OP_ResetSorter   120
9969#define OP_CreateIndex   121 /* synopsis: r[P2]=root iDb=P1                */
9970#define OP_CreateTable   122 /* synopsis: r[P2]=root iDb=P1                */
9971#define OP_ParseSchema   123
9972#define OP_LoadAnalysis  124
9973#define OP_DropTable     125
9974#define OP_DropIndex     126
9975#define OP_DropTrigger   127
9976#define OP_IntegrityCk   128
9977#define OP_RowSetAdd     129 /* synopsis: rowset(P1)=r[P2]                 */
9978#define OP_RowSetRead    130 /* synopsis: r[P3]=rowset(P1)                 */
9979#define OP_RowSetTest    131 /* synopsis: if r[P3] in rowset(P1) goto P2   */
9980#define OP_Program       132
9981#define OP_Real          133 /* same as TK_FLOAT, synopsis: r[P2]=P4       */
9982#define OP_Param         134
9983#define OP_FkCounter     135 /* synopsis: fkctr[P1]+=P2                    */
9984#define OP_FkIfZero      136 /* synopsis: if fkctr[P1]==0 goto P2          */
9985#define OP_MemMax        137 /* synopsis: r[P1]=max(r[P1],r[P2])           */
9986#define OP_IfPos         138 /* synopsis: if r[P1]>0 goto P2               */
9987#define OP_IfNeg         139 /* synopsis: r[P1]+=P3, if r[P1]<0 goto P2    */
9988#define OP_IfNotZero     140 /* synopsis: if r[P1]!=0 then r[P1]+=P3, goto P2 */
9989#define OP_DecrJumpZero  141 /* synopsis: if (--r[P1])==0 goto P2          */
9990#define OP_JumpZeroIncr  142 /* synopsis: if (r[P1]++)==0 ) goto P2        */
9991#define OP_AggStep0      143 /* synopsis: accum=r[P3] step(r[P2@P5])       */
9992#define OP_AggStep       144 /* synopsis: accum=r[P3] step(r[P2@P5])       */
9993#define OP_AggFinal      145 /* synopsis: accum=r[P1] N=P2                 */
9994#define OP_IncrVacuum    146
9995#define OP_Expire        147
9996#define OP_TableLock     148 /* synopsis: iDb=P1 root=P2 write=P3          */
9997#define OP_VBegin        149
9998#define OP_VCreate       150
9999#define OP_VDestroy      151
10000#define OP_VOpen         152
10001#define OP_VColumn       153 /* synopsis: r[P3]=vcolumn(P2)                */
10002#define OP_VNext         154
10003#define OP_VRename       155
10004#define OP_Pagecount     156
10005#define OP_MaxPgcnt      157
10006#define OP_Init          158 /* synopsis: Start at P2                      */
10007#define OP_Noop          159
10008#define OP_Explain       160
10009
10010
10011/* Properties such as "out2" or "jump" that are specified in
10012** comments following the "case" for each opcode in the vdbe.c
10013** are encoded into bitvectors as follows:
10014*/
10015#define OPFLG_JUMP            0x0001  /* jump:  P2 holds jmp target */
10016#define OPFLG_IN1             0x0002  /* in1:   P1 is an input */
10017#define OPFLG_IN2             0x0004  /* in2:   P2 is an input */
10018#define OPFLG_IN3             0x0008  /* in3:   P3 is an input */
10019#define OPFLG_OUT2            0x0010  /* out2:  P2 is an output */
10020#define OPFLG_OUT3            0x0020  /* out3:  P3 is an output */
10021#define OPFLG_INITIALIZER {\
10022/*   0 */ 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x01, 0x01,\
10023/*   8 */ 0x01, 0x00, 0x10, 0x00, 0x01, 0x00, 0x01, 0x01,\
10024/*  16 */ 0x02, 0x01, 0x02, 0x12, 0x03, 0x08, 0x00, 0x10,\
10025/*  24 */ 0x10, 0x10, 0x10, 0x00, 0x10, 0x10, 0x00, 0x00,\
10026/*  32 */ 0x10, 0x00, 0x00, 0x00, 0x00, 0x02, 0x03, 0x02,\
10027/*  40 */ 0x02, 0x00, 0x00, 0x01, 0x01, 0x03, 0x03, 0x00,\
10028/*  48 */ 0x00, 0x00, 0x10, 0x10, 0x08, 0x00, 0x00, 0x00,\
10029/*  56 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x09,\
10030/*  64 */ 0x09, 0x09, 0x09, 0x04, 0x09, 0x09, 0x09, 0x26,\
10031/*  72 */ 0x26, 0x09, 0x10, 0x10, 0x03, 0x03, 0x0b, 0x0b,\
10032/*  80 */ 0x0b, 0x0b, 0x0b, 0x0b, 0x00, 0x26, 0x26, 0x26,\
10033/*  88 */ 0x26, 0x26, 0x26, 0x26, 0x26, 0x26, 0x26, 0x00,\
10034/*  96 */ 0x12, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\
10035/* 104 */ 0x10, 0x00, 0x01, 0x01, 0x01, 0x01, 0x04, 0x04,\
10036/* 112 */ 0x00, 0x10, 0x01, 0x01, 0x01, 0x01, 0x10, 0x00,\
10037/* 120 */ 0x00, 0x10, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00,\
10038/* 128 */ 0x00, 0x06, 0x23, 0x0b, 0x01, 0x10, 0x10, 0x00,\
10039/* 136 */ 0x01, 0x04, 0x03, 0x03, 0x03, 0x03, 0x03, 0x00,\
10040/* 144 */ 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,\
10041/* 152 */ 0x00, 0x00, 0x01, 0x00, 0x10, 0x10, 0x01, 0x00,\
10042/* 160 */ 0x00,}
10043
10044/************** End of opcodes.h *********************************************/
10045/************** Continuing where we left off in vdbe.h ***********************/
10046
10047/*
10048** Prototypes for the VDBE interface.  See comments on the implementation
10049** for a description of what each of these routines does.
10050*/
10051SQLITE_PRIVATE Vdbe *sqlite3VdbeCreate(Parse*);
10052SQLITE_PRIVATE int sqlite3VdbeAddOp0(Vdbe*,int);
10053SQLITE_PRIVATE int sqlite3VdbeAddOp1(Vdbe*,int,int);
10054SQLITE_PRIVATE int sqlite3VdbeAddOp2(Vdbe*,int,int,int);
10055SQLITE_PRIVATE int sqlite3VdbeAddOp3(Vdbe*,int,int,int,int);
10056SQLITE_PRIVATE int sqlite3VdbeAddOp4(Vdbe*,int,int,int,int,const char *zP4,int);
10057SQLITE_PRIVATE int sqlite3VdbeAddOp4Dup8(Vdbe*,int,int,int,int,const u8*,int);
10058SQLITE_PRIVATE int sqlite3VdbeAddOp4Int(Vdbe*,int,int,int,int,int);
10059SQLITE_PRIVATE int sqlite3VdbeAddOpList(Vdbe*, int nOp, VdbeOpList const *aOp, int iLineno);
10060SQLITE_PRIVATE void sqlite3VdbeAddParseSchemaOp(Vdbe*,int,char*);
10061SQLITE_PRIVATE void sqlite3VdbeChangeP1(Vdbe*, u32 addr, int P1);
10062SQLITE_PRIVATE void sqlite3VdbeChangeP2(Vdbe*, u32 addr, int P2);
10063SQLITE_PRIVATE void sqlite3VdbeChangeP3(Vdbe*, u32 addr, int P3);
10064SQLITE_PRIVATE void sqlite3VdbeChangeP5(Vdbe*, u8 P5);
10065SQLITE_PRIVATE void sqlite3VdbeJumpHere(Vdbe*, int addr);
10066SQLITE_PRIVATE void sqlite3VdbeChangeToNoop(Vdbe*, int addr);
10067SQLITE_PRIVATE int sqlite3VdbeDeletePriorOpcode(Vdbe*, u8 op);
10068SQLITE_PRIVATE void sqlite3VdbeChangeP4(Vdbe*, int addr, const char *zP4, int N);
10069SQLITE_PRIVATE void sqlite3VdbeSetP4KeyInfo(Parse*, Index*);
10070SQLITE_PRIVATE void sqlite3VdbeUsesBtree(Vdbe*, int);
10071SQLITE_PRIVATE VdbeOp *sqlite3VdbeGetOp(Vdbe*, int);
10072SQLITE_PRIVATE int sqlite3VdbeMakeLabel(Vdbe*);
10073SQLITE_PRIVATE void sqlite3VdbeRunOnlyOnce(Vdbe*);
10074SQLITE_PRIVATE void sqlite3VdbeDelete(Vdbe*);
10075SQLITE_PRIVATE void sqlite3VdbeClearObject(sqlite3*,Vdbe*);
10076SQLITE_PRIVATE void sqlite3VdbeMakeReady(Vdbe*,Parse*);
10077SQLITE_PRIVATE int sqlite3VdbeFinalize(Vdbe*);
10078SQLITE_PRIVATE void sqlite3VdbeResolveLabel(Vdbe*, int);
10079SQLITE_PRIVATE int sqlite3VdbeCurrentAddr(Vdbe*);
10080#ifdef SQLITE_DEBUG
10081SQLITE_PRIVATE   int sqlite3VdbeAssertMayAbort(Vdbe *, int);
10082#endif
10083SQLITE_PRIVATE void sqlite3VdbeResetStepResult(Vdbe*);
10084SQLITE_PRIVATE void sqlite3VdbeRewind(Vdbe*);
10085SQLITE_PRIVATE int sqlite3VdbeReset(Vdbe*);
10086SQLITE_PRIVATE void sqlite3VdbeSetNumCols(Vdbe*,int);
10087SQLITE_PRIVATE int sqlite3VdbeSetColName(Vdbe*, int, int, const char *, void(*)(void*));
10088SQLITE_PRIVATE void sqlite3VdbeCountChanges(Vdbe*);
10089SQLITE_PRIVATE sqlite3 *sqlite3VdbeDb(Vdbe*);
10090SQLITE_PRIVATE void sqlite3VdbeSetSql(Vdbe*, const char *z, int n, int);
10091SQLITE_PRIVATE void sqlite3VdbeSwap(Vdbe*,Vdbe*);
10092SQLITE_PRIVATE VdbeOp *sqlite3VdbeTakeOpArray(Vdbe*, int*, int*);
10093SQLITE_PRIVATE sqlite3_value *sqlite3VdbeGetBoundValue(Vdbe*, int, u8);
10094SQLITE_PRIVATE void sqlite3VdbeSetVarmask(Vdbe*, int);
10095#ifndef SQLITE_OMIT_TRACE
10096SQLITE_PRIVATE   char *sqlite3VdbeExpandSql(Vdbe*, const char*);
10097#endif
10098SQLITE_PRIVATE int sqlite3MemCompare(const Mem*, const Mem*, const CollSeq*);
10099
10100SQLITE_PRIVATE void sqlite3VdbeRecordUnpack(KeyInfo*,int,const void*,UnpackedRecord*);
10101SQLITE_PRIVATE int sqlite3VdbeRecordCompare(int,const void*,UnpackedRecord*);
10102SQLITE_PRIVATE int sqlite3VdbeRecordCompareWithSkip(int, const void *, UnpackedRecord *, int);
10103SQLITE_PRIVATE UnpackedRecord *sqlite3VdbeAllocUnpackedRecord(KeyInfo *, char *, int, char **);
10104
10105typedef int (*RecordCompare)(int,const void*,UnpackedRecord*);
10106SQLITE_PRIVATE RecordCompare sqlite3VdbeFindCompare(UnpackedRecord*);
10107
10108#ifndef SQLITE_OMIT_TRIGGER
10109SQLITE_PRIVATE void sqlite3VdbeLinkSubProgram(Vdbe *, SubProgram *);
10110#endif
10111
10112/* Use SQLITE_ENABLE_COMMENTS to enable generation of extra comments on
10113** each VDBE opcode.
10114**
10115** Use the SQLITE_ENABLE_MODULE_COMMENTS macro to see some extra no-op
10116** comments in VDBE programs that show key decision points in the code
10117** generator.
10118*/
10119#ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
10120SQLITE_PRIVATE   void sqlite3VdbeComment(Vdbe*, const char*, ...);
10121# define VdbeComment(X)  sqlite3VdbeComment X
10122SQLITE_PRIVATE   void sqlite3VdbeNoopComment(Vdbe*, const char*, ...);
10123# define VdbeNoopComment(X)  sqlite3VdbeNoopComment X
10124# ifdef SQLITE_ENABLE_MODULE_COMMENTS
10125#   define VdbeModuleComment(X)  sqlite3VdbeNoopComment X
10126# else
10127#   define VdbeModuleComment(X)
10128# endif
10129#else
10130# define VdbeComment(X)
10131# define VdbeNoopComment(X)
10132# define VdbeModuleComment(X)
10133#endif
10134
10135/*
10136** The VdbeCoverage macros are used to set a coverage testing point
10137** for VDBE branch instructions.  The coverage testing points are line
10138** numbers in the sqlite3.c source file.  VDBE branch coverage testing
10139** only works with an amalagmation build.  That's ok since a VDBE branch
10140** coverage build designed for testing the test suite only.  No application
10141** should ever ship with VDBE branch coverage measuring turned on.
10142**
10143**    VdbeCoverage(v)                  // Mark the previously coded instruction
10144**                                     // as a branch
10145**
10146**    VdbeCoverageIf(v, conditional)   // Mark previous if conditional true
10147**
10148**    VdbeCoverageAlwaysTaken(v)       // Previous branch is always taken
10149**
10150**    VdbeCoverageNeverTaken(v)        // Previous branch is never taken
10151**
10152** Every VDBE branch operation must be tagged with one of the macros above.
10153** If not, then when "make test" is run with -DSQLITE_VDBE_COVERAGE and
10154** -DSQLITE_DEBUG then an ALWAYS() will fail in the vdbeTakeBranch()
10155** routine in vdbe.c, alerting the developer to the missed tag.
10156*/
10157#ifdef SQLITE_VDBE_COVERAGE
10158SQLITE_PRIVATE   void sqlite3VdbeSetLineNumber(Vdbe*,int);
10159# define VdbeCoverage(v) sqlite3VdbeSetLineNumber(v,__LINE__)
10160# define VdbeCoverageIf(v,x) if(x)sqlite3VdbeSetLineNumber(v,__LINE__)
10161# define VdbeCoverageAlwaysTaken(v) sqlite3VdbeSetLineNumber(v,2);
10162# define VdbeCoverageNeverTaken(v) sqlite3VdbeSetLineNumber(v,1);
10163# define VDBE_OFFSET_LINENO(x) (__LINE__+x)
10164#else
10165# define VdbeCoverage(v)
10166# define VdbeCoverageIf(v,x)
10167# define VdbeCoverageAlwaysTaken(v)
10168# define VdbeCoverageNeverTaken(v)
10169# define VDBE_OFFSET_LINENO(x) 0
10170#endif
10171
10172#ifdef SQLITE_ENABLE_STMT_SCANSTATUS
10173SQLITE_PRIVATE void sqlite3VdbeScanStatus(Vdbe*, int, int, int, LogEst, const char*);
10174#else
10175# define sqlite3VdbeScanStatus(a,b,c,d,e)
10176#endif
10177
10178#endif
10179
10180/************** End of vdbe.h ************************************************/
10181/************** Continuing where we left off in sqliteInt.h ******************/
10182/************** Include pager.h in the middle of sqliteInt.h *****************/
10183/************** Begin file pager.h *******************************************/
10184/*
10185** 2001 September 15
10186**
10187** The author disclaims copyright to this source code.  In place of
10188** a legal notice, here is a blessing:
10189**
10190**    May you do good and not evil.
10191**    May you find forgiveness for yourself and forgive others.
10192**    May you share freely, never taking more than you give.
10193**
10194*************************************************************************
10195** This header file defines the interface that the sqlite page cache
10196** subsystem.  The page cache subsystem reads and writes a file a page
10197** at a time and provides a journal for rollback.
10198*/
10199
10200#ifndef _PAGER_H_
10201#define _PAGER_H_
10202
10203/*
10204** Default maximum size for persistent journal files. A negative
10205** value means no limit. This value may be overridden using the
10206** sqlite3PagerJournalSizeLimit() API. See also "PRAGMA journal_size_limit".
10207*/
10208#ifndef SQLITE_DEFAULT_JOURNAL_SIZE_LIMIT
10209  #define SQLITE_DEFAULT_JOURNAL_SIZE_LIMIT -1
10210#endif
10211
10212/*
10213** The type used to represent a page number.  The first page in a file
10214** is called page 1.  0 is used to represent "not a page".
10215*/
10216typedef u32 Pgno;
10217
10218/*
10219** Each open file is managed by a separate instance of the "Pager" structure.
10220*/
10221typedef struct Pager Pager;
10222
10223/*
10224** Handle type for pages.
10225*/
10226typedef struct PgHdr DbPage;
10227
10228/*
10229** Page number PAGER_MJ_PGNO is never used in an SQLite database (it is
10230** reserved for working around a windows/posix incompatibility). It is
10231** used in the journal to signify that the remainder of the journal file
10232** is devoted to storing a master journal name - there are no more pages to
10233** roll back. See comments for function writeMasterJournal() in pager.c
10234** for details.
10235*/
10236#define PAGER_MJ_PGNO(x) ((Pgno)((PENDING_BYTE/((x)->pageSize))+1))
10237
10238/*
10239** Allowed values for the flags parameter to sqlite3PagerOpen().
10240**
10241** NOTE: These values must match the corresponding BTREE_ values in btree.h.
10242*/
10243#define PAGER_OMIT_JOURNAL  0x0001    /* Do not use a rollback journal */
10244#define PAGER_MEMORY        0x0002    /* In-memory database */
10245
10246/*
10247** Valid values for the second argument to sqlite3PagerLockingMode().
10248*/
10249#define PAGER_LOCKINGMODE_QUERY      -1
10250#define PAGER_LOCKINGMODE_NORMAL      0
10251#define PAGER_LOCKINGMODE_EXCLUSIVE   1
10252
10253/*
10254** Numeric constants that encode the journalmode.
10255*/
10256#define PAGER_JOURNALMODE_QUERY     (-1)  /* Query the value of journalmode */
10257#define PAGER_JOURNALMODE_DELETE      0   /* Commit by deleting journal file */
10258#define PAGER_JOURNALMODE_PERSIST     1   /* Commit by zeroing journal header */
10259#define PAGER_JOURNALMODE_OFF         2   /* Journal omitted.  */
10260#define PAGER_JOURNALMODE_TRUNCATE    3   /* Commit by truncating journal */
10261#define PAGER_JOURNALMODE_MEMORY      4   /* In-memory journal file */
10262#define PAGER_JOURNALMODE_WAL         5   /* Use write-ahead logging */
10263
10264/*
10265** Flags that make up the mask passed to sqlite3PagerAcquire().
10266*/
10267#define PAGER_GET_NOCONTENT     0x01  /* Do not load data from disk */
10268#define PAGER_GET_READONLY      0x02  /* Read-only page is acceptable */
10269
10270/*
10271** Flags for sqlite3PagerSetFlags()
10272*/
10273#define PAGER_SYNCHRONOUS_OFF       0x01  /* PRAGMA synchronous=OFF */
10274#define PAGER_SYNCHRONOUS_NORMAL    0x02  /* PRAGMA synchronous=NORMAL */
10275#define PAGER_SYNCHRONOUS_FULL      0x03  /* PRAGMA synchronous=FULL */
10276#define PAGER_SYNCHRONOUS_MASK      0x03  /* Mask for three values above */
10277#define PAGER_FULLFSYNC             0x04  /* PRAGMA fullfsync=ON */
10278#define PAGER_CKPT_FULLFSYNC        0x08  /* PRAGMA checkpoint_fullfsync=ON */
10279#define PAGER_CACHESPILL            0x10  /* PRAGMA cache_spill=ON */
10280#define PAGER_FLAGS_MASK            0x1c  /* All above except SYNCHRONOUS */
10281
10282/*
10283** The remainder of this file contains the declarations of the functions
10284** that make up the Pager sub-system API. See source code comments for
10285** a detailed description of each routine.
10286*/
10287
10288/* Open and close a Pager connection. */
10289SQLITE_PRIVATE int sqlite3PagerOpen(
10290  sqlite3_vfs*,
10291  Pager **ppPager,
10292  const char*,
10293  int,
10294  int,
10295  int,
10296  void(*)(DbPage*)
10297);
10298SQLITE_PRIVATE int sqlite3PagerClose(Pager *pPager);
10299SQLITE_PRIVATE int sqlite3PagerReadFileheader(Pager*, int, unsigned char*);
10300
10301/* Functions used to configure a Pager object. */
10302SQLITE_PRIVATE void sqlite3PagerSetBusyhandler(Pager*, int(*)(void *), void *);
10303SQLITE_PRIVATE int sqlite3PagerSetPagesize(Pager*, u32*, int);
10304SQLITE_PRIVATE int sqlite3PagerMaxPageCount(Pager*, int);
10305SQLITE_PRIVATE void sqlite3PagerSetCachesize(Pager*, int);
10306SQLITE_PRIVATE void sqlite3PagerSetMmapLimit(Pager *, sqlite3_int64);
10307SQLITE_PRIVATE void sqlite3PagerShrink(Pager*);
10308SQLITE_PRIVATE void sqlite3PagerSetFlags(Pager*,unsigned);
10309SQLITE_PRIVATE int sqlite3PagerLockingMode(Pager *, int);
10310SQLITE_PRIVATE int sqlite3PagerSetJournalMode(Pager *, int);
10311SQLITE_PRIVATE int sqlite3PagerGetJournalMode(Pager*);
10312SQLITE_PRIVATE int sqlite3PagerOkToChangeJournalMode(Pager*);
10313SQLITE_PRIVATE i64 sqlite3PagerJournalSizeLimit(Pager *, i64);
10314SQLITE_PRIVATE sqlite3_backup **sqlite3PagerBackupPtr(Pager*);
10315
10316/* Functions used to obtain and release page references. */
10317SQLITE_PRIVATE int sqlite3PagerAcquire(Pager *pPager, Pgno pgno, DbPage **ppPage, int clrFlag);
10318#define sqlite3PagerGet(A,B,C) sqlite3PagerAcquire(A,B,C,0)
10319SQLITE_PRIVATE DbPage *sqlite3PagerLookup(Pager *pPager, Pgno pgno);
10320SQLITE_PRIVATE void sqlite3PagerRef(DbPage*);
10321SQLITE_PRIVATE void sqlite3PagerUnref(DbPage*);
10322SQLITE_PRIVATE void sqlite3PagerUnrefNotNull(DbPage*);
10323
10324/* Operations on page references. */
10325SQLITE_PRIVATE int sqlite3PagerWrite(DbPage*);
10326SQLITE_PRIVATE void sqlite3PagerDontWrite(DbPage*);
10327SQLITE_PRIVATE int sqlite3PagerMovepage(Pager*,DbPage*,Pgno,int);
10328SQLITE_PRIVATE int sqlite3PagerPageRefcount(DbPage*);
10329SQLITE_PRIVATE void *sqlite3PagerGetData(DbPage *);
10330SQLITE_PRIVATE void *sqlite3PagerGetExtra(DbPage *);
10331
10332/* Functions used to manage pager transactions and savepoints. */
10333SQLITE_PRIVATE void sqlite3PagerPagecount(Pager*, int*);
10334SQLITE_PRIVATE int sqlite3PagerBegin(Pager*, int exFlag, int);
10335SQLITE_PRIVATE int sqlite3PagerCommitPhaseOne(Pager*,const char *zMaster, int);
10336SQLITE_PRIVATE int sqlite3PagerExclusiveLock(Pager*);
10337SQLITE_PRIVATE int sqlite3PagerSync(Pager *pPager, const char *zMaster);
10338SQLITE_PRIVATE int sqlite3PagerCommitPhaseTwo(Pager*);
10339SQLITE_PRIVATE int sqlite3PagerRollback(Pager*);
10340SQLITE_PRIVATE int sqlite3PagerOpenSavepoint(Pager *pPager, int n);
10341SQLITE_PRIVATE int sqlite3PagerSavepoint(Pager *pPager, int op, int iSavepoint);
10342SQLITE_PRIVATE int sqlite3PagerSharedLock(Pager *pPager);
10343
10344#ifndef SQLITE_OMIT_WAL
10345SQLITE_PRIVATE   int sqlite3PagerCheckpoint(Pager *pPager, int, int*, int*);
10346SQLITE_PRIVATE   int sqlite3PagerWalSupported(Pager *pPager);
10347SQLITE_PRIVATE   int sqlite3PagerWalCallback(Pager *pPager);
10348SQLITE_PRIVATE   int sqlite3PagerOpenWal(Pager *pPager, int *pisOpen);
10349SQLITE_PRIVATE   int sqlite3PagerCloseWal(Pager *pPager);
10350#endif
10351
10352#ifdef SQLITE_ENABLE_ZIPVFS
10353SQLITE_PRIVATE   int sqlite3PagerWalFramesize(Pager *pPager);
10354#endif
10355
10356/* Functions used to query pager state and configuration. */
10357SQLITE_PRIVATE u8 sqlite3PagerIsreadonly(Pager*);
10358SQLITE_PRIVATE u32 sqlite3PagerDataVersion(Pager*);
10359#ifdef SQLITE_DEBUG
10360SQLITE_PRIVATE   int sqlite3PagerRefcount(Pager*);
10361#endif
10362SQLITE_PRIVATE int sqlite3PagerMemUsed(Pager*);
10363SQLITE_PRIVATE const char *sqlite3PagerFilename(Pager*, int);
10364SQLITE_PRIVATE const sqlite3_vfs *sqlite3PagerVfs(Pager*);
10365SQLITE_PRIVATE sqlite3_file *sqlite3PagerFile(Pager*);
10366SQLITE_PRIVATE const char *sqlite3PagerJournalname(Pager*);
10367SQLITE_PRIVATE int sqlite3PagerNosync(Pager*);
10368SQLITE_PRIVATE void *sqlite3PagerTempSpace(Pager*);
10369SQLITE_PRIVATE int sqlite3PagerIsMemdb(Pager*);
10370SQLITE_PRIVATE void sqlite3PagerCacheStat(Pager *, int, int, int *);
10371SQLITE_PRIVATE void sqlite3PagerClearCache(Pager *);
10372SQLITE_PRIVATE int sqlite3SectorSize(sqlite3_file *);
10373
10374/* Functions used to truncate the database file. */
10375SQLITE_PRIVATE void sqlite3PagerTruncateImage(Pager*,Pgno);
10376
10377SQLITE_PRIVATE void sqlite3PagerRekey(DbPage*, Pgno, u16);
10378
10379#if defined(SQLITE_HAS_CODEC) && !defined(SQLITE_OMIT_WAL)
10380SQLITE_PRIVATE void *sqlite3PagerCodec(DbPage *);
10381#endif
10382
10383/* Functions to support testing and debugging. */
10384#if !defined(NDEBUG) || defined(SQLITE_TEST)
10385SQLITE_PRIVATE   Pgno sqlite3PagerPagenumber(DbPage*);
10386SQLITE_PRIVATE   int sqlite3PagerIswriteable(DbPage*);
10387#endif
10388#ifdef SQLITE_TEST
10389SQLITE_PRIVATE   int *sqlite3PagerStats(Pager*);
10390SQLITE_PRIVATE   void sqlite3PagerRefdump(Pager*);
10391  void disable_simulated_io_errors(void);
10392  void enable_simulated_io_errors(void);
10393#else
10394# define disable_simulated_io_errors()
10395# define enable_simulated_io_errors()
10396#endif
10397
10398#endif /* _PAGER_H_ */
10399
10400/************** End of pager.h ***********************************************/
10401/************** Continuing where we left off in sqliteInt.h ******************/
10402/************** Include pcache.h in the middle of sqliteInt.h ****************/
10403/************** Begin file pcache.h ******************************************/
10404/*
10405** 2008 August 05
10406**
10407** The author disclaims copyright to this source code.  In place of
10408** a legal notice, here is a blessing:
10409**
10410**    May you do good and not evil.
10411**    May you find forgiveness for yourself and forgive others.
10412**    May you share freely, never taking more than you give.
10413**
10414*************************************************************************
10415** This header file defines the interface that the sqlite page cache
10416** subsystem.
10417*/
10418
10419#ifndef _PCACHE_H_
10420
10421typedef struct PgHdr PgHdr;
10422typedef struct PCache PCache;
10423
10424/*
10425** Every page in the cache is controlled by an instance of the following
10426** structure.
10427*/
10428struct PgHdr {
10429  sqlite3_pcache_page *pPage;    /* Pcache object page handle */
10430  void *pData;                   /* Page data */
10431  void *pExtra;                  /* Extra content */
10432  PgHdr *pDirty;                 /* Transient list of dirty pages */
10433  Pager *pPager;                 /* The pager this page is part of */
10434  Pgno pgno;                     /* Page number for this page */
10435#ifdef SQLITE_CHECK_PAGES
10436  u32 pageHash;                  /* Hash of page content */
10437#endif
10438  u16 flags;                     /* PGHDR flags defined below */
10439
10440  /**********************************************************************
10441  ** Elements above are public.  All that follows is private to pcache.c
10442  ** and should not be accessed by other modules.
10443  */
10444  i16 nRef;                      /* Number of users of this page */
10445  PCache *pCache;                /* Cache that owns this page */
10446
10447  PgHdr *pDirtyNext;             /* Next element in list of dirty pages */
10448  PgHdr *pDirtyPrev;             /* Previous element in list of dirty pages */
10449};
10450
10451/* Bit values for PgHdr.flags */
10452#define PGHDR_CLEAN           0x001  /* Page not on the PCache.pDirty list */
10453#define PGHDR_DIRTY           0x002  /* Page is on the PCache.pDirty list */
10454#define PGHDR_WRITEABLE       0x004  /* Journaled and ready to modify */
10455#define PGHDR_NEED_SYNC       0x008  /* Fsync the rollback journal before
10456                                     ** writing this page to the database */
10457#define PGHDR_NEED_READ       0x010  /* Content is unread */
10458#define PGHDR_DONT_WRITE      0x020  /* Do not write content to disk */
10459#define PGHDR_MMAP            0x040  /* This is an mmap page object */
10460
10461/* Initialize and shutdown the page cache subsystem */
10462SQLITE_PRIVATE int sqlite3PcacheInitialize(void);
10463SQLITE_PRIVATE void sqlite3PcacheShutdown(void);
10464
10465/* Page cache buffer management:
10466** These routines implement SQLITE_CONFIG_PAGECACHE.
10467*/
10468SQLITE_PRIVATE void sqlite3PCacheBufferSetup(void *, int sz, int n);
10469
10470/* Create a new pager cache.
10471** Under memory stress, invoke xStress to try to make pages clean.
10472** Only clean and unpinned pages can be reclaimed.
10473*/
10474SQLITE_PRIVATE int sqlite3PcacheOpen(
10475  int szPage,                    /* Size of every page */
10476  int szExtra,                   /* Extra space associated with each page */
10477  int bPurgeable,                /* True if pages are on backing store */
10478  int (*xStress)(void*, PgHdr*), /* Call to try to make pages clean */
10479  void *pStress,                 /* Argument to xStress */
10480  PCache *pToInit                /* Preallocated space for the PCache */
10481);
10482
10483/* Modify the page-size after the cache has been created. */
10484SQLITE_PRIVATE int sqlite3PcacheSetPageSize(PCache *, int);
10485
10486/* Return the size in bytes of a PCache object.  Used to preallocate
10487** storage space.
10488*/
10489SQLITE_PRIVATE int sqlite3PcacheSize(void);
10490
10491/* One release per successful fetch.  Page is pinned until released.
10492** Reference counted.
10493*/
10494SQLITE_PRIVATE sqlite3_pcache_page *sqlite3PcacheFetch(PCache*, Pgno, int createFlag);
10495SQLITE_PRIVATE int sqlite3PcacheFetchStress(PCache*, Pgno, sqlite3_pcache_page**);
10496SQLITE_PRIVATE PgHdr *sqlite3PcacheFetchFinish(PCache*, Pgno, sqlite3_pcache_page *pPage);
10497SQLITE_PRIVATE void sqlite3PcacheRelease(PgHdr*);
10498
10499SQLITE_PRIVATE void sqlite3PcacheDrop(PgHdr*);         /* Remove page from cache */
10500SQLITE_PRIVATE void sqlite3PcacheMakeDirty(PgHdr*);    /* Make sure page is marked dirty */
10501SQLITE_PRIVATE void sqlite3PcacheMakeClean(PgHdr*);    /* Mark a single page as clean */
10502SQLITE_PRIVATE void sqlite3PcacheCleanAll(PCache*);    /* Mark all dirty list pages as clean */
10503
10504/* Change a page number.  Used by incr-vacuum. */
10505SQLITE_PRIVATE void sqlite3PcacheMove(PgHdr*, Pgno);
10506
10507/* Remove all pages with pgno>x.  Reset the cache if x==0 */
10508SQLITE_PRIVATE void sqlite3PcacheTruncate(PCache*, Pgno x);
10509
10510/* Get a list of all dirty pages in the cache, sorted by page number */
10511SQLITE_PRIVATE PgHdr *sqlite3PcacheDirtyList(PCache*);
10512
10513/* Reset and close the cache object */
10514SQLITE_PRIVATE void sqlite3PcacheClose(PCache*);
10515
10516/* Clear flags from pages of the page cache */
10517SQLITE_PRIVATE void sqlite3PcacheClearSyncFlags(PCache *);
10518
10519/* Discard the contents of the cache */
10520SQLITE_PRIVATE void sqlite3PcacheClear(PCache*);
10521
10522/* Return the total number of outstanding page references */
10523SQLITE_PRIVATE int sqlite3PcacheRefCount(PCache*);
10524
10525/* Increment the reference count of an existing page */
10526SQLITE_PRIVATE void sqlite3PcacheRef(PgHdr*);
10527
10528SQLITE_PRIVATE int sqlite3PcachePageRefcount(PgHdr*);
10529
10530/* Return the total number of pages stored in the cache */
10531SQLITE_PRIVATE int sqlite3PcachePagecount(PCache*);
10532
10533#if defined(SQLITE_CHECK_PAGES) || defined(SQLITE_DEBUG)
10534/* Iterate through all dirty pages currently stored in the cache. This
10535** interface is only available if SQLITE_CHECK_PAGES is defined when the
10536** library is built.
10537*/
10538SQLITE_PRIVATE void sqlite3PcacheIterateDirty(PCache *pCache, void (*xIter)(PgHdr *));
10539#endif
10540
10541/* Set and get the suggested cache-size for the specified pager-cache.
10542**
10543** If no global maximum is configured, then the system attempts to limit
10544** the total number of pages cached by purgeable pager-caches to the sum
10545** of the suggested cache-sizes.
10546*/
10547SQLITE_PRIVATE void sqlite3PcacheSetCachesize(PCache *, int);
10548#ifdef SQLITE_TEST
10549SQLITE_PRIVATE int sqlite3PcacheGetCachesize(PCache *);
10550#endif
10551
10552/* Free up as much memory as possible from the page cache */
10553SQLITE_PRIVATE void sqlite3PcacheShrink(PCache*);
10554
10555#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
10556/* Try to return memory used by the pcache module to the main memory heap */
10557SQLITE_PRIVATE int sqlite3PcacheReleaseMemory(int);
10558#endif
10559
10560#ifdef SQLITE_TEST
10561SQLITE_PRIVATE void sqlite3PcacheStats(int*,int*,int*,int*);
10562#endif
10563
10564SQLITE_PRIVATE void sqlite3PCacheSetDefault(void);
10565
10566/* Return the header size */
10567SQLITE_PRIVATE int sqlite3HeaderSizePcache(void);
10568SQLITE_PRIVATE int sqlite3HeaderSizePcache1(void);
10569
10570#endif /* _PCACHE_H_ */
10571
10572/************** End of pcache.h **********************************************/
10573/************** Continuing where we left off in sqliteInt.h ******************/
10574
10575/************** Include os.h in the middle of sqliteInt.h ********************/
10576/************** Begin file os.h **********************************************/
10577/*
10578** 2001 September 16
10579**
10580** The author disclaims copyright to this source code.  In place of
10581** a legal notice, here is a blessing:
10582**
10583**    May you do good and not evil.
10584**    May you find forgiveness for yourself and forgive others.
10585**    May you share freely, never taking more than you give.
10586**
10587******************************************************************************
10588**
10589** This header file (together with is companion C source-code file
10590** "os.c") attempt to abstract the underlying operating system so that
10591** the SQLite library will work on both POSIX and windows systems.
10592**
10593** This header file is #include-ed by sqliteInt.h and thus ends up
10594** being included by every source file.
10595*/
10596#ifndef _SQLITE_OS_H_
10597#define _SQLITE_OS_H_
10598
10599/*
10600** Attempt to automatically detect the operating system and setup the
10601** necessary pre-processor macros for it.
10602*/
10603/************** Include os_setup.h in the middle of os.h *********************/
10604/************** Begin file os_setup.h ****************************************/
10605/*
10606** 2013 November 25
10607**
10608** The author disclaims copyright to this source code.  In place of
10609** a legal notice, here is a blessing:
10610**
10611**    May you do good and not evil.
10612**    May you find forgiveness for yourself and forgive others.
10613**    May you share freely, never taking more than you give.
10614**
10615******************************************************************************
10616**
10617** This file contains pre-processor directives related to operating system
10618** detection and/or setup.
10619*/
10620#ifndef _OS_SETUP_H_
10621#define _OS_SETUP_H_
10622
10623/*
10624** Figure out if we are dealing with Unix, Windows, or some other operating
10625** system.
10626**
10627** After the following block of preprocess macros, all of SQLITE_OS_UNIX,
10628** SQLITE_OS_WIN, and SQLITE_OS_OTHER will defined to either 1 or 0.  One of
10629** the three will be 1.  The other two will be 0.
10630*/
10631#if defined(SQLITE_OS_OTHER)
10632#  if SQLITE_OS_OTHER==1
10633#    undef SQLITE_OS_UNIX
10634#    define SQLITE_OS_UNIX 0
10635#    undef SQLITE_OS_WIN
10636#    define SQLITE_OS_WIN 0
10637#  else
10638#    undef SQLITE_OS_OTHER
10639#  endif
10640#endif
10641#if !defined(SQLITE_OS_UNIX) && !defined(SQLITE_OS_OTHER)
10642#  define SQLITE_OS_OTHER 0
10643#  ifndef SQLITE_OS_WIN
10644#    if defined(_WIN32) || defined(WIN32) || defined(__CYGWIN__) || \
10645        defined(__MINGW32__) || defined(__BORLANDC__)
10646#      define SQLITE_OS_WIN 1
10647#      define SQLITE_OS_UNIX 0
10648#    else
10649#      define SQLITE_OS_WIN 0
10650#      define SQLITE_OS_UNIX 1
10651#    endif
10652#  else
10653#    define SQLITE_OS_UNIX 0
10654#  endif
10655#else
10656#  ifndef SQLITE_OS_WIN
10657#    define SQLITE_OS_WIN 0
10658#  endif
10659#endif
10660
10661#endif /* _OS_SETUP_H_ */
10662
10663/************** End of os_setup.h ********************************************/
10664/************** Continuing where we left off in os.h *************************/
10665
10666/* If the SET_FULLSYNC macro is not defined above, then make it
10667** a no-op
10668*/
10669#ifndef SET_FULLSYNC
10670# define SET_FULLSYNC(x,y)
10671#endif
10672
10673/*
10674** The default size of a disk sector
10675*/
10676#ifndef SQLITE_DEFAULT_SECTOR_SIZE
10677# define SQLITE_DEFAULT_SECTOR_SIZE 4096
10678#endif
10679
10680/*
10681** Temporary files are named starting with this prefix followed by 16 random
10682** alphanumeric characters, and no file extension. They are stored in the
10683** OS's standard temporary file directory, and are deleted prior to exit.
10684** If sqlite is being embedded in another program, you may wish to change the
10685** prefix to reflect your program's name, so that if your program exits
10686** prematurely, old temporary files can be easily identified. This can be done
10687** using -DSQLITE_TEMP_FILE_PREFIX=myprefix_ on the compiler command line.
10688**
10689** 2006-10-31:  The default prefix used to be "sqlite_".  But then
10690** Mcafee started using SQLite in their anti-virus product and it
10691** started putting files with the "sqlite" name in the c:/temp folder.
10692** This annoyed many windows users.  Those users would then do a
10693** Google search for "sqlite", find the telephone numbers of the
10694** developers and call to wake them up at night and complain.
10695** For this reason, the default name prefix is changed to be "sqlite"
10696** spelled backwards.  So the temp files are still identified, but
10697** anybody smart enough to figure out the code is also likely smart
10698** enough to know that calling the developer will not help get rid
10699** of the file.
10700*/
10701#ifndef SQLITE_TEMP_FILE_PREFIX
10702# define SQLITE_TEMP_FILE_PREFIX "etilqs_"
10703#endif
10704
10705/*
10706** The following values may be passed as the second argument to
10707** sqlite3OsLock(). The various locks exhibit the following semantics:
10708**
10709** SHARED:    Any number of processes may hold a SHARED lock simultaneously.
10710** RESERVED:  A single process may hold a RESERVED lock on a file at
10711**            any time. Other processes may hold and obtain new SHARED locks.
10712** PENDING:   A single process may hold a PENDING lock on a file at
10713**            any one time. Existing SHARED locks may persist, but no new
10714**            SHARED locks may be obtained by other processes.
10715** EXCLUSIVE: An EXCLUSIVE lock precludes all other locks.
10716**
10717** PENDING_LOCK may not be passed directly to sqlite3OsLock(). Instead, a
10718** process that requests an EXCLUSIVE lock may actually obtain a PENDING
10719** lock. This can be upgraded to an EXCLUSIVE lock by a subsequent call to
10720** sqlite3OsLock().
10721*/
10722#define NO_LOCK         0
10723#define SHARED_LOCK     1
10724#define RESERVED_LOCK   2
10725#define PENDING_LOCK    3
10726#define EXCLUSIVE_LOCK  4
10727
10728/*
10729** File Locking Notes:  (Mostly about windows but also some info for Unix)
10730**
10731** We cannot use LockFileEx() or UnlockFileEx() on Win95/98/ME because
10732** those functions are not available.  So we use only LockFile() and
10733** UnlockFile().
10734**
10735** LockFile() prevents not just writing but also reading by other processes.
10736** A SHARED_LOCK is obtained by locking a single randomly-chosen
10737** byte out of a specific range of bytes. The lock byte is obtained at
10738** random so two separate readers can probably access the file at the
10739** same time, unless they are unlucky and choose the same lock byte.
10740** An EXCLUSIVE_LOCK is obtained by locking all bytes in the range.
10741** There can only be one writer.  A RESERVED_LOCK is obtained by locking
10742** a single byte of the file that is designated as the reserved lock byte.
10743** A PENDING_LOCK is obtained by locking a designated byte different from
10744** the RESERVED_LOCK byte.
10745**
10746** On WinNT/2K/XP systems, LockFileEx() and UnlockFileEx() are available,
10747** which means we can use reader/writer locks.  When reader/writer locks
10748** are used, the lock is placed on the same range of bytes that is used
10749** for probabilistic locking in Win95/98/ME.  Hence, the locking scheme
10750** will support two or more Win95 readers or two or more WinNT readers.
10751** But a single Win95 reader will lock out all WinNT readers and a single
10752** WinNT reader will lock out all other Win95 readers.
10753**
10754** The following #defines specify the range of bytes used for locking.
10755** SHARED_SIZE is the number of bytes available in the pool from which
10756** a random byte is selected for a shared lock.  The pool of bytes for
10757** shared locks begins at SHARED_FIRST.
10758**
10759** The same locking strategy and
10760** byte ranges are used for Unix.  This leaves open the possibility of having
10761** clients on win95, winNT, and unix all talking to the same shared file
10762** and all locking correctly.  To do so would require that samba (or whatever
10763** tool is being used for file sharing) implements locks correctly between
10764** windows and unix.  I'm guessing that isn't likely to happen, but by
10765** using the same locking range we are at least open to the possibility.
10766**
10767** Locking in windows is manditory.  For this reason, we cannot store
10768** actual data in the bytes used for locking.  The pager never allocates
10769** the pages involved in locking therefore.  SHARED_SIZE is selected so
10770** that all locks will fit on a single page even at the minimum page size.
10771** PENDING_BYTE defines the beginning of the locks.  By default PENDING_BYTE
10772** is set high so that we don't have to allocate an unused page except
10773** for very large databases.  But one should test the page skipping logic
10774** by setting PENDING_BYTE low and running the entire regression suite.
10775**
10776** Changing the value of PENDING_BYTE results in a subtly incompatible
10777** file format.  Depending on how it is changed, you might not notice
10778** the incompatibility right away, even running a full regression test.
10779** The default location of PENDING_BYTE is the first byte past the
10780** 1GB boundary.
10781**
10782*/
10783#ifdef SQLITE_OMIT_WSD
10784# define PENDING_BYTE     (0x40000000)
10785#else
10786# define PENDING_BYTE      sqlite3PendingByte
10787#endif
10788#define RESERVED_BYTE     (PENDING_BYTE+1)
10789#define SHARED_FIRST      (PENDING_BYTE+2)
10790#define SHARED_SIZE       510
10791
10792/*
10793** Wrapper around OS specific sqlite3_os_init() function.
10794*/
10795SQLITE_PRIVATE int sqlite3OsInit(void);
10796
10797/*
10798** Functions for accessing sqlite3_file methods
10799*/
10800SQLITE_PRIVATE int sqlite3OsClose(sqlite3_file*);
10801SQLITE_PRIVATE int sqlite3OsRead(sqlite3_file*, void*, int amt, i64 offset);
10802SQLITE_PRIVATE int sqlite3OsWrite(sqlite3_file*, const void*, int amt, i64 offset);
10803SQLITE_PRIVATE int sqlite3OsTruncate(sqlite3_file*, i64 size);
10804SQLITE_PRIVATE int sqlite3OsSync(sqlite3_file*, int);
10805SQLITE_PRIVATE int sqlite3OsFileSize(sqlite3_file*, i64 *pSize);
10806SQLITE_PRIVATE int sqlite3OsLock(sqlite3_file*, int);
10807SQLITE_PRIVATE int sqlite3OsUnlock(sqlite3_file*, int);
10808SQLITE_PRIVATE int sqlite3OsCheckReservedLock(sqlite3_file *id, int *pResOut);
10809SQLITE_PRIVATE int sqlite3OsFileControl(sqlite3_file*,int,void*);
10810SQLITE_PRIVATE void sqlite3OsFileControlHint(sqlite3_file*,int,void*);
10811#define SQLITE_FCNTL_DB_UNCHANGED 0xca093fa0
10812SQLITE_PRIVATE int sqlite3OsSectorSize(sqlite3_file *id);
10813SQLITE_PRIVATE int sqlite3OsDeviceCharacteristics(sqlite3_file *id);
10814SQLITE_PRIVATE int sqlite3OsShmMap(sqlite3_file *,int,int,int,void volatile **);
10815SQLITE_PRIVATE int sqlite3OsShmLock(sqlite3_file *id, int, int, int);
10816SQLITE_PRIVATE void sqlite3OsShmBarrier(sqlite3_file *id);
10817SQLITE_PRIVATE int sqlite3OsShmUnmap(sqlite3_file *id, int);
10818SQLITE_PRIVATE int sqlite3OsFetch(sqlite3_file *id, i64, int, void **);
10819SQLITE_PRIVATE int sqlite3OsUnfetch(sqlite3_file *, i64, void *);
10820
10821
10822/*
10823** Functions for accessing sqlite3_vfs methods
10824*/
10825SQLITE_PRIVATE int sqlite3OsOpen(sqlite3_vfs *, const char *, sqlite3_file*, int, int *);
10826SQLITE_PRIVATE int sqlite3OsDelete(sqlite3_vfs *, const char *, int);
10827SQLITE_PRIVATE int sqlite3OsAccess(sqlite3_vfs *, const char *, int, int *pResOut);
10828SQLITE_PRIVATE int sqlite3OsFullPathname(sqlite3_vfs *, const char *, int, char *);
10829#ifndef SQLITE_OMIT_LOAD_EXTENSION
10830SQLITE_PRIVATE void *sqlite3OsDlOpen(sqlite3_vfs *, const char *);
10831SQLITE_PRIVATE void sqlite3OsDlError(sqlite3_vfs *, int, char *);
10832SQLITE_PRIVATE void (*sqlite3OsDlSym(sqlite3_vfs *, void *, const char *))(void);
10833SQLITE_PRIVATE void sqlite3OsDlClose(sqlite3_vfs *, void *);
10834#endif /* SQLITE_OMIT_LOAD_EXTENSION */
10835SQLITE_PRIVATE int sqlite3OsRandomness(sqlite3_vfs *, int, char *);
10836SQLITE_PRIVATE int sqlite3OsSleep(sqlite3_vfs *, int);
10837SQLITE_PRIVATE int sqlite3OsCurrentTimeInt64(sqlite3_vfs *, sqlite3_int64*);
10838
10839/*
10840** Convenience functions for opening and closing files using
10841** sqlite3_malloc() to obtain space for the file-handle structure.
10842*/
10843SQLITE_PRIVATE int sqlite3OsOpenMalloc(sqlite3_vfs *, const char *, sqlite3_file **, int,int*);
10844SQLITE_PRIVATE int sqlite3OsCloseFree(sqlite3_file *);
10845
10846#endif /* _SQLITE_OS_H_ */
10847
10848/************** End of os.h **************************************************/
10849/************** Continuing where we left off in sqliteInt.h ******************/
10850/************** Include mutex.h in the middle of sqliteInt.h *****************/
10851/************** Begin file mutex.h *******************************************/
10852/*
10853** 2007 August 28
10854**
10855** The author disclaims copyright to this source code.  In place of
10856** a legal notice, here is a blessing:
10857**
10858**    May you do good and not evil.
10859**    May you find forgiveness for yourself and forgive others.
10860**    May you share freely, never taking more than you give.
10861**
10862*************************************************************************
10863**
10864** This file contains the common header for all mutex implementations.
10865** The sqliteInt.h header #includes this file so that it is available
10866** to all source files.  We break it out in an effort to keep the code
10867** better organized.
10868**
10869** NOTE:  source files should *not* #include this header file directly.
10870** Source files should #include the sqliteInt.h file and let that file
10871** include this one indirectly.
10872*/
10873
10874
10875/*
10876** Figure out what version of the code to use.  The choices are
10877**
10878**   SQLITE_MUTEX_OMIT         No mutex logic.  Not even stubs.  The
10879**                             mutexes implementation cannot be overridden
10880**                             at start-time.
10881**
10882**   SQLITE_MUTEX_NOOP         For single-threaded applications.  No
10883**                             mutual exclusion is provided.  But this
10884**                             implementation can be overridden at
10885**                             start-time.
10886**
10887**   SQLITE_MUTEX_PTHREADS     For multi-threaded applications on Unix.
10888**
10889**   SQLITE_MUTEX_W32          For multi-threaded applications on Win32.
10890*/
10891#if !SQLITE_THREADSAFE
10892# define SQLITE_MUTEX_OMIT
10893#endif
10894#if SQLITE_THREADSAFE && !defined(SQLITE_MUTEX_NOOP)
10895#  if SQLITE_OS_UNIX
10896#    define SQLITE_MUTEX_PTHREADS
10897#  elif SQLITE_OS_WIN
10898#    define SQLITE_MUTEX_W32
10899#  else
10900#    define SQLITE_MUTEX_NOOP
10901#  endif
10902#endif
10903
10904#ifdef SQLITE_MUTEX_OMIT
10905/*
10906** If this is a no-op implementation, implement everything as macros.
10907*/
10908#define sqlite3_mutex_alloc(X)    ((sqlite3_mutex*)8)
10909#define sqlite3_mutex_free(X)
10910#define sqlite3_mutex_enter(X)
10911#define sqlite3_mutex_try(X)      SQLITE_OK
10912#define sqlite3_mutex_leave(X)
10913#define sqlite3_mutex_held(X)     ((void)(X),1)
10914#define sqlite3_mutex_notheld(X)  ((void)(X),1)
10915#define sqlite3MutexAlloc(X)      ((sqlite3_mutex*)8)
10916#define sqlite3MutexInit()        SQLITE_OK
10917#define sqlite3MutexEnd()
10918#define MUTEX_LOGIC(X)
10919#else
10920#define MUTEX_LOGIC(X)            X
10921#endif /* defined(SQLITE_MUTEX_OMIT) */
10922
10923/************** End of mutex.h ***********************************************/
10924/************** Continuing where we left off in sqliteInt.h ******************/
10925
10926
10927/*
10928** Each database file to be accessed by the system is an instance
10929** of the following structure.  There are normally two of these structures
10930** in the sqlite.aDb[] array.  aDb[0] is the main database file and
10931** aDb[1] is the database file used to hold temporary tables.  Additional
10932** databases may be attached.
10933*/
10934struct Db {
10935  char *zName;         /* Name of this database */
10936  Btree *pBt;          /* The B*Tree structure for this database file */
10937  u8 safety_level;     /* How aggressive at syncing data to disk */
10938  Schema *pSchema;     /* Pointer to database schema (possibly shared) */
10939};
10940
10941/*
10942** An instance of the following structure stores a database schema.
10943**
10944** Most Schema objects are associated with a Btree.  The exception is
10945** the Schema for the TEMP databaes (sqlite3.aDb[1]) which is free-standing.
10946** In shared cache mode, a single Schema object can be shared by multiple
10947** Btrees that refer to the same underlying BtShared object.
10948**
10949** Schema objects are automatically deallocated when the last Btree that
10950** references them is destroyed.   The TEMP Schema is manually freed by
10951** sqlite3_close().
10952*
10953** A thread must be holding a mutex on the corresponding Btree in order
10954** to access Schema content.  This implies that the thread must also be
10955** holding a mutex on the sqlite3 connection pointer that owns the Btree.
10956** For a TEMP Schema, only the connection mutex is required.
10957*/
10958struct Schema {
10959  int schema_cookie;   /* Database schema version number for this file */
10960  int iGeneration;     /* Generation counter.  Incremented with each change */
10961  Hash tblHash;        /* All tables indexed by name */
10962  Hash idxHash;        /* All (named) indices indexed by name */
10963  Hash trigHash;       /* All triggers indexed by name */
10964  Hash fkeyHash;       /* All foreign keys by referenced table name */
10965  Table *pSeqTab;      /* The sqlite_sequence table used by AUTOINCREMENT */
10966  u8 file_format;      /* Schema format version for this file */
10967  u8 enc;              /* Text encoding used by this database */
10968  u16 schemaFlags;     /* Flags associated with this schema */
10969  int cache_size;      /* Number of pages to use in the cache */
10970};
10971
10972/*
10973** These macros can be used to test, set, or clear bits in the
10974** Db.pSchema->flags field.
10975*/
10976#define DbHasProperty(D,I,P)     (((D)->aDb[I].pSchema->schemaFlags&(P))==(P))
10977#define DbHasAnyProperty(D,I,P)  (((D)->aDb[I].pSchema->schemaFlags&(P))!=0)
10978#define DbSetProperty(D,I,P)     (D)->aDb[I].pSchema->schemaFlags|=(P)
10979#define DbClearProperty(D,I,P)   (D)->aDb[I].pSchema->schemaFlags&=~(P)
10980
10981/*
10982** Allowed values for the DB.pSchema->flags field.
10983**
10984** The DB_SchemaLoaded flag is set after the database schema has been
10985** read into internal hash tables.
10986**
10987** DB_UnresetViews means that one or more views have column names that
10988** have been filled out.  If the schema changes, these column names might
10989** changes and so the view will need to be reset.
10990*/
10991#define DB_SchemaLoaded    0x0001  /* The schema has been loaded */
10992#define DB_UnresetViews    0x0002  /* Some views have defined column names */
10993#define DB_Empty           0x0004  /* The file is empty (length 0 bytes) */
10994
10995/*
10996** The number of different kinds of things that can be limited
10997** using the sqlite3_limit() interface.
10998*/
10999#define SQLITE_N_LIMIT (SQLITE_LIMIT_WORKER_THREADS+1)
11000
11001/*
11002** Lookaside malloc is a set of fixed-size buffers that can be used
11003** to satisfy small transient memory allocation requests for objects
11004** associated with a particular database connection.  The use of
11005** lookaside malloc provides a significant performance enhancement
11006** (approx 10%) by avoiding numerous malloc/free requests while parsing
11007** SQL statements.
11008**
11009** The Lookaside structure holds configuration information about the
11010** lookaside malloc subsystem.  Each available memory allocation in
11011** the lookaside subsystem is stored on a linked list of LookasideSlot
11012** objects.
11013**
11014** Lookaside allocations are only allowed for objects that are associated
11015** with a particular database connection.  Hence, schema information cannot
11016** be stored in lookaside because in shared cache mode the schema information
11017** is shared by multiple database connections.  Therefore, while parsing
11018** schema information, the Lookaside.bEnabled flag is cleared so that
11019** lookaside allocations are not used to construct the schema objects.
11020*/
11021struct Lookaside {
11022  u16 sz;                 /* Size of each buffer in bytes */
11023  u8 bEnabled;            /* False to disable new lookaside allocations */
11024  u8 bMalloced;           /* True if pStart obtained from sqlite3_malloc() */
11025  int nOut;               /* Number of buffers currently checked out */
11026  int mxOut;              /* Highwater mark for nOut */
11027  int anStat[3];          /* 0: hits.  1: size misses.  2: full misses */
11028  LookasideSlot *pFree;   /* List of available buffers */
11029  void *pStart;           /* First byte of available memory space */
11030  void *pEnd;             /* First byte past end of available space */
11031};
11032struct LookasideSlot {
11033  LookasideSlot *pNext;    /* Next buffer in the list of free buffers */
11034};
11035
11036/*
11037** A hash table for function definitions.
11038**
11039** Hash each FuncDef structure into one of the FuncDefHash.a[] slots.
11040** Collisions are on the FuncDef.pHash chain.
11041*/
11042struct FuncDefHash {
11043  FuncDef *a[23];       /* Hash table for functions */
11044};
11045
11046#ifdef SQLITE_USER_AUTHENTICATION
11047/*
11048** Information held in the "sqlite3" database connection object and used
11049** to manage user authentication.
11050*/
11051typedef struct sqlite3_userauth sqlite3_userauth;
11052struct sqlite3_userauth {
11053  u8 authLevel;                 /* Current authentication level */
11054  int nAuthPW;                  /* Size of the zAuthPW in bytes */
11055  char *zAuthPW;                /* Password used to authenticate */
11056  char *zAuthUser;              /* User name used to authenticate */
11057};
11058
11059/* Allowed values for sqlite3_userauth.authLevel */
11060#define UAUTH_Unknown     0     /* Authentication not yet checked */
11061#define UAUTH_Fail        1     /* User authentication failed */
11062#define UAUTH_User        2     /* Authenticated as a normal user */
11063#define UAUTH_Admin       3     /* Authenticated as an administrator */
11064
11065/* Functions used only by user authorization logic */
11066SQLITE_PRIVATE int sqlite3UserAuthTable(const char*);
11067SQLITE_PRIVATE int sqlite3UserAuthCheckLogin(sqlite3*,const char*,u8*);
11068SQLITE_PRIVATE void sqlite3UserAuthInit(sqlite3*);
11069SQLITE_PRIVATE void sqlite3CryptFunc(sqlite3_context*,int,sqlite3_value**);
11070
11071#endif /* SQLITE_USER_AUTHENTICATION */
11072
11073/*
11074** typedef for the authorization callback function.
11075*/
11076#ifdef SQLITE_USER_AUTHENTICATION
11077  typedef int (*sqlite3_xauth)(void*,int,const char*,const char*,const char*,
11078                               const char*, const char*);
11079#else
11080  typedef int (*sqlite3_xauth)(void*,int,const char*,const char*,const char*,
11081                               const char*);
11082#endif
11083
11084
11085/*
11086** Each database connection is an instance of the following structure.
11087*/
11088struct sqlite3 {
11089  sqlite3_vfs *pVfs;            /* OS Interface */
11090  struct Vdbe *pVdbe;           /* List of active virtual machines */
11091  CollSeq *pDfltColl;           /* The default collating sequence (BINARY) */
11092  sqlite3_mutex *mutex;         /* Connection mutex */
11093  Db *aDb;                      /* All backends */
11094  int nDb;                      /* Number of backends currently in use */
11095  int flags;                    /* Miscellaneous flags. See below */
11096  i64 lastRowid;                /* ROWID of most recent insert (see above) */
11097  i64 szMmap;                   /* Default mmap_size setting */
11098  unsigned int openFlags;       /* Flags passed to sqlite3_vfs.xOpen() */
11099  int errCode;                  /* Most recent error code (SQLITE_*) */
11100  int errMask;                  /* & result codes with this before returning */
11101  u16 dbOptFlags;               /* Flags to enable/disable optimizations */
11102  u8 enc;                       /* Text encoding */
11103  u8 autoCommit;                /* The auto-commit flag. */
11104  u8 temp_store;                /* 1: file 2: memory 0: default */
11105  u8 mallocFailed;              /* True if we have seen a malloc failure */
11106  u8 dfltLockMode;              /* Default locking-mode for attached dbs */
11107  signed char nextAutovac;      /* Autovac setting after VACUUM if >=0 */
11108  u8 suppressErr;               /* Do not issue error messages if true */
11109  u8 vtabOnConflict;            /* Value to return for s3_vtab_on_conflict() */
11110  u8 isTransactionSavepoint;    /* True if the outermost savepoint is a TS */
11111  int nextPagesize;             /* Pagesize after VACUUM if >0 */
11112  u32 magic;                    /* Magic number for detect library misuse */
11113  int nChange;                  /* Value returned by sqlite3_changes() */
11114  int nTotalChange;             /* Value returned by sqlite3_total_changes() */
11115  int aLimit[SQLITE_N_LIMIT];   /* Limits */
11116  int nMaxSorterMmap;           /* Maximum size of regions mapped by sorter */
11117  struct sqlite3InitInfo {      /* Information used during initialization */
11118    int newTnum;                /* Rootpage of table being initialized */
11119    u8 iDb;                     /* Which db file is being initialized */
11120    u8 busy;                    /* TRUE if currently initializing */
11121    u8 orphanTrigger;           /* Last statement is orphaned TEMP trigger */
11122    u8 imposterTable;           /* Building an imposter table */
11123  } init;
11124  int nVdbeActive;              /* Number of VDBEs currently running */
11125  int nVdbeRead;                /* Number of active VDBEs that read or write */
11126  int nVdbeWrite;               /* Number of active VDBEs that read and write */
11127  int nVdbeExec;                /* Number of nested calls to VdbeExec() */
11128  int nVDestroy;                /* Number of active OP_VDestroy operations */
11129  int nExtension;               /* Number of loaded extensions */
11130  void **aExtension;            /* Array of shared library handles */
11131  void (*xTrace)(void*,const char*);        /* Trace function */
11132  void *pTraceArg;                          /* Argument to the trace function */
11133  void (*xProfile)(void*,const char*,u64);  /* Profiling function */
11134  void *pProfileArg;                        /* Argument to profile function */
11135  void *pCommitArg;                 /* Argument to xCommitCallback() */
11136  int (*xCommitCallback)(void*);    /* Invoked at every commit. */
11137  void *pRollbackArg;               /* Argument to xRollbackCallback() */
11138  void (*xRollbackCallback)(void*); /* Invoked at every commit. */
11139  void *pUpdateArg;
11140  void (*xUpdateCallback)(void*,int, const char*,const char*,sqlite_int64);
11141#ifndef SQLITE_OMIT_WAL
11142  int (*xWalCallback)(void *, sqlite3 *, const char *, int);
11143  void *pWalArg;
11144#endif
11145  void(*xCollNeeded)(void*,sqlite3*,int eTextRep,const char*);
11146  void(*xCollNeeded16)(void*,sqlite3*,int eTextRep,const void*);
11147  void *pCollNeededArg;
11148  sqlite3_value *pErr;          /* Most recent error message */
11149  union {
11150    volatile int isInterrupted; /* True if sqlite3_interrupt has been called */
11151    double notUsed1;            /* Spacer */
11152  } u1;
11153  Lookaside lookaside;          /* Lookaside malloc configuration */
11154#ifndef SQLITE_OMIT_AUTHORIZATION
11155  sqlite3_xauth xAuth;          /* Access authorization function */
11156  void *pAuthArg;               /* 1st argument to the access auth function */
11157#endif
11158#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
11159  int (*xProgress)(void *);     /* The progress callback */
11160  void *pProgressArg;           /* Argument to the progress callback */
11161  unsigned nProgressOps;        /* Number of opcodes for progress callback */
11162#endif
11163#ifndef SQLITE_OMIT_VIRTUALTABLE
11164  int nVTrans;                  /* Allocated size of aVTrans */
11165  Hash aModule;                 /* populated by sqlite3_create_module() */
11166  VtabCtx *pVtabCtx;            /* Context for active vtab connect/create */
11167  VTable **aVTrans;             /* Virtual tables with open transactions */
11168  VTable *pDisconnect;    /* Disconnect these in next sqlite3_prepare() */
11169#endif
11170  FuncDefHash aFunc;            /* Hash table of connection functions */
11171  Hash aCollSeq;                /* All collating sequences */
11172  BusyHandler busyHandler;      /* Busy callback */
11173  Db aDbStatic[2];              /* Static space for the 2 default backends */
11174  Savepoint *pSavepoint;        /* List of active savepoints */
11175  int busyTimeout;              /* Busy handler timeout, in msec */
11176  int nSavepoint;               /* Number of non-transaction savepoints */
11177  int nStatement;               /* Number of nested statement-transactions  */
11178  i64 nDeferredCons;            /* Net deferred constraints this transaction. */
11179  i64 nDeferredImmCons;         /* Net deferred immediate constraints */
11180  int *pnBytesFreed;            /* If not NULL, increment this in DbFree() */
11181#ifdef SQLITE_ENABLE_UNLOCK_NOTIFY
11182  /* The following variables are all protected by the STATIC_MASTER
11183  ** mutex, not by sqlite3.mutex. They are used by code in notify.c.
11184  **
11185  ** When X.pUnlockConnection==Y, that means that X is waiting for Y to
11186  ** unlock so that it can proceed.
11187  **
11188  ** When X.pBlockingConnection==Y, that means that something that X tried
11189  ** tried to do recently failed with an SQLITE_LOCKED error due to locks
11190  ** held by Y.
11191  */
11192  sqlite3 *pBlockingConnection; /* Connection that caused SQLITE_LOCKED */
11193  sqlite3 *pUnlockConnection;           /* Connection to watch for unlock */
11194  void *pUnlockArg;                     /* Argument to xUnlockNotify */
11195  void (*xUnlockNotify)(void **, int);  /* Unlock notify callback */
11196  sqlite3 *pNextBlocked;        /* Next in list of all blocked connections */
11197#endif
11198#ifdef SQLITE_USER_AUTHENTICATION
11199  sqlite3_userauth auth;        /* User authentication information */
11200#endif
11201};
11202
11203/*
11204** A macro to discover the encoding of a database.
11205*/
11206#define SCHEMA_ENC(db) ((db)->aDb[0].pSchema->enc)
11207#define ENC(db)        ((db)->enc)
11208
11209/*
11210** Possible values for the sqlite3.flags.
11211*/
11212#define SQLITE_VdbeTrace      0x00000001  /* True to trace VDBE execution */
11213#define SQLITE_InternChanges  0x00000002  /* Uncommitted Hash table changes */
11214#define SQLITE_FullFSync      0x00000004  /* Use full fsync on the backend */
11215#define SQLITE_CkptFullFSync  0x00000008  /* Use full fsync for checkpoint */
11216#define SQLITE_CacheSpill     0x00000010  /* OK to spill pager cache */
11217#define SQLITE_FullColNames   0x00000020  /* Show full column names on SELECT */
11218#define SQLITE_ShortColNames  0x00000040  /* Show short columns names */
11219#define SQLITE_CountRows      0x00000080  /* Count rows changed by INSERT, */
11220                                          /*   DELETE, or UPDATE and return */
11221                                          /*   the count using a callback. */
11222#define SQLITE_NullCallback   0x00000100  /* Invoke the callback once if the */
11223                                          /*   result set is empty */
11224#define SQLITE_SqlTrace       0x00000200  /* Debug print SQL as it executes */
11225#define SQLITE_VdbeListing    0x00000400  /* Debug listings of VDBE programs */
11226#define SQLITE_WriteSchema    0x00000800  /* OK to update SQLITE_MASTER */
11227#define SQLITE_VdbeAddopTrace 0x00001000  /* Trace sqlite3VdbeAddOp() calls */
11228#define SQLITE_IgnoreChecks   0x00002000  /* Do not enforce check constraints */
11229#define SQLITE_ReadUncommitted 0x0004000  /* For shared-cache mode */
11230#define SQLITE_LegacyFileFmt  0x00008000  /* Create new databases in format 1 */
11231#define SQLITE_RecoveryMode   0x00010000  /* Ignore schema errors */
11232#define SQLITE_ReverseOrder   0x00020000  /* Reverse unordered SELECTs */
11233#define SQLITE_RecTriggers    0x00040000  /* Enable recursive triggers */
11234#define SQLITE_ForeignKeys    0x00080000  /* Enforce foreign key constraints  */
11235#define SQLITE_AutoIndex      0x00100000  /* Enable automatic indexes */
11236#define SQLITE_PreferBuiltin  0x00200000  /* Preference to built-in funcs */
11237#define SQLITE_LoadExtension  0x00400000  /* Enable load_extension */
11238#define SQLITE_EnableTrigger  0x00800000  /* True to enable triggers */
11239#define SQLITE_DeferFKs       0x01000000  /* Defer all FK constraints */
11240#define SQLITE_QueryOnly      0x02000000  /* Disable database changes */
11241#define SQLITE_VdbeEQP        0x04000000  /* Debug EXPLAIN QUERY PLAN */
11242#define SQLITE_Vacuum         0x08000000  /* Currently in a VACUUM */
11243#define SQLITE_CellSizeCk     0x10000000  /* Check btree cell sizes on load */
11244
11245
11246/*
11247** Bits of the sqlite3.dbOptFlags field that are used by the
11248** sqlite3_test_control(SQLITE_TESTCTRL_OPTIMIZATIONS,...) interface to
11249** selectively disable various optimizations.
11250*/
11251#define SQLITE_QueryFlattener 0x0001   /* Query flattening */
11252#define SQLITE_ColumnCache    0x0002   /* Column cache */
11253#define SQLITE_GroupByOrder   0x0004   /* GROUPBY cover of ORDERBY */
11254#define SQLITE_FactorOutConst 0x0008   /* Constant factoring */
11255/*                not used    0x0010   // Was: SQLITE_IdxRealAsInt */
11256#define SQLITE_DistinctOpt    0x0020   /* DISTINCT using indexes */
11257#define SQLITE_CoverIdxScan   0x0040   /* Covering index scans */
11258#define SQLITE_OrderByIdxJoin 0x0080   /* ORDER BY of joins via index */
11259#define SQLITE_SubqCoroutine  0x0100   /* Evaluate subqueries as coroutines */
11260#define SQLITE_Transitive     0x0200   /* Transitive constraints */
11261#define SQLITE_OmitNoopJoin   0x0400   /* Omit unused tables in joins */
11262#define SQLITE_Stat34         0x0800   /* Use STAT3 or STAT4 data */
11263#define SQLITE_AllOpts        0xffff   /* All optimizations */
11264
11265/*
11266** Macros for testing whether or not optimizations are enabled or disabled.
11267*/
11268#ifndef SQLITE_OMIT_BUILTIN_TEST
11269#define OptimizationDisabled(db, mask)  (((db)->dbOptFlags&(mask))!=0)
11270#define OptimizationEnabled(db, mask)   (((db)->dbOptFlags&(mask))==0)
11271#else
11272#define OptimizationDisabled(db, mask)  0
11273#define OptimizationEnabled(db, mask)   1
11274#endif
11275
11276/*
11277** Return true if it OK to factor constant expressions into the initialization
11278** code. The argument is a Parse object for the code generator.
11279*/
11280#define ConstFactorOk(P) ((P)->okConstFactor)
11281
11282/*
11283** Possible values for the sqlite.magic field.
11284** The numbers are obtained at random and have no special meaning, other
11285** than being distinct from one another.
11286*/
11287#define SQLITE_MAGIC_OPEN     0xa029a697  /* Database is open */
11288#define SQLITE_MAGIC_CLOSED   0x9f3c2d33  /* Database is closed */
11289#define SQLITE_MAGIC_SICK     0x4b771290  /* Error and awaiting close */
11290#define SQLITE_MAGIC_BUSY     0xf03b7906  /* Database currently in use */
11291#define SQLITE_MAGIC_ERROR    0xb5357930  /* An SQLITE_MISUSE error occurred */
11292#define SQLITE_MAGIC_ZOMBIE   0x64cffc7f  /* Close with last statement close */
11293
11294/*
11295** Each SQL function is defined by an instance of the following
11296** structure.  A pointer to this structure is stored in the sqlite.aFunc
11297** hash table.  When multiple functions have the same name, the hash table
11298** points to a linked list of these structures.
11299*/
11300struct FuncDef {
11301  i16 nArg;            /* Number of arguments.  -1 means unlimited */
11302  u16 funcFlags;       /* Some combination of SQLITE_FUNC_* */
11303  void *pUserData;     /* User data parameter */
11304  FuncDef *pNext;      /* Next function with same name */
11305  void (*xFunc)(sqlite3_context*,int,sqlite3_value**); /* Regular function */
11306  void (*xStep)(sqlite3_context*,int,sqlite3_value**); /* Aggregate step */
11307  void (*xFinalize)(sqlite3_context*);                /* Aggregate finalizer */
11308  char *zName;         /* SQL name of the function. */
11309  FuncDef *pHash;      /* Next with a different name but the same hash */
11310  FuncDestructor *pDestructor;   /* Reference counted destructor function */
11311};
11312
11313/*
11314** This structure encapsulates a user-function destructor callback (as
11315** configured using create_function_v2()) and a reference counter. When
11316** create_function_v2() is called to create a function with a destructor,
11317** a single object of this type is allocated. FuncDestructor.nRef is set to
11318** the number of FuncDef objects created (either 1 or 3, depending on whether
11319** or not the specified encoding is SQLITE_ANY). The FuncDef.pDestructor
11320** member of each of the new FuncDef objects is set to point to the allocated
11321** FuncDestructor.
11322**
11323** Thereafter, when one of the FuncDef objects is deleted, the reference
11324** count on this object is decremented. When it reaches 0, the destructor
11325** is invoked and the FuncDestructor structure freed.
11326*/
11327struct FuncDestructor {
11328  int nRef;
11329  void (*xDestroy)(void *);
11330  void *pUserData;
11331};
11332
11333/*
11334** Possible values for FuncDef.flags.  Note that the _LENGTH and _TYPEOF
11335** values must correspond to OPFLAG_LENGTHARG and OPFLAG_TYPEOFARG.  There
11336** are assert() statements in the code to verify this.
11337*/
11338#define SQLITE_FUNC_ENCMASK  0x003 /* SQLITE_UTF8, SQLITE_UTF16BE or UTF16LE */
11339#define SQLITE_FUNC_LIKE     0x004 /* Candidate for the LIKE optimization */
11340#define SQLITE_FUNC_CASE     0x008 /* Case-sensitive LIKE-type function */
11341#define SQLITE_FUNC_EPHEM    0x010 /* Ephemeral.  Delete with VDBE */
11342#define SQLITE_FUNC_NEEDCOLL 0x020 /* sqlite3GetFuncCollSeq() might be called */
11343#define SQLITE_FUNC_LENGTH   0x040 /* Built-in length() function */
11344#define SQLITE_FUNC_TYPEOF   0x080 /* Built-in typeof() function */
11345#define SQLITE_FUNC_COUNT    0x100 /* Built-in count(*) aggregate */
11346#define SQLITE_FUNC_COALESCE 0x200 /* Built-in coalesce() or ifnull() */
11347#define SQLITE_FUNC_UNLIKELY 0x400 /* Built-in unlikely() function */
11348#define SQLITE_FUNC_CONSTANT 0x800 /* Constant inputs give a constant output */
11349#define SQLITE_FUNC_MINMAX  0x1000 /* True for min() and max() aggregates */
11350
11351/*
11352** The following three macros, FUNCTION(), LIKEFUNC() and AGGREGATE() are
11353** used to create the initializers for the FuncDef structures.
11354**
11355**   FUNCTION(zName, nArg, iArg, bNC, xFunc)
11356**     Used to create a scalar function definition of a function zName
11357**     implemented by C function xFunc that accepts nArg arguments. The
11358**     value passed as iArg is cast to a (void*) and made available
11359**     as the user-data (sqlite3_user_data()) for the function. If
11360**     argument bNC is true, then the SQLITE_FUNC_NEEDCOLL flag is set.
11361**
11362**   VFUNCTION(zName, nArg, iArg, bNC, xFunc)
11363**     Like FUNCTION except it omits the SQLITE_FUNC_CONSTANT flag.
11364**
11365**   AGGREGATE(zName, nArg, iArg, bNC, xStep, xFinal)
11366**     Used to create an aggregate function definition implemented by
11367**     the C functions xStep and xFinal. The first four parameters
11368**     are interpreted in the same way as the first 4 parameters to
11369**     FUNCTION().
11370**
11371**   LIKEFUNC(zName, nArg, pArg, flags)
11372**     Used to create a scalar function definition of a function zName
11373**     that accepts nArg arguments and is implemented by a call to C
11374**     function likeFunc. Argument pArg is cast to a (void *) and made
11375**     available as the function user-data (sqlite3_user_data()). The
11376**     FuncDef.flags variable is set to the value passed as the flags
11377**     parameter.
11378*/
11379#define FUNCTION(zName, nArg, iArg, bNC, xFunc) \
11380  {nArg, SQLITE_FUNC_CONSTANT|SQLITE_UTF8|(bNC*SQLITE_FUNC_NEEDCOLL), \
11381   SQLITE_INT_TO_PTR(iArg), 0, xFunc, 0, 0, #zName, 0, 0}
11382#define VFUNCTION(zName, nArg, iArg, bNC, xFunc) \
11383  {nArg, SQLITE_UTF8|(bNC*SQLITE_FUNC_NEEDCOLL), \
11384   SQLITE_INT_TO_PTR(iArg), 0, xFunc, 0, 0, #zName, 0, 0}
11385#define FUNCTION2(zName, nArg, iArg, bNC, xFunc, extraFlags) \
11386  {nArg,SQLITE_FUNC_CONSTANT|SQLITE_UTF8|(bNC*SQLITE_FUNC_NEEDCOLL)|extraFlags,\
11387   SQLITE_INT_TO_PTR(iArg), 0, xFunc, 0, 0, #zName, 0, 0}
11388#define STR_FUNCTION(zName, nArg, pArg, bNC, xFunc) \
11389  {nArg, SQLITE_FUNC_CONSTANT|SQLITE_UTF8|(bNC*SQLITE_FUNC_NEEDCOLL), \
11390   pArg, 0, xFunc, 0, 0, #zName, 0, 0}
11391#define LIKEFUNC(zName, nArg, arg, flags) \
11392  {nArg, SQLITE_FUNC_CONSTANT|SQLITE_UTF8|flags, \
11393   (void *)arg, 0, likeFunc, 0, 0, #zName, 0, 0}
11394#define AGGREGATE(zName, nArg, arg, nc, xStep, xFinal) \
11395  {nArg, SQLITE_UTF8|(nc*SQLITE_FUNC_NEEDCOLL), \
11396   SQLITE_INT_TO_PTR(arg), 0, 0, xStep,xFinal,#zName,0,0}
11397#define AGGREGATE2(zName, nArg, arg, nc, xStep, xFinal, extraFlags) \
11398  {nArg, SQLITE_UTF8|(nc*SQLITE_FUNC_NEEDCOLL)|extraFlags, \
11399   SQLITE_INT_TO_PTR(arg), 0, 0, xStep,xFinal,#zName,0,0}
11400
11401/*
11402** All current savepoints are stored in a linked list starting at
11403** sqlite3.pSavepoint. The first element in the list is the most recently
11404** opened savepoint. Savepoints are added to the list by the vdbe
11405** OP_Savepoint instruction.
11406*/
11407struct Savepoint {
11408  char *zName;                        /* Savepoint name (nul-terminated) */
11409  i64 nDeferredCons;                  /* Number of deferred fk violations */
11410  i64 nDeferredImmCons;               /* Number of deferred imm fk. */
11411  Savepoint *pNext;                   /* Parent savepoint (if any) */
11412};
11413
11414/*
11415** The following are used as the second parameter to sqlite3Savepoint(),
11416** and as the P1 argument to the OP_Savepoint instruction.
11417*/
11418#define SAVEPOINT_BEGIN      0
11419#define SAVEPOINT_RELEASE    1
11420#define SAVEPOINT_ROLLBACK   2
11421
11422
11423/*
11424** Each SQLite module (virtual table definition) is defined by an
11425** instance of the following structure, stored in the sqlite3.aModule
11426** hash table.
11427*/
11428struct Module {
11429  const sqlite3_module *pModule;       /* Callback pointers */
11430  const char *zName;                   /* Name passed to create_module() */
11431  void *pAux;                          /* pAux passed to create_module() */
11432  void (*xDestroy)(void *);            /* Module destructor function */
11433};
11434
11435/*
11436** information about each column of an SQL table is held in an instance
11437** of this structure.
11438*/
11439struct Column {
11440  char *zName;     /* Name of this column */
11441  Expr *pDflt;     /* Default value of this column */
11442  char *zDflt;     /* Original text of the default value */
11443  char *zType;     /* Data type for this column */
11444  char *zColl;     /* Collating sequence.  If NULL, use the default */
11445  u8 notNull;      /* An OE_ code for handling a NOT NULL constraint */
11446  char affinity;   /* One of the SQLITE_AFF_... values */
11447  u8 szEst;        /* Estimated size of this column.  INT==1 */
11448  u8 colFlags;     /* Boolean properties.  See COLFLAG_ defines below */
11449};
11450
11451/* Allowed values for Column.colFlags:
11452*/
11453#define COLFLAG_PRIMKEY  0x0001    /* Column is part of the primary key */
11454#define COLFLAG_HIDDEN   0x0002    /* A hidden column in a virtual table */
11455
11456/*
11457** A "Collating Sequence" is defined by an instance of the following
11458** structure. Conceptually, a collating sequence consists of a name and
11459** a comparison routine that defines the order of that sequence.
11460**
11461** If CollSeq.xCmp is NULL, it means that the
11462** collating sequence is undefined.  Indices built on an undefined
11463** collating sequence may not be read or written.
11464*/
11465struct CollSeq {
11466  char *zName;          /* Name of the collating sequence, UTF-8 encoded */
11467  u8 enc;               /* Text encoding handled by xCmp() */
11468  void *pUser;          /* First argument to xCmp() */
11469  int (*xCmp)(void*,int, const void*, int, const void*);
11470  void (*xDel)(void*);  /* Destructor for pUser */
11471};
11472
11473/*
11474** A sort order can be either ASC or DESC.
11475*/
11476#define SQLITE_SO_ASC       0  /* Sort in ascending order */
11477#define SQLITE_SO_DESC      1  /* Sort in ascending order */
11478
11479/*
11480** Column affinity types.
11481**
11482** These used to have mnemonic name like 'i' for SQLITE_AFF_INTEGER and
11483** 't' for SQLITE_AFF_TEXT.  But we can save a little space and improve
11484** the speed a little by numbering the values consecutively.
11485**
11486** But rather than start with 0 or 1, we begin with 'A'.  That way,
11487** when multiple affinity types are concatenated into a string and
11488** used as the P4 operand, they will be more readable.
11489**
11490** Note also that the numeric types are grouped together so that testing
11491** for a numeric type is a single comparison.  And the BLOB type is first.
11492*/
11493#define SQLITE_AFF_BLOB     'A'
11494#define SQLITE_AFF_TEXT     'B'
11495#define SQLITE_AFF_NUMERIC  'C'
11496#define SQLITE_AFF_INTEGER  'D'
11497#define SQLITE_AFF_REAL     'E'
11498
11499#define sqlite3IsNumericAffinity(X)  ((X)>=SQLITE_AFF_NUMERIC)
11500
11501/*
11502** The SQLITE_AFF_MASK values masks off the significant bits of an
11503** affinity value.
11504*/
11505#define SQLITE_AFF_MASK     0x47
11506
11507/*
11508** Additional bit values that can be ORed with an affinity without
11509** changing the affinity.
11510**
11511** The SQLITE_NOTNULL flag is a combination of NULLEQ and JUMPIFNULL.
11512** It causes an assert() to fire if either operand to a comparison
11513** operator is NULL.  It is added to certain comparison operators to
11514** prove that the operands are always NOT NULL.
11515*/
11516#define SQLITE_JUMPIFNULL   0x10  /* jumps if either operand is NULL */
11517#define SQLITE_STOREP2      0x20  /* Store result in reg[P2] rather than jump */
11518#define SQLITE_NULLEQ       0x80  /* NULL=NULL */
11519#define SQLITE_NOTNULL      0x90  /* Assert that operands are never NULL */
11520
11521/*
11522** An object of this type is created for each virtual table present in
11523** the database schema.
11524**
11525** If the database schema is shared, then there is one instance of this
11526** structure for each database connection (sqlite3*) that uses the shared
11527** schema. This is because each database connection requires its own unique
11528** instance of the sqlite3_vtab* handle used to access the virtual table
11529** implementation. sqlite3_vtab* handles can not be shared between
11530** database connections, even when the rest of the in-memory database
11531** schema is shared, as the implementation often stores the database
11532** connection handle passed to it via the xConnect() or xCreate() method
11533** during initialization internally. This database connection handle may
11534** then be used by the virtual table implementation to access real tables
11535** within the database. So that they appear as part of the callers
11536** transaction, these accesses need to be made via the same database
11537** connection as that used to execute SQL operations on the virtual table.
11538**
11539** All VTable objects that correspond to a single table in a shared
11540** database schema are initially stored in a linked-list pointed to by
11541** the Table.pVTable member variable of the corresponding Table object.
11542** When an sqlite3_prepare() operation is required to access the virtual
11543** table, it searches the list for the VTable that corresponds to the
11544** database connection doing the preparing so as to use the correct
11545** sqlite3_vtab* handle in the compiled query.
11546**
11547** When an in-memory Table object is deleted (for example when the
11548** schema is being reloaded for some reason), the VTable objects are not
11549** deleted and the sqlite3_vtab* handles are not xDisconnect()ed
11550** immediately. Instead, they are moved from the Table.pVTable list to
11551** another linked list headed by the sqlite3.pDisconnect member of the
11552** corresponding sqlite3 structure. They are then deleted/xDisconnected
11553** next time a statement is prepared using said sqlite3*. This is done
11554** to avoid deadlock issues involving multiple sqlite3.mutex mutexes.
11555** Refer to comments above function sqlite3VtabUnlockList() for an
11556** explanation as to why it is safe to add an entry to an sqlite3.pDisconnect
11557** list without holding the corresponding sqlite3.mutex mutex.
11558**
11559** The memory for objects of this type is always allocated by
11560** sqlite3DbMalloc(), using the connection handle stored in VTable.db as
11561** the first argument.
11562*/
11563struct VTable {
11564  sqlite3 *db;              /* Database connection associated with this table */
11565  Module *pMod;             /* Pointer to module implementation */
11566  sqlite3_vtab *pVtab;      /* Pointer to vtab instance */
11567  int nRef;                 /* Number of pointers to this structure */
11568  u8 bConstraint;           /* True if constraints are supported */
11569  int iSavepoint;           /* Depth of the SAVEPOINT stack */
11570  VTable *pNext;            /* Next in linked list (see above) */
11571};
11572
11573/*
11574** The schema for each SQL table and view is represented in memory
11575** by an instance of the following structure.
11576*/
11577struct Table {
11578  char *zName;         /* Name of the table or view */
11579  Column *aCol;        /* Information about each column */
11580  Index *pIndex;       /* List of SQL indexes on this table. */
11581  Select *pSelect;     /* NULL for tables.  Points to definition if a view. */
11582  FKey *pFKey;         /* Linked list of all foreign keys in this table */
11583  char *zColAff;       /* String defining the affinity of each column */
11584#ifndef SQLITE_OMIT_CHECK
11585  ExprList *pCheck;    /* All CHECK constraints */
11586#endif
11587  int tnum;            /* Root BTree page for this table */
11588  i16 iPKey;           /* If not negative, use aCol[iPKey] as the rowid */
11589  i16 nCol;            /* Number of columns in this table */
11590  u16 nRef;            /* Number of pointers to this Table */
11591  LogEst nRowLogEst;   /* Estimated rows in table - from sqlite_stat1 table */
11592  LogEst szTabRow;     /* Estimated size of each table row in bytes */
11593#ifdef SQLITE_ENABLE_COSTMULT
11594  LogEst costMult;     /* Cost multiplier for using this table */
11595#endif
11596  u8 tabFlags;         /* Mask of TF_* values */
11597  u8 keyConf;          /* What to do in case of uniqueness conflict on iPKey */
11598#ifndef SQLITE_OMIT_ALTERTABLE
11599  int addColOffset;    /* Offset in CREATE TABLE stmt to add a new column */
11600#endif
11601#ifndef SQLITE_OMIT_VIRTUALTABLE
11602  int nModuleArg;      /* Number of arguments to the module */
11603  char **azModuleArg;  /* Text of all module args. [0] is module name */
11604  VTable *pVTable;     /* List of VTable objects. */
11605#endif
11606  Trigger *pTrigger;   /* List of triggers stored in pSchema */
11607  Schema *pSchema;     /* Schema that contains this table */
11608  Table *pNextZombie;  /* Next on the Parse.pZombieTab list */
11609};
11610
11611/*
11612** Allowed values for Table.tabFlags.
11613**
11614** TF_OOOHidden applies to virtual tables that have hidden columns that are
11615** followed by non-hidden columns.  Example:  "CREATE VIRTUAL TABLE x USING
11616** vtab1(a HIDDEN, b);".  Since "b" is a non-hidden column but "a" is hidden,
11617** the TF_OOOHidden attribute would apply in this case.  Such tables require
11618** special handling during INSERT processing.
11619*/
11620#define TF_Readonly        0x01    /* Read-only system table */
11621#define TF_Ephemeral       0x02    /* An ephemeral table */
11622#define TF_HasPrimaryKey   0x04    /* Table has a primary key */
11623#define TF_Autoincrement   0x08    /* Integer primary key is autoincrement */
11624#define TF_Virtual         0x10    /* Is a virtual table */
11625#define TF_WithoutRowid    0x20    /* No rowid.  PRIMARY KEY is the key */
11626#define TF_NoVisibleRowid  0x40    /* No user-visible "rowid" column */
11627#define TF_OOOHidden       0x80    /* Out-of-Order hidden columns */
11628
11629
11630/*
11631** Test to see whether or not a table is a virtual table.  This is
11632** done as a macro so that it will be optimized out when virtual
11633** table support is omitted from the build.
11634*/
11635#ifndef SQLITE_OMIT_VIRTUALTABLE
11636#  define IsVirtual(X)      (((X)->tabFlags & TF_Virtual)!=0)
11637#  define IsHiddenColumn(X) (((X)->colFlags & COLFLAG_HIDDEN)!=0)
11638#else
11639#  define IsVirtual(X)      0
11640#  define IsHiddenColumn(X) 0
11641#endif
11642
11643/* Does the table have a rowid */
11644#define HasRowid(X)     (((X)->tabFlags & TF_WithoutRowid)==0)
11645#define VisibleRowid(X) (((X)->tabFlags & TF_NoVisibleRowid)==0)
11646
11647/*
11648** Each foreign key constraint is an instance of the following structure.
11649**
11650** A foreign key is associated with two tables.  The "from" table is
11651** the table that contains the REFERENCES clause that creates the foreign
11652** key.  The "to" table is the table that is named in the REFERENCES clause.
11653** Consider this example:
11654**
11655**     CREATE TABLE ex1(
11656**       a INTEGER PRIMARY KEY,
11657**       b INTEGER CONSTRAINT fk1 REFERENCES ex2(x)
11658**     );
11659**
11660** For foreign key "fk1", the from-table is "ex1" and the to-table is "ex2".
11661** Equivalent names:
11662**
11663**     from-table == child-table
11664**       to-table == parent-table
11665**
11666** Each REFERENCES clause generates an instance of the following structure
11667** which is attached to the from-table.  The to-table need not exist when
11668** the from-table is created.  The existence of the to-table is not checked.
11669**
11670** The list of all parents for child Table X is held at X.pFKey.
11671**
11672** A list of all children for a table named Z (which might not even exist)
11673** is held in Schema.fkeyHash with a hash key of Z.
11674*/
11675struct FKey {
11676  Table *pFrom;     /* Table containing the REFERENCES clause (aka: Child) */
11677  FKey *pNextFrom;  /* Next FKey with the same in pFrom. Next parent of pFrom */
11678  char *zTo;        /* Name of table that the key points to (aka: Parent) */
11679  FKey *pNextTo;    /* Next with the same zTo. Next child of zTo. */
11680  FKey *pPrevTo;    /* Previous with the same zTo */
11681  int nCol;         /* Number of columns in this key */
11682  /* EV: R-30323-21917 */
11683  u8 isDeferred;       /* True if constraint checking is deferred till COMMIT */
11684  u8 aAction[2];        /* ON DELETE and ON UPDATE actions, respectively */
11685  Trigger *apTrigger[2];/* Triggers for aAction[] actions */
11686  struct sColMap {      /* Mapping of columns in pFrom to columns in zTo */
11687    int iFrom;            /* Index of column in pFrom */
11688    char *zCol;           /* Name of column in zTo.  If NULL use PRIMARY KEY */
11689  } aCol[1];            /* One entry for each of nCol columns */
11690};
11691
11692/*
11693** SQLite supports many different ways to resolve a constraint
11694** error.  ROLLBACK processing means that a constraint violation
11695** causes the operation in process to fail and for the current transaction
11696** to be rolled back.  ABORT processing means the operation in process
11697** fails and any prior changes from that one operation are backed out,
11698** but the transaction is not rolled back.  FAIL processing means that
11699** the operation in progress stops and returns an error code.  But prior
11700** changes due to the same operation are not backed out and no rollback
11701** occurs.  IGNORE means that the particular row that caused the constraint
11702** error is not inserted or updated.  Processing continues and no error
11703** is returned.  REPLACE means that preexisting database rows that caused
11704** a UNIQUE constraint violation are removed so that the new insert or
11705** update can proceed.  Processing continues and no error is reported.
11706**
11707** RESTRICT, SETNULL, and CASCADE actions apply only to foreign keys.
11708** RESTRICT is the same as ABORT for IMMEDIATE foreign keys and the
11709** same as ROLLBACK for DEFERRED keys.  SETNULL means that the foreign
11710** key is set to NULL.  CASCADE means that a DELETE or UPDATE of the
11711** referenced table row is propagated into the row that holds the
11712** foreign key.
11713**
11714** The following symbolic values are used to record which type
11715** of action to take.
11716*/
11717#define OE_None     0   /* There is no constraint to check */
11718#define OE_Rollback 1   /* Fail the operation and rollback the transaction */
11719#define OE_Abort    2   /* Back out changes but do no rollback transaction */
11720#define OE_Fail     3   /* Stop the operation but leave all prior changes */
11721#define OE_Ignore   4   /* Ignore the error. Do not do the INSERT or UPDATE */
11722#define OE_Replace  5   /* Delete existing record, then do INSERT or UPDATE */
11723
11724#define OE_Restrict 6   /* OE_Abort for IMMEDIATE, OE_Rollback for DEFERRED */
11725#define OE_SetNull  7   /* Set the foreign key value to NULL */
11726#define OE_SetDflt  8   /* Set the foreign key value to its default */
11727#define OE_Cascade  9   /* Cascade the changes */
11728
11729#define OE_Default  10  /* Do whatever the default action is */
11730
11731
11732/*
11733** An instance of the following structure is passed as the first
11734** argument to sqlite3VdbeKeyCompare and is used to control the
11735** comparison of the two index keys.
11736**
11737** Note that aSortOrder[] and aColl[] have nField+1 slots.  There
11738** are nField slots for the columns of an index then one extra slot
11739** for the rowid at the end.
11740*/
11741struct KeyInfo {
11742  u32 nRef;           /* Number of references to this KeyInfo object */
11743  u8 enc;             /* Text encoding - one of the SQLITE_UTF* values */
11744  u16 nField;         /* Number of key columns in the index */
11745  u16 nXField;        /* Number of columns beyond the key columns */
11746  sqlite3 *db;        /* The database connection */
11747  u8 *aSortOrder;     /* Sort order for each column. */
11748  CollSeq *aColl[1];  /* Collating sequence for each term of the key */
11749};
11750
11751/*
11752** An instance of the following structure holds information about a
11753** single index record that has already been parsed out into individual
11754** values.
11755**
11756** A record is an object that contains one or more fields of data.
11757** Records are used to store the content of a table row and to store
11758** the key of an index.  A blob encoding of a record is created by
11759** the OP_MakeRecord opcode of the VDBE and is disassembled by the
11760** OP_Column opcode.
11761**
11762** This structure holds a record that has already been disassembled
11763** into its constituent fields.
11764**
11765** The r1 and r2 member variables are only used by the optimized comparison
11766** functions vdbeRecordCompareInt() and vdbeRecordCompareString().
11767*/
11768struct UnpackedRecord {
11769  KeyInfo *pKeyInfo;  /* Collation and sort-order information */
11770  u16 nField;         /* Number of entries in apMem[] */
11771  i8 default_rc;      /* Comparison result if keys are equal */
11772  u8 errCode;         /* Error detected by xRecordCompare (CORRUPT or NOMEM) */
11773  Mem *aMem;          /* Values */
11774  int r1;             /* Value to return if (lhs > rhs) */
11775  int r2;             /* Value to return if (rhs < lhs) */
11776};
11777
11778
11779/*
11780** Each SQL index is represented in memory by an
11781** instance of the following structure.
11782**
11783** The columns of the table that are to be indexed are described
11784** by the aiColumn[] field of this structure.  For example, suppose
11785** we have the following table and index:
11786**
11787**     CREATE TABLE Ex1(c1 int, c2 int, c3 text);
11788**     CREATE INDEX Ex2 ON Ex1(c3,c1);
11789**
11790** In the Table structure describing Ex1, nCol==3 because there are
11791** three columns in the table.  In the Index structure describing
11792** Ex2, nColumn==2 since 2 of the 3 columns of Ex1 are indexed.
11793** The value of aiColumn is {2, 0}.  aiColumn[0]==2 because the
11794** first column to be indexed (c3) has an index of 2 in Ex1.aCol[].
11795** The second column to be indexed (c1) has an index of 0 in
11796** Ex1.aCol[], hence Ex2.aiColumn[1]==0.
11797**
11798** The Index.onError field determines whether or not the indexed columns
11799** must be unique and what to do if they are not.  When Index.onError=OE_None,
11800** it means this is not a unique index.  Otherwise it is a unique index
11801** and the value of Index.onError indicate the which conflict resolution
11802** algorithm to employ whenever an attempt is made to insert a non-unique
11803** element.
11804**
11805** While parsing a CREATE TABLE or CREATE INDEX statement in order to
11806** generate VDBE code (as opposed to parsing one read from an sqlite_master
11807** table as part of parsing an existing database schema), transient instances
11808** of this structure may be created. In this case the Index.tnum variable is
11809** used to store the address of a VDBE instruction, not a database page
11810** number (it cannot - the database page is not allocated until the VDBE
11811** program is executed). See convertToWithoutRowidTable() for details.
11812*/
11813struct Index {
11814  char *zName;             /* Name of this index */
11815  i16 *aiColumn;           /* Which columns are used by this index.  1st is 0 */
11816  LogEst *aiRowLogEst;     /* From ANALYZE: Est. rows selected by each column */
11817  Table *pTable;           /* The SQL table being indexed */
11818  char *zColAff;           /* String defining the affinity of each column */
11819  Index *pNext;            /* The next index associated with the same table */
11820  Schema *pSchema;         /* Schema containing this index */
11821  u8 *aSortOrder;          /* for each column: True==DESC, False==ASC */
11822  char **azColl;           /* Array of collation sequence names for index */
11823  Expr *pPartIdxWhere;     /* WHERE clause for partial indices */
11824  int tnum;                /* DB Page containing root of this index */
11825  LogEst szIdxRow;         /* Estimated average row size in bytes */
11826  u16 nKeyCol;             /* Number of columns forming the key */
11827  u16 nColumn;             /* Number of columns stored in the index */
11828  u8 onError;              /* OE_Abort, OE_Ignore, OE_Replace, or OE_None */
11829  unsigned idxType:2;      /* 1==UNIQUE, 2==PRIMARY KEY, 0==CREATE INDEX */
11830  unsigned bUnordered:1;   /* Use this index for == or IN queries only */
11831  unsigned uniqNotNull:1;  /* True if UNIQUE and NOT NULL for all columns */
11832  unsigned isResized:1;    /* True if resizeIndexObject() has been called */
11833  unsigned isCovering:1;   /* True if this is a covering index */
11834  unsigned noSkipScan:1;   /* Do not try to use skip-scan if true */
11835#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
11836  int nSample;             /* Number of elements in aSample[] */
11837  int nSampleCol;          /* Size of IndexSample.anEq[] and so on */
11838  tRowcnt *aAvgEq;         /* Average nEq values for keys not in aSample */
11839  IndexSample *aSample;    /* Samples of the left-most key */
11840  tRowcnt *aiRowEst;       /* Non-logarithmic stat1 data for this index */
11841  tRowcnt nRowEst0;        /* Non-logarithmic number of rows in the index */
11842#endif
11843};
11844
11845/*
11846** Allowed values for Index.idxType
11847*/
11848#define SQLITE_IDXTYPE_APPDEF      0   /* Created using CREATE INDEX */
11849#define SQLITE_IDXTYPE_UNIQUE      1   /* Implements a UNIQUE constraint */
11850#define SQLITE_IDXTYPE_PRIMARYKEY  2   /* Is the PRIMARY KEY for the table */
11851
11852/* Return true if index X is a PRIMARY KEY index */
11853#define IsPrimaryKeyIndex(X)  ((X)->idxType==SQLITE_IDXTYPE_PRIMARYKEY)
11854
11855/* Return true if index X is a UNIQUE index */
11856#define IsUniqueIndex(X)      ((X)->onError!=OE_None)
11857
11858/*
11859** Each sample stored in the sqlite_stat3 table is represented in memory
11860** using a structure of this type.  See documentation at the top of the
11861** analyze.c source file for additional information.
11862*/
11863struct IndexSample {
11864  void *p;          /* Pointer to sampled record */
11865  int n;            /* Size of record in bytes */
11866  tRowcnt *anEq;    /* Est. number of rows where the key equals this sample */
11867  tRowcnt *anLt;    /* Est. number of rows where key is less than this sample */
11868  tRowcnt *anDLt;   /* Est. number of distinct keys less than this sample */
11869};
11870
11871/*
11872** Each token coming out of the lexer is an instance of
11873** this structure.  Tokens are also used as part of an expression.
11874**
11875** Note if Token.z==0 then Token.dyn and Token.n are undefined and
11876** may contain random values.  Do not make any assumptions about Token.dyn
11877** and Token.n when Token.z==0.
11878*/
11879struct Token {
11880  const char *z;     /* Text of the token.  Not NULL-terminated! */
11881  unsigned int n;    /* Number of characters in this token */
11882};
11883
11884/*
11885** An instance of this structure contains information needed to generate
11886** code for a SELECT that contains aggregate functions.
11887**
11888** If Expr.op==TK_AGG_COLUMN or TK_AGG_FUNCTION then Expr.pAggInfo is a
11889** pointer to this structure.  The Expr.iColumn field is the index in
11890** AggInfo.aCol[] or AggInfo.aFunc[] of information needed to generate
11891** code for that node.
11892**
11893** AggInfo.pGroupBy and AggInfo.aFunc.pExpr point to fields within the
11894** original Select structure that describes the SELECT statement.  These
11895** fields do not need to be freed when deallocating the AggInfo structure.
11896*/
11897struct AggInfo {
11898  u8 directMode;          /* Direct rendering mode means take data directly
11899                          ** from source tables rather than from accumulators */
11900  u8 useSortingIdx;       /* In direct mode, reference the sorting index rather
11901                          ** than the source table */
11902  int sortingIdx;         /* Cursor number of the sorting index */
11903  int sortingIdxPTab;     /* Cursor number of pseudo-table */
11904  int nSortingColumn;     /* Number of columns in the sorting index */
11905  int mnReg, mxReg;       /* Range of registers allocated for aCol and aFunc */
11906  ExprList *pGroupBy;     /* The group by clause */
11907  struct AggInfo_col {    /* For each column used in source tables */
11908    Table *pTab;             /* Source table */
11909    int iTable;              /* Cursor number of the source table */
11910    int iColumn;             /* Column number within the source table */
11911    int iSorterColumn;       /* Column number in the sorting index */
11912    int iMem;                /* Memory location that acts as accumulator */
11913    Expr *pExpr;             /* The original expression */
11914  } *aCol;
11915  int nColumn;            /* Number of used entries in aCol[] */
11916  int nAccumulator;       /* Number of columns that show through to the output.
11917                          ** Additional columns are used only as parameters to
11918                          ** aggregate functions */
11919  struct AggInfo_func {   /* For each aggregate function */
11920    Expr *pExpr;             /* Expression encoding the function */
11921    FuncDef *pFunc;          /* The aggregate function implementation */
11922    int iMem;                /* Memory location that acts as accumulator */
11923    int iDistinct;           /* Ephemeral table used to enforce DISTINCT */
11924  } *aFunc;
11925  int nFunc;              /* Number of entries in aFunc[] */
11926};
11927
11928/*
11929** The datatype ynVar is a signed integer, either 16-bit or 32-bit.
11930** Usually it is 16-bits.  But if SQLITE_MAX_VARIABLE_NUMBER is greater
11931** than 32767 we have to make it 32-bit.  16-bit is preferred because
11932** it uses less memory in the Expr object, which is a big memory user
11933** in systems with lots of prepared statements.  And few applications
11934** need more than about 10 or 20 variables.  But some extreme users want
11935** to have prepared statements with over 32767 variables, and for them
11936** the option is available (at compile-time).
11937*/
11938#if SQLITE_MAX_VARIABLE_NUMBER<=32767
11939typedef i16 ynVar;
11940#else
11941typedef int ynVar;
11942#endif
11943
11944/*
11945** Each node of an expression in the parse tree is an instance
11946** of this structure.
11947**
11948** Expr.op is the opcode. The integer parser token codes are reused
11949** as opcodes here. For example, the parser defines TK_GE to be an integer
11950** code representing the ">=" operator. This same integer code is reused
11951** to represent the greater-than-or-equal-to operator in the expression
11952** tree.
11953**
11954** If the expression is an SQL literal (TK_INTEGER, TK_FLOAT, TK_BLOB,
11955** or TK_STRING), then Expr.token contains the text of the SQL literal. If
11956** the expression is a variable (TK_VARIABLE), then Expr.token contains the
11957** variable name. Finally, if the expression is an SQL function (TK_FUNCTION),
11958** then Expr.token contains the name of the function.
11959**
11960** Expr.pRight and Expr.pLeft are the left and right subexpressions of a
11961** binary operator. Either or both may be NULL.
11962**
11963** Expr.x.pList is a list of arguments if the expression is an SQL function,
11964** a CASE expression or an IN expression of the form "<lhs> IN (<y>, <z>...)".
11965** Expr.x.pSelect is used if the expression is a sub-select or an expression of
11966** the form "<lhs> IN (SELECT ...)". If the EP_xIsSelect bit is set in the
11967** Expr.flags mask, then Expr.x.pSelect is valid. Otherwise, Expr.x.pList is
11968** valid.
11969**
11970** An expression of the form ID or ID.ID refers to a column in a table.
11971** For such expressions, Expr.op is set to TK_COLUMN and Expr.iTable is
11972** the integer cursor number of a VDBE cursor pointing to that table and
11973** Expr.iColumn is the column number for the specific column.  If the
11974** expression is used as a result in an aggregate SELECT, then the
11975** value is also stored in the Expr.iAgg column in the aggregate so that
11976** it can be accessed after all aggregates are computed.
11977**
11978** If the expression is an unbound variable marker (a question mark
11979** character '?' in the original SQL) then the Expr.iTable holds the index
11980** number for that variable.
11981**
11982** If the expression is a subquery then Expr.iColumn holds an integer
11983** register number containing the result of the subquery.  If the
11984** subquery gives a constant result, then iTable is -1.  If the subquery
11985** gives a different answer at different times during statement processing
11986** then iTable is the address of a subroutine that computes the subquery.
11987**
11988** If the Expr is of type OP_Column, and the table it is selecting from
11989** is a disk table or the "old.*" pseudo-table, then pTab points to the
11990** corresponding table definition.
11991**
11992** ALLOCATION NOTES:
11993**
11994** Expr objects can use a lot of memory space in database schema.  To
11995** help reduce memory requirements, sometimes an Expr object will be
11996** truncated.  And to reduce the number of memory allocations, sometimes
11997** two or more Expr objects will be stored in a single memory allocation,
11998** together with Expr.zToken strings.
11999**
12000** If the EP_Reduced and EP_TokenOnly flags are set when
12001** an Expr object is truncated.  When EP_Reduced is set, then all
12002** the child Expr objects in the Expr.pLeft and Expr.pRight subtrees
12003** are contained within the same memory allocation.  Note, however, that
12004** the subtrees in Expr.x.pList or Expr.x.pSelect are always separately
12005** allocated, regardless of whether or not EP_Reduced is set.
12006*/
12007struct Expr {
12008  u8 op;                 /* Operation performed by this node */
12009  char affinity;         /* The affinity of the column or 0 if not a column */
12010  u32 flags;             /* Various flags.  EP_* See below */
12011  union {
12012    char *zToken;          /* Token value. Zero terminated and dequoted */
12013    int iValue;            /* Non-negative integer value if EP_IntValue */
12014  } u;
12015
12016  /* If the EP_TokenOnly flag is set in the Expr.flags mask, then no
12017  ** space is allocated for the fields below this point. An attempt to
12018  ** access them will result in a segfault or malfunction.
12019  *********************************************************************/
12020
12021  Expr *pLeft;           /* Left subnode */
12022  Expr *pRight;          /* Right subnode */
12023  union {
12024    ExprList *pList;     /* op = IN, EXISTS, SELECT, CASE, FUNCTION, BETWEEN */
12025    Select *pSelect;     /* EP_xIsSelect and op = IN, EXISTS, SELECT */
12026  } x;
12027
12028  /* If the EP_Reduced flag is set in the Expr.flags mask, then no
12029  ** space is allocated for the fields below this point. An attempt to
12030  ** access them will result in a segfault or malfunction.
12031  *********************************************************************/
12032
12033#if SQLITE_MAX_EXPR_DEPTH>0
12034  int nHeight;           /* Height of the tree headed by this node */
12035#endif
12036  int iTable;            /* TK_COLUMN: cursor number of table holding column
12037                         ** TK_REGISTER: register number
12038                         ** TK_TRIGGER: 1 -> new, 0 -> old
12039                         ** EP_Unlikely:  134217728 times likelihood */
12040  ynVar iColumn;         /* TK_COLUMN: column index.  -1 for rowid.
12041                         ** TK_VARIABLE: variable number (always >= 1). */
12042  i16 iAgg;              /* Which entry in pAggInfo->aCol[] or ->aFunc[] */
12043  i16 iRightJoinTable;   /* If EP_FromJoin, the right table of the join */
12044  u8 op2;                /* TK_REGISTER: original value of Expr.op
12045                         ** TK_COLUMN: the value of p5 for OP_Column
12046                         ** TK_AGG_FUNCTION: nesting depth */
12047  AggInfo *pAggInfo;     /* Used by TK_AGG_COLUMN and TK_AGG_FUNCTION */
12048  Table *pTab;           /* Table for TK_COLUMN expressions. */
12049};
12050
12051/*
12052** The following are the meanings of bits in the Expr.flags field.
12053*/
12054#define EP_FromJoin  0x000001 /* Originates in ON/USING clause of outer join */
12055#define EP_Agg       0x000002 /* Contains one or more aggregate functions */
12056#define EP_Resolved  0x000004 /* IDs have been resolved to COLUMNs */
12057#define EP_Error     0x000008 /* Expression contains one or more errors */
12058#define EP_Distinct  0x000010 /* Aggregate function with DISTINCT keyword */
12059#define EP_VarSelect 0x000020 /* pSelect is correlated, not constant */
12060#define EP_DblQuoted 0x000040 /* token.z was originally in "..." */
12061#define EP_InfixFunc 0x000080 /* True for an infix function: LIKE, GLOB, etc */
12062#define EP_Collate   0x000100 /* Tree contains a TK_COLLATE operator */
12063#define EP_Generic   0x000200 /* Ignore COLLATE or affinity on this tree */
12064#define EP_IntValue  0x000400 /* Integer value contained in u.iValue */
12065#define EP_xIsSelect 0x000800 /* x.pSelect is valid (otherwise x.pList is) */
12066#define EP_Skip      0x001000 /* COLLATE, AS, or UNLIKELY */
12067#define EP_Reduced   0x002000 /* Expr struct EXPR_REDUCEDSIZE bytes only */
12068#define EP_TokenOnly 0x004000 /* Expr struct EXPR_TOKENONLYSIZE bytes only */
12069#define EP_Static    0x008000 /* Held in memory not obtained from malloc() */
12070#define EP_MemToken  0x010000 /* Need to sqlite3DbFree() Expr.zToken */
12071#define EP_NoReduce  0x020000 /* Cannot EXPRDUP_REDUCE this Expr */
12072#define EP_Unlikely  0x040000 /* unlikely() or likelihood() function */
12073#define EP_ConstFunc 0x080000 /* Node is a SQLITE_FUNC_CONSTANT function */
12074#define EP_CanBeNull 0x100000 /* Can be null despite NOT NULL constraint */
12075#define EP_Subquery  0x200000 /* Tree contains a TK_SELECT operator */
12076
12077/*
12078** Combinations of two or more EP_* flags
12079*/
12080#define EP_Propagate (EP_Collate|EP_Subquery) /* Propagate these bits up tree */
12081
12082/*
12083** These macros can be used to test, set, or clear bits in the
12084** Expr.flags field.
12085*/
12086#define ExprHasProperty(E,P)     (((E)->flags&(P))!=0)
12087#define ExprHasAllProperty(E,P)  (((E)->flags&(P))==(P))
12088#define ExprSetProperty(E,P)     (E)->flags|=(P)
12089#define ExprClearProperty(E,P)   (E)->flags&=~(P)
12090
12091/* The ExprSetVVAProperty() macro is used for Verification, Validation,
12092** and Accreditation only.  It works like ExprSetProperty() during VVA
12093** processes but is a no-op for delivery.
12094*/
12095#ifdef SQLITE_DEBUG
12096# define ExprSetVVAProperty(E,P)  (E)->flags|=(P)
12097#else
12098# define ExprSetVVAProperty(E,P)
12099#endif
12100
12101/*
12102** Macros to determine the number of bytes required by a normal Expr
12103** struct, an Expr struct with the EP_Reduced flag set in Expr.flags
12104** and an Expr struct with the EP_TokenOnly flag set.
12105*/
12106#define EXPR_FULLSIZE           sizeof(Expr)           /* Full size */
12107#define EXPR_REDUCEDSIZE        offsetof(Expr,iTable)  /* Common features */
12108#define EXPR_TOKENONLYSIZE      offsetof(Expr,pLeft)   /* Fewer features */
12109
12110/*
12111** Flags passed to the sqlite3ExprDup() function. See the header comment
12112** above sqlite3ExprDup() for details.
12113*/
12114#define EXPRDUP_REDUCE         0x0001  /* Used reduced-size Expr nodes */
12115
12116/*
12117** A list of expressions.  Each expression may optionally have a
12118** name.  An expr/name combination can be used in several ways, such
12119** as the list of "expr AS ID" fields following a "SELECT" or in the
12120** list of "ID = expr" items in an UPDATE.  A list of expressions can
12121** also be used as the argument to a function, in which case the a.zName
12122** field is not used.
12123**
12124** By default the Expr.zSpan field holds a human-readable description of
12125** the expression that is used in the generation of error messages and
12126** column labels.  In this case, Expr.zSpan is typically the text of a
12127** column expression as it exists in a SELECT statement.  However, if
12128** the bSpanIsTab flag is set, then zSpan is overloaded to mean the name
12129** of the result column in the form: DATABASE.TABLE.COLUMN.  This later
12130** form is used for name resolution with nested FROM clauses.
12131*/
12132struct ExprList {
12133  int nExpr;             /* Number of expressions on the list */
12134  struct ExprList_item { /* For each expression in the list */
12135    Expr *pExpr;            /* The list of expressions */
12136    char *zName;            /* Token associated with this expression */
12137    char *zSpan;            /* Original text of the expression */
12138    u8 sortOrder;           /* 1 for DESC or 0 for ASC */
12139    unsigned done :1;       /* A flag to indicate when processing is finished */
12140    unsigned bSpanIsTab :1; /* zSpan holds DB.TABLE.COLUMN */
12141    unsigned reusable :1;   /* Constant expression is reusable */
12142    union {
12143      struct {
12144        u16 iOrderByCol;      /* For ORDER BY, column number in result set */
12145        u16 iAlias;           /* Index into Parse.aAlias[] for zName */
12146      } x;
12147      int iConstExprReg;      /* Register in which Expr value is cached */
12148    } u;
12149  } *a;                  /* Alloc a power of two greater or equal to nExpr */
12150};
12151
12152/*
12153** An instance of this structure is used by the parser to record both
12154** the parse tree for an expression and the span of input text for an
12155** expression.
12156*/
12157struct ExprSpan {
12158  Expr *pExpr;          /* The expression parse tree */
12159  const char *zStart;   /* First character of input text */
12160  const char *zEnd;     /* One character past the end of input text */
12161};
12162
12163/*
12164** An instance of this structure can hold a simple list of identifiers,
12165** such as the list "a,b,c" in the following statements:
12166**
12167**      INSERT INTO t(a,b,c) VALUES ...;
12168**      CREATE INDEX idx ON t(a,b,c);
12169**      CREATE TRIGGER trig BEFORE UPDATE ON t(a,b,c) ...;
12170**
12171** The IdList.a.idx field is used when the IdList represents the list of
12172** column names after a table name in an INSERT statement.  In the statement
12173**
12174**     INSERT INTO t(a,b,c) ...
12175**
12176** If "a" is the k-th column of table "t", then IdList.a[0].idx==k.
12177*/
12178struct IdList {
12179  struct IdList_item {
12180    char *zName;      /* Name of the identifier */
12181    int idx;          /* Index in some Table.aCol[] of a column named zName */
12182  } *a;
12183  int nId;         /* Number of identifiers on the list */
12184};
12185
12186/*
12187** The bitmask datatype defined below is used for various optimizations.
12188**
12189** Changing this from a 64-bit to a 32-bit type limits the number of
12190** tables in a join to 32 instead of 64.  But it also reduces the size
12191** of the library by 738 bytes on ix86.
12192*/
12193typedef u64 Bitmask;
12194
12195/*
12196** The number of bits in a Bitmask.  "BMS" means "BitMask Size".
12197*/
12198#define BMS  ((int)(sizeof(Bitmask)*8))
12199
12200/*
12201** A bit in a Bitmask
12202*/
12203#define MASKBIT(n)   (((Bitmask)1)<<(n))
12204#define MASKBIT32(n) (((unsigned int)1)<<(n))
12205
12206/*
12207** The following structure describes the FROM clause of a SELECT statement.
12208** Each table or subquery in the FROM clause is a separate element of
12209** the SrcList.a[] array.
12210**
12211** With the addition of multiple database support, the following structure
12212** can also be used to describe a particular table such as the table that
12213** is modified by an INSERT, DELETE, or UPDATE statement.  In standard SQL,
12214** such a table must be a simple name: ID.  But in SQLite, the table can
12215** now be identified by a database name, a dot, then the table name: ID.ID.
12216**
12217** The jointype starts out showing the join type between the current table
12218** and the next table on the list.  The parser builds the list this way.
12219** But sqlite3SrcListShiftJoinType() later shifts the jointypes so that each
12220** jointype expresses the join between the table and the previous table.
12221**
12222** In the colUsed field, the high-order bit (bit 63) is set if the table
12223** contains more than 63 columns and the 64-th or later column is used.
12224*/
12225struct SrcList {
12226  int nSrc;        /* Number of tables or subqueries in the FROM clause */
12227  u32 nAlloc;      /* Number of entries allocated in a[] below */
12228  struct SrcList_item {
12229    Schema *pSchema;  /* Schema to which this item is fixed */
12230    char *zDatabase;  /* Name of database holding this table */
12231    char *zName;      /* Name of the table */
12232    char *zAlias;     /* The "B" part of a "A AS B" phrase.  zName is the "A" */
12233    Table *pTab;      /* An SQL table corresponding to zName */
12234    Select *pSelect;  /* A SELECT statement used in place of a table name */
12235    int addrFillSub;  /* Address of subroutine to manifest a subquery */
12236    int regReturn;    /* Register holding return address of addrFillSub */
12237    int regResult;    /* Registers holding results of a co-routine */
12238    u8 jointype;      /* Type of join between this able and the previous */
12239    unsigned notIndexed :1;    /* True if there is a NOT INDEXED clause */
12240    unsigned isCorrelated :1;  /* True if sub-query is correlated */
12241    unsigned viaCoroutine :1;  /* Implemented as a co-routine */
12242    unsigned isRecursive :1;   /* True for recursive reference in WITH */
12243#ifndef SQLITE_OMIT_EXPLAIN
12244    u8 iSelectId;     /* If pSelect!=0, the id of the sub-select in EQP */
12245#endif
12246    int iCursor;      /* The VDBE cursor number used to access this table */
12247    Expr *pOn;        /* The ON clause of a join */
12248    IdList *pUsing;   /* The USING clause of a join */
12249    Bitmask colUsed;  /* Bit N (1<<N) set if column N of pTab is used */
12250    char *zIndexedBy; /* Identifier from "INDEXED BY <zIndex>" clause */
12251    Index *pIndex;    /* Index structure corresponding to zIndex, if any */
12252  } a[1];             /* One entry for each identifier on the list */
12253};
12254
12255/*
12256** Permitted values of the SrcList.a.jointype field
12257*/
12258#define JT_INNER     0x0001    /* Any kind of inner or cross join */
12259#define JT_CROSS     0x0002    /* Explicit use of the CROSS keyword */
12260#define JT_NATURAL   0x0004    /* True for a "natural" join */
12261#define JT_LEFT      0x0008    /* Left outer join */
12262#define JT_RIGHT     0x0010    /* Right outer join */
12263#define JT_OUTER     0x0020    /* The "OUTER" keyword is present */
12264#define JT_ERROR     0x0040    /* unknown or unsupported join type */
12265
12266
12267/*
12268** Flags appropriate for the wctrlFlags parameter of sqlite3WhereBegin()
12269** and the WhereInfo.wctrlFlags member.
12270*/
12271#define WHERE_ORDERBY_NORMAL   0x0000 /* No-op */
12272#define WHERE_ORDERBY_MIN      0x0001 /* ORDER BY processing for min() func */
12273#define WHERE_ORDERBY_MAX      0x0002 /* ORDER BY processing for max() func */
12274#define WHERE_ONEPASS_DESIRED  0x0004 /* Want to do one-pass UPDATE/DELETE */
12275#define WHERE_DUPLICATES_OK    0x0008 /* Ok to return a row more than once */
12276#define WHERE_OMIT_OPEN_CLOSE  0x0010 /* Table cursors are already open */
12277#define WHERE_FORCE_TABLE      0x0020 /* Do not use an index-only search */
12278#define WHERE_ONETABLE_ONLY    0x0040 /* Only code the 1st table in pTabList */
12279#define WHERE_NO_AUTOINDEX     0x0080 /* Disallow automatic indexes */
12280#define WHERE_GROUPBY          0x0100 /* pOrderBy is really a GROUP BY */
12281#define WHERE_DISTINCTBY       0x0200 /* pOrderby is really a DISTINCT clause */
12282#define WHERE_WANT_DISTINCT    0x0400 /* All output needs to be distinct */
12283#define WHERE_SORTBYGROUP      0x0800 /* Support sqlite3WhereIsSorted() */
12284#define WHERE_REOPEN_IDX       0x1000 /* Try to use OP_ReopenIdx */
12285
12286/* Allowed return values from sqlite3WhereIsDistinct()
12287*/
12288#define WHERE_DISTINCT_NOOP      0  /* DISTINCT keyword not used */
12289#define WHERE_DISTINCT_UNIQUE    1  /* No duplicates */
12290#define WHERE_DISTINCT_ORDERED   2  /* All duplicates are adjacent */
12291#define WHERE_DISTINCT_UNORDERED 3  /* Duplicates are scattered */
12292
12293/*
12294** A NameContext defines a context in which to resolve table and column
12295** names.  The context consists of a list of tables (the pSrcList) field and
12296** a list of named expression (pEList).  The named expression list may
12297** be NULL.  The pSrc corresponds to the FROM clause of a SELECT or
12298** to the table being operated on by INSERT, UPDATE, or DELETE.  The
12299** pEList corresponds to the result set of a SELECT and is NULL for
12300** other statements.
12301**
12302** NameContexts can be nested.  When resolving names, the inner-most
12303** context is searched first.  If no match is found, the next outer
12304** context is checked.  If there is still no match, the next context
12305** is checked.  This process continues until either a match is found
12306** or all contexts are check.  When a match is found, the nRef member of
12307** the context containing the match is incremented.
12308**
12309** Each subquery gets a new NameContext.  The pNext field points to the
12310** NameContext in the parent query.  Thus the process of scanning the
12311** NameContext list corresponds to searching through successively outer
12312** subqueries looking for a match.
12313*/
12314struct NameContext {
12315  Parse *pParse;       /* The parser */
12316  SrcList *pSrcList;   /* One or more tables used to resolve names */
12317  ExprList *pEList;    /* Optional list of result-set columns */
12318  AggInfo *pAggInfo;   /* Information about aggregates at this level */
12319  NameContext *pNext;  /* Next outer name context.  NULL for outermost */
12320  int nRef;            /* Number of names resolved by this context */
12321  int nErr;            /* Number of errors encountered while resolving names */
12322  u16 ncFlags;         /* Zero or more NC_* flags defined below */
12323};
12324
12325/*
12326** Allowed values for the NameContext, ncFlags field.
12327**
12328** Note:  NC_MinMaxAgg must have the same value as SF_MinMaxAgg and
12329** SQLITE_FUNC_MINMAX.
12330**
12331*/
12332#define NC_AllowAgg  0x0001  /* Aggregate functions are allowed here */
12333#define NC_HasAgg    0x0002  /* One or more aggregate functions seen */
12334#define NC_IsCheck   0x0004  /* True if resolving names in a CHECK constraint */
12335#define NC_InAggFunc 0x0008  /* True if analyzing arguments to an agg func */
12336#define NC_PartIdx   0x0010  /* True if resolving a partial index WHERE */
12337#define NC_MinMaxAgg 0x1000  /* min/max aggregates seen.  See note above */
12338
12339/*
12340** An instance of the following structure contains all information
12341** needed to generate code for a single SELECT statement.
12342**
12343** nLimit is set to -1 if there is no LIMIT clause.  nOffset is set to 0.
12344** If there is a LIMIT clause, the parser sets nLimit to the value of the
12345** limit and nOffset to the value of the offset (or 0 if there is not
12346** offset).  But later on, nLimit and nOffset become the memory locations
12347** in the VDBE that record the limit and offset counters.
12348**
12349** addrOpenEphm[] entries contain the address of OP_OpenEphemeral opcodes.
12350** These addresses must be stored so that we can go back and fill in
12351** the P4_KEYINFO and P2 parameters later.  Neither the KeyInfo nor
12352** the number of columns in P2 can be computed at the same time
12353** as the OP_OpenEphm instruction is coded because not
12354** enough information about the compound query is known at that point.
12355** The KeyInfo for addrOpenTran[0] and [1] contains collating sequences
12356** for the result set.  The KeyInfo for addrOpenEphm[2] contains collating
12357** sequences for the ORDER BY clause.
12358*/
12359struct Select {
12360  ExprList *pEList;      /* The fields of the result */
12361  u8 op;                 /* One of: TK_UNION TK_ALL TK_INTERSECT TK_EXCEPT */
12362  u16 selFlags;          /* Various SF_* values */
12363  int iLimit, iOffset;   /* Memory registers holding LIMIT & OFFSET counters */
12364#if SELECTTRACE_ENABLED
12365  char zSelName[12];     /* Symbolic name of this SELECT use for debugging */
12366#endif
12367  int addrOpenEphm[2];   /* OP_OpenEphem opcodes related to this select */
12368  u64 nSelectRow;        /* Estimated number of result rows */
12369  SrcList *pSrc;         /* The FROM clause */
12370  Expr *pWhere;          /* The WHERE clause */
12371  ExprList *pGroupBy;    /* The GROUP BY clause */
12372  Expr *pHaving;         /* The HAVING clause */
12373  ExprList *pOrderBy;    /* The ORDER BY clause */
12374  Select *pPrior;        /* Prior select in a compound select statement */
12375  Select *pNext;         /* Next select to the left in a compound */
12376  Expr *pLimit;          /* LIMIT expression. NULL means not used. */
12377  Expr *pOffset;         /* OFFSET expression. NULL means not used. */
12378  With *pWith;           /* WITH clause attached to this select. Or NULL. */
12379};
12380
12381/*
12382** Allowed values for Select.selFlags.  The "SF" prefix stands for
12383** "Select Flag".
12384*/
12385#define SF_Distinct        0x0001  /* Output should be DISTINCT */
12386#define SF_All             0x0002  /* Includes the ALL keyword */
12387#define SF_Resolved        0x0004  /* Identifiers have been resolved */
12388#define SF_Aggregate       0x0008  /* Contains aggregate functions */
12389#define SF_UsesEphemeral   0x0010  /* Uses the OpenEphemeral opcode */
12390#define SF_Expanded        0x0020  /* sqlite3SelectExpand() called on this */
12391#define SF_HasTypeInfo     0x0040  /* FROM subqueries have Table metadata */
12392#define SF_Compound        0x0080  /* Part of a compound query */
12393#define SF_Values          0x0100  /* Synthesized from VALUES clause */
12394#define SF_MultiValue      0x0200  /* Single VALUES term with multiple rows */
12395#define SF_NestedFrom      0x0400  /* Part of a parenthesized FROM clause */
12396#define SF_MaybeConvert    0x0800  /* Need convertCompoundSelectToSubquery() */
12397#define SF_MinMaxAgg       0x1000  /* Aggregate containing min() or max() */
12398#define SF_Recursive       0x2000  /* The recursive part of a recursive CTE */
12399#define SF_Converted       0x4000  /* By convertCompoundSelectToSubquery() */
12400
12401
12402/*
12403** The results of a SELECT can be distributed in several ways, as defined
12404** by one of the following macros.  The "SRT" prefix means "SELECT Result
12405** Type".
12406**
12407**     SRT_Union       Store results as a key in a temporary index
12408**                     identified by pDest->iSDParm.
12409**
12410**     SRT_Except      Remove results from the temporary index pDest->iSDParm.
12411**
12412**     SRT_Exists      Store a 1 in memory cell pDest->iSDParm if the result
12413**                     set is not empty.
12414**
12415**     SRT_Discard     Throw the results away.  This is used by SELECT
12416**                     statements within triggers whose only purpose is
12417**                     the side-effects of functions.
12418**
12419** All of the above are free to ignore their ORDER BY clause. Those that
12420** follow must honor the ORDER BY clause.
12421**
12422**     SRT_Output      Generate a row of output (using the OP_ResultRow
12423**                     opcode) for each row in the result set.
12424**
12425**     SRT_Mem         Only valid if the result is a single column.
12426**                     Store the first column of the first result row
12427**                     in register pDest->iSDParm then abandon the rest
12428**                     of the query.  This destination implies "LIMIT 1".
12429**
12430**     SRT_Set         The result must be a single column.  Store each
12431**                     row of result as the key in table pDest->iSDParm.
12432**                     Apply the affinity pDest->affSdst before storing
12433**                     results.  Used to implement "IN (SELECT ...)".
12434**
12435**     SRT_EphemTab    Create an temporary table pDest->iSDParm and store
12436**                     the result there. The cursor is left open after
12437**                     returning.  This is like SRT_Table except that
12438**                     this destination uses OP_OpenEphemeral to create
12439**                     the table first.
12440**
12441**     SRT_Coroutine   Generate a co-routine that returns a new row of
12442**                     results each time it is invoked.  The entry point
12443**                     of the co-routine is stored in register pDest->iSDParm
12444**                     and the result row is stored in pDest->nDest registers
12445**                     starting with pDest->iSdst.
12446**
12447**     SRT_Table       Store results in temporary table pDest->iSDParm.
12448**     SRT_Fifo        This is like SRT_EphemTab except that the table
12449**                     is assumed to already be open.  SRT_Fifo has
12450**                     the additional property of being able to ignore
12451**                     the ORDER BY clause.
12452**
12453**     SRT_DistFifo    Store results in a temporary table pDest->iSDParm.
12454**                     But also use temporary table pDest->iSDParm+1 as
12455**                     a record of all prior results and ignore any duplicate
12456**                     rows.  Name means:  "Distinct Fifo".
12457**
12458**     SRT_Queue       Store results in priority queue pDest->iSDParm (really
12459**                     an index).  Append a sequence number so that all entries
12460**                     are distinct.
12461**
12462**     SRT_DistQueue   Store results in priority queue pDest->iSDParm only if
12463**                     the same record has never been stored before.  The
12464**                     index at pDest->iSDParm+1 hold all prior stores.
12465*/
12466#define SRT_Union        1  /* Store result as keys in an index */
12467#define SRT_Except       2  /* Remove result from a UNION index */
12468#define SRT_Exists       3  /* Store 1 if the result is not empty */
12469#define SRT_Discard      4  /* Do not save the results anywhere */
12470#define SRT_Fifo         5  /* Store result as data with an automatic rowid */
12471#define SRT_DistFifo     6  /* Like SRT_Fifo, but unique results only */
12472#define SRT_Queue        7  /* Store result in an queue */
12473#define SRT_DistQueue    8  /* Like SRT_Queue, but unique results only */
12474
12475/* The ORDER BY clause is ignored for all of the above */
12476#define IgnorableOrderby(X) ((X->eDest)<=SRT_DistQueue)
12477
12478#define SRT_Output       9  /* Output each row of result */
12479#define SRT_Mem         10  /* Store result in a memory cell */
12480#define SRT_Set         11  /* Store results as keys in an index */
12481#define SRT_EphemTab    12  /* Create transient tab and store like SRT_Table */
12482#define SRT_Coroutine   13  /* Generate a single row of result */
12483#define SRT_Table       14  /* Store result as data with an automatic rowid */
12484
12485/*
12486** An instance of this object describes where to put of the results of
12487** a SELECT statement.
12488*/
12489struct SelectDest {
12490  u8 eDest;            /* How to dispose of the results.  On of SRT_* above. */
12491  char affSdst;        /* Affinity used when eDest==SRT_Set */
12492  int iSDParm;         /* A parameter used by the eDest disposal method */
12493  int iSdst;           /* Base register where results are written */
12494  int nSdst;           /* Number of registers allocated */
12495  ExprList *pOrderBy;  /* Key columns for SRT_Queue and SRT_DistQueue */
12496};
12497
12498/*
12499** During code generation of statements that do inserts into AUTOINCREMENT
12500** tables, the following information is attached to the Table.u.autoInc.p
12501** pointer of each autoincrement table to record some side information that
12502** the code generator needs.  We have to keep per-table autoincrement
12503** information in case inserts are down within triggers.  Triggers do not
12504** normally coordinate their activities, but we do need to coordinate the
12505** loading and saving of autoincrement information.
12506*/
12507struct AutoincInfo {
12508  AutoincInfo *pNext;   /* Next info block in a list of them all */
12509  Table *pTab;          /* Table this info block refers to */
12510  int iDb;              /* Index in sqlite3.aDb[] of database holding pTab */
12511  int regCtr;           /* Memory register holding the rowid counter */
12512};
12513
12514/*
12515** Size of the column cache
12516*/
12517#ifndef SQLITE_N_COLCACHE
12518# define SQLITE_N_COLCACHE 10
12519#endif
12520
12521/*
12522** At least one instance of the following structure is created for each
12523** trigger that may be fired while parsing an INSERT, UPDATE or DELETE
12524** statement. All such objects are stored in the linked list headed at
12525** Parse.pTriggerPrg and deleted once statement compilation has been
12526** completed.
12527**
12528** A Vdbe sub-program that implements the body and WHEN clause of trigger
12529** TriggerPrg.pTrigger, assuming a default ON CONFLICT clause of
12530** TriggerPrg.orconf, is stored in the TriggerPrg.pProgram variable.
12531** The Parse.pTriggerPrg list never contains two entries with the same
12532** values for both pTrigger and orconf.
12533**
12534** The TriggerPrg.aColmask[0] variable is set to a mask of old.* columns
12535** accessed (or set to 0 for triggers fired as a result of INSERT
12536** statements). Similarly, the TriggerPrg.aColmask[1] variable is set to
12537** a mask of new.* columns used by the program.
12538*/
12539struct TriggerPrg {
12540  Trigger *pTrigger;      /* Trigger this program was coded from */
12541  TriggerPrg *pNext;      /* Next entry in Parse.pTriggerPrg list */
12542  SubProgram *pProgram;   /* Program implementing pTrigger/orconf */
12543  int orconf;             /* Default ON CONFLICT policy */
12544  u32 aColmask[2];        /* Masks of old.*, new.* columns accessed */
12545};
12546
12547/*
12548** The yDbMask datatype for the bitmask of all attached databases.
12549*/
12550#if SQLITE_MAX_ATTACHED>30
12551  typedef unsigned char yDbMask[(SQLITE_MAX_ATTACHED+9)/8];
12552# define DbMaskTest(M,I)    (((M)[(I)/8]&(1<<((I)&7)))!=0)
12553# define DbMaskZero(M)      memset((M),0,sizeof(M))
12554# define DbMaskSet(M,I)     (M)[(I)/8]|=(1<<((I)&7))
12555# define DbMaskAllZero(M)   sqlite3DbMaskAllZero(M)
12556# define DbMaskNonZero(M)   (sqlite3DbMaskAllZero(M)==0)
12557#else
12558  typedef unsigned int yDbMask;
12559# define DbMaskTest(M,I)    (((M)&(((yDbMask)1)<<(I)))!=0)
12560# define DbMaskZero(M)      (M)=0
12561# define DbMaskSet(M,I)     (M)|=(((yDbMask)1)<<(I))
12562# define DbMaskAllZero(M)   (M)==0
12563# define DbMaskNonZero(M)   (M)!=0
12564#endif
12565
12566/*
12567** An SQL parser context.  A copy of this structure is passed through
12568** the parser and down into all the parser action routine in order to
12569** carry around information that is global to the entire parse.
12570**
12571** The structure is divided into two parts.  When the parser and code
12572** generate call themselves recursively, the first part of the structure
12573** is constant but the second part is reset at the beginning and end of
12574** each recursion.
12575**
12576** The nTableLock and aTableLock variables are only used if the shared-cache
12577** feature is enabled (if sqlite3Tsd()->useSharedData is true). They are
12578** used to store the set of table-locks required by the statement being
12579** compiled. Function sqlite3TableLock() is used to add entries to the
12580** list.
12581*/
12582struct Parse {
12583  sqlite3 *db;         /* The main database structure */
12584  char *zErrMsg;       /* An error message */
12585  Vdbe *pVdbe;         /* An engine for executing database bytecode */
12586  int rc;              /* Return code from execution */
12587  u8 colNamesSet;      /* TRUE after OP_ColumnName has been issued to pVdbe */
12588  u8 checkSchema;      /* Causes schema cookie check after an error */
12589  u8 nested;           /* Number of nested calls to the parser/code generator */
12590  u8 nTempReg;         /* Number of temporary registers in aTempReg[] */
12591  u8 isMultiWrite;     /* True if statement may modify/insert multiple rows */
12592  u8 mayAbort;         /* True if statement may throw an ABORT exception */
12593  u8 hasCompound;      /* Need to invoke convertCompoundSelectToSubquery() */
12594  u8 okConstFactor;    /* OK to factor out constants */
12595  int aTempReg[8];     /* Holding area for temporary registers */
12596  int nRangeReg;       /* Size of the temporary register block */
12597  int iRangeReg;       /* First register in temporary register block */
12598  int nErr;            /* Number of errors seen */
12599  int nTab;            /* Number of previously allocated VDBE cursors */
12600  int nMem;            /* Number of memory cells used so far */
12601  int nSet;            /* Number of sets used so far */
12602  int nOnce;           /* Number of OP_Once instructions so far */
12603  int nOpAlloc;        /* Number of slots allocated for Vdbe.aOp[] */
12604  int iFixedOp;        /* Never back out opcodes iFixedOp-1 or earlier */
12605  int ckBase;          /* Base register of data during check constraints */
12606  int iPartIdxTab;     /* Table corresponding to a partial index */
12607  int iCacheLevel;     /* ColCache valid when aColCache[].iLevel<=iCacheLevel */
12608  int iCacheCnt;       /* Counter used to generate aColCache[].lru values */
12609  int nLabel;          /* Number of labels used */
12610  int *aLabel;         /* Space to hold the labels */
12611  struct yColCache {
12612    int iTable;           /* Table cursor number */
12613    i16 iColumn;          /* Table column number */
12614    u8 tempReg;           /* iReg is a temp register that needs to be freed */
12615    int iLevel;           /* Nesting level */
12616    int iReg;             /* Reg with value of this column. 0 means none. */
12617    int lru;              /* Least recently used entry has the smallest value */
12618  } aColCache[SQLITE_N_COLCACHE];  /* One for each column cache entry */
12619  ExprList *pConstExpr;/* Constant expressions */
12620  Token constraintName;/* Name of the constraint currently being parsed */
12621  yDbMask writeMask;   /* Start a write transaction on these databases */
12622  yDbMask cookieMask;  /* Bitmask of schema verified databases */
12623  int cookieValue[SQLITE_MAX_ATTACHED+2];  /* Values of cookies to verify */
12624  int regRowid;        /* Register holding rowid of CREATE TABLE entry */
12625  int regRoot;         /* Register holding root page number for new objects */
12626  int nMaxArg;         /* Max args passed to user function by sub-program */
12627#if SELECTTRACE_ENABLED
12628  int nSelect;         /* Number of SELECT statements seen */
12629  int nSelectIndent;   /* How far to indent SELECTTRACE() output */
12630#endif
12631#ifndef SQLITE_OMIT_SHARED_CACHE
12632  int nTableLock;        /* Number of locks in aTableLock */
12633  TableLock *aTableLock; /* Required table locks for shared-cache mode */
12634#endif
12635  AutoincInfo *pAinc;  /* Information about AUTOINCREMENT counters */
12636
12637  /* Information used while coding trigger programs. */
12638  Parse *pToplevel;    /* Parse structure for main program (or NULL) */
12639  Table *pTriggerTab;  /* Table triggers are being coded for */
12640  int addrCrTab;       /* Address of OP_CreateTable opcode on CREATE TABLE */
12641  u32 nQueryLoop;      /* Est number of iterations of a query (10*log2(N)) */
12642  u32 oldmask;         /* Mask of old.* columns referenced */
12643  u32 newmask;         /* Mask of new.* columns referenced */
12644  u8 eTriggerOp;       /* TK_UPDATE, TK_INSERT or TK_DELETE */
12645  u8 eOrconf;          /* Default ON CONFLICT policy for trigger steps */
12646  u8 disableTriggers;  /* True to disable triggers */
12647
12648  /************************************************************************
12649  ** Above is constant between recursions.  Below is reset before and after
12650  ** each recursion.  The boundary between these two regions is determined
12651  ** using offsetof(Parse,nVar) so the nVar field must be the first field
12652  ** in the recursive region.
12653  ************************************************************************/
12654
12655  int nVar;                 /* Number of '?' variables seen in the SQL so far */
12656  int nzVar;                /* Number of available slots in azVar[] */
12657  u8 iPkSortOrder;          /* ASC or DESC for INTEGER PRIMARY KEY */
12658  u8 bFreeWith;             /* True if pWith should be freed with parser */
12659  u8 explain;               /* True if the EXPLAIN flag is found on the query */
12660#ifndef SQLITE_OMIT_VIRTUALTABLE
12661  u8 declareVtab;           /* True if inside sqlite3_declare_vtab() */
12662  int nVtabLock;            /* Number of virtual tables to lock */
12663#endif
12664  int nAlias;               /* Number of aliased result set columns */
12665  int nHeight;              /* Expression tree height of current sub-select */
12666#ifndef SQLITE_OMIT_EXPLAIN
12667  int iSelectId;            /* ID of current select for EXPLAIN output */
12668  int iNextSelectId;        /* Next available select ID for EXPLAIN output */
12669#endif
12670  char **azVar;             /* Pointers to names of parameters */
12671  Vdbe *pReprepare;         /* VM being reprepared (sqlite3Reprepare()) */
12672  const char *zTail;        /* All SQL text past the last semicolon parsed */
12673  Table *pNewTable;         /* A table being constructed by CREATE TABLE */
12674  Trigger *pNewTrigger;     /* Trigger under construct by a CREATE TRIGGER */
12675  const char *zAuthContext; /* The 6th parameter to db->xAuth callbacks */
12676  Token sNameToken;         /* Token with unqualified schema object name */
12677  Token sLastToken;         /* The last token parsed */
12678#ifndef SQLITE_OMIT_VIRTUALTABLE
12679  Token sArg;               /* Complete text of a module argument */
12680  Table **apVtabLock;       /* Pointer to virtual tables needing locking */
12681#endif
12682  Table *pZombieTab;        /* List of Table objects to delete after code gen */
12683  TriggerPrg *pTriggerPrg;  /* Linked list of coded triggers */
12684  With *pWith;              /* Current WITH clause, or NULL */
12685};
12686
12687/*
12688** Return true if currently inside an sqlite3_declare_vtab() call.
12689*/
12690#ifdef SQLITE_OMIT_VIRTUALTABLE
12691  #define IN_DECLARE_VTAB 0
12692#else
12693  #define IN_DECLARE_VTAB (pParse->declareVtab)
12694#endif
12695
12696/*
12697** An instance of the following structure can be declared on a stack and used
12698** to save the Parse.zAuthContext value so that it can be restored later.
12699*/
12700struct AuthContext {
12701  const char *zAuthContext;   /* Put saved Parse.zAuthContext here */
12702  Parse *pParse;              /* The Parse structure */
12703};
12704
12705/*
12706** Bitfield flags for P5 value in various opcodes.
12707*/
12708#define OPFLAG_NCHANGE       0x01    /* Set to update db->nChange */
12709#define OPFLAG_EPHEM         0x01    /* OP_Column: Ephemeral output is ok */
12710#define OPFLAG_LASTROWID     0x02    /* Set to update db->lastRowid */
12711#define OPFLAG_ISUPDATE      0x04    /* This OP_Insert is an sql UPDATE */
12712#define OPFLAG_APPEND        0x08    /* This is likely to be an append */
12713#define OPFLAG_USESEEKRESULT 0x10    /* Try to avoid a seek in BtreeInsert() */
12714#define OPFLAG_LENGTHARG     0x40    /* OP_Column only used for length() */
12715#define OPFLAG_TYPEOFARG     0x80    /* OP_Column only used for typeof() */
12716#define OPFLAG_BULKCSR       0x01    /* OP_Open** used to open bulk cursor */
12717#define OPFLAG_SEEKEQ        0x02    /* OP_Open** cursor uses EQ seek only */
12718#define OPFLAG_P2ISREG       0x04    /* P2 to OP_Open** is a register number */
12719#define OPFLAG_PERMUTE       0x01    /* OP_Compare: use the permutation */
12720
12721/*
12722 * Each trigger present in the database schema is stored as an instance of
12723 * struct Trigger.
12724 *
12725 * Pointers to instances of struct Trigger are stored in two ways.
12726 * 1. In the "trigHash" hash table (part of the sqlite3* that represents the
12727 *    database). This allows Trigger structures to be retrieved by name.
12728 * 2. All triggers associated with a single table form a linked list, using the
12729 *    pNext member of struct Trigger. A pointer to the first element of the
12730 *    linked list is stored as the "pTrigger" member of the associated
12731 *    struct Table.
12732 *
12733 * The "step_list" member points to the first element of a linked list
12734 * containing the SQL statements specified as the trigger program.
12735 */
12736struct Trigger {
12737  char *zName;            /* The name of the trigger                        */
12738  char *table;            /* The table or view to which the trigger applies */
12739  u8 op;                  /* One of TK_DELETE, TK_UPDATE, TK_INSERT         */
12740  u8 tr_tm;               /* One of TRIGGER_BEFORE, TRIGGER_AFTER */
12741  Expr *pWhen;            /* The WHEN clause of the expression (may be NULL) */
12742  IdList *pColumns;       /* If this is an UPDATE OF <column-list> trigger,
12743                             the <column-list> is stored here */
12744  Schema *pSchema;        /* Schema containing the trigger */
12745  Schema *pTabSchema;     /* Schema containing the table */
12746  TriggerStep *step_list; /* Link list of trigger program steps             */
12747  Trigger *pNext;         /* Next trigger associated with the table */
12748};
12749
12750/*
12751** A trigger is either a BEFORE or an AFTER trigger.  The following constants
12752** determine which.
12753**
12754** If there are multiple triggers, you might of some BEFORE and some AFTER.
12755** In that cases, the constants below can be ORed together.
12756*/
12757#define TRIGGER_BEFORE  1
12758#define TRIGGER_AFTER   2
12759
12760/*
12761 * An instance of struct TriggerStep is used to store a single SQL statement
12762 * that is a part of a trigger-program.
12763 *
12764 * Instances of struct TriggerStep are stored in a singly linked list (linked
12765 * using the "pNext" member) referenced by the "step_list" member of the
12766 * associated struct Trigger instance. The first element of the linked list is
12767 * the first step of the trigger-program.
12768 *
12769 * The "op" member indicates whether this is a "DELETE", "INSERT", "UPDATE" or
12770 * "SELECT" statement. The meanings of the other members is determined by the
12771 * value of "op" as follows:
12772 *
12773 * (op == TK_INSERT)
12774 * orconf    -> stores the ON CONFLICT algorithm
12775 * pSelect   -> If this is an INSERT INTO ... SELECT ... statement, then
12776 *              this stores a pointer to the SELECT statement. Otherwise NULL.
12777 * zTarget   -> Dequoted name of the table to insert into.
12778 * pExprList -> If this is an INSERT INTO ... VALUES ... statement, then
12779 *              this stores values to be inserted. Otherwise NULL.
12780 * pIdList   -> If this is an INSERT INTO ... (<column-names>) VALUES ...
12781 *              statement, then this stores the column-names to be
12782 *              inserted into.
12783 *
12784 * (op == TK_DELETE)
12785 * zTarget   -> Dequoted name of the table to delete from.
12786 * pWhere    -> The WHERE clause of the DELETE statement if one is specified.
12787 *              Otherwise NULL.
12788 *
12789 * (op == TK_UPDATE)
12790 * zTarget   -> Dequoted name of the table to update.
12791 * pWhere    -> The WHERE clause of the UPDATE statement if one is specified.
12792 *              Otherwise NULL.
12793 * pExprList -> A list of the columns to update and the expressions to update
12794 *              them to. See sqlite3Update() documentation of "pChanges"
12795 *              argument.
12796 *
12797 */
12798struct TriggerStep {
12799  u8 op;               /* One of TK_DELETE, TK_UPDATE, TK_INSERT, TK_SELECT */
12800  u8 orconf;           /* OE_Rollback etc. */
12801  Trigger *pTrig;      /* The trigger that this step is a part of */
12802  Select *pSelect;     /* SELECT statement or RHS of INSERT INTO SELECT ... */
12803  char *zTarget;       /* Target table for DELETE, UPDATE, INSERT */
12804  Expr *pWhere;        /* The WHERE clause for DELETE or UPDATE steps */
12805  ExprList *pExprList; /* SET clause for UPDATE. */
12806  IdList *pIdList;     /* Column names for INSERT */
12807  TriggerStep *pNext;  /* Next in the link-list */
12808  TriggerStep *pLast;  /* Last element in link-list. Valid for 1st elem only */
12809};
12810
12811/*
12812** The following structure contains information used by the sqliteFix...
12813** routines as they walk the parse tree to make database references
12814** explicit.
12815*/
12816typedef struct DbFixer DbFixer;
12817struct DbFixer {
12818  Parse *pParse;      /* The parsing context.  Error messages written here */
12819  Schema *pSchema;    /* Fix items to this schema */
12820  int bVarOnly;       /* Check for variable references only */
12821  const char *zDb;    /* Make sure all objects are contained in this database */
12822  const char *zType;  /* Type of the container - used for error messages */
12823  const Token *pName; /* Name of the container - used for error messages */
12824};
12825
12826/*
12827** An objected used to accumulate the text of a string where we
12828** do not necessarily know how big the string will be in the end.
12829*/
12830struct StrAccum {
12831  sqlite3 *db;         /* Optional database for lookaside.  Can be NULL */
12832  char *zBase;         /* A base allocation.  Not from malloc. */
12833  char *zText;         /* The string collected so far */
12834  int  nChar;          /* Length of the string so far */
12835  int  nAlloc;         /* Amount of space allocated in zText */
12836  int  mxAlloc;        /* Maximum allowed allocation.  0 for no malloc usage */
12837  u8   accError;       /* STRACCUM_NOMEM or STRACCUM_TOOBIG */
12838};
12839#define STRACCUM_NOMEM   1
12840#define STRACCUM_TOOBIG  2
12841
12842/*
12843** A pointer to this structure is used to communicate information
12844** from sqlite3Init and OP_ParseSchema into the sqlite3InitCallback.
12845*/
12846typedef struct {
12847  sqlite3 *db;        /* The database being initialized */
12848  char **pzErrMsg;    /* Error message stored here */
12849  int iDb;            /* 0 for main database.  1 for TEMP, 2.. for ATTACHed */
12850  int rc;             /* Result code stored here */
12851} InitData;
12852
12853/*
12854** Structure containing global configuration data for the SQLite library.
12855**
12856** This structure also contains some state information.
12857*/
12858struct Sqlite3Config {
12859  int bMemstat;                     /* True to enable memory status */
12860  int bCoreMutex;                   /* True to enable core mutexing */
12861  int bFullMutex;                   /* True to enable full mutexing */
12862  int bOpenUri;                     /* True to interpret filenames as URIs */
12863  int bUseCis;                      /* Use covering indices for full-scans */
12864  int mxStrlen;                     /* Maximum string length */
12865  int neverCorrupt;                 /* Database is always well-formed */
12866  int szLookaside;                  /* Default lookaside buffer size */
12867  int nLookaside;                   /* Default lookaside buffer count */
12868  sqlite3_mem_methods m;            /* Low-level memory allocation interface */
12869  sqlite3_mutex_methods mutex;      /* Low-level mutex interface */
12870  sqlite3_pcache_methods2 pcache2;  /* Low-level page-cache interface */
12871  void *pHeap;                      /* Heap storage space */
12872  int nHeap;                        /* Size of pHeap[] */
12873  int mnReq, mxReq;                 /* Min and max heap requests sizes */
12874  sqlite3_int64 szMmap;             /* mmap() space per open file */
12875  sqlite3_int64 mxMmap;             /* Maximum value for szMmap */
12876  void *pScratch;                   /* Scratch memory */
12877  int szScratch;                    /* Size of each scratch buffer */
12878  int nScratch;                     /* Number of scratch buffers */
12879  void *pPage;                      /* Page cache memory */
12880  int szPage;                       /* Size of each page in pPage[] */
12881  int nPage;                        /* Number of pages in pPage[] */
12882  int mxParserStack;                /* maximum depth of the parser stack */
12883  int sharedCacheEnabled;           /* true if shared-cache mode enabled */
12884  u32 szPma;                        /* Maximum Sorter PMA size */
12885  /* The above might be initialized to non-zero.  The following need to always
12886  ** initially be zero, however. */
12887  int isInit;                       /* True after initialization has finished */
12888  int inProgress;                   /* True while initialization in progress */
12889  int isMutexInit;                  /* True after mutexes are initialized */
12890  int isMallocInit;                 /* True after malloc is initialized */
12891  int isPCacheInit;                 /* True after malloc is initialized */
12892  int nRefInitMutex;                /* Number of users of pInitMutex */
12893  sqlite3_mutex *pInitMutex;        /* Mutex used by sqlite3_initialize() */
12894  void (*xLog)(void*,int,const char*); /* Function for logging */
12895  void *pLogArg;                       /* First argument to xLog() */
12896#ifdef SQLITE_ENABLE_SQLLOG
12897  void(*xSqllog)(void*,sqlite3*,const char*, int);
12898  void *pSqllogArg;
12899#endif
12900#ifdef SQLITE_VDBE_COVERAGE
12901  /* The following callback (if not NULL) is invoked on every VDBE branch
12902  ** operation.  Set the callback using SQLITE_TESTCTRL_VDBE_COVERAGE.
12903  */
12904  void (*xVdbeBranch)(void*,int iSrcLine,u8 eThis,u8 eMx);  /* Callback */
12905  void *pVdbeBranchArg;                                     /* 1st argument */
12906#endif
12907#ifndef SQLITE_OMIT_BUILTIN_TEST
12908  int (*xTestCallback)(int);        /* Invoked by sqlite3FaultSim() */
12909#endif
12910  int bLocaltimeFault;              /* True to fail localtime() calls */
12911};
12912
12913/*
12914** This macro is used inside of assert() statements to indicate that
12915** the assert is only valid on a well-formed database.  Instead of:
12916**
12917**     assert( X );
12918**
12919** One writes:
12920**
12921**     assert( X || CORRUPT_DB );
12922**
12923** CORRUPT_DB is true during normal operation.  CORRUPT_DB does not indicate
12924** that the database is definitely corrupt, only that it might be corrupt.
12925** For most test cases, CORRUPT_DB is set to false using a special
12926** sqlite3_test_control().  This enables assert() statements to prove
12927** things that are always true for well-formed databases.
12928*/
12929#define CORRUPT_DB  (sqlite3Config.neverCorrupt==0)
12930
12931/*
12932** Context pointer passed down through the tree-walk.
12933*/
12934struct Walker {
12935  int (*xExprCallback)(Walker*, Expr*);     /* Callback for expressions */
12936  int (*xSelectCallback)(Walker*,Select*);  /* Callback for SELECTs */
12937  void (*xSelectCallback2)(Walker*,Select*);/* Second callback for SELECTs */
12938  Parse *pParse;                            /* Parser context.  */
12939  int walkerDepth;                          /* Number of subqueries */
12940  u8 eCode;                                 /* A small processing code */
12941  union {                                   /* Extra data for callback */
12942    NameContext *pNC;                          /* Naming context */
12943    int n;                                     /* A counter */
12944    int iCur;                                  /* A cursor number */
12945    SrcList *pSrcList;                         /* FROM clause */
12946    struct SrcCount *pSrcCount;                /* Counting column references */
12947  } u;
12948};
12949
12950/* Forward declarations */
12951SQLITE_PRIVATE int sqlite3WalkExpr(Walker*, Expr*);
12952SQLITE_PRIVATE int sqlite3WalkExprList(Walker*, ExprList*);
12953SQLITE_PRIVATE int sqlite3WalkSelect(Walker*, Select*);
12954SQLITE_PRIVATE int sqlite3WalkSelectExpr(Walker*, Select*);
12955SQLITE_PRIVATE int sqlite3WalkSelectFrom(Walker*, Select*);
12956
12957/*
12958** Return code from the parse-tree walking primitives and their
12959** callbacks.
12960*/
12961#define WRC_Continue    0   /* Continue down into children */
12962#define WRC_Prune       1   /* Omit children but continue walking siblings */
12963#define WRC_Abort       2   /* Abandon the tree walk */
12964
12965/*
12966** An instance of this structure represents a set of one or more CTEs
12967** (common table expressions) created by a single WITH clause.
12968*/
12969struct With {
12970  int nCte;                       /* Number of CTEs in the WITH clause */
12971  With *pOuter;                   /* Containing WITH clause, or NULL */
12972  struct Cte {                    /* For each CTE in the WITH clause.... */
12973    char *zName;                    /* Name of this CTE */
12974    ExprList *pCols;                /* List of explicit column names, or NULL */
12975    Select *pSelect;                /* The definition of this CTE */
12976    const char *zErr;               /* Error message for circular references */
12977  } a[1];
12978};
12979
12980#ifdef SQLITE_DEBUG
12981/*
12982** An instance of the TreeView object is used for printing the content of
12983** data structures on sqlite3DebugPrintf() using a tree-like view.
12984*/
12985struct TreeView {
12986  int iLevel;             /* Which level of the tree we are on */
12987  u8  bLine[100];         /* Draw vertical in column i if bLine[i] is true */
12988};
12989#endif /* SQLITE_DEBUG */
12990
12991/*
12992** Assuming zIn points to the first byte of a UTF-8 character,
12993** advance zIn to point to the first byte of the next UTF-8 character.
12994*/
12995#define SQLITE_SKIP_UTF8(zIn) {                        \
12996  if( (*(zIn++))>=0xc0 ){                              \
12997    while( (*zIn & 0xc0)==0x80 ){ zIn++; }             \
12998  }                                                    \
12999}
13000
13001/*
13002** The SQLITE_*_BKPT macros are substitutes for the error codes with
13003** the same name but without the _BKPT suffix.  These macros invoke
13004** routines that report the line-number on which the error originated
13005** using sqlite3_log().  The routines also provide a convenient place
13006** to set a debugger breakpoint.
13007*/
13008SQLITE_PRIVATE int sqlite3CorruptError(int);
13009SQLITE_PRIVATE int sqlite3MisuseError(int);
13010SQLITE_PRIVATE int sqlite3CantopenError(int);
13011#define SQLITE_CORRUPT_BKPT sqlite3CorruptError(__LINE__)
13012#define SQLITE_MISUSE_BKPT sqlite3MisuseError(__LINE__)
13013#define SQLITE_CANTOPEN_BKPT sqlite3CantopenError(__LINE__)
13014
13015
13016/*
13017** FTS4 is really an extension for FTS3.  It is enabled using the
13018** SQLITE_ENABLE_FTS3 macro.  But to avoid confusion we also call
13019** the SQLITE_ENABLE_FTS4 macro to serve as an alias for SQLITE_ENABLE_FTS3.
13020*/
13021#if defined(SQLITE_ENABLE_FTS4) && !defined(SQLITE_ENABLE_FTS3)
13022# define SQLITE_ENABLE_FTS3 1
13023#endif
13024
13025/*
13026** The ctype.h header is needed for non-ASCII systems.  It is also
13027** needed by FTS3 when FTS3 is included in the amalgamation.
13028*/
13029#if !defined(SQLITE_ASCII) || \
13030    (defined(SQLITE_ENABLE_FTS3) && defined(SQLITE_AMALGAMATION))
13031# include <ctype.h>
13032#endif
13033
13034/*
13035** The following macros mimic the standard library functions toupper(),
13036** isspace(), isalnum(), isdigit() and isxdigit(), respectively. The
13037** sqlite versions only work for ASCII characters, regardless of locale.
13038*/
13039#ifdef SQLITE_ASCII
13040# define sqlite3Toupper(x)  ((x)&~(sqlite3CtypeMap[(unsigned char)(x)]&0x20))
13041# define sqlite3Isspace(x)   (sqlite3CtypeMap[(unsigned char)(x)]&0x01)
13042# define sqlite3Isalnum(x)   (sqlite3CtypeMap[(unsigned char)(x)]&0x06)
13043# define sqlite3Isalpha(x)   (sqlite3CtypeMap[(unsigned char)(x)]&0x02)
13044# define sqlite3Isdigit(x)   (sqlite3CtypeMap[(unsigned char)(x)]&0x04)
13045# define sqlite3Isxdigit(x)  (sqlite3CtypeMap[(unsigned char)(x)]&0x08)
13046# define sqlite3Tolower(x)   (sqlite3UpperToLower[(unsigned char)(x)])
13047#else
13048# define sqlite3Toupper(x)   toupper((unsigned char)(x))
13049# define sqlite3Isspace(x)   isspace((unsigned char)(x))
13050# define sqlite3Isalnum(x)   isalnum((unsigned char)(x))
13051# define sqlite3Isalpha(x)   isalpha((unsigned char)(x))
13052# define sqlite3Isdigit(x)   isdigit((unsigned char)(x))
13053# define sqlite3Isxdigit(x)  isxdigit((unsigned char)(x))
13054# define sqlite3Tolower(x)   tolower((unsigned char)(x))
13055#endif
13056#ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
13057SQLITE_PRIVATE int sqlite3IsIdChar(u8);
13058#endif
13059
13060/*
13061** Internal function prototypes
13062*/
13063#define sqlite3StrICmp sqlite3_stricmp
13064SQLITE_PRIVATE int sqlite3Strlen30(const char*);
13065#define sqlite3StrNICmp sqlite3_strnicmp
13066
13067SQLITE_PRIVATE int sqlite3MallocInit(void);
13068SQLITE_PRIVATE void sqlite3MallocEnd(void);
13069SQLITE_PRIVATE void *sqlite3Malloc(u64);
13070SQLITE_PRIVATE void *sqlite3MallocZero(u64);
13071SQLITE_PRIVATE void *sqlite3DbMallocZero(sqlite3*, u64);
13072SQLITE_PRIVATE void *sqlite3DbMallocRaw(sqlite3*, u64);
13073SQLITE_PRIVATE char *sqlite3DbStrDup(sqlite3*,const char*);
13074SQLITE_PRIVATE char *sqlite3DbStrNDup(sqlite3*,const char*, u64);
13075SQLITE_PRIVATE void *sqlite3Realloc(void*, u64);
13076SQLITE_PRIVATE void *sqlite3DbReallocOrFree(sqlite3 *, void *, u64);
13077SQLITE_PRIVATE void *sqlite3DbRealloc(sqlite3 *, void *, u64);
13078SQLITE_PRIVATE void sqlite3DbFree(sqlite3*, void*);
13079SQLITE_PRIVATE int sqlite3MallocSize(void*);
13080SQLITE_PRIVATE int sqlite3DbMallocSize(sqlite3*, void*);
13081SQLITE_PRIVATE void *sqlite3ScratchMalloc(int);
13082SQLITE_PRIVATE void sqlite3ScratchFree(void*);
13083SQLITE_PRIVATE void *sqlite3PageMalloc(int);
13084SQLITE_PRIVATE void sqlite3PageFree(void*);
13085SQLITE_PRIVATE void sqlite3MemSetDefault(void);
13086#ifndef SQLITE_OMIT_BUILTIN_TEST
13087SQLITE_PRIVATE void sqlite3BenignMallocHooks(void (*)(void), void (*)(void));
13088#endif
13089SQLITE_PRIVATE int sqlite3HeapNearlyFull(void);
13090
13091/*
13092** On systems with ample stack space and that support alloca(), make
13093** use of alloca() to obtain space for large automatic objects.  By default,
13094** obtain space from malloc().
13095**
13096** The alloca() routine never returns NULL.  This will cause code paths
13097** that deal with sqlite3StackAlloc() failures to be unreachable.
13098*/
13099#ifdef SQLITE_USE_ALLOCA
13100# define sqlite3StackAllocRaw(D,N)   alloca(N)
13101# define sqlite3StackAllocZero(D,N)  memset(alloca(N), 0, N)
13102# define sqlite3StackFree(D,P)
13103#else
13104# define sqlite3StackAllocRaw(D,N)   sqlite3DbMallocRaw(D,N)
13105# define sqlite3StackAllocZero(D,N)  sqlite3DbMallocZero(D,N)
13106# define sqlite3StackFree(D,P)       sqlite3DbFree(D,P)
13107#endif
13108
13109#ifdef SQLITE_ENABLE_MEMSYS3
13110SQLITE_PRIVATE const sqlite3_mem_methods *sqlite3MemGetMemsys3(void);
13111#endif
13112#ifdef SQLITE_ENABLE_MEMSYS5
13113SQLITE_PRIVATE const sqlite3_mem_methods *sqlite3MemGetMemsys5(void);
13114#endif
13115
13116
13117#ifndef SQLITE_MUTEX_OMIT
13118SQLITE_PRIVATE   sqlite3_mutex_methods const *sqlite3DefaultMutex(void);
13119SQLITE_PRIVATE   sqlite3_mutex_methods const *sqlite3NoopMutex(void);
13120SQLITE_PRIVATE   sqlite3_mutex *sqlite3MutexAlloc(int);
13121SQLITE_PRIVATE   int sqlite3MutexInit(void);
13122SQLITE_PRIVATE   int sqlite3MutexEnd(void);
13123#endif
13124
13125SQLITE_PRIVATE sqlite3_int64 sqlite3StatusValue(int);
13126SQLITE_PRIVATE void sqlite3StatusUp(int, int);
13127SQLITE_PRIVATE void sqlite3StatusDown(int, int);
13128SQLITE_PRIVATE void sqlite3StatusSet(int, int);
13129
13130/* Access to mutexes used by sqlite3_status() */
13131SQLITE_PRIVATE sqlite3_mutex *sqlite3Pcache1Mutex(void);
13132SQLITE_PRIVATE sqlite3_mutex *sqlite3MallocMutex(void);
13133
13134#ifndef SQLITE_OMIT_FLOATING_POINT
13135SQLITE_PRIVATE   int sqlite3IsNaN(double);
13136#else
13137# define sqlite3IsNaN(X)  0
13138#endif
13139
13140/*
13141** An instance of the following structure holds information about SQL
13142** functions arguments that are the parameters to the printf() function.
13143*/
13144struct PrintfArguments {
13145  int nArg;                /* Total number of arguments */
13146  int nUsed;               /* Number of arguments used so far */
13147  sqlite3_value **apArg;   /* The argument values */
13148};
13149
13150#define SQLITE_PRINTF_INTERNAL 0x01
13151#define SQLITE_PRINTF_SQLFUNC  0x02
13152SQLITE_PRIVATE void sqlite3VXPrintf(StrAccum*, u32, const char*, va_list);
13153SQLITE_PRIVATE void sqlite3XPrintf(StrAccum*, u32, const char*, ...);
13154SQLITE_PRIVATE char *sqlite3MPrintf(sqlite3*,const char*, ...);
13155SQLITE_PRIVATE char *sqlite3VMPrintf(sqlite3*,const char*, va_list);
13156#if defined(SQLITE_DEBUG) || defined(SQLITE_HAVE_OS_TRACE)
13157SQLITE_PRIVATE   void sqlite3DebugPrintf(const char*, ...);
13158#endif
13159#if defined(SQLITE_TEST)
13160SQLITE_PRIVATE   void *sqlite3TestTextToPtr(const char*);
13161#endif
13162
13163#if defined(SQLITE_DEBUG)
13164SQLITE_PRIVATE   void sqlite3TreeViewExpr(TreeView*, const Expr*, u8);
13165SQLITE_PRIVATE   void sqlite3TreeViewExprList(TreeView*, const ExprList*, u8, const char*);
13166SQLITE_PRIVATE   void sqlite3TreeViewSelect(TreeView*, const Select*, u8);
13167#endif
13168
13169
13170SQLITE_PRIVATE void sqlite3SetString(char **, sqlite3*, const char*);
13171SQLITE_PRIVATE void sqlite3ErrorMsg(Parse*, const char*, ...);
13172SQLITE_PRIVATE int sqlite3Dequote(char*);
13173SQLITE_PRIVATE int sqlite3KeywordCode(const unsigned char*, int);
13174SQLITE_PRIVATE int sqlite3RunParser(Parse*, const char*, char **);
13175SQLITE_PRIVATE void sqlite3FinishCoding(Parse*);
13176SQLITE_PRIVATE int sqlite3GetTempReg(Parse*);
13177SQLITE_PRIVATE void sqlite3ReleaseTempReg(Parse*,int);
13178SQLITE_PRIVATE int sqlite3GetTempRange(Parse*,int);
13179SQLITE_PRIVATE void sqlite3ReleaseTempRange(Parse*,int,int);
13180SQLITE_PRIVATE void sqlite3ClearTempRegCache(Parse*);
13181SQLITE_PRIVATE Expr *sqlite3ExprAlloc(sqlite3*,int,const Token*,int);
13182SQLITE_PRIVATE Expr *sqlite3Expr(sqlite3*,int,const char*);
13183SQLITE_PRIVATE void sqlite3ExprAttachSubtrees(sqlite3*,Expr*,Expr*,Expr*);
13184SQLITE_PRIVATE Expr *sqlite3PExpr(Parse*, int, Expr*, Expr*, const Token*);
13185SQLITE_PRIVATE Expr *sqlite3ExprAnd(sqlite3*,Expr*, Expr*);
13186SQLITE_PRIVATE Expr *sqlite3ExprFunction(Parse*,ExprList*, Token*);
13187SQLITE_PRIVATE void sqlite3ExprAssignVarNumber(Parse*, Expr*);
13188SQLITE_PRIVATE void sqlite3ExprDelete(sqlite3*, Expr*);
13189SQLITE_PRIVATE ExprList *sqlite3ExprListAppend(Parse*,ExprList*,Expr*);
13190SQLITE_PRIVATE void sqlite3ExprListSetName(Parse*,ExprList*,Token*,int);
13191SQLITE_PRIVATE void sqlite3ExprListSetSpan(Parse*,ExprList*,ExprSpan*);
13192SQLITE_PRIVATE void sqlite3ExprListDelete(sqlite3*, ExprList*);
13193SQLITE_PRIVATE u32 sqlite3ExprListFlags(const ExprList*);
13194SQLITE_PRIVATE int sqlite3Init(sqlite3*, char**);
13195SQLITE_PRIVATE int sqlite3InitCallback(void*, int, char**, char**);
13196SQLITE_PRIVATE void sqlite3Pragma(Parse*,Token*,Token*,Token*,int);
13197SQLITE_PRIVATE void sqlite3ResetAllSchemasOfConnection(sqlite3*);
13198SQLITE_PRIVATE void sqlite3ResetOneSchema(sqlite3*,int);
13199SQLITE_PRIVATE void sqlite3CollapseDatabaseArray(sqlite3*);
13200SQLITE_PRIVATE void sqlite3BeginParse(Parse*,int);
13201SQLITE_PRIVATE void sqlite3CommitInternalChanges(sqlite3*);
13202SQLITE_PRIVATE Table *sqlite3ResultSetOfSelect(Parse*,Select*);
13203SQLITE_PRIVATE void sqlite3OpenMasterTable(Parse *, int);
13204SQLITE_PRIVATE Index *sqlite3PrimaryKeyIndex(Table*);
13205SQLITE_PRIVATE i16 sqlite3ColumnOfIndex(Index*, i16);
13206SQLITE_PRIVATE void sqlite3StartTable(Parse*,Token*,Token*,int,int,int,int);
13207SQLITE_PRIVATE void sqlite3AddColumn(Parse*,Token*);
13208SQLITE_PRIVATE void sqlite3AddNotNull(Parse*, int);
13209SQLITE_PRIVATE void sqlite3AddPrimaryKey(Parse*, ExprList*, int, int, int);
13210SQLITE_PRIVATE void sqlite3AddCheckConstraint(Parse*, Expr*);
13211SQLITE_PRIVATE void sqlite3AddColumnType(Parse*,Token*);
13212SQLITE_PRIVATE void sqlite3AddDefaultValue(Parse*,ExprSpan*);
13213SQLITE_PRIVATE void sqlite3AddCollateType(Parse*, Token*);
13214SQLITE_PRIVATE void sqlite3EndTable(Parse*,Token*,Token*,u8,Select*);
13215SQLITE_PRIVATE int sqlite3ParseUri(const char*,const char*,unsigned int*,
13216                    sqlite3_vfs**,char**,char **);
13217SQLITE_PRIVATE Btree *sqlite3DbNameToBtree(sqlite3*,const char*);
13218SQLITE_PRIVATE int sqlite3CodeOnce(Parse *);
13219
13220#ifdef SQLITE_OMIT_BUILTIN_TEST
13221# define sqlite3FaultSim(X) SQLITE_OK
13222#else
13223SQLITE_PRIVATE   int sqlite3FaultSim(int);
13224#endif
13225
13226SQLITE_PRIVATE Bitvec *sqlite3BitvecCreate(u32);
13227SQLITE_PRIVATE int sqlite3BitvecTest(Bitvec*, u32);
13228SQLITE_PRIVATE int sqlite3BitvecTestNotNull(Bitvec*, u32);
13229SQLITE_PRIVATE int sqlite3BitvecSet(Bitvec*, u32);
13230SQLITE_PRIVATE void sqlite3BitvecClear(Bitvec*, u32, void*);
13231SQLITE_PRIVATE void sqlite3BitvecDestroy(Bitvec*);
13232SQLITE_PRIVATE u32 sqlite3BitvecSize(Bitvec*);
13233#ifndef SQLITE_OMIT_BUILTIN_TEST
13234SQLITE_PRIVATE int sqlite3BitvecBuiltinTest(int,int*);
13235#endif
13236
13237SQLITE_PRIVATE RowSet *sqlite3RowSetInit(sqlite3*, void*, unsigned int);
13238SQLITE_PRIVATE void sqlite3RowSetClear(RowSet*);
13239SQLITE_PRIVATE void sqlite3RowSetInsert(RowSet*, i64);
13240SQLITE_PRIVATE int sqlite3RowSetTest(RowSet*, int iBatch, i64);
13241SQLITE_PRIVATE int sqlite3RowSetNext(RowSet*, i64*);
13242
13243SQLITE_PRIVATE void sqlite3CreateView(Parse*,Token*,Token*,Token*,Select*,int,int);
13244
13245#if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_VIRTUALTABLE)
13246SQLITE_PRIVATE   int sqlite3ViewGetColumnNames(Parse*,Table*);
13247#else
13248# define sqlite3ViewGetColumnNames(A,B) 0
13249#endif
13250
13251#if SQLITE_MAX_ATTACHED>30
13252SQLITE_PRIVATE   int sqlite3DbMaskAllZero(yDbMask);
13253#endif
13254SQLITE_PRIVATE void sqlite3DropTable(Parse*, SrcList*, int, int);
13255SQLITE_PRIVATE void sqlite3CodeDropTable(Parse*, Table*, int, int);
13256SQLITE_PRIVATE void sqlite3DeleteTable(sqlite3*, Table*);
13257#ifndef SQLITE_OMIT_AUTOINCREMENT
13258SQLITE_PRIVATE   void sqlite3AutoincrementBegin(Parse *pParse);
13259SQLITE_PRIVATE   void sqlite3AutoincrementEnd(Parse *pParse);
13260#else
13261# define sqlite3AutoincrementBegin(X)
13262# define sqlite3AutoincrementEnd(X)
13263#endif
13264SQLITE_PRIVATE void sqlite3Insert(Parse*, SrcList*, Select*, IdList*, int);
13265SQLITE_PRIVATE void *sqlite3ArrayAllocate(sqlite3*,void*,int,int*,int*);
13266SQLITE_PRIVATE IdList *sqlite3IdListAppend(sqlite3*, IdList*, Token*);
13267SQLITE_PRIVATE int sqlite3IdListIndex(IdList*,const char*);
13268SQLITE_PRIVATE SrcList *sqlite3SrcListEnlarge(sqlite3*, SrcList*, int, int);
13269SQLITE_PRIVATE SrcList *sqlite3SrcListAppend(sqlite3*, SrcList*, Token*, Token*);
13270SQLITE_PRIVATE SrcList *sqlite3SrcListAppendFromTerm(Parse*, SrcList*, Token*, Token*,
13271                                      Token*, Select*, Expr*, IdList*);
13272SQLITE_PRIVATE void sqlite3SrcListIndexedBy(Parse *, SrcList *, Token *);
13273SQLITE_PRIVATE int sqlite3IndexedByLookup(Parse *, struct SrcList_item *);
13274SQLITE_PRIVATE void sqlite3SrcListShiftJoinType(SrcList*);
13275SQLITE_PRIVATE void sqlite3SrcListAssignCursors(Parse*, SrcList*);
13276SQLITE_PRIVATE void sqlite3IdListDelete(sqlite3*, IdList*);
13277SQLITE_PRIVATE void sqlite3SrcListDelete(sqlite3*, SrcList*);
13278SQLITE_PRIVATE Index *sqlite3AllocateIndexObject(sqlite3*,i16,int,char**);
13279SQLITE_PRIVATE Index *sqlite3CreateIndex(Parse*,Token*,Token*,SrcList*,ExprList*,int,Token*,
13280                          Expr*, int, int);
13281SQLITE_PRIVATE void sqlite3DropIndex(Parse*, SrcList*, int);
13282SQLITE_PRIVATE int sqlite3Select(Parse*, Select*, SelectDest*);
13283SQLITE_PRIVATE Select *sqlite3SelectNew(Parse*,ExprList*,SrcList*,Expr*,ExprList*,
13284                         Expr*,ExprList*,u16,Expr*,Expr*);
13285SQLITE_PRIVATE void sqlite3SelectDelete(sqlite3*, Select*);
13286SQLITE_PRIVATE Table *sqlite3SrcListLookup(Parse*, SrcList*);
13287SQLITE_PRIVATE int sqlite3IsReadOnly(Parse*, Table*, int);
13288SQLITE_PRIVATE void sqlite3OpenTable(Parse*, int iCur, int iDb, Table*, int);
13289#if defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT) && !defined(SQLITE_OMIT_SUBQUERY)
13290SQLITE_PRIVATE Expr *sqlite3LimitWhere(Parse*,SrcList*,Expr*,ExprList*,Expr*,Expr*,char*);
13291#endif
13292SQLITE_PRIVATE void sqlite3DeleteFrom(Parse*, SrcList*, Expr*);
13293SQLITE_PRIVATE void sqlite3Update(Parse*, SrcList*, ExprList*, Expr*, int);
13294SQLITE_PRIVATE WhereInfo *sqlite3WhereBegin(Parse*,SrcList*,Expr*,ExprList*,ExprList*,u16,int);
13295SQLITE_PRIVATE void sqlite3WhereEnd(WhereInfo*);
13296SQLITE_PRIVATE u64 sqlite3WhereOutputRowCount(WhereInfo*);
13297SQLITE_PRIVATE int sqlite3WhereIsDistinct(WhereInfo*);
13298SQLITE_PRIVATE int sqlite3WhereIsOrdered(WhereInfo*);
13299SQLITE_PRIVATE int sqlite3WhereIsSorted(WhereInfo*);
13300SQLITE_PRIVATE int sqlite3WhereContinueLabel(WhereInfo*);
13301SQLITE_PRIVATE int sqlite3WhereBreakLabel(WhereInfo*);
13302SQLITE_PRIVATE int sqlite3WhereOkOnePass(WhereInfo*, int*);
13303SQLITE_PRIVATE int sqlite3ExprCodeGetColumn(Parse*, Table*, int, int, int, u8);
13304SQLITE_PRIVATE void sqlite3ExprCodeGetColumnOfTable(Vdbe*, Table*, int, int, int);
13305SQLITE_PRIVATE void sqlite3ExprCodeMove(Parse*, int, int, int);
13306SQLITE_PRIVATE void sqlite3ExprCacheStore(Parse*, int, int, int);
13307SQLITE_PRIVATE void sqlite3ExprCachePush(Parse*);
13308SQLITE_PRIVATE void sqlite3ExprCachePop(Parse*);
13309SQLITE_PRIVATE void sqlite3ExprCacheRemove(Parse*, int, int);
13310SQLITE_PRIVATE void sqlite3ExprCacheClear(Parse*);
13311SQLITE_PRIVATE void sqlite3ExprCacheAffinityChange(Parse*, int, int);
13312SQLITE_PRIVATE void sqlite3ExprCode(Parse*, Expr*, int);
13313SQLITE_PRIVATE void sqlite3ExprCodeFactorable(Parse*, Expr*, int);
13314SQLITE_PRIVATE void sqlite3ExprCodeAtInit(Parse*, Expr*, int, u8);
13315SQLITE_PRIVATE int sqlite3ExprCodeTemp(Parse*, Expr*, int*);
13316SQLITE_PRIVATE int sqlite3ExprCodeTarget(Parse*, Expr*, int);
13317SQLITE_PRIVATE void sqlite3ExprCodeAndCache(Parse*, Expr*, int);
13318SQLITE_PRIVATE int sqlite3ExprCodeExprList(Parse*, ExprList*, int, u8);
13319#define SQLITE_ECEL_DUP      0x01  /* Deep, not shallow copies */
13320#define SQLITE_ECEL_FACTOR   0x02  /* Factor out constant terms */
13321SQLITE_PRIVATE void sqlite3ExprIfTrue(Parse*, Expr*, int, int);
13322SQLITE_PRIVATE void sqlite3ExprIfFalse(Parse*, Expr*, int, int);
13323SQLITE_PRIVATE void sqlite3ExprIfFalseDup(Parse*, Expr*, int, int);
13324SQLITE_PRIVATE Table *sqlite3FindTable(sqlite3*,const char*, const char*);
13325SQLITE_PRIVATE Table *sqlite3LocateTable(Parse*,int isView,const char*, const char*);
13326SQLITE_PRIVATE Table *sqlite3LocateTableItem(Parse*,int isView,struct SrcList_item *);
13327SQLITE_PRIVATE Index *sqlite3FindIndex(sqlite3*,const char*, const char*);
13328SQLITE_PRIVATE void sqlite3UnlinkAndDeleteTable(sqlite3*,int,const char*);
13329SQLITE_PRIVATE void sqlite3UnlinkAndDeleteIndex(sqlite3*,int,const char*);
13330SQLITE_PRIVATE void sqlite3Vacuum(Parse*);
13331SQLITE_PRIVATE int sqlite3RunVacuum(char**, sqlite3*);
13332SQLITE_PRIVATE char *sqlite3NameFromToken(sqlite3*, Token*);
13333SQLITE_PRIVATE int sqlite3ExprCompare(Expr*, Expr*, int);
13334SQLITE_PRIVATE int sqlite3ExprListCompare(ExprList*, ExprList*, int);
13335SQLITE_PRIVATE int sqlite3ExprImpliesExpr(Expr*, Expr*, int);
13336SQLITE_PRIVATE void sqlite3ExprAnalyzeAggregates(NameContext*, Expr*);
13337SQLITE_PRIVATE void sqlite3ExprAnalyzeAggList(NameContext*,ExprList*);
13338SQLITE_PRIVATE int sqlite3FunctionUsesThisSrc(Expr*, SrcList*);
13339SQLITE_PRIVATE Vdbe *sqlite3GetVdbe(Parse*);
13340#ifndef SQLITE_OMIT_BUILTIN_TEST
13341SQLITE_PRIVATE void sqlite3PrngSaveState(void);
13342SQLITE_PRIVATE void sqlite3PrngRestoreState(void);
13343#endif
13344SQLITE_PRIVATE void sqlite3RollbackAll(sqlite3*,int);
13345SQLITE_PRIVATE void sqlite3CodeVerifySchema(Parse*, int);
13346SQLITE_PRIVATE void sqlite3CodeVerifyNamedSchema(Parse*, const char *zDb);
13347SQLITE_PRIVATE void sqlite3BeginTransaction(Parse*, int);
13348SQLITE_PRIVATE void sqlite3CommitTransaction(Parse*);
13349SQLITE_PRIVATE void sqlite3RollbackTransaction(Parse*);
13350SQLITE_PRIVATE void sqlite3Savepoint(Parse*, int, Token*);
13351SQLITE_PRIVATE void sqlite3CloseSavepoints(sqlite3 *);
13352SQLITE_PRIVATE void sqlite3LeaveMutexAndCloseZombie(sqlite3*);
13353SQLITE_PRIVATE int sqlite3ExprIsConstant(Expr*);
13354SQLITE_PRIVATE int sqlite3ExprIsConstantNotJoin(Expr*);
13355SQLITE_PRIVATE int sqlite3ExprIsConstantOrFunction(Expr*, u8);
13356SQLITE_PRIVATE int sqlite3ExprIsTableConstant(Expr*,int);
13357SQLITE_PRIVATE int sqlite3ExprIsInteger(Expr*, int*);
13358SQLITE_PRIVATE int sqlite3ExprCanBeNull(const Expr*);
13359SQLITE_PRIVATE int sqlite3ExprNeedsNoAffinityChange(const Expr*, char);
13360SQLITE_PRIVATE int sqlite3IsRowid(const char*);
13361SQLITE_PRIVATE void sqlite3GenerateRowDelete(Parse*,Table*,Trigger*,int,int,int,i16,u8,u8,u8);
13362SQLITE_PRIVATE void sqlite3GenerateRowIndexDelete(Parse*, Table*, int, int, int*);
13363SQLITE_PRIVATE int sqlite3GenerateIndexKey(Parse*, Index*, int, int, int, int*,Index*,int);
13364SQLITE_PRIVATE void sqlite3ResolvePartIdxLabel(Parse*,int);
13365SQLITE_PRIVATE void sqlite3GenerateConstraintChecks(Parse*,Table*,int*,int,int,int,int,
13366                                     u8,u8,int,int*);
13367SQLITE_PRIVATE void sqlite3CompleteInsertion(Parse*,Table*,int,int,int,int*,int,int,int);
13368SQLITE_PRIVATE int sqlite3OpenTableAndIndices(Parse*, Table*, int, int, u8*, int*, int*);
13369SQLITE_PRIVATE void sqlite3BeginWriteOperation(Parse*, int, int);
13370SQLITE_PRIVATE void sqlite3MultiWrite(Parse*);
13371SQLITE_PRIVATE void sqlite3MayAbort(Parse*);
13372SQLITE_PRIVATE void sqlite3HaltConstraint(Parse*, int, int, char*, i8, u8);
13373SQLITE_PRIVATE void sqlite3UniqueConstraint(Parse*, int, Index*);
13374SQLITE_PRIVATE void sqlite3RowidConstraint(Parse*, int, Table*);
13375SQLITE_PRIVATE Expr *sqlite3ExprDup(sqlite3*,Expr*,int);
13376SQLITE_PRIVATE ExprList *sqlite3ExprListDup(sqlite3*,ExprList*,int);
13377SQLITE_PRIVATE SrcList *sqlite3SrcListDup(sqlite3*,SrcList*,int);
13378SQLITE_PRIVATE IdList *sqlite3IdListDup(sqlite3*,IdList*);
13379SQLITE_PRIVATE Select *sqlite3SelectDup(sqlite3*,Select*,int);
13380#if SELECTTRACE_ENABLED
13381SQLITE_PRIVATE void sqlite3SelectSetName(Select*,const char*);
13382#else
13383# define sqlite3SelectSetName(A,B)
13384#endif
13385SQLITE_PRIVATE void sqlite3FuncDefInsert(FuncDefHash*, FuncDef*);
13386SQLITE_PRIVATE FuncDef *sqlite3FindFunction(sqlite3*,const char*,int,int,u8,u8);
13387SQLITE_PRIVATE void sqlite3RegisterBuiltinFunctions(sqlite3*);
13388SQLITE_PRIVATE void sqlite3RegisterDateTimeFunctions(void);
13389SQLITE_PRIVATE void sqlite3RegisterGlobalFunctions(void);
13390SQLITE_PRIVATE int sqlite3SafetyCheckOk(sqlite3*);
13391SQLITE_PRIVATE int sqlite3SafetyCheckSickOrOk(sqlite3*);
13392SQLITE_PRIVATE void sqlite3ChangeCookie(Parse*, int);
13393
13394#if !defined(SQLITE_OMIT_VIEW) && !defined(SQLITE_OMIT_TRIGGER)
13395SQLITE_PRIVATE void sqlite3MaterializeView(Parse*, Table*, Expr*, int);
13396#endif
13397
13398#ifndef SQLITE_OMIT_TRIGGER
13399SQLITE_PRIVATE   void sqlite3BeginTrigger(Parse*, Token*,Token*,int,int,IdList*,SrcList*,
13400                           Expr*,int, int);
13401SQLITE_PRIVATE   void sqlite3FinishTrigger(Parse*, TriggerStep*, Token*);
13402SQLITE_PRIVATE   void sqlite3DropTrigger(Parse*, SrcList*, int);
13403SQLITE_PRIVATE   void sqlite3DropTriggerPtr(Parse*, Trigger*);
13404SQLITE_PRIVATE   Trigger *sqlite3TriggersExist(Parse *, Table*, int, ExprList*, int *pMask);
13405SQLITE_PRIVATE   Trigger *sqlite3TriggerList(Parse *, Table *);
13406SQLITE_PRIVATE   void sqlite3CodeRowTrigger(Parse*, Trigger *, int, ExprList*, int, Table *,
13407                            int, int, int);
13408SQLITE_PRIVATE   void sqlite3CodeRowTriggerDirect(Parse *, Trigger *, Table *, int, int, int);
13409  void sqliteViewTriggers(Parse*, Table*, Expr*, int, ExprList*);
13410SQLITE_PRIVATE   void sqlite3DeleteTriggerStep(sqlite3*, TriggerStep*);
13411SQLITE_PRIVATE   TriggerStep *sqlite3TriggerSelectStep(sqlite3*,Select*);
13412SQLITE_PRIVATE   TriggerStep *sqlite3TriggerInsertStep(sqlite3*,Token*, IdList*,
13413                                        Select*,u8);
13414SQLITE_PRIVATE   TriggerStep *sqlite3TriggerUpdateStep(sqlite3*,Token*,ExprList*, Expr*, u8);
13415SQLITE_PRIVATE   TriggerStep *sqlite3TriggerDeleteStep(sqlite3*,Token*, Expr*);
13416SQLITE_PRIVATE   void sqlite3DeleteTrigger(sqlite3*, Trigger*);
13417SQLITE_PRIVATE   void sqlite3UnlinkAndDeleteTrigger(sqlite3*,int,const char*);
13418SQLITE_PRIVATE   u32 sqlite3TriggerColmask(Parse*,Trigger*,ExprList*,int,int,Table*,int);
13419# define sqlite3ParseToplevel(p) ((p)->pToplevel ? (p)->pToplevel : (p))
13420#else
13421# define sqlite3TriggersExist(B,C,D,E,F) 0
13422# define sqlite3DeleteTrigger(A,B)
13423# define sqlite3DropTriggerPtr(A,B)
13424# define sqlite3UnlinkAndDeleteTrigger(A,B,C)
13425# define sqlite3CodeRowTrigger(A,B,C,D,E,F,G,H,I)
13426# define sqlite3CodeRowTriggerDirect(A,B,C,D,E,F)
13427# define sqlite3TriggerList(X, Y) 0
13428# define sqlite3ParseToplevel(p) p
13429# define sqlite3TriggerColmask(A,B,C,D,E,F,G) 0
13430#endif
13431
13432SQLITE_PRIVATE int sqlite3JoinType(Parse*, Token*, Token*, Token*);
13433SQLITE_PRIVATE void sqlite3CreateForeignKey(Parse*, ExprList*, Token*, ExprList*, int);
13434SQLITE_PRIVATE void sqlite3DeferForeignKey(Parse*, int);
13435#ifndef SQLITE_OMIT_AUTHORIZATION
13436SQLITE_PRIVATE   void sqlite3AuthRead(Parse*,Expr*,Schema*,SrcList*);
13437SQLITE_PRIVATE   int sqlite3AuthCheck(Parse*,int, const char*, const char*, const char*);
13438SQLITE_PRIVATE   void sqlite3AuthContextPush(Parse*, AuthContext*, const char*);
13439SQLITE_PRIVATE   void sqlite3AuthContextPop(AuthContext*);
13440SQLITE_PRIVATE   int sqlite3AuthReadCol(Parse*, const char *, const char *, int);
13441#else
13442# define sqlite3AuthRead(a,b,c,d)
13443# define sqlite3AuthCheck(a,b,c,d,e)    SQLITE_OK
13444# define sqlite3AuthContextPush(a,b,c)
13445# define sqlite3AuthContextPop(a)  ((void)(a))
13446#endif
13447SQLITE_PRIVATE void sqlite3Attach(Parse*, Expr*, Expr*, Expr*);
13448SQLITE_PRIVATE void sqlite3Detach(Parse*, Expr*);
13449SQLITE_PRIVATE void sqlite3FixInit(DbFixer*, Parse*, int, const char*, const Token*);
13450SQLITE_PRIVATE int sqlite3FixSrcList(DbFixer*, SrcList*);
13451SQLITE_PRIVATE int sqlite3FixSelect(DbFixer*, Select*);
13452SQLITE_PRIVATE int sqlite3FixExpr(DbFixer*, Expr*);
13453SQLITE_PRIVATE int sqlite3FixExprList(DbFixer*, ExprList*);
13454SQLITE_PRIVATE int sqlite3FixTriggerStep(DbFixer*, TriggerStep*);
13455SQLITE_PRIVATE int sqlite3AtoF(const char *z, double*, int, u8);
13456SQLITE_PRIVATE int sqlite3GetInt32(const char *, int*);
13457SQLITE_PRIVATE int sqlite3Atoi(const char*);
13458SQLITE_PRIVATE int sqlite3Utf16ByteLen(const void *pData, int nChar);
13459SQLITE_PRIVATE int sqlite3Utf8CharLen(const char *pData, int nByte);
13460SQLITE_PRIVATE u32 sqlite3Utf8Read(const u8**);
13461SQLITE_PRIVATE LogEst sqlite3LogEst(u64);
13462SQLITE_PRIVATE LogEst sqlite3LogEstAdd(LogEst,LogEst);
13463#ifndef SQLITE_OMIT_VIRTUALTABLE
13464SQLITE_PRIVATE LogEst sqlite3LogEstFromDouble(double);
13465#endif
13466SQLITE_PRIVATE u64 sqlite3LogEstToInt(LogEst);
13467
13468/*
13469** Routines to read and write variable-length integers.  These used to
13470** be defined locally, but now we use the varint routines in the util.c
13471** file.
13472*/
13473SQLITE_PRIVATE int sqlite3PutVarint(unsigned char*, u64);
13474SQLITE_PRIVATE u8 sqlite3GetVarint(const unsigned char *, u64 *);
13475SQLITE_PRIVATE u8 sqlite3GetVarint32(const unsigned char *, u32 *);
13476SQLITE_PRIVATE int sqlite3VarintLen(u64 v);
13477
13478/*
13479** The common case is for a varint to be a single byte.  They following
13480** macros handle the common case without a procedure call, but then call
13481** the procedure for larger varints.
13482*/
13483#define getVarint32(A,B)  \
13484  (u8)((*(A)<(u8)0x80)?((B)=(u32)*(A)),1:sqlite3GetVarint32((A),(u32 *)&(B)))
13485#define putVarint32(A,B)  \
13486  (u8)(((u32)(B)<(u32)0x80)?(*(A)=(unsigned char)(B)),1:\
13487  sqlite3PutVarint((A),(B)))
13488#define getVarint    sqlite3GetVarint
13489#define putVarint    sqlite3PutVarint
13490
13491
13492SQLITE_PRIVATE const char *sqlite3IndexAffinityStr(Vdbe *, Index *);
13493SQLITE_PRIVATE void sqlite3TableAffinity(Vdbe*, Table*, int);
13494SQLITE_PRIVATE char sqlite3CompareAffinity(Expr *pExpr, char aff2);
13495SQLITE_PRIVATE int sqlite3IndexAffinityOk(Expr *pExpr, char idx_affinity);
13496SQLITE_PRIVATE char sqlite3ExprAffinity(Expr *pExpr);
13497SQLITE_PRIVATE int sqlite3Atoi64(const char*, i64*, int, u8);
13498SQLITE_PRIVATE int sqlite3DecOrHexToI64(const char*, i64*);
13499SQLITE_PRIVATE void sqlite3ErrorWithMsg(sqlite3*, int, const char*,...);
13500SQLITE_PRIVATE void sqlite3Error(sqlite3*,int);
13501SQLITE_PRIVATE void *sqlite3HexToBlob(sqlite3*, const char *z, int n);
13502SQLITE_PRIVATE u8 sqlite3HexToInt(int h);
13503SQLITE_PRIVATE int sqlite3TwoPartName(Parse *, Token *, Token *, Token **);
13504
13505#if defined(SQLITE_NEED_ERR_NAME)
13506SQLITE_PRIVATE const char *sqlite3ErrName(int);
13507#endif
13508
13509SQLITE_PRIVATE const char *sqlite3ErrStr(int);
13510SQLITE_PRIVATE int sqlite3ReadSchema(Parse *pParse);
13511SQLITE_PRIVATE CollSeq *sqlite3FindCollSeq(sqlite3*,u8 enc, const char*,int);
13512SQLITE_PRIVATE CollSeq *sqlite3LocateCollSeq(Parse *pParse, const char*zName);
13513SQLITE_PRIVATE CollSeq *sqlite3ExprCollSeq(Parse *pParse, Expr *pExpr);
13514SQLITE_PRIVATE Expr *sqlite3ExprAddCollateToken(Parse *pParse, Expr*, const Token*, int);
13515SQLITE_PRIVATE Expr *sqlite3ExprAddCollateString(Parse*,Expr*,const char*);
13516SQLITE_PRIVATE Expr *sqlite3ExprSkipCollate(Expr*);
13517SQLITE_PRIVATE int sqlite3CheckCollSeq(Parse *, CollSeq *);
13518SQLITE_PRIVATE int sqlite3CheckObjectName(Parse *, const char *);
13519SQLITE_PRIVATE void sqlite3VdbeSetChanges(sqlite3 *, int);
13520SQLITE_PRIVATE int sqlite3AddInt64(i64*,i64);
13521SQLITE_PRIVATE int sqlite3SubInt64(i64*,i64);
13522SQLITE_PRIVATE int sqlite3MulInt64(i64*,i64);
13523SQLITE_PRIVATE int sqlite3AbsInt32(int);
13524#ifdef SQLITE_ENABLE_8_3_NAMES
13525SQLITE_PRIVATE void sqlite3FileSuffix3(const char*, char*);
13526#else
13527# define sqlite3FileSuffix3(X,Y)
13528#endif
13529SQLITE_PRIVATE u8 sqlite3GetBoolean(const char *z,u8);
13530
13531SQLITE_PRIVATE const void *sqlite3ValueText(sqlite3_value*, u8);
13532SQLITE_PRIVATE int sqlite3ValueBytes(sqlite3_value*, u8);
13533SQLITE_PRIVATE void sqlite3ValueSetStr(sqlite3_value*, int, const void *,u8,
13534                        void(*)(void*));
13535SQLITE_PRIVATE void sqlite3ValueSetNull(sqlite3_value*);
13536SQLITE_PRIVATE void sqlite3ValueFree(sqlite3_value*);
13537SQLITE_PRIVATE sqlite3_value *sqlite3ValueNew(sqlite3 *);
13538SQLITE_PRIVATE char *sqlite3Utf16to8(sqlite3 *, const void*, int, u8);
13539SQLITE_PRIVATE int sqlite3ValueFromExpr(sqlite3 *, Expr *, u8, u8, sqlite3_value **);
13540SQLITE_PRIVATE void sqlite3ValueApplyAffinity(sqlite3_value *, u8, u8);
13541#ifndef SQLITE_AMALGAMATION
13542SQLITE_PRIVATE const unsigned char sqlite3OpcodeProperty[];
13543SQLITE_PRIVATE const unsigned char sqlite3UpperToLower[];
13544SQLITE_PRIVATE const unsigned char sqlite3CtypeMap[];
13545SQLITE_PRIVATE const Token sqlite3IntTokens[];
13546SQLITE_PRIVATE SQLITE_WSD struct Sqlite3Config sqlite3Config;
13547SQLITE_PRIVATE SQLITE_WSD FuncDefHash sqlite3GlobalFunctions;
13548#ifndef SQLITE_OMIT_WSD
13549SQLITE_PRIVATE int sqlite3PendingByte;
13550#endif
13551#endif
13552SQLITE_PRIVATE void sqlite3RootPageMoved(sqlite3*, int, int, int);
13553SQLITE_PRIVATE void sqlite3Reindex(Parse*, Token*, Token*);
13554SQLITE_PRIVATE void sqlite3AlterFunctions(void);
13555SQLITE_PRIVATE void sqlite3AlterRenameTable(Parse*, SrcList*, Token*);
13556SQLITE_PRIVATE int sqlite3GetToken(const unsigned char *, int *);
13557SQLITE_PRIVATE void sqlite3NestedParse(Parse*, const char*, ...);
13558SQLITE_PRIVATE void sqlite3ExpirePreparedStatements(sqlite3*);
13559SQLITE_PRIVATE int sqlite3CodeSubselect(Parse *, Expr *, int, int);
13560SQLITE_PRIVATE void sqlite3SelectPrep(Parse*, Select*, NameContext*);
13561SQLITE_PRIVATE void sqlite3SelectWrongNumTermsError(Parse *pParse, Select *p);
13562SQLITE_PRIVATE int sqlite3MatchSpanName(const char*, const char*, const char*, const char*);
13563SQLITE_PRIVATE int sqlite3ResolveExprNames(NameContext*, Expr*);
13564SQLITE_PRIVATE void sqlite3ResolveSelectNames(Parse*, Select*, NameContext*);
13565SQLITE_PRIVATE void sqlite3ResolveSelfReference(Parse*,Table*,int,Expr*,ExprList*);
13566SQLITE_PRIVATE int sqlite3ResolveOrderGroupBy(Parse*, Select*, ExprList*, const char*);
13567SQLITE_PRIVATE void sqlite3ColumnDefault(Vdbe *, Table *, int, int);
13568SQLITE_PRIVATE void sqlite3AlterFinishAddColumn(Parse *, Token *);
13569SQLITE_PRIVATE void sqlite3AlterBeginAddColumn(Parse *, SrcList *);
13570SQLITE_PRIVATE CollSeq *sqlite3GetCollSeq(Parse*, u8, CollSeq *, const char*);
13571SQLITE_PRIVATE char sqlite3AffinityType(const char*, u8*);
13572SQLITE_PRIVATE void sqlite3Analyze(Parse*, Token*, Token*);
13573SQLITE_PRIVATE int sqlite3InvokeBusyHandler(BusyHandler*);
13574SQLITE_PRIVATE int sqlite3FindDb(sqlite3*, Token*);
13575SQLITE_PRIVATE int sqlite3FindDbName(sqlite3 *, const char *);
13576SQLITE_PRIVATE int sqlite3AnalysisLoad(sqlite3*,int iDB);
13577SQLITE_PRIVATE void sqlite3DeleteIndexSamples(sqlite3*,Index*);
13578SQLITE_PRIVATE void sqlite3DefaultRowEst(Index*);
13579SQLITE_PRIVATE void sqlite3RegisterLikeFunctions(sqlite3*, int);
13580SQLITE_PRIVATE int sqlite3IsLikeFunction(sqlite3*,Expr*,int*,char*);
13581SQLITE_PRIVATE void sqlite3MinimumFileFormat(Parse*, int, int);
13582SQLITE_PRIVATE void sqlite3SchemaClear(void *);
13583SQLITE_PRIVATE Schema *sqlite3SchemaGet(sqlite3 *, Btree *);
13584SQLITE_PRIVATE int sqlite3SchemaToIndex(sqlite3 *db, Schema *);
13585SQLITE_PRIVATE KeyInfo *sqlite3KeyInfoAlloc(sqlite3*,int,int);
13586SQLITE_PRIVATE void sqlite3KeyInfoUnref(KeyInfo*);
13587SQLITE_PRIVATE KeyInfo *sqlite3KeyInfoRef(KeyInfo*);
13588SQLITE_PRIVATE KeyInfo *sqlite3KeyInfoOfIndex(Parse*, Index*);
13589#ifdef SQLITE_DEBUG
13590SQLITE_PRIVATE int sqlite3KeyInfoIsWriteable(KeyInfo*);
13591#endif
13592SQLITE_PRIVATE int sqlite3CreateFunc(sqlite3 *, const char *, int, int, void *,
13593  void (*)(sqlite3_context*,int,sqlite3_value **),
13594  void (*)(sqlite3_context*,int,sqlite3_value **), void (*)(sqlite3_context*),
13595  FuncDestructor *pDestructor
13596);
13597SQLITE_PRIVATE int sqlite3ApiExit(sqlite3 *db, int);
13598SQLITE_PRIVATE int sqlite3OpenTempDatabase(Parse *);
13599
13600SQLITE_PRIVATE void sqlite3StrAccumInit(StrAccum*, sqlite3*, char*, int, int);
13601SQLITE_PRIVATE void sqlite3StrAccumAppend(StrAccum*,const char*,int);
13602SQLITE_PRIVATE void sqlite3StrAccumAppendAll(StrAccum*,const char*);
13603SQLITE_PRIVATE void sqlite3AppendChar(StrAccum*,int,char);
13604SQLITE_PRIVATE char *sqlite3StrAccumFinish(StrAccum*);
13605SQLITE_PRIVATE void sqlite3StrAccumReset(StrAccum*);
13606SQLITE_PRIVATE void sqlite3SelectDestInit(SelectDest*,int,int);
13607SQLITE_PRIVATE Expr *sqlite3CreateColumnExpr(sqlite3 *, SrcList *, int, int);
13608
13609SQLITE_PRIVATE void sqlite3BackupRestart(sqlite3_backup *);
13610SQLITE_PRIVATE void sqlite3BackupUpdate(sqlite3_backup *, Pgno, const u8 *);
13611
13612#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
13613SQLITE_PRIVATE void sqlite3AnalyzeFunctions(void);
13614SQLITE_PRIVATE int sqlite3Stat4ProbeSetValue(Parse*,Index*,UnpackedRecord**,Expr*,u8,int,int*);
13615SQLITE_PRIVATE int sqlite3Stat4ValueFromExpr(Parse*, Expr*, u8, sqlite3_value**);
13616SQLITE_PRIVATE void sqlite3Stat4ProbeFree(UnpackedRecord*);
13617SQLITE_PRIVATE int sqlite3Stat4Column(sqlite3*, const void*, int, int, sqlite3_value**);
13618#endif
13619
13620/*
13621** The interface to the LEMON-generated parser
13622*/
13623SQLITE_PRIVATE void *sqlite3ParserAlloc(void*(*)(u64));
13624SQLITE_PRIVATE void sqlite3ParserFree(void*, void(*)(void*));
13625SQLITE_PRIVATE void sqlite3Parser(void*, int, Token, Parse*);
13626#ifdef YYTRACKMAXSTACKDEPTH
13627SQLITE_PRIVATE   int sqlite3ParserStackPeak(void*);
13628#endif
13629
13630SQLITE_PRIVATE void sqlite3AutoLoadExtensions(sqlite3*);
13631#ifndef SQLITE_OMIT_LOAD_EXTENSION
13632SQLITE_PRIVATE   void sqlite3CloseExtensions(sqlite3*);
13633#else
13634# define sqlite3CloseExtensions(X)
13635#endif
13636
13637#ifndef SQLITE_OMIT_SHARED_CACHE
13638SQLITE_PRIVATE   void sqlite3TableLock(Parse *, int, int, u8, const char *);
13639#else
13640  #define sqlite3TableLock(v,w,x,y,z)
13641#endif
13642
13643#ifdef SQLITE_TEST
13644SQLITE_PRIVATE   int sqlite3Utf8To8(unsigned char*);
13645#endif
13646
13647#ifdef SQLITE_OMIT_VIRTUALTABLE
13648#  define sqlite3VtabClear(Y)
13649#  define sqlite3VtabSync(X,Y) SQLITE_OK
13650#  define sqlite3VtabRollback(X)
13651#  define sqlite3VtabCommit(X)
13652#  define sqlite3VtabInSync(db) 0
13653#  define sqlite3VtabLock(X)
13654#  define sqlite3VtabUnlock(X)
13655#  define sqlite3VtabUnlockList(X)
13656#  define sqlite3VtabSavepoint(X, Y, Z) SQLITE_OK
13657#  define sqlite3GetVTable(X,Y)  ((VTable*)0)
13658#else
13659SQLITE_PRIVATE    void sqlite3VtabClear(sqlite3 *db, Table*);
13660SQLITE_PRIVATE    void sqlite3VtabDisconnect(sqlite3 *db, Table *p);
13661SQLITE_PRIVATE    int sqlite3VtabSync(sqlite3 *db, Vdbe*);
13662SQLITE_PRIVATE    int sqlite3VtabRollback(sqlite3 *db);
13663SQLITE_PRIVATE    int sqlite3VtabCommit(sqlite3 *db);
13664SQLITE_PRIVATE    void sqlite3VtabLock(VTable *);
13665SQLITE_PRIVATE    void sqlite3VtabUnlock(VTable *);
13666SQLITE_PRIVATE    void sqlite3VtabUnlockList(sqlite3*);
13667SQLITE_PRIVATE    int sqlite3VtabSavepoint(sqlite3 *, int, int);
13668SQLITE_PRIVATE    void sqlite3VtabImportErrmsg(Vdbe*, sqlite3_vtab*);
13669SQLITE_PRIVATE    VTable *sqlite3GetVTable(sqlite3*, Table*);
13670#  define sqlite3VtabInSync(db) ((db)->nVTrans>0 && (db)->aVTrans==0)
13671#endif
13672SQLITE_PRIVATE void sqlite3VtabMakeWritable(Parse*,Table*);
13673SQLITE_PRIVATE void sqlite3VtabBeginParse(Parse*, Token*, Token*, Token*, int);
13674SQLITE_PRIVATE void sqlite3VtabFinishParse(Parse*, Token*);
13675SQLITE_PRIVATE void sqlite3VtabArgInit(Parse*);
13676SQLITE_PRIVATE void sqlite3VtabArgExtend(Parse*, Token*);
13677SQLITE_PRIVATE int sqlite3VtabCallCreate(sqlite3*, int, const char *, char **);
13678SQLITE_PRIVATE int sqlite3VtabCallConnect(Parse*, Table*);
13679SQLITE_PRIVATE int sqlite3VtabCallDestroy(sqlite3*, int, const char *);
13680SQLITE_PRIVATE int sqlite3VtabBegin(sqlite3 *, VTable *);
13681SQLITE_PRIVATE FuncDef *sqlite3VtabOverloadFunction(sqlite3 *,FuncDef*, int nArg, Expr*);
13682SQLITE_PRIVATE void sqlite3InvalidFunction(sqlite3_context*,int,sqlite3_value**);
13683SQLITE_PRIVATE sqlite3_int64 sqlite3StmtCurrentTime(sqlite3_context*);
13684SQLITE_PRIVATE int sqlite3VdbeParameterIndex(Vdbe*, const char*, int);
13685SQLITE_PRIVATE int sqlite3TransferBindings(sqlite3_stmt *, sqlite3_stmt *);
13686SQLITE_PRIVATE void sqlite3ParserReset(Parse*);
13687SQLITE_PRIVATE int sqlite3Reprepare(Vdbe*);
13688SQLITE_PRIVATE void sqlite3ExprListCheckLength(Parse*, ExprList*, const char*);
13689SQLITE_PRIVATE CollSeq *sqlite3BinaryCompareCollSeq(Parse *, Expr *, Expr *);
13690SQLITE_PRIVATE int sqlite3TempInMemory(const sqlite3*);
13691SQLITE_PRIVATE const char *sqlite3JournalModename(int);
13692#ifndef SQLITE_OMIT_WAL
13693SQLITE_PRIVATE   int sqlite3Checkpoint(sqlite3*, int, int, int*, int*);
13694SQLITE_PRIVATE   int sqlite3WalDefaultHook(void*,sqlite3*,const char*,int);
13695#endif
13696#ifndef SQLITE_OMIT_CTE
13697SQLITE_PRIVATE   With *sqlite3WithAdd(Parse*,With*,Token*,ExprList*,Select*);
13698SQLITE_PRIVATE   void sqlite3WithDelete(sqlite3*,With*);
13699SQLITE_PRIVATE   void sqlite3WithPush(Parse*, With*, u8);
13700#else
13701#define sqlite3WithPush(x,y,z)
13702#define sqlite3WithDelete(x,y)
13703#endif
13704
13705/* Declarations for functions in fkey.c. All of these are replaced by
13706** no-op macros if OMIT_FOREIGN_KEY is defined. In this case no foreign
13707** key functionality is available. If OMIT_TRIGGER is defined but
13708** OMIT_FOREIGN_KEY is not, only some of the functions are no-oped. In
13709** this case foreign keys are parsed, but no other functionality is
13710** provided (enforcement of FK constraints requires the triggers sub-system).
13711*/
13712#if !defined(SQLITE_OMIT_FOREIGN_KEY) && !defined(SQLITE_OMIT_TRIGGER)
13713SQLITE_PRIVATE   void sqlite3FkCheck(Parse*, Table*, int, int, int*, int);
13714SQLITE_PRIVATE   void sqlite3FkDropTable(Parse*, SrcList *, Table*);
13715SQLITE_PRIVATE   void sqlite3FkActions(Parse*, Table*, ExprList*, int, int*, int);
13716SQLITE_PRIVATE   int sqlite3FkRequired(Parse*, Table*, int*, int);
13717SQLITE_PRIVATE   u32 sqlite3FkOldmask(Parse*, Table*);
13718SQLITE_PRIVATE   FKey *sqlite3FkReferences(Table *);
13719#else
13720  #define sqlite3FkActions(a,b,c,d,e,f)
13721  #define sqlite3FkCheck(a,b,c,d,e,f)
13722  #define sqlite3FkDropTable(a,b,c)
13723  #define sqlite3FkOldmask(a,b)         0
13724  #define sqlite3FkRequired(a,b,c,d)    0
13725#endif
13726#ifndef SQLITE_OMIT_FOREIGN_KEY
13727SQLITE_PRIVATE   void sqlite3FkDelete(sqlite3 *, Table*);
13728SQLITE_PRIVATE   int sqlite3FkLocateIndex(Parse*,Table*,FKey*,Index**,int**);
13729#else
13730  #define sqlite3FkDelete(a,b)
13731  #define sqlite3FkLocateIndex(a,b,c,d,e)
13732#endif
13733
13734
13735/*
13736** Available fault injectors.  Should be numbered beginning with 0.
13737*/
13738#define SQLITE_FAULTINJECTOR_MALLOC     0
13739#define SQLITE_FAULTINJECTOR_COUNT      1
13740
13741/*
13742** The interface to the code in fault.c used for identifying "benign"
13743** malloc failures. This is only present if SQLITE_OMIT_BUILTIN_TEST
13744** is not defined.
13745*/
13746#ifndef SQLITE_OMIT_BUILTIN_TEST
13747SQLITE_PRIVATE   void sqlite3BeginBenignMalloc(void);
13748SQLITE_PRIVATE   void sqlite3EndBenignMalloc(void);
13749#else
13750  #define sqlite3BeginBenignMalloc()
13751  #define sqlite3EndBenignMalloc()
13752#endif
13753
13754/*
13755** Allowed return values from sqlite3FindInIndex()
13756*/
13757#define IN_INDEX_ROWID        1   /* Search the rowid of the table */
13758#define IN_INDEX_EPH          2   /* Search an ephemeral b-tree */
13759#define IN_INDEX_INDEX_ASC    3   /* Existing index ASCENDING */
13760#define IN_INDEX_INDEX_DESC   4   /* Existing index DESCENDING */
13761#define IN_INDEX_NOOP         5   /* No table available. Use comparisons */
13762/*
13763** Allowed flags for the 3rd parameter to sqlite3FindInIndex().
13764*/
13765#define IN_INDEX_NOOP_OK     0x0001  /* OK to return IN_INDEX_NOOP */
13766#define IN_INDEX_MEMBERSHIP  0x0002  /* IN operator used for membership test */
13767#define IN_INDEX_LOOP        0x0004  /* IN operator used as a loop */
13768SQLITE_PRIVATE int sqlite3FindInIndex(Parse *, Expr *, u32, int*);
13769
13770#ifdef SQLITE_ENABLE_ATOMIC_WRITE
13771SQLITE_PRIVATE   int sqlite3JournalOpen(sqlite3_vfs *, const char *, sqlite3_file *, int, int);
13772SQLITE_PRIVATE   int sqlite3JournalSize(sqlite3_vfs *);
13773SQLITE_PRIVATE   int sqlite3JournalCreate(sqlite3_file *);
13774SQLITE_PRIVATE   int sqlite3JournalExists(sqlite3_file *p);
13775#else
13776  #define sqlite3JournalSize(pVfs) ((pVfs)->szOsFile)
13777  #define sqlite3JournalExists(p) 1
13778#endif
13779
13780SQLITE_PRIVATE void sqlite3MemJournalOpen(sqlite3_file *);
13781SQLITE_PRIVATE int sqlite3MemJournalSize(void);
13782SQLITE_PRIVATE int sqlite3IsMemJournal(sqlite3_file *);
13783
13784SQLITE_PRIVATE void sqlite3ExprSetHeightAndFlags(Parse *pParse, Expr *p);
13785#if SQLITE_MAX_EXPR_DEPTH>0
13786SQLITE_PRIVATE   int sqlite3SelectExprHeight(Select *);
13787SQLITE_PRIVATE   int sqlite3ExprCheckHeight(Parse*, int);
13788#else
13789  #define sqlite3SelectExprHeight(x) 0
13790  #define sqlite3ExprCheckHeight(x,y)
13791#endif
13792
13793SQLITE_PRIVATE u32 sqlite3Get4byte(const u8*);
13794SQLITE_PRIVATE void sqlite3Put4byte(u8*, u32);
13795
13796#ifdef SQLITE_ENABLE_UNLOCK_NOTIFY
13797SQLITE_PRIVATE   void sqlite3ConnectionBlocked(sqlite3 *, sqlite3 *);
13798SQLITE_PRIVATE   void sqlite3ConnectionUnlocked(sqlite3 *db);
13799SQLITE_PRIVATE   void sqlite3ConnectionClosed(sqlite3 *db);
13800#else
13801  #define sqlite3ConnectionBlocked(x,y)
13802  #define sqlite3ConnectionUnlocked(x)
13803  #define sqlite3ConnectionClosed(x)
13804#endif
13805
13806#ifdef SQLITE_DEBUG
13807SQLITE_PRIVATE   void sqlite3ParserTrace(FILE*, char *);
13808#endif
13809
13810/*
13811** If the SQLITE_ENABLE IOTRACE exists then the global variable
13812** sqlite3IoTrace is a pointer to a printf-like routine used to
13813** print I/O tracing messages.
13814*/
13815#ifdef SQLITE_ENABLE_IOTRACE
13816# define IOTRACE(A)  if( sqlite3IoTrace ){ sqlite3IoTrace A; }
13817SQLITE_PRIVATE   void sqlite3VdbeIOTraceSql(Vdbe*);
13818SQLITE_API SQLITE_EXTERN void (SQLITE_CDECL *sqlite3IoTrace)(const char*,...);
13819#else
13820# define IOTRACE(A)
13821# define sqlite3VdbeIOTraceSql(X)
13822#endif
13823
13824/*
13825** These routines are available for the mem2.c debugging memory allocator
13826** only.  They are used to verify that different "types" of memory
13827** allocations are properly tracked by the system.
13828**
13829** sqlite3MemdebugSetType() sets the "type" of an allocation to one of
13830** the MEMTYPE_* macros defined below.  The type must be a bitmask with
13831** a single bit set.
13832**
13833** sqlite3MemdebugHasType() returns true if any of the bits in its second
13834** argument match the type set by the previous sqlite3MemdebugSetType().
13835** sqlite3MemdebugHasType() is intended for use inside assert() statements.
13836**
13837** sqlite3MemdebugNoType() returns true if none of the bits in its second
13838** argument match the type set by the previous sqlite3MemdebugSetType().
13839**
13840** Perhaps the most important point is the difference between MEMTYPE_HEAP
13841** and MEMTYPE_LOOKASIDE.  If an allocation is MEMTYPE_LOOKASIDE, that means
13842** it might have been allocated by lookaside, except the allocation was
13843** too large or lookaside was already full.  It is important to verify
13844** that allocations that might have been satisfied by lookaside are not
13845** passed back to non-lookaside free() routines.  Asserts such as the
13846** example above are placed on the non-lookaside free() routines to verify
13847** this constraint.
13848**
13849** All of this is no-op for a production build.  It only comes into
13850** play when the SQLITE_MEMDEBUG compile-time option is used.
13851*/
13852#ifdef SQLITE_MEMDEBUG
13853SQLITE_PRIVATE   void sqlite3MemdebugSetType(void*,u8);
13854SQLITE_PRIVATE   int sqlite3MemdebugHasType(void*,u8);
13855SQLITE_PRIVATE   int sqlite3MemdebugNoType(void*,u8);
13856#else
13857# define sqlite3MemdebugSetType(X,Y)  /* no-op */
13858# define sqlite3MemdebugHasType(X,Y)  1
13859# define sqlite3MemdebugNoType(X,Y)   1
13860#endif
13861#define MEMTYPE_HEAP       0x01  /* General heap allocations */
13862#define MEMTYPE_LOOKASIDE  0x02  /* Heap that might have been lookaside */
13863#define MEMTYPE_SCRATCH    0x04  /* Scratch allocations */
13864#define MEMTYPE_PCACHE     0x08  /* Page cache allocations */
13865
13866/*
13867** Threading interface
13868*/
13869#if SQLITE_MAX_WORKER_THREADS>0
13870SQLITE_PRIVATE int sqlite3ThreadCreate(SQLiteThread**,void*(*)(void*),void*);
13871SQLITE_PRIVATE int sqlite3ThreadJoin(SQLiteThread*, void**);
13872#endif
13873
13874#if defined(SQLITE_ENABLE_DBSTAT_VTAB) || defined(SQLITE_TEST)
13875SQLITE_PRIVATE int sqlite3DbstatRegister(sqlite3*);
13876#endif
13877
13878#endif /* _SQLITEINT_H_ */
13879
13880/************** End of sqliteInt.h *******************************************/
13881/************** Begin file global.c ******************************************/
13882/*
13883** 2008 June 13
13884**
13885** The author disclaims copyright to this source code.  In place of
13886** a legal notice, here is a blessing:
13887**
13888**    May you do good and not evil.
13889**    May you find forgiveness for yourself and forgive others.
13890**    May you share freely, never taking more than you give.
13891**
13892*************************************************************************
13893**
13894** This file contains definitions of global variables and constants.
13895*/
13896/* #include "sqliteInt.h" */
13897
13898/* An array to map all upper-case characters into their corresponding
13899** lower-case character.
13900**
13901** SQLite only considers US-ASCII (or EBCDIC) characters.  We do not
13902** handle case conversions for the UTF character set since the tables
13903** involved are nearly as big or bigger than SQLite itself.
13904*/
13905SQLITE_PRIVATE const unsigned char sqlite3UpperToLower[] = {
13906#ifdef SQLITE_ASCII
13907      0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16, 17,
13908     18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35,
13909     36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53,
13910     54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 97, 98, 99,100,101,102,103,
13911    104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,121,
13912    122, 91, 92, 93, 94, 95, 96, 97, 98, 99,100,101,102,103,104,105,106,107,
13913    108,109,110,111,112,113,114,115,116,117,118,119,120,121,122,123,124,125,
13914    126,127,128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143,
13915    144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159,160,161,
13916    162,163,164,165,166,167,168,169,170,171,172,173,174,175,176,177,178,179,
13917    180,181,182,183,184,185,186,187,188,189,190,191,192,193,194,195,196,197,
13918    198,199,200,201,202,203,204,205,206,207,208,209,210,211,212,213,214,215,
13919    216,217,218,219,220,221,222,223,224,225,226,227,228,229,230,231,232,233,
13920    234,235,236,237,238,239,240,241,242,243,244,245,246,247,248,249,250,251,
13921    252,253,254,255
13922#endif
13923#ifdef SQLITE_EBCDIC
13924      0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, /* 0x */
13925     16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, /* 1x */
13926     32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, /* 2x */
13927     48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, /* 3x */
13928     64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, /* 4x */
13929     80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, /* 5x */
13930     96, 97, 98, 99,100,101,102,103,104,105,106,107,108,109,110,111, /* 6x */
13931    112,113,114,115,116,117,118,119,120,121,122,123,124,125,126,127, /* 7x */
13932    128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143, /* 8x */
13933    144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159, /* 9x */
13934    160,161,162,163,164,165,166,167,168,169,170,171,140,141,142,175, /* Ax */
13935    176,177,178,179,180,181,182,183,184,185,186,187,188,189,190,191, /* Bx */
13936    192,129,130,131,132,133,134,135,136,137,202,203,204,205,206,207, /* Cx */
13937    208,145,146,147,148,149,150,151,152,153,218,219,220,221,222,223, /* Dx */
13938    224,225,162,163,164,165,166,167,168,169,234,235,236,237,238,239, /* Ex */
13939    240,241,242,243,244,245,246,247,248,249,250,251,252,253,254,255, /* Fx */
13940#endif
13941};
13942
13943/*
13944** The following 256 byte lookup table is used to support SQLites built-in
13945** equivalents to the following standard library functions:
13946**
13947**   isspace()                        0x01
13948**   isalpha()                        0x02
13949**   isdigit()                        0x04
13950**   isalnum()                        0x06
13951**   isxdigit()                       0x08
13952**   toupper()                        0x20
13953**   SQLite identifier character      0x40
13954**
13955** Bit 0x20 is set if the mapped character requires translation to upper
13956** case. i.e. if the character is a lower-case ASCII character.
13957** If x is a lower-case ASCII character, then its upper-case equivalent
13958** is (x - 0x20). Therefore toupper() can be implemented as:
13959**
13960**   (x & ~(map[x]&0x20))
13961**
13962** Standard function tolower() is implemented using the sqlite3UpperToLower[]
13963** array. tolower() is used more often than toupper() by SQLite.
13964**
13965** Bit 0x40 is set if the character non-alphanumeric and can be used in an
13966** SQLite identifier.  Identifiers are alphanumerics, "_", "$", and any
13967** non-ASCII UTF character. Hence the test for whether or not a character is
13968** part of an identifier is 0x46.
13969**
13970** SQLite's versions are identical to the standard versions assuming a
13971** locale of "C". They are implemented as macros in sqliteInt.h.
13972*/
13973#ifdef SQLITE_ASCII
13974SQLITE_PRIVATE const unsigned char sqlite3CtypeMap[256] = {
13975  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,  /* 00..07    ........ */
13976  0x00, 0x01, 0x01, 0x01, 0x01, 0x01, 0x00, 0x00,  /* 08..0f    ........ */
13977  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,  /* 10..17    ........ */
13978  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,  /* 18..1f    ........ */
13979  0x01, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00,  /* 20..27     !"#$%&' */
13980  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,  /* 28..2f    ()*+,-./ */
13981  0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c,  /* 30..37    01234567 */
13982  0x0c, 0x0c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,  /* 38..3f    89:;<=>? */
13983
13984  0x00, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x02,  /* 40..47    @ABCDEFG */
13985  0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02,  /* 48..4f    HIJKLMNO */
13986  0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02,  /* 50..57    PQRSTUVW */
13987  0x02, 0x02, 0x02, 0x00, 0x00, 0x00, 0x00, 0x40,  /* 58..5f    XYZ[\]^_ */
13988  0x00, 0x2a, 0x2a, 0x2a, 0x2a, 0x2a, 0x2a, 0x22,  /* 60..67    `abcdefg */
13989  0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22,  /* 68..6f    hijklmno */
13990  0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22,  /* 70..77    pqrstuvw */
13991  0x22, 0x22, 0x22, 0x00, 0x00, 0x00, 0x00, 0x00,  /* 78..7f    xyz{|}~. */
13992
13993  0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,  /* 80..87    ........ */
13994  0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,  /* 88..8f    ........ */
13995  0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,  /* 90..97    ........ */
13996  0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,  /* 98..9f    ........ */
13997  0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,  /* a0..a7    ........ */
13998  0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,  /* a8..af    ........ */
13999  0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,  /* b0..b7    ........ */
14000  0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,  /* b8..bf    ........ */
14001
14002  0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,  /* c0..c7    ........ */
14003  0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,  /* c8..cf    ........ */
14004  0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,  /* d0..d7    ........ */
14005  0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,  /* d8..df    ........ */
14006  0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,  /* e0..e7    ........ */
14007  0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,  /* e8..ef    ........ */
14008  0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,  /* f0..f7    ........ */
14009  0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40   /* f8..ff    ........ */
14010};
14011#endif
14012
14013/* EVIDENCE-OF: R-02982-34736 In order to maintain full backwards
14014** compatibility for legacy applications, the URI filename capability is
14015** disabled by default.
14016**
14017** EVIDENCE-OF: R-38799-08373 URI filenames can be enabled or disabled
14018** using the SQLITE_USE_URI=1 or SQLITE_USE_URI=0 compile-time options.
14019**
14020** EVIDENCE-OF: R-43642-56306 By default, URI handling is globally
14021** disabled. The default value may be changed by compiling with the
14022** SQLITE_USE_URI symbol defined.
14023*/
14024#ifndef SQLITE_USE_URI
14025# define  SQLITE_USE_URI 0
14026#endif
14027
14028/* EVIDENCE-OF: R-38720-18127 The default setting is determined by the
14029** SQLITE_ALLOW_COVERING_INDEX_SCAN compile-time option, or is "on" if
14030** that compile-time option is omitted.
14031*/
14032#ifndef SQLITE_ALLOW_COVERING_INDEX_SCAN
14033# define SQLITE_ALLOW_COVERING_INDEX_SCAN 1
14034#endif
14035
14036/* The minimum PMA size is set to this value multiplied by the database
14037** page size in bytes.
14038*/
14039#ifndef SQLITE_SORTER_PMASZ
14040# define SQLITE_SORTER_PMASZ 250
14041#endif
14042
14043/*
14044** The following singleton contains the global configuration for
14045** the SQLite library.
14046*/
14047SQLITE_PRIVATE SQLITE_WSD struct Sqlite3Config sqlite3Config = {
14048   SQLITE_DEFAULT_MEMSTATUS,  /* bMemstat */
14049   1,                         /* bCoreMutex */
14050   SQLITE_THREADSAFE==1,      /* bFullMutex */
14051   SQLITE_USE_URI,            /* bOpenUri */
14052   SQLITE_ALLOW_COVERING_INDEX_SCAN,   /* bUseCis */
14053   0x7ffffffe,                /* mxStrlen */
14054   0,                         /* neverCorrupt */
14055   128,                       /* szLookaside */
14056   500,                       /* nLookaside */
14057   {0,0,0,0,0,0,0,0},         /* m */
14058   {0,0,0,0,0,0,0,0,0},       /* mutex */
14059   {0,0,0,0,0,0,0,0,0,0,0,0,0},/* pcache2 */
14060   (void*)0,                  /* pHeap */
14061   0,                         /* nHeap */
14062   0, 0,                      /* mnHeap, mxHeap */
14063   SQLITE_DEFAULT_MMAP_SIZE,  /* szMmap */
14064   SQLITE_MAX_MMAP_SIZE,      /* mxMmap */
14065   (void*)0,                  /* pScratch */
14066   0,                         /* szScratch */
14067   0,                         /* nScratch */
14068   (void*)0,                  /* pPage */
14069   0,                         /* szPage */
14070   SQLITE_DEFAULT_PCACHE_INITSZ, /* nPage */
14071   0,                         /* mxParserStack */
14072   0,                         /* sharedCacheEnabled */
14073   SQLITE_SORTER_PMASZ,       /* szPma */
14074   /* All the rest should always be initialized to zero */
14075   0,                         /* isInit */
14076   0,                         /* inProgress */
14077   0,                         /* isMutexInit */
14078   0,                         /* isMallocInit */
14079   0,                         /* isPCacheInit */
14080   0,                         /* nRefInitMutex */
14081   0,                         /* pInitMutex */
14082   0,                         /* xLog */
14083   0,                         /* pLogArg */
14084#ifdef SQLITE_ENABLE_SQLLOG
14085   0,                         /* xSqllog */
14086   0,                         /* pSqllogArg */
14087#endif
14088#ifdef SQLITE_VDBE_COVERAGE
14089   0,                         /* xVdbeBranch */
14090   0,                         /* pVbeBranchArg */
14091#endif
14092#ifndef SQLITE_OMIT_BUILTIN_TEST
14093   0,                         /* xTestCallback */
14094#endif
14095   0                          /* bLocaltimeFault */
14096};
14097
14098/*
14099** Hash table for global functions - functions common to all
14100** database connections.  After initialization, this table is
14101** read-only.
14102*/
14103SQLITE_PRIVATE SQLITE_WSD FuncDefHash sqlite3GlobalFunctions;
14104
14105/*
14106** Constant tokens for values 0 and 1.
14107*/
14108SQLITE_PRIVATE const Token sqlite3IntTokens[] = {
14109   { "0", 1 },
14110   { "1", 1 }
14111};
14112
14113
14114/*
14115** The value of the "pending" byte must be 0x40000000 (1 byte past the
14116** 1-gibabyte boundary) in a compatible database.  SQLite never uses
14117** the database page that contains the pending byte.  It never attempts
14118** to read or write that page.  The pending byte page is set assign
14119** for use by the VFS layers as space for managing file locks.
14120**
14121** During testing, it is often desirable to move the pending byte to
14122** a different position in the file.  This allows code that has to
14123** deal with the pending byte to run on files that are much smaller
14124** than 1 GiB.  The sqlite3_test_control() interface can be used to
14125** move the pending byte.
14126**
14127** IMPORTANT:  Changing the pending byte to any value other than
14128** 0x40000000 results in an incompatible database file format!
14129** Changing the pending byte during operation will result in undefined
14130** and incorrect behavior.
14131*/
14132#ifndef SQLITE_OMIT_WSD
14133SQLITE_PRIVATE int sqlite3PendingByte = 0x40000000;
14134#endif
14135
14136/* #include "opcodes.h" */
14137/*
14138** Properties of opcodes.  The OPFLG_INITIALIZER macro is
14139** created by mkopcodeh.awk during compilation.  Data is obtained
14140** from the comments following the "case OP_xxxx:" statements in
14141** the vdbe.c file.
14142*/
14143SQLITE_PRIVATE const unsigned char sqlite3OpcodeProperty[] = OPFLG_INITIALIZER;
14144
14145/************** End of global.c **********************************************/
14146/************** Begin file ctime.c *******************************************/
14147/*
14148** 2010 February 23
14149**
14150** The author disclaims copyright to this source code.  In place of
14151** a legal notice, here is a blessing:
14152**
14153**    May you do good and not evil.
14154**    May you find forgiveness for yourself and forgive others.
14155**    May you share freely, never taking more than you give.
14156**
14157*************************************************************************
14158**
14159** This file implements routines used to report what compile-time options
14160** SQLite was built with.
14161*/
14162
14163#ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
14164
14165/* #include "sqliteInt.h" */
14166
14167/*
14168** An array of names of all compile-time options.  This array should
14169** be sorted A-Z.
14170**
14171** This array looks large, but in a typical installation actually uses
14172** only a handful of compile-time options, so most times this array is usually
14173** rather short and uses little memory space.
14174*/
14175static const char * const azCompileOpt[] = {
14176
14177/* These macros are provided to "stringify" the value of the define
14178** for those options in which the value is meaningful. */
14179#define CTIMEOPT_VAL_(opt) #opt
14180#define CTIMEOPT_VAL(opt) CTIMEOPT_VAL_(opt)
14181
14182#if SQLITE_32BIT_ROWID
14183  "32BIT_ROWID",
14184#endif
14185#if SQLITE_4_BYTE_ALIGNED_MALLOC
14186  "4_BYTE_ALIGNED_MALLOC",
14187#endif
14188#if SQLITE_CASE_SENSITIVE_LIKE
14189  "CASE_SENSITIVE_LIKE",
14190#endif
14191#if SQLITE_CHECK_PAGES
14192  "CHECK_PAGES",
14193#endif
14194#if SQLITE_COVERAGE_TEST
14195  "COVERAGE_TEST",
14196#endif
14197#if SQLITE_DEBUG
14198  "DEBUG",
14199#endif
14200#if SQLITE_DEFAULT_LOCKING_MODE
14201  "DEFAULT_LOCKING_MODE=" CTIMEOPT_VAL(SQLITE_DEFAULT_LOCKING_MODE),
14202#endif
14203#if defined(SQLITE_DEFAULT_MMAP_SIZE) && !defined(SQLITE_DEFAULT_MMAP_SIZE_xc)
14204  "DEFAULT_MMAP_SIZE=" CTIMEOPT_VAL(SQLITE_DEFAULT_MMAP_SIZE),
14205#endif
14206#if SQLITE_DISABLE_DIRSYNC
14207  "DISABLE_DIRSYNC",
14208#endif
14209#if SQLITE_DISABLE_LFS
14210  "DISABLE_LFS",
14211#endif
14212#if SQLITE_ENABLE_API_ARMOR
14213  "ENABLE_API_ARMOR",
14214#endif
14215#if SQLITE_ENABLE_ATOMIC_WRITE
14216  "ENABLE_ATOMIC_WRITE",
14217#endif
14218#if SQLITE_ENABLE_CEROD
14219  "ENABLE_CEROD",
14220#endif
14221#if SQLITE_ENABLE_COLUMN_METADATA
14222  "ENABLE_COLUMN_METADATA",
14223#endif
14224#if SQLITE_ENABLE_DBSTAT_VTAB
14225  "ENABLE_DBSTAT_VTAB",
14226#endif
14227#if SQLITE_ENABLE_EXPENSIVE_ASSERT
14228  "ENABLE_EXPENSIVE_ASSERT",
14229#endif
14230#if SQLITE_ENABLE_FTS1
14231  "ENABLE_FTS1",
14232#endif
14233#if SQLITE_ENABLE_FTS2
14234  "ENABLE_FTS2",
14235#endif
14236#if SQLITE_ENABLE_FTS3
14237  "ENABLE_FTS3",
14238#endif
14239#if SQLITE_ENABLE_FTS3_PARENTHESIS
14240  "ENABLE_FTS3_PARENTHESIS",
14241#endif
14242#if SQLITE_ENABLE_FTS4
14243  "ENABLE_FTS4",
14244#endif
14245#if SQLITE_ENABLE_ICU
14246  "ENABLE_ICU",
14247#endif
14248#if SQLITE_ENABLE_IOTRACE
14249  "ENABLE_IOTRACE",
14250#endif
14251#if SQLITE_ENABLE_LOAD_EXTENSION
14252  "ENABLE_LOAD_EXTENSION",
14253#endif
14254#if SQLITE_ENABLE_LOCKING_STYLE
14255  "ENABLE_LOCKING_STYLE=" CTIMEOPT_VAL(SQLITE_ENABLE_LOCKING_STYLE),
14256#endif
14257#if SQLITE_ENABLE_MEMORY_MANAGEMENT
14258  "ENABLE_MEMORY_MANAGEMENT",
14259#endif
14260#if SQLITE_ENABLE_MEMSYS3
14261  "ENABLE_MEMSYS3",
14262#endif
14263#if SQLITE_ENABLE_MEMSYS5
14264  "ENABLE_MEMSYS5",
14265#endif
14266#if SQLITE_ENABLE_OVERSIZE_CELL_CHECK
14267  "ENABLE_OVERSIZE_CELL_CHECK",
14268#endif
14269#if SQLITE_ENABLE_RTREE
14270  "ENABLE_RTREE",
14271#endif
14272#if defined(SQLITE_ENABLE_STAT4)
14273  "ENABLE_STAT4",
14274#elif defined(SQLITE_ENABLE_STAT3)
14275  "ENABLE_STAT3",
14276#endif
14277#if SQLITE_ENABLE_UNLOCK_NOTIFY
14278  "ENABLE_UNLOCK_NOTIFY",
14279#endif
14280#if SQLITE_ENABLE_UPDATE_DELETE_LIMIT
14281  "ENABLE_UPDATE_DELETE_LIMIT",
14282#endif
14283#if SQLITE_HAS_CODEC
14284  "HAS_CODEC",
14285#endif
14286#if HAVE_ISNAN || SQLITE_HAVE_ISNAN
14287  "HAVE_ISNAN",
14288#endif
14289#if SQLITE_HOMEGROWN_RECURSIVE_MUTEX
14290  "HOMEGROWN_RECURSIVE_MUTEX",
14291#endif
14292#if SQLITE_IGNORE_AFP_LOCK_ERRORS
14293  "IGNORE_AFP_LOCK_ERRORS",
14294#endif
14295#if SQLITE_IGNORE_FLOCK_LOCK_ERRORS
14296  "IGNORE_FLOCK_LOCK_ERRORS",
14297#endif
14298#ifdef SQLITE_INT64_TYPE
14299  "INT64_TYPE",
14300#endif
14301#if SQLITE_LOCK_TRACE
14302  "LOCK_TRACE",
14303#endif
14304#if defined(SQLITE_MAX_MMAP_SIZE) && !defined(SQLITE_MAX_MMAP_SIZE_xc)
14305  "MAX_MMAP_SIZE=" CTIMEOPT_VAL(SQLITE_MAX_MMAP_SIZE),
14306#endif
14307#ifdef SQLITE_MAX_SCHEMA_RETRY
14308  "MAX_SCHEMA_RETRY=" CTIMEOPT_VAL(SQLITE_MAX_SCHEMA_RETRY),
14309#endif
14310#if SQLITE_MEMDEBUG
14311  "MEMDEBUG",
14312#endif
14313#if SQLITE_MIXED_ENDIAN_64BIT_FLOAT
14314  "MIXED_ENDIAN_64BIT_FLOAT",
14315#endif
14316#if SQLITE_NO_SYNC
14317  "NO_SYNC",
14318#endif
14319#if SQLITE_OMIT_ALTERTABLE
14320  "OMIT_ALTERTABLE",
14321#endif
14322#if SQLITE_OMIT_ANALYZE
14323  "OMIT_ANALYZE",
14324#endif
14325#if SQLITE_OMIT_ATTACH
14326  "OMIT_ATTACH",
14327#endif
14328#if SQLITE_OMIT_AUTHORIZATION
14329  "OMIT_AUTHORIZATION",
14330#endif
14331#if SQLITE_OMIT_AUTOINCREMENT
14332  "OMIT_AUTOINCREMENT",
14333#endif
14334#if SQLITE_OMIT_AUTOINIT
14335  "OMIT_AUTOINIT",
14336#endif
14337#if SQLITE_OMIT_AUTOMATIC_INDEX
14338  "OMIT_AUTOMATIC_INDEX",
14339#endif
14340#if SQLITE_OMIT_AUTORESET
14341  "OMIT_AUTORESET",
14342#endif
14343#if SQLITE_OMIT_AUTOVACUUM
14344  "OMIT_AUTOVACUUM",
14345#endif
14346#if SQLITE_OMIT_BETWEEN_OPTIMIZATION
14347  "OMIT_BETWEEN_OPTIMIZATION",
14348#endif
14349#if SQLITE_OMIT_BLOB_LITERAL
14350  "OMIT_BLOB_LITERAL",
14351#endif
14352#if SQLITE_OMIT_BTREECOUNT
14353  "OMIT_BTREECOUNT",
14354#endif
14355#if SQLITE_OMIT_BUILTIN_TEST
14356  "OMIT_BUILTIN_TEST",
14357#endif
14358#if SQLITE_OMIT_CAST
14359  "OMIT_CAST",
14360#endif
14361#if SQLITE_OMIT_CHECK
14362  "OMIT_CHECK",
14363#endif
14364#if SQLITE_OMIT_COMPLETE
14365  "OMIT_COMPLETE",
14366#endif
14367#if SQLITE_OMIT_COMPOUND_SELECT
14368  "OMIT_COMPOUND_SELECT",
14369#endif
14370#if SQLITE_OMIT_CTE
14371  "OMIT_CTE",
14372#endif
14373#if SQLITE_OMIT_DATETIME_FUNCS
14374  "OMIT_DATETIME_FUNCS",
14375#endif
14376#if SQLITE_OMIT_DECLTYPE
14377  "OMIT_DECLTYPE",
14378#endif
14379#if SQLITE_OMIT_DEPRECATED
14380  "OMIT_DEPRECATED",
14381#endif
14382#if SQLITE_OMIT_DISKIO
14383  "OMIT_DISKIO",
14384#endif
14385#if SQLITE_OMIT_EXPLAIN
14386  "OMIT_EXPLAIN",
14387#endif
14388#if SQLITE_OMIT_FLAG_PRAGMAS
14389  "OMIT_FLAG_PRAGMAS",
14390#endif
14391#if SQLITE_OMIT_FLOATING_POINT
14392  "OMIT_FLOATING_POINT",
14393#endif
14394#if SQLITE_OMIT_FOREIGN_KEY
14395  "OMIT_FOREIGN_KEY",
14396#endif
14397#if SQLITE_OMIT_GET_TABLE
14398  "OMIT_GET_TABLE",
14399#endif
14400#if SQLITE_OMIT_INCRBLOB
14401  "OMIT_INCRBLOB",
14402#endif
14403#if SQLITE_OMIT_INTEGRITY_CHECK
14404  "OMIT_INTEGRITY_CHECK",
14405#endif
14406#if SQLITE_OMIT_LIKE_OPTIMIZATION
14407  "OMIT_LIKE_OPTIMIZATION",
14408#endif
14409#if SQLITE_OMIT_LOAD_EXTENSION
14410  "OMIT_LOAD_EXTENSION",
14411#endif
14412#if SQLITE_OMIT_LOCALTIME
14413  "OMIT_LOCALTIME",
14414#endif
14415#if SQLITE_OMIT_LOOKASIDE
14416  "OMIT_LOOKASIDE",
14417#endif
14418#if SQLITE_OMIT_MEMORYDB
14419  "OMIT_MEMORYDB",
14420#endif
14421#if SQLITE_OMIT_OR_OPTIMIZATION
14422  "OMIT_OR_OPTIMIZATION",
14423#endif
14424#if SQLITE_OMIT_PAGER_PRAGMAS
14425  "OMIT_PAGER_PRAGMAS",
14426#endif
14427#if SQLITE_OMIT_PRAGMA
14428  "OMIT_PRAGMA",
14429#endif
14430#if SQLITE_OMIT_PROGRESS_CALLBACK
14431  "OMIT_PROGRESS_CALLBACK",
14432#endif
14433#if SQLITE_OMIT_QUICKBALANCE
14434  "OMIT_QUICKBALANCE",
14435#endif
14436#if SQLITE_OMIT_REINDEX
14437  "OMIT_REINDEX",
14438#endif
14439#if SQLITE_OMIT_SCHEMA_PRAGMAS
14440  "OMIT_SCHEMA_PRAGMAS",
14441#endif
14442#if SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS
14443  "OMIT_SCHEMA_VERSION_PRAGMAS",
14444#endif
14445#if SQLITE_OMIT_SHARED_CACHE
14446  "OMIT_SHARED_CACHE",
14447#endif
14448#if SQLITE_OMIT_SUBQUERY
14449  "OMIT_SUBQUERY",
14450#endif
14451#if SQLITE_OMIT_TCL_VARIABLE
14452  "OMIT_TCL_VARIABLE",
14453#endif
14454#if SQLITE_OMIT_TEMPDB
14455  "OMIT_TEMPDB",
14456#endif
14457#if SQLITE_OMIT_TRACE
14458  "OMIT_TRACE",
14459#endif
14460#if SQLITE_OMIT_TRIGGER
14461  "OMIT_TRIGGER",
14462#endif
14463#if SQLITE_OMIT_TRUNCATE_OPTIMIZATION
14464  "OMIT_TRUNCATE_OPTIMIZATION",
14465#endif
14466#if SQLITE_OMIT_UTF16
14467  "OMIT_UTF16",
14468#endif
14469#if SQLITE_OMIT_VACUUM
14470  "OMIT_VACUUM",
14471#endif
14472#if SQLITE_OMIT_VIEW
14473  "OMIT_VIEW",
14474#endif
14475#if SQLITE_OMIT_VIRTUALTABLE
14476  "OMIT_VIRTUALTABLE",
14477#endif
14478#if SQLITE_OMIT_WAL
14479  "OMIT_WAL",
14480#endif
14481#if SQLITE_OMIT_WSD
14482  "OMIT_WSD",
14483#endif
14484#if SQLITE_OMIT_XFER_OPT
14485  "OMIT_XFER_OPT",
14486#endif
14487#if SQLITE_PERFORMANCE_TRACE
14488  "PERFORMANCE_TRACE",
14489#endif
14490#if SQLITE_PROXY_DEBUG
14491  "PROXY_DEBUG",
14492#endif
14493#if SQLITE_RTREE_INT_ONLY
14494  "RTREE_INT_ONLY",
14495#endif
14496#if SQLITE_SECURE_DELETE
14497  "SECURE_DELETE",
14498#endif
14499#if SQLITE_SMALL_STACK
14500  "SMALL_STACK",
14501#endif
14502#if SQLITE_SOUNDEX
14503  "SOUNDEX",
14504#endif
14505#if SQLITE_SYSTEM_MALLOC
14506  "SYSTEM_MALLOC",
14507#endif
14508#if SQLITE_TCL
14509  "TCL",
14510#endif
14511#if defined(SQLITE_TEMP_STORE) && !defined(SQLITE_TEMP_STORE_xc)
14512  "TEMP_STORE=" CTIMEOPT_VAL(SQLITE_TEMP_STORE),
14513#endif
14514#if SQLITE_TEST
14515  "TEST",
14516#endif
14517#if defined(SQLITE_THREADSAFE)
14518  "THREADSAFE=" CTIMEOPT_VAL(SQLITE_THREADSAFE),
14519#endif
14520#if SQLITE_USE_ALLOCA
14521  "USE_ALLOCA",
14522#endif
14523#if SQLITE_USER_AUTHENTICATION
14524  "USER_AUTHENTICATION",
14525#endif
14526#if SQLITE_WIN32_MALLOC
14527  "WIN32_MALLOC",
14528#endif
14529#if SQLITE_ZERO_MALLOC
14530  "ZERO_MALLOC"
14531#endif
14532};
14533
14534/*
14535** Given the name of a compile-time option, return true if that option
14536** was used and false if not.
14537**
14538** The name can optionally begin with "SQLITE_" but the "SQLITE_" prefix
14539** is not required for a match.
14540*/
14541SQLITE_API int SQLITE_STDCALL sqlite3_compileoption_used(const char *zOptName){
14542  int i, n;
14543
14544#if SQLITE_ENABLE_API_ARMOR
14545  if( zOptName==0 ){
14546    (void)SQLITE_MISUSE_BKPT;
14547    return 0;
14548  }
14549#endif
14550  if( sqlite3StrNICmp(zOptName, "SQLITE_", 7)==0 ) zOptName += 7;
14551  n = sqlite3Strlen30(zOptName);
14552
14553  /* Since ArraySize(azCompileOpt) is normally in single digits, a
14554  ** linear search is adequate.  No need for a binary search. */
14555  for(i=0; i<ArraySize(azCompileOpt); i++){
14556    if( sqlite3StrNICmp(zOptName, azCompileOpt[i], n)==0
14557     && sqlite3IsIdChar((unsigned char)azCompileOpt[i][n])==0
14558    ){
14559      return 1;
14560    }
14561  }
14562  return 0;
14563}
14564
14565/*
14566** Return the N-th compile-time option string.  If N is out of range,
14567** return a NULL pointer.
14568*/
14569SQLITE_API const char *SQLITE_STDCALL sqlite3_compileoption_get(int N){
14570  if( N>=0 && N<ArraySize(azCompileOpt) ){
14571    return azCompileOpt[N];
14572  }
14573  return 0;
14574}
14575
14576#endif /* SQLITE_OMIT_COMPILEOPTION_DIAGS */
14577
14578/************** End of ctime.c ***********************************************/
14579/************** Begin file status.c ******************************************/
14580/*
14581** 2008 June 18
14582**
14583** The author disclaims copyright to this source code.  In place of
14584** a legal notice, here is a blessing:
14585**
14586**    May you do good and not evil.
14587**    May you find forgiveness for yourself and forgive others.
14588**    May you share freely, never taking more than you give.
14589**
14590*************************************************************************
14591**
14592** This module implements the sqlite3_status() interface and related
14593** functionality.
14594*/
14595/* #include "sqliteInt.h" */
14596/************** Include vdbeInt.h in the middle of status.c ******************/
14597/************** Begin file vdbeInt.h *****************************************/
14598/*
14599** 2003 September 6
14600**
14601** The author disclaims copyright to this source code.  In place of
14602** a legal notice, here is a blessing:
14603**
14604**    May you do good and not evil.
14605**    May you find forgiveness for yourself and forgive others.
14606**    May you share freely, never taking more than you give.
14607**
14608*************************************************************************
14609** This is the header file for information that is private to the
14610** VDBE.  This information used to all be at the top of the single
14611** source code file "vdbe.c".  When that file became too big (over
14612** 6000 lines long) it was split up into several smaller files and
14613** this header information was factored out.
14614*/
14615#ifndef _VDBEINT_H_
14616#define _VDBEINT_H_
14617
14618/*
14619** The maximum number of times that a statement will try to reparse
14620** itself before giving up and returning SQLITE_SCHEMA.
14621*/
14622#ifndef SQLITE_MAX_SCHEMA_RETRY
14623# define SQLITE_MAX_SCHEMA_RETRY 50
14624#endif
14625
14626/*
14627** SQL is translated into a sequence of instructions to be
14628** executed by a virtual machine.  Each instruction is an instance
14629** of the following structure.
14630*/
14631typedef struct VdbeOp Op;
14632
14633/*
14634** Boolean values
14635*/
14636typedef unsigned Bool;
14637
14638/* Opaque type used by code in vdbesort.c */
14639typedef struct VdbeSorter VdbeSorter;
14640
14641/* Opaque type used by the explainer */
14642typedef struct Explain Explain;
14643
14644/* Elements of the linked list at Vdbe.pAuxData */
14645typedef struct AuxData AuxData;
14646
14647/*
14648** A cursor is a pointer into a single BTree within a database file.
14649** The cursor can seek to a BTree entry with a particular key, or
14650** loop over all entries of the Btree.  You can also insert new BTree
14651** entries or retrieve the key or data from the entry that the cursor
14652** is currently pointing to.
14653**
14654** Cursors can also point to virtual tables, sorters, or "pseudo-tables".
14655** A pseudo-table is a single-row table implemented by registers.
14656**
14657** Every cursor that the virtual machine has open is represented by an
14658** instance of the following structure.
14659*/
14660struct VdbeCursor {
14661  BtCursor *pCursor;    /* The cursor structure of the backend */
14662  Btree *pBt;           /* Separate file holding temporary table */
14663  KeyInfo *pKeyInfo;    /* Info about index keys needed by index cursors */
14664  int seekResult;       /* Result of previous sqlite3BtreeMoveto() */
14665  int pseudoTableReg;   /* Register holding pseudotable content. */
14666  i16 nField;           /* Number of fields in the header */
14667  u16 nHdrParsed;       /* Number of header fields parsed so far */
14668#ifdef SQLITE_DEBUG
14669  u8 seekOp;            /* Most recent seek operation on this cursor */
14670#endif
14671  i8 iDb;               /* Index of cursor database in db->aDb[] (or -1) */
14672  u8 nullRow;           /* True if pointing to a row with no data */
14673  u8 deferredMoveto;    /* A call to sqlite3BtreeMoveto() is needed */
14674  Bool isEphemeral:1;   /* True for an ephemeral table */
14675  Bool useRandomRowid:1;/* Generate new record numbers semi-randomly */
14676  Bool isTable:1;       /* True if a table requiring integer keys */
14677  Bool isOrdered:1;     /* True if the underlying table is BTREE_UNORDERED */
14678  Pgno pgnoRoot;        /* Root page of the open btree cursor */
14679  sqlite3_vtab_cursor *pVtabCursor;  /* The cursor for a virtual table */
14680  i64 seqCount;         /* Sequence counter */
14681  i64 movetoTarget;     /* Argument to the deferred sqlite3BtreeMoveto() */
14682  VdbeSorter *pSorter;  /* Sorter object for OP_SorterOpen cursors */
14683#ifdef SQLITE_ENABLE_COLUMN_USED_MASK
14684  u64 maskUsed;         /* Mask of columns used by this cursor */
14685#endif
14686
14687  /* Cached information about the header for the data record that the
14688  ** cursor is currently pointing to.  Only valid if cacheStatus matches
14689  ** Vdbe.cacheCtr.  Vdbe.cacheCtr will never take on the value of
14690  ** CACHE_STALE and so setting cacheStatus=CACHE_STALE guarantees that
14691  ** the cache is out of date.
14692  **
14693  ** aRow might point to (ephemeral) data for the current row, or it might
14694  ** be NULL.
14695  */
14696  u32 cacheStatus;      /* Cache is valid if this matches Vdbe.cacheCtr */
14697  u32 payloadSize;      /* Total number of bytes in the record */
14698  u32 szRow;            /* Byte available in aRow */
14699  u32 iHdrOffset;       /* Offset to next unparsed byte of the header */
14700  const u8 *aRow;       /* Data for the current row, if all on one page */
14701  u32 *aOffset;         /* Pointer to aType[nField] */
14702  u32 aType[1];         /* Type values for all entries in the record */
14703  /* 2*nField extra array elements allocated for aType[], beyond the one
14704  ** static element declared in the structure.  nField total array slots for
14705  ** aType[] and nField+1 array slots for aOffset[] */
14706};
14707typedef struct VdbeCursor VdbeCursor;
14708
14709/*
14710** When a sub-program is executed (OP_Program), a structure of this type
14711** is allocated to store the current value of the program counter, as
14712** well as the current memory cell array and various other frame specific
14713** values stored in the Vdbe struct. When the sub-program is finished,
14714** these values are copied back to the Vdbe from the VdbeFrame structure,
14715** restoring the state of the VM to as it was before the sub-program
14716** began executing.
14717**
14718** The memory for a VdbeFrame object is allocated and managed by a memory
14719** cell in the parent (calling) frame. When the memory cell is deleted or
14720** overwritten, the VdbeFrame object is not freed immediately. Instead, it
14721** is linked into the Vdbe.pDelFrame list. The contents of the Vdbe.pDelFrame
14722** list is deleted when the VM is reset in VdbeHalt(). The reason for doing
14723** this instead of deleting the VdbeFrame immediately is to avoid recursive
14724** calls to sqlite3VdbeMemRelease() when the memory cells belonging to the
14725** child frame are released.
14726**
14727** The currently executing frame is stored in Vdbe.pFrame. Vdbe.pFrame is
14728** set to NULL if the currently executing frame is the main program.
14729*/
14730typedef struct VdbeFrame VdbeFrame;
14731struct VdbeFrame {
14732  Vdbe *v;                /* VM this frame belongs to */
14733  VdbeFrame *pParent;     /* Parent of this frame, or NULL if parent is main */
14734  Op *aOp;                /* Program instructions for parent frame */
14735  i64 *anExec;            /* Event counters from parent frame */
14736  Mem *aMem;              /* Array of memory cells for parent frame */
14737  u8 *aOnceFlag;          /* Array of OP_Once flags for parent frame */
14738  VdbeCursor **apCsr;     /* Array of Vdbe cursors for parent frame */
14739  void *token;            /* Copy of SubProgram.token */
14740  i64 lastRowid;          /* Last insert rowid (sqlite3.lastRowid) */
14741  int nCursor;            /* Number of entries in apCsr */
14742  int pc;                 /* Program Counter in parent (calling) frame */
14743  int nOp;                /* Size of aOp array */
14744  int nMem;               /* Number of entries in aMem */
14745  int nOnceFlag;          /* Number of entries in aOnceFlag */
14746  int nChildMem;          /* Number of memory cells for child frame */
14747  int nChildCsr;          /* Number of cursors for child frame */
14748  int nChange;            /* Statement changes (Vdbe.nChange)     */
14749  int nDbChange;          /* Value of db->nChange */
14750};
14751
14752#define VdbeFrameMem(p) ((Mem *)&((u8 *)p)[ROUND8(sizeof(VdbeFrame))])
14753
14754/*
14755** A value for VdbeCursor.cacheValid that means the cache is always invalid.
14756*/
14757#define CACHE_STALE 0
14758
14759/*
14760** Internally, the vdbe manipulates nearly all SQL values as Mem
14761** structures. Each Mem struct may cache multiple representations (string,
14762** integer etc.) of the same value.
14763*/
14764struct Mem {
14765  union MemValue {
14766    double r;           /* Real value used when MEM_Real is set in flags */
14767    i64 i;              /* Integer value used when MEM_Int is set in flags */
14768    int nZero;          /* Used when bit MEM_Zero is set in flags */
14769    FuncDef *pDef;      /* Used only when flags==MEM_Agg */
14770    RowSet *pRowSet;    /* Used only when flags==MEM_RowSet */
14771    VdbeFrame *pFrame;  /* Used when flags==MEM_Frame */
14772  } u;
14773  u16 flags;          /* Some combination of MEM_Null, MEM_Str, MEM_Dyn, etc. */
14774  u8  enc;            /* SQLITE_UTF8, SQLITE_UTF16BE, SQLITE_UTF16LE */
14775  int n;              /* Number of characters in string value, excluding '\0' */
14776  char *z;            /* String or BLOB value */
14777  /* ShallowCopy only needs to copy the information above */
14778  char *zMalloc;      /* Space to hold MEM_Str or MEM_Blob if szMalloc>0 */
14779  int szMalloc;       /* Size of the zMalloc allocation */
14780  u32 uTemp;          /* Transient storage for serial_type in OP_MakeRecord */
14781  sqlite3 *db;        /* The associated database connection */
14782  void (*xDel)(void*);/* Destructor for Mem.z - only valid if MEM_Dyn */
14783#ifdef SQLITE_DEBUG
14784  Mem *pScopyFrom;    /* This Mem is a shallow copy of pScopyFrom */
14785  void *pFiller;      /* So that sizeof(Mem) is a multiple of 8 */
14786#endif
14787};
14788
14789/*
14790** Size of struct Mem not including the Mem.zMalloc member or anything that
14791** follows.
14792*/
14793#define MEMCELLSIZE offsetof(Mem,zMalloc)
14794
14795/* One or more of the following flags are set to indicate the validOK
14796** representations of the value stored in the Mem struct.
14797**
14798** If the MEM_Null flag is set, then the value is an SQL NULL value.
14799** No other flags may be set in this case.
14800**
14801** If the MEM_Str flag is set then Mem.z points at a string representation.
14802** Usually this is encoded in the same unicode encoding as the main
14803** database (see below for exceptions). If the MEM_Term flag is also
14804** set, then the string is nul terminated. The MEM_Int and MEM_Real
14805** flags may coexist with the MEM_Str flag.
14806*/
14807#define MEM_Null      0x0001   /* Value is NULL */
14808#define MEM_Str       0x0002   /* Value is a string */
14809#define MEM_Int       0x0004   /* Value is an integer */
14810#define MEM_Real      0x0008   /* Value is a real number */
14811#define MEM_Blob      0x0010   /* Value is a BLOB */
14812#define MEM_AffMask   0x001f   /* Mask of affinity bits */
14813#define MEM_RowSet    0x0020   /* Value is a RowSet object */
14814#define MEM_Frame     0x0040   /* Value is a VdbeFrame object */
14815#define MEM_Undefined 0x0080   /* Value is undefined */
14816#define MEM_Cleared   0x0100   /* NULL set by OP_Null, not from data */
14817#define MEM_TypeMask  0x01ff   /* Mask of type bits */
14818
14819
14820/* Whenever Mem contains a valid string or blob representation, one of
14821** the following flags must be set to determine the memory management
14822** policy for Mem.z.  The MEM_Term flag tells us whether or not the
14823** string is \000 or \u0000 terminated
14824*/
14825#define MEM_Term      0x0200   /* String rep is nul terminated */
14826#define MEM_Dyn       0x0400   /* Need to call Mem.xDel() on Mem.z */
14827#define MEM_Static    0x0800   /* Mem.z points to a static string */
14828#define MEM_Ephem     0x1000   /* Mem.z points to an ephemeral string */
14829#define MEM_Agg       0x2000   /* Mem.z points to an agg function context */
14830#define MEM_Zero      0x4000   /* Mem.i contains count of 0s appended to blob */
14831#ifdef SQLITE_OMIT_INCRBLOB
14832  #undef MEM_Zero
14833  #define MEM_Zero 0x0000
14834#endif
14835
14836/*
14837** Clear any existing type flags from a Mem and replace them with f
14838*/
14839#define MemSetTypeFlag(p, f) \
14840   ((p)->flags = ((p)->flags&~(MEM_TypeMask|MEM_Zero))|f)
14841
14842/*
14843** Return true if a memory cell is not marked as invalid.  This macro
14844** is for use inside assert() statements only.
14845*/
14846#ifdef SQLITE_DEBUG
14847#define memIsValid(M)  ((M)->flags & MEM_Undefined)==0
14848#endif
14849
14850/*
14851** Each auxiliary data pointer stored by a user defined function
14852** implementation calling sqlite3_set_auxdata() is stored in an instance
14853** of this structure. All such structures associated with a single VM
14854** are stored in a linked list headed at Vdbe.pAuxData. All are destroyed
14855** when the VM is halted (if not before).
14856*/
14857struct AuxData {
14858  int iOp;                        /* Instruction number of OP_Function opcode */
14859  int iArg;                       /* Index of function argument. */
14860  void *pAux;                     /* Aux data pointer */
14861  void (*xDelete)(void *);        /* Destructor for the aux data */
14862  AuxData *pNext;                 /* Next element in list */
14863};
14864
14865/*
14866** The "context" argument for an installable function.  A pointer to an
14867** instance of this structure is the first argument to the routines used
14868** implement the SQL functions.
14869**
14870** There is a typedef for this structure in sqlite.h.  So all routines,
14871** even the public interface to SQLite, can use a pointer to this structure.
14872** But this file is the only place where the internal details of this
14873** structure are known.
14874**
14875** This structure is defined inside of vdbeInt.h because it uses substructures
14876** (Mem) which are only defined there.
14877*/
14878struct sqlite3_context {
14879  Mem *pOut;              /* The return value is stored here */
14880  FuncDef *pFunc;         /* Pointer to function information */
14881  Mem *pMem;              /* Memory cell used to store aggregate context */
14882  Vdbe *pVdbe;            /* The VM that owns this context */
14883  int iOp;                /* Instruction number of OP_Function */
14884  int isError;            /* Error code returned by the function. */
14885  u8 skipFlag;            /* Skip accumulator loading if true */
14886  u8 fErrorOrAux;         /* isError!=0 or pVdbe->pAuxData modified */
14887  u8 argc;                /* Number of arguments */
14888  sqlite3_value *argv[1]; /* Argument set */
14889};
14890
14891/*
14892** An Explain object accumulates indented output which is helpful
14893** in describing recursive data structures.
14894*/
14895struct Explain {
14896  Vdbe *pVdbe;       /* Attach the explanation to this Vdbe */
14897  StrAccum str;      /* The string being accumulated */
14898  int nIndent;       /* Number of elements in aIndent */
14899  u16 aIndent[100];  /* Levels of indentation */
14900  char zBase[100];   /* Initial space */
14901};
14902
14903/* A bitfield type for use inside of structures.  Always follow with :N where
14904** N is the number of bits.
14905*/
14906typedef unsigned bft;  /* Bit Field Type */
14907
14908typedef struct ScanStatus ScanStatus;
14909struct ScanStatus {
14910  int addrExplain;                /* OP_Explain for loop */
14911  int addrLoop;                   /* Address of "loops" counter */
14912  int addrVisit;                  /* Address of "rows visited" counter */
14913  int iSelectID;                  /* The "Select-ID" for this loop */
14914  LogEst nEst;                    /* Estimated output rows per loop */
14915  char *zName;                    /* Name of table or index */
14916};
14917
14918/*
14919** An instance of the virtual machine.  This structure contains the complete
14920** state of the virtual machine.
14921**
14922** The "sqlite3_stmt" structure pointer that is returned by sqlite3_prepare()
14923** is really a pointer to an instance of this structure.
14924*/
14925struct Vdbe {
14926  sqlite3 *db;            /* The database connection that owns this statement */
14927  Op *aOp;                /* Space to hold the virtual machine's program */
14928  Mem *aMem;              /* The memory locations */
14929  Mem **apArg;            /* Arguments to currently executing user function */
14930  Mem *aColName;          /* Column names to return */
14931  Mem *pResultSet;        /* Pointer to an array of results */
14932  Parse *pParse;          /* Parsing context used to create this Vdbe */
14933  int nMem;               /* Number of memory locations currently allocated */
14934  int nOp;                /* Number of instructions in the program */
14935  int nCursor;            /* Number of slots in apCsr[] */
14936  u32 magic;              /* Magic number for sanity checking */
14937  char *zErrMsg;          /* Error message written here */
14938  Vdbe *pPrev,*pNext;     /* Linked list of VDBEs with the same Vdbe.db */
14939  VdbeCursor **apCsr;     /* One element of this array for each open cursor */
14940  Mem *aVar;              /* Values for the OP_Variable opcode. */
14941  char **azVar;           /* Name of variables */
14942  ynVar nVar;             /* Number of entries in aVar[] */
14943  ynVar nzVar;            /* Number of entries in azVar[] */
14944  u32 cacheCtr;           /* VdbeCursor row cache generation counter */
14945  int pc;                 /* The program counter */
14946  int rc;                 /* Value to return */
14947#ifdef SQLITE_DEBUG
14948  int rcApp;              /* errcode set by sqlite3_result_error_code() */
14949#endif
14950  u16 nResColumn;         /* Number of columns in one row of the result set */
14951  u8 errorAction;         /* Recovery action to do in case of an error */
14952  u8 minWriteFileFormat;  /* Minimum file format for writable database files */
14953  bft explain:2;          /* True if EXPLAIN present on SQL command */
14954  bft changeCntOn:1;      /* True to update the change-counter */
14955  bft expired:1;          /* True if the VM needs to be recompiled */
14956  bft runOnlyOnce:1;      /* Automatically expire on reset */
14957  bft usesStmtJournal:1;  /* True if uses a statement journal */
14958  bft readOnly:1;         /* True for statements that do not write */
14959  bft bIsReader:1;        /* True for statements that read */
14960  bft isPrepareV2:1;      /* True if prepared with prepare_v2() */
14961  bft doingRerun:1;       /* True if rerunning after an auto-reprepare */
14962  int nChange;            /* Number of db changes made since last reset */
14963  yDbMask btreeMask;      /* Bitmask of db->aDb[] entries referenced */
14964  yDbMask lockMask;       /* Subset of btreeMask that requires a lock */
14965  int iStatement;         /* Statement number (or 0 if has not opened stmt) */
14966  u32 aCounter[5];        /* Counters used by sqlite3_stmt_status() */
14967#ifndef SQLITE_OMIT_TRACE
14968  i64 startTime;          /* Time when query started - used for profiling */
14969#endif
14970  i64 iCurrentTime;       /* Value of julianday('now') for this statement */
14971  i64 nFkConstraint;      /* Number of imm. FK constraints this VM */
14972  i64 nStmtDefCons;       /* Number of def. constraints when stmt started */
14973  i64 nStmtDefImmCons;    /* Number of def. imm constraints when stmt started */
14974  char *zSql;             /* Text of the SQL statement that generated this */
14975  void *pFree;            /* Free this when deleting the vdbe */
14976  VdbeFrame *pFrame;      /* Parent frame */
14977  VdbeFrame *pDelFrame;   /* List of frame objects to free on VM reset */
14978  int nFrame;             /* Number of frames in pFrame list */
14979  u32 expmask;            /* Binding to these vars invalidates VM */
14980  SubProgram *pProgram;   /* Linked list of all sub-programs used by VM */
14981  int nOnceFlag;          /* Size of array aOnceFlag[] */
14982  u8 *aOnceFlag;          /* Flags for OP_Once */
14983  AuxData *pAuxData;      /* Linked list of auxdata allocations */
14984#ifdef SQLITE_ENABLE_STMT_SCANSTATUS
14985  i64 *anExec;            /* Number of times each op has been executed */
14986  int nScan;              /* Entries in aScan[] */
14987  ScanStatus *aScan;      /* Scan definitions for sqlite3_stmt_scanstatus() */
14988#endif
14989};
14990
14991/*
14992** The following are allowed values for Vdbe.magic
14993*/
14994#define VDBE_MAGIC_INIT     0x26bceaa5    /* Building a VDBE program */
14995#define VDBE_MAGIC_RUN      0xbdf20da3    /* VDBE is ready to execute */
14996#define VDBE_MAGIC_HALT     0x519c2973    /* VDBE has completed execution */
14997#define VDBE_MAGIC_DEAD     0xb606c3c8    /* The VDBE has been deallocated */
14998
14999/*
15000** Function prototypes
15001*/
15002SQLITE_PRIVATE void sqlite3VdbeError(Vdbe*, const char *, ...);
15003SQLITE_PRIVATE void sqlite3VdbeFreeCursor(Vdbe *, VdbeCursor*);
15004void sqliteVdbePopStack(Vdbe*,int);
15005SQLITE_PRIVATE int sqlite3VdbeCursorMoveto(VdbeCursor*);
15006SQLITE_PRIVATE int sqlite3VdbeCursorRestore(VdbeCursor*);
15007#if defined(SQLITE_DEBUG) || defined(VDBE_PROFILE)
15008SQLITE_PRIVATE void sqlite3VdbePrintOp(FILE*, int, Op*);
15009#endif
15010SQLITE_PRIVATE u32 sqlite3VdbeSerialTypeLen(u32);
15011SQLITE_PRIVATE u32 sqlite3VdbeSerialType(Mem*, int);
15012SQLITE_PRIVATE u32 sqlite3VdbeSerialPut(unsigned char*, Mem*, u32);
15013SQLITE_PRIVATE u32 sqlite3VdbeSerialGet(const unsigned char*, u32, Mem*);
15014SQLITE_PRIVATE void sqlite3VdbeDeleteAuxData(Vdbe*, int, int);
15015
15016int sqlite2BtreeKeyCompare(BtCursor *, const void *, int, int, int *);
15017SQLITE_PRIVATE int sqlite3VdbeIdxKeyCompare(sqlite3*,VdbeCursor*,UnpackedRecord*,int*);
15018SQLITE_PRIVATE int sqlite3VdbeIdxRowid(sqlite3*, BtCursor*, i64*);
15019SQLITE_PRIVATE int sqlite3VdbeExec(Vdbe*);
15020SQLITE_PRIVATE int sqlite3VdbeList(Vdbe*);
15021SQLITE_PRIVATE int sqlite3VdbeHalt(Vdbe*);
15022SQLITE_PRIVATE int sqlite3VdbeChangeEncoding(Mem *, int);
15023SQLITE_PRIVATE int sqlite3VdbeMemTooBig(Mem*);
15024SQLITE_PRIVATE int sqlite3VdbeMemCopy(Mem*, const Mem*);
15025SQLITE_PRIVATE void sqlite3VdbeMemShallowCopy(Mem*, const Mem*, int);
15026SQLITE_PRIVATE void sqlite3VdbeMemMove(Mem*, Mem*);
15027SQLITE_PRIVATE int sqlite3VdbeMemNulTerminate(Mem*);
15028SQLITE_PRIVATE int sqlite3VdbeMemSetStr(Mem*, const char*, int, u8, void(*)(void*));
15029SQLITE_PRIVATE void sqlite3VdbeMemSetInt64(Mem*, i64);
15030#ifdef SQLITE_OMIT_FLOATING_POINT
15031# define sqlite3VdbeMemSetDouble sqlite3VdbeMemSetInt64
15032#else
15033SQLITE_PRIVATE   void sqlite3VdbeMemSetDouble(Mem*, double);
15034#endif
15035SQLITE_PRIVATE void sqlite3VdbeMemInit(Mem*,sqlite3*,u16);
15036SQLITE_PRIVATE void sqlite3VdbeMemSetNull(Mem*);
15037SQLITE_PRIVATE void sqlite3VdbeMemSetZeroBlob(Mem*,int);
15038SQLITE_PRIVATE void sqlite3VdbeMemSetRowSet(Mem*);
15039SQLITE_PRIVATE int sqlite3VdbeMemMakeWriteable(Mem*);
15040SQLITE_PRIVATE int sqlite3VdbeMemStringify(Mem*, u8, u8);
15041SQLITE_PRIVATE i64 sqlite3VdbeIntValue(Mem*);
15042SQLITE_PRIVATE int sqlite3VdbeMemIntegerify(Mem*);
15043SQLITE_PRIVATE double sqlite3VdbeRealValue(Mem*);
15044SQLITE_PRIVATE void sqlite3VdbeIntegerAffinity(Mem*);
15045SQLITE_PRIVATE int sqlite3VdbeMemRealify(Mem*);
15046SQLITE_PRIVATE int sqlite3VdbeMemNumerify(Mem*);
15047SQLITE_PRIVATE void sqlite3VdbeMemCast(Mem*,u8,u8);
15048SQLITE_PRIVATE int sqlite3VdbeMemFromBtree(BtCursor*,u32,u32,int,Mem*);
15049SQLITE_PRIVATE void sqlite3VdbeMemRelease(Mem *p);
15050#define VdbeMemDynamic(X)  \
15051  (((X)->flags&(MEM_Agg|MEM_Dyn|MEM_RowSet|MEM_Frame))!=0)
15052SQLITE_PRIVATE int sqlite3VdbeMemFinalize(Mem*, FuncDef*);
15053SQLITE_PRIVATE const char *sqlite3OpcodeName(int);
15054SQLITE_PRIVATE int sqlite3VdbeMemGrow(Mem *pMem, int n, int preserve);
15055SQLITE_PRIVATE int sqlite3VdbeMemClearAndResize(Mem *pMem, int n);
15056SQLITE_PRIVATE int sqlite3VdbeCloseStatement(Vdbe *, int);
15057SQLITE_PRIVATE void sqlite3VdbeFrameDelete(VdbeFrame*);
15058SQLITE_PRIVATE int sqlite3VdbeFrameRestore(VdbeFrame *);
15059SQLITE_PRIVATE int sqlite3VdbeTransferError(Vdbe *p);
15060
15061SQLITE_PRIVATE int sqlite3VdbeSorterInit(sqlite3 *, int, VdbeCursor *);
15062SQLITE_PRIVATE void sqlite3VdbeSorterReset(sqlite3 *, VdbeSorter *);
15063SQLITE_PRIVATE void sqlite3VdbeSorterClose(sqlite3 *, VdbeCursor *);
15064SQLITE_PRIVATE int sqlite3VdbeSorterRowkey(const VdbeCursor *, Mem *);
15065SQLITE_PRIVATE int sqlite3VdbeSorterNext(sqlite3 *, const VdbeCursor *, int *);
15066SQLITE_PRIVATE int sqlite3VdbeSorterRewind(const VdbeCursor *, int *);
15067SQLITE_PRIVATE int sqlite3VdbeSorterWrite(const VdbeCursor *, Mem *);
15068SQLITE_PRIVATE int sqlite3VdbeSorterCompare(const VdbeCursor *, Mem *, int, int *);
15069
15070#if !defined(SQLITE_OMIT_SHARED_CACHE) && SQLITE_THREADSAFE>0
15071SQLITE_PRIVATE   void sqlite3VdbeEnter(Vdbe*);
15072SQLITE_PRIVATE   void sqlite3VdbeLeave(Vdbe*);
15073#else
15074# define sqlite3VdbeEnter(X)
15075# define sqlite3VdbeLeave(X)
15076#endif
15077
15078#ifdef SQLITE_DEBUG
15079SQLITE_PRIVATE void sqlite3VdbeMemAboutToChange(Vdbe*,Mem*);
15080SQLITE_PRIVATE int sqlite3VdbeCheckMemInvariants(Mem*);
15081#endif
15082
15083#ifndef SQLITE_OMIT_FOREIGN_KEY
15084SQLITE_PRIVATE int sqlite3VdbeCheckFk(Vdbe *, int);
15085#else
15086# define sqlite3VdbeCheckFk(p,i) 0
15087#endif
15088
15089SQLITE_PRIVATE int sqlite3VdbeMemTranslate(Mem*, u8);
15090#ifdef SQLITE_DEBUG
15091SQLITE_PRIVATE   void sqlite3VdbePrintSql(Vdbe*);
15092SQLITE_PRIVATE   void sqlite3VdbeMemPrettyPrint(Mem *pMem, char *zBuf);
15093#endif
15094SQLITE_PRIVATE int sqlite3VdbeMemHandleBom(Mem *pMem);
15095
15096#ifndef SQLITE_OMIT_INCRBLOB
15097SQLITE_PRIVATE   int sqlite3VdbeMemExpandBlob(Mem *);
15098  #define ExpandBlob(P) (((P)->flags&MEM_Zero)?sqlite3VdbeMemExpandBlob(P):0)
15099#else
15100  #define sqlite3VdbeMemExpandBlob(x) SQLITE_OK
15101  #define ExpandBlob(P) SQLITE_OK
15102#endif
15103
15104#endif /* !defined(_VDBEINT_H_) */
15105
15106/************** End of vdbeInt.h *********************************************/
15107/************** Continuing where we left off in status.c *********************/
15108
15109/*
15110** Variables in which to record status information.
15111*/
15112typedef struct sqlite3StatType sqlite3StatType;
15113static SQLITE_WSD struct sqlite3StatType {
15114#if SQLITE_PTRSIZE>4
15115  sqlite3_int64 nowValue[10];         /* Current value */
15116  sqlite3_int64 mxValue[10];          /* Maximum value */
15117#else
15118  u32 nowValue[10];                   /* Current value */
15119  u32 mxValue[10];                    /* Maximum value */
15120#endif
15121} sqlite3Stat = { {0,}, {0,} };
15122
15123/*
15124** Elements of sqlite3Stat[] are protected by either the memory allocator
15125** mutex, or by the pcache1 mutex.  The following array determines which.
15126*/
15127static const char statMutex[] = {
15128  0,  /* SQLITE_STATUS_MEMORY_USED */
15129  1,  /* SQLITE_STATUS_PAGECACHE_USED */
15130  1,  /* SQLITE_STATUS_PAGECACHE_OVERFLOW */
15131  0,  /* SQLITE_STATUS_SCRATCH_USED */
15132  0,  /* SQLITE_STATUS_SCRATCH_OVERFLOW */
15133  0,  /* SQLITE_STATUS_MALLOC_SIZE */
15134  0,  /* SQLITE_STATUS_PARSER_STACK */
15135  1,  /* SQLITE_STATUS_PAGECACHE_SIZE */
15136  0,  /* SQLITE_STATUS_SCRATCH_SIZE */
15137  0,  /* SQLITE_STATUS_MALLOC_COUNT */
15138};
15139
15140
15141/* The "wsdStat" macro will resolve to the status information
15142** state vector.  If writable static data is unsupported on the target,
15143** we have to locate the state vector at run-time.  In the more common
15144** case where writable static data is supported, wsdStat can refer directly
15145** to the "sqlite3Stat" state vector declared above.
15146*/
15147#ifdef SQLITE_OMIT_WSD
15148# define wsdStatInit  sqlite3StatType *x = &GLOBAL(sqlite3StatType,sqlite3Stat)
15149# define wsdStat x[0]
15150#else
15151# define wsdStatInit
15152# define wsdStat sqlite3Stat
15153#endif
15154
15155/*
15156** Return the current value of a status parameter.  The caller must
15157** be holding the appropriate mutex.
15158*/
15159SQLITE_PRIVATE sqlite3_int64 sqlite3StatusValue(int op){
15160  wsdStatInit;
15161  assert( op>=0 && op<ArraySize(wsdStat.nowValue) );
15162  assert( op>=0 && op<ArraySize(statMutex) );
15163  assert( sqlite3_mutex_held(statMutex[op] ? sqlite3Pcache1Mutex()
15164                                           : sqlite3MallocMutex()) );
15165  return wsdStat.nowValue[op];
15166}
15167
15168/*
15169** Add N to the value of a status record.  The caller must hold the
15170** appropriate mutex.  (Locking is checked by assert()).
15171**
15172** The StatusUp() routine can accept positive or negative values for N.
15173** The value of N is added to the current status value and the high-water
15174** mark is adjusted if necessary.
15175**
15176** The StatusDown() routine lowers the current value by N.  The highwater
15177** mark is unchanged.  N must be non-negative for StatusDown().
15178*/
15179SQLITE_PRIVATE void sqlite3StatusUp(int op, int N){
15180  wsdStatInit;
15181  assert( op>=0 && op<ArraySize(wsdStat.nowValue) );
15182  assert( op>=0 && op<ArraySize(statMutex) );
15183  assert( sqlite3_mutex_held(statMutex[op] ? sqlite3Pcache1Mutex()
15184                                           : sqlite3MallocMutex()) );
15185  wsdStat.nowValue[op] += N;
15186  if( wsdStat.nowValue[op]>wsdStat.mxValue[op] ){
15187    wsdStat.mxValue[op] = wsdStat.nowValue[op];
15188  }
15189}
15190SQLITE_PRIVATE void sqlite3StatusDown(int op, int N){
15191  wsdStatInit;
15192  assert( N>=0 );
15193  assert( op>=0 && op<ArraySize(statMutex) );
15194  assert( sqlite3_mutex_held(statMutex[op] ? sqlite3Pcache1Mutex()
15195                                           : sqlite3MallocMutex()) );
15196  assert( op>=0 && op<ArraySize(wsdStat.nowValue) );
15197  wsdStat.nowValue[op] -= N;
15198}
15199
15200/*
15201** Set the value of a status to X.  The highwater mark is adjusted if
15202** necessary.  The caller must hold the appropriate mutex.
15203*/
15204SQLITE_PRIVATE void sqlite3StatusSet(int op, int X){
15205  wsdStatInit;
15206  assert( op>=0 && op<ArraySize(wsdStat.nowValue) );
15207  assert( op>=0 && op<ArraySize(statMutex) );
15208  assert( sqlite3_mutex_held(statMutex[op] ? sqlite3Pcache1Mutex()
15209                                           : sqlite3MallocMutex()) );
15210  wsdStat.nowValue[op] = X;
15211  if( wsdStat.nowValue[op]>wsdStat.mxValue[op] ){
15212    wsdStat.mxValue[op] = wsdStat.nowValue[op];
15213  }
15214}
15215
15216/*
15217** Query status information.
15218*/
15219SQLITE_API int SQLITE_STDCALL sqlite3_status64(
15220  int op,
15221  sqlite3_int64 *pCurrent,
15222  sqlite3_int64 *pHighwater,
15223  int resetFlag
15224){
15225  sqlite3_mutex *pMutex;
15226  wsdStatInit;
15227  if( op<0 || op>=ArraySize(wsdStat.nowValue) ){
15228    return SQLITE_MISUSE_BKPT;
15229  }
15230#ifdef SQLITE_ENABLE_API_ARMOR
15231  if( pCurrent==0 || pHighwater==0 ) return SQLITE_MISUSE_BKPT;
15232#endif
15233  pMutex = statMutex[op] ? sqlite3Pcache1Mutex() : sqlite3MallocMutex();
15234  sqlite3_mutex_enter(pMutex);
15235  *pCurrent = wsdStat.nowValue[op];
15236  *pHighwater = wsdStat.mxValue[op];
15237  if( resetFlag ){
15238    wsdStat.mxValue[op] = wsdStat.nowValue[op];
15239  }
15240  sqlite3_mutex_leave(pMutex);
15241  (void)pMutex;  /* Prevent warning when SQLITE_THREADSAFE=0 */
15242  return SQLITE_OK;
15243}
15244SQLITE_API int SQLITE_STDCALL sqlite3_status(int op, int *pCurrent, int *pHighwater, int resetFlag){
15245  sqlite3_int64 iCur, iHwtr;
15246  int rc;
15247#ifdef SQLITE_ENABLE_API_ARMOR
15248  if( pCurrent==0 || pHighwater==0 ) return SQLITE_MISUSE_BKPT;
15249#endif
15250  rc = sqlite3_status64(op, &iCur, &iHwtr, resetFlag);
15251  if( rc==0 ){
15252    *pCurrent = (int)iCur;
15253    *pHighwater = (int)iHwtr;
15254  }
15255  return rc;
15256}
15257
15258/*
15259** Query status information for a single database connection
15260*/
15261SQLITE_API int SQLITE_STDCALL sqlite3_db_status(
15262  sqlite3 *db,          /* The database connection whose status is desired */
15263  int op,               /* Status verb */
15264  int *pCurrent,        /* Write current value here */
15265  int *pHighwater,      /* Write high-water mark here */
15266  int resetFlag         /* Reset high-water mark if true */
15267){
15268  int rc = SQLITE_OK;   /* Return code */
15269#ifdef SQLITE_ENABLE_API_ARMOR
15270  if( !sqlite3SafetyCheckOk(db) || pCurrent==0|| pHighwater==0 ){
15271    return SQLITE_MISUSE_BKPT;
15272  }
15273#endif
15274  sqlite3_mutex_enter(db->mutex);
15275  switch( op ){
15276    case SQLITE_DBSTATUS_LOOKASIDE_USED: {
15277      *pCurrent = db->lookaside.nOut;
15278      *pHighwater = db->lookaside.mxOut;
15279      if( resetFlag ){
15280        db->lookaside.mxOut = db->lookaside.nOut;
15281      }
15282      break;
15283    }
15284
15285    case SQLITE_DBSTATUS_LOOKASIDE_HIT:
15286    case SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE:
15287    case SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL: {
15288      testcase( op==SQLITE_DBSTATUS_LOOKASIDE_HIT );
15289      testcase( op==SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE );
15290      testcase( op==SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL );
15291      assert( (op-SQLITE_DBSTATUS_LOOKASIDE_HIT)>=0 );
15292      assert( (op-SQLITE_DBSTATUS_LOOKASIDE_HIT)<3 );
15293      *pCurrent = 0;
15294      *pHighwater = db->lookaside.anStat[op - SQLITE_DBSTATUS_LOOKASIDE_HIT];
15295      if( resetFlag ){
15296        db->lookaside.anStat[op - SQLITE_DBSTATUS_LOOKASIDE_HIT] = 0;
15297      }
15298      break;
15299    }
15300
15301    /*
15302    ** Return an approximation for the amount of memory currently used
15303    ** by all pagers associated with the given database connection.  The
15304    ** highwater mark is meaningless and is returned as zero.
15305    */
15306    case SQLITE_DBSTATUS_CACHE_USED: {
15307      int totalUsed = 0;
15308      int i;
15309      sqlite3BtreeEnterAll(db);
15310      for(i=0; i<db->nDb; i++){
15311        Btree *pBt = db->aDb[i].pBt;
15312        if( pBt ){
15313          Pager *pPager = sqlite3BtreePager(pBt);
15314          totalUsed += sqlite3PagerMemUsed(pPager);
15315        }
15316      }
15317      sqlite3BtreeLeaveAll(db);
15318      *pCurrent = totalUsed;
15319      *pHighwater = 0;
15320      break;
15321    }
15322
15323    /*
15324    ** *pCurrent gets an accurate estimate of the amount of memory used
15325    ** to store the schema for all databases (main, temp, and any ATTACHed
15326    ** databases.  *pHighwater is set to zero.
15327    */
15328    case SQLITE_DBSTATUS_SCHEMA_USED: {
15329      int i;                      /* Used to iterate through schemas */
15330      int nByte = 0;              /* Used to accumulate return value */
15331
15332      sqlite3BtreeEnterAll(db);
15333      db->pnBytesFreed = &nByte;
15334      for(i=0; i<db->nDb; i++){
15335        Schema *pSchema = db->aDb[i].pSchema;
15336        if( ALWAYS(pSchema!=0) ){
15337          HashElem *p;
15338
15339          nByte += sqlite3GlobalConfig.m.xRoundup(sizeof(HashElem)) * (
15340              pSchema->tblHash.count
15341            + pSchema->trigHash.count
15342            + pSchema->idxHash.count
15343            + pSchema->fkeyHash.count
15344          );
15345          nByte += sqlite3MallocSize(pSchema->tblHash.ht);
15346          nByte += sqlite3MallocSize(pSchema->trigHash.ht);
15347          nByte += sqlite3MallocSize(pSchema->idxHash.ht);
15348          nByte += sqlite3MallocSize(pSchema->fkeyHash.ht);
15349
15350          for(p=sqliteHashFirst(&pSchema->trigHash); p; p=sqliteHashNext(p)){
15351            sqlite3DeleteTrigger(db, (Trigger*)sqliteHashData(p));
15352          }
15353          for(p=sqliteHashFirst(&pSchema->tblHash); p; p=sqliteHashNext(p)){
15354            sqlite3DeleteTable(db, (Table *)sqliteHashData(p));
15355          }
15356        }
15357      }
15358      db->pnBytesFreed = 0;
15359      sqlite3BtreeLeaveAll(db);
15360
15361      *pHighwater = 0;
15362      *pCurrent = nByte;
15363      break;
15364    }
15365
15366    /*
15367    ** *pCurrent gets an accurate estimate of the amount of memory used
15368    ** to store all prepared statements.
15369    ** *pHighwater is set to zero.
15370    */
15371    case SQLITE_DBSTATUS_STMT_USED: {
15372      struct Vdbe *pVdbe;         /* Used to iterate through VMs */
15373      int nByte = 0;              /* Used to accumulate return value */
15374
15375      db->pnBytesFreed = &nByte;
15376      for(pVdbe=db->pVdbe; pVdbe; pVdbe=pVdbe->pNext){
15377        sqlite3VdbeClearObject(db, pVdbe);
15378        sqlite3DbFree(db, pVdbe);
15379      }
15380      db->pnBytesFreed = 0;
15381
15382      *pHighwater = 0;  /* IMP: R-64479-57858 */
15383      *pCurrent = nByte;
15384
15385      break;
15386    }
15387
15388    /*
15389    ** Set *pCurrent to the total cache hits or misses encountered by all
15390    ** pagers the database handle is connected to. *pHighwater is always set
15391    ** to zero.
15392    */
15393    case SQLITE_DBSTATUS_CACHE_HIT:
15394    case SQLITE_DBSTATUS_CACHE_MISS:
15395    case SQLITE_DBSTATUS_CACHE_WRITE:{
15396      int i;
15397      int nRet = 0;
15398      assert( SQLITE_DBSTATUS_CACHE_MISS==SQLITE_DBSTATUS_CACHE_HIT+1 );
15399      assert( SQLITE_DBSTATUS_CACHE_WRITE==SQLITE_DBSTATUS_CACHE_HIT+2 );
15400
15401      for(i=0; i<db->nDb; i++){
15402        if( db->aDb[i].pBt ){
15403          Pager *pPager = sqlite3BtreePager(db->aDb[i].pBt);
15404          sqlite3PagerCacheStat(pPager, op, resetFlag, &nRet);
15405        }
15406      }
15407      *pHighwater = 0; /* IMP: R-42420-56072 */
15408                       /* IMP: R-54100-20147 */
15409                       /* IMP: R-29431-39229 */
15410      *pCurrent = nRet;
15411      break;
15412    }
15413
15414    /* Set *pCurrent to non-zero if there are unresolved deferred foreign
15415    ** key constraints.  Set *pCurrent to zero if all foreign key constraints
15416    ** have been satisfied.  The *pHighwater is always set to zero.
15417    */
15418    case SQLITE_DBSTATUS_DEFERRED_FKS: {
15419      *pHighwater = 0;  /* IMP: R-11967-56545 */
15420      *pCurrent = db->nDeferredImmCons>0 || db->nDeferredCons>0;
15421      break;
15422    }
15423
15424    default: {
15425      rc = SQLITE_ERROR;
15426    }
15427  }
15428  sqlite3_mutex_leave(db->mutex);
15429  return rc;
15430}
15431
15432/************** End of status.c **********************************************/
15433/************** Begin file date.c ********************************************/
15434/*
15435** 2003 October 31
15436**
15437** The author disclaims copyright to this source code.  In place of
15438** a legal notice, here is a blessing:
15439**
15440**    May you do good and not evil.
15441**    May you find forgiveness for yourself and forgive others.
15442**    May you share freely, never taking more than you give.
15443**
15444*************************************************************************
15445** This file contains the C functions that implement date and time
15446** functions for SQLite.
15447**
15448** There is only one exported symbol in this file - the function
15449** sqlite3RegisterDateTimeFunctions() found at the bottom of the file.
15450** All other code has file scope.
15451**
15452** SQLite processes all times and dates as julian day numbers.  The
15453** dates and times are stored as the number of days since noon
15454** in Greenwich on November 24, 4714 B.C. according to the Gregorian
15455** calendar system.
15456**
15457** 1970-01-01 00:00:00 is JD 2440587.5
15458** 2000-01-01 00:00:00 is JD 2451544.5
15459**
15460** This implementation requires years to be expressed as a 4-digit number
15461** which means that only dates between 0000-01-01 and 9999-12-31 can
15462** be represented, even though julian day numbers allow a much wider
15463** range of dates.
15464**
15465** The Gregorian calendar system is used for all dates and times,
15466** even those that predate the Gregorian calendar.  Historians usually
15467** use the julian calendar for dates prior to 1582-10-15 and for some
15468** dates afterwards, depending on locale.  Beware of this difference.
15469**
15470** The conversion algorithms are implemented based on descriptions
15471** in the following text:
15472**
15473**      Jean Meeus
15474**      Astronomical Algorithms, 2nd Edition, 1998
15475**      ISBM 0-943396-61-1
15476**      Willmann-Bell, Inc
15477**      Richmond, Virginia (USA)
15478*/
15479/* #include "sqliteInt.h" */
15480/* #include <stdlib.h> */
15481/* #include <assert.h> */
15482#include <time.h>
15483
15484#ifndef SQLITE_OMIT_DATETIME_FUNCS
15485
15486
15487/*
15488** A structure for holding a single date and time.
15489*/
15490typedef struct DateTime DateTime;
15491struct DateTime {
15492  sqlite3_int64 iJD; /* The julian day number times 86400000 */
15493  int Y, M, D;       /* Year, month, and day */
15494  int h, m;          /* Hour and minutes */
15495  int tz;            /* Timezone offset in minutes */
15496  double s;          /* Seconds */
15497  char validYMD;     /* True (1) if Y,M,D are valid */
15498  char validHMS;     /* True (1) if h,m,s are valid */
15499  char validJD;      /* True (1) if iJD is valid */
15500  char validTZ;      /* True (1) if tz is valid */
15501};
15502
15503
15504/*
15505** Convert zDate into one or more integers.  Additional arguments
15506** come in groups of 5 as follows:
15507**
15508**       N       number of digits in the integer
15509**       min     minimum allowed value of the integer
15510**       max     maximum allowed value of the integer
15511**       nextC   first character after the integer
15512**       pVal    where to write the integers value.
15513**
15514** Conversions continue until one with nextC==0 is encountered.
15515** The function returns the number of successful conversions.
15516*/
15517static int getDigits(const char *zDate, ...){
15518  va_list ap;
15519  int val;
15520  int N;
15521  int min;
15522  int max;
15523  int nextC;
15524  int *pVal;
15525  int cnt = 0;
15526  va_start(ap, zDate);
15527  do{
15528    N = va_arg(ap, int);
15529    min = va_arg(ap, int);
15530    max = va_arg(ap, int);
15531    nextC = va_arg(ap, int);
15532    pVal = va_arg(ap, int*);
15533    val = 0;
15534    while( N-- ){
15535      if( !sqlite3Isdigit(*zDate) ){
15536        goto end_getDigits;
15537      }
15538      val = val*10 + *zDate - '0';
15539      zDate++;
15540    }
15541    if( val<min || val>max || (nextC!=0 && nextC!=*zDate) ){
15542      goto end_getDigits;
15543    }
15544    *pVal = val;
15545    zDate++;
15546    cnt++;
15547  }while( nextC );
15548end_getDigits:
15549  va_end(ap);
15550  return cnt;
15551}
15552
15553/*
15554** Parse a timezone extension on the end of a date-time.
15555** The extension is of the form:
15556**
15557**        (+/-)HH:MM
15558**
15559** Or the "zulu" notation:
15560**
15561**        Z
15562**
15563** If the parse is successful, write the number of minutes
15564** of change in p->tz and return 0.  If a parser error occurs,
15565** return non-zero.
15566**
15567** A missing specifier is not considered an error.
15568*/
15569static int parseTimezone(const char *zDate, DateTime *p){
15570  int sgn = 0;
15571  int nHr, nMn;
15572  int c;
15573  while( sqlite3Isspace(*zDate) ){ zDate++; }
15574  p->tz = 0;
15575  c = *zDate;
15576  if( c=='-' ){
15577    sgn = -1;
15578  }else if( c=='+' ){
15579    sgn = +1;
15580  }else if( c=='Z' || c=='z' ){
15581    zDate++;
15582    goto zulu_time;
15583  }else{
15584    return c!=0;
15585  }
15586  zDate++;
15587  if( getDigits(zDate, 2, 0, 14, ':', &nHr, 2, 0, 59, 0, &nMn)!=2 ){
15588    return 1;
15589  }
15590  zDate += 5;
15591  p->tz = sgn*(nMn + nHr*60);
15592zulu_time:
15593  while( sqlite3Isspace(*zDate) ){ zDate++; }
15594  return *zDate!=0;
15595}
15596
15597/*
15598** Parse times of the form HH:MM or HH:MM:SS or HH:MM:SS.FFFF.
15599** The HH, MM, and SS must each be exactly 2 digits.  The
15600** fractional seconds FFFF can be one or more digits.
15601**
15602** Return 1 if there is a parsing error and 0 on success.
15603*/
15604static int parseHhMmSs(const char *zDate, DateTime *p){
15605  int h, m, s;
15606  double ms = 0.0;
15607  if( getDigits(zDate, 2, 0, 24, ':', &h, 2, 0, 59, 0, &m)!=2 ){
15608    return 1;
15609  }
15610  zDate += 5;
15611  if( *zDate==':' ){
15612    zDate++;
15613    if( getDigits(zDate, 2, 0, 59, 0, &s)!=1 ){
15614      return 1;
15615    }
15616    zDate += 2;
15617    if( *zDate=='.' && sqlite3Isdigit(zDate[1]) ){
15618      double rScale = 1.0;
15619      zDate++;
15620      while( sqlite3Isdigit(*zDate) ){
15621        ms = ms*10.0 + *zDate - '0';
15622        rScale *= 10.0;
15623        zDate++;
15624      }
15625      ms /= rScale;
15626    }
15627  }else{
15628    s = 0;
15629  }
15630  p->validJD = 0;
15631  p->validHMS = 1;
15632  p->h = h;
15633  p->m = m;
15634  p->s = s + ms;
15635  if( parseTimezone(zDate, p) ) return 1;
15636  p->validTZ = (p->tz!=0)?1:0;
15637  return 0;
15638}
15639
15640/*
15641** Convert from YYYY-MM-DD HH:MM:SS to julian day.  We always assume
15642** that the YYYY-MM-DD is according to the Gregorian calendar.
15643**
15644** Reference:  Meeus page 61
15645*/
15646static void computeJD(DateTime *p){
15647  int Y, M, D, A, B, X1, X2;
15648
15649  if( p->validJD ) return;
15650  if( p->validYMD ){
15651    Y = p->Y;
15652    M = p->M;
15653    D = p->D;
15654  }else{
15655    Y = 2000;  /* If no YMD specified, assume 2000-Jan-01 */
15656    M = 1;
15657    D = 1;
15658  }
15659  if( M<=2 ){
15660    Y--;
15661    M += 12;
15662  }
15663  A = Y/100;
15664  B = 2 - A + (A/4);
15665  X1 = 36525*(Y+4716)/100;
15666  X2 = 306001*(M+1)/10000;
15667  p->iJD = (sqlite3_int64)((X1 + X2 + D + B - 1524.5 ) * 86400000);
15668  p->validJD = 1;
15669  if( p->validHMS ){
15670    p->iJD += p->h*3600000 + p->m*60000 + (sqlite3_int64)(p->s*1000);
15671    if( p->validTZ ){
15672      p->iJD -= p->tz*60000;
15673      p->validYMD = 0;
15674      p->validHMS = 0;
15675      p->validTZ = 0;
15676    }
15677  }
15678}
15679
15680/*
15681** Parse dates of the form
15682**
15683**     YYYY-MM-DD HH:MM:SS.FFF
15684**     YYYY-MM-DD HH:MM:SS
15685**     YYYY-MM-DD HH:MM
15686**     YYYY-MM-DD
15687**
15688** Write the result into the DateTime structure and return 0
15689** on success and 1 if the input string is not a well-formed
15690** date.
15691*/
15692static int parseYyyyMmDd(const char *zDate, DateTime *p){
15693  int Y, M, D, neg;
15694
15695  if( zDate[0]=='-' ){
15696    zDate++;
15697    neg = 1;
15698  }else{
15699    neg = 0;
15700  }
15701  if( getDigits(zDate,4,0,9999,'-',&Y,2,1,12,'-',&M,2,1,31,0,&D)!=3 ){
15702    return 1;
15703  }
15704  zDate += 10;
15705  while( sqlite3Isspace(*zDate) || 'T'==*(u8*)zDate ){ zDate++; }
15706  if( parseHhMmSs(zDate, p)==0 ){
15707    /* We got the time */
15708  }else if( *zDate==0 ){
15709    p->validHMS = 0;
15710  }else{
15711    return 1;
15712  }
15713  p->validJD = 0;
15714  p->validYMD = 1;
15715  p->Y = neg ? -Y : Y;
15716  p->M = M;
15717  p->D = D;
15718  if( p->validTZ ){
15719    computeJD(p);
15720  }
15721  return 0;
15722}
15723
15724/*
15725** Set the time to the current time reported by the VFS.
15726**
15727** Return the number of errors.
15728*/
15729static int setDateTimeToCurrent(sqlite3_context *context, DateTime *p){
15730  p->iJD = sqlite3StmtCurrentTime(context);
15731  if( p->iJD>0 ){
15732    p->validJD = 1;
15733    return 0;
15734  }else{
15735    return 1;
15736  }
15737}
15738
15739/*
15740** Attempt to parse the given string into a julian day number.  Return
15741** the number of errors.
15742**
15743** The following are acceptable forms for the input string:
15744**
15745**      YYYY-MM-DD HH:MM:SS.FFF  +/-HH:MM
15746**      DDDD.DD
15747**      now
15748**
15749** In the first form, the +/-HH:MM is always optional.  The fractional
15750** seconds extension (the ".FFF") is optional.  The seconds portion
15751** (":SS.FFF") is option.  The year and date can be omitted as long
15752** as there is a time string.  The time string can be omitted as long
15753** as there is a year and date.
15754*/
15755static int parseDateOrTime(
15756  sqlite3_context *context,
15757  const char *zDate,
15758  DateTime *p
15759){
15760  double r;
15761  if( parseYyyyMmDd(zDate,p)==0 ){
15762    return 0;
15763  }else if( parseHhMmSs(zDate, p)==0 ){
15764    return 0;
15765  }else if( sqlite3StrICmp(zDate,"now")==0){
15766    return setDateTimeToCurrent(context, p);
15767  }else if( sqlite3AtoF(zDate, &r, sqlite3Strlen30(zDate), SQLITE_UTF8) ){
15768    p->iJD = (sqlite3_int64)(r*86400000.0 + 0.5);
15769    p->validJD = 1;
15770    return 0;
15771  }
15772  return 1;
15773}
15774
15775/*
15776** Compute the Year, Month, and Day from the julian day number.
15777*/
15778static void computeYMD(DateTime *p){
15779  int Z, A, B, C, D, E, X1;
15780  if( p->validYMD ) return;
15781  if( !p->validJD ){
15782    p->Y = 2000;
15783    p->M = 1;
15784    p->D = 1;
15785  }else{
15786    Z = (int)((p->iJD + 43200000)/86400000);
15787    A = (int)((Z - 1867216.25)/36524.25);
15788    A = Z + 1 + A - (A/4);
15789    B = A + 1524;
15790    C = (int)((B - 122.1)/365.25);
15791    D = (36525*(C&32767))/100;
15792    E = (int)((B-D)/30.6001);
15793    X1 = (int)(30.6001*E);
15794    p->D = B - D - X1;
15795    p->M = E<14 ? E-1 : E-13;
15796    p->Y = p->M>2 ? C - 4716 : C - 4715;
15797  }
15798  p->validYMD = 1;
15799}
15800
15801/*
15802** Compute the Hour, Minute, and Seconds from the julian day number.
15803*/
15804static void computeHMS(DateTime *p){
15805  int s;
15806  if( p->validHMS ) return;
15807  computeJD(p);
15808  s = (int)((p->iJD + 43200000) % 86400000);
15809  p->s = s/1000.0;
15810  s = (int)p->s;
15811  p->s -= s;
15812  p->h = s/3600;
15813  s -= p->h*3600;
15814  p->m = s/60;
15815  p->s += s - p->m*60;
15816  p->validHMS = 1;
15817}
15818
15819/*
15820** Compute both YMD and HMS
15821*/
15822static void computeYMD_HMS(DateTime *p){
15823  computeYMD(p);
15824  computeHMS(p);
15825}
15826
15827/*
15828** Clear the YMD and HMS and the TZ
15829*/
15830static void clearYMD_HMS_TZ(DateTime *p){
15831  p->validYMD = 0;
15832  p->validHMS = 0;
15833  p->validTZ = 0;
15834}
15835
15836/*
15837** On recent Windows platforms, the localtime_s() function is available
15838** as part of the "Secure CRT". It is essentially equivalent to
15839** localtime_r() available under most POSIX platforms, except that the
15840** order of the parameters is reversed.
15841**
15842** See http://msdn.microsoft.com/en-us/library/a442x3ye(VS.80).aspx.
15843**
15844** If the user has not indicated to use localtime_r() or localtime_s()
15845** already, check for an MSVC build environment that provides
15846** localtime_s().
15847*/
15848#if !HAVE_LOCALTIME_R && !HAVE_LOCALTIME_S \
15849    && defined(_MSC_VER) && defined(_CRT_INSECURE_DEPRECATE)
15850#undef  HAVE_LOCALTIME_S
15851#define HAVE_LOCALTIME_S 1
15852#endif
15853
15854#ifndef SQLITE_OMIT_LOCALTIME
15855/*
15856** The following routine implements the rough equivalent of localtime_r()
15857** using whatever operating-system specific localtime facility that
15858** is available.  This routine returns 0 on success and
15859** non-zero on any kind of error.
15860**
15861** If the sqlite3GlobalConfig.bLocaltimeFault variable is true then this
15862** routine will always fail.
15863**
15864** EVIDENCE-OF: R-62172-00036 In this implementation, the standard C
15865** library function localtime_r() is used to assist in the calculation of
15866** local time.
15867*/
15868static int osLocaltime(time_t *t, struct tm *pTm){
15869  int rc;
15870#if !HAVE_LOCALTIME_R && !HAVE_LOCALTIME_S
15871  struct tm *pX;
15872#if SQLITE_THREADSAFE>0
15873  sqlite3_mutex *mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);
15874#endif
15875  sqlite3_mutex_enter(mutex);
15876  pX = localtime(t);
15877#ifndef SQLITE_OMIT_BUILTIN_TEST
15878  if( sqlite3GlobalConfig.bLocaltimeFault ) pX = 0;
15879#endif
15880  if( pX ) *pTm = *pX;
15881  sqlite3_mutex_leave(mutex);
15882  rc = pX==0;
15883#else
15884#ifndef SQLITE_OMIT_BUILTIN_TEST
15885  if( sqlite3GlobalConfig.bLocaltimeFault ) return 1;
15886#endif
15887#if HAVE_LOCALTIME_R
15888  rc = localtime_r(t, pTm)==0;
15889#else
15890  rc = localtime_s(pTm, t);
15891#endif /* HAVE_LOCALTIME_R */
15892#endif /* HAVE_LOCALTIME_R || HAVE_LOCALTIME_S */
15893  return rc;
15894}
15895#endif /* SQLITE_OMIT_LOCALTIME */
15896
15897
15898#ifndef SQLITE_OMIT_LOCALTIME
15899/*
15900** Compute the difference (in milliseconds) between localtime and UTC
15901** (a.k.a. GMT) for the time value p where p is in UTC. If no error occurs,
15902** return this value and set *pRc to SQLITE_OK.
15903**
15904** Or, if an error does occur, set *pRc to SQLITE_ERROR. The returned value
15905** is undefined in this case.
15906*/
15907static sqlite3_int64 localtimeOffset(
15908  DateTime *p,                    /* Date at which to calculate offset */
15909  sqlite3_context *pCtx,          /* Write error here if one occurs */
15910  int *pRc                        /* OUT: Error code. SQLITE_OK or ERROR */
15911){
15912  DateTime x, y;
15913  time_t t;
15914  struct tm sLocal;
15915
15916  /* Initialize the contents of sLocal to avoid a compiler warning. */
15917  memset(&sLocal, 0, sizeof(sLocal));
15918
15919  x = *p;
15920  computeYMD_HMS(&x);
15921  if( x.Y<1971 || x.Y>=2038 ){
15922    /* EVIDENCE-OF: R-55269-29598 The localtime_r() C function normally only
15923    ** works for years between 1970 and 2037. For dates outside this range,
15924    ** SQLite attempts to map the year into an equivalent year within this
15925    ** range, do the calculation, then map the year back.
15926    */
15927    x.Y = 2000;
15928    x.M = 1;
15929    x.D = 1;
15930    x.h = 0;
15931    x.m = 0;
15932    x.s = 0.0;
15933  } else {
15934    int s = (int)(x.s + 0.5);
15935    x.s = s;
15936  }
15937  x.tz = 0;
15938  x.validJD = 0;
15939  computeJD(&x);
15940  t = (time_t)(x.iJD/1000 - 21086676*(i64)10000);
15941  if( osLocaltime(&t, &sLocal) ){
15942    sqlite3_result_error(pCtx, "local time unavailable", -1);
15943    *pRc = SQLITE_ERROR;
15944    return 0;
15945  }
15946  y.Y = sLocal.tm_year + 1900;
15947  y.M = sLocal.tm_mon + 1;
15948  y.D = sLocal.tm_mday;
15949  y.h = sLocal.tm_hour;
15950  y.m = sLocal.tm_min;
15951  y.s = sLocal.tm_sec;
15952  y.validYMD = 1;
15953  y.validHMS = 1;
15954  y.validJD = 0;
15955  y.validTZ = 0;
15956  computeJD(&y);
15957  *pRc = SQLITE_OK;
15958  return y.iJD - x.iJD;
15959}
15960#endif /* SQLITE_OMIT_LOCALTIME */
15961
15962/*
15963** Process a modifier to a date-time stamp.  The modifiers are
15964** as follows:
15965**
15966**     NNN days
15967**     NNN hours
15968**     NNN minutes
15969**     NNN.NNNN seconds
15970**     NNN months
15971**     NNN years
15972**     start of month
15973**     start of year
15974**     start of week
15975**     start of day
15976**     weekday N
15977**     unixepoch
15978**     localtime
15979**     utc
15980**
15981** Return 0 on success and 1 if there is any kind of error. If the error
15982** is in a system call (i.e. localtime()), then an error message is written
15983** to context pCtx. If the error is an unrecognized modifier, no error is
15984** written to pCtx.
15985*/
15986static int parseModifier(sqlite3_context *pCtx, const char *zMod, DateTime *p){
15987  int rc = 1;
15988  int n;
15989  double r;
15990  char *z, zBuf[30];
15991  z = zBuf;
15992  for(n=0; n<ArraySize(zBuf)-1 && zMod[n]; n++){
15993    z[n] = (char)sqlite3UpperToLower[(u8)zMod[n]];
15994  }
15995  z[n] = 0;
15996  switch( z[0] ){
15997#ifndef SQLITE_OMIT_LOCALTIME
15998    case 'l': {
15999      /*    localtime
16000      **
16001      ** Assuming the current time value is UTC (a.k.a. GMT), shift it to
16002      ** show local time.
16003      */
16004      if( strcmp(z, "localtime")==0 ){
16005        computeJD(p);
16006        p->iJD += localtimeOffset(p, pCtx, &rc);
16007        clearYMD_HMS_TZ(p);
16008      }
16009      break;
16010    }
16011#endif
16012    case 'u': {
16013      /*
16014      **    unixepoch
16015      **
16016      ** Treat the current value of p->iJD as the number of
16017      ** seconds since 1970.  Convert to a real julian day number.
16018      */
16019      if( strcmp(z, "unixepoch")==0 && p->validJD ){
16020        p->iJD = (p->iJD + 43200)/86400 + 21086676*(i64)10000000;
16021        clearYMD_HMS_TZ(p);
16022        rc = 0;
16023      }
16024#ifndef SQLITE_OMIT_LOCALTIME
16025      else if( strcmp(z, "utc")==0 ){
16026        sqlite3_int64 c1;
16027        computeJD(p);
16028        c1 = localtimeOffset(p, pCtx, &rc);
16029        if( rc==SQLITE_OK ){
16030          p->iJD -= c1;
16031          clearYMD_HMS_TZ(p);
16032          p->iJD += c1 - localtimeOffset(p, pCtx, &rc);
16033        }
16034      }
16035#endif
16036      break;
16037    }
16038    case 'w': {
16039      /*
16040      **    weekday N
16041      **
16042      ** Move the date to the same time on the next occurrence of
16043      ** weekday N where 0==Sunday, 1==Monday, and so forth.  If the
16044      ** date is already on the appropriate weekday, this is a no-op.
16045      */
16046      if( strncmp(z, "weekday ", 8)==0
16047               && sqlite3AtoF(&z[8], &r, sqlite3Strlen30(&z[8]), SQLITE_UTF8)
16048               && (n=(int)r)==r && n>=0 && r<7 ){
16049        sqlite3_int64 Z;
16050        computeYMD_HMS(p);
16051        p->validTZ = 0;
16052        p->validJD = 0;
16053        computeJD(p);
16054        Z = ((p->iJD + 129600000)/86400000) % 7;
16055        if( Z>n ) Z -= 7;
16056        p->iJD += (n - Z)*86400000;
16057        clearYMD_HMS_TZ(p);
16058        rc = 0;
16059      }
16060      break;
16061    }
16062    case 's': {
16063      /*
16064      **    start of TTTTT
16065      **
16066      ** Move the date backwards to the beginning of the current day,
16067      ** or month or year.
16068      */
16069      if( strncmp(z, "start of ", 9)!=0 ) break;
16070      z += 9;
16071      computeYMD(p);
16072      p->validHMS = 1;
16073      p->h = p->m = 0;
16074      p->s = 0.0;
16075      p->validTZ = 0;
16076      p->validJD = 0;
16077      if( strcmp(z,"month")==0 ){
16078        p->D = 1;
16079        rc = 0;
16080      }else if( strcmp(z,"year")==0 ){
16081        computeYMD(p);
16082        p->M = 1;
16083        p->D = 1;
16084        rc = 0;
16085      }else if( strcmp(z,"day")==0 ){
16086        rc = 0;
16087      }
16088      break;
16089    }
16090    case '+':
16091    case '-':
16092    case '0':
16093    case '1':
16094    case '2':
16095    case '3':
16096    case '4':
16097    case '5':
16098    case '6':
16099    case '7':
16100    case '8':
16101    case '9': {
16102      double rRounder;
16103      for(n=1; z[n] && z[n]!=':' && !sqlite3Isspace(z[n]); n++){}
16104      if( !sqlite3AtoF(z, &r, n, SQLITE_UTF8) ){
16105        rc = 1;
16106        break;
16107      }
16108      if( z[n]==':' ){
16109        /* A modifier of the form (+|-)HH:MM:SS.FFF adds (or subtracts) the
16110        ** specified number of hours, minutes, seconds, and fractional seconds
16111        ** to the time.  The ".FFF" may be omitted.  The ":SS.FFF" may be
16112        ** omitted.
16113        */
16114        const char *z2 = z;
16115        DateTime tx;
16116        sqlite3_int64 day;
16117        if( !sqlite3Isdigit(*z2) ) z2++;
16118        memset(&tx, 0, sizeof(tx));
16119        if( parseHhMmSs(z2, &tx) ) break;
16120        computeJD(&tx);
16121        tx.iJD -= 43200000;
16122        day = tx.iJD/86400000;
16123        tx.iJD -= day*86400000;
16124        if( z[0]=='-' ) tx.iJD = -tx.iJD;
16125        computeJD(p);
16126        clearYMD_HMS_TZ(p);
16127        p->iJD += tx.iJD;
16128        rc = 0;
16129        break;
16130      }
16131      z += n;
16132      while( sqlite3Isspace(*z) ) z++;
16133      n = sqlite3Strlen30(z);
16134      if( n>10 || n<3 ) break;
16135      if( z[n-1]=='s' ){ z[n-1] = 0; n--; }
16136      computeJD(p);
16137      rc = 0;
16138      rRounder = r<0 ? -0.5 : +0.5;
16139      if( n==3 && strcmp(z,"day")==0 ){
16140        p->iJD += (sqlite3_int64)(r*86400000.0 + rRounder);
16141      }else if( n==4 && strcmp(z,"hour")==0 ){
16142        p->iJD += (sqlite3_int64)(r*(86400000.0/24.0) + rRounder);
16143      }else if( n==6 && strcmp(z,"minute")==0 ){
16144        p->iJD += (sqlite3_int64)(r*(86400000.0/(24.0*60.0)) + rRounder);
16145      }else if( n==6 && strcmp(z,"second")==0 ){
16146        p->iJD += (sqlite3_int64)(r*(86400000.0/(24.0*60.0*60.0)) + rRounder);
16147      }else if( n==5 && strcmp(z,"month")==0 ){
16148        int x, y;
16149        computeYMD_HMS(p);
16150        p->M += (int)r;
16151        x = p->M>0 ? (p->M-1)/12 : (p->M-12)/12;
16152        p->Y += x;
16153        p->M -= x*12;
16154        p->validJD = 0;
16155        computeJD(p);
16156        y = (int)r;
16157        if( y!=r ){
16158          p->iJD += (sqlite3_int64)((r - y)*30.0*86400000.0 + rRounder);
16159        }
16160      }else if( n==4 && strcmp(z,"year")==0 ){
16161        int y = (int)r;
16162        computeYMD_HMS(p);
16163        p->Y += y;
16164        p->validJD = 0;
16165        computeJD(p);
16166        if( y!=r ){
16167          p->iJD += (sqlite3_int64)((r - y)*365.0*86400000.0 + rRounder);
16168        }
16169      }else{
16170        rc = 1;
16171      }
16172      clearYMD_HMS_TZ(p);
16173      break;
16174    }
16175    default: {
16176      break;
16177    }
16178  }
16179  return rc;
16180}
16181
16182/*
16183** Process time function arguments.  argv[0] is a date-time stamp.
16184** argv[1] and following are modifiers.  Parse them all and write
16185** the resulting time into the DateTime structure p.  Return 0
16186** on success and 1 if there are any errors.
16187**
16188** If there are zero parameters (if even argv[0] is undefined)
16189** then assume a default value of "now" for argv[0].
16190*/
16191static int isDate(
16192  sqlite3_context *context,
16193  int argc,
16194  sqlite3_value **argv,
16195  DateTime *p
16196){
16197  int i;
16198  const unsigned char *z;
16199  int eType;
16200  memset(p, 0, sizeof(*p));
16201  if( argc==0 ){
16202    return setDateTimeToCurrent(context, p);
16203  }
16204  if( (eType = sqlite3_value_type(argv[0]))==SQLITE_FLOAT
16205                   || eType==SQLITE_INTEGER ){
16206    p->iJD = (sqlite3_int64)(sqlite3_value_double(argv[0])*86400000.0 + 0.5);
16207    p->validJD = 1;
16208  }else{
16209    z = sqlite3_value_text(argv[0]);
16210    if( !z || parseDateOrTime(context, (char*)z, p) ){
16211      return 1;
16212    }
16213  }
16214  for(i=1; i<argc; i++){
16215    z = sqlite3_value_text(argv[i]);
16216    if( z==0 || parseModifier(context, (char*)z, p) ) return 1;
16217  }
16218  return 0;
16219}
16220
16221
16222/*
16223** The following routines implement the various date and time functions
16224** of SQLite.
16225*/
16226
16227/*
16228**    julianday( TIMESTRING, MOD, MOD, ...)
16229**
16230** Return the julian day number of the date specified in the arguments
16231*/
16232static void juliandayFunc(
16233  sqlite3_context *context,
16234  int argc,
16235  sqlite3_value **argv
16236){
16237  DateTime x;
16238  if( isDate(context, argc, argv, &x)==0 ){
16239    computeJD(&x);
16240    sqlite3_result_double(context, x.iJD/86400000.0);
16241  }
16242}
16243
16244/*
16245**    datetime( TIMESTRING, MOD, MOD, ...)
16246**
16247** Return YYYY-MM-DD HH:MM:SS
16248*/
16249static void datetimeFunc(
16250  sqlite3_context *context,
16251  int argc,
16252  sqlite3_value **argv
16253){
16254  DateTime x;
16255  if( isDate(context, argc, argv, &x)==0 ){
16256    char zBuf[100];
16257    computeYMD_HMS(&x);
16258    sqlite3_snprintf(sizeof(zBuf), zBuf, "%04d-%02d-%02d %02d:%02d:%02d",
16259                     x.Y, x.M, x.D, x.h, x.m, (int)(x.s));
16260    sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT);
16261  }
16262}
16263
16264/*
16265**    time( TIMESTRING, MOD, MOD, ...)
16266**
16267** Return HH:MM:SS
16268*/
16269static void timeFunc(
16270  sqlite3_context *context,
16271  int argc,
16272  sqlite3_value **argv
16273){
16274  DateTime x;
16275  if( isDate(context, argc, argv, &x)==0 ){
16276    char zBuf[100];
16277    computeHMS(&x);
16278    sqlite3_snprintf(sizeof(zBuf), zBuf, "%02d:%02d:%02d", x.h, x.m, (int)x.s);
16279    sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT);
16280  }
16281}
16282
16283/*
16284**    date( TIMESTRING, MOD, MOD, ...)
16285**
16286** Return YYYY-MM-DD
16287*/
16288static void dateFunc(
16289  sqlite3_context *context,
16290  int argc,
16291  sqlite3_value **argv
16292){
16293  DateTime x;
16294  if( isDate(context, argc, argv, &x)==0 ){
16295    char zBuf[100];
16296    computeYMD(&x);
16297    sqlite3_snprintf(sizeof(zBuf), zBuf, "%04d-%02d-%02d", x.Y, x.M, x.D);
16298    sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT);
16299  }
16300}
16301
16302/*
16303**    strftime( FORMAT, TIMESTRING, MOD, MOD, ...)
16304**
16305** Return a string described by FORMAT.  Conversions as follows:
16306**
16307**   %d  day of month
16308**   %f  ** fractional seconds  SS.SSS
16309**   %H  hour 00-24
16310**   %j  day of year 000-366
16311**   %J  ** julian day number
16312**   %m  month 01-12
16313**   %M  minute 00-59
16314**   %s  seconds since 1970-01-01
16315**   %S  seconds 00-59
16316**   %w  day of week 0-6  sunday==0
16317**   %W  week of year 00-53
16318**   %Y  year 0000-9999
16319**   %%  %
16320*/
16321static void strftimeFunc(
16322  sqlite3_context *context,
16323  int argc,
16324  sqlite3_value **argv
16325){
16326  DateTime x;
16327  u64 n;
16328  size_t i,j;
16329  char *z;
16330  sqlite3 *db;
16331  const char *zFmt;
16332  char zBuf[100];
16333  if( argc==0 ) return;
16334  zFmt = (const char*)sqlite3_value_text(argv[0]);
16335  if( zFmt==0 || isDate(context, argc-1, argv+1, &x) ) return;
16336  db = sqlite3_context_db_handle(context);
16337  for(i=0, n=1; zFmt[i]; i++, n++){
16338    if( zFmt[i]=='%' ){
16339      switch( zFmt[i+1] ){
16340        case 'd':
16341        case 'H':
16342        case 'm':
16343        case 'M':
16344        case 'S':
16345        case 'W':
16346          n++;
16347          /* fall thru */
16348        case 'w':
16349        case '%':
16350          break;
16351        case 'f':
16352          n += 8;
16353          break;
16354        case 'j':
16355          n += 3;
16356          break;
16357        case 'Y':
16358          n += 8;
16359          break;
16360        case 's':
16361        case 'J':
16362          n += 50;
16363          break;
16364        default:
16365          return;  /* ERROR.  return a NULL */
16366      }
16367      i++;
16368    }
16369  }
16370  testcase( n==sizeof(zBuf)-1 );
16371  testcase( n==sizeof(zBuf) );
16372  testcase( n==(u64)db->aLimit[SQLITE_LIMIT_LENGTH]+1 );
16373  testcase( n==(u64)db->aLimit[SQLITE_LIMIT_LENGTH] );
16374  if( n<sizeof(zBuf) ){
16375    z = zBuf;
16376  }else if( n>(u64)db->aLimit[SQLITE_LIMIT_LENGTH] ){
16377    sqlite3_result_error_toobig(context);
16378    return;
16379  }else{
16380    z = sqlite3DbMallocRaw(db, (int)n);
16381    if( z==0 ){
16382      sqlite3_result_error_nomem(context);
16383      return;
16384    }
16385  }
16386  computeJD(&x);
16387  computeYMD_HMS(&x);
16388  for(i=j=0; zFmt[i]; i++){
16389    if( zFmt[i]!='%' ){
16390      z[j++] = zFmt[i];
16391    }else{
16392      i++;
16393      switch( zFmt[i] ){
16394        case 'd':  sqlite3_snprintf(3, &z[j],"%02d",x.D); j+=2; break;
16395        case 'f': {
16396          double s = x.s;
16397          if( s>59.999 ) s = 59.999;
16398          sqlite3_snprintf(7, &z[j],"%06.3f", s);
16399          j += sqlite3Strlen30(&z[j]);
16400          break;
16401        }
16402        case 'H':  sqlite3_snprintf(3, &z[j],"%02d",x.h); j+=2; break;
16403        case 'W': /* Fall thru */
16404        case 'j': {
16405          int nDay;             /* Number of days since 1st day of year */
16406          DateTime y = x;
16407          y.validJD = 0;
16408          y.M = 1;
16409          y.D = 1;
16410          computeJD(&y);
16411          nDay = (int)((x.iJD-y.iJD+43200000)/86400000);
16412          if( zFmt[i]=='W' ){
16413            int wd;   /* 0=Monday, 1=Tuesday, ... 6=Sunday */
16414            wd = (int)(((x.iJD+43200000)/86400000)%7);
16415            sqlite3_snprintf(3, &z[j],"%02d",(nDay+7-wd)/7);
16416            j += 2;
16417          }else{
16418            sqlite3_snprintf(4, &z[j],"%03d",nDay+1);
16419            j += 3;
16420          }
16421          break;
16422        }
16423        case 'J': {
16424          sqlite3_snprintf(20, &z[j],"%.16g",x.iJD/86400000.0);
16425          j+=sqlite3Strlen30(&z[j]);
16426          break;
16427        }
16428        case 'm':  sqlite3_snprintf(3, &z[j],"%02d",x.M); j+=2; break;
16429        case 'M':  sqlite3_snprintf(3, &z[j],"%02d",x.m); j+=2; break;
16430        case 's': {
16431          sqlite3_snprintf(30,&z[j],"%lld",
16432                           (i64)(x.iJD/1000 - 21086676*(i64)10000));
16433          j += sqlite3Strlen30(&z[j]);
16434          break;
16435        }
16436        case 'S':  sqlite3_snprintf(3,&z[j],"%02d",(int)x.s); j+=2; break;
16437        case 'w': {
16438          z[j++] = (char)(((x.iJD+129600000)/86400000) % 7) + '0';
16439          break;
16440        }
16441        case 'Y': {
16442          sqlite3_snprintf(5,&z[j],"%04d",x.Y); j+=sqlite3Strlen30(&z[j]);
16443          break;
16444        }
16445        default:   z[j++] = '%'; break;
16446      }
16447    }
16448  }
16449  z[j] = 0;
16450  sqlite3_result_text(context, z, -1,
16451                      z==zBuf ? SQLITE_TRANSIENT : SQLITE_DYNAMIC);
16452}
16453
16454/*
16455** current_time()
16456**
16457** This function returns the same value as time('now').
16458*/
16459static void ctimeFunc(
16460  sqlite3_context *context,
16461  int NotUsed,
16462  sqlite3_value **NotUsed2
16463){
16464  UNUSED_PARAMETER2(NotUsed, NotUsed2);
16465  timeFunc(context, 0, 0);
16466}
16467
16468/*
16469** current_date()
16470**
16471** This function returns the same value as date('now').
16472*/
16473static void cdateFunc(
16474  sqlite3_context *context,
16475  int NotUsed,
16476  sqlite3_value **NotUsed2
16477){
16478  UNUSED_PARAMETER2(NotUsed, NotUsed2);
16479  dateFunc(context, 0, 0);
16480}
16481
16482/*
16483** current_timestamp()
16484**
16485** This function returns the same value as datetime('now').
16486*/
16487static void ctimestampFunc(
16488  sqlite3_context *context,
16489  int NotUsed,
16490  sqlite3_value **NotUsed2
16491){
16492  UNUSED_PARAMETER2(NotUsed, NotUsed2);
16493  datetimeFunc(context, 0, 0);
16494}
16495#endif /* !defined(SQLITE_OMIT_DATETIME_FUNCS) */
16496
16497#ifdef SQLITE_OMIT_DATETIME_FUNCS
16498/*
16499** If the library is compiled to omit the full-scale date and time
16500** handling (to get a smaller binary), the following minimal version
16501** of the functions current_time(), current_date() and current_timestamp()
16502** are included instead. This is to support column declarations that
16503** include "DEFAULT CURRENT_TIME" etc.
16504**
16505** This function uses the C-library functions time(), gmtime()
16506** and strftime(). The format string to pass to strftime() is supplied
16507** as the user-data for the function.
16508*/
16509static void currentTimeFunc(
16510  sqlite3_context *context,
16511  int argc,
16512  sqlite3_value **argv
16513){
16514  time_t t;
16515  char *zFormat = (char *)sqlite3_user_data(context);
16516  sqlite3 *db;
16517  sqlite3_int64 iT;
16518  struct tm *pTm;
16519  struct tm sNow;
16520  char zBuf[20];
16521
16522  UNUSED_PARAMETER(argc);
16523  UNUSED_PARAMETER(argv);
16524
16525  iT = sqlite3StmtCurrentTime(context);
16526  if( iT<=0 ) return;
16527  t = iT/1000 - 10000*(sqlite3_int64)21086676;
16528#if HAVE_GMTIME_R
16529  pTm = gmtime_r(&t, &sNow);
16530#else
16531  sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
16532  pTm = gmtime(&t);
16533  if( pTm ) memcpy(&sNow, pTm, sizeof(sNow));
16534  sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
16535#endif
16536  if( pTm ){
16537    strftime(zBuf, 20, zFormat, &sNow);
16538    sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT);
16539  }
16540}
16541#endif
16542
16543/*
16544** This function registered all of the above C functions as SQL
16545** functions.  This should be the only routine in this file with
16546** external linkage.
16547*/
16548SQLITE_PRIVATE void sqlite3RegisterDateTimeFunctions(void){
16549  static SQLITE_WSD FuncDef aDateTimeFuncs[] = {
16550#ifndef SQLITE_OMIT_DATETIME_FUNCS
16551    FUNCTION(julianday,        -1, 0, 0, juliandayFunc ),
16552    FUNCTION(date,             -1, 0, 0, dateFunc      ),
16553    FUNCTION(time,             -1, 0, 0, timeFunc      ),
16554    FUNCTION(datetime,         -1, 0, 0, datetimeFunc  ),
16555    FUNCTION(strftime,         -1, 0, 0, strftimeFunc  ),
16556    FUNCTION(current_time,      0, 0, 0, ctimeFunc     ),
16557    FUNCTION(current_timestamp, 0, 0, 0, ctimestampFunc),
16558    FUNCTION(current_date,      0, 0, 0, cdateFunc     ),
16559#else
16560    STR_FUNCTION(current_time,      0, "%H:%M:%S",          0, currentTimeFunc),
16561    STR_FUNCTION(current_date,      0, "%Y-%m-%d",          0, currentTimeFunc),
16562    STR_FUNCTION(current_timestamp, 0, "%Y-%m-%d %H:%M:%S", 0, currentTimeFunc),
16563#endif
16564  };
16565  int i;
16566  FuncDefHash *pHash = &GLOBAL(FuncDefHash, sqlite3GlobalFunctions);
16567  FuncDef *aFunc = (FuncDef*)&GLOBAL(FuncDef, aDateTimeFuncs);
16568
16569  for(i=0; i<ArraySize(aDateTimeFuncs); i++){
16570    sqlite3FuncDefInsert(pHash, &aFunc[i]);
16571  }
16572}
16573
16574/************** End of date.c ************************************************/
16575/************** Begin file os.c **********************************************/
16576/*
16577** 2005 November 29
16578**
16579** The author disclaims copyright to this source code.  In place of
16580** a legal notice, here is a blessing:
16581**
16582**    May you do good and not evil.
16583**    May you find forgiveness for yourself and forgive others.
16584**    May you share freely, never taking more than you give.
16585**
16586******************************************************************************
16587**
16588** This file contains OS interface code that is common to all
16589** architectures.
16590*/
16591#define _SQLITE_OS_C_ 1
16592/* #include "sqliteInt.h" */
16593#undef _SQLITE_OS_C_
16594
16595/*
16596** The default SQLite sqlite3_vfs implementations do not allocate
16597** memory (actually, os_unix.c allocates a small amount of memory
16598** from within OsOpen()), but some third-party implementations may.
16599** So we test the effects of a malloc() failing and the sqlite3OsXXX()
16600** function returning SQLITE_IOERR_NOMEM using the DO_OS_MALLOC_TEST macro.
16601**
16602** The following functions are instrumented for malloc() failure
16603** testing:
16604**
16605**     sqlite3OsRead()
16606**     sqlite3OsWrite()
16607**     sqlite3OsSync()
16608**     sqlite3OsFileSize()
16609**     sqlite3OsLock()
16610**     sqlite3OsCheckReservedLock()
16611**     sqlite3OsFileControl()
16612**     sqlite3OsShmMap()
16613**     sqlite3OsOpen()
16614**     sqlite3OsDelete()
16615**     sqlite3OsAccess()
16616**     sqlite3OsFullPathname()
16617**
16618*/
16619#if defined(SQLITE_TEST)
16620SQLITE_API int sqlite3_memdebug_vfs_oom_test = 1;
16621  #define DO_OS_MALLOC_TEST(x)                                       \
16622  if (sqlite3_memdebug_vfs_oom_test && (!x || !sqlite3IsMemJournal(x))) {  \
16623    void *pTstAlloc = sqlite3Malloc(10);                             \
16624    if (!pTstAlloc) return SQLITE_IOERR_NOMEM;                       \
16625    sqlite3_free(pTstAlloc);                                         \
16626  }
16627#else
16628  #define DO_OS_MALLOC_TEST(x)
16629#endif
16630
16631/*
16632** The following routines are convenience wrappers around methods
16633** of the sqlite3_file object.  This is mostly just syntactic sugar. All
16634** of this would be completely automatic if SQLite were coded using
16635** C++ instead of plain old C.
16636*/
16637SQLITE_PRIVATE int sqlite3OsClose(sqlite3_file *pId){
16638  int rc = SQLITE_OK;
16639  if( pId->pMethods ){
16640    rc = pId->pMethods->xClose(pId);
16641    pId->pMethods = 0;
16642  }
16643  return rc;
16644}
16645SQLITE_PRIVATE int sqlite3OsRead(sqlite3_file *id, void *pBuf, int amt, i64 offset){
16646  DO_OS_MALLOC_TEST(id);
16647  return id->pMethods->xRead(id, pBuf, amt, offset);
16648}
16649SQLITE_PRIVATE int sqlite3OsWrite(sqlite3_file *id, const void *pBuf, int amt, i64 offset){
16650  DO_OS_MALLOC_TEST(id);
16651  return id->pMethods->xWrite(id, pBuf, amt, offset);
16652}
16653SQLITE_PRIVATE int sqlite3OsTruncate(sqlite3_file *id, i64 size){
16654  return id->pMethods->xTruncate(id, size);
16655}
16656SQLITE_PRIVATE int sqlite3OsSync(sqlite3_file *id, int flags){
16657  DO_OS_MALLOC_TEST(id);
16658  return id->pMethods->xSync(id, flags);
16659}
16660SQLITE_PRIVATE int sqlite3OsFileSize(sqlite3_file *id, i64 *pSize){
16661  DO_OS_MALLOC_TEST(id);
16662  return id->pMethods->xFileSize(id, pSize);
16663}
16664SQLITE_PRIVATE int sqlite3OsLock(sqlite3_file *id, int lockType){
16665  DO_OS_MALLOC_TEST(id);
16666  return id->pMethods->xLock(id, lockType);
16667}
16668SQLITE_PRIVATE int sqlite3OsUnlock(sqlite3_file *id, int lockType){
16669  return id->pMethods->xUnlock(id, lockType);
16670}
16671SQLITE_PRIVATE int sqlite3OsCheckReservedLock(sqlite3_file *id, int *pResOut){
16672  DO_OS_MALLOC_TEST(id);
16673  return id->pMethods->xCheckReservedLock(id, pResOut);
16674}
16675
16676/*
16677** Use sqlite3OsFileControl() when we are doing something that might fail
16678** and we need to know about the failures.  Use sqlite3OsFileControlHint()
16679** when simply tossing information over the wall to the VFS and we do not
16680** really care if the VFS receives and understands the information since it
16681** is only a hint and can be safely ignored.  The sqlite3OsFileControlHint()
16682** routine has no return value since the return value would be meaningless.
16683*/
16684SQLITE_PRIVATE int sqlite3OsFileControl(sqlite3_file *id, int op, void *pArg){
16685#ifdef SQLITE_TEST
16686  if( op!=SQLITE_FCNTL_COMMIT_PHASETWO ){
16687    /* Faults are not injected into COMMIT_PHASETWO because, assuming SQLite
16688    ** is using a regular VFS, it is called after the corresponding
16689    ** transaction has been committed. Injecting a fault at this point
16690    ** confuses the test scripts - the COMMIT comand returns SQLITE_NOMEM
16691    ** but the transaction is committed anyway.
16692    **
16693    ** The core must call OsFileControl() though, not OsFileControlHint(),
16694    ** as if a custom VFS (e.g. zipvfs) returns an error here, it probably
16695    ** means the commit really has failed and an error should be returned
16696    ** to the user.  */
16697    DO_OS_MALLOC_TEST(id);
16698  }
16699#endif
16700  return id->pMethods->xFileControl(id, op, pArg);
16701}
16702SQLITE_PRIVATE void sqlite3OsFileControlHint(sqlite3_file *id, int op, void *pArg){
16703  (void)id->pMethods->xFileControl(id, op, pArg);
16704}
16705
16706SQLITE_PRIVATE int sqlite3OsSectorSize(sqlite3_file *id){
16707  int (*xSectorSize)(sqlite3_file*) = id->pMethods->xSectorSize;
16708  return (xSectorSize ? xSectorSize(id) : SQLITE_DEFAULT_SECTOR_SIZE);
16709}
16710SQLITE_PRIVATE int sqlite3OsDeviceCharacteristics(sqlite3_file *id){
16711  return id->pMethods->xDeviceCharacteristics(id);
16712}
16713SQLITE_PRIVATE int sqlite3OsShmLock(sqlite3_file *id, int offset, int n, int flags){
16714  return id->pMethods->xShmLock(id, offset, n, flags);
16715}
16716SQLITE_PRIVATE void sqlite3OsShmBarrier(sqlite3_file *id){
16717  id->pMethods->xShmBarrier(id);
16718}
16719SQLITE_PRIVATE int sqlite3OsShmUnmap(sqlite3_file *id, int deleteFlag){
16720  return id->pMethods->xShmUnmap(id, deleteFlag);
16721}
16722SQLITE_PRIVATE int sqlite3OsShmMap(
16723  sqlite3_file *id,               /* Database file handle */
16724  int iPage,
16725  int pgsz,
16726  int bExtend,                    /* True to extend file if necessary */
16727  void volatile **pp              /* OUT: Pointer to mapping */
16728){
16729  DO_OS_MALLOC_TEST(id);
16730  return id->pMethods->xShmMap(id, iPage, pgsz, bExtend, pp);
16731}
16732
16733#if SQLITE_MAX_MMAP_SIZE>0
16734/* The real implementation of xFetch and xUnfetch */
16735SQLITE_PRIVATE int sqlite3OsFetch(sqlite3_file *id, i64 iOff, int iAmt, void **pp){
16736  DO_OS_MALLOC_TEST(id);
16737  return id->pMethods->xFetch(id, iOff, iAmt, pp);
16738}
16739SQLITE_PRIVATE int sqlite3OsUnfetch(sqlite3_file *id, i64 iOff, void *p){
16740  return id->pMethods->xUnfetch(id, iOff, p);
16741}
16742#else
16743/* No-op stubs to use when memory-mapped I/O is disabled */
16744SQLITE_PRIVATE int sqlite3OsFetch(sqlite3_file *id, i64 iOff, int iAmt, void **pp){
16745  *pp = 0;
16746  return SQLITE_OK;
16747}
16748SQLITE_PRIVATE int sqlite3OsUnfetch(sqlite3_file *id, i64 iOff, void *p){
16749  return SQLITE_OK;
16750}
16751#endif
16752
16753/*
16754** The next group of routines are convenience wrappers around the
16755** VFS methods.
16756*/
16757SQLITE_PRIVATE int sqlite3OsOpen(
16758  sqlite3_vfs *pVfs,
16759  const char *zPath,
16760  sqlite3_file *pFile,
16761  int flags,
16762  int *pFlagsOut
16763){
16764  int rc;
16765  DO_OS_MALLOC_TEST(0);
16766  /* 0x87f7f is a mask of SQLITE_OPEN_ flags that are valid to be passed
16767  ** down into the VFS layer.  Some SQLITE_OPEN_ flags (for example,
16768  ** SQLITE_OPEN_FULLMUTEX or SQLITE_OPEN_SHAREDCACHE) are blocked before
16769  ** reaching the VFS. */
16770  rc = pVfs->xOpen(pVfs, zPath, pFile, flags & 0x87f7f, pFlagsOut);
16771  assert( rc==SQLITE_OK || pFile->pMethods==0 );
16772  return rc;
16773}
16774SQLITE_PRIVATE int sqlite3OsDelete(sqlite3_vfs *pVfs, const char *zPath, int dirSync){
16775  DO_OS_MALLOC_TEST(0);
16776  assert( dirSync==0 || dirSync==1 );
16777  return pVfs->xDelete(pVfs, zPath, dirSync);
16778}
16779SQLITE_PRIVATE int sqlite3OsAccess(
16780  sqlite3_vfs *pVfs,
16781  const char *zPath,
16782  int flags,
16783  int *pResOut
16784){
16785  DO_OS_MALLOC_TEST(0);
16786  return pVfs->xAccess(pVfs, zPath, flags, pResOut);
16787}
16788SQLITE_PRIVATE int sqlite3OsFullPathname(
16789  sqlite3_vfs *pVfs,
16790  const char *zPath,
16791  int nPathOut,
16792  char *zPathOut
16793){
16794  DO_OS_MALLOC_TEST(0);
16795  zPathOut[0] = 0;
16796  return pVfs->xFullPathname(pVfs, zPath, nPathOut, zPathOut);
16797}
16798#ifndef SQLITE_OMIT_LOAD_EXTENSION
16799SQLITE_PRIVATE void *sqlite3OsDlOpen(sqlite3_vfs *pVfs, const char *zPath){
16800  return pVfs->xDlOpen(pVfs, zPath);
16801}
16802SQLITE_PRIVATE void sqlite3OsDlError(sqlite3_vfs *pVfs, int nByte, char *zBufOut){
16803  pVfs->xDlError(pVfs, nByte, zBufOut);
16804}
16805SQLITE_PRIVATE void (*sqlite3OsDlSym(sqlite3_vfs *pVfs, void *pHdle, const char *zSym))(void){
16806  return pVfs->xDlSym(pVfs, pHdle, zSym);
16807}
16808SQLITE_PRIVATE void sqlite3OsDlClose(sqlite3_vfs *pVfs, void *pHandle){
16809  pVfs->xDlClose(pVfs, pHandle);
16810}
16811#endif /* SQLITE_OMIT_LOAD_EXTENSION */
16812SQLITE_PRIVATE int sqlite3OsRandomness(sqlite3_vfs *pVfs, int nByte, char *zBufOut){
16813  return pVfs->xRandomness(pVfs, nByte, zBufOut);
16814}
16815SQLITE_PRIVATE int sqlite3OsSleep(sqlite3_vfs *pVfs, int nMicro){
16816  return pVfs->xSleep(pVfs, nMicro);
16817}
16818SQLITE_PRIVATE int sqlite3OsCurrentTimeInt64(sqlite3_vfs *pVfs, sqlite3_int64 *pTimeOut){
16819  int rc;
16820  /* IMPLEMENTATION-OF: R-49045-42493 SQLite will use the xCurrentTimeInt64()
16821  ** method to get the current date and time if that method is available
16822  ** (if iVersion is 2 or greater and the function pointer is not NULL) and
16823  ** will fall back to xCurrentTime() if xCurrentTimeInt64() is
16824  ** unavailable.
16825  */
16826  if( pVfs->iVersion>=2 && pVfs->xCurrentTimeInt64 ){
16827    rc = pVfs->xCurrentTimeInt64(pVfs, pTimeOut);
16828  }else{
16829    double r;
16830    rc = pVfs->xCurrentTime(pVfs, &r);
16831    *pTimeOut = (sqlite3_int64)(r*86400000.0);
16832  }
16833  return rc;
16834}
16835
16836SQLITE_PRIVATE int sqlite3OsOpenMalloc(
16837  sqlite3_vfs *pVfs,
16838  const char *zFile,
16839  sqlite3_file **ppFile,
16840  int flags,
16841  int *pOutFlags
16842){
16843  int rc = SQLITE_NOMEM;
16844  sqlite3_file *pFile;
16845  pFile = (sqlite3_file *)sqlite3MallocZero(pVfs->szOsFile);
16846  if( pFile ){
16847    rc = sqlite3OsOpen(pVfs, zFile, pFile, flags, pOutFlags);
16848    if( rc!=SQLITE_OK ){
16849      sqlite3_free(pFile);
16850    }else{
16851      *ppFile = pFile;
16852    }
16853  }
16854  return rc;
16855}
16856SQLITE_PRIVATE int sqlite3OsCloseFree(sqlite3_file *pFile){
16857  int rc = SQLITE_OK;
16858  assert( pFile );
16859  rc = sqlite3OsClose(pFile);
16860  sqlite3_free(pFile);
16861  return rc;
16862}
16863
16864/*
16865** This function is a wrapper around the OS specific implementation of
16866** sqlite3_os_init(). The purpose of the wrapper is to provide the
16867** ability to simulate a malloc failure, so that the handling of an
16868** error in sqlite3_os_init() by the upper layers can be tested.
16869*/
16870SQLITE_PRIVATE int sqlite3OsInit(void){
16871  void *p = sqlite3_malloc(10);
16872  if( p==0 ) return SQLITE_NOMEM;
16873  sqlite3_free(p);
16874  return sqlite3_os_init();
16875}
16876
16877/*
16878** The list of all registered VFS implementations.
16879*/
16880static sqlite3_vfs * SQLITE_WSD vfsList = 0;
16881#define vfsList GLOBAL(sqlite3_vfs *, vfsList)
16882
16883/*
16884** Locate a VFS by name.  If no name is given, simply return the
16885** first VFS on the list.
16886*/
16887SQLITE_API sqlite3_vfs *SQLITE_STDCALL sqlite3_vfs_find(const char *zVfs){
16888  sqlite3_vfs *pVfs = 0;
16889#if SQLITE_THREADSAFE
16890  sqlite3_mutex *mutex;
16891#endif
16892#ifndef SQLITE_OMIT_AUTOINIT
16893  int rc = sqlite3_initialize();
16894  if( rc ) return 0;
16895#endif
16896#if SQLITE_THREADSAFE
16897  mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);
16898#endif
16899  sqlite3_mutex_enter(mutex);
16900  for(pVfs = vfsList; pVfs; pVfs=pVfs->pNext){
16901    if( zVfs==0 ) break;
16902    if( strcmp(zVfs, pVfs->zName)==0 ) break;
16903  }
16904  sqlite3_mutex_leave(mutex);
16905  return pVfs;
16906}
16907
16908/*
16909** Unlink a VFS from the linked list
16910*/
16911static void vfsUnlink(sqlite3_vfs *pVfs){
16912  assert( sqlite3_mutex_held(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER)) );
16913  if( pVfs==0 ){
16914    /* No-op */
16915  }else if( vfsList==pVfs ){
16916    vfsList = pVfs->pNext;
16917  }else if( vfsList ){
16918    sqlite3_vfs *p = vfsList;
16919    while( p->pNext && p->pNext!=pVfs ){
16920      p = p->pNext;
16921    }
16922    if( p->pNext==pVfs ){
16923      p->pNext = pVfs->pNext;
16924    }
16925  }
16926}
16927
16928/*
16929** Register a VFS with the system.  It is harmless to register the same
16930** VFS multiple times.  The new VFS becomes the default if makeDflt is
16931** true.
16932*/
16933SQLITE_API int SQLITE_STDCALL sqlite3_vfs_register(sqlite3_vfs *pVfs, int makeDflt){
16934  MUTEX_LOGIC(sqlite3_mutex *mutex;)
16935#ifndef SQLITE_OMIT_AUTOINIT
16936  int rc = sqlite3_initialize();
16937  if( rc ) return rc;
16938#endif
16939#ifdef SQLITE_ENABLE_API_ARMOR
16940  if( pVfs==0 ) return SQLITE_MISUSE_BKPT;
16941#endif
16942
16943  MUTEX_LOGIC( mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER); )
16944  sqlite3_mutex_enter(mutex);
16945  vfsUnlink(pVfs);
16946  if( makeDflt || vfsList==0 ){
16947    pVfs->pNext = vfsList;
16948    vfsList = pVfs;
16949  }else{
16950    pVfs->pNext = vfsList->pNext;
16951    vfsList->pNext = pVfs;
16952  }
16953  assert(vfsList);
16954  sqlite3_mutex_leave(mutex);
16955  return SQLITE_OK;
16956}
16957
16958/*
16959** Unregister a VFS so that it is no longer accessible.
16960*/
16961SQLITE_API int SQLITE_STDCALL sqlite3_vfs_unregister(sqlite3_vfs *pVfs){
16962#if SQLITE_THREADSAFE
16963  sqlite3_mutex *mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);
16964#endif
16965  sqlite3_mutex_enter(mutex);
16966  vfsUnlink(pVfs);
16967  sqlite3_mutex_leave(mutex);
16968  return SQLITE_OK;
16969}
16970
16971/************** End of os.c **************************************************/
16972/************** Begin file fault.c *******************************************/
16973/*
16974** 2008 Jan 22
16975**
16976** The author disclaims copyright to this source code.  In place of
16977** a legal notice, here is a blessing:
16978**
16979**    May you do good and not evil.
16980**    May you find forgiveness for yourself and forgive others.
16981**    May you share freely, never taking more than you give.
16982**
16983*************************************************************************
16984**
16985** This file contains code to support the concept of "benign"
16986** malloc failures (when the xMalloc() or xRealloc() method of the
16987** sqlite3_mem_methods structure fails to allocate a block of memory
16988** and returns 0).
16989**
16990** Most malloc failures are non-benign. After they occur, SQLite
16991** abandons the current operation and returns an error code (usually
16992** SQLITE_NOMEM) to the user. However, sometimes a fault is not necessarily
16993** fatal. For example, if a malloc fails while resizing a hash table, this
16994** is completely recoverable simply by not carrying out the resize. The
16995** hash table will continue to function normally.  So a malloc failure
16996** during a hash table resize is a benign fault.
16997*/
16998
16999/* #include "sqliteInt.h" */
17000
17001#ifndef SQLITE_OMIT_BUILTIN_TEST
17002
17003/*
17004** Global variables.
17005*/
17006typedef struct BenignMallocHooks BenignMallocHooks;
17007static SQLITE_WSD struct BenignMallocHooks {
17008  void (*xBenignBegin)(void);
17009  void (*xBenignEnd)(void);
17010} sqlite3Hooks = { 0, 0 };
17011
17012/* The "wsdHooks" macro will resolve to the appropriate BenignMallocHooks
17013** structure.  If writable static data is unsupported on the target,
17014** we have to locate the state vector at run-time.  In the more common
17015** case where writable static data is supported, wsdHooks can refer directly
17016** to the "sqlite3Hooks" state vector declared above.
17017*/
17018#ifdef SQLITE_OMIT_WSD
17019# define wsdHooksInit \
17020  BenignMallocHooks *x = &GLOBAL(BenignMallocHooks,sqlite3Hooks)
17021# define wsdHooks x[0]
17022#else
17023# define wsdHooksInit
17024# define wsdHooks sqlite3Hooks
17025#endif
17026
17027
17028/*
17029** Register hooks to call when sqlite3BeginBenignMalloc() and
17030** sqlite3EndBenignMalloc() are called, respectively.
17031*/
17032SQLITE_PRIVATE void sqlite3BenignMallocHooks(
17033  void (*xBenignBegin)(void),
17034  void (*xBenignEnd)(void)
17035){
17036  wsdHooksInit;
17037  wsdHooks.xBenignBegin = xBenignBegin;
17038  wsdHooks.xBenignEnd = xBenignEnd;
17039}
17040
17041/*
17042** This (sqlite3EndBenignMalloc()) is called by SQLite code to indicate that
17043** subsequent malloc failures are benign. A call to sqlite3EndBenignMalloc()
17044** indicates that subsequent malloc failures are non-benign.
17045*/
17046SQLITE_PRIVATE void sqlite3BeginBenignMalloc(void){
17047  wsdHooksInit;
17048  if( wsdHooks.xBenignBegin ){
17049    wsdHooks.xBenignBegin();
17050  }
17051}
17052SQLITE_PRIVATE void sqlite3EndBenignMalloc(void){
17053  wsdHooksInit;
17054  if( wsdHooks.xBenignEnd ){
17055    wsdHooks.xBenignEnd();
17056  }
17057}
17058
17059#endif   /* #ifndef SQLITE_OMIT_BUILTIN_TEST */
17060
17061/************** End of fault.c ***********************************************/
17062/************** Begin file mem0.c ********************************************/
17063/*
17064** 2008 October 28
17065**
17066** The author disclaims copyright to this source code.  In place of
17067** a legal notice, here is a blessing:
17068**
17069**    May you do good and not evil.
17070**    May you find forgiveness for yourself and forgive others.
17071**    May you share freely, never taking more than you give.
17072**
17073*************************************************************************
17074**
17075** This file contains a no-op memory allocation drivers for use when
17076** SQLITE_ZERO_MALLOC is defined.  The allocation drivers implemented
17077** here always fail.  SQLite will not operate with these drivers.  These
17078** are merely placeholders.  Real drivers must be substituted using
17079** sqlite3_config() before SQLite will operate.
17080*/
17081/* #include "sqliteInt.h" */
17082
17083/*
17084** This version of the memory allocator is the default.  It is
17085** used when no other memory allocator is specified using compile-time
17086** macros.
17087*/
17088#ifdef SQLITE_ZERO_MALLOC
17089
17090/*
17091** No-op versions of all memory allocation routines
17092*/
17093static void *sqlite3MemMalloc(int nByte){ return 0; }
17094static void sqlite3MemFree(void *pPrior){ return; }
17095static void *sqlite3MemRealloc(void *pPrior, int nByte){ return 0; }
17096static int sqlite3MemSize(void *pPrior){ return 0; }
17097static int sqlite3MemRoundup(int n){ return n; }
17098static int sqlite3MemInit(void *NotUsed){ return SQLITE_OK; }
17099static void sqlite3MemShutdown(void *NotUsed){ return; }
17100
17101/*
17102** This routine is the only routine in this file with external linkage.
17103**
17104** Populate the low-level memory allocation function pointers in
17105** sqlite3GlobalConfig.m with pointers to the routines in this file.
17106*/
17107SQLITE_PRIVATE void sqlite3MemSetDefault(void){
17108  static const sqlite3_mem_methods defaultMethods = {
17109     sqlite3MemMalloc,
17110     sqlite3MemFree,
17111     sqlite3MemRealloc,
17112     sqlite3MemSize,
17113     sqlite3MemRoundup,
17114     sqlite3MemInit,
17115     sqlite3MemShutdown,
17116     0
17117  };
17118  sqlite3_config(SQLITE_CONFIG_MALLOC, &defaultMethods);
17119}
17120
17121#endif /* SQLITE_ZERO_MALLOC */
17122
17123/************** End of mem0.c ************************************************/
17124/************** Begin file mem1.c ********************************************/
17125/*
17126** 2007 August 14
17127**
17128** The author disclaims copyright to this source code.  In place of
17129** a legal notice, here is a blessing:
17130**
17131**    May you do good and not evil.
17132**    May you find forgiveness for yourself and forgive others.
17133**    May you share freely, never taking more than you give.
17134**
17135*************************************************************************
17136**
17137** This file contains low-level memory allocation drivers for when
17138** SQLite will use the standard C-library malloc/realloc/free interface
17139** to obtain the memory it needs.
17140**
17141** This file contains implementations of the low-level memory allocation
17142** routines specified in the sqlite3_mem_methods object.  The content of
17143** this file is only used if SQLITE_SYSTEM_MALLOC is defined.  The
17144** SQLITE_SYSTEM_MALLOC macro is defined automatically if neither the
17145** SQLITE_MEMDEBUG nor the SQLITE_WIN32_MALLOC macros are defined.  The
17146** default configuration is to use memory allocation routines in this
17147** file.
17148**
17149** C-preprocessor macro summary:
17150**
17151**    HAVE_MALLOC_USABLE_SIZE     The configure script sets this symbol if
17152**                                the malloc_usable_size() interface exists
17153**                                on the target platform.  Or, this symbol
17154**                                can be set manually, if desired.
17155**                                If an equivalent interface exists by
17156**                                a different name, using a separate -D
17157**                                option to rename it.
17158**
17159**    SQLITE_WITHOUT_ZONEMALLOC   Some older macs lack support for the zone
17160**                                memory allocator.  Set this symbol to enable
17161**                                building on older macs.
17162**
17163**    SQLITE_WITHOUT_MSIZE        Set this symbol to disable the use of
17164**                                _msize() on windows systems.  This might
17165**                                be necessary when compiling for Delphi,
17166**                                for example.
17167*/
17168/* #include "sqliteInt.h" */
17169
17170/*
17171** This version of the memory allocator is the default.  It is
17172** used when no other memory allocator is specified using compile-time
17173** macros.
17174*/
17175#ifdef SQLITE_SYSTEM_MALLOC
17176#if defined(__APPLE__) && !defined(SQLITE_WITHOUT_ZONEMALLOC)
17177
17178/*
17179** Use the zone allocator available on apple products unless the
17180** SQLITE_WITHOUT_ZONEMALLOC symbol is defined.
17181*/
17182#include <sys/sysctl.h>
17183#include <malloc/malloc.h>
17184#include <libkern/OSAtomic.h>
17185static malloc_zone_t* _sqliteZone_;
17186#define SQLITE_MALLOC(x) malloc_zone_malloc(_sqliteZone_, (x))
17187#define SQLITE_FREE(x) malloc_zone_free(_sqliteZone_, (x));
17188#define SQLITE_REALLOC(x,y) malloc_zone_realloc(_sqliteZone_, (x), (y))
17189#define SQLITE_MALLOCSIZE(x) \
17190        (_sqliteZone_ ? _sqliteZone_->size(_sqliteZone_,x) : malloc_size(x))
17191
17192#else /* if not __APPLE__ */
17193
17194/*
17195** Use standard C library malloc and free on non-Apple systems.
17196** Also used by Apple systems if SQLITE_WITHOUT_ZONEMALLOC is defined.
17197*/
17198#define SQLITE_MALLOC(x)             malloc(x)
17199#define SQLITE_FREE(x)               free(x)
17200#define SQLITE_REALLOC(x,y)          realloc((x),(y))
17201
17202/*
17203** The malloc.h header file is needed for malloc_usable_size() function
17204** on some systems (e.g. Linux).
17205*/
17206#if HAVE_MALLOC_H && HAVE_MALLOC_USABLE_SIZE
17207#  define SQLITE_USE_MALLOC_H 1
17208#  define SQLITE_USE_MALLOC_USABLE_SIZE 1
17209/*
17210** The MSVCRT has malloc_usable_size(), but it is called _msize().  The
17211** use of _msize() is automatic, but can be disabled by compiling with
17212** -DSQLITE_WITHOUT_MSIZE.  Using the _msize() function also requires
17213** the malloc.h header file.
17214*/
17215#elif defined(_MSC_VER) && !defined(SQLITE_WITHOUT_MSIZE)
17216#  define SQLITE_USE_MALLOC_H
17217#  define SQLITE_USE_MSIZE
17218#endif
17219
17220/*
17221** Include the malloc.h header file, if necessary.  Also set define macro
17222** SQLITE_MALLOCSIZE to the appropriate function name, which is _msize()
17223** for MSVC and malloc_usable_size() for most other systems (e.g. Linux).
17224** The memory size function can always be overridden manually by defining
17225** the macro SQLITE_MALLOCSIZE to the desired function name.
17226*/
17227#if defined(SQLITE_USE_MALLOC_H)
17228#  include <malloc.h>
17229#  if defined(SQLITE_USE_MALLOC_USABLE_SIZE)
17230#    if !defined(SQLITE_MALLOCSIZE)
17231#      define SQLITE_MALLOCSIZE(x)   malloc_usable_size(x)
17232#    endif
17233#  elif defined(SQLITE_USE_MSIZE)
17234#    if !defined(SQLITE_MALLOCSIZE)
17235#      define SQLITE_MALLOCSIZE      _msize
17236#    endif
17237#  endif
17238#endif /* defined(SQLITE_USE_MALLOC_H) */
17239
17240#endif /* __APPLE__ or not __APPLE__ */
17241
17242/*
17243** Like malloc(), but remember the size of the allocation
17244** so that we can find it later using sqlite3MemSize().
17245**
17246** For this low-level routine, we are guaranteed that nByte>0 because
17247** cases of nByte<=0 will be intercepted and dealt with by higher level
17248** routines.
17249*/
17250static void *sqlite3MemMalloc(int nByte){
17251#ifdef SQLITE_MALLOCSIZE
17252  void *p = SQLITE_MALLOC( nByte );
17253  if( p==0 ){
17254    testcase( sqlite3GlobalConfig.xLog!=0 );
17255    sqlite3_log(SQLITE_NOMEM, "failed to allocate %u bytes of memory", nByte);
17256  }
17257  return p;
17258#else
17259  sqlite3_int64 *p;
17260  assert( nByte>0 );
17261  nByte = ROUND8(nByte);
17262  p = SQLITE_MALLOC( nByte+8 );
17263  if( p ){
17264    p[0] = nByte;
17265    p++;
17266  }else{
17267    testcase( sqlite3GlobalConfig.xLog!=0 );
17268    sqlite3_log(SQLITE_NOMEM, "failed to allocate %u bytes of memory", nByte);
17269  }
17270  return (void *)p;
17271#endif
17272}
17273
17274/*
17275** Like free() but works for allocations obtained from sqlite3MemMalloc()
17276** or sqlite3MemRealloc().
17277**
17278** For this low-level routine, we already know that pPrior!=0 since
17279** cases where pPrior==0 will have been intecepted and dealt with
17280** by higher-level routines.
17281*/
17282static void sqlite3MemFree(void *pPrior){
17283#ifdef SQLITE_MALLOCSIZE
17284  SQLITE_FREE(pPrior);
17285#else
17286  sqlite3_int64 *p = (sqlite3_int64*)pPrior;
17287  assert( pPrior!=0 );
17288  p--;
17289  SQLITE_FREE(p);
17290#endif
17291}
17292
17293/*
17294** Report the allocated size of a prior return from xMalloc()
17295** or xRealloc().
17296*/
17297static int sqlite3MemSize(void *pPrior){
17298#ifdef SQLITE_MALLOCSIZE
17299  return pPrior ? (int)SQLITE_MALLOCSIZE(pPrior) : 0;
17300#else
17301  sqlite3_int64 *p;
17302  if( pPrior==0 ) return 0;
17303  p = (sqlite3_int64*)pPrior;
17304  p--;
17305  return (int)p[0];
17306#endif
17307}
17308
17309/*
17310** Like realloc().  Resize an allocation previously obtained from
17311** sqlite3MemMalloc().
17312**
17313** For this low-level interface, we know that pPrior!=0.  Cases where
17314** pPrior==0 while have been intercepted by higher-level routine and
17315** redirected to xMalloc.  Similarly, we know that nByte>0 because
17316** cases where nByte<=0 will have been intercepted by higher-level
17317** routines and redirected to xFree.
17318*/
17319static void *sqlite3MemRealloc(void *pPrior, int nByte){
17320#ifdef SQLITE_MALLOCSIZE
17321  void *p = SQLITE_REALLOC(pPrior, nByte);
17322  if( p==0 ){
17323    testcase( sqlite3GlobalConfig.xLog!=0 );
17324    sqlite3_log(SQLITE_NOMEM,
17325      "failed memory resize %u to %u bytes",
17326      SQLITE_MALLOCSIZE(pPrior), nByte);
17327  }
17328  return p;
17329#else
17330  sqlite3_int64 *p = (sqlite3_int64*)pPrior;
17331  assert( pPrior!=0 && nByte>0 );
17332  assert( nByte==ROUND8(nByte) ); /* EV: R-46199-30249 */
17333  p--;
17334  p = SQLITE_REALLOC(p, nByte+8 );
17335  if( p ){
17336    p[0] = nByte;
17337    p++;
17338  }else{
17339    testcase( sqlite3GlobalConfig.xLog!=0 );
17340    sqlite3_log(SQLITE_NOMEM,
17341      "failed memory resize %u to %u bytes",
17342      sqlite3MemSize(pPrior), nByte);
17343  }
17344  return (void*)p;
17345#endif
17346}
17347
17348/*
17349** Round up a request size to the next valid allocation size.
17350*/
17351static int sqlite3MemRoundup(int n){
17352  return ROUND8(n);
17353}
17354
17355/*
17356** Initialize this module.
17357*/
17358static int sqlite3MemInit(void *NotUsed){
17359#if defined(__APPLE__) && !defined(SQLITE_WITHOUT_ZONEMALLOC)
17360  int cpuCount;
17361  size_t len;
17362  if( _sqliteZone_ ){
17363    return SQLITE_OK;
17364  }
17365  len = sizeof(cpuCount);
17366  /* One usually wants to use hw.acctivecpu for MT decisions, but not here */
17367  sysctlbyname("hw.ncpu", &cpuCount, &len, NULL, 0);
17368  if( cpuCount>1 ){
17369    /* defer MT decisions to system malloc */
17370    _sqliteZone_ = malloc_default_zone();
17371  }else{
17372    /* only 1 core, use our own zone to contention over global locks,
17373    ** e.g. we have our own dedicated locks */
17374    bool success;
17375    malloc_zone_t* newzone = malloc_create_zone(4096, 0);
17376    malloc_set_zone_name(newzone, "Sqlite_Heap");
17377    do{
17378      success = OSAtomicCompareAndSwapPtrBarrier(NULL, newzone,
17379                                 (void * volatile *)&_sqliteZone_);
17380    }while(!_sqliteZone_);
17381    if( !success ){
17382      /* somebody registered a zone first */
17383      malloc_destroy_zone(newzone);
17384    }
17385  }
17386#endif
17387  UNUSED_PARAMETER(NotUsed);
17388  return SQLITE_OK;
17389}
17390
17391/*
17392** Deinitialize this module.
17393*/
17394static void sqlite3MemShutdown(void *NotUsed){
17395  UNUSED_PARAMETER(NotUsed);
17396  return;
17397}
17398
17399/*
17400** This routine is the only routine in this file with external linkage.
17401**
17402** Populate the low-level memory allocation function pointers in
17403** sqlite3GlobalConfig.m with pointers to the routines in this file.
17404*/
17405SQLITE_PRIVATE void sqlite3MemSetDefault(void){
17406  static const sqlite3_mem_methods defaultMethods = {
17407     sqlite3MemMalloc,
17408     sqlite3MemFree,
17409     sqlite3MemRealloc,
17410     sqlite3MemSize,
17411     sqlite3MemRoundup,
17412     sqlite3MemInit,
17413     sqlite3MemShutdown,
17414     0
17415  };
17416  sqlite3_config(SQLITE_CONFIG_MALLOC, &defaultMethods);
17417}
17418
17419#endif /* SQLITE_SYSTEM_MALLOC */
17420
17421/************** End of mem1.c ************************************************/
17422/************** Begin file mem2.c ********************************************/
17423/*
17424** 2007 August 15
17425**
17426** The author disclaims copyright to this source code.  In place of
17427** a legal notice, here is a blessing:
17428**
17429**    May you do good and not evil.
17430**    May you find forgiveness for yourself and forgive others.
17431**    May you share freely, never taking more than you give.
17432**
17433*************************************************************************
17434**
17435** This file contains low-level memory allocation drivers for when
17436** SQLite will use the standard C-library malloc/realloc/free interface
17437** to obtain the memory it needs while adding lots of additional debugging
17438** information to each allocation in order to help detect and fix memory
17439** leaks and memory usage errors.
17440**
17441** This file contains implementations of the low-level memory allocation
17442** routines specified in the sqlite3_mem_methods object.
17443*/
17444/* #include "sqliteInt.h" */
17445
17446/*
17447** This version of the memory allocator is used only if the
17448** SQLITE_MEMDEBUG macro is defined
17449*/
17450#ifdef SQLITE_MEMDEBUG
17451
17452/*
17453** The backtrace functionality is only available with GLIBC
17454*/
17455#ifdef __GLIBC__
17456  extern int backtrace(void**,int);
17457  extern void backtrace_symbols_fd(void*const*,int,int);
17458#else
17459# define backtrace(A,B) 1
17460# define backtrace_symbols_fd(A,B,C)
17461#endif
17462/* #include <stdio.h> */
17463
17464/*
17465** Each memory allocation looks like this:
17466**
17467**  ------------------------------------------------------------------------
17468**  | Title |  backtrace pointers |  MemBlockHdr |  allocation |  EndGuard |
17469**  ------------------------------------------------------------------------
17470**
17471** The application code sees only a pointer to the allocation.  We have
17472** to back up from the allocation pointer to find the MemBlockHdr.  The
17473** MemBlockHdr tells us the size of the allocation and the number of
17474** backtrace pointers.  There is also a guard word at the end of the
17475** MemBlockHdr.
17476*/
17477struct MemBlockHdr {
17478  i64 iSize;                          /* Size of this allocation */
17479  struct MemBlockHdr *pNext, *pPrev;  /* Linked list of all unfreed memory */
17480  char nBacktrace;                    /* Number of backtraces on this alloc */
17481  char nBacktraceSlots;               /* Available backtrace slots */
17482  u8 nTitle;                          /* Bytes of title; includes '\0' */
17483  u8 eType;                           /* Allocation type code */
17484  int iForeGuard;                     /* Guard word for sanity */
17485};
17486
17487/*
17488** Guard words
17489*/
17490#define FOREGUARD 0x80F5E153
17491#define REARGUARD 0xE4676B53
17492
17493/*
17494** Number of malloc size increments to track.
17495*/
17496#define NCSIZE  1000
17497
17498/*
17499** All of the static variables used by this module are collected
17500** into a single structure named "mem".  This is to keep the
17501** static variables organized and to reduce namespace pollution
17502** when this module is combined with other in the amalgamation.
17503*/
17504static struct {
17505
17506  /*
17507  ** Mutex to control access to the memory allocation subsystem.
17508  */
17509  sqlite3_mutex *mutex;
17510
17511  /*
17512  ** Head and tail of a linked list of all outstanding allocations
17513  */
17514  struct MemBlockHdr *pFirst;
17515  struct MemBlockHdr *pLast;
17516
17517  /*
17518  ** The number of levels of backtrace to save in new allocations.
17519  */
17520  int nBacktrace;
17521  void (*xBacktrace)(int, int, void **);
17522
17523  /*
17524  ** Title text to insert in front of each block
17525  */
17526  int nTitle;        /* Bytes of zTitle to save.  Includes '\0' and padding */
17527  char zTitle[100];  /* The title text */
17528
17529  /*
17530  ** sqlite3MallocDisallow() increments the following counter.
17531  ** sqlite3MallocAllow() decrements it.
17532  */
17533  int disallow; /* Do not allow memory allocation */
17534
17535  /*
17536  ** Gather statistics on the sizes of memory allocations.
17537  ** nAlloc[i] is the number of allocation attempts of i*8
17538  ** bytes.  i==NCSIZE is the number of allocation attempts for
17539  ** sizes more than NCSIZE*8 bytes.
17540  */
17541  int nAlloc[NCSIZE];      /* Total number of allocations */
17542  int nCurrent[NCSIZE];    /* Current number of allocations */
17543  int mxCurrent[NCSIZE];   /* Highwater mark for nCurrent */
17544
17545} mem;
17546
17547
17548/*
17549** Adjust memory usage statistics
17550*/
17551static void adjustStats(int iSize, int increment){
17552  int i = ROUND8(iSize)/8;
17553  if( i>NCSIZE-1 ){
17554    i = NCSIZE - 1;
17555  }
17556  if( increment>0 ){
17557    mem.nAlloc[i]++;
17558    mem.nCurrent[i]++;
17559    if( mem.nCurrent[i]>mem.mxCurrent[i] ){
17560      mem.mxCurrent[i] = mem.nCurrent[i];
17561    }
17562  }else{
17563    mem.nCurrent[i]--;
17564    assert( mem.nCurrent[i]>=0 );
17565  }
17566}
17567
17568/*
17569** Given an allocation, find the MemBlockHdr for that allocation.
17570**
17571** This routine checks the guards at either end of the allocation and
17572** if they are incorrect it asserts.
17573*/
17574static struct MemBlockHdr *sqlite3MemsysGetHeader(void *pAllocation){
17575  struct MemBlockHdr *p;
17576  int *pInt;
17577  u8 *pU8;
17578  int nReserve;
17579
17580  p = (struct MemBlockHdr*)pAllocation;
17581  p--;
17582  assert( p->iForeGuard==(int)FOREGUARD );
17583  nReserve = ROUND8(p->iSize);
17584  pInt = (int*)pAllocation;
17585  pU8 = (u8*)pAllocation;
17586  assert( pInt[nReserve/sizeof(int)]==(int)REARGUARD );
17587  /* This checks any of the "extra" bytes allocated due
17588  ** to rounding up to an 8 byte boundary to ensure
17589  ** they haven't been overwritten.
17590  */
17591  while( nReserve-- > p->iSize ) assert( pU8[nReserve]==0x65 );
17592  return p;
17593}
17594
17595/*
17596** Return the number of bytes currently allocated at address p.
17597*/
17598static int sqlite3MemSize(void *p){
17599  struct MemBlockHdr *pHdr;
17600  if( !p ){
17601    return 0;
17602  }
17603  pHdr = sqlite3MemsysGetHeader(p);
17604  return (int)pHdr->iSize;
17605}
17606
17607/*
17608** Initialize the memory allocation subsystem.
17609*/
17610static int sqlite3MemInit(void *NotUsed){
17611  UNUSED_PARAMETER(NotUsed);
17612  assert( (sizeof(struct MemBlockHdr)&7) == 0 );
17613  if( !sqlite3GlobalConfig.bMemstat ){
17614    /* If memory status is enabled, then the malloc.c wrapper will already
17615    ** hold the STATIC_MEM mutex when the routines here are invoked. */
17616    mem.mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MEM);
17617  }
17618  return SQLITE_OK;
17619}
17620
17621/*
17622** Deinitialize the memory allocation subsystem.
17623*/
17624static void sqlite3MemShutdown(void *NotUsed){
17625  UNUSED_PARAMETER(NotUsed);
17626  mem.mutex = 0;
17627}
17628
17629/*
17630** Round up a request size to the next valid allocation size.
17631*/
17632static int sqlite3MemRoundup(int n){
17633  return ROUND8(n);
17634}
17635
17636/*
17637** Fill a buffer with pseudo-random bytes.  This is used to preset
17638** the content of a new memory allocation to unpredictable values and
17639** to clear the content of a freed allocation to unpredictable values.
17640*/
17641static void randomFill(char *pBuf, int nByte){
17642  unsigned int x, y, r;
17643  x = SQLITE_PTR_TO_INT(pBuf);
17644  y = nByte | 1;
17645  while( nByte >= 4 ){
17646    x = (x>>1) ^ (-(int)(x&1) & 0xd0000001);
17647    y = y*1103515245 + 12345;
17648    r = x ^ y;
17649    *(int*)pBuf = r;
17650    pBuf += 4;
17651    nByte -= 4;
17652  }
17653  while( nByte-- > 0 ){
17654    x = (x>>1) ^ (-(int)(x&1) & 0xd0000001);
17655    y = y*1103515245 + 12345;
17656    r = x ^ y;
17657    *(pBuf++) = r & 0xff;
17658  }
17659}
17660
17661/*
17662** Allocate nByte bytes of memory.
17663*/
17664static void *sqlite3MemMalloc(int nByte){
17665  struct MemBlockHdr *pHdr;
17666  void **pBt;
17667  char *z;
17668  int *pInt;
17669  void *p = 0;
17670  int totalSize;
17671  int nReserve;
17672  sqlite3_mutex_enter(mem.mutex);
17673  assert( mem.disallow==0 );
17674  nReserve = ROUND8(nByte);
17675  totalSize = nReserve + sizeof(*pHdr) + sizeof(int) +
17676               mem.nBacktrace*sizeof(void*) + mem.nTitle;
17677  p = malloc(totalSize);
17678  if( p ){
17679    z = p;
17680    pBt = (void**)&z[mem.nTitle];
17681    pHdr = (struct MemBlockHdr*)&pBt[mem.nBacktrace];
17682    pHdr->pNext = 0;
17683    pHdr->pPrev = mem.pLast;
17684    if( mem.pLast ){
17685      mem.pLast->pNext = pHdr;
17686    }else{
17687      mem.pFirst = pHdr;
17688    }
17689    mem.pLast = pHdr;
17690    pHdr->iForeGuard = FOREGUARD;
17691    pHdr->eType = MEMTYPE_HEAP;
17692    pHdr->nBacktraceSlots = mem.nBacktrace;
17693    pHdr->nTitle = mem.nTitle;
17694    if( mem.nBacktrace ){
17695      void *aAddr[40];
17696      pHdr->nBacktrace = backtrace(aAddr, mem.nBacktrace+1)-1;
17697      memcpy(pBt, &aAddr[1], pHdr->nBacktrace*sizeof(void*));
17698      assert(pBt[0]);
17699      if( mem.xBacktrace ){
17700        mem.xBacktrace(nByte, pHdr->nBacktrace-1, &aAddr[1]);
17701      }
17702    }else{
17703      pHdr->nBacktrace = 0;
17704    }
17705    if( mem.nTitle ){
17706      memcpy(z, mem.zTitle, mem.nTitle);
17707    }
17708    pHdr->iSize = nByte;
17709    adjustStats(nByte, +1);
17710    pInt = (int*)&pHdr[1];
17711    pInt[nReserve/sizeof(int)] = REARGUARD;
17712    randomFill((char*)pInt, nByte);
17713    memset(((char*)pInt)+nByte, 0x65, nReserve-nByte);
17714    p = (void*)pInt;
17715  }
17716  sqlite3_mutex_leave(mem.mutex);
17717  return p;
17718}
17719
17720/*
17721** Free memory.
17722*/
17723static void sqlite3MemFree(void *pPrior){
17724  struct MemBlockHdr *pHdr;
17725  void **pBt;
17726  char *z;
17727  assert( sqlite3GlobalConfig.bMemstat || sqlite3GlobalConfig.bCoreMutex==0
17728       || mem.mutex!=0 );
17729  pHdr = sqlite3MemsysGetHeader(pPrior);
17730  pBt = (void**)pHdr;
17731  pBt -= pHdr->nBacktraceSlots;
17732  sqlite3_mutex_enter(mem.mutex);
17733  if( pHdr->pPrev ){
17734    assert( pHdr->pPrev->pNext==pHdr );
17735    pHdr->pPrev->pNext = pHdr->pNext;
17736  }else{
17737    assert( mem.pFirst==pHdr );
17738    mem.pFirst = pHdr->pNext;
17739  }
17740  if( pHdr->pNext ){
17741    assert( pHdr->pNext->pPrev==pHdr );
17742    pHdr->pNext->pPrev = pHdr->pPrev;
17743  }else{
17744    assert( mem.pLast==pHdr );
17745    mem.pLast = pHdr->pPrev;
17746  }
17747  z = (char*)pBt;
17748  z -= pHdr->nTitle;
17749  adjustStats((int)pHdr->iSize, -1);
17750  randomFill(z, sizeof(void*)*pHdr->nBacktraceSlots + sizeof(*pHdr) +
17751                (int)pHdr->iSize + sizeof(int) + pHdr->nTitle);
17752  free(z);
17753  sqlite3_mutex_leave(mem.mutex);
17754}
17755
17756/*
17757** Change the size of an existing memory allocation.
17758**
17759** For this debugging implementation, we *always* make a copy of the
17760** allocation into a new place in memory.  In this way, if the
17761** higher level code is using pointer to the old allocation, it is
17762** much more likely to break and we are much more liking to find
17763** the error.
17764*/
17765static void *sqlite3MemRealloc(void *pPrior, int nByte){
17766  struct MemBlockHdr *pOldHdr;
17767  void *pNew;
17768  assert( mem.disallow==0 );
17769  assert( (nByte & 7)==0 );     /* EV: R-46199-30249 */
17770  pOldHdr = sqlite3MemsysGetHeader(pPrior);
17771  pNew = sqlite3MemMalloc(nByte);
17772  if( pNew ){
17773    memcpy(pNew, pPrior, (int)(nByte<pOldHdr->iSize ? nByte : pOldHdr->iSize));
17774    if( nByte>pOldHdr->iSize ){
17775      randomFill(&((char*)pNew)[pOldHdr->iSize], nByte - (int)pOldHdr->iSize);
17776    }
17777    sqlite3MemFree(pPrior);
17778  }
17779  return pNew;
17780}
17781
17782/*
17783** Populate the low-level memory allocation function pointers in
17784** sqlite3GlobalConfig.m with pointers to the routines in this file.
17785*/
17786SQLITE_PRIVATE void sqlite3MemSetDefault(void){
17787  static const sqlite3_mem_methods defaultMethods = {
17788     sqlite3MemMalloc,
17789     sqlite3MemFree,
17790     sqlite3MemRealloc,
17791     sqlite3MemSize,
17792     sqlite3MemRoundup,
17793     sqlite3MemInit,
17794     sqlite3MemShutdown,
17795     0
17796  };
17797  sqlite3_config(SQLITE_CONFIG_MALLOC, &defaultMethods);
17798}
17799
17800/*
17801** Set the "type" of an allocation.
17802*/
17803SQLITE_PRIVATE void sqlite3MemdebugSetType(void *p, u8 eType){
17804  if( p && sqlite3GlobalConfig.m.xMalloc==sqlite3MemMalloc ){
17805    struct MemBlockHdr *pHdr;
17806    pHdr = sqlite3MemsysGetHeader(p);
17807    assert( pHdr->iForeGuard==FOREGUARD );
17808    pHdr->eType = eType;
17809  }
17810}
17811
17812/*
17813** Return TRUE if the mask of type in eType matches the type of the
17814** allocation p.  Also return true if p==NULL.
17815**
17816** This routine is designed for use within an assert() statement, to
17817** verify the type of an allocation.  For example:
17818**
17819**     assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) );
17820*/
17821SQLITE_PRIVATE int sqlite3MemdebugHasType(void *p, u8 eType){
17822  int rc = 1;
17823  if( p && sqlite3GlobalConfig.m.xMalloc==sqlite3MemMalloc ){
17824    struct MemBlockHdr *pHdr;
17825    pHdr = sqlite3MemsysGetHeader(p);
17826    assert( pHdr->iForeGuard==FOREGUARD );         /* Allocation is valid */
17827    if( (pHdr->eType&eType)==0 ){
17828      rc = 0;
17829    }
17830  }
17831  return rc;
17832}
17833
17834/*
17835** Return TRUE if the mask of type in eType matches no bits of the type of the
17836** allocation p.  Also return true if p==NULL.
17837**
17838** This routine is designed for use within an assert() statement, to
17839** verify the type of an allocation.  For example:
17840**
17841**     assert( sqlite3MemdebugNoType(p, MEMTYPE_LOOKASIDE) );
17842*/
17843SQLITE_PRIVATE int sqlite3MemdebugNoType(void *p, u8 eType){
17844  int rc = 1;
17845  if( p && sqlite3GlobalConfig.m.xMalloc==sqlite3MemMalloc ){
17846    struct MemBlockHdr *pHdr;
17847    pHdr = sqlite3MemsysGetHeader(p);
17848    assert( pHdr->iForeGuard==FOREGUARD );         /* Allocation is valid */
17849    if( (pHdr->eType&eType)!=0 ){
17850      rc = 0;
17851    }
17852  }
17853  return rc;
17854}
17855
17856/*
17857** Set the number of backtrace levels kept for each allocation.
17858** A value of zero turns off backtracing.  The number is always rounded
17859** up to a multiple of 2.
17860*/
17861SQLITE_PRIVATE void sqlite3MemdebugBacktrace(int depth){
17862  if( depth<0 ){ depth = 0; }
17863  if( depth>20 ){ depth = 20; }
17864  depth = (depth+1)&0xfe;
17865  mem.nBacktrace = depth;
17866}
17867
17868SQLITE_PRIVATE void sqlite3MemdebugBacktraceCallback(void (*xBacktrace)(int, int, void **)){
17869  mem.xBacktrace = xBacktrace;
17870}
17871
17872/*
17873** Set the title string for subsequent allocations.
17874*/
17875SQLITE_PRIVATE void sqlite3MemdebugSettitle(const char *zTitle){
17876  unsigned int n = sqlite3Strlen30(zTitle) + 1;
17877  sqlite3_mutex_enter(mem.mutex);
17878  if( n>=sizeof(mem.zTitle) ) n = sizeof(mem.zTitle)-1;
17879  memcpy(mem.zTitle, zTitle, n);
17880  mem.zTitle[n] = 0;
17881  mem.nTitle = ROUND8(n);
17882  sqlite3_mutex_leave(mem.mutex);
17883}
17884
17885SQLITE_PRIVATE void sqlite3MemdebugSync(){
17886  struct MemBlockHdr *pHdr;
17887  for(pHdr=mem.pFirst; pHdr; pHdr=pHdr->pNext){
17888    void **pBt = (void**)pHdr;
17889    pBt -= pHdr->nBacktraceSlots;
17890    mem.xBacktrace((int)pHdr->iSize, pHdr->nBacktrace-1, &pBt[1]);
17891  }
17892}
17893
17894/*
17895** Open the file indicated and write a log of all unfreed memory
17896** allocations into that log.
17897*/
17898SQLITE_PRIVATE void sqlite3MemdebugDump(const char *zFilename){
17899  FILE *out;
17900  struct MemBlockHdr *pHdr;
17901  void **pBt;
17902  int i;
17903  out = fopen(zFilename, "w");
17904  if( out==0 ){
17905    fprintf(stderr, "** Unable to output memory debug output log: %s **\n",
17906                    zFilename);
17907    return;
17908  }
17909  for(pHdr=mem.pFirst; pHdr; pHdr=pHdr->pNext){
17910    char *z = (char*)pHdr;
17911    z -= pHdr->nBacktraceSlots*sizeof(void*) + pHdr->nTitle;
17912    fprintf(out, "**** %lld bytes at %p from %s ****\n",
17913            pHdr->iSize, &pHdr[1], pHdr->nTitle ? z : "???");
17914    if( pHdr->nBacktrace ){
17915      fflush(out);
17916      pBt = (void**)pHdr;
17917      pBt -= pHdr->nBacktraceSlots;
17918      backtrace_symbols_fd(pBt, pHdr->nBacktrace, fileno(out));
17919      fprintf(out, "\n");
17920    }
17921  }
17922  fprintf(out, "COUNTS:\n");
17923  for(i=0; i<NCSIZE-1; i++){
17924    if( mem.nAlloc[i] ){
17925      fprintf(out, "   %5d: %10d %10d %10d\n",
17926            i*8, mem.nAlloc[i], mem.nCurrent[i], mem.mxCurrent[i]);
17927    }
17928  }
17929  if( mem.nAlloc[NCSIZE-1] ){
17930    fprintf(out, "   %5d: %10d %10d %10d\n",
17931             NCSIZE*8-8, mem.nAlloc[NCSIZE-1],
17932             mem.nCurrent[NCSIZE-1], mem.mxCurrent[NCSIZE-1]);
17933  }
17934  fclose(out);
17935}
17936
17937/*
17938** Return the number of times sqlite3MemMalloc() has been called.
17939*/
17940SQLITE_PRIVATE int sqlite3MemdebugMallocCount(){
17941  int i;
17942  int nTotal = 0;
17943  for(i=0; i<NCSIZE; i++){
17944    nTotal += mem.nAlloc[i];
17945  }
17946  return nTotal;
17947}
17948
17949
17950#endif /* SQLITE_MEMDEBUG */
17951
17952/************** End of mem2.c ************************************************/
17953/************** Begin file mem3.c ********************************************/
17954/*
17955** 2007 October 14
17956**
17957** The author disclaims copyright to this source code.  In place of
17958** a legal notice, here is a blessing:
17959**
17960**    May you do good and not evil.
17961**    May you find forgiveness for yourself and forgive others.
17962**    May you share freely, never taking more than you give.
17963**
17964*************************************************************************
17965** This file contains the C functions that implement a memory
17966** allocation subsystem for use by SQLite.
17967**
17968** This version of the memory allocation subsystem omits all
17969** use of malloc(). The SQLite user supplies a block of memory
17970** before calling sqlite3_initialize() from which allocations
17971** are made and returned by the xMalloc() and xRealloc()
17972** implementations. Once sqlite3_initialize() has been called,
17973** the amount of memory available to SQLite is fixed and cannot
17974** be changed.
17975**
17976** This version of the memory allocation subsystem is included
17977** in the build only if SQLITE_ENABLE_MEMSYS3 is defined.
17978*/
17979/* #include "sqliteInt.h" */
17980
17981/*
17982** This version of the memory allocator is only built into the library
17983** SQLITE_ENABLE_MEMSYS3 is defined. Defining this symbol does not
17984** mean that the library will use a memory-pool by default, just that
17985** it is available. The mempool allocator is activated by calling
17986** sqlite3_config().
17987*/
17988#ifdef SQLITE_ENABLE_MEMSYS3
17989
17990/*
17991** Maximum size (in Mem3Blocks) of a "small" chunk.
17992*/
17993#define MX_SMALL 10
17994
17995
17996/*
17997** Number of freelist hash slots
17998*/
17999#define N_HASH  61
18000
18001/*
18002** A memory allocation (also called a "chunk") consists of two or
18003** more blocks where each block is 8 bytes.  The first 8 bytes are
18004** a header that is not returned to the user.
18005**
18006** A chunk is two or more blocks that is either checked out or
18007** free.  The first block has format u.hdr.  u.hdr.size4x is 4 times the
18008** size of the allocation in blocks if the allocation is free.
18009** The u.hdr.size4x&1 bit is true if the chunk is checked out and
18010** false if the chunk is on the freelist.  The u.hdr.size4x&2 bit
18011** is true if the previous chunk is checked out and false if the
18012** previous chunk is free.  The u.hdr.prevSize field is the size of
18013** the previous chunk in blocks if the previous chunk is on the
18014** freelist. If the previous chunk is checked out, then
18015** u.hdr.prevSize can be part of the data for that chunk and should
18016** not be read or written.
18017**
18018** We often identify a chunk by its index in mem3.aPool[].  When
18019** this is done, the chunk index refers to the second block of
18020** the chunk.  In this way, the first chunk has an index of 1.
18021** A chunk index of 0 means "no such chunk" and is the equivalent
18022** of a NULL pointer.
18023**
18024** The second block of free chunks is of the form u.list.  The
18025** two fields form a double-linked list of chunks of related sizes.
18026** Pointers to the head of the list are stored in mem3.aiSmall[]
18027** for smaller chunks and mem3.aiHash[] for larger chunks.
18028**
18029** The second block of a chunk is user data if the chunk is checked
18030** out.  If a chunk is checked out, the user data may extend into
18031** the u.hdr.prevSize value of the following chunk.
18032*/
18033typedef struct Mem3Block Mem3Block;
18034struct Mem3Block {
18035  union {
18036    struct {
18037      u32 prevSize;   /* Size of previous chunk in Mem3Block elements */
18038      u32 size4x;     /* 4x the size of current chunk in Mem3Block elements */
18039    } hdr;
18040    struct {
18041      u32 next;       /* Index in mem3.aPool[] of next free chunk */
18042      u32 prev;       /* Index in mem3.aPool[] of previous free chunk */
18043    } list;
18044  } u;
18045};
18046
18047/*
18048** All of the static variables used by this module are collected
18049** into a single structure named "mem3".  This is to keep the
18050** static variables organized and to reduce namespace pollution
18051** when this module is combined with other in the amalgamation.
18052*/
18053static SQLITE_WSD struct Mem3Global {
18054  /*
18055  ** Memory available for allocation. nPool is the size of the array
18056  ** (in Mem3Blocks) pointed to by aPool less 2.
18057  */
18058  u32 nPool;
18059  Mem3Block *aPool;
18060
18061  /*
18062  ** True if we are evaluating an out-of-memory callback.
18063  */
18064  int alarmBusy;
18065
18066  /*
18067  ** Mutex to control access to the memory allocation subsystem.
18068  */
18069  sqlite3_mutex *mutex;
18070
18071  /*
18072  ** The minimum amount of free space that we have seen.
18073  */
18074  u32 mnMaster;
18075
18076  /*
18077  ** iMaster is the index of the master chunk.  Most new allocations
18078  ** occur off of this chunk.  szMaster is the size (in Mem3Blocks)
18079  ** of the current master.  iMaster is 0 if there is not master chunk.
18080  ** The master chunk is not in either the aiHash[] or aiSmall[].
18081  */
18082  u32 iMaster;
18083  u32 szMaster;
18084
18085  /*
18086  ** Array of lists of free blocks according to the block size
18087  ** for smaller chunks, or a hash on the block size for larger
18088  ** chunks.
18089  */
18090  u32 aiSmall[MX_SMALL-1];   /* For sizes 2 through MX_SMALL, inclusive */
18091  u32 aiHash[N_HASH];        /* For sizes MX_SMALL+1 and larger */
18092} mem3 = { 97535575 };
18093
18094#define mem3 GLOBAL(struct Mem3Global, mem3)
18095
18096/*
18097** Unlink the chunk at mem3.aPool[i] from list it is currently
18098** on.  *pRoot is the list that i is a member of.
18099*/
18100static void memsys3UnlinkFromList(u32 i, u32 *pRoot){
18101  u32 next = mem3.aPool[i].u.list.next;
18102  u32 prev = mem3.aPool[i].u.list.prev;
18103  assert( sqlite3_mutex_held(mem3.mutex) );
18104  if( prev==0 ){
18105    *pRoot = next;
18106  }else{
18107    mem3.aPool[prev].u.list.next = next;
18108  }
18109  if( next ){
18110    mem3.aPool[next].u.list.prev = prev;
18111  }
18112  mem3.aPool[i].u.list.next = 0;
18113  mem3.aPool[i].u.list.prev = 0;
18114}
18115
18116/*
18117** Unlink the chunk at index i from
18118** whatever list is currently a member of.
18119*/
18120static void memsys3Unlink(u32 i){
18121  u32 size, hash;
18122  assert( sqlite3_mutex_held(mem3.mutex) );
18123  assert( (mem3.aPool[i-1].u.hdr.size4x & 1)==0 );
18124  assert( i>=1 );
18125  size = mem3.aPool[i-1].u.hdr.size4x/4;
18126  assert( size==mem3.aPool[i+size-1].u.hdr.prevSize );
18127  assert( size>=2 );
18128  if( size <= MX_SMALL ){
18129    memsys3UnlinkFromList(i, &mem3.aiSmall[size-2]);
18130  }else{
18131    hash = size % N_HASH;
18132    memsys3UnlinkFromList(i, &mem3.aiHash[hash]);
18133  }
18134}
18135
18136/*
18137** Link the chunk at mem3.aPool[i] so that is on the list rooted
18138** at *pRoot.
18139*/
18140static void memsys3LinkIntoList(u32 i, u32 *pRoot){
18141  assert( sqlite3_mutex_held(mem3.mutex) );
18142  mem3.aPool[i].u.list.next = *pRoot;
18143  mem3.aPool[i].u.list.prev = 0;
18144  if( *pRoot ){
18145    mem3.aPool[*pRoot].u.list.prev = i;
18146  }
18147  *pRoot = i;
18148}
18149
18150/*
18151** Link the chunk at index i into either the appropriate
18152** small chunk list, or into the large chunk hash table.
18153*/
18154static void memsys3Link(u32 i){
18155  u32 size, hash;
18156  assert( sqlite3_mutex_held(mem3.mutex) );
18157  assert( i>=1 );
18158  assert( (mem3.aPool[i-1].u.hdr.size4x & 1)==0 );
18159  size = mem3.aPool[i-1].u.hdr.size4x/4;
18160  assert( size==mem3.aPool[i+size-1].u.hdr.prevSize );
18161  assert( size>=2 );
18162  if( size <= MX_SMALL ){
18163    memsys3LinkIntoList(i, &mem3.aiSmall[size-2]);
18164  }else{
18165    hash = size % N_HASH;
18166    memsys3LinkIntoList(i, &mem3.aiHash[hash]);
18167  }
18168}
18169
18170/*
18171** If the STATIC_MEM mutex is not already held, obtain it now. The mutex
18172** will already be held (obtained by code in malloc.c) if
18173** sqlite3GlobalConfig.bMemStat is true.
18174*/
18175static void memsys3Enter(void){
18176  if( sqlite3GlobalConfig.bMemstat==0 && mem3.mutex==0 ){
18177    mem3.mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MEM);
18178  }
18179  sqlite3_mutex_enter(mem3.mutex);
18180}
18181static void memsys3Leave(void){
18182  sqlite3_mutex_leave(mem3.mutex);
18183}
18184
18185/*
18186** Called when we are unable to satisfy an allocation of nBytes.
18187*/
18188static void memsys3OutOfMemory(int nByte){
18189  if( !mem3.alarmBusy ){
18190    mem3.alarmBusy = 1;
18191    assert( sqlite3_mutex_held(mem3.mutex) );
18192    sqlite3_mutex_leave(mem3.mutex);
18193    sqlite3_release_memory(nByte);
18194    sqlite3_mutex_enter(mem3.mutex);
18195    mem3.alarmBusy = 0;
18196  }
18197}
18198
18199
18200/*
18201** Chunk i is a free chunk that has been unlinked.  Adjust its
18202** size parameters for check-out and return a pointer to the
18203** user portion of the chunk.
18204*/
18205static void *memsys3Checkout(u32 i, u32 nBlock){
18206  u32 x;
18207  assert( sqlite3_mutex_held(mem3.mutex) );
18208  assert( i>=1 );
18209  assert( mem3.aPool[i-1].u.hdr.size4x/4==nBlock );
18210  assert( mem3.aPool[i+nBlock-1].u.hdr.prevSize==nBlock );
18211  x = mem3.aPool[i-1].u.hdr.size4x;
18212  mem3.aPool[i-1].u.hdr.size4x = nBlock*4 | 1 | (x&2);
18213  mem3.aPool[i+nBlock-1].u.hdr.prevSize = nBlock;
18214  mem3.aPool[i+nBlock-1].u.hdr.size4x |= 2;
18215  return &mem3.aPool[i];
18216}
18217
18218/*
18219** Carve a piece off of the end of the mem3.iMaster free chunk.
18220** Return a pointer to the new allocation.  Or, if the master chunk
18221** is not large enough, return 0.
18222*/
18223static void *memsys3FromMaster(u32 nBlock){
18224  assert( sqlite3_mutex_held(mem3.mutex) );
18225  assert( mem3.szMaster>=nBlock );
18226  if( nBlock>=mem3.szMaster-1 ){
18227    /* Use the entire master */
18228    void *p = memsys3Checkout(mem3.iMaster, mem3.szMaster);
18229    mem3.iMaster = 0;
18230    mem3.szMaster = 0;
18231    mem3.mnMaster = 0;
18232    return p;
18233  }else{
18234    /* Split the master block.  Return the tail. */
18235    u32 newi, x;
18236    newi = mem3.iMaster + mem3.szMaster - nBlock;
18237    assert( newi > mem3.iMaster+1 );
18238    mem3.aPool[mem3.iMaster+mem3.szMaster-1].u.hdr.prevSize = nBlock;
18239    mem3.aPool[mem3.iMaster+mem3.szMaster-1].u.hdr.size4x |= 2;
18240    mem3.aPool[newi-1].u.hdr.size4x = nBlock*4 + 1;
18241    mem3.szMaster -= nBlock;
18242    mem3.aPool[newi-1].u.hdr.prevSize = mem3.szMaster;
18243    x = mem3.aPool[mem3.iMaster-1].u.hdr.size4x & 2;
18244    mem3.aPool[mem3.iMaster-1].u.hdr.size4x = mem3.szMaster*4 | x;
18245    if( mem3.szMaster < mem3.mnMaster ){
18246      mem3.mnMaster = mem3.szMaster;
18247    }
18248    return (void*)&mem3.aPool[newi];
18249  }
18250}
18251
18252/*
18253** *pRoot is the head of a list of free chunks of the same size
18254** or same size hash.  In other words, *pRoot is an entry in either
18255** mem3.aiSmall[] or mem3.aiHash[].
18256**
18257** This routine examines all entries on the given list and tries
18258** to coalesce each entries with adjacent free chunks.
18259**
18260** If it sees a chunk that is larger than mem3.iMaster, it replaces
18261** the current mem3.iMaster with the new larger chunk.  In order for
18262** this mem3.iMaster replacement to work, the master chunk must be
18263** linked into the hash tables.  That is not the normal state of
18264** affairs, of course.  The calling routine must link the master
18265** chunk before invoking this routine, then must unlink the (possibly
18266** changed) master chunk once this routine has finished.
18267*/
18268static void memsys3Merge(u32 *pRoot){
18269  u32 iNext, prev, size, i, x;
18270
18271  assert( sqlite3_mutex_held(mem3.mutex) );
18272  for(i=*pRoot; i>0; i=iNext){
18273    iNext = mem3.aPool[i].u.list.next;
18274    size = mem3.aPool[i-1].u.hdr.size4x;
18275    assert( (size&1)==0 );
18276    if( (size&2)==0 ){
18277      memsys3UnlinkFromList(i, pRoot);
18278      assert( i > mem3.aPool[i-1].u.hdr.prevSize );
18279      prev = i - mem3.aPool[i-1].u.hdr.prevSize;
18280      if( prev==iNext ){
18281        iNext = mem3.aPool[prev].u.list.next;
18282      }
18283      memsys3Unlink(prev);
18284      size = i + size/4 - prev;
18285      x = mem3.aPool[prev-1].u.hdr.size4x & 2;
18286      mem3.aPool[prev-1].u.hdr.size4x = size*4 | x;
18287      mem3.aPool[prev+size-1].u.hdr.prevSize = size;
18288      memsys3Link(prev);
18289      i = prev;
18290    }else{
18291      size /= 4;
18292    }
18293    if( size>mem3.szMaster ){
18294      mem3.iMaster = i;
18295      mem3.szMaster = size;
18296    }
18297  }
18298}
18299
18300/*
18301** Return a block of memory of at least nBytes in size.
18302** Return NULL if unable.
18303**
18304** This function assumes that the necessary mutexes, if any, are
18305** already held by the caller. Hence "Unsafe".
18306*/
18307static void *memsys3MallocUnsafe(int nByte){
18308  u32 i;
18309  u32 nBlock;
18310  u32 toFree;
18311
18312  assert( sqlite3_mutex_held(mem3.mutex) );
18313  assert( sizeof(Mem3Block)==8 );
18314  if( nByte<=12 ){
18315    nBlock = 2;
18316  }else{
18317    nBlock = (nByte + 11)/8;
18318  }
18319  assert( nBlock>=2 );
18320
18321  /* STEP 1:
18322  ** Look for an entry of the correct size in either the small
18323  ** chunk table or in the large chunk hash table.  This is
18324  ** successful most of the time (about 9 times out of 10).
18325  */
18326  if( nBlock <= MX_SMALL ){
18327    i = mem3.aiSmall[nBlock-2];
18328    if( i>0 ){
18329      memsys3UnlinkFromList(i, &mem3.aiSmall[nBlock-2]);
18330      return memsys3Checkout(i, nBlock);
18331    }
18332  }else{
18333    int hash = nBlock % N_HASH;
18334    for(i=mem3.aiHash[hash]; i>0; i=mem3.aPool[i].u.list.next){
18335      if( mem3.aPool[i-1].u.hdr.size4x/4==nBlock ){
18336        memsys3UnlinkFromList(i, &mem3.aiHash[hash]);
18337        return memsys3Checkout(i, nBlock);
18338      }
18339    }
18340  }
18341
18342  /* STEP 2:
18343  ** Try to satisfy the allocation by carving a piece off of the end
18344  ** of the master chunk.  This step usually works if step 1 fails.
18345  */
18346  if( mem3.szMaster>=nBlock ){
18347    return memsys3FromMaster(nBlock);
18348  }
18349
18350
18351  /* STEP 3:
18352  ** Loop through the entire memory pool.  Coalesce adjacent free
18353  ** chunks.  Recompute the master chunk as the largest free chunk.
18354  ** Then try again to satisfy the allocation by carving a piece off
18355  ** of the end of the master chunk.  This step happens very
18356  ** rarely (we hope!)
18357  */
18358  for(toFree=nBlock*16; toFree<(mem3.nPool*16); toFree *= 2){
18359    memsys3OutOfMemory(toFree);
18360    if( mem3.iMaster ){
18361      memsys3Link(mem3.iMaster);
18362      mem3.iMaster = 0;
18363      mem3.szMaster = 0;
18364    }
18365    for(i=0; i<N_HASH; i++){
18366      memsys3Merge(&mem3.aiHash[i]);
18367    }
18368    for(i=0; i<MX_SMALL-1; i++){
18369      memsys3Merge(&mem3.aiSmall[i]);
18370    }
18371    if( mem3.szMaster ){
18372      memsys3Unlink(mem3.iMaster);
18373      if( mem3.szMaster>=nBlock ){
18374        return memsys3FromMaster(nBlock);
18375      }
18376    }
18377  }
18378
18379  /* If none of the above worked, then we fail. */
18380  return 0;
18381}
18382
18383/*
18384** Free an outstanding memory allocation.
18385**
18386** This function assumes that the necessary mutexes, if any, are
18387** already held by the caller. Hence "Unsafe".
18388*/
18389static void memsys3FreeUnsafe(void *pOld){
18390  Mem3Block *p = (Mem3Block*)pOld;
18391  int i;
18392  u32 size, x;
18393  assert( sqlite3_mutex_held(mem3.mutex) );
18394  assert( p>mem3.aPool && p<&mem3.aPool[mem3.nPool] );
18395  i = p - mem3.aPool;
18396  assert( (mem3.aPool[i-1].u.hdr.size4x&1)==1 );
18397  size = mem3.aPool[i-1].u.hdr.size4x/4;
18398  assert( i+size<=mem3.nPool+1 );
18399  mem3.aPool[i-1].u.hdr.size4x &= ~1;
18400  mem3.aPool[i+size-1].u.hdr.prevSize = size;
18401  mem3.aPool[i+size-1].u.hdr.size4x &= ~2;
18402  memsys3Link(i);
18403
18404  /* Try to expand the master using the newly freed chunk */
18405  if( mem3.iMaster ){
18406    while( (mem3.aPool[mem3.iMaster-1].u.hdr.size4x&2)==0 ){
18407      size = mem3.aPool[mem3.iMaster-1].u.hdr.prevSize;
18408      mem3.iMaster -= size;
18409      mem3.szMaster += size;
18410      memsys3Unlink(mem3.iMaster);
18411      x = mem3.aPool[mem3.iMaster-1].u.hdr.size4x & 2;
18412      mem3.aPool[mem3.iMaster-1].u.hdr.size4x = mem3.szMaster*4 | x;
18413      mem3.aPool[mem3.iMaster+mem3.szMaster-1].u.hdr.prevSize = mem3.szMaster;
18414    }
18415    x = mem3.aPool[mem3.iMaster-1].u.hdr.size4x & 2;
18416    while( (mem3.aPool[mem3.iMaster+mem3.szMaster-1].u.hdr.size4x&1)==0 ){
18417      memsys3Unlink(mem3.iMaster+mem3.szMaster);
18418      mem3.szMaster += mem3.aPool[mem3.iMaster+mem3.szMaster-1].u.hdr.size4x/4;
18419      mem3.aPool[mem3.iMaster-1].u.hdr.size4x = mem3.szMaster*4 | x;
18420      mem3.aPool[mem3.iMaster+mem3.szMaster-1].u.hdr.prevSize = mem3.szMaster;
18421    }
18422  }
18423}
18424
18425/*
18426** Return the size of an outstanding allocation, in bytes.  The
18427** size returned omits the 8-byte header overhead.  This only
18428** works for chunks that are currently checked out.
18429*/
18430static int memsys3Size(void *p){
18431  Mem3Block *pBlock;
18432  if( p==0 ) return 0;
18433  pBlock = (Mem3Block*)p;
18434  assert( (pBlock[-1].u.hdr.size4x&1)!=0 );
18435  return (pBlock[-1].u.hdr.size4x&~3)*2 - 4;
18436}
18437
18438/*
18439** Round up a request size to the next valid allocation size.
18440*/
18441static int memsys3Roundup(int n){
18442  if( n<=12 ){
18443    return 12;
18444  }else{
18445    return ((n+11)&~7) - 4;
18446  }
18447}
18448
18449/*
18450** Allocate nBytes of memory.
18451*/
18452static void *memsys3Malloc(int nBytes){
18453  sqlite3_int64 *p;
18454  assert( nBytes>0 );          /* malloc.c filters out 0 byte requests */
18455  memsys3Enter();
18456  p = memsys3MallocUnsafe(nBytes);
18457  memsys3Leave();
18458  return (void*)p;
18459}
18460
18461/*
18462** Free memory.
18463*/
18464static void memsys3Free(void *pPrior){
18465  assert( pPrior );
18466  memsys3Enter();
18467  memsys3FreeUnsafe(pPrior);
18468  memsys3Leave();
18469}
18470
18471/*
18472** Change the size of an existing memory allocation
18473*/
18474static void *memsys3Realloc(void *pPrior, int nBytes){
18475  int nOld;
18476  void *p;
18477  if( pPrior==0 ){
18478    return sqlite3_malloc(nBytes);
18479  }
18480  if( nBytes<=0 ){
18481    sqlite3_free(pPrior);
18482    return 0;
18483  }
18484  nOld = memsys3Size(pPrior);
18485  if( nBytes<=nOld && nBytes>=nOld-128 ){
18486    return pPrior;
18487  }
18488  memsys3Enter();
18489  p = memsys3MallocUnsafe(nBytes);
18490  if( p ){
18491    if( nOld<nBytes ){
18492      memcpy(p, pPrior, nOld);
18493    }else{
18494      memcpy(p, pPrior, nBytes);
18495    }
18496    memsys3FreeUnsafe(pPrior);
18497  }
18498  memsys3Leave();
18499  return p;
18500}
18501
18502/*
18503** Initialize this module.
18504*/
18505static int memsys3Init(void *NotUsed){
18506  UNUSED_PARAMETER(NotUsed);
18507  if( !sqlite3GlobalConfig.pHeap ){
18508    return SQLITE_ERROR;
18509  }
18510
18511  /* Store a pointer to the memory block in global structure mem3. */
18512  assert( sizeof(Mem3Block)==8 );
18513  mem3.aPool = (Mem3Block *)sqlite3GlobalConfig.pHeap;
18514  mem3.nPool = (sqlite3GlobalConfig.nHeap / sizeof(Mem3Block)) - 2;
18515
18516  /* Initialize the master block. */
18517  mem3.szMaster = mem3.nPool;
18518  mem3.mnMaster = mem3.szMaster;
18519  mem3.iMaster = 1;
18520  mem3.aPool[0].u.hdr.size4x = (mem3.szMaster<<2) + 2;
18521  mem3.aPool[mem3.nPool].u.hdr.prevSize = mem3.nPool;
18522  mem3.aPool[mem3.nPool].u.hdr.size4x = 1;
18523
18524  return SQLITE_OK;
18525}
18526
18527/*
18528** Deinitialize this module.
18529*/
18530static void memsys3Shutdown(void *NotUsed){
18531  UNUSED_PARAMETER(NotUsed);
18532  mem3.mutex = 0;
18533  return;
18534}
18535
18536
18537
18538/*
18539** Open the file indicated and write a log of all unfreed memory
18540** allocations into that log.
18541*/
18542SQLITE_PRIVATE void sqlite3Memsys3Dump(const char *zFilename){
18543#ifdef SQLITE_DEBUG
18544  FILE *out;
18545  u32 i, j;
18546  u32 size;
18547  if( zFilename==0 || zFilename[0]==0 ){
18548    out = stdout;
18549  }else{
18550    out = fopen(zFilename, "w");
18551    if( out==0 ){
18552      fprintf(stderr, "** Unable to output memory debug output log: %s **\n",
18553                      zFilename);
18554      return;
18555    }
18556  }
18557  memsys3Enter();
18558  fprintf(out, "CHUNKS:\n");
18559  for(i=1; i<=mem3.nPool; i+=size/4){
18560    size = mem3.aPool[i-1].u.hdr.size4x;
18561    if( size/4<=1 ){
18562      fprintf(out, "%p size error\n", &mem3.aPool[i]);
18563      assert( 0 );
18564      break;
18565    }
18566    if( (size&1)==0 && mem3.aPool[i+size/4-1].u.hdr.prevSize!=size/4 ){
18567      fprintf(out, "%p tail size does not match\n", &mem3.aPool[i]);
18568      assert( 0 );
18569      break;
18570    }
18571    if( ((mem3.aPool[i+size/4-1].u.hdr.size4x&2)>>1)!=(size&1) ){
18572      fprintf(out, "%p tail checkout bit is incorrect\n", &mem3.aPool[i]);
18573      assert( 0 );
18574      break;
18575    }
18576    if( size&1 ){
18577      fprintf(out, "%p %6d bytes checked out\n", &mem3.aPool[i], (size/4)*8-8);
18578    }else{
18579      fprintf(out, "%p %6d bytes free%s\n", &mem3.aPool[i], (size/4)*8-8,
18580                  i==mem3.iMaster ? " **master**" : "");
18581    }
18582  }
18583  for(i=0; i<MX_SMALL-1; i++){
18584    if( mem3.aiSmall[i]==0 ) continue;
18585    fprintf(out, "small(%2d):", i);
18586    for(j = mem3.aiSmall[i]; j>0; j=mem3.aPool[j].u.list.next){
18587      fprintf(out, " %p(%d)", &mem3.aPool[j],
18588              (mem3.aPool[j-1].u.hdr.size4x/4)*8-8);
18589    }
18590    fprintf(out, "\n");
18591  }
18592  for(i=0; i<N_HASH; i++){
18593    if( mem3.aiHash[i]==0 ) continue;
18594    fprintf(out, "hash(%2d):", i);
18595    for(j = mem3.aiHash[i]; j>0; j=mem3.aPool[j].u.list.next){
18596      fprintf(out, " %p(%d)", &mem3.aPool[j],
18597              (mem3.aPool[j-1].u.hdr.size4x/4)*8-8);
18598    }
18599    fprintf(out, "\n");
18600  }
18601  fprintf(out, "master=%d\n", mem3.iMaster);
18602  fprintf(out, "nowUsed=%d\n", mem3.nPool*8 - mem3.szMaster*8);
18603  fprintf(out, "mxUsed=%d\n", mem3.nPool*8 - mem3.mnMaster*8);
18604  sqlite3_mutex_leave(mem3.mutex);
18605  if( out==stdout ){
18606    fflush(stdout);
18607  }else{
18608    fclose(out);
18609  }
18610#else
18611  UNUSED_PARAMETER(zFilename);
18612#endif
18613}
18614
18615/*
18616** This routine is the only routine in this file with external
18617** linkage.
18618**
18619** Populate the low-level memory allocation function pointers in
18620** sqlite3GlobalConfig.m with pointers to the routines in this file. The
18621** arguments specify the block of memory to manage.
18622**
18623** This routine is only called by sqlite3_config(), and therefore
18624** is not required to be threadsafe (it is not).
18625*/
18626SQLITE_PRIVATE const sqlite3_mem_methods *sqlite3MemGetMemsys3(void){
18627  static const sqlite3_mem_methods mempoolMethods = {
18628     memsys3Malloc,
18629     memsys3Free,
18630     memsys3Realloc,
18631     memsys3Size,
18632     memsys3Roundup,
18633     memsys3Init,
18634     memsys3Shutdown,
18635     0
18636  };
18637  return &mempoolMethods;
18638}
18639
18640#endif /* SQLITE_ENABLE_MEMSYS3 */
18641
18642/************** End of mem3.c ************************************************/
18643/************** Begin file mem5.c ********************************************/
18644/*
18645** 2007 October 14
18646**
18647** The author disclaims copyright to this source code.  In place of
18648** a legal notice, here is a blessing:
18649**
18650**    May you do good and not evil.
18651**    May you find forgiveness for yourself and forgive others.
18652**    May you share freely, never taking more than you give.
18653**
18654*************************************************************************
18655** This file contains the C functions that implement a memory
18656** allocation subsystem for use by SQLite.
18657**
18658** This version of the memory allocation subsystem omits all
18659** use of malloc(). The application gives SQLite a block of memory
18660** before calling sqlite3_initialize() from which allocations
18661** are made and returned by the xMalloc() and xRealloc()
18662** implementations. Once sqlite3_initialize() has been called,
18663** the amount of memory available to SQLite is fixed and cannot
18664** be changed.
18665**
18666** This version of the memory allocation subsystem is included
18667** in the build only if SQLITE_ENABLE_MEMSYS5 is defined.
18668**
18669** This memory allocator uses the following algorithm:
18670**
18671**   1.  All memory allocations sizes are rounded up to a power of 2.
18672**
18673**   2.  If two adjacent free blocks are the halves of a larger block,
18674**       then the two blocks are coalesced into the single larger block.
18675**
18676**   3.  New memory is allocated from the first available free block.
18677**
18678** This algorithm is described in: J. M. Robson. "Bounds for Some Functions
18679** Concerning Dynamic Storage Allocation". Journal of the Association for
18680** Computing Machinery, Volume 21, Number 8, July 1974, pages 491-499.
18681**
18682** Let n be the size of the largest allocation divided by the minimum
18683** allocation size (after rounding all sizes up to a power of 2.)  Let M
18684** be the maximum amount of memory ever outstanding at one time.  Let
18685** N be the total amount of memory available for allocation.  Robson
18686** proved that this memory allocator will never breakdown due to
18687** fragmentation as long as the following constraint holds:
18688**
18689**      N >=  M*(1 + log2(n)/2) - n + 1
18690**
18691** The sqlite3_status() logic tracks the maximum values of n and M so
18692** that an application can, at any time, verify this constraint.
18693*/
18694/* #include "sqliteInt.h" */
18695
18696/*
18697** This version of the memory allocator is used only when
18698** SQLITE_ENABLE_MEMSYS5 is defined.
18699*/
18700#ifdef SQLITE_ENABLE_MEMSYS5
18701
18702/*
18703** A minimum allocation is an instance of the following structure.
18704** Larger allocations are an array of these structures where the
18705** size of the array is a power of 2.
18706**
18707** The size of this object must be a power of two.  That fact is
18708** verified in memsys5Init().
18709*/
18710typedef struct Mem5Link Mem5Link;
18711struct Mem5Link {
18712  int next;       /* Index of next free chunk */
18713  int prev;       /* Index of previous free chunk */
18714};
18715
18716/*
18717** Maximum size of any allocation is ((1<<LOGMAX)*mem5.szAtom). Since
18718** mem5.szAtom is always at least 8 and 32-bit integers are used,
18719** it is not actually possible to reach this limit.
18720*/
18721#define LOGMAX 30
18722
18723/*
18724** Masks used for mem5.aCtrl[] elements.
18725*/
18726#define CTRL_LOGSIZE  0x1f    /* Log2 Size of this block */
18727#define CTRL_FREE     0x20    /* True if not checked out */
18728
18729/*
18730** All of the static variables used by this module are collected
18731** into a single structure named "mem5".  This is to keep the
18732** static variables organized and to reduce namespace pollution
18733** when this module is combined with other in the amalgamation.
18734*/
18735static SQLITE_WSD struct Mem5Global {
18736  /*
18737  ** Memory available for allocation
18738  */
18739  int szAtom;      /* Smallest possible allocation in bytes */
18740  int nBlock;      /* Number of szAtom sized blocks in zPool */
18741  u8 *zPool;       /* Memory available to be allocated */
18742
18743  /*
18744  ** Mutex to control access to the memory allocation subsystem.
18745  */
18746  sqlite3_mutex *mutex;
18747
18748  /*
18749  ** Performance statistics
18750  */
18751  u64 nAlloc;         /* Total number of calls to malloc */
18752  u64 totalAlloc;     /* Total of all malloc calls - includes internal frag */
18753  u64 totalExcess;    /* Total internal fragmentation */
18754  u32 currentOut;     /* Current checkout, including internal fragmentation */
18755  u32 currentCount;   /* Current number of distinct checkouts */
18756  u32 maxOut;         /* Maximum instantaneous currentOut */
18757  u32 maxCount;       /* Maximum instantaneous currentCount */
18758  u32 maxRequest;     /* Largest allocation (exclusive of internal frag) */
18759
18760  /*
18761  ** Lists of free blocks.  aiFreelist[0] is a list of free blocks of
18762  ** size mem5.szAtom.  aiFreelist[1] holds blocks of size szAtom*2.
18763  ** and so forth.
18764  */
18765  int aiFreelist[LOGMAX+1];
18766
18767  /*
18768  ** Space for tracking which blocks are checked out and the size
18769  ** of each block.  One byte per block.
18770  */
18771  u8 *aCtrl;
18772
18773} mem5;
18774
18775/*
18776** Access the static variable through a macro for SQLITE_OMIT_WSD.
18777*/
18778#define mem5 GLOBAL(struct Mem5Global, mem5)
18779
18780/*
18781** Assuming mem5.zPool is divided up into an array of Mem5Link
18782** structures, return a pointer to the idx-th such link.
18783*/
18784#define MEM5LINK(idx) ((Mem5Link *)(&mem5.zPool[(idx)*mem5.szAtom]))
18785
18786/*
18787** Unlink the chunk at mem5.aPool[i] from list it is currently
18788** on.  It should be found on mem5.aiFreelist[iLogsize].
18789*/
18790static void memsys5Unlink(int i, int iLogsize){
18791  int next, prev;
18792  assert( i>=0 && i<mem5.nBlock );
18793  assert( iLogsize>=0 && iLogsize<=LOGMAX );
18794  assert( (mem5.aCtrl[i] & CTRL_LOGSIZE)==iLogsize );
18795
18796  next = MEM5LINK(i)->next;
18797  prev = MEM5LINK(i)->prev;
18798  if( prev<0 ){
18799    mem5.aiFreelist[iLogsize] = next;
18800  }else{
18801    MEM5LINK(prev)->next = next;
18802  }
18803  if( next>=0 ){
18804    MEM5LINK(next)->prev = prev;
18805  }
18806}
18807
18808/*
18809** Link the chunk at mem5.aPool[i] so that is on the iLogsize
18810** free list.
18811*/
18812static void memsys5Link(int i, int iLogsize){
18813  int x;
18814  assert( sqlite3_mutex_held(mem5.mutex) );
18815  assert( i>=0 && i<mem5.nBlock );
18816  assert( iLogsize>=0 && iLogsize<=LOGMAX );
18817  assert( (mem5.aCtrl[i] & CTRL_LOGSIZE)==iLogsize );
18818
18819  x = MEM5LINK(i)->next = mem5.aiFreelist[iLogsize];
18820  MEM5LINK(i)->prev = -1;
18821  if( x>=0 ){
18822    assert( x<mem5.nBlock );
18823    MEM5LINK(x)->prev = i;
18824  }
18825  mem5.aiFreelist[iLogsize] = i;
18826}
18827
18828/*
18829** If the STATIC_MEM mutex is not already held, obtain it now. The mutex
18830** will already be held (obtained by code in malloc.c) if
18831** sqlite3GlobalConfig.bMemStat is true.
18832*/
18833static void memsys5Enter(void){
18834  sqlite3_mutex_enter(mem5.mutex);
18835}
18836static void memsys5Leave(void){
18837  sqlite3_mutex_leave(mem5.mutex);
18838}
18839
18840/*
18841** Return the size of an outstanding allocation, in bytes.  The
18842** size returned omits the 8-byte header overhead.  This only
18843** works for chunks that are currently checked out.
18844*/
18845static int memsys5Size(void *p){
18846  int iSize = 0;
18847  if( p ){
18848    int i = (int)(((u8 *)p-mem5.zPool)/mem5.szAtom);
18849    assert( i>=0 && i<mem5.nBlock );
18850    iSize = mem5.szAtom * (1 << (mem5.aCtrl[i]&CTRL_LOGSIZE));
18851  }
18852  return iSize;
18853}
18854
18855/*
18856** Return a block of memory of at least nBytes in size.
18857** Return NULL if unable.  Return NULL if nBytes==0.
18858**
18859** The caller guarantees that nByte is positive.
18860**
18861** The caller has obtained a mutex prior to invoking this
18862** routine so there is never any chance that two or more
18863** threads can be in this routine at the same time.
18864*/
18865static void *memsys5MallocUnsafe(int nByte){
18866  int i;           /* Index of a mem5.aPool[] slot */
18867  int iBin;        /* Index into mem5.aiFreelist[] */
18868  int iFullSz;     /* Size of allocation rounded up to power of 2 */
18869  int iLogsize;    /* Log2 of iFullSz/POW2_MIN */
18870
18871  /* nByte must be a positive */
18872  assert( nByte>0 );
18873
18874  /* Keep track of the maximum allocation request.  Even unfulfilled
18875  ** requests are counted */
18876  if( (u32)nByte>mem5.maxRequest ){
18877    mem5.maxRequest = nByte;
18878  }
18879
18880  /* Abort if the requested allocation size is larger than the largest
18881  ** power of two that we can represent using 32-bit signed integers.
18882  */
18883  if( nByte > 0x40000000 ){
18884    return 0;
18885  }
18886
18887  /* Round nByte up to the next valid power of two */
18888  for(iFullSz=mem5.szAtom, iLogsize=0; iFullSz<nByte; iFullSz *= 2, iLogsize++){}
18889
18890  /* Make sure mem5.aiFreelist[iLogsize] contains at least one free
18891  ** block.  If not, then split a block of the next larger power of
18892  ** two in order to create a new free block of size iLogsize.
18893  */
18894  for(iBin=iLogsize; iBin<=LOGMAX && mem5.aiFreelist[iBin]<0; iBin++){}
18895  if( iBin>LOGMAX ){
18896    testcase( sqlite3GlobalConfig.xLog!=0 );
18897    sqlite3_log(SQLITE_NOMEM, "failed to allocate %u bytes", nByte);
18898    return 0;
18899  }
18900  i = mem5.aiFreelist[iBin];
18901  memsys5Unlink(i, iBin);
18902  while( iBin>iLogsize ){
18903    int newSize;
18904
18905    iBin--;
18906    newSize = 1 << iBin;
18907    mem5.aCtrl[i+newSize] = CTRL_FREE | iBin;
18908    memsys5Link(i+newSize, iBin);
18909  }
18910  mem5.aCtrl[i] = iLogsize;
18911
18912  /* Update allocator performance statistics. */
18913  mem5.nAlloc++;
18914  mem5.totalAlloc += iFullSz;
18915  mem5.totalExcess += iFullSz - nByte;
18916  mem5.currentCount++;
18917  mem5.currentOut += iFullSz;
18918  if( mem5.maxCount<mem5.currentCount ) mem5.maxCount = mem5.currentCount;
18919  if( mem5.maxOut<mem5.currentOut ) mem5.maxOut = mem5.currentOut;
18920
18921#ifdef SQLITE_DEBUG
18922  /* Make sure the allocated memory does not assume that it is set to zero
18923  ** or retains a value from a previous allocation */
18924  memset(&mem5.zPool[i*mem5.szAtom], 0xAA, iFullSz);
18925#endif
18926
18927  /* Return a pointer to the allocated memory. */
18928  return (void*)&mem5.zPool[i*mem5.szAtom];
18929}
18930
18931/*
18932** Free an outstanding memory allocation.
18933*/
18934static void memsys5FreeUnsafe(void *pOld){
18935  u32 size, iLogsize;
18936  int iBlock;
18937
18938  /* Set iBlock to the index of the block pointed to by pOld in
18939  ** the array of mem5.szAtom byte blocks pointed to by mem5.zPool.
18940  */
18941  iBlock = (int)(((u8 *)pOld-mem5.zPool)/mem5.szAtom);
18942
18943  /* Check that the pointer pOld points to a valid, non-free block. */
18944  assert( iBlock>=0 && iBlock<mem5.nBlock );
18945  assert( ((u8 *)pOld-mem5.zPool)%mem5.szAtom==0 );
18946  assert( (mem5.aCtrl[iBlock] & CTRL_FREE)==0 );
18947
18948  iLogsize = mem5.aCtrl[iBlock] & CTRL_LOGSIZE;
18949  size = 1<<iLogsize;
18950  assert( iBlock+size-1<(u32)mem5.nBlock );
18951
18952  mem5.aCtrl[iBlock] |= CTRL_FREE;
18953  mem5.aCtrl[iBlock+size-1] |= CTRL_FREE;
18954  assert( mem5.currentCount>0 );
18955  assert( mem5.currentOut>=(size*mem5.szAtom) );
18956  mem5.currentCount--;
18957  mem5.currentOut -= size*mem5.szAtom;
18958  assert( mem5.currentOut>0 || mem5.currentCount==0 );
18959  assert( mem5.currentCount>0 || mem5.currentOut==0 );
18960
18961  mem5.aCtrl[iBlock] = CTRL_FREE | iLogsize;
18962  while( ALWAYS(iLogsize<LOGMAX) ){
18963    int iBuddy;
18964    if( (iBlock>>iLogsize) & 1 ){
18965      iBuddy = iBlock - size;
18966    }else{
18967      iBuddy = iBlock + size;
18968    }
18969    assert( iBuddy>=0 );
18970    if( (iBuddy+(1<<iLogsize))>mem5.nBlock ) break;
18971    if( mem5.aCtrl[iBuddy]!=(CTRL_FREE | iLogsize) ) break;
18972    memsys5Unlink(iBuddy, iLogsize);
18973    iLogsize++;
18974    if( iBuddy<iBlock ){
18975      mem5.aCtrl[iBuddy] = CTRL_FREE | iLogsize;
18976      mem5.aCtrl[iBlock] = 0;
18977      iBlock = iBuddy;
18978    }else{
18979      mem5.aCtrl[iBlock] = CTRL_FREE | iLogsize;
18980      mem5.aCtrl[iBuddy] = 0;
18981    }
18982    size *= 2;
18983  }
18984
18985#ifdef SQLITE_DEBUG
18986  /* Overwrite freed memory with the 0x55 bit pattern to verify that it is
18987  ** not used after being freed */
18988  memset(&mem5.zPool[iBlock*mem5.szAtom], 0x55, size);
18989#endif
18990
18991  memsys5Link(iBlock, iLogsize);
18992}
18993
18994/*
18995** Allocate nBytes of memory.
18996*/
18997static void *memsys5Malloc(int nBytes){
18998  sqlite3_int64 *p = 0;
18999  if( nBytes>0 ){
19000    memsys5Enter();
19001    p = memsys5MallocUnsafe(nBytes);
19002    memsys5Leave();
19003  }
19004  return (void*)p;
19005}
19006
19007/*
19008** Free memory.
19009**
19010** The outer layer memory allocator prevents this routine from
19011** being called with pPrior==0.
19012*/
19013static void memsys5Free(void *pPrior){
19014  assert( pPrior!=0 );
19015  memsys5Enter();
19016  memsys5FreeUnsafe(pPrior);
19017  memsys5Leave();
19018}
19019
19020/*
19021** Change the size of an existing memory allocation.
19022**
19023** The outer layer memory allocator prevents this routine from
19024** being called with pPrior==0.
19025**
19026** nBytes is always a value obtained from a prior call to
19027** memsys5Round().  Hence nBytes is always a non-negative power
19028** of two.  If nBytes==0 that means that an oversize allocation
19029** (an allocation larger than 0x40000000) was requested and this
19030** routine should return 0 without freeing pPrior.
19031*/
19032static void *memsys5Realloc(void *pPrior, int nBytes){
19033  int nOld;
19034  void *p;
19035  assert( pPrior!=0 );
19036  assert( (nBytes&(nBytes-1))==0 );  /* EV: R-46199-30249 */
19037  assert( nBytes>=0 );
19038  if( nBytes==0 ){
19039    return 0;
19040  }
19041  nOld = memsys5Size(pPrior);
19042  if( nBytes<=nOld ){
19043    return pPrior;
19044  }
19045  memsys5Enter();
19046  p = memsys5MallocUnsafe(nBytes);
19047  if( p ){
19048    memcpy(p, pPrior, nOld);
19049    memsys5FreeUnsafe(pPrior);
19050  }
19051  memsys5Leave();
19052  return p;
19053}
19054
19055/*
19056** Round up a request size to the next valid allocation size.  If
19057** the allocation is too large to be handled by this allocation system,
19058** return 0.
19059**
19060** All allocations must be a power of two and must be expressed by a
19061** 32-bit signed integer.  Hence the largest allocation is 0x40000000
19062** or 1073741824 bytes.
19063*/
19064static int memsys5Roundup(int n){
19065  int iFullSz;
19066  if( n > 0x40000000 ) return 0;
19067  for(iFullSz=mem5.szAtom; iFullSz<n; iFullSz *= 2);
19068  return iFullSz;
19069}
19070
19071/*
19072** Return the ceiling of the logarithm base 2 of iValue.
19073**
19074** Examples:   memsys5Log(1) -> 0
19075**             memsys5Log(2) -> 1
19076**             memsys5Log(4) -> 2
19077**             memsys5Log(5) -> 3
19078**             memsys5Log(8) -> 3
19079**             memsys5Log(9) -> 4
19080*/
19081static int memsys5Log(int iValue){
19082  int iLog;
19083  for(iLog=0; (iLog<(int)((sizeof(int)*8)-1)) && (1<<iLog)<iValue; iLog++);
19084  return iLog;
19085}
19086
19087/*
19088** Initialize the memory allocator.
19089**
19090** This routine is not threadsafe.  The caller must be holding a mutex
19091** to prevent multiple threads from entering at the same time.
19092*/
19093static int memsys5Init(void *NotUsed){
19094  int ii;            /* Loop counter */
19095  int nByte;         /* Number of bytes of memory available to this allocator */
19096  u8 *zByte;         /* Memory usable by this allocator */
19097  int nMinLog;       /* Log base 2 of minimum allocation size in bytes */
19098  int iOffset;       /* An offset into mem5.aCtrl[] */
19099
19100  UNUSED_PARAMETER(NotUsed);
19101
19102  /* For the purposes of this routine, disable the mutex */
19103  mem5.mutex = 0;
19104
19105  /* The size of a Mem5Link object must be a power of two.  Verify that
19106  ** this is case.
19107  */
19108  assert( (sizeof(Mem5Link)&(sizeof(Mem5Link)-1))==0 );
19109
19110  nByte = sqlite3GlobalConfig.nHeap;
19111  zByte = (u8*)sqlite3GlobalConfig.pHeap;
19112  assert( zByte!=0 );  /* sqlite3_config() does not allow otherwise */
19113
19114  /* boundaries on sqlite3GlobalConfig.mnReq are enforced in sqlite3_config() */
19115  nMinLog = memsys5Log(sqlite3GlobalConfig.mnReq);
19116  mem5.szAtom = (1<<nMinLog);
19117  while( (int)sizeof(Mem5Link)>mem5.szAtom ){
19118    mem5.szAtom = mem5.szAtom << 1;
19119  }
19120
19121  mem5.nBlock = (nByte / (mem5.szAtom+sizeof(u8)));
19122  mem5.zPool = zByte;
19123  mem5.aCtrl = (u8 *)&mem5.zPool[mem5.nBlock*mem5.szAtom];
19124
19125  for(ii=0; ii<=LOGMAX; ii++){
19126    mem5.aiFreelist[ii] = -1;
19127  }
19128
19129  iOffset = 0;
19130  for(ii=LOGMAX; ii>=0; ii--){
19131    int nAlloc = (1<<ii);
19132    if( (iOffset+nAlloc)<=mem5.nBlock ){
19133      mem5.aCtrl[iOffset] = ii | CTRL_FREE;
19134      memsys5Link(iOffset, ii);
19135      iOffset += nAlloc;
19136    }
19137    assert((iOffset+nAlloc)>mem5.nBlock);
19138  }
19139
19140  /* If a mutex is required for normal operation, allocate one */
19141  if( sqlite3GlobalConfig.bMemstat==0 ){
19142    mem5.mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MEM);
19143  }
19144
19145  return SQLITE_OK;
19146}
19147
19148/*
19149** Deinitialize this module.
19150*/
19151static void memsys5Shutdown(void *NotUsed){
19152  UNUSED_PARAMETER(NotUsed);
19153  mem5.mutex = 0;
19154  return;
19155}
19156
19157#ifdef SQLITE_TEST
19158/*
19159** Open the file indicated and write a log of all unfreed memory
19160** allocations into that log.
19161*/
19162SQLITE_PRIVATE void sqlite3Memsys5Dump(const char *zFilename){
19163  FILE *out;
19164  int i, j, n;
19165  int nMinLog;
19166
19167  if( zFilename==0 || zFilename[0]==0 ){
19168    out = stdout;
19169  }else{
19170    out = fopen(zFilename, "w");
19171    if( out==0 ){
19172      fprintf(stderr, "** Unable to output memory debug output log: %s **\n",
19173                      zFilename);
19174      return;
19175    }
19176  }
19177  memsys5Enter();
19178  nMinLog = memsys5Log(mem5.szAtom);
19179  for(i=0; i<=LOGMAX && i+nMinLog<32; i++){
19180    for(n=0, j=mem5.aiFreelist[i]; j>=0; j = MEM5LINK(j)->next, n++){}
19181    fprintf(out, "freelist items of size %d: %d\n", mem5.szAtom << i, n);
19182  }
19183  fprintf(out, "mem5.nAlloc       = %llu\n", mem5.nAlloc);
19184  fprintf(out, "mem5.totalAlloc   = %llu\n", mem5.totalAlloc);
19185  fprintf(out, "mem5.totalExcess  = %llu\n", mem5.totalExcess);
19186  fprintf(out, "mem5.currentOut   = %u\n", mem5.currentOut);
19187  fprintf(out, "mem5.currentCount = %u\n", mem5.currentCount);
19188  fprintf(out, "mem5.maxOut       = %u\n", mem5.maxOut);
19189  fprintf(out, "mem5.maxCount     = %u\n", mem5.maxCount);
19190  fprintf(out, "mem5.maxRequest   = %u\n", mem5.maxRequest);
19191  memsys5Leave();
19192  if( out==stdout ){
19193    fflush(stdout);
19194  }else{
19195    fclose(out);
19196  }
19197}
19198#endif
19199
19200/*
19201** This routine is the only routine in this file with external
19202** linkage. It returns a pointer to a static sqlite3_mem_methods
19203** struct populated with the memsys5 methods.
19204*/
19205SQLITE_PRIVATE const sqlite3_mem_methods *sqlite3MemGetMemsys5(void){
19206  static const sqlite3_mem_methods memsys5Methods = {
19207     memsys5Malloc,
19208     memsys5Free,
19209     memsys5Realloc,
19210     memsys5Size,
19211     memsys5Roundup,
19212     memsys5Init,
19213     memsys5Shutdown,
19214     0
19215  };
19216  return &memsys5Methods;
19217}
19218
19219#endif /* SQLITE_ENABLE_MEMSYS5 */
19220
19221/************** End of mem5.c ************************************************/
19222/************** Begin file mutex.c *******************************************/
19223/*
19224** 2007 August 14
19225**
19226** The author disclaims copyright to this source code.  In place of
19227** a legal notice, here is a blessing:
19228**
19229**    May you do good and not evil.
19230**    May you find forgiveness for yourself and forgive others.
19231**    May you share freely, never taking more than you give.
19232**
19233*************************************************************************
19234** This file contains the C functions that implement mutexes.
19235**
19236** This file contains code that is common across all mutex implementations.
19237*/
19238/* #include "sqliteInt.h" */
19239
19240#if defined(SQLITE_DEBUG) && !defined(SQLITE_MUTEX_OMIT)
19241/*
19242** For debugging purposes, record when the mutex subsystem is initialized
19243** and uninitialized so that we can assert() if there is an attempt to
19244** allocate a mutex while the system is uninitialized.
19245*/
19246static SQLITE_WSD int mutexIsInit = 0;
19247#endif /* SQLITE_DEBUG */
19248
19249
19250#ifndef SQLITE_MUTEX_OMIT
19251/*
19252** Initialize the mutex system.
19253*/
19254SQLITE_PRIVATE int sqlite3MutexInit(void){
19255  int rc = SQLITE_OK;
19256  if( !sqlite3GlobalConfig.mutex.xMutexAlloc ){
19257    /* If the xMutexAlloc method has not been set, then the user did not
19258    ** install a mutex implementation via sqlite3_config() prior to
19259    ** sqlite3_initialize() being called. This block copies pointers to
19260    ** the default implementation into the sqlite3GlobalConfig structure.
19261    */
19262    sqlite3_mutex_methods const *pFrom;
19263    sqlite3_mutex_methods *pTo = &sqlite3GlobalConfig.mutex;
19264
19265    if( sqlite3GlobalConfig.bCoreMutex ){
19266      pFrom = sqlite3DefaultMutex();
19267    }else{
19268      pFrom = sqlite3NoopMutex();
19269    }
19270    pTo->xMutexInit = pFrom->xMutexInit;
19271    pTo->xMutexEnd = pFrom->xMutexEnd;
19272    pTo->xMutexFree = pFrom->xMutexFree;
19273    pTo->xMutexEnter = pFrom->xMutexEnter;
19274    pTo->xMutexTry = pFrom->xMutexTry;
19275    pTo->xMutexLeave = pFrom->xMutexLeave;
19276    pTo->xMutexHeld = pFrom->xMutexHeld;
19277    pTo->xMutexNotheld = pFrom->xMutexNotheld;
19278    pTo->xMutexAlloc = pFrom->xMutexAlloc;
19279  }
19280  rc = sqlite3GlobalConfig.mutex.xMutexInit();
19281
19282#ifdef SQLITE_DEBUG
19283  GLOBAL(int, mutexIsInit) = 1;
19284#endif
19285
19286  return rc;
19287}
19288
19289/*
19290** Shutdown the mutex system. This call frees resources allocated by
19291** sqlite3MutexInit().
19292*/
19293SQLITE_PRIVATE int sqlite3MutexEnd(void){
19294  int rc = SQLITE_OK;
19295  if( sqlite3GlobalConfig.mutex.xMutexEnd ){
19296    rc = sqlite3GlobalConfig.mutex.xMutexEnd();
19297  }
19298
19299#ifdef SQLITE_DEBUG
19300  GLOBAL(int, mutexIsInit) = 0;
19301#endif
19302
19303  return rc;
19304}
19305
19306/*
19307** Retrieve a pointer to a static mutex or allocate a new dynamic one.
19308*/
19309SQLITE_API sqlite3_mutex *SQLITE_STDCALL sqlite3_mutex_alloc(int id){
19310#ifndef SQLITE_OMIT_AUTOINIT
19311  if( id<=SQLITE_MUTEX_RECURSIVE && sqlite3_initialize() ) return 0;
19312  if( id>SQLITE_MUTEX_RECURSIVE && sqlite3MutexInit() ) return 0;
19313#endif
19314  return sqlite3GlobalConfig.mutex.xMutexAlloc(id);
19315}
19316
19317SQLITE_PRIVATE sqlite3_mutex *sqlite3MutexAlloc(int id){
19318  if( !sqlite3GlobalConfig.bCoreMutex ){
19319    return 0;
19320  }
19321  assert( GLOBAL(int, mutexIsInit) );
19322  return sqlite3GlobalConfig.mutex.xMutexAlloc(id);
19323}
19324
19325/*
19326** Free a dynamic mutex.
19327*/
19328SQLITE_API void SQLITE_STDCALL sqlite3_mutex_free(sqlite3_mutex *p){
19329  if( p ){
19330    sqlite3GlobalConfig.mutex.xMutexFree(p);
19331  }
19332}
19333
19334/*
19335** Obtain the mutex p. If some other thread already has the mutex, block
19336** until it can be obtained.
19337*/
19338SQLITE_API void SQLITE_STDCALL sqlite3_mutex_enter(sqlite3_mutex *p){
19339  if( p ){
19340    sqlite3GlobalConfig.mutex.xMutexEnter(p);
19341  }
19342}
19343
19344/*
19345** Obtain the mutex p. If successful, return SQLITE_OK. Otherwise, if another
19346** thread holds the mutex and it cannot be obtained, return SQLITE_BUSY.
19347*/
19348SQLITE_API int SQLITE_STDCALL sqlite3_mutex_try(sqlite3_mutex *p){
19349  int rc = SQLITE_OK;
19350  if( p ){
19351    return sqlite3GlobalConfig.mutex.xMutexTry(p);
19352  }
19353  return rc;
19354}
19355
19356/*
19357** The sqlite3_mutex_leave() routine exits a mutex that was previously
19358** entered by the same thread.  The behavior is undefined if the mutex
19359** is not currently entered. If a NULL pointer is passed as an argument
19360** this function is a no-op.
19361*/
19362SQLITE_API void SQLITE_STDCALL sqlite3_mutex_leave(sqlite3_mutex *p){
19363  if( p ){
19364    sqlite3GlobalConfig.mutex.xMutexLeave(p);
19365  }
19366}
19367
19368#ifndef NDEBUG
19369/*
19370** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routine are
19371** intended for use inside assert() statements.
19372*/
19373SQLITE_API int SQLITE_STDCALL sqlite3_mutex_held(sqlite3_mutex *p){
19374  return p==0 || sqlite3GlobalConfig.mutex.xMutexHeld(p);
19375}
19376SQLITE_API int SQLITE_STDCALL sqlite3_mutex_notheld(sqlite3_mutex *p){
19377  return p==0 || sqlite3GlobalConfig.mutex.xMutexNotheld(p);
19378}
19379#endif
19380
19381#endif /* !defined(SQLITE_MUTEX_OMIT) */
19382
19383/************** End of mutex.c ***********************************************/
19384/************** Begin file mutex_noop.c **************************************/
19385/*
19386** 2008 October 07
19387**
19388** The author disclaims copyright to this source code.  In place of
19389** a legal notice, here is a blessing:
19390**
19391**    May you do good and not evil.
19392**    May you find forgiveness for yourself and forgive others.
19393**    May you share freely, never taking more than you give.
19394**
19395*************************************************************************
19396** This file contains the C functions that implement mutexes.
19397**
19398** This implementation in this file does not provide any mutual
19399** exclusion and is thus suitable for use only in applications
19400** that use SQLite in a single thread.  The routines defined
19401** here are place-holders.  Applications can substitute working
19402** mutex routines at start-time using the
19403**
19404**     sqlite3_config(SQLITE_CONFIG_MUTEX,...)
19405**
19406** interface.
19407**
19408** If compiled with SQLITE_DEBUG, then additional logic is inserted
19409** that does error checking on mutexes to make sure they are being
19410** called correctly.
19411*/
19412/* #include "sqliteInt.h" */
19413
19414#ifndef SQLITE_MUTEX_OMIT
19415
19416#ifndef SQLITE_DEBUG
19417/*
19418** Stub routines for all mutex methods.
19419**
19420** This routines provide no mutual exclusion or error checking.
19421*/
19422static int noopMutexInit(void){ return SQLITE_OK; }
19423static int noopMutexEnd(void){ return SQLITE_OK; }
19424static sqlite3_mutex *noopMutexAlloc(int id){
19425  UNUSED_PARAMETER(id);
19426  return (sqlite3_mutex*)8;
19427}
19428static void noopMutexFree(sqlite3_mutex *p){ UNUSED_PARAMETER(p); return; }
19429static void noopMutexEnter(sqlite3_mutex *p){ UNUSED_PARAMETER(p); return; }
19430static int noopMutexTry(sqlite3_mutex *p){
19431  UNUSED_PARAMETER(p);
19432  return SQLITE_OK;
19433}
19434static void noopMutexLeave(sqlite3_mutex *p){ UNUSED_PARAMETER(p); return; }
19435
19436SQLITE_PRIVATE sqlite3_mutex_methods const *sqlite3NoopMutex(void){
19437  static const sqlite3_mutex_methods sMutex = {
19438    noopMutexInit,
19439    noopMutexEnd,
19440    noopMutexAlloc,
19441    noopMutexFree,
19442    noopMutexEnter,
19443    noopMutexTry,
19444    noopMutexLeave,
19445
19446    0,
19447    0,
19448  };
19449
19450  return &sMutex;
19451}
19452#endif /* !SQLITE_DEBUG */
19453
19454#ifdef SQLITE_DEBUG
19455/*
19456** In this implementation, error checking is provided for testing
19457** and debugging purposes.  The mutexes still do not provide any
19458** mutual exclusion.
19459*/
19460
19461/*
19462** The mutex object
19463*/
19464typedef struct sqlite3_debug_mutex {
19465  int id;     /* The mutex type */
19466  int cnt;    /* Number of entries without a matching leave */
19467} sqlite3_debug_mutex;
19468
19469/*
19470** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routine are
19471** intended for use inside assert() statements.
19472*/
19473static int debugMutexHeld(sqlite3_mutex *pX){
19474  sqlite3_debug_mutex *p = (sqlite3_debug_mutex*)pX;
19475  return p==0 || p->cnt>0;
19476}
19477static int debugMutexNotheld(sqlite3_mutex *pX){
19478  sqlite3_debug_mutex *p = (sqlite3_debug_mutex*)pX;
19479  return p==0 || p->cnt==0;
19480}
19481
19482/*
19483** Initialize and deinitialize the mutex subsystem.
19484*/
19485static int debugMutexInit(void){ return SQLITE_OK; }
19486static int debugMutexEnd(void){ return SQLITE_OK; }
19487
19488/*
19489** The sqlite3_mutex_alloc() routine allocates a new
19490** mutex and returns a pointer to it.  If it returns NULL
19491** that means that a mutex could not be allocated.
19492*/
19493static sqlite3_mutex *debugMutexAlloc(int id){
19494  static sqlite3_debug_mutex aStatic[SQLITE_MUTEX_STATIC_VFS3 - 1];
19495  sqlite3_debug_mutex *pNew = 0;
19496  switch( id ){
19497    case SQLITE_MUTEX_FAST:
19498    case SQLITE_MUTEX_RECURSIVE: {
19499      pNew = sqlite3Malloc(sizeof(*pNew));
19500      if( pNew ){
19501        pNew->id = id;
19502        pNew->cnt = 0;
19503      }
19504      break;
19505    }
19506    default: {
19507#ifdef SQLITE_ENABLE_API_ARMOR
19508      if( id-2<0 || id-2>=ArraySize(aStatic) ){
19509        (void)SQLITE_MISUSE_BKPT;
19510        return 0;
19511      }
19512#endif
19513      pNew = &aStatic[id-2];
19514      pNew->id = id;
19515      break;
19516    }
19517  }
19518  return (sqlite3_mutex*)pNew;
19519}
19520
19521/*
19522** This routine deallocates a previously allocated mutex.
19523*/
19524static void debugMutexFree(sqlite3_mutex *pX){
19525  sqlite3_debug_mutex *p = (sqlite3_debug_mutex*)pX;
19526  assert( p->cnt==0 );
19527  if( p->id==SQLITE_MUTEX_RECURSIVE || p->id==SQLITE_MUTEX_FAST ){
19528    sqlite3_free(p);
19529  }else{
19530#ifdef SQLITE_ENABLE_API_ARMOR
19531    (void)SQLITE_MISUSE_BKPT;
19532#endif
19533  }
19534}
19535
19536/*
19537** The sqlite3_mutex_enter() and sqlite3_mutex_try() routines attempt
19538** to enter a mutex.  If another thread is already within the mutex,
19539** sqlite3_mutex_enter() will block and sqlite3_mutex_try() will return
19540** SQLITE_BUSY.  The sqlite3_mutex_try() interface returns SQLITE_OK
19541** upon successful entry.  Mutexes created using SQLITE_MUTEX_RECURSIVE can
19542** be entered multiple times by the same thread.  In such cases the,
19543** mutex must be exited an equal number of times before another thread
19544** can enter.  If the same thread tries to enter any other kind of mutex
19545** more than once, the behavior is undefined.
19546*/
19547static void debugMutexEnter(sqlite3_mutex *pX){
19548  sqlite3_debug_mutex *p = (sqlite3_debug_mutex*)pX;
19549  assert( p->id==SQLITE_MUTEX_RECURSIVE || debugMutexNotheld(pX) );
19550  p->cnt++;
19551}
19552static int debugMutexTry(sqlite3_mutex *pX){
19553  sqlite3_debug_mutex *p = (sqlite3_debug_mutex*)pX;
19554  assert( p->id==SQLITE_MUTEX_RECURSIVE || debugMutexNotheld(pX) );
19555  p->cnt++;
19556  return SQLITE_OK;
19557}
19558
19559/*
19560** The sqlite3_mutex_leave() routine exits a mutex that was
19561** previously entered by the same thread.  The behavior
19562** is undefined if the mutex is not currently entered or
19563** is not currently allocated.  SQLite will never do either.
19564*/
19565static void debugMutexLeave(sqlite3_mutex *pX){
19566  sqlite3_debug_mutex *p = (sqlite3_debug_mutex*)pX;
19567  assert( debugMutexHeld(pX) );
19568  p->cnt--;
19569  assert( p->id==SQLITE_MUTEX_RECURSIVE || debugMutexNotheld(pX) );
19570}
19571
19572SQLITE_PRIVATE sqlite3_mutex_methods const *sqlite3NoopMutex(void){
19573  static const sqlite3_mutex_methods sMutex = {
19574    debugMutexInit,
19575    debugMutexEnd,
19576    debugMutexAlloc,
19577    debugMutexFree,
19578    debugMutexEnter,
19579    debugMutexTry,
19580    debugMutexLeave,
19581
19582    debugMutexHeld,
19583    debugMutexNotheld
19584  };
19585
19586  return &sMutex;
19587}
19588#endif /* SQLITE_DEBUG */
19589
19590/*
19591** If compiled with SQLITE_MUTEX_NOOP, then the no-op mutex implementation
19592** is used regardless of the run-time threadsafety setting.
19593*/
19594#ifdef SQLITE_MUTEX_NOOP
19595SQLITE_PRIVATE sqlite3_mutex_methods const *sqlite3DefaultMutex(void){
19596  return sqlite3NoopMutex();
19597}
19598#endif /* defined(SQLITE_MUTEX_NOOP) */
19599#endif /* !defined(SQLITE_MUTEX_OMIT) */
19600
19601/************** End of mutex_noop.c ******************************************/
19602/************** Begin file mutex_unix.c **************************************/
19603/*
19604** 2007 August 28
19605**
19606** The author disclaims copyright to this source code.  In place of
19607** a legal notice, here is a blessing:
19608**
19609**    May you do good and not evil.
19610**    May you find forgiveness for yourself and forgive others.
19611**    May you share freely, never taking more than you give.
19612**
19613*************************************************************************
19614** This file contains the C functions that implement mutexes for pthreads
19615*/
19616/* #include "sqliteInt.h" */
19617
19618/*
19619** The code in this file is only used if we are compiling threadsafe
19620** under unix with pthreads.
19621**
19622** Note that this implementation requires a version of pthreads that
19623** supports recursive mutexes.
19624*/
19625#ifdef SQLITE_MUTEX_PTHREADS
19626
19627#include <pthread.h>
19628
19629/*
19630** The sqlite3_mutex.id, sqlite3_mutex.nRef, and sqlite3_mutex.owner fields
19631** are necessary under two condidtions:  (1) Debug builds and (2) using
19632** home-grown mutexes.  Encapsulate these conditions into a single #define.
19633*/
19634#if defined(SQLITE_DEBUG) || defined(SQLITE_HOMEGROWN_RECURSIVE_MUTEX)
19635# define SQLITE_MUTEX_NREF 1
19636#else
19637# define SQLITE_MUTEX_NREF 0
19638#endif
19639
19640/*
19641** Each recursive mutex is an instance of the following structure.
19642*/
19643struct sqlite3_mutex {
19644  pthread_mutex_t mutex;     /* Mutex controlling the lock */
19645#if SQLITE_MUTEX_NREF || defined(SQLITE_ENABLE_API_ARMOR)
19646  int id;                    /* Mutex type */
19647#endif
19648#if SQLITE_MUTEX_NREF
19649  volatile int nRef;         /* Number of entrances */
19650  volatile pthread_t owner;  /* Thread that is within this mutex */
19651  int trace;                 /* True to trace changes */
19652#endif
19653};
19654#if SQLITE_MUTEX_NREF
19655#define SQLITE3_MUTEX_INITIALIZER { PTHREAD_MUTEX_INITIALIZER, 0, 0, (pthread_t)0, 0 }
19656#else
19657#define SQLITE3_MUTEX_INITIALIZER { PTHREAD_MUTEX_INITIALIZER }
19658#endif
19659
19660/*
19661** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routine are
19662** intended for use only inside assert() statements.  On some platforms,
19663** there might be race conditions that can cause these routines to
19664** deliver incorrect results.  In particular, if pthread_equal() is
19665** not an atomic operation, then these routines might delivery
19666** incorrect results.  On most platforms, pthread_equal() is a
19667** comparison of two integers and is therefore atomic.  But we are
19668** told that HPUX is not such a platform.  If so, then these routines
19669** will not always work correctly on HPUX.
19670**
19671** On those platforms where pthread_equal() is not atomic, SQLite
19672** should be compiled without -DSQLITE_DEBUG and with -DNDEBUG to
19673** make sure no assert() statements are evaluated and hence these
19674** routines are never called.
19675*/
19676#if !defined(NDEBUG) || defined(SQLITE_DEBUG)
19677static int pthreadMutexHeld(sqlite3_mutex *p){
19678  return (p->nRef!=0 && pthread_equal(p->owner, pthread_self()));
19679}
19680static int pthreadMutexNotheld(sqlite3_mutex *p){
19681  return p->nRef==0 || pthread_equal(p->owner, pthread_self())==0;
19682}
19683#endif
19684
19685/*
19686** Initialize and deinitialize the mutex subsystem.
19687*/
19688static int pthreadMutexInit(void){ return SQLITE_OK; }
19689static int pthreadMutexEnd(void){ return SQLITE_OK; }
19690
19691/*
19692** The sqlite3_mutex_alloc() routine allocates a new
19693** mutex and returns a pointer to it.  If it returns NULL
19694** that means that a mutex could not be allocated.  SQLite
19695** will unwind its stack and return an error.  The argument
19696** to sqlite3_mutex_alloc() is one of these integer constants:
19697**
19698** <ul>
19699** <li>  SQLITE_MUTEX_FAST
19700** <li>  SQLITE_MUTEX_RECURSIVE
19701** <li>  SQLITE_MUTEX_STATIC_MASTER
19702** <li>  SQLITE_MUTEX_STATIC_MEM
19703** <li>  SQLITE_MUTEX_STATIC_OPEN
19704** <li>  SQLITE_MUTEX_STATIC_PRNG
19705** <li>  SQLITE_MUTEX_STATIC_LRU
19706** <li>  SQLITE_MUTEX_STATIC_PMEM
19707** <li>  SQLITE_MUTEX_STATIC_APP1
19708** <li>  SQLITE_MUTEX_STATIC_APP2
19709** <li>  SQLITE_MUTEX_STATIC_APP3
19710** <li>  SQLITE_MUTEX_STATIC_VFS1
19711** <li>  SQLITE_MUTEX_STATIC_VFS2
19712** <li>  SQLITE_MUTEX_STATIC_VFS3
19713** </ul>
19714**
19715** The first two constants cause sqlite3_mutex_alloc() to create
19716** a new mutex.  The new mutex is recursive when SQLITE_MUTEX_RECURSIVE
19717** is used but not necessarily so when SQLITE_MUTEX_FAST is used.
19718** The mutex implementation does not need to make a distinction
19719** between SQLITE_MUTEX_RECURSIVE and SQLITE_MUTEX_FAST if it does
19720** not want to.  But SQLite will only request a recursive mutex in
19721** cases where it really needs one.  If a faster non-recursive mutex
19722** implementation is available on the host platform, the mutex subsystem
19723** might return such a mutex in response to SQLITE_MUTEX_FAST.
19724**
19725** The other allowed parameters to sqlite3_mutex_alloc() each return
19726** a pointer to a static preexisting mutex.  Six static mutexes are
19727** used by the current version of SQLite.  Future versions of SQLite
19728** may add additional static mutexes.  Static mutexes are for internal
19729** use by SQLite only.  Applications that use SQLite mutexes should
19730** use only the dynamic mutexes returned by SQLITE_MUTEX_FAST or
19731** SQLITE_MUTEX_RECURSIVE.
19732**
19733** Note that if one of the dynamic mutex parameters (SQLITE_MUTEX_FAST
19734** or SQLITE_MUTEX_RECURSIVE) is used then sqlite3_mutex_alloc()
19735** returns a different mutex on every call.  But for the static
19736** mutex types, the same mutex is returned on every call that has
19737** the same type number.
19738*/
19739static sqlite3_mutex *pthreadMutexAlloc(int iType){
19740  static sqlite3_mutex staticMutexes[] = {
19741    SQLITE3_MUTEX_INITIALIZER,
19742    SQLITE3_MUTEX_INITIALIZER,
19743    SQLITE3_MUTEX_INITIALIZER,
19744    SQLITE3_MUTEX_INITIALIZER,
19745    SQLITE3_MUTEX_INITIALIZER,
19746    SQLITE3_MUTEX_INITIALIZER,
19747    SQLITE3_MUTEX_INITIALIZER,
19748    SQLITE3_MUTEX_INITIALIZER,
19749    SQLITE3_MUTEX_INITIALIZER,
19750    SQLITE3_MUTEX_INITIALIZER,
19751    SQLITE3_MUTEX_INITIALIZER,
19752    SQLITE3_MUTEX_INITIALIZER
19753  };
19754  sqlite3_mutex *p;
19755  switch( iType ){
19756    case SQLITE_MUTEX_RECURSIVE: {
19757      p = sqlite3MallocZero( sizeof(*p) );
19758      if( p ){
19759#ifdef SQLITE_HOMEGROWN_RECURSIVE_MUTEX
19760        /* If recursive mutexes are not available, we will have to
19761        ** build our own.  See below. */
19762        pthread_mutex_init(&p->mutex, 0);
19763#else
19764        /* Use a recursive mutex if it is available */
19765        pthread_mutexattr_t recursiveAttr;
19766        pthread_mutexattr_init(&recursiveAttr);
19767        pthread_mutexattr_settype(&recursiveAttr, PTHREAD_MUTEX_RECURSIVE);
19768        pthread_mutex_init(&p->mutex, &recursiveAttr);
19769        pthread_mutexattr_destroy(&recursiveAttr);
19770#endif
19771      }
19772      break;
19773    }
19774    case SQLITE_MUTEX_FAST: {
19775      p = sqlite3MallocZero( sizeof(*p) );
19776      if( p ){
19777        pthread_mutex_init(&p->mutex, 0);
19778      }
19779      break;
19780    }
19781    default: {
19782#ifdef SQLITE_ENABLE_API_ARMOR
19783      if( iType-2<0 || iType-2>=ArraySize(staticMutexes) ){
19784        (void)SQLITE_MISUSE_BKPT;
19785        return 0;
19786      }
19787#endif
19788      p = &staticMutexes[iType-2];
19789      break;
19790    }
19791  }
19792#if SQLITE_MUTEX_NREF || defined(SQLITE_ENABLE_API_ARMOR)
19793  if( p ) p->id = iType;
19794#endif
19795  return p;
19796}
19797
19798
19799/*
19800** This routine deallocates a previously
19801** allocated mutex.  SQLite is careful to deallocate every
19802** mutex that it allocates.
19803*/
19804static void pthreadMutexFree(sqlite3_mutex *p){
19805  assert( p->nRef==0 );
19806#if SQLITE_ENABLE_API_ARMOR
19807  if( p->id==SQLITE_MUTEX_FAST || p->id==SQLITE_MUTEX_RECURSIVE )
19808#endif
19809  {
19810    pthread_mutex_destroy(&p->mutex);
19811    sqlite3_free(p);
19812  }
19813#ifdef SQLITE_ENABLE_API_ARMOR
19814  else{
19815    (void)SQLITE_MISUSE_BKPT;
19816  }
19817#endif
19818}
19819
19820/*
19821** The sqlite3_mutex_enter() and sqlite3_mutex_try() routines attempt
19822** to enter a mutex.  If another thread is already within the mutex,
19823** sqlite3_mutex_enter() will block and sqlite3_mutex_try() will return
19824** SQLITE_BUSY.  The sqlite3_mutex_try() interface returns SQLITE_OK
19825** upon successful entry.  Mutexes created using SQLITE_MUTEX_RECURSIVE can
19826** be entered multiple times by the same thread.  In such cases the,
19827** mutex must be exited an equal number of times before another thread
19828** can enter.  If the same thread tries to enter any other kind of mutex
19829** more than once, the behavior is undefined.
19830*/
19831static void pthreadMutexEnter(sqlite3_mutex *p){
19832  assert( p->id==SQLITE_MUTEX_RECURSIVE || pthreadMutexNotheld(p) );
19833
19834#ifdef SQLITE_HOMEGROWN_RECURSIVE_MUTEX
19835  /* If recursive mutexes are not available, then we have to grow
19836  ** our own.  This implementation assumes that pthread_equal()
19837  ** is atomic - that it cannot be deceived into thinking self
19838  ** and p->owner are equal if p->owner changes between two values
19839  ** that are not equal to self while the comparison is taking place.
19840  ** This implementation also assumes a coherent cache - that
19841  ** separate processes cannot read different values from the same
19842  ** address at the same time.  If either of these two conditions
19843  ** are not met, then the mutexes will fail and problems will result.
19844  */
19845  {
19846    pthread_t self = pthread_self();
19847    if( p->nRef>0 && pthread_equal(p->owner, self) ){
19848      p->nRef++;
19849    }else{
19850      pthread_mutex_lock(&p->mutex);
19851      assert( p->nRef==0 );
19852      p->owner = self;
19853      p->nRef = 1;
19854    }
19855  }
19856#else
19857  /* Use the built-in recursive mutexes if they are available.
19858  */
19859  pthread_mutex_lock(&p->mutex);
19860#if SQLITE_MUTEX_NREF
19861  assert( p->nRef>0 || p->owner==0 );
19862  p->owner = pthread_self();
19863  p->nRef++;
19864#endif
19865#endif
19866
19867#ifdef SQLITE_DEBUG
19868  if( p->trace ){
19869    printf("enter mutex %p (%d) with nRef=%d\n", p, p->trace, p->nRef);
19870  }
19871#endif
19872}
19873static int pthreadMutexTry(sqlite3_mutex *p){
19874  int rc;
19875  assert( p->id==SQLITE_MUTEX_RECURSIVE || pthreadMutexNotheld(p) );
19876
19877#ifdef SQLITE_HOMEGROWN_RECURSIVE_MUTEX
19878  /* If recursive mutexes are not available, then we have to grow
19879  ** our own.  This implementation assumes that pthread_equal()
19880  ** is atomic - that it cannot be deceived into thinking self
19881  ** and p->owner are equal if p->owner changes between two values
19882  ** that are not equal to self while the comparison is taking place.
19883  ** This implementation also assumes a coherent cache - that
19884  ** separate processes cannot read different values from the same
19885  ** address at the same time.  If either of these two conditions
19886  ** are not met, then the mutexes will fail and problems will result.
19887  */
19888  {
19889    pthread_t self = pthread_self();
19890    if( p->nRef>0 && pthread_equal(p->owner, self) ){
19891      p->nRef++;
19892      rc = SQLITE_OK;
19893    }else if( pthread_mutex_trylock(&p->mutex)==0 ){
19894      assert( p->nRef==0 );
19895      p->owner = self;
19896      p->nRef = 1;
19897      rc = SQLITE_OK;
19898    }else{
19899      rc = SQLITE_BUSY;
19900    }
19901  }
19902#else
19903  /* Use the built-in recursive mutexes if they are available.
19904  */
19905  if( pthread_mutex_trylock(&p->mutex)==0 ){
19906#if SQLITE_MUTEX_NREF
19907    p->owner = pthread_self();
19908    p->nRef++;
19909#endif
19910    rc = SQLITE_OK;
19911  }else{
19912    rc = SQLITE_BUSY;
19913  }
19914#endif
19915
19916#ifdef SQLITE_DEBUG
19917  if( rc==SQLITE_OK && p->trace ){
19918    printf("enter mutex %p (%d) with nRef=%d\n", p, p->trace, p->nRef);
19919  }
19920#endif
19921  return rc;
19922}
19923
19924/*
19925** The sqlite3_mutex_leave() routine exits a mutex that was
19926** previously entered by the same thread.  The behavior
19927** is undefined if the mutex is not currently entered or
19928** is not currently allocated.  SQLite will never do either.
19929*/
19930static void pthreadMutexLeave(sqlite3_mutex *p){
19931  assert( pthreadMutexHeld(p) );
19932#if SQLITE_MUTEX_NREF
19933  p->nRef--;
19934  if( p->nRef==0 ) p->owner = 0;
19935#endif
19936  assert( p->nRef==0 || p->id==SQLITE_MUTEX_RECURSIVE );
19937
19938#ifdef SQLITE_HOMEGROWN_RECURSIVE_MUTEX
19939  if( p->nRef==0 ){
19940    pthread_mutex_unlock(&p->mutex);
19941  }
19942#else
19943  pthread_mutex_unlock(&p->mutex);
19944#endif
19945
19946#ifdef SQLITE_DEBUG
19947  if( p->trace ){
19948    printf("leave mutex %p (%d) with nRef=%d\n", p, p->trace, p->nRef);
19949  }
19950#endif
19951}
19952
19953SQLITE_PRIVATE sqlite3_mutex_methods const *sqlite3DefaultMutex(void){
19954  static const sqlite3_mutex_methods sMutex = {
19955    pthreadMutexInit,
19956    pthreadMutexEnd,
19957    pthreadMutexAlloc,
19958    pthreadMutexFree,
19959    pthreadMutexEnter,
19960    pthreadMutexTry,
19961    pthreadMutexLeave,
19962#ifdef SQLITE_DEBUG
19963    pthreadMutexHeld,
19964    pthreadMutexNotheld
19965#else
19966    0,
19967    0
19968#endif
19969  };
19970
19971  return &sMutex;
19972}
19973
19974#endif /* SQLITE_MUTEX_PTHREADS */
19975
19976/************** End of mutex_unix.c ******************************************/
19977/************** Begin file mutex_w32.c ***************************************/
19978/*
19979** 2007 August 14
19980**
19981** The author disclaims copyright to this source code.  In place of
19982** a legal notice, here is a blessing:
19983**
19984**    May you do good and not evil.
19985**    May you find forgiveness for yourself and forgive others.
19986**    May you share freely, never taking more than you give.
19987**
19988*************************************************************************
19989** This file contains the C functions that implement mutexes for Win32.
19990*/
19991/* #include "sqliteInt.h" */
19992
19993#if SQLITE_OS_WIN
19994/*
19995** Include code that is common to all os_*.c files
19996*/
19997/************** Include os_common.h in the middle of mutex_w32.c *************/
19998/************** Begin file os_common.h ***************************************/
19999/*
20000** 2004 May 22
20001**
20002** The author disclaims copyright to this source code.  In place of
20003** a legal notice, here is a blessing:
20004**
20005**    May you do good and not evil.
20006**    May you find forgiveness for yourself and forgive others.
20007**    May you share freely, never taking more than you give.
20008**
20009******************************************************************************
20010**
20011** This file contains macros and a little bit of code that is common to
20012** all of the platform-specific files (os_*.c) and is #included into those
20013** files.
20014**
20015** This file should be #included by the os_*.c files only.  It is not a
20016** general purpose header file.
20017*/
20018#ifndef _OS_COMMON_H_
20019#define _OS_COMMON_H_
20020
20021/*
20022** At least two bugs have slipped in because we changed the MEMORY_DEBUG
20023** macro to SQLITE_DEBUG and some older makefiles have not yet made the
20024** switch.  The following code should catch this problem at compile-time.
20025*/
20026#ifdef MEMORY_DEBUG
20027# error "The MEMORY_DEBUG macro is obsolete.  Use SQLITE_DEBUG instead."
20028#endif
20029
20030/*
20031** Macros for performance tracing.  Normally turned off.  Only works
20032** on i486 hardware.
20033*/
20034#ifdef SQLITE_PERFORMANCE_TRACE
20035
20036/*
20037** hwtime.h contains inline assembler code for implementing
20038** high-performance timing routines.
20039*/
20040/************** Include hwtime.h in the middle of os_common.h ****************/
20041/************** Begin file hwtime.h ******************************************/
20042/*
20043** 2008 May 27
20044**
20045** The author disclaims copyright to this source code.  In place of
20046** a legal notice, here is a blessing:
20047**
20048**    May you do good and not evil.
20049**    May you find forgiveness for yourself and forgive others.
20050**    May you share freely, never taking more than you give.
20051**
20052******************************************************************************
20053**
20054** This file contains inline asm code for retrieving "high-performance"
20055** counters for x86 class CPUs.
20056*/
20057#ifndef _HWTIME_H_
20058#define _HWTIME_H_
20059
20060/*
20061** The following routine only works on pentium-class (or newer) processors.
20062** It uses the RDTSC opcode to read the cycle count value out of the
20063** processor and returns that value.  This can be used for high-res
20064** profiling.
20065*/
20066#if (defined(__GNUC__) || defined(_MSC_VER)) && \
20067      (defined(i386) || defined(__i386__) || defined(_M_IX86))
20068
20069  #if defined(__GNUC__)
20070
20071  __inline__ sqlite_uint64 sqlite3Hwtime(void){
20072     unsigned int lo, hi;
20073     __asm__ __volatile__ ("rdtsc" : "=a" (lo), "=d" (hi));
20074     return (sqlite_uint64)hi << 32 | lo;
20075  }
20076
20077  #elif defined(_MSC_VER)
20078
20079  __declspec(naked) __inline sqlite_uint64 __cdecl sqlite3Hwtime(void){
20080     __asm {
20081        rdtsc
20082        ret       ; return value at EDX:EAX
20083     }
20084  }
20085
20086  #endif
20087
20088#elif (defined(__GNUC__) && defined(__x86_64__))
20089
20090  __inline__ sqlite_uint64 sqlite3Hwtime(void){
20091      unsigned long val;
20092      __asm__ __volatile__ ("rdtsc" : "=A" (val));
20093      return val;
20094  }
20095
20096#elif (defined(__GNUC__) && defined(__ppc__))
20097
20098  __inline__ sqlite_uint64 sqlite3Hwtime(void){
20099      unsigned long long retval;
20100      unsigned long junk;
20101      __asm__ __volatile__ ("\n\
20102          1:      mftbu   %1\n\
20103                  mftb    %L0\n\
20104                  mftbu   %0\n\
20105                  cmpw    %0,%1\n\
20106                  bne     1b"
20107                  : "=r" (retval), "=r" (junk));
20108      return retval;
20109  }
20110
20111#else
20112
20113  #error Need implementation of sqlite3Hwtime() for your platform.
20114
20115  /*
20116  ** To compile without implementing sqlite3Hwtime() for your platform,
20117  ** you can remove the above #error and use the following
20118  ** stub function.  You will lose timing support for many
20119  ** of the debugging and testing utilities, but it should at
20120  ** least compile and run.
20121  */
20122SQLITE_PRIVATE   sqlite_uint64 sqlite3Hwtime(void){ return ((sqlite_uint64)0); }
20123
20124#endif
20125
20126#endif /* !defined(_HWTIME_H_) */
20127
20128/************** End of hwtime.h **********************************************/
20129/************** Continuing where we left off in os_common.h ******************/
20130
20131static sqlite_uint64 g_start;
20132static sqlite_uint64 g_elapsed;
20133#define TIMER_START       g_start=sqlite3Hwtime()
20134#define TIMER_END         g_elapsed=sqlite3Hwtime()-g_start
20135#define TIMER_ELAPSED     g_elapsed
20136#else
20137#define TIMER_START
20138#define TIMER_END
20139#define TIMER_ELAPSED     ((sqlite_uint64)0)
20140#endif
20141
20142/*
20143** If we compile with the SQLITE_TEST macro set, then the following block
20144** of code will give us the ability to simulate a disk I/O error.  This
20145** is used for testing the I/O recovery logic.
20146*/
20147#ifdef SQLITE_TEST
20148SQLITE_API int sqlite3_io_error_hit = 0;            /* Total number of I/O Errors */
20149SQLITE_API int sqlite3_io_error_hardhit = 0;        /* Number of non-benign errors */
20150SQLITE_API int sqlite3_io_error_pending = 0;        /* Count down to first I/O error */
20151SQLITE_API int sqlite3_io_error_persist = 0;        /* True if I/O errors persist */
20152SQLITE_API int sqlite3_io_error_benign = 0;         /* True if errors are benign */
20153SQLITE_API int sqlite3_diskfull_pending = 0;
20154SQLITE_API int sqlite3_diskfull = 0;
20155#define SimulateIOErrorBenign(X) sqlite3_io_error_benign=(X)
20156#define SimulateIOError(CODE)  \
20157  if( (sqlite3_io_error_persist && sqlite3_io_error_hit) \
20158       || sqlite3_io_error_pending-- == 1 )  \
20159              { local_ioerr(); CODE; }
20160static void local_ioerr(){
20161  IOTRACE(("IOERR\n"));
20162  sqlite3_io_error_hit++;
20163  if( !sqlite3_io_error_benign ) sqlite3_io_error_hardhit++;
20164}
20165#define SimulateDiskfullError(CODE) \
20166   if( sqlite3_diskfull_pending ){ \
20167     if( sqlite3_diskfull_pending == 1 ){ \
20168       local_ioerr(); \
20169       sqlite3_diskfull = 1; \
20170       sqlite3_io_error_hit = 1; \
20171       CODE; \
20172     }else{ \
20173       sqlite3_diskfull_pending--; \
20174     } \
20175   }
20176#else
20177#define SimulateIOErrorBenign(X)
20178#define SimulateIOError(A)
20179#define SimulateDiskfullError(A)
20180#endif
20181
20182/*
20183** When testing, keep a count of the number of open files.
20184*/
20185#ifdef SQLITE_TEST
20186SQLITE_API int sqlite3_open_file_count = 0;
20187#define OpenCounter(X)  sqlite3_open_file_count+=(X)
20188#else
20189#define OpenCounter(X)
20190#endif
20191
20192#endif /* !defined(_OS_COMMON_H_) */
20193
20194/************** End of os_common.h *******************************************/
20195/************** Continuing where we left off in mutex_w32.c ******************/
20196
20197/*
20198** Include the header file for the Windows VFS.
20199*/
20200/************** Include os_win.h in the middle of mutex_w32.c ****************/
20201/************** Begin file os_win.h ******************************************/
20202/*
20203** 2013 November 25
20204**
20205** The author disclaims copyright to this source code.  In place of
20206** a legal notice, here is a blessing:
20207**
20208**    May you do good and not evil.
20209**    May you find forgiveness for yourself and forgive others.
20210**    May you share freely, never taking more than you give.
20211**
20212******************************************************************************
20213**
20214** This file contains code that is specific to Windows.
20215*/
20216#ifndef _OS_WIN_H_
20217#define _OS_WIN_H_
20218
20219/*
20220** Include the primary Windows SDK header file.
20221*/
20222#include "windows.h"
20223
20224#ifdef __CYGWIN__
20225# include <sys/cygwin.h>
20226# include <errno.h> /* amalgamator: dontcache */
20227#endif
20228
20229/*
20230** Determine if we are dealing with Windows NT.
20231**
20232** We ought to be able to determine if we are compiling for Windows 9x or
20233** Windows NT using the _WIN32_WINNT macro as follows:
20234**
20235** #if defined(_WIN32_WINNT)
20236** # define SQLITE_OS_WINNT 1
20237** #else
20238** # define SQLITE_OS_WINNT 0
20239** #endif
20240**
20241** However, Visual Studio 2005 does not set _WIN32_WINNT by default, as
20242** it ought to, so the above test does not work.  We'll just assume that
20243** everything is Windows NT unless the programmer explicitly says otherwise
20244** by setting SQLITE_OS_WINNT to 0.
20245*/
20246#if SQLITE_OS_WIN && !defined(SQLITE_OS_WINNT)
20247# define SQLITE_OS_WINNT 1
20248#endif
20249
20250/*
20251** Determine if we are dealing with Windows CE - which has a much reduced
20252** API.
20253*/
20254#if defined(_WIN32_WCE)
20255# define SQLITE_OS_WINCE 1
20256#else
20257# define SQLITE_OS_WINCE 0
20258#endif
20259
20260/*
20261** Determine if we are dealing with WinRT, which provides only a subset of
20262** the full Win32 API.
20263*/
20264#if !defined(SQLITE_OS_WINRT)
20265# define SQLITE_OS_WINRT 0
20266#endif
20267
20268/*
20269** For WinCE, some API function parameters do not appear to be declared as
20270** volatile.
20271*/
20272#if SQLITE_OS_WINCE
20273# define SQLITE_WIN32_VOLATILE
20274#else
20275# define SQLITE_WIN32_VOLATILE volatile
20276#endif
20277
20278/*
20279** For some Windows sub-platforms, the _beginthreadex() / _endthreadex()
20280** functions are not available (e.g. those not using MSVC, Cygwin, etc).
20281*/
20282#if SQLITE_OS_WIN && !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && \
20283    SQLITE_THREADSAFE>0 && !defined(__CYGWIN__)
20284# define SQLITE_OS_WIN_THREADS 1
20285#else
20286# define SQLITE_OS_WIN_THREADS 0
20287#endif
20288
20289#endif /* _OS_WIN_H_ */
20290
20291/************** End of os_win.h **********************************************/
20292/************** Continuing where we left off in mutex_w32.c ******************/
20293#endif
20294
20295/*
20296** The code in this file is only used if we are compiling multithreaded
20297** on a Win32 system.
20298*/
20299#ifdef SQLITE_MUTEX_W32
20300
20301/*
20302** Each recursive mutex is an instance of the following structure.
20303*/
20304struct sqlite3_mutex {
20305  CRITICAL_SECTION mutex;    /* Mutex controlling the lock */
20306  int id;                    /* Mutex type */
20307#ifdef SQLITE_DEBUG
20308  volatile int nRef;         /* Number of enterances */
20309  volatile DWORD owner;      /* Thread holding this mutex */
20310  volatile int trace;        /* True to trace changes */
20311#endif
20312};
20313
20314/*
20315** These are the initializer values used when declaring a "static" mutex
20316** on Win32.  It should be noted that all mutexes require initialization
20317** on the Win32 platform.
20318*/
20319#define SQLITE_W32_MUTEX_INITIALIZER { 0 }
20320
20321#ifdef SQLITE_DEBUG
20322#define SQLITE3_MUTEX_INITIALIZER { SQLITE_W32_MUTEX_INITIALIZER, 0, \
20323                                    0L, (DWORD)0, 0 }
20324#else
20325#define SQLITE3_MUTEX_INITIALIZER { SQLITE_W32_MUTEX_INITIALIZER, 0 }
20326#endif
20327
20328#ifdef SQLITE_DEBUG
20329/*
20330** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routine are
20331** intended for use only inside assert() statements.
20332*/
20333static int winMutexHeld(sqlite3_mutex *p){
20334  return p->nRef!=0 && p->owner==GetCurrentThreadId();
20335}
20336
20337static int winMutexNotheld2(sqlite3_mutex *p, DWORD tid){
20338  return p->nRef==0 || p->owner!=tid;
20339}
20340
20341static int winMutexNotheld(sqlite3_mutex *p){
20342  DWORD tid = GetCurrentThreadId();
20343  return winMutexNotheld2(p, tid);
20344}
20345#endif
20346
20347/*
20348** Initialize and deinitialize the mutex subsystem.
20349*/
20350static sqlite3_mutex winMutex_staticMutexes[] = {
20351  SQLITE3_MUTEX_INITIALIZER,
20352  SQLITE3_MUTEX_INITIALIZER,
20353  SQLITE3_MUTEX_INITIALIZER,
20354  SQLITE3_MUTEX_INITIALIZER,
20355  SQLITE3_MUTEX_INITIALIZER,
20356  SQLITE3_MUTEX_INITIALIZER,
20357  SQLITE3_MUTEX_INITIALIZER,
20358  SQLITE3_MUTEX_INITIALIZER,
20359  SQLITE3_MUTEX_INITIALIZER,
20360  SQLITE3_MUTEX_INITIALIZER,
20361  SQLITE3_MUTEX_INITIALIZER,
20362  SQLITE3_MUTEX_INITIALIZER
20363};
20364
20365static int winMutex_isInit = 0;
20366static int winMutex_isNt = -1; /* <0 means "need to query" */
20367
20368/* As the winMutexInit() and winMutexEnd() functions are called as part
20369** of the sqlite3_initialize() and sqlite3_shutdown() processing, the
20370** "interlocked" magic used here is probably not strictly necessary.
20371*/
20372static LONG SQLITE_WIN32_VOLATILE winMutex_lock = 0;
20373
20374SQLITE_API int SQLITE_STDCALL sqlite3_win32_is_nt(void); /* os_win.c */
20375SQLITE_API void SQLITE_STDCALL sqlite3_win32_sleep(DWORD milliseconds); /* os_win.c */
20376
20377static int winMutexInit(void){
20378  /* The first to increment to 1 does actual initialization */
20379  if( InterlockedCompareExchange(&winMutex_lock, 1, 0)==0 ){
20380    int i;
20381    for(i=0; i<ArraySize(winMutex_staticMutexes); i++){
20382#if SQLITE_OS_WINRT
20383      InitializeCriticalSectionEx(&winMutex_staticMutexes[i].mutex, 0, 0);
20384#else
20385      InitializeCriticalSection(&winMutex_staticMutexes[i].mutex);
20386#endif
20387    }
20388    winMutex_isInit = 1;
20389  }else{
20390    /* Another thread is (in the process of) initializing the static
20391    ** mutexes */
20392    while( !winMutex_isInit ){
20393      sqlite3_win32_sleep(1);
20394    }
20395  }
20396  return SQLITE_OK;
20397}
20398
20399static int winMutexEnd(void){
20400  /* The first to decrement to 0 does actual shutdown
20401  ** (which should be the last to shutdown.) */
20402  if( InterlockedCompareExchange(&winMutex_lock, 0, 1)==1 ){
20403    if( winMutex_isInit==1 ){
20404      int i;
20405      for(i=0; i<ArraySize(winMutex_staticMutexes); i++){
20406        DeleteCriticalSection(&winMutex_staticMutexes[i].mutex);
20407      }
20408      winMutex_isInit = 0;
20409    }
20410  }
20411  return SQLITE_OK;
20412}
20413
20414/*
20415** The sqlite3_mutex_alloc() routine allocates a new
20416** mutex and returns a pointer to it.  If it returns NULL
20417** that means that a mutex could not be allocated.  SQLite
20418** will unwind its stack and return an error.  The argument
20419** to sqlite3_mutex_alloc() is one of these integer constants:
20420**
20421** <ul>
20422** <li>  SQLITE_MUTEX_FAST
20423** <li>  SQLITE_MUTEX_RECURSIVE
20424** <li>  SQLITE_MUTEX_STATIC_MASTER
20425** <li>  SQLITE_MUTEX_STATIC_MEM
20426** <li>  SQLITE_MUTEX_STATIC_OPEN
20427** <li>  SQLITE_MUTEX_STATIC_PRNG
20428** <li>  SQLITE_MUTEX_STATIC_LRU
20429** <li>  SQLITE_MUTEX_STATIC_PMEM
20430** <li>  SQLITE_MUTEX_STATIC_APP1
20431** <li>  SQLITE_MUTEX_STATIC_APP2
20432** <li>  SQLITE_MUTEX_STATIC_APP3
20433** <li>  SQLITE_MUTEX_STATIC_VFS1
20434** <li>  SQLITE_MUTEX_STATIC_VFS2
20435** <li>  SQLITE_MUTEX_STATIC_VFS3
20436** </ul>
20437**
20438** The first two constants cause sqlite3_mutex_alloc() to create
20439** a new mutex.  The new mutex is recursive when SQLITE_MUTEX_RECURSIVE
20440** is used but not necessarily so when SQLITE_MUTEX_FAST is used.
20441** The mutex implementation does not need to make a distinction
20442** between SQLITE_MUTEX_RECURSIVE and SQLITE_MUTEX_FAST if it does
20443** not want to.  But SQLite will only request a recursive mutex in
20444** cases where it really needs one.  If a faster non-recursive mutex
20445** implementation is available on the host platform, the mutex subsystem
20446** might return such a mutex in response to SQLITE_MUTEX_FAST.
20447**
20448** The other allowed parameters to sqlite3_mutex_alloc() each return
20449** a pointer to a static preexisting mutex.  Six static mutexes are
20450** used by the current version of SQLite.  Future versions of SQLite
20451** may add additional static mutexes.  Static mutexes are for internal
20452** use by SQLite only.  Applications that use SQLite mutexes should
20453** use only the dynamic mutexes returned by SQLITE_MUTEX_FAST or
20454** SQLITE_MUTEX_RECURSIVE.
20455**
20456** Note that if one of the dynamic mutex parameters (SQLITE_MUTEX_FAST
20457** or SQLITE_MUTEX_RECURSIVE) is used then sqlite3_mutex_alloc()
20458** returns a different mutex on every call.  But for the static
20459** mutex types, the same mutex is returned on every call that has
20460** the same type number.
20461*/
20462static sqlite3_mutex *winMutexAlloc(int iType){
20463  sqlite3_mutex *p;
20464
20465  switch( iType ){
20466    case SQLITE_MUTEX_FAST:
20467    case SQLITE_MUTEX_RECURSIVE: {
20468      p = sqlite3MallocZero( sizeof(*p) );
20469      if( p ){
20470        p->id = iType;
20471#ifdef SQLITE_DEBUG
20472#ifdef SQLITE_WIN32_MUTEX_TRACE_DYNAMIC
20473        p->trace = 1;
20474#endif
20475#endif
20476#if SQLITE_OS_WINRT
20477        InitializeCriticalSectionEx(&p->mutex, 0, 0);
20478#else
20479        InitializeCriticalSection(&p->mutex);
20480#endif
20481      }
20482      break;
20483    }
20484    default: {
20485#ifdef SQLITE_ENABLE_API_ARMOR
20486      if( iType-2<0 || iType-2>=ArraySize(winMutex_staticMutexes) ){
20487        (void)SQLITE_MISUSE_BKPT;
20488        return 0;
20489      }
20490#endif
20491      p = &winMutex_staticMutexes[iType-2];
20492      p->id = iType;
20493#ifdef SQLITE_DEBUG
20494#ifdef SQLITE_WIN32_MUTEX_TRACE_STATIC
20495      p->trace = 1;
20496#endif
20497#endif
20498      break;
20499    }
20500  }
20501  return p;
20502}
20503
20504
20505/*
20506** This routine deallocates a previously
20507** allocated mutex.  SQLite is careful to deallocate every
20508** mutex that it allocates.
20509*/
20510static void winMutexFree(sqlite3_mutex *p){
20511  assert( p );
20512  assert( p->nRef==0 && p->owner==0 );
20513  if( p->id==SQLITE_MUTEX_FAST || p->id==SQLITE_MUTEX_RECURSIVE ){
20514    DeleteCriticalSection(&p->mutex);
20515    sqlite3_free(p);
20516  }else{
20517#ifdef SQLITE_ENABLE_API_ARMOR
20518    (void)SQLITE_MISUSE_BKPT;
20519#endif
20520  }
20521}
20522
20523/*
20524** The sqlite3_mutex_enter() and sqlite3_mutex_try() routines attempt
20525** to enter a mutex.  If another thread is already within the mutex,
20526** sqlite3_mutex_enter() will block and sqlite3_mutex_try() will return
20527** SQLITE_BUSY.  The sqlite3_mutex_try() interface returns SQLITE_OK
20528** upon successful entry.  Mutexes created using SQLITE_MUTEX_RECURSIVE can
20529** be entered multiple times by the same thread.  In such cases the,
20530** mutex must be exited an equal number of times before another thread
20531** can enter.  If the same thread tries to enter any other kind of mutex
20532** more than once, the behavior is undefined.
20533*/
20534static void winMutexEnter(sqlite3_mutex *p){
20535#if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
20536  DWORD tid = GetCurrentThreadId();
20537#endif
20538#ifdef SQLITE_DEBUG
20539  assert( p );
20540  assert( p->id==SQLITE_MUTEX_RECURSIVE || winMutexNotheld2(p, tid) );
20541#else
20542  assert( p );
20543#endif
20544  assert( winMutex_isInit==1 );
20545  EnterCriticalSection(&p->mutex);
20546#ifdef SQLITE_DEBUG
20547  assert( p->nRef>0 || p->owner==0 );
20548  p->owner = tid;
20549  p->nRef++;
20550  if( p->trace ){
20551    OSTRACE(("ENTER-MUTEX tid=%lu, mutex=%p (%d), nRef=%d\n",
20552             tid, p, p->trace, p->nRef));
20553  }
20554#endif
20555}
20556
20557static int winMutexTry(sqlite3_mutex *p){
20558#if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
20559  DWORD tid = GetCurrentThreadId();
20560#endif
20561  int rc = SQLITE_BUSY;
20562  assert( p );
20563  assert( p->id==SQLITE_MUTEX_RECURSIVE || winMutexNotheld2(p, tid) );
20564  /*
20565  ** The sqlite3_mutex_try() routine is very rarely used, and when it
20566  ** is used it is merely an optimization.  So it is OK for it to always
20567  ** fail.
20568  **
20569  ** The TryEnterCriticalSection() interface is only available on WinNT.
20570  ** And some windows compilers complain if you try to use it without
20571  ** first doing some #defines that prevent SQLite from building on Win98.
20572  ** For that reason, we will omit this optimization for now.  See
20573  ** ticket #2685.
20574  */
20575#if defined(_WIN32_WINNT) && _WIN32_WINNT >= 0x0400
20576  assert( winMutex_isInit==1 );
20577  assert( winMutex_isNt>=-1 && winMutex_isNt<=1 );
20578  if( winMutex_isNt<0 ){
20579    winMutex_isNt = sqlite3_win32_is_nt();
20580  }
20581  assert( winMutex_isNt==0 || winMutex_isNt==1 );
20582  if( winMutex_isNt && TryEnterCriticalSection(&p->mutex) ){
20583#ifdef SQLITE_DEBUG
20584    p->owner = tid;
20585    p->nRef++;
20586#endif
20587    rc = SQLITE_OK;
20588  }
20589#else
20590  UNUSED_PARAMETER(p);
20591#endif
20592#ifdef SQLITE_DEBUG
20593  if( p->trace ){
20594    OSTRACE(("TRY-MUTEX tid=%lu, mutex=%p (%d), owner=%lu, nRef=%d, rc=%s\n",
20595             tid, p, p->trace, p->owner, p->nRef, sqlite3ErrName(rc)));
20596  }
20597#endif
20598  return rc;
20599}
20600
20601/*
20602** The sqlite3_mutex_leave() routine exits a mutex that was
20603** previously entered by the same thread.  The behavior
20604** is undefined if the mutex is not currently entered or
20605** is not currently allocated.  SQLite will never do either.
20606*/
20607static void winMutexLeave(sqlite3_mutex *p){
20608#if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
20609  DWORD tid = GetCurrentThreadId();
20610#endif
20611  assert( p );
20612#ifdef SQLITE_DEBUG
20613  assert( p->nRef>0 );
20614  assert( p->owner==tid );
20615  p->nRef--;
20616  if( p->nRef==0 ) p->owner = 0;
20617  assert( p->nRef==0 || p->id==SQLITE_MUTEX_RECURSIVE );
20618#endif
20619  assert( winMutex_isInit==1 );
20620  LeaveCriticalSection(&p->mutex);
20621#ifdef SQLITE_DEBUG
20622  if( p->trace ){
20623    OSTRACE(("LEAVE-MUTEX tid=%lu, mutex=%p (%d), nRef=%d\n",
20624             tid, p, p->trace, p->nRef));
20625  }
20626#endif
20627}
20628
20629SQLITE_PRIVATE sqlite3_mutex_methods const *sqlite3DefaultMutex(void){
20630  static const sqlite3_mutex_methods sMutex = {
20631    winMutexInit,
20632    winMutexEnd,
20633    winMutexAlloc,
20634    winMutexFree,
20635    winMutexEnter,
20636    winMutexTry,
20637    winMutexLeave,
20638#ifdef SQLITE_DEBUG
20639    winMutexHeld,
20640    winMutexNotheld
20641#else
20642    0,
20643    0
20644#endif
20645  };
20646  return &sMutex;
20647}
20648
20649#endif /* SQLITE_MUTEX_W32 */
20650
20651/************** End of mutex_w32.c *******************************************/
20652/************** Begin file malloc.c ******************************************/
20653/*
20654** 2001 September 15
20655**
20656** The author disclaims copyright to this source code.  In place of
20657** a legal notice, here is a blessing:
20658**
20659**    May you do good and not evil.
20660**    May you find forgiveness for yourself and forgive others.
20661**    May you share freely, never taking more than you give.
20662**
20663*************************************************************************
20664**
20665** Memory allocation functions used throughout sqlite.
20666*/
20667/* #include "sqliteInt.h" */
20668/* #include <stdarg.h> */
20669
20670/*
20671** Attempt to release up to n bytes of non-essential memory currently
20672** held by SQLite. An example of non-essential memory is memory used to
20673** cache database pages that are not currently in use.
20674*/
20675SQLITE_API int SQLITE_STDCALL sqlite3_release_memory(int n){
20676#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
20677  return sqlite3PcacheReleaseMemory(n);
20678#else
20679  /* IMPLEMENTATION-OF: R-34391-24921 The sqlite3_release_memory() routine
20680  ** is a no-op returning zero if SQLite is not compiled with
20681  ** SQLITE_ENABLE_MEMORY_MANAGEMENT. */
20682  UNUSED_PARAMETER(n);
20683  return 0;
20684#endif
20685}
20686
20687/*
20688** An instance of the following object records the location of
20689** each unused scratch buffer.
20690*/
20691typedef struct ScratchFreeslot {
20692  struct ScratchFreeslot *pNext;   /* Next unused scratch buffer */
20693} ScratchFreeslot;
20694
20695/*
20696** State information local to the memory allocation subsystem.
20697*/
20698static SQLITE_WSD struct Mem0Global {
20699  sqlite3_mutex *mutex;         /* Mutex to serialize access */
20700
20701  /*
20702  ** The alarm callback and its arguments.  The mem0.mutex lock will
20703  ** be held while the callback is running.  Recursive calls into
20704  ** the memory subsystem are allowed, but no new callbacks will be
20705  ** issued.
20706  */
20707  sqlite3_int64 alarmThreshold;
20708  void (*alarmCallback)(void*, sqlite3_int64,int);
20709  void *alarmArg;
20710
20711  /*
20712  ** Pointers to the end of sqlite3GlobalConfig.pScratch memory
20713  ** (so that a range test can be used to determine if an allocation
20714  ** being freed came from pScratch) and a pointer to the list of
20715  ** unused scratch allocations.
20716  */
20717  void *pScratchEnd;
20718  ScratchFreeslot *pScratchFree;
20719  u32 nScratchFree;
20720
20721  /*
20722  ** True if heap is nearly "full" where "full" is defined by the
20723  ** sqlite3_soft_heap_limit() setting.
20724  */
20725  int nearlyFull;
20726} mem0 = { 0, 0, 0, 0, 0, 0, 0, 0 };
20727
20728#define mem0 GLOBAL(struct Mem0Global, mem0)
20729
20730/*
20731** Return the memory allocator mutex. sqlite3_status() needs it.
20732*/
20733SQLITE_PRIVATE sqlite3_mutex *sqlite3MallocMutex(void){
20734  return mem0.mutex;
20735}
20736
20737/*
20738** This routine runs when the memory allocator sees that the
20739** total memory allocation is about to exceed the soft heap
20740** limit.
20741*/
20742static void softHeapLimitEnforcer(
20743  void *NotUsed,
20744  sqlite3_int64 NotUsed2,
20745  int allocSize
20746){
20747  UNUSED_PARAMETER2(NotUsed, NotUsed2);
20748  sqlite3_release_memory(allocSize);
20749}
20750
20751/*
20752** Change the alarm callback
20753*/
20754static int sqlite3MemoryAlarm(
20755  void(*xCallback)(void *pArg, sqlite3_int64 used,int N),
20756  void *pArg,
20757  sqlite3_int64 iThreshold
20758){
20759  sqlite3_int64 nUsed;
20760  sqlite3_mutex_enter(mem0.mutex);
20761  mem0.alarmCallback = xCallback;
20762  mem0.alarmArg = pArg;
20763  mem0.alarmThreshold = iThreshold;
20764  nUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED);
20765  mem0.nearlyFull = (iThreshold>0 && iThreshold<=nUsed);
20766  sqlite3_mutex_leave(mem0.mutex);
20767  return SQLITE_OK;
20768}
20769
20770#ifndef SQLITE_OMIT_DEPRECATED
20771/*
20772** Deprecated external interface.  Internal/core SQLite code
20773** should call sqlite3MemoryAlarm.
20774*/
20775SQLITE_API int SQLITE_STDCALL sqlite3_memory_alarm(
20776  void(*xCallback)(void *pArg, sqlite3_int64 used,int N),
20777  void *pArg,
20778  sqlite3_int64 iThreshold
20779){
20780  return sqlite3MemoryAlarm(xCallback, pArg, iThreshold);
20781}
20782#endif
20783
20784/*
20785** Set the soft heap-size limit for the library. Passing a zero or
20786** negative value indicates no limit.
20787*/
20788SQLITE_API sqlite3_int64 SQLITE_STDCALL sqlite3_soft_heap_limit64(sqlite3_int64 n){
20789  sqlite3_int64 priorLimit;
20790  sqlite3_int64 excess;
20791#ifndef SQLITE_OMIT_AUTOINIT
20792  int rc = sqlite3_initialize();
20793  if( rc ) return -1;
20794#endif
20795  sqlite3_mutex_enter(mem0.mutex);
20796  priorLimit = mem0.alarmThreshold;
20797  sqlite3_mutex_leave(mem0.mutex);
20798  if( n<0 ) return priorLimit;
20799  if( n>0 ){
20800    sqlite3MemoryAlarm(softHeapLimitEnforcer, 0, n);
20801  }else{
20802    sqlite3MemoryAlarm(0, 0, 0);
20803  }
20804  excess = sqlite3_memory_used() - n;
20805  if( excess>0 ) sqlite3_release_memory((int)(excess & 0x7fffffff));
20806  return priorLimit;
20807}
20808SQLITE_API void SQLITE_STDCALL sqlite3_soft_heap_limit(int n){
20809  if( n<0 ) n = 0;
20810  sqlite3_soft_heap_limit64(n);
20811}
20812
20813/*
20814** Initialize the memory allocation subsystem.
20815*/
20816SQLITE_PRIVATE int sqlite3MallocInit(void){
20817  int rc;
20818  if( sqlite3GlobalConfig.m.xMalloc==0 ){
20819    sqlite3MemSetDefault();
20820  }
20821  memset(&mem0, 0, sizeof(mem0));
20822  if( sqlite3GlobalConfig.bCoreMutex ){
20823    mem0.mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MEM);
20824  }
20825  if( sqlite3GlobalConfig.pScratch && sqlite3GlobalConfig.szScratch>=100
20826      && sqlite3GlobalConfig.nScratch>0 ){
20827    int i, n, sz;
20828    ScratchFreeslot *pSlot;
20829    sz = ROUNDDOWN8(sqlite3GlobalConfig.szScratch);
20830    sqlite3GlobalConfig.szScratch = sz;
20831    pSlot = (ScratchFreeslot*)sqlite3GlobalConfig.pScratch;
20832    n = sqlite3GlobalConfig.nScratch;
20833    mem0.pScratchFree = pSlot;
20834    mem0.nScratchFree = n;
20835    for(i=0; i<n-1; i++){
20836      pSlot->pNext = (ScratchFreeslot*)(sz+(char*)pSlot);
20837      pSlot = pSlot->pNext;
20838    }
20839    pSlot->pNext = 0;
20840    mem0.pScratchEnd = (void*)&pSlot[1];
20841  }else{
20842    mem0.pScratchEnd = 0;
20843    sqlite3GlobalConfig.pScratch = 0;
20844    sqlite3GlobalConfig.szScratch = 0;
20845    sqlite3GlobalConfig.nScratch = 0;
20846  }
20847  if( sqlite3GlobalConfig.pPage==0 || sqlite3GlobalConfig.szPage<512
20848      || sqlite3GlobalConfig.nPage<=0 ){
20849    sqlite3GlobalConfig.pPage = 0;
20850    sqlite3GlobalConfig.szPage = 0;
20851  }
20852  rc = sqlite3GlobalConfig.m.xInit(sqlite3GlobalConfig.m.pAppData);
20853  if( rc!=SQLITE_OK ) memset(&mem0, 0, sizeof(mem0));
20854  return rc;
20855}
20856
20857/*
20858** Return true if the heap is currently under memory pressure - in other
20859** words if the amount of heap used is close to the limit set by
20860** sqlite3_soft_heap_limit().
20861*/
20862SQLITE_PRIVATE int sqlite3HeapNearlyFull(void){
20863  return mem0.nearlyFull;
20864}
20865
20866/*
20867** Deinitialize the memory allocation subsystem.
20868*/
20869SQLITE_PRIVATE void sqlite3MallocEnd(void){
20870  if( sqlite3GlobalConfig.m.xShutdown ){
20871    sqlite3GlobalConfig.m.xShutdown(sqlite3GlobalConfig.m.pAppData);
20872  }
20873  memset(&mem0, 0, sizeof(mem0));
20874}
20875
20876/*
20877** Return the amount of memory currently checked out.
20878*/
20879SQLITE_API sqlite3_int64 SQLITE_STDCALL sqlite3_memory_used(void){
20880  sqlite3_int64 res, mx;
20881  sqlite3_status64(SQLITE_STATUS_MEMORY_USED, &res, &mx, 0);
20882  return res;
20883}
20884
20885/*
20886** Return the maximum amount of memory that has ever been
20887** checked out since either the beginning of this process
20888** or since the most recent reset.
20889*/
20890SQLITE_API sqlite3_int64 SQLITE_STDCALL sqlite3_memory_highwater(int resetFlag){
20891  sqlite3_int64 res, mx;
20892  sqlite3_status64(SQLITE_STATUS_MEMORY_USED, &res, &mx, resetFlag);
20893  return mx;
20894}
20895
20896/*
20897** Trigger the alarm
20898*/
20899static void sqlite3MallocAlarm(int nByte){
20900  void (*xCallback)(void*,sqlite3_int64,int);
20901  sqlite3_int64 nowUsed;
20902  void *pArg;
20903  if( mem0.alarmCallback==0 ) return;
20904  xCallback = mem0.alarmCallback;
20905  nowUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED);
20906  pArg = mem0.alarmArg;
20907  mem0.alarmCallback = 0;
20908  sqlite3_mutex_leave(mem0.mutex);
20909  xCallback(pArg, nowUsed, nByte);
20910  sqlite3_mutex_enter(mem0.mutex);
20911  mem0.alarmCallback = xCallback;
20912  mem0.alarmArg = pArg;
20913}
20914
20915/*
20916** Do a memory allocation with statistics and alarms.  Assume the
20917** lock is already held.
20918*/
20919static int mallocWithAlarm(int n, void **pp){
20920  int nFull;
20921  void *p;
20922  assert( sqlite3_mutex_held(mem0.mutex) );
20923  nFull = sqlite3GlobalConfig.m.xRoundup(n);
20924  sqlite3StatusSet(SQLITE_STATUS_MALLOC_SIZE, n);
20925  if( mem0.alarmCallback!=0 ){
20926    sqlite3_int64 nUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED);
20927    if( nUsed >= mem0.alarmThreshold - nFull ){
20928      mem0.nearlyFull = 1;
20929      sqlite3MallocAlarm(nFull);
20930    }else{
20931      mem0.nearlyFull = 0;
20932    }
20933  }
20934  p = sqlite3GlobalConfig.m.xMalloc(nFull);
20935#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
20936  if( p==0 && mem0.alarmCallback ){
20937    sqlite3MallocAlarm(nFull);
20938    p = sqlite3GlobalConfig.m.xMalloc(nFull);
20939  }
20940#endif
20941  if( p ){
20942    nFull = sqlite3MallocSize(p);
20943    sqlite3StatusUp(SQLITE_STATUS_MEMORY_USED, nFull);
20944    sqlite3StatusUp(SQLITE_STATUS_MALLOC_COUNT, 1);
20945  }
20946  *pp = p;
20947  return nFull;
20948}
20949
20950/*
20951** Allocate memory.  This routine is like sqlite3_malloc() except that it
20952** assumes the memory subsystem has already been initialized.
20953*/
20954SQLITE_PRIVATE void *sqlite3Malloc(u64 n){
20955  void *p;
20956  if( n==0 || n>=0x7fffff00 ){
20957    /* A memory allocation of a number of bytes which is near the maximum
20958    ** signed integer value might cause an integer overflow inside of the
20959    ** xMalloc().  Hence we limit the maximum size to 0x7fffff00, giving
20960    ** 255 bytes of overhead.  SQLite itself will never use anything near
20961    ** this amount.  The only way to reach the limit is with sqlite3_malloc() */
20962    p = 0;
20963  }else if( sqlite3GlobalConfig.bMemstat ){
20964    sqlite3_mutex_enter(mem0.mutex);
20965    mallocWithAlarm((int)n, &p);
20966    sqlite3_mutex_leave(mem0.mutex);
20967  }else{
20968    p = sqlite3GlobalConfig.m.xMalloc((int)n);
20969  }
20970  assert( EIGHT_BYTE_ALIGNMENT(p) );  /* IMP: R-11148-40995 */
20971  return p;
20972}
20973
20974/*
20975** This version of the memory allocation is for use by the application.
20976** First make sure the memory subsystem is initialized, then do the
20977** allocation.
20978*/
20979SQLITE_API void *SQLITE_STDCALL sqlite3_malloc(int n){
20980#ifndef SQLITE_OMIT_AUTOINIT
20981  if( sqlite3_initialize() ) return 0;
20982#endif
20983  return n<=0 ? 0 : sqlite3Malloc(n);
20984}
20985SQLITE_API void *SQLITE_STDCALL sqlite3_malloc64(sqlite3_uint64 n){
20986#ifndef SQLITE_OMIT_AUTOINIT
20987  if( sqlite3_initialize() ) return 0;
20988#endif
20989  return sqlite3Malloc(n);
20990}
20991
20992/*
20993** Each thread may only have a single outstanding allocation from
20994** xScratchMalloc().  We verify this constraint in the single-threaded
20995** case by setting scratchAllocOut to 1 when an allocation
20996** is outstanding clearing it when the allocation is freed.
20997*/
20998#if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
20999static int scratchAllocOut = 0;
21000#endif
21001
21002
21003/*
21004** Allocate memory that is to be used and released right away.
21005** This routine is similar to alloca() in that it is not intended
21006** for situations where the memory might be held long-term.  This
21007** routine is intended to get memory to old large transient data
21008** structures that would not normally fit on the stack of an
21009** embedded processor.
21010*/
21011SQLITE_PRIVATE void *sqlite3ScratchMalloc(int n){
21012  void *p;
21013  assert( n>0 );
21014
21015  sqlite3_mutex_enter(mem0.mutex);
21016  sqlite3StatusSet(SQLITE_STATUS_SCRATCH_SIZE, n);
21017  if( mem0.nScratchFree && sqlite3GlobalConfig.szScratch>=n ){
21018    p = mem0.pScratchFree;
21019    mem0.pScratchFree = mem0.pScratchFree->pNext;
21020    mem0.nScratchFree--;
21021    sqlite3StatusUp(SQLITE_STATUS_SCRATCH_USED, 1);
21022    sqlite3_mutex_leave(mem0.mutex);
21023  }else{
21024    sqlite3_mutex_leave(mem0.mutex);
21025    p = sqlite3Malloc(n);
21026    if( sqlite3GlobalConfig.bMemstat && p ){
21027      sqlite3_mutex_enter(mem0.mutex);
21028      sqlite3StatusUp(SQLITE_STATUS_SCRATCH_OVERFLOW, sqlite3MallocSize(p));
21029      sqlite3_mutex_leave(mem0.mutex);
21030    }
21031    sqlite3MemdebugSetType(p, MEMTYPE_SCRATCH);
21032  }
21033  assert( sqlite3_mutex_notheld(mem0.mutex) );
21034
21035
21036#if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
21037  /* EVIDENCE-OF: R-12970-05880 SQLite will not use more than one scratch
21038  ** buffers per thread.
21039  **
21040  ** This can only be checked in single-threaded mode.
21041  */
21042  assert( scratchAllocOut==0 );
21043  if( p ) scratchAllocOut++;
21044#endif
21045
21046  return p;
21047}
21048SQLITE_PRIVATE void sqlite3ScratchFree(void *p){
21049  if( p ){
21050
21051#if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
21052    /* Verify that no more than two scratch allocation per thread
21053    ** is outstanding at one time.  (This is only checked in the
21054    ** single-threaded case since checking in the multi-threaded case
21055    ** would be much more complicated.) */
21056    assert( scratchAllocOut>=1 && scratchAllocOut<=2 );
21057    scratchAllocOut--;
21058#endif
21059
21060    if( p>=sqlite3GlobalConfig.pScratch && p<mem0.pScratchEnd ){
21061      /* Release memory from the SQLITE_CONFIG_SCRATCH allocation */
21062      ScratchFreeslot *pSlot;
21063      pSlot = (ScratchFreeslot*)p;
21064      sqlite3_mutex_enter(mem0.mutex);
21065      pSlot->pNext = mem0.pScratchFree;
21066      mem0.pScratchFree = pSlot;
21067      mem0.nScratchFree++;
21068      assert( mem0.nScratchFree <= (u32)sqlite3GlobalConfig.nScratch );
21069      sqlite3StatusDown(SQLITE_STATUS_SCRATCH_USED, 1);
21070      sqlite3_mutex_leave(mem0.mutex);
21071    }else{
21072      /* Release memory back to the heap */
21073      assert( sqlite3MemdebugHasType(p, MEMTYPE_SCRATCH) );
21074      assert( sqlite3MemdebugNoType(p, (u8)~MEMTYPE_SCRATCH) );
21075      sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
21076      if( sqlite3GlobalConfig.bMemstat ){
21077        int iSize = sqlite3MallocSize(p);
21078        sqlite3_mutex_enter(mem0.mutex);
21079        sqlite3StatusDown(SQLITE_STATUS_SCRATCH_OVERFLOW, iSize);
21080        sqlite3StatusDown(SQLITE_STATUS_MEMORY_USED, iSize);
21081        sqlite3StatusDown(SQLITE_STATUS_MALLOC_COUNT, 1);
21082        sqlite3GlobalConfig.m.xFree(p);
21083        sqlite3_mutex_leave(mem0.mutex);
21084      }else{
21085        sqlite3GlobalConfig.m.xFree(p);
21086      }
21087    }
21088  }
21089}
21090
21091/*
21092** TRUE if p is a lookaside memory allocation from db
21093*/
21094#ifndef SQLITE_OMIT_LOOKASIDE
21095static int isLookaside(sqlite3 *db, void *p){
21096  return p>=db->lookaside.pStart && p<db->lookaside.pEnd;
21097}
21098#else
21099#define isLookaside(A,B) 0
21100#endif
21101
21102/*
21103** Return the size of a memory allocation previously obtained from
21104** sqlite3Malloc() or sqlite3_malloc().
21105*/
21106SQLITE_PRIVATE int sqlite3MallocSize(void *p){
21107  assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) );
21108  return sqlite3GlobalConfig.m.xSize(p);
21109}
21110SQLITE_PRIVATE int sqlite3DbMallocSize(sqlite3 *db, void *p){
21111  if( db==0 ){
21112    assert( sqlite3MemdebugNoType(p, (u8)~MEMTYPE_HEAP) );
21113    assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) );
21114    return sqlite3MallocSize(p);
21115  }else{
21116    assert( sqlite3_mutex_held(db->mutex) );
21117    if( isLookaside(db, p) ){
21118      return db->lookaside.sz;
21119    }else{
21120      assert( sqlite3MemdebugHasType(p, (MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
21121      assert( sqlite3MemdebugNoType(p, (u8)~(MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
21122      return sqlite3GlobalConfig.m.xSize(p);
21123    }
21124  }
21125}
21126SQLITE_API sqlite3_uint64 SQLITE_STDCALL sqlite3_msize(void *p){
21127  assert( sqlite3MemdebugNoType(p, (u8)~MEMTYPE_HEAP) );
21128  assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) );
21129  return (sqlite3_uint64)sqlite3GlobalConfig.m.xSize(p);
21130}
21131
21132/*
21133** Free memory previously obtained from sqlite3Malloc().
21134*/
21135SQLITE_API void SQLITE_STDCALL sqlite3_free(void *p){
21136  if( p==0 ) return;  /* IMP: R-49053-54554 */
21137  assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) );
21138  assert( sqlite3MemdebugNoType(p, (u8)~MEMTYPE_HEAP) );
21139  if( sqlite3GlobalConfig.bMemstat ){
21140    sqlite3_mutex_enter(mem0.mutex);
21141    sqlite3StatusDown(SQLITE_STATUS_MEMORY_USED, sqlite3MallocSize(p));
21142    sqlite3StatusDown(SQLITE_STATUS_MALLOC_COUNT, 1);
21143    sqlite3GlobalConfig.m.xFree(p);
21144    sqlite3_mutex_leave(mem0.mutex);
21145  }else{
21146    sqlite3GlobalConfig.m.xFree(p);
21147  }
21148}
21149
21150/*
21151** Add the size of memory allocation "p" to the count in
21152** *db->pnBytesFreed.
21153*/
21154static SQLITE_NOINLINE void measureAllocationSize(sqlite3 *db, void *p){
21155  *db->pnBytesFreed += sqlite3DbMallocSize(db,p);
21156}
21157
21158/*
21159** Free memory that might be associated with a particular database
21160** connection.
21161*/
21162SQLITE_PRIVATE void sqlite3DbFree(sqlite3 *db, void *p){
21163  assert( db==0 || sqlite3_mutex_held(db->mutex) );
21164  if( p==0 ) return;
21165  if( db ){
21166    if( db->pnBytesFreed ){
21167      measureAllocationSize(db, p);
21168      return;
21169    }
21170    if( isLookaside(db, p) ){
21171      LookasideSlot *pBuf = (LookasideSlot*)p;
21172#if SQLITE_DEBUG
21173      /* Trash all content in the buffer being freed */
21174      memset(p, 0xaa, db->lookaside.sz);
21175#endif
21176      pBuf->pNext = db->lookaside.pFree;
21177      db->lookaside.pFree = pBuf;
21178      db->lookaside.nOut--;
21179      return;
21180    }
21181  }
21182  assert( sqlite3MemdebugHasType(p, (MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
21183  assert( sqlite3MemdebugNoType(p, (u8)~(MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
21184  assert( db!=0 || sqlite3MemdebugNoType(p, MEMTYPE_LOOKASIDE) );
21185  sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
21186  sqlite3_free(p);
21187}
21188
21189/*
21190** Change the size of an existing memory allocation
21191*/
21192SQLITE_PRIVATE void *sqlite3Realloc(void *pOld, u64 nBytes){
21193  int nOld, nNew, nDiff;
21194  void *pNew;
21195  assert( sqlite3MemdebugHasType(pOld, MEMTYPE_HEAP) );
21196  assert( sqlite3MemdebugNoType(pOld, (u8)~MEMTYPE_HEAP) );
21197  if( pOld==0 ){
21198    return sqlite3Malloc(nBytes); /* IMP: R-04300-56712 */
21199  }
21200  if( nBytes==0 ){
21201    sqlite3_free(pOld); /* IMP: R-26507-47431 */
21202    return 0;
21203  }
21204  if( nBytes>=0x7fffff00 ){
21205    /* The 0x7ffff00 limit term is explained in comments on sqlite3Malloc() */
21206    return 0;
21207  }
21208  nOld = sqlite3MallocSize(pOld);
21209  /* IMPLEMENTATION-OF: R-46199-30249 SQLite guarantees that the second
21210  ** argument to xRealloc is always a value returned by a prior call to
21211  ** xRoundup. */
21212  nNew = sqlite3GlobalConfig.m.xRoundup((int)nBytes);
21213  if( nOld==nNew ){
21214    pNew = pOld;
21215  }else if( sqlite3GlobalConfig.bMemstat ){
21216    sqlite3_mutex_enter(mem0.mutex);
21217    sqlite3StatusSet(SQLITE_STATUS_MALLOC_SIZE, (int)nBytes);
21218    nDiff = nNew - nOld;
21219    if( sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED) >=
21220          mem0.alarmThreshold-nDiff ){
21221      sqlite3MallocAlarm(nDiff);
21222    }
21223    pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew);
21224    if( pNew==0 && mem0.alarmCallback ){
21225      sqlite3MallocAlarm((int)nBytes);
21226      pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew);
21227    }
21228    if( pNew ){
21229      nNew = sqlite3MallocSize(pNew);
21230      sqlite3StatusUp(SQLITE_STATUS_MEMORY_USED, nNew-nOld);
21231    }
21232    sqlite3_mutex_leave(mem0.mutex);
21233  }else{
21234    pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew);
21235  }
21236  assert( EIGHT_BYTE_ALIGNMENT(pNew) ); /* IMP: R-11148-40995 */
21237  return pNew;
21238}
21239
21240/*
21241** The public interface to sqlite3Realloc.  Make sure that the memory
21242** subsystem is initialized prior to invoking sqliteRealloc.
21243*/
21244SQLITE_API void *SQLITE_STDCALL sqlite3_realloc(void *pOld, int n){
21245#ifndef SQLITE_OMIT_AUTOINIT
21246  if( sqlite3_initialize() ) return 0;
21247#endif
21248  if( n<0 ) n = 0;  /* IMP: R-26507-47431 */
21249  return sqlite3Realloc(pOld, n);
21250}
21251SQLITE_API void *SQLITE_STDCALL sqlite3_realloc64(void *pOld, sqlite3_uint64 n){
21252#ifndef SQLITE_OMIT_AUTOINIT
21253  if( sqlite3_initialize() ) return 0;
21254#endif
21255  return sqlite3Realloc(pOld, n);
21256}
21257
21258
21259/*
21260** Allocate and zero memory.
21261*/
21262SQLITE_PRIVATE void *sqlite3MallocZero(u64 n){
21263  void *p = sqlite3Malloc(n);
21264  if( p ){
21265    memset(p, 0, (size_t)n);
21266  }
21267  return p;
21268}
21269
21270/*
21271** Allocate and zero memory.  If the allocation fails, make
21272** the mallocFailed flag in the connection pointer.
21273*/
21274SQLITE_PRIVATE void *sqlite3DbMallocZero(sqlite3 *db, u64 n){
21275  void *p = sqlite3DbMallocRaw(db, n);
21276  if( p ){
21277    memset(p, 0, (size_t)n);
21278  }
21279  return p;
21280}
21281
21282/*
21283** Allocate and zero memory.  If the allocation fails, make
21284** the mallocFailed flag in the connection pointer.
21285**
21286** If db!=0 and db->mallocFailed is true (indicating a prior malloc
21287** failure on the same database connection) then always return 0.
21288** Hence for a particular database connection, once malloc starts
21289** failing, it fails consistently until mallocFailed is reset.
21290** This is an important assumption.  There are many places in the
21291** code that do things like this:
21292**
21293**         int *a = (int*)sqlite3DbMallocRaw(db, 100);
21294**         int *b = (int*)sqlite3DbMallocRaw(db, 200);
21295**         if( b ) a[10] = 9;
21296**
21297** In other words, if a subsequent malloc (ex: "b") worked, it is assumed
21298** that all prior mallocs (ex: "a") worked too.
21299*/
21300SQLITE_PRIVATE void *sqlite3DbMallocRaw(sqlite3 *db, u64 n){
21301  void *p;
21302  assert( db==0 || sqlite3_mutex_held(db->mutex) );
21303  assert( db==0 || db->pnBytesFreed==0 );
21304#ifndef SQLITE_OMIT_LOOKASIDE
21305  if( db ){
21306    LookasideSlot *pBuf;
21307    if( db->mallocFailed ){
21308      return 0;
21309    }
21310    if( db->lookaside.bEnabled ){
21311      if( n>db->lookaside.sz ){
21312        db->lookaside.anStat[1]++;
21313      }else if( (pBuf = db->lookaside.pFree)==0 ){
21314        db->lookaside.anStat[2]++;
21315      }else{
21316        db->lookaside.pFree = pBuf->pNext;
21317        db->lookaside.nOut++;
21318        db->lookaside.anStat[0]++;
21319        if( db->lookaside.nOut>db->lookaside.mxOut ){
21320          db->lookaside.mxOut = db->lookaside.nOut;
21321        }
21322        return (void*)pBuf;
21323      }
21324    }
21325  }
21326#else
21327  if( db && db->mallocFailed ){
21328    return 0;
21329  }
21330#endif
21331  p = sqlite3Malloc(n);
21332  if( !p && db ){
21333    db->mallocFailed = 1;
21334  }
21335  sqlite3MemdebugSetType(p,
21336         (db && db->lookaside.bEnabled) ? MEMTYPE_LOOKASIDE : MEMTYPE_HEAP);
21337  return p;
21338}
21339
21340/*
21341** Resize the block of memory pointed to by p to n bytes. If the
21342** resize fails, set the mallocFailed flag in the connection object.
21343*/
21344SQLITE_PRIVATE void *sqlite3DbRealloc(sqlite3 *db, void *p, u64 n){
21345  void *pNew = 0;
21346  assert( db!=0 );
21347  assert( sqlite3_mutex_held(db->mutex) );
21348  if( db->mallocFailed==0 ){
21349    if( p==0 ){
21350      return sqlite3DbMallocRaw(db, n);
21351    }
21352    if( isLookaside(db, p) ){
21353      if( n<=db->lookaside.sz ){
21354        return p;
21355      }
21356      pNew = sqlite3DbMallocRaw(db, n);
21357      if( pNew ){
21358        memcpy(pNew, p, db->lookaside.sz);
21359        sqlite3DbFree(db, p);
21360      }
21361    }else{
21362      assert( sqlite3MemdebugHasType(p, (MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
21363      assert( sqlite3MemdebugNoType(p, (u8)~(MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
21364      sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
21365      pNew = sqlite3_realloc64(p, n);
21366      if( !pNew ){
21367        db->mallocFailed = 1;
21368      }
21369      sqlite3MemdebugSetType(pNew,
21370            (db->lookaside.bEnabled ? MEMTYPE_LOOKASIDE : MEMTYPE_HEAP));
21371    }
21372  }
21373  return pNew;
21374}
21375
21376/*
21377** Attempt to reallocate p.  If the reallocation fails, then free p
21378** and set the mallocFailed flag in the database connection.
21379*/
21380SQLITE_PRIVATE void *sqlite3DbReallocOrFree(sqlite3 *db, void *p, u64 n){
21381  void *pNew;
21382  pNew = sqlite3DbRealloc(db, p, n);
21383  if( !pNew ){
21384    sqlite3DbFree(db, p);
21385  }
21386  return pNew;
21387}
21388
21389/*
21390** Make a copy of a string in memory obtained from sqliteMalloc(). These
21391** functions call sqlite3MallocRaw() directly instead of sqliteMalloc(). This
21392** is because when memory debugging is turned on, these two functions are
21393** called via macros that record the current file and line number in the
21394** ThreadData structure.
21395*/
21396SQLITE_PRIVATE char *sqlite3DbStrDup(sqlite3 *db, const char *z){
21397  char *zNew;
21398  size_t n;
21399  if( z==0 ){
21400    return 0;
21401  }
21402  n = sqlite3Strlen30(z) + 1;
21403  assert( (n&0x7fffffff)==n );
21404  zNew = sqlite3DbMallocRaw(db, (int)n);
21405  if( zNew ){
21406    memcpy(zNew, z, n);
21407  }
21408  return zNew;
21409}
21410SQLITE_PRIVATE char *sqlite3DbStrNDup(sqlite3 *db, const char *z, u64 n){
21411  char *zNew;
21412  if( z==0 ){
21413    return 0;
21414  }
21415  assert( (n&0x7fffffff)==n );
21416  zNew = sqlite3DbMallocRaw(db, n+1);
21417  if( zNew ){
21418    memcpy(zNew, z, (size_t)n);
21419    zNew[n] = 0;
21420  }
21421  return zNew;
21422}
21423
21424/*
21425** Free any prior content in *pz and replace it with a copy of zNew.
21426*/
21427SQLITE_PRIVATE void sqlite3SetString(char **pz, sqlite3 *db, const char *zNew){
21428  sqlite3DbFree(db, *pz);
21429  *pz = sqlite3DbStrDup(db, zNew);
21430}
21431
21432/*
21433** Take actions at the end of an API call to indicate an OOM error
21434*/
21435static SQLITE_NOINLINE int apiOomError(sqlite3 *db){
21436  db->mallocFailed = 0;
21437  sqlite3Error(db, SQLITE_NOMEM);
21438  return SQLITE_NOMEM;
21439}
21440
21441/*
21442** This function must be called before exiting any API function (i.e.
21443** returning control to the user) that has called sqlite3_malloc or
21444** sqlite3_realloc.
21445**
21446** The returned value is normally a copy of the second argument to this
21447** function. However, if a malloc() failure has occurred since the previous
21448** invocation SQLITE_NOMEM is returned instead.
21449**
21450** If an OOM as occurred, then the connection error-code (the value
21451** returned by sqlite3_errcode()) is set to SQLITE_NOMEM.
21452*/
21453SQLITE_PRIVATE int sqlite3ApiExit(sqlite3* db, int rc){
21454  /* If the db handle must hold the connection handle mutex here.
21455  ** Otherwise the read (and possible write) of db->mallocFailed
21456  ** is unsafe, as is the call to sqlite3Error().
21457  */
21458  assert( db!=0 );
21459  assert( sqlite3_mutex_held(db->mutex) );
21460  if( db->mallocFailed || rc==SQLITE_IOERR_NOMEM ){
21461    return apiOomError(db);
21462  }
21463  return rc & db->errMask;
21464}
21465
21466/************** End of malloc.c **********************************************/
21467/************** Begin file printf.c ******************************************/
21468/*
21469** The "printf" code that follows dates from the 1980's.  It is in
21470** the public domain.
21471**
21472**************************************************************************
21473**
21474** This file contains code for a set of "printf"-like routines.  These
21475** routines format strings much like the printf() from the standard C
21476** library, though the implementation here has enhancements to support
21477** SQLite.
21478*/
21479/* #include "sqliteInt.h" */
21480
21481/*
21482** Conversion types fall into various categories as defined by the
21483** following enumeration.
21484*/
21485#define etRADIX       1 /* Integer types.  %d, %x, %o, and so forth */
21486#define etFLOAT       2 /* Floating point.  %f */
21487#define etEXP         3 /* Exponentional notation. %e and %E */
21488#define etGENERIC     4 /* Floating or exponential, depending on exponent. %g */
21489#define etSIZE        5 /* Return number of characters processed so far. %n */
21490#define etSTRING      6 /* Strings. %s */
21491#define etDYNSTRING   7 /* Dynamically allocated strings. %z */
21492#define etPERCENT     8 /* Percent symbol. %% */
21493#define etCHARX       9 /* Characters. %c */
21494/* The rest are extensions, not normally found in printf() */
21495#define etSQLESCAPE  10 /* Strings with '\'' doubled.  %q */
21496#define etSQLESCAPE2 11 /* Strings with '\'' doubled and enclosed in '',
21497                          NULL pointers replaced by SQL NULL.  %Q */
21498#define etTOKEN      12 /* a pointer to a Token structure */
21499#define etSRCLIST    13 /* a pointer to a SrcList */
21500#define etPOINTER    14 /* The %p conversion */
21501#define etSQLESCAPE3 15 /* %w -> Strings with '\"' doubled */
21502#define etORDINAL    16 /* %r -> 1st, 2nd, 3rd, 4th, etc.  English only */
21503
21504#define etINVALID     0 /* Any unrecognized conversion type */
21505
21506
21507/*
21508** An "etByte" is an 8-bit unsigned value.
21509*/
21510typedef unsigned char etByte;
21511
21512/*
21513** Each builtin conversion character (ex: the 'd' in "%d") is described
21514** by an instance of the following structure
21515*/
21516typedef struct et_info {   /* Information about each format field */
21517  char fmttype;            /* The format field code letter */
21518  etByte base;             /* The base for radix conversion */
21519  etByte flags;            /* One or more of FLAG_ constants below */
21520  etByte type;             /* Conversion paradigm */
21521  etByte charset;          /* Offset into aDigits[] of the digits string */
21522  etByte prefix;           /* Offset into aPrefix[] of the prefix string */
21523} et_info;
21524
21525/*
21526** Allowed values for et_info.flags
21527*/
21528#define FLAG_SIGNED  1     /* True if the value to convert is signed */
21529#define FLAG_INTERN  2     /* True if for internal use only */
21530#define FLAG_STRING  4     /* Allow infinity precision */
21531
21532
21533/*
21534** The following table is searched linearly, so it is good to put the
21535** most frequently used conversion types first.
21536*/
21537static const char aDigits[] = "0123456789ABCDEF0123456789abcdef";
21538static const char aPrefix[] = "-x0\000X0";
21539static const et_info fmtinfo[] = {
21540  {  'd', 10, 1, etRADIX,      0,  0 },
21541  {  's',  0, 4, etSTRING,     0,  0 },
21542  {  'g',  0, 1, etGENERIC,    30, 0 },
21543  {  'z',  0, 4, etDYNSTRING,  0,  0 },
21544  {  'q',  0, 4, etSQLESCAPE,  0,  0 },
21545  {  'Q',  0, 4, etSQLESCAPE2, 0,  0 },
21546  {  'w',  0, 4, etSQLESCAPE3, 0,  0 },
21547  {  'c',  0, 0, etCHARX,      0,  0 },
21548  {  'o',  8, 0, etRADIX,      0,  2 },
21549  {  'u', 10, 0, etRADIX,      0,  0 },
21550  {  'x', 16, 0, etRADIX,      16, 1 },
21551  {  'X', 16, 0, etRADIX,      0,  4 },
21552#ifndef SQLITE_OMIT_FLOATING_POINT
21553  {  'f',  0, 1, etFLOAT,      0,  0 },
21554  {  'e',  0, 1, etEXP,        30, 0 },
21555  {  'E',  0, 1, etEXP,        14, 0 },
21556  {  'G',  0, 1, etGENERIC,    14, 0 },
21557#endif
21558  {  'i', 10, 1, etRADIX,      0,  0 },
21559  {  'n',  0, 0, etSIZE,       0,  0 },
21560  {  '%',  0, 0, etPERCENT,    0,  0 },
21561  {  'p', 16, 0, etPOINTER,    0,  1 },
21562
21563/* All the rest have the FLAG_INTERN bit set and are thus for internal
21564** use only */
21565  {  'T',  0, 2, etTOKEN,      0,  0 },
21566  {  'S',  0, 2, etSRCLIST,    0,  0 },
21567  {  'r', 10, 3, etORDINAL,    0,  0 },
21568};
21569
21570/*
21571** If SQLITE_OMIT_FLOATING_POINT is defined, then none of the floating point
21572** conversions will work.
21573*/
21574#ifndef SQLITE_OMIT_FLOATING_POINT
21575/*
21576** "*val" is a double such that 0.1 <= *val < 10.0
21577** Return the ascii code for the leading digit of *val, then
21578** multiply "*val" by 10.0 to renormalize.
21579**
21580** Example:
21581**     input:     *val = 3.14159
21582**     output:    *val = 1.4159    function return = '3'
21583**
21584** The counter *cnt is incremented each time.  After counter exceeds
21585** 16 (the number of significant digits in a 64-bit float) '0' is
21586** always returned.
21587*/
21588static char et_getdigit(LONGDOUBLE_TYPE *val, int *cnt){
21589  int digit;
21590  LONGDOUBLE_TYPE d;
21591  if( (*cnt)<=0 ) return '0';
21592  (*cnt)--;
21593  digit = (int)*val;
21594  d = digit;
21595  digit += '0';
21596  *val = (*val - d)*10.0;
21597  return (char)digit;
21598}
21599#endif /* SQLITE_OMIT_FLOATING_POINT */
21600
21601/*
21602** Set the StrAccum object to an error mode.
21603*/
21604static void setStrAccumError(StrAccum *p, u8 eError){
21605  assert( eError==STRACCUM_NOMEM || eError==STRACCUM_TOOBIG );
21606  p->accError = eError;
21607  p->nAlloc = 0;
21608}
21609
21610/*
21611** Extra argument values from a PrintfArguments object
21612*/
21613static sqlite3_int64 getIntArg(PrintfArguments *p){
21614  if( p->nArg<=p->nUsed ) return 0;
21615  return sqlite3_value_int64(p->apArg[p->nUsed++]);
21616}
21617static double getDoubleArg(PrintfArguments *p){
21618  if( p->nArg<=p->nUsed ) return 0.0;
21619  return sqlite3_value_double(p->apArg[p->nUsed++]);
21620}
21621static char *getTextArg(PrintfArguments *p){
21622  if( p->nArg<=p->nUsed ) return 0;
21623  return (char*)sqlite3_value_text(p->apArg[p->nUsed++]);
21624}
21625
21626
21627/*
21628** On machines with a small stack size, you can redefine the
21629** SQLITE_PRINT_BUF_SIZE to be something smaller, if desired.
21630*/
21631#ifndef SQLITE_PRINT_BUF_SIZE
21632# define SQLITE_PRINT_BUF_SIZE 70
21633#endif
21634#define etBUFSIZE SQLITE_PRINT_BUF_SIZE  /* Size of the output buffer */
21635
21636/*
21637** Render a string given by "fmt" into the StrAccum object.
21638*/
21639SQLITE_PRIVATE void sqlite3VXPrintf(
21640  StrAccum *pAccum,          /* Accumulate results here */
21641  u32 bFlags,                /* SQLITE_PRINTF_* flags */
21642  const char *fmt,           /* Format string */
21643  va_list ap                 /* arguments */
21644){
21645  int c;                     /* Next character in the format string */
21646  char *bufpt;               /* Pointer to the conversion buffer */
21647  int precision;             /* Precision of the current field */
21648  int length;                /* Length of the field */
21649  int idx;                   /* A general purpose loop counter */
21650  int width;                 /* Width of the current field */
21651  etByte flag_leftjustify;   /* True if "-" flag is present */
21652  etByte flag_plussign;      /* True if "+" flag is present */
21653  etByte flag_blanksign;     /* True if " " flag is present */
21654  etByte flag_alternateform; /* True if "#" flag is present */
21655  etByte flag_altform2;      /* True if "!" flag is present */
21656  etByte flag_zeropad;       /* True if field width constant starts with zero */
21657  etByte flag_long;          /* True if "l" flag is present */
21658  etByte flag_longlong;      /* True if the "ll" flag is present */
21659  etByte done;               /* Loop termination flag */
21660  etByte xtype = 0;          /* Conversion paradigm */
21661  u8 bArgList;               /* True for SQLITE_PRINTF_SQLFUNC */
21662  u8 useIntern;              /* Ok to use internal conversions (ex: %T) */
21663  char prefix;               /* Prefix character.  "+" or "-" or " " or '\0'. */
21664  sqlite_uint64 longvalue;   /* Value for integer types */
21665  LONGDOUBLE_TYPE realvalue; /* Value for real types */
21666  const et_info *infop;      /* Pointer to the appropriate info structure */
21667  char *zOut;                /* Rendering buffer */
21668  int nOut;                  /* Size of the rendering buffer */
21669  char *zExtra = 0;          /* Malloced memory used by some conversion */
21670#ifndef SQLITE_OMIT_FLOATING_POINT
21671  int  exp, e2;              /* exponent of real numbers */
21672  int nsd;                   /* Number of significant digits returned */
21673  double rounder;            /* Used for rounding floating point values */
21674  etByte flag_dp;            /* True if decimal point should be shown */
21675  etByte flag_rtz;           /* True if trailing zeros should be removed */
21676#endif
21677  PrintfArguments *pArgList = 0; /* Arguments for SQLITE_PRINTF_SQLFUNC */
21678  char buf[etBUFSIZE];       /* Conversion buffer */
21679
21680  bufpt = 0;
21681  if( bFlags ){
21682    if( (bArgList = (bFlags & SQLITE_PRINTF_SQLFUNC))!=0 ){
21683      pArgList = va_arg(ap, PrintfArguments*);
21684    }
21685    useIntern = bFlags & SQLITE_PRINTF_INTERNAL;
21686  }else{
21687    bArgList = useIntern = 0;
21688  }
21689  for(; (c=(*fmt))!=0; ++fmt){
21690    if( c!='%' ){
21691      bufpt = (char *)fmt;
21692#if HAVE_STRCHRNUL
21693      fmt = strchrnul(fmt, '%');
21694#else
21695      do{ fmt++; }while( *fmt && *fmt != '%' );
21696#endif
21697      sqlite3StrAccumAppend(pAccum, bufpt, (int)(fmt - bufpt));
21698      if( *fmt==0 ) break;
21699    }
21700    if( (c=(*++fmt))==0 ){
21701      sqlite3StrAccumAppend(pAccum, "%", 1);
21702      break;
21703    }
21704    /* Find out what flags are present */
21705    flag_leftjustify = flag_plussign = flag_blanksign =
21706     flag_alternateform = flag_altform2 = flag_zeropad = 0;
21707    done = 0;
21708    do{
21709      switch( c ){
21710        case '-':   flag_leftjustify = 1;     break;
21711        case '+':   flag_plussign = 1;        break;
21712        case ' ':   flag_blanksign = 1;       break;
21713        case '#':   flag_alternateform = 1;   break;
21714        case '!':   flag_altform2 = 1;        break;
21715        case '0':   flag_zeropad = 1;         break;
21716        default:    done = 1;                 break;
21717      }
21718    }while( !done && (c=(*++fmt))!=0 );
21719    /* Get the field width */
21720    if( c=='*' ){
21721      if( bArgList ){
21722        width = (int)getIntArg(pArgList);
21723      }else{
21724        width = va_arg(ap,int);
21725      }
21726      if( width<0 ){
21727        flag_leftjustify = 1;
21728        width = width >= -2147483647 ? -width : 0;
21729      }
21730      c = *++fmt;
21731    }else{
21732      unsigned wx = 0;
21733      while( c>='0' && c<='9' ){
21734        wx = wx*10 + c - '0';
21735        c = *++fmt;
21736      }
21737      testcase( wx>0x7fffffff );
21738      width = wx & 0x7fffffff;
21739    }
21740
21741    /* Get the precision */
21742    if( c=='.' ){
21743      c = *++fmt;
21744      if( c=='*' ){
21745        if( bArgList ){
21746          precision = (int)getIntArg(pArgList);
21747        }else{
21748          precision = va_arg(ap,int);
21749        }
21750        c = *++fmt;
21751        if( precision<0 ){
21752          precision = precision >= -2147483647 ? -precision : -1;
21753        }
21754      }else{
21755        unsigned px = 0;
21756        while( c>='0' && c<='9' ){
21757          px = px*10 + c - '0';
21758          c = *++fmt;
21759        }
21760        testcase( px>0x7fffffff );
21761        precision = px & 0x7fffffff;
21762      }
21763    }else{
21764      precision = -1;
21765    }
21766    /* Get the conversion type modifier */
21767    if( c=='l' ){
21768      flag_long = 1;
21769      c = *++fmt;
21770      if( c=='l' ){
21771        flag_longlong = 1;
21772        c = *++fmt;
21773      }else{
21774        flag_longlong = 0;
21775      }
21776    }else{
21777      flag_long = flag_longlong = 0;
21778    }
21779    /* Fetch the info entry for the field */
21780    infop = &fmtinfo[0];
21781    xtype = etINVALID;
21782    for(idx=0; idx<ArraySize(fmtinfo); idx++){
21783      if( c==fmtinfo[idx].fmttype ){
21784        infop = &fmtinfo[idx];
21785        if( useIntern || (infop->flags & FLAG_INTERN)==0 ){
21786          xtype = infop->type;
21787        }else{
21788          return;
21789        }
21790        break;
21791      }
21792    }
21793
21794    /*
21795    ** At this point, variables are initialized as follows:
21796    **
21797    **   flag_alternateform          TRUE if a '#' is present.
21798    **   flag_altform2               TRUE if a '!' is present.
21799    **   flag_plussign               TRUE if a '+' is present.
21800    **   flag_leftjustify            TRUE if a '-' is present or if the
21801    **                               field width was negative.
21802    **   flag_zeropad                TRUE if the width began with 0.
21803    **   flag_long                   TRUE if the letter 'l' (ell) prefixed
21804    **                               the conversion character.
21805    **   flag_longlong               TRUE if the letter 'll' (ell ell) prefixed
21806    **                               the conversion character.
21807    **   flag_blanksign              TRUE if a ' ' is present.
21808    **   width                       The specified field width.  This is
21809    **                               always non-negative.  Zero is the default.
21810    **   precision                   The specified precision.  The default
21811    **                               is -1.
21812    **   xtype                       The class of the conversion.
21813    **   infop                       Pointer to the appropriate info struct.
21814    */
21815    switch( xtype ){
21816      case etPOINTER:
21817        flag_longlong = sizeof(char*)==sizeof(i64);
21818        flag_long = sizeof(char*)==sizeof(long int);
21819        /* Fall through into the next case */
21820      case etORDINAL:
21821      case etRADIX:
21822        if( infop->flags & FLAG_SIGNED ){
21823          i64 v;
21824          if( bArgList ){
21825            v = getIntArg(pArgList);
21826          }else if( flag_longlong ){
21827            v = va_arg(ap,i64);
21828          }else if( flag_long ){
21829            v = va_arg(ap,long int);
21830          }else{
21831            v = va_arg(ap,int);
21832          }
21833          if( v<0 ){
21834            if( v==SMALLEST_INT64 ){
21835              longvalue = ((u64)1)<<63;
21836            }else{
21837              longvalue = -v;
21838            }
21839            prefix = '-';
21840          }else{
21841            longvalue = v;
21842            if( flag_plussign )        prefix = '+';
21843            else if( flag_blanksign )  prefix = ' ';
21844            else                       prefix = 0;
21845          }
21846        }else{
21847          if( bArgList ){
21848            longvalue = (u64)getIntArg(pArgList);
21849          }else if( flag_longlong ){
21850            longvalue = va_arg(ap,u64);
21851          }else if( flag_long ){
21852            longvalue = va_arg(ap,unsigned long int);
21853          }else{
21854            longvalue = va_arg(ap,unsigned int);
21855          }
21856          prefix = 0;
21857        }
21858        if( longvalue==0 ) flag_alternateform = 0;
21859        if( flag_zeropad && precision<width-(prefix!=0) ){
21860          precision = width-(prefix!=0);
21861        }
21862        if( precision<etBUFSIZE-10 ){
21863          nOut = etBUFSIZE;
21864          zOut = buf;
21865        }else{
21866          nOut = precision + 10;
21867          zOut = zExtra = sqlite3Malloc( nOut );
21868          if( zOut==0 ){
21869            setStrAccumError(pAccum, STRACCUM_NOMEM);
21870            return;
21871          }
21872        }
21873        bufpt = &zOut[nOut-1];
21874        if( xtype==etORDINAL ){
21875          static const char zOrd[] = "thstndrd";
21876          int x = (int)(longvalue % 10);
21877          if( x>=4 || (longvalue/10)%10==1 ){
21878            x = 0;
21879          }
21880          *(--bufpt) = zOrd[x*2+1];
21881          *(--bufpt) = zOrd[x*2];
21882        }
21883        {
21884          const char *cset = &aDigits[infop->charset];
21885          u8 base = infop->base;
21886          do{                                           /* Convert to ascii */
21887            *(--bufpt) = cset[longvalue%base];
21888            longvalue = longvalue/base;
21889          }while( longvalue>0 );
21890        }
21891        length = (int)(&zOut[nOut-1]-bufpt);
21892        for(idx=precision-length; idx>0; idx--){
21893          *(--bufpt) = '0';                             /* Zero pad */
21894        }
21895        if( prefix ) *(--bufpt) = prefix;               /* Add sign */
21896        if( flag_alternateform && infop->prefix ){      /* Add "0" or "0x" */
21897          const char *pre;
21898          char x;
21899          pre = &aPrefix[infop->prefix];
21900          for(; (x=(*pre))!=0; pre++) *(--bufpt) = x;
21901        }
21902        length = (int)(&zOut[nOut-1]-bufpt);
21903        break;
21904      case etFLOAT:
21905      case etEXP:
21906      case etGENERIC:
21907        if( bArgList ){
21908          realvalue = getDoubleArg(pArgList);
21909        }else{
21910          realvalue = va_arg(ap,double);
21911        }
21912#ifdef SQLITE_OMIT_FLOATING_POINT
21913        length = 0;
21914#else
21915        if( precision<0 ) precision = 6;         /* Set default precision */
21916        if( realvalue<0.0 ){
21917          realvalue = -realvalue;
21918          prefix = '-';
21919        }else{
21920          if( flag_plussign )          prefix = '+';
21921          else if( flag_blanksign )    prefix = ' ';
21922          else                         prefix = 0;
21923        }
21924        if( xtype==etGENERIC && precision>0 ) precision--;
21925        testcase( precision>0xfff );
21926        for(idx=precision&0xfff, rounder=0.5; idx>0; idx--, rounder*=0.1){}
21927        if( xtype==etFLOAT ) realvalue += rounder;
21928        /* Normalize realvalue to within 10.0 > realvalue >= 1.0 */
21929        exp = 0;
21930        if( sqlite3IsNaN((double)realvalue) ){
21931          bufpt = "NaN";
21932          length = 3;
21933          break;
21934        }
21935        if( realvalue>0.0 ){
21936          LONGDOUBLE_TYPE scale = 1.0;
21937          while( realvalue>=1e100*scale && exp<=350 ){ scale *= 1e100;exp+=100;}
21938          while( realvalue>=1e64*scale && exp<=350 ){ scale *= 1e64; exp+=64; }
21939          while( realvalue>=1e8*scale && exp<=350 ){ scale *= 1e8; exp+=8; }
21940          while( realvalue>=10.0*scale && exp<=350 ){ scale *= 10.0; exp++; }
21941          realvalue /= scale;
21942          while( realvalue<1e-8 ){ realvalue *= 1e8; exp-=8; }
21943          while( realvalue<1.0 ){ realvalue *= 10.0; exp--; }
21944          if( exp>350 ){
21945            if( prefix=='-' ){
21946              bufpt = "-Inf";
21947            }else if( prefix=='+' ){
21948              bufpt = "+Inf";
21949            }else{
21950              bufpt = "Inf";
21951            }
21952            length = sqlite3Strlen30(bufpt);
21953            break;
21954          }
21955        }
21956        bufpt = buf;
21957        /*
21958        ** If the field type is etGENERIC, then convert to either etEXP
21959        ** or etFLOAT, as appropriate.
21960        */
21961        if( xtype!=etFLOAT ){
21962          realvalue += rounder;
21963          if( realvalue>=10.0 ){ realvalue *= 0.1; exp++; }
21964        }
21965        if( xtype==etGENERIC ){
21966          flag_rtz = !flag_alternateform;
21967          if( exp<-4 || exp>precision ){
21968            xtype = etEXP;
21969          }else{
21970            precision = precision - exp;
21971            xtype = etFLOAT;
21972          }
21973        }else{
21974          flag_rtz = flag_altform2;
21975        }
21976        if( xtype==etEXP ){
21977          e2 = 0;
21978        }else{
21979          e2 = exp;
21980        }
21981        if( MAX(e2,0)+(i64)precision+(i64)width > etBUFSIZE - 15 ){
21982          bufpt = zExtra
21983              = sqlite3Malloc( MAX(e2,0)+(i64)precision+(i64)width+15 );
21984          if( bufpt==0 ){
21985            setStrAccumError(pAccum, STRACCUM_NOMEM);
21986            return;
21987          }
21988        }
21989        zOut = bufpt;
21990        nsd = 16 + flag_altform2*10;
21991        flag_dp = (precision>0 ?1:0) | flag_alternateform | flag_altform2;
21992        /* The sign in front of the number */
21993        if( prefix ){
21994          *(bufpt++) = prefix;
21995        }
21996        /* Digits prior to the decimal point */
21997        if( e2<0 ){
21998          *(bufpt++) = '0';
21999        }else{
22000          for(; e2>=0; e2--){
22001            *(bufpt++) = et_getdigit(&realvalue,&nsd);
22002          }
22003        }
22004        /* The decimal point */
22005        if( flag_dp ){
22006          *(bufpt++) = '.';
22007        }
22008        /* "0" digits after the decimal point but before the first
22009        ** significant digit of the number */
22010        for(e2++; e2<0; precision--, e2++){
22011          assert( precision>0 );
22012          *(bufpt++) = '0';
22013        }
22014        /* Significant digits after the decimal point */
22015        while( (precision--)>0 ){
22016          *(bufpt++) = et_getdigit(&realvalue,&nsd);
22017        }
22018        /* Remove trailing zeros and the "." if no digits follow the "." */
22019        if( flag_rtz && flag_dp ){
22020          while( bufpt[-1]=='0' ) *(--bufpt) = 0;
22021          assert( bufpt>zOut );
22022          if( bufpt[-1]=='.' ){
22023            if( flag_altform2 ){
22024              *(bufpt++) = '0';
22025            }else{
22026              *(--bufpt) = 0;
22027            }
22028          }
22029        }
22030        /* Add the "eNNN" suffix */
22031        if( xtype==etEXP ){
22032          *(bufpt++) = aDigits[infop->charset];
22033          if( exp<0 ){
22034            *(bufpt++) = '-'; exp = -exp;
22035          }else{
22036            *(bufpt++) = '+';
22037          }
22038          if( exp>=100 ){
22039            *(bufpt++) = (char)((exp/100)+'0');        /* 100's digit */
22040            exp %= 100;
22041          }
22042          *(bufpt++) = (char)(exp/10+'0');             /* 10's digit */
22043          *(bufpt++) = (char)(exp%10+'0');             /* 1's digit */
22044        }
22045        *bufpt = 0;
22046
22047        /* The converted number is in buf[] and zero terminated. Output it.
22048        ** Note that the number is in the usual order, not reversed as with
22049        ** integer conversions. */
22050        length = (int)(bufpt-zOut);
22051        bufpt = zOut;
22052
22053        /* Special case:  Add leading zeros if the flag_zeropad flag is
22054        ** set and we are not left justified */
22055        if( flag_zeropad && !flag_leftjustify && length < width){
22056          int i;
22057          int nPad = width - length;
22058          for(i=width; i>=nPad; i--){
22059            bufpt[i] = bufpt[i-nPad];
22060          }
22061          i = prefix!=0;
22062          while( nPad-- ) bufpt[i++] = '0';
22063          length = width;
22064        }
22065#endif /* !defined(SQLITE_OMIT_FLOATING_POINT) */
22066        break;
22067      case etSIZE:
22068        if( !bArgList ){
22069          *(va_arg(ap,int*)) = pAccum->nChar;
22070        }
22071        length = width = 0;
22072        break;
22073      case etPERCENT:
22074        buf[0] = '%';
22075        bufpt = buf;
22076        length = 1;
22077        break;
22078      case etCHARX:
22079        if( bArgList ){
22080          bufpt = getTextArg(pArgList);
22081          c = bufpt ? bufpt[0] : 0;
22082        }else{
22083          c = va_arg(ap,int);
22084        }
22085        if( precision>1 ){
22086          width -= precision-1;
22087          if( width>1 && !flag_leftjustify ){
22088            sqlite3AppendChar(pAccum, width-1, ' ');
22089            width = 0;
22090          }
22091          sqlite3AppendChar(pAccum, precision-1, c);
22092        }
22093        length = 1;
22094        buf[0] = c;
22095        bufpt = buf;
22096        break;
22097      case etSTRING:
22098      case etDYNSTRING:
22099        if( bArgList ){
22100          bufpt = getTextArg(pArgList);
22101        }else{
22102          bufpt = va_arg(ap,char*);
22103        }
22104        if( bufpt==0 ){
22105          bufpt = "";
22106        }else if( xtype==etDYNSTRING && !bArgList ){
22107          zExtra = bufpt;
22108        }
22109        if( precision>=0 ){
22110          for(length=0; length<precision && bufpt[length]; length++){}
22111        }else{
22112          length = sqlite3Strlen30(bufpt);
22113        }
22114        break;
22115      case etSQLESCAPE:
22116      case etSQLESCAPE2:
22117      case etSQLESCAPE3: {
22118        int i, j, k, n, isnull;
22119        int needQuote;
22120        char ch;
22121        char q = ((xtype==etSQLESCAPE3)?'"':'\'');   /* Quote character */
22122        char *escarg;
22123
22124        if( bArgList ){
22125          escarg = getTextArg(pArgList);
22126        }else{
22127          escarg = va_arg(ap,char*);
22128        }
22129        isnull = escarg==0;
22130        if( isnull ) escarg = (xtype==etSQLESCAPE2 ? "NULL" : "(NULL)");
22131        k = precision;
22132        for(i=n=0; k!=0 && (ch=escarg[i])!=0; i++, k--){
22133          if( ch==q )  n++;
22134        }
22135        needQuote = !isnull && xtype==etSQLESCAPE2;
22136        n += i + 1 + needQuote*2;
22137        if( n>etBUFSIZE ){
22138          bufpt = zExtra = sqlite3Malloc( n );
22139          if( bufpt==0 ){
22140            setStrAccumError(pAccum, STRACCUM_NOMEM);
22141            return;
22142          }
22143        }else{
22144          bufpt = buf;
22145        }
22146        j = 0;
22147        if( needQuote ) bufpt[j++] = q;
22148        k = i;
22149        for(i=0; i<k; i++){
22150          bufpt[j++] = ch = escarg[i];
22151          if( ch==q ) bufpt[j++] = ch;
22152        }
22153        if( needQuote ) bufpt[j++] = q;
22154        bufpt[j] = 0;
22155        length = j;
22156        /* The precision in %q and %Q means how many input characters to
22157        ** consume, not the length of the output...
22158        ** if( precision>=0 && precision<length ) length = precision; */
22159        break;
22160      }
22161      case etTOKEN: {
22162        Token *pToken = va_arg(ap, Token*);
22163        assert( bArgList==0 );
22164        if( pToken && pToken->n ){
22165          sqlite3StrAccumAppend(pAccum, (const char*)pToken->z, pToken->n);
22166        }
22167        length = width = 0;
22168        break;
22169      }
22170      case etSRCLIST: {
22171        SrcList *pSrc = va_arg(ap, SrcList*);
22172        int k = va_arg(ap, int);
22173        struct SrcList_item *pItem = &pSrc->a[k];
22174        assert( bArgList==0 );
22175        assert( k>=0 && k<pSrc->nSrc );
22176        if( pItem->zDatabase ){
22177          sqlite3StrAccumAppendAll(pAccum, pItem->zDatabase);
22178          sqlite3StrAccumAppend(pAccum, ".", 1);
22179        }
22180        sqlite3StrAccumAppendAll(pAccum, pItem->zName);
22181        length = width = 0;
22182        break;
22183      }
22184      default: {
22185        assert( xtype==etINVALID );
22186        return;
22187      }
22188    }/* End switch over the format type */
22189    /*
22190    ** The text of the conversion is pointed to by "bufpt" and is
22191    ** "length" characters long.  The field width is "width".  Do
22192    ** the output.
22193    */
22194    width -= length;
22195    if( width>0 && !flag_leftjustify ) sqlite3AppendChar(pAccum, width, ' ');
22196    sqlite3StrAccumAppend(pAccum, bufpt, length);
22197    if( width>0 && flag_leftjustify ) sqlite3AppendChar(pAccum, width, ' ');
22198
22199    if( zExtra ){
22200      sqlite3_free(zExtra);
22201      zExtra = 0;
22202    }
22203  }/* End for loop over the format string */
22204} /* End of function */
22205
22206/*
22207** Enlarge the memory allocation on a StrAccum object so that it is
22208** able to accept at least N more bytes of text.
22209**
22210** Return the number of bytes of text that StrAccum is able to accept
22211** after the attempted enlargement.  The value returned might be zero.
22212*/
22213static int sqlite3StrAccumEnlarge(StrAccum *p, int N){
22214  char *zNew;
22215  assert( p->nChar+(i64)N >= p->nAlloc ); /* Only called if really needed */
22216  if( p->accError ){
22217    testcase(p->accError==STRACCUM_TOOBIG);
22218    testcase(p->accError==STRACCUM_NOMEM);
22219    return 0;
22220  }
22221  if( p->mxAlloc==0 ){
22222    N = p->nAlloc - p->nChar - 1;
22223    setStrAccumError(p, STRACCUM_TOOBIG);
22224    return N;
22225  }else{
22226    char *zOld = (p->zText==p->zBase ? 0 : p->zText);
22227    i64 szNew = p->nChar;
22228    szNew += N + 1;
22229    if( szNew+p->nChar<=p->mxAlloc ){
22230      /* Force exponential buffer size growth as long as it does not overflow,
22231      ** to avoid having to call this routine too often */
22232      szNew += p->nChar;
22233    }
22234    if( szNew > p->mxAlloc ){
22235      sqlite3StrAccumReset(p);
22236      setStrAccumError(p, STRACCUM_TOOBIG);
22237      return 0;
22238    }else{
22239      p->nAlloc = (int)szNew;
22240    }
22241    if( p->db ){
22242      zNew = sqlite3DbRealloc(p->db, zOld, p->nAlloc);
22243    }else{
22244      zNew = sqlite3_realloc64(zOld, p->nAlloc);
22245    }
22246    if( zNew ){
22247      assert( p->zText!=0 || p->nChar==0 );
22248      if( zOld==0 && p->nChar>0 ) memcpy(zNew, p->zText, p->nChar);
22249      p->zText = zNew;
22250      p->nAlloc = sqlite3DbMallocSize(p->db, zNew);
22251    }else{
22252      sqlite3StrAccumReset(p);
22253      setStrAccumError(p, STRACCUM_NOMEM);
22254      return 0;
22255    }
22256  }
22257  return N;
22258}
22259
22260/*
22261** Append N copies of character c to the given string buffer.
22262*/
22263SQLITE_PRIVATE void sqlite3AppendChar(StrAccum *p, int N, char c){
22264  testcase( p->nChar + (i64)N > 0x7fffffff );
22265  if( p->nChar+(i64)N >= p->nAlloc && (N = sqlite3StrAccumEnlarge(p, N))<=0 ){
22266    return;
22267  }
22268  while( (N--)>0 ) p->zText[p->nChar++] = c;
22269}
22270
22271/*
22272** The StrAccum "p" is not large enough to accept N new bytes of z[].
22273** So enlarge if first, then do the append.
22274**
22275** This is a helper routine to sqlite3StrAccumAppend() that does special-case
22276** work (enlarging the buffer) using tail recursion, so that the
22277** sqlite3StrAccumAppend() routine can use fast calling semantics.
22278*/
22279static void SQLITE_NOINLINE enlargeAndAppend(StrAccum *p, const char *z, int N){
22280  N = sqlite3StrAccumEnlarge(p, N);
22281  if( N>0 ){
22282    memcpy(&p->zText[p->nChar], z, N);
22283    p->nChar += N;
22284  }
22285}
22286
22287/*
22288** Append N bytes of text from z to the StrAccum object.  Increase the
22289** size of the memory allocation for StrAccum if necessary.
22290*/
22291SQLITE_PRIVATE void sqlite3StrAccumAppend(StrAccum *p, const char *z, int N){
22292  assert( z!=0 || N==0 );
22293  assert( p->zText!=0 || p->nChar==0 || p->accError );
22294  assert( N>=0 );
22295  assert( p->accError==0 || p->nAlloc==0 );
22296  if( p->nChar+N >= p->nAlloc ){
22297    enlargeAndAppend(p,z,N);
22298  }else{
22299    assert( p->zText );
22300    p->nChar += N;
22301    memcpy(&p->zText[p->nChar-N], z, N);
22302  }
22303}
22304
22305/*
22306** Append the complete text of zero-terminated string z[] to the p string.
22307*/
22308SQLITE_PRIVATE void sqlite3StrAccumAppendAll(StrAccum *p, const char *z){
22309  sqlite3StrAccumAppend(p, z, sqlite3Strlen30(z));
22310}
22311
22312
22313/*
22314** Finish off a string by making sure it is zero-terminated.
22315** Return a pointer to the resulting string.  Return a NULL
22316** pointer if any kind of error was encountered.
22317*/
22318SQLITE_PRIVATE char *sqlite3StrAccumFinish(StrAccum *p){
22319  if( p->zText ){
22320    p->zText[p->nChar] = 0;
22321    if( p->mxAlloc>0 && p->zText==p->zBase ){
22322      p->zText = sqlite3DbMallocRaw(p->db, p->nChar+1 );
22323      if( p->zText ){
22324        memcpy(p->zText, p->zBase, p->nChar+1);
22325      }else{
22326        setStrAccumError(p, STRACCUM_NOMEM);
22327      }
22328    }
22329  }
22330  return p->zText;
22331}
22332
22333/*
22334** Reset an StrAccum string.  Reclaim all malloced memory.
22335*/
22336SQLITE_PRIVATE void sqlite3StrAccumReset(StrAccum *p){
22337  if( p->zText!=p->zBase ){
22338    sqlite3DbFree(p->db, p->zText);
22339  }
22340  p->zText = 0;
22341}
22342
22343/*
22344** Initialize a string accumulator.
22345**
22346** p:     The accumulator to be initialized.
22347** db:    Pointer to a database connection.  May be NULL.  Lookaside
22348**        memory is used if not NULL. db->mallocFailed is set appropriately
22349**        when not NULL.
22350** zBase: An initial buffer.  May be NULL in which case the initial buffer
22351**        is malloced.
22352** n:     Size of zBase in bytes.  If total space requirements never exceed
22353**        n then no memory allocations ever occur.
22354** mx:    Maximum number of bytes to accumulate.  If mx==0 then no memory
22355**        allocations will ever occur.
22356*/
22357SQLITE_PRIVATE void sqlite3StrAccumInit(StrAccum *p, sqlite3 *db, char *zBase, int n, int mx){
22358  p->zText = p->zBase = zBase;
22359  p->db = db;
22360  p->nChar = 0;
22361  p->nAlloc = n;
22362  p->mxAlloc = mx;
22363  p->accError = 0;
22364}
22365
22366/*
22367** Print into memory obtained from sqliteMalloc().  Use the internal
22368** %-conversion extensions.
22369*/
22370SQLITE_PRIVATE char *sqlite3VMPrintf(sqlite3 *db, const char *zFormat, va_list ap){
22371  char *z;
22372  char zBase[SQLITE_PRINT_BUF_SIZE];
22373  StrAccum acc;
22374  assert( db!=0 );
22375  sqlite3StrAccumInit(&acc, db, zBase, sizeof(zBase),
22376                      db->aLimit[SQLITE_LIMIT_LENGTH]);
22377  sqlite3VXPrintf(&acc, SQLITE_PRINTF_INTERNAL, zFormat, ap);
22378  z = sqlite3StrAccumFinish(&acc);
22379  if( acc.accError==STRACCUM_NOMEM ){
22380    db->mallocFailed = 1;
22381  }
22382  return z;
22383}
22384
22385/*
22386** Print into memory obtained from sqliteMalloc().  Use the internal
22387** %-conversion extensions.
22388*/
22389SQLITE_PRIVATE char *sqlite3MPrintf(sqlite3 *db, const char *zFormat, ...){
22390  va_list ap;
22391  char *z;
22392  va_start(ap, zFormat);
22393  z = sqlite3VMPrintf(db, zFormat, ap);
22394  va_end(ap);
22395  return z;
22396}
22397
22398/*
22399** Print into memory obtained from sqlite3_malloc().  Omit the internal
22400** %-conversion extensions.
22401*/
22402SQLITE_API char *SQLITE_STDCALL sqlite3_vmprintf(const char *zFormat, va_list ap){
22403  char *z;
22404  char zBase[SQLITE_PRINT_BUF_SIZE];
22405  StrAccum acc;
22406
22407#ifdef SQLITE_ENABLE_API_ARMOR
22408  if( zFormat==0 ){
22409    (void)SQLITE_MISUSE_BKPT;
22410    return 0;
22411  }
22412#endif
22413#ifndef SQLITE_OMIT_AUTOINIT
22414  if( sqlite3_initialize() ) return 0;
22415#endif
22416  sqlite3StrAccumInit(&acc, 0, zBase, sizeof(zBase), SQLITE_MAX_LENGTH);
22417  sqlite3VXPrintf(&acc, 0, zFormat, ap);
22418  z = sqlite3StrAccumFinish(&acc);
22419  return z;
22420}
22421
22422/*
22423** Print into memory obtained from sqlite3_malloc()().  Omit the internal
22424** %-conversion extensions.
22425*/
22426SQLITE_API char *SQLITE_CDECL sqlite3_mprintf(const char *zFormat, ...){
22427  va_list ap;
22428  char *z;
22429#ifndef SQLITE_OMIT_AUTOINIT
22430  if( sqlite3_initialize() ) return 0;
22431#endif
22432  va_start(ap, zFormat);
22433  z = sqlite3_vmprintf(zFormat, ap);
22434  va_end(ap);
22435  return z;
22436}
22437
22438/*
22439** sqlite3_snprintf() works like snprintf() except that it ignores the
22440** current locale settings.  This is important for SQLite because we
22441** are not able to use a "," as the decimal point in place of "." as
22442** specified by some locales.
22443**
22444** Oops:  The first two arguments of sqlite3_snprintf() are backwards
22445** from the snprintf() standard.  Unfortunately, it is too late to change
22446** this without breaking compatibility, so we just have to live with the
22447** mistake.
22448**
22449** sqlite3_vsnprintf() is the varargs version.
22450*/
22451SQLITE_API char *SQLITE_STDCALL sqlite3_vsnprintf(int n, char *zBuf, const char *zFormat, va_list ap){
22452  StrAccum acc;
22453  if( n<=0 ) return zBuf;
22454#ifdef SQLITE_ENABLE_API_ARMOR
22455  if( zBuf==0 || zFormat==0 ) {
22456    (void)SQLITE_MISUSE_BKPT;
22457    if( zBuf ) zBuf[0] = 0;
22458    return zBuf;
22459  }
22460#endif
22461  sqlite3StrAccumInit(&acc, 0, zBuf, n, 0);
22462  sqlite3VXPrintf(&acc, 0, zFormat, ap);
22463  return sqlite3StrAccumFinish(&acc);
22464}
22465SQLITE_API char *SQLITE_CDECL sqlite3_snprintf(int n, char *zBuf, const char *zFormat, ...){
22466  char *z;
22467  va_list ap;
22468  va_start(ap,zFormat);
22469  z = sqlite3_vsnprintf(n, zBuf, zFormat, ap);
22470  va_end(ap);
22471  return z;
22472}
22473
22474/*
22475** This is the routine that actually formats the sqlite3_log() message.
22476** We house it in a separate routine from sqlite3_log() to avoid using
22477** stack space on small-stack systems when logging is disabled.
22478**
22479** sqlite3_log() must render into a static buffer.  It cannot dynamically
22480** allocate memory because it might be called while the memory allocator
22481** mutex is held.
22482**
22483** sqlite3VXPrintf() might ask for *temporary* memory allocations for
22484** certain format characters (%q) or for very large precisions or widths.
22485** Care must be taken that any sqlite3_log() calls that occur while the
22486** memory mutex is held do not use these mechanisms.
22487*/
22488static void renderLogMsg(int iErrCode, const char *zFormat, va_list ap){
22489  StrAccum acc;                          /* String accumulator */
22490  char zMsg[SQLITE_PRINT_BUF_SIZE*3];    /* Complete log message */
22491
22492  sqlite3StrAccumInit(&acc, 0, zMsg, sizeof(zMsg), 0);
22493  sqlite3VXPrintf(&acc, 0, zFormat, ap);
22494  sqlite3GlobalConfig.xLog(sqlite3GlobalConfig.pLogArg, iErrCode,
22495                           sqlite3StrAccumFinish(&acc));
22496}
22497
22498/*
22499** Format and write a message to the log if logging is enabled.
22500*/
22501SQLITE_API void SQLITE_CDECL sqlite3_log(int iErrCode, const char *zFormat, ...){
22502  va_list ap;                             /* Vararg list */
22503  if( sqlite3GlobalConfig.xLog ){
22504    va_start(ap, zFormat);
22505    renderLogMsg(iErrCode, zFormat, ap);
22506    va_end(ap);
22507  }
22508}
22509
22510#if defined(SQLITE_DEBUG) || defined(SQLITE_HAVE_OS_TRACE)
22511/*
22512** A version of printf() that understands %lld.  Used for debugging.
22513** The printf() built into some versions of windows does not understand %lld
22514** and segfaults if you give it a long long int.
22515*/
22516SQLITE_PRIVATE void sqlite3DebugPrintf(const char *zFormat, ...){
22517  va_list ap;
22518  StrAccum acc;
22519  char zBuf[500];
22520  sqlite3StrAccumInit(&acc, 0, zBuf, sizeof(zBuf), 0);
22521  va_start(ap,zFormat);
22522  sqlite3VXPrintf(&acc, 0, zFormat, ap);
22523  va_end(ap);
22524  sqlite3StrAccumFinish(&acc);
22525  fprintf(stdout,"%s", zBuf);
22526  fflush(stdout);
22527}
22528#endif
22529
22530
22531/*
22532** variable-argument wrapper around sqlite3VXPrintf().
22533*/
22534SQLITE_PRIVATE void sqlite3XPrintf(StrAccum *p, u32 bFlags, const char *zFormat, ...){
22535  va_list ap;
22536  va_start(ap,zFormat);
22537  sqlite3VXPrintf(p, bFlags, zFormat, ap);
22538  va_end(ap);
22539}
22540
22541/************** End of printf.c **********************************************/
22542/************** Begin file treeview.c ****************************************/
22543/*
22544** 2015-06-08
22545**
22546** The author disclaims copyright to this source code.  In place of
22547** a legal notice, here is a blessing:
22548**
22549**    May you do good and not evil.
22550**    May you find forgiveness for yourself and forgive others.
22551**    May you share freely, never taking more than you give.
22552**
22553*************************************************************************
22554**
22555** This file contains C code to implement the TreeView debugging routines.
22556** These routines print a parse tree to standard output for debugging and
22557** analysis.
22558**
22559** The interfaces in this file is only available when compiling
22560** with SQLITE_DEBUG.
22561*/
22562/* #include "sqliteInt.h" */
22563#ifdef SQLITE_DEBUG
22564
22565/*
22566** Add a new subitem to the tree.  The moreToFollow flag indicates that this
22567** is not the last item in the tree.
22568*/
22569static TreeView *sqlite3TreeViewPush(TreeView *p, u8 moreToFollow){
22570  if( p==0 ){
22571    p = sqlite3_malloc64( sizeof(*p) );
22572    if( p==0 ) return 0;
22573    memset(p, 0, sizeof(*p));
22574  }else{
22575    p->iLevel++;
22576  }
22577  assert( moreToFollow==0 || moreToFollow==1 );
22578  if( p->iLevel<sizeof(p->bLine) ) p->bLine[p->iLevel] = moreToFollow;
22579  return p;
22580}
22581
22582/*
22583** Finished with one layer of the tree
22584*/
22585static void sqlite3TreeViewPop(TreeView *p){
22586  if( p==0 ) return;
22587  p->iLevel--;
22588  if( p->iLevel<0 ) sqlite3_free(p);
22589}
22590
22591/*
22592** Generate a single line of output for the tree, with a prefix that contains
22593** all the appropriate tree lines
22594*/
22595static void sqlite3TreeViewLine(TreeView *p, const char *zFormat, ...){
22596  va_list ap;
22597  int i;
22598  StrAccum acc;
22599  char zBuf[500];
22600  sqlite3StrAccumInit(&acc, 0, zBuf, sizeof(zBuf), 0);
22601  if( p ){
22602    for(i=0; i<p->iLevel && i<sizeof(p->bLine)-1; i++){
22603      sqlite3StrAccumAppend(&acc, p->bLine[i] ? "|   " : "    ", 4);
22604    }
22605    sqlite3StrAccumAppend(&acc, p->bLine[i] ? "|-- " : "'-- ", 4);
22606  }
22607  va_start(ap, zFormat);
22608  sqlite3VXPrintf(&acc, 0, zFormat, ap);
22609  va_end(ap);
22610  if( zBuf[acc.nChar-1]!='\n' ) sqlite3StrAccumAppend(&acc, "\n", 1);
22611  sqlite3StrAccumFinish(&acc);
22612  fprintf(stdout,"%s", zBuf);
22613  fflush(stdout);
22614}
22615
22616/*
22617** Shorthand for starting a new tree item that consists of a single label
22618*/
22619static void sqlite3TreeViewItem(TreeView *p, const char *zLabel,u8 moreFollows){
22620  p = sqlite3TreeViewPush(p, moreFollows);
22621  sqlite3TreeViewLine(p, "%s", zLabel);
22622}
22623
22624
22625/*
22626** Generate a human-readable description of a the Select object.
22627*/
22628SQLITE_PRIVATE void sqlite3TreeViewSelect(TreeView *pView, const Select *p, u8 moreToFollow){
22629  int n = 0;
22630  pView = sqlite3TreeViewPush(pView, moreToFollow);
22631  sqlite3TreeViewLine(pView, "SELECT%s%s (0x%p) selFlags=0x%x",
22632    ((p->selFlags & SF_Distinct) ? " DISTINCT" : ""),
22633    ((p->selFlags & SF_Aggregate) ? " agg_flag" : ""), p, p->selFlags
22634  );
22635  if( p->pSrc && p->pSrc->nSrc ) n++;
22636  if( p->pWhere ) n++;
22637  if( p->pGroupBy ) n++;
22638  if( p->pHaving ) n++;
22639  if( p->pOrderBy ) n++;
22640  if( p->pLimit ) n++;
22641  if( p->pOffset ) n++;
22642  if( p->pPrior ) n++;
22643  sqlite3TreeViewExprList(pView, p->pEList, (n--)>0, "result-set");
22644  if( p->pSrc && p->pSrc->nSrc ){
22645    int i;
22646    pView = sqlite3TreeViewPush(pView, (n--)>0);
22647    sqlite3TreeViewLine(pView, "FROM");
22648    for(i=0; i<p->pSrc->nSrc; i++){
22649      struct SrcList_item *pItem = &p->pSrc->a[i];
22650      StrAccum x;
22651      char zLine[100];
22652      sqlite3StrAccumInit(&x, 0, zLine, sizeof(zLine), 0);
22653      sqlite3XPrintf(&x, 0, "{%d,*}", pItem->iCursor);
22654      if( pItem->zDatabase ){
22655        sqlite3XPrintf(&x, 0, " %s.%s", pItem->zDatabase, pItem->zName);
22656      }else if( pItem->zName ){
22657        sqlite3XPrintf(&x, 0, " %s", pItem->zName);
22658      }
22659      if( pItem->pTab ){
22660        sqlite3XPrintf(&x, 0, " tabname=%Q", pItem->pTab->zName);
22661      }
22662      if( pItem->zAlias ){
22663        sqlite3XPrintf(&x, 0, " (AS %s)", pItem->zAlias);
22664      }
22665      if( pItem->jointype & JT_LEFT ){
22666        sqlite3XPrintf(&x, 0, " LEFT-JOIN");
22667      }
22668      sqlite3StrAccumFinish(&x);
22669      sqlite3TreeViewItem(pView, zLine, i<p->pSrc->nSrc-1);
22670      if( pItem->pSelect ){
22671        sqlite3TreeViewSelect(pView, pItem->pSelect, 0);
22672      }
22673      sqlite3TreeViewPop(pView);
22674    }
22675    sqlite3TreeViewPop(pView);
22676  }
22677  if( p->pWhere ){
22678    sqlite3TreeViewItem(pView, "WHERE", (n--)>0);
22679    sqlite3TreeViewExpr(pView, p->pWhere, 0);
22680    sqlite3TreeViewPop(pView);
22681  }
22682  if( p->pGroupBy ){
22683    sqlite3TreeViewExprList(pView, p->pGroupBy, (n--)>0, "GROUPBY");
22684  }
22685  if( p->pHaving ){
22686    sqlite3TreeViewItem(pView, "HAVING", (n--)>0);
22687    sqlite3TreeViewExpr(pView, p->pHaving, 0);
22688    sqlite3TreeViewPop(pView);
22689  }
22690  if( p->pOrderBy ){
22691    sqlite3TreeViewExprList(pView, p->pOrderBy, (n--)>0, "ORDERBY");
22692  }
22693  if( p->pLimit ){
22694    sqlite3TreeViewItem(pView, "LIMIT", (n--)>0);
22695    sqlite3TreeViewExpr(pView, p->pLimit, 0);
22696    sqlite3TreeViewPop(pView);
22697  }
22698  if( p->pOffset ){
22699    sqlite3TreeViewItem(pView, "OFFSET", (n--)>0);
22700    sqlite3TreeViewExpr(pView, p->pOffset, 0);
22701    sqlite3TreeViewPop(pView);
22702  }
22703  if( p->pPrior ){
22704    const char *zOp = "UNION";
22705    switch( p->op ){
22706      case TK_ALL:         zOp = "UNION ALL";  break;
22707      case TK_INTERSECT:   zOp = "INTERSECT";  break;
22708      case TK_EXCEPT:      zOp = "EXCEPT";     break;
22709    }
22710    sqlite3TreeViewItem(pView, zOp, (n--)>0);
22711    sqlite3TreeViewSelect(pView, p->pPrior, 0);
22712    sqlite3TreeViewPop(pView);
22713  }
22714  sqlite3TreeViewPop(pView);
22715}
22716
22717/*
22718** Generate a human-readable explanation of an expression tree.
22719*/
22720SQLITE_PRIVATE void sqlite3TreeViewExpr(TreeView *pView, const Expr *pExpr, u8 moreToFollow){
22721  const char *zBinOp = 0;   /* Binary operator */
22722  const char *zUniOp = 0;   /* Unary operator */
22723  char zFlgs[30];
22724  pView = sqlite3TreeViewPush(pView, moreToFollow);
22725  if( pExpr==0 ){
22726    sqlite3TreeViewLine(pView, "nil");
22727    sqlite3TreeViewPop(pView);
22728    return;
22729  }
22730  if( pExpr->flags ){
22731    sqlite3_snprintf(sizeof(zFlgs),zFlgs,"  flags=0x%x",pExpr->flags);
22732  }else{
22733    zFlgs[0] = 0;
22734  }
22735  switch( pExpr->op ){
22736    case TK_AGG_COLUMN: {
22737      sqlite3TreeViewLine(pView, "AGG{%d:%d}%s",
22738            pExpr->iTable, pExpr->iColumn, zFlgs);
22739      break;
22740    }
22741    case TK_COLUMN: {
22742      if( pExpr->iTable<0 ){
22743        /* This only happens when coding check constraints */
22744        sqlite3TreeViewLine(pView, "COLUMN(%d)%s", pExpr->iColumn, zFlgs);
22745      }else{
22746        sqlite3TreeViewLine(pView, "{%d:%d}%s",
22747                             pExpr->iTable, pExpr->iColumn, zFlgs);
22748      }
22749      break;
22750    }
22751    case TK_INTEGER: {
22752      if( pExpr->flags & EP_IntValue ){
22753        sqlite3TreeViewLine(pView, "%d", pExpr->u.iValue);
22754      }else{
22755        sqlite3TreeViewLine(pView, "%s", pExpr->u.zToken);
22756      }
22757      break;
22758    }
22759#ifndef SQLITE_OMIT_FLOATING_POINT
22760    case TK_FLOAT: {
22761      sqlite3TreeViewLine(pView,"%s", pExpr->u.zToken);
22762      break;
22763    }
22764#endif
22765    case TK_STRING: {
22766      sqlite3TreeViewLine(pView,"%Q", pExpr->u.zToken);
22767      break;
22768    }
22769    case TK_NULL: {
22770      sqlite3TreeViewLine(pView,"NULL");
22771      break;
22772    }
22773#ifndef SQLITE_OMIT_BLOB_LITERAL
22774    case TK_BLOB: {
22775      sqlite3TreeViewLine(pView,"%s", pExpr->u.zToken);
22776      break;
22777    }
22778#endif
22779    case TK_VARIABLE: {
22780      sqlite3TreeViewLine(pView,"VARIABLE(%s,%d)",
22781                          pExpr->u.zToken, pExpr->iColumn);
22782      break;
22783    }
22784    case TK_REGISTER: {
22785      sqlite3TreeViewLine(pView,"REGISTER(%d)", pExpr->iTable);
22786      break;
22787    }
22788    case TK_AS: {
22789      sqlite3TreeViewLine(pView,"AS %Q", pExpr->u.zToken);
22790      sqlite3TreeViewExpr(pView, pExpr->pLeft, 0);
22791      break;
22792    }
22793    case TK_ID: {
22794      sqlite3TreeViewLine(pView,"ID \"%w\"", pExpr->u.zToken);
22795      break;
22796    }
22797#ifndef SQLITE_OMIT_CAST
22798    case TK_CAST: {
22799      /* Expressions of the form:   CAST(pLeft AS token) */
22800      sqlite3TreeViewLine(pView,"CAST %Q", pExpr->u.zToken);
22801      sqlite3TreeViewExpr(pView, pExpr->pLeft, 0);
22802      break;
22803    }
22804#endif /* SQLITE_OMIT_CAST */
22805    case TK_LT:      zBinOp = "LT";     break;
22806    case TK_LE:      zBinOp = "LE";     break;
22807    case TK_GT:      zBinOp = "GT";     break;
22808    case TK_GE:      zBinOp = "GE";     break;
22809    case TK_NE:      zBinOp = "NE";     break;
22810    case TK_EQ:      zBinOp = "EQ";     break;
22811    case TK_IS:      zBinOp = "IS";     break;
22812    case TK_ISNOT:   zBinOp = "ISNOT";  break;
22813    case TK_AND:     zBinOp = "AND";    break;
22814    case TK_OR:      zBinOp = "OR";     break;
22815    case TK_PLUS:    zBinOp = "ADD";    break;
22816    case TK_STAR:    zBinOp = "MUL";    break;
22817    case TK_MINUS:   zBinOp = "SUB";    break;
22818    case TK_REM:     zBinOp = "REM";    break;
22819    case TK_BITAND:  zBinOp = "BITAND"; break;
22820    case TK_BITOR:   zBinOp = "BITOR";  break;
22821    case TK_SLASH:   zBinOp = "DIV";    break;
22822    case TK_LSHIFT:  zBinOp = "LSHIFT"; break;
22823    case TK_RSHIFT:  zBinOp = "RSHIFT"; break;
22824    case TK_CONCAT:  zBinOp = "CONCAT"; break;
22825    case TK_DOT:     zBinOp = "DOT";    break;
22826
22827    case TK_UMINUS:  zUniOp = "UMINUS"; break;
22828    case TK_UPLUS:   zUniOp = "UPLUS";  break;
22829    case TK_BITNOT:  zUniOp = "BITNOT"; break;
22830    case TK_NOT:     zUniOp = "NOT";    break;
22831    case TK_ISNULL:  zUniOp = "ISNULL"; break;
22832    case TK_NOTNULL: zUniOp = "NOTNULL"; break;
22833
22834    case TK_COLLATE: {
22835      sqlite3TreeViewLine(pView, "COLLATE %Q", pExpr->u.zToken);
22836      sqlite3TreeViewExpr(pView, pExpr->pLeft, 0);
22837      break;
22838    }
22839
22840    case TK_AGG_FUNCTION:
22841    case TK_FUNCTION: {
22842      ExprList *pFarg;       /* List of function arguments */
22843      if( ExprHasProperty(pExpr, EP_TokenOnly) ){
22844        pFarg = 0;
22845      }else{
22846        pFarg = pExpr->x.pList;
22847      }
22848      if( pExpr->op==TK_AGG_FUNCTION ){
22849        sqlite3TreeViewLine(pView, "AGG_FUNCTION%d %Q",
22850                             pExpr->op2, pExpr->u.zToken);
22851      }else{
22852        sqlite3TreeViewLine(pView, "FUNCTION %Q", pExpr->u.zToken);
22853      }
22854      if( pFarg ){
22855        sqlite3TreeViewExprList(pView, pFarg, 0, 0);
22856      }
22857      break;
22858    }
22859#ifndef SQLITE_OMIT_SUBQUERY
22860    case TK_EXISTS: {
22861      sqlite3TreeViewLine(pView, "EXISTS-expr");
22862      sqlite3TreeViewSelect(pView, pExpr->x.pSelect, 0);
22863      break;
22864    }
22865    case TK_SELECT: {
22866      sqlite3TreeViewLine(pView, "SELECT-expr");
22867      sqlite3TreeViewSelect(pView, pExpr->x.pSelect, 0);
22868      break;
22869    }
22870    case TK_IN: {
22871      sqlite3TreeViewLine(pView, "IN");
22872      sqlite3TreeViewExpr(pView, pExpr->pLeft, 1);
22873      if( ExprHasProperty(pExpr, EP_xIsSelect) ){
22874        sqlite3TreeViewSelect(pView, pExpr->x.pSelect, 0);
22875      }else{
22876        sqlite3TreeViewExprList(pView, pExpr->x.pList, 0, 0);
22877      }
22878      break;
22879    }
22880#endif /* SQLITE_OMIT_SUBQUERY */
22881
22882    /*
22883    **    x BETWEEN y AND z
22884    **
22885    ** This is equivalent to
22886    **
22887    **    x>=y AND x<=z
22888    **
22889    ** X is stored in pExpr->pLeft.
22890    ** Y is stored in pExpr->pList->a[0].pExpr.
22891    ** Z is stored in pExpr->pList->a[1].pExpr.
22892    */
22893    case TK_BETWEEN: {
22894      Expr *pX = pExpr->pLeft;
22895      Expr *pY = pExpr->x.pList->a[0].pExpr;
22896      Expr *pZ = pExpr->x.pList->a[1].pExpr;
22897      sqlite3TreeViewLine(pView, "BETWEEN");
22898      sqlite3TreeViewExpr(pView, pX, 1);
22899      sqlite3TreeViewExpr(pView, pY, 1);
22900      sqlite3TreeViewExpr(pView, pZ, 0);
22901      break;
22902    }
22903    case TK_TRIGGER: {
22904      /* If the opcode is TK_TRIGGER, then the expression is a reference
22905      ** to a column in the new.* or old.* pseudo-tables available to
22906      ** trigger programs. In this case Expr.iTable is set to 1 for the
22907      ** new.* pseudo-table, or 0 for the old.* pseudo-table. Expr.iColumn
22908      ** is set to the column of the pseudo-table to read, or to -1 to
22909      ** read the rowid field.
22910      */
22911      sqlite3TreeViewLine(pView, "%s(%d)",
22912          pExpr->iTable ? "NEW" : "OLD", pExpr->iColumn);
22913      break;
22914    }
22915    case TK_CASE: {
22916      sqlite3TreeViewLine(pView, "CASE");
22917      sqlite3TreeViewExpr(pView, pExpr->pLeft, 1);
22918      sqlite3TreeViewExprList(pView, pExpr->x.pList, 0, 0);
22919      break;
22920    }
22921#ifndef SQLITE_OMIT_TRIGGER
22922    case TK_RAISE: {
22923      const char *zType = "unk";
22924      switch( pExpr->affinity ){
22925        case OE_Rollback:   zType = "rollback";  break;
22926        case OE_Abort:      zType = "abort";     break;
22927        case OE_Fail:       zType = "fail";      break;
22928        case OE_Ignore:     zType = "ignore";    break;
22929      }
22930      sqlite3TreeViewLine(pView, "RAISE %s(%Q)", zType, pExpr->u.zToken);
22931      break;
22932    }
22933#endif
22934    default: {
22935      sqlite3TreeViewLine(pView, "op=%d", pExpr->op);
22936      break;
22937    }
22938  }
22939  if( zBinOp ){
22940    sqlite3TreeViewLine(pView, "%s%s", zBinOp, zFlgs);
22941    sqlite3TreeViewExpr(pView, pExpr->pLeft, 1);
22942    sqlite3TreeViewExpr(pView, pExpr->pRight, 0);
22943  }else if( zUniOp ){
22944    sqlite3TreeViewLine(pView, "%s%s", zUniOp, zFlgs);
22945    sqlite3TreeViewExpr(pView, pExpr->pLeft, 0);
22946  }
22947  sqlite3TreeViewPop(pView);
22948}
22949
22950/*
22951** Generate a human-readable explanation of an expression list.
22952*/
22953SQLITE_PRIVATE void sqlite3TreeViewExprList(
22954  TreeView *pView,
22955  const ExprList *pList,
22956  u8 moreToFollow,
22957  const char *zLabel
22958){
22959  int i;
22960  pView = sqlite3TreeViewPush(pView, moreToFollow);
22961  if( zLabel==0 || zLabel[0]==0 ) zLabel = "LIST";
22962  if( pList==0 ){
22963    sqlite3TreeViewLine(pView, "%s (empty)", zLabel);
22964  }else{
22965    sqlite3TreeViewLine(pView, "%s", zLabel);
22966    for(i=0; i<pList->nExpr; i++){
22967      sqlite3TreeViewExpr(pView, pList->a[i].pExpr, i<pList->nExpr-1);
22968    }
22969  }
22970  sqlite3TreeViewPop(pView);
22971}
22972
22973#endif /* SQLITE_DEBUG */
22974
22975/************** End of treeview.c ********************************************/
22976/************** Begin file random.c ******************************************/
22977/*
22978** 2001 September 15
22979**
22980** The author disclaims copyright to this source code.  In place of
22981** a legal notice, here is a blessing:
22982**
22983**    May you do good and not evil.
22984**    May you find forgiveness for yourself and forgive others.
22985**    May you share freely, never taking more than you give.
22986**
22987*************************************************************************
22988** This file contains code to implement a pseudo-random number
22989** generator (PRNG) for SQLite.
22990**
22991** Random numbers are used by some of the database backends in order
22992** to generate random integer keys for tables or random filenames.
22993*/
22994/* #include "sqliteInt.h" */
22995
22996
22997/* All threads share a single random number generator.
22998** This structure is the current state of the generator.
22999*/
23000static SQLITE_WSD struct sqlite3PrngType {
23001  unsigned char isInit;          /* True if initialized */
23002  unsigned char i, j;            /* State variables */
23003  unsigned char s[256];          /* State variables */
23004} sqlite3Prng;
23005
23006/*
23007** Return N random bytes.
23008*/
23009SQLITE_API void SQLITE_STDCALL sqlite3_randomness(int N, void *pBuf){
23010  unsigned char t;
23011  unsigned char *zBuf = pBuf;
23012
23013  /* The "wsdPrng" macro will resolve to the pseudo-random number generator
23014  ** state vector.  If writable static data is unsupported on the target,
23015  ** we have to locate the state vector at run-time.  In the more common
23016  ** case where writable static data is supported, wsdPrng can refer directly
23017  ** to the "sqlite3Prng" state vector declared above.
23018  */
23019#ifdef SQLITE_OMIT_WSD
23020  struct sqlite3PrngType *p = &GLOBAL(struct sqlite3PrngType, sqlite3Prng);
23021# define wsdPrng p[0]
23022#else
23023# define wsdPrng sqlite3Prng
23024#endif
23025
23026#if SQLITE_THREADSAFE
23027  sqlite3_mutex *mutex;
23028#endif
23029
23030#ifndef SQLITE_OMIT_AUTOINIT
23031  if( sqlite3_initialize() ) return;
23032#endif
23033
23034#if SQLITE_THREADSAFE
23035  mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_PRNG);
23036#endif
23037
23038  sqlite3_mutex_enter(mutex);
23039  if( N<=0 || pBuf==0 ){
23040    wsdPrng.isInit = 0;
23041    sqlite3_mutex_leave(mutex);
23042    return;
23043  }
23044
23045  /* Initialize the state of the random number generator once,
23046  ** the first time this routine is called.  The seed value does
23047  ** not need to contain a lot of randomness since we are not
23048  ** trying to do secure encryption or anything like that...
23049  **
23050  ** Nothing in this file or anywhere else in SQLite does any kind of
23051  ** encryption.  The RC4 algorithm is being used as a PRNG (pseudo-random
23052  ** number generator) not as an encryption device.
23053  */
23054  if( !wsdPrng.isInit ){
23055    int i;
23056    char k[256];
23057    wsdPrng.j = 0;
23058    wsdPrng.i = 0;
23059    sqlite3OsRandomness(sqlite3_vfs_find(0), 256, k);
23060    for(i=0; i<256; i++){
23061      wsdPrng.s[i] = (u8)i;
23062    }
23063    for(i=0; i<256; i++){
23064      wsdPrng.j += wsdPrng.s[i] + k[i];
23065      t = wsdPrng.s[wsdPrng.j];
23066      wsdPrng.s[wsdPrng.j] = wsdPrng.s[i];
23067      wsdPrng.s[i] = t;
23068    }
23069    wsdPrng.isInit = 1;
23070  }
23071
23072  assert( N>0 );
23073  do{
23074    wsdPrng.i++;
23075    t = wsdPrng.s[wsdPrng.i];
23076    wsdPrng.j += t;
23077    wsdPrng.s[wsdPrng.i] = wsdPrng.s[wsdPrng.j];
23078    wsdPrng.s[wsdPrng.j] = t;
23079    t += wsdPrng.s[wsdPrng.i];
23080    *(zBuf++) = wsdPrng.s[t];
23081  }while( --N );
23082  sqlite3_mutex_leave(mutex);
23083}
23084
23085#ifndef SQLITE_OMIT_BUILTIN_TEST
23086/*
23087** For testing purposes, we sometimes want to preserve the state of
23088** PRNG and restore the PRNG to its saved state at a later time, or
23089** to reset the PRNG to its initial state.  These routines accomplish
23090** those tasks.
23091**
23092** The sqlite3_test_control() interface calls these routines to
23093** control the PRNG.
23094*/
23095static SQLITE_WSD struct sqlite3PrngType sqlite3SavedPrng;
23096SQLITE_PRIVATE void sqlite3PrngSaveState(void){
23097  memcpy(
23098    &GLOBAL(struct sqlite3PrngType, sqlite3SavedPrng),
23099    &GLOBAL(struct sqlite3PrngType, sqlite3Prng),
23100    sizeof(sqlite3Prng)
23101  );
23102}
23103SQLITE_PRIVATE void sqlite3PrngRestoreState(void){
23104  memcpy(
23105    &GLOBAL(struct sqlite3PrngType, sqlite3Prng),
23106    &GLOBAL(struct sqlite3PrngType, sqlite3SavedPrng),
23107    sizeof(sqlite3Prng)
23108  );
23109}
23110#endif /* SQLITE_OMIT_BUILTIN_TEST */
23111
23112/************** End of random.c **********************************************/
23113/************** Begin file threads.c *****************************************/
23114/*
23115** 2012 July 21
23116**
23117** The author disclaims copyright to this source code.  In place of
23118** a legal notice, here is a blessing:
23119**
23120**    May you do good and not evil.
23121**    May you find forgiveness for yourself and forgive others.
23122**    May you share freely, never taking more than you give.
23123**
23124******************************************************************************
23125**
23126** This file presents a simple cross-platform threading interface for
23127** use internally by SQLite.
23128**
23129** A "thread" can be created using sqlite3ThreadCreate().  This thread
23130** runs independently of its creator until it is joined using
23131** sqlite3ThreadJoin(), at which point it terminates.
23132**
23133** Threads do not have to be real.  It could be that the work of the
23134** "thread" is done by the main thread at either the sqlite3ThreadCreate()
23135** or sqlite3ThreadJoin() call.  This is, in fact, what happens in
23136** single threaded systems.  Nothing in SQLite requires multiple threads.
23137** This interface exists so that applications that want to take advantage
23138** of multiple cores can do so, while also allowing applications to stay
23139** single-threaded if desired.
23140*/
23141/* #include "sqliteInt.h" */
23142#if SQLITE_OS_WIN
23143/* #  include "os_win.h" */
23144#endif
23145
23146#if SQLITE_MAX_WORKER_THREADS>0
23147
23148/********************************* Unix Pthreads ****************************/
23149#if SQLITE_OS_UNIX && defined(SQLITE_MUTEX_PTHREADS) && SQLITE_THREADSAFE>0
23150
23151#define SQLITE_THREADS_IMPLEMENTED 1  /* Prevent the single-thread code below */
23152/* #include <pthread.h> */
23153
23154/* A running thread */
23155struct SQLiteThread {
23156  pthread_t tid;                 /* Thread ID */
23157  int done;                      /* Set to true when thread finishes */
23158  void *pOut;                    /* Result returned by the thread */
23159  void *(*xTask)(void*);         /* The thread routine */
23160  void *pIn;                     /* Argument to the thread */
23161};
23162
23163/* Create a new thread */
23164SQLITE_PRIVATE int sqlite3ThreadCreate(
23165  SQLiteThread **ppThread,  /* OUT: Write the thread object here */
23166  void *(*xTask)(void*),    /* Routine to run in a separate thread */
23167  void *pIn                 /* Argument passed into xTask() */
23168){
23169  SQLiteThread *p;
23170  int rc;
23171
23172  assert( ppThread!=0 );
23173  assert( xTask!=0 );
23174  /* This routine is never used in single-threaded mode */
23175  assert( sqlite3GlobalConfig.bCoreMutex!=0 );
23176
23177  *ppThread = 0;
23178  p = sqlite3Malloc(sizeof(*p));
23179  if( p==0 ) return SQLITE_NOMEM;
23180  memset(p, 0, sizeof(*p));
23181  p->xTask = xTask;
23182  p->pIn = pIn;
23183  if( sqlite3FaultSim(200) ){
23184    rc = 1;
23185  }else{
23186    rc = pthread_create(&p->tid, 0, xTask, pIn);
23187  }
23188  if( rc ){
23189    p->done = 1;
23190    p->pOut = xTask(pIn);
23191  }
23192  *ppThread = p;
23193  return SQLITE_OK;
23194}
23195
23196/* Get the results of the thread */
23197SQLITE_PRIVATE int sqlite3ThreadJoin(SQLiteThread *p, void **ppOut){
23198  int rc;
23199
23200  assert( ppOut!=0 );
23201  if( NEVER(p==0) ) return SQLITE_NOMEM;
23202  if( p->done ){
23203    *ppOut = p->pOut;
23204    rc = SQLITE_OK;
23205  }else{
23206    rc = pthread_join(p->tid, ppOut) ? SQLITE_ERROR : SQLITE_OK;
23207  }
23208  sqlite3_free(p);
23209  return rc;
23210}
23211
23212#endif /* SQLITE_OS_UNIX && defined(SQLITE_MUTEX_PTHREADS) */
23213/******************************** End Unix Pthreads *************************/
23214
23215
23216/********************************* Win32 Threads ****************************/
23217#if SQLITE_OS_WIN_THREADS
23218
23219#define SQLITE_THREADS_IMPLEMENTED 1  /* Prevent the single-thread code below */
23220#include <process.h>
23221
23222/* A running thread */
23223struct SQLiteThread {
23224  void *tid;               /* The thread handle */
23225  unsigned id;             /* The thread identifier */
23226  void *(*xTask)(void*);   /* The routine to run as a thread */
23227  void *pIn;               /* Argument to xTask */
23228  void *pResult;           /* Result of xTask */
23229};
23230
23231/* Thread procedure Win32 compatibility shim */
23232static unsigned __stdcall sqlite3ThreadProc(
23233  void *pArg  /* IN: Pointer to the SQLiteThread structure */
23234){
23235  SQLiteThread *p = (SQLiteThread *)pArg;
23236
23237  assert( p!=0 );
23238#if 0
23239  /*
23240  ** This assert appears to trigger spuriously on certain
23241  ** versions of Windows, possibly due to _beginthreadex()
23242  ** and/or CreateThread() not fully setting their thread
23243  ** ID parameter before starting the thread.
23244  */
23245  assert( p->id==GetCurrentThreadId() );
23246#endif
23247  assert( p->xTask!=0 );
23248  p->pResult = p->xTask(p->pIn);
23249
23250  _endthreadex(0);
23251  return 0; /* NOT REACHED */
23252}
23253
23254/* Create a new thread */
23255SQLITE_PRIVATE int sqlite3ThreadCreate(
23256  SQLiteThread **ppThread,  /* OUT: Write the thread object here */
23257  void *(*xTask)(void*),    /* Routine to run in a separate thread */
23258  void *pIn                 /* Argument passed into xTask() */
23259){
23260  SQLiteThread *p;
23261
23262  assert( ppThread!=0 );
23263  assert( xTask!=0 );
23264  *ppThread = 0;
23265  p = sqlite3Malloc(sizeof(*p));
23266  if( p==0 ) return SQLITE_NOMEM;
23267  if( sqlite3GlobalConfig.bCoreMutex==0 ){
23268    memset(p, 0, sizeof(*p));
23269  }else{
23270    p->xTask = xTask;
23271    p->pIn = pIn;
23272    p->tid = (void*)_beginthreadex(0, 0, sqlite3ThreadProc, p, 0, &p->id);
23273    if( p->tid==0 ){
23274      memset(p, 0, sizeof(*p));
23275    }
23276  }
23277  if( p->xTask==0 ){
23278    p->id = GetCurrentThreadId();
23279    p->pResult = xTask(pIn);
23280  }
23281  *ppThread = p;
23282  return SQLITE_OK;
23283}
23284
23285SQLITE_PRIVATE DWORD sqlite3Win32Wait(HANDLE hObject); /* os_win.c */
23286
23287/* Get the results of the thread */
23288SQLITE_PRIVATE int sqlite3ThreadJoin(SQLiteThread *p, void **ppOut){
23289  DWORD rc;
23290  BOOL bRc;
23291
23292  assert( ppOut!=0 );
23293  if( NEVER(p==0) ) return SQLITE_NOMEM;
23294  if( p->xTask==0 ){
23295    assert( p->id==GetCurrentThreadId() );
23296    rc = WAIT_OBJECT_0;
23297    assert( p->tid==0 );
23298  }else{
23299    assert( p->id!=0 && p->id!=GetCurrentThreadId() );
23300    rc = sqlite3Win32Wait((HANDLE)p->tid);
23301    assert( rc!=WAIT_IO_COMPLETION );
23302    bRc = CloseHandle((HANDLE)p->tid);
23303    assert( bRc );
23304  }
23305  if( rc==WAIT_OBJECT_0 ) *ppOut = p->pResult;
23306  sqlite3_free(p);
23307  return (rc==WAIT_OBJECT_0) ? SQLITE_OK : SQLITE_ERROR;
23308}
23309
23310#endif /* SQLITE_OS_WIN_THREADS */
23311/******************************** End Win32 Threads *************************/
23312
23313
23314/********************************* Single-Threaded **************************/
23315#ifndef SQLITE_THREADS_IMPLEMENTED
23316/*
23317** This implementation does not actually create a new thread.  It does the
23318** work of the thread in the main thread, when either the thread is created
23319** or when it is joined
23320*/
23321
23322/* A running thread */
23323struct SQLiteThread {
23324  void *(*xTask)(void*);   /* The routine to run as a thread */
23325  void *pIn;               /* Argument to xTask */
23326  void *pResult;           /* Result of xTask */
23327};
23328
23329/* Create a new thread */
23330SQLITE_PRIVATE int sqlite3ThreadCreate(
23331  SQLiteThread **ppThread,  /* OUT: Write the thread object here */
23332  void *(*xTask)(void*),    /* Routine to run in a separate thread */
23333  void *pIn                 /* Argument passed into xTask() */
23334){
23335  SQLiteThread *p;
23336
23337  assert( ppThread!=0 );
23338  assert( xTask!=0 );
23339  *ppThread = 0;
23340  p = sqlite3Malloc(sizeof(*p));
23341  if( p==0 ) return SQLITE_NOMEM;
23342  if( (SQLITE_PTR_TO_INT(p)/17)&1 ){
23343    p->xTask = xTask;
23344    p->pIn = pIn;
23345  }else{
23346    p->xTask = 0;
23347    p->pResult = xTask(pIn);
23348  }
23349  *ppThread = p;
23350  return SQLITE_OK;
23351}
23352
23353/* Get the results of the thread */
23354SQLITE_PRIVATE int sqlite3ThreadJoin(SQLiteThread *p, void **ppOut){
23355
23356  assert( ppOut!=0 );
23357  if( NEVER(p==0) ) return SQLITE_NOMEM;
23358  if( p->xTask ){
23359    *ppOut = p->xTask(p->pIn);
23360  }else{
23361    *ppOut = p->pResult;
23362  }
23363  sqlite3_free(p);
23364
23365#if defined(SQLITE_TEST)
23366  {
23367    void *pTstAlloc = sqlite3Malloc(10);
23368    if (!pTstAlloc) return SQLITE_NOMEM;
23369    sqlite3_free(pTstAlloc);
23370  }
23371#endif
23372
23373  return SQLITE_OK;
23374}
23375
23376#endif /* !defined(SQLITE_THREADS_IMPLEMENTED) */
23377/****************************** End Single-Threaded *************************/
23378#endif /* SQLITE_MAX_WORKER_THREADS>0 */
23379
23380/************** End of threads.c *********************************************/
23381/************** Begin file utf.c *********************************************/
23382/*
23383** 2004 April 13
23384**
23385** The author disclaims copyright to this source code.  In place of
23386** a legal notice, here is a blessing:
23387**
23388**    May you do good and not evil.
23389**    May you find forgiveness for yourself and forgive others.
23390**    May you share freely, never taking more than you give.
23391**
23392*************************************************************************
23393** This file contains routines used to translate between UTF-8,
23394** UTF-16, UTF-16BE, and UTF-16LE.
23395**
23396** Notes on UTF-8:
23397**
23398**   Byte-0    Byte-1    Byte-2    Byte-3    Value
23399**  0xxxxxxx                                 00000000 00000000 0xxxxxxx
23400**  110yyyyy  10xxxxxx                       00000000 00000yyy yyxxxxxx
23401**  1110zzzz  10yyyyyy  10xxxxxx             00000000 zzzzyyyy yyxxxxxx
23402**  11110uuu  10uuzzzz  10yyyyyy  10xxxxxx   000uuuuu zzzzyyyy yyxxxxxx
23403**
23404**
23405** Notes on UTF-16:  (with wwww+1==uuuuu)
23406**
23407**      Word-0               Word-1          Value
23408**  110110ww wwzzzzyy   110111yy yyxxxxxx    000uuuuu zzzzyyyy yyxxxxxx
23409**  zzzzyyyy yyxxxxxx                        00000000 zzzzyyyy yyxxxxxx
23410**
23411**
23412** BOM or Byte Order Mark:
23413**     0xff 0xfe   little-endian utf-16 follows
23414**     0xfe 0xff   big-endian utf-16 follows
23415**
23416*/
23417/* #include "sqliteInt.h" */
23418/* #include <assert.h> */
23419/* #include "vdbeInt.h" */
23420
23421#ifndef SQLITE_AMALGAMATION
23422/*
23423** The following constant value is used by the SQLITE_BIGENDIAN and
23424** SQLITE_LITTLEENDIAN macros.
23425*/
23426SQLITE_PRIVATE const int sqlite3one = 1;
23427#endif /* SQLITE_AMALGAMATION */
23428
23429/*
23430** This lookup table is used to help decode the first byte of
23431** a multi-byte UTF8 character.
23432*/
23433static const unsigned char sqlite3Utf8Trans1[] = {
23434  0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
23435  0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
23436  0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
23437  0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
23438  0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
23439  0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
23440  0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
23441  0x00, 0x01, 0x02, 0x03, 0x00, 0x01, 0x00, 0x00,
23442};
23443
23444
23445#define WRITE_UTF8(zOut, c) {                          \
23446  if( c<0x00080 ){                                     \
23447    *zOut++ = (u8)(c&0xFF);                            \
23448  }                                                    \
23449  else if( c<0x00800 ){                                \
23450    *zOut++ = 0xC0 + (u8)((c>>6)&0x1F);                \
23451    *zOut++ = 0x80 + (u8)(c & 0x3F);                   \
23452  }                                                    \
23453  else if( c<0x10000 ){                                \
23454    *zOut++ = 0xE0 + (u8)((c>>12)&0x0F);               \
23455    *zOut++ = 0x80 + (u8)((c>>6) & 0x3F);              \
23456    *zOut++ = 0x80 + (u8)(c & 0x3F);                   \
23457  }else{                                               \
23458    *zOut++ = 0xF0 + (u8)((c>>18) & 0x07);             \
23459    *zOut++ = 0x80 + (u8)((c>>12) & 0x3F);             \
23460    *zOut++ = 0x80 + (u8)((c>>6) & 0x3F);              \
23461    *zOut++ = 0x80 + (u8)(c & 0x3F);                   \
23462  }                                                    \
23463}
23464
23465#define WRITE_UTF16LE(zOut, c) {                                    \
23466  if( c<=0xFFFF ){                                                  \
23467    *zOut++ = (u8)(c&0x00FF);                                       \
23468    *zOut++ = (u8)((c>>8)&0x00FF);                                  \
23469  }else{                                                            \
23470    *zOut++ = (u8)(((c>>10)&0x003F) + (((c-0x10000)>>10)&0x00C0));  \
23471    *zOut++ = (u8)(0x00D8 + (((c-0x10000)>>18)&0x03));              \
23472    *zOut++ = (u8)(c&0x00FF);                                       \
23473    *zOut++ = (u8)(0x00DC + ((c>>8)&0x03));                         \
23474  }                                                                 \
23475}
23476
23477#define WRITE_UTF16BE(zOut, c) {                                    \
23478  if( c<=0xFFFF ){                                                  \
23479    *zOut++ = (u8)((c>>8)&0x00FF);                                  \
23480    *zOut++ = (u8)(c&0x00FF);                                       \
23481  }else{                                                            \
23482    *zOut++ = (u8)(0x00D8 + (((c-0x10000)>>18)&0x03));              \
23483    *zOut++ = (u8)(((c>>10)&0x003F) + (((c-0x10000)>>10)&0x00C0));  \
23484    *zOut++ = (u8)(0x00DC + ((c>>8)&0x03));                         \
23485    *zOut++ = (u8)(c&0x00FF);                                       \
23486  }                                                                 \
23487}
23488
23489#define READ_UTF16LE(zIn, TERM, c){                                   \
23490  c = (*zIn++);                                                       \
23491  c += ((*zIn++)<<8);                                                 \
23492  if( c>=0xD800 && c<0xE000 && TERM ){                                \
23493    int c2 = (*zIn++);                                                \
23494    c2 += ((*zIn++)<<8);                                              \
23495    c = (c2&0x03FF) + ((c&0x003F)<<10) + (((c&0x03C0)+0x0040)<<10);   \
23496  }                                                                   \
23497}
23498
23499#define READ_UTF16BE(zIn, TERM, c){                                   \
23500  c = ((*zIn++)<<8);                                                  \
23501  c += (*zIn++);                                                      \
23502  if( c>=0xD800 && c<0xE000 && TERM ){                                \
23503    int c2 = ((*zIn++)<<8);                                           \
23504    c2 += (*zIn++);                                                   \
23505    c = (c2&0x03FF) + ((c&0x003F)<<10) + (((c&0x03C0)+0x0040)<<10);   \
23506  }                                                                   \
23507}
23508
23509/*
23510** Translate a single UTF-8 character.  Return the unicode value.
23511**
23512** During translation, assume that the byte that zTerm points
23513** is a 0x00.
23514**
23515** Write a pointer to the next unread byte back into *pzNext.
23516**
23517** Notes On Invalid UTF-8:
23518**
23519**  *  This routine never allows a 7-bit character (0x00 through 0x7f) to
23520**     be encoded as a multi-byte character.  Any multi-byte character that
23521**     attempts to encode a value between 0x00 and 0x7f is rendered as 0xfffd.
23522**
23523**  *  This routine never allows a UTF16 surrogate value to be encoded.
23524**     If a multi-byte character attempts to encode a value between
23525**     0xd800 and 0xe000 then it is rendered as 0xfffd.
23526**
23527**  *  Bytes in the range of 0x80 through 0xbf which occur as the first
23528**     byte of a character are interpreted as single-byte characters
23529**     and rendered as themselves even though they are technically
23530**     invalid characters.
23531**
23532**  *  This routine accepts over-length UTF8 encodings
23533**     for unicode values 0x80 and greater.  It does not change over-length
23534**     encodings to 0xfffd as some systems recommend.
23535*/
23536#define READ_UTF8(zIn, zTerm, c)                           \
23537  c = *(zIn++);                                            \
23538  if( c>=0xc0 ){                                           \
23539    c = sqlite3Utf8Trans1[c-0xc0];                         \
23540    while( zIn!=zTerm && (*zIn & 0xc0)==0x80 ){            \
23541      c = (c<<6) + (0x3f & *(zIn++));                      \
23542    }                                                      \
23543    if( c<0x80                                             \
23544        || (c&0xFFFFF800)==0xD800                          \
23545        || (c&0xFFFFFFFE)==0xFFFE ){  c = 0xFFFD; }        \
23546  }
23547SQLITE_PRIVATE u32 sqlite3Utf8Read(
23548  const unsigned char **pz    /* Pointer to string from which to read char */
23549){
23550  unsigned int c;
23551
23552  /* Same as READ_UTF8() above but without the zTerm parameter.
23553  ** For this routine, we assume the UTF8 string is always zero-terminated.
23554  */
23555  c = *((*pz)++);
23556  if( c>=0xc0 ){
23557    c = sqlite3Utf8Trans1[c-0xc0];
23558    while( (*(*pz) & 0xc0)==0x80 ){
23559      c = (c<<6) + (0x3f & *((*pz)++));
23560    }
23561    if( c<0x80
23562        || (c&0xFFFFF800)==0xD800
23563        || (c&0xFFFFFFFE)==0xFFFE ){  c = 0xFFFD; }
23564  }
23565  return c;
23566}
23567
23568
23569
23570
23571/*
23572** If the TRANSLATE_TRACE macro is defined, the value of each Mem is
23573** printed on stderr on the way into and out of sqlite3VdbeMemTranslate().
23574*/
23575/* #define TRANSLATE_TRACE 1 */
23576
23577#ifndef SQLITE_OMIT_UTF16
23578/*
23579** This routine transforms the internal text encoding used by pMem to
23580** desiredEnc. It is an error if the string is already of the desired
23581** encoding, or if *pMem does not contain a string value.
23582*/
23583SQLITE_PRIVATE SQLITE_NOINLINE int sqlite3VdbeMemTranslate(Mem *pMem, u8 desiredEnc){
23584  int len;                    /* Maximum length of output string in bytes */
23585  unsigned char *zOut;                  /* Output buffer */
23586  unsigned char *zIn;                   /* Input iterator */
23587  unsigned char *zTerm;                 /* End of input */
23588  unsigned char *z;                     /* Output iterator */
23589  unsigned int c;
23590
23591  assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
23592  assert( pMem->flags&MEM_Str );
23593  assert( pMem->enc!=desiredEnc );
23594  assert( pMem->enc!=0 );
23595  assert( pMem->n>=0 );
23596
23597#if defined(TRANSLATE_TRACE) && defined(SQLITE_DEBUG)
23598  {
23599    char zBuf[100];
23600    sqlite3VdbeMemPrettyPrint(pMem, zBuf);
23601    fprintf(stderr, "INPUT:  %s\n", zBuf);
23602  }
23603#endif
23604
23605  /* If the translation is between UTF-16 little and big endian, then
23606  ** all that is required is to swap the byte order. This case is handled
23607  ** differently from the others.
23608  */
23609  if( pMem->enc!=SQLITE_UTF8 && desiredEnc!=SQLITE_UTF8 ){
23610    u8 temp;
23611    int rc;
23612    rc = sqlite3VdbeMemMakeWriteable(pMem);
23613    if( rc!=SQLITE_OK ){
23614      assert( rc==SQLITE_NOMEM );
23615      return SQLITE_NOMEM;
23616    }
23617    zIn = (u8*)pMem->z;
23618    zTerm = &zIn[pMem->n&~1];
23619    while( zIn<zTerm ){
23620      temp = *zIn;
23621      *zIn = *(zIn+1);
23622      zIn++;
23623      *zIn++ = temp;
23624    }
23625    pMem->enc = desiredEnc;
23626    goto translate_out;
23627  }
23628
23629  /* Set len to the maximum number of bytes required in the output buffer. */
23630  if( desiredEnc==SQLITE_UTF8 ){
23631    /* When converting from UTF-16, the maximum growth results from
23632    ** translating a 2-byte character to a 4-byte UTF-8 character.
23633    ** A single byte is required for the output string
23634    ** nul-terminator.
23635    */
23636    pMem->n &= ~1;
23637    len = pMem->n * 2 + 1;
23638  }else{
23639    /* When converting from UTF-8 to UTF-16 the maximum growth is caused
23640    ** when a 1-byte UTF-8 character is translated into a 2-byte UTF-16
23641    ** character. Two bytes are required in the output buffer for the
23642    ** nul-terminator.
23643    */
23644    len = pMem->n * 2 + 2;
23645  }
23646
23647  /* Set zIn to point at the start of the input buffer and zTerm to point 1
23648  ** byte past the end.
23649  **
23650  ** Variable zOut is set to point at the output buffer, space obtained
23651  ** from sqlite3_malloc().
23652  */
23653  zIn = (u8*)pMem->z;
23654  zTerm = &zIn[pMem->n];
23655  zOut = sqlite3DbMallocRaw(pMem->db, len);
23656  if( !zOut ){
23657    return SQLITE_NOMEM;
23658  }
23659  z = zOut;
23660
23661  if( pMem->enc==SQLITE_UTF8 ){
23662    if( desiredEnc==SQLITE_UTF16LE ){
23663      /* UTF-8 -> UTF-16 Little-endian */
23664      while( zIn<zTerm ){
23665        READ_UTF8(zIn, zTerm, c);
23666        WRITE_UTF16LE(z, c);
23667      }
23668    }else{
23669      assert( desiredEnc==SQLITE_UTF16BE );
23670      /* UTF-8 -> UTF-16 Big-endian */
23671      while( zIn<zTerm ){
23672        READ_UTF8(zIn, zTerm, c);
23673        WRITE_UTF16BE(z, c);
23674      }
23675    }
23676    pMem->n = (int)(z - zOut);
23677    *z++ = 0;
23678  }else{
23679    assert( desiredEnc==SQLITE_UTF8 );
23680    if( pMem->enc==SQLITE_UTF16LE ){
23681      /* UTF-16 Little-endian -> UTF-8 */
23682      while( zIn<zTerm ){
23683        READ_UTF16LE(zIn, zIn<zTerm, c);
23684        WRITE_UTF8(z, c);
23685      }
23686    }else{
23687      /* UTF-16 Big-endian -> UTF-8 */
23688      while( zIn<zTerm ){
23689        READ_UTF16BE(zIn, zIn<zTerm, c);
23690        WRITE_UTF8(z, c);
23691      }
23692    }
23693    pMem->n = (int)(z - zOut);
23694  }
23695  *z = 0;
23696  assert( (pMem->n+(desiredEnc==SQLITE_UTF8?1:2))<=len );
23697
23698  c = pMem->flags;
23699  sqlite3VdbeMemRelease(pMem);
23700  pMem->flags = MEM_Str|MEM_Term|(c&MEM_AffMask);
23701  pMem->enc = desiredEnc;
23702  pMem->z = (char*)zOut;
23703  pMem->zMalloc = pMem->z;
23704  pMem->szMalloc = sqlite3DbMallocSize(pMem->db, pMem->z);
23705
23706translate_out:
23707#if defined(TRANSLATE_TRACE) && defined(SQLITE_DEBUG)
23708  {
23709    char zBuf[100];
23710    sqlite3VdbeMemPrettyPrint(pMem, zBuf);
23711    fprintf(stderr, "OUTPUT: %s\n", zBuf);
23712  }
23713#endif
23714  return SQLITE_OK;
23715}
23716
23717/*
23718** This routine checks for a byte-order mark at the beginning of the
23719** UTF-16 string stored in *pMem. If one is present, it is removed and
23720** the encoding of the Mem adjusted. This routine does not do any
23721** byte-swapping, it just sets Mem.enc appropriately.
23722**
23723** The allocation (static, dynamic etc.) and encoding of the Mem may be
23724** changed by this function.
23725*/
23726SQLITE_PRIVATE int sqlite3VdbeMemHandleBom(Mem *pMem){
23727  int rc = SQLITE_OK;
23728  u8 bom = 0;
23729
23730  assert( pMem->n>=0 );
23731  if( pMem->n>1 ){
23732    u8 b1 = *(u8 *)pMem->z;
23733    u8 b2 = *(((u8 *)pMem->z) + 1);
23734    if( b1==0xFE && b2==0xFF ){
23735      bom = SQLITE_UTF16BE;
23736    }
23737    if( b1==0xFF && b2==0xFE ){
23738      bom = SQLITE_UTF16LE;
23739    }
23740  }
23741
23742  if( bom ){
23743    rc = sqlite3VdbeMemMakeWriteable(pMem);
23744    if( rc==SQLITE_OK ){
23745      pMem->n -= 2;
23746      memmove(pMem->z, &pMem->z[2], pMem->n);
23747      pMem->z[pMem->n] = '\0';
23748      pMem->z[pMem->n+1] = '\0';
23749      pMem->flags |= MEM_Term;
23750      pMem->enc = bom;
23751    }
23752  }
23753  return rc;
23754}
23755#endif /* SQLITE_OMIT_UTF16 */
23756
23757/*
23758** pZ is a UTF-8 encoded unicode string. If nByte is less than zero,
23759** return the number of unicode characters in pZ up to (but not including)
23760** the first 0x00 byte. If nByte is not less than zero, return the
23761** number of unicode characters in the first nByte of pZ (or up to
23762** the first 0x00, whichever comes first).
23763*/
23764SQLITE_PRIVATE int sqlite3Utf8CharLen(const char *zIn, int nByte){
23765  int r = 0;
23766  const u8 *z = (const u8*)zIn;
23767  const u8 *zTerm;
23768  if( nByte>=0 ){
23769    zTerm = &z[nByte];
23770  }else{
23771    zTerm = (const u8*)(-1);
23772  }
23773  assert( z<=zTerm );
23774  while( *z!=0 && z<zTerm ){
23775    SQLITE_SKIP_UTF8(z);
23776    r++;
23777  }
23778  return r;
23779}
23780
23781/* This test function is not currently used by the automated test-suite.
23782** Hence it is only available in debug builds.
23783*/
23784#if defined(SQLITE_TEST) && defined(SQLITE_DEBUG)
23785/*
23786** Translate UTF-8 to UTF-8.
23787**
23788** This has the effect of making sure that the string is well-formed
23789** UTF-8.  Miscoded characters are removed.
23790**
23791** The translation is done in-place and aborted if the output
23792** overruns the input.
23793*/
23794SQLITE_PRIVATE int sqlite3Utf8To8(unsigned char *zIn){
23795  unsigned char *zOut = zIn;
23796  unsigned char *zStart = zIn;
23797  u32 c;
23798
23799  while( zIn[0] && zOut<=zIn ){
23800    c = sqlite3Utf8Read((const u8**)&zIn);
23801    if( c!=0xfffd ){
23802      WRITE_UTF8(zOut, c);
23803    }
23804  }
23805  *zOut = 0;
23806  return (int)(zOut - zStart);
23807}
23808#endif
23809
23810#ifndef SQLITE_OMIT_UTF16
23811/*
23812** Convert a UTF-16 string in the native encoding into a UTF-8 string.
23813** Memory to hold the UTF-8 string is obtained from sqlite3_malloc and must
23814** be freed by the calling function.
23815**
23816** NULL is returned if there is an allocation error.
23817*/
23818SQLITE_PRIVATE char *sqlite3Utf16to8(sqlite3 *db, const void *z, int nByte, u8 enc){
23819  Mem m;
23820  memset(&m, 0, sizeof(m));
23821  m.db = db;
23822  sqlite3VdbeMemSetStr(&m, z, nByte, enc, SQLITE_STATIC);
23823  sqlite3VdbeChangeEncoding(&m, SQLITE_UTF8);
23824  if( db->mallocFailed ){
23825    sqlite3VdbeMemRelease(&m);
23826    m.z = 0;
23827  }
23828  assert( (m.flags & MEM_Term)!=0 || db->mallocFailed );
23829  assert( (m.flags & MEM_Str)!=0 || db->mallocFailed );
23830  assert( m.z || db->mallocFailed );
23831  return m.z;
23832}
23833
23834/*
23835** zIn is a UTF-16 encoded unicode string at least nChar characters long.
23836** Return the number of bytes in the first nChar unicode characters
23837** in pZ.  nChar must be non-negative.
23838*/
23839SQLITE_PRIVATE int sqlite3Utf16ByteLen(const void *zIn, int nChar){
23840  int c;
23841  unsigned char const *z = zIn;
23842  int n = 0;
23843
23844  if( SQLITE_UTF16NATIVE==SQLITE_UTF16BE ){
23845    while( n<nChar ){
23846      READ_UTF16BE(z, 1, c);
23847      n++;
23848    }
23849  }else{
23850    while( n<nChar ){
23851      READ_UTF16LE(z, 1, c);
23852      n++;
23853    }
23854  }
23855  return (int)(z-(unsigned char const *)zIn);
23856}
23857
23858#if defined(SQLITE_TEST)
23859/*
23860** This routine is called from the TCL test function "translate_selftest".
23861** It checks that the primitives for serializing and deserializing
23862** characters in each encoding are inverses of each other.
23863*/
23864SQLITE_PRIVATE void sqlite3UtfSelfTest(void){
23865  unsigned int i, t;
23866  unsigned char zBuf[20];
23867  unsigned char *z;
23868  int n;
23869  unsigned int c;
23870
23871  for(i=0; i<0x00110000; i++){
23872    z = zBuf;
23873    WRITE_UTF8(z, i);
23874    n = (int)(z-zBuf);
23875    assert( n>0 && n<=4 );
23876    z[0] = 0;
23877    z = zBuf;
23878    c = sqlite3Utf8Read((const u8**)&z);
23879    t = i;
23880    if( i>=0xD800 && i<=0xDFFF ) t = 0xFFFD;
23881    if( (i&0xFFFFFFFE)==0xFFFE ) t = 0xFFFD;
23882    assert( c==t );
23883    assert( (z-zBuf)==n );
23884  }
23885  for(i=0; i<0x00110000; i++){
23886    if( i>=0xD800 && i<0xE000 ) continue;
23887    z = zBuf;
23888    WRITE_UTF16LE(z, i);
23889    n = (int)(z-zBuf);
23890    assert( n>0 && n<=4 );
23891    z[0] = 0;
23892    z = zBuf;
23893    READ_UTF16LE(z, 1, c);
23894    assert( c==i );
23895    assert( (z-zBuf)==n );
23896  }
23897  for(i=0; i<0x00110000; i++){
23898    if( i>=0xD800 && i<0xE000 ) continue;
23899    z = zBuf;
23900    WRITE_UTF16BE(z, i);
23901    n = (int)(z-zBuf);
23902    assert( n>0 && n<=4 );
23903    z[0] = 0;
23904    z = zBuf;
23905    READ_UTF16BE(z, 1, c);
23906    assert( c==i );
23907    assert( (z-zBuf)==n );
23908  }
23909}
23910#endif /* SQLITE_TEST */
23911#endif /* SQLITE_OMIT_UTF16 */
23912
23913/************** End of utf.c *************************************************/
23914/************** Begin file util.c ********************************************/
23915/*
23916** 2001 September 15
23917**
23918** The author disclaims copyright to this source code.  In place of
23919** a legal notice, here is a blessing:
23920**
23921**    May you do good and not evil.
23922**    May you find forgiveness for yourself and forgive others.
23923**    May you share freely, never taking more than you give.
23924**
23925*************************************************************************
23926** Utility functions used throughout sqlite.
23927**
23928** This file contains functions for allocating memory, comparing
23929** strings, and stuff like that.
23930**
23931*/
23932/* #include "sqliteInt.h" */
23933/* #include <stdarg.h> */
23934#if HAVE_ISNAN || SQLITE_HAVE_ISNAN
23935# include <math.h>
23936#endif
23937
23938/*
23939** Routine needed to support the testcase() macro.
23940*/
23941#ifdef SQLITE_COVERAGE_TEST
23942SQLITE_PRIVATE void sqlite3Coverage(int x){
23943  static unsigned dummy = 0;
23944  dummy += (unsigned)x;
23945}
23946#endif
23947
23948/*
23949** Give a callback to the test harness that can be used to simulate faults
23950** in places where it is difficult or expensive to do so purely by means
23951** of inputs.
23952**
23953** The intent of the integer argument is to let the fault simulator know
23954** which of multiple sqlite3FaultSim() calls has been hit.
23955**
23956** Return whatever integer value the test callback returns, or return
23957** SQLITE_OK if no test callback is installed.
23958*/
23959#ifndef SQLITE_OMIT_BUILTIN_TEST
23960SQLITE_PRIVATE int sqlite3FaultSim(int iTest){
23961  int (*xCallback)(int) = sqlite3GlobalConfig.xTestCallback;
23962  return xCallback ? xCallback(iTest) : SQLITE_OK;
23963}
23964#endif
23965
23966#ifndef SQLITE_OMIT_FLOATING_POINT
23967/*
23968** Return true if the floating point value is Not a Number (NaN).
23969**
23970** Use the math library isnan() function if compiled with SQLITE_HAVE_ISNAN.
23971** Otherwise, we have our own implementation that works on most systems.
23972*/
23973SQLITE_PRIVATE int sqlite3IsNaN(double x){
23974  int rc;   /* The value return */
23975#if !SQLITE_HAVE_ISNAN && !HAVE_ISNAN
23976  /*
23977  ** Systems that support the isnan() library function should probably
23978  ** make use of it by compiling with -DSQLITE_HAVE_ISNAN.  But we have
23979  ** found that many systems do not have a working isnan() function so
23980  ** this implementation is provided as an alternative.
23981  **
23982  ** This NaN test sometimes fails if compiled on GCC with -ffast-math.
23983  ** On the other hand, the use of -ffast-math comes with the following
23984  ** warning:
23985  **
23986  **      This option [-ffast-math] should never be turned on by any
23987  **      -O option since it can result in incorrect output for programs
23988  **      which depend on an exact implementation of IEEE or ISO
23989  **      rules/specifications for math functions.
23990  **
23991  ** Under MSVC, this NaN test may fail if compiled with a floating-
23992  ** point precision mode other than /fp:precise.  From the MSDN
23993  ** documentation:
23994  **
23995  **      The compiler [with /fp:precise] will properly handle comparisons
23996  **      involving NaN. For example, x != x evaluates to true if x is NaN
23997  **      ...
23998  */
23999#ifdef __FAST_MATH__
24000# error SQLite will not work correctly with the -ffast-math option of GCC.
24001#endif
24002  volatile double y = x;
24003  volatile double z = y;
24004  rc = (y!=z);
24005#else  /* if HAVE_ISNAN */
24006  rc = isnan(x);
24007#endif /* HAVE_ISNAN */
24008  testcase( rc );
24009  return rc;
24010}
24011#endif /* SQLITE_OMIT_FLOATING_POINT */
24012
24013/*
24014** Compute a string length that is limited to what can be stored in
24015** lower 30 bits of a 32-bit signed integer.
24016**
24017** The value returned will never be negative.  Nor will it ever be greater
24018** than the actual length of the string.  For very long strings (greater
24019** than 1GiB) the value returned might be less than the true string length.
24020*/
24021SQLITE_PRIVATE int sqlite3Strlen30(const char *z){
24022  if( z==0 ) return 0;
24023  return 0x3fffffff & (int)strlen(z);
24024}
24025
24026/*
24027** Set the current error code to err_code and clear any prior error message.
24028*/
24029SQLITE_PRIVATE void sqlite3Error(sqlite3 *db, int err_code){
24030  assert( db!=0 );
24031  db->errCode = err_code;
24032  if( db->pErr ) sqlite3ValueSetNull(db->pErr);
24033}
24034
24035/*
24036** Set the most recent error code and error string for the sqlite
24037** handle "db". The error code is set to "err_code".
24038**
24039** If it is not NULL, string zFormat specifies the format of the
24040** error string in the style of the printf functions: The following
24041** format characters are allowed:
24042**
24043**      %s      Insert a string
24044**      %z      A string that should be freed after use
24045**      %d      Insert an integer
24046**      %T      Insert a token
24047**      %S      Insert the first element of a SrcList
24048**
24049** zFormat and any string tokens that follow it are assumed to be
24050** encoded in UTF-8.
24051**
24052** To clear the most recent error for sqlite handle "db", sqlite3Error
24053** should be called with err_code set to SQLITE_OK and zFormat set
24054** to NULL.
24055*/
24056SQLITE_PRIVATE void sqlite3ErrorWithMsg(sqlite3 *db, int err_code, const char *zFormat, ...){
24057  assert( db!=0 );
24058  db->errCode = err_code;
24059  if( zFormat==0 ){
24060    sqlite3Error(db, err_code);
24061  }else if( db->pErr || (db->pErr = sqlite3ValueNew(db))!=0 ){
24062    char *z;
24063    va_list ap;
24064    va_start(ap, zFormat);
24065    z = sqlite3VMPrintf(db, zFormat, ap);
24066    va_end(ap);
24067    sqlite3ValueSetStr(db->pErr, -1, z, SQLITE_UTF8, SQLITE_DYNAMIC);
24068  }
24069}
24070
24071/*
24072** Add an error message to pParse->zErrMsg and increment pParse->nErr.
24073** The following formatting characters are allowed:
24074**
24075**      %s      Insert a string
24076**      %z      A string that should be freed after use
24077**      %d      Insert an integer
24078**      %T      Insert a token
24079**      %S      Insert the first element of a SrcList
24080**
24081** This function should be used to report any error that occurs while
24082** compiling an SQL statement (i.e. within sqlite3_prepare()). The
24083** last thing the sqlite3_prepare() function does is copy the error
24084** stored by this function into the database handle using sqlite3Error().
24085** Functions sqlite3Error() or sqlite3ErrorWithMsg() should be used
24086** during statement execution (sqlite3_step() etc.).
24087*/
24088SQLITE_PRIVATE void sqlite3ErrorMsg(Parse *pParse, const char *zFormat, ...){
24089  char *zMsg;
24090  va_list ap;
24091  sqlite3 *db = pParse->db;
24092  va_start(ap, zFormat);
24093  zMsg = sqlite3VMPrintf(db, zFormat, ap);
24094  va_end(ap);
24095  if( db->suppressErr ){
24096    sqlite3DbFree(db, zMsg);
24097  }else{
24098    pParse->nErr++;
24099    sqlite3DbFree(db, pParse->zErrMsg);
24100    pParse->zErrMsg = zMsg;
24101    pParse->rc = SQLITE_ERROR;
24102  }
24103}
24104
24105/*
24106** Convert an SQL-style quoted string into a normal string by removing
24107** the quote characters.  The conversion is done in-place.  If the
24108** input does not begin with a quote character, then this routine
24109** is a no-op.
24110**
24111** The input string must be zero-terminated.  A new zero-terminator
24112** is added to the dequoted string.
24113**
24114** The return value is -1 if no dequoting occurs or the length of the
24115** dequoted string, exclusive of the zero terminator, if dequoting does
24116** occur.
24117**
24118** 2002-Feb-14: This routine is extended to remove MS-Access style
24119** brackets from around identifiers.  For example:  "[a-b-c]" becomes
24120** "a-b-c".
24121*/
24122SQLITE_PRIVATE int sqlite3Dequote(char *z){
24123  char quote;
24124  int i, j;
24125  if( z==0 ) return -1;
24126  quote = z[0];
24127  switch( quote ){
24128    case '\'':  break;
24129    case '"':   break;
24130    case '`':   break;                /* For MySQL compatibility */
24131    case '[':   quote = ']';  break;  /* For MS SqlServer compatibility */
24132    default:    return -1;
24133  }
24134  for(i=1, j=0;; i++){
24135    assert( z[i] );
24136    if( z[i]==quote ){
24137      if( z[i+1]==quote ){
24138        z[j++] = quote;
24139        i++;
24140      }else{
24141        break;
24142      }
24143    }else{
24144      z[j++] = z[i];
24145    }
24146  }
24147  z[j] = 0;
24148  return j;
24149}
24150
24151/* Convenient short-hand */
24152#define UpperToLower sqlite3UpperToLower
24153
24154/*
24155** Some systems have stricmp().  Others have strcasecmp().  Because
24156** there is no consistency, we will define our own.
24157**
24158** IMPLEMENTATION-OF: R-30243-02494 The sqlite3_stricmp() and
24159** sqlite3_strnicmp() APIs allow applications and extensions to compare
24160** the contents of two buffers containing UTF-8 strings in a
24161** case-independent fashion, using the same definition of "case
24162** independence" that SQLite uses internally when comparing identifiers.
24163*/
24164SQLITE_API int SQLITE_STDCALL sqlite3_stricmp(const char *zLeft, const char *zRight){
24165  register unsigned char *a, *b;
24166  if( zLeft==0 ){
24167    return zRight ? -1 : 0;
24168  }else if( zRight==0 ){
24169    return 1;
24170  }
24171  a = (unsigned char *)zLeft;
24172  b = (unsigned char *)zRight;
24173  while( *a!=0 && UpperToLower[*a]==UpperToLower[*b]){ a++; b++; }
24174  return UpperToLower[*a] - UpperToLower[*b];
24175}
24176SQLITE_API int SQLITE_STDCALL sqlite3_strnicmp(const char *zLeft, const char *zRight, int N){
24177  register unsigned char *a, *b;
24178  if( zLeft==0 ){
24179    return zRight ? -1 : 0;
24180  }else if( zRight==0 ){
24181    return 1;
24182  }
24183  a = (unsigned char *)zLeft;
24184  b = (unsigned char *)zRight;
24185  while( N-- > 0 && *a!=0 && UpperToLower[*a]==UpperToLower[*b]){ a++; b++; }
24186  return N<0 ? 0 : UpperToLower[*a] - UpperToLower[*b];
24187}
24188
24189/*
24190** The string z[] is an text representation of a real number.
24191** Convert this string to a double and write it into *pResult.
24192**
24193** The string z[] is length bytes in length (bytes, not characters) and
24194** uses the encoding enc.  The string is not necessarily zero-terminated.
24195**
24196** Return TRUE if the result is a valid real number (or integer) and FALSE
24197** if the string is empty or contains extraneous text.  Valid numbers
24198** are in one of these formats:
24199**
24200**    [+-]digits[E[+-]digits]
24201**    [+-]digits.[digits][E[+-]digits]
24202**    [+-].digits[E[+-]digits]
24203**
24204** Leading and trailing whitespace is ignored for the purpose of determining
24205** validity.
24206**
24207** If some prefix of the input string is a valid number, this routine
24208** returns FALSE but it still converts the prefix and writes the result
24209** into *pResult.
24210*/
24211SQLITE_PRIVATE int sqlite3AtoF(const char *z, double *pResult, int length, u8 enc){
24212#ifndef SQLITE_OMIT_FLOATING_POINT
24213  int incr;
24214  const char *zEnd = z + length;
24215  /* sign * significand * (10 ^ (esign * exponent)) */
24216  int sign = 1;    /* sign of significand */
24217  i64 s = 0;       /* significand */
24218  int d = 0;       /* adjust exponent for shifting decimal point */
24219  int esign = 1;   /* sign of exponent */
24220  int e = 0;       /* exponent */
24221  int eValid = 1;  /* True exponent is either not used or is well-formed */
24222  double result;
24223  int nDigits = 0;
24224  int nonNum = 0;
24225
24226  assert( enc==SQLITE_UTF8 || enc==SQLITE_UTF16LE || enc==SQLITE_UTF16BE );
24227  *pResult = 0.0;   /* Default return value, in case of an error */
24228
24229  if( enc==SQLITE_UTF8 ){
24230    incr = 1;
24231  }else{
24232    int i;
24233    incr = 2;
24234    assert( SQLITE_UTF16LE==2 && SQLITE_UTF16BE==3 );
24235    for(i=3-enc; i<length && z[i]==0; i+=2){}
24236    nonNum = i<length;
24237    zEnd = z+i+enc-3;
24238    z += (enc&1);
24239  }
24240
24241  /* skip leading spaces */
24242  while( z<zEnd && sqlite3Isspace(*z) ) z+=incr;
24243  if( z>=zEnd ) return 0;
24244
24245  /* get sign of significand */
24246  if( *z=='-' ){
24247    sign = -1;
24248    z+=incr;
24249  }else if( *z=='+' ){
24250    z+=incr;
24251  }
24252
24253  /* skip leading zeroes */
24254  while( z<zEnd && z[0]=='0' ) z+=incr, nDigits++;
24255
24256  /* copy max significant digits to significand */
24257  while( z<zEnd && sqlite3Isdigit(*z) && s<((LARGEST_INT64-9)/10) ){
24258    s = s*10 + (*z - '0');
24259    z+=incr, nDigits++;
24260  }
24261
24262  /* skip non-significant significand digits
24263  ** (increase exponent by d to shift decimal left) */
24264  while( z<zEnd && sqlite3Isdigit(*z) ) z+=incr, nDigits++, d++;
24265  if( z>=zEnd ) goto do_atof_calc;
24266
24267  /* if decimal point is present */
24268  if( *z=='.' ){
24269    z+=incr;
24270    /* copy digits from after decimal to significand
24271    ** (decrease exponent by d to shift decimal right) */
24272    while( z<zEnd && sqlite3Isdigit(*z) && s<((LARGEST_INT64-9)/10) ){
24273      s = s*10 + (*z - '0');
24274      z+=incr, nDigits++, d--;
24275    }
24276    /* skip non-significant digits */
24277    while( z<zEnd && sqlite3Isdigit(*z) ) z+=incr, nDigits++;
24278  }
24279  if( z>=zEnd ) goto do_atof_calc;
24280
24281  /* if exponent is present */
24282  if( *z=='e' || *z=='E' ){
24283    z+=incr;
24284    eValid = 0;
24285    if( z>=zEnd ) goto do_atof_calc;
24286    /* get sign of exponent */
24287    if( *z=='-' ){
24288      esign = -1;
24289      z+=incr;
24290    }else if( *z=='+' ){
24291      z+=incr;
24292    }
24293    /* copy digits to exponent */
24294    while( z<zEnd && sqlite3Isdigit(*z) ){
24295      e = e<10000 ? (e*10 + (*z - '0')) : 10000;
24296      z+=incr;
24297      eValid = 1;
24298    }
24299  }
24300
24301  /* skip trailing spaces */
24302  if( nDigits && eValid ){
24303    while( z<zEnd && sqlite3Isspace(*z) ) z+=incr;
24304  }
24305
24306do_atof_calc:
24307  /* adjust exponent by d, and update sign */
24308  e = (e*esign) + d;
24309  if( e<0 ) {
24310    esign = -1;
24311    e *= -1;
24312  } else {
24313    esign = 1;
24314  }
24315
24316  /* if 0 significand */
24317  if( !s ) {
24318    /* In the IEEE 754 standard, zero is signed.
24319    ** Add the sign if we've seen at least one digit */
24320    result = (sign<0 && nDigits) ? -(double)0 : (double)0;
24321  } else {
24322    /* attempt to reduce exponent */
24323    if( esign>0 ){
24324      while( s<(LARGEST_INT64/10) && e>0 ) e--,s*=10;
24325    }else{
24326      while( !(s%10) && e>0 ) e--,s/=10;
24327    }
24328
24329    /* adjust the sign of significand */
24330    s = sign<0 ? -s : s;
24331
24332    /* if exponent, scale significand as appropriate
24333    ** and store in result. */
24334    if( e ){
24335      LONGDOUBLE_TYPE scale = 1.0;
24336      /* attempt to handle extremely small/large numbers better */
24337      if( e>307 && e<342 ){
24338        while( e%308 ) { scale *= 1.0e+1; e -= 1; }
24339        if( esign<0 ){
24340          result = s / scale;
24341          result /= 1.0e+308;
24342        }else{
24343          result = s * scale;
24344          result *= 1.0e+308;
24345        }
24346      }else if( e>=342 ){
24347        if( esign<0 ){
24348          result = 0.0*s;
24349        }else{
24350          result = 1e308*1e308*s;  /* Infinity */
24351        }
24352      }else{
24353        /* 1.0e+22 is the largest power of 10 than can be
24354        ** represented exactly. */
24355        while( e%22 ) { scale *= 1.0e+1; e -= 1; }
24356        while( e>0 ) { scale *= 1.0e+22; e -= 22; }
24357        if( esign<0 ){
24358          result = s / scale;
24359        }else{
24360          result = s * scale;
24361        }
24362      }
24363    } else {
24364      result = (double)s;
24365    }
24366  }
24367
24368  /* store the result */
24369  *pResult = result;
24370
24371  /* return true if number and no extra non-whitespace chracters after */
24372  return z>=zEnd && nDigits>0 && eValid && nonNum==0;
24373#else
24374  return !sqlite3Atoi64(z, pResult, length, enc);
24375#endif /* SQLITE_OMIT_FLOATING_POINT */
24376}
24377
24378/*
24379** Compare the 19-character string zNum against the text representation
24380** value 2^63:  9223372036854775808.  Return negative, zero, or positive
24381** if zNum is less than, equal to, or greater than the string.
24382** Note that zNum must contain exactly 19 characters.
24383**
24384** Unlike memcmp() this routine is guaranteed to return the difference
24385** in the values of the last digit if the only difference is in the
24386** last digit.  So, for example,
24387**
24388**      compare2pow63("9223372036854775800", 1)
24389**
24390** will return -8.
24391*/
24392static int compare2pow63(const char *zNum, int incr){
24393  int c = 0;
24394  int i;
24395                    /* 012345678901234567 */
24396  const char *pow63 = "922337203685477580";
24397  for(i=0; c==0 && i<18; i++){
24398    c = (zNum[i*incr]-pow63[i])*10;
24399  }
24400  if( c==0 ){
24401    c = zNum[18*incr] - '8';
24402    testcase( c==(-1) );
24403    testcase( c==0 );
24404    testcase( c==(+1) );
24405  }
24406  return c;
24407}
24408
24409/*
24410** Convert zNum to a 64-bit signed integer.  zNum must be decimal. This
24411** routine does *not* accept hexadecimal notation.
24412**
24413** If the zNum value is representable as a 64-bit twos-complement
24414** integer, then write that value into *pNum and return 0.
24415**
24416** If zNum is exactly 9223372036854775808, return 2.  This special
24417** case is broken out because while 9223372036854775808 cannot be a
24418** signed 64-bit integer, its negative -9223372036854775808 can be.
24419**
24420** If zNum is too big for a 64-bit integer and is not
24421** 9223372036854775808  or if zNum contains any non-numeric text,
24422** then return 1.
24423**
24424** length is the number of bytes in the string (bytes, not characters).
24425** The string is not necessarily zero-terminated.  The encoding is
24426** given by enc.
24427*/
24428SQLITE_PRIVATE int sqlite3Atoi64(const char *zNum, i64 *pNum, int length, u8 enc){
24429  int incr;
24430  u64 u = 0;
24431  int neg = 0; /* assume positive */
24432  int i;
24433  int c = 0;
24434  int nonNum = 0;
24435  const char *zStart;
24436  const char *zEnd = zNum + length;
24437  assert( enc==SQLITE_UTF8 || enc==SQLITE_UTF16LE || enc==SQLITE_UTF16BE );
24438  if( enc==SQLITE_UTF8 ){
24439    incr = 1;
24440  }else{
24441    incr = 2;
24442    assert( SQLITE_UTF16LE==2 && SQLITE_UTF16BE==3 );
24443    for(i=3-enc; i<length && zNum[i]==0; i+=2){}
24444    nonNum = i<length;
24445    zEnd = zNum+i+enc-3;
24446    zNum += (enc&1);
24447  }
24448  while( zNum<zEnd && sqlite3Isspace(*zNum) ) zNum+=incr;
24449  if( zNum<zEnd ){
24450    if( *zNum=='-' ){
24451      neg = 1;
24452      zNum+=incr;
24453    }else if( *zNum=='+' ){
24454      zNum+=incr;
24455    }
24456  }
24457  zStart = zNum;
24458  while( zNum<zEnd && zNum[0]=='0' ){ zNum+=incr; } /* Skip leading zeros. */
24459  for(i=0; &zNum[i]<zEnd && (c=zNum[i])>='0' && c<='9'; i+=incr){
24460    u = u*10 + c - '0';
24461  }
24462  if( u>LARGEST_INT64 ){
24463    *pNum = neg ? SMALLEST_INT64 : LARGEST_INT64;
24464  }else if( neg ){
24465    *pNum = -(i64)u;
24466  }else{
24467    *pNum = (i64)u;
24468  }
24469  testcase( i==18 );
24470  testcase( i==19 );
24471  testcase( i==20 );
24472  if( (c!=0 && &zNum[i]<zEnd) || (i==0 && zStart==zNum) || i>19*incr || nonNum ){
24473    /* zNum is empty or contains non-numeric text or is longer
24474    ** than 19 digits (thus guaranteeing that it is too large) */
24475    return 1;
24476  }else if( i<19*incr ){
24477    /* Less than 19 digits, so we know that it fits in 64 bits */
24478    assert( u<=LARGEST_INT64 );
24479    return 0;
24480  }else{
24481    /* zNum is a 19-digit numbers.  Compare it against 9223372036854775808. */
24482    c = compare2pow63(zNum, incr);
24483    if( c<0 ){
24484      /* zNum is less than 9223372036854775808 so it fits */
24485      assert( u<=LARGEST_INT64 );
24486      return 0;
24487    }else if( c>0 ){
24488      /* zNum is greater than 9223372036854775808 so it overflows */
24489      return 1;
24490    }else{
24491      /* zNum is exactly 9223372036854775808.  Fits if negative.  The
24492      ** special case 2 overflow if positive */
24493      assert( u-1==LARGEST_INT64 );
24494      return neg ? 0 : 2;
24495    }
24496  }
24497}
24498
24499/*
24500** Transform a UTF-8 integer literal, in either decimal or hexadecimal,
24501** into a 64-bit signed integer.  This routine accepts hexadecimal literals,
24502** whereas sqlite3Atoi64() does not.
24503**
24504** Returns:
24505**
24506**     0    Successful transformation.  Fits in a 64-bit signed integer.
24507**     1    Integer too large for a 64-bit signed integer or is malformed
24508**     2    Special case of 9223372036854775808
24509*/
24510SQLITE_PRIVATE int sqlite3DecOrHexToI64(const char *z, i64 *pOut){
24511#ifndef SQLITE_OMIT_HEX_INTEGER
24512  if( z[0]=='0'
24513   && (z[1]=='x' || z[1]=='X')
24514   && sqlite3Isxdigit(z[2])
24515  ){
24516    u64 u = 0;
24517    int i, k;
24518    for(i=2; z[i]=='0'; i++){}
24519    for(k=i; sqlite3Isxdigit(z[k]); k++){
24520      u = u*16 + sqlite3HexToInt(z[k]);
24521    }
24522    memcpy(pOut, &u, 8);
24523    return (z[k]==0 && k-i<=16) ? 0 : 1;
24524  }else
24525#endif /* SQLITE_OMIT_HEX_INTEGER */
24526  {
24527    return sqlite3Atoi64(z, pOut, sqlite3Strlen30(z), SQLITE_UTF8);
24528  }
24529}
24530
24531/*
24532** If zNum represents an integer that will fit in 32-bits, then set
24533** *pValue to that integer and return true.  Otherwise return false.
24534**
24535** This routine accepts both decimal and hexadecimal notation for integers.
24536**
24537** Any non-numeric characters that following zNum are ignored.
24538** This is different from sqlite3Atoi64() which requires the
24539** input number to be zero-terminated.
24540*/
24541SQLITE_PRIVATE int sqlite3GetInt32(const char *zNum, int *pValue){
24542  sqlite_int64 v = 0;
24543  int i, c;
24544  int neg = 0;
24545  if( zNum[0]=='-' ){
24546    neg = 1;
24547    zNum++;
24548  }else if( zNum[0]=='+' ){
24549    zNum++;
24550  }
24551#ifndef SQLITE_OMIT_HEX_INTEGER
24552  else if( zNum[0]=='0'
24553        && (zNum[1]=='x' || zNum[1]=='X')
24554        && sqlite3Isxdigit(zNum[2])
24555  ){
24556    u32 u = 0;
24557    zNum += 2;
24558    while( zNum[0]=='0' ) zNum++;
24559    for(i=0; sqlite3Isxdigit(zNum[i]) && i<8; i++){
24560      u = u*16 + sqlite3HexToInt(zNum[i]);
24561    }
24562    if( (u&0x80000000)==0 && sqlite3Isxdigit(zNum[i])==0 ){
24563      memcpy(pValue, &u, 4);
24564      return 1;
24565    }else{
24566      return 0;
24567    }
24568  }
24569#endif
24570  while( zNum[0]=='0' ) zNum++;
24571  for(i=0; i<11 && (c = zNum[i] - '0')>=0 && c<=9; i++){
24572    v = v*10 + c;
24573  }
24574
24575  /* The longest decimal representation of a 32 bit integer is 10 digits:
24576  **
24577  **             1234567890
24578  **     2^31 -> 2147483648
24579  */
24580  testcase( i==10 );
24581  if( i>10 ){
24582    return 0;
24583  }
24584  testcase( v-neg==2147483647 );
24585  if( v-neg>2147483647 ){
24586    return 0;
24587  }
24588  if( neg ){
24589    v = -v;
24590  }
24591  *pValue = (int)v;
24592  return 1;
24593}
24594
24595/*
24596** Return a 32-bit integer value extracted from a string.  If the
24597** string is not an integer, just return 0.
24598*/
24599SQLITE_PRIVATE int sqlite3Atoi(const char *z){
24600  int x = 0;
24601  if( z ) sqlite3GetInt32(z, &x);
24602  return x;
24603}
24604
24605/*
24606** The variable-length integer encoding is as follows:
24607**
24608** KEY:
24609**         A = 0xxxxxxx    7 bits of data and one flag bit
24610**         B = 1xxxxxxx    7 bits of data and one flag bit
24611**         C = xxxxxxxx    8 bits of data
24612**
24613**  7 bits - A
24614** 14 bits - BA
24615** 21 bits - BBA
24616** 28 bits - BBBA
24617** 35 bits - BBBBA
24618** 42 bits - BBBBBA
24619** 49 bits - BBBBBBA
24620** 56 bits - BBBBBBBA
24621** 64 bits - BBBBBBBBC
24622*/
24623
24624/*
24625** Write a 64-bit variable-length integer to memory starting at p[0].
24626** The length of data write will be between 1 and 9 bytes.  The number
24627** of bytes written is returned.
24628**
24629** A variable-length integer consists of the lower 7 bits of each byte
24630** for all bytes that have the 8th bit set and one byte with the 8th
24631** bit clear.  Except, if we get to the 9th byte, it stores the full
24632** 8 bits and is the last byte.
24633*/
24634static int SQLITE_NOINLINE putVarint64(unsigned char *p, u64 v){
24635  int i, j, n;
24636  u8 buf[10];
24637  if( v & (((u64)0xff000000)<<32) ){
24638    p[8] = (u8)v;
24639    v >>= 8;
24640    for(i=7; i>=0; i--){
24641      p[i] = (u8)((v & 0x7f) | 0x80);
24642      v >>= 7;
24643    }
24644    return 9;
24645  }
24646  n = 0;
24647  do{
24648    buf[n++] = (u8)((v & 0x7f) | 0x80);
24649    v >>= 7;
24650  }while( v!=0 );
24651  buf[0] &= 0x7f;
24652  assert( n<=9 );
24653  for(i=0, j=n-1; j>=0; j--, i++){
24654    p[i] = buf[j];
24655  }
24656  return n;
24657}
24658SQLITE_PRIVATE int sqlite3PutVarint(unsigned char *p, u64 v){
24659  if( v<=0x7f ){
24660    p[0] = v&0x7f;
24661    return 1;
24662  }
24663  if( v<=0x3fff ){
24664    p[0] = ((v>>7)&0x7f)|0x80;
24665    p[1] = v&0x7f;
24666    return 2;
24667  }
24668  return putVarint64(p,v);
24669}
24670
24671/*
24672** Bitmasks used by sqlite3GetVarint().  These precomputed constants
24673** are defined here rather than simply putting the constant expressions
24674** inline in order to work around bugs in the RVT compiler.
24675**
24676** SLOT_2_0     A mask for  (0x7f<<14) | 0x7f
24677**
24678** SLOT_4_2_0   A mask for  (0x7f<<28) | SLOT_2_0
24679*/
24680#define SLOT_2_0     0x001fc07f
24681#define SLOT_4_2_0   0xf01fc07f
24682
24683
24684/*
24685** Read a 64-bit variable-length integer from memory starting at p[0].
24686** Return the number of bytes read.  The value is stored in *v.
24687*/
24688SQLITE_PRIVATE u8 sqlite3GetVarint(const unsigned char *p, u64 *v){
24689  u32 a,b,s;
24690
24691  a = *p;
24692  /* a: p0 (unmasked) */
24693  if (!(a&0x80))
24694  {
24695    *v = a;
24696    return 1;
24697  }
24698
24699  p++;
24700  b = *p;
24701  /* b: p1 (unmasked) */
24702  if (!(b&0x80))
24703  {
24704    a &= 0x7f;
24705    a = a<<7;
24706    a |= b;
24707    *v = a;
24708    return 2;
24709  }
24710
24711  /* Verify that constants are precomputed correctly */
24712  assert( SLOT_2_0 == ((0x7f<<14) | (0x7f)) );
24713  assert( SLOT_4_2_0 == ((0xfU<<28) | (0x7f<<14) | (0x7f)) );
24714
24715  p++;
24716  a = a<<14;
24717  a |= *p;
24718  /* a: p0<<14 | p2 (unmasked) */
24719  if (!(a&0x80))
24720  {
24721    a &= SLOT_2_0;
24722    b &= 0x7f;
24723    b = b<<7;
24724    a |= b;
24725    *v = a;
24726    return 3;
24727  }
24728
24729  /* CSE1 from below */
24730  a &= SLOT_2_0;
24731  p++;
24732  b = b<<14;
24733  b |= *p;
24734  /* b: p1<<14 | p3 (unmasked) */
24735  if (!(b&0x80))
24736  {
24737    b &= SLOT_2_0;
24738    /* moved CSE1 up */
24739    /* a &= (0x7f<<14)|(0x7f); */
24740    a = a<<7;
24741    a |= b;
24742    *v = a;
24743    return 4;
24744  }
24745
24746  /* a: p0<<14 | p2 (masked) */
24747  /* b: p1<<14 | p3 (unmasked) */
24748  /* 1:save off p0<<21 | p1<<14 | p2<<7 | p3 (masked) */
24749  /* moved CSE1 up */
24750  /* a &= (0x7f<<14)|(0x7f); */
24751  b &= SLOT_2_0;
24752  s = a;
24753  /* s: p0<<14 | p2 (masked) */
24754
24755  p++;
24756  a = a<<14;
24757  a |= *p;
24758  /* a: p0<<28 | p2<<14 | p4 (unmasked) */
24759  if (!(a&0x80))
24760  {
24761    /* we can skip these cause they were (effectively) done above in calc'ing s */
24762    /* a &= (0x7f<<28)|(0x7f<<14)|(0x7f); */
24763    /* b &= (0x7f<<14)|(0x7f); */
24764    b = b<<7;
24765    a |= b;
24766    s = s>>18;
24767    *v = ((u64)s)<<32 | a;
24768    return 5;
24769  }
24770
24771  /* 2:save off p0<<21 | p1<<14 | p2<<7 | p3 (masked) */
24772  s = s<<7;
24773  s |= b;
24774  /* s: p0<<21 | p1<<14 | p2<<7 | p3 (masked) */
24775
24776  p++;
24777  b = b<<14;
24778  b |= *p;
24779  /* b: p1<<28 | p3<<14 | p5 (unmasked) */
24780  if (!(b&0x80))
24781  {
24782    /* we can skip this cause it was (effectively) done above in calc'ing s */
24783    /* b &= (0x7f<<28)|(0x7f<<14)|(0x7f); */
24784    a &= SLOT_2_0;
24785    a = a<<7;
24786    a |= b;
24787    s = s>>18;
24788    *v = ((u64)s)<<32 | a;
24789    return 6;
24790  }
24791
24792  p++;
24793  a = a<<14;
24794  a |= *p;
24795  /* a: p2<<28 | p4<<14 | p6 (unmasked) */
24796  if (!(a&0x80))
24797  {
24798    a &= SLOT_4_2_0;
24799    b &= SLOT_2_0;
24800    b = b<<7;
24801    a |= b;
24802    s = s>>11;
24803    *v = ((u64)s)<<32 | a;
24804    return 7;
24805  }
24806
24807  /* CSE2 from below */
24808  a &= SLOT_2_0;
24809  p++;
24810  b = b<<14;
24811  b |= *p;
24812  /* b: p3<<28 | p5<<14 | p7 (unmasked) */
24813  if (!(b&0x80))
24814  {
24815    b &= SLOT_4_2_0;
24816    /* moved CSE2 up */
24817    /* a &= (0x7f<<14)|(0x7f); */
24818    a = a<<7;
24819    a |= b;
24820    s = s>>4;
24821    *v = ((u64)s)<<32 | a;
24822    return 8;
24823  }
24824
24825  p++;
24826  a = a<<15;
24827  a |= *p;
24828  /* a: p4<<29 | p6<<15 | p8 (unmasked) */
24829
24830  /* moved CSE2 up */
24831  /* a &= (0x7f<<29)|(0x7f<<15)|(0xff); */
24832  b &= SLOT_2_0;
24833  b = b<<8;
24834  a |= b;
24835
24836  s = s<<4;
24837  b = p[-4];
24838  b &= 0x7f;
24839  b = b>>3;
24840  s |= b;
24841
24842  *v = ((u64)s)<<32 | a;
24843
24844  return 9;
24845}
24846
24847/*
24848** Read a 32-bit variable-length integer from memory starting at p[0].
24849** Return the number of bytes read.  The value is stored in *v.
24850**
24851** If the varint stored in p[0] is larger than can fit in a 32-bit unsigned
24852** integer, then set *v to 0xffffffff.
24853**
24854** A MACRO version, getVarint32, is provided which inlines the
24855** single-byte case.  All code should use the MACRO version as
24856** this function assumes the single-byte case has already been handled.
24857*/
24858SQLITE_PRIVATE u8 sqlite3GetVarint32(const unsigned char *p, u32 *v){
24859  u32 a,b;
24860
24861  /* The 1-byte case.  Overwhelmingly the most common.  Handled inline
24862  ** by the getVarin32() macro */
24863  a = *p;
24864  /* a: p0 (unmasked) */
24865#ifndef getVarint32
24866  if (!(a&0x80))
24867  {
24868    /* Values between 0 and 127 */
24869    *v = a;
24870    return 1;
24871  }
24872#endif
24873
24874  /* The 2-byte case */
24875  p++;
24876  b = *p;
24877  /* b: p1 (unmasked) */
24878  if (!(b&0x80))
24879  {
24880    /* Values between 128 and 16383 */
24881    a &= 0x7f;
24882    a = a<<7;
24883    *v = a | b;
24884    return 2;
24885  }
24886
24887  /* The 3-byte case */
24888  p++;
24889  a = a<<14;
24890  a |= *p;
24891  /* a: p0<<14 | p2 (unmasked) */
24892  if (!(a&0x80))
24893  {
24894    /* Values between 16384 and 2097151 */
24895    a &= (0x7f<<14)|(0x7f);
24896    b &= 0x7f;
24897    b = b<<7;
24898    *v = a | b;
24899    return 3;
24900  }
24901
24902  /* A 32-bit varint is used to store size information in btrees.
24903  ** Objects are rarely larger than 2MiB limit of a 3-byte varint.
24904  ** A 3-byte varint is sufficient, for example, to record the size
24905  ** of a 1048569-byte BLOB or string.
24906  **
24907  ** We only unroll the first 1-, 2-, and 3- byte cases.  The very
24908  ** rare larger cases can be handled by the slower 64-bit varint
24909  ** routine.
24910  */
24911#if 1
24912  {
24913    u64 v64;
24914    u8 n;
24915
24916    p -= 2;
24917    n = sqlite3GetVarint(p, &v64);
24918    assert( n>3 && n<=9 );
24919    if( (v64 & SQLITE_MAX_U32)!=v64 ){
24920      *v = 0xffffffff;
24921    }else{
24922      *v = (u32)v64;
24923    }
24924    return n;
24925  }
24926
24927#else
24928  /* For following code (kept for historical record only) shows an
24929  ** unrolling for the 3- and 4-byte varint cases.  This code is
24930  ** slightly faster, but it is also larger and much harder to test.
24931  */
24932  p++;
24933  b = b<<14;
24934  b |= *p;
24935  /* b: p1<<14 | p3 (unmasked) */
24936  if (!(b&0x80))
24937  {
24938    /* Values between 2097152 and 268435455 */
24939    b &= (0x7f<<14)|(0x7f);
24940    a &= (0x7f<<14)|(0x7f);
24941    a = a<<7;
24942    *v = a | b;
24943    return 4;
24944  }
24945
24946  p++;
24947  a = a<<14;
24948  a |= *p;
24949  /* a: p0<<28 | p2<<14 | p4 (unmasked) */
24950  if (!(a&0x80))
24951  {
24952    /* Values  between 268435456 and 34359738367 */
24953    a &= SLOT_4_2_0;
24954    b &= SLOT_4_2_0;
24955    b = b<<7;
24956    *v = a | b;
24957    return 5;
24958  }
24959
24960  /* We can only reach this point when reading a corrupt database
24961  ** file.  In that case we are not in any hurry.  Use the (relatively
24962  ** slow) general-purpose sqlite3GetVarint() routine to extract the
24963  ** value. */
24964  {
24965    u64 v64;
24966    u8 n;
24967
24968    p -= 4;
24969    n = sqlite3GetVarint(p, &v64);
24970    assert( n>5 && n<=9 );
24971    *v = (u32)v64;
24972    return n;
24973  }
24974#endif
24975}
24976
24977/*
24978** Return the number of bytes that will be needed to store the given
24979** 64-bit integer.
24980*/
24981SQLITE_PRIVATE int sqlite3VarintLen(u64 v){
24982  int i = 0;
24983  do{
24984    i++;
24985    v >>= 7;
24986  }while( v!=0 && ALWAYS(i<9) );
24987  return i;
24988}
24989
24990
24991/*
24992** Read or write a four-byte big-endian integer value.
24993*/
24994SQLITE_PRIVATE u32 sqlite3Get4byte(const u8 *p){
24995#if SQLITE_BYTEORDER==4321
24996  u32 x;
24997  memcpy(&x,p,4);
24998  return x;
24999#elif SQLITE_BYTEORDER==1234 && defined(__GNUC__) && GCC_VERSION>=4003000
25000  u32 x;
25001  memcpy(&x,p,4);
25002  return __builtin_bswap32(x);
25003#elif SQLITE_BYTEORDER==1234 && defined(_MSC_VER) && _MSC_VER>=1300
25004  u32 x;
25005  memcpy(&x,p,4);
25006  return _byteswap_ulong(x);
25007#else
25008  testcase( p[0]&0x80 );
25009  return ((unsigned)p[0]<<24) | (p[1]<<16) | (p[2]<<8) | p[3];
25010#endif
25011}
25012SQLITE_PRIVATE void sqlite3Put4byte(unsigned char *p, u32 v){
25013#if SQLITE_BYTEORDER==4321
25014  memcpy(p,&v,4);
25015#elif SQLITE_BYTEORDER==1234 && defined(__GNUC__) && GCC_VERSION>=4003000
25016  u32 x = __builtin_bswap32(v);
25017  memcpy(p,&x,4);
25018#elif SQLITE_BYTEORDER==1234 && defined(_MSC_VER) && _MSC_VER>=1300
25019  u32 x = _byteswap_ulong(v);
25020  memcpy(p,&x,4);
25021#else
25022  p[0] = (u8)(v>>24);
25023  p[1] = (u8)(v>>16);
25024  p[2] = (u8)(v>>8);
25025  p[3] = (u8)v;
25026#endif
25027}
25028
25029
25030
25031/*
25032** Translate a single byte of Hex into an integer.
25033** This routine only works if h really is a valid hexadecimal
25034** character:  0..9a..fA..F
25035*/
25036SQLITE_PRIVATE u8 sqlite3HexToInt(int h){
25037  assert( (h>='0' && h<='9') ||  (h>='a' && h<='f') ||  (h>='A' && h<='F') );
25038#ifdef SQLITE_ASCII
25039  h += 9*(1&(h>>6));
25040#endif
25041#ifdef SQLITE_EBCDIC
25042  h += 9*(1&~(h>>4));
25043#endif
25044  return (u8)(h & 0xf);
25045}
25046
25047#if !defined(SQLITE_OMIT_BLOB_LITERAL) || defined(SQLITE_HAS_CODEC)
25048/*
25049** Convert a BLOB literal of the form "x'hhhhhh'" into its binary
25050** value.  Return a pointer to its binary value.  Space to hold the
25051** binary value has been obtained from malloc and must be freed by
25052** the calling routine.
25053*/
25054SQLITE_PRIVATE void *sqlite3HexToBlob(sqlite3 *db, const char *z, int n){
25055  char *zBlob;
25056  int i;
25057
25058  zBlob = (char *)sqlite3DbMallocRaw(db, n/2 + 1);
25059  n--;
25060  if( zBlob ){
25061    for(i=0; i<n; i+=2){
25062      zBlob[i/2] = (sqlite3HexToInt(z[i])<<4) | sqlite3HexToInt(z[i+1]);
25063    }
25064    zBlob[i/2] = 0;
25065  }
25066  return zBlob;
25067}
25068#endif /* !SQLITE_OMIT_BLOB_LITERAL || SQLITE_HAS_CODEC */
25069
25070/*
25071** Log an error that is an API call on a connection pointer that should
25072** not have been used.  The "type" of connection pointer is given as the
25073** argument.  The zType is a word like "NULL" or "closed" or "invalid".
25074*/
25075static void logBadConnection(const char *zType){
25076  sqlite3_log(SQLITE_MISUSE,
25077     "API call with %s database connection pointer",
25078     zType
25079  );
25080}
25081
25082/*
25083** Check to make sure we have a valid db pointer.  This test is not
25084** foolproof but it does provide some measure of protection against
25085** misuse of the interface such as passing in db pointers that are
25086** NULL or which have been previously closed.  If this routine returns
25087** 1 it means that the db pointer is valid and 0 if it should not be
25088** dereferenced for any reason.  The calling function should invoke
25089** SQLITE_MISUSE immediately.
25090**
25091** sqlite3SafetyCheckOk() requires that the db pointer be valid for
25092** use.  sqlite3SafetyCheckSickOrOk() allows a db pointer that failed to
25093** open properly and is not fit for general use but which can be
25094** used as an argument to sqlite3_errmsg() or sqlite3_close().
25095*/
25096SQLITE_PRIVATE int sqlite3SafetyCheckOk(sqlite3 *db){
25097  u32 magic;
25098  if( db==0 ){
25099    logBadConnection("NULL");
25100    return 0;
25101  }
25102  magic = db->magic;
25103  if( magic!=SQLITE_MAGIC_OPEN ){
25104    if( sqlite3SafetyCheckSickOrOk(db) ){
25105      testcase( sqlite3GlobalConfig.xLog!=0 );
25106      logBadConnection("unopened");
25107    }
25108    return 0;
25109  }else{
25110    return 1;
25111  }
25112}
25113SQLITE_PRIVATE int sqlite3SafetyCheckSickOrOk(sqlite3 *db){
25114  u32 magic;
25115  magic = db->magic;
25116  if( magic!=SQLITE_MAGIC_SICK &&
25117      magic!=SQLITE_MAGIC_OPEN &&
25118      magic!=SQLITE_MAGIC_BUSY ){
25119    testcase( sqlite3GlobalConfig.xLog!=0 );
25120    logBadConnection("invalid");
25121    return 0;
25122  }else{
25123    return 1;
25124  }
25125}
25126
25127/*
25128** Attempt to add, substract, or multiply the 64-bit signed value iB against
25129** the other 64-bit signed integer at *pA and store the result in *pA.
25130** Return 0 on success.  Or if the operation would have resulted in an
25131** overflow, leave *pA unchanged and return 1.
25132*/
25133SQLITE_PRIVATE int sqlite3AddInt64(i64 *pA, i64 iB){
25134  i64 iA = *pA;
25135  testcase( iA==0 ); testcase( iA==1 );
25136  testcase( iB==-1 ); testcase( iB==0 );
25137  if( iB>=0 ){
25138    testcase( iA>0 && LARGEST_INT64 - iA == iB );
25139    testcase( iA>0 && LARGEST_INT64 - iA == iB - 1 );
25140    if( iA>0 && LARGEST_INT64 - iA < iB ) return 1;
25141  }else{
25142    testcase( iA<0 && -(iA + LARGEST_INT64) == iB + 1 );
25143    testcase( iA<0 && -(iA + LARGEST_INT64) == iB + 2 );
25144    if( iA<0 && -(iA + LARGEST_INT64) > iB + 1 ) return 1;
25145  }
25146  *pA += iB;
25147  return 0;
25148}
25149SQLITE_PRIVATE int sqlite3SubInt64(i64 *pA, i64 iB){
25150  testcase( iB==SMALLEST_INT64+1 );
25151  if( iB==SMALLEST_INT64 ){
25152    testcase( (*pA)==(-1) ); testcase( (*pA)==0 );
25153    if( (*pA)>=0 ) return 1;
25154    *pA -= iB;
25155    return 0;
25156  }else{
25157    return sqlite3AddInt64(pA, -iB);
25158  }
25159}
25160#define TWOPOWER32 (((i64)1)<<32)
25161#define TWOPOWER31 (((i64)1)<<31)
25162SQLITE_PRIVATE int sqlite3MulInt64(i64 *pA, i64 iB){
25163  i64 iA = *pA;
25164  i64 iA1, iA0, iB1, iB0, r;
25165
25166  iA1 = iA/TWOPOWER32;
25167  iA0 = iA % TWOPOWER32;
25168  iB1 = iB/TWOPOWER32;
25169  iB0 = iB % TWOPOWER32;
25170  if( iA1==0 ){
25171    if( iB1==0 ){
25172      *pA *= iB;
25173      return 0;
25174    }
25175    r = iA0*iB1;
25176  }else if( iB1==0 ){
25177    r = iA1*iB0;
25178  }else{
25179    /* If both iA1 and iB1 are non-zero, overflow will result */
25180    return 1;
25181  }
25182  testcase( r==(-TWOPOWER31)-1 );
25183  testcase( r==(-TWOPOWER31) );
25184  testcase( r==TWOPOWER31 );
25185  testcase( r==TWOPOWER31-1 );
25186  if( r<(-TWOPOWER31) || r>=TWOPOWER31 ) return 1;
25187  r *= TWOPOWER32;
25188  if( sqlite3AddInt64(&r, iA0*iB0) ) return 1;
25189  *pA = r;
25190  return 0;
25191}
25192
25193/*
25194** Compute the absolute value of a 32-bit signed integer, of possible.  Or
25195** if the integer has a value of -2147483648, return +2147483647
25196*/
25197SQLITE_PRIVATE int sqlite3AbsInt32(int x){
25198  if( x>=0 ) return x;
25199  if( x==(int)0x80000000 ) return 0x7fffffff;
25200  return -x;
25201}
25202
25203#ifdef SQLITE_ENABLE_8_3_NAMES
25204/*
25205** If SQLITE_ENABLE_8_3_NAMES is set at compile-time and if the database
25206** filename in zBaseFilename is a URI with the "8_3_names=1" parameter and
25207** if filename in z[] has a suffix (a.k.a. "extension") that is longer than
25208** three characters, then shorten the suffix on z[] to be the last three
25209** characters of the original suffix.
25210**
25211** If SQLITE_ENABLE_8_3_NAMES is set to 2 at compile-time, then always
25212** do the suffix shortening regardless of URI parameter.
25213**
25214** Examples:
25215**
25216**     test.db-journal    =>   test.nal
25217**     test.db-wal        =>   test.wal
25218**     test.db-shm        =>   test.shm
25219**     test.db-mj7f3319fa =>   test.9fa
25220*/
25221SQLITE_PRIVATE void sqlite3FileSuffix3(const char *zBaseFilename, char *z){
25222#if SQLITE_ENABLE_8_3_NAMES<2
25223  if( sqlite3_uri_boolean(zBaseFilename, "8_3_names", 0) )
25224#endif
25225  {
25226    int i, sz;
25227    sz = sqlite3Strlen30(z);
25228    for(i=sz-1; i>0 && z[i]!='/' && z[i]!='.'; i--){}
25229    if( z[i]=='.' && ALWAYS(sz>i+4) ) memmove(&z[i+1], &z[sz-3], 4);
25230  }
25231}
25232#endif
25233
25234/*
25235** Find (an approximate) sum of two LogEst values.  This computation is
25236** not a simple "+" operator because LogEst is stored as a logarithmic
25237** value.
25238**
25239*/
25240SQLITE_PRIVATE LogEst sqlite3LogEstAdd(LogEst a, LogEst b){
25241  static const unsigned char x[] = {
25242     10, 10,                         /* 0,1 */
25243      9, 9,                          /* 2,3 */
25244      8, 8,                          /* 4,5 */
25245      7, 7, 7,                       /* 6,7,8 */
25246      6, 6, 6,                       /* 9,10,11 */
25247      5, 5, 5,                       /* 12-14 */
25248      4, 4, 4, 4,                    /* 15-18 */
25249      3, 3, 3, 3, 3, 3,              /* 19-24 */
25250      2, 2, 2, 2, 2, 2, 2,           /* 25-31 */
25251  };
25252  if( a>=b ){
25253    if( a>b+49 ) return a;
25254    if( a>b+31 ) return a+1;
25255    return a+x[a-b];
25256  }else{
25257    if( b>a+49 ) return b;
25258    if( b>a+31 ) return b+1;
25259    return b+x[b-a];
25260  }
25261}
25262
25263/*
25264** Convert an integer into a LogEst.  In other words, compute an
25265** approximation for 10*log2(x).
25266*/
25267SQLITE_PRIVATE LogEst sqlite3LogEst(u64 x){
25268  static LogEst a[] = { 0, 2, 3, 5, 6, 7, 8, 9 };
25269  LogEst y = 40;
25270  if( x<8 ){
25271    if( x<2 ) return 0;
25272    while( x<8 ){  y -= 10; x <<= 1; }
25273  }else{
25274    while( x>255 ){ y += 40; x >>= 4; }
25275    while( x>15 ){  y += 10; x >>= 1; }
25276  }
25277  return a[x&7] + y - 10;
25278}
25279
25280#ifndef SQLITE_OMIT_VIRTUALTABLE
25281/*
25282** Convert a double into a LogEst
25283** In other words, compute an approximation for 10*log2(x).
25284*/
25285SQLITE_PRIVATE LogEst sqlite3LogEstFromDouble(double x){
25286  u64 a;
25287  LogEst e;
25288  assert( sizeof(x)==8 && sizeof(a)==8 );
25289  if( x<=1 ) return 0;
25290  if( x<=2000000000 ) return sqlite3LogEst((u64)x);
25291  memcpy(&a, &x, 8);
25292  e = (a>>52) - 1022;
25293  return e*10;
25294}
25295#endif /* SQLITE_OMIT_VIRTUALTABLE */
25296
25297/*
25298** Convert a LogEst into an integer.
25299*/
25300SQLITE_PRIVATE u64 sqlite3LogEstToInt(LogEst x){
25301  u64 n;
25302  if( x<10 ) return 1;
25303  n = x%10;
25304  x /= 10;
25305  if( n>=5 ) n -= 2;
25306  else if( n>=1 ) n -= 1;
25307  if( x>=3 ){
25308    return x>60 ? (u64)LARGEST_INT64 : (n+8)<<(x-3);
25309  }
25310  return (n+8)>>(3-x);
25311}
25312
25313/************** End of util.c ************************************************/
25314/************** Begin file hash.c ********************************************/
25315/*
25316** 2001 September 22
25317**
25318** The author disclaims copyright to this source code.  In place of
25319** a legal notice, here is a blessing:
25320**
25321**    May you do good and not evil.
25322**    May you find forgiveness for yourself and forgive others.
25323**    May you share freely, never taking more than you give.
25324**
25325*************************************************************************
25326** This is the implementation of generic hash-tables
25327** used in SQLite.
25328*/
25329/* #include "sqliteInt.h" */
25330/* #include <assert.h> */
25331
25332/* Turn bulk memory into a hash table object by initializing the
25333** fields of the Hash structure.
25334**
25335** "pNew" is a pointer to the hash table that is to be initialized.
25336*/
25337SQLITE_PRIVATE void sqlite3HashInit(Hash *pNew){
25338  assert( pNew!=0 );
25339  pNew->first = 0;
25340  pNew->count = 0;
25341  pNew->htsize = 0;
25342  pNew->ht = 0;
25343}
25344
25345/* Remove all entries from a hash table.  Reclaim all memory.
25346** Call this routine to delete a hash table or to reset a hash table
25347** to the empty state.
25348*/
25349SQLITE_PRIVATE void sqlite3HashClear(Hash *pH){
25350  HashElem *elem;         /* For looping over all elements of the table */
25351
25352  assert( pH!=0 );
25353  elem = pH->first;
25354  pH->first = 0;
25355  sqlite3_free(pH->ht);
25356  pH->ht = 0;
25357  pH->htsize = 0;
25358  while( elem ){
25359    HashElem *next_elem = elem->next;
25360    sqlite3_free(elem);
25361    elem = next_elem;
25362  }
25363  pH->count = 0;
25364}
25365
25366/*
25367** The hashing function.
25368*/
25369static unsigned int strHash(const char *z){
25370  unsigned int h = 0;
25371  unsigned char c;
25372  while( (c = (unsigned char)*z++)!=0 ){
25373    h = (h<<3) ^ h ^ sqlite3UpperToLower[c];
25374  }
25375  return h;
25376}
25377
25378
25379/* Link pNew element into the hash table pH.  If pEntry!=0 then also
25380** insert pNew into the pEntry hash bucket.
25381*/
25382static void insertElement(
25383  Hash *pH,              /* The complete hash table */
25384  struct _ht *pEntry,    /* The entry into which pNew is inserted */
25385  HashElem *pNew         /* The element to be inserted */
25386){
25387  HashElem *pHead;       /* First element already in pEntry */
25388  if( pEntry ){
25389    pHead = pEntry->count ? pEntry->chain : 0;
25390    pEntry->count++;
25391    pEntry->chain = pNew;
25392  }else{
25393    pHead = 0;
25394  }
25395  if( pHead ){
25396    pNew->next = pHead;
25397    pNew->prev = pHead->prev;
25398    if( pHead->prev ){ pHead->prev->next = pNew; }
25399    else             { pH->first = pNew; }
25400    pHead->prev = pNew;
25401  }else{
25402    pNew->next = pH->first;
25403    if( pH->first ){ pH->first->prev = pNew; }
25404    pNew->prev = 0;
25405    pH->first = pNew;
25406  }
25407}
25408
25409
25410/* Resize the hash table so that it cantains "new_size" buckets.
25411**
25412** The hash table might fail to resize if sqlite3_malloc() fails or
25413** if the new size is the same as the prior size.
25414** Return TRUE if the resize occurs and false if not.
25415*/
25416static int rehash(Hash *pH, unsigned int new_size){
25417  struct _ht *new_ht;            /* The new hash table */
25418  HashElem *elem, *next_elem;    /* For looping over existing elements */
25419
25420#if SQLITE_MALLOC_SOFT_LIMIT>0
25421  if( new_size*sizeof(struct _ht)>SQLITE_MALLOC_SOFT_LIMIT ){
25422    new_size = SQLITE_MALLOC_SOFT_LIMIT/sizeof(struct _ht);
25423  }
25424  if( new_size==pH->htsize ) return 0;
25425#endif
25426
25427  /* The inability to allocates space for a larger hash table is
25428  ** a performance hit but it is not a fatal error.  So mark the
25429  ** allocation as a benign. Use sqlite3Malloc()/memset(0) instead of
25430  ** sqlite3MallocZero() to make the allocation, as sqlite3MallocZero()
25431  ** only zeroes the requested number of bytes whereas this module will
25432  ** use the actual amount of space allocated for the hash table (which
25433  ** may be larger than the requested amount).
25434  */
25435  sqlite3BeginBenignMalloc();
25436  new_ht = (struct _ht *)sqlite3Malloc( new_size*sizeof(struct _ht) );
25437  sqlite3EndBenignMalloc();
25438
25439  if( new_ht==0 ) return 0;
25440  sqlite3_free(pH->ht);
25441  pH->ht = new_ht;
25442  pH->htsize = new_size = sqlite3MallocSize(new_ht)/sizeof(struct _ht);
25443  memset(new_ht, 0, new_size*sizeof(struct _ht));
25444  for(elem=pH->first, pH->first=0; elem; elem = next_elem){
25445    unsigned int h = strHash(elem->pKey) % new_size;
25446    next_elem = elem->next;
25447    insertElement(pH, &new_ht[h], elem);
25448  }
25449  return 1;
25450}
25451
25452/* This function (for internal use only) locates an element in an
25453** hash table that matches the given key.  The hash for this key is
25454** also computed and returned in the *pH parameter.
25455*/
25456static HashElem *findElementWithHash(
25457  const Hash *pH,     /* The pH to be searched */
25458  const char *pKey,   /* The key we are searching for */
25459  unsigned int *pHash /* Write the hash value here */
25460){
25461  HashElem *elem;                /* Used to loop thru the element list */
25462  int count;                     /* Number of elements left to test */
25463  unsigned int h;                /* The computed hash */
25464
25465  if( pH->ht ){
25466    struct _ht *pEntry;
25467    h = strHash(pKey) % pH->htsize;
25468    pEntry = &pH->ht[h];
25469    elem = pEntry->chain;
25470    count = pEntry->count;
25471  }else{
25472    h = 0;
25473    elem = pH->first;
25474    count = pH->count;
25475  }
25476  *pHash = h;
25477  while( count-- ){
25478    assert( elem!=0 );
25479    if( sqlite3StrICmp(elem->pKey,pKey)==0 ){
25480      return elem;
25481    }
25482    elem = elem->next;
25483  }
25484  return 0;
25485}
25486
25487/* Remove a single entry from the hash table given a pointer to that
25488** element and a hash on the element's key.
25489*/
25490static void removeElementGivenHash(
25491  Hash *pH,         /* The pH containing "elem" */
25492  HashElem* elem,   /* The element to be removed from the pH */
25493  unsigned int h    /* Hash value for the element */
25494){
25495  struct _ht *pEntry;
25496  if( elem->prev ){
25497    elem->prev->next = elem->next;
25498  }else{
25499    pH->first = elem->next;
25500  }
25501  if( elem->next ){
25502    elem->next->prev = elem->prev;
25503  }
25504  if( pH->ht ){
25505    pEntry = &pH->ht[h];
25506    if( pEntry->chain==elem ){
25507      pEntry->chain = elem->next;
25508    }
25509    pEntry->count--;
25510    assert( pEntry->count>=0 );
25511  }
25512  sqlite3_free( elem );
25513  pH->count--;
25514  if( pH->count==0 ){
25515    assert( pH->first==0 );
25516    assert( pH->count==0 );
25517    sqlite3HashClear(pH);
25518  }
25519}
25520
25521/* Attempt to locate an element of the hash table pH with a key
25522** that matches pKey.  Return the data for this element if it is
25523** found, or NULL if there is no match.
25524*/
25525SQLITE_PRIVATE void *sqlite3HashFind(const Hash *pH, const char *pKey){
25526  HashElem *elem;    /* The element that matches key */
25527  unsigned int h;    /* A hash on key */
25528
25529  assert( pH!=0 );
25530  assert( pKey!=0 );
25531  elem = findElementWithHash(pH, pKey, &h);
25532  return elem ? elem->data : 0;
25533}
25534
25535/* Insert an element into the hash table pH.  The key is pKey
25536** and the data is "data".
25537**
25538** If no element exists with a matching key, then a new
25539** element is created and NULL is returned.
25540**
25541** If another element already exists with the same key, then the
25542** new data replaces the old data and the old data is returned.
25543** The key is not copied in this instance.  If a malloc fails, then
25544** the new data is returned and the hash table is unchanged.
25545**
25546** If the "data" parameter to this function is NULL, then the
25547** element corresponding to "key" is removed from the hash table.
25548*/
25549SQLITE_PRIVATE void *sqlite3HashInsert(Hash *pH, const char *pKey, void *data){
25550  unsigned int h;       /* the hash of the key modulo hash table size */
25551  HashElem *elem;       /* Used to loop thru the element list */
25552  HashElem *new_elem;   /* New element added to the pH */
25553
25554  assert( pH!=0 );
25555  assert( pKey!=0 );
25556  elem = findElementWithHash(pH,pKey,&h);
25557  if( elem ){
25558    void *old_data = elem->data;
25559    if( data==0 ){
25560      removeElementGivenHash(pH,elem,h);
25561    }else{
25562      elem->data = data;
25563      elem->pKey = pKey;
25564    }
25565    return old_data;
25566  }
25567  if( data==0 ) return 0;
25568  new_elem = (HashElem*)sqlite3Malloc( sizeof(HashElem) );
25569  if( new_elem==0 ) return data;
25570  new_elem->pKey = pKey;
25571  new_elem->data = data;
25572  pH->count++;
25573  if( pH->count>=10 && pH->count > 2*pH->htsize ){
25574    if( rehash(pH, pH->count*2) ){
25575      assert( pH->htsize>0 );
25576      h = strHash(pKey) % pH->htsize;
25577    }
25578  }
25579  insertElement(pH, pH->ht ? &pH->ht[h] : 0, new_elem);
25580  return 0;
25581}
25582
25583/************** End of hash.c ************************************************/
25584/************** Begin file opcodes.c *****************************************/
25585/* Automatically generated.  Do not edit */
25586/* See the mkopcodec.awk script for details. */
25587#if !defined(SQLITE_OMIT_EXPLAIN) || defined(VDBE_PROFILE) || defined(SQLITE_DEBUG)
25588#if defined(SQLITE_ENABLE_EXPLAIN_COMMENTS) || defined(SQLITE_DEBUG)
25589# define OpHelp(X) "\0" X
25590#else
25591# define OpHelp(X)
25592#endif
25593SQLITE_PRIVATE const char *sqlite3OpcodeName(int i){
25594 static const char *const azName[] = { "?",
25595     /*   1 */ "Savepoint"        OpHelp(""),
25596     /*   2 */ "AutoCommit"       OpHelp(""),
25597     /*   3 */ "Transaction"      OpHelp(""),
25598     /*   4 */ "SorterNext"       OpHelp(""),
25599     /*   5 */ "PrevIfOpen"       OpHelp(""),
25600     /*   6 */ "NextIfOpen"       OpHelp(""),
25601     /*   7 */ "Prev"             OpHelp(""),
25602     /*   8 */ "Next"             OpHelp(""),
25603     /*   9 */ "Checkpoint"       OpHelp(""),
25604     /*  10 */ "JournalMode"      OpHelp(""),
25605     /*  11 */ "Vacuum"           OpHelp(""),
25606     /*  12 */ "VFilter"          OpHelp("iplan=r[P3] zplan='P4'"),
25607     /*  13 */ "VUpdate"          OpHelp("data=r[P3@P2]"),
25608     /*  14 */ "Goto"             OpHelp(""),
25609     /*  15 */ "Gosub"            OpHelp(""),
25610     /*  16 */ "Return"           OpHelp(""),
25611     /*  17 */ "InitCoroutine"    OpHelp(""),
25612     /*  18 */ "EndCoroutine"     OpHelp(""),
25613     /*  19 */ "Not"              OpHelp("r[P2]= !r[P1]"),
25614     /*  20 */ "Yield"            OpHelp(""),
25615     /*  21 */ "HaltIfNull"       OpHelp("if r[P3]=null halt"),
25616     /*  22 */ "Halt"             OpHelp(""),
25617     /*  23 */ "Integer"          OpHelp("r[P2]=P1"),
25618     /*  24 */ "Int64"            OpHelp("r[P2]=P4"),
25619     /*  25 */ "String"           OpHelp("r[P2]='P4' (len=P1)"),
25620     /*  26 */ "Null"             OpHelp("r[P2..P3]=NULL"),
25621     /*  27 */ "SoftNull"         OpHelp("r[P1]=NULL"),
25622     /*  28 */ "Blob"             OpHelp("r[P2]=P4 (len=P1)"),
25623     /*  29 */ "Variable"         OpHelp("r[P2]=parameter(P1,P4)"),
25624     /*  30 */ "Move"             OpHelp("r[P2@P3]=r[P1@P3]"),
25625     /*  31 */ "Copy"             OpHelp("r[P2@P3+1]=r[P1@P3+1]"),
25626     /*  32 */ "SCopy"            OpHelp("r[P2]=r[P1]"),
25627     /*  33 */ "ResultRow"        OpHelp("output=r[P1@P2]"),
25628     /*  34 */ "CollSeq"          OpHelp(""),
25629     /*  35 */ "Function0"        OpHelp("r[P3]=func(r[P2@P5])"),
25630     /*  36 */ "Function"         OpHelp("r[P3]=func(r[P2@P5])"),
25631     /*  37 */ "AddImm"           OpHelp("r[P1]=r[P1]+P2"),
25632     /*  38 */ "MustBeInt"        OpHelp(""),
25633     /*  39 */ "RealAffinity"     OpHelp(""),
25634     /*  40 */ "Cast"             OpHelp("affinity(r[P1])"),
25635     /*  41 */ "Permutation"      OpHelp(""),
25636     /*  42 */ "Compare"          OpHelp("r[P1@P3] <-> r[P2@P3]"),
25637     /*  43 */ "Jump"             OpHelp(""),
25638     /*  44 */ "Once"             OpHelp(""),
25639     /*  45 */ "If"               OpHelp(""),
25640     /*  46 */ "IfNot"            OpHelp(""),
25641     /*  47 */ "Column"           OpHelp("r[P3]=PX"),
25642     /*  48 */ "Affinity"         OpHelp("affinity(r[P1@P2])"),
25643     /*  49 */ "MakeRecord"       OpHelp("r[P3]=mkrec(r[P1@P2])"),
25644     /*  50 */ "Count"            OpHelp("r[P2]=count()"),
25645     /*  51 */ "ReadCookie"       OpHelp(""),
25646     /*  52 */ "SetCookie"        OpHelp(""),
25647     /*  53 */ "ReopenIdx"        OpHelp("root=P2 iDb=P3"),
25648     /*  54 */ "OpenRead"         OpHelp("root=P2 iDb=P3"),
25649     /*  55 */ "OpenWrite"        OpHelp("root=P2 iDb=P3"),
25650     /*  56 */ "OpenAutoindex"    OpHelp("nColumn=P2"),
25651     /*  57 */ "OpenEphemeral"    OpHelp("nColumn=P2"),
25652     /*  58 */ "SorterOpen"       OpHelp(""),
25653     /*  59 */ "SequenceTest"     OpHelp("if( cursor[P1].ctr++ ) pc = P2"),
25654     /*  60 */ "OpenPseudo"       OpHelp("P3 columns in r[P2]"),
25655     /*  61 */ "Close"            OpHelp(""),
25656     /*  62 */ "ColumnsUsed"      OpHelp(""),
25657     /*  63 */ "SeekLT"           OpHelp("key=r[P3@P4]"),
25658     /*  64 */ "SeekLE"           OpHelp("key=r[P3@P4]"),
25659     /*  65 */ "SeekGE"           OpHelp("key=r[P3@P4]"),
25660     /*  66 */ "SeekGT"           OpHelp("key=r[P3@P4]"),
25661     /*  67 */ "Seek"             OpHelp("intkey=r[P2]"),
25662     /*  68 */ "NoConflict"       OpHelp("key=r[P3@P4]"),
25663     /*  69 */ "NotFound"         OpHelp("key=r[P3@P4]"),
25664     /*  70 */ "Found"            OpHelp("key=r[P3@P4]"),
25665     /*  71 */ "Or"               OpHelp("r[P3]=(r[P1] || r[P2])"),
25666     /*  72 */ "And"              OpHelp("r[P3]=(r[P1] && r[P2])"),
25667     /*  73 */ "NotExists"        OpHelp("intkey=r[P3]"),
25668     /*  74 */ "Sequence"         OpHelp("r[P2]=cursor[P1].ctr++"),
25669     /*  75 */ "NewRowid"         OpHelp("r[P2]=rowid"),
25670     /*  76 */ "IsNull"           OpHelp("if r[P1]==NULL goto P2"),
25671     /*  77 */ "NotNull"          OpHelp("if r[P1]!=NULL goto P2"),
25672     /*  78 */ "Ne"               OpHelp("if r[P1]!=r[P3] goto P2"),
25673     /*  79 */ "Eq"               OpHelp("if r[P1]==r[P3] goto P2"),
25674     /*  80 */ "Gt"               OpHelp("if r[P1]>r[P3] goto P2"),
25675     /*  81 */ "Le"               OpHelp("if r[P1]<=r[P3] goto P2"),
25676     /*  82 */ "Lt"               OpHelp("if r[P1]<r[P3] goto P2"),
25677     /*  83 */ "Ge"               OpHelp("if r[P1]>=r[P3] goto P2"),
25678     /*  84 */ "Insert"           OpHelp("intkey=r[P3] data=r[P2]"),
25679     /*  85 */ "BitAnd"           OpHelp("r[P3]=r[P1]&r[P2]"),
25680     /*  86 */ "BitOr"            OpHelp("r[P3]=r[P1]|r[P2]"),
25681     /*  87 */ "ShiftLeft"        OpHelp("r[P3]=r[P2]<<r[P1]"),
25682     /*  88 */ "ShiftRight"       OpHelp("r[P3]=r[P2]>>r[P1]"),
25683     /*  89 */ "Add"              OpHelp("r[P3]=r[P1]+r[P2]"),
25684     /*  90 */ "Subtract"         OpHelp("r[P3]=r[P2]-r[P1]"),
25685     /*  91 */ "Multiply"         OpHelp("r[P3]=r[P1]*r[P2]"),
25686     /*  92 */ "Divide"           OpHelp("r[P3]=r[P2]/r[P1]"),
25687     /*  93 */ "Remainder"        OpHelp("r[P3]=r[P2]%r[P1]"),
25688     /*  94 */ "Concat"           OpHelp("r[P3]=r[P2]+r[P1]"),
25689     /*  95 */ "InsertInt"        OpHelp("intkey=P3 data=r[P2]"),
25690     /*  96 */ "BitNot"           OpHelp("r[P1]= ~r[P1]"),
25691     /*  97 */ "String8"          OpHelp("r[P2]='P4'"),
25692     /*  98 */ "Delete"           OpHelp(""),
25693     /*  99 */ "ResetCount"       OpHelp(""),
25694     /* 100 */ "SorterCompare"    OpHelp("if key(P1)!=trim(r[P3],P4) goto P2"),
25695     /* 101 */ "SorterData"       OpHelp("r[P2]=data"),
25696     /* 102 */ "RowKey"           OpHelp("r[P2]=key"),
25697     /* 103 */ "RowData"          OpHelp("r[P2]=data"),
25698     /* 104 */ "Rowid"            OpHelp("r[P2]=rowid"),
25699     /* 105 */ "NullRow"          OpHelp(""),
25700     /* 106 */ "Last"             OpHelp(""),
25701     /* 107 */ "SorterSort"       OpHelp(""),
25702     /* 108 */ "Sort"             OpHelp(""),
25703     /* 109 */ "Rewind"           OpHelp(""),
25704     /* 110 */ "SorterInsert"     OpHelp(""),
25705     /* 111 */ "IdxInsert"        OpHelp("key=r[P2]"),
25706     /* 112 */ "IdxDelete"        OpHelp("key=r[P2@P3]"),
25707     /* 113 */ "IdxRowid"         OpHelp("r[P2]=rowid"),
25708     /* 114 */ "IdxLE"            OpHelp("key=r[P3@P4]"),
25709     /* 115 */ "IdxGT"            OpHelp("key=r[P3@P4]"),
25710     /* 116 */ "IdxLT"            OpHelp("key=r[P3@P4]"),
25711     /* 117 */ "IdxGE"            OpHelp("key=r[P3@P4]"),
25712     /* 118 */ "Destroy"          OpHelp(""),
25713     /* 119 */ "Clear"            OpHelp(""),
25714     /* 120 */ "ResetSorter"      OpHelp(""),
25715     /* 121 */ "CreateIndex"      OpHelp("r[P2]=root iDb=P1"),
25716     /* 122 */ "CreateTable"      OpHelp("r[P2]=root iDb=P1"),
25717     /* 123 */ "ParseSchema"      OpHelp(""),
25718     /* 124 */ "LoadAnalysis"     OpHelp(""),
25719     /* 125 */ "DropTable"        OpHelp(""),
25720     /* 126 */ "DropIndex"        OpHelp(""),
25721     /* 127 */ "DropTrigger"      OpHelp(""),
25722     /* 128 */ "IntegrityCk"      OpHelp(""),
25723     /* 129 */ "RowSetAdd"        OpHelp("rowset(P1)=r[P2]"),
25724     /* 130 */ "RowSetRead"       OpHelp("r[P3]=rowset(P1)"),
25725     /* 131 */ "RowSetTest"       OpHelp("if r[P3] in rowset(P1) goto P2"),
25726     /* 132 */ "Program"          OpHelp(""),
25727     /* 133 */ "Real"             OpHelp("r[P2]=P4"),
25728     /* 134 */ "Param"            OpHelp(""),
25729     /* 135 */ "FkCounter"        OpHelp("fkctr[P1]+=P2"),
25730     /* 136 */ "FkIfZero"         OpHelp("if fkctr[P1]==0 goto P2"),
25731     /* 137 */ "MemMax"           OpHelp("r[P1]=max(r[P1],r[P2])"),
25732     /* 138 */ "IfPos"            OpHelp("if r[P1]>0 goto P2"),
25733     /* 139 */ "IfNeg"            OpHelp("r[P1]+=P3, if r[P1]<0 goto P2"),
25734     /* 140 */ "IfNotZero"        OpHelp("if r[P1]!=0 then r[P1]+=P3, goto P2"),
25735     /* 141 */ "DecrJumpZero"     OpHelp("if (--r[P1])==0 goto P2"),
25736     /* 142 */ "JumpZeroIncr"     OpHelp("if (r[P1]++)==0 ) goto P2"),
25737     /* 143 */ "AggStep0"         OpHelp("accum=r[P3] step(r[P2@P5])"),
25738     /* 144 */ "AggStep"          OpHelp("accum=r[P3] step(r[P2@P5])"),
25739     /* 145 */ "AggFinal"         OpHelp("accum=r[P1] N=P2"),
25740     /* 146 */ "IncrVacuum"       OpHelp(""),
25741     /* 147 */ "Expire"           OpHelp(""),
25742     /* 148 */ "TableLock"        OpHelp("iDb=P1 root=P2 write=P3"),
25743     /* 149 */ "VBegin"           OpHelp(""),
25744     /* 150 */ "VCreate"          OpHelp(""),
25745     /* 151 */ "VDestroy"         OpHelp(""),
25746     /* 152 */ "VOpen"            OpHelp(""),
25747     /* 153 */ "VColumn"          OpHelp("r[P3]=vcolumn(P2)"),
25748     /* 154 */ "VNext"            OpHelp(""),
25749     /* 155 */ "VRename"          OpHelp(""),
25750     /* 156 */ "Pagecount"        OpHelp(""),
25751     /* 157 */ "MaxPgcnt"         OpHelp(""),
25752     /* 158 */ "Init"             OpHelp("Start at P2"),
25753     /* 159 */ "Noop"             OpHelp(""),
25754     /* 160 */ "Explain"          OpHelp(""),
25755  };
25756  return azName[i];
25757}
25758#endif
25759
25760/************** End of opcodes.c *********************************************/
25761/************** Begin file os_unix.c *****************************************/
25762/*
25763** 2004 May 22
25764**
25765** The author disclaims copyright to this source code.  In place of
25766** a legal notice, here is a blessing:
25767**
25768**    May you do good and not evil.
25769**    May you find forgiveness for yourself and forgive others.
25770**    May you share freely, never taking more than you give.
25771**
25772******************************************************************************
25773**
25774** This file contains the VFS implementation for unix-like operating systems
25775** include Linux, MacOSX, *BSD, QNX, VxWorks, AIX, HPUX, and others.
25776**
25777** There are actually several different VFS implementations in this file.
25778** The differences are in the way that file locking is done.  The default
25779** implementation uses Posix Advisory Locks.  Alternative implementations
25780** use flock(), dot-files, various proprietary locking schemas, or simply
25781** skip locking all together.
25782**
25783** This source file is organized into divisions where the logic for various
25784** subfunctions is contained within the appropriate division.  PLEASE
25785** KEEP THE STRUCTURE OF THIS FILE INTACT.  New code should be placed
25786** in the correct division and should be clearly labeled.
25787**
25788** The layout of divisions is as follows:
25789**
25790**   *  General-purpose declarations and utility functions.
25791**   *  Unique file ID logic used by VxWorks.
25792**   *  Various locking primitive implementations (all except proxy locking):
25793**      + for Posix Advisory Locks
25794**      + for no-op locks
25795**      + for dot-file locks
25796**      + for flock() locking
25797**      + for named semaphore locks (VxWorks only)
25798**      + for AFP filesystem locks (MacOSX only)
25799**   *  sqlite3_file methods not associated with locking.
25800**   *  Definitions of sqlite3_io_methods objects for all locking
25801**      methods plus "finder" functions for each locking method.
25802**   *  sqlite3_vfs method implementations.
25803**   *  Locking primitives for the proxy uber-locking-method. (MacOSX only)
25804**   *  Definitions of sqlite3_vfs objects for all locking methods
25805**      plus implementations of sqlite3_os_init() and sqlite3_os_end().
25806*/
25807/* #include "sqliteInt.h" */
25808#if SQLITE_OS_UNIX              /* This file is used on unix only */
25809
25810/*
25811** There are various methods for file locking used for concurrency
25812** control:
25813**
25814**   1. POSIX locking (the default),
25815**   2. No locking,
25816**   3. Dot-file locking,
25817**   4. flock() locking,
25818**   5. AFP locking (OSX only),
25819**   6. Named POSIX semaphores (VXWorks only),
25820**   7. proxy locking. (OSX only)
25821**
25822** Styles 4, 5, and 7 are only available of SQLITE_ENABLE_LOCKING_STYLE
25823** is defined to 1.  The SQLITE_ENABLE_LOCKING_STYLE also enables automatic
25824** selection of the appropriate locking style based on the filesystem
25825** where the database is located.
25826*/
25827#if !defined(SQLITE_ENABLE_LOCKING_STYLE)
25828#  if defined(__APPLE__)
25829#    define SQLITE_ENABLE_LOCKING_STYLE 1
25830#  else
25831#    define SQLITE_ENABLE_LOCKING_STYLE 0
25832#  endif
25833#endif
25834
25835/*
25836** standard include files.
25837*/
25838#include <sys/types.h>
25839#include <sys/stat.h>
25840#include <fcntl.h>
25841#include <unistd.h>
25842/* #include <time.h> */
25843#include <sys/time.h>
25844#include <errno.h>
25845#if !defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0
25846# include <sys/mman.h>
25847#endif
25848
25849#if SQLITE_ENABLE_LOCKING_STYLE
25850# include <sys/ioctl.h>
25851# include <sys/file.h>
25852# include <sys/param.h>
25853#endif /* SQLITE_ENABLE_LOCKING_STYLE */
25854
25855#if defined(__APPLE__) && ((__MAC_OS_X_VERSION_MIN_REQUIRED > 1050) || \
25856                           (__IPHONE_OS_VERSION_MIN_REQUIRED > 2000))
25857#  if (!defined(TARGET_OS_EMBEDDED) || (TARGET_OS_EMBEDDED==0)) \
25858       && (!defined(TARGET_IPHONE_SIMULATOR) || (TARGET_IPHONE_SIMULATOR==0))
25859#    define HAVE_GETHOSTUUID 1
25860#  else
25861#    warning "gethostuuid() is disabled."
25862#  endif
25863#endif
25864
25865
25866#if OS_VXWORKS
25867/* # include <sys/ioctl.h> */
25868# include <semaphore.h>
25869# include <limits.h>
25870#endif /* OS_VXWORKS */
25871
25872#if defined(__APPLE__) || SQLITE_ENABLE_LOCKING_STYLE
25873# include <sys/mount.h>
25874#endif
25875
25876#ifdef HAVE_UTIME
25877# include <utime.h>
25878#endif
25879
25880/*
25881** Allowed values of unixFile.fsFlags
25882*/
25883#define SQLITE_FSFLAGS_IS_MSDOS     0x1
25884
25885/*
25886** If we are to be thread-safe, include the pthreads header and define
25887** the SQLITE_UNIX_THREADS macro.
25888*/
25889#if SQLITE_THREADSAFE
25890/* # include <pthread.h> */
25891# define SQLITE_UNIX_THREADS 1
25892#endif
25893
25894/*
25895** Default permissions when creating a new file
25896*/
25897#ifndef SQLITE_DEFAULT_FILE_PERMISSIONS
25898# define SQLITE_DEFAULT_FILE_PERMISSIONS 0644
25899#endif
25900
25901/*
25902** Default permissions when creating auto proxy dir
25903*/
25904#ifndef SQLITE_DEFAULT_PROXYDIR_PERMISSIONS
25905# define SQLITE_DEFAULT_PROXYDIR_PERMISSIONS 0755
25906#endif
25907
25908/*
25909** Maximum supported path-length.
25910*/
25911#define MAX_PATHNAME 512
25912
25913/* Always cast the getpid() return type for compatibility with
25914** kernel modules in VxWorks. */
25915#define osGetpid(X) (pid_t)getpid()
25916
25917/*
25918** Only set the lastErrno if the error code is a real error and not
25919** a normal expected return code of SQLITE_BUSY or SQLITE_OK
25920*/
25921#define IS_LOCK_ERROR(x)  ((x != SQLITE_OK) && (x != SQLITE_BUSY))
25922
25923/* Forward references */
25924typedef struct unixShm unixShm;               /* Connection shared memory */
25925typedef struct unixShmNode unixShmNode;       /* Shared memory instance */
25926typedef struct unixInodeInfo unixInodeInfo;   /* An i-node */
25927typedef struct UnixUnusedFd UnixUnusedFd;     /* An unused file descriptor */
25928
25929/*
25930** Sometimes, after a file handle is closed by SQLite, the file descriptor
25931** cannot be closed immediately. In these cases, instances of the following
25932** structure are used to store the file descriptor while waiting for an
25933** opportunity to either close or reuse it.
25934*/
25935struct UnixUnusedFd {
25936  int fd;                   /* File descriptor to close */
25937  int flags;                /* Flags this file descriptor was opened with */
25938  UnixUnusedFd *pNext;      /* Next unused file descriptor on same file */
25939};
25940
25941/*
25942** The unixFile structure is subclass of sqlite3_file specific to the unix
25943** VFS implementations.
25944*/
25945typedef struct unixFile unixFile;
25946struct unixFile {
25947  sqlite3_io_methods const *pMethod;  /* Always the first entry */
25948  sqlite3_vfs *pVfs;                  /* The VFS that created this unixFile */
25949  unixInodeInfo *pInode;              /* Info about locks on this inode */
25950  int h;                              /* The file descriptor */
25951  unsigned char eFileLock;            /* The type of lock held on this fd */
25952  unsigned short int ctrlFlags;       /* Behavioral bits.  UNIXFILE_* flags */
25953  int lastErrno;                      /* The unix errno from last I/O error */
25954  void *lockingContext;               /* Locking style specific state */
25955  UnixUnusedFd *pUnused;              /* Pre-allocated UnixUnusedFd */
25956  const char *zPath;                  /* Name of the file */
25957  unixShm *pShm;                      /* Shared memory segment information */
25958  int szChunk;                        /* Configured by FCNTL_CHUNK_SIZE */
25959#if SQLITE_MAX_MMAP_SIZE>0
25960  int nFetchOut;                      /* Number of outstanding xFetch refs */
25961  sqlite3_int64 mmapSize;             /* Usable size of mapping at pMapRegion */
25962  sqlite3_int64 mmapSizeActual;       /* Actual size of mapping at pMapRegion */
25963  sqlite3_int64 mmapSizeMax;          /* Configured FCNTL_MMAP_SIZE value */
25964  void *pMapRegion;                   /* Memory mapped region */
25965#endif
25966#ifdef __QNXNTO__
25967  int sectorSize;                     /* Device sector size */
25968  int deviceCharacteristics;          /* Precomputed device characteristics */
25969#endif
25970#if SQLITE_ENABLE_LOCKING_STYLE
25971  int openFlags;                      /* The flags specified at open() */
25972#endif
25973#if SQLITE_ENABLE_LOCKING_STYLE || defined(__APPLE__)
25974  unsigned fsFlags;                   /* cached details from statfs() */
25975#endif
25976#if OS_VXWORKS
25977  struct vxworksFileId *pId;          /* Unique file ID */
25978#endif
25979#ifdef SQLITE_DEBUG
25980  /* The next group of variables are used to track whether or not the
25981  ** transaction counter in bytes 24-27 of database files are updated
25982  ** whenever any part of the database changes.  An assertion fault will
25983  ** occur if a file is updated without also updating the transaction
25984  ** counter.  This test is made to avoid new problems similar to the
25985  ** one described by ticket #3584.
25986  */
25987  unsigned char transCntrChng;   /* True if the transaction counter changed */
25988  unsigned char dbUpdate;        /* True if any part of database file changed */
25989  unsigned char inNormalWrite;   /* True if in a normal write operation */
25990
25991#endif
25992
25993#ifdef SQLITE_TEST
25994  /* In test mode, increase the size of this structure a bit so that
25995  ** it is larger than the struct CrashFile defined in test6.c.
25996  */
25997  char aPadding[32];
25998#endif
25999};
26000
26001/* This variable holds the process id (pid) from when the xRandomness()
26002** method was called.  If xOpen() is called from a different process id,
26003** indicating that a fork() has occurred, the PRNG will be reset.
26004*/
26005static pid_t randomnessPid = 0;
26006
26007/*
26008** Allowed values for the unixFile.ctrlFlags bitmask:
26009*/
26010#define UNIXFILE_EXCL        0x01     /* Connections from one process only */
26011#define UNIXFILE_RDONLY      0x02     /* Connection is read only */
26012#define UNIXFILE_PERSIST_WAL 0x04     /* Persistent WAL mode */
26013#ifndef SQLITE_DISABLE_DIRSYNC
26014# define UNIXFILE_DIRSYNC    0x08     /* Directory sync needed */
26015#else
26016# define UNIXFILE_DIRSYNC    0x00
26017#endif
26018#define UNIXFILE_PSOW        0x10     /* SQLITE_IOCAP_POWERSAFE_OVERWRITE */
26019#define UNIXFILE_DELETE      0x20     /* Delete on close */
26020#define UNIXFILE_URI         0x40     /* Filename might have query parameters */
26021#define UNIXFILE_NOLOCK      0x80     /* Do no file locking */
26022#define UNIXFILE_WARNED    0x0100     /* verifyDbFile() warnings issued */
26023#define UNIXFILE_BLOCK     0x0200     /* Next SHM lock might block */
26024
26025/*
26026** Include code that is common to all os_*.c files
26027*/
26028/************** Include os_common.h in the middle of os_unix.c ***************/
26029/************** Begin file os_common.h ***************************************/
26030/*
26031** 2004 May 22
26032**
26033** The author disclaims copyright to this source code.  In place of
26034** a legal notice, here is a blessing:
26035**
26036**    May you do good and not evil.
26037**    May you find forgiveness for yourself and forgive others.
26038**    May you share freely, never taking more than you give.
26039**
26040******************************************************************************
26041**
26042** This file contains macros and a little bit of code that is common to
26043** all of the platform-specific files (os_*.c) and is #included into those
26044** files.
26045**
26046** This file should be #included by the os_*.c files only.  It is not a
26047** general purpose header file.
26048*/
26049#ifndef _OS_COMMON_H_
26050#define _OS_COMMON_H_
26051
26052/*
26053** At least two bugs have slipped in because we changed the MEMORY_DEBUG
26054** macro to SQLITE_DEBUG and some older makefiles have not yet made the
26055** switch.  The following code should catch this problem at compile-time.
26056*/
26057#ifdef MEMORY_DEBUG
26058# error "The MEMORY_DEBUG macro is obsolete.  Use SQLITE_DEBUG instead."
26059#endif
26060
26061/*
26062** Macros for performance tracing.  Normally turned off.  Only works
26063** on i486 hardware.
26064*/
26065#ifdef SQLITE_PERFORMANCE_TRACE
26066
26067/*
26068** hwtime.h contains inline assembler code for implementing
26069** high-performance timing routines.
26070*/
26071/************** Include hwtime.h in the middle of os_common.h ****************/
26072/************** Begin file hwtime.h ******************************************/
26073/*
26074** 2008 May 27
26075**
26076** The author disclaims copyright to this source code.  In place of
26077** a legal notice, here is a blessing:
26078**
26079**    May you do good and not evil.
26080**    May you find forgiveness for yourself and forgive others.
26081**    May you share freely, never taking more than you give.
26082**
26083******************************************************************************
26084**
26085** This file contains inline asm code for retrieving "high-performance"
26086** counters for x86 class CPUs.
26087*/
26088#ifndef _HWTIME_H_
26089#define _HWTIME_H_
26090
26091/*
26092** The following routine only works on pentium-class (or newer) processors.
26093** It uses the RDTSC opcode to read the cycle count value out of the
26094** processor and returns that value.  This can be used for high-res
26095** profiling.
26096*/
26097#if (defined(__GNUC__) || defined(_MSC_VER)) && \
26098      (defined(i386) || defined(__i386__) || defined(_M_IX86))
26099
26100  #if defined(__GNUC__)
26101
26102  __inline__ sqlite_uint64 sqlite3Hwtime(void){
26103     unsigned int lo, hi;
26104     __asm__ __volatile__ ("rdtsc" : "=a" (lo), "=d" (hi));
26105     return (sqlite_uint64)hi << 32 | lo;
26106  }
26107
26108  #elif defined(_MSC_VER)
26109
26110  __declspec(naked) __inline sqlite_uint64 __cdecl sqlite3Hwtime(void){
26111     __asm {
26112        rdtsc
26113        ret       ; return value at EDX:EAX
26114     }
26115  }
26116
26117  #endif
26118
26119#elif (defined(__GNUC__) && defined(__x86_64__))
26120
26121  __inline__ sqlite_uint64 sqlite3Hwtime(void){
26122      unsigned long val;
26123      __asm__ __volatile__ ("rdtsc" : "=A" (val));
26124      return val;
26125  }
26126
26127#elif (defined(__GNUC__) && defined(__ppc__))
26128
26129  __inline__ sqlite_uint64 sqlite3Hwtime(void){
26130      unsigned long long retval;
26131      unsigned long junk;
26132      __asm__ __volatile__ ("\n\
26133          1:      mftbu   %1\n\
26134                  mftb    %L0\n\
26135                  mftbu   %0\n\
26136                  cmpw    %0,%1\n\
26137                  bne     1b"
26138                  : "=r" (retval), "=r" (junk));
26139      return retval;
26140  }
26141
26142#else
26143
26144  #error Need implementation of sqlite3Hwtime() for your platform.
26145
26146  /*
26147  ** To compile without implementing sqlite3Hwtime() for your platform,
26148  ** you can remove the above #error and use the following
26149  ** stub function.  You will lose timing support for many
26150  ** of the debugging and testing utilities, but it should at
26151  ** least compile and run.
26152  */
26153SQLITE_PRIVATE   sqlite_uint64 sqlite3Hwtime(void){ return ((sqlite_uint64)0); }
26154
26155#endif
26156
26157#endif /* !defined(_HWTIME_H_) */
26158
26159/************** End of hwtime.h **********************************************/
26160/************** Continuing where we left off in os_common.h ******************/
26161
26162static sqlite_uint64 g_start;
26163static sqlite_uint64 g_elapsed;
26164#define TIMER_START       g_start=sqlite3Hwtime()
26165#define TIMER_END         g_elapsed=sqlite3Hwtime()-g_start
26166#define TIMER_ELAPSED     g_elapsed
26167#else
26168#define TIMER_START
26169#define TIMER_END
26170#define TIMER_ELAPSED     ((sqlite_uint64)0)
26171#endif
26172
26173/*
26174** If we compile with the SQLITE_TEST macro set, then the following block
26175** of code will give us the ability to simulate a disk I/O error.  This
26176** is used for testing the I/O recovery logic.
26177*/
26178#ifdef SQLITE_TEST
26179SQLITE_API int sqlite3_io_error_hit = 0;            /* Total number of I/O Errors */
26180SQLITE_API int sqlite3_io_error_hardhit = 0;        /* Number of non-benign errors */
26181SQLITE_API int sqlite3_io_error_pending = 0;        /* Count down to first I/O error */
26182SQLITE_API int sqlite3_io_error_persist = 0;        /* True if I/O errors persist */
26183SQLITE_API int sqlite3_io_error_benign = 0;         /* True if errors are benign */
26184SQLITE_API int sqlite3_diskfull_pending = 0;
26185SQLITE_API int sqlite3_diskfull = 0;
26186#define SimulateIOErrorBenign(X) sqlite3_io_error_benign=(X)
26187#define SimulateIOError(CODE)  \
26188  if( (sqlite3_io_error_persist && sqlite3_io_error_hit) \
26189       || sqlite3_io_error_pending-- == 1 )  \
26190              { local_ioerr(); CODE; }
26191static void local_ioerr(){
26192  IOTRACE(("IOERR\n"));
26193  sqlite3_io_error_hit++;
26194  if( !sqlite3_io_error_benign ) sqlite3_io_error_hardhit++;
26195}
26196#define SimulateDiskfullError(CODE) \
26197   if( sqlite3_diskfull_pending ){ \
26198     if( sqlite3_diskfull_pending == 1 ){ \
26199       local_ioerr(); \
26200       sqlite3_diskfull = 1; \
26201       sqlite3_io_error_hit = 1; \
26202       CODE; \
26203     }else{ \
26204       sqlite3_diskfull_pending--; \
26205     } \
26206   }
26207#else
26208#define SimulateIOErrorBenign(X)
26209#define SimulateIOError(A)
26210#define SimulateDiskfullError(A)
26211#endif
26212
26213/*
26214** When testing, keep a count of the number of open files.
26215*/
26216#ifdef SQLITE_TEST
26217SQLITE_API int sqlite3_open_file_count = 0;
26218#define OpenCounter(X)  sqlite3_open_file_count+=(X)
26219#else
26220#define OpenCounter(X)
26221#endif
26222
26223#endif /* !defined(_OS_COMMON_H_) */
26224
26225/************** End of os_common.h *******************************************/
26226/************** Continuing where we left off in os_unix.c ********************/
26227
26228/*
26229** Define various macros that are missing from some systems.
26230*/
26231#ifndef O_LARGEFILE
26232# define O_LARGEFILE 0
26233#endif
26234#ifdef SQLITE_DISABLE_LFS
26235# undef O_LARGEFILE
26236# define O_LARGEFILE 0
26237#endif
26238#ifndef O_NOFOLLOW
26239# define O_NOFOLLOW 0
26240#endif
26241#ifndef O_BINARY
26242# define O_BINARY 0
26243#endif
26244
26245/*
26246** The threadid macro resolves to the thread-id or to 0.  Used for
26247** testing and debugging only.
26248*/
26249#if SQLITE_THREADSAFE
26250#define threadid pthread_self()
26251#else
26252#define threadid 0
26253#endif
26254
26255/*
26256** HAVE_MREMAP defaults to true on Linux and false everywhere else.
26257*/
26258#if !defined(HAVE_MREMAP)
26259# if defined(__linux__) && defined(_GNU_SOURCE)
26260#  define HAVE_MREMAP 1
26261# else
26262#  define HAVE_MREMAP 0
26263# endif
26264#endif
26265
26266/*
26267** Explicitly call the 64-bit version of lseek() on Android. Otherwise, lseek()
26268** is the 32-bit version, even if _FILE_OFFSET_BITS=64 is defined.
26269*/
26270#ifdef __ANDROID__
26271# define lseek lseek64
26272#endif
26273
26274/*
26275** Different Unix systems declare open() in different ways.  Same use
26276** open(const char*,int,mode_t).  Others use open(const char*,int,...).
26277** The difference is important when using a pointer to the function.
26278**
26279** The safest way to deal with the problem is to always use this wrapper
26280** which always has the same well-defined interface.
26281*/
26282static int posixOpen(const char *zFile, int flags, int mode){
26283  return open(zFile, flags, mode);
26284}
26285
26286/*
26287** On some systems, calls to fchown() will trigger a message in a security
26288** log if they come from non-root processes.  So avoid calling fchown() if
26289** we are not running as root.
26290*/
26291static int posixFchown(int fd, uid_t uid, gid_t gid){
26292#if OS_VXWORKS
26293  return 0;
26294#else
26295  return geteuid() ? 0 : fchown(fd,uid,gid);
26296#endif
26297}
26298
26299/* Forward reference */
26300static int openDirectory(const char*, int*);
26301static int unixGetpagesize(void);
26302
26303/*
26304** Many system calls are accessed through pointer-to-functions so that
26305** they may be overridden at runtime to facilitate fault injection during
26306** testing and sandboxing.  The following array holds the names and pointers
26307** to all overrideable system calls.
26308*/
26309static struct unix_syscall {
26310  const char *zName;            /* Name of the system call */
26311  sqlite3_syscall_ptr pCurrent; /* Current value of the system call */
26312  sqlite3_syscall_ptr pDefault; /* Default value */
26313} aSyscall[] = {
26314  { "open",         (sqlite3_syscall_ptr)posixOpen,  0  },
26315#define osOpen      ((int(*)(const char*,int,int))aSyscall[0].pCurrent)
26316
26317  { "close",        (sqlite3_syscall_ptr)close,      0  },
26318#define osClose     ((int(*)(int))aSyscall[1].pCurrent)
26319
26320  { "access",       (sqlite3_syscall_ptr)access,     0  },
26321#define osAccess    ((int(*)(const char*,int))aSyscall[2].pCurrent)
26322
26323  { "getcwd",       (sqlite3_syscall_ptr)getcwd,     0  },
26324#define osGetcwd    ((char*(*)(char*,size_t))aSyscall[3].pCurrent)
26325
26326  { "stat",         (sqlite3_syscall_ptr)stat,       0  },
26327#define osStat      ((int(*)(const char*,struct stat*))aSyscall[4].pCurrent)
26328
26329/*
26330** The DJGPP compiler environment looks mostly like Unix, but it
26331** lacks the fcntl() system call.  So redefine fcntl() to be something
26332** that always succeeds.  This means that locking does not occur under
26333** DJGPP.  But it is DOS - what did you expect?
26334*/
26335#ifdef __DJGPP__
26336  { "fstat",        0,                 0  },
26337#define osFstat(a,b,c)    0
26338#else
26339  { "fstat",        (sqlite3_syscall_ptr)fstat,      0  },
26340#define osFstat     ((int(*)(int,struct stat*))aSyscall[5].pCurrent)
26341#endif
26342
26343  { "ftruncate",    (sqlite3_syscall_ptr)ftruncate,  0  },
26344#define osFtruncate ((int(*)(int,off_t))aSyscall[6].pCurrent)
26345
26346  { "fcntl",        (sqlite3_syscall_ptr)fcntl,      0  },
26347#define osFcntl     ((int(*)(int,int,...))aSyscall[7].pCurrent)
26348
26349  { "read",         (sqlite3_syscall_ptr)read,       0  },
26350#define osRead      ((ssize_t(*)(int,void*,size_t))aSyscall[8].pCurrent)
26351
26352#if defined(USE_PREAD) || SQLITE_ENABLE_LOCKING_STYLE
26353  { "pread",        (sqlite3_syscall_ptr)pread,      0  },
26354#else
26355  { "pread",        (sqlite3_syscall_ptr)0,          0  },
26356#endif
26357#define osPread     ((ssize_t(*)(int,void*,size_t,off_t))aSyscall[9].pCurrent)
26358
26359#if defined(USE_PREAD64)
26360  { "pread64",      (sqlite3_syscall_ptr)pread64,    0  },
26361#else
26362  { "pread64",      (sqlite3_syscall_ptr)0,          0  },
26363#endif
26364#define osPread64   ((ssize_t(*)(int,void*,size_t,off_t))aSyscall[10].pCurrent)
26365
26366  { "write",        (sqlite3_syscall_ptr)write,      0  },
26367#define osWrite     ((ssize_t(*)(int,const void*,size_t))aSyscall[11].pCurrent)
26368
26369#if defined(USE_PREAD) || SQLITE_ENABLE_LOCKING_STYLE
26370  { "pwrite",       (sqlite3_syscall_ptr)pwrite,     0  },
26371#else
26372  { "pwrite",       (sqlite3_syscall_ptr)0,          0  },
26373#endif
26374#define osPwrite    ((ssize_t(*)(int,const void*,size_t,off_t))\
26375                    aSyscall[12].pCurrent)
26376
26377#if defined(USE_PREAD64)
26378  { "pwrite64",     (sqlite3_syscall_ptr)pwrite64,   0  },
26379#else
26380  { "pwrite64",     (sqlite3_syscall_ptr)0,          0  },
26381#endif
26382#define osPwrite64  ((ssize_t(*)(int,const void*,size_t,off_t))\
26383                    aSyscall[13].pCurrent)
26384
26385  { "fchmod",       (sqlite3_syscall_ptr)fchmod,     0  },
26386#define osFchmod    ((int(*)(int,mode_t))aSyscall[14].pCurrent)
26387
26388#if defined(HAVE_POSIX_FALLOCATE) && HAVE_POSIX_FALLOCATE
26389  { "fallocate",    (sqlite3_syscall_ptr)posix_fallocate,  0 },
26390#else
26391  { "fallocate",    (sqlite3_syscall_ptr)0,                0 },
26392#endif
26393#define osFallocate ((int(*)(int,off_t,off_t))aSyscall[15].pCurrent)
26394
26395  { "unlink",       (sqlite3_syscall_ptr)unlink,           0 },
26396#define osUnlink    ((int(*)(const char*))aSyscall[16].pCurrent)
26397
26398  { "openDirectory",    (sqlite3_syscall_ptr)openDirectory,      0 },
26399#define osOpenDirectory ((int(*)(const char*,int*))aSyscall[17].pCurrent)
26400
26401  { "mkdir",        (sqlite3_syscall_ptr)mkdir,           0 },
26402#define osMkdir     ((int(*)(const char*,mode_t))aSyscall[18].pCurrent)
26403
26404  { "rmdir",        (sqlite3_syscall_ptr)rmdir,           0 },
26405#define osRmdir     ((int(*)(const char*))aSyscall[19].pCurrent)
26406
26407  { "fchown",       (sqlite3_syscall_ptr)posixFchown,     0 },
26408#define osFchown    ((int(*)(int,uid_t,gid_t))aSyscall[20].pCurrent)
26409
26410#if !defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0
26411  { "mmap",       (sqlite3_syscall_ptr)mmap,     0 },
26412#define osMmap ((void*(*)(void*,size_t,int,int,int,off_t))aSyscall[21].pCurrent)
26413
26414  { "munmap",       (sqlite3_syscall_ptr)munmap,          0 },
26415#define osMunmap ((void*(*)(void*,size_t))aSyscall[22].pCurrent)
26416
26417#if HAVE_MREMAP
26418  { "mremap",       (sqlite3_syscall_ptr)mremap,          0 },
26419#else
26420  { "mremap",       (sqlite3_syscall_ptr)0,               0 },
26421#endif
26422#define osMremap ((void*(*)(void*,size_t,size_t,int,...))aSyscall[23].pCurrent)
26423  { "getpagesize",  (sqlite3_syscall_ptr)unixGetpagesize, 0 },
26424#define osGetpagesize ((int(*)(void))aSyscall[24].pCurrent)
26425
26426#endif
26427
26428}; /* End of the overrideable system calls */
26429
26430/*
26431** This is the xSetSystemCall() method of sqlite3_vfs for all of the
26432** "unix" VFSes.  Return SQLITE_OK opon successfully updating the
26433** system call pointer, or SQLITE_NOTFOUND if there is no configurable
26434** system call named zName.
26435*/
26436static int unixSetSystemCall(
26437  sqlite3_vfs *pNotUsed,        /* The VFS pointer.  Not used */
26438  const char *zName,            /* Name of system call to override */
26439  sqlite3_syscall_ptr pNewFunc  /* Pointer to new system call value */
26440){
26441  unsigned int i;
26442  int rc = SQLITE_NOTFOUND;
26443
26444  UNUSED_PARAMETER(pNotUsed);
26445  if( zName==0 ){
26446    /* If no zName is given, restore all system calls to their default
26447    ** settings and return NULL
26448    */
26449    rc = SQLITE_OK;
26450    for(i=0; i<sizeof(aSyscall)/sizeof(aSyscall[0]); i++){
26451      if( aSyscall[i].pDefault ){
26452        aSyscall[i].pCurrent = aSyscall[i].pDefault;
26453      }
26454    }
26455  }else{
26456    /* If zName is specified, operate on only the one system call
26457    ** specified.
26458    */
26459    for(i=0; i<sizeof(aSyscall)/sizeof(aSyscall[0]); i++){
26460      if( strcmp(zName, aSyscall[i].zName)==0 ){
26461        if( aSyscall[i].pDefault==0 ){
26462          aSyscall[i].pDefault = aSyscall[i].pCurrent;
26463        }
26464        rc = SQLITE_OK;
26465        if( pNewFunc==0 ) pNewFunc = aSyscall[i].pDefault;
26466        aSyscall[i].pCurrent = pNewFunc;
26467        break;
26468      }
26469    }
26470  }
26471  return rc;
26472}
26473
26474/*
26475** Return the value of a system call.  Return NULL if zName is not a
26476** recognized system call name.  NULL is also returned if the system call
26477** is currently undefined.
26478*/
26479static sqlite3_syscall_ptr unixGetSystemCall(
26480  sqlite3_vfs *pNotUsed,
26481  const char *zName
26482){
26483  unsigned int i;
26484
26485  UNUSED_PARAMETER(pNotUsed);
26486  for(i=0; i<sizeof(aSyscall)/sizeof(aSyscall[0]); i++){
26487    if( strcmp(zName, aSyscall[i].zName)==0 ) return aSyscall[i].pCurrent;
26488  }
26489  return 0;
26490}
26491
26492/*
26493** Return the name of the first system call after zName.  If zName==NULL
26494** then return the name of the first system call.  Return NULL if zName
26495** is the last system call or if zName is not the name of a valid
26496** system call.
26497*/
26498static const char *unixNextSystemCall(sqlite3_vfs *p, const char *zName){
26499  int i = -1;
26500
26501  UNUSED_PARAMETER(p);
26502  if( zName ){
26503    for(i=0; i<ArraySize(aSyscall)-1; i++){
26504      if( strcmp(zName, aSyscall[i].zName)==0 ) break;
26505    }
26506  }
26507  for(i++; i<ArraySize(aSyscall); i++){
26508    if( aSyscall[i].pCurrent!=0 ) return aSyscall[i].zName;
26509  }
26510  return 0;
26511}
26512
26513/*
26514** Do not accept any file descriptor less than this value, in order to avoid
26515** opening database file using file descriptors that are commonly used for
26516** standard input, output, and error.
26517*/
26518#ifndef SQLITE_MINIMUM_FILE_DESCRIPTOR
26519# define SQLITE_MINIMUM_FILE_DESCRIPTOR 3
26520#endif
26521
26522/*
26523** Invoke open().  Do so multiple times, until it either succeeds or
26524** fails for some reason other than EINTR.
26525**
26526** If the file creation mode "m" is 0 then set it to the default for
26527** SQLite.  The default is SQLITE_DEFAULT_FILE_PERMISSIONS (normally
26528** 0644) as modified by the system umask.  If m is not 0, then
26529** make the file creation mode be exactly m ignoring the umask.
26530**
26531** The m parameter will be non-zero only when creating -wal, -journal,
26532** and -shm files.  We want those files to have *exactly* the same
26533** permissions as their original database, unadulterated by the umask.
26534** In that way, if a database file is -rw-rw-rw or -rw-rw-r-, and a
26535** transaction crashes and leaves behind hot journals, then any
26536** process that is able to write to the database will also be able to
26537** recover the hot journals.
26538*/
26539static int robust_open(const char *z, int f, mode_t m){
26540  int fd;
26541  mode_t m2 = m ? m : SQLITE_DEFAULT_FILE_PERMISSIONS;
26542  while(1){
26543#if defined(O_CLOEXEC)
26544    fd = osOpen(z,f|O_CLOEXEC,m2);
26545#else
26546    fd = osOpen(z,f,m2);
26547#endif
26548    if( fd<0 ){
26549      if( errno==EINTR ) continue;
26550      break;
26551    }
26552    if( fd>=SQLITE_MINIMUM_FILE_DESCRIPTOR ) break;
26553    osClose(fd);
26554    sqlite3_log(SQLITE_WARNING,
26555                "attempt to open \"%s\" as file descriptor %d", z, fd);
26556    fd = -1;
26557    if( osOpen("/dev/null", f, m)<0 ) break;
26558  }
26559  if( fd>=0 ){
26560    if( m!=0 ){
26561      struct stat statbuf;
26562      if( osFstat(fd, &statbuf)==0
26563       && statbuf.st_size==0
26564       && (statbuf.st_mode&0777)!=m
26565      ){
26566        osFchmod(fd, m);
26567      }
26568    }
26569#if defined(FD_CLOEXEC) && (!defined(O_CLOEXEC) || O_CLOEXEC==0)
26570    osFcntl(fd, F_SETFD, osFcntl(fd, F_GETFD, 0) | FD_CLOEXEC);
26571#endif
26572  }
26573  return fd;
26574}
26575
26576/*
26577** Helper functions to obtain and relinquish the global mutex. The
26578** global mutex is used to protect the unixInodeInfo and
26579** vxworksFileId objects used by this file, all of which may be
26580** shared by multiple threads.
26581**
26582** Function unixMutexHeld() is used to assert() that the global mutex
26583** is held when required. This function is only used as part of assert()
26584** statements. e.g.
26585**
26586**   unixEnterMutex()
26587**     assert( unixMutexHeld() );
26588**   unixEnterLeave()
26589*/
26590static void unixEnterMutex(void){
26591  sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_VFS1));
26592}
26593static void unixLeaveMutex(void){
26594  sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_VFS1));
26595}
26596#ifdef SQLITE_DEBUG
26597static int unixMutexHeld(void) {
26598  return sqlite3_mutex_held(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_VFS1));
26599}
26600#endif
26601
26602
26603#ifdef SQLITE_HAVE_OS_TRACE
26604/*
26605** Helper function for printing out trace information from debugging
26606** binaries. This returns the string representation of the supplied
26607** integer lock-type.
26608*/
26609static const char *azFileLock(int eFileLock){
26610  switch( eFileLock ){
26611    case NO_LOCK: return "NONE";
26612    case SHARED_LOCK: return "SHARED";
26613    case RESERVED_LOCK: return "RESERVED";
26614    case PENDING_LOCK: return "PENDING";
26615    case EXCLUSIVE_LOCK: return "EXCLUSIVE";
26616  }
26617  return "ERROR";
26618}
26619#endif
26620
26621#ifdef SQLITE_LOCK_TRACE
26622/*
26623** Print out information about all locking operations.
26624**
26625** This routine is used for troubleshooting locks on multithreaded
26626** platforms.  Enable by compiling with the -DSQLITE_LOCK_TRACE
26627** command-line option on the compiler.  This code is normally
26628** turned off.
26629*/
26630static int lockTrace(int fd, int op, struct flock *p){
26631  char *zOpName, *zType;
26632  int s;
26633  int savedErrno;
26634  if( op==F_GETLK ){
26635    zOpName = "GETLK";
26636  }else if( op==F_SETLK ){
26637    zOpName = "SETLK";
26638  }else{
26639    s = osFcntl(fd, op, p);
26640    sqlite3DebugPrintf("fcntl unknown %d %d %d\n", fd, op, s);
26641    return s;
26642  }
26643  if( p->l_type==F_RDLCK ){
26644    zType = "RDLCK";
26645  }else if( p->l_type==F_WRLCK ){
26646    zType = "WRLCK";
26647  }else if( p->l_type==F_UNLCK ){
26648    zType = "UNLCK";
26649  }else{
26650    assert( 0 );
26651  }
26652  assert( p->l_whence==SEEK_SET );
26653  s = osFcntl(fd, op, p);
26654  savedErrno = errno;
26655  sqlite3DebugPrintf("fcntl %d %d %s %s %d %d %d %d\n",
26656     threadid, fd, zOpName, zType, (int)p->l_start, (int)p->l_len,
26657     (int)p->l_pid, s);
26658  if( s==(-1) && op==F_SETLK && (p->l_type==F_RDLCK || p->l_type==F_WRLCK) ){
26659    struct flock l2;
26660    l2 = *p;
26661    osFcntl(fd, F_GETLK, &l2);
26662    if( l2.l_type==F_RDLCK ){
26663      zType = "RDLCK";
26664    }else if( l2.l_type==F_WRLCK ){
26665      zType = "WRLCK";
26666    }else if( l2.l_type==F_UNLCK ){
26667      zType = "UNLCK";
26668    }else{
26669      assert( 0 );
26670    }
26671    sqlite3DebugPrintf("fcntl-failure-reason: %s %d %d %d\n",
26672       zType, (int)l2.l_start, (int)l2.l_len, (int)l2.l_pid);
26673  }
26674  errno = savedErrno;
26675  return s;
26676}
26677#undef osFcntl
26678#define osFcntl lockTrace
26679#endif /* SQLITE_LOCK_TRACE */
26680
26681/*
26682** Retry ftruncate() calls that fail due to EINTR
26683**
26684** All calls to ftruncate() within this file should be made through
26685** this wrapper.  On the Android platform, bypassing the logic below
26686** could lead to a corrupt database.
26687*/
26688static int robust_ftruncate(int h, sqlite3_int64 sz){
26689  int rc;
26690#ifdef __ANDROID__
26691  /* On Android, ftruncate() always uses 32-bit offsets, even if
26692  ** _FILE_OFFSET_BITS=64 is defined. This means it is unsafe to attempt to
26693  ** truncate a file to any size larger than 2GiB. Silently ignore any
26694  ** such attempts.  */
26695  if( sz>(sqlite3_int64)0x7FFFFFFF ){
26696    rc = SQLITE_OK;
26697  }else
26698#endif
26699  do{ rc = osFtruncate(h,sz); }while( rc<0 && errno==EINTR );
26700  return rc;
26701}
26702
26703/*
26704** This routine translates a standard POSIX errno code into something
26705** useful to the clients of the sqlite3 functions.  Specifically, it is
26706** intended to translate a variety of "try again" errors into SQLITE_BUSY
26707** and a variety of "please close the file descriptor NOW" errors into
26708** SQLITE_IOERR
26709**
26710** Errors during initialization of locks, or file system support for locks,
26711** should handle ENOLCK, ENOTSUP, EOPNOTSUPP separately.
26712*/
26713static int sqliteErrorFromPosixError(int posixError, int sqliteIOErr) {
26714  switch (posixError) {
26715#if 0
26716  /* At one point this code was not commented out. In theory, this branch
26717  ** should never be hit, as this function should only be called after
26718  ** a locking-related function (i.e. fcntl()) has returned non-zero with
26719  ** the value of errno as the first argument. Since a system call has failed,
26720  ** errno should be non-zero.
26721  **
26722  ** Despite this, if errno really is zero, we still don't want to return
26723  ** SQLITE_OK. The system call failed, and *some* SQLite error should be
26724  ** propagated back to the caller. Commenting this branch out means errno==0
26725  ** will be handled by the "default:" case below.
26726  */
26727  case 0:
26728    return SQLITE_OK;
26729#endif
26730
26731  case EAGAIN:
26732  case ETIMEDOUT:
26733  case EBUSY:
26734  case EINTR:
26735  case ENOLCK:
26736    /* random NFS retry error, unless during file system support
26737     * introspection, in which it actually means what it says */
26738    return SQLITE_BUSY;
26739
26740  case EACCES:
26741    /* EACCES is like EAGAIN during locking operations, but not any other time*/
26742    if( (sqliteIOErr == SQLITE_IOERR_LOCK) ||
26743        (sqliteIOErr == SQLITE_IOERR_UNLOCK) ||
26744        (sqliteIOErr == SQLITE_IOERR_RDLOCK) ||
26745        (sqliteIOErr == SQLITE_IOERR_CHECKRESERVEDLOCK) ){
26746      return SQLITE_BUSY;
26747    }
26748    /* else fall through */
26749  case EPERM:
26750    return SQLITE_PERM;
26751
26752#if EOPNOTSUPP!=ENOTSUP
26753  case EOPNOTSUPP:
26754    /* something went terribly awry, unless during file system support
26755     * introspection, in which it actually means what it says */
26756#endif
26757#ifdef ENOTSUP
26758  case ENOTSUP:
26759    /* invalid fd, unless during file system support introspection, in which
26760     * it actually means what it says */
26761#endif
26762  case EIO:
26763  case EBADF:
26764  case EINVAL:
26765  case ENOTCONN:
26766  case ENODEV:
26767  case ENXIO:
26768  case ENOENT:
26769#ifdef ESTALE                     /* ESTALE is not defined on Interix systems */
26770  case ESTALE:
26771#endif
26772  case ENOSYS:
26773    /* these should force the client to close the file and reconnect */
26774
26775  default:
26776    return sqliteIOErr;
26777  }
26778}
26779
26780
26781/******************************************************************************
26782****************** Begin Unique File ID Utility Used By VxWorks ***************
26783**
26784** On most versions of unix, we can get a unique ID for a file by concatenating
26785** the device number and the inode number.  But this does not work on VxWorks.
26786** On VxWorks, a unique file id must be based on the canonical filename.
26787**
26788** A pointer to an instance of the following structure can be used as a
26789** unique file ID in VxWorks.  Each instance of this structure contains
26790** a copy of the canonical filename.  There is also a reference count.
26791** The structure is reclaimed when the number of pointers to it drops to
26792** zero.
26793**
26794** There are never very many files open at one time and lookups are not
26795** a performance-critical path, so it is sufficient to put these
26796** structures on a linked list.
26797*/
26798struct vxworksFileId {
26799  struct vxworksFileId *pNext;  /* Next in a list of them all */
26800  int nRef;                     /* Number of references to this one */
26801  int nName;                    /* Length of the zCanonicalName[] string */
26802  char *zCanonicalName;         /* Canonical filename */
26803};
26804
26805#if OS_VXWORKS
26806/*
26807** All unique filenames are held on a linked list headed by this
26808** variable:
26809*/
26810static struct vxworksFileId *vxworksFileList = 0;
26811
26812/*
26813** Simplify a filename into its canonical form
26814** by making the following changes:
26815**
26816**  * removing any trailing and duplicate /
26817**  * convert /./ into just /
26818**  * convert /A/../ where A is any simple name into just /
26819**
26820** Changes are made in-place.  Return the new name length.
26821**
26822** The original filename is in z[0..n-1].  Return the number of
26823** characters in the simplified name.
26824*/
26825static int vxworksSimplifyName(char *z, int n){
26826  int i, j;
26827  while( n>1 && z[n-1]=='/' ){ n--; }
26828  for(i=j=0; i<n; i++){
26829    if( z[i]=='/' ){
26830      if( z[i+1]=='/' ) continue;
26831      if( z[i+1]=='.' && i+2<n && z[i+2]=='/' ){
26832        i += 1;
26833        continue;
26834      }
26835      if( z[i+1]=='.' && i+3<n && z[i+2]=='.' && z[i+3]=='/' ){
26836        while( j>0 && z[j-1]!='/' ){ j--; }
26837        if( j>0 ){ j--; }
26838        i += 2;
26839        continue;
26840      }
26841    }
26842    z[j++] = z[i];
26843  }
26844  z[j] = 0;
26845  return j;
26846}
26847
26848/*
26849** Find a unique file ID for the given absolute pathname.  Return
26850** a pointer to the vxworksFileId object.  This pointer is the unique
26851** file ID.
26852**
26853** The nRef field of the vxworksFileId object is incremented before
26854** the object is returned.  A new vxworksFileId object is created
26855** and added to the global list if necessary.
26856**
26857** If a memory allocation error occurs, return NULL.
26858*/
26859static struct vxworksFileId *vxworksFindFileId(const char *zAbsoluteName){
26860  struct vxworksFileId *pNew;         /* search key and new file ID */
26861  struct vxworksFileId *pCandidate;   /* For looping over existing file IDs */
26862  int n;                              /* Length of zAbsoluteName string */
26863
26864  assert( zAbsoluteName[0]=='/' );
26865  n = (int)strlen(zAbsoluteName);
26866  pNew = sqlite3_malloc64( sizeof(*pNew) + (n+1) );
26867  if( pNew==0 ) return 0;
26868  pNew->zCanonicalName = (char*)&pNew[1];
26869  memcpy(pNew->zCanonicalName, zAbsoluteName, n+1);
26870  n = vxworksSimplifyName(pNew->zCanonicalName, n);
26871
26872  /* Search for an existing entry that matching the canonical name.
26873  ** If found, increment the reference count and return a pointer to
26874  ** the existing file ID.
26875  */
26876  unixEnterMutex();
26877  for(pCandidate=vxworksFileList; pCandidate; pCandidate=pCandidate->pNext){
26878    if( pCandidate->nName==n
26879     && memcmp(pCandidate->zCanonicalName, pNew->zCanonicalName, n)==0
26880    ){
26881       sqlite3_free(pNew);
26882       pCandidate->nRef++;
26883       unixLeaveMutex();
26884       return pCandidate;
26885    }
26886  }
26887
26888  /* No match was found.  We will make a new file ID */
26889  pNew->nRef = 1;
26890  pNew->nName = n;
26891  pNew->pNext = vxworksFileList;
26892  vxworksFileList = pNew;
26893  unixLeaveMutex();
26894  return pNew;
26895}
26896
26897/*
26898** Decrement the reference count on a vxworksFileId object.  Free
26899** the object when the reference count reaches zero.
26900*/
26901static void vxworksReleaseFileId(struct vxworksFileId *pId){
26902  unixEnterMutex();
26903  assert( pId->nRef>0 );
26904  pId->nRef--;
26905  if( pId->nRef==0 ){
26906    struct vxworksFileId **pp;
26907    for(pp=&vxworksFileList; *pp && *pp!=pId; pp = &((*pp)->pNext)){}
26908    assert( *pp==pId );
26909    *pp = pId->pNext;
26910    sqlite3_free(pId);
26911  }
26912  unixLeaveMutex();
26913}
26914#endif /* OS_VXWORKS */
26915/*************** End of Unique File ID Utility Used By VxWorks ****************
26916******************************************************************************/
26917
26918
26919/******************************************************************************
26920*************************** Posix Advisory Locking ****************************
26921**
26922** POSIX advisory locks are broken by design.  ANSI STD 1003.1 (1996)
26923** section 6.5.2.2 lines 483 through 490 specify that when a process
26924** sets or clears a lock, that operation overrides any prior locks set
26925** by the same process.  It does not explicitly say so, but this implies
26926** that it overrides locks set by the same process using a different
26927** file descriptor.  Consider this test case:
26928**
26929**       int fd1 = open("./file1", O_RDWR|O_CREAT, 0644);
26930**       int fd2 = open("./file2", O_RDWR|O_CREAT, 0644);
26931**
26932** Suppose ./file1 and ./file2 are really the same file (because
26933** one is a hard or symbolic link to the other) then if you set
26934** an exclusive lock on fd1, then try to get an exclusive lock
26935** on fd2, it works.  I would have expected the second lock to
26936** fail since there was already a lock on the file due to fd1.
26937** But not so.  Since both locks came from the same process, the
26938** second overrides the first, even though they were on different
26939** file descriptors opened on different file names.
26940**
26941** This means that we cannot use POSIX locks to synchronize file access
26942** among competing threads of the same process.  POSIX locks will work fine
26943** to synchronize access for threads in separate processes, but not
26944** threads within the same process.
26945**
26946** To work around the problem, SQLite has to manage file locks internally
26947** on its own.  Whenever a new database is opened, we have to find the
26948** specific inode of the database file (the inode is determined by the
26949** st_dev and st_ino fields of the stat structure that fstat() fills in)
26950** and check for locks already existing on that inode.  When locks are
26951** created or removed, we have to look at our own internal record of the
26952** locks to see if another thread has previously set a lock on that same
26953** inode.
26954**
26955** (Aside: The use of inode numbers as unique IDs does not work on VxWorks.
26956** For VxWorks, we have to use the alternative unique ID system based on
26957** canonical filename and implemented in the previous division.)
26958**
26959** The sqlite3_file structure for POSIX is no longer just an integer file
26960** descriptor.  It is now a structure that holds the integer file
26961** descriptor and a pointer to a structure that describes the internal
26962** locks on the corresponding inode.  There is one locking structure
26963** per inode, so if the same inode is opened twice, both unixFile structures
26964** point to the same locking structure.  The locking structure keeps
26965** a reference count (so we will know when to delete it) and a "cnt"
26966** field that tells us its internal lock status.  cnt==0 means the
26967** file is unlocked.  cnt==-1 means the file has an exclusive lock.
26968** cnt>0 means there are cnt shared locks on the file.
26969**
26970** Any attempt to lock or unlock a file first checks the locking
26971** structure.  The fcntl() system call is only invoked to set a
26972** POSIX lock if the internal lock structure transitions between
26973** a locked and an unlocked state.
26974**
26975** But wait:  there are yet more problems with POSIX advisory locks.
26976**
26977** If you close a file descriptor that points to a file that has locks,
26978** all locks on that file that are owned by the current process are
26979** released.  To work around this problem, each unixInodeInfo object
26980** maintains a count of the number of pending locks on tha inode.
26981** When an attempt is made to close an unixFile, if there are
26982** other unixFile open on the same inode that are holding locks, the call
26983** to close() the file descriptor is deferred until all of the locks clear.
26984** The unixInodeInfo structure keeps a list of file descriptors that need to
26985** be closed and that list is walked (and cleared) when the last lock
26986** clears.
26987**
26988** Yet another problem:  LinuxThreads do not play well with posix locks.
26989**
26990** Many older versions of linux use the LinuxThreads library which is
26991** not posix compliant.  Under LinuxThreads, a lock created by thread
26992** A cannot be modified or overridden by a different thread B.
26993** Only thread A can modify the lock.  Locking behavior is correct
26994** if the appliation uses the newer Native Posix Thread Library (NPTL)
26995** on linux - with NPTL a lock created by thread A can override locks
26996** in thread B.  But there is no way to know at compile-time which
26997** threading library is being used.  So there is no way to know at
26998** compile-time whether or not thread A can override locks on thread B.
26999** One has to do a run-time check to discover the behavior of the
27000** current process.
27001**
27002** SQLite used to support LinuxThreads.  But support for LinuxThreads
27003** was dropped beginning with version 3.7.0.  SQLite will still work with
27004** LinuxThreads provided that (1) there is no more than one connection
27005** per database file in the same process and (2) database connections
27006** do not move across threads.
27007*/
27008
27009/*
27010** An instance of the following structure serves as the key used
27011** to locate a particular unixInodeInfo object.
27012*/
27013struct unixFileId {
27014  dev_t dev;                  /* Device number */
27015#if OS_VXWORKS
27016  struct vxworksFileId *pId;  /* Unique file ID for vxworks. */
27017#else
27018  ino_t ino;                  /* Inode number */
27019#endif
27020};
27021
27022/*
27023** An instance of the following structure is allocated for each open
27024** inode.  Or, on LinuxThreads, there is one of these structures for
27025** each inode opened by each thread.
27026**
27027** A single inode can have multiple file descriptors, so each unixFile
27028** structure contains a pointer to an instance of this object and this
27029** object keeps a count of the number of unixFile pointing to it.
27030*/
27031struct unixInodeInfo {
27032  struct unixFileId fileId;       /* The lookup key */
27033  int nShared;                    /* Number of SHARED locks held */
27034  unsigned char eFileLock;        /* One of SHARED_LOCK, RESERVED_LOCK etc. */
27035  unsigned char bProcessLock;     /* An exclusive process lock is held */
27036  int nRef;                       /* Number of pointers to this structure */
27037  unixShmNode *pShmNode;          /* Shared memory associated with this inode */
27038  int nLock;                      /* Number of outstanding file locks */
27039  UnixUnusedFd *pUnused;          /* Unused file descriptors to close */
27040  unixInodeInfo *pNext;           /* List of all unixInodeInfo objects */
27041  unixInodeInfo *pPrev;           /*    .... doubly linked */
27042#if SQLITE_ENABLE_LOCKING_STYLE
27043  unsigned long long sharedByte;  /* for AFP simulated shared lock */
27044#endif
27045#if OS_VXWORKS
27046  sem_t *pSem;                    /* Named POSIX semaphore */
27047  char aSemName[MAX_PATHNAME+2];  /* Name of that semaphore */
27048#endif
27049};
27050
27051/*
27052** A lists of all unixInodeInfo objects.
27053*/
27054static unixInodeInfo *inodeList = 0;
27055
27056/*
27057**
27058** This function - unixLogError_x(), is only ever called via the macro
27059** unixLogError().
27060**
27061** It is invoked after an error occurs in an OS function and errno has been
27062** set. It logs a message using sqlite3_log() containing the current value of
27063** errno and, if possible, the human-readable equivalent from strerror() or
27064** strerror_r().
27065**
27066** The first argument passed to the macro should be the error code that
27067** will be returned to SQLite (e.g. SQLITE_IOERR_DELETE, SQLITE_CANTOPEN).
27068** The two subsequent arguments should be the name of the OS function that
27069** failed (e.g. "unlink", "open") and the associated file-system path,
27070** if any.
27071*/
27072#define unixLogError(a,b,c)     unixLogErrorAtLine(a,b,c,__LINE__)
27073static int unixLogErrorAtLine(
27074  int errcode,                    /* SQLite error code */
27075  const char *zFunc,              /* Name of OS function that failed */
27076  const char *zPath,              /* File path associated with error */
27077  int iLine                       /* Source line number where error occurred */
27078){
27079  char *zErr;                     /* Message from strerror() or equivalent */
27080  int iErrno = errno;             /* Saved syscall error number */
27081
27082  /* If this is not a threadsafe build (SQLITE_THREADSAFE==0), then use
27083  ** the strerror() function to obtain the human-readable error message
27084  ** equivalent to errno. Otherwise, use strerror_r().
27085  */
27086#if SQLITE_THREADSAFE && defined(HAVE_STRERROR_R)
27087  char aErr[80];
27088  memset(aErr, 0, sizeof(aErr));
27089  zErr = aErr;
27090
27091  /* If STRERROR_R_CHAR_P (set by autoconf scripts) or __USE_GNU is defined,
27092  ** assume that the system provides the GNU version of strerror_r() that
27093  ** returns a pointer to a buffer containing the error message. That pointer
27094  ** may point to aErr[], or it may point to some static storage somewhere.
27095  ** Otherwise, assume that the system provides the POSIX version of
27096  ** strerror_r(), which always writes an error message into aErr[].
27097  **
27098  ** If the code incorrectly assumes that it is the POSIX version that is
27099  ** available, the error message will often be an empty string. Not a
27100  ** huge problem. Incorrectly concluding that the GNU version is available
27101  ** could lead to a segfault though.
27102  */
27103#if defined(STRERROR_R_CHAR_P) || defined(__USE_GNU)
27104  zErr =
27105# endif
27106  strerror_r(iErrno, aErr, sizeof(aErr)-1);
27107
27108#elif SQLITE_THREADSAFE
27109  /* This is a threadsafe build, but strerror_r() is not available. */
27110  zErr = "";
27111#else
27112  /* Non-threadsafe build, use strerror(). */
27113  zErr = strerror(iErrno);
27114#endif
27115
27116  if( zPath==0 ) zPath = "";
27117  sqlite3_log(errcode,
27118      "os_unix.c:%d: (%d) %s(%s) - %s",
27119      iLine, iErrno, zFunc, zPath, zErr
27120  );
27121
27122  return errcode;
27123}
27124
27125/*
27126** Close a file descriptor.
27127**
27128** We assume that close() almost always works, since it is only in a
27129** very sick application or on a very sick platform that it might fail.
27130** If it does fail, simply leak the file descriptor, but do log the
27131** error.
27132**
27133** Note that it is not safe to retry close() after EINTR since the
27134** file descriptor might have already been reused by another thread.
27135** So we don't even try to recover from an EINTR.  Just log the error
27136** and move on.
27137*/
27138static void robust_close(unixFile *pFile, int h, int lineno){
27139  if( osClose(h) ){
27140    unixLogErrorAtLine(SQLITE_IOERR_CLOSE, "close",
27141                       pFile ? pFile->zPath : 0, lineno);
27142  }
27143}
27144
27145/*
27146** Set the pFile->lastErrno.  Do this in a subroutine as that provides
27147** a convenient place to set a breakpoint.
27148*/
27149static void storeLastErrno(unixFile *pFile, int error){
27150  pFile->lastErrno = error;
27151}
27152
27153/*
27154** Close all file descriptors accumuated in the unixInodeInfo->pUnused list.
27155*/
27156static void closePendingFds(unixFile *pFile){
27157  unixInodeInfo *pInode = pFile->pInode;
27158  UnixUnusedFd *p;
27159  UnixUnusedFd *pNext;
27160  for(p=pInode->pUnused; p; p=pNext){
27161    pNext = p->pNext;
27162    robust_close(pFile, p->fd, __LINE__);
27163    sqlite3_free(p);
27164  }
27165  pInode->pUnused = 0;
27166}
27167
27168/*
27169** Release a unixInodeInfo structure previously allocated by findInodeInfo().
27170**
27171** The mutex entered using the unixEnterMutex() function must be held
27172** when this function is called.
27173*/
27174static void releaseInodeInfo(unixFile *pFile){
27175  unixInodeInfo *pInode = pFile->pInode;
27176  assert( unixMutexHeld() );
27177  if( ALWAYS(pInode) ){
27178    pInode->nRef--;
27179    if( pInode->nRef==0 ){
27180      assert( pInode->pShmNode==0 );
27181      closePendingFds(pFile);
27182      if( pInode->pPrev ){
27183        assert( pInode->pPrev->pNext==pInode );
27184        pInode->pPrev->pNext = pInode->pNext;
27185      }else{
27186        assert( inodeList==pInode );
27187        inodeList = pInode->pNext;
27188      }
27189      if( pInode->pNext ){
27190        assert( pInode->pNext->pPrev==pInode );
27191        pInode->pNext->pPrev = pInode->pPrev;
27192      }
27193      sqlite3_free(pInode);
27194    }
27195  }
27196}
27197
27198/*
27199** Given a file descriptor, locate the unixInodeInfo object that
27200** describes that file descriptor.  Create a new one if necessary.  The
27201** return value might be uninitialized if an error occurs.
27202**
27203** The mutex entered using the unixEnterMutex() function must be held
27204** when this function is called.
27205**
27206** Return an appropriate error code.
27207*/
27208static int findInodeInfo(
27209  unixFile *pFile,               /* Unix file with file desc used in the key */
27210  unixInodeInfo **ppInode        /* Return the unixInodeInfo object here */
27211){
27212  int rc;                        /* System call return code */
27213  int fd;                        /* The file descriptor for pFile */
27214  struct unixFileId fileId;      /* Lookup key for the unixInodeInfo */
27215  struct stat statbuf;           /* Low-level file information */
27216  unixInodeInfo *pInode = 0;     /* Candidate unixInodeInfo object */
27217
27218  assert( unixMutexHeld() );
27219
27220  /* Get low-level information about the file that we can used to
27221  ** create a unique name for the file.
27222  */
27223  fd = pFile->h;
27224  rc = osFstat(fd, &statbuf);
27225  if( rc!=0 ){
27226    storeLastErrno(pFile, errno);
27227#ifdef EOVERFLOW
27228    if( pFile->lastErrno==EOVERFLOW ) return SQLITE_NOLFS;
27229#endif
27230    return SQLITE_IOERR;
27231  }
27232
27233#ifdef __APPLE__
27234  /* On OS X on an msdos filesystem, the inode number is reported
27235  ** incorrectly for zero-size files.  See ticket #3260.  To work
27236  ** around this problem (we consider it a bug in OS X, not SQLite)
27237  ** we always increase the file size to 1 by writing a single byte
27238  ** prior to accessing the inode number.  The one byte written is
27239  ** an ASCII 'S' character which also happens to be the first byte
27240  ** in the header of every SQLite database.  In this way, if there
27241  ** is a race condition such that another thread has already populated
27242  ** the first page of the database, no damage is done.
27243  */
27244  if( statbuf.st_size==0 && (pFile->fsFlags & SQLITE_FSFLAGS_IS_MSDOS)!=0 ){
27245    do{ rc = osWrite(fd, "S", 1); }while( rc<0 && errno==EINTR );
27246    if( rc!=1 ){
27247      storeLastErrno(pFile, errno);
27248      return SQLITE_IOERR;
27249    }
27250    rc = osFstat(fd, &statbuf);
27251    if( rc!=0 ){
27252      storeLastErrno(pFile, errno);
27253      return SQLITE_IOERR;
27254    }
27255  }
27256#endif
27257
27258  memset(&fileId, 0, sizeof(fileId));
27259  fileId.dev = statbuf.st_dev;
27260#if OS_VXWORKS
27261  fileId.pId = pFile->pId;
27262#else
27263  fileId.ino = statbuf.st_ino;
27264#endif
27265  pInode = inodeList;
27266  while( pInode && memcmp(&fileId, &pInode->fileId, sizeof(fileId)) ){
27267    pInode = pInode->pNext;
27268  }
27269  if( pInode==0 ){
27270    pInode = sqlite3_malloc64( sizeof(*pInode) );
27271    if( pInode==0 ){
27272      return SQLITE_NOMEM;
27273    }
27274    memset(pInode, 0, sizeof(*pInode));
27275    memcpy(&pInode->fileId, &fileId, sizeof(fileId));
27276    pInode->nRef = 1;
27277    pInode->pNext = inodeList;
27278    pInode->pPrev = 0;
27279    if( inodeList ) inodeList->pPrev = pInode;
27280    inodeList = pInode;
27281  }else{
27282    pInode->nRef++;
27283  }
27284  *ppInode = pInode;
27285  return SQLITE_OK;
27286}
27287
27288/*
27289** Return TRUE if pFile has been renamed or unlinked since it was first opened.
27290*/
27291static int fileHasMoved(unixFile *pFile){
27292#if OS_VXWORKS
27293  return pFile->pInode!=0 && pFile->pId!=pFile->pInode->fileId.pId;
27294#else
27295  struct stat buf;
27296  return pFile->pInode!=0 &&
27297      (osStat(pFile->zPath, &buf)!=0 || buf.st_ino!=pFile->pInode->fileId.ino);
27298#endif
27299}
27300
27301
27302/*
27303** Check a unixFile that is a database.  Verify the following:
27304**
27305** (1) There is exactly one hard link on the file
27306** (2) The file is not a symbolic link
27307** (3) The file has not been renamed or unlinked
27308**
27309** Issue sqlite3_log(SQLITE_WARNING,...) messages if anything is not right.
27310*/
27311static void verifyDbFile(unixFile *pFile){
27312  struct stat buf;
27313  int rc;
27314  if( pFile->ctrlFlags & UNIXFILE_WARNED ){
27315    /* One or more of the following warnings have already been issued.  Do not
27316    ** repeat them so as not to clutter the error log */
27317    return;
27318  }
27319  rc = osFstat(pFile->h, &buf);
27320  if( rc!=0 ){
27321    sqlite3_log(SQLITE_WARNING, "cannot fstat db file %s", pFile->zPath);
27322    pFile->ctrlFlags |= UNIXFILE_WARNED;
27323    return;
27324  }
27325  if( buf.st_nlink==0 && (pFile->ctrlFlags & UNIXFILE_DELETE)==0 ){
27326    sqlite3_log(SQLITE_WARNING, "file unlinked while open: %s", pFile->zPath);
27327    pFile->ctrlFlags |= UNIXFILE_WARNED;
27328    return;
27329  }
27330  if( buf.st_nlink>1 ){
27331    sqlite3_log(SQLITE_WARNING, "multiple links to file: %s", pFile->zPath);
27332    pFile->ctrlFlags |= UNIXFILE_WARNED;
27333    return;
27334  }
27335  if( fileHasMoved(pFile) ){
27336    sqlite3_log(SQLITE_WARNING, "file renamed while open: %s", pFile->zPath);
27337    pFile->ctrlFlags |= UNIXFILE_WARNED;
27338    return;
27339  }
27340}
27341
27342
27343/*
27344** This routine checks if there is a RESERVED lock held on the specified
27345** file by this or any other process. If such a lock is held, set *pResOut
27346** to a non-zero value otherwise *pResOut is set to zero.  The return value
27347** is set to SQLITE_OK unless an I/O error occurs during lock checking.
27348*/
27349static int unixCheckReservedLock(sqlite3_file *id, int *pResOut){
27350  int rc = SQLITE_OK;
27351  int reserved = 0;
27352  unixFile *pFile = (unixFile*)id;
27353
27354  SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; );
27355
27356  assert( pFile );
27357  unixEnterMutex(); /* Because pFile->pInode is shared across threads */
27358
27359  /* Check if a thread in this process holds such a lock */
27360  if( pFile->pInode->eFileLock>SHARED_LOCK ){
27361    reserved = 1;
27362  }
27363
27364  /* Otherwise see if some other process holds it.
27365  */
27366#ifndef __DJGPP__
27367  if( !reserved && !pFile->pInode->bProcessLock ){
27368    struct flock lock;
27369    lock.l_whence = SEEK_SET;
27370    lock.l_start = RESERVED_BYTE;
27371    lock.l_len = 1;
27372    lock.l_type = F_WRLCK;
27373    if( osFcntl(pFile->h, F_GETLK, &lock) ){
27374      rc = SQLITE_IOERR_CHECKRESERVEDLOCK;
27375      storeLastErrno(pFile, errno);
27376    } else if( lock.l_type!=F_UNLCK ){
27377      reserved = 1;
27378    }
27379  }
27380#endif
27381
27382  unixLeaveMutex();
27383  OSTRACE(("TEST WR-LOCK %d %d %d (unix)\n", pFile->h, rc, reserved));
27384
27385  *pResOut = reserved;
27386  return rc;
27387}
27388
27389/*
27390** Attempt to set a system-lock on the file pFile.  The lock is
27391** described by pLock.
27392**
27393** If the pFile was opened read/write from unix-excl, then the only lock
27394** ever obtained is an exclusive lock, and it is obtained exactly once
27395** the first time any lock is attempted.  All subsequent system locking
27396** operations become no-ops.  Locking operations still happen internally,
27397** in order to coordinate access between separate database connections
27398** within this process, but all of that is handled in memory and the
27399** operating system does not participate.
27400**
27401** This function is a pass-through to fcntl(F_SETLK) if pFile is using
27402** any VFS other than "unix-excl" or if pFile is opened on "unix-excl"
27403** and is read-only.
27404**
27405** Zero is returned if the call completes successfully, or -1 if a call
27406** to fcntl() fails. In this case, errno is set appropriately (by fcntl()).
27407*/
27408static int unixFileLock(unixFile *pFile, struct flock *pLock){
27409  int rc;
27410  unixInodeInfo *pInode = pFile->pInode;
27411  assert( unixMutexHeld() );
27412  assert( pInode!=0 );
27413  if( ((pFile->ctrlFlags & UNIXFILE_EXCL)!=0 || pInode->bProcessLock)
27414   && ((pFile->ctrlFlags & UNIXFILE_RDONLY)==0)
27415  ){
27416    if( pInode->bProcessLock==0 ){
27417      struct flock lock;
27418      assert( pInode->nLock==0 );
27419      lock.l_whence = SEEK_SET;
27420      lock.l_start = SHARED_FIRST;
27421      lock.l_len = SHARED_SIZE;
27422      lock.l_type = F_WRLCK;
27423      rc = osFcntl(pFile->h, F_SETLK, &lock);
27424      if( rc<0 ) return rc;
27425      pInode->bProcessLock = 1;
27426      pInode->nLock++;
27427    }else{
27428      rc = 0;
27429    }
27430  }else{
27431    rc = osFcntl(pFile->h, F_SETLK, pLock);
27432  }
27433  return rc;
27434}
27435
27436/*
27437** Lock the file with the lock specified by parameter eFileLock - one
27438** of the following:
27439**
27440**     (1) SHARED_LOCK
27441**     (2) RESERVED_LOCK
27442**     (3) PENDING_LOCK
27443**     (4) EXCLUSIVE_LOCK
27444**
27445** Sometimes when requesting one lock state, additional lock states
27446** are inserted in between.  The locking might fail on one of the later
27447** transitions leaving the lock state different from what it started but
27448** still short of its goal.  The following chart shows the allowed
27449** transitions and the inserted intermediate states:
27450**
27451**    UNLOCKED -> SHARED
27452**    SHARED -> RESERVED
27453**    SHARED -> (PENDING) -> EXCLUSIVE
27454**    RESERVED -> (PENDING) -> EXCLUSIVE
27455**    PENDING -> EXCLUSIVE
27456**
27457** This routine will only increase a lock.  Use the sqlite3OsUnlock()
27458** routine to lower a locking level.
27459*/
27460static int unixLock(sqlite3_file *id, int eFileLock){
27461  /* The following describes the implementation of the various locks and
27462  ** lock transitions in terms of the POSIX advisory shared and exclusive
27463  ** lock primitives (called read-locks and write-locks below, to avoid
27464  ** confusion with SQLite lock names). The algorithms are complicated
27465  ** slightly in order to be compatible with windows systems simultaneously
27466  ** accessing the same database file, in case that is ever required.
27467  **
27468  ** Symbols defined in os.h indentify the 'pending byte' and the 'reserved
27469  ** byte', each single bytes at well known offsets, and the 'shared byte
27470  ** range', a range of 510 bytes at a well known offset.
27471  **
27472  ** To obtain a SHARED lock, a read-lock is obtained on the 'pending
27473  ** byte'.  If this is successful, a random byte from the 'shared byte
27474  ** range' is read-locked and the lock on the 'pending byte' released.
27475  **
27476  ** A process may only obtain a RESERVED lock after it has a SHARED lock.
27477  ** A RESERVED lock is implemented by grabbing a write-lock on the
27478  ** 'reserved byte'.
27479  **
27480  ** A process may only obtain a PENDING lock after it has obtained a
27481  ** SHARED lock. A PENDING lock is implemented by obtaining a write-lock
27482  ** on the 'pending byte'. This ensures that no new SHARED locks can be
27483  ** obtained, but existing SHARED locks are allowed to persist. A process
27484  ** does not have to obtain a RESERVED lock on the way to a PENDING lock.
27485  ** This property is used by the algorithm for rolling back a journal file
27486  ** after a crash.
27487  **
27488  ** An EXCLUSIVE lock, obtained after a PENDING lock is held, is
27489  ** implemented by obtaining a write-lock on the entire 'shared byte
27490  ** range'. Since all other locks require a read-lock on one of the bytes
27491  ** within this range, this ensures that no other locks are held on the
27492  ** database.
27493  **
27494  ** The reason a single byte cannot be used instead of the 'shared byte
27495  ** range' is that some versions of windows do not support read-locks. By
27496  ** locking a random byte from a range, concurrent SHARED locks may exist
27497  ** even if the locking primitive used is always a write-lock.
27498  */
27499  int rc = SQLITE_OK;
27500  unixFile *pFile = (unixFile*)id;
27501  unixInodeInfo *pInode;
27502  struct flock lock;
27503  int tErrno = 0;
27504
27505  assert( pFile );
27506  OSTRACE(("LOCK    %d %s was %s(%s,%d) pid=%d (unix)\n", pFile->h,
27507      azFileLock(eFileLock), azFileLock(pFile->eFileLock),
27508      azFileLock(pFile->pInode->eFileLock), pFile->pInode->nShared,
27509      osGetpid(0)));
27510
27511  /* If there is already a lock of this type or more restrictive on the
27512  ** unixFile, do nothing. Don't use the end_lock: exit path, as
27513  ** unixEnterMutex() hasn't been called yet.
27514  */
27515  if( pFile->eFileLock>=eFileLock ){
27516    OSTRACE(("LOCK    %d %s ok (already held) (unix)\n", pFile->h,
27517            azFileLock(eFileLock)));
27518    return SQLITE_OK;
27519  }
27520
27521  /* Make sure the locking sequence is correct.
27522  **  (1) We never move from unlocked to anything higher than shared lock.
27523  **  (2) SQLite never explicitly requests a pendig lock.
27524  **  (3) A shared lock is always held when a reserve lock is requested.
27525  */
27526  assert( pFile->eFileLock!=NO_LOCK || eFileLock==SHARED_LOCK );
27527  assert( eFileLock!=PENDING_LOCK );
27528  assert( eFileLock!=RESERVED_LOCK || pFile->eFileLock==SHARED_LOCK );
27529
27530  /* This mutex is needed because pFile->pInode is shared across threads
27531  */
27532  unixEnterMutex();
27533  pInode = pFile->pInode;
27534
27535  /* If some thread using this PID has a lock via a different unixFile*
27536  ** handle that precludes the requested lock, return BUSY.
27537  */
27538  if( (pFile->eFileLock!=pInode->eFileLock &&
27539          (pInode->eFileLock>=PENDING_LOCK || eFileLock>SHARED_LOCK))
27540  ){
27541    rc = SQLITE_BUSY;
27542    goto end_lock;
27543  }
27544
27545  /* If a SHARED lock is requested, and some thread using this PID already
27546  ** has a SHARED or RESERVED lock, then increment reference counts and
27547  ** return SQLITE_OK.
27548  */
27549  if( eFileLock==SHARED_LOCK &&
27550      (pInode->eFileLock==SHARED_LOCK || pInode->eFileLock==RESERVED_LOCK) ){
27551    assert( eFileLock==SHARED_LOCK );
27552    assert( pFile->eFileLock==0 );
27553    assert( pInode->nShared>0 );
27554    pFile->eFileLock = SHARED_LOCK;
27555    pInode->nShared++;
27556    pInode->nLock++;
27557    goto end_lock;
27558  }
27559
27560
27561  /* A PENDING lock is needed before acquiring a SHARED lock and before
27562  ** acquiring an EXCLUSIVE lock.  For the SHARED lock, the PENDING will
27563  ** be released.
27564  */
27565  lock.l_len = 1L;
27566  lock.l_whence = SEEK_SET;
27567  if( eFileLock==SHARED_LOCK
27568      || (eFileLock==EXCLUSIVE_LOCK && pFile->eFileLock<PENDING_LOCK)
27569  ){
27570    lock.l_type = (eFileLock==SHARED_LOCK?F_RDLCK:F_WRLCK);
27571    lock.l_start = PENDING_BYTE;
27572    if( unixFileLock(pFile, &lock) ){
27573      tErrno = errno;
27574      rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK);
27575      if( rc!=SQLITE_BUSY ){
27576        storeLastErrno(pFile, tErrno);
27577      }
27578      goto end_lock;
27579    }
27580  }
27581
27582
27583  /* If control gets to this point, then actually go ahead and make
27584  ** operating system calls for the specified lock.
27585  */
27586  if( eFileLock==SHARED_LOCK ){
27587    assert( pInode->nShared==0 );
27588    assert( pInode->eFileLock==0 );
27589    assert( rc==SQLITE_OK );
27590
27591    /* Now get the read-lock */
27592    lock.l_start = SHARED_FIRST;
27593    lock.l_len = SHARED_SIZE;
27594    if( unixFileLock(pFile, &lock) ){
27595      tErrno = errno;
27596      rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK);
27597    }
27598
27599    /* Drop the temporary PENDING lock */
27600    lock.l_start = PENDING_BYTE;
27601    lock.l_len = 1L;
27602    lock.l_type = F_UNLCK;
27603    if( unixFileLock(pFile, &lock) && rc==SQLITE_OK ){
27604      /* This could happen with a network mount */
27605      tErrno = errno;
27606      rc = SQLITE_IOERR_UNLOCK;
27607    }
27608
27609    if( rc ){
27610      if( rc!=SQLITE_BUSY ){
27611        storeLastErrno(pFile, tErrno);
27612      }
27613      goto end_lock;
27614    }else{
27615      pFile->eFileLock = SHARED_LOCK;
27616      pInode->nLock++;
27617      pInode->nShared = 1;
27618    }
27619  }else if( eFileLock==EXCLUSIVE_LOCK && pInode->nShared>1 ){
27620    /* We are trying for an exclusive lock but another thread in this
27621    ** same process is still holding a shared lock. */
27622    rc = SQLITE_BUSY;
27623  }else{
27624    /* The request was for a RESERVED or EXCLUSIVE lock.  It is
27625    ** assumed that there is a SHARED or greater lock on the file
27626    ** already.
27627    */
27628    assert( 0!=pFile->eFileLock );
27629    lock.l_type = F_WRLCK;
27630
27631    assert( eFileLock==RESERVED_LOCK || eFileLock==EXCLUSIVE_LOCK );
27632    if( eFileLock==RESERVED_LOCK ){
27633      lock.l_start = RESERVED_BYTE;
27634      lock.l_len = 1L;
27635    }else{
27636      lock.l_start = SHARED_FIRST;
27637      lock.l_len = SHARED_SIZE;
27638    }
27639
27640    if( unixFileLock(pFile, &lock) ){
27641      tErrno = errno;
27642      rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK);
27643      if( rc!=SQLITE_BUSY ){
27644        storeLastErrno(pFile, tErrno);
27645      }
27646    }
27647  }
27648
27649
27650#ifdef SQLITE_DEBUG
27651  /* Set up the transaction-counter change checking flags when
27652  ** transitioning from a SHARED to a RESERVED lock.  The change
27653  ** from SHARED to RESERVED marks the beginning of a normal
27654  ** write operation (not a hot journal rollback).
27655  */
27656  if( rc==SQLITE_OK
27657   && pFile->eFileLock<=SHARED_LOCK
27658   && eFileLock==RESERVED_LOCK
27659  ){
27660    pFile->transCntrChng = 0;
27661    pFile->dbUpdate = 0;
27662    pFile->inNormalWrite = 1;
27663  }
27664#endif
27665
27666
27667  if( rc==SQLITE_OK ){
27668    pFile->eFileLock = eFileLock;
27669    pInode->eFileLock = eFileLock;
27670  }else if( eFileLock==EXCLUSIVE_LOCK ){
27671    pFile->eFileLock = PENDING_LOCK;
27672    pInode->eFileLock = PENDING_LOCK;
27673  }
27674
27675end_lock:
27676  unixLeaveMutex();
27677  OSTRACE(("LOCK    %d %s %s (unix)\n", pFile->h, azFileLock(eFileLock),
27678      rc==SQLITE_OK ? "ok" : "failed"));
27679  return rc;
27680}
27681
27682/*
27683** Add the file descriptor used by file handle pFile to the corresponding
27684** pUnused list.
27685*/
27686static void setPendingFd(unixFile *pFile){
27687  unixInodeInfo *pInode = pFile->pInode;
27688  UnixUnusedFd *p = pFile->pUnused;
27689  p->pNext = pInode->pUnused;
27690  pInode->pUnused = p;
27691  pFile->h = -1;
27692  pFile->pUnused = 0;
27693}
27694
27695/*
27696** Lower the locking level on file descriptor pFile to eFileLock.  eFileLock
27697** must be either NO_LOCK or SHARED_LOCK.
27698**
27699** If the locking level of the file descriptor is already at or below
27700** the requested locking level, this routine is a no-op.
27701**
27702** If handleNFSUnlock is true, then on downgrading an EXCLUSIVE_LOCK to SHARED
27703** the byte range is divided into 2 parts and the first part is unlocked then
27704** set to a read lock, then the other part is simply unlocked.  This works
27705** around a bug in BSD NFS lockd (also seen on MacOSX 10.3+) that fails to
27706** remove the write lock on a region when a read lock is set.
27707*/
27708static int posixUnlock(sqlite3_file *id, int eFileLock, int handleNFSUnlock){
27709  unixFile *pFile = (unixFile*)id;
27710  unixInodeInfo *pInode;
27711  struct flock lock;
27712  int rc = SQLITE_OK;
27713
27714  assert( pFile );
27715  OSTRACE(("UNLOCK  %d %d was %d(%d,%d) pid=%d (unix)\n", pFile->h, eFileLock,
27716      pFile->eFileLock, pFile->pInode->eFileLock, pFile->pInode->nShared,
27717      osGetpid(0)));
27718
27719  assert( eFileLock<=SHARED_LOCK );
27720  if( pFile->eFileLock<=eFileLock ){
27721    return SQLITE_OK;
27722  }
27723  unixEnterMutex();
27724  pInode = pFile->pInode;
27725  assert( pInode->nShared!=0 );
27726  if( pFile->eFileLock>SHARED_LOCK ){
27727    assert( pInode->eFileLock==pFile->eFileLock );
27728
27729#ifdef SQLITE_DEBUG
27730    /* When reducing a lock such that other processes can start
27731    ** reading the database file again, make sure that the
27732    ** transaction counter was updated if any part of the database
27733    ** file changed.  If the transaction counter is not updated,
27734    ** other connections to the same file might not realize that
27735    ** the file has changed and hence might not know to flush their
27736    ** cache.  The use of a stale cache can lead to database corruption.
27737    */
27738    pFile->inNormalWrite = 0;
27739#endif
27740
27741    /* downgrading to a shared lock on NFS involves clearing the write lock
27742    ** before establishing the readlock - to avoid a race condition we downgrade
27743    ** the lock in 2 blocks, so that part of the range will be covered by a
27744    ** write lock until the rest is covered by a read lock:
27745    **  1:   [WWWWW]
27746    **  2:   [....W]
27747    **  3:   [RRRRW]
27748    **  4:   [RRRR.]
27749    */
27750    if( eFileLock==SHARED_LOCK ){
27751#if !defined(__APPLE__) || !SQLITE_ENABLE_LOCKING_STYLE
27752      (void)handleNFSUnlock;
27753      assert( handleNFSUnlock==0 );
27754#endif
27755#if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
27756      if( handleNFSUnlock ){
27757        int tErrno;               /* Error code from system call errors */
27758        off_t divSize = SHARED_SIZE - 1;
27759
27760        lock.l_type = F_UNLCK;
27761        lock.l_whence = SEEK_SET;
27762        lock.l_start = SHARED_FIRST;
27763        lock.l_len = divSize;
27764        if( unixFileLock(pFile, &lock)==(-1) ){
27765          tErrno = errno;
27766          rc = SQLITE_IOERR_UNLOCK;
27767          if( IS_LOCK_ERROR(rc) ){
27768            storeLastErrno(pFile, tErrno);
27769          }
27770          goto end_unlock;
27771        }
27772        lock.l_type = F_RDLCK;
27773        lock.l_whence = SEEK_SET;
27774        lock.l_start = SHARED_FIRST;
27775        lock.l_len = divSize;
27776        if( unixFileLock(pFile, &lock)==(-1) ){
27777          tErrno = errno;
27778          rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_RDLOCK);
27779          if( IS_LOCK_ERROR(rc) ){
27780            storeLastErrno(pFile, tErrno);
27781          }
27782          goto end_unlock;
27783        }
27784        lock.l_type = F_UNLCK;
27785        lock.l_whence = SEEK_SET;
27786        lock.l_start = SHARED_FIRST+divSize;
27787        lock.l_len = SHARED_SIZE-divSize;
27788        if( unixFileLock(pFile, &lock)==(-1) ){
27789          tErrno = errno;
27790          rc = SQLITE_IOERR_UNLOCK;
27791          if( IS_LOCK_ERROR(rc) ){
27792            storeLastErrno(pFile, tErrno);
27793          }
27794          goto end_unlock;
27795        }
27796      }else
27797#endif /* defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE */
27798      {
27799        lock.l_type = F_RDLCK;
27800        lock.l_whence = SEEK_SET;
27801        lock.l_start = SHARED_FIRST;
27802        lock.l_len = SHARED_SIZE;
27803        if( unixFileLock(pFile, &lock) ){
27804          /* In theory, the call to unixFileLock() cannot fail because another
27805          ** process is holding an incompatible lock. If it does, this
27806          ** indicates that the other process is not following the locking
27807          ** protocol. If this happens, return SQLITE_IOERR_RDLOCK. Returning
27808          ** SQLITE_BUSY would confuse the upper layer (in practice it causes
27809          ** an assert to fail). */
27810          rc = SQLITE_IOERR_RDLOCK;
27811          storeLastErrno(pFile, errno);
27812          goto end_unlock;
27813        }
27814      }
27815    }
27816    lock.l_type = F_UNLCK;
27817    lock.l_whence = SEEK_SET;
27818    lock.l_start = PENDING_BYTE;
27819    lock.l_len = 2L;  assert( PENDING_BYTE+1==RESERVED_BYTE );
27820    if( unixFileLock(pFile, &lock)==0 ){
27821      pInode->eFileLock = SHARED_LOCK;
27822    }else{
27823      rc = SQLITE_IOERR_UNLOCK;
27824      storeLastErrno(pFile, errno);
27825      goto end_unlock;
27826    }
27827  }
27828  if( eFileLock==NO_LOCK ){
27829    /* Decrement the shared lock counter.  Release the lock using an
27830    ** OS call only when all threads in this same process have released
27831    ** the lock.
27832    */
27833    pInode->nShared--;
27834    if( pInode->nShared==0 ){
27835      lock.l_type = F_UNLCK;
27836      lock.l_whence = SEEK_SET;
27837      lock.l_start = lock.l_len = 0L;
27838      if( unixFileLock(pFile, &lock)==0 ){
27839        pInode->eFileLock = NO_LOCK;
27840      }else{
27841        rc = SQLITE_IOERR_UNLOCK;
27842        storeLastErrno(pFile, errno);
27843        pInode->eFileLock = NO_LOCK;
27844        pFile->eFileLock = NO_LOCK;
27845      }
27846    }
27847
27848    /* Decrement the count of locks against this same file.  When the
27849    ** count reaches zero, close any other file descriptors whose close
27850    ** was deferred because of outstanding locks.
27851    */
27852    pInode->nLock--;
27853    assert( pInode->nLock>=0 );
27854    if( pInode->nLock==0 ){
27855      closePendingFds(pFile);
27856    }
27857  }
27858
27859end_unlock:
27860  unixLeaveMutex();
27861  if( rc==SQLITE_OK ) pFile->eFileLock = eFileLock;
27862  return rc;
27863}
27864
27865/*
27866** Lower the locking level on file descriptor pFile to eFileLock.  eFileLock
27867** must be either NO_LOCK or SHARED_LOCK.
27868**
27869** If the locking level of the file descriptor is already at or below
27870** the requested locking level, this routine is a no-op.
27871*/
27872static int unixUnlock(sqlite3_file *id, int eFileLock){
27873#if SQLITE_MAX_MMAP_SIZE>0
27874  assert( eFileLock==SHARED_LOCK || ((unixFile *)id)->nFetchOut==0 );
27875#endif
27876  return posixUnlock(id, eFileLock, 0);
27877}
27878
27879#if SQLITE_MAX_MMAP_SIZE>0
27880static int unixMapfile(unixFile *pFd, i64 nByte);
27881static void unixUnmapfile(unixFile *pFd);
27882#endif
27883
27884/*
27885** This function performs the parts of the "close file" operation
27886** common to all locking schemes. It closes the directory and file
27887** handles, if they are valid, and sets all fields of the unixFile
27888** structure to 0.
27889**
27890** It is *not* necessary to hold the mutex when this routine is called,
27891** even on VxWorks.  A mutex will be acquired on VxWorks by the
27892** vxworksReleaseFileId() routine.
27893*/
27894static int closeUnixFile(sqlite3_file *id){
27895  unixFile *pFile = (unixFile*)id;
27896#if SQLITE_MAX_MMAP_SIZE>0
27897  unixUnmapfile(pFile);
27898#endif
27899  if( pFile->h>=0 ){
27900    robust_close(pFile, pFile->h, __LINE__);
27901    pFile->h = -1;
27902  }
27903#if OS_VXWORKS
27904  if( pFile->pId ){
27905    if( pFile->ctrlFlags & UNIXFILE_DELETE ){
27906      osUnlink(pFile->pId->zCanonicalName);
27907    }
27908    vxworksReleaseFileId(pFile->pId);
27909    pFile->pId = 0;
27910  }
27911#endif
27912#ifdef SQLITE_UNLINK_AFTER_CLOSE
27913  if( pFile->ctrlFlags & UNIXFILE_DELETE ){
27914    osUnlink(pFile->zPath);
27915    sqlite3_free(*(char**)&pFile->zPath);
27916    pFile->zPath = 0;
27917  }
27918#endif
27919  OSTRACE(("CLOSE   %-3d\n", pFile->h));
27920  OpenCounter(-1);
27921  sqlite3_free(pFile->pUnused);
27922  memset(pFile, 0, sizeof(unixFile));
27923  return SQLITE_OK;
27924}
27925
27926/*
27927** Close a file.
27928*/
27929static int unixClose(sqlite3_file *id){
27930  int rc = SQLITE_OK;
27931  unixFile *pFile = (unixFile *)id;
27932  verifyDbFile(pFile);
27933  unixUnlock(id, NO_LOCK);
27934  unixEnterMutex();
27935
27936  /* unixFile.pInode is always valid here. Otherwise, a different close
27937  ** routine (e.g. nolockClose()) would be called instead.
27938  */
27939  assert( pFile->pInode->nLock>0 || pFile->pInode->bProcessLock==0 );
27940  if( ALWAYS(pFile->pInode) && pFile->pInode->nLock ){
27941    /* If there are outstanding locks, do not actually close the file just
27942    ** yet because that would clear those locks.  Instead, add the file
27943    ** descriptor to pInode->pUnused list.  It will be automatically closed
27944    ** when the last lock is cleared.
27945    */
27946    setPendingFd(pFile);
27947  }
27948  releaseInodeInfo(pFile);
27949  rc = closeUnixFile(id);
27950  unixLeaveMutex();
27951  return rc;
27952}
27953
27954/************** End of the posix advisory lock implementation *****************
27955******************************************************************************/
27956
27957/******************************************************************************
27958****************************** No-op Locking **********************************
27959**
27960** Of the various locking implementations available, this is by far the
27961** simplest:  locking is ignored.  No attempt is made to lock the database
27962** file for reading or writing.
27963**
27964** This locking mode is appropriate for use on read-only databases
27965** (ex: databases that are burned into CD-ROM, for example.)  It can
27966** also be used if the application employs some external mechanism to
27967** prevent simultaneous access of the same database by two or more
27968** database connections.  But there is a serious risk of database
27969** corruption if this locking mode is used in situations where multiple
27970** database connections are accessing the same database file at the same
27971** time and one or more of those connections are writing.
27972*/
27973
27974static int nolockCheckReservedLock(sqlite3_file *NotUsed, int *pResOut){
27975  UNUSED_PARAMETER(NotUsed);
27976  *pResOut = 0;
27977  return SQLITE_OK;
27978}
27979static int nolockLock(sqlite3_file *NotUsed, int NotUsed2){
27980  UNUSED_PARAMETER2(NotUsed, NotUsed2);
27981  return SQLITE_OK;
27982}
27983static int nolockUnlock(sqlite3_file *NotUsed, int NotUsed2){
27984  UNUSED_PARAMETER2(NotUsed, NotUsed2);
27985  return SQLITE_OK;
27986}
27987
27988/*
27989** Close the file.
27990*/
27991static int nolockClose(sqlite3_file *id) {
27992  return closeUnixFile(id);
27993}
27994
27995/******************* End of the no-op lock implementation *********************
27996******************************************************************************/
27997
27998/******************************************************************************
27999************************* Begin dot-file Locking ******************************
28000**
28001** The dotfile locking implementation uses the existence of separate lock
28002** files (really a directory) to control access to the database.  This works
28003** on just about every filesystem imaginable.  But there are serious downsides:
28004**
28005**    (1)  There is zero concurrency.  A single reader blocks all other
28006**         connections from reading or writing the database.
28007**
28008**    (2)  An application crash or power loss can leave stale lock files
28009**         sitting around that need to be cleared manually.
28010**
28011** Nevertheless, a dotlock is an appropriate locking mode for use if no
28012** other locking strategy is available.
28013**
28014** Dotfile locking works by creating a subdirectory in the same directory as
28015** the database and with the same name but with a ".lock" extension added.
28016** The existence of a lock directory implies an EXCLUSIVE lock.  All other
28017** lock types (SHARED, RESERVED, PENDING) are mapped into EXCLUSIVE.
28018*/
28019
28020/*
28021** The file suffix added to the data base filename in order to create the
28022** lock directory.
28023*/
28024#define DOTLOCK_SUFFIX ".lock"
28025
28026/*
28027** This routine checks if there is a RESERVED lock held on the specified
28028** file by this or any other process. If such a lock is held, set *pResOut
28029** to a non-zero value otherwise *pResOut is set to zero.  The return value
28030** is set to SQLITE_OK unless an I/O error occurs during lock checking.
28031**
28032** In dotfile locking, either a lock exists or it does not.  So in this
28033** variation of CheckReservedLock(), *pResOut is set to true if any lock
28034** is held on the file and false if the file is unlocked.
28035*/
28036static int dotlockCheckReservedLock(sqlite3_file *id, int *pResOut) {
28037  int rc = SQLITE_OK;
28038  int reserved = 0;
28039  unixFile *pFile = (unixFile*)id;
28040
28041  SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; );
28042
28043  assert( pFile );
28044
28045  /* Check if a thread in this process holds such a lock */
28046  if( pFile->eFileLock>SHARED_LOCK ){
28047    /* Either this connection or some other connection in the same process
28048    ** holds a lock on the file.  No need to check further. */
28049    reserved = 1;
28050  }else{
28051    /* The lock is held if and only if the lockfile exists */
28052    const char *zLockFile = (const char*)pFile->lockingContext;
28053    reserved = osAccess(zLockFile, 0)==0;
28054  }
28055  OSTRACE(("TEST WR-LOCK %d %d %d (dotlock)\n", pFile->h, rc, reserved));
28056  *pResOut = reserved;
28057  return rc;
28058}
28059
28060/*
28061** Lock the file with the lock specified by parameter eFileLock - one
28062** of the following:
28063**
28064**     (1) SHARED_LOCK
28065**     (2) RESERVED_LOCK
28066**     (3) PENDING_LOCK
28067**     (4) EXCLUSIVE_LOCK
28068**
28069** Sometimes when requesting one lock state, additional lock states
28070** are inserted in between.  The locking might fail on one of the later
28071** transitions leaving the lock state different from what it started but
28072** still short of its goal.  The following chart shows the allowed
28073** transitions and the inserted intermediate states:
28074**
28075**    UNLOCKED -> SHARED
28076**    SHARED -> RESERVED
28077**    SHARED -> (PENDING) -> EXCLUSIVE
28078**    RESERVED -> (PENDING) -> EXCLUSIVE
28079**    PENDING -> EXCLUSIVE
28080**
28081** This routine will only increase a lock.  Use the sqlite3OsUnlock()
28082** routine to lower a locking level.
28083**
28084** With dotfile locking, we really only support state (4): EXCLUSIVE.
28085** But we track the other locking levels internally.
28086*/
28087static int dotlockLock(sqlite3_file *id, int eFileLock) {
28088  unixFile *pFile = (unixFile*)id;
28089  char *zLockFile = (char *)pFile->lockingContext;
28090  int rc = SQLITE_OK;
28091
28092
28093  /* If we have any lock, then the lock file already exists.  All we have
28094  ** to do is adjust our internal record of the lock level.
28095  */
28096  if( pFile->eFileLock > NO_LOCK ){
28097    pFile->eFileLock = eFileLock;
28098    /* Always update the timestamp on the old file */
28099#ifdef HAVE_UTIME
28100    utime(zLockFile, NULL);
28101#else
28102    utimes(zLockFile, NULL);
28103#endif
28104    return SQLITE_OK;
28105  }
28106
28107  /* grab an exclusive lock */
28108  rc = osMkdir(zLockFile, 0777);
28109  if( rc<0 ){
28110    /* failed to open/create the lock directory */
28111    int tErrno = errno;
28112    if( EEXIST == tErrno ){
28113      rc = SQLITE_BUSY;
28114    } else {
28115      rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK);
28116      if( IS_LOCK_ERROR(rc) ){
28117        storeLastErrno(pFile, tErrno);
28118      }
28119    }
28120    return rc;
28121  }
28122
28123  /* got it, set the type and return ok */
28124  pFile->eFileLock = eFileLock;
28125  return rc;
28126}
28127
28128/*
28129** Lower the locking level on file descriptor pFile to eFileLock.  eFileLock
28130** must be either NO_LOCK or SHARED_LOCK.
28131**
28132** If the locking level of the file descriptor is already at or below
28133** the requested locking level, this routine is a no-op.
28134**
28135** When the locking level reaches NO_LOCK, delete the lock file.
28136*/
28137static int dotlockUnlock(sqlite3_file *id, int eFileLock) {
28138  unixFile *pFile = (unixFile*)id;
28139  char *zLockFile = (char *)pFile->lockingContext;
28140  int rc;
28141
28142  assert( pFile );
28143  OSTRACE(("UNLOCK  %d %d was %d pid=%d (dotlock)\n", pFile->h, eFileLock,
28144           pFile->eFileLock, osGetpid(0)));
28145  assert( eFileLock<=SHARED_LOCK );
28146
28147  /* no-op if possible */
28148  if( pFile->eFileLock==eFileLock ){
28149    return SQLITE_OK;
28150  }
28151
28152  /* To downgrade to shared, simply update our internal notion of the
28153  ** lock state.  No need to mess with the file on disk.
28154  */
28155  if( eFileLock==SHARED_LOCK ){
28156    pFile->eFileLock = SHARED_LOCK;
28157    return SQLITE_OK;
28158  }
28159
28160  /* To fully unlock the database, delete the lock file */
28161  assert( eFileLock==NO_LOCK );
28162  rc = osRmdir(zLockFile);
28163  if( rc<0 && errno==ENOTDIR ) rc = osUnlink(zLockFile);
28164  if( rc<0 ){
28165    int tErrno = errno;
28166    rc = 0;
28167    if( ENOENT != tErrno ){
28168      rc = SQLITE_IOERR_UNLOCK;
28169    }
28170    if( IS_LOCK_ERROR(rc) ){
28171      storeLastErrno(pFile, tErrno);
28172    }
28173    return rc;
28174  }
28175  pFile->eFileLock = NO_LOCK;
28176  return SQLITE_OK;
28177}
28178
28179/*
28180** Close a file.  Make sure the lock has been released before closing.
28181*/
28182static int dotlockClose(sqlite3_file *id) {
28183  int rc = SQLITE_OK;
28184  if( id ){
28185    unixFile *pFile = (unixFile*)id;
28186    dotlockUnlock(id, NO_LOCK);
28187    sqlite3_free(pFile->lockingContext);
28188    rc = closeUnixFile(id);
28189  }
28190  return rc;
28191}
28192/****************** End of the dot-file lock implementation *******************
28193******************************************************************************/
28194
28195/******************************************************************************
28196************************** Begin flock Locking ********************************
28197**
28198** Use the flock() system call to do file locking.
28199**
28200** flock() locking is like dot-file locking in that the various
28201** fine-grain locking levels supported by SQLite are collapsed into
28202** a single exclusive lock.  In other words, SHARED, RESERVED, and
28203** PENDING locks are the same thing as an EXCLUSIVE lock.  SQLite
28204** still works when you do this, but concurrency is reduced since
28205** only a single process can be reading the database at a time.
28206**
28207** Omit this section if SQLITE_ENABLE_LOCKING_STYLE is turned off
28208*/
28209#if SQLITE_ENABLE_LOCKING_STYLE
28210
28211/*
28212** Retry flock() calls that fail with EINTR
28213*/
28214#ifdef EINTR
28215static int robust_flock(int fd, int op){
28216  int rc;
28217  do{ rc = flock(fd,op); }while( rc<0 && errno==EINTR );
28218  return rc;
28219}
28220#else
28221# define robust_flock(a,b) flock(a,b)
28222#endif
28223
28224
28225/*
28226** This routine checks if there is a RESERVED lock held on the specified
28227** file by this or any other process. If such a lock is held, set *pResOut
28228** to a non-zero value otherwise *pResOut is set to zero.  The return value
28229** is set to SQLITE_OK unless an I/O error occurs during lock checking.
28230*/
28231static int flockCheckReservedLock(sqlite3_file *id, int *pResOut){
28232  int rc = SQLITE_OK;
28233  int reserved = 0;
28234  unixFile *pFile = (unixFile*)id;
28235
28236  SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; );
28237
28238  assert( pFile );
28239
28240  /* Check if a thread in this process holds such a lock */
28241  if( pFile->eFileLock>SHARED_LOCK ){
28242    reserved = 1;
28243  }
28244
28245  /* Otherwise see if some other process holds it. */
28246  if( !reserved ){
28247    /* attempt to get the lock */
28248    int lrc = robust_flock(pFile->h, LOCK_EX | LOCK_NB);
28249    if( !lrc ){
28250      /* got the lock, unlock it */
28251      lrc = robust_flock(pFile->h, LOCK_UN);
28252      if ( lrc ) {
28253        int tErrno = errno;
28254        /* unlock failed with an error */
28255        lrc = SQLITE_IOERR_UNLOCK;
28256        if( IS_LOCK_ERROR(lrc) ){
28257          storeLastErrno(pFile, tErrno);
28258          rc = lrc;
28259        }
28260      }
28261    } else {
28262      int tErrno = errno;
28263      reserved = 1;
28264      /* someone else might have it reserved */
28265      lrc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK);
28266      if( IS_LOCK_ERROR(lrc) ){
28267        storeLastErrno(pFile, tErrno);
28268        rc = lrc;
28269      }
28270    }
28271  }
28272  OSTRACE(("TEST WR-LOCK %d %d %d (flock)\n", pFile->h, rc, reserved));
28273
28274#ifdef SQLITE_IGNORE_FLOCK_LOCK_ERRORS
28275  if( (rc & SQLITE_IOERR) == SQLITE_IOERR ){
28276    rc = SQLITE_OK;
28277    reserved=1;
28278  }
28279#endif /* SQLITE_IGNORE_FLOCK_LOCK_ERRORS */
28280  *pResOut = reserved;
28281  return rc;
28282}
28283
28284/*
28285** Lock the file with the lock specified by parameter eFileLock - one
28286** of the following:
28287**
28288**     (1) SHARED_LOCK
28289**     (2) RESERVED_LOCK
28290**     (3) PENDING_LOCK
28291**     (4) EXCLUSIVE_LOCK
28292**
28293** Sometimes when requesting one lock state, additional lock states
28294** are inserted in between.  The locking might fail on one of the later
28295** transitions leaving the lock state different from what it started but
28296** still short of its goal.  The following chart shows the allowed
28297** transitions and the inserted intermediate states:
28298**
28299**    UNLOCKED -> SHARED
28300**    SHARED -> RESERVED
28301**    SHARED -> (PENDING) -> EXCLUSIVE
28302**    RESERVED -> (PENDING) -> EXCLUSIVE
28303**    PENDING -> EXCLUSIVE
28304**
28305** flock() only really support EXCLUSIVE locks.  We track intermediate
28306** lock states in the sqlite3_file structure, but all locks SHARED or
28307** above are really EXCLUSIVE locks and exclude all other processes from
28308** access the file.
28309**
28310** This routine will only increase a lock.  Use the sqlite3OsUnlock()
28311** routine to lower a locking level.
28312*/
28313static int flockLock(sqlite3_file *id, int eFileLock) {
28314  int rc = SQLITE_OK;
28315  unixFile *pFile = (unixFile*)id;
28316
28317  assert( pFile );
28318
28319  /* if we already have a lock, it is exclusive.
28320  ** Just adjust level and punt on outta here. */
28321  if (pFile->eFileLock > NO_LOCK) {
28322    pFile->eFileLock = eFileLock;
28323    return SQLITE_OK;
28324  }
28325
28326  /* grab an exclusive lock */
28327
28328  if (robust_flock(pFile->h, LOCK_EX | LOCK_NB)) {
28329    int tErrno = errno;
28330    /* didn't get, must be busy */
28331    rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK);
28332    if( IS_LOCK_ERROR(rc) ){
28333      storeLastErrno(pFile, tErrno);
28334    }
28335  } else {
28336    /* got it, set the type and return ok */
28337    pFile->eFileLock = eFileLock;
28338  }
28339  OSTRACE(("LOCK    %d %s %s (flock)\n", pFile->h, azFileLock(eFileLock),
28340           rc==SQLITE_OK ? "ok" : "failed"));
28341#ifdef SQLITE_IGNORE_FLOCK_LOCK_ERRORS
28342  if( (rc & SQLITE_IOERR) == SQLITE_IOERR ){
28343    rc = SQLITE_BUSY;
28344  }
28345#endif /* SQLITE_IGNORE_FLOCK_LOCK_ERRORS */
28346  return rc;
28347}
28348
28349
28350/*
28351** Lower the locking level on file descriptor pFile to eFileLock.  eFileLock
28352** must be either NO_LOCK or SHARED_LOCK.
28353**
28354** If the locking level of the file descriptor is already at or below
28355** the requested locking level, this routine is a no-op.
28356*/
28357static int flockUnlock(sqlite3_file *id, int eFileLock) {
28358  unixFile *pFile = (unixFile*)id;
28359
28360  assert( pFile );
28361  OSTRACE(("UNLOCK  %d %d was %d pid=%d (flock)\n", pFile->h, eFileLock,
28362           pFile->eFileLock, osGetpid(0)));
28363  assert( eFileLock<=SHARED_LOCK );
28364
28365  /* no-op if possible */
28366  if( pFile->eFileLock==eFileLock ){
28367    return SQLITE_OK;
28368  }
28369
28370  /* shared can just be set because we always have an exclusive */
28371  if (eFileLock==SHARED_LOCK) {
28372    pFile->eFileLock = eFileLock;
28373    return SQLITE_OK;
28374  }
28375
28376  /* no, really, unlock. */
28377  if( robust_flock(pFile->h, LOCK_UN) ){
28378#ifdef SQLITE_IGNORE_FLOCK_LOCK_ERRORS
28379    return SQLITE_OK;
28380#endif /* SQLITE_IGNORE_FLOCK_LOCK_ERRORS */
28381    return SQLITE_IOERR_UNLOCK;
28382  }else{
28383    pFile->eFileLock = NO_LOCK;
28384    return SQLITE_OK;
28385  }
28386}
28387
28388/*
28389** Close a file.
28390*/
28391static int flockClose(sqlite3_file *id) {
28392  int rc = SQLITE_OK;
28393  if( id ){
28394    flockUnlock(id, NO_LOCK);
28395    rc = closeUnixFile(id);
28396  }
28397  return rc;
28398}
28399
28400#endif /* SQLITE_ENABLE_LOCKING_STYLE && !OS_VXWORK */
28401
28402/******************* End of the flock lock implementation *********************
28403******************************************************************************/
28404
28405/******************************************************************************
28406************************ Begin Named Semaphore Locking ************************
28407**
28408** Named semaphore locking is only supported on VxWorks.
28409**
28410** Semaphore locking is like dot-lock and flock in that it really only
28411** supports EXCLUSIVE locking.  Only a single process can read or write
28412** the database file at a time.  This reduces potential concurrency, but
28413** makes the lock implementation much easier.
28414*/
28415#if OS_VXWORKS
28416
28417/*
28418** This routine checks if there is a RESERVED lock held on the specified
28419** file by this or any other process. If such a lock is held, set *pResOut
28420** to a non-zero value otherwise *pResOut is set to zero.  The return value
28421** is set to SQLITE_OK unless an I/O error occurs during lock checking.
28422*/
28423static int semXCheckReservedLock(sqlite3_file *id, int *pResOut) {
28424  int rc = SQLITE_OK;
28425  int reserved = 0;
28426  unixFile *pFile = (unixFile*)id;
28427
28428  SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; );
28429
28430  assert( pFile );
28431
28432  /* Check if a thread in this process holds such a lock */
28433  if( pFile->eFileLock>SHARED_LOCK ){
28434    reserved = 1;
28435  }
28436
28437  /* Otherwise see if some other process holds it. */
28438  if( !reserved ){
28439    sem_t *pSem = pFile->pInode->pSem;
28440
28441    if( sem_trywait(pSem)==-1 ){
28442      int tErrno = errno;
28443      if( EAGAIN != tErrno ){
28444        rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_CHECKRESERVEDLOCK);
28445        storeLastErrno(pFile, tErrno);
28446      } else {
28447        /* someone else has the lock when we are in NO_LOCK */
28448        reserved = (pFile->eFileLock < SHARED_LOCK);
28449      }
28450    }else{
28451      /* we could have it if we want it */
28452      sem_post(pSem);
28453    }
28454  }
28455  OSTRACE(("TEST WR-LOCK %d %d %d (sem)\n", pFile->h, rc, reserved));
28456
28457  *pResOut = reserved;
28458  return rc;
28459}
28460
28461/*
28462** Lock the file with the lock specified by parameter eFileLock - one
28463** of the following:
28464**
28465**     (1) SHARED_LOCK
28466**     (2) RESERVED_LOCK
28467**     (3) PENDING_LOCK
28468**     (4) EXCLUSIVE_LOCK
28469**
28470** Sometimes when requesting one lock state, additional lock states
28471** are inserted in between.  The locking might fail on one of the later
28472** transitions leaving the lock state different from what it started but
28473** still short of its goal.  The following chart shows the allowed
28474** transitions and the inserted intermediate states:
28475**
28476**    UNLOCKED -> SHARED
28477**    SHARED -> RESERVED
28478**    SHARED -> (PENDING) -> EXCLUSIVE
28479**    RESERVED -> (PENDING) -> EXCLUSIVE
28480**    PENDING -> EXCLUSIVE
28481**
28482** Semaphore locks only really support EXCLUSIVE locks.  We track intermediate
28483** lock states in the sqlite3_file structure, but all locks SHARED or
28484** above are really EXCLUSIVE locks and exclude all other processes from
28485** access the file.
28486**
28487** This routine will only increase a lock.  Use the sqlite3OsUnlock()
28488** routine to lower a locking level.
28489*/
28490static int semXLock(sqlite3_file *id, int eFileLock) {
28491  unixFile *pFile = (unixFile*)id;
28492  sem_t *pSem = pFile->pInode->pSem;
28493  int rc = SQLITE_OK;
28494
28495  /* if we already have a lock, it is exclusive.
28496  ** Just adjust level and punt on outta here. */
28497  if (pFile->eFileLock > NO_LOCK) {
28498    pFile->eFileLock = eFileLock;
28499    rc = SQLITE_OK;
28500    goto sem_end_lock;
28501  }
28502
28503  /* lock semaphore now but bail out when already locked. */
28504  if( sem_trywait(pSem)==-1 ){
28505    rc = SQLITE_BUSY;
28506    goto sem_end_lock;
28507  }
28508
28509  /* got it, set the type and return ok */
28510  pFile->eFileLock = eFileLock;
28511
28512 sem_end_lock:
28513  return rc;
28514}
28515
28516/*
28517** Lower the locking level on file descriptor pFile to eFileLock.  eFileLock
28518** must be either NO_LOCK or SHARED_LOCK.
28519**
28520** If the locking level of the file descriptor is already at or below
28521** the requested locking level, this routine is a no-op.
28522*/
28523static int semXUnlock(sqlite3_file *id, int eFileLock) {
28524  unixFile *pFile = (unixFile*)id;
28525  sem_t *pSem = pFile->pInode->pSem;
28526
28527  assert( pFile );
28528  assert( pSem );
28529  OSTRACE(("UNLOCK  %d %d was %d pid=%d (sem)\n", pFile->h, eFileLock,
28530           pFile->eFileLock, osGetpid(0)));
28531  assert( eFileLock<=SHARED_LOCK );
28532
28533  /* no-op if possible */
28534  if( pFile->eFileLock==eFileLock ){
28535    return SQLITE_OK;
28536  }
28537
28538  /* shared can just be set because we always have an exclusive */
28539  if (eFileLock==SHARED_LOCK) {
28540    pFile->eFileLock = eFileLock;
28541    return SQLITE_OK;
28542  }
28543
28544  /* no, really unlock. */
28545  if ( sem_post(pSem)==-1 ) {
28546    int rc, tErrno = errno;
28547    rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_UNLOCK);
28548    if( IS_LOCK_ERROR(rc) ){
28549      storeLastErrno(pFile, tErrno);
28550    }
28551    return rc;
28552  }
28553  pFile->eFileLock = NO_LOCK;
28554  return SQLITE_OK;
28555}
28556
28557/*
28558 ** Close a file.
28559 */
28560static int semXClose(sqlite3_file *id) {
28561  if( id ){
28562    unixFile *pFile = (unixFile*)id;
28563    semXUnlock(id, NO_LOCK);
28564    assert( pFile );
28565    unixEnterMutex();
28566    releaseInodeInfo(pFile);
28567    unixLeaveMutex();
28568    closeUnixFile(id);
28569  }
28570  return SQLITE_OK;
28571}
28572
28573#endif /* OS_VXWORKS */
28574/*
28575** Named semaphore locking is only available on VxWorks.
28576**
28577*************** End of the named semaphore lock implementation ****************
28578******************************************************************************/
28579
28580
28581/******************************************************************************
28582*************************** Begin AFP Locking *********************************
28583**
28584** AFP is the Apple Filing Protocol.  AFP is a network filesystem found
28585** on Apple Macintosh computers - both OS9 and OSX.
28586**
28587** Third-party implementations of AFP are available.  But this code here
28588** only works on OSX.
28589*/
28590
28591#if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
28592/*
28593** The afpLockingContext structure contains all afp lock specific state
28594*/
28595typedef struct afpLockingContext afpLockingContext;
28596struct afpLockingContext {
28597  int reserved;
28598  const char *dbPath;             /* Name of the open file */
28599};
28600
28601struct ByteRangeLockPB2
28602{
28603  unsigned long long offset;        /* offset to first byte to lock */
28604  unsigned long long length;        /* nbr of bytes to lock */
28605  unsigned long long retRangeStart; /* nbr of 1st byte locked if successful */
28606  unsigned char unLockFlag;         /* 1 = unlock, 0 = lock */
28607  unsigned char startEndFlag;       /* 1=rel to end of fork, 0=rel to start */
28608  int fd;                           /* file desc to assoc this lock with */
28609};
28610
28611#define afpfsByteRangeLock2FSCTL        _IOWR('z', 23, struct ByteRangeLockPB2)
28612
28613/*
28614** This is a utility for setting or clearing a bit-range lock on an
28615** AFP filesystem.
28616**
28617** Return SQLITE_OK on success, SQLITE_BUSY on failure.
28618*/
28619static int afpSetLock(
28620  const char *path,              /* Name of the file to be locked or unlocked */
28621  unixFile *pFile,               /* Open file descriptor on path */
28622  unsigned long long offset,     /* First byte to be locked */
28623  unsigned long long length,     /* Number of bytes to lock */
28624  int setLockFlag                /* True to set lock.  False to clear lock */
28625){
28626  struct ByteRangeLockPB2 pb;
28627  int err;
28628
28629  pb.unLockFlag = setLockFlag ? 0 : 1;
28630  pb.startEndFlag = 0;
28631  pb.offset = offset;
28632  pb.length = length;
28633  pb.fd = pFile->h;
28634
28635  OSTRACE(("AFPSETLOCK [%s] for %d%s in range %llx:%llx\n",
28636    (setLockFlag?"ON":"OFF"), pFile->h, (pb.fd==-1?"[testval-1]":""),
28637    offset, length));
28638  err = fsctl(path, afpfsByteRangeLock2FSCTL, &pb, 0);
28639  if ( err==-1 ) {
28640    int rc;
28641    int tErrno = errno;
28642    OSTRACE(("AFPSETLOCK failed to fsctl() '%s' %d %s\n",
28643             path, tErrno, strerror(tErrno)));
28644#ifdef SQLITE_IGNORE_AFP_LOCK_ERRORS
28645    rc = SQLITE_BUSY;
28646#else
28647    rc = sqliteErrorFromPosixError(tErrno,
28648                    setLockFlag ? SQLITE_IOERR_LOCK : SQLITE_IOERR_UNLOCK);
28649#endif /* SQLITE_IGNORE_AFP_LOCK_ERRORS */
28650    if( IS_LOCK_ERROR(rc) ){
28651      storeLastErrno(pFile, tErrno);
28652    }
28653    return rc;
28654  } else {
28655    return SQLITE_OK;
28656  }
28657}
28658
28659/*
28660** This routine checks if there is a RESERVED lock held on the specified
28661** file by this or any other process. If such a lock is held, set *pResOut
28662** to a non-zero value otherwise *pResOut is set to zero.  The return value
28663** is set to SQLITE_OK unless an I/O error occurs during lock checking.
28664*/
28665static int afpCheckReservedLock(sqlite3_file *id, int *pResOut){
28666  int rc = SQLITE_OK;
28667  int reserved = 0;
28668  unixFile *pFile = (unixFile*)id;
28669  afpLockingContext *context;
28670
28671  SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; );
28672
28673  assert( pFile );
28674  context = (afpLockingContext *) pFile->lockingContext;
28675  if( context->reserved ){
28676    *pResOut = 1;
28677    return SQLITE_OK;
28678  }
28679  unixEnterMutex(); /* Because pFile->pInode is shared across threads */
28680
28681  /* Check if a thread in this process holds such a lock */
28682  if( pFile->pInode->eFileLock>SHARED_LOCK ){
28683    reserved = 1;
28684  }
28685
28686  /* Otherwise see if some other process holds it.
28687   */
28688  if( !reserved ){
28689    /* lock the RESERVED byte */
28690    int lrc = afpSetLock(context->dbPath, pFile, RESERVED_BYTE, 1,1);
28691    if( SQLITE_OK==lrc ){
28692      /* if we succeeded in taking the reserved lock, unlock it to restore
28693      ** the original state */
28694      lrc = afpSetLock(context->dbPath, pFile, RESERVED_BYTE, 1, 0);
28695    } else {
28696      /* if we failed to get the lock then someone else must have it */
28697      reserved = 1;
28698    }
28699    if( IS_LOCK_ERROR(lrc) ){
28700      rc=lrc;
28701    }
28702  }
28703
28704  unixLeaveMutex();
28705  OSTRACE(("TEST WR-LOCK %d %d %d (afp)\n", pFile->h, rc, reserved));
28706
28707  *pResOut = reserved;
28708  return rc;
28709}
28710
28711/*
28712** Lock the file with the lock specified by parameter eFileLock - one
28713** of the following:
28714**
28715**     (1) SHARED_LOCK
28716**     (2) RESERVED_LOCK
28717**     (3) PENDING_LOCK
28718**     (4) EXCLUSIVE_LOCK
28719**
28720** Sometimes when requesting one lock state, additional lock states
28721** are inserted in between.  The locking might fail on one of the later
28722** transitions leaving the lock state different from what it started but
28723** still short of its goal.  The following chart shows the allowed
28724** transitions and the inserted intermediate states:
28725**
28726**    UNLOCKED -> SHARED
28727**    SHARED -> RESERVED
28728**    SHARED -> (PENDING) -> EXCLUSIVE
28729**    RESERVED -> (PENDING) -> EXCLUSIVE
28730**    PENDING -> EXCLUSIVE
28731**
28732** This routine will only increase a lock.  Use the sqlite3OsUnlock()
28733** routine to lower a locking level.
28734*/
28735static int afpLock(sqlite3_file *id, int eFileLock){
28736  int rc = SQLITE_OK;
28737  unixFile *pFile = (unixFile*)id;
28738  unixInodeInfo *pInode = pFile->pInode;
28739  afpLockingContext *context = (afpLockingContext *) pFile->lockingContext;
28740
28741  assert( pFile );
28742  OSTRACE(("LOCK    %d %s was %s(%s,%d) pid=%d (afp)\n", pFile->h,
28743           azFileLock(eFileLock), azFileLock(pFile->eFileLock),
28744           azFileLock(pInode->eFileLock), pInode->nShared , osGetpid(0)));
28745
28746  /* If there is already a lock of this type or more restrictive on the
28747  ** unixFile, do nothing. Don't use the afp_end_lock: exit path, as
28748  ** unixEnterMutex() hasn't been called yet.
28749  */
28750  if( pFile->eFileLock>=eFileLock ){
28751    OSTRACE(("LOCK    %d %s ok (already held) (afp)\n", pFile->h,
28752           azFileLock(eFileLock)));
28753    return SQLITE_OK;
28754  }
28755
28756  /* Make sure the locking sequence is correct
28757  **  (1) We never move from unlocked to anything higher than shared lock.
28758  **  (2) SQLite never explicitly requests a pendig lock.
28759  **  (3) A shared lock is always held when a reserve lock is requested.
28760  */
28761  assert( pFile->eFileLock!=NO_LOCK || eFileLock==SHARED_LOCK );
28762  assert( eFileLock!=PENDING_LOCK );
28763  assert( eFileLock!=RESERVED_LOCK || pFile->eFileLock==SHARED_LOCK );
28764
28765  /* This mutex is needed because pFile->pInode is shared across threads
28766  */
28767  unixEnterMutex();
28768  pInode = pFile->pInode;
28769
28770  /* If some thread using this PID has a lock via a different unixFile*
28771  ** handle that precludes the requested lock, return BUSY.
28772  */
28773  if( (pFile->eFileLock!=pInode->eFileLock &&
28774       (pInode->eFileLock>=PENDING_LOCK || eFileLock>SHARED_LOCK))
28775     ){
28776    rc = SQLITE_BUSY;
28777    goto afp_end_lock;
28778  }
28779
28780  /* If a SHARED lock is requested, and some thread using this PID already
28781  ** has a SHARED or RESERVED lock, then increment reference counts and
28782  ** return SQLITE_OK.
28783  */
28784  if( eFileLock==SHARED_LOCK &&
28785     (pInode->eFileLock==SHARED_LOCK || pInode->eFileLock==RESERVED_LOCK) ){
28786    assert( eFileLock==SHARED_LOCK );
28787    assert( pFile->eFileLock==0 );
28788    assert( pInode->nShared>0 );
28789    pFile->eFileLock = SHARED_LOCK;
28790    pInode->nShared++;
28791    pInode->nLock++;
28792    goto afp_end_lock;
28793  }
28794
28795  /* A PENDING lock is needed before acquiring a SHARED lock and before
28796  ** acquiring an EXCLUSIVE lock.  For the SHARED lock, the PENDING will
28797  ** be released.
28798  */
28799  if( eFileLock==SHARED_LOCK
28800      || (eFileLock==EXCLUSIVE_LOCK && pFile->eFileLock<PENDING_LOCK)
28801  ){
28802    int failed;
28803    failed = afpSetLock(context->dbPath, pFile, PENDING_BYTE, 1, 1);
28804    if (failed) {
28805      rc = failed;
28806      goto afp_end_lock;
28807    }
28808  }
28809
28810  /* If control gets to this point, then actually go ahead and make
28811  ** operating system calls for the specified lock.
28812  */
28813  if( eFileLock==SHARED_LOCK ){
28814    int lrc1, lrc2, lrc1Errno = 0;
28815    long lk, mask;
28816
28817    assert( pInode->nShared==0 );
28818    assert( pInode->eFileLock==0 );
28819
28820    mask = (sizeof(long)==8) ? LARGEST_INT64 : 0x7fffffff;
28821    /* Now get the read-lock SHARED_LOCK */
28822    /* note that the quality of the randomness doesn't matter that much */
28823    lk = random();
28824    pInode->sharedByte = (lk & mask)%(SHARED_SIZE - 1);
28825    lrc1 = afpSetLock(context->dbPath, pFile,
28826          SHARED_FIRST+pInode->sharedByte, 1, 1);
28827    if( IS_LOCK_ERROR(lrc1) ){
28828      lrc1Errno = pFile->lastErrno;
28829    }
28830    /* Drop the temporary PENDING lock */
28831    lrc2 = afpSetLock(context->dbPath, pFile, PENDING_BYTE, 1, 0);
28832
28833    if( IS_LOCK_ERROR(lrc1) ) {
28834      storeLastErrno(pFile, lrc1Errno);
28835      rc = lrc1;
28836      goto afp_end_lock;
28837    } else if( IS_LOCK_ERROR(lrc2) ){
28838      rc = lrc2;
28839      goto afp_end_lock;
28840    } else if( lrc1 != SQLITE_OK ) {
28841      rc = lrc1;
28842    } else {
28843      pFile->eFileLock = SHARED_LOCK;
28844      pInode->nLock++;
28845      pInode->nShared = 1;
28846    }
28847  }else if( eFileLock==EXCLUSIVE_LOCK && pInode->nShared>1 ){
28848    /* We are trying for an exclusive lock but another thread in this
28849     ** same process is still holding a shared lock. */
28850    rc = SQLITE_BUSY;
28851  }else{
28852    /* The request was for a RESERVED or EXCLUSIVE lock.  It is
28853    ** assumed that there is a SHARED or greater lock on the file
28854    ** already.
28855    */
28856    int failed = 0;
28857    assert( 0!=pFile->eFileLock );
28858    if (eFileLock >= RESERVED_LOCK && pFile->eFileLock < RESERVED_LOCK) {
28859        /* Acquire a RESERVED lock */
28860        failed = afpSetLock(context->dbPath, pFile, RESERVED_BYTE, 1,1);
28861      if( !failed ){
28862        context->reserved = 1;
28863      }
28864    }
28865    if (!failed && eFileLock == EXCLUSIVE_LOCK) {
28866      /* Acquire an EXCLUSIVE lock */
28867
28868      /* Remove the shared lock before trying the range.  we'll need to
28869      ** reestablish the shared lock if we can't get the  afpUnlock
28870      */
28871      if( !(failed = afpSetLock(context->dbPath, pFile, SHARED_FIRST +
28872                         pInode->sharedByte, 1, 0)) ){
28873        int failed2 = SQLITE_OK;
28874        /* now attemmpt to get the exclusive lock range */
28875        failed = afpSetLock(context->dbPath, pFile, SHARED_FIRST,
28876                               SHARED_SIZE, 1);
28877        if( failed && (failed2 = afpSetLock(context->dbPath, pFile,
28878                       SHARED_FIRST + pInode->sharedByte, 1, 1)) ){
28879          /* Can't reestablish the shared lock.  Sqlite can't deal, this is
28880          ** a critical I/O error
28881          */
28882          rc = ((failed & SQLITE_IOERR) == SQLITE_IOERR) ? failed2 :
28883               SQLITE_IOERR_LOCK;
28884          goto afp_end_lock;
28885        }
28886      }else{
28887        rc = failed;
28888      }
28889    }
28890    if( failed ){
28891      rc = failed;
28892    }
28893  }
28894
28895  if( rc==SQLITE_OK ){
28896    pFile->eFileLock = eFileLock;
28897    pInode->eFileLock = eFileLock;
28898  }else if( eFileLock==EXCLUSIVE_LOCK ){
28899    pFile->eFileLock = PENDING_LOCK;
28900    pInode->eFileLock = PENDING_LOCK;
28901  }
28902
28903afp_end_lock:
28904  unixLeaveMutex();
28905  OSTRACE(("LOCK    %d %s %s (afp)\n", pFile->h, azFileLock(eFileLock),
28906         rc==SQLITE_OK ? "ok" : "failed"));
28907  return rc;
28908}
28909
28910/*
28911** Lower the locking level on file descriptor pFile to eFileLock.  eFileLock
28912** must be either NO_LOCK or SHARED_LOCK.
28913**
28914** If the locking level of the file descriptor is already at or below
28915** the requested locking level, this routine is a no-op.
28916*/
28917static int afpUnlock(sqlite3_file *id, int eFileLock) {
28918  int rc = SQLITE_OK;
28919  unixFile *pFile = (unixFile*)id;
28920  unixInodeInfo *pInode;
28921  afpLockingContext *context = (afpLockingContext *) pFile->lockingContext;
28922  int skipShared = 0;
28923#ifdef SQLITE_TEST
28924  int h = pFile->h;
28925#endif
28926
28927  assert( pFile );
28928  OSTRACE(("UNLOCK  %d %d was %d(%d,%d) pid=%d (afp)\n", pFile->h, eFileLock,
28929           pFile->eFileLock, pFile->pInode->eFileLock, pFile->pInode->nShared,
28930           osGetpid(0)));
28931
28932  assert( eFileLock<=SHARED_LOCK );
28933  if( pFile->eFileLock<=eFileLock ){
28934    return SQLITE_OK;
28935  }
28936  unixEnterMutex();
28937  pInode = pFile->pInode;
28938  assert( pInode->nShared!=0 );
28939  if( pFile->eFileLock>SHARED_LOCK ){
28940    assert( pInode->eFileLock==pFile->eFileLock );
28941    SimulateIOErrorBenign(1);
28942    SimulateIOError( h=(-1) )
28943    SimulateIOErrorBenign(0);
28944
28945#ifdef SQLITE_DEBUG
28946    /* When reducing a lock such that other processes can start
28947    ** reading the database file again, make sure that the
28948    ** transaction counter was updated if any part of the database
28949    ** file changed.  If the transaction counter is not updated,
28950    ** other connections to the same file might not realize that
28951    ** the file has changed and hence might not know to flush their
28952    ** cache.  The use of a stale cache can lead to database corruption.
28953    */
28954    assert( pFile->inNormalWrite==0
28955           || pFile->dbUpdate==0
28956           || pFile->transCntrChng==1 );
28957    pFile->inNormalWrite = 0;
28958#endif
28959
28960    if( pFile->eFileLock==EXCLUSIVE_LOCK ){
28961      rc = afpSetLock(context->dbPath, pFile, SHARED_FIRST, SHARED_SIZE, 0);
28962      if( rc==SQLITE_OK && (eFileLock==SHARED_LOCK || pInode->nShared>1) ){
28963        /* only re-establish the shared lock if necessary */
28964        int sharedLockByte = SHARED_FIRST+pInode->sharedByte;
28965        rc = afpSetLock(context->dbPath, pFile, sharedLockByte, 1, 1);
28966      } else {
28967        skipShared = 1;
28968      }
28969    }
28970    if( rc==SQLITE_OK && pFile->eFileLock>=PENDING_LOCK ){
28971      rc = afpSetLock(context->dbPath, pFile, PENDING_BYTE, 1, 0);
28972    }
28973    if( rc==SQLITE_OK && pFile->eFileLock>=RESERVED_LOCK && context->reserved ){
28974      rc = afpSetLock(context->dbPath, pFile, RESERVED_BYTE, 1, 0);
28975      if( !rc ){
28976        context->reserved = 0;
28977      }
28978    }
28979    if( rc==SQLITE_OK && (eFileLock==SHARED_LOCK || pInode->nShared>1)){
28980      pInode->eFileLock = SHARED_LOCK;
28981    }
28982  }
28983  if( rc==SQLITE_OK && eFileLock==NO_LOCK ){
28984
28985    /* Decrement the shared lock counter.  Release the lock using an
28986    ** OS call only when all threads in this same process have released
28987    ** the lock.
28988    */
28989    unsigned long long sharedLockByte = SHARED_FIRST+pInode->sharedByte;
28990    pInode->nShared--;
28991    if( pInode->nShared==0 ){
28992      SimulateIOErrorBenign(1);
28993      SimulateIOError( h=(-1) )
28994      SimulateIOErrorBenign(0);
28995      if( !skipShared ){
28996        rc = afpSetLock(context->dbPath, pFile, sharedLockByte, 1, 0);
28997      }
28998      if( !rc ){
28999        pInode->eFileLock = NO_LOCK;
29000        pFile->eFileLock = NO_LOCK;
29001      }
29002    }
29003    if( rc==SQLITE_OK ){
29004      pInode->nLock--;
29005      assert( pInode->nLock>=0 );
29006      if( pInode->nLock==0 ){
29007        closePendingFds(pFile);
29008      }
29009    }
29010  }
29011
29012  unixLeaveMutex();
29013  if( rc==SQLITE_OK ) pFile->eFileLock = eFileLock;
29014  return rc;
29015}
29016
29017/*
29018** Close a file & cleanup AFP specific locking context
29019*/
29020static int afpClose(sqlite3_file *id) {
29021  int rc = SQLITE_OK;
29022  if( id ){
29023    unixFile *pFile = (unixFile*)id;
29024    afpUnlock(id, NO_LOCK);
29025    unixEnterMutex();
29026    if( pFile->pInode && pFile->pInode->nLock ){
29027      /* If there are outstanding locks, do not actually close the file just
29028      ** yet because that would clear those locks.  Instead, add the file
29029      ** descriptor to pInode->aPending.  It will be automatically closed when
29030      ** the last lock is cleared.
29031      */
29032      setPendingFd(pFile);
29033    }
29034    releaseInodeInfo(pFile);
29035    sqlite3_free(pFile->lockingContext);
29036    rc = closeUnixFile(id);
29037    unixLeaveMutex();
29038  }
29039  return rc;
29040}
29041
29042#endif /* defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE */
29043/*
29044** The code above is the AFP lock implementation.  The code is specific
29045** to MacOSX and does not work on other unix platforms.  No alternative
29046** is available.  If you don't compile for a mac, then the "unix-afp"
29047** VFS is not available.
29048**
29049********************* End of the AFP lock implementation **********************
29050******************************************************************************/
29051
29052/******************************************************************************
29053*************************** Begin NFS Locking ********************************/
29054
29055#if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
29056/*
29057 ** Lower the locking level on file descriptor pFile to eFileLock.  eFileLock
29058 ** must be either NO_LOCK or SHARED_LOCK.
29059 **
29060 ** If the locking level of the file descriptor is already at or below
29061 ** the requested locking level, this routine is a no-op.
29062 */
29063static int nfsUnlock(sqlite3_file *id, int eFileLock){
29064  return posixUnlock(id, eFileLock, 1);
29065}
29066
29067#endif /* defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE */
29068/*
29069** The code above is the NFS lock implementation.  The code is specific
29070** to MacOSX and does not work on other unix platforms.  No alternative
29071** is available.
29072**
29073********************* End of the NFS lock implementation **********************
29074******************************************************************************/
29075
29076/******************************************************************************
29077**************** Non-locking sqlite3_file methods *****************************
29078**
29079** The next division contains implementations for all methods of the
29080** sqlite3_file object other than the locking methods.  The locking
29081** methods were defined in divisions above (one locking method per
29082** division).  Those methods that are common to all locking modes
29083** are gather together into this division.
29084*/
29085
29086/*
29087** Seek to the offset passed as the second argument, then read cnt
29088** bytes into pBuf. Return the number of bytes actually read.
29089**
29090** NB:  If you define USE_PREAD or USE_PREAD64, then it might also
29091** be necessary to define _XOPEN_SOURCE to be 500.  This varies from
29092** one system to another.  Since SQLite does not define USE_PREAD
29093** in any form by default, we will not attempt to define _XOPEN_SOURCE.
29094** See tickets #2741 and #2681.
29095**
29096** To avoid stomping the errno value on a failed read the lastErrno value
29097** is set before returning.
29098*/
29099static int seekAndRead(unixFile *id, sqlite3_int64 offset, void *pBuf, int cnt){
29100  int got;
29101  int prior = 0;
29102#if (!defined(USE_PREAD) && !defined(USE_PREAD64))
29103  i64 newOffset;
29104#endif
29105  TIMER_START;
29106  assert( cnt==(cnt&0x1ffff) );
29107  assert( id->h>2 );
29108  cnt &= 0x1ffff;
29109  do{
29110#if defined(USE_PREAD)
29111    got = osPread(id->h, pBuf, cnt, offset);
29112    SimulateIOError( got = -1 );
29113#elif defined(USE_PREAD64)
29114    got = osPread64(id->h, pBuf, cnt, offset);
29115    SimulateIOError( got = -1 );
29116#else
29117    newOffset = lseek(id->h, offset, SEEK_SET);
29118    SimulateIOError( newOffset-- );
29119    if( newOffset!=offset ){
29120      if( newOffset == -1 ){
29121        storeLastErrno((unixFile*)id, errno);
29122      }else{
29123        storeLastErrno((unixFile*)id, 0);
29124      }
29125      return -1;
29126    }
29127    got = osRead(id->h, pBuf, cnt);
29128#endif
29129    if( got==cnt ) break;
29130    if( got<0 ){
29131      if( errno==EINTR ){ got = 1; continue; }
29132      prior = 0;
29133      storeLastErrno((unixFile*)id,  errno);
29134      break;
29135    }else if( got>0 ){
29136      cnt -= got;
29137      offset += got;
29138      prior += got;
29139      pBuf = (void*)(got + (char*)pBuf);
29140    }
29141  }while( got>0 );
29142  TIMER_END;
29143  OSTRACE(("READ    %-3d %5d %7lld %llu\n",
29144            id->h, got+prior, offset-prior, TIMER_ELAPSED));
29145  return got+prior;
29146}
29147
29148/*
29149** Read data from a file into a buffer.  Return SQLITE_OK if all
29150** bytes were read successfully and SQLITE_IOERR if anything goes
29151** wrong.
29152*/
29153static int unixRead(
29154  sqlite3_file *id,
29155  void *pBuf,
29156  int amt,
29157  sqlite3_int64 offset
29158){
29159  unixFile *pFile = (unixFile *)id;
29160  int got;
29161  assert( id );
29162  assert( offset>=0 );
29163  assert( amt>0 );
29164
29165  /* If this is a database file (not a journal, master-journal or temp
29166  ** file), the bytes in the locking range should never be read or written. */
29167#if 0
29168  assert( pFile->pUnused==0
29169       || offset>=PENDING_BYTE+512
29170       || offset+amt<=PENDING_BYTE
29171  );
29172#endif
29173
29174#if SQLITE_MAX_MMAP_SIZE>0
29175  /* Deal with as much of this read request as possible by transfering
29176  ** data from the memory mapping using memcpy().  */
29177  if( offset<pFile->mmapSize ){
29178    if( offset+amt <= pFile->mmapSize ){
29179      memcpy(pBuf, &((u8 *)(pFile->pMapRegion))[offset], amt);
29180      return SQLITE_OK;
29181    }else{
29182      int nCopy = pFile->mmapSize - offset;
29183      memcpy(pBuf, &((u8 *)(pFile->pMapRegion))[offset], nCopy);
29184      pBuf = &((u8 *)pBuf)[nCopy];
29185      amt -= nCopy;
29186      offset += nCopy;
29187    }
29188  }
29189#endif
29190
29191  got = seekAndRead(pFile, offset, pBuf, amt);
29192  if( got==amt ){
29193    return SQLITE_OK;
29194  }else if( got<0 ){
29195    /* lastErrno set by seekAndRead */
29196    return SQLITE_IOERR_READ;
29197  }else{
29198    storeLastErrno(pFile, 0);   /* not a system error */
29199    /* Unread parts of the buffer must be zero-filled */
29200    memset(&((char*)pBuf)[got], 0, amt-got);
29201    return SQLITE_IOERR_SHORT_READ;
29202  }
29203}
29204
29205/*
29206** Attempt to seek the file-descriptor passed as the first argument to
29207** absolute offset iOff, then attempt to write nBuf bytes of data from
29208** pBuf to it. If an error occurs, return -1 and set *piErrno. Otherwise,
29209** return the actual number of bytes written (which may be less than
29210** nBuf).
29211*/
29212static int seekAndWriteFd(
29213  int fd,                         /* File descriptor to write to */
29214  i64 iOff,                       /* File offset to begin writing at */
29215  const void *pBuf,               /* Copy data from this buffer to the file */
29216  int nBuf,                       /* Size of buffer pBuf in bytes */
29217  int *piErrno                    /* OUT: Error number if error occurs */
29218){
29219  int rc = 0;                     /* Value returned by system call */
29220
29221  assert( nBuf==(nBuf&0x1ffff) );
29222  assert( fd>2 );
29223  nBuf &= 0x1ffff;
29224  TIMER_START;
29225
29226#if defined(USE_PREAD)
29227  do{ rc = (int)osPwrite(fd, pBuf, nBuf, iOff); }while( rc<0 && errno==EINTR );
29228#elif defined(USE_PREAD64)
29229  do{ rc = (int)osPwrite64(fd, pBuf, nBuf, iOff);}while( rc<0 && errno==EINTR);
29230#else
29231  do{
29232    i64 iSeek = lseek(fd, iOff, SEEK_SET);
29233    SimulateIOError( iSeek-- );
29234
29235    if( iSeek!=iOff ){
29236      if( piErrno ) *piErrno = (iSeek==-1 ? errno : 0);
29237      return -1;
29238    }
29239    rc = osWrite(fd, pBuf, nBuf);
29240  }while( rc<0 && errno==EINTR );
29241#endif
29242
29243  TIMER_END;
29244  OSTRACE(("WRITE   %-3d %5d %7lld %llu\n", fd, rc, iOff, TIMER_ELAPSED));
29245
29246  if( rc<0 && piErrno ) *piErrno = errno;
29247  return rc;
29248}
29249
29250
29251/*
29252** Seek to the offset in id->offset then read cnt bytes into pBuf.
29253** Return the number of bytes actually read.  Update the offset.
29254**
29255** To avoid stomping the errno value on a failed write the lastErrno value
29256** is set before returning.
29257*/
29258static int seekAndWrite(unixFile *id, i64 offset, const void *pBuf, int cnt){
29259  return seekAndWriteFd(id->h, offset, pBuf, cnt, &id->lastErrno);
29260}
29261
29262
29263/*
29264** Write data from a buffer into a file.  Return SQLITE_OK on success
29265** or some other error code on failure.
29266*/
29267static int unixWrite(
29268  sqlite3_file *id,
29269  const void *pBuf,
29270  int amt,
29271  sqlite3_int64 offset
29272){
29273  unixFile *pFile = (unixFile*)id;
29274  int wrote = 0;
29275  assert( id );
29276  assert( amt>0 );
29277
29278  /* If this is a database file (not a journal, master-journal or temp
29279  ** file), the bytes in the locking range should never be read or written. */
29280#if 0
29281  assert( pFile->pUnused==0
29282       || offset>=PENDING_BYTE+512
29283       || offset+amt<=PENDING_BYTE
29284  );
29285#endif
29286
29287#ifdef SQLITE_DEBUG
29288  /* If we are doing a normal write to a database file (as opposed to
29289  ** doing a hot-journal rollback or a write to some file other than a
29290  ** normal database file) then record the fact that the database
29291  ** has changed.  If the transaction counter is modified, record that
29292  ** fact too.
29293  */
29294  if( pFile->inNormalWrite ){
29295    pFile->dbUpdate = 1;  /* The database has been modified */
29296    if( offset<=24 && offset+amt>=27 ){
29297      int rc;
29298      char oldCntr[4];
29299      SimulateIOErrorBenign(1);
29300      rc = seekAndRead(pFile, 24, oldCntr, 4);
29301      SimulateIOErrorBenign(0);
29302      if( rc!=4 || memcmp(oldCntr, &((char*)pBuf)[24-offset], 4)!=0 ){
29303        pFile->transCntrChng = 1;  /* The transaction counter has changed */
29304      }
29305    }
29306  }
29307#endif
29308
29309#if SQLITE_MAX_MMAP_SIZE>0
29310  /* Deal with as much of this write request as possible by transfering
29311  ** data from the memory mapping using memcpy().  */
29312  if( offset<pFile->mmapSize ){
29313    if( offset+amt <= pFile->mmapSize ){
29314      memcpy(&((u8 *)(pFile->pMapRegion))[offset], pBuf, amt);
29315      return SQLITE_OK;
29316    }else{
29317      int nCopy = pFile->mmapSize - offset;
29318      memcpy(&((u8 *)(pFile->pMapRegion))[offset], pBuf, nCopy);
29319      pBuf = &((u8 *)pBuf)[nCopy];
29320      amt -= nCopy;
29321      offset += nCopy;
29322    }
29323  }
29324#endif
29325
29326  while( amt>0 && (wrote = seekAndWrite(pFile, offset, pBuf, amt))>0 ){
29327    amt -= wrote;
29328    offset += wrote;
29329    pBuf = &((char*)pBuf)[wrote];
29330  }
29331  SimulateIOError(( wrote=(-1), amt=1 ));
29332  SimulateDiskfullError(( wrote=0, amt=1 ));
29333
29334  if( amt>0 ){
29335    if( wrote<0 && pFile->lastErrno!=ENOSPC ){
29336      /* lastErrno set by seekAndWrite */
29337      return SQLITE_IOERR_WRITE;
29338    }else{
29339      storeLastErrno(pFile, 0); /* not a system error */
29340      return SQLITE_FULL;
29341    }
29342  }
29343
29344  return SQLITE_OK;
29345}
29346
29347#ifdef SQLITE_TEST
29348/*
29349** Count the number of fullsyncs and normal syncs.  This is used to test
29350** that syncs and fullsyncs are occurring at the right times.
29351*/
29352SQLITE_API int sqlite3_sync_count = 0;
29353SQLITE_API int sqlite3_fullsync_count = 0;
29354#endif
29355
29356/*
29357** We do not trust systems to provide a working fdatasync().  Some do.
29358** Others do no.  To be safe, we will stick with the (slightly slower)
29359** fsync(). If you know that your system does support fdatasync() correctly,
29360** then simply compile with -Dfdatasync=fdatasync or -DHAVE_FDATASYNC
29361*/
29362#if !defined(fdatasync) && !HAVE_FDATASYNC
29363# define fdatasync fsync
29364#endif
29365
29366/*
29367** Define HAVE_FULLFSYNC to 0 or 1 depending on whether or not
29368** the F_FULLFSYNC macro is defined.  F_FULLFSYNC is currently
29369** only available on Mac OS X.  But that could change.
29370*/
29371#ifdef F_FULLFSYNC
29372# define HAVE_FULLFSYNC 1
29373#else
29374# define HAVE_FULLFSYNC 0
29375#endif
29376
29377
29378/*
29379** The fsync() system call does not work as advertised on many
29380** unix systems.  The following procedure is an attempt to make
29381** it work better.
29382**
29383** The SQLITE_NO_SYNC macro disables all fsync()s.  This is useful
29384** for testing when we want to run through the test suite quickly.
29385** You are strongly advised *not* to deploy with SQLITE_NO_SYNC
29386** enabled, however, since with SQLITE_NO_SYNC enabled, an OS crash
29387** or power failure will likely corrupt the database file.
29388**
29389** SQLite sets the dataOnly flag if the size of the file is unchanged.
29390** The idea behind dataOnly is that it should only write the file content
29391** to disk, not the inode.  We only set dataOnly if the file size is
29392** unchanged since the file size is part of the inode.  However,
29393** Ted Ts'o tells us that fdatasync() will also write the inode if the
29394** file size has changed.  The only real difference between fdatasync()
29395** and fsync(), Ted tells us, is that fdatasync() will not flush the
29396** inode if the mtime or owner or other inode attributes have changed.
29397** We only care about the file size, not the other file attributes, so
29398** as far as SQLite is concerned, an fdatasync() is always adequate.
29399** So, we always use fdatasync() if it is available, regardless of
29400** the value of the dataOnly flag.
29401*/
29402static int full_fsync(int fd, int fullSync, int dataOnly){
29403  int rc;
29404
29405  /* The following "ifdef/elif/else/" block has the same structure as
29406  ** the one below. It is replicated here solely to avoid cluttering
29407  ** up the real code with the UNUSED_PARAMETER() macros.
29408  */
29409#ifdef SQLITE_NO_SYNC
29410  UNUSED_PARAMETER(fd);
29411  UNUSED_PARAMETER(fullSync);
29412  UNUSED_PARAMETER(dataOnly);
29413#elif HAVE_FULLFSYNC
29414  UNUSED_PARAMETER(dataOnly);
29415#else
29416  UNUSED_PARAMETER(fullSync);
29417  UNUSED_PARAMETER(dataOnly);
29418#endif
29419
29420  /* Record the number of times that we do a normal fsync() and
29421  ** FULLSYNC.  This is used during testing to verify that this procedure
29422  ** gets called with the correct arguments.
29423  */
29424#ifdef SQLITE_TEST
29425  if( fullSync ) sqlite3_fullsync_count++;
29426  sqlite3_sync_count++;
29427#endif
29428
29429  /* If we compiled with the SQLITE_NO_SYNC flag, then syncing is a
29430  ** no-op
29431  */
29432#ifdef SQLITE_NO_SYNC
29433  rc = SQLITE_OK;
29434#elif HAVE_FULLFSYNC
29435  if( fullSync ){
29436    rc = osFcntl(fd, F_FULLFSYNC, 0);
29437  }else{
29438    rc = 1;
29439  }
29440  /* If the FULLFSYNC failed, fall back to attempting an fsync().
29441  ** It shouldn't be possible for fullfsync to fail on the local
29442  ** file system (on OSX), so failure indicates that FULLFSYNC
29443  ** isn't supported for this file system. So, attempt an fsync
29444  ** and (for now) ignore the overhead of a superfluous fcntl call.
29445  ** It'd be better to detect fullfsync support once and avoid
29446  ** the fcntl call every time sync is called.
29447  */
29448  if( rc ) rc = fsync(fd);
29449
29450#elif defined(__APPLE__)
29451  /* fdatasync() on HFS+ doesn't yet flush the file size if it changed correctly
29452  ** so currently we default to the macro that redefines fdatasync to fsync
29453  */
29454  rc = fsync(fd);
29455#else
29456  rc = fdatasync(fd);
29457#if OS_VXWORKS
29458  if( rc==-1 && errno==ENOTSUP ){
29459    rc = fsync(fd);
29460  }
29461#endif /* OS_VXWORKS */
29462#endif /* ifdef SQLITE_NO_SYNC elif HAVE_FULLFSYNC */
29463
29464  if( OS_VXWORKS && rc!= -1 ){
29465    rc = 0;
29466  }
29467  return rc;
29468}
29469
29470/*
29471** Open a file descriptor to the directory containing file zFilename.
29472** If successful, *pFd is set to the opened file descriptor and
29473** SQLITE_OK is returned. If an error occurs, either SQLITE_NOMEM
29474** or SQLITE_CANTOPEN is returned and *pFd is set to an undefined
29475** value.
29476**
29477** The directory file descriptor is used for only one thing - to
29478** fsync() a directory to make sure file creation and deletion events
29479** are flushed to disk.  Such fsyncs are not needed on newer
29480** journaling filesystems, but are required on older filesystems.
29481**
29482** This routine can be overridden using the xSetSysCall interface.
29483** The ability to override this routine was added in support of the
29484** chromium sandbox.  Opening a directory is a security risk (we are
29485** told) so making it overrideable allows the chromium sandbox to
29486** replace this routine with a harmless no-op.  To make this routine
29487** a no-op, replace it with a stub that returns SQLITE_OK but leaves
29488** *pFd set to a negative number.
29489**
29490** If SQLITE_OK is returned, the caller is responsible for closing
29491** the file descriptor *pFd using close().
29492*/
29493static int openDirectory(const char *zFilename, int *pFd){
29494  int ii;
29495  int fd = -1;
29496  char zDirname[MAX_PATHNAME+1];
29497
29498  sqlite3_snprintf(MAX_PATHNAME, zDirname, "%s", zFilename);
29499  for(ii=(int)strlen(zDirname); ii>1 && zDirname[ii]!='/'; ii--);
29500  if( ii>0 ){
29501    zDirname[ii] = '\0';
29502    fd = robust_open(zDirname, O_RDONLY|O_BINARY, 0);
29503    if( fd>=0 ){
29504      OSTRACE(("OPENDIR %-3d %s\n", fd, zDirname));
29505    }
29506  }
29507  *pFd = fd;
29508  return (fd>=0?SQLITE_OK:unixLogError(SQLITE_CANTOPEN_BKPT, "open", zDirname));
29509}
29510
29511/*
29512** Make sure all writes to a particular file are committed to disk.
29513**
29514** If dataOnly==0 then both the file itself and its metadata (file
29515** size, access time, etc) are synced.  If dataOnly!=0 then only the
29516** file data is synced.
29517**
29518** Under Unix, also make sure that the directory entry for the file
29519** has been created by fsync-ing the directory that contains the file.
29520** If we do not do this and we encounter a power failure, the directory
29521** entry for the journal might not exist after we reboot.  The next
29522** SQLite to access the file will not know that the journal exists (because
29523** the directory entry for the journal was never created) and the transaction
29524** will not roll back - possibly leading to database corruption.
29525*/
29526static int unixSync(sqlite3_file *id, int flags){
29527  int rc;
29528  unixFile *pFile = (unixFile*)id;
29529
29530  int isDataOnly = (flags&SQLITE_SYNC_DATAONLY);
29531  int isFullsync = (flags&0x0F)==SQLITE_SYNC_FULL;
29532
29533  /* Check that one of SQLITE_SYNC_NORMAL or FULL was passed */
29534  assert((flags&0x0F)==SQLITE_SYNC_NORMAL
29535      || (flags&0x0F)==SQLITE_SYNC_FULL
29536  );
29537
29538  /* Unix cannot, but some systems may return SQLITE_FULL from here. This
29539  ** line is to test that doing so does not cause any problems.
29540  */
29541  SimulateDiskfullError( return SQLITE_FULL );
29542
29543  assert( pFile );
29544  OSTRACE(("SYNC    %-3d\n", pFile->h));
29545  rc = full_fsync(pFile->h, isFullsync, isDataOnly);
29546  SimulateIOError( rc=1 );
29547  if( rc ){
29548    storeLastErrno(pFile, errno);
29549    return unixLogError(SQLITE_IOERR_FSYNC, "full_fsync", pFile->zPath);
29550  }
29551
29552  /* Also fsync the directory containing the file if the DIRSYNC flag
29553  ** is set.  This is a one-time occurrence.  Many systems (examples: AIX)
29554  ** are unable to fsync a directory, so ignore errors on the fsync.
29555  */
29556  if( pFile->ctrlFlags & UNIXFILE_DIRSYNC ){
29557    int dirfd;
29558    OSTRACE(("DIRSYNC %s (have_fullfsync=%d fullsync=%d)\n", pFile->zPath,
29559            HAVE_FULLFSYNC, isFullsync));
29560    rc = osOpenDirectory(pFile->zPath, &dirfd);
29561    if( rc==SQLITE_OK && dirfd>=0 ){
29562      full_fsync(dirfd, 0, 0);
29563      robust_close(pFile, dirfd, __LINE__);
29564    }else if( rc==SQLITE_CANTOPEN ){
29565      rc = SQLITE_OK;
29566    }
29567    pFile->ctrlFlags &= ~UNIXFILE_DIRSYNC;
29568  }
29569  return rc;
29570}
29571
29572/*
29573** Truncate an open file to a specified size
29574*/
29575static int unixTruncate(sqlite3_file *id, i64 nByte){
29576  unixFile *pFile = (unixFile *)id;
29577  int rc;
29578  assert( pFile );
29579  SimulateIOError( return SQLITE_IOERR_TRUNCATE );
29580
29581  /* If the user has configured a chunk-size for this file, truncate the
29582  ** file so that it consists of an integer number of chunks (i.e. the
29583  ** actual file size after the operation may be larger than the requested
29584  ** size).
29585  */
29586  if( pFile->szChunk>0 ){
29587    nByte = ((nByte + pFile->szChunk - 1)/pFile->szChunk) * pFile->szChunk;
29588  }
29589
29590  rc = robust_ftruncate(pFile->h, nByte);
29591  if( rc ){
29592    storeLastErrno(pFile, errno);
29593    return unixLogError(SQLITE_IOERR_TRUNCATE, "ftruncate", pFile->zPath);
29594  }else{
29595#ifdef SQLITE_DEBUG
29596    /* If we are doing a normal write to a database file (as opposed to
29597    ** doing a hot-journal rollback or a write to some file other than a
29598    ** normal database file) and we truncate the file to zero length,
29599    ** that effectively updates the change counter.  This might happen
29600    ** when restoring a database using the backup API from a zero-length
29601    ** source.
29602    */
29603    if( pFile->inNormalWrite && nByte==0 ){
29604      pFile->transCntrChng = 1;
29605    }
29606#endif
29607
29608#if SQLITE_MAX_MMAP_SIZE>0
29609    /* If the file was just truncated to a size smaller than the currently
29610    ** mapped region, reduce the effective mapping size as well. SQLite will
29611    ** use read() and write() to access data beyond this point from now on.
29612    */
29613    if( nByte<pFile->mmapSize ){
29614      pFile->mmapSize = nByte;
29615    }
29616#endif
29617
29618    return SQLITE_OK;
29619  }
29620}
29621
29622/*
29623** Determine the current size of a file in bytes
29624*/
29625static int unixFileSize(sqlite3_file *id, i64 *pSize){
29626  int rc;
29627  struct stat buf;
29628  assert( id );
29629  rc = osFstat(((unixFile*)id)->h, &buf);
29630  SimulateIOError( rc=1 );
29631  if( rc!=0 ){
29632    storeLastErrno((unixFile*)id, errno);
29633    return SQLITE_IOERR_FSTAT;
29634  }
29635  *pSize = buf.st_size;
29636
29637  /* When opening a zero-size database, the findInodeInfo() procedure
29638  ** writes a single byte into that file in order to work around a bug
29639  ** in the OS-X msdos filesystem.  In order to avoid problems with upper
29640  ** layers, we need to report this file size as zero even though it is
29641  ** really 1.   Ticket #3260.
29642  */
29643  if( *pSize==1 ) *pSize = 0;
29644
29645
29646  return SQLITE_OK;
29647}
29648
29649#if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__)
29650/*
29651** Handler for proxy-locking file-control verbs.  Defined below in the
29652** proxying locking division.
29653*/
29654static int proxyFileControl(sqlite3_file*,int,void*);
29655#endif
29656
29657/*
29658** This function is called to handle the SQLITE_FCNTL_SIZE_HINT
29659** file-control operation.  Enlarge the database to nBytes in size
29660** (rounded up to the next chunk-size).  If the database is already
29661** nBytes or larger, this routine is a no-op.
29662*/
29663static int fcntlSizeHint(unixFile *pFile, i64 nByte){
29664  if( pFile->szChunk>0 ){
29665    i64 nSize;                    /* Required file size */
29666    struct stat buf;              /* Used to hold return values of fstat() */
29667
29668    if( osFstat(pFile->h, &buf) ){
29669      return SQLITE_IOERR_FSTAT;
29670    }
29671
29672    nSize = ((nByte+pFile->szChunk-1) / pFile->szChunk) * pFile->szChunk;
29673    if( nSize>(i64)buf.st_size ){
29674
29675#if defined(HAVE_POSIX_FALLOCATE) && HAVE_POSIX_FALLOCATE
29676      /* The code below is handling the return value of osFallocate()
29677      ** correctly. posix_fallocate() is defined to "returns zero on success,
29678      ** or an error number on  failure". See the manpage for details. */
29679      int err;
29680      do{
29681        err = osFallocate(pFile->h, buf.st_size, nSize-buf.st_size);
29682      }while( err==EINTR );
29683      if( err ) return SQLITE_IOERR_WRITE;
29684#else
29685      /* If the OS does not have posix_fallocate(), fake it. Write a
29686      ** single byte to the last byte in each block that falls entirely
29687      ** within the extended region. Then, if required, a single byte
29688      ** at offset (nSize-1), to set the size of the file correctly.
29689      ** This is a similar technique to that used by glibc on systems
29690      ** that do not have a real fallocate() call.
29691      */
29692      int nBlk = buf.st_blksize;  /* File-system block size */
29693      int nWrite = 0;             /* Number of bytes written by seekAndWrite */
29694      i64 iWrite;                 /* Next offset to write to */
29695
29696      iWrite = ((buf.st_size + 2*nBlk - 1)/nBlk)*nBlk-1;
29697      assert( iWrite>=buf.st_size );
29698      assert( (iWrite/nBlk)==((buf.st_size+nBlk-1)/nBlk) );
29699      assert( ((iWrite+1)%nBlk)==0 );
29700      for(/*no-op*/; iWrite<nSize; iWrite+=nBlk ){
29701        nWrite = seekAndWrite(pFile, iWrite, "", 1);
29702        if( nWrite!=1 ) return SQLITE_IOERR_WRITE;
29703      }
29704      if( nWrite==0 || (nSize%nBlk) ){
29705        nWrite = seekAndWrite(pFile, nSize-1, "", 1);
29706        if( nWrite!=1 ) return SQLITE_IOERR_WRITE;
29707      }
29708#endif
29709    }
29710  }
29711
29712#if SQLITE_MAX_MMAP_SIZE>0
29713  if( pFile->mmapSizeMax>0 && nByte>pFile->mmapSize ){
29714    int rc;
29715    if( pFile->szChunk<=0 ){
29716      if( robust_ftruncate(pFile->h, nByte) ){
29717        storeLastErrno(pFile, errno);
29718        return unixLogError(SQLITE_IOERR_TRUNCATE, "ftruncate", pFile->zPath);
29719      }
29720    }
29721
29722    rc = unixMapfile(pFile, nByte);
29723    return rc;
29724  }
29725#endif
29726
29727  return SQLITE_OK;
29728}
29729
29730/*
29731** If *pArg is initially negative then this is a query.  Set *pArg to
29732** 1 or 0 depending on whether or not bit mask of pFile->ctrlFlags is set.
29733**
29734** If *pArg is 0 or 1, then clear or set the mask bit of pFile->ctrlFlags.
29735*/
29736static void unixModeBit(unixFile *pFile, unsigned char mask, int *pArg){
29737  if( *pArg<0 ){
29738    *pArg = (pFile->ctrlFlags & mask)!=0;
29739  }else if( (*pArg)==0 ){
29740    pFile->ctrlFlags &= ~mask;
29741  }else{
29742    pFile->ctrlFlags |= mask;
29743  }
29744}
29745
29746/* Forward declaration */
29747static int unixGetTempname(int nBuf, char *zBuf);
29748
29749/*
29750** Information and control of an open file handle.
29751*/
29752static int unixFileControl(sqlite3_file *id, int op, void *pArg){
29753  unixFile *pFile = (unixFile*)id;
29754  switch( op ){
29755    case SQLITE_FCNTL_WAL_BLOCK: {
29756      /* pFile->ctrlFlags |= UNIXFILE_BLOCK; // Deferred feature */
29757      return SQLITE_OK;
29758    }
29759    case SQLITE_FCNTL_LOCKSTATE: {
29760      *(int*)pArg = pFile->eFileLock;
29761      return SQLITE_OK;
29762    }
29763    case SQLITE_FCNTL_LAST_ERRNO: {
29764      *(int*)pArg = pFile->lastErrno;
29765      return SQLITE_OK;
29766    }
29767    case SQLITE_FCNTL_CHUNK_SIZE: {
29768      pFile->szChunk = *(int *)pArg;
29769      return SQLITE_OK;
29770    }
29771    case SQLITE_FCNTL_SIZE_HINT: {
29772      int rc;
29773      SimulateIOErrorBenign(1);
29774      rc = fcntlSizeHint(pFile, *(i64 *)pArg);
29775      SimulateIOErrorBenign(0);
29776      return rc;
29777    }
29778    case SQLITE_FCNTL_PERSIST_WAL: {
29779      unixModeBit(pFile, UNIXFILE_PERSIST_WAL, (int*)pArg);
29780      return SQLITE_OK;
29781    }
29782    case SQLITE_FCNTL_POWERSAFE_OVERWRITE: {
29783      unixModeBit(pFile, UNIXFILE_PSOW, (int*)pArg);
29784      return SQLITE_OK;
29785    }
29786    case SQLITE_FCNTL_VFSNAME: {
29787      *(char**)pArg = sqlite3_mprintf("%s", pFile->pVfs->zName);
29788      return SQLITE_OK;
29789    }
29790    case SQLITE_FCNTL_TEMPFILENAME: {
29791      char *zTFile = sqlite3_malloc64( pFile->pVfs->mxPathname );
29792      if( zTFile ){
29793        unixGetTempname(pFile->pVfs->mxPathname, zTFile);
29794        *(char**)pArg = zTFile;
29795      }
29796      return SQLITE_OK;
29797    }
29798    case SQLITE_FCNTL_HAS_MOVED: {
29799      *(int*)pArg = fileHasMoved(pFile);
29800      return SQLITE_OK;
29801    }
29802#if SQLITE_MAX_MMAP_SIZE>0
29803    case SQLITE_FCNTL_MMAP_SIZE: {
29804      i64 newLimit = *(i64*)pArg;
29805      int rc = SQLITE_OK;
29806      if( newLimit>sqlite3GlobalConfig.mxMmap ){
29807        newLimit = sqlite3GlobalConfig.mxMmap;
29808      }
29809      *(i64*)pArg = pFile->mmapSizeMax;
29810      if( newLimit>=0 && newLimit!=pFile->mmapSizeMax && pFile->nFetchOut==0 ){
29811        pFile->mmapSizeMax = newLimit;
29812        if( pFile->mmapSize>0 ){
29813          unixUnmapfile(pFile);
29814          rc = unixMapfile(pFile, -1);
29815        }
29816      }
29817      return rc;
29818    }
29819#endif
29820#ifdef SQLITE_DEBUG
29821    /* The pager calls this method to signal that it has done
29822    ** a rollback and that the database is therefore unchanged and
29823    ** it hence it is OK for the transaction change counter to be
29824    ** unchanged.
29825    */
29826    case SQLITE_FCNTL_DB_UNCHANGED: {
29827      ((unixFile*)id)->dbUpdate = 0;
29828      return SQLITE_OK;
29829    }
29830#endif
29831#if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__)
29832    case SQLITE_FCNTL_SET_LOCKPROXYFILE:
29833    case SQLITE_FCNTL_GET_LOCKPROXYFILE: {
29834      return proxyFileControl(id,op,pArg);
29835    }
29836#endif /* SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__) */
29837  }
29838  return SQLITE_NOTFOUND;
29839}
29840
29841/*
29842** Return the sector size in bytes of the underlying block device for
29843** the specified file. This is almost always 512 bytes, but may be
29844** larger for some devices.
29845**
29846** SQLite code assumes this function cannot fail. It also assumes that
29847** if two files are created in the same file-system directory (i.e.
29848** a database and its journal file) that the sector size will be the
29849** same for both.
29850*/
29851#ifndef __QNXNTO__
29852static int unixSectorSize(sqlite3_file *NotUsed){
29853  UNUSED_PARAMETER(NotUsed);
29854  return SQLITE_DEFAULT_SECTOR_SIZE;
29855}
29856#endif
29857
29858/*
29859** The following version of unixSectorSize() is optimized for QNX.
29860*/
29861#ifdef __QNXNTO__
29862#include <sys/dcmd_blk.h>
29863#include <sys/statvfs.h>
29864static int unixSectorSize(sqlite3_file *id){
29865  unixFile *pFile = (unixFile*)id;
29866  if( pFile->sectorSize == 0 ){
29867    struct statvfs fsInfo;
29868
29869    /* Set defaults for non-supported filesystems */
29870    pFile->sectorSize = SQLITE_DEFAULT_SECTOR_SIZE;
29871    pFile->deviceCharacteristics = 0;
29872    if( fstatvfs(pFile->h, &fsInfo) == -1 ) {
29873      return pFile->sectorSize;
29874    }
29875
29876    if( !strcmp(fsInfo.f_basetype, "tmp") ) {
29877      pFile->sectorSize = fsInfo.f_bsize;
29878      pFile->deviceCharacteristics =
29879        SQLITE_IOCAP_ATOMIC4K |       /* All ram filesystem writes are atomic */
29880        SQLITE_IOCAP_SAFE_APPEND |    /* growing the file does not occur until
29881                                      ** the write succeeds */
29882        SQLITE_IOCAP_SEQUENTIAL |     /* The ram filesystem has no write behind
29883                                      ** so it is ordered */
29884        0;
29885    }else if( strstr(fsInfo.f_basetype, "etfs") ){
29886      pFile->sectorSize = fsInfo.f_bsize;
29887      pFile->deviceCharacteristics =
29888        /* etfs cluster size writes are atomic */
29889        (pFile->sectorSize / 512 * SQLITE_IOCAP_ATOMIC512) |
29890        SQLITE_IOCAP_SAFE_APPEND |    /* growing the file does not occur until
29891                                      ** the write succeeds */
29892        SQLITE_IOCAP_SEQUENTIAL |     /* The ram filesystem has no write behind
29893                                      ** so it is ordered */
29894        0;
29895    }else if( !strcmp(fsInfo.f_basetype, "qnx6") ){
29896      pFile->sectorSize = fsInfo.f_bsize;
29897      pFile->deviceCharacteristics =
29898        SQLITE_IOCAP_ATOMIC |         /* All filesystem writes are atomic */
29899        SQLITE_IOCAP_SAFE_APPEND |    /* growing the file does not occur until
29900                                      ** the write succeeds */
29901        SQLITE_IOCAP_SEQUENTIAL |     /* The ram filesystem has no write behind
29902                                      ** so it is ordered */
29903        0;
29904    }else if( !strcmp(fsInfo.f_basetype, "qnx4") ){
29905      pFile->sectorSize = fsInfo.f_bsize;
29906      pFile->deviceCharacteristics =
29907        /* full bitset of atomics from max sector size and smaller */
29908        ((pFile->sectorSize / 512 * SQLITE_IOCAP_ATOMIC512) << 1) - 2 |
29909        SQLITE_IOCAP_SEQUENTIAL |     /* The ram filesystem has no write behind
29910                                      ** so it is ordered */
29911        0;
29912    }else if( strstr(fsInfo.f_basetype, "dos") ){
29913      pFile->sectorSize = fsInfo.f_bsize;
29914      pFile->deviceCharacteristics =
29915        /* full bitset of atomics from max sector size and smaller */
29916        ((pFile->sectorSize / 512 * SQLITE_IOCAP_ATOMIC512) << 1) - 2 |
29917        SQLITE_IOCAP_SEQUENTIAL |     /* The ram filesystem has no write behind
29918                                      ** so it is ordered */
29919        0;
29920    }else{
29921      pFile->deviceCharacteristics =
29922        SQLITE_IOCAP_ATOMIC512 |      /* blocks are atomic */
29923        SQLITE_IOCAP_SAFE_APPEND |    /* growing the file does not occur until
29924                                      ** the write succeeds */
29925        0;
29926    }
29927  }
29928  /* Last chance verification.  If the sector size isn't a multiple of 512
29929  ** then it isn't valid.*/
29930  if( pFile->sectorSize % 512 != 0 ){
29931    pFile->deviceCharacteristics = 0;
29932    pFile->sectorSize = SQLITE_DEFAULT_SECTOR_SIZE;
29933  }
29934  return pFile->sectorSize;
29935}
29936#endif /* __QNXNTO__ */
29937
29938/*
29939** Return the device characteristics for the file.
29940**
29941** This VFS is set up to return SQLITE_IOCAP_POWERSAFE_OVERWRITE by default.
29942** However, that choice is controversial since technically the underlying
29943** file system does not always provide powersafe overwrites.  (In other
29944** words, after a power-loss event, parts of the file that were never
29945** written might end up being altered.)  However, non-PSOW behavior is very,
29946** very rare.  And asserting PSOW makes a large reduction in the amount
29947** of required I/O for journaling, since a lot of padding is eliminated.
29948**  Hence, while POWERSAFE_OVERWRITE is on by default, there is a file-control
29949** available to turn it off and URI query parameter available to turn it off.
29950*/
29951static int unixDeviceCharacteristics(sqlite3_file *id){
29952  unixFile *p = (unixFile*)id;
29953  int rc = 0;
29954#ifdef __QNXNTO__
29955  if( p->sectorSize==0 ) unixSectorSize(id);
29956  rc = p->deviceCharacteristics;
29957#endif
29958  if( p->ctrlFlags & UNIXFILE_PSOW ){
29959    rc |= SQLITE_IOCAP_POWERSAFE_OVERWRITE;
29960  }
29961  return rc;
29962}
29963
29964#if !defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0
29965
29966/*
29967** Return the system page size.
29968**
29969** This function should not be called directly by other code in this file.
29970** Instead, it should be called via macro osGetpagesize().
29971*/
29972static int unixGetpagesize(void){
29973#if OS_VXWORKS
29974  return 1024;
29975#elif defined(_BSD_SOURCE)
29976  return getpagesize();
29977#else
29978  return (int)sysconf(_SC_PAGESIZE);
29979#endif
29980}
29981
29982#endif /* !defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0 */
29983
29984#ifndef SQLITE_OMIT_WAL
29985
29986/*
29987** Object used to represent an shared memory buffer.
29988**
29989** When multiple threads all reference the same wal-index, each thread
29990** has its own unixShm object, but they all point to a single instance
29991** of this unixShmNode object.  In other words, each wal-index is opened
29992** only once per process.
29993**
29994** Each unixShmNode object is connected to a single unixInodeInfo object.
29995** We could coalesce this object into unixInodeInfo, but that would mean
29996** every open file that does not use shared memory (in other words, most
29997** open files) would have to carry around this extra information.  So
29998** the unixInodeInfo object contains a pointer to this unixShmNode object
29999** and the unixShmNode object is created only when needed.
30000**
30001** unixMutexHeld() must be true when creating or destroying
30002** this object or while reading or writing the following fields:
30003**
30004**      nRef
30005**
30006** The following fields are read-only after the object is created:
30007**
30008**      fid
30009**      zFilename
30010**
30011** Either unixShmNode.mutex must be held or unixShmNode.nRef==0 and
30012** unixMutexHeld() is true when reading or writing any other field
30013** in this structure.
30014*/
30015struct unixShmNode {
30016  unixInodeInfo *pInode;     /* unixInodeInfo that owns this SHM node */
30017  sqlite3_mutex *mutex;      /* Mutex to access this object */
30018  char *zFilename;           /* Name of the mmapped file */
30019  int h;                     /* Open file descriptor */
30020  int szRegion;              /* Size of shared-memory regions */
30021  u16 nRegion;               /* Size of array apRegion */
30022  u8 isReadonly;             /* True if read-only */
30023  char **apRegion;           /* Array of mapped shared-memory regions */
30024  int nRef;                  /* Number of unixShm objects pointing to this */
30025  unixShm *pFirst;           /* All unixShm objects pointing to this */
30026#ifdef SQLITE_DEBUG
30027  u8 exclMask;               /* Mask of exclusive locks held */
30028  u8 sharedMask;             /* Mask of shared locks held */
30029  u8 nextShmId;              /* Next available unixShm.id value */
30030#endif
30031};
30032
30033/*
30034** Structure used internally by this VFS to record the state of an
30035** open shared memory connection.
30036**
30037** The following fields are initialized when this object is created and
30038** are read-only thereafter:
30039**
30040**    unixShm.pFile
30041**    unixShm.id
30042**
30043** All other fields are read/write.  The unixShm.pFile->mutex must be held
30044** while accessing any read/write fields.
30045*/
30046struct unixShm {
30047  unixShmNode *pShmNode;     /* The underlying unixShmNode object */
30048  unixShm *pNext;            /* Next unixShm with the same unixShmNode */
30049  u8 hasMutex;               /* True if holding the unixShmNode mutex */
30050  u8 id;                     /* Id of this connection within its unixShmNode */
30051  u16 sharedMask;            /* Mask of shared locks held */
30052  u16 exclMask;              /* Mask of exclusive locks held */
30053};
30054
30055/*
30056** Constants used for locking
30057*/
30058#define UNIX_SHM_BASE   ((22+SQLITE_SHM_NLOCK)*4)         /* first lock byte */
30059#define UNIX_SHM_DMS    (UNIX_SHM_BASE+SQLITE_SHM_NLOCK)  /* deadman switch */
30060
30061/*
30062** Apply posix advisory locks for all bytes from ofst through ofst+n-1.
30063**
30064** Locks block if the mask is exactly UNIX_SHM_C and are non-blocking
30065** otherwise.
30066*/
30067static int unixShmSystemLock(
30068  unixFile *pFile,       /* Open connection to the WAL file */
30069  int lockType,          /* F_UNLCK, F_RDLCK, or F_WRLCK */
30070  int ofst,              /* First byte of the locking range */
30071  int n                  /* Number of bytes to lock */
30072){
30073  unixShmNode *pShmNode; /* Apply locks to this open shared-memory segment */
30074  struct flock f;        /* The posix advisory locking structure */
30075  int rc = SQLITE_OK;    /* Result code form fcntl() */
30076
30077  /* Access to the unixShmNode object is serialized by the caller */
30078  pShmNode = pFile->pInode->pShmNode;
30079  assert( sqlite3_mutex_held(pShmNode->mutex) || pShmNode->nRef==0 );
30080
30081  /* Shared locks never span more than one byte */
30082  assert( n==1 || lockType!=F_RDLCK );
30083
30084  /* Locks are within range */
30085  assert( n>=1 && n<SQLITE_SHM_NLOCK );
30086
30087  if( pShmNode->h>=0 ){
30088    int lkType;
30089    /* Initialize the locking parameters */
30090    memset(&f, 0, sizeof(f));
30091    f.l_type = lockType;
30092    f.l_whence = SEEK_SET;
30093    f.l_start = ofst;
30094    f.l_len = n;
30095
30096    lkType = (pFile->ctrlFlags & UNIXFILE_BLOCK)!=0 ? F_SETLKW : F_SETLK;
30097    rc = osFcntl(pShmNode->h, lkType, &f);
30098    rc = (rc!=(-1)) ? SQLITE_OK : SQLITE_BUSY;
30099    pFile->ctrlFlags &= ~UNIXFILE_BLOCK;
30100  }
30101
30102  /* Update the global lock state and do debug tracing */
30103#ifdef SQLITE_DEBUG
30104  { u16 mask;
30105  OSTRACE(("SHM-LOCK "));
30106  mask = ofst>31 ? 0xffff : (1<<(ofst+n)) - (1<<ofst);
30107  if( rc==SQLITE_OK ){
30108    if( lockType==F_UNLCK ){
30109      OSTRACE(("unlock %d ok", ofst));
30110      pShmNode->exclMask &= ~mask;
30111      pShmNode->sharedMask &= ~mask;
30112    }else if( lockType==F_RDLCK ){
30113      OSTRACE(("read-lock %d ok", ofst));
30114      pShmNode->exclMask &= ~mask;
30115      pShmNode->sharedMask |= mask;
30116    }else{
30117      assert( lockType==F_WRLCK );
30118      OSTRACE(("write-lock %d ok", ofst));
30119      pShmNode->exclMask |= mask;
30120      pShmNode->sharedMask &= ~mask;
30121    }
30122  }else{
30123    if( lockType==F_UNLCK ){
30124      OSTRACE(("unlock %d failed", ofst));
30125    }else if( lockType==F_RDLCK ){
30126      OSTRACE(("read-lock failed"));
30127    }else{
30128      assert( lockType==F_WRLCK );
30129      OSTRACE(("write-lock %d failed", ofst));
30130    }
30131  }
30132  OSTRACE((" - afterwards %03x,%03x\n",
30133           pShmNode->sharedMask, pShmNode->exclMask));
30134  }
30135#endif
30136
30137  return rc;
30138}
30139
30140/*
30141** Return the minimum number of 32KB shm regions that should be mapped at
30142** a time, assuming that each mapping must be an integer multiple of the
30143** current system page-size.
30144**
30145** Usually, this is 1. The exception seems to be systems that are configured
30146** to use 64KB pages - in this case each mapping must cover at least two
30147** shm regions.
30148*/
30149static int unixShmRegionPerMap(void){
30150  int shmsz = 32*1024;            /* SHM region size */
30151  int pgsz = osGetpagesize();   /* System page size */
30152  assert( ((pgsz-1)&pgsz)==0 );   /* Page size must be a power of 2 */
30153  if( pgsz<shmsz ) return 1;
30154  return pgsz/shmsz;
30155}
30156
30157/*
30158** Purge the unixShmNodeList list of all entries with unixShmNode.nRef==0.
30159**
30160** This is not a VFS shared-memory method; it is a utility function called
30161** by VFS shared-memory methods.
30162*/
30163static void unixShmPurge(unixFile *pFd){
30164  unixShmNode *p = pFd->pInode->pShmNode;
30165  assert( unixMutexHeld() );
30166  if( p && p->nRef==0 ){
30167    int nShmPerMap = unixShmRegionPerMap();
30168    int i;
30169    assert( p->pInode==pFd->pInode );
30170    sqlite3_mutex_free(p->mutex);
30171    for(i=0; i<p->nRegion; i+=nShmPerMap){
30172      if( p->h>=0 ){
30173        osMunmap(p->apRegion[i], p->szRegion);
30174      }else{
30175        sqlite3_free(p->apRegion[i]);
30176      }
30177    }
30178    sqlite3_free(p->apRegion);
30179    if( p->h>=0 ){
30180      robust_close(pFd, p->h, __LINE__);
30181      p->h = -1;
30182    }
30183    p->pInode->pShmNode = 0;
30184    sqlite3_free(p);
30185  }
30186}
30187
30188/*
30189** Open a shared-memory area associated with open database file pDbFd.
30190** This particular implementation uses mmapped files.
30191**
30192** The file used to implement shared-memory is in the same directory
30193** as the open database file and has the same name as the open database
30194** file with the "-shm" suffix added.  For example, if the database file
30195** is "/home/user1/config.db" then the file that is created and mmapped
30196** for shared memory will be called "/home/user1/config.db-shm".
30197**
30198** Another approach to is to use files in /dev/shm or /dev/tmp or an
30199** some other tmpfs mount. But if a file in a different directory
30200** from the database file is used, then differing access permissions
30201** or a chroot() might cause two different processes on the same
30202** database to end up using different files for shared memory -
30203** meaning that their memory would not really be shared - resulting
30204** in database corruption.  Nevertheless, this tmpfs file usage
30205** can be enabled at compile-time using -DSQLITE_SHM_DIRECTORY="/dev/shm"
30206** or the equivalent.  The use of the SQLITE_SHM_DIRECTORY compile-time
30207** option results in an incompatible build of SQLite;  builds of SQLite
30208** that with differing SQLITE_SHM_DIRECTORY settings attempt to use the
30209** same database file at the same time, database corruption will likely
30210** result. The SQLITE_SHM_DIRECTORY compile-time option is considered
30211** "unsupported" and may go away in a future SQLite release.
30212**
30213** When opening a new shared-memory file, if no other instances of that
30214** file are currently open, in this process or in other processes, then
30215** the file must be truncated to zero length or have its header cleared.
30216**
30217** If the original database file (pDbFd) is using the "unix-excl" VFS
30218** that means that an exclusive lock is held on the database file and
30219** that no other processes are able to read or write the database.  In
30220** that case, we do not really need shared memory.  No shared memory
30221** file is created.  The shared memory will be simulated with heap memory.
30222*/
30223static int unixOpenSharedMemory(unixFile *pDbFd){
30224  struct unixShm *p = 0;          /* The connection to be opened */
30225  struct unixShmNode *pShmNode;   /* The underlying mmapped file */
30226  int rc;                         /* Result code */
30227  unixInodeInfo *pInode;          /* The inode of fd */
30228  char *zShmFilename;             /* Name of the file used for SHM */
30229  int nShmFilename;               /* Size of the SHM filename in bytes */
30230
30231  /* Allocate space for the new unixShm object. */
30232  p = sqlite3_malloc64( sizeof(*p) );
30233  if( p==0 ) return SQLITE_NOMEM;
30234  memset(p, 0, sizeof(*p));
30235  assert( pDbFd->pShm==0 );
30236
30237  /* Check to see if a unixShmNode object already exists. Reuse an existing
30238  ** one if present. Create a new one if necessary.
30239  */
30240  unixEnterMutex();
30241  pInode = pDbFd->pInode;
30242  pShmNode = pInode->pShmNode;
30243  if( pShmNode==0 ){
30244    struct stat sStat;                 /* fstat() info for database file */
30245#ifndef SQLITE_SHM_DIRECTORY
30246    const char *zBasePath = pDbFd->zPath;
30247#endif
30248
30249    /* Call fstat() to figure out the permissions on the database file. If
30250    ** a new *-shm file is created, an attempt will be made to create it
30251    ** with the same permissions.
30252    */
30253    if( osFstat(pDbFd->h, &sStat) && pInode->bProcessLock==0 ){
30254      rc = SQLITE_IOERR_FSTAT;
30255      goto shm_open_err;
30256    }
30257
30258#ifdef SQLITE_SHM_DIRECTORY
30259    nShmFilename = sizeof(SQLITE_SHM_DIRECTORY) + 31;
30260#else
30261    nShmFilename = 6 + (int)strlen(zBasePath);
30262#endif
30263    pShmNode = sqlite3_malloc64( sizeof(*pShmNode) + nShmFilename );
30264    if( pShmNode==0 ){
30265      rc = SQLITE_NOMEM;
30266      goto shm_open_err;
30267    }
30268    memset(pShmNode, 0, sizeof(*pShmNode)+nShmFilename);
30269    zShmFilename = pShmNode->zFilename = (char*)&pShmNode[1];
30270#ifdef SQLITE_SHM_DIRECTORY
30271    sqlite3_snprintf(nShmFilename, zShmFilename,
30272                     SQLITE_SHM_DIRECTORY "/sqlite-shm-%x-%x",
30273                     (u32)sStat.st_ino, (u32)sStat.st_dev);
30274#else
30275    sqlite3_snprintf(nShmFilename, zShmFilename, "%s-shm", zBasePath);
30276    sqlite3FileSuffix3(pDbFd->zPath, zShmFilename);
30277#endif
30278    pShmNode->h = -1;
30279    pDbFd->pInode->pShmNode = pShmNode;
30280    pShmNode->pInode = pDbFd->pInode;
30281    pShmNode->mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST);
30282    if( pShmNode->mutex==0 ){
30283      rc = SQLITE_NOMEM;
30284      goto shm_open_err;
30285    }
30286
30287    if( pInode->bProcessLock==0 ){
30288      int openFlags = O_RDWR | O_CREAT;
30289      if( sqlite3_uri_boolean(pDbFd->zPath, "readonly_shm", 0) ){
30290        openFlags = O_RDONLY;
30291        pShmNode->isReadonly = 1;
30292      }
30293      pShmNode->h = robust_open(zShmFilename, openFlags, (sStat.st_mode&0777));
30294      if( pShmNode->h<0 ){
30295        rc = unixLogError(SQLITE_CANTOPEN_BKPT, "open", zShmFilename);
30296        goto shm_open_err;
30297      }
30298
30299      /* If this process is running as root, make sure that the SHM file
30300      ** is owned by the same user that owns the original database.  Otherwise,
30301      ** the original owner will not be able to connect.
30302      */
30303      osFchown(pShmNode->h, sStat.st_uid, sStat.st_gid);
30304
30305      /* Check to see if another process is holding the dead-man switch.
30306      ** If not, truncate the file to zero length.
30307      */
30308      rc = SQLITE_OK;
30309      if( unixShmSystemLock(pDbFd, F_WRLCK, UNIX_SHM_DMS, 1)==SQLITE_OK ){
30310        if( robust_ftruncate(pShmNode->h, 0) ){
30311          rc = unixLogError(SQLITE_IOERR_SHMOPEN, "ftruncate", zShmFilename);
30312        }
30313      }
30314      if( rc==SQLITE_OK ){
30315        rc = unixShmSystemLock(pDbFd, F_RDLCK, UNIX_SHM_DMS, 1);
30316      }
30317      if( rc ) goto shm_open_err;
30318    }
30319  }
30320
30321  /* Make the new connection a child of the unixShmNode */
30322  p->pShmNode = pShmNode;
30323#ifdef SQLITE_DEBUG
30324  p->id = pShmNode->nextShmId++;
30325#endif
30326  pShmNode->nRef++;
30327  pDbFd->pShm = p;
30328  unixLeaveMutex();
30329
30330  /* The reference count on pShmNode has already been incremented under
30331  ** the cover of the unixEnterMutex() mutex and the pointer from the
30332  ** new (struct unixShm) object to the pShmNode has been set. All that is
30333  ** left to do is to link the new object into the linked list starting
30334  ** at pShmNode->pFirst. This must be done while holding the pShmNode->mutex
30335  ** mutex.
30336  */
30337  sqlite3_mutex_enter(pShmNode->mutex);
30338  p->pNext = pShmNode->pFirst;
30339  pShmNode->pFirst = p;
30340  sqlite3_mutex_leave(pShmNode->mutex);
30341  return SQLITE_OK;
30342
30343  /* Jump here on any error */
30344shm_open_err:
30345  unixShmPurge(pDbFd);       /* This call frees pShmNode if required */
30346  sqlite3_free(p);
30347  unixLeaveMutex();
30348  return rc;
30349}
30350
30351/*
30352** This function is called to obtain a pointer to region iRegion of the
30353** shared-memory associated with the database file fd. Shared-memory regions
30354** are numbered starting from zero. Each shared-memory region is szRegion
30355** bytes in size.
30356**
30357** If an error occurs, an error code is returned and *pp is set to NULL.
30358**
30359** Otherwise, if the bExtend parameter is 0 and the requested shared-memory
30360** region has not been allocated (by any client, including one running in a
30361** separate process), then *pp is set to NULL and SQLITE_OK returned. If
30362** bExtend is non-zero and the requested shared-memory region has not yet
30363** been allocated, it is allocated by this function.
30364**
30365** If the shared-memory region has already been allocated or is allocated by
30366** this call as described above, then it is mapped into this processes
30367** address space (if it is not already), *pp is set to point to the mapped
30368** memory and SQLITE_OK returned.
30369*/
30370static int unixShmMap(
30371  sqlite3_file *fd,               /* Handle open on database file */
30372  int iRegion,                    /* Region to retrieve */
30373  int szRegion,                   /* Size of regions */
30374  int bExtend,                    /* True to extend file if necessary */
30375  void volatile **pp              /* OUT: Mapped memory */
30376){
30377  unixFile *pDbFd = (unixFile*)fd;
30378  unixShm *p;
30379  unixShmNode *pShmNode;
30380  int rc = SQLITE_OK;
30381  int nShmPerMap = unixShmRegionPerMap();
30382  int nReqRegion;
30383
30384  /* If the shared-memory file has not yet been opened, open it now. */
30385  if( pDbFd->pShm==0 ){
30386    rc = unixOpenSharedMemory(pDbFd);
30387    if( rc!=SQLITE_OK ) return rc;
30388  }
30389
30390  p = pDbFd->pShm;
30391  pShmNode = p->pShmNode;
30392  sqlite3_mutex_enter(pShmNode->mutex);
30393  assert( szRegion==pShmNode->szRegion || pShmNode->nRegion==0 );
30394  assert( pShmNode->pInode==pDbFd->pInode );
30395  assert( pShmNode->h>=0 || pDbFd->pInode->bProcessLock==1 );
30396  assert( pShmNode->h<0 || pDbFd->pInode->bProcessLock==0 );
30397
30398  /* Minimum number of regions required to be mapped. */
30399  nReqRegion = ((iRegion+nShmPerMap) / nShmPerMap) * nShmPerMap;
30400
30401  if( pShmNode->nRegion<nReqRegion ){
30402    char **apNew;                      /* New apRegion[] array */
30403    int nByte = nReqRegion*szRegion;   /* Minimum required file size */
30404    struct stat sStat;                 /* Used by fstat() */
30405
30406    pShmNode->szRegion = szRegion;
30407
30408    if( pShmNode->h>=0 ){
30409      /* The requested region is not mapped into this processes address space.
30410      ** Check to see if it has been allocated (i.e. if the wal-index file is
30411      ** large enough to contain the requested region).
30412      */
30413      if( osFstat(pShmNode->h, &sStat) ){
30414        rc = SQLITE_IOERR_SHMSIZE;
30415        goto shmpage_out;
30416      }
30417
30418      if( sStat.st_size<nByte ){
30419        /* The requested memory region does not exist. If bExtend is set to
30420        ** false, exit early. *pp will be set to NULL and SQLITE_OK returned.
30421        */
30422        if( !bExtend ){
30423          goto shmpage_out;
30424        }
30425
30426        /* Alternatively, if bExtend is true, extend the file. Do this by
30427        ** writing a single byte to the end of each (OS) page being
30428        ** allocated or extended. Technically, we need only write to the
30429        ** last page in order to extend the file. But writing to all new
30430        ** pages forces the OS to allocate them immediately, which reduces
30431        ** the chances of SIGBUS while accessing the mapped region later on.
30432        */
30433        else{
30434          static const int pgsz = 4096;
30435          int iPg;
30436
30437          /* Write to the last byte of each newly allocated or extended page */
30438          assert( (nByte % pgsz)==0 );
30439          for(iPg=(sStat.st_size/pgsz); iPg<(nByte/pgsz); iPg++){
30440            if( seekAndWriteFd(pShmNode->h, iPg*pgsz + pgsz-1, "", 1, 0)!=1 ){
30441              const char *zFile = pShmNode->zFilename;
30442              rc = unixLogError(SQLITE_IOERR_SHMSIZE, "write", zFile);
30443              goto shmpage_out;
30444            }
30445          }
30446        }
30447      }
30448    }
30449
30450    /* Map the requested memory region into this processes address space. */
30451    apNew = (char **)sqlite3_realloc(
30452        pShmNode->apRegion, nReqRegion*sizeof(char *)
30453    );
30454    if( !apNew ){
30455      rc = SQLITE_IOERR_NOMEM;
30456      goto shmpage_out;
30457    }
30458    pShmNode->apRegion = apNew;
30459    while( pShmNode->nRegion<nReqRegion ){
30460      int nMap = szRegion*nShmPerMap;
30461      int i;
30462      void *pMem;
30463      if( pShmNode->h>=0 ){
30464        pMem = osMmap(0, nMap,
30465            pShmNode->isReadonly ? PROT_READ : PROT_READ|PROT_WRITE,
30466            MAP_SHARED, pShmNode->h, szRegion*(i64)pShmNode->nRegion
30467        );
30468        if( pMem==MAP_FAILED ){
30469          rc = unixLogError(SQLITE_IOERR_SHMMAP, "mmap", pShmNode->zFilename);
30470          goto shmpage_out;
30471        }
30472      }else{
30473        pMem = sqlite3_malloc64(szRegion);
30474        if( pMem==0 ){
30475          rc = SQLITE_NOMEM;
30476          goto shmpage_out;
30477        }
30478        memset(pMem, 0, szRegion);
30479      }
30480
30481      for(i=0; i<nShmPerMap; i++){
30482        pShmNode->apRegion[pShmNode->nRegion+i] = &((char*)pMem)[szRegion*i];
30483      }
30484      pShmNode->nRegion += nShmPerMap;
30485    }
30486  }
30487
30488shmpage_out:
30489  if( pShmNode->nRegion>iRegion ){
30490    *pp = pShmNode->apRegion[iRegion];
30491  }else{
30492    *pp = 0;
30493  }
30494  if( pShmNode->isReadonly && rc==SQLITE_OK ) rc = SQLITE_READONLY;
30495  sqlite3_mutex_leave(pShmNode->mutex);
30496  return rc;
30497}
30498
30499/*
30500** Change the lock state for a shared-memory segment.
30501**
30502** Note that the relationship between SHAREd and EXCLUSIVE locks is a little
30503** different here than in posix.  In xShmLock(), one can go from unlocked
30504** to shared and back or from unlocked to exclusive and back.  But one may
30505** not go from shared to exclusive or from exclusive to shared.
30506*/
30507static int unixShmLock(
30508  sqlite3_file *fd,          /* Database file holding the shared memory */
30509  int ofst,                  /* First lock to acquire or release */
30510  int n,                     /* Number of locks to acquire or release */
30511  int flags                  /* What to do with the lock */
30512){
30513  unixFile *pDbFd = (unixFile*)fd;      /* Connection holding shared memory */
30514  unixShm *p = pDbFd->pShm;             /* The shared memory being locked */
30515  unixShm *pX;                          /* For looping over all siblings */
30516  unixShmNode *pShmNode = p->pShmNode;  /* The underlying file iNode */
30517  int rc = SQLITE_OK;                   /* Result code */
30518  u16 mask;                             /* Mask of locks to take or release */
30519
30520  assert( pShmNode==pDbFd->pInode->pShmNode );
30521  assert( pShmNode->pInode==pDbFd->pInode );
30522  assert( ofst>=0 && ofst+n<=SQLITE_SHM_NLOCK );
30523  assert( n>=1 );
30524  assert( flags==(SQLITE_SHM_LOCK | SQLITE_SHM_SHARED)
30525       || flags==(SQLITE_SHM_LOCK | SQLITE_SHM_EXCLUSIVE)
30526       || flags==(SQLITE_SHM_UNLOCK | SQLITE_SHM_SHARED)
30527       || flags==(SQLITE_SHM_UNLOCK | SQLITE_SHM_EXCLUSIVE) );
30528  assert( n==1 || (flags & SQLITE_SHM_EXCLUSIVE)!=0 );
30529  assert( pShmNode->h>=0 || pDbFd->pInode->bProcessLock==1 );
30530  assert( pShmNode->h<0 || pDbFd->pInode->bProcessLock==0 );
30531
30532  mask = (1<<(ofst+n)) - (1<<ofst);
30533  assert( n>1 || mask==(1<<ofst) );
30534  sqlite3_mutex_enter(pShmNode->mutex);
30535  if( flags & SQLITE_SHM_UNLOCK ){
30536    u16 allMask = 0; /* Mask of locks held by siblings */
30537
30538    /* See if any siblings hold this same lock */
30539    for(pX=pShmNode->pFirst; pX; pX=pX->pNext){
30540      if( pX==p ) continue;
30541      assert( (pX->exclMask & (p->exclMask|p->sharedMask))==0 );
30542      allMask |= pX->sharedMask;
30543    }
30544
30545    /* Unlock the system-level locks */
30546    if( (mask & allMask)==0 ){
30547      rc = unixShmSystemLock(pDbFd, F_UNLCK, ofst+UNIX_SHM_BASE, n);
30548    }else{
30549      rc = SQLITE_OK;
30550    }
30551
30552    /* Undo the local locks */
30553    if( rc==SQLITE_OK ){
30554      p->exclMask &= ~mask;
30555      p->sharedMask &= ~mask;
30556    }
30557  }else if( flags & SQLITE_SHM_SHARED ){
30558    u16 allShared = 0;  /* Union of locks held by connections other than "p" */
30559
30560    /* Find out which shared locks are already held by sibling connections.
30561    ** If any sibling already holds an exclusive lock, go ahead and return
30562    ** SQLITE_BUSY.
30563    */
30564    for(pX=pShmNode->pFirst; pX; pX=pX->pNext){
30565      if( (pX->exclMask & mask)!=0 ){
30566        rc = SQLITE_BUSY;
30567        break;
30568      }
30569      allShared |= pX->sharedMask;
30570    }
30571
30572    /* Get shared locks at the system level, if necessary */
30573    if( rc==SQLITE_OK ){
30574      if( (allShared & mask)==0 ){
30575        rc = unixShmSystemLock(pDbFd, F_RDLCK, ofst+UNIX_SHM_BASE, n);
30576      }else{
30577        rc = SQLITE_OK;
30578      }
30579    }
30580
30581    /* Get the local shared locks */
30582    if( rc==SQLITE_OK ){
30583      p->sharedMask |= mask;
30584    }
30585  }else{
30586    /* Make sure no sibling connections hold locks that will block this
30587    ** lock.  If any do, return SQLITE_BUSY right away.
30588    */
30589    for(pX=pShmNode->pFirst; pX; pX=pX->pNext){
30590      if( (pX->exclMask & mask)!=0 || (pX->sharedMask & mask)!=0 ){
30591        rc = SQLITE_BUSY;
30592        break;
30593      }
30594    }
30595
30596    /* Get the exclusive locks at the system level.  Then if successful
30597    ** also mark the local connection as being locked.
30598    */
30599    if( rc==SQLITE_OK ){
30600      rc = unixShmSystemLock(pDbFd, F_WRLCK, ofst+UNIX_SHM_BASE, n);
30601      if( rc==SQLITE_OK ){
30602        assert( (p->sharedMask & mask)==0 );
30603        p->exclMask |= mask;
30604      }
30605    }
30606  }
30607  sqlite3_mutex_leave(pShmNode->mutex);
30608  OSTRACE(("SHM-LOCK shmid-%d, pid-%d got %03x,%03x\n",
30609           p->id, osGetpid(0), p->sharedMask, p->exclMask));
30610  return rc;
30611}
30612
30613/*
30614** Implement a memory barrier or memory fence on shared memory.
30615**
30616** All loads and stores begun before the barrier must complete before
30617** any load or store begun after the barrier.
30618*/
30619static void unixShmBarrier(
30620  sqlite3_file *fd                /* Database file holding the shared memory */
30621){
30622  UNUSED_PARAMETER(fd);
30623  unixEnterMutex();
30624  unixLeaveMutex();
30625}
30626
30627/*
30628** Close a connection to shared-memory.  Delete the underlying
30629** storage if deleteFlag is true.
30630**
30631** If there is no shared memory associated with the connection then this
30632** routine is a harmless no-op.
30633*/
30634static int unixShmUnmap(
30635  sqlite3_file *fd,               /* The underlying database file */
30636  int deleteFlag                  /* Delete shared-memory if true */
30637){
30638  unixShm *p;                     /* The connection to be closed */
30639  unixShmNode *pShmNode;          /* The underlying shared-memory file */
30640  unixShm **pp;                   /* For looping over sibling connections */
30641  unixFile *pDbFd;                /* The underlying database file */
30642
30643  pDbFd = (unixFile*)fd;
30644  p = pDbFd->pShm;
30645  if( p==0 ) return SQLITE_OK;
30646  pShmNode = p->pShmNode;
30647
30648  assert( pShmNode==pDbFd->pInode->pShmNode );
30649  assert( pShmNode->pInode==pDbFd->pInode );
30650
30651  /* Remove connection p from the set of connections associated
30652  ** with pShmNode */
30653  sqlite3_mutex_enter(pShmNode->mutex);
30654  for(pp=&pShmNode->pFirst; (*pp)!=p; pp = &(*pp)->pNext){}
30655  *pp = p->pNext;
30656
30657  /* Free the connection p */
30658  sqlite3_free(p);
30659  pDbFd->pShm = 0;
30660  sqlite3_mutex_leave(pShmNode->mutex);
30661
30662  /* If pShmNode->nRef has reached 0, then close the underlying
30663  ** shared-memory file, too */
30664  unixEnterMutex();
30665  assert( pShmNode->nRef>0 );
30666  pShmNode->nRef--;
30667  if( pShmNode->nRef==0 ){
30668    if( deleteFlag && pShmNode->h>=0 ){
30669      osUnlink(pShmNode->zFilename);
30670    }
30671    unixShmPurge(pDbFd);
30672  }
30673  unixLeaveMutex();
30674
30675  return SQLITE_OK;
30676}
30677
30678
30679#else
30680# define unixShmMap     0
30681# define unixShmLock    0
30682# define unixShmBarrier 0
30683# define unixShmUnmap   0
30684#endif /* #ifndef SQLITE_OMIT_WAL */
30685
30686#if SQLITE_MAX_MMAP_SIZE>0
30687/*
30688** If it is currently memory mapped, unmap file pFd.
30689*/
30690static void unixUnmapfile(unixFile *pFd){
30691  assert( pFd->nFetchOut==0 );
30692  if( pFd->pMapRegion ){
30693    osMunmap(pFd->pMapRegion, pFd->mmapSizeActual);
30694    pFd->pMapRegion = 0;
30695    pFd->mmapSize = 0;
30696    pFd->mmapSizeActual = 0;
30697  }
30698}
30699
30700/*
30701** Attempt to set the size of the memory mapping maintained by file
30702** descriptor pFd to nNew bytes. Any existing mapping is discarded.
30703**
30704** If successful, this function sets the following variables:
30705**
30706**       unixFile.pMapRegion
30707**       unixFile.mmapSize
30708**       unixFile.mmapSizeActual
30709**
30710** If unsuccessful, an error message is logged via sqlite3_log() and
30711** the three variables above are zeroed. In this case SQLite should
30712** continue accessing the database using the xRead() and xWrite()
30713** methods.
30714*/
30715static void unixRemapfile(
30716  unixFile *pFd,                  /* File descriptor object */
30717  i64 nNew                        /* Required mapping size */
30718){
30719  const char *zErr = "mmap";
30720  int h = pFd->h;                      /* File descriptor open on db file */
30721  u8 *pOrig = (u8 *)pFd->pMapRegion;   /* Pointer to current file mapping */
30722  i64 nOrig = pFd->mmapSizeActual;     /* Size of pOrig region in bytes */
30723  u8 *pNew = 0;                        /* Location of new mapping */
30724  int flags = PROT_READ;               /* Flags to pass to mmap() */
30725
30726  assert( pFd->nFetchOut==0 );
30727  assert( nNew>pFd->mmapSize );
30728  assert( nNew<=pFd->mmapSizeMax );
30729  assert( nNew>0 );
30730  assert( pFd->mmapSizeActual>=pFd->mmapSize );
30731  assert( MAP_FAILED!=0 );
30732
30733  if( (pFd->ctrlFlags & UNIXFILE_RDONLY)==0 ) flags |= PROT_WRITE;
30734
30735  if( pOrig ){
30736#if HAVE_MREMAP
30737    i64 nReuse = pFd->mmapSize;
30738#else
30739    const int szSyspage = osGetpagesize();
30740    i64 nReuse = (pFd->mmapSize & ~(szSyspage-1));
30741#endif
30742    u8 *pReq = &pOrig[nReuse];
30743
30744    /* Unmap any pages of the existing mapping that cannot be reused. */
30745    if( nReuse!=nOrig ){
30746      osMunmap(pReq, nOrig-nReuse);
30747    }
30748
30749#if HAVE_MREMAP
30750    pNew = osMremap(pOrig, nReuse, nNew, MREMAP_MAYMOVE);
30751    zErr = "mremap";
30752#else
30753    pNew = osMmap(pReq, nNew-nReuse, flags, MAP_SHARED, h, nReuse);
30754    if( pNew!=MAP_FAILED ){
30755      if( pNew!=pReq ){
30756        osMunmap(pNew, nNew - nReuse);
30757        pNew = 0;
30758      }else{
30759        pNew = pOrig;
30760      }
30761    }
30762#endif
30763
30764    /* The attempt to extend the existing mapping failed. Free it. */
30765    if( pNew==MAP_FAILED || pNew==0 ){
30766      osMunmap(pOrig, nReuse);
30767    }
30768  }
30769
30770  /* If pNew is still NULL, try to create an entirely new mapping. */
30771  if( pNew==0 ){
30772    pNew = osMmap(0, nNew, flags, MAP_SHARED, h, 0);
30773  }
30774
30775  if( pNew==MAP_FAILED ){
30776    pNew = 0;
30777    nNew = 0;
30778    unixLogError(SQLITE_OK, zErr, pFd->zPath);
30779
30780    /* If the mmap() above failed, assume that all subsequent mmap() calls
30781    ** will probably fail too. Fall back to using xRead/xWrite exclusively
30782    ** in this case.  */
30783    pFd->mmapSizeMax = 0;
30784  }
30785  pFd->pMapRegion = (void *)pNew;
30786  pFd->mmapSize = pFd->mmapSizeActual = nNew;
30787}
30788
30789/*
30790** Memory map or remap the file opened by file-descriptor pFd (if the file
30791** is already mapped, the existing mapping is replaced by the new). Or, if
30792** there already exists a mapping for this file, and there are still
30793** outstanding xFetch() references to it, this function is a no-op.
30794**
30795** If parameter nByte is non-negative, then it is the requested size of
30796** the mapping to create. Otherwise, if nByte is less than zero, then the
30797** requested size is the size of the file on disk. The actual size of the
30798** created mapping is either the requested size or the value configured
30799** using SQLITE_FCNTL_MMAP_LIMIT, whichever is smaller.
30800**
30801** SQLITE_OK is returned if no error occurs (even if the mapping is not
30802** recreated as a result of outstanding references) or an SQLite error
30803** code otherwise.
30804*/
30805static int unixMapfile(unixFile *pFd, i64 nByte){
30806  i64 nMap = nByte;
30807  int rc;
30808
30809  assert( nMap>=0 || pFd->nFetchOut==0 );
30810  if( pFd->nFetchOut>0 ) return SQLITE_OK;
30811
30812  if( nMap<0 ){
30813    struct stat statbuf;          /* Low-level file information */
30814    rc = osFstat(pFd->h, &statbuf);
30815    if( rc!=SQLITE_OK ){
30816      return SQLITE_IOERR_FSTAT;
30817    }
30818    nMap = statbuf.st_size;
30819  }
30820  if( nMap>pFd->mmapSizeMax ){
30821    nMap = pFd->mmapSizeMax;
30822  }
30823
30824  if( nMap!=pFd->mmapSize ){
30825    if( nMap>0 ){
30826      unixRemapfile(pFd, nMap);
30827    }else{
30828      unixUnmapfile(pFd);
30829    }
30830  }
30831
30832  return SQLITE_OK;
30833}
30834#endif /* SQLITE_MAX_MMAP_SIZE>0 */
30835
30836/*
30837** If possible, return a pointer to a mapping of file fd starting at offset
30838** iOff. The mapping must be valid for at least nAmt bytes.
30839**
30840** If such a pointer can be obtained, store it in *pp and return SQLITE_OK.
30841** Or, if one cannot but no error occurs, set *pp to 0 and return SQLITE_OK.
30842** Finally, if an error does occur, return an SQLite error code. The final
30843** value of *pp is undefined in this case.
30844**
30845** If this function does return a pointer, the caller must eventually
30846** release the reference by calling unixUnfetch().
30847*/
30848static int unixFetch(sqlite3_file *fd, i64 iOff, int nAmt, void **pp){
30849#if SQLITE_MAX_MMAP_SIZE>0
30850  unixFile *pFd = (unixFile *)fd;   /* The underlying database file */
30851#endif
30852  *pp = 0;
30853
30854#if SQLITE_MAX_MMAP_SIZE>0
30855  if( pFd->mmapSizeMax>0 ){
30856    if( pFd->pMapRegion==0 ){
30857      int rc = unixMapfile(pFd, -1);
30858      if( rc!=SQLITE_OK ) return rc;
30859    }
30860    if( pFd->mmapSize >= iOff+nAmt ){
30861      *pp = &((u8 *)pFd->pMapRegion)[iOff];
30862      pFd->nFetchOut++;
30863    }
30864  }
30865#endif
30866  return SQLITE_OK;
30867}
30868
30869/*
30870** If the third argument is non-NULL, then this function releases a
30871** reference obtained by an earlier call to unixFetch(). The second
30872** argument passed to this function must be the same as the corresponding
30873** argument that was passed to the unixFetch() invocation.
30874**
30875** Or, if the third argument is NULL, then this function is being called
30876** to inform the VFS layer that, according to POSIX, any existing mapping
30877** may now be invalid and should be unmapped.
30878*/
30879static int unixUnfetch(sqlite3_file *fd, i64 iOff, void *p){
30880#if SQLITE_MAX_MMAP_SIZE>0
30881  unixFile *pFd = (unixFile *)fd;   /* The underlying database file */
30882  UNUSED_PARAMETER(iOff);
30883
30884  /* If p==0 (unmap the entire file) then there must be no outstanding
30885  ** xFetch references. Or, if p!=0 (meaning it is an xFetch reference),
30886  ** then there must be at least one outstanding.  */
30887  assert( (p==0)==(pFd->nFetchOut==0) );
30888
30889  /* If p!=0, it must match the iOff value. */
30890  assert( p==0 || p==&((u8 *)pFd->pMapRegion)[iOff] );
30891
30892  if( p ){
30893    pFd->nFetchOut--;
30894  }else{
30895    unixUnmapfile(pFd);
30896  }
30897
30898  assert( pFd->nFetchOut>=0 );
30899#else
30900  UNUSED_PARAMETER(fd);
30901  UNUSED_PARAMETER(p);
30902  UNUSED_PARAMETER(iOff);
30903#endif
30904  return SQLITE_OK;
30905}
30906
30907/*
30908** Here ends the implementation of all sqlite3_file methods.
30909**
30910********************** End sqlite3_file Methods *******************************
30911******************************************************************************/
30912
30913/*
30914** This division contains definitions of sqlite3_io_methods objects that
30915** implement various file locking strategies.  It also contains definitions
30916** of "finder" functions.  A finder-function is used to locate the appropriate
30917** sqlite3_io_methods object for a particular database file.  The pAppData
30918** field of the sqlite3_vfs VFS objects are initialized to be pointers to
30919** the correct finder-function for that VFS.
30920**
30921** Most finder functions return a pointer to a fixed sqlite3_io_methods
30922** object.  The only interesting finder-function is autolockIoFinder, which
30923** looks at the filesystem type and tries to guess the best locking
30924** strategy from that.
30925**
30926** For finder-function F, two objects are created:
30927**
30928**    (1) The real finder-function named "FImpt()".
30929**
30930**    (2) A constant pointer to this function named just "F".
30931**
30932**
30933** A pointer to the F pointer is used as the pAppData value for VFS
30934** objects.  We have to do this instead of letting pAppData point
30935** directly at the finder-function since C90 rules prevent a void*
30936** from be cast into a function pointer.
30937**
30938**
30939** Each instance of this macro generates two objects:
30940**
30941**   *  A constant sqlite3_io_methods object call METHOD that has locking
30942**      methods CLOSE, LOCK, UNLOCK, CKRESLOCK.
30943**
30944**   *  An I/O method finder function called FINDER that returns a pointer
30945**      to the METHOD object in the previous bullet.
30946*/
30947#define IOMETHODS(FINDER,METHOD,VERSION,CLOSE,LOCK,UNLOCK,CKLOCK,SHMMAP)     \
30948static const sqlite3_io_methods METHOD = {                                   \
30949   VERSION,                    /* iVersion */                                \
30950   CLOSE,                      /* xClose */                                  \
30951   unixRead,                   /* xRead */                                   \
30952   unixWrite,                  /* xWrite */                                  \
30953   unixTruncate,               /* xTruncate */                               \
30954   unixSync,                   /* xSync */                                   \
30955   unixFileSize,               /* xFileSize */                               \
30956   LOCK,                       /* xLock */                                   \
30957   UNLOCK,                     /* xUnlock */                                 \
30958   CKLOCK,                     /* xCheckReservedLock */                      \
30959   unixFileControl,            /* xFileControl */                            \
30960   unixSectorSize,             /* xSectorSize */                             \
30961   unixDeviceCharacteristics,  /* xDeviceCapabilities */                     \
30962   SHMMAP,                     /* xShmMap */                                 \
30963   unixShmLock,                /* xShmLock */                                \
30964   unixShmBarrier,             /* xShmBarrier */                             \
30965   unixShmUnmap,               /* xShmUnmap */                               \
30966   unixFetch,                  /* xFetch */                                  \
30967   unixUnfetch,                /* xUnfetch */                                \
30968};                                                                           \
30969static const sqlite3_io_methods *FINDER##Impl(const char *z, unixFile *p){   \
30970  UNUSED_PARAMETER(z); UNUSED_PARAMETER(p);                                  \
30971  return &METHOD;                                                            \
30972}                                                                            \
30973static const sqlite3_io_methods *(*const FINDER)(const char*,unixFile *p)    \
30974    = FINDER##Impl;
30975
30976/*
30977** Here are all of the sqlite3_io_methods objects for each of the
30978** locking strategies.  Functions that return pointers to these methods
30979** are also created.
30980*/
30981IOMETHODS(
30982  posixIoFinder,            /* Finder function name */
30983  posixIoMethods,           /* sqlite3_io_methods object name */
30984  3,                        /* shared memory and mmap are enabled */
30985  unixClose,                /* xClose method */
30986  unixLock,                 /* xLock method */
30987  unixUnlock,               /* xUnlock method */
30988  unixCheckReservedLock,    /* xCheckReservedLock method */
30989  unixShmMap                /* xShmMap method */
30990)
30991IOMETHODS(
30992  nolockIoFinder,           /* Finder function name */
30993  nolockIoMethods,          /* sqlite3_io_methods object name */
30994  3,                        /* shared memory is disabled */
30995  nolockClose,              /* xClose method */
30996  nolockLock,               /* xLock method */
30997  nolockUnlock,             /* xUnlock method */
30998  nolockCheckReservedLock,  /* xCheckReservedLock method */
30999  0                         /* xShmMap method */
31000)
31001IOMETHODS(
31002  dotlockIoFinder,          /* Finder function name */
31003  dotlockIoMethods,         /* sqlite3_io_methods object name */
31004  1,                        /* shared memory is disabled */
31005  dotlockClose,             /* xClose method */
31006  dotlockLock,              /* xLock method */
31007  dotlockUnlock,            /* xUnlock method */
31008  dotlockCheckReservedLock, /* xCheckReservedLock method */
31009  0                         /* xShmMap method */
31010)
31011
31012#if SQLITE_ENABLE_LOCKING_STYLE
31013IOMETHODS(
31014  flockIoFinder,            /* Finder function name */
31015  flockIoMethods,           /* sqlite3_io_methods object name */
31016  1,                        /* shared memory is disabled */
31017  flockClose,               /* xClose method */
31018  flockLock,                /* xLock method */
31019  flockUnlock,              /* xUnlock method */
31020  flockCheckReservedLock,   /* xCheckReservedLock method */
31021  0                         /* xShmMap method */
31022)
31023#endif
31024
31025#if OS_VXWORKS
31026IOMETHODS(
31027  semIoFinder,              /* Finder function name */
31028  semIoMethods,             /* sqlite3_io_methods object name */
31029  1,                        /* shared memory is disabled */
31030  semXClose,                /* xClose method */
31031  semXLock,                 /* xLock method */
31032  semXUnlock,               /* xUnlock method */
31033  semXCheckReservedLock,    /* xCheckReservedLock method */
31034  0                         /* xShmMap method */
31035)
31036#endif
31037
31038#if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
31039IOMETHODS(
31040  afpIoFinder,              /* Finder function name */
31041  afpIoMethods,             /* sqlite3_io_methods object name */
31042  1,                        /* shared memory is disabled */
31043  afpClose,                 /* xClose method */
31044  afpLock,                  /* xLock method */
31045  afpUnlock,                /* xUnlock method */
31046  afpCheckReservedLock,     /* xCheckReservedLock method */
31047  0                         /* xShmMap method */
31048)
31049#endif
31050
31051/*
31052** The proxy locking method is a "super-method" in the sense that it
31053** opens secondary file descriptors for the conch and lock files and
31054** it uses proxy, dot-file, AFP, and flock() locking methods on those
31055** secondary files.  For this reason, the division that implements
31056** proxy locking is located much further down in the file.  But we need
31057** to go ahead and define the sqlite3_io_methods and finder function
31058** for proxy locking here.  So we forward declare the I/O methods.
31059*/
31060#if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
31061static int proxyClose(sqlite3_file*);
31062static int proxyLock(sqlite3_file*, int);
31063static int proxyUnlock(sqlite3_file*, int);
31064static int proxyCheckReservedLock(sqlite3_file*, int*);
31065IOMETHODS(
31066  proxyIoFinder,            /* Finder function name */
31067  proxyIoMethods,           /* sqlite3_io_methods object name */
31068  1,                        /* shared memory is disabled */
31069  proxyClose,               /* xClose method */
31070  proxyLock,                /* xLock method */
31071  proxyUnlock,              /* xUnlock method */
31072  proxyCheckReservedLock,   /* xCheckReservedLock method */
31073  0                         /* xShmMap method */
31074)
31075#endif
31076
31077/* nfs lockd on OSX 10.3+ doesn't clear write locks when a read lock is set */
31078#if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
31079IOMETHODS(
31080  nfsIoFinder,               /* Finder function name */
31081  nfsIoMethods,              /* sqlite3_io_methods object name */
31082  1,                         /* shared memory is disabled */
31083  unixClose,                 /* xClose method */
31084  unixLock,                  /* xLock method */
31085  nfsUnlock,                 /* xUnlock method */
31086  unixCheckReservedLock,     /* xCheckReservedLock method */
31087  0                          /* xShmMap method */
31088)
31089#endif
31090
31091#if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
31092/*
31093** This "finder" function attempts to determine the best locking strategy
31094** for the database file "filePath".  It then returns the sqlite3_io_methods
31095** object that implements that strategy.
31096**
31097** This is for MacOSX only.
31098*/
31099static const sqlite3_io_methods *autolockIoFinderImpl(
31100  const char *filePath,    /* name of the database file */
31101  unixFile *pNew           /* open file object for the database file */
31102){
31103  static const struct Mapping {
31104    const char *zFilesystem;              /* Filesystem type name */
31105    const sqlite3_io_methods *pMethods;   /* Appropriate locking method */
31106  } aMap[] = {
31107    { "hfs",    &posixIoMethods },
31108    { "ufs",    &posixIoMethods },
31109    { "afpfs",  &afpIoMethods },
31110    { "smbfs",  &afpIoMethods },
31111    { "webdav", &nolockIoMethods },
31112    { 0, 0 }
31113  };
31114  int i;
31115  struct statfs fsInfo;
31116  struct flock lockInfo;
31117
31118  if( !filePath ){
31119    /* If filePath==NULL that means we are dealing with a transient file
31120    ** that does not need to be locked. */
31121    return &nolockIoMethods;
31122  }
31123  if( statfs(filePath, &fsInfo) != -1 ){
31124    if( fsInfo.f_flags & MNT_RDONLY ){
31125      return &nolockIoMethods;
31126    }
31127    for(i=0; aMap[i].zFilesystem; i++){
31128      if( strcmp(fsInfo.f_fstypename, aMap[i].zFilesystem)==0 ){
31129        return aMap[i].pMethods;
31130      }
31131    }
31132  }
31133
31134  /* Default case. Handles, amongst others, "nfs".
31135  ** Test byte-range lock using fcntl(). If the call succeeds,
31136  ** assume that the file-system supports POSIX style locks.
31137  */
31138  lockInfo.l_len = 1;
31139  lockInfo.l_start = 0;
31140  lockInfo.l_whence = SEEK_SET;
31141  lockInfo.l_type = F_RDLCK;
31142  if( osFcntl(pNew->h, F_GETLK, &lockInfo)!=-1 ) {
31143    if( strcmp(fsInfo.f_fstypename, "nfs")==0 ){
31144      return &nfsIoMethods;
31145    } else {
31146      return &posixIoMethods;
31147    }
31148  }else{
31149    return &dotlockIoMethods;
31150  }
31151}
31152static const sqlite3_io_methods
31153  *(*const autolockIoFinder)(const char*,unixFile*) = autolockIoFinderImpl;
31154
31155#endif /* defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE */
31156
31157#if OS_VXWORKS
31158/*
31159** This "finder" function for VxWorks checks to see if posix advisory
31160** locking works.  If it does, then that is what is used.  If it does not
31161** work, then fallback to named semaphore locking.
31162*/
31163static const sqlite3_io_methods *vxworksIoFinderImpl(
31164  const char *filePath,    /* name of the database file */
31165  unixFile *pNew           /* the open file object */
31166){
31167  struct flock lockInfo;
31168
31169  if( !filePath ){
31170    /* If filePath==NULL that means we are dealing with a transient file
31171    ** that does not need to be locked. */
31172    return &nolockIoMethods;
31173  }
31174
31175  /* Test if fcntl() is supported and use POSIX style locks.
31176  ** Otherwise fall back to the named semaphore method.
31177  */
31178  lockInfo.l_len = 1;
31179  lockInfo.l_start = 0;
31180  lockInfo.l_whence = SEEK_SET;
31181  lockInfo.l_type = F_RDLCK;
31182  if( osFcntl(pNew->h, F_GETLK, &lockInfo)!=-1 ) {
31183    return &posixIoMethods;
31184  }else{
31185    return &semIoMethods;
31186  }
31187}
31188static const sqlite3_io_methods
31189  *(*const vxworksIoFinder)(const char*,unixFile*) = vxworksIoFinderImpl;
31190
31191#endif /* OS_VXWORKS */
31192
31193/*
31194** An abstract type for a pointer to an IO method finder function:
31195*/
31196typedef const sqlite3_io_methods *(*finder_type)(const char*,unixFile*);
31197
31198
31199/****************************************************************************
31200**************************** sqlite3_vfs methods ****************************
31201**
31202** This division contains the implementation of methods on the
31203** sqlite3_vfs object.
31204*/
31205
31206/*
31207** Initialize the contents of the unixFile structure pointed to by pId.
31208*/
31209static int fillInUnixFile(
31210  sqlite3_vfs *pVfs,      /* Pointer to vfs object */
31211  int h,                  /* Open file descriptor of file being opened */
31212  sqlite3_file *pId,      /* Write to the unixFile structure here */
31213  const char *zFilename,  /* Name of the file being opened */
31214  int ctrlFlags           /* Zero or more UNIXFILE_* values */
31215){
31216  const sqlite3_io_methods *pLockingStyle;
31217  unixFile *pNew = (unixFile *)pId;
31218  int rc = SQLITE_OK;
31219
31220  assert( pNew->pInode==NULL );
31221
31222  /* Usually the path zFilename should not be a relative pathname. The
31223  ** exception is when opening the proxy "conch" file in builds that
31224  ** include the special Apple locking styles.
31225  */
31226#if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
31227  assert( zFilename==0 || zFilename[0]=='/'
31228    || pVfs->pAppData==(void*)&autolockIoFinder );
31229#else
31230  assert( zFilename==0 || zFilename[0]=='/' );
31231#endif
31232
31233  /* No locking occurs in temporary files */
31234  assert( zFilename!=0 || (ctrlFlags & UNIXFILE_NOLOCK)!=0 );
31235
31236  OSTRACE(("OPEN    %-3d %s\n", h, zFilename));
31237  pNew->h = h;
31238  pNew->pVfs = pVfs;
31239  pNew->zPath = zFilename;
31240  pNew->ctrlFlags = (u8)ctrlFlags;
31241#if SQLITE_MAX_MMAP_SIZE>0
31242  pNew->mmapSizeMax = sqlite3GlobalConfig.szMmap;
31243#endif
31244  if( sqlite3_uri_boolean(((ctrlFlags & UNIXFILE_URI) ? zFilename : 0),
31245                           "psow", SQLITE_POWERSAFE_OVERWRITE) ){
31246    pNew->ctrlFlags |= UNIXFILE_PSOW;
31247  }
31248  if( strcmp(pVfs->zName,"unix-excl")==0 ){
31249    pNew->ctrlFlags |= UNIXFILE_EXCL;
31250  }
31251
31252#if OS_VXWORKS
31253  pNew->pId = vxworksFindFileId(zFilename);
31254  if( pNew->pId==0 ){
31255    ctrlFlags |= UNIXFILE_NOLOCK;
31256    rc = SQLITE_NOMEM;
31257  }
31258#endif
31259
31260  if( ctrlFlags & UNIXFILE_NOLOCK ){
31261    pLockingStyle = &nolockIoMethods;
31262  }else{
31263    pLockingStyle = (**(finder_type*)pVfs->pAppData)(zFilename, pNew);
31264#if SQLITE_ENABLE_LOCKING_STYLE
31265    /* Cache zFilename in the locking context (AFP and dotlock override) for
31266    ** proxyLock activation is possible (remote proxy is based on db name)
31267    ** zFilename remains valid until file is closed, to support */
31268    pNew->lockingContext = (void*)zFilename;
31269#endif
31270  }
31271
31272  if( pLockingStyle == &posixIoMethods
31273#if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
31274    || pLockingStyle == &nfsIoMethods
31275#endif
31276  ){
31277    unixEnterMutex();
31278    rc = findInodeInfo(pNew, &pNew->pInode);
31279    if( rc!=SQLITE_OK ){
31280      /* If an error occurred in findInodeInfo(), close the file descriptor
31281      ** immediately, before releasing the mutex. findInodeInfo() may fail
31282      ** in two scenarios:
31283      **
31284      **   (a) A call to fstat() failed.
31285      **   (b) A malloc failed.
31286      **
31287      ** Scenario (b) may only occur if the process is holding no other
31288      ** file descriptors open on the same file. If there were other file
31289      ** descriptors on this file, then no malloc would be required by
31290      ** findInodeInfo(). If this is the case, it is quite safe to close
31291      ** handle h - as it is guaranteed that no posix locks will be released
31292      ** by doing so.
31293      **
31294      ** If scenario (a) caused the error then things are not so safe. The
31295      ** implicit assumption here is that if fstat() fails, things are in
31296      ** such bad shape that dropping a lock or two doesn't matter much.
31297      */
31298      robust_close(pNew, h, __LINE__);
31299      h = -1;
31300    }
31301    unixLeaveMutex();
31302  }
31303
31304#if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__)
31305  else if( pLockingStyle == &afpIoMethods ){
31306    /* AFP locking uses the file path so it needs to be included in
31307    ** the afpLockingContext.
31308    */
31309    afpLockingContext *pCtx;
31310    pNew->lockingContext = pCtx = sqlite3_malloc64( sizeof(*pCtx) );
31311    if( pCtx==0 ){
31312      rc = SQLITE_NOMEM;
31313    }else{
31314      /* NB: zFilename exists and remains valid until the file is closed
31315      ** according to requirement F11141.  So we do not need to make a
31316      ** copy of the filename. */
31317      pCtx->dbPath = zFilename;
31318      pCtx->reserved = 0;
31319      srandomdev();
31320      unixEnterMutex();
31321      rc = findInodeInfo(pNew, &pNew->pInode);
31322      if( rc!=SQLITE_OK ){
31323        sqlite3_free(pNew->lockingContext);
31324        robust_close(pNew, h, __LINE__);
31325        h = -1;
31326      }
31327      unixLeaveMutex();
31328    }
31329  }
31330#endif
31331
31332  else if( pLockingStyle == &dotlockIoMethods ){
31333    /* Dotfile locking uses the file path so it needs to be included in
31334    ** the dotlockLockingContext
31335    */
31336    char *zLockFile;
31337    int nFilename;
31338    assert( zFilename!=0 );
31339    nFilename = (int)strlen(zFilename) + 6;
31340    zLockFile = (char *)sqlite3_malloc64(nFilename);
31341    if( zLockFile==0 ){
31342      rc = SQLITE_NOMEM;
31343    }else{
31344      sqlite3_snprintf(nFilename, zLockFile, "%s" DOTLOCK_SUFFIX, zFilename);
31345    }
31346    pNew->lockingContext = zLockFile;
31347  }
31348
31349#if OS_VXWORKS
31350  else if( pLockingStyle == &semIoMethods ){
31351    /* Named semaphore locking uses the file path so it needs to be
31352    ** included in the semLockingContext
31353    */
31354    unixEnterMutex();
31355    rc = findInodeInfo(pNew, &pNew->pInode);
31356    if( (rc==SQLITE_OK) && (pNew->pInode->pSem==NULL) ){
31357      char *zSemName = pNew->pInode->aSemName;
31358      int n;
31359      sqlite3_snprintf(MAX_PATHNAME, zSemName, "/%s.sem",
31360                       pNew->pId->zCanonicalName);
31361      for( n=1; zSemName[n]; n++ )
31362        if( zSemName[n]=='/' ) zSemName[n] = '_';
31363      pNew->pInode->pSem = sem_open(zSemName, O_CREAT, 0666, 1);
31364      if( pNew->pInode->pSem == SEM_FAILED ){
31365        rc = SQLITE_NOMEM;
31366        pNew->pInode->aSemName[0] = '\0';
31367      }
31368    }
31369    unixLeaveMutex();
31370  }
31371#endif
31372
31373  storeLastErrno(pNew, 0);
31374#if OS_VXWORKS
31375  if( rc!=SQLITE_OK ){
31376    if( h>=0 ) robust_close(pNew, h, __LINE__);
31377    h = -1;
31378    osUnlink(zFilename);
31379    pNew->ctrlFlags |= UNIXFILE_DELETE;
31380  }
31381#endif
31382  if( rc!=SQLITE_OK ){
31383    if( h>=0 ) robust_close(pNew, h, __LINE__);
31384  }else{
31385    pNew->pMethod = pLockingStyle;
31386    OpenCounter(+1);
31387    verifyDbFile(pNew);
31388  }
31389  return rc;
31390}
31391
31392/*
31393** Return the name of a directory in which to put temporary files.
31394** If no suitable temporary file directory can be found, return NULL.
31395*/
31396static const char *unixTempFileDir(void){
31397  static const char *azDirs[] = {
31398     0,
31399     0,
31400     0,
31401     "/var/tmp",
31402     "/usr/tmp",
31403     "/tmp",
31404     0        /* List terminator */
31405  };
31406  unsigned int i;
31407  struct stat buf;
31408  const char *zDir = 0;
31409
31410  azDirs[0] = sqlite3_temp_directory;
31411  if( !azDirs[1] ) azDirs[1] = getenv("SQLITE_TMPDIR");
31412  if( !azDirs[2] ) azDirs[2] = getenv("TMPDIR");
31413  for(i=0; i<sizeof(azDirs)/sizeof(azDirs[0]); zDir=azDirs[i++]){
31414    if( zDir==0 ) continue;
31415    if( osStat(zDir, &buf) ) continue;
31416    if( !S_ISDIR(buf.st_mode) ) continue;
31417    if( osAccess(zDir, 07) ) continue;
31418    break;
31419  }
31420  return zDir;
31421}
31422
31423/*
31424** Create a temporary file name in zBuf.  zBuf must be allocated
31425** by the calling process and must be big enough to hold at least
31426** pVfs->mxPathname bytes.
31427*/
31428static int unixGetTempname(int nBuf, char *zBuf){
31429  static const unsigned char zChars[] =
31430    "abcdefghijklmnopqrstuvwxyz"
31431    "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
31432    "0123456789";
31433  unsigned int i, j;
31434  const char *zDir;
31435
31436  /* It's odd to simulate an io-error here, but really this is just
31437  ** using the io-error infrastructure to test that SQLite handles this
31438  ** function failing.
31439  */
31440  SimulateIOError( return SQLITE_IOERR );
31441
31442  zDir = unixTempFileDir();
31443  if( zDir==0 ) zDir = ".";
31444
31445  /* Check that the output buffer is large enough for the temporary file
31446  ** name. If it is not, return SQLITE_ERROR.
31447  */
31448  if( (strlen(zDir) + strlen(SQLITE_TEMP_FILE_PREFIX) + 18) >= (size_t)nBuf ){
31449    return SQLITE_ERROR;
31450  }
31451
31452  do{
31453    sqlite3_snprintf(nBuf-18, zBuf, "%s/"SQLITE_TEMP_FILE_PREFIX, zDir);
31454    j = (int)strlen(zBuf);
31455    sqlite3_randomness(15, &zBuf[j]);
31456    for(i=0; i<15; i++, j++){
31457      zBuf[j] = (char)zChars[ ((unsigned char)zBuf[j])%(sizeof(zChars)-1) ];
31458    }
31459    zBuf[j] = 0;
31460    zBuf[j+1] = 0;
31461  }while( osAccess(zBuf,0)==0 );
31462  return SQLITE_OK;
31463}
31464
31465#if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__)
31466/*
31467** Routine to transform a unixFile into a proxy-locking unixFile.
31468** Implementation in the proxy-lock division, but used by unixOpen()
31469** if SQLITE_PREFER_PROXY_LOCKING is defined.
31470*/
31471static int proxyTransformUnixFile(unixFile*, const char*);
31472#endif
31473
31474/*
31475** Search for an unused file descriptor that was opened on the database
31476** file (not a journal or master-journal file) identified by pathname
31477** zPath with SQLITE_OPEN_XXX flags matching those passed as the second
31478** argument to this function.
31479**
31480** Such a file descriptor may exist if a database connection was closed
31481** but the associated file descriptor could not be closed because some
31482** other file descriptor open on the same file is holding a file-lock.
31483** Refer to comments in the unixClose() function and the lengthy comment
31484** describing "Posix Advisory Locking" at the start of this file for
31485** further details. Also, ticket #4018.
31486**
31487** If a suitable file descriptor is found, then it is returned. If no
31488** such file descriptor is located, -1 is returned.
31489*/
31490static UnixUnusedFd *findReusableFd(const char *zPath, int flags){
31491  UnixUnusedFd *pUnused = 0;
31492
31493  /* Do not search for an unused file descriptor on vxworks. Not because
31494  ** vxworks would not benefit from the change (it might, we're not sure),
31495  ** but because no way to test it is currently available. It is better
31496  ** not to risk breaking vxworks support for the sake of such an obscure
31497  ** feature.  */
31498#if !OS_VXWORKS
31499  struct stat sStat;                   /* Results of stat() call */
31500
31501  /* A stat() call may fail for various reasons. If this happens, it is
31502  ** almost certain that an open() call on the same path will also fail.
31503  ** For this reason, if an error occurs in the stat() call here, it is
31504  ** ignored and -1 is returned. The caller will try to open a new file
31505  ** descriptor on the same path, fail, and return an error to SQLite.
31506  **
31507  ** Even if a subsequent open() call does succeed, the consequences of
31508  ** not searching for a reusable file descriptor are not dire.  */
31509  if( 0==osStat(zPath, &sStat) ){
31510    unixInodeInfo *pInode;
31511
31512    unixEnterMutex();
31513    pInode = inodeList;
31514    while( pInode && (pInode->fileId.dev!=sStat.st_dev
31515                     || pInode->fileId.ino!=sStat.st_ino) ){
31516       pInode = pInode->pNext;
31517    }
31518    if( pInode ){
31519      UnixUnusedFd **pp;
31520      for(pp=&pInode->pUnused; *pp && (*pp)->flags!=flags; pp=&((*pp)->pNext));
31521      pUnused = *pp;
31522      if( pUnused ){
31523        *pp = pUnused->pNext;
31524      }
31525    }
31526    unixLeaveMutex();
31527  }
31528#endif    /* if !OS_VXWORKS */
31529  return pUnused;
31530}
31531
31532/*
31533** This function is called by unixOpen() to determine the unix permissions
31534** to create new files with. If no error occurs, then SQLITE_OK is returned
31535** and a value suitable for passing as the third argument to open(2) is
31536** written to *pMode. If an IO error occurs, an SQLite error code is
31537** returned and the value of *pMode is not modified.
31538**
31539** In most cases, this routine sets *pMode to 0, which will become
31540** an indication to robust_open() to create the file using
31541** SQLITE_DEFAULT_FILE_PERMISSIONS adjusted by the umask.
31542** But if the file being opened is a WAL or regular journal file, then
31543** this function queries the file-system for the permissions on the
31544** corresponding database file and sets *pMode to this value. Whenever
31545** possible, WAL and journal files are created using the same permissions
31546** as the associated database file.
31547**
31548** If the SQLITE_ENABLE_8_3_NAMES option is enabled, then the
31549** original filename is unavailable.  But 8_3_NAMES is only used for
31550** FAT filesystems and permissions do not matter there, so just use
31551** the default permissions.
31552*/
31553static int findCreateFileMode(
31554  const char *zPath,              /* Path of file (possibly) being created */
31555  int flags,                      /* Flags passed as 4th argument to xOpen() */
31556  mode_t *pMode,                  /* OUT: Permissions to open file with */
31557  uid_t *pUid,                    /* OUT: uid to set on the file */
31558  gid_t *pGid                     /* OUT: gid to set on the file */
31559){
31560  int rc = SQLITE_OK;             /* Return Code */
31561  *pMode = 0;
31562  *pUid = 0;
31563  *pGid = 0;
31564  if( flags & (SQLITE_OPEN_WAL|SQLITE_OPEN_MAIN_JOURNAL) ){
31565    char zDb[MAX_PATHNAME+1];     /* Database file path */
31566    int nDb;                      /* Number of valid bytes in zDb */
31567    struct stat sStat;            /* Output of stat() on database file */
31568
31569    /* zPath is a path to a WAL or journal file. The following block derives
31570    ** the path to the associated database file from zPath. This block handles
31571    ** the following naming conventions:
31572    **
31573    **   "<path to db>-journal"
31574    **   "<path to db>-wal"
31575    **   "<path to db>-journalNN"
31576    **   "<path to db>-walNN"
31577    **
31578    ** where NN is a decimal number. The NN naming schemes are
31579    ** used by the test_multiplex.c module.
31580    */
31581    nDb = sqlite3Strlen30(zPath) - 1;
31582#ifdef SQLITE_ENABLE_8_3_NAMES
31583    while( nDb>0 && sqlite3Isalnum(zPath[nDb]) ) nDb--;
31584    if( nDb==0 || zPath[nDb]!='-' ) return SQLITE_OK;
31585#else
31586    while( zPath[nDb]!='-' ){
31587      assert( nDb>0 );
31588      assert( zPath[nDb]!='\n' );
31589      nDb--;
31590    }
31591#endif
31592    memcpy(zDb, zPath, nDb);
31593    zDb[nDb] = '\0';
31594
31595    if( 0==osStat(zDb, &sStat) ){
31596      *pMode = sStat.st_mode & 0777;
31597      *pUid = sStat.st_uid;
31598      *pGid = sStat.st_gid;
31599    }else{
31600      rc = SQLITE_IOERR_FSTAT;
31601    }
31602  }else if( flags & SQLITE_OPEN_DELETEONCLOSE ){
31603    *pMode = 0600;
31604  }
31605  return rc;
31606}
31607
31608/*
31609** Open the file zPath.
31610**
31611** Previously, the SQLite OS layer used three functions in place of this
31612** one:
31613**
31614**     sqlite3OsOpenReadWrite();
31615**     sqlite3OsOpenReadOnly();
31616**     sqlite3OsOpenExclusive();
31617**
31618** These calls correspond to the following combinations of flags:
31619**
31620**     ReadWrite() ->     (READWRITE | CREATE)
31621**     ReadOnly()  ->     (READONLY)
31622**     OpenExclusive() -> (READWRITE | CREATE | EXCLUSIVE)
31623**
31624** The old OpenExclusive() accepted a boolean argument - "delFlag". If
31625** true, the file was configured to be automatically deleted when the
31626** file handle closed. To achieve the same effect using this new
31627** interface, add the DELETEONCLOSE flag to those specified above for
31628** OpenExclusive().
31629*/
31630static int unixOpen(
31631  sqlite3_vfs *pVfs,           /* The VFS for which this is the xOpen method */
31632  const char *zPath,           /* Pathname of file to be opened */
31633  sqlite3_file *pFile,         /* The file descriptor to be filled in */
31634  int flags,                   /* Input flags to control the opening */
31635  int *pOutFlags               /* Output flags returned to SQLite core */
31636){
31637  unixFile *p = (unixFile *)pFile;
31638  int fd = -1;                   /* File descriptor returned by open() */
31639  int openFlags = 0;             /* Flags to pass to open() */
31640  int eType = flags&0xFFFFFF00;  /* Type of file to open */
31641  int noLock;                    /* True to omit locking primitives */
31642  int rc = SQLITE_OK;            /* Function Return Code */
31643  int ctrlFlags = 0;             /* UNIXFILE_* flags */
31644
31645  int isExclusive  = (flags & SQLITE_OPEN_EXCLUSIVE);
31646  int isDelete     = (flags & SQLITE_OPEN_DELETEONCLOSE);
31647  int isCreate     = (flags & SQLITE_OPEN_CREATE);
31648  int isReadonly   = (flags & SQLITE_OPEN_READONLY);
31649  int isReadWrite  = (flags & SQLITE_OPEN_READWRITE);
31650#if SQLITE_ENABLE_LOCKING_STYLE
31651  int isAutoProxy  = (flags & SQLITE_OPEN_AUTOPROXY);
31652#endif
31653#if defined(__APPLE__) || SQLITE_ENABLE_LOCKING_STYLE
31654  struct statfs fsInfo;
31655#endif
31656
31657  /* If creating a master or main-file journal, this function will open
31658  ** a file-descriptor on the directory too. The first time unixSync()
31659  ** is called the directory file descriptor will be fsync()ed and close()d.
31660  */
31661  int syncDir = (isCreate && (
31662        eType==SQLITE_OPEN_MASTER_JOURNAL
31663     || eType==SQLITE_OPEN_MAIN_JOURNAL
31664     || eType==SQLITE_OPEN_WAL
31665  ));
31666
31667  /* If argument zPath is a NULL pointer, this function is required to open
31668  ** a temporary file. Use this buffer to store the file name in.
31669  */
31670  char zTmpname[MAX_PATHNAME+2];
31671  const char *zName = zPath;
31672
31673  /* Check the following statements are true:
31674  **
31675  **   (a) Exactly one of the READWRITE and READONLY flags must be set, and
31676  **   (b) if CREATE is set, then READWRITE must also be set, and
31677  **   (c) if EXCLUSIVE is set, then CREATE must also be set.
31678  **   (d) if DELETEONCLOSE is set, then CREATE must also be set.
31679  */
31680  assert((isReadonly==0 || isReadWrite==0) && (isReadWrite || isReadonly));
31681  assert(isCreate==0 || isReadWrite);
31682  assert(isExclusive==0 || isCreate);
31683  assert(isDelete==0 || isCreate);
31684
31685  /* The main DB, main journal, WAL file and master journal are never
31686  ** automatically deleted. Nor are they ever temporary files.  */
31687  assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MAIN_DB );
31688  assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MAIN_JOURNAL );
31689  assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MASTER_JOURNAL );
31690  assert( (!isDelete && zName) || eType!=SQLITE_OPEN_WAL );
31691
31692  /* Assert that the upper layer has set one of the "file-type" flags. */
31693  assert( eType==SQLITE_OPEN_MAIN_DB      || eType==SQLITE_OPEN_TEMP_DB
31694       || eType==SQLITE_OPEN_MAIN_JOURNAL || eType==SQLITE_OPEN_TEMP_JOURNAL
31695       || eType==SQLITE_OPEN_SUBJOURNAL   || eType==SQLITE_OPEN_MASTER_JOURNAL
31696       || eType==SQLITE_OPEN_TRANSIENT_DB || eType==SQLITE_OPEN_WAL
31697  );
31698
31699  /* Detect a pid change and reset the PRNG.  There is a race condition
31700  ** here such that two or more threads all trying to open databases at
31701  ** the same instant might all reset the PRNG.  But multiple resets
31702  ** are harmless.
31703  */
31704  if( randomnessPid!=osGetpid(0) ){
31705    randomnessPid = osGetpid(0);
31706    sqlite3_randomness(0,0);
31707  }
31708
31709  memset(p, 0, sizeof(unixFile));
31710
31711  if( eType==SQLITE_OPEN_MAIN_DB ){
31712    UnixUnusedFd *pUnused;
31713    pUnused = findReusableFd(zName, flags);
31714    if( pUnused ){
31715      fd = pUnused->fd;
31716    }else{
31717      pUnused = sqlite3_malloc64(sizeof(*pUnused));
31718      if( !pUnused ){
31719        return SQLITE_NOMEM;
31720      }
31721    }
31722    p->pUnused = pUnused;
31723
31724    /* Database filenames are double-zero terminated if they are not
31725    ** URIs with parameters.  Hence, they can always be passed into
31726    ** sqlite3_uri_parameter(). */
31727    assert( (flags & SQLITE_OPEN_URI) || zName[strlen(zName)+1]==0 );
31728
31729  }else if( !zName ){
31730    /* If zName is NULL, the upper layer is requesting a temp file. */
31731    assert(isDelete && !syncDir);
31732    rc = unixGetTempname(MAX_PATHNAME+2, zTmpname);
31733    if( rc!=SQLITE_OK ){
31734      return rc;
31735    }
31736    zName = zTmpname;
31737
31738    /* Generated temporary filenames are always double-zero terminated
31739    ** for use by sqlite3_uri_parameter(). */
31740    assert( zName[strlen(zName)+1]==0 );
31741  }
31742
31743  /* Determine the value of the flags parameter passed to POSIX function
31744  ** open(). These must be calculated even if open() is not called, as
31745  ** they may be stored as part of the file handle and used by the
31746  ** 'conch file' locking functions later on.  */
31747  if( isReadonly )  openFlags |= O_RDONLY;
31748  if( isReadWrite ) openFlags |= O_RDWR;
31749  if( isCreate )    openFlags |= O_CREAT;
31750  if( isExclusive ) openFlags |= (O_EXCL|O_NOFOLLOW);
31751  openFlags |= (O_LARGEFILE|O_BINARY);
31752
31753  if( fd<0 ){
31754    mode_t openMode;              /* Permissions to create file with */
31755    uid_t uid;                    /* Userid for the file */
31756    gid_t gid;                    /* Groupid for the file */
31757    rc = findCreateFileMode(zName, flags, &openMode, &uid, &gid);
31758    if( rc!=SQLITE_OK ){
31759      assert( !p->pUnused );
31760      assert( eType==SQLITE_OPEN_WAL || eType==SQLITE_OPEN_MAIN_JOURNAL );
31761      return rc;
31762    }
31763    fd = robust_open(zName, openFlags, openMode);
31764    OSTRACE(("OPENX   %-3d %s 0%o\n", fd, zName, openFlags));
31765    if( fd<0 && errno!=EISDIR && isReadWrite && !isExclusive ){
31766      /* Failed to open the file for read/write access. Try read-only. */
31767      flags &= ~(SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE);
31768      openFlags &= ~(O_RDWR|O_CREAT);
31769      flags |= SQLITE_OPEN_READONLY;
31770      openFlags |= O_RDONLY;
31771      isReadonly = 1;
31772      fd = robust_open(zName, openFlags, openMode);
31773    }
31774    if( fd<0 ){
31775      rc = unixLogError(SQLITE_CANTOPEN_BKPT, "open", zName);
31776      goto open_finished;
31777    }
31778
31779    /* If this process is running as root and if creating a new rollback
31780    ** journal or WAL file, set the ownership of the journal or WAL to be
31781    ** the same as the original database.
31782    */
31783    if( flags & (SQLITE_OPEN_WAL|SQLITE_OPEN_MAIN_JOURNAL) ){
31784      osFchown(fd, uid, gid);
31785    }
31786  }
31787  assert( fd>=0 );
31788  if( pOutFlags ){
31789    *pOutFlags = flags;
31790  }
31791
31792  if( p->pUnused ){
31793    p->pUnused->fd = fd;
31794    p->pUnused->flags = flags;
31795  }
31796
31797  if( isDelete ){
31798#if OS_VXWORKS
31799    zPath = zName;
31800#elif defined(SQLITE_UNLINK_AFTER_CLOSE)
31801    zPath = sqlite3_mprintf("%s", zName);
31802    if( zPath==0 ){
31803      robust_close(p, fd, __LINE__);
31804      return SQLITE_NOMEM;
31805    }
31806#else
31807    osUnlink(zName);
31808#endif
31809  }
31810#if SQLITE_ENABLE_LOCKING_STYLE
31811  else{
31812    p->openFlags = openFlags;
31813  }
31814#endif
31815
31816  noLock = eType!=SQLITE_OPEN_MAIN_DB;
31817
31818
31819#if defined(__APPLE__) || SQLITE_ENABLE_LOCKING_STYLE
31820  if( fstatfs(fd, &fsInfo) == -1 ){
31821    storeLastErrno(p, errno);
31822    robust_close(p, fd, __LINE__);
31823    return SQLITE_IOERR_ACCESS;
31824  }
31825  if (0 == strncmp("msdos", fsInfo.f_fstypename, 5)) {
31826    ((unixFile*)pFile)->fsFlags |= SQLITE_FSFLAGS_IS_MSDOS;
31827  }
31828  if (0 == strncmp("exfat", fsInfo.f_fstypename, 5)) {
31829    ((unixFile*)pFile)->fsFlags |= SQLITE_FSFLAGS_IS_MSDOS;
31830  }
31831#endif
31832
31833  /* Set up appropriate ctrlFlags */
31834  if( isDelete )                ctrlFlags |= UNIXFILE_DELETE;
31835  if( isReadonly )              ctrlFlags |= UNIXFILE_RDONLY;
31836  if( noLock )                  ctrlFlags |= UNIXFILE_NOLOCK;
31837  if( syncDir )                 ctrlFlags |= UNIXFILE_DIRSYNC;
31838  if( flags & SQLITE_OPEN_URI ) ctrlFlags |= UNIXFILE_URI;
31839
31840#if SQLITE_ENABLE_LOCKING_STYLE
31841#if SQLITE_PREFER_PROXY_LOCKING
31842  isAutoProxy = 1;
31843#endif
31844  if( isAutoProxy && (zPath!=NULL) && (!noLock) && pVfs->xOpen ){
31845    char *envforce = getenv("SQLITE_FORCE_PROXY_LOCKING");
31846    int useProxy = 0;
31847
31848    /* SQLITE_FORCE_PROXY_LOCKING==1 means force always use proxy, 0 means
31849    ** never use proxy, NULL means use proxy for non-local files only.  */
31850    if( envforce!=NULL ){
31851      useProxy = atoi(envforce)>0;
31852    }else{
31853      useProxy = !(fsInfo.f_flags&MNT_LOCAL);
31854    }
31855    if( useProxy ){
31856      rc = fillInUnixFile(pVfs, fd, pFile, zPath, ctrlFlags);
31857      if( rc==SQLITE_OK ){
31858        rc = proxyTransformUnixFile((unixFile*)pFile, ":auto:");
31859        if( rc!=SQLITE_OK ){
31860          /* Use unixClose to clean up the resources added in fillInUnixFile
31861          ** and clear all the structure's references.  Specifically,
31862          ** pFile->pMethods will be NULL so sqlite3OsClose will be a no-op
31863          */
31864          unixClose(pFile);
31865          return rc;
31866        }
31867      }
31868      goto open_finished;
31869    }
31870  }
31871#endif
31872
31873  rc = fillInUnixFile(pVfs, fd, pFile, zPath, ctrlFlags);
31874
31875open_finished:
31876  if( rc!=SQLITE_OK ){
31877    sqlite3_free(p->pUnused);
31878  }
31879  return rc;
31880}
31881
31882
31883/*
31884** Delete the file at zPath. If the dirSync argument is true, fsync()
31885** the directory after deleting the file.
31886*/
31887static int unixDelete(
31888  sqlite3_vfs *NotUsed,     /* VFS containing this as the xDelete method */
31889  const char *zPath,        /* Name of file to be deleted */
31890  int dirSync               /* If true, fsync() directory after deleting file */
31891){
31892  int rc = SQLITE_OK;
31893  UNUSED_PARAMETER(NotUsed);
31894  SimulateIOError(return SQLITE_IOERR_DELETE);
31895  if( osUnlink(zPath)==(-1) ){
31896    if( errno==ENOENT
31897#if OS_VXWORKS
31898        || osAccess(zPath,0)!=0
31899#endif
31900    ){
31901      rc = SQLITE_IOERR_DELETE_NOENT;
31902    }else{
31903      rc = unixLogError(SQLITE_IOERR_DELETE, "unlink", zPath);
31904    }
31905    return rc;
31906  }
31907#ifndef SQLITE_DISABLE_DIRSYNC
31908  if( (dirSync & 1)!=0 ){
31909    int fd;
31910    rc = osOpenDirectory(zPath, &fd);
31911    if( rc==SQLITE_OK ){
31912#if OS_VXWORKS
31913      if( fsync(fd)==-1 )
31914#else
31915      if( fsync(fd) )
31916#endif
31917      {
31918        rc = unixLogError(SQLITE_IOERR_DIR_FSYNC, "fsync", zPath);
31919      }
31920      robust_close(0, fd, __LINE__);
31921    }else if( rc==SQLITE_CANTOPEN ){
31922      rc = SQLITE_OK;
31923    }
31924  }
31925#endif
31926  return rc;
31927}
31928
31929/*
31930** Test the existence of or access permissions of file zPath. The
31931** test performed depends on the value of flags:
31932**
31933**     SQLITE_ACCESS_EXISTS: Return 1 if the file exists
31934**     SQLITE_ACCESS_READWRITE: Return 1 if the file is read and writable.
31935**     SQLITE_ACCESS_READONLY: Return 1 if the file is readable.
31936**
31937** Otherwise return 0.
31938*/
31939static int unixAccess(
31940  sqlite3_vfs *NotUsed,   /* The VFS containing this xAccess method */
31941  const char *zPath,      /* Path of the file to examine */
31942  int flags,              /* What do we want to learn about the zPath file? */
31943  int *pResOut            /* Write result boolean here */
31944){
31945  int amode = 0;
31946  UNUSED_PARAMETER(NotUsed);
31947  SimulateIOError( return SQLITE_IOERR_ACCESS; );
31948  switch( flags ){
31949    case SQLITE_ACCESS_EXISTS:
31950      amode = F_OK;
31951      break;
31952    case SQLITE_ACCESS_READWRITE:
31953      amode = W_OK|R_OK;
31954      break;
31955    case SQLITE_ACCESS_READ:
31956      amode = R_OK;
31957      break;
31958
31959    default:
31960      assert(!"Invalid flags argument");
31961  }
31962  *pResOut = (osAccess(zPath, amode)==0);
31963  if( flags==SQLITE_ACCESS_EXISTS && *pResOut ){
31964    struct stat buf;
31965    if( 0==osStat(zPath, &buf) && buf.st_size==0 ){
31966      *pResOut = 0;
31967    }
31968  }
31969  return SQLITE_OK;
31970}
31971
31972
31973/*
31974** Turn a relative pathname into a full pathname. The relative path
31975** is stored as a nul-terminated string in the buffer pointed to by
31976** zPath.
31977**
31978** zOut points to a buffer of at least sqlite3_vfs.mxPathname bytes
31979** (in this case, MAX_PATHNAME bytes). The full-path is written to
31980** this buffer before returning.
31981*/
31982static int unixFullPathname(
31983  sqlite3_vfs *pVfs,            /* Pointer to vfs object */
31984  const char *zPath,            /* Possibly relative input path */
31985  int nOut,                     /* Size of output buffer in bytes */
31986  char *zOut                    /* Output buffer */
31987){
31988
31989  /* It's odd to simulate an io-error here, but really this is just
31990  ** using the io-error infrastructure to test that SQLite handles this
31991  ** function failing. This function could fail if, for example, the
31992  ** current working directory has been unlinked.
31993  */
31994  SimulateIOError( return SQLITE_ERROR );
31995
31996  assert( pVfs->mxPathname==MAX_PATHNAME );
31997  UNUSED_PARAMETER(pVfs);
31998
31999  zOut[nOut-1] = '\0';
32000  if( zPath[0]=='/' ){
32001    sqlite3_snprintf(nOut, zOut, "%s", zPath);
32002  }else{
32003    int nCwd;
32004    if( osGetcwd(zOut, nOut-1)==0 ){
32005      return unixLogError(SQLITE_CANTOPEN_BKPT, "getcwd", zPath);
32006    }
32007    nCwd = (int)strlen(zOut);
32008    sqlite3_snprintf(nOut-nCwd, &zOut[nCwd], "/%s", zPath);
32009  }
32010  return SQLITE_OK;
32011}
32012
32013
32014#ifndef SQLITE_OMIT_LOAD_EXTENSION
32015/*
32016** Interfaces for opening a shared library, finding entry points
32017** within the shared library, and closing the shared library.
32018*/
32019#include <dlfcn.h>
32020static void *unixDlOpen(sqlite3_vfs *NotUsed, const char *zFilename){
32021  UNUSED_PARAMETER(NotUsed);
32022  return dlopen(zFilename, RTLD_NOW | RTLD_GLOBAL);
32023}
32024
32025/*
32026** SQLite calls this function immediately after a call to unixDlSym() or
32027** unixDlOpen() fails (returns a null pointer). If a more detailed error
32028** message is available, it is written to zBufOut. If no error message
32029** is available, zBufOut is left unmodified and SQLite uses a default
32030** error message.
32031*/
32032static void unixDlError(sqlite3_vfs *NotUsed, int nBuf, char *zBufOut){
32033  const char *zErr;
32034  UNUSED_PARAMETER(NotUsed);
32035  unixEnterMutex();
32036  zErr = dlerror();
32037  if( zErr ){
32038    sqlite3_snprintf(nBuf, zBufOut, "%s", zErr);
32039  }
32040  unixLeaveMutex();
32041}
32042static void (*unixDlSym(sqlite3_vfs *NotUsed, void *p, const char*zSym))(void){
32043  /*
32044  ** GCC with -pedantic-errors says that C90 does not allow a void* to be
32045  ** cast into a pointer to a function.  And yet the library dlsym() routine
32046  ** returns a void* which is really a pointer to a function.  So how do we
32047  ** use dlsym() with -pedantic-errors?
32048  **
32049  ** Variable x below is defined to be a pointer to a function taking
32050  ** parameters void* and const char* and returning a pointer to a function.
32051  ** We initialize x by assigning it a pointer to the dlsym() function.
32052  ** (That assignment requires a cast.)  Then we call the function that
32053  ** x points to.
32054  **
32055  ** This work-around is unlikely to work correctly on any system where
32056  ** you really cannot cast a function pointer into void*.  But then, on the
32057  ** other hand, dlsym() will not work on such a system either, so we have
32058  ** not really lost anything.
32059  */
32060  void (*(*x)(void*,const char*))(void);
32061  UNUSED_PARAMETER(NotUsed);
32062  x = (void(*(*)(void*,const char*))(void))dlsym;
32063  return (*x)(p, zSym);
32064}
32065static void unixDlClose(sqlite3_vfs *NotUsed, void *pHandle){
32066  UNUSED_PARAMETER(NotUsed);
32067  dlclose(pHandle);
32068}
32069#else /* if SQLITE_OMIT_LOAD_EXTENSION is defined: */
32070  #define unixDlOpen  0
32071  #define unixDlError 0
32072  #define unixDlSym   0
32073  #define unixDlClose 0
32074#endif
32075
32076/*
32077** Write nBuf bytes of random data to the supplied buffer zBuf.
32078*/
32079static int unixRandomness(sqlite3_vfs *NotUsed, int nBuf, char *zBuf){
32080  UNUSED_PARAMETER(NotUsed);
32081  assert((size_t)nBuf>=(sizeof(time_t)+sizeof(int)));
32082
32083  /* We have to initialize zBuf to prevent valgrind from reporting
32084  ** errors.  The reports issued by valgrind are incorrect - we would
32085  ** prefer that the randomness be increased by making use of the
32086  ** uninitialized space in zBuf - but valgrind errors tend to worry
32087  ** some users.  Rather than argue, it seems easier just to initialize
32088  ** the whole array and silence valgrind, even if that means less randomness
32089  ** in the random seed.
32090  **
32091  ** When testing, initializing zBuf[] to zero is all we do.  That means
32092  ** that we always use the same random number sequence.  This makes the
32093  ** tests repeatable.
32094  */
32095  memset(zBuf, 0, nBuf);
32096  randomnessPid = osGetpid(0);
32097#if !defined(SQLITE_TEST) && !defined(SQLITE_OMIT_RANDOMNESS)
32098  {
32099    int fd, got;
32100    fd = robust_open("/dev/urandom", O_RDONLY, 0);
32101    if( fd<0 ){
32102      time_t t;
32103      time(&t);
32104      memcpy(zBuf, &t, sizeof(t));
32105      memcpy(&zBuf[sizeof(t)], &randomnessPid, sizeof(randomnessPid));
32106      assert( sizeof(t)+sizeof(randomnessPid)<=(size_t)nBuf );
32107      nBuf = sizeof(t) + sizeof(randomnessPid);
32108    }else{
32109      do{ got = osRead(fd, zBuf, nBuf); }while( got<0 && errno==EINTR );
32110      robust_close(0, fd, __LINE__);
32111    }
32112  }
32113#endif
32114  return nBuf;
32115}
32116
32117
32118/*
32119** Sleep for a little while.  Return the amount of time slept.
32120** The argument is the number of microseconds we want to sleep.
32121** The return value is the number of microseconds of sleep actually
32122** requested from the underlying operating system, a number which
32123** might be greater than or equal to the argument, but not less
32124** than the argument.
32125*/
32126static int unixSleep(sqlite3_vfs *NotUsed, int microseconds){
32127#if OS_VXWORKS
32128  struct timespec sp;
32129
32130  sp.tv_sec = microseconds / 1000000;
32131  sp.tv_nsec = (microseconds % 1000000) * 1000;
32132  nanosleep(&sp, NULL);
32133  UNUSED_PARAMETER(NotUsed);
32134  return microseconds;
32135#elif defined(HAVE_USLEEP) && HAVE_USLEEP
32136  usleep(microseconds);
32137  UNUSED_PARAMETER(NotUsed);
32138  return microseconds;
32139#else
32140  int seconds = (microseconds+999999)/1000000;
32141  sleep(seconds);
32142  UNUSED_PARAMETER(NotUsed);
32143  return seconds*1000000;
32144#endif
32145}
32146
32147/*
32148** The following variable, if set to a non-zero value, is interpreted as
32149** the number of seconds since 1970 and is used to set the result of
32150** sqlite3OsCurrentTime() during testing.
32151*/
32152#ifdef SQLITE_TEST
32153SQLITE_API int sqlite3_current_time = 0;  /* Fake system time in seconds since 1970. */
32154#endif
32155
32156/*
32157** Find the current time (in Universal Coordinated Time).  Write into *piNow
32158** the current time and date as a Julian Day number times 86_400_000.  In
32159** other words, write into *piNow the number of milliseconds since the Julian
32160** epoch of noon in Greenwich on November 24, 4714 B.C according to the
32161** proleptic Gregorian calendar.
32162**
32163** On success, return SQLITE_OK.  Return SQLITE_ERROR if the time and date
32164** cannot be found.
32165*/
32166static int unixCurrentTimeInt64(sqlite3_vfs *NotUsed, sqlite3_int64 *piNow){
32167  static const sqlite3_int64 unixEpoch = 24405875*(sqlite3_int64)8640000;
32168  int rc = SQLITE_OK;
32169#if defined(NO_GETTOD)
32170  time_t t;
32171  time(&t);
32172  *piNow = ((sqlite3_int64)t)*1000 + unixEpoch;
32173#elif OS_VXWORKS
32174  struct timespec sNow;
32175  clock_gettime(CLOCK_REALTIME, &sNow);
32176  *piNow = unixEpoch + 1000*(sqlite3_int64)sNow.tv_sec + sNow.tv_nsec/1000000;
32177#else
32178  struct timeval sNow;
32179  if( gettimeofday(&sNow, 0)==0 ){
32180    *piNow = unixEpoch + 1000*(sqlite3_int64)sNow.tv_sec + sNow.tv_usec/1000;
32181  }else{
32182    rc = SQLITE_ERROR;
32183  }
32184#endif
32185
32186#ifdef SQLITE_TEST
32187  if( sqlite3_current_time ){
32188    *piNow = 1000*(sqlite3_int64)sqlite3_current_time + unixEpoch;
32189  }
32190#endif
32191  UNUSED_PARAMETER(NotUsed);
32192  return rc;
32193}
32194
32195/*
32196** Find the current time (in Universal Coordinated Time).  Write the
32197** current time and date as a Julian Day number into *prNow and
32198** return 0.  Return 1 if the time and date cannot be found.
32199*/
32200static int unixCurrentTime(sqlite3_vfs *NotUsed, double *prNow){
32201  sqlite3_int64 i = 0;
32202  int rc;
32203  UNUSED_PARAMETER(NotUsed);
32204  rc = unixCurrentTimeInt64(0, &i);
32205  *prNow = i/86400000.0;
32206  return rc;
32207}
32208
32209/*
32210** We added the xGetLastError() method with the intention of providing
32211** better low-level error messages when operating-system problems come up
32212** during SQLite operation.  But so far, none of that has been implemented
32213** in the core.  So this routine is never called.  For now, it is merely
32214** a place-holder.
32215*/
32216static int unixGetLastError(sqlite3_vfs *NotUsed, int NotUsed2, char *NotUsed3){
32217  UNUSED_PARAMETER(NotUsed);
32218  UNUSED_PARAMETER(NotUsed2);
32219  UNUSED_PARAMETER(NotUsed3);
32220  return 0;
32221}
32222
32223
32224/*
32225************************ End of sqlite3_vfs methods ***************************
32226******************************************************************************/
32227
32228/******************************************************************************
32229************************** Begin Proxy Locking ********************************
32230**
32231** Proxy locking is a "uber-locking-method" in this sense:  It uses the
32232** other locking methods on secondary lock files.  Proxy locking is a
32233** meta-layer over top of the primitive locking implemented above.  For
32234** this reason, the division that implements of proxy locking is deferred
32235** until late in the file (here) after all of the other I/O methods have
32236** been defined - so that the primitive locking methods are available
32237** as services to help with the implementation of proxy locking.
32238**
32239****
32240**
32241** The default locking schemes in SQLite use byte-range locks on the
32242** database file to coordinate safe, concurrent access by multiple readers
32243** and writers [http://sqlite.org/lockingv3.html].  The five file locking
32244** states (UNLOCKED, PENDING, SHARED, RESERVED, EXCLUSIVE) are implemented
32245** as POSIX read & write locks over fixed set of locations (via fsctl),
32246** on AFP and SMB only exclusive byte-range locks are available via fsctl
32247** with _IOWR('z', 23, struct ByteRangeLockPB2) to track the same 5 states.
32248** To simulate a F_RDLCK on the shared range, on AFP a randomly selected
32249** address in the shared range is taken for a SHARED lock, the entire
32250** shared range is taken for an EXCLUSIVE lock):
32251**
32252**      PENDING_BYTE        0x40000000
32253**      RESERVED_BYTE       0x40000001
32254**      SHARED_RANGE        0x40000002 -> 0x40000200
32255**
32256** This works well on the local file system, but shows a nearly 100x
32257** slowdown in read performance on AFP because the AFP client disables
32258** the read cache when byte-range locks are present.  Enabling the read
32259** cache exposes a cache coherency problem that is present on all OS X
32260** supported network file systems.  NFS and AFP both observe the
32261** close-to-open semantics for ensuring cache coherency
32262** [http://nfs.sourceforge.net/#faq_a8], which does not effectively
32263** address the requirements for concurrent database access by multiple
32264** readers and writers
32265** [http://www.nabble.com/SQLite-on-NFS-cache-coherency-td15655701.html].
32266**
32267** To address the performance and cache coherency issues, proxy file locking
32268** changes the way database access is controlled by limiting access to a
32269** single host at a time and moving file locks off of the database file
32270** and onto a proxy file on the local file system.
32271**
32272**
32273** Using proxy locks
32274** -----------------
32275**
32276** C APIs
32277**
32278**  sqlite3_file_control(db, dbname, SQLITE_FCNTL_SET_LOCKPROXYFILE,
32279**                       <proxy_path> | ":auto:");
32280**  sqlite3_file_control(db, dbname, SQLITE_FCNTL_GET_LOCKPROXYFILE,
32281**                       &<proxy_path>);
32282**
32283**
32284** SQL pragmas
32285**
32286**  PRAGMA [database.]lock_proxy_file=<proxy_path> | :auto:
32287**  PRAGMA [database.]lock_proxy_file
32288**
32289** Specifying ":auto:" means that if there is a conch file with a matching
32290** host ID in it, the proxy path in the conch file will be used, otherwise
32291** a proxy path based on the user's temp dir
32292** (via confstr(_CS_DARWIN_USER_TEMP_DIR,...)) will be used and the
32293** actual proxy file name is generated from the name and path of the
32294** database file.  For example:
32295**
32296**       For database path "/Users/me/foo.db"
32297**       The lock path will be "<tmpdir>/sqliteplocks/_Users_me_foo.db:auto:")
32298**
32299** Once a lock proxy is configured for a database connection, it can not
32300** be removed, however it may be switched to a different proxy path via
32301** the above APIs (assuming the conch file is not being held by another
32302** connection or process).
32303**
32304**
32305** How proxy locking works
32306** -----------------------
32307**
32308** Proxy file locking relies primarily on two new supporting files:
32309**
32310**   *  conch file to limit access to the database file to a single host
32311**      at a time
32312**
32313**   *  proxy file to act as a proxy for the advisory locks normally
32314**      taken on the database
32315**
32316** The conch file - to use a proxy file, sqlite must first "hold the conch"
32317** by taking an sqlite-style shared lock on the conch file, reading the
32318** contents and comparing the host's unique host ID (see below) and lock
32319** proxy path against the values stored in the conch.  The conch file is
32320** stored in the same directory as the database file and the file name
32321** is patterned after the database file name as ".<databasename>-conch".
32322** If the conch file does not exist, or its contents do not match the
32323** host ID and/or proxy path, then the lock is escalated to an exclusive
32324** lock and the conch file contents is updated with the host ID and proxy
32325** path and the lock is downgraded to a shared lock again.  If the conch
32326** is held by another process (with a shared lock), the exclusive lock
32327** will fail and SQLITE_BUSY is returned.
32328**
32329** The proxy file - a single-byte file used for all advisory file locks
32330** normally taken on the database file.   This allows for safe sharing
32331** of the database file for multiple readers and writers on the same
32332** host (the conch ensures that they all use the same local lock file).
32333**
32334** Requesting the lock proxy does not immediately take the conch, it is
32335** only taken when the first request to lock database file is made.
32336** This matches the semantics of the traditional locking behavior, where
32337** opening a connection to a database file does not take a lock on it.
32338** The shared lock and an open file descriptor are maintained until
32339** the connection to the database is closed.
32340**
32341** The proxy file and the lock file are never deleted so they only need
32342** to be created the first time they are used.
32343**
32344** Configuration options
32345** ---------------------
32346**
32347**  SQLITE_PREFER_PROXY_LOCKING
32348**
32349**       Database files accessed on non-local file systems are
32350**       automatically configured for proxy locking, lock files are
32351**       named automatically using the same logic as
32352**       PRAGMA lock_proxy_file=":auto:"
32353**
32354**  SQLITE_PROXY_DEBUG
32355**
32356**       Enables the logging of error messages during host id file
32357**       retrieval and creation
32358**
32359**  LOCKPROXYDIR
32360**
32361**       Overrides the default directory used for lock proxy files that
32362**       are named automatically via the ":auto:" setting
32363**
32364**  SQLITE_DEFAULT_PROXYDIR_PERMISSIONS
32365**
32366**       Permissions to use when creating a directory for storing the
32367**       lock proxy files, only used when LOCKPROXYDIR is not set.
32368**
32369**
32370** As mentioned above, when compiled with SQLITE_PREFER_PROXY_LOCKING,
32371** setting the environment variable SQLITE_FORCE_PROXY_LOCKING to 1 will
32372** force proxy locking to be used for every database file opened, and 0
32373** will force automatic proxy locking to be disabled for all database
32374** files (explicitly calling the SQLITE_FCNTL_SET_LOCKPROXYFILE pragma or
32375** sqlite_file_control API is not affected by SQLITE_FORCE_PROXY_LOCKING).
32376*/
32377
32378/*
32379** Proxy locking is only available on MacOSX
32380*/
32381#if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
32382
32383/*
32384** The proxyLockingContext has the path and file structures for the remote
32385** and local proxy files in it
32386*/
32387typedef struct proxyLockingContext proxyLockingContext;
32388struct proxyLockingContext {
32389  unixFile *conchFile;         /* Open conch file */
32390  char *conchFilePath;         /* Name of the conch file */
32391  unixFile *lockProxy;         /* Open proxy lock file */
32392  char *lockProxyPath;         /* Name of the proxy lock file */
32393  char *dbPath;                /* Name of the open file */
32394  int conchHeld;               /* 1 if the conch is held, -1 if lockless */
32395  int nFails;                  /* Number of conch taking failures */
32396  void *oldLockingContext;     /* Original lockingcontext to restore on close */
32397  sqlite3_io_methods const *pOldMethod;     /* Original I/O methods for close */
32398};
32399
32400/*
32401** The proxy lock file path for the database at dbPath is written into lPath,
32402** which must point to valid, writable memory large enough for a maxLen length
32403** file path.
32404*/
32405static int proxyGetLockPath(const char *dbPath, char *lPath, size_t maxLen){
32406  int len;
32407  int dbLen;
32408  int i;
32409
32410#ifdef LOCKPROXYDIR
32411  len = strlcpy(lPath, LOCKPROXYDIR, maxLen);
32412#else
32413# ifdef _CS_DARWIN_USER_TEMP_DIR
32414  {
32415    if( !confstr(_CS_DARWIN_USER_TEMP_DIR, lPath, maxLen) ){
32416      OSTRACE(("GETLOCKPATH  failed %s errno=%d pid=%d\n",
32417               lPath, errno, osGetpid(0)));
32418      return SQLITE_IOERR_LOCK;
32419    }
32420    len = strlcat(lPath, "sqliteplocks", maxLen);
32421  }
32422# else
32423  len = strlcpy(lPath, "/tmp/", maxLen);
32424# endif
32425#endif
32426
32427  if( lPath[len-1]!='/' ){
32428    len = strlcat(lPath, "/", maxLen);
32429  }
32430
32431  /* transform the db path to a unique cache name */
32432  dbLen = (int)strlen(dbPath);
32433  for( i=0; i<dbLen && (i+len+7)<(int)maxLen; i++){
32434    char c = dbPath[i];
32435    lPath[i+len] = (c=='/')?'_':c;
32436  }
32437  lPath[i+len]='\0';
32438  strlcat(lPath, ":auto:", maxLen);
32439  OSTRACE(("GETLOCKPATH  proxy lock path=%s pid=%d\n", lPath, osGetpid(0)));
32440  return SQLITE_OK;
32441}
32442
32443/*
32444 ** Creates the lock file and any missing directories in lockPath
32445 */
32446static int proxyCreateLockPath(const char *lockPath){
32447  int i, len;
32448  char buf[MAXPATHLEN];
32449  int start = 0;
32450
32451  assert(lockPath!=NULL);
32452  /* try to create all the intermediate directories */
32453  len = (int)strlen(lockPath);
32454  buf[0] = lockPath[0];
32455  for( i=1; i<len; i++ ){
32456    if( lockPath[i] == '/' && (i - start > 0) ){
32457      /* only mkdir if leaf dir != "." or "/" or ".." */
32458      if( i-start>2 || (i-start==1 && buf[start] != '.' && buf[start] != '/')
32459         || (i-start==2 && buf[start] != '.' && buf[start+1] != '.') ){
32460        buf[i]='\0';
32461        if( osMkdir(buf, SQLITE_DEFAULT_PROXYDIR_PERMISSIONS) ){
32462          int err=errno;
32463          if( err!=EEXIST ) {
32464            OSTRACE(("CREATELOCKPATH  FAILED creating %s, "
32465                     "'%s' proxy lock path=%s pid=%d\n",
32466                     buf, strerror(err), lockPath, osGetpid(0)));
32467            return err;
32468          }
32469        }
32470      }
32471      start=i+1;
32472    }
32473    buf[i] = lockPath[i];
32474  }
32475  OSTRACE(("CREATELOCKPATH  proxy lock path=%s pid=%d\n", lockPath, osGetpid(0)));
32476  return 0;
32477}
32478
32479/*
32480** Create a new VFS file descriptor (stored in memory obtained from
32481** sqlite3_malloc) and open the file named "path" in the file descriptor.
32482**
32483** The caller is responsible not only for closing the file descriptor
32484** but also for freeing the memory associated with the file descriptor.
32485*/
32486static int proxyCreateUnixFile(
32487    const char *path,        /* path for the new unixFile */
32488    unixFile **ppFile,       /* unixFile created and returned by ref */
32489    int islockfile           /* if non zero missing dirs will be created */
32490) {
32491  int fd = -1;
32492  unixFile *pNew;
32493  int rc = SQLITE_OK;
32494  int openFlags = O_RDWR | O_CREAT;
32495  sqlite3_vfs dummyVfs;
32496  int terrno = 0;
32497  UnixUnusedFd *pUnused = NULL;
32498
32499  /* 1. first try to open/create the file
32500  ** 2. if that fails, and this is a lock file (not-conch), try creating
32501  ** the parent directories and then try again.
32502  ** 3. if that fails, try to open the file read-only
32503  ** otherwise return BUSY (if lock file) or CANTOPEN for the conch file
32504  */
32505  pUnused = findReusableFd(path, openFlags);
32506  if( pUnused ){
32507    fd = pUnused->fd;
32508  }else{
32509    pUnused = sqlite3_malloc64(sizeof(*pUnused));
32510    if( !pUnused ){
32511      return SQLITE_NOMEM;
32512    }
32513  }
32514  if( fd<0 ){
32515    fd = robust_open(path, openFlags, 0);
32516    terrno = errno;
32517    if( fd<0 && errno==ENOENT && islockfile ){
32518      if( proxyCreateLockPath(path) == SQLITE_OK ){
32519        fd = robust_open(path, openFlags, 0);
32520      }
32521    }
32522  }
32523  if( fd<0 ){
32524    openFlags = O_RDONLY;
32525    fd = robust_open(path, openFlags, 0);
32526    terrno = errno;
32527  }
32528  if( fd<0 ){
32529    if( islockfile ){
32530      return SQLITE_BUSY;
32531    }
32532    switch (terrno) {
32533      case EACCES:
32534        return SQLITE_PERM;
32535      case EIO:
32536        return SQLITE_IOERR_LOCK; /* even though it is the conch */
32537      default:
32538        return SQLITE_CANTOPEN_BKPT;
32539    }
32540  }
32541
32542  pNew = (unixFile *)sqlite3_malloc64(sizeof(*pNew));
32543  if( pNew==NULL ){
32544    rc = SQLITE_NOMEM;
32545    goto end_create_proxy;
32546  }
32547  memset(pNew, 0, sizeof(unixFile));
32548  pNew->openFlags = openFlags;
32549  memset(&dummyVfs, 0, sizeof(dummyVfs));
32550  dummyVfs.pAppData = (void*)&autolockIoFinder;
32551  dummyVfs.zName = "dummy";
32552  pUnused->fd = fd;
32553  pUnused->flags = openFlags;
32554  pNew->pUnused = pUnused;
32555
32556  rc = fillInUnixFile(&dummyVfs, fd, (sqlite3_file*)pNew, path, 0);
32557  if( rc==SQLITE_OK ){
32558    *ppFile = pNew;
32559    return SQLITE_OK;
32560  }
32561end_create_proxy:
32562  robust_close(pNew, fd, __LINE__);
32563  sqlite3_free(pNew);
32564  sqlite3_free(pUnused);
32565  return rc;
32566}
32567
32568#ifdef SQLITE_TEST
32569/* simulate multiple hosts by creating unique hostid file paths */
32570SQLITE_API int sqlite3_hostid_num = 0;
32571#endif
32572
32573#define PROXY_HOSTIDLEN    16  /* conch file host id length */
32574
32575#ifdef HAVE_GETHOSTUUID
32576/* Not always defined in the headers as it ought to be */
32577extern int gethostuuid(uuid_t id, const struct timespec *wait);
32578#endif
32579
32580/* get the host ID via gethostuuid(), pHostID must point to PROXY_HOSTIDLEN
32581** bytes of writable memory.
32582*/
32583static int proxyGetHostID(unsigned char *pHostID, int *pError){
32584  assert(PROXY_HOSTIDLEN == sizeof(uuid_t));
32585  memset(pHostID, 0, PROXY_HOSTIDLEN);
32586#ifdef HAVE_GETHOSTUUID
32587  {
32588    struct timespec timeout = {1, 0}; /* 1 sec timeout */
32589    if( gethostuuid(pHostID, &timeout) ){
32590      int err = errno;
32591      if( pError ){
32592        *pError = err;
32593      }
32594      return SQLITE_IOERR;
32595    }
32596  }
32597#else
32598  UNUSED_PARAMETER(pError);
32599#endif
32600#ifdef SQLITE_TEST
32601  /* simulate multiple hosts by creating unique hostid file paths */
32602  if( sqlite3_hostid_num != 0){
32603    pHostID[0] = (char)(pHostID[0] + (char)(sqlite3_hostid_num & 0xFF));
32604  }
32605#endif
32606
32607  return SQLITE_OK;
32608}
32609
32610/* The conch file contains the header, host id and lock file path
32611 */
32612#define PROXY_CONCHVERSION 2   /* 1-byte header, 16-byte host id, path */
32613#define PROXY_HEADERLEN    1   /* conch file header length */
32614#define PROXY_PATHINDEX    (PROXY_HEADERLEN+PROXY_HOSTIDLEN)
32615#define PROXY_MAXCONCHLEN  (PROXY_HEADERLEN+PROXY_HOSTIDLEN+MAXPATHLEN)
32616
32617/*
32618** Takes an open conch file, copies the contents to a new path and then moves
32619** it back.  The newly created file's file descriptor is assigned to the
32620** conch file structure and finally the original conch file descriptor is
32621** closed.  Returns zero if successful.
32622*/
32623static int proxyBreakConchLock(unixFile *pFile, uuid_t myHostID){
32624  proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
32625  unixFile *conchFile = pCtx->conchFile;
32626  char tPath[MAXPATHLEN];
32627  char buf[PROXY_MAXCONCHLEN];
32628  char *cPath = pCtx->conchFilePath;
32629  size_t readLen = 0;
32630  size_t pathLen = 0;
32631  char errmsg[64] = "";
32632  int fd = -1;
32633  int rc = -1;
32634  UNUSED_PARAMETER(myHostID);
32635
32636  /* create a new path by replace the trailing '-conch' with '-break' */
32637  pathLen = strlcpy(tPath, cPath, MAXPATHLEN);
32638  if( pathLen>MAXPATHLEN || pathLen<6 ||
32639     (strlcpy(&tPath[pathLen-5], "break", 6) != 5) ){
32640    sqlite3_snprintf(sizeof(errmsg),errmsg,"path error (len %d)",(int)pathLen);
32641    goto end_breaklock;
32642  }
32643  /* read the conch content */
32644  readLen = osPread(conchFile->h, buf, PROXY_MAXCONCHLEN, 0);
32645  if( readLen<PROXY_PATHINDEX ){
32646    sqlite3_snprintf(sizeof(errmsg),errmsg,"read error (len %d)",(int)readLen);
32647    goto end_breaklock;
32648  }
32649  /* write it out to the temporary break file */
32650  fd = robust_open(tPath, (O_RDWR|O_CREAT|O_EXCL), 0);
32651  if( fd<0 ){
32652    sqlite3_snprintf(sizeof(errmsg), errmsg, "create failed (%d)", errno);
32653    goto end_breaklock;
32654  }
32655  if( osPwrite(fd, buf, readLen, 0) != (ssize_t)readLen ){
32656    sqlite3_snprintf(sizeof(errmsg), errmsg, "write failed (%d)", errno);
32657    goto end_breaklock;
32658  }
32659  if( rename(tPath, cPath) ){
32660    sqlite3_snprintf(sizeof(errmsg), errmsg, "rename failed (%d)", errno);
32661    goto end_breaklock;
32662  }
32663  rc = 0;
32664  fprintf(stderr, "broke stale lock on %s\n", cPath);
32665  robust_close(pFile, conchFile->h, __LINE__);
32666  conchFile->h = fd;
32667  conchFile->openFlags = O_RDWR | O_CREAT;
32668
32669end_breaklock:
32670  if( rc ){
32671    if( fd>=0 ){
32672      osUnlink(tPath);
32673      robust_close(pFile, fd, __LINE__);
32674    }
32675    fprintf(stderr, "failed to break stale lock on %s, %s\n", cPath, errmsg);
32676  }
32677  return rc;
32678}
32679
32680/* Take the requested lock on the conch file and break a stale lock if the
32681** host id matches.
32682*/
32683static int proxyConchLock(unixFile *pFile, uuid_t myHostID, int lockType){
32684  proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
32685  unixFile *conchFile = pCtx->conchFile;
32686  int rc = SQLITE_OK;
32687  int nTries = 0;
32688  struct timespec conchModTime;
32689
32690  memset(&conchModTime, 0, sizeof(conchModTime));
32691  do {
32692    rc = conchFile->pMethod->xLock((sqlite3_file*)conchFile, lockType);
32693    nTries ++;
32694    if( rc==SQLITE_BUSY ){
32695      /* If the lock failed (busy):
32696       * 1st try: get the mod time of the conch, wait 0.5s and try again.
32697       * 2nd try: fail if the mod time changed or host id is different, wait
32698       *           10 sec and try again
32699       * 3rd try: break the lock unless the mod time has changed.
32700       */
32701      struct stat buf;
32702      if( osFstat(conchFile->h, &buf) ){
32703        storeLastErrno(pFile, errno);
32704        return SQLITE_IOERR_LOCK;
32705      }
32706
32707      if( nTries==1 ){
32708        conchModTime = buf.st_mtimespec;
32709        usleep(500000); /* wait 0.5 sec and try the lock again*/
32710        continue;
32711      }
32712
32713      assert( nTries>1 );
32714      if( conchModTime.tv_sec != buf.st_mtimespec.tv_sec ||
32715         conchModTime.tv_nsec != buf.st_mtimespec.tv_nsec ){
32716        return SQLITE_BUSY;
32717      }
32718
32719      if( nTries==2 ){
32720        char tBuf[PROXY_MAXCONCHLEN];
32721        int len = osPread(conchFile->h, tBuf, PROXY_MAXCONCHLEN, 0);
32722        if( len<0 ){
32723          storeLastErrno(pFile, errno);
32724          return SQLITE_IOERR_LOCK;
32725        }
32726        if( len>PROXY_PATHINDEX && tBuf[0]==(char)PROXY_CONCHVERSION){
32727          /* don't break the lock if the host id doesn't match */
32728          if( 0!=memcmp(&tBuf[PROXY_HEADERLEN], myHostID, PROXY_HOSTIDLEN) ){
32729            return SQLITE_BUSY;
32730          }
32731        }else{
32732          /* don't break the lock on short read or a version mismatch */
32733          return SQLITE_BUSY;
32734        }
32735        usleep(10000000); /* wait 10 sec and try the lock again */
32736        continue;
32737      }
32738
32739      assert( nTries==3 );
32740      if( 0==proxyBreakConchLock(pFile, myHostID) ){
32741        rc = SQLITE_OK;
32742        if( lockType==EXCLUSIVE_LOCK ){
32743          rc = conchFile->pMethod->xLock((sqlite3_file*)conchFile, SHARED_LOCK);
32744        }
32745        if( !rc ){
32746          rc = conchFile->pMethod->xLock((sqlite3_file*)conchFile, lockType);
32747        }
32748      }
32749    }
32750  } while( rc==SQLITE_BUSY && nTries<3 );
32751
32752  return rc;
32753}
32754
32755/* Takes the conch by taking a shared lock and read the contents conch, if
32756** lockPath is non-NULL, the host ID and lock file path must match.  A NULL
32757** lockPath means that the lockPath in the conch file will be used if the
32758** host IDs match, or a new lock path will be generated automatically
32759** and written to the conch file.
32760*/
32761static int proxyTakeConch(unixFile *pFile){
32762  proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
32763
32764  if( pCtx->conchHeld!=0 ){
32765    return SQLITE_OK;
32766  }else{
32767    unixFile *conchFile = pCtx->conchFile;
32768    uuid_t myHostID;
32769    int pError = 0;
32770    char readBuf[PROXY_MAXCONCHLEN];
32771    char lockPath[MAXPATHLEN];
32772    char *tempLockPath = NULL;
32773    int rc = SQLITE_OK;
32774    int createConch = 0;
32775    int hostIdMatch = 0;
32776    int readLen = 0;
32777    int tryOldLockPath = 0;
32778    int forceNewLockPath = 0;
32779
32780    OSTRACE(("TAKECONCH  %d for %s pid=%d\n", conchFile->h,
32781             (pCtx->lockProxyPath ? pCtx->lockProxyPath : ":auto:"),
32782             osGetpid(0)));
32783
32784    rc = proxyGetHostID(myHostID, &pError);
32785    if( (rc&0xff)==SQLITE_IOERR ){
32786      storeLastErrno(pFile, pError);
32787      goto end_takeconch;
32788    }
32789    rc = proxyConchLock(pFile, myHostID, SHARED_LOCK);
32790    if( rc!=SQLITE_OK ){
32791      goto end_takeconch;
32792    }
32793    /* read the existing conch file */
32794    readLen = seekAndRead((unixFile*)conchFile, 0, readBuf, PROXY_MAXCONCHLEN);
32795    if( readLen<0 ){
32796      /* I/O error: lastErrno set by seekAndRead */
32797      storeLastErrno(pFile, conchFile->lastErrno);
32798      rc = SQLITE_IOERR_READ;
32799      goto end_takeconch;
32800    }else if( readLen<=(PROXY_HEADERLEN+PROXY_HOSTIDLEN) ||
32801             readBuf[0]!=(char)PROXY_CONCHVERSION ){
32802      /* a short read or version format mismatch means we need to create a new
32803      ** conch file.
32804      */
32805      createConch = 1;
32806    }
32807    /* if the host id matches and the lock path already exists in the conch
32808    ** we'll try to use the path there, if we can't open that path, we'll
32809    ** retry with a new auto-generated path
32810    */
32811    do { /* in case we need to try again for an :auto: named lock file */
32812
32813      if( !createConch && !forceNewLockPath ){
32814        hostIdMatch = !memcmp(&readBuf[PROXY_HEADERLEN], myHostID,
32815                                  PROXY_HOSTIDLEN);
32816        /* if the conch has data compare the contents */
32817        if( !pCtx->lockProxyPath ){
32818          /* for auto-named local lock file, just check the host ID and we'll
32819           ** use the local lock file path that's already in there
32820           */
32821          if( hostIdMatch ){
32822            size_t pathLen = (readLen - PROXY_PATHINDEX);
32823
32824            if( pathLen>=MAXPATHLEN ){
32825              pathLen=MAXPATHLEN-1;
32826            }
32827            memcpy(lockPath, &readBuf[PROXY_PATHINDEX], pathLen);
32828            lockPath[pathLen] = 0;
32829            tempLockPath = lockPath;
32830            tryOldLockPath = 1;
32831            /* create a copy of the lock path if the conch is taken */
32832            goto end_takeconch;
32833          }
32834        }else if( hostIdMatch
32835               && !strncmp(pCtx->lockProxyPath, &readBuf[PROXY_PATHINDEX],
32836                           readLen-PROXY_PATHINDEX)
32837        ){
32838          /* conch host and lock path match */
32839          goto end_takeconch;
32840        }
32841      }
32842
32843      /* if the conch isn't writable and doesn't match, we can't take it */
32844      if( (conchFile->openFlags&O_RDWR) == 0 ){
32845        rc = SQLITE_BUSY;
32846        goto end_takeconch;
32847      }
32848
32849      /* either the conch didn't match or we need to create a new one */
32850      if( !pCtx->lockProxyPath ){
32851        proxyGetLockPath(pCtx->dbPath, lockPath, MAXPATHLEN);
32852        tempLockPath = lockPath;
32853        /* create a copy of the lock path _only_ if the conch is taken */
32854      }
32855
32856      /* update conch with host and path (this will fail if other process
32857      ** has a shared lock already), if the host id matches, use the big
32858      ** stick.
32859      */
32860      futimes(conchFile->h, NULL);
32861      if( hostIdMatch && !createConch ){
32862        if( conchFile->pInode && conchFile->pInode->nShared>1 ){
32863          /* We are trying for an exclusive lock but another thread in this
32864           ** same process is still holding a shared lock. */
32865          rc = SQLITE_BUSY;
32866        } else {
32867          rc = proxyConchLock(pFile, myHostID, EXCLUSIVE_LOCK);
32868        }
32869      }else{
32870        rc = proxyConchLock(pFile, myHostID, EXCLUSIVE_LOCK);
32871      }
32872      if( rc==SQLITE_OK ){
32873        char writeBuffer[PROXY_MAXCONCHLEN];
32874        int writeSize = 0;
32875
32876        writeBuffer[0] = (char)PROXY_CONCHVERSION;
32877        memcpy(&writeBuffer[PROXY_HEADERLEN], myHostID, PROXY_HOSTIDLEN);
32878        if( pCtx->lockProxyPath!=NULL ){
32879          strlcpy(&writeBuffer[PROXY_PATHINDEX], pCtx->lockProxyPath,
32880                  MAXPATHLEN);
32881        }else{
32882          strlcpy(&writeBuffer[PROXY_PATHINDEX], tempLockPath, MAXPATHLEN);
32883        }
32884        writeSize = PROXY_PATHINDEX + strlen(&writeBuffer[PROXY_PATHINDEX]);
32885        robust_ftruncate(conchFile->h, writeSize);
32886        rc = unixWrite((sqlite3_file *)conchFile, writeBuffer, writeSize, 0);
32887        fsync(conchFile->h);
32888        /* If we created a new conch file (not just updated the contents of a
32889         ** valid conch file), try to match the permissions of the database
32890         */
32891        if( rc==SQLITE_OK && createConch ){
32892          struct stat buf;
32893          int err = osFstat(pFile->h, &buf);
32894          if( err==0 ){
32895            mode_t cmode = buf.st_mode&(S_IRUSR|S_IWUSR | S_IRGRP|S_IWGRP |
32896                                        S_IROTH|S_IWOTH);
32897            /* try to match the database file R/W permissions, ignore failure */
32898#ifndef SQLITE_PROXY_DEBUG
32899            osFchmod(conchFile->h, cmode);
32900#else
32901            do{
32902              rc = osFchmod(conchFile->h, cmode);
32903            }while( rc==(-1) && errno==EINTR );
32904            if( rc!=0 ){
32905              int code = errno;
32906              fprintf(stderr, "fchmod %o FAILED with %d %s\n",
32907                      cmode, code, strerror(code));
32908            } else {
32909              fprintf(stderr, "fchmod %o SUCCEDED\n",cmode);
32910            }
32911          }else{
32912            int code = errno;
32913            fprintf(stderr, "STAT FAILED[%d] with %d %s\n",
32914                    err, code, strerror(code));
32915#endif
32916          }
32917        }
32918      }
32919      conchFile->pMethod->xUnlock((sqlite3_file*)conchFile, SHARED_LOCK);
32920
32921    end_takeconch:
32922      OSTRACE(("TRANSPROXY: CLOSE  %d\n", pFile->h));
32923      if( rc==SQLITE_OK && pFile->openFlags ){
32924        int fd;
32925        if( pFile->h>=0 ){
32926          robust_close(pFile, pFile->h, __LINE__);
32927        }
32928        pFile->h = -1;
32929        fd = robust_open(pCtx->dbPath, pFile->openFlags, 0);
32930        OSTRACE(("TRANSPROXY: OPEN  %d\n", fd));
32931        if( fd>=0 ){
32932          pFile->h = fd;
32933        }else{
32934          rc=SQLITE_CANTOPEN_BKPT; /* SQLITE_BUSY? proxyTakeConch called
32935           during locking */
32936        }
32937      }
32938      if( rc==SQLITE_OK && !pCtx->lockProxy ){
32939        char *path = tempLockPath ? tempLockPath : pCtx->lockProxyPath;
32940        rc = proxyCreateUnixFile(path, &pCtx->lockProxy, 1);
32941        if( rc!=SQLITE_OK && rc!=SQLITE_NOMEM && tryOldLockPath ){
32942          /* we couldn't create the proxy lock file with the old lock file path
32943           ** so try again via auto-naming
32944           */
32945          forceNewLockPath = 1;
32946          tryOldLockPath = 0;
32947          continue; /* go back to the do {} while start point, try again */
32948        }
32949      }
32950      if( rc==SQLITE_OK ){
32951        /* Need to make a copy of path if we extracted the value
32952         ** from the conch file or the path was allocated on the stack
32953         */
32954        if( tempLockPath ){
32955          pCtx->lockProxyPath = sqlite3DbStrDup(0, tempLockPath);
32956          if( !pCtx->lockProxyPath ){
32957            rc = SQLITE_NOMEM;
32958          }
32959        }
32960      }
32961      if( rc==SQLITE_OK ){
32962        pCtx->conchHeld = 1;
32963
32964        if( pCtx->lockProxy->pMethod == &afpIoMethods ){
32965          afpLockingContext *afpCtx;
32966          afpCtx = (afpLockingContext *)pCtx->lockProxy->lockingContext;
32967          afpCtx->dbPath = pCtx->lockProxyPath;
32968        }
32969      } else {
32970        conchFile->pMethod->xUnlock((sqlite3_file*)conchFile, NO_LOCK);
32971      }
32972      OSTRACE(("TAKECONCH  %d %s\n", conchFile->h,
32973               rc==SQLITE_OK?"ok":"failed"));
32974      return rc;
32975    } while (1); /* in case we need to retry the :auto: lock file -
32976                 ** we should never get here except via the 'continue' call. */
32977  }
32978}
32979
32980/*
32981** If pFile holds a lock on a conch file, then release that lock.
32982*/
32983static int proxyReleaseConch(unixFile *pFile){
32984  int rc = SQLITE_OK;         /* Subroutine return code */
32985  proxyLockingContext *pCtx;  /* The locking context for the proxy lock */
32986  unixFile *conchFile;        /* Name of the conch file */
32987
32988  pCtx = (proxyLockingContext *)pFile->lockingContext;
32989  conchFile = pCtx->conchFile;
32990  OSTRACE(("RELEASECONCH  %d for %s pid=%d\n", conchFile->h,
32991           (pCtx->lockProxyPath ? pCtx->lockProxyPath : ":auto:"),
32992           osGetpid(0)));
32993  if( pCtx->conchHeld>0 ){
32994    rc = conchFile->pMethod->xUnlock((sqlite3_file*)conchFile, NO_LOCK);
32995  }
32996  pCtx->conchHeld = 0;
32997  OSTRACE(("RELEASECONCH  %d %s\n", conchFile->h,
32998           (rc==SQLITE_OK ? "ok" : "failed")));
32999  return rc;
33000}
33001
33002/*
33003** Given the name of a database file, compute the name of its conch file.
33004** Store the conch filename in memory obtained from sqlite3_malloc64().
33005** Make *pConchPath point to the new name.  Return SQLITE_OK on success
33006** or SQLITE_NOMEM if unable to obtain memory.
33007**
33008** The caller is responsible for ensuring that the allocated memory
33009** space is eventually freed.
33010**
33011** *pConchPath is set to NULL if a memory allocation error occurs.
33012*/
33013static int proxyCreateConchPathname(char *dbPath, char **pConchPath){
33014  int i;                        /* Loop counter */
33015  int len = (int)strlen(dbPath); /* Length of database filename - dbPath */
33016  char *conchPath;              /* buffer in which to construct conch name */
33017
33018  /* Allocate space for the conch filename and initialize the name to
33019  ** the name of the original database file. */
33020  *pConchPath = conchPath = (char *)sqlite3_malloc64(len + 8);
33021  if( conchPath==0 ){
33022    return SQLITE_NOMEM;
33023  }
33024  memcpy(conchPath, dbPath, len+1);
33025
33026  /* now insert a "." before the last / character */
33027  for( i=(len-1); i>=0; i-- ){
33028    if( conchPath[i]=='/' ){
33029      i++;
33030      break;
33031    }
33032  }
33033  conchPath[i]='.';
33034  while ( i<len ){
33035    conchPath[i+1]=dbPath[i];
33036    i++;
33037  }
33038
33039  /* append the "-conch" suffix to the file */
33040  memcpy(&conchPath[i+1], "-conch", 7);
33041  assert( (int)strlen(conchPath) == len+7 );
33042
33043  return SQLITE_OK;
33044}
33045
33046
33047/* Takes a fully configured proxy locking-style unix file and switches
33048** the local lock file path
33049*/
33050static int switchLockProxyPath(unixFile *pFile, const char *path) {
33051  proxyLockingContext *pCtx = (proxyLockingContext*)pFile->lockingContext;
33052  char *oldPath = pCtx->lockProxyPath;
33053  int rc = SQLITE_OK;
33054
33055  if( pFile->eFileLock!=NO_LOCK ){
33056    return SQLITE_BUSY;
33057  }
33058
33059  /* nothing to do if the path is NULL, :auto: or matches the existing path */
33060  if( !path || path[0]=='\0' || !strcmp(path, ":auto:") ||
33061    (oldPath && !strncmp(oldPath, path, MAXPATHLEN)) ){
33062    return SQLITE_OK;
33063  }else{
33064    unixFile *lockProxy = pCtx->lockProxy;
33065    pCtx->lockProxy=NULL;
33066    pCtx->conchHeld = 0;
33067    if( lockProxy!=NULL ){
33068      rc=lockProxy->pMethod->xClose((sqlite3_file *)lockProxy);
33069      if( rc ) return rc;
33070      sqlite3_free(lockProxy);
33071    }
33072    sqlite3_free(oldPath);
33073    pCtx->lockProxyPath = sqlite3DbStrDup(0, path);
33074  }
33075
33076  return rc;
33077}
33078
33079/*
33080** pFile is a file that has been opened by a prior xOpen call.  dbPath
33081** is a string buffer at least MAXPATHLEN+1 characters in size.
33082**
33083** This routine find the filename associated with pFile and writes it
33084** int dbPath.
33085*/
33086static int proxyGetDbPathForUnixFile(unixFile *pFile, char *dbPath){
33087#if defined(__APPLE__)
33088  if( pFile->pMethod == &afpIoMethods ){
33089    /* afp style keeps a reference to the db path in the filePath field
33090    ** of the struct */
33091    assert( (int)strlen((char*)pFile->lockingContext)<=MAXPATHLEN );
33092    strlcpy(dbPath, ((afpLockingContext *)pFile->lockingContext)->dbPath,
33093            MAXPATHLEN);
33094  } else
33095#endif
33096  if( pFile->pMethod == &dotlockIoMethods ){
33097    /* dot lock style uses the locking context to store the dot lock
33098    ** file path */
33099    int len = strlen((char *)pFile->lockingContext) - strlen(DOTLOCK_SUFFIX);
33100    memcpy(dbPath, (char *)pFile->lockingContext, len + 1);
33101  }else{
33102    /* all other styles use the locking context to store the db file path */
33103    assert( strlen((char*)pFile->lockingContext)<=MAXPATHLEN );
33104    strlcpy(dbPath, (char *)pFile->lockingContext, MAXPATHLEN);
33105  }
33106  return SQLITE_OK;
33107}
33108
33109/*
33110** Takes an already filled in unix file and alters it so all file locking
33111** will be performed on the local proxy lock file.  The following fields
33112** are preserved in the locking context so that they can be restored and
33113** the unix structure properly cleaned up at close time:
33114**  ->lockingContext
33115**  ->pMethod
33116*/
33117static int proxyTransformUnixFile(unixFile *pFile, const char *path) {
33118  proxyLockingContext *pCtx;
33119  char dbPath[MAXPATHLEN+1];       /* Name of the database file */
33120  char *lockPath=NULL;
33121  int rc = SQLITE_OK;
33122
33123  if( pFile->eFileLock!=NO_LOCK ){
33124    return SQLITE_BUSY;
33125  }
33126  proxyGetDbPathForUnixFile(pFile, dbPath);
33127  if( !path || path[0]=='\0' || !strcmp(path, ":auto:") ){
33128    lockPath=NULL;
33129  }else{
33130    lockPath=(char *)path;
33131  }
33132
33133  OSTRACE(("TRANSPROXY  %d for %s pid=%d\n", pFile->h,
33134           (lockPath ? lockPath : ":auto:"), osGetpid(0)));
33135
33136  pCtx = sqlite3_malloc64( sizeof(*pCtx) );
33137  if( pCtx==0 ){
33138    return SQLITE_NOMEM;
33139  }
33140  memset(pCtx, 0, sizeof(*pCtx));
33141
33142  rc = proxyCreateConchPathname(dbPath, &pCtx->conchFilePath);
33143  if( rc==SQLITE_OK ){
33144    rc = proxyCreateUnixFile(pCtx->conchFilePath, &pCtx->conchFile, 0);
33145    if( rc==SQLITE_CANTOPEN && ((pFile->openFlags&O_RDWR) == 0) ){
33146      /* if (a) the open flags are not O_RDWR, (b) the conch isn't there, and
33147      ** (c) the file system is read-only, then enable no-locking access.
33148      ** Ugh, since O_RDONLY==0x0000 we test for !O_RDWR since unixOpen asserts
33149      ** that openFlags will have only one of O_RDONLY or O_RDWR.
33150      */
33151      struct statfs fsInfo;
33152      struct stat conchInfo;
33153      int goLockless = 0;
33154
33155      if( osStat(pCtx->conchFilePath, &conchInfo) == -1 ) {
33156        int err = errno;
33157        if( (err==ENOENT) && (statfs(dbPath, &fsInfo) != -1) ){
33158          goLockless = (fsInfo.f_flags&MNT_RDONLY) == MNT_RDONLY;
33159        }
33160      }
33161      if( goLockless ){
33162        pCtx->conchHeld = -1; /* read only FS/ lockless */
33163        rc = SQLITE_OK;
33164      }
33165    }
33166  }
33167  if( rc==SQLITE_OK && lockPath ){
33168    pCtx->lockProxyPath = sqlite3DbStrDup(0, lockPath);
33169  }
33170
33171  if( rc==SQLITE_OK ){
33172    pCtx->dbPath = sqlite3DbStrDup(0, dbPath);
33173    if( pCtx->dbPath==NULL ){
33174      rc = SQLITE_NOMEM;
33175    }
33176  }
33177  if( rc==SQLITE_OK ){
33178    /* all memory is allocated, proxys are created and assigned,
33179    ** switch the locking context and pMethod then return.
33180    */
33181    pCtx->oldLockingContext = pFile->lockingContext;
33182    pFile->lockingContext = pCtx;
33183    pCtx->pOldMethod = pFile->pMethod;
33184    pFile->pMethod = &proxyIoMethods;
33185  }else{
33186    if( pCtx->conchFile ){
33187      pCtx->conchFile->pMethod->xClose((sqlite3_file *)pCtx->conchFile);
33188      sqlite3_free(pCtx->conchFile);
33189    }
33190    sqlite3DbFree(0, pCtx->lockProxyPath);
33191    sqlite3_free(pCtx->conchFilePath);
33192    sqlite3_free(pCtx);
33193  }
33194  OSTRACE(("TRANSPROXY  %d %s\n", pFile->h,
33195           (rc==SQLITE_OK ? "ok" : "failed")));
33196  return rc;
33197}
33198
33199
33200/*
33201** This routine handles sqlite3_file_control() calls that are specific
33202** to proxy locking.
33203*/
33204static int proxyFileControl(sqlite3_file *id, int op, void *pArg){
33205  switch( op ){
33206    case SQLITE_FCNTL_GET_LOCKPROXYFILE: {
33207      unixFile *pFile = (unixFile*)id;
33208      if( pFile->pMethod == &proxyIoMethods ){
33209        proxyLockingContext *pCtx = (proxyLockingContext*)pFile->lockingContext;
33210        proxyTakeConch(pFile);
33211        if( pCtx->lockProxyPath ){
33212          *(const char **)pArg = pCtx->lockProxyPath;
33213        }else{
33214          *(const char **)pArg = ":auto: (not held)";
33215        }
33216      } else {
33217        *(const char **)pArg = NULL;
33218      }
33219      return SQLITE_OK;
33220    }
33221    case SQLITE_FCNTL_SET_LOCKPROXYFILE: {
33222      unixFile *pFile = (unixFile*)id;
33223      int rc = SQLITE_OK;
33224      int isProxyStyle = (pFile->pMethod == &proxyIoMethods);
33225      if( pArg==NULL || (const char *)pArg==0 ){
33226        if( isProxyStyle ){
33227          /* turn off proxy locking - not supported.  If support is added for
33228          ** switching proxy locking mode off then it will need to fail if
33229          ** the journal mode is WAL mode.
33230          */
33231          rc = SQLITE_ERROR /*SQLITE_PROTOCOL? SQLITE_MISUSE?*/;
33232        }else{
33233          /* turn off proxy locking - already off - NOOP */
33234          rc = SQLITE_OK;
33235        }
33236      }else{
33237        const char *proxyPath = (const char *)pArg;
33238        if( isProxyStyle ){
33239          proxyLockingContext *pCtx =
33240            (proxyLockingContext*)pFile->lockingContext;
33241          if( !strcmp(pArg, ":auto:")
33242           || (pCtx->lockProxyPath &&
33243               !strncmp(pCtx->lockProxyPath, proxyPath, MAXPATHLEN))
33244          ){
33245            rc = SQLITE_OK;
33246          }else{
33247            rc = switchLockProxyPath(pFile, proxyPath);
33248          }
33249        }else{
33250          /* turn on proxy file locking */
33251          rc = proxyTransformUnixFile(pFile, proxyPath);
33252        }
33253      }
33254      return rc;
33255    }
33256    default: {
33257      assert( 0 );  /* The call assures that only valid opcodes are sent */
33258    }
33259  }
33260  /*NOTREACHED*/
33261  return SQLITE_ERROR;
33262}
33263
33264/*
33265** Within this division (the proxying locking implementation) the procedures
33266** above this point are all utilities.  The lock-related methods of the
33267** proxy-locking sqlite3_io_method object follow.
33268*/
33269
33270
33271/*
33272** This routine checks if there is a RESERVED lock held on the specified
33273** file by this or any other process. If such a lock is held, set *pResOut
33274** to a non-zero value otherwise *pResOut is set to zero.  The return value
33275** is set to SQLITE_OK unless an I/O error occurs during lock checking.
33276*/
33277static int proxyCheckReservedLock(sqlite3_file *id, int *pResOut) {
33278  unixFile *pFile = (unixFile*)id;
33279  int rc = proxyTakeConch(pFile);
33280  if( rc==SQLITE_OK ){
33281    proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
33282    if( pCtx->conchHeld>0 ){
33283      unixFile *proxy = pCtx->lockProxy;
33284      return proxy->pMethod->xCheckReservedLock((sqlite3_file*)proxy, pResOut);
33285    }else{ /* conchHeld < 0 is lockless */
33286      pResOut=0;
33287    }
33288  }
33289  return rc;
33290}
33291
33292/*
33293** Lock the file with the lock specified by parameter eFileLock - one
33294** of the following:
33295**
33296**     (1) SHARED_LOCK
33297**     (2) RESERVED_LOCK
33298**     (3) PENDING_LOCK
33299**     (4) EXCLUSIVE_LOCK
33300**
33301** Sometimes when requesting one lock state, additional lock states
33302** are inserted in between.  The locking might fail on one of the later
33303** transitions leaving the lock state different from what it started but
33304** still short of its goal.  The following chart shows the allowed
33305** transitions and the inserted intermediate states:
33306**
33307**    UNLOCKED -> SHARED
33308**    SHARED -> RESERVED
33309**    SHARED -> (PENDING) -> EXCLUSIVE
33310**    RESERVED -> (PENDING) -> EXCLUSIVE
33311**    PENDING -> EXCLUSIVE
33312**
33313** This routine will only increase a lock.  Use the sqlite3OsUnlock()
33314** routine to lower a locking level.
33315*/
33316static int proxyLock(sqlite3_file *id, int eFileLock) {
33317  unixFile *pFile = (unixFile*)id;
33318  int rc = proxyTakeConch(pFile);
33319  if( rc==SQLITE_OK ){
33320    proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
33321    if( pCtx->conchHeld>0 ){
33322      unixFile *proxy = pCtx->lockProxy;
33323      rc = proxy->pMethod->xLock((sqlite3_file*)proxy, eFileLock);
33324      pFile->eFileLock = proxy->eFileLock;
33325    }else{
33326      /* conchHeld < 0 is lockless */
33327    }
33328  }
33329  return rc;
33330}
33331
33332
33333/*
33334** Lower the locking level on file descriptor pFile to eFileLock.  eFileLock
33335** must be either NO_LOCK or SHARED_LOCK.
33336**
33337** If the locking level of the file descriptor is already at or below
33338** the requested locking level, this routine is a no-op.
33339*/
33340static int proxyUnlock(sqlite3_file *id, int eFileLock) {
33341  unixFile *pFile = (unixFile*)id;
33342  int rc = proxyTakeConch(pFile);
33343  if( rc==SQLITE_OK ){
33344    proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
33345    if( pCtx->conchHeld>0 ){
33346      unixFile *proxy = pCtx->lockProxy;
33347      rc = proxy->pMethod->xUnlock((sqlite3_file*)proxy, eFileLock);
33348      pFile->eFileLock = proxy->eFileLock;
33349    }else{
33350      /* conchHeld < 0 is lockless */
33351    }
33352  }
33353  return rc;
33354}
33355
33356/*
33357** Close a file that uses proxy locks.
33358*/
33359static int proxyClose(sqlite3_file *id) {
33360  if( id ){
33361    unixFile *pFile = (unixFile*)id;
33362    proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
33363    unixFile *lockProxy = pCtx->lockProxy;
33364    unixFile *conchFile = pCtx->conchFile;
33365    int rc = SQLITE_OK;
33366
33367    if( lockProxy ){
33368      rc = lockProxy->pMethod->xUnlock((sqlite3_file*)lockProxy, NO_LOCK);
33369      if( rc ) return rc;
33370      rc = lockProxy->pMethod->xClose((sqlite3_file*)lockProxy);
33371      if( rc ) return rc;
33372      sqlite3_free(lockProxy);
33373      pCtx->lockProxy = 0;
33374    }
33375    if( conchFile ){
33376      if( pCtx->conchHeld ){
33377        rc = proxyReleaseConch(pFile);
33378        if( rc ) return rc;
33379      }
33380      rc = conchFile->pMethod->xClose((sqlite3_file*)conchFile);
33381      if( rc ) return rc;
33382      sqlite3_free(conchFile);
33383    }
33384    sqlite3DbFree(0, pCtx->lockProxyPath);
33385    sqlite3_free(pCtx->conchFilePath);
33386    sqlite3DbFree(0, pCtx->dbPath);
33387    /* restore the original locking context and pMethod then close it */
33388    pFile->lockingContext = pCtx->oldLockingContext;
33389    pFile->pMethod = pCtx->pOldMethod;
33390    sqlite3_free(pCtx);
33391    return pFile->pMethod->xClose(id);
33392  }
33393  return SQLITE_OK;
33394}
33395
33396
33397
33398#endif /* defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE */
33399/*
33400** The proxy locking style is intended for use with AFP filesystems.
33401** And since AFP is only supported on MacOSX, the proxy locking is also
33402** restricted to MacOSX.
33403**
33404**
33405******************* End of the proxy lock implementation **********************
33406******************************************************************************/
33407
33408/*
33409** Initialize the operating system interface.
33410**
33411** This routine registers all VFS implementations for unix-like operating
33412** systems.  This routine, and the sqlite3_os_end() routine that follows,
33413** should be the only routines in this file that are visible from other
33414** files.
33415**
33416** This routine is called once during SQLite initialization and by a
33417** single thread.  The memory allocation and mutex subsystems have not
33418** necessarily been initialized when this routine is called, and so they
33419** should not be used.
33420*/
33421SQLITE_API int SQLITE_STDCALL sqlite3_os_init(void){
33422  /*
33423  ** The following macro defines an initializer for an sqlite3_vfs object.
33424  ** The name of the VFS is NAME.  The pAppData is a pointer to a pointer
33425  ** to the "finder" function.  (pAppData is a pointer to a pointer because
33426  ** silly C90 rules prohibit a void* from being cast to a function pointer
33427  ** and so we have to go through the intermediate pointer to avoid problems
33428  ** when compiling with -pedantic-errors on GCC.)
33429  **
33430  ** The FINDER parameter to this macro is the name of the pointer to the
33431  ** finder-function.  The finder-function returns a pointer to the
33432  ** sqlite_io_methods object that implements the desired locking
33433  ** behaviors.  See the division above that contains the IOMETHODS
33434  ** macro for addition information on finder-functions.
33435  **
33436  ** Most finders simply return a pointer to a fixed sqlite3_io_methods
33437  ** object.  But the "autolockIoFinder" available on MacOSX does a little
33438  ** more than that; it looks at the filesystem type that hosts the
33439  ** database file and tries to choose an locking method appropriate for
33440  ** that filesystem time.
33441  */
33442  #define UNIXVFS(VFSNAME, FINDER) {                        \
33443    3,                    /* iVersion */                    \
33444    sizeof(unixFile),     /* szOsFile */                    \
33445    MAX_PATHNAME,         /* mxPathname */                  \
33446    0,                    /* pNext */                       \
33447    VFSNAME,              /* zName */                       \
33448    (void*)&FINDER,       /* pAppData */                    \
33449    unixOpen,             /* xOpen */                       \
33450    unixDelete,           /* xDelete */                     \
33451    unixAccess,           /* xAccess */                     \
33452    unixFullPathname,     /* xFullPathname */               \
33453    unixDlOpen,           /* xDlOpen */                     \
33454    unixDlError,          /* xDlError */                    \
33455    unixDlSym,            /* xDlSym */                      \
33456    unixDlClose,          /* xDlClose */                    \
33457    unixRandomness,       /* xRandomness */                 \
33458    unixSleep,            /* xSleep */                      \
33459    unixCurrentTime,      /* xCurrentTime */                \
33460    unixGetLastError,     /* xGetLastError */               \
33461    unixCurrentTimeInt64, /* xCurrentTimeInt64 */           \
33462    unixSetSystemCall,    /* xSetSystemCall */              \
33463    unixGetSystemCall,    /* xGetSystemCall */              \
33464    unixNextSystemCall,   /* xNextSystemCall */             \
33465  }
33466
33467  /*
33468  ** All default VFSes for unix are contained in the following array.
33469  **
33470  ** Note that the sqlite3_vfs.pNext field of the VFS object is modified
33471  ** by the SQLite core when the VFS is registered.  So the following
33472  ** array cannot be const.
33473  */
33474  static sqlite3_vfs aVfs[] = {
33475#if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__)
33476    UNIXVFS("unix",          autolockIoFinder ),
33477#elif OS_VXWORKS
33478    UNIXVFS("unix",          vxworksIoFinder ),
33479#else
33480    UNIXVFS("unix",          posixIoFinder ),
33481#endif
33482    UNIXVFS("unix-none",     nolockIoFinder ),
33483    UNIXVFS("unix-dotfile",  dotlockIoFinder ),
33484    UNIXVFS("unix-excl",     posixIoFinder ),
33485#if OS_VXWORKS
33486    UNIXVFS("unix-namedsem", semIoFinder ),
33487#endif
33488#if SQLITE_ENABLE_LOCKING_STYLE || OS_VXWORKS
33489    UNIXVFS("unix-posix",    posixIoFinder ),
33490#endif
33491#if SQLITE_ENABLE_LOCKING_STYLE
33492    UNIXVFS("unix-flock",    flockIoFinder ),
33493#endif
33494#if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__)
33495    UNIXVFS("unix-afp",      afpIoFinder ),
33496    UNIXVFS("unix-nfs",      nfsIoFinder ),
33497    UNIXVFS("unix-proxy",    proxyIoFinder ),
33498#endif
33499  };
33500  unsigned int i;          /* Loop counter */
33501
33502  /* Double-check that the aSyscall[] array has been constructed
33503  ** correctly.  See ticket [bb3a86e890c8e96ab] */
33504  assert( ArraySize(aSyscall)==25 );
33505
33506  /* Register all VFSes defined in the aVfs[] array */
33507  for(i=0; i<(sizeof(aVfs)/sizeof(sqlite3_vfs)); i++){
33508    sqlite3_vfs_register(&aVfs[i], i==0);
33509  }
33510  return SQLITE_OK;
33511}
33512
33513/*
33514** Shutdown the operating system interface.
33515**
33516** Some operating systems might need to do some cleanup in this routine,
33517** to release dynamically allocated objects.  But not on unix.
33518** This routine is a no-op for unix.
33519*/
33520SQLITE_API int SQLITE_STDCALL sqlite3_os_end(void){
33521  return SQLITE_OK;
33522}
33523
33524#endif /* SQLITE_OS_UNIX */
33525
33526/************** End of os_unix.c *********************************************/
33527/************** Begin file os_win.c ******************************************/
33528/*
33529** 2004 May 22
33530**
33531** The author disclaims copyright to this source code.  In place of
33532** a legal notice, here is a blessing:
33533**
33534**    May you do good and not evil.
33535**    May you find forgiveness for yourself and forgive others.
33536**    May you share freely, never taking more than you give.
33537**
33538******************************************************************************
33539**
33540** This file contains code that is specific to Windows.
33541*/
33542/* #include "sqliteInt.h" */
33543#if SQLITE_OS_WIN               /* This file is used for Windows only */
33544
33545/*
33546** Include code that is common to all os_*.c files
33547*/
33548/************** Include os_common.h in the middle of os_win.c ****************/
33549/************** Begin file os_common.h ***************************************/
33550/*
33551** 2004 May 22
33552**
33553** The author disclaims copyright to this source code.  In place of
33554** a legal notice, here is a blessing:
33555**
33556**    May you do good and not evil.
33557**    May you find forgiveness for yourself and forgive others.
33558**    May you share freely, never taking more than you give.
33559**
33560******************************************************************************
33561**
33562** This file contains macros and a little bit of code that is common to
33563** all of the platform-specific files (os_*.c) and is #included into those
33564** files.
33565**
33566** This file should be #included by the os_*.c files only.  It is not a
33567** general purpose header file.
33568*/
33569#ifndef _OS_COMMON_H_
33570#define _OS_COMMON_H_
33571
33572/*
33573** At least two bugs have slipped in because we changed the MEMORY_DEBUG
33574** macro to SQLITE_DEBUG and some older makefiles have not yet made the
33575** switch.  The following code should catch this problem at compile-time.
33576*/
33577#ifdef MEMORY_DEBUG
33578# error "The MEMORY_DEBUG macro is obsolete.  Use SQLITE_DEBUG instead."
33579#endif
33580
33581/*
33582** Macros for performance tracing.  Normally turned off.  Only works
33583** on i486 hardware.
33584*/
33585#ifdef SQLITE_PERFORMANCE_TRACE
33586
33587/*
33588** hwtime.h contains inline assembler code for implementing
33589** high-performance timing routines.
33590*/
33591/************** Include hwtime.h in the middle of os_common.h ****************/
33592/************** Begin file hwtime.h ******************************************/
33593/*
33594** 2008 May 27
33595**
33596** The author disclaims copyright to this source code.  In place of
33597** a legal notice, here is a blessing:
33598**
33599**    May you do good and not evil.
33600**    May you find forgiveness for yourself and forgive others.
33601**    May you share freely, never taking more than you give.
33602**
33603******************************************************************************
33604**
33605** This file contains inline asm code for retrieving "high-performance"
33606** counters for x86 class CPUs.
33607*/
33608#ifndef _HWTIME_H_
33609#define _HWTIME_H_
33610
33611/*
33612** The following routine only works on pentium-class (or newer) processors.
33613** It uses the RDTSC opcode to read the cycle count value out of the
33614** processor and returns that value.  This can be used for high-res
33615** profiling.
33616*/
33617#if (defined(__GNUC__) || defined(_MSC_VER)) && \
33618      (defined(i386) || defined(__i386__) || defined(_M_IX86))
33619
33620  #if defined(__GNUC__)
33621
33622  __inline__ sqlite_uint64 sqlite3Hwtime(void){
33623     unsigned int lo, hi;
33624     __asm__ __volatile__ ("rdtsc" : "=a" (lo), "=d" (hi));
33625     return (sqlite_uint64)hi << 32 | lo;
33626  }
33627
33628  #elif defined(_MSC_VER)
33629
33630  __declspec(naked) __inline sqlite_uint64 __cdecl sqlite3Hwtime(void){
33631     __asm {
33632        rdtsc
33633        ret       ; return value at EDX:EAX
33634     }
33635  }
33636
33637  #endif
33638
33639#elif (defined(__GNUC__) && defined(__x86_64__))
33640
33641  __inline__ sqlite_uint64 sqlite3Hwtime(void){
33642      unsigned long val;
33643      __asm__ __volatile__ ("rdtsc" : "=A" (val));
33644      return val;
33645  }
33646
33647#elif (defined(__GNUC__) && defined(__ppc__))
33648
33649  __inline__ sqlite_uint64 sqlite3Hwtime(void){
33650      unsigned long long retval;
33651      unsigned long junk;
33652      __asm__ __volatile__ ("\n\
33653          1:      mftbu   %1\n\
33654                  mftb    %L0\n\
33655                  mftbu   %0\n\
33656                  cmpw    %0,%1\n\
33657                  bne     1b"
33658                  : "=r" (retval), "=r" (junk));
33659      return retval;
33660  }
33661
33662#else
33663
33664  #error Need implementation of sqlite3Hwtime() for your platform.
33665
33666  /*
33667  ** To compile without implementing sqlite3Hwtime() for your platform,
33668  ** you can remove the above #error and use the following
33669  ** stub function.  You will lose timing support for many
33670  ** of the debugging and testing utilities, but it should at
33671  ** least compile and run.
33672  */
33673SQLITE_PRIVATE   sqlite_uint64 sqlite3Hwtime(void){ return ((sqlite_uint64)0); }
33674
33675#endif
33676
33677#endif /* !defined(_HWTIME_H_) */
33678
33679/************** End of hwtime.h **********************************************/
33680/************** Continuing where we left off in os_common.h ******************/
33681
33682static sqlite_uint64 g_start;
33683static sqlite_uint64 g_elapsed;
33684#define TIMER_START       g_start=sqlite3Hwtime()
33685#define TIMER_END         g_elapsed=sqlite3Hwtime()-g_start
33686#define TIMER_ELAPSED     g_elapsed
33687#else
33688#define TIMER_START
33689#define TIMER_END
33690#define TIMER_ELAPSED     ((sqlite_uint64)0)
33691#endif
33692
33693/*
33694** If we compile with the SQLITE_TEST macro set, then the following block
33695** of code will give us the ability to simulate a disk I/O error.  This
33696** is used for testing the I/O recovery logic.
33697*/
33698#ifdef SQLITE_TEST
33699SQLITE_API int sqlite3_io_error_hit = 0;            /* Total number of I/O Errors */
33700SQLITE_API int sqlite3_io_error_hardhit = 0;        /* Number of non-benign errors */
33701SQLITE_API int sqlite3_io_error_pending = 0;        /* Count down to first I/O error */
33702SQLITE_API int sqlite3_io_error_persist = 0;        /* True if I/O errors persist */
33703SQLITE_API int sqlite3_io_error_benign = 0;         /* True if errors are benign */
33704SQLITE_API int sqlite3_diskfull_pending = 0;
33705SQLITE_API int sqlite3_diskfull = 0;
33706#define SimulateIOErrorBenign(X) sqlite3_io_error_benign=(X)
33707#define SimulateIOError(CODE)  \
33708  if( (sqlite3_io_error_persist && sqlite3_io_error_hit) \
33709       || sqlite3_io_error_pending-- == 1 )  \
33710              { local_ioerr(); CODE; }
33711static void local_ioerr(){
33712  IOTRACE(("IOERR\n"));
33713  sqlite3_io_error_hit++;
33714  if( !sqlite3_io_error_benign ) sqlite3_io_error_hardhit++;
33715}
33716#define SimulateDiskfullError(CODE) \
33717   if( sqlite3_diskfull_pending ){ \
33718     if( sqlite3_diskfull_pending == 1 ){ \
33719       local_ioerr(); \
33720       sqlite3_diskfull = 1; \
33721       sqlite3_io_error_hit = 1; \
33722       CODE; \
33723     }else{ \
33724       sqlite3_diskfull_pending--; \
33725     } \
33726   }
33727#else
33728#define SimulateIOErrorBenign(X)
33729#define SimulateIOError(A)
33730#define SimulateDiskfullError(A)
33731#endif
33732
33733/*
33734** When testing, keep a count of the number of open files.
33735*/
33736#ifdef SQLITE_TEST
33737SQLITE_API int sqlite3_open_file_count = 0;
33738#define OpenCounter(X)  sqlite3_open_file_count+=(X)
33739#else
33740#define OpenCounter(X)
33741#endif
33742
33743#endif /* !defined(_OS_COMMON_H_) */
33744
33745/************** End of os_common.h *******************************************/
33746/************** Continuing where we left off in os_win.c *********************/
33747
33748/*
33749** Include the header file for the Windows VFS.
33750*/
33751/* #include "os_win.h" */
33752
33753/*
33754** Compiling and using WAL mode requires several APIs that are only
33755** available in Windows platforms based on the NT kernel.
33756*/
33757#if !SQLITE_OS_WINNT && !defined(SQLITE_OMIT_WAL)
33758#  error "WAL mode requires support from the Windows NT kernel, compile\
33759 with SQLITE_OMIT_WAL."
33760#endif
33761
33762#if !SQLITE_OS_WINNT && SQLITE_MAX_MMAP_SIZE>0
33763#  error "Memory mapped files require support from the Windows NT kernel,\
33764 compile with SQLITE_MAX_MMAP_SIZE=0."
33765#endif
33766
33767/*
33768** Are most of the Win32 ANSI APIs available (i.e. with certain exceptions
33769** based on the sub-platform)?
33770*/
33771#if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && !defined(SQLITE_WIN32_NO_ANSI)
33772#  define SQLITE_WIN32_HAS_ANSI
33773#endif
33774
33775/*
33776** Are most of the Win32 Unicode APIs available (i.e. with certain exceptions
33777** based on the sub-platform)?
33778*/
33779#if (SQLITE_OS_WINCE || SQLITE_OS_WINNT || SQLITE_OS_WINRT) && \
33780    !defined(SQLITE_WIN32_NO_WIDE)
33781#  define SQLITE_WIN32_HAS_WIDE
33782#endif
33783
33784/*
33785** Make sure at least one set of Win32 APIs is available.
33786*/
33787#if !defined(SQLITE_WIN32_HAS_ANSI) && !defined(SQLITE_WIN32_HAS_WIDE)
33788#  error "At least one of SQLITE_WIN32_HAS_ANSI and SQLITE_WIN32_HAS_WIDE\
33789 must be defined."
33790#endif
33791
33792/*
33793** Define the required Windows SDK version constants if they are not
33794** already available.
33795*/
33796#ifndef NTDDI_WIN8
33797#  define NTDDI_WIN8                        0x06020000
33798#endif
33799
33800#ifndef NTDDI_WINBLUE
33801#  define NTDDI_WINBLUE                     0x06030000
33802#endif
33803
33804/*
33805** Check to see if the GetVersionEx[AW] functions are deprecated on the
33806** target system.  GetVersionEx was first deprecated in Win8.1.
33807*/
33808#ifndef SQLITE_WIN32_GETVERSIONEX
33809#  if defined(NTDDI_VERSION) && NTDDI_VERSION >= NTDDI_WINBLUE
33810#    define SQLITE_WIN32_GETVERSIONEX   0   /* GetVersionEx() is deprecated */
33811#  else
33812#    define SQLITE_WIN32_GETVERSIONEX   1   /* GetVersionEx() is current */
33813#  endif
33814#endif
33815
33816/*
33817** This constant should already be defined (in the "WinDef.h" SDK file).
33818*/
33819#ifndef MAX_PATH
33820#  define MAX_PATH                      (260)
33821#endif
33822
33823/*
33824** Maximum pathname length (in chars) for Win32.  This should normally be
33825** MAX_PATH.
33826*/
33827#ifndef SQLITE_WIN32_MAX_PATH_CHARS
33828#  define SQLITE_WIN32_MAX_PATH_CHARS   (MAX_PATH)
33829#endif
33830
33831/*
33832** This constant should already be defined (in the "WinNT.h" SDK file).
33833*/
33834#ifndef UNICODE_STRING_MAX_CHARS
33835#  define UNICODE_STRING_MAX_CHARS      (32767)
33836#endif
33837
33838/*
33839** Maximum pathname length (in chars) for WinNT.  This should normally be
33840** UNICODE_STRING_MAX_CHARS.
33841*/
33842#ifndef SQLITE_WINNT_MAX_PATH_CHARS
33843#  define SQLITE_WINNT_MAX_PATH_CHARS   (UNICODE_STRING_MAX_CHARS)
33844#endif
33845
33846/*
33847** Maximum pathname length (in bytes) for Win32.  The MAX_PATH macro is in
33848** characters, so we allocate 4 bytes per character assuming worst-case of
33849** 4-bytes-per-character for UTF8.
33850*/
33851#ifndef SQLITE_WIN32_MAX_PATH_BYTES
33852#  define SQLITE_WIN32_MAX_PATH_BYTES   (SQLITE_WIN32_MAX_PATH_CHARS*4)
33853#endif
33854
33855/*
33856** Maximum pathname length (in bytes) for WinNT.  This should normally be
33857** UNICODE_STRING_MAX_CHARS * sizeof(WCHAR).
33858*/
33859#ifndef SQLITE_WINNT_MAX_PATH_BYTES
33860#  define SQLITE_WINNT_MAX_PATH_BYTES   \
33861                            (sizeof(WCHAR) * SQLITE_WINNT_MAX_PATH_CHARS)
33862#endif
33863
33864/*
33865** Maximum error message length (in chars) for WinRT.
33866*/
33867#ifndef SQLITE_WIN32_MAX_ERRMSG_CHARS
33868#  define SQLITE_WIN32_MAX_ERRMSG_CHARS (1024)
33869#endif
33870
33871/*
33872** Returns non-zero if the character should be treated as a directory
33873** separator.
33874*/
33875#ifndef winIsDirSep
33876#  define winIsDirSep(a)                (((a) == '/') || ((a) == '\\'))
33877#endif
33878
33879/*
33880** This macro is used when a local variable is set to a value that is
33881** [sometimes] not used by the code (e.g. via conditional compilation).
33882*/
33883#ifndef UNUSED_VARIABLE_VALUE
33884#  define UNUSED_VARIABLE_VALUE(x)      (void)(x)
33885#endif
33886
33887/*
33888** Returns the character that should be used as the directory separator.
33889*/
33890#ifndef winGetDirSep
33891#  define winGetDirSep()                '\\'
33892#endif
33893
33894/*
33895** Do we need to manually define the Win32 file mapping APIs for use with WAL
33896** mode or memory mapped files (e.g. these APIs are available in the Windows
33897** CE SDK; however, they are not present in the header file)?
33898*/
33899#if SQLITE_WIN32_FILEMAPPING_API && \
33900        (!defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0)
33901/*
33902** Two of the file mapping APIs are different under WinRT.  Figure out which
33903** set we need.
33904*/
33905#if SQLITE_OS_WINRT
33906WINBASEAPI HANDLE WINAPI CreateFileMappingFromApp(HANDLE, \
33907        LPSECURITY_ATTRIBUTES, ULONG, ULONG64, LPCWSTR);
33908
33909WINBASEAPI LPVOID WINAPI MapViewOfFileFromApp(HANDLE, ULONG, ULONG64, SIZE_T);
33910#else
33911#if defined(SQLITE_WIN32_HAS_ANSI)
33912WINBASEAPI HANDLE WINAPI CreateFileMappingA(HANDLE, LPSECURITY_ATTRIBUTES, \
33913        DWORD, DWORD, DWORD, LPCSTR);
33914#endif /* defined(SQLITE_WIN32_HAS_ANSI) */
33915
33916#if defined(SQLITE_WIN32_HAS_WIDE)
33917WINBASEAPI HANDLE WINAPI CreateFileMappingW(HANDLE, LPSECURITY_ATTRIBUTES, \
33918        DWORD, DWORD, DWORD, LPCWSTR);
33919#endif /* defined(SQLITE_WIN32_HAS_WIDE) */
33920
33921WINBASEAPI LPVOID WINAPI MapViewOfFile(HANDLE, DWORD, DWORD, DWORD, SIZE_T);
33922#endif /* SQLITE_OS_WINRT */
33923
33924/*
33925** These file mapping APIs are common to both Win32 and WinRT.
33926*/
33927
33928WINBASEAPI BOOL WINAPI FlushViewOfFile(LPCVOID, SIZE_T);
33929WINBASEAPI BOOL WINAPI UnmapViewOfFile(LPCVOID);
33930#endif /* SQLITE_WIN32_FILEMAPPING_API */
33931
33932/*
33933** Some Microsoft compilers lack this definition.
33934*/
33935#ifndef INVALID_FILE_ATTRIBUTES
33936# define INVALID_FILE_ATTRIBUTES ((DWORD)-1)
33937#endif
33938
33939#ifndef FILE_FLAG_MASK
33940# define FILE_FLAG_MASK          (0xFF3C0000)
33941#endif
33942
33943#ifndef FILE_ATTRIBUTE_MASK
33944# define FILE_ATTRIBUTE_MASK     (0x0003FFF7)
33945#endif
33946
33947#ifndef SQLITE_OMIT_WAL
33948/* Forward references to structures used for WAL */
33949typedef struct winShm winShm;           /* A connection to shared-memory */
33950typedef struct winShmNode winShmNode;   /* A region of shared-memory */
33951#endif
33952
33953/*
33954** WinCE lacks native support for file locking so we have to fake it
33955** with some code of our own.
33956*/
33957#if SQLITE_OS_WINCE
33958typedef struct winceLock {
33959  int nReaders;       /* Number of reader locks obtained */
33960  BOOL bPending;      /* Indicates a pending lock has been obtained */
33961  BOOL bReserved;     /* Indicates a reserved lock has been obtained */
33962  BOOL bExclusive;    /* Indicates an exclusive lock has been obtained */
33963} winceLock;
33964#endif
33965
33966/*
33967** The winFile structure is a subclass of sqlite3_file* specific to the win32
33968** portability layer.
33969*/
33970typedef struct winFile winFile;
33971struct winFile {
33972  const sqlite3_io_methods *pMethod; /*** Must be first ***/
33973  sqlite3_vfs *pVfs;      /* The VFS used to open this file */
33974  HANDLE h;               /* Handle for accessing the file */
33975  u8 locktype;            /* Type of lock currently held on this file */
33976  short sharedLockByte;   /* Randomly chosen byte used as a shared lock */
33977  u8 ctrlFlags;           /* Flags.  See WINFILE_* below */
33978  DWORD lastErrno;        /* The Windows errno from the last I/O error */
33979#ifndef SQLITE_OMIT_WAL
33980  winShm *pShm;           /* Instance of shared memory on this file */
33981#endif
33982  const char *zPath;      /* Full pathname of this file */
33983  int szChunk;            /* Chunk size configured by FCNTL_CHUNK_SIZE */
33984#if SQLITE_OS_WINCE
33985  LPWSTR zDeleteOnClose;  /* Name of file to delete when closing */
33986  HANDLE hMutex;          /* Mutex used to control access to shared lock */
33987  HANDLE hShared;         /* Shared memory segment used for locking */
33988  winceLock local;        /* Locks obtained by this instance of winFile */
33989  winceLock *shared;      /* Global shared lock memory for the file  */
33990#endif
33991#if SQLITE_MAX_MMAP_SIZE>0
33992  int nFetchOut;                /* Number of outstanding xFetch references */
33993  HANDLE hMap;                  /* Handle for accessing memory mapping */
33994  void *pMapRegion;             /* Area memory mapped */
33995  sqlite3_int64 mmapSize;       /* Usable size of mapped region */
33996  sqlite3_int64 mmapSizeActual; /* Actual size of mapped region */
33997  sqlite3_int64 mmapSizeMax;    /* Configured FCNTL_MMAP_SIZE value */
33998#endif
33999};
34000
34001/*
34002** Allowed values for winFile.ctrlFlags
34003*/
34004#define WINFILE_RDONLY          0x02   /* Connection is read only */
34005#define WINFILE_PERSIST_WAL     0x04   /* Persistent WAL mode */
34006#define WINFILE_PSOW            0x10   /* SQLITE_IOCAP_POWERSAFE_OVERWRITE */
34007
34008/*
34009 * The size of the buffer used by sqlite3_win32_write_debug().
34010 */
34011#ifndef SQLITE_WIN32_DBG_BUF_SIZE
34012#  define SQLITE_WIN32_DBG_BUF_SIZE   ((int)(4096-sizeof(DWORD)))
34013#endif
34014
34015/*
34016 * The value used with sqlite3_win32_set_directory() to specify that
34017 * the data directory should be changed.
34018 */
34019#ifndef SQLITE_WIN32_DATA_DIRECTORY_TYPE
34020#  define SQLITE_WIN32_DATA_DIRECTORY_TYPE (1)
34021#endif
34022
34023/*
34024 * The value used with sqlite3_win32_set_directory() to specify that
34025 * the temporary directory should be changed.
34026 */
34027#ifndef SQLITE_WIN32_TEMP_DIRECTORY_TYPE
34028#  define SQLITE_WIN32_TEMP_DIRECTORY_TYPE (2)
34029#endif
34030
34031/*
34032 * If compiled with SQLITE_WIN32_MALLOC on Windows, we will use the
34033 * various Win32 API heap functions instead of our own.
34034 */
34035#ifdef SQLITE_WIN32_MALLOC
34036
34037/*
34038 * If this is non-zero, an isolated heap will be created by the native Win32
34039 * allocator subsystem; otherwise, the default process heap will be used.  This
34040 * setting has no effect when compiling for WinRT.  By default, this is enabled
34041 * and an isolated heap will be created to store all allocated data.
34042 *
34043 ******************************************************************************
34044 * WARNING: It is important to note that when this setting is non-zero and the
34045 *          winMemShutdown function is called (e.g. by the sqlite3_shutdown
34046 *          function), all data that was allocated using the isolated heap will
34047 *          be freed immediately and any attempt to access any of that freed
34048 *          data will almost certainly result in an immediate access violation.
34049 ******************************************************************************
34050 */
34051#ifndef SQLITE_WIN32_HEAP_CREATE
34052#  define SQLITE_WIN32_HEAP_CREATE    (TRUE)
34053#endif
34054
34055/*
34056 * The initial size of the Win32-specific heap.  This value may be zero.
34057 */
34058#ifndef SQLITE_WIN32_HEAP_INIT_SIZE
34059#  define SQLITE_WIN32_HEAP_INIT_SIZE ((SQLITE_DEFAULT_CACHE_SIZE) * \
34060                                       (SQLITE_DEFAULT_PAGE_SIZE) + 4194304)
34061#endif
34062
34063/*
34064 * The maximum size of the Win32-specific heap.  This value may be zero.
34065 */
34066#ifndef SQLITE_WIN32_HEAP_MAX_SIZE
34067#  define SQLITE_WIN32_HEAP_MAX_SIZE  (0)
34068#endif
34069
34070/*
34071 * The extra flags to use in calls to the Win32 heap APIs.  This value may be
34072 * zero for the default behavior.
34073 */
34074#ifndef SQLITE_WIN32_HEAP_FLAGS
34075#  define SQLITE_WIN32_HEAP_FLAGS     (0)
34076#endif
34077
34078
34079/*
34080** The winMemData structure stores information required by the Win32-specific
34081** sqlite3_mem_methods implementation.
34082*/
34083typedef struct winMemData winMemData;
34084struct winMemData {
34085#ifndef NDEBUG
34086  u32 magic1;   /* Magic number to detect structure corruption. */
34087#endif
34088  HANDLE hHeap; /* The handle to our heap. */
34089  BOOL bOwned;  /* Do we own the heap (i.e. destroy it on shutdown)? */
34090#ifndef NDEBUG
34091  u32 magic2;   /* Magic number to detect structure corruption. */
34092#endif
34093};
34094
34095#ifndef NDEBUG
34096#define WINMEM_MAGIC1     0x42b2830b
34097#define WINMEM_MAGIC2     0xbd4d7cf4
34098#endif
34099
34100static struct winMemData win_mem_data = {
34101#ifndef NDEBUG
34102  WINMEM_MAGIC1,
34103#endif
34104  NULL, FALSE
34105#ifndef NDEBUG
34106  ,WINMEM_MAGIC2
34107#endif
34108};
34109
34110#ifndef NDEBUG
34111#define winMemAssertMagic1() assert( win_mem_data.magic1==WINMEM_MAGIC1 )
34112#define winMemAssertMagic2() assert( win_mem_data.magic2==WINMEM_MAGIC2 )
34113#define winMemAssertMagic()  winMemAssertMagic1(); winMemAssertMagic2();
34114#else
34115#define winMemAssertMagic()
34116#endif
34117
34118#define winMemGetDataPtr()  &win_mem_data
34119#define winMemGetHeap()     win_mem_data.hHeap
34120#define winMemGetOwned()    win_mem_data.bOwned
34121
34122static void *winMemMalloc(int nBytes);
34123static void winMemFree(void *pPrior);
34124static void *winMemRealloc(void *pPrior, int nBytes);
34125static int winMemSize(void *p);
34126static int winMemRoundup(int n);
34127static int winMemInit(void *pAppData);
34128static void winMemShutdown(void *pAppData);
34129
34130SQLITE_PRIVATE const sqlite3_mem_methods *sqlite3MemGetWin32(void);
34131#endif /* SQLITE_WIN32_MALLOC */
34132
34133/*
34134** The following variable is (normally) set once and never changes
34135** thereafter.  It records whether the operating system is Win9x
34136** or WinNT.
34137**
34138** 0:   Operating system unknown.
34139** 1:   Operating system is Win9x.
34140** 2:   Operating system is WinNT.
34141**
34142** In order to facilitate testing on a WinNT system, the test fixture
34143** can manually set this value to 1 to emulate Win98 behavior.
34144*/
34145#ifdef SQLITE_TEST
34146SQLITE_API LONG SQLITE_WIN32_VOLATILE sqlite3_os_type = 0;
34147#else
34148static LONG SQLITE_WIN32_VOLATILE sqlite3_os_type = 0;
34149#endif
34150
34151#ifndef SYSCALL
34152#  define SYSCALL sqlite3_syscall_ptr
34153#endif
34154
34155/*
34156** This function is not available on Windows CE or WinRT.
34157 */
34158
34159#if SQLITE_OS_WINCE || SQLITE_OS_WINRT
34160#  define osAreFileApisANSI()       1
34161#endif
34162
34163/*
34164** Many system calls are accessed through pointer-to-functions so that
34165** they may be overridden at runtime to facilitate fault injection during
34166** testing and sandboxing.  The following array holds the names and pointers
34167** to all overrideable system calls.
34168*/
34169static struct win_syscall {
34170  const char *zName;            /* Name of the system call */
34171  sqlite3_syscall_ptr pCurrent; /* Current value of the system call */
34172  sqlite3_syscall_ptr pDefault; /* Default value */
34173} aSyscall[] = {
34174#if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT
34175  { "AreFileApisANSI",         (SYSCALL)AreFileApisANSI,         0 },
34176#else
34177  { "AreFileApisANSI",         (SYSCALL)0,                       0 },
34178#endif
34179
34180#ifndef osAreFileApisANSI
34181#define osAreFileApisANSI ((BOOL(WINAPI*)(VOID))aSyscall[0].pCurrent)
34182#endif
34183
34184#if SQLITE_OS_WINCE && defined(SQLITE_WIN32_HAS_WIDE)
34185  { "CharLowerW",              (SYSCALL)CharLowerW,              0 },
34186#else
34187  { "CharLowerW",              (SYSCALL)0,                       0 },
34188#endif
34189
34190#define osCharLowerW ((LPWSTR(WINAPI*)(LPWSTR))aSyscall[1].pCurrent)
34191
34192#if SQLITE_OS_WINCE && defined(SQLITE_WIN32_HAS_WIDE)
34193  { "CharUpperW",              (SYSCALL)CharUpperW,              0 },
34194#else
34195  { "CharUpperW",              (SYSCALL)0,                       0 },
34196#endif
34197
34198#define osCharUpperW ((LPWSTR(WINAPI*)(LPWSTR))aSyscall[2].pCurrent)
34199
34200  { "CloseHandle",             (SYSCALL)CloseHandle,             0 },
34201
34202#define osCloseHandle ((BOOL(WINAPI*)(HANDLE))aSyscall[3].pCurrent)
34203
34204#if defined(SQLITE_WIN32_HAS_ANSI)
34205  { "CreateFileA",             (SYSCALL)CreateFileA,             0 },
34206#else
34207  { "CreateFileA",             (SYSCALL)0,                       0 },
34208#endif
34209
34210#define osCreateFileA ((HANDLE(WINAPI*)(LPCSTR,DWORD,DWORD, \
34211        LPSECURITY_ATTRIBUTES,DWORD,DWORD,HANDLE))aSyscall[4].pCurrent)
34212
34213#if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_WIDE)
34214  { "CreateFileW",             (SYSCALL)CreateFileW,             0 },
34215#else
34216  { "CreateFileW",             (SYSCALL)0,                       0 },
34217#endif
34218
34219#define osCreateFileW ((HANDLE(WINAPI*)(LPCWSTR,DWORD,DWORD, \
34220        LPSECURITY_ATTRIBUTES,DWORD,DWORD,HANDLE))aSyscall[5].pCurrent)
34221
34222#if (!SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_ANSI) && \
34223        (!defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0))
34224  { "CreateFileMappingA",      (SYSCALL)CreateFileMappingA,      0 },
34225#else
34226  { "CreateFileMappingA",      (SYSCALL)0,                       0 },
34227#endif
34228
34229#define osCreateFileMappingA ((HANDLE(WINAPI*)(HANDLE,LPSECURITY_ATTRIBUTES, \
34230        DWORD,DWORD,DWORD,LPCSTR))aSyscall[6].pCurrent)
34231
34232#if SQLITE_OS_WINCE || (!SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_WIDE) && \
34233        (!defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0))
34234  { "CreateFileMappingW",      (SYSCALL)CreateFileMappingW,      0 },
34235#else
34236  { "CreateFileMappingW",      (SYSCALL)0,                       0 },
34237#endif
34238
34239#define osCreateFileMappingW ((HANDLE(WINAPI*)(HANDLE,LPSECURITY_ATTRIBUTES, \
34240        DWORD,DWORD,DWORD,LPCWSTR))aSyscall[7].pCurrent)
34241
34242#if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_WIDE)
34243  { "CreateMutexW",            (SYSCALL)CreateMutexW,            0 },
34244#else
34245  { "CreateMutexW",            (SYSCALL)0,                       0 },
34246#endif
34247
34248#define osCreateMutexW ((HANDLE(WINAPI*)(LPSECURITY_ATTRIBUTES,BOOL, \
34249        LPCWSTR))aSyscall[8].pCurrent)
34250
34251#if defined(SQLITE_WIN32_HAS_ANSI)
34252  { "DeleteFileA",             (SYSCALL)DeleteFileA,             0 },
34253#else
34254  { "DeleteFileA",             (SYSCALL)0,                       0 },
34255#endif
34256
34257#define osDeleteFileA ((BOOL(WINAPI*)(LPCSTR))aSyscall[9].pCurrent)
34258
34259#if defined(SQLITE_WIN32_HAS_WIDE)
34260  { "DeleteFileW",             (SYSCALL)DeleteFileW,             0 },
34261#else
34262  { "DeleteFileW",             (SYSCALL)0,                       0 },
34263#endif
34264
34265#define osDeleteFileW ((BOOL(WINAPI*)(LPCWSTR))aSyscall[10].pCurrent)
34266
34267#if SQLITE_OS_WINCE
34268  { "FileTimeToLocalFileTime", (SYSCALL)FileTimeToLocalFileTime, 0 },
34269#else
34270  { "FileTimeToLocalFileTime", (SYSCALL)0,                       0 },
34271#endif
34272
34273#define osFileTimeToLocalFileTime ((BOOL(WINAPI*)(CONST FILETIME*, \
34274        LPFILETIME))aSyscall[11].pCurrent)
34275
34276#if SQLITE_OS_WINCE
34277  { "FileTimeToSystemTime",    (SYSCALL)FileTimeToSystemTime,    0 },
34278#else
34279  { "FileTimeToSystemTime",    (SYSCALL)0,                       0 },
34280#endif
34281
34282#define osFileTimeToSystemTime ((BOOL(WINAPI*)(CONST FILETIME*, \
34283        LPSYSTEMTIME))aSyscall[12].pCurrent)
34284
34285  { "FlushFileBuffers",        (SYSCALL)FlushFileBuffers,        0 },
34286
34287#define osFlushFileBuffers ((BOOL(WINAPI*)(HANDLE))aSyscall[13].pCurrent)
34288
34289#if defined(SQLITE_WIN32_HAS_ANSI)
34290  { "FormatMessageA",          (SYSCALL)FormatMessageA,          0 },
34291#else
34292  { "FormatMessageA",          (SYSCALL)0,                       0 },
34293#endif
34294
34295#define osFormatMessageA ((DWORD(WINAPI*)(DWORD,LPCVOID,DWORD,DWORD,LPSTR, \
34296        DWORD,va_list*))aSyscall[14].pCurrent)
34297
34298#if defined(SQLITE_WIN32_HAS_WIDE)
34299  { "FormatMessageW",          (SYSCALL)FormatMessageW,          0 },
34300#else
34301  { "FormatMessageW",          (SYSCALL)0,                       0 },
34302#endif
34303
34304#define osFormatMessageW ((DWORD(WINAPI*)(DWORD,LPCVOID,DWORD,DWORD,LPWSTR, \
34305        DWORD,va_list*))aSyscall[15].pCurrent)
34306
34307#if !defined(SQLITE_OMIT_LOAD_EXTENSION)
34308  { "FreeLibrary",             (SYSCALL)FreeLibrary,             0 },
34309#else
34310  { "FreeLibrary",             (SYSCALL)0,                       0 },
34311#endif
34312
34313#define osFreeLibrary ((BOOL(WINAPI*)(HMODULE))aSyscall[16].pCurrent)
34314
34315  { "GetCurrentProcessId",     (SYSCALL)GetCurrentProcessId,     0 },
34316
34317#define osGetCurrentProcessId ((DWORD(WINAPI*)(VOID))aSyscall[17].pCurrent)
34318
34319#if !SQLITE_OS_WINCE && defined(SQLITE_WIN32_HAS_ANSI)
34320  { "GetDiskFreeSpaceA",       (SYSCALL)GetDiskFreeSpaceA,       0 },
34321#else
34322  { "GetDiskFreeSpaceA",       (SYSCALL)0,                       0 },
34323#endif
34324
34325#define osGetDiskFreeSpaceA ((BOOL(WINAPI*)(LPCSTR,LPDWORD,LPDWORD,LPDWORD, \
34326        LPDWORD))aSyscall[18].pCurrent)
34327
34328#if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_WIDE)
34329  { "GetDiskFreeSpaceW",       (SYSCALL)GetDiskFreeSpaceW,       0 },
34330#else
34331  { "GetDiskFreeSpaceW",       (SYSCALL)0,                       0 },
34332#endif
34333
34334#define osGetDiskFreeSpaceW ((BOOL(WINAPI*)(LPCWSTR,LPDWORD,LPDWORD,LPDWORD, \
34335        LPDWORD))aSyscall[19].pCurrent)
34336
34337#if defined(SQLITE_WIN32_HAS_ANSI)
34338  { "GetFileAttributesA",      (SYSCALL)GetFileAttributesA,      0 },
34339#else
34340  { "GetFileAttributesA",      (SYSCALL)0,                       0 },
34341#endif
34342
34343#define osGetFileAttributesA ((DWORD(WINAPI*)(LPCSTR))aSyscall[20].pCurrent)
34344
34345#if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_WIDE)
34346  { "GetFileAttributesW",      (SYSCALL)GetFileAttributesW,      0 },
34347#else
34348  { "GetFileAttributesW",      (SYSCALL)0,                       0 },
34349#endif
34350
34351#define osGetFileAttributesW ((DWORD(WINAPI*)(LPCWSTR))aSyscall[21].pCurrent)
34352
34353#if defined(SQLITE_WIN32_HAS_WIDE)
34354  { "GetFileAttributesExW",    (SYSCALL)GetFileAttributesExW,    0 },
34355#else
34356  { "GetFileAttributesExW",    (SYSCALL)0,                       0 },
34357#endif
34358
34359#define osGetFileAttributesExW ((BOOL(WINAPI*)(LPCWSTR,GET_FILEEX_INFO_LEVELS, \
34360        LPVOID))aSyscall[22].pCurrent)
34361
34362#if !SQLITE_OS_WINRT
34363  { "GetFileSize",             (SYSCALL)GetFileSize,             0 },
34364#else
34365  { "GetFileSize",             (SYSCALL)0,                       0 },
34366#endif
34367
34368#define osGetFileSize ((DWORD(WINAPI*)(HANDLE,LPDWORD))aSyscall[23].pCurrent)
34369
34370#if !SQLITE_OS_WINCE && defined(SQLITE_WIN32_HAS_ANSI)
34371  { "GetFullPathNameA",        (SYSCALL)GetFullPathNameA,        0 },
34372#else
34373  { "GetFullPathNameA",        (SYSCALL)0,                       0 },
34374#endif
34375
34376#define osGetFullPathNameA ((DWORD(WINAPI*)(LPCSTR,DWORD,LPSTR, \
34377        LPSTR*))aSyscall[24].pCurrent)
34378
34379#if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_WIDE)
34380  { "GetFullPathNameW",        (SYSCALL)GetFullPathNameW,        0 },
34381#else
34382  { "GetFullPathNameW",        (SYSCALL)0,                       0 },
34383#endif
34384
34385#define osGetFullPathNameW ((DWORD(WINAPI*)(LPCWSTR,DWORD,LPWSTR, \
34386        LPWSTR*))aSyscall[25].pCurrent)
34387
34388  { "GetLastError",            (SYSCALL)GetLastError,            0 },
34389
34390#define osGetLastError ((DWORD(WINAPI*)(VOID))aSyscall[26].pCurrent)
34391
34392#if !defined(SQLITE_OMIT_LOAD_EXTENSION)
34393#if SQLITE_OS_WINCE
34394  /* The GetProcAddressA() routine is only available on Windows CE. */
34395  { "GetProcAddressA",         (SYSCALL)GetProcAddressA,         0 },
34396#else
34397  /* All other Windows platforms expect GetProcAddress() to take
34398  ** an ANSI string regardless of the _UNICODE setting */
34399  { "GetProcAddressA",         (SYSCALL)GetProcAddress,          0 },
34400#endif
34401#else
34402  { "GetProcAddressA",         (SYSCALL)0,                       0 },
34403#endif
34404
34405#define osGetProcAddressA ((FARPROC(WINAPI*)(HMODULE, \
34406        LPCSTR))aSyscall[27].pCurrent)
34407
34408#if !SQLITE_OS_WINRT
34409  { "GetSystemInfo",           (SYSCALL)GetSystemInfo,           0 },
34410#else
34411  { "GetSystemInfo",           (SYSCALL)0,                       0 },
34412#endif
34413
34414#define osGetSystemInfo ((VOID(WINAPI*)(LPSYSTEM_INFO))aSyscall[28].pCurrent)
34415
34416  { "GetSystemTime",           (SYSCALL)GetSystemTime,           0 },
34417
34418#define osGetSystemTime ((VOID(WINAPI*)(LPSYSTEMTIME))aSyscall[29].pCurrent)
34419
34420#if !SQLITE_OS_WINCE
34421  { "GetSystemTimeAsFileTime", (SYSCALL)GetSystemTimeAsFileTime, 0 },
34422#else
34423  { "GetSystemTimeAsFileTime", (SYSCALL)0,                       0 },
34424#endif
34425
34426#define osGetSystemTimeAsFileTime ((VOID(WINAPI*)( \
34427        LPFILETIME))aSyscall[30].pCurrent)
34428
34429#if defined(SQLITE_WIN32_HAS_ANSI)
34430  { "GetTempPathA",            (SYSCALL)GetTempPathA,            0 },
34431#else
34432  { "GetTempPathA",            (SYSCALL)0,                       0 },
34433#endif
34434
34435#define osGetTempPathA ((DWORD(WINAPI*)(DWORD,LPSTR))aSyscall[31].pCurrent)
34436
34437#if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_WIDE)
34438  { "GetTempPathW",            (SYSCALL)GetTempPathW,            0 },
34439#else
34440  { "GetTempPathW",            (SYSCALL)0,                       0 },
34441#endif
34442
34443#define osGetTempPathW ((DWORD(WINAPI*)(DWORD,LPWSTR))aSyscall[32].pCurrent)
34444
34445#if !SQLITE_OS_WINRT
34446  { "GetTickCount",            (SYSCALL)GetTickCount,            0 },
34447#else
34448  { "GetTickCount",            (SYSCALL)0,                       0 },
34449#endif
34450
34451#define osGetTickCount ((DWORD(WINAPI*)(VOID))aSyscall[33].pCurrent)
34452
34453#if defined(SQLITE_WIN32_HAS_ANSI) && defined(SQLITE_WIN32_GETVERSIONEX) && \
34454        SQLITE_WIN32_GETVERSIONEX
34455  { "GetVersionExA",           (SYSCALL)GetVersionExA,           0 },
34456#else
34457  { "GetVersionExA",           (SYSCALL)0,                       0 },
34458#endif
34459
34460#define osGetVersionExA ((BOOL(WINAPI*)( \
34461        LPOSVERSIONINFOA))aSyscall[34].pCurrent)
34462
34463#if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_WIDE) && \
34464        defined(SQLITE_WIN32_GETVERSIONEX) && SQLITE_WIN32_GETVERSIONEX
34465  { "GetVersionExW",           (SYSCALL)GetVersionExW,           0 },
34466#else
34467  { "GetVersionExW",           (SYSCALL)0,                       0 },
34468#endif
34469
34470#define osGetVersionExW ((BOOL(WINAPI*)( \
34471        LPOSVERSIONINFOW))aSyscall[35].pCurrent)
34472
34473  { "HeapAlloc",               (SYSCALL)HeapAlloc,               0 },
34474
34475#define osHeapAlloc ((LPVOID(WINAPI*)(HANDLE,DWORD, \
34476        SIZE_T))aSyscall[36].pCurrent)
34477
34478#if !SQLITE_OS_WINRT
34479  { "HeapCreate",              (SYSCALL)HeapCreate,              0 },
34480#else
34481  { "HeapCreate",              (SYSCALL)0,                       0 },
34482#endif
34483
34484#define osHeapCreate ((HANDLE(WINAPI*)(DWORD,SIZE_T, \
34485        SIZE_T))aSyscall[37].pCurrent)
34486
34487#if !SQLITE_OS_WINRT
34488  { "HeapDestroy",             (SYSCALL)HeapDestroy,             0 },
34489#else
34490  { "HeapDestroy",             (SYSCALL)0,                       0 },
34491#endif
34492
34493#define osHeapDestroy ((BOOL(WINAPI*)(HANDLE))aSyscall[38].pCurrent)
34494
34495  { "HeapFree",                (SYSCALL)HeapFree,                0 },
34496
34497#define osHeapFree ((BOOL(WINAPI*)(HANDLE,DWORD,LPVOID))aSyscall[39].pCurrent)
34498
34499  { "HeapReAlloc",             (SYSCALL)HeapReAlloc,             0 },
34500
34501#define osHeapReAlloc ((LPVOID(WINAPI*)(HANDLE,DWORD,LPVOID, \
34502        SIZE_T))aSyscall[40].pCurrent)
34503
34504  { "HeapSize",                (SYSCALL)HeapSize,                0 },
34505
34506#define osHeapSize ((SIZE_T(WINAPI*)(HANDLE,DWORD, \
34507        LPCVOID))aSyscall[41].pCurrent)
34508
34509#if !SQLITE_OS_WINRT
34510  { "HeapValidate",            (SYSCALL)HeapValidate,            0 },
34511#else
34512  { "HeapValidate",            (SYSCALL)0,                       0 },
34513#endif
34514
34515#define osHeapValidate ((BOOL(WINAPI*)(HANDLE,DWORD, \
34516        LPCVOID))aSyscall[42].pCurrent)
34517
34518#if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT
34519  { "HeapCompact",             (SYSCALL)HeapCompact,             0 },
34520#else
34521  { "HeapCompact",             (SYSCALL)0,                       0 },
34522#endif
34523
34524#define osHeapCompact ((UINT(WINAPI*)(HANDLE,DWORD))aSyscall[43].pCurrent)
34525
34526#if defined(SQLITE_WIN32_HAS_ANSI) && !defined(SQLITE_OMIT_LOAD_EXTENSION)
34527  { "LoadLibraryA",            (SYSCALL)LoadLibraryA,            0 },
34528#else
34529  { "LoadLibraryA",            (SYSCALL)0,                       0 },
34530#endif
34531
34532#define osLoadLibraryA ((HMODULE(WINAPI*)(LPCSTR))aSyscall[44].pCurrent)
34533
34534#if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_WIDE) && \
34535        !defined(SQLITE_OMIT_LOAD_EXTENSION)
34536  { "LoadLibraryW",            (SYSCALL)LoadLibraryW,            0 },
34537#else
34538  { "LoadLibraryW",            (SYSCALL)0,                       0 },
34539#endif
34540
34541#define osLoadLibraryW ((HMODULE(WINAPI*)(LPCWSTR))aSyscall[45].pCurrent)
34542
34543#if !SQLITE_OS_WINRT
34544  { "LocalFree",               (SYSCALL)LocalFree,               0 },
34545#else
34546  { "LocalFree",               (SYSCALL)0,                       0 },
34547#endif
34548
34549#define osLocalFree ((HLOCAL(WINAPI*)(HLOCAL))aSyscall[46].pCurrent)
34550
34551#if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT
34552  { "LockFile",                (SYSCALL)LockFile,                0 },
34553#else
34554  { "LockFile",                (SYSCALL)0,                       0 },
34555#endif
34556
34557#ifndef osLockFile
34558#define osLockFile ((BOOL(WINAPI*)(HANDLE,DWORD,DWORD,DWORD, \
34559        DWORD))aSyscall[47].pCurrent)
34560#endif
34561
34562#if !SQLITE_OS_WINCE
34563  { "LockFileEx",              (SYSCALL)LockFileEx,              0 },
34564#else
34565  { "LockFileEx",              (SYSCALL)0,                       0 },
34566#endif
34567
34568#ifndef osLockFileEx
34569#define osLockFileEx ((BOOL(WINAPI*)(HANDLE,DWORD,DWORD,DWORD,DWORD, \
34570        LPOVERLAPPED))aSyscall[48].pCurrent)
34571#endif
34572
34573#if SQLITE_OS_WINCE || (!SQLITE_OS_WINRT && \
34574        (!defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0))
34575  { "MapViewOfFile",           (SYSCALL)MapViewOfFile,           0 },
34576#else
34577  { "MapViewOfFile",           (SYSCALL)0,                       0 },
34578#endif
34579
34580#define osMapViewOfFile ((LPVOID(WINAPI*)(HANDLE,DWORD,DWORD,DWORD, \
34581        SIZE_T))aSyscall[49].pCurrent)
34582
34583  { "MultiByteToWideChar",     (SYSCALL)MultiByteToWideChar,     0 },
34584
34585#define osMultiByteToWideChar ((int(WINAPI*)(UINT,DWORD,LPCSTR,int,LPWSTR, \
34586        int))aSyscall[50].pCurrent)
34587
34588  { "QueryPerformanceCounter", (SYSCALL)QueryPerformanceCounter, 0 },
34589
34590#define osQueryPerformanceCounter ((BOOL(WINAPI*)( \
34591        LARGE_INTEGER*))aSyscall[51].pCurrent)
34592
34593  { "ReadFile",                (SYSCALL)ReadFile,                0 },
34594
34595#define osReadFile ((BOOL(WINAPI*)(HANDLE,LPVOID,DWORD,LPDWORD, \
34596        LPOVERLAPPED))aSyscall[52].pCurrent)
34597
34598  { "SetEndOfFile",            (SYSCALL)SetEndOfFile,            0 },
34599
34600#define osSetEndOfFile ((BOOL(WINAPI*)(HANDLE))aSyscall[53].pCurrent)
34601
34602#if !SQLITE_OS_WINRT
34603  { "SetFilePointer",          (SYSCALL)SetFilePointer,          0 },
34604#else
34605  { "SetFilePointer",          (SYSCALL)0,                       0 },
34606#endif
34607
34608#define osSetFilePointer ((DWORD(WINAPI*)(HANDLE,LONG,PLONG, \
34609        DWORD))aSyscall[54].pCurrent)
34610
34611#if !SQLITE_OS_WINRT
34612  { "Sleep",                   (SYSCALL)Sleep,                   0 },
34613#else
34614  { "Sleep",                   (SYSCALL)0,                       0 },
34615#endif
34616
34617#define osSleep ((VOID(WINAPI*)(DWORD))aSyscall[55].pCurrent)
34618
34619  { "SystemTimeToFileTime",    (SYSCALL)SystemTimeToFileTime,    0 },
34620
34621#define osSystemTimeToFileTime ((BOOL(WINAPI*)(CONST SYSTEMTIME*, \
34622        LPFILETIME))aSyscall[56].pCurrent)
34623
34624#if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT
34625  { "UnlockFile",              (SYSCALL)UnlockFile,              0 },
34626#else
34627  { "UnlockFile",              (SYSCALL)0,                       0 },
34628#endif
34629
34630#ifndef osUnlockFile
34631#define osUnlockFile ((BOOL(WINAPI*)(HANDLE,DWORD,DWORD,DWORD, \
34632        DWORD))aSyscall[57].pCurrent)
34633#endif
34634
34635#if !SQLITE_OS_WINCE
34636  { "UnlockFileEx",            (SYSCALL)UnlockFileEx,            0 },
34637#else
34638  { "UnlockFileEx",            (SYSCALL)0,                       0 },
34639#endif
34640
34641#define osUnlockFileEx ((BOOL(WINAPI*)(HANDLE,DWORD,DWORD,DWORD, \
34642        LPOVERLAPPED))aSyscall[58].pCurrent)
34643
34644#if SQLITE_OS_WINCE || !defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0
34645  { "UnmapViewOfFile",         (SYSCALL)UnmapViewOfFile,         0 },
34646#else
34647  { "UnmapViewOfFile",         (SYSCALL)0,                       0 },
34648#endif
34649
34650#define osUnmapViewOfFile ((BOOL(WINAPI*)(LPCVOID))aSyscall[59].pCurrent)
34651
34652  { "WideCharToMultiByte",     (SYSCALL)WideCharToMultiByte,     0 },
34653
34654#define osWideCharToMultiByte ((int(WINAPI*)(UINT,DWORD,LPCWSTR,int,LPSTR,int, \
34655        LPCSTR,LPBOOL))aSyscall[60].pCurrent)
34656
34657  { "WriteFile",               (SYSCALL)WriteFile,               0 },
34658
34659#define osWriteFile ((BOOL(WINAPI*)(HANDLE,LPCVOID,DWORD,LPDWORD, \
34660        LPOVERLAPPED))aSyscall[61].pCurrent)
34661
34662#if SQLITE_OS_WINRT
34663  { "CreateEventExW",          (SYSCALL)CreateEventExW,          0 },
34664#else
34665  { "CreateEventExW",          (SYSCALL)0,                       0 },
34666#endif
34667
34668#define osCreateEventExW ((HANDLE(WINAPI*)(LPSECURITY_ATTRIBUTES,LPCWSTR, \
34669        DWORD,DWORD))aSyscall[62].pCurrent)
34670
34671#if !SQLITE_OS_WINRT
34672  { "WaitForSingleObject",     (SYSCALL)WaitForSingleObject,     0 },
34673#else
34674  { "WaitForSingleObject",     (SYSCALL)0,                       0 },
34675#endif
34676
34677#define osWaitForSingleObject ((DWORD(WINAPI*)(HANDLE, \
34678        DWORD))aSyscall[63].pCurrent)
34679
34680#if !SQLITE_OS_WINCE
34681  { "WaitForSingleObjectEx",   (SYSCALL)WaitForSingleObjectEx,   0 },
34682#else
34683  { "WaitForSingleObjectEx",   (SYSCALL)0,                       0 },
34684#endif
34685
34686#define osWaitForSingleObjectEx ((DWORD(WINAPI*)(HANDLE,DWORD, \
34687        BOOL))aSyscall[64].pCurrent)
34688
34689#if SQLITE_OS_WINRT
34690  { "SetFilePointerEx",        (SYSCALL)SetFilePointerEx,        0 },
34691#else
34692  { "SetFilePointerEx",        (SYSCALL)0,                       0 },
34693#endif
34694
34695#define osSetFilePointerEx ((BOOL(WINAPI*)(HANDLE,LARGE_INTEGER, \
34696        PLARGE_INTEGER,DWORD))aSyscall[65].pCurrent)
34697
34698#if SQLITE_OS_WINRT
34699  { "GetFileInformationByHandleEx", (SYSCALL)GetFileInformationByHandleEx, 0 },
34700#else
34701  { "GetFileInformationByHandleEx", (SYSCALL)0,                  0 },
34702#endif
34703
34704#define osGetFileInformationByHandleEx ((BOOL(WINAPI*)(HANDLE, \
34705        FILE_INFO_BY_HANDLE_CLASS,LPVOID,DWORD))aSyscall[66].pCurrent)
34706
34707#if SQLITE_OS_WINRT && (!defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0)
34708  { "MapViewOfFileFromApp",    (SYSCALL)MapViewOfFileFromApp,    0 },
34709#else
34710  { "MapViewOfFileFromApp",    (SYSCALL)0,                       0 },
34711#endif
34712
34713#define osMapViewOfFileFromApp ((LPVOID(WINAPI*)(HANDLE,ULONG,ULONG64, \
34714        SIZE_T))aSyscall[67].pCurrent)
34715
34716#if SQLITE_OS_WINRT
34717  { "CreateFile2",             (SYSCALL)CreateFile2,             0 },
34718#else
34719  { "CreateFile2",             (SYSCALL)0,                       0 },
34720#endif
34721
34722#define osCreateFile2 ((HANDLE(WINAPI*)(LPCWSTR,DWORD,DWORD,DWORD, \
34723        LPCREATEFILE2_EXTENDED_PARAMETERS))aSyscall[68].pCurrent)
34724
34725#if SQLITE_OS_WINRT && !defined(SQLITE_OMIT_LOAD_EXTENSION)
34726  { "LoadPackagedLibrary",     (SYSCALL)LoadPackagedLibrary,     0 },
34727#else
34728  { "LoadPackagedLibrary",     (SYSCALL)0,                       0 },
34729#endif
34730
34731#define osLoadPackagedLibrary ((HMODULE(WINAPI*)(LPCWSTR, \
34732        DWORD))aSyscall[69].pCurrent)
34733
34734#if SQLITE_OS_WINRT
34735  { "GetTickCount64",          (SYSCALL)GetTickCount64,          0 },
34736#else
34737  { "GetTickCount64",          (SYSCALL)0,                       0 },
34738#endif
34739
34740#define osGetTickCount64 ((ULONGLONG(WINAPI*)(VOID))aSyscall[70].pCurrent)
34741
34742#if SQLITE_OS_WINRT
34743  { "GetNativeSystemInfo",     (SYSCALL)GetNativeSystemInfo,     0 },
34744#else
34745  { "GetNativeSystemInfo",     (SYSCALL)0,                       0 },
34746#endif
34747
34748#define osGetNativeSystemInfo ((VOID(WINAPI*)( \
34749        LPSYSTEM_INFO))aSyscall[71].pCurrent)
34750
34751#if defined(SQLITE_WIN32_HAS_ANSI)
34752  { "OutputDebugStringA",      (SYSCALL)OutputDebugStringA,      0 },
34753#else
34754  { "OutputDebugStringA",      (SYSCALL)0,                       0 },
34755#endif
34756
34757#define osOutputDebugStringA ((VOID(WINAPI*)(LPCSTR))aSyscall[72].pCurrent)
34758
34759#if defined(SQLITE_WIN32_HAS_WIDE)
34760  { "OutputDebugStringW",      (SYSCALL)OutputDebugStringW,      0 },
34761#else
34762  { "OutputDebugStringW",      (SYSCALL)0,                       0 },
34763#endif
34764
34765#define osOutputDebugStringW ((VOID(WINAPI*)(LPCWSTR))aSyscall[73].pCurrent)
34766
34767  { "GetProcessHeap",          (SYSCALL)GetProcessHeap,          0 },
34768
34769#define osGetProcessHeap ((HANDLE(WINAPI*)(VOID))aSyscall[74].pCurrent)
34770
34771#if SQLITE_OS_WINRT && (!defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0)
34772  { "CreateFileMappingFromApp", (SYSCALL)CreateFileMappingFromApp, 0 },
34773#else
34774  { "CreateFileMappingFromApp", (SYSCALL)0,                      0 },
34775#endif
34776
34777#define osCreateFileMappingFromApp ((HANDLE(WINAPI*)(HANDLE, \
34778        LPSECURITY_ATTRIBUTES,ULONG,ULONG64,LPCWSTR))aSyscall[75].pCurrent)
34779
34780/*
34781** NOTE: On some sub-platforms, the InterlockedCompareExchange "function"
34782**       is really just a macro that uses a compiler intrinsic (e.g. x64).
34783**       So do not try to make this is into a redefinable interface.
34784*/
34785#if defined(InterlockedCompareExchange)
34786  { "InterlockedCompareExchange", (SYSCALL)0,                    0 },
34787
34788#define osInterlockedCompareExchange InterlockedCompareExchange
34789#else
34790  { "InterlockedCompareExchange", (SYSCALL)InterlockedCompareExchange, 0 },
34791
34792#define osInterlockedCompareExchange ((LONG(WINAPI*)(LONG \
34793        SQLITE_WIN32_VOLATILE*, LONG,LONG))aSyscall[76].pCurrent)
34794#endif /* defined(InterlockedCompareExchange) */
34795
34796#if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && SQLITE_WIN32_USE_UUID
34797  { "UuidCreate",               (SYSCALL)UuidCreate,             0 },
34798#else
34799  { "UuidCreate",               (SYSCALL)0,                      0 },
34800#endif
34801
34802#define osUuidCreate ((RPC_STATUS(RPC_ENTRY*)(UUID*))aSyscall[77].pCurrent)
34803
34804#if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && SQLITE_WIN32_USE_UUID
34805  { "UuidCreateSequential",     (SYSCALL)UuidCreateSequential,   0 },
34806#else
34807  { "UuidCreateSequential",     (SYSCALL)0,                      0 },
34808#endif
34809
34810#define osUuidCreateSequential \
34811        ((RPC_STATUS(RPC_ENTRY*)(UUID*))aSyscall[78].pCurrent)
34812
34813#if !defined(SQLITE_NO_SYNC) && SQLITE_MAX_MMAP_SIZE>0
34814  { "FlushViewOfFile",          (SYSCALL)FlushViewOfFile,        0 },
34815#else
34816  { "FlushViewOfFile",          (SYSCALL)0,                      0 },
34817#endif
34818
34819#define osFlushViewOfFile \
34820        ((BOOL(WINAPI*)(LPCVOID,SIZE_T))aSyscall[79].pCurrent)
34821
34822}; /* End of the overrideable system calls */
34823
34824/*
34825** This is the xSetSystemCall() method of sqlite3_vfs for all of the
34826** "win32" VFSes.  Return SQLITE_OK opon successfully updating the
34827** system call pointer, or SQLITE_NOTFOUND if there is no configurable
34828** system call named zName.
34829*/
34830static int winSetSystemCall(
34831  sqlite3_vfs *pNotUsed,        /* The VFS pointer.  Not used */
34832  const char *zName,            /* Name of system call to override */
34833  sqlite3_syscall_ptr pNewFunc  /* Pointer to new system call value */
34834){
34835  unsigned int i;
34836  int rc = SQLITE_NOTFOUND;
34837
34838  UNUSED_PARAMETER(pNotUsed);
34839  if( zName==0 ){
34840    /* If no zName is given, restore all system calls to their default
34841    ** settings and return NULL
34842    */
34843    rc = SQLITE_OK;
34844    for(i=0; i<sizeof(aSyscall)/sizeof(aSyscall[0]); i++){
34845      if( aSyscall[i].pDefault ){
34846        aSyscall[i].pCurrent = aSyscall[i].pDefault;
34847      }
34848    }
34849  }else{
34850    /* If zName is specified, operate on only the one system call
34851    ** specified.
34852    */
34853    for(i=0; i<sizeof(aSyscall)/sizeof(aSyscall[0]); i++){
34854      if( strcmp(zName, aSyscall[i].zName)==0 ){
34855        if( aSyscall[i].pDefault==0 ){
34856          aSyscall[i].pDefault = aSyscall[i].pCurrent;
34857        }
34858        rc = SQLITE_OK;
34859        if( pNewFunc==0 ) pNewFunc = aSyscall[i].pDefault;
34860        aSyscall[i].pCurrent = pNewFunc;
34861        break;
34862      }
34863    }
34864  }
34865  return rc;
34866}
34867
34868/*
34869** Return the value of a system call.  Return NULL if zName is not a
34870** recognized system call name.  NULL is also returned if the system call
34871** is currently undefined.
34872*/
34873static sqlite3_syscall_ptr winGetSystemCall(
34874  sqlite3_vfs *pNotUsed,
34875  const char *zName
34876){
34877  unsigned int i;
34878
34879  UNUSED_PARAMETER(pNotUsed);
34880  for(i=0; i<sizeof(aSyscall)/sizeof(aSyscall[0]); i++){
34881    if( strcmp(zName, aSyscall[i].zName)==0 ) return aSyscall[i].pCurrent;
34882  }
34883  return 0;
34884}
34885
34886/*
34887** Return the name of the first system call after zName.  If zName==NULL
34888** then return the name of the first system call.  Return NULL if zName
34889** is the last system call or if zName is not the name of a valid
34890** system call.
34891*/
34892static const char *winNextSystemCall(sqlite3_vfs *p, const char *zName){
34893  int i = -1;
34894
34895  UNUSED_PARAMETER(p);
34896  if( zName ){
34897    for(i=0; i<ArraySize(aSyscall)-1; i++){
34898      if( strcmp(zName, aSyscall[i].zName)==0 ) break;
34899    }
34900  }
34901  for(i++; i<ArraySize(aSyscall); i++){
34902    if( aSyscall[i].pCurrent!=0 ) return aSyscall[i].zName;
34903  }
34904  return 0;
34905}
34906
34907#ifdef SQLITE_WIN32_MALLOC
34908/*
34909** If a Win32 native heap has been configured, this function will attempt to
34910** compact it.  Upon success, SQLITE_OK will be returned.  Upon failure, one
34911** of SQLITE_NOMEM, SQLITE_ERROR, or SQLITE_NOTFOUND will be returned.  The
34912** "pnLargest" argument, if non-zero, will be used to return the size of the
34913** largest committed free block in the heap, in bytes.
34914*/
34915SQLITE_API int SQLITE_STDCALL sqlite3_win32_compact_heap(LPUINT pnLargest){
34916  int rc = SQLITE_OK;
34917  UINT nLargest = 0;
34918  HANDLE hHeap;
34919
34920  winMemAssertMagic();
34921  hHeap = winMemGetHeap();
34922  assert( hHeap!=0 );
34923  assert( hHeap!=INVALID_HANDLE_VALUE );
34924#if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_MALLOC_VALIDATE)
34925  assert( osHeapValidate(hHeap, SQLITE_WIN32_HEAP_FLAGS, NULL) );
34926#endif
34927#if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT
34928  if( (nLargest=osHeapCompact(hHeap, SQLITE_WIN32_HEAP_FLAGS))==0 ){
34929    DWORD lastErrno = osGetLastError();
34930    if( lastErrno==NO_ERROR ){
34931      sqlite3_log(SQLITE_NOMEM, "failed to HeapCompact (no space), heap=%p",
34932                  (void*)hHeap);
34933      rc = SQLITE_NOMEM;
34934    }else{
34935      sqlite3_log(SQLITE_ERROR, "failed to HeapCompact (%lu), heap=%p",
34936                  osGetLastError(), (void*)hHeap);
34937      rc = SQLITE_ERROR;
34938    }
34939  }
34940#else
34941  sqlite3_log(SQLITE_NOTFOUND, "failed to HeapCompact, heap=%p",
34942              (void*)hHeap);
34943  rc = SQLITE_NOTFOUND;
34944#endif
34945  if( pnLargest ) *pnLargest = nLargest;
34946  return rc;
34947}
34948
34949/*
34950** If a Win32 native heap has been configured, this function will attempt to
34951** destroy and recreate it.  If the Win32 native heap is not isolated and/or
34952** the sqlite3_memory_used() function does not return zero, SQLITE_BUSY will
34953** be returned and no changes will be made to the Win32 native heap.
34954*/
34955SQLITE_API int SQLITE_STDCALL sqlite3_win32_reset_heap(){
34956  int rc;
34957  MUTEX_LOGIC( sqlite3_mutex *pMaster; ) /* The main static mutex */
34958  MUTEX_LOGIC( sqlite3_mutex *pMem; )    /* The memsys static mutex */
34959  MUTEX_LOGIC( pMaster = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MASTER); )
34960  MUTEX_LOGIC( pMem = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MEM); )
34961  sqlite3_mutex_enter(pMaster);
34962  sqlite3_mutex_enter(pMem);
34963  winMemAssertMagic();
34964  if( winMemGetHeap()!=NULL && winMemGetOwned() && sqlite3_memory_used()==0 ){
34965    /*
34966    ** At this point, there should be no outstanding memory allocations on
34967    ** the heap.  Also, since both the master and memsys locks are currently
34968    ** being held by us, no other function (i.e. from another thread) should
34969    ** be able to even access the heap.  Attempt to destroy and recreate our
34970    ** isolated Win32 native heap now.
34971    */
34972    assert( winMemGetHeap()!=NULL );
34973    assert( winMemGetOwned() );
34974    assert( sqlite3_memory_used()==0 );
34975    winMemShutdown(winMemGetDataPtr());
34976    assert( winMemGetHeap()==NULL );
34977    assert( !winMemGetOwned() );
34978    assert( sqlite3_memory_used()==0 );
34979    rc = winMemInit(winMemGetDataPtr());
34980    assert( rc!=SQLITE_OK || winMemGetHeap()!=NULL );
34981    assert( rc!=SQLITE_OK || winMemGetOwned() );
34982    assert( rc!=SQLITE_OK || sqlite3_memory_used()==0 );
34983  }else{
34984    /*
34985    ** The Win32 native heap cannot be modified because it may be in use.
34986    */
34987    rc = SQLITE_BUSY;
34988  }
34989  sqlite3_mutex_leave(pMem);
34990  sqlite3_mutex_leave(pMaster);
34991  return rc;
34992}
34993#endif /* SQLITE_WIN32_MALLOC */
34994
34995/*
34996** This function outputs the specified (ANSI) string to the Win32 debugger
34997** (if available).
34998*/
34999
35000SQLITE_API void SQLITE_STDCALL sqlite3_win32_write_debug(const char *zBuf, int nBuf){
35001  char zDbgBuf[SQLITE_WIN32_DBG_BUF_SIZE];
35002  int nMin = MIN(nBuf, (SQLITE_WIN32_DBG_BUF_SIZE - 1)); /* may be negative. */
35003  if( nMin<-1 ) nMin = -1; /* all negative values become -1. */
35004  assert( nMin==-1 || nMin==0 || nMin<SQLITE_WIN32_DBG_BUF_SIZE );
35005#if defined(SQLITE_WIN32_HAS_ANSI)
35006  if( nMin>0 ){
35007    memset(zDbgBuf, 0, SQLITE_WIN32_DBG_BUF_SIZE);
35008    memcpy(zDbgBuf, zBuf, nMin);
35009    osOutputDebugStringA(zDbgBuf);
35010  }else{
35011    osOutputDebugStringA(zBuf);
35012  }
35013#elif defined(SQLITE_WIN32_HAS_WIDE)
35014  memset(zDbgBuf, 0, SQLITE_WIN32_DBG_BUF_SIZE);
35015  if ( osMultiByteToWideChar(
35016          osAreFileApisANSI() ? CP_ACP : CP_OEMCP, 0, zBuf,
35017          nMin, (LPWSTR)zDbgBuf, SQLITE_WIN32_DBG_BUF_SIZE/sizeof(WCHAR))<=0 ){
35018    return;
35019  }
35020  osOutputDebugStringW((LPCWSTR)zDbgBuf);
35021#else
35022  if( nMin>0 ){
35023    memset(zDbgBuf, 0, SQLITE_WIN32_DBG_BUF_SIZE);
35024    memcpy(zDbgBuf, zBuf, nMin);
35025    fprintf(stderr, "%s", zDbgBuf);
35026  }else{
35027    fprintf(stderr, "%s", zBuf);
35028  }
35029#endif
35030}
35031
35032/*
35033** The following routine suspends the current thread for at least ms
35034** milliseconds.  This is equivalent to the Win32 Sleep() interface.
35035*/
35036#if SQLITE_OS_WINRT
35037static HANDLE sleepObj = NULL;
35038#endif
35039
35040SQLITE_API void SQLITE_STDCALL sqlite3_win32_sleep(DWORD milliseconds){
35041#if SQLITE_OS_WINRT
35042  if ( sleepObj==NULL ){
35043    sleepObj = osCreateEventExW(NULL, NULL, CREATE_EVENT_MANUAL_RESET,
35044                                SYNCHRONIZE);
35045  }
35046  assert( sleepObj!=NULL );
35047  osWaitForSingleObjectEx(sleepObj, milliseconds, FALSE);
35048#else
35049  osSleep(milliseconds);
35050#endif
35051}
35052
35053#if SQLITE_MAX_WORKER_THREADS>0 && !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && \
35054        SQLITE_THREADSAFE>0
35055SQLITE_PRIVATE DWORD sqlite3Win32Wait(HANDLE hObject){
35056  DWORD rc;
35057  while( (rc = osWaitForSingleObjectEx(hObject, INFINITE,
35058                                       TRUE))==WAIT_IO_COMPLETION ){}
35059  return rc;
35060}
35061#endif
35062
35063/*
35064** Return true (non-zero) if we are running under WinNT, Win2K, WinXP,
35065** or WinCE.  Return false (zero) for Win95, Win98, or WinME.
35066**
35067** Here is an interesting observation:  Win95, Win98, and WinME lack
35068** the LockFileEx() API.  But we can still statically link against that
35069** API as long as we don't call it when running Win95/98/ME.  A call to
35070** this routine is used to determine if the host is Win95/98/ME or
35071** WinNT/2K/XP so that we will know whether or not we can safely call
35072** the LockFileEx() API.
35073*/
35074
35075#if !defined(SQLITE_WIN32_GETVERSIONEX) || !SQLITE_WIN32_GETVERSIONEX
35076# define osIsNT()  (1)
35077#elif SQLITE_OS_WINCE || SQLITE_OS_WINRT || !defined(SQLITE_WIN32_HAS_ANSI)
35078# define osIsNT()  (1)
35079#elif !defined(SQLITE_WIN32_HAS_WIDE)
35080# define osIsNT()  (0)
35081#else
35082# define osIsNT()  ((sqlite3_os_type==2) || sqlite3_win32_is_nt())
35083#endif
35084
35085/*
35086** This function determines if the machine is running a version of Windows
35087** based on the NT kernel.
35088*/
35089SQLITE_API int SQLITE_STDCALL sqlite3_win32_is_nt(void){
35090#if SQLITE_OS_WINRT
35091  /*
35092  ** NOTE: The WinRT sub-platform is always assumed to be based on the NT
35093  **       kernel.
35094  */
35095  return 1;
35096#elif defined(SQLITE_WIN32_GETVERSIONEX) && SQLITE_WIN32_GETVERSIONEX
35097  if( osInterlockedCompareExchange(&sqlite3_os_type, 0, 0)==0 ){
35098#if defined(SQLITE_WIN32_HAS_ANSI)
35099    OSVERSIONINFOA sInfo;
35100    sInfo.dwOSVersionInfoSize = sizeof(sInfo);
35101    osGetVersionExA(&sInfo);
35102    osInterlockedCompareExchange(&sqlite3_os_type,
35103        (sInfo.dwPlatformId == VER_PLATFORM_WIN32_NT) ? 2 : 1, 0);
35104#elif defined(SQLITE_WIN32_HAS_WIDE)
35105    OSVERSIONINFOW sInfo;
35106    sInfo.dwOSVersionInfoSize = sizeof(sInfo);
35107    osGetVersionExW(&sInfo);
35108    osInterlockedCompareExchange(&sqlite3_os_type,
35109        (sInfo.dwPlatformId == VER_PLATFORM_WIN32_NT) ? 2 : 1, 0);
35110#endif
35111  }
35112  return osInterlockedCompareExchange(&sqlite3_os_type, 2, 2)==2;
35113#elif SQLITE_TEST
35114  return osInterlockedCompareExchange(&sqlite3_os_type, 2, 2)==2;
35115#else
35116  /*
35117  ** NOTE: All sub-platforms where the GetVersionEx[AW] functions are
35118  **       deprecated are always assumed to be based on the NT kernel.
35119  */
35120  return 1;
35121#endif
35122}
35123
35124#ifdef SQLITE_WIN32_MALLOC
35125/*
35126** Allocate nBytes of memory.
35127*/
35128static void *winMemMalloc(int nBytes){
35129  HANDLE hHeap;
35130  void *p;
35131
35132  winMemAssertMagic();
35133  hHeap = winMemGetHeap();
35134  assert( hHeap!=0 );
35135  assert( hHeap!=INVALID_HANDLE_VALUE );
35136#if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_MALLOC_VALIDATE)
35137  assert( osHeapValidate(hHeap, SQLITE_WIN32_HEAP_FLAGS, NULL) );
35138#endif
35139  assert( nBytes>=0 );
35140  p = osHeapAlloc(hHeap, SQLITE_WIN32_HEAP_FLAGS, (SIZE_T)nBytes);
35141  if( !p ){
35142    sqlite3_log(SQLITE_NOMEM, "failed to HeapAlloc %u bytes (%lu), heap=%p",
35143                nBytes, osGetLastError(), (void*)hHeap);
35144  }
35145  return p;
35146}
35147
35148/*
35149** Free memory.
35150*/
35151static void winMemFree(void *pPrior){
35152  HANDLE hHeap;
35153
35154  winMemAssertMagic();
35155  hHeap = winMemGetHeap();
35156  assert( hHeap!=0 );
35157  assert( hHeap!=INVALID_HANDLE_VALUE );
35158#if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_MALLOC_VALIDATE)
35159  assert( osHeapValidate(hHeap, SQLITE_WIN32_HEAP_FLAGS, pPrior) );
35160#endif
35161  if( !pPrior ) return; /* Passing NULL to HeapFree is undefined. */
35162  if( !osHeapFree(hHeap, SQLITE_WIN32_HEAP_FLAGS, pPrior) ){
35163    sqlite3_log(SQLITE_NOMEM, "failed to HeapFree block %p (%lu), heap=%p",
35164                pPrior, osGetLastError(), (void*)hHeap);
35165  }
35166}
35167
35168/*
35169** Change the size of an existing memory allocation
35170*/
35171static void *winMemRealloc(void *pPrior, int nBytes){
35172  HANDLE hHeap;
35173  void *p;
35174
35175  winMemAssertMagic();
35176  hHeap = winMemGetHeap();
35177  assert( hHeap!=0 );
35178  assert( hHeap!=INVALID_HANDLE_VALUE );
35179#if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_MALLOC_VALIDATE)
35180  assert( osHeapValidate(hHeap, SQLITE_WIN32_HEAP_FLAGS, pPrior) );
35181#endif
35182  assert( nBytes>=0 );
35183  if( !pPrior ){
35184    p = osHeapAlloc(hHeap, SQLITE_WIN32_HEAP_FLAGS, (SIZE_T)nBytes);
35185  }else{
35186    p = osHeapReAlloc(hHeap, SQLITE_WIN32_HEAP_FLAGS, pPrior, (SIZE_T)nBytes);
35187  }
35188  if( !p ){
35189    sqlite3_log(SQLITE_NOMEM, "failed to %s %u bytes (%lu), heap=%p",
35190                pPrior ? "HeapReAlloc" : "HeapAlloc", nBytes, osGetLastError(),
35191                (void*)hHeap);
35192  }
35193  return p;
35194}
35195
35196/*
35197** Return the size of an outstanding allocation, in bytes.
35198*/
35199static int winMemSize(void *p){
35200  HANDLE hHeap;
35201  SIZE_T n;
35202
35203  winMemAssertMagic();
35204  hHeap = winMemGetHeap();
35205  assert( hHeap!=0 );
35206  assert( hHeap!=INVALID_HANDLE_VALUE );
35207#if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_MALLOC_VALIDATE)
35208  assert( osHeapValidate(hHeap, SQLITE_WIN32_HEAP_FLAGS, p) );
35209#endif
35210  if( !p ) return 0;
35211  n = osHeapSize(hHeap, SQLITE_WIN32_HEAP_FLAGS, p);
35212  if( n==(SIZE_T)-1 ){
35213    sqlite3_log(SQLITE_NOMEM, "failed to HeapSize block %p (%lu), heap=%p",
35214                p, osGetLastError(), (void*)hHeap);
35215    return 0;
35216  }
35217  return (int)n;
35218}
35219
35220/*
35221** Round up a request size to the next valid allocation size.
35222*/
35223static int winMemRoundup(int n){
35224  return n;
35225}
35226
35227/*
35228** Initialize this module.
35229*/
35230static int winMemInit(void *pAppData){
35231  winMemData *pWinMemData = (winMemData *)pAppData;
35232
35233  if( !pWinMemData ) return SQLITE_ERROR;
35234  assert( pWinMemData->magic1==WINMEM_MAGIC1 );
35235  assert( pWinMemData->magic2==WINMEM_MAGIC2 );
35236
35237#if !SQLITE_OS_WINRT && SQLITE_WIN32_HEAP_CREATE
35238  if( !pWinMemData->hHeap ){
35239    DWORD dwInitialSize = SQLITE_WIN32_HEAP_INIT_SIZE;
35240    DWORD dwMaximumSize = (DWORD)sqlite3GlobalConfig.nHeap;
35241    if( dwMaximumSize==0 ){
35242      dwMaximumSize = SQLITE_WIN32_HEAP_MAX_SIZE;
35243    }else if( dwInitialSize>dwMaximumSize ){
35244      dwInitialSize = dwMaximumSize;
35245    }
35246    pWinMemData->hHeap = osHeapCreate(SQLITE_WIN32_HEAP_FLAGS,
35247                                      dwInitialSize, dwMaximumSize);
35248    if( !pWinMemData->hHeap ){
35249      sqlite3_log(SQLITE_NOMEM,
35250          "failed to HeapCreate (%lu), flags=%u, initSize=%lu, maxSize=%lu",
35251          osGetLastError(), SQLITE_WIN32_HEAP_FLAGS, dwInitialSize,
35252          dwMaximumSize);
35253      return SQLITE_NOMEM;
35254    }
35255    pWinMemData->bOwned = TRUE;
35256    assert( pWinMemData->bOwned );
35257  }
35258#else
35259  pWinMemData->hHeap = osGetProcessHeap();
35260  if( !pWinMemData->hHeap ){
35261    sqlite3_log(SQLITE_NOMEM,
35262        "failed to GetProcessHeap (%lu)", osGetLastError());
35263    return SQLITE_NOMEM;
35264  }
35265  pWinMemData->bOwned = FALSE;
35266  assert( !pWinMemData->bOwned );
35267#endif
35268  assert( pWinMemData->hHeap!=0 );
35269  assert( pWinMemData->hHeap!=INVALID_HANDLE_VALUE );
35270#if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_MALLOC_VALIDATE)
35271  assert( osHeapValidate(pWinMemData->hHeap, SQLITE_WIN32_HEAP_FLAGS, NULL) );
35272#endif
35273  return SQLITE_OK;
35274}
35275
35276/*
35277** Deinitialize this module.
35278*/
35279static void winMemShutdown(void *pAppData){
35280  winMemData *pWinMemData = (winMemData *)pAppData;
35281
35282  if( !pWinMemData ) return;
35283  assert( pWinMemData->magic1==WINMEM_MAGIC1 );
35284  assert( pWinMemData->magic2==WINMEM_MAGIC2 );
35285
35286  if( pWinMemData->hHeap ){
35287    assert( pWinMemData->hHeap!=INVALID_HANDLE_VALUE );
35288#if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_MALLOC_VALIDATE)
35289    assert( osHeapValidate(pWinMemData->hHeap, SQLITE_WIN32_HEAP_FLAGS, NULL) );
35290#endif
35291    if( pWinMemData->bOwned ){
35292      if( !osHeapDestroy(pWinMemData->hHeap) ){
35293        sqlite3_log(SQLITE_NOMEM, "failed to HeapDestroy (%lu), heap=%p",
35294                    osGetLastError(), (void*)pWinMemData->hHeap);
35295      }
35296      pWinMemData->bOwned = FALSE;
35297    }
35298    pWinMemData->hHeap = NULL;
35299  }
35300}
35301
35302/*
35303** Populate the low-level memory allocation function pointers in
35304** sqlite3GlobalConfig.m with pointers to the routines in this file. The
35305** arguments specify the block of memory to manage.
35306**
35307** This routine is only called by sqlite3_config(), and therefore
35308** is not required to be threadsafe (it is not).
35309*/
35310SQLITE_PRIVATE const sqlite3_mem_methods *sqlite3MemGetWin32(void){
35311  static const sqlite3_mem_methods winMemMethods = {
35312    winMemMalloc,
35313    winMemFree,
35314    winMemRealloc,
35315    winMemSize,
35316    winMemRoundup,
35317    winMemInit,
35318    winMemShutdown,
35319    &win_mem_data
35320  };
35321  return &winMemMethods;
35322}
35323
35324SQLITE_PRIVATE void sqlite3MemSetDefault(void){
35325  sqlite3_config(SQLITE_CONFIG_MALLOC, sqlite3MemGetWin32());
35326}
35327#endif /* SQLITE_WIN32_MALLOC */
35328
35329/*
35330** Convert a UTF-8 string to Microsoft Unicode (UTF-16?).
35331**
35332** Space to hold the returned string is obtained from malloc.
35333*/
35334static LPWSTR winUtf8ToUnicode(const char *zFilename){
35335  int nChar;
35336  LPWSTR zWideFilename;
35337
35338  nChar = osMultiByteToWideChar(CP_UTF8, 0, zFilename, -1, NULL, 0);
35339  if( nChar==0 ){
35340    return 0;
35341  }
35342  zWideFilename = sqlite3MallocZero( nChar*sizeof(zWideFilename[0]) );
35343  if( zWideFilename==0 ){
35344    return 0;
35345  }
35346  nChar = osMultiByteToWideChar(CP_UTF8, 0, zFilename, -1, zWideFilename,
35347                                nChar);
35348  if( nChar==0 ){
35349    sqlite3_free(zWideFilename);
35350    zWideFilename = 0;
35351  }
35352  return zWideFilename;
35353}
35354
35355/*
35356** Convert Microsoft Unicode to UTF-8.  Space to hold the returned string is
35357** obtained from sqlite3_malloc().
35358*/
35359static char *winUnicodeToUtf8(LPCWSTR zWideFilename){
35360  int nByte;
35361  char *zFilename;
35362
35363  nByte = osWideCharToMultiByte(CP_UTF8, 0, zWideFilename, -1, 0, 0, 0, 0);
35364  if( nByte == 0 ){
35365    return 0;
35366  }
35367  zFilename = sqlite3MallocZero( nByte );
35368  if( zFilename==0 ){
35369    return 0;
35370  }
35371  nByte = osWideCharToMultiByte(CP_UTF8, 0, zWideFilename, -1, zFilename, nByte,
35372                                0, 0);
35373  if( nByte == 0 ){
35374    sqlite3_free(zFilename);
35375    zFilename = 0;
35376  }
35377  return zFilename;
35378}
35379
35380/*
35381** Convert an ANSI string to Microsoft Unicode, based on the
35382** current codepage settings for file apis.
35383**
35384** Space to hold the returned string is obtained
35385** from sqlite3_malloc.
35386*/
35387static LPWSTR winMbcsToUnicode(const char *zFilename){
35388  int nByte;
35389  LPWSTR zMbcsFilename;
35390  int codepage = osAreFileApisANSI() ? CP_ACP : CP_OEMCP;
35391
35392  nByte = osMultiByteToWideChar(codepage, 0, zFilename, -1, NULL,
35393                                0)*sizeof(WCHAR);
35394  if( nByte==0 ){
35395    return 0;
35396  }
35397  zMbcsFilename = sqlite3MallocZero( nByte*sizeof(zMbcsFilename[0]) );
35398  if( zMbcsFilename==0 ){
35399    return 0;
35400  }
35401  nByte = osMultiByteToWideChar(codepage, 0, zFilename, -1, zMbcsFilename,
35402                                nByte);
35403  if( nByte==0 ){
35404    sqlite3_free(zMbcsFilename);
35405    zMbcsFilename = 0;
35406  }
35407  return zMbcsFilename;
35408}
35409
35410/*
35411** Convert Microsoft Unicode to multi-byte character string, based on the
35412** user's ANSI codepage.
35413**
35414** Space to hold the returned string is obtained from
35415** sqlite3_malloc().
35416*/
35417static char *winUnicodeToMbcs(LPCWSTR zWideFilename){
35418  int nByte;
35419  char *zFilename;
35420  int codepage = osAreFileApisANSI() ? CP_ACP : CP_OEMCP;
35421
35422  nByte = osWideCharToMultiByte(codepage, 0, zWideFilename, -1, 0, 0, 0, 0);
35423  if( nByte == 0 ){
35424    return 0;
35425  }
35426  zFilename = sqlite3MallocZero( nByte );
35427  if( zFilename==0 ){
35428    return 0;
35429  }
35430  nByte = osWideCharToMultiByte(codepage, 0, zWideFilename, -1, zFilename,
35431                                nByte, 0, 0);
35432  if( nByte == 0 ){
35433    sqlite3_free(zFilename);
35434    zFilename = 0;
35435  }
35436  return zFilename;
35437}
35438
35439/*
35440** Convert multibyte character string to UTF-8.  Space to hold the
35441** returned string is obtained from sqlite3_malloc().
35442*/
35443SQLITE_API char *SQLITE_STDCALL sqlite3_win32_mbcs_to_utf8(const char *zFilename){
35444  char *zFilenameUtf8;
35445  LPWSTR zTmpWide;
35446
35447  zTmpWide = winMbcsToUnicode(zFilename);
35448  if( zTmpWide==0 ){
35449    return 0;
35450  }
35451  zFilenameUtf8 = winUnicodeToUtf8(zTmpWide);
35452  sqlite3_free(zTmpWide);
35453  return zFilenameUtf8;
35454}
35455
35456/*
35457** Convert UTF-8 to multibyte character string.  Space to hold the
35458** returned string is obtained from sqlite3_malloc().
35459*/
35460SQLITE_API char *SQLITE_STDCALL sqlite3_win32_utf8_to_mbcs(const char *zFilename){
35461  char *zFilenameMbcs;
35462  LPWSTR zTmpWide;
35463
35464  zTmpWide = winUtf8ToUnicode(zFilename);
35465  if( zTmpWide==0 ){
35466    return 0;
35467  }
35468  zFilenameMbcs = winUnicodeToMbcs(zTmpWide);
35469  sqlite3_free(zTmpWide);
35470  return zFilenameMbcs;
35471}
35472
35473/*
35474** This function sets the data directory or the temporary directory based on
35475** the provided arguments.  The type argument must be 1 in order to set the
35476** data directory or 2 in order to set the temporary directory.  The zValue
35477** argument is the name of the directory to use.  The return value will be
35478** SQLITE_OK if successful.
35479*/
35480SQLITE_API int SQLITE_STDCALL sqlite3_win32_set_directory(DWORD type, LPCWSTR zValue){
35481  char **ppDirectory = 0;
35482#ifndef SQLITE_OMIT_AUTOINIT
35483  int rc = sqlite3_initialize();
35484  if( rc ) return rc;
35485#endif
35486  if( type==SQLITE_WIN32_DATA_DIRECTORY_TYPE ){
35487    ppDirectory = &sqlite3_data_directory;
35488  }else if( type==SQLITE_WIN32_TEMP_DIRECTORY_TYPE ){
35489    ppDirectory = &sqlite3_temp_directory;
35490  }
35491  assert( !ppDirectory || type==SQLITE_WIN32_DATA_DIRECTORY_TYPE
35492          || type==SQLITE_WIN32_TEMP_DIRECTORY_TYPE
35493  );
35494  assert( !ppDirectory || sqlite3MemdebugHasType(*ppDirectory, MEMTYPE_HEAP) );
35495  if( ppDirectory ){
35496    char *zValueUtf8 = 0;
35497    if( zValue && zValue[0] ){
35498      zValueUtf8 = winUnicodeToUtf8(zValue);
35499      if ( zValueUtf8==0 ){
35500        return SQLITE_NOMEM;
35501      }
35502    }
35503    sqlite3_free(*ppDirectory);
35504    *ppDirectory = zValueUtf8;
35505    return SQLITE_OK;
35506  }
35507  return SQLITE_ERROR;
35508}
35509
35510/*
35511** The return value of winGetLastErrorMsg
35512** is zero if the error message fits in the buffer, or non-zero
35513** otherwise (if the message was truncated).
35514*/
35515static int winGetLastErrorMsg(DWORD lastErrno, int nBuf, char *zBuf){
35516  /* FormatMessage returns 0 on failure.  Otherwise it
35517  ** returns the number of TCHARs written to the output
35518  ** buffer, excluding the terminating null char.
35519  */
35520  DWORD dwLen = 0;
35521  char *zOut = 0;
35522
35523  if( osIsNT() ){
35524#if SQLITE_OS_WINRT
35525    WCHAR zTempWide[SQLITE_WIN32_MAX_ERRMSG_CHARS+1];
35526    dwLen = osFormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM |
35527                             FORMAT_MESSAGE_IGNORE_INSERTS,
35528                             NULL,
35529                             lastErrno,
35530                             0,
35531                             zTempWide,
35532                             SQLITE_WIN32_MAX_ERRMSG_CHARS,
35533                             0);
35534#else
35535    LPWSTR zTempWide = NULL;
35536    dwLen = osFormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER |
35537                             FORMAT_MESSAGE_FROM_SYSTEM |
35538                             FORMAT_MESSAGE_IGNORE_INSERTS,
35539                             NULL,
35540                             lastErrno,
35541                             0,
35542                             (LPWSTR) &zTempWide,
35543                             0,
35544                             0);
35545#endif
35546    if( dwLen > 0 ){
35547      /* allocate a buffer and convert to UTF8 */
35548      sqlite3BeginBenignMalloc();
35549      zOut = winUnicodeToUtf8(zTempWide);
35550      sqlite3EndBenignMalloc();
35551#if !SQLITE_OS_WINRT
35552      /* free the system buffer allocated by FormatMessage */
35553      osLocalFree(zTempWide);
35554#endif
35555    }
35556  }
35557#ifdef SQLITE_WIN32_HAS_ANSI
35558  else{
35559    char *zTemp = NULL;
35560    dwLen = osFormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER |
35561                             FORMAT_MESSAGE_FROM_SYSTEM |
35562                             FORMAT_MESSAGE_IGNORE_INSERTS,
35563                             NULL,
35564                             lastErrno,
35565                             0,
35566                             (LPSTR) &zTemp,
35567                             0,
35568                             0);
35569    if( dwLen > 0 ){
35570      /* allocate a buffer and convert to UTF8 */
35571      sqlite3BeginBenignMalloc();
35572      zOut = sqlite3_win32_mbcs_to_utf8(zTemp);
35573      sqlite3EndBenignMalloc();
35574      /* free the system buffer allocated by FormatMessage */
35575      osLocalFree(zTemp);
35576    }
35577  }
35578#endif
35579  if( 0 == dwLen ){
35580    sqlite3_snprintf(nBuf, zBuf, "OsError 0x%lx (%lu)", lastErrno, lastErrno);
35581  }else{
35582    /* copy a maximum of nBuf chars to output buffer */
35583    sqlite3_snprintf(nBuf, zBuf, "%s", zOut);
35584    /* free the UTF8 buffer */
35585    sqlite3_free(zOut);
35586  }
35587  return 0;
35588}
35589
35590/*
35591**
35592** This function - winLogErrorAtLine() - is only ever called via the macro
35593** winLogError().
35594**
35595** This routine is invoked after an error occurs in an OS function.
35596** It logs a message using sqlite3_log() containing the current value of
35597** error code and, if possible, the human-readable equivalent from
35598** FormatMessage.
35599**
35600** The first argument passed to the macro should be the error code that
35601** will be returned to SQLite (e.g. SQLITE_IOERR_DELETE, SQLITE_CANTOPEN).
35602** The two subsequent arguments should be the name of the OS function that
35603** failed and the associated file-system path, if any.
35604*/
35605#define winLogError(a,b,c,d)   winLogErrorAtLine(a,b,c,d,__LINE__)
35606static int winLogErrorAtLine(
35607  int errcode,                    /* SQLite error code */
35608  DWORD lastErrno,                /* Win32 last error */
35609  const char *zFunc,              /* Name of OS function that failed */
35610  const char *zPath,              /* File path associated with error */
35611  int iLine                       /* Source line number where error occurred */
35612){
35613  char zMsg[500];                 /* Human readable error text */
35614  int i;                          /* Loop counter */
35615
35616  zMsg[0] = 0;
35617  winGetLastErrorMsg(lastErrno, sizeof(zMsg), zMsg);
35618  assert( errcode!=SQLITE_OK );
35619  if( zPath==0 ) zPath = "";
35620  for(i=0; zMsg[i] && zMsg[i]!='\r' && zMsg[i]!='\n'; i++){}
35621  zMsg[i] = 0;
35622  sqlite3_log(errcode,
35623      "os_win.c:%d: (%lu) %s(%s) - %s",
35624      iLine, lastErrno, zFunc, zPath, zMsg
35625  );
35626
35627  return errcode;
35628}
35629
35630/*
35631** The number of times that a ReadFile(), WriteFile(), and DeleteFile()
35632** will be retried following a locking error - probably caused by
35633** antivirus software.  Also the initial delay before the first retry.
35634** The delay increases linearly with each retry.
35635*/
35636#ifndef SQLITE_WIN32_IOERR_RETRY
35637# define SQLITE_WIN32_IOERR_RETRY 10
35638#endif
35639#ifndef SQLITE_WIN32_IOERR_RETRY_DELAY
35640# define SQLITE_WIN32_IOERR_RETRY_DELAY 25
35641#endif
35642static int winIoerrRetry = SQLITE_WIN32_IOERR_RETRY;
35643static int winIoerrRetryDelay = SQLITE_WIN32_IOERR_RETRY_DELAY;
35644
35645/*
35646** The "winIoerrCanRetry1" macro is used to determine if a particular I/O
35647** error code obtained via GetLastError() is eligible to be retried.  It
35648** must accept the error code DWORD as its only argument and should return
35649** non-zero if the error code is transient in nature and the operation
35650** responsible for generating the original error might succeed upon being
35651** retried.  The argument to this macro should be a variable.
35652**
35653** Additionally, a macro named "winIoerrCanRetry2" may be defined.  If it
35654** is defined, it will be consulted only when the macro "winIoerrCanRetry1"
35655** returns zero.  The "winIoerrCanRetry2" macro is completely optional and
35656** may be used to include additional error codes in the set that should
35657** result in the failing I/O operation being retried by the caller.  If
35658** defined, the "winIoerrCanRetry2" macro must exhibit external semantics
35659** identical to those of the "winIoerrCanRetry1" macro.
35660*/
35661#if !defined(winIoerrCanRetry1)
35662#define winIoerrCanRetry1(a) (((a)==ERROR_ACCESS_DENIED)        || \
35663                              ((a)==ERROR_SHARING_VIOLATION)    || \
35664                              ((a)==ERROR_LOCK_VIOLATION)       || \
35665                              ((a)==ERROR_DEV_NOT_EXIST)        || \
35666                              ((a)==ERROR_NETNAME_DELETED)      || \
35667                              ((a)==ERROR_SEM_TIMEOUT)          || \
35668                              ((a)==ERROR_NETWORK_UNREACHABLE))
35669#endif
35670
35671/*
35672** If a ReadFile() or WriteFile() error occurs, invoke this routine
35673** to see if it should be retried.  Return TRUE to retry.  Return FALSE
35674** to give up with an error.
35675*/
35676static int winRetryIoerr(int *pnRetry, DWORD *pError){
35677  DWORD e = osGetLastError();
35678  if( *pnRetry>=winIoerrRetry ){
35679    if( pError ){
35680      *pError = e;
35681    }
35682    return 0;
35683  }
35684  if( winIoerrCanRetry1(e) ){
35685    sqlite3_win32_sleep(winIoerrRetryDelay*(1+*pnRetry));
35686    ++*pnRetry;
35687    return 1;
35688  }
35689#if defined(winIoerrCanRetry2)
35690  else if( winIoerrCanRetry2(e) ){
35691    sqlite3_win32_sleep(winIoerrRetryDelay*(1+*pnRetry));
35692    ++*pnRetry;
35693    return 1;
35694  }
35695#endif
35696  if( pError ){
35697    *pError = e;
35698  }
35699  return 0;
35700}
35701
35702/*
35703** Log a I/O error retry episode.
35704*/
35705static void winLogIoerr(int nRetry, int lineno){
35706  if( nRetry ){
35707    sqlite3_log(SQLITE_NOTICE,
35708      "delayed %dms for lock/sharing conflict at line %d",
35709      winIoerrRetryDelay*nRetry*(nRetry+1)/2, lineno
35710    );
35711  }
35712}
35713
35714#if SQLITE_OS_WINCE
35715/*************************************************************************
35716** This section contains code for WinCE only.
35717*/
35718#if !defined(SQLITE_MSVC_LOCALTIME_API) || !SQLITE_MSVC_LOCALTIME_API
35719/*
35720** The MSVC CRT on Windows CE may not have a localtime() function.  So
35721** create a substitute.
35722*/
35723/* #include <time.h> */
35724struct tm *__cdecl localtime(const time_t *t)
35725{
35726  static struct tm y;
35727  FILETIME uTm, lTm;
35728  SYSTEMTIME pTm;
35729  sqlite3_int64 t64;
35730  t64 = *t;
35731  t64 = (t64 + 11644473600)*10000000;
35732  uTm.dwLowDateTime = (DWORD)(t64 & 0xFFFFFFFF);
35733  uTm.dwHighDateTime= (DWORD)(t64 >> 32);
35734  osFileTimeToLocalFileTime(&uTm,&lTm);
35735  osFileTimeToSystemTime(&lTm,&pTm);
35736  y.tm_year = pTm.wYear - 1900;
35737  y.tm_mon = pTm.wMonth - 1;
35738  y.tm_wday = pTm.wDayOfWeek;
35739  y.tm_mday = pTm.wDay;
35740  y.tm_hour = pTm.wHour;
35741  y.tm_min = pTm.wMinute;
35742  y.tm_sec = pTm.wSecond;
35743  return &y;
35744}
35745#endif
35746
35747#define HANDLE_TO_WINFILE(a) (winFile*)&((char*)a)[-(int)offsetof(winFile,h)]
35748
35749/*
35750** Acquire a lock on the handle h
35751*/
35752static void winceMutexAcquire(HANDLE h){
35753   DWORD dwErr;
35754   do {
35755     dwErr = osWaitForSingleObject(h, INFINITE);
35756   } while (dwErr != WAIT_OBJECT_0 && dwErr != WAIT_ABANDONED);
35757}
35758/*
35759** Release a lock acquired by winceMutexAcquire()
35760*/
35761#define winceMutexRelease(h) ReleaseMutex(h)
35762
35763/*
35764** Create the mutex and shared memory used for locking in the file
35765** descriptor pFile
35766*/
35767static int winceCreateLock(const char *zFilename, winFile *pFile){
35768  LPWSTR zTok;
35769  LPWSTR zName;
35770  DWORD lastErrno;
35771  BOOL bLogged = FALSE;
35772  BOOL bInit = TRUE;
35773
35774  zName = winUtf8ToUnicode(zFilename);
35775  if( zName==0 ){
35776    /* out of memory */
35777    return SQLITE_IOERR_NOMEM;
35778  }
35779
35780  /* Initialize the local lockdata */
35781  memset(&pFile->local, 0, sizeof(pFile->local));
35782
35783  /* Replace the backslashes from the filename and lowercase it
35784  ** to derive a mutex name. */
35785  zTok = osCharLowerW(zName);
35786  for (;*zTok;zTok++){
35787    if (*zTok == '\\') *zTok = '_';
35788  }
35789
35790  /* Create/open the named mutex */
35791  pFile->hMutex = osCreateMutexW(NULL, FALSE, zName);
35792  if (!pFile->hMutex){
35793    pFile->lastErrno = osGetLastError();
35794    sqlite3_free(zName);
35795    return winLogError(SQLITE_IOERR, pFile->lastErrno,
35796                       "winceCreateLock1", zFilename);
35797  }
35798
35799  /* Acquire the mutex before continuing */
35800  winceMutexAcquire(pFile->hMutex);
35801
35802  /* Since the names of named mutexes, semaphores, file mappings etc are
35803  ** case-sensitive, take advantage of that by uppercasing the mutex name
35804  ** and using that as the shared filemapping name.
35805  */
35806  osCharUpperW(zName);
35807  pFile->hShared = osCreateFileMappingW(INVALID_HANDLE_VALUE, NULL,
35808                                        PAGE_READWRITE, 0, sizeof(winceLock),
35809                                        zName);
35810
35811  /* Set a flag that indicates we're the first to create the memory so it
35812  ** must be zero-initialized */
35813  lastErrno = osGetLastError();
35814  if (lastErrno == ERROR_ALREADY_EXISTS){
35815    bInit = FALSE;
35816  }
35817
35818  sqlite3_free(zName);
35819
35820  /* If we succeeded in making the shared memory handle, map it. */
35821  if( pFile->hShared ){
35822    pFile->shared = (winceLock*)osMapViewOfFile(pFile->hShared,
35823             FILE_MAP_READ|FILE_MAP_WRITE, 0, 0, sizeof(winceLock));
35824    /* If mapping failed, close the shared memory handle and erase it */
35825    if( !pFile->shared ){
35826      pFile->lastErrno = osGetLastError();
35827      winLogError(SQLITE_IOERR, pFile->lastErrno,
35828                  "winceCreateLock2", zFilename);
35829      bLogged = TRUE;
35830      osCloseHandle(pFile->hShared);
35831      pFile->hShared = NULL;
35832    }
35833  }
35834
35835  /* If shared memory could not be created, then close the mutex and fail */
35836  if( pFile->hShared==NULL ){
35837    if( !bLogged ){
35838      pFile->lastErrno = lastErrno;
35839      winLogError(SQLITE_IOERR, pFile->lastErrno,
35840                  "winceCreateLock3", zFilename);
35841      bLogged = TRUE;
35842    }
35843    winceMutexRelease(pFile->hMutex);
35844    osCloseHandle(pFile->hMutex);
35845    pFile->hMutex = NULL;
35846    return SQLITE_IOERR;
35847  }
35848
35849  /* Initialize the shared memory if we're supposed to */
35850  if( bInit ){
35851    memset(pFile->shared, 0, sizeof(winceLock));
35852  }
35853
35854  winceMutexRelease(pFile->hMutex);
35855  return SQLITE_OK;
35856}
35857
35858/*
35859** Destroy the part of winFile that deals with wince locks
35860*/
35861static void winceDestroyLock(winFile *pFile){
35862  if (pFile->hMutex){
35863    /* Acquire the mutex */
35864    winceMutexAcquire(pFile->hMutex);
35865
35866    /* The following blocks should probably assert in debug mode, but they
35867       are to cleanup in case any locks remained open */
35868    if (pFile->local.nReaders){
35869      pFile->shared->nReaders --;
35870    }
35871    if (pFile->local.bReserved){
35872      pFile->shared->bReserved = FALSE;
35873    }
35874    if (pFile->local.bPending){
35875      pFile->shared->bPending = FALSE;
35876    }
35877    if (pFile->local.bExclusive){
35878      pFile->shared->bExclusive = FALSE;
35879    }
35880
35881    /* De-reference and close our copy of the shared memory handle */
35882    osUnmapViewOfFile(pFile->shared);
35883    osCloseHandle(pFile->hShared);
35884
35885    /* Done with the mutex */
35886    winceMutexRelease(pFile->hMutex);
35887    osCloseHandle(pFile->hMutex);
35888    pFile->hMutex = NULL;
35889  }
35890}
35891
35892/*
35893** An implementation of the LockFile() API of Windows for CE
35894*/
35895static BOOL winceLockFile(
35896  LPHANDLE phFile,
35897  DWORD dwFileOffsetLow,
35898  DWORD dwFileOffsetHigh,
35899  DWORD nNumberOfBytesToLockLow,
35900  DWORD nNumberOfBytesToLockHigh
35901){
35902  winFile *pFile = HANDLE_TO_WINFILE(phFile);
35903  BOOL bReturn = FALSE;
35904
35905  UNUSED_PARAMETER(dwFileOffsetHigh);
35906  UNUSED_PARAMETER(nNumberOfBytesToLockHigh);
35907
35908  if (!pFile->hMutex) return TRUE;
35909  winceMutexAcquire(pFile->hMutex);
35910
35911  /* Wanting an exclusive lock? */
35912  if (dwFileOffsetLow == (DWORD)SHARED_FIRST
35913       && nNumberOfBytesToLockLow == (DWORD)SHARED_SIZE){
35914    if (pFile->shared->nReaders == 0 && pFile->shared->bExclusive == 0){
35915       pFile->shared->bExclusive = TRUE;
35916       pFile->local.bExclusive = TRUE;
35917       bReturn = TRUE;
35918    }
35919  }
35920
35921  /* Want a read-only lock? */
35922  else if (dwFileOffsetLow == (DWORD)SHARED_FIRST &&
35923           nNumberOfBytesToLockLow == 1){
35924    if (pFile->shared->bExclusive == 0){
35925      pFile->local.nReaders ++;
35926      if (pFile->local.nReaders == 1){
35927        pFile->shared->nReaders ++;
35928      }
35929      bReturn = TRUE;
35930    }
35931  }
35932
35933  /* Want a pending lock? */
35934  else if (dwFileOffsetLow == (DWORD)PENDING_BYTE
35935           && nNumberOfBytesToLockLow == 1){
35936    /* If no pending lock has been acquired, then acquire it */
35937    if (pFile->shared->bPending == 0) {
35938      pFile->shared->bPending = TRUE;
35939      pFile->local.bPending = TRUE;
35940      bReturn = TRUE;
35941    }
35942  }
35943
35944  /* Want a reserved lock? */
35945  else if (dwFileOffsetLow == (DWORD)RESERVED_BYTE
35946           && nNumberOfBytesToLockLow == 1){
35947    if (pFile->shared->bReserved == 0) {
35948      pFile->shared->bReserved = TRUE;
35949      pFile->local.bReserved = TRUE;
35950      bReturn = TRUE;
35951    }
35952  }
35953
35954  winceMutexRelease(pFile->hMutex);
35955  return bReturn;
35956}
35957
35958/*
35959** An implementation of the UnlockFile API of Windows for CE
35960*/
35961static BOOL winceUnlockFile(
35962  LPHANDLE phFile,
35963  DWORD dwFileOffsetLow,
35964  DWORD dwFileOffsetHigh,
35965  DWORD nNumberOfBytesToUnlockLow,
35966  DWORD nNumberOfBytesToUnlockHigh
35967){
35968  winFile *pFile = HANDLE_TO_WINFILE(phFile);
35969  BOOL bReturn = FALSE;
35970
35971  UNUSED_PARAMETER(dwFileOffsetHigh);
35972  UNUSED_PARAMETER(nNumberOfBytesToUnlockHigh);
35973
35974  if (!pFile->hMutex) return TRUE;
35975  winceMutexAcquire(pFile->hMutex);
35976
35977  /* Releasing a reader lock or an exclusive lock */
35978  if (dwFileOffsetLow == (DWORD)SHARED_FIRST){
35979    /* Did we have an exclusive lock? */
35980    if (pFile->local.bExclusive){
35981      assert(nNumberOfBytesToUnlockLow == (DWORD)SHARED_SIZE);
35982      pFile->local.bExclusive = FALSE;
35983      pFile->shared->bExclusive = FALSE;
35984      bReturn = TRUE;
35985    }
35986
35987    /* Did we just have a reader lock? */
35988    else if (pFile->local.nReaders){
35989      assert(nNumberOfBytesToUnlockLow == (DWORD)SHARED_SIZE
35990             || nNumberOfBytesToUnlockLow == 1);
35991      pFile->local.nReaders --;
35992      if (pFile->local.nReaders == 0)
35993      {
35994        pFile->shared->nReaders --;
35995      }
35996      bReturn = TRUE;
35997    }
35998  }
35999
36000  /* Releasing a pending lock */
36001  else if (dwFileOffsetLow == (DWORD)PENDING_BYTE
36002           && nNumberOfBytesToUnlockLow == 1){
36003    if (pFile->local.bPending){
36004      pFile->local.bPending = FALSE;
36005      pFile->shared->bPending = FALSE;
36006      bReturn = TRUE;
36007    }
36008  }
36009  /* Releasing a reserved lock */
36010  else if (dwFileOffsetLow == (DWORD)RESERVED_BYTE
36011           && nNumberOfBytesToUnlockLow == 1){
36012    if (pFile->local.bReserved) {
36013      pFile->local.bReserved = FALSE;
36014      pFile->shared->bReserved = FALSE;
36015      bReturn = TRUE;
36016    }
36017  }
36018
36019  winceMutexRelease(pFile->hMutex);
36020  return bReturn;
36021}
36022/*
36023** End of the special code for wince
36024*****************************************************************************/
36025#endif /* SQLITE_OS_WINCE */
36026
36027/*
36028** Lock a file region.
36029*/
36030static BOOL winLockFile(
36031  LPHANDLE phFile,
36032  DWORD flags,
36033  DWORD offsetLow,
36034  DWORD offsetHigh,
36035  DWORD numBytesLow,
36036  DWORD numBytesHigh
36037){
36038#if SQLITE_OS_WINCE
36039  /*
36040  ** NOTE: Windows CE is handled differently here due its lack of the Win32
36041  **       API LockFile.
36042  */
36043  return winceLockFile(phFile, offsetLow, offsetHigh,
36044                       numBytesLow, numBytesHigh);
36045#else
36046  if( osIsNT() ){
36047    OVERLAPPED ovlp;
36048    memset(&ovlp, 0, sizeof(OVERLAPPED));
36049    ovlp.Offset = offsetLow;
36050    ovlp.OffsetHigh = offsetHigh;
36051    return osLockFileEx(*phFile, flags, 0, numBytesLow, numBytesHigh, &ovlp);
36052  }else{
36053    return osLockFile(*phFile, offsetLow, offsetHigh, numBytesLow,
36054                      numBytesHigh);
36055  }
36056#endif
36057}
36058
36059/*
36060** Unlock a file region.
36061 */
36062static BOOL winUnlockFile(
36063  LPHANDLE phFile,
36064  DWORD offsetLow,
36065  DWORD offsetHigh,
36066  DWORD numBytesLow,
36067  DWORD numBytesHigh
36068){
36069#if SQLITE_OS_WINCE
36070  /*
36071  ** NOTE: Windows CE is handled differently here due its lack of the Win32
36072  **       API UnlockFile.
36073  */
36074  return winceUnlockFile(phFile, offsetLow, offsetHigh,
36075                         numBytesLow, numBytesHigh);
36076#else
36077  if( osIsNT() ){
36078    OVERLAPPED ovlp;
36079    memset(&ovlp, 0, sizeof(OVERLAPPED));
36080    ovlp.Offset = offsetLow;
36081    ovlp.OffsetHigh = offsetHigh;
36082    return osUnlockFileEx(*phFile, 0, numBytesLow, numBytesHigh, &ovlp);
36083  }else{
36084    return osUnlockFile(*phFile, offsetLow, offsetHigh, numBytesLow,
36085                        numBytesHigh);
36086  }
36087#endif
36088}
36089
36090/*****************************************************************************
36091** The next group of routines implement the I/O methods specified
36092** by the sqlite3_io_methods object.
36093******************************************************************************/
36094
36095/*
36096** Some Microsoft compilers lack this definition.
36097*/
36098#ifndef INVALID_SET_FILE_POINTER
36099# define INVALID_SET_FILE_POINTER ((DWORD)-1)
36100#endif
36101
36102/*
36103** Move the current position of the file handle passed as the first
36104** argument to offset iOffset within the file. If successful, return 0.
36105** Otherwise, set pFile->lastErrno and return non-zero.
36106*/
36107static int winSeekFile(winFile *pFile, sqlite3_int64 iOffset){
36108#if !SQLITE_OS_WINRT
36109  LONG upperBits;                 /* Most sig. 32 bits of new offset */
36110  LONG lowerBits;                 /* Least sig. 32 bits of new offset */
36111  DWORD dwRet;                    /* Value returned by SetFilePointer() */
36112  DWORD lastErrno;                /* Value returned by GetLastError() */
36113
36114  OSTRACE(("SEEK file=%p, offset=%lld\n", pFile->h, iOffset));
36115
36116  upperBits = (LONG)((iOffset>>32) & 0x7fffffff);
36117  lowerBits = (LONG)(iOffset & 0xffffffff);
36118
36119  /* API oddity: If successful, SetFilePointer() returns a dword
36120  ** containing the lower 32-bits of the new file-offset. Or, if it fails,
36121  ** it returns INVALID_SET_FILE_POINTER. However according to MSDN,
36122  ** INVALID_SET_FILE_POINTER may also be a valid new offset. So to determine
36123  ** whether an error has actually occurred, it is also necessary to call
36124  ** GetLastError().
36125  */
36126  dwRet = osSetFilePointer(pFile->h, lowerBits, &upperBits, FILE_BEGIN);
36127
36128  if( (dwRet==INVALID_SET_FILE_POINTER
36129      && ((lastErrno = osGetLastError())!=NO_ERROR)) ){
36130    pFile->lastErrno = lastErrno;
36131    winLogError(SQLITE_IOERR_SEEK, pFile->lastErrno,
36132                "winSeekFile", pFile->zPath);
36133    OSTRACE(("SEEK file=%p, rc=SQLITE_IOERR_SEEK\n", pFile->h));
36134    return 1;
36135  }
36136
36137  OSTRACE(("SEEK file=%p, rc=SQLITE_OK\n", pFile->h));
36138  return 0;
36139#else
36140  /*
36141  ** Same as above, except that this implementation works for WinRT.
36142  */
36143
36144  LARGE_INTEGER x;                /* The new offset */
36145  BOOL bRet;                      /* Value returned by SetFilePointerEx() */
36146
36147  x.QuadPart = iOffset;
36148  bRet = osSetFilePointerEx(pFile->h, x, 0, FILE_BEGIN);
36149
36150  if(!bRet){
36151    pFile->lastErrno = osGetLastError();
36152    winLogError(SQLITE_IOERR_SEEK, pFile->lastErrno,
36153                "winSeekFile", pFile->zPath);
36154    OSTRACE(("SEEK file=%p, rc=SQLITE_IOERR_SEEK\n", pFile->h));
36155    return 1;
36156  }
36157
36158  OSTRACE(("SEEK file=%p, rc=SQLITE_OK\n", pFile->h));
36159  return 0;
36160#endif
36161}
36162
36163#if SQLITE_MAX_MMAP_SIZE>0
36164/* Forward references to VFS helper methods used for memory mapped files */
36165static int winMapfile(winFile*, sqlite3_int64);
36166static int winUnmapfile(winFile*);
36167#endif
36168
36169/*
36170** Close a file.
36171**
36172** It is reported that an attempt to close a handle might sometimes
36173** fail.  This is a very unreasonable result, but Windows is notorious
36174** for being unreasonable so I do not doubt that it might happen.  If
36175** the close fails, we pause for 100 milliseconds and try again.  As
36176** many as MX_CLOSE_ATTEMPT attempts to close the handle are made before
36177** giving up and returning an error.
36178*/
36179#define MX_CLOSE_ATTEMPT 3
36180static int winClose(sqlite3_file *id){
36181  int rc, cnt = 0;
36182  winFile *pFile = (winFile*)id;
36183
36184  assert( id!=0 );
36185#ifndef SQLITE_OMIT_WAL
36186  assert( pFile->pShm==0 );
36187#endif
36188  assert( pFile->h!=NULL && pFile->h!=INVALID_HANDLE_VALUE );
36189  OSTRACE(("CLOSE pid=%lu, pFile=%p, file=%p\n",
36190           osGetCurrentProcessId(), pFile, pFile->h));
36191
36192#if SQLITE_MAX_MMAP_SIZE>0
36193  winUnmapfile(pFile);
36194#endif
36195
36196  do{
36197    rc = osCloseHandle(pFile->h);
36198    /* SimulateIOError( rc=0; cnt=MX_CLOSE_ATTEMPT; ); */
36199  }while( rc==0 && ++cnt < MX_CLOSE_ATTEMPT && (sqlite3_win32_sleep(100), 1) );
36200#if SQLITE_OS_WINCE
36201#define WINCE_DELETION_ATTEMPTS 3
36202  winceDestroyLock(pFile);
36203  if( pFile->zDeleteOnClose ){
36204    int cnt = 0;
36205    while(
36206           osDeleteFileW(pFile->zDeleteOnClose)==0
36207        && osGetFileAttributesW(pFile->zDeleteOnClose)!=0xffffffff
36208        && cnt++ < WINCE_DELETION_ATTEMPTS
36209    ){
36210       sqlite3_win32_sleep(100);  /* Wait a little before trying again */
36211    }
36212    sqlite3_free(pFile->zDeleteOnClose);
36213  }
36214#endif
36215  if( rc ){
36216    pFile->h = NULL;
36217  }
36218  OpenCounter(-1);
36219  OSTRACE(("CLOSE pid=%lu, pFile=%p, file=%p, rc=%s\n",
36220           osGetCurrentProcessId(), pFile, pFile->h, rc ? "ok" : "failed"));
36221  return rc ? SQLITE_OK
36222            : winLogError(SQLITE_IOERR_CLOSE, osGetLastError(),
36223                          "winClose", pFile->zPath);
36224}
36225
36226/*
36227** Read data from a file into a buffer.  Return SQLITE_OK if all
36228** bytes were read successfully and SQLITE_IOERR if anything goes
36229** wrong.
36230*/
36231static int winRead(
36232  sqlite3_file *id,          /* File to read from */
36233  void *pBuf,                /* Write content into this buffer */
36234  int amt,                   /* Number of bytes to read */
36235  sqlite3_int64 offset       /* Begin reading at this offset */
36236){
36237#if !SQLITE_OS_WINCE && !defined(SQLITE_WIN32_NO_OVERLAPPED)
36238  OVERLAPPED overlapped;          /* The offset for ReadFile. */
36239#endif
36240  winFile *pFile = (winFile*)id;  /* file handle */
36241  DWORD nRead;                    /* Number of bytes actually read from file */
36242  int nRetry = 0;                 /* Number of retrys */
36243
36244  assert( id!=0 );
36245  assert( amt>0 );
36246  assert( offset>=0 );
36247  SimulateIOError(return SQLITE_IOERR_READ);
36248  OSTRACE(("READ pid=%lu, pFile=%p, file=%p, buffer=%p, amount=%d, "
36249           "offset=%lld, lock=%d\n", osGetCurrentProcessId(), pFile,
36250           pFile->h, pBuf, amt, offset, pFile->locktype));
36251
36252#if SQLITE_MAX_MMAP_SIZE>0
36253  /* Deal with as much of this read request as possible by transfering
36254  ** data from the memory mapping using memcpy().  */
36255  if( offset<pFile->mmapSize ){
36256    if( offset+amt <= pFile->mmapSize ){
36257      memcpy(pBuf, &((u8 *)(pFile->pMapRegion))[offset], amt);
36258      OSTRACE(("READ-MMAP pid=%lu, pFile=%p, file=%p, rc=SQLITE_OK\n",
36259               osGetCurrentProcessId(), pFile, pFile->h));
36260      return SQLITE_OK;
36261    }else{
36262      int nCopy = (int)(pFile->mmapSize - offset);
36263      memcpy(pBuf, &((u8 *)(pFile->pMapRegion))[offset], nCopy);
36264      pBuf = &((u8 *)pBuf)[nCopy];
36265      amt -= nCopy;
36266      offset += nCopy;
36267    }
36268  }
36269#endif
36270
36271#if SQLITE_OS_WINCE || defined(SQLITE_WIN32_NO_OVERLAPPED)
36272  if( winSeekFile(pFile, offset) ){
36273    OSTRACE(("READ pid=%lu, pFile=%p, file=%p, rc=SQLITE_FULL\n",
36274             osGetCurrentProcessId(), pFile, pFile->h));
36275    return SQLITE_FULL;
36276  }
36277  while( !osReadFile(pFile->h, pBuf, amt, &nRead, 0) ){
36278#else
36279  memset(&overlapped, 0, sizeof(OVERLAPPED));
36280  overlapped.Offset = (LONG)(offset & 0xffffffff);
36281  overlapped.OffsetHigh = (LONG)((offset>>32) & 0x7fffffff);
36282  while( !osReadFile(pFile->h, pBuf, amt, &nRead, &overlapped) &&
36283         osGetLastError()!=ERROR_HANDLE_EOF ){
36284#endif
36285    DWORD lastErrno;
36286    if( winRetryIoerr(&nRetry, &lastErrno) ) continue;
36287    pFile->lastErrno = lastErrno;
36288    OSTRACE(("READ pid=%lu, pFile=%p, file=%p, rc=SQLITE_IOERR_READ\n",
36289             osGetCurrentProcessId(), pFile, pFile->h));
36290    return winLogError(SQLITE_IOERR_READ, pFile->lastErrno,
36291                       "winRead", pFile->zPath);
36292  }
36293  winLogIoerr(nRetry, __LINE__);
36294  if( nRead<(DWORD)amt ){
36295    /* Unread parts of the buffer must be zero-filled */
36296    memset(&((char*)pBuf)[nRead], 0, amt-nRead);
36297    OSTRACE(("READ pid=%lu, pFile=%p, file=%p, rc=SQLITE_IOERR_SHORT_READ\n",
36298             osGetCurrentProcessId(), pFile, pFile->h));
36299    return SQLITE_IOERR_SHORT_READ;
36300  }
36301
36302  OSTRACE(("READ pid=%lu, pFile=%p, file=%p, rc=SQLITE_OK\n",
36303           osGetCurrentProcessId(), pFile, pFile->h));
36304  return SQLITE_OK;
36305}
36306
36307/*
36308** Write data from a buffer into a file.  Return SQLITE_OK on success
36309** or some other error code on failure.
36310*/
36311static int winWrite(
36312  sqlite3_file *id,               /* File to write into */
36313  const void *pBuf,               /* The bytes to be written */
36314  int amt,                        /* Number of bytes to write */
36315  sqlite3_int64 offset            /* Offset into the file to begin writing at */
36316){
36317  int rc = 0;                     /* True if error has occurred, else false */
36318  winFile *pFile = (winFile*)id;  /* File handle */
36319  int nRetry = 0;                 /* Number of retries */
36320
36321  assert( amt>0 );
36322  assert( pFile );
36323  SimulateIOError(return SQLITE_IOERR_WRITE);
36324  SimulateDiskfullError(return SQLITE_FULL);
36325
36326  OSTRACE(("WRITE pid=%lu, pFile=%p, file=%p, buffer=%p, amount=%d, "
36327           "offset=%lld, lock=%d\n", osGetCurrentProcessId(), pFile,
36328           pFile->h, pBuf, amt, offset, pFile->locktype));
36329
36330#if SQLITE_MAX_MMAP_SIZE>0
36331  /* Deal with as much of this write request as possible by transfering
36332  ** data from the memory mapping using memcpy().  */
36333  if( offset<pFile->mmapSize ){
36334    if( offset+amt <= pFile->mmapSize ){
36335      memcpy(&((u8 *)(pFile->pMapRegion))[offset], pBuf, amt);
36336      OSTRACE(("WRITE-MMAP pid=%lu, pFile=%p, file=%p, rc=SQLITE_OK\n",
36337               osGetCurrentProcessId(), pFile, pFile->h));
36338      return SQLITE_OK;
36339    }else{
36340      int nCopy = (int)(pFile->mmapSize - offset);
36341      memcpy(&((u8 *)(pFile->pMapRegion))[offset], pBuf, nCopy);
36342      pBuf = &((u8 *)pBuf)[nCopy];
36343      amt -= nCopy;
36344      offset += nCopy;
36345    }
36346  }
36347#endif
36348
36349#if SQLITE_OS_WINCE || defined(SQLITE_WIN32_NO_OVERLAPPED)
36350  rc = winSeekFile(pFile, offset);
36351  if( rc==0 ){
36352#else
36353  {
36354#endif
36355#if !SQLITE_OS_WINCE && !defined(SQLITE_WIN32_NO_OVERLAPPED)
36356    OVERLAPPED overlapped;        /* The offset for WriteFile. */
36357#endif
36358    u8 *aRem = (u8 *)pBuf;        /* Data yet to be written */
36359    int nRem = amt;               /* Number of bytes yet to be written */
36360    DWORD nWrite;                 /* Bytes written by each WriteFile() call */
36361    DWORD lastErrno = NO_ERROR;   /* Value returned by GetLastError() */
36362
36363#if !SQLITE_OS_WINCE && !defined(SQLITE_WIN32_NO_OVERLAPPED)
36364    memset(&overlapped, 0, sizeof(OVERLAPPED));
36365    overlapped.Offset = (LONG)(offset & 0xffffffff);
36366    overlapped.OffsetHigh = (LONG)((offset>>32) & 0x7fffffff);
36367#endif
36368
36369    while( nRem>0 ){
36370#if SQLITE_OS_WINCE || defined(SQLITE_WIN32_NO_OVERLAPPED)
36371      if( !osWriteFile(pFile->h, aRem, nRem, &nWrite, 0) ){
36372#else
36373      if( !osWriteFile(pFile->h, aRem, nRem, &nWrite, &overlapped) ){
36374#endif
36375        if( winRetryIoerr(&nRetry, &lastErrno) ) continue;
36376        break;
36377      }
36378      assert( nWrite==0 || nWrite<=(DWORD)nRem );
36379      if( nWrite==0 || nWrite>(DWORD)nRem ){
36380        lastErrno = osGetLastError();
36381        break;
36382      }
36383#if !SQLITE_OS_WINCE && !defined(SQLITE_WIN32_NO_OVERLAPPED)
36384      offset += nWrite;
36385      overlapped.Offset = (LONG)(offset & 0xffffffff);
36386      overlapped.OffsetHigh = (LONG)((offset>>32) & 0x7fffffff);
36387#endif
36388      aRem += nWrite;
36389      nRem -= nWrite;
36390    }
36391    if( nRem>0 ){
36392      pFile->lastErrno = lastErrno;
36393      rc = 1;
36394    }
36395  }
36396
36397  if( rc ){
36398    if(   ( pFile->lastErrno==ERROR_HANDLE_DISK_FULL )
36399       || ( pFile->lastErrno==ERROR_DISK_FULL )){
36400      OSTRACE(("WRITE pid=%lu, pFile=%p, file=%p, rc=SQLITE_FULL\n",
36401               osGetCurrentProcessId(), pFile, pFile->h));
36402      return winLogError(SQLITE_FULL, pFile->lastErrno,
36403                         "winWrite1", pFile->zPath);
36404    }
36405    OSTRACE(("WRITE pid=%lu, pFile=%p, file=%p, rc=SQLITE_IOERR_WRITE\n",
36406             osGetCurrentProcessId(), pFile, pFile->h));
36407    return winLogError(SQLITE_IOERR_WRITE, pFile->lastErrno,
36408                       "winWrite2", pFile->zPath);
36409  }else{
36410    winLogIoerr(nRetry, __LINE__);
36411  }
36412  OSTRACE(("WRITE pid=%lu, pFile=%p, file=%p, rc=SQLITE_OK\n",
36413           osGetCurrentProcessId(), pFile, pFile->h));
36414  return SQLITE_OK;
36415}
36416
36417/*
36418** Truncate an open file to a specified size
36419*/
36420static int winTruncate(sqlite3_file *id, sqlite3_int64 nByte){
36421  winFile *pFile = (winFile*)id;  /* File handle object */
36422  int rc = SQLITE_OK;             /* Return code for this function */
36423  DWORD lastErrno;
36424
36425  assert( pFile );
36426  SimulateIOError(return SQLITE_IOERR_TRUNCATE);
36427  OSTRACE(("TRUNCATE pid=%lu, pFile=%p, file=%p, size=%lld, lock=%d\n",
36428           osGetCurrentProcessId(), pFile, pFile->h, nByte, pFile->locktype));
36429
36430  /* If the user has configured a chunk-size for this file, truncate the
36431  ** file so that it consists of an integer number of chunks (i.e. the
36432  ** actual file size after the operation may be larger than the requested
36433  ** size).
36434  */
36435  if( pFile->szChunk>0 ){
36436    nByte = ((nByte + pFile->szChunk - 1)/pFile->szChunk) * pFile->szChunk;
36437  }
36438
36439  /* SetEndOfFile() returns non-zero when successful, or zero when it fails. */
36440  if( winSeekFile(pFile, nByte) ){
36441    rc = winLogError(SQLITE_IOERR_TRUNCATE, pFile->lastErrno,
36442                     "winTruncate1", pFile->zPath);
36443  }else if( 0==osSetEndOfFile(pFile->h) &&
36444            ((lastErrno = osGetLastError())!=ERROR_USER_MAPPED_FILE) ){
36445    pFile->lastErrno = lastErrno;
36446    rc = winLogError(SQLITE_IOERR_TRUNCATE, pFile->lastErrno,
36447                     "winTruncate2", pFile->zPath);
36448  }
36449
36450#if SQLITE_MAX_MMAP_SIZE>0
36451  /* If the file was truncated to a size smaller than the currently
36452  ** mapped region, reduce the effective mapping size as well. SQLite will
36453  ** use read() and write() to access data beyond this point from now on.
36454  */
36455  if( pFile->pMapRegion && nByte<pFile->mmapSize ){
36456    pFile->mmapSize = nByte;
36457  }
36458#endif
36459
36460  OSTRACE(("TRUNCATE pid=%lu, pFile=%p, file=%p, rc=%s\n",
36461           osGetCurrentProcessId(), pFile, pFile->h, sqlite3ErrName(rc)));
36462  return rc;
36463}
36464
36465#ifdef SQLITE_TEST
36466/*
36467** Count the number of fullsyncs and normal syncs.  This is used to test
36468** that syncs and fullsyncs are occuring at the right times.
36469*/
36470SQLITE_API int sqlite3_sync_count = 0;
36471SQLITE_API int sqlite3_fullsync_count = 0;
36472#endif
36473
36474/*
36475** Make sure all writes to a particular file are committed to disk.
36476*/
36477static int winSync(sqlite3_file *id, int flags){
36478#ifndef SQLITE_NO_SYNC
36479  /*
36480  ** Used only when SQLITE_NO_SYNC is not defined.
36481   */
36482  BOOL rc;
36483#endif
36484#if !defined(NDEBUG) || !defined(SQLITE_NO_SYNC) || \
36485    defined(SQLITE_HAVE_OS_TRACE)
36486  /*
36487  ** Used when SQLITE_NO_SYNC is not defined and by the assert() and/or
36488  ** OSTRACE() macros.
36489   */
36490  winFile *pFile = (winFile*)id;
36491#else
36492  UNUSED_PARAMETER(id);
36493#endif
36494
36495  assert( pFile );
36496  /* Check that one of SQLITE_SYNC_NORMAL or FULL was passed */
36497  assert((flags&0x0F)==SQLITE_SYNC_NORMAL
36498      || (flags&0x0F)==SQLITE_SYNC_FULL
36499  );
36500
36501  /* Unix cannot, but some systems may return SQLITE_FULL from here. This
36502  ** line is to test that doing so does not cause any problems.
36503  */
36504  SimulateDiskfullError( return SQLITE_FULL );
36505
36506  OSTRACE(("SYNC pid=%lu, pFile=%p, file=%p, flags=%x, lock=%d\n",
36507           osGetCurrentProcessId(), pFile, pFile->h, flags,
36508           pFile->locktype));
36509
36510#ifndef SQLITE_TEST
36511  UNUSED_PARAMETER(flags);
36512#else
36513  if( (flags&0x0F)==SQLITE_SYNC_FULL ){
36514    sqlite3_fullsync_count++;
36515  }
36516  sqlite3_sync_count++;
36517#endif
36518
36519  /* If we compiled with the SQLITE_NO_SYNC flag, then syncing is a
36520  ** no-op
36521  */
36522#ifdef SQLITE_NO_SYNC
36523  OSTRACE(("SYNC-NOP pid=%lu, pFile=%p, file=%p, rc=SQLITE_OK\n",
36524           osGetCurrentProcessId(), pFile, pFile->h));
36525  return SQLITE_OK;
36526#else
36527#if SQLITE_MAX_MMAP_SIZE>0
36528  if( pFile->pMapRegion ){
36529    if( osFlushViewOfFile(pFile->pMapRegion, 0) ){
36530      OSTRACE(("SYNC-MMAP pid=%lu, pFile=%p, pMapRegion=%p, "
36531               "rc=SQLITE_OK\n", osGetCurrentProcessId(),
36532               pFile, pFile->pMapRegion));
36533    }else{
36534      pFile->lastErrno = osGetLastError();
36535      OSTRACE(("SYNC-MMAP pid=%lu, pFile=%p, pMapRegion=%p, "
36536               "rc=SQLITE_IOERR_MMAP\n", osGetCurrentProcessId(),
36537               pFile, pFile->pMapRegion));
36538      return winLogError(SQLITE_IOERR_MMAP, pFile->lastErrno,
36539                         "winSync1", pFile->zPath);
36540    }
36541  }
36542#endif
36543  rc = osFlushFileBuffers(pFile->h);
36544  SimulateIOError( rc=FALSE );
36545  if( rc ){
36546    OSTRACE(("SYNC pid=%lu, pFile=%p, file=%p, rc=SQLITE_OK\n",
36547             osGetCurrentProcessId(), pFile, pFile->h));
36548    return SQLITE_OK;
36549  }else{
36550    pFile->lastErrno = osGetLastError();
36551    OSTRACE(("SYNC pid=%lu, pFile=%p, file=%p, rc=SQLITE_IOERR_FSYNC\n",
36552             osGetCurrentProcessId(), pFile, pFile->h));
36553    return winLogError(SQLITE_IOERR_FSYNC, pFile->lastErrno,
36554                       "winSync2", pFile->zPath);
36555  }
36556#endif
36557}
36558
36559/*
36560** Determine the current size of a file in bytes
36561*/
36562static int winFileSize(sqlite3_file *id, sqlite3_int64 *pSize){
36563  winFile *pFile = (winFile*)id;
36564  int rc = SQLITE_OK;
36565
36566  assert( id!=0 );
36567  assert( pSize!=0 );
36568  SimulateIOError(return SQLITE_IOERR_FSTAT);
36569  OSTRACE(("SIZE file=%p, pSize=%p\n", pFile->h, pSize));
36570
36571#if SQLITE_OS_WINRT
36572  {
36573    FILE_STANDARD_INFO info;
36574    if( osGetFileInformationByHandleEx(pFile->h, FileStandardInfo,
36575                                     &info, sizeof(info)) ){
36576      *pSize = info.EndOfFile.QuadPart;
36577    }else{
36578      pFile->lastErrno = osGetLastError();
36579      rc = winLogError(SQLITE_IOERR_FSTAT, pFile->lastErrno,
36580                       "winFileSize", pFile->zPath);
36581    }
36582  }
36583#else
36584  {
36585    DWORD upperBits;
36586    DWORD lowerBits;
36587    DWORD lastErrno;
36588
36589    lowerBits = osGetFileSize(pFile->h, &upperBits);
36590    *pSize = (((sqlite3_int64)upperBits)<<32) + lowerBits;
36591    if(   (lowerBits == INVALID_FILE_SIZE)
36592       && ((lastErrno = osGetLastError())!=NO_ERROR) ){
36593      pFile->lastErrno = lastErrno;
36594      rc = winLogError(SQLITE_IOERR_FSTAT, pFile->lastErrno,
36595                       "winFileSize", pFile->zPath);
36596    }
36597  }
36598#endif
36599  OSTRACE(("SIZE file=%p, pSize=%p, *pSize=%lld, rc=%s\n",
36600           pFile->h, pSize, *pSize, sqlite3ErrName(rc)));
36601  return rc;
36602}
36603
36604/*
36605** LOCKFILE_FAIL_IMMEDIATELY is undefined on some Windows systems.
36606*/
36607#ifndef LOCKFILE_FAIL_IMMEDIATELY
36608# define LOCKFILE_FAIL_IMMEDIATELY 1
36609#endif
36610
36611#ifndef LOCKFILE_EXCLUSIVE_LOCK
36612# define LOCKFILE_EXCLUSIVE_LOCK 2
36613#endif
36614
36615/*
36616** Historically, SQLite has used both the LockFile and LockFileEx functions.
36617** When the LockFile function was used, it was always expected to fail
36618** immediately if the lock could not be obtained.  Also, it always expected to
36619** obtain an exclusive lock.  These flags are used with the LockFileEx function
36620** and reflect those expectations; therefore, they should not be changed.
36621*/
36622#ifndef SQLITE_LOCKFILE_FLAGS
36623# define SQLITE_LOCKFILE_FLAGS   (LOCKFILE_FAIL_IMMEDIATELY | \
36624                                  LOCKFILE_EXCLUSIVE_LOCK)
36625#endif
36626
36627/*
36628** Currently, SQLite never calls the LockFileEx function without wanting the
36629** call to fail immediately if the lock cannot be obtained.
36630*/
36631#ifndef SQLITE_LOCKFILEEX_FLAGS
36632# define SQLITE_LOCKFILEEX_FLAGS (LOCKFILE_FAIL_IMMEDIATELY)
36633#endif
36634
36635/*
36636** Acquire a reader lock.
36637** Different API routines are called depending on whether or not this
36638** is Win9x or WinNT.
36639*/
36640static int winGetReadLock(winFile *pFile){
36641  int res;
36642  OSTRACE(("READ-LOCK file=%p, lock=%d\n", pFile->h, pFile->locktype));
36643  if( osIsNT() ){
36644#if SQLITE_OS_WINCE
36645    /*
36646    ** NOTE: Windows CE is handled differently here due its lack of the Win32
36647    **       API LockFileEx.
36648    */
36649    res = winceLockFile(&pFile->h, SHARED_FIRST, 0, 1, 0);
36650#else
36651    res = winLockFile(&pFile->h, SQLITE_LOCKFILEEX_FLAGS, SHARED_FIRST, 0,
36652                      SHARED_SIZE, 0);
36653#endif
36654  }
36655#ifdef SQLITE_WIN32_HAS_ANSI
36656  else{
36657    int lk;
36658    sqlite3_randomness(sizeof(lk), &lk);
36659    pFile->sharedLockByte = (short)((lk & 0x7fffffff)%(SHARED_SIZE - 1));
36660    res = winLockFile(&pFile->h, SQLITE_LOCKFILE_FLAGS,
36661                      SHARED_FIRST+pFile->sharedLockByte, 0, 1, 0);
36662  }
36663#endif
36664  if( res == 0 ){
36665    pFile->lastErrno = osGetLastError();
36666    /* No need to log a failure to lock */
36667  }
36668  OSTRACE(("READ-LOCK file=%p, result=%d\n", pFile->h, res));
36669  return res;
36670}
36671
36672/*
36673** Undo a readlock
36674*/
36675static int winUnlockReadLock(winFile *pFile){
36676  int res;
36677  DWORD lastErrno;
36678  OSTRACE(("READ-UNLOCK file=%p, lock=%d\n", pFile->h, pFile->locktype));
36679  if( osIsNT() ){
36680    res = winUnlockFile(&pFile->h, SHARED_FIRST, 0, SHARED_SIZE, 0);
36681  }
36682#ifdef SQLITE_WIN32_HAS_ANSI
36683  else{
36684    res = winUnlockFile(&pFile->h, SHARED_FIRST+pFile->sharedLockByte, 0, 1, 0);
36685  }
36686#endif
36687  if( res==0 && ((lastErrno = osGetLastError())!=ERROR_NOT_LOCKED) ){
36688    pFile->lastErrno = lastErrno;
36689    winLogError(SQLITE_IOERR_UNLOCK, pFile->lastErrno,
36690                "winUnlockReadLock", pFile->zPath);
36691  }
36692  OSTRACE(("READ-UNLOCK file=%p, result=%d\n", pFile->h, res));
36693  return res;
36694}
36695
36696/*
36697** Lock the file with the lock specified by parameter locktype - one
36698** of the following:
36699**
36700**     (1) SHARED_LOCK
36701**     (2) RESERVED_LOCK
36702**     (3) PENDING_LOCK
36703**     (4) EXCLUSIVE_LOCK
36704**
36705** Sometimes when requesting one lock state, additional lock states
36706** are inserted in between.  The locking might fail on one of the later
36707** transitions leaving the lock state different from what it started but
36708** still short of its goal.  The following chart shows the allowed
36709** transitions and the inserted intermediate states:
36710**
36711**    UNLOCKED -> SHARED
36712**    SHARED -> RESERVED
36713**    SHARED -> (PENDING) -> EXCLUSIVE
36714**    RESERVED -> (PENDING) -> EXCLUSIVE
36715**    PENDING -> EXCLUSIVE
36716**
36717** This routine will only increase a lock.  The winUnlock() routine
36718** erases all locks at once and returns us immediately to locking level 0.
36719** It is not possible to lower the locking level one step at a time.  You
36720** must go straight to locking level 0.
36721*/
36722static int winLock(sqlite3_file *id, int locktype){
36723  int rc = SQLITE_OK;    /* Return code from subroutines */
36724  int res = 1;           /* Result of a Windows lock call */
36725  int newLocktype;       /* Set pFile->locktype to this value before exiting */
36726  int gotPendingLock = 0;/* True if we acquired a PENDING lock this time */
36727  winFile *pFile = (winFile*)id;
36728  DWORD lastErrno = NO_ERROR;
36729
36730  assert( id!=0 );
36731  OSTRACE(("LOCK file=%p, oldLock=%d(%d), newLock=%d\n",
36732           pFile->h, pFile->locktype, pFile->sharedLockByte, locktype));
36733
36734  /* If there is already a lock of this type or more restrictive on the
36735  ** OsFile, do nothing. Don't use the end_lock: exit path, as
36736  ** sqlite3OsEnterMutex() hasn't been called yet.
36737  */
36738  if( pFile->locktype>=locktype ){
36739    OSTRACE(("LOCK-HELD file=%p, rc=SQLITE_OK\n", pFile->h));
36740    return SQLITE_OK;
36741  }
36742
36743  /* Do not allow any kind of write-lock on a read-only database
36744  */
36745  if( (pFile->ctrlFlags & WINFILE_RDONLY)!=0 && locktype>=RESERVED_LOCK ){
36746    return SQLITE_IOERR_LOCK;
36747  }
36748
36749  /* Make sure the locking sequence is correct
36750  */
36751  assert( pFile->locktype!=NO_LOCK || locktype==SHARED_LOCK );
36752  assert( locktype!=PENDING_LOCK );
36753  assert( locktype!=RESERVED_LOCK || pFile->locktype==SHARED_LOCK );
36754
36755  /* Lock the PENDING_LOCK byte if we need to acquire a PENDING lock or
36756  ** a SHARED lock.  If we are acquiring a SHARED lock, the acquisition of
36757  ** the PENDING_LOCK byte is temporary.
36758  */
36759  newLocktype = pFile->locktype;
36760  if(   (pFile->locktype==NO_LOCK)
36761     || (   (locktype==EXCLUSIVE_LOCK)
36762         && (pFile->locktype==RESERVED_LOCK))
36763  ){
36764    int cnt = 3;
36765    while( cnt-->0 && (res = winLockFile(&pFile->h, SQLITE_LOCKFILE_FLAGS,
36766                                         PENDING_BYTE, 0, 1, 0))==0 ){
36767      /* Try 3 times to get the pending lock.  This is needed to work
36768      ** around problems caused by indexing and/or anti-virus software on
36769      ** Windows systems.
36770      ** If you are using this code as a model for alternative VFSes, do not
36771      ** copy this retry logic.  It is a hack intended for Windows only.
36772      */
36773      lastErrno = osGetLastError();
36774      OSTRACE(("LOCK-PENDING-FAIL file=%p, count=%d, result=%d\n",
36775               pFile->h, cnt, res));
36776      if( lastErrno==ERROR_INVALID_HANDLE ){
36777        pFile->lastErrno = lastErrno;
36778        rc = SQLITE_IOERR_LOCK;
36779        OSTRACE(("LOCK-FAIL file=%p, count=%d, rc=%s\n",
36780                 pFile->h, cnt, sqlite3ErrName(rc)));
36781        return rc;
36782      }
36783      if( cnt ) sqlite3_win32_sleep(1);
36784    }
36785    gotPendingLock = res;
36786    if( !res ){
36787      lastErrno = osGetLastError();
36788    }
36789  }
36790
36791  /* Acquire a shared lock
36792  */
36793  if( locktype==SHARED_LOCK && res ){
36794    assert( pFile->locktype==NO_LOCK );
36795    res = winGetReadLock(pFile);
36796    if( res ){
36797      newLocktype = SHARED_LOCK;
36798    }else{
36799      lastErrno = osGetLastError();
36800    }
36801  }
36802
36803  /* Acquire a RESERVED lock
36804  */
36805  if( locktype==RESERVED_LOCK && res ){
36806    assert( pFile->locktype==SHARED_LOCK );
36807    res = winLockFile(&pFile->h, SQLITE_LOCKFILE_FLAGS, RESERVED_BYTE, 0, 1, 0);
36808    if( res ){
36809      newLocktype = RESERVED_LOCK;
36810    }else{
36811      lastErrno = osGetLastError();
36812    }
36813  }
36814
36815  /* Acquire a PENDING lock
36816  */
36817  if( locktype==EXCLUSIVE_LOCK && res ){
36818    newLocktype = PENDING_LOCK;
36819    gotPendingLock = 0;
36820  }
36821
36822  /* Acquire an EXCLUSIVE lock
36823  */
36824  if( locktype==EXCLUSIVE_LOCK && res ){
36825    assert( pFile->locktype>=SHARED_LOCK );
36826    res = winUnlockReadLock(pFile);
36827    res = winLockFile(&pFile->h, SQLITE_LOCKFILE_FLAGS, SHARED_FIRST, 0,
36828                      SHARED_SIZE, 0);
36829    if( res ){
36830      newLocktype = EXCLUSIVE_LOCK;
36831    }else{
36832      lastErrno = osGetLastError();
36833      winGetReadLock(pFile);
36834    }
36835  }
36836
36837  /* If we are holding a PENDING lock that ought to be released, then
36838  ** release it now.
36839  */
36840  if( gotPendingLock && locktype==SHARED_LOCK ){
36841    winUnlockFile(&pFile->h, PENDING_BYTE, 0, 1, 0);
36842  }
36843
36844  /* Update the state of the lock has held in the file descriptor then
36845  ** return the appropriate result code.
36846  */
36847  if( res ){
36848    rc = SQLITE_OK;
36849  }else{
36850    pFile->lastErrno = lastErrno;
36851    rc = SQLITE_BUSY;
36852    OSTRACE(("LOCK-FAIL file=%p, wanted=%d, got=%d\n",
36853             pFile->h, locktype, newLocktype));
36854  }
36855  pFile->locktype = (u8)newLocktype;
36856  OSTRACE(("LOCK file=%p, lock=%d, rc=%s\n",
36857           pFile->h, pFile->locktype, sqlite3ErrName(rc)));
36858  return rc;
36859}
36860
36861/*
36862** This routine checks if there is a RESERVED lock held on the specified
36863** file by this or any other process. If such a lock is held, return
36864** non-zero, otherwise zero.
36865*/
36866static int winCheckReservedLock(sqlite3_file *id, int *pResOut){
36867  int res;
36868  winFile *pFile = (winFile*)id;
36869
36870  SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; );
36871  OSTRACE(("TEST-WR-LOCK file=%p, pResOut=%p\n", pFile->h, pResOut));
36872
36873  assert( id!=0 );
36874  if( pFile->locktype>=RESERVED_LOCK ){
36875    res = 1;
36876    OSTRACE(("TEST-WR-LOCK file=%p, result=%d (local)\n", pFile->h, res));
36877  }else{
36878    res = winLockFile(&pFile->h, SQLITE_LOCKFILEEX_FLAGS,RESERVED_BYTE, 0, 1, 0);
36879    if( res ){
36880      winUnlockFile(&pFile->h, RESERVED_BYTE, 0, 1, 0);
36881    }
36882    res = !res;
36883    OSTRACE(("TEST-WR-LOCK file=%p, result=%d (remote)\n", pFile->h, res));
36884  }
36885  *pResOut = res;
36886  OSTRACE(("TEST-WR-LOCK file=%p, pResOut=%p, *pResOut=%d, rc=SQLITE_OK\n",
36887           pFile->h, pResOut, *pResOut));
36888  return SQLITE_OK;
36889}
36890
36891/*
36892** Lower the locking level on file descriptor id to locktype.  locktype
36893** must be either NO_LOCK or SHARED_LOCK.
36894**
36895** If the locking level of the file descriptor is already at or below
36896** the requested locking level, this routine is a no-op.
36897**
36898** It is not possible for this routine to fail if the second argument
36899** is NO_LOCK.  If the second argument is SHARED_LOCK then this routine
36900** might return SQLITE_IOERR;
36901*/
36902static int winUnlock(sqlite3_file *id, int locktype){
36903  int type;
36904  winFile *pFile = (winFile*)id;
36905  int rc = SQLITE_OK;
36906  assert( pFile!=0 );
36907  assert( locktype<=SHARED_LOCK );
36908  OSTRACE(("UNLOCK file=%p, oldLock=%d(%d), newLock=%d\n",
36909           pFile->h, pFile->locktype, pFile->sharedLockByte, locktype));
36910  type = pFile->locktype;
36911  if( type>=EXCLUSIVE_LOCK ){
36912    winUnlockFile(&pFile->h, SHARED_FIRST, 0, SHARED_SIZE, 0);
36913    if( locktype==SHARED_LOCK && !winGetReadLock(pFile) ){
36914      /* This should never happen.  We should always be able to
36915      ** reacquire the read lock */
36916      rc = winLogError(SQLITE_IOERR_UNLOCK, osGetLastError(),
36917                       "winUnlock", pFile->zPath);
36918    }
36919  }
36920  if( type>=RESERVED_LOCK ){
36921    winUnlockFile(&pFile->h, RESERVED_BYTE, 0, 1, 0);
36922  }
36923  if( locktype==NO_LOCK && type>=SHARED_LOCK ){
36924    winUnlockReadLock(pFile);
36925  }
36926  if( type>=PENDING_LOCK ){
36927    winUnlockFile(&pFile->h, PENDING_BYTE, 0, 1, 0);
36928  }
36929  pFile->locktype = (u8)locktype;
36930  OSTRACE(("UNLOCK file=%p, lock=%d, rc=%s\n",
36931           pFile->h, pFile->locktype, sqlite3ErrName(rc)));
36932  return rc;
36933}
36934
36935/*
36936** If *pArg is initially negative then this is a query.  Set *pArg to
36937** 1 or 0 depending on whether or not bit mask of pFile->ctrlFlags is set.
36938**
36939** If *pArg is 0 or 1, then clear or set the mask bit of pFile->ctrlFlags.
36940*/
36941static void winModeBit(winFile *pFile, unsigned char mask, int *pArg){
36942  if( *pArg<0 ){
36943    *pArg = (pFile->ctrlFlags & mask)!=0;
36944  }else if( (*pArg)==0 ){
36945    pFile->ctrlFlags &= ~mask;
36946  }else{
36947    pFile->ctrlFlags |= mask;
36948  }
36949}
36950
36951/* Forward references to VFS helper methods used for temporary files */
36952static int winGetTempname(sqlite3_vfs *, char **);
36953static int winIsDir(const void *);
36954static BOOL winIsDriveLetterAndColon(const char *);
36955
36956/*
36957** Control and query of the open file handle.
36958*/
36959static int winFileControl(sqlite3_file *id, int op, void *pArg){
36960  winFile *pFile = (winFile*)id;
36961  OSTRACE(("FCNTL file=%p, op=%d, pArg=%p\n", pFile->h, op, pArg));
36962  switch( op ){
36963    case SQLITE_FCNTL_LOCKSTATE: {
36964      *(int*)pArg = pFile->locktype;
36965      OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
36966      return SQLITE_OK;
36967    }
36968    case SQLITE_LAST_ERRNO: {
36969      *(int*)pArg = (int)pFile->lastErrno;
36970      OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
36971      return SQLITE_OK;
36972    }
36973    case SQLITE_FCNTL_CHUNK_SIZE: {
36974      pFile->szChunk = *(int *)pArg;
36975      OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
36976      return SQLITE_OK;
36977    }
36978    case SQLITE_FCNTL_SIZE_HINT: {
36979      if( pFile->szChunk>0 ){
36980        sqlite3_int64 oldSz;
36981        int rc = winFileSize(id, &oldSz);
36982        if( rc==SQLITE_OK ){
36983          sqlite3_int64 newSz = *(sqlite3_int64*)pArg;
36984          if( newSz>oldSz ){
36985            SimulateIOErrorBenign(1);
36986            rc = winTruncate(id, newSz);
36987            SimulateIOErrorBenign(0);
36988          }
36989        }
36990        OSTRACE(("FCNTL file=%p, rc=%s\n", pFile->h, sqlite3ErrName(rc)));
36991        return rc;
36992      }
36993      OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
36994      return SQLITE_OK;
36995    }
36996    case SQLITE_FCNTL_PERSIST_WAL: {
36997      winModeBit(pFile, WINFILE_PERSIST_WAL, (int*)pArg);
36998      OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
36999      return SQLITE_OK;
37000    }
37001    case SQLITE_FCNTL_POWERSAFE_OVERWRITE: {
37002      winModeBit(pFile, WINFILE_PSOW, (int*)pArg);
37003      OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
37004      return SQLITE_OK;
37005    }
37006    case SQLITE_FCNTL_VFSNAME: {
37007      *(char**)pArg = sqlite3_mprintf("%s", pFile->pVfs->zName);
37008      OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
37009      return SQLITE_OK;
37010    }
37011    case SQLITE_FCNTL_WIN32_AV_RETRY: {
37012      int *a = (int*)pArg;
37013      if( a[0]>0 ){
37014        winIoerrRetry = a[0];
37015      }else{
37016        a[0] = winIoerrRetry;
37017      }
37018      if( a[1]>0 ){
37019        winIoerrRetryDelay = a[1];
37020      }else{
37021        a[1] = winIoerrRetryDelay;
37022      }
37023      OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
37024      return SQLITE_OK;
37025    }
37026#ifdef SQLITE_TEST
37027    case SQLITE_FCNTL_WIN32_SET_HANDLE: {
37028      LPHANDLE phFile = (LPHANDLE)pArg;
37029      HANDLE hOldFile = pFile->h;
37030      pFile->h = *phFile;
37031      *phFile = hOldFile;
37032      OSTRACE(("FCNTL oldFile=%p, newFile=%p, rc=SQLITE_OK\n",
37033               hOldFile, pFile->h));
37034      return SQLITE_OK;
37035    }
37036#endif
37037    case SQLITE_FCNTL_TEMPFILENAME: {
37038      char *zTFile = 0;
37039      int rc = winGetTempname(pFile->pVfs, &zTFile);
37040      if( rc==SQLITE_OK ){
37041        *(char**)pArg = zTFile;
37042      }
37043      OSTRACE(("FCNTL file=%p, rc=%s\n", pFile->h, sqlite3ErrName(rc)));
37044      return rc;
37045    }
37046#if SQLITE_MAX_MMAP_SIZE>0
37047    case SQLITE_FCNTL_MMAP_SIZE: {
37048      i64 newLimit = *(i64*)pArg;
37049      int rc = SQLITE_OK;
37050      if( newLimit>sqlite3GlobalConfig.mxMmap ){
37051        newLimit = sqlite3GlobalConfig.mxMmap;
37052      }
37053      *(i64*)pArg = pFile->mmapSizeMax;
37054      if( newLimit>=0 && newLimit!=pFile->mmapSizeMax && pFile->nFetchOut==0 ){
37055        pFile->mmapSizeMax = newLimit;
37056        if( pFile->mmapSize>0 ){
37057          winUnmapfile(pFile);
37058          rc = winMapfile(pFile, -1);
37059        }
37060      }
37061      OSTRACE(("FCNTL file=%p, rc=%s\n", pFile->h, sqlite3ErrName(rc)));
37062      return rc;
37063    }
37064#endif
37065  }
37066  OSTRACE(("FCNTL file=%p, rc=SQLITE_NOTFOUND\n", pFile->h));
37067  return SQLITE_NOTFOUND;
37068}
37069
37070/*
37071** Return the sector size in bytes of the underlying block device for
37072** the specified file. This is almost always 512 bytes, but may be
37073** larger for some devices.
37074**
37075** SQLite code assumes this function cannot fail. It also assumes that
37076** if two files are created in the same file-system directory (i.e.
37077** a database and its journal file) that the sector size will be the
37078** same for both.
37079*/
37080static int winSectorSize(sqlite3_file *id){
37081  (void)id;
37082  return SQLITE_DEFAULT_SECTOR_SIZE;
37083}
37084
37085/*
37086** Return a vector of device characteristics.
37087*/
37088static int winDeviceCharacteristics(sqlite3_file *id){
37089  winFile *p = (winFile*)id;
37090  return SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN |
37091         ((p->ctrlFlags & WINFILE_PSOW)?SQLITE_IOCAP_POWERSAFE_OVERWRITE:0);
37092}
37093
37094/*
37095** Windows will only let you create file view mappings
37096** on allocation size granularity boundaries.
37097** During sqlite3_os_init() we do a GetSystemInfo()
37098** to get the granularity size.
37099*/
37100static SYSTEM_INFO winSysInfo;
37101
37102#ifndef SQLITE_OMIT_WAL
37103
37104/*
37105** Helper functions to obtain and relinquish the global mutex. The
37106** global mutex is used to protect the winLockInfo objects used by
37107** this file, all of which may be shared by multiple threads.
37108**
37109** Function winShmMutexHeld() is used to assert() that the global mutex
37110** is held when required. This function is only used as part of assert()
37111** statements. e.g.
37112**
37113**   winShmEnterMutex()
37114**     assert( winShmMutexHeld() );
37115**   winShmLeaveMutex()
37116*/
37117static void winShmEnterMutex(void){
37118  sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_VFS1));
37119}
37120static void winShmLeaveMutex(void){
37121  sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_VFS1));
37122}
37123#ifndef NDEBUG
37124static int winShmMutexHeld(void) {
37125  return sqlite3_mutex_held(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_VFS1));
37126}
37127#endif
37128
37129/*
37130** Object used to represent a single file opened and mmapped to provide
37131** shared memory.  When multiple threads all reference the same
37132** log-summary, each thread has its own winFile object, but they all
37133** point to a single instance of this object.  In other words, each
37134** log-summary is opened only once per process.
37135**
37136** winShmMutexHeld() must be true when creating or destroying
37137** this object or while reading or writing the following fields:
37138**
37139**      nRef
37140**      pNext
37141**
37142** The following fields are read-only after the object is created:
37143**
37144**      fid
37145**      zFilename
37146**
37147** Either winShmNode.mutex must be held or winShmNode.nRef==0 and
37148** winShmMutexHeld() is true when reading or writing any other field
37149** in this structure.
37150**
37151*/
37152struct winShmNode {
37153  sqlite3_mutex *mutex;      /* Mutex to access this object */
37154  char *zFilename;           /* Name of the file */
37155  winFile hFile;             /* File handle from winOpen */
37156
37157  int szRegion;              /* Size of shared-memory regions */
37158  int nRegion;               /* Size of array apRegion */
37159  struct ShmRegion {
37160    HANDLE hMap;             /* File handle from CreateFileMapping */
37161    void *pMap;
37162  } *aRegion;
37163  DWORD lastErrno;           /* The Windows errno from the last I/O error */
37164
37165  int nRef;                  /* Number of winShm objects pointing to this */
37166  winShm *pFirst;            /* All winShm objects pointing to this */
37167  winShmNode *pNext;         /* Next in list of all winShmNode objects */
37168#if defined(SQLITE_DEBUG) || defined(SQLITE_HAVE_OS_TRACE)
37169  u8 nextShmId;              /* Next available winShm.id value */
37170#endif
37171};
37172
37173/*
37174** A global array of all winShmNode objects.
37175**
37176** The winShmMutexHeld() must be true while reading or writing this list.
37177*/
37178static winShmNode *winShmNodeList = 0;
37179
37180/*
37181** Structure used internally by this VFS to record the state of an
37182** open shared memory connection.
37183**
37184** The following fields are initialized when this object is created and
37185** are read-only thereafter:
37186**
37187**    winShm.pShmNode
37188**    winShm.id
37189**
37190** All other fields are read/write.  The winShm.pShmNode->mutex must be held
37191** while accessing any read/write fields.
37192*/
37193struct winShm {
37194  winShmNode *pShmNode;      /* The underlying winShmNode object */
37195  winShm *pNext;             /* Next winShm with the same winShmNode */
37196  u8 hasMutex;               /* True if holding the winShmNode mutex */
37197  u16 sharedMask;            /* Mask of shared locks held */
37198  u16 exclMask;              /* Mask of exclusive locks held */
37199#if defined(SQLITE_DEBUG) || defined(SQLITE_HAVE_OS_TRACE)
37200  u8 id;                     /* Id of this connection with its winShmNode */
37201#endif
37202};
37203
37204/*
37205** Constants used for locking
37206*/
37207#define WIN_SHM_BASE   ((22+SQLITE_SHM_NLOCK)*4)        /* first lock byte */
37208#define WIN_SHM_DMS    (WIN_SHM_BASE+SQLITE_SHM_NLOCK)  /* deadman switch */
37209
37210/*
37211** Apply advisory locks for all n bytes beginning at ofst.
37212*/
37213#define _SHM_UNLCK  1
37214#define _SHM_RDLCK  2
37215#define _SHM_WRLCK  3
37216static int winShmSystemLock(
37217  winShmNode *pFile,    /* Apply locks to this open shared-memory segment */
37218  int lockType,         /* _SHM_UNLCK, _SHM_RDLCK, or _SHM_WRLCK */
37219  int ofst,             /* Offset to first byte to be locked/unlocked */
37220  int nByte             /* Number of bytes to lock or unlock */
37221){
37222  int rc = 0;           /* Result code form Lock/UnlockFileEx() */
37223
37224  /* Access to the winShmNode object is serialized by the caller */
37225  assert( sqlite3_mutex_held(pFile->mutex) || pFile->nRef==0 );
37226
37227  OSTRACE(("SHM-LOCK file=%p, lock=%d, offset=%d, size=%d\n",
37228           pFile->hFile.h, lockType, ofst, nByte));
37229
37230  /* Release/Acquire the system-level lock */
37231  if( lockType==_SHM_UNLCK ){
37232    rc = winUnlockFile(&pFile->hFile.h, ofst, 0, nByte, 0);
37233  }else{
37234    /* Initialize the locking parameters */
37235    DWORD dwFlags = LOCKFILE_FAIL_IMMEDIATELY;
37236    if( lockType == _SHM_WRLCK ) dwFlags |= LOCKFILE_EXCLUSIVE_LOCK;
37237    rc = winLockFile(&pFile->hFile.h, dwFlags, ofst, 0, nByte, 0);
37238  }
37239
37240  if( rc!= 0 ){
37241    rc = SQLITE_OK;
37242  }else{
37243    pFile->lastErrno =  osGetLastError();
37244    rc = SQLITE_BUSY;
37245  }
37246
37247  OSTRACE(("SHM-LOCK file=%p, func=%s, errno=%lu, rc=%s\n",
37248           pFile->hFile.h, (lockType == _SHM_UNLCK) ? "winUnlockFile" :
37249           "winLockFile", pFile->lastErrno, sqlite3ErrName(rc)));
37250
37251  return rc;
37252}
37253
37254/* Forward references to VFS methods */
37255static int winOpen(sqlite3_vfs*,const char*,sqlite3_file*,int,int*);
37256static int winDelete(sqlite3_vfs *,const char*,int);
37257
37258/*
37259** Purge the winShmNodeList list of all entries with winShmNode.nRef==0.
37260**
37261** This is not a VFS shared-memory method; it is a utility function called
37262** by VFS shared-memory methods.
37263*/
37264static void winShmPurge(sqlite3_vfs *pVfs, int deleteFlag){
37265  winShmNode **pp;
37266  winShmNode *p;
37267  assert( winShmMutexHeld() );
37268  OSTRACE(("SHM-PURGE pid=%lu, deleteFlag=%d\n",
37269           osGetCurrentProcessId(), deleteFlag));
37270  pp = &winShmNodeList;
37271  while( (p = *pp)!=0 ){
37272    if( p->nRef==0 ){
37273      int i;
37274      if( p->mutex ){ sqlite3_mutex_free(p->mutex); }
37275      for(i=0; i<p->nRegion; i++){
37276        BOOL bRc = osUnmapViewOfFile(p->aRegion[i].pMap);
37277        OSTRACE(("SHM-PURGE-UNMAP pid=%lu, region=%d, rc=%s\n",
37278                 osGetCurrentProcessId(), i, bRc ? "ok" : "failed"));
37279        UNUSED_VARIABLE_VALUE(bRc);
37280        bRc = osCloseHandle(p->aRegion[i].hMap);
37281        OSTRACE(("SHM-PURGE-CLOSE pid=%lu, region=%d, rc=%s\n",
37282                 osGetCurrentProcessId(), i, bRc ? "ok" : "failed"));
37283        UNUSED_VARIABLE_VALUE(bRc);
37284      }
37285      if( p->hFile.h!=NULL && p->hFile.h!=INVALID_HANDLE_VALUE ){
37286        SimulateIOErrorBenign(1);
37287        winClose((sqlite3_file *)&p->hFile);
37288        SimulateIOErrorBenign(0);
37289      }
37290      if( deleteFlag ){
37291        SimulateIOErrorBenign(1);
37292        sqlite3BeginBenignMalloc();
37293        winDelete(pVfs, p->zFilename, 0);
37294        sqlite3EndBenignMalloc();
37295        SimulateIOErrorBenign(0);
37296      }
37297      *pp = p->pNext;
37298      sqlite3_free(p->aRegion);
37299      sqlite3_free(p);
37300    }else{
37301      pp = &p->pNext;
37302    }
37303  }
37304}
37305
37306/*
37307** Open the shared-memory area associated with database file pDbFd.
37308**
37309** When opening a new shared-memory file, if no other instances of that
37310** file are currently open, in this process or in other processes, then
37311** the file must be truncated to zero length or have its header cleared.
37312*/
37313static int winOpenSharedMemory(winFile *pDbFd){
37314  struct winShm *p;                  /* The connection to be opened */
37315  struct winShmNode *pShmNode = 0;   /* The underlying mmapped file */
37316  int rc;                            /* Result code */
37317  struct winShmNode *pNew;           /* Newly allocated winShmNode */
37318  int nName;                         /* Size of zName in bytes */
37319
37320  assert( pDbFd->pShm==0 );    /* Not previously opened */
37321
37322  /* Allocate space for the new sqlite3_shm object.  Also speculatively
37323  ** allocate space for a new winShmNode and filename.
37324  */
37325  p = sqlite3MallocZero( sizeof(*p) );
37326  if( p==0 ) return SQLITE_IOERR_NOMEM;
37327  nName = sqlite3Strlen30(pDbFd->zPath);
37328  pNew = sqlite3MallocZero( sizeof(*pShmNode) + nName + 17 );
37329  if( pNew==0 ){
37330    sqlite3_free(p);
37331    return SQLITE_IOERR_NOMEM;
37332  }
37333  pNew->zFilename = (char*)&pNew[1];
37334  sqlite3_snprintf(nName+15, pNew->zFilename, "%s-shm", pDbFd->zPath);
37335  sqlite3FileSuffix3(pDbFd->zPath, pNew->zFilename);
37336
37337  /* Look to see if there is an existing winShmNode that can be used.
37338  ** If no matching winShmNode currently exists, create a new one.
37339  */
37340  winShmEnterMutex();
37341  for(pShmNode = winShmNodeList; pShmNode; pShmNode=pShmNode->pNext){
37342    /* TBD need to come up with better match here.  Perhaps
37343    ** use FILE_ID_BOTH_DIR_INFO Structure.
37344    */
37345    if( sqlite3StrICmp(pShmNode->zFilename, pNew->zFilename)==0 ) break;
37346  }
37347  if( pShmNode ){
37348    sqlite3_free(pNew);
37349  }else{
37350    pShmNode = pNew;
37351    pNew = 0;
37352    ((winFile*)(&pShmNode->hFile))->h = INVALID_HANDLE_VALUE;
37353    pShmNode->pNext = winShmNodeList;
37354    winShmNodeList = pShmNode;
37355
37356    pShmNode->mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST);
37357    if( pShmNode->mutex==0 ){
37358      rc = SQLITE_IOERR_NOMEM;
37359      goto shm_open_err;
37360    }
37361
37362    rc = winOpen(pDbFd->pVfs,
37363                 pShmNode->zFilename,             /* Name of the file (UTF-8) */
37364                 (sqlite3_file*)&pShmNode->hFile,  /* File handle here */
37365                 SQLITE_OPEN_WAL | SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE,
37366                 0);
37367    if( SQLITE_OK!=rc ){
37368      goto shm_open_err;
37369    }
37370
37371    /* Check to see if another process is holding the dead-man switch.
37372    ** If not, truncate the file to zero length.
37373    */
37374    if( winShmSystemLock(pShmNode, _SHM_WRLCK, WIN_SHM_DMS, 1)==SQLITE_OK ){
37375      rc = winTruncate((sqlite3_file *)&pShmNode->hFile, 0);
37376      if( rc!=SQLITE_OK ){
37377        rc = winLogError(SQLITE_IOERR_SHMOPEN, osGetLastError(),
37378                         "winOpenShm", pDbFd->zPath);
37379      }
37380    }
37381    if( rc==SQLITE_OK ){
37382      winShmSystemLock(pShmNode, _SHM_UNLCK, WIN_SHM_DMS, 1);
37383      rc = winShmSystemLock(pShmNode, _SHM_RDLCK, WIN_SHM_DMS, 1);
37384    }
37385    if( rc ) goto shm_open_err;
37386  }
37387
37388  /* Make the new connection a child of the winShmNode */
37389  p->pShmNode = pShmNode;
37390#if defined(SQLITE_DEBUG) || defined(SQLITE_HAVE_OS_TRACE)
37391  p->id = pShmNode->nextShmId++;
37392#endif
37393  pShmNode->nRef++;
37394  pDbFd->pShm = p;
37395  winShmLeaveMutex();
37396
37397  /* The reference count on pShmNode has already been incremented under
37398  ** the cover of the winShmEnterMutex() mutex and the pointer from the
37399  ** new (struct winShm) object to the pShmNode has been set. All that is
37400  ** left to do is to link the new object into the linked list starting
37401  ** at pShmNode->pFirst. This must be done while holding the pShmNode->mutex
37402  ** mutex.
37403  */
37404  sqlite3_mutex_enter(pShmNode->mutex);
37405  p->pNext = pShmNode->pFirst;
37406  pShmNode->pFirst = p;
37407  sqlite3_mutex_leave(pShmNode->mutex);
37408  return SQLITE_OK;
37409
37410  /* Jump here on any error */
37411shm_open_err:
37412  winShmSystemLock(pShmNode, _SHM_UNLCK, WIN_SHM_DMS, 1);
37413  winShmPurge(pDbFd->pVfs, 0);      /* This call frees pShmNode if required */
37414  sqlite3_free(p);
37415  sqlite3_free(pNew);
37416  winShmLeaveMutex();
37417  return rc;
37418}
37419
37420/*
37421** Close a connection to shared-memory.  Delete the underlying
37422** storage if deleteFlag is true.
37423*/
37424static int winShmUnmap(
37425  sqlite3_file *fd,          /* Database holding shared memory */
37426  int deleteFlag             /* Delete after closing if true */
37427){
37428  winFile *pDbFd;       /* Database holding shared-memory */
37429  winShm *p;            /* The connection to be closed */
37430  winShmNode *pShmNode; /* The underlying shared-memory file */
37431  winShm **pp;          /* For looping over sibling connections */
37432
37433  pDbFd = (winFile*)fd;
37434  p = pDbFd->pShm;
37435  if( p==0 ) return SQLITE_OK;
37436  pShmNode = p->pShmNode;
37437
37438  /* Remove connection p from the set of connections associated
37439  ** with pShmNode */
37440  sqlite3_mutex_enter(pShmNode->mutex);
37441  for(pp=&pShmNode->pFirst; (*pp)!=p; pp = &(*pp)->pNext){}
37442  *pp = p->pNext;
37443
37444  /* Free the connection p */
37445  sqlite3_free(p);
37446  pDbFd->pShm = 0;
37447  sqlite3_mutex_leave(pShmNode->mutex);
37448
37449  /* If pShmNode->nRef has reached 0, then close the underlying
37450  ** shared-memory file, too */
37451  winShmEnterMutex();
37452  assert( pShmNode->nRef>0 );
37453  pShmNode->nRef--;
37454  if( pShmNode->nRef==0 ){
37455    winShmPurge(pDbFd->pVfs, deleteFlag);
37456  }
37457  winShmLeaveMutex();
37458
37459  return SQLITE_OK;
37460}
37461
37462/*
37463** Change the lock state for a shared-memory segment.
37464*/
37465static int winShmLock(
37466  sqlite3_file *fd,          /* Database file holding the shared memory */
37467  int ofst,                  /* First lock to acquire or release */
37468  int n,                     /* Number of locks to acquire or release */
37469  int flags                  /* What to do with the lock */
37470){
37471  winFile *pDbFd = (winFile*)fd;        /* Connection holding shared memory */
37472  winShm *p = pDbFd->pShm;              /* The shared memory being locked */
37473  winShm *pX;                           /* For looping over all siblings */
37474  winShmNode *pShmNode = p->pShmNode;
37475  int rc = SQLITE_OK;                   /* Result code */
37476  u16 mask;                             /* Mask of locks to take or release */
37477
37478  assert( ofst>=0 && ofst+n<=SQLITE_SHM_NLOCK );
37479  assert( n>=1 );
37480  assert( flags==(SQLITE_SHM_LOCK | SQLITE_SHM_SHARED)
37481       || flags==(SQLITE_SHM_LOCK | SQLITE_SHM_EXCLUSIVE)
37482       || flags==(SQLITE_SHM_UNLOCK | SQLITE_SHM_SHARED)
37483       || flags==(SQLITE_SHM_UNLOCK | SQLITE_SHM_EXCLUSIVE) );
37484  assert( n==1 || (flags & SQLITE_SHM_EXCLUSIVE)!=0 );
37485
37486  mask = (u16)((1U<<(ofst+n)) - (1U<<ofst));
37487  assert( n>1 || mask==(1<<ofst) );
37488  sqlite3_mutex_enter(pShmNode->mutex);
37489  if( flags & SQLITE_SHM_UNLOCK ){
37490    u16 allMask = 0; /* Mask of locks held by siblings */
37491
37492    /* See if any siblings hold this same lock */
37493    for(pX=pShmNode->pFirst; pX; pX=pX->pNext){
37494      if( pX==p ) continue;
37495      assert( (pX->exclMask & (p->exclMask|p->sharedMask))==0 );
37496      allMask |= pX->sharedMask;
37497    }
37498
37499    /* Unlock the system-level locks */
37500    if( (mask & allMask)==0 ){
37501      rc = winShmSystemLock(pShmNode, _SHM_UNLCK, ofst+WIN_SHM_BASE, n);
37502    }else{
37503      rc = SQLITE_OK;
37504    }
37505
37506    /* Undo the local locks */
37507    if( rc==SQLITE_OK ){
37508      p->exclMask &= ~mask;
37509      p->sharedMask &= ~mask;
37510    }
37511  }else if( flags & SQLITE_SHM_SHARED ){
37512    u16 allShared = 0;  /* Union of locks held by connections other than "p" */
37513
37514    /* Find out which shared locks are already held by sibling connections.
37515    ** If any sibling already holds an exclusive lock, go ahead and return
37516    ** SQLITE_BUSY.
37517    */
37518    for(pX=pShmNode->pFirst; pX; pX=pX->pNext){
37519      if( (pX->exclMask & mask)!=0 ){
37520        rc = SQLITE_BUSY;
37521        break;
37522      }
37523      allShared |= pX->sharedMask;
37524    }
37525
37526    /* Get shared locks at the system level, if necessary */
37527    if( rc==SQLITE_OK ){
37528      if( (allShared & mask)==0 ){
37529        rc = winShmSystemLock(pShmNode, _SHM_RDLCK, ofst+WIN_SHM_BASE, n);
37530      }else{
37531        rc = SQLITE_OK;
37532      }
37533    }
37534
37535    /* Get the local shared locks */
37536    if( rc==SQLITE_OK ){
37537      p->sharedMask |= mask;
37538    }
37539  }else{
37540    /* Make sure no sibling connections hold locks that will block this
37541    ** lock.  If any do, return SQLITE_BUSY right away.
37542    */
37543    for(pX=pShmNode->pFirst; pX; pX=pX->pNext){
37544      if( (pX->exclMask & mask)!=0 || (pX->sharedMask & mask)!=0 ){
37545        rc = SQLITE_BUSY;
37546        break;
37547      }
37548    }
37549
37550    /* Get the exclusive locks at the system level.  Then if successful
37551    ** also mark the local connection as being locked.
37552    */
37553    if( rc==SQLITE_OK ){
37554      rc = winShmSystemLock(pShmNode, _SHM_WRLCK, ofst+WIN_SHM_BASE, n);
37555      if( rc==SQLITE_OK ){
37556        assert( (p->sharedMask & mask)==0 );
37557        p->exclMask |= mask;
37558      }
37559    }
37560  }
37561  sqlite3_mutex_leave(pShmNode->mutex);
37562  OSTRACE(("SHM-LOCK pid=%lu, id=%d, sharedMask=%03x, exclMask=%03x, rc=%s\n",
37563           osGetCurrentProcessId(), p->id, p->sharedMask, p->exclMask,
37564           sqlite3ErrName(rc)));
37565  return rc;
37566}
37567
37568/*
37569** Implement a memory barrier or memory fence on shared memory.
37570**
37571** All loads and stores begun before the barrier must complete before
37572** any load or store begun after the barrier.
37573*/
37574static void winShmBarrier(
37575  sqlite3_file *fd          /* Database holding the shared memory */
37576){
37577  UNUSED_PARAMETER(fd);
37578  /* MemoryBarrier(); // does not work -- do not know why not */
37579  winShmEnterMutex();
37580  winShmLeaveMutex();
37581}
37582
37583/*
37584** This function is called to obtain a pointer to region iRegion of the
37585** shared-memory associated with the database file fd. Shared-memory regions
37586** are numbered starting from zero. Each shared-memory region is szRegion
37587** bytes in size.
37588**
37589** If an error occurs, an error code is returned and *pp is set to NULL.
37590**
37591** Otherwise, if the isWrite parameter is 0 and the requested shared-memory
37592** region has not been allocated (by any client, including one running in a
37593** separate process), then *pp is set to NULL and SQLITE_OK returned. If
37594** isWrite is non-zero and the requested shared-memory region has not yet
37595** been allocated, it is allocated by this function.
37596**
37597** If the shared-memory region has already been allocated or is allocated by
37598** this call as described above, then it is mapped into this processes
37599** address space (if it is not already), *pp is set to point to the mapped
37600** memory and SQLITE_OK returned.
37601*/
37602static int winShmMap(
37603  sqlite3_file *fd,               /* Handle open on database file */
37604  int iRegion,                    /* Region to retrieve */
37605  int szRegion,                   /* Size of regions */
37606  int isWrite,                    /* True to extend file if necessary */
37607  void volatile **pp              /* OUT: Mapped memory */
37608){
37609  winFile *pDbFd = (winFile*)fd;
37610  winShm *pShm = pDbFd->pShm;
37611  winShmNode *pShmNode;
37612  int rc = SQLITE_OK;
37613
37614  if( !pShm ){
37615    rc = winOpenSharedMemory(pDbFd);
37616    if( rc!=SQLITE_OK ) return rc;
37617    pShm = pDbFd->pShm;
37618  }
37619  pShmNode = pShm->pShmNode;
37620
37621  sqlite3_mutex_enter(pShmNode->mutex);
37622  assert( szRegion==pShmNode->szRegion || pShmNode->nRegion==0 );
37623
37624  if( pShmNode->nRegion<=iRegion ){
37625    struct ShmRegion *apNew;           /* New aRegion[] array */
37626    int nByte = (iRegion+1)*szRegion;  /* Minimum required file size */
37627    sqlite3_int64 sz;                  /* Current size of wal-index file */
37628
37629    pShmNode->szRegion = szRegion;
37630
37631    /* The requested region is not mapped into this processes address space.
37632    ** Check to see if it has been allocated (i.e. if the wal-index file is
37633    ** large enough to contain the requested region).
37634    */
37635    rc = winFileSize((sqlite3_file *)&pShmNode->hFile, &sz);
37636    if( rc!=SQLITE_OK ){
37637      rc = winLogError(SQLITE_IOERR_SHMSIZE, osGetLastError(),
37638                       "winShmMap1", pDbFd->zPath);
37639      goto shmpage_out;
37640    }
37641
37642    if( sz<nByte ){
37643      /* The requested memory region does not exist. If isWrite is set to
37644      ** zero, exit early. *pp will be set to NULL and SQLITE_OK returned.
37645      **
37646      ** Alternatively, if isWrite is non-zero, use ftruncate() to allocate
37647      ** the requested memory region.
37648      */
37649      if( !isWrite ) goto shmpage_out;
37650      rc = winTruncate((sqlite3_file *)&pShmNode->hFile, nByte);
37651      if( rc!=SQLITE_OK ){
37652        rc = winLogError(SQLITE_IOERR_SHMSIZE, osGetLastError(),
37653                         "winShmMap2", pDbFd->zPath);
37654        goto shmpage_out;
37655      }
37656    }
37657
37658    /* Map the requested memory region into this processes address space. */
37659    apNew = (struct ShmRegion *)sqlite3_realloc64(
37660        pShmNode->aRegion, (iRegion+1)*sizeof(apNew[0])
37661    );
37662    if( !apNew ){
37663      rc = SQLITE_IOERR_NOMEM;
37664      goto shmpage_out;
37665    }
37666    pShmNode->aRegion = apNew;
37667
37668    while( pShmNode->nRegion<=iRegion ){
37669      HANDLE hMap = NULL;         /* file-mapping handle */
37670      void *pMap = 0;             /* Mapped memory region */
37671
37672#if SQLITE_OS_WINRT
37673      hMap = osCreateFileMappingFromApp(pShmNode->hFile.h,
37674          NULL, PAGE_READWRITE, nByte, NULL
37675      );
37676#elif defined(SQLITE_WIN32_HAS_WIDE)
37677      hMap = osCreateFileMappingW(pShmNode->hFile.h,
37678          NULL, PAGE_READWRITE, 0, nByte, NULL
37679      );
37680#elif defined(SQLITE_WIN32_HAS_ANSI)
37681      hMap = osCreateFileMappingA(pShmNode->hFile.h,
37682          NULL, PAGE_READWRITE, 0, nByte, NULL
37683      );
37684#endif
37685      OSTRACE(("SHM-MAP-CREATE pid=%lu, region=%d, size=%d, rc=%s\n",
37686               osGetCurrentProcessId(), pShmNode->nRegion, nByte,
37687               hMap ? "ok" : "failed"));
37688      if( hMap ){
37689        int iOffset = pShmNode->nRegion*szRegion;
37690        int iOffsetShift = iOffset % winSysInfo.dwAllocationGranularity;
37691#if SQLITE_OS_WINRT
37692        pMap = osMapViewOfFileFromApp(hMap, FILE_MAP_WRITE | FILE_MAP_READ,
37693            iOffset - iOffsetShift, szRegion + iOffsetShift
37694        );
37695#else
37696        pMap = osMapViewOfFile(hMap, FILE_MAP_WRITE | FILE_MAP_READ,
37697            0, iOffset - iOffsetShift, szRegion + iOffsetShift
37698        );
37699#endif
37700        OSTRACE(("SHM-MAP-MAP pid=%lu, region=%d, offset=%d, size=%d, rc=%s\n",
37701                 osGetCurrentProcessId(), pShmNode->nRegion, iOffset,
37702                 szRegion, pMap ? "ok" : "failed"));
37703      }
37704      if( !pMap ){
37705        pShmNode->lastErrno = osGetLastError();
37706        rc = winLogError(SQLITE_IOERR_SHMMAP, pShmNode->lastErrno,
37707                         "winShmMap3", pDbFd->zPath);
37708        if( hMap ) osCloseHandle(hMap);
37709        goto shmpage_out;
37710      }
37711
37712      pShmNode->aRegion[pShmNode->nRegion].pMap = pMap;
37713      pShmNode->aRegion[pShmNode->nRegion].hMap = hMap;
37714      pShmNode->nRegion++;
37715    }
37716  }
37717
37718shmpage_out:
37719  if( pShmNode->nRegion>iRegion ){
37720    int iOffset = iRegion*szRegion;
37721    int iOffsetShift = iOffset % winSysInfo.dwAllocationGranularity;
37722    char *p = (char *)pShmNode->aRegion[iRegion].pMap;
37723    *pp = (void *)&p[iOffsetShift];
37724  }else{
37725    *pp = 0;
37726  }
37727  sqlite3_mutex_leave(pShmNode->mutex);
37728  return rc;
37729}
37730
37731#else
37732# define winShmMap     0
37733# define winShmLock    0
37734# define winShmBarrier 0
37735# define winShmUnmap   0
37736#endif /* #ifndef SQLITE_OMIT_WAL */
37737
37738/*
37739** Cleans up the mapped region of the specified file, if any.
37740*/
37741#if SQLITE_MAX_MMAP_SIZE>0
37742static int winUnmapfile(winFile *pFile){
37743  assert( pFile!=0 );
37744  OSTRACE(("UNMAP-FILE pid=%lu, pFile=%p, hMap=%p, pMapRegion=%p, "
37745           "mmapSize=%lld, mmapSizeActual=%lld, mmapSizeMax=%lld\n",
37746           osGetCurrentProcessId(), pFile, pFile->hMap, pFile->pMapRegion,
37747           pFile->mmapSize, pFile->mmapSizeActual, pFile->mmapSizeMax));
37748  if( pFile->pMapRegion ){
37749    if( !osUnmapViewOfFile(pFile->pMapRegion) ){
37750      pFile->lastErrno = osGetLastError();
37751      OSTRACE(("UNMAP-FILE pid=%lu, pFile=%p, pMapRegion=%p, "
37752               "rc=SQLITE_IOERR_MMAP\n", osGetCurrentProcessId(), pFile,
37753               pFile->pMapRegion));
37754      return winLogError(SQLITE_IOERR_MMAP, pFile->lastErrno,
37755                         "winUnmapfile1", pFile->zPath);
37756    }
37757    pFile->pMapRegion = 0;
37758    pFile->mmapSize = 0;
37759    pFile->mmapSizeActual = 0;
37760  }
37761  if( pFile->hMap!=NULL ){
37762    if( !osCloseHandle(pFile->hMap) ){
37763      pFile->lastErrno = osGetLastError();
37764      OSTRACE(("UNMAP-FILE pid=%lu, pFile=%p, hMap=%p, rc=SQLITE_IOERR_MMAP\n",
37765               osGetCurrentProcessId(), pFile, pFile->hMap));
37766      return winLogError(SQLITE_IOERR_MMAP, pFile->lastErrno,
37767                         "winUnmapfile2", pFile->zPath);
37768    }
37769    pFile->hMap = NULL;
37770  }
37771  OSTRACE(("UNMAP-FILE pid=%lu, pFile=%p, rc=SQLITE_OK\n",
37772           osGetCurrentProcessId(), pFile));
37773  return SQLITE_OK;
37774}
37775
37776/*
37777** Memory map or remap the file opened by file-descriptor pFd (if the file
37778** is already mapped, the existing mapping is replaced by the new). Or, if
37779** there already exists a mapping for this file, and there are still
37780** outstanding xFetch() references to it, this function is a no-op.
37781**
37782** If parameter nByte is non-negative, then it is the requested size of
37783** the mapping to create. Otherwise, if nByte is less than zero, then the
37784** requested size is the size of the file on disk. The actual size of the
37785** created mapping is either the requested size or the value configured
37786** using SQLITE_FCNTL_MMAP_SIZE, whichever is smaller.
37787**
37788** SQLITE_OK is returned if no error occurs (even if the mapping is not
37789** recreated as a result of outstanding references) or an SQLite error
37790** code otherwise.
37791*/
37792static int winMapfile(winFile *pFd, sqlite3_int64 nByte){
37793  sqlite3_int64 nMap = nByte;
37794  int rc;
37795
37796  assert( nMap>=0 || pFd->nFetchOut==0 );
37797  OSTRACE(("MAP-FILE pid=%lu, pFile=%p, size=%lld\n",
37798           osGetCurrentProcessId(), pFd, nByte));
37799
37800  if( pFd->nFetchOut>0 ) return SQLITE_OK;
37801
37802  if( nMap<0 ){
37803    rc = winFileSize((sqlite3_file*)pFd, &nMap);
37804    if( rc ){
37805      OSTRACE(("MAP-FILE pid=%lu, pFile=%p, rc=SQLITE_IOERR_FSTAT\n",
37806               osGetCurrentProcessId(), pFd));
37807      return SQLITE_IOERR_FSTAT;
37808    }
37809  }
37810  if( nMap>pFd->mmapSizeMax ){
37811    nMap = pFd->mmapSizeMax;
37812  }
37813  nMap &= ~(sqlite3_int64)(winSysInfo.dwPageSize - 1);
37814
37815  if( nMap==0 && pFd->mmapSize>0 ){
37816    winUnmapfile(pFd);
37817  }
37818  if( nMap!=pFd->mmapSize ){
37819    void *pNew = 0;
37820    DWORD protect = PAGE_READONLY;
37821    DWORD flags = FILE_MAP_READ;
37822
37823    winUnmapfile(pFd);
37824    if( (pFd->ctrlFlags & WINFILE_RDONLY)==0 ){
37825      protect = PAGE_READWRITE;
37826      flags |= FILE_MAP_WRITE;
37827    }
37828#if SQLITE_OS_WINRT
37829    pFd->hMap = osCreateFileMappingFromApp(pFd->h, NULL, protect, nMap, NULL);
37830#elif defined(SQLITE_WIN32_HAS_WIDE)
37831    pFd->hMap = osCreateFileMappingW(pFd->h, NULL, protect,
37832                                (DWORD)((nMap>>32) & 0xffffffff),
37833                                (DWORD)(nMap & 0xffffffff), NULL);
37834#elif defined(SQLITE_WIN32_HAS_ANSI)
37835    pFd->hMap = osCreateFileMappingA(pFd->h, NULL, protect,
37836                                (DWORD)((nMap>>32) & 0xffffffff),
37837                                (DWORD)(nMap & 0xffffffff), NULL);
37838#endif
37839    if( pFd->hMap==NULL ){
37840      pFd->lastErrno = osGetLastError();
37841      rc = winLogError(SQLITE_IOERR_MMAP, pFd->lastErrno,
37842                       "winMapfile1", pFd->zPath);
37843      /* Log the error, but continue normal operation using xRead/xWrite */
37844      OSTRACE(("MAP-FILE-CREATE pid=%lu, pFile=%p, rc=%s\n",
37845               osGetCurrentProcessId(), pFd, sqlite3ErrName(rc)));
37846      return SQLITE_OK;
37847    }
37848    assert( (nMap % winSysInfo.dwPageSize)==0 );
37849    assert( sizeof(SIZE_T)==sizeof(sqlite3_int64) || nMap<=0xffffffff );
37850#if SQLITE_OS_WINRT
37851    pNew = osMapViewOfFileFromApp(pFd->hMap, flags, 0, (SIZE_T)nMap);
37852#else
37853    pNew = osMapViewOfFile(pFd->hMap, flags, 0, 0, (SIZE_T)nMap);
37854#endif
37855    if( pNew==NULL ){
37856      osCloseHandle(pFd->hMap);
37857      pFd->hMap = NULL;
37858      pFd->lastErrno = osGetLastError();
37859      rc = winLogError(SQLITE_IOERR_MMAP, pFd->lastErrno,
37860                       "winMapfile2", pFd->zPath);
37861      /* Log the error, but continue normal operation using xRead/xWrite */
37862      OSTRACE(("MAP-FILE-MAP pid=%lu, pFile=%p, rc=%s\n",
37863               osGetCurrentProcessId(), pFd, sqlite3ErrName(rc)));
37864      return SQLITE_OK;
37865    }
37866    pFd->pMapRegion = pNew;
37867    pFd->mmapSize = nMap;
37868    pFd->mmapSizeActual = nMap;
37869  }
37870
37871  OSTRACE(("MAP-FILE pid=%lu, pFile=%p, rc=SQLITE_OK\n",
37872           osGetCurrentProcessId(), pFd));
37873  return SQLITE_OK;
37874}
37875#endif /* SQLITE_MAX_MMAP_SIZE>0 */
37876
37877/*
37878** If possible, return a pointer to a mapping of file fd starting at offset
37879** iOff. The mapping must be valid for at least nAmt bytes.
37880**
37881** If such a pointer can be obtained, store it in *pp and return SQLITE_OK.
37882** Or, if one cannot but no error occurs, set *pp to 0 and return SQLITE_OK.
37883** Finally, if an error does occur, return an SQLite error code. The final
37884** value of *pp is undefined in this case.
37885**
37886** If this function does return a pointer, the caller must eventually
37887** release the reference by calling winUnfetch().
37888*/
37889static int winFetch(sqlite3_file *fd, i64 iOff, int nAmt, void **pp){
37890#if SQLITE_MAX_MMAP_SIZE>0
37891  winFile *pFd = (winFile*)fd;   /* The underlying database file */
37892#endif
37893  *pp = 0;
37894
37895  OSTRACE(("FETCH pid=%lu, pFile=%p, offset=%lld, amount=%d, pp=%p\n",
37896           osGetCurrentProcessId(), fd, iOff, nAmt, pp));
37897
37898#if SQLITE_MAX_MMAP_SIZE>0
37899  if( pFd->mmapSizeMax>0 ){
37900    if( pFd->pMapRegion==0 ){
37901      int rc = winMapfile(pFd, -1);
37902      if( rc!=SQLITE_OK ){
37903        OSTRACE(("FETCH pid=%lu, pFile=%p, rc=%s\n",
37904                 osGetCurrentProcessId(), pFd, sqlite3ErrName(rc)));
37905        return rc;
37906      }
37907    }
37908    if( pFd->mmapSize >= iOff+nAmt ){
37909      *pp = &((u8 *)pFd->pMapRegion)[iOff];
37910      pFd->nFetchOut++;
37911    }
37912  }
37913#endif
37914
37915  OSTRACE(("FETCH pid=%lu, pFile=%p, pp=%p, *pp=%p, rc=SQLITE_OK\n",
37916           osGetCurrentProcessId(), fd, pp, *pp));
37917  return SQLITE_OK;
37918}
37919
37920/*
37921** If the third argument is non-NULL, then this function releases a
37922** reference obtained by an earlier call to winFetch(). The second
37923** argument passed to this function must be the same as the corresponding
37924** argument that was passed to the winFetch() invocation.
37925**
37926** Or, if the third argument is NULL, then this function is being called
37927** to inform the VFS layer that, according to POSIX, any existing mapping
37928** may now be invalid and should be unmapped.
37929*/
37930static int winUnfetch(sqlite3_file *fd, i64 iOff, void *p){
37931#if SQLITE_MAX_MMAP_SIZE>0
37932  winFile *pFd = (winFile*)fd;   /* The underlying database file */
37933
37934  /* If p==0 (unmap the entire file) then there must be no outstanding
37935  ** xFetch references. Or, if p!=0 (meaning it is an xFetch reference),
37936  ** then there must be at least one outstanding.  */
37937  assert( (p==0)==(pFd->nFetchOut==0) );
37938
37939  /* If p!=0, it must match the iOff value. */
37940  assert( p==0 || p==&((u8 *)pFd->pMapRegion)[iOff] );
37941
37942  OSTRACE(("UNFETCH pid=%lu, pFile=%p, offset=%lld, p=%p\n",
37943           osGetCurrentProcessId(), pFd, iOff, p));
37944
37945  if( p ){
37946    pFd->nFetchOut--;
37947  }else{
37948    /* FIXME:  If Windows truly always prevents truncating or deleting a
37949    ** file while a mapping is held, then the following winUnmapfile() call
37950    ** is unnecessary can be omitted - potentially improving
37951    ** performance.  */
37952    winUnmapfile(pFd);
37953  }
37954
37955  assert( pFd->nFetchOut>=0 );
37956#endif
37957
37958  OSTRACE(("UNFETCH pid=%lu, pFile=%p, rc=SQLITE_OK\n",
37959           osGetCurrentProcessId(), fd));
37960  return SQLITE_OK;
37961}
37962
37963/*
37964** Here ends the implementation of all sqlite3_file methods.
37965**
37966********************** End sqlite3_file Methods *******************************
37967******************************************************************************/
37968
37969/*
37970** This vector defines all the methods that can operate on an
37971** sqlite3_file for win32.
37972*/
37973static const sqlite3_io_methods winIoMethod = {
37974  3,                              /* iVersion */
37975  winClose,                       /* xClose */
37976  winRead,                        /* xRead */
37977  winWrite,                       /* xWrite */
37978  winTruncate,                    /* xTruncate */
37979  winSync,                        /* xSync */
37980  winFileSize,                    /* xFileSize */
37981  winLock,                        /* xLock */
37982  winUnlock,                      /* xUnlock */
37983  winCheckReservedLock,           /* xCheckReservedLock */
37984  winFileControl,                 /* xFileControl */
37985  winSectorSize,                  /* xSectorSize */
37986  winDeviceCharacteristics,       /* xDeviceCharacteristics */
37987  winShmMap,                      /* xShmMap */
37988  winShmLock,                     /* xShmLock */
37989  winShmBarrier,                  /* xShmBarrier */
37990  winShmUnmap,                    /* xShmUnmap */
37991  winFetch,                       /* xFetch */
37992  winUnfetch                      /* xUnfetch */
37993};
37994
37995/****************************************************************************
37996**************************** sqlite3_vfs methods ****************************
37997**
37998** This division contains the implementation of methods on the
37999** sqlite3_vfs object.
38000*/
38001
38002#if defined(__CYGWIN__)
38003/*
38004** Convert a filename from whatever the underlying operating system
38005** supports for filenames into UTF-8.  Space to hold the result is
38006** obtained from malloc and must be freed by the calling function.
38007*/
38008static char *winConvertToUtf8Filename(const void *zFilename){
38009  char *zConverted = 0;
38010  if( osIsNT() ){
38011    zConverted = winUnicodeToUtf8(zFilename);
38012  }
38013#ifdef SQLITE_WIN32_HAS_ANSI
38014  else{
38015    zConverted = sqlite3_win32_mbcs_to_utf8(zFilename);
38016  }
38017#endif
38018  /* caller will handle out of memory */
38019  return zConverted;
38020}
38021#endif
38022
38023/*
38024** Convert a UTF-8 filename into whatever form the underlying
38025** operating system wants filenames in.  Space to hold the result
38026** is obtained from malloc and must be freed by the calling
38027** function.
38028*/
38029static void *winConvertFromUtf8Filename(const char *zFilename){
38030  void *zConverted = 0;
38031  if( osIsNT() ){
38032    zConverted = winUtf8ToUnicode(zFilename);
38033  }
38034#ifdef SQLITE_WIN32_HAS_ANSI
38035  else{
38036    zConverted = sqlite3_win32_utf8_to_mbcs(zFilename);
38037  }
38038#endif
38039  /* caller will handle out of memory */
38040  return zConverted;
38041}
38042
38043/*
38044** This function returns non-zero if the specified UTF-8 string buffer
38045** ends with a directory separator character or one was successfully
38046** added to it.
38047*/
38048static int winMakeEndInDirSep(int nBuf, char *zBuf){
38049  if( zBuf ){
38050    int nLen = sqlite3Strlen30(zBuf);
38051    if( nLen>0 ){
38052      if( winIsDirSep(zBuf[nLen-1]) ){
38053        return 1;
38054      }else if( nLen+1<nBuf ){
38055        zBuf[nLen] = winGetDirSep();
38056        zBuf[nLen+1] = '\0';
38057        return 1;
38058      }
38059    }
38060  }
38061  return 0;
38062}
38063
38064/*
38065** Create a temporary file name and store the resulting pointer into pzBuf.
38066** The pointer returned in pzBuf must be freed via sqlite3_free().
38067*/
38068static int winGetTempname(sqlite3_vfs *pVfs, char **pzBuf){
38069  static char zChars[] =
38070    "abcdefghijklmnopqrstuvwxyz"
38071    "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
38072    "0123456789";
38073  size_t i, j;
38074  int nPre = sqlite3Strlen30(SQLITE_TEMP_FILE_PREFIX);
38075  int nMax, nBuf, nDir, nLen;
38076  char *zBuf;
38077
38078  /* It's odd to simulate an io-error here, but really this is just
38079  ** using the io-error infrastructure to test that SQLite handles this
38080  ** function failing.
38081  */
38082  SimulateIOError( return SQLITE_IOERR );
38083
38084  /* Allocate a temporary buffer to store the fully qualified file
38085  ** name for the temporary file.  If this fails, we cannot continue.
38086  */
38087  nMax = pVfs->mxPathname; nBuf = nMax + 2;
38088  zBuf = sqlite3MallocZero( nBuf );
38089  if( !zBuf ){
38090    OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
38091    return SQLITE_IOERR_NOMEM;
38092  }
38093
38094  /* Figure out the effective temporary directory.  First, check if one
38095  ** has been explicitly set by the application; otherwise, use the one
38096  ** configured by the operating system.
38097  */
38098  nDir = nMax - (nPre + 15);
38099  assert( nDir>0 );
38100  if( sqlite3_temp_directory ){
38101    int nDirLen = sqlite3Strlen30(sqlite3_temp_directory);
38102    if( nDirLen>0 ){
38103      if( !winIsDirSep(sqlite3_temp_directory[nDirLen-1]) ){
38104        nDirLen++;
38105      }
38106      if( nDirLen>nDir ){
38107        sqlite3_free(zBuf);
38108        OSTRACE(("TEMP-FILENAME rc=SQLITE_ERROR\n"));
38109        return winLogError(SQLITE_ERROR, 0, "winGetTempname1", 0);
38110      }
38111      sqlite3_snprintf(nMax, zBuf, "%s", sqlite3_temp_directory);
38112    }
38113  }
38114#if defined(__CYGWIN__)
38115  else{
38116    static const char *azDirs[] = {
38117       0, /* getenv("SQLITE_TMPDIR") */
38118       0, /* getenv("TMPDIR") */
38119       0, /* getenv("TMP") */
38120       0, /* getenv("TEMP") */
38121       0, /* getenv("USERPROFILE") */
38122       "/var/tmp",
38123       "/usr/tmp",
38124       "/tmp",
38125       ".",
38126       0        /* List terminator */
38127    };
38128    unsigned int i;
38129    const char *zDir = 0;
38130
38131    if( !azDirs[0] ) azDirs[0] = getenv("SQLITE_TMPDIR");
38132    if( !azDirs[1] ) azDirs[1] = getenv("TMPDIR");
38133    if( !azDirs[2] ) azDirs[2] = getenv("TMP");
38134    if( !azDirs[3] ) azDirs[3] = getenv("TEMP");
38135    if( !azDirs[4] ) azDirs[4] = getenv("USERPROFILE");
38136    for(i=0; i<sizeof(azDirs)/sizeof(azDirs[0]); zDir=azDirs[i++]){
38137      void *zConverted;
38138      if( zDir==0 ) continue;
38139      /* If the path starts with a drive letter followed by the colon
38140      ** character, assume it is already a native Win32 path; otherwise,
38141      ** it must be converted to a native Win32 path via the Cygwin API
38142      ** prior to using it.
38143      */
38144      if( winIsDriveLetterAndColon(zDir) ){
38145        zConverted = winConvertFromUtf8Filename(zDir);
38146        if( !zConverted ){
38147          sqlite3_free(zBuf);
38148          OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
38149          return SQLITE_IOERR_NOMEM;
38150        }
38151        if( winIsDir(zConverted) ){
38152          sqlite3_snprintf(nMax, zBuf, "%s", zDir);
38153          sqlite3_free(zConverted);
38154          break;
38155        }
38156        sqlite3_free(zConverted);
38157      }else{
38158        zConverted = sqlite3MallocZero( nMax+1 );
38159        if( !zConverted ){
38160          sqlite3_free(zBuf);
38161          OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
38162          return SQLITE_IOERR_NOMEM;
38163        }
38164        if( cygwin_conv_path(
38165                osIsNT() ? CCP_POSIX_TO_WIN_W : CCP_POSIX_TO_WIN_A, zDir,
38166                zConverted, nMax+1)<0 ){
38167          sqlite3_free(zConverted);
38168          sqlite3_free(zBuf);
38169          OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_CONVPATH\n"));
38170          return winLogError(SQLITE_IOERR_CONVPATH, (DWORD)errno,
38171                             "winGetTempname2", zDir);
38172        }
38173        if( winIsDir(zConverted) ){
38174          /* At this point, we know the candidate directory exists and should
38175          ** be used.  However, we may need to convert the string containing
38176          ** its name into UTF-8 (i.e. if it is UTF-16 right now).
38177          */
38178          char *zUtf8 = winConvertToUtf8Filename(zConverted);
38179          if( !zUtf8 ){
38180            sqlite3_free(zConverted);
38181            sqlite3_free(zBuf);
38182            OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
38183            return SQLITE_IOERR_NOMEM;
38184          }
38185          sqlite3_snprintf(nMax, zBuf, "%s", zUtf8);
38186          sqlite3_free(zUtf8);
38187          sqlite3_free(zConverted);
38188          break;
38189        }
38190        sqlite3_free(zConverted);
38191      }
38192    }
38193  }
38194#elif !SQLITE_OS_WINRT && !defined(__CYGWIN__)
38195  else if( osIsNT() ){
38196    char *zMulti;
38197    LPWSTR zWidePath = sqlite3MallocZero( nMax*sizeof(WCHAR) );
38198    if( !zWidePath ){
38199      sqlite3_free(zBuf);
38200      OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
38201      return SQLITE_IOERR_NOMEM;
38202    }
38203    if( osGetTempPathW(nMax, zWidePath)==0 ){
38204      sqlite3_free(zWidePath);
38205      sqlite3_free(zBuf);
38206      OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_GETTEMPPATH\n"));
38207      return winLogError(SQLITE_IOERR_GETTEMPPATH, osGetLastError(),
38208                         "winGetTempname2", 0);
38209    }
38210    zMulti = winUnicodeToUtf8(zWidePath);
38211    if( zMulti ){
38212      sqlite3_snprintf(nMax, zBuf, "%s", zMulti);
38213      sqlite3_free(zMulti);
38214      sqlite3_free(zWidePath);
38215    }else{
38216      sqlite3_free(zWidePath);
38217      sqlite3_free(zBuf);
38218      OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
38219      return SQLITE_IOERR_NOMEM;
38220    }
38221  }
38222#ifdef SQLITE_WIN32_HAS_ANSI
38223  else{
38224    char *zUtf8;
38225    char *zMbcsPath = sqlite3MallocZero( nMax );
38226    if( !zMbcsPath ){
38227      sqlite3_free(zBuf);
38228      OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
38229      return SQLITE_IOERR_NOMEM;
38230    }
38231    if( osGetTempPathA(nMax, zMbcsPath)==0 ){
38232      sqlite3_free(zBuf);
38233      OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_GETTEMPPATH\n"));
38234      return winLogError(SQLITE_IOERR_GETTEMPPATH, osGetLastError(),
38235                         "winGetTempname3", 0);
38236    }
38237    zUtf8 = sqlite3_win32_mbcs_to_utf8(zMbcsPath);
38238    if( zUtf8 ){
38239      sqlite3_snprintf(nMax, zBuf, "%s", zUtf8);
38240      sqlite3_free(zUtf8);
38241    }else{
38242      sqlite3_free(zBuf);
38243      OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
38244      return SQLITE_IOERR_NOMEM;
38245    }
38246  }
38247#endif /* SQLITE_WIN32_HAS_ANSI */
38248#endif /* !SQLITE_OS_WINRT */
38249
38250  /*
38251  ** Check to make sure the temporary directory ends with an appropriate
38252  ** separator.  If it does not and there is not enough space left to add
38253  ** one, fail.
38254  */
38255  if( !winMakeEndInDirSep(nDir+1, zBuf) ){
38256    sqlite3_free(zBuf);
38257    OSTRACE(("TEMP-FILENAME rc=SQLITE_ERROR\n"));
38258    return winLogError(SQLITE_ERROR, 0, "winGetTempname4", 0);
38259  }
38260
38261  /*
38262  ** Check that the output buffer is large enough for the temporary file
38263  ** name in the following format:
38264  **
38265  **   "<temporary_directory>/etilqs_XXXXXXXXXXXXXXX\0\0"
38266  **
38267  ** If not, return SQLITE_ERROR.  The number 17 is used here in order to
38268  ** account for the space used by the 15 character random suffix and the
38269  ** two trailing NUL characters.  The final directory separator character
38270  ** has already added if it was not already present.
38271  */
38272  nLen = sqlite3Strlen30(zBuf);
38273  if( (nLen + nPre + 17) > nBuf ){
38274    sqlite3_free(zBuf);
38275    OSTRACE(("TEMP-FILENAME rc=SQLITE_ERROR\n"));
38276    return winLogError(SQLITE_ERROR, 0, "winGetTempname5", 0);
38277  }
38278
38279  sqlite3_snprintf(nBuf-16-nLen, zBuf+nLen, SQLITE_TEMP_FILE_PREFIX);
38280
38281  j = sqlite3Strlen30(zBuf);
38282  sqlite3_randomness(15, &zBuf[j]);
38283  for(i=0; i<15; i++, j++){
38284    zBuf[j] = (char)zChars[ ((unsigned char)zBuf[j])%(sizeof(zChars)-1) ];
38285  }
38286  zBuf[j] = 0;
38287  zBuf[j+1] = 0;
38288  *pzBuf = zBuf;
38289
38290  OSTRACE(("TEMP-FILENAME name=%s, rc=SQLITE_OK\n", zBuf));
38291  return SQLITE_OK;
38292}
38293
38294/*
38295** Return TRUE if the named file is really a directory.  Return false if
38296** it is something other than a directory, or if there is any kind of memory
38297** allocation failure.
38298*/
38299static int winIsDir(const void *zConverted){
38300  DWORD attr;
38301  int rc = 0;
38302  DWORD lastErrno;
38303
38304  if( osIsNT() ){
38305    int cnt = 0;
38306    WIN32_FILE_ATTRIBUTE_DATA sAttrData;
38307    memset(&sAttrData, 0, sizeof(sAttrData));
38308    while( !(rc = osGetFileAttributesExW((LPCWSTR)zConverted,
38309                             GetFileExInfoStandard,
38310                             &sAttrData)) && winRetryIoerr(&cnt, &lastErrno) ){}
38311    if( !rc ){
38312      return 0; /* Invalid name? */
38313    }
38314    attr = sAttrData.dwFileAttributes;
38315#if SQLITE_OS_WINCE==0
38316  }else{
38317    attr = osGetFileAttributesA((char*)zConverted);
38318#endif
38319  }
38320  return (attr!=INVALID_FILE_ATTRIBUTES) && (attr&FILE_ATTRIBUTE_DIRECTORY);
38321}
38322
38323/*
38324** Open a file.
38325*/
38326static int winOpen(
38327  sqlite3_vfs *pVfs,        /* Used to get maximum path name length */
38328  const char *zName,        /* Name of the file (UTF-8) */
38329  sqlite3_file *id,         /* Write the SQLite file handle here */
38330  int flags,                /* Open mode flags */
38331  int *pOutFlags            /* Status return flags */
38332){
38333  HANDLE h;
38334  DWORD lastErrno = 0;
38335  DWORD dwDesiredAccess;
38336  DWORD dwShareMode;
38337  DWORD dwCreationDisposition;
38338  DWORD dwFlagsAndAttributes = 0;
38339#if SQLITE_OS_WINCE
38340  int isTemp = 0;
38341#endif
38342  winFile *pFile = (winFile*)id;
38343  void *zConverted;              /* Filename in OS encoding */
38344  const char *zUtf8Name = zName; /* Filename in UTF-8 encoding */
38345  int cnt = 0;
38346
38347  /* If argument zPath is a NULL pointer, this function is required to open
38348  ** a temporary file. Use this buffer to store the file name in.
38349  */
38350  char *zTmpname = 0; /* For temporary filename, if necessary. */
38351
38352  int rc = SQLITE_OK;            /* Function Return Code */
38353#if !defined(NDEBUG) || SQLITE_OS_WINCE
38354  int eType = flags&0xFFFFFF00;  /* Type of file to open */
38355#endif
38356
38357  int isExclusive  = (flags & SQLITE_OPEN_EXCLUSIVE);
38358  int isDelete     = (flags & SQLITE_OPEN_DELETEONCLOSE);
38359  int isCreate     = (flags & SQLITE_OPEN_CREATE);
38360  int isReadonly   = (flags & SQLITE_OPEN_READONLY);
38361  int isReadWrite  = (flags & SQLITE_OPEN_READWRITE);
38362
38363#ifndef NDEBUG
38364  int isOpenJournal = (isCreate && (
38365        eType==SQLITE_OPEN_MASTER_JOURNAL
38366     || eType==SQLITE_OPEN_MAIN_JOURNAL
38367     || eType==SQLITE_OPEN_WAL
38368  ));
38369#endif
38370
38371  OSTRACE(("OPEN name=%s, pFile=%p, flags=%x, pOutFlags=%p\n",
38372           zUtf8Name, id, flags, pOutFlags));
38373
38374  /* Check the following statements are true:
38375  **
38376  **   (a) Exactly one of the READWRITE and READONLY flags must be set, and
38377  **   (b) if CREATE is set, then READWRITE must also be set, and
38378  **   (c) if EXCLUSIVE is set, then CREATE must also be set.
38379  **   (d) if DELETEONCLOSE is set, then CREATE must also be set.
38380  */
38381  assert((isReadonly==0 || isReadWrite==0) && (isReadWrite || isReadonly));
38382  assert(isCreate==0 || isReadWrite);
38383  assert(isExclusive==0 || isCreate);
38384  assert(isDelete==0 || isCreate);
38385
38386  /* The main DB, main journal, WAL file and master journal are never
38387  ** automatically deleted. Nor are they ever temporary files.  */
38388  assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MAIN_DB );
38389  assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MAIN_JOURNAL );
38390  assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MASTER_JOURNAL );
38391  assert( (!isDelete && zName) || eType!=SQLITE_OPEN_WAL );
38392
38393  /* Assert that the upper layer has set one of the "file-type" flags. */
38394  assert( eType==SQLITE_OPEN_MAIN_DB      || eType==SQLITE_OPEN_TEMP_DB
38395       || eType==SQLITE_OPEN_MAIN_JOURNAL || eType==SQLITE_OPEN_TEMP_JOURNAL
38396       || eType==SQLITE_OPEN_SUBJOURNAL   || eType==SQLITE_OPEN_MASTER_JOURNAL
38397       || eType==SQLITE_OPEN_TRANSIENT_DB || eType==SQLITE_OPEN_WAL
38398  );
38399
38400  assert( pFile!=0 );
38401  memset(pFile, 0, sizeof(winFile));
38402  pFile->h = INVALID_HANDLE_VALUE;
38403
38404#if SQLITE_OS_WINRT
38405  if( !zUtf8Name && !sqlite3_temp_directory ){
38406    sqlite3_log(SQLITE_ERROR,
38407        "sqlite3_temp_directory variable should be set for WinRT");
38408  }
38409#endif
38410
38411  /* If the second argument to this function is NULL, generate a
38412  ** temporary file name to use
38413  */
38414  if( !zUtf8Name ){
38415    assert( isDelete && !isOpenJournal );
38416    rc = winGetTempname(pVfs, &zTmpname);
38417    if( rc!=SQLITE_OK ){
38418      OSTRACE(("OPEN name=%s, rc=%s", zUtf8Name, sqlite3ErrName(rc)));
38419      return rc;
38420    }
38421    zUtf8Name = zTmpname;
38422  }
38423
38424  /* Database filenames are double-zero terminated if they are not
38425  ** URIs with parameters.  Hence, they can always be passed into
38426  ** sqlite3_uri_parameter().
38427  */
38428  assert( (eType!=SQLITE_OPEN_MAIN_DB) || (flags & SQLITE_OPEN_URI) ||
38429       zUtf8Name[sqlite3Strlen30(zUtf8Name)+1]==0 );
38430
38431  /* Convert the filename to the system encoding. */
38432  zConverted = winConvertFromUtf8Filename(zUtf8Name);
38433  if( zConverted==0 ){
38434    sqlite3_free(zTmpname);
38435    OSTRACE(("OPEN name=%s, rc=SQLITE_IOERR_NOMEM", zUtf8Name));
38436    return SQLITE_IOERR_NOMEM;
38437  }
38438
38439  if( winIsDir(zConverted) ){
38440    sqlite3_free(zConverted);
38441    sqlite3_free(zTmpname);
38442    OSTRACE(("OPEN name=%s, rc=SQLITE_CANTOPEN_ISDIR", zUtf8Name));
38443    return SQLITE_CANTOPEN_ISDIR;
38444  }
38445
38446  if( isReadWrite ){
38447    dwDesiredAccess = GENERIC_READ | GENERIC_WRITE;
38448  }else{
38449    dwDesiredAccess = GENERIC_READ;
38450  }
38451
38452  /* SQLITE_OPEN_EXCLUSIVE is used to make sure that a new file is
38453  ** created. SQLite doesn't use it to indicate "exclusive access"
38454  ** as it is usually understood.
38455  */
38456  if( isExclusive ){
38457    /* Creates a new file, only if it does not already exist. */
38458    /* If the file exists, it fails. */
38459    dwCreationDisposition = CREATE_NEW;
38460  }else if( isCreate ){
38461    /* Open existing file, or create if it doesn't exist */
38462    dwCreationDisposition = OPEN_ALWAYS;
38463  }else{
38464    /* Opens a file, only if it exists. */
38465    dwCreationDisposition = OPEN_EXISTING;
38466  }
38467
38468  dwShareMode = FILE_SHARE_READ | FILE_SHARE_WRITE;
38469
38470  if( isDelete ){
38471#if SQLITE_OS_WINCE
38472    dwFlagsAndAttributes = FILE_ATTRIBUTE_HIDDEN;
38473    isTemp = 1;
38474#else
38475    dwFlagsAndAttributes = FILE_ATTRIBUTE_TEMPORARY
38476                               | FILE_ATTRIBUTE_HIDDEN
38477                               | FILE_FLAG_DELETE_ON_CLOSE;
38478#endif
38479  }else{
38480    dwFlagsAndAttributes = FILE_ATTRIBUTE_NORMAL;
38481  }
38482  /* Reports from the internet are that performance is always
38483  ** better if FILE_FLAG_RANDOM_ACCESS is used.  Ticket #2699. */
38484#if SQLITE_OS_WINCE
38485  dwFlagsAndAttributes |= FILE_FLAG_RANDOM_ACCESS;
38486#endif
38487
38488  if( osIsNT() ){
38489#if SQLITE_OS_WINRT
38490    CREATEFILE2_EXTENDED_PARAMETERS extendedParameters;
38491    extendedParameters.dwSize = sizeof(CREATEFILE2_EXTENDED_PARAMETERS);
38492    extendedParameters.dwFileAttributes =
38493            dwFlagsAndAttributes & FILE_ATTRIBUTE_MASK;
38494    extendedParameters.dwFileFlags = dwFlagsAndAttributes & FILE_FLAG_MASK;
38495    extendedParameters.dwSecurityQosFlags = SECURITY_ANONYMOUS;
38496    extendedParameters.lpSecurityAttributes = NULL;
38497    extendedParameters.hTemplateFile = NULL;
38498    while( (h = osCreateFile2((LPCWSTR)zConverted,
38499                              dwDesiredAccess,
38500                              dwShareMode,
38501                              dwCreationDisposition,
38502                              &extendedParameters))==INVALID_HANDLE_VALUE &&
38503                              winRetryIoerr(&cnt, &lastErrno) ){
38504               /* Noop */
38505    }
38506#else
38507    while( (h = osCreateFileW((LPCWSTR)zConverted,
38508                              dwDesiredAccess,
38509                              dwShareMode, NULL,
38510                              dwCreationDisposition,
38511                              dwFlagsAndAttributes,
38512                              NULL))==INVALID_HANDLE_VALUE &&
38513                              winRetryIoerr(&cnt, &lastErrno) ){
38514               /* Noop */
38515    }
38516#endif
38517  }
38518#ifdef SQLITE_WIN32_HAS_ANSI
38519  else{
38520    while( (h = osCreateFileA((LPCSTR)zConverted,
38521                              dwDesiredAccess,
38522                              dwShareMode, NULL,
38523                              dwCreationDisposition,
38524                              dwFlagsAndAttributes,
38525                              NULL))==INVALID_HANDLE_VALUE &&
38526                              winRetryIoerr(&cnt, &lastErrno) ){
38527               /* Noop */
38528    }
38529  }
38530#endif
38531  winLogIoerr(cnt, __LINE__);
38532
38533  OSTRACE(("OPEN file=%p, name=%s, access=%lx, rc=%s\n", h, zUtf8Name,
38534           dwDesiredAccess, (h==INVALID_HANDLE_VALUE) ? "failed" : "ok"));
38535
38536  if( h==INVALID_HANDLE_VALUE ){
38537    pFile->lastErrno = lastErrno;
38538    winLogError(SQLITE_CANTOPEN, pFile->lastErrno, "winOpen", zUtf8Name);
38539    sqlite3_free(zConverted);
38540    sqlite3_free(zTmpname);
38541    if( isReadWrite && !isExclusive ){
38542      return winOpen(pVfs, zName, id,
38543         ((flags|SQLITE_OPEN_READONLY) &
38544                     ~(SQLITE_OPEN_CREATE|SQLITE_OPEN_READWRITE)),
38545         pOutFlags);
38546    }else{
38547      return SQLITE_CANTOPEN_BKPT;
38548    }
38549  }
38550
38551  if( pOutFlags ){
38552    if( isReadWrite ){
38553      *pOutFlags = SQLITE_OPEN_READWRITE;
38554    }else{
38555      *pOutFlags = SQLITE_OPEN_READONLY;
38556    }
38557  }
38558
38559  OSTRACE(("OPEN file=%p, name=%s, access=%lx, pOutFlags=%p, *pOutFlags=%d, "
38560           "rc=%s\n", h, zUtf8Name, dwDesiredAccess, pOutFlags, pOutFlags ?
38561           *pOutFlags : 0, (h==INVALID_HANDLE_VALUE) ? "failed" : "ok"));
38562
38563#if SQLITE_OS_WINCE
38564  if( isReadWrite && eType==SQLITE_OPEN_MAIN_DB
38565       && (rc = winceCreateLock(zName, pFile))!=SQLITE_OK
38566  ){
38567    osCloseHandle(h);
38568    sqlite3_free(zConverted);
38569    sqlite3_free(zTmpname);
38570    OSTRACE(("OPEN-CE-LOCK name=%s, rc=%s\n", zName, sqlite3ErrName(rc)));
38571    return rc;
38572  }
38573  if( isTemp ){
38574    pFile->zDeleteOnClose = zConverted;
38575  }else
38576#endif
38577  {
38578    sqlite3_free(zConverted);
38579  }
38580
38581  sqlite3_free(zTmpname);
38582  pFile->pMethod = &winIoMethod;
38583  pFile->pVfs = pVfs;
38584  pFile->h = h;
38585  if( isReadonly ){
38586    pFile->ctrlFlags |= WINFILE_RDONLY;
38587  }
38588  if( sqlite3_uri_boolean(zName, "psow", SQLITE_POWERSAFE_OVERWRITE) ){
38589    pFile->ctrlFlags |= WINFILE_PSOW;
38590  }
38591  pFile->lastErrno = NO_ERROR;
38592  pFile->zPath = zName;
38593#if SQLITE_MAX_MMAP_SIZE>0
38594  pFile->hMap = NULL;
38595  pFile->pMapRegion = 0;
38596  pFile->mmapSize = 0;
38597  pFile->mmapSizeActual = 0;
38598  pFile->mmapSizeMax = sqlite3GlobalConfig.szMmap;
38599#endif
38600
38601  OpenCounter(+1);
38602  return rc;
38603}
38604
38605/*
38606** Delete the named file.
38607**
38608** Note that Windows does not allow a file to be deleted if some other
38609** process has it open.  Sometimes a virus scanner or indexing program
38610** will open a journal file shortly after it is created in order to do
38611** whatever it does.  While this other process is holding the
38612** file open, we will be unable to delete it.  To work around this
38613** problem, we delay 100 milliseconds and try to delete again.  Up
38614** to MX_DELETION_ATTEMPTs deletion attempts are run before giving
38615** up and returning an error.
38616*/
38617static int winDelete(
38618  sqlite3_vfs *pVfs,          /* Not used on win32 */
38619  const char *zFilename,      /* Name of file to delete */
38620  int syncDir                 /* Not used on win32 */
38621){
38622  int cnt = 0;
38623  int rc;
38624  DWORD attr;
38625  DWORD lastErrno = 0;
38626  void *zConverted;
38627  UNUSED_PARAMETER(pVfs);
38628  UNUSED_PARAMETER(syncDir);
38629
38630  SimulateIOError(return SQLITE_IOERR_DELETE);
38631  OSTRACE(("DELETE name=%s, syncDir=%d\n", zFilename, syncDir));
38632
38633  zConverted = winConvertFromUtf8Filename(zFilename);
38634  if( zConverted==0 ){
38635    OSTRACE(("DELETE name=%s, rc=SQLITE_IOERR_NOMEM\n", zFilename));
38636    return SQLITE_IOERR_NOMEM;
38637  }
38638  if( osIsNT() ){
38639    do {
38640#if SQLITE_OS_WINRT
38641      WIN32_FILE_ATTRIBUTE_DATA sAttrData;
38642      memset(&sAttrData, 0, sizeof(sAttrData));
38643      if ( osGetFileAttributesExW(zConverted, GetFileExInfoStandard,
38644                                  &sAttrData) ){
38645        attr = sAttrData.dwFileAttributes;
38646      }else{
38647        lastErrno = osGetLastError();
38648        if( lastErrno==ERROR_FILE_NOT_FOUND
38649         || lastErrno==ERROR_PATH_NOT_FOUND ){
38650          rc = SQLITE_IOERR_DELETE_NOENT; /* Already gone? */
38651        }else{
38652          rc = SQLITE_ERROR;
38653        }
38654        break;
38655      }
38656#else
38657      attr = osGetFileAttributesW(zConverted);
38658#endif
38659      if ( attr==INVALID_FILE_ATTRIBUTES ){
38660        lastErrno = osGetLastError();
38661        if( lastErrno==ERROR_FILE_NOT_FOUND
38662         || lastErrno==ERROR_PATH_NOT_FOUND ){
38663          rc = SQLITE_IOERR_DELETE_NOENT; /* Already gone? */
38664        }else{
38665          rc = SQLITE_ERROR;
38666        }
38667        break;
38668      }
38669      if ( attr&FILE_ATTRIBUTE_DIRECTORY ){
38670        rc = SQLITE_ERROR; /* Files only. */
38671        break;
38672      }
38673      if ( osDeleteFileW(zConverted) ){
38674        rc = SQLITE_OK; /* Deleted OK. */
38675        break;
38676      }
38677      if ( !winRetryIoerr(&cnt, &lastErrno) ){
38678        rc = SQLITE_ERROR; /* No more retries. */
38679        break;
38680      }
38681    } while(1);
38682  }
38683#ifdef SQLITE_WIN32_HAS_ANSI
38684  else{
38685    do {
38686      attr = osGetFileAttributesA(zConverted);
38687      if ( attr==INVALID_FILE_ATTRIBUTES ){
38688        lastErrno = osGetLastError();
38689        if( lastErrno==ERROR_FILE_NOT_FOUND
38690         || lastErrno==ERROR_PATH_NOT_FOUND ){
38691          rc = SQLITE_IOERR_DELETE_NOENT; /* Already gone? */
38692        }else{
38693          rc = SQLITE_ERROR;
38694        }
38695        break;
38696      }
38697      if ( attr&FILE_ATTRIBUTE_DIRECTORY ){
38698        rc = SQLITE_ERROR; /* Files only. */
38699        break;
38700      }
38701      if ( osDeleteFileA(zConverted) ){
38702        rc = SQLITE_OK; /* Deleted OK. */
38703        break;
38704      }
38705      if ( !winRetryIoerr(&cnt, &lastErrno) ){
38706        rc = SQLITE_ERROR; /* No more retries. */
38707        break;
38708      }
38709    } while(1);
38710  }
38711#endif
38712  if( rc && rc!=SQLITE_IOERR_DELETE_NOENT ){
38713    rc = winLogError(SQLITE_IOERR_DELETE, lastErrno, "winDelete", zFilename);
38714  }else{
38715    winLogIoerr(cnt, __LINE__);
38716  }
38717  sqlite3_free(zConverted);
38718  OSTRACE(("DELETE name=%s, rc=%s\n", zFilename, sqlite3ErrName(rc)));
38719  return rc;
38720}
38721
38722/*
38723** Check the existence and status of a file.
38724*/
38725static int winAccess(
38726  sqlite3_vfs *pVfs,         /* Not used on win32 */
38727  const char *zFilename,     /* Name of file to check */
38728  int flags,                 /* Type of test to make on this file */
38729  int *pResOut               /* OUT: Result */
38730){
38731  DWORD attr;
38732  int rc = 0;
38733  DWORD lastErrno = 0;
38734  void *zConverted;
38735  UNUSED_PARAMETER(pVfs);
38736
38737  SimulateIOError( return SQLITE_IOERR_ACCESS; );
38738  OSTRACE(("ACCESS name=%s, flags=%x, pResOut=%p\n",
38739           zFilename, flags, pResOut));
38740
38741  zConverted = winConvertFromUtf8Filename(zFilename);
38742  if( zConverted==0 ){
38743    OSTRACE(("ACCESS name=%s, rc=SQLITE_IOERR_NOMEM\n", zFilename));
38744    return SQLITE_IOERR_NOMEM;
38745  }
38746  if( osIsNT() ){
38747    int cnt = 0;
38748    WIN32_FILE_ATTRIBUTE_DATA sAttrData;
38749    memset(&sAttrData, 0, sizeof(sAttrData));
38750    while( !(rc = osGetFileAttributesExW((LPCWSTR)zConverted,
38751                             GetFileExInfoStandard,
38752                             &sAttrData)) && winRetryIoerr(&cnt, &lastErrno) ){}
38753    if( rc ){
38754      /* For an SQLITE_ACCESS_EXISTS query, treat a zero-length file
38755      ** as if it does not exist.
38756      */
38757      if(    flags==SQLITE_ACCESS_EXISTS
38758          && sAttrData.nFileSizeHigh==0
38759          && sAttrData.nFileSizeLow==0 ){
38760        attr = INVALID_FILE_ATTRIBUTES;
38761      }else{
38762        attr = sAttrData.dwFileAttributes;
38763      }
38764    }else{
38765      winLogIoerr(cnt, __LINE__);
38766      if( lastErrno!=ERROR_FILE_NOT_FOUND && lastErrno!=ERROR_PATH_NOT_FOUND ){
38767        sqlite3_free(zConverted);
38768        return winLogError(SQLITE_IOERR_ACCESS, lastErrno, "winAccess",
38769                           zFilename);
38770      }else{
38771        attr = INVALID_FILE_ATTRIBUTES;
38772      }
38773    }
38774  }
38775#ifdef SQLITE_WIN32_HAS_ANSI
38776  else{
38777    attr = osGetFileAttributesA((char*)zConverted);
38778  }
38779#endif
38780  sqlite3_free(zConverted);
38781  switch( flags ){
38782    case SQLITE_ACCESS_READ:
38783    case SQLITE_ACCESS_EXISTS:
38784      rc = attr!=INVALID_FILE_ATTRIBUTES;
38785      break;
38786    case SQLITE_ACCESS_READWRITE:
38787      rc = attr!=INVALID_FILE_ATTRIBUTES &&
38788             (attr & FILE_ATTRIBUTE_READONLY)==0;
38789      break;
38790    default:
38791      assert(!"Invalid flags argument");
38792  }
38793  *pResOut = rc;
38794  OSTRACE(("ACCESS name=%s, pResOut=%p, *pResOut=%d, rc=SQLITE_OK\n",
38795           zFilename, pResOut, *pResOut));
38796  return SQLITE_OK;
38797}
38798
38799/*
38800** Returns non-zero if the specified path name starts with a drive letter
38801** followed by a colon character.
38802*/
38803static BOOL winIsDriveLetterAndColon(
38804  const char *zPathname
38805){
38806  return ( sqlite3Isalpha(zPathname[0]) && zPathname[1]==':' );
38807}
38808
38809/*
38810** Returns non-zero if the specified path name should be used verbatim.  If
38811** non-zero is returned from this function, the calling function must simply
38812** use the provided path name verbatim -OR- resolve it into a full path name
38813** using the GetFullPathName Win32 API function (if available).
38814*/
38815static BOOL winIsVerbatimPathname(
38816  const char *zPathname
38817){
38818  /*
38819  ** If the path name starts with a forward slash or a backslash, it is either
38820  ** a legal UNC name, a volume relative path, or an absolute path name in the
38821  ** "Unix" format on Windows.  There is no easy way to differentiate between
38822  ** the final two cases; therefore, we return the safer return value of TRUE
38823  ** so that callers of this function will simply use it verbatim.
38824  */
38825  if ( winIsDirSep(zPathname[0]) ){
38826    return TRUE;
38827  }
38828
38829  /*
38830  ** If the path name starts with a letter and a colon it is either a volume
38831  ** relative path or an absolute path.  Callers of this function must not
38832  ** attempt to treat it as a relative path name (i.e. they should simply use
38833  ** it verbatim).
38834  */
38835  if ( winIsDriveLetterAndColon(zPathname) ){
38836    return TRUE;
38837  }
38838
38839  /*
38840  ** If we get to this point, the path name should almost certainly be a purely
38841  ** relative one (i.e. not a UNC name, not absolute, and not volume relative).
38842  */
38843  return FALSE;
38844}
38845
38846/*
38847** Turn a relative pathname into a full pathname.  Write the full
38848** pathname into zOut[].  zOut[] will be at least pVfs->mxPathname
38849** bytes in size.
38850*/
38851static int winFullPathname(
38852  sqlite3_vfs *pVfs,            /* Pointer to vfs object */
38853  const char *zRelative,        /* Possibly relative input path */
38854  int nFull,                    /* Size of output buffer in bytes */
38855  char *zFull                   /* Output buffer */
38856){
38857
38858#if defined(__CYGWIN__)
38859  SimulateIOError( return SQLITE_ERROR );
38860  UNUSED_PARAMETER(nFull);
38861  assert( nFull>=pVfs->mxPathname );
38862  if ( sqlite3_data_directory && !winIsVerbatimPathname(zRelative) ){
38863    /*
38864    ** NOTE: We are dealing with a relative path name and the data
38865    **       directory has been set.  Therefore, use it as the basis
38866    **       for converting the relative path name to an absolute
38867    **       one by prepending the data directory and a slash.
38868    */
38869    char *zOut = sqlite3MallocZero( pVfs->mxPathname+1 );
38870    if( !zOut ){
38871      return SQLITE_IOERR_NOMEM;
38872    }
38873    if( cygwin_conv_path(
38874            (osIsNT() ? CCP_POSIX_TO_WIN_W : CCP_POSIX_TO_WIN_A) |
38875            CCP_RELATIVE, zRelative, zOut, pVfs->mxPathname+1)<0 ){
38876      sqlite3_free(zOut);
38877      return winLogError(SQLITE_CANTOPEN_CONVPATH, (DWORD)errno,
38878                         "winFullPathname1", zRelative);
38879    }else{
38880      char *zUtf8 = winConvertToUtf8Filename(zOut);
38881      if( !zUtf8 ){
38882        sqlite3_free(zOut);
38883        return SQLITE_IOERR_NOMEM;
38884      }
38885      sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s%c%s",
38886                       sqlite3_data_directory, winGetDirSep(), zUtf8);
38887      sqlite3_free(zUtf8);
38888      sqlite3_free(zOut);
38889    }
38890  }else{
38891    char *zOut = sqlite3MallocZero( pVfs->mxPathname+1 );
38892    if( !zOut ){
38893      return SQLITE_IOERR_NOMEM;
38894    }
38895    if( cygwin_conv_path(
38896            (osIsNT() ? CCP_POSIX_TO_WIN_W : CCP_POSIX_TO_WIN_A),
38897            zRelative, zOut, pVfs->mxPathname+1)<0 ){
38898      sqlite3_free(zOut);
38899      return winLogError(SQLITE_CANTOPEN_CONVPATH, (DWORD)errno,
38900                         "winFullPathname2", zRelative);
38901    }else{
38902      char *zUtf8 = winConvertToUtf8Filename(zOut);
38903      if( !zUtf8 ){
38904        sqlite3_free(zOut);
38905        return SQLITE_IOERR_NOMEM;
38906      }
38907      sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s", zUtf8);
38908      sqlite3_free(zUtf8);
38909      sqlite3_free(zOut);
38910    }
38911  }
38912  return SQLITE_OK;
38913#endif
38914
38915#if (SQLITE_OS_WINCE || SQLITE_OS_WINRT) && !defined(__CYGWIN__)
38916  SimulateIOError( return SQLITE_ERROR );
38917  /* WinCE has no concept of a relative pathname, or so I am told. */
38918  /* WinRT has no way to convert a relative path to an absolute one. */
38919  if ( sqlite3_data_directory && !winIsVerbatimPathname(zRelative) ){
38920    /*
38921    ** NOTE: We are dealing with a relative path name and the data
38922    **       directory has been set.  Therefore, use it as the basis
38923    **       for converting the relative path name to an absolute
38924    **       one by prepending the data directory and a backslash.
38925    */
38926    sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s%c%s",
38927                     sqlite3_data_directory, winGetDirSep(), zRelative);
38928  }else{
38929    sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s", zRelative);
38930  }
38931  return SQLITE_OK;
38932#endif
38933
38934#if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && !defined(__CYGWIN__)
38935  DWORD nByte;
38936  void *zConverted;
38937  char *zOut;
38938
38939  /* If this path name begins with "/X:", where "X" is any alphabetic
38940  ** character, discard the initial "/" from the pathname.
38941  */
38942  if( zRelative[0]=='/' && winIsDriveLetterAndColon(zRelative+1) ){
38943    zRelative++;
38944  }
38945
38946  /* It's odd to simulate an io-error here, but really this is just
38947  ** using the io-error infrastructure to test that SQLite handles this
38948  ** function failing. This function could fail if, for example, the
38949  ** current working directory has been unlinked.
38950  */
38951  SimulateIOError( return SQLITE_ERROR );
38952  if ( sqlite3_data_directory && !winIsVerbatimPathname(zRelative) ){
38953    /*
38954    ** NOTE: We are dealing with a relative path name and the data
38955    **       directory has been set.  Therefore, use it as the basis
38956    **       for converting the relative path name to an absolute
38957    **       one by prepending the data directory and a backslash.
38958    */
38959    sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s%c%s",
38960                     sqlite3_data_directory, winGetDirSep(), zRelative);
38961    return SQLITE_OK;
38962  }
38963  zConverted = winConvertFromUtf8Filename(zRelative);
38964  if( zConverted==0 ){
38965    return SQLITE_IOERR_NOMEM;
38966  }
38967  if( osIsNT() ){
38968    LPWSTR zTemp;
38969    nByte = osGetFullPathNameW((LPCWSTR)zConverted, 0, 0, 0);
38970    if( nByte==0 ){
38971      sqlite3_free(zConverted);
38972      return winLogError(SQLITE_CANTOPEN_FULLPATH, osGetLastError(),
38973                         "winFullPathname1", zRelative);
38974    }
38975    nByte += 3;
38976    zTemp = sqlite3MallocZero( nByte*sizeof(zTemp[0]) );
38977    if( zTemp==0 ){
38978      sqlite3_free(zConverted);
38979      return SQLITE_IOERR_NOMEM;
38980    }
38981    nByte = osGetFullPathNameW((LPCWSTR)zConverted, nByte, zTemp, 0);
38982    if( nByte==0 ){
38983      sqlite3_free(zConverted);
38984      sqlite3_free(zTemp);
38985      return winLogError(SQLITE_CANTOPEN_FULLPATH, osGetLastError(),
38986                         "winFullPathname2", zRelative);
38987    }
38988    sqlite3_free(zConverted);
38989    zOut = winUnicodeToUtf8(zTemp);
38990    sqlite3_free(zTemp);
38991  }
38992#ifdef SQLITE_WIN32_HAS_ANSI
38993  else{
38994    char *zTemp;
38995    nByte = osGetFullPathNameA((char*)zConverted, 0, 0, 0);
38996    if( nByte==0 ){
38997      sqlite3_free(zConverted);
38998      return winLogError(SQLITE_CANTOPEN_FULLPATH, osGetLastError(),
38999                         "winFullPathname3", zRelative);
39000    }
39001    nByte += 3;
39002    zTemp = sqlite3MallocZero( nByte*sizeof(zTemp[0]) );
39003    if( zTemp==0 ){
39004      sqlite3_free(zConverted);
39005      return SQLITE_IOERR_NOMEM;
39006    }
39007    nByte = osGetFullPathNameA((char*)zConverted, nByte, zTemp, 0);
39008    if( nByte==0 ){
39009      sqlite3_free(zConverted);
39010      sqlite3_free(zTemp);
39011      return winLogError(SQLITE_CANTOPEN_FULLPATH, osGetLastError(),
39012                         "winFullPathname4", zRelative);
39013    }
39014    sqlite3_free(zConverted);
39015    zOut = sqlite3_win32_mbcs_to_utf8(zTemp);
39016    sqlite3_free(zTemp);
39017  }
39018#endif
39019  if( zOut ){
39020    sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s", zOut);
39021    sqlite3_free(zOut);
39022    return SQLITE_OK;
39023  }else{
39024    return SQLITE_IOERR_NOMEM;
39025  }
39026#endif
39027}
39028
39029#ifndef SQLITE_OMIT_LOAD_EXTENSION
39030/*
39031** Interfaces for opening a shared library, finding entry points
39032** within the shared library, and closing the shared library.
39033*/
39034static void *winDlOpen(sqlite3_vfs *pVfs, const char *zFilename){
39035  HANDLE h;
39036#if defined(__CYGWIN__)
39037  int nFull = pVfs->mxPathname+1;
39038  char *zFull = sqlite3MallocZero( nFull );
39039  void *zConverted = 0;
39040  if( zFull==0 ){
39041    OSTRACE(("DLOPEN name=%s, handle=%p\n", zFilename, (void*)0));
39042    return 0;
39043  }
39044  if( winFullPathname(pVfs, zFilename, nFull, zFull)!=SQLITE_OK ){
39045    sqlite3_free(zFull);
39046    OSTRACE(("DLOPEN name=%s, handle=%p\n", zFilename, (void*)0));
39047    return 0;
39048  }
39049  zConverted = winConvertFromUtf8Filename(zFull);
39050  sqlite3_free(zFull);
39051#else
39052  void *zConverted = winConvertFromUtf8Filename(zFilename);
39053  UNUSED_PARAMETER(pVfs);
39054#endif
39055  if( zConverted==0 ){
39056    OSTRACE(("DLOPEN name=%s, handle=%p\n", zFilename, (void*)0));
39057    return 0;
39058  }
39059  if( osIsNT() ){
39060#if SQLITE_OS_WINRT
39061    h = osLoadPackagedLibrary((LPCWSTR)zConverted, 0);
39062#else
39063    h = osLoadLibraryW((LPCWSTR)zConverted);
39064#endif
39065  }
39066#ifdef SQLITE_WIN32_HAS_ANSI
39067  else{
39068    h = osLoadLibraryA((char*)zConverted);
39069  }
39070#endif
39071  OSTRACE(("DLOPEN name=%s, handle=%p\n", zFilename, (void*)h));
39072  sqlite3_free(zConverted);
39073  return (void*)h;
39074}
39075static void winDlError(sqlite3_vfs *pVfs, int nBuf, char *zBufOut){
39076  UNUSED_PARAMETER(pVfs);
39077  winGetLastErrorMsg(osGetLastError(), nBuf, zBufOut);
39078}
39079static void (*winDlSym(sqlite3_vfs *pVfs,void *pH,const char *zSym))(void){
39080  FARPROC proc;
39081  UNUSED_PARAMETER(pVfs);
39082  proc = osGetProcAddressA((HANDLE)pH, zSym);
39083  OSTRACE(("DLSYM handle=%p, symbol=%s, address=%p\n",
39084           (void*)pH, zSym, (void*)proc));
39085  return (void(*)(void))proc;
39086}
39087static void winDlClose(sqlite3_vfs *pVfs, void *pHandle){
39088  UNUSED_PARAMETER(pVfs);
39089  osFreeLibrary((HANDLE)pHandle);
39090  OSTRACE(("DLCLOSE handle=%p\n", (void*)pHandle));
39091}
39092#else /* if SQLITE_OMIT_LOAD_EXTENSION is defined: */
39093  #define winDlOpen  0
39094  #define winDlError 0
39095  #define winDlSym   0
39096  #define winDlClose 0
39097#endif
39098
39099
39100/*
39101** Write up to nBuf bytes of randomness into zBuf.
39102*/
39103static int winRandomness(sqlite3_vfs *pVfs, int nBuf, char *zBuf){
39104  int n = 0;
39105  UNUSED_PARAMETER(pVfs);
39106#if defined(SQLITE_TEST) || defined(SQLITE_OMIT_RANDOMNESS)
39107  n = nBuf;
39108  memset(zBuf, 0, nBuf);
39109#else
39110  if( sizeof(SYSTEMTIME)<=nBuf-n ){
39111    SYSTEMTIME x;
39112    osGetSystemTime(&x);
39113    memcpy(&zBuf[n], &x, sizeof(x));
39114    n += sizeof(x);
39115  }
39116  if( sizeof(DWORD)<=nBuf-n ){
39117    DWORD pid = osGetCurrentProcessId();
39118    memcpy(&zBuf[n], &pid, sizeof(pid));
39119    n += sizeof(pid);
39120  }
39121#if SQLITE_OS_WINRT
39122  if( sizeof(ULONGLONG)<=nBuf-n ){
39123    ULONGLONG cnt = osGetTickCount64();
39124    memcpy(&zBuf[n], &cnt, sizeof(cnt));
39125    n += sizeof(cnt);
39126  }
39127#else
39128  if( sizeof(DWORD)<=nBuf-n ){
39129    DWORD cnt = osGetTickCount();
39130    memcpy(&zBuf[n], &cnt, sizeof(cnt));
39131    n += sizeof(cnt);
39132  }
39133#endif
39134  if( sizeof(LARGE_INTEGER)<=nBuf-n ){
39135    LARGE_INTEGER i;
39136    osQueryPerformanceCounter(&i);
39137    memcpy(&zBuf[n], &i, sizeof(i));
39138    n += sizeof(i);
39139  }
39140#if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && SQLITE_WIN32_USE_UUID
39141  if( sizeof(UUID)<=nBuf-n ){
39142    UUID id;
39143    memset(&id, 0, sizeof(UUID));
39144    osUuidCreate(&id);
39145    memcpy(&zBuf[n], &id, sizeof(UUID));
39146    n += sizeof(UUID);
39147  }
39148  if( sizeof(UUID)<=nBuf-n ){
39149    UUID id;
39150    memset(&id, 0, sizeof(UUID));
39151    osUuidCreateSequential(&id);
39152    memcpy(&zBuf[n], &id, sizeof(UUID));
39153    n += sizeof(UUID);
39154  }
39155#endif
39156#endif /* defined(SQLITE_TEST) || defined(SQLITE_ZERO_PRNG_SEED) */
39157  return n;
39158}
39159
39160
39161/*
39162** Sleep for a little while.  Return the amount of time slept.
39163*/
39164static int winSleep(sqlite3_vfs *pVfs, int microsec){
39165  sqlite3_win32_sleep((microsec+999)/1000);
39166  UNUSED_PARAMETER(pVfs);
39167  return ((microsec+999)/1000)*1000;
39168}
39169
39170/*
39171** The following variable, if set to a non-zero value, is interpreted as
39172** the number of seconds since 1970 and is used to set the result of
39173** sqlite3OsCurrentTime() during testing.
39174*/
39175#ifdef SQLITE_TEST
39176SQLITE_API int sqlite3_current_time = 0;  /* Fake system time in seconds since 1970. */
39177#endif
39178
39179/*
39180** Find the current time (in Universal Coordinated Time).  Write into *piNow
39181** the current time and date as a Julian Day number times 86_400_000.  In
39182** other words, write into *piNow the number of milliseconds since the Julian
39183** epoch of noon in Greenwich on November 24, 4714 B.C according to the
39184** proleptic Gregorian calendar.
39185**
39186** On success, return SQLITE_OK.  Return SQLITE_ERROR if the time and date
39187** cannot be found.
39188*/
39189static int winCurrentTimeInt64(sqlite3_vfs *pVfs, sqlite3_int64 *piNow){
39190  /* FILETIME structure is a 64-bit value representing the number of
39191     100-nanosecond intervals since January 1, 1601 (= JD 2305813.5).
39192  */
39193  FILETIME ft;
39194  static const sqlite3_int64 winFiletimeEpoch = 23058135*(sqlite3_int64)8640000;
39195#ifdef SQLITE_TEST
39196  static const sqlite3_int64 unixEpoch = 24405875*(sqlite3_int64)8640000;
39197#endif
39198  /* 2^32 - to avoid use of LL and warnings in gcc */
39199  static const sqlite3_int64 max32BitValue =
39200      (sqlite3_int64)2000000000 + (sqlite3_int64)2000000000 +
39201      (sqlite3_int64)294967296;
39202
39203#if SQLITE_OS_WINCE
39204  SYSTEMTIME time;
39205  osGetSystemTime(&time);
39206  /* if SystemTimeToFileTime() fails, it returns zero. */
39207  if (!osSystemTimeToFileTime(&time,&ft)){
39208    return SQLITE_ERROR;
39209  }
39210#else
39211  osGetSystemTimeAsFileTime( &ft );
39212#endif
39213
39214  *piNow = winFiletimeEpoch +
39215            ((((sqlite3_int64)ft.dwHighDateTime)*max32BitValue) +
39216               (sqlite3_int64)ft.dwLowDateTime)/(sqlite3_int64)10000;
39217
39218#ifdef SQLITE_TEST
39219  if( sqlite3_current_time ){
39220    *piNow = 1000*(sqlite3_int64)sqlite3_current_time + unixEpoch;
39221  }
39222#endif
39223  UNUSED_PARAMETER(pVfs);
39224  return SQLITE_OK;
39225}
39226
39227/*
39228** Find the current time (in Universal Coordinated Time).  Write the
39229** current time and date as a Julian Day number into *prNow and
39230** return 0.  Return 1 if the time and date cannot be found.
39231*/
39232static int winCurrentTime(sqlite3_vfs *pVfs, double *prNow){
39233  int rc;
39234  sqlite3_int64 i;
39235  rc = winCurrentTimeInt64(pVfs, &i);
39236  if( !rc ){
39237    *prNow = i/86400000.0;
39238  }
39239  return rc;
39240}
39241
39242/*
39243** The idea is that this function works like a combination of
39244** GetLastError() and FormatMessage() on Windows (or errno and
39245** strerror_r() on Unix). After an error is returned by an OS
39246** function, SQLite calls this function with zBuf pointing to
39247** a buffer of nBuf bytes. The OS layer should populate the
39248** buffer with a nul-terminated UTF-8 encoded error message
39249** describing the last IO error to have occurred within the calling
39250** thread.
39251**
39252** If the error message is too large for the supplied buffer,
39253** it should be truncated. The return value of xGetLastError
39254** is zero if the error message fits in the buffer, or non-zero
39255** otherwise (if the message was truncated). If non-zero is returned,
39256** then it is not necessary to include the nul-terminator character
39257** in the output buffer.
39258**
39259** Not supplying an error message will have no adverse effect
39260** on SQLite. It is fine to have an implementation that never
39261** returns an error message:
39262**
39263**   int xGetLastError(sqlite3_vfs *pVfs, int nBuf, char *zBuf){
39264**     assert(zBuf[0]=='\0');
39265**     return 0;
39266**   }
39267**
39268** However if an error message is supplied, it will be incorporated
39269** by sqlite into the error message available to the user using
39270** sqlite3_errmsg(), possibly making IO errors easier to debug.
39271*/
39272static int winGetLastError(sqlite3_vfs *pVfs, int nBuf, char *zBuf){
39273  UNUSED_PARAMETER(pVfs);
39274  return winGetLastErrorMsg(osGetLastError(), nBuf, zBuf);
39275}
39276
39277/*
39278** Initialize and deinitialize the operating system interface.
39279*/
39280SQLITE_API int SQLITE_STDCALL sqlite3_os_init(void){
39281  static sqlite3_vfs winVfs = {
39282    3,                   /* iVersion */
39283    sizeof(winFile),     /* szOsFile */
39284    SQLITE_WIN32_MAX_PATH_BYTES, /* mxPathname */
39285    0,                   /* pNext */
39286    "win32",             /* zName */
39287    0,                   /* pAppData */
39288    winOpen,             /* xOpen */
39289    winDelete,           /* xDelete */
39290    winAccess,           /* xAccess */
39291    winFullPathname,     /* xFullPathname */
39292    winDlOpen,           /* xDlOpen */
39293    winDlError,          /* xDlError */
39294    winDlSym,            /* xDlSym */
39295    winDlClose,          /* xDlClose */
39296    winRandomness,       /* xRandomness */
39297    winSleep,            /* xSleep */
39298    winCurrentTime,      /* xCurrentTime */
39299    winGetLastError,     /* xGetLastError */
39300    winCurrentTimeInt64, /* xCurrentTimeInt64 */
39301    winSetSystemCall,    /* xSetSystemCall */
39302    winGetSystemCall,    /* xGetSystemCall */
39303    winNextSystemCall,   /* xNextSystemCall */
39304  };
39305#if defined(SQLITE_WIN32_HAS_WIDE)
39306  static sqlite3_vfs winLongPathVfs = {
39307    3,                   /* iVersion */
39308    sizeof(winFile),     /* szOsFile */
39309    SQLITE_WINNT_MAX_PATH_BYTES, /* mxPathname */
39310    0,                   /* pNext */
39311    "win32-longpath",    /* zName */
39312    0,                   /* pAppData */
39313    winOpen,             /* xOpen */
39314    winDelete,           /* xDelete */
39315    winAccess,           /* xAccess */
39316    winFullPathname,     /* xFullPathname */
39317    winDlOpen,           /* xDlOpen */
39318    winDlError,          /* xDlError */
39319    winDlSym,            /* xDlSym */
39320    winDlClose,          /* xDlClose */
39321    winRandomness,       /* xRandomness */
39322    winSleep,            /* xSleep */
39323    winCurrentTime,      /* xCurrentTime */
39324    winGetLastError,     /* xGetLastError */
39325    winCurrentTimeInt64, /* xCurrentTimeInt64 */
39326    winSetSystemCall,    /* xSetSystemCall */
39327    winGetSystemCall,    /* xGetSystemCall */
39328    winNextSystemCall,   /* xNextSystemCall */
39329  };
39330#endif
39331
39332  /* Double-check that the aSyscall[] array has been constructed
39333  ** correctly.  See ticket [bb3a86e890c8e96ab] */
39334  assert( ArraySize(aSyscall)==80 );
39335
39336  /* get memory map allocation granularity */
39337  memset(&winSysInfo, 0, sizeof(SYSTEM_INFO));
39338#if SQLITE_OS_WINRT
39339  osGetNativeSystemInfo(&winSysInfo);
39340#else
39341  osGetSystemInfo(&winSysInfo);
39342#endif
39343  assert( winSysInfo.dwAllocationGranularity>0 );
39344  assert( winSysInfo.dwPageSize>0 );
39345
39346  sqlite3_vfs_register(&winVfs, 1);
39347
39348#if defined(SQLITE_WIN32_HAS_WIDE)
39349  sqlite3_vfs_register(&winLongPathVfs, 0);
39350#endif
39351
39352  return SQLITE_OK;
39353}
39354
39355SQLITE_API int SQLITE_STDCALL sqlite3_os_end(void){
39356#if SQLITE_OS_WINRT
39357  if( sleepObj!=NULL ){
39358    osCloseHandle(sleepObj);
39359    sleepObj = NULL;
39360  }
39361#endif
39362  return SQLITE_OK;
39363}
39364
39365#endif /* SQLITE_OS_WIN */
39366
39367/************** End of os_win.c **********************************************/
39368/************** Begin file bitvec.c ******************************************/
39369/*
39370** 2008 February 16
39371**
39372** The author disclaims copyright to this source code.  In place of
39373** a legal notice, here is a blessing:
39374**
39375**    May you do good and not evil.
39376**    May you find forgiveness for yourself and forgive others.
39377**    May you share freely, never taking more than you give.
39378**
39379*************************************************************************
39380** This file implements an object that represents a fixed-length
39381** bitmap.  Bits are numbered starting with 1.
39382**
39383** A bitmap is used to record which pages of a database file have been
39384** journalled during a transaction, or which pages have the "dont-write"
39385** property.  Usually only a few pages are meet either condition.
39386** So the bitmap is usually sparse and has low cardinality.
39387** But sometimes (for example when during a DROP of a large table) most
39388** or all of the pages in a database can get journalled.  In those cases,
39389** the bitmap becomes dense with high cardinality.  The algorithm needs
39390** to handle both cases well.
39391**
39392** The size of the bitmap is fixed when the object is created.
39393**
39394** All bits are clear when the bitmap is created.  Individual bits
39395** may be set or cleared one at a time.
39396**
39397** Test operations are about 100 times more common that set operations.
39398** Clear operations are exceedingly rare.  There are usually between
39399** 5 and 500 set operations per Bitvec object, though the number of sets can
39400** sometimes grow into tens of thousands or larger.  The size of the
39401** Bitvec object is the number of pages in the database file at the
39402** start of a transaction, and is thus usually less than a few thousand,
39403** but can be as large as 2 billion for a really big database.
39404*/
39405/* #include "sqliteInt.h" */
39406
39407/* Size of the Bitvec structure in bytes. */
39408#define BITVEC_SZ        512
39409
39410/* Round the union size down to the nearest pointer boundary, since that's how
39411** it will be aligned within the Bitvec struct. */
39412#define BITVEC_USIZE     (((BITVEC_SZ-(3*sizeof(u32)))/sizeof(Bitvec*))*sizeof(Bitvec*))
39413
39414/* Type of the array "element" for the bitmap representation.
39415** Should be a power of 2, and ideally, evenly divide into BITVEC_USIZE.
39416** Setting this to the "natural word" size of your CPU may improve
39417** performance. */
39418#define BITVEC_TELEM     u8
39419/* Size, in bits, of the bitmap element. */
39420#define BITVEC_SZELEM    8
39421/* Number of elements in a bitmap array. */
39422#define BITVEC_NELEM     (BITVEC_USIZE/sizeof(BITVEC_TELEM))
39423/* Number of bits in the bitmap array. */
39424#define BITVEC_NBIT      (BITVEC_NELEM*BITVEC_SZELEM)
39425
39426/* Number of u32 values in hash table. */
39427#define BITVEC_NINT      (BITVEC_USIZE/sizeof(u32))
39428/* Maximum number of entries in hash table before
39429** sub-dividing and re-hashing. */
39430#define BITVEC_MXHASH    (BITVEC_NINT/2)
39431/* Hashing function for the aHash representation.
39432** Empirical testing showed that the *37 multiplier
39433** (an arbitrary prime)in the hash function provided
39434** no fewer collisions than the no-op *1. */
39435#define BITVEC_HASH(X)   (((X)*1)%BITVEC_NINT)
39436
39437#define BITVEC_NPTR      (BITVEC_USIZE/sizeof(Bitvec *))
39438
39439
39440/*
39441** A bitmap is an instance of the following structure.
39442**
39443** This bitmap records the existence of zero or more bits
39444** with values between 1 and iSize, inclusive.
39445**
39446** There are three possible representations of the bitmap.
39447** If iSize<=BITVEC_NBIT, then Bitvec.u.aBitmap[] is a straight
39448** bitmap.  The least significant bit is bit 1.
39449**
39450** If iSize>BITVEC_NBIT and iDivisor==0 then Bitvec.u.aHash[] is
39451** a hash table that will hold up to BITVEC_MXHASH distinct values.
39452**
39453** Otherwise, the value i is redirected into one of BITVEC_NPTR
39454** sub-bitmaps pointed to by Bitvec.u.apSub[].  Each subbitmap
39455** handles up to iDivisor separate values of i.  apSub[0] holds
39456** values between 1 and iDivisor.  apSub[1] holds values between
39457** iDivisor+1 and 2*iDivisor.  apSub[N] holds values between
39458** N*iDivisor+1 and (N+1)*iDivisor.  Each subbitmap is normalized
39459** to hold deal with values between 1 and iDivisor.
39460*/
39461struct Bitvec {
39462  u32 iSize;      /* Maximum bit index.  Max iSize is 4,294,967,296. */
39463  u32 nSet;       /* Number of bits that are set - only valid for aHash
39464                  ** element.  Max is BITVEC_NINT.  For BITVEC_SZ of 512,
39465                  ** this would be 125. */
39466  u32 iDivisor;   /* Number of bits handled by each apSub[] entry. */
39467                  /* Should >=0 for apSub element. */
39468                  /* Max iDivisor is max(u32) / BITVEC_NPTR + 1.  */
39469                  /* For a BITVEC_SZ of 512, this would be 34,359,739. */
39470  union {
39471    BITVEC_TELEM aBitmap[BITVEC_NELEM];    /* Bitmap representation */
39472    u32 aHash[BITVEC_NINT];      /* Hash table representation */
39473    Bitvec *apSub[BITVEC_NPTR];  /* Recursive representation */
39474  } u;
39475};
39476
39477/*
39478** Create a new bitmap object able to handle bits between 0 and iSize,
39479** inclusive.  Return a pointer to the new object.  Return NULL if
39480** malloc fails.
39481*/
39482SQLITE_PRIVATE Bitvec *sqlite3BitvecCreate(u32 iSize){
39483  Bitvec *p;
39484  assert( sizeof(*p)==BITVEC_SZ );
39485  p = sqlite3MallocZero( sizeof(*p) );
39486  if( p ){
39487    p->iSize = iSize;
39488  }
39489  return p;
39490}
39491
39492/*
39493** Check to see if the i-th bit is set.  Return true or false.
39494** If p is NULL (if the bitmap has not been created) or if
39495** i is out of range, then return false.
39496*/
39497SQLITE_PRIVATE int sqlite3BitvecTestNotNull(Bitvec *p, u32 i){
39498  assert( p!=0 );
39499  i--;
39500  if( i>=p->iSize ) return 0;
39501  while( p->iDivisor ){
39502    u32 bin = i/p->iDivisor;
39503    i = i%p->iDivisor;
39504    p = p->u.apSub[bin];
39505    if (!p) {
39506      return 0;
39507    }
39508  }
39509  if( p->iSize<=BITVEC_NBIT ){
39510    return (p->u.aBitmap[i/BITVEC_SZELEM] & (1<<(i&(BITVEC_SZELEM-1))))!=0;
39511  } else{
39512    u32 h = BITVEC_HASH(i++);
39513    while( p->u.aHash[h] ){
39514      if( p->u.aHash[h]==i ) return 1;
39515      h = (h+1) % BITVEC_NINT;
39516    }
39517    return 0;
39518  }
39519}
39520SQLITE_PRIVATE int sqlite3BitvecTest(Bitvec *p, u32 i){
39521  return p!=0 && sqlite3BitvecTestNotNull(p,i);
39522}
39523
39524/*
39525** Set the i-th bit.  Return 0 on success and an error code if
39526** anything goes wrong.
39527**
39528** This routine might cause sub-bitmaps to be allocated.  Failing
39529** to get the memory needed to hold the sub-bitmap is the only
39530** that can go wrong with an insert, assuming p and i are valid.
39531**
39532** The calling function must ensure that p is a valid Bitvec object
39533** and that the value for "i" is within range of the Bitvec object.
39534** Otherwise the behavior is undefined.
39535*/
39536SQLITE_PRIVATE int sqlite3BitvecSet(Bitvec *p, u32 i){
39537  u32 h;
39538  if( p==0 ) return SQLITE_OK;
39539  assert( i>0 );
39540  assert( i<=p->iSize );
39541  i--;
39542  while((p->iSize > BITVEC_NBIT) && p->iDivisor) {
39543    u32 bin = i/p->iDivisor;
39544    i = i%p->iDivisor;
39545    if( p->u.apSub[bin]==0 ){
39546      p->u.apSub[bin] = sqlite3BitvecCreate( p->iDivisor );
39547      if( p->u.apSub[bin]==0 ) return SQLITE_NOMEM;
39548    }
39549    p = p->u.apSub[bin];
39550  }
39551  if( p->iSize<=BITVEC_NBIT ){
39552    p->u.aBitmap[i/BITVEC_SZELEM] |= 1 << (i&(BITVEC_SZELEM-1));
39553    return SQLITE_OK;
39554  }
39555  h = BITVEC_HASH(i++);
39556  /* if there wasn't a hash collision, and this doesn't */
39557  /* completely fill the hash, then just add it without */
39558  /* worring about sub-dividing and re-hashing. */
39559  if( !p->u.aHash[h] ){
39560    if (p->nSet<(BITVEC_NINT-1)) {
39561      goto bitvec_set_end;
39562    } else {
39563      goto bitvec_set_rehash;
39564    }
39565  }
39566  /* there was a collision, check to see if it's already */
39567  /* in hash, if not, try to find a spot for it */
39568  do {
39569    if( p->u.aHash[h]==i ) return SQLITE_OK;
39570    h++;
39571    if( h>=BITVEC_NINT ) h = 0;
39572  } while( p->u.aHash[h] );
39573  /* we didn't find it in the hash.  h points to the first */
39574  /* available free spot. check to see if this is going to */
39575  /* make our hash too "full".  */
39576bitvec_set_rehash:
39577  if( p->nSet>=BITVEC_MXHASH ){
39578    unsigned int j;
39579    int rc;
39580    u32 *aiValues = sqlite3StackAllocRaw(0, sizeof(p->u.aHash));
39581    if( aiValues==0 ){
39582      return SQLITE_NOMEM;
39583    }else{
39584      memcpy(aiValues, p->u.aHash, sizeof(p->u.aHash));
39585      memset(p->u.apSub, 0, sizeof(p->u.apSub));
39586      p->iDivisor = (p->iSize + BITVEC_NPTR - 1)/BITVEC_NPTR;
39587      rc = sqlite3BitvecSet(p, i);
39588      for(j=0; j<BITVEC_NINT; j++){
39589        if( aiValues[j] ) rc |= sqlite3BitvecSet(p, aiValues[j]);
39590      }
39591      sqlite3StackFree(0, aiValues);
39592      return rc;
39593    }
39594  }
39595bitvec_set_end:
39596  p->nSet++;
39597  p->u.aHash[h] = i;
39598  return SQLITE_OK;
39599}
39600
39601/*
39602** Clear the i-th bit.
39603**
39604** pBuf must be a pointer to at least BITVEC_SZ bytes of temporary storage
39605** that BitvecClear can use to rebuilt its hash table.
39606*/
39607SQLITE_PRIVATE void sqlite3BitvecClear(Bitvec *p, u32 i, void *pBuf){
39608  if( p==0 ) return;
39609  assert( i>0 );
39610  i--;
39611  while( p->iDivisor ){
39612    u32 bin = i/p->iDivisor;
39613    i = i%p->iDivisor;
39614    p = p->u.apSub[bin];
39615    if (!p) {
39616      return;
39617    }
39618  }
39619  if( p->iSize<=BITVEC_NBIT ){
39620    p->u.aBitmap[i/BITVEC_SZELEM] &= ~(1 << (i&(BITVEC_SZELEM-1)));
39621  }else{
39622    unsigned int j;
39623    u32 *aiValues = pBuf;
39624    memcpy(aiValues, p->u.aHash, sizeof(p->u.aHash));
39625    memset(p->u.aHash, 0, sizeof(p->u.aHash));
39626    p->nSet = 0;
39627    for(j=0; j<BITVEC_NINT; j++){
39628      if( aiValues[j] && aiValues[j]!=(i+1) ){
39629        u32 h = BITVEC_HASH(aiValues[j]-1);
39630        p->nSet++;
39631        while( p->u.aHash[h] ){
39632          h++;
39633          if( h>=BITVEC_NINT ) h = 0;
39634        }
39635        p->u.aHash[h] = aiValues[j];
39636      }
39637    }
39638  }
39639}
39640
39641/*
39642** Destroy a bitmap object.  Reclaim all memory used.
39643*/
39644SQLITE_PRIVATE void sqlite3BitvecDestroy(Bitvec *p){
39645  if( p==0 ) return;
39646  if( p->iDivisor ){
39647    unsigned int i;
39648    for(i=0; i<BITVEC_NPTR; i++){
39649      sqlite3BitvecDestroy(p->u.apSub[i]);
39650    }
39651  }
39652  sqlite3_free(p);
39653}
39654
39655/*
39656** Return the value of the iSize parameter specified when Bitvec *p
39657** was created.
39658*/
39659SQLITE_PRIVATE u32 sqlite3BitvecSize(Bitvec *p){
39660  return p->iSize;
39661}
39662
39663#ifndef SQLITE_OMIT_BUILTIN_TEST
39664/*
39665** Let V[] be an array of unsigned characters sufficient to hold
39666** up to N bits.  Let I be an integer between 0 and N.  0<=I<N.
39667** Then the following macros can be used to set, clear, or test
39668** individual bits within V.
39669*/
39670#define SETBIT(V,I)      V[I>>3] |= (1<<(I&7))
39671#define CLEARBIT(V,I)    V[I>>3] &= ~(1<<(I&7))
39672#define TESTBIT(V,I)     (V[I>>3]&(1<<(I&7)))!=0
39673
39674/*
39675** This routine runs an extensive test of the Bitvec code.
39676**
39677** The input is an array of integers that acts as a program
39678** to test the Bitvec.  The integers are opcodes followed
39679** by 0, 1, or 3 operands, depending on the opcode.  Another
39680** opcode follows immediately after the last operand.
39681**
39682** There are 6 opcodes numbered from 0 through 5.  0 is the
39683** "halt" opcode and causes the test to end.
39684**
39685**    0          Halt and return the number of errors
39686**    1 N S X    Set N bits beginning with S and incrementing by X
39687**    2 N S X    Clear N bits beginning with S and incrementing by X
39688**    3 N        Set N randomly chosen bits
39689**    4 N        Clear N randomly chosen bits
39690**    5 N S X    Set N bits from S increment X in array only, not in bitvec
39691**
39692** The opcodes 1 through 4 perform set and clear operations are performed
39693** on both a Bitvec object and on a linear array of bits obtained from malloc.
39694** Opcode 5 works on the linear array only, not on the Bitvec.
39695** Opcode 5 is used to deliberately induce a fault in order to
39696** confirm that error detection works.
39697**
39698** At the conclusion of the test the linear array is compared
39699** against the Bitvec object.  If there are any differences,
39700** an error is returned.  If they are the same, zero is returned.
39701**
39702** If a memory allocation error occurs, return -1.
39703*/
39704SQLITE_PRIVATE int sqlite3BitvecBuiltinTest(int sz, int *aOp){
39705  Bitvec *pBitvec = 0;
39706  unsigned char *pV = 0;
39707  int rc = -1;
39708  int i, nx, pc, op;
39709  void *pTmpSpace;
39710
39711  /* Allocate the Bitvec to be tested and a linear array of
39712  ** bits to act as the reference */
39713  pBitvec = sqlite3BitvecCreate( sz );
39714  pV = sqlite3MallocZero( (sz+7)/8 + 1 );
39715  pTmpSpace = sqlite3_malloc64(BITVEC_SZ);
39716  if( pBitvec==0 || pV==0 || pTmpSpace==0  ) goto bitvec_end;
39717
39718  /* NULL pBitvec tests */
39719  sqlite3BitvecSet(0, 1);
39720  sqlite3BitvecClear(0, 1, pTmpSpace);
39721
39722  /* Run the program */
39723  pc = 0;
39724  while( (op = aOp[pc])!=0 ){
39725    switch( op ){
39726      case 1:
39727      case 2:
39728      case 5: {
39729        nx = 4;
39730        i = aOp[pc+2] - 1;
39731        aOp[pc+2] += aOp[pc+3];
39732        break;
39733      }
39734      case 3:
39735      case 4:
39736      default: {
39737        nx = 2;
39738        sqlite3_randomness(sizeof(i), &i);
39739        break;
39740      }
39741    }
39742    if( (--aOp[pc+1]) > 0 ) nx = 0;
39743    pc += nx;
39744    i = (i & 0x7fffffff)%sz;
39745    if( (op & 1)!=0 ){
39746      SETBIT(pV, (i+1));
39747      if( op!=5 ){
39748        if( sqlite3BitvecSet(pBitvec, i+1) ) goto bitvec_end;
39749      }
39750    }else{
39751      CLEARBIT(pV, (i+1));
39752      sqlite3BitvecClear(pBitvec, i+1, pTmpSpace);
39753    }
39754  }
39755
39756  /* Test to make sure the linear array exactly matches the
39757  ** Bitvec object.  Start with the assumption that they do
39758  ** match (rc==0).  Change rc to non-zero if a discrepancy
39759  ** is found.
39760  */
39761  rc = sqlite3BitvecTest(0,0) + sqlite3BitvecTest(pBitvec, sz+1)
39762          + sqlite3BitvecTest(pBitvec, 0)
39763          + (sqlite3BitvecSize(pBitvec) - sz);
39764  for(i=1; i<=sz; i++){
39765    if(  (TESTBIT(pV,i))!=sqlite3BitvecTest(pBitvec,i) ){
39766      rc = i;
39767      break;
39768    }
39769  }
39770
39771  /* Free allocated structure */
39772bitvec_end:
39773  sqlite3_free(pTmpSpace);
39774  sqlite3_free(pV);
39775  sqlite3BitvecDestroy(pBitvec);
39776  return rc;
39777}
39778#endif /* SQLITE_OMIT_BUILTIN_TEST */
39779
39780/************** End of bitvec.c **********************************************/
39781/************** Begin file pcache.c ******************************************/
39782/*
39783** 2008 August 05
39784**
39785** The author disclaims copyright to this source code.  In place of
39786** a legal notice, here is a blessing:
39787**
39788**    May you do good and not evil.
39789**    May you find forgiveness for yourself and forgive others.
39790**    May you share freely, never taking more than you give.
39791**
39792*************************************************************************
39793** This file implements that page cache.
39794*/
39795/* #include "sqliteInt.h" */
39796
39797/*
39798** A complete page cache is an instance of this structure.
39799*/
39800struct PCache {
39801  PgHdr *pDirty, *pDirtyTail;         /* List of dirty pages in LRU order */
39802  PgHdr *pSynced;                     /* Last synced page in dirty page list */
39803  int nRef;                           /* Number of referenced pages */
39804  int szCache;                        /* Configured cache size */
39805  int szPage;                         /* Size of every page in this cache */
39806  int szExtra;                        /* Size of extra space for each page */
39807  u8 bPurgeable;                      /* True if pages are on backing store */
39808  u8 eCreate;                         /* eCreate value for for xFetch() */
39809  int (*xStress)(void*,PgHdr*);       /* Call to try make a page clean */
39810  void *pStress;                      /* Argument to xStress */
39811  sqlite3_pcache *pCache;             /* Pluggable cache module */
39812};
39813
39814/********************************** Linked List Management ********************/
39815
39816/* Allowed values for second argument to pcacheManageDirtyList() */
39817#define PCACHE_DIRTYLIST_REMOVE   1    /* Remove pPage from dirty list */
39818#define PCACHE_DIRTYLIST_ADD      2    /* Add pPage to the dirty list */
39819#define PCACHE_DIRTYLIST_FRONT    3    /* Move pPage to the front of the list */
39820
39821/*
39822** Manage pPage's participation on the dirty list.  Bits of the addRemove
39823** argument determines what operation to do.  The 0x01 bit means first
39824** remove pPage from the dirty list.  The 0x02 means add pPage back to
39825** the dirty list.  Doing both moves pPage to the front of the dirty list.
39826*/
39827static void pcacheManageDirtyList(PgHdr *pPage, u8 addRemove){
39828  PCache *p = pPage->pCache;
39829
39830  if( addRemove & PCACHE_DIRTYLIST_REMOVE ){
39831    assert( pPage->pDirtyNext || pPage==p->pDirtyTail );
39832    assert( pPage->pDirtyPrev || pPage==p->pDirty );
39833
39834    /* Update the PCache1.pSynced variable if necessary. */
39835    if( p->pSynced==pPage ){
39836      PgHdr *pSynced = pPage->pDirtyPrev;
39837      while( pSynced && (pSynced->flags&PGHDR_NEED_SYNC) ){
39838        pSynced = pSynced->pDirtyPrev;
39839      }
39840      p->pSynced = pSynced;
39841    }
39842
39843    if( pPage->pDirtyNext ){
39844      pPage->pDirtyNext->pDirtyPrev = pPage->pDirtyPrev;
39845    }else{
39846      assert( pPage==p->pDirtyTail );
39847      p->pDirtyTail = pPage->pDirtyPrev;
39848    }
39849    if( pPage->pDirtyPrev ){
39850      pPage->pDirtyPrev->pDirtyNext = pPage->pDirtyNext;
39851    }else{
39852      assert( pPage==p->pDirty );
39853      p->pDirty = pPage->pDirtyNext;
39854      if( p->pDirty==0 && p->bPurgeable ){
39855        assert( p->eCreate==1 );
39856        p->eCreate = 2;
39857      }
39858    }
39859    pPage->pDirtyNext = 0;
39860    pPage->pDirtyPrev = 0;
39861  }
39862  if( addRemove & PCACHE_DIRTYLIST_ADD ){
39863    assert( pPage->pDirtyNext==0 && pPage->pDirtyPrev==0 && p->pDirty!=pPage );
39864
39865    pPage->pDirtyNext = p->pDirty;
39866    if( pPage->pDirtyNext ){
39867      assert( pPage->pDirtyNext->pDirtyPrev==0 );
39868      pPage->pDirtyNext->pDirtyPrev = pPage;
39869    }else{
39870      p->pDirtyTail = pPage;
39871      if( p->bPurgeable ){
39872        assert( p->eCreate==2 );
39873        p->eCreate = 1;
39874      }
39875    }
39876    p->pDirty = pPage;
39877    if( !p->pSynced && 0==(pPage->flags&PGHDR_NEED_SYNC) ){
39878      p->pSynced = pPage;
39879    }
39880  }
39881}
39882
39883/*
39884** Wrapper around the pluggable caches xUnpin method. If the cache is
39885** being used for an in-memory database, this function is a no-op.
39886*/
39887static void pcacheUnpin(PgHdr *p){
39888  if( p->pCache->bPurgeable ){
39889    sqlite3GlobalConfig.pcache2.xUnpin(p->pCache->pCache, p->pPage, 0);
39890  }
39891}
39892
39893/*
39894** Compute the number of pages of cache requested.  p->szCache is the
39895** cache size requested by the "PRAGMA cache_size" statement.
39896**
39897**
39898*/
39899static int numberOfCachePages(PCache *p){
39900  if( p->szCache>=0 ){
39901    /* IMPLEMENTATION-OF: R-42059-47211 If the argument N is positive then the
39902    ** suggested cache size is set to N. */
39903    return p->szCache;
39904  }else{
39905    /* IMPLEMENTATION-OF: R-61436-13639 If the argument N is negative, then
39906    ** the number of cache pages is adjusted to use approximately abs(N*1024)
39907    ** bytes of memory. */
39908    return (int)((-1024*(i64)p->szCache)/(p->szPage+p->szExtra));
39909  }
39910}
39911
39912/*************************************************** General Interfaces ******
39913**
39914** Initialize and shutdown the page cache subsystem. Neither of these
39915** functions are threadsafe.
39916*/
39917SQLITE_PRIVATE int sqlite3PcacheInitialize(void){
39918  if( sqlite3GlobalConfig.pcache2.xInit==0 ){
39919    /* IMPLEMENTATION-OF: R-26801-64137 If the xInit() method is NULL, then the
39920    ** built-in default page cache is used instead of the application defined
39921    ** page cache. */
39922    sqlite3PCacheSetDefault();
39923  }
39924  return sqlite3GlobalConfig.pcache2.xInit(sqlite3GlobalConfig.pcache2.pArg);
39925}
39926SQLITE_PRIVATE void sqlite3PcacheShutdown(void){
39927  if( sqlite3GlobalConfig.pcache2.xShutdown ){
39928    /* IMPLEMENTATION-OF: R-26000-56589 The xShutdown() method may be NULL. */
39929    sqlite3GlobalConfig.pcache2.xShutdown(sqlite3GlobalConfig.pcache2.pArg);
39930  }
39931}
39932
39933/*
39934** Return the size in bytes of a PCache object.
39935*/
39936SQLITE_PRIVATE int sqlite3PcacheSize(void){ return sizeof(PCache); }
39937
39938/*
39939** Create a new PCache object. Storage space to hold the object
39940** has already been allocated and is passed in as the p pointer.
39941** The caller discovers how much space needs to be allocated by
39942** calling sqlite3PcacheSize().
39943*/
39944SQLITE_PRIVATE int sqlite3PcacheOpen(
39945  int szPage,                  /* Size of every page */
39946  int szExtra,                 /* Extra space associated with each page */
39947  int bPurgeable,              /* True if pages are on backing store */
39948  int (*xStress)(void*,PgHdr*),/* Call to try to make pages clean */
39949  void *pStress,               /* Argument to xStress */
39950  PCache *p                    /* Preallocated space for the PCache */
39951){
39952  memset(p, 0, sizeof(PCache));
39953  p->szPage = 1;
39954  p->szExtra = szExtra;
39955  p->bPurgeable = bPurgeable;
39956  p->eCreate = 2;
39957  p->xStress = xStress;
39958  p->pStress = pStress;
39959  p->szCache = 100;
39960  return sqlite3PcacheSetPageSize(p, szPage);
39961}
39962
39963/*
39964** Change the page size for PCache object. The caller must ensure that there
39965** are no outstanding page references when this function is called.
39966*/
39967SQLITE_PRIVATE int sqlite3PcacheSetPageSize(PCache *pCache, int szPage){
39968  assert( pCache->nRef==0 && pCache->pDirty==0 );
39969  if( pCache->szPage ){
39970    sqlite3_pcache *pNew;
39971    pNew = sqlite3GlobalConfig.pcache2.xCreate(
39972                szPage, pCache->szExtra + ROUND8(sizeof(PgHdr)),
39973                pCache->bPurgeable
39974    );
39975    if( pNew==0 ) return SQLITE_NOMEM;
39976    sqlite3GlobalConfig.pcache2.xCachesize(pNew, numberOfCachePages(pCache));
39977    if( pCache->pCache ){
39978      sqlite3GlobalConfig.pcache2.xDestroy(pCache->pCache);
39979    }
39980    pCache->pCache = pNew;
39981    pCache->szPage = szPage;
39982  }
39983  return SQLITE_OK;
39984}
39985
39986/*
39987** Try to obtain a page from the cache.
39988**
39989** This routine returns a pointer to an sqlite3_pcache_page object if
39990** such an object is already in cache, or if a new one is created.
39991** This routine returns a NULL pointer if the object was not in cache
39992** and could not be created.
39993**
39994** The createFlags should be 0 to check for existing pages and should
39995** be 3 (not 1, but 3) to try to create a new page.
39996**
39997** If the createFlag is 0, then NULL is always returned if the page
39998** is not already in the cache.  If createFlag is 1, then a new page
39999** is created only if that can be done without spilling dirty pages
40000** and without exceeding the cache size limit.
40001**
40002** The caller needs to invoke sqlite3PcacheFetchFinish() to properly
40003** initialize the sqlite3_pcache_page object and convert it into a
40004** PgHdr object.  The sqlite3PcacheFetch() and sqlite3PcacheFetchFinish()
40005** routines are split this way for performance reasons. When separated
40006** they can both (usually) operate without having to push values to
40007** the stack on entry and pop them back off on exit, which saves a
40008** lot of pushing and popping.
40009*/
40010SQLITE_PRIVATE sqlite3_pcache_page *sqlite3PcacheFetch(
40011  PCache *pCache,       /* Obtain the page from this cache */
40012  Pgno pgno,            /* Page number to obtain */
40013  int createFlag        /* If true, create page if it does not exist already */
40014){
40015  int eCreate;
40016
40017  assert( pCache!=0 );
40018  assert( pCache->pCache!=0 );
40019  assert( createFlag==3 || createFlag==0 );
40020  assert( pgno>0 );
40021
40022  /* eCreate defines what to do if the page does not exist.
40023  **    0     Do not allocate a new page.  (createFlag==0)
40024  **    1     Allocate a new page if doing so is inexpensive.
40025  **          (createFlag==1 AND bPurgeable AND pDirty)
40026  **    2     Allocate a new page even it doing so is difficult.
40027  **          (createFlag==1 AND !(bPurgeable AND pDirty)
40028  */
40029  eCreate = createFlag & pCache->eCreate;
40030  assert( eCreate==0 || eCreate==1 || eCreate==2 );
40031  assert( createFlag==0 || pCache->eCreate==eCreate );
40032  assert( createFlag==0 || eCreate==1+(!pCache->bPurgeable||!pCache->pDirty) );
40033  return sqlite3GlobalConfig.pcache2.xFetch(pCache->pCache, pgno, eCreate);
40034}
40035
40036/*
40037** If the sqlite3PcacheFetch() routine is unable to allocate a new
40038** page because new clean pages are available for reuse and the cache
40039** size limit has been reached, then this routine can be invoked to
40040** try harder to allocate a page.  This routine might invoke the stress
40041** callback to spill dirty pages to the journal.  It will then try to
40042** allocate the new page and will only fail to allocate a new page on
40043** an OOM error.
40044**
40045** This routine should be invoked only after sqlite3PcacheFetch() fails.
40046*/
40047SQLITE_PRIVATE int sqlite3PcacheFetchStress(
40048  PCache *pCache,                 /* Obtain the page from this cache */
40049  Pgno pgno,                      /* Page number to obtain */
40050  sqlite3_pcache_page **ppPage    /* Write result here */
40051){
40052  PgHdr *pPg;
40053  if( pCache->eCreate==2 ) return 0;
40054
40055
40056  /* Find a dirty page to write-out and recycle. First try to find a
40057  ** page that does not require a journal-sync (one with PGHDR_NEED_SYNC
40058  ** cleared), but if that is not possible settle for any other
40059  ** unreferenced dirty page.
40060  */
40061  for(pPg=pCache->pSynced;
40062      pPg && (pPg->nRef || (pPg->flags&PGHDR_NEED_SYNC));
40063      pPg=pPg->pDirtyPrev
40064  );
40065  pCache->pSynced = pPg;
40066  if( !pPg ){
40067    for(pPg=pCache->pDirtyTail; pPg && pPg->nRef; pPg=pPg->pDirtyPrev);
40068  }
40069  if( pPg ){
40070    int rc;
40071#ifdef SQLITE_LOG_CACHE_SPILL
40072    sqlite3_log(SQLITE_FULL,
40073                "spill page %d making room for %d - cache used: %d/%d",
40074                pPg->pgno, pgno,
40075                sqlite3GlobalConfig.pcache.xPagecount(pCache->pCache),
40076                numberOfCachePages(pCache));
40077#endif
40078    rc = pCache->xStress(pCache->pStress, pPg);
40079    if( rc!=SQLITE_OK && rc!=SQLITE_BUSY ){
40080      return rc;
40081    }
40082  }
40083  *ppPage = sqlite3GlobalConfig.pcache2.xFetch(pCache->pCache, pgno, 2);
40084  return *ppPage==0 ? SQLITE_NOMEM : SQLITE_OK;
40085}
40086
40087/*
40088** This is a helper routine for sqlite3PcacheFetchFinish()
40089**
40090** In the uncommon case where the page being fetched has not been
40091** initialized, this routine is invoked to do the initialization.
40092** This routine is broken out into a separate function since it
40093** requires extra stack manipulation that can be avoided in the common
40094** case.
40095*/
40096static SQLITE_NOINLINE PgHdr *pcacheFetchFinishWithInit(
40097  PCache *pCache,             /* Obtain the page from this cache */
40098  Pgno pgno,                  /* Page number obtained */
40099  sqlite3_pcache_page *pPage  /* Page obtained by prior PcacheFetch() call */
40100){
40101  PgHdr *pPgHdr;
40102  assert( pPage!=0 );
40103  pPgHdr = (PgHdr*)pPage->pExtra;
40104  assert( pPgHdr->pPage==0 );
40105  memset(pPgHdr, 0, sizeof(PgHdr));
40106  pPgHdr->pPage = pPage;
40107  pPgHdr->pData = pPage->pBuf;
40108  pPgHdr->pExtra = (void *)&pPgHdr[1];
40109  memset(pPgHdr->pExtra, 0, pCache->szExtra);
40110  pPgHdr->pCache = pCache;
40111  pPgHdr->pgno = pgno;
40112  pPgHdr->flags = PGHDR_CLEAN;
40113  return sqlite3PcacheFetchFinish(pCache,pgno,pPage);
40114}
40115
40116/*
40117** This routine converts the sqlite3_pcache_page object returned by
40118** sqlite3PcacheFetch() into an initialized PgHdr object.  This routine
40119** must be called after sqlite3PcacheFetch() in order to get a usable
40120** result.
40121*/
40122SQLITE_PRIVATE PgHdr *sqlite3PcacheFetchFinish(
40123  PCache *pCache,             /* Obtain the page from this cache */
40124  Pgno pgno,                  /* Page number obtained */
40125  sqlite3_pcache_page *pPage  /* Page obtained by prior PcacheFetch() call */
40126){
40127  PgHdr *pPgHdr;
40128
40129  assert( pPage!=0 );
40130  pPgHdr = (PgHdr *)pPage->pExtra;
40131
40132  if( !pPgHdr->pPage ){
40133    return pcacheFetchFinishWithInit(pCache, pgno, pPage);
40134  }
40135  if( 0==pPgHdr->nRef ){
40136    pCache->nRef++;
40137  }
40138  pPgHdr->nRef++;
40139  return pPgHdr;
40140}
40141
40142/*
40143** Decrement the reference count on a page. If the page is clean and the
40144** reference count drops to 0, then it is made eligible for recycling.
40145*/
40146SQLITE_PRIVATE void SQLITE_NOINLINE sqlite3PcacheRelease(PgHdr *p){
40147  assert( p->nRef>0 );
40148  p->nRef--;
40149  if( p->nRef==0 ){
40150    p->pCache->nRef--;
40151    if( p->flags&PGHDR_CLEAN ){
40152      pcacheUnpin(p);
40153    }else if( p->pDirtyPrev!=0 ){
40154      /* Move the page to the head of the dirty list. */
40155      pcacheManageDirtyList(p, PCACHE_DIRTYLIST_FRONT);
40156    }
40157  }
40158}
40159
40160/*
40161** Increase the reference count of a supplied page by 1.
40162*/
40163SQLITE_PRIVATE void sqlite3PcacheRef(PgHdr *p){
40164  assert(p->nRef>0);
40165  p->nRef++;
40166}
40167
40168/*
40169** Drop a page from the cache. There must be exactly one reference to the
40170** page. This function deletes that reference, so after it returns the
40171** page pointed to by p is invalid.
40172*/
40173SQLITE_PRIVATE void sqlite3PcacheDrop(PgHdr *p){
40174  assert( p->nRef==1 );
40175  if( p->flags&PGHDR_DIRTY ){
40176    pcacheManageDirtyList(p, PCACHE_DIRTYLIST_REMOVE);
40177  }
40178  p->pCache->nRef--;
40179  sqlite3GlobalConfig.pcache2.xUnpin(p->pCache->pCache, p->pPage, 1);
40180}
40181
40182/*
40183** Make sure the page is marked as dirty. If it isn't dirty already,
40184** make it so.
40185*/
40186SQLITE_PRIVATE void sqlite3PcacheMakeDirty(PgHdr *p){
40187  assert( p->nRef>0 );
40188  if( p->flags & (PGHDR_CLEAN|PGHDR_DONT_WRITE) ){
40189    p->flags &= ~PGHDR_DONT_WRITE;
40190    if( p->flags & PGHDR_CLEAN ){
40191      p->flags ^= (PGHDR_DIRTY|PGHDR_CLEAN);
40192      assert( (p->flags & (PGHDR_DIRTY|PGHDR_CLEAN))==PGHDR_DIRTY );
40193      pcacheManageDirtyList(p, PCACHE_DIRTYLIST_ADD);
40194    }
40195  }
40196}
40197
40198/*
40199** Make sure the page is marked as clean. If it isn't clean already,
40200** make it so.
40201*/
40202SQLITE_PRIVATE void sqlite3PcacheMakeClean(PgHdr *p){
40203  if( (p->flags & PGHDR_DIRTY) ){
40204    assert( (p->flags & PGHDR_CLEAN)==0 );
40205    pcacheManageDirtyList(p, PCACHE_DIRTYLIST_REMOVE);
40206    p->flags &= ~(PGHDR_DIRTY|PGHDR_NEED_SYNC|PGHDR_WRITEABLE);
40207    p->flags |= PGHDR_CLEAN;
40208    if( p->nRef==0 ){
40209      pcacheUnpin(p);
40210    }
40211  }
40212}
40213
40214/*
40215** Make every page in the cache clean.
40216*/
40217SQLITE_PRIVATE void sqlite3PcacheCleanAll(PCache *pCache){
40218  PgHdr *p;
40219  while( (p = pCache->pDirty)!=0 ){
40220    sqlite3PcacheMakeClean(p);
40221  }
40222}
40223
40224/*
40225** Clear the PGHDR_NEED_SYNC flag from all dirty pages.
40226*/
40227SQLITE_PRIVATE void sqlite3PcacheClearSyncFlags(PCache *pCache){
40228  PgHdr *p;
40229  for(p=pCache->pDirty; p; p=p->pDirtyNext){
40230    p->flags &= ~PGHDR_NEED_SYNC;
40231  }
40232  pCache->pSynced = pCache->pDirtyTail;
40233}
40234
40235/*
40236** Change the page number of page p to newPgno.
40237*/
40238SQLITE_PRIVATE void sqlite3PcacheMove(PgHdr *p, Pgno newPgno){
40239  PCache *pCache = p->pCache;
40240  assert( p->nRef>0 );
40241  assert( newPgno>0 );
40242  sqlite3GlobalConfig.pcache2.xRekey(pCache->pCache, p->pPage, p->pgno,newPgno);
40243  p->pgno = newPgno;
40244  if( (p->flags&PGHDR_DIRTY) && (p->flags&PGHDR_NEED_SYNC) ){
40245    pcacheManageDirtyList(p, PCACHE_DIRTYLIST_FRONT);
40246  }
40247}
40248
40249/*
40250** Drop every cache entry whose page number is greater than "pgno". The
40251** caller must ensure that there are no outstanding references to any pages
40252** other than page 1 with a page number greater than pgno.
40253**
40254** If there is a reference to page 1 and the pgno parameter passed to this
40255** function is 0, then the data area associated with page 1 is zeroed, but
40256** the page object is not dropped.
40257*/
40258SQLITE_PRIVATE void sqlite3PcacheTruncate(PCache *pCache, Pgno pgno){
40259  if( pCache->pCache ){
40260    PgHdr *p;
40261    PgHdr *pNext;
40262    for(p=pCache->pDirty; p; p=pNext){
40263      pNext = p->pDirtyNext;
40264      /* This routine never gets call with a positive pgno except right
40265      ** after sqlite3PcacheCleanAll().  So if there are dirty pages,
40266      ** it must be that pgno==0.
40267      */
40268      assert( p->pgno>0 );
40269      if( ALWAYS(p->pgno>pgno) ){
40270        assert( p->flags&PGHDR_DIRTY );
40271        sqlite3PcacheMakeClean(p);
40272      }
40273    }
40274    if( pgno==0 && pCache->nRef ){
40275      sqlite3_pcache_page *pPage1;
40276      pPage1 = sqlite3GlobalConfig.pcache2.xFetch(pCache->pCache,1,0);
40277      if( ALWAYS(pPage1) ){  /* Page 1 is always available in cache, because
40278                             ** pCache->nRef>0 */
40279        memset(pPage1->pBuf, 0, pCache->szPage);
40280        pgno = 1;
40281      }
40282    }
40283    sqlite3GlobalConfig.pcache2.xTruncate(pCache->pCache, pgno+1);
40284  }
40285}
40286
40287/*
40288** Close a cache.
40289*/
40290SQLITE_PRIVATE void sqlite3PcacheClose(PCache *pCache){
40291  assert( pCache->pCache!=0 );
40292  sqlite3GlobalConfig.pcache2.xDestroy(pCache->pCache);
40293}
40294
40295/*
40296** Discard the contents of the cache.
40297*/
40298SQLITE_PRIVATE void sqlite3PcacheClear(PCache *pCache){
40299  sqlite3PcacheTruncate(pCache, 0);
40300}
40301
40302/*
40303** Merge two lists of pages connected by pDirty and in pgno order.
40304** Do not both fixing the pDirtyPrev pointers.
40305*/
40306static PgHdr *pcacheMergeDirtyList(PgHdr *pA, PgHdr *pB){
40307  PgHdr result, *pTail;
40308  pTail = &result;
40309  while( pA && pB ){
40310    if( pA->pgno<pB->pgno ){
40311      pTail->pDirty = pA;
40312      pTail = pA;
40313      pA = pA->pDirty;
40314    }else{
40315      pTail->pDirty = pB;
40316      pTail = pB;
40317      pB = pB->pDirty;
40318    }
40319  }
40320  if( pA ){
40321    pTail->pDirty = pA;
40322  }else if( pB ){
40323    pTail->pDirty = pB;
40324  }else{
40325    pTail->pDirty = 0;
40326  }
40327  return result.pDirty;
40328}
40329
40330/*
40331** Sort the list of pages in accending order by pgno.  Pages are
40332** connected by pDirty pointers.  The pDirtyPrev pointers are
40333** corrupted by this sort.
40334**
40335** Since there cannot be more than 2^31 distinct pages in a database,
40336** there cannot be more than 31 buckets required by the merge sorter.
40337** One extra bucket is added to catch overflow in case something
40338** ever changes to make the previous sentence incorrect.
40339*/
40340#define N_SORT_BUCKET  32
40341static PgHdr *pcacheSortDirtyList(PgHdr *pIn){
40342  PgHdr *a[N_SORT_BUCKET], *p;
40343  int i;
40344  memset(a, 0, sizeof(a));
40345  while( pIn ){
40346    p = pIn;
40347    pIn = p->pDirty;
40348    p->pDirty = 0;
40349    for(i=0; ALWAYS(i<N_SORT_BUCKET-1); i++){
40350      if( a[i]==0 ){
40351        a[i] = p;
40352        break;
40353      }else{
40354        p = pcacheMergeDirtyList(a[i], p);
40355        a[i] = 0;
40356      }
40357    }
40358    if( NEVER(i==N_SORT_BUCKET-1) ){
40359      /* To get here, there need to be 2^(N_SORT_BUCKET) elements in
40360      ** the input list.  But that is impossible.
40361      */
40362      a[i] = pcacheMergeDirtyList(a[i], p);
40363    }
40364  }
40365  p = a[0];
40366  for(i=1; i<N_SORT_BUCKET; i++){
40367    p = pcacheMergeDirtyList(p, a[i]);
40368  }
40369  return p;
40370}
40371
40372/*
40373** Return a list of all dirty pages in the cache, sorted by page number.
40374*/
40375SQLITE_PRIVATE PgHdr *sqlite3PcacheDirtyList(PCache *pCache){
40376  PgHdr *p;
40377  for(p=pCache->pDirty; p; p=p->pDirtyNext){
40378    p->pDirty = p->pDirtyNext;
40379  }
40380  return pcacheSortDirtyList(pCache->pDirty);
40381}
40382
40383/*
40384** Return the total number of referenced pages held by the cache.
40385*/
40386SQLITE_PRIVATE int sqlite3PcacheRefCount(PCache *pCache){
40387  return pCache->nRef;
40388}
40389
40390/*
40391** Return the number of references to the page supplied as an argument.
40392*/
40393SQLITE_PRIVATE int sqlite3PcachePageRefcount(PgHdr *p){
40394  return p->nRef;
40395}
40396
40397/*
40398** Return the total number of pages in the cache.
40399*/
40400SQLITE_PRIVATE int sqlite3PcachePagecount(PCache *pCache){
40401  assert( pCache->pCache!=0 );
40402  return sqlite3GlobalConfig.pcache2.xPagecount(pCache->pCache);
40403}
40404
40405#ifdef SQLITE_TEST
40406/*
40407** Get the suggested cache-size value.
40408*/
40409SQLITE_PRIVATE int sqlite3PcacheGetCachesize(PCache *pCache){
40410  return numberOfCachePages(pCache);
40411}
40412#endif
40413
40414/*
40415** Set the suggested cache-size value.
40416*/
40417SQLITE_PRIVATE void sqlite3PcacheSetCachesize(PCache *pCache, int mxPage){
40418  assert( pCache->pCache!=0 );
40419  pCache->szCache = mxPage;
40420  sqlite3GlobalConfig.pcache2.xCachesize(pCache->pCache,
40421                                         numberOfCachePages(pCache));
40422}
40423
40424/*
40425** Free up as much memory as possible from the page cache.
40426*/
40427SQLITE_PRIVATE void sqlite3PcacheShrink(PCache *pCache){
40428  assert( pCache->pCache!=0 );
40429  sqlite3GlobalConfig.pcache2.xShrink(pCache->pCache);
40430}
40431
40432/*
40433** Return the size of the header added by this middleware layer
40434** in the page-cache hierarchy.
40435*/
40436SQLITE_PRIVATE int sqlite3HeaderSizePcache(void){ return ROUND8(sizeof(PgHdr)); }
40437
40438
40439#if defined(SQLITE_CHECK_PAGES) || defined(SQLITE_DEBUG)
40440/*
40441** For all dirty pages currently in the cache, invoke the specified
40442** callback. This is only used if the SQLITE_CHECK_PAGES macro is
40443** defined.
40444*/
40445SQLITE_PRIVATE void sqlite3PcacheIterateDirty(PCache *pCache, void (*xIter)(PgHdr *)){
40446  PgHdr *pDirty;
40447  for(pDirty=pCache->pDirty; pDirty; pDirty=pDirty->pDirtyNext){
40448    xIter(pDirty);
40449  }
40450}
40451#endif
40452
40453/************** End of pcache.c **********************************************/
40454/************** Begin file pcache1.c *****************************************/
40455/*
40456** 2008 November 05
40457**
40458** The author disclaims copyright to this source code.  In place of
40459** a legal notice, here is a blessing:
40460**
40461**    May you do good and not evil.
40462**    May you find forgiveness for yourself and forgive others.
40463**    May you share freely, never taking more than you give.
40464**
40465*************************************************************************
40466**
40467** This file implements the default page cache implementation (the
40468** sqlite3_pcache interface). It also contains part of the implementation
40469** of the SQLITE_CONFIG_PAGECACHE and sqlite3_release_memory() features.
40470** If the default page cache implementation is overridden, then neither of
40471** these two features are available.
40472**
40473** A Page cache line looks like this:
40474**
40475**  -------------------------------------------------------------
40476**  |  database page content   |  PgHdr1  |  MemPage  |  PgHdr  |
40477**  -------------------------------------------------------------
40478**
40479** The database page content is up front (so that buffer overreads tend to
40480** flow harmlessly into the PgHdr1, MemPage, and PgHdr extensions).   MemPage
40481** is the extension added by the btree.c module containing information such
40482** as the database page number and how that database page is used.  PgHdr
40483** is added by the pcache.c layer and contains information used to keep track
40484** of which pages are "dirty".  PgHdr1 is an extension added by this
40485** module (pcache1.c).  The PgHdr1 header is a subclass of sqlite3_pcache_page.
40486** PgHdr1 contains information needed to look up a page by its page number.
40487** The superclass sqlite3_pcache_page.pBuf points to the start of the
40488** database page content and sqlite3_pcache_page.pExtra points to PgHdr.
40489**
40490** The size of the extension (MemPage+PgHdr+PgHdr1) can be determined at
40491** runtime using sqlite3_config(SQLITE_CONFIG_PCACHE_HDRSZ, &size).  The
40492** sizes of the extensions sum to 272 bytes on x64 for 3.8.10, but this
40493** size can vary according to architecture, compile-time options, and
40494** SQLite library version number.
40495**
40496** If SQLITE_PCACHE_SEPARATE_HEADER is defined, then the extension is obtained
40497** using a separate memory allocation from the database page content.  This
40498** seeks to overcome the "clownshoe" problem (also called "internal
40499** fragmentation" in academic literature) of allocating a few bytes more
40500** than a power of two with the memory allocator rounding up to the next
40501** power of two, and leaving the rounded-up space unused.
40502**
40503** This module tracks pointers to PgHdr1 objects.  Only pcache.c communicates
40504** with this module.  Information is passed back and forth as PgHdr1 pointers.
40505**
40506** The pcache.c and pager.c modules deal pointers to PgHdr objects.
40507** The btree.c module deals with pointers to MemPage objects.
40508**
40509** SOURCE OF PAGE CACHE MEMORY:
40510**
40511** Memory for a page might come from any of three sources:
40512**
40513**    (1)  The general-purpose memory allocator - sqlite3Malloc()
40514**    (2)  Global page-cache memory provided using sqlite3_config() with
40515**         SQLITE_CONFIG_PAGECACHE.
40516**    (3)  PCache-local bulk allocation.
40517**
40518** The third case is a chunk of heap memory (defaulting to 100 pages worth)
40519** that is allocated when the page cache is created.  The size of the local
40520** bulk allocation can be adjusted using
40521**
40522**     sqlite3_config(SQLITE_CONFIG_PCACHE, 0, 0, N).
40523**
40524** If N is positive, then N pages worth of memory are allocated using a single
40525** sqlite3Malloc() call and that memory is used for the first N pages allocated.
40526** Or if N is negative, then -1024*N bytes of memory are allocated and used
40527** for as many pages as can be accomodated.
40528**
40529** Only one of (2) or (3) can be used.  Once the memory available to (2) or
40530** (3) is exhausted, subsequent allocations fail over to the general-purpose
40531** memory allocator (1).
40532**
40533** Earlier versions of SQLite used only methods (1) and (2).  But experiments
40534** show that method (3) with N==100 provides about a 5% performance boost for
40535** common workloads.
40536*/
40537/* #include "sqliteInt.h" */
40538
40539typedef struct PCache1 PCache1;
40540typedef struct PgHdr1 PgHdr1;
40541typedef struct PgFreeslot PgFreeslot;
40542typedef struct PGroup PGroup;
40543
40544/* Each page cache (or PCache) belongs to a PGroup.  A PGroup is a set
40545** of one or more PCaches that are able to recycle each other's unpinned
40546** pages when they are under memory pressure.  A PGroup is an instance of
40547** the following object.
40548**
40549** This page cache implementation works in one of two modes:
40550**
40551**   (1)  Every PCache is the sole member of its own PGroup.  There is
40552**        one PGroup per PCache.
40553**
40554**   (2)  There is a single global PGroup that all PCaches are a member
40555**        of.
40556**
40557** Mode 1 uses more memory (since PCache instances are not able to rob
40558** unused pages from other PCaches) but it also operates without a mutex,
40559** and is therefore often faster.  Mode 2 requires a mutex in order to be
40560** threadsafe, but recycles pages more efficiently.
40561**
40562** For mode (1), PGroup.mutex is NULL.  For mode (2) there is only a single
40563** PGroup which is the pcache1.grp global variable and its mutex is
40564** SQLITE_MUTEX_STATIC_LRU.
40565*/
40566struct PGroup {
40567  sqlite3_mutex *mutex;          /* MUTEX_STATIC_LRU or NULL */
40568  unsigned int nMaxPage;         /* Sum of nMax for purgeable caches */
40569  unsigned int nMinPage;         /* Sum of nMin for purgeable caches */
40570  unsigned int mxPinned;         /* nMaxpage + 10 - nMinPage */
40571  unsigned int nCurrentPage;     /* Number of purgeable pages allocated */
40572  PgHdr1 *pLruHead, *pLruTail;   /* LRU list of unpinned pages */
40573};
40574
40575/* Each page cache is an instance of the following object.  Every
40576** open database file (including each in-memory database and each
40577** temporary or transient database) has a single page cache which
40578** is an instance of this object.
40579**
40580** Pointers to structures of this type are cast and returned as
40581** opaque sqlite3_pcache* handles.
40582*/
40583struct PCache1 {
40584  /* Cache configuration parameters. Page size (szPage) and the purgeable
40585  ** flag (bPurgeable) are set when the cache is created. nMax may be
40586  ** modified at any time by a call to the pcache1Cachesize() method.
40587  ** The PGroup mutex must be held when accessing nMax.
40588  */
40589  PGroup *pGroup;                     /* PGroup this cache belongs to */
40590  int szPage;                         /* Size of database content section */
40591  int szExtra;                        /* sizeof(MemPage)+sizeof(PgHdr) */
40592  int szAlloc;                        /* Total size of one pcache line */
40593  int bPurgeable;                     /* True if cache is purgeable */
40594  unsigned int nMin;                  /* Minimum number of pages reserved */
40595  unsigned int nMax;                  /* Configured "cache_size" value */
40596  unsigned int n90pct;                /* nMax*9/10 */
40597  unsigned int iMaxKey;               /* Largest key seen since xTruncate() */
40598
40599  /* Hash table of all pages. The following variables may only be accessed
40600  ** when the accessor is holding the PGroup mutex.
40601  */
40602  unsigned int nRecyclable;           /* Number of pages in the LRU list */
40603  unsigned int nPage;                 /* Total number of pages in apHash */
40604  unsigned int nHash;                 /* Number of slots in apHash[] */
40605  PgHdr1 **apHash;                    /* Hash table for fast lookup by key */
40606  PgHdr1 *pFree;                      /* List of unused pcache-local pages */
40607  void *pBulk;                        /* Bulk memory used by pcache-local */
40608};
40609
40610/*
40611** Each cache entry is represented by an instance of the following
40612** structure. Unless SQLITE_PCACHE_SEPARATE_HEADER is defined, a buffer of
40613** PgHdr1.pCache->szPage bytes is allocated directly before this structure
40614** in memory.
40615*/
40616struct PgHdr1 {
40617  sqlite3_pcache_page page;
40618  unsigned int iKey;             /* Key value (page number) */
40619  u8 isPinned;                   /* Page in use, not on the LRU list */
40620  u8 isBulkLocal;                /* This page from bulk local storage */
40621  PgHdr1 *pNext;                 /* Next in hash table chain */
40622  PCache1 *pCache;               /* Cache that currently owns this page */
40623  PgHdr1 *pLruNext;              /* Next in LRU list of unpinned pages */
40624  PgHdr1 *pLruPrev;              /* Previous in LRU list of unpinned pages */
40625};
40626
40627/*
40628** Free slots in the allocator used to divide up the global page cache
40629** buffer provided using the SQLITE_CONFIG_PAGECACHE mechanism.
40630*/
40631struct PgFreeslot {
40632  PgFreeslot *pNext;  /* Next free slot */
40633};
40634
40635/*
40636** Global data used by this cache.
40637*/
40638static SQLITE_WSD struct PCacheGlobal {
40639  PGroup grp;                    /* The global PGroup for mode (2) */
40640
40641  /* Variables related to SQLITE_CONFIG_PAGECACHE settings.  The
40642  ** szSlot, nSlot, pStart, pEnd, nReserve, and isInit values are all
40643  ** fixed at sqlite3_initialize() time and do not require mutex protection.
40644  ** The nFreeSlot and pFree values do require mutex protection.
40645  */
40646  int isInit;                    /* True if initialized */
40647  int separateCache;             /* Use a new PGroup for each PCache */
40648  int nInitPage;                 /* Initial bulk allocation size */
40649  int szSlot;                    /* Size of each free slot */
40650  int nSlot;                     /* The number of pcache slots */
40651  int nReserve;                  /* Try to keep nFreeSlot above this */
40652  void *pStart, *pEnd;           /* Bounds of global page cache memory */
40653  /* Above requires no mutex.  Use mutex below for variable that follow. */
40654  sqlite3_mutex *mutex;          /* Mutex for accessing the following: */
40655  PgFreeslot *pFree;             /* Free page blocks */
40656  int nFreeSlot;                 /* Number of unused pcache slots */
40657  /* The following value requires a mutex to change.  We skip the mutex on
40658  ** reading because (1) most platforms read a 32-bit integer atomically and
40659  ** (2) even if an incorrect value is read, no great harm is done since this
40660  ** is really just an optimization. */
40661  int bUnderPressure;            /* True if low on PAGECACHE memory */
40662} pcache1_g;
40663
40664/*
40665** All code in this file should access the global structure above via the
40666** alias "pcache1". This ensures that the WSD emulation is used when
40667** compiling for systems that do not support real WSD.
40668*/
40669#define pcache1 (GLOBAL(struct PCacheGlobal, pcache1_g))
40670
40671/*
40672** Macros to enter and leave the PCache LRU mutex.
40673*/
40674#if !defined(SQLITE_ENABLE_MEMORY_MANAGEMENT) || SQLITE_THREADSAFE==0
40675# define pcache1EnterMutex(X)  assert((X)->mutex==0)
40676# define pcache1LeaveMutex(X)  assert((X)->mutex==0)
40677# define PCACHE1_MIGHT_USE_GROUP_MUTEX 0
40678#else
40679# define pcache1EnterMutex(X) sqlite3_mutex_enter((X)->mutex)
40680# define pcache1LeaveMutex(X) sqlite3_mutex_leave((X)->mutex)
40681# define PCACHE1_MIGHT_USE_GROUP_MUTEX 1
40682#endif
40683
40684/******************************************************************************/
40685/******** Page Allocation/SQLITE_CONFIG_PCACHE Related Functions **************/
40686
40687/*
40688** This function is called during initialization if a static buffer is
40689** supplied to use for the page-cache by passing the SQLITE_CONFIG_PAGECACHE
40690** verb to sqlite3_config(). Parameter pBuf points to an allocation large
40691** enough to contain 'n' buffers of 'sz' bytes each.
40692**
40693** This routine is called from sqlite3_initialize() and so it is guaranteed
40694** to be serialized already.  There is no need for further mutexing.
40695*/
40696SQLITE_PRIVATE void sqlite3PCacheBufferSetup(void *pBuf, int sz, int n){
40697  if( pcache1.isInit ){
40698    PgFreeslot *p;
40699    if( pBuf==0 ) sz = n = 0;
40700    sz = ROUNDDOWN8(sz);
40701    pcache1.szSlot = sz;
40702    pcache1.nSlot = pcache1.nFreeSlot = n;
40703    pcache1.nReserve = n>90 ? 10 : (n/10 + 1);
40704    pcache1.pStart = pBuf;
40705    pcache1.pFree = 0;
40706    pcache1.bUnderPressure = 0;
40707    while( n-- ){
40708      p = (PgFreeslot*)pBuf;
40709      p->pNext = pcache1.pFree;
40710      pcache1.pFree = p;
40711      pBuf = (void*)&((char*)pBuf)[sz];
40712    }
40713    pcache1.pEnd = pBuf;
40714  }
40715}
40716
40717/*
40718** Try to initialize the pCache->pFree and pCache->pBulk fields.  Return
40719** true if pCache->pFree ends up containing one or more free pages.
40720*/
40721static int pcache1InitBulk(PCache1 *pCache){
40722  i64 szBulk;
40723  char *zBulk;
40724  if( pcache1.nInitPage==0 ) return 0;
40725  /* Do not bother with a bulk allocation if the cache size very small */
40726  if( pCache->nMax<3 ) return 0;
40727  sqlite3BeginBenignMalloc();
40728  if( pcache1.nInitPage>0 ){
40729    szBulk = pCache->szAlloc * (i64)pcache1.nInitPage;
40730  }else{
40731    szBulk = -1024 * (i64)pcache1.nInitPage;
40732  }
40733  if( szBulk > pCache->szAlloc*(i64)pCache->nMax ){
40734    szBulk = pCache->szAlloc*pCache->nMax;
40735  }
40736  zBulk = pCache->pBulk = sqlite3Malloc( szBulk );
40737  sqlite3EndBenignMalloc();
40738  if( zBulk ){
40739    int nBulk = sqlite3MallocSize(zBulk)/pCache->szAlloc;
40740    int i;
40741    for(i=0; i<nBulk; i++){
40742      PgHdr1 *pX = (PgHdr1*)&zBulk[pCache->szPage];
40743      pX->page.pBuf = zBulk;
40744      pX->page.pExtra = &pX[1];
40745      pX->isBulkLocal = 1;
40746      pX->pNext = pCache->pFree;
40747      pCache->pFree = pX;
40748      zBulk += pCache->szAlloc;
40749    }
40750  }
40751  return pCache->pFree!=0;
40752}
40753
40754/*
40755** Malloc function used within this file to allocate space from the buffer
40756** configured using sqlite3_config(SQLITE_CONFIG_PAGECACHE) option. If no
40757** such buffer exists or there is no space left in it, this function falls
40758** back to sqlite3Malloc().
40759**
40760** Multiple threads can run this routine at the same time.  Global variables
40761** in pcache1 need to be protected via mutex.
40762*/
40763static void *pcache1Alloc(int nByte){
40764  void *p = 0;
40765  assert( sqlite3_mutex_notheld(pcache1.grp.mutex) );
40766  if( nByte<=pcache1.szSlot ){
40767    sqlite3_mutex_enter(pcache1.mutex);
40768    p = (PgHdr1 *)pcache1.pFree;
40769    if( p ){
40770      pcache1.pFree = pcache1.pFree->pNext;
40771      pcache1.nFreeSlot--;
40772      pcache1.bUnderPressure = pcache1.nFreeSlot<pcache1.nReserve;
40773      assert( pcache1.nFreeSlot>=0 );
40774      sqlite3StatusSet(SQLITE_STATUS_PAGECACHE_SIZE, nByte);
40775      sqlite3StatusUp(SQLITE_STATUS_PAGECACHE_USED, 1);
40776    }
40777    sqlite3_mutex_leave(pcache1.mutex);
40778  }
40779  if( p==0 ){
40780    /* Memory is not available in the SQLITE_CONFIG_PAGECACHE pool.  Get
40781    ** it from sqlite3Malloc instead.
40782    */
40783    p = sqlite3Malloc(nByte);
40784#ifndef SQLITE_DISABLE_PAGECACHE_OVERFLOW_STATS
40785    if( p ){
40786      int sz = sqlite3MallocSize(p);
40787      sqlite3_mutex_enter(pcache1.mutex);
40788      sqlite3StatusSet(SQLITE_STATUS_PAGECACHE_SIZE, nByte);
40789      sqlite3StatusUp(SQLITE_STATUS_PAGECACHE_OVERFLOW, sz);
40790      sqlite3_mutex_leave(pcache1.mutex);
40791    }
40792#endif
40793    sqlite3MemdebugSetType(p, MEMTYPE_PCACHE);
40794  }
40795  return p;
40796}
40797
40798/*
40799** Free an allocated buffer obtained from pcache1Alloc().
40800*/
40801static void pcache1Free(void *p){
40802  int nFreed = 0;
40803  if( p==0 ) return;
40804  if( p>=pcache1.pStart && p<pcache1.pEnd ){
40805    PgFreeslot *pSlot;
40806    sqlite3_mutex_enter(pcache1.mutex);
40807    sqlite3StatusDown(SQLITE_STATUS_PAGECACHE_USED, 1);
40808    pSlot = (PgFreeslot*)p;
40809    pSlot->pNext = pcache1.pFree;
40810    pcache1.pFree = pSlot;
40811    pcache1.nFreeSlot++;
40812    pcache1.bUnderPressure = pcache1.nFreeSlot<pcache1.nReserve;
40813    assert( pcache1.nFreeSlot<=pcache1.nSlot );
40814    sqlite3_mutex_leave(pcache1.mutex);
40815  }else{
40816    assert( sqlite3MemdebugHasType(p, MEMTYPE_PCACHE) );
40817    sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
40818#ifndef SQLITE_DISABLE_PAGECACHE_OVERFLOW_STATS
40819    nFreed = sqlite3MallocSize(p);
40820    sqlite3_mutex_enter(pcache1.mutex);
40821    sqlite3StatusDown(SQLITE_STATUS_PAGECACHE_OVERFLOW, nFreed);
40822    sqlite3_mutex_leave(pcache1.mutex);
40823#endif
40824    sqlite3_free(p);
40825  }
40826}
40827
40828#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
40829/*
40830** Return the size of a pcache allocation
40831*/
40832static int pcache1MemSize(void *p){
40833  if( p>=pcache1.pStart && p<pcache1.pEnd ){
40834    return pcache1.szSlot;
40835  }else{
40836    int iSize;
40837    assert( sqlite3MemdebugHasType(p, MEMTYPE_PCACHE) );
40838    sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
40839    iSize = sqlite3MallocSize(p);
40840    sqlite3MemdebugSetType(p, MEMTYPE_PCACHE);
40841    return iSize;
40842  }
40843}
40844#endif /* SQLITE_ENABLE_MEMORY_MANAGEMENT */
40845
40846/*
40847** Allocate a new page object initially associated with cache pCache.
40848*/
40849static PgHdr1 *pcache1AllocPage(PCache1 *pCache){
40850  PgHdr1 *p = 0;
40851  void *pPg;
40852
40853  assert( sqlite3_mutex_held(pCache->pGroup->mutex) );
40854  if( pCache->pFree || (pCache->nPage==0 && pcache1InitBulk(pCache)) ){
40855    p = pCache->pFree;
40856    pCache->pFree = p->pNext;
40857    p->pNext = 0;
40858  }else{
40859#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
40860    /* The group mutex must be released before pcache1Alloc() is called. This
40861    ** is because it might call sqlite3_release_memory(), which assumes that
40862    ** this mutex is not held. */
40863    assert( pcache1.separateCache==0 );
40864    assert( pCache->pGroup==&pcache1.grp );
40865    pcache1LeaveMutex(pCache->pGroup);
40866#endif
40867#ifdef SQLITE_PCACHE_SEPARATE_HEADER
40868    pPg = pcache1Alloc(pCache->szPage);
40869    p = sqlite3Malloc(sizeof(PgHdr1) + pCache->szExtra);
40870    if( !pPg || !p ){
40871      pcache1Free(pPg);
40872      sqlite3_free(p);
40873      pPg = 0;
40874    }
40875#else
40876    pPg = pcache1Alloc(pCache->szAlloc);
40877    p = (PgHdr1 *)&((u8 *)pPg)[pCache->szPage];
40878#endif
40879#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
40880    pcache1EnterMutex(pCache->pGroup);
40881#endif
40882    if( pPg==0 ) return 0;
40883    p->page.pBuf = pPg;
40884    p->page.pExtra = &p[1];
40885    p->isBulkLocal = 0;
40886  }
40887  if( pCache->bPurgeable ){
40888    pCache->pGroup->nCurrentPage++;
40889  }
40890  return p;
40891}
40892
40893/*
40894** Free a page object allocated by pcache1AllocPage().
40895*/
40896static void pcache1FreePage(PgHdr1 *p){
40897  PCache1 *pCache;
40898  assert( p!=0 );
40899  pCache = p->pCache;
40900  assert( sqlite3_mutex_held(p->pCache->pGroup->mutex) );
40901  if( p->isBulkLocal ){
40902    p->pNext = pCache->pFree;
40903    pCache->pFree = p;
40904  }else{
40905    pcache1Free(p->page.pBuf);
40906#ifdef SQLITE_PCACHE_SEPARATE_HEADER
40907    sqlite3_free(p);
40908#endif
40909  }
40910  if( pCache->bPurgeable ){
40911    pCache->pGroup->nCurrentPage--;
40912  }
40913}
40914
40915/*
40916** Malloc function used by SQLite to obtain space from the buffer configured
40917** using sqlite3_config(SQLITE_CONFIG_PAGECACHE) option. If no such buffer
40918** exists, this function falls back to sqlite3Malloc().
40919*/
40920SQLITE_PRIVATE void *sqlite3PageMalloc(int sz){
40921  return pcache1Alloc(sz);
40922}
40923
40924/*
40925** Free an allocated buffer obtained from sqlite3PageMalloc().
40926*/
40927SQLITE_PRIVATE void sqlite3PageFree(void *p){
40928  pcache1Free(p);
40929}
40930
40931
40932/*
40933** Return true if it desirable to avoid allocating a new page cache
40934** entry.
40935**
40936** If memory was allocated specifically to the page cache using
40937** SQLITE_CONFIG_PAGECACHE but that memory has all been used, then
40938** it is desirable to avoid allocating a new page cache entry because
40939** presumably SQLITE_CONFIG_PAGECACHE was suppose to be sufficient
40940** for all page cache needs and we should not need to spill the
40941** allocation onto the heap.
40942**
40943** Or, the heap is used for all page cache memory but the heap is
40944** under memory pressure, then again it is desirable to avoid
40945** allocating a new page cache entry in order to avoid stressing
40946** the heap even further.
40947*/
40948static int pcache1UnderMemoryPressure(PCache1 *pCache){
40949  if( pcache1.nSlot && (pCache->szPage+pCache->szExtra)<=pcache1.szSlot ){
40950    return pcache1.bUnderPressure;
40951  }else{
40952    return sqlite3HeapNearlyFull();
40953  }
40954}
40955
40956/******************************************************************************/
40957/******** General Implementation Functions ************************************/
40958
40959/*
40960** This function is used to resize the hash table used by the cache passed
40961** as the first argument.
40962**
40963** The PCache mutex must be held when this function is called.
40964*/
40965static void pcache1ResizeHash(PCache1 *p){
40966  PgHdr1 **apNew;
40967  unsigned int nNew;
40968  unsigned int i;
40969
40970  assert( sqlite3_mutex_held(p->pGroup->mutex) );
40971
40972  nNew = p->nHash*2;
40973  if( nNew<256 ){
40974    nNew = 256;
40975  }
40976
40977  pcache1LeaveMutex(p->pGroup);
40978  if( p->nHash ){ sqlite3BeginBenignMalloc(); }
40979  apNew = (PgHdr1 **)sqlite3MallocZero(sizeof(PgHdr1 *)*nNew);
40980  if( p->nHash ){ sqlite3EndBenignMalloc(); }
40981  pcache1EnterMutex(p->pGroup);
40982  if( apNew ){
40983    for(i=0; i<p->nHash; i++){
40984      PgHdr1 *pPage;
40985      PgHdr1 *pNext = p->apHash[i];
40986      while( (pPage = pNext)!=0 ){
40987        unsigned int h = pPage->iKey % nNew;
40988        pNext = pPage->pNext;
40989        pPage->pNext = apNew[h];
40990        apNew[h] = pPage;
40991      }
40992    }
40993    sqlite3_free(p->apHash);
40994    p->apHash = apNew;
40995    p->nHash = nNew;
40996  }
40997}
40998
40999/*
41000** This function is used internally to remove the page pPage from the
41001** PGroup LRU list, if is part of it. If pPage is not part of the PGroup
41002** LRU list, then this function is a no-op.
41003**
41004** The PGroup mutex must be held when this function is called.
41005*/
41006static PgHdr1 *pcache1PinPage(PgHdr1 *pPage){
41007  PCache1 *pCache;
41008
41009  assert( pPage!=0 );
41010  assert( pPage->isPinned==0 );
41011  pCache = pPage->pCache;
41012  assert( pPage->pLruNext || pPage==pCache->pGroup->pLruTail );
41013  assert( pPage->pLruPrev || pPage==pCache->pGroup->pLruHead );
41014  assert( sqlite3_mutex_held(pCache->pGroup->mutex) );
41015  if( pPage->pLruPrev ){
41016    pPage->pLruPrev->pLruNext = pPage->pLruNext;
41017  }else{
41018    pCache->pGroup->pLruHead = pPage->pLruNext;
41019  }
41020  if( pPage->pLruNext ){
41021    pPage->pLruNext->pLruPrev = pPage->pLruPrev;
41022  }else{
41023    pCache->pGroup->pLruTail = pPage->pLruPrev;
41024  }
41025  pPage->pLruNext = 0;
41026  pPage->pLruPrev = 0;
41027  pPage->isPinned = 1;
41028  pCache->nRecyclable--;
41029  return pPage;
41030}
41031
41032
41033/*
41034** Remove the page supplied as an argument from the hash table
41035** (PCache1.apHash structure) that it is currently stored in.
41036** Also free the page if freePage is true.
41037**
41038** The PGroup mutex must be held when this function is called.
41039*/
41040static void pcache1RemoveFromHash(PgHdr1 *pPage, int freeFlag){
41041  unsigned int h;
41042  PCache1 *pCache = pPage->pCache;
41043  PgHdr1 **pp;
41044
41045  assert( sqlite3_mutex_held(pCache->pGroup->mutex) );
41046  h = pPage->iKey % pCache->nHash;
41047  for(pp=&pCache->apHash[h]; (*pp)!=pPage; pp=&(*pp)->pNext);
41048  *pp = (*pp)->pNext;
41049
41050  pCache->nPage--;
41051  if( freeFlag ) pcache1FreePage(pPage);
41052}
41053
41054/*
41055** If there are currently more than nMaxPage pages allocated, try
41056** to recycle pages to reduce the number allocated to nMaxPage.
41057*/
41058static void pcache1EnforceMaxPage(PCache1 *pCache){
41059  PGroup *pGroup = pCache->pGroup;
41060  assert( sqlite3_mutex_held(pGroup->mutex) );
41061  while( pGroup->nCurrentPage>pGroup->nMaxPage && pGroup->pLruTail ){
41062    PgHdr1 *p = pGroup->pLruTail;
41063    assert( p->pCache->pGroup==pGroup );
41064    assert( p->isPinned==0 );
41065    pcache1PinPage(p);
41066    pcache1RemoveFromHash(p, 1);
41067  }
41068  if( pCache->nPage==0 && pCache->pBulk ){
41069    sqlite3_free(pCache->pBulk);
41070    pCache->pBulk = pCache->pFree = 0;
41071  }
41072}
41073
41074/*
41075** Discard all pages from cache pCache with a page number (key value)
41076** greater than or equal to iLimit. Any pinned pages that meet this
41077** criteria are unpinned before they are discarded.
41078**
41079** The PCache mutex must be held when this function is called.
41080*/
41081static void pcache1TruncateUnsafe(
41082  PCache1 *pCache,             /* The cache to truncate */
41083  unsigned int iLimit          /* Drop pages with this pgno or larger */
41084){
41085  TESTONLY( unsigned int nPage = 0; )  /* To assert pCache->nPage is correct */
41086  unsigned int h;
41087  assert( sqlite3_mutex_held(pCache->pGroup->mutex) );
41088  for(h=0; h<pCache->nHash; h++){
41089    PgHdr1 **pp = &pCache->apHash[h];
41090    PgHdr1 *pPage;
41091    while( (pPage = *pp)!=0 ){
41092      if( pPage->iKey>=iLimit ){
41093        pCache->nPage--;
41094        *pp = pPage->pNext;
41095        if( !pPage->isPinned ) pcache1PinPage(pPage);
41096        pcache1FreePage(pPage);
41097      }else{
41098        pp = &pPage->pNext;
41099        TESTONLY( nPage++; )
41100      }
41101    }
41102  }
41103  assert( pCache->nPage==nPage );
41104}
41105
41106/******************************************************************************/
41107/******** sqlite3_pcache Methods **********************************************/
41108
41109/*
41110** Implementation of the sqlite3_pcache.xInit method.
41111*/
41112static int pcache1Init(void *NotUsed){
41113  UNUSED_PARAMETER(NotUsed);
41114  assert( pcache1.isInit==0 );
41115  memset(&pcache1, 0, sizeof(pcache1));
41116
41117
41118  /*
41119  ** The pcache1.separateCache variable is true if each PCache has its own
41120  ** private PGroup (mode-1).  pcache1.separateCache is false if the single
41121  ** PGroup in pcache1.grp is used for all page caches (mode-2).
41122  **
41123  **   *  Always use a unified cache (mode-2) if ENABLE_MEMORY_MANAGEMENT
41124  **
41125  **   *  Use a unified cache in single-threaded applications that have
41126  **      configured a start-time buffer for use as page-cache memory using
41127  **      sqlite3_config(SQLITE_CONFIG_PAGECACHE, pBuf, sz, N) with non-NULL
41128  **      pBuf argument.
41129  **
41130  **   *  Otherwise use separate caches (mode-1)
41131  */
41132#if defined(SQLITE_ENABLE_MEMORY_MANAGEMENT)
41133  pcache1.separateCache = 0;
41134#elif SQLITE_THREADSAFE
41135  pcache1.separateCache = sqlite3GlobalConfig.pPage==0
41136                          || sqlite3GlobalConfig.bCoreMutex>0;
41137#else
41138  pcache1.separateCache = sqlite3GlobalConfig.pPage==0;
41139#endif
41140
41141#if SQLITE_THREADSAFE
41142  if( sqlite3GlobalConfig.bCoreMutex ){
41143    pcache1.grp.mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_LRU);
41144    pcache1.mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_PMEM);
41145  }
41146#endif
41147  if( pcache1.separateCache
41148   && sqlite3GlobalConfig.nPage!=0
41149   && sqlite3GlobalConfig.pPage==0
41150  ){
41151    pcache1.nInitPage = sqlite3GlobalConfig.nPage;
41152  }else{
41153    pcache1.nInitPage = 0;
41154  }
41155  pcache1.grp.mxPinned = 10;
41156  pcache1.isInit = 1;
41157  return SQLITE_OK;
41158}
41159
41160/*
41161** Implementation of the sqlite3_pcache.xShutdown method.
41162** Note that the static mutex allocated in xInit does
41163** not need to be freed.
41164*/
41165static void pcache1Shutdown(void *NotUsed){
41166  UNUSED_PARAMETER(NotUsed);
41167  assert( pcache1.isInit!=0 );
41168  memset(&pcache1, 0, sizeof(pcache1));
41169}
41170
41171/* forward declaration */
41172static void pcache1Destroy(sqlite3_pcache *p);
41173
41174/*
41175** Implementation of the sqlite3_pcache.xCreate method.
41176**
41177** Allocate a new cache.
41178*/
41179static sqlite3_pcache *pcache1Create(int szPage, int szExtra, int bPurgeable){
41180  PCache1 *pCache;      /* The newly created page cache */
41181  PGroup *pGroup;       /* The group the new page cache will belong to */
41182  int sz;               /* Bytes of memory required to allocate the new cache */
41183
41184  assert( (szPage & (szPage-1))==0 && szPage>=512 && szPage<=65536 );
41185  assert( szExtra < 300 );
41186
41187  sz = sizeof(PCache1) + sizeof(PGroup)*pcache1.separateCache;
41188  pCache = (PCache1 *)sqlite3MallocZero(sz);
41189  if( pCache ){
41190    if( pcache1.separateCache ){
41191      pGroup = (PGroup*)&pCache[1];
41192      pGroup->mxPinned = 10;
41193    }else{
41194      pGroup = &pcache1.grp;
41195    }
41196    pCache->pGroup = pGroup;
41197    pCache->szPage = szPage;
41198    pCache->szExtra = szExtra;
41199    pCache->szAlloc = szPage + szExtra + ROUND8(sizeof(PgHdr1));
41200    pCache->bPurgeable = (bPurgeable ? 1 : 0);
41201    pcache1EnterMutex(pGroup);
41202    pcache1ResizeHash(pCache);
41203    if( bPurgeable ){
41204      pCache->nMin = 10;
41205      pGroup->nMinPage += pCache->nMin;
41206      pGroup->mxPinned = pGroup->nMaxPage + 10 - pGroup->nMinPage;
41207    }
41208    pcache1LeaveMutex(pGroup);
41209    if( pCache->nHash==0 ){
41210      pcache1Destroy((sqlite3_pcache*)pCache);
41211      pCache = 0;
41212    }
41213  }
41214  return (sqlite3_pcache *)pCache;
41215}
41216
41217/*
41218** Implementation of the sqlite3_pcache.xCachesize method.
41219**
41220** Configure the cache_size limit for a cache.
41221*/
41222static void pcache1Cachesize(sqlite3_pcache *p, int nMax){
41223  PCache1 *pCache = (PCache1 *)p;
41224  if( pCache->bPurgeable ){
41225    PGroup *pGroup = pCache->pGroup;
41226    pcache1EnterMutex(pGroup);
41227    pGroup->nMaxPage += (nMax - pCache->nMax);
41228    pGroup->mxPinned = pGroup->nMaxPage + 10 - pGroup->nMinPage;
41229    pCache->nMax = nMax;
41230    pCache->n90pct = pCache->nMax*9/10;
41231    pcache1EnforceMaxPage(pCache);
41232    pcache1LeaveMutex(pGroup);
41233  }
41234}
41235
41236/*
41237** Implementation of the sqlite3_pcache.xShrink method.
41238**
41239** Free up as much memory as possible.
41240*/
41241static void pcache1Shrink(sqlite3_pcache *p){
41242  PCache1 *pCache = (PCache1*)p;
41243  if( pCache->bPurgeable ){
41244    PGroup *pGroup = pCache->pGroup;
41245    int savedMaxPage;
41246    pcache1EnterMutex(pGroup);
41247    savedMaxPage = pGroup->nMaxPage;
41248    pGroup->nMaxPage = 0;
41249    pcache1EnforceMaxPage(pCache);
41250    pGroup->nMaxPage = savedMaxPage;
41251    pcache1LeaveMutex(pGroup);
41252  }
41253}
41254
41255/*
41256** Implementation of the sqlite3_pcache.xPagecount method.
41257*/
41258static int pcache1Pagecount(sqlite3_pcache *p){
41259  int n;
41260  PCache1 *pCache = (PCache1*)p;
41261  pcache1EnterMutex(pCache->pGroup);
41262  n = pCache->nPage;
41263  pcache1LeaveMutex(pCache->pGroup);
41264  return n;
41265}
41266
41267
41268/*
41269** Implement steps 3, 4, and 5 of the pcache1Fetch() algorithm described
41270** in the header of the pcache1Fetch() procedure.
41271**
41272** This steps are broken out into a separate procedure because they are
41273** usually not needed, and by avoiding the stack initialization required
41274** for these steps, the main pcache1Fetch() procedure can run faster.
41275*/
41276static SQLITE_NOINLINE PgHdr1 *pcache1FetchStage2(
41277  PCache1 *pCache,
41278  unsigned int iKey,
41279  int createFlag
41280){
41281  unsigned int nPinned;
41282  PGroup *pGroup = pCache->pGroup;
41283  PgHdr1 *pPage = 0;
41284
41285  /* Step 3: Abort if createFlag is 1 but the cache is nearly full */
41286  assert( pCache->nPage >= pCache->nRecyclable );
41287  nPinned = pCache->nPage - pCache->nRecyclable;
41288  assert( pGroup->mxPinned == pGroup->nMaxPage + 10 - pGroup->nMinPage );
41289  assert( pCache->n90pct == pCache->nMax*9/10 );
41290  if( createFlag==1 && (
41291        nPinned>=pGroup->mxPinned
41292     || nPinned>=pCache->n90pct
41293     || (pcache1UnderMemoryPressure(pCache) && pCache->nRecyclable<nPinned)
41294  )){
41295    return 0;
41296  }
41297
41298  if( pCache->nPage>=pCache->nHash ) pcache1ResizeHash(pCache);
41299  assert( pCache->nHash>0 && pCache->apHash );
41300
41301  /* Step 4. Try to recycle a page. */
41302  if( pCache->bPurgeable
41303   && pGroup->pLruTail
41304   && ((pCache->nPage+1>=pCache->nMax) || pcache1UnderMemoryPressure(pCache))
41305  ){
41306    PCache1 *pOther;
41307    pPage = pGroup->pLruTail;
41308    assert( pPage->isPinned==0 );
41309    pcache1RemoveFromHash(pPage, 0);
41310    pcache1PinPage(pPage);
41311    pOther = pPage->pCache;
41312    if( pOther->szAlloc != pCache->szAlloc ){
41313      pcache1FreePage(pPage);
41314      pPage = 0;
41315    }else{
41316      pGroup->nCurrentPage -= (pOther->bPurgeable - pCache->bPurgeable);
41317    }
41318  }
41319
41320  /* Step 5. If a usable page buffer has still not been found,
41321  ** attempt to allocate a new one.
41322  */
41323  if( !pPage ){
41324    if( createFlag==1 ){ sqlite3BeginBenignMalloc(); }
41325    pPage = pcache1AllocPage(pCache);
41326    if( createFlag==1 ){ sqlite3EndBenignMalloc(); }
41327  }
41328
41329  if( pPage ){
41330    unsigned int h = iKey % pCache->nHash;
41331    pCache->nPage++;
41332    pPage->iKey = iKey;
41333    pPage->pNext = pCache->apHash[h];
41334    pPage->pCache = pCache;
41335    pPage->pLruPrev = 0;
41336    pPage->pLruNext = 0;
41337    pPage->isPinned = 1;
41338    *(void **)pPage->page.pExtra = 0;
41339    pCache->apHash[h] = pPage;
41340    if( iKey>pCache->iMaxKey ){
41341      pCache->iMaxKey = iKey;
41342    }
41343  }
41344  return pPage;
41345}
41346
41347/*
41348** Implementation of the sqlite3_pcache.xFetch method.
41349**
41350** Fetch a page by key value.
41351**
41352** Whether or not a new page may be allocated by this function depends on
41353** the value of the createFlag argument.  0 means do not allocate a new
41354** page.  1 means allocate a new page if space is easily available.  2
41355** means to try really hard to allocate a new page.
41356**
41357** For a non-purgeable cache (a cache used as the storage for an in-memory
41358** database) there is really no difference between createFlag 1 and 2.  So
41359** the calling function (pcache.c) will never have a createFlag of 1 on
41360** a non-purgeable cache.
41361**
41362** There are three different approaches to obtaining space for a page,
41363** depending on the value of parameter createFlag (which may be 0, 1 or 2).
41364**
41365**   1. Regardless of the value of createFlag, the cache is searched for a
41366**      copy of the requested page. If one is found, it is returned.
41367**
41368**   2. If createFlag==0 and the page is not already in the cache, NULL is
41369**      returned.
41370**
41371**   3. If createFlag is 1, and the page is not already in the cache, then
41372**      return NULL (do not allocate a new page) if any of the following
41373**      conditions are true:
41374**
41375**       (a) the number of pages pinned by the cache is greater than
41376**           PCache1.nMax, or
41377**
41378**       (b) the number of pages pinned by the cache is greater than
41379**           the sum of nMax for all purgeable caches, less the sum of
41380**           nMin for all other purgeable caches, or
41381**
41382**   4. If none of the first three conditions apply and the cache is marked
41383**      as purgeable, and if one of the following is true:
41384**
41385**       (a) The number of pages allocated for the cache is already
41386**           PCache1.nMax, or
41387**
41388**       (b) The number of pages allocated for all purgeable caches is
41389**           already equal to or greater than the sum of nMax for all
41390**           purgeable caches,
41391**
41392**       (c) The system is under memory pressure and wants to avoid
41393**           unnecessary pages cache entry allocations
41394**
41395**      then attempt to recycle a page from the LRU list. If it is the right
41396**      size, return the recycled buffer. Otherwise, free the buffer and
41397**      proceed to step 5.
41398**
41399**   5. Otherwise, allocate and return a new page buffer.
41400**
41401** There are two versions of this routine.  pcache1FetchWithMutex() is
41402** the general case.  pcache1FetchNoMutex() is a faster implementation for
41403** the common case where pGroup->mutex is NULL.  The pcache1Fetch() wrapper
41404** invokes the appropriate routine.
41405*/
41406static PgHdr1 *pcache1FetchNoMutex(
41407  sqlite3_pcache *p,
41408  unsigned int iKey,
41409  int createFlag
41410){
41411  PCache1 *pCache = (PCache1 *)p;
41412  PgHdr1 *pPage = 0;
41413
41414  /* Step 1: Search the hash table for an existing entry. */
41415  pPage = pCache->apHash[iKey % pCache->nHash];
41416  while( pPage && pPage->iKey!=iKey ){ pPage = pPage->pNext; }
41417
41418  /* Step 2: Abort if no existing page is found and createFlag is 0 */
41419  if( pPage ){
41420    if( !pPage->isPinned ){
41421      return pcache1PinPage(pPage);
41422    }else{
41423      return pPage;
41424    }
41425  }else if( createFlag ){
41426    /* Steps 3, 4, and 5 implemented by this subroutine */
41427    return pcache1FetchStage2(pCache, iKey, createFlag);
41428  }else{
41429    return 0;
41430  }
41431}
41432#if PCACHE1_MIGHT_USE_GROUP_MUTEX
41433static PgHdr1 *pcache1FetchWithMutex(
41434  sqlite3_pcache *p,
41435  unsigned int iKey,
41436  int createFlag
41437){
41438  PCache1 *pCache = (PCache1 *)p;
41439  PgHdr1 *pPage;
41440
41441  pcache1EnterMutex(pCache->pGroup);
41442  pPage = pcache1FetchNoMutex(p, iKey, createFlag);
41443  assert( pPage==0 || pCache->iMaxKey>=iKey );
41444  pcache1LeaveMutex(pCache->pGroup);
41445  return pPage;
41446}
41447#endif
41448static sqlite3_pcache_page *pcache1Fetch(
41449  sqlite3_pcache *p,
41450  unsigned int iKey,
41451  int createFlag
41452){
41453#if PCACHE1_MIGHT_USE_GROUP_MUTEX || defined(SQLITE_DEBUG)
41454  PCache1 *pCache = (PCache1 *)p;
41455#endif
41456
41457  assert( offsetof(PgHdr1,page)==0 );
41458  assert( pCache->bPurgeable || createFlag!=1 );
41459  assert( pCache->bPurgeable || pCache->nMin==0 );
41460  assert( pCache->bPurgeable==0 || pCache->nMin==10 );
41461  assert( pCache->nMin==0 || pCache->bPurgeable );
41462  assert( pCache->nHash>0 );
41463#if PCACHE1_MIGHT_USE_GROUP_MUTEX
41464  if( pCache->pGroup->mutex ){
41465    return (sqlite3_pcache_page*)pcache1FetchWithMutex(p, iKey, createFlag);
41466  }else
41467#endif
41468  {
41469    return (sqlite3_pcache_page*)pcache1FetchNoMutex(p, iKey, createFlag);
41470  }
41471}
41472
41473
41474/*
41475** Implementation of the sqlite3_pcache.xUnpin method.
41476**
41477** Mark a page as unpinned (eligible for asynchronous recycling).
41478*/
41479static void pcache1Unpin(
41480  sqlite3_pcache *p,
41481  sqlite3_pcache_page *pPg,
41482  int reuseUnlikely
41483){
41484  PCache1 *pCache = (PCache1 *)p;
41485  PgHdr1 *pPage = (PgHdr1 *)pPg;
41486  PGroup *pGroup = pCache->pGroup;
41487
41488  assert( pPage->pCache==pCache );
41489  pcache1EnterMutex(pGroup);
41490
41491  /* It is an error to call this function if the page is already
41492  ** part of the PGroup LRU list.
41493  */
41494  assert( pPage->pLruPrev==0 && pPage->pLruNext==0 );
41495  assert( pGroup->pLruHead!=pPage && pGroup->pLruTail!=pPage );
41496  assert( pPage->isPinned==1 );
41497
41498  if( reuseUnlikely || pGroup->nCurrentPage>pGroup->nMaxPage ){
41499    pcache1RemoveFromHash(pPage, 1);
41500  }else{
41501    /* Add the page to the PGroup LRU list. */
41502    if( pGroup->pLruHead ){
41503      pGroup->pLruHead->pLruPrev = pPage;
41504      pPage->pLruNext = pGroup->pLruHead;
41505      pGroup->pLruHead = pPage;
41506    }else{
41507      pGroup->pLruTail = pPage;
41508      pGroup->pLruHead = pPage;
41509    }
41510    pCache->nRecyclable++;
41511    pPage->isPinned = 0;
41512  }
41513
41514  pcache1LeaveMutex(pCache->pGroup);
41515}
41516
41517/*
41518** Implementation of the sqlite3_pcache.xRekey method.
41519*/
41520static void pcache1Rekey(
41521  sqlite3_pcache *p,
41522  sqlite3_pcache_page *pPg,
41523  unsigned int iOld,
41524  unsigned int iNew
41525){
41526  PCache1 *pCache = (PCache1 *)p;
41527  PgHdr1 *pPage = (PgHdr1 *)pPg;
41528  PgHdr1 **pp;
41529  unsigned int h;
41530  assert( pPage->iKey==iOld );
41531  assert( pPage->pCache==pCache );
41532
41533  pcache1EnterMutex(pCache->pGroup);
41534
41535  h = iOld%pCache->nHash;
41536  pp = &pCache->apHash[h];
41537  while( (*pp)!=pPage ){
41538    pp = &(*pp)->pNext;
41539  }
41540  *pp = pPage->pNext;
41541
41542  h = iNew%pCache->nHash;
41543  pPage->iKey = iNew;
41544  pPage->pNext = pCache->apHash[h];
41545  pCache->apHash[h] = pPage;
41546  if( iNew>pCache->iMaxKey ){
41547    pCache->iMaxKey = iNew;
41548  }
41549
41550  pcache1LeaveMutex(pCache->pGroup);
41551}
41552
41553/*
41554** Implementation of the sqlite3_pcache.xTruncate method.
41555**
41556** Discard all unpinned pages in the cache with a page number equal to
41557** or greater than parameter iLimit. Any pinned pages with a page number
41558** equal to or greater than iLimit are implicitly unpinned.
41559*/
41560static void pcache1Truncate(sqlite3_pcache *p, unsigned int iLimit){
41561  PCache1 *pCache = (PCache1 *)p;
41562  pcache1EnterMutex(pCache->pGroup);
41563  if( iLimit<=pCache->iMaxKey ){
41564    pcache1TruncateUnsafe(pCache, iLimit);
41565    pCache->iMaxKey = iLimit-1;
41566  }
41567  pcache1LeaveMutex(pCache->pGroup);
41568}
41569
41570/*
41571** Implementation of the sqlite3_pcache.xDestroy method.
41572**
41573** Destroy a cache allocated using pcache1Create().
41574*/
41575static void pcache1Destroy(sqlite3_pcache *p){
41576  PCache1 *pCache = (PCache1 *)p;
41577  PGroup *pGroup = pCache->pGroup;
41578  assert( pCache->bPurgeable || (pCache->nMax==0 && pCache->nMin==0) );
41579  pcache1EnterMutex(pGroup);
41580  pcache1TruncateUnsafe(pCache, 0);
41581  assert( pGroup->nMaxPage >= pCache->nMax );
41582  pGroup->nMaxPage -= pCache->nMax;
41583  assert( pGroup->nMinPage >= pCache->nMin );
41584  pGroup->nMinPage -= pCache->nMin;
41585  pGroup->mxPinned = pGroup->nMaxPage + 10 - pGroup->nMinPage;
41586  pcache1EnforceMaxPage(pCache);
41587  pcache1LeaveMutex(pGroup);
41588  sqlite3_free(pCache->pBulk);
41589  sqlite3_free(pCache->apHash);
41590  sqlite3_free(pCache);
41591}
41592
41593/*
41594** This function is called during initialization (sqlite3_initialize()) to
41595** install the default pluggable cache module, assuming the user has not
41596** already provided an alternative.
41597*/
41598SQLITE_PRIVATE void sqlite3PCacheSetDefault(void){
41599  static const sqlite3_pcache_methods2 defaultMethods = {
41600    1,                       /* iVersion */
41601    0,                       /* pArg */
41602    pcache1Init,             /* xInit */
41603    pcache1Shutdown,         /* xShutdown */
41604    pcache1Create,           /* xCreate */
41605    pcache1Cachesize,        /* xCachesize */
41606    pcache1Pagecount,        /* xPagecount */
41607    pcache1Fetch,            /* xFetch */
41608    pcache1Unpin,            /* xUnpin */
41609    pcache1Rekey,            /* xRekey */
41610    pcache1Truncate,         /* xTruncate */
41611    pcache1Destroy,          /* xDestroy */
41612    pcache1Shrink            /* xShrink */
41613  };
41614  sqlite3_config(SQLITE_CONFIG_PCACHE2, &defaultMethods);
41615}
41616
41617/*
41618** Return the size of the header on each page of this PCACHE implementation.
41619*/
41620SQLITE_PRIVATE int sqlite3HeaderSizePcache1(void){ return ROUND8(sizeof(PgHdr1)); }
41621
41622/*
41623** Return the global mutex used by this PCACHE implementation.  The
41624** sqlite3_status() routine needs access to this mutex.
41625*/
41626SQLITE_PRIVATE sqlite3_mutex *sqlite3Pcache1Mutex(void){
41627  return pcache1.mutex;
41628}
41629
41630#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
41631/*
41632** This function is called to free superfluous dynamically allocated memory
41633** held by the pager system. Memory in use by any SQLite pager allocated
41634** by the current thread may be sqlite3_free()ed.
41635**
41636** nReq is the number of bytes of memory required. Once this much has
41637** been released, the function returns. The return value is the total number
41638** of bytes of memory released.
41639*/
41640SQLITE_PRIVATE int sqlite3PcacheReleaseMemory(int nReq){
41641  int nFree = 0;
41642  assert( sqlite3_mutex_notheld(pcache1.grp.mutex) );
41643  assert( sqlite3_mutex_notheld(pcache1.mutex) );
41644  if( sqlite3GlobalConfig.nPage==0 ){
41645    PgHdr1 *p;
41646    pcache1EnterMutex(&pcache1.grp);
41647    while( (nReq<0 || nFree<nReq) && ((p=pcache1.grp.pLruTail)!=0) ){
41648      nFree += pcache1MemSize(p->page.pBuf);
41649#ifdef SQLITE_PCACHE_SEPARATE_HEADER
41650      nFree += sqlite3MemSize(p);
41651#endif
41652      assert( p->isPinned==0 );
41653      pcache1PinPage(p);
41654      pcache1RemoveFromHash(p, 1);
41655    }
41656    pcache1LeaveMutex(&pcache1.grp);
41657  }
41658  return nFree;
41659}
41660#endif /* SQLITE_ENABLE_MEMORY_MANAGEMENT */
41661
41662#ifdef SQLITE_TEST
41663/*
41664** This function is used by test procedures to inspect the internal state
41665** of the global cache.
41666*/
41667SQLITE_PRIVATE void sqlite3PcacheStats(
41668  int *pnCurrent,      /* OUT: Total number of pages cached */
41669  int *pnMax,          /* OUT: Global maximum cache size */
41670  int *pnMin,          /* OUT: Sum of PCache1.nMin for purgeable caches */
41671  int *pnRecyclable    /* OUT: Total number of pages available for recycling */
41672){
41673  PgHdr1 *p;
41674  int nRecyclable = 0;
41675  for(p=pcache1.grp.pLruHead; p; p=p->pLruNext){
41676    assert( p->isPinned==0 );
41677    nRecyclable++;
41678  }
41679  *pnCurrent = pcache1.grp.nCurrentPage;
41680  *pnMax = (int)pcache1.grp.nMaxPage;
41681  *pnMin = (int)pcache1.grp.nMinPage;
41682  *pnRecyclable = nRecyclable;
41683}
41684#endif
41685
41686/************** End of pcache1.c *********************************************/
41687/************** Begin file rowset.c ******************************************/
41688/*
41689** 2008 December 3
41690**
41691** The author disclaims copyright to this source code.  In place of
41692** a legal notice, here is a blessing:
41693**
41694**    May you do good and not evil.
41695**    May you find forgiveness for yourself and forgive others.
41696**    May you share freely, never taking more than you give.
41697**
41698*************************************************************************
41699**
41700** This module implements an object we call a "RowSet".
41701**
41702** The RowSet object is a collection of rowids.  Rowids
41703** are inserted into the RowSet in an arbitrary order.  Inserts
41704** can be intermixed with tests to see if a given rowid has been
41705** previously inserted into the RowSet.
41706**
41707** After all inserts are finished, it is possible to extract the
41708** elements of the RowSet in sorted order.  Once this extraction
41709** process has started, no new elements may be inserted.
41710**
41711** Hence, the primitive operations for a RowSet are:
41712**
41713**    CREATE
41714**    INSERT
41715**    TEST
41716**    SMALLEST
41717**    DESTROY
41718**
41719** The CREATE and DESTROY primitives are the constructor and destructor,
41720** obviously.  The INSERT primitive adds a new element to the RowSet.
41721** TEST checks to see if an element is already in the RowSet.  SMALLEST
41722** extracts the least value from the RowSet.
41723**
41724** The INSERT primitive might allocate additional memory.  Memory is
41725** allocated in chunks so most INSERTs do no allocation.  There is an
41726** upper bound on the size of allocated memory.  No memory is freed
41727** until DESTROY.
41728**
41729** The TEST primitive includes a "batch" number.  The TEST primitive
41730** will only see elements that were inserted before the last change
41731** in the batch number.  In other words, if an INSERT occurs between
41732** two TESTs where the TESTs have the same batch nubmer, then the
41733** value added by the INSERT will not be visible to the second TEST.
41734** The initial batch number is zero, so if the very first TEST contains
41735** a non-zero batch number, it will see all prior INSERTs.
41736**
41737** No INSERTs may occurs after a SMALLEST.  An assertion will fail if
41738** that is attempted.
41739**
41740** The cost of an INSERT is roughly constant.  (Sometimes new memory
41741** has to be allocated on an INSERT.)  The cost of a TEST with a new
41742** batch number is O(NlogN) where N is the number of elements in the RowSet.
41743** The cost of a TEST using the same batch number is O(logN).  The cost
41744** of the first SMALLEST is O(NlogN).  Second and subsequent SMALLEST
41745** primitives are constant time.  The cost of DESTROY is O(N).
41746**
41747** There is an added cost of O(N) when switching between TEST and
41748** SMALLEST primitives.
41749*/
41750/* #include "sqliteInt.h" */
41751
41752
41753/*
41754** Target size for allocation chunks.
41755*/
41756#define ROWSET_ALLOCATION_SIZE 1024
41757
41758/*
41759** The number of rowset entries per allocation chunk.
41760*/
41761#define ROWSET_ENTRY_PER_CHUNK  \
41762                       ((ROWSET_ALLOCATION_SIZE-8)/sizeof(struct RowSetEntry))
41763
41764/*
41765** Each entry in a RowSet is an instance of the following object.
41766**
41767** This same object is reused to store a linked list of trees of RowSetEntry
41768** objects.  In that alternative use, pRight points to the next entry
41769** in the list, pLeft points to the tree, and v is unused.  The
41770** RowSet.pForest value points to the head of this forest list.
41771*/
41772struct RowSetEntry {
41773  i64 v;                        /* ROWID value for this entry */
41774  struct RowSetEntry *pRight;   /* Right subtree (larger entries) or list */
41775  struct RowSetEntry *pLeft;    /* Left subtree (smaller entries) */
41776};
41777
41778/*
41779** RowSetEntry objects are allocated in large chunks (instances of the
41780** following structure) to reduce memory allocation overhead.  The
41781** chunks are kept on a linked list so that they can be deallocated
41782** when the RowSet is destroyed.
41783*/
41784struct RowSetChunk {
41785  struct RowSetChunk *pNextChunk;        /* Next chunk on list of them all */
41786  struct RowSetEntry aEntry[ROWSET_ENTRY_PER_CHUNK]; /* Allocated entries */
41787};
41788
41789/*
41790** A RowSet in an instance of the following structure.
41791**
41792** A typedef of this structure if found in sqliteInt.h.
41793*/
41794struct RowSet {
41795  struct RowSetChunk *pChunk;    /* List of all chunk allocations */
41796  sqlite3 *db;                   /* The database connection */
41797  struct RowSetEntry *pEntry;    /* List of entries using pRight */
41798  struct RowSetEntry *pLast;     /* Last entry on the pEntry list */
41799  struct RowSetEntry *pFresh;    /* Source of new entry objects */
41800  struct RowSetEntry *pForest;   /* List of binary trees of entries */
41801  u16 nFresh;                    /* Number of objects on pFresh */
41802  u16 rsFlags;                   /* Various flags */
41803  int iBatch;                    /* Current insert batch */
41804};
41805
41806/*
41807** Allowed values for RowSet.rsFlags
41808*/
41809#define ROWSET_SORTED  0x01   /* True if RowSet.pEntry is sorted */
41810#define ROWSET_NEXT    0x02   /* True if sqlite3RowSetNext() has been called */
41811
41812/*
41813** Turn bulk memory into a RowSet object.  N bytes of memory
41814** are available at pSpace.  The db pointer is used as a memory context
41815** for any subsequent allocations that need to occur.
41816** Return a pointer to the new RowSet object.
41817**
41818** It must be the case that N is sufficient to make a Rowset.  If not
41819** an assertion fault occurs.
41820**
41821** If N is larger than the minimum, use the surplus as an initial
41822** allocation of entries available to be filled.
41823*/
41824SQLITE_PRIVATE RowSet *sqlite3RowSetInit(sqlite3 *db, void *pSpace, unsigned int N){
41825  RowSet *p;
41826  assert( N >= ROUND8(sizeof(*p)) );
41827  p = pSpace;
41828  p->pChunk = 0;
41829  p->db = db;
41830  p->pEntry = 0;
41831  p->pLast = 0;
41832  p->pForest = 0;
41833  p->pFresh = (struct RowSetEntry*)(ROUND8(sizeof(*p)) + (char*)p);
41834  p->nFresh = (u16)((N - ROUND8(sizeof(*p)))/sizeof(struct RowSetEntry));
41835  p->rsFlags = ROWSET_SORTED;
41836  p->iBatch = 0;
41837  return p;
41838}
41839
41840/*
41841** Deallocate all chunks from a RowSet.  This frees all memory that
41842** the RowSet has allocated over its lifetime.  This routine is
41843** the destructor for the RowSet.
41844*/
41845SQLITE_PRIVATE void sqlite3RowSetClear(RowSet *p){
41846  struct RowSetChunk *pChunk, *pNextChunk;
41847  for(pChunk=p->pChunk; pChunk; pChunk = pNextChunk){
41848    pNextChunk = pChunk->pNextChunk;
41849    sqlite3DbFree(p->db, pChunk);
41850  }
41851  p->pChunk = 0;
41852  p->nFresh = 0;
41853  p->pEntry = 0;
41854  p->pLast = 0;
41855  p->pForest = 0;
41856  p->rsFlags = ROWSET_SORTED;
41857}
41858
41859/*
41860** Allocate a new RowSetEntry object that is associated with the
41861** given RowSet.  Return a pointer to the new and completely uninitialized
41862** objected.
41863**
41864** In an OOM situation, the RowSet.db->mallocFailed flag is set and this
41865** routine returns NULL.
41866*/
41867static struct RowSetEntry *rowSetEntryAlloc(RowSet *p){
41868  assert( p!=0 );
41869  if( p->nFresh==0 ){
41870    struct RowSetChunk *pNew;
41871    pNew = sqlite3DbMallocRaw(p->db, sizeof(*pNew));
41872    if( pNew==0 ){
41873      return 0;
41874    }
41875    pNew->pNextChunk = p->pChunk;
41876    p->pChunk = pNew;
41877    p->pFresh = pNew->aEntry;
41878    p->nFresh = ROWSET_ENTRY_PER_CHUNK;
41879  }
41880  p->nFresh--;
41881  return p->pFresh++;
41882}
41883
41884/*
41885** Insert a new value into a RowSet.
41886**
41887** The mallocFailed flag of the database connection is set if a
41888** memory allocation fails.
41889*/
41890SQLITE_PRIVATE void sqlite3RowSetInsert(RowSet *p, i64 rowid){
41891  struct RowSetEntry *pEntry;  /* The new entry */
41892  struct RowSetEntry *pLast;   /* The last prior entry */
41893
41894  /* This routine is never called after sqlite3RowSetNext() */
41895  assert( p!=0 && (p->rsFlags & ROWSET_NEXT)==0 );
41896
41897  pEntry = rowSetEntryAlloc(p);
41898  if( pEntry==0 ) return;
41899  pEntry->v = rowid;
41900  pEntry->pRight = 0;
41901  pLast = p->pLast;
41902  if( pLast ){
41903    if( (p->rsFlags & ROWSET_SORTED)!=0 && rowid<=pLast->v ){
41904      p->rsFlags &= ~ROWSET_SORTED;
41905    }
41906    pLast->pRight = pEntry;
41907  }else{
41908    p->pEntry = pEntry;
41909  }
41910  p->pLast = pEntry;
41911}
41912
41913/*
41914** Merge two lists of RowSetEntry objects.  Remove duplicates.
41915**
41916** The input lists are connected via pRight pointers and are
41917** assumed to each already be in sorted order.
41918*/
41919static struct RowSetEntry *rowSetEntryMerge(
41920  struct RowSetEntry *pA,    /* First sorted list to be merged */
41921  struct RowSetEntry *pB     /* Second sorted list to be merged */
41922){
41923  struct RowSetEntry head;
41924  struct RowSetEntry *pTail;
41925
41926  pTail = &head;
41927  while( pA && pB ){
41928    assert( pA->pRight==0 || pA->v<=pA->pRight->v );
41929    assert( pB->pRight==0 || pB->v<=pB->pRight->v );
41930    if( pA->v<pB->v ){
41931      pTail->pRight = pA;
41932      pA = pA->pRight;
41933      pTail = pTail->pRight;
41934    }else if( pB->v<pA->v ){
41935      pTail->pRight = pB;
41936      pB = pB->pRight;
41937      pTail = pTail->pRight;
41938    }else{
41939      pA = pA->pRight;
41940    }
41941  }
41942  if( pA ){
41943    assert( pA->pRight==0 || pA->v<=pA->pRight->v );
41944    pTail->pRight = pA;
41945  }else{
41946    assert( pB==0 || pB->pRight==0 || pB->v<=pB->pRight->v );
41947    pTail->pRight = pB;
41948  }
41949  return head.pRight;
41950}
41951
41952/*
41953** Sort all elements on the list of RowSetEntry objects into order of
41954** increasing v.
41955*/
41956static struct RowSetEntry *rowSetEntrySort(struct RowSetEntry *pIn){
41957  unsigned int i;
41958  struct RowSetEntry *pNext, *aBucket[40];
41959
41960  memset(aBucket, 0, sizeof(aBucket));
41961  while( pIn ){
41962    pNext = pIn->pRight;
41963    pIn->pRight = 0;
41964    for(i=0; aBucket[i]; i++){
41965      pIn = rowSetEntryMerge(aBucket[i], pIn);
41966      aBucket[i] = 0;
41967    }
41968    aBucket[i] = pIn;
41969    pIn = pNext;
41970  }
41971  pIn = 0;
41972  for(i=0; i<sizeof(aBucket)/sizeof(aBucket[0]); i++){
41973    pIn = rowSetEntryMerge(pIn, aBucket[i]);
41974  }
41975  return pIn;
41976}
41977
41978
41979/*
41980** The input, pIn, is a binary tree (or subtree) of RowSetEntry objects.
41981** Convert this tree into a linked list connected by the pRight pointers
41982** and return pointers to the first and last elements of the new list.
41983*/
41984static void rowSetTreeToList(
41985  struct RowSetEntry *pIn,         /* Root of the input tree */
41986  struct RowSetEntry **ppFirst,    /* Write head of the output list here */
41987  struct RowSetEntry **ppLast      /* Write tail of the output list here */
41988){
41989  assert( pIn!=0 );
41990  if( pIn->pLeft ){
41991    struct RowSetEntry *p;
41992    rowSetTreeToList(pIn->pLeft, ppFirst, &p);
41993    p->pRight = pIn;
41994  }else{
41995    *ppFirst = pIn;
41996  }
41997  if( pIn->pRight ){
41998    rowSetTreeToList(pIn->pRight, &pIn->pRight, ppLast);
41999  }else{
42000    *ppLast = pIn;
42001  }
42002  assert( (*ppLast)->pRight==0 );
42003}
42004
42005
42006/*
42007** Convert a sorted list of elements (connected by pRight) into a binary
42008** tree with depth of iDepth.  A depth of 1 means the tree contains a single
42009** node taken from the head of *ppList.  A depth of 2 means a tree with
42010** three nodes.  And so forth.
42011**
42012** Use as many entries from the input list as required and update the
42013** *ppList to point to the unused elements of the list.  If the input
42014** list contains too few elements, then construct an incomplete tree
42015** and leave *ppList set to NULL.
42016**
42017** Return a pointer to the root of the constructed binary tree.
42018*/
42019static struct RowSetEntry *rowSetNDeepTree(
42020  struct RowSetEntry **ppList,
42021  int iDepth
42022){
42023  struct RowSetEntry *p;         /* Root of the new tree */
42024  struct RowSetEntry *pLeft;     /* Left subtree */
42025  if( *ppList==0 ){
42026    return 0;
42027  }
42028  if( iDepth==1 ){
42029    p = *ppList;
42030    *ppList = p->pRight;
42031    p->pLeft = p->pRight = 0;
42032    return p;
42033  }
42034  pLeft = rowSetNDeepTree(ppList, iDepth-1);
42035  p = *ppList;
42036  if( p==0 ){
42037    return pLeft;
42038  }
42039  p->pLeft = pLeft;
42040  *ppList = p->pRight;
42041  p->pRight = rowSetNDeepTree(ppList, iDepth-1);
42042  return p;
42043}
42044
42045/*
42046** Convert a sorted list of elements into a binary tree. Make the tree
42047** as deep as it needs to be in order to contain the entire list.
42048*/
42049static struct RowSetEntry *rowSetListToTree(struct RowSetEntry *pList){
42050  int iDepth;           /* Depth of the tree so far */
42051  struct RowSetEntry *p;       /* Current tree root */
42052  struct RowSetEntry *pLeft;   /* Left subtree */
42053
42054  assert( pList!=0 );
42055  p = pList;
42056  pList = p->pRight;
42057  p->pLeft = p->pRight = 0;
42058  for(iDepth=1; pList; iDepth++){
42059    pLeft = p;
42060    p = pList;
42061    pList = p->pRight;
42062    p->pLeft = pLeft;
42063    p->pRight = rowSetNDeepTree(&pList, iDepth);
42064  }
42065  return p;
42066}
42067
42068/*
42069** Take all the entries on p->pEntry and on the trees in p->pForest and
42070** sort them all together into one big ordered list on p->pEntry.
42071**
42072** This routine should only be called once in the life of a RowSet.
42073*/
42074static void rowSetToList(RowSet *p){
42075
42076  /* This routine is called only once */
42077  assert( p!=0 && (p->rsFlags & ROWSET_NEXT)==0 );
42078
42079  if( (p->rsFlags & ROWSET_SORTED)==0 ){
42080    p->pEntry = rowSetEntrySort(p->pEntry);
42081  }
42082
42083  /* While this module could theoretically support it, sqlite3RowSetNext()
42084  ** is never called after sqlite3RowSetText() for the same RowSet.  So
42085  ** there is never a forest to deal with.  Should this change, simply
42086  ** remove the assert() and the #if 0. */
42087  assert( p->pForest==0 );
42088#if 0
42089  while( p->pForest ){
42090    struct RowSetEntry *pTree = p->pForest->pLeft;
42091    if( pTree ){
42092      struct RowSetEntry *pHead, *pTail;
42093      rowSetTreeToList(pTree, &pHead, &pTail);
42094      p->pEntry = rowSetEntryMerge(p->pEntry, pHead);
42095    }
42096    p->pForest = p->pForest->pRight;
42097  }
42098#endif
42099  p->rsFlags |= ROWSET_NEXT;  /* Verify this routine is never called again */
42100}
42101
42102/*
42103** Extract the smallest element from the RowSet.
42104** Write the element into *pRowid.  Return 1 on success.  Return
42105** 0 if the RowSet is already empty.
42106**
42107** After this routine has been called, the sqlite3RowSetInsert()
42108** routine may not be called again.
42109*/
42110SQLITE_PRIVATE int sqlite3RowSetNext(RowSet *p, i64 *pRowid){
42111  assert( p!=0 );
42112
42113  /* Merge the forest into a single sorted list on first call */
42114  if( (p->rsFlags & ROWSET_NEXT)==0 ) rowSetToList(p);
42115
42116  /* Return the next entry on the list */
42117  if( p->pEntry ){
42118    *pRowid = p->pEntry->v;
42119    p->pEntry = p->pEntry->pRight;
42120    if( p->pEntry==0 ){
42121      sqlite3RowSetClear(p);
42122    }
42123    return 1;
42124  }else{
42125    return 0;
42126  }
42127}
42128
42129/*
42130** Check to see if element iRowid was inserted into the rowset as
42131** part of any insert batch prior to iBatch.  Return 1 or 0.
42132**
42133** If this is the first test of a new batch and if there exist entries
42134** on pRowSet->pEntry, then sort those entries into the forest at
42135** pRowSet->pForest so that they can be tested.
42136*/
42137SQLITE_PRIVATE int sqlite3RowSetTest(RowSet *pRowSet, int iBatch, sqlite3_int64 iRowid){
42138  struct RowSetEntry *p, *pTree;
42139
42140  /* This routine is never called after sqlite3RowSetNext() */
42141  assert( pRowSet!=0 && (pRowSet->rsFlags & ROWSET_NEXT)==0 );
42142
42143  /* Sort entries into the forest on the first test of a new batch
42144  */
42145  if( iBatch!=pRowSet->iBatch ){
42146    p = pRowSet->pEntry;
42147    if( p ){
42148      struct RowSetEntry **ppPrevTree = &pRowSet->pForest;
42149      if( (pRowSet->rsFlags & ROWSET_SORTED)==0 ){
42150        p = rowSetEntrySort(p);
42151      }
42152      for(pTree = pRowSet->pForest; pTree; pTree=pTree->pRight){
42153        ppPrevTree = &pTree->pRight;
42154        if( pTree->pLeft==0 ){
42155          pTree->pLeft = rowSetListToTree(p);
42156          break;
42157        }else{
42158          struct RowSetEntry *pAux, *pTail;
42159          rowSetTreeToList(pTree->pLeft, &pAux, &pTail);
42160          pTree->pLeft = 0;
42161          p = rowSetEntryMerge(pAux, p);
42162        }
42163      }
42164      if( pTree==0 ){
42165        *ppPrevTree = pTree = rowSetEntryAlloc(pRowSet);
42166        if( pTree ){
42167          pTree->v = 0;
42168          pTree->pRight = 0;
42169          pTree->pLeft = rowSetListToTree(p);
42170        }
42171      }
42172      pRowSet->pEntry = 0;
42173      pRowSet->pLast = 0;
42174      pRowSet->rsFlags |= ROWSET_SORTED;
42175    }
42176    pRowSet->iBatch = iBatch;
42177  }
42178
42179  /* Test to see if the iRowid value appears anywhere in the forest.
42180  ** Return 1 if it does and 0 if not.
42181  */
42182  for(pTree = pRowSet->pForest; pTree; pTree=pTree->pRight){
42183    p = pTree->pLeft;
42184    while( p ){
42185      if( p->v<iRowid ){
42186        p = p->pRight;
42187      }else if( p->v>iRowid ){
42188        p = p->pLeft;
42189      }else{
42190        return 1;
42191      }
42192    }
42193  }
42194  return 0;
42195}
42196
42197/************** End of rowset.c **********************************************/
42198/************** Begin file pager.c *******************************************/
42199/*
42200** 2001 September 15
42201**
42202** The author disclaims copyright to this source code.  In place of
42203** a legal notice, here is a blessing:
42204**
42205**    May you do good and not evil.
42206**    May you find forgiveness for yourself and forgive others.
42207**    May you share freely, never taking more than you give.
42208**
42209*************************************************************************
42210** This is the implementation of the page cache subsystem or "pager".
42211**
42212** The pager is used to access a database disk file.  It implements
42213** atomic commit and rollback through the use of a journal file that
42214** is separate from the database file.  The pager also implements file
42215** locking to prevent two processes from writing the same database
42216** file simultaneously, or one process from reading the database while
42217** another is writing.
42218*/
42219#ifndef SQLITE_OMIT_DISKIO
42220/* #include "sqliteInt.h" */
42221/************** Include wal.h in the middle of pager.c ***********************/
42222/************** Begin file wal.h *********************************************/
42223/*
42224** 2010 February 1
42225**
42226** The author disclaims copyright to this source code.  In place of
42227** a legal notice, here is a blessing:
42228**
42229**    May you do good and not evil.
42230**    May you find forgiveness for yourself and forgive others.
42231**    May you share freely, never taking more than you give.
42232**
42233*************************************************************************
42234** This header file defines the interface to the write-ahead logging
42235** system. Refer to the comments below and the header comment attached to
42236** the implementation of each function in log.c for further details.
42237*/
42238
42239#ifndef _WAL_H_
42240#define _WAL_H_
42241
42242/* #include "sqliteInt.h" */
42243
42244/* Additional values that can be added to the sync_flags argument of
42245** sqlite3WalFrames():
42246*/
42247#define WAL_SYNC_TRANSACTIONS  0x20   /* Sync at the end of each transaction */
42248#define SQLITE_SYNC_MASK       0x13   /* Mask off the SQLITE_SYNC_* values */
42249
42250#ifdef SQLITE_OMIT_WAL
42251# define sqlite3WalOpen(x,y,z)                   0
42252# define sqlite3WalLimit(x,y)
42253# define sqlite3WalClose(w,x,y,z)                0
42254# define sqlite3WalBeginReadTransaction(y,z)     0
42255# define sqlite3WalEndReadTransaction(z)
42256# define sqlite3WalDbsize(y)                     0
42257# define sqlite3WalBeginWriteTransaction(y)      0
42258# define sqlite3WalEndWriteTransaction(x)        0
42259# define sqlite3WalUndo(x,y,z)                   0
42260# define sqlite3WalSavepoint(y,z)
42261# define sqlite3WalSavepointUndo(y,z)            0
42262# define sqlite3WalFrames(u,v,w,x,y,z)           0
42263# define sqlite3WalCheckpoint(r,s,t,u,v,w,x,y,z) 0
42264# define sqlite3WalCallback(z)                   0
42265# define sqlite3WalExclusiveMode(y,z)            0
42266# define sqlite3WalHeapMemory(z)                 0
42267# define sqlite3WalFramesize(z)                  0
42268# define sqlite3WalFindFrame(x,y,z)              0
42269#else
42270
42271#define WAL_SAVEPOINT_NDATA 4
42272
42273/* Connection to a write-ahead log (WAL) file.
42274** There is one object of this type for each pager.
42275*/
42276typedef struct Wal Wal;
42277
42278/* Open and close a connection to a write-ahead log. */
42279SQLITE_PRIVATE int sqlite3WalOpen(sqlite3_vfs*, sqlite3_file*, const char *, int, i64, Wal**);
42280SQLITE_PRIVATE int sqlite3WalClose(Wal *pWal, int sync_flags, int, u8 *);
42281
42282/* Set the limiting size of a WAL file. */
42283SQLITE_PRIVATE void sqlite3WalLimit(Wal*, i64);
42284
42285/* Used by readers to open (lock) and close (unlock) a snapshot.  A
42286** snapshot is like a read-transaction.  It is the state of the database
42287** at an instant in time.  sqlite3WalOpenSnapshot gets a read lock and
42288** preserves the current state even if the other threads or processes
42289** write to or checkpoint the WAL.  sqlite3WalCloseSnapshot() closes the
42290** transaction and releases the lock.
42291*/
42292SQLITE_PRIVATE int sqlite3WalBeginReadTransaction(Wal *pWal, int *);
42293SQLITE_PRIVATE void sqlite3WalEndReadTransaction(Wal *pWal);
42294
42295/* Read a page from the write-ahead log, if it is present. */
42296SQLITE_PRIVATE int sqlite3WalFindFrame(Wal *, Pgno, u32 *);
42297SQLITE_PRIVATE int sqlite3WalReadFrame(Wal *, u32, int, u8 *);
42298
42299/* If the WAL is not empty, return the size of the database. */
42300SQLITE_PRIVATE Pgno sqlite3WalDbsize(Wal *pWal);
42301
42302/* Obtain or release the WRITER lock. */
42303SQLITE_PRIVATE int sqlite3WalBeginWriteTransaction(Wal *pWal);
42304SQLITE_PRIVATE int sqlite3WalEndWriteTransaction(Wal *pWal);
42305
42306/* Undo any frames written (but not committed) to the log */
42307SQLITE_PRIVATE int sqlite3WalUndo(Wal *pWal, int (*xUndo)(void *, Pgno), void *pUndoCtx);
42308
42309/* Return an integer that records the current (uncommitted) write
42310** position in the WAL */
42311SQLITE_PRIVATE void sqlite3WalSavepoint(Wal *pWal, u32 *aWalData);
42312
42313/* Move the write position of the WAL back to iFrame.  Called in
42314** response to a ROLLBACK TO command. */
42315SQLITE_PRIVATE int sqlite3WalSavepointUndo(Wal *pWal, u32 *aWalData);
42316
42317/* Write a frame or frames to the log. */
42318SQLITE_PRIVATE int sqlite3WalFrames(Wal *pWal, int, PgHdr *, Pgno, int, int);
42319
42320/* Copy pages from the log to the database file */
42321SQLITE_PRIVATE int sqlite3WalCheckpoint(
42322  Wal *pWal,                      /* Write-ahead log connection */
42323  int eMode,                      /* One of PASSIVE, FULL and RESTART */
42324  int (*xBusy)(void*),            /* Function to call when busy */
42325  void *pBusyArg,                 /* Context argument for xBusyHandler */
42326  int sync_flags,                 /* Flags to sync db file with (or 0) */
42327  int nBuf,                       /* Size of buffer nBuf */
42328  u8 *zBuf,                       /* Temporary buffer to use */
42329  int *pnLog,                     /* OUT: Number of frames in WAL */
42330  int *pnCkpt                     /* OUT: Number of backfilled frames in WAL */
42331);
42332
42333/* Return the value to pass to a sqlite3_wal_hook callback, the
42334** number of frames in the WAL at the point of the last commit since
42335** sqlite3WalCallback() was called.  If no commits have occurred since
42336** the last call, then return 0.
42337*/
42338SQLITE_PRIVATE int sqlite3WalCallback(Wal *pWal);
42339
42340/* Tell the wal layer that an EXCLUSIVE lock has been obtained (or released)
42341** by the pager layer on the database file.
42342*/
42343SQLITE_PRIVATE int sqlite3WalExclusiveMode(Wal *pWal, int op);
42344
42345/* Return true if the argument is non-NULL and the WAL module is using
42346** heap-memory for the wal-index. Otherwise, if the argument is NULL or the
42347** WAL module is using shared-memory, return false.
42348*/
42349SQLITE_PRIVATE int sqlite3WalHeapMemory(Wal *pWal);
42350
42351#ifdef SQLITE_ENABLE_ZIPVFS
42352/* If the WAL file is not empty, return the number of bytes of content
42353** stored in each frame (i.e. the db page-size when the WAL was created).
42354*/
42355SQLITE_PRIVATE int sqlite3WalFramesize(Wal *pWal);
42356#endif
42357
42358#endif /* ifndef SQLITE_OMIT_WAL */
42359#endif /* _WAL_H_ */
42360
42361/************** End of wal.h *************************************************/
42362/************** Continuing where we left off in pager.c **********************/
42363
42364
42365/******************* NOTES ON THE DESIGN OF THE PAGER ************************
42366**
42367** This comment block describes invariants that hold when using a rollback
42368** journal.  These invariants do not apply for journal_mode=WAL,
42369** journal_mode=MEMORY, or journal_mode=OFF.
42370**
42371** Within this comment block, a page is deemed to have been synced
42372** automatically as soon as it is written when PRAGMA synchronous=OFF.
42373** Otherwise, the page is not synced until the xSync method of the VFS
42374** is called successfully on the file containing the page.
42375**
42376** Definition:  A page of the database file is said to be "overwriteable" if
42377** one or more of the following are true about the page:
42378**
42379**     (a)  The original content of the page as it was at the beginning of
42380**          the transaction has been written into the rollback journal and
42381**          synced.
42382**
42383**     (b)  The page was a freelist leaf page at the start of the transaction.
42384**
42385**     (c)  The page number is greater than the largest page that existed in
42386**          the database file at the start of the transaction.
42387**
42388** (1) A page of the database file is never overwritten unless one of the
42389**     following are true:
42390**
42391**     (a) The page and all other pages on the same sector are overwriteable.
42392**
42393**     (b) The atomic page write optimization is enabled, and the entire
42394**         transaction other than the update of the transaction sequence
42395**         number consists of a single page change.
42396**
42397** (2) The content of a page written into the rollback journal exactly matches
42398**     both the content in the database when the rollback journal was written
42399**     and the content in the database at the beginning of the current
42400**     transaction.
42401**
42402** (3) Writes to the database file are an integer multiple of the page size
42403**     in length and are aligned on a page boundary.
42404**
42405** (4) Reads from the database file are either aligned on a page boundary and
42406**     an integer multiple of the page size in length or are taken from the
42407**     first 100 bytes of the database file.
42408**
42409** (5) All writes to the database file are synced prior to the rollback journal
42410**     being deleted, truncated, or zeroed.
42411**
42412** (6) If a master journal file is used, then all writes to the database file
42413**     are synced prior to the master journal being deleted.
42414**
42415** Definition: Two databases (or the same database at two points it time)
42416** are said to be "logically equivalent" if they give the same answer to
42417** all queries.  Note in particular the content of freelist leaf
42418** pages can be changed arbitrarily without affecting the logical equivalence
42419** of the database.
42420**
42421** (7) At any time, if any subset, including the empty set and the total set,
42422**     of the unsynced changes to a rollback journal are removed and the
42423**     journal is rolled back, the resulting database file will be logically
42424**     equivalent to the database file at the beginning of the transaction.
42425**
42426** (8) When a transaction is rolled back, the xTruncate method of the VFS
42427**     is called to restore the database file to the same size it was at
42428**     the beginning of the transaction.  (In some VFSes, the xTruncate
42429**     method is a no-op, but that does not change the fact the SQLite will
42430**     invoke it.)
42431**
42432** (9) Whenever the database file is modified, at least one bit in the range
42433**     of bytes from 24 through 39 inclusive will be changed prior to releasing
42434**     the EXCLUSIVE lock, thus signaling other connections on the same
42435**     database to flush their caches.
42436**
42437** (10) The pattern of bits in bytes 24 through 39 shall not repeat in less
42438**      than one billion transactions.
42439**
42440** (11) A database file is well-formed at the beginning and at the conclusion
42441**      of every transaction.
42442**
42443** (12) An EXCLUSIVE lock is held on the database file when writing to
42444**      the database file.
42445**
42446** (13) A SHARED lock is held on the database file while reading any
42447**      content out of the database file.
42448**
42449******************************************************************************/
42450
42451/*
42452** Macros for troubleshooting.  Normally turned off
42453*/
42454#if 0
42455int sqlite3PagerTrace=1;  /* True to enable tracing */
42456#define sqlite3DebugPrintf printf
42457#define PAGERTRACE(X)     if( sqlite3PagerTrace ){ sqlite3DebugPrintf X; }
42458#else
42459#define PAGERTRACE(X)
42460#endif
42461
42462/*
42463** The following two macros are used within the PAGERTRACE() macros above
42464** to print out file-descriptors.
42465**
42466** PAGERID() takes a pointer to a Pager struct as its argument. The
42467** associated file-descriptor is returned. FILEHANDLEID() takes an sqlite3_file
42468** struct as its argument.
42469*/
42470#define PAGERID(p) ((int)(p->fd))
42471#define FILEHANDLEID(fd) ((int)fd)
42472
42473/*
42474** The Pager.eState variable stores the current 'state' of a pager. A
42475** pager may be in any one of the seven states shown in the following
42476** state diagram.
42477**
42478**                            OPEN <------+------+
42479**                              |         |      |
42480**                              V         |      |
42481**               +---------> READER-------+      |
42482**               |              |                |
42483**               |              V                |
42484**               |<-------WRITER_LOCKED------> ERROR
42485**               |              |                ^
42486**               |              V                |
42487**               |<------WRITER_CACHEMOD-------->|
42488**               |              |                |
42489**               |              V                |
42490**               |<-------WRITER_DBMOD---------->|
42491**               |              |                |
42492**               |              V                |
42493**               +<------WRITER_FINISHED-------->+
42494**
42495**
42496** List of state transitions and the C [function] that performs each:
42497**
42498**   OPEN              -> READER              [sqlite3PagerSharedLock]
42499**   READER            -> OPEN                [pager_unlock]
42500**
42501**   READER            -> WRITER_LOCKED       [sqlite3PagerBegin]
42502**   WRITER_LOCKED     -> WRITER_CACHEMOD     [pager_open_journal]
42503**   WRITER_CACHEMOD   -> WRITER_DBMOD        [syncJournal]
42504**   WRITER_DBMOD      -> WRITER_FINISHED     [sqlite3PagerCommitPhaseOne]
42505**   WRITER_***        -> READER              [pager_end_transaction]
42506**
42507**   WRITER_***        -> ERROR               [pager_error]
42508**   ERROR             -> OPEN                [pager_unlock]
42509**
42510**
42511**  OPEN:
42512**
42513**    The pager starts up in this state. Nothing is guaranteed in this
42514**    state - the file may or may not be locked and the database size is
42515**    unknown. The database may not be read or written.
42516**
42517**    * No read or write transaction is active.
42518**    * Any lock, or no lock at all, may be held on the database file.
42519**    * The dbSize, dbOrigSize and dbFileSize variables may not be trusted.
42520**
42521**  READER:
42522**
42523**    In this state all the requirements for reading the database in
42524**    rollback (non-WAL) mode are met. Unless the pager is (or recently
42525**    was) in exclusive-locking mode, a user-level read transaction is
42526**    open. The database size is known in this state.
42527**
42528**    A connection running with locking_mode=normal enters this state when
42529**    it opens a read-transaction on the database and returns to state
42530**    OPEN after the read-transaction is completed. However a connection
42531**    running in locking_mode=exclusive (including temp databases) remains in
42532**    this state even after the read-transaction is closed. The only way
42533**    a locking_mode=exclusive connection can transition from READER to OPEN
42534**    is via the ERROR state (see below).
42535**
42536**    * A read transaction may be active (but a write-transaction cannot).
42537**    * A SHARED or greater lock is held on the database file.
42538**    * The dbSize variable may be trusted (even if a user-level read
42539**      transaction is not active). The dbOrigSize and dbFileSize variables
42540**      may not be trusted at this point.
42541**    * If the database is a WAL database, then the WAL connection is open.
42542**    * Even if a read-transaction is not open, it is guaranteed that
42543**      there is no hot-journal in the file-system.
42544**
42545**  WRITER_LOCKED:
42546**
42547**    The pager moves to this state from READER when a write-transaction
42548**    is first opened on the database. In WRITER_LOCKED state, all locks
42549**    required to start a write-transaction are held, but no actual
42550**    modifications to the cache or database have taken place.
42551**
42552**    In rollback mode, a RESERVED or (if the transaction was opened with
42553**    BEGIN EXCLUSIVE) EXCLUSIVE lock is obtained on the database file when
42554**    moving to this state, but the journal file is not written to or opened
42555**    to in this state. If the transaction is committed or rolled back while
42556**    in WRITER_LOCKED state, all that is required is to unlock the database
42557**    file.
42558**
42559**    IN WAL mode, WalBeginWriteTransaction() is called to lock the log file.
42560**    If the connection is running with locking_mode=exclusive, an attempt
42561**    is made to obtain an EXCLUSIVE lock on the database file.
42562**
42563**    * A write transaction is active.
42564**    * If the connection is open in rollback-mode, a RESERVED or greater
42565**      lock is held on the database file.
42566**    * If the connection is open in WAL-mode, a WAL write transaction
42567**      is open (i.e. sqlite3WalBeginWriteTransaction() has been successfully
42568**      called).
42569**    * The dbSize, dbOrigSize and dbFileSize variables are all valid.
42570**    * The contents of the pager cache have not been modified.
42571**    * The journal file may or may not be open.
42572**    * Nothing (not even the first header) has been written to the journal.
42573**
42574**  WRITER_CACHEMOD:
42575**
42576**    A pager moves from WRITER_LOCKED state to this state when a page is
42577**    first modified by the upper layer. In rollback mode the journal file
42578**    is opened (if it is not already open) and a header written to the
42579**    start of it. The database file on disk has not been modified.
42580**
42581**    * A write transaction is active.
42582**    * A RESERVED or greater lock is held on the database file.
42583**    * The journal file is open and the first header has been written
42584**      to it, but the header has not been synced to disk.
42585**    * The contents of the page cache have been modified.
42586**
42587**  WRITER_DBMOD:
42588**
42589**    The pager transitions from WRITER_CACHEMOD into WRITER_DBMOD state
42590**    when it modifies the contents of the database file. WAL connections
42591**    never enter this state (since they do not modify the database file,
42592**    just the log file).
42593**
42594**    * A write transaction is active.
42595**    * An EXCLUSIVE or greater lock is held on the database file.
42596**    * The journal file is open and the first header has been written
42597**      and synced to disk.
42598**    * The contents of the page cache have been modified (and possibly
42599**      written to disk).
42600**
42601**  WRITER_FINISHED:
42602**
42603**    It is not possible for a WAL connection to enter this state.
42604**
42605**    A rollback-mode pager changes to WRITER_FINISHED state from WRITER_DBMOD
42606**    state after the entire transaction has been successfully written into the
42607**    database file. In this state the transaction may be committed simply
42608**    by finalizing the journal file. Once in WRITER_FINISHED state, it is
42609**    not possible to modify the database further. At this point, the upper
42610**    layer must either commit or rollback the transaction.
42611**
42612**    * A write transaction is active.
42613**    * An EXCLUSIVE or greater lock is held on the database file.
42614**    * All writing and syncing of journal and database data has finished.
42615**      If no error occurred, all that remains is to finalize the journal to
42616**      commit the transaction. If an error did occur, the caller will need
42617**      to rollback the transaction.
42618**
42619**  ERROR:
42620**
42621**    The ERROR state is entered when an IO or disk-full error (including
42622**    SQLITE_IOERR_NOMEM) occurs at a point in the code that makes it
42623**    difficult to be sure that the in-memory pager state (cache contents,
42624**    db size etc.) are consistent with the contents of the file-system.
42625**
42626**    Temporary pager files may enter the ERROR state, but in-memory pagers
42627**    cannot.
42628**
42629**    For example, if an IO error occurs while performing a rollback,
42630**    the contents of the page-cache may be left in an inconsistent state.
42631**    At this point it would be dangerous to change back to READER state
42632**    (as usually happens after a rollback). Any subsequent readers might
42633**    report database corruption (due to the inconsistent cache), and if
42634**    they upgrade to writers, they may inadvertently corrupt the database
42635**    file. To avoid this hazard, the pager switches into the ERROR state
42636**    instead of READER following such an error.
42637**
42638**    Once it has entered the ERROR state, any attempt to use the pager
42639**    to read or write data returns an error. Eventually, once all
42640**    outstanding transactions have been abandoned, the pager is able to
42641**    transition back to OPEN state, discarding the contents of the
42642**    page-cache and any other in-memory state at the same time. Everything
42643**    is reloaded from disk (and, if necessary, hot-journal rollback peformed)
42644**    when a read-transaction is next opened on the pager (transitioning
42645**    the pager into READER state). At that point the system has recovered
42646**    from the error.
42647**
42648**    Specifically, the pager jumps into the ERROR state if:
42649**
42650**      1. An error occurs while attempting a rollback. This happens in
42651**         function sqlite3PagerRollback().
42652**
42653**      2. An error occurs while attempting to finalize a journal file
42654**         following a commit in function sqlite3PagerCommitPhaseTwo().
42655**
42656**      3. An error occurs while attempting to write to the journal or
42657**         database file in function pagerStress() in order to free up
42658**         memory.
42659**
42660**    In other cases, the error is returned to the b-tree layer. The b-tree
42661**    layer then attempts a rollback operation. If the error condition
42662**    persists, the pager enters the ERROR state via condition (1) above.
42663**
42664**    Condition (3) is necessary because it can be triggered by a read-only
42665**    statement executed within a transaction. In this case, if the error
42666**    code were simply returned to the user, the b-tree layer would not
42667**    automatically attempt a rollback, as it assumes that an error in a
42668**    read-only statement cannot leave the pager in an internally inconsistent
42669**    state.
42670**
42671**    * The Pager.errCode variable is set to something other than SQLITE_OK.
42672**    * There are one or more outstanding references to pages (after the
42673**      last reference is dropped the pager should move back to OPEN state).
42674**    * The pager is not an in-memory pager.
42675**
42676**
42677** Notes:
42678**
42679**   * A pager is never in WRITER_DBMOD or WRITER_FINISHED state if the
42680**     connection is open in WAL mode. A WAL connection is always in one
42681**     of the first four states.
42682**
42683**   * Normally, a connection open in exclusive mode is never in PAGER_OPEN
42684**     state. There are two exceptions: immediately after exclusive-mode has
42685**     been turned on (and before any read or write transactions are
42686**     executed), and when the pager is leaving the "error state".
42687**
42688**   * See also: assert_pager_state().
42689*/
42690#define PAGER_OPEN                  0
42691#define PAGER_READER                1
42692#define PAGER_WRITER_LOCKED         2
42693#define PAGER_WRITER_CACHEMOD       3
42694#define PAGER_WRITER_DBMOD          4
42695#define PAGER_WRITER_FINISHED       5
42696#define PAGER_ERROR                 6
42697
42698/*
42699** The Pager.eLock variable is almost always set to one of the
42700** following locking-states, according to the lock currently held on
42701** the database file: NO_LOCK, SHARED_LOCK, RESERVED_LOCK or EXCLUSIVE_LOCK.
42702** This variable is kept up to date as locks are taken and released by
42703** the pagerLockDb() and pagerUnlockDb() wrappers.
42704**
42705** If the VFS xLock() or xUnlock() returns an error other than SQLITE_BUSY
42706** (i.e. one of the SQLITE_IOERR subtypes), it is not clear whether or not
42707** the operation was successful. In these circumstances pagerLockDb() and
42708** pagerUnlockDb() take a conservative approach - eLock is always updated
42709** when unlocking the file, and only updated when locking the file if the
42710** VFS call is successful. This way, the Pager.eLock variable may be set
42711** to a less exclusive (lower) value than the lock that is actually held
42712** at the system level, but it is never set to a more exclusive value.
42713**
42714** This is usually safe. If an xUnlock fails or appears to fail, there may
42715** be a few redundant xLock() calls or a lock may be held for longer than
42716** required, but nothing really goes wrong.
42717**
42718** The exception is when the database file is unlocked as the pager moves
42719** from ERROR to OPEN state. At this point there may be a hot-journal file
42720** in the file-system that needs to be rolled back (as part of an OPEN->SHARED
42721** transition, by the same pager or any other). If the call to xUnlock()
42722** fails at this point and the pager is left holding an EXCLUSIVE lock, this
42723** can confuse the call to xCheckReservedLock() call made later as part
42724** of hot-journal detection.
42725**
42726** xCheckReservedLock() is defined as returning true "if there is a RESERVED
42727** lock held by this process or any others". So xCheckReservedLock may
42728** return true because the caller itself is holding an EXCLUSIVE lock (but
42729** doesn't know it because of a previous error in xUnlock). If this happens
42730** a hot-journal may be mistaken for a journal being created by an active
42731** transaction in another process, causing SQLite to read from the database
42732** without rolling it back.
42733**
42734** To work around this, if a call to xUnlock() fails when unlocking the
42735** database in the ERROR state, Pager.eLock is set to UNKNOWN_LOCK. It
42736** is only changed back to a real locking state after a successful call
42737** to xLock(EXCLUSIVE). Also, the code to do the OPEN->SHARED state transition
42738** omits the check for a hot-journal if Pager.eLock is set to UNKNOWN_LOCK
42739** lock. Instead, it assumes a hot-journal exists and obtains an EXCLUSIVE
42740** lock on the database file before attempting to roll it back. See function
42741** PagerSharedLock() for more detail.
42742**
42743** Pager.eLock may only be set to UNKNOWN_LOCK when the pager is in
42744** PAGER_OPEN state.
42745*/
42746#define UNKNOWN_LOCK                (EXCLUSIVE_LOCK+1)
42747
42748/*
42749** A macro used for invoking the codec if there is one
42750*/
42751#ifdef SQLITE_HAS_CODEC
42752# define CODEC1(P,D,N,X,E) \
42753    if( P->xCodec && P->xCodec(P->pCodec,D,N,X)==0 ){ E; }
42754# define CODEC2(P,D,N,X,E,O) \
42755    if( P->xCodec==0 ){ O=(char*)D; }else \
42756    if( (O=(char*)(P->xCodec(P->pCodec,D,N,X)))==0 ){ E; }
42757#else
42758# define CODEC1(P,D,N,X,E)   /* NO-OP */
42759# define CODEC2(P,D,N,X,E,O) O=(char*)D
42760#endif
42761
42762/*
42763** The maximum allowed sector size. 64KiB. If the xSectorsize() method
42764** returns a value larger than this, then MAX_SECTOR_SIZE is used instead.
42765** This could conceivably cause corruption following a power failure on
42766** such a system. This is currently an undocumented limit.
42767*/
42768#define MAX_SECTOR_SIZE 0x10000
42769
42770/*
42771** An instance of the following structure is allocated for each active
42772** savepoint and statement transaction in the system. All such structures
42773** are stored in the Pager.aSavepoint[] array, which is allocated and
42774** resized using sqlite3Realloc().
42775**
42776** When a savepoint is created, the PagerSavepoint.iHdrOffset field is
42777** set to 0. If a journal-header is written into the main journal while
42778** the savepoint is active, then iHdrOffset is set to the byte offset
42779** immediately following the last journal record written into the main
42780** journal before the journal-header. This is required during savepoint
42781** rollback (see pagerPlaybackSavepoint()).
42782*/
42783typedef struct PagerSavepoint PagerSavepoint;
42784struct PagerSavepoint {
42785  i64 iOffset;                 /* Starting offset in main journal */
42786  i64 iHdrOffset;              /* See above */
42787  Bitvec *pInSavepoint;        /* Set of pages in this savepoint */
42788  Pgno nOrig;                  /* Original number of pages in file */
42789  Pgno iSubRec;                /* Index of first record in sub-journal */
42790#ifndef SQLITE_OMIT_WAL
42791  u32 aWalData[WAL_SAVEPOINT_NDATA];        /* WAL savepoint context */
42792#endif
42793};
42794
42795/*
42796** Bits of the Pager.doNotSpill flag.  See further description below.
42797*/
42798#define SPILLFLAG_OFF         0x01 /* Never spill cache.  Set via pragma */
42799#define SPILLFLAG_ROLLBACK    0x02 /* Current rolling back, so do not spill */
42800#define SPILLFLAG_NOSYNC      0x04 /* Spill is ok, but do not sync */
42801
42802/*
42803** An open page cache is an instance of struct Pager. A description of
42804** some of the more important member variables follows:
42805**
42806** eState
42807**
42808**   The current 'state' of the pager object. See the comment and state
42809**   diagram above for a description of the pager state.
42810**
42811** eLock
42812**
42813**   For a real on-disk database, the current lock held on the database file -
42814**   NO_LOCK, SHARED_LOCK, RESERVED_LOCK or EXCLUSIVE_LOCK.
42815**
42816**   For a temporary or in-memory database (neither of which require any
42817**   locks), this variable is always set to EXCLUSIVE_LOCK. Since such
42818**   databases always have Pager.exclusiveMode==1, this tricks the pager
42819**   logic into thinking that it already has all the locks it will ever
42820**   need (and no reason to release them).
42821**
42822**   In some (obscure) circumstances, this variable may also be set to
42823**   UNKNOWN_LOCK. See the comment above the #define of UNKNOWN_LOCK for
42824**   details.
42825**
42826** changeCountDone
42827**
42828**   This boolean variable is used to make sure that the change-counter
42829**   (the 4-byte header field at byte offset 24 of the database file) is
42830**   not updated more often than necessary.
42831**
42832**   It is set to true when the change-counter field is updated, which
42833**   can only happen if an exclusive lock is held on the database file.
42834**   It is cleared (set to false) whenever an exclusive lock is
42835**   relinquished on the database file. Each time a transaction is committed,
42836**   The changeCountDone flag is inspected. If it is true, the work of
42837**   updating the change-counter is omitted for the current transaction.
42838**
42839**   This mechanism means that when running in exclusive mode, a connection
42840**   need only update the change-counter once, for the first transaction
42841**   committed.
42842**
42843** setMaster
42844**
42845**   When PagerCommitPhaseOne() is called to commit a transaction, it may
42846**   (or may not) specify a master-journal name to be written into the
42847**   journal file before it is synced to disk.
42848**
42849**   Whether or not a journal file contains a master-journal pointer affects
42850**   the way in which the journal file is finalized after the transaction is
42851**   committed or rolled back when running in "journal_mode=PERSIST" mode.
42852**   If a journal file does not contain a master-journal pointer, it is
42853**   finalized by overwriting the first journal header with zeroes. If
42854**   it does contain a master-journal pointer the journal file is finalized
42855**   by truncating it to zero bytes, just as if the connection were
42856**   running in "journal_mode=truncate" mode.
42857**
42858**   Journal files that contain master journal pointers cannot be finalized
42859**   simply by overwriting the first journal-header with zeroes, as the
42860**   master journal pointer could interfere with hot-journal rollback of any
42861**   subsequently interrupted transaction that reuses the journal file.
42862**
42863**   The flag is cleared as soon as the journal file is finalized (either
42864**   by PagerCommitPhaseTwo or PagerRollback). If an IO error prevents the
42865**   journal file from being successfully finalized, the setMaster flag
42866**   is cleared anyway (and the pager will move to ERROR state).
42867**
42868** doNotSpill
42869**
42870**   This variables control the behavior of cache-spills  (calls made by
42871**   the pcache module to the pagerStress() routine to write cached data
42872**   to the file-system in order to free up memory).
42873**
42874**   When bits SPILLFLAG_OFF or SPILLFLAG_ROLLBACK of doNotSpill are set,
42875**   writing to the database from pagerStress() is disabled altogether.
42876**   The SPILLFLAG_ROLLBACK case is done in a very obscure case that
42877**   comes up during savepoint rollback that requires the pcache module
42878**   to allocate a new page to prevent the journal file from being written
42879**   while it is being traversed by code in pager_playback().  The SPILLFLAG_OFF
42880**   case is a user preference.
42881**
42882**   If the SPILLFLAG_NOSYNC bit is set, writing to the database from
42883**   pagerStress() is permitted, but syncing the journal file is not.
42884**   This flag is set by sqlite3PagerWrite() when the file-system sector-size
42885**   is larger than the database page-size in order to prevent a journal sync
42886**   from happening in between the journalling of two pages on the same sector.
42887**
42888** subjInMemory
42889**
42890**   This is a boolean variable. If true, then any required sub-journal
42891**   is opened as an in-memory journal file. If false, then in-memory
42892**   sub-journals are only used for in-memory pager files.
42893**
42894**   This variable is updated by the upper layer each time a new
42895**   write-transaction is opened.
42896**
42897** dbSize, dbOrigSize, dbFileSize
42898**
42899**   Variable dbSize is set to the number of pages in the database file.
42900**   It is valid in PAGER_READER and higher states (all states except for
42901**   OPEN and ERROR).
42902**
42903**   dbSize is set based on the size of the database file, which may be
42904**   larger than the size of the database (the value stored at offset
42905**   28 of the database header by the btree). If the size of the file
42906**   is not an integer multiple of the page-size, the value stored in
42907**   dbSize is rounded down (i.e. a 5KB file with 2K page-size has dbSize==2).
42908**   Except, any file that is greater than 0 bytes in size is considered
42909**   to have at least one page. (i.e. a 1KB file with 2K page-size leads
42910**   to dbSize==1).
42911**
42912**   During a write-transaction, if pages with page-numbers greater than
42913**   dbSize are modified in the cache, dbSize is updated accordingly.
42914**   Similarly, if the database is truncated using PagerTruncateImage(),
42915**   dbSize is updated.
42916**
42917**   Variables dbOrigSize and dbFileSize are valid in states
42918**   PAGER_WRITER_LOCKED and higher. dbOrigSize is a copy of the dbSize
42919**   variable at the start of the transaction. It is used during rollback,
42920**   and to determine whether or not pages need to be journalled before
42921**   being modified.
42922**
42923**   Throughout a write-transaction, dbFileSize contains the size of
42924**   the file on disk in pages. It is set to a copy of dbSize when the
42925**   write-transaction is first opened, and updated when VFS calls are made
42926**   to write or truncate the database file on disk.
42927**
42928**   The only reason the dbFileSize variable is required is to suppress
42929**   unnecessary calls to xTruncate() after committing a transaction. If,
42930**   when a transaction is committed, the dbFileSize variable indicates
42931**   that the database file is larger than the database image (Pager.dbSize),
42932**   pager_truncate() is called. The pager_truncate() call uses xFilesize()
42933**   to measure the database file on disk, and then truncates it if required.
42934**   dbFileSize is not used when rolling back a transaction. In this case
42935**   pager_truncate() is called unconditionally (which means there may be
42936**   a call to xFilesize() that is not strictly required). In either case,
42937**   pager_truncate() may cause the file to become smaller or larger.
42938**
42939** dbHintSize
42940**
42941**   The dbHintSize variable is used to limit the number of calls made to
42942**   the VFS xFileControl(FCNTL_SIZE_HINT) method.
42943**
42944**   dbHintSize is set to a copy of the dbSize variable when a
42945**   write-transaction is opened (at the same time as dbFileSize and
42946**   dbOrigSize). If the xFileControl(FCNTL_SIZE_HINT) method is called,
42947**   dbHintSize is increased to the number of pages that correspond to the
42948**   size-hint passed to the method call. See pager_write_pagelist() for
42949**   details.
42950**
42951** errCode
42952**
42953**   The Pager.errCode variable is only ever used in PAGER_ERROR state. It
42954**   is set to zero in all other states. In PAGER_ERROR state, Pager.errCode
42955**   is always set to SQLITE_FULL, SQLITE_IOERR or one of the SQLITE_IOERR_XXX
42956**   sub-codes.
42957*/
42958struct Pager {
42959  sqlite3_vfs *pVfs;          /* OS functions to use for IO */
42960  u8 exclusiveMode;           /* Boolean. True if locking_mode==EXCLUSIVE */
42961  u8 journalMode;             /* One of the PAGER_JOURNALMODE_* values */
42962  u8 useJournal;              /* Use a rollback journal on this file */
42963  u8 noSync;                  /* Do not sync the journal if true */
42964  u8 fullSync;                /* Do extra syncs of the journal for robustness */
42965  u8 ckptSyncFlags;           /* SYNC_NORMAL or SYNC_FULL for checkpoint */
42966  u8 walSyncFlags;            /* SYNC_NORMAL or SYNC_FULL for wal writes */
42967  u8 syncFlags;               /* SYNC_NORMAL or SYNC_FULL otherwise */
42968  u8 tempFile;                /* zFilename is a temporary or immutable file */
42969  u8 noLock;                  /* Do not lock (except in WAL mode) */
42970  u8 readOnly;                /* True for a read-only database */
42971  u8 memDb;                   /* True to inhibit all file I/O */
42972
42973  /**************************************************************************
42974  ** The following block contains those class members that change during
42975  ** routine operation.  Class members not in this block are either fixed
42976  ** when the pager is first created or else only change when there is a
42977  ** significant mode change (such as changing the page_size, locking_mode,
42978  ** or the journal_mode).  From another view, these class members describe
42979  ** the "state" of the pager, while other class members describe the
42980  ** "configuration" of the pager.
42981  */
42982  u8 eState;                  /* Pager state (OPEN, READER, WRITER_LOCKED..) */
42983  u8 eLock;                   /* Current lock held on database file */
42984  u8 changeCountDone;         /* Set after incrementing the change-counter */
42985  u8 setMaster;               /* True if a m-j name has been written to jrnl */
42986  u8 doNotSpill;              /* Do not spill the cache when non-zero */
42987  u8 subjInMemory;            /* True to use in-memory sub-journals */
42988  u8 bUseFetch;               /* True to use xFetch() */
42989  u8 hasBeenUsed;             /* True if any content previously read */
42990  Pgno dbSize;                /* Number of pages in the database */
42991  Pgno dbOrigSize;            /* dbSize before the current transaction */
42992  Pgno dbFileSize;            /* Number of pages in the database file */
42993  Pgno dbHintSize;            /* Value passed to FCNTL_SIZE_HINT call */
42994  int errCode;                /* One of several kinds of errors */
42995  int nRec;                   /* Pages journalled since last j-header written */
42996  u32 cksumInit;              /* Quasi-random value added to every checksum */
42997  u32 nSubRec;                /* Number of records written to sub-journal */
42998  Bitvec *pInJournal;         /* One bit for each page in the database file */
42999  sqlite3_file *fd;           /* File descriptor for database */
43000  sqlite3_file *jfd;          /* File descriptor for main journal */
43001  sqlite3_file *sjfd;         /* File descriptor for sub-journal */
43002  i64 journalOff;             /* Current write offset in the journal file */
43003  i64 journalHdr;             /* Byte offset to previous journal header */
43004  sqlite3_backup *pBackup;    /* Pointer to list of ongoing backup processes */
43005  PagerSavepoint *aSavepoint; /* Array of active savepoints */
43006  int nSavepoint;             /* Number of elements in aSavepoint[] */
43007  u32 iDataVersion;           /* Changes whenever database content changes */
43008  char dbFileVers[16];        /* Changes whenever database file changes */
43009
43010  int nMmapOut;               /* Number of mmap pages currently outstanding */
43011  sqlite3_int64 szMmap;       /* Desired maximum mmap size */
43012  PgHdr *pMmapFreelist;       /* List of free mmap page headers (pDirty) */
43013  /*
43014  ** End of the routinely-changing class members
43015  ***************************************************************************/
43016
43017  u16 nExtra;                 /* Add this many bytes to each in-memory page */
43018  i16 nReserve;               /* Number of unused bytes at end of each page */
43019  u32 vfsFlags;               /* Flags for sqlite3_vfs.xOpen() */
43020  u32 sectorSize;             /* Assumed sector size during rollback */
43021  int pageSize;               /* Number of bytes in a page */
43022  Pgno mxPgno;                /* Maximum allowed size of the database */
43023  i64 journalSizeLimit;       /* Size limit for persistent journal files */
43024  char *zFilename;            /* Name of the database file */
43025  char *zJournal;             /* Name of the journal file */
43026  int (*xBusyHandler)(void*); /* Function to call when busy */
43027  void *pBusyHandlerArg;      /* Context argument for xBusyHandler */
43028  int aStat[3];               /* Total cache hits, misses and writes */
43029#ifdef SQLITE_TEST
43030  int nRead;                  /* Database pages read */
43031#endif
43032  void (*xReiniter)(DbPage*); /* Call this routine when reloading pages */
43033#ifdef SQLITE_HAS_CODEC
43034  void *(*xCodec)(void*,void*,Pgno,int); /* Routine for en/decoding data */
43035  void (*xCodecSizeChng)(void*,int,int); /* Notify of page size changes */
43036  void (*xCodecFree)(void*);             /* Destructor for the codec */
43037  void *pCodec;               /* First argument to xCodec... methods */
43038#endif
43039  char *pTmpSpace;            /* Pager.pageSize bytes of space for tmp use */
43040  PCache *pPCache;            /* Pointer to page cache object */
43041#ifndef SQLITE_OMIT_WAL
43042  Wal *pWal;                  /* Write-ahead log used by "journal_mode=wal" */
43043  char *zWal;                 /* File name for write-ahead log */
43044#endif
43045};
43046
43047/*
43048** Indexes for use with Pager.aStat[]. The Pager.aStat[] array contains
43049** the values accessed by passing SQLITE_DBSTATUS_CACHE_HIT, CACHE_MISS
43050** or CACHE_WRITE to sqlite3_db_status().
43051*/
43052#define PAGER_STAT_HIT   0
43053#define PAGER_STAT_MISS  1
43054#define PAGER_STAT_WRITE 2
43055
43056/*
43057** The following global variables hold counters used for
43058** testing purposes only.  These variables do not exist in
43059** a non-testing build.  These variables are not thread-safe.
43060*/
43061#ifdef SQLITE_TEST
43062SQLITE_API int sqlite3_pager_readdb_count = 0;    /* Number of full pages read from DB */
43063SQLITE_API int sqlite3_pager_writedb_count = 0;   /* Number of full pages written to DB */
43064SQLITE_API int sqlite3_pager_writej_count = 0;    /* Number of pages written to journal */
43065# define PAGER_INCR(v)  v++
43066#else
43067# define PAGER_INCR(v)
43068#endif
43069
43070
43071
43072/*
43073** Journal files begin with the following magic string.  The data
43074** was obtained from /dev/random.  It is used only as a sanity check.
43075**
43076** Since version 2.8.0, the journal format contains additional sanity
43077** checking information.  If the power fails while the journal is being
43078** written, semi-random garbage data might appear in the journal
43079** file after power is restored.  If an attempt is then made
43080** to roll the journal back, the database could be corrupted.  The additional
43081** sanity checking data is an attempt to discover the garbage in the
43082** journal and ignore it.
43083**
43084** The sanity checking information for the new journal format consists
43085** of a 32-bit checksum on each page of data.  The checksum covers both
43086** the page number and the pPager->pageSize bytes of data for the page.
43087** This cksum is initialized to a 32-bit random value that appears in the
43088** journal file right after the header.  The random initializer is important,
43089** because garbage data that appears at the end of a journal is likely
43090** data that was once in other files that have now been deleted.  If the
43091** garbage data came from an obsolete journal file, the checksums might
43092** be correct.  But by initializing the checksum to random value which
43093** is different for every journal, we minimize that risk.
43094*/
43095static const unsigned char aJournalMagic[] = {
43096  0xd9, 0xd5, 0x05, 0xf9, 0x20, 0xa1, 0x63, 0xd7,
43097};
43098
43099/*
43100** The size of the of each page record in the journal is given by
43101** the following macro.
43102*/
43103#define JOURNAL_PG_SZ(pPager)  ((pPager->pageSize) + 8)
43104
43105/*
43106** The journal header size for this pager. This is usually the same
43107** size as a single disk sector. See also setSectorSize().
43108*/
43109#define JOURNAL_HDR_SZ(pPager) (pPager->sectorSize)
43110
43111/*
43112** The macro MEMDB is true if we are dealing with an in-memory database.
43113** We do this as a macro so that if the SQLITE_OMIT_MEMORYDB macro is set,
43114** the value of MEMDB will be a constant and the compiler will optimize
43115** out code that would never execute.
43116*/
43117#ifdef SQLITE_OMIT_MEMORYDB
43118# define MEMDB 0
43119#else
43120# define MEMDB pPager->memDb
43121#endif
43122
43123/*
43124** The macro USEFETCH is true if we are allowed to use the xFetch and xUnfetch
43125** interfaces to access the database using memory-mapped I/O.
43126*/
43127#if SQLITE_MAX_MMAP_SIZE>0
43128# define USEFETCH(x) ((x)->bUseFetch)
43129#else
43130# define USEFETCH(x) 0
43131#endif
43132
43133/*
43134** The maximum legal page number is (2^31 - 1).
43135*/
43136#define PAGER_MAX_PGNO 2147483647
43137
43138/*
43139** The argument to this macro is a file descriptor (type sqlite3_file*).
43140** Return 0 if it is not open, or non-zero (but not 1) if it is.
43141**
43142** This is so that expressions can be written as:
43143**
43144**   if( isOpen(pPager->jfd) ){ ...
43145**
43146** instead of
43147**
43148**   if( pPager->jfd->pMethods ){ ...
43149*/
43150#define isOpen(pFd) ((pFd)->pMethods!=0)
43151
43152/*
43153** Return true if this pager uses a write-ahead log instead of the usual
43154** rollback journal. Otherwise false.
43155*/
43156#ifndef SQLITE_OMIT_WAL
43157static int pagerUseWal(Pager *pPager){
43158  return (pPager->pWal!=0);
43159}
43160#else
43161# define pagerUseWal(x) 0
43162# define pagerRollbackWal(x) 0
43163# define pagerWalFrames(v,w,x,y) 0
43164# define pagerOpenWalIfPresent(z) SQLITE_OK
43165# define pagerBeginReadTransaction(z) SQLITE_OK
43166#endif
43167
43168#ifndef NDEBUG
43169/*
43170** Usage:
43171**
43172**   assert( assert_pager_state(pPager) );
43173**
43174** This function runs many asserts to try to find inconsistencies in
43175** the internal state of the Pager object.
43176*/
43177static int assert_pager_state(Pager *p){
43178  Pager *pPager = p;
43179
43180  /* State must be valid. */
43181  assert( p->eState==PAGER_OPEN
43182       || p->eState==PAGER_READER
43183       || p->eState==PAGER_WRITER_LOCKED
43184       || p->eState==PAGER_WRITER_CACHEMOD
43185       || p->eState==PAGER_WRITER_DBMOD
43186       || p->eState==PAGER_WRITER_FINISHED
43187       || p->eState==PAGER_ERROR
43188  );
43189
43190  /* Regardless of the current state, a temp-file connection always behaves
43191  ** as if it has an exclusive lock on the database file. It never updates
43192  ** the change-counter field, so the changeCountDone flag is always set.
43193  */
43194  assert( p->tempFile==0 || p->eLock==EXCLUSIVE_LOCK );
43195  assert( p->tempFile==0 || pPager->changeCountDone );
43196
43197  /* If the useJournal flag is clear, the journal-mode must be "OFF".
43198  ** And if the journal-mode is "OFF", the journal file must not be open.
43199  */
43200  assert( p->journalMode==PAGER_JOURNALMODE_OFF || p->useJournal );
43201  assert( p->journalMode!=PAGER_JOURNALMODE_OFF || !isOpen(p->jfd) );
43202
43203  /* Check that MEMDB implies noSync. And an in-memory journal. Since
43204  ** this means an in-memory pager performs no IO at all, it cannot encounter
43205  ** either SQLITE_IOERR or SQLITE_FULL during rollback or while finalizing
43206  ** a journal file. (although the in-memory journal implementation may
43207  ** return SQLITE_IOERR_NOMEM while the journal file is being written). It
43208  ** is therefore not possible for an in-memory pager to enter the ERROR
43209  ** state.
43210  */
43211  if( MEMDB ){
43212    assert( p->noSync );
43213    assert( p->journalMode==PAGER_JOURNALMODE_OFF
43214         || p->journalMode==PAGER_JOURNALMODE_MEMORY
43215    );
43216    assert( p->eState!=PAGER_ERROR && p->eState!=PAGER_OPEN );
43217    assert( pagerUseWal(p)==0 );
43218  }
43219
43220  /* If changeCountDone is set, a RESERVED lock or greater must be held
43221  ** on the file.
43222  */
43223  assert( pPager->changeCountDone==0 || pPager->eLock>=RESERVED_LOCK );
43224  assert( p->eLock!=PENDING_LOCK );
43225
43226  switch( p->eState ){
43227    case PAGER_OPEN:
43228      assert( !MEMDB );
43229      assert( pPager->errCode==SQLITE_OK );
43230      assert( sqlite3PcacheRefCount(pPager->pPCache)==0 || pPager->tempFile );
43231      break;
43232
43233    case PAGER_READER:
43234      assert( pPager->errCode==SQLITE_OK );
43235      assert( p->eLock!=UNKNOWN_LOCK );
43236      assert( p->eLock>=SHARED_LOCK );
43237      break;
43238
43239    case PAGER_WRITER_LOCKED:
43240      assert( p->eLock!=UNKNOWN_LOCK );
43241      assert( pPager->errCode==SQLITE_OK );
43242      if( !pagerUseWal(pPager) ){
43243        assert( p->eLock>=RESERVED_LOCK );
43244      }
43245      assert( pPager->dbSize==pPager->dbOrigSize );
43246      assert( pPager->dbOrigSize==pPager->dbFileSize );
43247      assert( pPager->dbOrigSize==pPager->dbHintSize );
43248      assert( pPager->setMaster==0 );
43249      break;
43250
43251    case PAGER_WRITER_CACHEMOD:
43252      assert( p->eLock!=UNKNOWN_LOCK );
43253      assert( pPager->errCode==SQLITE_OK );
43254      if( !pagerUseWal(pPager) ){
43255        /* It is possible that if journal_mode=wal here that neither the
43256        ** journal file nor the WAL file are open. This happens during
43257        ** a rollback transaction that switches from journal_mode=off
43258        ** to journal_mode=wal.
43259        */
43260        assert( p->eLock>=RESERVED_LOCK );
43261        assert( isOpen(p->jfd)
43262             || p->journalMode==PAGER_JOURNALMODE_OFF
43263             || p->journalMode==PAGER_JOURNALMODE_WAL
43264        );
43265      }
43266      assert( pPager->dbOrigSize==pPager->dbFileSize );
43267      assert( pPager->dbOrigSize==pPager->dbHintSize );
43268      break;
43269
43270    case PAGER_WRITER_DBMOD:
43271      assert( p->eLock==EXCLUSIVE_LOCK );
43272      assert( pPager->errCode==SQLITE_OK );
43273      assert( !pagerUseWal(pPager) );
43274      assert( p->eLock>=EXCLUSIVE_LOCK );
43275      assert( isOpen(p->jfd)
43276           || p->journalMode==PAGER_JOURNALMODE_OFF
43277           || p->journalMode==PAGER_JOURNALMODE_WAL
43278      );
43279      assert( pPager->dbOrigSize<=pPager->dbHintSize );
43280      break;
43281
43282    case PAGER_WRITER_FINISHED:
43283      assert( p->eLock==EXCLUSIVE_LOCK );
43284      assert( pPager->errCode==SQLITE_OK );
43285      assert( !pagerUseWal(pPager) );
43286      assert( isOpen(p->jfd)
43287           || p->journalMode==PAGER_JOURNALMODE_OFF
43288           || p->journalMode==PAGER_JOURNALMODE_WAL
43289      );
43290      break;
43291
43292    case PAGER_ERROR:
43293      /* There must be at least one outstanding reference to the pager if
43294      ** in ERROR state. Otherwise the pager should have already dropped
43295      ** back to OPEN state.
43296      */
43297      assert( pPager->errCode!=SQLITE_OK );
43298      assert( sqlite3PcacheRefCount(pPager->pPCache)>0 );
43299      break;
43300  }
43301
43302  return 1;
43303}
43304#endif /* ifndef NDEBUG */
43305
43306#ifdef SQLITE_DEBUG
43307/*
43308** Return a pointer to a human readable string in a static buffer
43309** containing the state of the Pager object passed as an argument. This
43310** is intended to be used within debuggers. For example, as an alternative
43311** to "print *pPager" in gdb:
43312**
43313** (gdb) printf "%s", print_pager_state(pPager)
43314*/
43315static char *print_pager_state(Pager *p){
43316  static char zRet[1024];
43317
43318  sqlite3_snprintf(1024, zRet,
43319      "Filename:      %s\n"
43320      "State:         %s errCode=%d\n"
43321      "Lock:          %s\n"
43322      "Locking mode:  locking_mode=%s\n"
43323      "Journal mode:  journal_mode=%s\n"
43324      "Backing store: tempFile=%d memDb=%d useJournal=%d\n"
43325      "Journal:       journalOff=%lld journalHdr=%lld\n"
43326      "Size:          dbsize=%d dbOrigSize=%d dbFileSize=%d\n"
43327      , p->zFilename
43328      , p->eState==PAGER_OPEN            ? "OPEN" :
43329        p->eState==PAGER_READER          ? "READER" :
43330        p->eState==PAGER_WRITER_LOCKED   ? "WRITER_LOCKED" :
43331        p->eState==PAGER_WRITER_CACHEMOD ? "WRITER_CACHEMOD" :
43332        p->eState==PAGER_WRITER_DBMOD    ? "WRITER_DBMOD" :
43333        p->eState==PAGER_WRITER_FINISHED ? "WRITER_FINISHED" :
43334        p->eState==PAGER_ERROR           ? "ERROR" : "?error?"
43335      , (int)p->errCode
43336      , p->eLock==NO_LOCK         ? "NO_LOCK" :
43337        p->eLock==RESERVED_LOCK   ? "RESERVED" :
43338        p->eLock==EXCLUSIVE_LOCK  ? "EXCLUSIVE" :
43339        p->eLock==SHARED_LOCK     ? "SHARED" :
43340        p->eLock==UNKNOWN_LOCK    ? "UNKNOWN" : "?error?"
43341      , p->exclusiveMode ? "exclusive" : "normal"
43342      , p->journalMode==PAGER_JOURNALMODE_MEMORY   ? "memory" :
43343        p->journalMode==PAGER_JOURNALMODE_OFF      ? "off" :
43344        p->journalMode==PAGER_JOURNALMODE_DELETE   ? "delete" :
43345        p->journalMode==PAGER_JOURNALMODE_PERSIST  ? "persist" :
43346        p->journalMode==PAGER_JOURNALMODE_TRUNCATE ? "truncate" :
43347        p->journalMode==PAGER_JOURNALMODE_WAL      ? "wal" : "?error?"
43348      , (int)p->tempFile, (int)p->memDb, (int)p->useJournal
43349      , p->journalOff, p->journalHdr
43350      , (int)p->dbSize, (int)p->dbOrigSize, (int)p->dbFileSize
43351  );
43352
43353  return zRet;
43354}
43355#endif
43356
43357/*
43358** Return true if it is necessary to write page *pPg into the sub-journal.
43359** A page needs to be written into the sub-journal if there exists one
43360** or more open savepoints for which:
43361**
43362**   * The page-number is less than or equal to PagerSavepoint.nOrig, and
43363**   * The bit corresponding to the page-number is not set in
43364**     PagerSavepoint.pInSavepoint.
43365*/
43366static int subjRequiresPage(PgHdr *pPg){
43367  Pager *pPager = pPg->pPager;
43368  PagerSavepoint *p;
43369  Pgno pgno = pPg->pgno;
43370  int i;
43371  for(i=0; i<pPager->nSavepoint; i++){
43372    p = &pPager->aSavepoint[i];
43373    if( p->nOrig>=pgno && 0==sqlite3BitvecTestNotNull(p->pInSavepoint, pgno) ){
43374      return 1;
43375    }
43376  }
43377  return 0;
43378}
43379
43380#ifdef SQLITE_DEBUG
43381/*
43382** Return true if the page is already in the journal file.
43383*/
43384static int pageInJournal(Pager *pPager, PgHdr *pPg){
43385  return sqlite3BitvecTest(pPager->pInJournal, pPg->pgno);
43386}
43387#endif
43388
43389/*
43390** Read a 32-bit integer from the given file descriptor.  Store the integer
43391** that is read in *pRes.  Return SQLITE_OK if everything worked, or an
43392** error code is something goes wrong.
43393**
43394** All values are stored on disk as big-endian.
43395*/
43396static int read32bits(sqlite3_file *fd, i64 offset, u32 *pRes){
43397  unsigned char ac[4];
43398  int rc = sqlite3OsRead(fd, ac, sizeof(ac), offset);
43399  if( rc==SQLITE_OK ){
43400    *pRes = sqlite3Get4byte(ac);
43401  }
43402  return rc;
43403}
43404
43405/*
43406** Write a 32-bit integer into a string buffer in big-endian byte order.
43407*/
43408#define put32bits(A,B)  sqlite3Put4byte((u8*)A,B)
43409
43410
43411/*
43412** Write a 32-bit integer into the given file descriptor.  Return SQLITE_OK
43413** on success or an error code is something goes wrong.
43414*/
43415static int write32bits(sqlite3_file *fd, i64 offset, u32 val){
43416  char ac[4];
43417  put32bits(ac, val);
43418  return sqlite3OsWrite(fd, ac, 4, offset);
43419}
43420
43421/*
43422** Unlock the database file to level eLock, which must be either NO_LOCK
43423** or SHARED_LOCK. Regardless of whether or not the call to xUnlock()
43424** succeeds, set the Pager.eLock variable to match the (attempted) new lock.
43425**
43426** Except, if Pager.eLock is set to UNKNOWN_LOCK when this function is
43427** called, do not modify it. See the comment above the #define of
43428** UNKNOWN_LOCK for an explanation of this.
43429*/
43430static int pagerUnlockDb(Pager *pPager, int eLock){
43431  int rc = SQLITE_OK;
43432
43433  assert( !pPager->exclusiveMode || pPager->eLock==eLock );
43434  assert( eLock==NO_LOCK || eLock==SHARED_LOCK );
43435  assert( eLock!=NO_LOCK || pagerUseWal(pPager)==0 );
43436  if( isOpen(pPager->fd) ){
43437    assert( pPager->eLock>=eLock );
43438    rc = pPager->noLock ? SQLITE_OK : sqlite3OsUnlock(pPager->fd, eLock);
43439    if( pPager->eLock!=UNKNOWN_LOCK ){
43440      pPager->eLock = (u8)eLock;
43441    }
43442    IOTRACE(("UNLOCK %p %d\n", pPager, eLock))
43443  }
43444  return rc;
43445}
43446
43447/*
43448** Lock the database file to level eLock, which must be either SHARED_LOCK,
43449** RESERVED_LOCK or EXCLUSIVE_LOCK. If the caller is successful, set the
43450** Pager.eLock variable to the new locking state.
43451**
43452** Except, if Pager.eLock is set to UNKNOWN_LOCK when this function is
43453** called, do not modify it unless the new locking state is EXCLUSIVE_LOCK.
43454** See the comment above the #define of UNKNOWN_LOCK for an explanation
43455** of this.
43456*/
43457static int pagerLockDb(Pager *pPager, int eLock){
43458  int rc = SQLITE_OK;
43459
43460  assert( eLock==SHARED_LOCK || eLock==RESERVED_LOCK || eLock==EXCLUSIVE_LOCK );
43461  if( pPager->eLock<eLock || pPager->eLock==UNKNOWN_LOCK ){
43462    rc = pPager->noLock ? SQLITE_OK : sqlite3OsLock(pPager->fd, eLock);
43463    if( rc==SQLITE_OK && (pPager->eLock!=UNKNOWN_LOCK||eLock==EXCLUSIVE_LOCK) ){
43464      pPager->eLock = (u8)eLock;
43465      IOTRACE(("LOCK %p %d\n", pPager, eLock))
43466    }
43467  }
43468  return rc;
43469}
43470
43471/*
43472** This function determines whether or not the atomic-write optimization
43473** can be used with this pager. The optimization can be used if:
43474**
43475**  (a) the value returned by OsDeviceCharacteristics() indicates that
43476**      a database page may be written atomically, and
43477**  (b) the value returned by OsSectorSize() is less than or equal
43478**      to the page size.
43479**
43480** The optimization is also always enabled for temporary files. It is
43481** an error to call this function if pPager is opened on an in-memory
43482** database.
43483**
43484** If the optimization cannot be used, 0 is returned. If it can be used,
43485** then the value returned is the size of the journal file when it
43486** contains rollback data for exactly one page.
43487*/
43488#ifdef SQLITE_ENABLE_ATOMIC_WRITE
43489static int jrnlBufferSize(Pager *pPager){
43490  assert( !MEMDB );
43491  if( !pPager->tempFile ){
43492    int dc;                           /* Device characteristics */
43493    int nSector;                      /* Sector size */
43494    int szPage;                       /* Page size */
43495
43496    assert( isOpen(pPager->fd) );
43497    dc = sqlite3OsDeviceCharacteristics(pPager->fd);
43498    nSector = pPager->sectorSize;
43499    szPage = pPager->pageSize;
43500
43501    assert(SQLITE_IOCAP_ATOMIC512==(512>>8));
43502    assert(SQLITE_IOCAP_ATOMIC64K==(65536>>8));
43503    if( 0==(dc&(SQLITE_IOCAP_ATOMIC|(szPage>>8)) || nSector>szPage) ){
43504      return 0;
43505    }
43506  }
43507
43508  return JOURNAL_HDR_SZ(pPager) + JOURNAL_PG_SZ(pPager);
43509}
43510#endif
43511
43512/*
43513** If SQLITE_CHECK_PAGES is defined then we do some sanity checking
43514** on the cache using a hash function.  This is used for testing
43515** and debugging only.
43516*/
43517#ifdef SQLITE_CHECK_PAGES
43518/*
43519** Return a 32-bit hash of the page data for pPage.
43520*/
43521static u32 pager_datahash(int nByte, unsigned char *pData){
43522  u32 hash = 0;
43523  int i;
43524  for(i=0; i<nByte; i++){
43525    hash = (hash*1039) + pData[i];
43526  }
43527  return hash;
43528}
43529static u32 pager_pagehash(PgHdr *pPage){
43530  return pager_datahash(pPage->pPager->pageSize, (unsigned char *)pPage->pData);
43531}
43532static void pager_set_pagehash(PgHdr *pPage){
43533  pPage->pageHash = pager_pagehash(pPage);
43534}
43535
43536/*
43537** The CHECK_PAGE macro takes a PgHdr* as an argument. If SQLITE_CHECK_PAGES
43538** is defined, and NDEBUG is not defined, an assert() statement checks
43539** that the page is either dirty or still matches the calculated page-hash.
43540*/
43541#define CHECK_PAGE(x) checkPage(x)
43542static void checkPage(PgHdr *pPg){
43543  Pager *pPager = pPg->pPager;
43544  assert( pPager->eState!=PAGER_ERROR );
43545  assert( (pPg->flags&PGHDR_DIRTY) || pPg->pageHash==pager_pagehash(pPg) );
43546}
43547
43548#else
43549#define pager_datahash(X,Y)  0
43550#define pager_pagehash(X)  0
43551#define pager_set_pagehash(X)
43552#define CHECK_PAGE(x)
43553#endif  /* SQLITE_CHECK_PAGES */
43554
43555/*
43556** When this is called the journal file for pager pPager must be open.
43557** This function attempts to read a master journal file name from the
43558** end of the file and, if successful, copies it into memory supplied
43559** by the caller. See comments above writeMasterJournal() for the format
43560** used to store a master journal file name at the end of a journal file.
43561**
43562** zMaster must point to a buffer of at least nMaster bytes allocated by
43563** the caller. This should be sqlite3_vfs.mxPathname+1 (to ensure there is
43564** enough space to write the master journal name). If the master journal
43565** name in the journal is longer than nMaster bytes (including a
43566** nul-terminator), then this is handled as if no master journal name
43567** were present in the journal.
43568**
43569** If a master journal file name is present at the end of the journal
43570** file, then it is copied into the buffer pointed to by zMaster. A
43571** nul-terminator byte is appended to the buffer following the master
43572** journal file name.
43573**
43574** If it is determined that no master journal file name is present
43575** zMaster[0] is set to 0 and SQLITE_OK returned.
43576**
43577** If an error occurs while reading from the journal file, an SQLite
43578** error code is returned.
43579*/
43580static int readMasterJournal(sqlite3_file *pJrnl, char *zMaster, u32 nMaster){
43581  int rc;                    /* Return code */
43582  u32 len;                   /* Length in bytes of master journal name */
43583  i64 szJ;                   /* Total size in bytes of journal file pJrnl */
43584  u32 cksum;                 /* MJ checksum value read from journal */
43585  u32 u;                     /* Unsigned loop counter */
43586  unsigned char aMagic[8];   /* A buffer to hold the magic header */
43587  zMaster[0] = '\0';
43588
43589  if( SQLITE_OK!=(rc = sqlite3OsFileSize(pJrnl, &szJ))
43590   || szJ<16
43591   || SQLITE_OK!=(rc = read32bits(pJrnl, szJ-16, &len))
43592   || len>=nMaster
43593   || len==0
43594   || SQLITE_OK!=(rc = read32bits(pJrnl, szJ-12, &cksum))
43595   || SQLITE_OK!=(rc = sqlite3OsRead(pJrnl, aMagic, 8, szJ-8))
43596   || memcmp(aMagic, aJournalMagic, 8)
43597   || SQLITE_OK!=(rc = sqlite3OsRead(pJrnl, zMaster, len, szJ-16-len))
43598  ){
43599    return rc;
43600  }
43601
43602  /* See if the checksum matches the master journal name */
43603  for(u=0; u<len; u++){
43604    cksum -= zMaster[u];
43605  }
43606  if( cksum ){
43607    /* If the checksum doesn't add up, then one or more of the disk sectors
43608    ** containing the master journal filename is corrupted. This means
43609    ** definitely roll back, so just return SQLITE_OK and report a (nul)
43610    ** master-journal filename.
43611    */
43612    len = 0;
43613  }
43614  zMaster[len] = '\0';
43615
43616  return SQLITE_OK;
43617}
43618
43619/*
43620** Return the offset of the sector boundary at or immediately
43621** following the value in pPager->journalOff, assuming a sector
43622** size of pPager->sectorSize bytes.
43623**
43624** i.e for a sector size of 512:
43625**
43626**   Pager.journalOff          Return value
43627**   ---------------------------------------
43628**   0                         0
43629**   512                       512
43630**   100                       512
43631**   2000                      2048
43632**
43633*/
43634static i64 journalHdrOffset(Pager *pPager){
43635  i64 offset = 0;
43636  i64 c = pPager->journalOff;
43637  if( c ){
43638    offset = ((c-1)/JOURNAL_HDR_SZ(pPager) + 1) * JOURNAL_HDR_SZ(pPager);
43639  }
43640  assert( offset%JOURNAL_HDR_SZ(pPager)==0 );
43641  assert( offset>=c );
43642  assert( (offset-c)<JOURNAL_HDR_SZ(pPager) );
43643  return offset;
43644}
43645
43646/*
43647** The journal file must be open when this function is called.
43648**
43649** This function is a no-op if the journal file has not been written to
43650** within the current transaction (i.e. if Pager.journalOff==0).
43651**
43652** If doTruncate is non-zero or the Pager.journalSizeLimit variable is
43653** set to 0, then truncate the journal file to zero bytes in size. Otherwise,
43654** zero the 28-byte header at the start of the journal file. In either case,
43655** if the pager is not in no-sync mode, sync the journal file immediately
43656** after writing or truncating it.
43657**
43658** If Pager.journalSizeLimit is set to a positive, non-zero value, and
43659** following the truncation or zeroing described above the size of the
43660** journal file in bytes is larger than this value, then truncate the
43661** journal file to Pager.journalSizeLimit bytes. The journal file does
43662** not need to be synced following this operation.
43663**
43664** If an IO error occurs, abandon processing and return the IO error code.
43665** Otherwise, return SQLITE_OK.
43666*/
43667static int zeroJournalHdr(Pager *pPager, int doTruncate){
43668  int rc = SQLITE_OK;                               /* Return code */
43669  assert( isOpen(pPager->jfd) );
43670  if( pPager->journalOff ){
43671    const i64 iLimit = pPager->journalSizeLimit;    /* Local cache of jsl */
43672
43673    IOTRACE(("JZEROHDR %p\n", pPager))
43674    if( doTruncate || iLimit==0 ){
43675      rc = sqlite3OsTruncate(pPager->jfd, 0);
43676    }else{
43677      static const char zeroHdr[28] = {0};
43678      rc = sqlite3OsWrite(pPager->jfd, zeroHdr, sizeof(zeroHdr), 0);
43679    }
43680    if( rc==SQLITE_OK && !pPager->noSync ){
43681      rc = sqlite3OsSync(pPager->jfd, SQLITE_SYNC_DATAONLY|pPager->syncFlags);
43682    }
43683
43684    /* At this point the transaction is committed but the write lock
43685    ** is still held on the file. If there is a size limit configured for
43686    ** the persistent journal and the journal file currently consumes more
43687    ** space than that limit allows for, truncate it now. There is no need
43688    ** to sync the file following this operation.
43689    */
43690    if( rc==SQLITE_OK && iLimit>0 ){
43691      i64 sz;
43692      rc = sqlite3OsFileSize(pPager->jfd, &sz);
43693      if( rc==SQLITE_OK && sz>iLimit ){
43694        rc = sqlite3OsTruncate(pPager->jfd, iLimit);
43695      }
43696    }
43697  }
43698  return rc;
43699}
43700
43701/*
43702** The journal file must be open when this routine is called. A journal
43703** header (JOURNAL_HDR_SZ bytes) is written into the journal file at the
43704** current location.
43705**
43706** The format for the journal header is as follows:
43707** - 8 bytes: Magic identifying journal format.
43708** - 4 bytes: Number of records in journal, or -1 no-sync mode is on.
43709** - 4 bytes: Random number used for page hash.
43710** - 4 bytes: Initial database page count.
43711** - 4 bytes: Sector size used by the process that wrote this journal.
43712** - 4 bytes: Database page size.
43713**
43714** Followed by (JOURNAL_HDR_SZ - 28) bytes of unused space.
43715*/
43716static int writeJournalHdr(Pager *pPager){
43717  int rc = SQLITE_OK;                 /* Return code */
43718  char *zHeader = pPager->pTmpSpace;  /* Temporary space used to build header */
43719  u32 nHeader = (u32)pPager->pageSize;/* Size of buffer pointed to by zHeader */
43720  u32 nWrite;                         /* Bytes of header sector written */
43721  int ii;                             /* Loop counter */
43722
43723  assert( isOpen(pPager->jfd) );      /* Journal file must be open. */
43724
43725  if( nHeader>JOURNAL_HDR_SZ(pPager) ){
43726    nHeader = JOURNAL_HDR_SZ(pPager);
43727  }
43728
43729  /* If there are active savepoints and any of them were created
43730  ** since the most recent journal header was written, update the
43731  ** PagerSavepoint.iHdrOffset fields now.
43732  */
43733  for(ii=0; ii<pPager->nSavepoint; ii++){
43734    if( pPager->aSavepoint[ii].iHdrOffset==0 ){
43735      pPager->aSavepoint[ii].iHdrOffset = pPager->journalOff;
43736    }
43737  }
43738
43739  pPager->journalHdr = pPager->journalOff = journalHdrOffset(pPager);
43740
43741  /*
43742  ** Write the nRec Field - the number of page records that follow this
43743  ** journal header. Normally, zero is written to this value at this time.
43744  ** After the records are added to the journal (and the journal synced,
43745  ** if in full-sync mode), the zero is overwritten with the true number
43746  ** of records (see syncJournal()).
43747  **
43748  ** A faster alternative is to write 0xFFFFFFFF to the nRec field. When
43749  ** reading the journal this value tells SQLite to assume that the
43750  ** rest of the journal file contains valid page records. This assumption
43751  ** is dangerous, as if a failure occurred whilst writing to the journal
43752  ** file it may contain some garbage data. There are two scenarios
43753  ** where this risk can be ignored:
43754  **
43755  **   * When the pager is in no-sync mode. Corruption can follow a
43756  **     power failure in this case anyway.
43757  **
43758  **   * When the SQLITE_IOCAP_SAFE_APPEND flag is set. This guarantees
43759  **     that garbage data is never appended to the journal file.
43760  */
43761  assert( isOpen(pPager->fd) || pPager->noSync );
43762  if( pPager->noSync || (pPager->journalMode==PAGER_JOURNALMODE_MEMORY)
43763   || (sqlite3OsDeviceCharacteristics(pPager->fd)&SQLITE_IOCAP_SAFE_APPEND)
43764  ){
43765    memcpy(zHeader, aJournalMagic, sizeof(aJournalMagic));
43766    put32bits(&zHeader[sizeof(aJournalMagic)], 0xffffffff);
43767  }else{
43768    memset(zHeader, 0, sizeof(aJournalMagic)+4);
43769  }
43770
43771  /* The random check-hash initializer */
43772  sqlite3_randomness(sizeof(pPager->cksumInit), &pPager->cksumInit);
43773  put32bits(&zHeader[sizeof(aJournalMagic)+4], pPager->cksumInit);
43774  /* The initial database size */
43775  put32bits(&zHeader[sizeof(aJournalMagic)+8], pPager->dbOrigSize);
43776  /* The assumed sector size for this process */
43777  put32bits(&zHeader[sizeof(aJournalMagic)+12], pPager->sectorSize);
43778
43779  /* The page size */
43780  put32bits(&zHeader[sizeof(aJournalMagic)+16], pPager->pageSize);
43781
43782  /* Initializing the tail of the buffer is not necessary.  Everything
43783  ** works find if the following memset() is omitted.  But initializing
43784  ** the memory prevents valgrind from complaining, so we are willing to
43785  ** take the performance hit.
43786  */
43787  memset(&zHeader[sizeof(aJournalMagic)+20], 0,
43788         nHeader-(sizeof(aJournalMagic)+20));
43789
43790  /* In theory, it is only necessary to write the 28 bytes that the
43791  ** journal header consumes to the journal file here. Then increment the
43792  ** Pager.journalOff variable by JOURNAL_HDR_SZ so that the next
43793  ** record is written to the following sector (leaving a gap in the file
43794  ** that will be implicitly filled in by the OS).
43795  **
43796  ** However it has been discovered that on some systems this pattern can
43797  ** be significantly slower than contiguously writing data to the file,
43798  ** even if that means explicitly writing data to the block of
43799  ** (JOURNAL_HDR_SZ - 28) bytes that will not be used. So that is what
43800  ** is done.
43801  **
43802  ** The loop is required here in case the sector-size is larger than the
43803  ** database page size. Since the zHeader buffer is only Pager.pageSize
43804  ** bytes in size, more than one call to sqlite3OsWrite() may be required
43805  ** to populate the entire journal header sector.
43806  */
43807  for(nWrite=0; rc==SQLITE_OK&&nWrite<JOURNAL_HDR_SZ(pPager); nWrite+=nHeader){
43808    IOTRACE(("JHDR %p %lld %d\n", pPager, pPager->journalHdr, nHeader))
43809    rc = sqlite3OsWrite(pPager->jfd, zHeader, nHeader, pPager->journalOff);
43810    assert( pPager->journalHdr <= pPager->journalOff );
43811    pPager->journalOff += nHeader;
43812  }
43813
43814  return rc;
43815}
43816
43817/*
43818** The journal file must be open when this is called. A journal header file
43819** (JOURNAL_HDR_SZ bytes) is read from the current location in the journal
43820** file. The current location in the journal file is given by
43821** pPager->journalOff. See comments above function writeJournalHdr() for
43822** a description of the journal header format.
43823**
43824** If the header is read successfully, *pNRec is set to the number of
43825** page records following this header and *pDbSize is set to the size of the
43826** database before the transaction began, in pages. Also, pPager->cksumInit
43827** is set to the value read from the journal header. SQLITE_OK is returned
43828** in this case.
43829**
43830** If the journal header file appears to be corrupted, SQLITE_DONE is
43831** returned and *pNRec and *PDbSize are undefined.  If JOURNAL_HDR_SZ bytes
43832** cannot be read from the journal file an error code is returned.
43833*/
43834static int readJournalHdr(
43835  Pager *pPager,               /* Pager object */
43836  int isHot,
43837  i64 journalSize,             /* Size of the open journal file in bytes */
43838  u32 *pNRec,                  /* OUT: Value read from the nRec field */
43839  u32 *pDbSize                 /* OUT: Value of original database size field */
43840){
43841  int rc;                      /* Return code */
43842  unsigned char aMagic[8];     /* A buffer to hold the magic header */
43843  i64 iHdrOff;                 /* Offset of journal header being read */
43844
43845  assert( isOpen(pPager->jfd) );      /* Journal file must be open. */
43846
43847  /* Advance Pager.journalOff to the start of the next sector. If the
43848  ** journal file is too small for there to be a header stored at this
43849  ** point, return SQLITE_DONE.
43850  */
43851  pPager->journalOff = journalHdrOffset(pPager);
43852  if( pPager->journalOff+JOURNAL_HDR_SZ(pPager) > journalSize ){
43853    return SQLITE_DONE;
43854  }
43855  iHdrOff = pPager->journalOff;
43856
43857  /* Read in the first 8 bytes of the journal header. If they do not match
43858  ** the  magic string found at the start of each journal header, return
43859  ** SQLITE_DONE. If an IO error occurs, return an error code. Otherwise,
43860  ** proceed.
43861  */
43862  if( isHot || iHdrOff!=pPager->journalHdr ){
43863    rc = sqlite3OsRead(pPager->jfd, aMagic, sizeof(aMagic), iHdrOff);
43864    if( rc ){
43865      return rc;
43866    }
43867    if( memcmp(aMagic, aJournalMagic, sizeof(aMagic))!=0 ){
43868      return SQLITE_DONE;
43869    }
43870  }
43871
43872  /* Read the first three 32-bit fields of the journal header: The nRec
43873  ** field, the checksum-initializer and the database size at the start
43874  ** of the transaction. Return an error code if anything goes wrong.
43875  */
43876  if( SQLITE_OK!=(rc = read32bits(pPager->jfd, iHdrOff+8, pNRec))
43877   || SQLITE_OK!=(rc = read32bits(pPager->jfd, iHdrOff+12, &pPager->cksumInit))
43878   || SQLITE_OK!=(rc = read32bits(pPager->jfd, iHdrOff+16, pDbSize))
43879  ){
43880    return rc;
43881  }
43882
43883  if( pPager->journalOff==0 ){
43884    u32 iPageSize;               /* Page-size field of journal header */
43885    u32 iSectorSize;             /* Sector-size field of journal header */
43886
43887    /* Read the page-size and sector-size journal header fields. */
43888    if( SQLITE_OK!=(rc = read32bits(pPager->jfd, iHdrOff+20, &iSectorSize))
43889     || SQLITE_OK!=(rc = read32bits(pPager->jfd, iHdrOff+24, &iPageSize))
43890    ){
43891      return rc;
43892    }
43893
43894    /* Versions of SQLite prior to 3.5.8 set the page-size field of the
43895    ** journal header to zero. In this case, assume that the Pager.pageSize
43896    ** variable is already set to the correct page size.
43897    */
43898    if( iPageSize==0 ){
43899      iPageSize = pPager->pageSize;
43900    }
43901
43902    /* Check that the values read from the page-size and sector-size fields
43903    ** are within range. To be 'in range', both values need to be a power
43904    ** of two greater than or equal to 512 or 32, and not greater than their
43905    ** respective compile time maximum limits.
43906    */
43907    if( iPageSize<512                  || iSectorSize<32
43908     || iPageSize>SQLITE_MAX_PAGE_SIZE || iSectorSize>MAX_SECTOR_SIZE
43909     || ((iPageSize-1)&iPageSize)!=0   || ((iSectorSize-1)&iSectorSize)!=0
43910    ){
43911      /* If the either the page-size or sector-size in the journal-header is
43912      ** invalid, then the process that wrote the journal-header must have
43913      ** crashed before the header was synced. In this case stop reading
43914      ** the journal file here.
43915      */
43916      return SQLITE_DONE;
43917    }
43918
43919    /* Update the page-size to match the value read from the journal.
43920    ** Use a testcase() macro to make sure that malloc failure within
43921    ** PagerSetPagesize() is tested.
43922    */
43923    rc = sqlite3PagerSetPagesize(pPager, &iPageSize, -1);
43924    testcase( rc!=SQLITE_OK );
43925
43926    /* Update the assumed sector-size to match the value used by
43927    ** the process that created this journal. If this journal was
43928    ** created by a process other than this one, then this routine
43929    ** is being called from within pager_playback(). The local value
43930    ** of Pager.sectorSize is restored at the end of that routine.
43931    */
43932    pPager->sectorSize = iSectorSize;
43933  }
43934
43935  pPager->journalOff += JOURNAL_HDR_SZ(pPager);
43936  return rc;
43937}
43938
43939
43940/*
43941** Write the supplied master journal name into the journal file for pager
43942** pPager at the current location. The master journal name must be the last
43943** thing written to a journal file. If the pager is in full-sync mode, the
43944** journal file descriptor is advanced to the next sector boundary before
43945** anything is written. The format is:
43946**
43947**   + 4 bytes: PAGER_MJ_PGNO.
43948**   + N bytes: Master journal filename in utf-8.
43949**   + 4 bytes: N (length of master journal name in bytes, no nul-terminator).
43950**   + 4 bytes: Master journal name checksum.
43951**   + 8 bytes: aJournalMagic[].
43952**
43953** The master journal page checksum is the sum of the bytes in the master
43954** journal name, where each byte is interpreted as a signed 8-bit integer.
43955**
43956** If zMaster is a NULL pointer (occurs for a single database transaction),
43957** this call is a no-op.
43958*/
43959static int writeMasterJournal(Pager *pPager, const char *zMaster){
43960  int rc;                          /* Return code */
43961  int nMaster;                     /* Length of string zMaster */
43962  i64 iHdrOff;                     /* Offset of header in journal file */
43963  i64 jrnlSize;                    /* Size of journal file on disk */
43964  u32 cksum = 0;                   /* Checksum of string zMaster */
43965
43966  assert( pPager->setMaster==0 );
43967  assert( !pagerUseWal(pPager) );
43968
43969  if( !zMaster
43970   || pPager->journalMode==PAGER_JOURNALMODE_MEMORY
43971   || !isOpen(pPager->jfd)
43972  ){
43973    return SQLITE_OK;
43974  }
43975  pPager->setMaster = 1;
43976  assert( pPager->journalHdr <= pPager->journalOff );
43977
43978  /* Calculate the length in bytes and the checksum of zMaster */
43979  for(nMaster=0; zMaster[nMaster]; nMaster++){
43980    cksum += zMaster[nMaster];
43981  }
43982
43983  /* If in full-sync mode, advance to the next disk sector before writing
43984  ** the master journal name. This is in case the previous page written to
43985  ** the journal has already been synced.
43986  */
43987  if( pPager->fullSync ){
43988    pPager->journalOff = journalHdrOffset(pPager);
43989  }
43990  iHdrOff = pPager->journalOff;
43991
43992  /* Write the master journal data to the end of the journal file. If
43993  ** an error occurs, return the error code to the caller.
43994  */
43995  if( (0 != (rc = write32bits(pPager->jfd, iHdrOff, PAGER_MJ_PGNO(pPager))))
43996   || (0 != (rc = sqlite3OsWrite(pPager->jfd, zMaster, nMaster, iHdrOff+4)))
43997   || (0 != (rc = write32bits(pPager->jfd, iHdrOff+4+nMaster, nMaster)))
43998   || (0 != (rc = write32bits(pPager->jfd, iHdrOff+4+nMaster+4, cksum)))
43999   || (0 != (rc = sqlite3OsWrite(pPager->jfd, aJournalMagic, 8,
44000                                 iHdrOff+4+nMaster+8)))
44001  ){
44002    return rc;
44003  }
44004  pPager->journalOff += (nMaster+20);
44005
44006  /* If the pager is in peristent-journal mode, then the physical
44007  ** journal-file may extend past the end of the master-journal name
44008  ** and 8 bytes of magic data just written to the file. This is
44009  ** dangerous because the code to rollback a hot-journal file
44010  ** will not be able to find the master-journal name to determine
44011  ** whether or not the journal is hot.
44012  **
44013  ** Easiest thing to do in this scenario is to truncate the journal
44014  ** file to the required size.
44015  */
44016  if( SQLITE_OK==(rc = sqlite3OsFileSize(pPager->jfd, &jrnlSize))
44017   && jrnlSize>pPager->journalOff
44018  ){
44019    rc = sqlite3OsTruncate(pPager->jfd, pPager->journalOff);
44020  }
44021  return rc;
44022}
44023
44024/*
44025** Discard the entire contents of the in-memory page-cache.
44026*/
44027static void pager_reset(Pager *pPager){
44028  pPager->iDataVersion++;
44029  sqlite3BackupRestart(pPager->pBackup);
44030  sqlite3PcacheClear(pPager->pPCache);
44031}
44032
44033/*
44034** Return the pPager->iDataVersion value
44035*/
44036SQLITE_PRIVATE u32 sqlite3PagerDataVersion(Pager *pPager){
44037  assert( pPager->eState>PAGER_OPEN );
44038  return pPager->iDataVersion;
44039}
44040
44041/*
44042** Free all structures in the Pager.aSavepoint[] array and set both
44043** Pager.aSavepoint and Pager.nSavepoint to zero. Close the sub-journal
44044** if it is open and the pager is not in exclusive mode.
44045*/
44046static void releaseAllSavepoints(Pager *pPager){
44047  int ii;               /* Iterator for looping through Pager.aSavepoint */
44048  for(ii=0; ii<pPager->nSavepoint; ii++){
44049    sqlite3BitvecDestroy(pPager->aSavepoint[ii].pInSavepoint);
44050  }
44051  if( !pPager->exclusiveMode || sqlite3IsMemJournal(pPager->sjfd) ){
44052    sqlite3OsClose(pPager->sjfd);
44053  }
44054  sqlite3_free(pPager->aSavepoint);
44055  pPager->aSavepoint = 0;
44056  pPager->nSavepoint = 0;
44057  pPager->nSubRec = 0;
44058}
44059
44060/*
44061** Set the bit number pgno in the PagerSavepoint.pInSavepoint
44062** bitvecs of all open savepoints. Return SQLITE_OK if successful
44063** or SQLITE_NOMEM if a malloc failure occurs.
44064*/
44065static int addToSavepointBitvecs(Pager *pPager, Pgno pgno){
44066  int ii;                   /* Loop counter */
44067  int rc = SQLITE_OK;       /* Result code */
44068
44069  for(ii=0; ii<pPager->nSavepoint; ii++){
44070    PagerSavepoint *p = &pPager->aSavepoint[ii];
44071    if( pgno<=p->nOrig ){
44072      rc |= sqlite3BitvecSet(p->pInSavepoint, pgno);
44073      testcase( rc==SQLITE_NOMEM );
44074      assert( rc==SQLITE_OK || rc==SQLITE_NOMEM );
44075    }
44076  }
44077  return rc;
44078}
44079
44080/*
44081** This function is a no-op if the pager is in exclusive mode and not
44082** in the ERROR state. Otherwise, it switches the pager to PAGER_OPEN
44083** state.
44084**
44085** If the pager is not in exclusive-access mode, the database file is
44086** completely unlocked. If the file is unlocked and the file-system does
44087** not exhibit the UNDELETABLE_WHEN_OPEN property, the journal file is
44088** closed (if it is open).
44089**
44090** If the pager is in ERROR state when this function is called, the
44091** contents of the pager cache are discarded before switching back to
44092** the OPEN state. Regardless of whether the pager is in exclusive-mode
44093** or not, any journal file left in the file-system will be treated
44094** as a hot-journal and rolled back the next time a read-transaction
44095** is opened (by this or by any other connection).
44096*/
44097static void pager_unlock(Pager *pPager){
44098
44099  assert( pPager->eState==PAGER_READER
44100       || pPager->eState==PAGER_OPEN
44101       || pPager->eState==PAGER_ERROR
44102  );
44103
44104  sqlite3BitvecDestroy(pPager->pInJournal);
44105  pPager->pInJournal = 0;
44106  releaseAllSavepoints(pPager);
44107
44108  if( pagerUseWal(pPager) ){
44109    assert( !isOpen(pPager->jfd) );
44110    sqlite3WalEndReadTransaction(pPager->pWal);
44111    pPager->eState = PAGER_OPEN;
44112  }else if( !pPager->exclusiveMode ){
44113    int rc;                       /* Error code returned by pagerUnlockDb() */
44114    int iDc = isOpen(pPager->fd)?sqlite3OsDeviceCharacteristics(pPager->fd):0;
44115
44116    /* If the operating system support deletion of open files, then
44117    ** close the journal file when dropping the database lock.  Otherwise
44118    ** another connection with journal_mode=delete might delete the file
44119    ** out from under us.
44120    */
44121    assert( (PAGER_JOURNALMODE_MEMORY   & 5)!=1 );
44122    assert( (PAGER_JOURNALMODE_OFF      & 5)!=1 );
44123    assert( (PAGER_JOURNALMODE_WAL      & 5)!=1 );
44124    assert( (PAGER_JOURNALMODE_DELETE   & 5)!=1 );
44125    assert( (PAGER_JOURNALMODE_TRUNCATE & 5)==1 );
44126    assert( (PAGER_JOURNALMODE_PERSIST  & 5)==1 );
44127    if( 0==(iDc & SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN)
44128     || 1!=(pPager->journalMode & 5)
44129    ){
44130      sqlite3OsClose(pPager->jfd);
44131    }
44132
44133    /* If the pager is in the ERROR state and the call to unlock the database
44134    ** file fails, set the current lock to UNKNOWN_LOCK. See the comment
44135    ** above the #define for UNKNOWN_LOCK for an explanation of why this
44136    ** is necessary.
44137    */
44138    rc = pagerUnlockDb(pPager, NO_LOCK);
44139    if( rc!=SQLITE_OK && pPager->eState==PAGER_ERROR ){
44140      pPager->eLock = UNKNOWN_LOCK;
44141    }
44142
44143    /* The pager state may be changed from PAGER_ERROR to PAGER_OPEN here
44144    ** without clearing the error code. This is intentional - the error
44145    ** code is cleared and the cache reset in the block below.
44146    */
44147    assert( pPager->errCode || pPager->eState!=PAGER_ERROR );
44148    pPager->changeCountDone = 0;
44149    pPager->eState = PAGER_OPEN;
44150  }
44151
44152  /* If Pager.errCode is set, the contents of the pager cache cannot be
44153  ** trusted. Now that there are no outstanding references to the pager,
44154  ** it can safely move back to PAGER_OPEN state. This happens in both
44155  ** normal and exclusive-locking mode.
44156  */
44157  if( pPager->errCode ){
44158    assert( !MEMDB );
44159    pager_reset(pPager);
44160    pPager->changeCountDone = pPager->tempFile;
44161    pPager->eState = PAGER_OPEN;
44162    pPager->errCode = SQLITE_OK;
44163    if( USEFETCH(pPager) ) sqlite3OsUnfetch(pPager->fd, 0, 0);
44164  }
44165
44166  pPager->journalOff = 0;
44167  pPager->journalHdr = 0;
44168  pPager->setMaster = 0;
44169}
44170
44171/*
44172** This function is called whenever an IOERR or FULL error that requires
44173** the pager to transition into the ERROR state may ahve occurred.
44174** The first argument is a pointer to the pager structure, the second
44175** the error-code about to be returned by a pager API function. The
44176** value returned is a copy of the second argument to this function.
44177**
44178** If the second argument is SQLITE_FULL, SQLITE_IOERR or one of the
44179** IOERR sub-codes, the pager enters the ERROR state and the error code
44180** is stored in Pager.errCode. While the pager remains in the ERROR state,
44181** all major API calls on the Pager will immediately return Pager.errCode.
44182**
44183** The ERROR state indicates that the contents of the pager-cache
44184** cannot be trusted. This state can be cleared by completely discarding
44185** the contents of the pager-cache. If a transaction was active when
44186** the persistent error occurred, then the rollback journal may need
44187** to be replayed to restore the contents of the database file (as if
44188** it were a hot-journal).
44189*/
44190static int pager_error(Pager *pPager, int rc){
44191  int rc2 = rc & 0xff;
44192  assert( rc==SQLITE_OK || !MEMDB );
44193  assert(
44194       pPager->errCode==SQLITE_FULL ||
44195       pPager->errCode==SQLITE_OK ||
44196       (pPager->errCode & 0xff)==SQLITE_IOERR
44197  );
44198  if( rc2==SQLITE_FULL || rc2==SQLITE_IOERR ){
44199    pPager->errCode = rc;
44200    pPager->eState = PAGER_ERROR;
44201  }
44202  return rc;
44203}
44204
44205static int pager_truncate(Pager *pPager, Pgno nPage);
44206
44207/*
44208** This routine ends a transaction. A transaction is usually ended by
44209** either a COMMIT or a ROLLBACK operation. This routine may be called
44210** after rollback of a hot-journal, or if an error occurs while opening
44211** the journal file or writing the very first journal-header of a
44212** database transaction.
44213**
44214** This routine is never called in PAGER_ERROR state. If it is called
44215** in PAGER_NONE or PAGER_SHARED state and the lock held is less
44216** exclusive than a RESERVED lock, it is a no-op.
44217**
44218** Otherwise, any active savepoints are released.
44219**
44220** If the journal file is open, then it is "finalized". Once a journal
44221** file has been finalized it is not possible to use it to roll back a
44222** transaction. Nor will it be considered to be a hot-journal by this
44223** or any other database connection. Exactly how a journal is finalized
44224** depends on whether or not the pager is running in exclusive mode and
44225** the current journal-mode (Pager.journalMode value), as follows:
44226**
44227**   journalMode==MEMORY
44228**     Journal file descriptor is simply closed. This destroys an
44229**     in-memory journal.
44230**
44231**   journalMode==TRUNCATE
44232**     Journal file is truncated to zero bytes in size.
44233**
44234**   journalMode==PERSIST
44235**     The first 28 bytes of the journal file are zeroed. This invalidates
44236**     the first journal header in the file, and hence the entire journal
44237**     file. An invalid journal file cannot be rolled back.
44238**
44239**   journalMode==DELETE
44240**     The journal file is closed and deleted using sqlite3OsDelete().
44241**
44242**     If the pager is running in exclusive mode, this method of finalizing
44243**     the journal file is never used. Instead, if the journalMode is
44244**     DELETE and the pager is in exclusive mode, the method described under
44245**     journalMode==PERSIST is used instead.
44246**
44247** After the journal is finalized, the pager moves to PAGER_READER state.
44248** If running in non-exclusive rollback mode, the lock on the file is
44249** downgraded to a SHARED_LOCK.
44250**
44251** SQLITE_OK is returned if no error occurs. If an error occurs during
44252** any of the IO operations to finalize the journal file or unlock the
44253** database then the IO error code is returned to the user. If the
44254** operation to finalize the journal file fails, then the code still
44255** tries to unlock the database file if not in exclusive mode. If the
44256** unlock operation fails as well, then the first error code related
44257** to the first error encountered (the journal finalization one) is
44258** returned.
44259*/
44260static int pager_end_transaction(Pager *pPager, int hasMaster, int bCommit){
44261  int rc = SQLITE_OK;      /* Error code from journal finalization operation */
44262  int rc2 = SQLITE_OK;     /* Error code from db file unlock operation */
44263
44264  /* Do nothing if the pager does not have an open write transaction
44265  ** or at least a RESERVED lock. This function may be called when there
44266  ** is no write-transaction active but a RESERVED or greater lock is
44267  ** held under two circumstances:
44268  **
44269  **   1. After a successful hot-journal rollback, it is called with
44270  **      eState==PAGER_NONE and eLock==EXCLUSIVE_LOCK.
44271  **
44272  **   2. If a connection with locking_mode=exclusive holding an EXCLUSIVE
44273  **      lock switches back to locking_mode=normal and then executes a
44274  **      read-transaction, this function is called with eState==PAGER_READER
44275  **      and eLock==EXCLUSIVE_LOCK when the read-transaction is closed.
44276  */
44277  assert( assert_pager_state(pPager) );
44278  assert( pPager->eState!=PAGER_ERROR );
44279  if( pPager->eState<PAGER_WRITER_LOCKED && pPager->eLock<RESERVED_LOCK ){
44280    return SQLITE_OK;
44281  }
44282
44283  releaseAllSavepoints(pPager);
44284  assert( isOpen(pPager->jfd) || pPager->pInJournal==0 );
44285  if( isOpen(pPager->jfd) ){
44286    assert( !pagerUseWal(pPager) );
44287
44288    /* Finalize the journal file. */
44289    if( sqlite3IsMemJournal(pPager->jfd) ){
44290      assert( pPager->journalMode==PAGER_JOURNALMODE_MEMORY );
44291      sqlite3OsClose(pPager->jfd);
44292    }else if( pPager->journalMode==PAGER_JOURNALMODE_TRUNCATE ){
44293      if( pPager->journalOff==0 ){
44294        rc = SQLITE_OK;
44295      }else{
44296        rc = sqlite3OsTruncate(pPager->jfd, 0);
44297        if( rc==SQLITE_OK && pPager->fullSync ){
44298          /* Make sure the new file size is written into the inode right away.
44299          ** Otherwise the journal might resurrect following a power loss and
44300          ** cause the last transaction to roll back.  See
44301          ** https://bugzilla.mozilla.org/show_bug.cgi?id=1072773
44302          */
44303          rc = sqlite3OsSync(pPager->jfd, pPager->syncFlags);
44304        }
44305      }
44306      pPager->journalOff = 0;
44307    }else if( pPager->journalMode==PAGER_JOURNALMODE_PERSIST
44308      || (pPager->exclusiveMode && pPager->journalMode!=PAGER_JOURNALMODE_WAL)
44309    ){
44310      rc = zeroJournalHdr(pPager, hasMaster);
44311      pPager->journalOff = 0;
44312    }else{
44313      /* This branch may be executed with Pager.journalMode==MEMORY if
44314      ** a hot-journal was just rolled back. In this case the journal
44315      ** file should be closed and deleted. If this connection writes to
44316      ** the database file, it will do so using an in-memory journal.
44317      */
44318      int bDelete = (!pPager->tempFile && sqlite3JournalExists(pPager->jfd));
44319      assert( pPager->journalMode==PAGER_JOURNALMODE_DELETE
44320           || pPager->journalMode==PAGER_JOURNALMODE_MEMORY
44321           || pPager->journalMode==PAGER_JOURNALMODE_WAL
44322      );
44323      sqlite3OsClose(pPager->jfd);
44324      if( bDelete ){
44325        rc = sqlite3OsDelete(pPager->pVfs, pPager->zJournal, 0);
44326      }
44327    }
44328  }
44329
44330#ifdef SQLITE_CHECK_PAGES
44331  sqlite3PcacheIterateDirty(pPager->pPCache, pager_set_pagehash);
44332  if( pPager->dbSize==0 && sqlite3PcacheRefCount(pPager->pPCache)>0 ){
44333    PgHdr *p = sqlite3PagerLookup(pPager, 1);
44334    if( p ){
44335      p->pageHash = 0;
44336      sqlite3PagerUnrefNotNull(p);
44337    }
44338  }
44339#endif
44340
44341  sqlite3BitvecDestroy(pPager->pInJournal);
44342  pPager->pInJournal = 0;
44343  pPager->nRec = 0;
44344  sqlite3PcacheCleanAll(pPager->pPCache);
44345  sqlite3PcacheTruncate(pPager->pPCache, pPager->dbSize);
44346
44347  if( pagerUseWal(pPager) ){
44348    /* Drop the WAL write-lock, if any. Also, if the connection was in
44349    ** locking_mode=exclusive mode but is no longer, drop the EXCLUSIVE
44350    ** lock held on the database file.
44351    */
44352    rc2 = sqlite3WalEndWriteTransaction(pPager->pWal);
44353    assert( rc2==SQLITE_OK );
44354  }else if( rc==SQLITE_OK && bCommit && pPager->dbFileSize>pPager->dbSize ){
44355    /* This branch is taken when committing a transaction in rollback-journal
44356    ** mode if the database file on disk is larger than the database image.
44357    ** At this point the journal has been finalized and the transaction
44358    ** successfully committed, but the EXCLUSIVE lock is still held on the
44359    ** file. So it is safe to truncate the database file to its minimum
44360    ** required size.  */
44361    assert( pPager->eLock==EXCLUSIVE_LOCK );
44362    rc = pager_truncate(pPager, pPager->dbSize);
44363  }
44364
44365  if( rc==SQLITE_OK && bCommit && isOpen(pPager->fd) ){
44366    rc = sqlite3OsFileControl(pPager->fd, SQLITE_FCNTL_COMMIT_PHASETWO, 0);
44367    if( rc==SQLITE_NOTFOUND ) rc = SQLITE_OK;
44368  }
44369
44370  if( !pPager->exclusiveMode
44371   && (!pagerUseWal(pPager) || sqlite3WalExclusiveMode(pPager->pWal, 0))
44372  ){
44373    rc2 = pagerUnlockDb(pPager, SHARED_LOCK);
44374    pPager->changeCountDone = 0;
44375  }
44376  pPager->eState = PAGER_READER;
44377  pPager->setMaster = 0;
44378
44379  return (rc==SQLITE_OK?rc2:rc);
44380}
44381
44382/*
44383** Execute a rollback if a transaction is active and unlock the
44384** database file.
44385**
44386** If the pager has already entered the ERROR state, do not attempt
44387** the rollback at this time. Instead, pager_unlock() is called. The
44388** call to pager_unlock() will discard all in-memory pages, unlock
44389** the database file and move the pager back to OPEN state. If this
44390** means that there is a hot-journal left in the file-system, the next
44391** connection to obtain a shared lock on the pager (which may be this one)
44392** will roll it back.
44393**
44394** If the pager has not already entered the ERROR state, but an IO or
44395** malloc error occurs during a rollback, then this will itself cause
44396** the pager to enter the ERROR state. Which will be cleared by the
44397** call to pager_unlock(), as described above.
44398*/
44399static void pagerUnlockAndRollback(Pager *pPager){
44400  if( pPager->eState!=PAGER_ERROR && pPager->eState!=PAGER_OPEN ){
44401    assert( assert_pager_state(pPager) );
44402    if( pPager->eState>=PAGER_WRITER_LOCKED ){
44403      sqlite3BeginBenignMalloc();
44404      sqlite3PagerRollback(pPager);
44405      sqlite3EndBenignMalloc();
44406    }else if( !pPager->exclusiveMode ){
44407      assert( pPager->eState==PAGER_READER );
44408      pager_end_transaction(pPager, 0, 0);
44409    }
44410  }
44411  pager_unlock(pPager);
44412}
44413
44414/*
44415** Parameter aData must point to a buffer of pPager->pageSize bytes
44416** of data. Compute and return a checksum based ont the contents of the
44417** page of data and the current value of pPager->cksumInit.
44418**
44419** This is not a real checksum. It is really just the sum of the
44420** random initial value (pPager->cksumInit) and every 200th byte
44421** of the page data, starting with byte offset (pPager->pageSize%200).
44422** Each byte is interpreted as an 8-bit unsigned integer.
44423**
44424** Changing the formula used to compute this checksum results in an
44425** incompatible journal file format.
44426**
44427** If journal corruption occurs due to a power failure, the most likely
44428** scenario is that one end or the other of the record will be changed.
44429** It is much less likely that the two ends of the journal record will be
44430** correct and the middle be corrupt.  Thus, this "checksum" scheme,
44431** though fast and simple, catches the mostly likely kind of corruption.
44432*/
44433static u32 pager_cksum(Pager *pPager, const u8 *aData){
44434  u32 cksum = pPager->cksumInit;         /* Checksum value to return */
44435  int i = pPager->pageSize-200;          /* Loop counter */
44436  while( i>0 ){
44437    cksum += aData[i];
44438    i -= 200;
44439  }
44440  return cksum;
44441}
44442
44443/*
44444** Report the current page size and number of reserved bytes back
44445** to the codec.
44446*/
44447#ifdef SQLITE_HAS_CODEC
44448static void pagerReportSize(Pager *pPager){
44449  if( pPager->xCodecSizeChng ){
44450    pPager->xCodecSizeChng(pPager->pCodec, pPager->pageSize,
44451                           (int)pPager->nReserve);
44452  }
44453}
44454#else
44455# define pagerReportSize(X)     /* No-op if we do not support a codec */
44456#endif
44457
44458/*
44459** Read a single page from either the journal file (if isMainJrnl==1) or
44460** from the sub-journal (if isMainJrnl==0) and playback that page.
44461** The page begins at offset *pOffset into the file. The *pOffset
44462** value is increased to the start of the next page in the journal.
44463**
44464** The main rollback journal uses checksums - the statement journal does
44465** not.
44466**
44467** If the page number of the page record read from the (sub-)journal file
44468** is greater than the current value of Pager.dbSize, then playback is
44469** skipped and SQLITE_OK is returned.
44470**
44471** If pDone is not NULL, then it is a record of pages that have already
44472** been played back.  If the page at *pOffset has already been played back
44473** (if the corresponding pDone bit is set) then skip the playback.
44474** Make sure the pDone bit corresponding to the *pOffset page is set
44475** prior to returning.
44476**
44477** If the page record is successfully read from the (sub-)journal file
44478** and played back, then SQLITE_OK is returned. If an IO error occurs
44479** while reading the record from the (sub-)journal file or while writing
44480** to the database file, then the IO error code is returned. If data
44481** is successfully read from the (sub-)journal file but appears to be
44482** corrupted, SQLITE_DONE is returned. Data is considered corrupted in
44483** two circumstances:
44484**
44485**   * If the record page-number is illegal (0 or PAGER_MJ_PGNO), or
44486**   * If the record is being rolled back from the main journal file
44487**     and the checksum field does not match the record content.
44488**
44489** Neither of these two scenarios are possible during a savepoint rollback.
44490**
44491** If this is a savepoint rollback, then memory may have to be dynamically
44492** allocated by this function. If this is the case and an allocation fails,
44493** SQLITE_NOMEM is returned.
44494*/
44495static int pager_playback_one_page(
44496  Pager *pPager,                /* The pager being played back */
44497  i64 *pOffset,                 /* Offset of record to playback */
44498  Bitvec *pDone,                /* Bitvec of pages already played back */
44499  int isMainJrnl,               /* 1 -> main journal. 0 -> sub-journal. */
44500  int isSavepnt                 /* True for a savepoint rollback */
44501){
44502  int rc;
44503  PgHdr *pPg;                   /* An existing page in the cache */
44504  Pgno pgno;                    /* The page number of a page in journal */
44505  u32 cksum;                    /* Checksum used for sanity checking */
44506  char *aData;                  /* Temporary storage for the page */
44507  sqlite3_file *jfd;            /* The file descriptor for the journal file */
44508  int isSynced;                 /* True if journal page is synced */
44509
44510  assert( (isMainJrnl&~1)==0 );      /* isMainJrnl is 0 or 1 */
44511  assert( (isSavepnt&~1)==0 );       /* isSavepnt is 0 or 1 */
44512  assert( isMainJrnl || pDone );     /* pDone always used on sub-journals */
44513  assert( isSavepnt || pDone==0 );   /* pDone never used on non-savepoint */
44514
44515  aData = pPager->pTmpSpace;
44516  assert( aData );         /* Temp storage must have already been allocated */
44517  assert( pagerUseWal(pPager)==0 || (!isMainJrnl && isSavepnt) );
44518
44519  /* Either the state is greater than PAGER_WRITER_CACHEMOD (a transaction
44520  ** or savepoint rollback done at the request of the caller) or this is
44521  ** a hot-journal rollback. If it is a hot-journal rollback, the pager
44522  ** is in state OPEN and holds an EXCLUSIVE lock. Hot-journal rollback
44523  ** only reads from the main journal, not the sub-journal.
44524  */
44525  assert( pPager->eState>=PAGER_WRITER_CACHEMOD
44526       || (pPager->eState==PAGER_OPEN && pPager->eLock==EXCLUSIVE_LOCK)
44527  );
44528  assert( pPager->eState>=PAGER_WRITER_CACHEMOD || isMainJrnl );
44529
44530  /* Read the page number and page data from the journal or sub-journal
44531  ** file. Return an error code to the caller if an IO error occurs.
44532  */
44533  jfd = isMainJrnl ? pPager->jfd : pPager->sjfd;
44534  rc = read32bits(jfd, *pOffset, &pgno);
44535  if( rc!=SQLITE_OK ) return rc;
44536  rc = sqlite3OsRead(jfd, (u8*)aData, pPager->pageSize, (*pOffset)+4);
44537  if( rc!=SQLITE_OK ) return rc;
44538  *pOffset += pPager->pageSize + 4 + isMainJrnl*4;
44539
44540  /* Sanity checking on the page.  This is more important that I originally
44541  ** thought.  If a power failure occurs while the journal is being written,
44542  ** it could cause invalid data to be written into the journal.  We need to
44543  ** detect this invalid data (with high probability) and ignore it.
44544  */
44545  if( pgno==0 || pgno==PAGER_MJ_PGNO(pPager) ){
44546    assert( !isSavepnt );
44547    return SQLITE_DONE;
44548  }
44549  if( pgno>(Pgno)pPager->dbSize || sqlite3BitvecTest(pDone, pgno) ){
44550    return SQLITE_OK;
44551  }
44552  if( isMainJrnl ){
44553    rc = read32bits(jfd, (*pOffset)-4, &cksum);
44554    if( rc ) return rc;
44555    if( !isSavepnt && pager_cksum(pPager, (u8*)aData)!=cksum ){
44556      return SQLITE_DONE;
44557    }
44558  }
44559
44560  /* If this page has already been played back before during the current
44561  ** rollback, then don't bother to play it back again.
44562  */
44563  if( pDone && (rc = sqlite3BitvecSet(pDone, pgno))!=SQLITE_OK ){
44564    return rc;
44565  }
44566
44567  /* When playing back page 1, restore the nReserve setting
44568  */
44569  if( pgno==1 && pPager->nReserve!=((u8*)aData)[20] ){
44570    pPager->nReserve = ((u8*)aData)[20];
44571    pagerReportSize(pPager);
44572  }
44573
44574  /* If the pager is in CACHEMOD state, then there must be a copy of this
44575  ** page in the pager cache. In this case just update the pager cache,
44576  ** not the database file. The page is left marked dirty in this case.
44577  **
44578  ** An exception to the above rule: If the database is in no-sync mode
44579  ** and a page is moved during an incremental vacuum then the page may
44580  ** not be in the pager cache. Later: if a malloc() or IO error occurs
44581  ** during a Movepage() call, then the page may not be in the cache
44582  ** either. So the condition described in the above paragraph is not
44583  ** assert()able.
44584  **
44585  ** If in WRITER_DBMOD, WRITER_FINISHED or OPEN state, then we update the
44586  ** pager cache if it exists and the main file. The page is then marked
44587  ** not dirty. Since this code is only executed in PAGER_OPEN state for
44588  ** a hot-journal rollback, it is guaranteed that the page-cache is empty
44589  ** if the pager is in OPEN state.
44590  **
44591  ** Ticket #1171:  The statement journal might contain page content that is
44592  ** different from the page content at the start of the transaction.
44593  ** This occurs when a page is changed prior to the start of a statement
44594  ** then changed again within the statement.  When rolling back such a
44595  ** statement we must not write to the original database unless we know
44596  ** for certain that original page contents are synced into the main rollback
44597  ** journal.  Otherwise, a power loss might leave modified data in the
44598  ** database file without an entry in the rollback journal that can
44599  ** restore the database to its original form.  Two conditions must be
44600  ** met before writing to the database files. (1) the database must be
44601  ** locked.  (2) we know that the original page content is fully synced
44602  ** in the main journal either because the page is not in cache or else
44603  ** the page is marked as needSync==0.
44604  **
44605  ** 2008-04-14:  When attempting to vacuum a corrupt database file, it
44606  ** is possible to fail a statement on a database that does not yet exist.
44607  ** Do not attempt to write if database file has never been opened.
44608  */
44609  if( pagerUseWal(pPager) ){
44610    pPg = 0;
44611  }else{
44612    pPg = sqlite3PagerLookup(pPager, pgno);
44613  }
44614  assert( pPg || !MEMDB );
44615  assert( pPager->eState!=PAGER_OPEN || pPg==0 );
44616  PAGERTRACE(("PLAYBACK %d page %d hash(%08x) %s\n",
44617           PAGERID(pPager), pgno, pager_datahash(pPager->pageSize, (u8*)aData),
44618           (isMainJrnl?"main-journal":"sub-journal")
44619  ));
44620  if( isMainJrnl ){
44621    isSynced = pPager->noSync || (*pOffset <= pPager->journalHdr);
44622  }else{
44623    isSynced = (pPg==0 || 0==(pPg->flags & PGHDR_NEED_SYNC));
44624  }
44625  if( isOpen(pPager->fd)
44626   && (pPager->eState>=PAGER_WRITER_DBMOD || pPager->eState==PAGER_OPEN)
44627   && isSynced
44628  ){
44629    i64 ofst = (pgno-1)*(i64)pPager->pageSize;
44630    testcase( !isSavepnt && pPg!=0 && (pPg->flags&PGHDR_NEED_SYNC)!=0 );
44631    assert( !pagerUseWal(pPager) );
44632    rc = sqlite3OsWrite(pPager->fd, (u8 *)aData, pPager->pageSize, ofst);
44633    if( pgno>pPager->dbFileSize ){
44634      pPager->dbFileSize = pgno;
44635    }
44636    if( pPager->pBackup ){
44637      CODEC1(pPager, aData, pgno, 3, rc=SQLITE_NOMEM);
44638      sqlite3BackupUpdate(pPager->pBackup, pgno, (u8*)aData);
44639      CODEC2(pPager, aData, pgno, 7, rc=SQLITE_NOMEM, aData);
44640    }
44641  }else if( !isMainJrnl && pPg==0 ){
44642    /* If this is a rollback of a savepoint and data was not written to
44643    ** the database and the page is not in-memory, there is a potential
44644    ** problem. When the page is next fetched by the b-tree layer, it
44645    ** will be read from the database file, which may or may not be
44646    ** current.
44647    **
44648    ** There are a couple of different ways this can happen. All are quite
44649    ** obscure. When running in synchronous mode, this can only happen
44650    ** if the page is on the free-list at the start of the transaction, then
44651    ** populated, then moved using sqlite3PagerMovepage().
44652    **
44653    ** The solution is to add an in-memory page to the cache containing
44654    ** the data just read from the sub-journal. Mark the page as dirty
44655    ** and if the pager requires a journal-sync, then mark the page as
44656    ** requiring a journal-sync before it is written.
44657    */
44658    assert( isSavepnt );
44659    assert( (pPager->doNotSpill & SPILLFLAG_ROLLBACK)==0 );
44660    pPager->doNotSpill |= SPILLFLAG_ROLLBACK;
44661    rc = sqlite3PagerAcquire(pPager, pgno, &pPg, 1);
44662    assert( (pPager->doNotSpill & SPILLFLAG_ROLLBACK)!=0 );
44663    pPager->doNotSpill &= ~SPILLFLAG_ROLLBACK;
44664    if( rc!=SQLITE_OK ) return rc;
44665    pPg->flags &= ~PGHDR_NEED_READ;
44666    sqlite3PcacheMakeDirty(pPg);
44667  }
44668  if( pPg ){
44669    /* No page should ever be explicitly rolled back that is in use, except
44670    ** for page 1 which is held in use in order to keep the lock on the
44671    ** database active. However such a page may be rolled back as a result
44672    ** of an internal error resulting in an automatic call to
44673    ** sqlite3PagerRollback().
44674    */
44675    void *pData;
44676    pData = pPg->pData;
44677    memcpy(pData, (u8*)aData, pPager->pageSize);
44678    pPager->xReiniter(pPg);
44679    if( isMainJrnl && (!isSavepnt || *pOffset<=pPager->journalHdr) ){
44680      /* If the contents of this page were just restored from the main
44681      ** journal file, then its content must be as they were when the
44682      ** transaction was first opened. In this case we can mark the page
44683      ** as clean, since there will be no need to write it out to the
44684      ** database.
44685      **
44686      ** There is one exception to this rule. If the page is being rolled
44687      ** back as part of a savepoint (or statement) rollback from an
44688      ** unsynced portion of the main journal file, then it is not safe
44689      ** to mark the page as clean. This is because marking the page as
44690      ** clean will clear the PGHDR_NEED_SYNC flag. Since the page is
44691      ** already in the journal file (recorded in Pager.pInJournal) and
44692      ** the PGHDR_NEED_SYNC flag is cleared, if the page is written to
44693      ** again within this transaction, it will be marked as dirty but
44694      ** the PGHDR_NEED_SYNC flag will not be set. It could then potentially
44695      ** be written out into the database file before its journal file
44696      ** segment is synced. If a crash occurs during or following this,
44697      ** database corruption may ensue.
44698      */
44699      assert( !pagerUseWal(pPager) );
44700      sqlite3PcacheMakeClean(pPg);
44701    }
44702    pager_set_pagehash(pPg);
44703
44704    /* If this was page 1, then restore the value of Pager.dbFileVers.
44705    ** Do this before any decoding. */
44706    if( pgno==1 ){
44707      memcpy(&pPager->dbFileVers, &((u8*)pData)[24],sizeof(pPager->dbFileVers));
44708    }
44709
44710    /* Decode the page just read from disk */
44711    CODEC1(pPager, pData, pPg->pgno, 3, rc=SQLITE_NOMEM);
44712    sqlite3PcacheRelease(pPg);
44713  }
44714  return rc;
44715}
44716
44717/*
44718** Parameter zMaster is the name of a master journal file. A single journal
44719** file that referred to the master journal file has just been rolled back.
44720** This routine checks if it is possible to delete the master journal file,
44721** and does so if it is.
44722**
44723** Argument zMaster may point to Pager.pTmpSpace. So that buffer is not
44724** available for use within this function.
44725**
44726** When a master journal file is created, it is populated with the names
44727** of all of its child journals, one after another, formatted as utf-8
44728** encoded text. The end of each child journal file is marked with a
44729** nul-terminator byte (0x00). i.e. the entire contents of a master journal
44730** file for a transaction involving two databases might be:
44731**
44732**   "/home/bill/a.db-journal\x00/home/bill/b.db-journal\x00"
44733**
44734** A master journal file may only be deleted once all of its child
44735** journals have been rolled back.
44736**
44737** This function reads the contents of the master-journal file into
44738** memory and loops through each of the child journal names. For
44739** each child journal, it checks if:
44740**
44741**   * if the child journal exists, and if so
44742**   * if the child journal contains a reference to master journal
44743**     file zMaster
44744**
44745** If a child journal can be found that matches both of the criteria
44746** above, this function returns without doing anything. Otherwise, if
44747** no such child journal can be found, file zMaster is deleted from
44748** the file-system using sqlite3OsDelete().
44749**
44750** If an IO error within this function, an error code is returned. This
44751** function allocates memory by calling sqlite3Malloc(). If an allocation
44752** fails, SQLITE_NOMEM is returned. Otherwise, if no IO or malloc errors
44753** occur, SQLITE_OK is returned.
44754**
44755** TODO: This function allocates a single block of memory to load
44756** the entire contents of the master journal file. This could be
44757** a couple of kilobytes or so - potentially larger than the page
44758** size.
44759*/
44760static int pager_delmaster(Pager *pPager, const char *zMaster){
44761  sqlite3_vfs *pVfs = pPager->pVfs;
44762  int rc;                   /* Return code */
44763  sqlite3_file *pMaster;    /* Malloc'd master-journal file descriptor */
44764  sqlite3_file *pJournal;   /* Malloc'd child-journal file descriptor */
44765  char *zMasterJournal = 0; /* Contents of master journal file */
44766  i64 nMasterJournal;       /* Size of master journal file */
44767  char *zJournal;           /* Pointer to one journal within MJ file */
44768  char *zMasterPtr;         /* Space to hold MJ filename from a journal file */
44769  int nMasterPtr;           /* Amount of space allocated to zMasterPtr[] */
44770
44771  /* Allocate space for both the pJournal and pMaster file descriptors.
44772  ** If successful, open the master journal file for reading.
44773  */
44774  pMaster = (sqlite3_file *)sqlite3MallocZero(pVfs->szOsFile * 2);
44775  pJournal = (sqlite3_file *)(((u8 *)pMaster) + pVfs->szOsFile);
44776  if( !pMaster ){
44777    rc = SQLITE_NOMEM;
44778  }else{
44779    const int flags = (SQLITE_OPEN_READONLY|SQLITE_OPEN_MASTER_JOURNAL);
44780    rc = sqlite3OsOpen(pVfs, zMaster, pMaster, flags, 0);
44781  }
44782  if( rc!=SQLITE_OK ) goto delmaster_out;
44783
44784  /* Load the entire master journal file into space obtained from
44785  ** sqlite3_malloc() and pointed to by zMasterJournal.   Also obtain
44786  ** sufficient space (in zMasterPtr) to hold the names of master
44787  ** journal files extracted from regular rollback-journals.
44788  */
44789  rc = sqlite3OsFileSize(pMaster, &nMasterJournal);
44790  if( rc!=SQLITE_OK ) goto delmaster_out;
44791  nMasterPtr = pVfs->mxPathname+1;
44792  zMasterJournal = sqlite3Malloc(nMasterJournal + nMasterPtr + 1);
44793  if( !zMasterJournal ){
44794    rc = SQLITE_NOMEM;
44795    goto delmaster_out;
44796  }
44797  zMasterPtr = &zMasterJournal[nMasterJournal+1];
44798  rc = sqlite3OsRead(pMaster, zMasterJournal, (int)nMasterJournal, 0);
44799  if( rc!=SQLITE_OK ) goto delmaster_out;
44800  zMasterJournal[nMasterJournal] = 0;
44801
44802  zJournal = zMasterJournal;
44803  while( (zJournal-zMasterJournal)<nMasterJournal ){
44804    int exists;
44805    rc = sqlite3OsAccess(pVfs, zJournal, SQLITE_ACCESS_EXISTS, &exists);
44806    if( rc!=SQLITE_OK ){
44807      goto delmaster_out;
44808    }
44809    if( exists ){
44810      /* One of the journals pointed to by the master journal exists.
44811      ** Open it and check if it points at the master journal. If
44812      ** so, return without deleting the master journal file.
44813      */
44814      int c;
44815      int flags = (SQLITE_OPEN_READONLY|SQLITE_OPEN_MAIN_JOURNAL);
44816      rc = sqlite3OsOpen(pVfs, zJournal, pJournal, flags, 0);
44817      if( rc!=SQLITE_OK ){
44818        goto delmaster_out;
44819      }
44820
44821      rc = readMasterJournal(pJournal, zMasterPtr, nMasterPtr);
44822      sqlite3OsClose(pJournal);
44823      if( rc!=SQLITE_OK ){
44824        goto delmaster_out;
44825      }
44826
44827      c = zMasterPtr[0]!=0 && strcmp(zMasterPtr, zMaster)==0;
44828      if( c ){
44829        /* We have a match. Do not delete the master journal file. */
44830        goto delmaster_out;
44831      }
44832    }
44833    zJournal += (sqlite3Strlen30(zJournal)+1);
44834  }
44835
44836  sqlite3OsClose(pMaster);
44837  rc = sqlite3OsDelete(pVfs, zMaster, 0);
44838
44839delmaster_out:
44840  sqlite3_free(zMasterJournal);
44841  if( pMaster ){
44842    sqlite3OsClose(pMaster);
44843    assert( !isOpen(pJournal) );
44844    sqlite3_free(pMaster);
44845  }
44846  return rc;
44847}
44848
44849
44850/*
44851** This function is used to change the actual size of the database
44852** file in the file-system. This only happens when committing a transaction,
44853** or rolling back a transaction (including rolling back a hot-journal).
44854**
44855** If the main database file is not open, or the pager is not in either
44856** DBMOD or OPEN state, this function is a no-op. Otherwise, the size
44857** of the file is changed to nPage pages (nPage*pPager->pageSize bytes).
44858** If the file on disk is currently larger than nPage pages, then use the VFS
44859** xTruncate() method to truncate it.
44860**
44861** Or, it might be the case that the file on disk is smaller than
44862** nPage pages. Some operating system implementations can get confused if
44863** you try to truncate a file to some size that is larger than it
44864** currently is, so detect this case and write a single zero byte to
44865** the end of the new file instead.
44866**
44867** If successful, return SQLITE_OK. If an IO error occurs while modifying
44868** the database file, return the error code to the caller.
44869*/
44870static int pager_truncate(Pager *pPager, Pgno nPage){
44871  int rc = SQLITE_OK;
44872  assert( pPager->eState!=PAGER_ERROR );
44873  assert( pPager->eState!=PAGER_READER );
44874
44875  if( isOpen(pPager->fd)
44876   && (pPager->eState>=PAGER_WRITER_DBMOD || pPager->eState==PAGER_OPEN)
44877  ){
44878    i64 currentSize, newSize;
44879    int szPage = pPager->pageSize;
44880    assert( pPager->eLock==EXCLUSIVE_LOCK );
44881    /* TODO: Is it safe to use Pager.dbFileSize here? */
44882    rc = sqlite3OsFileSize(pPager->fd, &currentSize);
44883    newSize = szPage*(i64)nPage;
44884    if( rc==SQLITE_OK && currentSize!=newSize ){
44885      if( currentSize>newSize ){
44886        rc = sqlite3OsTruncate(pPager->fd, newSize);
44887      }else if( (currentSize+szPage)<=newSize ){
44888        char *pTmp = pPager->pTmpSpace;
44889        memset(pTmp, 0, szPage);
44890        testcase( (newSize-szPage) == currentSize );
44891        testcase( (newSize-szPage) >  currentSize );
44892        rc = sqlite3OsWrite(pPager->fd, pTmp, szPage, newSize-szPage);
44893      }
44894      if( rc==SQLITE_OK ){
44895        pPager->dbFileSize = nPage;
44896      }
44897    }
44898  }
44899  return rc;
44900}
44901
44902/*
44903** Return a sanitized version of the sector-size of OS file pFile. The
44904** return value is guaranteed to lie between 32 and MAX_SECTOR_SIZE.
44905*/
44906SQLITE_PRIVATE int sqlite3SectorSize(sqlite3_file *pFile){
44907  int iRet = sqlite3OsSectorSize(pFile);
44908  if( iRet<32 ){
44909    iRet = 512;
44910  }else if( iRet>MAX_SECTOR_SIZE ){
44911    assert( MAX_SECTOR_SIZE>=512 );
44912    iRet = MAX_SECTOR_SIZE;
44913  }
44914  return iRet;
44915}
44916
44917/*
44918** Set the value of the Pager.sectorSize variable for the given
44919** pager based on the value returned by the xSectorSize method
44920** of the open database file. The sector size will be used
44921** to determine the size and alignment of journal header and
44922** master journal pointers within created journal files.
44923**
44924** For temporary files the effective sector size is always 512 bytes.
44925**
44926** Otherwise, for non-temporary files, the effective sector size is
44927** the value returned by the xSectorSize() method rounded up to 32 if
44928** it is less than 32, or rounded down to MAX_SECTOR_SIZE if it
44929** is greater than MAX_SECTOR_SIZE.
44930**
44931** If the file has the SQLITE_IOCAP_POWERSAFE_OVERWRITE property, then set
44932** the effective sector size to its minimum value (512).  The purpose of
44933** pPager->sectorSize is to define the "blast radius" of bytes that
44934** might change if a crash occurs while writing to a single byte in
44935** that range.  But with POWERSAFE_OVERWRITE, the blast radius is zero
44936** (that is what POWERSAFE_OVERWRITE means), so we minimize the sector
44937** size.  For backwards compatibility of the rollback journal file format,
44938** we cannot reduce the effective sector size below 512.
44939*/
44940static void setSectorSize(Pager *pPager){
44941  assert( isOpen(pPager->fd) || pPager->tempFile );
44942
44943  if( pPager->tempFile
44944   || (sqlite3OsDeviceCharacteristics(pPager->fd) &
44945              SQLITE_IOCAP_POWERSAFE_OVERWRITE)!=0
44946  ){
44947    /* Sector size doesn't matter for temporary files. Also, the file
44948    ** may not have been opened yet, in which case the OsSectorSize()
44949    ** call will segfault. */
44950    pPager->sectorSize = 512;
44951  }else{
44952    pPager->sectorSize = sqlite3SectorSize(pPager->fd);
44953  }
44954}
44955
44956/*
44957** Playback the journal and thus restore the database file to
44958** the state it was in before we started making changes.
44959**
44960** The journal file format is as follows:
44961**
44962**  (1)  8 byte prefix.  A copy of aJournalMagic[].
44963**  (2)  4 byte big-endian integer which is the number of valid page records
44964**       in the journal.  If this value is 0xffffffff, then compute the
44965**       number of page records from the journal size.
44966**  (3)  4 byte big-endian integer which is the initial value for the
44967**       sanity checksum.
44968**  (4)  4 byte integer which is the number of pages to truncate the
44969**       database to during a rollback.
44970**  (5)  4 byte big-endian integer which is the sector size.  The header
44971**       is this many bytes in size.
44972**  (6)  4 byte big-endian integer which is the page size.
44973**  (7)  zero padding out to the next sector size.
44974**  (8)  Zero or more pages instances, each as follows:
44975**        +  4 byte page number.
44976**        +  pPager->pageSize bytes of data.
44977**        +  4 byte checksum
44978**
44979** When we speak of the journal header, we mean the first 7 items above.
44980** Each entry in the journal is an instance of the 8th item.
44981**
44982** Call the value from the second bullet "nRec".  nRec is the number of
44983** valid page entries in the journal.  In most cases, you can compute the
44984** value of nRec from the size of the journal file.  But if a power
44985** failure occurred while the journal was being written, it could be the
44986** case that the size of the journal file had already been increased but
44987** the extra entries had not yet made it safely to disk.  In such a case,
44988** the value of nRec computed from the file size would be too large.  For
44989** that reason, we always use the nRec value in the header.
44990**
44991** If the nRec value is 0xffffffff it means that nRec should be computed
44992** from the file size.  This value is used when the user selects the
44993** no-sync option for the journal.  A power failure could lead to corruption
44994** in this case.  But for things like temporary table (which will be
44995** deleted when the power is restored) we don't care.
44996**
44997** If the file opened as the journal file is not a well-formed
44998** journal file then all pages up to the first corrupted page are rolled
44999** back (or no pages if the journal header is corrupted). The journal file
45000** is then deleted and SQLITE_OK returned, just as if no corruption had
45001** been encountered.
45002**
45003** If an I/O or malloc() error occurs, the journal-file is not deleted
45004** and an error code is returned.
45005**
45006** The isHot parameter indicates that we are trying to rollback a journal
45007** that might be a hot journal.  Or, it could be that the journal is
45008** preserved because of JOURNALMODE_PERSIST or JOURNALMODE_TRUNCATE.
45009** If the journal really is hot, reset the pager cache prior rolling
45010** back any content.  If the journal is merely persistent, no reset is
45011** needed.
45012*/
45013static int pager_playback(Pager *pPager, int isHot){
45014  sqlite3_vfs *pVfs = pPager->pVfs;
45015  i64 szJ;                 /* Size of the journal file in bytes */
45016  u32 nRec;                /* Number of Records in the journal */
45017  u32 u;                   /* Unsigned loop counter */
45018  Pgno mxPg = 0;           /* Size of the original file in pages */
45019  int rc;                  /* Result code of a subroutine */
45020  int res = 1;             /* Value returned by sqlite3OsAccess() */
45021  char *zMaster = 0;       /* Name of master journal file if any */
45022  int needPagerReset;      /* True to reset page prior to first page rollback */
45023  int nPlayback = 0;       /* Total number of pages restored from journal */
45024
45025  /* Figure out how many records are in the journal.  Abort early if
45026  ** the journal is empty.
45027  */
45028  assert( isOpen(pPager->jfd) );
45029  rc = sqlite3OsFileSize(pPager->jfd, &szJ);
45030  if( rc!=SQLITE_OK ){
45031    goto end_playback;
45032  }
45033
45034  /* Read the master journal name from the journal, if it is present.
45035  ** If a master journal file name is specified, but the file is not
45036  ** present on disk, then the journal is not hot and does not need to be
45037  ** played back.
45038  **
45039  ** TODO: Technically the following is an error because it assumes that
45040  ** buffer Pager.pTmpSpace is (mxPathname+1) bytes or larger. i.e. that
45041  ** (pPager->pageSize >= pPager->pVfs->mxPathname+1). Using os_unix.c,
45042  **  mxPathname is 512, which is the same as the minimum allowable value
45043  ** for pageSize.
45044  */
45045  zMaster = pPager->pTmpSpace;
45046  rc = readMasterJournal(pPager->jfd, zMaster, pPager->pVfs->mxPathname+1);
45047  if( rc==SQLITE_OK && zMaster[0] ){
45048    rc = sqlite3OsAccess(pVfs, zMaster, SQLITE_ACCESS_EXISTS, &res);
45049  }
45050  zMaster = 0;
45051  if( rc!=SQLITE_OK || !res ){
45052    goto end_playback;
45053  }
45054  pPager->journalOff = 0;
45055  needPagerReset = isHot;
45056
45057  /* This loop terminates either when a readJournalHdr() or
45058  ** pager_playback_one_page() call returns SQLITE_DONE or an IO error
45059  ** occurs.
45060  */
45061  while( 1 ){
45062    /* Read the next journal header from the journal file.  If there are
45063    ** not enough bytes left in the journal file for a complete header, or
45064    ** it is corrupted, then a process must have failed while writing it.
45065    ** This indicates nothing more needs to be rolled back.
45066    */
45067    rc = readJournalHdr(pPager, isHot, szJ, &nRec, &mxPg);
45068    if( rc!=SQLITE_OK ){
45069      if( rc==SQLITE_DONE ){
45070        rc = SQLITE_OK;
45071      }
45072      goto end_playback;
45073    }
45074
45075    /* If nRec is 0xffffffff, then this journal was created by a process
45076    ** working in no-sync mode. This means that the rest of the journal
45077    ** file consists of pages, there are no more journal headers. Compute
45078    ** the value of nRec based on this assumption.
45079    */
45080    if( nRec==0xffffffff ){
45081      assert( pPager->journalOff==JOURNAL_HDR_SZ(pPager) );
45082      nRec = (int)((szJ - JOURNAL_HDR_SZ(pPager))/JOURNAL_PG_SZ(pPager));
45083    }
45084
45085    /* If nRec is 0 and this rollback is of a transaction created by this
45086    ** process and if this is the final header in the journal, then it means
45087    ** that this part of the journal was being filled but has not yet been
45088    ** synced to disk.  Compute the number of pages based on the remaining
45089    ** size of the file.
45090    **
45091    ** The third term of the test was added to fix ticket #2565.
45092    ** When rolling back a hot journal, nRec==0 always means that the next
45093    ** chunk of the journal contains zero pages to be rolled back.  But
45094    ** when doing a ROLLBACK and the nRec==0 chunk is the last chunk in
45095    ** the journal, it means that the journal might contain additional
45096    ** pages that need to be rolled back and that the number of pages
45097    ** should be computed based on the journal file size.
45098    */
45099    if( nRec==0 && !isHot &&
45100        pPager->journalHdr+JOURNAL_HDR_SZ(pPager)==pPager->journalOff ){
45101      nRec = (int)((szJ - pPager->journalOff) / JOURNAL_PG_SZ(pPager));
45102    }
45103
45104    /* If this is the first header read from the journal, truncate the
45105    ** database file back to its original size.
45106    */
45107    if( pPager->journalOff==JOURNAL_HDR_SZ(pPager) ){
45108      rc = pager_truncate(pPager, mxPg);
45109      if( rc!=SQLITE_OK ){
45110        goto end_playback;
45111      }
45112      pPager->dbSize = mxPg;
45113    }
45114
45115    /* Copy original pages out of the journal and back into the
45116    ** database file and/or page cache.
45117    */
45118    for(u=0; u<nRec; u++){
45119      if( needPagerReset ){
45120        pager_reset(pPager);
45121        needPagerReset = 0;
45122      }
45123      rc = pager_playback_one_page(pPager,&pPager->journalOff,0,1,0);
45124      if( rc==SQLITE_OK ){
45125        nPlayback++;
45126      }else{
45127        if( rc==SQLITE_DONE ){
45128          pPager->journalOff = szJ;
45129          break;
45130        }else if( rc==SQLITE_IOERR_SHORT_READ ){
45131          /* If the journal has been truncated, simply stop reading and
45132          ** processing the journal. This might happen if the journal was
45133          ** not completely written and synced prior to a crash.  In that
45134          ** case, the database should have never been written in the
45135          ** first place so it is OK to simply abandon the rollback. */
45136          rc = SQLITE_OK;
45137          goto end_playback;
45138        }else{
45139          /* If we are unable to rollback, quit and return the error
45140          ** code.  This will cause the pager to enter the error state
45141          ** so that no further harm will be done.  Perhaps the next
45142          ** process to come along will be able to rollback the database.
45143          */
45144          goto end_playback;
45145        }
45146      }
45147    }
45148  }
45149  /*NOTREACHED*/
45150  assert( 0 );
45151
45152end_playback:
45153  /* Following a rollback, the database file should be back in its original
45154  ** state prior to the start of the transaction, so invoke the
45155  ** SQLITE_FCNTL_DB_UNCHANGED file-control method to disable the
45156  ** assertion that the transaction counter was modified.
45157  */
45158#ifdef SQLITE_DEBUG
45159  if( pPager->fd->pMethods ){
45160    sqlite3OsFileControlHint(pPager->fd,SQLITE_FCNTL_DB_UNCHANGED,0);
45161  }
45162#endif
45163
45164  /* If this playback is happening automatically as a result of an IO or
45165  ** malloc error that occurred after the change-counter was updated but
45166  ** before the transaction was committed, then the change-counter
45167  ** modification may just have been reverted. If this happens in exclusive
45168  ** mode, then subsequent transactions performed by the connection will not
45169  ** update the change-counter at all. This may lead to cache inconsistency
45170  ** problems for other processes at some point in the future. So, just
45171  ** in case this has happened, clear the changeCountDone flag now.
45172  */
45173  pPager->changeCountDone = pPager->tempFile;
45174
45175  if( rc==SQLITE_OK ){
45176    zMaster = pPager->pTmpSpace;
45177    rc = readMasterJournal(pPager->jfd, zMaster, pPager->pVfs->mxPathname+1);
45178    testcase( rc!=SQLITE_OK );
45179  }
45180  if( rc==SQLITE_OK
45181   && (pPager->eState>=PAGER_WRITER_DBMOD || pPager->eState==PAGER_OPEN)
45182  ){
45183    rc = sqlite3PagerSync(pPager, 0);
45184  }
45185  if( rc==SQLITE_OK ){
45186    rc = pager_end_transaction(pPager, zMaster[0]!='\0', 0);
45187    testcase( rc!=SQLITE_OK );
45188  }
45189  if( rc==SQLITE_OK && zMaster[0] && res ){
45190    /* If there was a master journal and this routine will return success,
45191    ** see if it is possible to delete the master journal.
45192    */
45193    rc = pager_delmaster(pPager, zMaster);
45194    testcase( rc!=SQLITE_OK );
45195  }
45196  if( isHot && nPlayback ){
45197    sqlite3_log(SQLITE_NOTICE_RECOVER_ROLLBACK, "recovered %d pages from %s",
45198                nPlayback, pPager->zJournal);
45199  }
45200
45201  /* The Pager.sectorSize variable may have been updated while rolling
45202  ** back a journal created by a process with a different sector size
45203  ** value. Reset it to the correct value for this process.
45204  */
45205  setSectorSize(pPager);
45206  return rc;
45207}
45208
45209
45210/*
45211** Read the content for page pPg out of the database file and into
45212** pPg->pData. A shared lock or greater must be held on the database
45213** file before this function is called.
45214**
45215** If page 1 is read, then the value of Pager.dbFileVers[] is set to
45216** the value read from the database file.
45217**
45218** If an IO error occurs, then the IO error is returned to the caller.
45219** Otherwise, SQLITE_OK is returned.
45220*/
45221static int readDbPage(PgHdr *pPg, u32 iFrame){
45222  Pager *pPager = pPg->pPager; /* Pager object associated with page pPg */
45223  Pgno pgno = pPg->pgno;       /* Page number to read */
45224  int rc = SQLITE_OK;          /* Return code */
45225  int pgsz = pPager->pageSize; /* Number of bytes to read */
45226
45227  assert( pPager->eState>=PAGER_READER && !MEMDB );
45228  assert( isOpen(pPager->fd) );
45229
45230#ifndef SQLITE_OMIT_WAL
45231  if( iFrame ){
45232    /* Try to pull the page from the write-ahead log. */
45233    rc = sqlite3WalReadFrame(pPager->pWal, iFrame, pgsz, pPg->pData);
45234  }else
45235#endif
45236  {
45237    i64 iOffset = (pgno-1)*(i64)pPager->pageSize;
45238    rc = sqlite3OsRead(pPager->fd, pPg->pData, pgsz, iOffset);
45239    if( rc==SQLITE_IOERR_SHORT_READ ){
45240      rc = SQLITE_OK;
45241    }
45242  }
45243
45244  if( pgno==1 ){
45245    if( rc ){
45246      /* If the read is unsuccessful, set the dbFileVers[] to something
45247      ** that will never be a valid file version.  dbFileVers[] is a copy
45248      ** of bytes 24..39 of the database.  Bytes 28..31 should always be
45249      ** zero or the size of the database in page. Bytes 32..35 and 35..39
45250      ** should be page numbers which are never 0xffffffff.  So filling
45251      ** pPager->dbFileVers[] with all 0xff bytes should suffice.
45252      **
45253      ** For an encrypted database, the situation is more complex:  bytes
45254      ** 24..39 of the database are white noise.  But the probability of
45255      ** white noise equaling 16 bytes of 0xff is vanishingly small so
45256      ** we should still be ok.
45257      */
45258      memset(pPager->dbFileVers, 0xff, sizeof(pPager->dbFileVers));
45259    }else{
45260      u8 *dbFileVers = &((u8*)pPg->pData)[24];
45261      memcpy(&pPager->dbFileVers, dbFileVers, sizeof(pPager->dbFileVers));
45262    }
45263  }
45264  CODEC1(pPager, pPg->pData, pgno, 3, rc = SQLITE_NOMEM);
45265
45266  PAGER_INCR(sqlite3_pager_readdb_count);
45267  PAGER_INCR(pPager->nRead);
45268  IOTRACE(("PGIN %p %d\n", pPager, pgno));
45269  PAGERTRACE(("FETCH %d page %d hash(%08x)\n",
45270               PAGERID(pPager), pgno, pager_pagehash(pPg)));
45271
45272  return rc;
45273}
45274
45275/*
45276** Update the value of the change-counter at offsets 24 and 92 in
45277** the header and the sqlite version number at offset 96.
45278**
45279** This is an unconditional update.  See also the pager_incr_changecounter()
45280** routine which only updates the change-counter if the update is actually
45281** needed, as determined by the pPager->changeCountDone state variable.
45282*/
45283static void pager_write_changecounter(PgHdr *pPg){
45284  u32 change_counter;
45285
45286  /* Increment the value just read and write it back to byte 24. */
45287  change_counter = sqlite3Get4byte((u8*)pPg->pPager->dbFileVers)+1;
45288  put32bits(((char*)pPg->pData)+24, change_counter);
45289
45290  /* Also store the SQLite version number in bytes 96..99 and in
45291  ** bytes 92..95 store the change counter for which the version number
45292  ** is valid. */
45293  put32bits(((char*)pPg->pData)+92, change_counter);
45294  put32bits(((char*)pPg->pData)+96, SQLITE_VERSION_NUMBER);
45295}
45296
45297#ifndef SQLITE_OMIT_WAL
45298/*
45299** This function is invoked once for each page that has already been
45300** written into the log file when a WAL transaction is rolled back.
45301** Parameter iPg is the page number of said page. The pCtx argument
45302** is actually a pointer to the Pager structure.
45303**
45304** If page iPg is present in the cache, and has no outstanding references,
45305** it is discarded. Otherwise, if there are one or more outstanding
45306** references, the page content is reloaded from the database. If the
45307** attempt to reload content from the database is required and fails,
45308** return an SQLite error code. Otherwise, SQLITE_OK.
45309*/
45310static int pagerUndoCallback(void *pCtx, Pgno iPg){
45311  int rc = SQLITE_OK;
45312  Pager *pPager = (Pager *)pCtx;
45313  PgHdr *pPg;
45314
45315  assert( pagerUseWal(pPager) );
45316  pPg = sqlite3PagerLookup(pPager, iPg);
45317  if( pPg ){
45318    if( sqlite3PcachePageRefcount(pPg)==1 ){
45319      sqlite3PcacheDrop(pPg);
45320    }else{
45321      u32 iFrame = 0;
45322      rc = sqlite3WalFindFrame(pPager->pWal, pPg->pgno, &iFrame);
45323      if( rc==SQLITE_OK ){
45324        rc = readDbPage(pPg, iFrame);
45325      }
45326      if( rc==SQLITE_OK ){
45327        pPager->xReiniter(pPg);
45328      }
45329      sqlite3PagerUnrefNotNull(pPg);
45330    }
45331  }
45332
45333  /* Normally, if a transaction is rolled back, any backup processes are
45334  ** updated as data is copied out of the rollback journal and into the
45335  ** database. This is not generally possible with a WAL database, as
45336  ** rollback involves simply truncating the log file. Therefore, if one
45337  ** or more frames have already been written to the log (and therefore
45338  ** also copied into the backup databases) as part of this transaction,
45339  ** the backups must be restarted.
45340  */
45341  sqlite3BackupRestart(pPager->pBackup);
45342
45343  return rc;
45344}
45345
45346/*
45347** This function is called to rollback a transaction on a WAL database.
45348*/
45349static int pagerRollbackWal(Pager *pPager){
45350  int rc;                         /* Return Code */
45351  PgHdr *pList;                   /* List of dirty pages to revert */
45352
45353  /* For all pages in the cache that are currently dirty or have already
45354  ** been written (but not committed) to the log file, do one of the
45355  ** following:
45356  **
45357  **   + Discard the cached page (if refcount==0), or
45358  **   + Reload page content from the database (if refcount>0).
45359  */
45360  pPager->dbSize = pPager->dbOrigSize;
45361  rc = sqlite3WalUndo(pPager->pWal, pagerUndoCallback, (void *)pPager);
45362  pList = sqlite3PcacheDirtyList(pPager->pPCache);
45363  while( pList && rc==SQLITE_OK ){
45364    PgHdr *pNext = pList->pDirty;
45365    rc = pagerUndoCallback((void *)pPager, pList->pgno);
45366    pList = pNext;
45367  }
45368
45369  return rc;
45370}
45371
45372/*
45373** This function is a wrapper around sqlite3WalFrames(). As well as logging
45374** the contents of the list of pages headed by pList (connected by pDirty),
45375** this function notifies any active backup processes that the pages have
45376** changed.
45377**
45378** The list of pages passed into this routine is always sorted by page number.
45379** Hence, if page 1 appears anywhere on the list, it will be the first page.
45380*/
45381static int pagerWalFrames(
45382  Pager *pPager,                  /* Pager object */
45383  PgHdr *pList,                   /* List of frames to log */
45384  Pgno nTruncate,                 /* Database size after this commit */
45385  int isCommit                    /* True if this is a commit */
45386){
45387  int rc;                         /* Return code */
45388  int nList;                      /* Number of pages in pList */
45389  PgHdr *p;                       /* For looping over pages */
45390
45391  assert( pPager->pWal );
45392  assert( pList );
45393#ifdef SQLITE_DEBUG
45394  /* Verify that the page list is in accending order */
45395  for(p=pList; p && p->pDirty; p=p->pDirty){
45396    assert( p->pgno < p->pDirty->pgno );
45397  }
45398#endif
45399
45400  assert( pList->pDirty==0 || isCommit );
45401  if( isCommit ){
45402    /* If a WAL transaction is being committed, there is no point in writing
45403    ** any pages with page numbers greater than nTruncate into the WAL file.
45404    ** They will never be read by any client. So remove them from the pDirty
45405    ** list here. */
45406    PgHdr **ppNext = &pList;
45407    nList = 0;
45408    for(p=pList; (*ppNext = p)!=0; p=p->pDirty){
45409      if( p->pgno<=nTruncate ){
45410        ppNext = &p->pDirty;
45411        nList++;
45412      }
45413    }
45414    assert( pList );
45415  }else{
45416    nList = 1;
45417  }
45418  pPager->aStat[PAGER_STAT_WRITE] += nList;
45419
45420  if( pList->pgno==1 ) pager_write_changecounter(pList);
45421  rc = sqlite3WalFrames(pPager->pWal,
45422      pPager->pageSize, pList, nTruncate, isCommit, pPager->walSyncFlags
45423  );
45424  if( rc==SQLITE_OK && pPager->pBackup ){
45425    for(p=pList; p; p=p->pDirty){
45426      sqlite3BackupUpdate(pPager->pBackup, p->pgno, (u8 *)p->pData);
45427    }
45428  }
45429
45430#ifdef SQLITE_CHECK_PAGES
45431  pList = sqlite3PcacheDirtyList(pPager->pPCache);
45432  for(p=pList; p; p=p->pDirty){
45433    pager_set_pagehash(p);
45434  }
45435#endif
45436
45437  return rc;
45438}
45439
45440/*
45441** Begin a read transaction on the WAL.
45442**
45443** This routine used to be called "pagerOpenSnapshot()" because it essentially
45444** makes a snapshot of the database at the current point in time and preserves
45445** that snapshot for use by the reader in spite of concurrently changes by
45446** other writers or checkpointers.
45447*/
45448static int pagerBeginReadTransaction(Pager *pPager){
45449  int rc;                         /* Return code */
45450  int changed = 0;                /* True if cache must be reset */
45451
45452  assert( pagerUseWal(pPager) );
45453  assert( pPager->eState==PAGER_OPEN || pPager->eState==PAGER_READER );
45454
45455  /* sqlite3WalEndReadTransaction() was not called for the previous
45456  ** transaction in locking_mode=EXCLUSIVE.  So call it now.  If we
45457  ** are in locking_mode=NORMAL and EndRead() was previously called,
45458  ** the duplicate call is harmless.
45459  */
45460  sqlite3WalEndReadTransaction(pPager->pWal);
45461
45462  rc = sqlite3WalBeginReadTransaction(pPager->pWal, &changed);
45463  if( rc!=SQLITE_OK || changed ){
45464    pager_reset(pPager);
45465    if( USEFETCH(pPager) ) sqlite3OsUnfetch(pPager->fd, 0, 0);
45466  }
45467
45468  return rc;
45469}
45470#endif
45471
45472/*
45473** This function is called as part of the transition from PAGER_OPEN
45474** to PAGER_READER state to determine the size of the database file
45475** in pages (assuming the page size currently stored in Pager.pageSize).
45476**
45477** If no error occurs, SQLITE_OK is returned and the size of the database
45478** in pages is stored in *pnPage. Otherwise, an error code (perhaps
45479** SQLITE_IOERR_FSTAT) is returned and *pnPage is left unmodified.
45480*/
45481static int pagerPagecount(Pager *pPager, Pgno *pnPage){
45482  Pgno nPage;                     /* Value to return via *pnPage */
45483
45484  /* Query the WAL sub-system for the database size. The WalDbsize()
45485  ** function returns zero if the WAL is not open (i.e. Pager.pWal==0), or
45486  ** if the database size is not available. The database size is not
45487  ** available from the WAL sub-system if the log file is empty or
45488  ** contains no valid committed transactions.
45489  */
45490  assert( pPager->eState==PAGER_OPEN );
45491  assert( pPager->eLock>=SHARED_LOCK );
45492  nPage = sqlite3WalDbsize(pPager->pWal);
45493
45494  /* If the number of pages in the database is not available from the
45495  ** WAL sub-system, determine the page counte based on the size of
45496  ** the database file.  If the size of the database file is not an
45497  ** integer multiple of the page-size, round up the result.
45498  */
45499  if( nPage==0 ){
45500    i64 n = 0;                    /* Size of db file in bytes */
45501    assert( isOpen(pPager->fd) || pPager->tempFile );
45502    if( isOpen(pPager->fd) ){
45503      int rc = sqlite3OsFileSize(pPager->fd, &n);
45504      if( rc!=SQLITE_OK ){
45505        return rc;
45506      }
45507    }
45508    nPage = (Pgno)((n+pPager->pageSize-1) / pPager->pageSize);
45509  }
45510
45511  /* If the current number of pages in the file is greater than the
45512  ** configured maximum pager number, increase the allowed limit so
45513  ** that the file can be read.
45514  */
45515  if( nPage>pPager->mxPgno ){
45516    pPager->mxPgno = (Pgno)nPage;
45517  }
45518
45519  *pnPage = nPage;
45520  return SQLITE_OK;
45521}
45522
45523#ifndef SQLITE_OMIT_WAL
45524/*
45525** Check if the *-wal file that corresponds to the database opened by pPager
45526** exists if the database is not empy, or verify that the *-wal file does
45527** not exist (by deleting it) if the database file is empty.
45528**
45529** If the database is not empty and the *-wal file exists, open the pager
45530** in WAL mode.  If the database is empty or if no *-wal file exists and
45531** if no error occurs, make sure Pager.journalMode is not set to
45532** PAGER_JOURNALMODE_WAL.
45533**
45534** Return SQLITE_OK or an error code.
45535**
45536** The caller must hold a SHARED lock on the database file to call this
45537** function. Because an EXCLUSIVE lock on the db file is required to delete
45538** a WAL on a none-empty database, this ensures there is no race condition
45539** between the xAccess() below and an xDelete() being executed by some
45540** other connection.
45541*/
45542static int pagerOpenWalIfPresent(Pager *pPager){
45543  int rc = SQLITE_OK;
45544  assert( pPager->eState==PAGER_OPEN );
45545  assert( pPager->eLock>=SHARED_LOCK );
45546
45547  if( !pPager->tempFile ){
45548    int isWal;                    /* True if WAL file exists */
45549    Pgno nPage;                   /* Size of the database file */
45550
45551    rc = pagerPagecount(pPager, &nPage);
45552    if( rc ) return rc;
45553    if( nPage==0 ){
45554      rc = sqlite3OsDelete(pPager->pVfs, pPager->zWal, 0);
45555      if( rc==SQLITE_IOERR_DELETE_NOENT ) rc = SQLITE_OK;
45556      isWal = 0;
45557    }else{
45558      rc = sqlite3OsAccess(
45559          pPager->pVfs, pPager->zWal, SQLITE_ACCESS_EXISTS, &isWal
45560      );
45561    }
45562    if( rc==SQLITE_OK ){
45563      if( isWal ){
45564        testcase( sqlite3PcachePagecount(pPager->pPCache)==0 );
45565        rc = sqlite3PagerOpenWal(pPager, 0);
45566      }else if( pPager->journalMode==PAGER_JOURNALMODE_WAL ){
45567        pPager->journalMode = PAGER_JOURNALMODE_DELETE;
45568      }
45569    }
45570  }
45571  return rc;
45572}
45573#endif
45574
45575/*
45576** Playback savepoint pSavepoint. Or, if pSavepoint==NULL, then playback
45577** the entire master journal file. The case pSavepoint==NULL occurs when
45578** a ROLLBACK TO command is invoked on a SAVEPOINT that is a transaction
45579** savepoint.
45580**
45581** When pSavepoint is not NULL (meaning a non-transaction savepoint is
45582** being rolled back), then the rollback consists of up to three stages,
45583** performed in the order specified:
45584**
45585**   * Pages are played back from the main journal starting at byte
45586**     offset PagerSavepoint.iOffset and continuing to
45587**     PagerSavepoint.iHdrOffset, or to the end of the main journal
45588**     file if PagerSavepoint.iHdrOffset is zero.
45589**
45590**   * If PagerSavepoint.iHdrOffset is not zero, then pages are played
45591**     back starting from the journal header immediately following
45592**     PagerSavepoint.iHdrOffset to the end of the main journal file.
45593**
45594**   * Pages are then played back from the sub-journal file, starting
45595**     with the PagerSavepoint.iSubRec and continuing to the end of
45596**     the journal file.
45597**
45598** Throughout the rollback process, each time a page is rolled back, the
45599** corresponding bit is set in a bitvec structure (variable pDone in the
45600** implementation below). This is used to ensure that a page is only
45601** rolled back the first time it is encountered in either journal.
45602**
45603** If pSavepoint is NULL, then pages are only played back from the main
45604** journal file. There is no need for a bitvec in this case.
45605**
45606** In either case, before playback commences the Pager.dbSize variable
45607** is reset to the value that it held at the start of the savepoint
45608** (or transaction). No page with a page-number greater than this value
45609** is played back. If one is encountered it is simply skipped.
45610*/
45611static int pagerPlaybackSavepoint(Pager *pPager, PagerSavepoint *pSavepoint){
45612  i64 szJ;                 /* Effective size of the main journal */
45613  i64 iHdrOff;             /* End of first segment of main-journal records */
45614  int rc = SQLITE_OK;      /* Return code */
45615  Bitvec *pDone = 0;       /* Bitvec to ensure pages played back only once */
45616
45617  assert( pPager->eState!=PAGER_ERROR );
45618  assert( pPager->eState>=PAGER_WRITER_LOCKED );
45619
45620  /* Allocate a bitvec to use to store the set of pages rolled back */
45621  if( pSavepoint ){
45622    pDone = sqlite3BitvecCreate(pSavepoint->nOrig);
45623    if( !pDone ){
45624      return SQLITE_NOMEM;
45625    }
45626  }
45627
45628  /* Set the database size back to the value it was before the savepoint
45629  ** being reverted was opened.
45630  */
45631  pPager->dbSize = pSavepoint ? pSavepoint->nOrig : pPager->dbOrigSize;
45632  pPager->changeCountDone = pPager->tempFile;
45633
45634  if( !pSavepoint && pagerUseWal(pPager) ){
45635    return pagerRollbackWal(pPager);
45636  }
45637
45638  /* Use pPager->journalOff as the effective size of the main rollback
45639  ** journal.  The actual file might be larger than this in
45640  ** PAGER_JOURNALMODE_TRUNCATE or PAGER_JOURNALMODE_PERSIST.  But anything
45641  ** past pPager->journalOff is off-limits to us.
45642  */
45643  szJ = pPager->journalOff;
45644  assert( pagerUseWal(pPager)==0 || szJ==0 );
45645
45646  /* Begin by rolling back records from the main journal starting at
45647  ** PagerSavepoint.iOffset and continuing to the next journal header.
45648  ** There might be records in the main journal that have a page number
45649  ** greater than the current database size (pPager->dbSize) but those
45650  ** will be skipped automatically.  Pages are added to pDone as they
45651  ** are played back.
45652  */
45653  if( pSavepoint && !pagerUseWal(pPager) ){
45654    iHdrOff = pSavepoint->iHdrOffset ? pSavepoint->iHdrOffset : szJ;
45655    pPager->journalOff = pSavepoint->iOffset;
45656    while( rc==SQLITE_OK && pPager->journalOff<iHdrOff ){
45657      rc = pager_playback_one_page(pPager, &pPager->journalOff, pDone, 1, 1);
45658    }
45659    assert( rc!=SQLITE_DONE );
45660  }else{
45661    pPager->journalOff = 0;
45662  }
45663
45664  /* Continue rolling back records out of the main journal starting at
45665  ** the first journal header seen and continuing until the effective end
45666  ** of the main journal file.  Continue to skip out-of-range pages and
45667  ** continue adding pages rolled back to pDone.
45668  */
45669  while( rc==SQLITE_OK && pPager->journalOff<szJ ){
45670    u32 ii;            /* Loop counter */
45671    u32 nJRec = 0;     /* Number of Journal Records */
45672    u32 dummy;
45673    rc = readJournalHdr(pPager, 0, szJ, &nJRec, &dummy);
45674    assert( rc!=SQLITE_DONE );
45675
45676    /*
45677    ** The "pPager->journalHdr+JOURNAL_HDR_SZ(pPager)==pPager->journalOff"
45678    ** test is related to ticket #2565.  See the discussion in the
45679    ** pager_playback() function for additional information.
45680    */
45681    if( nJRec==0
45682     && pPager->journalHdr+JOURNAL_HDR_SZ(pPager)==pPager->journalOff
45683    ){
45684      nJRec = (u32)((szJ - pPager->journalOff)/JOURNAL_PG_SZ(pPager));
45685    }
45686    for(ii=0; rc==SQLITE_OK && ii<nJRec && pPager->journalOff<szJ; ii++){
45687      rc = pager_playback_one_page(pPager, &pPager->journalOff, pDone, 1, 1);
45688    }
45689    assert( rc!=SQLITE_DONE );
45690  }
45691  assert( rc!=SQLITE_OK || pPager->journalOff>=szJ );
45692
45693  /* Finally,  rollback pages from the sub-journal.  Page that were
45694  ** previously rolled back out of the main journal (and are hence in pDone)
45695  ** will be skipped.  Out-of-range pages are also skipped.
45696  */
45697  if( pSavepoint ){
45698    u32 ii;            /* Loop counter */
45699    i64 offset = (i64)pSavepoint->iSubRec*(4+pPager->pageSize);
45700
45701    if( pagerUseWal(pPager) ){
45702      rc = sqlite3WalSavepointUndo(pPager->pWal, pSavepoint->aWalData);
45703    }
45704    for(ii=pSavepoint->iSubRec; rc==SQLITE_OK && ii<pPager->nSubRec; ii++){
45705      assert( offset==(i64)ii*(4+pPager->pageSize) );
45706      rc = pager_playback_one_page(pPager, &offset, pDone, 0, 1);
45707    }
45708    assert( rc!=SQLITE_DONE );
45709  }
45710
45711  sqlite3BitvecDestroy(pDone);
45712  if( rc==SQLITE_OK ){
45713    pPager->journalOff = szJ;
45714  }
45715
45716  return rc;
45717}
45718
45719/*
45720** Change the maximum number of in-memory pages that are allowed.
45721*/
45722SQLITE_PRIVATE void sqlite3PagerSetCachesize(Pager *pPager, int mxPage){
45723  sqlite3PcacheSetCachesize(pPager->pPCache, mxPage);
45724}
45725
45726/*
45727** Invoke SQLITE_FCNTL_MMAP_SIZE based on the current value of szMmap.
45728*/
45729static void pagerFixMaplimit(Pager *pPager){
45730#if SQLITE_MAX_MMAP_SIZE>0
45731  sqlite3_file *fd = pPager->fd;
45732  if( isOpen(fd) && fd->pMethods->iVersion>=3 ){
45733    sqlite3_int64 sz;
45734    sz = pPager->szMmap;
45735    pPager->bUseFetch = (sz>0);
45736    sqlite3OsFileControlHint(pPager->fd, SQLITE_FCNTL_MMAP_SIZE, &sz);
45737  }
45738#endif
45739}
45740
45741/*
45742** Change the maximum size of any memory mapping made of the database file.
45743*/
45744SQLITE_PRIVATE void sqlite3PagerSetMmapLimit(Pager *pPager, sqlite3_int64 szMmap){
45745  pPager->szMmap = szMmap;
45746  pagerFixMaplimit(pPager);
45747}
45748
45749/*
45750** Free as much memory as possible from the pager.
45751*/
45752SQLITE_PRIVATE void sqlite3PagerShrink(Pager *pPager){
45753  sqlite3PcacheShrink(pPager->pPCache);
45754}
45755
45756/*
45757** Adjust settings of the pager to those specified in the pgFlags parameter.
45758**
45759** The "level" in pgFlags & PAGER_SYNCHRONOUS_MASK sets the robustness
45760** of the database to damage due to OS crashes or power failures by
45761** changing the number of syncs()s when writing the journals.
45762** There are three levels:
45763**
45764**    OFF       sqlite3OsSync() is never called.  This is the default
45765**              for temporary and transient files.
45766**
45767**    NORMAL    The journal is synced once before writes begin on the
45768**              database.  This is normally adequate protection, but
45769**              it is theoretically possible, though very unlikely,
45770**              that an inopertune power failure could leave the journal
45771**              in a state which would cause damage to the database
45772**              when it is rolled back.
45773**
45774**    FULL      The journal is synced twice before writes begin on the
45775**              database (with some additional information - the nRec field
45776**              of the journal header - being written in between the two
45777**              syncs).  If we assume that writing a
45778**              single disk sector is atomic, then this mode provides
45779**              assurance that the journal will not be corrupted to the
45780**              point of causing damage to the database during rollback.
45781**
45782** The above is for a rollback-journal mode.  For WAL mode, OFF continues
45783** to mean that no syncs ever occur.  NORMAL means that the WAL is synced
45784** prior to the start of checkpoint and that the database file is synced
45785** at the conclusion of the checkpoint if the entire content of the WAL
45786** was written back into the database.  But no sync operations occur for
45787** an ordinary commit in NORMAL mode with WAL.  FULL means that the WAL
45788** file is synced following each commit operation, in addition to the
45789** syncs associated with NORMAL.
45790**
45791** Do not confuse synchronous=FULL with SQLITE_SYNC_FULL.  The
45792** SQLITE_SYNC_FULL macro means to use the MacOSX-style full-fsync
45793** using fcntl(F_FULLFSYNC).  SQLITE_SYNC_NORMAL means to do an
45794** ordinary fsync() call.  There is no difference between SQLITE_SYNC_FULL
45795** and SQLITE_SYNC_NORMAL on platforms other than MacOSX.  But the
45796** synchronous=FULL versus synchronous=NORMAL setting determines when
45797** the xSync primitive is called and is relevant to all platforms.
45798**
45799** Numeric values associated with these states are OFF==1, NORMAL=2,
45800** and FULL=3.
45801*/
45802#ifndef SQLITE_OMIT_PAGER_PRAGMAS
45803SQLITE_PRIVATE void sqlite3PagerSetFlags(
45804  Pager *pPager,        /* The pager to set safety level for */
45805  unsigned pgFlags      /* Various flags */
45806){
45807  unsigned level = pgFlags & PAGER_SYNCHRONOUS_MASK;
45808  assert( level>=1 && level<=3 );
45809  pPager->noSync =  (level==1 || pPager->tempFile) ?1:0;
45810  pPager->fullSync = (level==3 && !pPager->tempFile) ?1:0;
45811  if( pPager->noSync ){
45812    pPager->syncFlags = 0;
45813    pPager->ckptSyncFlags = 0;
45814  }else if( pgFlags & PAGER_FULLFSYNC ){
45815    pPager->syncFlags = SQLITE_SYNC_FULL;
45816    pPager->ckptSyncFlags = SQLITE_SYNC_FULL;
45817  }else if( pgFlags & PAGER_CKPT_FULLFSYNC ){
45818    pPager->syncFlags = SQLITE_SYNC_NORMAL;
45819    pPager->ckptSyncFlags = SQLITE_SYNC_FULL;
45820  }else{
45821    pPager->syncFlags = SQLITE_SYNC_NORMAL;
45822    pPager->ckptSyncFlags = SQLITE_SYNC_NORMAL;
45823  }
45824  pPager->walSyncFlags = pPager->syncFlags;
45825  if( pPager->fullSync ){
45826    pPager->walSyncFlags |= WAL_SYNC_TRANSACTIONS;
45827  }
45828  if( pgFlags & PAGER_CACHESPILL ){
45829    pPager->doNotSpill &= ~SPILLFLAG_OFF;
45830  }else{
45831    pPager->doNotSpill |= SPILLFLAG_OFF;
45832  }
45833}
45834#endif
45835
45836/*
45837** The following global variable is incremented whenever the library
45838** attempts to open a temporary file.  This information is used for
45839** testing and analysis only.
45840*/
45841#ifdef SQLITE_TEST
45842SQLITE_API int sqlite3_opentemp_count = 0;
45843#endif
45844
45845/*
45846** Open a temporary file.
45847**
45848** Write the file descriptor into *pFile. Return SQLITE_OK on success
45849** or some other error code if we fail. The OS will automatically
45850** delete the temporary file when it is closed.
45851**
45852** The flags passed to the VFS layer xOpen() call are those specified
45853** by parameter vfsFlags ORed with the following:
45854**
45855**     SQLITE_OPEN_READWRITE
45856**     SQLITE_OPEN_CREATE
45857**     SQLITE_OPEN_EXCLUSIVE
45858**     SQLITE_OPEN_DELETEONCLOSE
45859*/
45860static int pagerOpentemp(
45861  Pager *pPager,        /* The pager object */
45862  sqlite3_file *pFile,  /* Write the file descriptor here */
45863  int vfsFlags          /* Flags passed through to the VFS */
45864){
45865  int rc;               /* Return code */
45866
45867#ifdef SQLITE_TEST
45868  sqlite3_opentemp_count++;  /* Used for testing and analysis only */
45869#endif
45870
45871  vfsFlags |=  SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE |
45872            SQLITE_OPEN_EXCLUSIVE | SQLITE_OPEN_DELETEONCLOSE;
45873  rc = sqlite3OsOpen(pPager->pVfs, 0, pFile, vfsFlags, 0);
45874  assert( rc!=SQLITE_OK || isOpen(pFile) );
45875  return rc;
45876}
45877
45878/*
45879** Set the busy handler function.
45880**
45881** The pager invokes the busy-handler if sqlite3OsLock() returns
45882** SQLITE_BUSY when trying to upgrade from no-lock to a SHARED lock,
45883** or when trying to upgrade from a RESERVED lock to an EXCLUSIVE
45884** lock. It does *not* invoke the busy handler when upgrading from
45885** SHARED to RESERVED, or when upgrading from SHARED to EXCLUSIVE
45886** (which occurs during hot-journal rollback). Summary:
45887**
45888**   Transition                        | Invokes xBusyHandler
45889**   --------------------------------------------------------
45890**   NO_LOCK       -> SHARED_LOCK      | Yes
45891**   SHARED_LOCK   -> RESERVED_LOCK    | No
45892**   SHARED_LOCK   -> EXCLUSIVE_LOCK   | No
45893**   RESERVED_LOCK -> EXCLUSIVE_LOCK   | Yes
45894**
45895** If the busy-handler callback returns non-zero, the lock is
45896** retried. If it returns zero, then the SQLITE_BUSY error is
45897** returned to the caller of the pager API function.
45898*/
45899SQLITE_PRIVATE void sqlite3PagerSetBusyhandler(
45900  Pager *pPager,                       /* Pager object */
45901  int (*xBusyHandler)(void *),         /* Pointer to busy-handler function */
45902  void *pBusyHandlerArg                /* Argument to pass to xBusyHandler */
45903){
45904  pPager->xBusyHandler = xBusyHandler;
45905  pPager->pBusyHandlerArg = pBusyHandlerArg;
45906
45907  if( isOpen(pPager->fd) ){
45908    void **ap = (void **)&pPager->xBusyHandler;
45909    assert( ((int(*)(void *))(ap[0]))==xBusyHandler );
45910    assert( ap[1]==pBusyHandlerArg );
45911    sqlite3OsFileControlHint(pPager->fd, SQLITE_FCNTL_BUSYHANDLER, (void *)ap);
45912  }
45913}
45914
45915/*
45916** Change the page size used by the Pager object. The new page size
45917** is passed in *pPageSize.
45918**
45919** If the pager is in the error state when this function is called, it
45920** is a no-op. The value returned is the error state error code (i.e.
45921** one of SQLITE_IOERR, an SQLITE_IOERR_xxx sub-code or SQLITE_FULL).
45922**
45923** Otherwise, if all of the following are true:
45924**
45925**   * the new page size (value of *pPageSize) is valid (a power
45926**     of two between 512 and SQLITE_MAX_PAGE_SIZE, inclusive), and
45927**
45928**   * there are no outstanding page references, and
45929**
45930**   * the database is either not an in-memory database or it is
45931**     an in-memory database that currently consists of zero pages.
45932**
45933** then the pager object page size is set to *pPageSize.
45934**
45935** If the page size is changed, then this function uses sqlite3PagerMalloc()
45936** to obtain a new Pager.pTmpSpace buffer. If this allocation attempt
45937** fails, SQLITE_NOMEM is returned and the page size remains unchanged.
45938** In all other cases, SQLITE_OK is returned.
45939**
45940** If the page size is not changed, either because one of the enumerated
45941** conditions above is not true, the pager was in error state when this
45942** function was called, or because the memory allocation attempt failed,
45943** then *pPageSize is set to the old, retained page size before returning.
45944*/
45945SQLITE_PRIVATE int sqlite3PagerSetPagesize(Pager *pPager, u32 *pPageSize, int nReserve){
45946  int rc = SQLITE_OK;
45947
45948  /* It is not possible to do a full assert_pager_state() here, as this
45949  ** function may be called from within PagerOpen(), before the state
45950  ** of the Pager object is internally consistent.
45951  **
45952  ** At one point this function returned an error if the pager was in
45953  ** PAGER_ERROR state. But since PAGER_ERROR state guarantees that
45954  ** there is at least one outstanding page reference, this function
45955  ** is a no-op for that case anyhow.
45956  */
45957
45958  u32 pageSize = *pPageSize;
45959  assert( pageSize==0 || (pageSize>=512 && pageSize<=SQLITE_MAX_PAGE_SIZE) );
45960  if( (pPager->memDb==0 || pPager->dbSize==0)
45961   && sqlite3PcacheRefCount(pPager->pPCache)==0
45962   && pageSize && pageSize!=(u32)pPager->pageSize
45963  ){
45964    char *pNew = NULL;             /* New temp space */
45965    i64 nByte = 0;
45966
45967    if( pPager->eState>PAGER_OPEN && isOpen(pPager->fd) ){
45968      rc = sqlite3OsFileSize(pPager->fd, &nByte);
45969    }
45970    if( rc==SQLITE_OK ){
45971      pNew = (char *)sqlite3PageMalloc(pageSize);
45972      if( !pNew ) rc = SQLITE_NOMEM;
45973    }
45974
45975    if( rc==SQLITE_OK ){
45976      pager_reset(pPager);
45977      rc = sqlite3PcacheSetPageSize(pPager->pPCache, pageSize);
45978    }
45979    if( rc==SQLITE_OK ){
45980      sqlite3PageFree(pPager->pTmpSpace);
45981      pPager->pTmpSpace = pNew;
45982      pPager->dbSize = (Pgno)((nByte+pageSize-1)/pageSize);
45983      pPager->pageSize = pageSize;
45984    }else{
45985      sqlite3PageFree(pNew);
45986    }
45987  }
45988
45989  *pPageSize = pPager->pageSize;
45990  if( rc==SQLITE_OK ){
45991    if( nReserve<0 ) nReserve = pPager->nReserve;
45992    assert( nReserve>=0 && nReserve<1000 );
45993    pPager->nReserve = (i16)nReserve;
45994    pagerReportSize(pPager);
45995    pagerFixMaplimit(pPager);
45996  }
45997  return rc;
45998}
45999
46000/*
46001** Return a pointer to the "temporary page" buffer held internally
46002** by the pager.  This is a buffer that is big enough to hold the
46003** entire content of a database page.  This buffer is used internally
46004** during rollback and will be overwritten whenever a rollback
46005** occurs.  But other modules are free to use it too, as long as
46006** no rollbacks are happening.
46007*/
46008SQLITE_PRIVATE void *sqlite3PagerTempSpace(Pager *pPager){
46009  return pPager->pTmpSpace;
46010}
46011
46012/*
46013** Attempt to set the maximum database page count if mxPage is positive.
46014** Make no changes if mxPage is zero or negative.  And never reduce the
46015** maximum page count below the current size of the database.
46016**
46017** Regardless of mxPage, return the current maximum page count.
46018*/
46019SQLITE_PRIVATE int sqlite3PagerMaxPageCount(Pager *pPager, int mxPage){
46020  if( mxPage>0 ){
46021    pPager->mxPgno = mxPage;
46022  }
46023  assert( pPager->eState!=PAGER_OPEN );      /* Called only by OP_MaxPgcnt */
46024  assert( pPager->mxPgno>=pPager->dbSize );  /* OP_MaxPgcnt enforces this */
46025  return pPager->mxPgno;
46026}
46027
46028/*
46029** The following set of routines are used to disable the simulated
46030** I/O error mechanism.  These routines are used to avoid simulated
46031** errors in places where we do not care about errors.
46032**
46033** Unless -DSQLITE_TEST=1 is used, these routines are all no-ops
46034** and generate no code.
46035*/
46036#ifdef SQLITE_TEST
46037SQLITE_API extern int sqlite3_io_error_pending;
46038SQLITE_API extern int sqlite3_io_error_hit;
46039static int saved_cnt;
46040void disable_simulated_io_errors(void){
46041  saved_cnt = sqlite3_io_error_pending;
46042  sqlite3_io_error_pending = -1;
46043}
46044void enable_simulated_io_errors(void){
46045  sqlite3_io_error_pending = saved_cnt;
46046}
46047#else
46048# define disable_simulated_io_errors()
46049# define enable_simulated_io_errors()
46050#endif
46051
46052/*
46053** Read the first N bytes from the beginning of the file into memory
46054** that pDest points to.
46055**
46056** If the pager was opened on a transient file (zFilename==""), or
46057** opened on a file less than N bytes in size, the output buffer is
46058** zeroed and SQLITE_OK returned. The rationale for this is that this
46059** function is used to read database headers, and a new transient or
46060** zero sized database has a header than consists entirely of zeroes.
46061**
46062** If any IO error apart from SQLITE_IOERR_SHORT_READ is encountered,
46063** the error code is returned to the caller and the contents of the
46064** output buffer undefined.
46065*/
46066SQLITE_PRIVATE int sqlite3PagerReadFileheader(Pager *pPager, int N, unsigned char *pDest){
46067  int rc = SQLITE_OK;
46068  memset(pDest, 0, N);
46069  assert( isOpen(pPager->fd) || pPager->tempFile );
46070
46071  /* This routine is only called by btree immediately after creating
46072  ** the Pager object.  There has not been an opportunity to transition
46073  ** to WAL mode yet.
46074  */
46075  assert( !pagerUseWal(pPager) );
46076
46077  if( isOpen(pPager->fd) ){
46078    IOTRACE(("DBHDR %p 0 %d\n", pPager, N))
46079    rc = sqlite3OsRead(pPager->fd, pDest, N, 0);
46080    if( rc==SQLITE_IOERR_SHORT_READ ){
46081      rc = SQLITE_OK;
46082    }
46083  }
46084  return rc;
46085}
46086
46087/*
46088** This function may only be called when a read-transaction is open on
46089** the pager. It returns the total number of pages in the database.
46090**
46091** However, if the file is between 1 and <page-size> bytes in size, then
46092** this is considered a 1 page file.
46093*/
46094SQLITE_PRIVATE void sqlite3PagerPagecount(Pager *pPager, int *pnPage){
46095  assert( pPager->eState>=PAGER_READER );
46096  assert( pPager->eState!=PAGER_WRITER_FINISHED );
46097  *pnPage = (int)pPager->dbSize;
46098}
46099
46100
46101/*
46102** Try to obtain a lock of type locktype on the database file. If
46103** a similar or greater lock is already held, this function is a no-op
46104** (returning SQLITE_OK immediately).
46105**
46106** Otherwise, attempt to obtain the lock using sqlite3OsLock(). Invoke
46107** the busy callback if the lock is currently not available. Repeat
46108** until the busy callback returns false or until the attempt to
46109** obtain the lock succeeds.
46110**
46111** Return SQLITE_OK on success and an error code if we cannot obtain
46112** the lock. If the lock is obtained successfully, set the Pager.state
46113** variable to locktype before returning.
46114*/
46115static int pager_wait_on_lock(Pager *pPager, int locktype){
46116  int rc;                              /* Return code */
46117
46118  /* Check that this is either a no-op (because the requested lock is
46119  ** already held), or one of the transitions that the busy-handler
46120  ** may be invoked during, according to the comment above
46121  ** sqlite3PagerSetBusyhandler().
46122  */
46123  assert( (pPager->eLock>=locktype)
46124       || (pPager->eLock==NO_LOCK && locktype==SHARED_LOCK)
46125       || (pPager->eLock==RESERVED_LOCK && locktype==EXCLUSIVE_LOCK)
46126  );
46127
46128  do {
46129    rc = pagerLockDb(pPager, locktype);
46130  }while( rc==SQLITE_BUSY && pPager->xBusyHandler(pPager->pBusyHandlerArg) );
46131  return rc;
46132}
46133
46134/*
46135** Function assertTruncateConstraint(pPager) checks that one of the
46136** following is true for all dirty pages currently in the page-cache:
46137**
46138**   a) The page number is less than or equal to the size of the
46139**      current database image, in pages, OR
46140**
46141**   b) if the page content were written at this time, it would not
46142**      be necessary to write the current content out to the sub-journal
46143**      (as determined by function subjRequiresPage()).
46144**
46145** If the condition asserted by this function were not true, and the
46146** dirty page were to be discarded from the cache via the pagerStress()
46147** routine, pagerStress() would not write the current page content to
46148** the database file. If a savepoint transaction were rolled back after
46149** this happened, the correct behavior would be to restore the current
46150** content of the page. However, since this content is not present in either
46151** the database file or the portion of the rollback journal and
46152** sub-journal rolled back the content could not be restored and the
46153** database image would become corrupt. It is therefore fortunate that
46154** this circumstance cannot arise.
46155*/
46156#if defined(SQLITE_DEBUG)
46157static void assertTruncateConstraintCb(PgHdr *pPg){
46158  assert( pPg->flags&PGHDR_DIRTY );
46159  assert( !subjRequiresPage(pPg) || pPg->pgno<=pPg->pPager->dbSize );
46160}
46161static void assertTruncateConstraint(Pager *pPager){
46162  sqlite3PcacheIterateDirty(pPager->pPCache, assertTruncateConstraintCb);
46163}
46164#else
46165# define assertTruncateConstraint(pPager)
46166#endif
46167
46168/*
46169** Truncate the in-memory database file image to nPage pages. This
46170** function does not actually modify the database file on disk. It
46171** just sets the internal state of the pager object so that the
46172** truncation will be done when the current transaction is committed.
46173**
46174** This function is only called right before committing a transaction.
46175** Once this function has been called, the transaction must either be
46176** rolled back or committed. It is not safe to call this function and
46177** then continue writing to the database.
46178*/
46179SQLITE_PRIVATE void sqlite3PagerTruncateImage(Pager *pPager, Pgno nPage){
46180  assert( pPager->dbSize>=nPage );
46181  assert( pPager->eState>=PAGER_WRITER_CACHEMOD );
46182  pPager->dbSize = nPage;
46183
46184  /* At one point the code here called assertTruncateConstraint() to
46185  ** ensure that all pages being truncated away by this operation are,
46186  ** if one or more savepoints are open, present in the savepoint
46187  ** journal so that they can be restored if the savepoint is rolled
46188  ** back. This is no longer necessary as this function is now only
46189  ** called right before committing a transaction. So although the
46190  ** Pager object may still have open savepoints (Pager.nSavepoint!=0),
46191  ** they cannot be rolled back. So the assertTruncateConstraint() call
46192  ** is no longer correct. */
46193}
46194
46195
46196/*
46197** This function is called before attempting a hot-journal rollback. It
46198** syncs the journal file to disk, then sets pPager->journalHdr to the
46199** size of the journal file so that the pager_playback() routine knows
46200** that the entire journal file has been synced.
46201**
46202** Syncing a hot-journal to disk before attempting to roll it back ensures
46203** that if a power-failure occurs during the rollback, the process that
46204** attempts rollback following system recovery sees the same journal
46205** content as this process.
46206**
46207** If everything goes as planned, SQLITE_OK is returned. Otherwise,
46208** an SQLite error code.
46209*/
46210static int pagerSyncHotJournal(Pager *pPager){
46211  int rc = SQLITE_OK;
46212  if( !pPager->noSync ){
46213    rc = sqlite3OsSync(pPager->jfd, SQLITE_SYNC_NORMAL);
46214  }
46215  if( rc==SQLITE_OK ){
46216    rc = sqlite3OsFileSize(pPager->jfd, &pPager->journalHdr);
46217  }
46218  return rc;
46219}
46220
46221/*
46222** Obtain a reference to a memory mapped page object for page number pgno.
46223** The new object will use the pointer pData, obtained from xFetch().
46224** If successful, set *ppPage to point to the new page reference
46225** and return SQLITE_OK. Otherwise, return an SQLite error code and set
46226** *ppPage to zero.
46227**
46228** Page references obtained by calling this function should be released
46229** by calling pagerReleaseMapPage().
46230*/
46231static int pagerAcquireMapPage(
46232  Pager *pPager,                  /* Pager object */
46233  Pgno pgno,                      /* Page number */
46234  void *pData,                    /* xFetch()'d data for this page */
46235  PgHdr **ppPage                  /* OUT: Acquired page object */
46236){
46237  PgHdr *p;                       /* Memory mapped page to return */
46238
46239  if( pPager->pMmapFreelist ){
46240    *ppPage = p = pPager->pMmapFreelist;
46241    pPager->pMmapFreelist = p->pDirty;
46242    p->pDirty = 0;
46243    memset(p->pExtra, 0, pPager->nExtra);
46244  }else{
46245    *ppPage = p = (PgHdr *)sqlite3MallocZero(sizeof(PgHdr) + pPager->nExtra);
46246    if( p==0 ){
46247      sqlite3OsUnfetch(pPager->fd, (i64)(pgno-1) * pPager->pageSize, pData);
46248      return SQLITE_NOMEM;
46249    }
46250    p->pExtra = (void *)&p[1];
46251    p->flags = PGHDR_MMAP;
46252    p->nRef = 1;
46253    p->pPager = pPager;
46254  }
46255
46256  assert( p->pExtra==(void *)&p[1] );
46257  assert( p->pPage==0 );
46258  assert( p->flags==PGHDR_MMAP );
46259  assert( p->pPager==pPager );
46260  assert( p->nRef==1 );
46261
46262  p->pgno = pgno;
46263  p->pData = pData;
46264  pPager->nMmapOut++;
46265
46266  return SQLITE_OK;
46267}
46268
46269/*
46270** Release a reference to page pPg. pPg must have been returned by an
46271** earlier call to pagerAcquireMapPage().
46272*/
46273static void pagerReleaseMapPage(PgHdr *pPg){
46274  Pager *pPager = pPg->pPager;
46275  pPager->nMmapOut--;
46276  pPg->pDirty = pPager->pMmapFreelist;
46277  pPager->pMmapFreelist = pPg;
46278
46279  assert( pPager->fd->pMethods->iVersion>=3 );
46280  sqlite3OsUnfetch(pPager->fd, (i64)(pPg->pgno-1)*pPager->pageSize, pPg->pData);
46281}
46282
46283/*
46284** Free all PgHdr objects stored in the Pager.pMmapFreelist list.
46285*/
46286static void pagerFreeMapHdrs(Pager *pPager){
46287  PgHdr *p;
46288  PgHdr *pNext;
46289  for(p=pPager->pMmapFreelist; p; p=pNext){
46290    pNext = p->pDirty;
46291    sqlite3_free(p);
46292  }
46293}
46294
46295
46296/*
46297** Shutdown the page cache.  Free all memory and close all files.
46298**
46299** If a transaction was in progress when this routine is called, that
46300** transaction is rolled back.  All outstanding pages are invalidated
46301** and their memory is freed.  Any attempt to use a page associated
46302** with this page cache after this function returns will likely
46303** result in a coredump.
46304**
46305** This function always succeeds. If a transaction is active an attempt
46306** is made to roll it back. If an error occurs during the rollback
46307** a hot journal may be left in the filesystem but no error is returned
46308** to the caller.
46309*/
46310SQLITE_PRIVATE int sqlite3PagerClose(Pager *pPager){
46311  u8 *pTmp = (u8 *)pPager->pTmpSpace;
46312
46313  assert( assert_pager_state(pPager) );
46314  disable_simulated_io_errors();
46315  sqlite3BeginBenignMalloc();
46316  pagerFreeMapHdrs(pPager);
46317  /* pPager->errCode = 0; */
46318  pPager->exclusiveMode = 0;
46319#ifndef SQLITE_OMIT_WAL
46320  sqlite3WalClose(pPager->pWal, pPager->ckptSyncFlags, pPager->pageSize, pTmp);
46321  pPager->pWal = 0;
46322#endif
46323  pager_reset(pPager);
46324  if( MEMDB ){
46325    pager_unlock(pPager);
46326  }else{
46327    /* If it is open, sync the journal file before calling UnlockAndRollback.
46328    ** If this is not done, then an unsynced portion of the open journal
46329    ** file may be played back into the database. If a power failure occurs
46330    ** while this is happening, the database could become corrupt.
46331    **
46332    ** If an error occurs while trying to sync the journal, shift the pager
46333    ** into the ERROR state. This causes UnlockAndRollback to unlock the
46334    ** database and close the journal file without attempting to roll it
46335    ** back or finalize it. The next database user will have to do hot-journal
46336    ** rollback before accessing the database file.
46337    */
46338    if( isOpen(pPager->jfd) ){
46339      pager_error(pPager, pagerSyncHotJournal(pPager));
46340    }
46341    pagerUnlockAndRollback(pPager);
46342  }
46343  sqlite3EndBenignMalloc();
46344  enable_simulated_io_errors();
46345  PAGERTRACE(("CLOSE %d\n", PAGERID(pPager)));
46346  IOTRACE(("CLOSE %p\n", pPager))
46347  sqlite3OsClose(pPager->jfd);
46348  sqlite3OsClose(pPager->fd);
46349  sqlite3PageFree(pTmp);
46350  sqlite3PcacheClose(pPager->pPCache);
46351
46352#ifdef SQLITE_HAS_CODEC
46353  if( pPager->xCodecFree ) pPager->xCodecFree(pPager->pCodec);
46354#endif
46355
46356  assert( !pPager->aSavepoint && !pPager->pInJournal );
46357  assert( !isOpen(pPager->jfd) && !isOpen(pPager->sjfd) );
46358
46359  sqlite3_free(pPager);
46360  return SQLITE_OK;
46361}
46362
46363#if !defined(NDEBUG) || defined(SQLITE_TEST)
46364/*
46365** Return the page number for page pPg.
46366*/
46367SQLITE_PRIVATE Pgno sqlite3PagerPagenumber(DbPage *pPg){
46368  return pPg->pgno;
46369}
46370#endif
46371
46372/*
46373** Increment the reference count for page pPg.
46374*/
46375SQLITE_PRIVATE void sqlite3PagerRef(DbPage *pPg){
46376  sqlite3PcacheRef(pPg);
46377}
46378
46379/*
46380** Sync the journal. In other words, make sure all the pages that have
46381** been written to the journal have actually reached the surface of the
46382** disk and can be restored in the event of a hot-journal rollback.
46383**
46384** If the Pager.noSync flag is set, then this function is a no-op.
46385** Otherwise, the actions required depend on the journal-mode and the
46386** device characteristics of the file-system, as follows:
46387**
46388**   * If the journal file is an in-memory journal file, no action need
46389**     be taken.
46390**
46391**   * Otherwise, if the device does not support the SAFE_APPEND property,
46392**     then the nRec field of the most recently written journal header
46393**     is updated to contain the number of journal records that have
46394**     been written following it. If the pager is operating in full-sync
46395**     mode, then the journal file is synced before this field is updated.
46396**
46397**   * If the device does not support the SEQUENTIAL property, then
46398**     journal file is synced.
46399**
46400** Or, in pseudo-code:
46401**
46402**   if( NOT <in-memory journal> ){
46403**     if( NOT SAFE_APPEND ){
46404**       if( <full-sync mode> ) xSync(<journal file>);
46405**       <update nRec field>
46406**     }
46407**     if( NOT SEQUENTIAL ) xSync(<journal file>);
46408**   }
46409**
46410** If successful, this routine clears the PGHDR_NEED_SYNC flag of every
46411** page currently held in memory before returning SQLITE_OK. If an IO
46412** error is encountered, then the IO error code is returned to the caller.
46413*/
46414static int syncJournal(Pager *pPager, int newHdr){
46415  int rc;                         /* Return code */
46416
46417  assert( pPager->eState==PAGER_WRITER_CACHEMOD
46418       || pPager->eState==PAGER_WRITER_DBMOD
46419  );
46420  assert( assert_pager_state(pPager) );
46421  assert( !pagerUseWal(pPager) );
46422
46423  rc = sqlite3PagerExclusiveLock(pPager);
46424  if( rc!=SQLITE_OK ) return rc;
46425
46426  if( !pPager->noSync ){
46427    assert( !pPager->tempFile );
46428    if( isOpen(pPager->jfd) && pPager->journalMode!=PAGER_JOURNALMODE_MEMORY ){
46429      const int iDc = sqlite3OsDeviceCharacteristics(pPager->fd);
46430      assert( isOpen(pPager->jfd) );
46431
46432      if( 0==(iDc&SQLITE_IOCAP_SAFE_APPEND) ){
46433        /* This block deals with an obscure problem. If the last connection
46434        ** that wrote to this database was operating in persistent-journal
46435        ** mode, then the journal file may at this point actually be larger
46436        ** than Pager.journalOff bytes. If the next thing in the journal
46437        ** file happens to be a journal-header (written as part of the
46438        ** previous connection's transaction), and a crash or power-failure
46439        ** occurs after nRec is updated but before this connection writes
46440        ** anything else to the journal file (or commits/rolls back its
46441        ** transaction), then SQLite may become confused when doing the
46442        ** hot-journal rollback following recovery. It may roll back all
46443        ** of this connections data, then proceed to rolling back the old,
46444        ** out-of-date data that follows it. Database corruption.
46445        **
46446        ** To work around this, if the journal file does appear to contain
46447        ** a valid header following Pager.journalOff, then write a 0x00
46448        ** byte to the start of it to prevent it from being recognized.
46449        **
46450        ** Variable iNextHdrOffset is set to the offset at which this
46451        ** problematic header will occur, if it exists. aMagic is used
46452        ** as a temporary buffer to inspect the first couple of bytes of
46453        ** the potential journal header.
46454        */
46455        i64 iNextHdrOffset;
46456        u8 aMagic[8];
46457        u8 zHeader[sizeof(aJournalMagic)+4];
46458
46459        memcpy(zHeader, aJournalMagic, sizeof(aJournalMagic));
46460        put32bits(&zHeader[sizeof(aJournalMagic)], pPager->nRec);
46461
46462        iNextHdrOffset = journalHdrOffset(pPager);
46463        rc = sqlite3OsRead(pPager->jfd, aMagic, 8, iNextHdrOffset);
46464        if( rc==SQLITE_OK && 0==memcmp(aMagic, aJournalMagic, 8) ){
46465          static const u8 zerobyte = 0;
46466          rc = sqlite3OsWrite(pPager->jfd, &zerobyte, 1, iNextHdrOffset);
46467        }
46468        if( rc!=SQLITE_OK && rc!=SQLITE_IOERR_SHORT_READ ){
46469          return rc;
46470        }
46471
46472        /* Write the nRec value into the journal file header. If in
46473        ** full-synchronous mode, sync the journal first. This ensures that
46474        ** all data has really hit the disk before nRec is updated to mark
46475        ** it as a candidate for rollback.
46476        **
46477        ** This is not required if the persistent media supports the
46478        ** SAFE_APPEND property. Because in this case it is not possible
46479        ** for garbage data to be appended to the file, the nRec field
46480        ** is populated with 0xFFFFFFFF when the journal header is written
46481        ** and never needs to be updated.
46482        */
46483        if( pPager->fullSync && 0==(iDc&SQLITE_IOCAP_SEQUENTIAL) ){
46484          PAGERTRACE(("SYNC journal of %d\n", PAGERID(pPager)));
46485          IOTRACE(("JSYNC %p\n", pPager))
46486          rc = sqlite3OsSync(pPager->jfd, pPager->syncFlags);
46487          if( rc!=SQLITE_OK ) return rc;
46488        }
46489        IOTRACE(("JHDR %p %lld\n", pPager, pPager->journalHdr));
46490        rc = sqlite3OsWrite(
46491            pPager->jfd, zHeader, sizeof(zHeader), pPager->journalHdr
46492        );
46493        if( rc!=SQLITE_OK ) return rc;
46494      }
46495      if( 0==(iDc&SQLITE_IOCAP_SEQUENTIAL) ){
46496        PAGERTRACE(("SYNC journal of %d\n", PAGERID(pPager)));
46497        IOTRACE(("JSYNC %p\n", pPager))
46498        rc = sqlite3OsSync(pPager->jfd, pPager->syncFlags|
46499          (pPager->syncFlags==SQLITE_SYNC_FULL?SQLITE_SYNC_DATAONLY:0)
46500        );
46501        if( rc!=SQLITE_OK ) return rc;
46502      }
46503
46504      pPager->journalHdr = pPager->journalOff;
46505      if( newHdr && 0==(iDc&SQLITE_IOCAP_SAFE_APPEND) ){
46506        pPager->nRec = 0;
46507        rc = writeJournalHdr(pPager);
46508        if( rc!=SQLITE_OK ) return rc;
46509      }
46510    }else{
46511      pPager->journalHdr = pPager->journalOff;
46512    }
46513  }
46514
46515  /* Unless the pager is in noSync mode, the journal file was just
46516  ** successfully synced. Either way, clear the PGHDR_NEED_SYNC flag on
46517  ** all pages.
46518  */
46519  sqlite3PcacheClearSyncFlags(pPager->pPCache);
46520  pPager->eState = PAGER_WRITER_DBMOD;
46521  assert( assert_pager_state(pPager) );
46522  return SQLITE_OK;
46523}
46524
46525/*
46526** The argument is the first in a linked list of dirty pages connected
46527** by the PgHdr.pDirty pointer. This function writes each one of the
46528** in-memory pages in the list to the database file. The argument may
46529** be NULL, representing an empty list. In this case this function is
46530** a no-op.
46531**
46532** The pager must hold at least a RESERVED lock when this function
46533** is called. Before writing anything to the database file, this lock
46534** is upgraded to an EXCLUSIVE lock. If the lock cannot be obtained,
46535** SQLITE_BUSY is returned and no data is written to the database file.
46536**
46537** If the pager is a temp-file pager and the actual file-system file
46538** is not yet open, it is created and opened before any data is
46539** written out.
46540**
46541** Once the lock has been upgraded and, if necessary, the file opened,
46542** the pages are written out to the database file in list order. Writing
46543** a page is skipped if it meets either of the following criteria:
46544**
46545**   * The page number is greater than Pager.dbSize, or
46546**   * The PGHDR_DONT_WRITE flag is set on the page.
46547**
46548** If writing out a page causes the database file to grow, Pager.dbFileSize
46549** is updated accordingly. If page 1 is written out, then the value cached
46550** in Pager.dbFileVers[] is updated to match the new value stored in
46551** the database file.
46552**
46553** If everything is successful, SQLITE_OK is returned. If an IO error
46554** occurs, an IO error code is returned. Or, if the EXCLUSIVE lock cannot
46555** be obtained, SQLITE_BUSY is returned.
46556*/
46557static int pager_write_pagelist(Pager *pPager, PgHdr *pList){
46558  int rc = SQLITE_OK;                  /* Return code */
46559
46560  /* This function is only called for rollback pagers in WRITER_DBMOD state. */
46561  assert( !pagerUseWal(pPager) );
46562  assert( pPager->eState==PAGER_WRITER_DBMOD );
46563  assert( pPager->eLock==EXCLUSIVE_LOCK );
46564
46565  /* If the file is a temp-file has not yet been opened, open it now. It
46566  ** is not possible for rc to be other than SQLITE_OK if this branch
46567  ** is taken, as pager_wait_on_lock() is a no-op for temp-files.
46568  */
46569  if( !isOpen(pPager->fd) ){
46570    assert( pPager->tempFile && rc==SQLITE_OK );
46571    rc = pagerOpentemp(pPager, pPager->fd, pPager->vfsFlags);
46572  }
46573
46574  /* Before the first write, give the VFS a hint of what the final
46575  ** file size will be.
46576  */
46577  assert( rc!=SQLITE_OK || isOpen(pPager->fd) );
46578  if( rc==SQLITE_OK
46579   && pPager->dbHintSize<pPager->dbSize
46580   && (pList->pDirty || pList->pgno>pPager->dbHintSize)
46581  ){
46582    sqlite3_int64 szFile = pPager->pageSize * (sqlite3_int64)pPager->dbSize;
46583    sqlite3OsFileControlHint(pPager->fd, SQLITE_FCNTL_SIZE_HINT, &szFile);
46584    pPager->dbHintSize = pPager->dbSize;
46585  }
46586
46587  while( rc==SQLITE_OK && pList ){
46588    Pgno pgno = pList->pgno;
46589
46590    /* If there are dirty pages in the page cache with page numbers greater
46591    ** than Pager.dbSize, this means sqlite3PagerTruncateImage() was called to
46592    ** make the file smaller (presumably by auto-vacuum code). Do not write
46593    ** any such pages to the file.
46594    **
46595    ** Also, do not write out any page that has the PGHDR_DONT_WRITE flag
46596    ** set (set by sqlite3PagerDontWrite()).
46597    */
46598    if( pgno<=pPager->dbSize && 0==(pList->flags&PGHDR_DONT_WRITE) ){
46599      i64 offset = (pgno-1)*(i64)pPager->pageSize;   /* Offset to write */
46600      char *pData;                                   /* Data to write */
46601
46602      assert( (pList->flags&PGHDR_NEED_SYNC)==0 );
46603      if( pList->pgno==1 ) pager_write_changecounter(pList);
46604
46605      /* Encode the database */
46606      CODEC2(pPager, pList->pData, pgno, 6, return SQLITE_NOMEM, pData);
46607
46608      /* Write out the page data. */
46609      rc = sqlite3OsWrite(pPager->fd, pData, pPager->pageSize, offset);
46610
46611      /* If page 1 was just written, update Pager.dbFileVers to match
46612      ** the value now stored in the database file. If writing this
46613      ** page caused the database file to grow, update dbFileSize.
46614      */
46615      if( pgno==1 ){
46616        memcpy(&pPager->dbFileVers, &pData[24], sizeof(pPager->dbFileVers));
46617      }
46618      if( pgno>pPager->dbFileSize ){
46619        pPager->dbFileSize = pgno;
46620      }
46621      pPager->aStat[PAGER_STAT_WRITE]++;
46622
46623      /* Update any backup objects copying the contents of this pager. */
46624      sqlite3BackupUpdate(pPager->pBackup, pgno, (u8*)pList->pData);
46625
46626      PAGERTRACE(("STORE %d page %d hash(%08x)\n",
46627                   PAGERID(pPager), pgno, pager_pagehash(pList)));
46628      IOTRACE(("PGOUT %p %d\n", pPager, pgno));
46629      PAGER_INCR(sqlite3_pager_writedb_count);
46630    }else{
46631      PAGERTRACE(("NOSTORE %d page %d\n", PAGERID(pPager), pgno));
46632    }
46633    pager_set_pagehash(pList);
46634    pList = pList->pDirty;
46635  }
46636
46637  return rc;
46638}
46639
46640/*
46641** Ensure that the sub-journal file is open. If it is already open, this
46642** function is a no-op.
46643**
46644** SQLITE_OK is returned if everything goes according to plan. An
46645** SQLITE_IOERR_XXX error code is returned if a call to sqlite3OsOpen()
46646** fails.
46647*/
46648static int openSubJournal(Pager *pPager){
46649  int rc = SQLITE_OK;
46650  if( !isOpen(pPager->sjfd) ){
46651    if( pPager->journalMode==PAGER_JOURNALMODE_MEMORY || pPager->subjInMemory ){
46652      sqlite3MemJournalOpen(pPager->sjfd);
46653    }else{
46654      rc = pagerOpentemp(pPager, pPager->sjfd, SQLITE_OPEN_SUBJOURNAL);
46655    }
46656  }
46657  return rc;
46658}
46659
46660/*
46661** Append a record of the current state of page pPg to the sub-journal.
46662**
46663** If successful, set the bit corresponding to pPg->pgno in the bitvecs
46664** for all open savepoints before returning.
46665**
46666** This function returns SQLITE_OK if everything is successful, an IO
46667** error code if the attempt to write to the sub-journal fails, or
46668** SQLITE_NOMEM if a malloc fails while setting a bit in a savepoint
46669** bitvec.
46670*/
46671static int subjournalPage(PgHdr *pPg){
46672  int rc = SQLITE_OK;
46673  Pager *pPager = pPg->pPager;
46674  if( pPager->journalMode!=PAGER_JOURNALMODE_OFF ){
46675
46676    /* Open the sub-journal, if it has not already been opened */
46677    assert( pPager->useJournal );
46678    assert( isOpen(pPager->jfd) || pagerUseWal(pPager) );
46679    assert( isOpen(pPager->sjfd) || pPager->nSubRec==0 );
46680    assert( pagerUseWal(pPager)
46681         || pageInJournal(pPager, pPg)
46682         || pPg->pgno>pPager->dbOrigSize
46683    );
46684    rc = openSubJournal(pPager);
46685
46686    /* If the sub-journal was opened successfully (or was already open),
46687    ** write the journal record into the file.  */
46688    if( rc==SQLITE_OK ){
46689      void *pData = pPg->pData;
46690      i64 offset = (i64)pPager->nSubRec*(4+pPager->pageSize);
46691      char *pData2;
46692
46693      CODEC2(pPager, pData, pPg->pgno, 7, return SQLITE_NOMEM, pData2);
46694      PAGERTRACE(("STMT-JOURNAL %d page %d\n", PAGERID(pPager), pPg->pgno));
46695      rc = write32bits(pPager->sjfd, offset, pPg->pgno);
46696      if( rc==SQLITE_OK ){
46697        rc = sqlite3OsWrite(pPager->sjfd, pData2, pPager->pageSize, offset+4);
46698      }
46699    }
46700  }
46701  if( rc==SQLITE_OK ){
46702    pPager->nSubRec++;
46703    assert( pPager->nSavepoint>0 );
46704    rc = addToSavepointBitvecs(pPager, pPg->pgno);
46705  }
46706  return rc;
46707}
46708static int subjournalPageIfRequired(PgHdr *pPg){
46709  if( subjRequiresPage(pPg) ){
46710    return subjournalPage(pPg);
46711  }else{
46712    return SQLITE_OK;
46713  }
46714}
46715
46716/*
46717** This function is called by the pcache layer when it has reached some
46718** soft memory limit. The first argument is a pointer to a Pager object
46719** (cast as a void*). The pager is always 'purgeable' (not an in-memory
46720** database). The second argument is a reference to a page that is
46721** currently dirty but has no outstanding references. The page
46722** is always associated with the Pager object passed as the first
46723** argument.
46724**
46725** The job of this function is to make pPg clean by writing its contents
46726** out to the database file, if possible. This may involve syncing the
46727** journal file.
46728**
46729** If successful, sqlite3PcacheMakeClean() is called on the page and
46730** SQLITE_OK returned. If an IO error occurs while trying to make the
46731** page clean, the IO error code is returned. If the page cannot be
46732** made clean for some other reason, but no error occurs, then SQLITE_OK
46733** is returned by sqlite3PcacheMakeClean() is not called.
46734*/
46735static int pagerStress(void *p, PgHdr *pPg){
46736  Pager *pPager = (Pager *)p;
46737  int rc = SQLITE_OK;
46738
46739  assert( pPg->pPager==pPager );
46740  assert( pPg->flags&PGHDR_DIRTY );
46741
46742  /* The doNotSpill NOSYNC bit is set during times when doing a sync of
46743  ** journal (and adding a new header) is not allowed.  This occurs
46744  ** during calls to sqlite3PagerWrite() while trying to journal multiple
46745  ** pages belonging to the same sector.
46746  **
46747  ** The doNotSpill ROLLBACK and OFF bits inhibits all cache spilling
46748  ** regardless of whether or not a sync is required.  This is set during
46749  ** a rollback or by user request, respectively.
46750  **
46751  ** Spilling is also prohibited when in an error state since that could
46752  ** lead to database corruption.   In the current implementation it
46753  ** is impossible for sqlite3PcacheFetch() to be called with createFlag==3
46754  ** while in the error state, hence it is impossible for this routine to
46755  ** be called in the error state.  Nevertheless, we include a NEVER()
46756  ** test for the error state as a safeguard against future changes.
46757  */
46758  if( NEVER(pPager->errCode) ) return SQLITE_OK;
46759  testcase( pPager->doNotSpill & SPILLFLAG_ROLLBACK );
46760  testcase( pPager->doNotSpill & SPILLFLAG_OFF );
46761  testcase( pPager->doNotSpill & SPILLFLAG_NOSYNC );
46762  if( pPager->doNotSpill
46763   && ((pPager->doNotSpill & (SPILLFLAG_ROLLBACK|SPILLFLAG_OFF))!=0
46764      || (pPg->flags & PGHDR_NEED_SYNC)!=0)
46765  ){
46766    return SQLITE_OK;
46767  }
46768
46769  pPg->pDirty = 0;
46770  if( pagerUseWal(pPager) ){
46771    /* Write a single frame for this page to the log. */
46772    rc = subjournalPageIfRequired(pPg);
46773    if( rc==SQLITE_OK ){
46774      rc = pagerWalFrames(pPager, pPg, 0, 0);
46775    }
46776  }else{
46777
46778    /* Sync the journal file if required. */
46779    if( pPg->flags&PGHDR_NEED_SYNC
46780     || pPager->eState==PAGER_WRITER_CACHEMOD
46781    ){
46782      rc = syncJournal(pPager, 1);
46783    }
46784
46785    /* Write the contents of the page out to the database file. */
46786    if( rc==SQLITE_OK ){
46787      assert( (pPg->flags&PGHDR_NEED_SYNC)==0 );
46788      rc = pager_write_pagelist(pPager, pPg);
46789    }
46790  }
46791
46792  /* Mark the page as clean. */
46793  if( rc==SQLITE_OK ){
46794    PAGERTRACE(("STRESS %d page %d\n", PAGERID(pPager), pPg->pgno));
46795    sqlite3PcacheMakeClean(pPg);
46796  }
46797
46798  return pager_error(pPager, rc);
46799}
46800
46801
46802/*
46803** Allocate and initialize a new Pager object and put a pointer to it
46804** in *ppPager. The pager should eventually be freed by passing it
46805** to sqlite3PagerClose().
46806**
46807** The zFilename argument is the path to the database file to open.
46808** If zFilename is NULL then a randomly-named temporary file is created
46809** and used as the file to be cached. Temporary files are be deleted
46810** automatically when they are closed. If zFilename is ":memory:" then
46811** all information is held in cache. It is never written to disk.
46812** This can be used to implement an in-memory database.
46813**
46814** The nExtra parameter specifies the number of bytes of space allocated
46815** along with each page reference. This space is available to the user
46816** via the sqlite3PagerGetExtra() API.
46817**
46818** The flags argument is used to specify properties that affect the
46819** operation of the pager. It should be passed some bitwise combination
46820** of the PAGER_* flags.
46821**
46822** The vfsFlags parameter is a bitmask to pass to the flags parameter
46823** of the xOpen() method of the supplied VFS when opening files.
46824**
46825** If the pager object is allocated and the specified file opened
46826** successfully, SQLITE_OK is returned and *ppPager set to point to
46827** the new pager object. If an error occurs, *ppPager is set to NULL
46828** and error code returned. This function may return SQLITE_NOMEM
46829** (sqlite3Malloc() is used to allocate memory), SQLITE_CANTOPEN or
46830** various SQLITE_IO_XXX errors.
46831*/
46832SQLITE_PRIVATE int sqlite3PagerOpen(
46833  sqlite3_vfs *pVfs,       /* The virtual file system to use */
46834  Pager **ppPager,         /* OUT: Return the Pager structure here */
46835  const char *zFilename,   /* Name of the database file to open */
46836  int nExtra,              /* Extra bytes append to each in-memory page */
46837  int flags,               /* flags controlling this file */
46838  int vfsFlags,            /* flags passed through to sqlite3_vfs.xOpen() */
46839  void (*xReinit)(DbPage*) /* Function to reinitialize pages */
46840){
46841  u8 *pPtr;
46842  Pager *pPager = 0;       /* Pager object to allocate and return */
46843  int rc = SQLITE_OK;      /* Return code */
46844  int tempFile = 0;        /* True for temp files (incl. in-memory files) */
46845  int memDb = 0;           /* True if this is an in-memory file */
46846  int readOnly = 0;        /* True if this is a read-only file */
46847  int journalFileSize;     /* Bytes to allocate for each journal fd */
46848  char *zPathname = 0;     /* Full path to database file */
46849  int nPathname = 0;       /* Number of bytes in zPathname */
46850  int useJournal = (flags & PAGER_OMIT_JOURNAL)==0; /* False to omit journal */
46851  int pcacheSize = sqlite3PcacheSize();       /* Bytes to allocate for PCache */
46852  u32 szPageDflt = SQLITE_DEFAULT_PAGE_SIZE;  /* Default page size */
46853  const char *zUri = 0;    /* URI args to copy */
46854  int nUri = 0;            /* Number of bytes of URI args at *zUri */
46855
46856  /* Figure out how much space is required for each journal file-handle
46857  ** (there are two of them, the main journal and the sub-journal). This
46858  ** is the maximum space required for an in-memory journal file handle
46859  ** and a regular journal file-handle. Note that a "regular journal-handle"
46860  ** may be a wrapper capable of caching the first portion of the journal
46861  ** file in memory to implement the atomic-write optimization (see
46862  ** source file journal.c).
46863  */
46864  if( sqlite3JournalSize(pVfs)>sqlite3MemJournalSize() ){
46865    journalFileSize = ROUND8(sqlite3JournalSize(pVfs));
46866  }else{
46867    journalFileSize = ROUND8(sqlite3MemJournalSize());
46868  }
46869
46870  /* Set the output variable to NULL in case an error occurs. */
46871  *ppPager = 0;
46872
46873#ifndef SQLITE_OMIT_MEMORYDB
46874  if( flags & PAGER_MEMORY ){
46875    memDb = 1;
46876    if( zFilename && zFilename[0] ){
46877      zPathname = sqlite3DbStrDup(0, zFilename);
46878      if( zPathname==0  ) return SQLITE_NOMEM;
46879      nPathname = sqlite3Strlen30(zPathname);
46880      zFilename = 0;
46881    }
46882  }
46883#endif
46884
46885  /* Compute and store the full pathname in an allocated buffer pointed
46886  ** to by zPathname, length nPathname. Or, if this is a temporary file,
46887  ** leave both nPathname and zPathname set to 0.
46888  */
46889  if( zFilename && zFilename[0] ){
46890    const char *z;
46891    nPathname = pVfs->mxPathname+1;
46892    zPathname = sqlite3DbMallocRaw(0, nPathname*2);
46893    if( zPathname==0 ){
46894      return SQLITE_NOMEM;
46895    }
46896    zPathname[0] = 0; /* Make sure initialized even if FullPathname() fails */
46897    rc = sqlite3OsFullPathname(pVfs, zFilename, nPathname, zPathname);
46898    nPathname = sqlite3Strlen30(zPathname);
46899    z = zUri = &zFilename[sqlite3Strlen30(zFilename)+1];
46900    while( *z ){
46901      z += sqlite3Strlen30(z)+1;
46902      z += sqlite3Strlen30(z)+1;
46903    }
46904    nUri = (int)(&z[1] - zUri);
46905    assert( nUri>=0 );
46906    if( rc==SQLITE_OK && nPathname+8>pVfs->mxPathname ){
46907      /* This branch is taken when the journal path required by
46908      ** the database being opened will be more than pVfs->mxPathname
46909      ** bytes in length. This means the database cannot be opened,
46910      ** as it will not be possible to open the journal file or even
46911      ** check for a hot-journal before reading.
46912      */
46913      rc = SQLITE_CANTOPEN_BKPT;
46914    }
46915    if( rc!=SQLITE_OK ){
46916      sqlite3DbFree(0, zPathname);
46917      return rc;
46918    }
46919  }
46920
46921  /* Allocate memory for the Pager structure, PCache object, the
46922  ** three file descriptors, the database file name and the journal
46923  ** file name. The layout in memory is as follows:
46924  **
46925  **     Pager object                    (sizeof(Pager) bytes)
46926  **     PCache object                   (sqlite3PcacheSize() bytes)
46927  **     Database file handle            (pVfs->szOsFile bytes)
46928  **     Sub-journal file handle         (journalFileSize bytes)
46929  **     Main journal file handle        (journalFileSize bytes)
46930  **     Database file name              (nPathname+1 bytes)
46931  **     Journal file name               (nPathname+8+1 bytes)
46932  */
46933  pPtr = (u8 *)sqlite3MallocZero(
46934    ROUND8(sizeof(*pPager)) +      /* Pager structure */
46935    ROUND8(pcacheSize) +           /* PCache object */
46936    ROUND8(pVfs->szOsFile) +       /* The main db file */
46937    journalFileSize * 2 +          /* The two journal files */
46938    nPathname + 1 + nUri +         /* zFilename */
46939    nPathname + 8 + 2              /* zJournal */
46940#ifndef SQLITE_OMIT_WAL
46941    + nPathname + 4 + 2            /* zWal */
46942#endif
46943  );
46944  assert( EIGHT_BYTE_ALIGNMENT(SQLITE_INT_TO_PTR(journalFileSize)) );
46945  if( !pPtr ){
46946    sqlite3DbFree(0, zPathname);
46947    return SQLITE_NOMEM;
46948  }
46949  pPager =              (Pager*)(pPtr);
46950  pPager->pPCache =    (PCache*)(pPtr += ROUND8(sizeof(*pPager)));
46951  pPager->fd =   (sqlite3_file*)(pPtr += ROUND8(pcacheSize));
46952  pPager->sjfd = (sqlite3_file*)(pPtr += ROUND8(pVfs->szOsFile));
46953  pPager->jfd =  (sqlite3_file*)(pPtr += journalFileSize);
46954  pPager->zFilename =    (char*)(pPtr += journalFileSize);
46955  assert( EIGHT_BYTE_ALIGNMENT(pPager->jfd) );
46956
46957  /* Fill in the Pager.zFilename and Pager.zJournal buffers, if required. */
46958  if( zPathname ){
46959    assert( nPathname>0 );
46960    pPager->zJournal =   (char*)(pPtr += nPathname + 1 + nUri);
46961    memcpy(pPager->zFilename, zPathname, nPathname);
46962    if( nUri ) memcpy(&pPager->zFilename[nPathname+1], zUri, nUri);
46963    memcpy(pPager->zJournal, zPathname, nPathname);
46964    memcpy(&pPager->zJournal[nPathname], "-journal\000", 8+2);
46965    sqlite3FileSuffix3(pPager->zFilename, pPager->zJournal);
46966#ifndef SQLITE_OMIT_WAL
46967    pPager->zWal = &pPager->zJournal[nPathname+8+1];
46968    memcpy(pPager->zWal, zPathname, nPathname);
46969    memcpy(&pPager->zWal[nPathname], "-wal\000", 4+1);
46970    sqlite3FileSuffix3(pPager->zFilename, pPager->zWal);
46971#endif
46972    sqlite3DbFree(0, zPathname);
46973  }
46974  pPager->pVfs = pVfs;
46975  pPager->vfsFlags = vfsFlags;
46976
46977  /* Open the pager file.
46978  */
46979  if( zFilename && zFilename[0] ){
46980    int fout = 0;                    /* VFS flags returned by xOpen() */
46981    rc = sqlite3OsOpen(pVfs, pPager->zFilename, pPager->fd, vfsFlags, &fout);
46982    assert( !memDb );
46983    readOnly = (fout&SQLITE_OPEN_READONLY);
46984
46985    /* If the file was successfully opened for read/write access,
46986    ** choose a default page size in case we have to create the
46987    ** database file. The default page size is the maximum of:
46988    **
46989    **    + SQLITE_DEFAULT_PAGE_SIZE,
46990    **    + The value returned by sqlite3OsSectorSize()
46991    **    + The largest page size that can be written atomically.
46992    */
46993    if( rc==SQLITE_OK ){
46994      int iDc = sqlite3OsDeviceCharacteristics(pPager->fd);
46995      if( !readOnly ){
46996        setSectorSize(pPager);
46997        assert(SQLITE_DEFAULT_PAGE_SIZE<=SQLITE_MAX_DEFAULT_PAGE_SIZE);
46998        if( szPageDflt<pPager->sectorSize ){
46999          if( pPager->sectorSize>SQLITE_MAX_DEFAULT_PAGE_SIZE ){
47000            szPageDflt = SQLITE_MAX_DEFAULT_PAGE_SIZE;
47001          }else{
47002            szPageDflt = (u32)pPager->sectorSize;
47003          }
47004        }
47005#ifdef SQLITE_ENABLE_ATOMIC_WRITE
47006        {
47007          int ii;
47008          assert(SQLITE_IOCAP_ATOMIC512==(512>>8));
47009          assert(SQLITE_IOCAP_ATOMIC64K==(65536>>8));
47010          assert(SQLITE_MAX_DEFAULT_PAGE_SIZE<=65536);
47011          for(ii=szPageDflt; ii<=SQLITE_MAX_DEFAULT_PAGE_SIZE; ii=ii*2){
47012            if( iDc&(SQLITE_IOCAP_ATOMIC|(ii>>8)) ){
47013              szPageDflt = ii;
47014            }
47015          }
47016        }
47017#endif
47018      }
47019      pPager->noLock = sqlite3_uri_boolean(zFilename, "nolock", 0);
47020      if( (iDc & SQLITE_IOCAP_IMMUTABLE)!=0
47021       || sqlite3_uri_boolean(zFilename, "immutable", 0) ){
47022          vfsFlags |= SQLITE_OPEN_READONLY;
47023          goto act_like_temp_file;
47024      }
47025    }
47026  }else{
47027    /* If a temporary file is requested, it is not opened immediately.
47028    ** In this case we accept the default page size and delay actually
47029    ** opening the file until the first call to OsWrite().
47030    **
47031    ** This branch is also run for an in-memory database. An in-memory
47032    ** database is the same as a temp-file that is never written out to
47033    ** disk and uses an in-memory rollback journal.
47034    **
47035    ** This branch also runs for files marked as immutable.
47036    */
47037act_like_temp_file:
47038    tempFile = 1;
47039    pPager->eState = PAGER_READER;     /* Pretend we already have a lock */
47040    pPager->eLock = EXCLUSIVE_LOCK;    /* Pretend we are in EXCLUSIVE mode */
47041    pPager->noLock = 1;                /* Do no locking */
47042    readOnly = (vfsFlags&SQLITE_OPEN_READONLY);
47043  }
47044
47045  /* The following call to PagerSetPagesize() serves to set the value of
47046  ** Pager.pageSize and to allocate the Pager.pTmpSpace buffer.
47047  */
47048  if( rc==SQLITE_OK ){
47049    assert( pPager->memDb==0 );
47050    rc = sqlite3PagerSetPagesize(pPager, &szPageDflt, -1);
47051    testcase( rc!=SQLITE_OK );
47052  }
47053
47054  /* Initialize the PCache object. */
47055  if( rc==SQLITE_OK ){
47056    assert( nExtra<1000 );
47057    nExtra = ROUND8(nExtra);
47058    rc = sqlite3PcacheOpen(szPageDflt, nExtra, !memDb,
47059                       !memDb?pagerStress:0, (void *)pPager, pPager->pPCache);
47060  }
47061
47062  /* If an error occurred above, free the  Pager structure and close the file.
47063  */
47064  if( rc!=SQLITE_OK ){
47065    sqlite3OsClose(pPager->fd);
47066    sqlite3PageFree(pPager->pTmpSpace);
47067    sqlite3_free(pPager);
47068    return rc;
47069  }
47070
47071  PAGERTRACE(("OPEN %d %s\n", FILEHANDLEID(pPager->fd), pPager->zFilename));
47072  IOTRACE(("OPEN %p %s\n", pPager, pPager->zFilename))
47073
47074  pPager->useJournal = (u8)useJournal;
47075  /* pPager->stmtOpen = 0; */
47076  /* pPager->stmtInUse = 0; */
47077  /* pPager->nRef = 0; */
47078  /* pPager->stmtSize = 0; */
47079  /* pPager->stmtJSize = 0; */
47080  /* pPager->nPage = 0; */
47081  pPager->mxPgno = SQLITE_MAX_PAGE_COUNT;
47082  /* pPager->state = PAGER_UNLOCK; */
47083  /* pPager->errMask = 0; */
47084  pPager->tempFile = (u8)tempFile;
47085  assert( tempFile==PAGER_LOCKINGMODE_NORMAL
47086          || tempFile==PAGER_LOCKINGMODE_EXCLUSIVE );
47087  assert( PAGER_LOCKINGMODE_EXCLUSIVE==1 );
47088  pPager->exclusiveMode = (u8)tempFile;
47089  pPager->changeCountDone = pPager->tempFile;
47090  pPager->memDb = (u8)memDb;
47091  pPager->readOnly = (u8)readOnly;
47092  assert( useJournal || pPager->tempFile );
47093  pPager->noSync = pPager->tempFile;
47094  if( pPager->noSync ){
47095    assert( pPager->fullSync==0 );
47096    assert( pPager->syncFlags==0 );
47097    assert( pPager->walSyncFlags==0 );
47098    assert( pPager->ckptSyncFlags==0 );
47099  }else{
47100    pPager->fullSync = 1;
47101    pPager->syncFlags = SQLITE_SYNC_NORMAL;
47102    pPager->walSyncFlags = SQLITE_SYNC_NORMAL | WAL_SYNC_TRANSACTIONS;
47103    pPager->ckptSyncFlags = SQLITE_SYNC_NORMAL;
47104  }
47105  /* pPager->pFirst = 0; */
47106  /* pPager->pFirstSynced = 0; */
47107  /* pPager->pLast = 0; */
47108  pPager->nExtra = (u16)nExtra;
47109  pPager->journalSizeLimit = SQLITE_DEFAULT_JOURNAL_SIZE_LIMIT;
47110  assert( isOpen(pPager->fd) || tempFile );
47111  setSectorSize(pPager);
47112  if( !useJournal ){
47113    pPager->journalMode = PAGER_JOURNALMODE_OFF;
47114  }else if( memDb ){
47115    pPager->journalMode = PAGER_JOURNALMODE_MEMORY;
47116  }
47117  /* pPager->xBusyHandler = 0; */
47118  /* pPager->pBusyHandlerArg = 0; */
47119  pPager->xReiniter = xReinit;
47120  /* memset(pPager->aHash, 0, sizeof(pPager->aHash)); */
47121  /* pPager->szMmap = SQLITE_DEFAULT_MMAP_SIZE // will be set by btree.c */
47122
47123  *ppPager = pPager;
47124  return SQLITE_OK;
47125}
47126
47127
47128/* Verify that the database file has not be deleted or renamed out from
47129** under the pager.  Return SQLITE_OK if the database is still were it ought
47130** to be on disk.  Return non-zero (SQLITE_READONLY_DBMOVED or some other error
47131** code from sqlite3OsAccess()) if the database has gone missing.
47132*/
47133static int databaseIsUnmoved(Pager *pPager){
47134  int bHasMoved = 0;
47135  int rc;
47136
47137  if( pPager->tempFile ) return SQLITE_OK;
47138  if( pPager->dbSize==0 ) return SQLITE_OK;
47139  assert( pPager->zFilename && pPager->zFilename[0] );
47140  rc = sqlite3OsFileControl(pPager->fd, SQLITE_FCNTL_HAS_MOVED, &bHasMoved);
47141  if( rc==SQLITE_NOTFOUND ){
47142    /* If the HAS_MOVED file-control is unimplemented, assume that the file
47143    ** has not been moved.  That is the historical behavior of SQLite: prior to
47144    ** version 3.8.3, it never checked */
47145    rc = SQLITE_OK;
47146  }else if( rc==SQLITE_OK && bHasMoved ){
47147    rc = SQLITE_READONLY_DBMOVED;
47148  }
47149  return rc;
47150}
47151
47152
47153/*
47154** This function is called after transitioning from PAGER_UNLOCK to
47155** PAGER_SHARED state. It tests if there is a hot journal present in
47156** the file-system for the given pager. A hot journal is one that
47157** needs to be played back. According to this function, a hot-journal
47158** file exists if the following criteria are met:
47159**
47160**   * The journal file exists in the file system, and
47161**   * No process holds a RESERVED or greater lock on the database file, and
47162**   * The database file itself is greater than 0 bytes in size, and
47163**   * The first byte of the journal file exists and is not 0x00.
47164**
47165** If the current size of the database file is 0 but a journal file
47166** exists, that is probably an old journal left over from a prior
47167** database with the same name. In this case the journal file is
47168** just deleted using OsDelete, *pExists is set to 0 and SQLITE_OK
47169** is returned.
47170**
47171** This routine does not check if there is a master journal filename
47172** at the end of the file. If there is, and that master journal file
47173** does not exist, then the journal file is not really hot. In this
47174** case this routine will return a false-positive. The pager_playback()
47175** routine will discover that the journal file is not really hot and
47176** will not roll it back.
47177**
47178** If a hot-journal file is found to exist, *pExists is set to 1 and
47179** SQLITE_OK returned. If no hot-journal file is present, *pExists is
47180** set to 0 and SQLITE_OK returned. If an IO error occurs while trying
47181** to determine whether or not a hot-journal file exists, the IO error
47182** code is returned and the value of *pExists is undefined.
47183*/
47184static int hasHotJournal(Pager *pPager, int *pExists){
47185  sqlite3_vfs * const pVfs = pPager->pVfs;
47186  int rc = SQLITE_OK;           /* Return code */
47187  int exists = 1;               /* True if a journal file is present */
47188  int jrnlOpen = !!isOpen(pPager->jfd);
47189
47190  assert( pPager->useJournal );
47191  assert( isOpen(pPager->fd) );
47192  assert( pPager->eState==PAGER_OPEN );
47193
47194  assert( jrnlOpen==0 || ( sqlite3OsDeviceCharacteristics(pPager->jfd) &
47195    SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN
47196  ));
47197
47198  *pExists = 0;
47199  if( !jrnlOpen ){
47200    rc = sqlite3OsAccess(pVfs, pPager->zJournal, SQLITE_ACCESS_EXISTS, &exists);
47201  }
47202  if( rc==SQLITE_OK && exists ){
47203    int locked = 0;             /* True if some process holds a RESERVED lock */
47204
47205    /* Race condition here:  Another process might have been holding the
47206    ** the RESERVED lock and have a journal open at the sqlite3OsAccess()
47207    ** call above, but then delete the journal and drop the lock before
47208    ** we get to the following sqlite3OsCheckReservedLock() call.  If that
47209    ** is the case, this routine might think there is a hot journal when
47210    ** in fact there is none.  This results in a false-positive which will
47211    ** be dealt with by the playback routine.  Ticket #3883.
47212    */
47213    rc = sqlite3OsCheckReservedLock(pPager->fd, &locked);
47214    if( rc==SQLITE_OK && !locked ){
47215      Pgno nPage;                 /* Number of pages in database file */
47216
47217      rc = pagerPagecount(pPager, &nPage);
47218      if( rc==SQLITE_OK ){
47219        /* If the database is zero pages in size, that means that either (1) the
47220        ** journal is a remnant from a prior database with the same name where
47221        ** the database file but not the journal was deleted, or (2) the initial
47222        ** transaction that populates a new database is being rolled back.
47223        ** In either case, the journal file can be deleted.  However, take care
47224        ** not to delete the journal file if it is already open due to
47225        ** journal_mode=PERSIST.
47226        */
47227        if( nPage==0 && !jrnlOpen ){
47228          sqlite3BeginBenignMalloc();
47229          if( pagerLockDb(pPager, RESERVED_LOCK)==SQLITE_OK ){
47230            sqlite3OsDelete(pVfs, pPager->zJournal, 0);
47231            if( !pPager->exclusiveMode ) pagerUnlockDb(pPager, SHARED_LOCK);
47232          }
47233          sqlite3EndBenignMalloc();
47234        }else{
47235          /* The journal file exists and no other connection has a reserved
47236          ** or greater lock on the database file. Now check that there is
47237          ** at least one non-zero bytes at the start of the journal file.
47238          ** If there is, then we consider this journal to be hot. If not,
47239          ** it can be ignored.
47240          */
47241          if( !jrnlOpen ){
47242            int f = SQLITE_OPEN_READONLY|SQLITE_OPEN_MAIN_JOURNAL;
47243            rc = sqlite3OsOpen(pVfs, pPager->zJournal, pPager->jfd, f, &f);
47244          }
47245          if( rc==SQLITE_OK ){
47246            u8 first = 0;
47247            rc = sqlite3OsRead(pPager->jfd, (void *)&first, 1, 0);
47248            if( rc==SQLITE_IOERR_SHORT_READ ){
47249              rc = SQLITE_OK;
47250            }
47251            if( !jrnlOpen ){
47252              sqlite3OsClose(pPager->jfd);
47253            }
47254            *pExists = (first!=0);
47255          }else if( rc==SQLITE_CANTOPEN ){
47256            /* If we cannot open the rollback journal file in order to see if
47257            ** it has a zero header, that might be due to an I/O error, or
47258            ** it might be due to the race condition described above and in
47259            ** ticket #3883.  Either way, assume that the journal is hot.
47260            ** This might be a false positive.  But if it is, then the
47261            ** automatic journal playback and recovery mechanism will deal
47262            ** with it under an EXCLUSIVE lock where we do not need to
47263            ** worry so much with race conditions.
47264            */
47265            *pExists = 1;
47266            rc = SQLITE_OK;
47267          }
47268        }
47269      }
47270    }
47271  }
47272
47273  return rc;
47274}
47275
47276/*
47277** This function is called to obtain a shared lock on the database file.
47278** It is illegal to call sqlite3PagerAcquire() until after this function
47279** has been successfully called. If a shared-lock is already held when
47280** this function is called, it is a no-op.
47281**
47282** The following operations are also performed by this function.
47283**
47284**   1) If the pager is currently in PAGER_OPEN state (no lock held
47285**      on the database file), then an attempt is made to obtain a
47286**      SHARED lock on the database file. Immediately after obtaining
47287**      the SHARED lock, the file-system is checked for a hot-journal,
47288**      which is played back if present. Following any hot-journal
47289**      rollback, the contents of the cache are validated by checking
47290**      the 'change-counter' field of the database file header and
47291**      discarded if they are found to be invalid.
47292**
47293**   2) If the pager is running in exclusive-mode, and there are currently
47294**      no outstanding references to any pages, and is in the error state,
47295**      then an attempt is made to clear the error state by discarding
47296**      the contents of the page cache and rolling back any open journal
47297**      file.
47298**
47299** If everything is successful, SQLITE_OK is returned. If an IO error
47300** occurs while locking the database, checking for a hot-journal file or
47301** rolling back a journal file, the IO error code is returned.
47302*/
47303SQLITE_PRIVATE int sqlite3PagerSharedLock(Pager *pPager){
47304  int rc = SQLITE_OK;                /* Return code */
47305
47306  /* This routine is only called from b-tree and only when there are no
47307  ** outstanding pages. This implies that the pager state should either
47308  ** be OPEN or READER. READER is only possible if the pager is or was in
47309  ** exclusive access mode.
47310  */
47311  assert( sqlite3PcacheRefCount(pPager->pPCache)==0 );
47312  assert( assert_pager_state(pPager) );
47313  assert( pPager->eState==PAGER_OPEN || pPager->eState==PAGER_READER );
47314  if( NEVER(MEMDB && pPager->errCode) ){ return pPager->errCode; }
47315
47316  if( !pagerUseWal(pPager) && pPager->eState==PAGER_OPEN ){
47317    int bHotJournal = 1;          /* True if there exists a hot journal-file */
47318
47319    assert( !MEMDB );
47320
47321    rc = pager_wait_on_lock(pPager, SHARED_LOCK);
47322    if( rc!=SQLITE_OK ){
47323      assert( pPager->eLock==NO_LOCK || pPager->eLock==UNKNOWN_LOCK );
47324      goto failed;
47325    }
47326
47327    /* If a journal file exists, and there is no RESERVED lock on the
47328    ** database file, then it either needs to be played back or deleted.
47329    */
47330    if( pPager->eLock<=SHARED_LOCK ){
47331      rc = hasHotJournal(pPager, &bHotJournal);
47332    }
47333    if( rc!=SQLITE_OK ){
47334      goto failed;
47335    }
47336    if( bHotJournal ){
47337      if( pPager->readOnly ){
47338        rc = SQLITE_READONLY_ROLLBACK;
47339        goto failed;
47340      }
47341
47342      /* Get an EXCLUSIVE lock on the database file. At this point it is
47343      ** important that a RESERVED lock is not obtained on the way to the
47344      ** EXCLUSIVE lock. If it were, another process might open the
47345      ** database file, detect the RESERVED lock, and conclude that the
47346      ** database is safe to read while this process is still rolling the
47347      ** hot-journal back.
47348      **
47349      ** Because the intermediate RESERVED lock is not requested, any
47350      ** other process attempting to access the database file will get to
47351      ** this point in the code and fail to obtain its own EXCLUSIVE lock
47352      ** on the database file.
47353      **
47354      ** Unless the pager is in locking_mode=exclusive mode, the lock is
47355      ** downgraded to SHARED_LOCK before this function returns.
47356      */
47357      rc = pagerLockDb(pPager, EXCLUSIVE_LOCK);
47358      if( rc!=SQLITE_OK ){
47359        goto failed;
47360      }
47361
47362      /* If it is not already open and the file exists on disk, open the
47363      ** journal for read/write access. Write access is required because
47364      ** in exclusive-access mode the file descriptor will be kept open
47365      ** and possibly used for a transaction later on. Also, write-access
47366      ** is usually required to finalize the journal in journal_mode=persist
47367      ** mode (and also for journal_mode=truncate on some systems).
47368      **
47369      ** If the journal does not exist, it usually means that some
47370      ** other connection managed to get in and roll it back before
47371      ** this connection obtained the exclusive lock above. Or, it
47372      ** may mean that the pager was in the error-state when this
47373      ** function was called and the journal file does not exist.
47374      */
47375      if( !isOpen(pPager->jfd) ){
47376        sqlite3_vfs * const pVfs = pPager->pVfs;
47377        int bExists;              /* True if journal file exists */
47378        rc = sqlite3OsAccess(
47379            pVfs, pPager->zJournal, SQLITE_ACCESS_EXISTS, &bExists);
47380        if( rc==SQLITE_OK && bExists ){
47381          int fout = 0;
47382          int f = SQLITE_OPEN_READWRITE|SQLITE_OPEN_MAIN_JOURNAL;
47383          assert( !pPager->tempFile );
47384          rc = sqlite3OsOpen(pVfs, pPager->zJournal, pPager->jfd, f, &fout);
47385          assert( rc!=SQLITE_OK || isOpen(pPager->jfd) );
47386          if( rc==SQLITE_OK && fout&SQLITE_OPEN_READONLY ){
47387            rc = SQLITE_CANTOPEN_BKPT;
47388            sqlite3OsClose(pPager->jfd);
47389          }
47390        }
47391      }
47392
47393      /* Playback and delete the journal.  Drop the database write
47394      ** lock and reacquire the read lock. Purge the cache before
47395      ** playing back the hot-journal so that we don't end up with
47396      ** an inconsistent cache.  Sync the hot journal before playing
47397      ** it back since the process that crashed and left the hot journal
47398      ** probably did not sync it and we are required to always sync
47399      ** the journal before playing it back.
47400      */
47401      if( isOpen(pPager->jfd) ){
47402        assert( rc==SQLITE_OK );
47403        rc = pagerSyncHotJournal(pPager);
47404        if( rc==SQLITE_OK ){
47405          rc = pager_playback(pPager, 1);
47406          pPager->eState = PAGER_OPEN;
47407        }
47408      }else if( !pPager->exclusiveMode ){
47409        pagerUnlockDb(pPager, SHARED_LOCK);
47410      }
47411
47412      if( rc!=SQLITE_OK ){
47413        /* This branch is taken if an error occurs while trying to open
47414        ** or roll back a hot-journal while holding an EXCLUSIVE lock. The
47415        ** pager_unlock() routine will be called before returning to unlock
47416        ** the file. If the unlock attempt fails, then Pager.eLock must be
47417        ** set to UNKNOWN_LOCK (see the comment above the #define for
47418        ** UNKNOWN_LOCK above for an explanation).
47419        **
47420        ** In order to get pager_unlock() to do this, set Pager.eState to
47421        ** PAGER_ERROR now. This is not actually counted as a transition
47422        ** to ERROR state in the state diagram at the top of this file,
47423        ** since we know that the same call to pager_unlock() will very
47424        ** shortly transition the pager object to the OPEN state. Calling
47425        ** assert_pager_state() would fail now, as it should not be possible
47426        ** to be in ERROR state when there are zero outstanding page
47427        ** references.
47428        */
47429        pager_error(pPager, rc);
47430        goto failed;
47431      }
47432
47433      assert( pPager->eState==PAGER_OPEN );
47434      assert( (pPager->eLock==SHARED_LOCK)
47435           || (pPager->exclusiveMode && pPager->eLock>SHARED_LOCK)
47436      );
47437    }
47438
47439    if( !pPager->tempFile && pPager->hasBeenUsed ){
47440      /* The shared-lock has just been acquired then check to
47441      ** see if the database has been modified.  If the database has changed,
47442      ** flush the cache.  The pPager->hasBeenUsed flag prevents this from
47443      ** occurring on the very first access to a file, in order to save a
47444      ** single unnecessary sqlite3OsRead() call at the start-up.
47445      **
47446      ** Database changes are detected by looking at 15 bytes beginning
47447      ** at offset 24 into the file.  The first 4 of these 16 bytes are
47448      ** a 32-bit counter that is incremented with each change.  The
47449      ** other bytes change randomly with each file change when
47450      ** a codec is in use.
47451      **
47452      ** There is a vanishingly small chance that a change will not be
47453      ** detected.  The chance of an undetected change is so small that
47454      ** it can be neglected.
47455      */
47456      Pgno nPage = 0;
47457      char dbFileVers[sizeof(pPager->dbFileVers)];
47458
47459      rc = pagerPagecount(pPager, &nPage);
47460      if( rc ) goto failed;
47461
47462      if( nPage>0 ){
47463        IOTRACE(("CKVERS %p %d\n", pPager, sizeof(dbFileVers)));
47464        rc = sqlite3OsRead(pPager->fd, &dbFileVers, sizeof(dbFileVers), 24);
47465        if( rc!=SQLITE_OK && rc!=SQLITE_IOERR_SHORT_READ ){
47466          goto failed;
47467        }
47468      }else{
47469        memset(dbFileVers, 0, sizeof(dbFileVers));
47470      }
47471
47472      if( memcmp(pPager->dbFileVers, dbFileVers, sizeof(dbFileVers))!=0 ){
47473        pager_reset(pPager);
47474
47475        /* Unmap the database file. It is possible that external processes
47476        ** may have truncated the database file and then extended it back
47477        ** to its original size while this process was not holding a lock.
47478        ** In this case there may exist a Pager.pMap mapping that appears
47479        ** to be the right size but is not actually valid. Avoid this
47480        ** possibility by unmapping the db here. */
47481        if( USEFETCH(pPager) ){
47482          sqlite3OsUnfetch(pPager->fd, 0, 0);
47483        }
47484      }
47485    }
47486
47487    /* If there is a WAL file in the file-system, open this database in WAL
47488    ** mode. Otherwise, the following function call is a no-op.
47489    */
47490    rc = pagerOpenWalIfPresent(pPager);
47491#ifndef SQLITE_OMIT_WAL
47492    assert( pPager->pWal==0 || rc==SQLITE_OK );
47493#endif
47494  }
47495
47496  if( pagerUseWal(pPager) ){
47497    assert( rc==SQLITE_OK );
47498    rc = pagerBeginReadTransaction(pPager);
47499  }
47500
47501  if( pPager->eState==PAGER_OPEN && rc==SQLITE_OK ){
47502    rc = pagerPagecount(pPager, &pPager->dbSize);
47503  }
47504
47505 failed:
47506  if( rc!=SQLITE_OK ){
47507    assert( !MEMDB );
47508    pager_unlock(pPager);
47509    assert( pPager->eState==PAGER_OPEN );
47510  }else{
47511    pPager->eState = PAGER_READER;
47512  }
47513  return rc;
47514}
47515
47516/*
47517** If the reference count has reached zero, rollback any active
47518** transaction and unlock the pager.
47519**
47520** Except, in locking_mode=EXCLUSIVE when there is nothing to in
47521** the rollback journal, the unlock is not performed and there is
47522** nothing to rollback, so this routine is a no-op.
47523*/
47524static void pagerUnlockIfUnused(Pager *pPager){
47525  if( pPager->nMmapOut==0 && (sqlite3PcacheRefCount(pPager->pPCache)==0) ){
47526    pagerUnlockAndRollback(pPager);
47527  }
47528}
47529
47530/*
47531** Acquire a reference to page number pgno in pager pPager (a page
47532** reference has type DbPage*). If the requested reference is
47533** successfully obtained, it is copied to *ppPage and SQLITE_OK returned.
47534**
47535** If the requested page is already in the cache, it is returned.
47536** Otherwise, a new page object is allocated and populated with data
47537** read from the database file. In some cases, the pcache module may
47538** choose not to allocate a new page object and may reuse an existing
47539** object with no outstanding references.
47540**
47541** The extra data appended to a page is always initialized to zeros the
47542** first time a page is loaded into memory. If the page requested is
47543** already in the cache when this function is called, then the extra
47544** data is left as it was when the page object was last used.
47545**
47546** If the database image is smaller than the requested page or if a
47547** non-zero value is passed as the noContent parameter and the
47548** requested page is not already stored in the cache, then no
47549** actual disk read occurs. In this case the memory image of the
47550** page is initialized to all zeros.
47551**
47552** If noContent is true, it means that we do not care about the contents
47553** of the page. This occurs in two scenarios:
47554**
47555**   a) When reading a free-list leaf page from the database, and
47556**
47557**   b) When a savepoint is being rolled back and we need to load
47558**      a new page into the cache to be filled with the data read
47559**      from the savepoint journal.
47560**
47561** If noContent is true, then the data returned is zeroed instead of
47562** being read from the database. Additionally, the bits corresponding
47563** to pgno in Pager.pInJournal (bitvec of pages already written to the
47564** journal file) and the PagerSavepoint.pInSavepoint bitvecs of any open
47565** savepoints are set. This means if the page is made writable at any
47566** point in the future, using a call to sqlite3PagerWrite(), its contents
47567** will not be journaled. This saves IO.
47568**
47569** The acquisition might fail for several reasons.  In all cases,
47570** an appropriate error code is returned and *ppPage is set to NULL.
47571**
47572** See also sqlite3PagerLookup().  Both this routine and Lookup() attempt
47573** to find a page in the in-memory cache first.  If the page is not already
47574** in memory, this routine goes to disk to read it in whereas Lookup()
47575** just returns 0.  This routine acquires a read-lock the first time it
47576** has to go to disk, and could also playback an old journal if necessary.
47577** Since Lookup() never goes to disk, it never has to deal with locks
47578** or journal files.
47579*/
47580SQLITE_PRIVATE int sqlite3PagerAcquire(
47581  Pager *pPager,      /* The pager open on the database file */
47582  Pgno pgno,          /* Page number to fetch */
47583  DbPage **ppPage,    /* Write a pointer to the page here */
47584  int flags           /* PAGER_GET_XXX flags */
47585){
47586  int rc = SQLITE_OK;
47587  PgHdr *pPg = 0;
47588  u32 iFrame = 0;                 /* Frame to read from WAL file */
47589  const int noContent = (flags & PAGER_GET_NOCONTENT);
47590
47591  /* It is acceptable to use a read-only (mmap) page for any page except
47592  ** page 1 if there is no write-transaction open or the ACQUIRE_READONLY
47593  ** flag was specified by the caller. And so long as the db is not a
47594  ** temporary or in-memory database.  */
47595  const int bMmapOk = (pgno!=1 && USEFETCH(pPager)
47596   && (pPager->eState==PAGER_READER || (flags & PAGER_GET_READONLY))
47597#ifdef SQLITE_HAS_CODEC
47598   && pPager->xCodec==0
47599#endif
47600  );
47601
47602  assert( pPager->eState>=PAGER_READER );
47603  assert( assert_pager_state(pPager) );
47604  assert( noContent==0 || bMmapOk==0 );
47605
47606  if( pgno==0 ){
47607    return SQLITE_CORRUPT_BKPT;
47608  }
47609  pPager->hasBeenUsed = 1;
47610
47611  /* If the pager is in the error state, return an error immediately.
47612  ** Otherwise, request the page from the PCache layer. */
47613  if( pPager->errCode!=SQLITE_OK ){
47614    rc = pPager->errCode;
47615  }else{
47616    if( bMmapOk && pagerUseWal(pPager) ){
47617      rc = sqlite3WalFindFrame(pPager->pWal, pgno, &iFrame);
47618      if( rc!=SQLITE_OK ) goto pager_acquire_err;
47619    }
47620
47621    if( bMmapOk && iFrame==0 ){
47622      void *pData = 0;
47623
47624      rc = sqlite3OsFetch(pPager->fd,
47625          (i64)(pgno-1) * pPager->pageSize, pPager->pageSize, &pData
47626      );
47627
47628      if( rc==SQLITE_OK && pData ){
47629        if( pPager->eState>PAGER_READER ){
47630          pPg = sqlite3PagerLookup(pPager, pgno);
47631        }
47632        if( pPg==0 ){
47633          rc = pagerAcquireMapPage(pPager, pgno, pData, &pPg);
47634        }else{
47635          sqlite3OsUnfetch(pPager->fd, (i64)(pgno-1)*pPager->pageSize, pData);
47636        }
47637        if( pPg ){
47638          assert( rc==SQLITE_OK );
47639          *ppPage = pPg;
47640          return SQLITE_OK;
47641        }
47642      }
47643      if( rc!=SQLITE_OK ){
47644        goto pager_acquire_err;
47645      }
47646    }
47647
47648    {
47649      sqlite3_pcache_page *pBase;
47650      pBase = sqlite3PcacheFetch(pPager->pPCache, pgno, 3);
47651      if( pBase==0 ){
47652        rc = sqlite3PcacheFetchStress(pPager->pPCache, pgno, &pBase);
47653        if( rc!=SQLITE_OK ) goto pager_acquire_err;
47654        if( pBase==0 ){
47655          pPg = *ppPage = 0;
47656          rc = SQLITE_NOMEM;
47657          goto pager_acquire_err;
47658        }
47659      }
47660      pPg = *ppPage = sqlite3PcacheFetchFinish(pPager->pPCache, pgno, pBase);
47661      assert( pPg!=0 );
47662    }
47663  }
47664
47665  if( rc!=SQLITE_OK ){
47666    /* Either the call to sqlite3PcacheFetch() returned an error or the
47667    ** pager was already in the error-state when this function was called.
47668    ** Set pPg to 0 and jump to the exception handler.  */
47669    pPg = 0;
47670    goto pager_acquire_err;
47671  }
47672  assert( pPg==(*ppPage) );
47673  assert( pPg->pgno==pgno );
47674  assert( pPg->pPager==pPager || pPg->pPager==0 );
47675
47676  if( pPg->pPager && !noContent ){
47677    /* In this case the pcache already contains an initialized copy of
47678    ** the page. Return without further ado.  */
47679    assert( pgno<=PAGER_MAX_PGNO && pgno!=PAGER_MJ_PGNO(pPager) );
47680    pPager->aStat[PAGER_STAT_HIT]++;
47681    return SQLITE_OK;
47682
47683  }else{
47684    /* The pager cache has created a new page. Its content needs to
47685    ** be initialized.  */
47686
47687    pPg->pPager = pPager;
47688
47689    /* The maximum page number is 2^31. Return SQLITE_CORRUPT if a page
47690    ** number greater than this, or the unused locking-page, is requested. */
47691    if( pgno>PAGER_MAX_PGNO || pgno==PAGER_MJ_PGNO(pPager) ){
47692      rc = SQLITE_CORRUPT_BKPT;
47693      goto pager_acquire_err;
47694    }
47695
47696    if( MEMDB || pPager->dbSize<pgno || noContent || !isOpen(pPager->fd) ){
47697      if( pgno>pPager->mxPgno ){
47698        rc = SQLITE_FULL;
47699        goto pager_acquire_err;
47700      }
47701      if( noContent ){
47702        /* Failure to set the bits in the InJournal bit-vectors is benign.
47703        ** It merely means that we might do some extra work to journal a
47704        ** page that does not need to be journaled.  Nevertheless, be sure
47705        ** to test the case where a malloc error occurs while trying to set
47706        ** a bit in a bit vector.
47707        */
47708        sqlite3BeginBenignMalloc();
47709        if( pgno<=pPager->dbOrigSize ){
47710          TESTONLY( rc = ) sqlite3BitvecSet(pPager->pInJournal, pgno);
47711          testcase( rc==SQLITE_NOMEM );
47712        }
47713        TESTONLY( rc = ) addToSavepointBitvecs(pPager, pgno);
47714        testcase( rc==SQLITE_NOMEM );
47715        sqlite3EndBenignMalloc();
47716      }
47717      memset(pPg->pData, 0, pPager->pageSize);
47718      IOTRACE(("ZERO %p %d\n", pPager, pgno));
47719    }else{
47720      if( pagerUseWal(pPager) && bMmapOk==0 ){
47721        rc = sqlite3WalFindFrame(pPager->pWal, pgno, &iFrame);
47722        if( rc!=SQLITE_OK ) goto pager_acquire_err;
47723      }
47724      assert( pPg->pPager==pPager );
47725      pPager->aStat[PAGER_STAT_MISS]++;
47726      rc = readDbPage(pPg, iFrame);
47727      if( rc!=SQLITE_OK ){
47728        goto pager_acquire_err;
47729      }
47730    }
47731    pager_set_pagehash(pPg);
47732  }
47733
47734  return SQLITE_OK;
47735
47736pager_acquire_err:
47737  assert( rc!=SQLITE_OK );
47738  if( pPg ){
47739    sqlite3PcacheDrop(pPg);
47740  }
47741  pagerUnlockIfUnused(pPager);
47742
47743  *ppPage = 0;
47744  return rc;
47745}
47746
47747/*
47748** Acquire a page if it is already in the in-memory cache.  Do
47749** not read the page from disk.  Return a pointer to the page,
47750** or 0 if the page is not in cache.
47751**
47752** See also sqlite3PagerGet().  The difference between this routine
47753** and sqlite3PagerGet() is that _get() will go to the disk and read
47754** in the page if the page is not already in cache.  This routine
47755** returns NULL if the page is not in cache or if a disk I/O error
47756** has ever happened.
47757*/
47758SQLITE_PRIVATE DbPage *sqlite3PagerLookup(Pager *pPager, Pgno pgno){
47759  sqlite3_pcache_page *pPage;
47760  assert( pPager!=0 );
47761  assert( pgno!=0 );
47762  assert( pPager->pPCache!=0 );
47763  pPage = sqlite3PcacheFetch(pPager->pPCache, pgno, 0);
47764  assert( pPage==0 || pPager->hasBeenUsed );
47765  if( pPage==0 ) return 0;
47766  return sqlite3PcacheFetchFinish(pPager->pPCache, pgno, pPage);
47767}
47768
47769/*
47770** Release a page reference.
47771**
47772** If the number of references to the page drop to zero, then the
47773** page is added to the LRU list.  When all references to all pages
47774** are released, a rollback occurs and the lock on the database is
47775** removed.
47776*/
47777SQLITE_PRIVATE void sqlite3PagerUnrefNotNull(DbPage *pPg){
47778  Pager *pPager;
47779  assert( pPg!=0 );
47780  pPager = pPg->pPager;
47781  if( pPg->flags & PGHDR_MMAP ){
47782    pagerReleaseMapPage(pPg);
47783  }else{
47784    sqlite3PcacheRelease(pPg);
47785  }
47786  pagerUnlockIfUnused(pPager);
47787}
47788SQLITE_PRIVATE void sqlite3PagerUnref(DbPage *pPg){
47789  if( pPg ) sqlite3PagerUnrefNotNull(pPg);
47790}
47791
47792/*
47793** This function is called at the start of every write transaction.
47794** There must already be a RESERVED or EXCLUSIVE lock on the database
47795** file when this routine is called.
47796**
47797** Open the journal file for pager pPager and write a journal header
47798** to the start of it. If there are active savepoints, open the sub-journal
47799** as well. This function is only used when the journal file is being
47800** opened to write a rollback log for a transaction. It is not used
47801** when opening a hot journal file to roll it back.
47802**
47803** If the journal file is already open (as it may be in exclusive mode),
47804** then this function just writes a journal header to the start of the
47805** already open file.
47806**
47807** Whether or not the journal file is opened by this function, the
47808** Pager.pInJournal bitvec structure is allocated.
47809**
47810** Return SQLITE_OK if everything is successful. Otherwise, return
47811** SQLITE_NOMEM if the attempt to allocate Pager.pInJournal fails, or
47812** an IO error code if opening or writing the journal file fails.
47813*/
47814static int pager_open_journal(Pager *pPager){
47815  int rc = SQLITE_OK;                        /* Return code */
47816  sqlite3_vfs * const pVfs = pPager->pVfs;   /* Local cache of vfs pointer */
47817
47818  assert( pPager->eState==PAGER_WRITER_LOCKED );
47819  assert( assert_pager_state(pPager) );
47820  assert( pPager->pInJournal==0 );
47821
47822  /* If already in the error state, this function is a no-op.  But on
47823  ** the other hand, this routine is never called if we are already in
47824  ** an error state. */
47825  if( NEVER(pPager->errCode) ) return pPager->errCode;
47826
47827  if( !pagerUseWal(pPager) && pPager->journalMode!=PAGER_JOURNALMODE_OFF ){
47828    pPager->pInJournal = sqlite3BitvecCreate(pPager->dbSize);
47829    if( pPager->pInJournal==0 ){
47830      return SQLITE_NOMEM;
47831    }
47832
47833    /* Open the journal file if it is not already open. */
47834    if( !isOpen(pPager->jfd) ){
47835      if( pPager->journalMode==PAGER_JOURNALMODE_MEMORY ){
47836        sqlite3MemJournalOpen(pPager->jfd);
47837      }else{
47838        const int flags =                   /* VFS flags to open journal file */
47839          SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|
47840          (pPager->tempFile ?
47841            (SQLITE_OPEN_DELETEONCLOSE|SQLITE_OPEN_TEMP_JOURNAL):
47842            (SQLITE_OPEN_MAIN_JOURNAL)
47843          );
47844
47845        /* Verify that the database still has the same name as it did when
47846        ** it was originally opened. */
47847        rc = databaseIsUnmoved(pPager);
47848        if( rc==SQLITE_OK ){
47849#ifdef SQLITE_ENABLE_ATOMIC_WRITE
47850          rc = sqlite3JournalOpen(
47851              pVfs, pPager->zJournal, pPager->jfd, flags, jrnlBufferSize(pPager)
47852          );
47853#else
47854          rc = sqlite3OsOpen(pVfs, pPager->zJournal, pPager->jfd, flags, 0);
47855#endif
47856        }
47857      }
47858      assert( rc!=SQLITE_OK || isOpen(pPager->jfd) );
47859    }
47860
47861
47862    /* Write the first journal header to the journal file and open
47863    ** the sub-journal if necessary.
47864    */
47865    if( rc==SQLITE_OK ){
47866      /* TODO: Check if all of these are really required. */
47867      pPager->nRec = 0;
47868      pPager->journalOff = 0;
47869      pPager->setMaster = 0;
47870      pPager->journalHdr = 0;
47871      rc = writeJournalHdr(pPager);
47872    }
47873  }
47874
47875  if( rc!=SQLITE_OK ){
47876    sqlite3BitvecDestroy(pPager->pInJournal);
47877    pPager->pInJournal = 0;
47878  }else{
47879    assert( pPager->eState==PAGER_WRITER_LOCKED );
47880    pPager->eState = PAGER_WRITER_CACHEMOD;
47881  }
47882
47883  return rc;
47884}
47885
47886/*
47887** Begin a write-transaction on the specified pager object. If a
47888** write-transaction has already been opened, this function is a no-op.
47889**
47890** If the exFlag argument is false, then acquire at least a RESERVED
47891** lock on the database file. If exFlag is true, then acquire at least
47892** an EXCLUSIVE lock. If such a lock is already held, no locking
47893** functions need be called.
47894**
47895** If the subjInMemory argument is non-zero, then any sub-journal opened
47896** within this transaction will be opened as an in-memory file. This
47897** has no effect if the sub-journal is already opened (as it may be when
47898** running in exclusive mode) or if the transaction does not require a
47899** sub-journal. If the subjInMemory argument is zero, then any required
47900** sub-journal is implemented in-memory if pPager is an in-memory database,
47901** or using a temporary file otherwise.
47902*/
47903SQLITE_PRIVATE int sqlite3PagerBegin(Pager *pPager, int exFlag, int subjInMemory){
47904  int rc = SQLITE_OK;
47905
47906  if( pPager->errCode ) return pPager->errCode;
47907  assert( pPager->eState>=PAGER_READER && pPager->eState<PAGER_ERROR );
47908  pPager->subjInMemory = (u8)subjInMemory;
47909
47910  if( ALWAYS(pPager->eState==PAGER_READER) ){
47911    assert( pPager->pInJournal==0 );
47912
47913    if( pagerUseWal(pPager) ){
47914      /* If the pager is configured to use locking_mode=exclusive, and an
47915      ** exclusive lock on the database is not already held, obtain it now.
47916      */
47917      if( pPager->exclusiveMode && sqlite3WalExclusiveMode(pPager->pWal, -1) ){
47918        rc = pagerLockDb(pPager, EXCLUSIVE_LOCK);
47919        if( rc!=SQLITE_OK ){
47920          return rc;
47921        }
47922        sqlite3WalExclusiveMode(pPager->pWal, 1);
47923      }
47924
47925      /* Grab the write lock on the log file. If successful, upgrade to
47926      ** PAGER_RESERVED state. Otherwise, return an error code to the caller.
47927      ** The busy-handler is not invoked if another connection already
47928      ** holds the write-lock. If possible, the upper layer will call it.
47929      */
47930      rc = sqlite3WalBeginWriteTransaction(pPager->pWal);
47931    }else{
47932      /* Obtain a RESERVED lock on the database file. If the exFlag parameter
47933      ** is true, then immediately upgrade this to an EXCLUSIVE lock. The
47934      ** busy-handler callback can be used when upgrading to the EXCLUSIVE
47935      ** lock, but not when obtaining the RESERVED lock.
47936      */
47937      rc = pagerLockDb(pPager, RESERVED_LOCK);
47938      if( rc==SQLITE_OK && exFlag ){
47939        rc = pager_wait_on_lock(pPager, EXCLUSIVE_LOCK);
47940      }
47941    }
47942
47943    if( rc==SQLITE_OK ){
47944      /* Change to WRITER_LOCKED state.
47945      **
47946      ** WAL mode sets Pager.eState to PAGER_WRITER_LOCKED or CACHEMOD
47947      ** when it has an open transaction, but never to DBMOD or FINISHED.
47948      ** This is because in those states the code to roll back savepoint
47949      ** transactions may copy data from the sub-journal into the database
47950      ** file as well as into the page cache. Which would be incorrect in
47951      ** WAL mode.
47952      */
47953      pPager->eState = PAGER_WRITER_LOCKED;
47954      pPager->dbHintSize = pPager->dbSize;
47955      pPager->dbFileSize = pPager->dbSize;
47956      pPager->dbOrigSize = pPager->dbSize;
47957      pPager->journalOff = 0;
47958    }
47959
47960    assert( rc==SQLITE_OK || pPager->eState==PAGER_READER );
47961    assert( rc!=SQLITE_OK || pPager->eState==PAGER_WRITER_LOCKED );
47962    assert( assert_pager_state(pPager) );
47963  }
47964
47965  PAGERTRACE(("TRANSACTION %d\n", PAGERID(pPager)));
47966  return rc;
47967}
47968
47969/*
47970** Write page pPg onto the end of the rollback journal.
47971*/
47972static SQLITE_NOINLINE int pagerAddPageToRollbackJournal(PgHdr *pPg){
47973  Pager *pPager = pPg->pPager;
47974  int rc;
47975  u32 cksum;
47976  char *pData2;
47977  i64 iOff = pPager->journalOff;
47978
47979  /* We should never write to the journal file the page that
47980  ** contains the database locks.  The following assert verifies
47981  ** that we do not. */
47982  assert( pPg->pgno!=PAGER_MJ_PGNO(pPager) );
47983
47984  assert( pPager->journalHdr<=pPager->journalOff );
47985  CODEC2(pPager, pPg->pData, pPg->pgno, 7, return SQLITE_NOMEM, pData2);
47986  cksum = pager_cksum(pPager, (u8*)pData2);
47987
47988  /* Even if an IO or diskfull error occurs while journalling the
47989  ** page in the block above, set the need-sync flag for the page.
47990  ** Otherwise, when the transaction is rolled back, the logic in
47991  ** playback_one_page() will think that the page needs to be restored
47992  ** in the database file. And if an IO error occurs while doing so,
47993  ** then corruption may follow.
47994  */
47995  pPg->flags |= PGHDR_NEED_SYNC;
47996
47997  rc = write32bits(pPager->jfd, iOff, pPg->pgno);
47998  if( rc!=SQLITE_OK ) return rc;
47999  rc = sqlite3OsWrite(pPager->jfd, pData2, pPager->pageSize, iOff+4);
48000  if( rc!=SQLITE_OK ) return rc;
48001  rc = write32bits(pPager->jfd, iOff+pPager->pageSize+4, cksum);
48002  if( rc!=SQLITE_OK ) return rc;
48003
48004  IOTRACE(("JOUT %p %d %lld %d\n", pPager, pPg->pgno,
48005           pPager->journalOff, pPager->pageSize));
48006  PAGER_INCR(sqlite3_pager_writej_count);
48007  PAGERTRACE(("JOURNAL %d page %d needSync=%d hash(%08x)\n",
48008       PAGERID(pPager), pPg->pgno,
48009       ((pPg->flags&PGHDR_NEED_SYNC)?1:0), pager_pagehash(pPg)));
48010
48011  pPager->journalOff += 8 + pPager->pageSize;
48012  pPager->nRec++;
48013  assert( pPager->pInJournal!=0 );
48014  rc = sqlite3BitvecSet(pPager->pInJournal, pPg->pgno);
48015  testcase( rc==SQLITE_NOMEM );
48016  assert( rc==SQLITE_OK || rc==SQLITE_NOMEM );
48017  rc |= addToSavepointBitvecs(pPager, pPg->pgno);
48018  assert( rc==SQLITE_OK || rc==SQLITE_NOMEM );
48019  return rc;
48020}
48021
48022/*
48023** Mark a single data page as writeable. The page is written into the
48024** main journal or sub-journal as required. If the page is written into
48025** one of the journals, the corresponding bit is set in the
48026** Pager.pInJournal bitvec and the PagerSavepoint.pInSavepoint bitvecs
48027** of any open savepoints as appropriate.
48028*/
48029static int pager_write(PgHdr *pPg){
48030  Pager *pPager = pPg->pPager;
48031  int rc = SQLITE_OK;
48032
48033  /* This routine is not called unless a write-transaction has already
48034  ** been started. The journal file may or may not be open at this point.
48035  ** It is never called in the ERROR state.
48036  */
48037  assert( pPager->eState==PAGER_WRITER_LOCKED
48038       || pPager->eState==PAGER_WRITER_CACHEMOD
48039       || pPager->eState==PAGER_WRITER_DBMOD
48040  );
48041  assert( assert_pager_state(pPager) );
48042  assert( pPager->errCode==0 );
48043  assert( pPager->readOnly==0 );
48044  CHECK_PAGE(pPg);
48045
48046  /* The journal file needs to be opened. Higher level routines have already
48047  ** obtained the necessary locks to begin the write-transaction, but the
48048  ** rollback journal might not yet be open. Open it now if this is the case.
48049  **
48050  ** This is done before calling sqlite3PcacheMakeDirty() on the page.
48051  ** Otherwise, if it were done after calling sqlite3PcacheMakeDirty(), then
48052  ** an error might occur and the pager would end up in WRITER_LOCKED state
48053  ** with pages marked as dirty in the cache.
48054  */
48055  if( pPager->eState==PAGER_WRITER_LOCKED ){
48056    rc = pager_open_journal(pPager);
48057    if( rc!=SQLITE_OK ) return rc;
48058  }
48059  assert( pPager->eState>=PAGER_WRITER_CACHEMOD );
48060  assert( assert_pager_state(pPager) );
48061
48062  /* Mark the page that is about to be modified as dirty. */
48063  sqlite3PcacheMakeDirty(pPg);
48064
48065  /* If a rollback journal is in use, them make sure the page that is about
48066  ** to change is in the rollback journal, or if the page is a new page off
48067  ** then end of the file, make sure it is marked as PGHDR_NEED_SYNC.
48068  */
48069  assert( (pPager->pInJournal!=0) == isOpen(pPager->jfd) );
48070  if( pPager->pInJournal!=0
48071   && sqlite3BitvecTestNotNull(pPager->pInJournal, pPg->pgno)==0
48072  ){
48073    assert( pagerUseWal(pPager)==0 );
48074    if( pPg->pgno<=pPager->dbOrigSize ){
48075      rc = pagerAddPageToRollbackJournal(pPg);
48076      if( rc!=SQLITE_OK ){
48077        return rc;
48078      }
48079    }else{
48080      if( pPager->eState!=PAGER_WRITER_DBMOD ){
48081        pPg->flags |= PGHDR_NEED_SYNC;
48082      }
48083      PAGERTRACE(("APPEND %d page %d needSync=%d\n",
48084              PAGERID(pPager), pPg->pgno,
48085             ((pPg->flags&PGHDR_NEED_SYNC)?1:0)));
48086    }
48087  }
48088
48089  /* The PGHDR_DIRTY bit is set above when the page was added to the dirty-list
48090  ** and before writing the page into the rollback journal.  Wait until now,
48091  ** after the page has been successfully journalled, before setting the
48092  ** PGHDR_WRITEABLE bit that indicates that the page can be safely modified.
48093  */
48094  pPg->flags |= PGHDR_WRITEABLE;
48095
48096  /* If the statement journal is open and the page is not in it,
48097  ** then write the page into the statement journal.
48098  */
48099  if( pPager->nSavepoint>0 ){
48100    rc = subjournalPageIfRequired(pPg);
48101  }
48102
48103  /* Update the database size and return. */
48104  if( pPager->dbSize<pPg->pgno ){
48105    pPager->dbSize = pPg->pgno;
48106  }
48107  return rc;
48108}
48109
48110/*
48111** This is a variant of sqlite3PagerWrite() that runs when the sector size
48112** is larger than the page size.  SQLite makes the (reasonable) assumption that
48113** all bytes of a sector are written together by hardware.  Hence, all bytes of
48114** a sector need to be journalled in case of a power loss in the middle of
48115** a write.
48116**
48117** Usually, the sector size is less than or equal to the page size, in which
48118** case pages can be individually written.  This routine only runs in the
48119** exceptional case where the page size is smaller than the sector size.
48120*/
48121static SQLITE_NOINLINE int pagerWriteLargeSector(PgHdr *pPg){
48122  int rc = SQLITE_OK;          /* Return code */
48123  Pgno nPageCount;             /* Total number of pages in database file */
48124  Pgno pg1;                    /* First page of the sector pPg is located on. */
48125  int nPage = 0;               /* Number of pages starting at pg1 to journal */
48126  int ii;                      /* Loop counter */
48127  int needSync = 0;            /* True if any page has PGHDR_NEED_SYNC */
48128  Pager *pPager = pPg->pPager; /* The pager that owns pPg */
48129  Pgno nPagePerSector = (pPager->sectorSize/pPager->pageSize);
48130
48131  /* Set the doNotSpill NOSYNC bit to 1. This is because we cannot allow
48132  ** a journal header to be written between the pages journaled by
48133  ** this function.
48134  */
48135  assert( !MEMDB );
48136  assert( (pPager->doNotSpill & SPILLFLAG_NOSYNC)==0 );
48137  pPager->doNotSpill |= SPILLFLAG_NOSYNC;
48138
48139  /* This trick assumes that both the page-size and sector-size are
48140  ** an integer power of 2. It sets variable pg1 to the identifier
48141  ** of the first page of the sector pPg is located on.
48142  */
48143  pg1 = ((pPg->pgno-1) & ~(nPagePerSector-1)) + 1;
48144
48145  nPageCount = pPager->dbSize;
48146  if( pPg->pgno>nPageCount ){
48147    nPage = (pPg->pgno - pg1)+1;
48148  }else if( (pg1+nPagePerSector-1)>nPageCount ){
48149    nPage = nPageCount+1-pg1;
48150  }else{
48151    nPage = nPagePerSector;
48152  }
48153  assert(nPage>0);
48154  assert(pg1<=pPg->pgno);
48155  assert((pg1+nPage)>pPg->pgno);
48156
48157  for(ii=0; ii<nPage && rc==SQLITE_OK; ii++){
48158    Pgno pg = pg1+ii;
48159    PgHdr *pPage;
48160    if( pg==pPg->pgno || !sqlite3BitvecTest(pPager->pInJournal, pg) ){
48161      if( pg!=PAGER_MJ_PGNO(pPager) ){
48162        rc = sqlite3PagerGet(pPager, pg, &pPage);
48163        if( rc==SQLITE_OK ){
48164          rc = pager_write(pPage);
48165          if( pPage->flags&PGHDR_NEED_SYNC ){
48166            needSync = 1;
48167          }
48168          sqlite3PagerUnrefNotNull(pPage);
48169        }
48170      }
48171    }else if( (pPage = sqlite3PagerLookup(pPager, pg))!=0 ){
48172      if( pPage->flags&PGHDR_NEED_SYNC ){
48173        needSync = 1;
48174      }
48175      sqlite3PagerUnrefNotNull(pPage);
48176    }
48177  }
48178
48179  /* If the PGHDR_NEED_SYNC flag is set for any of the nPage pages
48180  ** starting at pg1, then it needs to be set for all of them. Because
48181  ** writing to any of these nPage pages may damage the others, the
48182  ** journal file must contain sync()ed copies of all of them
48183  ** before any of them can be written out to the database file.
48184  */
48185  if( rc==SQLITE_OK && needSync ){
48186    assert( !MEMDB );
48187    for(ii=0; ii<nPage; ii++){
48188      PgHdr *pPage = sqlite3PagerLookup(pPager, pg1+ii);
48189      if( pPage ){
48190        pPage->flags |= PGHDR_NEED_SYNC;
48191        sqlite3PagerUnrefNotNull(pPage);
48192      }
48193    }
48194  }
48195
48196  assert( (pPager->doNotSpill & SPILLFLAG_NOSYNC)!=0 );
48197  pPager->doNotSpill &= ~SPILLFLAG_NOSYNC;
48198  return rc;
48199}
48200
48201/*
48202** Mark a data page as writeable. This routine must be called before
48203** making changes to a page. The caller must check the return value
48204** of this function and be careful not to change any page data unless
48205** this routine returns SQLITE_OK.
48206**
48207** The difference between this function and pager_write() is that this
48208** function also deals with the special case where 2 or more pages
48209** fit on a single disk sector. In this case all co-resident pages
48210** must have been written to the journal file before returning.
48211**
48212** If an error occurs, SQLITE_NOMEM or an IO error code is returned
48213** as appropriate. Otherwise, SQLITE_OK.
48214*/
48215SQLITE_PRIVATE int sqlite3PagerWrite(PgHdr *pPg){
48216  Pager *pPager = pPg->pPager;
48217  assert( (pPg->flags & PGHDR_MMAP)==0 );
48218  assert( pPager->eState>=PAGER_WRITER_LOCKED );
48219  assert( pPager->eState!=PAGER_ERROR );
48220  assert( assert_pager_state(pPager) );
48221  if( (pPg->flags & PGHDR_WRITEABLE)!=0 && pPager->dbSize>=pPg->pgno ){
48222    if( pPager->nSavepoint ) return subjournalPageIfRequired(pPg);
48223    return SQLITE_OK;
48224  }else if( pPager->sectorSize > (u32)pPager->pageSize ){
48225    return pagerWriteLargeSector(pPg);
48226  }else{
48227    return pager_write(pPg);
48228  }
48229}
48230
48231/*
48232** Return TRUE if the page given in the argument was previously passed
48233** to sqlite3PagerWrite().  In other words, return TRUE if it is ok
48234** to change the content of the page.
48235*/
48236#ifndef NDEBUG
48237SQLITE_PRIVATE int sqlite3PagerIswriteable(DbPage *pPg){
48238  return pPg->flags & PGHDR_WRITEABLE;
48239}
48240#endif
48241
48242/*
48243** A call to this routine tells the pager that it is not necessary to
48244** write the information on page pPg back to the disk, even though
48245** that page might be marked as dirty.  This happens, for example, when
48246** the page has been added as a leaf of the freelist and so its
48247** content no longer matters.
48248**
48249** The overlying software layer calls this routine when all of the data
48250** on the given page is unused. The pager marks the page as clean so
48251** that it does not get written to disk.
48252**
48253** Tests show that this optimization can quadruple the speed of large
48254** DELETE operations.
48255*/
48256SQLITE_PRIVATE void sqlite3PagerDontWrite(PgHdr *pPg){
48257  Pager *pPager = pPg->pPager;
48258  if( (pPg->flags&PGHDR_DIRTY) && pPager->nSavepoint==0 ){
48259    PAGERTRACE(("DONT_WRITE page %d of %d\n", pPg->pgno, PAGERID(pPager)));
48260    IOTRACE(("CLEAN %p %d\n", pPager, pPg->pgno))
48261    pPg->flags |= PGHDR_DONT_WRITE;
48262    pPg->flags &= ~PGHDR_WRITEABLE;
48263    pager_set_pagehash(pPg);
48264  }
48265}
48266
48267/*
48268** This routine is called to increment the value of the database file
48269** change-counter, stored as a 4-byte big-endian integer starting at
48270** byte offset 24 of the pager file.  The secondary change counter at
48271** 92 is also updated, as is the SQLite version number at offset 96.
48272**
48273** But this only happens if the pPager->changeCountDone flag is false.
48274** To avoid excess churning of page 1, the update only happens once.
48275** See also the pager_write_changecounter() routine that does an
48276** unconditional update of the change counters.
48277**
48278** If the isDirectMode flag is zero, then this is done by calling
48279** sqlite3PagerWrite() on page 1, then modifying the contents of the
48280** page data. In this case the file will be updated when the current
48281** transaction is committed.
48282**
48283** The isDirectMode flag may only be non-zero if the library was compiled
48284** with the SQLITE_ENABLE_ATOMIC_WRITE macro defined. In this case,
48285** if isDirect is non-zero, then the database file is updated directly
48286** by writing an updated version of page 1 using a call to the
48287** sqlite3OsWrite() function.
48288*/
48289static int pager_incr_changecounter(Pager *pPager, int isDirectMode){
48290  int rc = SQLITE_OK;
48291
48292  assert( pPager->eState==PAGER_WRITER_CACHEMOD
48293       || pPager->eState==PAGER_WRITER_DBMOD
48294  );
48295  assert( assert_pager_state(pPager) );
48296
48297  /* Declare and initialize constant integer 'isDirect'. If the
48298  ** atomic-write optimization is enabled in this build, then isDirect
48299  ** is initialized to the value passed as the isDirectMode parameter
48300  ** to this function. Otherwise, it is always set to zero.
48301  **
48302  ** The idea is that if the atomic-write optimization is not
48303  ** enabled at compile time, the compiler can omit the tests of
48304  ** 'isDirect' below, as well as the block enclosed in the
48305  ** "if( isDirect )" condition.
48306  */
48307#ifndef SQLITE_ENABLE_ATOMIC_WRITE
48308# define DIRECT_MODE 0
48309  assert( isDirectMode==0 );
48310  UNUSED_PARAMETER(isDirectMode);
48311#else
48312# define DIRECT_MODE isDirectMode
48313#endif
48314
48315  if( !pPager->changeCountDone && ALWAYS(pPager->dbSize>0) ){
48316    PgHdr *pPgHdr;                /* Reference to page 1 */
48317
48318    assert( !pPager->tempFile && isOpen(pPager->fd) );
48319
48320    /* Open page 1 of the file for writing. */
48321    rc = sqlite3PagerGet(pPager, 1, &pPgHdr);
48322    assert( pPgHdr==0 || rc==SQLITE_OK );
48323
48324    /* If page one was fetched successfully, and this function is not
48325    ** operating in direct-mode, make page 1 writable.  When not in
48326    ** direct mode, page 1 is always held in cache and hence the PagerGet()
48327    ** above is always successful - hence the ALWAYS on rc==SQLITE_OK.
48328    */
48329    if( !DIRECT_MODE && ALWAYS(rc==SQLITE_OK) ){
48330      rc = sqlite3PagerWrite(pPgHdr);
48331    }
48332
48333    if( rc==SQLITE_OK ){
48334      /* Actually do the update of the change counter */
48335      pager_write_changecounter(pPgHdr);
48336
48337      /* If running in direct mode, write the contents of page 1 to the file. */
48338      if( DIRECT_MODE ){
48339        const void *zBuf;
48340        assert( pPager->dbFileSize>0 );
48341        CODEC2(pPager, pPgHdr->pData, 1, 6, rc=SQLITE_NOMEM, zBuf);
48342        if( rc==SQLITE_OK ){
48343          rc = sqlite3OsWrite(pPager->fd, zBuf, pPager->pageSize, 0);
48344          pPager->aStat[PAGER_STAT_WRITE]++;
48345        }
48346        if( rc==SQLITE_OK ){
48347          /* Update the pager's copy of the change-counter. Otherwise, the
48348          ** next time a read transaction is opened the cache will be
48349          ** flushed (as the change-counter values will not match).  */
48350          const void *pCopy = (const void *)&((const char *)zBuf)[24];
48351          memcpy(&pPager->dbFileVers, pCopy, sizeof(pPager->dbFileVers));
48352          pPager->changeCountDone = 1;
48353        }
48354      }else{
48355        pPager->changeCountDone = 1;
48356      }
48357    }
48358
48359    /* Release the page reference. */
48360    sqlite3PagerUnref(pPgHdr);
48361  }
48362  return rc;
48363}
48364
48365/*
48366** Sync the database file to disk. This is a no-op for in-memory databases
48367** or pages with the Pager.noSync flag set.
48368**
48369** If successful, or if called on a pager for which it is a no-op, this
48370** function returns SQLITE_OK. Otherwise, an IO error code is returned.
48371*/
48372SQLITE_PRIVATE int sqlite3PagerSync(Pager *pPager, const char *zMaster){
48373  int rc = SQLITE_OK;
48374
48375  if( isOpen(pPager->fd) ){
48376    void *pArg = (void*)zMaster;
48377    rc = sqlite3OsFileControl(pPager->fd, SQLITE_FCNTL_SYNC, pArg);
48378    if( rc==SQLITE_NOTFOUND ) rc = SQLITE_OK;
48379  }
48380  if( rc==SQLITE_OK && !pPager->noSync ){
48381    assert( !MEMDB );
48382    rc = sqlite3OsSync(pPager->fd, pPager->syncFlags);
48383  }
48384  return rc;
48385}
48386
48387/*
48388** This function may only be called while a write-transaction is active in
48389** rollback. If the connection is in WAL mode, this call is a no-op.
48390** Otherwise, if the connection does not already have an EXCLUSIVE lock on
48391** the database file, an attempt is made to obtain one.
48392**
48393** If the EXCLUSIVE lock is already held or the attempt to obtain it is
48394** successful, or the connection is in WAL mode, SQLITE_OK is returned.
48395** Otherwise, either SQLITE_BUSY or an SQLITE_IOERR_XXX error code is
48396** returned.
48397*/
48398SQLITE_PRIVATE int sqlite3PagerExclusiveLock(Pager *pPager){
48399  int rc = SQLITE_OK;
48400  assert( pPager->eState==PAGER_WRITER_CACHEMOD
48401       || pPager->eState==PAGER_WRITER_DBMOD
48402       || pPager->eState==PAGER_WRITER_LOCKED
48403  );
48404  assert( assert_pager_state(pPager) );
48405  if( 0==pagerUseWal(pPager) ){
48406    rc = pager_wait_on_lock(pPager, EXCLUSIVE_LOCK);
48407  }
48408  return rc;
48409}
48410
48411/*
48412** Sync the database file for the pager pPager. zMaster points to the name
48413** of a master journal file that should be written into the individual
48414** journal file. zMaster may be NULL, which is interpreted as no master
48415** journal (a single database transaction).
48416**
48417** This routine ensures that:
48418**
48419**   * The database file change-counter is updated,
48420**   * the journal is synced (unless the atomic-write optimization is used),
48421**   * all dirty pages are written to the database file,
48422**   * the database file is truncated (if required), and
48423**   * the database file synced.
48424**
48425** The only thing that remains to commit the transaction is to finalize
48426** (delete, truncate or zero the first part of) the journal file (or
48427** delete the master journal file if specified).
48428**
48429** Note that if zMaster==NULL, this does not overwrite a previous value
48430** passed to an sqlite3PagerCommitPhaseOne() call.
48431**
48432** If the final parameter - noSync - is true, then the database file itself
48433** is not synced. The caller must call sqlite3PagerSync() directly to
48434** sync the database file before calling CommitPhaseTwo() to delete the
48435** journal file in this case.
48436*/
48437SQLITE_PRIVATE int sqlite3PagerCommitPhaseOne(
48438  Pager *pPager,                  /* Pager object */
48439  const char *zMaster,            /* If not NULL, the master journal name */
48440  int noSync                      /* True to omit the xSync on the db file */
48441){
48442  int rc = SQLITE_OK;             /* Return code */
48443
48444  assert( pPager->eState==PAGER_WRITER_LOCKED
48445       || pPager->eState==PAGER_WRITER_CACHEMOD
48446       || pPager->eState==PAGER_WRITER_DBMOD
48447       || pPager->eState==PAGER_ERROR
48448  );
48449  assert( assert_pager_state(pPager) );
48450
48451  /* If a prior error occurred, report that error again. */
48452  if( NEVER(pPager->errCode) ) return pPager->errCode;
48453
48454  PAGERTRACE(("DATABASE SYNC: File=%s zMaster=%s nSize=%d\n",
48455      pPager->zFilename, zMaster, pPager->dbSize));
48456
48457  /* If no database changes have been made, return early. */
48458  if( pPager->eState<PAGER_WRITER_CACHEMOD ) return SQLITE_OK;
48459
48460  if( MEMDB ){
48461    /* If this is an in-memory db, or no pages have been written to, or this
48462    ** function has already been called, it is mostly a no-op.  However, any
48463    ** backup in progress needs to be restarted.
48464    */
48465    sqlite3BackupRestart(pPager->pBackup);
48466  }else{
48467    if( pagerUseWal(pPager) ){
48468      PgHdr *pList = sqlite3PcacheDirtyList(pPager->pPCache);
48469      PgHdr *pPageOne = 0;
48470      if( pList==0 ){
48471        /* Must have at least one page for the WAL commit flag.
48472        ** Ticket [2d1a5c67dfc2363e44f29d9bbd57f] 2011-05-18 */
48473        rc = sqlite3PagerGet(pPager, 1, &pPageOne);
48474        pList = pPageOne;
48475        pList->pDirty = 0;
48476      }
48477      assert( rc==SQLITE_OK );
48478      if( ALWAYS(pList) ){
48479        rc = pagerWalFrames(pPager, pList, pPager->dbSize, 1);
48480      }
48481      sqlite3PagerUnref(pPageOne);
48482      if( rc==SQLITE_OK ){
48483        sqlite3PcacheCleanAll(pPager->pPCache);
48484      }
48485    }else{
48486      /* The following block updates the change-counter. Exactly how it
48487      ** does this depends on whether or not the atomic-update optimization
48488      ** was enabled at compile time, and if this transaction meets the
48489      ** runtime criteria to use the operation:
48490      **
48491      **    * The file-system supports the atomic-write property for
48492      **      blocks of size page-size, and
48493      **    * This commit is not part of a multi-file transaction, and
48494      **    * Exactly one page has been modified and store in the journal file.
48495      **
48496      ** If the optimization was not enabled at compile time, then the
48497      ** pager_incr_changecounter() function is called to update the change
48498      ** counter in 'indirect-mode'. If the optimization is compiled in but
48499      ** is not applicable to this transaction, call sqlite3JournalCreate()
48500      ** to make sure the journal file has actually been created, then call
48501      ** pager_incr_changecounter() to update the change-counter in indirect
48502      ** mode.
48503      **
48504      ** Otherwise, if the optimization is both enabled and applicable,
48505      ** then call pager_incr_changecounter() to update the change-counter
48506      ** in 'direct' mode. In this case the journal file will never be
48507      ** created for this transaction.
48508      */
48509  #ifdef SQLITE_ENABLE_ATOMIC_WRITE
48510      PgHdr *pPg;
48511      assert( isOpen(pPager->jfd)
48512           || pPager->journalMode==PAGER_JOURNALMODE_OFF
48513           || pPager->journalMode==PAGER_JOURNALMODE_WAL
48514      );
48515      if( !zMaster && isOpen(pPager->jfd)
48516       && pPager->journalOff==jrnlBufferSize(pPager)
48517       && pPager->dbSize>=pPager->dbOrigSize
48518       && (0==(pPg = sqlite3PcacheDirtyList(pPager->pPCache)) || 0==pPg->pDirty)
48519      ){
48520        /* Update the db file change counter via the direct-write method. The
48521        ** following call will modify the in-memory representation of page 1
48522        ** to include the updated change counter and then write page 1
48523        ** directly to the database file. Because of the atomic-write
48524        ** property of the host file-system, this is safe.
48525        */
48526        rc = pager_incr_changecounter(pPager, 1);
48527      }else{
48528        rc = sqlite3JournalCreate(pPager->jfd);
48529        if( rc==SQLITE_OK ){
48530          rc = pager_incr_changecounter(pPager, 0);
48531        }
48532      }
48533  #else
48534      rc = pager_incr_changecounter(pPager, 0);
48535  #endif
48536      if( rc!=SQLITE_OK ) goto commit_phase_one_exit;
48537
48538      /* Write the master journal name into the journal file. If a master
48539      ** journal file name has already been written to the journal file,
48540      ** or if zMaster is NULL (no master journal), then this call is a no-op.
48541      */
48542      rc = writeMasterJournal(pPager, zMaster);
48543      if( rc!=SQLITE_OK ) goto commit_phase_one_exit;
48544
48545      /* Sync the journal file and write all dirty pages to the database.
48546      ** If the atomic-update optimization is being used, this sync will not
48547      ** create the journal file or perform any real IO.
48548      **
48549      ** Because the change-counter page was just modified, unless the
48550      ** atomic-update optimization is used it is almost certain that the
48551      ** journal requires a sync here. However, in locking_mode=exclusive
48552      ** on a system under memory pressure it is just possible that this is
48553      ** not the case. In this case it is likely enough that the redundant
48554      ** xSync() call will be changed to a no-op by the OS anyhow.
48555      */
48556      rc = syncJournal(pPager, 0);
48557      if( rc!=SQLITE_OK ) goto commit_phase_one_exit;
48558
48559      rc = pager_write_pagelist(pPager,sqlite3PcacheDirtyList(pPager->pPCache));
48560      if( rc!=SQLITE_OK ){
48561        assert( rc!=SQLITE_IOERR_BLOCKED );
48562        goto commit_phase_one_exit;
48563      }
48564      sqlite3PcacheCleanAll(pPager->pPCache);
48565
48566      /* If the file on disk is smaller than the database image, use
48567      ** pager_truncate to grow the file here. This can happen if the database
48568      ** image was extended as part of the current transaction and then the
48569      ** last page in the db image moved to the free-list. In this case the
48570      ** last page is never written out to disk, leaving the database file
48571      ** undersized. Fix this now if it is the case.  */
48572      if( pPager->dbSize>pPager->dbFileSize ){
48573        Pgno nNew = pPager->dbSize - (pPager->dbSize==PAGER_MJ_PGNO(pPager));
48574        assert( pPager->eState==PAGER_WRITER_DBMOD );
48575        rc = pager_truncate(pPager, nNew);
48576        if( rc!=SQLITE_OK ) goto commit_phase_one_exit;
48577      }
48578
48579      /* Finally, sync the database file. */
48580      if( !noSync ){
48581        rc = sqlite3PagerSync(pPager, zMaster);
48582      }
48583      IOTRACE(("DBSYNC %p\n", pPager))
48584    }
48585  }
48586
48587commit_phase_one_exit:
48588  if( rc==SQLITE_OK && !pagerUseWal(pPager) ){
48589    pPager->eState = PAGER_WRITER_FINISHED;
48590  }
48591  return rc;
48592}
48593
48594
48595/*
48596** When this function is called, the database file has been completely
48597** updated to reflect the changes made by the current transaction and
48598** synced to disk. The journal file still exists in the file-system
48599** though, and if a failure occurs at this point it will eventually
48600** be used as a hot-journal and the current transaction rolled back.
48601**
48602** This function finalizes the journal file, either by deleting,
48603** truncating or partially zeroing it, so that it cannot be used
48604** for hot-journal rollback. Once this is done the transaction is
48605** irrevocably committed.
48606**
48607** If an error occurs, an IO error code is returned and the pager
48608** moves into the error state. Otherwise, SQLITE_OK is returned.
48609*/
48610SQLITE_PRIVATE int sqlite3PagerCommitPhaseTwo(Pager *pPager){
48611  int rc = SQLITE_OK;                  /* Return code */
48612
48613  /* This routine should not be called if a prior error has occurred.
48614  ** But if (due to a coding error elsewhere in the system) it does get
48615  ** called, just return the same error code without doing anything. */
48616  if( NEVER(pPager->errCode) ) return pPager->errCode;
48617
48618  assert( pPager->eState==PAGER_WRITER_LOCKED
48619       || pPager->eState==PAGER_WRITER_FINISHED
48620       || (pagerUseWal(pPager) && pPager->eState==PAGER_WRITER_CACHEMOD)
48621  );
48622  assert( assert_pager_state(pPager) );
48623
48624  /* An optimization. If the database was not actually modified during
48625  ** this transaction, the pager is running in exclusive-mode and is
48626  ** using persistent journals, then this function is a no-op.
48627  **
48628  ** The start of the journal file currently contains a single journal
48629  ** header with the nRec field set to 0. If such a journal is used as
48630  ** a hot-journal during hot-journal rollback, 0 changes will be made
48631  ** to the database file. So there is no need to zero the journal
48632  ** header. Since the pager is in exclusive mode, there is no need
48633  ** to drop any locks either.
48634  */
48635  if( pPager->eState==PAGER_WRITER_LOCKED
48636   && pPager->exclusiveMode
48637   && pPager->journalMode==PAGER_JOURNALMODE_PERSIST
48638  ){
48639    assert( pPager->journalOff==JOURNAL_HDR_SZ(pPager) || !pPager->journalOff );
48640    pPager->eState = PAGER_READER;
48641    return SQLITE_OK;
48642  }
48643
48644  PAGERTRACE(("COMMIT %d\n", PAGERID(pPager)));
48645  pPager->iDataVersion++;
48646  rc = pager_end_transaction(pPager, pPager->setMaster, 1);
48647  return pager_error(pPager, rc);
48648}
48649
48650/*
48651** If a write transaction is open, then all changes made within the
48652** transaction are reverted and the current write-transaction is closed.
48653** The pager falls back to PAGER_READER state if successful, or PAGER_ERROR
48654** state if an error occurs.
48655**
48656** If the pager is already in PAGER_ERROR state when this function is called,
48657** it returns Pager.errCode immediately. No work is performed in this case.
48658**
48659** Otherwise, in rollback mode, this function performs two functions:
48660**
48661**   1) It rolls back the journal file, restoring all database file and
48662**      in-memory cache pages to the state they were in when the transaction
48663**      was opened, and
48664**
48665**   2) It finalizes the journal file, so that it is not used for hot
48666**      rollback at any point in the future.
48667**
48668** Finalization of the journal file (task 2) is only performed if the
48669** rollback is successful.
48670**
48671** In WAL mode, all cache-entries containing data modified within the
48672** current transaction are either expelled from the cache or reverted to
48673** their pre-transaction state by re-reading data from the database or
48674** WAL files. The WAL transaction is then closed.
48675*/
48676SQLITE_PRIVATE int sqlite3PagerRollback(Pager *pPager){
48677  int rc = SQLITE_OK;                  /* Return code */
48678  PAGERTRACE(("ROLLBACK %d\n", PAGERID(pPager)));
48679
48680  /* PagerRollback() is a no-op if called in READER or OPEN state. If
48681  ** the pager is already in the ERROR state, the rollback is not
48682  ** attempted here. Instead, the error code is returned to the caller.
48683  */
48684  assert( assert_pager_state(pPager) );
48685  if( pPager->eState==PAGER_ERROR ) return pPager->errCode;
48686  if( pPager->eState<=PAGER_READER ) return SQLITE_OK;
48687
48688  if( pagerUseWal(pPager) ){
48689    int rc2;
48690    rc = sqlite3PagerSavepoint(pPager, SAVEPOINT_ROLLBACK, -1);
48691    rc2 = pager_end_transaction(pPager, pPager->setMaster, 0);
48692    if( rc==SQLITE_OK ) rc = rc2;
48693  }else if( !isOpen(pPager->jfd) || pPager->eState==PAGER_WRITER_LOCKED ){
48694    int eState = pPager->eState;
48695    rc = pager_end_transaction(pPager, 0, 0);
48696    if( !MEMDB && eState>PAGER_WRITER_LOCKED ){
48697      /* This can happen using journal_mode=off. Move the pager to the error
48698      ** state to indicate that the contents of the cache may not be trusted.
48699      ** Any active readers will get SQLITE_ABORT.
48700      */
48701      pPager->errCode = SQLITE_ABORT;
48702      pPager->eState = PAGER_ERROR;
48703      return rc;
48704    }
48705  }else{
48706    rc = pager_playback(pPager, 0);
48707  }
48708
48709  assert( pPager->eState==PAGER_READER || rc!=SQLITE_OK );
48710  assert( rc==SQLITE_OK || rc==SQLITE_FULL || rc==SQLITE_CORRUPT
48711          || rc==SQLITE_NOMEM || (rc&0xFF)==SQLITE_IOERR
48712          || rc==SQLITE_CANTOPEN
48713  );
48714
48715  /* If an error occurs during a ROLLBACK, we can no longer trust the pager
48716  ** cache. So call pager_error() on the way out to make any error persistent.
48717  */
48718  return pager_error(pPager, rc);
48719}
48720
48721/*
48722** Return TRUE if the database file is opened read-only.  Return FALSE
48723** if the database is (in theory) writable.
48724*/
48725SQLITE_PRIVATE u8 sqlite3PagerIsreadonly(Pager *pPager){
48726  return pPager->readOnly;
48727}
48728
48729#ifdef SQLITE_DEBUG
48730/*
48731** Return the number of references to the pager.
48732*/
48733SQLITE_PRIVATE int sqlite3PagerRefcount(Pager *pPager){
48734  return sqlite3PcacheRefCount(pPager->pPCache);
48735}
48736#endif
48737
48738/*
48739** Return the approximate number of bytes of memory currently
48740** used by the pager and its associated cache.
48741*/
48742SQLITE_PRIVATE int sqlite3PagerMemUsed(Pager *pPager){
48743  int perPageSize = pPager->pageSize + pPager->nExtra + sizeof(PgHdr)
48744                                     + 5*sizeof(void*);
48745  return perPageSize*sqlite3PcachePagecount(pPager->pPCache)
48746           + sqlite3MallocSize(pPager)
48747           + pPager->pageSize;
48748}
48749
48750/*
48751** Return the number of references to the specified page.
48752*/
48753SQLITE_PRIVATE int sqlite3PagerPageRefcount(DbPage *pPage){
48754  return sqlite3PcachePageRefcount(pPage);
48755}
48756
48757#ifdef SQLITE_TEST
48758/*
48759** This routine is used for testing and analysis only.
48760*/
48761SQLITE_PRIVATE int *sqlite3PagerStats(Pager *pPager){
48762  static int a[11];
48763  a[0] = sqlite3PcacheRefCount(pPager->pPCache);
48764  a[1] = sqlite3PcachePagecount(pPager->pPCache);
48765  a[2] = sqlite3PcacheGetCachesize(pPager->pPCache);
48766  a[3] = pPager->eState==PAGER_OPEN ? -1 : (int) pPager->dbSize;
48767  a[4] = pPager->eState;
48768  a[5] = pPager->errCode;
48769  a[6] = pPager->aStat[PAGER_STAT_HIT];
48770  a[7] = pPager->aStat[PAGER_STAT_MISS];
48771  a[8] = 0;  /* Used to be pPager->nOvfl */
48772  a[9] = pPager->nRead;
48773  a[10] = pPager->aStat[PAGER_STAT_WRITE];
48774  return a;
48775}
48776#endif
48777
48778/*
48779** Parameter eStat must be either SQLITE_DBSTATUS_CACHE_HIT or
48780** SQLITE_DBSTATUS_CACHE_MISS. Before returning, *pnVal is incremented by the
48781** current cache hit or miss count, according to the value of eStat. If the
48782** reset parameter is non-zero, the cache hit or miss count is zeroed before
48783** returning.
48784*/
48785SQLITE_PRIVATE void sqlite3PagerCacheStat(Pager *pPager, int eStat, int reset, int *pnVal){
48786
48787  assert( eStat==SQLITE_DBSTATUS_CACHE_HIT
48788       || eStat==SQLITE_DBSTATUS_CACHE_MISS
48789       || eStat==SQLITE_DBSTATUS_CACHE_WRITE
48790  );
48791
48792  assert( SQLITE_DBSTATUS_CACHE_HIT+1==SQLITE_DBSTATUS_CACHE_MISS );
48793  assert( SQLITE_DBSTATUS_CACHE_HIT+2==SQLITE_DBSTATUS_CACHE_WRITE );
48794  assert( PAGER_STAT_HIT==0 && PAGER_STAT_MISS==1 && PAGER_STAT_WRITE==2 );
48795
48796  *pnVal += pPager->aStat[eStat - SQLITE_DBSTATUS_CACHE_HIT];
48797  if( reset ){
48798    pPager->aStat[eStat - SQLITE_DBSTATUS_CACHE_HIT] = 0;
48799  }
48800}
48801
48802/*
48803** Return true if this is an in-memory pager.
48804*/
48805SQLITE_PRIVATE int sqlite3PagerIsMemdb(Pager *pPager){
48806  return MEMDB;
48807}
48808
48809/*
48810** Check that there are at least nSavepoint savepoints open. If there are
48811** currently less than nSavepoints open, then open one or more savepoints
48812** to make up the difference. If the number of savepoints is already
48813** equal to nSavepoint, then this function is a no-op.
48814**
48815** If a memory allocation fails, SQLITE_NOMEM is returned. If an error
48816** occurs while opening the sub-journal file, then an IO error code is
48817** returned. Otherwise, SQLITE_OK.
48818*/
48819static SQLITE_NOINLINE int pagerOpenSavepoint(Pager *pPager, int nSavepoint){
48820  int rc = SQLITE_OK;                       /* Return code */
48821  int nCurrent = pPager->nSavepoint;        /* Current number of savepoints */
48822  int ii;                                   /* Iterator variable */
48823  PagerSavepoint *aNew;                     /* New Pager.aSavepoint array */
48824
48825  assert( pPager->eState>=PAGER_WRITER_LOCKED );
48826  assert( assert_pager_state(pPager) );
48827  assert( nSavepoint>nCurrent && pPager->useJournal );
48828
48829  /* Grow the Pager.aSavepoint array using realloc(). Return SQLITE_NOMEM
48830  ** if the allocation fails. Otherwise, zero the new portion in case a
48831  ** malloc failure occurs while populating it in the for(...) loop below.
48832  */
48833  aNew = (PagerSavepoint *)sqlite3Realloc(
48834      pPager->aSavepoint, sizeof(PagerSavepoint)*nSavepoint
48835  );
48836  if( !aNew ){
48837    return SQLITE_NOMEM;
48838  }
48839  memset(&aNew[nCurrent], 0, (nSavepoint-nCurrent) * sizeof(PagerSavepoint));
48840  pPager->aSavepoint = aNew;
48841
48842  /* Populate the PagerSavepoint structures just allocated. */
48843  for(ii=nCurrent; ii<nSavepoint; ii++){
48844    aNew[ii].nOrig = pPager->dbSize;
48845    if( isOpen(pPager->jfd) && pPager->journalOff>0 ){
48846      aNew[ii].iOffset = pPager->journalOff;
48847    }else{
48848      aNew[ii].iOffset = JOURNAL_HDR_SZ(pPager);
48849    }
48850    aNew[ii].iSubRec = pPager->nSubRec;
48851    aNew[ii].pInSavepoint = sqlite3BitvecCreate(pPager->dbSize);
48852    if( !aNew[ii].pInSavepoint ){
48853      return SQLITE_NOMEM;
48854    }
48855    if( pagerUseWal(pPager) ){
48856      sqlite3WalSavepoint(pPager->pWal, aNew[ii].aWalData);
48857    }
48858    pPager->nSavepoint = ii+1;
48859  }
48860  assert( pPager->nSavepoint==nSavepoint );
48861  assertTruncateConstraint(pPager);
48862  return rc;
48863}
48864SQLITE_PRIVATE int sqlite3PagerOpenSavepoint(Pager *pPager, int nSavepoint){
48865  assert( pPager->eState>=PAGER_WRITER_LOCKED );
48866  assert( assert_pager_state(pPager) );
48867
48868  if( nSavepoint>pPager->nSavepoint && pPager->useJournal ){
48869    return pagerOpenSavepoint(pPager, nSavepoint);
48870  }else{
48871    return SQLITE_OK;
48872  }
48873}
48874
48875
48876/*
48877** This function is called to rollback or release (commit) a savepoint.
48878** The savepoint to release or rollback need not be the most recently
48879** created savepoint.
48880**
48881** Parameter op is always either SAVEPOINT_ROLLBACK or SAVEPOINT_RELEASE.
48882** If it is SAVEPOINT_RELEASE, then release and destroy the savepoint with
48883** index iSavepoint. If it is SAVEPOINT_ROLLBACK, then rollback all changes
48884** that have occurred since the specified savepoint was created.
48885**
48886** The savepoint to rollback or release is identified by parameter
48887** iSavepoint. A value of 0 means to operate on the outermost savepoint
48888** (the first created). A value of (Pager.nSavepoint-1) means operate
48889** on the most recently created savepoint. If iSavepoint is greater than
48890** (Pager.nSavepoint-1), then this function is a no-op.
48891**
48892** If a negative value is passed to this function, then the current
48893** transaction is rolled back. This is different to calling
48894** sqlite3PagerRollback() because this function does not terminate
48895** the transaction or unlock the database, it just restores the
48896** contents of the database to its original state.
48897**
48898** In any case, all savepoints with an index greater than iSavepoint
48899** are destroyed. If this is a release operation (op==SAVEPOINT_RELEASE),
48900** then savepoint iSavepoint is also destroyed.
48901**
48902** This function may return SQLITE_NOMEM if a memory allocation fails,
48903** or an IO error code if an IO error occurs while rolling back a
48904** savepoint. If no errors occur, SQLITE_OK is returned.
48905*/
48906SQLITE_PRIVATE int sqlite3PagerSavepoint(Pager *pPager, int op, int iSavepoint){
48907  int rc = pPager->errCode;       /* Return code */
48908
48909  assert( op==SAVEPOINT_RELEASE || op==SAVEPOINT_ROLLBACK );
48910  assert( iSavepoint>=0 || op==SAVEPOINT_ROLLBACK );
48911
48912  if( rc==SQLITE_OK && iSavepoint<pPager->nSavepoint ){
48913    int ii;            /* Iterator variable */
48914    int nNew;          /* Number of remaining savepoints after this op. */
48915
48916    /* Figure out how many savepoints will still be active after this
48917    ** operation. Store this value in nNew. Then free resources associated
48918    ** with any savepoints that are destroyed by this operation.
48919    */
48920    nNew = iSavepoint + (( op==SAVEPOINT_RELEASE ) ? 0 : 1);
48921    for(ii=nNew; ii<pPager->nSavepoint; ii++){
48922      sqlite3BitvecDestroy(pPager->aSavepoint[ii].pInSavepoint);
48923    }
48924    pPager->nSavepoint = nNew;
48925
48926    /* If this is a release of the outermost savepoint, truncate
48927    ** the sub-journal to zero bytes in size. */
48928    if( op==SAVEPOINT_RELEASE ){
48929      if( nNew==0 && isOpen(pPager->sjfd) ){
48930        /* Only truncate if it is an in-memory sub-journal. */
48931        if( sqlite3IsMemJournal(pPager->sjfd) ){
48932          rc = sqlite3OsTruncate(pPager->sjfd, 0);
48933          assert( rc==SQLITE_OK );
48934        }
48935        pPager->nSubRec = 0;
48936      }
48937    }
48938    /* Else this is a rollback operation, playback the specified savepoint.
48939    ** If this is a temp-file, it is possible that the journal file has
48940    ** not yet been opened. In this case there have been no changes to
48941    ** the database file, so the playback operation can be skipped.
48942    */
48943    else if( pagerUseWal(pPager) || isOpen(pPager->jfd) ){
48944      PagerSavepoint *pSavepoint = (nNew==0)?0:&pPager->aSavepoint[nNew-1];
48945      rc = pagerPlaybackSavepoint(pPager, pSavepoint);
48946      assert(rc!=SQLITE_DONE);
48947    }
48948  }
48949
48950  return rc;
48951}
48952
48953/*
48954** Return the full pathname of the database file.
48955**
48956** Except, if the pager is in-memory only, then return an empty string if
48957** nullIfMemDb is true.  This routine is called with nullIfMemDb==1 when
48958** used to report the filename to the user, for compatibility with legacy
48959** behavior.  But when the Btree needs to know the filename for matching to
48960** shared cache, it uses nullIfMemDb==0 so that in-memory databases can
48961** participate in shared-cache.
48962*/
48963SQLITE_PRIVATE const char *sqlite3PagerFilename(Pager *pPager, int nullIfMemDb){
48964  return (nullIfMemDb && pPager->memDb) ? "" : pPager->zFilename;
48965}
48966
48967/*
48968** Return the VFS structure for the pager.
48969*/
48970SQLITE_PRIVATE const sqlite3_vfs *sqlite3PagerVfs(Pager *pPager){
48971  return pPager->pVfs;
48972}
48973
48974/*
48975** Return the file handle for the database file associated
48976** with the pager.  This might return NULL if the file has
48977** not yet been opened.
48978*/
48979SQLITE_PRIVATE sqlite3_file *sqlite3PagerFile(Pager *pPager){
48980  return pPager->fd;
48981}
48982
48983/*
48984** Return the full pathname of the journal file.
48985*/
48986SQLITE_PRIVATE const char *sqlite3PagerJournalname(Pager *pPager){
48987  return pPager->zJournal;
48988}
48989
48990/*
48991** Return true if fsync() calls are disabled for this pager.  Return FALSE
48992** if fsync()s are executed normally.
48993*/
48994SQLITE_PRIVATE int sqlite3PagerNosync(Pager *pPager){
48995  return pPager->noSync;
48996}
48997
48998#ifdef SQLITE_HAS_CODEC
48999/*
49000** Set or retrieve the codec for this pager
49001*/
49002SQLITE_PRIVATE void sqlite3PagerSetCodec(
49003  Pager *pPager,
49004  void *(*xCodec)(void*,void*,Pgno,int),
49005  void (*xCodecSizeChng)(void*,int,int),
49006  void (*xCodecFree)(void*),
49007  void *pCodec
49008){
49009  if( pPager->xCodecFree ) pPager->xCodecFree(pPager->pCodec);
49010  pPager->xCodec = pPager->memDb ? 0 : xCodec;
49011  pPager->xCodecSizeChng = xCodecSizeChng;
49012  pPager->xCodecFree = xCodecFree;
49013  pPager->pCodec = pCodec;
49014  pagerReportSize(pPager);
49015}
49016SQLITE_PRIVATE void *sqlite3PagerGetCodec(Pager *pPager){
49017  return pPager->pCodec;
49018}
49019
49020/*
49021** This function is called by the wal module when writing page content
49022** into the log file.
49023**
49024** This function returns a pointer to a buffer containing the encrypted
49025** page content. If a malloc fails, this function may return NULL.
49026*/
49027SQLITE_PRIVATE void *sqlite3PagerCodec(PgHdr *pPg){
49028  void *aData = 0;
49029  CODEC2(pPg->pPager, pPg->pData, pPg->pgno, 6, return 0, aData);
49030  return aData;
49031}
49032
49033/*
49034** Return the current pager state
49035*/
49036SQLITE_PRIVATE int sqlite3PagerState(Pager *pPager){
49037  return pPager->eState;
49038}
49039#endif /* SQLITE_HAS_CODEC */
49040
49041#ifndef SQLITE_OMIT_AUTOVACUUM
49042/*
49043** Move the page pPg to location pgno in the file.
49044**
49045** There must be no references to the page previously located at
49046** pgno (which we call pPgOld) though that page is allowed to be
49047** in cache.  If the page previously located at pgno is not already
49048** in the rollback journal, it is not put there by by this routine.
49049**
49050** References to the page pPg remain valid. Updating any
49051** meta-data associated with pPg (i.e. data stored in the nExtra bytes
49052** allocated along with the page) is the responsibility of the caller.
49053**
49054** A transaction must be active when this routine is called. It used to be
49055** required that a statement transaction was not active, but this restriction
49056** has been removed (CREATE INDEX needs to move a page when a statement
49057** transaction is active).
49058**
49059** If the fourth argument, isCommit, is non-zero, then this page is being
49060** moved as part of a database reorganization just before the transaction
49061** is being committed. In this case, it is guaranteed that the database page
49062** pPg refers to will not be written to again within this transaction.
49063**
49064** This function may return SQLITE_NOMEM or an IO error code if an error
49065** occurs. Otherwise, it returns SQLITE_OK.
49066*/
49067SQLITE_PRIVATE int sqlite3PagerMovepage(Pager *pPager, DbPage *pPg, Pgno pgno, int isCommit){
49068  PgHdr *pPgOld;               /* The page being overwritten. */
49069  Pgno needSyncPgno = 0;       /* Old value of pPg->pgno, if sync is required */
49070  int rc;                      /* Return code */
49071  Pgno origPgno;               /* The original page number */
49072
49073  assert( pPg->nRef>0 );
49074  assert( pPager->eState==PAGER_WRITER_CACHEMOD
49075       || pPager->eState==PAGER_WRITER_DBMOD
49076  );
49077  assert( assert_pager_state(pPager) );
49078
49079  /* In order to be able to rollback, an in-memory database must journal
49080  ** the page we are moving from.
49081  */
49082  if( MEMDB ){
49083    rc = sqlite3PagerWrite(pPg);
49084    if( rc ) return rc;
49085  }
49086
49087  /* If the page being moved is dirty and has not been saved by the latest
49088  ** savepoint, then save the current contents of the page into the
49089  ** sub-journal now. This is required to handle the following scenario:
49090  **
49091  **   BEGIN;
49092  **     <journal page X, then modify it in memory>
49093  **     SAVEPOINT one;
49094  **       <Move page X to location Y>
49095  **     ROLLBACK TO one;
49096  **
49097  ** If page X were not written to the sub-journal here, it would not
49098  ** be possible to restore its contents when the "ROLLBACK TO one"
49099  ** statement were is processed.
49100  **
49101  ** subjournalPage() may need to allocate space to store pPg->pgno into
49102  ** one or more savepoint bitvecs. This is the reason this function
49103  ** may return SQLITE_NOMEM.
49104  */
49105  if( (pPg->flags & PGHDR_DIRTY)!=0
49106   && SQLITE_OK!=(rc = subjournalPageIfRequired(pPg))
49107  ){
49108    return rc;
49109  }
49110
49111  PAGERTRACE(("MOVE %d page %d (needSync=%d) moves to %d\n",
49112      PAGERID(pPager), pPg->pgno, (pPg->flags&PGHDR_NEED_SYNC)?1:0, pgno));
49113  IOTRACE(("MOVE %p %d %d\n", pPager, pPg->pgno, pgno))
49114
49115  /* If the journal needs to be sync()ed before page pPg->pgno can
49116  ** be written to, store pPg->pgno in local variable needSyncPgno.
49117  **
49118  ** If the isCommit flag is set, there is no need to remember that
49119  ** the journal needs to be sync()ed before database page pPg->pgno
49120  ** can be written to. The caller has already promised not to write to it.
49121  */
49122  if( (pPg->flags&PGHDR_NEED_SYNC) && !isCommit ){
49123    needSyncPgno = pPg->pgno;
49124    assert( pPager->journalMode==PAGER_JOURNALMODE_OFF ||
49125            pageInJournal(pPager, pPg) || pPg->pgno>pPager->dbOrigSize );
49126    assert( pPg->flags&PGHDR_DIRTY );
49127  }
49128
49129  /* If the cache contains a page with page-number pgno, remove it
49130  ** from its hash chain. Also, if the PGHDR_NEED_SYNC flag was set for
49131  ** page pgno before the 'move' operation, it needs to be retained
49132  ** for the page moved there.
49133  */
49134  pPg->flags &= ~PGHDR_NEED_SYNC;
49135  pPgOld = sqlite3PagerLookup(pPager, pgno);
49136  assert( !pPgOld || pPgOld->nRef==1 );
49137  if( pPgOld ){
49138    pPg->flags |= (pPgOld->flags&PGHDR_NEED_SYNC);
49139    if( MEMDB ){
49140      /* Do not discard pages from an in-memory database since we might
49141      ** need to rollback later.  Just move the page out of the way. */
49142      sqlite3PcacheMove(pPgOld, pPager->dbSize+1);
49143    }else{
49144      sqlite3PcacheDrop(pPgOld);
49145    }
49146  }
49147
49148  origPgno = pPg->pgno;
49149  sqlite3PcacheMove(pPg, pgno);
49150  sqlite3PcacheMakeDirty(pPg);
49151
49152  /* For an in-memory database, make sure the original page continues
49153  ** to exist, in case the transaction needs to roll back.  Use pPgOld
49154  ** as the original page since it has already been allocated.
49155  */
49156  if( MEMDB ){
49157    assert( pPgOld );
49158    sqlite3PcacheMove(pPgOld, origPgno);
49159    sqlite3PagerUnrefNotNull(pPgOld);
49160  }
49161
49162  if( needSyncPgno ){
49163    /* If needSyncPgno is non-zero, then the journal file needs to be
49164    ** sync()ed before any data is written to database file page needSyncPgno.
49165    ** Currently, no such page exists in the page-cache and the
49166    ** "is journaled" bitvec flag has been set. This needs to be remedied by
49167    ** loading the page into the pager-cache and setting the PGHDR_NEED_SYNC
49168    ** flag.
49169    **
49170    ** If the attempt to load the page into the page-cache fails, (due
49171    ** to a malloc() or IO failure), clear the bit in the pInJournal[]
49172    ** array. Otherwise, if the page is loaded and written again in
49173    ** this transaction, it may be written to the database file before
49174    ** it is synced into the journal file. This way, it may end up in
49175    ** the journal file twice, but that is not a problem.
49176    */
49177    PgHdr *pPgHdr;
49178    rc = sqlite3PagerGet(pPager, needSyncPgno, &pPgHdr);
49179    if( rc!=SQLITE_OK ){
49180      if( needSyncPgno<=pPager->dbOrigSize ){
49181        assert( pPager->pTmpSpace!=0 );
49182        sqlite3BitvecClear(pPager->pInJournal, needSyncPgno, pPager->pTmpSpace);
49183      }
49184      return rc;
49185    }
49186    pPgHdr->flags |= PGHDR_NEED_SYNC;
49187    sqlite3PcacheMakeDirty(pPgHdr);
49188    sqlite3PagerUnrefNotNull(pPgHdr);
49189  }
49190
49191  return SQLITE_OK;
49192}
49193#endif
49194
49195/*
49196** The page handle passed as the first argument refers to a dirty page
49197** with a page number other than iNew. This function changes the page's
49198** page number to iNew and sets the value of the PgHdr.flags field to
49199** the value passed as the third parameter.
49200*/
49201SQLITE_PRIVATE void sqlite3PagerRekey(DbPage *pPg, Pgno iNew, u16 flags){
49202  assert( pPg->pgno!=iNew );
49203  pPg->flags = flags;
49204  sqlite3PcacheMove(pPg, iNew);
49205}
49206
49207/*
49208** Return a pointer to the data for the specified page.
49209*/
49210SQLITE_PRIVATE void *sqlite3PagerGetData(DbPage *pPg){
49211  assert( pPg->nRef>0 || pPg->pPager->memDb );
49212  return pPg->pData;
49213}
49214
49215/*
49216** Return a pointer to the Pager.nExtra bytes of "extra" space
49217** allocated along with the specified page.
49218*/
49219SQLITE_PRIVATE void *sqlite3PagerGetExtra(DbPage *pPg){
49220  return pPg->pExtra;
49221}
49222
49223/*
49224** Get/set the locking-mode for this pager. Parameter eMode must be one
49225** of PAGER_LOCKINGMODE_QUERY, PAGER_LOCKINGMODE_NORMAL or
49226** PAGER_LOCKINGMODE_EXCLUSIVE. If the parameter is not _QUERY, then
49227** the locking-mode is set to the value specified.
49228**
49229** The returned value is either PAGER_LOCKINGMODE_NORMAL or
49230** PAGER_LOCKINGMODE_EXCLUSIVE, indicating the current (possibly updated)
49231** locking-mode.
49232*/
49233SQLITE_PRIVATE int sqlite3PagerLockingMode(Pager *pPager, int eMode){
49234  assert( eMode==PAGER_LOCKINGMODE_QUERY
49235            || eMode==PAGER_LOCKINGMODE_NORMAL
49236            || eMode==PAGER_LOCKINGMODE_EXCLUSIVE );
49237  assert( PAGER_LOCKINGMODE_QUERY<0 );
49238  assert( PAGER_LOCKINGMODE_NORMAL>=0 && PAGER_LOCKINGMODE_EXCLUSIVE>=0 );
49239  assert( pPager->exclusiveMode || 0==sqlite3WalHeapMemory(pPager->pWal) );
49240  if( eMode>=0 && !pPager->tempFile && !sqlite3WalHeapMemory(pPager->pWal) ){
49241    pPager->exclusiveMode = (u8)eMode;
49242  }
49243  return (int)pPager->exclusiveMode;
49244}
49245
49246/*
49247** Set the journal-mode for this pager. Parameter eMode must be one of:
49248**
49249**    PAGER_JOURNALMODE_DELETE
49250**    PAGER_JOURNALMODE_TRUNCATE
49251**    PAGER_JOURNALMODE_PERSIST
49252**    PAGER_JOURNALMODE_OFF
49253**    PAGER_JOURNALMODE_MEMORY
49254**    PAGER_JOURNALMODE_WAL
49255**
49256** The journalmode is set to the value specified if the change is allowed.
49257** The change may be disallowed for the following reasons:
49258**
49259**   *  An in-memory database can only have its journal_mode set to _OFF
49260**      or _MEMORY.
49261**
49262**   *  Temporary databases cannot have _WAL journalmode.
49263**
49264** The returned indicate the current (possibly updated) journal-mode.
49265*/
49266SQLITE_PRIVATE int sqlite3PagerSetJournalMode(Pager *pPager, int eMode){
49267  u8 eOld = pPager->journalMode;    /* Prior journalmode */
49268
49269#ifdef SQLITE_DEBUG
49270  /* The print_pager_state() routine is intended to be used by the debugger
49271  ** only.  We invoke it once here to suppress a compiler warning. */
49272  print_pager_state(pPager);
49273#endif
49274
49275
49276  /* The eMode parameter is always valid */
49277  assert(      eMode==PAGER_JOURNALMODE_DELETE
49278            || eMode==PAGER_JOURNALMODE_TRUNCATE
49279            || eMode==PAGER_JOURNALMODE_PERSIST
49280            || eMode==PAGER_JOURNALMODE_OFF
49281            || eMode==PAGER_JOURNALMODE_WAL
49282            || eMode==PAGER_JOURNALMODE_MEMORY );
49283
49284  /* This routine is only called from the OP_JournalMode opcode, and
49285  ** the logic there will never allow a temporary file to be changed
49286  ** to WAL mode.
49287  */
49288  assert( pPager->tempFile==0 || eMode!=PAGER_JOURNALMODE_WAL );
49289
49290  /* Do allow the journalmode of an in-memory database to be set to
49291  ** anything other than MEMORY or OFF
49292  */
49293  if( MEMDB ){
49294    assert( eOld==PAGER_JOURNALMODE_MEMORY || eOld==PAGER_JOURNALMODE_OFF );
49295    if( eMode!=PAGER_JOURNALMODE_MEMORY && eMode!=PAGER_JOURNALMODE_OFF ){
49296      eMode = eOld;
49297    }
49298  }
49299
49300  if( eMode!=eOld ){
49301
49302    /* Change the journal mode. */
49303    assert( pPager->eState!=PAGER_ERROR );
49304    pPager->journalMode = (u8)eMode;
49305
49306    /* When transistioning from TRUNCATE or PERSIST to any other journal
49307    ** mode except WAL, unless the pager is in locking_mode=exclusive mode,
49308    ** delete the journal file.
49309    */
49310    assert( (PAGER_JOURNALMODE_TRUNCATE & 5)==1 );
49311    assert( (PAGER_JOURNALMODE_PERSIST & 5)==1 );
49312    assert( (PAGER_JOURNALMODE_DELETE & 5)==0 );
49313    assert( (PAGER_JOURNALMODE_MEMORY & 5)==4 );
49314    assert( (PAGER_JOURNALMODE_OFF & 5)==0 );
49315    assert( (PAGER_JOURNALMODE_WAL & 5)==5 );
49316
49317    assert( isOpen(pPager->fd) || pPager->exclusiveMode );
49318    if( !pPager->exclusiveMode && (eOld & 5)==1 && (eMode & 1)==0 ){
49319
49320      /* In this case we would like to delete the journal file. If it is
49321      ** not possible, then that is not a problem. Deleting the journal file
49322      ** here is an optimization only.
49323      **
49324      ** Before deleting the journal file, obtain a RESERVED lock on the
49325      ** database file. This ensures that the journal file is not deleted
49326      ** while it is in use by some other client.
49327      */
49328      sqlite3OsClose(pPager->jfd);
49329      if( pPager->eLock>=RESERVED_LOCK ){
49330        sqlite3OsDelete(pPager->pVfs, pPager->zJournal, 0);
49331      }else{
49332        int rc = SQLITE_OK;
49333        int state = pPager->eState;
49334        assert( state==PAGER_OPEN || state==PAGER_READER );
49335        if( state==PAGER_OPEN ){
49336          rc = sqlite3PagerSharedLock(pPager);
49337        }
49338        if( pPager->eState==PAGER_READER ){
49339          assert( rc==SQLITE_OK );
49340          rc = pagerLockDb(pPager, RESERVED_LOCK);
49341        }
49342        if( rc==SQLITE_OK ){
49343          sqlite3OsDelete(pPager->pVfs, pPager->zJournal, 0);
49344        }
49345        if( rc==SQLITE_OK && state==PAGER_READER ){
49346          pagerUnlockDb(pPager, SHARED_LOCK);
49347        }else if( state==PAGER_OPEN ){
49348          pager_unlock(pPager);
49349        }
49350        assert( state==pPager->eState );
49351      }
49352    }else if( eMode==PAGER_JOURNALMODE_OFF ){
49353      sqlite3OsClose(pPager->jfd);
49354    }
49355  }
49356
49357  /* Return the new journal mode */
49358  return (int)pPager->journalMode;
49359}
49360
49361/*
49362** Return the current journal mode.
49363*/
49364SQLITE_PRIVATE int sqlite3PagerGetJournalMode(Pager *pPager){
49365  return (int)pPager->journalMode;
49366}
49367
49368/*
49369** Return TRUE if the pager is in a state where it is OK to change the
49370** journalmode.  Journalmode changes can only happen when the database
49371** is unmodified.
49372*/
49373SQLITE_PRIVATE int sqlite3PagerOkToChangeJournalMode(Pager *pPager){
49374  assert( assert_pager_state(pPager) );
49375  if( pPager->eState>=PAGER_WRITER_CACHEMOD ) return 0;
49376  if( NEVER(isOpen(pPager->jfd) && pPager->journalOff>0) ) return 0;
49377  return 1;
49378}
49379
49380/*
49381** Get/set the size-limit used for persistent journal files.
49382**
49383** Setting the size limit to -1 means no limit is enforced.
49384** An attempt to set a limit smaller than -1 is a no-op.
49385*/
49386SQLITE_PRIVATE i64 sqlite3PagerJournalSizeLimit(Pager *pPager, i64 iLimit){
49387  if( iLimit>=-1 ){
49388    pPager->journalSizeLimit = iLimit;
49389    sqlite3WalLimit(pPager->pWal, iLimit);
49390  }
49391  return pPager->journalSizeLimit;
49392}
49393
49394/*
49395** Return a pointer to the pPager->pBackup variable. The backup module
49396** in backup.c maintains the content of this variable. This module
49397** uses it opaquely as an argument to sqlite3BackupRestart() and
49398** sqlite3BackupUpdate() only.
49399*/
49400SQLITE_PRIVATE sqlite3_backup **sqlite3PagerBackupPtr(Pager *pPager){
49401  return &pPager->pBackup;
49402}
49403
49404#ifndef SQLITE_OMIT_VACUUM
49405/*
49406** Unless this is an in-memory or temporary database, clear the pager cache.
49407*/
49408SQLITE_PRIVATE void sqlite3PagerClearCache(Pager *pPager){
49409  if( !MEMDB && pPager->tempFile==0 ) pager_reset(pPager);
49410}
49411#endif
49412
49413#ifndef SQLITE_OMIT_WAL
49414/*
49415** This function is called when the user invokes "PRAGMA wal_checkpoint",
49416** "PRAGMA wal_blocking_checkpoint" or calls the sqlite3_wal_checkpoint()
49417** or wal_blocking_checkpoint() API functions.
49418**
49419** Parameter eMode is one of SQLITE_CHECKPOINT_PASSIVE, FULL or RESTART.
49420*/
49421SQLITE_PRIVATE int sqlite3PagerCheckpoint(Pager *pPager, int eMode, int *pnLog, int *pnCkpt){
49422  int rc = SQLITE_OK;
49423  if( pPager->pWal ){
49424    rc = sqlite3WalCheckpoint(pPager->pWal, eMode,
49425        (eMode==SQLITE_CHECKPOINT_PASSIVE ? 0 : pPager->xBusyHandler),
49426        pPager->pBusyHandlerArg,
49427        pPager->ckptSyncFlags, pPager->pageSize, (u8 *)pPager->pTmpSpace,
49428        pnLog, pnCkpt
49429    );
49430  }
49431  return rc;
49432}
49433
49434SQLITE_PRIVATE int sqlite3PagerWalCallback(Pager *pPager){
49435  return sqlite3WalCallback(pPager->pWal);
49436}
49437
49438/*
49439** Return true if the underlying VFS for the given pager supports the
49440** primitives necessary for write-ahead logging.
49441*/
49442SQLITE_PRIVATE int sqlite3PagerWalSupported(Pager *pPager){
49443  const sqlite3_io_methods *pMethods = pPager->fd->pMethods;
49444  return pPager->exclusiveMode || (pMethods->iVersion>=2 && pMethods->xShmMap);
49445}
49446
49447/*
49448** Attempt to take an exclusive lock on the database file. If a PENDING lock
49449** is obtained instead, immediately release it.
49450*/
49451static int pagerExclusiveLock(Pager *pPager){
49452  int rc;                         /* Return code */
49453
49454  assert( pPager->eLock==SHARED_LOCK || pPager->eLock==EXCLUSIVE_LOCK );
49455  rc = pagerLockDb(pPager, EXCLUSIVE_LOCK);
49456  if( rc!=SQLITE_OK ){
49457    /* If the attempt to grab the exclusive lock failed, release the
49458    ** pending lock that may have been obtained instead.  */
49459    pagerUnlockDb(pPager, SHARED_LOCK);
49460  }
49461
49462  return rc;
49463}
49464
49465/*
49466** Call sqlite3WalOpen() to open the WAL handle. If the pager is in
49467** exclusive-locking mode when this function is called, take an EXCLUSIVE
49468** lock on the database file and use heap-memory to store the wal-index
49469** in. Otherwise, use the normal shared-memory.
49470*/
49471static int pagerOpenWal(Pager *pPager){
49472  int rc = SQLITE_OK;
49473
49474  assert( pPager->pWal==0 && pPager->tempFile==0 );
49475  assert( pPager->eLock==SHARED_LOCK || pPager->eLock==EXCLUSIVE_LOCK );
49476
49477  /* If the pager is already in exclusive-mode, the WAL module will use
49478  ** heap-memory for the wal-index instead of the VFS shared-memory
49479  ** implementation. Take the exclusive lock now, before opening the WAL
49480  ** file, to make sure this is safe.
49481  */
49482  if( pPager->exclusiveMode ){
49483    rc = pagerExclusiveLock(pPager);
49484  }
49485
49486  /* Open the connection to the log file. If this operation fails,
49487  ** (e.g. due to malloc() failure), return an error code.
49488  */
49489  if( rc==SQLITE_OK ){
49490    rc = sqlite3WalOpen(pPager->pVfs,
49491        pPager->fd, pPager->zWal, pPager->exclusiveMode,
49492        pPager->journalSizeLimit, &pPager->pWal
49493    );
49494  }
49495  pagerFixMaplimit(pPager);
49496
49497  return rc;
49498}
49499
49500
49501/*
49502** The caller must be holding a SHARED lock on the database file to call
49503** this function.
49504**
49505** If the pager passed as the first argument is open on a real database
49506** file (not a temp file or an in-memory database), and the WAL file
49507** is not already open, make an attempt to open it now. If successful,
49508** return SQLITE_OK. If an error occurs or the VFS used by the pager does
49509** not support the xShmXXX() methods, return an error code. *pbOpen is
49510** not modified in either case.
49511**
49512** If the pager is open on a temp-file (or in-memory database), or if
49513** the WAL file is already open, set *pbOpen to 1 and return SQLITE_OK
49514** without doing anything.
49515*/
49516SQLITE_PRIVATE int sqlite3PagerOpenWal(
49517  Pager *pPager,                  /* Pager object */
49518  int *pbOpen                     /* OUT: Set to true if call is a no-op */
49519){
49520  int rc = SQLITE_OK;             /* Return code */
49521
49522  assert( assert_pager_state(pPager) );
49523  assert( pPager->eState==PAGER_OPEN   || pbOpen );
49524  assert( pPager->eState==PAGER_READER || !pbOpen );
49525  assert( pbOpen==0 || *pbOpen==0 );
49526  assert( pbOpen!=0 || (!pPager->tempFile && !pPager->pWal) );
49527
49528  if( !pPager->tempFile && !pPager->pWal ){
49529    if( !sqlite3PagerWalSupported(pPager) ) return SQLITE_CANTOPEN;
49530
49531    /* Close any rollback journal previously open */
49532    sqlite3OsClose(pPager->jfd);
49533
49534    rc = pagerOpenWal(pPager);
49535    if( rc==SQLITE_OK ){
49536      pPager->journalMode = PAGER_JOURNALMODE_WAL;
49537      pPager->eState = PAGER_OPEN;
49538    }
49539  }else{
49540    *pbOpen = 1;
49541  }
49542
49543  return rc;
49544}
49545
49546/*
49547** This function is called to close the connection to the log file prior
49548** to switching from WAL to rollback mode.
49549**
49550** Before closing the log file, this function attempts to take an
49551** EXCLUSIVE lock on the database file. If this cannot be obtained, an
49552** error (SQLITE_BUSY) is returned and the log connection is not closed.
49553** If successful, the EXCLUSIVE lock is not released before returning.
49554*/
49555SQLITE_PRIVATE int sqlite3PagerCloseWal(Pager *pPager){
49556  int rc = SQLITE_OK;
49557
49558  assert( pPager->journalMode==PAGER_JOURNALMODE_WAL );
49559
49560  /* If the log file is not already open, but does exist in the file-system,
49561  ** it may need to be checkpointed before the connection can switch to
49562  ** rollback mode. Open it now so this can happen.
49563  */
49564  if( !pPager->pWal ){
49565    int logexists = 0;
49566    rc = pagerLockDb(pPager, SHARED_LOCK);
49567    if( rc==SQLITE_OK ){
49568      rc = sqlite3OsAccess(
49569          pPager->pVfs, pPager->zWal, SQLITE_ACCESS_EXISTS, &logexists
49570      );
49571    }
49572    if( rc==SQLITE_OK && logexists ){
49573      rc = pagerOpenWal(pPager);
49574    }
49575  }
49576
49577  /* Checkpoint and close the log. Because an EXCLUSIVE lock is held on
49578  ** the database file, the log and log-summary files will be deleted.
49579  */
49580  if( rc==SQLITE_OK && pPager->pWal ){
49581    rc = pagerExclusiveLock(pPager);
49582    if( rc==SQLITE_OK ){
49583      rc = sqlite3WalClose(pPager->pWal, pPager->ckptSyncFlags,
49584                           pPager->pageSize, (u8*)pPager->pTmpSpace);
49585      pPager->pWal = 0;
49586      pagerFixMaplimit(pPager);
49587    }
49588  }
49589  return rc;
49590}
49591
49592#endif /* !SQLITE_OMIT_WAL */
49593
49594#ifdef SQLITE_ENABLE_ZIPVFS
49595/*
49596** A read-lock must be held on the pager when this function is called. If
49597** the pager is in WAL mode and the WAL file currently contains one or more
49598** frames, return the size in bytes of the page images stored within the
49599** WAL frames. Otherwise, if this is not a WAL database or the WAL file
49600** is empty, return 0.
49601*/
49602SQLITE_PRIVATE int sqlite3PagerWalFramesize(Pager *pPager){
49603  assert( pPager->eState>=PAGER_READER );
49604  return sqlite3WalFramesize(pPager->pWal);
49605}
49606#endif
49607
49608
49609#endif /* SQLITE_OMIT_DISKIO */
49610
49611/************** End of pager.c ***********************************************/
49612/************** Begin file wal.c *********************************************/
49613/*
49614** 2010 February 1
49615**
49616** The author disclaims copyright to this source code.  In place of
49617** a legal notice, here is a blessing:
49618**
49619**    May you do good and not evil.
49620**    May you find forgiveness for yourself and forgive others.
49621**    May you share freely, never taking more than you give.
49622**
49623*************************************************************************
49624**
49625** This file contains the implementation of a write-ahead log (WAL) used in
49626** "journal_mode=WAL" mode.
49627**
49628** WRITE-AHEAD LOG (WAL) FILE FORMAT
49629**
49630** A WAL file consists of a header followed by zero or more "frames".
49631** Each frame records the revised content of a single page from the
49632** database file.  All changes to the database are recorded by writing
49633** frames into the WAL.  Transactions commit when a frame is written that
49634** contains a commit marker.  A single WAL can and usually does record
49635** multiple transactions.  Periodically, the content of the WAL is
49636** transferred back into the database file in an operation called a
49637** "checkpoint".
49638**
49639** A single WAL file can be used multiple times.  In other words, the
49640** WAL can fill up with frames and then be checkpointed and then new
49641** frames can overwrite the old ones.  A WAL always grows from beginning
49642** toward the end.  Checksums and counters attached to each frame are
49643** used to determine which frames within the WAL are valid and which
49644** are leftovers from prior checkpoints.
49645**
49646** The WAL header is 32 bytes in size and consists of the following eight
49647** big-endian 32-bit unsigned integer values:
49648**
49649**     0: Magic number.  0x377f0682 or 0x377f0683
49650**     4: File format version.  Currently 3007000
49651**     8: Database page size.  Example: 1024
49652**    12: Checkpoint sequence number
49653**    16: Salt-1, random integer incremented with each checkpoint
49654**    20: Salt-2, a different random integer changing with each ckpt
49655**    24: Checksum-1 (first part of checksum for first 24 bytes of header).
49656**    28: Checksum-2 (second part of checksum for first 24 bytes of header).
49657**
49658** Immediately following the wal-header are zero or more frames. Each
49659** frame consists of a 24-byte frame-header followed by a <page-size> bytes
49660** of page data. The frame-header is six big-endian 32-bit unsigned
49661** integer values, as follows:
49662**
49663**     0: Page number.
49664**     4: For commit records, the size of the database image in pages
49665**        after the commit. For all other records, zero.
49666**     8: Salt-1 (copied from the header)
49667**    12: Salt-2 (copied from the header)
49668**    16: Checksum-1.
49669**    20: Checksum-2.
49670**
49671** A frame is considered valid if and only if the following conditions are
49672** true:
49673**
49674**    (1) The salt-1 and salt-2 values in the frame-header match
49675**        salt values in the wal-header
49676**
49677**    (2) The checksum values in the final 8 bytes of the frame-header
49678**        exactly match the checksum computed consecutively on the
49679**        WAL header and the first 8 bytes and the content of all frames
49680**        up to and including the current frame.
49681**
49682** The checksum is computed using 32-bit big-endian integers if the
49683** magic number in the first 4 bytes of the WAL is 0x377f0683 and it
49684** is computed using little-endian if the magic number is 0x377f0682.
49685** The checksum values are always stored in the frame header in a
49686** big-endian format regardless of which byte order is used to compute
49687** the checksum.  The checksum is computed by interpreting the input as
49688** an even number of unsigned 32-bit integers: x[0] through x[N].  The
49689** algorithm used for the checksum is as follows:
49690**
49691**   for i from 0 to n-1 step 2:
49692**     s0 += x[i] + s1;
49693**     s1 += x[i+1] + s0;
49694**   endfor
49695**
49696** Note that s0 and s1 are both weighted checksums using fibonacci weights
49697** in reverse order (the largest fibonacci weight occurs on the first element
49698** of the sequence being summed.)  The s1 value spans all 32-bit
49699** terms of the sequence whereas s0 omits the final term.
49700**
49701** On a checkpoint, the WAL is first VFS.xSync-ed, then valid content of the
49702** WAL is transferred into the database, then the database is VFS.xSync-ed.
49703** The VFS.xSync operations serve as write barriers - all writes launched
49704** before the xSync must complete before any write that launches after the
49705** xSync begins.
49706**
49707** After each checkpoint, the salt-1 value is incremented and the salt-2
49708** value is randomized.  This prevents old and new frames in the WAL from
49709** being considered valid at the same time and being checkpointing together
49710** following a crash.
49711**
49712** READER ALGORITHM
49713**
49714** To read a page from the database (call it page number P), a reader
49715** first checks the WAL to see if it contains page P.  If so, then the
49716** last valid instance of page P that is a followed by a commit frame
49717** or is a commit frame itself becomes the value read.  If the WAL
49718** contains no copies of page P that are valid and which are a commit
49719** frame or are followed by a commit frame, then page P is read from
49720** the database file.
49721**
49722** To start a read transaction, the reader records the index of the last
49723** valid frame in the WAL.  The reader uses this recorded "mxFrame" value
49724** for all subsequent read operations.  New transactions can be appended
49725** to the WAL, but as long as the reader uses its original mxFrame value
49726** and ignores the newly appended content, it will see a consistent snapshot
49727** of the database from a single point in time.  This technique allows
49728** multiple concurrent readers to view different versions of the database
49729** content simultaneously.
49730**
49731** The reader algorithm in the previous paragraphs works correctly, but
49732** because frames for page P can appear anywhere within the WAL, the
49733** reader has to scan the entire WAL looking for page P frames.  If the
49734** WAL is large (multiple megabytes is typical) that scan can be slow,
49735** and read performance suffers.  To overcome this problem, a separate
49736** data structure called the wal-index is maintained to expedite the
49737** search for frames of a particular page.
49738**
49739** WAL-INDEX FORMAT
49740**
49741** Conceptually, the wal-index is shared memory, though VFS implementations
49742** might choose to implement the wal-index using a mmapped file.  Because
49743** the wal-index is shared memory, SQLite does not support journal_mode=WAL
49744** on a network filesystem.  All users of the database must be able to
49745** share memory.
49746**
49747** The wal-index is transient.  After a crash, the wal-index can (and should
49748** be) reconstructed from the original WAL file.  In fact, the VFS is required
49749** to either truncate or zero the header of the wal-index when the last
49750** connection to it closes.  Because the wal-index is transient, it can
49751** use an architecture-specific format; it does not have to be cross-platform.
49752** Hence, unlike the database and WAL file formats which store all values
49753** as big endian, the wal-index can store multi-byte values in the native
49754** byte order of the host computer.
49755**
49756** The purpose of the wal-index is to answer this question quickly:  Given
49757** a page number P and a maximum frame index M, return the index of the
49758** last frame in the wal before frame M for page P in the WAL, or return
49759** NULL if there are no frames for page P in the WAL prior to M.
49760**
49761** The wal-index consists of a header region, followed by an one or
49762** more index blocks.
49763**
49764** The wal-index header contains the total number of frames within the WAL
49765** in the mxFrame field.
49766**
49767** Each index block except for the first contains information on
49768** HASHTABLE_NPAGE frames. The first index block contains information on
49769** HASHTABLE_NPAGE_ONE frames. The values of HASHTABLE_NPAGE_ONE and
49770** HASHTABLE_NPAGE are selected so that together the wal-index header and
49771** first index block are the same size as all other index blocks in the
49772** wal-index.
49773**
49774** Each index block contains two sections, a page-mapping that contains the
49775** database page number associated with each wal frame, and a hash-table
49776** that allows readers to query an index block for a specific page number.
49777** The page-mapping is an array of HASHTABLE_NPAGE (or HASHTABLE_NPAGE_ONE
49778** for the first index block) 32-bit page numbers. The first entry in the
49779** first index-block contains the database page number corresponding to the
49780** first frame in the WAL file. The first entry in the second index block
49781** in the WAL file corresponds to the (HASHTABLE_NPAGE_ONE+1)th frame in
49782** the log, and so on.
49783**
49784** The last index block in a wal-index usually contains less than the full
49785** complement of HASHTABLE_NPAGE (or HASHTABLE_NPAGE_ONE) page-numbers,
49786** depending on the contents of the WAL file. This does not change the
49787** allocated size of the page-mapping array - the page-mapping array merely
49788** contains unused entries.
49789**
49790** Even without using the hash table, the last frame for page P
49791** can be found by scanning the page-mapping sections of each index block
49792** starting with the last index block and moving toward the first, and
49793** within each index block, starting at the end and moving toward the
49794** beginning.  The first entry that equals P corresponds to the frame
49795** holding the content for that page.
49796**
49797** The hash table consists of HASHTABLE_NSLOT 16-bit unsigned integers.
49798** HASHTABLE_NSLOT = 2*HASHTABLE_NPAGE, and there is one entry in the
49799** hash table for each page number in the mapping section, so the hash
49800** table is never more than half full.  The expected number of collisions
49801** prior to finding a match is 1.  Each entry of the hash table is an
49802** 1-based index of an entry in the mapping section of the same
49803** index block.   Let K be the 1-based index of the largest entry in
49804** the mapping section.  (For index blocks other than the last, K will
49805** always be exactly HASHTABLE_NPAGE (4096) and for the last index block
49806** K will be (mxFrame%HASHTABLE_NPAGE).)  Unused slots of the hash table
49807** contain a value of 0.
49808**
49809** To look for page P in the hash table, first compute a hash iKey on
49810** P as follows:
49811**
49812**      iKey = (P * 383) % HASHTABLE_NSLOT
49813**
49814** Then start scanning entries of the hash table, starting with iKey
49815** (wrapping around to the beginning when the end of the hash table is
49816** reached) until an unused hash slot is found. Let the first unused slot
49817** be at index iUnused.  (iUnused might be less than iKey if there was
49818** wrap-around.) Because the hash table is never more than half full,
49819** the search is guaranteed to eventually hit an unused entry.  Let
49820** iMax be the value between iKey and iUnused, closest to iUnused,
49821** where aHash[iMax]==P.  If there is no iMax entry (if there exists
49822** no hash slot such that aHash[i]==p) then page P is not in the
49823** current index block.  Otherwise the iMax-th mapping entry of the
49824** current index block corresponds to the last entry that references
49825** page P.
49826**
49827** A hash search begins with the last index block and moves toward the
49828** first index block, looking for entries corresponding to page P.  On
49829** average, only two or three slots in each index block need to be
49830** examined in order to either find the last entry for page P, or to
49831** establish that no such entry exists in the block.  Each index block
49832** holds over 4000 entries.  So two or three index blocks are sufficient
49833** to cover a typical 10 megabyte WAL file, assuming 1K pages.  8 or 10
49834** comparisons (on average) suffice to either locate a frame in the
49835** WAL or to establish that the frame does not exist in the WAL.  This
49836** is much faster than scanning the entire 10MB WAL.
49837**
49838** Note that entries are added in order of increasing K.  Hence, one
49839** reader might be using some value K0 and a second reader that started
49840** at a later time (after additional transactions were added to the WAL
49841** and to the wal-index) might be using a different value K1, where K1>K0.
49842** Both readers can use the same hash table and mapping section to get
49843** the correct result.  There may be entries in the hash table with
49844** K>K0 but to the first reader, those entries will appear to be unused
49845** slots in the hash table and so the first reader will get an answer as
49846** if no values greater than K0 had ever been inserted into the hash table
49847** in the first place - which is what reader one wants.  Meanwhile, the
49848** second reader using K1 will see additional values that were inserted
49849** later, which is exactly what reader two wants.
49850**
49851** When a rollback occurs, the value of K is decreased. Hash table entries
49852** that correspond to frames greater than the new K value are removed
49853** from the hash table at this point.
49854*/
49855#ifndef SQLITE_OMIT_WAL
49856
49857/* #include "wal.h" */
49858
49859/*
49860** Trace output macros
49861*/
49862#if defined(SQLITE_TEST) && defined(SQLITE_DEBUG)
49863SQLITE_PRIVATE int sqlite3WalTrace = 0;
49864# define WALTRACE(X)  if(sqlite3WalTrace) sqlite3DebugPrintf X
49865#else
49866# define WALTRACE(X)
49867#endif
49868
49869/*
49870** The maximum (and only) versions of the wal and wal-index formats
49871** that may be interpreted by this version of SQLite.
49872**
49873** If a client begins recovering a WAL file and finds that (a) the checksum
49874** values in the wal-header are correct and (b) the version field is not
49875** WAL_MAX_VERSION, recovery fails and SQLite returns SQLITE_CANTOPEN.
49876**
49877** Similarly, if a client successfully reads a wal-index header (i.e. the
49878** checksum test is successful) and finds that the version field is not
49879** WALINDEX_MAX_VERSION, then no read-transaction is opened and SQLite
49880** returns SQLITE_CANTOPEN.
49881*/
49882#define WAL_MAX_VERSION      3007000
49883#define WALINDEX_MAX_VERSION 3007000
49884
49885/*
49886** Indices of various locking bytes.   WAL_NREADER is the number
49887** of available reader locks and should be at least 3.
49888*/
49889#define WAL_WRITE_LOCK         0
49890#define WAL_ALL_BUT_WRITE      1
49891#define WAL_CKPT_LOCK          1
49892#define WAL_RECOVER_LOCK       2
49893#define WAL_READ_LOCK(I)       (3+(I))
49894#define WAL_NREADER            (SQLITE_SHM_NLOCK-3)
49895
49896
49897/* Object declarations */
49898typedef struct WalIndexHdr WalIndexHdr;
49899typedef struct WalIterator WalIterator;
49900typedef struct WalCkptInfo WalCkptInfo;
49901
49902
49903/*
49904** The following object holds a copy of the wal-index header content.
49905**
49906** The actual header in the wal-index consists of two copies of this
49907** object.
49908**
49909** The szPage value can be any power of 2 between 512 and 32768, inclusive.
49910** Or it can be 1 to represent a 65536-byte page.  The latter case was
49911** added in 3.7.1 when support for 64K pages was added.
49912*/
49913struct WalIndexHdr {
49914  u32 iVersion;                   /* Wal-index version */
49915  u32 unused;                     /* Unused (padding) field */
49916  u32 iChange;                    /* Counter incremented each transaction */
49917  u8 isInit;                      /* 1 when initialized */
49918  u8 bigEndCksum;                 /* True if checksums in WAL are big-endian */
49919  u16 szPage;                     /* Database page size in bytes. 1==64K */
49920  u32 mxFrame;                    /* Index of last valid frame in the WAL */
49921  u32 nPage;                      /* Size of database in pages */
49922  u32 aFrameCksum[2];             /* Checksum of last frame in log */
49923  u32 aSalt[2];                   /* Two salt values copied from WAL header */
49924  u32 aCksum[2];                  /* Checksum over all prior fields */
49925};
49926
49927/*
49928** A copy of the following object occurs in the wal-index immediately
49929** following the second copy of the WalIndexHdr.  This object stores
49930** information used by checkpoint.
49931**
49932** nBackfill is the number of frames in the WAL that have been written
49933** back into the database. (We call the act of moving content from WAL to
49934** database "backfilling".)  The nBackfill number is never greater than
49935** WalIndexHdr.mxFrame.  nBackfill can only be increased by threads
49936** holding the WAL_CKPT_LOCK lock (which includes a recovery thread).
49937** However, a WAL_WRITE_LOCK thread can move the value of nBackfill from
49938** mxFrame back to zero when the WAL is reset.
49939**
49940** There is one entry in aReadMark[] for each reader lock.  If a reader
49941** holds read-lock K, then the value in aReadMark[K] is no greater than
49942** the mxFrame for that reader.  The value READMARK_NOT_USED (0xffffffff)
49943** for any aReadMark[] means that entry is unused.  aReadMark[0] is
49944** a special case; its value is never used and it exists as a place-holder
49945** to avoid having to offset aReadMark[] indexs by one.  Readers holding
49946** WAL_READ_LOCK(0) always ignore the entire WAL and read all content
49947** directly from the database.
49948**
49949** The value of aReadMark[K] may only be changed by a thread that
49950** is holding an exclusive lock on WAL_READ_LOCK(K).  Thus, the value of
49951** aReadMark[K] cannot changed while there is a reader is using that mark
49952** since the reader will be holding a shared lock on WAL_READ_LOCK(K).
49953**
49954** The checkpointer may only transfer frames from WAL to database where
49955** the frame numbers are less than or equal to every aReadMark[] that is
49956** in use (that is, every aReadMark[j] for which there is a corresponding
49957** WAL_READ_LOCK(j)).  New readers (usually) pick the aReadMark[] with the
49958** largest value and will increase an unused aReadMark[] to mxFrame if there
49959** is not already an aReadMark[] equal to mxFrame.  The exception to the
49960** previous sentence is when nBackfill equals mxFrame (meaning that everything
49961** in the WAL has been backfilled into the database) then new readers
49962** will choose aReadMark[0] which has value 0 and hence such reader will
49963** get all their all content directly from the database file and ignore
49964** the WAL.
49965**
49966** Writers normally append new frames to the end of the WAL.  However,
49967** if nBackfill equals mxFrame (meaning that all WAL content has been
49968** written back into the database) and if no readers are using the WAL
49969** (in other words, if there are no WAL_READ_LOCK(i) where i>0) then
49970** the writer will first "reset" the WAL back to the beginning and start
49971** writing new content beginning at frame 1.
49972**
49973** We assume that 32-bit loads are atomic and so no locks are needed in
49974** order to read from any aReadMark[] entries.
49975*/
49976struct WalCkptInfo {
49977  u32 nBackfill;                  /* Number of WAL frames backfilled into DB */
49978  u32 aReadMark[WAL_NREADER];     /* Reader marks */
49979};
49980#define READMARK_NOT_USED  0xffffffff
49981
49982
49983/* A block of WALINDEX_LOCK_RESERVED bytes beginning at
49984** WALINDEX_LOCK_OFFSET is reserved for locks. Since some systems
49985** only support mandatory file-locks, we do not read or write data
49986** from the region of the file on which locks are applied.
49987*/
49988#define WALINDEX_LOCK_OFFSET   (sizeof(WalIndexHdr)*2 + sizeof(WalCkptInfo))
49989#define WALINDEX_LOCK_RESERVED 16
49990#define WALINDEX_HDR_SIZE      (WALINDEX_LOCK_OFFSET+WALINDEX_LOCK_RESERVED)
49991
49992/* Size of header before each frame in wal */
49993#define WAL_FRAME_HDRSIZE 24
49994
49995/* Size of write ahead log header, including checksum. */
49996/* #define WAL_HDRSIZE 24 */
49997#define WAL_HDRSIZE 32
49998
49999/* WAL magic value. Either this value, or the same value with the least
50000** significant bit also set (WAL_MAGIC | 0x00000001) is stored in 32-bit
50001** big-endian format in the first 4 bytes of a WAL file.
50002**
50003** If the LSB is set, then the checksums for each frame within the WAL
50004** file are calculated by treating all data as an array of 32-bit
50005** big-endian words. Otherwise, they are calculated by interpreting
50006** all data as 32-bit little-endian words.
50007*/
50008#define WAL_MAGIC 0x377f0682
50009
50010/*
50011** Return the offset of frame iFrame in the write-ahead log file,
50012** assuming a database page size of szPage bytes. The offset returned
50013** is to the start of the write-ahead log frame-header.
50014*/
50015#define walFrameOffset(iFrame, szPage) (                               \
50016  WAL_HDRSIZE + ((iFrame)-1)*(i64)((szPage)+WAL_FRAME_HDRSIZE)         \
50017)
50018
50019/*
50020** An open write-ahead log file is represented by an instance of the
50021** following object.
50022*/
50023struct Wal {
50024  sqlite3_vfs *pVfs;         /* The VFS used to create pDbFd */
50025  sqlite3_file *pDbFd;       /* File handle for the database file */
50026  sqlite3_file *pWalFd;      /* File handle for WAL file */
50027  u32 iCallback;             /* Value to pass to log callback (or 0) */
50028  i64 mxWalSize;             /* Truncate WAL to this size upon reset */
50029  int nWiData;               /* Size of array apWiData */
50030  int szFirstBlock;          /* Size of first block written to WAL file */
50031  volatile u32 **apWiData;   /* Pointer to wal-index content in memory */
50032  u32 szPage;                /* Database page size */
50033  i16 readLock;              /* Which read lock is being held.  -1 for none */
50034  u8 syncFlags;              /* Flags to use to sync header writes */
50035  u8 exclusiveMode;          /* Non-zero if connection is in exclusive mode */
50036  u8 writeLock;              /* True if in a write transaction */
50037  u8 ckptLock;               /* True if holding a checkpoint lock */
50038  u8 readOnly;               /* WAL_RDWR, WAL_RDONLY, or WAL_SHM_RDONLY */
50039  u8 truncateOnCommit;       /* True to truncate WAL file on commit */
50040  u8 syncHeader;             /* Fsync the WAL header if true */
50041  u8 padToSectorBoundary;    /* Pad transactions out to the next sector */
50042  WalIndexHdr hdr;           /* Wal-index header for current transaction */
50043  const char *zWalName;      /* Name of WAL file */
50044  u32 nCkpt;                 /* Checkpoint sequence counter in the wal-header */
50045#ifdef SQLITE_DEBUG
50046  u8 lockError;              /* True if a locking error has occurred */
50047#endif
50048};
50049
50050/*
50051** Candidate values for Wal.exclusiveMode.
50052*/
50053#define WAL_NORMAL_MODE     0
50054#define WAL_EXCLUSIVE_MODE  1
50055#define WAL_HEAPMEMORY_MODE 2
50056
50057/*
50058** Possible values for WAL.readOnly
50059*/
50060#define WAL_RDWR        0    /* Normal read/write connection */
50061#define WAL_RDONLY      1    /* The WAL file is readonly */
50062#define WAL_SHM_RDONLY  2    /* The SHM file is readonly */
50063
50064/*
50065** Each page of the wal-index mapping contains a hash-table made up of
50066** an array of HASHTABLE_NSLOT elements of the following type.
50067*/
50068typedef u16 ht_slot;
50069
50070/*
50071** This structure is used to implement an iterator that loops through
50072** all frames in the WAL in database page order. Where two or more frames
50073** correspond to the same database page, the iterator visits only the
50074** frame most recently written to the WAL (in other words, the frame with
50075** the largest index).
50076**
50077** The internals of this structure are only accessed by:
50078**
50079**   walIteratorInit() - Create a new iterator,
50080**   walIteratorNext() - Step an iterator,
50081**   walIteratorFree() - Free an iterator.
50082**
50083** This functionality is used by the checkpoint code (see walCheckpoint()).
50084*/
50085struct WalIterator {
50086  int iPrior;                     /* Last result returned from the iterator */
50087  int nSegment;                   /* Number of entries in aSegment[] */
50088  struct WalSegment {
50089    int iNext;                    /* Next slot in aIndex[] not yet returned */
50090    ht_slot *aIndex;              /* i0, i1, i2... such that aPgno[iN] ascend */
50091    u32 *aPgno;                   /* Array of page numbers. */
50092    int nEntry;                   /* Nr. of entries in aPgno[] and aIndex[] */
50093    int iZero;                    /* Frame number associated with aPgno[0] */
50094  } aSegment[1];                  /* One for every 32KB page in the wal-index */
50095};
50096
50097/*
50098** Define the parameters of the hash tables in the wal-index file. There
50099** is a hash-table following every HASHTABLE_NPAGE page numbers in the
50100** wal-index.
50101**
50102** Changing any of these constants will alter the wal-index format and
50103** create incompatibilities.
50104*/
50105#define HASHTABLE_NPAGE      4096                 /* Must be power of 2 */
50106#define HASHTABLE_HASH_1     383                  /* Should be prime */
50107#define HASHTABLE_NSLOT      (HASHTABLE_NPAGE*2)  /* Must be a power of 2 */
50108
50109/*
50110** The block of page numbers associated with the first hash-table in a
50111** wal-index is smaller than usual. This is so that there is a complete
50112** hash-table on each aligned 32KB page of the wal-index.
50113*/
50114#define HASHTABLE_NPAGE_ONE  (HASHTABLE_NPAGE - (WALINDEX_HDR_SIZE/sizeof(u32)))
50115
50116/* The wal-index is divided into pages of WALINDEX_PGSZ bytes each. */
50117#define WALINDEX_PGSZ   (                                         \
50118    sizeof(ht_slot)*HASHTABLE_NSLOT + HASHTABLE_NPAGE*sizeof(u32) \
50119)
50120
50121/*
50122** Obtain a pointer to the iPage'th page of the wal-index. The wal-index
50123** is broken into pages of WALINDEX_PGSZ bytes. Wal-index pages are
50124** numbered from zero.
50125**
50126** If this call is successful, *ppPage is set to point to the wal-index
50127** page and SQLITE_OK is returned. If an error (an OOM or VFS error) occurs,
50128** then an SQLite error code is returned and *ppPage is set to 0.
50129*/
50130static int walIndexPage(Wal *pWal, int iPage, volatile u32 **ppPage){
50131  int rc = SQLITE_OK;
50132
50133  /* Enlarge the pWal->apWiData[] array if required */
50134  if( pWal->nWiData<=iPage ){
50135    int nByte = sizeof(u32*)*(iPage+1);
50136    volatile u32 **apNew;
50137    apNew = (volatile u32 **)sqlite3_realloc64((void *)pWal->apWiData, nByte);
50138    if( !apNew ){
50139      *ppPage = 0;
50140      return SQLITE_NOMEM;
50141    }
50142    memset((void*)&apNew[pWal->nWiData], 0,
50143           sizeof(u32*)*(iPage+1-pWal->nWiData));
50144    pWal->apWiData = apNew;
50145    pWal->nWiData = iPage+1;
50146  }
50147
50148  /* Request a pointer to the required page from the VFS */
50149  if( pWal->apWiData[iPage]==0 ){
50150    if( pWal->exclusiveMode==WAL_HEAPMEMORY_MODE ){
50151      pWal->apWiData[iPage] = (u32 volatile *)sqlite3MallocZero(WALINDEX_PGSZ);
50152      if( !pWal->apWiData[iPage] ) rc = SQLITE_NOMEM;
50153    }else{
50154      rc = sqlite3OsShmMap(pWal->pDbFd, iPage, WALINDEX_PGSZ,
50155          pWal->writeLock, (void volatile **)&pWal->apWiData[iPage]
50156      );
50157      if( rc==SQLITE_READONLY ){
50158        pWal->readOnly |= WAL_SHM_RDONLY;
50159        rc = SQLITE_OK;
50160      }
50161    }
50162  }
50163
50164  *ppPage = pWal->apWiData[iPage];
50165  assert( iPage==0 || *ppPage || rc!=SQLITE_OK );
50166  return rc;
50167}
50168
50169/*
50170** Return a pointer to the WalCkptInfo structure in the wal-index.
50171*/
50172static volatile WalCkptInfo *walCkptInfo(Wal *pWal){
50173  assert( pWal->nWiData>0 && pWal->apWiData[0] );
50174  return (volatile WalCkptInfo*)&(pWal->apWiData[0][sizeof(WalIndexHdr)/2]);
50175}
50176
50177/*
50178** Return a pointer to the WalIndexHdr structure in the wal-index.
50179*/
50180static volatile WalIndexHdr *walIndexHdr(Wal *pWal){
50181  assert( pWal->nWiData>0 && pWal->apWiData[0] );
50182  return (volatile WalIndexHdr*)pWal->apWiData[0];
50183}
50184
50185/*
50186** The argument to this macro must be of type u32. On a little-endian
50187** architecture, it returns the u32 value that results from interpreting
50188** the 4 bytes as a big-endian value. On a big-endian architecture, it
50189** returns the value that would be produced by interpreting the 4 bytes
50190** of the input value as a little-endian integer.
50191*/
50192#define BYTESWAP32(x) ( \
50193    (((x)&0x000000FF)<<24) + (((x)&0x0000FF00)<<8)  \
50194  + (((x)&0x00FF0000)>>8)  + (((x)&0xFF000000)>>24) \
50195)
50196
50197/*
50198** Generate or extend an 8 byte checksum based on the data in
50199** array aByte[] and the initial values of aIn[0] and aIn[1] (or
50200** initial values of 0 and 0 if aIn==NULL).
50201**
50202** The checksum is written back into aOut[] before returning.
50203**
50204** nByte must be a positive multiple of 8.
50205*/
50206static void walChecksumBytes(
50207  int nativeCksum, /* True for native byte-order, false for non-native */
50208  u8 *a,           /* Content to be checksummed */
50209  int nByte,       /* Bytes of content in a[].  Must be a multiple of 8. */
50210  const u32 *aIn,  /* Initial checksum value input */
50211  u32 *aOut        /* OUT: Final checksum value output */
50212){
50213  u32 s1, s2;
50214  u32 *aData = (u32 *)a;
50215  u32 *aEnd = (u32 *)&a[nByte];
50216
50217  if( aIn ){
50218    s1 = aIn[0];
50219    s2 = aIn[1];
50220  }else{
50221    s1 = s2 = 0;
50222  }
50223
50224  assert( nByte>=8 );
50225  assert( (nByte&0x00000007)==0 );
50226
50227  if( nativeCksum ){
50228    do {
50229      s1 += *aData++ + s2;
50230      s2 += *aData++ + s1;
50231    }while( aData<aEnd );
50232  }else{
50233    do {
50234      s1 += BYTESWAP32(aData[0]) + s2;
50235      s2 += BYTESWAP32(aData[1]) + s1;
50236      aData += 2;
50237    }while( aData<aEnd );
50238  }
50239
50240  aOut[0] = s1;
50241  aOut[1] = s2;
50242}
50243
50244static void walShmBarrier(Wal *pWal){
50245  if( pWal->exclusiveMode!=WAL_HEAPMEMORY_MODE ){
50246    sqlite3OsShmBarrier(pWal->pDbFd);
50247  }
50248}
50249
50250/*
50251** Write the header information in pWal->hdr into the wal-index.
50252**
50253** The checksum on pWal->hdr is updated before it is written.
50254*/
50255static void walIndexWriteHdr(Wal *pWal){
50256  volatile WalIndexHdr *aHdr = walIndexHdr(pWal);
50257  const int nCksum = offsetof(WalIndexHdr, aCksum);
50258
50259  assert( pWal->writeLock );
50260  pWal->hdr.isInit = 1;
50261  pWal->hdr.iVersion = WALINDEX_MAX_VERSION;
50262  walChecksumBytes(1, (u8*)&pWal->hdr, nCksum, 0, pWal->hdr.aCksum);
50263  memcpy((void*)&aHdr[1], (const void*)&pWal->hdr, sizeof(WalIndexHdr));
50264  walShmBarrier(pWal);
50265  memcpy((void*)&aHdr[0], (const void*)&pWal->hdr, sizeof(WalIndexHdr));
50266}
50267
50268/*
50269** This function encodes a single frame header and writes it to a buffer
50270** supplied by the caller. A frame-header is made up of a series of
50271** 4-byte big-endian integers, as follows:
50272**
50273**     0: Page number.
50274**     4: For commit records, the size of the database image in pages
50275**        after the commit. For all other records, zero.
50276**     8: Salt-1 (copied from the wal-header)
50277**    12: Salt-2 (copied from the wal-header)
50278**    16: Checksum-1.
50279**    20: Checksum-2.
50280*/
50281static void walEncodeFrame(
50282  Wal *pWal,                      /* The write-ahead log */
50283  u32 iPage,                      /* Database page number for frame */
50284  u32 nTruncate,                  /* New db size (or 0 for non-commit frames) */
50285  u8 *aData,                      /* Pointer to page data */
50286  u8 *aFrame                      /* OUT: Write encoded frame here */
50287){
50288  int nativeCksum;                /* True for native byte-order checksums */
50289  u32 *aCksum = pWal->hdr.aFrameCksum;
50290  assert( WAL_FRAME_HDRSIZE==24 );
50291  sqlite3Put4byte(&aFrame[0], iPage);
50292  sqlite3Put4byte(&aFrame[4], nTruncate);
50293  memcpy(&aFrame[8], pWal->hdr.aSalt, 8);
50294
50295  nativeCksum = (pWal->hdr.bigEndCksum==SQLITE_BIGENDIAN);
50296  walChecksumBytes(nativeCksum, aFrame, 8, aCksum, aCksum);
50297  walChecksumBytes(nativeCksum, aData, pWal->szPage, aCksum, aCksum);
50298
50299  sqlite3Put4byte(&aFrame[16], aCksum[0]);
50300  sqlite3Put4byte(&aFrame[20], aCksum[1]);
50301}
50302
50303/*
50304** Check to see if the frame with header in aFrame[] and content
50305** in aData[] is valid.  If it is a valid frame, fill *piPage and
50306** *pnTruncate and return true.  Return if the frame is not valid.
50307*/
50308static int walDecodeFrame(
50309  Wal *pWal,                      /* The write-ahead log */
50310  u32 *piPage,                    /* OUT: Database page number for frame */
50311  u32 *pnTruncate,                /* OUT: New db size (or 0 if not commit) */
50312  u8 *aData,                      /* Pointer to page data (for checksum) */
50313  u8 *aFrame                      /* Frame data */
50314){
50315  int nativeCksum;                /* True for native byte-order checksums */
50316  u32 *aCksum = pWal->hdr.aFrameCksum;
50317  u32 pgno;                       /* Page number of the frame */
50318  assert( WAL_FRAME_HDRSIZE==24 );
50319
50320  /* A frame is only valid if the salt values in the frame-header
50321  ** match the salt values in the wal-header.
50322  */
50323  if( memcmp(&pWal->hdr.aSalt, &aFrame[8], 8)!=0 ){
50324    return 0;
50325  }
50326
50327  /* A frame is only valid if the page number is creater than zero.
50328  */
50329  pgno = sqlite3Get4byte(&aFrame[0]);
50330  if( pgno==0 ){
50331    return 0;
50332  }
50333
50334  /* A frame is only valid if a checksum of the WAL header,
50335  ** all prior frams, the first 16 bytes of this frame-header,
50336  ** and the frame-data matches the checksum in the last 8
50337  ** bytes of this frame-header.
50338  */
50339  nativeCksum = (pWal->hdr.bigEndCksum==SQLITE_BIGENDIAN);
50340  walChecksumBytes(nativeCksum, aFrame, 8, aCksum, aCksum);
50341  walChecksumBytes(nativeCksum, aData, pWal->szPage, aCksum, aCksum);
50342  if( aCksum[0]!=sqlite3Get4byte(&aFrame[16])
50343   || aCksum[1]!=sqlite3Get4byte(&aFrame[20])
50344  ){
50345    /* Checksum failed. */
50346    return 0;
50347  }
50348
50349  /* If we reach this point, the frame is valid.  Return the page number
50350  ** and the new database size.
50351  */
50352  *piPage = pgno;
50353  *pnTruncate = sqlite3Get4byte(&aFrame[4]);
50354  return 1;
50355}
50356
50357
50358#if defined(SQLITE_TEST) && defined(SQLITE_DEBUG)
50359/*
50360** Names of locks.  This routine is used to provide debugging output and is not
50361** a part of an ordinary build.
50362*/
50363static const char *walLockName(int lockIdx){
50364  if( lockIdx==WAL_WRITE_LOCK ){
50365    return "WRITE-LOCK";
50366  }else if( lockIdx==WAL_CKPT_LOCK ){
50367    return "CKPT-LOCK";
50368  }else if( lockIdx==WAL_RECOVER_LOCK ){
50369    return "RECOVER-LOCK";
50370  }else{
50371    static char zName[15];
50372    sqlite3_snprintf(sizeof(zName), zName, "READ-LOCK[%d]",
50373                     lockIdx-WAL_READ_LOCK(0));
50374    return zName;
50375  }
50376}
50377#endif /*defined(SQLITE_TEST) || defined(SQLITE_DEBUG) */
50378
50379
50380/*
50381** Set or release locks on the WAL.  Locks are either shared or exclusive.
50382** A lock cannot be moved directly between shared and exclusive - it must go
50383** through the unlocked state first.
50384**
50385** In locking_mode=EXCLUSIVE, all of these routines become no-ops.
50386*/
50387static int walLockShared(Wal *pWal, int lockIdx){
50388  int rc;
50389  if( pWal->exclusiveMode ) return SQLITE_OK;
50390  rc = sqlite3OsShmLock(pWal->pDbFd, lockIdx, 1,
50391                        SQLITE_SHM_LOCK | SQLITE_SHM_SHARED);
50392  WALTRACE(("WAL%p: acquire SHARED-%s %s\n", pWal,
50393            walLockName(lockIdx), rc ? "failed" : "ok"));
50394  VVA_ONLY( pWal->lockError = (u8)(rc!=SQLITE_OK && rc!=SQLITE_BUSY); )
50395  return rc;
50396}
50397static void walUnlockShared(Wal *pWal, int lockIdx){
50398  if( pWal->exclusiveMode ) return;
50399  (void)sqlite3OsShmLock(pWal->pDbFd, lockIdx, 1,
50400                         SQLITE_SHM_UNLOCK | SQLITE_SHM_SHARED);
50401  WALTRACE(("WAL%p: release SHARED-%s\n", pWal, walLockName(lockIdx)));
50402}
50403static int walLockExclusive(Wal *pWal, int lockIdx, int n, int fBlock){
50404  int rc;
50405  if( pWal->exclusiveMode ) return SQLITE_OK;
50406  if( fBlock ) sqlite3OsFileControl(pWal->pDbFd, SQLITE_FCNTL_WAL_BLOCK, 0);
50407  rc = sqlite3OsShmLock(pWal->pDbFd, lockIdx, n,
50408                        SQLITE_SHM_LOCK | SQLITE_SHM_EXCLUSIVE);
50409  WALTRACE(("WAL%p: acquire EXCLUSIVE-%s cnt=%d %s\n", pWal,
50410            walLockName(lockIdx), n, rc ? "failed" : "ok"));
50411  VVA_ONLY( pWal->lockError = (u8)(rc!=SQLITE_OK && rc!=SQLITE_BUSY); )
50412  return rc;
50413}
50414static void walUnlockExclusive(Wal *pWal, int lockIdx, int n){
50415  if( pWal->exclusiveMode ) return;
50416  (void)sqlite3OsShmLock(pWal->pDbFd, lockIdx, n,
50417                         SQLITE_SHM_UNLOCK | SQLITE_SHM_EXCLUSIVE);
50418  WALTRACE(("WAL%p: release EXCLUSIVE-%s cnt=%d\n", pWal,
50419             walLockName(lockIdx), n));
50420}
50421
50422/*
50423** Compute a hash on a page number.  The resulting hash value must land
50424** between 0 and (HASHTABLE_NSLOT-1).  The walHashNext() function advances
50425** the hash to the next value in the event of a collision.
50426*/
50427static int walHash(u32 iPage){
50428  assert( iPage>0 );
50429  assert( (HASHTABLE_NSLOT & (HASHTABLE_NSLOT-1))==0 );
50430  return (iPage*HASHTABLE_HASH_1) & (HASHTABLE_NSLOT-1);
50431}
50432static int walNextHash(int iPriorHash){
50433  return (iPriorHash+1)&(HASHTABLE_NSLOT-1);
50434}
50435
50436/*
50437** Return pointers to the hash table and page number array stored on
50438** page iHash of the wal-index. The wal-index is broken into 32KB pages
50439** numbered starting from 0.
50440**
50441** Set output variable *paHash to point to the start of the hash table
50442** in the wal-index file. Set *piZero to one less than the frame
50443** number of the first frame indexed by this hash table. If a
50444** slot in the hash table is set to N, it refers to frame number
50445** (*piZero+N) in the log.
50446**
50447** Finally, set *paPgno so that *paPgno[1] is the page number of the
50448** first frame indexed by the hash table, frame (*piZero+1).
50449*/
50450static int walHashGet(
50451  Wal *pWal,                      /* WAL handle */
50452  int iHash,                      /* Find the iHash'th table */
50453  volatile ht_slot **paHash,      /* OUT: Pointer to hash index */
50454  volatile u32 **paPgno,          /* OUT: Pointer to page number array */
50455  u32 *piZero                     /* OUT: Frame associated with *paPgno[0] */
50456){
50457  int rc;                         /* Return code */
50458  volatile u32 *aPgno;
50459
50460  rc = walIndexPage(pWal, iHash, &aPgno);
50461  assert( rc==SQLITE_OK || iHash>0 );
50462
50463  if( rc==SQLITE_OK ){
50464    u32 iZero;
50465    volatile ht_slot *aHash;
50466
50467    aHash = (volatile ht_slot *)&aPgno[HASHTABLE_NPAGE];
50468    if( iHash==0 ){
50469      aPgno = &aPgno[WALINDEX_HDR_SIZE/sizeof(u32)];
50470      iZero = 0;
50471    }else{
50472      iZero = HASHTABLE_NPAGE_ONE + (iHash-1)*HASHTABLE_NPAGE;
50473    }
50474
50475    *paPgno = &aPgno[-1];
50476    *paHash = aHash;
50477    *piZero = iZero;
50478  }
50479  return rc;
50480}
50481
50482/*
50483** Return the number of the wal-index page that contains the hash-table
50484** and page-number array that contain entries corresponding to WAL frame
50485** iFrame. The wal-index is broken up into 32KB pages. Wal-index pages
50486** are numbered starting from 0.
50487*/
50488static int walFramePage(u32 iFrame){
50489  int iHash = (iFrame+HASHTABLE_NPAGE-HASHTABLE_NPAGE_ONE-1) / HASHTABLE_NPAGE;
50490  assert( (iHash==0 || iFrame>HASHTABLE_NPAGE_ONE)
50491       && (iHash>=1 || iFrame<=HASHTABLE_NPAGE_ONE)
50492       && (iHash<=1 || iFrame>(HASHTABLE_NPAGE_ONE+HASHTABLE_NPAGE))
50493       && (iHash>=2 || iFrame<=HASHTABLE_NPAGE_ONE+HASHTABLE_NPAGE)
50494       && (iHash<=2 || iFrame>(HASHTABLE_NPAGE_ONE+2*HASHTABLE_NPAGE))
50495  );
50496  return iHash;
50497}
50498
50499/*
50500** Return the page number associated with frame iFrame in this WAL.
50501*/
50502static u32 walFramePgno(Wal *pWal, u32 iFrame){
50503  int iHash = walFramePage(iFrame);
50504  if( iHash==0 ){
50505    return pWal->apWiData[0][WALINDEX_HDR_SIZE/sizeof(u32) + iFrame - 1];
50506  }
50507  return pWal->apWiData[iHash][(iFrame-1-HASHTABLE_NPAGE_ONE)%HASHTABLE_NPAGE];
50508}
50509
50510/*
50511** Remove entries from the hash table that point to WAL slots greater
50512** than pWal->hdr.mxFrame.
50513**
50514** This function is called whenever pWal->hdr.mxFrame is decreased due
50515** to a rollback or savepoint.
50516**
50517** At most only the hash table containing pWal->hdr.mxFrame needs to be
50518** updated.  Any later hash tables will be automatically cleared when
50519** pWal->hdr.mxFrame advances to the point where those hash tables are
50520** actually needed.
50521*/
50522static void walCleanupHash(Wal *pWal){
50523  volatile ht_slot *aHash = 0;    /* Pointer to hash table to clear */
50524  volatile u32 *aPgno = 0;        /* Page number array for hash table */
50525  u32 iZero = 0;                  /* frame == (aHash[x]+iZero) */
50526  int iLimit = 0;                 /* Zero values greater than this */
50527  int nByte;                      /* Number of bytes to zero in aPgno[] */
50528  int i;                          /* Used to iterate through aHash[] */
50529
50530  assert( pWal->writeLock );
50531  testcase( pWal->hdr.mxFrame==HASHTABLE_NPAGE_ONE-1 );
50532  testcase( pWal->hdr.mxFrame==HASHTABLE_NPAGE_ONE );
50533  testcase( pWal->hdr.mxFrame==HASHTABLE_NPAGE_ONE+1 );
50534
50535  if( pWal->hdr.mxFrame==0 ) return;
50536
50537  /* Obtain pointers to the hash-table and page-number array containing
50538  ** the entry that corresponds to frame pWal->hdr.mxFrame. It is guaranteed
50539  ** that the page said hash-table and array reside on is already mapped.
50540  */
50541  assert( pWal->nWiData>walFramePage(pWal->hdr.mxFrame) );
50542  assert( pWal->apWiData[walFramePage(pWal->hdr.mxFrame)] );
50543  walHashGet(pWal, walFramePage(pWal->hdr.mxFrame), &aHash, &aPgno, &iZero);
50544
50545  /* Zero all hash-table entries that correspond to frame numbers greater
50546  ** than pWal->hdr.mxFrame.
50547  */
50548  iLimit = pWal->hdr.mxFrame - iZero;
50549  assert( iLimit>0 );
50550  for(i=0; i<HASHTABLE_NSLOT; i++){
50551    if( aHash[i]>iLimit ){
50552      aHash[i] = 0;
50553    }
50554  }
50555
50556  /* Zero the entries in the aPgno array that correspond to frames with
50557  ** frame numbers greater than pWal->hdr.mxFrame.
50558  */
50559  nByte = (int)((char *)aHash - (char *)&aPgno[iLimit+1]);
50560  memset((void *)&aPgno[iLimit+1], 0, nByte);
50561
50562#ifdef SQLITE_ENABLE_EXPENSIVE_ASSERT
50563  /* Verify that the every entry in the mapping region is still reachable
50564  ** via the hash table even after the cleanup.
50565  */
50566  if( iLimit ){
50567    int j;           /* Loop counter */
50568    int iKey;        /* Hash key */
50569    for(j=1; j<=iLimit; j++){
50570      for(iKey=walHash(aPgno[j]); aHash[iKey]; iKey=walNextHash(iKey)){
50571        if( aHash[iKey]==j ) break;
50572      }
50573      assert( aHash[iKey]==j );
50574    }
50575  }
50576#endif /* SQLITE_ENABLE_EXPENSIVE_ASSERT */
50577}
50578
50579
50580/*
50581** Set an entry in the wal-index that will map database page number
50582** pPage into WAL frame iFrame.
50583*/
50584static int walIndexAppend(Wal *pWal, u32 iFrame, u32 iPage){
50585  int rc;                         /* Return code */
50586  u32 iZero = 0;                  /* One less than frame number of aPgno[1] */
50587  volatile u32 *aPgno = 0;        /* Page number array */
50588  volatile ht_slot *aHash = 0;    /* Hash table */
50589
50590  rc = walHashGet(pWal, walFramePage(iFrame), &aHash, &aPgno, &iZero);
50591
50592  /* Assuming the wal-index file was successfully mapped, populate the
50593  ** page number array and hash table entry.
50594  */
50595  if( rc==SQLITE_OK ){
50596    int iKey;                     /* Hash table key */
50597    int idx;                      /* Value to write to hash-table slot */
50598    int nCollide;                 /* Number of hash collisions */
50599
50600    idx = iFrame - iZero;
50601    assert( idx <= HASHTABLE_NSLOT/2 + 1 );
50602
50603    /* If this is the first entry to be added to this hash-table, zero the
50604    ** entire hash table and aPgno[] array before proceeding.
50605    */
50606    if( idx==1 ){
50607      int nByte = (int)((u8 *)&aHash[HASHTABLE_NSLOT] - (u8 *)&aPgno[1]);
50608      memset((void*)&aPgno[1], 0, nByte);
50609    }
50610
50611    /* If the entry in aPgno[] is already set, then the previous writer
50612    ** must have exited unexpectedly in the middle of a transaction (after
50613    ** writing one or more dirty pages to the WAL to free up memory).
50614    ** Remove the remnants of that writers uncommitted transaction from
50615    ** the hash-table before writing any new entries.
50616    */
50617    if( aPgno[idx] ){
50618      walCleanupHash(pWal);
50619      assert( !aPgno[idx] );
50620    }
50621
50622    /* Write the aPgno[] array entry and the hash-table slot. */
50623    nCollide = idx;
50624    for(iKey=walHash(iPage); aHash[iKey]; iKey=walNextHash(iKey)){
50625      if( (nCollide--)==0 ) return SQLITE_CORRUPT_BKPT;
50626    }
50627    aPgno[idx] = iPage;
50628    aHash[iKey] = (ht_slot)idx;
50629
50630#ifdef SQLITE_ENABLE_EXPENSIVE_ASSERT
50631    /* Verify that the number of entries in the hash table exactly equals
50632    ** the number of entries in the mapping region.
50633    */
50634    {
50635      int i;           /* Loop counter */
50636      int nEntry = 0;  /* Number of entries in the hash table */
50637      for(i=0; i<HASHTABLE_NSLOT; i++){ if( aHash[i] ) nEntry++; }
50638      assert( nEntry==idx );
50639    }
50640
50641    /* Verify that the every entry in the mapping region is reachable
50642    ** via the hash table.  This turns out to be a really, really expensive
50643    ** thing to check, so only do this occasionally - not on every
50644    ** iteration.
50645    */
50646    if( (idx&0x3ff)==0 ){
50647      int i;           /* Loop counter */
50648      for(i=1; i<=idx; i++){
50649        for(iKey=walHash(aPgno[i]); aHash[iKey]; iKey=walNextHash(iKey)){
50650          if( aHash[iKey]==i ) break;
50651        }
50652        assert( aHash[iKey]==i );
50653      }
50654    }
50655#endif /* SQLITE_ENABLE_EXPENSIVE_ASSERT */
50656  }
50657
50658
50659  return rc;
50660}
50661
50662
50663/*
50664** Recover the wal-index by reading the write-ahead log file.
50665**
50666** This routine first tries to establish an exclusive lock on the
50667** wal-index to prevent other threads/processes from doing anything
50668** with the WAL or wal-index while recovery is running.  The
50669** WAL_RECOVER_LOCK is also held so that other threads will know
50670** that this thread is running recovery.  If unable to establish
50671** the necessary locks, this routine returns SQLITE_BUSY.
50672*/
50673static int walIndexRecover(Wal *pWal){
50674  int rc;                         /* Return Code */
50675  i64 nSize;                      /* Size of log file */
50676  u32 aFrameCksum[2] = {0, 0};
50677  int iLock;                      /* Lock offset to lock for checkpoint */
50678  int nLock;                      /* Number of locks to hold */
50679
50680  /* Obtain an exclusive lock on all byte in the locking range not already
50681  ** locked by the caller. The caller is guaranteed to have locked the
50682  ** WAL_WRITE_LOCK byte, and may have also locked the WAL_CKPT_LOCK byte.
50683  ** If successful, the same bytes that are locked here are unlocked before
50684  ** this function returns.
50685  */
50686  assert( pWal->ckptLock==1 || pWal->ckptLock==0 );
50687  assert( WAL_ALL_BUT_WRITE==WAL_WRITE_LOCK+1 );
50688  assert( WAL_CKPT_LOCK==WAL_ALL_BUT_WRITE );
50689  assert( pWal->writeLock );
50690  iLock = WAL_ALL_BUT_WRITE + pWal->ckptLock;
50691  nLock = SQLITE_SHM_NLOCK - iLock;
50692  rc = walLockExclusive(pWal, iLock, nLock, 0);
50693  if( rc ){
50694    return rc;
50695  }
50696  WALTRACE(("WAL%p: recovery begin...\n", pWal));
50697
50698  memset(&pWal->hdr, 0, sizeof(WalIndexHdr));
50699
50700  rc = sqlite3OsFileSize(pWal->pWalFd, &nSize);
50701  if( rc!=SQLITE_OK ){
50702    goto recovery_error;
50703  }
50704
50705  if( nSize>WAL_HDRSIZE ){
50706    u8 aBuf[WAL_HDRSIZE];         /* Buffer to load WAL header into */
50707    u8 *aFrame = 0;               /* Malloc'd buffer to load entire frame */
50708    int szFrame;                  /* Number of bytes in buffer aFrame[] */
50709    u8 *aData;                    /* Pointer to data part of aFrame buffer */
50710    int iFrame;                   /* Index of last frame read */
50711    i64 iOffset;                  /* Next offset to read from log file */
50712    int szPage;                   /* Page size according to the log */
50713    u32 magic;                    /* Magic value read from WAL header */
50714    u32 version;                  /* Magic value read from WAL header */
50715    int isValid;                  /* True if this frame is valid */
50716
50717    /* Read in the WAL header. */
50718    rc = sqlite3OsRead(pWal->pWalFd, aBuf, WAL_HDRSIZE, 0);
50719    if( rc!=SQLITE_OK ){
50720      goto recovery_error;
50721    }
50722
50723    /* If the database page size is not a power of two, or is greater than
50724    ** SQLITE_MAX_PAGE_SIZE, conclude that the WAL file contains no valid
50725    ** data. Similarly, if the 'magic' value is invalid, ignore the whole
50726    ** WAL file.
50727    */
50728    magic = sqlite3Get4byte(&aBuf[0]);
50729    szPage = sqlite3Get4byte(&aBuf[8]);
50730    if( (magic&0xFFFFFFFE)!=WAL_MAGIC
50731     || szPage&(szPage-1)
50732     || szPage>SQLITE_MAX_PAGE_SIZE
50733     || szPage<512
50734    ){
50735      goto finished;
50736    }
50737    pWal->hdr.bigEndCksum = (u8)(magic&0x00000001);
50738    pWal->szPage = szPage;
50739    pWal->nCkpt = sqlite3Get4byte(&aBuf[12]);
50740    memcpy(&pWal->hdr.aSalt, &aBuf[16], 8);
50741
50742    /* Verify that the WAL header checksum is correct */
50743    walChecksumBytes(pWal->hdr.bigEndCksum==SQLITE_BIGENDIAN,
50744        aBuf, WAL_HDRSIZE-2*4, 0, pWal->hdr.aFrameCksum
50745    );
50746    if( pWal->hdr.aFrameCksum[0]!=sqlite3Get4byte(&aBuf[24])
50747     || pWal->hdr.aFrameCksum[1]!=sqlite3Get4byte(&aBuf[28])
50748    ){
50749      goto finished;
50750    }
50751
50752    /* Verify that the version number on the WAL format is one that
50753    ** are able to understand */
50754    version = sqlite3Get4byte(&aBuf[4]);
50755    if( version!=WAL_MAX_VERSION ){
50756      rc = SQLITE_CANTOPEN_BKPT;
50757      goto finished;
50758    }
50759
50760    /* Malloc a buffer to read frames into. */
50761    szFrame = szPage + WAL_FRAME_HDRSIZE;
50762    aFrame = (u8 *)sqlite3_malloc64(szFrame);
50763    if( !aFrame ){
50764      rc = SQLITE_NOMEM;
50765      goto recovery_error;
50766    }
50767    aData = &aFrame[WAL_FRAME_HDRSIZE];
50768
50769    /* Read all frames from the log file. */
50770    iFrame = 0;
50771    for(iOffset=WAL_HDRSIZE; (iOffset+szFrame)<=nSize; iOffset+=szFrame){
50772      u32 pgno;                   /* Database page number for frame */
50773      u32 nTruncate;              /* dbsize field from frame header */
50774
50775      /* Read and decode the next log frame. */
50776      iFrame++;
50777      rc = sqlite3OsRead(pWal->pWalFd, aFrame, szFrame, iOffset);
50778      if( rc!=SQLITE_OK ) break;
50779      isValid = walDecodeFrame(pWal, &pgno, &nTruncate, aData, aFrame);
50780      if( !isValid ) break;
50781      rc = walIndexAppend(pWal, iFrame, pgno);
50782      if( rc!=SQLITE_OK ) break;
50783
50784      /* If nTruncate is non-zero, this is a commit record. */
50785      if( nTruncate ){
50786        pWal->hdr.mxFrame = iFrame;
50787        pWal->hdr.nPage = nTruncate;
50788        pWal->hdr.szPage = (u16)((szPage&0xff00) | (szPage>>16));
50789        testcase( szPage<=32768 );
50790        testcase( szPage>=65536 );
50791        aFrameCksum[0] = pWal->hdr.aFrameCksum[0];
50792        aFrameCksum[1] = pWal->hdr.aFrameCksum[1];
50793      }
50794    }
50795
50796    sqlite3_free(aFrame);
50797  }
50798
50799finished:
50800  if( rc==SQLITE_OK ){
50801    volatile WalCkptInfo *pInfo;
50802    int i;
50803    pWal->hdr.aFrameCksum[0] = aFrameCksum[0];
50804    pWal->hdr.aFrameCksum[1] = aFrameCksum[1];
50805    walIndexWriteHdr(pWal);
50806
50807    /* Reset the checkpoint-header. This is safe because this thread is
50808    ** currently holding locks that exclude all other readers, writers and
50809    ** checkpointers.
50810    */
50811    pInfo = walCkptInfo(pWal);
50812    pInfo->nBackfill = 0;
50813    pInfo->aReadMark[0] = 0;
50814    for(i=1; i<WAL_NREADER; i++) pInfo->aReadMark[i] = READMARK_NOT_USED;
50815    if( pWal->hdr.mxFrame ) pInfo->aReadMark[1] = pWal->hdr.mxFrame;
50816
50817    /* If more than one frame was recovered from the log file, report an
50818    ** event via sqlite3_log(). This is to help with identifying performance
50819    ** problems caused by applications routinely shutting down without
50820    ** checkpointing the log file.
50821    */
50822    if( pWal->hdr.nPage ){
50823      sqlite3_log(SQLITE_NOTICE_RECOVER_WAL,
50824          "recovered %d frames from WAL file %s",
50825          pWal->hdr.mxFrame, pWal->zWalName
50826      );
50827    }
50828  }
50829
50830recovery_error:
50831  WALTRACE(("WAL%p: recovery %s\n", pWal, rc ? "failed" : "ok"));
50832  walUnlockExclusive(pWal, iLock, nLock);
50833  return rc;
50834}
50835
50836/*
50837** Close an open wal-index.
50838*/
50839static void walIndexClose(Wal *pWal, int isDelete){
50840  if( pWal->exclusiveMode==WAL_HEAPMEMORY_MODE ){
50841    int i;
50842    for(i=0; i<pWal->nWiData; i++){
50843      sqlite3_free((void *)pWal->apWiData[i]);
50844      pWal->apWiData[i] = 0;
50845    }
50846  }else{
50847    sqlite3OsShmUnmap(pWal->pDbFd, isDelete);
50848  }
50849}
50850
50851/*
50852** Open a connection to the WAL file zWalName. The database file must
50853** already be opened on connection pDbFd. The buffer that zWalName points
50854** to must remain valid for the lifetime of the returned Wal* handle.
50855**
50856** A SHARED lock should be held on the database file when this function
50857** is called. The purpose of this SHARED lock is to prevent any other
50858** client from unlinking the WAL or wal-index file. If another process
50859** were to do this just after this client opened one of these files, the
50860** system would be badly broken.
50861**
50862** If the log file is successfully opened, SQLITE_OK is returned and
50863** *ppWal is set to point to a new WAL handle. If an error occurs,
50864** an SQLite error code is returned and *ppWal is left unmodified.
50865*/
50866SQLITE_PRIVATE int sqlite3WalOpen(
50867  sqlite3_vfs *pVfs,              /* vfs module to open wal and wal-index */
50868  sqlite3_file *pDbFd,            /* The open database file */
50869  const char *zWalName,           /* Name of the WAL file */
50870  int bNoShm,                     /* True to run in heap-memory mode */
50871  i64 mxWalSize,                  /* Truncate WAL to this size on reset */
50872  Wal **ppWal                     /* OUT: Allocated Wal handle */
50873){
50874  int rc;                         /* Return Code */
50875  Wal *pRet;                      /* Object to allocate and return */
50876  int flags;                      /* Flags passed to OsOpen() */
50877
50878  assert( zWalName && zWalName[0] );
50879  assert( pDbFd );
50880
50881  /* In the amalgamation, the os_unix.c and os_win.c source files come before
50882  ** this source file.  Verify that the #defines of the locking byte offsets
50883  ** in os_unix.c and os_win.c agree with the WALINDEX_LOCK_OFFSET value.
50884  */
50885#ifdef WIN_SHM_BASE
50886  assert( WIN_SHM_BASE==WALINDEX_LOCK_OFFSET );
50887#endif
50888#ifdef UNIX_SHM_BASE
50889  assert( UNIX_SHM_BASE==WALINDEX_LOCK_OFFSET );
50890#endif
50891
50892
50893  /* Allocate an instance of struct Wal to return. */
50894  *ppWal = 0;
50895  pRet = (Wal*)sqlite3MallocZero(sizeof(Wal) + pVfs->szOsFile);
50896  if( !pRet ){
50897    return SQLITE_NOMEM;
50898  }
50899
50900  pRet->pVfs = pVfs;
50901  pRet->pWalFd = (sqlite3_file *)&pRet[1];
50902  pRet->pDbFd = pDbFd;
50903  pRet->readLock = -1;
50904  pRet->mxWalSize = mxWalSize;
50905  pRet->zWalName = zWalName;
50906  pRet->syncHeader = 1;
50907  pRet->padToSectorBoundary = 1;
50908  pRet->exclusiveMode = (bNoShm ? WAL_HEAPMEMORY_MODE: WAL_NORMAL_MODE);
50909
50910  /* Open file handle on the write-ahead log file. */
50911  flags = (SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|SQLITE_OPEN_WAL);
50912  rc = sqlite3OsOpen(pVfs, zWalName, pRet->pWalFd, flags, &flags);
50913  if( rc==SQLITE_OK && flags&SQLITE_OPEN_READONLY ){
50914    pRet->readOnly = WAL_RDONLY;
50915  }
50916
50917  if( rc!=SQLITE_OK ){
50918    walIndexClose(pRet, 0);
50919    sqlite3OsClose(pRet->pWalFd);
50920    sqlite3_free(pRet);
50921  }else{
50922    int iDC = sqlite3OsDeviceCharacteristics(pDbFd);
50923    if( iDC & SQLITE_IOCAP_SEQUENTIAL ){ pRet->syncHeader = 0; }
50924    if( iDC & SQLITE_IOCAP_POWERSAFE_OVERWRITE ){
50925      pRet->padToSectorBoundary = 0;
50926    }
50927    *ppWal = pRet;
50928    WALTRACE(("WAL%d: opened\n", pRet));
50929  }
50930  return rc;
50931}
50932
50933/*
50934** Change the size to which the WAL file is trucated on each reset.
50935*/
50936SQLITE_PRIVATE void sqlite3WalLimit(Wal *pWal, i64 iLimit){
50937  if( pWal ) pWal->mxWalSize = iLimit;
50938}
50939
50940/*
50941** Find the smallest page number out of all pages held in the WAL that
50942** has not been returned by any prior invocation of this method on the
50943** same WalIterator object.   Write into *piFrame the frame index where
50944** that page was last written into the WAL.  Write into *piPage the page
50945** number.
50946**
50947** Return 0 on success.  If there are no pages in the WAL with a page
50948** number larger than *piPage, then return 1.
50949*/
50950static int walIteratorNext(
50951  WalIterator *p,               /* Iterator */
50952  u32 *piPage,                  /* OUT: The page number of the next page */
50953  u32 *piFrame                  /* OUT: Wal frame index of next page */
50954){
50955  u32 iMin;                     /* Result pgno must be greater than iMin */
50956  u32 iRet = 0xFFFFFFFF;        /* 0xffffffff is never a valid page number */
50957  int i;                        /* For looping through segments */
50958
50959  iMin = p->iPrior;
50960  assert( iMin<0xffffffff );
50961  for(i=p->nSegment-1; i>=0; i--){
50962    struct WalSegment *pSegment = &p->aSegment[i];
50963    while( pSegment->iNext<pSegment->nEntry ){
50964      u32 iPg = pSegment->aPgno[pSegment->aIndex[pSegment->iNext]];
50965      if( iPg>iMin ){
50966        if( iPg<iRet ){
50967          iRet = iPg;
50968          *piFrame = pSegment->iZero + pSegment->aIndex[pSegment->iNext];
50969        }
50970        break;
50971      }
50972      pSegment->iNext++;
50973    }
50974  }
50975
50976  *piPage = p->iPrior = iRet;
50977  return (iRet==0xFFFFFFFF);
50978}
50979
50980/*
50981** This function merges two sorted lists into a single sorted list.
50982**
50983** aLeft[] and aRight[] are arrays of indices.  The sort key is
50984** aContent[aLeft[]] and aContent[aRight[]].  Upon entry, the following
50985** is guaranteed for all J<K:
50986**
50987**        aContent[aLeft[J]] < aContent[aLeft[K]]
50988**        aContent[aRight[J]] < aContent[aRight[K]]
50989**
50990** This routine overwrites aRight[] with a new (probably longer) sequence
50991** of indices such that the aRight[] contains every index that appears in
50992** either aLeft[] or the old aRight[] and such that the second condition
50993** above is still met.
50994**
50995** The aContent[aLeft[X]] values will be unique for all X.  And the
50996** aContent[aRight[X]] values will be unique too.  But there might be
50997** one or more combinations of X and Y such that
50998**
50999**      aLeft[X]!=aRight[Y]  &&  aContent[aLeft[X]] == aContent[aRight[Y]]
51000**
51001** When that happens, omit the aLeft[X] and use the aRight[Y] index.
51002*/
51003static void walMerge(
51004  const u32 *aContent,            /* Pages in wal - keys for the sort */
51005  ht_slot *aLeft,                 /* IN: Left hand input list */
51006  int nLeft,                      /* IN: Elements in array *paLeft */
51007  ht_slot **paRight,              /* IN/OUT: Right hand input list */
51008  int *pnRight,                   /* IN/OUT: Elements in *paRight */
51009  ht_slot *aTmp                   /* Temporary buffer */
51010){
51011  int iLeft = 0;                  /* Current index in aLeft */
51012  int iRight = 0;                 /* Current index in aRight */
51013  int iOut = 0;                   /* Current index in output buffer */
51014  int nRight = *pnRight;
51015  ht_slot *aRight = *paRight;
51016
51017  assert( nLeft>0 && nRight>0 );
51018  while( iRight<nRight || iLeft<nLeft ){
51019    ht_slot logpage;
51020    Pgno dbpage;
51021
51022    if( (iLeft<nLeft)
51023     && (iRight>=nRight || aContent[aLeft[iLeft]]<aContent[aRight[iRight]])
51024    ){
51025      logpage = aLeft[iLeft++];
51026    }else{
51027      logpage = aRight[iRight++];
51028    }
51029    dbpage = aContent[logpage];
51030
51031    aTmp[iOut++] = logpage;
51032    if( iLeft<nLeft && aContent[aLeft[iLeft]]==dbpage ) iLeft++;
51033
51034    assert( iLeft>=nLeft || aContent[aLeft[iLeft]]>dbpage );
51035    assert( iRight>=nRight || aContent[aRight[iRight]]>dbpage );
51036  }
51037
51038  *paRight = aLeft;
51039  *pnRight = iOut;
51040  memcpy(aLeft, aTmp, sizeof(aTmp[0])*iOut);
51041}
51042
51043/*
51044** Sort the elements in list aList using aContent[] as the sort key.
51045** Remove elements with duplicate keys, preferring to keep the
51046** larger aList[] values.
51047**
51048** The aList[] entries are indices into aContent[].  The values in
51049** aList[] are to be sorted so that for all J<K:
51050**
51051**      aContent[aList[J]] < aContent[aList[K]]
51052**
51053** For any X and Y such that
51054**
51055**      aContent[aList[X]] == aContent[aList[Y]]
51056**
51057** Keep the larger of the two values aList[X] and aList[Y] and discard
51058** the smaller.
51059*/
51060static void walMergesort(
51061  const u32 *aContent,            /* Pages in wal */
51062  ht_slot *aBuffer,               /* Buffer of at least *pnList items to use */
51063  ht_slot *aList,                 /* IN/OUT: List to sort */
51064  int *pnList                     /* IN/OUT: Number of elements in aList[] */
51065){
51066  struct Sublist {
51067    int nList;                    /* Number of elements in aList */
51068    ht_slot *aList;               /* Pointer to sub-list content */
51069  };
51070
51071  const int nList = *pnList;      /* Size of input list */
51072  int nMerge = 0;                 /* Number of elements in list aMerge */
51073  ht_slot *aMerge = 0;            /* List to be merged */
51074  int iList;                      /* Index into input list */
51075  u32 iSub = 0;                   /* Index into aSub array */
51076  struct Sublist aSub[13];        /* Array of sub-lists */
51077
51078  memset(aSub, 0, sizeof(aSub));
51079  assert( nList<=HASHTABLE_NPAGE && nList>0 );
51080  assert( HASHTABLE_NPAGE==(1<<(ArraySize(aSub)-1)) );
51081
51082  for(iList=0; iList<nList; iList++){
51083    nMerge = 1;
51084    aMerge = &aList[iList];
51085    for(iSub=0; iList & (1<<iSub); iSub++){
51086      struct Sublist *p;
51087      assert( iSub<ArraySize(aSub) );
51088      p = &aSub[iSub];
51089      assert( p->aList && p->nList<=(1<<iSub) );
51090      assert( p->aList==&aList[iList&~((2<<iSub)-1)] );
51091      walMerge(aContent, p->aList, p->nList, &aMerge, &nMerge, aBuffer);
51092    }
51093    aSub[iSub].aList = aMerge;
51094    aSub[iSub].nList = nMerge;
51095  }
51096
51097  for(iSub++; iSub<ArraySize(aSub); iSub++){
51098    if( nList & (1<<iSub) ){
51099      struct Sublist *p;
51100      assert( iSub<ArraySize(aSub) );
51101      p = &aSub[iSub];
51102      assert( p->nList<=(1<<iSub) );
51103      assert( p->aList==&aList[nList&~((2<<iSub)-1)] );
51104      walMerge(aContent, p->aList, p->nList, &aMerge, &nMerge, aBuffer);
51105    }
51106  }
51107  assert( aMerge==aList );
51108  *pnList = nMerge;
51109
51110#ifdef SQLITE_DEBUG
51111  {
51112    int i;
51113    for(i=1; i<*pnList; i++){
51114      assert( aContent[aList[i]] > aContent[aList[i-1]] );
51115    }
51116  }
51117#endif
51118}
51119
51120/*
51121** Free an iterator allocated by walIteratorInit().
51122*/
51123static void walIteratorFree(WalIterator *p){
51124  sqlite3_free(p);
51125}
51126
51127/*
51128** Construct a WalInterator object that can be used to loop over all
51129** pages in the WAL in ascending order. The caller must hold the checkpoint
51130** lock.
51131**
51132** On success, make *pp point to the newly allocated WalInterator object
51133** return SQLITE_OK. Otherwise, return an error code. If this routine
51134** returns an error, the value of *pp is undefined.
51135**
51136** The calling routine should invoke walIteratorFree() to destroy the
51137** WalIterator object when it has finished with it.
51138*/
51139static int walIteratorInit(Wal *pWal, WalIterator **pp){
51140  WalIterator *p;                 /* Return value */
51141  int nSegment;                   /* Number of segments to merge */
51142  u32 iLast;                      /* Last frame in log */
51143  int nByte;                      /* Number of bytes to allocate */
51144  int i;                          /* Iterator variable */
51145  ht_slot *aTmp;                  /* Temp space used by merge-sort */
51146  int rc = SQLITE_OK;             /* Return Code */
51147
51148  /* This routine only runs while holding the checkpoint lock. And
51149  ** it only runs if there is actually content in the log (mxFrame>0).
51150  */
51151  assert( pWal->ckptLock && pWal->hdr.mxFrame>0 );
51152  iLast = pWal->hdr.mxFrame;
51153
51154  /* Allocate space for the WalIterator object. */
51155  nSegment = walFramePage(iLast) + 1;
51156  nByte = sizeof(WalIterator)
51157        + (nSegment-1)*sizeof(struct WalSegment)
51158        + iLast*sizeof(ht_slot);
51159  p = (WalIterator *)sqlite3_malloc64(nByte);
51160  if( !p ){
51161    return SQLITE_NOMEM;
51162  }
51163  memset(p, 0, nByte);
51164  p->nSegment = nSegment;
51165
51166  /* Allocate temporary space used by the merge-sort routine. This block
51167  ** of memory will be freed before this function returns.
51168  */
51169  aTmp = (ht_slot *)sqlite3_malloc64(
51170      sizeof(ht_slot) * (iLast>HASHTABLE_NPAGE?HASHTABLE_NPAGE:iLast)
51171  );
51172  if( !aTmp ){
51173    rc = SQLITE_NOMEM;
51174  }
51175
51176  for(i=0; rc==SQLITE_OK && i<nSegment; i++){
51177    volatile ht_slot *aHash;
51178    u32 iZero;
51179    volatile u32 *aPgno;
51180
51181    rc = walHashGet(pWal, i, &aHash, &aPgno, &iZero);
51182    if( rc==SQLITE_OK ){
51183      int j;                      /* Counter variable */
51184      int nEntry;                 /* Number of entries in this segment */
51185      ht_slot *aIndex;            /* Sorted index for this segment */
51186
51187      aPgno++;
51188      if( (i+1)==nSegment ){
51189        nEntry = (int)(iLast - iZero);
51190      }else{
51191        nEntry = (int)((u32*)aHash - (u32*)aPgno);
51192      }
51193      aIndex = &((ht_slot *)&p->aSegment[p->nSegment])[iZero];
51194      iZero++;
51195
51196      for(j=0; j<nEntry; j++){
51197        aIndex[j] = (ht_slot)j;
51198      }
51199      walMergesort((u32 *)aPgno, aTmp, aIndex, &nEntry);
51200      p->aSegment[i].iZero = iZero;
51201      p->aSegment[i].nEntry = nEntry;
51202      p->aSegment[i].aIndex = aIndex;
51203      p->aSegment[i].aPgno = (u32 *)aPgno;
51204    }
51205  }
51206  sqlite3_free(aTmp);
51207
51208  if( rc!=SQLITE_OK ){
51209    walIteratorFree(p);
51210  }
51211  *pp = p;
51212  return rc;
51213}
51214
51215/*
51216** Attempt to obtain the exclusive WAL lock defined by parameters lockIdx and
51217** n. If the attempt fails and parameter xBusy is not NULL, then it is a
51218** busy-handler function. Invoke it and retry the lock until either the
51219** lock is successfully obtained or the busy-handler returns 0.
51220*/
51221static int walBusyLock(
51222  Wal *pWal,                      /* WAL connection */
51223  int (*xBusy)(void*),            /* Function to call when busy */
51224  void *pBusyArg,                 /* Context argument for xBusyHandler */
51225  int lockIdx,                    /* Offset of first byte to lock */
51226  int n                           /* Number of bytes to lock */
51227){
51228  int rc;
51229  do {
51230    rc = walLockExclusive(pWal, lockIdx, n, 0);
51231  }while( xBusy && rc==SQLITE_BUSY && xBusy(pBusyArg) );
51232  return rc;
51233}
51234
51235/*
51236** The cache of the wal-index header must be valid to call this function.
51237** Return the page-size in bytes used by the database.
51238*/
51239static int walPagesize(Wal *pWal){
51240  return (pWal->hdr.szPage&0xfe00) + ((pWal->hdr.szPage&0x0001)<<16);
51241}
51242
51243/*
51244** The following is guaranteed when this function is called:
51245**
51246**   a) the WRITER lock is held,
51247**   b) the entire log file has been checkpointed, and
51248**   c) any existing readers are reading exclusively from the database
51249**      file - there are no readers that may attempt to read a frame from
51250**      the log file.
51251**
51252** This function updates the shared-memory structures so that the next
51253** client to write to the database (which may be this one) does so by
51254** writing frames into the start of the log file.
51255**
51256** The value of parameter salt1 is used as the aSalt[1] value in the
51257** new wal-index header. It should be passed a pseudo-random value (i.e.
51258** one obtained from sqlite3_randomness()).
51259*/
51260static void walRestartHdr(Wal *pWal, u32 salt1){
51261  volatile WalCkptInfo *pInfo = walCkptInfo(pWal);
51262  int i;                          /* Loop counter */
51263  u32 *aSalt = pWal->hdr.aSalt;   /* Big-endian salt values */
51264  pWal->nCkpt++;
51265  pWal->hdr.mxFrame = 0;
51266  sqlite3Put4byte((u8*)&aSalt[0], 1 + sqlite3Get4byte((u8*)&aSalt[0]));
51267  memcpy(&pWal->hdr.aSalt[1], &salt1, 4);
51268  walIndexWriteHdr(pWal);
51269  pInfo->nBackfill = 0;
51270  pInfo->aReadMark[1] = 0;
51271  for(i=2; i<WAL_NREADER; i++) pInfo->aReadMark[i] = READMARK_NOT_USED;
51272  assert( pInfo->aReadMark[0]==0 );
51273}
51274
51275/*
51276** Copy as much content as we can from the WAL back into the database file
51277** in response to an sqlite3_wal_checkpoint() request or the equivalent.
51278**
51279** The amount of information copies from WAL to database might be limited
51280** by active readers.  This routine will never overwrite a database page
51281** that a concurrent reader might be using.
51282**
51283** All I/O barrier operations (a.k.a fsyncs) occur in this routine when
51284** SQLite is in WAL-mode in synchronous=NORMAL.  That means that if
51285** checkpoints are always run by a background thread or background
51286** process, foreground threads will never block on a lengthy fsync call.
51287**
51288** Fsync is called on the WAL before writing content out of the WAL and
51289** into the database.  This ensures that if the new content is persistent
51290** in the WAL and can be recovered following a power-loss or hard reset.
51291**
51292** Fsync is also called on the database file if (and only if) the entire
51293** WAL content is copied into the database file.  This second fsync makes
51294** it safe to delete the WAL since the new content will persist in the
51295** database file.
51296**
51297** This routine uses and updates the nBackfill field of the wal-index header.
51298** This is the only routine that will increase the value of nBackfill.
51299** (A WAL reset or recovery will revert nBackfill to zero, but not increase
51300** its value.)
51301**
51302** The caller must be holding sufficient locks to ensure that no other
51303** checkpoint is running (in any other thread or process) at the same
51304** time.
51305*/
51306static int walCheckpoint(
51307  Wal *pWal,                      /* Wal connection */
51308  int eMode,                      /* One of PASSIVE, FULL or RESTART */
51309  int (*xBusy)(void*),            /* Function to call when busy */
51310  void *pBusyArg,                 /* Context argument for xBusyHandler */
51311  int sync_flags,                 /* Flags for OsSync() (or 0) */
51312  u8 *zBuf                        /* Temporary buffer to use */
51313){
51314  int rc = SQLITE_OK;             /* Return code */
51315  int szPage;                     /* Database page-size */
51316  WalIterator *pIter = 0;         /* Wal iterator context */
51317  u32 iDbpage = 0;                /* Next database page to write */
51318  u32 iFrame = 0;                 /* Wal frame containing data for iDbpage */
51319  u32 mxSafeFrame;                /* Max frame that can be backfilled */
51320  u32 mxPage;                     /* Max database page to write */
51321  int i;                          /* Loop counter */
51322  volatile WalCkptInfo *pInfo;    /* The checkpoint status information */
51323
51324  szPage = walPagesize(pWal);
51325  testcase( szPage<=32768 );
51326  testcase( szPage>=65536 );
51327  pInfo = walCkptInfo(pWal);
51328  if( pInfo->nBackfill<pWal->hdr.mxFrame ){
51329
51330    /* Allocate the iterator */
51331    rc = walIteratorInit(pWal, &pIter);
51332    if( rc!=SQLITE_OK ){
51333      return rc;
51334    }
51335    assert( pIter );
51336
51337    /* EVIDENCE-OF: R-62920-47450 The busy-handler callback is never invoked
51338    ** in the SQLITE_CHECKPOINT_PASSIVE mode. */
51339    assert( eMode!=SQLITE_CHECKPOINT_PASSIVE || xBusy==0 );
51340
51341    /* Compute in mxSafeFrame the index of the last frame of the WAL that is
51342    ** safe to write into the database.  Frames beyond mxSafeFrame might
51343    ** overwrite database pages that are in use by active readers and thus
51344    ** cannot be backfilled from the WAL.
51345    */
51346    mxSafeFrame = pWal->hdr.mxFrame;
51347    mxPage = pWal->hdr.nPage;
51348    for(i=1; i<WAL_NREADER; i++){
51349      /* Thread-sanitizer reports that the following is an unsafe read,
51350      ** as some other thread may be in the process of updating the value
51351      ** of the aReadMark[] slot. The assumption here is that if that is
51352      ** happening, the other client may only be increasing the value,
51353      ** not decreasing it. So assuming either that either the "old" or
51354      ** "new" version of the value is read, and not some arbitrary value
51355      ** that would never be written by a real client, things are still
51356      ** safe.  */
51357      u32 y = pInfo->aReadMark[i];
51358      if( mxSafeFrame>y ){
51359        assert( y<=pWal->hdr.mxFrame );
51360        rc = walBusyLock(pWal, xBusy, pBusyArg, WAL_READ_LOCK(i), 1);
51361        if( rc==SQLITE_OK ){
51362          pInfo->aReadMark[i] = (i==1 ? mxSafeFrame : READMARK_NOT_USED);
51363          walUnlockExclusive(pWal, WAL_READ_LOCK(i), 1);
51364        }else if( rc==SQLITE_BUSY ){
51365          mxSafeFrame = y;
51366          xBusy = 0;
51367        }else{
51368          goto walcheckpoint_out;
51369        }
51370      }
51371    }
51372
51373    if( pInfo->nBackfill<mxSafeFrame
51374     && (rc = walBusyLock(pWal, xBusy, pBusyArg, WAL_READ_LOCK(0),1))==SQLITE_OK
51375    ){
51376      i64 nSize;                    /* Current size of database file */
51377      u32 nBackfill = pInfo->nBackfill;
51378
51379      /* Sync the WAL to disk */
51380      if( sync_flags ){
51381        rc = sqlite3OsSync(pWal->pWalFd, sync_flags);
51382      }
51383
51384      /* If the database may grow as a result of this checkpoint, hint
51385      ** about the eventual size of the db file to the VFS layer.
51386      */
51387      if( rc==SQLITE_OK ){
51388        i64 nReq = ((i64)mxPage * szPage);
51389        rc = sqlite3OsFileSize(pWal->pDbFd, &nSize);
51390        if( rc==SQLITE_OK && nSize<nReq ){
51391          sqlite3OsFileControlHint(pWal->pDbFd, SQLITE_FCNTL_SIZE_HINT, &nReq);
51392        }
51393      }
51394
51395
51396      /* Iterate through the contents of the WAL, copying data to the db file */
51397      while( rc==SQLITE_OK && 0==walIteratorNext(pIter, &iDbpage, &iFrame) ){
51398        i64 iOffset;
51399        assert( walFramePgno(pWal, iFrame)==iDbpage );
51400        if( iFrame<=nBackfill || iFrame>mxSafeFrame || iDbpage>mxPage ){
51401          continue;
51402        }
51403        iOffset = walFrameOffset(iFrame, szPage) + WAL_FRAME_HDRSIZE;
51404        /* testcase( IS_BIG_INT(iOffset) ); // requires a 4GiB WAL file */
51405        rc = sqlite3OsRead(pWal->pWalFd, zBuf, szPage, iOffset);
51406        if( rc!=SQLITE_OK ) break;
51407        iOffset = (iDbpage-1)*(i64)szPage;
51408        testcase( IS_BIG_INT(iOffset) );
51409        rc = sqlite3OsWrite(pWal->pDbFd, zBuf, szPage, iOffset);
51410        if( rc!=SQLITE_OK ) break;
51411      }
51412
51413      /* If work was actually accomplished... */
51414      if( rc==SQLITE_OK ){
51415        if( mxSafeFrame==walIndexHdr(pWal)->mxFrame ){
51416          i64 szDb = pWal->hdr.nPage*(i64)szPage;
51417          testcase( IS_BIG_INT(szDb) );
51418          rc = sqlite3OsTruncate(pWal->pDbFd, szDb);
51419          if( rc==SQLITE_OK && sync_flags ){
51420            rc = sqlite3OsSync(pWal->pDbFd, sync_flags);
51421          }
51422        }
51423        if( rc==SQLITE_OK ){
51424          pInfo->nBackfill = mxSafeFrame;
51425        }
51426      }
51427
51428      /* Release the reader lock held while backfilling */
51429      walUnlockExclusive(pWal, WAL_READ_LOCK(0), 1);
51430    }
51431
51432    if( rc==SQLITE_BUSY ){
51433      /* Reset the return code so as not to report a checkpoint failure
51434      ** just because there are active readers.  */
51435      rc = SQLITE_OK;
51436    }
51437  }
51438
51439  /* If this is an SQLITE_CHECKPOINT_RESTART or TRUNCATE operation, and the
51440  ** entire wal file has been copied into the database file, then block
51441  ** until all readers have finished using the wal file. This ensures that
51442  ** the next process to write to the database restarts the wal file.
51443  */
51444  if( rc==SQLITE_OK && eMode!=SQLITE_CHECKPOINT_PASSIVE ){
51445    assert( pWal->writeLock );
51446    if( pInfo->nBackfill<pWal->hdr.mxFrame ){
51447      rc = SQLITE_BUSY;
51448    }else if( eMode>=SQLITE_CHECKPOINT_RESTART ){
51449      u32 salt1;
51450      sqlite3_randomness(4, &salt1);
51451      assert( pInfo->nBackfill==pWal->hdr.mxFrame );
51452      rc = walBusyLock(pWal, xBusy, pBusyArg, WAL_READ_LOCK(1), WAL_NREADER-1);
51453      if( rc==SQLITE_OK ){
51454        if( eMode==SQLITE_CHECKPOINT_TRUNCATE ){
51455          /* IMPLEMENTATION-OF: R-44699-57140 This mode works the same way as
51456          ** SQLITE_CHECKPOINT_RESTART with the addition that it also
51457          ** truncates the log file to zero bytes just prior to a
51458          ** successful return.
51459          **
51460          ** In theory, it might be safe to do this without updating the
51461          ** wal-index header in shared memory, as all subsequent reader or
51462          ** writer clients should see that the entire log file has been
51463          ** checkpointed and behave accordingly. This seems unsafe though,
51464          ** as it would leave the system in a state where the contents of
51465          ** the wal-index header do not match the contents of the
51466          ** file-system. To avoid this, update the wal-index header to
51467          ** indicate that the log file contains zero valid frames.  */
51468          walRestartHdr(pWal, salt1);
51469          rc = sqlite3OsTruncate(pWal->pWalFd, 0);
51470        }
51471        walUnlockExclusive(pWal, WAL_READ_LOCK(1), WAL_NREADER-1);
51472      }
51473    }
51474  }
51475
51476 walcheckpoint_out:
51477  walIteratorFree(pIter);
51478  return rc;
51479}
51480
51481/*
51482** If the WAL file is currently larger than nMax bytes in size, truncate
51483** it to exactly nMax bytes. If an error occurs while doing so, ignore it.
51484*/
51485static void walLimitSize(Wal *pWal, i64 nMax){
51486  i64 sz;
51487  int rx;
51488  sqlite3BeginBenignMalloc();
51489  rx = sqlite3OsFileSize(pWal->pWalFd, &sz);
51490  if( rx==SQLITE_OK && (sz > nMax ) ){
51491    rx = sqlite3OsTruncate(pWal->pWalFd, nMax);
51492  }
51493  sqlite3EndBenignMalloc();
51494  if( rx ){
51495    sqlite3_log(rx, "cannot limit WAL size: %s", pWal->zWalName);
51496  }
51497}
51498
51499/*
51500** Close a connection to a log file.
51501*/
51502SQLITE_PRIVATE int sqlite3WalClose(
51503  Wal *pWal,                      /* Wal to close */
51504  int sync_flags,                 /* Flags to pass to OsSync() (or 0) */
51505  int nBuf,
51506  u8 *zBuf                        /* Buffer of at least nBuf bytes */
51507){
51508  int rc = SQLITE_OK;
51509  if( pWal ){
51510    int isDelete = 0;             /* True to unlink wal and wal-index files */
51511
51512    /* If an EXCLUSIVE lock can be obtained on the database file (using the
51513    ** ordinary, rollback-mode locking methods, this guarantees that the
51514    ** connection associated with this log file is the only connection to
51515    ** the database. In this case checkpoint the database and unlink both
51516    ** the wal and wal-index files.
51517    **
51518    ** The EXCLUSIVE lock is not released before returning.
51519    */
51520    rc = sqlite3OsLock(pWal->pDbFd, SQLITE_LOCK_EXCLUSIVE);
51521    if( rc==SQLITE_OK ){
51522      if( pWal->exclusiveMode==WAL_NORMAL_MODE ){
51523        pWal->exclusiveMode = WAL_EXCLUSIVE_MODE;
51524      }
51525      rc = sqlite3WalCheckpoint(
51526          pWal, SQLITE_CHECKPOINT_PASSIVE, 0, 0, sync_flags, nBuf, zBuf, 0, 0
51527      );
51528      if( rc==SQLITE_OK ){
51529        int bPersist = -1;
51530        sqlite3OsFileControlHint(
51531            pWal->pDbFd, SQLITE_FCNTL_PERSIST_WAL, &bPersist
51532        );
51533        if( bPersist!=1 ){
51534          /* Try to delete the WAL file if the checkpoint completed and
51535          ** fsyned (rc==SQLITE_OK) and if we are not in persistent-wal
51536          ** mode (!bPersist) */
51537          isDelete = 1;
51538        }else if( pWal->mxWalSize>=0 ){
51539          /* Try to truncate the WAL file to zero bytes if the checkpoint
51540          ** completed and fsynced (rc==SQLITE_OK) and we are in persistent
51541          ** WAL mode (bPersist) and if the PRAGMA journal_size_limit is a
51542          ** non-negative value (pWal->mxWalSize>=0).  Note that we truncate
51543          ** to zero bytes as truncating to the journal_size_limit might
51544          ** leave a corrupt WAL file on disk. */
51545          walLimitSize(pWal, 0);
51546        }
51547      }
51548    }
51549
51550    walIndexClose(pWal, isDelete);
51551    sqlite3OsClose(pWal->pWalFd);
51552    if( isDelete ){
51553      sqlite3BeginBenignMalloc();
51554      sqlite3OsDelete(pWal->pVfs, pWal->zWalName, 0);
51555      sqlite3EndBenignMalloc();
51556    }
51557    WALTRACE(("WAL%p: closed\n", pWal));
51558    sqlite3_free((void *)pWal->apWiData);
51559    sqlite3_free(pWal);
51560  }
51561  return rc;
51562}
51563
51564/*
51565** Try to read the wal-index header.  Return 0 on success and 1 if
51566** there is a problem.
51567**
51568** The wal-index is in shared memory.  Another thread or process might
51569** be writing the header at the same time this procedure is trying to
51570** read it, which might result in inconsistency.  A dirty read is detected
51571** by verifying that both copies of the header are the same and also by
51572** a checksum on the header.
51573**
51574** If and only if the read is consistent and the header is different from
51575** pWal->hdr, then pWal->hdr is updated to the content of the new header
51576** and *pChanged is set to 1.
51577**
51578** If the checksum cannot be verified return non-zero. If the header
51579** is read successfully and the checksum verified, return zero.
51580*/
51581static int walIndexTryHdr(Wal *pWal, int *pChanged){
51582  u32 aCksum[2];                  /* Checksum on the header content */
51583  WalIndexHdr h1, h2;             /* Two copies of the header content */
51584  WalIndexHdr volatile *aHdr;     /* Header in shared memory */
51585
51586  /* The first page of the wal-index must be mapped at this point. */
51587  assert( pWal->nWiData>0 && pWal->apWiData[0] );
51588
51589  /* Read the header. This might happen concurrently with a write to the
51590  ** same area of shared memory on a different CPU in a SMP,
51591  ** meaning it is possible that an inconsistent snapshot is read
51592  ** from the file. If this happens, return non-zero.
51593  **
51594  ** There are two copies of the header at the beginning of the wal-index.
51595  ** When reading, read [0] first then [1].  Writes are in the reverse order.
51596  ** Memory barriers are used to prevent the compiler or the hardware from
51597  ** reordering the reads and writes.
51598  */
51599  aHdr = walIndexHdr(pWal);
51600  memcpy(&h1, (void *)&aHdr[0], sizeof(h1));
51601  walShmBarrier(pWal);
51602  memcpy(&h2, (void *)&aHdr[1], sizeof(h2));
51603
51604  if( memcmp(&h1, &h2, sizeof(h1))!=0 ){
51605    return 1;   /* Dirty read */
51606  }
51607  if( h1.isInit==0 ){
51608    return 1;   /* Malformed header - probably all zeros */
51609  }
51610  walChecksumBytes(1, (u8*)&h1, sizeof(h1)-sizeof(h1.aCksum), 0, aCksum);
51611  if( aCksum[0]!=h1.aCksum[0] || aCksum[1]!=h1.aCksum[1] ){
51612    return 1;   /* Checksum does not match */
51613  }
51614
51615  if( memcmp(&pWal->hdr, &h1, sizeof(WalIndexHdr)) ){
51616    *pChanged = 1;
51617    memcpy(&pWal->hdr, &h1, sizeof(WalIndexHdr));
51618    pWal->szPage = (pWal->hdr.szPage&0xfe00) + ((pWal->hdr.szPage&0x0001)<<16);
51619    testcase( pWal->szPage<=32768 );
51620    testcase( pWal->szPage>=65536 );
51621  }
51622
51623  /* The header was successfully read. Return zero. */
51624  return 0;
51625}
51626
51627/*
51628** Read the wal-index header from the wal-index and into pWal->hdr.
51629** If the wal-header appears to be corrupt, try to reconstruct the
51630** wal-index from the WAL before returning.
51631**
51632** Set *pChanged to 1 if the wal-index header value in pWal->hdr is
51633** changed by this operation.  If pWal->hdr is unchanged, set *pChanged
51634** to 0.
51635**
51636** If the wal-index header is successfully read, return SQLITE_OK.
51637** Otherwise an SQLite error code.
51638*/
51639static int walIndexReadHdr(Wal *pWal, int *pChanged){
51640  int rc;                         /* Return code */
51641  int badHdr;                     /* True if a header read failed */
51642  volatile u32 *page0;            /* Chunk of wal-index containing header */
51643
51644  /* Ensure that page 0 of the wal-index (the page that contains the
51645  ** wal-index header) is mapped. Return early if an error occurs here.
51646  */
51647  assert( pChanged );
51648  rc = walIndexPage(pWal, 0, &page0);
51649  if( rc!=SQLITE_OK ){
51650    return rc;
51651  };
51652  assert( page0 || pWal->writeLock==0 );
51653
51654  /* If the first page of the wal-index has been mapped, try to read the
51655  ** wal-index header immediately, without holding any lock. This usually
51656  ** works, but may fail if the wal-index header is corrupt or currently
51657  ** being modified by another thread or process.
51658  */
51659  badHdr = (page0 ? walIndexTryHdr(pWal, pChanged) : 1);
51660
51661  /* If the first attempt failed, it might have been due to a race
51662  ** with a writer.  So get a WRITE lock and try again.
51663  */
51664  assert( badHdr==0 || pWal->writeLock==0 );
51665  if( badHdr ){
51666    if( pWal->readOnly & WAL_SHM_RDONLY ){
51667      if( SQLITE_OK==(rc = walLockShared(pWal, WAL_WRITE_LOCK)) ){
51668        walUnlockShared(pWal, WAL_WRITE_LOCK);
51669        rc = SQLITE_READONLY_RECOVERY;
51670      }
51671    }else if( SQLITE_OK==(rc = walLockExclusive(pWal, WAL_WRITE_LOCK, 1, 1)) ){
51672      pWal->writeLock = 1;
51673      if( SQLITE_OK==(rc = walIndexPage(pWal, 0, &page0)) ){
51674        badHdr = walIndexTryHdr(pWal, pChanged);
51675        if( badHdr ){
51676          /* If the wal-index header is still malformed even while holding
51677          ** a WRITE lock, it can only mean that the header is corrupted and
51678          ** needs to be reconstructed.  So run recovery to do exactly that.
51679          */
51680          rc = walIndexRecover(pWal);
51681          *pChanged = 1;
51682        }
51683      }
51684      pWal->writeLock = 0;
51685      walUnlockExclusive(pWal, WAL_WRITE_LOCK, 1);
51686    }
51687  }
51688
51689  /* If the header is read successfully, check the version number to make
51690  ** sure the wal-index was not constructed with some future format that
51691  ** this version of SQLite cannot understand.
51692  */
51693  if( badHdr==0 && pWal->hdr.iVersion!=WALINDEX_MAX_VERSION ){
51694    rc = SQLITE_CANTOPEN_BKPT;
51695  }
51696
51697  return rc;
51698}
51699
51700/*
51701** This is the value that walTryBeginRead returns when it needs to
51702** be retried.
51703*/
51704#define WAL_RETRY  (-1)
51705
51706/*
51707** Attempt to start a read transaction.  This might fail due to a race or
51708** other transient condition.  When that happens, it returns WAL_RETRY to
51709** indicate to the caller that it is safe to retry immediately.
51710**
51711** On success return SQLITE_OK.  On a permanent failure (such an
51712** I/O error or an SQLITE_BUSY because another process is running
51713** recovery) return a positive error code.
51714**
51715** The useWal parameter is true to force the use of the WAL and disable
51716** the case where the WAL is bypassed because it has been completely
51717** checkpointed.  If useWal==0 then this routine calls walIndexReadHdr()
51718** to make a copy of the wal-index header into pWal->hdr.  If the
51719** wal-index header has changed, *pChanged is set to 1 (as an indication
51720** to the caller that the local paget cache is obsolete and needs to be
51721** flushed.)  When useWal==1, the wal-index header is assumed to already
51722** be loaded and the pChanged parameter is unused.
51723**
51724** The caller must set the cnt parameter to the number of prior calls to
51725** this routine during the current read attempt that returned WAL_RETRY.
51726** This routine will start taking more aggressive measures to clear the
51727** race conditions after multiple WAL_RETRY returns, and after an excessive
51728** number of errors will ultimately return SQLITE_PROTOCOL.  The
51729** SQLITE_PROTOCOL return indicates that some other process has gone rogue
51730** and is not honoring the locking protocol.  There is a vanishingly small
51731** chance that SQLITE_PROTOCOL could be returned because of a run of really
51732** bad luck when there is lots of contention for the wal-index, but that
51733** possibility is so small that it can be safely neglected, we believe.
51734**
51735** On success, this routine obtains a read lock on
51736** WAL_READ_LOCK(pWal->readLock).  The pWal->readLock integer is
51737** in the range 0 <= pWal->readLock < WAL_NREADER.  If pWal->readLock==(-1)
51738** that means the Wal does not hold any read lock.  The reader must not
51739** access any database page that is modified by a WAL frame up to and
51740** including frame number aReadMark[pWal->readLock].  The reader will
51741** use WAL frames up to and including pWal->hdr.mxFrame if pWal->readLock>0
51742** Or if pWal->readLock==0, then the reader will ignore the WAL
51743** completely and get all content directly from the database file.
51744** If the useWal parameter is 1 then the WAL will never be ignored and
51745** this routine will always set pWal->readLock>0 on success.
51746** When the read transaction is completed, the caller must release the
51747** lock on WAL_READ_LOCK(pWal->readLock) and set pWal->readLock to -1.
51748**
51749** This routine uses the nBackfill and aReadMark[] fields of the header
51750** to select a particular WAL_READ_LOCK() that strives to let the
51751** checkpoint process do as much work as possible.  This routine might
51752** update values of the aReadMark[] array in the header, but if it does
51753** so it takes care to hold an exclusive lock on the corresponding
51754** WAL_READ_LOCK() while changing values.
51755*/
51756static int walTryBeginRead(Wal *pWal, int *pChanged, int useWal, int cnt){
51757  volatile WalCkptInfo *pInfo;    /* Checkpoint information in wal-index */
51758  u32 mxReadMark;                 /* Largest aReadMark[] value */
51759  int mxI;                        /* Index of largest aReadMark[] value */
51760  int i;                          /* Loop counter */
51761  int rc = SQLITE_OK;             /* Return code  */
51762
51763  assert( pWal->readLock<0 );     /* Not currently locked */
51764
51765  /* Take steps to avoid spinning forever if there is a protocol error.
51766  **
51767  ** Circumstances that cause a RETRY should only last for the briefest
51768  ** instances of time.  No I/O or other system calls are done while the
51769  ** locks are held, so the locks should not be held for very long. But
51770  ** if we are unlucky, another process that is holding a lock might get
51771  ** paged out or take a page-fault that is time-consuming to resolve,
51772  ** during the few nanoseconds that it is holding the lock.  In that case,
51773  ** it might take longer than normal for the lock to free.
51774  **
51775  ** After 5 RETRYs, we begin calling sqlite3OsSleep().  The first few
51776  ** calls to sqlite3OsSleep() have a delay of 1 microsecond.  Really this
51777  ** is more of a scheduler yield than an actual delay.  But on the 10th
51778  ** an subsequent retries, the delays start becoming longer and longer,
51779  ** so that on the 100th (and last) RETRY we delay for 323 milliseconds.
51780  ** The total delay time before giving up is less than 10 seconds.
51781  */
51782  if( cnt>5 ){
51783    int nDelay = 1;                      /* Pause time in microseconds */
51784    if( cnt>100 ){
51785      VVA_ONLY( pWal->lockError = 1; )
51786      return SQLITE_PROTOCOL;
51787    }
51788    if( cnt>=10 ) nDelay = (cnt-9)*(cnt-9)*39;
51789    sqlite3OsSleep(pWal->pVfs, nDelay);
51790  }
51791
51792  if( !useWal ){
51793    rc = walIndexReadHdr(pWal, pChanged);
51794    if( rc==SQLITE_BUSY ){
51795      /* If there is not a recovery running in another thread or process
51796      ** then convert BUSY errors to WAL_RETRY.  If recovery is known to
51797      ** be running, convert BUSY to BUSY_RECOVERY.  There is a race here
51798      ** which might cause WAL_RETRY to be returned even if BUSY_RECOVERY
51799      ** would be technically correct.  But the race is benign since with
51800      ** WAL_RETRY this routine will be called again and will probably be
51801      ** right on the second iteration.
51802      */
51803      if( pWal->apWiData[0]==0 ){
51804        /* This branch is taken when the xShmMap() method returns SQLITE_BUSY.
51805        ** We assume this is a transient condition, so return WAL_RETRY. The
51806        ** xShmMap() implementation used by the default unix and win32 VFS
51807        ** modules may return SQLITE_BUSY due to a race condition in the
51808        ** code that determines whether or not the shared-memory region
51809        ** must be zeroed before the requested page is returned.
51810        */
51811        rc = WAL_RETRY;
51812      }else if( SQLITE_OK==(rc = walLockShared(pWal, WAL_RECOVER_LOCK)) ){
51813        walUnlockShared(pWal, WAL_RECOVER_LOCK);
51814        rc = WAL_RETRY;
51815      }else if( rc==SQLITE_BUSY ){
51816        rc = SQLITE_BUSY_RECOVERY;
51817      }
51818    }
51819    if( rc!=SQLITE_OK ){
51820      return rc;
51821    }
51822  }
51823
51824  pInfo = walCkptInfo(pWal);
51825  if( !useWal && pInfo->nBackfill==pWal->hdr.mxFrame ){
51826    /* The WAL has been completely backfilled (or it is empty).
51827    ** and can be safely ignored.
51828    */
51829    rc = walLockShared(pWal, WAL_READ_LOCK(0));
51830    walShmBarrier(pWal);
51831    if( rc==SQLITE_OK ){
51832      if( memcmp((void *)walIndexHdr(pWal), &pWal->hdr, sizeof(WalIndexHdr)) ){
51833        /* It is not safe to allow the reader to continue here if frames
51834        ** may have been appended to the log before READ_LOCK(0) was obtained.
51835        ** When holding READ_LOCK(0), the reader ignores the entire log file,
51836        ** which implies that the database file contains a trustworthy
51837        ** snapshot. Since holding READ_LOCK(0) prevents a checkpoint from
51838        ** happening, this is usually correct.
51839        **
51840        ** However, if frames have been appended to the log (or if the log
51841        ** is wrapped and written for that matter) before the READ_LOCK(0)
51842        ** is obtained, that is not necessarily true. A checkpointer may
51843        ** have started to backfill the appended frames but crashed before
51844        ** it finished. Leaving a corrupt image in the database file.
51845        */
51846        walUnlockShared(pWal, WAL_READ_LOCK(0));
51847        return WAL_RETRY;
51848      }
51849      pWal->readLock = 0;
51850      return SQLITE_OK;
51851    }else if( rc!=SQLITE_BUSY ){
51852      return rc;
51853    }
51854  }
51855
51856  /* If we get this far, it means that the reader will want to use
51857  ** the WAL to get at content from recent commits.  The job now is
51858  ** to select one of the aReadMark[] entries that is closest to
51859  ** but not exceeding pWal->hdr.mxFrame and lock that entry.
51860  */
51861  mxReadMark = 0;
51862  mxI = 0;
51863  for(i=1; i<WAL_NREADER; i++){
51864    u32 thisMark = pInfo->aReadMark[i];
51865    if( mxReadMark<=thisMark && thisMark<=pWal->hdr.mxFrame ){
51866      assert( thisMark!=READMARK_NOT_USED );
51867      mxReadMark = thisMark;
51868      mxI = i;
51869    }
51870  }
51871  /* There was once an "if" here. The extra "{" is to preserve indentation. */
51872  {
51873    if( (pWal->readOnly & WAL_SHM_RDONLY)==0
51874     && (mxReadMark<pWal->hdr.mxFrame || mxI==0)
51875    ){
51876      for(i=1; i<WAL_NREADER; i++){
51877        rc = walLockExclusive(pWal, WAL_READ_LOCK(i), 1, 0);
51878        if( rc==SQLITE_OK ){
51879          mxReadMark = pInfo->aReadMark[i] = pWal->hdr.mxFrame;
51880          mxI = i;
51881          walUnlockExclusive(pWal, WAL_READ_LOCK(i), 1);
51882          break;
51883        }else if( rc!=SQLITE_BUSY ){
51884          return rc;
51885        }
51886      }
51887    }
51888    if( mxI==0 ){
51889      assert( rc==SQLITE_BUSY || (pWal->readOnly & WAL_SHM_RDONLY)!=0 );
51890      return rc==SQLITE_BUSY ? WAL_RETRY : SQLITE_READONLY_CANTLOCK;
51891    }
51892
51893    rc = walLockShared(pWal, WAL_READ_LOCK(mxI));
51894    if( rc ){
51895      return rc==SQLITE_BUSY ? WAL_RETRY : rc;
51896    }
51897    /* Now that the read-lock has been obtained, check that neither the
51898    ** value in the aReadMark[] array or the contents of the wal-index
51899    ** header have changed.
51900    **
51901    ** It is necessary to check that the wal-index header did not change
51902    ** between the time it was read and when the shared-lock was obtained
51903    ** on WAL_READ_LOCK(mxI) was obtained to account for the possibility
51904    ** that the log file may have been wrapped by a writer, or that frames
51905    ** that occur later in the log than pWal->hdr.mxFrame may have been
51906    ** copied into the database by a checkpointer. If either of these things
51907    ** happened, then reading the database with the current value of
51908    ** pWal->hdr.mxFrame risks reading a corrupted snapshot. So, retry
51909    ** instead.
51910    **
51911    ** This does not guarantee that the copy of the wal-index header is up to
51912    ** date before proceeding. That would not be possible without somehow
51913    ** blocking writers. It only guarantees that a dangerous checkpoint or
51914    ** log-wrap (either of which would require an exclusive lock on
51915    ** WAL_READ_LOCK(mxI)) has not occurred since the snapshot was valid.
51916    */
51917    walShmBarrier(pWal);
51918    if( pInfo->aReadMark[mxI]!=mxReadMark
51919     || memcmp((void *)walIndexHdr(pWal), &pWal->hdr, sizeof(WalIndexHdr))
51920    ){
51921      walUnlockShared(pWal, WAL_READ_LOCK(mxI));
51922      return WAL_RETRY;
51923    }else{
51924      assert( mxReadMark<=pWal->hdr.mxFrame );
51925      pWal->readLock = (i16)mxI;
51926    }
51927  }
51928  return rc;
51929}
51930
51931/*
51932** Begin a read transaction on the database.
51933**
51934** This routine used to be called sqlite3OpenSnapshot() and with good reason:
51935** it takes a snapshot of the state of the WAL and wal-index for the current
51936** instant in time.  The current thread will continue to use this snapshot.
51937** Other threads might append new content to the WAL and wal-index but
51938** that extra content is ignored by the current thread.
51939**
51940** If the database contents have changes since the previous read
51941** transaction, then *pChanged is set to 1 before returning.  The
51942** Pager layer will use this to know that is cache is stale and
51943** needs to be flushed.
51944*/
51945SQLITE_PRIVATE int sqlite3WalBeginReadTransaction(Wal *pWal, int *pChanged){
51946  int rc;                         /* Return code */
51947  int cnt = 0;                    /* Number of TryBeginRead attempts */
51948
51949  do{
51950    rc = walTryBeginRead(pWal, pChanged, 0, ++cnt);
51951  }while( rc==WAL_RETRY );
51952  testcase( (rc&0xff)==SQLITE_BUSY );
51953  testcase( (rc&0xff)==SQLITE_IOERR );
51954  testcase( rc==SQLITE_PROTOCOL );
51955  testcase( rc==SQLITE_OK );
51956  return rc;
51957}
51958
51959/*
51960** Finish with a read transaction.  All this does is release the
51961** read-lock.
51962*/
51963SQLITE_PRIVATE void sqlite3WalEndReadTransaction(Wal *pWal){
51964  sqlite3WalEndWriteTransaction(pWal);
51965  if( pWal->readLock>=0 ){
51966    walUnlockShared(pWal, WAL_READ_LOCK(pWal->readLock));
51967    pWal->readLock = -1;
51968  }
51969}
51970
51971/*
51972** Search the wal file for page pgno. If found, set *piRead to the frame that
51973** contains the page. Otherwise, if pgno is not in the wal file, set *piRead
51974** to zero.
51975**
51976** Return SQLITE_OK if successful, or an error code if an error occurs. If an
51977** error does occur, the final value of *piRead is undefined.
51978*/
51979SQLITE_PRIVATE int sqlite3WalFindFrame(
51980  Wal *pWal,                      /* WAL handle */
51981  Pgno pgno,                      /* Database page number to read data for */
51982  u32 *piRead                     /* OUT: Frame number (or zero) */
51983){
51984  u32 iRead = 0;                  /* If !=0, WAL frame to return data from */
51985  u32 iLast = pWal->hdr.mxFrame;  /* Last page in WAL for this reader */
51986  int iHash;                      /* Used to loop through N hash tables */
51987
51988  /* This routine is only be called from within a read transaction. */
51989  assert( pWal->readLock>=0 || pWal->lockError );
51990
51991  /* If the "last page" field of the wal-index header snapshot is 0, then
51992  ** no data will be read from the wal under any circumstances. Return early
51993  ** in this case as an optimization.  Likewise, if pWal->readLock==0,
51994  ** then the WAL is ignored by the reader so return early, as if the
51995  ** WAL were empty.
51996  */
51997  if( iLast==0 || pWal->readLock==0 ){
51998    *piRead = 0;
51999    return SQLITE_OK;
52000  }
52001
52002  /* Search the hash table or tables for an entry matching page number
52003  ** pgno. Each iteration of the following for() loop searches one
52004  ** hash table (each hash table indexes up to HASHTABLE_NPAGE frames).
52005  **
52006  ** This code might run concurrently to the code in walIndexAppend()
52007  ** that adds entries to the wal-index (and possibly to this hash
52008  ** table). This means the value just read from the hash
52009  ** slot (aHash[iKey]) may have been added before or after the
52010  ** current read transaction was opened. Values added after the
52011  ** read transaction was opened may have been written incorrectly -
52012  ** i.e. these slots may contain garbage data. However, we assume
52013  ** that any slots written before the current read transaction was
52014  ** opened remain unmodified.
52015  **
52016  ** For the reasons above, the if(...) condition featured in the inner
52017  ** loop of the following block is more stringent that would be required
52018  ** if we had exclusive access to the hash-table:
52019  **
52020  **   (aPgno[iFrame]==pgno):
52021  **     This condition filters out normal hash-table collisions.
52022  **
52023  **   (iFrame<=iLast):
52024  **     This condition filters out entries that were added to the hash
52025  **     table after the current read-transaction had started.
52026  */
52027  for(iHash=walFramePage(iLast); iHash>=0 && iRead==0; iHash--){
52028    volatile ht_slot *aHash;      /* Pointer to hash table */
52029    volatile u32 *aPgno;          /* Pointer to array of page numbers */
52030    u32 iZero;                    /* Frame number corresponding to aPgno[0] */
52031    int iKey;                     /* Hash slot index */
52032    int nCollide;                 /* Number of hash collisions remaining */
52033    int rc;                       /* Error code */
52034
52035    rc = walHashGet(pWal, iHash, &aHash, &aPgno, &iZero);
52036    if( rc!=SQLITE_OK ){
52037      return rc;
52038    }
52039    nCollide = HASHTABLE_NSLOT;
52040    for(iKey=walHash(pgno); aHash[iKey]; iKey=walNextHash(iKey)){
52041      u32 iFrame = aHash[iKey] + iZero;
52042      if( iFrame<=iLast && aPgno[aHash[iKey]]==pgno ){
52043        assert( iFrame>iRead || CORRUPT_DB );
52044        iRead = iFrame;
52045      }
52046      if( (nCollide--)==0 ){
52047        return SQLITE_CORRUPT_BKPT;
52048      }
52049    }
52050  }
52051
52052#ifdef SQLITE_ENABLE_EXPENSIVE_ASSERT
52053  /* If expensive assert() statements are available, do a linear search
52054  ** of the wal-index file content. Make sure the results agree with the
52055  ** result obtained using the hash indexes above.  */
52056  {
52057    u32 iRead2 = 0;
52058    u32 iTest;
52059    for(iTest=iLast; iTest>0; iTest--){
52060      if( walFramePgno(pWal, iTest)==pgno ){
52061        iRead2 = iTest;
52062        break;
52063      }
52064    }
52065    assert( iRead==iRead2 );
52066  }
52067#endif
52068
52069  *piRead = iRead;
52070  return SQLITE_OK;
52071}
52072
52073/*
52074** Read the contents of frame iRead from the wal file into buffer pOut
52075** (which is nOut bytes in size). Return SQLITE_OK if successful, or an
52076** error code otherwise.
52077*/
52078SQLITE_PRIVATE int sqlite3WalReadFrame(
52079  Wal *pWal,                      /* WAL handle */
52080  u32 iRead,                      /* Frame to read */
52081  int nOut,                       /* Size of buffer pOut in bytes */
52082  u8 *pOut                        /* Buffer to write page data to */
52083){
52084  int sz;
52085  i64 iOffset;
52086  sz = pWal->hdr.szPage;
52087  sz = (sz&0xfe00) + ((sz&0x0001)<<16);
52088  testcase( sz<=32768 );
52089  testcase( sz>=65536 );
52090  iOffset = walFrameOffset(iRead, sz) + WAL_FRAME_HDRSIZE;
52091  /* testcase( IS_BIG_INT(iOffset) ); // requires a 4GiB WAL */
52092  return sqlite3OsRead(pWal->pWalFd, pOut, (nOut>sz ? sz : nOut), iOffset);
52093}
52094
52095/*
52096** Return the size of the database in pages (or zero, if unknown).
52097*/
52098SQLITE_PRIVATE Pgno sqlite3WalDbsize(Wal *pWal){
52099  if( pWal && ALWAYS(pWal->readLock>=0) ){
52100    return pWal->hdr.nPage;
52101  }
52102  return 0;
52103}
52104
52105
52106/*
52107** This function starts a write transaction on the WAL.
52108**
52109** A read transaction must have already been started by a prior call
52110** to sqlite3WalBeginReadTransaction().
52111**
52112** If another thread or process has written into the database since
52113** the read transaction was started, then it is not possible for this
52114** thread to write as doing so would cause a fork.  So this routine
52115** returns SQLITE_BUSY in that case and no write transaction is started.
52116**
52117** There can only be a single writer active at a time.
52118*/
52119SQLITE_PRIVATE int sqlite3WalBeginWriteTransaction(Wal *pWal){
52120  int rc;
52121
52122  /* Cannot start a write transaction without first holding a read
52123  ** transaction. */
52124  assert( pWal->readLock>=0 );
52125
52126  if( pWal->readOnly ){
52127    return SQLITE_READONLY;
52128  }
52129
52130  /* Only one writer allowed at a time.  Get the write lock.  Return
52131  ** SQLITE_BUSY if unable.
52132  */
52133  rc = walLockExclusive(pWal, WAL_WRITE_LOCK, 1, 0);
52134  if( rc ){
52135    return rc;
52136  }
52137  pWal->writeLock = 1;
52138
52139  /* If another connection has written to the database file since the
52140  ** time the read transaction on this connection was started, then
52141  ** the write is disallowed.
52142  */
52143  if( memcmp(&pWal->hdr, (void *)walIndexHdr(pWal), sizeof(WalIndexHdr))!=0 ){
52144    walUnlockExclusive(pWal, WAL_WRITE_LOCK, 1);
52145    pWal->writeLock = 0;
52146    rc = SQLITE_BUSY_SNAPSHOT;
52147  }
52148
52149  return rc;
52150}
52151
52152/*
52153** End a write transaction.  The commit has already been done.  This
52154** routine merely releases the lock.
52155*/
52156SQLITE_PRIVATE int sqlite3WalEndWriteTransaction(Wal *pWal){
52157  if( pWal->writeLock ){
52158    walUnlockExclusive(pWal, WAL_WRITE_LOCK, 1);
52159    pWal->writeLock = 0;
52160    pWal->truncateOnCommit = 0;
52161  }
52162  return SQLITE_OK;
52163}
52164
52165/*
52166** If any data has been written (but not committed) to the log file, this
52167** function moves the write-pointer back to the start of the transaction.
52168**
52169** Additionally, the callback function is invoked for each frame written
52170** to the WAL since the start of the transaction. If the callback returns
52171** other than SQLITE_OK, it is not invoked again and the error code is
52172** returned to the caller.
52173**
52174** Otherwise, if the callback function does not return an error, this
52175** function returns SQLITE_OK.
52176*/
52177SQLITE_PRIVATE int sqlite3WalUndo(Wal *pWal, int (*xUndo)(void *, Pgno), void *pUndoCtx){
52178  int rc = SQLITE_OK;
52179  if( ALWAYS(pWal->writeLock) ){
52180    Pgno iMax = pWal->hdr.mxFrame;
52181    Pgno iFrame;
52182
52183    /* Restore the clients cache of the wal-index header to the state it
52184    ** was in before the client began writing to the database.
52185    */
52186    memcpy(&pWal->hdr, (void *)walIndexHdr(pWal), sizeof(WalIndexHdr));
52187
52188    for(iFrame=pWal->hdr.mxFrame+1;
52189        ALWAYS(rc==SQLITE_OK) && iFrame<=iMax;
52190        iFrame++
52191    ){
52192      /* This call cannot fail. Unless the page for which the page number
52193      ** is passed as the second argument is (a) in the cache and
52194      ** (b) has an outstanding reference, then xUndo is either a no-op
52195      ** (if (a) is false) or simply expels the page from the cache (if (b)
52196      ** is false).
52197      **
52198      ** If the upper layer is doing a rollback, it is guaranteed that there
52199      ** are no outstanding references to any page other than page 1. And
52200      ** page 1 is never written to the log until the transaction is
52201      ** committed. As a result, the call to xUndo may not fail.
52202      */
52203      assert( walFramePgno(pWal, iFrame)!=1 );
52204      rc = xUndo(pUndoCtx, walFramePgno(pWal, iFrame));
52205    }
52206    if( iMax!=pWal->hdr.mxFrame ) walCleanupHash(pWal);
52207  }
52208  return rc;
52209}
52210
52211/*
52212** Argument aWalData must point to an array of WAL_SAVEPOINT_NDATA u32
52213** values. This function populates the array with values required to
52214** "rollback" the write position of the WAL handle back to the current
52215** point in the event of a savepoint rollback (via WalSavepointUndo()).
52216*/
52217SQLITE_PRIVATE void sqlite3WalSavepoint(Wal *pWal, u32 *aWalData){
52218  assert( pWal->writeLock );
52219  aWalData[0] = pWal->hdr.mxFrame;
52220  aWalData[1] = pWal->hdr.aFrameCksum[0];
52221  aWalData[2] = pWal->hdr.aFrameCksum[1];
52222  aWalData[3] = pWal->nCkpt;
52223}
52224
52225/*
52226** Move the write position of the WAL back to the point identified by
52227** the values in the aWalData[] array. aWalData must point to an array
52228** of WAL_SAVEPOINT_NDATA u32 values that has been previously populated
52229** by a call to WalSavepoint().
52230*/
52231SQLITE_PRIVATE int sqlite3WalSavepointUndo(Wal *pWal, u32 *aWalData){
52232  int rc = SQLITE_OK;
52233
52234  assert( pWal->writeLock );
52235  assert( aWalData[3]!=pWal->nCkpt || aWalData[0]<=pWal->hdr.mxFrame );
52236
52237  if( aWalData[3]!=pWal->nCkpt ){
52238    /* This savepoint was opened immediately after the write-transaction
52239    ** was started. Right after that, the writer decided to wrap around
52240    ** to the start of the log. Update the savepoint values to match.
52241    */
52242    aWalData[0] = 0;
52243    aWalData[3] = pWal->nCkpt;
52244  }
52245
52246  if( aWalData[0]<pWal->hdr.mxFrame ){
52247    pWal->hdr.mxFrame = aWalData[0];
52248    pWal->hdr.aFrameCksum[0] = aWalData[1];
52249    pWal->hdr.aFrameCksum[1] = aWalData[2];
52250    walCleanupHash(pWal);
52251  }
52252
52253  return rc;
52254}
52255
52256/*
52257** This function is called just before writing a set of frames to the log
52258** file (see sqlite3WalFrames()). It checks to see if, instead of appending
52259** to the current log file, it is possible to overwrite the start of the
52260** existing log file with the new frames (i.e. "reset" the log). If so,
52261** it sets pWal->hdr.mxFrame to 0. Otherwise, pWal->hdr.mxFrame is left
52262** unchanged.
52263**
52264** SQLITE_OK is returned if no error is encountered (regardless of whether
52265** or not pWal->hdr.mxFrame is modified). An SQLite error code is returned
52266** if an error occurs.
52267*/
52268static int walRestartLog(Wal *pWal){
52269  int rc = SQLITE_OK;
52270  int cnt;
52271
52272  if( pWal->readLock==0 ){
52273    volatile WalCkptInfo *pInfo = walCkptInfo(pWal);
52274    assert( pInfo->nBackfill==pWal->hdr.mxFrame );
52275    if( pInfo->nBackfill>0 ){
52276      u32 salt1;
52277      sqlite3_randomness(4, &salt1);
52278      rc = walLockExclusive(pWal, WAL_READ_LOCK(1), WAL_NREADER-1, 0);
52279      if( rc==SQLITE_OK ){
52280        /* If all readers are using WAL_READ_LOCK(0) (in other words if no
52281        ** readers are currently using the WAL), then the transactions
52282        ** frames will overwrite the start of the existing log. Update the
52283        ** wal-index header to reflect this.
52284        **
52285        ** In theory it would be Ok to update the cache of the header only
52286        ** at this point. But updating the actual wal-index header is also
52287        ** safe and means there is no special case for sqlite3WalUndo()
52288        ** to handle if this transaction is rolled back.  */
52289        walRestartHdr(pWal, salt1);
52290        walUnlockExclusive(pWal, WAL_READ_LOCK(1), WAL_NREADER-1);
52291      }else if( rc!=SQLITE_BUSY ){
52292        return rc;
52293      }
52294    }
52295    walUnlockShared(pWal, WAL_READ_LOCK(0));
52296    pWal->readLock = -1;
52297    cnt = 0;
52298    do{
52299      int notUsed;
52300      rc = walTryBeginRead(pWal, &notUsed, 1, ++cnt);
52301    }while( rc==WAL_RETRY );
52302    assert( (rc&0xff)!=SQLITE_BUSY ); /* BUSY not possible when useWal==1 */
52303    testcase( (rc&0xff)==SQLITE_IOERR );
52304    testcase( rc==SQLITE_PROTOCOL );
52305    testcase( rc==SQLITE_OK );
52306  }
52307  return rc;
52308}
52309
52310/*
52311** Information about the current state of the WAL file and where
52312** the next fsync should occur - passed from sqlite3WalFrames() into
52313** walWriteToLog().
52314*/
52315typedef struct WalWriter {
52316  Wal *pWal;                   /* The complete WAL information */
52317  sqlite3_file *pFd;           /* The WAL file to which we write */
52318  sqlite3_int64 iSyncPoint;    /* Fsync at this offset */
52319  int syncFlags;               /* Flags for the fsync */
52320  int szPage;                  /* Size of one page */
52321} WalWriter;
52322
52323/*
52324** Write iAmt bytes of content into the WAL file beginning at iOffset.
52325** Do a sync when crossing the p->iSyncPoint boundary.
52326**
52327** In other words, if iSyncPoint is in between iOffset and iOffset+iAmt,
52328** first write the part before iSyncPoint, then sync, then write the
52329** rest.
52330*/
52331static int walWriteToLog(
52332  WalWriter *p,              /* WAL to write to */
52333  void *pContent,            /* Content to be written */
52334  int iAmt,                  /* Number of bytes to write */
52335  sqlite3_int64 iOffset      /* Start writing at this offset */
52336){
52337  int rc;
52338  if( iOffset<p->iSyncPoint && iOffset+iAmt>=p->iSyncPoint ){
52339    int iFirstAmt = (int)(p->iSyncPoint - iOffset);
52340    rc = sqlite3OsWrite(p->pFd, pContent, iFirstAmt, iOffset);
52341    if( rc ) return rc;
52342    iOffset += iFirstAmt;
52343    iAmt -= iFirstAmt;
52344    pContent = (void*)(iFirstAmt + (char*)pContent);
52345    assert( p->syncFlags & (SQLITE_SYNC_NORMAL|SQLITE_SYNC_FULL) );
52346    rc = sqlite3OsSync(p->pFd, p->syncFlags & SQLITE_SYNC_MASK);
52347    if( iAmt==0 || rc ) return rc;
52348  }
52349  rc = sqlite3OsWrite(p->pFd, pContent, iAmt, iOffset);
52350  return rc;
52351}
52352
52353/*
52354** Write out a single frame of the WAL
52355*/
52356static int walWriteOneFrame(
52357  WalWriter *p,               /* Where to write the frame */
52358  PgHdr *pPage,               /* The page of the frame to be written */
52359  int nTruncate,              /* The commit flag.  Usually 0.  >0 for commit */
52360  sqlite3_int64 iOffset       /* Byte offset at which to write */
52361){
52362  int rc;                         /* Result code from subfunctions */
52363  void *pData;                    /* Data actually written */
52364  u8 aFrame[WAL_FRAME_HDRSIZE];   /* Buffer to assemble frame-header in */
52365#if defined(SQLITE_HAS_CODEC)
52366  if( (pData = sqlite3PagerCodec(pPage))==0 ) return SQLITE_NOMEM;
52367#else
52368  pData = pPage->pData;
52369#endif
52370  walEncodeFrame(p->pWal, pPage->pgno, nTruncate, pData, aFrame);
52371  rc = walWriteToLog(p, aFrame, sizeof(aFrame), iOffset);
52372  if( rc ) return rc;
52373  /* Write the page data */
52374  rc = walWriteToLog(p, pData, p->szPage, iOffset+sizeof(aFrame));
52375  return rc;
52376}
52377
52378/*
52379** Write a set of frames to the log. The caller must hold the write-lock
52380** on the log file (obtained using sqlite3WalBeginWriteTransaction()).
52381*/
52382SQLITE_PRIVATE int sqlite3WalFrames(
52383  Wal *pWal,                      /* Wal handle to write to */
52384  int szPage,                     /* Database page-size in bytes */
52385  PgHdr *pList,                   /* List of dirty pages to write */
52386  Pgno nTruncate,                 /* Database size after this commit */
52387  int isCommit,                   /* True if this is a commit */
52388  int sync_flags                  /* Flags to pass to OsSync() (or 0) */
52389){
52390  int rc;                         /* Used to catch return codes */
52391  u32 iFrame;                     /* Next frame address */
52392  PgHdr *p;                       /* Iterator to run through pList with. */
52393  PgHdr *pLast = 0;               /* Last frame in list */
52394  int nExtra = 0;                 /* Number of extra copies of last page */
52395  int szFrame;                    /* The size of a single frame */
52396  i64 iOffset;                    /* Next byte to write in WAL file */
52397  WalWriter w;                    /* The writer */
52398
52399  assert( pList );
52400  assert( pWal->writeLock );
52401
52402  /* If this frame set completes a transaction, then nTruncate>0.  If
52403  ** nTruncate==0 then this frame set does not complete the transaction. */
52404  assert( (isCommit!=0)==(nTruncate!=0) );
52405
52406#if defined(SQLITE_TEST) && defined(SQLITE_DEBUG)
52407  { int cnt; for(cnt=0, p=pList; p; p=p->pDirty, cnt++){}
52408    WALTRACE(("WAL%p: frame write begin. %d frames. mxFrame=%d. %s\n",
52409              pWal, cnt, pWal->hdr.mxFrame, isCommit ? "Commit" : "Spill"));
52410  }
52411#endif
52412
52413  /* See if it is possible to write these frames into the start of the
52414  ** log file, instead of appending to it at pWal->hdr.mxFrame.
52415  */
52416  if( SQLITE_OK!=(rc = walRestartLog(pWal)) ){
52417    return rc;
52418  }
52419
52420  /* If this is the first frame written into the log, write the WAL
52421  ** header to the start of the WAL file. See comments at the top of
52422  ** this source file for a description of the WAL header format.
52423  */
52424  iFrame = pWal->hdr.mxFrame;
52425  if( iFrame==0 ){
52426    u8 aWalHdr[WAL_HDRSIZE];      /* Buffer to assemble wal-header in */
52427    u32 aCksum[2];                /* Checksum for wal-header */
52428
52429    sqlite3Put4byte(&aWalHdr[0], (WAL_MAGIC | SQLITE_BIGENDIAN));
52430    sqlite3Put4byte(&aWalHdr[4], WAL_MAX_VERSION);
52431    sqlite3Put4byte(&aWalHdr[8], szPage);
52432    sqlite3Put4byte(&aWalHdr[12], pWal->nCkpt);
52433    if( pWal->nCkpt==0 ) sqlite3_randomness(8, pWal->hdr.aSalt);
52434    memcpy(&aWalHdr[16], pWal->hdr.aSalt, 8);
52435    walChecksumBytes(1, aWalHdr, WAL_HDRSIZE-2*4, 0, aCksum);
52436    sqlite3Put4byte(&aWalHdr[24], aCksum[0]);
52437    sqlite3Put4byte(&aWalHdr[28], aCksum[1]);
52438
52439    pWal->szPage = szPage;
52440    pWal->hdr.bigEndCksum = SQLITE_BIGENDIAN;
52441    pWal->hdr.aFrameCksum[0] = aCksum[0];
52442    pWal->hdr.aFrameCksum[1] = aCksum[1];
52443    pWal->truncateOnCommit = 1;
52444
52445    rc = sqlite3OsWrite(pWal->pWalFd, aWalHdr, sizeof(aWalHdr), 0);
52446    WALTRACE(("WAL%p: wal-header write %s\n", pWal, rc ? "failed" : "ok"));
52447    if( rc!=SQLITE_OK ){
52448      return rc;
52449    }
52450
52451    /* Sync the header (unless SQLITE_IOCAP_SEQUENTIAL is true or unless
52452    ** all syncing is turned off by PRAGMA synchronous=OFF).  Otherwise
52453    ** an out-of-order write following a WAL restart could result in
52454    ** database corruption.  See the ticket:
52455    **
52456    **     http://localhost:591/sqlite/info/ff5be73dee
52457    */
52458    if( pWal->syncHeader && sync_flags ){
52459      rc = sqlite3OsSync(pWal->pWalFd, sync_flags & SQLITE_SYNC_MASK);
52460      if( rc ) return rc;
52461    }
52462  }
52463  assert( (int)pWal->szPage==szPage );
52464
52465  /* Setup information needed to write frames into the WAL */
52466  w.pWal = pWal;
52467  w.pFd = pWal->pWalFd;
52468  w.iSyncPoint = 0;
52469  w.syncFlags = sync_flags;
52470  w.szPage = szPage;
52471  iOffset = walFrameOffset(iFrame+1, szPage);
52472  szFrame = szPage + WAL_FRAME_HDRSIZE;
52473
52474  /* Write all frames into the log file exactly once */
52475  for(p=pList; p; p=p->pDirty){
52476    int nDbSize;   /* 0 normally.  Positive == commit flag */
52477    iFrame++;
52478    assert( iOffset==walFrameOffset(iFrame, szPage) );
52479    nDbSize = (isCommit && p->pDirty==0) ? nTruncate : 0;
52480    rc = walWriteOneFrame(&w, p, nDbSize, iOffset);
52481    if( rc ) return rc;
52482    pLast = p;
52483    iOffset += szFrame;
52484  }
52485
52486  /* If this is the end of a transaction, then we might need to pad
52487  ** the transaction and/or sync the WAL file.
52488  **
52489  ** Padding and syncing only occur if this set of frames complete a
52490  ** transaction and if PRAGMA synchronous=FULL.  If synchronous==NORMAL
52491  ** or synchronous==OFF, then no padding or syncing are needed.
52492  **
52493  ** If SQLITE_IOCAP_POWERSAFE_OVERWRITE is defined, then padding is not
52494  ** needed and only the sync is done.  If padding is needed, then the
52495  ** final frame is repeated (with its commit mark) until the next sector
52496  ** boundary is crossed.  Only the part of the WAL prior to the last
52497  ** sector boundary is synced; the part of the last frame that extends
52498  ** past the sector boundary is written after the sync.
52499  */
52500  if( isCommit && (sync_flags & WAL_SYNC_TRANSACTIONS)!=0 ){
52501    if( pWal->padToSectorBoundary ){
52502      int sectorSize = sqlite3SectorSize(pWal->pWalFd);
52503      w.iSyncPoint = ((iOffset+sectorSize-1)/sectorSize)*sectorSize;
52504      while( iOffset<w.iSyncPoint ){
52505        rc = walWriteOneFrame(&w, pLast, nTruncate, iOffset);
52506        if( rc ) return rc;
52507        iOffset += szFrame;
52508        nExtra++;
52509      }
52510    }else{
52511      rc = sqlite3OsSync(w.pFd, sync_flags & SQLITE_SYNC_MASK);
52512    }
52513  }
52514
52515  /* If this frame set completes the first transaction in the WAL and
52516  ** if PRAGMA journal_size_limit is set, then truncate the WAL to the
52517  ** journal size limit, if possible.
52518  */
52519  if( isCommit && pWal->truncateOnCommit && pWal->mxWalSize>=0 ){
52520    i64 sz = pWal->mxWalSize;
52521    if( walFrameOffset(iFrame+nExtra+1, szPage)>pWal->mxWalSize ){
52522      sz = walFrameOffset(iFrame+nExtra+1, szPage);
52523    }
52524    walLimitSize(pWal, sz);
52525    pWal->truncateOnCommit = 0;
52526  }
52527
52528  /* Append data to the wal-index. It is not necessary to lock the
52529  ** wal-index to do this as the SQLITE_SHM_WRITE lock held on the wal-index
52530  ** guarantees that there are no other writers, and no data that may
52531  ** be in use by existing readers is being overwritten.
52532  */
52533  iFrame = pWal->hdr.mxFrame;
52534  for(p=pList; p && rc==SQLITE_OK; p=p->pDirty){
52535    iFrame++;
52536    rc = walIndexAppend(pWal, iFrame, p->pgno);
52537  }
52538  while( rc==SQLITE_OK && nExtra>0 ){
52539    iFrame++;
52540    nExtra--;
52541    rc = walIndexAppend(pWal, iFrame, pLast->pgno);
52542  }
52543
52544  if( rc==SQLITE_OK ){
52545    /* Update the private copy of the header. */
52546    pWal->hdr.szPage = (u16)((szPage&0xff00) | (szPage>>16));
52547    testcase( szPage<=32768 );
52548    testcase( szPage>=65536 );
52549    pWal->hdr.mxFrame = iFrame;
52550    if( isCommit ){
52551      pWal->hdr.iChange++;
52552      pWal->hdr.nPage = nTruncate;
52553    }
52554    /* If this is a commit, update the wal-index header too. */
52555    if( isCommit ){
52556      walIndexWriteHdr(pWal);
52557      pWal->iCallback = iFrame;
52558    }
52559  }
52560
52561  WALTRACE(("WAL%p: frame write %s\n", pWal, rc ? "failed" : "ok"));
52562  return rc;
52563}
52564
52565/*
52566** This routine is called to implement sqlite3_wal_checkpoint() and
52567** related interfaces.
52568**
52569** Obtain a CHECKPOINT lock and then backfill as much information as
52570** we can from WAL into the database.
52571**
52572** If parameter xBusy is not NULL, it is a pointer to a busy-handler
52573** callback. In this case this function runs a blocking checkpoint.
52574*/
52575SQLITE_PRIVATE int sqlite3WalCheckpoint(
52576  Wal *pWal,                      /* Wal connection */
52577  int eMode,                      /* PASSIVE, FULL, RESTART, or TRUNCATE */
52578  int (*xBusy)(void*),            /* Function to call when busy */
52579  void *pBusyArg,                 /* Context argument for xBusyHandler */
52580  int sync_flags,                 /* Flags to sync db file with (or 0) */
52581  int nBuf,                       /* Size of temporary buffer */
52582  u8 *zBuf,                       /* Temporary buffer to use */
52583  int *pnLog,                     /* OUT: Number of frames in WAL */
52584  int *pnCkpt                     /* OUT: Number of backfilled frames in WAL */
52585){
52586  int rc;                         /* Return code */
52587  int isChanged = 0;              /* True if a new wal-index header is loaded */
52588  int eMode2 = eMode;             /* Mode to pass to walCheckpoint() */
52589  int (*xBusy2)(void*) = xBusy;   /* Busy handler for eMode2 */
52590
52591  assert( pWal->ckptLock==0 );
52592  assert( pWal->writeLock==0 );
52593
52594  /* EVIDENCE-OF: R-62920-47450 The busy-handler callback is never invoked
52595  ** in the SQLITE_CHECKPOINT_PASSIVE mode. */
52596  assert( eMode!=SQLITE_CHECKPOINT_PASSIVE || xBusy==0 );
52597
52598  if( pWal->readOnly ) return SQLITE_READONLY;
52599  WALTRACE(("WAL%p: checkpoint begins\n", pWal));
52600
52601  /* IMPLEMENTATION-OF: R-62028-47212 All calls obtain an exclusive
52602  ** "checkpoint" lock on the database file. */
52603  rc = walLockExclusive(pWal, WAL_CKPT_LOCK, 1, 0);
52604  if( rc ){
52605    /* EVIDENCE-OF: R-10421-19736 If any other process is running a
52606    ** checkpoint operation at the same time, the lock cannot be obtained and
52607    ** SQLITE_BUSY is returned.
52608    ** EVIDENCE-OF: R-53820-33897 Even if there is a busy-handler configured,
52609    ** it will not be invoked in this case.
52610    */
52611    testcase( rc==SQLITE_BUSY );
52612    testcase( xBusy!=0 );
52613    return rc;
52614  }
52615  pWal->ckptLock = 1;
52616
52617  /* IMPLEMENTATION-OF: R-59782-36818 The SQLITE_CHECKPOINT_FULL, RESTART and
52618  ** TRUNCATE modes also obtain the exclusive "writer" lock on the database
52619  ** file.
52620  **
52621  ** EVIDENCE-OF: R-60642-04082 If the writer lock cannot be obtained
52622  ** immediately, and a busy-handler is configured, it is invoked and the
52623  ** writer lock retried until either the busy-handler returns 0 or the
52624  ** lock is successfully obtained.
52625  */
52626  if( eMode!=SQLITE_CHECKPOINT_PASSIVE ){
52627    rc = walBusyLock(pWal, xBusy, pBusyArg, WAL_WRITE_LOCK, 1);
52628    if( rc==SQLITE_OK ){
52629      pWal->writeLock = 1;
52630    }else if( rc==SQLITE_BUSY ){
52631      eMode2 = SQLITE_CHECKPOINT_PASSIVE;
52632      xBusy2 = 0;
52633      rc = SQLITE_OK;
52634    }
52635  }
52636
52637  /* Read the wal-index header. */
52638  if( rc==SQLITE_OK ){
52639    rc = walIndexReadHdr(pWal, &isChanged);
52640    if( isChanged && pWal->pDbFd->pMethods->iVersion>=3 ){
52641      sqlite3OsUnfetch(pWal->pDbFd, 0, 0);
52642    }
52643  }
52644
52645  /* Copy data from the log to the database file. */
52646  if( rc==SQLITE_OK ){
52647    if( pWal->hdr.mxFrame && walPagesize(pWal)!=nBuf ){
52648      rc = SQLITE_CORRUPT_BKPT;
52649    }else{
52650      rc = walCheckpoint(pWal, eMode2, xBusy2, pBusyArg, sync_flags, zBuf);
52651    }
52652
52653    /* If no error occurred, set the output variables. */
52654    if( rc==SQLITE_OK || rc==SQLITE_BUSY ){
52655      if( pnLog ) *pnLog = (int)pWal->hdr.mxFrame;
52656      if( pnCkpt ) *pnCkpt = (int)(walCkptInfo(pWal)->nBackfill);
52657    }
52658  }
52659
52660  if( isChanged ){
52661    /* If a new wal-index header was loaded before the checkpoint was
52662    ** performed, then the pager-cache associated with pWal is now
52663    ** out of date. So zero the cached wal-index header to ensure that
52664    ** next time the pager opens a snapshot on this database it knows that
52665    ** the cache needs to be reset.
52666    */
52667    memset(&pWal->hdr, 0, sizeof(WalIndexHdr));
52668  }
52669
52670  /* Release the locks. */
52671  sqlite3WalEndWriteTransaction(pWal);
52672  walUnlockExclusive(pWal, WAL_CKPT_LOCK, 1);
52673  pWal->ckptLock = 0;
52674  WALTRACE(("WAL%p: checkpoint %s\n", pWal, rc ? "failed" : "ok"));
52675  return (rc==SQLITE_OK && eMode!=eMode2 ? SQLITE_BUSY : rc);
52676}
52677
52678/* Return the value to pass to a sqlite3_wal_hook callback, the
52679** number of frames in the WAL at the point of the last commit since
52680** sqlite3WalCallback() was called.  If no commits have occurred since
52681** the last call, then return 0.
52682*/
52683SQLITE_PRIVATE int sqlite3WalCallback(Wal *pWal){
52684  u32 ret = 0;
52685  if( pWal ){
52686    ret = pWal->iCallback;
52687    pWal->iCallback = 0;
52688  }
52689  return (int)ret;
52690}
52691
52692/*
52693** This function is called to change the WAL subsystem into or out
52694** of locking_mode=EXCLUSIVE.
52695**
52696** If op is zero, then attempt to change from locking_mode=EXCLUSIVE
52697** into locking_mode=NORMAL.  This means that we must acquire a lock
52698** on the pWal->readLock byte.  If the WAL is already in locking_mode=NORMAL
52699** or if the acquisition of the lock fails, then return 0.  If the
52700** transition out of exclusive-mode is successful, return 1.  This
52701** operation must occur while the pager is still holding the exclusive
52702** lock on the main database file.
52703**
52704** If op is one, then change from locking_mode=NORMAL into
52705** locking_mode=EXCLUSIVE.  This means that the pWal->readLock must
52706** be released.  Return 1 if the transition is made and 0 if the
52707** WAL is already in exclusive-locking mode - meaning that this
52708** routine is a no-op.  The pager must already hold the exclusive lock
52709** on the main database file before invoking this operation.
52710**
52711** If op is negative, then do a dry-run of the op==1 case but do
52712** not actually change anything. The pager uses this to see if it
52713** should acquire the database exclusive lock prior to invoking
52714** the op==1 case.
52715*/
52716SQLITE_PRIVATE int sqlite3WalExclusiveMode(Wal *pWal, int op){
52717  int rc;
52718  assert( pWal->writeLock==0 );
52719  assert( pWal->exclusiveMode!=WAL_HEAPMEMORY_MODE || op==-1 );
52720
52721  /* pWal->readLock is usually set, but might be -1 if there was a
52722  ** prior error while attempting to acquire are read-lock. This cannot
52723  ** happen if the connection is actually in exclusive mode (as no xShmLock
52724  ** locks are taken in this case). Nor should the pager attempt to
52725  ** upgrade to exclusive-mode following such an error.
52726  */
52727  assert( pWal->readLock>=0 || pWal->lockError );
52728  assert( pWal->readLock>=0 || (op<=0 && pWal->exclusiveMode==0) );
52729
52730  if( op==0 ){
52731    if( pWal->exclusiveMode ){
52732      pWal->exclusiveMode = 0;
52733      if( walLockShared(pWal, WAL_READ_LOCK(pWal->readLock))!=SQLITE_OK ){
52734        pWal->exclusiveMode = 1;
52735      }
52736      rc = pWal->exclusiveMode==0;
52737    }else{
52738      /* Already in locking_mode=NORMAL */
52739      rc = 0;
52740    }
52741  }else if( op>0 ){
52742    assert( pWal->exclusiveMode==0 );
52743    assert( pWal->readLock>=0 );
52744    walUnlockShared(pWal, WAL_READ_LOCK(pWal->readLock));
52745    pWal->exclusiveMode = 1;
52746    rc = 1;
52747  }else{
52748    rc = pWal->exclusiveMode==0;
52749  }
52750  return rc;
52751}
52752
52753/*
52754** Return true if the argument is non-NULL and the WAL module is using
52755** heap-memory for the wal-index. Otherwise, if the argument is NULL or the
52756** WAL module is using shared-memory, return false.
52757*/
52758SQLITE_PRIVATE int sqlite3WalHeapMemory(Wal *pWal){
52759  return (pWal && pWal->exclusiveMode==WAL_HEAPMEMORY_MODE );
52760}
52761
52762#ifdef SQLITE_ENABLE_ZIPVFS
52763/*
52764** If the argument is not NULL, it points to a Wal object that holds a
52765** read-lock. This function returns the database page-size if it is known,
52766** or zero if it is not (or if pWal is NULL).
52767*/
52768SQLITE_PRIVATE int sqlite3WalFramesize(Wal *pWal){
52769  assert( pWal==0 || pWal->readLock>=0 );
52770  return (pWal ? pWal->szPage : 0);
52771}
52772#endif
52773
52774#endif /* #ifndef SQLITE_OMIT_WAL */
52775
52776/************** End of wal.c *************************************************/
52777/************** Begin file btmutex.c *****************************************/
52778/*
52779** 2007 August 27
52780**
52781** The author disclaims copyright to this source code.  In place of
52782** a legal notice, here is a blessing:
52783**
52784**    May you do good and not evil.
52785**    May you find forgiveness for yourself and forgive others.
52786**    May you share freely, never taking more than you give.
52787**
52788*************************************************************************
52789**
52790** This file contains code used to implement mutexes on Btree objects.
52791** This code really belongs in btree.c.  But btree.c is getting too
52792** big and we want to break it down some.  This packaged seemed like
52793** a good breakout.
52794*/
52795/************** Include btreeInt.h in the middle of btmutex.c ****************/
52796/************** Begin file btreeInt.h ****************************************/
52797/*
52798** 2004 April 6
52799**
52800** The author disclaims copyright to this source code.  In place of
52801** a legal notice, here is a blessing:
52802**
52803**    May you do good and not evil.
52804**    May you find forgiveness for yourself and forgive others.
52805**    May you share freely, never taking more than you give.
52806**
52807*************************************************************************
52808** This file implements an external (disk-based) database using BTrees.
52809** For a detailed discussion of BTrees, refer to
52810**
52811**     Donald E. Knuth, THE ART OF COMPUTER PROGRAMMING, Volume 3:
52812**     "Sorting And Searching", pages 473-480. Addison-Wesley
52813**     Publishing Company, Reading, Massachusetts.
52814**
52815** The basic idea is that each page of the file contains N database
52816** entries and N+1 pointers to subpages.
52817**
52818**   ----------------------------------------------------------------
52819**   |  Ptr(0) | Key(0) | Ptr(1) | Key(1) | ... | Key(N-1) | Ptr(N) |
52820**   ----------------------------------------------------------------
52821**
52822** All of the keys on the page that Ptr(0) points to have values less
52823** than Key(0).  All of the keys on page Ptr(1) and its subpages have
52824** values greater than Key(0) and less than Key(1).  All of the keys
52825** on Ptr(N) and its subpages have values greater than Key(N-1).  And
52826** so forth.
52827**
52828** Finding a particular key requires reading O(log(M)) pages from the
52829** disk where M is the number of entries in the tree.
52830**
52831** In this implementation, a single file can hold one or more separate
52832** BTrees.  Each BTree is identified by the index of its root page.  The
52833** key and data for any entry are combined to form the "payload".  A
52834** fixed amount of payload can be carried directly on the database
52835** page.  If the payload is larger than the preset amount then surplus
52836** bytes are stored on overflow pages.  The payload for an entry
52837** and the preceding pointer are combined to form a "Cell".  Each
52838** page has a small header which contains the Ptr(N) pointer and other
52839** information such as the size of key and data.
52840**
52841** FORMAT DETAILS
52842**
52843** The file is divided into pages.  The first page is called page 1,
52844** the second is page 2, and so forth.  A page number of zero indicates
52845** "no such page".  The page size can be any power of 2 between 512 and 65536.
52846** Each page can be either a btree page, a freelist page, an overflow
52847** page, or a pointer-map page.
52848**
52849** The first page is always a btree page.  The first 100 bytes of the first
52850** page contain a special header (the "file header") that describes the file.
52851** The format of the file header is as follows:
52852**
52853**   OFFSET   SIZE    DESCRIPTION
52854**      0      16     Header string: "SQLite format 3\000"
52855**     16       2     Page size in bytes.  (1 means 65536)
52856**     18       1     File format write version
52857**     19       1     File format read version
52858**     20       1     Bytes of unused space at the end of each page
52859**     21       1     Max embedded payload fraction (must be 64)
52860**     22       1     Min embedded payload fraction (must be 32)
52861**     23       1     Min leaf payload fraction (must be 32)
52862**     24       4     File change counter
52863**     28       4     Reserved for future use
52864**     32       4     First freelist page
52865**     36       4     Number of freelist pages in the file
52866**     40      60     15 4-byte meta values passed to higher layers
52867**
52868**     40       4     Schema cookie
52869**     44       4     File format of schema layer
52870**     48       4     Size of page cache
52871**     52       4     Largest root-page (auto/incr_vacuum)
52872**     56       4     1=UTF-8 2=UTF16le 3=UTF16be
52873**     60       4     User version
52874**     64       4     Incremental vacuum mode
52875**     68       4     Application-ID
52876**     72      20     unused
52877**     92       4     The version-valid-for number
52878**     96       4     SQLITE_VERSION_NUMBER
52879**
52880** All of the integer values are big-endian (most significant byte first).
52881**
52882** The file change counter is incremented when the database is changed
52883** This counter allows other processes to know when the file has changed
52884** and thus when they need to flush their cache.
52885**
52886** The max embedded payload fraction is the amount of the total usable
52887** space in a page that can be consumed by a single cell for standard
52888** B-tree (non-LEAFDATA) tables.  A value of 255 means 100%.  The default
52889** is to limit the maximum cell size so that at least 4 cells will fit
52890** on one page.  Thus the default max embedded payload fraction is 64.
52891**
52892** If the payload for a cell is larger than the max payload, then extra
52893** payload is spilled to overflow pages.  Once an overflow page is allocated,
52894** as many bytes as possible are moved into the overflow pages without letting
52895** the cell size drop below the min embedded payload fraction.
52896**
52897** The min leaf payload fraction is like the min embedded payload fraction
52898** except that it applies to leaf nodes in a LEAFDATA tree.  The maximum
52899** payload fraction for a LEAFDATA tree is always 100% (or 255) and it
52900** not specified in the header.
52901**
52902** Each btree pages is divided into three sections:  The header, the
52903** cell pointer array, and the cell content area.  Page 1 also has a 100-byte
52904** file header that occurs before the page header.
52905**
52906**      |----------------|
52907**      | file header    |   100 bytes.  Page 1 only.
52908**      |----------------|
52909**      | page header    |   8 bytes for leaves.  12 bytes for interior nodes
52910**      |----------------|
52911**      | cell pointer   |   |  2 bytes per cell.  Sorted order.
52912**      | array          |   |  Grows downward
52913**      |                |   v
52914**      |----------------|
52915**      | unallocated    |
52916**      | space          |
52917**      |----------------|   ^  Grows upwards
52918**      | cell content   |   |  Arbitrary order interspersed with freeblocks.
52919**      | area           |   |  and free space fragments.
52920**      |----------------|
52921**
52922** The page headers looks like this:
52923**
52924**   OFFSET   SIZE     DESCRIPTION
52925**      0       1      Flags. 1: intkey, 2: zerodata, 4: leafdata, 8: leaf
52926**      1       2      byte offset to the first freeblock
52927**      3       2      number of cells on this page
52928**      5       2      first byte of the cell content area
52929**      7       1      number of fragmented free bytes
52930**      8       4      Right child (the Ptr(N) value).  Omitted on leaves.
52931**
52932** The flags define the format of this btree page.  The leaf flag means that
52933** this page has no children.  The zerodata flag means that this page carries
52934** only keys and no data.  The intkey flag means that the key is an integer
52935** which is stored in the key size entry of the cell header rather than in
52936** the payload area.
52937**
52938** The cell pointer array begins on the first byte after the page header.
52939** The cell pointer array contains zero or more 2-byte numbers which are
52940** offsets from the beginning of the page to the cell content in the cell
52941** content area.  The cell pointers occur in sorted order.  The system strives
52942** to keep free space after the last cell pointer so that new cells can
52943** be easily added without having to defragment the page.
52944**
52945** Cell content is stored at the very end of the page and grows toward the
52946** beginning of the page.
52947**
52948** Unused space within the cell content area is collected into a linked list of
52949** freeblocks.  Each freeblock is at least 4 bytes in size.  The byte offset
52950** to the first freeblock is given in the header.  Freeblocks occur in
52951** increasing order.  Because a freeblock must be at least 4 bytes in size,
52952** any group of 3 or fewer unused bytes in the cell content area cannot
52953** exist on the freeblock chain.  A group of 3 or fewer free bytes is called
52954** a fragment.  The total number of bytes in all fragments is recorded.
52955** in the page header at offset 7.
52956**
52957**    SIZE    DESCRIPTION
52958**      2     Byte offset of the next freeblock
52959**      2     Bytes in this freeblock
52960**
52961** Cells are of variable length.  Cells are stored in the cell content area at
52962** the end of the page.  Pointers to the cells are in the cell pointer array
52963** that immediately follows the page header.  Cells is not necessarily
52964** contiguous or in order, but cell pointers are contiguous and in order.
52965**
52966** Cell content makes use of variable length integers.  A variable
52967** length integer is 1 to 9 bytes where the lower 7 bits of each
52968** byte are used.  The integer consists of all bytes that have bit 8 set and
52969** the first byte with bit 8 clear.  The most significant byte of the integer
52970** appears first.  A variable-length integer may not be more than 9 bytes long.
52971** As a special case, all 8 bytes of the 9th byte are used as data.  This
52972** allows a 64-bit integer to be encoded in 9 bytes.
52973**
52974**    0x00                      becomes  0x00000000
52975**    0x7f                      becomes  0x0000007f
52976**    0x81 0x00                 becomes  0x00000080
52977**    0x82 0x00                 becomes  0x00000100
52978**    0x80 0x7f                 becomes  0x0000007f
52979**    0x8a 0x91 0xd1 0xac 0x78  becomes  0x12345678
52980**    0x81 0x81 0x81 0x81 0x01  becomes  0x10204081
52981**
52982** Variable length integers are used for rowids and to hold the number of
52983** bytes of key and data in a btree cell.
52984**
52985** The content of a cell looks like this:
52986**
52987**    SIZE    DESCRIPTION
52988**      4     Page number of the left child. Omitted if leaf flag is set.
52989**     var    Number of bytes of data. Omitted if the zerodata flag is set.
52990**     var    Number of bytes of key. Or the key itself if intkey flag is set.
52991**      *     Payload
52992**      4     First page of the overflow chain.  Omitted if no overflow
52993**
52994** Overflow pages form a linked list.  Each page except the last is completely
52995** filled with data (pagesize - 4 bytes).  The last page can have as little
52996** as 1 byte of data.
52997**
52998**    SIZE    DESCRIPTION
52999**      4     Page number of next overflow page
53000**      *     Data
53001**
53002** Freelist pages come in two subtypes: trunk pages and leaf pages.  The
53003** file header points to the first in a linked list of trunk page.  Each trunk
53004** page points to multiple leaf pages.  The content of a leaf page is
53005** unspecified.  A trunk page looks like this:
53006**
53007**    SIZE    DESCRIPTION
53008**      4     Page number of next trunk page
53009**      4     Number of leaf pointers on this page
53010**      *     zero or more pages numbers of leaves
53011*/
53012/* #include "sqliteInt.h" */
53013
53014
53015/* The following value is the maximum cell size assuming a maximum page
53016** size give above.
53017*/
53018#define MX_CELL_SIZE(pBt)  ((int)(pBt->pageSize-8))
53019
53020/* The maximum number of cells on a single page of the database.  This
53021** assumes a minimum cell size of 6 bytes  (4 bytes for the cell itself
53022** plus 2 bytes for the index to the cell in the page header).  Such
53023** small cells will be rare, but they are possible.
53024*/
53025#define MX_CELL(pBt) ((pBt->pageSize-8)/6)
53026
53027/* Forward declarations */
53028typedef struct MemPage MemPage;
53029typedef struct BtLock BtLock;
53030typedef struct CellInfo CellInfo;
53031
53032/*
53033** This is a magic string that appears at the beginning of every
53034** SQLite database in order to identify the file as a real database.
53035**
53036** You can change this value at compile-time by specifying a
53037** -DSQLITE_FILE_HEADER="..." on the compiler command-line.  The
53038** header must be exactly 16 bytes including the zero-terminator so
53039** the string itself should be 15 characters long.  If you change
53040** the header, then your custom library will not be able to read
53041** databases generated by the standard tools and the standard tools
53042** will not be able to read databases created by your custom library.
53043*/
53044#ifndef SQLITE_FILE_HEADER /* 123456789 123456 */
53045#  define SQLITE_FILE_HEADER "SQLite format 3"
53046#endif
53047
53048/*
53049** Page type flags.  An ORed combination of these flags appear as the
53050** first byte of on-disk image of every BTree page.
53051*/
53052#define PTF_INTKEY    0x01
53053#define PTF_ZERODATA  0x02
53054#define PTF_LEAFDATA  0x04
53055#define PTF_LEAF      0x08
53056
53057/*
53058** As each page of the file is loaded into memory, an instance of the following
53059** structure is appended and initialized to zero.  This structure stores
53060** information about the page that is decoded from the raw file page.
53061**
53062** The pParent field points back to the parent page.  This allows us to
53063** walk up the BTree from any leaf to the root.  Care must be taken to
53064** unref() the parent page pointer when this page is no longer referenced.
53065** The pageDestructor() routine handles that chore.
53066**
53067** Access to all fields of this structure is controlled by the mutex
53068** stored in MemPage.pBt->mutex.
53069*/
53070struct MemPage {
53071  u8 isInit;           /* True if previously initialized. MUST BE FIRST! */
53072  u8 nOverflow;        /* Number of overflow cell bodies in aCell[] */
53073  u8 intKey;           /* True if table b-trees.  False for index b-trees */
53074  u8 intKeyLeaf;       /* True if the leaf of an intKey table */
53075  u8 noPayload;        /* True if internal intKey page (thus w/o data) */
53076  u8 leaf;             /* True if a leaf page */
53077  u8 hdrOffset;        /* 100 for page 1.  0 otherwise */
53078  u8 childPtrSize;     /* 0 if leaf==1.  4 if leaf==0 */
53079  u8 max1bytePayload;  /* min(maxLocal,127) */
53080  u8 bBusy;            /* Prevent endless loops on corrupt database files */
53081  u16 maxLocal;        /* Copy of BtShared.maxLocal or BtShared.maxLeaf */
53082  u16 minLocal;        /* Copy of BtShared.minLocal or BtShared.minLeaf */
53083  u16 cellOffset;      /* Index in aData of first cell pointer */
53084  u16 nFree;           /* Number of free bytes on the page */
53085  u16 nCell;           /* Number of cells on this page, local and ovfl */
53086  u16 maskPage;        /* Mask for page offset */
53087  u16 aiOvfl[5];       /* Insert the i-th overflow cell before the aiOvfl-th
53088                       ** non-overflow cell */
53089  u8 *apOvfl[5];       /* Pointers to the body of overflow cells */
53090  BtShared *pBt;       /* Pointer to BtShared that this page is part of */
53091  u8 *aData;           /* Pointer to disk image of the page data */
53092  u8 *aDataEnd;        /* One byte past the end of usable data */
53093  u8 *aCellIdx;        /* The cell index area */
53094  u8 *aDataOfst;       /* Same as aData for leaves.  aData+4 for interior */
53095  DbPage *pDbPage;     /* Pager page handle */
53096  u16 (*xCellSize)(MemPage*,u8*);             /* cellSizePtr method */
53097  void (*xParseCell)(MemPage*,u8*,CellInfo*); /* btreeParseCell method */
53098  Pgno pgno;           /* Page number for this page */
53099};
53100
53101/*
53102** The in-memory image of a disk page has the auxiliary information appended
53103** to the end.  EXTRA_SIZE is the number of bytes of space needed to hold
53104** that extra information.
53105*/
53106#define EXTRA_SIZE sizeof(MemPage)
53107
53108/*
53109** A linked list of the following structures is stored at BtShared.pLock.
53110** Locks are added (or upgraded from READ_LOCK to WRITE_LOCK) when a cursor
53111** is opened on the table with root page BtShared.iTable. Locks are removed
53112** from this list when a transaction is committed or rolled back, or when
53113** a btree handle is closed.
53114*/
53115struct BtLock {
53116  Btree *pBtree;        /* Btree handle holding this lock */
53117  Pgno iTable;          /* Root page of table */
53118  u8 eLock;             /* READ_LOCK or WRITE_LOCK */
53119  BtLock *pNext;        /* Next in BtShared.pLock list */
53120};
53121
53122/* Candidate values for BtLock.eLock */
53123#define READ_LOCK     1
53124#define WRITE_LOCK    2
53125
53126/* A Btree handle
53127**
53128** A database connection contains a pointer to an instance of
53129** this object for every database file that it has open.  This structure
53130** is opaque to the database connection.  The database connection cannot
53131** see the internals of this structure and only deals with pointers to
53132** this structure.
53133**
53134** For some database files, the same underlying database cache might be
53135** shared between multiple connections.  In that case, each connection
53136** has it own instance of this object.  But each instance of this object
53137** points to the same BtShared object.  The database cache and the
53138** schema associated with the database file are all contained within
53139** the BtShared object.
53140**
53141** All fields in this structure are accessed under sqlite3.mutex.
53142** The pBt pointer itself may not be changed while there exists cursors
53143** in the referenced BtShared that point back to this Btree since those
53144** cursors have to go through this Btree to find their BtShared and
53145** they often do so without holding sqlite3.mutex.
53146*/
53147struct Btree {
53148  sqlite3 *db;       /* The database connection holding this btree */
53149  BtShared *pBt;     /* Sharable content of this btree */
53150  u8 inTrans;        /* TRANS_NONE, TRANS_READ or TRANS_WRITE */
53151  u8 sharable;       /* True if we can share pBt with another db */
53152  u8 locked;         /* True if db currently has pBt locked */
53153  u8 hasIncrblobCur; /* True if there are one or more Incrblob cursors */
53154  int wantToLock;    /* Number of nested calls to sqlite3BtreeEnter() */
53155  int nBackup;       /* Number of backup operations reading this btree */
53156  u32 iDataVersion;  /* Combines with pBt->pPager->iDataVersion */
53157  Btree *pNext;      /* List of other sharable Btrees from the same db */
53158  Btree *pPrev;      /* Back pointer of the same list */
53159#ifndef SQLITE_OMIT_SHARED_CACHE
53160  BtLock lock;       /* Object used to lock page 1 */
53161#endif
53162};
53163
53164/*
53165** Btree.inTrans may take one of the following values.
53166**
53167** If the shared-data extension is enabled, there may be multiple users
53168** of the Btree structure. At most one of these may open a write transaction,
53169** but any number may have active read transactions.
53170*/
53171#define TRANS_NONE  0
53172#define TRANS_READ  1
53173#define TRANS_WRITE 2
53174
53175/*
53176** An instance of this object represents a single database file.
53177**
53178** A single database file can be in use at the same time by two
53179** or more database connections.  When two or more connections are
53180** sharing the same database file, each connection has it own
53181** private Btree object for the file and each of those Btrees points
53182** to this one BtShared object.  BtShared.nRef is the number of
53183** connections currently sharing this database file.
53184**
53185** Fields in this structure are accessed under the BtShared.mutex
53186** mutex, except for nRef and pNext which are accessed under the
53187** global SQLITE_MUTEX_STATIC_MASTER mutex.  The pPager field
53188** may not be modified once it is initially set as long as nRef>0.
53189** The pSchema field may be set once under BtShared.mutex and
53190** thereafter is unchanged as long as nRef>0.
53191**
53192** isPending:
53193**
53194**   If a BtShared client fails to obtain a write-lock on a database
53195**   table (because there exists one or more read-locks on the table),
53196**   the shared-cache enters 'pending-lock' state and isPending is
53197**   set to true.
53198**
53199**   The shared-cache leaves the 'pending lock' state when either of
53200**   the following occur:
53201**
53202**     1) The current writer (BtShared.pWriter) concludes its transaction, OR
53203**     2) The number of locks held by other connections drops to zero.
53204**
53205**   while in the 'pending-lock' state, no connection may start a new
53206**   transaction.
53207**
53208**   This feature is included to help prevent writer-starvation.
53209*/
53210struct BtShared {
53211  Pager *pPager;        /* The page cache */
53212  sqlite3 *db;          /* Database connection currently using this Btree */
53213  BtCursor *pCursor;    /* A list of all open cursors */
53214  MemPage *pPage1;      /* First page of the database */
53215  u8 openFlags;         /* Flags to sqlite3BtreeOpen() */
53216#ifndef SQLITE_OMIT_AUTOVACUUM
53217  u8 autoVacuum;        /* True if auto-vacuum is enabled */
53218  u8 incrVacuum;        /* True if incr-vacuum is enabled */
53219  u8 bDoTruncate;       /* True to truncate db on commit */
53220#endif
53221  u8 inTransaction;     /* Transaction state */
53222  u8 max1bytePayload;   /* Maximum first byte of cell for a 1-byte payload */
53223#ifdef SQLITE_HAS_CODEC
53224  u8 optimalReserve;    /* Desired amount of reserved space per page */
53225#endif
53226  u16 btsFlags;         /* Boolean parameters.  See BTS_* macros below */
53227  u16 maxLocal;         /* Maximum local payload in non-LEAFDATA tables */
53228  u16 minLocal;         /* Minimum local payload in non-LEAFDATA tables */
53229  u16 maxLeaf;          /* Maximum local payload in a LEAFDATA table */
53230  u16 minLeaf;          /* Minimum local payload in a LEAFDATA table */
53231  u32 pageSize;         /* Total number of bytes on a page */
53232  u32 usableSize;       /* Number of usable bytes on each page */
53233  int nTransaction;     /* Number of open transactions (read + write) */
53234  u32 nPage;            /* Number of pages in the database */
53235  void *pSchema;        /* Pointer to space allocated by sqlite3BtreeSchema() */
53236  void (*xFreeSchema)(void*);  /* Destructor for BtShared.pSchema */
53237  sqlite3_mutex *mutex; /* Non-recursive mutex required to access this object */
53238  Bitvec *pHasContent;  /* Set of pages moved to free-list this transaction */
53239#ifndef SQLITE_OMIT_SHARED_CACHE
53240  int nRef;             /* Number of references to this structure */
53241  BtShared *pNext;      /* Next on a list of sharable BtShared structs */
53242  BtLock *pLock;        /* List of locks held on this shared-btree struct */
53243  Btree *pWriter;       /* Btree with currently open write transaction */
53244#endif
53245  u8 *pTmpSpace;        /* Temp space sufficient to hold a single cell */
53246};
53247
53248/*
53249** Allowed values for BtShared.btsFlags
53250*/
53251#define BTS_READ_ONLY        0x0001   /* Underlying file is readonly */
53252#define BTS_PAGESIZE_FIXED   0x0002   /* Page size can no longer be changed */
53253#define BTS_SECURE_DELETE    0x0004   /* PRAGMA secure_delete is enabled */
53254#define BTS_INITIALLY_EMPTY  0x0008   /* Database was empty at trans start */
53255#define BTS_NO_WAL           0x0010   /* Do not open write-ahead-log files */
53256#define BTS_EXCLUSIVE        0x0020   /* pWriter has an exclusive lock */
53257#define BTS_PENDING          0x0040   /* Waiting for read-locks to clear */
53258
53259/*
53260** An instance of the following structure is used to hold information
53261** about a cell.  The parseCellPtr() function fills in this structure
53262** based on information extract from the raw disk page.
53263*/
53264struct CellInfo {
53265  i64 nKey;      /* The key for INTKEY tables, or nPayload otherwise */
53266  u8 *pPayload;  /* Pointer to the start of payload */
53267  u32 nPayload;  /* Bytes of payload */
53268  u16 nLocal;    /* Amount of payload held locally, not on overflow */
53269  u16 iOverflow; /* Offset to overflow page number.  Zero if no overflow */
53270  u16 nSize;     /* Size of the cell content on the main b-tree page */
53271};
53272
53273/*
53274** Maximum depth of an SQLite B-Tree structure. Any B-Tree deeper than
53275** this will be declared corrupt. This value is calculated based on a
53276** maximum database size of 2^31 pages a minimum fanout of 2 for a
53277** root-node and 3 for all other internal nodes.
53278**
53279** If a tree that appears to be taller than this is encountered, it is
53280** assumed that the database is corrupt.
53281*/
53282#define BTCURSOR_MAX_DEPTH 20
53283
53284/*
53285** A cursor is a pointer to a particular entry within a particular
53286** b-tree within a database file.
53287**
53288** The entry is identified by its MemPage and the index in
53289** MemPage.aCell[] of the entry.
53290**
53291** A single database file can be shared by two more database connections,
53292** but cursors cannot be shared.  Each cursor is associated with a
53293** particular database connection identified BtCursor.pBtree.db.
53294**
53295** Fields in this structure are accessed under the BtShared.mutex
53296** found at self->pBt->mutex.
53297**
53298** skipNext meaning:
53299**    eState==SKIPNEXT && skipNext>0:  Next sqlite3BtreeNext() is no-op.
53300**    eState==SKIPNEXT && skipNext<0:  Next sqlite3BtreePrevious() is no-op.
53301**    eState==FAULT:                   Cursor fault with skipNext as error code.
53302*/
53303struct BtCursor {
53304  Btree *pBtree;            /* The Btree to which this cursor belongs */
53305  BtShared *pBt;            /* The BtShared this cursor points to */
53306  BtCursor *pNext;          /* Forms a linked list of all cursors */
53307  Pgno *aOverflow;          /* Cache of overflow page locations */
53308  CellInfo info;            /* A parse of the cell we are pointing at */
53309  i64 nKey;                 /* Size of pKey, or last integer key */
53310  void *pKey;               /* Saved key that was cursor last known position */
53311  Pgno pgnoRoot;            /* The root page of this tree */
53312  int nOvflAlloc;           /* Allocated size of aOverflow[] array */
53313  int skipNext;    /* Prev() is noop if negative. Next() is noop if positive.
53314                   ** Error code if eState==CURSOR_FAULT */
53315  u8 curFlags;              /* zero or more BTCF_* flags defined below */
53316  u8 curPagerFlags;         /* Flags to send to sqlite3PagerAcquire() */
53317  u8 eState;                /* One of the CURSOR_XXX constants (see below) */
53318  u8 hints;                 /* As configured by CursorSetHints() */
53319  /* All fields above are zeroed when the cursor is allocated.  See
53320  ** sqlite3BtreeCursorZero().  Fields that follow must be manually
53321  ** initialized. */
53322  i8 iPage;                 /* Index of current page in apPage */
53323  u8 curIntKey;             /* Value of apPage[0]->intKey */
53324  struct KeyInfo *pKeyInfo; /* Argument passed to comparison function */
53325  void *padding1;           /* Make object size a multiple of 16 */
53326  u16 aiIdx[BTCURSOR_MAX_DEPTH];        /* Current index in apPage[i] */
53327  MemPage *apPage[BTCURSOR_MAX_DEPTH];  /* Pages from root to current page */
53328};
53329
53330/*
53331** Legal values for BtCursor.curFlags
53332*/
53333#define BTCF_WriteFlag    0x01   /* True if a write cursor */
53334#define BTCF_ValidNKey    0x02   /* True if info.nKey is valid */
53335#define BTCF_ValidOvfl    0x04   /* True if aOverflow is valid */
53336#define BTCF_AtLast       0x08   /* Cursor is pointing ot the last entry */
53337#define BTCF_Incrblob     0x10   /* True if an incremental I/O handle */
53338#define BTCF_Multiple     0x20   /* Maybe another cursor on the same btree */
53339
53340/*
53341** Potential values for BtCursor.eState.
53342**
53343** CURSOR_INVALID:
53344**   Cursor does not point to a valid entry. This can happen (for example)
53345**   because the table is empty or because BtreeCursorFirst() has not been
53346**   called.
53347**
53348** CURSOR_VALID:
53349**   Cursor points to a valid entry. getPayload() etc. may be called.
53350**
53351** CURSOR_SKIPNEXT:
53352**   Cursor is valid except that the Cursor.skipNext field is non-zero
53353**   indicating that the next sqlite3BtreeNext() or sqlite3BtreePrevious()
53354**   operation should be a no-op.
53355**
53356** CURSOR_REQUIRESEEK:
53357**   The table that this cursor was opened on still exists, but has been
53358**   modified since the cursor was last used. The cursor position is saved
53359**   in variables BtCursor.pKey and BtCursor.nKey. When a cursor is in
53360**   this state, restoreCursorPosition() can be called to attempt to
53361**   seek the cursor to the saved position.
53362**
53363** CURSOR_FAULT:
53364**   An unrecoverable error (an I/O error or a malloc failure) has occurred
53365**   on a different connection that shares the BtShared cache with this
53366**   cursor.  The error has left the cache in an inconsistent state.
53367**   Do nothing else with this cursor.  Any attempt to use the cursor
53368**   should return the error code stored in BtCursor.skipNext
53369*/
53370#define CURSOR_INVALID           0
53371#define CURSOR_VALID             1
53372#define CURSOR_SKIPNEXT          2
53373#define CURSOR_REQUIRESEEK       3
53374#define CURSOR_FAULT             4
53375
53376/*
53377** The database page the PENDING_BYTE occupies. This page is never used.
53378*/
53379# define PENDING_BYTE_PAGE(pBt) PAGER_MJ_PGNO(pBt)
53380
53381/*
53382** These macros define the location of the pointer-map entry for a
53383** database page. The first argument to each is the number of usable
53384** bytes on each page of the database (often 1024). The second is the
53385** page number to look up in the pointer map.
53386**
53387** PTRMAP_PAGENO returns the database page number of the pointer-map
53388** page that stores the required pointer. PTRMAP_PTROFFSET returns
53389** the offset of the requested map entry.
53390**
53391** If the pgno argument passed to PTRMAP_PAGENO is a pointer-map page,
53392** then pgno is returned. So (pgno==PTRMAP_PAGENO(pgsz, pgno)) can be
53393** used to test if pgno is a pointer-map page. PTRMAP_ISPAGE implements
53394** this test.
53395*/
53396#define PTRMAP_PAGENO(pBt, pgno) ptrmapPageno(pBt, pgno)
53397#define PTRMAP_PTROFFSET(pgptrmap, pgno) (5*(pgno-pgptrmap-1))
53398#define PTRMAP_ISPAGE(pBt, pgno) (PTRMAP_PAGENO((pBt),(pgno))==(pgno))
53399
53400/*
53401** The pointer map is a lookup table that identifies the parent page for
53402** each child page in the database file.  The parent page is the page that
53403** contains a pointer to the child.  Every page in the database contains
53404** 0 or 1 parent pages.  (In this context 'database page' refers
53405** to any page that is not part of the pointer map itself.)  Each pointer map
53406** entry consists of a single byte 'type' and a 4 byte parent page number.
53407** The PTRMAP_XXX identifiers below are the valid types.
53408**
53409** The purpose of the pointer map is to facility moving pages from one
53410** position in the file to another as part of autovacuum.  When a page
53411** is moved, the pointer in its parent must be updated to point to the
53412** new location.  The pointer map is used to locate the parent page quickly.
53413**
53414** PTRMAP_ROOTPAGE: The database page is a root-page. The page-number is not
53415**                  used in this case.
53416**
53417** PTRMAP_FREEPAGE: The database page is an unused (free) page. The page-number
53418**                  is not used in this case.
53419**
53420** PTRMAP_OVERFLOW1: The database page is the first page in a list of
53421**                   overflow pages. The page number identifies the page that
53422**                   contains the cell with a pointer to this overflow page.
53423**
53424** PTRMAP_OVERFLOW2: The database page is the second or later page in a list of
53425**                   overflow pages. The page-number identifies the previous
53426**                   page in the overflow page list.
53427**
53428** PTRMAP_BTREE: The database page is a non-root btree page. The page number
53429**               identifies the parent page in the btree.
53430*/
53431#define PTRMAP_ROOTPAGE 1
53432#define PTRMAP_FREEPAGE 2
53433#define PTRMAP_OVERFLOW1 3
53434#define PTRMAP_OVERFLOW2 4
53435#define PTRMAP_BTREE 5
53436
53437/* A bunch of assert() statements to check the transaction state variables
53438** of handle p (type Btree*) are internally consistent.
53439*/
53440#define btreeIntegrity(p) \
53441  assert( p->pBt->inTransaction!=TRANS_NONE || p->pBt->nTransaction==0 ); \
53442  assert( p->pBt->inTransaction>=p->inTrans );
53443
53444
53445/*
53446** The ISAUTOVACUUM macro is used within balance_nonroot() to determine
53447** if the database supports auto-vacuum or not. Because it is used
53448** within an expression that is an argument to another macro
53449** (sqliteMallocRaw), it is not possible to use conditional compilation.
53450** So, this macro is defined instead.
53451*/
53452#ifndef SQLITE_OMIT_AUTOVACUUM
53453#define ISAUTOVACUUM (pBt->autoVacuum)
53454#else
53455#define ISAUTOVACUUM 0
53456#endif
53457
53458
53459/*
53460** This structure is passed around through all the sanity checking routines
53461** in order to keep track of some global state information.
53462**
53463** The aRef[] array is allocated so that there is 1 bit for each page in
53464** the database. As the integrity-check proceeds, for each page used in
53465** the database the corresponding bit is set. This allows integrity-check to
53466** detect pages that are used twice and orphaned pages (both of which
53467** indicate corruption).
53468*/
53469typedef struct IntegrityCk IntegrityCk;
53470struct IntegrityCk {
53471  BtShared *pBt;    /* The tree being checked out */
53472  Pager *pPager;    /* The associated pager.  Also accessible by pBt->pPager */
53473  u8 *aPgRef;       /* 1 bit per page in the db (see above) */
53474  Pgno nPage;       /* Number of pages in the database */
53475  int mxErr;        /* Stop accumulating errors when this reaches zero */
53476  int nErr;         /* Number of messages written to zErrMsg so far */
53477  int mallocFailed; /* A memory allocation error has occurred */
53478  const char *zPfx; /* Error message prefix */
53479  int v1, v2;       /* Values for up to two %d fields in zPfx */
53480  StrAccum errMsg;  /* Accumulate the error message text here */
53481  u32 *heap;        /* Min-heap used for analyzing cell coverage */
53482};
53483
53484/*
53485** Routines to read or write a two- and four-byte big-endian integer values.
53486*/
53487#define get2byte(x)   ((x)[0]<<8 | (x)[1])
53488#define put2byte(p,v) ((p)[0] = (u8)((v)>>8), (p)[1] = (u8)(v))
53489#define get4byte sqlite3Get4byte
53490#define put4byte sqlite3Put4byte
53491
53492/*
53493** get2byteAligned(), unlike get2byte(), requires that its argument point to a
53494** two-byte aligned address.  get2bytea() is only used for accessing the
53495** cell addresses in a btree header.
53496*/
53497#if SQLITE_BYTEORDER==4321
53498# define get2byteAligned(x)  (*(u16*)(x))
53499#elif SQLITE_BYTEORDER==1234 && GCC_VERSION>=4008000
53500# define get2byteAligned(x)  __builtin_bswap16(*(u16*)(x))
53501#elif SQLITE_BYTEORDER==1234 && defined(_MSC_VER) && _MSC_VER>=1300
53502# define get2byteAligned(x)  _byteswap_ushort(*(u16*)(x))
53503#else
53504# define get2byteAligned(x)  ((x)[0]<<8 | (x)[1])
53505#endif
53506
53507/************** End of btreeInt.h ********************************************/
53508/************** Continuing where we left off in btmutex.c ********************/
53509#ifndef SQLITE_OMIT_SHARED_CACHE
53510#if SQLITE_THREADSAFE
53511
53512/*
53513** Obtain the BtShared mutex associated with B-Tree handle p. Also,
53514** set BtShared.db to the database handle associated with p and the
53515** p->locked boolean to true.
53516*/
53517static void lockBtreeMutex(Btree *p){
53518  assert( p->locked==0 );
53519  assert( sqlite3_mutex_notheld(p->pBt->mutex) );
53520  assert( sqlite3_mutex_held(p->db->mutex) );
53521
53522  sqlite3_mutex_enter(p->pBt->mutex);
53523  p->pBt->db = p->db;
53524  p->locked = 1;
53525}
53526
53527/*
53528** Release the BtShared mutex associated with B-Tree handle p and
53529** clear the p->locked boolean.
53530*/
53531static void SQLITE_NOINLINE unlockBtreeMutex(Btree *p){
53532  BtShared *pBt = p->pBt;
53533  assert( p->locked==1 );
53534  assert( sqlite3_mutex_held(pBt->mutex) );
53535  assert( sqlite3_mutex_held(p->db->mutex) );
53536  assert( p->db==pBt->db );
53537
53538  sqlite3_mutex_leave(pBt->mutex);
53539  p->locked = 0;
53540}
53541
53542/* Forward reference */
53543static void SQLITE_NOINLINE btreeLockCarefully(Btree *p);
53544
53545/*
53546** Enter a mutex on the given BTree object.
53547**
53548** If the object is not sharable, then no mutex is ever required
53549** and this routine is a no-op.  The underlying mutex is non-recursive.
53550** But we keep a reference count in Btree.wantToLock so the behavior
53551** of this interface is recursive.
53552**
53553** To avoid deadlocks, multiple Btrees are locked in the same order
53554** by all database connections.  The p->pNext is a list of other
53555** Btrees belonging to the same database connection as the p Btree
53556** which need to be locked after p.  If we cannot get a lock on
53557** p, then first unlock all of the others on p->pNext, then wait
53558** for the lock to become available on p, then relock all of the
53559** subsequent Btrees that desire a lock.
53560*/
53561SQLITE_PRIVATE void sqlite3BtreeEnter(Btree *p){
53562  /* Some basic sanity checking on the Btree.  The list of Btrees
53563  ** connected by pNext and pPrev should be in sorted order by
53564  ** Btree.pBt value. All elements of the list should belong to
53565  ** the same connection. Only shared Btrees are on the list. */
53566  assert( p->pNext==0 || p->pNext->pBt>p->pBt );
53567  assert( p->pPrev==0 || p->pPrev->pBt<p->pBt );
53568  assert( p->pNext==0 || p->pNext->db==p->db );
53569  assert( p->pPrev==0 || p->pPrev->db==p->db );
53570  assert( p->sharable || (p->pNext==0 && p->pPrev==0) );
53571
53572  /* Check for locking consistency */
53573  assert( !p->locked || p->wantToLock>0 );
53574  assert( p->sharable || p->wantToLock==0 );
53575
53576  /* We should already hold a lock on the database connection */
53577  assert( sqlite3_mutex_held(p->db->mutex) );
53578
53579  /* Unless the database is sharable and unlocked, then BtShared.db
53580  ** should already be set correctly. */
53581  assert( (p->locked==0 && p->sharable) || p->pBt->db==p->db );
53582
53583  if( !p->sharable ) return;
53584  p->wantToLock++;
53585  if( p->locked ) return;
53586  btreeLockCarefully(p);
53587}
53588
53589/* This is a helper function for sqlite3BtreeLock(). By moving
53590** complex, but seldom used logic, out of sqlite3BtreeLock() and
53591** into this routine, we avoid unnecessary stack pointer changes
53592** and thus help the sqlite3BtreeLock() routine to run much faster
53593** in the common case.
53594*/
53595static void SQLITE_NOINLINE btreeLockCarefully(Btree *p){
53596  Btree *pLater;
53597
53598  /* In most cases, we should be able to acquire the lock we
53599  ** want without having to go through the ascending lock
53600  ** procedure that follows.  Just be sure not to block.
53601  */
53602  if( sqlite3_mutex_try(p->pBt->mutex)==SQLITE_OK ){
53603    p->pBt->db = p->db;
53604    p->locked = 1;
53605    return;
53606  }
53607
53608  /* To avoid deadlock, first release all locks with a larger
53609  ** BtShared address.  Then acquire our lock.  Then reacquire
53610  ** the other BtShared locks that we used to hold in ascending
53611  ** order.
53612  */
53613  for(pLater=p->pNext; pLater; pLater=pLater->pNext){
53614    assert( pLater->sharable );
53615    assert( pLater->pNext==0 || pLater->pNext->pBt>pLater->pBt );
53616    assert( !pLater->locked || pLater->wantToLock>0 );
53617    if( pLater->locked ){
53618      unlockBtreeMutex(pLater);
53619    }
53620  }
53621  lockBtreeMutex(p);
53622  for(pLater=p->pNext; pLater; pLater=pLater->pNext){
53623    if( pLater->wantToLock ){
53624      lockBtreeMutex(pLater);
53625    }
53626  }
53627}
53628
53629
53630/*
53631** Exit the recursive mutex on a Btree.
53632*/
53633SQLITE_PRIVATE void sqlite3BtreeLeave(Btree *p){
53634  assert( sqlite3_mutex_held(p->db->mutex) );
53635  if( p->sharable ){
53636    assert( p->wantToLock>0 );
53637    p->wantToLock--;
53638    if( p->wantToLock==0 ){
53639      unlockBtreeMutex(p);
53640    }
53641  }
53642}
53643
53644#ifndef NDEBUG
53645/*
53646** Return true if the BtShared mutex is held on the btree, or if the
53647** B-Tree is not marked as sharable.
53648**
53649** This routine is used only from within assert() statements.
53650*/
53651SQLITE_PRIVATE int sqlite3BtreeHoldsMutex(Btree *p){
53652  assert( p->sharable==0 || p->locked==0 || p->wantToLock>0 );
53653  assert( p->sharable==0 || p->locked==0 || p->db==p->pBt->db );
53654  assert( p->sharable==0 || p->locked==0 || sqlite3_mutex_held(p->pBt->mutex) );
53655  assert( p->sharable==0 || p->locked==0 || sqlite3_mutex_held(p->db->mutex) );
53656
53657  return (p->sharable==0 || p->locked);
53658}
53659#endif
53660
53661
53662#ifndef SQLITE_OMIT_INCRBLOB
53663/*
53664** Enter and leave a mutex on a Btree given a cursor owned by that
53665** Btree.  These entry points are used by incremental I/O and can be
53666** omitted if that module is not used.
53667*/
53668SQLITE_PRIVATE void sqlite3BtreeEnterCursor(BtCursor *pCur){
53669  sqlite3BtreeEnter(pCur->pBtree);
53670}
53671SQLITE_PRIVATE void sqlite3BtreeLeaveCursor(BtCursor *pCur){
53672  sqlite3BtreeLeave(pCur->pBtree);
53673}
53674#endif /* SQLITE_OMIT_INCRBLOB */
53675
53676
53677/*
53678** Enter the mutex on every Btree associated with a database
53679** connection.  This is needed (for example) prior to parsing
53680** a statement since we will be comparing table and column names
53681** against all schemas and we do not want those schemas being
53682** reset out from under us.
53683**
53684** There is a corresponding leave-all procedures.
53685**
53686** Enter the mutexes in accending order by BtShared pointer address
53687** to avoid the possibility of deadlock when two threads with
53688** two or more btrees in common both try to lock all their btrees
53689** at the same instant.
53690*/
53691SQLITE_PRIVATE void sqlite3BtreeEnterAll(sqlite3 *db){
53692  int i;
53693  Btree *p;
53694  assert( sqlite3_mutex_held(db->mutex) );
53695  for(i=0; i<db->nDb; i++){
53696    p = db->aDb[i].pBt;
53697    if( p ) sqlite3BtreeEnter(p);
53698  }
53699}
53700SQLITE_PRIVATE void sqlite3BtreeLeaveAll(sqlite3 *db){
53701  int i;
53702  Btree *p;
53703  assert( sqlite3_mutex_held(db->mutex) );
53704  for(i=0; i<db->nDb; i++){
53705    p = db->aDb[i].pBt;
53706    if( p ) sqlite3BtreeLeave(p);
53707  }
53708}
53709
53710/*
53711** Return true if a particular Btree requires a lock.  Return FALSE if
53712** no lock is ever required since it is not sharable.
53713*/
53714SQLITE_PRIVATE int sqlite3BtreeSharable(Btree *p){
53715  return p->sharable;
53716}
53717
53718#ifndef NDEBUG
53719/*
53720** Return true if the current thread holds the database connection
53721** mutex and all required BtShared mutexes.
53722**
53723** This routine is used inside assert() statements only.
53724*/
53725SQLITE_PRIVATE int sqlite3BtreeHoldsAllMutexes(sqlite3 *db){
53726  int i;
53727  if( !sqlite3_mutex_held(db->mutex) ){
53728    return 0;
53729  }
53730  for(i=0; i<db->nDb; i++){
53731    Btree *p;
53732    p = db->aDb[i].pBt;
53733    if( p && p->sharable &&
53734         (p->wantToLock==0 || !sqlite3_mutex_held(p->pBt->mutex)) ){
53735      return 0;
53736    }
53737  }
53738  return 1;
53739}
53740#endif /* NDEBUG */
53741
53742#ifndef NDEBUG
53743/*
53744** Return true if the correct mutexes are held for accessing the
53745** db->aDb[iDb].pSchema structure.  The mutexes required for schema
53746** access are:
53747**
53748**   (1) The mutex on db
53749**   (2) if iDb!=1, then the mutex on db->aDb[iDb].pBt.
53750**
53751** If pSchema is not NULL, then iDb is computed from pSchema and
53752** db using sqlite3SchemaToIndex().
53753*/
53754SQLITE_PRIVATE int sqlite3SchemaMutexHeld(sqlite3 *db, int iDb, Schema *pSchema){
53755  Btree *p;
53756  assert( db!=0 );
53757  if( pSchema ) iDb = sqlite3SchemaToIndex(db, pSchema);
53758  assert( iDb>=0 && iDb<db->nDb );
53759  if( !sqlite3_mutex_held(db->mutex) ) return 0;
53760  if( iDb==1 ) return 1;
53761  p = db->aDb[iDb].pBt;
53762  assert( p!=0 );
53763  return p->sharable==0 || p->locked==1;
53764}
53765#endif /* NDEBUG */
53766
53767#else /* SQLITE_THREADSAFE>0 above.  SQLITE_THREADSAFE==0 below */
53768/*
53769** The following are special cases for mutex enter routines for use
53770** in single threaded applications that use shared cache.  Except for
53771** these two routines, all mutex operations are no-ops in that case and
53772** are null #defines in btree.h.
53773**
53774** If shared cache is disabled, then all btree mutex routines, including
53775** the ones below, are no-ops and are null #defines in btree.h.
53776*/
53777
53778SQLITE_PRIVATE void sqlite3BtreeEnter(Btree *p){
53779  p->pBt->db = p->db;
53780}
53781SQLITE_PRIVATE void sqlite3BtreeEnterAll(sqlite3 *db){
53782  int i;
53783  for(i=0; i<db->nDb; i++){
53784    Btree *p = db->aDb[i].pBt;
53785    if( p ){
53786      p->pBt->db = p->db;
53787    }
53788  }
53789}
53790#endif /* if SQLITE_THREADSAFE */
53791#endif /* ifndef SQLITE_OMIT_SHARED_CACHE */
53792
53793/************** End of btmutex.c *********************************************/
53794/************** Begin file btree.c *******************************************/
53795/*
53796** 2004 April 6
53797**
53798** The author disclaims copyright to this source code.  In place of
53799** a legal notice, here is a blessing:
53800**
53801**    May you do good and not evil.
53802**    May you find forgiveness for yourself and forgive others.
53803**    May you share freely, never taking more than you give.
53804**
53805*************************************************************************
53806** This file implements an external (disk-based) database using BTrees.
53807** See the header comment on "btreeInt.h" for additional information.
53808** Including a description of file format and an overview of operation.
53809*/
53810/* #include "btreeInt.h" */
53811
53812/*
53813** The header string that appears at the beginning of every
53814** SQLite database.
53815*/
53816static const char zMagicHeader[] = SQLITE_FILE_HEADER;
53817
53818/*
53819** Set this global variable to 1 to enable tracing using the TRACE
53820** macro.
53821*/
53822#if 0
53823int sqlite3BtreeTrace=1;  /* True to enable tracing */
53824# define TRACE(X)  if(sqlite3BtreeTrace){printf X;fflush(stdout);}
53825#else
53826# define TRACE(X)
53827#endif
53828
53829/*
53830** Extract a 2-byte big-endian integer from an array of unsigned bytes.
53831** But if the value is zero, make it 65536.
53832**
53833** This routine is used to extract the "offset to cell content area" value
53834** from the header of a btree page.  If the page size is 65536 and the page
53835** is empty, the offset should be 65536, but the 2-byte value stores zero.
53836** This routine makes the necessary adjustment to 65536.
53837*/
53838#define get2byteNotZero(X)  (((((int)get2byte(X))-1)&0xffff)+1)
53839
53840/*
53841** Values passed as the 5th argument to allocateBtreePage()
53842*/
53843#define BTALLOC_ANY   0           /* Allocate any page */
53844#define BTALLOC_EXACT 1           /* Allocate exact page if possible */
53845#define BTALLOC_LE    2           /* Allocate any page <= the parameter */
53846
53847/*
53848** Macro IfNotOmitAV(x) returns (x) if SQLITE_OMIT_AUTOVACUUM is not
53849** defined, or 0 if it is. For example:
53850**
53851**   bIncrVacuum = IfNotOmitAV(pBtShared->incrVacuum);
53852*/
53853#ifndef SQLITE_OMIT_AUTOVACUUM
53854#define IfNotOmitAV(expr) (expr)
53855#else
53856#define IfNotOmitAV(expr) 0
53857#endif
53858
53859#ifndef SQLITE_OMIT_SHARED_CACHE
53860/*
53861** A list of BtShared objects that are eligible for participation
53862** in shared cache.  This variable has file scope during normal builds,
53863** but the test harness needs to access it so we make it global for
53864** test builds.
53865**
53866** Access to this variable is protected by SQLITE_MUTEX_STATIC_MASTER.
53867*/
53868#ifdef SQLITE_TEST
53869SQLITE_PRIVATE BtShared *SQLITE_WSD sqlite3SharedCacheList = 0;
53870#else
53871static BtShared *SQLITE_WSD sqlite3SharedCacheList = 0;
53872#endif
53873#endif /* SQLITE_OMIT_SHARED_CACHE */
53874
53875#ifndef SQLITE_OMIT_SHARED_CACHE
53876/*
53877** Enable or disable the shared pager and schema features.
53878**
53879** This routine has no effect on existing database connections.
53880** The shared cache setting effects only future calls to
53881** sqlite3_open(), sqlite3_open16(), or sqlite3_open_v2().
53882*/
53883SQLITE_API int SQLITE_STDCALL sqlite3_enable_shared_cache(int enable){
53884  sqlite3GlobalConfig.sharedCacheEnabled = enable;
53885  return SQLITE_OK;
53886}
53887#endif
53888
53889
53890
53891#ifdef SQLITE_OMIT_SHARED_CACHE
53892  /*
53893  ** The functions querySharedCacheTableLock(), setSharedCacheTableLock(),
53894  ** and clearAllSharedCacheTableLocks()
53895  ** manipulate entries in the BtShared.pLock linked list used to store
53896  ** shared-cache table level locks. If the library is compiled with the
53897  ** shared-cache feature disabled, then there is only ever one user
53898  ** of each BtShared structure and so this locking is not necessary.
53899  ** So define the lock related functions as no-ops.
53900  */
53901  #define querySharedCacheTableLock(a,b,c) SQLITE_OK
53902  #define setSharedCacheTableLock(a,b,c) SQLITE_OK
53903  #define clearAllSharedCacheTableLocks(a)
53904  #define downgradeAllSharedCacheTableLocks(a)
53905  #define hasSharedCacheTableLock(a,b,c,d) 1
53906  #define hasReadConflicts(a, b) 0
53907#endif
53908
53909#ifndef SQLITE_OMIT_SHARED_CACHE
53910
53911#ifdef SQLITE_DEBUG
53912/*
53913**** This function is only used as part of an assert() statement. ***
53914**
53915** Check to see if pBtree holds the required locks to read or write to the
53916** table with root page iRoot.   Return 1 if it does and 0 if not.
53917**
53918** For example, when writing to a table with root-page iRoot via
53919** Btree connection pBtree:
53920**
53921**    assert( hasSharedCacheTableLock(pBtree, iRoot, 0, WRITE_LOCK) );
53922**
53923** When writing to an index that resides in a sharable database, the
53924** caller should have first obtained a lock specifying the root page of
53925** the corresponding table. This makes things a bit more complicated,
53926** as this module treats each table as a separate structure. To determine
53927** the table corresponding to the index being written, this
53928** function has to search through the database schema.
53929**
53930** Instead of a lock on the table/index rooted at page iRoot, the caller may
53931** hold a write-lock on the schema table (root page 1). This is also
53932** acceptable.
53933*/
53934static int hasSharedCacheTableLock(
53935  Btree *pBtree,         /* Handle that must hold lock */
53936  Pgno iRoot,            /* Root page of b-tree */
53937  int isIndex,           /* True if iRoot is the root of an index b-tree */
53938  int eLockType          /* Required lock type (READ_LOCK or WRITE_LOCK) */
53939){
53940  Schema *pSchema = (Schema *)pBtree->pBt->pSchema;
53941  Pgno iTab = 0;
53942  BtLock *pLock;
53943
53944  /* If this database is not shareable, or if the client is reading
53945  ** and has the read-uncommitted flag set, then no lock is required.
53946  ** Return true immediately.
53947  */
53948  if( (pBtree->sharable==0)
53949   || (eLockType==READ_LOCK && (pBtree->db->flags & SQLITE_ReadUncommitted))
53950  ){
53951    return 1;
53952  }
53953
53954  /* If the client is reading  or writing an index and the schema is
53955  ** not loaded, then it is too difficult to actually check to see if
53956  ** the correct locks are held.  So do not bother - just return true.
53957  ** This case does not come up very often anyhow.
53958  */
53959  if( isIndex && (!pSchema || (pSchema->schemaFlags&DB_SchemaLoaded)==0) ){
53960    return 1;
53961  }
53962
53963  /* Figure out the root-page that the lock should be held on. For table
53964  ** b-trees, this is just the root page of the b-tree being read or
53965  ** written. For index b-trees, it is the root page of the associated
53966  ** table.  */
53967  if( isIndex ){
53968    HashElem *p;
53969    for(p=sqliteHashFirst(&pSchema->idxHash); p; p=sqliteHashNext(p)){
53970      Index *pIdx = (Index *)sqliteHashData(p);
53971      if( pIdx->tnum==(int)iRoot ){
53972        if( iTab ){
53973          /* Two or more indexes share the same root page.  There must
53974          ** be imposter tables.  So just return true.  The assert is not
53975          ** useful in that case. */
53976          return 1;
53977        }
53978        iTab = pIdx->pTable->tnum;
53979      }
53980    }
53981  }else{
53982    iTab = iRoot;
53983  }
53984
53985  /* Search for the required lock. Either a write-lock on root-page iTab, a
53986  ** write-lock on the schema table, or (if the client is reading) a
53987  ** read-lock on iTab will suffice. Return 1 if any of these are found.  */
53988  for(pLock=pBtree->pBt->pLock; pLock; pLock=pLock->pNext){
53989    if( pLock->pBtree==pBtree
53990     && (pLock->iTable==iTab || (pLock->eLock==WRITE_LOCK && pLock->iTable==1))
53991     && pLock->eLock>=eLockType
53992    ){
53993      return 1;
53994    }
53995  }
53996
53997  /* Failed to find the required lock. */
53998  return 0;
53999}
54000#endif /* SQLITE_DEBUG */
54001
54002#ifdef SQLITE_DEBUG
54003/*
54004**** This function may be used as part of assert() statements only. ****
54005**
54006** Return true if it would be illegal for pBtree to write into the
54007** table or index rooted at iRoot because other shared connections are
54008** simultaneously reading that same table or index.
54009**
54010** It is illegal for pBtree to write if some other Btree object that
54011** shares the same BtShared object is currently reading or writing
54012** the iRoot table.  Except, if the other Btree object has the
54013** read-uncommitted flag set, then it is OK for the other object to
54014** have a read cursor.
54015**
54016** For example, before writing to any part of the table or index
54017** rooted at page iRoot, one should call:
54018**
54019**    assert( !hasReadConflicts(pBtree, iRoot) );
54020*/
54021static int hasReadConflicts(Btree *pBtree, Pgno iRoot){
54022  BtCursor *p;
54023  for(p=pBtree->pBt->pCursor; p; p=p->pNext){
54024    if( p->pgnoRoot==iRoot
54025     && p->pBtree!=pBtree
54026     && 0==(p->pBtree->db->flags & SQLITE_ReadUncommitted)
54027    ){
54028      return 1;
54029    }
54030  }
54031  return 0;
54032}
54033#endif    /* #ifdef SQLITE_DEBUG */
54034
54035/*
54036** Query to see if Btree handle p may obtain a lock of type eLock
54037** (READ_LOCK or WRITE_LOCK) on the table with root-page iTab. Return
54038** SQLITE_OK if the lock may be obtained (by calling
54039** setSharedCacheTableLock()), or SQLITE_LOCKED if not.
54040*/
54041static int querySharedCacheTableLock(Btree *p, Pgno iTab, u8 eLock){
54042  BtShared *pBt = p->pBt;
54043  BtLock *pIter;
54044
54045  assert( sqlite3BtreeHoldsMutex(p) );
54046  assert( eLock==READ_LOCK || eLock==WRITE_LOCK );
54047  assert( p->db!=0 );
54048  assert( !(p->db->flags&SQLITE_ReadUncommitted)||eLock==WRITE_LOCK||iTab==1 );
54049
54050  /* If requesting a write-lock, then the Btree must have an open write
54051  ** transaction on this file. And, obviously, for this to be so there
54052  ** must be an open write transaction on the file itself.
54053  */
54054  assert( eLock==READ_LOCK || (p==pBt->pWriter && p->inTrans==TRANS_WRITE) );
54055  assert( eLock==READ_LOCK || pBt->inTransaction==TRANS_WRITE );
54056
54057  /* This routine is a no-op if the shared-cache is not enabled */
54058  if( !p->sharable ){
54059    return SQLITE_OK;
54060  }
54061
54062  /* If some other connection is holding an exclusive lock, the
54063  ** requested lock may not be obtained.
54064  */
54065  if( pBt->pWriter!=p && (pBt->btsFlags & BTS_EXCLUSIVE)!=0 ){
54066    sqlite3ConnectionBlocked(p->db, pBt->pWriter->db);
54067    return SQLITE_LOCKED_SHAREDCACHE;
54068  }
54069
54070  for(pIter=pBt->pLock; pIter; pIter=pIter->pNext){
54071    /* The condition (pIter->eLock!=eLock) in the following if(...)
54072    ** statement is a simplification of:
54073    **
54074    **   (eLock==WRITE_LOCK || pIter->eLock==WRITE_LOCK)
54075    **
54076    ** since we know that if eLock==WRITE_LOCK, then no other connection
54077    ** may hold a WRITE_LOCK on any table in this file (since there can
54078    ** only be a single writer).
54079    */
54080    assert( pIter->eLock==READ_LOCK || pIter->eLock==WRITE_LOCK );
54081    assert( eLock==READ_LOCK || pIter->pBtree==p || pIter->eLock==READ_LOCK);
54082    if( pIter->pBtree!=p && pIter->iTable==iTab && pIter->eLock!=eLock ){
54083      sqlite3ConnectionBlocked(p->db, pIter->pBtree->db);
54084      if( eLock==WRITE_LOCK ){
54085        assert( p==pBt->pWriter );
54086        pBt->btsFlags |= BTS_PENDING;
54087      }
54088      return SQLITE_LOCKED_SHAREDCACHE;
54089    }
54090  }
54091  return SQLITE_OK;
54092}
54093#endif /* !SQLITE_OMIT_SHARED_CACHE */
54094
54095#ifndef SQLITE_OMIT_SHARED_CACHE
54096/*
54097** Add a lock on the table with root-page iTable to the shared-btree used
54098** by Btree handle p. Parameter eLock must be either READ_LOCK or
54099** WRITE_LOCK.
54100**
54101** This function assumes the following:
54102**
54103**   (a) The specified Btree object p is connected to a sharable
54104**       database (one with the BtShared.sharable flag set), and
54105**
54106**   (b) No other Btree objects hold a lock that conflicts
54107**       with the requested lock (i.e. querySharedCacheTableLock() has
54108**       already been called and returned SQLITE_OK).
54109**
54110** SQLITE_OK is returned if the lock is added successfully. SQLITE_NOMEM
54111** is returned if a malloc attempt fails.
54112*/
54113static int setSharedCacheTableLock(Btree *p, Pgno iTable, u8 eLock){
54114  BtShared *pBt = p->pBt;
54115  BtLock *pLock = 0;
54116  BtLock *pIter;
54117
54118  assert( sqlite3BtreeHoldsMutex(p) );
54119  assert( eLock==READ_LOCK || eLock==WRITE_LOCK );
54120  assert( p->db!=0 );
54121
54122  /* A connection with the read-uncommitted flag set will never try to
54123  ** obtain a read-lock using this function. The only read-lock obtained
54124  ** by a connection in read-uncommitted mode is on the sqlite_master
54125  ** table, and that lock is obtained in BtreeBeginTrans().  */
54126  assert( 0==(p->db->flags&SQLITE_ReadUncommitted) || eLock==WRITE_LOCK );
54127
54128  /* This function should only be called on a sharable b-tree after it
54129  ** has been determined that no other b-tree holds a conflicting lock.  */
54130  assert( p->sharable );
54131  assert( SQLITE_OK==querySharedCacheTableLock(p, iTable, eLock) );
54132
54133  /* First search the list for an existing lock on this table. */
54134  for(pIter=pBt->pLock; pIter; pIter=pIter->pNext){
54135    if( pIter->iTable==iTable && pIter->pBtree==p ){
54136      pLock = pIter;
54137      break;
54138    }
54139  }
54140
54141  /* If the above search did not find a BtLock struct associating Btree p
54142  ** with table iTable, allocate one and link it into the list.
54143  */
54144  if( !pLock ){
54145    pLock = (BtLock *)sqlite3MallocZero(sizeof(BtLock));
54146    if( !pLock ){
54147      return SQLITE_NOMEM;
54148    }
54149    pLock->iTable = iTable;
54150    pLock->pBtree = p;
54151    pLock->pNext = pBt->pLock;
54152    pBt->pLock = pLock;
54153  }
54154
54155  /* Set the BtLock.eLock variable to the maximum of the current lock
54156  ** and the requested lock. This means if a write-lock was already held
54157  ** and a read-lock requested, we don't incorrectly downgrade the lock.
54158  */
54159  assert( WRITE_LOCK>READ_LOCK );
54160  if( eLock>pLock->eLock ){
54161    pLock->eLock = eLock;
54162  }
54163
54164  return SQLITE_OK;
54165}
54166#endif /* !SQLITE_OMIT_SHARED_CACHE */
54167
54168#ifndef SQLITE_OMIT_SHARED_CACHE
54169/*
54170** Release all the table locks (locks obtained via calls to
54171** the setSharedCacheTableLock() procedure) held by Btree object p.
54172**
54173** This function assumes that Btree p has an open read or write
54174** transaction. If it does not, then the BTS_PENDING flag
54175** may be incorrectly cleared.
54176*/
54177static void clearAllSharedCacheTableLocks(Btree *p){
54178  BtShared *pBt = p->pBt;
54179  BtLock **ppIter = &pBt->pLock;
54180
54181  assert( sqlite3BtreeHoldsMutex(p) );
54182  assert( p->sharable || 0==*ppIter );
54183  assert( p->inTrans>0 );
54184
54185  while( *ppIter ){
54186    BtLock *pLock = *ppIter;
54187    assert( (pBt->btsFlags & BTS_EXCLUSIVE)==0 || pBt->pWriter==pLock->pBtree );
54188    assert( pLock->pBtree->inTrans>=pLock->eLock );
54189    if( pLock->pBtree==p ){
54190      *ppIter = pLock->pNext;
54191      assert( pLock->iTable!=1 || pLock==&p->lock );
54192      if( pLock->iTable!=1 ){
54193        sqlite3_free(pLock);
54194      }
54195    }else{
54196      ppIter = &pLock->pNext;
54197    }
54198  }
54199
54200  assert( (pBt->btsFlags & BTS_PENDING)==0 || pBt->pWriter );
54201  if( pBt->pWriter==p ){
54202    pBt->pWriter = 0;
54203    pBt->btsFlags &= ~(BTS_EXCLUSIVE|BTS_PENDING);
54204  }else if( pBt->nTransaction==2 ){
54205    /* This function is called when Btree p is concluding its
54206    ** transaction. If there currently exists a writer, and p is not
54207    ** that writer, then the number of locks held by connections other
54208    ** than the writer must be about to drop to zero. In this case
54209    ** set the BTS_PENDING flag to 0.
54210    **
54211    ** If there is not currently a writer, then BTS_PENDING must
54212    ** be zero already. So this next line is harmless in that case.
54213    */
54214    pBt->btsFlags &= ~BTS_PENDING;
54215  }
54216}
54217
54218/*
54219** This function changes all write-locks held by Btree p into read-locks.
54220*/
54221static void downgradeAllSharedCacheTableLocks(Btree *p){
54222  BtShared *pBt = p->pBt;
54223  if( pBt->pWriter==p ){
54224    BtLock *pLock;
54225    pBt->pWriter = 0;
54226    pBt->btsFlags &= ~(BTS_EXCLUSIVE|BTS_PENDING);
54227    for(pLock=pBt->pLock; pLock; pLock=pLock->pNext){
54228      assert( pLock->eLock==READ_LOCK || pLock->pBtree==p );
54229      pLock->eLock = READ_LOCK;
54230    }
54231  }
54232}
54233
54234#endif /* SQLITE_OMIT_SHARED_CACHE */
54235
54236static void releasePage(MemPage *pPage);  /* Forward reference */
54237
54238/*
54239***** This routine is used inside of assert() only ****
54240**
54241** Verify that the cursor holds the mutex on its BtShared
54242*/
54243#ifdef SQLITE_DEBUG
54244static int cursorHoldsMutex(BtCursor *p){
54245  return sqlite3_mutex_held(p->pBt->mutex);
54246}
54247#endif
54248
54249/*
54250** Invalidate the overflow cache of the cursor passed as the first argument.
54251** on the shared btree structure pBt.
54252*/
54253#define invalidateOverflowCache(pCur) (pCur->curFlags &= ~BTCF_ValidOvfl)
54254
54255/*
54256** Invalidate the overflow page-list cache for all cursors opened
54257** on the shared btree structure pBt.
54258*/
54259static void invalidateAllOverflowCache(BtShared *pBt){
54260  BtCursor *p;
54261  assert( sqlite3_mutex_held(pBt->mutex) );
54262  for(p=pBt->pCursor; p; p=p->pNext){
54263    invalidateOverflowCache(p);
54264  }
54265}
54266
54267#ifndef SQLITE_OMIT_INCRBLOB
54268/*
54269** This function is called before modifying the contents of a table
54270** to invalidate any incrblob cursors that are open on the
54271** row or one of the rows being modified.
54272**
54273** If argument isClearTable is true, then the entire contents of the
54274** table is about to be deleted. In this case invalidate all incrblob
54275** cursors open on any row within the table with root-page pgnoRoot.
54276**
54277** Otherwise, if argument isClearTable is false, then the row with
54278** rowid iRow is being replaced or deleted. In this case invalidate
54279** only those incrblob cursors open on that specific row.
54280*/
54281static void invalidateIncrblobCursors(
54282  Btree *pBtree,          /* The database file to check */
54283  i64 iRow,               /* The rowid that might be changing */
54284  int isClearTable        /* True if all rows are being deleted */
54285){
54286  BtCursor *p;
54287  if( pBtree->hasIncrblobCur==0 ) return;
54288  assert( sqlite3BtreeHoldsMutex(pBtree) );
54289  pBtree->hasIncrblobCur = 0;
54290  for(p=pBtree->pBt->pCursor; p; p=p->pNext){
54291    if( (p->curFlags & BTCF_Incrblob)!=0 ){
54292      pBtree->hasIncrblobCur = 1;
54293      if( isClearTable || p->info.nKey==iRow ){
54294        p->eState = CURSOR_INVALID;
54295      }
54296    }
54297  }
54298}
54299
54300#else
54301  /* Stub function when INCRBLOB is omitted */
54302  #define invalidateIncrblobCursors(x,y,z)
54303#endif /* SQLITE_OMIT_INCRBLOB */
54304
54305/*
54306** Set bit pgno of the BtShared.pHasContent bitvec. This is called
54307** when a page that previously contained data becomes a free-list leaf
54308** page.
54309**
54310** The BtShared.pHasContent bitvec exists to work around an obscure
54311** bug caused by the interaction of two useful IO optimizations surrounding
54312** free-list leaf pages:
54313**
54314**   1) When all data is deleted from a page and the page becomes
54315**      a free-list leaf page, the page is not written to the database
54316**      (as free-list leaf pages contain no meaningful data). Sometimes
54317**      such a page is not even journalled (as it will not be modified,
54318**      why bother journalling it?).
54319**
54320**   2) When a free-list leaf page is reused, its content is not read
54321**      from the database or written to the journal file (why should it
54322**      be, if it is not at all meaningful?).
54323**
54324** By themselves, these optimizations work fine and provide a handy
54325** performance boost to bulk delete or insert operations. However, if
54326** a page is moved to the free-list and then reused within the same
54327** transaction, a problem comes up. If the page is not journalled when
54328** it is moved to the free-list and it is also not journalled when it
54329** is extracted from the free-list and reused, then the original data
54330** may be lost. In the event of a rollback, it may not be possible
54331** to restore the database to its original configuration.
54332**
54333** The solution is the BtShared.pHasContent bitvec. Whenever a page is
54334** moved to become a free-list leaf page, the corresponding bit is
54335** set in the bitvec. Whenever a leaf page is extracted from the free-list,
54336** optimization 2 above is omitted if the corresponding bit is already
54337** set in BtShared.pHasContent. The contents of the bitvec are cleared
54338** at the end of every transaction.
54339*/
54340static int btreeSetHasContent(BtShared *pBt, Pgno pgno){
54341  int rc = SQLITE_OK;
54342  if( !pBt->pHasContent ){
54343    assert( pgno<=pBt->nPage );
54344    pBt->pHasContent = sqlite3BitvecCreate(pBt->nPage);
54345    if( !pBt->pHasContent ){
54346      rc = SQLITE_NOMEM;
54347    }
54348  }
54349  if( rc==SQLITE_OK && pgno<=sqlite3BitvecSize(pBt->pHasContent) ){
54350    rc = sqlite3BitvecSet(pBt->pHasContent, pgno);
54351  }
54352  return rc;
54353}
54354
54355/*
54356** Query the BtShared.pHasContent vector.
54357**
54358** This function is called when a free-list leaf page is removed from the
54359** free-list for reuse. It returns false if it is safe to retrieve the
54360** page from the pager layer with the 'no-content' flag set. True otherwise.
54361*/
54362static int btreeGetHasContent(BtShared *pBt, Pgno pgno){
54363  Bitvec *p = pBt->pHasContent;
54364  return (p && (pgno>sqlite3BitvecSize(p) || sqlite3BitvecTest(p, pgno)));
54365}
54366
54367/*
54368** Clear (destroy) the BtShared.pHasContent bitvec. This should be
54369** invoked at the conclusion of each write-transaction.
54370*/
54371static void btreeClearHasContent(BtShared *pBt){
54372  sqlite3BitvecDestroy(pBt->pHasContent);
54373  pBt->pHasContent = 0;
54374}
54375
54376/*
54377** Release all of the apPage[] pages for a cursor.
54378*/
54379static void btreeReleaseAllCursorPages(BtCursor *pCur){
54380  int i;
54381  for(i=0; i<=pCur->iPage; i++){
54382    releasePage(pCur->apPage[i]);
54383    pCur->apPage[i] = 0;
54384  }
54385  pCur->iPage = -1;
54386}
54387
54388
54389/*
54390** Save the current cursor position in the variables BtCursor.nKey
54391** and BtCursor.pKey. The cursor's state is set to CURSOR_REQUIRESEEK.
54392**
54393** The caller must ensure that the cursor is valid (has eState==CURSOR_VALID)
54394** prior to calling this routine.
54395*/
54396static int saveCursorPosition(BtCursor *pCur){
54397  int rc;
54398
54399  assert( CURSOR_VALID==pCur->eState || CURSOR_SKIPNEXT==pCur->eState );
54400  assert( 0==pCur->pKey );
54401  assert( cursorHoldsMutex(pCur) );
54402
54403  if( pCur->eState==CURSOR_SKIPNEXT ){
54404    pCur->eState = CURSOR_VALID;
54405  }else{
54406    pCur->skipNext = 0;
54407  }
54408  rc = sqlite3BtreeKeySize(pCur, &pCur->nKey);
54409  assert( rc==SQLITE_OK );  /* KeySize() cannot fail */
54410
54411  /* If this is an intKey table, then the above call to BtreeKeySize()
54412  ** stores the integer key in pCur->nKey. In this case this value is
54413  ** all that is required. Otherwise, if pCur is not open on an intKey
54414  ** table, then malloc space for and store the pCur->nKey bytes of key
54415  ** data.
54416  */
54417  if( 0==pCur->curIntKey ){
54418    void *pKey = sqlite3Malloc( pCur->nKey );
54419    if( pKey ){
54420      rc = sqlite3BtreeKey(pCur, 0, (int)pCur->nKey, pKey);
54421      if( rc==SQLITE_OK ){
54422        pCur->pKey = pKey;
54423      }else{
54424        sqlite3_free(pKey);
54425      }
54426    }else{
54427      rc = SQLITE_NOMEM;
54428    }
54429  }
54430  assert( !pCur->curIntKey || !pCur->pKey );
54431
54432  if( rc==SQLITE_OK ){
54433    btreeReleaseAllCursorPages(pCur);
54434    pCur->eState = CURSOR_REQUIRESEEK;
54435  }
54436
54437  invalidateOverflowCache(pCur);
54438  return rc;
54439}
54440
54441/* Forward reference */
54442static int SQLITE_NOINLINE saveCursorsOnList(BtCursor*,Pgno,BtCursor*);
54443
54444/*
54445** Save the positions of all cursors (except pExcept) that are open on
54446** the table with root-page iRoot.  "Saving the cursor position" means that
54447** the location in the btree is remembered in such a way that it can be
54448** moved back to the same spot after the btree has been modified.  This
54449** routine is called just before cursor pExcept is used to modify the
54450** table, for example in BtreeDelete() or BtreeInsert().
54451**
54452** If there are two or more cursors on the same btree, then all such
54453** cursors should have their BTCF_Multiple flag set.  The btreeCursor()
54454** routine enforces that rule.  This routine only needs to be called in
54455** the uncommon case when pExpect has the BTCF_Multiple flag set.
54456**
54457** If pExpect!=NULL and if no other cursors are found on the same root-page,
54458** then the BTCF_Multiple flag on pExpect is cleared, to avoid another
54459** pointless call to this routine.
54460**
54461** Implementation note:  This routine merely checks to see if any cursors
54462** need to be saved.  It calls out to saveCursorsOnList() in the (unusual)
54463** event that cursors are in need to being saved.
54464*/
54465static int saveAllCursors(BtShared *pBt, Pgno iRoot, BtCursor *pExcept){
54466  BtCursor *p;
54467  assert( sqlite3_mutex_held(pBt->mutex) );
54468  assert( pExcept==0 || pExcept->pBt==pBt );
54469  for(p=pBt->pCursor; p; p=p->pNext){
54470    if( p!=pExcept && (0==iRoot || p->pgnoRoot==iRoot) ) break;
54471  }
54472  if( p ) return saveCursorsOnList(p, iRoot, pExcept);
54473  if( pExcept ) pExcept->curFlags &= ~BTCF_Multiple;
54474  return SQLITE_OK;
54475}
54476
54477/* This helper routine to saveAllCursors does the actual work of saving
54478** the cursors if and when a cursor is found that actually requires saving.
54479** The common case is that no cursors need to be saved, so this routine is
54480** broken out from its caller to avoid unnecessary stack pointer movement.
54481*/
54482static int SQLITE_NOINLINE saveCursorsOnList(
54483  BtCursor *p,         /* The first cursor that needs saving */
54484  Pgno iRoot,          /* Only save cursor with this iRoot. Save all if zero */
54485  BtCursor *pExcept    /* Do not save this cursor */
54486){
54487  do{
54488    if( p!=pExcept && (0==iRoot || p->pgnoRoot==iRoot) ){
54489      if( p->eState==CURSOR_VALID || p->eState==CURSOR_SKIPNEXT ){
54490        int rc = saveCursorPosition(p);
54491        if( SQLITE_OK!=rc ){
54492          return rc;
54493        }
54494      }else{
54495        testcase( p->iPage>0 );
54496        btreeReleaseAllCursorPages(p);
54497      }
54498    }
54499    p = p->pNext;
54500  }while( p );
54501  return SQLITE_OK;
54502}
54503
54504/*
54505** Clear the current cursor position.
54506*/
54507SQLITE_PRIVATE void sqlite3BtreeClearCursor(BtCursor *pCur){
54508  assert( cursorHoldsMutex(pCur) );
54509  sqlite3_free(pCur->pKey);
54510  pCur->pKey = 0;
54511  pCur->eState = CURSOR_INVALID;
54512}
54513
54514/*
54515** In this version of BtreeMoveto, pKey is a packed index record
54516** such as is generated by the OP_MakeRecord opcode.  Unpack the
54517** record and then call BtreeMovetoUnpacked() to do the work.
54518*/
54519static int btreeMoveto(
54520  BtCursor *pCur,     /* Cursor open on the btree to be searched */
54521  const void *pKey,   /* Packed key if the btree is an index */
54522  i64 nKey,           /* Integer key for tables.  Size of pKey for indices */
54523  int bias,           /* Bias search to the high end */
54524  int *pRes           /* Write search results here */
54525){
54526  int rc;                    /* Status code */
54527  UnpackedRecord *pIdxKey;   /* Unpacked index key */
54528  char aSpace[200];          /* Temp space for pIdxKey - to avoid a malloc */
54529  char *pFree = 0;
54530
54531  if( pKey ){
54532    assert( nKey==(i64)(int)nKey );
54533    pIdxKey = sqlite3VdbeAllocUnpackedRecord(
54534        pCur->pKeyInfo, aSpace, sizeof(aSpace), &pFree
54535    );
54536    if( pIdxKey==0 ) return SQLITE_NOMEM;
54537    sqlite3VdbeRecordUnpack(pCur->pKeyInfo, (int)nKey, pKey, pIdxKey);
54538    if( pIdxKey->nField==0 ){
54539      sqlite3DbFree(pCur->pKeyInfo->db, pFree);
54540      return SQLITE_CORRUPT_BKPT;
54541    }
54542  }else{
54543    pIdxKey = 0;
54544  }
54545  rc = sqlite3BtreeMovetoUnpacked(pCur, pIdxKey, nKey, bias, pRes);
54546  if( pFree ){
54547    sqlite3DbFree(pCur->pKeyInfo->db, pFree);
54548  }
54549  return rc;
54550}
54551
54552/*
54553** Restore the cursor to the position it was in (or as close to as possible)
54554** when saveCursorPosition() was called. Note that this call deletes the
54555** saved position info stored by saveCursorPosition(), so there can be
54556** at most one effective restoreCursorPosition() call after each
54557** saveCursorPosition().
54558*/
54559static int btreeRestoreCursorPosition(BtCursor *pCur){
54560  int rc;
54561  int skipNext;
54562  assert( cursorHoldsMutex(pCur) );
54563  assert( pCur->eState>=CURSOR_REQUIRESEEK );
54564  if( pCur->eState==CURSOR_FAULT ){
54565    return pCur->skipNext;
54566  }
54567  pCur->eState = CURSOR_INVALID;
54568  rc = btreeMoveto(pCur, pCur->pKey, pCur->nKey, 0, &skipNext);
54569  if( rc==SQLITE_OK ){
54570    sqlite3_free(pCur->pKey);
54571    pCur->pKey = 0;
54572    assert( pCur->eState==CURSOR_VALID || pCur->eState==CURSOR_INVALID );
54573    pCur->skipNext |= skipNext;
54574    if( pCur->skipNext && pCur->eState==CURSOR_VALID ){
54575      pCur->eState = CURSOR_SKIPNEXT;
54576    }
54577  }
54578  return rc;
54579}
54580
54581#define restoreCursorPosition(p) \
54582  (p->eState>=CURSOR_REQUIRESEEK ? \
54583         btreeRestoreCursorPosition(p) : \
54584         SQLITE_OK)
54585
54586/*
54587** Determine whether or not a cursor has moved from the position where
54588** it was last placed, or has been invalidated for any other reason.
54589** Cursors can move when the row they are pointing at is deleted out
54590** from under them, for example.  Cursor might also move if a btree
54591** is rebalanced.
54592**
54593** Calling this routine with a NULL cursor pointer returns false.
54594**
54595** Use the separate sqlite3BtreeCursorRestore() routine to restore a cursor
54596** back to where it ought to be if this routine returns true.
54597*/
54598SQLITE_PRIVATE int sqlite3BtreeCursorHasMoved(BtCursor *pCur){
54599  return pCur->eState!=CURSOR_VALID;
54600}
54601
54602/*
54603** This routine restores a cursor back to its original position after it
54604** has been moved by some outside activity (such as a btree rebalance or
54605** a row having been deleted out from under the cursor).
54606**
54607** On success, the *pDifferentRow parameter is false if the cursor is left
54608** pointing at exactly the same row.  *pDifferntRow is the row the cursor
54609** was pointing to has been deleted, forcing the cursor to point to some
54610** nearby row.
54611**
54612** This routine should only be called for a cursor that just returned
54613** TRUE from sqlite3BtreeCursorHasMoved().
54614*/
54615SQLITE_PRIVATE int sqlite3BtreeCursorRestore(BtCursor *pCur, int *pDifferentRow){
54616  int rc;
54617
54618  assert( pCur!=0 );
54619  assert( pCur->eState!=CURSOR_VALID );
54620  rc = restoreCursorPosition(pCur);
54621  if( rc ){
54622    *pDifferentRow = 1;
54623    return rc;
54624  }
54625  if( pCur->eState!=CURSOR_VALID ){
54626    *pDifferentRow = 1;
54627  }else{
54628    assert( pCur->skipNext==0 );
54629    *pDifferentRow = 0;
54630  }
54631  return SQLITE_OK;
54632}
54633
54634#ifndef SQLITE_OMIT_AUTOVACUUM
54635/*
54636** Given a page number of a regular database page, return the page
54637** number for the pointer-map page that contains the entry for the
54638** input page number.
54639**
54640** Return 0 (not a valid page) for pgno==1 since there is
54641** no pointer map associated with page 1.  The integrity_check logic
54642** requires that ptrmapPageno(*,1)!=1.
54643*/
54644static Pgno ptrmapPageno(BtShared *pBt, Pgno pgno){
54645  int nPagesPerMapPage;
54646  Pgno iPtrMap, ret;
54647  assert( sqlite3_mutex_held(pBt->mutex) );
54648  if( pgno<2 ) return 0;
54649  nPagesPerMapPage = (pBt->usableSize/5)+1;
54650  iPtrMap = (pgno-2)/nPagesPerMapPage;
54651  ret = (iPtrMap*nPagesPerMapPage) + 2;
54652  if( ret==PENDING_BYTE_PAGE(pBt) ){
54653    ret++;
54654  }
54655  return ret;
54656}
54657
54658/*
54659** Write an entry into the pointer map.
54660**
54661** This routine updates the pointer map entry for page number 'key'
54662** so that it maps to type 'eType' and parent page number 'pgno'.
54663**
54664** If *pRC is initially non-zero (non-SQLITE_OK) then this routine is
54665** a no-op.  If an error occurs, the appropriate error code is written
54666** into *pRC.
54667*/
54668static void ptrmapPut(BtShared *pBt, Pgno key, u8 eType, Pgno parent, int *pRC){
54669  DbPage *pDbPage;  /* The pointer map page */
54670  u8 *pPtrmap;      /* The pointer map data */
54671  Pgno iPtrmap;     /* The pointer map page number */
54672  int offset;       /* Offset in pointer map page */
54673  int rc;           /* Return code from subfunctions */
54674
54675  if( *pRC ) return;
54676
54677  assert( sqlite3_mutex_held(pBt->mutex) );
54678  /* The master-journal page number must never be used as a pointer map page */
54679  assert( 0==PTRMAP_ISPAGE(pBt, PENDING_BYTE_PAGE(pBt)) );
54680
54681  assert( pBt->autoVacuum );
54682  if( key==0 ){
54683    *pRC = SQLITE_CORRUPT_BKPT;
54684    return;
54685  }
54686  iPtrmap = PTRMAP_PAGENO(pBt, key);
54687  rc = sqlite3PagerGet(pBt->pPager, iPtrmap, &pDbPage);
54688  if( rc!=SQLITE_OK ){
54689    *pRC = rc;
54690    return;
54691  }
54692  offset = PTRMAP_PTROFFSET(iPtrmap, key);
54693  if( offset<0 ){
54694    *pRC = SQLITE_CORRUPT_BKPT;
54695    goto ptrmap_exit;
54696  }
54697  assert( offset <= (int)pBt->usableSize-5 );
54698  pPtrmap = (u8 *)sqlite3PagerGetData(pDbPage);
54699
54700  if( eType!=pPtrmap[offset] || get4byte(&pPtrmap[offset+1])!=parent ){
54701    TRACE(("PTRMAP_UPDATE: %d->(%d,%d)\n", key, eType, parent));
54702    *pRC= rc = sqlite3PagerWrite(pDbPage);
54703    if( rc==SQLITE_OK ){
54704      pPtrmap[offset] = eType;
54705      put4byte(&pPtrmap[offset+1], parent);
54706    }
54707  }
54708
54709ptrmap_exit:
54710  sqlite3PagerUnref(pDbPage);
54711}
54712
54713/*
54714** Read an entry from the pointer map.
54715**
54716** This routine retrieves the pointer map entry for page 'key', writing
54717** the type and parent page number to *pEType and *pPgno respectively.
54718** An error code is returned if something goes wrong, otherwise SQLITE_OK.
54719*/
54720static int ptrmapGet(BtShared *pBt, Pgno key, u8 *pEType, Pgno *pPgno){
54721  DbPage *pDbPage;   /* The pointer map page */
54722  int iPtrmap;       /* Pointer map page index */
54723  u8 *pPtrmap;       /* Pointer map page data */
54724  int offset;        /* Offset of entry in pointer map */
54725  int rc;
54726
54727  assert( sqlite3_mutex_held(pBt->mutex) );
54728
54729  iPtrmap = PTRMAP_PAGENO(pBt, key);
54730  rc = sqlite3PagerGet(pBt->pPager, iPtrmap, &pDbPage);
54731  if( rc!=0 ){
54732    return rc;
54733  }
54734  pPtrmap = (u8 *)sqlite3PagerGetData(pDbPage);
54735
54736  offset = PTRMAP_PTROFFSET(iPtrmap, key);
54737  if( offset<0 ){
54738    sqlite3PagerUnref(pDbPage);
54739    return SQLITE_CORRUPT_BKPT;
54740  }
54741  assert( offset <= (int)pBt->usableSize-5 );
54742  assert( pEType!=0 );
54743  *pEType = pPtrmap[offset];
54744  if( pPgno ) *pPgno = get4byte(&pPtrmap[offset+1]);
54745
54746  sqlite3PagerUnref(pDbPage);
54747  if( *pEType<1 || *pEType>5 ) return SQLITE_CORRUPT_BKPT;
54748  return SQLITE_OK;
54749}
54750
54751#else /* if defined SQLITE_OMIT_AUTOVACUUM */
54752  #define ptrmapPut(w,x,y,z,rc)
54753  #define ptrmapGet(w,x,y,z) SQLITE_OK
54754  #define ptrmapPutOvflPtr(x, y, rc)
54755#endif
54756
54757/*
54758** Given a btree page and a cell index (0 means the first cell on
54759** the page, 1 means the second cell, and so forth) return a pointer
54760** to the cell content.
54761**
54762** findCellPastPtr() does the same except it skips past the initial
54763** 4-byte child pointer found on interior pages, if there is one.
54764**
54765** This routine works only for pages that do not contain overflow cells.
54766*/
54767#define findCell(P,I) \
54768  ((P)->aData + ((P)->maskPage & get2byteAligned(&(P)->aCellIdx[2*(I)])))
54769#define findCellPastPtr(P,I) \
54770  ((P)->aDataOfst + ((P)->maskPage & get2byteAligned(&(P)->aCellIdx[2*(I)])))
54771
54772
54773/*
54774** This is common tail processing for btreeParseCellPtr() and
54775** btreeParseCellPtrIndex() for the case when the cell does not fit entirely
54776** on a single B-tree page.  Make necessary adjustments to the CellInfo
54777** structure.
54778*/
54779static SQLITE_NOINLINE void btreeParseCellAdjustSizeForOverflow(
54780  MemPage *pPage,         /* Page containing the cell */
54781  u8 *pCell,              /* Pointer to the cell text. */
54782  CellInfo *pInfo         /* Fill in this structure */
54783){
54784  /* If the payload will not fit completely on the local page, we have
54785  ** to decide how much to store locally and how much to spill onto
54786  ** overflow pages.  The strategy is to minimize the amount of unused
54787  ** space on overflow pages while keeping the amount of local storage
54788  ** in between minLocal and maxLocal.
54789  **
54790  ** Warning:  changing the way overflow payload is distributed in any
54791  ** way will result in an incompatible file format.
54792  */
54793  int minLocal;  /* Minimum amount of payload held locally */
54794  int maxLocal;  /* Maximum amount of payload held locally */
54795  int surplus;   /* Overflow payload available for local storage */
54796
54797  minLocal = pPage->minLocal;
54798  maxLocal = pPage->maxLocal;
54799  surplus = minLocal + (pInfo->nPayload - minLocal)%(pPage->pBt->usableSize-4);
54800  testcase( surplus==maxLocal );
54801  testcase( surplus==maxLocal+1 );
54802  if( surplus <= maxLocal ){
54803    pInfo->nLocal = (u16)surplus;
54804  }else{
54805    pInfo->nLocal = (u16)minLocal;
54806  }
54807  pInfo->iOverflow = (u16)(&pInfo->pPayload[pInfo->nLocal] - pCell);
54808  pInfo->nSize = pInfo->iOverflow + 4;
54809}
54810
54811/*
54812** The following routines are implementations of the MemPage.xParseCell()
54813** method.
54814**
54815** Parse a cell content block and fill in the CellInfo structure.
54816**
54817** btreeParseCellPtr()        =>   table btree leaf nodes
54818** btreeParseCellNoPayload()  =>   table btree internal nodes
54819** btreeParseCellPtrIndex()   =>   index btree nodes
54820**
54821** There is also a wrapper function btreeParseCell() that works for
54822** all MemPage types and that references the cell by index rather than
54823** by pointer.
54824*/
54825static void btreeParseCellPtrNoPayload(
54826  MemPage *pPage,         /* Page containing the cell */
54827  u8 *pCell,              /* Pointer to the cell text. */
54828  CellInfo *pInfo         /* Fill in this structure */
54829){
54830  assert( sqlite3_mutex_held(pPage->pBt->mutex) );
54831  assert( pPage->leaf==0 );
54832  assert( pPage->noPayload );
54833  assert( pPage->childPtrSize==4 );
54834#ifndef SQLITE_DEBUG
54835  UNUSED_PARAMETER(pPage);
54836#endif
54837  pInfo->nSize = 4 + getVarint(&pCell[4], (u64*)&pInfo->nKey);
54838  pInfo->nPayload = 0;
54839  pInfo->nLocal = 0;
54840  pInfo->iOverflow = 0;
54841  pInfo->pPayload = 0;
54842  return;
54843}
54844static void btreeParseCellPtr(
54845  MemPage *pPage,         /* Page containing the cell */
54846  u8 *pCell,              /* Pointer to the cell text. */
54847  CellInfo *pInfo         /* Fill in this structure */
54848){
54849  u8 *pIter;              /* For scanning through pCell */
54850  u32 nPayload;           /* Number of bytes of cell payload */
54851  u64 iKey;               /* Extracted Key value */
54852
54853  assert( sqlite3_mutex_held(pPage->pBt->mutex) );
54854  assert( pPage->leaf==0 || pPage->leaf==1 );
54855  assert( pPage->intKeyLeaf || pPage->noPayload );
54856  assert( pPage->noPayload==0 );
54857  assert( pPage->intKeyLeaf );
54858  assert( pPage->childPtrSize==0 );
54859  pIter = pCell;
54860
54861  /* The next block of code is equivalent to:
54862  **
54863  **     pIter += getVarint32(pIter, nPayload);
54864  **
54865  ** The code is inlined to avoid a function call.
54866  */
54867  nPayload = *pIter;
54868  if( nPayload>=0x80 ){
54869    u8 *pEnd = &pIter[8];
54870    nPayload &= 0x7f;
54871    do{
54872      nPayload = (nPayload<<7) | (*++pIter & 0x7f);
54873    }while( (*pIter)>=0x80 && pIter<pEnd );
54874  }
54875  pIter++;
54876
54877  /* The next block of code is equivalent to:
54878  **
54879  **     pIter += getVarint(pIter, (u64*)&pInfo->nKey);
54880  **
54881  ** The code is inlined to avoid a function call.
54882  */
54883  iKey = *pIter;
54884  if( iKey>=0x80 ){
54885    u8 *pEnd = &pIter[7];
54886    iKey &= 0x7f;
54887    while(1){
54888      iKey = (iKey<<7) | (*++pIter & 0x7f);
54889      if( (*pIter)<0x80 ) break;
54890      if( pIter>=pEnd ){
54891        iKey = (iKey<<8) | *++pIter;
54892        break;
54893      }
54894    }
54895  }
54896  pIter++;
54897
54898  pInfo->nKey = *(i64*)&iKey;
54899  pInfo->nPayload = nPayload;
54900  pInfo->pPayload = pIter;
54901  testcase( nPayload==pPage->maxLocal );
54902  testcase( nPayload==pPage->maxLocal+1 );
54903  if( nPayload<=pPage->maxLocal ){
54904    /* This is the (easy) common case where the entire payload fits
54905    ** on the local page.  No overflow is required.
54906    */
54907    pInfo->nSize = nPayload + (u16)(pIter - pCell);
54908    if( pInfo->nSize<4 ) pInfo->nSize = 4;
54909    pInfo->nLocal = (u16)nPayload;
54910    pInfo->iOverflow = 0;
54911  }else{
54912    btreeParseCellAdjustSizeForOverflow(pPage, pCell, pInfo);
54913  }
54914}
54915static void btreeParseCellPtrIndex(
54916  MemPage *pPage,         /* Page containing the cell */
54917  u8 *pCell,              /* Pointer to the cell text. */
54918  CellInfo *pInfo         /* Fill in this structure */
54919){
54920  u8 *pIter;              /* For scanning through pCell */
54921  u32 nPayload;           /* Number of bytes of cell payload */
54922
54923  assert( sqlite3_mutex_held(pPage->pBt->mutex) );
54924  assert( pPage->leaf==0 || pPage->leaf==1 );
54925  assert( pPage->intKeyLeaf==0 );
54926  assert( pPage->noPayload==0 );
54927  pIter = pCell + pPage->childPtrSize;
54928  nPayload = *pIter;
54929  if( nPayload>=0x80 ){
54930    u8 *pEnd = &pIter[8];
54931    nPayload &= 0x7f;
54932    do{
54933      nPayload = (nPayload<<7) | (*++pIter & 0x7f);
54934    }while( *(pIter)>=0x80 && pIter<pEnd );
54935  }
54936  pIter++;
54937  pInfo->nKey = nPayload;
54938  pInfo->nPayload = nPayload;
54939  pInfo->pPayload = pIter;
54940  testcase( nPayload==pPage->maxLocal );
54941  testcase( nPayload==pPage->maxLocal+1 );
54942  if( nPayload<=pPage->maxLocal ){
54943    /* This is the (easy) common case where the entire payload fits
54944    ** on the local page.  No overflow is required.
54945    */
54946    pInfo->nSize = nPayload + (u16)(pIter - pCell);
54947    if( pInfo->nSize<4 ) pInfo->nSize = 4;
54948    pInfo->nLocal = (u16)nPayload;
54949    pInfo->iOverflow = 0;
54950  }else{
54951    btreeParseCellAdjustSizeForOverflow(pPage, pCell, pInfo);
54952  }
54953}
54954static void btreeParseCell(
54955  MemPage *pPage,         /* Page containing the cell */
54956  int iCell,              /* The cell index.  First cell is 0 */
54957  CellInfo *pInfo         /* Fill in this structure */
54958){
54959  pPage->xParseCell(pPage, findCell(pPage, iCell), pInfo);
54960}
54961
54962/*
54963** The following routines are implementations of the MemPage.xCellSize
54964** method.
54965**
54966** Compute the total number of bytes that a Cell needs in the cell
54967** data area of the btree-page.  The return number includes the cell
54968** data header and the local payload, but not any overflow page or
54969** the space used by the cell pointer.
54970**
54971** cellSizePtrNoPayload()    =>   table internal nodes
54972** cellSizePtr()             =>   all index nodes & table leaf nodes
54973*/
54974static u16 cellSizePtr(MemPage *pPage, u8 *pCell){
54975  u8 *pIter = pCell + pPage->childPtrSize; /* For looping over bytes of pCell */
54976  u8 *pEnd;                                /* End mark for a varint */
54977  u32 nSize;                               /* Size value to return */
54978
54979#ifdef SQLITE_DEBUG
54980  /* The value returned by this function should always be the same as
54981  ** the (CellInfo.nSize) value found by doing a full parse of the
54982  ** cell. If SQLITE_DEBUG is defined, an assert() at the bottom of
54983  ** this function verifies that this invariant is not violated. */
54984  CellInfo debuginfo;
54985  pPage->xParseCell(pPage, pCell, &debuginfo);
54986#endif
54987
54988  assert( pPage->noPayload==0 );
54989  nSize = *pIter;
54990  if( nSize>=0x80 ){
54991    pEnd = &pIter[8];
54992    nSize &= 0x7f;
54993    do{
54994      nSize = (nSize<<7) | (*++pIter & 0x7f);
54995    }while( *(pIter)>=0x80 && pIter<pEnd );
54996  }
54997  pIter++;
54998  if( pPage->intKey ){
54999    /* pIter now points at the 64-bit integer key value, a variable length
55000    ** integer. The following block moves pIter to point at the first byte
55001    ** past the end of the key value. */
55002    pEnd = &pIter[9];
55003    while( (*pIter++)&0x80 && pIter<pEnd );
55004  }
55005  testcase( nSize==pPage->maxLocal );
55006  testcase( nSize==pPage->maxLocal+1 );
55007  if( nSize<=pPage->maxLocal ){
55008    nSize += (u32)(pIter - pCell);
55009    if( nSize<4 ) nSize = 4;
55010  }else{
55011    int minLocal = pPage->minLocal;
55012    nSize = minLocal + (nSize - minLocal) % (pPage->pBt->usableSize - 4);
55013    testcase( nSize==pPage->maxLocal );
55014    testcase( nSize==pPage->maxLocal+1 );
55015    if( nSize>pPage->maxLocal ){
55016      nSize = minLocal;
55017    }
55018    nSize += 4 + (u16)(pIter - pCell);
55019  }
55020  assert( nSize==debuginfo.nSize || CORRUPT_DB );
55021  return (u16)nSize;
55022}
55023static u16 cellSizePtrNoPayload(MemPage *pPage, u8 *pCell){
55024  u8 *pIter = pCell + 4; /* For looping over bytes of pCell */
55025  u8 *pEnd;              /* End mark for a varint */
55026
55027#ifdef SQLITE_DEBUG
55028  /* The value returned by this function should always be the same as
55029  ** the (CellInfo.nSize) value found by doing a full parse of the
55030  ** cell. If SQLITE_DEBUG is defined, an assert() at the bottom of
55031  ** this function verifies that this invariant is not violated. */
55032  CellInfo debuginfo;
55033  pPage->xParseCell(pPage, pCell, &debuginfo);
55034#else
55035  UNUSED_PARAMETER(pPage);
55036#endif
55037
55038  assert( pPage->childPtrSize==4 );
55039  pEnd = pIter + 9;
55040  while( (*pIter++)&0x80 && pIter<pEnd );
55041  assert( debuginfo.nSize==(u16)(pIter - pCell) || CORRUPT_DB );
55042  return (u16)(pIter - pCell);
55043}
55044
55045
55046#ifdef SQLITE_DEBUG
55047/* This variation on cellSizePtr() is used inside of assert() statements
55048** only. */
55049static u16 cellSize(MemPage *pPage, int iCell){
55050  return pPage->xCellSize(pPage, findCell(pPage, iCell));
55051}
55052#endif
55053
55054#ifndef SQLITE_OMIT_AUTOVACUUM
55055/*
55056** If the cell pCell, part of page pPage contains a pointer
55057** to an overflow page, insert an entry into the pointer-map
55058** for the overflow page.
55059*/
55060static void ptrmapPutOvflPtr(MemPage *pPage, u8 *pCell, int *pRC){
55061  CellInfo info;
55062  if( *pRC ) return;
55063  assert( pCell!=0 );
55064  pPage->xParseCell(pPage, pCell, &info);
55065  if( info.iOverflow ){
55066    Pgno ovfl = get4byte(&pCell[info.iOverflow]);
55067    ptrmapPut(pPage->pBt, ovfl, PTRMAP_OVERFLOW1, pPage->pgno, pRC);
55068  }
55069}
55070#endif
55071
55072
55073/*
55074** Defragment the page given.  All Cells are moved to the
55075** end of the page and all free space is collected into one
55076** big FreeBlk that occurs in between the header and cell
55077** pointer array and the cell content area.
55078**
55079** EVIDENCE-OF: R-44582-60138 SQLite may from time to time reorganize a
55080** b-tree page so that there are no freeblocks or fragment bytes, all
55081** unused bytes are contained in the unallocated space region, and all
55082** cells are packed tightly at the end of the page.
55083*/
55084static int defragmentPage(MemPage *pPage){
55085  int i;                     /* Loop counter */
55086  int pc;                    /* Address of the i-th cell */
55087  int hdr;                   /* Offset to the page header */
55088  int size;                  /* Size of a cell */
55089  int usableSize;            /* Number of usable bytes on a page */
55090  int cellOffset;            /* Offset to the cell pointer array */
55091  int cbrk;                  /* Offset to the cell content area */
55092  int nCell;                 /* Number of cells on the page */
55093  unsigned char *data;       /* The page data */
55094  unsigned char *temp;       /* Temp area for cell content */
55095  unsigned char *src;        /* Source of content */
55096  int iCellFirst;            /* First allowable cell index */
55097  int iCellLast;             /* Last possible cell index */
55098
55099
55100  assert( sqlite3PagerIswriteable(pPage->pDbPage) );
55101  assert( pPage->pBt!=0 );
55102  assert( pPage->pBt->usableSize <= SQLITE_MAX_PAGE_SIZE );
55103  assert( pPage->nOverflow==0 );
55104  assert( sqlite3_mutex_held(pPage->pBt->mutex) );
55105  temp = 0;
55106  src = data = pPage->aData;
55107  hdr = pPage->hdrOffset;
55108  cellOffset = pPage->cellOffset;
55109  nCell = pPage->nCell;
55110  assert( nCell==get2byte(&data[hdr+3]) );
55111  usableSize = pPage->pBt->usableSize;
55112  cbrk = usableSize;
55113  iCellFirst = cellOffset + 2*nCell;
55114  iCellLast = usableSize - 4;
55115  for(i=0; i<nCell; i++){
55116    u8 *pAddr;     /* The i-th cell pointer */
55117    pAddr = &data[cellOffset + i*2];
55118    pc = get2byte(pAddr);
55119    testcase( pc==iCellFirst );
55120    testcase( pc==iCellLast );
55121    /* These conditions have already been verified in btreeInitPage()
55122    ** if PRAGMA cell_size_check=ON.
55123    */
55124    if( pc<iCellFirst || pc>iCellLast ){
55125      return SQLITE_CORRUPT_BKPT;
55126    }
55127    assert( pc>=iCellFirst && pc<=iCellLast );
55128    size = pPage->xCellSize(pPage, &src[pc]);
55129    cbrk -= size;
55130    if( cbrk<iCellFirst || pc+size>usableSize ){
55131      return SQLITE_CORRUPT_BKPT;
55132    }
55133    assert( cbrk+size<=usableSize && cbrk>=iCellFirst );
55134    testcase( cbrk+size==usableSize );
55135    testcase( pc+size==usableSize );
55136    put2byte(pAddr, cbrk);
55137    if( temp==0 ){
55138      int x;
55139      if( cbrk==pc ) continue;
55140      temp = sqlite3PagerTempSpace(pPage->pBt->pPager);
55141      x = get2byte(&data[hdr+5]);
55142      memcpy(&temp[x], &data[x], (cbrk+size) - x);
55143      src = temp;
55144    }
55145    memcpy(&data[cbrk], &src[pc], size);
55146  }
55147  assert( cbrk>=iCellFirst );
55148  put2byte(&data[hdr+5], cbrk);
55149  data[hdr+1] = 0;
55150  data[hdr+2] = 0;
55151  data[hdr+7] = 0;
55152  memset(&data[iCellFirst], 0, cbrk-iCellFirst);
55153  assert( sqlite3PagerIswriteable(pPage->pDbPage) );
55154  if( cbrk-iCellFirst!=pPage->nFree ){
55155    return SQLITE_CORRUPT_BKPT;
55156  }
55157  return SQLITE_OK;
55158}
55159
55160/*
55161** Search the free-list on page pPg for space to store a cell nByte bytes in
55162** size. If one can be found, return a pointer to the space and remove it
55163** from the free-list.
55164**
55165** If no suitable space can be found on the free-list, return NULL.
55166**
55167** This function may detect corruption within pPg.  If corruption is
55168** detected then *pRc is set to SQLITE_CORRUPT and NULL is returned.
55169**
55170** Slots on the free list that are between 1 and 3 bytes larger than nByte
55171** will be ignored if adding the extra space to the fragmentation count
55172** causes the fragmentation count to exceed 60.
55173*/
55174static u8 *pageFindSlot(MemPage *pPg, int nByte, int *pRc){
55175  const int hdr = pPg->hdrOffset;
55176  u8 * const aData = pPg->aData;
55177  int iAddr = hdr + 1;
55178  int pc = get2byte(&aData[iAddr]);
55179  int x;
55180  int usableSize = pPg->pBt->usableSize;
55181
55182  assert( pc>0 );
55183  do{
55184    int size;            /* Size of the free slot */
55185    /* EVIDENCE-OF: R-06866-39125 Freeblocks are always connected in order of
55186    ** increasing offset. */
55187    if( pc>usableSize-4 || pc<iAddr+4 ){
55188      *pRc = SQLITE_CORRUPT_BKPT;
55189      return 0;
55190    }
55191    /* EVIDENCE-OF: R-22710-53328 The third and fourth bytes of each
55192    ** freeblock form a big-endian integer which is the size of the freeblock
55193    ** in bytes, including the 4-byte header. */
55194    size = get2byte(&aData[pc+2]);
55195    if( (x = size - nByte)>=0 ){
55196      testcase( x==4 );
55197      testcase( x==3 );
55198      if( pc < pPg->cellOffset+2*pPg->nCell || size+pc > usableSize ){
55199        *pRc = SQLITE_CORRUPT_BKPT;
55200        return 0;
55201      }else if( x<4 ){
55202        /* EVIDENCE-OF: R-11498-58022 In a well-formed b-tree page, the total
55203        ** number of bytes in fragments may not exceed 60. */
55204        if( aData[hdr+7]>57 ) return 0;
55205
55206        /* Remove the slot from the free-list. Update the number of
55207        ** fragmented bytes within the page. */
55208        memcpy(&aData[iAddr], &aData[pc], 2);
55209        aData[hdr+7] += (u8)x;
55210      }else{
55211        /* The slot remains on the free-list. Reduce its size to account
55212         ** for the portion used by the new allocation. */
55213        put2byte(&aData[pc+2], x);
55214      }
55215      return &aData[pc + x];
55216    }
55217    iAddr = pc;
55218    pc = get2byte(&aData[pc]);
55219  }while( pc );
55220
55221  return 0;
55222}
55223
55224/*
55225** Allocate nByte bytes of space from within the B-Tree page passed
55226** as the first argument. Write into *pIdx the index into pPage->aData[]
55227** of the first byte of allocated space. Return either SQLITE_OK or
55228** an error code (usually SQLITE_CORRUPT).
55229**
55230** The caller guarantees that there is sufficient space to make the
55231** allocation.  This routine might need to defragment in order to bring
55232** all the space together, however.  This routine will avoid using
55233** the first two bytes past the cell pointer area since presumably this
55234** allocation is being made in order to insert a new cell, so we will
55235** also end up needing a new cell pointer.
55236*/
55237static int allocateSpace(MemPage *pPage, int nByte, int *pIdx){
55238  const int hdr = pPage->hdrOffset;    /* Local cache of pPage->hdrOffset */
55239  u8 * const data = pPage->aData;      /* Local cache of pPage->aData */
55240  int top;                             /* First byte of cell content area */
55241  int rc = SQLITE_OK;                  /* Integer return code */
55242  int gap;        /* First byte of gap between cell pointers and cell content */
55243
55244  assert( sqlite3PagerIswriteable(pPage->pDbPage) );
55245  assert( pPage->pBt );
55246  assert( sqlite3_mutex_held(pPage->pBt->mutex) );
55247  assert( nByte>=0 );  /* Minimum cell size is 4 */
55248  assert( pPage->nFree>=nByte );
55249  assert( pPage->nOverflow==0 );
55250  assert( nByte < (int)(pPage->pBt->usableSize-8) );
55251
55252  assert( pPage->cellOffset == hdr + 12 - 4*pPage->leaf );
55253  gap = pPage->cellOffset + 2*pPage->nCell;
55254  assert( gap<=65536 );
55255  /* EVIDENCE-OF: R-29356-02391 If the database uses a 65536-byte page size
55256  ** and the reserved space is zero (the usual value for reserved space)
55257  ** then the cell content offset of an empty page wants to be 65536.
55258  ** However, that integer is too large to be stored in a 2-byte unsigned
55259  ** integer, so a value of 0 is used in its place. */
55260  top = get2byte(&data[hdr+5]);
55261  assert( top<=(int)pPage->pBt->usableSize ); /* Prevent by getAndInitPage() */
55262  if( gap>top ){
55263    if( top==0 && pPage->pBt->usableSize==65536 ){
55264      top = 65536;
55265    }else{
55266      return SQLITE_CORRUPT_BKPT;
55267    }
55268  }
55269
55270  /* If there is enough space between gap and top for one more cell pointer
55271  ** array entry offset, and if the freelist is not empty, then search the
55272  ** freelist looking for a free slot big enough to satisfy the request.
55273  */
55274  testcase( gap+2==top );
55275  testcase( gap+1==top );
55276  testcase( gap==top );
55277  if( (data[hdr+2] || data[hdr+1]) && gap+2<=top ){
55278    u8 *pSpace = pageFindSlot(pPage, nByte, &rc);
55279    if( pSpace ){
55280      assert( pSpace>=data && (pSpace - data)<65536 );
55281      *pIdx = (int)(pSpace - data);
55282      return SQLITE_OK;
55283    }else if( rc ){
55284      return rc;
55285    }
55286  }
55287
55288  /* The request could not be fulfilled using a freelist slot.  Check
55289  ** to see if defragmentation is necessary.
55290  */
55291  testcase( gap+2+nByte==top );
55292  if( gap+2+nByte>top ){
55293    assert( pPage->nCell>0 || CORRUPT_DB );
55294    rc = defragmentPage(pPage);
55295    if( rc ) return rc;
55296    top = get2byteNotZero(&data[hdr+5]);
55297    assert( gap+nByte<=top );
55298  }
55299
55300
55301  /* Allocate memory from the gap in between the cell pointer array
55302  ** and the cell content area.  The btreeInitPage() call has already
55303  ** validated the freelist.  Given that the freelist is valid, there
55304  ** is no way that the allocation can extend off the end of the page.
55305  ** The assert() below verifies the previous sentence.
55306  */
55307  top -= nByte;
55308  put2byte(&data[hdr+5], top);
55309  assert( top+nByte <= (int)pPage->pBt->usableSize );
55310  *pIdx = top;
55311  return SQLITE_OK;
55312}
55313
55314/*
55315** Return a section of the pPage->aData to the freelist.
55316** The first byte of the new free block is pPage->aData[iStart]
55317** and the size of the block is iSize bytes.
55318**
55319** Adjacent freeblocks are coalesced.
55320**
55321** Note that even though the freeblock list was checked by btreeInitPage(),
55322** that routine will not detect overlap between cells or freeblocks.  Nor
55323** does it detect cells or freeblocks that encrouch into the reserved bytes
55324** at the end of the page.  So do additional corruption checks inside this
55325** routine and return SQLITE_CORRUPT if any problems are found.
55326*/
55327static int freeSpace(MemPage *pPage, u16 iStart, u16 iSize){
55328  u16 iPtr;                             /* Address of ptr to next freeblock */
55329  u16 iFreeBlk;                         /* Address of the next freeblock */
55330  u8 hdr;                               /* Page header size.  0 or 100 */
55331  u8 nFrag = 0;                         /* Reduction in fragmentation */
55332  u16 iOrigSize = iSize;                /* Original value of iSize */
55333  u32 iLast = pPage->pBt->usableSize-4; /* Largest possible freeblock offset */
55334  u32 iEnd = iStart + iSize;            /* First byte past the iStart buffer */
55335  unsigned char *data = pPage->aData;   /* Page content */
55336
55337  assert( pPage->pBt!=0 );
55338  assert( sqlite3PagerIswriteable(pPage->pDbPage) );
55339  assert( CORRUPT_DB || iStart>=pPage->hdrOffset+6+pPage->childPtrSize );
55340  assert( CORRUPT_DB || iEnd <= pPage->pBt->usableSize );
55341  assert( sqlite3_mutex_held(pPage->pBt->mutex) );
55342  assert( iSize>=4 );   /* Minimum cell size is 4 */
55343  assert( iStart<=iLast );
55344
55345  /* Overwrite deleted information with zeros when the secure_delete
55346  ** option is enabled */
55347  if( pPage->pBt->btsFlags & BTS_SECURE_DELETE ){
55348    memset(&data[iStart], 0, iSize);
55349  }
55350
55351  /* The list of freeblocks must be in ascending order.  Find the
55352  ** spot on the list where iStart should be inserted.
55353  */
55354  hdr = pPage->hdrOffset;
55355  iPtr = hdr + 1;
55356  if( data[iPtr+1]==0 && data[iPtr]==0 ){
55357    iFreeBlk = 0;  /* Shortcut for the case when the freelist is empty */
55358  }else{
55359    while( (iFreeBlk = get2byte(&data[iPtr]))>0 && iFreeBlk<iStart ){
55360      if( iFreeBlk<iPtr+4 ) return SQLITE_CORRUPT_BKPT;
55361      iPtr = iFreeBlk;
55362    }
55363    if( iFreeBlk>iLast ) return SQLITE_CORRUPT_BKPT;
55364    assert( iFreeBlk>iPtr || iFreeBlk==0 );
55365
55366    /* At this point:
55367    **    iFreeBlk:   First freeblock after iStart, or zero if none
55368    **    iPtr:       The address of a pointer to iFreeBlk
55369    **
55370    ** Check to see if iFreeBlk should be coalesced onto the end of iStart.
55371    */
55372    if( iFreeBlk && iEnd+3>=iFreeBlk ){
55373      nFrag = iFreeBlk - iEnd;
55374      if( iEnd>iFreeBlk ) return SQLITE_CORRUPT_BKPT;
55375      iEnd = iFreeBlk + get2byte(&data[iFreeBlk+2]);
55376      if( iEnd > pPage->pBt->usableSize ) return SQLITE_CORRUPT_BKPT;
55377      iSize = iEnd - iStart;
55378      iFreeBlk = get2byte(&data[iFreeBlk]);
55379    }
55380
55381    /* If iPtr is another freeblock (that is, if iPtr is not the freelist
55382    ** pointer in the page header) then check to see if iStart should be
55383    ** coalesced onto the end of iPtr.
55384    */
55385    if( iPtr>hdr+1 ){
55386      int iPtrEnd = iPtr + get2byte(&data[iPtr+2]);
55387      if( iPtrEnd+3>=iStart ){
55388        if( iPtrEnd>iStart ) return SQLITE_CORRUPT_BKPT;
55389        nFrag += iStart - iPtrEnd;
55390        iSize = iEnd - iPtr;
55391        iStart = iPtr;
55392      }
55393    }
55394    if( nFrag>data[hdr+7] ) return SQLITE_CORRUPT_BKPT;
55395    data[hdr+7] -= nFrag;
55396  }
55397  if( iStart==get2byte(&data[hdr+5]) ){
55398    /* The new freeblock is at the beginning of the cell content area,
55399    ** so just extend the cell content area rather than create another
55400    ** freelist entry */
55401    if( iPtr!=hdr+1 ) return SQLITE_CORRUPT_BKPT;
55402    put2byte(&data[hdr+1], iFreeBlk);
55403    put2byte(&data[hdr+5], iEnd);
55404  }else{
55405    /* Insert the new freeblock into the freelist */
55406    put2byte(&data[iPtr], iStart);
55407    put2byte(&data[iStart], iFreeBlk);
55408    put2byte(&data[iStart+2], iSize);
55409  }
55410  pPage->nFree += iOrigSize;
55411  return SQLITE_OK;
55412}
55413
55414/*
55415** Decode the flags byte (the first byte of the header) for a page
55416** and initialize fields of the MemPage structure accordingly.
55417**
55418** Only the following combinations are supported.  Anything different
55419** indicates a corrupt database files:
55420**
55421**         PTF_ZERODATA
55422**         PTF_ZERODATA | PTF_LEAF
55423**         PTF_LEAFDATA | PTF_INTKEY
55424**         PTF_LEAFDATA | PTF_INTKEY | PTF_LEAF
55425*/
55426static int decodeFlags(MemPage *pPage, int flagByte){
55427  BtShared *pBt;     /* A copy of pPage->pBt */
55428
55429  assert( pPage->hdrOffset==(pPage->pgno==1 ? 100 : 0) );
55430  assert( sqlite3_mutex_held(pPage->pBt->mutex) );
55431  pPage->leaf = (u8)(flagByte>>3);  assert( PTF_LEAF == 1<<3 );
55432  flagByte &= ~PTF_LEAF;
55433  pPage->childPtrSize = 4-4*pPage->leaf;
55434  pPage->xCellSize = cellSizePtr;
55435  pBt = pPage->pBt;
55436  if( flagByte==(PTF_LEAFDATA | PTF_INTKEY) ){
55437    /* EVIDENCE-OF: R-03640-13415 A value of 5 means the page is an interior
55438    ** table b-tree page. */
55439    assert( (PTF_LEAFDATA|PTF_INTKEY)==5 );
55440    /* EVIDENCE-OF: R-20501-61796 A value of 13 means the page is a leaf
55441    ** table b-tree page. */
55442    assert( (PTF_LEAFDATA|PTF_INTKEY|PTF_LEAF)==13 );
55443    pPage->intKey = 1;
55444    if( pPage->leaf ){
55445      pPage->intKeyLeaf = 1;
55446      pPage->noPayload = 0;
55447      pPage->xParseCell = btreeParseCellPtr;
55448    }else{
55449      pPage->intKeyLeaf = 0;
55450      pPage->noPayload = 1;
55451      pPage->xCellSize = cellSizePtrNoPayload;
55452      pPage->xParseCell = btreeParseCellPtrNoPayload;
55453    }
55454    pPage->maxLocal = pBt->maxLeaf;
55455    pPage->minLocal = pBt->minLeaf;
55456  }else if( flagByte==PTF_ZERODATA ){
55457    /* EVIDENCE-OF: R-27225-53936 A value of 2 means the page is an interior
55458    ** index b-tree page. */
55459    assert( (PTF_ZERODATA)==2 );
55460    /* EVIDENCE-OF: R-16571-11615 A value of 10 means the page is a leaf
55461    ** index b-tree page. */
55462    assert( (PTF_ZERODATA|PTF_LEAF)==10 );
55463    pPage->intKey = 0;
55464    pPage->intKeyLeaf = 0;
55465    pPage->noPayload = 0;
55466    pPage->xParseCell = btreeParseCellPtrIndex;
55467    pPage->maxLocal = pBt->maxLocal;
55468    pPage->minLocal = pBt->minLocal;
55469  }else{
55470    /* EVIDENCE-OF: R-47608-56469 Any other value for the b-tree page type is
55471    ** an error. */
55472    return SQLITE_CORRUPT_BKPT;
55473  }
55474  pPage->max1bytePayload = pBt->max1bytePayload;
55475  return SQLITE_OK;
55476}
55477
55478/*
55479** Initialize the auxiliary information for a disk block.
55480**
55481** Return SQLITE_OK on success.  If we see that the page does
55482** not contain a well-formed database page, then return
55483** SQLITE_CORRUPT.  Note that a return of SQLITE_OK does not
55484** guarantee that the page is well-formed.  It only shows that
55485** we failed to detect any corruption.
55486*/
55487static int btreeInitPage(MemPage *pPage){
55488
55489  assert( pPage->pBt!=0 );
55490  assert( pPage->pBt->db!=0 );
55491  assert( sqlite3_mutex_held(pPage->pBt->mutex) );
55492  assert( pPage->pgno==sqlite3PagerPagenumber(pPage->pDbPage) );
55493  assert( pPage == sqlite3PagerGetExtra(pPage->pDbPage) );
55494  assert( pPage->aData == sqlite3PagerGetData(pPage->pDbPage) );
55495
55496  if( !pPage->isInit ){
55497    u16 pc;            /* Address of a freeblock within pPage->aData[] */
55498    u8 hdr;            /* Offset to beginning of page header */
55499    u8 *data;          /* Equal to pPage->aData */
55500    BtShared *pBt;        /* The main btree structure */
55501    int usableSize;    /* Amount of usable space on each page */
55502    u16 cellOffset;    /* Offset from start of page to first cell pointer */
55503    int nFree;         /* Number of unused bytes on the page */
55504    int top;           /* First byte of the cell content area */
55505    int iCellFirst;    /* First allowable cell or freeblock offset */
55506    int iCellLast;     /* Last possible cell or freeblock offset */
55507
55508    pBt = pPage->pBt;
55509
55510    hdr = pPage->hdrOffset;
55511    data = pPage->aData;
55512    /* EVIDENCE-OF: R-28594-02890 The one-byte flag at offset 0 indicating
55513    ** the b-tree page type. */
55514    if( decodeFlags(pPage, data[hdr]) ) return SQLITE_CORRUPT_BKPT;
55515    assert( pBt->pageSize>=512 && pBt->pageSize<=65536 );
55516    pPage->maskPage = (u16)(pBt->pageSize - 1);
55517    pPage->nOverflow = 0;
55518    usableSize = pBt->usableSize;
55519    pPage->cellOffset = cellOffset = hdr + 8 + pPage->childPtrSize;
55520    pPage->aDataEnd = &data[usableSize];
55521    pPage->aCellIdx = &data[cellOffset];
55522    pPage->aDataOfst = &data[pPage->childPtrSize];
55523    /* EVIDENCE-OF: R-58015-48175 The two-byte integer at offset 5 designates
55524    ** the start of the cell content area. A zero value for this integer is
55525    ** interpreted as 65536. */
55526    top = get2byteNotZero(&data[hdr+5]);
55527    /* EVIDENCE-OF: R-37002-32774 The two-byte integer at offset 3 gives the
55528    ** number of cells on the page. */
55529    pPage->nCell = get2byte(&data[hdr+3]);
55530    if( pPage->nCell>MX_CELL(pBt) ){
55531      /* To many cells for a single page.  The page must be corrupt */
55532      return SQLITE_CORRUPT_BKPT;
55533    }
55534    testcase( pPage->nCell==MX_CELL(pBt) );
55535    /* EVIDENCE-OF: R-24089-57979 If a page contains no cells (which is only
55536    ** possible for a root page of a table that contains no rows) then the
55537    ** offset to the cell content area will equal the page size minus the
55538    ** bytes of reserved space. */
55539    assert( pPage->nCell>0 || top==usableSize || CORRUPT_DB );
55540
55541    /* A malformed database page might cause us to read past the end
55542    ** of page when parsing a cell.
55543    **
55544    ** The following block of code checks early to see if a cell extends
55545    ** past the end of a page boundary and causes SQLITE_CORRUPT to be
55546    ** returned if it does.
55547    */
55548    iCellFirst = cellOffset + 2*pPage->nCell;
55549    iCellLast = usableSize - 4;
55550    if( pBt->db->flags & SQLITE_CellSizeCk ){
55551      int i;            /* Index into the cell pointer array */
55552      int sz;           /* Size of a cell */
55553
55554      if( !pPage->leaf ) iCellLast--;
55555      for(i=0; i<pPage->nCell; i++){
55556        pc = get2byteAligned(&data[cellOffset+i*2]);
55557        testcase( pc==iCellFirst );
55558        testcase( pc==iCellLast );
55559        if( pc<iCellFirst || pc>iCellLast ){
55560          return SQLITE_CORRUPT_BKPT;
55561        }
55562        sz = pPage->xCellSize(pPage, &data[pc]);
55563        testcase( pc+sz==usableSize );
55564        if( pc+sz>usableSize ){
55565          return SQLITE_CORRUPT_BKPT;
55566        }
55567      }
55568      if( !pPage->leaf ) iCellLast++;
55569    }
55570
55571    /* Compute the total free space on the page
55572    ** EVIDENCE-OF: R-23588-34450 The two-byte integer at offset 1 gives the
55573    ** start of the first freeblock on the page, or is zero if there are no
55574    ** freeblocks. */
55575    pc = get2byte(&data[hdr+1]);
55576    nFree = data[hdr+7] + top;  /* Init nFree to non-freeblock free space */
55577    while( pc>0 ){
55578      u16 next, size;
55579      if( pc<iCellFirst || pc>iCellLast ){
55580        /* EVIDENCE-OF: R-55530-52930 In a well-formed b-tree page, there will
55581        ** always be at least one cell before the first freeblock.
55582        **
55583        ** Or, the freeblock is off the end of the page
55584        */
55585        return SQLITE_CORRUPT_BKPT;
55586      }
55587      next = get2byte(&data[pc]);
55588      size = get2byte(&data[pc+2]);
55589      if( (next>0 && next<=pc+size+3) || pc+size>usableSize ){
55590        /* Free blocks must be in ascending order. And the last byte of
55591        ** the free-block must lie on the database page.  */
55592        return SQLITE_CORRUPT_BKPT;
55593      }
55594      nFree = nFree + size;
55595      pc = next;
55596    }
55597
55598    /* At this point, nFree contains the sum of the offset to the start
55599    ** of the cell-content area plus the number of free bytes within
55600    ** the cell-content area. If this is greater than the usable-size
55601    ** of the page, then the page must be corrupted. This check also
55602    ** serves to verify that the offset to the start of the cell-content
55603    ** area, according to the page header, lies within the page.
55604    */
55605    if( nFree>usableSize ){
55606      return SQLITE_CORRUPT_BKPT;
55607    }
55608    pPage->nFree = (u16)(nFree - iCellFirst);
55609    pPage->isInit = 1;
55610  }
55611  return SQLITE_OK;
55612}
55613
55614/*
55615** Set up a raw page so that it looks like a database page holding
55616** no entries.
55617*/
55618static void zeroPage(MemPage *pPage, int flags){
55619  unsigned char *data = pPage->aData;
55620  BtShared *pBt = pPage->pBt;
55621  u8 hdr = pPage->hdrOffset;
55622  u16 first;
55623
55624  assert( sqlite3PagerPagenumber(pPage->pDbPage)==pPage->pgno );
55625  assert( sqlite3PagerGetExtra(pPage->pDbPage) == (void*)pPage );
55626  assert( sqlite3PagerGetData(pPage->pDbPage) == data );
55627  assert( sqlite3PagerIswriteable(pPage->pDbPage) );
55628  assert( sqlite3_mutex_held(pBt->mutex) );
55629  if( pBt->btsFlags & BTS_SECURE_DELETE ){
55630    memset(&data[hdr], 0, pBt->usableSize - hdr);
55631  }
55632  data[hdr] = (char)flags;
55633  first = hdr + ((flags&PTF_LEAF)==0 ? 12 : 8);
55634  memset(&data[hdr+1], 0, 4);
55635  data[hdr+7] = 0;
55636  put2byte(&data[hdr+5], pBt->usableSize);
55637  pPage->nFree = (u16)(pBt->usableSize - first);
55638  decodeFlags(pPage, flags);
55639  pPage->cellOffset = first;
55640  pPage->aDataEnd = &data[pBt->usableSize];
55641  pPage->aCellIdx = &data[first];
55642  pPage->aDataOfst = &data[pPage->childPtrSize];
55643  pPage->nOverflow = 0;
55644  assert( pBt->pageSize>=512 && pBt->pageSize<=65536 );
55645  pPage->maskPage = (u16)(pBt->pageSize - 1);
55646  pPage->nCell = 0;
55647  pPage->isInit = 1;
55648}
55649
55650
55651/*
55652** Convert a DbPage obtained from the pager into a MemPage used by
55653** the btree layer.
55654*/
55655static MemPage *btreePageFromDbPage(DbPage *pDbPage, Pgno pgno, BtShared *pBt){
55656  MemPage *pPage = (MemPage*)sqlite3PagerGetExtra(pDbPage);
55657  pPage->aData = sqlite3PagerGetData(pDbPage);
55658  pPage->pDbPage = pDbPage;
55659  pPage->pBt = pBt;
55660  pPage->pgno = pgno;
55661  pPage->hdrOffset = pgno==1 ? 100 : 0;
55662  return pPage;
55663}
55664
55665/*
55666** Get a page from the pager.  Initialize the MemPage.pBt and
55667** MemPage.aData elements if needed.  See also: btreeGetUnusedPage().
55668**
55669** If the PAGER_GET_NOCONTENT flag is set, it means that we do not care
55670** about the content of the page at this time.  So do not go to the disk
55671** to fetch the content.  Just fill in the content with zeros for now.
55672** If in the future we call sqlite3PagerWrite() on this page, that
55673** means we have started to be concerned about content and the disk
55674** read should occur at that point.
55675*/
55676static int btreeGetPage(
55677  BtShared *pBt,       /* The btree */
55678  Pgno pgno,           /* Number of the page to fetch */
55679  MemPage **ppPage,    /* Return the page in this parameter */
55680  int flags            /* PAGER_GET_NOCONTENT or PAGER_GET_READONLY */
55681){
55682  int rc;
55683  DbPage *pDbPage;
55684
55685  assert( flags==0 || flags==PAGER_GET_NOCONTENT || flags==PAGER_GET_READONLY );
55686  assert( sqlite3_mutex_held(pBt->mutex) );
55687  rc = sqlite3PagerAcquire(pBt->pPager, pgno, (DbPage**)&pDbPage, flags);
55688  if( rc ) return rc;
55689  *ppPage = btreePageFromDbPage(pDbPage, pgno, pBt);
55690  return SQLITE_OK;
55691}
55692
55693/*
55694** Retrieve a page from the pager cache. If the requested page is not
55695** already in the pager cache return NULL. Initialize the MemPage.pBt and
55696** MemPage.aData elements if needed.
55697*/
55698static MemPage *btreePageLookup(BtShared *pBt, Pgno pgno){
55699  DbPage *pDbPage;
55700  assert( sqlite3_mutex_held(pBt->mutex) );
55701  pDbPage = sqlite3PagerLookup(pBt->pPager, pgno);
55702  if( pDbPage ){
55703    return btreePageFromDbPage(pDbPage, pgno, pBt);
55704  }
55705  return 0;
55706}
55707
55708/*
55709** Return the size of the database file in pages. If there is any kind of
55710** error, return ((unsigned int)-1).
55711*/
55712static Pgno btreePagecount(BtShared *pBt){
55713  return pBt->nPage;
55714}
55715SQLITE_PRIVATE u32 sqlite3BtreeLastPage(Btree *p){
55716  assert( sqlite3BtreeHoldsMutex(p) );
55717  assert( ((p->pBt->nPage)&0x8000000)==0 );
55718  return btreePagecount(p->pBt);
55719}
55720
55721/*
55722** Get a page from the pager and initialize it.
55723**
55724** If pCur!=0 then the page is being fetched as part of a moveToChild()
55725** call.  Do additional sanity checking on the page in this case.
55726** And if the fetch fails, this routine must decrement pCur->iPage.
55727**
55728** The page is fetched as read-write unless pCur is not NULL and is
55729** a read-only cursor.
55730**
55731** If an error occurs, then *ppPage is undefined. It
55732** may remain unchanged, or it may be set to an invalid value.
55733*/
55734static int getAndInitPage(
55735  BtShared *pBt,                  /* The database file */
55736  Pgno pgno,                      /* Number of the page to get */
55737  MemPage **ppPage,               /* Write the page pointer here */
55738  BtCursor *pCur,                 /* Cursor to receive the page, or NULL */
55739  int bReadOnly                   /* True for a read-only page */
55740){
55741  int rc;
55742  DbPage *pDbPage;
55743  assert( sqlite3_mutex_held(pBt->mutex) );
55744  assert( pCur==0 || ppPage==&pCur->apPage[pCur->iPage] );
55745  assert( pCur==0 || bReadOnly==pCur->curPagerFlags );
55746  assert( pCur==0 || pCur->iPage>0 );
55747
55748  if( pgno>btreePagecount(pBt) ){
55749    rc = SQLITE_CORRUPT_BKPT;
55750    goto getAndInitPage_error;
55751  }
55752  rc = sqlite3PagerAcquire(pBt->pPager, pgno, (DbPage**)&pDbPage, bReadOnly);
55753  if( rc ){
55754    goto getAndInitPage_error;
55755  }
55756  *ppPage = btreePageFromDbPage(pDbPage, pgno, pBt);
55757  if( (*ppPage)->isInit==0 ){
55758    rc = btreeInitPage(*ppPage);
55759    if( rc!=SQLITE_OK ){
55760      releasePage(*ppPage);
55761      goto getAndInitPage_error;
55762    }
55763  }
55764
55765  /* If obtaining a child page for a cursor, we must verify that the page is
55766  ** compatible with the root page. */
55767  if( pCur
55768   && ((*ppPage)->nCell<1 || (*ppPage)->intKey!=pCur->curIntKey)
55769  ){
55770    rc = SQLITE_CORRUPT_BKPT;
55771    releasePage(*ppPage);
55772    goto getAndInitPage_error;
55773  }
55774  return SQLITE_OK;
55775
55776getAndInitPage_error:
55777  if( pCur ) pCur->iPage--;
55778  testcase( pgno==0 );
55779  assert( pgno!=0 || rc==SQLITE_CORRUPT );
55780  return rc;
55781}
55782
55783/*
55784** Release a MemPage.  This should be called once for each prior
55785** call to btreeGetPage.
55786*/
55787static void releasePageNotNull(MemPage *pPage){
55788  assert( pPage->aData );
55789  assert( pPage->pBt );
55790  assert( pPage->pDbPage!=0 );
55791  assert( sqlite3PagerGetExtra(pPage->pDbPage) == (void*)pPage );
55792  assert( sqlite3PagerGetData(pPage->pDbPage)==pPage->aData );
55793  assert( sqlite3_mutex_held(pPage->pBt->mutex) );
55794  sqlite3PagerUnrefNotNull(pPage->pDbPage);
55795}
55796static void releasePage(MemPage *pPage){
55797  if( pPage ) releasePageNotNull(pPage);
55798}
55799
55800/*
55801** Get an unused page.
55802**
55803** This works just like btreeGetPage() with the addition:
55804**
55805**   *  If the page is already in use for some other purpose, immediately
55806**      release it and return an SQLITE_CURRUPT error.
55807**   *  Make sure the isInit flag is clear
55808*/
55809static int btreeGetUnusedPage(
55810  BtShared *pBt,       /* The btree */
55811  Pgno pgno,           /* Number of the page to fetch */
55812  MemPage **ppPage,    /* Return the page in this parameter */
55813  int flags            /* PAGER_GET_NOCONTENT or PAGER_GET_READONLY */
55814){
55815  int rc = btreeGetPage(pBt, pgno, ppPage, flags);
55816  if( rc==SQLITE_OK ){
55817    if( sqlite3PagerPageRefcount((*ppPage)->pDbPage)>1 ){
55818      releasePage(*ppPage);
55819      *ppPage = 0;
55820      return SQLITE_CORRUPT_BKPT;
55821    }
55822    (*ppPage)->isInit = 0;
55823  }else{
55824    *ppPage = 0;
55825  }
55826  return rc;
55827}
55828
55829
55830/*
55831** During a rollback, when the pager reloads information into the cache
55832** so that the cache is restored to its original state at the start of
55833** the transaction, for each page restored this routine is called.
55834**
55835** This routine needs to reset the extra data section at the end of the
55836** page to agree with the restored data.
55837*/
55838static void pageReinit(DbPage *pData){
55839  MemPage *pPage;
55840  pPage = (MemPage *)sqlite3PagerGetExtra(pData);
55841  assert( sqlite3PagerPageRefcount(pData)>0 );
55842  if( pPage->isInit ){
55843    assert( sqlite3_mutex_held(pPage->pBt->mutex) );
55844    pPage->isInit = 0;
55845    if( sqlite3PagerPageRefcount(pData)>1 ){
55846      /* pPage might not be a btree page;  it might be an overflow page
55847      ** or ptrmap page or a free page.  In those cases, the following
55848      ** call to btreeInitPage() will likely return SQLITE_CORRUPT.
55849      ** But no harm is done by this.  And it is very important that
55850      ** btreeInitPage() be called on every btree page so we make
55851      ** the call for every page that comes in for re-initing. */
55852      btreeInitPage(pPage);
55853    }
55854  }
55855}
55856
55857/*
55858** Invoke the busy handler for a btree.
55859*/
55860static int btreeInvokeBusyHandler(void *pArg){
55861  BtShared *pBt = (BtShared*)pArg;
55862  assert( pBt->db );
55863  assert( sqlite3_mutex_held(pBt->db->mutex) );
55864  return sqlite3InvokeBusyHandler(&pBt->db->busyHandler);
55865}
55866
55867/*
55868** Open a database file.
55869**
55870** zFilename is the name of the database file.  If zFilename is NULL
55871** then an ephemeral database is created.  The ephemeral database might
55872** be exclusively in memory, or it might use a disk-based memory cache.
55873** Either way, the ephemeral database will be automatically deleted
55874** when sqlite3BtreeClose() is called.
55875**
55876** If zFilename is ":memory:" then an in-memory database is created
55877** that is automatically destroyed when it is closed.
55878**
55879** The "flags" parameter is a bitmask that might contain bits like
55880** BTREE_OMIT_JOURNAL and/or BTREE_MEMORY.
55881**
55882** If the database is already opened in the same database connection
55883** and we are in shared cache mode, then the open will fail with an
55884** SQLITE_CONSTRAINT error.  We cannot allow two or more BtShared
55885** objects in the same database connection since doing so will lead
55886** to problems with locking.
55887*/
55888SQLITE_PRIVATE int sqlite3BtreeOpen(
55889  sqlite3_vfs *pVfs,      /* VFS to use for this b-tree */
55890  const char *zFilename,  /* Name of the file containing the BTree database */
55891  sqlite3 *db,            /* Associated database handle */
55892  Btree **ppBtree,        /* Pointer to new Btree object written here */
55893  int flags,              /* Options */
55894  int vfsFlags            /* Flags passed through to sqlite3_vfs.xOpen() */
55895){
55896  BtShared *pBt = 0;             /* Shared part of btree structure */
55897  Btree *p;                      /* Handle to return */
55898  sqlite3_mutex *mutexOpen = 0;  /* Prevents a race condition. Ticket #3537 */
55899  int rc = SQLITE_OK;            /* Result code from this function */
55900  u8 nReserve;                   /* Byte of unused space on each page */
55901  unsigned char zDbHeader[100];  /* Database header content */
55902
55903  /* True if opening an ephemeral, temporary database */
55904  const int isTempDb = zFilename==0 || zFilename[0]==0;
55905
55906  /* Set the variable isMemdb to true for an in-memory database, or
55907  ** false for a file-based database.
55908  */
55909#ifdef SQLITE_OMIT_MEMORYDB
55910  const int isMemdb = 0;
55911#else
55912  const int isMemdb = (zFilename && strcmp(zFilename, ":memory:")==0)
55913                       || (isTempDb && sqlite3TempInMemory(db))
55914                       || (vfsFlags & SQLITE_OPEN_MEMORY)!=0;
55915#endif
55916
55917  assert( db!=0 );
55918  assert( pVfs!=0 );
55919  assert( sqlite3_mutex_held(db->mutex) );
55920  assert( (flags&0xff)==flags );   /* flags fit in 8 bits */
55921
55922  /* Only a BTREE_SINGLE database can be BTREE_UNORDERED */
55923  assert( (flags & BTREE_UNORDERED)==0 || (flags & BTREE_SINGLE)!=0 );
55924
55925  /* A BTREE_SINGLE database is always a temporary and/or ephemeral */
55926  assert( (flags & BTREE_SINGLE)==0 || isTempDb );
55927
55928  if( isMemdb ){
55929    flags |= BTREE_MEMORY;
55930  }
55931  if( (vfsFlags & SQLITE_OPEN_MAIN_DB)!=0 && (isMemdb || isTempDb) ){
55932    vfsFlags = (vfsFlags & ~SQLITE_OPEN_MAIN_DB) | SQLITE_OPEN_TEMP_DB;
55933  }
55934  p = sqlite3MallocZero(sizeof(Btree));
55935  if( !p ){
55936    return SQLITE_NOMEM;
55937  }
55938  p->inTrans = TRANS_NONE;
55939  p->db = db;
55940#ifndef SQLITE_OMIT_SHARED_CACHE
55941  p->lock.pBtree = p;
55942  p->lock.iTable = 1;
55943#endif
55944
55945#if !defined(SQLITE_OMIT_SHARED_CACHE) && !defined(SQLITE_OMIT_DISKIO)
55946  /*
55947  ** If this Btree is a candidate for shared cache, try to find an
55948  ** existing BtShared object that we can share with
55949  */
55950  if( isTempDb==0 && (isMemdb==0 || (vfsFlags&SQLITE_OPEN_URI)!=0) ){
55951    if( vfsFlags & SQLITE_OPEN_SHAREDCACHE ){
55952      int nFilename = sqlite3Strlen30(zFilename)+1;
55953      int nFullPathname = pVfs->mxPathname+1;
55954      char *zFullPathname = sqlite3Malloc(MAX(nFullPathname,nFilename));
55955      MUTEX_LOGIC( sqlite3_mutex *mutexShared; )
55956
55957      p->sharable = 1;
55958      if( !zFullPathname ){
55959        sqlite3_free(p);
55960        return SQLITE_NOMEM;
55961      }
55962      if( isMemdb ){
55963        memcpy(zFullPathname, zFilename, nFilename);
55964      }else{
55965        rc = sqlite3OsFullPathname(pVfs, zFilename,
55966                                   nFullPathname, zFullPathname);
55967        if( rc ){
55968          sqlite3_free(zFullPathname);
55969          sqlite3_free(p);
55970          return rc;
55971        }
55972      }
55973#if SQLITE_THREADSAFE
55974      mutexOpen = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_OPEN);
55975      sqlite3_mutex_enter(mutexOpen);
55976      mutexShared = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);
55977      sqlite3_mutex_enter(mutexShared);
55978#endif
55979      for(pBt=GLOBAL(BtShared*,sqlite3SharedCacheList); pBt; pBt=pBt->pNext){
55980        assert( pBt->nRef>0 );
55981        if( 0==strcmp(zFullPathname, sqlite3PagerFilename(pBt->pPager, 0))
55982                 && sqlite3PagerVfs(pBt->pPager)==pVfs ){
55983          int iDb;
55984          for(iDb=db->nDb-1; iDb>=0; iDb--){
55985            Btree *pExisting = db->aDb[iDb].pBt;
55986            if( pExisting && pExisting->pBt==pBt ){
55987              sqlite3_mutex_leave(mutexShared);
55988              sqlite3_mutex_leave(mutexOpen);
55989              sqlite3_free(zFullPathname);
55990              sqlite3_free(p);
55991              return SQLITE_CONSTRAINT;
55992            }
55993          }
55994          p->pBt = pBt;
55995          pBt->nRef++;
55996          break;
55997        }
55998      }
55999      sqlite3_mutex_leave(mutexShared);
56000      sqlite3_free(zFullPathname);
56001    }
56002#ifdef SQLITE_DEBUG
56003    else{
56004      /* In debug mode, we mark all persistent databases as sharable
56005      ** even when they are not.  This exercises the locking code and
56006      ** gives more opportunity for asserts(sqlite3_mutex_held())
56007      ** statements to find locking problems.
56008      */
56009      p->sharable = 1;
56010    }
56011#endif
56012  }
56013#endif
56014  if( pBt==0 ){
56015    /*
56016    ** The following asserts make sure that structures used by the btree are
56017    ** the right size.  This is to guard against size changes that result
56018    ** when compiling on a different architecture.
56019    */
56020    assert( sizeof(i64)==8 );
56021    assert( sizeof(u64)==8 );
56022    assert( sizeof(u32)==4 );
56023    assert( sizeof(u16)==2 );
56024    assert( sizeof(Pgno)==4 );
56025
56026    pBt = sqlite3MallocZero( sizeof(*pBt) );
56027    if( pBt==0 ){
56028      rc = SQLITE_NOMEM;
56029      goto btree_open_out;
56030    }
56031    rc = sqlite3PagerOpen(pVfs, &pBt->pPager, zFilename,
56032                          EXTRA_SIZE, flags, vfsFlags, pageReinit);
56033    if( rc==SQLITE_OK ){
56034      sqlite3PagerSetMmapLimit(pBt->pPager, db->szMmap);
56035      rc = sqlite3PagerReadFileheader(pBt->pPager,sizeof(zDbHeader),zDbHeader);
56036    }
56037    if( rc!=SQLITE_OK ){
56038      goto btree_open_out;
56039    }
56040    pBt->openFlags = (u8)flags;
56041    pBt->db = db;
56042    sqlite3PagerSetBusyhandler(pBt->pPager, btreeInvokeBusyHandler, pBt);
56043    p->pBt = pBt;
56044
56045    pBt->pCursor = 0;
56046    pBt->pPage1 = 0;
56047    if( sqlite3PagerIsreadonly(pBt->pPager) ) pBt->btsFlags |= BTS_READ_ONLY;
56048#ifdef SQLITE_SECURE_DELETE
56049    pBt->btsFlags |= BTS_SECURE_DELETE;
56050#endif
56051    /* EVIDENCE-OF: R-51873-39618 The page size for a database file is
56052    ** determined by the 2-byte integer located at an offset of 16 bytes from
56053    ** the beginning of the database file. */
56054    pBt->pageSize = (zDbHeader[16]<<8) | (zDbHeader[17]<<16);
56055    if( pBt->pageSize<512 || pBt->pageSize>SQLITE_MAX_PAGE_SIZE
56056         || ((pBt->pageSize-1)&pBt->pageSize)!=0 ){
56057      pBt->pageSize = 0;
56058#ifndef SQLITE_OMIT_AUTOVACUUM
56059      /* If the magic name ":memory:" will create an in-memory database, then
56060      ** leave the autoVacuum mode at 0 (do not auto-vacuum), even if
56061      ** SQLITE_DEFAULT_AUTOVACUUM is true. On the other hand, if
56062      ** SQLITE_OMIT_MEMORYDB has been defined, then ":memory:" is just a
56063      ** regular file-name. In this case the auto-vacuum applies as per normal.
56064      */
56065      if( zFilename && !isMemdb ){
56066        pBt->autoVacuum = (SQLITE_DEFAULT_AUTOVACUUM ? 1 : 0);
56067        pBt->incrVacuum = (SQLITE_DEFAULT_AUTOVACUUM==2 ? 1 : 0);
56068      }
56069#endif
56070      nReserve = 0;
56071    }else{
56072      /* EVIDENCE-OF: R-37497-42412 The size of the reserved region is
56073      ** determined by the one-byte unsigned integer found at an offset of 20
56074      ** into the database file header. */
56075      nReserve = zDbHeader[20];
56076      pBt->btsFlags |= BTS_PAGESIZE_FIXED;
56077#ifndef SQLITE_OMIT_AUTOVACUUM
56078      pBt->autoVacuum = (get4byte(&zDbHeader[36 + 4*4])?1:0);
56079      pBt->incrVacuum = (get4byte(&zDbHeader[36 + 7*4])?1:0);
56080#endif
56081    }
56082    rc = sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize, nReserve);
56083    if( rc ) goto btree_open_out;
56084    pBt->usableSize = pBt->pageSize - nReserve;
56085    assert( (pBt->pageSize & 7)==0 );  /* 8-byte alignment of pageSize */
56086
56087#if !defined(SQLITE_OMIT_SHARED_CACHE) && !defined(SQLITE_OMIT_DISKIO)
56088    /* Add the new BtShared object to the linked list sharable BtShareds.
56089    */
56090    if( p->sharable ){
56091      MUTEX_LOGIC( sqlite3_mutex *mutexShared; )
56092      pBt->nRef = 1;
56093      MUTEX_LOGIC( mutexShared = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);)
56094      if( SQLITE_THREADSAFE && sqlite3GlobalConfig.bCoreMutex ){
56095        pBt->mutex = sqlite3MutexAlloc(SQLITE_MUTEX_FAST);
56096        if( pBt->mutex==0 ){
56097          rc = SQLITE_NOMEM;
56098          db->mallocFailed = 0;
56099          goto btree_open_out;
56100        }
56101      }
56102      sqlite3_mutex_enter(mutexShared);
56103      pBt->pNext = GLOBAL(BtShared*,sqlite3SharedCacheList);
56104      GLOBAL(BtShared*,sqlite3SharedCacheList) = pBt;
56105      sqlite3_mutex_leave(mutexShared);
56106    }
56107#endif
56108  }
56109
56110#if !defined(SQLITE_OMIT_SHARED_CACHE) && !defined(SQLITE_OMIT_DISKIO)
56111  /* If the new Btree uses a sharable pBtShared, then link the new
56112  ** Btree into the list of all sharable Btrees for the same connection.
56113  ** The list is kept in ascending order by pBt address.
56114  */
56115  if( p->sharable ){
56116    int i;
56117    Btree *pSib;
56118    for(i=0; i<db->nDb; i++){
56119      if( (pSib = db->aDb[i].pBt)!=0 && pSib->sharable ){
56120        while( pSib->pPrev ){ pSib = pSib->pPrev; }
56121        if( p->pBt<pSib->pBt ){
56122          p->pNext = pSib;
56123          p->pPrev = 0;
56124          pSib->pPrev = p;
56125        }else{
56126          while( pSib->pNext && pSib->pNext->pBt<p->pBt ){
56127            pSib = pSib->pNext;
56128          }
56129          p->pNext = pSib->pNext;
56130          p->pPrev = pSib;
56131          if( p->pNext ){
56132            p->pNext->pPrev = p;
56133          }
56134          pSib->pNext = p;
56135        }
56136        break;
56137      }
56138    }
56139  }
56140#endif
56141  *ppBtree = p;
56142
56143btree_open_out:
56144  if( rc!=SQLITE_OK ){
56145    if( pBt && pBt->pPager ){
56146      sqlite3PagerClose(pBt->pPager);
56147    }
56148    sqlite3_free(pBt);
56149    sqlite3_free(p);
56150    *ppBtree = 0;
56151  }else{
56152    /* If the B-Tree was successfully opened, set the pager-cache size to the
56153    ** default value. Except, when opening on an existing shared pager-cache,
56154    ** do not change the pager-cache size.
56155    */
56156    if( sqlite3BtreeSchema(p, 0, 0)==0 ){
56157      sqlite3PagerSetCachesize(p->pBt->pPager, SQLITE_DEFAULT_CACHE_SIZE);
56158    }
56159  }
56160  if( mutexOpen ){
56161    assert( sqlite3_mutex_held(mutexOpen) );
56162    sqlite3_mutex_leave(mutexOpen);
56163  }
56164  return rc;
56165}
56166
56167/*
56168** Decrement the BtShared.nRef counter.  When it reaches zero,
56169** remove the BtShared structure from the sharing list.  Return
56170** true if the BtShared.nRef counter reaches zero and return
56171** false if it is still positive.
56172*/
56173static int removeFromSharingList(BtShared *pBt){
56174#ifndef SQLITE_OMIT_SHARED_CACHE
56175  MUTEX_LOGIC( sqlite3_mutex *pMaster; )
56176  BtShared *pList;
56177  int removed = 0;
56178
56179  assert( sqlite3_mutex_notheld(pBt->mutex) );
56180  MUTEX_LOGIC( pMaster = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER); )
56181  sqlite3_mutex_enter(pMaster);
56182  pBt->nRef--;
56183  if( pBt->nRef<=0 ){
56184    if( GLOBAL(BtShared*,sqlite3SharedCacheList)==pBt ){
56185      GLOBAL(BtShared*,sqlite3SharedCacheList) = pBt->pNext;
56186    }else{
56187      pList = GLOBAL(BtShared*,sqlite3SharedCacheList);
56188      while( ALWAYS(pList) && pList->pNext!=pBt ){
56189        pList=pList->pNext;
56190      }
56191      if( ALWAYS(pList) ){
56192        pList->pNext = pBt->pNext;
56193      }
56194    }
56195    if( SQLITE_THREADSAFE ){
56196      sqlite3_mutex_free(pBt->mutex);
56197    }
56198    removed = 1;
56199  }
56200  sqlite3_mutex_leave(pMaster);
56201  return removed;
56202#else
56203  return 1;
56204#endif
56205}
56206
56207/*
56208** Make sure pBt->pTmpSpace points to an allocation of
56209** MX_CELL_SIZE(pBt) bytes with a 4-byte prefix for a left-child
56210** pointer.
56211*/
56212static void allocateTempSpace(BtShared *pBt){
56213  if( !pBt->pTmpSpace ){
56214    pBt->pTmpSpace = sqlite3PageMalloc( pBt->pageSize );
56215
56216    /* One of the uses of pBt->pTmpSpace is to format cells before
56217    ** inserting them into a leaf page (function fillInCell()). If
56218    ** a cell is less than 4 bytes in size, it is rounded up to 4 bytes
56219    ** by the various routines that manipulate binary cells. Which
56220    ** can mean that fillInCell() only initializes the first 2 or 3
56221    ** bytes of pTmpSpace, but that the first 4 bytes are copied from
56222    ** it into a database page. This is not actually a problem, but it
56223    ** does cause a valgrind error when the 1 or 2 bytes of unitialized
56224    ** data is passed to system call write(). So to avoid this error,
56225    ** zero the first 4 bytes of temp space here.
56226    **
56227    ** Also:  Provide four bytes of initialized space before the
56228    ** beginning of pTmpSpace as an area available to prepend the
56229    ** left-child pointer to the beginning of a cell.
56230    */
56231    if( pBt->pTmpSpace ){
56232      memset(pBt->pTmpSpace, 0, 8);
56233      pBt->pTmpSpace += 4;
56234    }
56235  }
56236}
56237
56238/*
56239** Free the pBt->pTmpSpace allocation
56240*/
56241static void freeTempSpace(BtShared *pBt){
56242  if( pBt->pTmpSpace ){
56243    pBt->pTmpSpace -= 4;
56244    sqlite3PageFree(pBt->pTmpSpace);
56245    pBt->pTmpSpace = 0;
56246  }
56247}
56248
56249/*
56250** Close an open database and invalidate all cursors.
56251*/
56252SQLITE_PRIVATE int sqlite3BtreeClose(Btree *p){
56253  BtShared *pBt = p->pBt;
56254  BtCursor *pCur;
56255
56256  /* Close all cursors opened via this handle.  */
56257  assert( sqlite3_mutex_held(p->db->mutex) );
56258  sqlite3BtreeEnter(p);
56259  pCur = pBt->pCursor;
56260  while( pCur ){
56261    BtCursor *pTmp = pCur;
56262    pCur = pCur->pNext;
56263    if( pTmp->pBtree==p ){
56264      sqlite3BtreeCloseCursor(pTmp);
56265    }
56266  }
56267
56268  /* Rollback any active transaction and free the handle structure.
56269  ** The call to sqlite3BtreeRollback() drops any table-locks held by
56270  ** this handle.
56271  */
56272  sqlite3BtreeRollback(p, SQLITE_OK, 0);
56273  sqlite3BtreeLeave(p);
56274
56275  /* If there are still other outstanding references to the shared-btree
56276  ** structure, return now. The remainder of this procedure cleans
56277  ** up the shared-btree.
56278  */
56279  assert( p->wantToLock==0 && p->locked==0 );
56280  if( !p->sharable || removeFromSharingList(pBt) ){
56281    /* The pBt is no longer on the sharing list, so we can access
56282    ** it without having to hold the mutex.
56283    **
56284    ** Clean out and delete the BtShared object.
56285    */
56286    assert( !pBt->pCursor );
56287    sqlite3PagerClose(pBt->pPager);
56288    if( pBt->xFreeSchema && pBt->pSchema ){
56289      pBt->xFreeSchema(pBt->pSchema);
56290    }
56291    sqlite3DbFree(0, pBt->pSchema);
56292    freeTempSpace(pBt);
56293    sqlite3_free(pBt);
56294  }
56295
56296#ifndef SQLITE_OMIT_SHARED_CACHE
56297  assert( p->wantToLock==0 );
56298  assert( p->locked==0 );
56299  if( p->pPrev ) p->pPrev->pNext = p->pNext;
56300  if( p->pNext ) p->pNext->pPrev = p->pPrev;
56301#endif
56302
56303  sqlite3_free(p);
56304  return SQLITE_OK;
56305}
56306
56307/*
56308** Change the limit on the number of pages allowed in the cache.
56309**
56310** The maximum number of cache pages is set to the absolute
56311** value of mxPage.  If mxPage is negative, the pager will
56312** operate asynchronously - it will not stop to do fsync()s
56313** to insure data is written to the disk surface before
56314** continuing.  Transactions still work if synchronous is off,
56315** and the database cannot be corrupted if this program
56316** crashes.  But if the operating system crashes or there is
56317** an abrupt power failure when synchronous is off, the database
56318** could be left in an inconsistent and unrecoverable state.
56319** Synchronous is on by default so database corruption is not
56320** normally a worry.
56321*/
56322SQLITE_PRIVATE int sqlite3BtreeSetCacheSize(Btree *p, int mxPage){
56323  BtShared *pBt = p->pBt;
56324  assert( sqlite3_mutex_held(p->db->mutex) );
56325  sqlite3BtreeEnter(p);
56326  sqlite3PagerSetCachesize(pBt->pPager, mxPage);
56327  sqlite3BtreeLeave(p);
56328  return SQLITE_OK;
56329}
56330
56331#if SQLITE_MAX_MMAP_SIZE>0
56332/*
56333** Change the limit on the amount of the database file that may be
56334** memory mapped.
56335*/
56336SQLITE_PRIVATE int sqlite3BtreeSetMmapLimit(Btree *p, sqlite3_int64 szMmap){
56337  BtShared *pBt = p->pBt;
56338  assert( sqlite3_mutex_held(p->db->mutex) );
56339  sqlite3BtreeEnter(p);
56340  sqlite3PagerSetMmapLimit(pBt->pPager, szMmap);
56341  sqlite3BtreeLeave(p);
56342  return SQLITE_OK;
56343}
56344#endif /* SQLITE_MAX_MMAP_SIZE>0 */
56345
56346/*
56347** Change the way data is synced to disk in order to increase or decrease
56348** how well the database resists damage due to OS crashes and power
56349** failures.  Level 1 is the same as asynchronous (no syncs() occur and
56350** there is a high probability of damage)  Level 2 is the default.  There
56351** is a very low but non-zero probability of damage.  Level 3 reduces the
56352** probability of damage to near zero but with a write performance reduction.
56353*/
56354#ifndef SQLITE_OMIT_PAGER_PRAGMAS
56355SQLITE_PRIVATE int sqlite3BtreeSetPagerFlags(
56356  Btree *p,              /* The btree to set the safety level on */
56357  unsigned pgFlags       /* Various PAGER_* flags */
56358){
56359  BtShared *pBt = p->pBt;
56360  assert( sqlite3_mutex_held(p->db->mutex) );
56361  sqlite3BtreeEnter(p);
56362  sqlite3PagerSetFlags(pBt->pPager, pgFlags);
56363  sqlite3BtreeLeave(p);
56364  return SQLITE_OK;
56365}
56366#endif
56367
56368/*
56369** Return TRUE if the given btree is set to safety level 1.  In other
56370** words, return TRUE if no sync() occurs on the disk files.
56371*/
56372SQLITE_PRIVATE int sqlite3BtreeSyncDisabled(Btree *p){
56373  BtShared *pBt = p->pBt;
56374  int rc;
56375  assert( sqlite3_mutex_held(p->db->mutex) );
56376  sqlite3BtreeEnter(p);
56377  assert( pBt && pBt->pPager );
56378  rc = sqlite3PagerNosync(pBt->pPager);
56379  sqlite3BtreeLeave(p);
56380  return rc;
56381}
56382
56383/*
56384** Change the default pages size and the number of reserved bytes per page.
56385** Or, if the page size has already been fixed, return SQLITE_READONLY
56386** without changing anything.
56387**
56388** The page size must be a power of 2 between 512 and 65536.  If the page
56389** size supplied does not meet this constraint then the page size is not
56390** changed.
56391**
56392** Page sizes are constrained to be a power of two so that the region
56393** of the database file used for locking (beginning at PENDING_BYTE,
56394** the first byte past the 1GB boundary, 0x40000000) needs to occur
56395** at the beginning of a page.
56396**
56397** If parameter nReserve is less than zero, then the number of reserved
56398** bytes per page is left unchanged.
56399**
56400** If the iFix!=0 then the BTS_PAGESIZE_FIXED flag is set so that the page size
56401** and autovacuum mode can no longer be changed.
56402*/
56403SQLITE_PRIVATE int sqlite3BtreeSetPageSize(Btree *p, int pageSize, int nReserve, int iFix){
56404  int rc = SQLITE_OK;
56405  BtShared *pBt = p->pBt;
56406  assert( nReserve>=-1 && nReserve<=255 );
56407  sqlite3BtreeEnter(p);
56408#if SQLITE_HAS_CODEC
56409  if( nReserve>pBt->optimalReserve ) pBt->optimalReserve = (u8)nReserve;
56410#endif
56411  if( pBt->btsFlags & BTS_PAGESIZE_FIXED ){
56412    sqlite3BtreeLeave(p);
56413    return SQLITE_READONLY;
56414  }
56415  if( nReserve<0 ){
56416    nReserve = pBt->pageSize - pBt->usableSize;
56417  }
56418  assert( nReserve>=0 && nReserve<=255 );
56419  if( pageSize>=512 && pageSize<=SQLITE_MAX_PAGE_SIZE &&
56420        ((pageSize-1)&pageSize)==0 ){
56421    assert( (pageSize & 7)==0 );
56422    assert( !pBt->pCursor );
56423    pBt->pageSize = (u32)pageSize;
56424    freeTempSpace(pBt);
56425  }
56426  rc = sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize, nReserve);
56427  pBt->usableSize = pBt->pageSize - (u16)nReserve;
56428  if( iFix ) pBt->btsFlags |= BTS_PAGESIZE_FIXED;
56429  sqlite3BtreeLeave(p);
56430  return rc;
56431}
56432
56433/*
56434** Return the currently defined page size
56435*/
56436SQLITE_PRIVATE int sqlite3BtreeGetPageSize(Btree *p){
56437  return p->pBt->pageSize;
56438}
56439
56440/*
56441** This function is similar to sqlite3BtreeGetReserve(), except that it
56442** may only be called if it is guaranteed that the b-tree mutex is already
56443** held.
56444**
56445** This is useful in one special case in the backup API code where it is
56446** known that the shared b-tree mutex is held, but the mutex on the
56447** database handle that owns *p is not. In this case if sqlite3BtreeEnter()
56448** were to be called, it might collide with some other operation on the
56449** database handle that owns *p, causing undefined behavior.
56450*/
56451SQLITE_PRIVATE int sqlite3BtreeGetReserveNoMutex(Btree *p){
56452  int n;
56453  assert( sqlite3_mutex_held(p->pBt->mutex) );
56454  n = p->pBt->pageSize - p->pBt->usableSize;
56455  return n;
56456}
56457
56458/*
56459** Return the number of bytes of space at the end of every page that
56460** are intentually left unused.  This is the "reserved" space that is
56461** sometimes used by extensions.
56462**
56463** If SQLITE_HAS_MUTEX is defined then the number returned is the
56464** greater of the current reserved space and the maximum requested
56465** reserve space.
56466*/
56467SQLITE_PRIVATE int sqlite3BtreeGetOptimalReserve(Btree *p){
56468  int n;
56469  sqlite3BtreeEnter(p);
56470  n = sqlite3BtreeGetReserveNoMutex(p);
56471#ifdef SQLITE_HAS_CODEC
56472  if( n<p->pBt->optimalReserve ) n = p->pBt->optimalReserve;
56473#endif
56474  sqlite3BtreeLeave(p);
56475  return n;
56476}
56477
56478
56479/*
56480** Set the maximum page count for a database if mxPage is positive.
56481** No changes are made if mxPage is 0 or negative.
56482** Regardless of the value of mxPage, return the maximum page count.
56483*/
56484SQLITE_PRIVATE int sqlite3BtreeMaxPageCount(Btree *p, int mxPage){
56485  int n;
56486  sqlite3BtreeEnter(p);
56487  n = sqlite3PagerMaxPageCount(p->pBt->pPager, mxPage);
56488  sqlite3BtreeLeave(p);
56489  return n;
56490}
56491
56492/*
56493** Set the BTS_SECURE_DELETE flag if newFlag is 0 or 1.  If newFlag is -1,
56494** then make no changes.  Always return the value of the BTS_SECURE_DELETE
56495** setting after the change.
56496*/
56497SQLITE_PRIVATE int sqlite3BtreeSecureDelete(Btree *p, int newFlag){
56498  int b;
56499  if( p==0 ) return 0;
56500  sqlite3BtreeEnter(p);
56501  if( newFlag>=0 ){
56502    p->pBt->btsFlags &= ~BTS_SECURE_DELETE;
56503    if( newFlag ) p->pBt->btsFlags |= BTS_SECURE_DELETE;
56504  }
56505  b = (p->pBt->btsFlags & BTS_SECURE_DELETE)!=0;
56506  sqlite3BtreeLeave(p);
56507  return b;
56508}
56509
56510/*
56511** Change the 'auto-vacuum' property of the database. If the 'autoVacuum'
56512** parameter is non-zero, then auto-vacuum mode is enabled. If zero, it
56513** is disabled. The default value for the auto-vacuum property is
56514** determined by the SQLITE_DEFAULT_AUTOVACUUM macro.
56515*/
56516SQLITE_PRIVATE int sqlite3BtreeSetAutoVacuum(Btree *p, int autoVacuum){
56517#ifdef SQLITE_OMIT_AUTOVACUUM
56518  return SQLITE_READONLY;
56519#else
56520  BtShared *pBt = p->pBt;
56521  int rc = SQLITE_OK;
56522  u8 av = (u8)autoVacuum;
56523
56524  sqlite3BtreeEnter(p);
56525  if( (pBt->btsFlags & BTS_PAGESIZE_FIXED)!=0 && (av ?1:0)!=pBt->autoVacuum ){
56526    rc = SQLITE_READONLY;
56527  }else{
56528    pBt->autoVacuum = av ?1:0;
56529    pBt->incrVacuum = av==2 ?1:0;
56530  }
56531  sqlite3BtreeLeave(p);
56532  return rc;
56533#endif
56534}
56535
56536/*
56537** Return the value of the 'auto-vacuum' property. If auto-vacuum is
56538** enabled 1 is returned. Otherwise 0.
56539*/
56540SQLITE_PRIVATE int sqlite3BtreeGetAutoVacuum(Btree *p){
56541#ifdef SQLITE_OMIT_AUTOVACUUM
56542  return BTREE_AUTOVACUUM_NONE;
56543#else
56544  int rc;
56545  sqlite3BtreeEnter(p);
56546  rc = (
56547    (!p->pBt->autoVacuum)?BTREE_AUTOVACUUM_NONE:
56548    (!p->pBt->incrVacuum)?BTREE_AUTOVACUUM_FULL:
56549    BTREE_AUTOVACUUM_INCR
56550  );
56551  sqlite3BtreeLeave(p);
56552  return rc;
56553#endif
56554}
56555
56556
56557/*
56558** Get a reference to pPage1 of the database file.  This will
56559** also acquire a readlock on that file.
56560**
56561** SQLITE_OK is returned on success.  If the file is not a
56562** well-formed database file, then SQLITE_CORRUPT is returned.
56563** SQLITE_BUSY is returned if the database is locked.  SQLITE_NOMEM
56564** is returned if we run out of memory.
56565*/
56566static int lockBtree(BtShared *pBt){
56567  int rc;              /* Result code from subfunctions */
56568  MemPage *pPage1;     /* Page 1 of the database file */
56569  int nPage;           /* Number of pages in the database */
56570  int nPageFile = 0;   /* Number of pages in the database file */
56571  int nPageHeader;     /* Number of pages in the database according to hdr */
56572
56573  assert( sqlite3_mutex_held(pBt->mutex) );
56574  assert( pBt->pPage1==0 );
56575  rc = sqlite3PagerSharedLock(pBt->pPager);
56576  if( rc!=SQLITE_OK ) return rc;
56577  rc = btreeGetPage(pBt, 1, &pPage1, 0);
56578  if( rc!=SQLITE_OK ) return rc;
56579
56580  /* Do some checking to help insure the file we opened really is
56581  ** a valid database file.
56582  */
56583  nPage = nPageHeader = get4byte(28+(u8*)pPage1->aData);
56584  sqlite3PagerPagecount(pBt->pPager, &nPageFile);
56585  if( nPage==0 || memcmp(24+(u8*)pPage1->aData, 92+(u8*)pPage1->aData,4)!=0 ){
56586    nPage = nPageFile;
56587  }
56588  if( nPage>0 ){
56589    u32 pageSize;
56590    u32 usableSize;
56591    u8 *page1 = pPage1->aData;
56592    rc = SQLITE_NOTADB;
56593    /* EVIDENCE-OF: R-43737-39999 Every valid SQLite database file begins
56594    ** with the following 16 bytes (in hex): 53 51 4c 69 74 65 20 66 6f 72 6d
56595    ** 61 74 20 33 00. */
56596    if( memcmp(page1, zMagicHeader, 16)!=0 ){
56597      goto page1_init_failed;
56598    }
56599
56600#ifdef SQLITE_OMIT_WAL
56601    if( page1[18]>1 ){
56602      pBt->btsFlags |= BTS_READ_ONLY;
56603    }
56604    if( page1[19]>1 ){
56605      goto page1_init_failed;
56606    }
56607#else
56608    if( page1[18]>2 ){
56609      pBt->btsFlags |= BTS_READ_ONLY;
56610    }
56611    if( page1[19]>2 ){
56612      goto page1_init_failed;
56613    }
56614
56615    /* If the write version is set to 2, this database should be accessed
56616    ** in WAL mode. If the log is not already open, open it now. Then
56617    ** return SQLITE_OK and return without populating BtShared.pPage1.
56618    ** The caller detects this and calls this function again. This is
56619    ** required as the version of page 1 currently in the page1 buffer
56620    ** may not be the latest version - there may be a newer one in the log
56621    ** file.
56622    */
56623    if( page1[19]==2 && (pBt->btsFlags & BTS_NO_WAL)==0 ){
56624      int isOpen = 0;
56625      rc = sqlite3PagerOpenWal(pBt->pPager, &isOpen);
56626      if( rc!=SQLITE_OK ){
56627        goto page1_init_failed;
56628      }else if( isOpen==0 ){
56629        releasePage(pPage1);
56630        return SQLITE_OK;
56631      }
56632      rc = SQLITE_NOTADB;
56633    }
56634#endif
56635
56636    /* EVIDENCE-OF: R-15465-20813 The maximum and minimum embedded payload
56637    ** fractions and the leaf payload fraction values must be 64, 32, and 32.
56638    **
56639    ** The original design allowed these amounts to vary, but as of
56640    ** version 3.6.0, we require them to be fixed.
56641    */
56642    if( memcmp(&page1[21], "\100\040\040",3)!=0 ){
56643      goto page1_init_failed;
56644    }
56645    /* EVIDENCE-OF: R-51873-39618 The page size for a database file is
56646    ** determined by the 2-byte integer located at an offset of 16 bytes from
56647    ** the beginning of the database file. */
56648    pageSize = (page1[16]<<8) | (page1[17]<<16);
56649    /* EVIDENCE-OF: R-25008-21688 The size of a page is a power of two
56650    ** between 512 and 65536 inclusive. */
56651    if( ((pageSize-1)&pageSize)!=0
56652     || pageSize>SQLITE_MAX_PAGE_SIZE
56653     || pageSize<=256
56654    ){
56655      goto page1_init_failed;
56656    }
56657    assert( (pageSize & 7)==0 );
56658    /* EVIDENCE-OF: R-59310-51205 The "reserved space" size in the 1-byte
56659    ** integer at offset 20 is the number of bytes of space at the end of
56660    ** each page to reserve for extensions.
56661    **
56662    ** EVIDENCE-OF: R-37497-42412 The size of the reserved region is
56663    ** determined by the one-byte unsigned integer found at an offset of 20
56664    ** into the database file header. */
56665    usableSize = pageSize - page1[20];
56666    if( (u32)pageSize!=pBt->pageSize ){
56667      /* After reading the first page of the database assuming a page size
56668      ** of BtShared.pageSize, we have discovered that the page-size is
56669      ** actually pageSize. Unlock the database, leave pBt->pPage1 at
56670      ** zero and return SQLITE_OK. The caller will call this function
56671      ** again with the correct page-size.
56672      */
56673      releasePage(pPage1);
56674      pBt->usableSize = usableSize;
56675      pBt->pageSize = pageSize;
56676      freeTempSpace(pBt);
56677      rc = sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize,
56678                                   pageSize-usableSize);
56679      return rc;
56680    }
56681    if( (pBt->db->flags & SQLITE_RecoveryMode)==0 && nPage>nPageFile ){
56682      rc = SQLITE_CORRUPT_BKPT;
56683      goto page1_init_failed;
56684    }
56685    /* EVIDENCE-OF: R-28312-64704 However, the usable size is not allowed to
56686    ** be less than 480. In other words, if the page size is 512, then the
56687    ** reserved space size cannot exceed 32. */
56688    if( usableSize<480 ){
56689      goto page1_init_failed;
56690    }
56691    pBt->pageSize = pageSize;
56692    pBt->usableSize = usableSize;
56693#ifndef SQLITE_OMIT_AUTOVACUUM
56694    pBt->autoVacuum = (get4byte(&page1[36 + 4*4])?1:0);
56695    pBt->incrVacuum = (get4byte(&page1[36 + 7*4])?1:0);
56696#endif
56697  }
56698
56699  /* maxLocal is the maximum amount of payload to store locally for
56700  ** a cell.  Make sure it is small enough so that at least minFanout
56701  ** cells can will fit on one page.  We assume a 10-byte page header.
56702  ** Besides the payload, the cell must store:
56703  **     2-byte pointer to the cell
56704  **     4-byte child pointer
56705  **     9-byte nKey value
56706  **     4-byte nData value
56707  **     4-byte overflow page pointer
56708  ** So a cell consists of a 2-byte pointer, a header which is as much as
56709  ** 17 bytes long, 0 to N bytes of payload, and an optional 4 byte overflow
56710  ** page pointer.
56711  */
56712  pBt->maxLocal = (u16)((pBt->usableSize-12)*64/255 - 23);
56713  pBt->minLocal = (u16)((pBt->usableSize-12)*32/255 - 23);
56714  pBt->maxLeaf = (u16)(pBt->usableSize - 35);
56715  pBt->minLeaf = (u16)((pBt->usableSize-12)*32/255 - 23);
56716  if( pBt->maxLocal>127 ){
56717    pBt->max1bytePayload = 127;
56718  }else{
56719    pBt->max1bytePayload = (u8)pBt->maxLocal;
56720  }
56721  assert( pBt->maxLeaf + 23 <= MX_CELL_SIZE(pBt) );
56722  pBt->pPage1 = pPage1;
56723  pBt->nPage = nPage;
56724  return SQLITE_OK;
56725
56726page1_init_failed:
56727  releasePage(pPage1);
56728  pBt->pPage1 = 0;
56729  return rc;
56730}
56731
56732#ifndef NDEBUG
56733/*
56734** Return the number of cursors open on pBt. This is for use
56735** in assert() expressions, so it is only compiled if NDEBUG is not
56736** defined.
56737**
56738** Only write cursors are counted if wrOnly is true.  If wrOnly is
56739** false then all cursors are counted.
56740**
56741** For the purposes of this routine, a cursor is any cursor that
56742** is capable of reading or writing to the database.  Cursors that
56743** have been tripped into the CURSOR_FAULT state are not counted.
56744*/
56745static int countValidCursors(BtShared *pBt, int wrOnly){
56746  BtCursor *pCur;
56747  int r = 0;
56748  for(pCur=pBt->pCursor; pCur; pCur=pCur->pNext){
56749    if( (wrOnly==0 || (pCur->curFlags & BTCF_WriteFlag)!=0)
56750     && pCur->eState!=CURSOR_FAULT ) r++;
56751  }
56752  return r;
56753}
56754#endif
56755
56756/*
56757** If there are no outstanding cursors and we are not in the middle
56758** of a transaction but there is a read lock on the database, then
56759** this routine unrefs the first page of the database file which
56760** has the effect of releasing the read lock.
56761**
56762** If there is a transaction in progress, this routine is a no-op.
56763*/
56764static void unlockBtreeIfUnused(BtShared *pBt){
56765  assert( sqlite3_mutex_held(pBt->mutex) );
56766  assert( countValidCursors(pBt,0)==0 || pBt->inTransaction>TRANS_NONE );
56767  if( pBt->inTransaction==TRANS_NONE && pBt->pPage1!=0 ){
56768    MemPage *pPage1 = pBt->pPage1;
56769    assert( pPage1->aData );
56770    assert( sqlite3PagerRefcount(pBt->pPager)==1 );
56771    pBt->pPage1 = 0;
56772    releasePageNotNull(pPage1);
56773  }
56774}
56775
56776/*
56777** If pBt points to an empty file then convert that empty file
56778** into a new empty database by initializing the first page of
56779** the database.
56780*/
56781static int newDatabase(BtShared *pBt){
56782  MemPage *pP1;
56783  unsigned char *data;
56784  int rc;
56785
56786  assert( sqlite3_mutex_held(pBt->mutex) );
56787  if( pBt->nPage>0 ){
56788    return SQLITE_OK;
56789  }
56790  pP1 = pBt->pPage1;
56791  assert( pP1!=0 );
56792  data = pP1->aData;
56793  rc = sqlite3PagerWrite(pP1->pDbPage);
56794  if( rc ) return rc;
56795  memcpy(data, zMagicHeader, sizeof(zMagicHeader));
56796  assert( sizeof(zMagicHeader)==16 );
56797  data[16] = (u8)((pBt->pageSize>>8)&0xff);
56798  data[17] = (u8)((pBt->pageSize>>16)&0xff);
56799  data[18] = 1;
56800  data[19] = 1;
56801  assert( pBt->usableSize<=pBt->pageSize && pBt->usableSize+255>=pBt->pageSize);
56802  data[20] = (u8)(pBt->pageSize - pBt->usableSize);
56803  data[21] = 64;
56804  data[22] = 32;
56805  data[23] = 32;
56806  memset(&data[24], 0, 100-24);
56807  zeroPage(pP1, PTF_INTKEY|PTF_LEAF|PTF_LEAFDATA );
56808  pBt->btsFlags |= BTS_PAGESIZE_FIXED;
56809#ifndef SQLITE_OMIT_AUTOVACUUM
56810  assert( pBt->autoVacuum==1 || pBt->autoVacuum==0 );
56811  assert( pBt->incrVacuum==1 || pBt->incrVacuum==0 );
56812  put4byte(&data[36 + 4*4], pBt->autoVacuum);
56813  put4byte(&data[36 + 7*4], pBt->incrVacuum);
56814#endif
56815  pBt->nPage = 1;
56816  data[31] = 1;
56817  return SQLITE_OK;
56818}
56819
56820/*
56821** Initialize the first page of the database file (creating a database
56822** consisting of a single page and no schema objects). Return SQLITE_OK
56823** if successful, or an SQLite error code otherwise.
56824*/
56825SQLITE_PRIVATE int sqlite3BtreeNewDb(Btree *p){
56826  int rc;
56827  sqlite3BtreeEnter(p);
56828  p->pBt->nPage = 0;
56829  rc = newDatabase(p->pBt);
56830  sqlite3BtreeLeave(p);
56831  return rc;
56832}
56833
56834/*
56835** Attempt to start a new transaction. A write-transaction
56836** is started if the second argument is nonzero, otherwise a read-
56837** transaction.  If the second argument is 2 or more and exclusive
56838** transaction is started, meaning that no other process is allowed
56839** to access the database.  A preexisting transaction may not be
56840** upgraded to exclusive by calling this routine a second time - the
56841** exclusivity flag only works for a new transaction.
56842**
56843** A write-transaction must be started before attempting any
56844** changes to the database.  None of the following routines
56845** will work unless a transaction is started first:
56846**
56847**      sqlite3BtreeCreateTable()
56848**      sqlite3BtreeCreateIndex()
56849**      sqlite3BtreeClearTable()
56850**      sqlite3BtreeDropTable()
56851**      sqlite3BtreeInsert()
56852**      sqlite3BtreeDelete()
56853**      sqlite3BtreeUpdateMeta()
56854**
56855** If an initial attempt to acquire the lock fails because of lock contention
56856** and the database was previously unlocked, then invoke the busy handler
56857** if there is one.  But if there was previously a read-lock, do not
56858** invoke the busy handler - just return SQLITE_BUSY.  SQLITE_BUSY is
56859** returned when there is already a read-lock in order to avoid a deadlock.
56860**
56861** Suppose there are two processes A and B.  A has a read lock and B has
56862** a reserved lock.  B tries to promote to exclusive but is blocked because
56863** of A's read lock.  A tries to promote to reserved but is blocked by B.
56864** One or the other of the two processes must give way or there can be
56865** no progress.  By returning SQLITE_BUSY and not invoking the busy callback
56866** when A already has a read lock, we encourage A to give up and let B
56867** proceed.
56868*/
56869SQLITE_PRIVATE int sqlite3BtreeBeginTrans(Btree *p, int wrflag){
56870  sqlite3 *pBlock = 0;
56871  BtShared *pBt = p->pBt;
56872  int rc = SQLITE_OK;
56873
56874  sqlite3BtreeEnter(p);
56875  btreeIntegrity(p);
56876
56877  /* If the btree is already in a write-transaction, or it
56878  ** is already in a read-transaction and a read-transaction
56879  ** is requested, this is a no-op.
56880  */
56881  if( p->inTrans==TRANS_WRITE || (p->inTrans==TRANS_READ && !wrflag) ){
56882    goto trans_begun;
56883  }
56884  assert( pBt->inTransaction==TRANS_WRITE || IfNotOmitAV(pBt->bDoTruncate)==0 );
56885
56886  /* Write transactions are not possible on a read-only database */
56887  if( (pBt->btsFlags & BTS_READ_ONLY)!=0 && wrflag ){
56888    rc = SQLITE_READONLY;
56889    goto trans_begun;
56890  }
56891
56892#ifndef SQLITE_OMIT_SHARED_CACHE
56893  /* If another database handle has already opened a write transaction
56894  ** on this shared-btree structure and a second write transaction is
56895  ** requested, return SQLITE_LOCKED.
56896  */
56897  if( (wrflag && pBt->inTransaction==TRANS_WRITE)
56898   || (pBt->btsFlags & BTS_PENDING)!=0
56899  ){
56900    pBlock = pBt->pWriter->db;
56901  }else if( wrflag>1 ){
56902    BtLock *pIter;
56903    for(pIter=pBt->pLock; pIter; pIter=pIter->pNext){
56904      if( pIter->pBtree!=p ){
56905        pBlock = pIter->pBtree->db;
56906        break;
56907      }
56908    }
56909  }
56910  if( pBlock ){
56911    sqlite3ConnectionBlocked(p->db, pBlock);
56912    rc = SQLITE_LOCKED_SHAREDCACHE;
56913    goto trans_begun;
56914  }
56915#endif
56916
56917  /* Any read-only or read-write transaction implies a read-lock on
56918  ** page 1. So if some other shared-cache client already has a write-lock
56919  ** on page 1, the transaction cannot be opened. */
56920  rc = querySharedCacheTableLock(p, MASTER_ROOT, READ_LOCK);
56921  if( SQLITE_OK!=rc ) goto trans_begun;
56922
56923  pBt->btsFlags &= ~BTS_INITIALLY_EMPTY;
56924  if( pBt->nPage==0 ) pBt->btsFlags |= BTS_INITIALLY_EMPTY;
56925  do {
56926    /* Call lockBtree() until either pBt->pPage1 is populated or
56927    ** lockBtree() returns something other than SQLITE_OK. lockBtree()
56928    ** may return SQLITE_OK but leave pBt->pPage1 set to 0 if after
56929    ** reading page 1 it discovers that the page-size of the database
56930    ** file is not pBt->pageSize. In this case lockBtree() will update
56931    ** pBt->pageSize to the page-size of the file on disk.
56932    */
56933    while( pBt->pPage1==0 && SQLITE_OK==(rc = lockBtree(pBt)) );
56934
56935    if( rc==SQLITE_OK && wrflag ){
56936      if( (pBt->btsFlags & BTS_READ_ONLY)!=0 ){
56937        rc = SQLITE_READONLY;
56938      }else{
56939        rc = sqlite3PagerBegin(pBt->pPager,wrflag>1,sqlite3TempInMemory(p->db));
56940        if( rc==SQLITE_OK ){
56941          rc = newDatabase(pBt);
56942        }
56943      }
56944    }
56945
56946    if( rc!=SQLITE_OK ){
56947      unlockBtreeIfUnused(pBt);
56948    }
56949  }while( (rc&0xFF)==SQLITE_BUSY && pBt->inTransaction==TRANS_NONE &&
56950          btreeInvokeBusyHandler(pBt) );
56951
56952  if( rc==SQLITE_OK ){
56953    if( p->inTrans==TRANS_NONE ){
56954      pBt->nTransaction++;
56955#ifndef SQLITE_OMIT_SHARED_CACHE
56956      if( p->sharable ){
56957        assert( p->lock.pBtree==p && p->lock.iTable==1 );
56958        p->lock.eLock = READ_LOCK;
56959        p->lock.pNext = pBt->pLock;
56960        pBt->pLock = &p->lock;
56961      }
56962#endif
56963    }
56964    p->inTrans = (wrflag?TRANS_WRITE:TRANS_READ);
56965    if( p->inTrans>pBt->inTransaction ){
56966      pBt->inTransaction = p->inTrans;
56967    }
56968    if( wrflag ){
56969      MemPage *pPage1 = pBt->pPage1;
56970#ifndef SQLITE_OMIT_SHARED_CACHE
56971      assert( !pBt->pWriter );
56972      pBt->pWriter = p;
56973      pBt->btsFlags &= ~BTS_EXCLUSIVE;
56974      if( wrflag>1 ) pBt->btsFlags |= BTS_EXCLUSIVE;
56975#endif
56976
56977      /* If the db-size header field is incorrect (as it may be if an old
56978      ** client has been writing the database file), update it now. Doing
56979      ** this sooner rather than later means the database size can safely
56980      ** re-read the database size from page 1 if a savepoint or transaction
56981      ** rollback occurs within the transaction.
56982      */
56983      if( pBt->nPage!=get4byte(&pPage1->aData[28]) ){
56984        rc = sqlite3PagerWrite(pPage1->pDbPage);
56985        if( rc==SQLITE_OK ){
56986          put4byte(&pPage1->aData[28], pBt->nPage);
56987        }
56988      }
56989    }
56990  }
56991
56992
56993trans_begun:
56994  if( rc==SQLITE_OK && wrflag ){
56995    /* This call makes sure that the pager has the correct number of
56996    ** open savepoints. If the second parameter is greater than 0 and
56997    ** the sub-journal is not already open, then it will be opened here.
56998    */
56999    rc = sqlite3PagerOpenSavepoint(pBt->pPager, p->db->nSavepoint);
57000  }
57001
57002  btreeIntegrity(p);
57003  sqlite3BtreeLeave(p);
57004  return rc;
57005}
57006
57007#ifndef SQLITE_OMIT_AUTOVACUUM
57008
57009/*
57010** Set the pointer-map entries for all children of page pPage. Also, if
57011** pPage contains cells that point to overflow pages, set the pointer
57012** map entries for the overflow pages as well.
57013*/
57014static int setChildPtrmaps(MemPage *pPage){
57015  int i;                             /* Counter variable */
57016  int nCell;                         /* Number of cells in page pPage */
57017  int rc;                            /* Return code */
57018  BtShared *pBt = pPage->pBt;
57019  u8 isInitOrig = pPage->isInit;
57020  Pgno pgno = pPage->pgno;
57021
57022  assert( sqlite3_mutex_held(pPage->pBt->mutex) );
57023  rc = btreeInitPage(pPage);
57024  if( rc!=SQLITE_OK ){
57025    goto set_child_ptrmaps_out;
57026  }
57027  nCell = pPage->nCell;
57028
57029  for(i=0; i<nCell; i++){
57030    u8 *pCell = findCell(pPage, i);
57031
57032    ptrmapPutOvflPtr(pPage, pCell, &rc);
57033
57034    if( !pPage->leaf ){
57035      Pgno childPgno = get4byte(pCell);
57036      ptrmapPut(pBt, childPgno, PTRMAP_BTREE, pgno, &rc);
57037    }
57038  }
57039
57040  if( !pPage->leaf ){
57041    Pgno childPgno = get4byte(&pPage->aData[pPage->hdrOffset+8]);
57042    ptrmapPut(pBt, childPgno, PTRMAP_BTREE, pgno, &rc);
57043  }
57044
57045set_child_ptrmaps_out:
57046  pPage->isInit = isInitOrig;
57047  return rc;
57048}
57049
57050/*
57051** Somewhere on pPage is a pointer to page iFrom.  Modify this pointer so
57052** that it points to iTo. Parameter eType describes the type of pointer to
57053** be modified, as  follows:
57054**
57055** PTRMAP_BTREE:     pPage is a btree-page. The pointer points at a child
57056**                   page of pPage.
57057**
57058** PTRMAP_OVERFLOW1: pPage is a btree-page. The pointer points at an overflow
57059**                   page pointed to by one of the cells on pPage.
57060**
57061** PTRMAP_OVERFLOW2: pPage is an overflow-page. The pointer points at the next
57062**                   overflow page in the list.
57063*/
57064static int modifyPagePointer(MemPage *pPage, Pgno iFrom, Pgno iTo, u8 eType){
57065  assert( sqlite3_mutex_held(pPage->pBt->mutex) );
57066  assert( sqlite3PagerIswriteable(pPage->pDbPage) );
57067  if( eType==PTRMAP_OVERFLOW2 ){
57068    /* The pointer is always the first 4 bytes of the page in this case.  */
57069    if( get4byte(pPage->aData)!=iFrom ){
57070      return SQLITE_CORRUPT_BKPT;
57071    }
57072    put4byte(pPage->aData, iTo);
57073  }else{
57074    u8 isInitOrig = pPage->isInit;
57075    int i;
57076    int nCell;
57077    int rc;
57078
57079    rc = btreeInitPage(pPage);
57080    if( rc ) return rc;
57081    nCell = pPage->nCell;
57082
57083    for(i=0; i<nCell; i++){
57084      u8 *pCell = findCell(pPage, i);
57085      if( eType==PTRMAP_OVERFLOW1 ){
57086        CellInfo info;
57087        pPage->xParseCell(pPage, pCell, &info);
57088        if( info.iOverflow
57089         && pCell+info.iOverflow+3<=pPage->aData+pPage->maskPage
57090         && iFrom==get4byte(&pCell[info.iOverflow])
57091        ){
57092          put4byte(&pCell[info.iOverflow], iTo);
57093          break;
57094        }
57095      }else{
57096        if( get4byte(pCell)==iFrom ){
57097          put4byte(pCell, iTo);
57098          break;
57099        }
57100      }
57101    }
57102
57103    if( i==nCell ){
57104      if( eType!=PTRMAP_BTREE ||
57105          get4byte(&pPage->aData[pPage->hdrOffset+8])!=iFrom ){
57106        return SQLITE_CORRUPT_BKPT;
57107      }
57108      put4byte(&pPage->aData[pPage->hdrOffset+8], iTo);
57109    }
57110
57111    pPage->isInit = isInitOrig;
57112  }
57113  return SQLITE_OK;
57114}
57115
57116
57117/*
57118** Move the open database page pDbPage to location iFreePage in the
57119** database. The pDbPage reference remains valid.
57120**
57121** The isCommit flag indicates that there is no need to remember that
57122** the journal needs to be sync()ed before database page pDbPage->pgno
57123** can be written to. The caller has already promised not to write to that
57124** page.
57125*/
57126static int relocatePage(
57127  BtShared *pBt,           /* Btree */
57128  MemPage *pDbPage,        /* Open page to move */
57129  u8 eType,                /* Pointer map 'type' entry for pDbPage */
57130  Pgno iPtrPage,           /* Pointer map 'page-no' entry for pDbPage */
57131  Pgno iFreePage,          /* The location to move pDbPage to */
57132  int isCommit             /* isCommit flag passed to sqlite3PagerMovepage */
57133){
57134  MemPage *pPtrPage;   /* The page that contains a pointer to pDbPage */
57135  Pgno iDbPage = pDbPage->pgno;
57136  Pager *pPager = pBt->pPager;
57137  int rc;
57138
57139  assert( eType==PTRMAP_OVERFLOW2 || eType==PTRMAP_OVERFLOW1 ||
57140      eType==PTRMAP_BTREE || eType==PTRMAP_ROOTPAGE );
57141  assert( sqlite3_mutex_held(pBt->mutex) );
57142  assert( pDbPage->pBt==pBt );
57143
57144  /* Move page iDbPage from its current location to page number iFreePage */
57145  TRACE(("AUTOVACUUM: Moving %d to free page %d (ptr page %d type %d)\n",
57146      iDbPage, iFreePage, iPtrPage, eType));
57147  rc = sqlite3PagerMovepage(pPager, pDbPage->pDbPage, iFreePage, isCommit);
57148  if( rc!=SQLITE_OK ){
57149    return rc;
57150  }
57151  pDbPage->pgno = iFreePage;
57152
57153  /* If pDbPage was a btree-page, then it may have child pages and/or cells
57154  ** that point to overflow pages. The pointer map entries for all these
57155  ** pages need to be changed.
57156  **
57157  ** If pDbPage is an overflow page, then the first 4 bytes may store a
57158  ** pointer to a subsequent overflow page. If this is the case, then
57159  ** the pointer map needs to be updated for the subsequent overflow page.
57160  */
57161  if( eType==PTRMAP_BTREE || eType==PTRMAP_ROOTPAGE ){
57162    rc = setChildPtrmaps(pDbPage);
57163    if( rc!=SQLITE_OK ){
57164      return rc;
57165    }
57166  }else{
57167    Pgno nextOvfl = get4byte(pDbPage->aData);
57168    if( nextOvfl!=0 ){
57169      ptrmapPut(pBt, nextOvfl, PTRMAP_OVERFLOW2, iFreePage, &rc);
57170      if( rc!=SQLITE_OK ){
57171        return rc;
57172      }
57173    }
57174  }
57175
57176  /* Fix the database pointer on page iPtrPage that pointed at iDbPage so
57177  ** that it points at iFreePage. Also fix the pointer map entry for
57178  ** iPtrPage.
57179  */
57180  if( eType!=PTRMAP_ROOTPAGE ){
57181    rc = btreeGetPage(pBt, iPtrPage, &pPtrPage, 0);
57182    if( rc!=SQLITE_OK ){
57183      return rc;
57184    }
57185    rc = sqlite3PagerWrite(pPtrPage->pDbPage);
57186    if( rc!=SQLITE_OK ){
57187      releasePage(pPtrPage);
57188      return rc;
57189    }
57190    rc = modifyPagePointer(pPtrPage, iDbPage, iFreePage, eType);
57191    releasePage(pPtrPage);
57192    if( rc==SQLITE_OK ){
57193      ptrmapPut(pBt, iFreePage, eType, iPtrPage, &rc);
57194    }
57195  }
57196  return rc;
57197}
57198
57199/* Forward declaration required by incrVacuumStep(). */
57200static int allocateBtreePage(BtShared *, MemPage **, Pgno *, Pgno, u8);
57201
57202/*
57203** Perform a single step of an incremental-vacuum. If successful, return
57204** SQLITE_OK. If there is no work to do (and therefore no point in
57205** calling this function again), return SQLITE_DONE. Or, if an error
57206** occurs, return some other error code.
57207**
57208** More specifically, this function attempts to re-organize the database so
57209** that the last page of the file currently in use is no longer in use.
57210**
57211** Parameter nFin is the number of pages that this database would contain
57212** were this function called until it returns SQLITE_DONE.
57213**
57214** If the bCommit parameter is non-zero, this function assumes that the
57215** caller will keep calling incrVacuumStep() until it returns SQLITE_DONE
57216** or an error. bCommit is passed true for an auto-vacuum-on-commit
57217** operation, or false for an incremental vacuum.
57218*/
57219static int incrVacuumStep(BtShared *pBt, Pgno nFin, Pgno iLastPg, int bCommit){
57220  Pgno nFreeList;           /* Number of pages still on the free-list */
57221  int rc;
57222
57223  assert( sqlite3_mutex_held(pBt->mutex) );
57224  assert( iLastPg>nFin );
57225
57226  if( !PTRMAP_ISPAGE(pBt, iLastPg) && iLastPg!=PENDING_BYTE_PAGE(pBt) ){
57227    u8 eType;
57228    Pgno iPtrPage;
57229
57230    nFreeList = get4byte(&pBt->pPage1->aData[36]);
57231    if( nFreeList==0 ){
57232      return SQLITE_DONE;
57233    }
57234
57235    rc = ptrmapGet(pBt, iLastPg, &eType, &iPtrPage);
57236    if( rc!=SQLITE_OK ){
57237      return rc;
57238    }
57239    if( eType==PTRMAP_ROOTPAGE ){
57240      return SQLITE_CORRUPT_BKPT;
57241    }
57242
57243    if( eType==PTRMAP_FREEPAGE ){
57244      if( bCommit==0 ){
57245        /* Remove the page from the files free-list. This is not required
57246        ** if bCommit is non-zero. In that case, the free-list will be
57247        ** truncated to zero after this function returns, so it doesn't
57248        ** matter if it still contains some garbage entries.
57249        */
57250        Pgno iFreePg;
57251        MemPage *pFreePg;
57252        rc = allocateBtreePage(pBt, &pFreePg, &iFreePg, iLastPg, BTALLOC_EXACT);
57253        if( rc!=SQLITE_OK ){
57254          return rc;
57255        }
57256        assert( iFreePg==iLastPg );
57257        releasePage(pFreePg);
57258      }
57259    } else {
57260      Pgno iFreePg;             /* Index of free page to move pLastPg to */
57261      MemPage *pLastPg;
57262      u8 eMode = BTALLOC_ANY;   /* Mode parameter for allocateBtreePage() */
57263      Pgno iNear = 0;           /* nearby parameter for allocateBtreePage() */
57264
57265      rc = btreeGetPage(pBt, iLastPg, &pLastPg, 0);
57266      if( rc!=SQLITE_OK ){
57267        return rc;
57268      }
57269
57270      /* If bCommit is zero, this loop runs exactly once and page pLastPg
57271      ** is swapped with the first free page pulled off the free list.
57272      **
57273      ** On the other hand, if bCommit is greater than zero, then keep
57274      ** looping until a free-page located within the first nFin pages
57275      ** of the file is found.
57276      */
57277      if( bCommit==0 ){
57278        eMode = BTALLOC_LE;
57279        iNear = nFin;
57280      }
57281      do {
57282        MemPage *pFreePg;
57283        rc = allocateBtreePage(pBt, &pFreePg, &iFreePg, iNear, eMode);
57284        if( rc!=SQLITE_OK ){
57285          releasePage(pLastPg);
57286          return rc;
57287        }
57288        releasePage(pFreePg);
57289      }while( bCommit && iFreePg>nFin );
57290      assert( iFreePg<iLastPg );
57291
57292      rc = relocatePage(pBt, pLastPg, eType, iPtrPage, iFreePg, bCommit);
57293      releasePage(pLastPg);
57294      if( rc!=SQLITE_OK ){
57295        return rc;
57296      }
57297    }
57298  }
57299
57300  if( bCommit==0 ){
57301    do {
57302      iLastPg--;
57303    }while( iLastPg==PENDING_BYTE_PAGE(pBt) || PTRMAP_ISPAGE(pBt, iLastPg) );
57304    pBt->bDoTruncate = 1;
57305    pBt->nPage = iLastPg;
57306  }
57307  return SQLITE_OK;
57308}
57309
57310/*
57311** The database opened by the first argument is an auto-vacuum database
57312** nOrig pages in size containing nFree free pages. Return the expected
57313** size of the database in pages following an auto-vacuum operation.
57314*/
57315static Pgno finalDbSize(BtShared *pBt, Pgno nOrig, Pgno nFree){
57316  int nEntry;                     /* Number of entries on one ptrmap page */
57317  Pgno nPtrmap;                   /* Number of PtrMap pages to be freed */
57318  Pgno nFin;                      /* Return value */
57319
57320  nEntry = pBt->usableSize/5;
57321  nPtrmap = (nFree-nOrig+PTRMAP_PAGENO(pBt, nOrig)+nEntry)/nEntry;
57322  nFin = nOrig - nFree - nPtrmap;
57323  if( nOrig>PENDING_BYTE_PAGE(pBt) && nFin<PENDING_BYTE_PAGE(pBt) ){
57324    nFin--;
57325  }
57326  while( PTRMAP_ISPAGE(pBt, nFin) || nFin==PENDING_BYTE_PAGE(pBt) ){
57327    nFin--;
57328  }
57329
57330  return nFin;
57331}
57332
57333/*
57334** A write-transaction must be opened before calling this function.
57335** It performs a single unit of work towards an incremental vacuum.
57336**
57337** If the incremental vacuum is finished after this function has run,
57338** SQLITE_DONE is returned. If it is not finished, but no error occurred,
57339** SQLITE_OK is returned. Otherwise an SQLite error code.
57340*/
57341SQLITE_PRIVATE int sqlite3BtreeIncrVacuum(Btree *p){
57342  int rc;
57343  BtShared *pBt = p->pBt;
57344
57345  sqlite3BtreeEnter(p);
57346  assert( pBt->inTransaction==TRANS_WRITE && p->inTrans==TRANS_WRITE );
57347  if( !pBt->autoVacuum ){
57348    rc = SQLITE_DONE;
57349  }else{
57350    Pgno nOrig = btreePagecount(pBt);
57351    Pgno nFree = get4byte(&pBt->pPage1->aData[36]);
57352    Pgno nFin = finalDbSize(pBt, nOrig, nFree);
57353
57354    if( nOrig<nFin ){
57355      rc = SQLITE_CORRUPT_BKPT;
57356    }else if( nFree>0 ){
57357      rc = saveAllCursors(pBt, 0, 0);
57358      if( rc==SQLITE_OK ){
57359        invalidateAllOverflowCache(pBt);
57360        rc = incrVacuumStep(pBt, nFin, nOrig, 0);
57361      }
57362      if( rc==SQLITE_OK ){
57363        rc = sqlite3PagerWrite(pBt->pPage1->pDbPage);
57364        put4byte(&pBt->pPage1->aData[28], pBt->nPage);
57365      }
57366    }else{
57367      rc = SQLITE_DONE;
57368    }
57369  }
57370  sqlite3BtreeLeave(p);
57371  return rc;
57372}
57373
57374/*
57375** This routine is called prior to sqlite3PagerCommit when a transaction
57376** is committed for an auto-vacuum database.
57377**
57378** If SQLITE_OK is returned, then *pnTrunc is set to the number of pages
57379** the database file should be truncated to during the commit process.
57380** i.e. the database has been reorganized so that only the first *pnTrunc
57381** pages are in use.
57382*/
57383static int autoVacuumCommit(BtShared *pBt){
57384  int rc = SQLITE_OK;
57385  Pager *pPager = pBt->pPager;
57386  VVA_ONLY( int nRef = sqlite3PagerRefcount(pPager); )
57387
57388  assert( sqlite3_mutex_held(pBt->mutex) );
57389  invalidateAllOverflowCache(pBt);
57390  assert(pBt->autoVacuum);
57391  if( !pBt->incrVacuum ){
57392    Pgno nFin;         /* Number of pages in database after autovacuuming */
57393    Pgno nFree;        /* Number of pages on the freelist initially */
57394    Pgno iFree;        /* The next page to be freed */
57395    Pgno nOrig;        /* Database size before freeing */
57396
57397    nOrig = btreePagecount(pBt);
57398    if( PTRMAP_ISPAGE(pBt, nOrig) || nOrig==PENDING_BYTE_PAGE(pBt) ){
57399      /* It is not possible to create a database for which the final page
57400      ** is either a pointer-map page or the pending-byte page. If one
57401      ** is encountered, this indicates corruption.
57402      */
57403      return SQLITE_CORRUPT_BKPT;
57404    }
57405
57406    nFree = get4byte(&pBt->pPage1->aData[36]);
57407    nFin = finalDbSize(pBt, nOrig, nFree);
57408    if( nFin>nOrig ) return SQLITE_CORRUPT_BKPT;
57409    if( nFin<nOrig ){
57410      rc = saveAllCursors(pBt, 0, 0);
57411    }
57412    for(iFree=nOrig; iFree>nFin && rc==SQLITE_OK; iFree--){
57413      rc = incrVacuumStep(pBt, nFin, iFree, 1);
57414    }
57415    if( (rc==SQLITE_DONE || rc==SQLITE_OK) && nFree>0 ){
57416      rc = sqlite3PagerWrite(pBt->pPage1->pDbPage);
57417      put4byte(&pBt->pPage1->aData[32], 0);
57418      put4byte(&pBt->pPage1->aData[36], 0);
57419      put4byte(&pBt->pPage1->aData[28], nFin);
57420      pBt->bDoTruncate = 1;
57421      pBt->nPage = nFin;
57422    }
57423    if( rc!=SQLITE_OK ){
57424      sqlite3PagerRollback(pPager);
57425    }
57426  }
57427
57428  assert( nRef>=sqlite3PagerRefcount(pPager) );
57429  return rc;
57430}
57431
57432#else /* ifndef SQLITE_OMIT_AUTOVACUUM */
57433# define setChildPtrmaps(x) SQLITE_OK
57434#endif
57435
57436/*
57437** This routine does the first phase of a two-phase commit.  This routine
57438** causes a rollback journal to be created (if it does not already exist)
57439** and populated with enough information so that if a power loss occurs
57440** the database can be restored to its original state by playing back
57441** the journal.  Then the contents of the journal are flushed out to
57442** the disk.  After the journal is safely on oxide, the changes to the
57443** database are written into the database file and flushed to oxide.
57444** At the end of this call, the rollback journal still exists on the
57445** disk and we are still holding all locks, so the transaction has not
57446** committed.  See sqlite3BtreeCommitPhaseTwo() for the second phase of the
57447** commit process.
57448**
57449** This call is a no-op if no write-transaction is currently active on pBt.
57450**
57451** Otherwise, sync the database file for the btree pBt. zMaster points to
57452** the name of a master journal file that should be written into the
57453** individual journal file, or is NULL, indicating no master journal file
57454** (single database transaction).
57455**
57456** When this is called, the master journal should already have been
57457** created, populated with this journal pointer and synced to disk.
57458**
57459** Once this is routine has returned, the only thing required to commit
57460** the write-transaction for this database file is to delete the journal.
57461*/
57462SQLITE_PRIVATE int sqlite3BtreeCommitPhaseOne(Btree *p, const char *zMaster){
57463  int rc = SQLITE_OK;
57464  if( p->inTrans==TRANS_WRITE ){
57465    BtShared *pBt = p->pBt;
57466    sqlite3BtreeEnter(p);
57467#ifndef SQLITE_OMIT_AUTOVACUUM
57468    if( pBt->autoVacuum ){
57469      rc = autoVacuumCommit(pBt);
57470      if( rc!=SQLITE_OK ){
57471        sqlite3BtreeLeave(p);
57472        return rc;
57473      }
57474    }
57475    if( pBt->bDoTruncate ){
57476      sqlite3PagerTruncateImage(pBt->pPager, pBt->nPage);
57477    }
57478#endif
57479    rc = sqlite3PagerCommitPhaseOne(pBt->pPager, zMaster, 0);
57480    sqlite3BtreeLeave(p);
57481  }
57482  return rc;
57483}
57484
57485/*
57486** This function is called from both BtreeCommitPhaseTwo() and BtreeRollback()
57487** at the conclusion of a transaction.
57488*/
57489static void btreeEndTransaction(Btree *p){
57490  BtShared *pBt = p->pBt;
57491  sqlite3 *db = p->db;
57492  assert( sqlite3BtreeHoldsMutex(p) );
57493
57494#ifndef SQLITE_OMIT_AUTOVACUUM
57495  pBt->bDoTruncate = 0;
57496#endif
57497  if( p->inTrans>TRANS_NONE && db->nVdbeRead>1 ){
57498    /* If there are other active statements that belong to this database
57499    ** handle, downgrade to a read-only transaction. The other statements
57500    ** may still be reading from the database.  */
57501    downgradeAllSharedCacheTableLocks(p);
57502    p->inTrans = TRANS_READ;
57503  }else{
57504    /* If the handle had any kind of transaction open, decrement the
57505    ** transaction count of the shared btree. If the transaction count
57506    ** reaches 0, set the shared state to TRANS_NONE. The unlockBtreeIfUnused()
57507    ** call below will unlock the pager.  */
57508    if( p->inTrans!=TRANS_NONE ){
57509      clearAllSharedCacheTableLocks(p);
57510      pBt->nTransaction--;
57511      if( 0==pBt->nTransaction ){
57512        pBt->inTransaction = TRANS_NONE;
57513      }
57514    }
57515
57516    /* Set the current transaction state to TRANS_NONE and unlock the
57517    ** pager if this call closed the only read or write transaction.  */
57518    p->inTrans = TRANS_NONE;
57519    unlockBtreeIfUnused(pBt);
57520  }
57521
57522  btreeIntegrity(p);
57523}
57524
57525/*
57526** Commit the transaction currently in progress.
57527**
57528** This routine implements the second phase of a 2-phase commit.  The
57529** sqlite3BtreeCommitPhaseOne() routine does the first phase and should
57530** be invoked prior to calling this routine.  The sqlite3BtreeCommitPhaseOne()
57531** routine did all the work of writing information out to disk and flushing the
57532** contents so that they are written onto the disk platter.  All this
57533** routine has to do is delete or truncate or zero the header in the
57534** the rollback journal (which causes the transaction to commit) and
57535** drop locks.
57536**
57537** Normally, if an error occurs while the pager layer is attempting to
57538** finalize the underlying journal file, this function returns an error and
57539** the upper layer will attempt a rollback. However, if the second argument
57540** is non-zero then this b-tree transaction is part of a multi-file
57541** transaction. In this case, the transaction has already been committed
57542** (by deleting a master journal file) and the caller will ignore this
57543** functions return code. So, even if an error occurs in the pager layer,
57544** reset the b-tree objects internal state to indicate that the write
57545** transaction has been closed. This is quite safe, as the pager will have
57546** transitioned to the error state.
57547**
57548** This will release the write lock on the database file.  If there
57549** are no active cursors, it also releases the read lock.
57550*/
57551SQLITE_PRIVATE int sqlite3BtreeCommitPhaseTwo(Btree *p, int bCleanup){
57552
57553  if( p->inTrans==TRANS_NONE ) return SQLITE_OK;
57554  sqlite3BtreeEnter(p);
57555  btreeIntegrity(p);
57556
57557  /* If the handle has a write-transaction open, commit the shared-btrees
57558  ** transaction and set the shared state to TRANS_READ.
57559  */
57560  if( p->inTrans==TRANS_WRITE ){
57561    int rc;
57562    BtShared *pBt = p->pBt;
57563    assert( pBt->inTransaction==TRANS_WRITE );
57564    assert( pBt->nTransaction>0 );
57565    rc = sqlite3PagerCommitPhaseTwo(pBt->pPager);
57566    if( rc!=SQLITE_OK && bCleanup==0 ){
57567      sqlite3BtreeLeave(p);
57568      return rc;
57569    }
57570    p->iDataVersion--;  /* Compensate for pPager->iDataVersion++; */
57571    pBt->inTransaction = TRANS_READ;
57572    btreeClearHasContent(pBt);
57573  }
57574
57575  btreeEndTransaction(p);
57576  sqlite3BtreeLeave(p);
57577  return SQLITE_OK;
57578}
57579
57580/*
57581** Do both phases of a commit.
57582*/
57583SQLITE_PRIVATE int sqlite3BtreeCommit(Btree *p){
57584  int rc;
57585  sqlite3BtreeEnter(p);
57586  rc = sqlite3BtreeCommitPhaseOne(p, 0);
57587  if( rc==SQLITE_OK ){
57588    rc = sqlite3BtreeCommitPhaseTwo(p, 0);
57589  }
57590  sqlite3BtreeLeave(p);
57591  return rc;
57592}
57593
57594/*
57595** This routine sets the state to CURSOR_FAULT and the error
57596** code to errCode for every cursor on any BtShared that pBtree
57597** references.  Or if the writeOnly flag is set to 1, then only
57598** trip write cursors and leave read cursors unchanged.
57599**
57600** Every cursor is a candidate to be tripped, including cursors
57601** that belong to other database connections that happen to be
57602** sharing the cache with pBtree.
57603**
57604** This routine gets called when a rollback occurs. If the writeOnly
57605** flag is true, then only write-cursors need be tripped - read-only
57606** cursors save their current positions so that they may continue
57607** following the rollback. Or, if writeOnly is false, all cursors are
57608** tripped. In general, writeOnly is false if the transaction being
57609** rolled back modified the database schema. In this case b-tree root
57610** pages may be moved or deleted from the database altogether, making
57611** it unsafe for read cursors to continue.
57612**
57613** If the writeOnly flag is true and an error is encountered while
57614** saving the current position of a read-only cursor, all cursors,
57615** including all read-cursors are tripped.
57616**
57617** SQLITE_OK is returned if successful, or if an error occurs while
57618** saving a cursor position, an SQLite error code.
57619*/
57620SQLITE_PRIVATE int sqlite3BtreeTripAllCursors(Btree *pBtree, int errCode, int writeOnly){
57621  BtCursor *p;
57622  int rc = SQLITE_OK;
57623
57624  assert( (writeOnly==0 || writeOnly==1) && BTCF_WriteFlag==1 );
57625  if( pBtree ){
57626    sqlite3BtreeEnter(pBtree);
57627    for(p=pBtree->pBt->pCursor; p; p=p->pNext){
57628      int i;
57629      if( writeOnly && (p->curFlags & BTCF_WriteFlag)==0 ){
57630        if( p->eState==CURSOR_VALID || p->eState==CURSOR_SKIPNEXT ){
57631          rc = saveCursorPosition(p);
57632          if( rc!=SQLITE_OK ){
57633            (void)sqlite3BtreeTripAllCursors(pBtree, rc, 0);
57634            break;
57635          }
57636        }
57637      }else{
57638        sqlite3BtreeClearCursor(p);
57639        p->eState = CURSOR_FAULT;
57640        p->skipNext = errCode;
57641      }
57642      for(i=0; i<=p->iPage; i++){
57643        releasePage(p->apPage[i]);
57644        p->apPage[i] = 0;
57645      }
57646    }
57647    sqlite3BtreeLeave(pBtree);
57648  }
57649  return rc;
57650}
57651
57652/*
57653** Rollback the transaction in progress.
57654**
57655** If tripCode is not SQLITE_OK then cursors will be invalidated (tripped).
57656** Only write cursors are tripped if writeOnly is true but all cursors are
57657** tripped if writeOnly is false.  Any attempt to use
57658** a tripped cursor will result in an error.
57659**
57660** This will release the write lock on the database file.  If there
57661** are no active cursors, it also releases the read lock.
57662*/
57663SQLITE_PRIVATE int sqlite3BtreeRollback(Btree *p, int tripCode, int writeOnly){
57664  int rc;
57665  BtShared *pBt = p->pBt;
57666  MemPage *pPage1;
57667
57668  assert( writeOnly==1 || writeOnly==0 );
57669  assert( tripCode==SQLITE_ABORT_ROLLBACK || tripCode==SQLITE_OK );
57670  sqlite3BtreeEnter(p);
57671  if( tripCode==SQLITE_OK ){
57672    rc = tripCode = saveAllCursors(pBt, 0, 0);
57673    if( rc ) writeOnly = 0;
57674  }else{
57675    rc = SQLITE_OK;
57676  }
57677  if( tripCode ){
57678    int rc2 = sqlite3BtreeTripAllCursors(p, tripCode, writeOnly);
57679    assert( rc==SQLITE_OK || (writeOnly==0 && rc2==SQLITE_OK) );
57680    if( rc2!=SQLITE_OK ) rc = rc2;
57681  }
57682  btreeIntegrity(p);
57683
57684  if( p->inTrans==TRANS_WRITE ){
57685    int rc2;
57686
57687    assert( TRANS_WRITE==pBt->inTransaction );
57688    rc2 = sqlite3PagerRollback(pBt->pPager);
57689    if( rc2!=SQLITE_OK ){
57690      rc = rc2;
57691    }
57692
57693    /* The rollback may have destroyed the pPage1->aData value.  So
57694    ** call btreeGetPage() on page 1 again to make
57695    ** sure pPage1->aData is set correctly. */
57696    if( btreeGetPage(pBt, 1, &pPage1, 0)==SQLITE_OK ){
57697      int nPage = get4byte(28+(u8*)pPage1->aData);
57698      testcase( nPage==0 );
57699      if( nPage==0 ) sqlite3PagerPagecount(pBt->pPager, &nPage);
57700      testcase( pBt->nPage!=nPage );
57701      pBt->nPage = nPage;
57702      releasePage(pPage1);
57703    }
57704    assert( countValidCursors(pBt, 1)==0 );
57705    pBt->inTransaction = TRANS_READ;
57706    btreeClearHasContent(pBt);
57707  }
57708
57709  btreeEndTransaction(p);
57710  sqlite3BtreeLeave(p);
57711  return rc;
57712}
57713
57714/*
57715** Start a statement subtransaction. The subtransaction can be rolled
57716** back independently of the main transaction. You must start a transaction
57717** before starting a subtransaction. The subtransaction is ended automatically
57718** if the main transaction commits or rolls back.
57719**
57720** Statement subtransactions are used around individual SQL statements
57721** that are contained within a BEGIN...COMMIT block.  If a constraint
57722** error occurs within the statement, the effect of that one statement
57723** can be rolled back without having to rollback the entire transaction.
57724**
57725** A statement sub-transaction is implemented as an anonymous savepoint. The
57726** value passed as the second parameter is the total number of savepoints,
57727** including the new anonymous savepoint, open on the B-Tree. i.e. if there
57728** are no active savepoints and no other statement-transactions open,
57729** iStatement is 1. This anonymous savepoint can be released or rolled back
57730** using the sqlite3BtreeSavepoint() function.
57731*/
57732SQLITE_PRIVATE int sqlite3BtreeBeginStmt(Btree *p, int iStatement){
57733  int rc;
57734  BtShared *pBt = p->pBt;
57735  sqlite3BtreeEnter(p);
57736  assert( p->inTrans==TRANS_WRITE );
57737  assert( (pBt->btsFlags & BTS_READ_ONLY)==0 );
57738  assert( iStatement>0 );
57739  assert( iStatement>p->db->nSavepoint );
57740  assert( pBt->inTransaction==TRANS_WRITE );
57741  /* At the pager level, a statement transaction is a savepoint with
57742  ** an index greater than all savepoints created explicitly using
57743  ** SQL statements. It is illegal to open, release or rollback any
57744  ** such savepoints while the statement transaction savepoint is active.
57745  */
57746  rc = sqlite3PagerOpenSavepoint(pBt->pPager, iStatement);
57747  sqlite3BtreeLeave(p);
57748  return rc;
57749}
57750
57751/*
57752** The second argument to this function, op, is always SAVEPOINT_ROLLBACK
57753** or SAVEPOINT_RELEASE. This function either releases or rolls back the
57754** savepoint identified by parameter iSavepoint, depending on the value
57755** of op.
57756**
57757** Normally, iSavepoint is greater than or equal to zero. However, if op is
57758** SAVEPOINT_ROLLBACK, then iSavepoint may also be -1. In this case the
57759** contents of the entire transaction are rolled back. This is different
57760** from a normal transaction rollback, as no locks are released and the
57761** transaction remains open.
57762*/
57763SQLITE_PRIVATE int sqlite3BtreeSavepoint(Btree *p, int op, int iSavepoint){
57764  int rc = SQLITE_OK;
57765  if( p && p->inTrans==TRANS_WRITE ){
57766    BtShared *pBt = p->pBt;
57767    assert( op==SAVEPOINT_RELEASE || op==SAVEPOINT_ROLLBACK );
57768    assert( iSavepoint>=0 || (iSavepoint==-1 && op==SAVEPOINT_ROLLBACK) );
57769    sqlite3BtreeEnter(p);
57770    rc = sqlite3PagerSavepoint(pBt->pPager, op, iSavepoint);
57771    if( rc==SQLITE_OK ){
57772      if( iSavepoint<0 && (pBt->btsFlags & BTS_INITIALLY_EMPTY)!=0 ){
57773        pBt->nPage = 0;
57774      }
57775      rc = newDatabase(pBt);
57776      pBt->nPage = get4byte(28 + pBt->pPage1->aData);
57777
57778      /* The database size was written into the offset 28 of the header
57779      ** when the transaction started, so we know that the value at offset
57780      ** 28 is nonzero. */
57781      assert( pBt->nPage>0 );
57782    }
57783    sqlite3BtreeLeave(p);
57784  }
57785  return rc;
57786}
57787
57788/*
57789** Create a new cursor for the BTree whose root is on the page
57790** iTable. If a read-only cursor is requested, it is assumed that
57791** the caller already has at least a read-only transaction open
57792** on the database already. If a write-cursor is requested, then
57793** the caller is assumed to have an open write transaction.
57794**
57795** If wrFlag==0, then the cursor can only be used for reading.
57796** If wrFlag==1, then the cursor can be used for reading or for
57797** writing if other conditions for writing are also met.  These
57798** are the conditions that must be met in order for writing to
57799** be allowed:
57800**
57801** 1:  The cursor must have been opened with wrFlag==1
57802**
57803** 2:  Other database connections that share the same pager cache
57804**     but which are not in the READ_UNCOMMITTED state may not have
57805**     cursors open with wrFlag==0 on the same table.  Otherwise
57806**     the changes made by this write cursor would be visible to
57807**     the read cursors in the other database connection.
57808**
57809** 3:  The database must be writable (not on read-only media)
57810**
57811** 4:  There must be an active transaction.
57812**
57813** No checking is done to make sure that page iTable really is the
57814** root page of a b-tree.  If it is not, then the cursor acquired
57815** will not work correctly.
57816**
57817** It is assumed that the sqlite3BtreeCursorZero() has been called
57818** on pCur to initialize the memory space prior to invoking this routine.
57819*/
57820static int btreeCursor(
57821  Btree *p,                              /* The btree */
57822  int iTable,                            /* Root page of table to open */
57823  int wrFlag,                            /* 1 to write. 0 read-only */
57824  struct KeyInfo *pKeyInfo,              /* First arg to comparison function */
57825  BtCursor *pCur                         /* Space for new cursor */
57826){
57827  BtShared *pBt = p->pBt;                /* Shared b-tree handle */
57828  BtCursor *pX;                          /* Looping over other all cursors */
57829
57830  assert( sqlite3BtreeHoldsMutex(p) );
57831  assert( wrFlag==0 || wrFlag==1 );
57832
57833  /* The following assert statements verify that if this is a sharable
57834  ** b-tree database, the connection is holding the required table locks,
57835  ** and that no other connection has any open cursor that conflicts with
57836  ** this lock.  */
57837  assert( hasSharedCacheTableLock(p, iTable, pKeyInfo!=0, wrFlag+1) );
57838  assert( wrFlag==0 || !hasReadConflicts(p, iTable) );
57839
57840  /* Assert that the caller has opened the required transaction. */
57841  assert( p->inTrans>TRANS_NONE );
57842  assert( wrFlag==0 || p->inTrans==TRANS_WRITE );
57843  assert( pBt->pPage1 && pBt->pPage1->aData );
57844  assert( wrFlag==0 || (pBt->btsFlags & BTS_READ_ONLY)==0 );
57845
57846  if( wrFlag ){
57847    allocateTempSpace(pBt);
57848    if( pBt->pTmpSpace==0 ) return SQLITE_NOMEM;
57849  }
57850  if( iTable==1 && btreePagecount(pBt)==0 ){
57851    assert( wrFlag==0 );
57852    iTable = 0;
57853  }
57854
57855  /* Now that no other errors can occur, finish filling in the BtCursor
57856  ** variables and link the cursor into the BtShared list.  */
57857  pCur->pgnoRoot = (Pgno)iTable;
57858  pCur->iPage = -1;
57859  pCur->pKeyInfo = pKeyInfo;
57860  pCur->pBtree = p;
57861  pCur->pBt = pBt;
57862  assert( wrFlag==0 || wrFlag==BTCF_WriteFlag );
57863  pCur->curFlags = wrFlag;
57864  pCur->curPagerFlags = wrFlag ? 0 : PAGER_GET_READONLY;
57865  /* If there are two or more cursors on the same btree, then all such
57866  ** cursors *must* have the BTCF_Multiple flag set. */
57867  for(pX=pBt->pCursor; pX; pX=pX->pNext){
57868    if( pX->pgnoRoot==(Pgno)iTable ){
57869      pX->curFlags |= BTCF_Multiple;
57870      pCur->curFlags |= BTCF_Multiple;
57871    }
57872  }
57873  pCur->pNext = pBt->pCursor;
57874  pBt->pCursor = pCur;
57875  pCur->eState = CURSOR_INVALID;
57876  return SQLITE_OK;
57877}
57878SQLITE_PRIVATE int sqlite3BtreeCursor(
57879  Btree *p,                                   /* The btree */
57880  int iTable,                                 /* Root page of table to open */
57881  int wrFlag,                                 /* 1 to write. 0 read-only */
57882  struct KeyInfo *pKeyInfo,                   /* First arg to xCompare() */
57883  BtCursor *pCur                              /* Write new cursor here */
57884){
57885  int rc;
57886  if( iTable<1 ){
57887    rc = SQLITE_CORRUPT_BKPT;
57888  }else{
57889    sqlite3BtreeEnter(p);
57890    rc = btreeCursor(p, iTable, wrFlag, pKeyInfo, pCur);
57891    sqlite3BtreeLeave(p);
57892  }
57893  return rc;
57894}
57895
57896/*
57897** Return the size of a BtCursor object in bytes.
57898**
57899** This interfaces is needed so that users of cursors can preallocate
57900** sufficient storage to hold a cursor.  The BtCursor object is opaque
57901** to users so they cannot do the sizeof() themselves - they must call
57902** this routine.
57903*/
57904SQLITE_PRIVATE int sqlite3BtreeCursorSize(void){
57905  return ROUND8(sizeof(BtCursor));
57906}
57907
57908/*
57909** Initialize memory that will be converted into a BtCursor object.
57910**
57911** The simple approach here would be to memset() the entire object
57912** to zero.  But it turns out that the apPage[] and aiIdx[] arrays
57913** do not need to be zeroed and they are large, so we can save a lot
57914** of run-time by skipping the initialization of those elements.
57915*/
57916SQLITE_PRIVATE void sqlite3BtreeCursorZero(BtCursor *p){
57917  memset(p, 0, offsetof(BtCursor, iPage));
57918}
57919
57920/*
57921** Close a cursor.  The read lock on the database file is released
57922** when the last cursor is closed.
57923*/
57924SQLITE_PRIVATE int sqlite3BtreeCloseCursor(BtCursor *pCur){
57925  Btree *pBtree = pCur->pBtree;
57926  if( pBtree ){
57927    int i;
57928    BtShared *pBt = pCur->pBt;
57929    sqlite3BtreeEnter(pBtree);
57930    sqlite3BtreeClearCursor(pCur);
57931    assert( pBt->pCursor!=0 );
57932    if( pBt->pCursor==pCur ){
57933      pBt->pCursor = pCur->pNext;
57934    }else{
57935      BtCursor *pPrev = pBt->pCursor;
57936      do{
57937        if( pPrev->pNext==pCur ){
57938          pPrev->pNext = pCur->pNext;
57939          break;
57940        }
57941        pPrev = pPrev->pNext;
57942      }while( ALWAYS(pPrev) );
57943    }
57944    for(i=0; i<=pCur->iPage; i++){
57945      releasePage(pCur->apPage[i]);
57946    }
57947    unlockBtreeIfUnused(pBt);
57948    sqlite3_free(pCur->aOverflow);
57949    /* sqlite3_free(pCur); */
57950    sqlite3BtreeLeave(pBtree);
57951  }
57952  return SQLITE_OK;
57953}
57954
57955/*
57956** Make sure the BtCursor* given in the argument has a valid
57957** BtCursor.info structure.  If it is not already valid, call
57958** btreeParseCell() to fill it in.
57959**
57960** BtCursor.info is a cache of the information in the current cell.
57961** Using this cache reduces the number of calls to btreeParseCell().
57962*/
57963#ifndef NDEBUG
57964  static void assertCellInfo(BtCursor *pCur){
57965    CellInfo info;
57966    int iPage = pCur->iPage;
57967    memset(&info, 0, sizeof(info));
57968    btreeParseCell(pCur->apPage[iPage], pCur->aiIdx[iPage], &info);
57969    assert( CORRUPT_DB || memcmp(&info, &pCur->info, sizeof(info))==0 );
57970  }
57971#else
57972  #define assertCellInfo(x)
57973#endif
57974static SQLITE_NOINLINE void getCellInfo(BtCursor *pCur){
57975  if( pCur->info.nSize==0 ){
57976    int iPage = pCur->iPage;
57977    pCur->curFlags |= BTCF_ValidNKey;
57978    btreeParseCell(pCur->apPage[iPage],pCur->aiIdx[iPage],&pCur->info);
57979  }else{
57980    assertCellInfo(pCur);
57981  }
57982}
57983
57984#ifndef NDEBUG  /* The next routine used only within assert() statements */
57985/*
57986** Return true if the given BtCursor is valid.  A valid cursor is one
57987** that is currently pointing to a row in a (non-empty) table.
57988** This is a verification routine is used only within assert() statements.
57989*/
57990SQLITE_PRIVATE int sqlite3BtreeCursorIsValid(BtCursor *pCur){
57991  return pCur && pCur->eState==CURSOR_VALID;
57992}
57993#endif /* NDEBUG */
57994
57995/*
57996** Set *pSize to the size of the buffer needed to hold the value of
57997** the key for the current entry.  If the cursor is not pointing
57998** to a valid entry, *pSize is set to 0.
57999**
58000** For a table with the INTKEY flag set, this routine returns the key
58001** itself, not the number of bytes in the key.
58002**
58003** The caller must position the cursor prior to invoking this routine.
58004**
58005** This routine cannot fail.  It always returns SQLITE_OK.
58006*/
58007SQLITE_PRIVATE int sqlite3BtreeKeySize(BtCursor *pCur, i64 *pSize){
58008  assert( cursorHoldsMutex(pCur) );
58009  assert( pCur->eState==CURSOR_VALID );
58010  getCellInfo(pCur);
58011  *pSize = pCur->info.nKey;
58012  return SQLITE_OK;
58013}
58014
58015/*
58016** Set *pSize to the number of bytes of data in the entry the
58017** cursor currently points to.
58018**
58019** The caller must guarantee that the cursor is pointing to a non-NULL
58020** valid entry.  In other words, the calling procedure must guarantee
58021** that the cursor has Cursor.eState==CURSOR_VALID.
58022**
58023** Failure is not possible.  This function always returns SQLITE_OK.
58024** It might just as well be a procedure (returning void) but we continue
58025** to return an integer result code for historical reasons.
58026*/
58027SQLITE_PRIVATE int sqlite3BtreeDataSize(BtCursor *pCur, u32 *pSize){
58028  assert( cursorHoldsMutex(pCur) );
58029  assert( pCur->eState==CURSOR_VALID );
58030  assert( pCur->iPage>=0 );
58031  assert( pCur->iPage<BTCURSOR_MAX_DEPTH );
58032  assert( pCur->apPage[pCur->iPage]->intKeyLeaf==1 );
58033  getCellInfo(pCur);
58034  *pSize = pCur->info.nPayload;
58035  return SQLITE_OK;
58036}
58037
58038/*
58039** Given the page number of an overflow page in the database (parameter
58040** ovfl), this function finds the page number of the next page in the
58041** linked list of overflow pages. If possible, it uses the auto-vacuum
58042** pointer-map data instead of reading the content of page ovfl to do so.
58043**
58044** If an error occurs an SQLite error code is returned. Otherwise:
58045**
58046** The page number of the next overflow page in the linked list is
58047** written to *pPgnoNext. If page ovfl is the last page in its linked
58048** list, *pPgnoNext is set to zero.
58049**
58050** If ppPage is not NULL, and a reference to the MemPage object corresponding
58051** to page number pOvfl was obtained, then *ppPage is set to point to that
58052** reference. It is the responsibility of the caller to call releasePage()
58053** on *ppPage to free the reference. In no reference was obtained (because
58054** the pointer-map was used to obtain the value for *pPgnoNext), then
58055** *ppPage is set to zero.
58056*/
58057static int getOverflowPage(
58058  BtShared *pBt,               /* The database file */
58059  Pgno ovfl,                   /* Current overflow page number */
58060  MemPage **ppPage,            /* OUT: MemPage handle (may be NULL) */
58061  Pgno *pPgnoNext              /* OUT: Next overflow page number */
58062){
58063  Pgno next = 0;
58064  MemPage *pPage = 0;
58065  int rc = SQLITE_OK;
58066
58067  assert( sqlite3_mutex_held(pBt->mutex) );
58068  assert(pPgnoNext);
58069
58070#ifndef SQLITE_OMIT_AUTOVACUUM
58071  /* Try to find the next page in the overflow list using the
58072  ** autovacuum pointer-map pages. Guess that the next page in
58073  ** the overflow list is page number (ovfl+1). If that guess turns
58074  ** out to be wrong, fall back to loading the data of page
58075  ** number ovfl to determine the next page number.
58076  */
58077  if( pBt->autoVacuum ){
58078    Pgno pgno;
58079    Pgno iGuess = ovfl+1;
58080    u8 eType;
58081
58082    while( PTRMAP_ISPAGE(pBt, iGuess) || iGuess==PENDING_BYTE_PAGE(pBt) ){
58083      iGuess++;
58084    }
58085
58086    if( iGuess<=btreePagecount(pBt) ){
58087      rc = ptrmapGet(pBt, iGuess, &eType, &pgno);
58088      if( rc==SQLITE_OK && eType==PTRMAP_OVERFLOW2 && pgno==ovfl ){
58089        next = iGuess;
58090        rc = SQLITE_DONE;
58091      }
58092    }
58093  }
58094#endif
58095
58096  assert( next==0 || rc==SQLITE_DONE );
58097  if( rc==SQLITE_OK ){
58098    rc = btreeGetPage(pBt, ovfl, &pPage, (ppPage==0) ? PAGER_GET_READONLY : 0);
58099    assert( rc==SQLITE_OK || pPage==0 );
58100    if( rc==SQLITE_OK ){
58101      next = get4byte(pPage->aData);
58102    }
58103  }
58104
58105  *pPgnoNext = next;
58106  if( ppPage ){
58107    *ppPage = pPage;
58108  }else{
58109    releasePage(pPage);
58110  }
58111  return (rc==SQLITE_DONE ? SQLITE_OK : rc);
58112}
58113
58114/*
58115** Copy data from a buffer to a page, or from a page to a buffer.
58116**
58117** pPayload is a pointer to data stored on database page pDbPage.
58118** If argument eOp is false, then nByte bytes of data are copied
58119** from pPayload to the buffer pointed at by pBuf. If eOp is true,
58120** then sqlite3PagerWrite() is called on pDbPage and nByte bytes
58121** of data are copied from the buffer pBuf to pPayload.
58122**
58123** SQLITE_OK is returned on success, otherwise an error code.
58124*/
58125static int copyPayload(
58126  void *pPayload,           /* Pointer to page data */
58127  void *pBuf,               /* Pointer to buffer */
58128  int nByte,                /* Number of bytes to copy */
58129  int eOp,                  /* 0 -> copy from page, 1 -> copy to page */
58130  DbPage *pDbPage           /* Page containing pPayload */
58131){
58132  if( eOp ){
58133    /* Copy data from buffer to page (a write operation) */
58134    int rc = sqlite3PagerWrite(pDbPage);
58135    if( rc!=SQLITE_OK ){
58136      return rc;
58137    }
58138    memcpy(pPayload, pBuf, nByte);
58139  }else{
58140    /* Copy data from page to buffer (a read operation) */
58141    memcpy(pBuf, pPayload, nByte);
58142  }
58143  return SQLITE_OK;
58144}
58145
58146/*
58147** This function is used to read or overwrite payload information
58148** for the entry that the pCur cursor is pointing to. The eOp
58149** argument is interpreted as follows:
58150**
58151**   0: The operation is a read. Populate the overflow cache.
58152**   1: The operation is a write. Populate the overflow cache.
58153**   2: The operation is a read. Do not populate the overflow cache.
58154**
58155** A total of "amt" bytes are read or written beginning at "offset".
58156** Data is read to or from the buffer pBuf.
58157**
58158** The content being read or written might appear on the main page
58159** or be scattered out on multiple overflow pages.
58160**
58161** If the current cursor entry uses one or more overflow pages and the
58162** eOp argument is not 2, this function may allocate space for and lazily
58163** populates the overflow page-list cache array (BtCursor.aOverflow).
58164** Subsequent calls use this cache to make seeking to the supplied offset
58165** more efficient.
58166**
58167** Once an overflow page-list cache has been allocated, it may be
58168** invalidated if some other cursor writes to the same table, or if
58169** the cursor is moved to a different row. Additionally, in auto-vacuum
58170** mode, the following events may invalidate an overflow page-list cache.
58171**
58172**   * An incremental vacuum,
58173**   * A commit in auto_vacuum="full" mode,
58174**   * Creating a table (may require moving an overflow page).
58175*/
58176static int accessPayload(
58177  BtCursor *pCur,      /* Cursor pointing to entry to read from */
58178  u32 offset,          /* Begin reading this far into payload */
58179  u32 amt,             /* Read this many bytes */
58180  unsigned char *pBuf, /* Write the bytes into this buffer */
58181  int eOp              /* zero to read. non-zero to write. */
58182){
58183  unsigned char *aPayload;
58184  int rc = SQLITE_OK;
58185  int iIdx = 0;
58186  MemPage *pPage = pCur->apPage[pCur->iPage]; /* Btree page of current entry */
58187  BtShared *pBt = pCur->pBt;                  /* Btree this cursor belongs to */
58188#ifdef SQLITE_DIRECT_OVERFLOW_READ
58189  unsigned char * const pBufStart = pBuf;
58190  int bEnd;                                 /* True if reading to end of data */
58191#endif
58192
58193  assert( pPage );
58194  assert( pCur->eState==CURSOR_VALID );
58195  assert( pCur->aiIdx[pCur->iPage]<pPage->nCell );
58196  assert( cursorHoldsMutex(pCur) );
58197  assert( eOp!=2 || offset==0 );    /* Always start from beginning for eOp==2 */
58198
58199  getCellInfo(pCur);
58200  aPayload = pCur->info.pPayload;
58201#ifdef SQLITE_DIRECT_OVERFLOW_READ
58202  bEnd = offset+amt==pCur->info.nPayload;
58203#endif
58204  assert( offset+amt <= pCur->info.nPayload );
58205
58206  if( &aPayload[pCur->info.nLocal] > &pPage->aData[pBt->usableSize] ){
58207    /* Trying to read or write past the end of the data is an error */
58208    return SQLITE_CORRUPT_BKPT;
58209  }
58210
58211  /* Check if data must be read/written to/from the btree page itself. */
58212  if( offset<pCur->info.nLocal ){
58213    int a = amt;
58214    if( a+offset>pCur->info.nLocal ){
58215      a = pCur->info.nLocal - offset;
58216    }
58217    rc = copyPayload(&aPayload[offset], pBuf, a, (eOp & 0x01), pPage->pDbPage);
58218    offset = 0;
58219    pBuf += a;
58220    amt -= a;
58221  }else{
58222    offset -= pCur->info.nLocal;
58223  }
58224
58225
58226  if( rc==SQLITE_OK && amt>0 ){
58227    const u32 ovflSize = pBt->usableSize - 4;  /* Bytes content per ovfl page */
58228    Pgno nextPage;
58229
58230    nextPage = get4byte(&aPayload[pCur->info.nLocal]);
58231
58232    /* If the BtCursor.aOverflow[] has not been allocated, allocate it now.
58233    ** Except, do not allocate aOverflow[] for eOp==2.
58234    **
58235    ** The aOverflow[] array is sized at one entry for each overflow page
58236    ** in the overflow chain. The page number of the first overflow page is
58237    ** stored in aOverflow[0], etc. A value of 0 in the aOverflow[] array
58238    ** means "not yet known" (the cache is lazily populated).
58239    */
58240    if( eOp!=2 && (pCur->curFlags & BTCF_ValidOvfl)==0 ){
58241      int nOvfl = (pCur->info.nPayload-pCur->info.nLocal+ovflSize-1)/ovflSize;
58242      if( nOvfl>pCur->nOvflAlloc ){
58243        Pgno *aNew = (Pgno*)sqlite3Realloc(
58244            pCur->aOverflow, nOvfl*2*sizeof(Pgno)
58245        );
58246        if( aNew==0 ){
58247          rc = SQLITE_NOMEM;
58248        }else{
58249          pCur->nOvflAlloc = nOvfl*2;
58250          pCur->aOverflow = aNew;
58251        }
58252      }
58253      if( rc==SQLITE_OK ){
58254        memset(pCur->aOverflow, 0, nOvfl*sizeof(Pgno));
58255        pCur->curFlags |= BTCF_ValidOvfl;
58256      }
58257    }
58258
58259    /* If the overflow page-list cache has been allocated and the
58260    ** entry for the first required overflow page is valid, skip
58261    ** directly to it.
58262    */
58263    if( (pCur->curFlags & BTCF_ValidOvfl)!=0
58264     && pCur->aOverflow[offset/ovflSize]
58265    ){
58266      iIdx = (offset/ovflSize);
58267      nextPage = pCur->aOverflow[iIdx];
58268      offset = (offset%ovflSize);
58269    }
58270
58271    for( ; rc==SQLITE_OK && amt>0 && nextPage; iIdx++){
58272
58273      /* If required, populate the overflow page-list cache. */
58274      if( (pCur->curFlags & BTCF_ValidOvfl)!=0 ){
58275        assert(!pCur->aOverflow[iIdx] || pCur->aOverflow[iIdx]==nextPage);
58276        pCur->aOverflow[iIdx] = nextPage;
58277      }
58278
58279      if( offset>=ovflSize ){
58280        /* The only reason to read this page is to obtain the page
58281        ** number for the next page in the overflow chain. The page
58282        ** data is not required. So first try to lookup the overflow
58283        ** page-list cache, if any, then fall back to the getOverflowPage()
58284        ** function.
58285        **
58286        ** Note that the aOverflow[] array must be allocated because eOp!=2
58287        ** here.  If eOp==2, then offset==0 and this branch is never taken.
58288        */
58289        assert( eOp!=2 );
58290        assert( pCur->curFlags & BTCF_ValidOvfl );
58291        assert( pCur->pBtree->db==pBt->db );
58292        if( pCur->aOverflow[iIdx+1] ){
58293          nextPage = pCur->aOverflow[iIdx+1];
58294        }else{
58295          rc = getOverflowPage(pBt, nextPage, 0, &nextPage);
58296        }
58297        offset -= ovflSize;
58298      }else{
58299        /* Need to read this page properly. It contains some of the
58300        ** range of data that is being read (eOp==0) or written (eOp!=0).
58301        */
58302#ifdef SQLITE_DIRECT_OVERFLOW_READ
58303        sqlite3_file *fd;
58304#endif
58305        int a = amt;
58306        if( a + offset > ovflSize ){
58307          a = ovflSize - offset;
58308        }
58309
58310#ifdef SQLITE_DIRECT_OVERFLOW_READ
58311        /* If all the following are true:
58312        **
58313        **   1) this is a read operation, and
58314        **   2) data is required from the start of this overflow page, and
58315        **   3) the database is file-backed, and
58316        **   4) there is no open write-transaction, and
58317        **   5) the database is not a WAL database,
58318        **   6) all data from the page is being read.
58319        **   7) at least 4 bytes have already been read into the output buffer
58320        **
58321        ** then data can be read directly from the database file into the
58322        ** output buffer, bypassing the page-cache altogether. This speeds
58323        ** up loading large records that span many overflow pages.
58324        */
58325        if( (eOp&0x01)==0                                      /* (1) */
58326         && offset==0                                          /* (2) */
58327         && (bEnd || a==ovflSize)                              /* (6) */
58328         && pBt->inTransaction==TRANS_READ                     /* (4) */
58329         && (fd = sqlite3PagerFile(pBt->pPager))->pMethods     /* (3) */
58330         && pBt->pPage1->aData[19]==0x01                       /* (5) */
58331         && &pBuf[-4]>=pBufStart                               /* (7) */
58332        ){
58333          u8 aSave[4];
58334          u8 *aWrite = &pBuf[-4];
58335          assert( aWrite>=pBufStart );                         /* hence (7) */
58336          memcpy(aSave, aWrite, 4);
58337          rc = sqlite3OsRead(fd, aWrite, a+4, (i64)pBt->pageSize*(nextPage-1));
58338          nextPage = get4byte(aWrite);
58339          memcpy(aWrite, aSave, 4);
58340        }else
58341#endif
58342
58343        {
58344          DbPage *pDbPage;
58345          rc = sqlite3PagerAcquire(pBt->pPager, nextPage, &pDbPage,
58346              ((eOp&0x01)==0 ? PAGER_GET_READONLY : 0)
58347          );
58348          if( rc==SQLITE_OK ){
58349            aPayload = sqlite3PagerGetData(pDbPage);
58350            nextPage = get4byte(aPayload);
58351            rc = copyPayload(&aPayload[offset+4], pBuf, a, (eOp&0x01), pDbPage);
58352            sqlite3PagerUnref(pDbPage);
58353            offset = 0;
58354          }
58355        }
58356        amt -= a;
58357        pBuf += a;
58358      }
58359    }
58360  }
58361
58362  if( rc==SQLITE_OK && amt>0 ){
58363    return SQLITE_CORRUPT_BKPT;
58364  }
58365  return rc;
58366}
58367
58368/*
58369** Read part of the key associated with cursor pCur.  Exactly
58370** "amt" bytes will be transferred into pBuf[].  The transfer
58371** begins at "offset".
58372**
58373** The caller must ensure that pCur is pointing to a valid row
58374** in the table.
58375**
58376** Return SQLITE_OK on success or an error code if anything goes
58377** wrong.  An error is returned if "offset+amt" is larger than
58378** the available payload.
58379*/
58380SQLITE_PRIVATE int sqlite3BtreeKey(BtCursor *pCur, u32 offset, u32 amt, void *pBuf){
58381  assert( cursorHoldsMutex(pCur) );
58382  assert( pCur->eState==CURSOR_VALID );
58383  assert( pCur->iPage>=0 && pCur->apPage[pCur->iPage] );
58384  assert( pCur->aiIdx[pCur->iPage]<pCur->apPage[pCur->iPage]->nCell );
58385  return accessPayload(pCur, offset, amt, (unsigned char*)pBuf, 0);
58386}
58387
58388/*
58389** Read part of the data associated with cursor pCur.  Exactly
58390** "amt" bytes will be transfered into pBuf[].  The transfer
58391** begins at "offset".
58392**
58393** Return SQLITE_OK on success or an error code if anything goes
58394** wrong.  An error is returned if "offset+amt" is larger than
58395** the available payload.
58396*/
58397SQLITE_PRIVATE int sqlite3BtreeData(BtCursor *pCur, u32 offset, u32 amt, void *pBuf){
58398  int rc;
58399
58400#ifndef SQLITE_OMIT_INCRBLOB
58401  if ( pCur->eState==CURSOR_INVALID ){
58402    return SQLITE_ABORT;
58403  }
58404#endif
58405
58406  assert( cursorHoldsMutex(pCur) );
58407  rc = restoreCursorPosition(pCur);
58408  if( rc==SQLITE_OK ){
58409    assert( pCur->eState==CURSOR_VALID );
58410    assert( pCur->iPage>=0 && pCur->apPage[pCur->iPage] );
58411    assert( pCur->aiIdx[pCur->iPage]<pCur->apPage[pCur->iPage]->nCell );
58412    rc = accessPayload(pCur, offset, amt, pBuf, 0);
58413  }
58414  return rc;
58415}
58416
58417/*
58418** Return a pointer to payload information from the entry that the
58419** pCur cursor is pointing to.  The pointer is to the beginning of
58420** the key if index btrees (pPage->intKey==0) and is the data for
58421** table btrees (pPage->intKey==1). The number of bytes of available
58422** key/data is written into *pAmt.  If *pAmt==0, then the value
58423** returned will not be a valid pointer.
58424**
58425** This routine is an optimization.  It is common for the entire key
58426** and data to fit on the local page and for there to be no overflow
58427** pages.  When that is so, this routine can be used to access the
58428** key and data without making a copy.  If the key and/or data spills
58429** onto overflow pages, then accessPayload() must be used to reassemble
58430** the key/data and copy it into a preallocated buffer.
58431**
58432** The pointer returned by this routine looks directly into the cached
58433** page of the database.  The data might change or move the next time
58434** any btree routine is called.
58435*/
58436static const void *fetchPayload(
58437  BtCursor *pCur,      /* Cursor pointing to entry to read from */
58438  u32 *pAmt            /* Write the number of available bytes here */
58439){
58440  u32 amt;
58441  assert( pCur!=0 && pCur->iPage>=0 && pCur->apPage[pCur->iPage]);
58442  assert( pCur->eState==CURSOR_VALID );
58443  assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
58444  assert( cursorHoldsMutex(pCur) );
58445  assert( pCur->aiIdx[pCur->iPage]<pCur->apPage[pCur->iPage]->nCell );
58446  assert( pCur->info.nSize>0 );
58447  assert( pCur->info.pPayload>pCur->apPage[pCur->iPage]->aData || CORRUPT_DB );
58448  assert( pCur->info.pPayload<pCur->apPage[pCur->iPage]->aDataEnd ||CORRUPT_DB);
58449  amt = (int)(pCur->apPage[pCur->iPage]->aDataEnd - pCur->info.pPayload);
58450  if( pCur->info.nLocal<amt ) amt = pCur->info.nLocal;
58451  *pAmt = amt;
58452  return (void*)pCur->info.pPayload;
58453}
58454
58455
58456/*
58457** For the entry that cursor pCur is point to, return as
58458** many bytes of the key or data as are available on the local
58459** b-tree page.  Write the number of available bytes into *pAmt.
58460**
58461** The pointer returned is ephemeral.  The key/data may move
58462** or be destroyed on the next call to any Btree routine,
58463** including calls from other threads against the same cache.
58464** Hence, a mutex on the BtShared should be held prior to calling
58465** this routine.
58466**
58467** These routines is used to get quick access to key and data
58468** in the common case where no overflow pages are used.
58469*/
58470SQLITE_PRIVATE const void *sqlite3BtreeKeyFetch(BtCursor *pCur, u32 *pAmt){
58471  return fetchPayload(pCur, pAmt);
58472}
58473SQLITE_PRIVATE const void *sqlite3BtreeDataFetch(BtCursor *pCur, u32 *pAmt){
58474  return fetchPayload(pCur, pAmt);
58475}
58476
58477
58478/*
58479** Move the cursor down to a new child page.  The newPgno argument is the
58480** page number of the child page to move to.
58481**
58482** This function returns SQLITE_CORRUPT if the page-header flags field of
58483** the new child page does not match the flags field of the parent (i.e.
58484** if an intkey page appears to be the parent of a non-intkey page, or
58485** vice-versa).
58486*/
58487static int moveToChild(BtCursor *pCur, u32 newPgno){
58488  BtShared *pBt = pCur->pBt;
58489
58490  assert( cursorHoldsMutex(pCur) );
58491  assert( pCur->eState==CURSOR_VALID );
58492  assert( pCur->iPage<BTCURSOR_MAX_DEPTH );
58493  assert( pCur->iPage>=0 );
58494  if( pCur->iPage>=(BTCURSOR_MAX_DEPTH-1) ){
58495    return SQLITE_CORRUPT_BKPT;
58496  }
58497  pCur->info.nSize = 0;
58498  pCur->curFlags &= ~(BTCF_ValidNKey|BTCF_ValidOvfl);
58499  pCur->iPage++;
58500  pCur->aiIdx[pCur->iPage] = 0;
58501  return getAndInitPage(pBt, newPgno, &pCur->apPage[pCur->iPage],
58502                        pCur, pCur->curPagerFlags);
58503}
58504
58505#if SQLITE_DEBUG
58506/*
58507** Page pParent is an internal (non-leaf) tree page. This function
58508** asserts that page number iChild is the left-child if the iIdx'th
58509** cell in page pParent. Or, if iIdx is equal to the total number of
58510** cells in pParent, that page number iChild is the right-child of
58511** the page.
58512*/
58513static void assertParentIndex(MemPage *pParent, int iIdx, Pgno iChild){
58514  if( CORRUPT_DB ) return;  /* The conditions tested below might not be true
58515                            ** in a corrupt database */
58516  assert( iIdx<=pParent->nCell );
58517  if( iIdx==pParent->nCell ){
58518    assert( get4byte(&pParent->aData[pParent->hdrOffset+8])==iChild );
58519  }else{
58520    assert( get4byte(findCell(pParent, iIdx))==iChild );
58521  }
58522}
58523#else
58524#  define assertParentIndex(x,y,z)
58525#endif
58526
58527/*
58528** Move the cursor up to the parent page.
58529**
58530** pCur->idx is set to the cell index that contains the pointer
58531** to the page we are coming from.  If we are coming from the
58532** right-most child page then pCur->idx is set to one more than
58533** the largest cell index.
58534*/
58535static void moveToParent(BtCursor *pCur){
58536  assert( cursorHoldsMutex(pCur) );
58537  assert( pCur->eState==CURSOR_VALID );
58538  assert( pCur->iPage>0 );
58539  assert( pCur->apPage[pCur->iPage] );
58540  assertParentIndex(
58541    pCur->apPage[pCur->iPage-1],
58542    pCur->aiIdx[pCur->iPage-1],
58543    pCur->apPage[pCur->iPage]->pgno
58544  );
58545  testcase( pCur->aiIdx[pCur->iPage-1] > pCur->apPage[pCur->iPage-1]->nCell );
58546  pCur->info.nSize = 0;
58547  pCur->curFlags &= ~(BTCF_ValidNKey|BTCF_ValidOvfl);
58548  releasePageNotNull(pCur->apPage[pCur->iPage--]);
58549}
58550
58551/*
58552** Move the cursor to point to the root page of its b-tree structure.
58553**
58554** If the table has a virtual root page, then the cursor is moved to point
58555** to the virtual root page instead of the actual root page. A table has a
58556** virtual root page when the actual root page contains no cells and a
58557** single child page. This can only happen with the table rooted at page 1.
58558**
58559** If the b-tree structure is empty, the cursor state is set to
58560** CURSOR_INVALID. Otherwise, the cursor is set to point to the first
58561** cell located on the root (or virtual root) page and the cursor state
58562** is set to CURSOR_VALID.
58563**
58564** If this function returns successfully, it may be assumed that the
58565** page-header flags indicate that the [virtual] root-page is the expected
58566** kind of b-tree page (i.e. if when opening the cursor the caller did not
58567** specify a KeyInfo structure the flags byte is set to 0x05 or 0x0D,
58568** indicating a table b-tree, or if the caller did specify a KeyInfo
58569** structure the flags byte is set to 0x02 or 0x0A, indicating an index
58570** b-tree).
58571*/
58572static int moveToRoot(BtCursor *pCur){
58573  MemPage *pRoot;
58574  int rc = SQLITE_OK;
58575
58576  assert( cursorHoldsMutex(pCur) );
58577  assert( CURSOR_INVALID < CURSOR_REQUIRESEEK );
58578  assert( CURSOR_VALID   < CURSOR_REQUIRESEEK );
58579  assert( CURSOR_FAULT   > CURSOR_REQUIRESEEK );
58580  if( pCur->eState>=CURSOR_REQUIRESEEK ){
58581    if( pCur->eState==CURSOR_FAULT ){
58582      assert( pCur->skipNext!=SQLITE_OK );
58583      return pCur->skipNext;
58584    }
58585    sqlite3BtreeClearCursor(pCur);
58586  }
58587
58588  if( pCur->iPage>=0 ){
58589    while( pCur->iPage ){
58590      assert( pCur->apPage[pCur->iPage]!=0 );
58591      releasePageNotNull(pCur->apPage[pCur->iPage--]);
58592    }
58593  }else if( pCur->pgnoRoot==0 ){
58594    pCur->eState = CURSOR_INVALID;
58595    return SQLITE_OK;
58596  }else{
58597    assert( pCur->iPage==(-1) );
58598    rc = getAndInitPage(pCur->pBtree->pBt, pCur->pgnoRoot, &pCur->apPage[0],
58599                        0, pCur->curPagerFlags);
58600    if( rc!=SQLITE_OK ){
58601      pCur->eState = CURSOR_INVALID;
58602      return rc;
58603    }
58604    pCur->iPage = 0;
58605    pCur->curIntKey = pCur->apPage[0]->intKey;
58606  }
58607  pRoot = pCur->apPage[0];
58608  assert( pRoot->pgno==pCur->pgnoRoot );
58609
58610  /* If pCur->pKeyInfo is not NULL, then the caller that opened this cursor
58611  ** expected to open it on an index b-tree. Otherwise, if pKeyInfo is
58612  ** NULL, the caller expects a table b-tree. If this is not the case,
58613  ** return an SQLITE_CORRUPT error.
58614  **
58615  ** Earlier versions of SQLite assumed that this test could not fail
58616  ** if the root page was already loaded when this function was called (i.e.
58617  ** if pCur->iPage>=0). But this is not so if the database is corrupted
58618  ** in such a way that page pRoot is linked into a second b-tree table
58619  ** (or the freelist).  */
58620  assert( pRoot->intKey==1 || pRoot->intKey==0 );
58621  if( pRoot->isInit==0 || (pCur->pKeyInfo==0)!=pRoot->intKey ){
58622    return SQLITE_CORRUPT_BKPT;
58623  }
58624
58625  pCur->aiIdx[0] = 0;
58626  pCur->info.nSize = 0;
58627  pCur->curFlags &= ~(BTCF_AtLast|BTCF_ValidNKey|BTCF_ValidOvfl);
58628
58629  if( pRoot->nCell>0 ){
58630    pCur->eState = CURSOR_VALID;
58631  }else if( !pRoot->leaf ){
58632    Pgno subpage;
58633    if( pRoot->pgno!=1 ) return SQLITE_CORRUPT_BKPT;
58634    subpage = get4byte(&pRoot->aData[pRoot->hdrOffset+8]);
58635    pCur->eState = CURSOR_VALID;
58636    rc = moveToChild(pCur, subpage);
58637  }else{
58638    pCur->eState = CURSOR_INVALID;
58639  }
58640  return rc;
58641}
58642
58643/*
58644** Move the cursor down to the left-most leaf entry beneath the
58645** entry to which it is currently pointing.
58646**
58647** The left-most leaf is the one with the smallest key - the first
58648** in ascending order.
58649*/
58650static int moveToLeftmost(BtCursor *pCur){
58651  Pgno pgno;
58652  int rc = SQLITE_OK;
58653  MemPage *pPage;
58654
58655  assert( cursorHoldsMutex(pCur) );
58656  assert( pCur->eState==CURSOR_VALID );
58657  while( rc==SQLITE_OK && !(pPage = pCur->apPage[pCur->iPage])->leaf ){
58658    assert( pCur->aiIdx[pCur->iPage]<pPage->nCell );
58659    pgno = get4byte(findCell(pPage, pCur->aiIdx[pCur->iPage]));
58660    rc = moveToChild(pCur, pgno);
58661  }
58662  return rc;
58663}
58664
58665/*
58666** Move the cursor down to the right-most leaf entry beneath the
58667** page to which it is currently pointing.  Notice the difference
58668** between moveToLeftmost() and moveToRightmost().  moveToLeftmost()
58669** finds the left-most entry beneath the *entry* whereas moveToRightmost()
58670** finds the right-most entry beneath the *page*.
58671**
58672** The right-most entry is the one with the largest key - the last
58673** key in ascending order.
58674*/
58675static int moveToRightmost(BtCursor *pCur){
58676  Pgno pgno;
58677  int rc = SQLITE_OK;
58678  MemPage *pPage = 0;
58679
58680  assert( cursorHoldsMutex(pCur) );
58681  assert( pCur->eState==CURSOR_VALID );
58682  while( !(pPage = pCur->apPage[pCur->iPage])->leaf ){
58683    pgno = get4byte(&pPage->aData[pPage->hdrOffset+8]);
58684    pCur->aiIdx[pCur->iPage] = pPage->nCell;
58685    rc = moveToChild(pCur, pgno);
58686    if( rc ) return rc;
58687  }
58688  pCur->aiIdx[pCur->iPage] = pPage->nCell-1;
58689  assert( pCur->info.nSize==0 );
58690  assert( (pCur->curFlags & BTCF_ValidNKey)==0 );
58691  return SQLITE_OK;
58692}
58693
58694/* Move the cursor to the first entry in the table.  Return SQLITE_OK
58695** on success.  Set *pRes to 0 if the cursor actually points to something
58696** or set *pRes to 1 if the table is empty.
58697*/
58698SQLITE_PRIVATE int sqlite3BtreeFirst(BtCursor *pCur, int *pRes){
58699  int rc;
58700
58701  assert( cursorHoldsMutex(pCur) );
58702  assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
58703  rc = moveToRoot(pCur);
58704  if( rc==SQLITE_OK ){
58705    if( pCur->eState==CURSOR_INVALID ){
58706      assert( pCur->pgnoRoot==0 || pCur->apPage[pCur->iPage]->nCell==0 );
58707      *pRes = 1;
58708    }else{
58709      assert( pCur->apPage[pCur->iPage]->nCell>0 );
58710      *pRes = 0;
58711      rc = moveToLeftmost(pCur);
58712    }
58713  }
58714  return rc;
58715}
58716
58717/* Move the cursor to the last entry in the table.  Return SQLITE_OK
58718** on success.  Set *pRes to 0 if the cursor actually points to something
58719** or set *pRes to 1 if the table is empty.
58720*/
58721SQLITE_PRIVATE int sqlite3BtreeLast(BtCursor *pCur, int *pRes){
58722  int rc;
58723
58724  assert( cursorHoldsMutex(pCur) );
58725  assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
58726
58727  /* If the cursor already points to the last entry, this is a no-op. */
58728  if( CURSOR_VALID==pCur->eState && (pCur->curFlags & BTCF_AtLast)!=0 ){
58729#ifdef SQLITE_DEBUG
58730    /* This block serves to assert() that the cursor really does point
58731    ** to the last entry in the b-tree. */
58732    int ii;
58733    for(ii=0; ii<pCur->iPage; ii++){
58734      assert( pCur->aiIdx[ii]==pCur->apPage[ii]->nCell );
58735    }
58736    assert( pCur->aiIdx[pCur->iPage]==pCur->apPage[pCur->iPage]->nCell-1 );
58737    assert( pCur->apPage[pCur->iPage]->leaf );
58738#endif
58739    return SQLITE_OK;
58740  }
58741
58742  rc = moveToRoot(pCur);
58743  if( rc==SQLITE_OK ){
58744    if( CURSOR_INVALID==pCur->eState ){
58745      assert( pCur->pgnoRoot==0 || pCur->apPage[pCur->iPage]->nCell==0 );
58746      *pRes = 1;
58747    }else{
58748      assert( pCur->eState==CURSOR_VALID );
58749      *pRes = 0;
58750      rc = moveToRightmost(pCur);
58751      if( rc==SQLITE_OK ){
58752        pCur->curFlags |= BTCF_AtLast;
58753      }else{
58754        pCur->curFlags &= ~BTCF_AtLast;
58755      }
58756
58757    }
58758  }
58759  return rc;
58760}
58761
58762/* Move the cursor so that it points to an entry near the key
58763** specified by pIdxKey or intKey.   Return a success code.
58764**
58765** For INTKEY tables, the intKey parameter is used.  pIdxKey
58766** must be NULL.  For index tables, pIdxKey is used and intKey
58767** is ignored.
58768**
58769** If an exact match is not found, then the cursor is always
58770** left pointing at a leaf page which would hold the entry if it
58771** were present.  The cursor might point to an entry that comes
58772** before or after the key.
58773**
58774** An integer is written into *pRes which is the result of
58775** comparing the key with the entry to which the cursor is
58776** pointing.  The meaning of the integer written into
58777** *pRes is as follows:
58778**
58779**     *pRes<0      The cursor is left pointing at an entry that
58780**                  is smaller than intKey/pIdxKey or if the table is empty
58781**                  and the cursor is therefore left point to nothing.
58782**
58783**     *pRes==0     The cursor is left pointing at an entry that
58784**                  exactly matches intKey/pIdxKey.
58785**
58786**     *pRes>0      The cursor is left pointing at an entry that
58787**                  is larger than intKey/pIdxKey.
58788**
58789*/
58790SQLITE_PRIVATE int sqlite3BtreeMovetoUnpacked(
58791  BtCursor *pCur,          /* The cursor to be moved */
58792  UnpackedRecord *pIdxKey, /* Unpacked index key */
58793  i64 intKey,              /* The table key */
58794  int biasRight,           /* If true, bias the search to the high end */
58795  int *pRes                /* Write search results here */
58796){
58797  int rc;
58798  RecordCompare xRecordCompare;
58799
58800  assert( cursorHoldsMutex(pCur) );
58801  assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
58802  assert( pRes );
58803  assert( (pIdxKey==0)==(pCur->pKeyInfo==0) );
58804
58805  /* If the cursor is already positioned at the point we are trying
58806  ** to move to, then just return without doing any work */
58807  if( pCur->eState==CURSOR_VALID && (pCur->curFlags & BTCF_ValidNKey)!=0
58808   && pCur->curIntKey
58809  ){
58810    if( pCur->info.nKey==intKey ){
58811      *pRes = 0;
58812      return SQLITE_OK;
58813    }
58814    if( (pCur->curFlags & BTCF_AtLast)!=0 && pCur->info.nKey<intKey ){
58815      *pRes = -1;
58816      return SQLITE_OK;
58817    }
58818  }
58819
58820  if( pIdxKey ){
58821    xRecordCompare = sqlite3VdbeFindCompare(pIdxKey);
58822    pIdxKey->errCode = 0;
58823    assert( pIdxKey->default_rc==1
58824         || pIdxKey->default_rc==0
58825         || pIdxKey->default_rc==-1
58826    );
58827  }else{
58828    xRecordCompare = 0; /* All keys are integers */
58829  }
58830
58831  rc = moveToRoot(pCur);
58832  if( rc ){
58833    return rc;
58834  }
58835  assert( pCur->pgnoRoot==0 || pCur->apPage[pCur->iPage] );
58836  assert( pCur->pgnoRoot==0 || pCur->apPage[pCur->iPage]->isInit );
58837  assert( pCur->eState==CURSOR_INVALID || pCur->apPage[pCur->iPage]->nCell>0 );
58838  if( pCur->eState==CURSOR_INVALID ){
58839    *pRes = -1;
58840    assert( pCur->pgnoRoot==0 || pCur->apPage[pCur->iPage]->nCell==0 );
58841    return SQLITE_OK;
58842  }
58843  assert( pCur->apPage[0]->intKey==pCur->curIntKey );
58844  assert( pCur->curIntKey || pIdxKey );
58845  for(;;){
58846    int lwr, upr, idx, c;
58847    Pgno chldPg;
58848    MemPage *pPage = pCur->apPage[pCur->iPage];
58849    u8 *pCell;                          /* Pointer to current cell in pPage */
58850
58851    /* pPage->nCell must be greater than zero. If this is the root-page
58852    ** the cursor would have been INVALID above and this for(;;) loop
58853    ** not run. If this is not the root-page, then the moveToChild() routine
58854    ** would have already detected db corruption. Similarly, pPage must
58855    ** be the right kind (index or table) of b-tree page. Otherwise
58856    ** a moveToChild() or moveToRoot() call would have detected corruption.  */
58857    assert( pPage->nCell>0 );
58858    assert( pPage->intKey==(pIdxKey==0) );
58859    lwr = 0;
58860    upr = pPage->nCell-1;
58861    assert( biasRight==0 || biasRight==1 );
58862    idx = upr>>(1-biasRight); /* idx = biasRight ? upr : (lwr+upr)/2; */
58863    pCur->aiIdx[pCur->iPage] = (u16)idx;
58864    if( xRecordCompare==0 ){
58865      for(;;){
58866        i64 nCellKey;
58867        pCell = findCellPastPtr(pPage, idx);
58868        if( pPage->intKeyLeaf ){
58869          while( 0x80 <= *(pCell++) ){
58870            if( pCell>=pPage->aDataEnd ) return SQLITE_CORRUPT_BKPT;
58871          }
58872        }
58873        getVarint(pCell, (u64*)&nCellKey);
58874        if( nCellKey<intKey ){
58875          lwr = idx+1;
58876          if( lwr>upr ){ c = -1; break; }
58877        }else if( nCellKey>intKey ){
58878          upr = idx-1;
58879          if( lwr>upr ){ c = +1; break; }
58880        }else{
58881          assert( nCellKey==intKey );
58882          pCur->curFlags |= BTCF_ValidNKey;
58883          pCur->info.nKey = nCellKey;
58884          pCur->aiIdx[pCur->iPage] = (u16)idx;
58885          if( !pPage->leaf ){
58886            lwr = idx;
58887            goto moveto_next_layer;
58888          }else{
58889            *pRes = 0;
58890            rc = SQLITE_OK;
58891            goto moveto_finish;
58892          }
58893        }
58894        assert( lwr+upr>=0 );
58895        idx = (lwr+upr)>>1;  /* idx = (lwr+upr)/2; */
58896      }
58897    }else{
58898      for(;;){
58899        int nCell;  /* Size of the pCell cell in bytes */
58900        pCell = findCellPastPtr(pPage, idx);
58901
58902        /* The maximum supported page-size is 65536 bytes. This means that
58903        ** the maximum number of record bytes stored on an index B-Tree
58904        ** page is less than 16384 bytes and may be stored as a 2-byte
58905        ** varint. This information is used to attempt to avoid parsing
58906        ** the entire cell by checking for the cases where the record is
58907        ** stored entirely within the b-tree page by inspecting the first
58908        ** 2 bytes of the cell.
58909        */
58910        nCell = pCell[0];
58911        if( nCell<=pPage->max1bytePayload ){
58912          /* This branch runs if the record-size field of the cell is a
58913          ** single byte varint and the record fits entirely on the main
58914          ** b-tree page.  */
58915          testcase( pCell+nCell+1==pPage->aDataEnd );
58916          c = xRecordCompare(nCell, (void*)&pCell[1], pIdxKey);
58917        }else if( !(pCell[1] & 0x80)
58918          && (nCell = ((nCell&0x7f)<<7) + pCell[1])<=pPage->maxLocal
58919        ){
58920          /* The record-size field is a 2 byte varint and the record
58921          ** fits entirely on the main b-tree page.  */
58922          testcase( pCell+nCell+2==pPage->aDataEnd );
58923          c = xRecordCompare(nCell, (void*)&pCell[2], pIdxKey);
58924        }else{
58925          /* The record flows over onto one or more overflow pages. In
58926          ** this case the whole cell needs to be parsed, a buffer allocated
58927          ** and accessPayload() used to retrieve the record into the
58928          ** buffer before VdbeRecordCompare() can be called.
58929          **
58930          ** If the record is corrupt, the xRecordCompare routine may read
58931          ** up to two varints past the end of the buffer. An extra 18
58932          ** bytes of padding is allocated at the end of the buffer in
58933          ** case this happens.  */
58934          void *pCellKey;
58935          u8 * const pCellBody = pCell - pPage->childPtrSize;
58936          pPage->xParseCell(pPage, pCellBody, &pCur->info);
58937          nCell = (int)pCur->info.nKey;
58938          testcase( nCell<0 );   /* True if key size is 2^32 or more */
58939          testcase( nCell==0 );  /* Invalid key size:  0x80 0x80 0x00 */
58940          testcase( nCell==1 );  /* Invalid key size:  0x80 0x80 0x01 */
58941          testcase( nCell==2 );  /* Minimum legal index key size */
58942          if( nCell<2 ){
58943            rc = SQLITE_CORRUPT_BKPT;
58944            goto moveto_finish;
58945          }
58946          pCellKey = sqlite3Malloc( nCell+18 );
58947          if( pCellKey==0 ){
58948            rc = SQLITE_NOMEM;
58949            goto moveto_finish;
58950          }
58951          pCur->aiIdx[pCur->iPage] = (u16)idx;
58952          rc = accessPayload(pCur, 0, nCell, (unsigned char*)pCellKey, 2);
58953          if( rc ){
58954            sqlite3_free(pCellKey);
58955            goto moveto_finish;
58956          }
58957          c = xRecordCompare(nCell, pCellKey, pIdxKey);
58958          sqlite3_free(pCellKey);
58959        }
58960        assert(
58961            (pIdxKey->errCode!=SQLITE_CORRUPT || c==0)
58962         && (pIdxKey->errCode!=SQLITE_NOMEM || pCur->pBtree->db->mallocFailed)
58963        );
58964        if( c<0 ){
58965          lwr = idx+1;
58966        }else if( c>0 ){
58967          upr = idx-1;
58968        }else{
58969          assert( c==0 );
58970          *pRes = 0;
58971          rc = SQLITE_OK;
58972          pCur->aiIdx[pCur->iPage] = (u16)idx;
58973          if( pIdxKey->errCode ) rc = SQLITE_CORRUPT;
58974          goto moveto_finish;
58975        }
58976        if( lwr>upr ) break;
58977        assert( lwr+upr>=0 );
58978        idx = (lwr+upr)>>1;  /* idx = (lwr+upr)/2 */
58979      }
58980    }
58981    assert( lwr==upr+1 || (pPage->intKey && !pPage->leaf) );
58982    assert( pPage->isInit );
58983    if( pPage->leaf ){
58984      assert( pCur->aiIdx[pCur->iPage]<pCur->apPage[pCur->iPage]->nCell );
58985      pCur->aiIdx[pCur->iPage] = (u16)idx;
58986      *pRes = c;
58987      rc = SQLITE_OK;
58988      goto moveto_finish;
58989    }
58990moveto_next_layer:
58991    if( lwr>=pPage->nCell ){
58992      chldPg = get4byte(&pPage->aData[pPage->hdrOffset+8]);
58993    }else{
58994      chldPg = get4byte(findCell(pPage, lwr));
58995    }
58996    pCur->aiIdx[pCur->iPage] = (u16)lwr;
58997    rc = moveToChild(pCur, chldPg);
58998    if( rc ) break;
58999  }
59000moveto_finish:
59001  pCur->info.nSize = 0;
59002  pCur->curFlags &= ~(BTCF_ValidNKey|BTCF_ValidOvfl);
59003  return rc;
59004}
59005
59006
59007/*
59008** Return TRUE if the cursor is not pointing at an entry of the table.
59009**
59010** TRUE will be returned after a call to sqlite3BtreeNext() moves
59011** past the last entry in the table or sqlite3BtreePrev() moves past
59012** the first entry.  TRUE is also returned if the table is empty.
59013*/
59014SQLITE_PRIVATE int sqlite3BtreeEof(BtCursor *pCur){
59015  /* TODO: What if the cursor is in CURSOR_REQUIRESEEK but all table entries
59016  ** have been deleted? This API will need to change to return an error code
59017  ** as well as the boolean result value.
59018  */
59019  return (CURSOR_VALID!=pCur->eState);
59020}
59021
59022/*
59023** Advance the cursor to the next entry in the database.  If
59024** successful then set *pRes=0.  If the cursor
59025** was already pointing to the last entry in the database before
59026** this routine was called, then set *pRes=1.
59027**
59028** The main entry point is sqlite3BtreeNext().  That routine is optimized
59029** for the common case of merely incrementing the cell counter BtCursor.aiIdx
59030** to the next cell on the current page.  The (slower) btreeNext() helper
59031** routine is called when it is necessary to move to a different page or
59032** to restore the cursor.
59033**
59034** The calling function will set *pRes to 0 or 1.  The initial *pRes value
59035** will be 1 if the cursor being stepped corresponds to an SQL index and
59036** if this routine could have been skipped if that SQL index had been
59037** a unique index.  Otherwise the caller will have set *pRes to zero.
59038** Zero is the common case. The btree implementation is free to use the
59039** initial *pRes value as a hint to improve performance, but the current
59040** SQLite btree implementation does not. (Note that the comdb2 btree
59041** implementation does use this hint, however.)
59042*/
59043static SQLITE_NOINLINE int btreeNext(BtCursor *pCur, int *pRes){
59044  int rc;
59045  int idx;
59046  MemPage *pPage;
59047
59048  assert( cursorHoldsMutex(pCur) );
59049  assert( pCur->skipNext==0 || pCur->eState!=CURSOR_VALID );
59050  assert( *pRes==0 );
59051  if( pCur->eState!=CURSOR_VALID ){
59052    assert( (pCur->curFlags & BTCF_ValidOvfl)==0 );
59053    rc = restoreCursorPosition(pCur);
59054    if( rc!=SQLITE_OK ){
59055      return rc;
59056    }
59057    if( CURSOR_INVALID==pCur->eState ){
59058      *pRes = 1;
59059      return SQLITE_OK;
59060    }
59061    if( pCur->skipNext ){
59062      assert( pCur->eState==CURSOR_VALID || pCur->eState==CURSOR_SKIPNEXT );
59063      pCur->eState = CURSOR_VALID;
59064      if( pCur->skipNext>0 ){
59065        pCur->skipNext = 0;
59066        return SQLITE_OK;
59067      }
59068      pCur->skipNext = 0;
59069    }
59070  }
59071
59072  pPage = pCur->apPage[pCur->iPage];
59073  idx = ++pCur->aiIdx[pCur->iPage];
59074  assert( pPage->isInit );
59075
59076  /* If the database file is corrupt, it is possible for the value of idx
59077  ** to be invalid here. This can only occur if a second cursor modifies
59078  ** the page while cursor pCur is holding a reference to it. Which can
59079  ** only happen if the database is corrupt in such a way as to link the
59080  ** page into more than one b-tree structure. */
59081  testcase( idx>pPage->nCell );
59082
59083  if( idx>=pPage->nCell ){
59084    if( !pPage->leaf ){
59085      rc = moveToChild(pCur, get4byte(&pPage->aData[pPage->hdrOffset+8]));
59086      if( rc ) return rc;
59087      return moveToLeftmost(pCur);
59088    }
59089    do{
59090      if( pCur->iPage==0 ){
59091        *pRes = 1;
59092        pCur->eState = CURSOR_INVALID;
59093        return SQLITE_OK;
59094      }
59095      moveToParent(pCur);
59096      pPage = pCur->apPage[pCur->iPage];
59097    }while( pCur->aiIdx[pCur->iPage]>=pPage->nCell );
59098    if( pPage->intKey ){
59099      return sqlite3BtreeNext(pCur, pRes);
59100    }else{
59101      return SQLITE_OK;
59102    }
59103  }
59104  if( pPage->leaf ){
59105    return SQLITE_OK;
59106  }else{
59107    return moveToLeftmost(pCur);
59108  }
59109}
59110SQLITE_PRIVATE int sqlite3BtreeNext(BtCursor *pCur, int *pRes){
59111  MemPage *pPage;
59112  assert( cursorHoldsMutex(pCur) );
59113  assert( pRes!=0 );
59114  assert( *pRes==0 || *pRes==1 );
59115  assert( pCur->skipNext==0 || pCur->eState!=CURSOR_VALID );
59116  pCur->info.nSize = 0;
59117  pCur->curFlags &= ~(BTCF_ValidNKey|BTCF_ValidOvfl);
59118  *pRes = 0;
59119  if( pCur->eState!=CURSOR_VALID ) return btreeNext(pCur, pRes);
59120  pPage = pCur->apPage[pCur->iPage];
59121  if( (++pCur->aiIdx[pCur->iPage])>=pPage->nCell ){
59122    pCur->aiIdx[pCur->iPage]--;
59123    return btreeNext(pCur, pRes);
59124  }
59125  if( pPage->leaf ){
59126    return SQLITE_OK;
59127  }else{
59128    return moveToLeftmost(pCur);
59129  }
59130}
59131
59132/*
59133** Step the cursor to the back to the previous entry in the database.  If
59134** successful then set *pRes=0.  If the cursor
59135** was already pointing to the first entry in the database before
59136** this routine was called, then set *pRes=1.
59137**
59138** The main entry point is sqlite3BtreePrevious().  That routine is optimized
59139** for the common case of merely decrementing the cell counter BtCursor.aiIdx
59140** to the previous cell on the current page.  The (slower) btreePrevious()
59141** helper routine is called when it is necessary to move to a different page
59142** or to restore the cursor.
59143**
59144** The calling function will set *pRes to 0 or 1.  The initial *pRes value
59145** will be 1 if the cursor being stepped corresponds to an SQL index and
59146** if this routine could have been skipped if that SQL index had been
59147** a unique index.  Otherwise the caller will have set *pRes to zero.
59148** Zero is the common case. The btree implementation is free to use the
59149** initial *pRes value as a hint to improve performance, but the current
59150** SQLite btree implementation does not. (Note that the comdb2 btree
59151** implementation does use this hint, however.)
59152*/
59153static SQLITE_NOINLINE int btreePrevious(BtCursor *pCur, int *pRes){
59154  int rc;
59155  MemPage *pPage;
59156
59157  assert( cursorHoldsMutex(pCur) );
59158  assert( pRes!=0 );
59159  assert( *pRes==0 );
59160  assert( pCur->skipNext==0 || pCur->eState!=CURSOR_VALID );
59161  assert( (pCur->curFlags & (BTCF_AtLast|BTCF_ValidOvfl|BTCF_ValidNKey))==0 );
59162  assert( pCur->info.nSize==0 );
59163  if( pCur->eState!=CURSOR_VALID ){
59164    rc = restoreCursorPosition(pCur);
59165    if( rc!=SQLITE_OK ){
59166      return rc;
59167    }
59168    if( CURSOR_INVALID==pCur->eState ){
59169      *pRes = 1;
59170      return SQLITE_OK;
59171    }
59172    if( pCur->skipNext ){
59173      assert( pCur->eState==CURSOR_VALID || pCur->eState==CURSOR_SKIPNEXT );
59174      pCur->eState = CURSOR_VALID;
59175      if( pCur->skipNext<0 ){
59176        pCur->skipNext = 0;
59177        return SQLITE_OK;
59178      }
59179      pCur->skipNext = 0;
59180    }
59181  }
59182
59183  pPage = pCur->apPage[pCur->iPage];
59184  assert( pPage->isInit );
59185  if( !pPage->leaf ){
59186    int idx = pCur->aiIdx[pCur->iPage];
59187    rc = moveToChild(pCur, get4byte(findCell(pPage, idx)));
59188    if( rc ) return rc;
59189    rc = moveToRightmost(pCur);
59190  }else{
59191    while( pCur->aiIdx[pCur->iPage]==0 ){
59192      if( pCur->iPage==0 ){
59193        pCur->eState = CURSOR_INVALID;
59194        *pRes = 1;
59195        return SQLITE_OK;
59196      }
59197      moveToParent(pCur);
59198    }
59199    assert( pCur->info.nSize==0 );
59200    assert( (pCur->curFlags & (BTCF_ValidNKey|BTCF_ValidOvfl))==0 );
59201
59202    pCur->aiIdx[pCur->iPage]--;
59203    pPage = pCur->apPage[pCur->iPage];
59204    if( pPage->intKey && !pPage->leaf ){
59205      rc = sqlite3BtreePrevious(pCur, pRes);
59206    }else{
59207      rc = SQLITE_OK;
59208    }
59209  }
59210  return rc;
59211}
59212SQLITE_PRIVATE int sqlite3BtreePrevious(BtCursor *pCur, int *pRes){
59213  assert( cursorHoldsMutex(pCur) );
59214  assert( pRes!=0 );
59215  assert( *pRes==0 || *pRes==1 );
59216  assert( pCur->skipNext==0 || pCur->eState!=CURSOR_VALID );
59217  *pRes = 0;
59218  pCur->curFlags &= ~(BTCF_AtLast|BTCF_ValidOvfl|BTCF_ValidNKey);
59219  pCur->info.nSize = 0;
59220  if( pCur->eState!=CURSOR_VALID
59221   || pCur->aiIdx[pCur->iPage]==0
59222   || pCur->apPage[pCur->iPage]->leaf==0
59223  ){
59224    return btreePrevious(pCur, pRes);
59225  }
59226  pCur->aiIdx[pCur->iPage]--;
59227  return SQLITE_OK;
59228}
59229
59230/*
59231** Allocate a new page from the database file.
59232**
59233** The new page is marked as dirty.  (In other words, sqlite3PagerWrite()
59234** has already been called on the new page.)  The new page has also
59235** been referenced and the calling routine is responsible for calling
59236** sqlite3PagerUnref() on the new page when it is done.
59237**
59238** SQLITE_OK is returned on success.  Any other return value indicates
59239** an error.  *ppPage is set to NULL in the event of an error.
59240**
59241** If the "nearby" parameter is not 0, then an effort is made to
59242** locate a page close to the page number "nearby".  This can be used in an
59243** attempt to keep related pages close to each other in the database file,
59244** which in turn can make database access faster.
59245**
59246** If the eMode parameter is BTALLOC_EXACT and the nearby page exists
59247** anywhere on the free-list, then it is guaranteed to be returned.  If
59248** eMode is BTALLOC_LT then the page returned will be less than or equal
59249** to nearby if any such page exists.  If eMode is BTALLOC_ANY then there
59250** are no restrictions on which page is returned.
59251*/
59252static int allocateBtreePage(
59253  BtShared *pBt,         /* The btree */
59254  MemPage **ppPage,      /* Store pointer to the allocated page here */
59255  Pgno *pPgno,           /* Store the page number here */
59256  Pgno nearby,           /* Search for a page near this one */
59257  u8 eMode               /* BTALLOC_EXACT, BTALLOC_LT, or BTALLOC_ANY */
59258){
59259  MemPage *pPage1;
59260  int rc;
59261  u32 n;     /* Number of pages on the freelist */
59262  u32 k;     /* Number of leaves on the trunk of the freelist */
59263  MemPage *pTrunk = 0;
59264  MemPage *pPrevTrunk = 0;
59265  Pgno mxPage;     /* Total size of the database file */
59266
59267  assert( sqlite3_mutex_held(pBt->mutex) );
59268  assert( eMode==BTALLOC_ANY || (nearby>0 && IfNotOmitAV(pBt->autoVacuum)) );
59269  pPage1 = pBt->pPage1;
59270  mxPage = btreePagecount(pBt);
59271  /* EVIDENCE-OF: R-05119-02637 The 4-byte big-endian integer at offset 36
59272  ** stores stores the total number of pages on the freelist. */
59273  n = get4byte(&pPage1->aData[36]);
59274  testcase( n==mxPage-1 );
59275  if( n>=mxPage ){
59276    return SQLITE_CORRUPT_BKPT;
59277  }
59278  if( n>0 ){
59279    /* There are pages on the freelist.  Reuse one of those pages. */
59280    Pgno iTrunk;
59281    u8 searchList = 0; /* If the free-list must be searched for 'nearby' */
59282    u32 nSearch = 0;   /* Count of the number of search attempts */
59283
59284    /* If eMode==BTALLOC_EXACT and a query of the pointer-map
59285    ** shows that the page 'nearby' is somewhere on the free-list, then
59286    ** the entire-list will be searched for that page.
59287    */
59288#ifndef SQLITE_OMIT_AUTOVACUUM
59289    if( eMode==BTALLOC_EXACT ){
59290      if( nearby<=mxPage ){
59291        u8 eType;
59292        assert( nearby>0 );
59293        assert( pBt->autoVacuum );
59294        rc = ptrmapGet(pBt, nearby, &eType, 0);
59295        if( rc ) return rc;
59296        if( eType==PTRMAP_FREEPAGE ){
59297          searchList = 1;
59298        }
59299      }
59300    }else if( eMode==BTALLOC_LE ){
59301      searchList = 1;
59302    }
59303#endif
59304
59305    /* Decrement the free-list count by 1. Set iTrunk to the index of the
59306    ** first free-list trunk page. iPrevTrunk is initially 1.
59307    */
59308    rc = sqlite3PagerWrite(pPage1->pDbPage);
59309    if( rc ) return rc;
59310    put4byte(&pPage1->aData[36], n-1);
59311
59312    /* The code within this loop is run only once if the 'searchList' variable
59313    ** is not true. Otherwise, it runs once for each trunk-page on the
59314    ** free-list until the page 'nearby' is located (eMode==BTALLOC_EXACT)
59315    ** or until a page less than 'nearby' is located (eMode==BTALLOC_LT)
59316    */
59317    do {
59318      pPrevTrunk = pTrunk;
59319      if( pPrevTrunk ){
59320        /* EVIDENCE-OF: R-01506-11053 The first integer on a freelist trunk page
59321        ** is the page number of the next freelist trunk page in the list or
59322        ** zero if this is the last freelist trunk page. */
59323        iTrunk = get4byte(&pPrevTrunk->aData[0]);
59324      }else{
59325        /* EVIDENCE-OF: R-59841-13798 The 4-byte big-endian integer at offset 32
59326        ** stores the page number of the first page of the freelist, or zero if
59327        ** the freelist is empty. */
59328        iTrunk = get4byte(&pPage1->aData[32]);
59329      }
59330      testcase( iTrunk==mxPage );
59331      if( iTrunk>mxPage || nSearch++ > n ){
59332        rc = SQLITE_CORRUPT_BKPT;
59333      }else{
59334        rc = btreeGetUnusedPage(pBt, iTrunk, &pTrunk, 0);
59335      }
59336      if( rc ){
59337        pTrunk = 0;
59338        goto end_allocate_page;
59339      }
59340      assert( pTrunk!=0 );
59341      assert( pTrunk->aData!=0 );
59342      /* EVIDENCE-OF: R-13523-04394 The second integer on a freelist trunk page
59343      ** is the number of leaf page pointers to follow. */
59344      k = get4byte(&pTrunk->aData[4]);
59345      if( k==0 && !searchList ){
59346        /* The trunk has no leaves and the list is not being searched.
59347        ** So extract the trunk page itself and use it as the newly
59348        ** allocated page */
59349        assert( pPrevTrunk==0 );
59350        rc = sqlite3PagerWrite(pTrunk->pDbPage);
59351        if( rc ){
59352          goto end_allocate_page;
59353        }
59354        *pPgno = iTrunk;
59355        memcpy(&pPage1->aData[32], &pTrunk->aData[0], 4);
59356        *ppPage = pTrunk;
59357        pTrunk = 0;
59358        TRACE(("ALLOCATE: %d trunk - %d free pages left\n", *pPgno, n-1));
59359      }else if( k>(u32)(pBt->usableSize/4 - 2) ){
59360        /* Value of k is out of range.  Database corruption */
59361        rc = SQLITE_CORRUPT_BKPT;
59362        goto end_allocate_page;
59363#ifndef SQLITE_OMIT_AUTOVACUUM
59364      }else if( searchList
59365            && (nearby==iTrunk || (iTrunk<nearby && eMode==BTALLOC_LE))
59366      ){
59367        /* The list is being searched and this trunk page is the page
59368        ** to allocate, regardless of whether it has leaves.
59369        */
59370        *pPgno = iTrunk;
59371        *ppPage = pTrunk;
59372        searchList = 0;
59373        rc = sqlite3PagerWrite(pTrunk->pDbPage);
59374        if( rc ){
59375          goto end_allocate_page;
59376        }
59377        if( k==0 ){
59378          if( !pPrevTrunk ){
59379            memcpy(&pPage1->aData[32], &pTrunk->aData[0], 4);
59380          }else{
59381            rc = sqlite3PagerWrite(pPrevTrunk->pDbPage);
59382            if( rc!=SQLITE_OK ){
59383              goto end_allocate_page;
59384            }
59385            memcpy(&pPrevTrunk->aData[0], &pTrunk->aData[0], 4);
59386          }
59387        }else{
59388          /* The trunk page is required by the caller but it contains
59389          ** pointers to free-list leaves. The first leaf becomes a trunk
59390          ** page in this case.
59391          */
59392          MemPage *pNewTrunk;
59393          Pgno iNewTrunk = get4byte(&pTrunk->aData[8]);
59394          if( iNewTrunk>mxPage ){
59395            rc = SQLITE_CORRUPT_BKPT;
59396            goto end_allocate_page;
59397          }
59398          testcase( iNewTrunk==mxPage );
59399          rc = btreeGetUnusedPage(pBt, iNewTrunk, &pNewTrunk, 0);
59400          if( rc!=SQLITE_OK ){
59401            goto end_allocate_page;
59402          }
59403          rc = sqlite3PagerWrite(pNewTrunk->pDbPage);
59404          if( rc!=SQLITE_OK ){
59405            releasePage(pNewTrunk);
59406            goto end_allocate_page;
59407          }
59408          memcpy(&pNewTrunk->aData[0], &pTrunk->aData[0], 4);
59409          put4byte(&pNewTrunk->aData[4], k-1);
59410          memcpy(&pNewTrunk->aData[8], &pTrunk->aData[12], (k-1)*4);
59411          releasePage(pNewTrunk);
59412          if( !pPrevTrunk ){
59413            assert( sqlite3PagerIswriteable(pPage1->pDbPage) );
59414            put4byte(&pPage1->aData[32], iNewTrunk);
59415          }else{
59416            rc = sqlite3PagerWrite(pPrevTrunk->pDbPage);
59417            if( rc ){
59418              goto end_allocate_page;
59419            }
59420            put4byte(&pPrevTrunk->aData[0], iNewTrunk);
59421          }
59422        }
59423        pTrunk = 0;
59424        TRACE(("ALLOCATE: %d trunk - %d free pages left\n", *pPgno, n-1));
59425#endif
59426      }else if( k>0 ){
59427        /* Extract a leaf from the trunk */
59428        u32 closest;
59429        Pgno iPage;
59430        unsigned char *aData = pTrunk->aData;
59431        if( nearby>0 ){
59432          u32 i;
59433          closest = 0;
59434          if( eMode==BTALLOC_LE ){
59435            for(i=0; i<k; i++){
59436              iPage = get4byte(&aData[8+i*4]);
59437              if( iPage<=nearby ){
59438                closest = i;
59439                break;
59440              }
59441            }
59442          }else{
59443            int dist;
59444            dist = sqlite3AbsInt32(get4byte(&aData[8]) - nearby);
59445            for(i=1; i<k; i++){
59446              int d2 = sqlite3AbsInt32(get4byte(&aData[8+i*4]) - nearby);
59447              if( d2<dist ){
59448                closest = i;
59449                dist = d2;
59450              }
59451            }
59452          }
59453        }else{
59454          closest = 0;
59455        }
59456
59457        iPage = get4byte(&aData[8+closest*4]);
59458        testcase( iPage==mxPage );
59459        if( iPage>mxPage ){
59460          rc = SQLITE_CORRUPT_BKPT;
59461          goto end_allocate_page;
59462        }
59463        testcase( iPage==mxPage );
59464        if( !searchList
59465         || (iPage==nearby || (iPage<nearby && eMode==BTALLOC_LE))
59466        ){
59467          int noContent;
59468          *pPgno = iPage;
59469          TRACE(("ALLOCATE: %d was leaf %d of %d on trunk %d"
59470                 ": %d more free pages\n",
59471                 *pPgno, closest+1, k, pTrunk->pgno, n-1));
59472          rc = sqlite3PagerWrite(pTrunk->pDbPage);
59473          if( rc ) goto end_allocate_page;
59474          if( closest<k-1 ){
59475            memcpy(&aData[8+closest*4], &aData[4+k*4], 4);
59476          }
59477          put4byte(&aData[4], k-1);
59478          noContent = !btreeGetHasContent(pBt, *pPgno)? PAGER_GET_NOCONTENT : 0;
59479          rc = btreeGetUnusedPage(pBt, *pPgno, ppPage, noContent);
59480          if( rc==SQLITE_OK ){
59481            rc = sqlite3PagerWrite((*ppPage)->pDbPage);
59482            if( rc!=SQLITE_OK ){
59483              releasePage(*ppPage);
59484              *ppPage = 0;
59485            }
59486          }
59487          searchList = 0;
59488        }
59489      }
59490      releasePage(pPrevTrunk);
59491      pPrevTrunk = 0;
59492    }while( searchList );
59493  }else{
59494    /* There are no pages on the freelist, so append a new page to the
59495    ** database image.
59496    **
59497    ** Normally, new pages allocated by this block can be requested from the
59498    ** pager layer with the 'no-content' flag set. This prevents the pager
59499    ** from trying to read the pages content from disk. However, if the
59500    ** current transaction has already run one or more incremental-vacuum
59501    ** steps, then the page we are about to allocate may contain content
59502    ** that is required in the event of a rollback. In this case, do
59503    ** not set the no-content flag. This causes the pager to load and journal
59504    ** the current page content before overwriting it.
59505    **
59506    ** Note that the pager will not actually attempt to load or journal
59507    ** content for any page that really does lie past the end of the database
59508    ** file on disk. So the effects of disabling the no-content optimization
59509    ** here are confined to those pages that lie between the end of the
59510    ** database image and the end of the database file.
59511    */
59512    int bNoContent = (0==IfNotOmitAV(pBt->bDoTruncate))? PAGER_GET_NOCONTENT:0;
59513
59514    rc = sqlite3PagerWrite(pBt->pPage1->pDbPage);
59515    if( rc ) return rc;
59516    pBt->nPage++;
59517    if( pBt->nPage==PENDING_BYTE_PAGE(pBt) ) pBt->nPage++;
59518
59519#ifndef SQLITE_OMIT_AUTOVACUUM
59520    if( pBt->autoVacuum && PTRMAP_ISPAGE(pBt, pBt->nPage) ){
59521      /* If *pPgno refers to a pointer-map page, allocate two new pages
59522      ** at the end of the file instead of one. The first allocated page
59523      ** becomes a new pointer-map page, the second is used by the caller.
59524      */
59525      MemPage *pPg = 0;
59526      TRACE(("ALLOCATE: %d from end of file (pointer-map page)\n", pBt->nPage));
59527      assert( pBt->nPage!=PENDING_BYTE_PAGE(pBt) );
59528      rc = btreeGetUnusedPage(pBt, pBt->nPage, &pPg, bNoContent);
59529      if( rc==SQLITE_OK ){
59530        rc = sqlite3PagerWrite(pPg->pDbPage);
59531        releasePage(pPg);
59532      }
59533      if( rc ) return rc;
59534      pBt->nPage++;
59535      if( pBt->nPage==PENDING_BYTE_PAGE(pBt) ){ pBt->nPage++; }
59536    }
59537#endif
59538    put4byte(28 + (u8*)pBt->pPage1->aData, pBt->nPage);
59539    *pPgno = pBt->nPage;
59540
59541    assert( *pPgno!=PENDING_BYTE_PAGE(pBt) );
59542    rc = btreeGetUnusedPage(pBt, *pPgno, ppPage, bNoContent);
59543    if( rc ) return rc;
59544    rc = sqlite3PagerWrite((*ppPage)->pDbPage);
59545    if( rc!=SQLITE_OK ){
59546      releasePage(*ppPage);
59547      *ppPage = 0;
59548    }
59549    TRACE(("ALLOCATE: %d from end of file\n", *pPgno));
59550  }
59551
59552  assert( *pPgno!=PENDING_BYTE_PAGE(pBt) );
59553
59554end_allocate_page:
59555  releasePage(pTrunk);
59556  releasePage(pPrevTrunk);
59557  assert( rc!=SQLITE_OK || sqlite3PagerPageRefcount((*ppPage)->pDbPage)<=1 );
59558  assert( rc!=SQLITE_OK || (*ppPage)->isInit==0 );
59559  return rc;
59560}
59561
59562/*
59563** This function is used to add page iPage to the database file free-list.
59564** It is assumed that the page is not already a part of the free-list.
59565**
59566** The value passed as the second argument to this function is optional.
59567** If the caller happens to have a pointer to the MemPage object
59568** corresponding to page iPage handy, it may pass it as the second value.
59569** Otherwise, it may pass NULL.
59570**
59571** If a pointer to a MemPage object is passed as the second argument,
59572** its reference count is not altered by this function.
59573*/
59574static int freePage2(BtShared *pBt, MemPage *pMemPage, Pgno iPage){
59575  MemPage *pTrunk = 0;                /* Free-list trunk page */
59576  Pgno iTrunk = 0;                    /* Page number of free-list trunk page */
59577  MemPage *pPage1 = pBt->pPage1;      /* Local reference to page 1 */
59578  MemPage *pPage;                     /* Page being freed. May be NULL. */
59579  int rc;                             /* Return Code */
59580  int nFree;                          /* Initial number of pages on free-list */
59581
59582  assert( sqlite3_mutex_held(pBt->mutex) );
59583  assert( CORRUPT_DB || iPage>1 );
59584  assert( !pMemPage || pMemPage->pgno==iPage );
59585
59586  if( iPage<2 ) return SQLITE_CORRUPT_BKPT;
59587  if( pMemPage ){
59588    pPage = pMemPage;
59589    sqlite3PagerRef(pPage->pDbPage);
59590  }else{
59591    pPage = btreePageLookup(pBt, iPage);
59592  }
59593
59594  /* Increment the free page count on pPage1 */
59595  rc = sqlite3PagerWrite(pPage1->pDbPage);
59596  if( rc ) goto freepage_out;
59597  nFree = get4byte(&pPage1->aData[36]);
59598  put4byte(&pPage1->aData[36], nFree+1);
59599
59600  if( pBt->btsFlags & BTS_SECURE_DELETE ){
59601    /* If the secure_delete option is enabled, then
59602    ** always fully overwrite deleted information with zeros.
59603    */
59604    if( (!pPage && ((rc = btreeGetPage(pBt, iPage, &pPage, 0))!=0) )
59605     ||            ((rc = sqlite3PagerWrite(pPage->pDbPage))!=0)
59606    ){
59607      goto freepage_out;
59608    }
59609    memset(pPage->aData, 0, pPage->pBt->pageSize);
59610  }
59611
59612  /* If the database supports auto-vacuum, write an entry in the pointer-map
59613  ** to indicate that the page is free.
59614  */
59615  if( ISAUTOVACUUM ){
59616    ptrmapPut(pBt, iPage, PTRMAP_FREEPAGE, 0, &rc);
59617    if( rc ) goto freepage_out;
59618  }
59619
59620  /* Now manipulate the actual database free-list structure. There are two
59621  ** possibilities. If the free-list is currently empty, or if the first
59622  ** trunk page in the free-list is full, then this page will become a
59623  ** new free-list trunk page. Otherwise, it will become a leaf of the
59624  ** first trunk page in the current free-list. This block tests if it
59625  ** is possible to add the page as a new free-list leaf.
59626  */
59627  if( nFree!=0 ){
59628    u32 nLeaf;                /* Initial number of leaf cells on trunk page */
59629
59630    iTrunk = get4byte(&pPage1->aData[32]);
59631    rc = btreeGetPage(pBt, iTrunk, &pTrunk, 0);
59632    if( rc!=SQLITE_OK ){
59633      goto freepage_out;
59634    }
59635
59636    nLeaf = get4byte(&pTrunk->aData[4]);
59637    assert( pBt->usableSize>32 );
59638    if( nLeaf > (u32)pBt->usableSize/4 - 2 ){
59639      rc = SQLITE_CORRUPT_BKPT;
59640      goto freepage_out;
59641    }
59642    if( nLeaf < (u32)pBt->usableSize/4 - 8 ){
59643      /* In this case there is room on the trunk page to insert the page
59644      ** being freed as a new leaf.
59645      **
59646      ** Note that the trunk page is not really full until it contains
59647      ** usableSize/4 - 2 entries, not usableSize/4 - 8 entries as we have
59648      ** coded.  But due to a coding error in versions of SQLite prior to
59649      ** 3.6.0, databases with freelist trunk pages holding more than
59650      ** usableSize/4 - 8 entries will be reported as corrupt.  In order
59651      ** to maintain backwards compatibility with older versions of SQLite,
59652      ** we will continue to restrict the number of entries to usableSize/4 - 8
59653      ** for now.  At some point in the future (once everyone has upgraded
59654      ** to 3.6.0 or later) we should consider fixing the conditional above
59655      ** to read "usableSize/4-2" instead of "usableSize/4-8".
59656      **
59657      ** EVIDENCE-OF: R-19920-11576 However, newer versions of SQLite still
59658      ** avoid using the last six entries in the freelist trunk page array in
59659      ** order that database files created by newer versions of SQLite can be
59660      ** read by older versions of SQLite.
59661      */
59662      rc = sqlite3PagerWrite(pTrunk->pDbPage);
59663      if( rc==SQLITE_OK ){
59664        put4byte(&pTrunk->aData[4], nLeaf+1);
59665        put4byte(&pTrunk->aData[8+nLeaf*4], iPage);
59666        if( pPage && (pBt->btsFlags & BTS_SECURE_DELETE)==0 ){
59667          sqlite3PagerDontWrite(pPage->pDbPage);
59668        }
59669        rc = btreeSetHasContent(pBt, iPage);
59670      }
59671      TRACE(("FREE-PAGE: %d leaf on trunk page %d\n",pPage->pgno,pTrunk->pgno));
59672      goto freepage_out;
59673    }
59674  }
59675
59676  /* If control flows to this point, then it was not possible to add the
59677  ** the page being freed as a leaf page of the first trunk in the free-list.
59678  ** Possibly because the free-list is empty, or possibly because the
59679  ** first trunk in the free-list is full. Either way, the page being freed
59680  ** will become the new first trunk page in the free-list.
59681  */
59682  if( pPage==0 && SQLITE_OK!=(rc = btreeGetPage(pBt, iPage, &pPage, 0)) ){
59683    goto freepage_out;
59684  }
59685  rc = sqlite3PagerWrite(pPage->pDbPage);
59686  if( rc!=SQLITE_OK ){
59687    goto freepage_out;
59688  }
59689  put4byte(pPage->aData, iTrunk);
59690  put4byte(&pPage->aData[4], 0);
59691  put4byte(&pPage1->aData[32], iPage);
59692  TRACE(("FREE-PAGE: %d new trunk page replacing %d\n", pPage->pgno, iTrunk));
59693
59694freepage_out:
59695  if( pPage ){
59696    pPage->isInit = 0;
59697  }
59698  releasePage(pPage);
59699  releasePage(pTrunk);
59700  return rc;
59701}
59702static void freePage(MemPage *pPage, int *pRC){
59703  if( (*pRC)==SQLITE_OK ){
59704    *pRC = freePage2(pPage->pBt, pPage, pPage->pgno);
59705  }
59706}
59707
59708/*
59709** Free any overflow pages associated with the given Cell.  Write the
59710** local Cell size (the number of bytes on the original page, omitting
59711** overflow) into *pnSize.
59712*/
59713static int clearCell(
59714  MemPage *pPage,          /* The page that contains the Cell */
59715  unsigned char *pCell,    /* First byte of the Cell */
59716  u16 *pnSize              /* Write the size of the Cell here */
59717){
59718  BtShared *pBt = pPage->pBt;
59719  CellInfo info;
59720  Pgno ovflPgno;
59721  int rc;
59722  int nOvfl;
59723  u32 ovflPageSize;
59724
59725  assert( sqlite3_mutex_held(pPage->pBt->mutex) );
59726  pPage->xParseCell(pPage, pCell, &info);
59727  *pnSize = info.nSize;
59728  if( info.iOverflow==0 ){
59729    return SQLITE_OK;  /* No overflow pages. Return without doing anything */
59730  }
59731  if( pCell+info.iOverflow+3 > pPage->aData+pPage->maskPage ){
59732    return SQLITE_CORRUPT_BKPT;  /* Cell extends past end of page */
59733  }
59734  ovflPgno = get4byte(&pCell[info.iOverflow]);
59735  assert( pBt->usableSize > 4 );
59736  ovflPageSize = pBt->usableSize - 4;
59737  nOvfl = (info.nPayload - info.nLocal + ovflPageSize - 1)/ovflPageSize;
59738  assert( nOvfl>0 ||
59739    (CORRUPT_DB && (info.nPayload + ovflPageSize)<ovflPageSize)
59740  );
59741  while( nOvfl-- ){
59742    Pgno iNext = 0;
59743    MemPage *pOvfl = 0;
59744    if( ovflPgno<2 || ovflPgno>btreePagecount(pBt) ){
59745      /* 0 is not a legal page number and page 1 cannot be an
59746      ** overflow page. Therefore if ovflPgno<2 or past the end of the
59747      ** file the database must be corrupt. */
59748      return SQLITE_CORRUPT_BKPT;
59749    }
59750    if( nOvfl ){
59751      rc = getOverflowPage(pBt, ovflPgno, &pOvfl, &iNext);
59752      if( rc ) return rc;
59753    }
59754
59755    if( ( pOvfl || ((pOvfl = btreePageLookup(pBt, ovflPgno))!=0) )
59756     && sqlite3PagerPageRefcount(pOvfl->pDbPage)!=1
59757    ){
59758      /* There is no reason any cursor should have an outstanding reference
59759      ** to an overflow page belonging to a cell that is being deleted/updated.
59760      ** So if there exists more than one reference to this page, then it
59761      ** must not really be an overflow page and the database must be corrupt.
59762      ** It is helpful to detect this before calling freePage2(), as
59763      ** freePage2() may zero the page contents if secure-delete mode is
59764      ** enabled. If this 'overflow' page happens to be a page that the
59765      ** caller is iterating through or using in some other way, this
59766      ** can be problematic.
59767      */
59768      rc = SQLITE_CORRUPT_BKPT;
59769    }else{
59770      rc = freePage2(pBt, pOvfl, ovflPgno);
59771    }
59772
59773    if( pOvfl ){
59774      sqlite3PagerUnref(pOvfl->pDbPage);
59775    }
59776    if( rc ) return rc;
59777    ovflPgno = iNext;
59778  }
59779  return SQLITE_OK;
59780}
59781
59782/*
59783** Create the byte sequence used to represent a cell on page pPage
59784** and write that byte sequence into pCell[].  Overflow pages are
59785** allocated and filled in as necessary.  The calling procedure
59786** is responsible for making sure sufficient space has been allocated
59787** for pCell[].
59788**
59789** Note that pCell does not necessary need to point to the pPage->aData
59790** area.  pCell might point to some temporary storage.  The cell will
59791** be constructed in this temporary area then copied into pPage->aData
59792** later.
59793*/
59794static int fillInCell(
59795  MemPage *pPage,                /* The page that contains the cell */
59796  unsigned char *pCell,          /* Complete text of the cell */
59797  const void *pKey, i64 nKey,    /* The key */
59798  const void *pData,int nData,   /* The data */
59799  int nZero,                     /* Extra zero bytes to append to pData */
59800  int *pnSize                    /* Write cell size here */
59801){
59802  int nPayload;
59803  const u8 *pSrc;
59804  int nSrc, n, rc;
59805  int spaceLeft;
59806  MemPage *pOvfl = 0;
59807  MemPage *pToRelease = 0;
59808  unsigned char *pPrior;
59809  unsigned char *pPayload;
59810  BtShared *pBt = pPage->pBt;
59811  Pgno pgnoOvfl = 0;
59812  int nHeader;
59813
59814  assert( sqlite3_mutex_held(pPage->pBt->mutex) );
59815
59816  /* pPage is not necessarily writeable since pCell might be auxiliary
59817  ** buffer space that is separate from the pPage buffer area */
59818  assert( pCell<pPage->aData || pCell>=&pPage->aData[pBt->pageSize]
59819            || sqlite3PagerIswriteable(pPage->pDbPage) );
59820
59821  /* Fill in the header. */
59822  nHeader = pPage->childPtrSize;
59823  nPayload = nData + nZero;
59824  if( pPage->intKeyLeaf ){
59825    nHeader += putVarint32(&pCell[nHeader], nPayload);
59826  }else{
59827    assert( nData==0 );
59828    assert( nZero==0 );
59829  }
59830  nHeader += putVarint(&pCell[nHeader], *(u64*)&nKey);
59831
59832  /* Fill in the payload size */
59833  if( pPage->intKey ){
59834    pSrc = pData;
59835    nSrc = nData;
59836    nData = 0;
59837  }else{
59838    assert( nKey<=0x7fffffff && pKey!=0 );
59839    nPayload = (int)nKey;
59840    pSrc = pKey;
59841    nSrc = (int)nKey;
59842  }
59843  if( nPayload<=pPage->maxLocal ){
59844    n = nHeader + nPayload;
59845    testcase( n==3 );
59846    testcase( n==4 );
59847    if( n<4 ) n = 4;
59848    *pnSize = n;
59849    spaceLeft = nPayload;
59850    pPrior = pCell;
59851  }else{
59852    int mn = pPage->minLocal;
59853    n = mn + (nPayload - mn) % (pPage->pBt->usableSize - 4);
59854    testcase( n==pPage->maxLocal );
59855    testcase( n==pPage->maxLocal+1 );
59856    if( n > pPage->maxLocal ) n = mn;
59857    spaceLeft = n;
59858    *pnSize = n + nHeader + 4;
59859    pPrior = &pCell[nHeader+n];
59860  }
59861  pPayload = &pCell[nHeader];
59862
59863  /* At this point variables should be set as follows:
59864  **
59865  **   nPayload           Total payload size in bytes
59866  **   pPayload           Begin writing payload here
59867  **   spaceLeft          Space available at pPayload.  If nPayload>spaceLeft,
59868  **                      that means content must spill into overflow pages.
59869  **   *pnSize            Size of the local cell (not counting overflow pages)
59870  **   pPrior             Where to write the pgno of the first overflow page
59871  **
59872  ** Use a call to btreeParseCellPtr() to verify that the values above
59873  ** were computed correctly.
59874  */
59875#if SQLITE_DEBUG
59876  {
59877    CellInfo info;
59878    pPage->xParseCell(pPage, pCell, &info);
59879    assert( nHeader=(int)(info.pPayload - pCell) );
59880    assert( info.nKey==nKey );
59881    assert( *pnSize == info.nSize );
59882    assert( spaceLeft == info.nLocal );
59883    assert( pPrior == &pCell[info.iOverflow] );
59884  }
59885#endif
59886
59887  /* Write the payload into the local Cell and any extra into overflow pages */
59888  while( nPayload>0 ){
59889    if( spaceLeft==0 ){
59890#ifndef SQLITE_OMIT_AUTOVACUUM
59891      Pgno pgnoPtrmap = pgnoOvfl; /* Overflow page pointer-map entry page */
59892      if( pBt->autoVacuum ){
59893        do{
59894          pgnoOvfl++;
59895        } while(
59896          PTRMAP_ISPAGE(pBt, pgnoOvfl) || pgnoOvfl==PENDING_BYTE_PAGE(pBt)
59897        );
59898      }
59899#endif
59900      rc = allocateBtreePage(pBt, &pOvfl, &pgnoOvfl, pgnoOvfl, 0);
59901#ifndef SQLITE_OMIT_AUTOVACUUM
59902      /* If the database supports auto-vacuum, and the second or subsequent
59903      ** overflow page is being allocated, add an entry to the pointer-map
59904      ** for that page now.
59905      **
59906      ** If this is the first overflow page, then write a partial entry
59907      ** to the pointer-map. If we write nothing to this pointer-map slot,
59908      ** then the optimistic overflow chain processing in clearCell()
59909      ** may misinterpret the uninitialized values and delete the
59910      ** wrong pages from the database.
59911      */
59912      if( pBt->autoVacuum && rc==SQLITE_OK ){
59913        u8 eType = (pgnoPtrmap?PTRMAP_OVERFLOW2:PTRMAP_OVERFLOW1);
59914        ptrmapPut(pBt, pgnoOvfl, eType, pgnoPtrmap, &rc);
59915        if( rc ){
59916          releasePage(pOvfl);
59917        }
59918      }
59919#endif
59920      if( rc ){
59921        releasePage(pToRelease);
59922        return rc;
59923      }
59924
59925      /* If pToRelease is not zero than pPrior points into the data area
59926      ** of pToRelease.  Make sure pToRelease is still writeable. */
59927      assert( pToRelease==0 || sqlite3PagerIswriteable(pToRelease->pDbPage) );
59928
59929      /* If pPrior is part of the data area of pPage, then make sure pPage
59930      ** is still writeable */
59931      assert( pPrior<pPage->aData || pPrior>=&pPage->aData[pBt->pageSize]
59932            || sqlite3PagerIswriteable(pPage->pDbPage) );
59933
59934      put4byte(pPrior, pgnoOvfl);
59935      releasePage(pToRelease);
59936      pToRelease = pOvfl;
59937      pPrior = pOvfl->aData;
59938      put4byte(pPrior, 0);
59939      pPayload = &pOvfl->aData[4];
59940      spaceLeft = pBt->usableSize - 4;
59941    }
59942    n = nPayload;
59943    if( n>spaceLeft ) n = spaceLeft;
59944
59945    /* If pToRelease is not zero than pPayload points into the data area
59946    ** of pToRelease.  Make sure pToRelease is still writeable. */
59947    assert( pToRelease==0 || sqlite3PagerIswriteable(pToRelease->pDbPage) );
59948
59949    /* If pPayload is part of the data area of pPage, then make sure pPage
59950    ** is still writeable */
59951    assert( pPayload<pPage->aData || pPayload>=&pPage->aData[pBt->pageSize]
59952            || sqlite3PagerIswriteable(pPage->pDbPage) );
59953
59954    if( nSrc>0 ){
59955      if( n>nSrc ) n = nSrc;
59956      assert( pSrc );
59957      memcpy(pPayload, pSrc, n);
59958    }else{
59959      memset(pPayload, 0, n);
59960    }
59961    nPayload -= n;
59962    pPayload += n;
59963    pSrc += n;
59964    nSrc -= n;
59965    spaceLeft -= n;
59966    if( nSrc==0 ){
59967      nSrc = nData;
59968      pSrc = pData;
59969    }
59970  }
59971  releasePage(pToRelease);
59972  return SQLITE_OK;
59973}
59974
59975/*
59976** Remove the i-th cell from pPage.  This routine effects pPage only.
59977** The cell content is not freed or deallocated.  It is assumed that
59978** the cell content has been copied someplace else.  This routine just
59979** removes the reference to the cell from pPage.
59980**
59981** "sz" must be the number of bytes in the cell.
59982*/
59983static void dropCell(MemPage *pPage, int idx, int sz, int *pRC){
59984  u32 pc;         /* Offset to cell content of cell being deleted */
59985  u8 *data;       /* pPage->aData */
59986  u8 *ptr;        /* Used to move bytes around within data[] */
59987  int rc;         /* The return code */
59988  int hdr;        /* Beginning of the header.  0 most pages.  100 page 1 */
59989
59990  if( *pRC ) return;
59991
59992  assert( idx>=0 && idx<pPage->nCell );
59993  assert( CORRUPT_DB || sz==cellSize(pPage, idx) );
59994  assert( sqlite3PagerIswriteable(pPage->pDbPage) );
59995  assert( sqlite3_mutex_held(pPage->pBt->mutex) );
59996  data = pPage->aData;
59997  ptr = &pPage->aCellIdx[2*idx];
59998  pc = get2byte(ptr);
59999  hdr = pPage->hdrOffset;
60000  testcase( pc==get2byte(&data[hdr+5]) );
60001  testcase( pc+sz==pPage->pBt->usableSize );
60002  if( pc < (u32)get2byte(&data[hdr+5]) || pc+sz > pPage->pBt->usableSize ){
60003    *pRC = SQLITE_CORRUPT_BKPT;
60004    return;
60005  }
60006  rc = freeSpace(pPage, pc, sz);
60007  if( rc ){
60008    *pRC = rc;
60009    return;
60010  }
60011  pPage->nCell--;
60012  if( pPage->nCell==0 ){
60013    memset(&data[hdr+1], 0, 4);
60014    data[hdr+7] = 0;
60015    put2byte(&data[hdr+5], pPage->pBt->usableSize);
60016    pPage->nFree = pPage->pBt->usableSize - pPage->hdrOffset
60017                       - pPage->childPtrSize - 8;
60018  }else{
60019    memmove(ptr, ptr+2, 2*(pPage->nCell - idx));
60020    put2byte(&data[hdr+3], pPage->nCell);
60021    pPage->nFree += 2;
60022  }
60023}
60024
60025/*
60026** Insert a new cell on pPage at cell index "i".  pCell points to the
60027** content of the cell.
60028**
60029** If the cell content will fit on the page, then put it there.  If it
60030** will not fit, then make a copy of the cell content into pTemp if
60031** pTemp is not null.  Regardless of pTemp, allocate a new entry
60032** in pPage->apOvfl[] and make it point to the cell content (either
60033** in pTemp or the original pCell) and also record its index.
60034** Allocating a new entry in pPage->aCell[] implies that
60035** pPage->nOverflow is incremented.
60036*/
60037static void insertCell(
60038  MemPage *pPage,   /* Page into which we are copying */
60039  int i,            /* New cell becomes the i-th cell of the page */
60040  u8 *pCell,        /* Content of the new cell */
60041  int sz,           /* Bytes of content in pCell */
60042  u8 *pTemp,        /* Temp storage space for pCell, if needed */
60043  Pgno iChild,      /* If non-zero, replace first 4 bytes with this value */
60044  int *pRC          /* Read and write return code from here */
60045){
60046  int idx = 0;      /* Where to write new cell content in data[] */
60047  int j;            /* Loop counter */
60048  u8 *data;         /* The content of the whole page */
60049  u8 *pIns;         /* The point in pPage->aCellIdx[] where no cell inserted */
60050
60051  if( *pRC ) return;
60052
60053  assert( i>=0 && i<=pPage->nCell+pPage->nOverflow );
60054  assert( MX_CELL(pPage->pBt)<=10921 );
60055  assert( pPage->nCell<=MX_CELL(pPage->pBt) || CORRUPT_DB );
60056  assert( pPage->nOverflow<=ArraySize(pPage->apOvfl) );
60057  assert( ArraySize(pPage->apOvfl)==ArraySize(pPage->aiOvfl) );
60058  assert( sqlite3_mutex_held(pPage->pBt->mutex) );
60059  /* The cell should normally be sized correctly.  However, when moving a
60060  ** malformed cell from a leaf page to an interior page, if the cell size
60061  ** wanted to be less than 4 but got rounded up to 4 on the leaf, then size
60062  ** might be less than 8 (leaf-size + pointer) on the interior node.  Hence
60063  ** the term after the || in the following assert(). */
60064  assert( sz==pPage->xCellSize(pPage, pCell) || (sz==8 && iChild>0) );
60065  if( pPage->nOverflow || sz+2>pPage->nFree ){
60066    if( pTemp ){
60067      memcpy(pTemp, pCell, sz);
60068      pCell = pTemp;
60069    }
60070    if( iChild ){
60071      put4byte(pCell, iChild);
60072    }
60073    j = pPage->nOverflow++;
60074    assert( j<(int)(sizeof(pPage->apOvfl)/sizeof(pPage->apOvfl[0])) );
60075    pPage->apOvfl[j] = pCell;
60076    pPage->aiOvfl[j] = (u16)i;
60077
60078    /* When multiple overflows occur, they are always sequential and in
60079    ** sorted order.  This invariants arise because multiple overflows can
60080    ** only occur when inserting divider cells into the parent page during
60081    ** balancing, and the dividers are adjacent and sorted.
60082    */
60083    assert( j==0 || pPage->aiOvfl[j-1]<(u16)i ); /* Overflows in sorted order */
60084    assert( j==0 || i==pPage->aiOvfl[j-1]+1 );   /* Overflows are sequential */
60085  }else{
60086    int rc = sqlite3PagerWrite(pPage->pDbPage);
60087    if( rc!=SQLITE_OK ){
60088      *pRC = rc;
60089      return;
60090    }
60091    assert( sqlite3PagerIswriteable(pPage->pDbPage) );
60092    data = pPage->aData;
60093    assert( &data[pPage->cellOffset]==pPage->aCellIdx );
60094    rc = allocateSpace(pPage, sz, &idx);
60095    if( rc ){ *pRC = rc; return; }
60096    /* The allocateSpace() routine guarantees the following properties
60097    ** if it returns successfully */
60098    assert( idx >= 0 );
60099    assert( idx >= pPage->cellOffset+2*pPage->nCell+2 || CORRUPT_DB );
60100    assert( idx+sz <= (int)pPage->pBt->usableSize );
60101    pPage->nFree -= (u16)(2 + sz);
60102    memcpy(&data[idx], pCell, sz);
60103    if( iChild ){
60104      put4byte(&data[idx], iChild);
60105    }
60106    pIns = pPage->aCellIdx + i*2;
60107    memmove(pIns+2, pIns, 2*(pPage->nCell - i));
60108    put2byte(pIns, idx);
60109    pPage->nCell++;
60110    /* increment the cell count */
60111    if( (++data[pPage->hdrOffset+4])==0 ) data[pPage->hdrOffset+3]++;
60112    assert( get2byte(&data[pPage->hdrOffset+3])==pPage->nCell );
60113#ifndef SQLITE_OMIT_AUTOVACUUM
60114    if( pPage->pBt->autoVacuum ){
60115      /* The cell may contain a pointer to an overflow page. If so, write
60116      ** the entry for the overflow page into the pointer map.
60117      */
60118      ptrmapPutOvflPtr(pPage, pCell, pRC);
60119    }
60120#endif
60121  }
60122}
60123
60124/*
60125** A CellArray object contains a cache of pointers and sizes for a
60126** consecutive sequence of cells that might be held multiple pages.
60127*/
60128typedef struct CellArray CellArray;
60129struct CellArray {
60130  int nCell;              /* Number of cells in apCell[] */
60131  MemPage *pRef;          /* Reference page */
60132  u8 **apCell;            /* All cells begin balanced */
60133  u16 *szCell;            /* Local size of all cells in apCell[] */
60134};
60135
60136/*
60137** Make sure the cell sizes at idx, idx+1, ..., idx+N-1 have been
60138** computed.
60139*/
60140static void populateCellCache(CellArray *p, int idx, int N){
60141  assert( idx>=0 && idx+N<=p->nCell );
60142  while( N>0 ){
60143    assert( p->apCell[idx]!=0 );
60144    if( p->szCell[idx]==0 ){
60145      p->szCell[idx] = p->pRef->xCellSize(p->pRef, p->apCell[idx]);
60146    }else{
60147      assert( CORRUPT_DB ||
60148              p->szCell[idx]==p->pRef->xCellSize(p->pRef, p->apCell[idx]) );
60149    }
60150    idx++;
60151    N--;
60152  }
60153}
60154
60155/*
60156** Return the size of the Nth element of the cell array
60157*/
60158static SQLITE_NOINLINE u16 computeCellSize(CellArray *p, int N){
60159  assert( N>=0 && N<p->nCell );
60160  assert( p->szCell[N]==0 );
60161  p->szCell[N] = p->pRef->xCellSize(p->pRef, p->apCell[N]);
60162  return p->szCell[N];
60163}
60164static u16 cachedCellSize(CellArray *p, int N){
60165  assert( N>=0 && N<p->nCell );
60166  if( p->szCell[N] ) return p->szCell[N];
60167  return computeCellSize(p, N);
60168}
60169
60170/*
60171** Array apCell[] contains pointers to nCell b-tree page cells. The
60172** szCell[] array contains the size in bytes of each cell. This function
60173** replaces the current contents of page pPg with the contents of the cell
60174** array.
60175**
60176** Some of the cells in apCell[] may currently be stored in pPg. This
60177** function works around problems caused by this by making a copy of any
60178** such cells before overwriting the page data.
60179**
60180** The MemPage.nFree field is invalidated by this function. It is the
60181** responsibility of the caller to set it correctly.
60182*/
60183static int rebuildPage(
60184  MemPage *pPg,                   /* Edit this page */
60185  int nCell,                      /* Final number of cells on page */
60186  u8 **apCell,                    /* Array of cells */
60187  u16 *szCell                     /* Array of cell sizes */
60188){
60189  const int hdr = pPg->hdrOffset;          /* Offset of header on pPg */
60190  u8 * const aData = pPg->aData;           /* Pointer to data for pPg */
60191  const int usableSize = pPg->pBt->usableSize;
60192  u8 * const pEnd = &aData[usableSize];
60193  int i;
60194  u8 *pCellptr = pPg->aCellIdx;
60195  u8 *pTmp = sqlite3PagerTempSpace(pPg->pBt->pPager);
60196  u8 *pData;
60197
60198  i = get2byte(&aData[hdr+5]);
60199  memcpy(&pTmp[i], &aData[i], usableSize - i);
60200
60201  pData = pEnd;
60202  for(i=0; i<nCell; i++){
60203    u8 *pCell = apCell[i];
60204    if( pCell>aData && pCell<pEnd ){
60205      pCell = &pTmp[pCell - aData];
60206    }
60207    pData -= szCell[i];
60208    put2byte(pCellptr, (pData - aData));
60209    pCellptr += 2;
60210    if( pData < pCellptr ) return SQLITE_CORRUPT_BKPT;
60211    memcpy(pData, pCell, szCell[i]);
60212    assert( szCell[i]==pPg->xCellSize(pPg, pCell) || CORRUPT_DB );
60213    testcase( szCell[i]!=pPg->xCellSize(pPg,pCell) );
60214  }
60215
60216  /* The pPg->nFree field is now set incorrectly. The caller will fix it. */
60217  pPg->nCell = nCell;
60218  pPg->nOverflow = 0;
60219
60220  put2byte(&aData[hdr+1], 0);
60221  put2byte(&aData[hdr+3], pPg->nCell);
60222  put2byte(&aData[hdr+5], pData - aData);
60223  aData[hdr+7] = 0x00;
60224  return SQLITE_OK;
60225}
60226
60227/*
60228** Array apCell[] contains nCell pointers to b-tree cells. Array szCell
60229** contains the size in bytes of each such cell. This function attempts to
60230** add the cells stored in the array to page pPg. If it cannot (because
60231** the page needs to be defragmented before the cells will fit), non-zero
60232** is returned. Otherwise, if the cells are added successfully, zero is
60233** returned.
60234**
60235** Argument pCellptr points to the first entry in the cell-pointer array
60236** (part of page pPg) to populate. After cell apCell[0] is written to the
60237** page body, a 16-bit offset is written to pCellptr. And so on, for each
60238** cell in the array. It is the responsibility of the caller to ensure
60239** that it is safe to overwrite this part of the cell-pointer array.
60240**
60241** When this function is called, *ppData points to the start of the
60242** content area on page pPg. If the size of the content area is extended,
60243** *ppData is updated to point to the new start of the content area
60244** before returning.
60245**
60246** Finally, argument pBegin points to the byte immediately following the
60247** end of the space required by this page for the cell-pointer area (for
60248** all cells - not just those inserted by the current call). If the content
60249** area must be extended to before this point in order to accomodate all
60250** cells in apCell[], then the cells do not fit and non-zero is returned.
60251*/
60252static int pageInsertArray(
60253  MemPage *pPg,                   /* Page to add cells to */
60254  u8 *pBegin,                     /* End of cell-pointer array */
60255  u8 **ppData,                    /* IN/OUT: Page content -area pointer */
60256  u8 *pCellptr,                   /* Pointer to cell-pointer area */
60257  int iFirst,                     /* Index of first cell to add */
60258  int nCell,                      /* Number of cells to add to pPg */
60259  CellArray *pCArray              /* Array of cells */
60260){
60261  int i;
60262  u8 *aData = pPg->aData;
60263  u8 *pData = *ppData;
60264  int iEnd = iFirst + nCell;
60265  assert( CORRUPT_DB || pPg->hdrOffset==0 );    /* Never called on page 1 */
60266  for(i=iFirst; i<iEnd; i++){
60267    int sz, rc;
60268    u8 *pSlot;
60269    sz = cachedCellSize(pCArray, i);
60270    if( (aData[1]==0 && aData[2]==0) || (pSlot = pageFindSlot(pPg,sz,&rc))==0 ){
60271      pData -= sz;
60272      if( pData<pBegin ) return 1;
60273      pSlot = pData;
60274    }
60275    memcpy(pSlot, pCArray->apCell[i], sz);
60276    put2byte(pCellptr, (pSlot - aData));
60277    pCellptr += 2;
60278  }
60279  *ppData = pData;
60280  return 0;
60281}
60282
60283/*
60284** Array apCell[] contains nCell pointers to b-tree cells. Array szCell
60285** contains the size in bytes of each such cell. This function adds the
60286** space associated with each cell in the array that is currently stored
60287** within the body of pPg to the pPg free-list. The cell-pointers and other
60288** fields of the page are not updated.
60289**
60290** This function returns the total number of cells added to the free-list.
60291*/
60292static int pageFreeArray(
60293  MemPage *pPg,                   /* Page to edit */
60294  int iFirst,                     /* First cell to delete */
60295  int nCell,                      /* Cells to delete */
60296  CellArray *pCArray              /* Array of cells */
60297){
60298  u8 * const aData = pPg->aData;
60299  u8 * const pEnd = &aData[pPg->pBt->usableSize];
60300  u8 * const pStart = &aData[pPg->hdrOffset + 8 + pPg->childPtrSize];
60301  int nRet = 0;
60302  int i;
60303  int iEnd = iFirst + nCell;
60304  u8 *pFree = 0;
60305  int szFree = 0;
60306
60307  for(i=iFirst; i<iEnd; i++){
60308    u8 *pCell = pCArray->apCell[i];
60309    if( pCell>=pStart && pCell<pEnd ){
60310      int sz;
60311      /* No need to use cachedCellSize() here.  The sizes of all cells that
60312      ** are to be freed have already been computing while deciding which
60313      ** cells need freeing */
60314      sz = pCArray->szCell[i];  assert( sz>0 );
60315      if( pFree!=(pCell + sz) ){
60316        if( pFree ){
60317          assert( pFree>aData && (pFree - aData)<65536 );
60318          freeSpace(pPg, (u16)(pFree - aData), szFree);
60319        }
60320        pFree = pCell;
60321        szFree = sz;
60322        if( pFree+sz>pEnd ) return 0;
60323      }else{
60324        pFree = pCell;
60325        szFree += sz;
60326      }
60327      nRet++;
60328    }
60329  }
60330  if( pFree ){
60331    assert( pFree>aData && (pFree - aData)<65536 );
60332    freeSpace(pPg, (u16)(pFree - aData), szFree);
60333  }
60334  return nRet;
60335}
60336
60337/*
60338** apCell[] and szCell[] contains pointers to and sizes of all cells in the
60339** pages being balanced.  The current page, pPg, has pPg->nCell cells starting
60340** with apCell[iOld].  After balancing, this page should hold nNew cells
60341** starting at apCell[iNew].
60342**
60343** This routine makes the necessary adjustments to pPg so that it contains
60344** the correct cells after being balanced.
60345**
60346** The pPg->nFree field is invalid when this function returns. It is the
60347** responsibility of the caller to set it correctly.
60348*/
60349static int editPage(
60350  MemPage *pPg,                   /* Edit this page */
60351  int iOld,                       /* Index of first cell currently on page */
60352  int iNew,                       /* Index of new first cell on page */
60353  int nNew,                       /* Final number of cells on page */
60354  CellArray *pCArray              /* Array of cells and sizes */
60355){
60356  u8 * const aData = pPg->aData;
60357  const int hdr = pPg->hdrOffset;
60358  u8 *pBegin = &pPg->aCellIdx[nNew * 2];
60359  int nCell = pPg->nCell;       /* Cells stored on pPg */
60360  u8 *pData;
60361  u8 *pCellptr;
60362  int i;
60363  int iOldEnd = iOld + pPg->nCell + pPg->nOverflow;
60364  int iNewEnd = iNew + nNew;
60365
60366#ifdef SQLITE_DEBUG
60367  u8 *pTmp = sqlite3PagerTempSpace(pPg->pBt->pPager);
60368  memcpy(pTmp, aData, pPg->pBt->usableSize);
60369#endif
60370
60371  /* Remove cells from the start and end of the page */
60372  if( iOld<iNew ){
60373    int nShift = pageFreeArray(pPg, iOld, iNew-iOld, pCArray);
60374    memmove(pPg->aCellIdx, &pPg->aCellIdx[nShift*2], nCell*2);
60375    nCell -= nShift;
60376  }
60377  if( iNewEnd < iOldEnd ){
60378    nCell -= pageFreeArray(pPg, iNewEnd, iOldEnd - iNewEnd, pCArray);
60379  }
60380
60381  pData = &aData[get2byteNotZero(&aData[hdr+5])];
60382  if( pData<pBegin ) goto editpage_fail;
60383
60384  /* Add cells to the start of the page */
60385  if( iNew<iOld ){
60386    int nAdd = MIN(nNew,iOld-iNew);
60387    assert( (iOld-iNew)<nNew || nCell==0 || CORRUPT_DB );
60388    pCellptr = pPg->aCellIdx;
60389    memmove(&pCellptr[nAdd*2], pCellptr, nCell*2);
60390    if( pageInsertArray(
60391          pPg, pBegin, &pData, pCellptr,
60392          iNew, nAdd, pCArray
60393    ) ) goto editpage_fail;
60394    nCell += nAdd;
60395  }
60396
60397  /* Add any overflow cells */
60398  for(i=0; i<pPg->nOverflow; i++){
60399    int iCell = (iOld + pPg->aiOvfl[i]) - iNew;
60400    if( iCell>=0 && iCell<nNew ){
60401      pCellptr = &pPg->aCellIdx[iCell * 2];
60402      memmove(&pCellptr[2], pCellptr, (nCell - iCell) * 2);
60403      nCell++;
60404      if( pageInsertArray(
60405            pPg, pBegin, &pData, pCellptr,
60406            iCell+iNew, 1, pCArray
60407      ) ) goto editpage_fail;
60408    }
60409  }
60410
60411  /* Append cells to the end of the page */
60412  pCellptr = &pPg->aCellIdx[nCell*2];
60413  if( pageInsertArray(
60414        pPg, pBegin, &pData, pCellptr,
60415        iNew+nCell, nNew-nCell, pCArray
60416  ) ) goto editpage_fail;
60417
60418  pPg->nCell = nNew;
60419  pPg->nOverflow = 0;
60420
60421  put2byte(&aData[hdr+3], pPg->nCell);
60422  put2byte(&aData[hdr+5], pData - aData);
60423
60424#ifdef SQLITE_DEBUG
60425  for(i=0; i<nNew && !CORRUPT_DB; i++){
60426    u8 *pCell = pCArray->apCell[i+iNew];
60427    int iOff = get2byteAligned(&pPg->aCellIdx[i*2]);
60428    if( pCell>=aData && pCell<&aData[pPg->pBt->usableSize] ){
60429      pCell = &pTmp[pCell - aData];
60430    }
60431    assert( 0==memcmp(pCell, &aData[iOff],
60432            pCArray->pRef->xCellSize(pCArray->pRef, pCArray->apCell[i+iNew])) );
60433  }
60434#endif
60435
60436  return SQLITE_OK;
60437 editpage_fail:
60438  /* Unable to edit this page. Rebuild it from scratch instead. */
60439  populateCellCache(pCArray, iNew, nNew);
60440  return rebuildPage(pPg, nNew, &pCArray->apCell[iNew], &pCArray->szCell[iNew]);
60441}
60442
60443/*
60444** The following parameters determine how many adjacent pages get involved
60445** in a balancing operation.  NN is the number of neighbors on either side
60446** of the page that participate in the balancing operation.  NB is the
60447** total number of pages that participate, including the target page and
60448** NN neighbors on either side.
60449**
60450** The minimum value of NN is 1 (of course).  Increasing NN above 1
60451** (to 2 or 3) gives a modest improvement in SELECT and DELETE performance
60452** in exchange for a larger degradation in INSERT and UPDATE performance.
60453** The value of NN appears to give the best results overall.
60454*/
60455#define NN 1             /* Number of neighbors on either side of pPage */
60456#define NB (NN*2+1)      /* Total pages involved in the balance */
60457
60458
60459#ifndef SQLITE_OMIT_QUICKBALANCE
60460/*
60461** This version of balance() handles the common special case where
60462** a new entry is being inserted on the extreme right-end of the
60463** tree, in other words, when the new entry will become the largest
60464** entry in the tree.
60465**
60466** Instead of trying to balance the 3 right-most leaf pages, just add
60467** a new page to the right-hand side and put the one new entry in
60468** that page.  This leaves the right side of the tree somewhat
60469** unbalanced.  But odds are that we will be inserting new entries
60470** at the end soon afterwards so the nearly empty page will quickly
60471** fill up.  On average.
60472**
60473** pPage is the leaf page which is the right-most page in the tree.
60474** pParent is its parent.  pPage must have a single overflow entry
60475** which is also the right-most entry on the page.
60476**
60477** The pSpace buffer is used to store a temporary copy of the divider
60478** cell that will be inserted into pParent. Such a cell consists of a 4
60479** byte page number followed by a variable length integer. In other
60480** words, at most 13 bytes. Hence the pSpace buffer must be at
60481** least 13 bytes in size.
60482*/
60483static int balance_quick(MemPage *pParent, MemPage *pPage, u8 *pSpace){
60484  BtShared *const pBt = pPage->pBt;    /* B-Tree Database */
60485  MemPage *pNew;                       /* Newly allocated page */
60486  int rc;                              /* Return Code */
60487  Pgno pgnoNew;                        /* Page number of pNew */
60488
60489  assert( sqlite3_mutex_held(pPage->pBt->mutex) );
60490  assert( sqlite3PagerIswriteable(pParent->pDbPage) );
60491  assert( pPage->nOverflow==1 );
60492
60493  /* This error condition is now caught prior to reaching this function */
60494  if( NEVER(pPage->nCell==0) ) return SQLITE_CORRUPT_BKPT;
60495
60496  /* Allocate a new page. This page will become the right-sibling of
60497  ** pPage. Make the parent page writable, so that the new divider cell
60498  ** may be inserted. If both these operations are successful, proceed.
60499  */
60500  rc = allocateBtreePage(pBt, &pNew, &pgnoNew, 0, 0);
60501
60502  if( rc==SQLITE_OK ){
60503
60504    u8 *pOut = &pSpace[4];
60505    u8 *pCell = pPage->apOvfl[0];
60506    u16 szCell = pPage->xCellSize(pPage, pCell);
60507    u8 *pStop;
60508
60509    assert( sqlite3PagerIswriteable(pNew->pDbPage) );
60510    assert( pPage->aData[0]==(PTF_INTKEY|PTF_LEAFDATA|PTF_LEAF) );
60511    zeroPage(pNew, PTF_INTKEY|PTF_LEAFDATA|PTF_LEAF);
60512    rc = rebuildPage(pNew, 1, &pCell, &szCell);
60513    if( NEVER(rc) ) return rc;
60514    pNew->nFree = pBt->usableSize - pNew->cellOffset - 2 - szCell;
60515
60516    /* If this is an auto-vacuum database, update the pointer map
60517    ** with entries for the new page, and any pointer from the
60518    ** cell on the page to an overflow page. If either of these
60519    ** operations fails, the return code is set, but the contents
60520    ** of the parent page are still manipulated by thh code below.
60521    ** That is Ok, at this point the parent page is guaranteed to
60522    ** be marked as dirty. Returning an error code will cause a
60523    ** rollback, undoing any changes made to the parent page.
60524    */
60525    if( ISAUTOVACUUM ){
60526      ptrmapPut(pBt, pgnoNew, PTRMAP_BTREE, pParent->pgno, &rc);
60527      if( szCell>pNew->minLocal ){
60528        ptrmapPutOvflPtr(pNew, pCell, &rc);
60529      }
60530    }
60531
60532    /* Create a divider cell to insert into pParent. The divider cell
60533    ** consists of a 4-byte page number (the page number of pPage) and
60534    ** a variable length key value (which must be the same value as the
60535    ** largest key on pPage).
60536    **
60537    ** To find the largest key value on pPage, first find the right-most
60538    ** cell on pPage. The first two fields of this cell are the
60539    ** record-length (a variable length integer at most 32-bits in size)
60540    ** and the key value (a variable length integer, may have any value).
60541    ** The first of the while(...) loops below skips over the record-length
60542    ** field. The second while(...) loop copies the key value from the
60543    ** cell on pPage into the pSpace buffer.
60544    */
60545    pCell = findCell(pPage, pPage->nCell-1);
60546    pStop = &pCell[9];
60547    while( (*(pCell++)&0x80) && pCell<pStop );
60548    pStop = &pCell[9];
60549    while( ((*(pOut++) = *(pCell++))&0x80) && pCell<pStop );
60550
60551    /* Insert the new divider cell into pParent. */
60552    insertCell(pParent, pParent->nCell, pSpace, (int)(pOut-pSpace),
60553               0, pPage->pgno, &rc);
60554
60555    /* Set the right-child pointer of pParent to point to the new page. */
60556    put4byte(&pParent->aData[pParent->hdrOffset+8], pgnoNew);
60557
60558    /* Release the reference to the new page. */
60559    releasePage(pNew);
60560  }
60561
60562  return rc;
60563}
60564#endif /* SQLITE_OMIT_QUICKBALANCE */
60565
60566#if 0
60567/*
60568** This function does not contribute anything to the operation of SQLite.
60569** it is sometimes activated temporarily while debugging code responsible
60570** for setting pointer-map entries.
60571*/
60572static int ptrmapCheckPages(MemPage **apPage, int nPage){
60573  int i, j;
60574  for(i=0; i<nPage; i++){
60575    Pgno n;
60576    u8 e;
60577    MemPage *pPage = apPage[i];
60578    BtShared *pBt = pPage->pBt;
60579    assert( pPage->isInit );
60580
60581    for(j=0; j<pPage->nCell; j++){
60582      CellInfo info;
60583      u8 *z;
60584
60585      z = findCell(pPage, j);
60586      pPage->xParseCell(pPage, z, &info);
60587      if( info.iOverflow ){
60588        Pgno ovfl = get4byte(&z[info.iOverflow]);
60589        ptrmapGet(pBt, ovfl, &e, &n);
60590        assert( n==pPage->pgno && e==PTRMAP_OVERFLOW1 );
60591      }
60592      if( !pPage->leaf ){
60593        Pgno child = get4byte(z);
60594        ptrmapGet(pBt, child, &e, &n);
60595        assert( n==pPage->pgno && e==PTRMAP_BTREE );
60596      }
60597    }
60598    if( !pPage->leaf ){
60599      Pgno child = get4byte(&pPage->aData[pPage->hdrOffset+8]);
60600      ptrmapGet(pBt, child, &e, &n);
60601      assert( n==pPage->pgno && e==PTRMAP_BTREE );
60602    }
60603  }
60604  return 1;
60605}
60606#endif
60607
60608/*
60609** This function is used to copy the contents of the b-tree node stored
60610** on page pFrom to page pTo. If page pFrom was not a leaf page, then
60611** the pointer-map entries for each child page are updated so that the
60612** parent page stored in the pointer map is page pTo. If pFrom contained
60613** any cells with overflow page pointers, then the corresponding pointer
60614** map entries are also updated so that the parent page is page pTo.
60615**
60616** If pFrom is currently carrying any overflow cells (entries in the
60617** MemPage.apOvfl[] array), they are not copied to pTo.
60618**
60619** Before returning, page pTo is reinitialized using btreeInitPage().
60620**
60621** The performance of this function is not critical. It is only used by
60622** the balance_shallower() and balance_deeper() procedures, neither of
60623** which are called often under normal circumstances.
60624*/
60625static void copyNodeContent(MemPage *pFrom, MemPage *pTo, int *pRC){
60626  if( (*pRC)==SQLITE_OK ){
60627    BtShared * const pBt = pFrom->pBt;
60628    u8 * const aFrom = pFrom->aData;
60629    u8 * const aTo = pTo->aData;
60630    int const iFromHdr = pFrom->hdrOffset;
60631    int const iToHdr = ((pTo->pgno==1) ? 100 : 0);
60632    int rc;
60633    int iData;
60634
60635
60636    assert( pFrom->isInit );
60637    assert( pFrom->nFree>=iToHdr );
60638    assert( get2byte(&aFrom[iFromHdr+5]) <= (int)pBt->usableSize );
60639
60640    /* Copy the b-tree node content from page pFrom to page pTo. */
60641    iData = get2byte(&aFrom[iFromHdr+5]);
60642    memcpy(&aTo[iData], &aFrom[iData], pBt->usableSize-iData);
60643    memcpy(&aTo[iToHdr], &aFrom[iFromHdr], pFrom->cellOffset + 2*pFrom->nCell);
60644
60645    /* Reinitialize page pTo so that the contents of the MemPage structure
60646    ** match the new data. The initialization of pTo can actually fail under
60647    ** fairly obscure circumstances, even though it is a copy of initialized
60648    ** page pFrom.
60649    */
60650    pTo->isInit = 0;
60651    rc = btreeInitPage(pTo);
60652    if( rc!=SQLITE_OK ){
60653      *pRC = rc;
60654      return;
60655    }
60656
60657    /* If this is an auto-vacuum database, update the pointer-map entries
60658    ** for any b-tree or overflow pages that pTo now contains the pointers to.
60659    */
60660    if( ISAUTOVACUUM ){
60661      *pRC = setChildPtrmaps(pTo);
60662    }
60663  }
60664}
60665
60666/*
60667** This routine redistributes cells on the iParentIdx'th child of pParent
60668** (hereafter "the page") and up to 2 siblings so that all pages have about the
60669** same amount of free space. Usually a single sibling on either side of the
60670** page are used in the balancing, though both siblings might come from one
60671** side if the page is the first or last child of its parent. If the page
60672** has fewer than 2 siblings (something which can only happen if the page
60673** is a root page or a child of a root page) then all available siblings
60674** participate in the balancing.
60675**
60676** The number of siblings of the page might be increased or decreased by
60677** one or two in an effort to keep pages nearly full but not over full.
60678**
60679** Note that when this routine is called, some of the cells on the page
60680** might not actually be stored in MemPage.aData[]. This can happen
60681** if the page is overfull. This routine ensures that all cells allocated
60682** to the page and its siblings fit into MemPage.aData[] before returning.
60683**
60684** In the course of balancing the page and its siblings, cells may be
60685** inserted into or removed from the parent page (pParent). Doing so
60686** may cause the parent page to become overfull or underfull. If this
60687** happens, it is the responsibility of the caller to invoke the correct
60688** balancing routine to fix this problem (see the balance() routine).
60689**
60690** If this routine fails for any reason, it might leave the database
60691** in a corrupted state. So if this routine fails, the database should
60692** be rolled back.
60693**
60694** The third argument to this function, aOvflSpace, is a pointer to a
60695** buffer big enough to hold one page. If while inserting cells into the parent
60696** page (pParent) the parent page becomes overfull, this buffer is
60697** used to store the parent's overflow cells. Because this function inserts
60698** a maximum of four divider cells into the parent page, and the maximum
60699** size of a cell stored within an internal node is always less than 1/4
60700** of the page-size, the aOvflSpace[] buffer is guaranteed to be large
60701** enough for all overflow cells.
60702**
60703** If aOvflSpace is set to a null pointer, this function returns
60704** SQLITE_NOMEM.
60705*/
60706#if defined(_MSC_VER) && _MSC_VER >= 1700 && defined(_M_ARM)
60707#pragma optimize("", off)
60708#endif
60709static int balance_nonroot(
60710  MemPage *pParent,               /* Parent page of siblings being balanced */
60711  int iParentIdx,                 /* Index of "the page" in pParent */
60712  u8 *aOvflSpace,                 /* page-size bytes of space for parent ovfl */
60713  int isRoot,                     /* True if pParent is a root-page */
60714  int bBulk                       /* True if this call is part of a bulk load */
60715){
60716  BtShared *pBt;               /* The whole database */
60717  int nMaxCells = 0;           /* Allocated size of apCell, szCell, aFrom. */
60718  int nNew = 0;                /* Number of pages in apNew[] */
60719  int nOld;                    /* Number of pages in apOld[] */
60720  int i, j, k;                 /* Loop counters */
60721  int nxDiv;                   /* Next divider slot in pParent->aCell[] */
60722  int rc = SQLITE_OK;          /* The return code */
60723  u16 leafCorrection;          /* 4 if pPage is a leaf.  0 if not */
60724  int leafData;                /* True if pPage is a leaf of a LEAFDATA tree */
60725  int usableSpace;             /* Bytes in pPage beyond the header */
60726  int pageFlags;               /* Value of pPage->aData[0] */
60727  int iSpace1 = 0;             /* First unused byte of aSpace1[] */
60728  int iOvflSpace = 0;          /* First unused byte of aOvflSpace[] */
60729  int szScratch;               /* Size of scratch memory requested */
60730  MemPage *apOld[NB];          /* pPage and up to two siblings */
60731  MemPage *apNew[NB+2];        /* pPage and up to NB siblings after balancing */
60732  u8 *pRight;                  /* Location in parent of right-sibling pointer */
60733  u8 *apDiv[NB-1];             /* Divider cells in pParent */
60734  int cntNew[NB+2];            /* Index in b.paCell[] of cell after i-th page */
60735  int cntOld[NB+2];            /* Old index in b.apCell[] */
60736  int szNew[NB+2];             /* Combined size of cells placed on i-th page */
60737  u8 *aSpace1;                 /* Space for copies of dividers cells */
60738  Pgno pgno;                   /* Temp var to store a page number in */
60739  u8 abDone[NB+2];             /* True after i'th new page is populated */
60740  Pgno aPgno[NB+2];            /* Page numbers of new pages before shuffling */
60741  Pgno aPgOrder[NB+2];         /* Copy of aPgno[] used for sorting pages */
60742  u16 aPgFlags[NB+2];          /* flags field of new pages before shuffling */
60743  CellArray b;                  /* Parsed information on cells being balanced */
60744
60745  memset(abDone, 0, sizeof(abDone));
60746  b.nCell = 0;
60747  b.apCell = 0;
60748  pBt = pParent->pBt;
60749  assert( sqlite3_mutex_held(pBt->mutex) );
60750  assert( sqlite3PagerIswriteable(pParent->pDbPage) );
60751
60752#if 0
60753  TRACE(("BALANCE: begin page %d child of %d\n", pPage->pgno, pParent->pgno));
60754#endif
60755
60756  /* At this point pParent may have at most one overflow cell. And if
60757  ** this overflow cell is present, it must be the cell with
60758  ** index iParentIdx. This scenario comes about when this function
60759  ** is called (indirectly) from sqlite3BtreeDelete().
60760  */
60761  assert( pParent->nOverflow==0 || pParent->nOverflow==1 );
60762  assert( pParent->nOverflow==0 || pParent->aiOvfl[0]==iParentIdx );
60763
60764  if( !aOvflSpace ){
60765    return SQLITE_NOMEM;
60766  }
60767
60768  /* Find the sibling pages to balance. Also locate the cells in pParent
60769  ** that divide the siblings. An attempt is made to find NN siblings on
60770  ** either side of pPage. More siblings are taken from one side, however,
60771  ** if there are fewer than NN siblings on the other side. If pParent
60772  ** has NB or fewer children then all children of pParent are taken.
60773  **
60774  ** This loop also drops the divider cells from the parent page. This
60775  ** way, the remainder of the function does not have to deal with any
60776  ** overflow cells in the parent page, since if any existed they will
60777  ** have already been removed.
60778  */
60779  i = pParent->nOverflow + pParent->nCell;
60780  if( i<2 ){
60781    nxDiv = 0;
60782  }else{
60783    assert( bBulk==0 || bBulk==1 );
60784    if( iParentIdx==0 ){
60785      nxDiv = 0;
60786    }else if( iParentIdx==i ){
60787      nxDiv = i-2+bBulk;
60788    }else{
60789      nxDiv = iParentIdx-1;
60790    }
60791    i = 2-bBulk;
60792  }
60793  nOld = i+1;
60794  if( (i+nxDiv-pParent->nOverflow)==pParent->nCell ){
60795    pRight = &pParent->aData[pParent->hdrOffset+8];
60796  }else{
60797    pRight = findCell(pParent, i+nxDiv-pParent->nOverflow);
60798  }
60799  pgno = get4byte(pRight);
60800  while( 1 ){
60801    rc = getAndInitPage(pBt, pgno, &apOld[i], 0, 0);
60802    if( rc ){
60803      memset(apOld, 0, (i+1)*sizeof(MemPage*));
60804      goto balance_cleanup;
60805    }
60806    nMaxCells += 1+apOld[i]->nCell+apOld[i]->nOverflow;
60807    if( (i--)==0 ) break;
60808
60809    if( i+nxDiv==pParent->aiOvfl[0] && pParent->nOverflow ){
60810      apDiv[i] = pParent->apOvfl[0];
60811      pgno = get4byte(apDiv[i]);
60812      szNew[i] = pParent->xCellSize(pParent, apDiv[i]);
60813      pParent->nOverflow = 0;
60814    }else{
60815      apDiv[i] = findCell(pParent, i+nxDiv-pParent->nOverflow);
60816      pgno = get4byte(apDiv[i]);
60817      szNew[i] = pParent->xCellSize(pParent, apDiv[i]);
60818
60819      /* Drop the cell from the parent page. apDiv[i] still points to
60820      ** the cell within the parent, even though it has been dropped.
60821      ** This is safe because dropping a cell only overwrites the first
60822      ** four bytes of it, and this function does not need the first
60823      ** four bytes of the divider cell. So the pointer is safe to use
60824      ** later on.
60825      **
60826      ** But not if we are in secure-delete mode. In secure-delete mode,
60827      ** the dropCell() routine will overwrite the entire cell with zeroes.
60828      ** In this case, temporarily copy the cell into the aOvflSpace[]
60829      ** buffer. It will be copied out again as soon as the aSpace[] buffer
60830      ** is allocated.  */
60831      if( pBt->btsFlags & BTS_SECURE_DELETE ){
60832        int iOff;
60833
60834        iOff = SQLITE_PTR_TO_INT(apDiv[i]) - SQLITE_PTR_TO_INT(pParent->aData);
60835        if( (iOff+szNew[i])>(int)pBt->usableSize ){
60836          rc = SQLITE_CORRUPT_BKPT;
60837          memset(apOld, 0, (i+1)*sizeof(MemPage*));
60838          goto balance_cleanup;
60839        }else{
60840          memcpy(&aOvflSpace[iOff], apDiv[i], szNew[i]);
60841          apDiv[i] = &aOvflSpace[apDiv[i]-pParent->aData];
60842        }
60843      }
60844      dropCell(pParent, i+nxDiv-pParent->nOverflow, szNew[i], &rc);
60845    }
60846  }
60847
60848  /* Make nMaxCells a multiple of 4 in order to preserve 8-byte
60849  ** alignment */
60850  nMaxCells = (nMaxCells + 3)&~3;
60851
60852  /*
60853  ** Allocate space for memory structures
60854  */
60855  szScratch =
60856       nMaxCells*sizeof(u8*)                       /* b.apCell */
60857     + nMaxCells*sizeof(u16)                       /* b.szCell */
60858     + pBt->pageSize;                              /* aSpace1 */
60859
60860  /* EVIDENCE-OF: R-28375-38319 SQLite will never request a scratch buffer
60861  ** that is more than 6 times the database page size. */
60862  assert( szScratch<=6*(int)pBt->pageSize );
60863  b.apCell = sqlite3ScratchMalloc( szScratch );
60864  if( b.apCell==0 ){
60865    rc = SQLITE_NOMEM;
60866    goto balance_cleanup;
60867  }
60868  b.szCell = (u16*)&b.apCell[nMaxCells];
60869  aSpace1 = (u8*)&b.szCell[nMaxCells];
60870  assert( EIGHT_BYTE_ALIGNMENT(aSpace1) );
60871
60872  /*
60873  ** Load pointers to all cells on sibling pages and the divider cells
60874  ** into the local b.apCell[] array.  Make copies of the divider cells
60875  ** into space obtained from aSpace1[]. The divider cells have already
60876  ** been removed from pParent.
60877  **
60878  ** If the siblings are on leaf pages, then the child pointers of the
60879  ** divider cells are stripped from the cells before they are copied
60880  ** into aSpace1[].  In this way, all cells in b.apCell[] are without
60881  ** child pointers.  If siblings are not leaves, then all cell in
60882  ** b.apCell[] include child pointers.  Either way, all cells in b.apCell[]
60883  ** are alike.
60884  **
60885  ** leafCorrection:  4 if pPage is a leaf.  0 if pPage is not a leaf.
60886  **       leafData:  1 if pPage holds key+data and pParent holds only keys.
60887  */
60888  b.pRef = apOld[0];
60889  leafCorrection = b.pRef->leaf*4;
60890  leafData = b.pRef->intKeyLeaf;
60891  for(i=0; i<nOld; i++){
60892    MemPage *pOld = apOld[i];
60893    int limit = pOld->nCell;
60894    u8 *aData = pOld->aData;
60895    u16 maskPage = pOld->maskPage;
60896    u8 *piCell = aData + pOld->cellOffset;
60897    u8 *piEnd;
60898
60899    /* Verify that all sibling pages are of the same "type" (table-leaf,
60900    ** table-interior, index-leaf, or index-interior).
60901    */
60902    if( pOld->aData[0]!=apOld[0]->aData[0] ){
60903      rc = SQLITE_CORRUPT_BKPT;
60904      goto balance_cleanup;
60905    }
60906
60907    /* Load b.apCell[] with pointers to all cells in pOld.  If pOld
60908    ** constains overflow cells, include them in the b.apCell[] array
60909    ** in the correct spot.
60910    **
60911    ** Note that when there are multiple overflow cells, it is always the
60912    ** case that they are sequential and adjacent.  This invariant arises
60913    ** because multiple overflows can only occurs when inserting divider
60914    ** cells into a parent on a prior balance, and divider cells are always
60915    ** adjacent and are inserted in order.  There is an assert() tagged
60916    ** with "NOTE 1" in the overflow cell insertion loop to prove this
60917    ** invariant.
60918    **
60919    ** This must be done in advance.  Once the balance starts, the cell
60920    ** offset section of the btree page will be overwritten and we will no
60921    ** long be able to find the cells if a pointer to each cell is not saved
60922    ** first.
60923    */
60924    memset(&b.szCell[b.nCell], 0, sizeof(b.szCell[0])*limit);
60925    if( pOld->nOverflow>0 ){
60926      memset(&b.szCell[b.nCell+limit], 0, sizeof(b.szCell[0])*pOld->nOverflow);
60927      limit = pOld->aiOvfl[0];
60928      for(j=0; j<limit; j++){
60929        b.apCell[b.nCell] = aData + (maskPage & get2byteAligned(piCell));
60930        piCell += 2;
60931        b.nCell++;
60932      }
60933      for(k=0; k<pOld->nOverflow; k++){
60934        assert( k==0 || pOld->aiOvfl[k-1]+1==pOld->aiOvfl[k] );/* NOTE 1 */
60935        b.apCell[b.nCell] = pOld->apOvfl[k];
60936        b.nCell++;
60937      }
60938    }
60939    piEnd = aData + pOld->cellOffset + 2*pOld->nCell;
60940    while( piCell<piEnd ){
60941      assert( b.nCell<nMaxCells );
60942      b.apCell[b.nCell] = aData + (maskPage & get2byteAligned(piCell));
60943      piCell += 2;
60944      b.nCell++;
60945    }
60946
60947    cntOld[i] = b.nCell;
60948    if( i<nOld-1 && !leafData){
60949      u16 sz = (u16)szNew[i];
60950      u8 *pTemp;
60951      assert( b.nCell<nMaxCells );
60952      b.szCell[b.nCell] = sz;
60953      pTemp = &aSpace1[iSpace1];
60954      iSpace1 += sz;
60955      assert( sz<=pBt->maxLocal+23 );
60956      assert( iSpace1 <= (int)pBt->pageSize );
60957      memcpy(pTemp, apDiv[i], sz);
60958      b.apCell[b.nCell] = pTemp+leafCorrection;
60959      assert( leafCorrection==0 || leafCorrection==4 );
60960      b.szCell[b.nCell] = b.szCell[b.nCell] - leafCorrection;
60961      if( !pOld->leaf ){
60962        assert( leafCorrection==0 );
60963        assert( pOld->hdrOffset==0 );
60964        /* The right pointer of the child page pOld becomes the left
60965        ** pointer of the divider cell */
60966        memcpy(b.apCell[b.nCell], &pOld->aData[8], 4);
60967      }else{
60968        assert( leafCorrection==4 );
60969        while( b.szCell[b.nCell]<4 ){
60970          /* Do not allow any cells smaller than 4 bytes. If a smaller cell
60971          ** does exist, pad it with 0x00 bytes. */
60972          assert( b.szCell[b.nCell]==3 || CORRUPT_DB );
60973          assert( b.apCell[b.nCell]==&aSpace1[iSpace1-3] || CORRUPT_DB );
60974          aSpace1[iSpace1++] = 0x00;
60975          b.szCell[b.nCell]++;
60976        }
60977      }
60978      b.nCell++;
60979    }
60980  }
60981
60982  /*
60983  ** Figure out the number of pages needed to hold all b.nCell cells.
60984  ** Store this number in "k".  Also compute szNew[] which is the total
60985  ** size of all cells on the i-th page and cntNew[] which is the index
60986  ** in b.apCell[] of the cell that divides page i from page i+1.
60987  ** cntNew[k] should equal b.nCell.
60988  **
60989  ** Values computed by this block:
60990  **
60991  **           k: The total number of sibling pages
60992  **    szNew[i]: Spaced used on the i-th sibling page.
60993  **   cntNew[i]: Index in b.apCell[] and b.szCell[] for the first cell to
60994  **              the right of the i-th sibling page.
60995  ** usableSpace: Number of bytes of space available on each sibling.
60996  **
60997  */
60998  usableSpace = pBt->usableSize - 12 + leafCorrection;
60999  for(i=0; i<nOld; i++){
61000    MemPage *p = apOld[i];
61001    szNew[i] = usableSpace - p->nFree;
61002    if( szNew[i]<0 ){ rc = SQLITE_CORRUPT_BKPT; goto balance_cleanup; }
61003    for(j=0; j<p->nOverflow; j++){
61004      szNew[i] += 2 + p->xCellSize(p, p->apOvfl[j]);
61005    }
61006    cntNew[i] = cntOld[i];
61007  }
61008  k = nOld;
61009  for(i=0; i<k; i++){
61010    int sz;
61011    while( szNew[i]>usableSpace ){
61012      if( i+1>=k ){
61013        k = i+2;
61014        if( k>NB+2 ){ rc = SQLITE_CORRUPT_BKPT; goto balance_cleanup; }
61015        szNew[k-1] = 0;
61016        cntNew[k-1] = b.nCell;
61017      }
61018      sz = 2 + cachedCellSize(&b, cntNew[i]-1);
61019      szNew[i] -= sz;
61020      if( !leafData ){
61021        if( cntNew[i]<b.nCell ){
61022          sz = 2 + cachedCellSize(&b, cntNew[i]);
61023        }else{
61024          sz = 0;
61025        }
61026      }
61027      szNew[i+1] += sz;
61028      cntNew[i]--;
61029    }
61030    while( cntNew[i]<b.nCell ){
61031      sz = 2 + cachedCellSize(&b, cntNew[i]);
61032      if( szNew[i]+sz>usableSpace ) break;
61033      szNew[i] += sz;
61034      cntNew[i]++;
61035      if( !leafData ){
61036        if( cntNew[i]<b.nCell ){
61037          sz = 2 + cachedCellSize(&b, cntNew[i]);
61038        }else{
61039          sz = 0;
61040        }
61041      }
61042      szNew[i+1] -= sz;
61043    }
61044    if( cntNew[i]>=b.nCell ){
61045      k = i+1;
61046    }else if( cntNew[i] <= (i>0 ? cntNew[i-1] : 0) ){
61047      rc = SQLITE_CORRUPT_BKPT;
61048      goto balance_cleanup;
61049    }
61050  }
61051
61052  /*
61053  ** The packing computed by the previous block is biased toward the siblings
61054  ** on the left side (siblings with smaller keys). The left siblings are
61055  ** always nearly full, while the right-most sibling might be nearly empty.
61056  ** The next block of code attempts to adjust the packing of siblings to
61057  ** get a better balance.
61058  **
61059  ** This adjustment is more than an optimization.  The packing above might
61060  ** be so out of balance as to be illegal.  For example, the right-most
61061  ** sibling might be completely empty.  This adjustment is not optional.
61062  */
61063  for(i=k-1; i>0; i--){
61064    int szRight = szNew[i];  /* Size of sibling on the right */
61065    int szLeft = szNew[i-1]; /* Size of sibling on the left */
61066    int r;              /* Index of right-most cell in left sibling */
61067    int d;              /* Index of first cell to the left of right sibling */
61068
61069    r = cntNew[i-1] - 1;
61070    d = r + 1 - leafData;
61071    (void)cachedCellSize(&b, d);
61072    do{
61073      assert( d<nMaxCells );
61074      assert( r<nMaxCells );
61075      (void)cachedCellSize(&b, r);
61076      if( szRight!=0
61077       && (bBulk || szRight+b.szCell[d]+2 > szLeft-(b.szCell[r]+2)) ){
61078        break;
61079      }
61080      szRight += b.szCell[d] + 2;
61081      szLeft -= b.szCell[r] + 2;
61082      cntNew[i-1] = r;
61083      r--;
61084      d--;
61085    }while( r>=0 );
61086    szNew[i] = szRight;
61087    szNew[i-1] = szLeft;
61088    if( cntNew[i-1] <= (i>1 ? cntNew[i-2] : 0) ){
61089      rc = SQLITE_CORRUPT_BKPT;
61090      goto balance_cleanup;
61091    }
61092  }
61093
61094  /* Sanity check:  For a non-corrupt database file one of the follwing
61095  ** must be true:
61096  **    (1) We found one or more cells (cntNew[0])>0), or
61097  **    (2) pPage is a virtual root page.  A virtual root page is when
61098  **        the real root page is page 1 and we are the only child of
61099  **        that page.
61100  */
61101  assert( cntNew[0]>0 || (pParent->pgno==1 && pParent->nCell==0) || CORRUPT_DB);
61102  TRACE(("BALANCE: old: %d(nc=%d) %d(nc=%d) %d(nc=%d)\n",
61103    apOld[0]->pgno, apOld[0]->nCell,
61104    nOld>=2 ? apOld[1]->pgno : 0, nOld>=2 ? apOld[1]->nCell : 0,
61105    nOld>=3 ? apOld[2]->pgno : 0, nOld>=3 ? apOld[2]->nCell : 0
61106  ));
61107
61108  /*
61109  ** Allocate k new pages.  Reuse old pages where possible.
61110  */
61111  pageFlags = apOld[0]->aData[0];
61112  for(i=0; i<k; i++){
61113    MemPage *pNew;
61114    if( i<nOld ){
61115      pNew = apNew[i] = apOld[i];
61116      apOld[i] = 0;
61117      rc = sqlite3PagerWrite(pNew->pDbPage);
61118      nNew++;
61119      if( rc ) goto balance_cleanup;
61120    }else{
61121      assert( i>0 );
61122      rc = allocateBtreePage(pBt, &pNew, &pgno, (bBulk ? 1 : pgno), 0);
61123      if( rc ) goto balance_cleanup;
61124      zeroPage(pNew, pageFlags);
61125      apNew[i] = pNew;
61126      nNew++;
61127      cntOld[i] = b.nCell;
61128
61129      /* Set the pointer-map entry for the new sibling page. */
61130      if( ISAUTOVACUUM ){
61131        ptrmapPut(pBt, pNew->pgno, PTRMAP_BTREE, pParent->pgno, &rc);
61132        if( rc!=SQLITE_OK ){
61133          goto balance_cleanup;
61134        }
61135      }
61136    }
61137  }
61138
61139  /*
61140  ** Reassign page numbers so that the new pages are in ascending order.
61141  ** This helps to keep entries in the disk file in order so that a scan
61142  ** of the table is closer to a linear scan through the file. That in turn
61143  ** helps the operating system to deliver pages from the disk more rapidly.
61144  **
61145  ** An O(n^2) insertion sort algorithm is used, but since n is never more
61146  ** than (NB+2) (a small constant), that should not be a problem.
61147  **
61148  ** When NB==3, this one optimization makes the database about 25% faster
61149  ** for large insertions and deletions.
61150  */
61151  for(i=0; i<nNew; i++){
61152    aPgOrder[i] = aPgno[i] = apNew[i]->pgno;
61153    aPgFlags[i] = apNew[i]->pDbPage->flags;
61154    for(j=0; j<i; j++){
61155      if( aPgno[j]==aPgno[i] ){
61156        /* This branch is taken if the set of sibling pages somehow contains
61157        ** duplicate entries. This can happen if the database is corrupt.
61158        ** It would be simpler to detect this as part of the loop below, but
61159        ** we do the detection here in order to avoid populating the pager
61160        ** cache with two separate objects associated with the same
61161        ** page number.  */
61162        assert( CORRUPT_DB );
61163        rc = SQLITE_CORRUPT_BKPT;
61164        goto balance_cleanup;
61165      }
61166    }
61167  }
61168  for(i=0; i<nNew; i++){
61169    int iBest = 0;                /* aPgno[] index of page number to use */
61170    for(j=1; j<nNew; j++){
61171      if( aPgOrder[j]<aPgOrder[iBest] ) iBest = j;
61172    }
61173    pgno = aPgOrder[iBest];
61174    aPgOrder[iBest] = 0xffffffff;
61175    if( iBest!=i ){
61176      if( iBest>i ){
61177        sqlite3PagerRekey(apNew[iBest]->pDbPage, pBt->nPage+iBest+1, 0);
61178      }
61179      sqlite3PagerRekey(apNew[i]->pDbPage, pgno, aPgFlags[iBest]);
61180      apNew[i]->pgno = pgno;
61181    }
61182  }
61183
61184  TRACE(("BALANCE: new: %d(%d nc=%d) %d(%d nc=%d) %d(%d nc=%d) "
61185         "%d(%d nc=%d) %d(%d nc=%d)\n",
61186    apNew[0]->pgno, szNew[0], cntNew[0],
61187    nNew>=2 ? apNew[1]->pgno : 0, nNew>=2 ? szNew[1] : 0,
61188    nNew>=2 ? cntNew[1] - cntNew[0] - !leafData : 0,
61189    nNew>=3 ? apNew[2]->pgno : 0, nNew>=3 ? szNew[2] : 0,
61190    nNew>=3 ? cntNew[2] - cntNew[1] - !leafData : 0,
61191    nNew>=4 ? apNew[3]->pgno : 0, nNew>=4 ? szNew[3] : 0,
61192    nNew>=4 ? cntNew[3] - cntNew[2] - !leafData : 0,
61193    nNew>=5 ? apNew[4]->pgno : 0, nNew>=5 ? szNew[4] : 0,
61194    nNew>=5 ? cntNew[4] - cntNew[3] - !leafData : 0
61195  ));
61196
61197  assert( sqlite3PagerIswriteable(pParent->pDbPage) );
61198  put4byte(pRight, apNew[nNew-1]->pgno);
61199
61200  /* If the sibling pages are not leaves, ensure that the right-child pointer
61201  ** of the right-most new sibling page is set to the value that was
61202  ** originally in the same field of the right-most old sibling page. */
61203  if( (pageFlags & PTF_LEAF)==0 && nOld!=nNew ){
61204    MemPage *pOld = (nNew>nOld ? apNew : apOld)[nOld-1];
61205    memcpy(&apNew[nNew-1]->aData[8], &pOld->aData[8], 4);
61206  }
61207
61208  /* Make any required updates to pointer map entries associated with
61209  ** cells stored on sibling pages following the balance operation. Pointer
61210  ** map entries associated with divider cells are set by the insertCell()
61211  ** routine. The associated pointer map entries are:
61212  **
61213  **   a) if the cell contains a reference to an overflow chain, the
61214  **      entry associated with the first page in the overflow chain, and
61215  **
61216  **   b) if the sibling pages are not leaves, the child page associated
61217  **      with the cell.
61218  **
61219  ** If the sibling pages are not leaves, then the pointer map entry
61220  ** associated with the right-child of each sibling may also need to be
61221  ** updated. This happens below, after the sibling pages have been
61222  ** populated, not here.
61223  */
61224  if( ISAUTOVACUUM ){
61225    MemPage *pNew = apNew[0];
61226    u8 *aOld = pNew->aData;
61227    int cntOldNext = pNew->nCell + pNew->nOverflow;
61228    int usableSize = pBt->usableSize;
61229    int iNew = 0;
61230    int iOld = 0;
61231
61232    for(i=0; i<b.nCell; i++){
61233      u8 *pCell = b.apCell[i];
61234      if( i==cntOldNext ){
61235        MemPage *pOld = (++iOld)<nNew ? apNew[iOld] : apOld[iOld];
61236        cntOldNext += pOld->nCell + pOld->nOverflow + !leafData;
61237        aOld = pOld->aData;
61238      }
61239      if( i==cntNew[iNew] ){
61240        pNew = apNew[++iNew];
61241        if( !leafData ) continue;
61242      }
61243
61244      /* Cell pCell is destined for new sibling page pNew. Originally, it
61245      ** was either part of sibling page iOld (possibly an overflow cell),
61246      ** or else the divider cell to the left of sibling page iOld. So,
61247      ** if sibling page iOld had the same page number as pNew, and if
61248      ** pCell really was a part of sibling page iOld (not a divider or
61249      ** overflow cell), we can skip updating the pointer map entries.  */
61250      if( iOld>=nNew
61251       || pNew->pgno!=aPgno[iOld]
61252       || pCell<aOld
61253       || pCell>=&aOld[usableSize]
61254      ){
61255        if( !leafCorrection ){
61256          ptrmapPut(pBt, get4byte(pCell), PTRMAP_BTREE, pNew->pgno, &rc);
61257        }
61258        if( cachedCellSize(&b,i)>pNew->minLocal ){
61259          ptrmapPutOvflPtr(pNew, pCell, &rc);
61260        }
61261        if( rc ) goto balance_cleanup;
61262      }
61263    }
61264  }
61265
61266  /* Insert new divider cells into pParent. */
61267  for(i=0; i<nNew-1; i++){
61268    u8 *pCell;
61269    u8 *pTemp;
61270    int sz;
61271    MemPage *pNew = apNew[i];
61272    j = cntNew[i];
61273
61274    assert( j<nMaxCells );
61275    assert( b.apCell[j]!=0 );
61276    pCell = b.apCell[j];
61277    sz = b.szCell[j] + leafCorrection;
61278    pTemp = &aOvflSpace[iOvflSpace];
61279    if( !pNew->leaf ){
61280      memcpy(&pNew->aData[8], pCell, 4);
61281    }else if( leafData ){
61282      /* If the tree is a leaf-data tree, and the siblings are leaves,
61283      ** then there is no divider cell in b.apCell[]. Instead, the divider
61284      ** cell consists of the integer key for the right-most cell of
61285      ** the sibling-page assembled above only.
61286      */
61287      CellInfo info;
61288      j--;
61289      pNew->xParseCell(pNew, b.apCell[j], &info);
61290      pCell = pTemp;
61291      sz = 4 + putVarint(&pCell[4], info.nKey);
61292      pTemp = 0;
61293    }else{
61294      pCell -= 4;
61295      /* Obscure case for non-leaf-data trees: If the cell at pCell was
61296      ** previously stored on a leaf node, and its reported size was 4
61297      ** bytes, then it may actually be smaller than this
61298      ** (see btreeParseCellPtr(), 4 bytes is the minimum size of
61299      ** any cell). But it is important to pass the correct size to
61300      ** insertCell(), so reparse the cell now.
61301      **
61302      ** Note that this can never happen in an SQLite data file, as all
61303      ** cells are at least 4 bytes. It only happens in b-trees used
61304      ** to evaluate "IN (SELECT ...)" and similar clauses.
61305      */
61306      if( b.szCell[j]==4 ){
61307        assert(leafCorrection==4);
61308        sz = pParent->xCellSize(pParent, pCell);
61309      }
61310    }
61311    iOvflSpace += sz;
61312    assert( sz<=pBt->maxLocal+23 );
61313    assert( iOvflSpace <= (int)pBt->pageSize );
61314    insertCell(pParent, nxDiv+i, pCell, sz, pTemp, pNew->pgno, &rc);
61315    if( rc!=SQLITE_OK ) goto balance_cleanup;
61316    assert( sqlite3PagerIswriteable(pParent->pDbPage) );
61317  }
61318
61319  /* Now update the actual sibling pages. The order in which they are updated
61320  ** is important, as this code needs to avoid disrupting any page from which
61321  ** cells may still to be read. In practice, this means:
61322  **
61323  **  (1) If cells are moving left (from apNew[iPg] to apNew[iPg-1])
61324  **      then it is not safe to update page apNew[iPg] until after
61325  **      the left-hand sibling apNew[iPg-1] has been updated.
61326  **
61327  **  (2) If cells are moving right (from apNew[iPg] to apNew[iPg+1])
61328  **      then it is not safe to update page apNew[iPg] until after
61329  **      the right-hand sibling apNew[iPg+1] has been updated.
61330  **
61331  ** If neither of the above apply, the page is safe to update.
61332  **
61333  ** The iPg value in the following loop starts at nNew-1 goes down
61334  ** to 0, then back up to nNew-1 again, thus making two passes over
61335  ** the pages.  On the initial downward pass, only condition (1) above
61336  ** needs to be tested because (2) will always be true from the previous
61337  ** step.  On the upward pass, both conditions are always true, so the
61338  ** upwards pass simply processes pages that were missed on the downward
61339  ** pass.
61340  */
61341  for(i=1-nNew; i<nNew; i++){
61342    int iPg = i<0 ? -i : i;
61343    assert( iPg>=0 && iPg<nNew );
61344    if( abDone[iPg] ) continue;         /* Skip pages already processed */
61345    if( i>=0                            /* On the upwards pass, or... */
61346     || cntOld[iPg-1]>=cntNew[iPg-1]    /* Condition (1) is true */
61347    ){
61348      int iNew;
61349      int iOld;
61350      int nNewCell;
61351
61352      /* Verify condition (1):  If cells are moving left, update iPg
61353      ** only after iPg-1 has already been updated. */
61354      assert( iPg==0 || cntOld[iPg-1]>=cntNew[iPg-1] || abDone[iPg-1] );
61355
61356      /* Verify condition (2):  If cells are moving right, update iPg
61357      ** only after iPg+1 has already been updated. */
61358      assert( cntNew[iPg]>=cntOld[iPg] || abDone[iPg+1] );
61359
61360      if( iPg==0 ){
61361        iNew = iOld = 0;
61362        nNewCell = cntNew[0];
61363      }else{
61364        iOld = iPg<nOld ? (cntOld[iPg-1] + !leafData) : b.nCell;
61365        iNew = cntNew[iPg-1] + !leafData;
61366        nNewCell = cntNew[iPg] - iNew;
61367      }
61368
61369      rc = editPage(apNew[iPg], iOld, iNew, nNewCell, &b);
61370      if( rc ) goto balance_cleanup;
61371      abDone[iPg]++;
61372      apNew[iPg]->nFree = usableSpace-szNew[iPg];
61373      assert( apNew[iPg]->nOverflow==0 );
61374      assert( apNew[iPg]->nCell==nNewCell );
61375    }
61376  }
61377
61378  /* All pages have been processed exactly once */
61379  assert( memcmp(abDone, "\01\01\01\01\01", nNew)==0 );
61380
61381  assert( nOld>0 );
61382  assert( nNew>0 );
61383
61384  if( isRoot && pParent->nCell==0 && pParent->hdrOffset<=apNew[0]->nFree ){
61385    /* The root page of the b-tree now contains no cells. The only sibling
61386    ** page is the right-child of the parent. Copy the contents of the
61387    ** child page into the parent, decreasing the overall height of the
61388    ** b-tree structure by one. This is described as the "balance-shallower"
61389    ** sub-algorithm in some documentation.
61390    **
61391    ** If this is an auto-vacuum database, the call to copyNodeContent()
61392    ** sets all pointer-map entries corresponding to database image pages
61393    ** for which the pointer is stored within the content being copied.
61394    **
61395    ** It is critical that the child page be defragmented before being
61396    ** copied into the parent, because if the parent is page 1 then it will
61397    ** by smaller than the child due to the database header, and so all the
61398    ** free space needs to be up front.
61399    */
61400    assert( nNew==1 );
61401    rc = defragmentPage(apNew[0]);
61402    testcase( rc!=SQLITE_OK );
61403    assert( apNew[0]->nFree ==
61404        (get2byte(&apNew[0]->aData[5])-apNew[0]->cellOffset-apNew[0]->nCell*2)
61405      || rc!=SQLITE_OK
61406    );
61407    copyNodeContent(apNew[0], pParent, &rc);
61408    freePage(apNew[0], &rc);
61409  }else if( ISAUTOVACUUM && !leafCorrection ){
61410    /* Fix the pointer map entries associated with the right-child of each
61411    ** sibling page. All other pointer map entries have already been taken
61412    ** care of.  */
61413    for(i=0; i<nNew; i++){
61414      u32 key = get4byte(&apNew[i]->aData[8]);
61415      ptrmapPut(pBt, key, PTRMAP_BTREE, apNew[i]->pgno, &rc);
61416    }
61417  }
61418
61419  assert( pParent->isInit );
61420  TRACE(("BALANCE: finished: old=%d new=%d cells=%d\n",
61421          nOld, nNew, b.nCell));
61422
61423  /* Free any old pages that were not reused as new pages.
61424  */
61425  for(i=nNew; i<nOld; i++){
61426    freePage(apOld[i], &rc);
61427  }
61428
61429#if 0
61430  if( ISAUTOVACUUM && rc==SQLITE_OK && apNew[0]->isInit ){
61431    /* The ptrmapCheckPages() contains assert() statements that verify that
61432    ** all pointer map pages are set correctly. This is helpful while
61433    ** debugging. This is usually disabled because a corrupt database may
61434    ** cause an assert() statement to fail.  */
61435    ptrmapCheckPages(apNew, nNew);
61436    ptrmapCheckPages(&pParent, 1);
61437  }
61438#endif
61439
61440  /*
61441  ** Cleanup before returning.
61442  */
61443balance_cleanup:
61444  sqlite3ScratchFree(b.apCell);
61445  for(i=0; i<nOld; i++){
61446    releasePage(apOld[i]);
61447  }
61448  for(i=0; i<nNew; i++){
61449    releasePage(apNew[i]);
61450  }
61451
61452  return rc;
61453}
61454#if defined(_MSC_VER) && _MSC_VER >= 1700 && defined(_M_ARM)
61455#pragma optimize("", on)
61456#endif
61457
61458
61459/*
61460** This function is called when the root page of a b-tree structure is
61461** overfull (has one or more overflow pages).
61462**
61463** A new child page is allocated and the contents of the current root
61464** page, including overflow cells, are copied into the child. The root
61465** page is then overwritten to make it an empty page with the right-child
61466** pointer pointing to the new page.
61467**
61468** Before returning, all pointer-map entries corresponding to pages
61469** that the new child-page now contains pointers to are updated. The
61470** entry corresponding to the new right-child pointer of the root
61471** page is also updated.
61472**
61473** If successful, *ppChild is set to contain a reference to the child
61474** page and SQLITE_OK is returned. In this case the caller is required
61475** to call releasePage() on *ppChild exactly once. If an error occurs,
61476** an error code is returned and *ppChild is set to 0.
61477*/
61478static int balance_deeper(MemPage *pRoot, MemPage **ppChild){
61479  int rc;                        /* Return value from subprocedures */
61480  MemPage *pChild = 0;           /* Pointer to a new child page */
61481  Pgno pgnoChild = 0;            /* Page number of the new child page */
61482  BtShared *pBt = pRoot->pBt;    /* The BTree */
61483
61484  assert( pRoot->nOverflow>0 );
61485  assert( sqlite3_mutex_held(pBt->mutex) );
61486
61487  /* Make pRoot, the root page of the b-tree, writable. Allocate a new
61488  ** page that will become the new right-child of pPage. Copy the contents
61489  ** of the node stored on pRoot into the new child page.
61490  */
61491  rc = sqlite3PagerWrite(pRoot->pDbPage);
61492  if( rc==SQLITE_OK ){
61493    rc = allocateBtreePage(pBt,&pChild,&pgnoChild,pRoot->pgno,0);
61494    copyNodeContent(pRoot, pChild, &rc);
61495    if( ISAUTOVACUUM ){
61496      ptrmapPut(pBt, pgnoChild, PTRMAP_BTREE, pRoot->pgno, &rc);
61497    }
61498  }
61499  if( rc ){
61500    *ppChild = 0;
61501    releasePage(pChild);
61502    return rc;
61503  }
61504  assert( sqlite3PagerIswriteable(pChild->pDbPage) );
61505  assert( sqlite3PagerIswriteable(pRoot->pDbPage) );
61506  assert( pChild->nCell==pRoot->nCell );
61507
61508  TRACE(("BALANCE: copy root %d into %d\n", pRoot->pgno, pChild->pgno));
61509
61510  /* Copy the overflow cells from pRoot to pChild */
61511  memcpy(pChild->aiOvfl, pRoot->aiOvfl,
61512         pRoot->nOverflow*sizeof(pRoot->aiOvfl[0]));
61513  memcpy(pChild->apOvfl, pRoot->apOvfl,
61514         pRoot->nOverflow*sizeof(pRoot->apOvfl[0]));
61515  pChild->nOverflow = pRoot->nOverflow;
61516
61517  /* Zero the contents of pRoot. Then install pChild as the right-child. */
61518  zeroPage(pRoot, pChild->aData[0] & ~PTF_LEAF);
61519  put4byte(&pRoot->aData[pRoot->hdrOffset+8], pgnoChild);
61520
61521  *ppChild = pChild;
61522  return SQLITE_OK;
61523}
61524
61525/*
61526** The page that pCur currently points to has just been modified in
61527** some way. This function figures out if this modification means the
61528** tree needs to be balanced, and if so calls the appropriate balancing
61529** routine. Balancing routines are:
61530**
61531**   balance_quick()
61532**   balance_deeper()
61533**   balance_nonroot()
61534*/
61535static int balance(BtCursor *pCur){
61536  int rc = SQLITE_OK;
61537  const int nMin = pCur->pBt->usableSize * 2 / 3;
61538  u8 aBalanceQuickSpace[13];
61539  u8 *pFree = 0;
61540
61541  TESTONLY( int balance_quick_called = 0 );
61542  TESTONLY( int balance_deeper_called = 0 );
61543
61544  do {
61545    int iPage = pCur->iPage;
61546    MemPage *pPage = pCur->apPage[iPage];
61547
61548    if( iPage==0 ){
61549      if( pPage->nOverflow ){
61550        /* The root page of the b-tree is overfull. In this case call the
61551        ** balance_deeper() function to create a new child for the root-page
61552        ** and copy the current contents of the root-page to it. The
61553        ** next iteration of the do-loop will balance the child page.
61554        */
61555        assert( (balance_deeper_called++)==0 );
61556        rc = balance_deeper(pPage, &pCur->apPage[1]);
61557        if( rc==SQLITE_OK ){
61558          pCur->iPage = 1;
61559          pCur->aiIdx[0] = 0;
61560          pCur->aiIdx[1] = 0;
61561          assert( pCur->apPage[1]->nOverflow );
61562        }
61563      }else{
61564        break;
61565      }
61566    }else if( pPage->nOverflow==0 && pPage->nFree<=nMin ){
61567      break;
61568    }else{
61569      MemPage * const pParent = pCur->apPage[iPage-1];
61570      int const iIdx = pCur->aiIdx[iPage-1];
61571
61572      rc = sqlite3PagerWrite(pParent->pDbPage);
61573      if( rc==SQLITE_OK ){
61574#ifndef SQLITE_OMIT_QUICKBALANCE
61575        if( pPage->intKeyLeaf
61576         && pPage->nOverflow==1
61577         && pPage->aiOvfl[0]==pPage->nCell
61578         && pParent->pgno!=1
61579         && pParent->nCell==iIdx
61580        ){
61581          /* Call balance_quick() to create a new sibling of pPage on which
61582          ** to store the overflow cell. balance_quick() inserts a new cell
61583          ** into pParent, which may cause pParent overflow. If this
61584          ** happens, the next iteration of the do-loop will balance pParent
61585          ** use either balance_nonroot() or balance_deeper(). Until this
61586          ** happens, the overflow cell is stored in the aBalanceQuickSpace[]
61587          ** buffer.
61588          **
61589          ** The purpose of the following assert() is to check that only a
61590          ** single call to balance_quick() is made for each call to this
61591          ** function. If this were not verified, a subtle bug involving reuse
61592          ** of the aBalanceQuickSpace[] might sneak in.
61593          */
61594          assert( (balance_quick_called++)==0 );
61595          rc = balance_quick(pParent, pPage, aBalanceQuickSpace);
61596        }else
61597#endif
61598        {
61599          /* In this case, call balance_nonroot() to redistribute cells
61600          ** between pPage and up to 2 of its sibling pages. This involves
61601          ** modifying the contents of pParent, which may cause pParent to
61602          ** become overfull or underfull. The next iteration of the do-loop
61603          ** will balance the parent page to correct this.
61604          **
61605          ** If the parent page becomes overfull, the overflow cell or cells
61606          ** are stored in the pSpace buffer allocated immediately below.
61607          ** A subsequent iteration of the do-loop will deal with this by
61608          ** calling balance_nonroot() (balance_deeper() may be called first,
61609          ** but it doesn't deal with overflow cells - just moves them to a
61610          ** different page). Once this subsequent call to balance_nonroot()
61611          ** has completed, it is safe to release the pSpace buffer used by
61612          ** the previous call, as the overflow cell data will have been
61613          ** copied either into the body of a database page or into the new
61614          ** pSpace buffer passed to the latter call to balance_nonroot().
61615          */
61616          u8 *pSpace = sqlite3PageMalloc(pCur->pBt->pageSize);
61617          rc = balance_nonroot(pParent, iIdx, pSpace, iPage==1,
61618                               pCur->hints&BTREE_BULKLOAD);
61619          if( pFree ){
61620            /* If pFree is not NULL, it points to the pSpace buffer used
61621            ** by a previous call to balance_nonroot(). Its contents are
61622            ** now stored either on real database pages or within the
61623            ** new pSpace buffer, so it may be safely freed here. */
61624            sqlite3PageFree(pFree);
61625          }
61626
61627          /* The pSpace buffer will be freed after the next call to
61628          ** balance_nonroot(), or just before this function returns, whichever
61629          ** comes first. */
61630          pFree = pSpace;
61631        }
61632      }
61633
61634      pPage->nOverflow = 0;
61635
61636      /* The next iteration of the do-loop balances the parent page. */
61637      releasePage(pPage);
61638      pCur->iPage--;
61639      assert( pCur->iPage>=0 );
61640    }
61641  }while( rc==SQLITE_OK );
61642
61643  if( pFree ){
61644    sqlite3PageFree(pFree);
61645  }
61646  return rc;
61647}
61648
61649
61650/*
61651** Insert a new record into the BTree.  The key is given by (pKey,nKey)
61652** and the data is given by (pData,nData).  The cursor is used only to
61653** define what table the record should be inserted into.  The cursor
61654** is left pointing at a random location.
61655**
61656** For an INTKEY table, only the nKey value of the key is used.  pKey is
61657** ignored.  For a ZERODATA table, the pData and nData are both ignored.
61658**
61659** If the seekResult parameter is non-zero, then a successful call to
61660** MovetoUnpacked() to seek cursor pCur to (pKey, nKey) has already
61661** been performed. seekResult is the search result returned (a negative
61662** number if pCur points at an entry that is smaller than (pKey, nKey), or
61663** a positive value if pCur points at an entry that is larger than
61664** (pKey, nKey)).
61665**
61666** If the seekResult parameter is non-zero, then the caller guarantees that
61667** cursor pCur is pointing at the existing copy of a row that is to be
61668** overwritten.  If the seekResult parameter is 0, then cursor pCur may
61669** point to any entry or to no entry at all and so this function has to seek
61670** the cursor before the new key can be inserted.
61671*/
61672SQLITE_PRIVATE int sqlite3BtreeInsert(
61673  BtCursor *pCur,                /* Insert data into the table of this cursor */
61674  const void *pKey, i64 nKey,    /* The key of the new record */
61675  const void *pData, int nData,  /* The data of the new record */
61676  int nZero,                     /* Number of extra 0 bytes to append to data */
61677  int appendBias,                /* True if this is likely an append */
61678  int seekResult                 /* Result of prior MovetoUnpacked() call */
61679){
61680  int rc;
61681  int loc = seekResult;          /* -1: before desired location  +1: after */
61682  int szNew = 0;
61683  int idx;
61684  MemPage *pPage;
61685  Btree *p = pCur->pBtree;
61686  BtShared *pBt = p->pBt;
61687  unsigned char *oldCell;
61688  unsigned char *newCell = 0;
61689
61690  if( pCur->eState==CURSOR_FAULT ){
61691    assert( pCur->skipNext!=SQLITE_OK );
61692    return pCur->skipNext;
61693  }
61694
61695  assert( cursorHoldsMutex(pCur) );
61696  assert( (pCur->curFlags & BTCF_WriteFlag)!=0
61697              && pBt->inTransaction==TRANS_WRITE
61698              && (pBt->btsFlags & BTS_READ_ONLY)==0 );
61699  assert( hasSharedCacheTableLock(p, pCur->pgnoRoot, pCur->pKeyInfo!=0, 2) );
61700
61701  /* Assert that the caller has been consistent. If this cursor was opened
61702  ** expecting an index b-tree, then the caller should be inserting blob
61703  ** keys with no associated data. If the cursor was opened expecting an
61704  ** intkey table, the caller should be inserting integer keys with a
61705  ** blob of associated data.  */
61706  assert( (pKey==0)==(pCur->pKeyInfo==0) );
61707
61708  /* Save the positions of any other cursors open on this table.
61709  **
61710  ** In some cases, the call to btreeMoveto() below is a no-op. For
61711  ** example, when inserting data into a table with auto-generated integer
61712  ** keys, the VDBE layer invokes sqlite3BtreeLast() to figure out the
61713  ** integer key to use. It then calls this function to actually insert the
61714  ** data into the intkey B-Tree. In this case btreeMoveto() recognizes
61715  ** that the cursor is already where it needs to be and returns without
61716  ** doing any work. To avoid thwarting these optimizations, it is important
61717  ** not to clear the cursor here.
61718  */
61719  if( pCur->curFlags & BTCF_Multiple ){
61720    rc = saveAllCursors(pBt, pCur->pgnoRoot, pCur);
61721    if( rc ) return rc;
61722  }
61723
61724  if( pCur->pKeyInfo==0 ){
61725    assert( pKey==0 );
61726    /* If this is an insert into a table b-tree, invalidate any incrblob
61727    ** cursors open on the row being replaced */
61728    invalidateIncrblobCursors(p, nKey, 0);
61729
61730    /* If the cursor is currently on the last row and we are appending a
61731    ** new row onto the end, set the "loc" to avoid an unnecessary
61732    ** btreeMoveto() call */
61733    if( (pCur->curFlags&BTCF_ValidNKey)!=0 && nKey>0
61734      && pCur->info.nKey==nKey-1 ){
61735       loc = -1;
61736    }else if( loc==0 ){
61737      rc = sqlite3BtreeMovetoUnpacked(pCur, 0, nKey, appendBias, &loc);
61738      if( rc ) return rc;
61739    }
61740  }else if( loc==0 ){
61741    rc = btreeMoveto(pCur, pKey, nKey, appendBias, &loc);
61742    if( rc ) return rc;
61743  }
61744  assert( pCur->eState==CURSOR_VALID || (pCur->eState==CURSOR_INVALID && loc) );
61745
61746  pPage = pCur->apPage[pCur->iPage];
61747  assert( pPage->intKey || nKey>=0 );
61748  assert( pPage->leaf || !pPage->intKey );
61749
61750  TRACE(("INSERT: table=%d nkey=%lld ndata=%d page=%d %s\n",
61751          pCur->pgnoRoot, nKey, nData, pPage->pgno,
61752          loc==0 ? "overwrite" : "new entry"));
61753  assert( pPage->isInit );
61754  newCell = pBt->pTmpSpace;
61755  assert( newCell!=0 );
61756  rc = fillInCell(pPage, newCell, pKey, nKey, pData, nData, nZero, &szNew);
61757  if( rc ) goto end_insert;
61758  assert( szNew==pPage->xCellSize(pPage, newCell) );
61759  assert( szNew <= MX_CELL_SIZE(pBt) );
61760  idx = pCur->aiIdx[pCur->iPage];
61761  if( loc==0 ){
61762    u16 szOld;
61763    assert( idx<pPage->nCell );
61764    rc = sqlite3PagerWrite(pPage->pDbPage);
61765    if( rc ){
61766      goto end_insert;
61767    }
61768    oldCell = findCell(pPage, idx);
61769    if( !pPage->leaf ){
61770      memcpy(newCell, oldCell, 4);
61771    }
61772    rc = clearCell(pPage, oldCell, &szOld);
61773    dropCell(pPage, idx, szOld, &rc);
61774    if( rc ) goto end_insert;
61775  }else if( loc<0 && pPage->nCell>0 ){
61776    assert( pPage->leaf );
61777    idx = ++pCur->aiIdx[pCur->iPage];
61778  }else{
61779    assert( pPage->leaf );
61780  }
61781  insertCell(pPage, idx, newCell, szNew, 0, 0, &rc);
61782  assert( rc!=SQLITE_OK || pPage->nCell>0 || pPage->nOverflow>0 );
61783
61784  /* If no error has occurred and pPage has an overflow cell, call balance()
61785  ** to redistribute the cells within the tree. Since balance() may move
61786  ** the cursor, zero the BtCursor.info.nSize and BTCF_ValidNKey
61787  ** variables.
61788  **
61789  ** Previous versions of SQLite called moveToRoot() to move the cursor
61790  ** back to the root page as balance() used to invalidate the contents
61791  ** of BtCursor.apPage[] and BtCursor.aiIdx[]. Instead of doing that,
61792  ** set the cursor state to "invalid". This makes common insert operations
61793  ** slightly faster.
61794  **
61795  ** There is a subtle but important optimization here too. When inserting
61796  ** multiple records into an intkey b-tree using a single cursor (as can
61797  ** happen while processing an "INSERT INTO ... SELECT" statement), it
61798  ** is advantageous to leave the cursor pointing to the last entry in
61799  ** the b-tree if possible. If the cursor is left pointing to the last
61800  ** entry in the table, and the next row inserted has an integer key
61801  ** larger than the largest existing key, it is possible to insert the
61802  ** row without seeking the cursor. This can be a big performance boost.
61803  */
61804  pCur->info.nSize = 0;
61805  if( rc==SQLITE_OK && pPage->nOverflow ){
61806    pCur->curFlags &= ~(BTCF_ValidNKey);
61807    rc = balance(pCur);
61808
61809    /* Must make sure nOverflow is reset to zero even if the balance()
61810    ** fails. Internal data structure corruption will result otherwise.
61811    ** Also, set the cursor state to invalid. This stops saveCursorPosition()
61812    ** from trying to save the current position of the cursor.  */
61813    pCur->apPage[pCur->iPage]->nOverflow = 0;
61814    pCur->eState = CURSOR_INVALID;
61815  }
61816  assert( pCur->apPage[pCur->iPage]->nOverflow==0 );
61817
61818end_insert:
61819  return rc;
61820}
61821
61822/*
61823** Delete the entry that the cursor is pointing to.  The cursor
61824** is left pointing at an arbitrary location.
61825*/
61826SQLITE_PRIVATE int sqlite3BtreeDelete(BtCursor *pCur){
61827  Btree *p = pCur->pBtree;
61828  BtShared *pBt = p->pBt;
61829  int rc;                              /* Return code */
61830  MemPage *pPage;                      /* Page to delete cell from */
61831  unsigned char *pCell;                /* Pointer to cell to delete */
61832  int iCellIdx;                        /* Index of cell to delete */
61833  int iCellDepth;                      /* Depth of node containing pCell */
61834  u16 szCell;                          /* Size of the cell being deleted */
61835
61836  assert( cursorHoldsMutex(pCur) );
61837  assert( pBt->inTransaction==TRANS_WRITE );
61838  assert( (pBt->btsFlags & BTS_READ_ONLY)==0 );
61839  assert( pCur->curFlags & BTCF_WriteFlag );
61840  assert( hasSharedCacheTableLock(p, pCur->pgnoRoot, pCur->pKeyInfo!=0, 2) );
61841  assert( !hasReadConflicts(p, pCur->pgnoRoot) );
61842  assert( pCur->aiIdx[pCur->iPage]<pCur->apPage[pCur->iPage]->nCell );
61843  assert( pCur->eState==CURSOR_VALID );
61844
61845  iCellDepth = pCur->iPage;
61846  iCellIdx = pCur->aiIdx[iCellDepth];
61847  pPage = pCur->apPage[iCellDepth];
61848  pCell = findCell(pPage, iCellIdx);
61849
61850  /* If the page containing the entry to delete is not a leaf page, move
61851  ** the cursor to the largest entry in the tree that is smaller than
61852  ** the entry being deleted. This cell will replace the cell being deleted
61853  ** from the internal node. The 'previous' entry is used for this instead
61854  ** of the 'next' entry, as the previous entry is always a part of the
61855  ** sub-tree headed by the child page of the cell being deleted. This makes
61856  ** balancing the tree following the delete operation easier.  */
61857  if( !pPage->leaf ){
61858    int notUsed = 0;
61859    rc = sqlite3BtreePrevious(pCur, &notUsed);
61860    if( rc ) return rc;
61861  }
61862
61863  /* Save the positions of any other cursors open on this table before
61864  ** making any modifications. Make the page containing the entry to be
61865  ** deleted writable. Then free any overflow pages associated with the
61866  ** entry and finally remove the cell itself from within the page.
61867  */
61868  if( pCur->curFlags & BTCF_Multiple ){
61869    rc = saveAllCursors(pBt, pCur->pgnoRoot, pCur);
61870    if( rc ) return rc;
61871  }
61872
61873  /* If this is a delete operation to remove a row from a table b-tree,
61874  ** invalidate any incrblob cursors open on the row being deleted.  */
61875  if( pCur->pKeyInfo==0 ){
61876    invalidateIncrblobCursors(p, pCur->info.nKey, 0);
61877  }
61878
61879  rc = sqlite3PagerWrite(pPage->pDbPage);
61880  if( rc ) return rc;
61881  rc = clearCell(pPage, pCell, &szCell);
61882  dropCell(pPage, iCellIdx, szCell, &rc);
61883  if( rc ) return rc;
61884
61885  /* If the cell deleted was not located on a leaf page, then the cursor
61886  ** is currently pointing to the largest entry in the sub-tree headed
61887  ** by the child-page of the cell that was just deleted from an internal
61888  ** node. The cell from the leaf node needs to be moved to the internal
61889  ** node to replace the deleted cell.  */
61890  if( !pPage->leaf ){
61891    MemPage *pLeaf = pCur->apPage[pCur->iPage];
61892    int nCell;
61893    Pgno n = pCur->apPage[iCellDepth+1]->pgno;
61894    unsigned char *pTmp;
61895
61896    pCell = findCell(pLeaf, pLeaf->nCell-1);
61897    if( pCell<&pLeaf->aData[4] ) return SQLITE_CORRUPT_BKPT;
61898    nCell = pLeaf->xCellSize(pLeaf, pCell);
61899    assert( MX_CELL_SIZE(pBt) >= nCell );
61900    pTmp = pBt->pTmpSpace;
61901    assert( pTmp!=0 );
61902    rc = sqlite3PagerWrite(pLeaf->pDbPage);
61903    insertCell(pPage, iCellIdx, pCell-4, nCell+4, pTmp, n, &rc);
61904    dropCell(pLeaf, pLeaf->nCell-1, nCell, &rc);
61905    if( rc ) return rc;
61906  }
61907
61908  /* Balance the tree. If the entry deleted was located on a leaf page,
61909  ** then the cursor still points to that page. In this case the first
61910  ** call to balance() repairs the tree, and the if(...) condition is
61911  ** never true.
61912  **
61913  ** Otherwise, if the entry deleted was on an internal node page, then
61914  ** pCur is pointing to the leaf page from which a cell was removed to
61915  ** replace the cell deleted from the internal node. This is slightly
61916  ** tricky as the leaf node may be underfull, and the internal node may
61917  ** be either under or overfull. In this case run the balancing algorithm
61918  ** on the leaf node first. If the balance proceeds far enough up the
61919  ** tree that we can be sure that any problem in the internal node has
61920  ** been corrected, so be it. Otherwise, after balancing the leaf node,
61921  ** walk the cursor up the tree to the internal node and balance it as
61922  ** well.  */
61923  rc = balance(pCur);
61924  if( rc==SQLITE_OK && pCur->iPage>iCellDepth ){
61925    while( pCur->iPage>iCellDepth ){
61926      releasePage(pCur->apPage[pCur->iPage--]);
61927    }
61928    rc = balance(pCur);
61929  }
61930
61931  if( rc==SQLITE_OK ){
61932    moveToRoot(pCur);
61933  }
61934  return rc;
61935}
61936
61937/*
61938** Create a new BTree table.  Write into *piTable the page
61939** number for the root page of the new table.
61940**
61941** The type of type is determined by the flags parameter.  Only the
61942** following values of flags are currently in use.  Other values for
61943** flags might not work:
61944**
61945**     BTREE_INTKEY|BTREE_LEAFDATA     Used for SQL tables with rowid keys
61946**     BTREE_ZERODATA                  Used for SQL indices
61947*/
61948static int btreeCreateTable(Btree *p, int *piTable, int createTabFlags){
61949  BtShared *pBt = p->pBt;
61950  MemPage *pRoot;
61951  Pgno pgnoRoot;
61952  int rc;
61953  int ptfFlags;          /* Page-type flage for the root page of new table */
61954
61955  assert( sqlite3BtreeHoldsMutex(p) );
61956  assert( pBt->inTransaction==TRANS_WRITE );
61957  assert( (pBt->btsFlags & BTS_READ_ONLY)==0 );
61958
61959#ifdef SQLITE_OMIT_AUTOVACUUM
61960  rc = allocateBtreePage(pBt, &pRoot, &pgnoRoot, 1, 0);
61961  if( rc ){
61962    return rc;
61963  }
61964#else
61965  if( pBt->autoVacuum ){
61966    Pgno pgnoMove;      /* Move a page here to make room for the root-page */
61967    MemPage *pPageMove; /* The page to move to. */
61968
61969    /* Creating a new table may probably require moving an existing database
61970    ** to make room for the new tables root page. In case this page turns
61971    ** out to be an overflow page, delete all overflow page-map caches
61972    ** held by open cursors.
61973    */
61974    invalidateAllOverflowCache(pBt);
61975
61976    /* Read the value of meta[3] from the database to determine where the
61977    ** root page of the new table should go. meta[3] is the largest root-page
61978    ** created so far, so the new root-page is (meta[3]+1).
61979    */
61980    sqlite3BtreeGetMeta(p, BTREE_LARGEST_ROOT_PAGE, &pgnoRoot);
61981    pgnoRoot++;
61982
61983    /* The new root-page may not be allocated on a pointer-map page, or the
61984    ** PENDING_BYTE page.
61985    */
61986    while( pgnoRoot==PTRMAP_PAGENO(pBt, pgnoRoot) ||
61987        pgnoRoot==PENDING_BYTE_PAGE(pBt) ){
61988      pgnoRoot++;
61989    }
61990    assert( pgnoRoot>=3 || CORRUPT_DB );
61991    testcase( pgnoRoot<3 );
61992
61993    /* Allocate a page. The page that currently resides at pgnoRoot will
61994    ** be moved to the allocated page (unless the allocated page happens
61995    ** to reside at pgnoRoot).
61996    */
61997    rc = allocateBtreePage(pBt, &pPageMove, &pgnoMove, pgnoRoot, BTALLOC_EXACT);
61998    if( rc!=SQLITE_OK ){
61999      return rc;
62000    }
62001
62002    if( pgnoMove!=pgnoRoot ){
62003      /* pgnoRoot is the page that will be used for the root-page of
62004      ** the new table (assuming an error did not occur). But we were
62005      ** allocated pgnoMove. If required (i.e. if it was not allocated
62006      ** by extending the file), the current page at position pgnoMove
62007      ** is already journaled.
62008      */
62009      u8 eType = 0;
62010      Pgno iPtrPage = 0;
62011
62012      /* Save the positions of any open cursors. This is required in
62013      ** case they are holding a reference to an xFetch reference
62014      ** corresponding to page pgnoRoot.  */
62015      rc = saveAllCursors(pBt, 0, 0);
62016      releasePage(pPageMove);
62017      if( rc!=SQLITE_OK ){
62018        return rc;
62019      }
62020
62021      /* Move the page currently at pgnoRoot to pgnoMove. */
62022      rc = btreeGetPage(pBt, pgnoRoot, &pRoot, 0);
62023      if( rc!=SQLITE_OK ){
62024        return rc;
62025      }
62026      rc = ptrmapGet(pBt, pgnoRoot, &eType, &iPtrPage);
62027      if( eType==PTRMAP_ROOTPAGE || eType==PTRMAP_FREEPAGE ){
62028        rc = SQLITE_CORRUPT_BKPT;
62029      }
62030      if( rc!=SQLITE_OK ){
62031        releasePage(pRoot);
62032        return rc;
62033      }
62034      assert( eType!=PTRMAP_ROOTPAGE );
62035      assert( eType!=PTRMAP_FREEPAGE );
62036      rc = relocatePage(pBt, pRoot, eType, iPtrPage, pgnoMove, 0);
62037      releasePage(pRoot);
62038
62039      /* Obtain the page at pgnoRoot */
62040      if( rc!=SQLITE_OK ){
62041        return rc;
62042      }
62043      rc = btreeGetPage(pBt, pgnoRoot, &pRoot, 0);
62044      if( rc!=SQLITE_OK ){
62045        return rc;
62046      }
62047      rc = sqlite3PagerWrite(pRoot->pDbPage);
62048      if( rc!=SQLITE_OK ){
62049        releasePage(pRoot);
62050        return rc;
62051      }
62052    }else{
62053      pRoot = pPageMove;
62054    }
62055
62056    /* Update the pointer-map and meta-data with the new root-page number. */
62057    ptrmapPut(pBt, pgnoRoot, PTRMAP_ROOTPAGE, 0, &rc);
62058    if( rc ){
62059      releasePage(pRoot);
62060      return rc;
62061    }
62062
62063    /* When the new root page was allocated, page 1 was made writable in
62064    ** order either to increase the database filesize, or to decrement the
62065    ** freelist count.  Hence, the sqlite3BtreeUpdateMeta() call cannot fail.
62066    */
62067    assert( sqlite3PagerIswriteable(pBt->pPage1->pDbPage) );
62068    rc = sqlite3BtreeUpdateMeta(p, 4, pgnoRoot);
62069    if( NEVER(rc) ){
62070      releasePage(pRoot);
62071      return rc;
62072    }
62073
62074  }else{
62075    rc = allocateBtreePage(pBt, &pRoot, &pgnoRoot, 1, 0);
62076    if( rc ) return rc;
62077  }
62078#endif
62079  assert( sqlite3PagerIswriteable(pRoot->pDbPage) );
62080  if( createTabFlags & BTREE_INTKEY ){
62081    ptfFlags = PTF_INTKEY | PTF_LEAFDATA | PTF_LEAF;
62082  }else{
62083    ptfFlags = PTF_ZERODATA | PTF_LEAF;
62084  }
62085  zeroPage(pRoot, ptfFlags);
62086  sqlite3PagerUnref(pRoot->pDbPage);
62087  assert( (pBt->openFlags & BTREE_SINGLE)==0 || pgnoRoot==2 );
62088  *piTable = (int)pgnoRoot;
62089  return SQLITE_OK;
62090}
62091SQLITE_PRIVATE int sqlite3BtreeCreateTable(Btree *p, int *piTable, int flags){
62092  int rc;
62093  sqlite3BtreeEnter(p);
62094  rc = btreeCreateTable(p, piTable, flags);
62095  sqlite3BtreeLeave(p);
62096  return rc;
62097}
62098
62099/*
62100** Erase the given database page and all its children.  Return
62101** the page to the freelist.
62102*/
62103static int clearDatabasePage(
62104  BtShared *pBt,           /* The BTree that contains the table */
62105  Pgno pgno,               /* Page number to clear */
62106  int freePageFlag,        /* Deallocate page if true */
62107  int *pnChange            /* Add number of Cells freed to this counter */
62108){
62109  MemPage *pPage;
62110  int rc;
62111  unsigned char *pCell;
62112  int i;
62113  int hdr;
62114  u16 szCell;
62115
62116  assert( sqlite3_mutex_held(pBt->mutex) );
62117  if( pgno>btreePagecount(pBt) ){
62118    return SQLITE_CORRUPT_BKPT;
62119  }
62120  rc = getAndInitPage(pBt, pgno, &pPage, 0, 0);
62121  if( rc ) return rc;
62122  if( pPage->bBusy ){
62123    rc = SQLITE_CORRUPT_BKPT;
62124    goto cleardatabasepage_out;
62125  }
62126  pPage->bBusy = 1;
62127  hdr = pPage->hdrOffset;
62128  for(i=0; i<pPage->nCell; i++){
62129    pCell = findCell(pPage, i);
62130    if( !pPage->leaf ){
62131      rc = clearDatabasePage(pBt, get4byte(pCell), 1, pnChange);
62132      if( rc ) goto cleardatabasepage_out;
62133    }
62134    rc = clearCell(pPage, pCell, &szCell);
62135    if( rc ) goto cleardatabasepage_out;
62136  }
62137  if( !pPage->leaf ){
62138    rc = clearDatabasePage(pBt, get4byte(&pPage->aData[hdr+8]), 1, pnChange);
62139    if( rc ) goto cleardatabasepage_out;
62140  }else if( pnChange ){
62141    assert( pPage->intKey || CORRUPT_DB );
62142    testcase( !pPage->intKey );
62143    *pnChange += pPage->nCell;
62144  }
62145  if( freePageFlag ){
62146    freePage(pPage, &rc);
62147  }else if( (rc = sqlite3PagerWrite(pPage->pDbPage))==0 ){
62148    zeroPage(pPage, pPage->aData[hdr] | PTF_LEAF);
62149  }
62150
62151cleardatabasepage_out:
62152  pPage->bBusy = 0;
62153  releasePage(pPage);
62154  return rc;
62155}
62156
62157/*
62158** Delete all information from a single table in the database.  iTable is
62159** the page number of the root of the table.  After this routine returns,
62160** the root page is empty, but still exists.
62161**
62162** This routine will fail with SQLITE_LOCKED if there are any open
62163** read cursors on the table.  Open write cursors are moved to the
62164** root of the table.
62165**
62166** If pnChange is not NULL, then table iTable must be an intkey table. The
62167** integer value pointed to by pnChange is incremented by the number of
62168** entries in the table.
62169*/
62170SQLITE_PRIVATE int sqlite3BtreeClearTable(Btree *p, int iTable, int *pnChange){
62171  int rc;
62172  BtShared *pBt = p->pBt;
62173  sqlite3BtreeEnter(p);
62174  assert( p->inTrans==TRANS_WRITE );
62175
62176  rc = saveAllCursors(pBt, (Pgno)iTable, 0);
62177
62178  if( SQLITE_OK==rc ){
62179    /* Invalidate all incrblob cursors open on table iTable (assuming iTable
62180    ** is the root of a table b-tree - if it is not, the following call is
62181    ** a no-op).  */
62182    invalidateIncrblobCursors(p, 0, 1);
62183    rc = clearDatabasePage(pBt, (Pgno)iTable, 0, pnChange);
62184  }
62185  sqlite3BtreeLeave(p);
62186  return rc;
62187}
62188
62189/*
62190** Delete all information from the single table that pCur is open on.
62191**
62192** This routine only work for pCur on an ephemeral table.
62193*/
62194SQLITE_PRIVATE int sqlite3BtreeClearTableOfCursor(BtCursor *pCur){
62195  return sqlite3BtreeClearTable(pCur->pBtree, pCur->pgnoRoot, 0);
62196}
62197
62198/*
62199** Erase all information in a table and add the root of the table to
62200** the freelist.  Except, the root of the principle table (the one on
62201** page 1) is never added to the freelist.
62202**
62203** This routine will fail with SQLITE_LOCKED if there are any open
62204** cursors on the table.
62205**
62206** If AUTOVACUUM is enabled and the page at iTable is not the last
62207** root page in the database file, then the last root page
62208** in the database file is moved into the slot formerly occupied by
62209** iTable and that last slot formerly occupied by the last root page
62210** is added to the freelist instead of iTable.  In this say, all
62211** root pages are kept at the beginning of the database file, which
62212** is necessary for AUTOVACUUM to work right.  *piMoved is set to the
62213** page number that used to be the last root page in the file before
62214** the move.  If no page gets moved, *piMoved is set to 0.
62215** The last root page is recorded in meta[3] and the value of
62216** meta[3] is updated by this procedure.
62217*/
62218static int btreeDropTable(Btree *p, Pgno iTable, int *piMoved){
62219  int rc;
62220  MemPage *pPage = 0;
62221  BtShared *pBt = p->pBt;
62222
62223  assert( sqlite3BtreeHoldsMutex(p) );
62224  assert( p->inTrans==TRANS_WRITE );
62225
62226  /* It is illegal to drop a table if any cursors are open on the
62227  ** database. This is because in auto-vacuum mode the backend may
62228  ** need to move another root-page to fill a gap left by the deleted
62229  ** root page. If an open cursor was using this page a problem would
62230  ** occur.
62231  **
62232  ** This error is caught long before control reaches this point.
62233  */
62234  if( NEVER(pBt->pCursor) ){
62235    sqlite3ConnectionBlocked(p->db, pBt->pCursor->pBtree->db);
62236    return SQLITE_LOCKED_SHAREDCACHE;
62237  }
62238
62239  rc = btreeGetPage(pBt, (Pgno)iTable, &pPage, 0);
62240  if( rc ) return rc;
62241  rc = sqlite3BtreeClearTable(p, iTable, 0);
62242  if( rc ){
62243    releasePage(pPage);
62244    return rc;
62245  }
62246
62247  *piMoved = 0;
62248
62249  if( iTable>1 ){
62250#ifdef SQLITE_OMIT_AUTOVACUUM
62251    freePage(pPage, &rc);
62252    releasePage(pPage);
62253#else
62254    if( pBt->autoVacuum ){
62255      Pgno maxRootPgno;
62256      sqlite3BtreeGetMeta(p, BTREE_LARGEST_ROOT_PAGE, &maxRootPgno);
62257
62258      if( iTable==maxRootPgno ){
62259        /* If the table being dropped is the table with the largest root-page
62260        ** number in the database, put the root page on the free list.
62261        */
62262        freePage(pPage, &rc);
62263        releasePage(pPage);
62264        if( rc!=SQLITE_OK ){
62265          return rc;
62266        }
62267      }else{
62268        /* The table being dropped does not have the largest root-page
62269        ** number in the database. So move the page that does into the
62270        ** gap left by the deleted root-page.
62271        */
62272        MemPage *pMove;
62273        releasePage(pPage);
62274        rc = btreeGetPage(pBt, maxRootPgno, &pMove, 0);
62275        if( rc!=SQLITE_OK ){
62276          return rc;
62277        }
62278        rc = relocatePage(pBt, pMove, PTRMAP_ROOTPAGE, 0, iTable, 0);
62279        releasePage(pMove);
62280        if( rc!=SQLITE_OK ){
62281          return rc;
62282        }
62283        pMove = 0;
62284        rc = btreeGetPage(pBt, maxRootPgno, &pMove, 0);
62285        freePage(pMove, &rc);
62286        releasePage(pMove);
62287        if( rc!=SQLITE_OK ){
62288          return rc;
62289        }
62290        *piMoved = maxRootPgno;
62291      }
62292
62293      /* Set the new 'max-root-page' value in the database header. This
62294      ** is the old value less one, less one more if that happens to
62295      ** be a root-page number, less one again if that is the
62296      ** PENDING_BYTE_PAGE.
62297      */
62298      maxRootPgno--;
62299      while( maxRootPgno==PENDING_BYTE_PAGE(pBt)
62300             || PTRMAP_ISPAGE(pBt, maxRootPgno) ){
62301        maxRootPgno--;
62302      }
62303      assert( maxRootPgno!=PENDING_BYTE_PAGE(pBt) );
62304
62305      rc = sqlite3BtreeUpdateMeta(p, 4, maxRootPgno);
62306    }else{
62307      freePage(pPage, &rc);
62308      releasePage(pPage);
62309    }
62310#endif
62311  }else{
62312    /* If sqlite3BtreeDropTable was called on page 1.
62313    ** This really never should happen except in a corrupt
62314    ** database.
62315    */
62316    zeroPage(pPage, PTF_INTKEY|PTF_LEAF );
62317    releasePage(pPage);
62318  }
62319  return rc;
62320}
62321SQLITE_PRIVATE int sqlite3BtreeDropTable(Btree *p, int iTable, int *piMoved){
62322  int rc;
62323  sqlite3BtreeEnter(p);
62324  rc = btreeDropTable(p, iTable, piMoved);
62325  sqlite3BtreeLeave(p);
62326  return rc;
62327}
62328
62329
62330/*
62331** This function may only be called if the b-tree connection already
62332** has a read or write transaction open on the database.
62333**
62334** Read the meta-information out of a database file.  Meta[0]
62335** is the number of free pages currently in the database.  Meta[1]
62336** through meta[15] are available for use by higher layers.  Meta[0]
62337** is read-only, the others are read/write.
62338**
62339** The schema layer numbers meta values differently.  At the schema
62340** layer (and the SetCookie and ReadCookie opcodes) the number of
62341** free pages is not visible.  So Cookie[0] is the same as Meta[1].
62342**
62343** This routine treats Meta[BTREE_DATA_VERSION] as a special case.  Instead
62344** of reading the value out of the header, it instead loads the "DataVersion"
62345** from the pager.  The BTREE_DATA_VERSION value is not actually stored in the
62346** database file.  It is a number computed by the pager.  But its access
62347** pattern is the same as header meta values, and so it is convenient to
62348** read it from this routine.
62349*/
62350SQLITE_PRIVATE void sqlite3BtreeGetMeta(Btree *p, int idx, u32 *pMeta){
62351  BtShared *pBt = p->pBt;
62352
62353  sqlite3BtreeEnter(p);
62354  assert( p->inTrans>TRANS_NONE );
62355  assert( SQLITE_OK==querySharedCacheTableLock(p, MASTER_ROOT, READ_LOCK) );
62356  assert( pBt->pPage1 );
62357  assert( idx>=0 && idx<=15 );
62358
62359  if( idx==BTREE_DATA_VERSION ){
62360    *pMeta = sqlite3PagerDataVersion(pBt->pPager) + p->iDataVersion;
62361  }else{
62362    *pMeta = get4byte(&pBt->pPage1->aData[36 + idx*4]);
62363  }
62364
62365  /* If auto-vacuum is disabled in this build and this is an auto-vacuum
62366  ** database, mark the database as read-only.  */
62367#ifdef SQLITE_OMIT_AUTOVACUUM
62368  if( idx==BTREE_LARGEST_ROOT_PAGE && *pMeta>0 ){
62369    pBt->btsFlags |= BTS_READ_ONLY;
62370  }
62371#endif
62372
62373  sqlite3BtreeLeave(p);
62374}
62375
62376/*
62377** Write meta-information back into the database.  Meta[0] is
62378** read-only and may not be written.
62379*/
62380SQLITE_PRIVATE int sqlite3BtreeUpdateMeta(Btree *p, int idx, u32 iMeta){
62381  BtShared *pBt = p->pBt;
62382  unsigned char *pP1;
62383  int rc;
62384  assert( idx>=1 && idx<=15 );
62385  sqlite3BtreeEnter(p);
62386  assert( p->inTrans==TRANS_WRITE );
62387  assert( pBt->pPage1!=0 );
62388  pP1 = pBt->pPage1->aData;
62389  rc = sqlite3PagerWrite(pBt->pPage1->pDbPage);
62390  if( rc==SQLITE_OK ){
62391    put4byte(&pP1[36 + idx*4], iMeta);
62392#ifndef SQLITE_OMIT_AUTOVACUUM
62393    if( idx==BTREE_INCR_VACUUM ){
62394      assert( pBt->autoVacuum || iMeta==0 );
62395      assert( iMeta==0 || iMeta==1 );
62396      pBt->incrVacuum = (u8)iMeta;
62397    }
62398#endif
62399  }
62400  sqlite3BtreeLeave(p);
62401  return rc;
62402}
62403
62404#ifndef SQLITE_OMIT_BTREECOUNT
62405/*
62406** The first argument, pCur, is a cursor opened on some b-tree. Count the
62407** number of entries in the b-tree and write the result to *pnEntry.
62408**
62409** SQLITE_OK is returned if the operation is successfully executed.
62410** Otherwise, if an error is encountered (i.e. an IO error or database
62411** corruption) an SQLite error code is returned.
62412*/
62413SQLITE_PRIVATE int sqlite3BtreeCount(BtCursor *pCur, i64 *pnEntry){
62414  i64 nEntry = 0;                      /* Value to return in *pnEntry */
62415  int rc;                              /* Return code */
62416
62417  if( pCur->pgnoRoot==0 ){
62418    *pnEntry = 0;
62419    return SQLITE_OK;
62420  }
62421  rc = moveToRoot(pCur);
62422
62423  /* Unless an error occurs, the following loop runs one iteration for each
62424  ** page in the B-Tree structure (not including overflow pages).
62425  */
62426  while( rc==SQLITE_OK ){
62427    int iIdx;                          /* Index of child node in parent */
62428    MemPage *pPage;                    /* Current page of the b-tree */
62429
62430    /* If this is a leaf page or the tree is not an int-key tree, then
62431    ** this page contains countable entries. Increment the entry counter
62432    ** accordingly.
62433    */
62434    pPage = pCur->apPage[pCur->iPage];
62435    if( pPage->leaf || !pPage->intKey ){
62436      nEntry += pPage->nCell;
62437    }
62438
62439    /* pPage is a leaf node. This loop navigates the cursor so that it
62440    ** points to the first interior cell that it points to the parent of
62441    ** the next page in the tree that has not yet been visited. The
62442    ** pCur->aiIdx[pCur->iPage] value is set to the index of the parent cell
62443    ** of the page, or to the number of cells in the page if the next page
62444    ** to visit is the right-child of its parent.
62445    **
62446    ** If all pages in the tree have been visited, return SQLITE_OK to the
62447    ** caller.
62448    */
62449    if( pPage->leaf ){
62450      do {
62451        if( pCur->iPage==0 ){
62452          /* All pages of the b-tree have been visited. Return successfully. */
62453          *pnEntry = nEntry;
62454          return moveToRoot(pCur);
62455        }
62456        moveToParent(pCur);
62457      }while ( pCur->aiIdx[pCur->iPage]>=pCur->apPage[pCur->iPage]->nCell );
62458
62459      pCur->aiIdx[pCur->iPage]++;
62460      pPage = pCur->apPage[pCur->iPage];
62461    }
62462
62463    /* Descend to the child node of the cell that the cursor currently
62464    ** points at. This is the right-child if (iIdx==pPage->nCell).
62465    */
62466    iIdx = pCur->aiIdx[pCur->iPage];
62467    if( iIdx==pPage->nCell ){
62468      rc = moveToChild(pCur, get4byte(&pPage->aData[pPage->hdrOffset+8]));
62469    }else{
62470      rc = moveToChild(pCur, get4byte(findCell(pPage, iIdx)));
62471    }
62472  }
62473
62474  /* An error has occurred. Return an error code. */
62475  return rc;
62476}
62477#endif
62478
62479/*
62480** Return the pager associated with a BTree.  This routine is used for
62481** testing and debugging only.
62482*/
62483SQLITE_PRIVATE Pager *sqlite3BtreePager(Btree *p){
62484  return p->pBt->pPager;
62485}
62486
62487#ifndef SQLITE_OMIT_INTEGRITY_CHECK
62488/*
62489** Append a message to the error message string.
62490*/
62491static void checkAppendMsg(
62492  IntegrityCk *pCheck,
62493  const char *zFormat,
62494  ...
62495){
62496  va_list ap;
62497  char zBuf[200];
62498  if( !pCheck->mxErr ) return;
62499  pCheck->mxErr--;
62500  pCheck->nErr++;
62501  va_start(ap, zFormat);
62502  if( pCheck->errMsg.nChar ){
62503    sqlite3StrAccumAppend(&pCheck->errMsg, "\n", 1);
62504  }
62505  if( pCheck->zPfx ){
62506    sqlite3_snprintf(sizeof(zBuf), zBuf, pCheck->zPfx, pCheck->v1, pCheck->v2);
62507    sqlite3StrAccumAppendAll(&pCheck->errMsg, zBuf);
62508  }
62509  sqlite3VXPrintf(&pCheck->errMsg, 1, zFormat, ap);
62510  va_end(ap);
62511  if( pCheck->errMsg.accError==STRACCUM_NOMEM ){
62512    pCheck->mallocFailed = 1;
62513  }
62514}
62515#endif /* SQLITE_OMIT_INTEGRITY_CHECK */
62516
62517#ifndef SQLITE_OMIT_INTEGRITY_CHECK
62518
62519/*
62520** Return non-zero if the bit in the IntegrityCk.aPgRef[] array that
62521** corresponds to page iPg is already set.
62522*/
62523static int getPageReferenced(IntegrityCk *pCheck, Pgno iPg){
62524  assert( iPg<=pCheck->nPage && sizeof(pCheck->aPgRef[0])==1 );
62525  return (pCheck->aPgRef[iPg/8] & (1 << (iPg & 0x07)));
62526}
62527
62528/*
62529** Set the bit in the IntegrityCk.aPgRef[] array that corresponds to page iPg.
62530*/
62531static void setPageReferenced(IntegrityCk *pCheck, Pgno iPg){
62532  assert( iPg<=pCheck->nPage && sizeof(pCheck->aPgRef[0])==1 );
62533  pCheck->aPgRef[iPg/8] |= (1 << (iPg & 0x07));
62534}
62535
62536
62537/*
62538** Add 1 to the reference count for page iPage.  If this is the second
62539** reference to the page, add an error message to pCheck->zErrMsg.
62540** Return 1 if there are 2 or more references to the page and 0 if
62541** if this is the first reference to the page.
62542**
62543** Also check that the page number is in bounds.
62544*/
62545static int checkRef(IntegrityCk *pCheck, Pgno iPage){
62546  if( iPage==0 ) return 1;
62547  if( iPage>pCheck->nPage ){
62548    checkAppendMsg(pCheck, "invalid page number %d", iPage);
62549    return 1;
62550  }
62551  if( getPageReferenced(pCheck, iPage) ){
62552    checkAppendMsg(pCheck, "2nd reference to page %d", iPage);
62553    return 1;
62554  }
62555  setPageReferenced(pCheck, iPage);
62556  return 0;
62557}
62558
62559#ifndef SQLITE_OMIT_AUTOVACUUM
62560/*
62561** Check that the entry in the pointer-map for page iChild maps to
62562** page iParent, pointer type ptrType. If not, append an error message
62563** to pCheck.
62564*/
62565static void checkPtrmap(
62566  IntegrityCk *pCheck,   /* Integrity check context */
62567  Pgno iChild,           /* Child page number */
62568  u8 eType,              /* Expected pointer map type */
62569  Pgno iParent           /* Expected pointer map parent page number */
62570){
62571  int rc;
62572  u8 ePtrmapType;
62573  Pgno iPtrmapParent;
62574
62575  rc = ptrmapGet(pCheck->pBt, iChild, &ePtrmapType, &iPtrmapParent);
62576  if( rc!=SQLITE_OK ){
62577    if( rc==SQLITE_NOMEM || rc==SQLITE_IOERR_NOMEM ) pCheck->mallocFailed = 1;
62578    checkAppendMsg(pCheck, "Failed to read ptrmap key=%d", iChild);
62579    return;
62580  }
62581
62582  if( ePtrmapType!=eType || iPtrmapParent!=iParent ){
62583    checkAppendMsg(pCheck,
62584      "Bad ptr map entry key=%d expected=(%d,%d) got=(%d,%d)",
62585      iChild, eType, iParent, ePtrmapType, iPtrmapParent);
62586  }
62587}
62588#endif
62589
62590/*
62591** Check the integrity of the freelist or of an overflow page list.
62592** Verify that the number of pages on the list is N.
62593*/
62594static void checkList(
62595  IntegrityCk *pCheck,  /* Integrity checking context */
62596  int isFreeList,       /* True for a freelist.  False for overflow page list */
62597  int iPage,            /* Page number for first page in the list */
62598  int N                 /* Expected number of pages in the list */
62599){
62600  int i;
62601  int expected = N;
62602  int iFirst = iPage;
62603  while( N-- > 0 && pCheck->mxErr ){
62604    DbPage *pOvflPage;
62605    unsigned char *pOvflData;
62606    if( iPage<1 ){
62607      checkAppendMsg(pCheck,
62608         "%d of %d pages missing from overflow list starting at %d",
62609          N+1, expected, iFirst);
62610      break;
62611    }
62612    if( checkRef(pCheck, iPage) ) break;
62613    if( sqlite3PagerGet(pCheck->pPager, (Pgno)iPage, &pOvflPage) ){
62614      checkAppendMsg(pCheck, "failed to get page %d", iPage);
62615      break;
62616    }
62617    pOvflData = (unsigned char *)sqlite3PagerGetData(pOvflPage);
62618    if( isFreeList ){
62619      int n = get4byte(&pOvflData[4]);
62620#ifndef SQLITE_OMIT_AUTOVACUUM
62621      if( pCheck->pBt->autoVacuum ){
62622        checkPtrmap(pCheck, iPage, PTRMAP_FREEPAGE, 0);
62623      }
62624#endif
62625      if( n>(int)pCheck->pBt->usableSize/4-2 ){
62626        checkAppendMsg(pCheck,
62627           "freelist leaf count too big on page %d", iPage);
62628        N--;
62629      }else{
62630        for(i=0; i<n; i++){
62631          Pgno iFreePage = get4byte(&pOvflData[8+i*4]);
62632#ifndef SQLITE_OMIT_AUTOVACUUM
62633          if( pCheck->pBt->autoVacuum ){
62634            checkPtrmap(pCheck, iFreePage, PTRMAP_FREEPAGE, 0);
62635          }
62636#endif
62637          checkRef(pCheck, iFreePage);
62638        }
62639        N -= n;
62640      }
62641    }
62642#ifndef SQLITE_OMIT_AUTOVACUUM
62643    else{
62644      /* If this database supports auto-vacuum and iPage is not the last
62645      ** page in this overflow list, check that the pointer-map entry for
62646      ** the following page matches iPage.
62647      */
62648      if( pCheck->pBt->autoVacuum && N>0 ){
62649        i = get4byte(pOvflData);
62650        checkPtrmap(pCheck, i, PTRMAP_OVERFLOW2, iPage);
62651      }
62652    }
62653#endif
62654    iPage = get4byte(pOvflData);
62655    sqlite3PagerUnref(pOvflPage);
62656  }
62657}
62658#endif /* SQLITE_OMIT_INTEGRITY_CHECK */
62659
62660/*
62661** An implementation of a min-heap.
62662**
62663** aHeap[0] is the number of elements on the heap.  aHeap[1] is the
62664** root element.  The daughter nodes of aHeap[N] are aHeap[N*2]
62665** and aHeap[N*2+1].
62666**
62667** The heap property is this:  Every node is less than or equal to both
62668** of its daughter nodes.  A consequence of the heap property is that the
62669** root node aHeap[1] is always the minimum value currently in the heap.
62670**
62671** The btreeHeapInsert() routine inserts an unsigned 32-bit number onto
62672** the heap, preserving the heap property.  The btreeHeapPull() routine
62673** removes the root element from the heap (the minimum value in the heap)
62674** and then moves other nodes around as necessary to preserve the heap
62675** property.
62676**
62677** This heap is used for cell overlap and coverage testing.  Each u32
62678** entry represents the span of a cell or freeblock on a btree page.
62679** The upper 16 bits are the index of the first byte of a range and the
62680** lower 16 bits are the index of the last byte of that range.
62681*/
62682static void btreeHeapInsert(u32 *aHeap, u32 x){
62683  u32 j, i = ++aHeap[0];
62684  aHeap[i] = x;
62685  while( (j = i/2)>0 && aHeap[j]>aHeap[i] ){
62686    x = aHeap[j];
62687    aHeap[j] = aHeap[i];
62688    aHeap[i] = x;
62689    i = j;
62690  }
62691}
62692static int btreeHeapPull(u32 *aHeap, u32 *pOut){
62693  u32 j, i, x;
62694  if( (x = aHeap[0])==0 ) return 0;
62695  *pOut = aHeap[1];
62696  aHeap[1] = aHeap[x];
62697  aHeap[x] = 0xffffffff;
62698  aHeap[0]--;
62699  i = 1;
62700  while( (j = i*2)<=aHeap[0] ){
62701    if( aHeap[j]>aHeap[j+1] ) j++;
62702    if( aHeap[i]<aHeap[j] ) break;
62703    x = aHeap[i];
62704    aHeap[i] = aHeap[j];
62705    aHeap[j] = x;
62706    i = j;
62707  }
62708  return 1;
62709}
62710
62711#ifndef SQLITE_OMIT_INTEGRITY_CHECK
62712/*
62713** Do various sanity checks on a single page of a tree.  Return
62714** the tree depth.  Root pages return 0.  Parents of root pages
62715** return 1, and so forth.
62716**
62717** These checks are done:
62718**
62719**      1.  Make sure that cells and freeblocks do not overlap
62720**          but combine to completely cover the page.
62721**      2.  Make sure integer cell keys are in order.
62722**      3.  Check the integrity of overflow pages.
62723**      4.  Recursively call checkTreePage on all children.
62724**      5.  Verify that the depth of all children is the same.
62725*/
62726static int checkTreePage(
62727  IntegrityCk *pCheck,  /* Context for the sanity check */
62728  int iPage,            /* Page number of the page to check */
62729  i64 *piMinKey,        /* Write minimum integer primary key here */
62730  i64 maxKey            /* Error if integer primary key greater than this */
62731){
62732  MemPage *pPage = 0;      /* The page being analyzed */
62733  int i;                   /* Loop counter */
62734  int rc;                  /* Result code from subroutine call */
62735  int depth = -1, d2;      /* Depth of a subtree */
62736  int pgno;                /* Page number */
62737  int nFrag;               /* Number of fragmented bytes on the page */
62738  int hdr;                 /* Offset to the page header */
62739  int cellStart;           /* Offset to the start of the cell pointer array */
62740  int nCell;               /* Number of cells */
62741  int doCoverageCheck = 1; /* True if cell coverage checking should be done */
62742  int keyCanBeEqual = 1;   /* True if IPK can be equal to maxKey
62743                           ** False if IPK must be strictly less than maxKey */
62744  u8 *data;                /* Page content */
62745  u8 *pCell;               /* Cell content */
62746  u8 *pCellIdx;            /* Next element of the cell pointer array */
62747  BtShared *pBt;           /* The BtShared object that owns pPage */
62748  u32 pc;                  /* Address of a cell */
62749  u32 usableSize;          /* Usable size of the page */
62750  u32 contentOffset;       /* Offset to the start of the cell content area */
62751  u32 *heap = 0;           /* Min-heap used for checking cell coverage */
62752  u32 x, prev = 0;         /* Next and previous entry on the min-heap */
62753  const char *saved_zPfx = pCheck->zPfx;
62754  int saved_v1 = pCheck->v1;
62755  int saved_v2 = pCheck->v2;
62756  u8 savedIsInit = 0;
62757
62758  /* Check that the page exists
62759  */
62760  pBt = pCheck->pBt;
62761  usableSize = pBt->usableSize;
62762  if( iPage==0 ) return 0;
62763  if( checkRef(pCheck, iPage) ) return 0;
62764  pCheck->zPfx = "Page %d: ";
62765  pCheck->v1 = iPage;
62766  if( (rc = btreeGetPage(pBt, (Pgno)iPage, &pPage, 0))!=0 ){
62767    checkAppendMsg(pCheck,
62768       "unable to get the page. error code=%d", rc);
62769    goto end_of_check;
62770  }
62771
62772  /* Clear MemPage.isInit to make sure the corruption detection code in
62773  ** btreeInitPage() is executed.  */
62774  savedIsInit = pPage->isInit;
62775  pPage->isInit = 0;
62776  if( (rc = btreeInitPage(pPage))!=0 ){
62777    assert( rc==SQLITE_CORRUPT );  /* The only possible error from InitPage */
62778    checkAppendMsg(pCheck,
62779                   "btreeInitPage() returns error code %d", rc);
62780    goto end_of_check;
62781  }
62782  data = pPage->aData;
62783  hdr = pPage->hdrOffset;
62784
62785  /* Set up for cell analysis */
62786  pCheck->zPfx = "On tree page %d cell %d: ";
62787  contentOffset = get2byteNotZero(&data[hdr+5]);
62788  assert( contentOffset<=usableSize );  /* Enforced by btreeInitPage() */
62789
62790  /* EVIDENCE-OF: R-37002-32774 The two-byte integer at offset 3 gives the
62791  ** number of cells on the page. */
62792  nCell = get2byte(&data[hdr+3]);
62793  assert( pPage->nCell==nCell );
62794
62795  /* EVIDENCE-OF: R-23882-45353 The cell pointer array of a b-tree page
62796  ** immediately follows the b-tree page header. */
62797  cellStart = hdr + 12 - 4*pPage->leaf;
62798  assert( pPage->aCellIdx==&data[cellStart] );
62799  pCellIdx = &data[cellStart + 2*(nCell-1)];
62800
62801  if( !pPage->leaf ){
62802    /* Analyze the right-child page of internal pages */
62803    pgno = get4byte(&data[hdr+8]);
62804#ifndef SQLITE_OMIT_AUTOVACUUM
62805    if( pBt->autoVacuum ){
62806      pCheck->zPfx = "On page %d at right child: ";
62807      checkPtrmap(pCheck, pgno, PTRMAP_BTREE, iPage);
62808    }
62809#endif
62810    depth = checkTreePage(pCheck, pgno, &maxKey, maxKey);
62811    keyCanBeEqual = 0;
62812  }else{
62813    /* For leaf pages, the coverage check will occur in the same loop
62814    ** as the other cell checks, so initialize the heap.  */
62815    heap = pCheck->heap;
62816    heap[0] = 0;
62817  }
62818
62819  /* EVIDENCE-OF: R-02776-14802 The cell pointer array consists of K 2-byte
62820  ** integer offsets to the cell contents. */
62821  for(i=nCell-1; i>=0 && pCheck->mxErr; i--){
62822    CellInfo info;
62823
62824    /* Check cell size */
62825    pCheck->v2 = i;
62826    assert( pCellIdx==&data[cellStart + i*2] );
62827    pc = get2byteAligned(pCellIdx);
62828    pCellIdx -= 2;
62829    if( pc<contentOffset || pc>usableSize-4 ){
62830      checkAppendMsg(pCheck, "Offset %d out of range %d..%d",
62831                             pc, contentOffset, usableSize-4);
62832      doCoverageCheck = 0;
62833      continue;
62834    }
62835    pCell = &data[pc];
62836    pPage->xParseCell(pPage, pCell, &info);
62837    if( pc+info.nSize>usableSize ){
62838      checkAppendMsg(pCheck, "Extends off end of page");
62839      doCoverageCheck = 0;
62840      continue;
62841    }
62842
62843    /* Check for integer primary key out of range */
62844    if( pPage->intKey ){
62845      if( keyCanBeEqual ? (info.nKey > maxKey) : (info.nKey >= maxKey) ){
62846        checkAppendMsg(pCheck, "Rowid %lld out of order", info.nKey);
62847      }
62848      maxKey = info.nKey;
62849    }
62850
62851    /* Check the content overflow list */
62852    if( info.nPayload>info.nLocal ){
62853      int nPage;       /* Number of pages on the overflow chain */
62854      Pgno pgnoOvfl;   /* First page of the overflow chain */
62855      assert( pc + info.iOverflow <= usableSize );
62856      nPage = (info.nPayload - info.nLocal + usableSize - 5)/(usableSize - 4);
62857      pgnoOvfl = get4byte(&pCell[info.iOverflow]);
62858#ifndef SQLITE_OMIT_AUTOVACUUM
62859      if( pBt->autoVacuum ){
62860        checkPtrmap(pCheck, pgnoOvfl, PTRMAP_OVERFLOW1, iPage);
62861      }
62862#endif
62863      checkList(pCheck, 0, pgnoOvfl, nPage);
62864    }
62865
62866    if( !pPage->leaf ){
62867      /* Check sanity of left child page for internal pages */
62868      pgno = get4byte(pCell);
62869#ifndef SQLITE_OMIT_AUTOVACUUM
62870      if( pBt->autoVacuum ){
62871        checkPtrmap(pCheck, pgno, PTRMAP_BTREE, iPage);
62872      }
62873#endif
62874      d2 = checkTreePage(pCheck, pgno, &maxKey, maxKey);
62875      keyCanBeEqual = 0;
62876      if( d2!=depth ){
62877        checkAppendMsg(pCheck, "Child page depth differs");
62878        depth = d2;
62879      }
62880    }else{
62881      /* Populate the coverage-checking heap for leaf pages */
62882      btreeHeapInsert(heap, (pc<<16)|(pc+info.nSize-1));
62883    }
62884  }
62885  *piMinKey = maxKey;
62886
62887  /* Check for complete coverage of the page
62888  */
62889  pCheck->zPfx = 0;
62890  if( doCoverageCheck && pCheck->mxErr>0 ){
62891    /* For leaf pages, the min-heap has already been initialized and the
62892    ** cells have already been inserted.  But for internal pages, that has
62893    ** not yet been done, so do it now */
62894    if( !pPage->leaf ){
62895      heap = pCheck->heap;
62896      heap[0] = 0;
62897      for(i=nCell-1; i>=0; i--){
62898        u32 size;
62899        pc = get2byteAligned(&data[cellStart+i*2]);
62900        size = pPage->xCellSize(pPage, &data[pc]);
62901        btreeHeapInsert(heap, (pc<<16)|(pc+size-1));
62902      }
62903    }
62904    /* Add the freeblocks to the min-heap
62905    **
62906    ** EVIDENCE-OF: R-20690-50594 The second field of the b-tree page header
62907    ** is the offset of the first freeblock, or zero if there are no
62908    ** freeblocks on the page.
62909    */
62910    i = get2byte(&data[hdr+1]);
62911    while( i>0 ){
62912      int size, j;
62913      assert( (u32)i<=usableSize-4 );     /* Enforced by btreeInitPage() */
62914      size = get2byte(&data[i+2]);
62915      assert( (u32)(i+size)<=usableSize );  /* Enforced by btreeInitPage() */
62916      btreeHeapInsert(heap, (((u32)i)<<16)|(i+size-1));
62917      /* EVIDENCE-OF: R-58208-19414 The first 2 bytes of a freeblock are a
62918      ** big-endian integer which is the offset in the b-tree page of the next
62919      ** freeblock in the chain, or zero if the freeblock is the last on the
62920      ** chain. */
62921      j = get2byte(&data[i]);
62922      /* EVIDENCE-OF: R-06866-39125 Freeblocks are always connected in order of
62923      ** increasing offset. */
62924      assert( j==0 || j>i+size );  /* Enforced by btreeInitPage() */
62925      assert( (u32)j<=usableSize-4 );   /* Enforced by btreeInitPage() */
62926      i = j;
62927    }
62928    /* Analyze the min-heap looking for overlap between cells and/or
62929    ** freeblocks, and counting the number of untracked bytes in nFrag.
62930    **
62931    ** Each min-heap entry is of the form:    (start_address<<16)|end_address.
62932    ** There is an implied first entry the covers the page header, the cell
62933    ** pointer index, and the gap between the cell pointer index and the start
62934    ** of cell content.
62935    **
62936    ** The loop below pulls entries from the min-heap in order and compares
62937    ** the start_address against the previous end_address.  If there is an
62938    ** overlap, that means bytes are used multiple times.  If there is a gap,
62939    ** that gap is added to the fragmentation count.
62940    */
62941    nFrag = 0;
62942    prev = contentOffset - 1;   /* Implied first min-heap entry */
62943    while( btreeHeapPull(heap,&x) ){
62944      if( (prev&0xffff)>=(x>>16) ){
62945        checkAppendMsg(pCheck,
62946          "Multiple uses for byte %u of page %d", x>>16, iPage);
62947        break;
62948      }else{
62949        nFrag += (x>>16) - (prev&0xffff) - 1;
62950        prev = x;
62951      }
62952    }
62953    nFrag += usableSize - (prev&0xffff) - 1;
62954    /* EVIDENCE-OF: R-43263-13491 The total number of bytes in all fragments
62955    ** is stored in the fifth field of the b-tree page header.
62956    ** EVIDENCE-OF: R-07161-27322 The one-byte integer at offset 7 gives the
62957    ** number of fragmented free bytes within the cell content area.
62958    */
62959    if( heap[0]==0 && nFrag!=data[hdr+7] ){
62960      checkAppendMsg(pCheck,
62961          "Fragmentation of %d bytes reported as %d on page %d",
62962          nFrag, data[hdr+7], iPage);
62963    }
62964  }
62965
62966end_of_check:
62967  if( !doCoverageCheck ) pPage->isInit = savedIsInit;
62968  releasePage(pPage);
62969  pCheck->zPfx = saved_zPfx;
62970  pCheck->v1 = saved_v1;
62971  pCheck->v2 = saved_v2;
62972  return depth+1;
62973}
62974#endif /* SQLITE_OMIT_INTEGRITY_CHECK */
62975
62976#ifndef SQLITE_OMIT_INTEGRITY_CHECK
62977/*
62978** This routine does a complete check of the given BTree file.  aRoot[] is
62979** an array of pages numbers were each page number is the root page of
62980** a table.  nRoot is the number of entries in aRoot.
62981**
62982** A read-only or read-write transaction must be opened before calling
62983** this function.
62984**
62985** Write the number of error seen in *pnErr.  Except for some memory
62986** allocation errors,  an error message held in memory obtained from
62987** malloc is returned if *pnErr is non-zero.  If *pnErr==0 then NULL is
62988** returned.  If a memory allocation error occurs, NULL is returned.
62989*/
62990SQLITE_PRIVATE char *sqlite3BtreeIntegrityCheck(
62991  Btree *p,     /* The btree to be checked */
62992  int *aRoot,   /* An array of root pages numbers for individual trees */
62993  int nRoot,    /* Number of entries in aRoot[] */
62994  int mxErr,    /* Stop reporting errors after this many */
62995  int *pnErr    /* Write number of errors seen to this variable */
62996){
62997  Pgno i;
62998  IntegrityCk sCheck;
62999  BtShared *pBt = p->pBt;
63000  int savedDbFlags = pBt->db->flags;
63001  char zErr[100];
63002  VVA_ONLY( int nRef );
63003
63004  sqlite3BtreeEnter(p);
63005  assert( p->inTrans>TRANS_NONE && pBt->inTransaction>TRANS_NONE );
63006  assert( (nRef = sqlite3PagerRefcount(pBt->pPager))>=0 );
63007  sCheck.pBt = pBt;
63008  sCheck.pPager = pBt->pPager;
63009  sCheck.nPage = btreePagecount(sCheck.pBt);
63010  sCheck.mxErr = mxErr;
63011  sCheck.nErr = 0;
63012  sCheck.mallocFailed = 0;
63013  sCheck.zPfx = 0;
63014  sCheck.v1 = 0;
63015  sCheck.v2 = 0;
63016  sCheck.aPgRef = 0;
63017  sCheck.heap = 0;
63018  sqlite3StrAccumInit(&sCheck.errMsg, 0, zErr, sizeof(zErr), SQLITE_MAX_LENGTH);
63019  if( sCheck.nPage==0 ){
63020    goto integrity_ck_cleanup;
63021  }
63022
63023  sCheck.aPgRef = sqlite3MallocZero((sCheck.nPage / 8)+ 1);
63024  if( !sCheck.aPgRef ){
63025    sCheck.mallocFailed = 1;
63026    goto integrity_ck_cleanup;
63027  }
63028  sCheck.heap = (u32*)sqlite3PageMalloc( pBt->pageSize );
63029  if( sCheck.heap==0 ){
63030    sCheck.mallocFailed = 1;
63031    goto integrity_ck_cleanup;
63032  }
63033
63034  i = PENDING_BYTE_PAGE(pBt);
63035  if( i<=sCheck.nPage ) setPageReferenced(&sCheck, i);
63036
63037  /* Check the integrity of the freelist
63038  */
63039  sCheck.zPfx = "Main freelist: ";
63040  checkList(&sCheck, 1, get4byte(&pBt->pPage1->aData[32]),
63041            get4byte(&pBt->pPage1->aData[36]));
63042  sCheck.zPfx = 0;
63043
63044  /* Check all the tables.
63045  */
63046  testcase( pBt->db->flags & SQLITE_CellSizeCk );
63047  pBt->db->flags &= ~SQLITE_CellSizeCk;
63048  for(i=0; (int)i<nRoot && sCheck.mxErr; i++){
63049    i64 notUsed;
63050    if( aRoot[i]==0 ) continue;
63051#ifndef SQLITE_OMIT_AUTOVACUUM
63052    if( pBt->autoVacuum && aRoot[i]>1 ){
63053      checkPtrmap(&sCheck, aRoot[i], PTRMAP_ROOTPAGE, 0);
63054    }
63055#endif
63056    checkTreePage(&sCheck, aRoot[i], &notUsed, LARGEST_INT64);
63057  }
63058  pBt->db->flags = savedDbFlags;
63059
63060  /* Make sure every page in the file is referenced
63061  */
63062  for(i=1; i<=sCheck.nPage && sCheck.mxErr; i++){
63063#ifdef SQLITE_OMIT_AUTOVACUUM
63064    if( getPageReferenced(&sCheck, i)==0 ){
63065      checkAppendMsg(&sCheck, "Page %d is never used", i);
63066    }
63067#else
63068    /* If the database supports auto-vacuum, make sure no tables contain
63069    ** references to pointer-map pages.
63070    */
63071    if( getPageReferenced(&sCheck, i)==0 &&
63072       (PTRMAP_PAGENO(pBt, i)!=i || !pBt->autoVacuum) ){
63073      checkAppendMsg(&sCheck, "Page %d is never used", i);
63074    }
63075    if( getPageReferenced(&sCheck, i)!=0 &&
63076       (PTRMAP_PAGENO(pBt, i)==i && pBt->autoVacuum) ){
63077      checkAppendMsg(&sCheck, "Pointer map page %d is referenced", i);
63078    }
63079#endif
63080  }
63081
63082  /* Clean  up and report errors.
63083  */
63084integrity_ck_cleanup:
63085  sqlite3PageFree(sCheck.heap);
63086  sqlite3_free(sCheck.aPgRef);
63087  if( sCheck.mallocFailed ){
63088    sqlite3StrAccumReset(&sCheck.errMsg);
63089    sCheck.nErr++;
63090  }
63091  *pnErr = sCheck.nErr;
63092  if( sCheck.nErr==0 ) sqlite3StrAccumReset(&sCheck.errMsg);
63093  /* Make sure this analysis did not leave any unref() pages. */
63094  assert( nRef==sqlite3PagerRefcount(pBt->pPager) );
63095  sqlite3BtreeLeave(p);
63096  return sqlite3StrAccumFinish(&sCheck.errMsg);
63097}
63098#endif /* SQLITE_OMIT_INTEGRITY_CHECK */
63099
63100/*
63101** Return the full pathname of the underlying database file.  Return
63102** an empty string if the database is in-memory or a TEMP database.
63103**
63104** The pager filename is invariant as long as the pager is
63105** open so it is safe to access without the BtShared mutex.
63106*/
63107SQLITE_PRIVATE const char *sqlite3BtreeGetFilename(Btree *p){
63108  assert( p->pBt->pPager!=0 );
63109  return sqlite3PagerFilename(p->pBt->pPager, 1);
63110}
63111
63112/*
63113** Return the pathname of the journal file for this database. The return
63114** value of this routine is the same regardless of whether the journal file
63115** has been created or not.
63116**
63117** The pager journal filename is invariant as long as the pager is
63118** open so it is safe to access without the BtShared mutex.
63119*/
63120SQLITE_PRIVATE const char *sqlite3BtreeGetJournalname(Btree *p){
63121  assert( p->pBt->pPager!=0 );
63122  return sqlite3PagerJournalname(p->pBt->pPager);
63123}
63124
63125/*
63126** Return non-zero if a transaction is active.
63127*/
63128SQLITE_PRIVATE int sqlite3BtreeIsInTrans(Btree *p){
63129  assert( p==0 || sqlite3_mutex_held(p->db->mutex) );
63130  return (p && (p->inTrans==TRANS_WRITE));
63131}
63132
63133#ifndef SQLITE_OMIT_WAL
63134/*
63135** Run a checkpoint on the Btree passed as the first argument.
63136**
63137** Return SQLITE_LOCKED if this or any other connection has an open
63138** transaction on the shared-cache the argument Btree is connected to.
63139**
63140** Parameter eMode is one of SQLITE_CHECKPOINT_PASSIVE, FULL or RESTART.
63141*/
63142SQLITE_PRIVATE int sqlite3BtreeCheckpoint(Btree *p, int eMode, int *pnLog, int *pnCkpt){
63143  int rc = SQLITE_OK;
63144  if( p ){
63145    BtShared *pBt = p->pBt;
63146    sqlite3BtreeEnter(p);
63147    if( pBt->inTransaction!=TRANS_NONE ){
63148      rc = SQLITE_LOCKED;
63149    }else{
63150      rc = sqlite3PagerCheckpoint(pBt->pPager, eMode, pnLog, pnCkpt);
63151    }
63152    sqlite3BtreeLeave(p);
63153  }
63154  return rc;
63155}
63156#endif
63157
63158/*
63159** Return non-zero if a read (or write) transaction is active.
63160*/
63161SQLITE_PRIVATE int sqlite3BtreeIsInReadTrans(Btree *p){
63162  assert( p );
63163  assert( sqlite3_mutex_held(p->db->mutex) );
63164  return p->inTrans!=TRANS_NONE;
63165}
63166
63167SQLITE_PRIVATE int sqlite3BtreeIsInBackup(Btree *p){
63168  assert( p );
63169  assert( sqlite3_mutex_held(p->db->mutex) );
63170  return p->nBackup!=0;
63171}
63172
63173/*
63174** This function returns a pointer to a blob of memory associated with
63175** a single shared-btree. The memory is used by client code for its own
63176** purposes (for example, to store a high-level schema associated with
63177** the shared-btree). The btree layer manages reference counting issues.
63178**
63179** The first time this is called on a shared-btree, nBytes bytes of memory
63180** are allocated, zeroed, and returned to the caller. For each subsequent
63181** call the nBytes parameter is ignored and a pointer to the same blob
63182** of memory returned.
63183**
63184** If the nBytes parameter is 0 and the blob of memory has not yet been
63185** allocated, a null pointer is returned. If the blob has already been
63186** allocated, it is returned as normal.
63187**
63188** Just before the shared-btree is closed, the function passed as the
63189** xFree argument when the memory allocation was made is invoked on the
63190** blob of allocated memory. The xFree function should not call sqlite3_free()
63191** on the memory, the btree layer does that.
63192*/
63193SQLITE_PRIVATE void *sqlite3BtreeSchema(Btree *p, int nBytes, void(*xFree)(void *)){
63194  BtShared *pBt = p->pBt;
63195  sqlite3BtreeEnter(p);
63196  if( !pBt->pSchema && nBytes ){
63197    pBt->pSchema = sqlite3DbMallocZero(0, nBytes);
63198    pBt->xFreeSchema = xFree;
63199  }
63200  sqlite3BtreeLeave(p);
63201  return pBt->pSchema;
63202}
63203
63204/*
63205** Return SQLITE_LOCKED_SHAREDCACHE if another user of the same shared
63206** btree as the argument handle holds an exclusive lock on the
63207** sqlite_master table. Otherwise SQLITE_OK.
63208*/
63209SQLITE_PRIVATE int sqlite3BtreeSchemaLocked(Btree *p){
63210  int rc;
63211  assert( sqlite3_mutex_held(p->db->mutex) );
63212  sqlite3BtreeEnter(p);
63213  rc = querySharedCacheTableLock(p, MASTER_ROOT, READ_LOCK);
63214  assert( rc==SQLITE_OK || rc==SQLITE_LOCKED_SHAREDCACHE );
63215  sqlite3BtreeLeave(p);
63216  return rc;
63217}
63218
63219
63220#ifndef SQLITE_OMIT_SHARED_CACHE
63221/*
63222** Obtain a lock on the table whose root page is iTab.  The
63223** lock is a write lock if isWritelock is true or a read lock
63224** if it is false.
63225*/
63226SQLITE_PRIVATE int sqlite3BtreeLockTable(Btree *p, int iTab, u8 isWriteLock){
63227  int rc = SQLITE_OK;
63228  assert( p->inTrans!=TRANS_NONE );
63229  if( p->sharable ){
63230    u8 lockType = READ_LOCK + isWriteLock;
63231    assert( READ_LOCK+1==WRITE_LOCK );
63232    assert( isWriteLock==0 || isWriteLock==1 );
63233
63234    sqlite3BtreeEnter(p);
63235    rc = querySharedCacheTableLock(p, iTab, lockType);
63236    if( rc==SQLITE_OK ){
63237      rc = setSharedCacheTableLock(p, iTab, lockType);
63238    }
63239    sqlite3BtreeLeave(p);
63240  }
63241  return rc;
63242}
63243#endif
63244
63245#ifndef SQLITE_OMIT_INCRBLOB
63246/*
63247** Argument pCsr must be a cursor opened for writing on an
63248** INTKEY table currently pointing at a valid table entry.
63249** This function modifies the data stored as part of that entry.
63250**
63251** Only the data content may only be modified, it is not possible to
63252** change the length of the data stored. If this function is called with
63253** parameters that attempt to write past the end of the existing data,
63254** no modifications are made and SQLITE_CORRUPT is returned.
63255*/
63256SQLITE_PRIVATE int sqlite3BtreePutData(BtCursor *pCsr, u32 offset, u32 amt, void *z){
63257  int rc;
63258  assert( cursorHoldsMutex(pCsr) );
63259  assert( sqlite3_mutex_held(pCsr->pBtree->db->mutex) );
63260  assert( pCsr->curFlags & BTCF_Incrblob );
63261
63262  rc = restoreCursorPosition(pCsr);
63263  if( rc!=SQLITE_OK ){
63264    return rc;
63265  }
63266  assert( pCsr->eState!=CURSOR_REQUIRESEEK );
63267  if( pCsr->eState!=CURSOR_VALID ){
63268    return SQLITE_ABORT;
63269  }
63270
63271  /* Save the positions of all other cursors open on this table. This is
63272  ** required in case any of them are holding references to an xFetch
63273  ** version of the b-tree page modified by the accessPayload call below.
63274  **
63275  ** Note that pCsr must be open on a INTKEY table and saveCursorPosition()
63276  ** and hence saveAllCursors() cannot fail on a BTREE_INTKEY table, hence
63277  ** saveAllCursors can only return SQLITE_OK.
63278  */
63279  VVA_ONLY(rc =) saveAllCursors(pCsr->pBt, pCsr->pgnoRoot, pCsr);
63280  assert( rc==SQLITE_OK );
63281
63282  /* Check some assumptions:
63283  **   (a) the cursor is open for writing,
63284  **   (b) there is a read/write transaction open,
63285  **   (c) the connection holds a write-lock on the table (if required),
63286  **   (d) there are no conflicting read-locks, and
63287  **   (e) the cursor points at a valid row of an intKey table.
63288  */
63289  if( (pCsr->curFlags & BTCF_WriteFlag)==0 ){
63290    return SQLITE_READONLY;
63291  }
63292  assert( (pCsr->pBt->btsFlags & BTS_READ_ONLY)==0
63293              && pCsr->pBt->inTransaction==TRANS_WRITE );
63294  assert( hasSharedCacheTableLock(pCsr->pBtree, pCsr->pgnoRoot, 0, 2) );
63295  assert( !hasReadConflicts(pCsr->pBtree, pCsr->pgnoRoot) );
63296  assert( pCsr->apPage[pCsr->iPage]->intKey );
63297
63298  return accessPayload(pCsr, offset, amt, (unsigned char *)z, 1);
63299}
63300
63301/*
63302** Mark this cursor as an incremental blob cursor.
63303*/
63304SQLITE_PRIVATE void sqlite3BtreeIncrblobCursor(BtCursor *pCur){
63305  pCur->curFlags |= BTCF_Incrblob;
63306  pCur->pBtree->hasIncrblobCur = 1;
63307}
63308#endif
63309
63310/*
63311** Set both the "read version" (single byte at byte offset 18) and
63312** "write version" (single byte at byte offset 19) fields in the database
63313** header to iVersion.
63314*/
63315SQLITE_PRIVATE int sqlite3BtreeSetVersion(Btree *pBtree, int iVersion){
63316  BtShared *pBt = pBtree->pBt;
63317  int rc;                         /* Return code */
63318
63319  assert( iVersion==1 || iVersion==2 );
63320
63321  /* If setting the version fields to 1, do not automatically open the
63322  ** WAL connection, even if the version fields are currently set to 2.
63323  */
63324  pBt->btsFlags &= ~BTS_NO_WAL;
63325  if( iVersion==1 ) pBt->btsFlags |= BTS_NO_WAL;
63326
63327  rc = sqlite3BtreeBeginTrans(pBtree, 0);
63328  if( rc==SQLITE_OK ){
63329    u8 *aData = pBt->pPage1->aData;
63330    if( aData[18]!=(u8)iVersion || aData[19]!=(u8)iVersion ){
63331      rc = sqlite3BtreeBeginTrans(pBtree, 2);
63332      if( rc==SQLITE_OK ){
63333        rc = sqlite3PagerWrite(pBt->pPage1->pDbPage);
63334        if( rc==SQLITE_OK ){
63335          aData[18] = (u8)iVersion;
63336          aData[19] = (u8)iVersion;
63337        }
63338      }
63339    }
63340  }
63341
63342  pBt->btsFlags &= ~BTS_NO_WAL;
63343  return rc;
63344}
63345
63346/*
63347** set the mask of hint flags for cursor pCsr.
63348*/
63349SQLITE_PRIVATE void sqlite3BtreeCursorHints(BtCursor *pCsr, unsigned int mask){
63350  assert( mask==BTREE_BULKLOAD || mask==BTREE_SEEK_EQ || mask==0 );
63351  pCsr->hints = mask;
63352}
63353
63354#ifdef SQLITE_DEBUG
63355/*
63356** Return true if the cursor has a hint specified.  This routine is
63357** only used from within assert() statements
63358*/
63359SQLITE_PRIVATE int sqlite3BtreeCursorHasHint(BtCursor *pCsr, unsigned int mask){
63360  return (pCsr->hints & mask)!=0;
63361}
63362#endif
63363
63364/*
63365** Return true if the given Btree is read-only.
63366*/
63367SQLITE_PRIVATE int sqlite3BtreeIsReadonly(Btree *p){
63368  return (p->pBt->btsFlags & BTS_READ_ONLY)!=0;
63369}
63370
63371/*
63372** Return the size of the header added to each page by this module.
63373*/
63374SQLITE_PRIVATE int sqlite3HeaderSizeBtree(void){ return ROUND8(sizeof(MemPage)); }
63375
63376/************** End of btree.c ***********************************************/
63377/************** Begin file backup.c ******************************************/
63378/*
63379** 2009 January 28
63380**
63381** The author disclaims copyright to this source code.  In place of
63382** a legal notice, here is a blessing:
63383**
63384**    May you do good and not evil.
63385**    May you find forgiveness for yourself and forgive others.
63386**    May you share freely, never taking more than you give.
63387**
63388*************************************************************************
63389** This file contains the implementation of the sqlite3_backup_XXX()
63390** API functions and the related features.
63391*/
63392/* #include "sqliteInt.h" */
63393/* #include "btreeInt.h" */
63394
63395/*
63396** Structure allocated for each backup operation.
63397*/
63398struct sqlite3_backup {
63399  sqlite3* pDestDb;        /* Destination database handle */
63400  Btree *pDest;            /* Destination b-tree file */
63401  u32 iDestSchema;         /* Original schema cookie in destination */
63402  int bDestLocked;         /* True once a write-transaction is open on pDest */
63403
63404  Pgno iNext;              /* Page number of the next source page to copy */
63405  sqlite3* pSrcDb;         /* Source database handle */
63406  Btree *pSrc;             /* Source b-tree file */
63407
63408  int rc;                  /* Backup process error code */
63409
63410  /* These two variables are set by every call to backup_step(). They are
63411  ** read by calls to backup_remaining() and backup_pagecount().
63412  */
63413  Pgno nRemaining;         /* Number of pages left to copy */
63414  Pgno nPagecount;         /* Total number of pages to copy */
63415
63416  int isAttached;          /* True once backup has been registered with pager */
63417  sqlite3_backup *pNext;   /* Next backup associated with source pager */
63418};
63419
63420/*
63421** THREAD SAFETY NOTES:
63422**
63423**   Once it has been created using backup_init(), a single sqlite3_backup
63424**   structure may be accessed via two groups of thread-safe entry points:
63425**
63426**     * Via the sqlite3_backup_XXX() API function backup_step() and
63427**       backup_finish(). Both these functions obtain the source database
63428**       handle mutex and the mutex associated with the source BtShared
63429**       structure, in that order.
63430**
63431**     * Via the BackupUpdate() and BackupRestart() functions, which are
63432**       invoked by the pager layer to report various state changes in
63433**       the page cache associated with the source database. The mutex
63434**       associated with the source database BtShared structure will always
63435**       be held when either of these functions are invoked.
63436**
63437**   The other sqlite3_backup_XXX() API functions, backup_remaining() and
63438**   backup_pagecount() are not thread-safe functions. If they are called
63439**   while some other thread is calling backup_step() or backup_finish(),
63440**   the values returned may be invalid. There is no way for a call to
63441**   BackupUpdate() or BackupRestart() to interfere with backup_remaining()
63442**   or backup_pagecount().
63443**
63444**   Depending on the SQLite configuration, the database handles and/or
63445**   the Btree objects may have their own mutexes that require locking.
63446**   Non-sharable Btrees (in-memory databases for example), do not have
63447**   associated mutexes.
63448*/
63449
63450/*
63451** Return a pointer corresponding to database zDb (i.e. "main", "temp")
63452** in connection handle pDb. If such a database cannot be found, return
63453** a NULL pointer and write an error message to pErrorDb.
63454**
63455** If the "temp" database is requested, it may need to be opened by this
63456** function. If an error occurs while doing so, return 0 and write an
63457** error message to pErrorDb.
63458*/
63459static Btree *findBtree(sqlite3 *pErrorDb, sqlite3 *pDb, const char *zDb){
63460  int i = sqlite3FindDbName(pDb, zDb);
63461
63462  if( i==1 ){
63463    Parse *pParse;
63464    int rc = 0;
63465    pParse = sqlite3StackAllocZero(pErrorDb, sizeof(*pParse));
63466    if( pParse==0 ){
63467      sqlite3ErrorWithMsg(pErrorDb, SQLITE_NOMEM, "out of memory");
63468      rc = SQLITE_NOMEM;
63469    }else{
63470      pParse->db = pDb;
63471      if( sqlite3OpenTempDatabase(pParse) ){
63472        sqlite3ErrorWithMsg(pErrorDb, pParse->rc, "%s", pParse->zErrMsg);
63473        rc = SQLITE_ERROR;
63474      }
63475      sqlite3DbFree(pErrorDb, pParse->zErrMsg);
63476      sqlite3ParserReset(pParse);
63477      sqlite3StackFree(pErrorDb, pParse);
63478    }
63479    if( rc ){
63480      return 0;
63481    }
63482  }
63483
63484  if( i<0 ){
63485    sqlite3ErrorWithMsg(pErrorDb, SQLITE_ERROR, "unknown database %s", zDb);
63486    return 0;
63487  }
63488
63489  return pDb->aDb[i].pBt;
63490}
63491
63492/*
63493** Attempt to set the page size of the destination to match the page size
63494** of the source.
63495*/
63496static int setDestPgsz(sqlite3_backup *p){
63497  int rc;
63498  rc = sqlite3BtreeSetPageSize(p->pDest,sqlite3BtreeGetPageSize(p->pSrc),-1,0);
63499  return rc;
63500}
63501
63502/*
63503** Check that there is no open read-transaction on the b-tree passed as the
63504** second argument. If there is not, return SQLITE_OK. Otherwise, if there
63505** is an open read-transaction, return SQLITE_ERROR and leave an error
63506** message in database handle db.
63507*/
63508static int checkReadTransaction(sqlite3 *db, Btree *p){
63509  if( sqlite3BtreeIsInReadTrans(p) ){
63510    sqlite3ErrorWithMsg(db, SQLITE_ERROR, "destination database is in use");
63511    return SQLITE_ERROR;
63512  }
63513  return SQLITE_OK;
63514}
63515
63516/*
63517** Create an sqlite3_backup process to copy the contents of zSrcDb from
63518** connection handle pSrcDb to zDestDb in pDestDb. If successful, return
63519** a pointer to the new sqlite3_backup object.
63520**
63521** If an error occurs, NULL is returned and an error code and error message
63522** stored in database handle pDestDb.
63523*/
63524SQLITE_API sqlite3_backup *SQLITE_STDCALL sqlite3_backup_init(
63525  sqlite3* pDestDb,                     /* Database to write to */
63526  const char *zDestDb,                  /* Name of database within pDestDb */
63527  sqlite3* pSrcDb,                      /* Database connection to read from */
63528  const char *zSrcDb                    /* Name of database within pSrcDb */
63529){
63530  sqlite3_backup *p;                    /* Value to return */
63531
63532#ifdef SQLITE_ENABLE_API_ARMOR
63533  if( !sqlite3SafetyCheckOk(pSrcDb)||!sqlite3SafetyCheckOk(pDestDb) ){
63534    (void)SQLITE_MISUSE_BKPT;
63535    return 0;
63536  }
63537#endif
63538
63539  /* Lock the source database handle. The destination database
63540  ** handle is not locked in this routine, but it is locked in
63541  ** sqlite3_backup_step(). The user is required to ensure that no
63542  ** other thread accesses the destination handle for the duration
63543  ** of the backup operation.  Any attempt to use the destination
63544  ** database connection while a backup is in progress may cause
63545  ** a malfunction or a deadlock.
63546  */
63547  sqlite3_mutex_enter(pSrcDb->mutex);
63548  sqlite3_mutex_enter(pDestDb->mutex);
63549
63550  if( pSrcDb==pDestDb ){
63551    sqlite3ErrorWithMsg(
63552        pDestDb, SQLITE_ERROR, "source and destination must be distinct"
63553    );
63554    p = 0;
63555  }else {
63556    /* Allocate space for a new sqlite3_backup object...
63557    ** EVIDENCE-OF: R-64852-21591 The sqlite3_backup object is created by a
63558    ** call to sqlite3_backup_init() and is destroyed by a call to
63559    ** sqlite3_backup_finish(). */
63560    p = (sqlite3_backup *)sqlite3MallocZero(sizeof(sqlite3_backup));
63561    if( !p ){
63562      sqlite3Error(pDestDb, SQLITE_NOMEM);
63563    }
63564  }
63565
63566  /* If the allocation succeeded, populate the new object. */
63567  if( p ){
63568    p->pSrc = findBtree(pDestDb, pSrcDb, zSrcDb);
63569    p->pDest = findBtree(pDestDb, pDestDb, zDestDb);
63570    p->pDestDb = pDestDb;
63571    p->pSrcDb = pSrcDb;
63572    p->iNext = 1;
63573    p->isAttached = 0;
63574
63575    if( 0==p->pSrc || 0==p->pDest
63576     || setDestPgsz(p)==SQLITE_NOMEM
63577     || checkReadTransaction(pDestDb, p->pDest)!=SQLITE_OK
63578     ){
63579      /* One (or both) of the named databases did not exist or an OOM
63580      ** error was hit. Or there is a transaction open on the destination
63581      ** database. The error has already been written into the pDestDb
63582      ** handle. All that is left to do here is free the sqlite3_backup
63583      ** structure.  */
63584      sqlite3_free(p);
63585      p = 0;
63586    }
63587  }
63588  if( p ){
63589    p->pSrc->nBackup++;
63590  }
63591
63592  sqlite3_mutex_leave(pDestDb->mutex);
63593  sqlite3_mutex_leave(pSrcDb->mutex);
63594  return p;
63595}
63596
63597/*
63598** Argument rc is an SQLite error code. Return true if this error is
63599** considered fatal if encountered during a backup operation. All errors
63600** are considered fatal except for SQLITE_BUSY and SQLITE_LOCKED.
63601*/
63602static int isFatalError(int rc){
63603  return (rc!=SQLITE_OK && rc!=SQLITE_BUSY && ALWAYS(rc!=SQLITE_LOCKED));
63604}
63605
63606/*
63607** Parameter zSrcData points to a buffer containing the data for
63608** page iSrcPg from the source database. Copy this data into the
63609** destination database.
63610*/
63611static int backupOnePage(
63612  sqlite3_backup *p,              /* Backup handle */
63613  Pgno iSrcPg,                    /* Source database page to backup */
63614  const u8 *zSrcData,             /* Source database page data */
63615  int bUpdate                     /* True for an update, false otherwise */
63616){
63617  Pager * const pDestPager = sqlite3BtreePager(p->pDest);
63618  const int nSrcPgsz = sqlite3BtreeGetPageSize(p->pSrc);
63619  int nDestPgsz = sqlite3BtreeGetPageSize(p->pDest);
63620  const int nCopy = MIN(nSrcPgsz, nDestPgsz);
63621  const i64 iEnd = (i64)iSrcPg*(i64)nSrcPgsz;
63622#ifdef SQLITE_HAS_CODEC
63623  /* Use BtreeGetReserveNoMutex() for the source b-tree, as although it is
63624  ** guaranteed that the shared-mutex is held by this thread, handle
63625  ** p->pSrc may not actually be the owner.  */
63626  int nSrcReserve = sqlite3BtreeGetReserveNoMutex(p->pSrc);
63627  int nDestReserve = sqlite3BtreeGetOptimalReserve(p->pDest);
63628#endif
63629  int rc = SQLITE_OK;
63630  i64 iOff;
63631
63632  assert( sqlite3BtreeGetReserveNoMutex(p->pSrc)>=0 );
63633  assert( p->bDestLocked );
63634  assert( !isFatalError(p->rc) );
63635  assert( iSrcPg!=PENDING_BYTE_PAGE(p->pSrc->pBt) );
63636  assert( zSrcData );
63637
63638  /* Catch the case where the destination is an in-memory database and the
63639  ** page sizes of the source and destination differ.
63640  */
63641  if( nSrcPgsz!=nDestPgsz && sqlite3PagerIsMemdb(pDestPager) ){
63642    rc = SQLITE_READONLY;
63643  }
63644
63645#ifdef SQLITE_HAS_CODEC
63646  /* Backup is not possible if the page size of the destination is changing
63647  ** and a codec is in use.
63648  */
63649  if( nSrcPgsz!=nDestPgsz && sqlite3PagerGetCodec(pDestPager)!=0 ){
63650    rc = SQLITE_READONLY;
63651  }
63652
63653  /* Backup is not possible if the number of bytes of reserve space differ
63654  ** between source and destination.  If there is a difference, try to
63655  ** fix the destination to agree with the source.  If that is not possible,
63656  ** then the backup cannot proceed.
63657  */
63658  if( nSrcReserve!=nDestReserve ){
63659    u32 newPgsz = nSrcPgsz;
63660    rc = sqlite3PagerSetPagesize(pDestPager, &newPgsz, nSrcReserve);
63661    if( rc==SQLITE_OK && newPgsz!=nSrcPgsz ) rc = SQLITE_READONLY;
63662  }
63663#endif
63664
63665  /* This loop runs once for each destination page spanned by the source
63666  ** page. For each iteration, variable iOff is set to the byte offset
63667  ** of the destination page.
63668  */
63669  for(iOff=iEnd-(i64)nSrcPgsz; rc==SQLITE_OK && iOff<iEnd; iOff+=nDestPgsz){
63670    DbPage *pDestPg = 0;
63671    Pgno iDest = (Pgno)(iOff/nDestPgsz)+1;
63672    if( iDest==PENDING_BYTE_PAGE(p->pDest->pBt) ) continue;
63673    if( SQLITE_OK==(rc = sqlite3PagerGet(pDestPager, iDest, &pDestPg))
63674     && SQLITE_OK==(rc = sqlite3PagerWrite(pDestPg))
63675    ){
63676      const u8 *zIn = &zSrcData[iOff%nSrcPgsz];
63677      u8 *zDestData = sqlite3PagerGetData(pDestPg);
63678      u8 *zOut = &zDestData[iOff%nDestPgsz];
63679
63680      /* Copy the data from the source page into the destination page.
63681      ** Then clear the Btree layer MemPage.isInit flag. Both this module
63682      ** and the pager code use this trick (clearing the first byte
63683      ** of the page 'extra' space to invalidate the Btree layers
63684      ** cached parse of the page). MemPage.isInit is marked
63685      ** "MUST BE FIRST" for this purpose.
63686      */
63687      memcpy(zOut, zIn, nCopy);
63688      ((u8 *)sqlite3PagerGetExtra(pDestPg))[0] = 0;
63689      if( iOff==0 && bUpdate==0 ){
63690        sqlite3Put4byte(&zOut[28], sqlite3BtreeLastPage(p->pSrc));
63691      }
63692    }
63693    sqlite3PagerUnref(pDestPg);
63694  }
63695
63696  return rc;
63697}
63698
63699/*
63700** If pFile is currently larger than iSize bytes, then truncate it to
63701** exactly iSize bytes. If pFile is not larger than iSize bytes, then
63702** this function is a no-op.
63703**
63704** Return SQLITE_OK if everything is successful, or an SQLite error
63705** code if an error occurs.
63706*/
63707static int backupTruncateFile(sqlite3_file *pFile, i64 iSize){
63708  i64 iCurrent;
63709  int rc = sqlite3OsFileSize(pFile, &iCurrent);
63710  if( rc==SQLITE_OK && iCurrent>iSize ){
63711    rc = sqlite3OsTruncate(pFile, iSize);
63712  }
63713  return rc;
63714}
63715
63716/*
63717** Register this backup object with the associated source pager for
63718** callbacks when pages are changed or the cache invalidated.
63719*/
63720static void attachBackupObject(sqlite3_backup *p){
63721  sqlite3_backup **pp;
63722  assert( sqlite3BtreeHoldsMutex(p->pSrc) );
63723  pp = sqlite3PagerBackupPtr(sqlite3BtreePager(p->pSrc));
63724  p->pNext = *pp;
63725  *pp = p;
63726  p->isAttached = 1;
63727}
63728
63729/*
63730** Copy nPage pages from the source b-tree to the destination.
63731*/
63732SQLITE_API int SQLITE_STDCALL sqlite3_backup_step(sqlite3_backup *p, int nPage){
63733  int rc;
63734  int destMode;       /* Destination journal mode */
63735  int pgszSrc = 0;    /* Source page size */
63736  int pgszDest = 0;   /* Destination page size */
63737
63738#ifdef SQLITE_ENABLE_API_ARMOR
63739  if( p==0 ) return SQLITE_MISUSE_BKPT;
63740#endif
63741  sqlite3_mutex_enter(p->pSrcDb->mutex);
63742  sqlite3BtreeEnter(p->pSrc);
63743  if( p->pDestDb ){
63744    sqlite3_mutex_enter(p->pDestDb->mutex);
63745  }
63746
63747  rc = p->rc;
63748  if( !isFatalError(rc) ){
63749    Pager * const pSrcPager = sqlite3BtreePager(p->pSrc);     /* Source pager */
63750    Pager * const pDestPager = sqlite3BtreePager(p->pDest);   /* Dest pager */
63751    int ii;                            /* Iterator variable */
63752    int nSrcPage = -1;                 /* Size of source db in pages */
63753    int bCloseTrans = 0;               /* True if src db requires unlocking */
63754
63755    /* If the source pager is currently in a write-transaction, return
63756    ** SQLITE_BUSY immediately.
63757    */
63758    if( p->pDestDb && p->pSrc->pBt->inTransaction==TRANS_WRITE ){
63759      rc = SQLITE_BUSY;
63760    }else{
63761      rc = SQLITE_OK;
63762    }
63763
63764    /* Lock the destination database, if it is not locked already. */
63765    if( SQLITE_OK==rc && p->bDestLocked==0
63766     && SQLITE_OK==(rc = sqlite3BtreeBeginTrans(p->pDest, 2))
63767    ){
63768      p->bDestLocked = 1;
63769      sqlite3BtreeGetMeta(p->pDest, BTREE_SCHEMA_VERSION, &p->iDestSchema);
63770    }
63771
63772    /* If there is no open read-transaction on the source database, open
63773    ** one now. If a transaction is opened here, then it will be closed
63774    ** before this function exits.
63775    */
63776    if( rc==SQLITE_OK && 0==sqlite3BtreeIsInReadTrans(p->pSrc) ){
63777      rc = sqlite3BtreeBeginTrans(p->pSrc, 0);
63778      bCloseTrans = 1;
63779    }
63780
63781    /* Do not allow backup if the destination database is in WAL mode
63782    ** and the page sizes are different between source and destination */
63783    pgszSrc = sqlite3BtreeGetPageSize(p->pSrc);
63784    pgszDest = sqlite3BtreeGetPageSize(p->pDest);
63785    destMode = sqlite3PagerGetJournalMode(sqlite3BtreePager(p->pDest));
63786    if( SQLITE_OK==rc && destMode==PAGER_JOURNALMODE_WAL && pgszSrc!=pgszDest ){
63787      rc = SQLITE_READONLY;
63788    }
63789
63790    /* Now that there is a read-lock on the source database, query the
63791    ** source pager for the number of pages in the database.
63792    */
63793    nSrcPage = (int)sqlite3BtreeLastPage(p->pSrc);
63794    assert( nSrcPage>=0 );
63795    for(ii=0; (nPage<0 || ii<nPage) && p->iNext<=(Pgno)nSrcPage && !rc; ii++){
63796      const Pgno iSrcPg = p->iNext;                 /* Source page number */
63797      if( iSrcPg!=PENDING_BYTE_PAGE(p->pSrc->pBt) ){
63798        DbPage *pSrcPg;                             /* Source page object */
63799        rc = sqlite3PagerAcquire(pSrcPager, iSrcPg, &pSrcPg,
63800                                 PAGER_GET_READONLY);
63801        if( rc==SQLITE_OK ){
63802          rc = backupOnePage(p, iSrcPg, sqlite3PagerGetData(pSrcPg), 0);
63803          sqlite3PagerUnref(pSrcPg);
63804        }
63805      }
63806      p->iNext++;
63807    }
63808    if( rc==SQLITE_OK ){
63809      p->nPagecount = nSrcPage;
63810      p->nRemaining = nSrcPage+1-p->iNext;
63811      if( p->iNext>(Pgno)nSrcPage ){
63812        rc = SQLITE_DONE;
63813      }else if( !p->isAttached ){
63814        attachBackupObject(p);
63815      }
63816    }
63817
63818    /* Update the schema version field in the destination database. This
63819    ** is to make sure that the schema-version really does change in
63820    ** the case where the source and destination databases have the
63821    ** same schema version.
63822    */
63823    if( rc==SQLITE_DONE ){
63824      if( nSrcPage==0 ){
63825        rc = sqlite3BtreeNewDb(p->pDest);
63826        nSrcPage = 1;
63827      }
63828      if( rc==SQLITE_OK || rc==SQLITE_DONE ){
63829        rc = sqlite3BtreeUpdateMeta(p->pDest,1,p->iDestSchema+1);
63830      }
63831      if( rc==SQLITE_OK ){
63832        if( p->pDestDb ){
63833          sqlite3ResetAllSchemasOfConnection(p->pDestDb);
63834        }
63835        if( destMode==PAGER_JOURNALMODE_WAL ){
63836          rc = sqlite3BtreeSetVersion(p->pDest, 2);
63837        }
63838      }
63839      if( rc==SQLITE_OK ){
63840        int nDestTruncate;
63841        /* Set nDestTruncate to the final number of pages in the destination
63842        ** database. The complication here is that the destination page
63843        ** size may be different to the source page size.
63844        **
63845        ** If the source page size is smaller than the destination page size,
63846        ** round up. In this case the call to sqlite3OsTruncate() below will
63847        ** fix the size of the file. However it is important to call
63848        ** sqlite3PagerTruncateImage() here so that any pages in the
63849        ** destination file that lie beyond the nDestTruncate page mark are
63850        ** journalled by PagerCommitPhaseOne() before they are destroyed
63851        ** by the file truncation.
63852        */
63853        assert( pgszSrc==sqlite3BtreeGetPageSize(p->pSrc) );
63854        assert( pgszDest==sqlite3BtreeGetPageSize(p->pDest) );
63855        if( pgszSrc<pgszDest ){
63856          int ratio = pgszDest/pgszSrc;
63857          nDestTruncate = (nSrcPage+ratio-1)/ratio;
63858          if( nDestTruncate==(int)PENDING_BYTE_PAGE(p->pDest->pBt) ){
63859            nDestTruncate--;
63860          }
63861        }else{
63862          nDestTruncate = nSrcPage * (pgszSrc/pgszDest);
63863        }
63864        assert( nDestTruncate>0 );
63865
63866        if( pgszSrc<pgszDest ){
63867          /* If the source page-size is smaller than the destination page-size,
63868          ** two extra things may need to happen:
63869          **
63870          **   * The destination may need to be truncated, and
63871          **
63872          **   * Data stored on the pages immediately following the
63873          **     pending-byte page in the source database may need to be
63874          **     copied into the destination database.
63875          */
63876          const i64 iSize = (i64)pgszSrc * (i64)nSrcPage;
63877          sqlite3_file * const pFile = sqlite3PagerFile(pDestPager);
63878          Pgno iPg;
63879          int nDstPage;
63880          i64 iOff;
63881          i64 iEnd;
63882
63883          assert( pFile );
63884          assert( nDestTruncate==0
63885              || (i64)nDestTruncate*(i64)pgszDest >= iSize || (
63886                nDestTruncate==(int)(PENDING_BYTE_PAGE(p->pDest->pBt)-1)
63887             && iSize>=PENDING_BYTE && iSize<=PENDING_BYTE+pgszDest
63888          ));
63889
63890          /* This block ensures that all data required to recreate the original
63891          ** database has been stored in the journal for pDestPager and the
63892          ** journal synced to disk. So at this point we may safely modify
63893          ** the database file in any way, knowing that if a power failure
63894          ** occurs, the original database will be reconstructed from the
63895          ** journal file.  */
63896          sqlite3PagerPagecount(pDestPager, &nDstPage);
63897          for(iPg=nDestTruncate; rc==SQLITE_OK && iPg<=(Pgno)nDstPage; iPg++){
63898            if( iPg!=PENDING_BYTE_PAGE(p->pDest->pBt) ){
63899              DbPage *pPg;
63900              rc = sqlite3PagerGet(pDestPager, iPg, &pPg);
63901              if( rc==SQLITE_OK ){
63902                rc = sqlite3PagerWrite(pPg);
63903                sqlite3PagerUnref(pPg);
63904              }
63905            }
63906          }
63907          if( rc==SQLITE_OK ){
63908            rc = sqlite3PagerCommitPhaseOne(pDestPager, 0, 1);
63909          }
63910
63911          /* Write the extra pages and truncate the database file as required */
63912          iEnd = MIN(PENDING_BYTE + pgszDest, iSize);
63913          for(
63914            iOff=PENDING_BYTE+pgszSrc;
63915            rc==SQLITE_OK && iOff<iEnd;
63916            iOff+=pgszSrc
63917          ){
63918            PgHdr *pSrcPg = 0;
63919            const Pgno iSrcPg = (Pgno)((iOff/pgszSrc)+1);
63920            rc = sqlite3PagerGet(pSrcPager, iSrcPg, &pSrcPg);
63921            if( rc==SQLITE_OK ){
63922              u8 *zData = sqlite3PagerGetData(pSrcPg);
63923              rc = sqlite3OsWrite(pFile, zData, pgszSrc, iOff);
63924            }
63925            sqlite3PagerUnref(pSrcPg);
63926          }
63927          if( rc==SQLITE_OK ){
63928            rc = backupTruncateFile(pFile, iSize);
63929          }
63930
63931          /* Sync the database file to disk. */
63932          if( rc==SQLITE_OK ){
63933            rc = sqlite3PagerSync(pDestPager, 0);
63934          }
63935        }else{
63936          sqlite3PagerTruncateImage(pDestPager, nDestTruncate);
63937          rc = sqlite3PagerCommitPhaseOne(pDestPager, 0, 0);
63938        }
63939
63940        /* Finish committing the transaction to the destination database. */
63941        if( SQLITE_OK==rc
63942         && SQLITE_OK==(rc = sqlite3BtreeCommitPhaseTwo(p->pDest, 0))
63943        ){
63944          rc = SQLITE_DONE;
63945        }
63946      }
63947    }
63948
63949    /* If bCloseTrans is true, then this function opened a read transaction
63950    ** on the source database. Close the read transaction here. There is
63951    ** no need to check the return values of the btree methods here, as
63952    ** "committing" a read-only transaction cannot fail.
63953    */
63954    if( bCloseTrans ){
63955      TESTONLY( int rc2 );
63956      TESTONLY( rc2  = ) sqlite3BtreeCommitPhaseOne(p->pSrc, 0);
63957      TESTONLY( rc2 |= ) sqlite3BtreeCommitPhaseTwo(p->pSrc, 0);
63958      assert( rc2==SQLITE_OK );
63959    }
63960
63961    if( rc==SQLITE_IOERR_NOMEM ){
63962      rc = SQLITE_NOMEM;
63963    }
63964    p->rc = rc;
63965  }
63966  if( p->pDestDb ){
63967    sqlite3_mutex_leave(p->pDestDb->mutex);
63968  }
63969  sqlite3BtreeLeave(p->pSrc);
63970  sqlite3_mutex_leave(p->pSrcDb->mutex);
63971  return rc;
63972}
63973
63974/*
63975** Release all resources associated with an sqlite3_backup* handle.
63976*/
63977SQLITE_API int SQLITE_STDCALL sqlite3_backup_finish(sqlite3_backup *p){
63978  sqlite3_backup **pp;                 /* Ptr to head of pagers backup list */
63979  sqlite3 *pSrcDb;                     /* Source database connection */
63980  int rc;                              /* Value to return */
63981
63982  /* Enter the mutexes */
63983  if( p==0 ) return SQLITE_OK;
63984  pSrcDb = p->pSrcDb;
63985  sqlite3_mutex_enter(pSrcDb->mutex);
63986  sqlite3BtreeEnter(p->pSrc);
63987  if( p->pDestDb ){
63988    sqlite3_mutex_enter(p->pDestDb->mutex);
63989  }
63990
63991  /* Detach this backup from the source pager. */
63992  if( p->pDestDb ){
63993    p->pSrc->nBackup--;
63994  }
63995  if( p->isAttached ){
63996    pp = sqlite3PagerBackupPtr(sqlite3BtreePager(p->pSrc));
63997    while( *pp!=p ){
63998      pp = &(*pp)->pNext;
63999    }
64000    *pp = p->pNext;
64001  }
64002
64003  /* If a transaction is still open on the Btree, roll it back. */
64004  sqlite3BtreeRollback(p->pDest, SQLITE_OK, 0);
64005
64006  /* Set the error code of the destination database handle. */
64007  rc = (p->rc==SQLITE_DONE) ? SQLITE_OK : p->rc;
64008  if( p->pDestDb ){
64009    sqlite3Error(p->pDestDb, rc);
64010
64011    /* Exit the mutexes and free the backup context structure. */
64012    sqlite3LeaveMutexAndCloseZombie(p->pDestDb);
64013  }
64014  sqlite3BtreeLeave(p->pSrc);
64015  if( p->pDestDb ){
64016    /* EVIDENCE-OF: R-64852-21591 The sqlite3_backup object is created by a
64017    ** call to sqlite3_backup_init() and is destroyed by a call to
64018    ** sqlite3_backup_finish(). */
64019    sqlite3_free(p);
64020  }
64021  sqlite3LeaveMutexAndCloseZombie(pSrcDb);
64022  return rc;
64023}
64024
64025/*
64026** Return the number of pages still to be backed up as of the most recent
64027** call to sqlite3_backup_step().
64028*/
64029SQLITE_API int SQLITE_STDCALL sqlite3_backup_remaining(sqlite3_backup *p){
64030#ifdef SQLITE_ENABLE_API_ARMOR
64031  if( p==0 ){
64032    (void)SQLITE_MISUSE_BKPT;
64033    return 0;
64034  }
64035#endif
64036  return p->nRemaining;
64037}
64038
64039/*
64040** Return the total number of pages in the source database as of the most
64041** recent call to sqlite3_backup_step().
64042*/
64043SQLITE_API int SQLITE_STDCALL sqlite3_backup_pagecount(sqlite3_backup *p){
64044#ifdef SQLITE_ENABLE_API_ARMOR
64045  if( p==0 ){
64046    (void)SQLITE_MISUSE_BKPT;
64047    return 0;
64048  }
64049#endif
64050  return p->nPagecount;
64051}
64052
64053/*
64054** This function is called after the contents of page iPage of the
64055** source database have been modified. If page iPage has already been
64056** copied into the destination database, then the data written to the
64057** destination is now invalidated. The destination copy of iPage needs
64058** to be updated with the new data before the backup operation is
64059** complete.
64060**
64061** It is assumed that the mutex associated with the BtShared object
64062** corresponding to the source database is held when this function is
64063** called.
64064*/
64065static SQLITE_NOINLINE void backupUpdate(
64066  sqlite3_backup *p,
64067  Pgno iPage,
64068  const u8 *aData
64069){
64070  assert( p!=0 );
64071  do{
64072    assert( sqlite3_mutex_held(p->pSrc->pBt->mutex) );
64073    if( !isFatalError(p->rc) && iPage<p->iNext ){
64074      /* The backup process p has already copied page iPage. But now it
64075      ** has been modified by a transaction on the source pager. Copy
64076      ** the new data into the backup.
64077      */
64078      int rc;
64079      assert( p->pDestDb );
64080      sqlite3_mutex_enter(p->pDestDb->mutex);
64081      rc = backupOnePage(p, iPage, aData, 1);
64082      sqlite3_mutex_leave(p->pDestDb->mutex);
64083      assert( rc!=SQLITE_BUSY && rc!=SQLITE_LOCKED );
64084      if( rc!=SQLITE_OK ){
64085        p->rc = rc;
64086      }
64087    }
64088  }while( (p = p->pNext)!=0 );
64089}
64090SQLITE_PRIVATE void sqlite3BackupUpdate(sqlite3_backup *pBackup, Pgno iPage, const u8 *aData){
64091  if( pBackup ) backupUpdate(pBackup, iPage, aData);
64092}
64093
64094/*
64095** Restart the backup process. This is called when the pager layer
64096** detects that the database has been modified by an external database
64097** connection. In this case there is no way of knowing which of the
64098** pages that have been copied into the destination database are still
64099** valid and which are not, so the entire process needs to be restarted.
64100**
64101** It is assumed that the mutex associated with the BtShared object
64102** corresponding to the source database is held when this function is
64103** called.
64104*/
64105SQLITE_PRIVATE void sqlite3BackupRestart(sqlite3_backup *pBackup){
64106  sqlite3_backup *p;                   /* Iterator variable */
64107  for(p=pBackup; p; p=p->pNext){
64108    assert( sqlite3_mutex_held(p->pSrc->pBt->mutex) );
64109    p->iNext = 1;
64110  }
64111}
64112
64113#ifndef SQLITE_OMIT_VACUUM
64114/*
64115** Copy the complete content of pBtFrom into pBtTo.  A transaction
64116** must be active for both files.
64117**
64118** The size of file pTo may be reduced by this operation. If anything
64119** goes wrong, the transaction on pTo is rolled back. If successful, the
64120** transaction is committed before returning.
64121*/
64122SQLITE_PRIVATE int sqlite3BtreeCopyFile(Btree *pTo, Btree *pFrom){
64123  int rc;
64124  sqlite3_file *pFd;              /* File descriptor for database pTo */
64125  sqlite3_backup b;
64126  sqlite3BtreeEnter(pTo);
64127  sqlite3BtreeEnter(pFrom);
64128
64129  assert( sqlite3BtreeIsInTrans(pTo) );
64130  pFd = sqlite3PagerFile(sqlite3BtreePager(pTo));
64131  if( pFd->pMethods ){
64132    i64 nByte = sqlite3BtreeGetPageSize(pFrom)*(i64)sqlite3BtreeLastPage(pFrom);
64133    rc = sqlite3OsFileControl(pFd, SQLITE_FCNTL_OVERWRITE, &nByte);
64134    if( rc==SQLITE_NOTFOUND ) rc = SQLITE_OK;
64135    if( rc ) goto copy_finished;
64136  }
64137
64138  /* Set up an sqlite3_backup object. sqlite3_backup.pDestDb must be set
64139  ** to 0. This is used by the implementations of sqlite3_backup_step()
64140  ** and sqlite3_backup_finish() to detect that they are being called
64141  ** from this function, not directly by the user.
64142  */
64143  memset(&b, 0, sizeof(b));
64144  b.pSrcDb = pFrom->db;
64145  b.pSrc = pFrom;
64146  b.pDest = pTo;
64147  b.iNext = 1;
64148
64149  /* 0x7FFFFFFF is the hard limit for the number of pages in a database
64150  ** file. By passing this as the number of pages to copy to
64151  ** sqlite3_backup_step(), we can guarantee that the copy finishes
64152  ** within a single call (unless an error occurs). The assert() statement
64153  ** checks this assumption - (p->rc) should be set to either SQLITE_DONE
64154  ** or an error code.
64155  */
64156  sqlite3_backup_step(&b, 0x7FFFFFFF);
64157  assert( b.rc!=SQLITE_OK );
64158  rc = sqlite3_backup_finish(&b);
64159  if( rc==SQLITE_OK ){
64160    pTo->pBt->btsFlags &= ~BTS_PAGESIZE_FIXED;
64161  }else{
64162    sqlite3PagerClearCache(sqlite3BtreePager(b.pDest));
64163  }
64164
64165  assert( sqlite3BtreeIsInTrans(pTo)==0 );
64166copy_finished:
64167  sqlite3BtreeLeave(pFrom);
64168  sqlite3BtreeLeave(pTo);
64169  return rc;
64170}
64171#endif /* SQLITE_OMIT_VACUUM */
64172
64173/************** End of backup.c **********************************************/
64174/************** Begin file vdbemem.c *****************************************/
64175/*
64176** 2004 May 26
64177**
64178** The author disclaims copyright to this source code.  In place of
64179** a legal notice, here is a blessing:
64180**
64181**    May you do good and not evil.
64182**    May you find forgiveness for yourself and forgive others.
64183**    May you share freely, never taking more than you give.
64184**
64185*************************************************************************
64186**
64187** This file contains code use to manipulate "Mem" structure.  A "Mem"
64188** stores a single value in the VDBE.  Mem is an opaque structure visible
64189** only within the VDBE.  Interface routines refer to a Mem using the
64190** name sqlite_value
64191*/
64192/* #include "sqliteInt.h" */
64193/* #include "vdbeInt.h" */
64194
64195#ifdef SQLITE_DEBUG
64196/*
64197** Check invariants on a Mem object.
64198**
64199** This routine is intended for use inside of assert() statements, like
64200** this:    assert( sqlite3VdbeCheckMemInvariants(pMem) );
64201*/
64202SQLITE_PRIVATE int sqlite3VdbeCheckMemInvariants(Mem *p){
64203  /* If MEM_Dyn is set then Mem.xDel!=0.
64204  ** Mem.xDel is might not be initialized if MEM_Dyn is clear.
64205  */
64206  assert( (p->flags & MEM_Dyn)==0 || p->xDel!=0 );
64207
64208  /* MEM_Dyn may only be set if Mem.szMalloc==0.  In this way we
64209  ** ensure that if Mem.szMalloc>0 then it is safe to do
64210  ** Mem.z = Mem.zMalloc without having to check Mem.flags&MEM_Dyn.
64211  ** That saves a few cycles in inner loops. */
64212  assert( (p->flags & MEM_Dyn)==0 || p->szMalloc==0 );
64213
64214  /* Cannot be both MEM_Int and MEM_Real at the same time */
64215  assert( (p->flags & (MEM_Int|MEM_Real))!=(MEM_Int|MEM_Real) );
64216
64217  /* The szMalloc field holds the correct memory allocation size */
64218  assert( p->szMalloc==0
64219       || p->szMalloc==sqlite3DbMallocSize(p->db,p->zMalloc) );
64220
64221  /* If p holds a string or blob, the Mem.z must point to exactly
64222  ** one of the following:
64223  **
64224  **   (1) Memory in Mem.zMalloc and managed by the Mem object
64225  **   (2) Memory to be freed using Mem.xDel
64226  **   (3) An ephemeral string or blob
64227  **   (4) A static string or blob
64228  */
64229  if( (p->flags & (MEM_Str|MEM_Blob)) && p->n>0 ){
64230    assert(
64231      ((p->szMalloc>0 && p->z==p->zMalloc)? 1 : 0) +
64232      ((p->flags&MEM_Dyn)!=0 ? 1 : 0) +
64233      ((p->flags&MEM_Ephem)!=0 ? 1 : 0) +
64234      ((p->flags&MEM_Static)!=0 ? 1 : 0) == 1
64235    );
64236  }
64237  return 1;
64238}
64239#endif
64240
64241
64242/*
64243** If pMem is an object with a valid string representation, this routine
64244** ensures the internal encoding for the string representation is
64245** 'desiredEnc', one of SQLITE_UTF8, SQLITE_UTF16LE or SQLITE_UTF16BE.
64246**
64247** If pMem is not a string object, or the encoding of the string
64248** representation is already stored using the requested encoding, then this
64249** routine is a no-op.
64250**
64251** SQLITE_OK is returned if the conversion is successful (or not required).
64252** SQLITE_NOMEM may be returned if a malloc() fails during conversion
64253** between formats.
64254*/
64255SQLITE_PRIVATE int sqlite3VdbeChangeEncoding(Mem *pMem, int desiredEnc){
64256#ifndef SQLITE_OMIT_UTF16
64257  int rc;
64258#endif
64259  assert( (pMem->flags&MEM_RowSet)==0 );
64260  assert( desiredEnc==SQLITE_UTF8 || desiredEnc==SQLITE_UTF16LE
64261           || desiredEnc==SQLITE_UTF16BE );
64262  if( !(pMem->flags&MEM_Str) || pMem->enc==desiredEnc ){
64263    return SQLITE_OK;
64264  }
64265  assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
64266#ifdef SQLITE_OMIT_UTF16
64267  return SQLITE_ERROR;
64268#else
64269
64270  /* MemTranslate() may return SQLITE_OK or SQLITE_NOMEM. If NOMEM is returned,
64271  ** then the encoding of the value may not have changed.
64272  */
64273  rc = sqlite3VdbeMemTranslate(pMem, (u8)desiredEnc);
64274  assert(rc==SQLITE_OK    || rc==SQLITE_NOMEM);
64275  assert(rc==SQLITE_OK    || pMem->enc!=desiredEnc);
64276  assert(rc==SQLITE_NOMEM || pMem->enc==desiredEnc);
64277  return rc;
64278#endif
64279}
64280
64281/*
64282** Make sure pMem->z points to a writable allocation of at least
64283** min(n,32) bytes.
64284**
64285** If the bPreserve argument is true, then copy of the content of
64286** pMem->z into the new allocation.  pMem must be either a string or
64287** blob if bPreserve is true.  If bPreserve is false, any prior content
64288** in pMem->z is discarded.
64289*/
64290SQLITE_PRIVATE SQLITE_NOINLINE int sqlite3VdbeMemGrow(Mem *pMem, int n, int bPreserve){
64291  assert( sqlite3VdbeCheckMemInvariants(pMem) );
64292  assert( (pMem->flags&MEM_RowSet)==0 );
64293
64294  /* If the bPreserve flag is set to true, then the memory cell must already
64295  ** contain a valid string or blob value.  */
64296  assert( bPreserve==0 || pMem->flags&(MEM_Blob|MEM_Str) );
64297  testcase( bPreserve && pMem->z==0 );
64298
64299  assert( pMem->szMalloc==0
64300       || pMem->szMalloc==sqlite3DbMallocSize(pMem->db, pMem->zMalloc) );
64301  if( pMem->szMalloc<n ){
64302    if( n<32 ) n = 32;
64303    if( bPreserve && pMem->szMalloc>0 && pMem->z==pMem->zMalloc ){
64304      pMem->z = pMem->zMalloc = sqlite3DbReallocOrFree(pMem->db, pMem->z, n);
64305      bPreserve = 0;
64306    }else{
64307      if( pMem->szMalloc>0 ) sqlite3DbFree(pMem->db, pMem->zMalloc);
64308      pMem->zMalloc = sqlite3DbMallocRaw(pMem->db, n);
64309    }
64310    if( pMem->zMalloc==0 ){
64311      sqlite3VdbeMemSetNull(pMem);
64312      pMem->z = 0;
64313      pMem->szMalloc = 0;
64314      return SQLITE_NOMEM;
64315    }else{
64316      pMem->szMalloc = sqlite3DbMallocSize(pMem->db, pMem->zMalloc);
64317    }
64318  }
64319
64320  if( bPreserve && pMem->z && pMem->z!=pMem->zMalloc ){
64321    memcpy(pMem->zMalloc, pMem->z, pMem->n);
64322  }
64323  if( (pMem->flags&MEM_Dyn)!=0 ){
64324    assert( pMem->xDel!=0 && pMem->xDel!=SQLITE_DYNAMIC );
64325    pMem->xDel((void *)(pMem->z));
64326  }
64327
64328  pMem->z = pMem->zMalloc;
64329  pMem->flags &= ~(MEM_Dyn|MEM_Ephem|MEM_Static);
64330  return SQLITE_OK;
64331}
64332
64333/*
64334** Change the pMem->zMalloc allocation to be at least szNew bytes.
64335** If pMem->zMalloc already meets or exceeds the requested size, this
64336** routine is a no-op.
64337**
64338** Any prior string or blob content in the pMem object may be discarded.
64339** The pMem->xDel destructor is called, if it exists.  Though MEM_Str
64340** and MEM_Blob values may be discarded, MEM_Int, MEM_Real, and MEM_Null
64341** values are preserved.
64342**
64343** Return SQLITE_OK on success or an error code (probably SQLITE_NOMEM)
64344** if unable to complete the resizing.
64345*/
64346SQLITE_PRIVATE int sqlite3VdbeMemClearAndResize(Mem *pMem, int szNew){
64347  assert( szNew>0 );
64348  assert( (pMem->flags & MEM_Dyn)==0 || pMem->szMalloc==0 );
64349  if( pMem->szMalloc<szNew ){
64350    return sqlite3VdbeMemGrow(pMem, szNew, 0);
64351  }
64352  assert( (pMem->flags & MEM_Dyn)==0 );
64353  pMem->z = pMem->zMalloc;
64354  pMem->flags &= (MEM_Null|MEM_Int|MEM_Real);
64355  return SQLITE_OK;
64356}
64357
64358/*
64359** Change pMem so that its MEM_Str or MEM_Blob value is stored in
64360** MEM.zMalloc, where it can be safely written.
64361**
64362** Return SQLITE_OK on success or SQLITE_NOMEM if malloc fails.
64363*/
64364SQLITE_PRIVATE int sqlite3VdbeMemMakeWriteable(Mem *pMem){
64365  int f;
64366  assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
64367  assert( (pMem->flags&MEM_RowSet)==0 );
64368  ExpandBlob(pMem);
64369  f = pMem->flags;
64370  if( (f&(MEM_Str|MEM_Blob)) && (pMem->szMalloc==0 || pMem->z!=pMem->zMalloc) ){
64371    if( sqlite3VdbeMemGrow(pMem, pMem->n + 2, 1) ){
64372      return SQLITE_NOMEM;
64373    }
64374    pMem->z[pMem->n] = 0;
64375    pMem->z[pMem->n+1] = 0;
64376    pMem->flags |= MEM_Term;
64377  }
64378  pMem->flags &= ~MEM_Ephem;
64379#ifdef SQLITE_DEBUG
64380  pMem->pScopyFrom = 0;
64381#endif
64382
64383  return SQLITE_OK;
64384}
64385
64386/*
64387** If the given Mem* has a zero-filled tail, turn it into an ordinary
64388** blob stored in dynamically allocated space.
64389*/
64390#ifndef SQLITE_OMIT_INCRBLOB
64391SQLITE_PRIVATE int sqlite3VdbeMemExpandBlob(Mem *pMem){
64392  if( pMem->flags & MEM_Zero ){
64393    int nByte;
64394    assert( pMem->flags&MEM_Blob );
64395    assert( (pMem->flags&MEM_RowSet)==0 );
64396    assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
64397
64398    /* Set nByte to the number of bytes required to store the expanded blob. */
64399    nByte = pMem->n + pMem->u.nZero;
64400    if( nByte<=0 ){
64401      nByte = 1;
64402    }
64403    if( sqlite3VdbeMemGrow(pMem, nByte, 1) ){
64404      return SQLITE_NOMEM;
64405    }
64406
64407    memset(&pMem->z[pMem->n], 0, pMem->u.nZero);
64408    pMem->n += pMem->u.nZero;
64409    pMem->flags &= ~(MEM_Zero|MEM_Term);
64410  }
64411  return SQLITE_OK;
64412}
64413#endif
64414
64415/*
64416** It is already known that pMem contains an unterminated string.
64417** Add the zero terminator.
64418*/
64419static SQLITE_NOINLINE int vdbeMemAddTerminator(Mem *pMem){
64420  if( sqlite3VdbeMemGrow(pMem, pMem->n+2, 1) ){
64421    return SQLITE_NOMEM;
64422  }
64423  pMem->z[pMem->n] = 0;
64424  pMem->z[pMem->n+1] = 0;
64425  pMem->flags |= MEM_Term;
64426  return SQLITE_OK;
64427}
64428
64429/*
64430** Make sure the given Mem is \u0000 terminated.
64431*/
64432SQLITE_PRIVATE int sqlite3VdbeMemNulTerminate(Mem *pMem){
64433  assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
64434  testcase( (pMem->flags & (MEM_Term|MEM_Str))==(MEM_Term|MEM_Str) );
64435  testcase( (pMem->flags & (MEM_Term|MEM_Str))==0 );
64436  if( (pMem->flags & (MEM_Term|MEM_Str))!=MEM_Str ){
64437    return SQLITE_OK;   /* Nothing to do */
64438  }else{
64439    return vdbeMemAddTerminator(pMem);
64440  }
64441}
64442
64443/*
64444** Add MEM_Str to the set of representations for the given Mem.  Numbers
64445** are converted using sqlite3_snprintf().  Converting a BLOB to a string
64446** is a no-op.
64447**
64448** Existing representations MEM_Int and MEM_Real are invalidated if
64449** bForce is true but are retained if bForce is false.
64450**
64451** A MEM_Null value will never be passed to this function. This function is
64452** used for converting values to text for returning to the user (i.e. via
64453** sqlite3_value_text()), or for ensuring that values to be used as btree
64454** keys are strings. In the former case a NULL pointer is returned the
64455** user and the latter is an internal programming error.
64456*/
64457SQLITE_PRIVATE int sqlite3VdbeMemStringify(Mem *pMem, u8 enc, u8 bForce){
64458  int fg = pMem->flags;
64459  const int nByte = 32;
64460
64461  assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
64462  assert( !(fg&MEM_Zero) );
64463  assert( !(fg&(MEM_Str|MEM_Blob)) );
64464  assert( fg&(MEM_Int|MEM_Real) );
64465  assert( (pMem->flags&MEM_RowSet)==0 );
64466  assert( EIGHT_BYTE_ALIGNMENT(pMem) );
64467
64468
64469  if( sqlite3VdbeMemClearAndResize(pMem, nByte) ){
64470    return SQLITE_NOMEM;
64471  }
64472
64473  /* For a Real or Integer, use sqlite3_snprintf() to produce the UTF-8
64474  ** string representation of the value. Then, if the required encoding
64475  ** is UTF-16le or UTF-16be do a translation.
64476  **
64477  ** FIX ME: It would be better if sqlite3_snprintf() could do UTF-16.
64478  */
64479  if( fg & MEM_Int ){
64480    sqlite3_snprintf(nByte, pMem->z, "%lld", pMem->u.i);
64481  }else{
64482    assert( fg & MEM_Real );
64483    sqlite3_snprintf(nByte, pMem->z, "%!.15g", pMem->u.r);
64484  }
64485  pMem->n = sqlite3Strlen30(pMem->z);
64486  pMem->enc = SQLITE_UTF8;
64487  pMem->flags |= MEM_Str|MEM_Term;
64488  if( bForce ) pMem->flags &= ~(MEM_Int|MEM_Real);
64489  sqlite3VdbeChangeEncoding(pMem, enc);
64490  return SQLITE_OK;
64491}
64492
64493/*
64494** Memory cell pMem contains the context of an aggregate function.
64495** This routine calls the finalize method for that function.  The
64496** result of the aggregate is stored back into pMem.
64497**
64498** Return SQLITE_ERROR if the finalizer reports an error.  SQLITE_OK
64499** otherwise.
64500*/
64501SQLITE_PRIVATE int sqlite3VdbeMemFinalize(Mem *pMem, FuncDef *pFunc){
64502  int rc = SQLITE_OK;
64503  if( ALWAYS(pFunc && pFunc->xFinalize) ){
64504    sqlite3_context ctx;
64505    Mem t;
64506    assert( (pMem->flags & MEM_Null)!=0 || pFunc==pMem->u.pDef );
64507    assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
64508    memset(&ctx, 0, sizeof(ctx));
64509    memset(&t, 0, sizeof(t));
64510    t.flags = MEM_Null;
64511    t.db = pMem->db;
64512    ctx.pOut = &t;
64513    ctx.pMem = pMem;
64514    ctx.pFunc = pFunc;
64515    pFunc->xFinalize(&ctx); /* IMP: R-24505-23230 */
64516    assert( (pMem->flags & MEM_Dyn)==0 );
64517    if( pMem->szMalloc>0 ) sqlite3DbFree(pMem->db, pMem->zMalloc);
64518    memcpy(pMem, &t, sizeof(t));
64519    rc = ctx.isError;
64520  }
64521  return rc;
64522}
64523
64524/*
64525** If the memory cell contains a value that must be freed by
64526** invoking the external callback in Mem.xDel, then this routine
64527** will free that value.  It also sets Mem.flags to MEM_Null.
64528**
64529** This is a helper routine for sqlite3VdbeMemSetNull() and
64530** for sqlite3VdbeMemRelease().  Use those other routines as the
64531** entry point for releasing Mem resources.
64532*/
64533static SQLITE_NOINLINE void vdbeMemClearExternAndSetNull(Mem *p){
64534  assert( p->db==0 || sqlite3_mutex_held(p->db->mutex) );
64535  assert( VdbeMemDynamic(p) );
64536  if( p->flags&MEM_Agg ){
64537    sqlite3VdbeMemFinalize(p, p->u.pDef);
64538    assert( (p->flags & MEM_Agg)==0 );
64539    testcase( p->flags & MEM_Dyn );
64540  }
64541  if( p->flags&MEM_Dyn ){
64542    assert( (p->flags&MEM_RowSet)==0 );
64543    assert( p->xDel!=SQLITE_DYNAMIC && p->xDel!=0 );
64544    p->xDel((void *)p->z);
64545  }else if( p->flags&MEM_RowSet ){
64546    sqlite3RowSetClear(p->u.pRowSet);
64547  }else if( p->flags&MEM_Frame ){
64548    VdbeFrame *pFrame = p->u.pFrame;
64549    pFrame->pParent = pFrame->v->pDelFrame;
64550    pFrame->v->pDelFrame = pFrame;
64551  }
64552  p->flags = MEM_Null;
64553}
64554
64555/*
64556** Release memory held by the Mem p, both external memory cleared
64557** by p->xDel and memory in p->zMalloc.
64558**
64559** This is a helper routine invoked by sqlite3VdbeMemRelease() in
64560** the unusual case where there really is memory in p that needs
64561** to be freed.
64562*/
64563static SQLITE_NOINLINE void vdbeMemClear(Mem *p){
64564  if( VdbeMemDynamic(p) ){
64565    vdbeMemClearExternAndSetNull(p);
64566  }
64567  if( p->szMalloc ){
64568    sqlite3DbFree(p->db, p->zMalloc);
64569    p->szMalloc = 0;
64570  }
64571  p->z = 0;
64572}
64573
64574/*
64575** Release any memory resources held by the Mem.  Both the memory that is
64576** free by Mem.xDel and the Mem.zMalloc allocation are freed.
64577**
64578** Use this routine prior to clean up prior to abandoning a Mem, or to
64579** reset a Mem back to its minimum memory utilization.
64580**
64581** Use sqlite3VdbeMemSetNull() to release just the Mem.xDel space
64582** prior to inserting new content into the Mem.
64583*/
64584SQLITE_PRIVATE void sqlite3VdbeMemRelease(Mem *p){
64585  assert( sqlite3VdbeCheckMemInvariants(p) );
64586  if( VdbeMemDynamic(p) || p->szMalloc ){
64587    vdbeMemClear(p);
64588  }
64589}
64590
64591/*
64592** Convert a 64-bit IEEE double into a 64-bit signed integer.
64593** If the double is out of range of a 64-bit signed integer then
64594** return the closest available 64-bit signed integer.
64595*/
64596static i64 doubleToInt64(double r){
64597#ifdef SQLITE_OMIT_FLOATING_POINT
64598  /* When floating-point is omitted, double and int64 are the same thing */
64599  return r;
64600#else
64601  /*
64602  ** Many compilers we encounter do not define constants for the
64603  ** minimum and maximum 64-bit integers, or they define them
64604  ** inconsistently.  And many do not understand the "LL" notation.
64605  ** So we define our own static constants here using nothing
64606  ** larger than a 32-bit integer constant.
64607  */
64608  static const i64 maxInt = LARGEST_INT64;
64609  static const i64 minInt = SMALLEST_INT64;
64610
64611  if( r<=(double)minInt ){
64612    return minInt;
64613  }else if( r>=(double)maxInt ){
64614    return maxInt;
64615  }else{
64616    return (i64)r;
64617  }
64618#endif
64619}
64620
64621/*
64622** Return some kind of integer value which is the best we can do
64623** at representing the value that *pMem describes as an integer.
64624** If pMem is an integer, then the value is exact.  If pMem is
64625** a floating-point then the value returned is the integer part.
64626** If pMem is a string or blob, then we make an attempt to convert
64627** it into an integer and return that.  If pMem represents an
64628** an SQL-NULL value, return 0.
64629**
64630** If pMem represents a string value, its encoding might be changed.
64631*/
64632SQLITE_PRIVATE i64 sqlite3VdbeIntValue(Mem *pMem){
64633  int flags;
64634  assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
64635  assert( EIGHT_BYTE_ALIGNMENT(pMem) );
64636  flags = pMem->flags;
64637  if( flags & MEM_Int ){
64638    return pMem->u.i;
64639  }else if( flags & MEM_Real ){
64640    return doubleToInt64(pMem->u.r);
64641  }else if( flags & (MEM_Str|MEM_Blob) ){
64642    i64 value = 0;
64643    assert( pMem->z || pMem->n==0 );
64644    sqlite3Atoi64(pMem->z, &value, pMem->n, pMem->enc);
64645    return value;
64646  }else{
64647    return 0;
64648  }
64649}
64650
64651/*
64652** Return the best representation of pMem that we can get into a
64653** double.  If pMem is already a double or an integer, return its
64654** value.  If it is a string or blob, try to convert it to a double.
64655** If it is a NULL, return 0.0.
64656*/
64657SQLITE_PRIVATE double sqlite3VdbeRealValue(Mem *pMem){
64658  assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
64659  assert( EIGHT_BYTE_ALIGNMENT(pMem) );
64660  if( pMem->flags & MEM_Real ){
64661    return pMem->u.r;
64662  }else if( pMem->flags & MEM_Int ){
64663    return (double)pMem->u.i;
64664  }else if( pMem->flags & (MEM_Str|MEM_Blob) ){
64665    /* (double)0 In case of SQLITE_OMIT_FLOATING_POINT... */
64666    double val = (double)0;
64667    sqlite3AtoF(pMem->z, &val, pMem->n, pMem->enc);
64668    return val;
64669  }else{
64670    /* (double)0 In case of SQLITE_OMIT_FLOATING_POINT... */
64671    return (double)0;
64672  }
64673}
64674
64675/*
64676** The MEM structure is already a MEM_Real.  Try to also make it a
64677** MEM_Int if we can.
64678*/
64679SQLITE_PRIVATE void sqlite3VdbeIntegerAffinity(Mem *pMem){
64680  i64 ix;
64681  assert( pMem->flags & MEM_Real );
64682  assert( (pMem->flags & MEM_RowSet)==0 );
64683  assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
64684  assert( EIGHT_BYTE_ALIGNMENT(pMem) );
64685
64686  ix = doubleToInt64(pMem->u.r);
64687
64688  /* Only mark the value as an integer if
64689  **
64690  **    (1) the round-trip conversion real->int->real is a no-op, and
64691  **    (2) The integer is neither the largest nor the smallest
64692  **        possible integer (ticket #3922)
64693  **
64694  ** The second and third terms in the following conditional enforces
64695  ** the second condition under the assumption that addition overflow causes
64696  ** values to wrap around.
64697  */
64698  if( pMem->u.r==ix && ix>SMALLEST_INT64 && ix<LARGEST_INT64 ){
64699    pMem->u.i = ix;
64700    MemSetTypeFlag(pMem, MEM_Int);
64701  }
64702}
64703
64704/*
64705** Convert pMem to type integer.  Invalidate any prior representations.
64706*/
64707SQLITE_PRIVATE int sqlite3VdbeMemIntegerify(Mem *pMem){
64708  assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
64709  assert( (pMem->flags & MEM_RowSet)==0 );
64710  assert( EIGHT_BYTE_ALIGNMENT(pMem) );
64711
64712  pMem->u.i = sqlite3VdbeIntValue(pMem);
64713  MemSetTypeFlag(pMem, MEM_Int);
64714  return SQLITE_OK;
64715}
64716
64717/*
64718** Convert pMem so that it is of type MEM_Real.
64719** Invalidate any prior representations.
64720*/
64721SQLITE_PRIVATE int sqlite3VdbeMemRealify(Mem *pMem){
64722  assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
64723  assert( EIGHT_BYTE_ALIGNMENT(pMem) );
64724
64725  pMem->u.r = sqlite3VdbeRealValue(pMem);
64726  MemSetTypeFlag(pMem, MEM_Real);
64727  return SQLITE_OK;
64728}
64729
64730/*
64731** Convert pMem so that it has types MEM_Real or MEM_Int or both.
64732** Invalidate any prior representations.
64733**
64734** Every effort is made to force the conversion, even if the input
64735** is a string that does not look completely like a number.  Convert
64736** as much of the string as we can and ignore the rest.
64737*/
64738SQLITE_PRIVATE int sqlite3VdbeMemNumerify(Mem *pMem){
64739  if( (pMem->flags & (MEM_Int|MEM_Real|MEM_Null))==0 ){
64740    assert( (pMem->flags & (MEM_Blob|MEM_Str))!=0 );
64741    assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
64742    if( 0==sqlite3Atoi64(pMem->z, &pMem->u.i, pMem->n, pMem->enc) ){
64743      MemSetTypeFlag(pMem, MEM_Int);
64744    }else{
64745      pMem->u.r = sqlite3VdbeRealValue(pMem);
64746      MemSetTypeFlag(pMem, MEM_Real);
64747      sqlite3VdbeIntegerAffinity(pMem);
64748    }
64749  }
64750  assert( (pMem->flags & (MEM_Int|MEM_Real|MEM_Null))!=0 );
64751  pMem->flags &= ~(MEM_Str|MEM_Blob);
64752  return SQLITE_OK;
64753}
64754
64755/*
64756** Cast the datatype of the value in pMem according to the affinity
64757** "aff".  Casting is different from applying affinity in that a cast
64758** is forced.  In other words, the value is converted into the desired
64759** affinity even if that results in loss of data.  This routine is
64760** used (for example) to implement the SQL "cast()" operator.
64761*/
64762SQLITE_PRIVATE void sqlite3VdbeMemCast(Mem *pMem, u8 aff, u8 encoding){
64763  if( pMem->flags & MEM_Null ) return;
64764  switch( aff ){
64765    case SQLITE_AFF_BLOB: {   /* Really a cast to BLOB */
64766      if( (pMem->flags & MEM_Blob)==0 ){
64767        sqlite3ValueApplyAffinity(pMem, SQLITE_AFF_TEXT, encoding);
64768        assert( pMem->flags & MEM_Str || pMem->db->mallocFailed );
64769        MemSetTypeFlag(pMem, MEM_Blob);
64770      }else{
64771        pMem->flags &= ~(MEM_TypeMask&~MEM_Blob);
64772      }
64773      break;
64774    }
64775    case SQLITE_AFF_NUMERIC: {
64776      sqlite3VdbeMemNumerify(pMem);
64777      break;
64778    }
64779    case SQLITE_AFF_INTEGER: {
64780      sqlite3VdbeMemIntegerify(pMem);
64781      break;
64782    }
64783    case SQLITE_AFF_REAL: {
64784      sqlite3VdbeMemRealify(pMem);
64785      break;
64786    }
64787    default: {
64788      assert( aff==SQLITE_AFF_TEXT );
64789      assert( MEM_Str==(MEM_Blob>>3) );
64790      pMem->flags |= (pMem->flags&MEM_Blob)>>3;
64791      sqlite3ValueApplyAffinity(pMem, SQLITE_AFF_TEXT, encoding);
64792      assert( pMem->flags & MEM_Str || pMem->db->mallocFailed );
64793      pMem->flags &= ~(MEM_Int|MEM_Real|MEM_Blob|MEM_Zero);
64794      break;
64795    }
64796  }
64797}
64798
64799/*
64800** Initialize bulk memory to be a consistent Mem object.
64801**
64802** The minimum amount of initialization feasible is performed.
64803*/
64804SQLITE_PRIVATE void sqlite3VdbeMemInit(Mem *pMem, sqlite3 *db, u16 flags){
64805  assert( (flags & ~MEM_TypeMask)==0 );
64806  pMem->flags = flags;
64807  pMem->db = db;
64808  pMem->szMalloc = 0;
64809}
64810
64811
64812/*
64813** Delete any previous value and set the value stored in *pMem to NULL.
64814**
64815** This routine calls the Mem.xDel destructor to dispose of values that
64816** require the destructor.  But it preserves the Mem.zMalloc memory allocation.
64817** To free all resources, use sqlite3VdbeMemRelease(), which both calls this
64818** routine to invoke the destructor and deallocates Mem.zMalloc.
64819**
64820** Use this routine to reset the Mem prior to insert a new value.
64821**
64822** Use sqlite3VdbeMemRelease() to complete erase the Mem prior to abandoning it.
64823*/
64824SQLITE_PRIVATE void sqlite3VdbeMemSetNull(Mem *pMem){
64825  if( VdbeMemDynamic(pMem) ){
64826    vdbeMemClearExternAndSetNull(pMem);
64827  }else{
64828    pMem->flags = MEM_Null;
64829  }
64830}
64831SQLITE_PRIVATE void sqlite3ValueSetNull(sqlite3_value *p){
64832  sqlite3VdbeMemSetNull((Mem*)p);
64833}
64834
64835/*
64836** Delete any previous value and set the value to be a BLOB of length
64837** n containing all zeros.
64838*/
64839SQLITE_PRIVATE void sqlite3VdbeMemSetZeroBlob(Mem *pMem, int n){
64840  sqlite3VdbeMemRelease(pMem);
64841  pMem->flags = MEM_Blob|MEM_Zero;
64842  pMem->n = 0;
64843  if( n<0 ) n = 0;
64844  pMem->u.nZero = n;
64845  pMem->enc = SQLITE_UTF8;
64846  pMem->z = 0;
64847}
64848
64849/*
64850** The pMem is known to contain content that needs to be destroyed prior
64851** to a value change.  So invoke the destructor, then set the value to
64852** a 64-bit integer.
64853*/
64854static SQLITE_NOINLINE void vdbeReleaseAndSetInt64(Mem *pMem, i64 val){
64855  sqlite3VdbeMemSetNull(pMem);
64856  pMem->u.i = val;
64857  pMem->flags = MEM_Int;
64858}
64859
64860/*
64861** Delete any previous value and set the value stored in *pMem to val,
64862** manifest type INTEGER.
64863*/
64864SQLITE_PRIVATE void sqlite3VdbeMemSetInt64(Mem *pMem, i64 val){
64865  if( VdbeMemDynamic(pMem) ){
64866    vdbeReleaseAndSetInt64(pMem, val);
64867  }else{
64868    pMem->u.i = val;
64869    pMem->flags = MEM_Int;
64870  }
64871}
64872
64873#ifndef SQLITE_OMIT_FLOATING_POINT
64874/*
64875** Delete any previous value and set the value stored in *pMem to val,
64876** manifest type REAL.
64877*/
64878SQLITE_PRIVATE void sqlite3VdbeMemSetDouble(Mem *pMem, double val){
64879  sqlite3VdbeMemSetNull(pMem);
64880  if( !sqlite3IsNaN(val) ){
64881    pMem->u.r = val;
64882    pMem->flags = MEM_Real;
64883  }
64884}
64885#endif
64886
64887/*
64888** Delete any previous value and set the value of pMem to be an
64889** empty boolean index.
64890*/
64891SQLITE_PRIVATE void sqlite3VdbeMemSetRowSet(Mem *pMem){
64892  sqlite3 *db = pMem->db;
64893  assert( db!=0 );
64894  assert( (pMem->flags & MEM_RowSet)==0 );
64895  sqlite3VdbeMemRelease(pMem);
64896  pMem->zMalloc = sqlite3DbMallocRaw(db, 64);
64897  if( db->mallocFailed ){
64898    pMem->flags = MEM_Null;
64899    pMem->szMalloc = 0;
64900  }else{
64901    assert( pMem->zMalloc );
64902    pMem->szMalloc = sqlite3DbMallocSize(db, pMem->zMalloc);
64903    pMem->u.pRowSet = sqlite3RowSetInit(db, pMem->zMalloc, pMem->szMalloc);
64904    assert( pMem->u.pRowSet!=0 );
64905    pMem->flags = MEM_RowSet;
64906  }
64907}
64908
64909/*
64910** Return true if the Mem object contains a TEXT or BLOB that is
64911** too large - whose size exceeds SQLITE_MAX_LENGTH.
64912*/
64913SQLITE_PRIVATE int sqlite3VdbeMemTooBig(Mem *p){
64914  assert( p->db!=0 );
64915  if( p->flags & (MEM_Str|MEM_Blob) ){
64916    int n = p->n;
64917    if( p->flags & MEM_Zero ){
64918      n += p->u.nZero;
64919    }
64920    return n>p->db->aLimit[SQLITE_LIMIT_LENGTH];
64921  }
64922  return 0;
64923}
64924
64925#ifdef SQLITE_DEBUG
64926/*
64927** This routine prepares a memory cell for modification by breaking
64928** its link to a shallow copy and by marking any current shallow
64929** copies of this cell as invalid.
64930**
64931** This is used for testing and debugging only - to make sure shallow
64932** copies are not misused.
64933*/
64934SQLITE_PRIVATE void sqlite3VdbeMemAboutToChange(Vdbe *pVdbe, Mem *pMem){
64935  int i;
64936  Mem *pX;
64937  for(i=1, pX=&pVdbe->aMem[1]; i<=pVdbe->nMem; i++, pX++){
64938    if( pX->pScopyFrom==pMem ){
64939      pX->flags |= MEM_Undefined;
64940      pX->pScopyFrom = 0;
64941    }
64942  }
64943  pMem->pScopyFrom = 0;
64944}
64945#endif /* SQLITE_DEBUG */
64946
64947
64948/*
64949** Make an shallow copy of pFrom into pTo.  Prior contents of
64950** pTo are freed.  The pFrom->z field is not duplicated.  If
64951** pFrom->z is used, then pTo->z points to the same thing as pFrom->z
64952** and flags gets srcType (either MEM_Ephem or MEM_Static).
64953*/
64954static SQLITE_NOINLINE void vdbeClrCopy(Mem *pTo, const Mem *pFrom, int eType){
64955  vdbeMemClearExternAndSetNull(pTo);
64956  assert( !VdbeMemDynamic(pTo) );
64957  sqlite3VdbeMemShallowCopy(pTo, pFrom, eType);
64958}
64959SQLITE_PRIVATE void sqlite3VdbeMemShallowCopy(Mem *pTo, const Mem *pFrom, int srcType){
64960  assert( (pFrom->flags & MEM_RowSet)==0 );
64961  assert( pTo->db==pFrom->db );
64962  if( VdbeMemDynamic(pTo) ){ vdbeClrCopy(pTo,pFrom,srcType); return; }
64963  memcpy(pTo, pFrom, MEMCELLSIZE);
64964  if( (pFrom->flags&MEM_Static)==0 ){
64965    pTo->flags &= ~(MEM_Dyn|MEM_Static|MEM_Ephem);
64966    assert( srcType==MEM_Ephem || srcType==MEM_Static );
64967    pTo->flags |= srcType;
64968  }
64969}
64970
64971/*
64972** Make a full copy of pFrom into pTo.  Prior contents of pTo are
64973** freed before the copy is made.
64974*/
64975SQLITE_PRIVATE int sqlite3VdbeMemCopy(Mem *pTo, const Mem *pFrom){
64976  int rc = SQLITE_OK;
64977
64978  /* The pFrom==0 case in the following assert() is when an sqlite3_value
64979  ** from sqlite3_value_dup() is used as the argument
64980  ** to sqlite3_result_value(). */
64981  assert( pTo->db==pFrom->db || pFrom->db==0 );
64982  assert( (pFrom->flags & MEM_RowSet)==0 );
64983  if( VdbeMemDynamic(pTo) ) vdbeMemClearExternAndSetNull(pTo);
64984  memcpy(pTo, pFrom, MEMCELLSIZE);
64985  pTo->flags &= ~MEM_Dyn;
64986  if( pTo->flags&(MEM_Str|MEM_Blob) ){
64987    if( 0==(pFrom->flags&MEM_Static) ){
64988      pTo->flags |= MEM_Ephem;
64989      rc = sqlite3VdbeMemMakeWriteable(pTo);
64990    }
64991  }
64992
64993  return rc;
64994}
64995
64996/*
64997** Transfer the contents of pFrom to pTo. Any existing value in pTo is
64998** freed. If pFrom contains ephemeral data, a copy is made.
64999**
65000** pFrom contains an SQL NULL when this routine returns.
65001*/
65002SQLITE_PRIVATE void sqlite3VdbeMemMove(Mem *pTo, Mem *pFrom){
65003  assert( pFrom->db==0 || sqlite3_mutex_held(pFrom->db->mutex) );
65004  assert( pTo->db==0 || sqlite3_mutex_held(pTo->db->mutex) );
65005  assert( pFrom->db==0 || pTo->db==0 || pFrom->db==pTo->db );
65006
65007  sqlite3VdbeMemRelease(pTo);
65008  memcpy(pTo, pFrom, sizeof(Mem));
65009  pFrom->flags = MEM_Null;
65010  pFrom->szMalloc = 0;
65011}
65012
65013/*
65014** Change the value of a Mem to be a string or a BLOB.
65015**
65016** The memory management strategy depends on the value of the xDel
65017** parameter. If the value passed is SQLITE_TRANSIENT, then the
65018** string is copied into a (possibly existing) buffer managed by the
65019** Mem structure. Otherwise, any existing buffer is freed and the
65020** pointer copied.
65021**
65022** If the string is too large (if it exceeds the SQLITE_LIMIT_LENGTH
65023** size limit) then no memory allocation occurs.  If the string can be
65024** stored without allocating memory, then it is.  If a memory allocation
65025** is required to store the string, then value of pMem is unchanged.  In
65026** either case, SQLITE_TOOBIG is returned.
65027*/
65028SQLITE_PRIVATE int sqlite3VdbeMemSetStr(
65029  Mem *pMem,          /* Memory cell to set to string value */
65030  const char *z,      /* String pointer */
65031  int n,              /* Bytes in string, or negative */
65032  u8 enc,             /* Encoding of z.  0 for BLOBs */
65033  void (*xDel)(void*) /* Destructor function */
65034){
65035  int nByte = n;      /* New value for pMem->n */
65036  int iLimit;         /* Maximum allowed string or blob size */
65037  u16 flags = 0;      /* New value for pMem->flags */
65038
65039  assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
65040  assert( (pMem->flags & MEM_RowSet)==0 );
65041
65042  /* If z is a NULL pointer, set pMem to contain an SQL NULL. */
65043  if( !z ){
65044    sqlite3VdbeMemSetNull(pMem);
65045    return SQLITE_OK;
65046  }
65047
65048  if( pMem->db ){
65049    iLimit = pMem->db->aLimit[SQLITE_LIMIT_LENGTH];
65050  }else{
65051    iLimit = SQLITE_MAX_LENGTH;
65052  }
65053  flags = (enc==0?MEM_Blob:MEM_Str);
65054  if( nByte<0 ){
65055    assert( enc!=0 );
65056    if( enc==SQLITE_UTF8 ){
65057      nByte = sqlite3Strlen30(z);
65058      if( nByte>iLimit ) nByte = iLimit+1;
65059    }else{
65060      for(nByte=0; nByte<=iLimit && (z[nByte] | z[nByte+1]); nByte+=2){}
65061    }
65062    flags |= MEM_Term;
65063  }
65064
65065  /* The following block sets the new values of Mem.z and Mem.xDel. It
65066  ** also sets a flag in local variable "flags" to indicate the memory
65067  ** management (one of MEM_Dyn or MEM_Static).
65068  */
65069  if( xDel==SQLITE_TRANSIENT ){
65070    int nAlloc = nByte;
65071    if( flags&MEM_Term ){
65072      nAlloc += (enc==SQLITE_UTF8?1:2);
65073    }
65074    if( nByte>iLimit ){
65075      return SQLITE_TOOBIG;
65076    }
65077    testcase( nAlloc==0 );
65078    testcase( nAlloc==31 );
65079    testcase( nAlloc==32 );
65080    if( sqlite3VdbeMemClearAndResize(pMem, MAX(nAlloc,32)) ){
65081      return SQLITE_NOMEM;
65082    }
65083    memcpy(pMem->z, z, nAlloc);
65084  }else if( xDel==SQLITE_DYNAMIC ){
65085    sqlite3VdbeMemRelease(pMem);
65086    pMem->zMalloc = pMem->z = (char *)z;
65087    pMem->szMalloc = sqlite3DbMallocSize(pMem->db, pMem->zMalloc);
65088  }else{
65089    sqlite3VdbeMemRelease(pMem);
65090    pMem->z = (char *)z;
65091    pMem->xDel = xDel;
65092    flags |= ((xDel==SQLITE_STATIC)?MEM_Static:MEM_Dyn);
65093  }
65094
65095  pMem->n = nByte;
65096  pMem->flags = flags;
65097  pMem->enc = (enc==0 ? SQLITE_UTF8 : enc);
65098
65099#ifndef SQLITE_OMIT_UTF16
65100  if( pMem->enc!=SQLITE_UTF8 && sqlite3VdbeMemHandleBom(pMem) ){
65101    return SQLITE_NOMEM;
65102  }
65103#endif
65104
65105  if( nByte>iLimit ){
65106    return SQLITE_TOOBIG;
65107  }
65108
65109  return SQLITE_OK;
65110}
65111
65112/*
65113** Move data out of a btree key or data field and into a Mem structure.
65114** The data or key is taken from the entry that pCur is currently pointing
65115** to.  offset and amt determine what portion of the data or key to retrieve.
65116** key is true to get the key or false to get data.  The result is written
65117** into the pMem element.
65118**
65119** The pMem object must have been initialized.  This routine will use
65120** pMem->zMalloc to hold the content from the btree, if possible.  New
65121** pMem->zMalloc space will be allocated if necessary.  The calling routine
65122** is responsible for making sure that the pMem object is eventually
65123** destroyed.
65124**
65125** If this routine fails for any reason (malloc returns NULL or unable
65126** to read from the disk) then the pMem is left in an inconsistent state.
65127*/
65128static SQLITE_NOINLINE int vdbeMemFromBtreeResize(
65129  BtCursor *pCur,   /* Cursor pointing at record to retrieve. */
65130  u32 offset,       /* Offset from the start of data to return bytes from. */
65131  u32 amt,          /* Number of bytes to return. */
65132  int key,          /* If true, retrieve from the btree key, not data. */
65133  Mem *pMem         /* OUT: Return data in this Mem structure. */
65134){
65135  int rc;
65136  pMem->flags = MEM_Null;
65137  if( SQLITE_OK==(rc = sqlite3VdbeMemClearAndResize(pMem, amt+2)) ){
65138    if( key ){
65139      rc = sqlite3BtreeKey(pCur, offset, amt, pMem->z);
65140    }else{
65141      rc = sqlite3BtreeData(pCur, offset, amt, pMem->z);
65142    }
65143    if( rc==SQLITE_OK ){
65144      pMem->z[amt] = 0;
65145      pMem->z[amt+1] = 0;
65146      pMem->flags = MEM_Blob|MEM_Term;
65147      pMem->n = (int)amt;
65148    }else{
65149      sqlite3VdbeMemRelease(pMem);
65150    }
65151  }
65152  return rc;
65153}
65154SQLITE_PRIVATE int sqlite3VdbeMemFromBtree(
65155  BtCursor *pCur,   /* Cursor pointing at record to retrieve. */
65156  u32 offset,       /* Offset from the start of data to return bytes from. */
65157  u32 amt,          /* Number of bytes to return. */
65158  int key,          /* If true, retrieve from the btree key, not data. */
65159  Mem *pMem         /* OUT: Return data in this Mem structure. */
65160){
65161  char *zData;        /* Data from the btree layer */
65162  u32 available = 0;  /* Number of bytes available on the local btree page */
65163  int rc = SQLITE_OK; /* Return code */
65164
65165  assert( sqlite3BtreeCursorIsValid(pCur) );
65166  assert( !VdbeMemDynamic(pMem) );
65167
65168  /* Note: the calls to BtreeKeyFetch() and DataFetch() below assert()
65169  ** that both the BtShared and database handle mutexes are held. */
65170  assert( (pMem->flags & MEM_RowSet)==0 );
65171  if( key ){
65172    zData = (char *)sqlite3BtreeKeyFetch(pCur, &available);
65173  }else{
65174    zData = (char *)sqlite3BtreeDataFetch(pCur, &available);
65175  }
65176  assert( zData!=0 );
65177
65178  if( offset+amt<=available ){
65179    pMem->z = &zData[offset];
65180    pMem->flags = MEM_Blob|MEM_Ephem;
65181    pMem->n = (int)amt;
65182  }else{
65183    rc = vdbeMemFromBtreeResize(pCur, offset, amt, key, pMem);
65184  }
65185
65186  return rc;
65187}
65188
65189/*
65190** The pVal argument is known to be a value other than NULL.
65191** Convert it into a string with encoding enc and return a pointer
65192** to a zero-terminated version of that string.
65193*/
65194static SQLITE_NOINLINE const void *valueToText(sqlite3_value* pVal, u8 enc){
65195  assert( pVal!=0 );
65196  assert( pVal->db==0 || sqlite3_mutex_held(pVal->db->mutex) );
65197  assert( (enc&3)==(enc&~SQLITE_UTF16_ALIGNED) );
65198  assert( (pVal->flags & MEM_RowSet)==0 );
65199  assert( (pVal->flags & (MEM_Null))==0 );
65200  if( pVal->flags & (MEM_Blob|MEM_Str) ){
65201    pVal->flags |= MEM_Str;
65202    if( pVal->flags & MEM_Zero ){
65203      sqlite3VdbeMemExpandBlob(pVal);
65204    }
65205    if( pVal->enc != (enc & ~SQLITE_UTF16_ALIGNED) ){
65206      sqlite3VdbeChangeEncoding(pVal, enc & ~SQLITE_UTF16_ALIGNED);
65207    }
65208    if( (enc & SQLITE_UTF16_ALIGNED)!=0 && 1==(1&SQLITE_PTR_TO_INT(pVal->z)) ){
65209      assert( (pVal->flags & (MEM_Ephem|MEM_Static))!=0 );
65210      if( sqlite3VdbeMemMakeWriteable(pVal)!=SQLITE_OK ){
65211        return 0;
65212      }
65213    }
65214    sqlite3VdbeMemNulTerminate(pVal); /* IMP: R-31275-44060 */
65215  }else{
65216    sqlite3VdbeMemStringify(pVal, enc, 0);
65217    assert( 0==(1&SQLITE_PTR_TO_INT(pVal->z)) );
65218  }
65219  assert(pVal->enc==(enc & ~SQLITE_UTF16_ALIGNED) || pVal->db==0
65220              || pVal->db->mallocFailed );
65221  if( pVal->enc==(enc & ~SQLITE_UTF16_ALIGNED) ){
65222    return pVal->z;
65223  }else{
65224    return 0;
65225  }
65226}
65227
65228/* This function is only available internally, it is not part of the
65229** external API. It works in a similar way to sqlite3_value_text(),
65230** except the data returned is in the encoding specified by the second
65231** parameter, which must be one of SQLITE_UTF16BE, SQLITE_UTF16LE or
65232** SQLITE_UTF8.
65233**
65234** (2006-02-16:)  The enc value can be or-ed with SQLITE_UTF16_ALIGNED.
65235** If that is the case, then the result must be aligned on an even byte
65236** boundary.
65237*/
65238SQLITE_PRIVATE const void *sqlite3ValueText(sqlite3_value* pVal, u8 enc){
65239  if( !pVal ) return 0;
65240  assert( pVal->db==0 || sqlite3_mutex_held(pVal->db->mutex) );
65241  assert( (enc&3)==(enc&~SQLITE_UTF16_ALIGNED) );
65242  assert( (pVal->flags & MEM_RowSet)==0 );
65243  if( (pVal->flags&(MEM_Str|MEM_Term))==(MEM_Str|MEM_Term) && pVal->enc==enc ){
65244    return pVal->z;
65245  }
65246  if( pVal->flags&MEM_Null ){
65247    return 0;
65248  }
65249  return valueToText(pVal, enc);
65250}
65251
65252/*
65253** Create a new sqlite3_value object.
65254*/
65255SQLITE_PRIVATE sqlite3_value *sqlite3ValueNew(sqlite3 *db){
65256  Mem *p = sqlite3DbMallocZero(db, sizeof(*p));
65257  if( p ){
65258    p->flags = MEM_Null;
65259    p->db = db;
65260  }
65261  return p;
65262}
65263
65264/*
65265** Context object passed by sqlite3Stat4ProbeSetValue() through to
65266** valueNew(). See comments above valueNew() for details.
65267*/
65268struct ValueNewStat4Ctx {
65269  Parse *pParse;
65270  Index *pIdx;
65271  UnpackedRecord **ppRec;
65272  int iVal;
65273};
65274
65275/*
65276** Allocate and return a pointer to a new sqlite3_value object. If
65277** the second argument to this function is NULL, the object is allocated
65278** by calling sqlite3ValueNew().
65279**
65280** Otherwise, if the second argument is non-zero, then this function is
65281** being called indirectly by sqlite3Stat4ProbeSetValue(). If it has not
65282** already been allocated, allocate the UnpackedRecord structure that
65283** that function will return to its caller here. Then return a pointer to
65284** an sqlite3_value within the UnpackedRecord.a[] array.
65285*/
65286static sqlite3_value *valueNew(sqlite3 *db, struct ValueNewStat4Ctx *p){
65287#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
65288  if( p ){
65289    UnpackedRecord *pRec = p->ppRec[0];
65290
65291    if( pRec==0 ){
65292      Index *pIdx = p->pIdx;      /* Index being probed */
65293      int nByte;                  /* Bytes of space to allocate */
65294      int i;                      /* Counter variable */
65295      int nCol = pIdx->nColumn;   /* Number of index columns including rowid */
65296
65297      nByte = sizeof(Mem) * nCol + ROUND8(sizeof(UnpackedRecord));
65298      pRec = (UnpackedRecord*)sqlite3DbMallocZero(db, nByte);
65299      if( pRec ){
65300        pRec->pKeyInfo = sqlite3KeyInfoOfIndex(p->pParse, pIdx);
65301        if( pRec->pKeyInfo ){
65302          assert( pRec->pKeyInfo->nField+pRec->pKeyInfo->nXField==nCol );
65303          assert( pRec->pKeyInfo->enc==ENC(db) );
65304          pRec->aMem = (Mem *)((u8*)pRec + ROUND8(sizeof(UnpackedRecord)));
65305          for(i=0; i<nCol; i++){
65306            pRec->aMem[i].flags = MEM_Null;
65307            pRec->aMem[i].db = db;
65308          }
65309        }else{
65310          sqlite3DbFree(db, pRec);
65311          pRec = 0;
65312        }
65313      }
65314      if( pRec==0 ) return 0;
65315      p->ppRec[0] = pRec;
65316    }
65317
65318    pRec->nField = p->iVal+1;
65319    return &pRec->aMem[p->iVal];
65320  }
65321#else
65322  UNUSED_PARAMETER(p);
65323#endif /* defined(SQLITE_ENABLE_STAT3_OR_STAT4) */
65324  return sqlite3ValueNew(db);
65325}
65326
65327/*
65328** The expression object indicated by the second argument is guaranteed
65329** to be a scalar SQL function. If
65330**
65331**   * all function arguments are SQL literals,
65332**   * the SQLITE_FUNC_CONSTANT function flag is set, and
65333**   * the SQLITE_FUNC_NEEDCOLL function flag is not set,
65334**
65335** then this routine attempts to invoke the SQL function. Assuming no
65336** error occurs, output parameter (*ppVal) is set to point to a value
65337** object containing the result before returning SQLITE_OK.
65338**
65339** Affinity aff is applied to the result of the function before returning.
65340** If the result is a text value, the sqlite3_value object uses encoding
65341** enc.
65342**
65343** If the conditions above are not met, this function returns SQLITE_OK
65344** and sets (*ppVal) to NULL. Or, if an error occurs, (*ppVal) is set to
65345** NULL and an SQLite error code returned.
65346*/
65347#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
65348static int valueFromFunction(
65349  sqlite3 *db,                    /* The database connection */
65350  Expr *p,                        /* The expression to evaluate */
65351  u8 enc,                         /* Encoding to use */
65352  u8 aff,                         /* Affinity to use */
65353  sqlite3_value **ppVal,          /* Write the new value here */
65354  struct ValueNewStat4Ctx *pCtx   /* Second argument for valueNew() */
65355){
65356  sqlite3_context ctx;            /* Context object for function invocation */
65357  sqlite3_value **apVal = 0;      /* Function arguments */
65358  int nVal = 0;                   /* Size of apVal[] array */
65359  FuncDef *pFunc = 0;             /* Function definition */
65360  sqlite3_value *pVal = 0;        /* New value */
65361  int rc = SQLITE_OK;             /* Return code */
65362  int nName;                      /* Size of function name in bytes */
65363  ExprList *pList = 0;            /* Function arguments */
65364  int i;                          /* Iterator variable */
65365
65366  assert( pCtx!=0 );
65367  assert( (p->flags & EP_TokenOnly)==0 );
65368  pList = p->x.pList;
65369  if( pList ) nVal = pList->nExpr;
65370  nName = sqlite3Strlen30(p->u.zToken);
65371  pFunc = sqlite3FindFunction(db, p->u.zToken, nName, nVal, enc, 0);
65372  assert( pFunc );
65373  if( (pFunc->funcFlags & SQLITE_FUNC_CONSTANT)==0
65374   || (pFunc->funcFlags & SQLITE_FUNC_NEEDCOLL)
65375  ){
65376    return SQLITE_OK;
65377  }
65378
65379  if( pList ){
65380    apVal = (sqlite3_value**)sqlite3DbMallocZero(db, sizeof(apVal[0]) * nVal);
65381    if( apVal==0 ){
65382      rc = SQLITE_NOMEM;
65383      goto value_from_function_out;
65384    }
65385    for(i=0; i<nVal; i++){
65386      rc = sqlite3ValueFromExpr(db, pList->a[i].pExpr, enc, aff, &apVal[i]);
65387      if( apVal[i]==0 || rc!=SQLITE_OK ) goto value_from_function_out;
65388    }
65389  }
65390
65391  pVal = valueNew(db, pCtx);
65392  if( pVal==0 ){
65393    rc = SQLITE_NOMEM;
65394    goto value_from_function_out;
65395  }
65396
65397  assert( pCtx->pParse->rc==SQLITE_OK );
65398  memset(&ctx, 0, sizeof(ctx));
65399  ctx.pOut = pVal;
65400  ctx.pFunc = pFunc;
65401  pFunc->xFunc(&ctx, nVal, apVal);
65402  if( ctx.isError ){
65403    rc = ctx.isError;
65404    sqlite3ErrorMsg(pCtx->pParse, "%s", sqlite3_value_text(pVal));
65405  }else{
65406    sqlite3ValueApplyAffinity(pVal, aff, SQLITE_UTF8);
65407    assert( rc==SQLITE_OK );
65408    rc = sqlite3VdbeChangeEncoding(pVal, enc);
65409    if( rc==SQLITE_OK && sqlite3VdbeMemTooBig(pVal) ){
65410      rc = SQLITE_TOOBIG;
65411      pCtx->pParse->nErr++;
65412    }
65413  }
65414  pCtx->pParse->rc = rc;
65415
65416 value_from_function_out:
65417  if( rc!=SQLITE_OK ){
65418    pVal = 0;
65419  }
65420  if( apVal ){
65421    for(i=0; i<nVal; i++){
65422      sqlite3ValueFree(apVal[i]);
65423    }
65424    sqlite3DbFree(db, apVal);
65425  }
65426
65427  *ppVal = pVal;
65428  return rc;
65429}
65430#else
65431# define valueFromFunction(a,b,c,d,e,f) SQLITE_OK
65432#endif /* defined(SQLITE_ENABLE_STAT3_OR_STAT4) */
65433
65434/*
65435** Extract a value from the supplied expression in the manner described
65436** above sqlite3ValueFromExpr(). Allocate the sqlite3_value object
65437** using valueNew().
65438**
65439** If pCtx is NULL and an error occurs after the sqlite3_value object
65440** has been allocated, it is freed before returning. Or, if pCtx is not
65441** NULL, it is assumed that the caller will free any allocated object
65442** in all cases.
65443*/
65444static int valueFromExpr(
65445  sqlite3 *db,                    /* The database connection */
65446  Expr *pExpr,                    /* The expression to evaluate */
65447  u8 enc,                         /* Encoding to use */
65448  u8 affinity,                    /* Affinity to use */
65449  sqlite3_value **ppVal,          /* Write the new value here */
65450  struct ValueNewStat4Ctx *pCtx   /* Second argument for valueNew() */
65451){
65452  int op;
65453  char *zVal = 0;
65454  sqlite3_value *pVal = 0;
65455  int negInt = 1;
65456  const char *zNeg = "";
65457  int rc = SQLITE_OK;
65458
65459  if( !pExpr ){
65460    *ppVal = 0;
65461    return SQLITE_OK;
65462  }
65463  while( (op = pExpr->op)==TK_UPLUS ) pExpr = pExpr->pLeft;
65464  if( NEVER(op==TK_REGISTER) ) op = pExpr->op2;
65465
65466  /* Compressed expressions only appear when parsing the DEFAULT clause
65467  ** on a table column definition, and hence only when pCtx==0.  This
65468  ** check ensures that an EP_TokenOnly expression is never passed down
65469  ** into valueFromFunction(). */
65470  assert( (pExpr->flags & EP_TokenOnly)==0 || pCtx==0 );
65471
65472  if( op==TK_CAST ){
65473    u8 aff = sqlite3AffinityType(pExpr->u.zToken,0);
65474    rc = valueFromExpr(db, pExpr->pLeft, enc, aff, ppVal, pCtx);
65475    testcase( rc!=SQLITE_OK );
65476    if( *ppVal ){
65477      sqlite3VdbeMemCast(*ppVal, aff, SQLITE_UTF8);
65478      sqlite3ValueApplyAffinity(*ppVal, affinity, SQLITE_UTF8);
65479    }
65480    return rc;
65481  }
65482
65483  /* Handle negative integers in a single step.  This is needed in the
65484  ** case when the value is -9223372036854775808.
65485  */
65486  if( op==TK_UMINUS
65487   && (pExpr->pLeft->op==TK_INTEGER || pExpr->pLeft->op==TK_FLOAT) ){
65488    pExpr = pExpr->pLeft;
65489    op = pExpr->op;
65490    negInt = -1;
65491    zNeg = "-";
65492  }
65493
65494  if( op==TK_STRING || op==TK_FLOAT || op==TK_INTEGER ){
65495    pVal = valueNew(db, pCtx);
65496    if( pVal==0 ) goto no_mem;
65497    if( ExprHasProperty(pExpr, EP_IntValue) ){
65498      sqlite3VdbeMemSetInt64(pVal, (i64)pExpr->u.iValue*negInt);
65499    }else{
65500      zVal = sqlite3MPrintf(db, "%s%s", zNeg, pExpr->u.zToken);
65501      if( zVal==0 ) goto no_mem;
65502      sqlite3ValueSetStr(pVal, -1, zVal, SQLITE_UTF8, SQLITE_DYNAMIC);
65503    }
65504    if( (op==TK_INTEGER || op==TK_FLOAT ) && affinity==SQLITE_AFF_BLOB ){
65505      sqlite3ValueApplyAffinity(pVal, SQLITE_AFF_NUMERIC, SQLITE_UTF8);
65506    }else{
65507      sqlite3ValueApplyAffinity(pVal, affinity, SQLITE_UTF8);
65508    }
65509    if( pVal->flags & (MEM_Int|MEM_Real) ) pVal->flags &= ~MEM_Str;
65510    if( enc!=SQLITE_UTF8 ){
65511      rc = sqlite3VdbeChangeEncoding(pVal, enc);
65512    }
65513  }else if( op==TK_UMINUS ) {
65514    /* This branch happens for multiple negative signs.  Ex: -(-5) */
65515    if( SQLITE_OK==sqlite3ValueFromExpr(db,pExpr->pLeft,enc,affinity,&pVal)
65516     && pVal!=0
65517    ){
65518      sqlite3VdbeMemNumerify(pVal);
65519      if( pVal->flags & MEM_Real ){
65520        pVal->u.r = -pVal->u.r;
65521      }else if( pVal->u.i==SMALLEST_INT64 ){
65522        pVal->u.r = -(double)SMALLEST_INT64;
65523        MemSetTypeFlag(pVal, MEM_Real);
65524      }else{
65525        pVal->u.i = -pVal->u.i;
65526      }
65527      sqlite3ValueApplyAffinity(pVal, affinity, enc);
65528    }
65529  }else if( op==TK_NULL ){
65530    pVal = valueNew(db, pCtx);
65531    if( pVal==0 ) goto no_mem;
65532  }
65533#ifndef SQLITE_OMIT_BLOB_LITERAL
65534  else if( op==TK_BLOB ){
65535    int nVal;
65536    assert( pExpr->u.zToken[0]=='x' || pExpr->u.zToken[0]=='X' );
65537    assert( pExpr->u.zToken[1]=='\'' );
65538    pVal = valueNew(db, pCtx);
65539    if( !pVal ) goto no_mem;
65540    zVal = &pExpr->u.zToken[2];
65541    nVal = sqlite3Strlen30(zVal)-1;
65542    assert( zVal[nVal]=='\'' );
65543    sqlite3VdbeMemSetStr(pVal, sqlite3HexToBlob(db, zVal, nVal), nVal/2,
65544                         0, SQLITE_DYNAMIC);
65545  }
65546#endif
65547
65548#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
65549  else if( op==TK_FUNCTION && pCtx!=0 ){
65550    rc = valueFromFunction(db, pExpr, enc, affinity, &pVal, pCtx);
65551  }
65552#endif
65553
65554  *ppVal = pVal;
65555  return rc;
65556
65557no_mem:
65558  db->mallocFailed = 1;
65559  sqlite3DbFree(db, zVal);
65560  assert( *ppVal==0 );
65561#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
65562  if( pCtx==0 ) sqlite3ValueFree(pVal);
65563#else
65564  assert( pCtx==0 ); sqlite3ValueFree(pVal);
65565#endif
65566  return SQLITE_NOMEM;
65567}
65568
65569/*
65570** Create a new sqlite3_value object, containing the value of pExpr.
65571**
65572** This only works for very simple expressions that consist of one constant
65573** token (i.e. "5", "5.1", "'a string'"). If the expression can
65574** be converted directly into a value, then the value is allocated and
65575** a pointer written to *ppVal. The caller is responsible for deallocating
65576** the value by passing it to sqlite3ValueFree() later on. If the expression
65577** cannot be converted to a value, then *ppVal is set to NULL.
65578*/
65579SQLITE_PRIVATE int sqlite3ValueFromExpr(
65580  sqlite3 *db,              /* The database connection */
65581  Expr *pExpr,              /* The expression to evaluate */
65582  u8 enc,                   /* Encoding to use */
65583  u8 affinity,              /* Affinity to use */
65584  sqlite3_value **ppVal     /* Write the new value here */
65585){
65586  return valueFromExpr(db, pExpr, enc, affinity, ppVal, 0);
65587}
65588
65589#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
65590/*
65591** The implementation of the sqlite_record() function. This function accepts
65592** a single argument of any type. The return value is a formatted database
65593** record (a blob) containing the argument value.
65594**
65595** This is used to convert the value stored in the 'sample' column of the
65596** sqlite_stat3 table to the record format SQLite uses internally.
65597*/
65598static void recordFunc(
65599  sqlite3_context *context,
65600  int argc,
65601  sqlite3_value **argv
65602){
65603  const int file_format = 1;
65604  int iSerial;                    /* Serial type */
65605  int nSerial;                    /* Bytes of space for iSerial as varint */
65606  int nVal;                       /* Bytes of space required for argv[0] */
65607  int nRet;
65608  sqlite3 *db;
65609  u8 *aRet;
65610
65611  UNUSED_PARAMETER( argc );
65612  iSerial = sqlite3VdbeSerialType(argv[0], file_format);
65613  nSerial = sqlite3VarintLen(iSerial);
65614  nVal = sqlite3VdbeSerialTypeLen(iSerial);
65615  db = sqlite3_context_db_handle(context);
65616
65617  nRet = 1 + nSerial + nVal;
65618  aRet = sqlite3DbMallocRaw(db, nRet);
65619  if( aRet==0 ){
65620    sqlite3_result_error_nomem(context);
65621  }else{
65622    aRet[0] = nSerial+1;
65623    putVarint32(&aRet[1], iSerial);
65624    sqlite3VdbeSerialPut(&aRet[1+nSerial], argv[0], iSerial);
65625    sqlite3_result_blob(context, aRet, nRet, SQLITE_TRANSIENT);
65626    sqlite3DbFree(db, aRet);
65627  }
65628}
65629
65630/*
65631** Register built-in functions used to help read ANALYZE data.
65632*/
65633SQLITE_PRIVATE void sqlite3AnalyzeFunctions(void){
65634  static SQLITE_WSD FuncDef aAnalyzeTableFuncs[] = {
65635    FUNCTION(sqlite_record,   1, 0, 0, recordFunc),
65636  };
65637  int i;
65638  FuncDefHash *pHash = &GLOBAL(FuncDefHash, sqlite3GlobalFunctions);
65639  FuncDef *aFunc = (FuncDef*)&GLOBAL(FuncDef, aAnalyzeTableFuncs);
65640  for(i=0; i<ArraySize(aAnalyzeTableFuncs); i++){
65641    sqlite3FuncDefInsert(pHash, &aFunc[i]);
65642  }
65643}
65644
65645/*
65646** Attempt to extract a value from pExpr and use it to construct *ppVal.
65647**
65648** If pAlloc is not NULL, then an UnpackedRecord object is created for
65649** pAlloc if one does not exist and the new value is added to the
65650** UnpackedRecord object.
65651**
65652** A value is extracted in the following cases:
65653**
65654**  * (pExpr==0). In this case the value is assumed to be an SQL NULL,
65655**
65656**  * The expression is a bound variable, and this is a reprepare, or
65657**
65658**  * The expression is a literal value.
65659**
65660** On success, *ppVal is made to point to the extracted value.  The caller
65661** is responsible for ensuring that the value is eventually freed.
65662*/
65663static int stat4ValueFromExpr(
65664  Parse *pParse,                  /* Parse context */
65665  Expr *pExpr,                    /* The expression to extract a value from */
65666  u8 affinity,                    /* Affinity to use */
65667  struct ValueNewStat4Ctx *pAlloc,/* How to allocate space.  Or NULL */
65668  sqlite3_value **ppVal           /* OUT: New value object (or NULL) */
65669){
65670  int rc = SQLITE_OK;
65671  sqlite3_value *pVal = 0;
65672  sqlite3 *db = pParse->db;
65673
65674  /* Skip over any TK_COLLATE nodes */
65675  pExpr = sqlite3ExprSkipCollate(pExpr);
65676
65677  if( !pExpr ){
65678    pVal = valueNew(db, pAlloc);
65679    if( pVal ){
65680      sqlite3VdbeMemSetNull((Mem*)pVal);
65681    }
65682  }else if( pExpr->op==TK_VARIABLE
65683        || NEVER(pExpr->op==TK_REGISTER && pExpr->op2==TK_VARIABLE)
65684  ){
65685    Vdbe *v;
65686    int iBindVar = pExpr->iColumn;
65687    sqlite3VdbeSetVarmask(pParse->pVdbe, iBindVar);
65688    if( (v = pParse->pReprepare)!=0 ){
65689      pVal = valueNew(db, pAlloc);
65690      if( pVal ){
65691        rc = sqlite3VdbeMemCopy((Mem*)pVal, &v->aVar[iBindVar-1]);
65692        if( rc==SQLITE_OK ){
65693          sqlite3ValueApplyAffinity(pVal, affinity, ENC(db));
65694        }
65695        pVal->db = pParse->db;
65696      }
65697    }
65698  }else{
65699    rc = valueFromExpr(db, pExpr, ENC(db), affinity, &pVal, pAlloc);
65700  }
65701
65702  assert( pVal==0 || pVal->db==db );
65703  *ppVal = pVal;
65704  return rc;
65705}
65706
65707/*
65708** This function is used to allocate and populate UnpackedRecord
65709** structures intended to be compared against sample index keys stored
65710** in the sqlite_stat4 table.
65711**
65712** A single call to this function attempts to populates field iVal (leftmost
65713** is 0 etc.) of the unpacked record with a value extracted from expression
65714** pExpr. Extraction of values is possible if:
65715**
65716**  * (pExpr==0). In this case the value is assumed to be an SQL NULL,
65717**
65718**  * The expression is a bound variable, and this is a reprepare, or
65719**
65720**  * The sqlite3ValueFromExpr() function is able to extract a value
65721**    from the expression (i.e. the expression is a literal value).
65722**
65723** If a value can be extracted, the affinity passed as the 5th argument
65724** is applied to it before it is copied into the UnpackedRecord. Output
65725** parameter *pbOk is set to true if a value is extracted, or false
65726** otherwise.
65727**
65728** When this function is called, *ppRec must either point to an object
65729** allocated by an earlier call to this function, or must be NULL. If it
65730** is NULL and a value can be successfully extracted, a new UnpackedRecord
65731** is allocated (and *ppRec set to point to it) before returning.
65732**
65733** Unless an error is encountered, SQLITE_OK is returned. It is not an
65734** error if a value cannot be extracted from pExpr. If an error does
65735** occur, an SQLite error code is returned.
65736*/
65737SQLITE_PRIVATE int sqlite3Stat4ProbeSetValue(
65738  Parse *pParse,                  /* Parse context */
65739  Index *pIdx,                    /* Index being probed */
65740  UnpackedRecord **ppRec,         /* IN/OUT: Probe record */
65741  Expr *pExpr,                    /* The expression to extract a value from */
65742  u8 affinity,                    /* Affinity to use */
65743  int iVal,                       /* Array element to populate */
65744  int *pbOk                       /* OUT: True if value was extracted */
65745){
65746  int rc;
65747  sqlite3_value *pVal = 0;
65748  struct ValueNewStat4Ctx alloc;
65749
65750  alloc.pParse = pParse;
65751  alloc.pIdx = pIdx;
65752  alloc.ppRec = ppRec;
65753  alloc.iVal = iVal;
65754
65755  rc = stat4ValueFromExpr(pParse, pExpr, affinity, &alloc, &pVal);
65756  assert( pVal==0 || pVal->db==pParse->db );
65757  *pbOk = (pVal!=0);
65758  return rc;
65759}
65760
65761/*
65762** Attempt to extract a value from expression pExpr using the methods
65763** as described for sqlite3Stat4ProbeSetValue() above.
65764**
65765** If successful, set *ppVal to point to a new value object and return
65766** SQLITE_OK. If no value can be extracted, but no other error occurs
65767** (e.g. OOM), return SQLITE_OK and set *ppVal to NULL. Or, if an error
65768** does occur, return an SQLite error code. The final value of *ppVal
65769** is undefined in this case.
65770*/
65771SQLITE_PRIVATE int sqlite3Stat4ValueFromExpr(
65772  Parse *pParse,                  /* Parse context */
65773  Expr *pExpr,                    /* The expression to extract a value from */
65774  u8 affinity,                    /* Affinity to use */
65775  sqlite3_value **ppVal           /* OUT: New value object (or NULL) */
65776){
65777  return stat4ValueFromExpr(pParse, pExpr, affinity, 0, ppVal);
65778}
65779
65780/*
65781** Extract the iCol-th column from the nRec-byte record in pRec.  Write
65782** the column value into *ppVal.  If *ppVal is initially NULL then a new
65783** sqlite3_value object is allocated.
65784**
65785** If *ppVal is initially NULL then the caller is responsible for
65786** ensuring that the value written into *ppVal is eventually freed.
65787*/
65788SQLITE_PRIVATE int sqlite3Stat4Column(
65789  sqlite3 *db,                    /* Database handle */
65790  const void *pRec,               /* Pointer to buffer containing record */
65791  int nRec,                       /* Size of buffer pRec in bytes */
65792  int iCol,                       /* Column to extract */
65793  sqlite3_value **ppVal           /* OUT: Extracted value */
65794){
65795  u32 t;                          /* a column type code */
65796  int nHdr;                       /* Size of the header in the record */
65797  int iHdr;                       /* Next unread header byte */
65798  int iField;                     /* Next unread data byte */
65799  int szField;                    /* Size of the current data field */
65800  int i;                          /* Column index */
65801  u8 *a = (u8*)pRec;              /* Typecast byte array */
65802  Mem *pMem = *ppVal;             /* Write result into this Mem object */
65803
65804  assert( iCol>0 );
65805  iHdr = getVarint32(a, nHdr);
65806  if( nHdr>nRec || iHdr>=nHdr ) return SQLITE_CORRUPT_BKPT;
65807  iField = nHdr;
65808  for(i=0; i<=iCol; i++){
65809    iHdr += getVarint32(&a[iHdr], t);
65810    testcase( iHdr==nHdr );
65811    testcase( iHdr==nHdr+1 );
65812    if( iHdr>nHdr ) return SQLITE_CORRUPT_BKPT;
65813    szField = sqlite3VdbeSerialTypeLen(t);
65814    iField += szField;
65815  }
65816  testcase( iField==nRec );
65817  testcase( iField==nRec+1 );
65818  if( iField>nRec ) return SQLITE_CORRUPT_BKPT;
65819  if( pMem==0 ){
65820    pMem = *ppVal = sqlite3ValueNew(db);
65821    if( pMem==0 ) return SQLITE_NOMEM;
65822  }
65823  sqlite3VdbeSerialGet(&a[iField-szField], t, pMem);
65824  pMem->enc = ENC(db);
65825  return SQLITE_OK;
65826}
65827
65828/*
65829** Unless it is NULL, the argument must be an UnpackedRecord object returned
65830** by an earlier call to sqlite3Stat4ProbeSetValue(). This call deletes
65831** the object.
65832*/
65833SQLITE_PRIVATE void sqlite3Stat4ProbeFree(UnpackedRecord *pRec){
65834  if( pRec ){
65835    int i;
65836    int nCol = pRec->pKeyInfo->nField+pRec->pKeyInfo->nXField;
65837    Mem *aMem = pRec->aMem;
65838    sqlite3 *db = aMem[0].db;
65839    for(i=0; i<nCol; i++){
65840      sqlite3VdbeMemRelease(&aMem[i]);
65841    }
65842    sqlite3KeyInfoUnref(pRec->pKeyInfo);
65843    sqlite3DbFree(db, pRec);
65844  }
65845}
65846#endif /* ifdef SQLITE_ENABLE_STAT4 */
65847
65848/*
65849** Change the string value of an sqlite3_value object
65850*/
65851SQLITE_PRIVATE void sqlite3ValueSetStr(
65852  sqlite3_value *v,     /* Value to be set */
65853  int n,                /* Length of string z */
65854  const void *z,        /* Text of the new string */
65855  u8 enc,               /* Encoding to use */
65856  void (*xDel)(void*)   /* Destructor for the string */
65857){
65858  if( v ) sqlite3VdbeMemSetStr((Mem *)v, z, n, enc, xDel);
65859}
65860
65861/*
65862** Free an sqlite3_value object
65863*/
65864SQLITE_PRIVATE void sqlite3ValueFree(sqlite3_value *v){
65865  if( !v ) return;
65866  sqlite3VdbeMemRelease((Mem *)v);
65867  sqlite3DbFree(((Mem*)v)->db, v);
65868}
65869
65870/*
65871** The sqlite3ValueBytes() routine returns the number of bytes in the
65872** sqlite3_value object assuming that it uses the encoding "enc".
65873** The valueBytes() routine is a helper function.
65874*/
65875static SQLITE_NOINLINE int valueBytes(sqlite3_value *pVal, u8 enc){
65876  return valueToText(pVal, enc)!=0 ? pVal->n : 0;
65877}
65878SQLITE_PRIVATE int sqlite3ValueBytes(sqlite3_value *pVal, u8 enc){
65879  Mem *p = (Mem*)pVal;
65880  assert( (p->flags & MEM_Null)==0 || (p->flags & (MEM_Str|MEM_Blob))==0 );
65881  if( (p->flags & MEM_Str)!=0 && pVal->enc==enc ){
65882    return p->n;
65883  }
65884  if( (p->flags & MEM_Blob)!=0 ){
65885    if( p->flags & MEM_Zero ){
65886      return p->n + p->u.nZero;
65887    }else{
65888      return p->n;
65889    }
65890  }
65891  if( p->flags & MEM_Null ) return 0;
65892  return valueBytes(pVal, enc);
65893}
65894
65895/************** End of vdbemem.c *********************************************/
65896/************** Begin file vdbeaux.c *****************************************/
65897/*
65898** 2003 September 6
65899**
65900** The author disclaims copyright to this source code.  In place of
65901** a legal notice, here is a blessing:
65902**
65903**    May you do good and not evil.
65904**    May you find forgiveness for yourself and forgive others.
65905**    May you share freely, never taking more than you give.
65906**
65907*************************************************************************
65908** This file contains code used for creating, destroying, and populating
65909** a VDBE (or an "sqlite3_stmt" as it is known to the outside world.)
65910*/
65911/* #include "sqliteInt.h" */
65912/* #include "vdbeInt.h" */
65913
65914/*
65915** Create a new virtual database engine.
65916*/
65917SQLITE_PRIVATE Vdbe *sqlite3VdbeCreate(Parse *pParse){
65918  sqlite3 *db = pParse->db;
65919  Vdbe *p;
65920  p = sqlite3DbMallocZero(db, sizeof(Vdbe) );
65921  if( p==0 ) return 0;
65922  p->db = db;
65923  if( db->pVdbe ){
65924    db->pVdbe->pPrev = p;
65925  }
65926  p->pNext = db->pVdbe;
65927  p->pPrev = 0;
65928  db->pVdbe = p;
65929  p->magic = VDBE_MAGIC_INIT;
65930  p->pParse = pParse;
65931  assert( pParse->aLabel==0 );
65932  assert( pParse->nLabel==0 );
65933  assert( pParse->nOpAlloc==0 );
65934  return p;
65935}
65936
65937/*
65938** Change the error string stored in Vdbe.zErrMsg
65939*/
65940SQLITE_PRIVATE void sqlite3VdbeError(Vdbe *p, const char *zFormat, ...){
65941  va_list ap;
65942  sqlite3DbFree(p->db, p->zErrMsg);
65943  va_start(ap, zFormat);
65944  p->zErrMsg = sqlite3VMPrintf(p->db, zFormat, ap);
65945  va_end(ap);
65946}
65947
65948/*
65949** Remember the SQL string for a prepared statement.
65950*/
65951SQLITE_PRIVATE void sqlite3VdbeSetSql(Vdbe *p, const char *z, int n, int isPrepareV2){
65952  assert( isPrepareV2==1 || isPrepareV2==0 );
65953  if( p==0 ) return;
65954#if defined(SQLITE_OMIT_TRACE) && !defined(SQLITE_ENABLE_SQLLOG)
65955  if( !isPrepareV2 ) return;
65956#endif
65957  assert( p->zSql==0 );
65958  p->zSql = sqlite3DbStrNDup(p->db, z, n);
65959  p->isPrepareV2 = (u8)isPrepareV2;
65960}
65961
65962/*
65963** Return the SQL associated with a prepared statement
65964*/
65965SQLITE_API const char *SQLITE_STDCALL sqlite3_sql(sqlite3_stmt *pStmt){
65966  Vdbe *p = (Vdbe *)pStmt;
65967  return (p && p->isPrepareV2) ? p->zSql : 0;
65968}
65969
65970/*
65971** Swap all content between two VDBE structures.
65972*/
65973SQLITE_PRIVATE void sqlite3VdbeSwap(Vdbe *pA, Vdbe *pB){
65974  Vdbe tmp, *pTmp;
65975  char *zTmp;
65976  tmp = *pA;
65977  *pA = *pB;
65978  *pB = tmp;
65979  pTmp = pA->pNext;
65980  pA->pNext = pB->pNext;
65981  pB->pNext = pTmp;
65982  pTmp = pA->pPrev;
65983  pA->pPrev = pB->pPrev;
65984  pB->pPrev = pTmp;
65985  zTmp = pA->zSql;
65986  pA->zSql = pB->zSql;
65987  pB->zSql = zTmp;
65988  pB->isPrepareV2 = pA->isPrepareV2;
65989}
65990
65991/*
65992** Resize the Vdbe.aOp array so that it is at least nOp elements larger
65993** than its current size. nOp is guaranteed to be less than or equal
65994** to 1024/sizeof(Op).
65995**
65996** If an out-of-memory error occurs while resizing the array, return
65997** SQLITE_NOMEM. In this case Vdbe.aOp and Parse.nOpAlloc remain
65998** unchanged (this is so that any opcodes already allocated can be
65999** correctly deallocated along with the rest of the Vdbe).
66000*/
66001static int growOpArray(Vdbe *v, int nOp){
66002  VdbeOp *pNew;
66003  Parse *p = v->pParse;
66004
66005  /* The SQLITE_TEST_REALLOC_STRESS compile-time option is designed to force
66006  ** more frequent reallocs and hence provide more opportunities for
66007  ** simulated OOM faults.  SQLITE_TEST_REALLOC_STRESS is generally used
66008  ** during testing only.  With SQLITE_TEST_REALLOC_STRESS grow the op array
66009  ** by the minimum* amount required until the size reaches 512.  Normal
66010  ** operation (without SQLITE_TEST_REALLOC_STRESS) is to double the current
66011  ** size of the op array or add 1KB of space, whichever is smaller. */
66012#ifdef SQLITE_TEST_REALLOC_STRESS
66013  int nNew = (p->nOpAlloc>=512 ? p->nOpAlloc*2 : p->nOpAlloc+nOp);
66014#else
66015  int nNew = (p->nOpAlloc ? p->nOpAlloc*2 : (int)(1024/sizeof(Op)));
66016  UNUSED_PARAMETER(nOp);
66017#endif
66018
66019  assert( nOp<=(1024/sizeof(Op)) );
66020  assert( nNew>=(p->nOpAlloc+nOp) );
66021  pNew = sqlite3DbRealloc(p->db, v->aOp, nNew*sizeof(Op));
66022  if( pNew ){
66023    p->nOpAlloc = sqlite3DbMallocSize(p->db, pNew)/sizeof(Op);
66024    v->aOp = pNew;
66025  }
66026  return (pNew ? SQLITE_OK : SQLITE_NOMEM);
66027}
66028
66029#ifdef SQLITE_DEBUG
66030/* This routine is just a convenient place to set a breakpoint that will
66031** fire after each opcode is inserted and displayed using
66032** "PRAGMA vdbe_addoptrace=on".
66033*/
66034static void test_addop_breakpoint(void){
66035  static int n = 0;
66036  n++;
66037}
66038#endif
66039
66040/*
66041** Add a new instruction to the list of instructions current in the
66042** VDBE.  Return the address of the new instruction.
66043**
66044** Parameters:
66045**
66046**    p               Pointer to the VDBE
66047**
66048**    op              The opcode for this instruction
66049**
66050**    p1, p2, p3      Operands
66051**
66052** Use the sqlite3VdbeResolveLabel() function to fix an address and
66053** the sqlite3VdbeChangeP4() function to change the value of the P4
66054** operand.
66055*/
66056SQLITE_PRIVATE int sqlite3VdbeAddOp3(Vdbe *p, int op, int p1, int p2, int p3){
66057  int i;
66058  VdbeOp *pOp;
66059
66060  i = p->nOp;
66061  assert( p->magic==VDBE_MAGIC_INIT );
66062  assert( op>0 && op<0xff );
66063  if( p->pParse->nOpAlloc<=i ){
66064    if( growOpArray(p, 1) ){
66065      return 1;
66066    }
66067  }
66068  p->nOp++;
66069  pOp = &p->aOp[i];
66070  pOp->opcode = (u8)op;
66071  pOp->p5 = 0;
66072  pOp->p1 = p1;
66073  pOp->p2 = p2;
66074  pOp->p3 = p3;
66075  pOp->p4.p = 0;
66076  pOp->p4type = P4_NOTUSED;
66077#ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
66078  pOp->zComment = 0;
66079#endif
66080#ifdef SQLITE_DEBUG
66081  if( p->db->flags & SQLITE_VdbeAddopTrace ){
66082    int jj, kk;
66083    Parse *pParse = p->pParse;
66084    for(jj=kk=0; jj<SQLITE_N_COLCACHE; jj++){
66085      struct yColCache *x = pParse->aColCache + jj;
66086      if( x->iLevel>pParse->iCacheLevel || x->iReg==0 ) continue;
66087      printf(" r[%d]={%d:%d}", x->iReg, x->iTable, x->iColumn);
66088      kk++;
66089    }
66090    if( kk ) printf("\n");
66091    sqlite3VdbePrintOp(0, i, &p->aOp[i]);
66092    test_addop_breakpoint();
66093  }
66094#endif
66095#ifdef VDBE_PROFILE
66096  pOp->cycles = 0;
66097  pOp->cnt = 0;
66098#endif
66099#ifdef SQLITE_VDBE_COVERAGE
66100  pOp->iSrcLine = 0;
66101#endif
66102  return i;
66103}
66104SQLITE_PRIVATE int sqlite3VdbeAddOp0(Vdbe *p, int op){
66105  return sqlite3VdbeAddOp3(p, op, 0, 0, 0);
66106}
66107SQLITE_PRIVATE int sqlite3VdbeAddOp1(Vdbe *p, int op, int p1){
66108  return sqlite3VdbeAddOp3(p, op, p1, 0, 0);
66109}
66110SQLITE_PRIVATE int sqlite3VdbeAddOp2(Vdbe *p, int op, int p1, int p2){
66111  return sqlite3VdbeAddOp3(p, op, p1, p2, 0);
66112}
66113
66114
66115/*
66116** Add an opcode that includes the p4 value as a pointer.
66117*/
66118SQLITE_PRIVATE int sqlite3VdbeAddOp4(
66119  Vdbe *p,            /* Add the opcode to this VM */
66120  int op,             /* The new opcode */
66121  int p1,             /* The P1 operand */
66122  int p2,             /* The P2 operand */
66123  int p3,             /* The P3 operand */
66124  const char *zP4,    /* The P4 operand */
66125  int p4type          /* P4 operand type */
66126){
66127  int addr = sqlite3VdbeAddOp3(p, op, p1, p2, p3);
66128  sqlite3VdbeChangeP4(p, addr, zP4, p4type);
66129  return addr;
66130}
66131
66132/*
66133** Add an opcode that includes the p4 value with a P4_INT64 type.
66134*/
66135SQLITE_PRIVATE int sqlite3VdbeAddOp4Dup8(
66136  Vdbe *p,            /* Add the opcode to this VM */
66137  int op,             /* The new opcode */
66138  int p1,             /* The P1 operand */
66139  int p2,             /* The P2 operand */
66140  int p3,             /* The P3 operand */
66141  const u8 *zP4,      /* The P4 operand */
66142  int p4type          /* P4 operand type */
66143){
66144  char *p4copy = sqlite3DbMallocRaw(sqlite3VdbeDb(p), 8);
66145  if( p4copy ) memcpy(p4copy, zP4, 8);
66146  return sqlite3VdbeAddOp4(p, op, p1, p2, p3, p4copy, p4type);
66147}
66148
66149/*
66150** Add an OP_ParseSchema opcode.  This routine is broken out from
66151** sqlite3VdbeAddOp4() since it needs to also needs to mark all btrees
66152** as having been used.
66153**
66154** The zWhere string must have been obtained from sqlite3_malloc().
66155** This routine will take ownership of the allocated memory.
66156*/
66157SQLITE_PRIVATE void sqlite3VdbeAddParseSchemaOp(Vdbe *p, int iDb, char *zWhere){
66158  int j;
66159  int addr = sqlite3VdbeAddOp3(p, OP_ParseSchema, iDb, 0, 0);
66160  sqlite3VdbeChangeP4(p, addr, zWhere, P4_DYNAMIC);
66161  for(j=0; j<p->db->nDb; j++) sqlite3VdbeUsesBtree(p, j);
66162}
66163
66164/*
66165** Add an opcode that includes the p4 value as an integer.
66166*/
66167SQLITE_PRIVATE int sqlite3VdbeAddOp4Int(
66168  Vdbe *p,            /* Add the opcode to this VM */
66169  int op,             /* The new opcode */
66170  int p1,             /* The P1 operand */
66171  int p2,             /* The P2 operand */
66172  int p3,             /* The P3 operand */
66173  int p4              /* The P4 operand as an integer */
66174){
66175  int addr = sqlite3VdbeAddOp3(p, op, p1, p2, p3);
66176  sqlite3VdbeChangeP4(p, addr, SQLITE_INT_TO_PTR(p4), P4_INT32);
66177  return addr;
66178}
66179
66180/*
66181** Create a new symbolic label for an instruction that has yet to be
66182** coded.  The symbolic label is really just a negative number.  The
66183** label can be used as the P2 value of an operation.  Later, when
66184** the label is resolved to a specific address, the VDBE will scan
66185** through its operation list and change all values of P2 which match
66186** the label into the resolved address.
66187**
66188** The VDBE knows that a P2 value is a label because labels are
66189** always negative and P2 values are suppose to be non-negative.
66190** Hence, a negative P2 value is a label that has yet to be resolved.
66191**
66192** Zero is returned if a malloc() fails.
66193*/
66194SQLITE_PRIVATE int sqlite3VdbeMakeLabel(Vdbe *v){
66195  Parse *p = v->pParse;
66196  int i = p->nLabel++;
66197  assert( v->magic==VDBE_MAGIC_INIT );
66198  if( (i & (i-1))==0 ){
66199    p->aLabel = sqlite3DbReallocOrFree(p->db, p->aLabel,
66200                                       (i*2+1)*sizeof(p->aLabel[0]));
66201  }
66202  if( p->aLabel ){
66203    p->aLabel[i] = -1;
66204  }
66205  return -1-i;
66206}
66207
66208/*
66209** Resolve label "x" to be the address of the next instruction to
66210** be inserted.  The parameter "x" must have been obtained from
66211** a prior call to sqlite3VdbeMakeLabel().
66212*/
66213SQLITE_PRIVATE void sqlite3VdbeResolveLabel(Vdbe *v, int x){
66214  Parse *p = v->pParse;
66215  int j = -1-x;
66216  assert( v->magic==VDBE_MAGIC_INIT );
66217  assert( j<p->nLabel );
66218  if( ALWAYS(j>=0) && p->aLabel ){
66219    p->aLabel[j] = v->nOp;
66220  }
66221  p->iFixedOp = v->nOp - 1;
66222}
66223
66224/*
66225** Mark the VDBE as one that can only be run one time.
66226*/
66227SQLITE_PRIVATE void sqlite3VdbeRunOnlyOnce(Vdbe *p){
66228  p->runOnlyOnce = 1;
66229}
66230
66231#ifdef SQLITE_DEBUG /* sqlite3AssertMayAbort() logic */
66232
66233/*
66234** The following type and function are used to iterate through all opcodes
66235** in a Vdbe main program and each of the sub-programs (triggers) it may
66236** invoke directly or indirectly. It should be used as follows:
66237**
66238**   Op *pOp;
66239**   VdbeOpIter sIter;
66240**
66241**   memset(&sIter, 0, sizeof(sIter));
66242**   sIter.v = v;                            // v is of type Vdbe*
66243**   while( (pOp = opIterNext(&sIter)) ){
66244**     // Do something with pOp
66245**   }
66246**   sqlite3DbFree(v->db, sIter.apSub);
66247**
66248*/
66249typedef struct VdbeOpIter VdbeOpIter;
66250struct VdbeOpIter {
66251  Vdbe *v;                   /* Vdbe to iterate through the opcodes of */
66252  SubProgram **apSub;        /* Array of subprograms */
66253  int nSub;                  /* Number of entries in apSub */
66254  int iAddr;                 /* Address of next instruction to return */
66255  int iSub;                  /* 0 = main program, 1 = first sub-program etc. */
66256};
66257static Op *opIterNext(VdbeOpIter *p){
66258  Vdbe *v = p->v;
66259  Op *pRet = 0;
66260  Op *aOp;
66261  int nOp;
66262
66263  if( p->iSub<=p->nSub ){
66264
66265    if( p->iSub==0 ){
66266      aOp = v->aOp;
66267      nOp = v->nOp;
66268    }else{
66269      aOp = p->apSub[p->iSub-1]->aOp;
66270      nOp = p->apSub[p->iSub-1]->nOp;
66271    }
66272    assert( p->iAddr<nOp );
66273
66274    pRet = &aOp[p->iAddr];
66275    p->iAddr++;
66276    if( p->iAddr==nOp ){
66277      p->iSub++;
66278      p->iAddr = 0;
66279    }
66280
66281    if( pRet->p4type==P4_SUBPROGRAM ){
66282      int nByte = (p->nSub+1)*sizeof(SubProgram*);
66283      int j;
66284      for(j=0; j<p->nSub; j++){
66285        if( p->apSub[j]==pRet->p4.pProgram ) break;
66286      }
66287      if( j==p->nSub ){
66288        p->apSub = sqlite3DbReallocOrFree(v->db, p->apSub, nByte);
66289        if( !p->apSub ){
66290          pRet = 0;
66291        }else{
66292          p->apSub[p->nSub++] = pRet->p4.pProgram;
66293        }
66294      }
66295    }
66296  }
66297
66298  return pRet;
66299}
66300
66301/*
66302** Check if the program stored in the VM associated with pParse may
66303** throw an ABORT exception (causing the statement, but not entire transaction
66304** to be rolled back). This condition is true if the main program or any
66305** sub-programs contains any of the following:
66306**
66307**   *  OP_Halt with P1=SQLITE_CONSTRAINT and P2=OE_Abort.
66308**   *  OP_HaltIfNull with P1=SQLITE_CONSTRAINT and P2=OE_Abort.
66309**   *  OP_Destroy
66310**   *  OP_VUpdate
66311**   *  OP_VRename
66312**   *  OP_FkCounter with P2==0 (immediate foreign key constraint)
66313**   *  OP_CreateTable and OP_InitCoroutine (for CREATE TABLE AS SELECT ...)
66314**
66315** Then check that the value of Parse.mayAbort is true if an
66316** ABORT may be thrown, or false otherwise. Return true if it does
66317** match, or false otherwise. This function is intended to be used as
66318** part of an assert statement in the compiler. Similar to:
66319**
66320**   assert( sqlite3VdbeAssertMayAbort(pParse->pVdbe, pParse->mayAbort) );
66321*/
66322SQLITE_PRIVATE int sqlite3VdbeAssertMayAbort(Vdbe *v, int mayAbort){
66323  int hasAbort = 0;
66324  int hasFkCounter = 0;
66325  int hasCreateTable = 0;
66326  int hasInitCoroutine = 0;
66327  Op *pOp;
66328  VdbeOpIter sIter;
66329  memset(&sIter, 0, sizeof(sIter));
66330  sIter.v = v;
66331
66332  while( (pOp = opIterNext(&sIter))!=0 ){
66333    int opcode = pOp->opcode;
66334    if( opcode==OP_Destroy || opcode==OP_VUpdate || opcode==OP_VRename
66335     || ((opcode==OP_Halt || opcode==OP_HaltIfNull)
66336      && ((pOp->p1&0xff)==SQLITE_CONSTRAINT && pOp->p2==OE_Abort))
66337    ){
66338      hasAbort = 1;
66339      break;
66340    }
66341    if( opcode==OP_CreateTable ) hasCreateTable = 1;
66342    if( opcode==OP_InitCoroutine ) hasInitCoroutine = 1;
66343#ifndef SQLITE_OMIT_FOREIGN_KEY
66344    if( opcode==OP_FkCounter && pOp->p1==0 && pOp->p2==1 ){
66345      hasFkCounter = 1;
66346    }
66347#endif
66348  }
66349  sqlite3DbFree(v->db, sIter.apSub);
66350
66351  /* Return true if hasAbort==mayAbort. Or if a malloc failure occurred.
66352  ** If malloc failed, then the while() loop above may not have iterated
66353  ** through all opcodes and hasAbort may be set incorrectly. Return
66354  ** true for this case to prevent the assert() in the callers frame
66355  ** from failing.  */
66356  return ( v->db->mallocFailed || hasAbort==mayAbort || hasFkCounter
66357              || (hasCreateTable && hasInitCoroutine) );
66358}
66359#endif /* SQLITE_DEBUG - the sqlite3AssertMayAbort() function */
66360
66361/*
66362** Loop through the program looking for P2 values that are negative
66363** on jump instructions.  Each such value is a label.  Resolve the
66364** label by setting the P2 value to its correct non-zero value.
66365**
66366** This routine is called once after all opcodes have been inserted.
66367**
66368** Variable *pMaxFuncArgs is set to the maximum value of any P2 argument
66369** to an OP_Function, OP_AggStep or OP_VFilter opcode. This is used by
66370** sqlite3VdbeMakeReady() to size the Vdbe.apArg[] array.
66371**
66372** The Op.opflags field is set on all opcodes.
66373*/
66374static void resolveP2Values(Vdbe *p, int *pMaxFuncArgs){
66375  int i;
66376  int nMaxArgs = *pMaxFuncArgs;
66377  Op *pOp;
66378  Parse *pParse = p->pParse;
66379  int *aLabel = pParse->aLabel;
66380  p->readOnly = 1;
66381  p->bIsReader = 0;
66382  for(pOp=p->aOp, i=p->nOp-1; i>=0; i--, pOp++){
66383    u8 opcode = pOp->opcode;
66384
66385    /* NOTE: Be sure to update mkopcodeh.awk when adding or removing
66386    ** cases from this switch! */
66387    switch( opcode ){
66388      case OP_Transaction: {
66389        if( pOp->p2!=0 ) p->readOnly = 0;
66390        /* fall thru */
66391      }
66392      case OP_AutoCommit:
66393      case OP_Savepoint: {
66394        p->bIsReader = 1;
66395        break;
66396      }
66397#ifndef SQLITE_OMIT_WAL
66398      case OP_Checkpoint:
66399#endif
66400      case OP_Vacuum:
66401      case OP_JournalMode: {
66402        p->readOnly = 0;
66403        p->bIsReader = 1;
66404        break;
66405      }
66406#ifndef SQLITE_OMIT_VIRTUALTABLE
66407      case OP_VUpdate: {
66408        if( pOp->p2>nMaxArgs ) nMaxArgs = pOp->p2;
66409        break;
66410      }
66411      case OP_VFilter: {
66412        int n;
66413        assert( p->nOp - i >= 3 );
66414        assert( pOp[-1].opcode==OP_Integer );
66415        n = pOp[-1].p1;
66416        if( n>nMaxArgs ) nMaxArgs = n;
66417        break;
66418      }
66419#endif
66420      case OP_Next:
66421      case OP_NextIfOpen:
66422      case OP_SorterNext: {
66423        pOp->p4.xAdvance = sqlite3BtreeNext;
66424        pOp->p4type = P4_ADVANCE;
66425        break;
66426      }
66427      case OP_Prev:
66428      case OP_PrevIfOpen: {
66429        pOp->p4.xAdvance = sqlite3BtreePrevious;
66430        pOp->p4type = P4_ADVANCE;
66431        break;
66432      }
66433    }
66434
66435    pOp->opflags = sqlite3OpcodeProperty[opcode];
66436    if( (pOp->opflags & OPFLG_JUMP)!=0 && pOp->p2<0 ){
66437      assert( -1-pOp->p2<pParse->nLabel );
66438      pOp->p2 = aLabel[-1-pOp->p2];
66439    }
66440  }
66441  sqlite3DbFree(p->db, pParse->aLabel);
66442  pParse->aLabel = 0;
66443  pParse->nLabel = 0;
66444  *pMaxFuncArgs = nMaxArgs;
66445  assert( p->bIsReader!=0 || DbMaskAllZero(p->btreeMask) );
66446}
66447
66448/*
66449** Return the address of the next instruction to be inserted.
66450*/
66451SQLITE_PRIVATE int sqlite3VdbeCurrentAddr(Vdbe *p){
66452  assert( p->magic==VDBE_MAGIC_INIT );
66453  return p->nOp;
66454}
66455
66456/*
66457** This function returns a pointer to the array of opcodes associated with
66458** the Vdbe passed as the first argument. It is the callers responsibility
66459** to arrange for the returned array to be eventually freed using the
66460** vdbeFreeOpArray() function.
66461**
66462** Before returning, *pnOp is set to the number of entries in the returned
66463** array. Also, *pnMaxArg is set to the larger of its current value and
66464** the number of entries in the Vdbe.apArg[] array required to execute the
66465** returned program.
66466*/
66467SQLITE_PRIVATE VdbeOp *sqlite3VdbeTakeOpArray(Vdbe *p, int *pnOp, int *pnMaxArg){
66468  VdbeOp *aOp = p->aOp;
66469  assert( aOp && !p->db->mallocFailed );
66470
66471  /* Check that sqlite3VdbeUsesBtree() was not called on this VM */
66472  assert( DbMaskAllZero(p->btreeMask) );
66473
66474  resolveP2Values(p, pnMaxArg);
66475  *pnOp = p->nOp;
66476  p->aOp = 0;
66477  return aOp;
66478}
66479
66480/*
66481** Add a whole list of operations to the operation stack.  Return the
66482** address of the first operation added.
66483*/
66484SQLITE_PRIVATE int sqlite3VdbeAddOpList(Vdbe *p, int nOp, VdbeOpList const *aOp, int iLineno){
66485  int addr;
66486  assert( p->magic==VDBE_MAGIC_INIT );
66487  if( p->nOp + nOp > p->pParse->nOpAlloc && growOpArray(p, nOp) ){
66488    return 0;
66489  }
66490  addr = p->nOp;
66491  if( ALWAYS(nOp>0) ){
66492    int i;
66493    VdbeOpList const *pIn = aOp;
66494    for(i=0; i<nOp; i++, pIn++){
66495      int p2 = pIn->p2;
66496      VdbeOp *pOut = &p->aOp[i+addr];
66497      pOut->opcode = pIn->opcode;
66498      pOut->p1 = pIn->p1;
66499      if( p2<0 ){
66500        assert( sqlite3OpcodeProperty[pOut->opcode] & OPFLG_JUMP );
66501        pOut->p2 = addr + ADDR(p2);
66502      }else{
66503        pOut->p2 = p2;
66504      }
66505      pOut->p3 = pIn->p3;
66506      pOut->p4type = P4_NOTUSED;
66507      pOut->p4.p = 0;
66508      pOut->p5 = 0;
66509#ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
66510      pOut->zComment = 0;
66511#endif
66512#ifdef SQLITE_VDBE_COVERAGE
66513      pOut->iSrcLine = iLineno+i;
66514#else
66515      (void)iLineno;
66516#endif
66517#ifdef SQLITE_DEBUG
66518      if( p->db->flags & SQLITE_VdbeAddopTrace ){
66519        sqlite3VdbePrintOp(0, i+addr, &p->aOp[i+addr]);
66520      }
66521#endif
66522    }
66523    p->nOp += nOp;
66524  }
66525  return addr;
66526}
66527
66528#if defined(SQLITE_ENABLE_STMT_SCANSTATUS)
66529/*
66530** Add an entry to the array of counters managed by sqlite3_stmt_scanstatus().
66531*/
66532SQLITE_PRIVATE void sqlite3VdbeScanStatus(
66533  Vdbe *p,                        /* VM to add scanstatus() to */
66534  int addrExplain,                /* Address of OP_Explain (or 0) */
66535  int addrLoop,                   /* Address of loop counter */
66536  int addrVisit,                  /* Address of rows visited counter */
66537  LogEst nEst,                    /* Estimated number of output rows */
66538  const char *zName               /* Name of table or index being scanned */
66539){
66540  int nByte = (p->nScan+1) * sizeof(ScanStatus);
66541  ScanStatus *aNew;
66542  aNew = (ScanStatus*)sqlite3DbRealloc(p->db, p->aScan, nByte);
66543  if( aNew ){
66544    ScanStatus *pNew = &aNew[p->nScan++];
66545    pNew->addrExplain = addrExplain;
66546    pNew->addrLoop = addrLoop;
66547    pNew->addrVisit = addrVisit;
66548    pNew->nEst = nEst;
66549    pNew->zName = sqlite3DbStrDup(p->db, zName);
66550    p->aScan = aNew;
66551  }
66552}
66553#endif
66554
66555
66556/*
66557** Change the value of the P1 operand for a specific instruction.
66558** This routine is useful when a large program is loaded from a
66559** static array using sqlite3VdbeAddOpList but we want to make a
66560** few minor changes to the program.
66561*/
66562SQLITE_PRIVATE void sqlite3VdbeChangeP1(Vdbe *p, u32 addr, int val){
66563  assert( p!=0 );
66564  if( ((u32)p->nOp)>addr ){
66565    p->aOp[addr].p1 = val;
66566  }
66567}
66568
66569/*
66570** Change the value of the P2 operand for a specific instruction.
66571** This routine is useful for setting a jump destination.
66572*/
66573SQLITE_PRIVATE void sqlite3VdbeChangeP2(Vdbe *p, u32 addr, int val){
66574  assert( p!=0 );
66575  if( ((u32)p->nOp)>addr ){
66576    p->aOp[addr].p2 = val;
66577  }
66578}
66579
66580/*
66581** Change the value of the P3 operand for a specific instruction.
66582*/
66583SQLITE_PRIVATE void sqlite3VdbeChangeP3(Vdbe *p, u32 addr, int val){
66584  assert( p!=0 );
66585  if( ((u32)p->nOp)>addr ){
66586    p->aOp[addr].p3 = val;
66587  }
66588}
66589
66590/*
66591** Change the value of the P5 operand for the most recently
66592** added operation.
66593*/
66594SQLITE_PRIVATE void sqlite3VdbeChangeP5(Vdbe *p, u8 val){
66595  assert( p!=0 );
66596  if( p->aOp ){
66597    assert( p->nOp>0 );
66598    p->aOp[p->nOp-1].p5 = val;
66599  }
66600}
66601
66602/*
66603** Change the P2 operand of instruction addr so that it points to
66604** the address of the next instruction to be coded.
66605*/
66606SQLITE_PRIVATE void sqlite3VdbeJumpHere(Vdbe *p, int addr){
66607  sqlite3VdbeChangeP2(p, addr, p->nOp);
66608  p->pParse->iFixedOp = p->nOp - 1;
66609}
66610
66611
66612/*
66613** If the input FuncDef structure is ephemeral, then free it.  If
66614** the FuncDef is not ephermal, then do nothing.
66615*/
66616static void freeEphemeralFunction(sqlite3 *db, FuncDef *pDef){
66617  if( ALWAYS(pDef) && (pDef->funcFlags & SQLITE_FUNC_EPHEM)!=0 ){
66618    sqlite3DbFree(db, pDef);
66619  }
66620}
66621
66622static void vdbeFreeOpArray(sqlite3 *, Op *, int);
66623
66624/*
66625** Delete a P4 value if necessary.
66626*/
66627static void freeP4(sqlite3 *db, int p4type, void *p4){
66628  if( p4 ){
66629    assert( db );
66630    switch( p4type ){
66631      case P4_FUNCCTX: {
66632        freeEphemeralFunction(db, ((sqlite3_context*)p4)->pFunc);
66633        /* Fall through into the next case */
66634      }
66635      case P4_REAL:
66636      case P4_INT64:
66637      case P4_DYNAMIC:
66638      case P4_INTARRAY: {
66639        sqlite3DbFree(db, p4);
66640        break;
66641      }
66642      case P4_KEYINFO: {
66643        if( db->pnBytesFreed==0 ) sqlite3KeyInfoUnref((KeyInfo*)p4);
66644        break;
66645      }
66646      case P4_MPRINTF: {
66647        if( db->pnBytesFreed==0 ) sqlite3_free(p4);
66648        break;
66649      }
66650      case P4_FUNCDEF: {
66651        freeEphemeralFunction(db, (FuncDef*)p4);
66652        break;
66653      }
66654      case P4_MEM: {
66655        if( db->pnBytesFreed==0 ){
66656          sqlite3ValueFree((sqlite3_value*)p4);
66657        }else{
66658          Mem *p = (Mem*)p4;
66659          if( p->szMalloc ) sqlite3DbFree(db, p->zMalloc);
66660          sqlite3DbFree(db, p);
66661        }
66662        break;
66663      }
66664      case P4_VTAB : {
66665        if( db->pnBytesFreed==0 ) sqlite3VtabUnlock((VTable *)p4);
66666        break;
66667      }
66668    }
66669  }
66670}
66671
66672/*
66673** Free the space allocated for aOp and any p4 values allocated for the
66674** opcodes contained within. If aOp is not NULL it is assumed to contain
66675** nOp entries.
66676*/
66677static void vdbeFreeOpArray(sqlite3 *db, Op *aOp, int nOp){
66678  if( aOp ){
66679    Op *pOp;
66680    for(pOp=aOp; pOp<&aOp[nOp]; pOp++){
66681      freeP4(db, pOp->p4type, pOp->p4.p);
66682#ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
66683      sqlite3DbFree(db, pOp->zComment);
66684#endif
66685    }
66686  }
66687  sqlite3DbFree(db, aOp);
66688}
66689
66690/*
66691** Link the SubProgram object passed as the second argument into the linked
66692** list at Vdbe.pSubProgram. This list is used to delete all sub-program
66693** objects when the VM is no longer required.
66694*/
66695SQLITE_PRIVATE void sqlite3VdbeLinkSubProgram(Vdbe *pVdbe, SubProgram *p){
66696  p->pNext = pVdbe->pProgram;
66697  pVdbe->pProgram = p;
66698}
66699
66700/*
66701** Change the opcode at addr into OP_Noop
66702*/
66703SQLITE_PRIVATE void sqlite3VdbeChangeToNoop(Vdbe *p, int addr){
66704  if( addr<p->nOp ){
66705    VdbeOp *pOp = &p->aOp[addr];
66706    sqlite3 *db = p->db;
66707    freeP4(db, pOp->p4type, pOp->p4.p);
66708    memset(pOp, 0, sizeof(pOp[0]));
66709    pOp->opcode = OP_Noop;
66710    if( addr==p->nOp-1 ) p->nOp--;
66711  }
66712}
66713
66714/*
66715** If the last opcode is "op" and it is not a jump destination,
66716** then remove it.  Return true if and only if an opcode was removed.
66717*/
66718SQLITE_PRIVATE int sqlite3VdbeDeletePriorOpcode(Vdbe *p, u8 op){
66719  if( (p->nOp-1)>(p->pParse->iFixedOp) && p->aOp[p->nOp-1].opcode==op ){
66720    sqlite3VdbeChangeToNoop(p, p->nOp-1);
66721    return 1;
66722  }else{
66723    return 0;
66724  }
66725}
66726
66727/*
66728** Change the value of the P4 operand for a specific instruction.
66729** This routine is useful when a large program is loaded from a
66730** static array using sqlite3VdbeAddOpList but we want to make a
66731** few minor changes to the program.
66732**
66733** If n>=0 then the P4 operand is dynamic, meaning that a copy of
66734** the string is made into memory obtained from sqlite3_malloc().
66735** A value of n==0 means copy bytes of zP4 up to and including the
66736** first null byte.  If n>0 then copy n+1 bytes of zP4.
66737**
66738** Other values of n (P4_STATIC, P4_COLLSEQ etc.) indicate that zP4 points
66739** to a string or structure that is guaranteed to exist for the lifetime of
66740** the Vdbe. In these cases we can just copy the pointer.
66741**
66742** If addr<0 then change P4 on the most recently inserted instruction.
66743*/
66744SQLITE_PRIVATE void sqlite3VdbeChangeP4(Vdbe *p, int addr, const char *zP4, int n){
66745  Op *pOp;
66746  sqlite3 *db;
66747  assert( p!=0 );
66748  db = p->db;
66749  assert( p->magic==VDBE_MAGIC_INIT );
66750  if( p->aOp==0 || db->mallocFailed ){
66751    if( n!=P4_VTAB ){
66752      freeP4(db, n, (void*)*(char**)&zP4);
66753    }
66754    return;
66755  }
66756  assert( p->nOp>0 );
66757  assert( addr<p->nOp );
66758  if( addr<0 ){
66759    addr = p->nOp - 1;
66760  }
66761  pOp = &p->aOp[addr];
66762  assert( pOp->p4type==P4_NOTUSED
66763       || pOp->p4type==P4_INT32
66764       || pOp->p4type==P4_KEYINFO );
66765  freeP4(db, pOp->p4type, pOp->p4.p);
66766  pOp->p4.p = 0;
66767  if( n==P4_INT32 ){
66768    /* Note: this cast is safe, because the origin data point was an int
66769    ** that was cast to a (const char *). */
66770    pOp->p4.i = SQLITE_PTR_TO_INT(zP4);
66771    pOp->p4type = P4_INT32;
66772  }else if( zP4==0 ){
66773    pOp->p4.p = 0;
66774    pOp->p4type = P4_NOTUSED;
66775  }else if( n==P4_KEYINFO ){
66776    pOp->p4.p = (void*)zP4;
66777    pOp->p4type = P4_KEYINFO;
66778  }else if( n==P4_VTAB ){
66779    pOp->p4.p = (void*)zP4;
66780    pOp->p4type = P4_VTAB;
66781    sqlite3VtabLock((VTable *)zP4);
66782    assert( ((VTable *)zP4)->db==p->db );
66783  }else if( n<0 ){
66784    pOp->p4.p = (void*)zP4;
66785    pOp->p4type = (signed char)n;
66786  }else{
66787    if( n==0 ) n = sqlite3Strlen30(zP4);
66788    pOp->p4.z = sqlite3DbStrNDup(p->db, zP4, n);
66789    pOp->p4type = P4_DYNAMIC;
66790  }
66791}
66792
66793/*
66794** Set the P4 on the most recently added opcode to the KeyInfo for the
66795** index given.
66796*/
66797SQLITE_PRIVATE void sqlite3VdbeSetP4KeyInfo(Parse *pParse, Index *pIdx){
66798  Vdbe *v = pParse->pVdbe;
66799  assert( v!=0 );
66800  assert( pIdx!=0 );
66801  sqlite3VdbeChangeP4(v, -1, (char*)sqlite3KeyInfoOfIndex(pParse, pIdx),
66802                      P4_KEYINFO);
66803}
66804
66805#ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
66806/*
66807** Change the comment on the most recently coded instruction.  Or
66808** insert a No-op and add the comment to that new instruction.  This
66809** makes the code easier to read during debugging.  None of this happens
66810** in a production build.
66811*/
66812static void vdbeVComment(Vdbe *p, const char *zFormat, va_list ap){
66813  assert( p->nOp>0 || p->aOp==0 );
66814  assert( p->aOp==0 || p->aOp[p->nOp-1].zComment==0 || p->db->mallocFailed );
66815  if( p->nOp ){
66816    assert( p->aOp );
66817    sqlite3DbFree(p->db, p->aOp[p->nOp-1].zComment);
66818    p->aOp[p->nOp-1].zComment = sqlite3VMPrintf(p->db, zFormat, ap);
66819  }
66820}
66821SQLITE_PRIVATE void sqlite3VdbeComment(Vdbe *p, const char *zFormat, ...){
66822  va_list ap;
66823  if( p ){
66824    va_start(ap, zFormat);
66825    vdbeVComment(p, zFormat, ap);
66826    va_end(ap);
66827  }
66828}
66829SQLITE_PRIVATE void sqlite3VdbeNoopComment(Vdbe *p, const char *zFormat, ...){
66830  va_list ap;
66831  if( p ){
66832    sqlite3VdbeAddOp0(p, OP_Noop);
66833    va_start(ap, zFormat);
66834    vdbeVComment(p, zFormat, ap);
66835    va_end(ap);
66836  }
66837}
66838#endif  /* NDEBUG */
66839
66840#ifdef SQLITE_VDBE_COVERAGE
66841/*
66842** Set the value if the iSrcLine field for the previously coded instruction.
66843*/
66844SQLITE_PRIVATE void sqlite3VdbeSetLineNumber(Vdbe *v, int iLine){
66845  sqlite3VdbeGetOp(v,-1)->iSrcLine = iLine;
66846}
66847#endif /* SQLITE_VDBE_COVERAGE */
66848
66849/*
66850** Return the opcode for a given address.  If the address is -1, then
66851** return the most recently inserted opcode.
66852**
66853** If a memory allocation error has occurred prior to the calling of this
66854** routine, then a pointer to a dummy VdbeOp will be returned.  That opcode
66855** is readable but not writable, though it is cast to a writable value.
66856** The return of a dummy opcode allows the call to continue functioning
66857** after an OOM fault without having to check to see if the return from
66858** this routine is a valid pointer.  But because the dummy.opcode is 0,
66859** dummy will never be written to.  This is verified by code inspection and
66860** by running with Valgrind.
66861*/
66862SQLITE_PRIVATE VdbeOp *sqlite3VdbeGetOp(Vdbe *p, int addr){
66863  /* C89 specifies that the constant "dummy" will be initialized to all
66864  ** zeros, which is correct.  MSVC generates a warning, nevertheless. */
66865  static VdbeOp dummy;  /* Ignore the MSVC warning about no initializer */
66866  assert( p->magic==VDBE_MAGIC_INIT );
66867  if( addr<0 ){
66868    addr = p->nOp - 1;
66869  }
66870  assert( (addr>=0 && addr<p->nOp) || p->db->mallocFailed );
66871  if( p->db->mallocFailed ){
66872    return (VdbeOp*)&dummy;
66873  }else{
66874    return &p->aOp[addr];
66875  }
66876}
66877
66878#if defined(SQLITE_ENABLE_EXPLAIN_COMMENTS)
66879/*
66880** Return an integer value for one of the parameters to the opcode pOp
66881** determined by character c.
66882*/
66883static int translateP(char c, const Op *pOp){
66884  if( c=='1' ) return pOp->p1;
66885  if( c=='2' ) return pOp->p2;
66886  if( c=='3' ) return pOp->p3;
66887  if( c=='4' ) return pOp->p4.i;
66888  return pOp->p5;
66889}
66890
66891/*
66892** Compute a string for the "comment" field of a VDBE opcode listing.
66893**
66894** The Synopsis: field in comments in the vdbe.c source file gets converted
66895** to an extra string that is appended to the sqlite3OpcodeName().  In the
66896** absence of other comments, this synopsis becomes the comment on the opcode.
66897** Some translation occurs:
66898**
66899**       "PX"      ->  "r[X]"
66900**       "PX@PY"   ->  "r[X..X+Y-1]"  or "r[x]" if y is 0 or 1
66901**       "PX@PY+1" ->  "r[X..X+Y]"    or "r[x]" if y is 0
66902**       "PY..PY"  ->  "r[X..Y]"      or "r[x]" if y<=x
66903*/
66904static int displayComment(
66905  const Op *pOp,     /* The opcode to be commented */
66906  const char *zP4,   /* Previously obtained value for P4 */
66907  char *zTemp,       /* Write result here */
66908  int nTemp          /* Space available in zTemp[] */
66909){
66910  const char *zOpName;
66911  const char *zSynopsis;
66912  int nOpName;
66913  int ii, jj;
66914  zOpName = sqlite3OpcodeName(pOp->opcode);
66915  nOpName = sqlite3Strlen30(zOpName);
66916  if( zOpName[nOpName+1] ){
66917    int seenCom = 0;
66918    char c;
66919    zSynopsis = zOpName += nOpName + 1;
66920    for(ii=jj=0; jj<nTemp-1 && (c = zSynopsis[ii])!=0; ii++){
66921      if( c=='P' ){
66922        c = zSynopsis[++ii];
66923        if( c=='4' ){
66924          sqlite3_snprintf(nTemp-jj, zTemp+jj, "%s", zP4);
66925        }else if( c=='X' ){
66926          sqlite3_snprintf(nTemp-jj, zTemp+jj, "%s", pOp->zComment);
66927          seenCom = 1;
66928        }else{
66929          int v1 = translateP(c, pOp);
66930          int v2;
66931          sqlite3_snprintf(nTemp-jj, zTemp+jj, "%d", v1);
66932          if( strncmp(zSynopsis+ii+1, "@P", 2)==0 ){
66933            ii += 3;
66934            jj += sqlite3Strlen30(zTemp+jj);
66935            v2 = translateP(zSynopsis[ii], pOp);
66936            if( strncmp(zSynopsis+ii+1,"+1",2)==0 ){
66937              ii += 2;
66938              v2++;
66939            }
66940            if( v2>1 ){
66941              sqlite3_snprintf(nTemp-jj, zTemp+jj, "..%d", v1+v2-1);
66942            }
66943          }else if( strncmp(zSynopsis+ii+1, "..P3", 4)==0 && pOp->p3==0 ){
66944            ii += 4;
66945          }
66946        }
66947        jj += sqlite3Strlen30(zTemp+jj);
66948      }else{
66949        zTemp[jj++] = c;
66950      }
66951    }
66952    if( !seenCom && jj<nTemp-5 && pOp->zComment ){
66953      sqlite3_snprintf(nTemp-jj, zTemp+jj, "; %s", pOp->zComment);
66954      jj += sqlite3Strlen30(zTemp+jj);
66955    }
66956    if( jj<nTemp ) zTemp[jj] = 0;
66957  }else if( pOp->zComment ){
66958    sqlite3_snprintf(nTemp, zTemp, "%s", pOp->zComment);
66959    jj = sqlite3Strlen30(zTemp);
66960  }else{
66961    zTemp[0] = 0;
66962    jj = 0;
66963  }
66964  return jj;
66965}
66966#endif /* SQLITE_DEBUG */
66967
66968
66969#if !defined(SQLITE_OMIT_EXPLAIN) || !defined(NDEBUG) \
66970     || defined(VDBE_PROFILE) || defined(SQLITE_DEBUG)
66971/*
66972** Compute a string that describes the P4 parameter for an opcode.
66973** Use zTemp for any required temporary buffer space.
66974*/
66975static char *displayP4(Op *pOp, char *zTemp, int nTemp){
66976  char *zP4 = zTemp;
66977  assert( nTemp>=20 );
66978  switch( pOp->p4type ){
66979    case P4_KEYINFO: {
66980      int i, j;
66981      KeyInfo *pKeyInfo = pOp->p4.pKeyInfo;
66982      assert( pKeyInfo->aSortOrder!=0 );
66983      sqlite3_snprintf(nTemp, zTemp, "k(%d", pKeyInfo->nField);
66984      i = sqlite3Strlen30(zTemp);
66985      for(j=0; j<pKeyInfo->nField; j++){
66986        CollSeq *pColl = pKeyInfo->aColl[j];
66987        const char *zColl = pColl ? pColl->zName : "nil";
66988        int n = sqlite3Strlen30(zColl);
66989        if( n==6 && memcmp(zColl,"BINARY",6)==0 ){
66990          zColl = "B";
66991          n = 1;
66992        }
66993        if( i+n>nTemp-6 ){
66994          memcpy(&zTemp[i],",...",4);
66995          break;
66996        }
66997        zTemp[i++] = ',';
66998        if( pKeyInfo->aSortOrder[j] ){
66999          zTemp[i++] = '-';
67000        }
67001        memcpy(&zTemp[i], zColl, n+1);
67002        i += n;
67003      }
67004      zTemp[i++] = ')';
67005      zTemp[i] = 0;
67006      assert( i<nTemp );
67007      break;
67008    }
67009    case P4_COLLSEQ: {
67010      CollSeq *pColl = pOp->p4.pColl;
67011      sqlite3_snprintf(nTemp, zTemp, "(%.20s)", pColl->zName);
67012      break;
67013    }
67014    case P4_FUNCDEF: {
67015      FuncDef *pDef = pOp->p4.pFunc;
67016      sqlite3_snprintf(nTemp, zTemp, "%s(%d)", pDef->zName, pDef->nArg);
67017      break;
67018    }
67019#ifdef SQLITE_DEBUG
67020    case P4_FUNCCTX: {
67021      FuncDef *pDef = pOp->p4.pCtx->pFunc;
67022      sqlite3_snprintf(nTemp, zTemp, "%s(%d)", pDef->zName, pDef->nArg);
67023      break;
67024    }
67025#endif
67026    case P4_INT64: {
67027      sqlite3_snprintf(nTemp, zTemp, "%lld", *pOp->p4.pI64);
67028      break;
67029    }
67030    case P4_INT32: {
67031      sqlite3_snprintf(nTemp, zTemp, "%d", pOp->p4.i);
67032      break;
67033    }
67034    case P4_REAL: {
67035      sqlite3_snprintf(nTemp, zTemp, "%.16g", *pOp->p4.pReal);
67036      break;
67037    }
67038    case P4_MEM: {
67039      Mem *pMem = pOp->p4.pMem;
67040      if( pMem->flags & MEM_Str ){
67041        zP4 = pMem->z;
67042      }else if( pMem->flags & MEM_Int ){
67043        sqlite3_snprintf(nTemp, zTemp, "%lld", pMem->u.i);
67044      }else if( pMem->flags & MEM_Real ){
67045        sqlite3_snprintf(nTemp, zTemp, "%.16g", pMem->u.r);
67046      }else if( pMem->flags & MEM_Null ){
67047        sqlite3_snprintf(nTemp, zTemp, "NULL");
67048      }else{
67049        assert( pMem->flags & MEM_Blob );
67050        zP4 = "(blob)";
67051      }
67052      break;
67053    }
67054#ifndef SQLITE_OMIT_VIRTUALTABLE
67055    case P4_VTAB: {
67056      sqlite3_vtab *pVtab = pOp->p4.pVtab->pVtab;
67057      sqlite3_snprintf(nTemp, zTemp, "vtab:%p", pVtab);
67058      break;
67059    }
67060#endif
67061    case P4_INTARRAY: {
67062      sqlite3_snprintf(nTemp, zTemp, "intarray");
67063      break;
67064    }
67065    case P4_SUBPROGRAM: {
67066      sqlite3_snprintf(nTemp, zTemp, "program");
67067      break;
67068    }
67069    case P4_ADVANCE: {
67070      zTemp[0] = 0;
67071      break;
67072    }
67073    default: {
67074      zP4 = pOp->p4.z;
67075      if( zP4==0 ){
67076        zP4 = zTemp;
67077        zTemp[0] = 0;
67078      }
67079    }
67080  }
67081  assert( zP4!=0 );
67082  return zP4;
67083}
67084#endif
67085
67086/*
67087** Declare to the Vdbe that the BTree object at db->aDb[i] is used.
67088**
67089** The prepared statements need to know in advance the complete set of
67090** attached databases that will be use.  A mask of these databases
67091** is maintained in p->btreeMask.  The p->lockMask value is the subset of
67092** p->btreeMask of databases that will require a lock.
67093*/
67094SQLITE_PRIVATE void sqlite3VdbeUsesBtree(Vdbe *p, int i){
67095  assert( i>=0 && i<p->db->nDb && i<(int)sizeof(yDbMask)*8 );
67096  assert( i<(int)sizeof(p->btreeMask)*8 );
67097  DbMaskSet(p->btreeMask, i);
67098  if( i!=1 && sqlite3BtreeSharable(p->db->aDb[i].pBt) ){
67099    DbMaskSet(p->lockMask, i);
67100  }
67101}
67102
67103#if !defined(SQLITE_OMIT_SHARED_CACHE) && SQLITE_THREADSAFE>0
67104/*
67105** If SQLite is compiled to support shared-cache mode and to be threadsafe,
67106** this routine obtains the mutex associated with each BtShared structure
67107** that may be accessed by the VM passed as an argument. In doing so it also
67108** sets the BtShared.db member of each of the BtShared structures, ensuring
67109** that the correct busy-handler callback is invoked if required.
67110**
67111** If SQLite is not threadsafe but does support shared-cache mode, then
67112** sqlite3BtreeEnter() is invoked to set the BtShared.db variables
67113** of all of BtShared structures accessible via the database handle
67114** associated with the VM.
67115**
67116** If SQLite is not threadsafe and does not support shared-cache mode, this
67117** function is a no-op.
67118**
67119** The p->btreeMask field is a bitmask of all btrees that the prepared
67120** statement p will ever use.  Let N be the number of bits in p->btreeMask
67121** corresponding to btrees that use shared cache.  Then the runtime of
67122** this routine is N*N.  But as N is rarely more than 1, this should not
67123** be a problem.
67124*/
67125SQLITE_PRIVATE void sqlite3VdbeEnter(Vdbe *p){
67126  int i;
67127  sqlite3 *db;
67128  Db *aDb;
67129  int nDb;
67130  if( DbMaskAllZero(p->lockMask) ) return;  /* The common case */
67131  db = p->db;
67132  aDb = db->aDb;
67133  nDb = db->nDb;
67134  for(i=0; i<nDb; i++){
67135    if( i!=1 && DbMaskTest(p->lockMask,i) && ALWAYS(aDb[i].pBt!=0) ){
67136      sqlite3BtreeEnter(aDb[i].pBt);
67137    }
67138  }
67139}
67140#endif
67141
67142#if !defined(SQLITE_OMIT_SHARED_CACHE) && SQLITE_THREADSAFE>0
67143/*
67144** Unlock all of the btrees previously locked by a call to sqlite3VdbeEnter().
67145*/
67146static SQLITE_NOINLINE void vdbeLeave(Vdbe *p){
67147  int i;
67148  sqlite3 *db;
67149  Db *aDb;
67150  int nDb;
67151  db = p->db;
67152  aDb = db->aDb;
67153  nDb = db->nDb;
67154  for(i=0; i<nDb; i++){
67155    if( i!=1 && DbMaskTest(p->lockMask,i) && ALWAYS(aDb[i].pBt!=0) ){
67156      sqlite3BtreeLeave(aDb[i].pBt);
67157    }
67158  }
67159}
67160SQLITE_PRIVATE void sqlite3VdbeLeave(Vdbe *p){
67161  if( DbMaskAllZero(p->lockMask) ) return;  /* The common case */
67162  vdbeLeave(p);
67163}
67164#endif
67165
67166#if defined(VDBE_PROFILE) || defined(SQLITE_DEBUG)
67167/*
67168** Print a single opcode.  This routine is used for debugging only.
67169*/
67170SQLITE_PRIVATE void sqlite3VdbePrintOp(FILE *pOut, int pc, Op *pOp){
67171  char *zP4;
67172  char zPtr[50];
67173  char zCom[100];
67174  static const char *zFormat1 = "%4d %-13s %4d %4d %4d %-13s %.2X %s\n";
67175  if( pOut==0 ) pOut = stdout;
67176  zP4 = displayP4(pOp, zPtr, sizeof(zPtr));
67177#ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
67178  displayComment(pOp, zP4, zCom, sizeof(zCom));
67179#else
67180  zCom[0] = 0;
67181#endif
67182  /* NB:  The sqlite3OpcodeName() function is implemented by code created
67183  ** by the mkopcodeh.awk and mkopcodec.awk scripts which extract the
67184  ** information from the vdbe.c source text */
67185  fprintf(pOut, zFormat1, pc,
67186      sqlite3OpcodeName(pOp->opcode), pOp->p1, pOp->p2, pOp->p3, zP4, pOp->p5,
67187      zCom
67188  );
67189  fflush(pOut);
67190}
67191#endif
67192
67193/*
67194** Release an array of N Mem elements
67195*/
67196static void releaseMemArray(Mem *p, int N){
67197  if( p && N ){
67198    Mem *pEnd = &p[N];
67199    sqlite3 *db = p->db;
67200    u8 malloc_failed = db->mallocFailed;
67201    if( db->pnBytesFreed ){
67202      do{
67203        if( p->szMalloc ) sqlite3DbFree(db, p->zMalloc);
67204      }while( (++p)<pEnd );
67205      return;
67206    }
67207    do{
67208      assert( (&p[1])==pEnd || p[0].db==p[1].db );
67209      assert( sqlite3VdbeCheckMemInvariants(p) );
67210
67211      /* This block is really an inlined version of sqlite3VdbeMemRelease()
67212      ** that takes advantage of the fact that the memory cell value is
67213      ** being set to NULL after releasing any dynamic resources.
67214      **
67215      ** The justification for duplicating code is that according to
67216      ** callgrind, this causes a certain test case to hit the CPU 4.7
67217      ** percent less (x86 linux, gcc version 4.1.2, -O6) than if
67218      ** sqlite3MemRelease() were called from here. With -O2, this jumps
67219      ** to 6.6 percent. The test case is inserting 1000 rows into a table
67220      ** with no indexes using a single prepared INSERT statement, bind()
67221      ** and reset(). Inserts are grouped into a transaction.
67222      */
67223      testcase( p->flags & MEM_Agg );
67224      testcase( p->flags & MEM_Dyn );
67225      testcase( p->flags & MEM_Frame );
67226      testcase( p->flags & MEM_RowSet );
67227      if( p->flags&(MEM_Agg|MEM_Dyn|MEM_Frame|MEM_RowSet) ){
67228        sqlite3VdbeMemRelease(p);
67229      }else if( p->szMalloc ){
67230        sqlite3DbFree(db, p->zMalloc);
67231        p->szMalloc = 0;
67232      }
67233
67234      p->flags = MEM_Undefined;
67235    }while( (++p)<pEnd );
67236    db->mallocFailed = malloc_failed;
67237  }
67238}
67239
67240/*
67241** Delete a VdbeFrame object and its contents. VdbeFrame objects are
67242** allocated by the OP_Program opcode in sqlite3VdbeExec().
67243*/
67244SQLITE_PRIVATE void sqlite3VdbeFrameDelete(VdbeFrame *p){
67245  int i;
67246  Mem *aMem = VdbeFrameMem(p);
67247  VdbeCursor **apCsr = (VdbeCursor **)&aMem[p->nChildMem];
67248  for(i=0; i<p->nChildCsr; i++){
67249    sqlite3VdbeFreeCursor(p->v, apCsr[i]);
67250  }
67251  releaseMemArray(aMem, p->nChildMem);
67252  sqlite3DbFree(p->v->db, p);
67253}
67254
67255#ifndef SQLITE_OMIT_EXPLAIN
67256/*
67257** Give a listing of the program in the virtual machine.
67258**
67259** The interface is the same as sqlite3VdbeExec().  But instead of
67260** running the code, it invokes the callback once for each instruction.
67261** This feature is used to implement "EXPLAIN".
67262**
67263** When p->explain==1, each instruction is listed.  When
67264** p->explain==2, only OP_Explain instructions are listed and these
67265** are shown in a different format.  p->explain==2 is used to implement
67266** EXPLAIN QUERY PLAN.
67267**
67268** When p->explain==1, first the main program is listed, then each of
67269** the trigger subprograms are listed one by one.
67270*/
67271SQLITE_PRIVATE int sqlite3VdbeList(
67272  Vdbe *p                   /* The VDBE */
67273){
67274  int nRow;                            /* Stop when row count reaches this */
67275  int nSub = 0;                        /* Number of sub-vdbes seen so far */
67276  SubProgram **apSub = 0;              /* Array of sub-vdbes */
67277  Mem *pSub = 0;                       /* Memory cell hold array of subprogs */
67278  sqlite3 *db = p->db;                 /* The database connection */
67279  int i;                               /* Loop counter */
67280  int rc = SQLITE_OK;                  /* Return code */
67281  Mem *pMem = &p->aMem[1];             /* First Mem of result set */
67282
67283  assert( p->explain );
67284  assert( p->magic==VDBE_MAGIC_RUN );
67285  assert( p->rc==SQLITE_OK || p->rc==SQLITE_BUSY || p->rc==SQLITE_NOMEM );
67286
67287  /* Even though this opcode does not use dynamic strings for
67288  ** the result, result columns may become dynamic if the user calls
67289  ** sqlite3_column_text16(), causing a translation to UTF-16 encoding.
67290  */
67291  releaseMemArray(pMem, 8);
67292  p->pResultSet = 0;
67293
67294  if( p->rc==SQLITE_NOMEM ){
67295    /* This happens if a malloc() inside a call to sqlite3_column_text() or
67296    ** sqlite3_column_text16() failed.  */
67297    db->mallocFailed = 1;
67298    return SQLITE_ERROR;
67299  }
67300
67301  /* When the number of output rows reaches nRow, that means the
67302  ** listing has finished and sqlite3_step() should return SQLITE_DONE.
67303  ** nRow is the sum of the number of rows in the main program, plus
67304  ** the sum of the number of rows in all trigger subprograms encountered
67305  ** so far.  The nRow value will increase as new trigger subprograms are
67306  ** encountered, but p->pc will eventually catch up to nRow.
67307  */
67308  nRow = p->nOp;
67309  if( p->explain==1 ){
67310    /* The first 8 memory cells are used for the result set.  So we will
67311    ** commandeer the 9th cell to use as storage for an array of pointers
67312    ** to trigger subprograms.  The VDBE is guaranteed to have at least 9
67313    ** cells.  */
67314    assert( p->nMem>9 );
67315    pSub = &p->aMem[9];
67316    if( pSub->flags&MEM_Blob ){
67317      /* On the first call to sqlite3_step(), pSub will hold a NULL.  It is
67318      ** initialized to a BLOB by the P4_SUBPROGRAM processing logic below */
67319      nSub = pSub->n/sizeof(Vdbe*);
67320      apSub = (SubProgram **)pSub->z;
67321    }
67322    for(i=0; i<nSub; i++){
67323      nRow += apSub[i]->nOp;
67324    }
67325  }
67326
67327  do{
67328    i = p->pc++;
67329  }while( i<nRow && p->explain==2 && p->aOp[i].opcode!=OP_Explain );
67330  if( i>=nRow ){
67331    p->rc = SQLITE_OK;
67332    rc = SQLITE_DONE;
67333  }else if( db->u1.isInterrupted ){
67334    p->rc = SQLITE_INTERRUPT;
67335    rc = SQLITE_ERROR;
67336    sqlite3VdbeError(p, sqlite3ErrStr(p->rc));
67337  }else{
67338    char *zP4;
67339    Op *pOp;
67340    if( i<p->nOp ){
67341      /* The output line number is small enough that we are still in the
67342      ** main program. */
67343      pOp = &p->aOp[i];
67344    }else{
67345      /* We are currently listing subprograms.  Figure out which one and
67346      ** pick up the appropriate opcode. */
67347      int j;
67348      i -= p->nOp;
67349      for(j=0; i>=apSub[j]->nOp; j++){
67350        i -= apSub[j]->nOp;
67351      }
67352      pOp = &apSub[j]->aOp[i];
67353    }
67354    if( p->explain==1 ){
67355      pMem->flags = MEM_Int;
67356      pMem->u.i = i;                                /* Program counter */
67357      pMem++;
67358
67359      pMem->flags = MEM_Static|MEM_Str|MEM_Term;
67360      pMem->z = (char*)sqlite3OpcodeName(pOp->opcode); /* Opcode */
67361      assert( pMem->z!=0 );
67362      pMem->n = sqlite3Strlen30(pMem->z);
67363      pMem->enc = SQLITE_UTF8;
67364      pMem++;
67365
67366      /* When an OP_Program opcode is encounter (the only opcode that has
67367      ** a P4_SUBPROGRAM argument), expand the size of the array of subprograms
67368      ** kept in p->aMem[9].z to hold the new program - assuming this subprogram
67369      ** has not already been seen.
67370      */
67371      if( pOp->p4type==P4_SUBPROGRAM ){
67372        int nByte = (nSub+1)*sizeof(SubProgram*);
67373        int j;
67374        for(j=0; j<nSub; j++){
67375          if( apSub[j]==pOp->p4.pProgram ) break;
67376        }
67377        if( j==nSub && SQLITE_OK==sqlite3VdbeMemGrow(pSub, nByte, nSub!=0) ){
67378          apSub = (SubProgram **)pSub->z;
67379          apSub[nSub++] = pOp->p4.pProgram;
67380          pSub->flags |= MEM_Blob;
67381          pSub->n = nSub*sizeof(SubProgram*);
67382        }
67383      }
67384    }
67385
67386    pMem->flags = MEM_Int;
67387    pMem->u.i = pOp->p1;                          /* P1 */
67388    pMem++;
67389
67390    pMem->flags = MEM_Int;
67391    pMem->u.i = pOp->p2;                          /* P2 */
67392    pMem++;
67393
67394    pMem->flags = MEM_Int;
67395    pMem->u.i = pOp->p3;                          /* P3 */
67396    pMem++;
67397
67398    if( sqlite3VdbeMemClearAndResize(pMem, 32) ){ /* P4 */
67399      assert( p->db->mallocFailed );
67400      return SQLITE_ERROR;
67401    }
67402    pMem->flags = MEM_Str|MEM_Term;
67403    zP4 = displayP4(pOp, pMem->z, 32);
67404    if( zP4!=pMem->z ){
67405      sqlite3VdbeMemSetStr(pMem, zP4, -1, SQLITE_UTF8, 0);
67406    }else{
67407      assert( pMem->z!=0 );
67408      pMem->n = sqlite3Strlen30(pMem->z);
67409      pMem->enc = SQLITE_UTF8;
67410    }
67411    pMem++;
67412
67413    if( p->explain==1 ){
67414      if( sqlite3VdbeMemClearAndResize(pMem, 4) ){
67415        assert( p->db->mallocFailed );
67416        return SQLITE_ERROR;
67417      }
67418      pMem->flags = MEM_Str|MEM_Term;
67419      pMem->n = 2;
67420      sqlite3_snprintf(3, pMem->z, "%.2x", pOp->p5);   /* P5 */
67421      pMem->enc = SQLITE_UTF8;
67422      pMem++;
67423
67424#ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
67425      if( sqlite3VdbeMemClearAndResize(pMem, 500) ){
67426        assert( p->db->mallocFailed );
67427        return SQLITE_ERROR;
67428      }
67429      pMem->flags = MEM_Str|MEM_Term;
67430      pMem->n = displayComment(pOp, zP4, pMem->z, 500);
67431      pMem->enc = SQLITE_UTF8;
67432#else
67433      pMem->flags = MEM_Null;                       /* Comment */
67434#endif
67435    }
67436
67437    p->nResColumn = 8 - 4*(p->explain-1);
67438    p->pResultSet = &p->aMem[1];
67439    p->rc = SQLITE_OK;
67440    rc = SQLITE_ROW;
67441  }
67442  return rc;
67443}
67444#endif /* SQLITE_OMIT_EXPLAIN */
67445
67446#ifdef SQLITE_DEBUG
67447/*
67448** Print the SQL that was used to generate a VDBE program.
67449*/
67450SQLITE_PRIVATE void sqlite3VdbePrintSql(Vdbe *p){
67451  const char *z = 0;
67452  if( p->zSql ){
67453    z = p->zSql;
67454  }else if( p->nOp>=1 ){
67455    const VdbeOp *pOp = &p->aOp[0];
67456    if( pOp->opcode==OP_Init && pOp->p4.z!=0 ){
67457      z = pOp->p4.z;
67458      while( sqlite3Isspace(*z) ) z++;
67459    }
67460  }
67461  if( z ) printf("SQL: [%s]\n", z);
67462}
67463#endif
67464
67465#if !defined(SQLITE_OMIT_TRACE) && defined(SQLITE_ENABLE_IOTRACE)
67466/*
67467** Print an IOTRACE message showing SQL content.
67468*/
67469SQLITE_PRIVATE void sqlite3VdbeIOTraceSql(Vdbe *p){
67470  int nOp = p->nOp;
67471  VdbeOp *pOp;
67472  if( sqlite3IoTrace==0 ) return;
67473  if( nOp<1 ) return;
67474  pOp = &p->aOp[0];
67475  if( pOp->opcode==OP_Init && pOp->p4.z!=0 ){
67476    int i, j;
67477    char z[1000];
67478    sqlite3_snprintf(sizeof(z), z, "%s", pOp->p4.z);
67479    for(i=0; sqlite3Isspace(z[i]); i++){}
67480    for(j=0; z[i]; i++){
67481      if( sqlite3Isspace(z[i]) ){
67482        if( z[i-1]!=' ' ){
67483          z[j++] = ' ';
67484        }
67485      }else{
67486        z[j++] = z[i];
67487      }
67488    }
67489    z[j] = 0;
67490    sqlite3IoTrace("SQL %s\n", z);
67491  }
67492}
67493#endif /* !SQLITE_OMIT_TRACE && SQLITE_ENABLE_IOTRACE */
67494
67495/*
67496** Allocate space from a fixed size buffer and return a pointer to
67497** that space.  If insufficient space is available, return NULL.
67498**
67499** The pBuf parameter is the initial value of a pointer which will
67500** receive the new memory.  pBuf is normally NULL.  If pBuf is not
67501** NULL, it means that memory space has already been allocated and that
67502** this routine should not allocate any new memory.  When pBuf is not
67503** NULL simply return pBuf.  Only allocate new memory space when pBuf
67504** is NULL.
67505**
67506** nByte is the number of bytes of space needed.
67507**
67508** *ppFrom points to available space and pEnd points to the end of the
67509** available space.  When space is allocated, *ppFrom is advanced past
67510** the end of the allocated space.
67511**
67512** *pnByte is a counter of the number of bytes of space that have failed
67513** to allocate.  If there is insufficient space in *ppFrom to satisfy the
67514** request, then increment *pnByte by the amount of the request.
67515*/
67516static void *allocSpace(
67517  void *pBuf,          /* Where return pointer will be stored */
67518  int nByte,           /* Number of bytes to allocate */
67519  u8 **ppFrom,         /* IN/OUT: Allocate from *ppFrom */
67520  u8 *pEnd,            /* Pointer to 1 byte past the end of *ppFrom buffer */
67521  int *pnByte          /* If allocation cannot be made, increment *pnByte */
67522){
67523  assert( EIGHT_BYTE_ALIGNMENT(*ppFrom) );
67524  if( pBuf ) return pBuf;
67525  nByte = ROUND8(nByte);
67526  if( &(*ppFrom)[nByte] <= pEnd ){
67527    pBuf = (void*)*ppFrom;
67528    *ppFrom += nByte;
67529  }else{
67530    *pnByte += nByte;
67531  }
67532  return pBuf;
67533}
67534
67535/*
67536** Rewind the VDBE back to the beginning in preparation for
67537** running it.
67538*/
67539SQLITE_PRIVATE void sqlite3VdbeRewind(Vdbe *p){
67540#if defined(SQLITE_DEBUG) || defined(VDBE_PROFILE)
67541  int i;
67542#endif
67543  assert( p!=0 );
67544  assert( p->magic==VDBE_MAGIC_INIT );
67545
67546  /* There should be at least one opcode.
67547  */
67548  assert( p->nOp>0 );
67549
67550  /* Set the magic to VDBE_MAGIC_RUN sooner rather than later. */
67551  p->magic = VDBE_MAGIC_RUN;
67552
67553#ifdef SQLITE_DEBUG
67554  for(i=1; i<p->nMem; i++){
67555    assert( p->aMem[i].db==p->db );
67556  }
67557#endif
67558  p->pc = -1;
67559  p->rc = SQLITE_OK;
67560  p->errorAction = OE_Abort;
67561  p->magic = VDBE_MAGIC_RUN;
67562  p->nChange = 0;
67563  p->cacheCtr = 1;
67564  p->minWriteFileFormat = 255;
67565  p->iStatement = 0;
67566  p->nFkConstraint = 0;
67567#ifdef VDBE_PROFILE
67568  for(i=0; i<p->nOp; i++){
67569    p->aOp[i].cnt = 0;
67570    p->aOp[i].cycles = 0;
67571  }
67572#endif
67573}
67574
67575/*
67576** Prepare a virtual machine for execution for the first time after
67577** creating the virtual machine.  This involves things such
67578** as allocating registers and initializing the program counter.
67579** After the VDBE has be prepped, it can be executed by one or more
67580** calls to sqlite3VdbeExec().
67581**
67582** This function may be called exactly once on each virtual machine.
67583** After this routine is called the VM has been "packaged" and is ready
67584** to run.  After this routine is called, further calls to
67585** sqlite3VdbeAddOp() functions are prohibited.  This routine disconnects
67586** the Vdbe from the Parse object that helped generate it so that the
67587** the Vdbe becomes an independent entity and the Parse object can be
67588** destroyed.
67589**
67590** Use the sqlite3VdbeRewind() procedure to restore a virtual machine back
67591** to its initial state after it has been run.
67592*/
67593SQLITE_PRIVATE void sqlite3VdbeMakeReady(
67594  Vdbe *p,                       /* The VDBE */
67595  Parse *pParse                  /* Parsing context */
67596){
67597  sqlite3 *db;                   /* The database connection */
67598  int nVar;                      /* Number of parameters */
67599  int nMem;                      /* Number of VM memory registers */
67600  int nCursor;                   /* Number of cursors required */
67601  int nArg;                      /* Number of arguments in subprograms */
67602  int nOnce;                     /* Number of OP_Once instructions */
67603  int n;                         /* Loop counter */
67604  u8 *zCsr;                      /* Memory available for allocation */
67605  u8 *zEnd;                      /* First byte past allocated memory */
67606  int nByte;                     /* How much extra memory is needed */
67607
67608  assert( p!=0 );
67609  assert( p->nOp>0 );
67610  assert( pParse!=0 );
67611  assert( p->magic==VDBE_MAGIC_INIT );
67612  assert( pParse==p->pParse );
67613  db = p->db;
67614  assert( db->mallocFailed==0 );
67615  nVar = pParse->nVar;
67616  nMem = pParse->nMem;
67617  nCursor = pParse->nTab;
67618  nArg = pParse->nMaxArg;
67619  nOnce = pParse->nOnce;
67620  if( nOnce==0 ) nOnce = 1; /* Ensure at least one byte in p->aOnceFlag[] */
67621
67622  /* For each cursor required, also allocate a memory cell. Memory
67623  ** cells (nMem+1-nCursor)..nMem, inclusive, will never be used by
67624  ** the vdbe program. Instead they are used to allocate space for
67625  ** VdbeCursor/BtCursor structures. The blob of memory associated with
67626  ** cursor 0 is stored in memory cell nMem. Memory cell (nMem-1)
67627  ** stores the blob of memory associated with cursor 1, etc.
67628  **
67629  ** See also: allocateCursor().
67630  */
67631  nMem += nCursor;
67632
67633  /* Allocate space for memory registers, SQL variables, VDBE cursors and
67634  ** an array to marshal SQL function arguments in.
67635  */
67636  zCsr = (u8*)&p->aOp[p->nOp];            /* Memory avaliable for allocation */
67637  zEnd = (u8*)&p->aOp[pParse->nOpAlloc];  /* First byte past end of zCsr[] */
67638
67639  resolveP2Values(p, &nArg);
67640  p->usesStmtJournal = (u8)(pParse->isMultiWrite && pParse->mayAbort);
67641  if( pParse->explain && nMem<10 ){
67642    nMem = 10;
67643  }
67644  memset(zCsr, 0, zEnd-zCsr);
67645  zCsr += (zCsr - (u8*)0)&7;
67646  assert( EIGHT_BYTE_ALIGNMENT(zCsr) );
67647  p->expired = 0;
67648
67649  /* Memory for registers, parameters, cursor, etc, is allocated in two
67650  ** passes.  On the first pass, we try to reuse unused space at the
67651  ** end of the opcode array.  If we are unable to satisfy all memory
67652  ** requirements by reusing the opcode array tail, then the second
67653  ** pass will fill in the rest using a fresh allocation.
67654  **
67655  ** This two-pass approach that reuses as much memory as possible from
67656  ** the leftover space at the end of the opcode array can significantly
67657  ** reduce the amount of memory held by a prepared statement.
67658  */
67659  do {
67660    nByte = 0;
67661    p->aMem = allocSpace(p->aMem, nMem*sizeof(Mem), &zCsr, zEnd, &nByte);
67662    p->aVar = allocSpace(p->aVar, nVar*sizeof(Mem), &zCsr, zEnd, &nByte);
67663    p->apArg = allocSpace(p->apArg, nArg*sizeof(Mem*), &zCsr, zEnd, &nByte);
67664    p->azVar = allocSpace(p->azVar, nVar*sizeof(char*), &zCsr, zEnd, &nByte);
67665    p->apCsr = allocSpace(p->apCsr, nCursor*sizeof(VdbeCursor*),
67666                          &zCsr, zEnd, &nByte);
67667    p->aOnceFlag = allocSpace(p->aOnceFlag, nOnce, &zCsr, zEnd, &nByte);
67668#ifdef SQLITE_ENABLE_STMT_SCANSTATUS
67669    p->anExec = allocSpace(p->anExec, p->nOp*sizeof(i64), &zCsr, zEnd, &nByte);
67670#endif
67671    if( nByte ){
67672      p->pFree = sqlite3DbMallocZero(db, nByte);
67673    }
67674    zCsr = p->pFree;
67675    zEnd = &zCsr[nByte];
67676  }while( nByte && !db->mallocFailed );
67677
67678  p->nCursor = nCursor;
67679  p->nOnceFlag = nOnce;
67680  if( p->aVar ){
67681    p->nVar = (ynVar)nVar;
67682    for(n=0; n<nVar; n++){
67683      p->aVar[n].flags = MEM_Null;
67684      p->aVar[n].db = db;
67685    }
67686  }
67687  if( p->azVar && pParse->nzVar>0 ){
67688    p->nzVar = pParse->nzVar;
67689    memcpy(p->azVar, pParse->azVar, p->nzVar*sizeof(p->azVar[0]));
67690    memset(pParse->azVar, 0, pParse->nzVar*sizeof(pParse->azVar[0]));
67691  }
67692  if( p->aMem ){
67693    p->aMem--;                      /* aMem[] goes from 1..nMem */
67694    p->nMem = nMem;                 /*       not from 0..nMem-1 */
67695    for(n=1; n<=nMem; n++){
67696      p->aMem[n].flags = MEM_Undefined;
67697      p->aMem[n].db = db;
67698    }
67699  }
67700  p->explain = pParse->explain;
67701  sqlite3VdbeRewind(p);
67702}
67703
67704/*
67705** Close a VDBE cursor and release all the resources that cursor
67706** happens to hold.
67707*/
67708SQLITE_PRIVATE void sqlite3VdbeFreeCursor(Vdbe *p, VdbeCursor *pCx){
67709  if( pCx==0 ){
67710    return;
67711  }
67712  sqlite3VdbeSorterClose(p->db, pCx);
67713  if( pCx->pBt ){
67714    sqlite3BtreeClose(pCx->pBt);
67715    /* The pCx->pCursor will be close automatically, if it exists, by
67716    ** the call above. */
67717  }else if( pCx->pCursor ){
67718    sqlite3BtreeCloseCursor(pCx->pCursor);
67719  }
67720#ifndef SQLITE_OMIT_VIRTUALTABLE
67721  else if( pCx->pVtabCursor ){
67722    sqlite3_vtab_cursor *pVtabCursor = pCx->pVtabCursor;
67723    const sqlite3_module *pModule = pVtabCursor->pVtab->pModule;
67724    assert( pVtabCursor->pVtab->nRef>0 );
67725    pVtabCursor->pVtab->nRef--;
67726    pModule->xClose(pVtabCursor);
67727  }
67728#endif
67729}
67730
67731/*
67732** Close all cursors in the current frame.
67733*/
67734static void closeCursorsInFrame(Vdbe *p){
67735  if( p->apCsr ){
67736    int i;
67737    for(i=0; i<p->nCursor; i++){
67738      VdbeCursor *pC = p->apCsr[i];
67739      if( pC ){
67740        sqlite3VdbeFreeCursor(p, pC);
67741        p->apCsr[i] = 0;
67742      }
67743    }
67744  }
67745}
67746
67747/*
67748** Copy the values stored in the VdbeFrame structure to its Vdbe. This
67749** is used, for example, when a trigger sub-program is halted to restore
67750** control to the main program.
67751*/
67752SQLITE_PRIVATE int sqlite3VdbeFrameRestore(VdbeFrame *pFrame){
67753  Vdbe *v = pFrame->v;
67754  closeCursorsInFrame(v);
67755#ifdef SQLITE_ENABLE_STMT_SCANSTATUS
67756  v->anExec = pFrame->anExec;
67757#endif
67758  v->aOnceFlag = pFrame->aOnceFlag;
67759  v->nOnceFlag = pFrame->nOnceFlag;
67760  v->aOp = pFrame->aOp;
67761  v->nOp = pFrame->nOp;
67762  v->aMem = pFrame->aMem;
67763  v->nMem = pFrame->nMem;
67764  v->apCsr = pFrame->apCsr;
67765  v->nCursor = pFrame->nCursor;
67766  v->db->lastRowid = pFrame->lastRowid;
67767  v->nChange = pFrame->nChange;
67768  v->db->nChange = pFrame->nDbChange;
67769  return pFrame->pc;
67770}
67771
67772/*
67773** Close all cursors.
67774**
67775** Also release any dynamic memory held by the VM in the Vdbe.aMem memory
67776** cell array. This is necessary as the memory cell array may contain
67777** pointers to VdbeFrame objects, which may in turn contain pointers to
67778** open cursors.
67779*/
67780static void closeAllCursors(Vdbe *p){
67781  if( p->pFrame ){
67782    VdbeFrame *pFrame;
67783    for(pFrame=p->pFrame; pFrame->pParent; pFrame=pFrame->pParent);
67784    sqlite3VdbeFrameRestore(pFrame);
67785    p->pFrame = 0;
67786    p->nFrame = 0;
67787  }
67788  assert( p->nFrame==0 );
67789  closeCursorsInFrame(p);
67790  if( p->aMem ){
67791    releaseMemArray(&p->aMem[1], p->nMem);
67792  }
67793  while( p->pDelFrame ){
67794    VdbeFrame *pDel = p->pDelFrame;
67795    p->pDelFrame = pDel->pParent;
67796    sqlite3VdbeFrameDelete(pDel);
67797  }
67798
67799  /* Delete any auxdata allocations made by the VM */
67800  if( p->pAuxData ) sqlite3VdbeDeleteAuxData(p, -1, 0);
67801  assert( p->pAuxData==0 );
67802}
67803
67804/*
67805** Clean up the VM after a single run.
67806*/
67807static void Cleanup(Vdbe *p){
67808  sqlite3 *db = p->db;
67809
67810#ifdef SQLITE_DEBUG
67811  /* Execute assert() statements to ensure that the Vdbe.apCsr[] and
67812  ** Vdbe.aMem[] arrays have already been cleaned up.  */
67813  int i;
67814  if( p->apCsr ) for(i=0; i<p->nCursor; i++) assert( p->apCsr[i]==0 );
67815  if( p->aMem ){
67816    for(i=1; i<=p->nMem; i++) assert( p->aMem[i].flags==MEM_Undefined );
67817  }
67818#endif
67819
67820  sqlite3DbFree(db, p->zErrMsg);
67821  p->zErrMsg = 0;
67822  p->pResultSet = 0;
67823}
67824
67825/*
67826** Set the number of result columns that will be returned by this SQL
67827** statement. This is now set at compile time, rather than during
67828** execution of the vdbe program so that sqlite3_column_count() can
67829** be called on an SQL statement before sqlite3_step().
67830*/
67831SQLITE_PRIVATE void sqlite3VdbeSetNumCols(Vdbe *p, int nResColumn){
67832  Mem *pColName;
67833  int n;
67834  sqlite3 *db = p->db;
67835
67836  releaseMemArray(p->aColName, p->nResColumn*COLNAME_N);
67837  sqlite3DbFree(db, p->aColName);
67838  n = nResColumn*COLNAME_N;
67839  p->nResColumn = (u16)nResColumn;
67840  p->aColName = pColName = (Mem*)sqlite3DbMallocZero(db, sizeof(Mem)*n );
67841  if( p->aColName==0 ) return;
67842  while( n-- > 0 ){
67843    pColName->flags = MEM_Null;
67844    pColName->db = p->db;
67845    pColName++;
67846  }
67847}
67848
67849/*
67850** Set the name of the idx'th column to be returned by the SQL statement.
67851** zName must be a pointer to a nul terminated string.
67852**
67853** This call must be made after a call to sqlite3VdbeSetNumCols().
67854**
67855** The final parameter, xDel, must be one of SQLITE_DYNAMIC, SQLITE_STATIC
67856** or SQLITE_TRANSIENT. If it is SQLITE_DYNAMIC, then the buffer pointed
67857** to by zName will be freed by sqlite3DbFree() when the vdbe is destroyed.
67858*/
67859SQLITE_PRIVATE int sqlite3VdbeSetColName(
67860  Vdbe *p,                         /* Vdbe being configured */
67861  int idx,                         /* Index of column zName applies to */
67862  int var,                         /* One of the COLNAME_* constants */
67863  const char *zName,               /* Pointer to buffer containing name */
67864  void (*xDel)(void*)              /* Memory management strategy for zName */
67865){
67866  int rc;
67867  Mem *pColName;
67868  assert( idx<p->nResColumn );
67869  assert( var<COLNAME_N );
67870  if( p->db->mallocFailed ){
67871    assert( !zName || xDel!=SQLITE_DYNAMIC );
67872    return SQLITE_NOMEM;
67873  }
67874  assert( p->aColName!=0 );
67875  pColName = &(p->aColName[idx+var*p->nResColumn]);
67876  rc = sqlite3VdbeMemSetStr(pColName, zName, -1, SQLITE_UTF8, xDel);
67877  assert( rc!=0 || !zName || (pColName->flags&MEM_Term)!=0 );
67878  return rc;
67879}
67880
67881/*
67882** A read or write transaction may or may not be active on database handle
67883** db. If a transaction is active, commit it. If there is a
67884** write-transaction spanning more than one database file, this routine
67885** takes care of the master journal trickery.
67886*/
67887static int vdbeCommit(sqlite3 *db, Vdbe *p){
67888  int i;
67889  int nTrans = 0;  /* Number of databases with an active write-transaction */
67890  int rc = SQLITE_OK;
67891  int needXcommit = 0;
67892
67893#ifdef SQLITE_OMIT_VIRTUALTABLE
67894  /* With this option, sqlite3VtabSync() is defined to be simply
67895  ** SQLITE_OK so p is not used.
67896  */
67897  UNUSED_PARAMETER(p);
67898#endif
67899
67900  /* Before doing anything else, call the xSync() callback for any
67901  ** virtual module tables written in this transaction. This has to
67902  ** be done before determining whether a master journal file is
67903  ** required, as an xSync() callback may add an attached database
67904  ** to the transaction.
67905  */
67906  rc = sqlite3VtabSync(db, p);
67907
67908  /* This loop determines (a) if the commit hook should be invoked and
67909  ** (b) how many database files have open write transactions, not
67910  ** including the temp database. (b) is important because if more than
67911  ** one database file has an open write transaction, a master journal
67912  ** file is required for an atomic commit.
67913  */
67914  for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
67915    Btree *pBt = db->aDb[i].pBt;
67916    if( sqlite3BtreeIsInTrans(pBt) ){
67917      needXcommit = 1;
67918      if( i!=1 ) nTrans++;
67919      sqlite3BtreeEnter(pBt);
67920      rc = sqlite3PagerExclusiveLock(sqlite3BtreePager(pBt));
67921      sqlite3BtreeLeave(pBt);
67922    }
67923  }
67924  if( rc!=SQLITE_OK ){
67925    return rc;
67926  }
67927
67928  /* If there are any write-transactions at all, invoke the commit hook */
67929  if( needXcommit && db->xCommitCallback ){
67930    rc = db->xCommitCallback(db->pCommitArg);
67931    if( rc ){
67932      return SQLITE_CONSTRAINT_COMMITHOOK;
67933    }
67934  }
67935
67936  /* The simple case - no more than one database file (not counting the
67937  ** TEMP database) has a transaction active.   There is no need for the
67938  ** master-journal.
67939  **
67940  ** If the return value of sqlite3BtreeGetFilename() is a zero length
67941  ** string, it means the main database is :memory: or a temp file.  In
67942  ** that case we do not support atomic multi-file commits, so use the
67943  ** simple case then too.
67944  */
67945  if( 0==sqlite3Strlen30(sqlite3BtreeGetFilename(db->aDb[0].pBt))
67946   || nTrans<=1
67947  ){
67948    for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
67949      Btree *pBt = db->aDb[i].pBt;
67950      if( pBt ){
67951        rc = sqlite3BtreeCommitPhaseOne(pBt, 0);
67952      }
67953    }
67954
67955    /* Do the commit only if all databases successfully complete phase 1.
67956    ** If one of the BtreeCommitPhaseOne() calls fails, this indicates an
67957    ** IO error while deleting or truncating a journal file. It is unlikely,
67958    ** but could happen. In this case abandon processing and return the error.
67959    */
67960    for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
67961      Btree *pBt = db->aDb[i].pBt;
67962      if( pBt ){
67963        rc = sqlite3BtreeCommitPhaseTwo(pBt, 0);
67964      }
67965    }
67966    if( rc==SQLITE_OK ){
67967      sqlite3VtabCommit(db);
67968    }
67969  }
67970
67971  /* The complex case - There is a multi-file write-transaction active.
67972  ** This requires a master journal file to ensure the transaction is
67973  ** committed atomically.
67974  */
67975#ifndef SQLITE_OMIT_DISKIO
67976  else{
67977    sqlite3_vfs *pVfs = db->pVfs;
67978    int needSync = 0;
67979    char *zMaster = 0;   /* File-name for the master journal */
67980    char const *zMainFile = sqlite3BtreeGetFilename(db->aDb[0].pBt);
67981    sqlite3_file *pMaster = 0;
67982    i64 offset = 0;
67983    int res;
67984    int retryCount = 0;
67985    int nMainFile;
67986
67987    /* Select a master journal file name */
67988    nMainFile = sqlite3Strlen30(zMainFile);
67989    zMaster = sqlite3MPrintf(db, "%s-mjXXXXXX9XXz", zMainFile);
67990    if( zMaster==0 ) return SQLITE_NOMEM;
67991    do {
67992      u32 iRandom;
67993      if( retryCount ){
67994        if( retryCount>100 ){
67995          sqlite3_log(SQLITE_FULL, "MJ delete: %s", zMaster);
67996          sqlite3OsDelete(pVfs, zMaster, 0);
67997          break;
67998        }else if( retryCount==1 ){
67999          sqlite3_log(SQLITE_FULL, "MJ collide: %s", zMaster);
68000        }
68001      }
68002      retryCount++;
68003      sqlite3_randomness(sizeof(iRandom), &iRandom);
68004      sqlite3_snprintf(13, &zMaster[nMainFile], "-mj%06X9%02X",
68005                               (iRandom>>8)&0xffffff, iRandom&0xff);
68006      /* The antipenultimate character of the master journal name must
68007      ** be "9" to avoid name collisions when using 8+3 filenames. */
68008      assert( zMaster[sqlite3Strlen30(zMaster)-3]=='9' );
68009      sqlite3FileSuffix3(zMainFile, zMaster);
68010      rc = sqlite3OsAccess(pVfs, zMaster, SQLITE_ACCESS_EXISTS, &res);
68011    }while( rc==SQLITE_OK && res );
68012    if( rc==SQLITE_OK ){
68013      /* Open the master journal. */
68014      rc = sqlite3OsOpenMalloc(pVfs, zMaster, &pMaster,
68015          SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|
68016          SQLITE_OPEN_EXCLUSIVE|SQLITE_OPEN_MASTER_JOURNAL, 0
68017      );
68018    }
68019    if( rc!=SQLITE_OK ){
68020      sqlite3DbFree(db, zMaster);
68021      return rc;
68022    }
68023
68024    /* Write the name of each database file in the transaction into the new
68025    ** master journal file. If an error occurs at this point close
68026    ** and delete the master journal file. All the individual journal files
68027    ** still have 'null' as the master journal pointer, so they will roll
68028    ** back independently if a failure occurs.
68029    */
68030    for(i=0; i<db->nDb; i++){
68031      Btree *pBt = db->aDb[i].pBt;
68032      if( sqlite3BtreeIsInTrans(pBt) ){
68033        char const *zFile = sqlite3BtreeGetJournalname(pBt);
68034        if( zFile==0 ){
68035          continue;  /* Ignore TEMP and :memory: databases */
68036        }
68037        assert( zFile[0]!=0 );
68038        if( !needSync && !sqlite3BtreeSyncDisabled(pBt) ){
68039          needSync = 1;
68040        }
68041        rc = sqlite3OsWrite(pMaster, zFile, sqlite3Strlen30(zFile)+1, offset);
68042        offset += sqlite3Strlen30(zFile)+1;
68043        if( rc!=SQLITE_OK ){
68044          sqlite3OsCloseFree(pMaster);
68045          sqlite3OsDelete(pVfs, zMaster, 0);
68046          sqlite3DbFree(db, zMaster);
68047          return rc;
68048        }
68049      }
68050    }
68051
68052    /* Sync the master journal file. If the IOCAP_SEQUENTIAL device
68053    ** flag is set this is not required.
68054    */
68055    if( needSync
68056     && 0==(sqlite3OsDeviceCharacteristics(pMaster)&SQLITE_IOCAP_SEQUENTIAL)
68057     && SQLITE_OK!=(rc = sqlite3OsSync(pMaster, SQLITE_SYNC_NORMAL))
68058    ){
68059      sqlite3OsCloseFree(pMaster);
68060      sqlite3OsDelete(pVfs, zMaster, 0);
68061      sqlite3DbFree(db, zMaster);
68062      return rc;
68063    }
68064
68065    /* Sync all the db files involved in the transaction. The same call
68066    ** sets the master journal pointer in each individual journal. If
68067    ** an error occurs here, do not delete the master journal file.
68068    **
68069    ** If the error occurs during the first call to
68070    ** sqlite3BtreeCommitPhaseOne(), then there is a chance that the
68071    ** master journal file will be orphaned. But we cannot delete it,
68072    ** in case the master journal file name was written into the journal
68073    ** file before the failure occurred.
68074    */
68075    for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
68076      Btree *pBt = db->aDb[i].pBt;
68077      if( pBt ){
68078        rc = sqlite3BtreeCommitPhaseOne(pBt, zMaster);
68079      }
68080    }
68081    sqlite3OsCloseFree(pMaster);
68082    assert( rc!=SQLITE_BUSY );
68083    if( rc!=SQLITE_OK ){
68084      sqlite3DbFree(db, zMaster);
68085      return rc;
68086    }
68087
68088    /* Delete the master journal file. This commits the transaction. After
68089    ** doing this the directory is synced again before any individual
68090    ** transaction files are deleted.
68091    */
68092    rc = sqlite3OsDelete(pVfs, zMaster, needSync);
68093    sqlite3DbFree(db, zMaster);
68094    zMaster = 0;
68095    if( rc ){
68096      return rc;
68097    }
68098
68099    /* All files and directories have already been synced, so the following
68100    ** calls to sqlite3BtreeCommitPhaseTwo() are only closing files and
68101    ** deleting or truncating journals. If something goes wrong while
68102    ** this is happening we don't really care. The integrity of the
68103    ** transaction is already guaranteed, but some stray 'cold' journals
68104    ** may be lying around. Returning an error code won't help matters.
68105    */
68106    disable_simulated_io_errors();
68107    sqlite3BeginBenignMalloc();
68108    for(i=0; i<db->nDb; i++){
68109      Btree *pBt = db->aDb[i].pBt;
68110      if( pBt ){
68111        sqlite3BtreeCommitPhaseTwo(pBt, 1);
68112      }
68113    }
68114    sqlite3EndBenignMalloc();
68115    enable_simulated_io_errors();
68116
68117    sqlite3VtabCommit(db);
68118  }
68119#endif
68120
68121  return rc;
68122}
68123
68124/*
68125** This routine checks that the sqlite3.nVdbeActive count variable
68126** matches the number of vdbe's in the list sqlite3.pVdbe that are
68127** currently active. An assertion fails if the two counts do not match.
68128** This is an internal self-check only - it is not an essential processing
68129** step.
68130**
68131** This is a no-op if NDEBUG is defined.
68132*/
68133#ifndef NDEBUG
68134static void checkActiveVdbeCnt(sqlite3 *db){
68135  Vdbe *p;
68136  int cnt = 0;
68137  int nWrite = 0;
68138  int nRead = 0;
68139  p = db->pVdbe;
68140  while( p ){
68141    if( sqlite3_stmt_busy((sqlite3_stmt*)p) ){
68142      cnt++;
68143      if( p->readOnly==0 ) nWrite++;
68144      if( p->bIsReader ) nRead++;
68145    }
68146    p = p->pNext;
68147  }
68148  assert( cnt==db->nVdbeActive );
68149  assert( nWrite==db->nVdbeWrite );
68150  assert( nRead==db->nVdbeRead );
68151}
68152#else
68153#define checkActiveVdbeCnt(x)
68154#endif
68155
68156/*
68157** If the Vdbe passed as the first argument opened a statement-transaction,
68158** close it now. Argument eOp must be either SAVEPOINT_ROLLBACK or
68159** SAVEPOINT_RELEASE. If it is SAVEPOINT_ROLLBACK, then the statement
68160** transaction is rolled back. If eOp is SAVEPOINT_RELEASE, then the
68161** statement transaction is committed.
68162**
68163** If an IO error occurs, an SQLITE_IOERR_XXX error code is returned.
68164** Otherwise SQLITE_OK.
68165*/
68166SQLITE_PRIVATE int sqlite3VdbeCloseStatement(Vdbe *p, int eOp){
68167  sqlite3 *const db = p->db;
68168  int rc = SQLITE_OK;
68169
68170  /* If p->iStatement is greater than zero, then this Vdbe opened a
68171  ** statement transaction that should be closed here. The only exception
68172  ** is that an IO error may have occurred, causing an emergency rollback.
68173  ** In this case (db->nStatement==0), and there is nothing to do.
68174  */
68175  if( db->nStatement && p->iStatement ){
68176    int i;
68177    const int iSavepoint = p->iStatement-1;
68178
68179    assert( eOp==SAVEPOINT_ROLLBACK || eOp==SAVEPOINT_RELEASE);
68180    assert( db->nStatement>0 );
68181    assert( p->iStatement==(db->nStatement+db->nSavepoint) );
68182
68183    for(i=0; i<db->nDb; i++){
68184      int rc2 = SQLITE_OK;
68185      Btree *pBt = db->aDb[i].pBt;
68186      if( pBt ){
68187        if( eOp==SAVEPOINT_ROLLBACK ){
68188          rc2 = sqlite3BtreeSavepoint(pBt, SAVEPOINT_ROLLBACK, iSavepoint);
68189        }
68190        if( rc2==SQLITE_OK ){
68191          rc2 = sqlite3BtreeSavepoint(pBt, SAVEPOINT_RELEASE, iSavepoint);
68192        }
68193        if( rc==SQLITE_OK ){
68194          rc = rc2;
68195        }
68196      }
68197    }
68198    db->nStatement--;
68199    p->iStatement = 0;
68200
68201    if( rc==SQLITE_OK ){
68202      if( eOp==SAVEPOINT_ROLLBACK ){
68203        rc = sqlite3VtabSavepoint(db, SAVEPOINT_ROLLBACK, iSavepoint);
68204      }
68205      if( rc==SQLITE_OK ){
68206        rc = sqlite3VtabSavepoint(db, SAVEPOINT_RELEASE, iSavepoint);
68207      }
68208    }
68209
68210    /* If the statement transaction is being rolled back, also restore the
68211    ** database handles deferred constraint counter to the value it had when
68212    ** the statement transaction was opened.  */
68213    if( eOp==SAVEPOINT_ROLLBACK ){
68214      db->nDeferredCons = p->nStmtDefCons;
68215      db->nDeferredImmCons = p->nStmtDefImmCons;
68216    }
68217  }
68218  return rc;
68219}
68220
68221/*
68222** This function is called when a transaction opened by the database
68223** handle associated with the VM passed as an argument is about to be
68224** committed. If there are outstanding deferred foreign key constraint
68225** violations, return SQLITE_ERROR. Otherwise, SQLITE_OK.
68226**
68227** If there are outstanding FK violations and this function returns
68228** SQLITE_ERROR, set the result of the VM to SQLITE_CONSTRAINT_FOREIGNKEY
68229** and write an error message to it. Then return SQLITE_ERROR.
68230*/
68231#ifndef SQLITE_OMIT_FOREIGN_KEY
68232SQLITE_PRIVATE int sqlite3VdbeCheckFk(Vdbe *p, int deferred){
68233  sqlite3 *db = p->db;
68234  if( (deferred && (db->nDeferredCons+db->nDeferredImmCons)>0)
68235   || (!deferred && p->nFkConstraint>0)
68236  ){
68237    p->rc = SQLITE_CONSTRAINT_FOREIGNKEY;
68238    p->errorAction = OE_Abort;
68239    sqlite3VdbeError(p, "FOREIGN KEY constraint failed");
68240    return SQLITE_ERROR;
68241  }
68242  return SQLITE_OK;
68243}
68244#endif
68245
68246/*
68247** This routine is called the when a VDBE tries to halt.  If the VDBE
68248** has made changes and is in autocommit mode, then commit those
68249** changes.  If a rollback is needed, then do the rollback.
68250**
68251** This routine is the only way to move the state of a VM from
68252** SQLITE_MAGIC_RUN to SQLITE_MAGIC_HALT.  It is harmless to
68253** call this on a VM that is in the SQLITE_MAGIC_HALT state.
68254**
68255** Return an error code.  If the commit could not complete because of
68256** lock contention, return SQLITE_BUSY.  If SQLITE_BUSY is returned, it
68257** means the close did not happen and needs to be repeated.
68258*/
68259SQLITE_PRIVATE int sqlite3VdbeHalt(Vdbe *p){
68260  int rc;                         /* Used to store transient return codes */
68261  sqlite3 *db = p->db;
68262
68263  /* This function contains the logic that determines if a statement or
68264  ** transaction will be committed or rolled back as a result of the
68265  ** execution of this virtual machine.
68266  **
68267  ** If any of the following errors occur:
68268  **
68269  **     SQLITE_NOMEM
68270  **     SQLITE_IOERR
68271  **     SQLITE_FULL
68272  **     SQLITE_INTERRUPT
68273  **
68274  ** Then the internal cache might have been left in an inconsistent
68275  ** state.  We need to rollback the statement transaction, if there is
68276  ** one, or the complete transaction if there is no statement transaction.
68277  */
68278
68279  if( p->db->mallocFailed ){
68280    p->rc = SQLITE_NOMEM;
68281  }
68282  if( p->aOnceFlag ) memset(p->aOnceFlag, 0, p->nOnceFlag);
68283  closeAllCursors(p);
68284  if( p->magic!=VDBE_MAGIC_RUN ){
68285    return SQLITE_OK;
68286  }
68287  checkActiveVdbeCnt(db);
68288
68289  /* No commit or rollback needed if the program never started or if the
68290  ** SQL statement does not read or write a database file.  */
68291  if( p->pc>=0 && p->bIsReader ){
68292    int mrc;   /* Primary error code from p->rc */
68293    int eStatementOp = 0;
68294    int isSpecialError;            /* Set to true if a 'special' error */
68295
68296    /* Lock all btrees used by the statement */
68297    sqlite3VdbeEnter(p);
68298
68299    /* Check for one of the special errors */
68300    mrc = p->rc & 0xff;
68301    isSpecialError = mrc==SQLITE_NOMEM || mrc==SQLITE_IOERR
68302                     || mrc==SQLITE_INTERRUPT || mrc==SQLITE_FULL;
68303    if( isSpecialError ){
68304      /* If the query was read-only and the error code is SQLITE_INTERRUPT,
68305      ** no rollback is necessary. Otherwise, at least a savepoint
68306      ** transaction must be rolled back to restore the database to a
68307      ** consistent state.
68308      **
68309      ** Even if the statement is read-only, it is important to perform
68310      ** a statement or transaction rollback operation. If the error
68311      ** occurred while writing to the journal, sub-journal or database
68312      ** file as part of an effort to free up cache space (see function
68313      ** pagerStress() in pager.c), the rollback is required to restore
68314      ** the pager to a consistent state.
68315      */
68316      if( !p->readOnly || mrc!=SQLITE_INTERRUPT ){
68317        if( (mrc==SQLITE_NOMEM || mrc==SQLITE_FULL) && p->usesStmtJournal ){
68318          eStatementOp = SAVEPOINT_ROLLBACK;
68319        }else{
68320          /* We are forced to roll back the active transaction. Before doing
68321          ** so, abort any other statements this handle currently has active.
68322          */
68323          sqlite3RollbackAll(db, SQLITE_ABORT_ROLLBACK);
68324          sqlite3CloseSavepoints(db);
68325          db->autoCommit = 1;
68326          p->nChange = 0;
68327        }
68328      }
68329    }
68330
68331    /* Check for immediate foreign key violations. */
68332    if( p->rc==SQLITE_OK ){
68333      sqlite3VdbeCheckFk(p, 0);
68334    }
68335
68336    /* If the auto-commit flag is set and this is the only active writer
68337    ** VM, then we do either a commit or rollback of the current transaction.
68338    **
68339    ** Note: This block also runs if one of the special errors handled
68340    ** above has occurred.
68341    */
68342    if( !sqlite3VtabInSync(db)
68343     && db->autoCommit
68344     && db->nVdbeWrite==(p->readOnly==0)
68345    ){
68346      if( p->rc==SQLITE_OK || (p->errorAction==OE_Fail && !isSpecialError) ){
68347        rc = sqlite3VdbeCheckFk(p, 1);
68348        if( rc!=SQLITE_OK ){
68349          if( NEVER(p->readOnly) ){
68350            sqlite3VdbeLeave(p);
68351            return SQLITE_ERROR;
68352          }
68353          rc = SQLITE_CONSTRAINT_FOREIGNKEY;
68354        }else{
68355          /* The auto-commit flag is true, the vdbe program was successful
68356          ** or hit an 'OR FAIL' constraint and there are no deferred foreign
68357          ** key constraints to hold up the transaction. This means a commit
68358          ** is required. */
68359          rc = vdbeCommit(db, p);
68360        }
68361        if( rc==SQLITE_BUSY && p->readOnly ){
68362          sqlite3VdbeLeave(p);
68363          return SQLITE_BUSY;
68364        }else if( rc!=SQLITE_OK ){
68365          p->rc = rc;
68366          sqlite3RollbackAll(db, SQLITE_OK);
68367          p->nChange = 0;
68368        }else{
68369          db->nDeferredCons = 0;
68370          db->nDeferredImmCons = 0;
68371          db->flags &= ~SQLITE_DeferFKs;
68372          sqlite3CommitInternalChanges(db);
68373        }
68374      }else{
68375        sqlite3RollbackAll(db, SQLITE_OK);
68376        p->nChange = 0;
68377      }
68378      db->nStatement = 0;
68379    }else if( eStatementOp==0 ){
68380      if( p->rc==SQLITE_OK || p->errorAction==OE_Fail ){
68381        eStatementOp = SAVEPOINT_RELEASE;
68382      }else if( p->errorAction==OE_Abort ){
68383        eStatementOp = SAVEPOINT_ROLLBACK;
68384      }else{
68385        sqlite3RollbackAll(db, SQLITE_ABORT_ROLLBACK);
68386        sqlite3CloseSavepoints(db);
68387        db->autoCommit = 1;
68388        p->nChange = 0;
68389      }
68390    }
68391
68392    /* If eStatementOp is non-zero, then a statement transaction needs to
68393    ** be committed or rolled back. Call sqlite3VdbeCloseStatement() to
68394    ** do so. If this operation returns an error, and the current statement
68395    ** error code is SQLITE_OK or SQLITE_CONSTRAINT, then promote the
68396    ** current statement error code.
68397    */
68398    if( eStatementOp ){
68399      rc = sqlite3VdbeCloseStatement(p, eStatementOp);
68400      if( rc ){
68401        if( p->rc==SQLITE_OK || (p->rc&0xff)==SQLITE_CONSTRAINT ){
68402          p->rc = rc;
68403          sqlite3DbFree(db, p->zErrMsg);
68404          p->zErrMsg = 0;
68405        }
68406        sqlite3RollbackAll(db, SQLITE_ABORT_ROLLBACK);
68407        sqlite3CloseSavepoints(db);
68408        db->autoCommit = 1;
68409        p->nChange = 0;
68410      }
68411    }
68412
68413    /* If this was an INSERT, UPDATE or DELETE and no statement transaction
68414    ** has been rolled back, update the database connection change-counter.
68415    */
68416    if( p->changeCntOn ){
68417      if( eStatementOp!=SAVEPOINT_ROLLBACK ){
68418        sqlite3VdbeSetChanges(db, p->nChange);
68419      }else{
68420        sqlite3VdbeSetChanges(db, 0);
68421      }
68422      p->nChange = 0;
68423    }
68424
68425    /* Release the locks */
68426    sqlite3VdbeLeave(p);
68427  }
68428
68429  /* We have successfully halted and closed the VM.  Record this fact. */
68430  if( p->pc>=0 ){
68431    db->nVdbeActive--;
68432    if( !p->readOnly ) db->nVdbeWrite--;
68433    if( p->bIsReader ) db->nVdbeRead--;
68434    assert( db->nVdbeActive>=db->nVdbeRead );
68435    assert( db->nVdbeRead>=db->nVdbeWrite );
68436    assert( db->nVdbeWrite>=0 );
68437  }
68438  p->magic = VDBE_MAGIC_HALT;
68439  checkActiveVdbeCnt(db);
68440  if( p->db->mallocFailed ){
68441    p->rc = SQLITE_NOMEM;
68442  }
68443
68444  /* If the auto-commit flag is set to true, then any locks that were held
68445  ** by connection db have now been released. Call sqlite3ConnectionUnlocked()
68446  ** to invoke any required unlock-notify callbacks.
68447  */
68448  if( db->autoCommit ){
68449    sqlite3ConnectionUnlocked(db);
68450  }
68451
68452  assert( db->nVdbeActive>0 || db->autoCommit==0 || db->nStatement==0 );
68453  return (p->rc==SQLITE_BUSY ? SQLITE_BUSY : SQLITE_OK);
68454}
68455
68456
68457/*
68458** Each VDBE holds the result of the most recent sqlite3_step() call
68459** in p->rc.  This routine sets that result back to SQLITE_OK.
68460*/
68461SQLITE_PRIVATE void sqlite3VdbeResetStepResult(Vdbe *p){
68462  p->rc = SQLITE_OK;
68463}
68464
68465/*
68466** Copy the error code and error message belonging to the VDBE passed
68467** as the first argument to its database handle (so that they will be
68468** returned by calls to sqlite3_errcode() and sqlite3_errmsg()).
68469**
68470** This function does not clear the VDBE error code or message, just
68471** copies them to the database handle.
68472*/
68473SQLITE_PRIVATE int sqlite3VdbeTransferError(Vdbe *p){
68474  sqlite3 *db = p->db;
68475  int rc = p->rc;
68476  if( p->zErrMsg ){
68477    u8 mallocFailed = db->mallocFailed;
68478    sqlite3BeginBenignMalloc();
68479    if( db->pErr==0 ) db->pErr = sqlite3ValueNew(db);
68480    sqlite3ValueSetStr(db->pErr, -1, p->zErrMsg, SQLITE_UTF8, SQLITE_TRANSIENT);
68481    sqlite3EndBenignMalloc();
68482    db->mallocFailed = mallocFailed;
68483    db->errCode = rc;
68484  }else{
68485    sqlite3Error(db, rc);
68486  }
68487  return rc;
68488}
68489
68490#ifdef SQLITE_ENABLE_SQLLOG
68491/*
68492** If an SQLITE_CONFIG_SQLLOG hook is registered and the VM has been run,
68493** invoke it.
68494*/
68495static void vdbeInvokeSqllog(Vdbe *v){
68496  if( sqlite3GlobalConfig.xSqllog && v->rc==SQLITE_OK && v->zSql && v->pc>=0 ){
68497    char *zExpanded = sqlite3VdbeExpandSql(v, v->zSql);
68498    assert( v->db->init.busy==0 );
68499    if( zExpanded ){
68500      sqlite3GlobalConfig.xSqllog(
68501          sqlite3GlobalConfig.pSqllogArg, v->db, zExpanded, 1
68502      );
68503      sqlite3DbFree(v->db, zExpanded);
68504    }
68505  }
68506}
68507#else
68508# define vdbeInvokeSqllog(x)
68509#endif
68510
68511/*
68512** Clean up a VDBE after execution but do not delete the VDBE just yet.
68513** Write any error messages into *pzErrMsg.  Return the result code.
68514**
68515** After this routine is run, the VDBE should be ready to be executed
68516** again.
68517**
68518** To look at it another way, this routine resets the state of the
68519** virtual machine from VDBE_MAGIC_RUN or VDBE_MAGIC_HALT back to
68520** VDBE_MAGIC_INIT.
68521*/
68522SQLITE_PRIVATE int sqlite3VdbeReset(Vdbe *p){
68523  sqlite3 *db;
68524  db = p->db;
68525
68526  /* If the VM did not run to completion or if it encountered an
68527  ** error, then it might not have been halted properly.  So halt
68528  ** it now.
68529  */
68530  sqlite3VdbeHalt(p);
68531
68532  /* If the VDBE has be run even partially, then transfer the error code
68533  ** and error message from the VDBE into the main database structure.  But
68534  ** if the VDBE has just been set to run but has not actually executed any
68535  ** instructions yet, leave the main database error information unchanged.
68536  */
68537  if( p->pc>=0 ){
68538    vdbeInvokeSqllog(p);
68539    sqlite3VdbeTransferError(p);
68540    sqlite3DbFree(db, p->zErrMsg);
68541    p->zErrMsg = 0;
68542    if( p->runOnlyOnce ) p->expired = 1;
68543  }else if( p->rc && p->expired ){
68544    /* The expired flag was set on the VDBE before the first call
68545    ** to sqlite3_step(). For consistency (since sqlite3_step() was
68546    ** called), set the database error in this case as well.
68547    */
68548    sqlite3ErrorWithMsg(db, p->rc, p->zErrMsg ? "%s" : 0, p->zErrMsg);
68549    sqlite3DbFree(db, p->zErrMsg);
68550    p->zErrMsg = 0;
68551  }
68552
68553  /* Reclaim all memory used by the VDBE
68554  */
68555  Cleanup(p);
68556
68557  /* Save profiling information from this VDBE run.
68558  */
68559#ifdef VDBE_PROFILE
68560  {
68561    FILE *out = fopen("vdbe_profile.out", "a");
68562    if( out ){
68563      int i;
68564      fprintf(out, "---- ");
68565      for(i=0; i<p->nOp; i++){
68566        fprintf(out, "%02x", p->aOp[i].opcode);
68567      }
68568      fprintf(out, "\n");
68569      if( p->zSql ){
68570        char c, pc = 0;
68571        fprintf(out, "-- ");
68572        for(i=0; (c = p->zSql[i])!=0; i++){
68573          if( pc=='\n' ) fprintf(out, "-- ");
68574          putc(c, out);
68575          pc = c;
68576        }
68577        if( pc!='\n' ) fprintf(out, "\n");
68578      }
68579      for(i=0; i<p->nOp; i++){
68580        char zHdr[100];
68581        sqlite3_snprintf(sizeof(zHdr), zHdr, "%6u %12llu %8llu ",
68582           p->aOp[i].cnt,
68583           p->aOp[i].cycles,
68584           p->aOp[i].cnt>0 ? p->aOp[i].cycles/p->aOp[i].cnt : 0
68585        );
68586        fprintf(out, "%s", zHdr);
68587        sqlite3VdbePrintOp(out, i, &p->aOp[i]);
68588      }
68589      fclose(out);
68590    }
68591  }
68592#endif
68593  p->iCurrentTime = 0;
68594  p->magic = VDBE_MAGIC_INIT;
68595  return p->rc & db->errMask;
68596}
68597
68598/*
68599** Clean up and delete a VDBE after execution.  Return an integer which is
68600** the result code.  Write any error message text into *pzErrMsg.
68601*/
68602SQLITE_PRIVATE int sqlite3VdbeFinalize(Vdbe *p){
68603  int rc = SQLITE_OK;
68604  if( p->magic==VDBE_MAGIC_RUN || p->magic==VDBE_MAGIC_HALT ){
68605    rc = sqlite3VdbeReset(p);
68606    assert( (rc & p->db->errMask)==rc );
68607  }
68608  sqlite3VdbeDelete(p);
68609  return rc;
68610}
68611
68612/*
68613** If parameter iOp is less than zero, then invoke the destructor for
68614** all auxiliary data pointers currently cached by the VM passed as
68615** the first argument.
68616**
68617** Or, if iOp is greater than or equal to zero, then the destructor is
68618** only invoked for those auxiliary data pointers created by the user
68619** function invoked by the OP_Function opcode at instruction iOp of
68620** VM pVdbe, and only then if:
68621**
68622**    * the associated function parameter is the 32nd or later (counting
68623**      from left to right), or
68624**
68625**    * the corresponding bit in argument mask is clear (where the first
68626**      function parameter corresponds to bit 0 etc.).
68627*/
68628SQLITE_PRIVATE void sqlite3VdbeDeleteAuxData(Vdbe *pVdbe, int iOp, int mask){
68629  AuxData **pp = &pVdbe->pAuxData;
68630  while( *pp ){
68631    AuxData *pAux = *pp;
68632    if( (iOp<0)
68633     || (pAux->iOp==iOp && (pAux->iArg>31 || !(mask & MASKBIT32(pAux->iArg))))
68634    ){
68635      testcase( pAux->iArg==31 );
68636      if( pAux->xDelete ){
68637        pAux->xDelete(pAux->pAux);
68638      }
68639      *pp = pAux->pNext;
68640      sqlite3DbFree(pVdbe->db, pAux);
68641    }else{
68642      pp= &pAux->pNext;
68643    }
68644  }
68645}
68646
68647/*
68648** Free all memory associated with the Vdbe passed as the second argument,
68649** except for object itself, which is preserved.
68650**
68651** The difference between this function and sqlite3VdbeDelete() is that
68652** VdbeDelete() also unlinks the Vdbe from the list of VMs associated with
68653** the database connection and frees the object itself.
68654*/
68655SQLITE_PRIVATE void sqlite3VdbeClearObject(sqlite3 *db, Vdbe *p){
68656  SubProgram *pSub, *pNext;
68657  int i;
68658  assert( p->db==0 || p->db==db );
68659  releaseMemArray(p->aVar, p->nVar);
68660  releaseMemArray(p->aColName, p->nResColumn*COLNAME_N);
68661  for(pSub=p->pProgram; pSub; pSub=pNext){
68662    pNext = pSub->pNext;
68663    vdbeFreeOpArray(db, pSub->aOp, pSub->nOp);
68664    sqlite3DbFree(db, pSub);
68665  }
68666  for(i=p->nzVar-1; i>=0; i--) sqlite3DbFree(db, p->azVar[i]);
68667  vdbeFreeOpArray(db, p->aOp, p->nOp);
68668  sqlite3DbFree(db, p->aColName);
68669  sqlite3DbFree(db, p->zSql);
68670  sqlite3DbFree(db, p->pFree);
68671#ifdef SQLITE_ENABLE_STMT_SCANSTATUS
68672  for(i=0; i<p->nScan; i++){
68673    sqlite3DbFree(db, p->aScan[i].zName);
68674  }
68675  sqlite3DbFree(db, p->aScan);
68676#endif
68677}
68678
68679/*
68680** Delete an entire VDBE.
68681*/
68682SQLITE_PRIVATE void sqlite3VdbeDelete(Vdbe *p){
68683  sqlite3 *db;
68684
68685  if( NEVER(p==0) ) return;
68686  db = p->db;
68687  assert( sqlite3_mutex_held(db->mutex) );
68688  sqlite3VdbeClearObject(db, p);
68689  if( p->pPrev ){
68690    p->pPrev->pNext = p->pNext;
68691  }else{
68692    assert( db->pVdbe==p );
68693    db->pVdbe = p->pNext;
68694  }
68695  if( p->pNext ){
68696    p->pNext->pPrev = p->pPrev;
68697  }
68698  p->magic = VDBE_MAGIC_DEAD;
68699  p->db = 0;
68700  sqlite3DbFree(db, p);
68701}
68702
68703/*
68704** The cursor "p" has a pending seek operation that has not yet been
68705** carried out.  Seek the cursor now.  If an error occurs, return
68706** the appropriate error code.
68707*/
68708static int SQLITE_NOINLINE handleDeferredMoveto(VdbeCursor *p){
68709  int res, rc;
68710#ifdef SQLITE_TEST
68711  extern int sqlite3_search_count;
68712#endif
68713  assert( p->deferredMoveto );
68714  assert( p->isTable );
68715  rc = sqlite3BtreeMovetoUnpacked(p->pCursor, 0, p->movetoTarget, 0, &res);
68716  if( rc ) return rc;
68717  if( res!=0 ) return SQLITE_CORRUPT_BKPT;
68718#ifdef SQLITE_TEST
68719  sqlite3_search_count++;
68720#endif
68721  p->deferredMoveto = 0;
68722  p->cacheStatus = CACHE_STALE;
68723  return SQLITE_OK;
68724}
68725
68726/*
68727** Something has moved cursor "p" out of place.  Maybe the row it was
68728** pointed to was deleted out from under it.  Or maybe the btree was
68729** rebalanced.  Whatever the cause, try to restore "p" to the place it
68730** is supposed to be pointing.  If the row was deleted out from under the
68731** cursor, set the cursor to point to a NULL row.
68732*/
68733static int SQLITE_NOINLINE handleMovedCursor(VdbeCursor *p){
68734  int isDifferentRow, rc;
68735  assert( p->pCursor!=0 );
68736  assert( sqlite3BtreeCursorHasMoved(p->pCursor) );
68737  rc = sqlite3BtreeCursorRestore(p->pCursor, &isDifferentRow);
68738  p->cacheStatus = CACHE_STALE;
68739  if( isDifferentRow ) p->nullRow = 1;
68740  return rc;
68741}
68742
68743/*
68744** Check to ensure that the cursor is valid.  Restore the cursor
68745** if need be.  Return any I/O error from the restore operation.
68746*/
68747SQLITE_PRIVATE int sqlite3VdbeCursorRestore(VdbeCursor *p){
68748  if( sqlite3BtreeCursorHasMoved(p->pCursor) ){
68749    return handleMovedCursor(p);
68750  }
68751  return SQLITE_OK;
68752}
68753
68754/*
68755** Make sure the cursor p is ready to read or write the row to which it
68756** was last positioned.  Return an error code if an OOM fault or I/O error
68757** prevents us from positioning the cursor to its correct position.
68758**
68759** If a MoveTo operation is pending on the given cursor, then do that
68760** MoveTo now.  If no move is pending, check to see if the row has been
68761** deleted out from under the cursor and if it has, mark the row as
68762** a NULL row.
68763**
68764** If the cursor is already pointing to the correct row and that row has
68765** not been deleted out from under the cursor, then this routine is a no-op.
68766*/
68767SQLITE_PRIVATE int sqlite3VdbeCursorMoveto(VdbeCursor *p){
68768  if( p->deferredMoveto ){
68769    return handleDeferredMoveto(p);
68770  }
68771  if( p->pCursor && sqlite3BtreeCursorHasMoved(p->pCursor) ){
68772    return handleMovedCursor(p);
68773  }
68774  return SQLITE_OK;
68775}
68776
68777/*
68778** The following functions:
68779**
68780** sqlite3VdbeSerialType()
68781** sqlite3VdbeSerialTypeLen()
68782** sqlite3VdbeSerialLen()
68783** sqlite3VdbeSerialPut()
68784** sqlite3VdbeSerialGet()
68785**
68786** encapsulate the code that serializes values for storage in SQLite
68787** data and index records. Each serialized value consists of a
68788** 'serial-type' and a blob of data. The serial type is an 8-byte unsigned
68789** integer, stored as a varint.
68790**
68791** In an SQLite index record, the serial type is stored directly before
68792** the blob of data that it corresponds to. In a table record, all serial
68793** types are stored at the start of the record, and the blobs of data at
68794** the end. Hence these functions allow the caller to handle the
68795** serial-type and data blob separately.
68796**
68797** The following table describes the various storage classes for data:
68798**
68799**   serial type        bytes of data      type
68800**   --------------     ---------------    ---------------
68801**      0                     0            NULL
68802**      1                     1            signed integer
68803**      2                     2            signed integer
68804**      3                     3            signed integer
68805**      4                     4            signed integer
68806**      5                     6            signed integer
68807**      6                     8            signed integer
68808**      7                     8            IEEE float
68809**      8                     0            Integer constant 0
68810**      9                     0            Integer constant 1
68811**     10,11                               reserved for expansion
68812**    N>=12 and even       (N-12)/2        BLOB
68813**    N>=13 and odd        (N-13)/2        text
68814**
68815** The 8 and 9 types were added in 3.3.0, file format 4.  Prior versions
68816** of SQLite will not understand those serial types.
68817*/
68818
68819/*
68820** Return the serial-type for the value stored in pMem.
68821*/
68822SQLITE_PRIVATE u32 sqlite3VdbeSerialType(Mem *pMem, int file_format){
68823  int flags = pMem->flags;
68824  u32 n;
68825
68826  if( flags&MEM_Null ){
68827    return 0;
68828  }
68829  if( flags&MEM_Int ){
68830    /* Figure out whether to use 1, 2, 4, 6 or 8 bytes. */
68831#   define MAX_6BYTE ((((i64)0x00008000)<<32)-1)
68832    i64 i = pMem->u.i;
68833    u64 u;
68834    if( i<0 ){
68835      u = ~i;
68836    }else{
68837      u = i;
68838    }
68839    if( u<=127 ){
68840      return ((i&1)==i && file_format>=4) ? 8+(u32)u : 1;
68841    }
68842    if( u<=32767 ) return 2;
68843    if( u<=8388607 ) return 3;
68844    if( u<=2147483647 ) return 4;
68845    if( u<=MAX_6BYTE ) return 5;
68846    return 6;
68847  }
68848  if( flags&MEM_Real ){
68849    return 7;
68850  }
68851  assert( pMem->db->mallocFailed || flags&(MEM_Str|MEM_Blob) );
68852  assert( pMem->n>=0 );
68853  n = (u32)pMem->n;
68854  if( flags & MEM_Zero ){
68855    n += pMem->u.nZero;
68856  }
68857  return ((n*2) + 12 + ((flags&MEM_Str)!=0));
68858}
68859
68860/*
68861** The sizes for serial types less than 12
68862*/
68863static const u8 sqlite3SmallTypeSizes[] = {
68864  0, 1, 2, 3, 4, 6, 8, 8, 0, 0, 0, 0
68865};
68866
68867/*
68868** Return the length of the data corresponding to the supplied serial-type.
68869*/
68870SQLITE_PRIVATE u32 sqlite3VdbeSerialTypeLen(u32 serial_type){
68871  if( serial_type>=12 ){
68872    return (serial_type-12)/2;
68873  }else{
68874    return sqlite3SmallTypeSizes[serial_type];
68875  }
68876}
68877
68878/*
68879** If we are on an architecture with mixed-endian floating
68880** points (ex: ARM7) then swap the lower 4 bytes with the
68881** upper 4 bytes.  Return the result.
68882**
68883** For most architectures, this is a no-op.
68884**
68885** (later):  It is reported to me that the mixed-endian problem
68886** on ARM7 is an issue with GCC, not with the ARM7 chip.  It seems
68887** that early versions of GCC stored the two words of a 64-bit
68888** float in the wrong order.  And that error has been propagated
68889** ever since.  The blame is not necessarily with GCC, though.
68890** GCC might have just copying the problem from a prior compiler.
68891** I am also told that newer versions of GCC that follow a different
68892** ABI get the byte order right.
68893**
68894** Developers using SQLite on an ARM7 should compile and run their
68895** application using -DSQLITE_DEBUG=1 at least once.  With DEBUG
68896** enabled, some asserts below will ensure that the byte order of
68897** floating point values is correct.
68898**
68899** (2007-08-30)  Frank van Vugt has studied this problem closely
68900** and has send his findings to the SQLite developers.  Frank
68901** writes that some Linux kernels offer floating point hardware
68902** emulation that uses only 32-bit mantissas instead of a full
68903** 48-bits as required by the IEEE standard.  (This is the
68904** CONFIG_FPE_FASTFPE option.)  On such systems, floating point
68905** byte swapping becomes very complicated.  To avoid problems,
68906** the necessary byte swapping is carried out using a 64-bit integer
68907** rather than a 64-bit float.  Frank assures us that the code here
68908** works for him.  We, the developers, have no way to independently
68909** verify this, but Frank seems to know what he is talking about
68910** so we trust him.
68911*/
68912#ifdef SQLITE_MIXED_ENDIAN_64BIT_FLOAT
68913static u64 floatSwap(u64 in){
68914  union {
68915    u64 r;
68916    u32 i[2];
68917  } u;
68918  u32 t;
68919
68920  u.r = in;
68921  t = u.i[0];
68922  u.i[0] = u.i[1];
68923  u.i[1] = t;
68924  return u.r;
68925}
68926# define swapMixedEndianFloat(X)  X = floatSwap(X)
68927#else
68928# define swapMixedEndianFloat(X)
68929#endif
68930
68931/*
68932** Write the serialized data blob for the value stored in pMem into
68933** buf. It is assumed that the caller has allocated sufficient space.
68934** Return the number of bytes written.
68935**
68936** nBuf is the amount of space left in buf[].  The caller is responsible
68937** for allocating enough space to buf[] to hold the entire field, exclusive
68938** of the pMem->u.nZero bytes for a MEM_Zero value.
68939**
68940** Return the number of bytes actually written into buf[].  The number
68941** of bytes in the zero-filled tail is included in the return value only
68942** if those bytes were zeroed in buf[].
68943*/
68944SQLITE_PRIVATE u32 sqlite3VdbeSerialPut(u8 *buf, Mem *pMem, u32 serial_type){
68945  u32 len;
68946
68947  /* Integer and Real */
68948  if( serial_type<=7 && serial_type>0 ){
68949    u64 v;
68950    u32 i;
68951    if( serial_type==7 ){
68952      assert( sizeof(v)==sizeof(pMem->u.r) );
68953      memcpy(&v, &pMem->u.r, sizeof(v));
68954      swapMixedEndianFloat(v);
68955    }else{
68956      v = pMem->u.i;
68957    }
68958    len = i = sqlite3SmallTypeSizes[serial_type];
68959    assert( i>0 );
68960    do{
68961      buf[--i] = (u8)(v&0xFF);
68962      v >>= 8;
68963    }while( i );
68964    return len;
68965  }
68966
68967  /* String or blob */
68968  if( serial_type>=12 ){
68969    assert( pMem->n + ((pMem->flags & MEM_Zero)?pMem->u.nZero:0)
68970             == (int)sqlite3VdbeSerialTypeLen(serial_type) );
68971    len = pMem->n;
68972    memcpy(buf, pMem->z, len);
68973    return len;
68974  }
68975
68976  /* NULL or constants 0 or 1 */
68977  return 0;
68978}
68979
68980/* Input "x" is a sequence of unsigned characters that represent a
68981** big-endian integer.  Return the equivalent native integer
68982*/
68983#define ONE_BYTE_INT(x)    ((i8)(x)[0])
68984#define TWO_BYTE_INT(x)    (256*(i8)((x)[0])|(x)[1])
68985#define THREE_BYTE_INT(x)  (65536*(i8)((x)[0])|((x)[1]<<8)|(x)[2])
68986#define FOUR_BYTE_UINT(x)  (((u32)(x)[0]<<24)|((x)[1]<<16)|((x)[2]<<8)|(x)[3])
68987#define FOUR_BYTE_INT(x) (16777216*(i8)((x)[0])|((x)[1]<<16)|((x)[2]<<8)|(x)[3])
68988
68989/*
68990** Deserialize the data blob pointed to by buf as serial type serial_type
68991** and store the result in pMem.  Return the number of bytes read.
68992**
68993** This function is implemented as two separate routines for performance.
68994** The few cases that require local variables are broken out into a separate
68995** routine so that in most cases the overhead of moving the stack pointer
68996** is avoided.
68997*/
68998static u32 SQLITE_NOINLINE serialGet(
68999  const unsigned char *buf,     /* Buffer to deserialize from */
69000  u32 serial_type,              /* Serial type to deserialize */
69001  Mem *pMem                     /* Memory cell to write value into */
69002){
69003  u64 x = FOUR_BYTE_UINT(buf);
69004  u32 y = FOUR_BYTE_UINT(buf+4);
69005  x = (x<<32) + y;
69006  if( serial_type==6 ){
69007    /* EVIDENCE-OF: R-29851-52272 Value is a big-endian 64-bit
69008    ** twos-complement integer. */
69009    pMem->u.i = *(i64*)&x;
69010    pMem->flags = MEM_Int;
69011    testcase( pMem->u.i<0 );
69012  }else{
69013    /* EVIDENCE-OF: R-57343-49114 Value is a big-endian IEEE 754-2008 64-bit
69014    ** floating point number. */
69015#if !defined(NDEBUG) && !defined(SQLITE_OMIT_FLOATING_POINT)
69016    /* Verify that integers and floating point values use the same
69017    ** byte order.  Or, that if SQLITE_MIXED_ENDIAN_64BIT_FLOAT is
69018    ** defined that 64-bit floating point values really are mixed
69019    ** endian.
69020    */
69021    static const u64 t1 = ((u64)0x3ff00000)<<32;
69022    static const double r1 = 1.0;
69023    u64 t2 = t1;
69024    swapMixedEndianFloat(t2);
69025    assert( sizeof(r1)==sizeof(t2) && memcmp(&r1, &t2, sizeof(r1))==0 );
69026#endif
69027    assert( sizeof(x)==8 && sizeof(pMem->u.r)==8 );
69028    swapMixedEndianFloat(x);
69029    memcpy(&pMem->u.r, &x, sizeof(x));
69030    pMem->flags = sqlite3IsNaN(pMem->u.r) ? MEM_Null : MEM_Real;
69031  }
69032  return 8;
69033}
69034SQLITE_PRIVATE u32 sqlite3VdbeSerialGet(
69035  const unsigned char *buf,     /* Buffer to deserialize from */
69036  u32 serial_type,              /* Serial type to deserialize */
69037  Mem *pMem                     /* Memory cell to write value into */
69038){
69039  switch( serial_type ){
69040    case 10:   /* Reserved for future use */
69041    case 11:   /* Reserved for future use */
69042    case 0: {  /* Null */
69043      /* EVIDENCE-OF: R-24078-09375 Value is a NULL. */
69044      pMem->flags = MEM_Null;
69045      break;
69046    }
69047    case 1: {
69048      /* EVIDENCE-OF: R-44885-25196 Value is an 8-bit twos-complement
69049      ** integer. */
69050      pMem->u.i = ONE_BYTE_INT(buf);
69051      pMem->flags = MEM_Int;
69052      testcase( pMem->u.i<0 );
69053      return 1;
69054    }
69055    case 2: { /* 2-byte signed integer */
69056      /* EVIDENCE-OF: R-49794-35026 Value is a big-endian 16-bit
69057      ** twos-complement integer. */
69058      pMem->u.i = TWO_BYTE_INT(buf);
69059      pMem->flags = MEM_Int;
69060      testcase( pMem->u.i<0 );
69061      return 2;
69062    }
69063    case 3: { /* 3-byte signed integer */
69064      /* EVIDENCE-OF: R-37839-54301 Value is a big-endian 24-bit
69065      ** twos-complement integer. */
69066      pMem->u.i = THREE_BYTE_INT(buf);
69067      pMem->flags = MEM_Int;
69068      testcase( pMem->u.i<0 );
69069      return 3;
69070    }
69071    case 4: { /* 4-byte signed integer */
69072      /* EVIDENCE-OF: R-01849-26079 Value is a big-endian 32-bit
69073      ** twos-complement integer. */
69074      pMem->u.i = FOUR_BYTE_INT(buf);
69075      pMem->flags = MEM_Int;
69076      testcase( pMem->u.i<0 );
69077      return 4;
69078    }
69079    case 5: { /* 6-byte signed integer */
69080      /* EVIDENCE-OF: R-50385-09674 Value is a big-endian 48-bit
69081      ** twos-complement integer. */
69082      pMem->u.i = FOUR_BYTE_UINT(buf+2) + (((i64)1)<<32)*TWO_BYTE_INT(buf);
69083      pMem->flags = MEM_Int;
69084      testcase( pMem->u.i<0 );
69085      return 6;
69086    }
69087    case 6:   /* 8-byte signed integer */
69088    case 7: { /* IEEE floating point */
69089      /* These use local variables, so do them in a separate routine
69090      ** to avoid having to move the frame pointer in the common case */
69091      return serialGet(buf,serial_type,pMem);
69092    }
69093    case 8:    /* Integer 0 */
69094    case 9: {  /* Integer 1 */
69095      /* EVIDENCE-OF: R-12976-22893 Value is the integer 0. */
69096      /* EVIDENCE-OF: R-18143-12121 Value is the integer 1. */
69097      pMem->u.i = serial_type-8;
69098      pMem->flags = MEM_Int;
69099      return 0;
69100    }
69101    default: {
69102      /* EVIDENCE-OF: R-14606-31564 Value is a BLOB that is (N-12)/2 bytes in
69103      ** length.
69104      ** EVIDENCE-OF: R-28401-00140 Value is a string in the text encoding and
69105      ** (N-13)/2 bytes in length. */
69106      static const u16 aFlag[] = { MEM_Blob|MEM_Ephem, MEM_Str|MEM_Ephem };
69107      pMem->z = (char *)buf;
69108      pMem->n = (serial_type-12)/2;
69109      pMem->flags = aFlag[serial_type&1];
69110      return pMem->n;
69111    }
69112  }
69113  return 0;
69114}
69115/*
69116** This routine is used to allocate sufficient space for an UnpackedRecord
69117** structure large enough to be used with sqlite3VdbeRecordUnpack() if
69118** the first argument is a pointer to KeyInfo structure pKeyInfo.
69119**
69120** The space is either allocated using sqlite3DbMallocRaw() or from within
69121** the unaligned buffer passed via the second and third arguments (presumably
69122** stack space). If the former, then *ppFree is set to a pointer that should
69123** be eventually freed by the caller using sqlite3DbFree(). Or, if the
69124** allocation comes from the pSpace/szSpace buffer, *ppFree is set to NULL
69125** before returning.
69126**
69127** If an OOM error occurs, NULL is returned.
69128*/
69129SQLITE_PRIVATE UnpackedRecord *sqlite3VdbeAllocUnpackedRecord(
69130  KeyInfo *pKeyInfo,              /* Description of the record */
69131  char *pSpace,                   /* Unaligned space available */
69132  int szSpace,                    /* Size of pSpace[] in bytes */
69133  char **ppFree                   /* OUT: Caller should free this pointer */
69134){
69135  UnpackedRecord *p;              /* Unpacked record to return */
69136  int nOff;                       /* Increment pSpace by nOff to align it */
69137  int nByte;                      /* Number of bytes required for *p */
69138
69139  /* We want to shift the pointer pSpace up such that it is 8-byte aligned.
69140  ** Thus, we need to calculate a value, nOff, between 0 and 7, to shift
69141  ** it by.  If pSpace is already 8-byte aligned, nOff should be zero.
69142  */
69143  nOff = (8 - (SQLITE_PTR_TO_INT(pSpace) & 7)) & 7;
69144  nByte = ROUND8(sizeof(UnpackedRecord)) + sizeof(Mem)*(pKeyInfo->nField+1);
69145  if( nByte>szSpace+nOff ){
69146    p = (UnpackedRecord *)sqlite3DbMallocRaw(pKeyInfo->db, nByte);
69147    *ppFree = (char *)p;
69148    if( !p ) return 0;
69149  }else{
69150    p = (UnpackedRecord*)&pSpace[nOff];
69151    *ppFree = 0;
69152  }
69153
69154  p->aMem = (Mem*)&((char*)p)[ROUND8(sizeof(UnpackedRecord))];
69155  assert( pKeyInfo->aSortOrder!=0 );
69156  p->pKeyInfo = pKeyInfo;
69157  p->nField = pKeyInfo->nField + 1;
69158  return p;
69159}
69160
69161/*
69162** Given the nKey-byte encoding of a record in pKey[], populate the
69163** UnpackedRecord structure indicated by the fourth argument with the
69164** contents of the decoded record.
69165*/
69166SQLITE_PRIVATE void sqlite3VdbeRecordUnpack(
69167  KeyInfo *pKeyInfo,     /* Information about the record format */
69168  int nKey,              /* Size of the binary record */
69169  const void *pKey,      /* The binary record */
69170  UnpackedRecord *p      /* Populate this structure before returning. */
69171){
69172  const unsigned char *aKey = (const unsigned char *)pKey;
69173  int d;
69174  u32 idx;                        /* Offset in aKey[] to read from */
69175  u16 u;                          /* Unsigned loop counter */
69176  u32 szHdr;
69177  Mem *pMem = p->aMem;
69178
69179  p->default_rc = 0;
69180  assert( EIGHT_BYTE_ALIGNMENT(pMem) );
69181  idx = getVarint32(aKey, szHdr);
69182  d = szHdr;
69183  u = 0;
69184  while( idx<szHdr && d<=nKey ){
69185    u32 serial_type;
69186
69187    idx += getVarint32(&aKey[idx], serial_type);
69188    pMem->enc = pKeyInfo->enc;
69189    pMem->db = pKeyInfo->db;
69190    /* pMem->flags = 0; // sqlite3VdbeSerialGet() will set this for us */
69191    pMem->szMalloc = 0;
69192    d += sqlite3VdbeSerialGet(&aKey[d], serial_type, pMem);
69193    pMem++;
69194    if( (++u)>=p->nField ) break;
69195  }
69196  assert( u<=pKeyInfo->nField + 1 );
69197  p->nField = u;
69198}
69199
69200#if SQLITE_DEBUG
69201/*
69202** This function compares two index or table record keys in the same way
69203** as the sqlite3VdbeRecordCompare() routine. Unlike VdbeRecordCompare(),
69204** this function deserializes and compares values using the
69205** sqlite3VdbeSerialGet() and sqlite3MemCompare() functions. It is used
69206** in assert() statements to ensure that the optimized code in
69207** sqlite3VdbeRecordCompare() returns results with these two primitives.
69208**
69209** Return true if the result of comparison is equivalent to desiredResult.
69210** Return false if there is a disagreement.
69211*/
69212static int vdbeRecordCompareDebug(
69213  int nKey1, const void *pKey1, /* Left key */
69214  const UnpackedRecord *pPKey2, /* Right key */
69215  int desiredResult             /* Correct answer */
69216){
69217  u32 d1;            /* Offset into aKey[] of next data element */
69218  u32 idx1;          /* Offset into aKey[] of next header element */
69219  u32 szHdr1;        /* Number of bytes in header */
69220  int i = 0;
69221  int rc = 0;
69222  const unsigned char *aKey1 = (const unsigned char *)pKey1;
69223  KeyInfo *pKeyInfo;
69224  Mem mem1;
69225
69226  pKeyInfo = pPKey2->pKeyInfo;
69227  if( pKeyInfo->db==0 ) return 1;
69228  mem1.enc = pKeyInfo->enc;
69229  mem1.db = pKeyInfo->db;
69230  /* mem1.flags = 0;  // Will be initialized by sqlite3VdbeSerialGet() */
69231  VVA_ONLY( mem1.szMalloc = 0; ) /* Only needed by assert() statements */
69232
69233  /* Compilers may complain that mem1.u.i is potentially uninitialized.
69234  ** We could initialize it, as shown here, to silence those complaints.
69235  ** But in fact, mem1.u.i will never actually be used uninitialized, and doing
69236  ** the unnecessary initialization has a measurable negative performance
69237  ** impact, since this routine is a very high runner.  And so, we choose
69238  ** to ignore the compiler warnings and leave this variable uninitialized.
69239  */
69240  /*  mem1.u.i = 0;  // not needed, here to silence compiler warning */
69241
69242  idx1 = getVarint32(aKey1, szHdr1);
69243  if( szHdr1>98307 ) return SQLITE_CORRUPT;
69244  d1 = szHdr1;
69245  assert( pKeyInfo->nField+pKeyInfo->nXField>=pPKey2->nField || CORRUPT_DB );
69246  assert( pKeyInfo->aSortOrder!=0 );
69247  assert( pKeyInfo->nField>0 );
69248  assert( idx1<=szHdr1 || CORRUPT_DB );
69249  do{
69250    u32 serial_type1;
69251
69252    /* Read the serial types for the next element in each key. */
69253    idx1 += getVarint32( aKey1+idx1, serial_type1 );
69254
69255    /* Verify that there is enough key space remaining to avoid
69256    ** a buffer overread.  The "d1+serial_type1+2" subexpression will
69257    ** always be greater than or equal to the amount of required key space.
69258    ** Use that approximation to avoid the more expensive call to
69259    ** sqlite3VdbeSerialTypeLen() in the common case.
69260    */
69261    if( d1+serial_type1+2>(u32)nKey1
69262     && d1+sqlite3VdbeSerialTypeLen(serial_type1)>(u32)nKey1
69263    ){
69264      break;
69265    }
69266
69267    /* Extract the values to be compared.
69268    */
69269    d1 += sqlite3VdbeSerialGet(&aKey1[d1], serial_type1, &mem1);
69270
69271    /* Do the comparison
69272    */
69273    rc = sqlite3MemCompare(&mem1, &pPKey2->aMem[i], pKeyInfo->aColl[i]);
69274    if( rc!=0 ){
69275      assert( mem1.szMalloc==0 );  /* See comment below */
69276      if( pKeyInfo->aSortOrder[i] ){
69277        rc = -rc;  /* Invert the result for DESC sort order. */
69278      }
69279      goto debugCompareEnd;
69280    }
69281    i++;
69282  }while( idx1<szHdr1 && i<pPKey2->nField );
69283
69284  /* No memory allocation is ever used on mem1.  Prove this using
69285  ** the following assert().  If the assert() fails, it indicates a
69286  ** memory leak and a need to call sqlite3VdbeMemRelease(&mem1).
69287  */
69288  assert( mem1.szMalloc==0 );
69289
69290  /* rc==0 here means that one of the keys ran out of fields and
69291  ** all the fields up to that point were equal. Return the default_rc
69292  ** value.  */
69293  rc = pPKey2->default_rc;
69294
69295debugCompareEnd:
69296  if( desiredResult==0 && rc==0 ) return 1;
69297  if( desiredResult<0 && rc<0 ) return 1;
69298  if( desiredResult>0 && rc>0 ) return 1;
69299  if( CORRUPT_DB ) return 1;
69300  if( pKeyInfo->db->mallocFailed ) return 1;
69301  return 0;
69302}
69303#endif
69304
69305#if SQLITE_DEBUG
69306/*
69307** Count the number of fields (a.k.a. columns) in the record given by
69308** pKey,nKey.  The verify that this count is less than or equal to the
69309** limit given by pKeyInfo->nField + pKeyInfo->nXField.
69310**
69311** If this constraint is not satisfied, it means that the high-speed
69312** vdbeRecordCompareInt() and vdbeRecordCompareString() routines will
69313** not work correctly.  If this assert() ever fires, it probably means
69314** that the KeyInfo.nField or KeyInfo.nXField values were computed
69315** incorrectly.
69316*/
69317static void vdbeAssertFieldCountWithinLimits(
69318  int nKey, const void *pKey,   /* The record to verify */
69319  const KeyInfo *pKeyInfo       /* Compare size with this KeyInfo */
69320){
69321  int nField = 0;
69322  u32 szHdr;
69323  u32 idx;
69324  u32 notUsed;
69325  const unsigned char *aKey = (const unsigned char*)pKey;
69326
69327  if( CORRUPT_DB ) return;
69328  idx = getVarint32(aKey, szHdr);
69329  assert( nKey>=0 );
69330  assert( szHdr<=(u32)nKey );
69331  while( idx<szHdr ){
69332    idx += getVarint32(aKey+idx, notUsed);
69333    nField++;
69334  }
69335  assert( nField <= pKeyInfo->nField+pKeyInfo->nXField );
69336}
69337#else
69338# define vdbeAssertFieldCountWithinLimits(A,B,C)
69339#endif
69340
69341/*
69342** Both *pMem1 and *pMem2 contain string values. Compare the two values
69343** using the collation sequence pColl. As usual, return a negative , zero
69344** or positive value if *pMem1 is less than, equal to or greater than
69345** *pMem2, respectively. Similar in spirit to "rc = (*pMem1) - (*pMem2);".
69346*/
69347static int vdbeCompareMemString(
69348  const Mem *pMem1,
69349  const Mem *pMem2,
69350  const CollSeq *pColl,
69351  u8 *prcErr                      /* If an OOM occurs, set to SQLITE_NOMEM */
69352){
69353  if( pMem1->enc==pColl->enc ){
69354    /* The strings are already in the correct encoding.  Call the
69355     ** comparison function directly */
69356    return pColl->xCmp(pColl->pUser,pMem1->n,pMem1->z,pMem2->n,pMem2->z);
69357  }else{
69358    int rc;
69359    const void *v1, *v2;
69360    int n1, n2;
69361    Mem c1;
69362    Mem c2;
69363    sqlite3VdbeMemInit(&c1, pMem1->db, MEM_Null);
69364    sqlite3VdbeMemInit(&c2, pMem1->db, MEM_Null);
69365    sqlite3VdbeMemShallowCopy(&c1, pMem1, MEM_Ephem);
69366    sqlite3VdbeMemShallowCopy(&c2, pMem2, MEM_Ephem);
69367    v1 = sqlite3ValueText((sqlite3_value*)&c1, pColl->enc);
69368    n1 = v1==0 ? 0 : c1.n;
69369    v2 = sqlite3ValueText((sqlite3_value*)&c2, pColl->enc);
69370    n2 = v2==0 ? 0 : c2.n;
69371    rc = pColl->xCmp(pColl->pUser, n1, v1, n2, v2);
69372    sqlite3VdbeMemRelease(&c1);
69373    sqlite3VdbeMemRelease(&c2);
69374    if( (v1==0 || v2==0) && prcErr ) *prcErr = SQLITE_NOMEM;
69375    return rc;
69376  }
69377}
69378
69379/*
69380** Compare two blobs.  Return negative, zero, or positive if the first
69381** is less than, equal to, or greater than the second, respectively.
69382** If one blob is a prefix of the other, then the shorter is the lessor.
69383*/
69384static SQLITE_NOINLINE int sqlite3BlobCompare(const Mem *pB1, const Mem *pB2){
69385  int c = memcmp(pB1->z, pB2->z, pB1->n>pB2->n ? pB2->n : pB1->n);
69386  if( c ) return c;
69387  return pB1->n - pB2->n;
69388}
69389
69390
69391/*
69392** Compare the values contained by the two memory cells, returning
69393** negative, zero or positive if pMem1 is less than, equal to, or greater
69394** than pMem2. Sorting order is NULL's first, followed by numbers (integers
69395** and reals) sorted numerically, followed by text ordered by the collating
69396** sequence pColl and finally blob's ordered by memcmp().
69397**
69398** Two NULL values are considered equal by this function.
69399*/
69400SQLITE_PRIVATE int sqlite3MemCompare(const Mem *pMem1, const Mem *pMem2, const CollSeq *pColl){
69401  int f1, f2;
69402  int combined_flags;
69403
69404  f1 = pMem1->flags;
69405  f2 = pMem2->flags;
69406  combined_flags = f1|f2;
69407  assert( (combined_flags & MEM_RowSet)==0 );
69408
69409  /* If one value is NULL, it is less than the other. If both values
69410  ** are NULL, return 0.
69411  */
69412  if( combined_flags&MEM_Null ){
69413    return (f2&MEM_Null) - (f1&MEM_Null);
69414  }
69415
69416  /* If one value is a number and the other is not, the number is less.
69417  ** If both are numbers, compare as reals if one is a real, or as integers
69418  ** if both values are integers.
69419  */
69420  if( combined_flags&(MEM_Int|MEM_Real) ){
69421    double r1, r2;
69422    if( (f1 & f2 & MEM_Int)!=0 ){
69423      if( pMem1->u.i < pMem2->u.i ) return -1;
69424      if( pMem1->u.i > pMem2->u.i ) return 1;
69425      return 0;
69426    }
69427    if( (f1&MEM_Real)!=0 ){
69428      r1 = pMem1->u.r;
69429    }else if( (f1&MEM_Int)!=0 ){
69430      r1 = (double)pMem1->u.i;
69431    }else{
69432      return 1;
69433    }
69434    if( (f2&MEM_Real)!=0 ){
69435      r2 = pMem2->u.r;
69436    }else if( (f2&MEM_Int)!=0 ){
69437      r2 = (double)pMem2->u.i;
69438    }else{
69439      return -1;
69440    }
69441    if( r1<r2 ) return -1;
69442    if( r1>r2 ) return 1;
69443    return 0;
69444  }
69445
69446  /* If one value is a string and the other is a blob, the string is less.
69447  ** If both are strings, compare using the collating functions.
69448  */
69449  if( combined_flags&MEM_Str ){
69450    if( (f1 & MEM_Str)==0 ){
69451      return 1;
69452    }
69453    if( (f2 & MEM_Str)==0 ){
69454      return -1;
69455    }
69456
69457    assert( pMem1->enc==pMem2->enc );
69458    assert( pMem1->enc==SQLITE_UTF8 ||
69459            pMem1->enc==SQLITE_UTF16LE || pMem1->enc==SQLITE_UTF16BE );
69460
69461    /* The collation sequence must be defined at this point, even if
69462    ** the user deletes the collation sequence after the vdbe program is
69463    ** compiled (this was not always the case).
69464    */
69465    assert( !pColl || pColl->xCmp );
69466
69467    if( pColl ){
69468      return vdbeCompareMemString(pMem1, pMem2, pColl, 0);
69469    }
69470    /* If a NULL pointer was passed as the collate function, fall through
69471    ** to the blob case and use memcmp().  */
69472  }
69473
69474  /* Both values must be blobs.  Compare using memcmp().  */
69475  return sqlite3BlobCompare(pMem1, pMem2);
69476}
69477
69478
69479/*
69480** The first argument passed to this function is a serial-type that
69481** corresponds to an integer - all values between 1 and 9 inclusive
69482** except 7. The second points to a buffer containing an integer value
69483** serialized according to serial_type. This function deserializes
69484** and returns the value.
69485*/
69486static i64 vdbeRecordDecodeInt(u32 serial_type, const u8 *aKey){
69487  u32 y;
69488  assert( CORRUPT_DB || (serial_type>=1 && serial_type<=9 && serial_type!=7) );
69489  switch( serial_type ){
69490    case 0:
69491    case 1:
69492      testcase( aKey[0]&0x80 );
69493      return ONE_BYTE_INT(aKey);
69494    case 2:
69495      testcase( aKey[0]&0x80 );
69496      return TWO_BYTE_INT(aKey);
69497    case 3:
69498      testcase( aKey[0]&0x80 );
69499      return THREE_BYTE_INT(aKey);
69500    case 4: {
69501      testcase( aKey[0]&0x80 );
69502      y = FOUR_BYTE_UINT(aKey);
69503      return (i64)*(int*)&y;
69504    }
69505    case 5: {
69506      testcase( aKey[0]&0x80 );
69507      return FOUR_BYTE_UINT(aKey+2) + (((i64)1)<<32)*TWO_BYTE_INT(aKey);
69508    }
69509    case 6: {
69510      u64 x = FOUR_BYTE_UINT(aKey);
69511      testcase( aKey[0]&0x80 );
69512      x = (x<<32) | FOUR_BYTE_UINT(aKey+4);
69513      return (i64)*(i64*)&x;
69514    }
69515  }
69516
69517  return (serial_type - 8);
69518}
69519
69520/*
69521** This function compares the two table rows or index records
69522** specified by {nKey1, pKey1} and pPKey2.  It returns a negative, zero
69523** or positive integer if key1 is less than, equal to or
69524** greater than key2.  The {nKey1, pKey1} key must be a blob
69525** created by the OP_MakeRecord opcode of the VDBE.  The pPKey2
69526** key must be a parsed key such as obtained from
69527** sqlite3VdbeParseRecord.
69528**
69529** If argument bSkip is non-zero, it is assumed that the caller has already
69530** determined that the first fields of the keys are equal.
69531**
69532** Key1 and Key2 do not have to contain the same number of fields. If all
69533** fields that appear in both keys are equal, then pPKey2->default_rc is
69534** returned.
69535**
69536** If database corruption is discovered, set pPKey2->errCode to
69537** SQLITE_CORRUPT and return 0. If an OOM error is encountered,
69538** pPKey2->errCode is set to SQLITE_NOMEM and, if it is not NULL, the
69539** malloc-failed flag set on database handle (pPKey2->pKeyInfo->db).
69540*/
69541SQLITE_PRIVATE int sqlite3VdbeRecordCompareWithSkip(
69542  int nKey1, const void *pKey1,   /* Left key */
69543  UnpackedRecord *pPKey2,         /* Right key */
69544  int bSkip                       /* If true, skip the first field */
69545){
69546  u32 d1;                         /* Offset into aKey[] of next data element */
69547  int i;                          /* Index of next field to compare */
69548  u32 szHdr1;                     /* Size of record header in bytes */
69549  u32 idx1;                       /* Offset of first type in header */
69550  int rc = 0;                     /* Return value */
69551  Mem *pRhs = pPKey2->aMem;       /* Next field of pPKey2 to compare */
69552  KeyInfo *pKeyInfo = pPKey2->pKeyInfo;
69553  const unsigned char *aKey1 = (const unsigned char *)pKey1;
69554  Mem mem1;
69555
69556  /* If bSkip is true, then the caller has already determined that the first
69557  ** two elements in the keys are equal. Fix the various stack variables so
69558  ** that this routine begins comparing at the second field. */
69559  if( bSkip ){
69560    u32 s1;
69561    idx1 = 1 + getVarint32(&aKey1[1], s1);
69562    szHdr1 = aKey1[0];
69563    d1 = szHdr1 + sqlite3VdbeSerialTypeLen(s1);
69564    i = 1;
69565    pRhs++;
69566  }else{
69567    idx1 = getVarint32(aKey1, szHdr1);
69568    d1 = szHdr1;
69569    if( d1>(unsigned)nKey1 ){
69570      pPKey2->errCode = (u8)SQLITE_CORRUPT_BKPT;
69571      return 0;  /* Corruption */
69572    }
69573    i = 0;
69574  }
69575
69576  VVA_ONLY( mem1.szMalloc = 0; ) /* Only needed by assert() statements */
69577  assert( pPKey2->pKeyInfo->nField+pPKey2->pKeyInfo->nXField>=pPKey2->nField
69578       || CORRUPT_DB );
69579  assert( pPKey2->pKeyInfo->aSortOrder!=0 );
69580  assert( pPKey2->pKeyInfo->nField>0 );
69581  assert( idx1<=szHdr1 || CORRUPT_DB );
69582  do{
69583    u32 serial_type;
69584
69585    /* RHS is an integer */
69586    if( pRhs->flags & MEM_Int ){
69587      serial_type = aKey1[idx1];
69588      testcase( serial_type==12 );
69589      if( serial_type>=10 ){
69590        rc = +1;
69591      }else if( serial_type==0 ){
69592        rc = -1;
69593      }else if( serial_type==7 ){
69594        double rhs = (double)pRhs->u.i;
69595        sqlite3VdbeSerialGet(&aKey1[d1], serial_type, &mem1);
69596        if( mem1.u.r<rhs ){
69597          rc = -1;
69598        }else if( mem1.u.r>rhs ){
69599          rc = +1;
69600        }
69601      }else{
69602        i64 lhs = vdbeRecordDecodeInt(serial_type, &aKey1[d1]);
69603        i64 rhs = pRhs->u.i;
69604        if( lhs<rhs ){
69605          rc = -1;
69606        }else if( lhs>rhs ){
69607          rc = +1;
69608        }
69609      }
69610    }
69611
69612    /* RHS is real */
69613    else if( pRhs->flags & MEM_Real ){
69614      serial_type = aKey1[idx1];
69615      if( serial_type>=10 ){
69616        /* Serial types 12 or greater are strings and blobs (greater than
69617        ** numbers). Types 10 and 11 are currently "reserved for future
69618        ** use", so it doesn't really matter what the results of comparing
69619        ** them to numberic values are.  */
69620        rc = +1;
69621      }else if( serial_type==0 ){
69622        rc = -1;
69623      }else{
69624        double rhs = pRhs->u.r;
69625        double lhs;
69626        sqlite3VdbeSerialGet(&aKey1[d1], serial_type, &mem1);
69627        if( serial_type==7 ){
69628          lhs = mem1.u.r;
69629        }else{
69630          lhs = (double)mem1.u.i;
69631        }
69632        if( lhs<rhs ){
69633          rc = -1;
69634        }else if( lhs>rhs ){
69635          rc = +1;
69636        }
69637      }
69638    }
69639
69640    /* RHS is a string */
69641    else if( pRhs->flags & MEM_Str ){
69642      getVarint32(&aKey1[idx1], serial_type);
69643      testcase( serial_type==12 );
69644      if( serial_type<12 ){
69645        rc = -1;
69646      }else if( !(serial_type & 0x01) ){
69647        rc = +1;
69648      }else{
69649        mem1.n = (serial_type - 12) / 2;
69650        testcase( (d1+mem1.n)==(unsigned)nKey1 );
69651        testcase( (d1+mem1.n+1)==(unsigned)nKey1 );
69652        if( (d1+mem1.n) > (unsigned)nKey1 ){
69653          pPKey2->errCode = (u8)SQLITE_CORRUPT_BKPT;
69654          return 0;                /* Corruption */
69655        }else if( pKeyInfo->aColl[i] ){
69656          mem1.enc = pKeyInfo->enc;
69657          mem1.db = pKeyInfo->db;
69658          mem1.flags = MEM_Str;
69659          mem1.z = (char*)&aKey1[d1];
69660          rc = vdbeCompareMemString(
69661              &mem1, pRhs, pKeyInfo->aColl[i], &pPKey2->errCode
69662          );
69663        }else{
69664          int nCmp = MIN(mem1.n, pRhs->n);
69665          rc = memcmp(&aKey1[d1], pRhs->z, nCmp);
69666          if( rc==0 ) rc = mem1.n - pRhs->n;
69667        }
69668      }
69669    }
69670
69671    /* RHS is a blob */
69672    else if( pRhs->flags & MEM_Blob ){
69673      getVarint32(&aKey1[idx1], serial_type);
69674      testcase( serial_type==12 );
69675      if( serial_type<12 || (serial_type & 0x01) ){
69676        rc = -1;
69677      }else{
69678        int nStr = (serial_type - 12) / 2;
69679        testcase( (d1+nStr)==(unsigned)nKey1 );
69680        testcase( (d1+nStr+1)==(unsigned)nKey1 );
69681        if( (d1+nStr) > (unsigned)nKey1 ){
69682          pPKey2->errCode = (u8)SQLITE_CORRUPT_BKPT;
69683          return 0;                /* Corruption */
69684        }else{
69685          int nCmp = MIN(nStr, pRhs->n);
69686          rc = memcmp(&aKey1[d1], pRhs->z, nCmp);
69687          if( rc==0 ) rc = nStr - pRhs->n;
69688        }
69689      }
69690    }
69691
69692    /* RHS is null */
69693    else{
69694      serial_type = aKey1[idx1];
69695      rc = (serial_type!=0);
69696    }
69697
69698    if( rc!=0 ){
69699      if( pKeyInfo->aSortOrder[i] ){
69700        rc = -rc;
69701      }
69702      assert( vdbeRecordCompareDebug(nKey1, pKey1, pPKey2, rc) );
69703      assert( mem1.szMalloc==0 );  /* See comment below */
69704      return rc;
69705    }
69706
69707    i++;
69708    pRhs++;
69709    d1 += sqlite3VdbeSerialTypeLen(serial_type);
69710    idx1 += sqlite3VarintLen(serial_type);
69711  }while( idx1<(unsigned)szHdr1 && i<pPKey2->nField && d1<=(unsigned)nKey1 );
69712
69713  /* No memory allocation is ever used on mem1.  Prove this using
69714  ** the following assert().  If the assert() fails, it indicates a
69715  ** memory leak and a need to call sqlite3VdbeMemRelease(&mem1).  */
69716  assert( mem1.szMalloc==0 );
69717
69718  /* rc==0 here means that one or both of the keys ran out of fields and
69719  ** all the fields up to that point were equal. Return the default_rc
69720  ** value.  */
69721  assert( CORRUPT_DB
69722       || vdbeRecordCompareDebug(nKey1, pKey1, pPKey2, pPKey2->default_rc)
69723       || pKeyInfo->db->mallocFailed
69724  );
69725  return pPKey2->default_rc;
69726}
69727SQLITE_PRIVATE int sqlite3VdbeRecordCompare(
69728  int nKey1, const void *pKey1,   /* Left key */
69729  UnpackedRecord *pPKey2          /* Right key */
69730){
69731  return sqlite3VdbeRecordCompareWithSkip(nKey1, pKey1, pPKey2, 0);
69732}
69733
69734
69735/*
69736** This function is an optimized version of sqlite3VdbeRecordCompare()
69737** that (a) the first field of pPKey2 is an integer, and (b) the
69738** size-of-header varint at the start of (pKey1/nKey1) fits in a single
69739** byte (i.e. is less than 128).
69740**
69741** To avoid concerns about buffer overreads, this routine is only used
69742** on schemas where the maximum valid header size is 63 bytes or less.
69743*/
69744static int vdbeRecordCompareInt(
69745  int nKey1, const void *pKey1, /* Left key */
69746  UnpackedRecord *pPKey2        /* Right key */
69747){
69748  const u8 *aKey = &((const u8*)pKey1)[*(const u8*)pKey1 & 0x3F];
69749  int serial_type = ((const u8*)pKey1)[1];
69750  int res;
69751  u32 y;
69752  u64 x;
69753  i64 v = pPKey2->aMem[0].u.i;
69754  i64 lhs;
69755
69756  vdbeAssertFieldCountWithinLimits(nKey1, pKey1, pPKey2->pKeyInfo);
69757  assert( (*(u8*)pKey1)<=0x3F || CORRUPT_DB );
69758  switch( serial_type ){
69759    case 1: { /* 1-byte signed integer */
69760      lhs = ONE_BYTE_INT(aKey);
69761      testcase( lhs<0 );
69762      break;
69763    }
69764    case 2: { /* 2-byte signed integer */
69765      lhs = TWO_BYTE_INT(aKey);
69766      testcase( lhs<0 );
69767      break;
69768    }
69769    case 3: { /* 3-byte signed integer */
69770      lhs = THREE_BYTE_INT(aKey);
69771      testcase( lhs<0 );
69772      break;
69773    }
69774    case 4: { /* 4-byte signed integer */
69775      y = FOUR_BYTE_UINT(aKey);
69776      lhs = (i64)*(int*)&y;
69777      testcase( lhs<0 );
69778      break;
69779    }
69780    case 5: { /* 6-byte signed integer */
69781      lhs = FOUR_BYTE_UINT(aKey+2) + (((i64)1)<<32)*TWO_BYTE_INT(aKey);
69782      testcase( lhs<0 );
69783      break;
69784    }
69785    case 6: { /* 8-byte signed integer */
69786      x = FOUR_BYTE_UINT(aKey);
69787      x = (x<<32) | FOUR_BYTE_UINT(aKey+4);
69788      lhs = *(i64*)&x;
69789      testcase( lhs<0 );
69790      break;
69791    }
69792    case 8:
69793      lhs = 0;
69794      break;
69795    case 9:
69796      lhs = 1;
69797      break;
69798
69799    /* This case could be removed without changing the results of running
69800    ** this code. Including it causes gcc to generate a faster switch
69801    ** statement (since the range of switch targets now starts at zero and
69802    ** is contiguous) but does not cause any duplicate code to be generated
69803    ** (as gcc is clever enough to combine the two like cases). Other
69804    ** compilers might be similar.  */
69805    case 0: case 7:
69806      return sqlite3VdbeRecordCompare(nKey1, pKey1, pPKey2);
69807
69808    default:
69809      return sqlite3VdbeRecordCompare(nKey1, pKey1, pPKey2);
69810  }
69811
69812  if( v>lhs ){
69813    res = pPKey2->r1;
69814  }else if( v<lhs ){
69815    res = pPKey2->r2;
69816  }else if( pPKey2->nField>1 ){
69817    /* The first fields of the two keys are equal. Compare the trailing
69818    ** fields.  */
69819    res = sqlite3VdbeRecordCompareWithSkip(nKey1, pKey1, pPKey2, 1);
69820  }else{
69821    /* The first fields of the two keys are equal and there are no trailing
69822    ** fields. Return pPKey2->default_rc in this case. */
69823    res = pPKey2->default_rc;
69824  }
69825
69826  assert( vdbeRecordCompareDebug(nKey1, pKey1, pPKey2, res) );
69827  return res;
69828}
69829
69830/*
69831** This function is an optimized version of sqlite3VdbeRecordCompare()
69832** that (a) the first field of pPKey2 is a string, that (b) the first field
69833** uses the collation sequence BINARY and (c) that the size-of-header varint
69834** at the start of (pKey1/nKey1) fits in a single byte.
69835*/
69836static int vdbeRecordCompareString(
69837  int nKey1, const void *pKey1, /* Left key */
69838  UnpackedRecord *pPKey2        /* Right key */
69839){
69840  const u8 *aKey1 = (const u8*)pKey1;
69841  int serial_type;
69842  int res;
69843
69844  vdbeAssertFieldCountWithinLimits(nKey1, pKey1, pPKey2->pKeyInfo);
69845  getVarint32(&aKey1[1], serial_type);
69846  if( serial_type<12 ){
69847    res = pPKey2->r1;      /* (pKey1/nKey1) is a number or a null */
69848  }else if( !(serial_type & 0x01) ){
69849    res = pPKey2->r2;      /* (pKey1/nKey1) is a blob */
69850  }else{
69851    int nCmp;
69852    int nStr;
69853    int szHdr = aKey1[0];
69854
69855    nStr = (serial_type-12) / 2;
69856    if( (szHdr + nStr) > nKey1 ){
69857      pPKey2->errCode = (u8)SQLITE_CORRUPT_BKPT;
69858      return 0;    /* Corruption */
69859    }
69860    nCmp = MIN( pPKey2->aMem[0].n, nStr );
69861    res = memcmp(&aKey1[szHdr], pPKey2->aMem[0].z, nCmp);
69862
69863    if( res==0 ){
69864      res = nStr - pPKey2->aMem[0].n;
69865      if( res==0 ){
69866        if( pPKey2->nField>1 ){
69867          res = sqlite3VdbeRecordCompareWithSkip(nKey1, pKey1, pPKey2, 1);
69868        }else{
69869          res = pPKey2->default_rc;
69870        }
69871      }else if( res>0 ){
69872        res = pPKey2->r2;
69873      }else{
69874        res = pPKey2->r1;
69875      }
69876    }else if( res>0 ){
69877      res = pPKey2->r2;
69878    }else{
69879      res = pPKey2->r1;
69880    }
69881  }
69882
69883  assert( vdbeRecordCompareDebug(nKey1, pKey1, pPKey2, res)
69884       || CORRUPT_DB
69885       || pPKey2->pKeyInfo->db->mallocFailed
69886  );
69887  return res;
69888}
69889
69890/*
69891** Return a pointer to an sqlite3VdbeRecordCompare() compatible function
69892** suitable for comparing serialized records to the unpacked record passed
69893** as the only argument.
69894*/
69895SQLITE_PRIVATE RecordCompare sqlite3VdbeFindCompare(UnpackedRecord *p){
69896  /* varintRecordCompareInt() and varintRecordCompareString() both assume
69897  ** that the size-of-header varint that occurs at the start of each record
69898  ** fits in a single byte (i.e. is 127 or less). varintRecordCompareInt()
69899  ** also assumes that it is safe to overread a buffer by at least the
69900  ** maximum possible legal header size plus 8 bytes. Because there is
69901  ** guaranteed to be at least 74 (but not 136) bytes of padding following each
69902  ** buffer passed to varintRecordCompareInt() this makes it convenient to
69903  ** limit the size of the header to 64 bytes in cases where the first field
69904  ** is an integer.
69905  **
69906  ** The easiest way to enforce this limit is to consider only records with
69907  ** 13 fields or less. If the first field is an integer, the maximum legal
69908  ** header size is (12*5 + 1 + 1) bytes.  */
69909  if( (p->pKeyInfo->nField + p->pKeyInfo->nXField)<=13 ){
69910    int flags = p->aMem[0].flags;
69911    if( p->pKeyInfo->aSortOrder[0] ){
69912      p->r1 = 1;
69913      p->r2 = -1;
69914    }else{
69915      p->r1 = -1;
69916      p->r2 = 1;
69917    }
69918    if( (flags & MEM_Int) ){
69919      return vdbeRecordCompareInt;
69920    }
69921    testcase( flags & MEM_Real );
69922    testcase( flags & MEM_Null );
69923    testcase( flags & MEM_Blob );
69924    if( (flags & (MEM_Real|MEM_Null|MEM_Blob))==0 && p->pKeyInfo->aColl[0]==0 ){
69925      assert( flags & MEM_Str );
69926      return vdbeRecordCompareString;
69927    }
69928  }
69929
69930  return sqlite3VdbeRecordCompare;
69931}
69932
69933/*
69934** pCur points at an index entry created using the OP_MakeRecord opcode.
69935** Read the rowid (the last field in the record) and store it in *rowid.
69936** Return SQLITE_OK if everything works, or an error code otherwise.
69937**
69938** pCur might be pointing to text obtained from a corrupt database file.
69939** So the content cannot be trusted.  Do appropriate checks on the content.
69940*/
69941SQLITE_PRIVATE int sqlite3VdbeIdxRowid(sqlite3 *db, BtCursor *pCur, i64 *rowid){
69942  i64 nCellKey = 0;
69943  int rc;
69944  u32 szHdr;        /* Size of the header */
69945  u32 typeRowid;    /* Serial type of the rowid */
69946  u32 lenRowid;     /* Size of the rowid */
69947  Mem m, v;
69948
69949  /* Get the size of the index entry.  Only indices entries of less
69950  ** than 2GiB are support - anything large must be database corruption.
69951  ** Any corruption is detected in sqlite3BtreeParseCellPtr(), though, so
69952  ** this code can safely assume that nCellKey is 32-bits
69953  */
69954  assert( sqlite3BtreeCursorIsValid(pCur) );
69955  VVA_ONLY(rc =) sqlite3BtreeKeySize(pCur, &nCellKey);
69956  assert( rc==SQLITE_OK );     /* pCur is always valid so KeySize cannot fail */
69957  assert( (nCellKey & SQLITE_MAX_U32)==(u64)nCellKey );
69958
69959  /* Read in the complete content of the index entry */
69960  sqlite3VdbeMemInit(&m, db, 0);
69961  rc = sqlite3VdbeMemFromBtree(pCur, 0, (u32)nCellKey, 1, &m);
69962  if( rc ){
69963    return rc;
69964  }
69965
69966  /* The index entry must begin with a header size */
69967  (void)getVarint32((u8*)m.z, szHdr);
69968  testcase( szHdr==3 );
69969  testcase( szHdr==m.n );
69970  if( unlikely(szHdr<3 || (int)szHdr>m.n) ){
69971    goto idx_rowid_corruption;
69972  }
69973
69974  /* The last field of the index should be an integer - the ROWID.
69975  ** Verify that the last entry really is an integer. */
69976  (void)getVarint32((u8*)&m.z[szHdr-1], typeRowid);
69977  testcase( typeRowid==1 );
69978  testcase( typeRowid==2 );
69979  testcase( typeRowid==3 );
69980  testcase( typeRowid==4 );
69981  testcase( typeRowid==5 );
69982  testcase( typeRowid==6 );
69983  testcase( typeRowid==8 );
69984  testcase( typeRowid==9 );
69985  if( unlikely(typeRowid<1 || typeRowid>9 || typeRowid==7) ){
69986    goto idx_rowid_corruption;
69987  }
69988  lenRowid = sqlite3SmallTypeSizes[typeRowid];
69989  testcase( (u32)m.n==szHdr+lenRowid );
69990  if( unlikely((u32)m.n<szHdr+lenRowid) ){
69991    goto idx_rowid_corruption;
69992  }
69993
69994  /* Fetch the integer off the end of the index record */
69995  sqlite3VdbeSerialGet((u8*)&m.z[m.n-lenRowid], typeRowid, &v);
69996  *rowid = v.u.i;
69997  sqlite3VdbeMemRelease(&m);
69998  return SQLITE_OK;
69999
70000  /* Jump here if database corruption is detected after m has been
70001  ** allocated.  Free the m object and return SQLITE_CORRUPT. */
70002idx_rowid_corruption:
70003  testcase( m.szMalloc!=0 );
70004  sqlite3VdbeMemRelease(&m);
70005  return SQLITE_CORRUPT_BKPT;
70006}
70007
70008/*
70009** Compare the key of the index entry that cursor pC is pointing to against
70010** the key string in pUnpacked.  Write into *pRes a number
70011** that is negative, zero, or positive if pC is less than, equal to,
70012** or greater than pUnpacked.  Return SQLITE_OK on success.
70013**
70014** pUnpacked is either created without a rowid or is truncated so that it
70015** omits the rowid at the end.  The rowid at the end of the index entry
70016** is ignored as well.  Hence, this routine only compares the prefixes
70017** of the keys prior to the final rowid, not the entire key.
70018*/
70019SQLITE_PRIVATE int sqlite3VdbeIdxKeyCompare(
70020  sqlite3 *db,                     /* Database connection */
70021  VdbeCursor *pC,                  /* The cursor to compare against */
70022  UnpackedRecord *pUnpacked,       /* Unpacked version of key */
70023  int *res                         /* Write the comparison result here */
70024){
70025  i64 nCellKey = 0;
70026  int rc;
70027  BtCursor *pCur = pC->pCursor;
70028  Mem m;
70029
70030  assert( sqlite3BtreeCursorIsValid(pCur) );
70031  VVA_ONLY(rc =) sqlite3BtreeKeySize(pCur, &nCellKey);
70032  assert( rc==SQLITE_OK );    /* pCur is always valid so KeySize cannot fail */
70033  /* nCellKey will always be between 0 and 0xffffffff because of the way
70034  ** that btreeParseCellPtr() and sqlite3GetVarint32() are implemented */
70035  if( nCellKey<=0 || nCellKey>0x7fffffff ){
70036    *res = 0;
70037    return SQLITE_CORRUPT_BKPT;
70038  }
70039  sqlite3VdbeMemInit(&m, db, 0);
70040  rc = sqlite3VdbeMemFromBtree(pC->pCursor, 0, (u32)nCellKey, 1, &m);
70041  if( rc ){
70042    return rc;
70043  }
70044  *res = sqlite3VdbeRecordCompare(m.n, m.z, pUnpacked);
70045  sqlite3VdbeMemRelease(&m);
70046  return SQLITE_OK;
70047}
70048
70049/*
70050** This routine sets the value to be returned by subsequent calls to
70051** sqlite3_changes() on the database handle 'db'.
70052*/
70053SQLITE_PRIVATE void sqlite3VdbeSetChanges(sqlite3 *db, int nChange){
70054  assert( sqlite3_mutex_held(db->mutex) );
70055  db->nChange = nChange;
70056  db->nTotalChange += nChange;
70057}
70058
70059/*
70060** Set a flag in the vdbe to update the change counter when it is finalised
70061** or reset.
70062*/
70063SQLITE_PRIVATE void sqlite3VdbeCountChanges(Vdbe *v){
70064  v->changeCntOn = 1;
70065}
70066
70067/*
70068** Mark every prepared statement associated with a database connection
70069** as expired.
70070**
70071** An expired statement means that recompilation of the statement is
70072** recommend.  Statements expire when things happen that make their
70073** programs obsolete.  Removing user-defined functions or collating
70074** sequences, or changing an authorization function are the types of
70075** things that make prepared statements obsolete.
70076*/
70077SQLITE_PRIVATE void sqlite3ExpirePreparedStatements(sqlite3 *db){
70078  Vdbe *p;
70079  for(p = db->pVdbe; p; p=p->pNext){
70080    p->expired = 1;
70081  }
70082}
70083
70084/*
70085** Return the database associated with the Vdbe.
70086*/
70087SQLITE_PRIVATE sqlite3 *sqlite3VdbeDb(Vdbe *v){
70088  return v->db;
70089}
70090
70091/*
70092** Return a pointer to an sqlite3_value structure containing the value bound
70093** parameter iVar of VM v. Except, if the value is an SQL NULL, return
70094** 0 instead. Unless it is NULL, apply affinity aff (one of the SQLITE_AFF_*
70095** constants) to the value before returning it.
70096**
70097** The returned value must be freed by the caller using sqlite3ValueFree().
70098*/
70099SQLITE_PRIVATE sqlite3_value *sqlite3VdbeGetBoundValue(Vdbe *v, int iVar, u8 aff){
70100  assert( iVar>0 );
70101  if( v ){
70102    Mem *pMem = &v->aVar[iVar-1];
70103    if( 0==(pMem->flags & MEM_Null) ){
70104      sqlite3_value *pRet = sqlite3ValueNew(v->db);
70105      if( pRet ){
70106        sqlite3VdbeMemCopy((Mem *)pRet, pMem);
70107        sqlite3ValueApplyAffinity(pRet, aff, SQLITE_UTF8);
70108      }
70109      return pRet;
70110    }
70111  }
70112  return 0;
70113}
70114
70115/*
70116** Configure SQL variable iVar so that binding a new value to it signals
70117** to sqlite3_reoptimize() that re-preparing the statement may result
70118** in a better query plan.
70119*/
70120SQLITE_PRIVATE void sqlite3VdbeSetVarmask(Vdbe *v, int iVar){
70121  assert( iVar>0 );
70122  if( iVar>32 ){
70123    v->expmask = 0xffffffff;
70124  }else{
70125    v->expmask |= ((u32)1 << (iVar-1));
70126  }
70127}
70128
70129#ifndef SQLITE_OMIT_VIRTUALTABLE
70130/*
70131** Transfer error message text from an sqlite3_vtab.zErrMsg (text stored
70132** in memory obtained from sqlite3_malloc) into a Vdbe.zErrMsg (text stored
70133** in memory obtained from sqlite3DbMalloc).
70134*/
70135SQLITE_PRIVATE void sqlite3VtabImportErrmsg(Vdbe *p, sqlite3_vtab *pVtab){
70136  sqlite3 *db = p->db;
70137  sqlite3DbFree(db, p->zErrMsg);
70138  p->zErrMsg = sqlite3DbStrDup(db, pVtab->zErrMsg);
70139  sqlite3_free(pVtab->zErrMsg);
70140  pVtab->zErrMsg = 0;
70141}
70142#endif /* SQLITE_OMIT_VIRTUALTABLE */
70143
70144/************** End of vdbeaux.c *********************************************/
70145/************** Begin file vdbeapi.c *****************************************/
70146/*
70147** 2004 May 26
70148**
70149** The author disclaims copyright to this source code.  In place of
70150** a legal notice, here is a blessing:
70151**
70152**    May you do good and not evil.
70153**    May you find forgiveness for yourself and forgive others.
70154**    May you share freely, never taking more than you give.
70155**
70156*************************************************************************
70157**
70158** This file contains code use to implement APIs that are part of the
70159** VDBE.
70160*/
70161/* #include "sqliteInt.h" */
70162/* #include "vdbeInt.h" */
70163
70164#ifndef SQLITE_OMIT_DEPRECATED
70165/*
70166** Return TRUE (non-zero) of the statement supplied as an argument needs
70167** to be recompiled.  A statement needs to be recompiled whenever the
70168** execution environment changes in a way that would alter the program
70169** that sqlite3_prepare() generates.  For example, if new functions or
70170** collating sequences are registered or if an authorizer function is
70171** added or changed.
70172*/
70173SQLITE_API int SQLITE_STDCALL sqlite3_expired(sqlite3_stmt *pStmt){
70174  Vdbe *p = (Vdbe*)pStmt;
70175  return p==0 || p->expired;
70176}
70177#endif
70178
70179/*
70180** Check on a Vdbe to make sure it has not been finalized.  Log
70181** an error and return true if it has been finalized (or is otherwise
70182** invalid).  Return false if it is ok.
70183*/
70184static int vdbeSafety(Vdbe *p){
70185  if( p->db==0 ){
70186    sqlite3_log(SQLITE_MISUSE, "API called with finalized prepared statement");
70187    return 1;
70188  }else{
70189    return 0;
70190  }
70191}
70192static int vdbeSafetyNotNull(Vdbe *p){
70193  if( p==0 ){
70194    sqlite3_log(SQLITE_MISUSE, "API called with NULL prepared statement");
70195    return 1;
70196  }else{
70197    return vdbeSafety(p);
70198  }
70199}
70200
70201#ifndef SQLITE_OMIT_TRACE
70202/*
70203** Invoke the profile callback.  This routine is only called if we already
70204** know that the profile callback is defined and needs to be invoked.
70205*/
70206static SQLITE_NOINLINE void invokeProfileCallback(sqlite3 *db, Vdbe *p){
70207  sqlite3_int64 iNow;
70208  assert( p->startTime>0 );
70209  assert( db->xProfile!=0 );
70210  assert( db->init.busy==0 );
70211  assert( p->zSql!=0 );
70212  sqlite3OsCurrentTimeInt64(db->pVfs, &iNow);
70213  db->xProfile(db->pProfileArg, p->zSql, (iNow - p->startTime)*1000000);
70214  p->startTime = 0;
70215}
70216/*
70217** The checkProfileCallback(DB,P) macro checks to see if a profile callback
70218** is needed, and it invokes the callback if it is needed.
70219*/
70220# define checkProfileCallback(DB,P) \
70221   if( ((P)->startTime)>0 ){ invokeProfileCallback(DB,P); }
70222#else
70223# define checkProfileCallback(DB,P)  /*no-op*/
70224#endif
70225
70226/*
70227** The following routine destroys a virtual machine that is created by
70228** the sqlite3_compile() routine. The integer returned is an SQLITE_
70229** success/failure code that describes the result of executing the virtual
70230** machine.
70231**
70232** This routine sets the error code and string returned by
70233** sqlite3_errcode(), sqlite3_errmsg() and sqlite3_errmsg16().
70234*/
70235SQLITE_API int SQLITE_STDCALL sqlite3_finalize(sqlite3_stmt *pStmt){
70236  int rc;
70237  if( pStmt==0 ){
70238    /* IMPLEMENTATION-OF: R-57228-12904 Invoking sqlite3_finalize() on a NULL
70239    ** pointer is a harmless no-op. */
70240    rc = SQLITE_OK;
70241  }else{
70242    Vdbe *v = (Vdbe*)pStmt;
70243    sqlite3 *db = v->db;
70244    if( vdbeSafety(v) ) return SQLITE_MISUSE_BKPT;
70245    sqlite3_mutex_enter(db->mutex);
70246    checkProfileCallback(db, v);
70247    rc = sqlite3VdbeFinalize(v);
70248    rc = sqlite3ApiExit(db, rc);
70249    sqlite3LeaveMutexAndCloseZombie(db);
70250  }
70251  return rc;
70252}
70253
70254/*
70255** Terminate the current execution of an SQL statement and reset it
70256** back to its starting state so that it can be reused. A success code from
70257** the prior execution is returned.
70258**
70259** This routine sets the error code and string returned by
70260** sqlite3_errcode(), sqlite3_errmsg() and sqlite3_errmsg16().
70261*/
70262SQLITE_API int SQLITE_STDCALL sqlite3_reset(sqlite3_stmt *pStmt){
70263  int rc;
70264  if( pStmt==0 ){
70265    rc = SQLITE_OK;
70266  }else{
70267    Vdbe *v = (Vdbe*)pStmt;
70268    sqlite3 *db = v->db;
70269    sqlite3_mutex_enter(db->mutex);
70270    checkProfileCallback(db, v);
70271    rc = sqlite3VdbeReset(v);
70272    sqlite3VdbeRewind(v);
70273    assert( (rc & (db->errMask))==rc );
70274    rc = sqlite3ApiExit(db, rc);
70275    sqlite3_mutex_leave(db->mutex);
70276  }
70277  return rc;
70278}
70279
70280/*
70281** Set all the parameters in the compiled SQL statement to NULL.
70282*/
70283SQLITE_API int SQLITE_STDCALL sqlite3_clear_bindings(sqlite3_stmt *pStmt){
70284  int i;
70285  int rc = SQLITE_OK;
70286  Vdbe *p = (Vdbe*)pStmt;
70287#if SQLITE_THREADSAFE
70288  sqlite3_mutex *mutex = ((Vdbe*)pStmt)->db->mutex;
70289#endif
70290  sqlite3_mutex_enter(mutex);
70291  for(i=0; i<p->nVar; i++){
70292    sqlite3VdbeMemRelease(&p->aVar[i]);
70293    p->aVar[i].flags = MEM_Null;
70294  }
70295  if( p->isPrepareV2 && p->expmask ){
70296    p->expired = 1;
70297  }
70298  sqlite3_mutex_leave(mutex);
70299  return rc;
70300}
70301
70302
70303/**************************** sqlite3_value_  *******************************
70304** The following routines extract information from a Mem or sqlite3_value
70305** structure.
70306*/
70307SQLITE_API const void *SQLITE_STDCALL sqlite3_value_blob(sqlite3_value *pVal){
70308  Mem *p = (Mem*)pVal;
70309  if( p->flags & (MEM_Blob|MEM_Str) ){
70310    if( sqlite3VdbeMemExpandBlob(p)!=SQLITE_OK ){
70311      assert( p->flags==MEM_Null && p->z==0 );
70312      return 0;
70313    }
70314    p->flags |= MEM_Blob;
70315    return p->n ? p->z : 0;
70316  }else{
70317    return sqlite3_value_text(pVal);
70318  }
70319}
70320SQLITE_API int SQLITE_STDCALL sqlite3_value_bytes(sqlite3_value *pVal){
70321  return sqlite3ValueBytes(pVal, SQLITE_UTF8);
70322}
70323SQLITE_API int SQLITE_STDCALL sqlite3_value_bytes16(sqlite3_value *pVal){
70324  return sqlite3ValueBytes(pVal, SQLITE_UTF16NATIVE);
70325}
70326SQLITE_API double SQLITE_STDCALL sqlite3_value_double(sqlite3_value *pVal){
70327  return sqlite3VdbeRealValue((Mem*)pVal);
70328}
70329SQLITE_API int SQLITE_STDCALL sqlite3_value_int(sqlite3_value *pVal){
70330  return (int)sqlite3VdbeIntValue((Mem*)pVal);
70331}
70332SQLITE_API sqlite_int64 SQLITE_STDCALL sqlite3_value_int64(sqlite3_value *pVal){
70333  return sqlite3VdbeIntValue((Mem*)pVal);
70334}
70335SQLITE_API const unsigned char *SQLITE_STDCALL sqlite3_value_text(sqlite3_value *pVal){
70336  return (const unsigned char *)sqlite3ValueText(pVal, SQLITE_UTF8);
70337}
70338#ifndef SQLITE_OMIT_UTF16
70339SQLITE_API const void *SQLITE_STDCALL sqlite3_value_text16(sqlite3_value* pVal){
70340  return sqlite3ValueText(pVal, SQLITE_UTF16NATIVE);
70341}
70342SQLITE_API const void *SQLITE_STDCALL sqlite3_value_text16be(sqlite3_value *pVal){
70343  return sqlite3ValueText(pVal, SQLITE_UTF16BE);
70344}
70345SQLITE_API const void *SQLITE_STDCALL sqlite3_value_text16le(sqlite3_value *pVal){
70346  return sqlite3ValueText(pVal, SQLITE_UTF16LE);
70347}
70348#endif /* SQLITE_OMIT_UTF16 */
70349/* EVIDENCE-OF: R-12793-43283 Every value in SQLite has one of five
70350** fundamental datatypes: 64-bit signed integer 64-bit IEEE floating
70351** point number string BLOB NULL
70352*/
70353SQLITE_API int SQLITE_STDCALL sqlite3_value_type(sqlite3_value* pVal){
70354  static const u8 aType[] = {
70355     SQLITE_BLOB,     /* 0x00 */
70356     SQLITE_NULL,     /* 0x01 */
70357     SQLITE_TEXT,     /* 0x02 */
70358     SQLITE_NULL,     /* 0x03 */
70359     SQLITE_INTEGER,  /* 0x04 */
70360     SQLITE_NULL,     /* 0x05 */
70361     SQLITE_INTEGER,  /* 0x06 */
70362     SQLITE_NULL,     /* 0x07 */
70363     SQLITE_FLOAT,    /* 0x08 */
70364     SQLITE_NULL,     /* 0x09 */
70365     SQLITE_FLOAT,    /* 0x0a */
70366     SQLITE_NULL,     /* 0x0b */
70367     SQLITE_INTEGER,  /* 0x0c */
70368     SQLITE_NULL,     /* 0x0d */
70369     SQLITE_INTEGER,  /* 0x0e */
70370     SQLITE_NULL,     /* 0x0f */
70371     SQLITE_BLOB,     /* 0x10 */
70372     SQLITE_NULL,     /* 0x11 */
70373     SQLITE_TEXT,     /* 0x12 */
70374     SQLITE_NULL,     /* 0x13 */
70375     SQLITE_INTEGER,  /* 0x14 */
70376     SQLITE_NULL,     /* 0x15 */
70377     SQLITE_INTEGER,  /* 0x16 */
70378     SQLITE_NULL,     /* 0x17 */
70379     SQLITE_FLOAT,    /* 0x18 */
70380     SQLITE_NULL,     /* 0x19 */
70381     SQLITE_FLOAT,    /* 0x1a */
70382     SQLITE_NULL,     /* 0x1b */
70383     SQLITE_INTEGER,  /* 0x1c */
70384     SQLITE_NULL,     /* 0x1d */
70385     SQLITE_INTEGER,  /* 0x1e */
70386     SQLITE_NULL,     /* 0x1f */
70387  };
70388  return aType[pVal->flags&MEM_AffMask];
70389}
70390
70391/* Make a copy of an sqlite3_value object
70392*/
70393SQLITE_API sqlite3_value *SQLITE_STDCALL sqlite3_value_dup(const sqlite3_value *pOrig){
70394  sqlite3_value *pNew;
70395  if( pOrig==0 ) return 0;
70396  pNew = sqlite3_malloc( sizeof(*pNew) );
70397  if( pNew==0 ) return 0;
70398  memset(pNew, 0, sizeof(*pNew));
70399  memcpy(pNew, pOrig, MEMCELLSIZE);
70400  pNew->flags &= ~MEM_Dyn;
70401  pNew->db = 0;
70402  if( pNew->flags&(MEM_Str|MEM_Blob) ){
70403    pNew->flags &= ~(MEM_Static|MEM_Dyn);
70404    pNew->flags |= MEM_Ephem;
70405    if( sqlite3VdbeMemMakeWriteable(pNew)!=SQLITE_OK ){
70406      sqlite3ValueFree(pNew);
70407      pNew = 0;
70408    }
70409  }
70410  return pNew;
70411}
70412
70413/* Destroy an sqlite3_value object previously obtained from
70414** sqlite3_value_dup().
70415*/
70416SQLITE_API void SQLITE_STDCALL sqlite3_value_free(sqlite3_value *pOld){
70417  sqlite3ValueFree(pOld);
70418}
70419
70420
70421/**************************** sqlite3_result_  *******************************
70422** The following routines are used by user-defined functions to specify
70423** the function result.
70424**
70425** The setStrOrError() function calls sqlite3VdbeMemSetStr() to store the
70426** result as a string or blob but if the string or blob is too large, it
70427** then sets the error code to SQLITE_TOOBIG
70428**
70429** The invokeValueDestructor(P,X) routine invokes destructor function X()
70430** on value P is not going to be used and need to be destroyed.
70431*/
70432static void setResultStrOrError(
70433  sqlite3_context *pCtx,  /* Function context */
70434  const char *z,          /* String pointer */
70435  int n,                  /* Bytes in string, or negative */
70436  u8 enc,                 /* Encoding of z.  0 for BLOBs */
70437  void (*xDel)(void*)     /* Destructor function */
70438){
70439  if( sqlite3VdbeMemSetStr(pCtx->pOut, z, n, enc, xDel)==SQLITE_TOOBIG ){
70440    sqlite3_result_error_toobig(pCtx);
70441  }
70442}
70443static int invokeValueDestructor(
70444  const void *p,             /* Value to destroy */
70445  void (*xDel)(void*),       /* The destructor */
70446  sqlite3_context *pCtx      /* Set a SQLITE_TOOBIG error if no NULL */
70447){
70448  assert( xDel!=SQLITE_DYNAMIC );
70449  if( xDel==0 ){
70450    /* noop */
70451  }else if( xDel==SQLITE_TRANSIENT ){
70452    /* noop */
70453  }else{
70454    xDel((void*)p);
70455  }
70456  if( pCtx ) sqlite3_result_error_toobig(pCtx);
70457  return SQLITE_TOOBIG;
70458}
70459SQLITE_API void SQLITE_STDCALL sqlite3_result_blob(
70460  sqlite3_context *pCtx,
70461  const void *z,
70462  int n,
70463  void (*xDel)(void *)
70464){
70465  assert( n>=0 );
70466  assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
70467  setResultStrOrError(pCtx, z, n, 0, xDel);
70468}
70469SQLITE_API void SQLITE_STDCALL sqlite3_result_blob64(
70470  sqlite3_context *pCtx,
70471  const void *z,
70472  sqlite3_uint64 n,
70473  void (*xDel)(void *)
70474){
70475  assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
70476  assert( xDel!=SQLITE_DYNAMIC );
70477  if( n>0x7fffffff ){
70478    (void)invokeValueDestructor(z, xDel, pCtx);
70479  }else{
70480    setResultStrOrError(pCtx, z, (int)n, 0, xDel);
70481  }
70482}
70483SQLITE_API void SQLITE_STDCALL sqlite3_result_double(sqlite3_context *pCtx, double rVal){
70484  assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
70485  sqlite3VdbeMemSetDouble(pCtx->pOut, rVal);
70486}
70487SQLITE_API void SQLITE_STDCALL sqlite3_result_error(sqlite3_context *pCtx, const char *z, int n){
70488  assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
70489  pCtx->isError = SQLITE_ERROR;
70490  pCtx->fErrorOrAux = 1;
70491  sqlite3VdbeMemSetStr(pCtx->pOut, z, n, SQLITE_UTF8, SQLITE_TRANSIENT);
70492}
70493#ifndef SQLITE_OMIT_UTF16
70494SQLITE_API void SQLITE_STDCALL sqlite3_result_error16(sqlite3_context *pCtx, const void *z, int n){
70495  assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
70496  pCtx->isError = SQLITE_ERROR;
70497  pCtx->fErrorOrAux = 1;
70498  sqlite3VdbeMemSetStr(pCtx->pOut, z, n, SQLITE_UTF16NATIVE, SQLITE_TRANSIENT);
70499}
70500#endif
70501SQLITE_API void SQLITE_STDCALL sqlite3_result_int(sqlite3_context *pCtx, int iVal){
70502  assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
70503  sqlite3VdbeMemSetInt64(pCtx->pOut, (i64)iVal);
70504}
70505SQLITE_API void SQLITE_STDCALL sqlite3_result_int64(sqlite3_context *pCtx, i64 iVal){
70506  assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
70507  sqlite3VdbeMemSetInt64(pCtx->pOut, iVal);
70508}
70509SQLITE_API void SQLITE_STDCALL sqlite3_result_null(sqlite3_context *pCtx){
70510  assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
70511  sqlite3VdbeMemSetNull(pCtx->pOut);
70512}
70513SQLITE_API void SQLITE_STDCALL sqlite3_result_text(
70514  sqlite3_context *pCtx,
70515  const char *z,
70516  int n,
70517  void (*xDel)(void *)
70518){
70519  assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
70520  setResultStrOrError(pCtx, z, n, SQLITE_UTF8, xDel);
70521}
70522SQLITE_API void SQLITE_STDCALL sqlite3_result_text64(
70523  sqlite3_context *pCtx,
70524  const char *z,
70525  sqlite3_uint64 n,
70526  void (*xDel)(void *),
70527  unsigned char enc
70528){
70529  assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
70530  assert( xDel!=SQLITE_DYNAMIC );
70531  if( enc==SQLITE_UTF16 ) enc = SQLITE_UTF16NATIVE;
70532  if( n>0x7fffffff ){
70533    (void)invokeValueDestructor(z, xDel, pCtx);
70534  }else{
70535    setResultStrOrError(pCtx, z, (int)n, enc, xDel);
70536  }
70537}
70538#ifndef SQLITE_OMIT_UTF16
70539SQLITE_API void SQLITE_STDCALL sqlite3_result_text16(
70540  sqlite3_context *pCtx,
70541  const void *z,
70542  int n,
70543  void (*xDel)(void *)
70544){
70545  assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
70546  setResultStrOrError(pCtx, z, n, SQLITE_UTF16NATIVE, xDel);
70547}
70548SQLITE_API void SQLITE_STDCALL sqlite3_result_text16be(
70549  sqlite3_context *pCtx,
70550  const void *z,
70551  int n,
70552  void (*xDel)(void *)
70553){
70554  assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
70555  setResultStrOrError(pCtx, z, n, SQLITE_UTF16BE, xDel);
70556}
70557SQLITE_API void SQLITE_STDCALL sqlite3_result_text16le(
70558  sqlite3_context *pCtx,
70559  const void *z,
70560  int n,
70561  void (*xDel)(void *)
70562){
70563  assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
70564  setResultStrOrError(pCtx, z, n, SQLITE_UTF16LE, xDel);
70565}
70566#endif /* SQLITE_OMIT_UTF16 */
70567SQLITE_API void SQLITE_STDCALL sqlite3_result_value(sqlite3_context *pCtx, sqlite3_value *pValue){
70568  assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
70569  sqlite3VdbeMemCopy(pCtx->pOut, pValue);
70570}
70571SQLITE_API void SQLITE_STDCALL sqlite3_result_zeroblob(sqlite3_context *pCtx, int n){
70572  assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
70573  sqlite3VdbeMemSetZeroBlob(pCtx->pOut, n);
70574}
70575SQLITE_API int SQLITE_STDCALL sqlite3_result_zeroblob64(sqlite3_context *pCtx, u64 n){
70576  Mem *pOut = pCtx->pOut;
70577  assert( sqlite3_mutex_held(pOut->db->mutex) );
70578  if( n>(u64)pOut->db->aLimit[SQLITE_LIMIT_LENGTH] ){
70579    return SQLITE_TOOBIG;
70580  }
70581  sqlite3VdbeMemSetZeroBlob(pCtx->pOut, (int)n);
70582  return SQLITE_OK;
70583}
70584SQLITE_API void SQLITE_STDCALL sqlite3_result_error_code(sqlite3_context *pCtx, int errCode){
70585  pCtx->isError = errCode;
70586  pCtx->fErrorOrAux = 1;
70587#ifdef SQLITE_DEBUG
70588  if( pCtx->pVdbe ) pCtx->pVdbe->rcApp = errCode;
70589#endif
70590  if( pCtx->pOut->flags & MEM_Null ){
70591    sqlite3VdbeMemSetStr(pCtx->pOut, sqlite3ErrStr(errCode), -1,
70592                         SQLITE_UTF8, SQLITE_STATIC);
70593  }
70594}
70595
70596/* Force an SQLITE_TOOBIG error. */
70597SQLITE_API void SQLITE_STDCALL sqlite3_result_error_toobig(sqlite3_context *pCtx){
70598  assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
70599  pCtx->isError = SQLITE_TOOBIG;
70600  pCtx->fErrorOrAux = 1;
70601  sqlite3VdbeMemSetStr(pCtx->pOut, "string or blob too big", -1,
70602                       SQLITE_UTF8, SQLITE_STATIC);
70603}
70604
70605/* An SQLITE_NOMEM error. */
70606SQLITE_API void SQLITE_STDCALL sqlite3_result_error_nomem(sqlite3_context *pCtx){
70607  assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
70608  sqlite3VdbeMemSetNull(pCtx->pOut);
70609  pCtx->isError = SQLITE_NOMEM;
70610  pCtx->fErrorOrAux = 1;
70611  pCtx->pOut->db->mallocFailed = 1;
70612}
70613
70614/*
70615** This function is called after a transaction has been committed. It
70616** invokes callbacks registered with sqlite3_wal_hook() as required.
70617*/
70618static int doWalCallbacks(sqlite3 *db){
70619  int rc = SQLITE_OK;
70620#ifndef SQLITE_OMIT_WAL
70621  int i;
70622  for(i=0; i<db->nDb; i++){
70623    Btree *pBt = db->aDb[i].pBt;
70624    if( pBt ){
70625      int nEntry;
70626      sqlite3BtreeEnter(pBt);
70627      nEntry = sqlite3PagerWalCallback(sqlite3BtreePager(pBt));
70628      sqlite3BtreeLeave(pBt);
70629      if( db->xWalCallback && nEntry>0 && rc==SQLITE_OK ){
70630        rc = db->xWalCallback(db->pWalArg, db, db->aDb[i].zName, nEntry);
70631      }
70632    }
70633  }
70634#endif
70635  return rc;
70636}
70637
70638
70639/*
70640** Execute the statement pStmt, either until a row of data is ready, the
70641** statement is completely executed or an error occurs.
70642**
70643** This routine implements the bulk of the logic behind the sqlite_step()
70644** API.  The only thing omitted is the automatic recompile if a
70645** schema change has occurred.  That detail is handled by the
70646** outer sqlite3_step() wrapper procedure.
70647*/
70648static int sqlite3Step(Vdbe *p){
70649  sqlite3 *db;
70650  int rc;
70651
70652  assert(p);
70653  if( p->magic!=VDBE_MAGIC_RUN ){
70654    /* We used to require that sqlite3_reset() be called before retrying
70655    ** sqlite3_step() after any error or after SQLITE_DONE.  But beginning
70656    ** with version 3.7.0, we changed this so that sqlite3_reset() would
70657    ** be called automatically instead of throwing the SQLITE_MISUSE error.
70658    ** This "automatic-reset" change is not technically an incompatibility,
70659    ** since any application that receives an SQLITE_MISUSE is broken by
70660    ** definition.
70661    **
70662    ** Nevertheless, some published applications that were originally written
70663    ** for version 3.6.23 or earlier do in fact depend on SQLITE_MISUSE
70664    ** returns, and those were broken by the automatic-reset change.  As a
70665    ** a work-around, the SQLITE_OMIT_AUTORESET compile-time restores the
70666    ** legacy behavior of returning SQLITE_MISUSE for cases where the
70667    ** previous sqlite3_step() returned something other than a SQLITE_LOCKED
70668    ** or SQLITE_BUSY error.
70669    */
70670#ifdef SQLITE_OMIT_AUTORESET
70671    if( (rc = p->rc&0xff)==SQLITE_BUSY || rc==SQLITE_LOCKED ){
70672      sqlite3_reset((sqlite3_stmt*)p);
70673    }else{
70674      return SQLITE_MISUSE_BKPT;
70675    }
70676#else
70677    sqlite3_reset((sqlite3_stmt*)p);
70678#endif
70679  }
70680
70681  /* Check that malloc() has not failed. If it has, return early. */
70682  db = p->db;
70683  if( db->mallocFailed ){
70684    p->rc = SQLITE_NOMEM;
70685    return SQLITE_NOMEM;
70686  }
70687
70688  if( p->pc<=0 && p->expired ){
70689    p->rc = SQLITE_SCHEMA;
70690    rc = SQLITE_ERROR;
70691    goto end_of_step;
70692  }
70693  if( p->pc<0 ){
70694    /* If there are no other statements currently running, then
70695    ** reset the interrupt flag.  This prevents a call to sqlite3_interrupt
70696    ** from interrupting a statement that has not yet started.
70697    */
70698    if( db->nVdbeActive==0 ){
70699      db->u1.isInterrupted = 0;
70700    }
70701
70702    assert( db->nVdbeWrite>0 || db->autoCommit==0
70703        || (db->nDeferredCons==0 && db->nDeferredImmCons==0)
70704    );
70705
70706#ifndef SQLITE_OMIT_TRACE
70707    if( db->xProfile && !db->init.busy && p->zSql ){
70708      sqlite3OsCurrentTimeInt64(db->pVfs, &p->startTime);
70709    }else{
70710      assert( p->startTime==0 );
70711    }
70712#endif
70713
70714    db->nVdbeActive++;
70715    if( p->readOnly==0 ) db->nVdbeWrite++;
70716    if( p->bIsReader ) db->nVdbeRead++;
70717    p->pc = 0;
70718  }
70719#ifdef SQLITE_DEBUG
70720  p->rcApp = SQLITE_OK;
70721#endif
70722#ifndef SQLITE_OMIT_EXPLAIN
70723  if( p->explain ){
70724    rc = sqlite3VdbeList(p);
70725  }else
70726#endif /* SQLITE_OMIT_EXPLAIN */
70727  {
70728    db->nVdbeExec++;
70729    rc = sqlite3VdbeExec(p);
70730    db->nVdbeExec--;
70731  }
70732
70733#ifndef SQLITE_OMIT_TRACE
70734  /* If the statement completed successfully, invoke the profile callback */
70735  if( rc!=SQLITE_ROW ) checkProfileCallback(db, p);
70736#endif
70737
70738  if( rc==SQLITE_DONE ){
70739    assert( p->rc==SQLITE_OK );
70740    p->rc = doWalCallbacks(db);
70741    if( p->rc!=SQLITE_OK ){
70742      rc = SQLITE_ERROR;
70743    }
70744  }
70745
70746  db->errCode = rc;
70747  if( SQLITE_NOMEM==sqlite3ApiExit(p->db, p->rc) ){
70748    p->rc = SQLITE_NOMEM;
70749  }
70750end_of_step:
70751  /* At this point local variable rc holds the value that should be
70752  ** returned if this statement was compiled using the legacy
70753  ** sqlite3_prepare() interface. According to the docs, this can only
70754  ** be one of the values in the first assert() below. Variable p->rc
70755  ** contains the value that would be returned if sqlite3_finalize()
70756  ** were called on statement p.
70757  */
70758  assert( rc==SQLITE_ROW  || rc==SQLITE_DONE   || rc==SQLITE_ERROR
70759       || rc==SQLITE_BUSY || rc==SQLITE_MISUSE
70760  );
70761  assert( (p->rc!=SQLITE_ROW && p->rc!=SQLITE_DONE) || p->rc==p->rcApp );
70762  if( p->isPrepareV2 && rc!=SQLITE_ROW && rc!=SQLITE_DONE ){
70763    /* If this statement was prepared using sqlite3_prepare_v2(), and an
70764    ** error has occurred, then return the error code in p->rc to the
70765    ** caller. Set the error code in the database handle to the same value.
70766    */
70767    rc = sqlite3VdbeTransferError(p);
70768  }
70769  return (rc&db->errMask);
70770}
70771
70772/*
70773** This is the top-level implementation of sqlite3_step().  Call
70774** sqlite3Step() to do most of the work.  If a schema error occurs,
70775** call sqlite3Reprepare() and try again.
70776*/
70777SQLITE_API int SQLITE_STDCALL sqlite3_step(sqlite3_stmt *pStmt){
70778  int rc = SQLITE_OK;      /* Result from sqlite3Step() */
70779  int rc2 = SQLITE_OK;     /* Result from sqlite3Reprepare() */
70780  Vdbe *v = (Vdbe*)pStmt;  /* the prepared statement */
70781  int cnt = 0;             /* Counter to prevent infinite loop of reprepares */
70782  sqlite3 *db;             /* The database connection */
70783
70784  if( vdbeSafetyNotNull(v) ){
70785    return SQLITE_MISUSE_BKPT;
70786  }
70787  db = v->db;
70788  sqlite3_mutex_enter(db->mutex);
70789  v->doingRerun = 0;
70790  while( (rc = sqlite3Step(v))==SQLITE_SCHEMA
70791         && cnt++ < SQLITE_MAX_SCHEMA_RETRY ){
70792    int savedPc = v->pc;
70793    rc2 = rc = sqlite3Reprepare(v);
70794    if( rc!=SQLITE_OK) break;
70795    sqlite3_reset(pStmt);
70796    if( savedPc>=0 ) v->doingRerun = 1;
70797    assert( v->expired==0 );
70798  }
70799  if( rc2!=SQLITE_OK ){
70800    /* This case occurs after failing to recompile an sql statement.
70801    ** The error message from the SQL compiler has already been loaded
70802    ** into the database handle. This block copies the error message
70803    ** from the database handle into the statement and sets the statement
70804    ** program counter to 0 to ensure that when the statement is
70805    ** finalized or reset the parser error message is available via
70806    ** sqlite3_errmsg() and sqlite3_errcode().
70807    */
70808    const char *zErr = (const char *)sqlite3_value_text(db->pErr);
70809    sqlite3DbFree(db, v->zErrMsg);
70810    if( !db->mallocFailed ){
70811      v->zErrMsg = sqlite3DbStrDup(db, zErr);
70812      v->rc = rc2;
70813    } else {
70814      v->zErrMsg = 0;
70815      v->rc = rc = SQLITE_NOMEM;
70816    }
70817  }
70818  rc = sqlite3ApiExit(db, rc);
70819  sqlite3_mutex_leave(db->mutex);
70820  return rc;
70821}
70822
70823
70824/*
70825** Extract the user data from a sqlite3_context structure and return a
70826** pointer to it.
70827*/
70828SQLITE_API void *SQLITE_STDCALL sqlite3_user_data(sqlite3_context *p){
70829  assert( p && p->pFunc );
70830  return p->pFunc->pUserData;
70831}
70832
70833/*
70834** Extract the user data from a sqlite3_context structure and return a
70835** pointer to it.
70836**
70837** IMPLEMENTATION-OF: R-46798-50301 The sqlite3_context_db_handle() interface
70838** returns a copy of the pointer to the database connection (the 1st
70839** parameter) of the sqlite3_create_function() and
70840** sqlite3_create_function16() routines that originally registered the
70841** application defined function.
70842*/
70843SQLITE_API sqlite3 *SQLITE_STDCALL sqlite3_context_db_handle(sqlite3_context *p){
70844  assert( p && p->pFunc );
70845  return p->pOut->db;
70846}
70847
70848/*
70849** Return the current time for a statement.  If the current time
70850** is requested more than once within the same run of a single prepared
70851** statement, the exact same time is returned for each invocation regardless
70852** of the amount of time that elapses between invocations.  In other words,
70853** the time returned is always the time of the first call.
70854*/
70855SQLITE_PRIVATE sqlite3_int64 sqlite3StmtCurrentTime(sqlite3_context *p){
70856  int rc;
70857#ifndef SQLITE_ENABLE_STAT3_OR_STAT4
70858  sqlite3_int64 *piTime = &p->pVdbe->iCurrentTime;
70859  assert( p->pVdbe!=0 );
70860#else
70861  sqlite3_int64 iTime = 0;
70862  sqlite3_int64 *piTime = p->pVdbe!=0 ? &p->pVdbe->iCurrentTime : &iTime;
70863#endif
70864  if( *piTime==0 ){
70865    rc = sqlite3OsCurrentTimeInt64(p->pOut->db->pVfs, piTime);
70866    if( rc ) *piTime = 0;
70867  }
70868  return *piTime;
70869}
70870
70871/*
70872** The following is the implementation of an SQL function that always
70873** fails with an error message stating that the function is used in the
70874** wrong context.  The sqlite3_overload_function() API might construct
70875** SQL function that use this routine so that the functions will exist
70876** for name resolution but are actually overloaded by the xFindFunction
70877** method of virtual tables.
70878*/
70879SQLITE_PRIVATE void sqlite3InvalidFunction(
70880  sqlite3_context *context,  /* The function calling context */
70881  int NotUsed,               /* Number of arguments to the function */
70882  sqlite3_value **NotUsed2   /* Value of each argument */
70883){
70884  const char *zName = context->pFunc->zName;
70885  char *zErr;
70886  UNUSED_PARAMETER2(NotUsed, NotUsed2);
70887  zErr = sqlite3_mprintf(
70888      "unable to use function %s in the requested context", zName);
70889  sqlite3_result_error(context, zErr, -1);
70890  sqlite3_free(zErr);
70891}
70892
70893/*
70894** Create a new aggregate context for p and return a pointer to
70895** its pMem->z element.
70896*/
70897static SQLITE_NOINLINE void *createAggContext(sqlite3_context *p, int nByte){
70898  Mem *pMem = p->pMem;
70899  assert( (pMem->flags & MEM_Agg)==0 );
70900  if( nByte<=0 ){
70901    sqlite3VdbeMemSetNull(pMem);
70902    pMem->z = 0;
70903  }else{
70904    sqlite3VdbeMemClearAndResize(pMem, nByte);
70905    pMem->flags = MEM_Agg;
70906    pMem->u.pDef = p->pFunc;
70907    if( pMem->z ){
70908      memset(pMem->z, 0, nByte);
70909    }
70910  }
70911  return (void*)pMem->z;
70912}
70913
70914/*
70915** Allocate or return the aggregate context for a user function.  A new
70916** context is allocated on the first call.  Subsequent calls return the
70917** same context that was returned on prior calls.
70918*/
70919SQLITE_API void *SQLITE_STDCALL sqlite3_aggregate_context(sqlite3_context *p, int nByte){
70920  assert( p && p->pFunc && p->pFunc->xStep );
70921  assert( sqlite3_mutex_held(p->pOut->db->mutex) );
70922  testcase( nByte<0 );
70923  if( (p->pMem->flags & MEM_Agg)==0 ){
70924    return createAggContext(p, nByte);
70925  }else{
70926    return (void*)p->pMem->z;
70927  }
70928}
70929
70930/*
70931** Return the auxiliary data pointer, if any, for the iArg'th argument to
70932** the user-function defined by pCtx.
70933*/
70934SQLITE_API void *SQLITE_STDCALL sqlite3_get_auxdata(sqlite3_context *pCtx, int iArg){
70935  AuxData *pAuxData;
70936
70937  assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
70938#if SQLITE_ENABLE_STAT3_OR_STAT4
70939  if( pCtx->pVdbe==0 ) return 0;
70940#else
70941  assert( pCtx->pVdbe!=0 );
70942#endif
70943  for(pAuxData=pCtx->pVdbe->pAuxData; pAuxData; pAuxData=pAuxData->pNext){
70944    if( pAuxData->iOp==pCtx->iOp && pAuxData->iArg==iArg ) break;
70945  }
70946
70947  return (pAuxData ? pAuxData->pAux : 0);
70948}
70949
70950/*
70951** Set the auxiliary data pointer and delete function, for the iArg'th
70952** argument to the user-function defined by pCtx. Any previous value is
70953** deleted by calling the delete function specified when it was set.
70954*/
70955SQLITE_API void SQLITE_STDCALL sqlite3_set_auxdata(
70956  sqlite3_context *pCtx,
70957  int iArg,
70958  void *pAux,
70959  void (*xDelete)(void*)
70960){
70961  AuxData *pAuxData;
70962  Vdbe *pVdbe = pCtx->pVdbe;
70963
70964  assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
70965  if( iArg<0 ) goto failed;
70966#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
70967  if( pVdbe==0 ) goto failed;
70968#else
70969  assert( pVdbe!=0 );
70970#endif
70971
70972  for(pAuxData=pVdbe->pAuxData; pAuxData; pAuxData=pAuxData->pNext){
70973    if( pAuxData->iOp==pCtx->iOp && pAuxData->iArg==iArg ) break;
70974  }
70975  if( pAuxData==0 ){
70976    pAuxData = sqlite3DbMallocZero(pVdbe->db, sizeof(AuxData));
70977    if( !pAuxData ) goto failed;
70978    pAuxData->iOp = pCtx->iOp;
70979    pAuxData->iArg = iArg;
70980    pAuxData->pNext = pVdbe->pAuxData;
70981    pVdbe->pAuxData = pAuxData;
70982    if( pCtx->fErrorOrAux==0 ){
70983      pCtx->isError = 0;
70984      pCtx->fErrorOrAux = 1;
70985    }
70986  }else if( pAuxData->xDelete ){
70987    pAuxData->xDelete(pAuxData->pAux);
70988  }
70989
70990  pAuxData->pAux = pAux;
70991  pAuxData->xDelete = xDelete;
70992  return;
70993
70994failed:
70995  if( xDelete ){
70996    xDelete(pAux);
70997  }
70998}
70999
71000#ifndef SQLITE_OMIT_DEPRECATED
71001/*
71002** Return the number of times the Step function of an aggregate has been
71003** called.
71004**
71005** This function is deprecated.  Do not use it for new code.  It is
71006** provide only to avoid breaking legacy code.  New aggregate function
71007** implementations should keep their own counts within their aggregate
71008** context.
71009*/
71010SQLITE_API int SQLITE_STDCALL sqlite3_aggregate_count(sqlite3_context *p){
71011  assert( p && p->pMem && p->pFunc && p->pFunc->xStep );
71012  return p->pMem->n;
71013}
71014#endif
71015
71016/*
71017** Return the number of columns in the result set for the statement pStmt.
71018*/
71019SQLITE_API int SQLITE_STDCALL sqlite3_column_count(sqlite3_stmt *pStmt){
71020  Vdbe *pVm = (Vdbe *)pStmt;
71021  return pVm ? pVm->nResColumn : 0;
71022}
71023
71024/*
71025** Return the number of values available from the current row of the
71026** currently executing statement pStmt.
71027*/
71028SQLITE_API int SQLITE_STDCALL sqlite3_data_count(sqlite3_stmt *pStmt){
71029  Vdbe *pVm = (Vdbe *)pStmt;
71030  if( pVm==0 || pVm->pResultSet==0 ) return 0;
71031  return pVm->nResColumn;
71032}
71033
71034/*
71035** Return a pointer to static memory containing an SQL NULL value.
71036*/
71037static const Mem *columnNullValue(void){
71038  /* Even though the Mem structure contains an element
71039  ** of type i64, on certain architectures (x86) with certain compiler
71040  ** switches (-Os), gcc may align this Mem object on a 4-byte boundary
71041  ** instead of an 8-byte one. This all works fine, except that when
71042  ** running with SQLITE_DEBUG defined the SQLite code sometimes assert()s
71043  ** that a Mem structure is located on an 8-byte boundary. To prevent
71044  ** these assert()s from failing, when building with SQLITE_DEBUG defined
71045  ** using gcc, we force nullMem to be 8-byte aligned using the magical
71046  ** __attribute__((aligned(8))) macro.  */
71047  static const Mem nullMem
71048#if defined(SQLITE_DEBUG) && defined(__GNUC__)
71049    __attribute__((aligned(8)))
71050#endif
71051    = {
71052        /* .u          = */ {0},
71053        /* .flags      = */ MEM_Null,
71054        /* .enc        = */ 0,
71055        /* .n          = */ 0,
71056        /* .z          = */ 0,
71057        /* .zMalloc    = */ 0,
71058        /* .szMalloc   = */ 0,
71059        /* .iPadding1  = */ 0,
71060        /* .db         = */ 0,
71061        /* .xDel       = */ 0,
71062#ifdef SQLITE_DEBUG
71063        /* .pScopyFrom = */ 0,
71064        /* .pFiller    = */ 0,
71065#endif
71066      };
71067  return &nullMem;
71068}
71069
71070/*
71071** Check to see if column iCol of the given statement is valid.  If
71072** it is, return a pointer to the Mem for the value of that column.
71073** If iCol is not valid, return a pointer to a Mem which has a value
71074** of NULL.
71075*/
71076static Mem *columnMem(sqlite3_stmt *pStmt, int i){
71077  Vdbe *pVm;
71078  Mem *pOut;
71079
71080  pVm = (Vdbe *)pStmt;
71081  if( pVm && pVm->pResultSet!=0 && i<pVm->nResColumn && i>=0 ){
71082    sqlite3_mutex_enter(pVm->db->mutex);
71083    pOut = &pVm->pResultSet[i];
71084  }else{
71085    if( pVm && ALWAYS(pVm->db) ){
71086      sqlite3_mutex_enter(pVm->db->mutex);
71087      sqlite3Error(pVm->db, SQLITE_RANGE);
71088    }
71089    pOut = (Mem*)columnNullValue();
71090  }
71091  return pOut;
71092}
71093
71094/*
71095** This function is called after invoking an sqlite3_value_XXX function on a
71096** column value (i.e. a value returned by evaluating an SQL expression in the
71097** select list of a SELECT statement) that may cause a malloc() failure. If
71098** malloc() has failed, the threads mallocFailed flag is cleared and the result
71099** code of statement pStmt set to SQLITE_NOMEM.
71100**
71101** Specifically, this is called from within:
71102**
71103**     sqlite3_column_int()
71104**     sqlite3_column_int64()
71105**     sqlite3_column_text()
71106**     sqlite3_column_text16()
71107**     sqlite3_column_real()
71108**     sqlite3_column_bytes()
71109**     sqlite3_column_bytes16()
71110**     sqiite3_column_blob()
71111*/
71112static void columnMallocFailure(sqlite3_stmt *pStmt)
71113{
71114  /* If malloc() failed during an encoding conversion within an
71115  ** sqlite3_column_XXX API, then set the return code of the statement to
71116  ** SQLITE_NOMEM. The next call to _step() (if any) will return SQLITE_ERROR
71117  ** and _finalize() will return NOMEM.
71118  */
71119  Vdbe *p = (Vdbe *)pStmt;
71120  if( p ){
71121    p->rc = sqlite3ApiExit(p->db, p->rc);
71122    sqlite3_mutex_leave(p->db->mutex);
71123  }
71124}
71125
71126/**************************** sqlite3_column_  *******************************
71127** The following routines are used to access elements of the current row
71128** in the result set.
71129*/
71130SQLITE_API const void *SQLITE_STDCALL sqlite3_column_blob(sqlite3_stmt *pStmt, int i){
71131  const void *val;
71132  val = sqlite3_value_blob( columnMem(pStmt,i) );
71133  /* Even though there is no encoding conversion, value_blob() might
71134  ** need to call malloc() to expand the result of a zeroblob()
71135  ** expression.
71136  */
71137  columnMallocFailure(pStmt);
71138  return val;
71139}
71140SQLITE_API int SQLITE_STDCALL sqlite3_column_bytes(sqlite3_stmt *pStmt, int i){
71141  int val = sqlite3_value_bytes( columnMem(pStmt,i) );
71142  columnMallocFailure(pStmt);
71143  return val;
71144}
71145SQLITE_API int SQLITE_STDCALL sqlite3_column_bytes16(sqlite3_stmt *pStmt, int i){
71146  int val = sqlite3_value_bytes16( columnMem(pStmt,i) );
71147  columnMallocFailure(pStmt);
71148  return val;
71149}
71150SQLITE_API double SQLITE_STDCALL sqlite3_column_double(sqlite3_stmt *pStmt, int i){
71151  double val = sqlite3_value_double( columnMem(pStmt,i) );
71152  columnMallocFailure(pStmt);
71153  return val;
71154}
71155SQLITE_API int SQLITE_STDCALL sqlite3_column_int(sqlite3_stmt *pStmt, int i){
71156  int val = sqlite3_value_int( columnMem(pStmt,i) );
71157  columnMallocFailure(pStmt);
71158  return val;
71159}
71160SQLITE_API sqlite_int64 SQLITE_STDCALL sqlite3_column_int64(sqlite3_stmt *pStmt, int i){
71161  sqlite_int64 val = sqlite3_value_int64( columnMem(pStmt,i) );
71162  columnMallocFailure(pStmt);
71163  return val;
71164}
71165SQLITE_API const unsigned char *SQLITE_STDCALL sqlite3_column_text(sqlite3_stmt *pStmt, int i){
71166  const unsigned char *val = sqlite3_value_text( columnMem(pStmt,i) );
71167  columnMallocFailure(pStmt);
71168  return val;
71169}
71170SQLITE_API sqlite3_value *SQLITE_STDCALL sqlite3_column_value(sqlite3_stmt *pStmt, int i){
71171  Mem *pOut = columnMem(pStmt, i);
71172  if( pOut->flags&MEM_Static ){
71173    pOut->flags &= ~MEM_Static;
71174    pOut->flags |= MEM_Ephem;
71175  }
71176  columnMallocFailure(pStmt);
71177  return (sqlite3_value *)pOut;
71178}
71179#ifndef SQLITE_OMIT_UTF16
71180SQLITE_API const void *SQLITE_STDCALL sqlite3_column_text16(sqlite3_stmt *pStmt, int i){
71181  const void *val = sqlite3_value_text16( columnMem(pStmt,i) );
71182  columnMallocFailure(pStmt);
71183  return val;
71184}
71185#endif /* SQLITE_OMIT_UTF16 */
71186SQLITE_API int SQLITE_STDCALL sqlite3_column_type(sqlite3_stmt *pStmt, int i){
71187  int iType = sqlite3_value_type( columnMem(pStmt,i) );
71188  columnMallocFailure(pStmt);
71189  return iType;
71190}
71191
71192/*
71193** Convert the N-th element of pStmt->pColName[] into a string using
71194** xFunc() then return that string.  If N is out of range, return 0.
71195**
71196** There are up to 5 names for each column.  useType determines which
71197** name is returned.  Here are the names:
71198**
71199**    0      The column name as it should be displayed for output
71200**    1      The datatype name for the column
71201**    2      The name of the database that the column derives from
71202**    3      The name of the table that the column derives from
71203**    4      The name of the table column that the result column derives from
71204**
71205** If the result is not a simple column reference (if it is an expression
71206** or a constant) then useTypes 2, 3, and 4 return NULL.
71207*/
71208static const void *columnName(
71209  sqlite3_stmt *pStmt,
71210  int N,
71211  const void *(*xFunc)(Mem*),
71212  int useType
71213){
71214  const void *ret;
71215  Vdbe *p;
71216  int n;
71217  sqlite3 *db;
71218#ifdef SQLITE_ENABLE_API_ARMOR
71219  if( pStmt==0 ){
71220    (void)SQLITE_MISUSE_BKPT;
71221    return 0;
71222  }
71223#endif
71224  ret = 0;
71225  p = (Vdbe *)pStmt;
71226  db = p->db;
71227  assert( db!=0 );
71228  n = sqlite3_column_count(pStmt);
71229  if( N<n && N>=0 ){
71230    N += useType*n;
71231    sqlite3_mutex_enter(db->mutex);
71232    assert( db->mallocFailed==0 );
71233    ret = xFunc(&p->aColName[N]);
71234     /* A malloc may have failed inside of the xFunc() call. If this
71235    ** is the case, clear the mallocFailed flag and return NULL.
71236    */
71237    if( db->mallocFailed ){
71238      db->mallocFailed = 0;
71239      ret = 0;
71240    }
71241    sqlite3_mutex_leave(db->mutex);
71242  }
71243  return ret;
71244}
71245
71246/*
71247** Return the name of the Nth column of the result set returned by SQL
71248** statement pStmt.
71249*/
71250SQLITE_API const char *SQLITE_STDCALL sqlite3_column_name(sqlite3_stmt *pStmt, int N){
71251  return columnName(
71252      pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_NAME);
71253}
71254#ifndef SQLITE_OMIT_UTF16
71255SQLITE_API const void *SQLITE_STDCALL sqlite3_column_name16(sqlite3_stmt *pStmt, int N){
71256  return columnName(
71257      pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_NAME);
71258}
71259#endif
71260
71261/*
71262** Constraint:  If you have ENABLE_COLUMN_METADATA then you must
71263** not define OMIT_DECLTYPE.
71264*/
71265#if defined(SQLITE_OMIT_DECLTYPE) && defined(SQLITE_ENABLE_COLUMN_METADATA)
71266# error "Must not define both SQLITE_OMIT_DECLTYPE \
71267         and SQLITE_ENABLE_COLUMN_METADATA"
71268#endif
71269
71270#ifndef SQLITE_OMIT_DECLTYPE
71271/*
71272** Return the column declaration type (if applicable) of the 'i'th column
71273** of the result set of SQL statement pStmt.
71274*/
71275SQLITE_API const char *SQLITE_STDCALL sqlite3_column_decltype(sqlite3_stmt *pStmt, int N){
71276  return columnName(
71277      pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_DECLTYPE);
71278}
71279#ifndef SQLITE_OMIT_UTF16
71280SQLITE_API const void *SQLITE_STDCALL sqlite3_column_decltype16(sqlite3_stmt *pStmt, int N){
71281  return columnName(
71282      pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_DECLTYPE);
71283}
71284#endif /* SQLITE_OMIT_UTF16 */
71285#endif /* SQLITE_OMIT_DECLTYPE */
71286
71287#ifdef SQLITE_ENABLE_COLUMN_METADATA
71288/*
71289** Return the name of the database from which a result column derives.
71290** NULL is returned if the result column is an expression or constant or
71291** anything else which is not an unambiguous reference to a database column.
71292*/
71293SQLITE_API const char *SQLITE_STDCALL sqlite3_column_database_name(sqlite3_stmt *pStmt, int N){
71294  return columnName(
71295      pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_DATABASE);
71296}
71297#ifndef SQLITE_OMIT_UTF16
71298SQLITE_API const void *SQLITE_STDCALL sqlite3_column_database_name16(sqlite3_stmt *pStmt, int N){
71299  return columnName(
71300      pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_DATABASE);
71301}
71302#endif /* SQLITE_OMIT_UTF16 */
71303
71304/*
71305** Return the name of the table from which a result column derives.
71306** NULL is returned if the result column is an expression or constant or
71307** anything else which is not an unambiguous reference to a database column.
71308*/
71309SQLITE_API const char *SQLITE_STDCALL sqlite3_column_table_name(sqlite3_stmt *pStmt, int N){
71310  return columnName(
71311      pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_TABLE);
71312}
71313#ifndef SQLITE_OMIT_UTF16
71314SQLITE_API const void *SQLITE_STDCALL sqlite3_column_table_name16(sqlite3_stmt *pStmt, int N){
71315  return columnName(
71316      pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_TABLE);
71317}
71318#endif /* SQLITE_OMIT_UTF16 */
71319
71320/*
71321** Return the name of the table column from which a result column derives.
71322** NULL is returned if the result column is an expression or constant or
71323** anything else which is not an unambiguous reference to a database column.
71324*/
71325SQLITE_API const char *SQLITE_STDCALL sqlite3_column_origin_name(sqlite3_stmt *pStmt, int N){
71326  return columnName(
71327      pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_COLUMN);
71328}
71329#ifndef SQLITE_OMIT_UTF16
71330SQLITE_API const void *SQLITE_STDCALL sqlite3_column_origin_name16(sqlite3_stmt *pStmt, int N){
71331  return columnName(
71332      pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_COLUMN);
71333}
71334#endif /* SQLITE_OMIT_UTF16 */
71335#endif /* SQLITE_ENABLE_COLUMN_METADATA */
71336
71337
71338/******************************* sqlite3_bind_  ***************************
71339**
71340** Routines used to attach values to wildcards in a compiled SQL statement.
71341*/
71342/*
71343** Unbind the value bound to variable i in virtual machine p. This is the
71344** the same as binding a NULL value to the column. If the "i" parameter is
71345** out of range, then SQLITE_RANGE is returned. Othewise SQLITE_OK.
71346**
71347** A successful evaluation of this routine acquires the mutex on p.
71348** the mutex is released if any kind of error occurs.
71349**
71350** The error code stored in database p->db is overwritten with the return
71351** value in any case.
71352*/
71353static int vdbeUnbind(Vdbe *p, int i){
71354  Mem *pVar;
71355  if( vdbeSafetyNotNull(p) ){
71356    return SQLITE_MISUSE_BKPT;
71357  }
71358  sqlite3_mutex_enter(p->db->mutex);
71359  if( p->magic!=VDBE_MAGIC_RUN || p->pc>=0 ){
71360    sqlite3Error(p->db, SQLITE_MISUSE);
71361    sqlite3_mutex_leave(p->db->mutex);
71362    sqlite3_log(SQLITE_MISUSE,
71363        "bind on a busy prepared statement: [%s]", p->zSql);
71364    return SQLITE_MISUSE_BKPT;
71365  }
71366  if( i<1 || i>p->nVar ){
71367    sqlite3Error(p->db, SQLITE_RANGE);
71368    sqlite3_mutex_leave(p->db->mutex);
71369    return SQLITE_RANGE;
71370  }
71371  i--;
71372  pVar = &p->aVar[i];
71373  sqlite3VdbeMemRelease(pVar);
71374  pVar->flags = MEM_Null;
71375  sqlite3Error(p->db, SQLITE_OK);
71376
71377  /* If the bit corresponding to this variable in Vdbe.expmask is set, then
71378  ** binding a new value to this variable invalidates the current query plan.
71379  **
71380  ** IMPLEMENTATION-OF: R-48440-37595 If the specific value bound to host
71381  ** parameter in the WHERE clause might influence the choice of query plan
71382  ** for a statement, then the statement will be automatically recompiled,
71383  ** as if there had been a schema change, on the first sqlite3_step() call
71384  ** following any change to the bindings of that parameter.
71385  */
71386  if( p->isPrepareV2 &&
71387     ((i<32 && p->expmask & ((u32)1 << i)) || p->expmask==0xffffffff)
71388  ){
71389    p->expired = 1;
71390  }
71391  return SQLITE_OK;
71392}
71393
71394/*
71395** Bind a text or BLOB value.
71396*/
71397static int bindText(
71398  sqlite3_stmt *pStmt,   /* The statement to bind against */
71399  int i,                 /* Index of the parameter to bind */
71400  const void *zData,     /* Pointer to the data to be bound */
71401  int nData,             /* Number of bytes of data to be bound */
71402  void (*xDel)(void*),   /* Destructor for the data */
71403  u8 encoding            /* Encoding for the data */
71404){
71405  Vdbe *p = (Vdbe *)pStmt;
71406  Mem *pVar;
71407  int rc;
71408
71409  rc = vdbeUnbind(p, i);
71410  if( rc==SQLITE_OK ){
71411    if( zData!=0 ){
71412      pVar = &p->aVar[i-1];
71413      rc = sqlite3VdbeMemSetStr(pVar, zData, nData, encoding, xDel);
71414      if( rc==SQLITE_OK && encoding!=0 ){
71415        rc = sqlite3VdbeChangeEncoding(pVar, ENC(p->db));
71416      }
71417      sqlite3Error(p->db, rc);
71418      rc = sqlite3ApiExit(p->db, rc);
71419    }
71420    sqlite3_mutex_leave(p->db->mutex);
71421  }else if( xDel!=SQLITE_STATIC && xDel!=SQLITE_TRANSIENT ){
71422    xDel((void*)zData);
71423  }
71424  return rc;
71425}
71426
71427
71428/*
71429** Bind a blob value to an SQL statement variable.
71430*/
71431SQLITE_API int SQLITE_STDCALL sqlite3_bind_blob(
71432  sqlite3_stmt *pStmt,
71433  int i,
71434  const void *zData,
71435  int nData,
71436  void (*xDel)(void*)
71437){
71438  return bindText(pStmt, i, zData, nData, xDel, 0);
71439}
71440SQLITE_API int SQLITE_STDCALL sqlite3_bind_blob64(
71441  sqlite3_stmt *pStmt,
71442  int i,
71443  const void *zData,
71444  sqlite3_uint64 nData,
71445  void (*xDel)(void*)
71446){
71447  assert( xDel!=SQLITE_DYNAMIC );
71448  if( nData>0x7fffffff ){
71449    return invokeValueDestructor(zData, xDel, 0);
71450  }else{
71451    return bindText(pStmt, i, zData, (int)nData, xDel, 0);
71452  }
71453}
71454SQLITE_API int SQLITE_STDCALL sqlite3_bind_double(sqlite3_stmt *pStmt, int i, double rValue){
71455  int rc;
71456  Vdbe *p = (Vdbe *)pStmt;
71457  rc = vdbeUnbind(p, i);
71458  if( rc==SQLITE_OK ){
71459    sqlite3VdbeMemSetDouble(&p->aVar[i-1], rValue);
71460    sqlite3_mutex_leave(p->db->mutex);
71461  }
71462  return rc;
71463}
71464SQLITE_API int SQLITE_STDCALL sqlite3_bind_int(sqlite3_stmt *p, int i, int iValue){
71465  return sqlite3_bind_int64(p, i, (i64)iValue);
71466}
71467SQLITE_API int SQLITE_STDCALL sqlite3_bind_int64(sqlite3_stmt *pStmt, int i, sqlite_int64 iValue){
71468  int rc;
71469  Vdbe *p = (Vdbe *)pStmt;
71470  rc = vdbeUnbind(p, i);
71471  if( rc==SQLITE_OK ){
71472    sqlite3VdbeMemSetInt64(&p->aVar[i-1], iValue);
71473    sqlite3_mutex_leave(p->db->mutex);
71474  }
71475  return rc;
71476}
71477SQLITE_API int SQLITE_STDCALL sqlite3_bind_null(sqlite3_stmt *pStmt, int i){
71478  int rc;
71479  Vdbe *p = (Vdbe*)pStmt;
71480  rc = vdbeUnbind(p, i);
71481  if( rc==SQLITE_OK ){
71482    sqlite3_mutex_leave(p->db->mutex);
71483  }
71484  return rc;
71485}
71486SQLITE_API int SQLITE_STDCALL sqlite3_bind_text(
71487  sqlite3_stmt *pStmt,
71488  int i,
71489  const char *zData,
71490  int nData,
71491  void (*xDel)(void*)
71492){
71493  return bindText(pStmt, i, zData, nData, xDel, SQLITE_UTF8);
71494}
71495SQLITE_API int SQLITE_STDCALL sqlite3_bind_text64(
71496  sqlite3_stmt *pStmt,
71497  int i,
71498  const char *zData,
71499  sqlite3_uint64 nData,
71500  void (*xDel)(void*),
71501  unsigned char enc
71502){
71503  assert( xDel!=SQLITE_DYNAMIC );
71504  if( nData>0x7fffffff ){
71505    return invokeValueDestructor(zData, xDel, 0);
71506  }else{
71507    if( enc==SQLITE_UTF16 ) enc = SQLITE_UTF16NATIVE;
71508    return bindText(pStmt, i, zData, (int)nData, xDel, enc);
71509  }
71510}
71511#ifndef SQLITE_OMIT_UTF16
71512SQLITE_API int SQLITE_STDCALL sqlite3_bind_text16(
71513  sqlite3_stmt *pStmt,
71514  int i,
71515  const void *zData,
71516  int nData,
71517  void (*xDel)(void*)
71518){
71519  return bindText(pStmt, i, zData, nData, xDel, SQLITE_UTF16NATIVE);
71520}
71521#endif /* SQLITE_OMIT_UTF16 */
71522SQLITE_API int SQLITE_STDCALL sqlite3_bind_value(sqlite3_stmt *pStmt, int i, const sqlite3_value *pValue){
71523  int rc;
71524  switch( sqlite3_value_type((sqlite3_value*)pValue) ){
71525    case SQLITE_INTEGER: {
71526      rc = sqlite3_bind_int64(pStmt, i, pValue->u.i);
71527      break;
71528    }
71529    case SQLITE_FLOAT: {
71530      rc = sqlite3_bind_double(pStmt, i, pValue->u.r);
71531      break;
71532    }
71533    case SQLITE_BLOB: {
71534      if( pValue->flags & MEM_Zero ){
71535        rc = sqlite3_bind_zeroblob(pStmt, i, pValue->u.nZero);
71536      }else{
71537        rc = sqlite3_bind_blob(pStmt, i, pValue->z, pValue->n,SQLITE_TRANSIENT);
71538      }
71539      break;
71540    }
71541    case SQLITE_TEXT: {
71542      rc = bindText(pStmt,i,  pValue->z, pValue->n, SQLITE_TRANSIENT,
71543                              pValue->enc);
71544      break;
71545    }
71546    default: {
71547      rc = sqlite3_bind_null(pStmt, i);
71548      break;
71549    }
71550  }
71551  return rc;
71552}
71553SQLITE_API int SQLITE_STDCALL sqlite3_bind_zeroblob(sqlite3_stmt *pStmt, int i, int n){
71554  int rc;
71555  Vdbe *p = (Vdbe *)pStmt;
71556  rc = vdbeUnbind(p, i);
71557  if( rc==SQLITE_OK ){
71558    sqlite3VdbeMemSetZeroBlob(&p->aVar[i-1], n);
71559    sqlite3_mutex_leave(p->db->mutex);
71560  }
71561  return rc;
71562}
71563SQLITE_API int SQLITE_STDCALL sqlite3_bind_zeroblob64(sqlite3_stmt *pStmt, int i, sqlite3_uint64 n){
71564  int rc;
71565  Vdbe *p = (Vdbe *)pStmt;
71566  sqlite3_mutex_enter(p->db->mutex);
71567  if( n>(u64)p->db->aLimit[SQLITE_LIMIT_LENGTH] ){
71568    rc = SQLITE_TOOBIG;
71569  }else{
71570    assert( (n & 0x7FFFFFFF)==n );
71571    rc = sqlite3_bind_zeroblob(pStmt, i, n);
71572  }
71573  rc = sqlite3ApiExit(p->db, rc);
71574  sqlite3_mutex_leave(p->db->mutex);
71575  return rc;
71576}
71577
71578/*
71579** Return the number of wildcards that can be potentially bound to.
71580** This routine is added to support DBD::SQLite.
71581*/
71582SQLITE_API int SQLITE_STDCALL sqlite3_bind_parameter_count(sqlite3_stmt *pStmt){
71583  Vdbe *p = (Vdbe*)pStmt;
71584  return p ? p->nVar : 0;
71585}
71586
71587/*
71588** Return the name of a wildcard parameter.  Return NULL if the index
71589** is out of range or if the wildcard is unnamed.
71590**
71591** The result is always UTF-8.
71592*/
71593SQLITE_API const char *SQLITE_STDCALL sqlite3_bind_parameter_name(sqlite3_stmt *pStmt, int i){
71594  Vdbe *p = (Vdbe*)pStmt;
71595  if( p==0 || i<1 || i>p->nzVar ){
71596    return 0;
71597  }
71598  return p->azVar[i-1];
71599}
71600
71601/*
71602** Given a wildcard parameter name, return the index of the variable
71603** with that name.  If there is no variable with the given name,
71604** return 0.
71605*/
71606SQLITE_PRIVATE int sqlite3VdbeParameterIndex(Vdbe *p, const char *zName, int nName){
71607  int i;
71608  if( p==0 ){
71609    return 0;
71610  }
71611  if( zName ){
71612    for(i=0; i<p->nzVar; i++){
71613      const char *z = p->azVar[i];
71614      if( z && strncmp(z,zName,nName)==0 && z[nName]==0 ){
71615        return i+1;
71616      }
71617    }
71618  }
71619  return 0;
71620}
71621SQLITE_API int SQLITE_STDCALL sqlite3_bind_parameter_index(sqlite3_stmt *pStmt, const char *zName){
71622  return sqlite3VdbeParameterIndex((Vdbe*)pStmt, zName, sqlite3Strlen30(zName));
71623}
71624
71625/*
71626** Transfer all bindings from the first statement over to the second.
71627*/
71628SQLITE_PRIVATE int sqlite3TransferBindings(sqlite3_stmt *pFromStmt, sqlite3_stmt *pToStmt){
71629  Vdbe *pFrom = (Vdbe*)pFromStmt;
71630  Vdbe *pTo = (Vdbe*)pToStmt;
71631  int i;
71632  assert( pTo->db==pFrom->db );
71633  assert( pTo->nVar==pFrom->nVar );
71634  sqlite3_mutex_enter(pTo->db->mutex);
71635  for(i=0; i<pFrom->nVar; i++){
71636    sqlite3VdbeMemMove(&pTo->aVar[i], &pFrom->aVar[i]);
71637  }
71638  sqlite3_mutex_leave(pTo->db->mutex);
71639  return SQLITE_OK;
71640}
71641
71642#ifndef SQLITE_OMIT_DEPRECATED
71643/*
71644** Deprecated external interface.  Internal/core SQLite code
71645** should call sqlite3TransferBindings.
71646**
71647** It is misuse to call this routine with statements from different
71648** database connections.  But as this is a deprecated interface, we
71649** will not bother to check for that condition.
71650**
71651** If the two statements contain a different number of bindings, then
71652** an SQLITE_ERROR is returned.  Nothing else can go wrong, so otherwise
71653** SQLITE_OK is returned.
71654*/
71655SQLITE_API int SQLITE_STDCALL sqlite3_transfer_bindings(sqlite3_stmt *pFromStmt, sqlite3_stmt *pToStmt){
71656  Vdbe *pFrom = (Vdbe*)pFromStmt;
71657  Vdbe *pTo = (Vdbe*)pToStmt;
71658  if( pFrom->nVar!=pTo->nVar ){
71659    return SQLITE_ERROR;
71660  }
71661  if( pTo->isPrepareV2 && pTo->expmask ){
71662    pTo->expired = 1;
71663  }
71664  if( pFrom->isPrepareV2 && pFrom->expmask ){
71665    pFrom->expired = 1;
71666  }
71667  return sqlite3TransferBindings(pFromStmt, pToStmt);
71668}
71669#endif
71670
71671/*
71672** Return the sqlite3* database handle to which the prepared statement given
71673** in the argument belongs.  This is the same database handle that was
71674** the first argument to the sqlite3_prepare() that was used to create
71675** the statement in the first place.
71676*/
71677SQLITE_API sqlite3 *SQLITE_STDCALL sqlite3_db_handle(sqlite3_stmt *pStmt){
71678  return pStmt ? ((Vdbe*)pStmt)->db : 0;
71679}
71680
71681/*
71682** Return true if the prepared statement is guaranteed to not modify the
71683** database.
71684*/
71685SQLITE_API int SQLITE_STDCALL sqlite3_stmt_readonly(sqlite3_stmt *pStmt){
71686  return pStmt ? ((Vdbe*)pStmt)->readOnly : 1;
71687}
71688
71689/*
71690** Return true if the prepared statement is in need of being reset.
71691*/
71692SQLITE_API int SQLITE_STDCALL sqlite3_stmt_busy(sqlite3_stmt *pStmt){
71693  Vdbe *v = (Vdbe*)pStmt;
71694  return v!=0 && v->pc>=0 && v->magic==VDBE_MAGIC_RUN;
71695}
71696
71697/*
71698** Return a pointer to the next prepared statement after pStmt associated
71699** with database connection pDb.  If pStmt is NULL, return the first
71700** prepared statement for the database connection.  Return NULL if there
71701** are no more.
71702*/
71703SQLITE_API sqlite3_stmt *SQLITE_STDCALL sqlite3_next_stmt(sqlite3 *pDb, sqlite3_stmt *pStmt){
71704  sqlite3_stmt *pNext;
71705#ifdef SQLITE_ENABLE_API_ARMOR
71706  if( !sqlite3SafetyCheckOk(pDb) ){
71707    (void)SQLITE_MISUSE_BKPT;
71708    return 0;
71709  }
71710#endif
71711  sqlite3_mutex_enter(pDb->mutex);
71712  if( pStmt==0 ){
71713    pNext = (sqlite3_stmt*)pDb->pVdbe;
71714  }else{
71715    pNext = (sqlite3_stmt*)((Vdbe*)pStmt)->pNext;
71716  }
71717  sqlite3_mutex_leave(pDb->mutex);
71718  return pNext;
71719}
71720
71721/*
71722** Return the value of a status counter for a prepared statement
71723*/
71724SQLITE_API int SQLITE_STDCALL sqlite3_stmt_status(sqlite3_stmt *pStmt, int op, int resetFlag){
71725  Vdbe *pVdbe = (Vdbe*)pStmt;
71726  u32 v;
71727#ifdef SQLITE_ENABLE_API_ARMOR
71728  if( !pStmt ){
71729    (void)SQLITE_MISUSE_BKPT;
71730    return 0;
71731  }
71732#endif
71733  v = pVdbe->aCounter[op];
71734  if( resetFlag ) pVdbe->aCounter[op] = 0;
71735  return (int)v;
71736}
71737
71738#ifdef SQLITE_ENABLE_STMT_SCANSTATUS
71739/*
71740** Return status data for a single loop within query pStmt.
71741*/
71742SQLITE_API int SQLITE_STDCALL sqlite3_stmt_scanstatus(
71743  sqlite3_stmt *pStmt,            /* Prepared statement being queried */
71744  int idx,                        /* Index of loop to report on */
71745  int iScanStatusOp,              /* Which metric to return */
71746  void *pOut                      /* OUT: Write the answer here */
71747){
71748  Vdbe *p = (Vdbe*)pStmt;
71749  ScanStatus *pScan;
71750  if( idx<0 || idx>=p->nScan ) return 1;
71751  pScan = &p->aScan[idx];
71752  switch( iScanStatusOp ){
71753    case SQLITE_SCANSTAT_NLOOP: {
71754      *(sqlite3_int64*)pOut = p->anExec[pScan->addrLoop];
71755      break;
71756    }
71757    case SQLITE_SCANSTAT_NVISIT: {
71758      *(sqlite3_int64*)pOut = p->anExec[pScan->addrVisit];
71759      break;
71760    }
71761    case SQLITE_SCANSTAT_EST: {
71762      double r = 1.0;
71763      LogEst x = pScan->nEst;
71764      while( x<100 ){
71765        x += 10;
71766        r *= 0.5;
71767      }
71768      *(double*)pOut = r*sqlite3LogEstToInt(x);
71769      break;
71770    }
71771    case SQLITE_SCANSTAT_NAME: {
71772      *(const char**)pOut = pScan->zName;
71773      break;
71774    }
71775    case SQLITE_SCANSTAT_EXPLAIN: {
71776      if( pScan->addrExplain ){
71777        *(const char**)pOut = p->aOp[ pScan->addrExplain ].p4.z;
71778      }else{
71779        *(const char**)pOut = 0;
71780      }
71781      break;
71782    }
71783    case SQLITE_SCANSTAT_SELECTID: {
71784      if( pScan->addrExplain ){
71785        *(int*)pOut = p->aOp[ pScan->addrExplain ].p1;
71786      }else{
71787        *(int*)pOut = -1;
71788      }
71789      break;
71790    }
71791    default: {
71792      return 1;
71793    }
71794  }
71795  return 0;
71796}
71797
71798/*
71799** Zero all counters associated with the sqlite3_stmt_scanstatus() data.
71800*/
71801SQLITE_API void SQLITE_STDCALL sqlite3_stmt_scanstatus_reset(sqlite3_stmt *pStmt){
71802  Vdbe *p = (Vdbe*)pStmt;
71803  memset(p->anExec, 0, p->nOp * sizeof(i64));
71804}
71805#endif /* SQLITE_ENABLE_STMT_SCANSTATUS */
71806
71807/************** End of vdbeapi.c *********************************************/
71808/************** Begin file vdbetrace.c ***************************************/
71809/*
71810** 2009 November 25
71811**
71812** The author disclaims copyright to this source code.  In place of
71813** a legal notice, here is a blessing:
71814**
71815**    May you do good and not evil.
71816**    May you find forgiveness for yourself and forgive others.
71817**    May you share freely, never taking more than you give.
71818**
71819*************************************************************************
71820**
71821** This file contains code used to insert the values of host parameters
71822** (aka "wildcards") into the SQL text output by sqlite3_trace().
71823**
71824** The Vdbe parse-tree explainer is also found here.
71825*/
71826/* #include "sqliteInt.h" */
71827/* #include "vdbeInt.h" */
71828
71829#ifndef SQLITE_OMIT_TRACE
71830
71831/*
71832** zSql is a zero-terminated string of UTF-8 SQL text.  Return the number of
71833** bytes in this text up to but excluding the first character in
71834** a host parameter.  If the text contains no host parameters, return
71835** the total number of bytes in the text.
71836*/
71837static int findNextHostParameter(const char *zSql, int *pnToken){
71838  int tokenType;
71839  int nTotal = 0;
71840  int n;
71841
71842  *pnToken = 0;
71843  while( zSql[0] ){
71844    n = sqlite3GetToken((u8*)zSql, &tokenType);
71845    assert( n>0 && tokenType!=TK_ILLEGAL );
71846    if( tokenType==TK_VARIABLE ){
71847      *pnToken = n;
71848      break;
71849    }
71850    nTotal += n;
71851    zSql += n;
71852  }
71853  return nTotal;
71854}
71855
71856/*
71857** This function returns a pointer to a nul-terminated string in memory
71858** obtained from sqlite3DbMalloc(). If sqlite3.nVdbeExec is 1, then the
71859** string contains a copy of zRawSql but with host parameters expanded to
71860** their current bindings. Or, if sqlite3.nVdbeExec is greater than 1,
71861** then the returned string holds a copy of zRawSql with "-- " prepended
71862** to each line of text.
71863**
71864** If the SQLITE_TRACE_SIZE_LIMIT macro is defined to an integer, then
71865** then long strings and blobs are truncated to that many bytes.  This
71866** can be used to prevent unreasonably large trace strings when dealing
71867** with large (multi-megabyte) strings and blobs.
71868**
71869** The calling function is responsible for making sure the memory returned
71870** is eventually freed.
71871**
71872** ALGORITHM:  Scan the input string looking for host parameters in any of
71873** these forms:  ?, ?N, $A, @A, :A.  Take care to avoid text within
71874** string literals, quoted identifier names, and comments.  For text forms,
71875** the host parameter index is found by scanning the prepared
71876** statement for the corresponding OP_Variable opcode.  Once the host
71877** parameter index is known, locate the value in p->aVar[].  Then render
71878** the value as a literal in place of the host parameter name.
71879*/
71880SQLITE_PRIVATE char *sqlite3VdbeExpandSql(
71881  Vdbe *p,                 /* The prepared statement being evaluated */
71882  const char *zRawSql      /* Raw text of the SQL statement */
71883){
71884  sqlite3 *db;             /* The database connection */
71885  int idx = 0;             /* Index of a host parameter */
71886  int nextIndex = 1;       /* Index of next ? host parameter */
71887  int n;                   /* Length of a token prefix */
71888  int nToken;              /* Length of the parameter token */
71889  int i;                   /* Loop counter */
71890  Mem *pVar;               /* Value of a host parameter */
71891  StrAccum out;            /* Accumulate the output here */
71892  char zBase[100];         /* Initial working space */
71893
71894  db = p->db;
71895  sqlite3StrAccumInit(&out, db, zBase, sizeof(zBase),
71896                      db->aLimit[SQLITE_LIMIT_LENGTH]);
71897  if( db->nVdbeExec>1 ){
71898    while( *zRawSql ){
71899      const char *zStart = zRawSql;
71900      while( *(zRawSql++)!='\n' && *zRawSql );
71901      sqlite3StrAccumAppend(&out, "-- ", 3);
71902      assert( (zRawSql - zStart) > 0 );
71903      sqlite3StrAccumAppend(&out, zStart, (int)(zRawSql-zStart));
71904    }
71905  }else if( p->nVar==0 ){
71906    sqlite3StrAccumAppend(&out, zRawSql, sqlite3Strlen30(zRawSql));
71907  }else{
71908    while( zRawSql[0] ){
71909      n = findNextHostParameter(zRawSql, &nToken);
71910      assert( n>0 );
71911      sqlite3StrAccumAppend(&out, zRawSql, n);
71912      zRawSql += n;
71913      assert( zRawSql[0] || nToken==0 );
71914      if( nToken==0 ) break;
71915      if( zRawSql[0]=='?' ){
71916        if( nToken>1 ){
71917          assert( sqlite3Isdigit(zRawSql[1]) );
71918          sqlite3GetInt32(&zRawSql[1], &idx);
71919        }else{
71920          idx = nextIndex;
71921        }
71922      }else{
71923        assert( zRawSql[0]==':' || zRawSql[0]=='$' ||
71924                zRawSql[0]=='@' || zRawSql[0]=='#' );
71925        testcase( zRawSql[0]==':' );
71926        testcase( zRawSql[0]=='$' );
71927        testcase( zRawSql[0]=='@' );
71928        testcase( zRawSql[0]=='#' );
71929        idx = sqlite3VdbeParameterIndex(p, zRawSql, nToken);
71930        assert( idx>0 );
71931      }
71932      zRawSql += nToken;
71933      nextIndex = idx + 1;
71934      assert( idx>0 && idx<=p->nVar );
71935      pVar = &p->aVar[idx-1];
71936      if( pVar->flags & MEM_Null ){
71937        sqlite3StrAccumAppend(&out, "NULL", 4);
71938      }else if( pVar->flags & MEM_Int ){
71939        sqlite3XPrintf(&out, 0, "%lld", pVar->u.i);
71940      }else if( pVar->flags & MEM_Real ){
71941        sqlite3XPrintf(&out, 0, "%!.15g", pVar->u.r);
71942      }else if( pVar->flags & MEM_Str ){
71943        int nOut;  /* Number of bytes of the string text to include in output */
71944#ifndef SQLITE_OMIT_UTF16
71945        u8 enc = ENC(db);
71946        Mem utf8;
71947        if( enc!=SQLITE_UTF8 ){
71948          memset(&utf8, 0, sizeof(utf8));
71949          utf8.db = db;
71950          sqlite3VdbeMemSetStr(&utf8, pVar->z, pVar->n, enc, SQLITE_STATIC);
71951          sqlite3VdbeChangeEncoding(&utf8, SQLITE_UTF8);
71952          pVar = &utf8;
71953        }
71954#endif
71955        nOut = pVar->n;
71956#ifdef SQLITE_TRACE_SIZE_LIMIT
71957        if( nOut>SQLITE_TRACE_SIZE_LIMIT ){
71958          nOut = SQLITE_TRACE_SIZE_LIMIT;
71959          while( nOut<pVar->n && (pVar->z[nOut]&0xc0)==0x80 ){ nOut++; }
71960        }
71961#endif
71962        sqlite3XPrintf(&out, 0, "'%.*q'", nOut, pVar->z);
71963#ifdef SQLITE_TRACE_SIZE_LIMIT
71964        if( nOut<pVar->n ){
71965          sqlite3XPrintf(&out, 0, "/*+%d bytes*/", pVar->n-nOut);
71966        }
71967#endif
71968#ifndef SQLITE_OMIT_UTF16
71969        if( enc!=SQLITE_UTF8 ) sqlite3VdbeMemRelease(&utf8);
71970#endif
71971      }else if( pVar->flags & MEM_Zero ){
71972        sqlite3XPrintf(&out, 0, "zeroblob(%d)", pVar->u.nZero);
71973      }else{
71974        int nOut;  /* Number of bytes of the blob to include in output */
71975        assert( pVar->flags & MEM_Blob );
71976        sqlite3StrAccumAppend(&out, "x'", 2);
71977        nOut = pVar->n;
71978#ifdef SQLITE_TRACE_SIZE_LIMIT
71979        if( nOut>SQLITE_TRACE_SIZE_LIMIT ) nOut = SQLITE_TRACE_SIZE_LIMIT;
71980#endif
71981        for(i=0; i<nOut; i++){
71982          sqlite3XPrintf(&out, 0, "%02x", pVar->z[i]&0xff);
71983        }
71984        sqlite3StrAccumAppend(&out, "'", 1);
71985#ifdef SQLITE_TRACE_SIZE_LIMIT
71986        if( nOut<pVar->n ){
71987          sqlite3XPrintf(&out, 0, "/*+%d bytes*/", pVar->n-nOut);
71988        }
71989#endif
71990      }
71991    }
71992  }
71993  return sqlite3StrAccumFinish(&out);
71994}
71995
71996#endif /* #ifndef SQLITE_OMIT_TRACE */
71997
71998/************** End of vdbetrace.c *******************************************/
71999/************** Begin file vdbe.c ********************************************/
72000/*
72001** 2001 September 15
72002**
72003** The author disclaims copyright to this source code.  In place of
72004** a legal notice, here is a blessing:
72005**
72006**    May you do good and not evil.
72007**    May you find forgiveness for yourself and forgive others.
72008**    May you share freely, never taking more than you give.
72009**
72010*************************************************************************
72011** The code in this file implements the function that runs the
72012** bytecode of a prepared statement.
72013**
72014** Various scripts scan this source file in order to generate HTML
72015** documentation, headers files, or other derived files.  The formatting
72016** of the code in this file is, therefore, important.  See other comments
72017** in this file for details.  If in doubt, do not deviate from existing
72018** commenting and indentation practices when changing or adding code.
72019*/
72020/* #include "sqliteInt.h" */
72021/* #include "vdbeInt.h" */
72022
72023/*
72024** Invoke this macro on memory cells just prior to changing the
72025** value of the cell.  This macro verifies that shallow copies are
72026** not misused.  A shallow copy of a string or blob just copies a
72027** pointer to the string or blob, not the content.  If the original
72028** is changed while the copy is still in use, the string or blob might
72029** be changed out from under the copy.  This macro verifies that nothing
72030** like that ever happens.
72031*/
72032#ifdef SQLITE_DEBUG
72033# define memAboutToChange(P,M) sqlite3VdbeMemAboutToChange(P,M)
72034#else
72035# define memAboutToChange(P,M)
72036#endif
72037
72038/*
72039** The following global variable is incremented every time a cursor
72040** moves, either by the OP_SeekXX, OP_Next, or OP_Prev opcodes.  The test
72041** procedures use this information to make sure that indices are
72042** working correctly.  This variable has no function other than to
72043** help verify the correct operation of the library.
72044*/
72045#ifdef SQLITE_TEST
72046SQLITE_API int sqlite3_search_count = 0;
72047#endif
72048
72049/*
72050** When this global variable is positive, it gets decremented once before
72051** each instruction in the VDBE.  When it reaches zero, the u1.isInterrupted
72052** field of the sqlite3 structure is set in order to simulate an interrupt.
72053**
72054** This facility is used for testing purposes only.  It does not function
72055** in an ordinary build.
72056*/
72057#ifdef SQLITE_TEST
72058SQLITE_API int sqlite3_interrupt_count = 0;
72059#endif
72060
72061/*
72062** The next global variable is incremented each type the OP_Sort opcode
72063** is executed.  The test procedures use this information to make sure that
72064** sorting is occurring or not occurring at appropriate times.   This variable
72065** has no function other than to help verify the correct operation of the
72066** library.
72067*/
72068#ifdef SQLITE_TEST
72069SQLITE_API int sqlite3_sort_count = 0;
72070#endif
72071
72072/*
72073** The next global variable records the size of the largest MEM_Blob
72074** or MEM_Str that has been used by a VDBE opcode.  The test procedures
72075** use this information to make sure that the zero-blob functionality
72076** is working correctly.   This variable has no function other than to
72077** help verify the correct operation of the library.
72078*/
72079#ifdef SQLITE_TEST
72080SQLITE_API int sqlite3_max_blobsize = 0;
72081static void updateMaxBlobsize(Mem *p){
72082  if( (p->flags & (MEM_Str|MEM_Blob))!=0 && p->n>sqlite3_max_blobsize ){
72083    sqlite3_max_blobsize = p->n;
72084  }
72085}
72086#endif
72087
72088/*
72089** The next global variable is incremented each time the OP_Found opcode
72090** is executed. This is used to test whether or not the foreign key
72091** operation implemented using OP_FkIsZero is working. This variable
72092** has no function other than to help verify the correct operation of the
72093** library.
72094*/
72095#ifdef SQLITE_TEST
72096SQLITE_API int sqlite3_found_count = 0;
72097#endif
72098
72099/*
72100** Test a register to see if it exceeds the current maximum blob size.
72101** If it does, record the new maximum blob size.
72102*/
72103#if defined(SQLITE_TEST) && !defined(SQLITE_OMIT_BUILTIN_TEST)
72104# define UPDATE_MAX_BLOBSIZE(P)  updateMaxBlobsize(P)
72105#else
72106# define UPDATE_MAX_BLOBSIZE(P)
72107#endif
72108
72109/*
72110** Invoke the VDBE coverage callback, if that callback is defined.  This
72111** feature is used for test suite validation only and does not appear an
72112** production builds.
72113**
72114** M is an integer, 2 or 3, that indices how many different ways the
72115** branch can go.  It is usually 2.  "I" is the direction the branch
72116** goes.  0 means falls through.  1 means branch is taken.  2 means the
72117** second alternative branch is taken.
72118**
72119** iSrcLine is the source code line (from the __LINE__ macro) that
72120** generated the VDBE instruction.  This instrumentation assumes that all
72121** source code is in a single file (the amalgamation).  Special values 1
72122** and 2 for the iSrcLine parameter mean that this particular branch is
72123** always taken or never taken, respectively.
72124*/
72125#if !defined(SQLITE_VDBE_COVERAGE)
72126# define VdbeBranchTaken(I,M)
72127#else
72128# define VdbeBranchTaken(I,M) vdbeTakeBranch(pOp->iSrcLine,I,M)
72129  static void vdbeTakeBranch(int iSrcLine, u8 I, u8 M){
72130    if( iSrcLine<=2 && ALWAYS(iSrcLine>0) ){
72131      M = iSrcLine;
72132      /* Assert the truth of VdbeCoverageAlwaysTaken() and
72133      ** VdbeCoverageNeverTaken() */
72134      assert( (M & I)==I );
72135    }else{
72136      if( sqlite3GlobalConfig.xVdbeBranch==0 ) return;  /*NO_TEST*/
72137      sqlite3GlobalConfig.xVdbeBranch(sqlite3GlobalConfig.pVdbeBranchArg,
72138                                      iSrcLine,I,M);
72139    }
72140  }
72141#endif
72142
72143/*
72144** Convert the given register into a string if it isn't one
72145** already. Return non-zero if a malloc() fails.
72146*/
72147#define Stringify(P, enc) \
72148   if(((P)->flags&(MEM_Str|MEM_Blob))==0 && sqlite3VdbeMemStringify(P,enc,0)) \
72149     { goto no_mem; }
72150
72151/*
72152** An ephemeral string value (signified by the MEM_Ephem flag) contains
72153** a pointer to a dynamically allocated string where some other entity
72154** is responsible for deallocating that string.  Because the register
72155** does not control the string, it might be deleted without the register
72156** knowing it.
72157**
72158** This routine converts an ephemeral string into a dynamically allocated
72159** string that the register itself controls.  In other words, it
72160** converts an MEM_Ephem string into a string with P.z==P.zMalloc.
72161*/
72162#define Deephemeralize(P) \
72163   if( ((P)->flags&MEM_Ephem)!=0 \
72164       && sqlite3VdbeMemMakeWriteable(P) ){ goto no_mem;}
72165
72166/* Return true if the cursor was opened using the OP_OpenSorter opcode. */
72167#define isSorter(x) ((x)->pSorter!=0)
72168
72169/*
72170** Allocate VdbeCursor number iCur.  Return a pointer to it.  Return NULL
72171** if we run out of memory.
72172*/
72173static VdbeCursor *allocateCursor(
72174  Vdbe *p,              /* The virtual machine */
72175  int iCur,             /* Index of the new VdbeCursor */
72176  int nField,           /* Number of fields in the table or index */
72177  int iDb,              /* Database the cursor belongs to, or -1 */
72178  int isBtreeCursor     /* True for B-Tree.  False for pseudo-table or vtab */
72179){
72180  /* Find the memory cell that will be used to store the blob of memory
72181  ** required for this VdbeCursor structure. It is convenient to use a
72182  ** vdbe memory cell to manage the memory allocation required for a
72183  ** VdbeCursor structure for the following reasons:
72184  **
72185  **   * Sometimes cursor numbers are used for a couple of different
72186  **     purposes in a vdbe program. The different uses might require
72187  **     different sized allocations. Memory cells provide growable
72188  **     allocations.
72189  **
72190  **   * When using ENABLE_MEMORY_MANAGEMENT, memory cell buffers can
72191  **     be freed lazily via the sqlite3_release_memory() API. This
72192  **     minimizes the number of malloc calls made by the system.
72193  **
72194  ** Memory cells for cursors are allocated at the top of the address
72195  ** space. Memory cell (p->nMem) corresponds to cursor 0. Space for
72196  ** cursor 1 is managed by memory cell (p->nMem-1), etc.
72197  */
72198  Mem *pMem = &p->aMem[p->nMem-iCur];
72199
72200  int nByte;
72201  VdbeCursor *pCx = 0;
72202  nByte =
72203      ROUND8(sizeof(VdbeCursor)) + 2*sizeof(u32)*nField +
72204      (isBtreeCursor?sqlite3BtreeCursorSize():0);
72205
72206  assert( iCur<p->nCursor );
72207  if( p->apCsr[iCur] ){
72208    sqlite3VdbeFreeCursor(p, p->apCsr[iCur]);
72209    p->apCsr[iCur] = 0;
72210  }
72211  if( SQLITE_OK==sqlite3VdbeMemClearAndResize(pMem, nByte) ){
72212    p->apCsr[iCur] = pCx = (VdbeCursor*)pMem->z;
72213    memset(pCx, 0, sizeof(VdbeCursor));
72214    pCx->iDb = iDb;
72215    pCx->nField = nField;
72216    pCx->aOffset = &pCx->aType[nField];
72217    if( isBtreeCursor ){
72218      pCx->pCursor = (BtCursor*)
72219          &pMem->z[ROUND8(sizeof(VdbeCursor))+2*sizeof(u32)*nField];
72220      sqlite3BtreeCursorZero(pCx->pCursor);
72221    }
72222  }
72223  return pCx;
72224}
72225
72226/*
72227** Try to convert a value into a numeric representation if we can
72228** do so without loss of information.  In other words, if the string
72229** looks like a number, convert it into a number.  If it does not
72230** look like a number, leave it alone.
72231**
72232** If the bTryForInt flag is true, then extra effort is made to give
72233** an integer representation.  Strings that look like floating point
72234** values but which have no fractional component (example: '48.00')
72235** will have a MEM_Int representation when bTryForInt is true.
72236**
72237** If bTryForInt is false, then if the input string contains a decimal
72238** point or exponential notation, the result is only MEM_Real, even
72239** if there is an exact integer representation of the quantity.
72240*/
72241static void applyNumericAffinity(Mem *pRec, int bTryForInt){
72242  double rValue;
72243  i64 iValue;
72244  u8 enc = pRec->enc;
72245  assert( (pRec->flags & (MEM_Str|MEM_Int|MEM_Real))==MEM_Str );
72246  if( sqlite3AtoF(pRec->z, &rValue, pRec->n, enc)==0 ) return;
72247  if( 0==sqlite3Atoi64(pRec->z, &iValue, pRec->n, enc) ){
72248    pRec->u.i = iValue;
72249    pRec->flags |= MEM_Int;
72250  }else{
72251    pRec->u.r = rValue;
72252    pRec->flags |= MEM_Real;
72253    if( bTryForInt ) sqlite3VdbeIntegerAffinity(pRec);
72254  }
72255}
72256
72257/*
72258** Processing is determine by the affinity parameter:
72259**
72260** SQLITE_AFF_INTEGER:
72261** SQLITE_AFF_REAL:
72262** SQLITE_AFF_NUMERIC:
72263**    Try to convert pRec to an integer representation or a
72264**    floating-point representation if an integer representation
72265**    is not possible.  Note that the integer representation is
72266**    always preferred, even if the affinity is REAL, because
72267**    an integer representation is more space efficient on disk.
72268**
72269** SQLITE_AFF_TEXT:
72270**    Convert pRec to a text representation.
72271**
72272** SQLITE_AFF_BLOB:
72273**    No-op.  pRec is unchanged.
72274*/
72275static void applyAffinity(
72276  Mem *pRec,          /* The value to apply affinity to */
72277  char affinity,      /* The affinity to be applied */
72278  u8 enc              /* Use this text encoding */
72279){
72280  if( affinity>=SQLITE_AFF_NUMERIC ){
72281    assert( affinity==SQLITE_AFF_INTEGER || affinity==SQLITE_AFF_REAL
72282             || affinity==SQLITE_AFF_NUMERIC );
72283    if( (pRec->flags & MEM_Int)==0 ){
72284      if( (pRec->flags & MEM_Real)==0 ){
72285        if( pRec->flags & MEM_Str ) applyNumericAffinity(pRec,1);
72286      }else{
72287        sqlite3VdbeIntegerAffinity(pRec);
72288      }
72289    }
72290  }else if( affinity==SQLITE_AFF_TEXT ){
72291    /* Only attempt the conversion to TEXT if there is an integer or real
72292    ** representation (blob and NULL do not get converted) but no string
72293    ** representation.
72294    */
72295    if( 0==(pRec->flags&MEM_Str) && (pRec->flags&(MEM_Real|MEM_Int)) ){
72296      sqlite3VdbeMemStringify(pRec, enc, 1);
72297    }
72298    pRec->flags &= ~(MEM_Real|MEM_Int);
72299  }
72300}
72301
72302/*
72303** Try to convert the type of a function argument or a result column
72304** into a numeric representation.  Use either INTEGER or REAL whichever
72305** is appropriate.  But only do the conversion if it is possible without
72306** loss of information and return the revised type of the argument.
72307*/
72308SQLITE_API int SQLITE_STDCALL sqlite3_value_numeric_type(sqlite3_value *pVal){
72309  int eType = sqlite3_value_type(pVal);
72310  if( eType==SQLITE_TEXT ){
72311    Mem *pMem = (Mem*)pVal;
72312    applyNumericAffinity(pMem, 0);
72313    eType = sqlite3_value_type(pVal);
72314  }
72315  return eType;
72316}
72317
72318/*
72319** Exported version of applyAffinity(). This one works on sqlite3_value*,
72320** not the internal Mem* type.
72321*/
72322SQLITE_PRIVATE void sqlite3ValueApplyAffinity(
72323  sqlite3_value *pVal,
72324  u8 affinity,
72325  u8 enc
72326){
72327  applyAffinity((Mem *)pVal, affinity, enc);
72328}
72329
72330/*
72331** pMem currently only holds a string type (or maybe a BLOB that we can
72332** interpret as a string if we want to).  Compute its corresponding
72333** numeric type, if has one.  Set the pMem->u.r and pMem->u.i fields
72334** accordingly.
72335*/
72336static u16 SQLITE_NOINLINE computeNumericType(Mem *pMem){
72337  assert( (pMem->flags & (MEM_Int|MEM_Real))==0 );
72338  assert( (pMem->flags & (MEM_Str|MEM_Blob))!=0 );
72339  if( sqlite3AtoF(pMem->z, &pMem->u.r, pMem->n, pMem->enc)==0 ){
72340    return 0;
72341  }
72342  if( sqlite3Atoi64(pMem->z, &pMem->u.i, pMem->n, pMem->enc)==SQLITE_OK ){
72343    return MEM_Int;
72344  }
72345  return MEM_Real;
72346}
72347
72348/*
72349** Return the numeric type for pMem, either MEM_Int or MEM_Real or both or
72350** none.
72351**
72352** Unlike applyNumericAffinity(), this routine does not modify pMem->flags.
72353** But it does set pMem->u.r and pMem->u.i appropriately.
72354*/
72355static u16 numericType(Mem *pMem){
72356  if( pMem->flags & (MEM_Int|MEM_Real) ){
72357    return pMem->flags & (MEM_Int|MEM_Real);
72358  }
72359  if( pMem->flags & (MEM_Str|MEM_Blob) ){
72360    return computeNumericType(pMem);
72361  }
72362  return 0;
72363}
72364
72365#ifdef SQLITE_DEBUG
72366/*
72367** Write a nice string representation of the contents of cell pMem
72368** into buffer zBuf, length nBuf.
72369*/
72370SQLITE_PRIVATE void sqlite3VdbeMemPrettyPrint(Mem *pMem, char *zBuf){
72371  char *zCsr = zBuf;
72372  int f = pMem->flags;
72373
72374  static const char *const encnames[] = {"(X)", "(8)", "(16LE)", "(16BE)"};
72375
72376  if( f&MEM_Blob ){
72377    int i;
72378    char c;
72379    if( f & MEM_Dyn ){
72380      c = 'z';
72381      assert( (f & (MEM_Static|MEM_Ephem))==0 );
72382    }else if( f & MEM_Static ){
72383      c = 't';
72384      assert( (f & (MEM_Dyn|MEM_Ephem))==0 );
72385    }else if( f & MEM_Ephem ){
72386      c = 'e';
72387      assert( (f & (MEM_Static|MEM_Dyn))==0 );
72388    }else{
72389      c = 's';
72390    }
72391
72392    sqlite3_snprintf(100, zCsr, "%c", c);
72393    zCsr += sqlite3Strlen30(zCsr);
72394    sqlite3_snprintf(100, zCsr, "%d[", pMem->n);
72395    zCsr += sqlite3Strlen30(zCsr);
72396    for(i=0; i<16 && i<pMem->n; i++){
72397      sqlite3_snprintf(100, zCsr, "%02X", ((int)pMem->z[i] & 0xFF));
72398      zCsr += sqlite3Strlen30(zCsr);
72399    }
72400    for(i=0; i<16 && i<pMem->n; i++){
72401      char z = pMem->z[i];
72402      if( z<32 || z>126 ) *zCsr++ = '.';
72403      else *zCsr++ = z;
72404    }
72405
72406    sqlite3_snprintf(100, zCsr, "]%s", encnames[pMem->enc]);
72407    zCsr += sqlite3Strlen30(zCsr);
72408    if( f & MEM_Zero ){
72409      sqlite3_snprintf(100, zCsr,"+%dz",pMem->u.nZero);
72410      zCsr += sqlite3Strlen30(zCsr);
72411    }
72412    *zCsr = '\0';
72413  }else if( f & MEM_Str ){
72414    int j, k;
72415    zBuf[0] = ' ';
72416    if( f & MEM_Dyn ){
72417      zBuf[1] = 'z';
72418      assert( (f & (MEM_Static|MEM_Ephem))==0 );
72419    }else if( f & MEM_Static ){
72420      zBuf[1] = 't';
72421      assert( (f & (MEM_Dyn|MEM_Ephem))==0 );
72422    }else if( f & MEM_Ephem ){
72423      zBuf[1] = 'e';
72424      assert( (f & (MEM_Static|MEM_Dyn))==0 );
72425    }else{
72426      zBuf[1] = 's';
72427    }
72428    k = 2;
72429    sqlite3_snprintf(100, &zBuf[k], "%d", pMem->n);
72430    k += sqlite3Strlen30(&zBuf[k]);
72431    zBuf[k++] = '[';
72432    for(j=0; j<15 && j<pMem->n; j++){
72433      u8 c = pMem->z[j];
72434      if( c>=0x20 && c<0x7f ){
72435        zBuf[k++] = c;
72436      }else{
72437        zBuf[k++] = '.';
72438      }
72439    }
72440    zBuf[k++] = ']';
72441    sqlite3_snprintf(100,&zBuf[k], encnames[pMem->enc]);
72442    k += sqlite3Strlen30(&zBuf[k]);
72443    zBuf[k++] = 0;
72444  }
72445}
72446#endif
72447
72448#ifdef SQLITE_DEBUG
72449/*
72450** Print the value of a register for tracing purposes:
72451*/
72452static void memTracePrint(Mem *p){
72453  if( p->flags & MEM_Undefined ){
72454    printf(" undefined");
72455  }else if( p->flags & MEM_Null ){
72456    printf(" NULL");
72457  }else if( (p->flags & (MEM_Int|MEM_Str))==(MEM_Int|MEM_Str) ){
72458    printf(" si:%lld", p->u.i);
72459  }else if( p->flags & MEM_Int ){
72460    printf(" i:%lld", p->u.i);
72461#ifndef SQLITE_OMIT_FLOATING_POINT
72462  }else if( p->flags & MEM_Real ){
72463    printf(" r:%g", p->u.r);
72464#endif
72465  }else if( p->flags & MEM_RowSet ){
72466    printf(" (rowset)");
72467  }else{
72468    char zBuf[200];
72469    sqlite3VdbeMemPrettyPrint(p, zBuf);
72470    printf(" %s", zBuf);
72471  }
72472}
72473static void registerTrace(int iReg, Mem *p){
72474  printf("REG[%d] = ", iReg);
72475  memTracePrint(p);
72476  printf("\n");
72477}
72478#endif
72479
72480#ifdef SQLITE_DEBUG
72481#  define REGISTER_TRACE(R,M) if(db->flags&SQLITE_VdbeTrace)registerTrace(R,M)
72482#else
72483#  define REGISTER_TRACE(R,M)
72484#endif
72485
72486
72487#ifdef VDBE_PROFILE
72488
72489/*
72490** hwtime.h contains inline assembler code for implementing
72491** high-performance timing routines.
72492*/
72493/************** Include hwtime.h in the middle of vdbe.c *********************/
72494/************** Begin file hwtime.h ******************************************/
72495/*
72496** 2008 May 27
72497**
72498** The author disclaims copyright to this source code.  In place of
72499** a legal notice, here is a blessing:
72500**
72501**    May you do good and not evil.
72502**    May you find forgiveness for yourself and forgive others.
72503**    May you share freely, never taking more than you give.
72504**
72505******************************************************************************
72506**
72507** This file contains inline asm code for retrieving "high-performance"
72508** counters for x86 class CPUs.
72509*/
72510#ifndef _HWTIME_H_
72511#define _HWTIME_H_
72512
72513/*
72514** The following routine only works on pentium-class (or newer) processors.
72515** It uses the RDTSC opcode to read the cycle count value out of the
72516** processor and returns that value.  This can be used for high-res
72517** profiling.
72518*/
72519#if (defined(__GNUC__) || defined(_MSC_VER)) && \
72520      (defined(i386) || defined(__i386__) || defined(_M_IX86))
72521
72522  #if defined(__GNUC__)
72523
72524  __inline__ sqlite_uint64 sqlite3Hwtime(void){
72525     unsigned int lo, hi;
72526     __asm__ __volatile__ ("rdtsc" : "=a" (lo), "=d" (hi));
72527     return (sqlite_uint64)hi << 32 | lo;
72528  }
72529
72530  #elif defined(_MSC_VER)
72531
72532  __declspec(naked) __inline sqlite_uint64 __cdecl sqlite3Hwtime(void){
72533     __asm {
72534        rdtsc
72535        ret       ; return value at EDX:EAX
72536     }
72537  }
72538
72539  #endif
72540
72541#elif (defined(__GNUC__) && defined(__x86_64__))
72542
72543  __inline__ sqlite_uint64 sqlite3Hwtime(void){
72544      unsigned long val;
72545      __asm__ __volatile__ ("rdtsc" : "=A" (val));
72546      return val;
72547  }
72548
72549#elif (defined(__GNUC__) && defined(__ppc__))
72550
72551  __inline__ sqlite_uint64 sqlite3Hwtime(void){
72552      unsigned long long retval;
72553      unsigned long junk;
72554      __asm__ __volatile__ ("\n\
72555          1:      mftbu   %1\n\
72556                  mftb    %L0\n\
72557                  mftbu   %0\n\
72558                  cmpw    %0,%1\n\
72559                  bne     1b"
72560                  : "=r" (retval), "=r" (junk));
72561      return retval;
72562  }
72563
72564#else
72565
72566  #error Need implementation of sqlite3Hwtime() for your platform.
72567
72568  /*
72569  ** To compile without implementing sqlite3Hwtime() for your platform,
72570  ** you can remove the above #error and use the following
72571  ** stub function.  You will lose timing support for many
72572  ** of the debugging and testing utilities, but it should at
72573  ** least compile and run.
72574  */
72575SQLITE_PRIVATE   sqlite_uint64 sqlite3Hwtime(void){ return ((sqlite_uint64)0); }
72576
72577#endif
72578
72579#endif /* !defined(_HWTIME_H_) */
72580
72581/************** End of hwtime.h **********************************************/
72582/************** Continuing where we left off in vdbe.c ***********************/
72583
72584#endif
72585
72586#ifndef NDEBUG
72587/*
72588** This function is only called from within an assert() expression. It
72589** checks that the sqlite3.nTransaction variable is correctly set to
72590** the number of non-transaction savepoints currently in the
72591** linked list starting at sqlite3.pSavepoint.
72592**
72593** Usage:
72594**
72595**     assert( checkSavepointCount(db) );
72596*/
72597static int checkSavepointCount(sqlite3 *db){
72598  int n = 0;
72599  Savepoint *p;
72600  for(p=db->pSavepoint; p; p=p->pNext) n++;
72601  assert( n==(db->nSavepoint + db->isTransactionSavepoint) );
72602  return 1;
72603}
72604#endif
72605
72606/*
72607** Return the register of pOp->p2 after first preparing it to be
72608** overwritten with an integer value.
72609*/
72610static Mem *out2Prerelease(Vdbe *p, VdbeOp *pOp){
72611  Mem *pOut;
72612  assert( pOp->p2>0 );
72613  assert( pOp->p2<=(p->nMem-p->nCursor) );
72614  pOut = &p->aMem[pOp->p2];
72615  memAboutToChange(p, pOut);
72616  if( VdbeMemDynamic(pOut) ) sqlite3VdbeMemSetNull(pOut);
72617  pOut->flags = MEM_Int;
72618  return pOut;
72619}
72620
72621
72622/*
72623** Execute as much of a VDBE program as we can.
72624** This is the core of sqlite3_step().
72625*/
72626SQLITE_PRIVATE int sqlite3VdbeExec(
72627  Vdbe *p                    /* The VDBE */
72628){
72629  Op *aOp = p->aOp;          /* Copy of p->aOp */
72630  Op *pOp = aOp;             /* Current operation */
72631#if defined(SQLITE_DEBUG) || defined(VDBE_PROFILE)
72632  Op *pOrigOp;               /* Value of pOp at the top of the loop */
72633#endif
72634  int rc = SQLITE_OK;        /* Value to return */
72635  sqlite3 *db = p->db;       /* The database */
72636  u8 resetSchemaOnFault = 0; /* Reset schema after an error if positive */
72637  u8 encoding = ENC(db);     /* The database encoding */
72638  int iCompare = 0;          /* Result of last OP_Compare operation */
72639  unsigned nVmStep = 0;      /* Number of virtual machine steps */
72640#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
72641  unsigned nProgressLimit = 0;/* Invoke xProgress() when nVmStep reaches this */
72642#endif
72643  Mem *aMem = p->aMem;       /* Copy of p->aMem */
72644  Mem *pIn1 = 0;             /* 1st input operand */
72645  Mem *pIn2 = 0;             /* 2nd input operand */
72646  Mem *pIn3 = 0;             /* 3rd input operand */
72647  Mem *pOut = 0;             /* Output operand */
72648  int *aPermute = 0;         /* Permutation of columns for OP_Compare */
72649  i64 lastRowid = db->lastRowid;  /* Saved value of the last insert ROWID */
72650#ifdef VDBE_PROFILE
72651  u64 start;                 /* CPU clock count at start of opcode */
72652#endif
72653  /*** INSERT STACK UNION HERE ***/
72654
72655  assert( p->magic==VDBE_MAGIC_RUN );  /* sqlite3_step() verifies this */
72656  sqlite3VdbeEnter(p);
72657  if( p->rc==SQLITE_NOMEM ){
72658    /* This happens if a malloc() inside a call to sqlite3_column_text() or
72659    ** sqlite3_column_text16() failed.  */
72660    goto no_mem;
72661  }
72662  assert( p->rc==SQLITE_OK || p->rc==SQLITE_BUSY );
72663  assert( p->bIsReader || p->readOnly!=0 );
72664  p->rc = SQLITE_OK;
72665  p->iCurrentTime = 0;
72666  assert( p->explain==0 );
72667  p->pResultSet = 0;
72668  db->busyHandler.nBusy = 0;
72669  if( db->u1.isInterrupted ) goto abort_due_to_interrupt;
72670  sqlite3VdbeIOTraceSql(p);
72671#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
72672  if( db->xProgress ){
72673    u32 iPrior = p->aCounter[SQLITE_STMTSTATUS_VM_STEP];
72674    assert( 0 < db->nProgressOps );
72675    nProgressLimit = db->nProgressOps - (iPrior % db->nProgressOps);
72676  }
72677#endif
72678#ifdef SQLITE_DEBUG
72679  sqlite3BeginBenignMalloc();
72680  if( p->pc==0
72681   && (p->db->flags & (SQLITE_VdbeListing|SQLITE_VdbeEQP|SQLITE_VdbeTrace))!=0
72682  ){
72683    int i;
72684    int once = 1;
72685    sqlite3VdbePrintSql(p);
72686    if( p->db->flags & SQLITE_VdbeListing ){
72687      printf("VDBE Program Listing:\n");
72688      for(i=0; i<p->nOp; i++){
72689        sqlite3VdbePrintOp(stdout, i, &aOp[i]);
72690      }
72691    }
72692    if( p->db->flags & SQLITE_VdbeEQP ){
72693      for(i=0; i<p->nOp; i++){
72694        if( aOp[i].opcode==OP_Explain ){
72695          if( once ) printf("VDBE Query Plan:\n");
72696          printf("%s\n", aOp[i].p4.z);
72697          once = 0;
72698        }
72699      }
72700    }
72701    if( p->db->flags & SQLITE_VdbeTrace )  printf("VDBE Trace:\n");
72702  }
72703  sqlite3EndBenignMalloc();
72704#endif
72705  for(pOp=&aOp[p->pc]; rc==SQLITE_OK; pOp++){
72706    assert( pOp>=aOp && pOp<&aOp[p->nOp]);
72707    if( db->mallocFailed ) goto no_mem;
72708#ifdef VDBE_PROFILE
72709    start = sqlite3Hwtime();
72710#endif
72711    nVmStep++;
72712#ifdef SQLITE_ENABLE_STMT_SCANSTATUS
72713    if( p->anExec ) p->anExec[(int)(pOp-aOp)]++;
72714#endif
72715
72716    /* Only allow tracing if SQLITE_DEBUG is defined.
72717    */
72718#ifdef SQLITE_DEBUG
72719    if( db->flags & SQLITE_VdbeTrace ){
72720      sqlite3VdbePrintOp(stdout, (int)(pOp - aOp), pOp);
72721    }
72722#endif
72723
72724
72725    /* Check to see if we need to simulate an interrupt.  This only happens
72726    ** if we have a special test build.
72727    */
72728#ifdef SQLITE_TEST
72729    if( sqlite3_interrupt_count>0 ){
72730      sqlite3_interrupt_count--;
72731      if( sqlite3_interrupt_count==0 ){
72732        sqlite3_interrupt(db);
72733      }
72734    }
72735#endif
72736
72737    /* Sanity checking on other operands */
72738#ifdef SQLITE_DEBUG
72739    assert( pOp->opflags==sqlite3OpcodeProperty[pOp->opcode] );
72740    if( (pOp->opflags & OPFLG_IN1)!=0 ){
72741      assert( pOp->p1>0 );
72742      assert( pOp->p1<=(p->nMem-p->nCursor) );
72743      assert( memIsValid(&aMem[pOp->p1]) );
72744      assert( sqlite3VdbeCheckMemInvariants(&aMem[pOp->p1]) );
72745      REGISTER_TRACE(pOp->p1, &aMem[pOp->p1]);
72746    }
72747    if( (pOp->opflags & OPFLG_IN2)!=0 ){
72748      assert( pOp->p2>0 );
72749      assert( pOp->p2<=(p->nMem-p->nCursor) );
72750      assert( memIsValid(&aMem[pOp->p2]) );
72751      assert( sqlite3VdbeCheckMemInvariants(&aMem[pOp->p2]) );
72752      REGISTER_TRACE(pOp->p2, &aMem[pOp->p2]);
72753    }
72754    if( (pOp->opflags & OPFLG_IN3)!=0 ){
72755      assert( pOp->p3>0 );
72756      assert( pOp->p3<=(p->nMem-p->nCursor) );
72757      assert( memIsValid(&aMem[pOp->p3]) );
72758      assert( sqlite3VdbeCheckMemInvariants(&aMem[pOp->p3]) );
72759      REGISTER_TRACE(pOp->p3, &aMem[pOp->p3]);
72760    }
72761    if( (pOp->opflags & OPFLG_OUT2)!=0 ){
72762      assert( pOp->p2>0 );
72763      assert( pOp->p2<=(p->nMem-p->nCursor) );
72764      memAboutToChange(p, &aMem[pOp->p2]);
72765    }
72766    if( (pOp->opflags & OPFLG_OUT3)!=0 ){
72767      assert( pOp->p3>0 );
72768      assert( pOp->p3<=(p->nMem-p->nCursor) );
72769      memAboutToChange(p, &aMem[pOp->p3]);
72770    }
72771#endif
72772#if defined(SQLITE_DEBUG) || defined(VDBE_PROFILE)
72773    pOrigOp = pOp;
72774#endif
72775
72776    switch( pOp->opcode ){
72777
72778/*****************************************************************************
72779** What follows is a massive switch statement where each case implements a
72780** separate instruction in the virtual machine.  If we follow the usual
72781** indentation conventions, each case should be indented by 6 spaces.  But
72782** that is a lot of wasted space on the left margin.  So the code within
72783** the switch statement will break with convention and be flush-left. Another
72784** big comment (similar to this one) will mark the point in the code where
72785** we transition back to normal indentation.
72786**
72787** The formatting of each case is important.  The makefile for SQLite
72788** generates two C files "opcodes.h" and "opcodes.c" by scanning this
72789** file looking for lines that begin with "case OP_".  The opcodes.h files
72790** will be filled with #defines that give unique integer values to each
72791** opcode and the opcodes.c file is filled with an array of strings where
72792** each string is the symbolic name for the corresponding opcode.  If the
72793** case statement is followed by a comment of the form "/# same as ... #/"
72794** that comment is used to determine the particular value of the opcode.
72795**
72796** Other keywords in the comment that follows each case are used to
72797** construct the OPFLG_INITIALIZER value that initializes opcodeProperty[].
72798** Keywords include: in1, in2, in3, out2, out3.  See
72799** the mkopcodeh.awk script for additional information.
72800**
72801** Documentation about VDBE opcodes is generated by scanning this file
72802** for lines of that contain "Opcode:".  That line and all subsequent
72803** comment lines are used in the generation of the opcode.html documentation
72804** file.
72805**
72806** SUMMARY:
72807**
72808**     Formatting is important to scripts that scan this file.
72809**     Do not deviate from the formatting style currently in use.
72810**
72811*****************************************************************************/
72812
72813/* Opcode:  Goto * P2 * * *
72814**
72815** An unconditional jump to address P2.
72816** The next instruction executed will be
72817** the one at index P2 from the beginning of
72818** the program.
72819**
72820** The P1 parameter is not actually used by this opcode.  However, it
72821** is sometimes set to 1 instead of 0 as a hint to the command-line shell
72822** that this Goto is the bottom of a loop and that the lines from P2 down
72823** to the current line should be indented for EXPLAIN output.
72824*/
72825case OP_Goto: {             /* jump */
72826jump_to_p2_and_check_for_interrupt:
72827  pOp = &aOp[pOp->p2 - 1];
72828
72829  /* Opcodes that are used as the bottom of a loop (OP_Next, OP_Prev,
72830  ** OP_VNext, OP_RowSetNext, or OP_SorterNext) all jump here upon
72831  ** completion.  Check to see if sqlite3_interrupt() has been called
72832  ** or if the progress callback needs to be invoked.
72833  **
72834  ** This code uses unstructured "goto" statements and does not look clean.
72835  ** But that is not due to sloppy coding habits. The code is written this
72836  ** way for performance, to avoid having to run the interrupt and progress
72837  ** checks on every opcode.  This helps sqlite3_step() to run about 1.5%
72838  ** faster according to "valgrind --tool=cachegrind" */
72839check_for_interrupt:
72840  if( db->u1.isInterrupted ) goto abort_due_to_interrupt;
72841#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
72842  /* Call the progress callback if it is configured and the required number
72843  ** of VDBE ops have been executed (either since this invocation of
72844  ** sqlite3VdbeExec() or since last time the progress callback was called).
72845  ** If the progress callback returns non-zero, exit the virtual machine with
72846  ** a return code SQLITE_ABORT.
72847  */
72848  if( db->xProgress!=0 && nVmStep>=nProgressLimit ){
72849    assert( db->nProgressOps!=0 );
72850    nProgressLimit = nVmStep + db->nProgressOps - (nVmStep%db->nProgressOps);
72851    if( db->xProgress(db->pProgressArg) ){
72852      rc = SQLITE_INTERRUPT;
72853      goto vdbe_error_halt;
72854    }
72855  }
72856#endif
72857
72858  break;
72859}
72860
72861/* Opcode:  Gosub P1 P2 * * *
72862**
72863** Write the current address onto register P1
72864** and then jump to address P2.
72865*/
72866case OP_Gosub: {            /* jump */
72867  assert( pOp->p1>0 && pOp->p1<=(p->nMem-p->nCursor) );
72868  pIn1 = &aMem[pOp->p1];
72869  assert( VdbeMemDynamic(pIn1)==0 );
72870  memAboutToChange(p, pIn1);
72871  pIn1->flags = MEM_Int;
72872  pIn1->u.i = (int)(pOp-aOp);
72873  REGISTER_TRACE(pOp->p1, pIn1);
72874
72875  /* Most jump operations do a goto to this spot in order to update
72876  ** the pOp pointer. */
72877jump_to_p2:
72878  pOp = &aOp[pOp->p2 - 1];
72879  break;
72880}
72881
72882/* Opcode:  Return P1 * * * *
72883**
72884** Jump to the next instruction after the address in register P1.  After
72885** the jump, register P1 becomes undefined.
72886*/
72887case OP_Return: {           /* in1 */
72888  pIn1 = &aMem[pOp->p1];
72889  assert( pIn1->flags==MEM_Int );
72890  pOp = &aOp[pIn1->u.i];
72891  pIn1->flags = MEM_Undefined;
72892  break;
72893}
72894
72895/* Opcode: InitCoroutine P1 P2 P3 * *
72896**
72897** Set up register P1 so that it will Yield to the coroutine
72898** located at address P3.
72899**
72900** If P2!=0 then the coroutine implementation immediately follows
72901** this opcode.  So jump over the coroutine implementation to
72902** address P2.
72903**
72904** See also: EndCoroutine
72905*/
72906case OP_InitCoroutine: {     /* jump */
72907  assert( pOp->p1>0 &&  pOp->p1<=(p->nMem-p->nCursor) );
72908  assert( pOp->p2>=0 && pOp->p2<p->nOp );
72909  assert( pOp->p3>=0 && pOp->p3<p->nOp );
72910  pOut = &aMem[pOp->p1];
72911  assert( !VdbeMemDynamic(pOut) );
72912  pOut->u.i = pOp->p3 - 1;
72913  pOut->flags = MEM_Int;
72914  if( pOp->p2 ) goto jump_to_p2;
72915  break;
72916}
72917
72918/* Opcode:  EndCoroutine P1 * * * *
72919**
72920** The instruction at the address in register P1 is a Yield.
72921** Jump to the P2 parameter of that Yield.
72922** After the jump, register P1 becomes undefined.
72923**
72924** See also: InitCoroutine
72925*/
72926case OP_EndCoroutine: {           /* in1 */
72927  VdbeOp *pCaller;
72928  pIn1 = &aMem[pOp->p1];
72929  assert( pIn1->flags==MEM_Int );
72930  assert( pIn1->u.i>=0 && pIn1->u.i<p->nOp );
72931  pCaller = &aOp[pIn1->u.i];
72932  assert( pCaller->opcode==OP_Yield );
72933  assert( pCaller->p2>=0 && pCaller->p2<p->nOp );
72934  pOp = &aOp[pCaller->p2 - 1];
72935  pIn1->flags = MEM_Undefined;
72936  break;
72937}
72938
72939/* Opcode:  Yield P1 P2 * * *
72940**
72941** Swap the program counter with the value in register P1.  This
72942** has the effect of yielding to a coroutine.
72943**
72944** If the coroutine that is launched by this instruction ends with
72945** Yield or Return then continue to the next instruction.  But if
72946** the coroutine launched by this instruction ends with
72947** EndCoroutine, then jump to P2 rather than continuing with the
72948** next instruction.
72949**
72950** See also: InitCoroutine
72951*/
72952case OP_Yield: {            /* in1, jump */
72953  int pcDest;
72954  pIn1 = &aMem[pOp->p1];
72955  assert( VdbeMemDynamic(pIn1)==0 );
72956  pIn1->flags = MEM_Int;
72957  pcDest = (int)pIn1->u.i;
72958  pIn1->u.i = (int)(pOp - aOp);
72959  REGISTER_TRACE(pOp->p1, pIn1);
72960  pOp = &aOp[pcDest];
72961  break;
72962}
72963
72964/* Opcode:  HaltIfNull  P1 P2 P3 P4 P5
72965** Synopsis:  if r[P3]=null halt
72966**
72967** Check the value in register P3.  If it is NULL then Halt using
72968** parameter P1, P2, and P4 as if this were a Halt instruction.  If the
72969** value in register P3 is not NULL, then this routine is a no-op.
72970** The P5 parameter should be 1.
72971*/
72972case OP_HaltIfNull: {      /* in3 */
72973  pIn3 = &aMem[pOp->p3];
72974  if( (pIn3->flags & MEM_Null)==0 ) break;
72975  /* Fall through into OP_Halt */
72976}
72977
72978/* Opcode:  Halt P1 P2 * P4 P5
72979**
72980** Exit immediately.  All open cursors, etc are closed
72981** automatically.
72982**
72983** P1 is the result code returned by sqlite3_exec(), sqlite3_reset(),
72984** or sqlite3_finalize().  For a normal halt, this should be SQLITE_OK (0).
72985** For errors, it can be some other value.  If P1!=0 then P2 will determine
72986** whether or not to rollback the current transaction.  Do not rollback
72987** if P2==OE_Fail. Do the rollback if P2==OE_Rollback.  If P2==OE_Abort,
72988** then back out all changes that have occurred during this execution of the
72989** VDBE, but do not rollback the transaction.
72990**
72991** If P4 is not null then it is an error message string.
72992**
72993** P5 is a value between 0 and 4, inclusive, that modifies the P4 string.
72994**
72995**    0:  (no change)
72996**    1:  NOT NULL contraint failed: P4
72997**    2:  UNIQUE constraint failed: P4
72998**    3:  CHECK constraint failed: P4
72999**    4:  FOREIGN KEY constraint failed: P4
73000**
73001** If P5 is not zero and P4 is NULL, then everything after the ":" is
73002** omitted.
73003**
73004** There is an implied "Halt 0 0 0" instruction inserted at the very end of
73005** every program.  So a jump past the last instruction of the program
73006** is the same as executing Halt.
73007*/
73008case OP_Halt: {
73009  const char *zType;
73010  const char *zLogFmt;
73011  VdbeFrame *pFrame;
73012  int pcx;
73013
73014  pcx = (int)(pOp - aOp);
73015  if( pOp->p1==SQLITE_OK && p->pFrame ){
73016    /* Halt the sub-program. Return control to the parent frame. */
73017    pFrame = p->pFrame;
73018    p->pFrame = pFrame->pParent;
73019    p->nFrame--;
73020    sqlite3VdbeSetChanges(db, p->nChange);
73021    pcx = sqlite3VdbeFrameRestore(pFrame);
73022    lastRowid = db->lastRowid;
73023    if( pOp->p2==OE_Ignore ){
73024      /* Instruction pcx is the OP_Program that invoked the sub-program
73025      ** currently being halted. If the p2 instruction of this OP_Halt
73026      ** instruction is set to OE_Ignore, then the sub-program is throwing
73027      ** an IGNORE exception. In this case jump to the address specified
73028      ** as the p2 of the calling OP_Program.  */
73029      pcx = p->aOp[pcx].p2-1;
73030    }
73031    aOp = p->aOp;
73032    aMem = p->aMem;
73033    pOp = &aOp[pcx];
73034    break;
73035  }
73036  p->rc = pOp->p1;
73037  p->errorAction = (u8)pOp->p2;
73038  p->pc = pcx;
73039  if( p->rc ){
73040    if( pOp->p5 ){
73041      static const char * const azType[] = { "NOT NULL", "UNIQUE", "CHECK",
73042                                             "FOREIGN KEY" };
73043      assert( pOp->p5>=1 && pOp->p5<=4 );
73044      testcase( pOp->p5==1 );
73045      testcase( pOp->p5==2 );
73046      testcase( pOp->p5==3 );
73047      testcase( pOp->p5==4 );
73048      zType = azType[pOp->p5-1];
73049    }else{
73050      zType = 0;
73051    }
73052    assert( zType!=0 || pOp->p4.z!=0 );
73053    zLogFmt = "abort at %d in [%s]: %s";
73054    if( zType && pOp->p4.z ){
73055      sqlite3VdbeError(p, "%s constraint failed: %s", zType, pOp->p4.z);
73056    }else if( pOp->p4.z ){
73057      sqlite3VdbeError(p, "%s", pOp->p4.z);
73058    }else{
73059      sqlite3VdbeError(p, "%s constraint failed", zType);
73060    }
73061    sqlite3_log(pOp->p1, zLogFmt, pcx, p->zSql, p->zErrMsg);
73062  }
73063  rc = sqlite3VdbeHalt(p);
73064  assert( rc==SQLITE_BUSY || rc==SQLITE_OK || rc==SQLITE_ERROR );
73065  if( rc==SQLITE_BUSY ){
73066    p->rc = rc = SQLITE_BUSY;
73067  }else{
73068    assert( rc==SQLITE_OK || (p->rc&0xff)==SQLITE_CONSTRAINT );
73069    assert( rc==SQLITE_OK || db->nDeferredCons>0 || db->nDeferredImmCons>0 );
73070    rc = p->rc ? SQLITE_ERROR : SQLITE_DONE;
73071  }
73072  goto vdbe_return;
73073}
73074
73075/* Opcode: Integer P1 P2 * * *
73076** Synopsis: r[P2]=P1
73077**
73078** The 32-bit integer value P1 is written into register P2.
73079*/
73080case OP_Integer: {         /* out2 */
73081  pOut = out2Prerelease(p, pOp);
73082  pOut->u.i = pOp->p1;
73083  break;
73084}
73085
73086/* Opcode: Int64 * P2 * P4 *
73087** Synopsis: r[P2]=P4
73088**
73089** P4 is a pointer to a 64-bit integer value.
73090** Write that value into register P2.
73091*/
73092case OP_Int64: {           /* out2 */
73093  pOut = out2Prerelease(p, pOp);
73094  assert( pOp->p4.pI64!=0 );
73095  pOut->u.i = *pOp->p4.pI64;
73096  break;
73097}
73098
73099#ifndef SQLITE_OMIT_FLOATING_POINT
73100/* Opcode: Real * P2 * P4 *
73101** Synopsis: r[P2]=P4
73102**
73103** P4 is a pointer to a 64-bit floating point value.
73104** Write that value into register P2.
73105*/
73106case OP_Real: {            /* same as TK_FLOAT, out2 */
73107  pOut = out2Prerelease(p, pOp);
73108  pOut->flags = MEM_Real;
73109  assert( !sqlite3IsNaN(*pOp->p4.pReal) );
73110  pOut->u.r = *pOp->p4.pReal;
73111  break;
73112}
73113#endif
73114
73115/* Opcode: String8 * P2 * P4 *
73116** Synopsis: r[P2]='P4'
73117**
73118** P4 points to a nul terminated UTF-8 string. This opcode is transformed
73119** into a String opcode before it is executed for the first time.  During
73120** this transformation, the length of string P4 is computed and stored
73121** as the P1 parameter.
73122*/
73123case OP_String8: {         /* same as TK_STRING, out2 */
73124  assert( pOp->p4.z!=0 );
73125  pOut = out2Prerelease(p, pOp);
73126  pOp->opcode = OP_String;
73127  pOp->p1 = sqlite3Strlen30(pOp->p4.z);
73128
73129#ifndef SQLITE_OMIT_UTF16
73130  if( encoding!=SQLITE_UTF8 ){
73131    rc = sqlite3VdbeMemSetStr(pOut, pOp->p4.z, -1, SQLITE_UTF8, SQLITE_STATIC);
73132    if( rc==SQLITE_TOOBIG ) goto too_big;
73133    if( SQLITE_OK!=sqlite3VdbeChangeEncoding(pOut, encoding) ) goto no_mem;
73134    assert( pOut->szMalloc>0 && pOut->zMalloc==pOut->z );
73135    assert( VdbeMemDynamic(pOut)==0 );
73136    pOut->szMalloc = 0;
73137    pOut->flags |= MEM_Static;
73138    if( pOp->p4type==P4_DYNAMIC ){
73139      sqlite3DbFree(db, pOp->p4.z);
73140    }
73141    pOp->p4type = P4_DYNAMIC;
73142    pOp->p4.z = pOut->z;
73143    pOp->p1 = pOut->n;
73144  }
73145#endif
73146  if( pOp->p1>db->aLimit[SQLITE_LIMIT_LENGTH] ){
73147    goto too_big;
73148  }
73149  /* Fall through to the next case, OP_String */
73150}
73151
73152/* Opcode: String P1 P2 P3 P4 P5
73153** Synopsis: r[P2]='P4' (len=P1)
73154**
73155** The string value P4 of length P1 (bytes) is stored in register P2.
73156**
73157** If P5!=0 and the content of register P3 is greater than zero, then
73158** the datatype of the register P2 is converted to BLOB.  The content is
73159** the same sequence of bytes, it is merely interpreted as a BLOB instead
73160** of a string, as if it had been CAST.
73161*/
73162case OP_String: {          /* out2 */
73163  assert( pOp->p4.z!=0 );
73164  pOut = out2Prerelease(p, pOp);
73165  pOut->flags = MEM_Str|MEM_Static|MEM_Term;
73166  pOut->z = pOp->p4.z;
73167  pOut->n = pOp->p1;
73168  pOut->enc = encoding;
73169  UPDATE_MAX_BLOBSIZE(pOut);
73170  if( pOp->p5 ){
73171    assert( pOp->p3>0 );
73172    assert( pOp->p3<=(p->nMem-p->nCursor) );
73173    pIn3 = &aMem[pOp->p3];
73174    assert( pIn3->flags & MEM_Int );
73175    if( pIn3->u.i ) pOut->flags = MEM_Blob|MEM_Static|MEM_Term;
73176  }
73177  break;
73178}
73179
73180/* Opcode: Null P1 P2 P3 * *
73181** Synopsis:  r[P2..P3]=NULL
73182**
73183** Write a NULL into registers P2.  If P3 greater than P2, then also write
73184** NULL into register P3 and every register in between P2 and P3.  If P3
73185** is less than P2 (typically P3 is zero) then only register P2 is
73186** set to NULL.
73187**
73188** If the P1 value is non-zero, then also set the MEM_Cleared flag so that
73189** NULL values will not compare equal even if SQLITE_NULLEQ is set on
73190** OP_Ne or OP_Eq.
73191*/
73192case OP_Null: {           /* out2 */
73193  int cnt;
73194  u16 nullFlag;
73195  pOut = out2Prerelease(p, pOp);
73196  cnt = pOp->p3-pOp->p2;
73197  assert( pOp->p3<=(p->nMem-p->nCursor) );
73198  pOut->flags = nullFlag = pOp->p1 ? (MEM_Null|MEM_Cleared) : MEM_Null;
73199  while( cnt>0 ){
73200    pOut++;
73201    memAboutToChange(p, pOut);
73202    sqlite3VdbeMemSetNull(pOut);
73203    pOut->flags = nullFlag;
73204    cnt--;
73205  }
73206  break;
73207}
73208
73209/* Opcode: SoftNull P1 * * * *
73210** Synopsis:  r[P1]=NULL
73211**
73212** Set register P1 to have the value NULL as seen by the OP_MakeRecord
73213** instruction, but do not free any string or blob memory associated with
73214** the register, so that if the value was a string or blob that was
73215** previously copied using OP_SCopy, the copies will continue to be valid.
73216*/
73217case OP_SoftNull: {
73218  assert( pOp->p1>0 && pOp->p1<=(p->nMem-p->nCursor) );
73219  pOut = &aMem[pOp->p1];
73220  pOut->flags = (pOut->flags|MEM_Null)&~MEM_Undefined;
73221  break;
73222}
73223
73224/* Opcode: Blob P1 P2 * P4 *
73225** Synopsis: r[P2]=P4 (len=P1)
73226**
73227** P4 points to a blob of data P1 bytes long.  Store this
73228** blob in register P2.
73229*/
73230case OP_Blob: {                /* out2 */
73231  assert( pOp->p1 <= SQLITE_MAX_LENGTH );
73232  pOut = out2Prerelease(p, pOp);
73233  sqlite3VdbeMemSetStr(pOut, pOp->p4.z, pOp->p1, 0, 0);
73234  pOut->enc = encoding;
73235  UPDATE_MAX_BLOBSIZE(pOut);
73236  break;
73237}
73238
73239/* Opcode: Variable P1 P2 * P4 *
73240** Synopsis: r[P2]=parameter(P1,P4)
73241**
73242** Transfer the values of bound parameter P1 into register P2
73243**
73244** If the parameter is named, then its name appears in P4.
73245** The P4 value is used by sqlite3_bind_parameter_name().
73246*/
73247case OP_Variable: {            /* out2 */
73248  Mem *pVar;       /* Value being transferred */
73249
73250  assert( pOp->p1>0 && pOp->p1<=p->nVar );
73251  assert( pOp->p4.z==0 || pOp->p4.z==p->azVar[pOp->p1-1] );
73252  pVar = &p->aVar[pOp->p1 - 1];
73253  if( sqlite3VdbeMemTooBig(pVar) ){
73254    goto too_big;
73255  }
73256  pOut = out2Prerelease(p, pOp);
73257  sqlite3VdbeMemShallowCopy(pOut, pVar, MEM_Static);
73258  UPDATE_MAX_BLOBSIZE(pOut);
73259  break;
73260}
73261
73262/* Opcode: Move P1 P2 P3 * *
73263** Synopsis:  r[P2@P3]=r[P1@P3]
73264**
73265** Move the P3 values in register P1..P1+P3-1 over into
73266** registers P2..P2+P3-1.  Registers P1..P1+P3-1 are
73267** left holding a NULL.  It is an error for register ranges
73268** P1..P1+P3-1 and P2..P2+P3-1 to overlap.  It is an error
73269** for P3 to be less than 1.
73270*/
73271case OP_Move: {
73272  int n;           /* Number of registers left to copy */
73273  int p1;          /* Register to copy from */
73274  int p2;          /* Register to copy to */
73275
73276  n = pOp->p3;
73277  p1 = pOp->p1;
73278  p2 = pOp->p2;
73279  assert( n>0 && p1>0 && p2>0 );
73280  assert( p1+n<=p2 || p2+n<=p1 );
73281
73282  pIn1 = &aMem[p1];
73283  pOut = &aMem[p2];
73284  do{
73285    assert( pOut<=&aMem[(p->nMem-p->nCursor)] );
73286    assert( pIn1<=&aMem[(p->nMem-p->nCursor)] );
73287    assert( memIsValid(pIn1) );
73288    memAboutToChange(p, pOut);
73289    sqlite3VdbeMemMove(pOut, pIn1);
73290#ifdef SQLITE_DEBUG
73291    if( pOut->pScopyFrom>=&aMem[p1] && pOut->pScopyFrom<pOut ){
73292      pOut->pScopyFrom += pOp->p2 - p1;
73293    }
73294#endif
73295    Deephemeralize(pOut);
73296    REGISTER_TRACE(p2++, pOut);
73297    pIn1++;
73298    pOut++;
73299  }while( --n );
73300  break;
73301}
73302
73303/* Opcode: Copy P1 P2 P3 * *
73304** Synopsis: r[P2@P3+1]=r[P1@P3+1]
73305**
73306** Make a copy of registers P1..P1+P3 into registers P2..P2+P3.
73307**
73308** This instruction makes a deep copy of the value.  A duplicate
73309** is made of any string or blob constant.  See also OP_SCopy.
73310*/
73311case OP_Copy: {
73312  int n;
73313
73314  n = pOp->p3;
73315  pIn1 = &aMem[pOp->p1];
73316  pOut = &aMem[pOp->p2];
73317  assert( pOut!=pIn1 );
73318  while( 1 ){
73319    sqlite3VdbeMemShallowCopy(pOut, pIn1, MEM_Ephem);
73320    Deephemeralize(pOut);
73321#ifdef SQLITE_DEBUG
73322    pOut->pScopyFrom = 0;
73323#endif
73324    REGISTER_TRACE(pOp->p2+pOp->p3-n, pOut);
73325    if( (n--)==0 ) break;
73326    pOut++;
73327    pIn1++;
73328  }
73329  break;
73330}
73331
73332/* Opcode: SCopy P1 P2 * * *
73333** Synopsis: r[P2]=r[P1]
73334**
73335** Make a shallow copy of register P1 into register P2.
73336**
73337** This instruction makes a shallow copy of the value.  If the value
73338** is a string or blob, then the copy is only a pointer to the
73339** original and hence if the original changes so will the copy.
73340** Worse, if the original is deallocated, the copy becomes invalid.
73341** Thus the program must guarantee that the original will not change
73342** during the lifetime of the copy.  Use OP_Copy to make a complete
73343** copy.
73344*/
73345case OP_SCopy: {            /* out2 */
73346  pIn1 = &aMem[pOp->p1];
73347  pOut = &aMem[pOp->p2];
73348  assert( pOut!=pIn1 );
73349  sqlite3VdbeMemShallowCopy(pOut, pIn1, MEM_Ephem);
73350#ifdef SQLITE_DEBUG
73351  if( pOut->pScopyFrom==0 ) pOut->pScopyFrom = pIn1;
73352#endif
73353  break;
73354}
73355
73356/* Opcode: ResultRow P1 P2 * * *
73357** Synopsis:  output=r[P1@P2]
73358**
73359** The registers P1 through P1+P2-1 contain a single row of
73360** results. This opcode causes the sqlite3_step() call to terminate
73361** with an SQLITE_ROW return code and it sets up the sqlite3_stmt
73362** structure to provide access to the r(P1)..r(P1+P2-1) values as
73363** the result row.
73364*/
73365case OP_ResultRow: {
73366  Mem *pMem;
73367  int i;
73368  assert( p->nResColumn==pOp->p2 );
73369  assert( pOp->p1>0 );
73370  assert( pOp->p1+pOp->p2<=(p->nMem-p->nCursor)+1 );
73371
73372#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
73373  /* Run the progress counter just before returning.
73374  */
73375  if( db->xProgress!=0
73376   && nVmStep>=nProgressLimit
73377   && db->xProgress(db->pProgressArg)!=0
73378  ){
73379    rc = SQLITE_INTERRUPT;
73380    goto vdbe_error_halt;
73381  }
73382#endif
73383
73384  /* If this statement has violated immediate foreign key constraints, do
73385  ** not return the number of rows modified. And do not RELEASE the statement
73386  ** transaction. It needs to be rolled back.  */
73387  if( SQLITE_OK!=(rc = sqlite3VdbeCheckFk(p, 0)) ){
73388    assert( db->flags&SQLITE_CountRows );
73389    assert( p->usesStmtJournal );
73390    break;
73391  }
73392
73393  /* If the SQLITE_CountRows flag is set in sqlite3.flags mask, then
73394  ** DML statements invoke this opcode to return the number of rows
73395  ** modified to the user. This is the only way that a VM that
73396  ** opens a statement transaction may invoke this opcode.
73397  **
73398  ** In case this is such a statement, close any statement transaction
73399  ** opened by this VM before returning control to the user. This is to
73400  ** ensure that statement-transactions are always nested, not overlapping.
73401  ** If the open statement-transaction is not closed here, then the user
73402  ** may step another VM that opens its own statement transaction. This
73403  ** may lead to overlapping statement transactions.
73404  **
73405  ** The statement transaction is never a top-level transaction.  Hence
73406  ** the RELEASE call below can never fail.
73407  */
73408  assert( p->iStatement==0 || db->flags&SQLITE_CountRows );
73409  rc = sqlite3VdbeCloseStatement(p, SAVEPOINT_RELEASE);
73410  if( NEVER(rc!=SQLITE_OK) ){
73411    break;
73412  }
73413
73414  /* Invalidate all ephemeral cursor row caches */
73415  p->cacheCtr = (p->cacheCtr + 2)|1;
73416
73417  /* Make sure the results of the current row are \000 terminated
73418  ** and have an assigned type.  The results are de-ephemeralized as
73419  ** a side effect.
73420  */
73421  pMem = p->pResultSet = &aMem[pOp->p1];
73422  for(i=0; i<pOp->p2; i++){
73423    assert( memIsValid(&pMem[i]) );
73424    Deephemeralize(&pMem[i]);
73425    assert( (pMem[i].flags & MEM_Ephem)==0
73426            || (pMem[i].flags & (MEM_Str|MEM_Blob))==0 );
73427    sqlite3VdbeMemNulTerminate(&pMem[i]);
73428    REGISTER_TRACE(pOp->p1+i, &pMem[i]);
73429  }
73430  if( db->mallocFailed ) goto no_mem;
73431
73432  /* Return SQLITE_ROW
73433  */
73434  p->pc = (int)(pOp - aOp) + 1;
73435  rc = SQLITE_ROW;
73436  goto vdbe_return;
73437}
73438
73439/* Opcode: Concat P1 P2 P3 * *
73440** Synopsis: r[P3]=r[P2]+r[P1]
73441**
73442** Add the text in register P1 onto the end of the text in
73443** register P2 and store the result in register P3.
73444** If either the P1 or P2 text are NULL then store NULL in P3.
73445**
73446**   P3 = P2 || P1
73447**
73448** It is illegal for P1 and P3 to be the same register. Sometimes,
73449** if P3 is the same register as P2, the implementation is able
73450** to avoid a memcpy().
73451*/
73452case OP_Concat: {           /* same as TK_CONCAT, in1, in2, out3 */
73453  i64 nByte;
73454
73455  pIn1 = &aMem[pOp->p1];
73456  pIn2 = &aMem[pOp->p2];
73457  pOut = &aMem[pOp->p3];
73458  assert( pIn1!=pOut );
73459  if( (pIn1->flags | pIn2->flags) & MEM_Null ){
73460    sqlite3VdbeMemSetNull(pOut);
73461    break;
73462  }
73463  if( ExpandBlob(pIn1) || ExpandBlob(pIn2) ) goto no_mem;
73464  Stringify(pIn1, encoding);
73465  Stringify(pIn2, encoding);
73466  nByte = pIn1->n + pIn2->n;
73467  if( nByte>db->aLimit[SQLITE_LIMIT_LENGTH] ){
73468    goto too_big;
73469  }
73470  if( sqlite3VdbeMemGrow(pOut, (int)nByte+2, pOut==pIn2) ){
73471    goto no_mem;
73472  }
73473  MemSetTypeFlag(pOut, MEM_Str);
73474  if( pOut!=pIn2 ){
73475    memcpy(pOut->z, pIn2->z, pIn2->n);
73476  }
73477  memcpy(&pOut->z[pIn2->n], pIn1->z, pIn1->n);
73478  pOut->z[nByte]=0;
73479  pOut->z[nByte+1] = 0;
73480  pOut->flags |= MEM_Term;
73481  pOut->n = (int)nByte;
73482  pOut->enc = encoding;
73483  UPDATE_MAX_BLOBSIZE(pOut);
73484  break;
73485}
73486
73487/* Opcode: Add P1 P2 P3 * *
73488** Synopsis:  r[P3]=r[P1]+r[P2]
73489**
73490** Add the value in register P1 to the value in register P2
73491** and store the result in register P3.
73492** If either input is NULL, the result is NULL.
73493*/
73494/* Opcode: Multiply P1 P2 P3 * *
73495** Synopsis:  r[P3]=r[P1]*r[P2]
73496**
73497**
73498** Multiply the value in register P1 by the value in register P2
73499** and store the result in register P3.
73500** If either input is NULL, the result is NULL.
73501*/
73502/* Opcode: Subtract P1 P2 P3 * *
73503** Synopsis:  r[P3]=r[P2]-r[P1]
73504**
73505** Subtract the value in register P1 from the value in register P2
73506** and store the result in register P3.
73507** If either input is NULL, the result is NULL.
73508*/
73509/* Opcode: Divide P1 P2 P3 * *
73510** Synopsis:  r[P3]=r[P2]/r[P1]
73511**
73512** Divide the value in register P1 by the value in register P2
73513** and store the result in register P3 (P3=P2/P1). If the value in
73514** register P1 is zero, then the result is NULL. If either input is
73515** NULL, the result is NULL.
73516*/
73517/* Opcode: Remainder P1 P2 P3 * *
73518** Synopsis:  r[P3]=r[P2]%r[P1]
73519**
73520** Compute the remainder after integer register P2 is divided by
73521** register P1 and store the result in register P3.
73522** If the value in register P1 is zero the result is NULL.
73523** If either operand is NULL, the result is NULL.
73524*/
73525case OP_Add:                   /* same as TK_PLUS, in1, in2, out3 */
73526case OP_Subtract:              /* same as TK_MINUS, in1, in2, out3 */
73527case OP_Multiply:              /* same as TK_STAR, in1, in2, out3 */
73528case OP_Divide:                /* same as TK_SLASH, in1, in2, out3 */
73529case OP_Remainder: {           /* same as TK_REM, in1, in2, out3 */
73530  char bIntint;   /* Started out as two integer operands */
73531  u16 flags;      /* Combined MEM_* flags from both inputs */
73532  u16 type1;      /* Numeric type of left operand */
73533  u16 type2;      /* Numeric type of right operand */
73534  i64 iA;         /* Integer value of left operand */
73535  i64 iB;         /* Integer value of right operand */
73536  double rA;      /* Real value of left operand */
73537  double rB;      /* Real value of right operand */
73538
73539  pIn1 = &aMem[pOp->p1];
73540  type1 = numericType(pIn1);
73541  pIn2 = &aMem[pOp->p2];
73542  type2 = numericType(pIn2);
73543  pOut = &aMem[pOp->p3];
73544  flags = pIn1->flags | pIn2->flags;
73545  if( (flags & MEM_Null)!=0 ) goto arithmetic_result_is_null;
73546  if( (type1 & type2 & MEM_Int)!=0 ){
73547    iA = pIn1->u.i;
73548    iB = pIn2->u.i;
73549    bIntint = 1;
73550    switch( pOp->opcode ){
73551      case OP_Add:       if( sqlite3AddInt64(&iB,iA) ) goto fp_math;  break;
73552      case OP_Subtract:  if( sqlite3SubInt64(&iB,iA) ) goto fp_math;  break;
73553      case OP_Multiply:  if( sqlite3MulInt64(&iB,iA) ) goto fp_math;  break;
73554      case OP_Divide: {
73555        if( iA==0 ) goto arithmetic_result_is_null;
73556        if( iA==-1 && iB==SMALLEST_INT64 ) goto fp_math;
73557        iB /= iA;
73558        break;
73559      }
73560      default: {
73561        if( iA==0 ) goto arithmetic_result_is_null;
73562        if( iA==-1 ) iA = 1;
73563        iB %= iA;
73564        break;
73565      }
73566    }
73567    pOut->u.i = iB;
73568    MemSetTypeFlag(pOut, MEM_Int);
73569  }else{
73570    bIntint = 0;
73571fp_math:
73572    rA = sqlite3VdbeRealValue(pIn1);
73573    rB = sqlite3VdbeRealValue(pIn2);
73574    switch( pOp->opcode ){
73575      case OP_Add:         rB += rA;       break;
73576      case OP_Subtract:    rB -= rA;       break;
73577      case OP_Multiply:    rB *= rA;       break;
73578      case OP_Divide: {
73579        /* (double)0 In case of SQLITE_OMIT_FLOATING_POINT... */
73580        if( rA==(double)0 ) goto arithmetic_result_is_null;
73581        rB /= rA;
73582        break;
73583      }
73584      default: {
73585        iA = (i64)rA;
73586        iB = (i64)rB;
73587        if( iA==0 ) goto arithmetic_result_is_null;
73588        if( iA==-1 ) iA = 1;
73589        rB = (double)(iB % iA);
73590        break;
73591      }
73592    }
73593#ifdef SQLITE_OMIT_FLOATING_POINT
73594    pOut->u.i = rB;
73595    MemSetTypeFlag(pOut, MEM_Int);
73596#else
73597    if( sqlite3IsNaN(rB) ){
73598      goto arithmetic_result_is_null;
73599    }
73600    pOut->u.r = rB;
73601    MemSetTypeFlag(pOut, MEM_Real);
73602    if( ((type1|type2)&MEM_Real)==0 && !bIntint ){
73603      sqlite3VdbeIntegerAffinity(pOut);
73604    }
73605#endif
73606  }
73607  break;
73608
73609arithmetic_result_is_null:
73610  sqlite3VdbeMemSetNull(pOut);
73611  break;
73612}
73613
73614/* Opcode: CollSeq P1 * * P4
73615**
73616** P4 is a pointer to a CollSeq struct. If the next call to a user function
73617** or aggregate calls sqlite3GetFuncCollSeq(), this collation sequence will
73618** be returned. This is used by the built-in min(), max() and nullif()
73619** functions.
73620**
73621** If P1 is not zero, then it is a register that a subsequent min() or
73622** max() aggregate will set to 1 if the current row is not the minimum or
73623** maximum.  The P1 register is initialized to 0 by this instruction.
73624**
73625** The interface used by the implementation of the aforementioned functions
73626** to retrieve the collation sequence set by this opcode is not available
73627** publicly.  Only built-in functions have access to this feature.
73628*/
73629case OP_CollSeq: {
73630  assert( pOp->p4type==P4_COLLSEQ );
73631  if( pOp->p1 ){
73632    sqlite3VdbeMemSetInt64(&aMem[pOp->p1], 0);
73633  }
73634  break;
73635}
73636
73637/* Opcode: Function0 P1 P2 P3 P4 P5
73638** Synopsis: r[P3]=func(r[P2@P5])
73639**
73640** Invoke a user function (P4 is a pointer to a FuncDef object that
73641** defines the function) with P5 arguments taken from register P2 and
73642** successors.  The result of the function is stored in register P3.
73643** Register P3 must not be one of the function inputs.
73644**
73645** P1 is a 32-bit bitmask indicating whether or not each argument to the
73646** function was determined to be constant at compile time. If the first
73647** argument was constant then bit 0 of P1 is set. This is used to determine
73648** whether meta data associated with a user function argument using the
73649** sqlite3_set_auxdata() API may be safely retained until the next
73650** invocation of this opcode.
73651**
73652** See also: Function, AggStep, AggFinal
73653*/
73654/* Opcode: Function P1 P2 P3 P4 P5
73655** Synopsis: r[P3]=func(r[P2@P5])
73656**
73657** Invoke a user function (P4 is a pointer to an sqlite3_context object that
73658** contains a pointer to the function to be run) with P5 arguments taken
73659** from register P2 and successors.  The result of the function is stored
73660** in register P3.  Register P3 must not be one of the function inputs.
73661**
73662** P1 is a 32-bit bitmask indicating whether or not each argument to the
73663** function was determined to be constant at compile time. If the first
73664** argument was constant then bit 0 of P1 is set. This is used to determine
73665** whether meta data associated with a user function argument using the
73666** sqlite3_set_auxdata() API may be safely retained until the next
73667** invocation of this opcode.
73668**
73669** SQL functions are initially coded as OP_Function0 with P4 pointing
73670** to a FuncDef object.  But on first evaluation, the P4 operand is
73671** automatically converted into an sqlite3_context object and the operation
73672** changed to this OP_Function opcode.  In this way, the initialization of
73673** the sqlite3_context object occurs only once, rather than once for each
73674** evaluation of the function.
73675**
73676** See also: Function0, AggStep, AggFinal
73677*/
73678case OP_Function0: {
73679  int n;
73680  sqlite3_context *pCtx;
73681
73682  assert( pOp->p4type==P4_FUNCDEF );
73683  n = pOp->p5;
73684  assert( pOp->p3>0 && pOp->p3<=(p->nMem-p->nCursor) );
73685  assert( n==0 || (pOp->p2>0 && pOp->p2+n<=(p->nMem-p->nCursor)+1) );
73686  assert( pOp->p3<pOp->p2 || pOp->p3>=pOp->p2+n );
73687  pCtx = sqlite3DbMallocRaw(db, sizeof(*pCtx) + (n-1)*sizeof(sqlite3_value*));
73688  if( pCtx==0 ) goto no_mem;
73689  pCtx->pOut = 0;
73690  pCtx->pFunc = pOp->p4.pFunc;
73691  pCtx->iOp = (int)(pOp - aOp);
73692  pCtx->pVdbe = p;
73693  pCtx->argc = n;
73694  pOp->p4type = P4_FUNCCTX;
73695  pOp->p4.pCtx = pCtx;
73696  pOp->opcode = OP_Function;
73697  /* Fall through into OP_Function */
73698}
73699case OP_Function: {
73700  int i;
73701  sqlite3_context *pCtx;
73702
73703  assert( pOp->p4type==P4_FUNCCTX );
73704  pCtx = pOp->p4.pCtx;
73705
73706  /* If this function is inside of a trigger, the register array in aMem[]
73707  ** might change from one evaluation to the next.  The next block of code
73708  ** checks to see if the register array has changed, and if so it
73709  ** reinitializes the relavant parts of the sqlite3_context object */
73710  pOut = &aMem[pOp->p3];
73711  if( pCtx->pOut != pOut ){
73712    pCtx->pOut = pOut;
73713    for(i=pCtx->argc-1; i>=0; i--) pCtx->argv[i] = &aMem[pOp->p2+i];
73714  }
73715
73716  memAboutToChange(p, pCtx->pOut);
73717#ifdef SQLITE_DEBUG
73718  for(i=0; i<pCtx->argc; i++){
73719    assert( memIsValid(pCtx->argv[i]) );
73720    REGISTER_TRACE(pOp->p2+i, pCtx->argv[i]);
73721  }
73722#endif
73723  MemSetTypeFlag(pCtx->pOut, MEM_Null);
73724  pCtx->fErrorOrAux = 0;
73725  db->lastRowid = lastRowid;
73726  (*pCtx->pFunc->xFunc)(pCtx, pCtx->argc, pCtx->argv); /* IMP: R-24505-23230 */
73727  lastRowid = db->lastRowid;  /* Remember rowid changes made by xFunc */
73728
73729  /* If the function returned an error, throw an exception */
73730  if( pCtx->fErrorOrAux ){
73731    if( pCtx->isError ){
73732      sqlite3VdbeError(p, "%s", sqlite3_value_text(pCtx->pOut));
73733      rc = pCtx->isError;
73734    }
73735    sqlite3VdbeDeleteAuxData(p, pCtx->iOp, pOp->p1);
73736  }
73737
73738  /* Copy the result of the function into register P3 */
73739  if( pOut->flags & (MEM_Str|MEM_Blob) ){
73740    sqlite3VdbeChangeEncoding(pCtx->pOut, encoding);
73741    if( sqlite3VdbeMemTooBig(pCtx->pOut) ) goto too_big;
73742  }
73743
73744  REGISTER_TRACE(pOp->p3, pCtx->pOut);
73745  UPDATE_MAX_BLOBSIZE(pCtx->pOut);
73746  break;
73747}
73748
73749/* Opcode: BitAnd P1 P2 P3 * *
73750** Synopsis:  r[P3]=r[P1]&r[P2]
73751**
73752** Take the bit-wise AND of the values in register P1 and P2 and
73753** store the result in register P3.
73754** If either input is NULL, the result is NULL.
73755*/
73756/* Opcode: BitOr P1 P2 P3 * *
73757** Synopsis:  r[P3]=r[P1]|r[P2]
73758**
73759** Take the bit-wise OR of the values in register P1 and P2 and
73760** store the result in register P3.
73761** If either input is NULL, the result is NULL.
73762*/
73763/* Opcode: ShiftLeft P1 P2 P3 * *
73764** Synopsis:  r[P3]=r[P2]<<r[P1]
73765**
73766** Shift the integer value in register P2 to the left by the
73767** number of bits specified by the integer in register P1.
73768** Store the result in register P3.
73769** If either input is NULL, the result is NULL.
73770*/
73771/* Opcode: ShiftRight P1 P2 P3 * *
73772** Synopsis:  r[P3]=r[P2]>>r[P1]
73773**
73774** Shift the integer value in register P2 to the right by the
73775** number of bits specified by the integer in register P1.
73776** Store the result in register P3.
73777** If either input is NULL, the result is NULL.
73778*/
73779case OP_BitAnd:                 /* same as TK_BITAND, in1, in2, out3 */
73780case OP_BitOr:                  /* same as TK_BITOR, in1, in2, out3 */
73781case OP_ShiftLeft:              /* same as TK_LSHIFT, in1, in2, out3 */
73782case OP_ShiftRight: {           /* same as TK_RSHIFT, in1, in2, out3 */
73783  i64 iA;
73784  u64 uA;
73785  i64 iB;
73786  u8 op;
73787
73788  pIn1 = &aMem[pOp->p1];
73789  pIn2 = &aMem[pOp->p2];
73790  pOut = &aMem[pOp->p3];
73791  if( (pIn1->flags | pIn2->flags) & MEM_Null ){
73792    sqlite3VdbeMemSetNull(pOut);
73793    break;
73794  }
73795  iA = sqlite3VdbeIntValue(pIn2);
73796  iB = sqlite3VdbeIntValue(pIn1);
73797  op = pOp->opcode;
73798  if( op==OP_BitAnd ){
73799    iA &= iB;
73800  }else if( op==OP_BitOr ){
73801    iA |= iB;
73802  }else if( iB!=0 ){
73803    assert( op==OP_ShiftRight || op==OP_ShiftLeft );
73804
73805    /* If shifting by a negative amount, shift in the other direction */
73806    if( iB<0 ){
73807      assert( OP_ShiftRight==OP_ShiftLeft+1 );
73808      op = 2*OP_ShiftLeft + 1 - op;
73809      iB = iB>(-64) ? -iB : 64;
73810    }
73811
73812    if( iB>=64 ){
73813      iA = (iA>=0 || op==OP_ShiftLeft) ? 0 : -1;
73814    }else{
73815      memcpy(&uA, &iA, sizeof(uA));
73816      if( op==OP_ShiftLeft ){
73817        uA <<= iB;
73818      }else{
73819        uA >>= iB;
73820        /* Sign-extend on a right shift of a negative number */
73821        if( iA<0 ) uA |= ((((u64)0xffffffff)<<32)|0xffffffff) << (64-iB);
73822      }
73823      memcpy(&iA, &uA, sizeof(iA));
73824    }
73825  }
73826  pOut->u.i = iA;
73827  MemSetTypeFlag(pOut, MEM_Int);
73828  break;
73829}
73830
73831/* Opcode: AddImm  P1 P2 * * *
73832** Synopsis:  r[P1]=r[P1]+P2
73833**
73834** Add the constant P2 to the value in register P1.
73835** The result is always an integer.
73836**
73837** To force any register to be an integer, just add 0.
73838*/
73839case OP_AddImm: {            /* in1 */
73840  pIn1 = &aMem[pOp->p1];
73841  memAboutToChange(p, pIn1);
73842  sqlite3VdbeMemIntegerify(pIn1);
73843  pIn1->u.i += pOp->p2;
73844  break;
73845}
73846
73847/* Opcode: MustBeInt P1 P2 * * *
73848**
73849** Force the value in register P1 to be an integer.  If the value
73850** in P1 is not an integer and cannot be converted into an integer
73851** without data loss, then jump immediately to P2, or if P2==0
73852** raise an SQLITE_MISMATCH exception.
73853*/
73854case OP_MustBeInt: {            /* jump, in1 */
73855  pIn1 = &aMem[pOp->p1];
73856  if( (pIn1->flags & MEM_Int)==0 ){
73857    applyAffinity(pIn1, SQLITE_AFF_NUMERIC, encoding);
73858    VdbeBranchTaken((pIn1->flags&MEM_Int)==0, 2);
73859    if( (pIn1->flags & MEM_Int)==0 ){
73860      if( pOp->p2==0 ){
73861        rc = SQLITE_MISMATCH;
73862        goto abort_due_to_error;
73863      }else{
73864        goto jump_to_p2;
73865      }
73866    }
73867  }
73868  MemSetTypeFlag(pIn1, MEM_Int);
73869  break;
73870}
73871
73872#ifndef SQLITE_OMIT_FLOATING_POINT
73873/* Opcode: RealAffinity P1 * * * *
73874**
73875** If register P1 holds an integer convert it to a real value.
73876**
73877** This opcode is used when extracting information from a column that
73878** has REAL affinity.  Such column values may still be stored as
73879** integers, for space efficiency, but after extraction we want them
73880** to have only a real value.
73881*/
73882case OP_RealAffinity: {                  /* in1 */
73883  pIn1 = &aMem[pOp->p1];
73884  if( pIn1->flags & MEM_Int ){
73885    sqlite3VdbeMemRealify(pIn1);
73886  }
73887  break;
73888}
73889#endif
73890
73891#ifndef SQLITE_OMIT_CAST
73892/* Opcode: Cast P1 P2 * * *
73893** Synopsis: affinity(r[P1])
73894**
73895** Force the value in register P1 to be the type defined by P2.
73896**
73897** <ul>
73898** <li value="97"> TEXT
73899** <li value="98"> BLOB
73900** <li value="99"> NUMERIC
73901** <li value="100"> INTEGER
73902** <li value="101"> REAL
73903** </ul>
73904**
73905** A NULL value is not changed by this routine.  It remains NULL.
73906*/
73907case OP_Cast: {                  /* in1 */
73908  assert( pOp->p2>=SQLITE_AFF_BLOB && pOp->p2<=SQLITE_AFF_REAL );
73909  testcase( pOp->p2==SQLITE_AFF_TEXT );
73910  testcase( pOp->p2==SQLITE_AFF_BLOB );
73911  testcase( pOp->p2==SQLITE_AFF_NUMERIC );
73912  testcase( pOp->p2==SQLITE_AFF_INTEGER );
73913  testcase( pOp->p2==SQLITE_AFF_REAL );
73914  pIn1 = &aMem[pOp->p1];
73915  memAboutToChange(p, pIn1);
73916  rc = ExpandBlob(pIn1);
73917  sqlite3VdbeMemCast(pIn1, pOp->p2, encoding);
73918  UPDATE_MAX_BLOBSIZE(pIn1);
73919  break;
73920}
73921#endif /* SQLITE_OMIT_CAST */
73922
73923/* Opcode: Lt P1 P2 P3 P4 P5
73924** Synopsis: if r[P1]<r[P3] goto P2
73925**
73926** Compare the values in register P1 and P3.  If reg(P3)<reg(P1) then
73927** jump to address P2.
73928**
73929** If the SQLITE_JUMPIFNULL bit of P5 is set and either reg(P1) or
73930** reg(P3) is NULL then take the jump.  If the SQLITE_JUMPIFNULL
73931** bit is clear then fall through if either operand is NULL.
73932**
73933** The SQLITE_AFF_MASK portion of P5 must be an affinity character -
73934** SQLITE_AFF_TEXT, SQLITE_AFF_INTEGER, and so forth. An attempt is made
73935** to coerce both inputs according to this affinity before the
73936** comparison is made. If the SQLITE_AFF_MASK is 0x00, then numeric
73937** affinity is used. Note that the affinity conversions are stored
73938** back into the input registers P1 and P3.  So this opcode can cause
73939** persistent changes to registers P1 and P3.
73940**
73941** Once any conversions have taken place, and neither value is NULL,
73942** the values are compared. If both values are blobs then memcmp() is
73943** used to determine the results of the comparison.  If both values
73944** are text, then the appropriate collating function specified in
73945** P4 is  used to do the comparison.  If P4 is not specified then
73946** memcmp() is used to compare text string.  If both values are
73947** numeric, then a numeric comparison is used. If the two values
73948** are of different types, then numbers are considered less than
73949** strings and strings are considered less than blobs.
73950**
73951** If the SQLITE_STOREP2 bit of P5 is set, then do not jump.  Instead,
73952** store a boolean result (either 0, or 1, or NULL) in register P2.
73953**
73954** If the SQLITE_NULLEQ bit is set in P5, then NULL values are considered
73955** equal to one another, provided that they do not have their MEM_Cleared
73956** bit set.
73957*/
73958/* Opcode: Ne P1 P2 P3 P4 P5
73959** Synopsis: if r[P1]!=r[P3] goto P2
73960**
73961** This works just like the Lt opcode except that the jump is taken if
73962** the operands in registers P1 and P3 are not equal.  See the Lt opcode for
73963** additional information.
73964**
73965** If SQLITE_NULLEQ is set in P5 then the result of comparison is always either
73966** true or false and is never NULL.  If both operands are NULL then the result
73967** of comparison is false.  If either operand is NULL then the result is true.
73968** If neither operand is NULL the result is the same as it would be if
73969** the SQLITE_NULLEQ flag were omitted from P5.
73970*/
73971/* Opcode: Eq P1 P2 P3 P4 P5
73972** Synopsis: if r[P1]==r[P3] goto P2
73973**
73974** This works just like the Lt opcode except that the jump is taken if
73975** the operands in registers P1 and P3 are equal.
73976** See the Lt opcode for additional information.
73977**
73978** If SQLITE_NULLEQ is set in P5 then the result of comparison is always either
73979** true or false and is never NULL.  If both operands are NULL then the result
73980** of comparison is true.  If either operand is NULL then the result is false.
73981** If neither operand is NULL the result is the same as it would be if
73982** the SQLITE_NULLEQ flag were omitted from P5.
73983*/
73984/* Opcode: Le P1 P2 P3 P4 P5
73985** Synopsis: if r[P1]<=r[P3] goto P2
73986**
73987** This works just like the Lt opcode except that the jump is taken if
73988** the content of register P3 is less than or equal to the content of
73989** register P1.  See the Lt opcode for additional information.
73990*/
73991/* Opcode: Gt P1 P2 P3 P4 P5
73992** Synopsis: if r[P1]>r[P3] goto P2
73993**
73994** This works just like the Lt opcode except that the jump is taken if
73995** the content of register P3 is greater than the content of
73996** register P1.  See the Lt opcode for additional information.
73997*/
73998/* Opcode: Ge P1 P2 P3 P4 P5
73999** Synopsis: if r[P1]>=r[P3] goto P2
74000**
74001** This works just like the Lt opcode except that the jump is taken if
74002** the content of register P3 is greater than or equal to the content of
74003** register P1.  See the Lt opcode for additional information.
74004*/
74005case OP_Eq:               /* same as TK_EQ, jump, in1, in3 */
74006case OP_Ne:               /* same as TK_NE, jump, in1, in3 */
74007case OP_Lt:               /* same as TK_LT, jump, in1, in3 */
74008case OP_Le:               /* same as TK_LE, jump, in1, in3 */
74009case OP_Gt:               /* same as TK_GT, jump, in1, in3 */
74010case OP_Ge: {             /* same as TK_GE, jump, in1, in3 */
74011  int res;            /* Result of the comparison of pIn1 against pIn3 */
74012  char affinity;      /* Affinity to use for comparison */
74013  u16 flags1;         /* Copy of initial value of pIn1->flags */
74014  u16 flags3;         /* Copy of initial value of pIn3->flags */
74015
74016  pIn1 = &aMem[pOp->p1];
74017  pIn3 = &aMem[pOp->p3];
74018  flags1 = pIn1->flags;
74019  flags3 = pIn3->flags;
74020  if( (flags1 | flags3)&MEM_Null ){
74021    /* One or both operands are NULL */
74022    if( pOp->p5 & SQLITE_NULLEQ ){
74023      /* If SQLITE_NULLEQ is set (which will only happen if the operator is
74024      ** OP_Eq or OP_Ne) then take the jump or not depending on whether
74025      ** or not both operands are null.
74026      */
74027      assert( pOp->opcode==OP_Eq || pOp->opcode==OP_Ne );
74028      assert( (flags1 & MEM_Cleared)==0 );
74029      assert( (pOp->p5 & SQLITE_JUMPIFNULL)==0 );
74030      if( (flags1&MEM_Null)!=0
74031       && (flags3&MEM_Null)!=0
74032       && (flags3&MEM_Cleared)==0
74033      ){
74034        res = 0;  /* Results are equal */
74035      }else{
74036        res = 1;  /* Results are not equal */
74037      }
74038    }else{
74039      /* SQLITE_NULLEQ is clear and at least one operand is NULL,
74040      ** then the result is always NULL.
74041      ** The jump is taken if the SQLITE_JUMPIFNULL bit is set.
74042      */
74043      if( pOp->p5 & SQLITE_STOREP2 ){
74044        pOut = &aMem[pOp->p2];
74045        MemSetTypeFlag(pOut, MEM_Null);
74046        REGISTER_TRACE(pOp->p2, pOut);
74047      }else{
74048        VdbeBranchTaken(2,3);
74049        if( pOp->p5 & SQLITE_JUMPIFNULL ){
74050          goto jump_to_p2;
74051        }
74052      }
74053      break;
74054    }
74055  }else{
74056    /* Neither operand is NULL.  Do a comparison. */
74057    affinity = pOp->p5 & SQLITE_AFF_MASK;
74058    if( affinity>=SQLITE_AFF_NUMERIC ){
74059      if( (pIn1->flags & (MEM_Int|MEM_Real|MEM_Str))==MEM_Str ){
74060        applyNumericAffinity(pIn1,0);
74061      }
74062      if( (pIn3->flags & (MEM_Int|MEM_Real|MEM_Str))==MEM_Str ){
74063        applyNumericAffinity(pIn3,0);
74064      }
74065    }else if( affinity==SQLITE_AFF_TEXT ){
74066      if( (pIn1->flags & MEM_Str)==0 && (pIn1->flags & (MEM_Int|MEM_Real))!=0 ){
74067        testcase( pIn1->flags & MEM_Int );
74068        testcase( pIn1->flags & MEM_Real );
74069        sqlite3VdbeMemStringify(pIn1, encoding, 1);
74070        testcase( (flags1&MEM_Dyn) != (pIn1->flags&MEM_Dyn) );
74071        flags1 = (pIn1->flags & ~MEM_TypeMask) | (flags1 & MEM_TypeMask);
74072      }
74073      if( (pIn3->flags & MEM_Str)==0 && (pIn3->flags & (MEM_Int|MEM_Real))!=0 ){
74074        testcase( pIn3->flags & MEM_Int );
74075        testcase( pIn3->flags & MEM_Real );
74076        sqlite3VdbeMemStringify(pIn3, encoding, 1);
74077        testcase( (flags3&MEM_Dyn) != (pIn3->flags&MEM_Dyn) );
74078        flags3 = (pIn3->flags & ~MEM_TypeMask) | (flags3 & MEM_TypeMask);
74079      }
74080    }
74081    assert( pOp->p4type==P4_COLLSEQ || pOp->p4.pColl==0 );
74082    if( pIn1->flags & MEM_Zero ){
74083      sqlite3VdbeMemExpandBlob(pIn1);
74084      flags1 &= ~MEM_Zero;
74085    }
74086    if( pIn3->flags & MEM_Zero ){
74087      sqlite3VdbeMemExpandBlob(pIn3);
74088      flags3 &= ~MEM_Zero;
74089    }
74090    if( db->mallocFailed ) goto no_mem;
74091    res = sqlite3MemCompare(pIn3, pIn1, pOp->p4.pColl);
74092  }
74093  switch( pOp->opcode ){
74094    case OP_Eq:    res = res==0;     break;
74095    case OP_Ne:    res = res!=0;     break;
74096    case OP_Lt:    res = res<0;      break;
74097    case OP_Le:    res = res<=0;     break;
74098    case OP_Gt:    res = res>0;      break;
74099    default:       res = res>=0;     break;
74100  }
74101
74102  /* Undo any changes made by applyAffinity() to the input registers. */
74103  assert( (pIn1->flags & MEM_Dyn) == (flags1 & MEM_Dyn) );
74104  pIn1->flags = flags1;
74105  assert( (pIn3->flags & MEM_Dyn) == (flags3 & MEM_Dyn) );
74106  pIn3->flags = flags3;
74107
74108  if( pOp->p5 & SQLITE_STOREP2 ){
74109    pOut = &aMem[pOp->p2];
74110    memAboutToChange(p, pOut);
74111    MemSetTypeFlag(pOut, MEM_Int);
74112    pOut->u.i = res;
74113    REGISTER_TRACE(pOp->p2, pOut);
74114  }else{
74115    VdbeBranchTaken(res!=0, (pOp->p5 & SQLITE_NULLEQ)?2:3);
74116    if( res ){
74117      goto jump_to_p2;
74118    }
74119  }
74120  break;
74121}
74122
74123/* Opcode: Permutation * * * P4 *
74124**
74125** Set the permutation used by the OP_Compare operator to be the array
74126** of integers in P4.
74127**
74128** The permutation is only valid until the next OP_Compare that has
74129** the OPFLAG_PERMUTE bit set in P5. Typically the OP_Permutation should
74130** occur immediately prior to the OP_Compare.
74131*/
74132case OP_Permutation: {
74133  assert( pOp->p4type==P4_INTARRAY );
74134  assert( pOp->p4.ai );
74135  aPermute = pOp->p4.ai;
74136  break;
74137}
74138
74139/* Opcode: Compare P1 P2 P3 P4 P5
74140** Synopsis: r[P1@P3] <-> r[P2@P3]
74141**
74142** Compare two vectors of registers in reg(P1)..reg(P1+P3-1) (call this
74143** vector "A") and in reg(P2)..reg(P2+P3-1) ("B").  Save the result of
74144** the comparison for use by the next OP_Jump instruct.
74145**
74146** If P5 has the OPFLAG_PERMUTE bit set, then the order of comparison is
74147** determined by the most recent OP_Permutation operator.  If the
74148** OPFLAG_PERMUTE bit is clear, then register are compared in sequential
74149** order.
74150**
74151** P4 is a KeyInfo structure that defines collating sequences and sort
74152** orders for the comparison.  The permutation applies to registers
74153** only.  The KeyInfo elements are used sequentially.
74154**
74155** The comparison is a sort comparison, so NULLs compare equal,
74156** NULLs are less than numbers, numbers are less than strings,
74157** and strings are less than blobs.
74158*/
74159case OP_Compare: {
74160  int n;
74161  int i;
74162  int p1;
74163  int p2;
74164  const KeyInfo *pKeyInfo;
74165  int idx;
74166  CollSeq *pColl;    /* Collating sequence to use on this term */
74167  int bRev;          /* True for DESCENDING sort order */
74168
74169  if( (pOp->p5 & OPFLAG_PERMUTE)==0 ) aPermute = 0;
74170  n = pOp->p3;
74171  pKeyInfo = pOp->p4.pKeyInfo;
74172  assert( n>0 );
74173  assert( pKeyInfo!=0 );
74174  p1 = pOp->p1;
74175  p2 = pOp->p2;
74176#if SQLITE_DEBUG
74177  if( aPermute ){
74178    int k, mx = 0;
74179    for(k=0; k<n; k++) if( aPermute[k]>mx ) mx = aPermute[k];
74180    assert( p1>0 && p1+mx<=(p->nMem-p->nCursor)+1 );
74181    assert( p2>0 && p2+mx<=(p->nMem-p->nCursor)+1 );
74182  }else{
74183    assert( p1>0 && p1+n<=(p->nMem-p->nCursor)+1 );
74184    assert( p2>0 && p2+n<=(p->nMem-p->nCursor)+1 );
74185  }
74186#endif /* SQLITE_DEBUG */
74187  for(i=0; i<n; i++){
74188    idx = aPermute ? aPermute[i] : i;
74189    assert( memIsValid(&aMem[p1+idx]) );
74190    assert( memIsValid(&aMem[p2+idx]) );
74191    REGISTER_TRACE(p1+idx, &aMem[p1+idx]);
74192    REGISTER_TRACE(p2+idx, &aMem[p2+idx]);
74193    assert( i<pKeyInfo->nField );
74194    pColl = pKeyInfo->aColl[i];
74195    bRev = pKeyInfo->aSortOrder[i];
74196    iCompare = sqlite3MemCompare(&aMem[p1+idx], &aMem[p2+idx], pColl);
74197    if( iCompare ){
74198      if( bRev ) iCompare = -iCompare;
74199      break;
74200    }
74201  }
74202  aPermute = 0;
74203  break;
74204}
74205
74206/* Opcode: Jump P1 P2 P3 * *
74207**
74208** Jump to the instruction at address P1, P2, or P3 depending on whether
74209** in the most recent OP_Compare instruction the P1 vector was less than
74210** equal to, or greater than the P2 vector, respectively.
74211*/
74212case OP_Jump: {             /* jump */
74213  if( iCompare<0 ){
74214    VdbeBranchTaken(0,3); pOp = &aOp[pOp->p1 - 1];
74215  }else if( iCompare==0 ){
74216    VdbeBranchTaken(1,3); pOp = &aOp[pOp->p2 - 1];
74217  }else{
74218    VdbeBranchTaken(2,3); pOp = &aOp[pOp->p3 - 1];
74219  }
74220  break;
74221}
74222
74223/* Opcode: And P1 P2 P3 * *
74224** Synopsis: r[P3]=(r[P1] && r[P2])
74225**
74226** Take the logical AND of the values in registers P1 and P2 and
74227** write the result into register P3.
74228**
74229** If either P1 or P2 is 0 (false) then the result is 0 even if
74230** the other input is NULL.  A NULL and true or two NULLs give
74231** a NULL output.
74232*/
74233/* Opcode: Or P1 P2 P3 * *
74234** Synopsis: r[P3]=(r[P1] || r[P2])
74235**
74236** Take the logical OR of the values in register P1 and P2 and
74237** store the answer in register P3.
74238**
74239** If either P1 or P2 is nonzero (true) then the result is 1 (true)
74240** even if the other input is NULL.  A NULL and false or two NULLs
74241** give a NULL output.
74242*/
74243case OP_And:              /* same as TK_AND, in1, in2, out3 */
74244case OP_Or: {             /* same as TK_OR, in1, in2, out3 */
74245  int v1;    /* Left operand:  0==FALSE, 1==TRUE, 2==UNKNOWN or NULL */
74246  int v2;    /* Right operand: 0==FALSE, 1==TRUE, 2==UNKNOWN or NULL */
74247
74248  pIn1 = &aMem[pOp->p1];
74249  if( pIn1->flags & MEM_Null ){
74250    v1 = 2;
74251  }else{
74252    v1 = sqlite3VdbeIntValue(pIn1)!=0;
74253  }
74254  pIn2 = &aMem[pOp->p2];
74255  if( pIn2->flags & MEM_Null ){
74256    v2 = 2;
74257  }else{
74258    v2 = sqlite3VdbeIntValue(pIn2)!=0;
74259  }
74260  if( pOp->opcode==OP_And ){
74261    static const unsigned char and_logic[] = { 0, 0, 0, 0, 1, 2, 0, 2, 2 };
74262    v1 = and_logic[v1*3+v2];
74263  }else{
74264    static const unsigned char or_logic[] = { 0, 1, 2, 1, 1, 1, 2, 1, 2 };
74265    v1 = or_logic[v1*3+v2];
74266  }
74267  pOut = &aMem[pOp->p3];
74268  if( v1==2 ){
74269    MemSetTypeFlag(pOut, MEM_Null);
74270  }else{
74271    pOut->u.i = v1;
74272    MemSetTypeFlag(pOut, MEM_Int);
74273  }
74274  break;
74275}
74276
74277/* Opcode: Not P1 P2 * * *
74278** Synopsis: r[P2]= !r[P1]
74279**
74280** Interpret the value in register P1 as a boolean value.  Store the
74281** boolean complement in register P2.  If the value in register P1 is
74282** NULL, then a NULL is stored in P2.
74283*/
74284case OP_Not: {                /* same as TK_NOT, in1, out2 */
74285  pIn1 = &aMem[pOp->p1];
74286  pOut = &aMem[pOp->p2];
74287  sqlite3VdbeMemSetNull(pOut);
74288  if( (pIn1->flags & MEM_Null)==0 ){
74289    pOut->flags = MEM_Int;
74290    pOut->u.i = !sqlite3VdbeIntValue(pIn1);
74291  }
74292  break;
74293}
74294
74295/* Opcode: BitNot P1 P2 * * *
74296** Synopsis: r[P1]= ~r[P1]
74297**
74298** Interpret the content of register P1 as an integer.  Store the
74299** ones-complement of the P1 value into register P2.  If P1 holds
74300** a NULL then store a NULL in P2.
74301*/
74302case OP_BitNot: {             /* same as TK_BITNOT, in1, out2 */
74303  pIn1 = &aMem[pOp->p1];
74304  pOut = &aMem[pOp->p2];
74305  sqlite3VdbeMemSetNull(pOut);
74306  if( (pIn1->flags & MEM_Null)==0 ){
74307    pOut->flags = MEM_Int;
74308    pOut->u.i = ~sqlite3VdbeIntValue(pIn1);
74309  }
74310  break;
74311}
74312
74313/* Opcode: Once P1 P2 * * *
74314**
74315** Check the "once" flag number P1. If it is set, jump to instruction P2.
74316** Otherwise, set the flag and fall through to the next instruction.
74317** In other words, this opcode causes all following opcodes up through P2
74318** (but not including P2) to run just once and to be skipped on subsequent
74319** times through the loop.
74320**
74321** All "once" flags are initially cleared whenever a prepared statement
74322** first begins to run.
74323*/
74324case OP_Once: {             /* jump */
74325  assert( pOp->p1<p->nOnceFlag );
74326  VdbeBranchTaken(p->aOnceFlag[pOp->p1]!=0, 2);
74327  if( p->aOnceFlag[pOp->p1] ){
74328    goto jump_to_p2;
74329  }else{
74330    p->aOnceFlag[pOp->p1] = 1;
74331  }
74332  break;
74333}
74334
74335/* Opcode: If P1 P2 P3 * *
74336**
74337** Jump to P2 if the value in register P1 is true.  The value
74338** is considered true if it is numeric and non-zero.  If the value
74339** in P1 is NULL then take the jump if and only if P3 is non-zero.
74340*/
74341/* Opcode: IfNot P1 P2 P3 * *
74342**
74343** Jump to P2 if the value in register P1 is False.  The value
74344** is considered false if it has a numeric value of zero.  If the value
74345** in P1 is NULL then take the jump if and only if P3 is non-zero.
74346*/
74347case OP_If:                 /* jump, in1 */
74348case OP_IfNot: {            /* jump, in1 */
74349  int c;
74350  pIn1 = &aMem[pOp->p1];
74351  if( pIn1->flags & MEM_Null ){
74352    c = pOp->p3;
74353  }else{
74354#ifdef SQLITE_OMIT_FLOATING_POINT
74355    c = sqlite3VdbeIntValue(pIn1)!=0;
74356#else
74357    c = sqlite3VdbeRealValue(pIn1)!=0.0;
74358#endif
74359    if( pOp->opcode==OP_IfNot ) c = !c;
74360  }
74361  VdbeBranchTaken(c!=0, 2);
74362  if( c ){
74363    goto jump_to_p2;
74364  }
74365  break;
74366}
74367
74368/* Opcode: IsNull P1 P2 * * *
74369** Synopsis:  if r[P1]==NULL goto P2
74370**
74371** Jump to P2 if the value in register P1 is NULL.
74372*/
74373case OP_IsNull: {            /* same as TK_ISNULL, jump, in1 */
74374  pIn1 = &aMem[pOp->p1];
74375  VdbeBranchTaken( (pIn1->flags & MEM_Null)!=0, 2);
74376  if( (pIn1->flags & MEM_Null)!=0 ){
74377    goto jump_to_p2;
74378  }
74379  break;
74380}
74381
74382/* Opcode: NotNull P1 P2 * * *
74383** Synopsis: if r[P1]!=NULL goto P2
74384**
74385** Jump to P2 if the value in register P1 is not NULL.
74386*/
74387case OP_NotNull: {            /* same as TK_NOTNULL, jump, in1 */
74388  pIn1 = &aMem[pOp->p1];
74389  VdbeBranchTaken( (pIn1->flags & MEM_Null)==0, 2);
74390  if( (pIn1->flags & MEM_Null)==0 ){
74391    goto jump_to_p2;
74392  }
74393  break;
74394}
74395
74396/* Opcode: Column P1 P2 P3 P4 P5
74397** Synopsis:  r[P3]=PX
74398**
74399** Interpret the data that cursor P1 points to as a structure built using
74400** the MakeRecord instruction.  (See the MakeRecord opcode for additional
74401** information about the format of the data.)  Extract the P2-th column
74402** from this record.  If there are less that (P2+1)
74403** values in the record, extract a NULL.
74404**
74405** The value extracted is stored in register P3.
74406**
74407** If the column contains fewer than P2 fields, then extract a NULL.  Or,
74408** if the P4 argument is a P4_MEM use the value of the P4 argument as
74409** the result.
74410**
74411** If the OPFLAG_CLEARCACHE bit is set on P5 and P1 is a pseudo-table cursor,
74412** then the cache of the cursor is reset prior to extracting the column.
74413** The first OP_Column against a pseudo-table after the value of the content
74414** register has changed should have this bit set.
74415**
74416** If the OPFLAG_LENGTHARG and OPFLAG_TYPEOFARG bits are set on P5 when
74417** the result is guaranteed to only be used as the argument of a length()
74418** or typeof() function, respectively.  The loading of large blobs can be
74419** skipped for length() and all content loading can be skipped for typeof().
74420*/
74421case OP_Column: {
74422  i64 payloadSize64; /* Number of bytes in the record */
74423  int p2;            /* column number to retrieve */
74424  VdbeCursor *pC;    /* The VDBE cursor */
74425  BtCursor *pCrsr;   /* The BTree cursor */
74426  u32 *aOffset;      /* aOffset[i] is offset to start of data for i-th column */
74427  int len;           /* The length of the serialized data for the column */
74428  int i;             /* Loop counter */
74429  Mem *pDest;        /* Where to write the extracted value */
74430  Mem sMem;          /* For storing the record being decoded */
74431  const u8 *zData;   /* Part of the record being decoded */
74432  const u8 *zHdr;    /* Next unparsed byte of the header */
74433  const u8 *zEndHdr; /* Pointer to first byte after the header */
74434  u32 offset;        /* Offset into the data */
74435  u32 szField;       /* Number of bytes in the content of a field */
74436  u32 avail;         /* Number of bytes of available data */
74437  u32 t;             /* A type code from the record header */
74438  u16 fx;            /* pDest->flags value */
74439  Mem *pReg;         /* PseudoTable input register */
74440
74441  p2 = pOp->p2;
74442  assert( pOp->p3>0 && pOp->p3<=(p->nMem-p->nCursor) );
74443  pDest = &aMem[pOp->p3];
74444  memAboutToChange(p, pDest);
74445  assert( pOp->p1>=0 && pOp->p1<p->nCursor );
74446  pC = p->apCsr[pOp->p1];
74447  assert( pC!=0 );
74448  assert( p2<pC->nField );
74449  aOffset = pC->aOffset;
74450#ifndef SQLITE_OMIT_VIRTUALTABLE
74451  assert( pC->pVtabCursor==0 ); /* OP_Column never called on virtual table */
74452#endif
74453  pCrsr = pC->pCursor;
74454  assert( pCrsr!=0 || pC->pseudoTableReg>0 ); /* pCrsr NULL on PseudoTables */
74455  assert( pCrsr!=0 || pC->nullRow );          /* pC->nullRow on PseudoTables */
74456
74457  /* If the cursor cache is stale, bring it up-to-date */
74458  rc = sqlite3VdbeCursorMoveto(pC);
74459  if( rc ) goto abort_due_to_error;
74460  if( pC->cacheStatus!=p->cacheCtr ){
74461    if( pC->nullRow ){
74462      if( pCrsr==0 ){
74463        assert( pC->pseudoTableReg>0 );
74464        pReg = &aMem[pC->pseudoTableReg];
74465        assert( pReg->flags & MEM_Blob );
74466        assert( memIsValid(pReg) );
74467        pC->payloadSize = pC->szRow = avail = pReg->n;
74468        pC->aRow = (u8*)pReg->z;
74469      }else{
74470        sqlite3VdbeMemSetNull(pDest);
74471        goto op_column_out;
74472      }
74473    }else{
74474      assert( pCrsr );
74475      if( pC->isTable==0 ){
74476        assert( sqlite3BtreeCursorIsValid(pCrsr) );
74477        VVA_ONLY(rc =) sqlite3BtreeKeySize(pCrsr, &payloadSize64);
74478        assert( rc==SQLITE_OK ); /* True because of CursorMoveto() call above */
74479        /* sqlite3BtreeParseCellPtr() uses getVarint32() to extract the
74480        ** payload size, so it is impossible for payloadSize64 to be
74481        ** larger than 32 bits. */
74482        assert( (payloadSize64 & SQLITE_MAX_U32)==(u64)payloadSize64 );
74483        pC->aRow = sqlite3BtreeKeyFetch(pCrsr, &avail);
74484        pC->payloadSize = (u32)payloadSize64;
74485      }else{
74486        assert( sqlite3BtreeCursorIsValid(pCrsr) );
74487        VVA_ONLY(rc =) sqlite3BtreeDataSize(pCrsr, &pC->payloadSize);
74488        assert( rc==SQLITE_OK );   /* DataSize() cannot fail */
74489        pC->aRow = sqlite3BtreeDataFetch(pCrsr, &avail);
74490      }
74491      assert( avail<=65536 );  /* Maximum page size is 64KiB */
74492      if( pC->payloadSize <= (u32)avail ){
74493        pC->szRow = pC->payloadSize;
74494      }else{
74495        pC->szRow = avail;
74496      }
74497      if( pC->payloadSize > (u32)db->aLimit[SQLITE_LIMIT_LENGTH] ){
74498        goto too_big;
74499      }
74500    }
74501    pC->cacheStatus = p->cacheCtr;
74502    pC->iHdrOffset = getVarint32(pC->aRow, offset);
74503    pC->nHdrParsed = 0;
74504    aOffset[0] = offset;
74505
74506    /* Make sure a corrupt database has not given us an oversize header.
74507    ** Do this now to avoid an oversize memory allocation.
74508    **
74509    ** Type entries can be between 1 and 5 bytes each.  But 4 and 5 byte
74510    ** types use so much data space that there can only be 4096 and 32 of
74511    ** them, respectively.  So the maximum header length results from a
74512    ** 3-byte type for each of the maximum of 32768 columns plus three
74513    ** extra bytes for the header length itself.  32768*3 + 3 = 98307.
74514    */
74515    if( offset > 98307 || offset > pC->payloadSize ){
74516      rc = SQLITE_CORRUPT_BKPT;
74517      goto op_column_error;
74518    }
74519
74520    if( avail<offset ){
74521      /* pC->aRow does not have to hold the entire row, but it does at least
74522      ** need to cover the header of the record.  If pC->aRow does not contain
74523      ** the complete header, then set it to zero, forcing the header to be
74524      ** dynamically allocated. */
74525      pC->aRow = 0;
74526      pC->szRow = 0;
74527    }
74528
74529    /* The following goto is an optimization.  It can be omitted and
74530    ** everything will still work.  But OP_Column is measurably faster
74531    ** by skipping the subsequent conditional, which is always true.
74532    */
74533    assert( pC->nHdrParsed<=p2 );         /* Conditional skipped */
74534    goto op_column_read_header;
74535  }
74536
74537  /* Make sure at least the first p2+1 entries of the header have been
74538  ** parsed and valid information is in aOffset[] and pC->aType[].
74539  */
74540  if( pC->nHdrParsed<=p2 ){
74541    /* If there is more header available for parsing in the record, try
74542    ** to extract additional fields up through the p2+1-th field
74543    */
74544    op_column_read_header:
74545    if( pC->iHdrOffset<aOffset[0] ){
74546      /* Make sure zData points to enough of the record to cover the header. */
74547      if( pC->aRow==0 ){
74548        memset(&sMem, 0, sizeof(sMem));
74549        rc = sqlite3VdbeMemFromBtree(pCrsr, 0, aOffset[0],
74550                                     !pC->isTable, &sMem);
74551        if( rc!=SQLITE_OK ){
74552          goto op_column_error;
74553        }
74554        zData = (u8*)sMem.z;
74555      }else{
74556        zData = pC->aRow;
74557      }
74558
74559      /* Fill in pC->aType[i] and aOffset[i] values through the p2-th field. */
74560      i = pC->nHdrParsed;
74561      offset = aOffset[i];
74562      zHdr = zData + pC->iHdrOffset;
74563      zEndHdr = zData + aOffset[0];
74564      assert( i<=p2 && zHdr<zEndHdr );
74565      do{
74566        if( zHdr[0]<0x80 ){
74567          t = zHdr[0];
74568          zHdr++;
74569        }else{
74570          zHdr += sqlite3GetVarint32(zHdr, &t);
74571        }
74572        pC->aType[i] = t;
74573        szField = sqlite3VdbeSerialTypeLen(t);
74574        offset += szField;
74575        if( offset<szField ){  /* True if offset overflows */
74576          zHdr = &zEndHdr[1];  /* Forces SQLITE_CORRUPT return below */
74577          break;
74578        }
74579        i++;
74580        aOffset[i] = offset;
74581      }while( i<=p2 && zHdr<zEndHdr );
74582      pC->nHdrParsed = i;
74583      pC->iHdrOffset = (u32)(zHdr - zData);
74584      if( pC->aRow==0 ){
74585        sqlite3VdbeMemRelease(&sMem);
74586        sMem.flags = MEM_Null;
74587      }
74588
74589      /* The record is corrupt if any of the following are true:
74590      ** (1) the bytes of the header extend past the declared header size
74591      **          (zHdr>zEndHdr)
74592      ** (2) the entire header was used but not all data was used
74593      **          (zHdr==zEndHdr && offset!=pC->payloadSize)
74594      ** (3) the end of the data extends beyond the end of the record.
74595      **          (offset > pC->payloadSize)
74596      */
74597      if( (zHdr>=zEndHdr && (zHdr>zEndHdr || offset!=pC->payloadSize))
74598       || (offset > pC->payloadSize)
74599      ){
74600        rc = SQLITE_CORRUPT_BKPT;
74601        goto op_column_error;
74602      }
74603    }
74604
74605    /* If after trying to extract new entries from the header, nHdrParsed is
74606    ** still not up to p2, that means that the record has fewer than p2
74607    ** columns.  So the result will be either the default value or a NULL.
74608    */
74609    if( pC->nHdrParsed<=p2 ){
74610      if( pOp->p4type==P4_MEM ){
74611        sqlite3VdbeMemShallowCopy(pDest, pOp->p4.pMem, MEM_Static);
74612      }else{
74613        sqlite3VdbeMemSetNull(pDest);
74614      }
74615      goto op_column_out;
74616    }
74617  }
74618
74619  /* Extract the content for the p2+1-th column.  Control can only
74620  ** reach this point if aOffset[p2], aOffset[p2+1], and pC->aType[p2] are
74621  ** all valid.
74622  */
74623  assert( p2<pC->nHdrParsed );
74624  assert( rc==SQLITE_OK );
74625  assert( sqlite3VdbeCheckMemInvariants(pDest) );
74626  if( VdbeMemDynamic(pDest) ) sqlite3VdbeMemSetNull(pDest);
74627  t = pC->aType[p2];
74628  if( pC->szRow>=aOffset[p2+1] ){
74629    /* This is the common case where the desired content fits on the original
74630    ** page - where the content is not on an overflow page */
74631    sqlite3VdbeSerialGet(pC->aRow+aOffset[p2], t, pDest);
74632  }else{
74633    /* This branch happens only when content is on overflow pages */
74634    if( ((pOp->p5 & (OPFLAG_LENGTHARG|OPFLAG_TYPEOFARG))!=0
74635          && ((t>=12 && (t&1)==0) || (pOp->p5 & OPFLAG_TYPEOFARG)!=0))
74636     || (len = sqlite3VdbeSerialTypeLen(t))==0
74637    ){
74638      /* Content is irrelevant for
74639      **    1. the typeof() function,
74640      **    2. the length(X) function if X is a blob, and
74641      **    3. if the content length is zero.
74642      ** So we might as well use bogus content rather than reading
74643      ** content from disk.  NULL will work for the value for strings
74644      ** and blobs and whatever is in the payloadSize64 variable
74645      ** will work for everything else. */
74646      sqlite3VdbeSerialGet(t<=13 ? (u8*)&payloadSize64 : 0, t, pDest);
74647    }else{
74648      rc = sqlite3VdbeMemFromBtree(pCrsr, aOffset[p2], len, !pC->isTable,
74649                                   pDest);
74650      if( rc!=SQLITE_OK ){
74651        goto op_column_error;
74652      }
74653      sqlite3VdbeSerialGet((const u8*)pDest->z, t, pDest);
74654      pDest->flags &= ~MEM_Ephem;
74655    }
74656  }
74657  pDest->enc = encoding;
74658
74659op_column_out:
74660  /* If the column value is an ephemeral string, go ahead and persist
74661  ** that string in case the cursor moves before the column value is
74662  ** used.  The following code does the equivalent of Deephemeralize()
74663  ** but does it faster. */
74664  if( (pDest->flags & MEM_Ephem)!=0 && pDest->z ){
74665    fx = pDest->flags & (MEM_Str|MEM_Blob);
74666    assert( fx!=0 );
74667    zData = (const u8*)pDest->z;
74668    len = pDest->n;
74669    if( sqlite3VdbeMemClearAndResize(pDest, len+2) ) goto no_mem;
74670    memcpy(pDest->z, zData, len);
74671    pDest->z[len] = 0;
74672    pDest->z[len+1] = 0;
74673    pDest->flags = fx|MEM_Term;
74674  }
74675op_column_error:
74676  UPDATE_MAX_BLOBSIZE(pDest);
74677  REGISTER_TRACE(pOp->p3, pDest);
74678  break;
74679}
74680
74681/* Opcode: Affinity P1 P2 * P4 *
74682** Synopsis: affinity(r[P1@P2])
74683**
74684** Apply affinities to a range of P2 registers starting with P1.
74685**
74686** P4 is a string that is P2 characters long. The nth character of the
74687** string indicates the column affinity that should be used for the nth
74688** memory cell in the range.
74689*/
74690case OP_Affinity: {
74691  const char *zAffinity;   /* The affinity to be applied */
74692  char cAff;               /* A single character of affinity */
74693
74694  zAffinity = pOp->p4.z;
74695  assert( zAffinity!=0 );
74696  assert( zAffinity[pOp->p2]==0 );
74697  pIn1 = &aMem[pOp->p1];
74698  while( (cAff = *(zAffinity++))!=0 ){
74699    assert( pIn1 <= &p->aMem[(p->nMem-p->nCursor)] );
74700    assert( memIsValid(pIn1) );
74701    applyAffinity(pIn1, cAff, encoding);
74702    pIn1++;
74703  }
74704  break;
74705}
74706
74707/* Opcode: MakeRecord P1 P2 P3 P4 *
74708** Synopsis: r[P3]=mkrec(r[P1@P2])
74709**
74710** Convert P2 registers beginning with P1 into the [record format]
74711** use as a data record in a database table or as a key
74712** in an index.  The OP_Column opcode can decode the record later.
74713**
74714** P4 may be a string that is P2 characters long.  The nth character of the
74715** string indicates the column affinity that should be used for the nth
74716** field of the index key.
74717**
74718** The mapping from character to affinity is given by the SQLITE_AFF_
74719** macros defined in sqliteInt.h.
74720**
74721** If P4 is NULL then all index fields have the affinity BLOB.
74722*/
74723case OP_MakeRecord: {
74724  u8 *zNewRecord;        /* A buffer to hold the data for the new record */
74725  Mem *pRec;             /* The new record */
74726  u64 nData;             /* Number of bytes of data space */
74727  int nHdr;              /* Number of bytes of header space */
74728  i64 nByte;             /* Data space required for this record */
74729  i64 nZero;             /* Number of zero bytes at the end of the record */
74730  int nVarint;           /* Number of bytes in a varint */
74731  u32 serial_type;       /* Type field */
74732  Mem *pData0;           /* First field to be combined into the record */
74733  Mem *pLast;            /* Last field of the record */
74734  int nField;            /* Number of fields in the record */
74735  char *zAffinity;       /* The affinity string for the record */
74736  int file_format;       /* File format to use for encoding */
74737  int i;                 /* Space used in zNewRecord[] header */
74738  int j;                 /* Space used in zNewRecord[] content */
74739  int len;               /* Length of a field */
74740
74741  /* Assuming the record contains N fields, the record format looks
74742  ** like this:
74743  **
74744  ** ------------------------------------------------------------------------
74745  ** | hdr-size | type 0 | type 1 | ... | type N-1 | data0 | ... | data N-1 |
74746  ** ------------------------------------------------------------------------
74747  **
74748  ** Data(0) is taken from register P1.  Data(1) comes from register P1+1
74749  ** and so forth.
74750  **
74751  ** Each type field is a varint representing the serial type of the
74752  ** corresponding data element (see sqlite3VdbeSerialType()). The
74753  ** hdr-size field is also a varint which is the offset from the beginning
74754  ** of the record to data0.
74755  */
74756  nData = 0;         /* Number of bytes of data space */
74757  nHdr = 0;          /* Number of bytes of header space */
74758  nZero = 0;         /* Number of zero bytes at the end of the record */
74759  nField = pOp->p1;
74760  zAffinity = pOp->p4.z;
74761  assert( nField>0 && pOp->p2>0 && pOp->p2+nField<=(p->nMem-p->nCursor)+1 );
74762  pData0 = &aMem[nField];
74763  nField = pOp->p2;
74764  pLast = &pData0[nField-1];
74765  file_format = p->minWriteFileFormat;
74766
74767  /* Identify the output register */
74768  assert( pOp->p3<pOp->p1 || pOp->p3>=pOp->p1+pOp->p2 );
74769  pOut = &aMem[pOp->p3];
74770  memAboutToChange(p, pOut);
74771
74772  /* Apply the requested affinity to all inputs
74773  */
74774  assert( pData0<=pLast );
74775  if( zAffinity ){
74776    pRec = pData0;
74777    do{
74778      applyAffinity(pRec++, *(zAffinity++), encoding);
74779      assert( zAffinity[0]==0 || pRec<=pLast );
74780    }while( zAffinity[0] );
74781  }
74782
74783  /* Loop through the elements that will make up the record to figure
74784  ** out how much space is required for the new record.
74785  */
74786  pRec = pLast;
74787  do{
74788    assert( memIsValid(pRec) );
74789    pRec->uTemp = serial_type = sqlite3VdbeSerialType(pRec, file_format);
74790    len = sqlite3VdbeSerialTypeLen(serial_type);
74791    if( pRec->flags & MEM_Zero ){
74792      if( nData ){
74793        if( sqlite3VdbeMemExpandBlob(pRec) ) goto no_mem;
74794      }else{
74795        nZero += pRec->u.nZero;
74796        len -= pRec->u.nZero;
74797      }
74798    }
74799    nData += len;
74800    testcase( serial_type==127 );
74801    testcase( serial_type==128 );
74802    nHdr += serial_type<=127 ? 1 : sqlite3VarintLen(serial_type);
74803  }while( (--pRec)>=pData0 );
74804
74805  /* EVIDENCE-OF: R-22564-11647 The header begins with a single varint
74806  ** which determines the total number of bytes in the header. The varint
74807  ** value is the size of the header in bytes including the size varint
74808  ** itself. */
74809  testcase( nHdr==126 );
74810  testcase( nHdr==127 );
74811  if( nHdr<=126 ){
74812    /* The common case */
74813    nHdr += 1;
74814  }else{
74815    /* Rare case of a really large header */
74816    nVarint = sqlite3VarintLen(nHdr);
74817    nHdr += nVarint;
74818    if( nVarint<sqlite3VarintLen(nHdr) ) nHdr++;
74819  }
74820  nByte = nHdr+nData;
74821  if( nByte+nZero>db->aLimit[SQLITE_LIMIT_LENGTH] ){
74822    goto too_big;
74823  }
74824
74825  /* Make sure the output register has a buffer large enough to store
74826  ** the new record. The output register (pOp->p3) is not allowed to
74827  ** be one of the input registers (because the following call to
74828  ** sqlite3VdbeMemClearAndResize() could clobber the value before it is used).
74829  */
74830  if( sqlite3VdbeMemClearAndResize(pOut, (int)nByte) ){
74831    goto no_mem;
74832  }
74833  zNewRecord = (u8 *)pOut->z;
74834
74835  /* Write the record */
74836  i = putVarint32(zNewRecord, nHdr);
74837  j = nHdr;
74838  assert( pData0<=pLast );
74839  pRec = pData0;
74840  do{
74841    serial_type = pRec->uTemp;
74842    /* EVIDENCE-OF: R-06529-47362 Following the size varint are one or more
74843    ** additional varints, one per column. */
74844    i += putVarint32(&zNewRecord[i], serial_type);            /* serial type */
74845    /* EVIDENCE-OF: R-64536-51728 The values for each column in the record
74846    ** immediately follow the header. */
74847    j += sqlite3VdbeSerialPut(&zNewRecord[j], pRec, serial_type); /* content */
74848  }while( (++pRec)<=pLast );
74849  assert( i==nHdr );
74850  assert( j==nByte );
74851
74852  assert( pOp->p3>0 && pOp->p3<=(p->nMem-p->nCursor) );
74853  pOut->n = (int)nByte;
74854  pOut->flags = MEM_Blob;
74855  if( nZero ){
74856    pOut->u.nZero = nZero;
74857    pOut->flags |= MEM_Zero;
74858  }
74859  pOut->enc = SQLITE_UTF8;  /* In case the blob is ever converted to text */
74860  REGISTER_TRACE(pOp->p3, pOut);
74861  UPDATE_MAX_BLOBSIZE(pOut);
74862  break;
74863}
74864
74865/* Opcode: Count P1 P2 * * *
74866** Synopsis: r[P2]=count()
74867**
74868** Store the number of entries (an integer value) in the table or index
74869** opened by cursor P1 in register P2
74870*/
74871#ifndef SQLITE_OMIT_BTREECOUNT
74872case OP_Count: {         /* out2 */
74873  i64 nEntry;
74874  BtCursor *pCrsr;
74875
74876  pCrsr = p->apCsr[pOp->p1]->pCursor;
74877  assert( pCrsr );
74878  nEntry = 0;  /* Not needed.  Only used to silence a warning. */
74879  rc = sqlite3BtreeCount(pCrsr, &nEntry);
74880  pOut = out2Prerelease(p, pOp);
74881  pOut->u.i = nEntry;
74882  break;
74883}
74884#endif
74885
74886/* Opcode: Savepoint P1 * * P4 *
74887**
74888** Open, release or rollback the savepoint named by parameter P4, depending
74889** on the value of P1. To open a new savepoint, P1==0. To release (commit) an
74890** existing savepoint, P1==1, or to rollback an existing savepoint P1==2.
74891*/
74892case OP_Savepoint: {
74893  int p1;                         /* Value of P1 operand */
74894  char *zName;                    /* Name of savepoint */
74895  int nName;
74896  Savepoint *pNew;
74897  Savepoint *pSavepoint;
74898  Savepoint *pTmp;
74899  int iSavepoint;
74900  int ii;
74901
74902  p1 = pOp->p1;
74903  zName = pOp->p4.z;
74904
74905  /* Assert that the p1 parameter is valid. Also that if there is no open
74906  ** transaction, then there cannot be any savepoints.
74907  */
74908  assert( db->pSavepoint==0 || db->autoCommit==0 );
74909  assert( p1==SAVEPOINT_BEGIN||p1==SAVEPOINT_RELEASE||p1==SAVEPOINT_ROLLBACK );
74910  assert( db->pSavepoint || db->isTransactionSavepoint==0 );
74911  assert( checkSavepointCount(db) );
74912  assert( p->bIsReader );
74913
74914  if( p1==SAVEPOINT_BEGIN ){
74915    if( db->nVdbeWrite>0 ){
74916      /* A new savepoint cannot be created if there are active write
74917      ** statements (i.e. open read/write incremental blob handles).
74918      */
74919      sqlite3VdbeError(p, "cannot open savepoint - SQL statements in progress");
74920      rc = SQLITE_BUSY;
74921    }else{
74922      nName = sqlite3Strlen30(zName);
74923
74924#ifndef SQLITE_OMIT_VIRTUALTABLE
74925      /* This call is Ok even if this savepoint is actually a transaction
74926      ** savepoint (and therefore should not prompt xSavepoint()) callbacks.
74927      ** If this is a transaction savepoint being opened, it is guaranteed
74928      ** that the db->aVTrans[] array is empty.  */
74929      assert( db->autoCommit==0 || db->nVTrans==0 );
74930      rc = sqlite3VtabSavepoint(db, SAVEPOINT_BEGIN,
74931                                db->nStatement+db->nSavepoint);
74932      if( rc!=SQLITE_OK ) goto abort_due_to_error;
74933#endif
74934
74935      /* Create a new savepoint structure. */
74936      pNew = sqlite3DbMallocRaw(db, sizeof(Savepoint)+nName+1);
74937      if( pNew ){
74938        pNew->zName = (char *)&pNew[1];
74939        memcpy(pNew->zName, zName, nName+1);
74940
74941        /* If there is no open transaction, then mark this as a special
74942        ** "transaction savepoint". */
74943        if( db->autoCommit ){
74944          db->autoCommit = 0;
74945          db->isTransactionSavepoint = 1;
74946        }else{
74947          db->nSavepoint++;
74948        }
74949
74950        /* Link the new savepoint into the database handle's list. */
74951        pNew->pNext = db->pSavepoint;
74952        db->pSavepoint = pNew;
74953        pNew->nDeferredCons = db->nDeferredCons;
74954        pNew->nDeferredImmCons = db->nDeferredImmCons;
74955      }
74956    }
74957  }else{
74958    iSavepoint = 0;
74959
74960    /* Find the named savepoint. If there is no such savepoint, then an
74961    ** an error is returned to the user.  */
74962    for(
74963      pSavepoint = db->pSavepoint;
74964      pSavepoint && sqlite3StrICmp(pSavepoint->zName, zName);
74965      pSavepoint = pSavepoint->pNext
74966    ){
74967      iSavepoint++;
74968    }
74969    if( !pSavepoint ){
74970      sqlite3VdbeError(p, "no such savepoint: %s", zName);
74971      rc = SQLITE_ERROR;
74972    }else if( db->nVdbeWrite>0 && p1==SAVEPOINT_RELEASE ){
74973      /* It is not possible to release (commit) a savepoint if there are
74974      ** active write statements.
74975      */
74976      sqlite3VdbeError(p, "cannot release savepoint - "
74977                          "SQL statements in progress");
74978      rc = SQLITE_BUSY;
74979    }else{
74980
74981      /* Determine whether or not this is a transaction savepoint. If so,
74982      ** and this is a RELEASE command, then the current transaction
74983      ** is committed.
74984      */
74985      int isTransaction = pSavepoint->pNext==0 && db->isTransactionSavepoint;
74986      if( isTransaction && p1==SAVEPOINT_RELEASE ){
74987        if( (rc = sqlite3VdbeCheckFk(p, 1))!=SQLITE_OK ){
74988          goto vdbe_return;
74989        }
74990        db->autoCommit = 1;
74991        if( sqlite3VdbeHalt(p)==SQLITE_BUSY ){
74992          p->pc = (int)(pOp - aOp);
74993          db->autoCommit = 0;
74994          p->rc = rc = SQLITE_BUSY;
74995          goto vdbe_return;
74996        }
74997        db->isTransactionSavepoint = 0;
74998        rc = p->rc;
74999      }else{
75000        int isSchemaChange;
75001        iSavepoint = db->nSavepoint - iSavepoint - 1;
75002        if( p1==SAVEPOINT_ROLLBACK ){
75003          isSchemaChange = (db->flags & SQLITE_InternChanges)!=0;
75004          for(ii=0; ii<db->nDb; ii++){
75005            rc = sqlite3BtreeTripAllCursors(db->aDb[ii].pBt,
75006                                       SQLITE_ABORT_ROLLBACK,
75007                                       isSchemaChange==0);
75008            if( rc!=SQLITE_OK ) goto abort_due_to_error;
75009          }
75010        }else{
75011          isSchemaChange = 0;
75012        }
75013        for(ii=0; ii<db->nDb; ii++){
75014          rc = sqlite3BtreeSavepoint(db->aDb[ii].pBt, p1, iSavepoint);
75015          if( rc!=SQLITE_OK ){
75016            goto abort_due_to_error;
75017          }
75018        }
75019        if( isSchemaChange ){
75020          sqlite3ExpirePreparedStatements(db);
75021          sqlite3ResetAllSchemasOfConnection(db);
75022          db->flags = (db->flags | SQLITE_InternChanges);
75023        }
75024      }
75025
75026      /* Regardless of whether this is a RELEASE or ROLLBACK, destroy all
75027      ** savepoints nested inside of the savepoint being operated on. */
75028      while( db->pSavepoint!=pSavepoint ){
75029        pTmp = db->pSavepoint;
75030        db->pSavepoint = pTmp->pNext;
75031        sqlite3DbFree(db, pTmp);
75032        db->nSavepoint--;
75033      }
75034
75035      /* If it is a RELEASE, then destroy the savepoint being operated on
75036      ** too. If it is a ROLLBACK TO, then set the number of deferred
75037      ** constraint violations present in the database to the value stored
75038      ** when the savepoint was created.  */
75039      if( p1==SAVEPOINT_RELEASE ){
75040        assert( pSavepoint==db->pSavepoint );
75041        db->pSavepoint = pSavepoint->pNext;
75042        sqlite3DbFree(db, pSavepoint);
75043        if( !isTransaction ){
75044          db->nSavepoint--;
75045        }
75046      }else{
75047        db->nDeferredCons = pSavepoint->nDeferredCons;
75048        db->nDeferredImmCons = pSavepoint->nDeferredImmCons;
75049      }
75050
75051      if( !isTransaction || p1==SAVEPOINT_ROLLBACK ){
75052        rc = sqlite3VtabSavepoint(db, p1, iSavepoint);
75053        if( rc!=SQLITE_OK ) goto abort_due_to_error;
75054      }
75055    }
75056  }
75057
75058  break;
75059}
75060
75061/* Opcode: AutoCommit P1 P2 * * *
75062**
75063** Set the database auto-commit flag to P1 (1 or 0). If P2 is true, roll
75064** back any currently active btree transactions. If there are any active
75065** VMs (apart from this one), then a ROLLBACK fails.  A COMMIT fails if
75066** there are active writing VMs or active VMs that use shared cache.
75067**
75068** This instruction causes the VM to halt.
75069*/
75070case OP_AutoCommit: {
75071  int desiredAutoCommit;
75072  int iRollback;
75073  int turnOnAC;
75074
75075  desiredAutoCommit = pOp->p1;
75076  iRollback = pOp->p2;
75077  turnOnAC = desiredAutoCommit && !db->autoCommit;
75078  assert( desiredAutoCommit==1 || desiredAutoCommit==0 );
75079  assert( desiredAutoCommit==1 || iRollback==0 );
75080  assert( db->nVdbeActive>0 );  /* At least this one VM is active */
75081  assert( p->bIsReader );
75082
75083  if( turnOnAC && !iRollback && db->nVdbeWrite>0 ){
75084    /* If this instruction implements a COMMIT and other VMs are writing
75085    ** return an error indicating that the other VMs must complete first.
75086    */
75087    sqlite3VdbeError(p, "cannot commit transaction - "
75088                        "SQL statements in progress");
75089    rc = SQLITE_BUSY;
75090  }else if( desiredAutoCommit!=db->autoCommit ){
75091    if( iRollback ){
75092      assert( desiredAutoCommit==1 );
75093      sqlite3RollbackAll(db, SQLITE_ABORT_ROLLBACK);
75094      db->autoCommit = 1;
75095    }else if( (rc = sqlite3VdbeCheckFk(p, 1))!=SQLITE_OK ){
75096      goto vdbe_return;
75097    }else{
75098      db->autoCommit = (u8)desiredAutoCommit;
75099      if( sqlite3VdbeHalt(p)==SQLITE_BUSY ){
75100        p->pc = (int)(pOp - aOp);
75101        db->autoCommit = (u8)(1-desiredAutoCommit);
75102        p->rc = rc = SQLITE_BUSY;
75103        goto vdbe_return;
75104      }
75105    }
75106    assert( db->nStatement==0 );
75107    sqlite3CloseSavepoints(db);
75108    if( p->rc==SQLITE_OK ){
75109      rc = SQLITE_DONE;
75110    }else{
75111      rc = SQLITE_ERROR;
75112    }
75113    goto vdbe_return;
75114  }else{
75115    sqlite3VdbeError(p,
75116        (!desiredAutoCommit)?"cannot start a transaction within a transaction":(
75117        (iRollback)?"cannot rollback - no transaction is active":
75118                   "cannot commit - no transaction is active"));
75119
75120    rc = SQLITE_ERROR;
75121  }
75122  break;
75123}
75124
75125/* Opcode: Transaction P1 P2 P3 P4 P5
75126**
75127** Begin a transaction on database P1 if a transaction is not already
75128** active.
75129** If P2 is non-zero, then a write-transaction is started, or if a
75130** read-transaction is already active, it is upgraded to a write-transaction.
75131** If P2 is zero, then a read-transaction is started.
75132**
75133** P1 is the index of the database file on which the transaction is
75134** started.  Index 0 is the main database file and index 1 is the
75135** file used for temporary tables.  Indices of 2 or more are used for
75136** attached databases.
75137**
75138** If a write-transaction is started and the Vdbe.usesStmtJournal flag is
75139** true (this flag is set if the Vdbe may modify more than one row and may
75140** throw an ABORT exception), a statement transaction may also be opened.
75141** More specifically, a statement transaction is opened iff the database
75142** connection is currently not in autocommit mode, or if there are other
75143** active statements. A statement transaction allows the changes made by this
75144** VDBE to be rolled back after an error without having to roll back the
75145** entire transaction. If no error is encountered, the statement transaction
75146** will automatically commit when the VDBE halts.
75147**
75148** If P5!=0 then this opcode also checks the schema cookie against P3
75149** and the schema generation counter against P4.
75150** The cookie changes its value whenever the database schema changes.
75151** This operation is used to detect when that the cookie has changed
75152** and that the current process needs to reread the schema.  If the schema
75153** cookie in P3 differs from the schema cookie in the database header or
75154** if the schema generation counter in P4 differs from the current
75155** generation counter, then an SQLITE_SCHEMA error is raised and execution
75156** halts.  The sqlite3_step() wrapper function might then reprepare the
75157** statement and rerun it from the beginning.
75158*/
75159case OP_Transaction: {
75160  Btree *pBt;
75161  int iMeta;
75162  int iGen;
75163
75164  assert( p->bIsReader );
75165  assert( p->readOnly==0 || pOp->p2==0 );
75166  assert( pOp->p1>=0 && pOp->p1<db->nDb );
75167  assert( DbMaskTest(p->btreeMask, pOp->p1) );
75168  if( pOp->p2 && (db->flags & SQLITE_QueryOnly)!=0 ){
75169    rc = SQLITE_READONLY;
75170    goto abort_due_to_error;
75171  }
75172  pBt = db->aDb[pOp->p1].pBt;
75173
75174  if( pBt ){
75175    rc = sqlite3BtreeBeginTrans(pBt, pOp->p2);
75176    if( rc==SQLITE_BUSY ){
75177      p->pc = (int)(pOp - aOp);
75178      p->rc = rc = SQLITE_BUSY;
75179      goto vdbe_return;
75180    }
75181    if( rc!=SQLITE_OK ){
75182      goto abort_due_to_error;
75183    }
75184
75185    if( pOp->p2 && p->usesStmtJournal
75186     && (db->autoCommit==0 || db->nVdbeRead>1)
75187    ){
75188      assert( sqlite3BtreeIsInTrans(pBt) );
75189      if( p->iStatement==0 ){
75190        assert( db->nStatement>=0 && db->nSavepoint>=0 );
75191        db->nStatement++;
75192        p->iStatement = db->nSavepoint + db->nStatement;
75193      }
75194
75195      rc = sqlite3VtabSavepoint(db, SAVEPOINT_BEGIN, p->iStatement-1);
75196      if( rc==SQLITE_OK ){
75197        rc = sqlite3BtreeBeginStmt(pBt, p->iStatement);
75198      }
75199
75200      /* Store the current value of the database handles deferred constraint
75201      ** counter. If the statement transaction needs to be rolled back,
75202      ** the value of this counter needs to be restored too.  */
75203      p->nStmtDefCons = db->nDeferredCons;
75204      p->nStmtDefImmCons = db->nDeferredImmCons;
75205    }
75206
75207    /* Gather the schema version number for checking:
75208    ** IMPLEMENTATION-OF: R-32195-19465 The schema version is used by SQLite
75209    ** each time a query is executed to ensure that the internal cache of the
75210    ** schema used when compiling the SQL query matches the schema of the
75211    ** database against which the compiled query is actually executed.
75212    */
75213    sqlite3BtreeGetMeta(pBt, BTREE_SCHEMA_VERSION, (u32 *)&iMeta);
75214    iGen = db->aDb[pOp->p1].pSchema->iGeneration;
75215  }else{
75216    iGen = iMeta = 0;
75217  }
75218  assert( pOp->p5==0 || pOp->p4type==P4_INT32 );
75219  if( pOp->p5 && (iMeta!=pOp->p3 || iGen!=pOp->p4.i) ){
75220    sqlite3DbFree(db, p->zErrMsg);
75221    p->zErrMsg = sqlite3DbStrDup(db, "database schema has changed");
75222    /* If the schema-cookie from the database file matches the cookie
75223    ** stored with the in-memory representation of the schema, do
75224    ** not reload the schema from the database file.
75225    **
75226    ** If virtual-tables are in use, this is not just an optimization.
75227    ** Often, v-tables store their data in other SQLite tables, which
75228    ** are queried from within xNext() and other v-table methods using
75229    ** prepared queries. If such a query is out-of-date, we do not want to
75230    ** discard the database schema, as the user code implementing the
75231    ** v-table would have to be ready for the sqlite3_vtab structure itself
75232    ** to be invalidated whenever sqlite3_step() is called from within
75233    ** a v-table method.
75234    */
75235    if( db->aDb[pOp->p1].pSchema->schema_cookie!=iMeta ){
75236      sqlite3ResetOneSchema(db, pOp->p1);
75237    }
75238    p->expired = 1;
75239    rc = SQLITE_SCHEMA;
75240  }
75241  break;
75242}
75243
75244/* Opcode: ReadCookie P1 P2 P3 * *
75245**
75246** Read cookie number P3 from database P1 and write it into register P2.
75247** P3==1 is the schema version.  P3==2 is the database format.
75248** P3==3 is the recommended pager cache size, and so forth.  P1==0 is
75249** the main database file and P1==1 is the database file used to store
75250** temporary tables.
75251**
75252** There must be a read-lock on the database (either a transaction
75253** must be started or there must be an open cursor) before
75254** executing this instruction.
75255*/
75256case OP_ReadCookie: {               /* out2 */
75257  int iMeta;
75258  int iDb;
75259  int iCookie;
75260
75261  assert( p->bIsReader );
75262  iDb = pOp->p1;
75263  iCookie = pOp->p3;
75264  assert( pOp->p3<SQLITE_N_BTREE_META );
75265  assert( iDb>=0 && iDb<db->nDb );
75266  assert( db->aDb[iDb].pBt!=0 );
75267  assert( DbMaskTest(p->btreeMask, iDb) );
75268
75269  sqlite3BtreeGetMeta(db->aDb[iDb].pBt, iCookie, (u32 *)&iMeta);
75270  pOut = out2Prerelease(p, pOp);
75271  pOut->u.i = iMeta;
75272  break;
75273}
75274
75275/* Opcode: SetCookie P1 P2 P3 * *
75276**
75277** Write the content of register P3 (interpreted as an integer)
75278** into cookie number P2 of database P1.  P2==1 is the schema version.
75279** P2==2 is the database format. P2==3 is the recommended pager cache
75280** size, and so forth.  P1==0 is the main database file and P1==1 is the
75281** database file used to store temporary tables.
75282**
75283** A transaction must be started before executing this opcode.
75284*/
75285case OP_SetCookie: {       /* in3 */
75286  Db *pDb;
75287  assert( pOp->p2<SQLITE_N_BTREE_META );
75288  assert( pOp->p1>=0 && pOp->p1<db->nDb );
75289  assert( DbMaskTest(p->btreeMask, pOp->p1) );
75290  assert( p->readOnly==0 );
75291  pDb = &db->aDb[pOp->p1];
75292  assert( pDb->pBt!=0 );
75293  assert( sqlite3SchemaMutexHeld(db, pOp->p1, 0) );
75294  pIn3 = &aMem[pOp->p3];
75295  sqlite3VdbeMemIntegerify(pIn3);
75296  /* See note about index shifting on OP_ReadCookie */
75297  rc = sqlite3BtreeUpdateMeta(pDb->pBt, pOp->p2, (int)pIn3->u.i);
75298  if( pOp->p2==BTREE_SCHEMA_VERSION ){
75299    /* When the schema cookie changes, record the new cookie internally */
75300    pDb->pSchema->schema_cookie = (int)pIn3->u.i;
75301    db->flags |= SQLITE_InternChanges;
75302  }else if( pOp->p2==BTREE_FILE_FORMAT ){
75303    /* Record changes in the file format */
75304    pDb->pSchema->file_format = (u8)pIn3->u.i;
75305  }
75306  if( pOp->p1==1 ){
75307    /* Invalidate all prepared statements whenever the TEMP database
75308    ** schema is changed.  Ticket #1644 */
75309    sqlite3ExpirePreparedStatements(db);
75310    p->expired = 0;
75311  }
75312  break;
75313}
75314
75315/* Opcode: OpenRead P1 P2 P3 P4 P5
75316** Synopsis: root=P2 iDb=P3
75317**
75318** Open a read-only cursor for the database table whose root page is
75319** P2 in a database file.  The database file is determined by P3.
75320** P3==0 means the main database, P3==1 means the database used for
75321** temporary tables, and P3>1 means used the corresponding attached
75322** database.  Give the new cursor an identifier of P1.  The P1
75323** values need not be contiguous but all P1 values should be small integers.
75324** It is an error for P1 to be negative.
75325**
75326** If P5!=0 then use the content of register P2 as the root page, not
75327** the value of P2 itself.
75328**
75329** There will be a read lock on the database whenever there is an
75330** open cursor.  If the database was unlocked prior to this instruction
75331** then a read lock is acquired as part of this instruction.  A read
75332** lock allows other processes to read the database but prohibits
75333** any other process from modifying the database.  The read lock is
75334** released when all cursors are closed.  If this instruction attempts
75335** to get a read lock but fails, the script terminates with an
75336** SQLITE_BUSY error code.
75337**
75338** The P4 value may be either an integer (P4_INT32) or a pointer to
75339** a KeyInfo structure (P4_KEYINFO). If it is a pointer to a KeyInfo
75340** structure, then said structure defines the content and collating
75341** sequence of the index being opened. Otherwise, if P4 is an integer
75342** value, it is set to the number of columns in the table.
75343**
75344** See also: OpenWrite, ReopenIdx
75345*/
75346/* Opcode: ReopenIdx P1 P2 P3 P4 P5
75347** Synopsis: root=P2 iDb=P3
75348**
75349** The ReopenIdx opcode works exactly like ReadOpen except that it first
75350** checks to see if the cursor on P1 is already open with a root page
75351** number of P2 and if it is this opcode becomes a no-op.  In other words,
75352** if the cursor is already open, do not reopen it.
75353**
75354** The ReopenIdx opcode may only be used with P5==0 and with P4 being
75355** a P4_KEYINFO object.  Furthermore, the P3 value must be the same as
75356** every other ReopenIdx or OpenRead for the same cursor number.
75357**
75358** See the OpenRead opcode documentation for additional information.
75359*/
75360/* Opcode: OpenWrite P1 P2 P3 P4 P5
75361** Synopsis: root=P2 iDb=P3
75362**
75363** Open a read/write cursor named P1 on the table or index whose root
75364** page is P2.  Or if P5!=0 use the content of register P2 to find the
75365** root page.
75366**
75367** The P4 value may be either an integer (P4_INT32) or a pointer to
75368** a KeyInfo structure (P4_KEYINFO). If it is a pointer to a KeyInfo
75369** structure, then said structure defines the content and collating
75370** sequence of the index being opened. Otherwise, if P4 is an integer
75371** value, it is set to the number of columns in the table, or to the
75372** largest index of any column of the table that is actually used.
75373**
75374** This instruction works just like OpenRead except that it opens the cursor
75375** in read/write mode.  For a given table, there can be one or more read-only
75376** cursors or a single read/write cursor but not both.
75377**
75378** See also OpenRead.
75379*/
75380case OP_ReopenIdx: {
75381  int nField;
75382  KeyInfo *pKeyInfo;
75383  int p2;
75384  int iDb;
75385  int wrFlag;
75386  Btree *pX;
75387  VdbeCursor *pCur;
75388  Db *pDb;
75389
75390  assert( pOp->p5==0 || pOp->p5==OPFLAG_SEEKEQ );
75391  assert( pOp->p4type==P4_KEYINFO );
75392  pCur = p->apCsr[pOp->p1];
75393  if( pCur && pCur->pgnoRoot==(u32)pOp->p2 ){
75394    assert( pCur->iDb==pOp->p3 );      /* Guaranteed by the code generator */
75395    goto open_cursor_set_hints;
75396  }
75397  /* If the cursor is not currently open or is open on a different
75398  ** index, then fall through into OP_OpenRead to force a reopen */
75399case OP_OpenRead:
75400case OP_OpenWrite:
75401
75402  assert( (pOp->p5&(OPFLAG_P2ISREG|OPFLAG_BULKCSR|OPFLAG_SEEKEQ))==pOp->p5 );
75403  assert( pOp->opcode==OP_OpenWrite || pOp->p5==0 || pOp->p5==OPFLAG_SEEKEQ );
75404  assert( p->bIsReader );
75405  assert( pOp->opcode==OP_OpenRead || pOp->opcode==OP_ReopenIdx
75406          || p->readOnly==0 );
75407
75408  if( p->expired ){
75409    rc = SQLITE_ABORT_ROLLBACK;
75410    break;
75411  }
75412
75413  nField = 0;
75414  pKeyInfo = 0;
75415  p2 = pOp->p2;
75416  iDb = pOp->p3;
75417  assert( iDb>=0 && iDb<db->nDb );
75418  assert( DbMaskTest(p->btreeMask, iDb) );
75419  pDb = &db->aDb[iDb];
75420  pX = pDb->pBt;
75421  assert( pX!=0 );
75422  if( pOp->opcode==OP_OpenWrite ){
75423    wrFlag = 1;
75424    assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
75425    if( pDb->pSchema->file_format < p->minWriteFileFormat ){
75426      p->minWriteFileFormat = pDb->pSchema->file_format;
75427    }
75428  }else{
75429    wrFlag = 0;
75430  }
75431  if( pOp->p5 & OPFLAG_P2ISREG ){
75432    assert( p2>0 );
75433    assert( p2<=(p->nMem-p->nCursor) );
75434    pIn2 = &aMem[p2];
75435    assert( memIsValid(pIn2) );
75436    assert( (pIn2->flags & MEM_Int)!=0 );
75437    sqlite3VdbeMemIntegerify(pIn2);
75438    p2 = (int)pIn2->u.i;
75439    /* The p2 value always comes from a prior OP_CreateTable opcode and
75440    ** that opcode will always set the p2 value to 2 or more or else fail.
75441    ** If there were a failure, the prepared statement would have halted
75442    ** before reaching this instruction. */
75443    if( NEVER(p2<2) ) {
75444      rc = SQLITE_CORRUPT_BKPT;
75445      goto abort_due_to_error;
75446    }
75447  }
75448  if( pOp->p4type==P4_KEYINFO ){
75449    pKeyInfo = pOp->p4.pKeyInfo;
75450    assert( pKeyInfo->enc==ENC(db) );
75451    assert( pKeyInfo->db==db );
75452    nField = pKeyInfo->nField+pKeyInfo->nXField;
75453  }else if( pOp->p4type==P4_INT32 ){
75454    nField = pOp->p4.i;
75455  }
75456  assert( pOp->p1>=0 );
75457  assert( nField>=0 );
75458  testcase( nField==0 );  /* Table with INTEGER PRIMARY KEY and nothing else */
75459  pCur = allocateCursor(p, pOp->p1, nField, iDb, 1);
75460  if( pCur==0 ) goto no_mem;
75461  pCur->nullRow = 1;
75462  pCur->isOrdered = 1;
75463  pCur->pgnoRoot = p2;
75464  rc = sqlite3BtreeCursor(pX, p2, wrFlag, pKeyInfo, pCur->pCursor);
75465  pCur->pKeyInfo = pKeyInfo;
75466  /* Set the VdbeCursor.isTable variable. Previous versions of
75467  ** SQLite used to check if the root-page flags were sane at this point
75468  ** and report database corruption if they were not, but this check has
75469  ** since moved into the btree layer.  */
75470  pCur->isTable = pOp->p4type!=P4_KEYINFO;
75471
75472open_cursor_set_hints:
75473  assert( OPFLAG_BULKCSR==BTREE_BULKLOAD );
75474  assert( OPFLAG_SEEKEQ==BTREE_SEEK_EQ );
75475  sqlite3BtreeCursorHints(pCur->pCursor,
75476                          (pOp->p5 & (OPFLAG_BULKCSR|OPFLAG_SEEKEQ)));
75477  break;
75478}
75479
75480/* Opcode: OpenEphemeral P1 P2 * P4 P5
75481** Synopsis: nColumn=P2
75482**
75483** Open a new cursor P1 to a transient table.
75484** The cursor is always opened read/write even if
75485** the main database is read-only.  The ephemeral
75486** table is deleted automatically when the cursor is closed.
75487**
75488** P2 is the number of columns in the ephemeral table.
75489** The cursor points to a BTree table if P4==0 and to a BTree index
75490** if P4 is not 0.  If P4 is not NULL, it points to a KeyInfo structure
75491** that defines the format of keys in the index.
75492**
75493** The P5 parameter can be a mask of the BTREE_* flags defined
75494** in btree.h.  These flags control aspects of the operation of
75495** the btree.  The BTREE_OMIT_JOURNAL and BTREE_SINGLE flags are
75496** added automatically.
75497*/
75498/* Opcode: OpenAutoindex P1 P2 * P4 *
75499** Synopsis: nColumn=P2
75500**
75501** This opcode works the same as OP_OpenEphemeral.  It has a
75502** different name to distinguish its use.  Tables created using
75503** by this opcode will be used for automatically created transient
75504** indices in joins.
75505*/
75506case OP_OpenAutoindex:
75507case OP_OpenEphemeral: {
75508  VdbeCursor *pCx;
75509  KeyInfo *pKeyInfo;
75510
75511  static const int vfsFlags =
75512      SQLITE_OPEN_READWRITE |
75513      SQLITE_OPEN_CREATE |
75514      SQLITE_OPEN_EXCLUSIVE |
75515      SQLITE_OPEN_DELETEONCLOSE |
75516      SQLITE_OPEN_TRANSIENT_DB;
75517  assert( pOp->p1>=0 );
75518  assert( pOp->p2>=0 );
75519  pCx = allocateCursor(p, pOp->p1, pOp->p2, -1, 1);
75520  if( pCx==0 ) goto no_mem;
75521  pCx->nullRow = 1;
75522  pCx->isEphemeral = 1;
75523  rc = sqlite3BtreeOpen(db->pVfs, 0, db, &pCx->pBt,
75524                        BTREE_OMIT_JOURNAL | BTREE_SINGLE | pOp->p5, vfsFlags);
75525  if( rc==SQLITE_OK ){
75526    rc = sqlite3BtreeBeginTrans(pCx->pBt, 1);
75527  }
75528  if( rc==SQLITE_OK ){
75529    /* If a transient index is required, create it by calling
75530    ** sqlite3BtreeCreateTable() with the BTREE_BLOBKEY flag before
75531    ** opening it. If a transient table is required, just use the
75532    ** automatically created table with root-page 1 (an BLOB_INTKEY table).
75533    */
75534    if( (pKeyInfo = pOp->p4.pKeyInfo)!=0 ){
75535      int pgno;
75536      assert( pOp->p4type==P4_KEYINFO );
75537      rc = sqlite3BtreeCreateTable(pCx->pBt, &pgno, BTREE_BLOBKEY | pOp->p5);
75538      if( rc==SQLITE_OK ){
75539        assert( pgno==MASTER_ROOT+1 );
75540        assert( pKeyInfo->db==db );
75541        assert( pKeyInfo->enc==ENC(db) );
75542        pCx->pKeyInfo = pKeyInfo;
75543        rc = sqlite3BtreeCursor(pCx->pBt, pgno, 1, pKeyInfo, pCx->pCursor);
75544      }
75545      pCx->isTable = 0;
75546    }else{
75547      rc = sqlite3BtreeCursor(pCx->pBt, MASTER_ROOT, 1, 0, pCx->pCursor);
75548      pCx->isTable = 1;
75549    }
75550  }
75551  pCx->isOrdered = (pOp->p5!=BTREE_UNORDERED);
75552  break;
75553}
75554
75555/* Opcode: SorterOpen P1 P2 P3 P4 *
75556**
75557** This opcode works like OP_OpenEphemeral except that it opens
75558** a transient index that is specifically designed to sort large
75559** tables using an external merge-sort algorithm.
75560**
75561** If argument P3 is non-zero, then it indicates that the sorter may
75562** assume that a stable sort considering the first P3 fields of each
75563** key is sufficient to produce the required results.
75564*/
75565case OP_SorterOpen: {
75566  VdbeCursor *pCx;
75567
75568  assert( pOp->p1>=0 );
75569  assert( pOp->p2>=0 );
75570  pCx = allocateCursor(p, pOp->p1, pOp->p2, -1, 1);
75571  if( pCx==0 ) goto no_mem;
75572  pCx->pKeyInfo = pOp->p4.pKeyInfo;
75573  assert( pCx->pKeyInfo->db==db );
75574  assert( pCx->pKeyInfo->enc==ENC(db) );
75575  rc = sqlite3VdbeSorterInit(db, pOp->p3, pCx);
75576  break;
75577}
75578
75579/* Opcode: SequenceTest P1 P2 * * *
75580** Synopsis: if( cursor[P1].ctr++ ) pc = P2
75581**
75582** P1 is a sorter cursor. If the sequence counter is currently zero, jump
75583** to P2. Regardless of whether or not the jump is taken, increment the
75584** the sequence value.
75585*/
75586case OP_SequenceTest: {
75587  VdbeCursor *pC;
75588  assert( pOp->p1>=0 && pOp->p1<p->nCursor );
75589  pC = p->apCsr[pOp->p1];
75590  assert( pC->pSorter );
75591  if( (pC->seqCount++)==0 ){
75592    goto jump_to_p2;
75593  }
75594  break;
75595}
75596
75597/* Opcode: OpenPseudo P1 P2 P3 * *
75598** Synopsis: P3 columns in r[P2]
75599**
75600** Open a new cursor that points to a fake table that contains a single
75601** row of data.  The content of that one row is the content of memory
75602** register P2.  In other words, cursor P1 becomes an alias for the
75603** MEM_Blob content contained in register P2.
75604**
75605** A pseudo-table created by this opcode is used to hold a single
75606** row output from the sorter so that the row can be decomposed into
75607** individual columns using the OP_Column opcode.  The OP_Column opcode
75608** is the only cursor opcode that works with a pseudo-table.
75609**
75610** P3 is the number of fields in the records that will be stored by
75611** the pseudo-table.
75612*/
75613case OP_OpenPseudo: {
75614  VdbeCursor *pCx;
75615
75616  assert( pOp->p1>=0 );
75617  assert( pOp->p3>=0 );
75618  pCx = allocateCursor(p, pOp->p1, pOp->p3, -1, 0);
75619  if( pCx==0 ) goto no_mem;
75620  pCx->nullRow = 1;
75621  pCx->pseudoTableReg = pOp->p2;
75622  pCx->isTable = 1;
75623  assert( pOp->p5==0 );
75624  break;
75625}
75626
75627/* Opcode: Close P1 * * * *
75628**
75629** Close a cursor previously opened as P1.  If P1 is not
75630** currently open, this instruction is a no-op.
75631*/
75632case OP_Close: {
75633  assert( pOp->p1>=0 && pOp->p1<p->nCursor );
75634  sqlite3VdbeFreeCursor(p, p->apCsr[pOp->p1]);
75635  p->apCsr[pOp->p1] = 0;
75636  break;
75637}
75638
75639#ifdef SQLITE_ENABLE_COLUMN_USED_MASK
75640/* Opcode: ColumnsUsed P1 * * P4 *
75641**
75642** This opcode (which only exists if SQLite was compiled with
75643** SQLITE_ENABLE_COLUMN_USED_MASK) identifies which columns of the
75644** table or index for cursor P1 are used.  P4 is a 64-bit integer
75645** (P4_INT64) in which the first 63 bits are one for each of the
75646** first 63 columns of the table or index that are actually used
75647** by the cursor.  The high-order bit is set if any column after
75648** the 64th is used.
75649*/
75650case OP_ColumnsUsed: {
75651  VdbeCursor *pC;
75652  pC = p->apCsr[pOp->p1];
75653  assert( pC->pCursor );
75654  pC->maskUsed = *(u64*)pOp->p4.pI64;
75655  break;
75656}
75657#endif
75658
75659/* Opcode: SeekGE P1 P2 P3 P4 *
75660** Synopsis: key=r[P3@P4]
75661**
75662** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
75663** use the value in register P3 as the key.  If cursor P1 refers
75664** to an SQL index, then P3 is the first in an array of P4 registers
75665** that are used as an unpacked index key.
75666**
75667** Reposition cursor P1 so that  it points to the smallest entry that
75668** is greater than or equal to the key value. If there are no records
75669** greater than or equal to the key and P2 is not zero, then jump to P2.
75670**
75671** This opcode leaves the cursor configured to move in forward order,
75672** from the beginning toward the end.  In other words, the cursor is
75673** configured to use Next, not Prev.
75674**
75675** See also: Found, NotFound, SeekLt, SeekGt, SeekLe
75676*/
75677/* Opcode: SeekGT P1 P2 P3 P4 *
75678** Synopsis: key=r[P3@P4]
75679**
75680** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
75681** use the value in register P3 as a key. If cursor P1 refers
75682** to an SQL index, then P3 is the first in an array of P4 registers
75683** that are used as an unpacked index key.
75684**
75685** Reposition cursor P1 so that  it points to the smallest entry that
75686** is greater than the key value. If there are no records greater than
75687** the key and P2 is not zero, then jump to P2.
75688**
75689** This opcode leaves the cursor configured to move in forward order,
75690** from the beginning toward the end.  In other words, the cursor is
75691** configured to use Next, not Prev.
75692**
75693** See also: Found, NotFound, SeekLt, SeekGe, SeekLe
75694*/
75695/* Opcode: SeekLT P1 P2 P3 P4 *
75696** Synopsis: key=r[P3@P4]
75697**
75698** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
75699** use the value in register P3 as a key. If cursor P1 refers
75700** to an SQL index, then P3 is the first in an array of P4 registers
75701** that are used as an unpacked index key.
75702**
75703** Reposition cursor P1 so that  it points to the largest entry that
75704** is less than the key value. If there are no records less than
75705** the key and P2 is not zero, then jump to P2.
75706**
75707** This opcode leaves the cursor configured to move in reverse order,
75708** from the end toward the beginning.  In other words, the cursor is
75709** configured to use Prev, not Next.
75710**
75711** See also: Found, NotFound, SeekGt, SeekGe, SeekLe
75712*/
75713/* Opcode: SeekLE P1 P2 P3 P4 *
75714** Synopsis: key=r[P3@P4]
75715**
75716** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
75717** use the value in register P3 as a key. If cursor P1 refers
75718** to an SQL index, then P3 is the first in an array of P4 registers
75719** that are used as an unpacked index key.
75720**
75721** Reposition cursor P1 so that it points to the largest entry that
75722** is less than or equal to the key value. If there are no records
75723** less than or equal to the key and P2 is not zero, then jump to P2.
75724**
75725** This opcode leaves the cursor configured to move in reverse order,
75726** from the end toward the beginning.  In other words, the cursor is
75727** configured to use Prev, not Next.
75728**
75729** See also: Found, NotFound, SeekGt, SeekGe, SeekLt
75730*/
75731case OP_SeekLT:         /* jump, in3 */
75732case OP_SeekLE:         /* jump, in3 */
75733case OP_SeekGE:         /* jump, in3 */
75734case OP_SeekGT: {       /* jump, in3 */
75735  int res;
75736  int oc;
75737  VdbeCursor *pC;
75738  UnpackedRecord r;
75739  int nField;
75740  i64 iKey;      /* The rowid we are to seek to */
75741
75742  assert( pOp->p1>=0 && pOp->p1<p->nCursor );
75743  assert( pOp->p2!=0 );
75744  pC = p->apCsr[pOp->p1];
75745  assert( pC!=0 );
75746  assert( pC->pseudoTableReg==0 );
75747  assert( OP_SeekLE == OP_SeekLT+1 );
75748  assert( OP_SeekGE == OP_SeekLT+2 );
75749  assert( OP_SeekGT == OP_SeekLT+3 );
75750  assert( pC->isOrdered );
75751  assert( pC->pCursor!=0 );
75752  oc = pOp->opcode;
75753  pC->nullRow = 0;
75754#ifdef SQLITE_DEBUG
75755  pC->seekOp = pOp->opcode;
75756#endif
75757
75758  /* For a cursor with the BTREE_SEEK_EQ hint, only the OP_SeekGE and
75759  ** OP_SeekLE opcodes are allowed, and these must be immediately followed
75760  ** by an OP_IdxGT or OP_IdxLT opcode, respectively, with the same key.
75761  */
75762#ifdef SQLITE_DEBUG
75763  if( sqlite3BtreeCursorHasHint(pC->pCursor, BTREE_SEEK_EQ) ){
75764    assert( pOp->opcode==OP_SeekGE || pOp->opcode==OP_SeekLE );
75765    assert( pOp[1].opcode==OP_IdxLT || pOp[1].opcode==OP_IdxGT );
75766    assert( pOp[1].p1==pOp[0].p1 );
75767    assert( pOp[1].p2==pOp[0].p2 );
75768    assert( pOp[1].p3==pOp[0].p3 );
75769    assert( pOp[1].p4.i==pOp[0].p4.i );
75770  }
75771#endif
75772
75773  if( pC->isTable ){
75774    /* The input value in P3 might be of any type: integer, real, string,
75775    ** blob, or NULL.  But it needs to be an integer before we can do
75776    ** the seek, so convert it. */
75777    pIn3 = &aMem[pOp->p3];
75778    if( (pIn3->flags & (MEM_Int|MEM_Real|MEM_Str))==MEM_Str ){
75779      applyNumericAffinity(pIn3, 0);
75780    }
75781    iKey = sqlite3VdbeIntValue(pIn3);
75782
75783    /* If the P3 value could not be converted into an integer without
75784    ** loss of information, then special processing is required... */
75785    if( (pIn3->flags & MEM_Int)==0 ){
75786      if( (pIn3->flags & MEM_Real)==0 ){
75787        /* If the P3 value cannot be converted into any kind of a number,
75788        ** then the seek is not possible, so jump to P2 */
75789        VdbeBranchTaken(1,2); goto jump_to_p2;
75790        break;
75791      }
75792
75793      /* If the approximation iKey is larger than the actual real search
75794      ** term, substitute >= for > and < for <=. e.g. if the search term
75795      ** is 4.9 and the integer approximation 5:
75796      **
75797      **        (x >  4.9)    ->     (x >= 5)
75798      **        (x <= 4.9)    ->     (x <  5)
75799      */
75800      if( pIn3->u.r<(double)iKey ){
75801        assert( OP_SeekGE==(OP_SeekGT-1) );
75802        assert( OP_SeekLT==(OP_SeekLE-1) );
75803        assert( (OP_SeekLE & 0x0001)==(OP_SeekGT & 0x0001) );
75804        if( (oc & 0x0001)==(OP_SeekGT & 0x0001) ) oc--;
75805      }
75806
75807      /* If the approximation iKey is smaller than the actual real search
75808      ** term, substitute <= for < and > for >=.  */
75809      else if( pIn3->u.r>(double)iKey ){
75810        assert( OP_SeekLE==(OP_SeekLT+1) );
75811        assert( OP_SeekGT==(OP_SeekGE+1) );
75812        assert( (OP_SeekLT & 0x0001)==(OP_SeekGE & 0x0001) );
75813        if( (oc & 0x0001)==(OP_SeekLT & 0x0001) ) oc++;
75814      }
75815    }
75816    rc = sqlite3BtreeMovetoUnpacked(pC->pCursor, 0, (u64)iKey, 0, &res);
75817    pC->movetoTarget = iKey;  /* Used by OP_Delete */
75818    if( rc!=SQLITE_OK ){
75819      goto abort_due_to_error;
75820    }
75821  }else{
75822    nField = pOp->p4.i;
75823    assert( pOp->p4type==P4_INT32 );
75824    assert( nField>0 );
75825    r.pKeyInfo = pC->pKeyInfo;
75826    r.nField = (u16)nField;
75827
75828    /* The next line of code computes as follows, only faster:
75829    **   if( oc==OP_SeekGT || oc==OP_SeekLE ){
75830    **     r.default_rc = -1;
75831    **   }else{
75832    **     r.default_rc = +1;
75833    **   }
75834    */
75835    r.default_rc = ((1 & (oc - OP_SeekLT)) ? -1 : +1);
75836    assert( oc!=OP_SeekGT || r.default_rc==-1 );
75837    assert( oc!=OP_SeekLE || r.default_rc==-1 );
75838    assert( oc!=OP_SeekGE || r.default_rc==+1 );
75839    assert( oc!=OP_SeekLT || r.default_rc==+1 );
75840
75841    r.aMem = &aMem[pOp->p3];
75842#ifdef SQLITE_DEBUG
75843    { int i; for(i=0; i<r.nField; i++) assert( memIsValid(&r.aMem[i]) ); }
75844#endif
75845    ExpandBlob(r.aMem);
75846    rc = sqlite3BtreeMovetoUnpacked(pC->pCursor, &r, 0, 0, &res);
75847    if( rc!=SQLITE_OK ){
75848      goto abort_due_to_error;
75849    }
75850  }
75851  pC->deferredMoveto = 0;
75852  pC->cacheStatus = CACHE_STALE;
75853#ifdef SQLITE_TEST
75854  sqlite3_search_count++;
75855#endif
75856  if( oc>=OP_SeekGE ){  assert( oc==OP_SeekGE || oc==OP_SeekGT );
75857    if( res<0 || (res==0 && oc==OP_SeekGT) ){
75858      res = 0;
75859      rc = sqlite3BtreeNext(pC->pCursor, &res);
75860      if( rc!=SQLITE_OK ) goto abort_due_to_error;
75861    }else{
75862      res = 0;
75863    }
75864  }else{
75865    assert( oc==OP_SeekLT || oc==OP_SeekLE );
75866    if( res>0 || (res==0 && oc==OP_SeekLT) ){
75867      res = 0;
75868      rc = sqlite3BtreePrevious(pC->pCursor, &res);
75869      if( rc!=SQLITE_OK ) goto abort_due_to_error;
75870    }else{
75871      /* res might be negative because the table is empty.  Check to
75872      ** see if this is the case.
75873      */
75874      res = sqlite3BtreeEof(pC->pCursor);
75875    }
75876  }
75877  assert( pOp->p2>0 );
75878  VdbeBranchTaken(res!=0,2);
75879  if( res ){
75880    goto jump_to_p2;
75881  }
75882  break;
75883}
75884
75885/* Opcode: Seek P1 P2 * * *
75886** Synopsis:  intkey=r[P2]
75887**
75888** P1 is an open table cursor and P2 is a rowid integer.  Arrange
75889** for P1 to move so that it points to the rowid given by P2.
75890**
75891** This is actually a deferred seek.  Nothing actually happens until
75892** the cursor is used to read a record.  That way, if no reads
75893** occur, no unnecessary I/O happens.
75894*/
75895case OP_Seek: {    /* in2 */
75896  VdbeCursor *pC;
75897
75898  assert( pOp->p1>=0 && pOp->p1<p->nCursor );
75899  pC = p->apCsr[pOp->p1];
75900  assert( pC!=0 );
75901  assert( pC->pCursor!=0 );
75902  assert( pC->isTable );
75903  pC->nullRow = 0;
75904  pIn2 = &aMem[pOp->p2];
75905  pC->movetoTarget = sqlite3VdbeIntValue(pIn2);
75906  pC->deferredMoveto = 1;
75907  break;
75908}
75909
75910
75911/* Opcode: Found P1 P2 P3 P4 *
75912** Synopsis: key=r[P3@P4]
75913**
75914** If P4==0 then register P3 holds a blob constructed by MakeRecord.  If
75915** P4>0 then register P3 is the first of P4 registers that form an unpacked
75916** record.
75917**
75918** Cursor P1 is on an index btree.  If the record identified by P3 and P4
75919** is a prefix of any entry in P1 then a jump is made to P2 and
75920** P1 is left pointing at the matching entry.
75921**
75922** This operation leaves the cursor in a state where it can be
75923** advanced in the forward direction.  The Next instruction will work,
75924** but not the Prev instruction.
75925**
75926** See also: NotFound, NoConflict, NotExists. SeekGe
75927*/
75928/* Opcode: NotFound P1 P2 P3 P4 *
75929** Synopsis: key=r[P3@P4]
75930**
75931** If P4==0 then register P3 holds a blob constructed by MakeRecord.  If
75932** P4>0 then register P3 is the first of P4 registers that form an unpacked
75933** record.
75934**
75935** Cursor P1 is on an index btree.  If the record identified by P3 and P4
75936** is not the prefix of any entry in P1 then a jump is made to P2.  If P1
75937** does contain an entry whose prefix matches the P3/P4 record then control
75938** falls through to the next instruction and P1 is left pointing at the
75939** matching entry.
75940**
75941** This operation leaves the cursor in a state where it cannot be
75942** advanced in either direction.  In other words, the Next and Prev
75943** opcodes do not work after this operation.
75944**
75945** See also: Found, NotExists, NoConflict
75946*/
75947/* Opcode: NoConflict P1 P2 P3 P4 *
75948** Synopsis: key=r[P3@P4]
75949**
75950** If P4==0 then register P3 holds a blob constructed by MakeRecord.  If
75951** P4>0 then register P3 is the first of P4 registers that form an unpacked
75952** record.
75953**
75954** Cursor P1 is on an index btree.  If the record identified by P3 and P4
75955** contains any NULL value, jump immediately to P2.  If all terms of the
75956** record are not-NULL then a check is done to determine if any row in the
75957** P1 index btree has a matching key prefix.  If there are no matches, jump
75958** immediately to P2.  If there is a match, fall through and leave the P1
75959** cursor pointing to the matching row.
75960**
75961** This opcode is similar to OP_NotFound with the exceptions that the
75962** branch is always taken if any part of the search key input is NULL.
75963**
75964** This operation leaves the cursor in a state where it cannot be
75965** advanced in either direction.  In other words, the Next and Prev
75966** opcodes do not work after this operation.
75967**
75968** See also: NotFound, Found, NotExists
75969*/
75970case OP_NoConflict:     /* jump, in3 */
75971case OP_NotFound:       /* jump, in3 */
75972case OP_Found: {        /* jump, in3 */
75973  int alreadyExists;
75974  int takeJump;
75975  int ii;
75976  VdbeCursor *pC;
75977  int res;
75978  char *pFree;
75979  UnpackedRecord *pIdxKey;
75980  UnpackedRecord r;
75981  char aTempRec[ROUND8(sizeof(UnpackedRecord)) + sizeof(Mem)*4 + 7];
75982
75983#ifdef SQLITE_TEST
75984  if( pOp->opcode!=OP_NoConflict ) sqlite3_found_count++;
75985#endif
75986
75987  assert( pOp->p1>=0 && pOp->p1<p->nCursor );
75988  assert( pOp->p4type==P4_INT32 );
75989  pC = p->apCsr[pOp->p1];
75990  assert( pC!=0 );
75991#ifdef SQLITE_DEBUG
75992  pC->seekOp = pOp->opcode;
75993#endif
75994  pIn3 = &aMem[pOp->p3];
75995  assert( pC->pCursor!=0 );
75996  assert( pC->isTable==0 );
75997  pFree = 0;
75998  if( pOp->p4.i>0 ){
75999    r.pKeyInfo = pC->pKeyInfo;
76000    r.nField = (u16)pOp->p4.i;
76001    r.aMem = pIn3;
76002    for(ii=0; ii<r.nField; ii++){
76003      assert( memIsValid(&r.aMem[ii]) );
76004      ExpandBlob(&r.aMem[ii]);
76005#ifdef SQLITE_DEBUG
76006      if( ii ) REGISTER_TRACE(pOp->p3+ii, &r.aMem[ii]);
76007#endif
76008    }
76009    pIdxKey = &r;
76010  }else{
76011    pIdxKey = sqlite3VdbeAllocUnpackedRecord(
76012        pC->pKeyInfo, aTempRec, sizeof(aTempRec), &pFree
76013    );
76014    if( pIdxKey==0 ) goto no_mem;
76015    assert( pIn3->flags & MEM_Blob );
76016    ExpandBlob(pIn3);
76017    sqlite3VdbeRecordUnpack(pC->pKeyInfo, pIn3->n, pIn3->z, pIdxKey);
76018  }
76019  pIdxKey->default_rc = 0;
76020  takeJump = 0;
76021  if( pOp->opcode==OP_NoConflict ){
76022    /* For the OP_NoConflict opcode, take the jump if any of the
76023    ** input fields are NULL, since any key with a NULL will not
76024    ** conflict */
76025    for(ii=0; ii<pIdxKey->nField; ii++){
76026      if( pIdxKey->aMem[ii].flags & MEM_Null ){
76027        takeJump = 1;
76028        break;
76029      }
76030    }
76031  }
76032  rc = sqlite3BtreeMovetoUnpacked(pC->pCursor, pIdxKey, 0, 0, &res);
76033  sqlite3DbFree(db, pFree);
76034  if( rc!=SQLITE_OK ){
76035    break;
76036  }
76037  pC->seekResult = res;
76038  alreadyExists = (res==0);
76039  pC->nullRow = 1-alreadyExists;
76040  pC->deferredMoveto = 0;
76041  pC->cacheStatus = CACHE_STALE;
76042  if( pOp->opcode==OP_Found ){
76043    VdbeBranchTaken(alreadyExists!=0,2);
76044    if( alreadyExists ) goto jump_to_p2;
76045  }else{
76046    VdbeBranchTaken(takeJump||alreadyExists==0,2);
76047    if( takeJump || !alreadyExists ) goto jump_to_p2;
76048  }
76049  break;
76050}
76051
76052/* Opcode: NotExists P1 P2 P3 * *
76053** Synopsis: intkey=r[P3]
76054**
76055** P1 is the index of a cursor open on an SQL table btree (with integer
76056** keys).  P3 is an integer rowid.  If P1 does not contain a record with
76057** rowid P3 then jump immediately to P2.  If P1 does contain a record
76058** with rowid P3 then leave the cursor pointing at that record and fall
76059** through to the next instruction.
76060**
76061** The OP_NotFound opcode performs the same operation on index btrees
76062** (with arbitrary multi-value keys).
76063**
76064** This opcode leaves the cursor in a state where it cannot be advanced
76065** in either direction.  In other words, the Next and Prev opcodes will
76066** not work following this opcode.
76067**
76068** See also: Found, NotFound, NoConflict
76069*/
76070case OP_NotExists: {        /* jump, in3 */
76071  VdbeCursor *pC;
76072  BtCursor *pCrsr;
76073  int res;
76074  u64 iKey;
76075
76076  pIn3 = &aMem[pOp->p3];
76077  assert( pIn3->flags & MEM_Int );
76078  assert( pOp->p1>=0 && pOp->p1<p->nCursor );
76079  pC = p->apCsr[pOp->p1];
76080  assert( pC!=0 );
76081#ifdef SQLITE_DEBUG
76082  pC->seekOp = 0;
76083#endif
76084  assert( pC->isTable );
76085  assert( pC->pseudoTableReg==0 );
76086  pCrsr = pC->pCursor;
76087  assert( pCrsr!=0 );
76088  res = 0;
76089  iKey = pIn3->u.i;
76090  rc = sqlite3BtreeMovetoUnpacked(pCrsr, 0, iKey, 0, &res);
76091  pC->movetoTarget = iKey;  /* Used by OP_Delete */
76092  pC->nullRow = 0;
76093  pC->cacheStatus = CACHE_STALE;
76094  pC->deferredMoveto = 0;
76095  VdbeBranchTaken(res!=0,2);
76096  pC->seekResult = res;
76097  if( res!=0 ) goto jump_to_p2;
76098  break;
76099}
76100
76101/* Opcode: Sequence P1 P2 * * *
76102** Synopsis: r[P2]=cursor[P1].ctr++
76103**
76104** Find the next available sequence number for cursor P1.
76105** Write the sequence number into register P2.
76106** The sequence number on the cursor is incremented after this
76107** instruction.
76108*/
76109case OP_Sequence: {           /* out2 */
76110  assert( pOp->p1>=0 && pOp->p1<p->nCursor );
76111  assert( p->apCsr[pOp->p1]!=0 );
76112  pOut = out2Prerelease(p, pOp);
76113  pOut->u.i = p->apCsr[pOp->p1]->seqCount++;
76114  break;
76115}
76116
76117
76118/* Opcode: NewRowid P1 P2 P3 * *
76119** Synopsis: r[P2]=rowid
76120**
76121** Get a new integer record number (a.k.a "rowid") used as the key to a table.
76122** The record number is not previously used as a key in the database
76123** table that cursor P1 points to.  The new record number is written
76124** written to register P2.
76125**
76126** If P3>0 then P3 is a register in the root frame of this VDBE that holds
76127** the largest previously generated record number. No new record numbers are
76128** allowed to be less than this value. When this value reaches its maximum,
76129** an SQLITE_FULL error is generated. The P3 register is updated with the '
76130** generated record number. This P3 mechanism is used to help implement the
76131** AUTOINCREMENT feature.
76132*/
76133case OP_NewRowid: {           /* out2 */
76134  i64 v;                 /* The new rowid */
76135  VdbeCursor *pC;        /* Cursor of table to get the new rowid */
76136  int res;               /* Result of an sqlite3BtreeLast() */
76137  int cnt;               /* Counter to limit the number of searches */
76138  Mem *pMem;             /* Register holding largest rowid for AUTOINCREMENT */
76139  VdbeFrame *pFrame;     /* Root frame of VDBE */
76140
76141  v = 0;
76142  res = 0;
76143  pOut = out2Prerelease(p, pOp);
76144  assert( pOp->p1>=0 && pOp->p1<p->nCursor );
76145  pC = p->apCsr[pOp->p1];
76146  assert( pC!=0 );
76147  assert( pC->pCursor!=0 );
76148  {
76149    /* The next rowid or record number (different terms for the same
76150    ** thing) is obtained in a two-step algorithm.
76151    **
76152    ** First we attempt to find the largest existing rowid and add one
76153    ** to that.  But if the largest existing rowid is already the maximum
76154    ** positive integer, we have to fall through to the second
76155    ** probabilistic algorithm
76156    **
76157    ** The second algorithm is to select a rowid at random and see if
76158    ** it already exists in the table.  If it does not exist, we have
76159    ** succeeded.  If the random rowid does exist, we select a new one
76160    ** and try again, up to 100 times.
76161    */
76162    assert( pC->isTable );
76163
76164#ifdef SQLITE_32BIT_ROWID
76165#   define MAX_ROWID 0x7fffffff
76166#else
76167    /* Some compilers complain about constants of the form 0x7fffffffffffffff.
76168    ** Others complain about 0x7ffffffffffffffffLL.  The following macro seems
76169    ** to provide the constant while making all compilers happy.
76170    */
76171#   define MAX_ROWID  (i64)( (((u64)0x7fffffff)<<32) | (u64)0xffffffff )
76172#endif
76173
76174    if( !pC->useRandomRowid ){
76175      rc = sqlite3BtreeLast(pC->pCursor, &res);
76176      if( rc!=SQLITE_OK ){
76177        goto abort_due_to_error;
76178      }
76179      if( res ){
76180        v = 1;   /* IMP: R-61914-48074 */
76181      }else{
76182        assert( sqlite3BtreeCursorIsValid(pC->pCursor) );
76183        rc = sqlite3BtreeKeySize(pC->pCursor, &v);
76184        assert( rc==SQLITE_OK );   /* Cannot fail following BtreeLast() */
76185        if( v>=MAX_ROWID ){
76186          pC->useRandomRowid = 1;
76187        }else{
76188          v++;   /* IMP: R-29538-34987 */
76189        }
76190      }
76191    }
76192
76193#ifndef SQLITE_OMIT_AUTOINCREMENT
76194    if( pOp->p3 ){
76195      /* Assert that P3 is a valid memory cell. */
76196      assert( pOp->p3>0 );
76197      if( p->pFrame ){
76198        for(pFrame=p->pFrame; pFrame->pParent; pFrame=pFrame->pParent);
76199        /* Assert that P3 is a valid memory cell. */
76200        assert( pOp->p3<=pFrame->nMem );
76201        pMem = &pFrame->aMem[pOp->p3];
76202      }else{
76203        /* Assert that P3 is a valid memory cell. */
76204        assert( pOp->p3<=(p->nMem-p->nCursor) );
76205        pMem = &aMem[pOp->p3];
76206        memAboutToChange(p, pMem);
76207      }
76208      assert( memIsValid(pMem) );
76209
76210      REGISTER_TRACE(pOp->p3, pMem);
76211      sqlite3VdbeMemIntegerify(pMem);
76212      assert( (pMem->flags & MEM_Int)!=0 );  /* mem(P3) holds an integer */
76213      if( pMem->u.i==MAX_ROWID || pC->useRandomRowid ){
76214        rc = SQLITE_FULL;   /* IMP: R-12275-61338 */
76215        goto abort_due_to_error;
76216      }
76217      if( v<pMem->u.i+1 ){
76218        v = pMem->u.i + 1;
76219      }
76220      pMem->u.i = v;
76221    }
76222#endif
76223    if( pC->useRandomRowid ){
76224      /* IMPLEMENTATION-OF: R-07677-41881 If the largest ROWID is equal to the
76225      ** largest possible integer (9223372036854775807) then the database
76226      ** engine starts picking positive candidate ROWIDs at random until
76227      ** it finds one that is not previously used. */
76228      assert( pOp->p3==0 );  /* We cannot be in random rowid mode if this is
76229                             ** an AUTOINCREMENT table. */
76230      cnt = 0;
76231      do{
76232        sqlite3_randomness(sizeof(v), &v);
76233        v &= (MAX_ROWID>>1); v++;  /* Ensure that v is greater than zero */
76234      }while(  ((rc = sqlite3BtreeMovetoUnpacked(pC->pCursor, 0, (u64)v,
76235                                                 0, &res))==SQLITE_OK)
76236            && (res==0)
76237            && (++cnt<100));
76238      if( rc==SQLITE_OK && res==0 ){
76239        rc = SQLITE_FULL;   /* IMP: R-38219-53002 */
76240        goto abort_due_to_error;
76241      }
76242      assert( v>0 );  /* EV: R-40812-03570 */
76243    }
76244    pC->deferredMoveto = 0;
76245    pC->cacheStatus = CACHE_STALE;
76246  }
76247  pOut->u.i = v;
76248  break;
76249}
76250
76251/* Opcode: Insert P1 P2 P3 P4 P5
76252** Synopsis: intkey=r[P3] data=r[P2]
76253**
76254** Write an entry into the table of cursor P1.  A new entry is
76255** created if it doesn't already exist or the data for an existing
76256** entry is overwritten.  The data is the value MEM_Blob stored in register
76257** number P2. The key is stored in register P3. The key must
76258** be a MEM_Int.
76259**
76260** If the OPFLAG_NCHANGE flag of P5 is set, then the row change count is
76261** incremented (otherwise not).  If the OPFLAG_LASTROWID flag of P5 is set,
76262** then rowid is stored for subsequent return by the
76263** sqlite3_last_insert_rowid() function (otherwise it is unmodified).
76264**
76265** If the OPFLAG_USESEEKRESULT flag of P5 is set and if the result of
76266** the last seek operation (OP_NotExists) was a success, then this
76267** operation will not attempt to find the appropriate row before doing
76268** the insert but will instead overwrite the row that the cursor is
76269** currently pointing to.  Presumably, the prior OP_NotExists opcode
76270** has already positioned the cursor correctly.  This is an optimization
76271** that boosts performance by avoiding redundant seeks.
76272**
76273** If the OPFLAG_ISUPDATE flag is set, then this opcode is part of an
76274** UPDATE operation.  Otherwise (if the flag is clear) then this opcode
76275** is part of an INSERT operation.  The difference is only important to
76276** the update hook.
76277**
76278** Parameter P4 may point to a string containing the table-name, or
76279** may be NULL. If it is not NULL, then the update-hook
76280** (sqlite3.xUpdateCallback) is invoked following a successful insert.
76281**
76282** (WARNING/TODO: If P1 is a pseudo-cursor and P2 is dynamically
76283** allocated, then ownership of P2 is transferred to the pseudo-cursor
76284** and register P2 becomes ephemeral.  If the cursor is changed, the
76285** value of register P2 will then change.  Make sure this does not
76286** cause any problems.)
76287**
76288** This instruction only works on tables.  The equivalent instruction
76289** for indices is OP_IdxInsert.
76290*/
76291/* Opcode: InsertInt P1 P2 P3 P4 P5
76292** Synopsis:  intkey=P3 data=r[P2]
76293**
76294** This works exactly like OP_Insert except that the key is the
76295** integer value P3, not the value of the integer stored in register P3.
76296*/
76297case OP_Insert:
76298case OP_InsertInt: {
76299  Mem *pData;       /* MEM cell holding data for the record to be inserted */
76300  Mem *pKey;        /* MEM cell holding key  for the record */
76301  i64 iKey;         /* The integer ROWID or key for the record to be inserted */
76302  VdbeCursor *pC;   /* Cursor to table into which insert is written */
76303  int nZero;        /* Number of zero-bytes to append */
76304  int seekResult;   /* Result of prior seek or 0 if no USESEEKRESULT flag */
76305  const char *zDb;  /* database name - used by the update hook */
76306  const char *zTbl; /* Table name - used by the opdate hook */
76307  int op;           /* Opcode for update hook: SQLITE_UPDATE or SQLITE_INSERT */
76308
76309  pData = &aMem[pOp->p2];
76310  assert( pOp->p1>=0 && pOp->p1<p->nCursor );
76311  assert( memIsValid(pData) );
76312  pC = p->apCsr[pOp->p1];
76313  assert( pC!=0 );
76314  assert( pC->pCursor!=0 );
76315  assert( pC->pseudoTableReg==0 );
76316  assert( pC->isTable );
76317  REGISTER_TRACE(pOp->p2, pData);
76318
76319  if( pOp->opcode==OP_Insert ){
76320    pKey = &aMem[pOp->p3];
76321    assert( pKey->flags & MEM_Int );
76322    assert( memIsValid(pKey) );
76323    REGISTER_TRACE(pOp->p3, pKey);
76324    iKey = pKey->u.i;
76325  }else{
76326    assert( pOp->opcode==OP_InsertInt );
76327    iKey = pOp->p3;
76328  }
76329
76330  if( pOp->p5 & OPFLAG_NCHANGE ) p->nChange++;
76331  if( pOp->p5 & OPFLAG_LASTROWID ) db->lastRowid = lastRowid = iKey;
76332  if( pData->flags & MEM_Null ){
76333    pData->z = 0;
76334    pData->n = 0;
76335  }else{
76336    assert( pData->flags & (MEM_Blob|MEM_Str) );
76337  }
76338  seekResult = ((pOp->p5 & OPFLAG_USESEEKRESULT) ? pC->seekResult : 0);
76339  if( pData->flags & MEM_Zero ){
76340    nZero = pData->u.nZero;
76341  }else{
76342    nZero = 0;
76343  }
76344  rc = sqlite3BtreeInsert(pC->pCursor, 0, iKey,
76345                          pData->z, pData->n, nZero,
76346                          (pOp->p5 & OPFLAG_APPEND)!=0, seekResult
76347  );
76348  pC->deferredMoveto = 0;
76349  pC->cacheStatus = CACHE_STALE;
76350
76351  /* Invoke the update-hook if required. */
76352  if( rc==SQLITE_OK && db->xUpdateCallback && pOp->p4.z ){
76353    zDb = db->aDb[pC->iDb].zName;
76354    zTbl = pOp->p4.z;
76355    op = ((pOp->p5 & OPFLAG_ISUPDATE) ? SQLITE_UPDATE : SQLITE_INSERT);
76356    assert( pC->isTable );
76357    db->xUpdateCallback(db->pUpdateArg, op, zDb, zTbl, iKey);
76358    assert( pC->iDb>=0 );
76359  }
76360  break;
76361}
76362
76363/* Opcode: Delete P1 P2 * P4 *
76364**
76365** Delete the record at which the P1 cursor is currently pointing.
76366**
76367** The cursor will be left pointing at either the next or the previous
76368** record in the table. If it is left pointing at the next record, then
76369** the next Next instruction will be a no-op.  Hence it is OK to delete
76370** a record from within a Next loop.
76371**
76372** If the OPFLAG_NCHANGE flag of P2 is set, then the row change count is
76373** incremented (otherwise not).
76374**
76375** P1 must not be pseudo-table.  It has to be a real table with
76376** multiple rows.
76377**
76378** If P4 is not NULL, then it is the name of the table that P1 is
76379** pointing to.  The update hook will be invoked, if it exists.
76380** If P4 is not NULL then the P1 cursor must have been positioned
76381** using OP_NotFound prior to invoking this opcode.
76382*/
76383case OP_Delete: {
76384  VdbeCursor *pC;
76385
76386  assert( pOp->p1>=0 && pOp->p1<p->nCursor );
76387  pC = p->apCsr[pOp->p1];
76388  assert( pC!=0 );
76389  assert( pC->pCursor!=0 );  /* Only valid for real tables, no pseudotables */
76390  assert( pC->deferredMoveto==0 );
76391
76392#ifdef SQLITE_DEBUG
76393  /* The seek operation that positioned the cursor prior to OP_Delete will
76394  ** have also set the pC->movetoTarget field to the rowid of the row that
76395  ** is being deleted */
76396  if( pOp->p4.z && pC->isTable ){
76397    i64 iKey = 0;
76398    sqlite3BtreeKeySize(pC->pCursor, &iKey);
76399    assert( pC->movetoTarget==iKey );
76400  }
76401#endif
76402
76403  rc = sqlite3BtreeDelete(pC->pCursor);
76404  pC->cacheStatus = CACHE_STALE;
76405
76406  /* Invoke the update-hook if required. */
76407  if( rc==SQLITE_OK && db->xUpdateCallback && pOp->p4.z && pC->isTable ){
76408    db->xUpdateCallback(db->pUpdateArg, SQLITE_DELETE,
76409                        db->aDb[pC->iDb].zName, pOp->p4.z, pC->movetoTarget);
76410    assert( pC->iDb>=0 );
76411  }
76412  if( pOp->p2 & OPFLAG_NCHANGE ) p->nChange++;
76413  break;
76414}
76415/* Opcode: ResetCount * * * * *
76416**
76417** The value of the change counter is copied to the database handle
76418** change counter (returned by subsequent calls to sqlite3_changes()).
76419** Then the VMs internal change counter resets to 0.
76420** This is used by trigger programs.
76421*/
76422case OP_ResetCount: {
76423  sqlite3VdbeSetChanges(db, p->nChange);
76424  p->nChange = 0;
76425  break;
76426}
76427
76428/* Opcode: SorterCompare P1 P2 P3 P4
76429** Synopsis:  if key(P1)!=trim(r[P3],P4) goto P2
76430**
76431** P1 is a sorter cursor. This instruction compares a prefix of the
76432** record blob in register P3 against a prefix of the entry that
76433** the sorter cursor currently points to.  Only the first P4 fields
76434** of r[P3] and the sorter record are compared.
76435**
76436** If either P3 or the sorter contains a NULL in one of their significant
76437** fields (not counting the P4 fields at the end which are ignored) then
76438** the comparison is assumed to be equal.
76439**
76440** Fall through to next instruction if the two records compare equal to
76441** each other.  Jump to P2 if they are different.
76442*/
76443case OP_SorterCompare: {
76444  VdbeCursor *pC;
76445  int res;
76446  int nKeyCol;
76447
76448  pC = p->apCsr[pOp->p1];
76449  assert( isSorter(pC) );
76450  assert( pOp->p4type==P4_INT32 );
76451  pIn3 = &aMem[pOp->p3];
76452  nKeyCol = pOp->p4.i;
76453  res = 0;
76454  rc = sqlite3VdbeSorterCompare(pC, pIn3, nKeyCol, &res);
76455  VdbeBranchTaken(res!=0,2);
76456  if( res ) goto jump_to_p2;
76457  break;
76458};
76459
76460/* Opcode: SorterData P1 P2 P3 * *
76461** Synopsis: r[P2]=data
76462**
76463** Write into register P2 the current sorter data for sorter cursor P1.
76464** Then clear the column header cache on cursor P3.
76465**
76466** This opcode is normally use to move a record out of the sorter and into
76467** a register that is the source for a pseudo-table cursor created using
76468** OpenPseudo.  That pseudo-table cursor is the one that is identified by
76469** parameter P3.  Clearing the P3 column cache as part of this opcode saves
76470** us from having to issue a separate NullRow instruction to clear that cache.
76471*/
76472case OP_SorterData: {
76473  VdbeCursor *pC;
76474
76475  pOut = &aMem[pOp->p2];
76476  pC = p->apCsr[pOp->p1];
76477  assert( isSorter(pC) );
76478  rc = sqlite3VdbeSorterRowkey(pC, pOut);
76479  assert( rc!=SQLITE_OK || (pOut->flags & MEM_Blob) );
76480  assert( pOp->p1>=0 && pOp->p1<p->nCursor );
76481  p->apCsr[pOp->p3]->cacheStatus = CACHE_STALE;
76482  break;
76483}
76484
76485/* Opcode: RowData P1 P2 * * *
76486** Synopsis: r[P2]=data
76487**
76488** Write into register P2 the complete row data for cursor P1.
76489** There is no interpretation of the data.
76490** It is just copied onto the P2 register exactly as
76491** it is found in the database file.
76492**
76493** If the P1 cursor must be pointing to a valid row (not a NULL row)
76494** of a real table, not a pseudo-table.
76495*/
76496/* Opcode: RowKey P1 P2 * * *
76497** Synopsis: r[P2]=key
76498**
76499** Write into register P2 the complete row key for cursor P1.
76500** There is no interpretation of the data.
76501** The key is copied onto the P2 register exactly as
76502** it is found in the database file.
76503**
76504** If the P1 cursor must be pointing to a valid row (not a NULL row)
76505** of a real table, not a pseudo-table.
76506*/
76507case OP_RowKey:
76508case OP_RowData: {
76509  VdbeCursor *pC;
76510  BtCursor *pCrsr;
76511  u32 n;
76512  i64 n64;
76513
76514  pOut = &aMem[pOp->p2];
76515  memAboutToChange(p, pOut);
76516
76517  /* Note that RowKey and RowData are really exactly the same instruction */
76518  assert( pOp->p1>=0 && pOp->p1<p->nCursor );
76519  pC = p->apCsr[pOp->p1];
76520  assert( isSorter(pC)==0 );
76521  assert( pC->isTable || pOp->opcode!=OP_RowData );
76522  assert( pC->isTable==0 || pOp->opcode==OP_RowData );
76523  assert( pC!=0 );
76524  assert( pC->nullRow==0 );
76525  assert( pC->pseudoTableReg==0 );
76526  assert( pC->pCursor!=0 );
76527  pCrsr = pC->pCursor;
76528
76529  /* The OP_RowKey and OP_RowData opcodes always follow OP_NotExists or
76530  ** OP_Rewind/Op_Next with no intervening instructions that might invalidate
76531  ** the cursor.  If this where not the case, on of the following assert()s
76532  ** would fail.  Should this ever change (because of changes in the code
76533  ** generator) then the fix would be to insert a call to
76534  ** sqlite3VdbeCursorMoveto().
76535  */
76536  assert( pC->deferredMoveto==0 );
76537  assert( sqlite3BtreeCursorIsValid(pCrsr) );
76538#if 0  /* Not required due to the previous to assert() statements */
76539  rc = sqlite3VdbeCursorMoveto(pC);
76540  if( rc!=SQLITE_OK ) goto abort_due_to_error;
76541#endif
76542
76543  if( pC->isTable==0 ){
76544    assert( !pC->isTable );
76545    VVA_ONLY(rc =) sqlite3BtreeKeySize(pCrsr, &n64);
76546    assert( rc==SQLITE_OK );    /* True because of CursorMoveto() call above */
76547    if( n64>db->aLimit[SQLITE_LIMIT_LENGTH] ){
76548      goto too_big;
76549    }
76550    n = (u32)n64;
76551  }else{
76552    VVA_ONLY(rc =) sqlite3BtreeDataSize(pCrsr, &n);
76553    assert( rc==SQLITE_OK );    /* DataSize() cannot fail */
76554    if( n>(u32)db->aLimit[SQLITE_LIMIT_LENGTH] ){
76555      goto too_big;
76556    }
76557  }
76558  testcase( n==0 );
76559  if( sqlite3VdbeMemClearAndResize(pOut, MAX(n,32)) ){
76560    goto no_mem;
76561  }
76562  pOut->n = n;
76563  MemSetTypeFlag(pOut, MEM_Blob);
76564  if( pC->isTable==0 ){
76565    rc = sqlite3BtreeKey(pCrsr, 0, n, pOut->z);
76566  }else{
76567    rc = sqlite3BtreeData(pCrsr, 0, n, pOut->z);
76568  }
76569  pOut->enc = SQLITE_UTF8;  /* In case the blob is ever cast to text */
76570  UPDATE_MAX_BLOBSIZE(pOut);
76571  REGISTER_TRACE(pOp->p2, pOut);
76572  break;
76573}
76574
76575/* Opcode: Rowid P1 P2 * * *
76576** Synopsis: r[P2]=rowid
76577**
76578** Store in register P2 an integer which is the key of the table entry that
76579** P1 is currently point to.
76580**
76581** P1 can be either an ordinary table or a virtual table.  There used to
76582** be a separate OP_VRowid opcode for use with virtual tables, but this
76583** one opcode now works for both table types.
76584*/
76585case OP_Rowid: {                 /* out2 */
76586  VdbeCursor *pC;
76587  i64 v;
76588  sqlite3_vtab *pVtab;
76589  const sqlite3_module *pModule;
76590
76591  pOut = out2Prerelease(p, pOp);
76592  assert( pOp->p1>=0 && pOp->p1<p->nCursor );
76593  pC = p->apCsr[pOp->p1];
76594  assert( pC!=0 );
76595  assert( pC->pseudoTableReg==0 || pC->nullRow );
76596  if( pC->nullRow ){
76597    pOut->flags = MEM_Null;
76598    break;
76599  }else if( pC->deferredMoveto ){
76600    v = pC->movetoTarget;
76601#ifndef SQLITE_OMIT_VIRTUALTABLE
76602  }else if( pC->pVtabCursor ){
76603    pVtab = pC->pVtabCursor->pVtab;
76604    pModule = pVtab->pModule;
76605    assert( pModule->xRowid );
76606    rc = pModule->xRowid(pC->pVtabCursor, &v);
76607    sqlite3VtabImportErrmsg(p, pVtab);
76608#endif /* SQLITE_OMIT_VIRTUALTABLE */
76609  }else{
76610    assert( pC->pCursor!=0 );
76611    rc = sqlite3VdbeCursorRestore(pC);
76612    if( rc ) goto abort_due_to_error;
76613    if( pC->nullRow ){
76614      pOut->flags = MEM_Null;
76615      break;
76616    }
76617    rc = sqlite3BtreeKeySize(pC->pCursor, &v);
76618    assert( rc==SQLITE_OK );  /* Always so because of CursorRestore() above */
76619  }
76620  pOut->u.i = v;
76621  break;
76622}
76623
76624/* Opcode: NullRow P1 * * * *
76625**
76626** Move the cursor P1 to a null row.  Any OP_Column operations
76627** that occur while the cursor is on the null row will always
76628** write a NULL.
76629*/
76630case OP_NullRow: {
76631  VdbeCursor *pC;
76632
76633  assert( pOp->p1>=0 && pOp->p1<p->nCursor );
76634  pC = p->apCsr[pOp->p1];
76635  assert( pC!=0 );
76636  pC->nullRow = 1;
76637  pC->cacheStatus = CACHE_STALE;
76638  if( pC->pCursor ){
76639    sqlite3BtreeClearCursor(pC->pCursor);
76640  }
76641  break;
76642}
76643
76644/* Opcode: Last P1 P2 P3 * *
76645**
76646** The next use of the Rowid or Column or Prev instruction for P1
76647** will refer to the last entry in the database table or index.
76648** If the table or index is empty and P2>0, then jump immediately to P2.
76649** If P2 is 0 or if the table or index is not empty, fall through
76650** to the following instruction.
76651**
76652** This opcode leaves the cursor configured to move in reverse order,
76653** from the end toward the beginning.  In other words, the cursor is
76654** configured to use Prev, not Next.
76655*/
76656case OP_Last: {        /* jump */
76657  VdbeCursor *pC;
76658  BtCursor *pCrsr;
76659  int res;
76660
76661  assert( pOp->p1>=0 && pOp->p1<p->nCursor );
76662  pC = p->apCsr[pOp->p1];
76663  assert( pC!=0 );
76664  pCrsr = pC->pCursor;
76665  res = 0;
76666  assert( pCrsr!=0 );
76667  rc = sqlite3BtreeLast(pCrsr, &res);
76668  pC->nullRow = (u8)res;
76669  pC->deferredMoveto = 0;
76670  pC->cacheStatus = CACHE_STALE;
76671  pC->seekResult = pOp->p3;
76672#ifdef SQLITE_DEBUG
76673  pC->seekOp = OP_Last;
76674#endif
76675  if( pOp->p2>0 ){
76676    VdbeBranchTaken(res!=0,2);
76677    if( res ) goto jump_to_p2;
76678  }
76679  break;
76680}
76681
76682
76683/* Opcode: Sort P1 P2 * * *
76684**
76685** This opcode does exactly the same thing as OP_Rewind except that
76686** it increments an undocumented global variable used for testing.
76687**
76688** Sorting is accomplished by writing records into a sorting index,
76689** then rewinding that index and playing it back from beginning to
76690** end.  We use the OP_Sort opcode instead of OP_Rewind to do the
76691** rewinding so that the global variable will be incremented and
76692** regression tests can determine whether or not the optimizer is
76693** correctly optimizing out sorts.
76694*/
76695case OP_SorterSort:    /* jump */
76696case OP_Sort: {        /* jump */
76697#ifdef SQLITE_TEST
76698  sqlite3_sort_count++;
76699  sqlite3_search_count--;
76700#endif
76701  p->aCounter[SQLITE_STMTSTATUS_SORT]++;
76702  /* Fall through into OP_Rewind */
76703}
76704/* Opcode: Rewind P1 P2 * * *
76705**
76706** The next use of the Rowid or Column or Next instruction for P1
76707** will refer to the first entry in the database table or index.
76708** If the table or index is empty, jump immediately to P2.
76709** If the table or index is not empty, fall through to the following
76710** instruction.
76711**
76712** This opcode leaves the cursor configured to move in forward order,
76713** from the beginning toward the end.  In other words, the cursor is
76714** configured to use Next, not Prev.
76715*/
76716case OP_Rewind: {        /* jump */
76717  VdbeCursor *pC;
76718  BtCursor *pCrsr;
76719  int res;
76720
76721  assert( pOp->p1>=0 && pOp->p1<p->nCursor );
76722  pC = p->apCsr[pOp->p1];
76723  assert( pC!=0 );
76724  assert( isSorter(pC)==(pOp->opcode==OP_SorterSort) );
76725  res = 1;
76726#ifdef SQLITE_DEBUG
76727  pC->seekOp = OP_Rewind;
76728#endif
76729  if( isSorter(pC) ){
76730    rc = sqlite3VdbeSorterRewind(pC, &res);
76731  }else{
76732    pCrsr = pC->pCursor;
76733    assert( pCrsr );
76734    rc = sqlite3BtreeFirst(pCrsr, &res);
76735    pC->deferredMoveto = 0;
76736    pC->cacheStatus = CACHE_STALE;
76737  }
76738  pC->nullRow = (u8)res;
76739  assert( pOp->p2>0 && pOp->p2<p->nOp );
76740  VdbeBranchTaken(res!=0,2);
76741  if( res ) goto jump_to_p2;
76742  break;
76743}
76744
76745/* Opcode: Next P1 P2 P3 P4 P5
76746**
76747** Advance cursor P1 so that it points to the next key/data pair in its
76748** table or index.  If there are no more key/value pairs then fall through
76749** to the following instruction.  But if the cursor advance was successful,
76750** jump immediately to P2.
76751**
76752** The Next opcode is only valid following an SeekGT, SeekGE, or
76753** OP_Rewind opcode used to position the cursor.  Next is not allowed
76754** to follow SeekLT, SeekLE, or OP_Last.
76755**
76756** The P1 cursor must be for a real table, not a pseudo-table.  P1 must have
76757** been opened prior to this opcode or the program will segfault.
76758**
76759** The P3 value is a hint to the btree implementation. If P3==1, that
76760** means P1 is an SQL index and that this instruction could have been
76761** omitted if that index had been unique.  P3 is usually 0.  P3 is
76762** always either 0 or 1.
76763**
76764** P4 is always of type P4_ADVANCE. The function pointer points to
76765** sqlite3BtreeNext().
76766**
76767** If P5 is positive and the jump is taken, then event counter
76768** number P5-1 in the prepared statement is incremented.
76769**
76770** See also: Prev, NextIfOpen
76771*/
76772/* Opcode: NextIfOpen P1 P2 P3 P4 P5
76773**
76774** This opcode works just like Next except that if cursor P1 is not
76775** open it behaves a no-op.
76776*/
76777/* Opcode: Prev P1 P2 P3 P4 P5
76778**
76779** Back up cursor P1 so that it points to the previous key/data pair in its
76780** table or index.  If there is no previous key/value pairs then fall through
76781** to the following instruction.  But if the cursor backup was successful,
76782** jump immediately to P2.
76783**
76784**
76785** The Prev opcode is only valid following an SeekLT, SeekLE, or
76786** OP_Last opcode used to position the cursor.  Prev is not allowed
76787** to follow SeekGT, SeekGE, or OP_Rewind.
76788**
76789** The P1 cursor must be for a real table, not a pseudo-table.  If P1 is
76790** not open then the behavior is undefined.
76791**
76792** The P3 value is a hint to the btree implementation. If P3==1, that
76793** means P1 is an SQL index and that this instruction could have been
76794** omitted if that index had been unique.  P3 is usually 0.  P3 is
76795** always either 0 or 1.
76796**
76797** P4 is always of type P4_ADVANCE. The function pointer points to
76798** sqlite3BtreePrevious().
76799**
76800** If P5 is positive and the jump is taken, then event counter
76801** number P5-1 in the prepared statement is incremented.
76802*/
76803/* Opcode: PrevIfOpen P1 P2 P3 P4 P5
76804**
76805** This opcode works just like Prev except that if cursor P1 is not
76806** open it behaves a no-op.
76807*/
76808case OP_SorterNext: {  /* jump */
76809  VdbeCursor *pC;
76810  int res;
76811
76812  pC = p->apCsr[pOp->p1];
76813  assert( isSorter(pC) );
76814  res = 0;
76815  rc = sqlite3VdbeSorterNext(db, pC, &res);
76816  goto next_tail;
76817case OP_PrevIfOpen:    /* jump */
76818case OP_NextIfOpen:    /* jump */
76819  if( p->apCsr[pOp->p1]==0 ) break;
76820  /* Fall through */
76821case OP_Prev:          /* jump */
76822case OP_Next:          /* jump */
76823  assert( pOp->p1>=0 && pOp->p1<p->nCursor );
76824  assert( pOp->p5<ArraySize(p->aCounter) );
76825  pC = p->apCsr[pOp->p1];
76826  res = pOp->p3;
76827  assert( pC!=0 );
76828  assert( pC->deferredMoveto==0 );
76829  assert( pC->pCursor );
76830  assert( res==0 || (res==1 && pC->isTable==0) );
76831  testcase( res==1 );
76832  assert( pOp->opcode!=OP_Next || pOp->p4.xAdvance==sqlite3BtreeNext );
76833  assert( pOp->opcode!=OP_Prev || pOp->p4.xAdvance==sqlite3BtreePrevious );
76834  assert( pOp->opcode!=OP_NextIfOpen || pOp->p4.xAdvance==sqlite3BtreeNext );
76835  assert( pOp->opcode!=OP_PrevIfOpen || pOp->p4.xAdvance==sqlite3BtreePrevious);
76836
76837  /* The Next opcode is only used after SeekGT, SeekGE, and Rewind.
76838  ** The Prev opcode is only used after SeekLT, SeekLE, and Last. */
76839  assert( pOp->opcode!=OP_Next || pOp->opcode!=OP_NextIfOpen
76840       || pC->seekOp==OP_SeekGT || pC->seekOp==OP_SeekGE
76841       || pC->seekOp==OP_Rewind || pC->seekOp==OP_Found);
76842  assert( pOp->opcode!=OP_Prev || pOp->opcode!=OP_PrevIfOpen
76843       || pC->seekOp==OP_SeekLT || pC->seekOp==OP_SeekLE
76844       || pC->seekOp==OP_Last );
76845
76846  rc = pOp->p4.xAdvance(pC->pCursor, &res);
76847next_tail:
76848  pC->cacheStatus = CACHE_STALE;
76849  VdbeBranchTaken(res==0,2);
76850  if( res==0 ){
76851    pC->nullRow = 0;
76852    p->aCounter[pOp->p5]++;
76853#ifdef SQLITE_TEST
76854    sqlite3_search_count++;
76855#endif
76856    goto jump_to_p2_and_check_for_interrupt;
76857  }else{
76858    pC->nullRow = 1;
76859  }
76860  goto check_for_interrupt;
76861}
76862
76863/* Opcode: IdxInsert P1 P2 P3 * P5
76864** Synopsis: key=r[P2]
76865**
76866** Register P2 holds an SQL index key made using the
76867** MakeRecord instructions.  This opcode writes that key
76868** into the index P1.  Data for the entry is nil.
76869**
76870** P3 is a flag that provides a hint to the b-tree layer that this
76871** insert is likely to be an append.
76872**
76873** If P5 has the OPFLAG_NCHANGE bit set, then the change counter is
76874** incremented by this instruction.  If the OPFLAG_NCHANGE bit is clear,
76875** then the change counter is unchanged.
76876**
76877** If P5 has the OPFLAG_USESEEKRESULT bit set, then the cursor must have
76878** just done a seek to the spot where the new entry is to be inserted.
76879** This flag avoids doing an extra seek.
76880**
76881** This instruction only works for indices.  The equivalent instruction
76882** for tables is OP_Insert.
76883*/
76884case OP_SorterInsert:       /* in2 */
76885case OP_IdxInsert: {        /* in2 */
76886  VdbeCursor *pC;
76887  int nKey;
76888  const char *zKey;
76889
76890  assert( pOp->p1>=0 && pOp->p1<p->nCursor );
76891  pC = p->apCsr[pOp->p1];
76892  assert( pC!=0 );
76893  assert( isSorter(pC)==(pOp->opcode==OP_SorterInsert) );
76894  pIn2 = &aMem[pOp->p2];
76895  assert( pIn2->flags & MEM_Blob );
76896  if( pOp->p5 & OPFLAG_NCHANGE ) p->nChange++;
76897  assert( pC->pCursor!=0 );
76898  assert( pC->isTable==0 );
76899  rc = ExpandBlob(pIn2);
76900  if( rc==SQLITE_OK ){
76901    if( pOp->opcode==OP_SorterInsert ){
76902      rc = sqlite3VdbeSorterWrite(pC, pIn2);
76903    }else{
76904      nKey = pIn2->n;
76905      zKey = pIn2->z;
76906      rc = sqlite3BtreeInsert(pC->pCursor, zKey, nKey, "", 0, 0, pOp->p3,
76907          ((pOp->p5 & OPFLAG_USESEEKRESULT) ? pC->seekResult : 0)
76908          );
76909      assert( pC->deferredMoveto==0 );
76910      pC->cacheStatus = CACHE_STALE;
76911    }
76912  }
76913  break;
76914}
76915
76916/* Opcode: IdxDelete P1 P2 P3 * *
76917** Synopsis: key=r[P2@P3]
76918**
76919** The content of P3 registers starting at register P2 form
76920** an unpacked index key. This opcode removes that entry from the
76921** index opened by cursor P1.
76922*/
76923case OP_IdxDelete: {
76924  VdbeCursor *pC;
76925  BtCursor *pCrsr;
76926  int res;
76927  UnpackedRecord r;
76928
76929  assert( pOp->p3>0 );
76930  assert( pOp->p2>0 && pOp->p2+pOp->p3<=(p->nMem-p->nCursor)+1 );
76931  assert( pOp->p1>=0 && pOp->p1<p->nCursor );
76932  pC = p->apCsr[pOp->p1];
76933  assert( pC!=0 );
76934  pCrsr = pC->pCursor;
76935  assert( pCrsr!=0 );
76936  assert( pOp->p5==0 );
76937  r.pKeyInfo = pC->pKeyInfo;
76938  r.nField = (u16)pOp->p3;
76939  r.default_rc = 0;
76940  r.aMem = &aMem[pOp->p2];
76941#ifdef SQLITE_DEBUG
76942  { int i; for(i=0; i<r.nField; i++) assert( memIsValid(&r.aMem[i]) ); }
76943#endif
76944  rc = sqlite3BtreeMovetoUnpacked(pCrsr, &r, 0, 0, &res);
76945  if( rc==SQLITE_OK && res==0 ){
76946    rc = sqlite3BtreeDelete(pCrsr);
76947  }
76948  assert( pC->deferredMoveto==0 );
76949  pC->cacheStatus = CACHE_STALE;
76950  break;
76951}
76952
76953/* Opcode: IdxRowid P1 P2 * * *
76954** Synopsis: r[P2]=rowid
76955**
76956** Write into register P2 an integer which is the last entry in the record at
76957** the end of the index key pointed to by cursor P1.  This integer should be
76958** the rowid of the table entry to which this index entry points.
76959**
76960** See also: Rowid, MakeRecord.
76961*/
76962case OP_IdxRowid: {              /* out2 */
76963  BtCursor *pCrsr;
76964  VdbeCursor *pC;
76965  i64 rowid;
76966
76967  pOut = out2Prerelease(p, pOp);
76968  assert( pOp->p1>=0 && pOp->p1<p->nCursor );
76969  pC = p->apCsr[pOp->p1];
76970  assert( pC!=0 );
76971  pCrsr = pC->pCursor;
76972  assert( pCrsr!=0 );
76973  pOut->flags = MEM_Null;
76974  assert( pC->isTable==0 );
76975  assert( pC->deferredMoveto==0 );
76976
76977  /* sqlite3VbeCursorRestore() can only fail if the record has been deleted
76978  ** out from under the cursor.  That will never happend for an IdxRowid
76979  ** opcode, hence the NEVER() arround the check of the return value.
76980  */
76981  rc = sqlite3VdbeCursorRestore(pC);
76982  if( NEVER(rc!=SQLITE_OK) ) goto abort_due_to_error;
76983
76984  if( !pC->nullRow ){
76985    rowid = 0;  /* Not needed.  Only used to silence a warning. */
76986    rc = sqlite3VdbeIdxRowid(db, pCrsr, &rowid);
76987    if( rc!=SQLITE_OK ){
76988      goto abort_due_to_error;
76989    }
76990    pOut->u.i = rowid;
76991    pOut->flags = MEM_Int;
76992  }
76993  break;
76994}
76995
76996/* Opcode: IdxGE P1 P2 P3 P4 P5
76997** Synopsis: key=r[P3@P4]
76998**
76999** The P4 register values beginning with P3 form an unpacked index
77000** key that omits the PRIMARY KEY.  Compare this key value against the index
77001** that P1 is currently pointing to, ignoring the PRIMARY KEY or ROWID
77002** fields at the end.
77003**
77004** If the P1 index entry is greater than or equal to the key value
77005** then jump to P2.  Otherwise fall through to the next instruction.
77006*/
77007/* Opcode: IdxGT P1 P2 P3 P4 P5
77008** Synopsis: key=r[P3@P4]
77009**
77010** The P4 register values beginning with P3 form an unpacked index
77011** key that omits the PRIMARY KEY.  Compare this key value against the index
77012** that P1 is currently pointing to, ignoring the PRIMARY KEY or ROWID
77013** fields at the end.
77014**
77015** If the P1 index entry is greater than the key value
77016** then jump to P2.  Otherwise fall through to the next instruction.
77017*/
77018/* Opcode: IdxLT P1 P2 P3 P4 P5
77019** Synopsis: key=r[P3@P4]
77020**
77021** The P4 register values beginning with P3 form an unpacked index
77022** key that omits the PRIMARY KEY or ROWID.  Compare this key value against
77023** the index that P1 is currently pointing to, ignoring the PRIMARY KEY or
77024** ROWID on the P1 index.
77025**
77026** If the P1 index entry is less than the key value then jump to P2.
77027** Otherwise fall through to the next instruction.
77028*/
77029/* Opcode: IdxLE P1 P2 P3 P4 P5
77030** Synopsis: key=r[P3@P4]
77031**
77032** The P4 register values beginning with P3 form an unpacked index
77033** key that omits the PRIMARY KEY or ROWID.  Compare this key value against
77034** the index that P1 is currently pointing to, ignoring the PRIMARY KEY or
77035** ROWID on the P1 index.
77036**
77037** If the P1 index entry is less than or equal to the key value then jump
77038** to P2. Otherwise fall through to the next instruction.
77039*/
77040case OP_IdxLE:          /* jump */
77041case OP_IdxGT:          /* jump */
77042case OP_IdxLT:          /* jump */
77043case OP_IdxGE:  {       /* jump */
77044  VdbeCursor *pC;
77045  int res;
77046  UnpackedRecord r;
77047
77048  assert( pOp->p1>=0 && pOp->p1<p->nCursor );
77049  pC = p->apCsr[pOp->p1];
77050  assert( pC!=0 );
77051  assert( pC->isOrdered );
77052  assert( pC->pCursor!=0);
77053  assert( pC->deferredMoveto==0 );
77054  assert( pOp->p5==0 || pOp->p5==1 );
77055  assert( pOp->p4type==P4_INT32 );
77056  r.pKeyInfo = pC->pKeyInfo;
77057  r.nField = (u16)pOp->p4.i;
77058  if( pOp->opcode<OP_IdxLT ){
77059    assert( pOp->opcode==OP_IdxLE || pOp->opcode==OP_IdxGT );
77060    r.default_rc = -1;
77061  }else{
77062    assert( pOp->opcode==OP_IdxGE || pOp->opcode==OP_IdxLT );
77063    r.default_rc = 0;
77064  }
77065  r.aMem = &aMem[pOp->p3];
77066#ifdef SQLITE_DEBUG
77067  { int i; for(i=0; i<r.nField; i++) assert( memIsValid(&r.aMem[i]) ); }
77068#endif
77069  res = 0;  /* Not needed.  Only used to silence a warning. */
77070  rc = sqlite3VdbeIdxKeyCompare(db, pC, &r, &res);
77071  assert( (OP_IdxLE&1)==(OP_IdxLT&1) && (OP_IdxGE&1)==(OP_IdxGT&1) );
77072  if( (pOp->opcode&1)==(OP_IdxLT&1) ){
77073    assert( pOp->opcode==OP_IdxLE || pOp->opcode==OP_IdxLT );
77074    res = -res;
77075  }else{
77076    assert( pOp->opcode==OP_IdxGE || pOp->opcode==OP_IdxGT );
77077    res++;
77078  }
77079  VdbeBranchTaken(res>0,2);
77080  if( res>0 ) goto jump_to_p2;
77081  break;
77082}
77083
77084/* Opcode: Destroy P1 P2 P3 * *
77085**
77086** Delete an entire database table or index whose root page in the database
77087** file is given by P1.
77088**
77089** The table being destroyed is in the main database file if P3==0.  If
77090** P3==1 then the table to be clear is in the auxiliary database file
77091** that is used to store tables create using CREATE TEMPORARY TABLE.
77092**
77093** If AUTOVACUUM is enabled then it is possible that another root page
77094** might be moved into the newly deleted root page in order to keep all
77095** root pages contiguous at the beginning of the database.  The former
77096** value of the root page that moved - its value before the move occurred -
77097** is stored in register P2.  If no page
77098** movement was required (because the table being dropped was already
77099** the last one in the database) then a zero is stored in register P2.
77100** If AUTOVACUUM is disabled then a zero is stored in register P2.
77101**
77102** See also: Clear
77103*/
77104case OP_Destroy: {     /* out2 */
77105  int iMoved;
77106  int iDb;
77107
77108  assert( p->readOnly==0 );
77109  pOut = out2Prerelease(p, pOp);
77110  pOut->flags = MEM_Null;
77111  if( db->nVdbeRead > db->nVDestroy+1 ){
77112    rc = SQLITE_LOCKED;
77113    p->errorAction = OE_Abort;
77114  }else{
77115    iDb = pOp->p3;
77116    assert( DbMaskTest(p->btreeMask, iDb) );
77117    iMoved = 0;  /* Not needed.  Only to silence a warning. */
77118    rc = sqlite3BtreeDropTable(db->aDb[iDb].pBt, pOp->p1, &iMoved);
77119    pOut->flags = MEM_Int;
77120    pOut->u.i = iMoved;
77121#ifndef SQLITE_OMIT_AUTOVACUUM
77122    if( rc==SQLITE_OK && iMoved!=0 ){
77123      sqlite3RootPageMoved(db, iDb, iMoved, pOp->p1);
77124      /* All OP_Destroy operations occur on the same btree */
77125      assert( resetSchemaOnFault==0 || resetSchemaOnFault==iDb+1 );
77126      resetSchemaOnFault = iDb+1;
77127    }
77128#endif
77129  }
77130  break;
77131}
77132
77133/* Opcode: Clear P1 P2 P3
77134**
77135** Delete all contents of the database table or index whose root page
77136** in the database file is given by P1.  But, unlike Destroy, do not
77137** remove the table or index from the database file.
77138**
77139** The table being clear is in the main database file if P2==0.  If
77140** P2==1 then the table to be clear is in the auxiliary database file
77141** that is used to store tables create using CREATE TEMPORARY TABLE.
77142**
77143** If the P3 value is non-zero, then the table referred to must be an
77144** intkey table (an SQL table, not an index). In this case the row change
77145** count is incremented by the number of rows in the table being cleared.
77146** If P3 is greater than zero, then the value stored in register P3 is
77147** also incremented by the number of rows in the table being cleared.
77148**
77149** See also: Destroy
77150*/
77151case OP_Clear: {
77152  int nChange;
77153
77154  nChange = 0;
77155  assert( p->readOnly==0 );
77156  assert( DbMaskTest(p->btreeMask, pOp->p2) );
77157  rc = sqlite3BtreeClearTable(
77158      db->aDb[pOp->p2].pBt, pOp->p1, (pOp->p3 ? &nChange : 0)
77159  );
77160  if( pOp->p3 ){
77161    p->nChange += nChange;
77162    if( pOp->p3>0 ){
77163      assert( memIsValid(&aMem[pOp->p3]) );
77164      memAboutToChange(p, &aMem[pOp->p3]);
77165      aMem[pOp->p3].u.i += nChange;
77166    }
77167  }
77168  break;
77169}
77170
77171/* Opcode: ResetSorter P1 * * * *
77172**
77173** Delete all contents from the ephemeral table or sorter
77174** that is open on cursor P1.
77175**
77176** This opcode only works for cursors used for sorting and
77177** opened with OP_OpenEphemeral or OP_SorterOpen.
77178*/
77179case OP_ResetSorter: {
77180  VdbeCursor *pC;
77181
77182  assert( pOp->p1>=0 && pOp->p1<p->nCursor );
77183  pC = p->apCsr[pOp->p1];
77184  assert( pC!=0 );
77185  if( pC->pSorter ){
77186    sqlite3VdbeSorterReset(db, pC->pSorter);
77187  }else{
77188    assert( pC->isEphemeral );
77189    rc = sqlite3BtreeClearTableOfCursor(pC->pCursor);
77190  }
77191  break;
77192}
77193
77194/* Opcode: CreateTable P1 P2 * * *
77195** Synopsis: r[P2]=root iDb=P1
77196**
77197** Allocate a new table in the main database file if P1==0 or in the
77198** auxiliary database file if P1==1 or in an attached database if
77199** P1>1.  Write the root page number of the new table into
77200** register P2
77201**
77202** The difference between a table and an index is this:  A table must
77203** have a 4-byte integer key and can have arbitrary data.  An index
77204** has an arbitrary key but no data.
77205**
77206** See also: CreateIndex
77207*/
77208/* Opcode: CreateIndex P1 P2 * * *
77209** Synopsis: r[P2]=root iDb=P1
77210**
77211** Allocate a new index in the main database file if P1==0 or in the
77212** auxiliary database file if P1==1 or in an attached database if
77213** P1>1.  Write the root page number of the new table into
77214** register P2.
77215**
77216** See documentation on OP_CreateTable for additional information.
77217*/
77218case OP_CreateIndex:            /* out2 */
77219case OP_CreateTable: {          /* out2 */
77220  int pgno;
77221  int flags;
77222  Db *pDb;
77223
77224  pOut = out2Prerelease(p, pOp);
77225  pgno = 0;
77226  assert( pOp->p1>=0 && pOp->p1<db->nDb );
77227  assert( DbMaskTest(p->btreeMask, pOp->p1) );
77228  assert( p->readOnly==0 );
77229  pDb = &db->aDb[pOp->p1];
77230  assert( pDb->pBt!=0 );
77231  if( pOp->opcode==OP_CreateTable ){
77232    /* flags = BTREE_INTKEY; */
77233    flags = BTREE_INTKEY;
77234  }else{
77235    flags = BTREE_BLOBKEY;
77236  }
77237  rc = sqlite3BtreeCreateTable(pDb->pBt, &pgno, flags);
77238  pOut->u.i = pgno;
77239  break;
77240}
77241
77242/* Opcode: ParseSchema P1 * * P4 *
77243**
77244** Read and parse all entries from the SQLITE_MASTER table of database P1
77245** that match the WHERE clause P4.
77246**
77247** This opcode invokes the parser to create a new virtual machine,
77248** then runs the new virtual machine.  It is thus a re-entrant opcode.
77249*/
77250case OP_ParseSchema: {
77251  int iDb;
77252  const char *zMaster;
77253  char *zSql;
77254  InitData initData;
77255
77256  /* Any prepared statement that invokes this opcode will hold mutexes
77257  ** on every btree.  This is a prerequisite for invoking
77258  ** sqlite3InitCallback().
77259  */
77260#ifdef SQLITE_DEBUG
77261  for(iDb=0; iDb<db->nDb; iDb++){
77262    assert( iDb==1 || sqlite3BtreeHoldsMutex(db->aDb[iDb].pBt) );
77263  }
77264#endif
77265
77266  iDb = pOp->p1;
77267  assert( iDb>=0 && iDb<db->nDb );
77268  assert( DbHasProperty(db, iDb, DB_SchemaLoaded) );
77269  /* Used to be a conditional */ {
77270    zMaster = SCHEMA_TABLE(iDb);
77271    initData.db = db;
77272    initData.iDb = pOp->p1;
77273    initData.pzErrMsg = &p->zErrMsg;
77274    zSql = sqlite3MPrintf(db,
77275       "SELECT name, rootpage, sql FROM '%q'.%s WHERE %s ORDER BY rowid",
77276       db->aDb[iDb].zName, zMaster, pOp->p4.z);
77277    if( zSql==0 ){
77278      rc = SQLITE_NOMEM;
77279    }else{
77280      assert( db->init.busy==0 );
77281      db->init.busy = 1;
77282      initData.rc = SQLITE_OK;
77283      assert( !db->mallocFailed );
77284      rc = sqlite3_exec(db, zSql, sqlite3InitCallback, &initData, 0);
77285      if( rc==SQLITE_OK ) rc = initData.rc;
77286      sqlite3DbFree(db, zSql);
77287      db->init.busy = 0;
77288    }
77289  }
77290  if( rc ) sqlite3ResetAllSchemasOfConnection(db);
77291  if( rc==SQLITE_NOMEM ){
77292    goto no_mem;
77293  }
77294  break;
77295}
77296
77297#if !defined(SQLITE_OMIT_ANALYZE)
77298/* Opcode: LoadAnalysis P1 * * * *
77299**
77300** Read the sqlite_stat1 table for database P1 and load the content
77301** of that table into the internal index hash table.  This will cause
77302** the analysis to be used when preparing all subsequent queries.
77303*/
77304case OP_LoadAnalysis: {
77305  assert( pOp->p1>=0 && pOp->p1<db->nDb );
77306  rc = sqlite3AnalysisLoad(db, pOp->p1);
77307  break;
77308}
77309#endif /* !defined(SQLITE_OMIT_ANALYZE) */
77310
77311/* Opcode: DropTable P1 * * P4 *
77312**
77313** Remove the internal (in-memory) data structures that describe
77314** the table named P4 in database P1.  This is called after a table
77315** is dropped from disk (using the Destroy opcode) in order to keep
77316** the internal representation of the
77317** schema consistent with what is on disk.
77318*/
77319case OP_DropTable: {
77320  sqlite3UnlinkAndDeleteTable(db, pOp->p1, pOp->p4.z);
77321  break;
77322}
77323
77324/* Opcode: DropIndex P1 * * P4 *
77325**
77326** Remove the internal (in-memory) data structures that describe
77327** the index named P4 in database P1.  This is called after an index
77328** is dropped from disk (using the Destroy opcode)
77329** in order to keep the internal representation of the
77330** schema consistent with what is on disk.
77331*/
77332case OP_DropIndex: {
77333  sqlite3UnlinkAndDeleteIndex(db, pOp->p1, pOp->p4.z);
77334  break;
77335}
77336
77337/* Opcode: DropTrigger P1 * * P4 *
77338**
77339** Remove the internal (in-memory) data structures that describe
77340** the trigger named P4 in database P1.  This is called after a trigger
77341** is dropped from disk (using the Destroy opcode) in order to keep
77342** the internal representation of the
77343** schema consistent with what is on disk.
77344*/
77345case OP_DropTrigger: {
77346  sqlite3UnlinkAndDeleteTrigger(db, pOp->p1, pOp->p4.z);
77347  break;
77348}
77349
77350
77351#ifndef SQLITE_OMIT_INTEGRITY_CHECK
77352/* Opcode: IntegrityCk P1 P2 P3 * P5
77353**
77354** Do an analysis of the currently open database.  Store in
77355** register P1 the text of an error message describing any problems.
77356** If no problems are found, store a NULL in register P1.
77357**
77358** The register P3 contains the maximum number of allowed errors.
77359** At most reg(P3) errors will be reported.
77360** In other words, the analysis stops as soon as reg(P1) errors are
77361** seen.  Reg(P1) is updated with the number of errors remaining.
77362**
77363** The root page numbers of all tables in the database are integer
77364** stored in reg(P1), reg(P1+1), reg(P1+2), ....  There are P2 tables
77365** total.
77366**
77367** If P5 is not zero, the check is done on the auxiliary database
77368** file, not the main database file.
77369**
77370** This opcode is used to implement the integrity_check pragma.
77371*/
77372case OP_IntegrityCk: {
77373  int nRoot;      /* Number of tables to check.  (Number of root pages.) */
77374  int *aRoot;     /* Array of rootpage numbers for tables to be checked */
77375  int j;          /* Loop counter */
77376  int nErr;       /* Number of errors reported */
77377  char *z;        /* Text of the error report */
77378  Mem *pnErr;     /* Register keeping track of errors remaining */
77379
77380  assert( p->bIsReader );
77381  nRoot = pOp->p2;
77382  assert( nRoot>0 );
77383  aRoot = sqlite3DbMallocRaw(db, sizeof(int)*(nRoot+1) );
77384  if( aRoot==0 ) goto no_mem;
77385  assert( pOp->p3>0 && pOp->p3<=(p->nMem-p->nCursor) );
77386  pnErr = &aMem[pOp->p3];
77387  assert( (pnErr->flags & MEM_Int)!=0 );
77388  assert( (pnErr->flags & (MEM_Str|MEM_Blob))==0 );
77389  pIn1 = &aMem[pOp->p1];
77390  for(j=0; j<nRoot; j++){
77391    aRoot[j] = (int)sqlite3VdbeIntValue(&pIn1[j]);
77392  }
77393  aRoot[j] = 0;
77394  assert( pOp->p5<db->nDb );
77395  assert( DbMaskTest(p->btreeMask, pOp->p5) );
77396  z = sqlite3BtreeIntegrityCheck(db->aDb[pOp->p5].pBt, aRoot, nRoot,
77397                                 (int)pnErr->u.i, &nErr);
77398  sqlite3DbFree(db, aRoot);
77399  pnErr->u.i -= nErr;
77400  sqlite3VdbeMemSetNull(pIn1);
77401  if( nErr==0 ){
77402    assert( z==0 );
77403  }else if( z==0 ){
77404    goto no_mem;
77405  }else{
77406    sqlite3VdbeMemSetStr(pIn1, z, -1, SQLITE_UTF8, sqlite3_free);
77407  }
77408  UPDATE_MAX_BLOBSIZE(pIn1);
77409  sqlite3VdbeChangeEncoding(pIn1, encoding);
77410  break;
77411}
77412#endif /* SQLITE_OMIT_INTEGRITY_CHECK */
77413
77414/* Opcode: RowSetAdd P1 P2 * * *
77415** Synopsis:  rowset(P1)=r[P2]
77416**
77417** Insert the integer value held by register P2 into a boolean index
77418** held in register P1.
77419**
77420** An assertion fails if P2 is not an integer.
77421*/
77422case OP_RowSetAdd: {       /* in1, in2 */
77423  pIn1 = &aMem[pOp->p1];
77424  pIn2 = &aMem[pOp->p2];
77425  assert( (pIn2->flags & MEM_Int)!=0 );
77426  if( (pIn1->flags & MEM_RowSet)==0 ){
77427    sqlite3VdbeMemSetRowSet(pIn1);
77428    if( (pIn1->flags & MEM_RowSet)==0 ) goto no_mem;
77429  }
77430  sqlite3RowSetInsert(pIn1->u.pRowSet, pIn2->u.i);
77431  break;
77432}
77433
77434/* Opcode: RowSetRead P1 P2 P3 * *
77435** Synopsis:  r[P3]=rowset(P1)
77436**
77437** Extract the smallest value from boolean index P1 and put that value into
77438** register P3.  Or, if boolean index P1 is initially empty, leave P3
77439** unchanged and jump to instruction P2.
77440*/
77441case OP_RowSetRead: {       /* jump, in1, out3 */
77442  i64 val;
77443
77444  pIn1 = &aMem[pOp->p1];
77445  if( (pIn1->flags & MEM_RowSet)==0
77446   || sqlite3RowSetNext(pIn1->u.pRowSet, &val)==0
77447  ){
77448    /* The boolean index is empty */
77449    sqlite3VdbeMemSetNull(pIn1);
77450    VdbeBranchTaken(1,2);
77451    goto jump_to_p2_and_check_for_interrupt;
77452  }else{
77453    /* A value was pulled from the index */
77454    VdbeBranchTaken(0,2);
77455    sqlite3VdbeMemSetInt64(&aMem[pOp->p3], val);
77456  }
77457  goto check_for_interrupt;
77458}
77459
77460/* Opcode: RowSetTest P1 P2 P3 P4
77461** Synopsis: if r[P3] in rowset(P1) goto P2
77462**
77463** Register P3 is assumed to hold a 64-bit integer value. If register P1
77464** contains a RowSet object and that RowSet object contains
77465** the value held in P3, jump to register P2. Otherwise, insert the
77466** integer in P3 into the RowSet and continue on to the
77467** next opcode.
77468**
77469** The RowSet object is optimized for the case where successive sets
77470** of integers, where each set contains no duplicates. Each set
77471** of values is identified by a unique P4 value. The first set
77472** must have P4==0, the final set P4=-1.  P4 must be either -1 or
77473** non-negative.  For non-negative values of P4 only the lower 4
77474** bits are significant.
77475**
77476** This allows optimizations: (a) when P4==0 there is no need to test
77477** the rowset object for P3, as it is guaranteed not to contain it,
77478** (b) when P4==-1 there is no need to insert the value, as it will
77479** never be tested for, and (c) when a value that is part of set X is
77480** inserted, there is no need to search to see if the same value was
77481** previously inserted as part of set X (only if it was previously
77482** inserted as part of some other set).
77483*/
77484case OP_RowSetTest: {                     /* jump, in1, in3 */
77485  int iSet;
77486  int exists;
77487
77488  pIn1 = &aMem[pOp->p1];
77489  pIn3 = &aMem[pOp->p3];
77490  iSet = pOp->p4.i;
77491  assert( pIn3->flags&MEM_Int );
77492
77493  /* If there is anything other than a rowset object in memory cell P1,
77494  ** delete it now and initialize P1 with an empty rowset
77495  */
77496  if( (pIn1->flags & MEM_RowSet)==0 ){
77497    sqlite3VdbeMemSetRowSet(pIn1);
77498    if( (pIn1->flags & MEM_RowSet)==0 ) goto no_mem;
77499  }
77500
77501  assert( pOp->p4type==P4_INT32 );
77502  assert( iSet==-1 || iSet>=0 );
77503  if( iSet ){
77504    exists = sqlite3RowSetTest(pIn1->u.pRowSet, iSet, pIn3->u.i);
77505    VdbeBranchTaken(exists!=0,2);
77506    if( exists ) goto jump_to_p2;
77507  }
77508  if( iSet>=0 ){
77509    sqlite3RowSetInsert(pIn1->u.pRowSet, pIn3->u.i);
77510  }
77511  break;
77512}
77513
77514
77515#ifndef SQLITE_OMIT_TRIGGER
77516
77517/* Opcode: Program P1 P2 P3 P4 P5
77518**
77519** Execute the trigger program passed as P4 (type P4_SUBPROGRAM).
77520**
77521** P1 contains the address of the memory cell that contains the first memory
77522** cell in an array of values used as arguments to the sub-program. P2
77523** contains the address to jump to if the sub-program throws an IGNORE
77524** exception using the RAISE() function. Register P3 contains the address
77525** of a memory cell in this (the parent) VM that is used to allocate the
77526** memory required by the sub-vdbe at runtime.
77527**
77528** P4 is a pointer to the VM containing the trigger program.
77529**
77530** If P5 is non-zero, then recursive program invocation is enabled.
77531*/
77532case OP_Program: {        /* jump */
77533  int nMem;               /* Number of memory registers for sub-program */
77534  int nByte;              /* Bytes of runtime space required for sub-program */
77535  Mem *pRt;               /* Register to allocate runtime space */
77536  Mem *pMem;              /* Used to iterate through memory cells */
77537  Mem *pEnd;              /* Last memory cell in new array */
77538  VdbeFrame *pFrame;      /* New vdbe frame to execute in */
77539  SubProgram *pProgram;   /* Sub-program to execute */
77540  void *t;                /* Token identifying trigger */
77541
77542  pProgram = pOp->p4.pProgram;
77543  pRt = &aMem[pOp->p3];
77544  assert( pProgram->nOp>0 );
77545
77546  /* If the p5 flag is clear, then recursive invocation of triggers is
77547  ** disabled for backwards compatibility (p5 is set if this sub-program
77548  ** is really a trigger, not a foreign key action, and the flag set
77549  ** and cleared by the "PRAGMA recursive_triggers" command is clear).
77550  **
77551  ** It is recursive invocation of triggers, at the SQL level, that is
77552  ** disabled. In some cases a single trigger may generate more than one
77553  ** SubProgram (if the trigger may be executed with more than one different
77554  ** ON CONFLICT algorithm). SubProgram structures associated with a
77555  ** single trigger all have the same value for the SubProgram.token
77556  ** variable.  */
77557  if( pOp->p5 ){
77558    t = pProgram->token;
77559    for(pFrame=p->pFrame; pFrame && pFrame->token!=t; pFrame=pFrame->pParent);
77560    if( pFrame ) break;
77561  }
77562
77563  if( p->nFrame>=db->aLimit[SQLITE_LIMIT_TRIGGER_DEPTH] ){
77564    rc = SQLITE_ERROR;
77565    sqlite3VdbeError(p, "too many levels of trigger recursion");
77566    break;
77567  }
77568
77569  /* Register pRt is used to store the memory required to save the state
77570  ** of the current program, and the memory required at runtime to execute
77571  ** the trigger program. If this trigger has been fired before, then pRt
77572  ** is already allocated. Otherwise, it must be initialized.  */
77573  if( (pRt->flags&MEM_Frame)==0 ){
77574    /* SubProgram.nMem is set to the number of memory cells used by the
77575    ** program stored in SubProgram.aOp. As well as these, one memory
77576    ** cell is required for each cursor used by the program. Set local
77577    ** variable nMem (and later, VdbeFrame.nChildMem) to this value.
77578    */
77579    nMem = pProgram->nMem + pProgram->nCsr;
77580    nByte = ROUND8(sizeof(VdbeFrame))
77581              + nMem * sizeof(Mem)
77582              + pProgram->nCsr * sizeof(VdbeCursor *)
77583              + pProgram->nOnce * sizeof(u8);
77584    pFrame = sqlite3DbMallocZero(db, nByte);
77585    if( !pFrame ){
77586      goto no_mem;
77587    }
77588    sqlite3VdbeMemRelease(pRt);
77589    pRt->flags = MEM_Frame;
77590    pRt->u.pFrame = pFrame;
77591
77592    pFrame->v = p;
77593    pFrame->nChildMem = nMem;
77594    pFrame->nChildCsr = pProgram->nCsr;
77595    pFrame->pc = (int)(pOp - aOp);
77596    pFrame->aMem = p->aMem;
77597    pFrame->nMem = p->nMem;
77598    pFrame->apCsr = p->apCsr;
77599    pFrame->nCursor = p->nCursor;
77600    pFrame->aOp = p->aOp;
77601    pFrame->nOp = p->nOp;
77602    pFrame->token = pProgram->token;
77603    pFrame->aOnceFlag = p->aOnceFlag;
77604    pFrame->nOnceFlag = p->nOnceFlag;
77605#ifdef SQLITE_ENABLE_STMT_SCANSTATUS
77606    pFrame->anExec = p->anExec;
77607#endif
77608
77609    pEnd = &VdbeFrameMem(pFrame)[pFrame->nChildMem];
77610    for(pMem=VdbeFrameMem(pFrame); pMem!=pEnd; pMem++){
77611      pMem->flags = MEM_Undefined;
77612      pMem->db = db;
77613    }
77614  }else{
77615    pFrame = pRt->u.pFrame;
77616    assert( pProgram->nMem+pProgram->nCsr==pFrame->nChildMem );
77617    assert( pProgram->nCsr==pFrame->nChildCsr );
77618    assert( (int)(pOp - aOp)==pFrame->pc );
77619  }
77620
77621  p->nFrame++;
77622  pFrame->pParent = p->pFrame;
77623  pFrame->lastRowid = lastRowid;
77624  pFrame->nChange = p->nChange;
77625  pFrame->nDbChange = p->db->nChange;
77626  p->nChange = 0;
77627  p->pFrame = pFrame;
77628  p->aMem = aMem = &VdbeFrameMem(pFrame)[-1];
77629  p->nMem = pFrame->nChildMem;
77630  p->nCursor = (u16)pFrame->nChildCsr;
77631  p->apCsr = (VdbeCursor **)&aMem[p->nMem+1];
77632  p->aOp = aOp = pProgram->aOp;
77633  p->nOp = pProgram->nOp;
77634  p->aOnceFlag = (u8 *)&p->apCsr[p->nCursor];
77635  p->nOnceFlag = pProgram->nOnce;
77636#ifdef SQLITE_ENABLE_STMT_SCANSTATUS
77637  p->anExec = 0;
77638#endif
77639  pOp = &aOp[-1];
77640  memset(p->aOnceFlag, 0, p->nOnceFlag);
77641
77642  break;
77643}
77644
77645/* Opcode: Param P1 P2 * * *
77646**
77647** This opcode is only ever present in sub-programs called via the
77648** OP_Program instruction. Copy a value currently stored in a memory
77649** cell of the calling (parent) frame to cell P2 in the current frames
77650** address space. This is used by trigger programs to access the new.*
77651** and old.* values.
77652**
77653** The address of the cell in the parent frame is determined by adding
77654** the value of the P1 argument to the value of the P1 argument to the
77655** calling OP_Program instruction.
77656*/
77657case OP_Param: {           /* out2 */
77658  VdbeFrame *pFrame;
77659  Mem *pIn;
77660  pOut = out2Prerelease(p, pOp);
77661  pFrame = p->pFrame;
77662  pIn = &pFrame->aMem[pOp->p1 + pFrame->aOp[pFrame->pc].p1];
77663  sqlite3VdbeMemShallowCopy(pOut, pIn, MEM_Ephem);
77664  break;
77665}
77666
77667#endif /* #ifndef SQLITE_OMIT_TRIGGER */
77668
77669#ifndef SQLITE_OMIT_FOREIGN_KEY
77670/* Opcode: FkCounter P1 P2 * * *
77671** Synopsis: fkctr[P1]+=P2
77672**
77673** Increment a "constraint counter" by P2 (P2 may be negative or positive).
77674** If P1 is non-zero, the database constraint counter is incremented
77675** (deferred foreign key constraints). Otherwise, if P1 is zero, the
77676** statement counter is incremented (immediate foreign key constraints).
77677*/
77678case OP_FkCounter: {
77679  if( db->flags & SQLITE_DeferFKs ){
77680    db->nDeferredImmCons += pOp->p2;
77681  }else if( pOp->p1 ){
77682    db->nDeferredCons += pOp->p2;
77683  }else{
77684    p->nFkConstraint += pOp->p2;
77685  }
77686  break;
77687}
77688
77689/* Opcode: FkIfZero P1 P2 * * *
77690** Synopsis: if fkctr[P1]==0 goto P2
77691**
77692** This opcode tests if a foreign key constraint-counter is currently zero.
77693** If so, jump to instruction P2. Otherwise, fall through to the next
77694** instruction.
77695**
77696** If P1 is non-zero, then the jump is taken if the database constraint-counter
77697** is zero (the one that counts deferred constraint violations). If P1 is
77698** zero, the jump is taken if the statement constraint-counter is zero
77699** (immediate foreign key constraint violations).
77700*/
77701case OP_FkIfZero: {         /* jump */
77702  if( pOp->p1 ){
77703    VdbeBranchTaken(db->nDeferredCons==0 && db->nDeferredImmCons==0, 2);
77704    if( db->nDeferredCons==0 && db->nDeferredImmCons==0 ) goto jump_to_p2;
77705  }else{
77706    VdbeBranchTaken(p->nFkConstraint==0 && db->nDeferredImmCons==0, 2);
77707    if( p->nFkConstraint==0 && db->nDeferredImmCons==0 ) goto jump_to_p2;
77708  }
77709  break;
77710}
77711#endif /* #ifndef SQLITE_OMIT_FOREIGN_KEY */
77712
77713#ifndef SQLITE_OMIT_AUTOINCREMENT
77714/* Opcode: MemMax P1 P2 * * *
77715** Synopsis: r[P1]=max(r[P1],r[P2])
77716**
77717** P1 is a register in the root frame of this VM (the root frame is
77718** different from the current frame if this instruction is being executed
77719** within a sub-program). Set the value of register P1 to the maximum of
77720** its current value and the value in register P2.
77721**
77722** This instruction throws an error if the memory cell is not initially
77723** an integer.
77724*/
77725case OP_MemMax: {        /* in2 */
77726  VdbeFrame *pFrame;
77727  if( p->pFrame ){
77728    for(pFrame=p->pFrame; pFrame->pParent; pFrame=pFrame->pParent);
77729    pIn1 = &pFrame->aMem[pOp->p1];
77730  }else{
77731    pIn1 = &aMem[pOp->p1];
77732  }
77733  assert( memIsValid(pIn1) );
77734  sqlite3VdbeMemIntegerify(pIn1);
77735  pIn2 = &aMem[pOp->p2];
77736  sqlite3VdbeMemIntegerify(pIn2);
77737  if( pIn1->u.i<pIn2->u.i){
77738    pIn1->u.i = pIn2->u.i;
77739  }
77740  break;
77741}
77742#endif /* SQLITE_OMIT_AUTOINCREMENT */
77743
77744/* Opcode: IfPos P1 P2 * * *
77745** Synopsis: if r[P1]>0 goto P2
77746**
77747** Register P1 must contain an integer.
77748** If the value of register P1 is 1 or greater, jump to P2 and
77749** add the literal value P3 to register P1.
77750**
77751** If the initial value of register P1 is less than 1, then the
77752** value is unchanged and control passes through to the next instruction.
77753*/
77754case OP_IfPos: {        /* jump, in1 */
77755  pIn1 = &aMem[pOp->p1];
77756  assert( pIn1->flags&MEM_Int );
77757  VdbeBranchTaken( pIn1->u.i>0, 2);
77758  if( pIn1->u.i>0 ) goto jump_to_p2;
77759  break;
77760}
77761
77762/* Opcode: IfNeg P1 P2 P3 * *
77763** Synopsis: r[P1]+=P3, if r[P1]<0 goto P2
77764**
77765** Register P1 must contain an integer.  Add literal P3 to the value in
77766** register P1 then if the value of register P1 is less than zero, jump to P2.
77767*/
77768case OP_IfNeg: {        /* jump, in1 */
77769  pIn1 = &aMem[pOp->p1];
77770  assert( pIn1->flags&MEM_Int );
77771  pIn1->u.i += pOp->p3;
77772  VdbeBranchTaken(pIn1->u.i<0, 2);
77773  if( pIn1->u.i<0 ) goto jump_to_p2;
77774  break;
77775}
77776
77777/* Opcode: IfNotZero P1 P2 P3 * *
77778** Synopsis: if r[P1]!=0 then r[P1]+=P3, goto P2
77779**
77780** Register P1 must contain an integer.  If the content of register P1 is
77781** initially nonzero, then add P3 to P1 and jump to P2.  If register P1 is
77782** initially zero, leave it unchanged and fall through.
77783*/
77784case OP_IfNotZero: {        /* jump, in1 */
77785  pIn1 = &aMem[pOp->p1];
77786  assert( pIn1->flags&MEM_Int );
77787  VdbeBranchTaken(pIn1->u.i<0, 2);
77788  if( pIn1->u.i ){
77789     pIn1->u.i += pOp->p3;
77790     goto jump_to_p2;
77791  }
77792  break;
77793}
77794
77795/* Opcode: DecrJumpZero P1 P2 * * *
77796** Synopsis: if (--r[P1])==0 goto P2
77797**
77798** Register P1 must hold an integer.  Decrement the value in register P1
77799** then jump to P2 if the new value is exactly zero.
77800*/
77801case OP_DecrJumpZero: {      /* jump, in1 */
77802  pIn1 = &aMem[pOp->p1];
77803  assert( pIn1->flags&MEM_Int );
77804  pIn1->u.i--;
77805  VdbeBranchTaken(pIn1->u.i==0, 2);
77806  if( pIn1->u.i==0 ) goto jump_to_p2;
77807  break;
77808}
77809
77810
77811/* Opcode: JumpZeroIncr P1 P2 * * *
77812** Synopsis: if (r[P1]++)==0 ) goto P2
77813**
77814** The register P1 must contain an integer.  If register P1 is initially
77815** zero, then jump to P2.  Increment register P1 regardless of whether or
77816** not the jump is taken.
77817*/
77818case OP_JumpZeroIncr: {        /* jump, in1 */
77819  pIn1 = &aMem[pOp->p1];
77820  assert( pIn1->flags&MEM_Int );
77821  VdbeBranchTaken(pIn1->u.i==0, 2);
77822  if( (pIn1->u.i++)==0 ) goto jump_to_p2;
77823  break;
77824}
77825
77826/* Opcode: AggStep0 * P2 P3 P4 P5
77827** Synopsis: accum=r[P3] step(r[P2@P5])
77828**
77829** Execute the step function for an aggregate.  The
77830** function has P5 arguments.   P4 is a pointer to the FuncDef
77831** structure that specifies the function.  Register P3 is the
77832** accumulator.
77833**
77834** The P5 arguments are taken from register P2 and its
77835** successors.
77836*/
77837/* Opcode: AggStep * P2 P3 P4 P5
77838** Synopsis: accum=r[P3] step(r[P2@P5])
77839**
77840** Execute the step function for an aggregate.  The
77841** function has P5 arguments.   P4 is a pointer to an sqlite3_context
77842** object that is used to run the function.  Register P3 is
77843** as the accumulator.
77844**
77845** The P5 arguments are taken from register P2 and its
77846** successors.
77847**
77848** This opcode is initially coded as OP_AggStep0.  On first evaluation,
77849** the FuncDef stored in P4 is converted into an sqlite3_context and
77850** the opcode is changed.  In this way, the initialization of the
77851** sqlite3_context only happens once, instead of on each call to the
77852** step function.
77853*/
77854case OP_AggStep0: {
77855  int n;
77856  sqlite3_context *pCtx;
77857
77858  assert( pOp->p4type==P4_FUNCDEF );
77859  n = pOp->p5;
77860  assert( pOp->p3>0 && pOp->p3<=(p->nMem-p->nCursor) );
77861  assert( n==0 || (pOp->p2>0 && pOp->p2+n<=(p->nMem-p->nCursor)+1) );
77862  assert( pOp->p3<pOp->p2 || pOp->p3>=pOp->p2+n );
77863  pCtx = sqlite3DbMallocRaw(db, sizeof(*pCtx) + (n-1)*sizeof(sqlite3_value*));
77864  if( pCtx==0 ) goto no_mem;
77865  pCtx->pMem = 0;
77866  pCtx->pFunc = pOp->p4.pFunc;
77867  pCtx->iOp = (int)(pOp - aOp);
77868  pCtx->pVdbe = p;
77869  pCtx->argc = n;
77870  pOp->p4type = P4_FUNCCTX;
77871  pOp->p4.pCtx = pCtx;
77872  pOp->opcode = OP_AggStep;
77873  /* Fall through into OP_AggStep */
77874}
77875case OP_AggStep: {
77876  int i;
77877  sqlite3_context *pCtx;
77878  Mem *pMem;
77879  Mem t;
77880
77881  assert( pOp->p4type==P4_FUNCCTX );
77882  pCtx = pOp->p4.pCtx;
77883  pMem = &aMem[pOp->p3];
77884
77885  /* If this function is inside of a trigger, the register array in aMem[]
77886  ** might change from one evaluation to the next.  The next block of code
77887  ** checks to see if the register array has changed, and if so it
77888  ** reinitializes the relavant parts of the sqlite3_context object */
77889  if( pCtx->pMem != pMem ){
77890    pCtx->pMem = pMem;
77891    for(i=pCtx->argc-1; i>=0; i--) pCtx->argv[i] = &aMem[pOp->p2+i];
77892  }
77893
77894#ifdef SQLITE_DEBUG
77895  for(i=0; i<pCtx->argc; i++){
77896    assert( memIsValid(pCtx->argv[i]) );
77897    REGISTER_TRACE(pOp->p2+i, pCtx->argv[i]);
77898  }
77899#endif
77900
77901  pMem->n++;
77902  sqlite3VdbeMemInit(&t, db, MEM_Null);
77903  pCtx->pOut = &t;
77904  pCtx->fErrorOrAux = 0;
77905  pCtx->skipFlag = 0;
77906  (pCtx->pFunc->xStep)(pCtx,pCtx->argc,pCtx->argv); /* IMP: R-24505-23230 */
77907  if( pCtx->fErrorOrAux ){
77908    if( pCtx->isError ){
77909      sqlite3VdbeError(p, "%s", sqlite3_value_text(&t));
77910      rc = pCtx->isError;
77911    }
77912    sqlite3VdbeMemRelease(&t);
77913  }else{
77914    assert( t.flags==MEM_Null );
77915  }
77916  if( pCtx->skipFlag ){
77917    assert( pOp[-1].opcode==OP_CollSeq );
77918    i = pOp[-1].p1;
77919    if( i ) sqlite3VdbeMemSetInt64(&aMem[i], 1);
77920  }
77921  break;
77922}
77923
77924/* Opcode: AggFinal P1 P2 * P4 *
77925** Synopsis: accum=r[P1] N=P2
77926**
77927** Execute the finalizer function for an aggregate.  P1 is
77928** the memory location that is the accumulator for the aggregate.
77929**
77930** P2 is the number of arguments that the step function takes and
77931** P4 is a pointer to the FuncDef for this function.  The P2
77932** argument is not used by this opcode.  It is only there to disambiguate
77933** functions that can take varying numbers of arguments.  The
77934** P4 argument is only needed for the degenerate case where
77935** the step function was not previously called.
77936*/
77937case OP_AggFinal: {
77938  Mem *pMem;
77939  assert( pOp->p1>0 && pOp->p1<=(p->nMem-p->nCursor) );
77940  pMem = &aMem[pOp->p1];
77941  assert( (pMem->flags & ~(MEM_Null|MEM_Agg))==0 );
77942  rc = sqlite3VdbeMemFinalize(pMem, pOp->p4.pFunc);
77943  if( rc ){
77944    sqlite3VdbeError(p, "%s", sqlite3_value_text(pMem));
77945  }
77946  sqlite3VdbeChangeEncoding(pMem, encoding);
77947  UPDATE_MAX_BLOBSIZE(pMem);
77948  if( sqlite3VdbeMemTooBig(pMem) ){
77949    goto too_big;
77950  }
77951  break;
77952}
77953
77954#ifndef SQLITE_OMIT_WAL
77955/* Opcode: Checkpoint P1 P2 P3 * *
77956**
77957** Checkpoint database P1. This is a no-op if P1 is not currently in
77958** WAL mode. Parameter P2 is one of SQLITE_CHECKPOINT_PASSIVE, FULL,
77959** RESTART, or TRUNCATE.  Write 1 or 0 into mem[P3] if the checkpoint returns
77960** SQLITE_BUSY or not, respectively.  Write the number of pages in the
77961** WAL after the checkpoint into mem[P3+1] and the number of pages
77962** in the WAL that have been checkpointed after the checkpoint
77963** completes into mem[P3+2].  However on an error, mem[P3+1] and
77964** mem[P3+2] are initialized to -1.
77965*/
77966case OP_Checkpoint: {
77967  int i;                          /* Loop counter */
77968  int aRes[3];                    /* Results */
77969  Mem *pMem;                      /* Write results here */
77970
77971  assert( p->readOnly==0 );
77972  aRes[0] = 0;
77973  aRes[1] = aRes[2] = -1;
77974  assert( pOp->p2==SQLITE_CHECKPOINT_PASSIVE
77975       || pOp->p2==SQLITE_CHECKPOINT_FULL
77976       || pOp->p2==SQLITE_CHECKPOINT_RESTART
77977       || pOp->p2==SQLITE_CHECKPOINT_TRUNCATE
77978  );
77979  rc = sqlite3Checkpoint(db, pOp->p1, pOp->p2, &aRes[1], &aRes[2]);
77980  if( rc==SQLITE_BUSY ){
77981    rc = SQLITE_OK;
77982    aRes[0] = 1;
77983  }
77984  for(i=0, pMem = &aMem[pOp->p3]; i<3; i++, pMem++){
77985    sqlite3VdbeMemSetInt64(pMem, (i64)aRes[i]);
77986  }
77987  break;
77988};
77989#endif
77990
77991#ifndef SQLITE_OMIT_PRAGMA
77992/* Opcode: JournalMode P1 P2 P3 * *
77993**
77994** Change the journal mode of database P1 to P3. P3 must be one of the
77995** PAGER_JOURNALMODE_XXX values. If changing between the various rollback
77996** modes (delete, truncate, persist, off and memory), this is a simple
77997** operation. No IO is required.
77998**
77999** If changing into or out of WAL mode the procedure is more complicated.
78000**
78001** Write a string containing the final journal-mode to register P2.
78002*/
78003case OP_JournalMode: {    /* out2 */
78004  Btree *pBt;                     /* Btree to change journal mode of */
78005  Pager *pPager;                  /* Pager associated with pBt */
78006  int eNew;                       /* New journal mode */
78007  int eOld;                       /* The old journal mode */
78008#ifndef SQLITE_OMIT_WAL
78009  const char *zFilename;          /* Name of database file for pPager */
78010#endif
78011
78012  pOut = out2Prerelease(p, pOp);
78013  eNew = pOp->p3;
78014  assert( eNew==PAGER_JOURNALMODE_DELETE
78015       || eNew==PAGER_JOURNALMODE_TRUNCATE
78016       || eNew==PAGER_JOURNALMODE_PERSIST
78017       || eNew==PAGER_JOURNALMODE_OFF
78018       || eNew==PAGER_JOURNALMODE_MEMORY
78019       || eNew==PAGER_JOURNALMODE_WAL
78020       || eNew==PAGER_JOURNALMODE_QUERY
78021  );
78022  assert( pOp->p1>=0 && pOp->p1<db->nDb );
78023  assert( p->readOnly==0 );
78024
78025  pBt = db->aDb[pOp->p1].pBt;
78026  pPager = sqlite3BtreePager(pBt);
78027  eOld = sqlite3PagerGetJournalMode(pPager);
78028  if( eNew==PAGER_JOURNALMODE_QUERY ) eNew = eOld;
78029  if( !sqlite3PagerOkToChangeJournalMode(pPager) ) eNew = eOld;
78030
78031#ifndef SQLITE_OMIT_WAL
78032  zFilename = sqlite3PagerFilename(pPager, 1);
78033
78034  /* Do not allow a transition to journal_mode=WAL for a database
78035  ** in temporary storage or if the VFS does not support shared memory
78036  */
78037  if( eNew==PAGER_JOURNALMODE_WAL
78038   && (sqlite3Strlen30(zFilename)==0           /* Temp file */
78039       || !sqlite3PagerWalSupported(pPager))   /* No shared-memory support */
78040  ){
78041    eNew = eOld;
78042  }
78043
78044  if( (eNew!=eOld)
78045   && (eOld==PAGER_JOURNALMODE_WAL || eNew==PAGER_JOURNALMODE_WAL)
78046  ){
78047    if( !db->autoCommit || db->nVdbeRead>1 ){
78048      rc = SQLITE_ERROR;
78049      sqlite3VdbeError(p,
78050          "cannot change %s wal mode from within a transaction",
78051          (eNew==PAGER_JOURNALMODE_WAL ? "into" : "out of")
78052      );
78053      break;
78054    }else{
78055
78056      if( eOld==PAGER_JOURNALMODE_WAL ){
78057        /* If leaving WAL mode, close the log file. If successful, the call
78058        ** to PagerCloseWal() checkpoints and deletes the write-ahead-log
78059        ** file. An EXCLUSIVE lock may still be held on the database file
78060        ** after a successful return.
78061        */
78062        rc = sqlite3PagerCloseWal(pPager);
78063        if( rc==SQLITE_OK ){
78064          sqlite3PagerSetJournalMode(pPager, eNew);
78065        }
78066      }else if( eOld==PAGER_JOURNALMODE_MEMORY ){
78067        /* Cannot transition directly from MEMORY to WAL.  Use mode OFF
78068        ** as an intermediate */
78069        sqlite3PagerSetJournalMode(pPager, PAGER_JOURNALMODE_OFF);
78070      }
78071
78072      /* Open a transaction on the database file. Regardless of the journal
78073      ** mode, this transaction always uses a rollback journal.
78074      */
78075      assert( sqlite3BtreeIsInTrans(pBt)==0 );
78076      if( rc==SQLITE_OK ){
78077        rc = sqlite3BtreeSetVersion(pBt, (eNew==PAGER_JOURNALMODE_WAL ? 2 : 1));
78078      }
78079    }
78080  }
78081#endif /* ifndef SQLITE_OMIT_WAL */
78082
78083  if( rc ){
78084    eNew = eOld;
78085  }
78086  eNew = sqlite3PagerSetJournalMode(pPager, eNew);
78087
78088  pOut->flags = MEM_Str|MEM_Static|MEM_Term;
78089  pOut->z = (char *)sqlite3JournalModename(eNew);
78090  pOut->n = sqlite3Strlen30(pOut->z);
78091  pOut->enc = SQLITE_UTF8;
78092  sqlite3VdbeChangeEncoding(pOut, encoding);
78093  break;
78094};
78095#endif /* SQLITE_OMIT_PRAGMA */
78096
78097#if !defined(SQLITE_OMIT_VACUUM) && !defined(SQLITE_OMIT_ATTACH)
78098/* Opcode: Vacuum * * * * *
78099**
78100** Vacuum the entire database.  This opcode will cause other virtual
78101** machines to be created and run.  It may not be called from within
78102** a transaction.
78103*/
78104case OP_Vacuum: {
78105  assert( p->readOnly==0 );
78106  rc = sqlite3RunVacuum(&p->zErrMsg, db);
78107  break;
78108}
78109#endif
78110
78111#if !defined(SQLITE_OMIT_AUTOVACUUM)
78112/* Opcode: IncrVacuum P1 P2 * * *
78113**
78114** Perform a single step of the incremental vacuum procedure on
78115** the P1 database. If the vacuum has finished, jump to instruction
78116** P2. Otherwise, fall through to the next instruction.
78117*/
78118case OP_IncrVacuum: {        /* jump */
78119  Btree *pBt;
78120
78121  assert( pOp->p1>=0 && pOp->p1<db->nDb );
78122  assert( DbMaskTest(p->btreeMask, pOp->p1) );
78123  assert( p->readOnly==0 );
78124  pBt = db->aDb[pOp->p1].pBt;
78125  rc = sqlite3BtreeIncrVacuum(pBt);
78126  VdbeBranchTaken(rc==SQLITE_DONE,2);
78127  if( rc==SQLITE_DONE ){
78128    rc = SQLITE_OK;
78129    goto jump_to_p2;
78130  }
78131  break;
78132}
78133#endif
78134
78135/* Opcode: Expire P1 * * * *
78136**
78137** Cause precompiled statements to expire.  When an expired statement
78138** is executed using sqlite3_step() it will either automatically
78139** reprepare itself (if it was originally created using sqlite3_prepare_v2())
78140** or it will fail with SQLITE_SCHEMA.
78141**
78142** If P1 is 0, then all SQL statements become expired. If P1 is non-zero,
78143** then only the currently executing statement is expired.
78144*/
78145case OP_Expire: {
78146  if( !pOp->p1 ){
78147    sqlite3ExpirePreparedStatements(db);
78148  }else{
78149    p->expired = 1;
78150  }
78151  break;
78152}
78153
78154#ifndef SQLITE_OMIT_SHARED_CACHE
78155/* Opcode: TableLock P1 P2 P3 P4 *
78156** Synopsis: iDb=P1 root=P2 write=P3
78157**
78158** Obtain a lock on a particular table. This instruction is only used when
78159** the shared-cache feature is enabled.
78160**
78161** P1 is the index of the database in sqlite3.aDb[] of the database
78162** on which the lock is acquired.  A readlock is obtained if P3==0 or
78163** a write lock if P3==1.
78164**
78165** P2 contains the root-page of the table to lock.
78166**
78167** P4 contains a pointer to the name of the table being locked. This is only
78168** used to generate an error message if the lock cannot be obtained.
78169*/
78170case OP_TableLock: {
78171  u8 isWriteLock = (u8)pOp->p3;
78172  if( isWriteLock || 0==(db->flags&SQLITE_ReadUncommitted) ){
78173    int p1 = pOp->p1;
78174    assert( p1>=0 && p1<db->nDb );
78175    assert( DbMaskTest(p->btreeMask, p1) );
78176    assert( isWriteLock==0 || isWriteLock==1 );
78177    rc = sqlite3BtreeLockTable(db->aDb[p1].pBt, pOp->p2, isWriteLock);
78178    if( (rc&0xFF)==SQLITE_LOCKED ){
78179      const char *z = pOp->p4.z;
78180      sqlite3VdbeError(p, "database table is locked: %s", z);
78181    }
78182  }
78183  break;
78184}
78185#endif /* SQLITE_OMIT_SHARED_CACHE */
78186
78187#ifndef SQLITE_OMIT_VIRTUALTABLE
78188/* Opcode: VBegin * * * P4 *
78189**
78190** P4 may be a pointer to an sqlite3_vtab structure. If so, call the
78191** xBegin method for that table.
78192**
78193** Also, whether or not P4 is set, check that this is not being called from
78194** within a callback to a virtual table xSync() method. If it is, the error
78195** code will be set to SQLITE_LOCKED.
78196*/
78197case OP_VBegin: {
78198  VTable *pVTab;
78199  pVTab = pOp->p4.pVtab;
78200  rc = sqlite3VtabBegin(db, pVTab);
78201  if( pVTab ) sqlite3VtabImportErrmsg(p, pVTab->pVtab);
78202  break;
78203}
78204#endif /* SQLITE_OMIT_VIRTUALTABLE */
78205
78206#ifndef SQLITE_OMIT_VIRTUALTABLE
78207/* Opcode: VCreate P1 P2 * * *
78208**
78209** P2 is a register that holds the name of a virtual table in database
78210** P1. Call the xCreate method for that table.
78211*/
78212case OP_VCreate: {
78213  Mem sMem;          /* For storing the record being decoded */
78214  const char *zTab;  /* Name of the virtual table */
78215
78216  memset(&sMem, 0, sizeof(sMem));
78217  sMem.db = db;
78218  /* Because P2 is always a static string, it is impossible for the
78219  ** sqlite3VdbeMemCopy() to fail */
78220  assert( (aMem[pOp->p2].flags & MEM_Str)!=0 );
78221  assert( (aMem[pOp->p2].flags & MEM_Static)!=0 );
78222  rc = sqlite3VdbeMemCopy(&sMem, &aMem[pOp->p2]);
78223  assert( rc==SQLITE_OK );
78224  zTab = (const char*)sqlite3_value_text(&sMem);
78225  assert( zTab || db->mallocFailed );
78226  if( zTab ){
78227    rc = sqlite3VtabCallCreate(db, pOp->p1, zTab, &p->zErrMsg);
78228  }
78229  sqlite3VdbeMemRelease(&sMem);
78230  break;
78231}
78232#endif /* SQLITE_OMIT_VIRTUALTABLE */
78233
78234#ifndef SQLITE_OMIT_VIRTUALTABLE
78235/* Opcode: VDestroy P1 * * P4 *
78236**
78237** P4 is the name of a virtual table in database P1.  Call the xDestroy method
78238** of that table.
78239*/
78240case OP_VDestroy: {
78241  db->nVDestroy++;
78242  rc = sqlite3VtabCallDestroy(db, pOp->p1, pOp->p4.z);
78243  db->nVDestroy--;
78244  break;
78245}
78246#endif /* SQLITE_OMIT_VIRTUALTABLE */
78247
78248#ifndef SQLITE_OMIT_VIRTUALTABLE
78249/* Opcode: VOpen P1 * * P4 *
78250**
78251** P4 is a pointer to a virtual table object, an sqlite3_vtab structure.
78252** P1 is a cursor number.  This opcode opens a cursor to the virtual
78253** table and stores that cursor in P1.
78254*/
78255case OP_VOpen: {
78256  VdbeCursor *pCur;
78257  sqlite3_vtab_cursor *pVtabCursor;
78258  sqlite3_vtab *pVtab;
78259  const sqlite3_module *pModule;
78260
78261  assert( p->bIsReader );
78262  pCur = 0;
78263  pVtabCursor = 0;
78264  pVtab = pOp->p4.pVtab->pVtab;
78265  if( pVtab==0 || NEVER(pVtab->pModule==0) ){
78266    rc = SQLITE_LOCKED;
78267    break;
78268  }
78269  pModule = pVtab->pModule;
78270  rc = pModule->xOpen(pVtab, &pVtabCursor);
78271  sqlite3VtabImportErrmsg(p, pVtab);
78272  if( SQLITE_OK==rc ){
78273    /* Initialize sqlite3_vtab_cursor base class */
78274    pVtabCursor->pVtab = pVtab;
78275
78276    /* Initialize vdbe cursor object */
78277    pCur = allocateCursor(p, pOp->p1, 0, -1, 0);
78278    if( pCur ){
78279      pCur->pVtabCursor = pVtabCursor;
78280      pVtab->nRef++;
78281    }else{
78282      assert( db->mallocFailed );
78283      pModule->xClose(pVtabCursor);
78284      goto no_mem;
78285    }
78286  }
78287  break;
78288}
78289#endif /* SQLITE_OMIT_VIRTUALTABLE */
78290
78291#ifndef SQLITE_OMIT_VIRTUALTABLE
78292/* Opcode: VFilter P1 P2 P3 P4 *
78293** Synopsis: iplan=r[P3] zplan='P4'
78294**
78295** P1 is a cursor opened using VOpen.  P2 is an address to jump to if
78296** the filtered result set is empty.
78297**
78298** P4 is either NULL or a string that was generated by the xBestIndex
78299** method of the module.  The interpretation of the P4 string is left
78300** to the module implementation.
78301**
78302** This opcode invokes the xFilter method on the virtual table specified
78303** by P1.  The integer query plan parameter to xFilter is stored in register
78304** P3. Register P3+1 stores the argc parameter to be passed to the
78305** xFilter method. Registers P3+2..P3+1+argc are the argc
78306** additional parameters which are passed to
78307** xFilter as argv. Register P3+2 becomes argv[0] when passed to xFilter.
78308**
78309** A jump is made to P2 if the result set after filtering would be empty.
78310*/
78311case OP_VFilter: {   /* jump */
78312  int nArg;
78313  int iQuery;
78314  const sqlite3_module *pModule;
78315  Mem *pQuery;
78316  Mem *pArgc;
78317  sqlite3_vtab_cursor *pVtabCursor;
78318  sqlite3_vtab *pVtab;
78319  VdbeCursor *pCur;
78320  int res;
78321  int i;
78322  Mem **apArg;
78323
78324  pQuery = &aMem[pOp->p3];
78325  pArgc = &pQuery[1];
78326  pCur = p->apCsr[pOp->p1];
78327  assert( memIsValid(pQuery) );
78328  REGISTER_TRACE(pOp->p3, pQuery);
78329  assert( pCur->pVtabCursor );
78330  pVtabCursor = pCur->pVtabCursor;
78331  pVtab = pVtabCursor->pVtab;
78332  pModule = pVtab->pModule;
78333
78334  /* Grab the index number and argc parameters */
78335  assert( (pQuery->flags&MEM_Int)!=0 && pArgc->flags==MEM_Int );
78336  nArg = (int)pArgc->u.i;
78337  iQuery = (int)pQuery->u.i;
78338
78339  /* Invoke the xFilter method */
78340  res = 0;
78341  apArg = p->apArg;
78342  for(i = 0; i<nArg; i++){
78343    apArg[i] = &pArgc[i+1];
78344  }
78345  rc = pModule->xFilter(pVtabCursor, iQuery, pOp->p4.z, nArg, apArg);
78346  sqlite3VtabImportErrmsg(p, pVtab);
78347  if( rc==SQLITE_OK ){
78348    res = pModule->xEof(pVtabCursor);
78349  }
78350  pCur->nullRow = 0;
78351  VdbeBranchTaken(res!=0,2);
78352  if( res ) goto jump_to_p2;
78353  break;
78354}
78355#endif /* SQLITE_OMIT_VIRTUALTABLE */
78356
78357#ifndef SQLITE_OMIT_VIRTUALTABLE
78358/* Opcode: VColumn P1 P2 P3 * *
78359** Synopsis: r[P3]=vcolumn(P2)
78360**
78361** Store the value of the P2-th column of
78362** the row of the virtual-table that the
78363** P1 cursor is pointing to into register P3.
78364*/
78365case OP_VColumn: {
78366  sqlite3_vtab *pVtab;
78367  const sqlite3_module *pModule;
78368  Mem *pDest;
78369  sqlite3_context sContext;
78370
78371  VdbeCursor *pCur = p->apCsr[pOp->p1];
78372  assert( pCur->pVtabCursor );
78373  assert( pOp->p3>0 && pOp->p3<=(p->nMem-p->nCursor) );
78374  pDest = &aMem[pOp->p3];
78375  memAboutToChange(p, pDest);
78376  if( pCur->nullRow ){
78377    sqlite3VdbeMemSetNull(pDest);
78378    break;
78379  }
78380  pVtab = pCur->pVtabCursor->pVtab;
78381  pModule = pVtab->pModule;
78382  assert( pModule->xColumn );
78383  memset(&sContext, 0, sizeof(sContext));
78384  sContext.pOut = pDest;
78385  MemSetTypeFlag(pDest, MEM_Null);
78386  rc = pModule->xColumn(pCur->pVtabCursor, &sContext, pOp->p2);
78387  sqlite3VtabImportErrmsg(p, pVtab);
78388  if( sContext.isError ){
78389    rc = sContext.isError;
78390  }
78391  sqlite3VdbeChangeEncoding(pDest, encoding);
78392  REGISTER_TRACE(pOp->p3, pDest);
78393  UPDATE_MAX_BLOBSIZE(pDest);
78394
78395  if( sqlite3VdbeMemTooBig(pDest) ){
78396    goto too_big;
78397  }
78398  break;
78399}
78400#endif /* SQLITE_OMIT_VIRTUALTABLE */
78401
78402#ifndef SQLITE_OMIT_VIRTUALTABLE
78403/* Opcode: VNext P1 P2 * * *
78404**
78405** Advance virtual table P1 to the next row in its result set and
78406** jump to instruction P2.  Or, if the virtual table has reached
78407** the end of its result set, then fall through to the next instruction.
78408*/
78409case OP_VNext: {   /* jump */
78410  sqlite3_vtab *pVtab;
78411  const sqlite3_module *pModule;
78412  int res;
78413  VdbeCursor *pCur;
78414
78415  res = 0;
78416  pCur = p->apCsr[pOp->p1];
78417  assert( pCur->pVtabCursor );
78418  if( pCur->nullRow ){
78419    break;
78420  }
78421  pVtab = pCur->pVtabCursor->pVtab;
78422  pModule = pVtab->pModule;
78423  assert( pModule->xNext );
78424
78425  /* Invoke the xNext() method of the module. There is no way for the
78426  ** underlying implementation to return an error if one occurs during
78427  ** xNext(). Instead, if an error occurs, true is returned (indicating that
78428  ** data is available) and the error code returned when xColumn or
78429  ** some other method is next invoked on the save virtual table cursor.
78430  */
78431  rc = pModule->xNext(pCur->pVtabCursor);
78432  sqlite3VtabImportErrmsg(p, pVtab);
78433  if( rc==SQLITE_OK ){
78434    res = pModule->xEof(pCur->pVtabCursor);
78435  }
78436  VdbeBranchTaken(!res,2);
78437  if( !res ){
78438    /* If there is data, jump to P2 */
78439    goto jump_to_p2_and_check_for_interrupt;
78440  }
78441  goto check_for_interrupt;
78442}
78443#endif /* SQLITE_OMIT_VIRTUALTABLE */
78444
78445#ifndef SQLITE_OMIT_VIRTUALTABLE
78446/* Opcode: VRename P1 * * P4 *
78447**
78448** P4 is a pointer to a virtual table object, an sqlite3_vtab structure.
78449** This opcode invokes the corresponding xRename method. The value
78450** in register P1 is passed as the zName argument to the xRename method.
78451*/
78452case OP_VRename: {
78453  sqlite3_vtab *pVtab;
78454  Mem *pName;
78455
78456  pVtab = pOp->p4.pVtab->pVtab;
78457  pName = &aMem[pOp->p1];
78458  assert( pVtab->pModule->xRename );
78459  assert( memIsValid(pName) );
78460  assert( p->readOnly==0 );
78461  REGISTER_TRACE(pOp->p1, pName);
78462  assert( pName->flags & MEM_Str );
78463  testcase( pName->enc==SQLITE_UTF8 );
78464  testcase( pName->enc==SQLITE_UTF16BE );
78465  testcase( pName->enc==SQLITE_UTF16LE );
78466  rc = sqlite3VdbeChangeEncoding(pName, SQLITE_UTF8);
78467  if( rc==SQLITE_OK ){
78468    rc = pVtab->pModule->xRename(pVtab, pName->z);
78469    sqlite3VtabImportErrmsg(p, pVtab);
78470    p->expired = 0;
78471  }
78472  break;
78473}
78474#endif
78475
78476#ifndef SQLITE_OMIT_VIRTUALTABLE
78477/* Opcode: VUpdate P1 P2 P3 P4 P5
78478** Synopsis: data=r[P3@P2]
78479**
78480** P4 is a pointer to a virtual table object, an sqlite3_vtab structure.
78481** This opcode invokes the corresponding xUpdate method. P2 values
78482** are contiguous memory cells starting at P3 to pass to the xUpdate
78483** invocation. The value in register (P3+P2-1) corresponds to the
78484** p2th element of the argv array passed to xUpdate.
78485**
78486** The xUpdate method will do a DELETE or an INSERT or both.
78487** The argv[0] element (which corresponds to memory cell P3)
78488** is the rowid of a row to delete.  If argv[0] is NULL then no
78489** deletion occurs.  The argv[1] element is the rowid of the new
78490** row.  This can be NULL to have the virtual table select the new
78491** rowid for itself.  The subsequent elements in the array are
78492** the values of columns in the new row.
78493**
78494** If P2==1 then no insert is performed.  argv[0] is the rowid of
78495** a row to delete.
78496**
78497** P1 is a boolean flag. If it is set to true and the xUpdate call
78498** is successful, then the value returned by sqlite3_last_insert_rowid()
78499** is set to the value of the rowid for the row just inserted.
78500**
78501** P5 is the error actions (OE_Replace, OE_Fail, OE_Ignore, etc) to
78502** apply in the case of a constraint failure on an insert or update.
78503*/
78504case OP_VUpdate: {
78505  sqlite3_vtab *pVtab;
78506  const sqlite3_module *pModule;
78507  int nArg;
78508  int i;
78509  sqlite_int64 rowid;
78510  Mem **apArg;
78511  Mem *pX;
78512
78513  assert( pOp->p2==1        || pOp->p5==OE_Fail   || pOp->p5==OE_Rollback
78514       || pOp->p5==OE_Abort || pOp->p5==OE_Ignore || pOp->p5==OE_Replace
78515  );
78516  assert( p->readOnly==0 );
78517  pVtab = pOp->p4.pVtab->pVtab;
78518  if( pVtab==0 || NEVER(pVtab->pModule==0) ){
78519    rc = SQLITE_LOCKED;
78520    break;
78521  }
78522  pModule = pVtab->pModule;
78523  nArg = pOp->p2;
78524  assert( pOp->p4type==P4_VTAB );
78525  if( ALWAYS(pModule->xUpdate) ){
78526    u8 vtabOnConflict = db->vtabOnConflict;
78527    apArg = p->apArg;
78528    pX = &aMem[pOp->p3];
78529    for(i=0; i<nArg; i++){
78530      assert( memIsValid(pX) );
78531      memAboutToChange(p, pX);
78532      apArg[i] = pX;
78533      pX++;
78534    }
78535    db->vtabOnConflict = pOp->p5;
78536    rc = pModule->xUpdate(pVtab, nArg, apArg, &rowid);
78537    db->vtabOnConflict = vtabOnConflict;
78538    sqlite3VtabImportErrmsg(p, pVtab);
78539    if( rc==SQLITE_OK && pOp->p1 ){
78540      assert( nArg>1 && apArg[0] && (apArg[0]->flags&MEM_Null) );
78541      db->lastRowid = lastRowid = rowid;
78542    }
78543    if( (rc&0xff)==SQLITE_CONSTRAINT && pOp->p4.pVtab->bConstraint ){
78544      if( pOp->p5==OE_Ignore ){
78545        rc = SQLITE_OK;
78546      }else{
78547        p->errorAction = ((pOp->p5==OE_Replace) ? OE_Abort : pOp->p5);
78548      }
78549    }else{
78550      p->nChange++;
78551    }
78552  }
78553  break;
78554}
78555#endif /* SQLITE_OMIT_VIRTUALTABLE */
78556
78557#ifndef  SQLITE_OMIT_PAGER_PRAGMAS
78558/* Opcode: Pagecount P1 P2 * * *
78559**
78560** Write the current number of pages in database P1 to memory cell P2.
78561*/
78562case OP_Pagecount: {            /* out2 */
78563  pOut = out2Prerelease(p, pOp);
78564  pOut->u.i = sqlite3BtreeLastPage(db->aDb[pOp->p1].pBt);
78565  break;
78566}
78567#endif
78568
78569
78570#ifndef  SQLITE_OMIT_PAGER_PRAGMAS
78571/* Opcode: MaxPgcnt P1 P2 P3 * *
78572**
78573** Try to set the maximum page count for database P1 to the value in P3.
78574** Do not let the maximum page count fall below the current page count and
78575** do not change the maximum page count value if P3==0.
78576**
78577** Store the maximum page count after the change in register P2.
78578*/
78579case OP_MaxPgcnt: {            /* out2 */
78580  unsigned int newMax;
78581  Btree *pBt;
78582
78583  pOut = out2Prerelease(p, pOp);
78584  pBt = db->aDb[pOp->p1].pBt;
78585  newMax = 0;
78586  if( pOp->p3 ){
78587    newMax = sqlite3BtreeLastPage(pBt);
78588    if( newMax < (unsigned)pOp->p3 ) newMax = (unsigned)pOp->p3;
78589  }
78590  pOut->u.i = sqlite3BtreeMaxPageCount(pBt, newMax);
78591  break;
78592}
78593#endif
78594
78595
78596/* Opcode: Init * P2 * P4 *
78597** Synopsis:  Start at P2
78598**
78599** Programs contain a single instance of this opcode as the very first
78600** opcode.
78601**
78602** If tracing is enabled (by the sqlite3_trace()) interface, then
78603** the UTF-8 string contained in P4 is emitted on the trace callback.
78604** Or if P4 is blank, use the string returned by sqlite3_sql().
78605**
78606** If P2 is not zero, jump to instruction P2.
78607*/
78608case OP_Init: {          /* jump */
78609  char *zTrace;
78610  char *z;
78611
78612#ifndef SQLITE_OMIT_TRACE
78613  if( db->xTrace
78614   && !p->doingRerun
78615   && (zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql))!=0
78616  ){
78617    z = sqlite3VdbeExpandSql(p, zTrace);
78618    db->xTrace(db->pTraceArg, z);
78619    sqlite3DbFree(db, z);
78620  }
78621#ifdef SQLITE_USE_FCNTL_TRACE
78622  zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql);
78623  if( zTrace ){
78624    int i;
78625    for(i=0; i<db->nDb; i++){
78626      if( DbMaskTest(p->btreeMask, i)==0 ) continue;
78627      sqlite3_file_control(db, db->aDb[i].zName, SQLITE_FCNTL_TRACE, zTrace);
78628    }
78629  }
78630#endif /* SQLITE_USE_FCNTL_TRACE */
78631#ifdef SQLITE_DEBUG
78632  if( (db->flags & SQLITE_SqlTrace)!=0
78633   && (zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql))!=0
78634  ){
78635    sqlite3DebugPrintf("SQL-trace: %s\n", zTrace);
78636  }
78637#endif /* SQLITE_DEBUG */
78638#endif /* SQLITE_OMIT_TRACE */
78639  if( pOp->p2 ) goto jump_to_p2;
78640  break;
78641}
78642
78643
78644/* Opcode: Noop * * * * *
78645**
78646** Do nothing.  This instruction is often useful as a jump
78647** destination.
78648*/
78649/*
78650** The magic Explain opcode are only inserted when explain==2 (which
78651** is to say when the EXPLAIN QUERY PLAN syntax is used.)
78652** This opcode records information from the optimizer.  It is the
78653** the same as a no-op.  This opcodesnever appears in a real VM program.
78654*/
78655default: {          /* This is really OP_Noop and OP_Explain */
78656  assert( pOp->opcode==OP_Noop || pOp->opcode==OP_Explain );
78657  break;
78658}
78659
78660/*****************************************************************************
78661** The cases of the switch statement above this line should all be indented
78662** by 6 spaces.  But the left-most 6 spaces have been removed to improve the
78663** readability.  From this point on down, the normal indentation rules are
78664** restored.
78665*****************************************************************************/
78666    }
78667
78668#ifdef VDBE_PROFILE
78669    {
78670      u64 endTime = sqlite3Hwtime();
78671      if( endTime>start ) pOrigOp->cycles += endTime - start;
78672      pOrigOp->cnt++;
78673    }
78674#endif
78675
78676    /* The following code adds nothing to the actual functionality
78677    ** of the program.  It is only here for testing and debugging.
78678    ** On the other hand, it does burn CPU cycles every time through
78679    ** the evaluator loop.  So we can leave it out when NDEBUG is defined.
78680    */
78681#ifndef NDEBUG
78682    assert( pOp>=&aOp[-1] && pOp<&aOp[p->nOp-1] );
78683
78684#ifdef SQLITE_DEBUG
78685    if( db->flags & SQLITE_VdbeTrace ){
78686      if( rc!=0 ) printf("rc=%d\n",rc);
78687      if( pOrigOp->opflags & (OPFLG_OUT2) ){
78688        registerTrace(pOrigOp->p2, &aMem[pOrigOp->p2]);
78689      }
78690      if( pOrigOp->opflags & OPFLG_OUT3 ){
78691        registerTrace(pOrigOp->p3, &aMem[pOrigOp->p3]);
78692      }
78693    }
78694#endif  /* SQLITE_DEBUG */
78695#endif  /* NDEBUG */
78696  }  /* The end of the for(;;) loop the loops through opcodes */
78697
78698  /* If we reach this point, it means that execution is finished with
78699  ** an error of some kind.
78700  */
78701vdbe_error_halt:
78702  assert( rc );
78703  p->rc = rc;
78704  testcase( sqlite3GlobalConfig.xLog!=0 );
78705  sqlite3_log(rc, "statement aborts at %d: [%s] %s",
78706                   (int)(pOp - aOp), p->zSql, p->zErrMsg);
78707  sqlite3VdbeHalt(p);
78708  if( rc==SQLITE_IOERR_NOMEM ) db->mallocFailed = 1;
78709  rc = SQLITE_ERROR;
78710  if( resetSchemaOnFault>0 ){
78711    sqlite3ResetOneSchema(db, resetSchemaOnFault-1);
78712  }
78713
78714  /* This is the only way out of this procedure.  We have to
78715  ** release the mutexes on btrees that were acquired at the
78716  ** top. */
78717vdbe_return:
78718  db->lastRowid = lastRowid;
78719  testcase( nVmStep>0 );
78720  p->aCounter[SQLITE_STMTSTATUS_VM_STEP] += (int)nVmStep;
78721  sqlite3VdbeLeave(p);
78722  return rc;
78723
78724  /* Jump to here if a string or blob larger than SQLITE_MAX_LENGTH
78725  ** is encountered.
78726  */
78727too_big:
78728  sqlite3VdbeError(p, "string or blob too big");
78729  rc = SQLITE_TOOBIG;
78730  goto vdbe_error_halt;
78731
78732  /* Jump to here if a malloc() fails.
78733  */
78734no_mem:
78735  db->mallocFailed = 1;
78736  sqlite3VdbeError(p, "out of memory");
78737  rc = SQLITE_NOMEM;
78738  goto vdbe_error_halt;
78739
78740  /* Jump to here for any other kind of fatal error.  The "rc" variable
78741  ** should hold the error number.
78742  */
78743abort_due_to_error:
78744  assert( p->zErrMsg==0 );
78745  if( db->mallocFailed ) rc = SQLITE_NOMEM;
78746  if( rc!=SQLITE_IOERR_NOMEM ){
78747    sqlite3VdbeError(p, "%s", sqlite3ErrStr(rc));
78748  }
78749  goto vdbe_error_halt;
78750
78751  /* Jump to here if the sqlite3_interrupt() API sets the interrupt
78752  ** flag.
78753  */
78754abort_due_to_interrupt:
78755  assert( db->u1.isInterrupted );
78756  rc = SQLITE_INTERRUPT;
78757  p->rc = rc;
78758  sqlite3VdbeError(p, "%s", sqlite3ErrStr(rc));
78759  goto vdbe_error_halt;
78760}
78761
78762
78763/************** End of vdbe.c ************************************************/
78764/************** Begin file vdbeblob.c ****************************************/
78765/*
78766** 2007 May 1
78767**
78768** The author disclaims copyright to this source code.  In place of
78769** a legal notice, here is a blessing:
78770**
78771**    May you do good and not evil.
78772**    May you find forgiveness for yourself and forgive others.
78773**    May you share freely, never taking more than you give.
78774**
78775*************************************************************************
78776**
78777** This file contains code used to implement incremental BLOB I/O.
78778*/
78779
78780/* #include "sqliteInt.h" */
78781/* #include "vdbeInt.h" */
78782
78783#ifndef SQLITE_OMIT_INCRBLOB
78784
78785/*
78786** Valid sqlite3_blob* handles point to Incrblob structures.
78787*/
78788typedef struct Incrblob Incrblob;
78789struct Incrblob {
78790  int flags;              /* Copy of "flags" passed to sqlite3_blob_open() */
78791  int nByte;              /* Size of open blob, in bytes */
78792  int iOffset;            /* Byte offset of blob in cursor data */
78793  int iCol;               /* Table column this handle is open on */
78794  BtCursor *pCsr;         /* Cursor pointing at blob row */
78795  sqlite3_stmt *pStmt;    /* Statement holding cursor open */
78796  sqlite3 *db;            /* The associated database */
78797};
78798
78799
78800/*
78801** This function is used by both blob_open() and blob_reopen(). It seeks
78802** the b-tree cursor associated with blob handle p to point to row iRow.
78803** If successful, SQLITE_OK is returned and subsequent calls to
78804** sqlite3_blob_read() or sqlite3_blob_write() access the specified row.
78805**
78806** If an error occurs, or if the specified row does not exist or does not
78807** contain a value of type TEXT or BLOB in the column nominated when the
78808** blob handle was opened, then an error code is returned and *pzErr may
78809** be set to point to a buffer containing an error message. It is the
78810** responsibility of the caller to free the error message buffer using
78811** sqlite3DbFree().
78812**
78813** If an error does occur, then the b-tree cursor is closed. All subsequent
78814** calls to sqlite3_blob_read(), blob_write() or blob_reopen() will
78815** immediately return SQLITE_ABORT.
78816*/
78817static int blobSeekToRow(Incrblob *p, sqlite3_int64 iRow, char **pzErr){
78818  int rc;                         /* Error code */
78819  char *zErr = 0;                 /* Error message */
78820  Vdbe *v = (Vdbe *)p->pStmt;
78821
78822  /* Set the value of the SQL statements only variable to integer iRow.
78823  ** This is done directly instead of using sqlite3_bind_int64() to avoid
78824  ** triggering asserts related to mutexes.
78825  */
78826  assert( v->aVar[0].flags&MEM_Int );
78827  v->aVar[0].u.i = iRow;
78828
78829  rc = sqlite3_step(p->pStmt);
78830  if( rc==SQLITE_ROW ){
78831    VdbeCursor *pC = v->apCsr[0];
78832    u32 type = pC->aType[p->iCol];
78833    if( type<12 ){
78834      zErr = sqlite3MPrintf(p->db, "cannot open value of type %s",
78835          type==0?"null": type==7?"real": "integer"
78836      );
78837      rc = SQLITE_ERROR;
78838      sqlite3_finalize(p->pStmt);
78839      p->pStmt = 0;
78840    }else{
78841      p->iOffset = pC->aType[p->iCol + pC->nField];
78842      p->nByte = sqlite3VdbeSerialTypeLen(type);
78843      p->pCsr =  pC->pCursor;
78844      sqlite3BtreeIncrblobCursor(p->pCsr);
78845    }
78846  }
78847
78848  if( rc==SQLITE_ROW ){
78849    rc = SQLITE_OK;
78850  }else if( p->pStmt ){
78851    rc = sqlite3_finalize(p->pStmt);
78852    p->pStmt = 0;
78853    if( rc==SQLITE_OK ){
78854      zErr = sqlite3MPrintf(p->db, "no such rowid: %lld", iRow);
78855      rc = SQLITE_ERROR;
78856    }else{
78857      zErr = sqlite3MPrintf(p->db, "%s", sqlite3_errmsg(p->db));
78858    }
78859  }
78860
78861  assert( rc!=SQLITE_OK || zErr==0 );
78862  assert( rc!=SQLITE_ROW && rc!=SQLITE_DONE );
78863
78864  *pzErr = zErr;
78865  return rc;
78866}
78867
78868/*
78869** Open a blob handle.
78870*/
78871SQLITE_API int SQLITE_STDCALL sqlite3_blob_open(
78872  sqlite3* db,            /* The database connection */
78873  const char *zDb,        /* The attached database containing the blob */
78874  const char *zTable,     /* The table containing the blob */
78875  const char *zColumn,    /* The column containing the blob */
78876  sqlite_int64 iRow,      /* The row containing the glob */
78877  int flags,              /* True -> read/write access, false -> read-only */
78878  sqlite3_blob **ppBlob   /* Handle for accessing the blob returned here */
78879){
78880  int nAttempt = 0;
78881  int iCol;               /* Index of zColumn in row-record */
78882
78883  /* This VDBE program seeks a btree cursor to the identified
78884  ** db/table/row entry. The reason for using a vdbe program instead
78885  ** of writing code to use the b-tree layer directly is that the
78886  ** vdbe program will take advantage of the various transaction,
78887  ** locking and error handling infrastructure built into the vdbe.
78888  **
78889  ** After seeking the cursor, the vdbe executes an OP_ResultRow.
78890  ** Code external to the Vdbe then "borrows" the b-tree cursor and
78891  ** uses it to implement the blob_read(), blob_write() and
78892  ** blob_bytes() functions.
78893  **
78894  ** The sqlite3_blob_close() function finalizes the vdbe program,
78895  ** which closes the b-tree cursor and (possibly) commits the
78896  ** transaction.
78897  */
78898  static const int iLn = VDBE_OFFSET_LINENO(4);
78899  static const VdbeOpList openBlob[] = {
78900    /* {OP_Transaction, 0, 0, 0},  // 0: Inserted separately */
78901    {OP_TableLock, 0, 0, 0},       /* 1: Acquire a read or write lock */
78902    /* One of the following two instructions is replaced by an OP_Noop. */
78903    {OP_OpenRead, 0, 0, 0},        /* 2: Open cursor 0 for reading */
78904    {OP_OpenWrite, 0, 0, 0},       /* 3: Open cursor 0 for read/write */
78905    {OP_Variable, 1, 1, 1},        /* 4: Push the rowid to the stack */
78906    {OP_NotExists, 0, 10, 1},      /* 5: Seek the cursor */
78907    {OP_Column, 0, 0, 1},          /* 6  */
78908    {OP_ResultRow, 1, 0, 0},       /* 7  */
78909    {OP_Goto, 0, 4, 0},            /* 8  */
78910    {OP_Close, 0, 0, 0},           /* 9  */
78911    {OP_Halt, 0, 0, 0},            /* 10 */
78912  };
78913
78914  int rc = SQLITE_OK;
78915  char *zErr = 0;
78916  Table *pTab;
78917  Parse *pParse = 0;
78918  Incrblob *pBlob = 0;
78919
78920#ifdef SQLITE_ENABLE_API_ARMOR
78921  if( ppBlob==0 ){
78922    return SQLITE_MISUSE_BKPT;
78923  }
78924#endif
78925  *ppBlob = 0;
78926#ifdef SQLITE_ENABLE_API_ARMOR
78927  if( !sqlite3SafetyCheckOk(db) || zTable==0 ){
78928    return SQLITE_MISUSE_BKPT;
78929  }
78930#endif
78931  flags = !!flags;                /* flags = (flags ? 1 : 0); */
78932
78933  sqlite3_mutex_enter(db->mutex);
78934
78935  pBlob = (Incrblob *)sqlite3DbMallocZero(db, sizeof(Incrblob));
78936  if( !pBlob ) goto blob_open_out;
78937  pParse = sqlite3StackAllocRaw(db, sizeof(*pParse));
78938  if( !pParse ) goto blob_open_out;
78939
78940  do {
78941    memset(pParse, 0, sizeof(Parse));
78942    pParse->db = db;
78943    sqlite3DbFree(db, zErr);
78944    zErr = 0;
78945
78946    sqlite3BtreeEnterAll(db);
78947    pTab = sqlite3LocateTable(pParse, 0, zTable, zDb);
78948    if( pTab && IsVirtual(pTab) ){
78949      pTab = 0;
78950      sqlite3ErrorMsg(pParse, "cannot open virtual table: %s", zTable);
78951    }
78952    if( pTab && !HasRowid(pTab) ){
78953      pTab = 0;
78954      sqlite3ErrorMsg(pParse, "cannot open table without rowid: %s", zTable);
78955    }
78956#ifndef SQLITE_OMIT_VIEW
78957    if( pTab && pTab->pSelect ){
78958      pTab = 0;
78959      sqlite3ErrorMsg(pParse, "cannot open view: %s", zTable);
78960    }
78961#endif
78962    if( !pTab ){
78963      if( pParse->zErrMsg ){
78964        sqlite3DbFree(db, zErr);
78965        zErr = pParse->zErrMsg;
78966        pParse->zErrMsg = 0;
78967      }
78968      rc = SQLITE_ERROR;
78969      sqlite3BtreeLeaveAll(db);
78970      goto blob_open_out;
78971    }
78972
78973    /* Now search pTab for the exact column. */
78974    for(iCol=0; iCol<pTab->nCol; iCol++) {
78975      if( sqlite3StrICmp(pTab->aCol[iCol].zName, zColumn)==0 ){
78976        break;
78977      }
78978    }
78979    if( iCol==pTab->nCol ){
78980      sqlite3DbFree(db, zErr);
78981      zErr = sqlite3MPrintf(db, "no such column: \"%s\"", zColumn);
78982      rc = SQLITE_ERROR;
78983      sqlite3BtreeLeaveAll(db);
78984      goto blob_open_out;
78985    }
78986
78987    /* If the value is being opened for writing, check that the
78988    ** column is not indexed, and that it is not part of a foreign key.
78989    ** It is against the rules to open a column to which either of these
78990    ** descriptions applies for writing.  */
78991    if( flags ){
78992      const char *zFault = 0;
78993      Index *pIdx;
78994#ifndef SQLITE_OMIT_FOREIGN_KEY
78995      if( db->flags&SQLITE_ForeignKeys ){
78996        /* Check that the column is not part of an FK child key definition. It
78997        ** is not necessary to check if it is part of a parent key, as parent
78998        ** key columns must be indexed. The check below will pick up this
78999        ** case.  */
79000        FKey *pFKey;
79001        for(pFKey=pTab->pFKey; pFKey; pFKey=pFKey->pNextFrom){
79002          int j;
79003          for(j=0; j<pFKey->nCol; j++){
79004            if( pFKey->aCol[j].iFrom==iCol ){
79005              zFault = "foreign key";
79006            }
79007          }
79008        }
79009      }
79010#endif
79011      for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
79012        int j;
79013        for(j=0; j<pIdx->nKeyCol; j++){
79014          if( pIdx->aiColumn[j]==iCol ){
79015            zFault = "indexed";
79016          }
79017        }
79018      }
79019      if( zFault ){
79020        sqlite3DbFree(db, zErr);
79021        zErr = sqlite3MPrintf(db, "cannot open %s column for writing", zFault);
79022        rc = SQLITE_ERROR;
79023        sqlite3BtreeLeaveAll(db);
79024        goto blob_open_out;
79025      }
79026    }
79027
79028    pBlob->pStmt = (sqlite3_stmt *)sqlite3VdbeCreate(pParse);
79029    assert( pBlob->pStmt || db->mallocFailed );
79030    if( pBlob->pStmt ){
79031      Vdbe *v = (Vdbe *)pBlob->pStmt;
79032      int iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
79033
79034
79035      sqlite3VdbeAddOp4Int(v, OP_Transaction, iDb, flags,
79036                           pTab->pSchema->schema_cookie,
79037                           pTab->pSchema->iGeneration);
79038      sqlite3VdbeChangeP5(v, 1);
79039      sqlite3VdbeAddOpList(v, ArraySize(openBlob), openBlob, iLn);
79040
79041      /* Make sure a mutex is held on the table to be accessed */
79042      sqlite3VdbeUsesBtree(v, iDb);
79043
79044      /* Configure the OP_TableLock instruction */
79045#ifdef SQLITE_OMIT_SHARED_CACHE
79046      sqlite3VdbeChangeToNoop(v, 1);
79047#else
79048      sqlite3VdbeChangeP1(v, 1, iDb);
79049      sqlite3VdbeChangeP2(v, 1, pTab->tnum);
79050      sqlite3VdbeChangeP3(v, 1, flags);
79051      sqlite3VdbeChangeP4(v, 1, pTab->zName, P4_TRANSIENT);
79052#endif
79053
79054      /* Remove either the OP_OpenWrite or OpenRead. Set the P2
79055      ** parameter of the other to pTab->tnum.  */
79056      sqlite3VdbeChangeToNoop(v, 3 - flags);
79057      sqlite3VdbeChangeP2(v, 2 + flags, pTab->tnum);
79058      sqlite3VdbeChangeP3(v, 2 + flags, iDb);
79059
79060      /* Configure the number of columns. Configure the cursor to
79061      ** think that the table has one more column than it really
79062      ** does. An OP_Column to retrieve this imaginary column will
79063      ** always return an SQL NULL. This is useful because it means
79064      ** we can invoke OP_Column to fill in the vdbe cursors type
79065      ** and offset cache without causing any IO.
79066      */
79067      sqlite3VdbeChangeP4(v, 2+flags, SQLITE_INT_TO_PTR(pTab->nCol+1),P4_INT32);
79068      sqlite3VdbeChangeP2(v, 6, pTab->nCol);
79069      if( !db->mallocFailed ){
79070        pParse->nVar = 1;
79071        pParse->nMem = 1;
79072        pParse->nTab = 1;
79073        sqlite3VdbeMakeReady(v, pParse);
79074      }
79075    }
79076
79077    pBlob->flags = flags;
79078    pBlob->iCol = iCol;
79079    pBlob->db = db;
79080    sqlite3BtreeLeaveAll(db);
79081    if( db->mallocFailed ){
79082      goto blob_open_out;
79083    }
79084    sqlite3_bind_int64(pBlob->pStmt, 1, iRow);
79085    rc = blobSeekToRow(pBlob, iRow, &zErr);
79086  } while( (++nAttempt)<SQLITE_MAX_SCHEMA_RETRY && rc==SQLITE_SCHEMA );
79087
79088blob_open_out:
79089  if( rc==SQLITE_OK && db->mallocFailed==0 ){
79090    *ppBlob = (sqlite3_blob *)pBlob;
79091  }else{
79092    if( pBlob && pBlob->pStmt ) sqlite3VdbeFinalize((Vdbe *)pBlob->pStmt);
79093    sqlite3DbFree(db, pBlob);
79094  }
79095  sqlite3ErrorWithMsg(db, rc, (zErr ? "%s" : 0), zErr);
79096  sqlite3DbFree(db, zErr);
79097  sqlite3ParserReset(pParse);
79098  sqlite3StackFree(db, pParse);
79099  rc = sqlite3ApiExit(db, rc);
79100  sqlite3_mutex_leave(db->mutex);
79101  return rc;
79102}
79103
79104/*
79105** Close a blob handle that was previously created using
79106** sqlite3_blob_open().
79107*/
79108SQLITE_API int SQLITE_STDCALL sqlite3_blob_close(sqlite3_blob *pBlob){
79109  Incrblob *p = (Incrblob *)pBlob;
79110  int rc;
79111  sqlite3 *db;
79112
79113  if( p ){
79114    db = p->db;
79115    sqlite3_mutex_enter(db->mutex);
79116    rc = sqlite3_finalize(p->pStmt);
79117    sqlite3DbFree(db, p);
79118    sqlite3_mutex_leave(db->mutex);
79119  }else{
79120    rc = SQLITE_OK;
79121  }
79122  return rc;
79123}
79124
79125/*
79126** Perform a read or write operation on a blob
79127*/
79128static int blobReadWrite(
79129  sqlite3_blob *pBlob,
79130  void *z,
79131  int n,
79132  int iOffset,
79133  int (*xCall)(BtCursor*, u32, u32, void*)
79134){
79135  int rc;
79136  Incrblob *p = (Incrblob *)pBlob;
79137  Vdbe *v;
79138  sqlite3 *db;
79139
79140  if( p==0 ) return SQLITE_MISUSE_BKPT;
79141  db = p->db;
79142  sqlite3_mutex_enter(db->mutex);
79143  v = (Vdbe*)p->pStmt;
79144
79145  if( n<0 || iOffset<0 || ((sqlite3_int64)iOffset+n)>p->nByte ){
79146    /* Request is out of range. Return a transient error. */
79147    rc = SQLITE_ERROR;
79148  }else if( v==0 ){
79149    /* If there is no statement handle, then the blob-handle has
79150    ** already been invalidated. Return SQLITE_ABORT in this case.
79151    */
79152    rc = SQLITE_ABORT;
79153  }else{
79154    /* Call either BtreeData() or BtreePutData(). If SQLITE_ABORT is
79155    ** returned, clean-up the statement handle.
79156    */
79157    assert( db == v->db );
79158    sqlite3BtreeEnterCursor(p->pCsr);
79159    rc = xCall(p->pCsr, iOffset+p->iOffset, n, z);
79160    sqlite3BtreeLeaveCursor(p->pCsr);
79161    if( rc==SQLITE_ABORT ){
79162      sqlite3VdbeFinalize(v);
79163      p->pStmt = 0;
79164    }else{
79165      v->rc = rc;
79166    }
79167  }
79168  sqlite3Error(db, rc);
79169  rc = sqlite3ApiExit(db, rc);
79170  sqlite3_mutex_leave(db->mutex);
79171  return rc;
79172}
79173
79174/*
79175** Read data from a blob handle.
79176*/
79177SQLITE_API int SQLITE_STDCALL sqlite3_blob_read(sqlite3_blob *pBlob, void *z, int n, int iOffset){
79178  return blobReadWrite(pBlob, z, n, iOffset, sqlite3BtreeData);
79179}
79180
79181/*
79182** Write data to a blob handle.
79183*/
79184SQLITE_API int SQLITE_STDCALL sqlite3_blob_write(sqlite3_blob *pBlob, const void *z, int n, int iOffset){
79185  return blobReadWrite(pBlob, (void *)z, n, iOffset, sqlite3BtreePutData);
79186}
79187
79188/*
79189** Query a blob handle for the size of the data.
79190**
79191** The Incrblob.nByte field is fixed for the lifetime of the Incrblob
79192** so no mutex is required for access.
79193*/
79194SQLITE_API int SQLITE_STDCALL sqlite3_blob_bytes(sqlite3_blob *pBlob){
79195  Incrblob *p = (Incrblob *)pBlob;
79196  return (p && p->pStmt) ? p->nByte : 0;
79197}
79198
79199/*
79200** Move an existing blob handle to point to a different row of the same
79201** database table.
79202**
79203** If an error occurs, or if the specified row does not exist or does not
79204** contain a blob or text value, then an error code is returned and the
79205** database handle error code and message set. If this happens, then all
79206** subsequent calls to sqlite3_blob_xxx() functions (except blob_close())
79207** immediately return SQLITE_ABORT.
79208*/
79209SQLITE_API int SQLITE_STDCALL sqlite3_blob_reopen(sqlite3_blob *pBlob, sqlite3_int64 iRow){
79210  int rc;
79211  Incrblob *p = (Incrblob *)pBlob;
79212  sqlite3 *db;
79213
79214  if( p==0 ) return SQLITE_MISUSE_BKPT;
79215  db = p->db;
79216  sqlite3_mutex_enter(db->mutex);
79217
79218  if( p->pStmt==0 ){
79219    /* If there is no statement handle, then the blob-handle has
79220    ** already been invalidated. Return SQLITE_ABORT in this case.
79221    */
79222    rc = SQLITE_ABORT;
79223  }else{
79224    char *zErr;
79225    rc = blobSeekToRow(p, iRow, &zErr);
79226    if( rc!=SQLITE_OK ){
79227      sqlite3ErrorWithMsg(db, rc, (zErr ? "%s" : 0), zErr);
79228      sqlite3DbFree(db, zErr);
79229    }
79230    assert( rc!=SQLITE_SCHEMA );
79231  }
79232
79233  rc = sqlite3ApiExit(db, rc);
79234  assert( rc==SQLITE_OK || p->pStmt==0 );
79235  sqlite3_mutex_leave(db->mutex);
79236  return rc;
79237}
79238
79239#endif /* #ifndef SQLITE_OMIT_INCRBLOB */
79240
79241/************** End of vdbeblob.c ********************************************/
79242/************** Begin file vdbesort.c ****************************************/
79243/*
79244** 2011-07-09
79245**
79246** The author disclaims copyright to this source code.  In place of
79247** a legal notice, here is a blessing:
79248**
79249**    May you do good and not evil.
79250**    May you find forgiveness for yourself and forgive others.
79251**    May you share freely, never taking more than you give.
79252**
79253*************************************************************************
79254** This file contains code for the VdbeSorter object, used in concert with
79255** a VdbeCursor to sort large numbers of keys for CREATE INDEX statements
79256** or by SELECT statements with ORDER BY clauses that cannot be satisfied
79257** using indexes and without LIMIT clauses.
79258**
79259** The VdbeSorter object implements a multi-threaded external merge sort
79260** algorithm that is efficient even if the number of elements being sorted
79261** exceeds the available memory.
79262**
79263** Here is the (internal, non-API) interface between this module and the
79264** rest of the SQLite system:
79265**
79266**    sqlite3VdbeSorterInit()       Create a new VdbeSorter object.
79267**
79268**    sqlite3VdbeSorterWrite()      Add a single new row to the VdbeSorter
79269**                                  object.  The row is a binary blob in the
79270**                                  OP_MakeRecord format that contains both
79271**                                  the ORDER BY key columns and result columns
79272**                                  in the case of a SELECT w/ ORDER BY, or
79273**                                  the complete record for an index entry
79274**                                  in the case of a CREATE INDEX.
79275**
79276**    sqlite3VdbeSorterRewind()     Sort all content previously added.
79277**                                  Position the read cursor on the
79278**                                  first sorted element.
79279**
79280**    sqlite3VdbeSorterNext()       Advance the read cursor to the next sorted
79281**                                  element.
79282**
79283**    sqlite3VdbeSorterRowkey()     Return the complete binary blob for the
79284**                                  row currently under the read cursor.
79285**
79286**    sqlite3VdbeSorterCompare()    Compare the binary blob for the row
79287**                                  currently under the read cursor against
79288**                                  another binary blob X and report if
79289**                                  X is strictly less than the read cursor.
79290**                                  Used to enforce uniqueness in a
79291**                                  CREATE UNIQUE INDEX statement.
79292**
79293**    sqlite3VdbeSorterClose()      Close the VdbeSorter object and reclaim
79294**                                  all resources.
79295**
79296**    sqlite3VdbeSorterReset()      Refurbish the VdbeSorter for reuse.  This
79297**                                  is like Close() followed by Init() only
79298**                                  much faster.
79299**
79300** The interfaces above must be called in a particular order.  Write() can
79301** only occur in between Init()/Reset() and Rewind().  Next(), Rowkey(), and
79302** Compare() can only occur in between Rewind() and Close()/Reset(). i.e.
79303**
79304**   Init()
79305**   for each record: Write()
79306**   Rewind()
79307**     Rowkey()/Compare()
79308**   Next()
79309**   Close()
79310**
79311** Algorithm:
79312**
79313** Records passed to the sorter via calls to Write() are initially held
79314** unsorted in main memory. Assuming the amount of memory used never exceeds
79315** a threshold, when Rewind() is called the set of records is sorted using
79316** an in-memory merge sort. In this case, no temporary files are required
79317** and subsequent calls to Rowkey(), Next() and Compare() read records
79318** directly from main memory.
79319**
79320** If the amount of space used to store records in main memory exceeds the
79321** threshold, then the set of records currently in memory are sorted and
79322** written to a temporary file in "Packed Memory Array" (PMA) format.
79323** A PMA created at this point is known as a "level-0 PMA". Higher levels
79324** of PMAs may be created by merging existing PMAs together - for example
79325** merging two or more level-0 PMAs together creates a level-1 PMA.
79326**
79327** The threshold for the amount of main memory to use before flushing
79328** records to a PMA is roughly the same as the limit configured for the
79329** page-cache of the main database. Specifically, the threshold is set to
79330** the value returned by "PRAGMA main.page_size" multipled by
79331** that returned by "PRAGMA main.cache_size", in bytes.
79332**
79333** If the sorter is running in single-threaded mode, then all PMAs generated
79334** are appended to a single temporary file. Or, if the sorter is running in
79335** multi-threaded mode then up to (N+1) temporary files may be opened, where
79336** N is the configured number of worker threads. In this case, instead of
79337** sorting the records and writing the PMA to a temporary file itself, the
79338** calling thread usually launches a worker thread to do so. Except, if
79339** there are already N worker threads running, the main thread does the work
79340** itself.
79341**
79342** The sorter is running in multi-threaded mode if (a) the library was built
79343** with pre-processor symbol SQLITE_MAX_WORKER_THREADS set to a value greater
79344** than zero, and (b) worker threads have been enabled at runtime by calling
79345** "PRAGMA threads=N" with some value of N greater than 0.
79346**
79347** When Rewind() is called, any data remaining in memory is flushed to a
79348** final PMA. So at this point the data is stored in some number of sorted
79349** PMAs within temporary files on disk.
79350**
79351** If there are fewer than SORTER_MAX_MERGE_COUNT PMAs in total and the
79352** sorter is running in single-threaded mode, then these PMAs are merged
79353** incrementally as keys are retreived from the sorter by the VDBE.  The
79354** MergeEngine object, described in further detail below, performs this
79355** merge.
79356**
79357** Or, if running in multi-threaded mode, then a background thread is
79358** launched to merge the existing PMAs. Once the background thread has
79359** merged T bytes of data into a single sorted PMA, the main thread
79360** begins reading keys from that PMA while the background thread proceeds
79361** with merging the next T bytes of data. And so on.
79362**
79363** Parameter T is set to half the value of the memory threshold used
79364** by Write() above to determine when to create a new PMA.
79365**
79366** If there are more than SORTER_MAX_MERGE_COUNT PMAs in total when
79367** Rewind() is called, then a hierarchy of incremental-merges is used.
79368** First, T bytes of data from the first SORTER_MAX_MERGE_COUNT PMAs on
79369** disk are merged together. Then T bytes of data from the second set, and
79370** so on, such that no operation ever merges more than SORTER_MAX_MERGE_COUNT
79371** PMAs at a time. This done is to improve locality.
79372**
79373** If running in multi-threaded mode and there are more than
79374** SORTER_MAX_MERGE_COUNT PMAs on disk when Rewind() is called, then more
79375** than one background thread may be created. Specifically, there may be
79376** one background thread for each temporary file on disk, and one background
79377** thread to merge the output of each of the others to a single PMA for
79378** the main thread to read from.
79379*/
79380/* #include "sqliteInt.h" */
79381/* #include "vdbeInt.h" */
79382
79383/*
79384** If SQLITE_DEBUG_SORTER_THREADS is defined, this module outputs various
79385** messages to stderr that may be helpful in understanding the performance
79386** characteristics of the sorter in multi-threaded mode.
79387*/
79388#if 0
79389# define SQLITE_DEBUG_SORTER_THREADS 1
79390#endif
79391
79392/*
79393** Hard-coded maximum amount of data to accumulate in memory before flushing
79394** to a level 0 PMA. The purpose of this limit is to prevent various integer
79395** overflows. 512MiB.
79396*/
79397#define SQLITE_MAX_PMASZ    (1<<29)
79398
79399/*
79400** Private objects used by the sorter
79401*/
79402typedef struct MergeEngine MergeEngine;     /* Merge PMAs together */
79403typedef struct PmaReader PmaReader;         /* Incrementally read one PMA */
79404typedef struct PmaWriter PmaWriter;         /* Incrementally write one PMA */
79405typedef struct SorterRecord SorterRecord;   /* A record being sorted */
79406typedef struct SortSubtask SortSubtask;     /* A sub-task in the sort process */
79407typedef struct SorterFile SorterFile;       /* Temporary file object wrapper */
79408typedef struct SorterList SorterList;       /* In-memory list of records */
79409typedef struct IncrMerger IncrMerger;       /* Read & merge multiple PMAs */
79410
79411/*
79412** A container for a temp file handle and the current amount of data
79413** stored in the file.
79414*/
79415struct SorterFile {
79416  sqlite3_file *pFd;              /* File handle */
79417  i64 iEof;                       /* Bytes of data stored in pFd */
79418};
79419
79420/*
79421** An in-memory list of objects to be sorted.
79422**
79423** If aMemory==0 then each object is allocated separately and the objects
79424** are connected using SorterRecord.u.pNext.  If aMemory!=0 then all objects
79425** are stored in the aMemory[] bulk memory, one right after the other, and
79426** are connected using SorterRecord.u.iNext.
79427*/
79428struct SorterList {
79429  SorterRecord *pList;            /* Linked list of records */
79430  u8 *aMemory;                    /* If non-NULL, bulk memory to hold pList */
79431  int szPMA;                      /* Size of pList as PMA in bytes */
79432};
79433
79434/*
79435** The MergeEngine object is used to combine two or more smaller PMAs into
79436** one big PMA using a merge operation.  Separate PMAs all need to be
79437** combined into one big PMA in order to be able to step through the sorted
79438** records in order.
79439**
79440** The aReadr[] array contains a PmaReader object for each of the PMAs being
79441** merged.  An aReadr[] object either points to a valid key or else is at EOF.
79442** ("EOF" means "End Of File".  When aReadr[] is at EOF there is no more data.)
79443** For the purposes of the paragraphs below, we assume that the array is
79444** actually N elements in size, where N is the smallest power of 2 greater
79445** to or equal to the number of PMAs being merged. The extra aReadr[] elements
79446** are treated as if they are empty (always at EOF).
79447**
79448** The aTree[] array is also N elements in size. The value of N is stored in
79449** the MergeEngine.nTree variable.
79450**
79451** The final (N/2) elements of aTree[] contain the results of comparing
79452** pairs of PMA keys together. Element i contains the result of
79453** comparing aReadr[2*i-N] and aReadr[2*i-N+1]. Whichever key is smaller, the
79454** aTree element is set to the index of it.
79455**
79456** For the purposes of this comparison, EOF is considered greater than any
79457** other key value. If the keys are equal (only possible with two EOF
79458** values), it doesn't matter which index is stored.
79459**
79460** The (N/4) elements of aTree[] that precede the final (N/2) described
79461** above contains the index of the smallest of each block of 4 PmaReaders
79462** And so on. So that aTree[1] contains the index of the PmaReader that
79463** currently points to the smallest key value. aTree[0] is unused.
79464**
79465** Example:
79466**
79467**     aReadr[0] -> Banana
79468**     aReadr[1] -> Feijoa
79469**     aReadr[2] -> Elderberry
79470**     aReadr[3] -> Currant
79471**     aReadr[4] -> Grapefruit
79472**     aReadr[5] -> Apple
79473**     aReadr[6] -> Durian
79474**     aReadr[7] -> EOF
79475**
79476**     aTree[] = { X, 5   0, 5    0, 3, 5, 6 }
79477**
79478** The current element is "Apple" (the value of the key indicated by
79479** PmaReader 5). When the Next() operation is invoked, PmaReader 5 will
79480** be advanced to the next key in its segment. Say the next key is
79481** "Eggplant":
79482**
79483**     aReadr[5] -> Eggplant
79484**
79485** The contents of aTree[] are updated first by comparing the new PmaReader
79486** 5 key to the current key of PmaReader 4 (still "Grapefruit"). The PmaReader
79487** 5 value is still smaller, so aTree[6] is set to 5. And so on up the tree.
79488** The value of PmaReader 6 - "Durian" - is now smaller than that of PmaReader
79489** 5, so aTree[3] is set to 6. Key 0 is smaller than key 6 (Banana<Durian),
79490** so the value written into element 1 of the array is 0. As follows:
79491**
79492**     aTree[] = { X, 0   0, 6    0, 3, 5, 6 }
79493**
79494** In other words, each time we advance to the next sorter element, log2(N)
79495** key comparison operations are required, where N is the number of segments
79496** being merged (rounded up to the next power of 2).
79497*/
79498struct MergeEngine {
79499  int nTree;                 /* Used size of aTree/aReadr (power of 2) */
79500  SortSubtask *pTask;        /* Used by this thread only */
79501  int *aTree;                /* Current state of incremental merge */
79502  PmaReader *aReadr;         /* Array of PmaReaders to merge data from */
79503};
79504
79505/*
79506** This object represents a single thread of control in a sort operation.
79507** Exactly VdbeSorter.nTask instances of this object are allocated
79508** as part of each VdbeSorter object. Instances are never allocated any
79509** other way. VdbeSorter.nTask is set to the number of worker threads allowed
79510** (see SQLITE_CONFIG_WORKER_THREADS) plus one (the main thread).  Thus for
79511** single-threaded operation, there is exactly one instance of this object
79512** and for multi-threaded operation there are two or more instances.
79513**
79514** Essentially, this structure contains all those fields of the VdbeSorter
79515** structure for which each thread requires a separate instance. For example,
79516** each thread requries its own UnpackedRecord object to unpack records in
79517** as part of comparison operations.
79518**
79519** Before a background thread is launched, variable bDone is set to 0. Then,
79520** right before it exits, the thread itself sets bDone to 1. This is used for
79521** two purposes:
79522**
79523**   1. When flushing the contents of memory to a level-0 PMA on disk, to
79524**      attempt to select a SortSubtask for which there is not already an
79525**      active background thread (since doing so causes the main thread
79526**      to block until it finishes).
79527**
79528**   2. If SQLITE_DEBUG_SORTER_THREADS is defined, to determine if a call
79529**      to sqlite3ThreadJoin() is likely to block. Cases that are likely to
79530**      block provoke debugging output.
79531**
79532** In both cases, the effects of the main thread seeing (bDone==0) even
79533** after the thread has finished are not dire. So we don't worry about
79534** memory barriers and such here.
79535*/
79536typedef int (*SorterCompare)(SortSubtask*,int*,const void*,int,const void*,int);
79537struct SortSubtask {
79538  SQLiteThread *pThread;          /* Background thread, if any */
79539  int bDone;                      /* Set if thread is finished but not joined */
79540  VdbeSorter *pSorter;            /* Sorter that owns this sub-task */
79541  UnpackedRecord *pUnpacked;      /* Space to unpack a record */
79542  SorterList list;                /* List for thread to write to a PMA */
79543  int nPMA;                       /* Number of PMAs currently in file */
79544  SorterCompare xCompare;         /* Compare function to use */
79545  SorterFile file;                /* Temp file for level-0 PMAs */
79546  SorterFile file2;               /* Space for other PMAs */
79547};
79548
79549
79550/*
79551** Main sorter structure. A single instance of this is allocated for each
79552** sorter cursor created by the VDBE.
79553**
79554** mxKeysize:
79555**   As records are added to the sorter by calls to sqlite3VdbeSorterWrite(),
79556**   this variable is updated so as to be set to the size on disk of the
79557**   largest record in the sorter.
79558*/
79559struct VdbeSorter {
79560  int mnPmaSize;                  /* Minimum PMA size, in bytes */
79561  int mxPmaSize;                  /* Maximum PMA size, in bytes.  0==no limit */
79562  int mxKeysize;                  /* Largest serialized key seen so far */
79563  int pgsz;                       /* Main database page size */
79564  PmaReader *pReader;             /* Readr data from here after Rewind() */
79565  MergeEngine *pMerger;           /* Or here, if bUseThreads==0 */
79566  sqlite3 *db;                    /* Database connection */
79567  KeyInfo *pKeyInfo;              /* How to compare records */
79568  UnpackedRecord *pUnpacked;      /* Used by VdbeSorterCompare() */
79569  SorterList list;                /* List of in-memory records */
79570  int iMemory;                    /* Offset of free space in list.aMemory */
79571  int nMemory;                    /* Size of list.aMemory allocation in bytes */
79572  u8 bUsePMA;                     /* True if one or more PMAs created */
79573  u8 bUseThreads;                 /* True to use background threads */
79574  u8 iPrev;                       /* Previous thread used to flush PMA */
79575  u8 nTask;                       /* Size of aTask[] array */
79576  u8 typeMask;
79577  SortSubtask aTask[1];           /* One or more subtasks */
79578};
79579
79580#define SORTER_TYPE_INTEGER 0x01
79581#define SORTER_TYPE_TEXT    0x02
79582
79583/*
79584** An instance of the following object is used to read records out of a
79585** PMA, in sorted order.  The next key to be read is cached in nKey/aKey.
79586** aKey might point into aMap or into aBuffer.  If neither of those locations
79587** contain a contiguous representation of the key, then aAlloc is allocated
79588** and the key is copied into aAlloc and aKey is made to poitn to aAlloc.
79589**
79590** pFd==0 at EOF.
79591*/
79592struct PmaReader {
79593  i64 iReadOff;               /* Current read offset */
79594  i64 iEof;                   /* 1 byte past EOF for this PmaReader */
79595  int nAlloc;                 /* Bytes of space at aAlloc */
79596  int nKey;                   /* Number of bytes in key */
79597  sqlite3_file *pFd;          /* File handle we are reading from */
79598  u8 *aAlloc;                 /* Space for aKey if aBuffer and pMap wont work */
79599  u8 *aKey;                   /* Pointer to current key */
79600  u8 *aBuffer;                /* Current read buffer */
79601  int nBuffer;                /* Size of read buffer in bytes */
79602  u8 *aMap;                   /* Pointer to mapping of entire file */
79603  IncrMerger *pIncr;          /* Incremental merger */
79604};
79605
79606/*
79607** Normally, a PmaReader object iterates through an existing PMA stored
79608** within a temp file. However, if the PmaReader.pIncr variable points to
79609** an object of the following type, it may be used to iterate/merge through
79610** multiple PMAs simultaneously.
79611**
79612** There are two types of IncrMerger object - single (bUseThread==0) and
79613** multi-threaded (bUseThread==1).
79614**
79615** A multi-threaded IncrMerger object uses two temporary files - aFile[0]
79616** and aFile[1]. Neither file is allowed to grow to more than mxSz bytes in
79617** size. When the IncrMerger is initialized, it reads enough data from
79618** pMerger to populate aFile[0]. It then sets variables within the
79619** corresponding PmaReader object to read from that file and kicks off
79620** a background thread to populate aFile[1] with the next mxSz bytes of
79621** sorted record data from pMerger.
79622**
79623** When the PmaReader reaches the end of aFile[0], it blocks until the
79624** background thread has finished populating aFile[1]. It then exchanges
79625** the contents of the aFile[0] and aFile[1] variables within this structure,
79626** sets the PmaReader fields to read from the new aFile[0] and kicks off
79627** another background thread to populate the new aFile[1]. And so on, until
79628** the contents of pMerger are exhausted.
79629**
79630** A single-threaded IncrMerger does not open any temporary files of its
79631** own. Instead, it has exclusive access to mxSz bytes of space beginning
79632** at offset iStartOff of file pTask->file2. And instead of using a
79633** background thread to prepare data for the PmaReader, with a single
79634** threaded IncrMerger the allocate part of pTask->file2 is "refilled" with
79635** keys from pMerger by the calling thread whenever the PmaReader runs out
79636** of data.
79637*/
79638struct IncrMerger {
79639  SortSubtask *pTask;             /* Task that owns this merger */
79640  MergeEngine *pMerger;           /* Merge engine thread reads data from */
79641  i64 iStartOff;                  /* Offset to start writing file at */
79642  int mxSz;                       /* Maximum bytes of data to store */
79643  int bEof;                       /* Set to true when merge is finished */
79644  int bUseThread;                 /* True to use a bg thread for this object */
79645  SorterFile aFile[2];            /* aFile[0] for reading, [1] for writing */
79646};
79647
79648/*
79649** An instance of this object is used for writing a PMA.
79650**
79651** The PMA is written one record at a time.  Each record is of an arbitrary
79652** size.  But I/O is more efficient if it occurs in page-sized blocks where
79653** each block is aligned on a page boundary.  This object caches writes to
79654** the PMA so that aligned, page-size blocks are written.
79655*/
79656struct PmaWriter {
79657  int eFWErr;                     /* Non-zero if in an error state */
79658  u8 *aBuffer;                    /* Pointer to write buffer */
79659  int nBuffer;                    /* Size of write buffer in bytes */
79660  int iBufStart;                  /* First byte of buffer to write */
79661  int iBufEnd;                    /* Last byte of buffer to write */
79662  i64 iWriteOff;                  /* Offset of start of buffer in file */
79663  sqlite3_file *pFd;              /* File handle to write to */
79664};
79665
79666/*
79667** This object is the header on a single record while that record is being
79668** held in memory and prior to being written out as part of a PMA.
79669**
79670** How the linked list is connected depends on how memory is being managed
79671** by this module. If using a separate allocation for each in-memory record
79672** (VdbeSorter.list.aMemory==0), then the list is always connected using the
79673** SorterRecord.u.pNext pointers.
79674**
79675** Or, if using the single large allocation method (VdbeSorter.list.aMemory!=0),
79676** then while records are being accumulated the list is linked using the
79677** SorterRecord.u.iNext offset. This is because the aMemory[] array may
79678** be sqlite3Realloc()ed while records are being accumulated. Once the VM
79679** has finished passing records to the sorter, or when the in-memory buffer
79680** is full, the list is sorted. As part of the sorting process, it is
79681** converted to use the SorterRecord.u.pNext pointers. See function
79682** vdbeSorterSort() for details.
79683*/
79684struct SorterRecord {
79685  int nVal;                       /* Size of the record in bytes */
79686  union {
79687    SorterRecord *pNext;          /* Pointer to next record in list */
79688    int iNext;                    /* Offset within aMemory of next record */
79689  } u;
79690  /* The data for the record immediately follows this header */
79691};
79692
79693/* Return a pointer to the buffer containing the record data for SorterRecord
79694** object p. Should be used as if:
79695**
79696**   void *SRVAL(SorterRecord *p) { return (void*)&p[1]; }
79697*/
79698#define SRVAL(p) ((void*)((SorterRecord*)(p) + 1))
79699
79700
79701/* Maximum number of PMAs that a single MergeEngine can merge */
79702#define SORTER_MAX_MERGE_COUNT 16
79703
79704static int vdbeIncrSwap(IncrMerger*);
79705static void vdbeIncrFree(IncrMerger *);
79706
79707/*
79708** Free all memory belonging to the PmaReader object passed as the
79709** argument. All structure fields are set to zero before returning.
79710*/
79711static void vdbePmaReaderClear(PmaReader *pReadr){
79712  sqlite3_free(pReadr->aAlloc);
79713  sqlite3_free(pReadr->aBuffer);
79714  if( pReadr->aMap ) sqlite3OsUnfetch(pReadr->pFd, 0, pReadr->aMap);
79715  vdbeIncrFree(pReadr->pIncr);
79716  memset(pReadr, 0, sizeof(PmaReader));
79717}
79718
79719/*
79720** Read the next nByte bytes of data from the PMA p.
79721** If successful, set *ppOut to point to a buffer containing the data
79722** and return SQLITE_OK. Otherwise, if an error occurs, return an SQLite
79723** error code.
79724**
79725** The buffer returned in *ppOut is only valid until the
79726** next call to this function.
79727*/
79728static int vdbePmaReadBlob(
79729  PmaReader *p,                   /* PmaReader from which to take the blob */
79730  int nByte,                      /* Bytes of data to read */
79731  u8 **ppOut                      /* OUT: Pointer to buffer containing data */
79732){
79733  int iBuf;                       /* Offset within buffer to read from */
79734  int nAvail;                     /* Bytes of data available in buffer */
79735
79736  if( p->aMap ){
79737    *ppOut = &p->aMap[p->iReadOff];
79738    p->iReadOff += nByte;
79739    return SQLITE_OK;
79740  }
79741
79742  assert( p->aBuffer );
79743
79744  /* If there is no more data to be read from the buffer, read the next
79745  ** p->nBuffer bytes of data from the file into it. Or, if there are less
79746  ** than p->nBuffer bytes remaining in the PMA, read all remaining data.  */
79747  iBuf = p->iReadOff % p->nBuffer;
79748  if( iBuf==0 ){
79749    int nRead;                    /* Bytes to read from disk */
79750    int rc;                       /* sqlite3OsRead() return code */
79751
79752    /* Determine how many bytes of data to read. */
79753    if( (p->iEof - p->iReadOff) > (i64)p->nBuffer ){
79754      nRead = p->nBuffer;
79755    }else{
79756      nRead = (int)(p->iEof - p->iReadOff);
79757    }
79758    assert( nRead>0 );
79759
79760    /* Readr data from the file. Return early if an error occurs. */
79761    rc = sqlite3OsRead(p->pFd, p->aBuffer, nRead, p->iReadOff);
79762    assert( rc!=SQLITE_IOERR_SHORT_READ );
79763    if( rc!=SQLITE_OK ) return rc;
79764  }
79765  nAvail = p->nBuffer - iBuf;
79766
79767  if( nByte<=nAvail ){
79768    /* The requested data is available in the in-memory buffer. In this
79769    ** case there is no need to make a copy of the data, just return a
79770    ** pointer into the buffer to the caller.  */
79771    *ppOut = &p->aBuffer[iBuf];
79772    p->iReadOff += nByte;
79773  }else{
79774    /* The requested data is not all available in the in-memory buffer.
79775    ** In this case, allocate space at p->aAlloc[] to copy the requested
79776    ** range into. Then return a copy of pointer p->aAlloc to the caller.  */
79777    int nRem;                     /* Bytes remaining to copy */
79778
79779    /* Extend the p->aAlloc[] allocation if required. */
79780    if( p->nAlloc<nByte ){
79781      u8 *aNew;
79782      int nNew = MAX(128, p->nAlloc*2);
79783      while( nByte>nNew ) nNew = nNew*2;
79784      aNew = sqlite3Realloc(p->aAlloc, nNew);
79785      if( !aNew ) return SQLITE_NOMEM;
79786      p->nAlloc = nNew;
79787      p->aAlloc = aNew;
79788    }
79789
79790    /* Copy as much data as is available in the buffer into the start of
79791    ** p->aAlloc[].  */
79792    memcpy(p->aAlloc, &p->aBuffer[iBuf], nAvail);
79793    p->iReadOff += nAvail;
79794    nRem = nByte - nAvail;
79795
79796    /* The following loop copies up to p->nBuffer bytes per iteration into
79797    ** the p->aAlloc[] buffer.  */
79798    while( nRem>0 ){
79799      int rc;                     /* vdbePmaReadBlob() return code */
79800      int nCopy;                  /* Number of bytes to copy */
79801      u8 *aNext;                  /* Pointer to buffer to copy data from */
79802
79803      nCopy = nRem;
79804      if( nRem>p->nBuffer ) nCopy = p->nBuffer;
79805      rc = vdbePmaReadBlob(p, nCopy, &aNext);
79806      if( rc!=SQLITE_OK ) return rc;
79807      assert( aNext!=p->aAlloc );
79808      memcpy(&p->aAlloc[nByte - nRem], aNext, nCopy);
79809      nRem -= nCopy;
79810    }
79811
79812    *ppOut = p->aAlloc;
79813  }
79814
79815  return SQLITE_OK;
79816}
79817
79818/*
79819** Read a varint from the stream of data accessed by p. Set *pnOut to
79820** the value read.
79821*/
79822static int vdbePmaReadVarint(PmaReader *p, u64 *pnOut){
79823  int iBuf;
79824
79825  if( p->aMap ){
79826    p->iReadOff += sqlite3GetVarint(&p->aMap[p->iReadOff], pnOut);
79827  }else{
79828    iBuf = p->iReadOff % p->nBuffer;
79829    if( iBuf && (p->nBuffer-iBuf)>=9 ){
79830      p->iReadOff += sqlite3GetVarint(&p->aBuffer[iBuf], pnOut);
79831    }else{
79832      u8 aVarint[16], *a;
79833      int i = 0, rc;
79834      do{
79835        rc = vdbePmaReadBlob(p, 1, &a);
79836        if( rc ) return rc;
79837        aVarint[(i++)&0xf] = a[0];
79838      }while( (a[0]&0x80)!=0 );
79839      sqlite3GetVarint(aVarint, pnOut);
79840    }
79841  }
79842
79843  return SQLITE_OK;
79844}
79845
79846/*
79847** Attempt to memory map file pFile. If successful, set *pp to point to the
79848** new mapping and return SQLITE_OK. If the mapping is not attempted
79849** (because the file is too large or the VFS layer is configured not to use
79850** mmap), return SQLITE_OK and set *pp to NULL.
79851**
79852** Or, if an error occurs, return an SQLite error code. The final value of
79853** *pp is undefined in this case.
79854*/
79855static int vdbeSorterMapFile(SortSubtask *pTask, SorterFile *pFile, u8 **pp){
79856  int rc = SQLITE_OK;
79857  if( pFile->iEof<=(i64)(pTask->pSorter->db->nMaxSorterMmap) ){
79858    sqlite3_file *pFd = pFile->pFd;
79859    if( pFd->pMethods->iVersion>=3 ){
79860      rc = sqlite3OsFetch(pFd, 0, (int)pFile->iEof, (void**)pp);
79861      testcase( rc!=SQLITE_OK );
79862    }
79863  }
79864  return rc;
79865}
79866
79867/*
79868** Attach PmaReader pReadr to file pFile (if it is not already attached to
79869** that file) and seek it to offset iOff within the file.  Return SQLITE_OK
79870** if successful, or an SQLite error code if an error occurs.
79871*/
79872static int vdbePmaReaderSeek(
79873  SortSubtask *pTask,             /* Task context */
79874  PmaReader *pReadr,              /* Reader whose cursor is to be moved */
79875  SorterFile *pFile,              /* Sorter file to read from */
79876  i64 iOff                        /* Offset in pFile */
79877){
79878  int rc = SQLITE_OK;
79879
79880  assert( pReadr->pIncr==0 || pReadr->pIncr->bEof==0 );
79881
79882  if( sqlite3FaultSim(201) ) return SQLITE_IOERR_READ;
79883  if( pReadr->aMap ){
79884    sqlite3OsUnfetch(pReadr->pFd, 0, pReadr->aMap);
79885    pReadr->aMap = 0;
79886  }
79887  pReadr->iReadOff = iOff;
79888  pReadr->iEof = pFile->iEof;
79889  pReadr->pFd = pFile->pFd;
79890
79891  rc = vdbeSorterMapFile(pTask, pFile, &pReadr->aMap);
79892  if( rc==SQLITE_OK && pReadr->aMap==0 ){
79893    int pgsz = pTask->pSorter->pgsz;
79894    int iBuf = pReadr->iReadOff % pgsz;
79895    if( pReadr->aBuffer==0 ){
79896      pReadr->aBuffer = (u8*)sqlite3Malloc(pgsz);
79897      if( pReadr->aBuffer==0 ) rc = SQLITE_NOMEM;
79898      pReadr->nBuffer = pgsz;
79899    }
79900    if( rc==SQLITE_OK && iBuf ){
79901      int nRead = pgsz - iBuf;
79902      if( (pReadr->iReadOff + nRead) > pReadr->iEof ){
79903        nRead = (int)(pReadr->iEof - pReadr->iReadOff);
79904      }
79905      rc = sqlite3OsRead(
79906          pReadr->pFd, &pReadr->aBuffer[iBuf], nRead, pReadr->iReadOff
79907      );
79908      testcase( rc!=SQLITE_OK );
79909    }
79910  }
79911
79912  return rc;
79913}
79914
79915/*
79916** Advance PmaReader pReadr to the next key in its PMA. Return SQLITE_OK if
79917** no error occurs, or an SQLite error code if one does.
79918*/
79919static int vdbePmaReaderNext(PmaReader *pReadr){
79920  int rc = SQLITE_OK;             /* Return Code */
79921  u64 nRec = 0;                   /* Size of record in bytes */
79922
79923
79924  if( pReadr->iReadOff>=pReadr->iEof ){
79925    IncrMerger *pIncr = pReadr->pIncr;
79926    int bEof = 1;
79927    if( pIncr ){
79928      rc = vdbeIncrSwap(pIncr);
79929      if( rc==SQLITE_OK && pIncr->bEof==0 ){
79930        rc = vdbePmaReaderSeek(
79931            pIncr->pTask, pReadr, &pIncr->aFile[0], pIncr->iStartOff
79932        );
79933        bEof = 0;
79934      }
79935    }
79936
79937    if( bEof ){
79938      /* This is an EOF condition */
79939      vdbePmaReaderClear(pReadr);
79940      testcase( rc!=SQLITE_OK );
79941      return rc;
79942    }
79943  }
79944
79945  if( rc==SQLITE_OK ){
79946    rc = vdbePmaReadVarint(pReadr, &nRec);
79947  }
79948  if( rc==SQLITE_OK ){
79949    pReadr->nKey = (int)nRec;
79950    rc = vdbePmaReadBlob(pReadr, (int)nRec, &pReadr->aKey);
79951    testcase( rc!=SQLITE_OK );
79952  }
79953
79954  return rc;
79955}
79956
79957/*
79958** Initialize PmaReader pReadr to scan through the PMA stored in file pFile
79959** starting at offset iStart and ending at offset iEof-1. This function
79960** leaves the PmaReader pointing to the first key in the PMA (or EOF if the
79961** PMA is empty).
79962**
79963** If the pnByte parameter is NULL, then it is assumed that the file
79964** contains a single PMA, and that that PMA omits the initial length varint.
79965*/
79966static int vdbePmaReaderInit(
79967  SortSubtask *pTask,             /* Task context */
79968  SorterFile *pFile,              /* Sorter file to read from */
79969  i64 iStart,                     /* Start offset in pFile */
79970  PmaReader *pReadr,              /* PmaReader to populate */
79971  i64 *pnByte                     /* IN/OUT: Increment this value by PMA size */
79972){
79973  int rc;
79974
79975  assert( pFile->iEof>iStart );
79976  assert( pReadr->aAlloc==0 && pReadr->nAlloc==0 );
79977  assert( pReadr->aBuffer==0 );
79978  assert( pReadr->aMap==0 );
79979
79980  rc = vdbePmaReaderSeek(pTask, pReadr, pFile, iStart);
79981  if( rc==SQLITE_OK ){
79982    u64 nByte;                    /* Size of PMA in bytes */
79983    rc = vdbePmaReadVarint(pReadr, &nByte);
79984    pReadr->iEof = pReadr->iReadOff + nByte;
79985    *pnByte += nByte;
79986  }
79987
79988  if( rc==SQLITE_OK ){
79989    rc = vdbePmaReaderNext(pReadr);
79990  }
79991  return rc;
79992}
79993
79994/*
79995** A version of vdbeSorterCompare() that assumes that it has already been
79996** determined that the first field of key1 is equal to the first field of
79997** key2.
79998*/
79999static int vdbeSorterCompareTail(
80000  SortSubtask *pTask,             /* Subtask context (for pKeyInfo) */
80001  int *pbKey2Cached,              /* True if pTask->pUnpacked is pKey2 */
80002  const void *pKey1, int nKey1,   /* Left side of comparison */
80003  const void *pKey2, int nKey2    /* Right side of comparison */
80004){
80005  UnpackedRecord *r2 = pTask->pUnpacked;
80006  if( *pbKey2Cached==0 ){
80007    sqlite3VdbeRecordUnpack(pTask->pSorter->pKeyInfo, nKey2, pKey2, r2);
80008    *pbKey2Cached = 1;
80009  }
80010  return sqlite3VdbeRecordCompareWithSkip(nKey1, pKey1, r2, 1);
80011}
80012
80013/*
80014** Compare key1 (buffer pKey1, size nKey1 bytes) with key2 (buffer pKey2,
80015** size nKey2 bytes). Use (pTask->pKeyInfo) for the collation sequences
80016** used by the comparison. Return the result of the comparison.
80017**
80018** If IN/OUT parameter *pbKey2Cached is true when this function is called,
80019** it is assumed that (pTask->pUnpacked) contains the unpacked version
80020** of key2. If it is false, (pTask->pUnpacked) is populated with the unpacked
80021** version of key2 and *pbKey2Cached set to true before returning.
80022**
80023** If an OOM error is encountered, (pTask->pUnpacked->error_rc) is set
80024** to SQLITE_NOMEM.
80025*/
80026static int vdbeSorterCompare(
80027  SortSubtask *pTask,             /* Subtask context (for pKeyInfo) */
80028  int *pbKey2Cached,              /* True if pTask->pUnpacked is pKey2 */
80029  const void *pKey1, int nKey1,   /* Left side of comparison */
80030  const void *pKey2, int nKey2    /* Right side of comparison */
80031){
80032  UnpackedRecord *r2 = pTask->pUnpacked;
80033  if( !*pbKey2Cached ){
80034    sqlite3VdbeRecordUnpack(pTask->pSorter->pKeyInfo, nKey2, pKey2, r2);
80035    *pbKey2Cached = 1;
80036  }
80037  return sqlite3VdbeRecordCompare(nKey1, pKey1, r2);
80038}
80039
80040/*
80041** A specially optimized version of vdbeSorterCompare() that assumes that
80042** the first field of each key is a TEXT value and that the collation
80043** sequence to compare them with is BINARY.
80044*/
80045static int vdbeSorterCompareText(
80046  SortSubtask *pTask,             /* Subtask context (for pKeyInfo) */
80047  int *pbKey2Cached,              /* True if pTask->pUnpacked is pKey2 */
80048  const void *pKey1, int nKey1,   /* Left side of comparison */
80049  const void *pKey2, int nKey2    /* Right side of comparison */
80050){
80051  const u8 * const p1 = (const u8 * const)pKey1;
80052  const u8 * const p2 = (const u8 * const)pKey2;
80053  const u8 * const v1 = &p1[ p1[0] ];   /* Pointer to value 1 */
80054  const u8 * const v2 = &p2[ p2[0] ];   /* Pointer to value 2 */
80055
80056  int n1;
80057  int n2;
80058  int res;
80059
80060  getVarint32(&p1[1], n1); n1 = (n1 - 13) / 2;
80061  getVarint32(&p2[1], n2); n2 = (n2 - 13) / 2;
80062  res = memcmp(v1, v2, MIN(n1, n2));
80063  if( res==0 ){
80064    res = n1 - n2;
80065  }
80066
80067  if( res==0 ){
80068    if( pTask->pSorter->pKeyInfo->nField>1 ){
80069      res = vdbeSorterCompareTail(
80070          pTask, pbKey2Cached, pKey1, nKey1, pKey2, nKey2
80071      );
80072    }
80073  }else{
80074    if( pTask->pSorter->pKeyInfo->aSortOrder[0] ){
80075      res = res * -1;
80076    }
80077  }
80078
80079  return res;
80080}
80081
80082/*
80083** A specially optimized version of vdbeSorterCompare() that assumes that
80084** the first field of each key is an INTEGER value.
80085*/
80086static int vdbeSorterCompareInt(
80087  SortSubtask *pTask,             /* Subtask context (for pKeyInfo) */
80088  int *pbKey2Cached,              /* True if pTask->pUnpacked is pKey2 */
80089  const void *pKey1, int nKey1,   /* Left side of comparison */
80090  const void *pKey2, int nKey2    /* Right side of comparison */
80091){
80092  const u8 * const p1 = (const u8 * const)pKey1;
80093  const u8 * const p2 = (const u8 * const)pKey2;
80094  const int s1 = p1[1];                 /* Left hand serial type */
80095  const int s2 = p2[1];                 /* Right hand serial type */
80096  const u8 * const v1 = &p1[ p1[0] ];   /* Pointer to value 1 */
80097  const u8 * const v2 = &p2[ p2[0] ];   /* Pointer to value 2 */
80098  int res;                              /* Return value */
80099
80100  assert( (s1>0 && s1<7) || s1==8 || s1==9 );
80101  assert( (s2>0 && s2<7) || s2==8 || s2==9 );
80102
80103  if( s1>7 && s2>7 ){
80104    res = s1 - s2;
80105  }else{
80106    if( s1==s2 ){
80107      if( (*v1 ^ *v2) & 0x80 ){
80108        /* The two values have different signs */
80109        res = (*v1 & 0x80) ? -1 : +1;
80110      }else{
80111        /* The two values have the same sign. Compare using memcmp(). */
80112        static const u8 aLen[] = {0, 1, 2, 3, 4, 6, 8 };
80113        int i;
80114        res = 0;
80115        for(i=0; i<aLen[s1]; i++){
80116          if( (res = v1[i] - v2[i]) ) break;
80117        }
80118      }
80119    }else{
80120      if( s2>7 ){
80121        res = +1;
80122      }else if( s1>7 ){
80123        res = -1;
80124      }else{
80125        res = s1 - s2;
80126      }
80127      assert( res!=0 );
80128
80129      if( res>0 ){
80130        if( *v1 & 0x80 ) res = -1;
80131      }else{
80132        if( *v2 & 0x80 ) res = +1;
80133      }
80134    }
80135  }
80136
80137  if( res==0 ){
80138    if( pTask->pSorter->pKeyInfo->nField>1 ){
80139      res = vdbeSorterCompareTail(
80140          pTask, pbKey2Cached, pKey1, nKey1, pKey2, nKey2
80141      );
80142    }
80143  }else if( pTask->pSorter->pKeyInfo->aSortOrder[0] ){
80144    res = res * -1;
80145  }
80146
80147  return res;
80148}
80149
80150/*
80151** Initialize the temporary index cursor just opened as a sorter cursor.
80152**
80153** Usually, the sorter module uses the value of (pCsr->pKeyInfo->nField)
80154** to determine the number of fields that should be compared from the
80155** records being sorted. However, if the value passed as argument nField
80156** is non-zero and the sorter is able to guarantee a stable sort, nField
80157** is used instead. This is used when sorting records for a CREATE INDEX
80158** statement. In this case, keys are always delivered to the sorter in
80159** order of the primary key, which happens to be make up the final part
80160** of the records being sorted. So if the sort is stable, there is never
80161** any reason to compare PK fields and they can be ignored for a small
80162** performance boost.
80163**
80164** The sorter can guarantee a stable sort when running in single-threaded
80165** mode, but not in multi-threaded mode.
80166**
80167** SQLITE_OK is returned if successful, or an SQLite error code otherwise.
80168*/
80169SQLITE_PRIVATE int sqlite3VdbeSorterInit(
80170  sqlite3 *db,                    /* Database connection (for malloc()) */
80171  int nField,                     /* Number of key fields in each record */
80172  VdbeCursor *pCsr                /* Cursor that holds the new sorter */
80173){
80174  int pgsz;                       /* Page size of main database */
80175  int i;                          /* Used to iterate through aTask[] */
80176  int mxCache;                    /* Cache size */
80177  VdbeSorter *pSorter;            /* The new sorter */
80178  KeyInfo *pKeyInfo;              /* Copy of pCsr->pKeyInfo with db==0 */
80179  int szKeyInfo;                  /* Size of pCsr->pKeyInfo in bytes */
80180  int sz;                         /* Size of pSorter in bytes */
80181  int rc = SQLITE_OK;
80182#if SQLITE_MAX_WORKER_THREADS==0
80183# define nWorker 0
80184#else
80185  int nWorker;
80186#endif
80187
80188  /* Initialize the upper limit on the number of worker threads */
80189#if SQLITE_MAX_WORKER_THREADS>0
80190  if( sqlite3TempInMemory(db) || sqlite3GlobalConfig.bCoreMutex==0 ){
80191    nWorker = 0;
80192  }else{
80193    nWorker = db->aLimit[SQLITE_LIMIT_WORKER_THREADS];
80194  }
80195#endif
80196
80197  /* Do not allow the total number of threads (main thread + all workers)
80198  ** to exceed the maximum merge count */
80199#if SQLITE_MAX_WORKER_THREADS>=SORTER_MAX_MERGE_COUNT
80200  if( nWorker>=SORTER_MAX_MERGE_COUNT ){
80201    nWorker = SORTER_MAX_MERGE_COUNT-1;
80202  }
80203#endif
80204
80205  assert( pCsr->pKeyInfo && pCsr->pBt==0 );
80206  szKeyInfo = sizeof(KeyInfo) + (pCsr->pKeyInfo->nField-1)*sizeof(CollSeq*);
80207  sz = sizeof(VdbeSorter) + nWorker * sizeof(SortSubtask);
80208
80209  pSorter = (VdbeSorter*)sqlite3DbMallocZero(db, sz + szKeyInfo);
80210  pCsr->pSorter = pSorter;
80211  if( pSorter==0 ){
80212    rc = SQLITE_NOMEM;
80213  }else{
80214    pSorter->pKeyInfo = pKeyInfo = (KeyInfo*)((u8*)pSorter + sz);
80215    memcpy(pKeyInfo, pCsr->pKeyInfo, szKeyInfo);
80216    pKeyInfo->db = 0;
80217    if( nField && nWorker==0 ){
80218      pKeyInfo->nXField += (pKeyInfo->nField - nField);
80219      pKeyInfo->nField = nField;
80220    }
80221    pSorter->pgsz = pgsz = sqlite3BtreeGetPageSize(db->aDb[0].pBt);
80222    pSorter->nTask = nWorker + 1;
80223    pSorter->iPrev = nWorker-1;
80224    pSorter->bUseThreads = (pSorter->nTask>1);
80225    pSorter->db = db;
80226    for(i=0; i<pSorter->nTask; i++){
80227      SortSubtask *pTask = &pSorter->aTask[i];
80228      pTask->pSorter = pSorter;
80229    }
80230
80231    if( !sqlite3TempInMemory(db) ){
80232      u32 szPma = sqlite3GlobalConfig.szPma;
80233      pSorter->mnPmaSize = szPma * pgsz;
80234      mxCache = db->aDb[0].pSchema->cache_size;
80235      if( mxCache<(int)szPma ) mxCache = (int)szPma;
80236      pSorter->mxPmaSize = MIN((i64)mxCache*pgsz, SQLITE_MAX_PMASZ);
80237
80238      /* EVIDENCE-OF: R-26747-61719 When the application provides any amount of
80239      ** scratch memory using SQLITE_CONFIG_SCRATCH, SQLite avoids unnecessary
80240      ** large heap allocations.
80241      */
80242      if( sqlite3GlobalConfig.pScratch==0 ){
80243        assert( pSorter->iMemory==0 );
80244        pSorter->nMemory = pgsz;
80245        pSorter->list.aMemory = (u8*)sqlite3Malloc(pgsz);
80246        if( !pSorter->list.aMemory ) rc = SQLITE_NOMEM;
80247      }
80248    }
80249
80250    if( (pKeyInfo->nField+pKeyInfo->nXField)<13
80251     && (pKeyInfo->aColl[0]==0 || pKeyInfo->aColl[0]==db->pDfltColl)
80252    ){
80253      pSorter->typeMask = SORTER_TYPE_INTEGER | SORTER_TYPE_TEXT;
80254    }
80255  }
80256
80257  return rc;
80258}
80259#undef nWorker   /* Defined at the top of this function */
80260
80261/*
80262** Free the list of sorted records starting at pRecord.
80263*/
80264static void vdbeSorterRecordFree(sqlite3 *db, SorterRecord *pRecord){
80265  SorterRecord *p;
80266  SorterRecord *pNext;
80267  for(p=pRecord; p; p=pNext){
80268    pNext = p->u.pNext;
80269    sqlite3DbFree(db, p);
80270  }
80271}
80272
80273/*
80274** Free all resources owned by the object indicated by argument pTask. All
80275** fields of *pTask are zeroed before returning.
80276*/
80277static void vdbeSortSubtaskCleanup(sqlite3 *db, SortSubtask *pTask){
80278  sqlite3DbFree(db, pTask->pUnpacked);
80279#if SQLITE_MAX_WORKER_THREADS>0
80280  /* pTask->list.aMemory can only be non-zero if it was handed memory
80281  ** from the main thread.  That only occurs SQLITE_MAX_WORKER_THREADS>0 */
80282  if( pTask->list.aMemory ){
80283    sqlite3_free(pTask->list.aMemory);
80284  }else
80285#endif
80286  {
80287    assert( pTask->list.aMemory==0 );
80288    vdbeSorterRecordFree(0, pTask->list.pList);
80289  }
80290  if( pTask->file.pFd ){
80291    sqlite3OsCloseFree(pTask->file.pFd);
80292  }
80293  if( pTask->file2.pFd ){
80294    sqlite3OsCloseFree(pTask->file2.pFd);
80295  }
80296  memset(pTask, 0, sizeof(SortSubtask));
80297}
80298
80299#ifdef SQLITE_DEBUG_SORTER_THREADS
80300static void vdbeSorterWorkDebug(SortSubtask *pTask, const char *zEvent){
80301  i64 t;
80302  int iTask = (pTask - pTask->pSorter->aTask);
80303  sqlite3OsCurrentTimeInt64(pTask->pSorter->db->pVfs, &t);
80304  fprintf(stderr, "%lld:%d %s\n", t, iTask, zEvent);
80305}
80306static void vdbeSorterRewindDebug(const char *zEvent){
80307  i64 t;
80308  sqlite3OsCurrentTimeInt64(sqlite3_vfs_find(0), &t);
80309  fprintf(stderr, "%lld:X %s\n", t, zEvent);
80310}
80311static void vdbeSorterPopulateDebug(
80312  SortSubtask *pTask,
80313  const char *zEvent
80314){
80315  i64 t;
80316  int iTask = (pTask - pTask->pSorter->aTask);
80317  sqlite3OsCurrentTimeInt64(pTask->pSorter->db->pVfs, &t);
80318  fprintf(stderr, "%lld:bg%d %s\n", t, iTask, zEvent);
80319}
80320static void vdbeSorterBlockDebug(
80321  SortSubtask *pTask,
80322  int bBlocked,
80323  const char *zEvent
80324){
80325  if( bBlocked ){
80326    i64 t;
80327    sqlite3OsCurrentTimeInt64(pTask->pSorter->db->pVfs, &t);
80328    fprintf(stderr, "%lld:main %s\n", t, zEvent);
80329  }
80330}
80331#else
80332# define vdbeSorterWorkDebug(x,y)
80333# define vdbeSorterRewindDebug(y)
80334# define vdbeSorterPopulateDebug(x,y)
80335# define vdbeSorterBlockDebug(x,y,z)
80336#endif
80337
80338#if SQLITE_MAX_WORKER_THREADS>0
80339/*
80340** Join thread pTask->thread.
80341*/
80342static int vdbeSorterJoinThread(SortSubtask *pTask){
80343  int rc = SQLITE_OK;
80344  if( pTask->pThread ){
80345#ifdef SQLITE_DEBUG_SORTER_THREADS
80346    int bDone = pTask->bDone;
80347#endif
80348    void *pRet = SQLITE_INT_TO_PTR(SQLITE_ERROR);
80349    vdbeSorterBlockDebug(pTask, !bDone, "enter");
80350    (void)sqlite3ThreadJoin(pTask->pThread, &pRet);
80351    vdbeSorterBlockDebug(pTask, !bDone, "exit");
80352    rc = SQLITE_PTR_TO_INT(pRet);
80353    assert( pTask->bDone==1 );
80354    pTask->bDone = 0;
80355    pTask->pThread = 0;
80356  }
80357  return rc;
80358}
80359
80360/*
80361** Launch a background thread to run xTask(pIn).
80362*/
80363static int vdbeSorterCreateThread(
80364  SortSubtask *pTask,             /* Thread will use this task object */
80365  void *(*xTask)(void*),          /* Routine to run in a separate thread */
80366  void *pIn                       /* Argument passed into xTask() */
80367){
80368  assert( pTask->pThread==0 && pTask->bDone==0 );
80369  return sqlite3ThreadCreate(&pTask->pThread, xTask, pIn);
80370}
80371
80372/*
80373** Join all outstanding threads launched by SorterWrite() to create
80374** level-0 PMAs.
80375*/
80376static int vdbeSorterJoinAll(VdbeSorter *pSorter, int rcin){
80377  int rc = rcin;
80378  int i;
80379
80380  /* This function is always called by the main user thread.
80381  **
80382  ** If this function is being called after SorterRewind() has been called,
80383  ** it is possible that thread pSorter->aTask[pSorter->nTask-1].pThread
80384  ** is currently attempt to join one of the other threads. To avoid a race
80385  ** condition where this thread also attempts to join the same object, join
80386  ** thread pSorter->aTask[pSorter->nTask-1].pThread first. */
80387  for(i=pSorter->nTask-1; i>=0; i--){
80388    SortSubtask *pTask = &pSorter->aTask[i];
80389    int rc2 = vdbeSorterJoinThread(pTask);
80390    if( rc==SQLITE_OK ) rc = rc2;
80391  }
80392  return rc;
80393}
80394#else
80395# define vdbeSorterJoinAll(x,rcin) (rcin)
80396# define vdbeSorterJoinThread(pTask) SQLITE_OK
80397#endif
80398
80399/*
80400** Allocate a new MergeEngine object capable of handling up to
80401** nReader PmaReader inputs.
80402**
80403** nReader is automatically rounded up to the next power of two.
80404** nReader may not exceed SORTER_MAX_MERGE_COUNT even after rounding up.
80405*/
80406static MergeEngine *vdbeMergeEngineNew(int nReader){
80407  int N = 2;                      /* Smallest power of two >= nReader */
80408  int nByte;                      /* Total bytes of space to allocate */
80409  MergeEngine *pNew;              /* Pointer to allocated object to return */
80410
80411  assert( nReader<=SORTER_MAX_MERGE_COUNT );
80412
80413  while( N<nReader ) N += N;
80414  nByte = sizeof(MergeEngine) + N * (sizeof(int) + sizeof(PmaReader));
80415
80416  pNew = sqlite3FaultSim(100) ? 0 : (MergeEngine*)sqlite3MallocZero(nByte);
80417  if( pNew ){
80418    pNew->nTree = N;
80419    pNew->pTask = 0;
80420    pNew->aReadr = (PmaReader*)&pNew[1];
80421    pNew->aTree = (int*)&pNew->aReadr[N];
80422  }
80423  return pNew;
80424}
80425
80426/*
80427** Free the MergeEngine object passed as the only argument.
80428*/
80429static void vdbeMergeEngineFree(MergeEngine *pMerger){
80430  int i;
80431  if( pMerger ){
80432    for(i=0; i<pMerger->nTree; i++){
80433      vdbePmaReaderClear(&pMerger->aReadr[i]);
80434    }
80435  }
80436  sqlite3_free(pMerger);
80437}
80438
80439/*
80440** Free all resources associated with the IncrMerger object indicated by
80441** the first argument.
80442*/
80443static void vdbeIncrFree(IncrMerger *pIncr){
80444  if( pIncr ){
80445#if SQLITE_MAX_WORKER_THREADS>0
80446    if( pIncr->bUseThread ){
80447      vdbeSorterJoinThread(pIncr->pTask);
80448      if( pIncr->aFile[0].pFd ) sqlite3OsCloseFree(pIncr->aFile[0].pFd);
80449      if( pIncr->aFile[1].pFd ) sqlite3OsCloseFree(pIncr->aFile[1].pFd);
80450    }
80451#endif
80452    vdbeMergeEngineFree(pIncr->pMerger);
80453    sqlite3_free(pIncr);
80454  }
80455}
80456
80457/*
80458** Reset a sorting cursor back to its original empty state.
80459*/
80460SQLITE_PRIVATE void sqlite3VdbeSorterReset(sqlite3 *db, VdbeSorter *pSorter){
80461  int i;
80462  (void)vdbeSorterJoinAll(pSorter, SQLITE_OK);
80463  assert( pSorter->bUseThreads || pSorter->pReader==0 );
80464#if SQLITE_MAX_WORKER_THREADS>0
80465  if( pSorter->pReader ){
80466    vdbePmaReaderClear(pSorter->pReader);
80467    sqlite3DbFree(db, pSorter->pReader);
80468    pSorter->pReader = 0;
80469  }
80470#endif
80471  vdbeMergeEngineFree(pSorter->pMerger);
80472  pSorter->pMerger = 0;
80473  for(i=0; i<pSorter->nTask; i++){
80474    SortSubtask *pTask = &pSorter->aTask[i];
80475    vdbeSortSubtaskCleanup(db, pTask);
80476    pTask->pSorter = pSorter;
80477  }
80478  if( pSorter->list.aMemory==0 ){
80479    vdbeSorterRecordFree(0, pSorter->list.pList);
80480  }
80481  pSorter->list.pList = 0;
80482  pSorter->list.szPMA = 0;
80483  pSorter->bUsePMA = 0;
80484  pSorter->iMemory = 0;
80485  pSorter->mxKeysize = 0;
80486  sqlite3DbFree(db, pSorter->pUnpacked);
80487  pSorter->pUnpacked = 0;
80488}
80489
80490/*
80491** Free any cursor components allocated by sqlite3VdbeSorterXXX routines.
80492*/
80493SQLITE_PRIVATE void sqlite3VdbeSorterClose(sqlite3 *db, VdbeCursor *pCsr){
80494  VdbeSorter *pSorter = pCsr->pSorter;
80495  if( pSorter ){
80496    sqlite3VdbeSorterReset(db, pSorter);
80497    sqlite3_free(pSorter->list.aMemory);
80498    sqlite3DbFree(db, pSorter);
80499    pCsr->pSorter = 0;
80500  }
80501}
80502
80503#if SQLITE_MAX_MMAP_SIZE>0
80504/*
80505** The first argument is a file-handle open on a temporary file. The file
80506** is guaranteed to be nByte bytes or smaller in size. This function
80507** attempts to extend the file to nByte bytes in size and to ensure that
80508** the VFS has memory mapped it.
80509**
80510** Whether or not the file does end up memory mapped of course depends on
80511** the specific VFS implementation.
80512*/
80513static void vdbeSorterExtendFile(sqlite3 *db, sqlite3_file *pFd, i64 nByte){
80514  if( nByte<=(i64)(db->nMaxSorterMmap) && pFd->pMethods->iVersion>=3 ){
80515    void *p = 0;
80516    int chunksize = 4*1024;
80517    sqlite3OsFileControlHint(pFd, SQLITE_FCNTL_CHUNK_SIZE, &chunksize);
80518    sqlite3OsFileControlHint(pFd, SQLITE_FCNTL_SIZE_HINT, &nByte);
80519    sqlite3OsFetch(pFd, 0, (int)nByte, &p);
80520    sqlite3OsUnfetch(pFd, 0, p);
80521  }
80522}
80523#else
80524# define vdbeSorterExtendFile(x,y,z)
80525#endif
80526
80527/*
80528** Allocate space for a file-handle and open a temporary file. If successful,
80529** set *ppFd to point to the malloc'd file-handle and return SQLITE_OK.
80530** Otherwise, set *ppFd to 0 and return an SQLite error code.
80531*/
80532static int vdbeSorterOpenTempFile(
80533  sqlite3 *db,                    /* Database handle doing sort */
80534  i64 nExtend,                    /* Attempt to extend file to this size */
80535  sqlite3_file **ppFd
80536){
80537  int rc;
80538  if( sqlite3FaultSim(202) ) return SQLITE_IOERR_ACCESS;
80539  rc = sqlite3OsOpenMalloc(db->pVfs, 0, ppFd,
80540      SQLITE_OPEN_TEMP_JOURNAL |
80541      SQLITE_OPEN_READWRITE    | SQLITE_OPEN_CREATE |
80542      SQLITE_OPEN_EXCLUSIVE    | SQLITE_OPEN_DELETEONCLOSE, &rc
80543  );
80544  if( rc==SQLITE_OK ){
80545    i64 max = SQLITE_MAX_MMAP_SIZE;
80546    sqlite3OsFileControlHint(*ppFd, SQLITE_FCNTL_MMAP_SIZE, (void*)&max);
80547    if( nExtend>0 ){
80548      vdbeSorterExtendFile(db, *ppFd, nExtend);
80549    }
80550  }
80551  return rc;
80552}
80553
80554/*
80555** If it has not already been allocated, allocate the UnpackedRecord
80556** structure at pTask->pUnpacked. Return SQLITE_OK if successful (or
80557** if no allocation was required), or SQLITE_NOMEM otherwise.
80558*/
80559static int vdbeSortAllocUnpacked(SortSubtask *pTask){
80560  if( pTask->pUnpacked==0 ){
80561    char *pFree;
80562    pTask->pUnpacked = sqlite3VdbeAllocUnpackedRecord(
80563        pTask->pSorter->pKeyInfo, 0, 0, &pFree
80564    );
80565    assert( pTask->pUnpacked==(UnpackedRecord*)pFree );
80566    if( pFree==0 ) return SQLITE_NOMEM;
80567    pTask->pUnpacked->nField = pTask->pSorter->pKeyInfo->nField;
80568    pTask->pUnpacked->errCode = 0;
80569  }
80570  return SQLITE_OK;
80571}
80572
80573
80574/*
80575** Merge the two sorted lists p1 and p2 into a single list.
80576** Set *ppOut to the head of the new list.
80577*/
80578static void vdbeSorterMerge(
80579  SortSubtask *pTask,             /* Calling thread context */
80580  SorterRecord *p1,               /* First list to merge */
80581  SorterRecord *p2,               /* Second list to merge */
80582  SorterRecord **ppOut            /* OUT: Head of merged list */
80583){
80584  SorterRecord *pFinal = 0;
80585  SorterRecord **pp = &pFinal;
80586  int bCached = 0;
80587
80588  while( p1 && p2 ){
80589    int res;
80590    res = pTask->xCompare(
80591        pTask, &bCached, SRVAL(p1), p1->nVal, SRVAL(p2), p2->nVal
80592    );
80593
80594    if( res<=0 ){
80595      *pp = p1;
80596      pp = &p1->u.pNext;
80597      p1 = p1->u.pNext;
80598    }else{
80599      *pp = p2;
80600      pp = &p2->u.pNext;
80601      p2 = p2->u.pNext;
80602      bCached = 0;
80603    }
80604  }
80605  *pp = p1 ? p1 : p2;
80606  *ppOut = pFinal;
80607}
80608
80609/*
80610** Return the SorterCompare function to compare values collected by the
80611** sorter object passed as the only argument.
80612*/
80613static SorterCompare vdbeSorterGetCompare(VdbeSorter *p){
80614  if( p->typeMask==SORTER_TYPE_INTEGER ){
80615    return vdbeSorterCompareInt;
80616  }else if( p->typeMask==SORTER_TYPE_TEXT ){
80617    return vdbeSorterCompareText;
80618  }
80619  return vdbeSorterCompare;
80620}
80621
80622/*
80623** Sort the linked list of records headed at pTask->pList. Return
80624** SQLITE_OK if successful, or an SQLite error code (i.e. SQLITE_NOMEM) if
80625** an error occurs.
80626*/
80627static int vdbeSorterSort(SortSubtask *pTask, SorterList *pList){
80628  int i;
80629  SorterRecord **aSlot;
80630  SorterRecord *p;
80631  int rc;
80632
80633  rc = vdbeSortAllocUnpacked(pTask);
80634  if( rc!=SQLITE_OK ) return rc;
80635
80636  p = pList->pList;
80637  pTask->xCompare = vdbeSorterGetCompare(pTask->pSorter);
80638
80639  aSlot = (SorterRecord **)sqlite3MallocZero(64 * sizeof(SorterRecord *));
80640  if( !aSlot ){
80641    return SQLITE_NOMEM;
80642  }
80643
80644  while( p ){
80645    SorterRecord *pNext;
80646    if( pList->aMemory ){
80647      if( (u8*)p==pList->aMemory ){
80648        pNext = 0;
80649      }else{
80650        assert( p->u.iNext<sqlite3MallocSize(pList->aMemory) );
80651        pNext = (SorterRecord*)&pList->aMemory[p->u.iNext];
80652      }
80653    }else{
80654      pNext = p->u.pNext;
80655    }
80656
80657    p->u.pNext = 0;
80658    for(i=0; aSlot[i]; i++){
80659      vdbeSorterMerge(pTask, p, aSlot[i], &p);
80660      aSlot[i] = 0;
80661    }
80662    aSlot[i] = p;
80663    p = pNext;
80664  }
80665
80666  p = 0;
80667  for(i=0; i<64; i++){
80668    vdbeSorterMerge(pTask, p, aSlot[i], &p);
80669  }
80670  pList->pList = p;
80671
80672  sqlite3_free(aSlot);
80673  assert( pTask->pUnpacked->errCode==SQLITE_OK
80674       || pTask->pUnpacked->errCode==SQLITE_NOMEM
80675  );
80676  return pTask->pUnpacked->errCode;
80677}
80678
80679/*
80680** Initialize a PMA-writer object.
80681*/
80682static void vdbePmaWriterInit(
80683  sqlite3_file *pFd,              /* File handle to write to */
80684  PmaWriter *p,                   /* Object to populate */
80685  int nBuf,                       /* Buffer size */
80686  i64 iStart                      /* Offset of pFd to begin writing at */
80687){
80688  memset(p, 0, sizeof(PmaWriter));
80689  p->aBuffer = (u8*)sqlite3Malloc(nBuf);
80690  if( !p->aBuffer ){
80691    p->eFWErr = SQLITE_NOMEM;
80692  }else{
80693    p->iBufEnd = p->iBufStart = (iStart % nBuf);
80694    p->iWriteOff = iStart - p->iBufStart;
80695    p->nBuffer = nBuf;
80696    p->pFd = pFd;
80697  }
80698}
80699
80700/*
80701** Write nData bytes of data to the PMA. Return SQLITE_OK
80702** if successful, or an SQLite error code if an error occurs.
80703*/
80704static void vdbePmaWriteBlob(PmaWriter *p, u8 *pData, int nData){
80705  int nRem = nData;
80706  while( nRem>0 && p->eFWErr==0 ){
80707    int nCopy = nRem;
80708    if( nCopy>(p->nBuffer - p->iBufEnd) ){
80709      nCopy = p->nBuffer - p->iBufEnd;
80710    }
80711
80712    memcpy(&p->aBuffer[p->iBufEnd], &pData[nData-nRem], nCopy);
80713    p->iBufEnd += nCopy;
80714    if( p->iBufEnd==p->nBuffer ){
80715      p->eFWErr = sqlite3OsWrite(p->pFd,
80716          &p->aBuffer[p->iBufStart], p->iBufEnd - p->iBufStart,
80717          p->iWriteOff + p->iBufStart
80718      );
80719      p->iBufStart = p->iBufEnd = 0;
80720      p->iWriteOff += p->nBuffer;
80721    }
80722    assert( p->iBufEnd<p->nBuffer );
80723
80724    nRem -= nCopy;
80725  }
80726}
80727
80728/*
80729** Flush any buffered data to disk and clean up the PMA-writer object.
80730** The results of using the PMA-writer after this call are undefined.
80731** Return SQLITE_OK if flushing the buffered data succeeds or is not
80732** required. Otherwise, return an SQLite error code.
80733**
80734** Before returning, set *piEof to the offset immediately following the
80735** last byte written to the file.
80736*/
80737static int vdbePmaWriterFinish(PmaWriter *p, i64 *piEof){
80738  int rc;
80739  if( p->eFWErr==0 && ALWAYS(p->aBuffer) && p->iBufEnd>p->iBufStart ){
80740    p->eFWErr = sqlite3OsWrite(p->pFd,
80741        &p->aBuffer[p->iBufStart], p->iBufEnd - p->iBufStart,
80742        p->iWriteOff + p->iBufStart
80743    );
80744  }
80745  *piEof = (p->iWriteOff + p->iBufEnd);
80746  sqlite3_free(p->aBuffer);
80747  rc = p->eFWErr;
80748  memset(p, 0, sizeof(PmaWriter));
80749  return rc;
80750}
80751
80752/*
80753** Write value iVal encoded as a varint to the PMA. Return
80754** SQLITE_OK if successful, or an SQLite error code if an error occurs.
80755*/
80756static void vdbePmaWriteVarint(PmaWriter *p, u64 iVal){
80757  int nByte;
80758  u8 aByte[10];
80759  nByte = sqlite3PutVarint(aByte, iVal);
80760  vdbePmaWriteBlob(p, aByte, nByte);
80761}
80762
80763/*
80764** Write the current contents of in-memory linked-list pList to a level-0
80765** PMA in the temp file belonging to sub-task pTask. Return SQLITE_OK if
80766** successful, or an SQLite error code otherwise.
80767**
80768** The format of a PMA is:
80769**
80770**     * A varint. This varint contains the total number of bytes of content
80771**       in the PMA (not including the varint itself).
80772**
80773**     * One or more records packed end-to-end in order of ascending keys.
80774**       Each record consists of a varint followed by a blob of data (the
80775**       key). The varint is the number of bytes in the blob of data.
80776*/
80777static int vdbeSorterListToPMA(SortSubtask *pTask, SorterList *pList){
80778  sqlite3 *db = pTask->pSorter->db;
80779  int rc = SQLITE_OK;             /* Return code */
80780  PmaWriter writer;               /* Object used to write to the file */
80781
80782#ifdef SQLITE_DEBUG
80783  /* Set iSz to the expected size of file pTask->file after writing the PMA.
80784  ** This is used by an assert() statement at the end of this function.  */
80785  i64 iSz = pList->szPMA + sqlite3VarintLen(pList->szPMA) + pTask->file.iEof;
80786#endif
80787
80788  vdbeSorterWorkDebug(pTask, "enter");
80789  memset(&writer, 0, sizeof(PmaWriter));
80790  assert( pList->szPMA>0 );
80791
80792  /* If the first temporary PMA file has not been opened, open it now. */
80793  if( pTask->file.pFd==0 ){
80794    rc = vdbeSorterOpenTempFile(db, 0, &pTask->file.pFd);
80795    assert( rc!=SQLITE_OK || pTask->file.pFd );
80796    assert( pTask->file.iEof==0 );
80797    assert( pTask->nPMA==0 );
80798  }
80799
80800  /* Try to get the file to memory map */
80801  if( rc==SQLITE_OK ){
80802    vdbeSorterExtendFile(db, pTask->file.pFd, pTask->file.iEof+pList->szPMA+9);
80803  }
80804
80805  /* Sort the list */
80806  if( rc==SQLITE_OK ){
80807    rc = vdbeSorterSort(pTask, pList);
80808  }
80809
80810  if( rc==SQLITE_OK ){
80811    SorterRecord *p;
80812    SorterRecord *pNext = 0;
80813
80814    vdbePmaWriterInit(pTask->file.pFd, &writer, pTask->pSorter->pgsz,
80815                      pTask->file.iEof);
80816    pTask->nPMA++;
80817    vdbePmaWriteVarint(&writer, pList->szPMA);
80818    for(p=pList->pList; p; p=pNext){
80819      pNext = p->u.pNext;
80820      vdbePmaWriteVarint(&writer, p->nVal);
80821      vdbePmaWriteBlob(&writer, SRVAL(p), p->nVal);
80822      if( pList->aMemory==0 ) sqlite3_free(p);
80823    }
80824    pList->pList = p;
80825    rc = vdbePmaWriterFinish(&writer, &pTask->file.iEof);
80826  }
80827
80828  vdbeSorterWorkDebug(pTask, "exit");
80829  assert( rc!=SQLITE_OK || pList->pList==0 );
80830  assert( rc!=SQLITE_OK || pTask->file.iEof==iSz );
80831  return rc;
80832}
80833
80834/*
80835** Advance the MergeEngine to its next entry.
80836** Set *pbEof to true there is no next entry because
80837** the MergeEngine has reached the end of all its inputs.
80838**
80839** Return SQLITE_OK if successful or an error code if an error occurs.
80840*/
80841static int vdbeMergeEngineStep(
80842  MergeEngine *pMerger,      /* The merge engine to advance to the next row */
80843  int *pbEof                 /* Set TRUE at EOF.  Set false for more content */
80844){
80845  int rc;
80846  int iPrev = pMerger->aTree[1];/* Index of PmaReader to advance */
80847  SortSubtask *pTask = pMerger->pTask;
80848
80849  /* Advance the current PmaReader */
80850  rc = vdbePmaReaderNext(&pMerger->aReadr[iPrev]);
80851
80852  /* Update contents of aTree[] */
80853  if( rc==SQLITE_OK ){
80854    int i;                      /* Index of aTree[] to recalculate */
80855    PmaReader *pReadr1;         /* First PmaReader to compare */
80856    PmaReader *pReadr2;         /* Second PmaReader to compare */
80857    int bCached = 0;
80858
80859    /* Find the first two PmaReaders to compare. The one that was just
80860    ** advanced (iPrev) and the one next to it in the array.  */
80861    pReadr1 = &pMerger->aReadr[(iPrev & 0xFFFE)];
80862    pReadr2 = &pMerger->aReadr[(iPrev | 0x0001)];
80863
80864    for(i=(pMerger->nTree+iPrev)/2; i>0; i=i/2){
80865      /* Compare pReadr1 and pReadr2. Store the result in variable iRes. */
80866      int iRes;
80867      if( pReadr1->pFd==0 ){
80868        iRes = +1;
80869      }else if( pReadr2->pFd==0 ){
80870        iRes = -1;
80871      }else{
80872        iRes = pTask->xCompare(pTask, &bCached,
80873            pReadr1->aKey, pReadr1->nKey, pReadr2->aKey, pReadr2->nKey
80874        );
80875      }
80876
80877      /* If pReadr1 contained the smaller value, set aTree[i] to its index.
80878      ** Then set pReadr2 to the next PmaReader to compare to pReadr1. In this
80879      ** case there is no cache of pReadr2 in pTask->pUnpacked, so set
80880      ** pKey2 to point to the record belonging to pReadr2.
80881      **
80882      ** Alternatively, if pReadr2 contains the smaller of the two values,
80883      ** set aTree[i] to its index and update pReadr1. If vdbeSorterCompare()
80884      ** was actually called above, then pTask->pUnpacked now contains
80885      ** a value equivalent to pReadr2. So set pKey2 to NULL to prevent
80886      ** vdbeSorterCompare() from decoding pReadr2 again.
80887      **
80888      ** If the two values were equal, then the value from the oldest
80889      ** PMA should be considered smaller. The VdbeSorter.aReadr[] array
80890      ** is sorted from oldest to newest, so pReadr1 contains older values
80891      ** than pReadr2 iff (pReadr1<pReadr2).  */
80892      if( iRes<0 || (iRes==0 && pReadr1<pReadr2) ){
80893        pMerger->aTree[i] = (int)(pReadr1 - pMerger->aReadr);
80894        pReadr2 = &pMerger->aReadr[ pMerger->aTree[i ^ 0x0001] ];
80895        bCached = 0;
80896      }else{
80897        if( pReadr1->pFd ) bCached = 0;
80898        pMerger->aTree[i] = (int)(pReadr2 - pMerger->aReadr);
80899        pReadr1 = &pMerger->aReadr[ pMerger->aTree[i ^ 0x0001] ];
80900      }
80901    }
80902    *pbEof = (pMerger->aReadr[pMerger->aTree[1]].pFd==0);
80903  }
80904
80905  return (rc==SQLITE_OK ? pTask->pUnpacked->errCode : rc);
80906}
80907
80908#if SQLITE_MAX_WORKER_THREADS>0
80909/*
80910** The main routine for background threads that write level-0 PMAs.
80911*/
80912static void *vdbeSorterFlushThread(void *pCtx){
80913  SortSubtask *pTask = (SortSubtask*)pCtx;
80914  int rc;                         /* Return code */
80915  assert( pTask->bDone==0 );
80916  rc = vdbeSorterListToPMA(pTask, &pTask->list);
80917  pTask->bDone = 1;
80918  return SQLITE_INT_TO_PTR(rc);
80919}
80920#endif /* SQLITE_MAX_WORKER_THREADS>0 */
80921
80922/*
80923** Flush the current contents of VdbeSorter.list to a new PMA, possibly
80924** using a background thread.
80925*/
80926static int vdbeSorterFlushPMA(VdbeSorter *pSorter){
80927#if SQLITE_MAX_WORKER_THREADS==0
80928  pSorter->bUsePMA = 1;
80929  return vdbeSorterListToPMA(&pSorter->aTask[0], &pSorter->list);
80930#else
80931  int rc = SQLITE_OK;
80932  int i;
80933  SortSubtask *pTask = 0;    /* Thread context used to create new PMA */
80934  int nWorker = (pSorter->nTask-1);
80935
80936  /* Set the flag to indicate that at least one PMA has been written.
80937  ** Or will be, anyhow.  */
80938  pSorter->bUsePMA = 1;
80939
80940  /* Select a sub-task to sort and flush the current list of in-memory
80941  ** records to disk. If the sorter is running in multi-threaded mode,
80942  ** round-robin between the first (pSorter->nTask-1) tasks. Except, if
80943  ** the background thread from a sub-tasks previous turn is still running,
80944  ** skip it. If the first (pSorter->nTask-1) sub-tasks are all still busy,
80945  ** fall back to using the final sub-task. The first (pSorter->nTask-1)
80946  ** sub-tasks are prefered as they use background threads - the final
80947  ** sub-task uses the main thread. */
80948  for(i=0; i<nWorker; i++){
80949    int iTest = (pSorter->iPrev + i + 1) % nWorker;
80950    pTask = &pSorter->aTask[iTest];
80951    if( pTask->bDone ){
80952      rc = vdbeSorterJoinThread(pTask);
80953    }
80954    if( rc!=SQLITE_OK || pTask->pThread==0 ) break;
80955  }
80956
80957  if( rc==SQLITE_OK ){
80958    if( i==nWorker ){
80959      /* Use the foreground thread for this operation */
80960      rc = vdbeSorterListToPMA(&pSorter->aTask[nWorker], &pSorter->list);
80961    }else{
80962      /* Launch a background thread for this operation */
80963      u8 *aMem = pTask->list.aMemory;
80964      void *pCtx = (void*)pTask;
80965
80966      assert( pTask->pThread==0 && pTask->bDone==0 );
80967      assert( pTask->list.pList==0 );
80968      assert( pTask->list.aMemory==0 || pSorter->list.aMemory!=0 );
80969
80970      pSorter->iPrev = (u8)(pTask - pSorter->aTask);
80971      pTask->list = pSorter->list;
80972      pSorter->list.pList = 0;
80973      pSorter->list.szPMA = 0;
80974      if( aMem ){
80975        pSorter->list.aMemory = aMem;
80976        pSorter->nMemory = sqlite3MallocSize(aMem);
80977      }else if( pSorter->list.aMemory ){
80978        pSorter->list.aMemory = sqlite3Malloc(pSorter->nMemory);
80979        if( !pSorter->list.aMemory ) return SQLITE_NOMEM;
80980      }
80981
80982      rc = vdbeSorterCreateThread(pTask, vdbeSorterFlushThread, pCtx);
80983    }
80984  }
80985
80986  return rc;
80987#endif /* SQLITE_MAX_WORKER_THREADS!=0 */
80988}
80989
80990/*
80991** Add a record to the sorter.
80992*/
80993SQLITE_PRIVATE int sqlite3VdbeSorterWrite(
80994  const VdbeCursor *pCsr,         /* Sorter cursor */
80995  Mem *pVal                       /* Memory cell containing record */
80996){
80997  VdbeSorter *pSorter = pCsr->pSorter;
80998  int rc = SQLITE_OK;             /* Return Code */
80999  SorterRecord *pNew;             /* New list element */
81000
81001  int bFlush;                     /* True to flush contents of memory to PMA */
81002  int nReq;                       /* Bytes of memory required */
81003  int nPMA;                       /* Bytes of PMA space required */
81004  int t;                          /* serial type of first record field */
81005
81006  getVarint32((const u8*)&pVal->z[1], t);
81007  if( t>0 && t<10 && t!=7 ){
81008    pSorter->typeMask &= SORTER_TYPE_INTEGER;
81009  }else if( t>10 && (t & 0x01) ){
81010    pSorter->typeMask &= SORTER_TYPE_TEXT;
81011  }else{
81012    pSorter->typeMask = 0;
81013  }
81014
81015  assert( pSorter );
81016
81017  /* Figure out whether or not the current contents of memory should be
81018  ** flushed to a PMA before continuing. If so, do so.
81019  **
81020  ** If using the single large allocation mode (pSorter->aMemory!=0), then
81021  ** flush the contents of memory to a new PMA if (a) at least one value is
81022  ** already in memory and (b) the new value will not fit in memory.
81023  **
81024  ** Or, if using separate allocations for each record, flush the contents
81025  ** of memory to a PMA if either of the following are true:
81026  **
81027  **   * The total memory allocated for the in-memory list is greater
81028  **     than (page-size * cache-size), or
81029  **
81030  **   * The total memory allocated for the in-memory list is greater
81031  **     than (page-size * 10) and sqlite3HeapNearlyFull() returns true.
81032  */
81033  nReq = pVal->n + sizeof(SorterRecord);
81034  nPMA = pVal->n + sqlite3VarintLen(pVal->n);
81035  if( pSorter->mxPmaSize ){
81036    if( pSorter->list.aMemory ){
81037      bFlush = pSorter->iMemory && (pSorter->iMemory+nReq) > pSorter->mxPmaSize;
81038    }else{
81039      bFlush = (
81040          (pSorter->list.szPMA > pSorter->mxPmaSize)
81041       || (pSorter->list.szPMA > pSorter->mnPmaSize && sqlite3HeapNearlyFull())
81042      );
81043    }
81044    if( bFlush ){
81045      rc = vdbeSorterFlushPMA(pSorter);
81046      pSorter->list.szPMA = 0;
81047      pSorter->iMemory = 0;
81048      assert( rc!=SQLITE_OK || pSorter->list.pList==0 );
81049    }
81050  }
81051
81052  pSorter->list.szPMA += nPMA;
81053  if( nPMA>pSorter->mxKeysize ){
81054    pSorter->mxKeysize = nPMA;
81055  }
81056
81057  if( pSorter->list.aMemory ){
81058    int nMin = pSorter->iMemory + nReq;
81059
81060    if( nMin>pSorter->nMemory ){
81061      u8 *aNew;
81062      int nNew = pSorter->nMemory * 2;
81063      while( nNew < nMin ) nNew = nNew*2;
81064      if( nNew > pSorter->mxPmaSize ) nNew = pSorter->mxPmaSize;
81065      if( nNew < nMin ) nNew = nMin;
81066
81067      aNew = sqlite3Realloc(pSorter->list.aMemory, nNew);
81068      if( !aNew ) return SQLITE_NOMEM;
81069      pSorter->list.pList = (SorterRecord*)(
81070          aNew + ((u8*)pSorter->list.pList - pSorter->list.aMemory)
81071      );
81072      pSorter->list.aMemory = aNew;
81073      pSorter->nMemory = nNew;
81074    }
81075
81076    pNew = (SorterRecord*)&pSorter->list.aMemory[pSorter->iMemory];
81077    pSorter->iMemory += ROUND8(nReq);
81078    pNew->u.iNext = (int)((u8*)(pSorter->list.pList) - pSorter->list.aMemory);
81079  }else{
81080    pNew = (SorterRecord *)sqlite3Malloc(nReq);
81081    if( pNew==0 ){
81082      return SQLITE_NOMEM;
81083    }
81084    pNew->u.pNext = pSorter->list.pList;
81085  }
81086
81087  memcpy(SRVAL(pNew), pVal->z, pVal->n);
81088  pNew->nVal = pVal->n;
81089  pSorter->list.pList = pNew;
81090
81091  return rc;
81092}
81093
81094/*
81095** Read keys from pIncr->pMerger and populate pIncr->aFile[1]. The format
81096** of the data stored in aFile[1] is the same as that used by regular PMAs,
81097** except that the number-of-bytes varint is omitted from the start.
81098*/
81099static int vdbeIncrPopulate(IncrMerger *pIncr){
81100  int rc = SQLITE_OK;
81101  int rc2;
81102  i64 iStart = pIncr->iStartOff;
81103  SorterFile *pOut = &pIncr->aFile[1];
81104  SortSubtask *pTask = pIncr->pTask;
81105  MergeEngine *pMerger = pIncr->pMerger;
81106  PmaWriter writer;
81107  assert( pIncr->bEof==0 );
81108
81109  vdbeSorterPopulateDebug(pTask, "enter");
81110
81111  vdbePmaWriterInit(pOut->pFd, &writer, pTask->pSorter->pgsz, iStart);
81112  while( rc==SQLITE_OK ){
81113    int dummy;
81114    PmaReader *pReader = &pMerger->aReadr[ pMerger->aTree[1] ];
81115    int nKey = pReader->nKey;
81116    i64 iEof = writer.iWriteOff + writer.iBufEnd;
81117
81118    /* Check if the output file is full or if the input has been exhausted.
81119    ** In either case exit the loop. */
81120    if( pReader->pFd==0 ) break;
81121    if( (iEof + nKey + sqlite3VarintLen(nKey))>(iStart + pIncr->mxSz) ) break;
81122
81123    /* Write the next key to the output. */
81124    vdbePmaWriteVarint(&writer, nKey);
81125    vdbePmaWriteBlob(&writer, pReader->aKey, nKey);
81126    assert( pIncr->pMerger->pTask==pTask );
81127    rc = vdbeMergeEngineStep(pIncr->pMerger, &dummy);
81128  }
81129
81130  rc2 = vdbePmaWriterFinish(&writer, &pOut->iEof);
81131  if( rc==SQLITE_OK ) rc = rc2;
81132  vdbeSorterPopulateDebug(pTask, "exit");
81133  return rc;
81134}
81135
81136#if SQLITE_MAX_WORKER_THREADS>0
81137/*
81138** The main routine for background threads that populate aFile[1] of
81139** multi-threaded IncrMerger objects.
81140*/
81141static void *vdbeIncrPopulateThread(void *pCtx){
81142  IncrMerger *pIncr = (IncrMerger*)pCtx;
81143  void *pRet = SQLITE_INT_TO_PTR( vdbeIncrPopulate(pIncr) );
81144  pIncr->pTask->bDone = 1;
81145  return pRet;
81146}
81147
81148/*
81149** Launch a background thread to populate aFile[1] of pIncr.
81150*/
81151static int vdbeIncrBgPopulate(IncrMerger *pIncr){
81152  void *p = (void*)pIncr;
81153  assert( pIncr->bUseThread );
81154  return vdbeSorterCreateThread(pIncr->pTask, vdbeIncrPopulateThread, p);
81155}
81156#endif
81157
81158/*
81159** This function is called when the PmaReader corresponding to pIncr has
81160** finished reading the contents of aFile[0]. Its purpose is to "refill"
81161** aFile[0] such that the PmaReader should start rereading it from the
81162** beginning.
81163**
81164** For single-threaded objects, this is accomplished by literally reading
81165** keys from pIncr->pMerger and repopulating aFile[0].
81166**
81167** For multi-threaded objects, all that is required is to wait until the
81168** background thread is finished (if it is not already) and then swap
81169** aFile[0] and aFile[1] in place. If the contents of pMerger have not
81170** been exhausted, this function also launches a new background thread
81171** to populate the new aFile[1].
81172**
81173** SQLITE_OK is returned on success, or an SQLite error code otherwise.
81174*/
81175static int vdbeIncrSwap(IncrMerger *pIncr){
81176  int rc = SQLITE_OK;
81177
81178#if SQLITE_MAX_WORKER_THREADS>0
81179  if( pIncr->bUseThread ){
81180    rc = vdbeSorterJoinThread(pIncr->pTask);
81181
81182    if( rc==SQLITE_OK ){
81183      SorterFile f0 = pIncr->aFile[0];
81184      pIncr->aFile[0] = pIncr->aFile[1];
81185      pIncr->aFile[1] = f0;
81186    }
81187
81188    if( rc==SQLITE_OK ){
81189      if( pIncr->aFile[0].iEof==pIncr->iStartOff ){
81190        pIncr->bEof = 1;
81191      }else{
81192        rc = vdbeIncrBgPopulate(pIncr);
81193      }
81194    }
81195  }else
81196#endif
81197  {
81198    rc = vdbeIncrPopulate(pIncr);
81199    pIncr->aFile[0] = pIncr->aFile[1];
81200    if( pIncr->aFile[0].iEof==pIncr->iStartOff ){
81201      pIncr->bEof = 1;
81202    }
81203  }
81204
81205  return rc;
81206}
81207
81208/*
81209** Allocate and return a new IncrMerger object to read data from pMerger.
81210**
81211** If an OOM condition is encountered, return NULL. In this case free the
81212** pMerger argument before returning.
81213*/
81214static int vdbeIncrMergerNew(
81215  SortSubtask *pTask,     /* The thread that will be using the new IncrMerger */
81216  MergeEngine *pMerger,   /* The MergeEngine that the IncrMerger will control */
81217  IncrMerger **ppOut      /* Write the new IncrMerger here */
81218){
81219  int rc = SQLITE_OK;
81220  IncrMerger *pIncr = *ppOut = (IncrMerger*)
81221       (sqlite3FaultSim(100) ? 0 : sqlite3MallocZero(sizeof(*pIncr)));
81222  if( pIncr ){
81223    pIncr->pMerger = pMerger;
81224    pIncr->pTask = pTask;
81225    pIncr->mxSz = MAX(pTask->pSorter->mxKeysize+9,pTask->pSorter->mxPmaSize/2);
81226    pTask->file2.iEof += pIncr->mxSz;
81227  }else{
81228    vdbeMergeEngineFree(pMerger);
81229    rc = SQLITE_NOMEM;
81230  }
81231  return rc;
81232}
81233
81234#if SQLITE_MAX_WORKER_THREADS>0
81235/*
81236** Set the "use-threads" flag on object pIncr.
81237*/
81238static void vdbeIncrMergerSetThreads(IncrMerger *pIncr){
81239  pIncr->bUseThread = 1;
81240  pIncr->pTask->file2.iEof -= pIncr->mxSz;
81241}
81242#endif /* SQLITE_MAX_WORKER_THREADS>0 */
81243
81244
81245
81246/*
81247** Recompute pMerger->aTree[iOut] by comparing the next keys on the
81248** two PmaReaders that feed that entry.  Neither of the PmaReaders
81249** are advanced.  This routine merely does the comparison.
81250*/
81251static void vdbeMergeEngineCompare(
81252  MergeEngine *pMerger,  /* Merge engine containing PmaReaders to compare */
81253  int iOut               /* Store the result in pMerger->aTree[iOut] */
81254){
81255  int i1;
81256  int i2;
81257  int iRes;
81258  PmaReader *p1;
81259  PmaReader *p2;
81260
81261  assert( iOut<pMerger->nTree && iOut>0 );
81262
81263  if( iOut>=(pMerger->nTree/2) ){
81264    i1 = (iOut - pMerger->nTree/2) * 2;
81265    i2 = i1 + 1;
81266  }else{
81267    i1 = pMerger->aTree[iOut*2];
81268    i2 = pMerger->aTree[iOut*2+1];
81269  }
81270
81271  p1 = &pMerger->aReadr[i1];
81272  p2 = &pMerger->aReadr[i2];
81273
81274  if( p1->pFd==0 ){
81275    iRes = i2;
81276  }else if( p2->pFd==0 ){
81277    iRes = i1;
81278  }else{
81279    SortSubtask *pTask = pMerger->pTask;
81280    int bCached = 0;
81281    int res;
81282    assert( pTask->pUnpacked!=0 );  /* from vdbeSortSubtaskMain() */
81283    res = pTask->xCompare(
81284        pTask, &bCached, p1->aKey, p1->nKey, p2->aKey, p2->nKey
81285    );
81286    if( res<=0 ){
81287      iRes = i1;
81288    }else{
81289      iRes = i2;
81290    }
81291  }
81292
81293  pMerger->aTree[iOut] = iRes;
81294}
81295
81296/*
81297** Allowed values for the eMode parameter to vdbeMergeEngineInit()
81298** and vdbePmaReaderIncrMergeInit().
81299**
81300** Only INCRINIT_NORMAL is valid in single-threaded builds (when
81301** SQLITE_MAX_WORKER_THREADS==0).  The other values are only used
81302** when there exists one or more separate worker threads.
81303*/
81304#define INCRINIT_NORMAL 0
81305#define INCRINIT_TASK   1
81306#define INCRINIT_ROOT   2
81307
81308/*
81309** Forward reference required as the vdbeIncrMergeInit() and
81310** vdbePmaReaderIncrInit() routines are called mutually recursively when
81311** building a merge tree.
81312*/
81313static int vdbePmaReaderIncrInit(PmaReader *pReadr, int eMode);
81314
81315/*
81316** Initialize the MergeEngine object passed as the second argument. Once this
81317** function returns, the first key of merged data may be read from the
81318** MergeEngine object in the usual fashion.
81319**
81320** If argument eMode is INCRINIT_ROOT, then it is assumed that any IncrMerge
81321** objects attached to the PmaReader objects that the merger reads from have
81322** already been populated, but that they have not yet populated aFile[0] and
81323** set the PmaReader objects up to read from it. In this case all that is
81324** required is to call vdbePmaReaderNext() on each PmaReader to point it at
81325** its first key.
81326**
81327** Otherwise, if eMode is any value other than INCRINIT_ROOT, then use
81328** vdbePmaReaderIncrMergeInit() to initialize each PmaReader that feeds data
81329** to pMerger.
81330**
81331** SQLITE_OK is returned if successful, or an SQLite error code otherwise.
81332*/
81333static int vdbeMergeEngineInit(
81334  SortSubtask *pTask,             /* Thread that will run pMerger */
81335  MergeEngine *pMerger,           /* MergeEngine to initialize */
81336  int eMode                       /* One of the INCRINIT_XXX constants */
81337){
81338  int rc = SQLITE_OK;             /* Return code */
81339  int i;                          /* For looping over PmaReader objects */
81340  int nTree = pMerger->nTree;
81341
81342  /* eMode is always INCRINIT_NORMAL in single-threaded mode */
81343  assert( SQLITE_MAX_WORKER_THREADS>0 || eMode==INCRINIT_NORMAL );
81344
81345  /* Verify that the MergeEngine is assigned to a single thread */
81346  assert( pMerger->pTask==0 );
81347  pMerger->pTask = pTask;
81348
81349  for(i=0; i<nTree; i++){
81350    if( SQLITE_MAX_WORKER_THREADS>0 && eMode==INCRINIT_ROOT ){
81351      /* PmaReaders should be normally initialized in order, as if they are
81352      ** reading from the same temp file this makes for more linear file IO.
81353      ** However, in the INCRINIT_ROOT case, if PmaReader aReadr[nTask-1] is
81354      ** in use it will block the vdbePmaReaderNext() call while it uses
81355      ** the main thread to fill its buffer. So calling PmaReaderNext()
81356      ** on this PmaReader before any of the multi-threaded PmaReaders takes
81357      ** better advantage of multi-processor hardware. */
81358      rc = vdbePmaReaderNext(&pMerger->aReadr[nTree-i-1]);
81359    }else{
81360      rc = vdbePmaReaderIncrInit(&pMerger->aReadr[i], INCRINIT_NORMAL);
81361    }
81362    if( rc!=SQLITE_OK ) return rc;
81363  }
81364
81365  for(i=pMerger->nTree-1; i>0; i--){
81366    vdbeMergeEngineCompare(pMerger, i);
81367  }
81368  return pTask->pUnpacked->errCode;
81369}
81370
81371/*
81372** The PmaReader passed as the first argument is guaranteed to be an
81373** incremental-reader (pReadr->pIncr!=0). This function serves to open
81374** and/or initialize the temp file related fields of the IncrMerge
81375** object at (pReadr->pIncr).
81376**
81377** If argument eMode is set to INCRINIT_NORMAL, then all PmaReaders
81378** in the sub-tree headed by pReadr are also initialized. Data is then
81379** loaded into the buffers belonging to pReadr and it is set to point to
81380** the first key in its range.
81381**
81382** If argument eMode is set to INCRINIT_TASK, then pReadr is guaranteed
81383** to be a multi-threaded PmaReader and this function is being called in a
81384** background thread. In this case all PmaReaders in the sub-tree are
81385** initialized as for INCRINIT_NORMAL and the aFile[1] buffer belonging to
81386** pReadr is populated. However, pReadr itself is not set up to point
81387** to its first key. A call to vdbePmaReaderNext() is still required to do
81388** that.
81389**
81390** The reason this function does not call vdbePmaReaderNext() immediately
81391** in the INCRINIT_TASK case is that vdbePmaReaderNext() assumes that it has
81392** to block on thread (pTask->thread) before accessing aFile[1]. But, since
81393** this entire function is being run by thread (pTask->thread), that will
81394** lead to the current background thread attempting to join itself.
81395**
81396** Finally, if argument eMode is set to INCRINIT_ROOT, it may be assumed
81397** that pReadr->pIncr is a multi-threaded IncrMerge objects, and that all
81398** child-trees have already been initialized using IncrInit(INCRINIT_TASK).
81399** In this case vdbePmaReaderNext() is called on all child PmaReaders and
81400** the current PmaReader set to point to the first key in its range.
81401**
81402** SQLITE_OK is returned if successful, or an SQLite error code otherwise.
81403*/
81404static int vdbePmaReaderIncrMergeInit(PmaReader *pReadr, int eMode){
81405  int rc = SQLITE_OK;
81406  IncrMerger *pIncr = pReadr->pIncr;
81407  SortSubtask *pTask = pIncr->pTask;
81408  sqlite3 *db = pTask->pSorter->db;
81409
81410  /* eMode is always INCRINIT_NORMAL in single-threaded mode */
81411  assert( SQLITE_MAX_WORKER_THREADS>0 || eMode==INCRINIT_NORMAL );
81412
81413  rc = vdbeMergeEngineInit(pTask, pIncr->pMerger, eMode);
81414
81415  /* Set up the required files for pIncr. A multi-theaded IncrMerge object
81416  ** requires two temp files to itself, whereas a single-threaded object
81417  ** only requires a region of pTask->file2. */
81418  if( rc==SQLITE_OK ){
81419    int mxSz = pIncr->mxSz;
81420#if SQLITE_MAX_WORKER_THREADS>0
81421    if( pIncr->bUseThread ){
81422      rc = vdbeSorterOpenTempFile(db, mxSz, &pIncr->aFile[0].pFd);
81423      if( rc==SQLITE_OK ){
81424        rc = vdbeSorterOpenTempFile(db, mxSz, &pIncr->aFile[1].pFd);
81425      }
81426    }else
81427#endif
81428    /*if( !pIncr->bUseThread )*/{
81429      if( pTask->file2.pFd==0 ){
81430        assert( pTask->file2.iEof>0 );
81431        rc = vdbeSorterOpenTempFile(db, pTask->file2.iEof, &pTask->file2.pFd);
81432        pTask->file2.iEof = 0;
81433      }
81434      if( rc==SQLITE_OK ){
81435        pIncr->aFile[1].pFd = pTask->file2.pFd;
81436        pIncr->iStartOff = pTask->file2.iEof;
81437        pTask->file2.iEof += mxSz;
81438      }
81439    }
81440  }
81441
81442#if SQLITE_MAX_WORKER_THREADS>0
81443  if( rc==SQLITE_OK && pIncr->bUseThread ){
81444    /* Use the current thread to populate aFile[1], even though this
81445    ** PmaReader is multi-threaded. If this is an INCRINIT_TASK object,
81446    ** then this function is already running in background thread
81447    ** pIncr->pTask->thread.
81448    **
81449    ** If this is the INCRINIT_ROOT object, then it is running in the
81450    ** main VDBE thread. But that is Ok, as that thread cannot return
81451    ** control to the VDBE or proceed with anything useful until the
81452    ** first results are ready from this merger object anyway.
81453    */
81454    assert( eMode==INCRINIT_ROOT || eMode==INCRINIT_TASK );
81455    rc = vdbeIncrPopulate(pIncr);
81456  }
81457#endif
81458
81459  if( rc==SQLITE_OK && (SQLITE_MAX_WORKER_THREADS==0 || eMode!=INCRINIT_TASK) ){
81460    rc = vdbePmaReaderNext(pReadr);
81461  }
81462
81463  return rc;
81464}
81465
81466#if SQLITE_MAX_WORKER_THREADS>0
81467/*
81468** The main routine for vdbePmaReaderIncrMergeInit() operations run in
81469** background threads.
81470*/
81471static void *vdbePmaReaderBgIncrInit(void *pCtx){
81472  PmaReader *pReader = (PmaReader*)pCtx;
81473  void *pRet = SQLITE_INT_TO_PTR(
81474                  vdbePmaReaderIncrMergeInit(pReader,INCRINIT_TASK)
81475               );
81476  pReader->pIncr->pTask->bDone = 1;
81477  return pRet;
81478}
81479#endif
81480
81481/*
81482** If the PmaReader passed as the first argument is not an incremental-reader
81483** (if pReadr->pIncr==0), then this function is a no-op. Otherwise, it invokes
81484** the vdbePmaReaderIncrMergeInit() function with the parameters passed to
81485** this routine to initialize the incremental merge.
81486**
81487** If the IncrMerger object is multi-threaded (IncrMerger.bUseThread==1),
81488** then a background thread is launched to call vdbePmaReaderIncrMergeInit().
81489** Or, if the IncrMerger is single threaded, the same function is called
81490** using the current thread.
81491*/
81492static int vdbePmaReaderIncrInit(PmaReader *pReadr, int eMode){
81493  IncrMerger *pIncr = pReadr->pIncr;   /* Incremental merger */
81494  int rc = SQLITE_OK;                  /* Return code */
81495  if( pIncr ){
81496#if SQLITE_MAX_WORKER_THREADS>0
81497    assert( pIncr->bUseThread==0 || eMode==INCRINIT_TASK );
81498    if( pIncr->bUseThread ){
81499      void *pCtx = (void*)pReadr;
81500      rc = vdbeSorterCreateThread(pIncr->pTask, vdbePmaReaderBgIncrInit, pCtx);
81501    }else
81502#endif
81503    {
81504      rc = vdbePmaReaderIncrMergeInit(pReadr, eMode);
81505    }
81506  }
81507  return rc;
81508}
81509
81510/*
81511** Allocate a new MergeEngine object to merge the contents of nPMA level-0
81512** PMAs from pTask->file. If no error occurs, set *ppOut to point to
81513** the new object and return SQLITE_OK. Or, if an error does occur, set *ppOut
81514** to NULL and return an SQLite error code.
81515**
81516** When this function is called, *piOffset is set to the offset of the
81517** first PMA to read from pTask->file. Assuming no error occurs, it is
81518** set to the offset immediately following the last byte of the last
81519** PMA before returning. If an error does occur, then the final value of
81520** *piOffset is undefined.
81521*/
81522static int vdbeMergeEngineLevel0(
81523  SortSubtask *pTask,             /* Sorter task to read from */
81524  int nPMA,                       /* Number of PMAs to read */
81525  i64 *piOffset,                  /* IN/OUT: Readr offset in pTask->file */
81526  MergeEngine **ppOut             /* OUT: New merge-engine */
81527){
81528  MergeEngine *pNew;              /* Merge engine to return */
81529  i64 iOff = *piOffset;
81530  int i;
81531  int rc = SQLITE_OK;
81532
81533  *ppOut = pNew = vdbeMergeEngineNew(nPMA);
81534  if( pNew==0 ) rc = SQLITE_NOMEM;
81535
81536  for(i=0; i<nPMA && rc==SQLITE_OK; i++){
81537    i64 nDummy;
81538    PmaReader *pReadr = &pNew->aReadr[i];
81539    rc = vdbePmaReaderInit(pTask, &pTask->file, iOff, pReadr, &nDummy);
81540    iOff = pReadr->iEof;
81541  }
81542
81543  if( rc!=SQLITE_OK ){
81544    vdbeMergeEngineFree(pNew);
81545    *ppOut = 0;
81546  }
81547  *piOffset = iOff;
81548  return rc;
81549}
81550
81551/*
81552** Return the depth of a tree comprising nPMA PMAs, assuming a fanout of
81553** SORTER_MAX_MERGE_COUNT. The returned value does not include leaf nodes.
81554**
81555** i.e.
81556**
81557**   nPMA<=16    -> TreeDepth() == 0
81558**   nPMA<=256   -> TreeDepth() == 1
81559**   nPMA<=65536 -> TreeDepth() == 2
81560*/
81561static int vdbeSorterTreeDepth(int nPMA){
81562  int nDepth = 0;
81563  i64 nDiv = SORTER_MAX_MERGE_COUNT;
81564  while( nDiv < (i64)nPMA ){
81565    nDiv = nDiv * SORTER_MAX_MERGE_COUNT;
81566    nDepth++;
81567  }
81568  return nDepth;
81569}
81570
81571/*
81572** pRoot is the root of an incremental merge-tree with depth nDepth (according
81573** to vdbeSorterTreeDepth()). pLeaf is the iSeq'th leaf to be added to the
81574** tree, counting from zero. This function adds pLeaf to the tree.
81575**
81576** If successful, SQLITE_OK is returned. If an error occurs, an SQLite error
81577** code is returned and pLeaf is freed.
81578*/
81579static int vdbeSorterAddToTree(
81580  SortSubtask *pTask,             /* Task context */
81581  int nDepth,                     /* Depth of tree according to TreeDepth() */
81582  int iSeq,                       /* Sequence number of leaf within tree */
81583  MergeEngine *pRoot,             /* Root of tree */
81584  MergeEngine *pLeaf              /* Leaf to add to tree */
81585){
81586  int rc = SQLITE_OK;
81587  int nDiv = 1;
81588  int i;
81589  MergeEngine *p = pRoot;
81590  IncrMerger *pIncr;
81591
81592  rc = vdbeIncrMergerNew(pTask, pLeaf, &pIncr);
81593
81594  for(i=1; i<nDepth; i++){
81595    nDiv = nDiv * SORTER_MAX_MERGE_COUNT;
81596  }
81597
81598  for(i=1; i<nDepth && rc==SQLITE_OK; i++){
81599    int iIter = (iSeq / nDiv) % SORTER_MAX_MERGE_COUNT;
81600    PmaReader *pReadr = &p->aReadr[iIter];
81601
81602    if( pReadr->pIncr==0 ){
81603      MergeEngine *pNew = vdbeMergeEngineNew(SORTER_MAX_MERGE_COUNT);
81604      if( pNew==0 ){
81605        rc = SQLITE_NOMEM;
81606      }else{
81607        rc = vdbeIncrMergerNew(pTask, pNew, &pReadr->pIncr);
81608      }
81609    }
81610    if( rc==SQLITE_OK ){
81611      p = pReadr->pIncr->pMerger;
81612      nDiv = nDiv / SORTER_MAX_MERGE_COUNT;
81613    }
81614  }
81615
81616  if( rc==SQLITE_OK ){
81617    p->aReadr[iSeq % SORTER_MAX_MERGE_COUNT].pIncr = pIncr;
81618  }else{
81619    vdbeIncrFree(pIncr);
81620  }
81621  return rc;
81622}
81623
81624/*
81625** This function is called as part of a SorterRewind() operation on a sorter
81626** that has already written two or more level-0 PMAs to one or more temp
81627** files. It builds a tree of MergeEngine/IncrMerger/PmaReader objects that
81628** can be used to incrementally merge all PMAs on disk.
81629**
81630** If successful, SQLITE_OK is returned and *ppOut set to point to the
81631** MergeEngine object at the root of the tree before returning. Or, if an
81632** error occurs, an SQLite error code is returned and the final value
81633** of *ppOut is undefined.
81634*/
81635static int vdbeSorterMergeTreeBuild(
81636  VdbeSorter *pSorter,       /* The VDBE cursor that implements the sort */
81637  MergeEngine **ppOut        /* Write the MergeEngine here */
81638){
81639  MergeEngine *pMain = 0;
81640  int rc = SQLITE_OK;
81641  int iTask;
81642
81643#if SQLITE_MAX_WORKER_THREADS>0
81644  /* If the sorter uses more than one task, then create the top-level
81645  ** MergeEngine here. This MergeEngine will read data from exactly
81646  ** one PmaReader per sub-task.  */
81647  assert( pSorter->bUseThreads || pSorter->nTask==1 );
81648  if( pSorter->nTask>1 ){
81649    pMain = vdbeMergeEngineNew(pSorter->nTask);
81650    if( pMain==0 ) rc = SQLITE_NOMEM;
81651  }
81652#endif
81653
81654  for(iTask=0; rc==SQLITE_OK && iTask<pSorter->nTask; iTask++){
81655    SortSubtask *pTask = &pSorter->aTask[iTask];
81656    assert( pTask->nPMA>0 || SQLITE_MAX_WORKER_THREADS>0 );
81657    if( SQLITE_MAX_WORKER_THREADS==0 || pTask->nPMA ){
81658      MergeEngine *pRoot = 0;     /* Root node of tree for this task */
81659      int nDepth = vdbeSorterTreeDepth(pTask->nPMA);
81660      i64 iReadOff = 0;
81661
81662      if( pTask->nPMA<=SORTER_MAX_MERGE_COUNT ){
81663        rc = vdbeMergeEngineLevel0(pTask, pTask->nPMA, &iReadOff, &pRoot);
81664      }else{
81665        int i;
81666        int iSeq = 0;
81667        pRoot = vdbeMergeEngineNew(SORTER_MAX_MERGE_COUNT);
81668        if( pRoot==0 ) rc = SQLITE_NOMEM;
81669        for(i=0; i<pTask->nPMA && rc==SQLITE_OK; i += SORTER_MAX_MERGE_COUNT){
81670          MergeEngine *pMerger = 0; /* New level-0 PMA merger */
81671          int nReader;              /* Number of level-0 PMAs to merge */
81672
81673          nReader = MIN(pTask->nPMA - i, SORTER_MAX_MERGE_COUNT);
81674          rc = vdbeMergeEngineLevel0(pTask, nReader, &iReadOff, &pMerger);
81675          if( rc==SQLITE_OK ){
81676            rc = vdbeSorterAddToTree(pTask, nDepth, iSeq++, pRoot, pMerger);
81677          }
81678        }
81679      }
81680
81681      if( rc==SQLITE_OK ){
81682#if SQLITE_MAX_WORKER_THREADS>0
81683        if( pMain!=0 ){
81684          rc = vdbeIncrMergerNew(pTask, pRoot, &pMain->aReadr[iTask].pIncr);
81685        }else
81686#endif
81687        {
81688          assert( pMain==0 );
81689          pMain = pRoot;
81690        }
81691      }else{
81692        vdbeMergeEngineFree(pRoot);
81693      }
81694    }
81695  }
81696
81697  if( rc!=SQLITE_OK ){
81698    vdbeMergeEngineFree(pMain);
81699    pMain = 0;
81700  }
81701  *ppOut = pMain;
81702  return rc;
81703}
81704
81705/*
81706** This function is called as part of an sqlite3VdbeSorterRewind() operation
81707** on a sorter that has written two or more PMAs to temporary files. It sets
81708** up either VdbeSorter.pMerger (for single threaded sorters) or pReader
81709** (for multi-threaded sorters) so that it can be used to iterate through
81710** all records stored in the sorter.
81711**
81712** SQLITE_OK is returned if successful, or an SQLite error code otherwise.
81713*/
81714static int vdbeSorterSetupMerge(VdbeSorter *pSorter){
81715  int rc;                         /* Return code */
81716  SortSubtask *pTask0 = &pSorter->aTask[0];
81717  MergeEngine *pMain = 0;
81718#if SQLITE_MAX_WORKER_THREADS
81719  sqlite3 *db = pTask0->pSorter->db;
81720  int i;
81721  SorterCompare xCompare = vdbeSorterGetCompare(pSorter);
81722  for(i=0; i<pSorter->nTask; i++){
81723    pSorter->aTask[i].xCompare = xCompare;
81724  }
81725#endif
81726
81727  rc = vdbeSorterMergeTreeBuild(pSorter, &pMain);
81728  if( rc==SQLITE_OK ){
81729#if SQLITE_MAX_WORKER_THREADS
81730    assert( pSorter->bUseThreads==0 || pSorter->nTask>1 );
81731    if( pSorter->bUseThreads ){
81732      int iTask;
81733      PmaReader *pReadr = 0;
81734      SortSubtask *pLast = &pSorter->aTask[pSorter->nTask-1];
81735      rc = vdbeSortAllocUnpacked(pLast);
81736      if( rc==SQLITE_OK ){
81737        pReadr = (PmaReader*)sqlite3DbMallocZero(db, sizeof(PmaReader));
81738        pSorter->pReader = pReadr;
81739        if( pReadr==0 ) rc = SQLITE_NOMEM;
81740      }
81741      if( rc==SQLITE_OK ){
81742        rc = vdbeIncrMergerNew(pLast, pMain, &pReadr->pIncr);
81743        if( rc==SQLITE_OK ){
81744          vdbeIncrMergerSetThreads(pReadr->pIncr);
81745          for(iTask=0; iTask<(pSorter->nTask-1); iTask++){
81746            IncrMerger *pIncr;
81747            if( (pIncr = pMain->aReadr[iTask].pIncr) ){
81748              vdbeIncrMergerSetThreads(pIncr);
81749              assert( pIncr->pTask!=pLast );
81750            }
81751          }
81752          for(iTask=0; rc==SQLITE_OK && iTask<pSorter->nTask; iTask++){
81753            /* Check that:
81754            **
81755            **   a) The incremental merge object is configured to use the
81756            **      right task, and
81757            **   b) If it is using task (nTask-1), it is configured to run
81758            **      in single-threaded mode. This is important, as the
81759            **      root merge (INCRINIT_ROOT) will be using the same task
81760            **      object.
81761            */
81762            PmaReader *p = &pMain->aReadr[iTask];
81763            assert( p->pIncr==0 || (
81764                (p->pIncr->pTask==&pSorter->aTask[iTask])             /* a */
81765             && (iTask!=pSorter->nTask-1 || p->pIncr->bUseThread==0)  /* b */
81766            ));
81767            rc = vdbePmaReaderIncrInit(p, INCRINIT_TASK);
81768          }
81769        }
81770        pMain = 0;
81771      }
81772      if( rc==SQLITE_OK ){
81773        rc = vdbePmaReaderIncrMergeInit(pReadr, INCRINIT_ROOT);
81774      }
81775    }else
81776#endif
81777    {
81778      rc = vdbeMergeEngineInit(pTask0, pMain, INCRINIT_NORMAL);
81779      pSorter->pMerger = pMain;
81780      pMain = 0;
81781    }
81782  }
81783
81784  if( rc!=SQLITE_OK ){
81785    vdbeMergeEngineFree(pMain);
81786  }
81787  return rc;
81788}
81789
81790
81791/*
81792** Once the sorter has been populated by calls to sqlite3VdbeSorterWrite,
81793** this function is called to prepare for iterating through the records
81794** in sorted order.
81795*/
81796SQLITE_PRIVATE int sqlite3VdbeSorterRewind(const VdbeCursor *pCsr, int *pbEof){
81797  VdbeSorter *pSorter = pCsr->pSorter;
81798  int rc = SQLITE_OK;             /* Return code */
81799
81800  assert( pSorter );
81801
81802  /* If no data has been written to disk, then do not do so now. Instead,
81803  ** sort the VdbeSorter.pRecord list. The vdbe layer will read data directly
81804  ** from the in-memory list.  */
81805  if( pSorter->bUsePMA==0 ){
81806    if( pSorter->list.pList ){
81807      *pbEof = 0;
81808      rc = vdbeSorterSort(&pSorter->aTask[0], &pSorter->list);
81809    }else{
81810      *pbEof = 1;
81811    }
81812    return rc;
81813  }
81814
81815  /* Write the current in-memory list to a PMA. When the VdbeSorterWrite()
81816  ** function flushes the contents of memory to disk, it immediately always
81817  ** creates a new list consisting of a single key immediately afterwards.
81818  ** So the list is never empty at this point.  */
81819  assert( pSorter->list.pList );
81820  rc = vdbeSorterFlushPMA(pSorter);
81821
81822  /* Join all threads */
81823  rc = vdbeSorterJoinAll(pSorter, rc);
81824
81825  vdbeSorterRewindDebug("rewind");
81826
81827  /* Assuming no errors have occurred, set up a merger structure to
81828  ** incrementally read and merge all remaining PMAs.  */
81829  assert( pSorter->pReader==0 );
81830  if( rc==SQLITE_OK ){
81831    rc = vdbeSorterSetupMerge(pSorter);
81832    *pbEof = 0;
81833  }
81834
81835  vdbeSorterRewindDebug("rewinddone");
81836  return rc;
81837}
81838
81839/*
81840** Advance to the next element in the sorter.
81841*/
81842SQLITE_PRIVATE int sqlite3VdbeSorterNext(sqlite3 *db, const VdbeCursor *pCsr, int *pbEof){
81843  VdbeSorter *pSorter = pCsr->pSorter;
81844  int rc;                         /* Return code */
81845
81846  assert( pSorter->bUsePMA || (pSorter->pReader==0 && pSorter->pMerger==0) );
81847  if( pSorter->bUsePMA ){
81848    assert( pSorter->pReader==0 || pSorter->pMerger==0 );
81849    assert( pSorter->bUseThreads==0 || pSorter->pReader );
81850    assert( pSorter->bUseThreads==1 || pSorter->pMerger );
81851#if SQLITE_MAX_WORKER_THREADS>0
81852    if( pSorter->bUseThreads ){
81853      rc = vdbePmaReaderNext(pSorter->pReader);
81854      *pbEof = (pSorter->pReader->pFd==0);
81855    }else
81856#endif
81857    /*if( !pSorter->bUseThreads )*/ {
81858      assert( pSorter->pMerger!=0 );
81859      assert( pSorter->pMerger->pTask==(&pSorter->aTask[0]) );
81860      rc = vdbeMergeEngineStep(pSorter->pMerger, pbEof);
81861    }
81862  }else{
81863    SorterRecord *pFree = pSorter->list.pList;
81864    pSorter->list.pList = pFree->u.pNext;
81865    pFree->u.pNext = 0;
81866    if( pSorter->list.aMemory==0 ) vdbeSorterRecordFree(db, pFree);
81867    *pbEof = !pSorter->list.pList;
81868    rc = SQLITE_OK;
81869  }
81870  return rc;
81871}
81872
81873/*
81874** Return a pointer to a buffer owned by the sorter that contains the
81875** current key.
81876*/
81877static void *vdbeSorterRowkey(
81878  const VdbeSorter *pSorter,      /* Sorter object */
81879  int *pnKey                      /* OUT: Size of current key in bytes */
81880){
81881  void *pKey;
81882  if( pSorter->bUsePMA ){
81883    PmaReader *pReader;
81884#if SQLITE_MAX_WORKER_THREADS>0
81885    if( pSorter->bUseThreads ){
81886      pReader = pSorter->pReader;
81887    }else
81888#endif
81889    /*if( !pSorter->bUseThreads )*/{
81890      pReader = &pSorter->pMerger->aReadr[pSorter->pMerger->aTree[1]];
81891    }
81892    *pnKey = pReader->nKey;
81893    pKey = pReader->aKey;
81894  }else{
81895    *pnKey = pSorter->list.pList->nVal;
81896    pKey = SRVAL(pSorter->list.pList);
81897  }
81898  return pKey;
81899}
81900
81901/*
81902** Copy the current sorter key into the memory cell pOut.
81903*/
81904SQLITE_PRIVATE int sqlite3VdbeSorterRowkey(const VdbeCursor *pCsr, Mem *pOut){
81905  VdbeSorter *pSorter = pCsr->pSorter;
81906  void *pKey; int nKey;           /* Sorter key to copy into pOut */
81907
81908  pKey = vdbeSorterRowkey(pSorter, &nKey);
81909  if( sqlite3VdbeMemClearAndResize(pOut, nKey) ){
81910    return SQLITE_NOMEM;
81911  }
81912  pOut->n = nKey;
81913  MemSetTypeFlag(pOut, MEM_Blob);
81914  memcpy(pOut->z, pKey, nKey);
81915
81916  return SQLITE_OK;
81917}
81918
81919/*
81920** Compare the key in memory cell pVal with the key that the sorter cursor
81921** passed as the first argument currently points to. For the purposes of
81922** the comparison, ignore the rowid field at the end of each record.
81923**
81924** If the sorter cursor key contains any NULL values, consider it to be
81925** less than pVal. Even if pVal also contains NULL values.
81926**
81927** If an error occurs, return an SQLite error code (i.e. SQLITE_NOMEM).
81928** Otherwise, set *pRes to a negative, zero or positive value if the
81929** key in pVal is smaller than, equal to or larger than the current sorter
81930** key.
81931**
81932** This routine forms the core of the OP_SorterCompare opcode, which in
81933** turn is used to verify uniqueness when constructing a UNIQUE INDEX.
81934*/
81935SQLITE_PRIVATE int sqlite3VdbeSorterCompare(
81936  const VdbeCursor *pCsr,         /* Sorter cursor */
81937  Mem *pVal,                      /* Value to compare to current sorter key */
81938  int nKeyCol,                    /* Compare this many columns */
81939  int *pRes                       /* OUT: Result of comparison */
81940){
81941  VdbeSorter *pSorter = pCsr->pSorter;
81942  UnpackedRecord *r2 = pSorter->pUnpacked;
81943  KeyInfo *pKeyInfo = pCsr->pKeyInfo;
81944  int i;
81945  void *pKey; int nKey;           /* Sorter key to compare pVal with */
81946
81947  if( r2==0 ){
81948    char *p;
81949    r2 = pSorter->pUnpacked = sqlite3VdbeAllocUnpackedRecord(pKeyInfo,0,0,&p);
81950    assert( pSorter->pUnpacked==(UnpackedRecord*)p );
81951    if( r2==0 ) return SQLITE_NOMEM;
81952    r2->nField = nKeyCol;
81953  }
81954  assert( r2->nField==nKeyCol );
81955
81956  pKey = vdbeSorterRowkey(pSorter, &nKey);
81957  sqlite3VdbeRecordUnpack(pKeyInfo, nKey, pKey, r2);
81958  for(i=0; i<nKeyCol; i++){
81959    if( r2->aMem[i].flags & MEM_Null ){
81960      *pRes = -1;
81961      return SQLITE_OK;
81962    }
81963  }
81964
81965  *pRes = sqlite3VdbeRecordCompare(pVal->n, pVal->z, r2);
81966  return SQLITE_OK;
81967}
81968
81969/************** End of vdbesort.c ********************************************/
81970/************** Begin file journal.c *****************************************/
81971/*
81972** 2007 August 22
81973**
81974** The author disclaims copyright to this source code.  In place of
81975** a legal notice, here is a blessing:
81976**
81977**    May you do good and not evil.
81978**    May you find forgiveness for yourself and forgive others.
81979**    May you share freely, never taking more than you give.
81980**
81981*************************************************************************
81982**
81983** This file implements a special kind of sqlite3_file object used
81984** by SQLite to create journal files if the atomic-write optimization
81985** is enabled.
81986**
81987** The distinctive characteristic of this sqlite3_file is that the
81988** actual on disk file is created lazily. When the file is created,
81989** the caller specifies a buffer size for an in-memory buffer to
81990** be used to service read() and write() requests. The actual file
81991** on disk is not created or populated until either:
81992**
81993**   1) The in-memory representation grows too large for the allocated
81994**      buffer, or
81995**   2) The sqlite3JournalCreate() function is called.
81996*/
81997#ifdef SQLITE_ENABLE_ATOMIC_WRITE
81998/* #include "sqliteInt.h" */
81999
82000
82001/*
82002** A JournalFile object is a subclass of sqlite3_file used by
82003** as an open file handle for journal files.
82004*/
82005struct JournalFile {
82006  sqlite3_io_methods *pMethod;    /* I/O methods on journal files */
82007  int nBuf;                       /* Size of zBuf[] in bytes */
82008  char *zBuf;                     /* Space to buffer journal writes */
82009  int iSize;                      /* Amount of zBuf[] currently used */
82010  int flags;                      /* xOpen flags */
82011  sqlite3_vfs *pVfs;              /* The "real" underlying VFS */
82012  sqlite3_file *pReal;            /* The "real" underlying file descriptor */
82013  const char *zJournal;           /* Name of the journal file */
82014};
82015typedef struct JournalFile JournalFile;
82016
82017/*
82018** If it does not already exists, create and populate the on-disk file
82019** for JournalFile p.
82020*/
82021static int createFile(JournalFile *p){
82022  int rc = SQLITE_OK;
82023  if( !p->pReal ){
82024    sqlite3_file *pReal = (sqlite3_file *)&p[1];
82025    rc = sqlite3OsOpen(p->pVfs, p->zJournal, pReal, p->flags, 0);
82026    if( rc==SQLITE_OK ){
82027      p->pReal = pReal;
82028      if( p->iSize>0 ){
82029        assert(p->iSize<=p->nBuf);
82030        rc = sqlite3OsWrite(p->pReal, p->zBuf, p->iSize, 0);
82031      }
82032      if( rc!=SQLITE_OK ){
82033        /* If an error occurred while writing to the file, close it before
82034        ** returning. This way, SQLite uses the in-memory journal data to
82035        ** roll back changes made to the internal page-cache before this
82036        ** function was called.  */
82037        sqlite3OsClose(pReal);
82038        p->pReal = 0;
82039      }
82040    }
82041  }
82042  return rc;
82043}
82044
82045/*
82046** Close the file.
82047*/
82048static int jrnlClose(sqlite3_file *pJfd){
82049  JournalFile *p = (JournalFile *)pJfd;
82050  if( p->pReal ){
82051    sqlite3OsClose(p->pReal);
82052  }
82053  sqlite3_free(p->zBuf);
82054  return SQLITE_OK;
82055}
82056
82057/*
82058** Read data from the file.
82059*/
82060static int jrnlRead(
82061  sqlite3_file *pJfd,    /* The journal file from which to read */
82062  void *zBuf,            /* Put the results here */
82063  int iAmt,              /* Number of bytes to read */
82064  sqlite_int64 iOfst     /* Begin reading at this offset */
82065){
82066  int rc = SQLITE_OK;
82067  JournalFile *p = (JournalFile *)pJfd;
82068  if( p->pReal ){
82069    rc = sqlite3OsRead(p->pReal, zBuf, iAmt, iOfst);
82070  }else if( (iAmt+iOfst)>p->iSize ){
82071    rc = SQLITE_IOERR_SHORT_READ;
82072  }else{
82073    memcpy(zBuf, &p->zBuf[iOfst], iAmt);
82074  }
82075  return rc;
82076}
82077
82078/*
82079** Write data to the file.
82080*/
82081static int jrnlWrite(
82082  sqlite3_file *pJfd,    /* The journal file into which to write */
82083  const void *zBuf,      /* Take data to be written from here */
82084  int iAmt,              /* Number of bytes to write */
82085  sqlite_int64 iOfst     /* Begin writing at this offset into the file */
82086){
82087  int rc = SQLITE_OK;
82088  JournalFile *p = (JournalFile *)pJfd;
82089  if( !p->pReal && (iOfst+iAmt)>p->nBuf ){
82090    rc = createFile(p);
82091  }
82092  if( rc==SQLITE_OK ){
82093    if( p->pReal ){
82094      rc = sqlite3OsWrite(p->pReal, zBuf, iAmt, iOfst);
82095    }else{
82096      memcpy(&p->zBuf[iOfst], zBuf, iAmt);
82097      if( p->iSize<(iOfst+iAmt) ){
82098        p->iSize = (iOfst+iAmt);
82099      }
82100    }
82101  }
82102  return rc;
82103}
82104
82105/*
82106** Truncate the file.
82107*/
82108static int jrnlTruncate(sqlite3_file *pJfd, sqlite_int64 size){
82109  int rc = SQLITE_OK;
82110  JournalFile *p = (JournalFile *)pJfd;
82111  if( p->pReal ){
82112    rc = sqlite3OsTruncate(p->pReal, size);
82113  }else if( size<p->iSize ){
82114    p->iSize = size;
82115  }
82116  return rc;
82117}
82118
82119/*
82120** Sync the file.
82121*/
82122static int jrnlSync(sqlite3_file *pJfd, int flags){
82123  int rc;
82124  JournalFile *p = (JournalFile *)pJfd;
82125  if( p->pReal ){
82126    rc = sqlite3OsSync(p->pReal, flags);
82127  }else{
82128    rc = SQLITE_OK;
82129  }
82130  return rc;
82131}
82132
82133/*
82134** Query the size of the file in bytes.
82135*/
82136static int jrnlFileSize(sqlite3_file *pJfd, sqlite_int64 *pSize){
82137  int rc = SQLITE_OK;
82138  JournalFile *p = (JournalFile *)pJfd;
82139  if( p->pReal ){
82140    rc = sqlite3OsFileSize(p->pReal, pSize);
82141  }else{
82142    *pSize = (sqlite_int64) p->iSize;
82143  }
82144  return rc;
82145}
82146
82147/*
82148** Table of methods for JournalFile sqlite3_file object.
82149*/
82150static struct sqlite3_io_methods JournalFileMethods = {
82151  1,             /* iVersion */
82152  jrnlClose,     /* xClose */
82153  jrnlRead,      /* xRead */
82154  jrnlWrite,     /* xWrite */
82155  jrnlTruncate,  /* xTruncate */
82156  jrnlSync,      /* xSync */
82157  jrnlFileSize,  /* xFileSize */
82158  0,             /* xLock */
82159  0,             /* xUnlock */
82160  0,             /* xCheckReservedLock */
82161  0,             /* xFileControl */
82162  0,             /* xSectorSize */
82163  0,             /* xDeviceCharacteristics */
82164  0,             /* xShmMap */
82165  0,             /* xShmLock */
82166  0,             /* xShmBarrier */
82167  0              /* xShmUnmap */
82168};
82169
82170/*
82171** Open a journal file.
82172*/
82173SQLITE_PRIVATE int sqlite3JournalOpen(
82174  sqlite3_vfs *pVfs,         /* The VFS to use for actual file I/O */
82175  const char *zName,         /* Name of the journal file */
82176  sqlite3_file *pJfd,        /* Preallocated, blank file handle */
82177  int flags,                 /* Opening flags */
82178  int nBuf                   /* Bytes buffered before opening the file */
82179){
82180  JournalFile *p = (JournalFile *)pJfd;
82181  memset(p, 0, sqlite3JournalSize(pVfs));
82182  if( nBuf>0 ){
82183    p->zBuf = sqlite3MallocZero(nBuf);
82184    if( !p->zBuf ){
82185      return SQLITE_NOMEM;
82186    }
82187  }else{
82188    return sqlite3OsOpen(pVfs, zName, pJfd, flags, 0);
82189  }
82190  p->pMethod = &JournalFileMethods;
82191  p->nBuf = nBuf;
82192  p->flags = flags;
82193  p->zJournal = zName;
82194  p->pVfs = pVfs;
82195  return SQLITE_OK;
82196}
82197
82198/*
82199** If the argument p points to a JournalFile structure, and the underlying
82200** file has not yet been created, create it now.
82201*/
82202SQLITE_PRIVATE int sqlite3JournalCreate(sqlite3_file *p){
82203  if( p->pMethods!=&JournalFileMethods ){
82204    return SQLITE_OK;
82205  }
82206  return createFile((JournalFile *)p);
82207}
82208
82209/*
82210** The file-handle passed as the only argument is guaranteed to be an open
82211** file. It may or may not be of class JournalFile. If the file is a
82212** JournalFile, and the underlying file on disk has not yet been opened,
82213** return 0. Otherwise, return 1.
82214*/
82215SQLITE_PRIVATE int sqlite3JournalExists(sqlite3_file *p){
82216  return (p->pMethods!=&JournalFileMethods || ((JournalFile *)p)->pReal!=0);
82217}
82218
82219/*
82220** Return the number of bytes required to store a JournalFile that uses vfs
82221** pVfs to create the underlying on-disk files.
82222*/
82223SQLITE_PRIVATE int sqlite3JournalSize(sqlite3_vfs *pVfs){
82224  return (pVfs->szOsFile+sizeof(JournalFile));
82225}
82226#endif
82227
82228/************** End of journal.c *********************************************/
82229/************** Begin file memjournal.c **************************************/
82230/*
82231** 2008 October 7
82232**
82233** The author disclaims copyright to this source code.  In place of
82234** a legal notice, here is a blessing:
82235**
82236**    May you do good and not evil.
82237**    May you find forgiveness for yourself and forgive others.
82238**    May you share freely, never taking more than you give.
82239**
82240*************************************************************************
82241**
82242** This file contains code use to implement an in-memory rollback journal.
82243** The in-memory rollback journal is used to journal transactions for
82244** ":memory:" databases and when the journal_mode=MEMORY pragma is used.
82245*/
82246/* #include "sqliteInt.h" */
82247
82248/* Forward references to internal structures */
82249typedef struct MemJournal MemJournal;
82250typedef struct FilePoint FilePoint;
82251typedef struct FileChunk FileChunk;
82252
82253/* Space to hold the rollback journal is allocated in increments of
82254** this many bytes.
82255**
82256** The size chosen is a little less than a power of two.  That way,
82257** the FileChunk object will have a size that almost exactly fills
82258** a power-of-two allocation.  This minimizes wasted space in power-of-two
82259** memory allocators.
82260*/
82261#define JOURNAL_CHUNKSIZE ((int)(1024-sizeof(FileChunk*)))
82262
82263/*
82264** The rollback journal is composed of a linked list of these structures.
82265*/
82266struct FileChunk {
82267  FileChunk *pNext;               /* Next chunk in the journal */
82268  u8 zChunk[JOURNAL_CHUNKSIZE];   /* Content of this chunk */
82269};
82270
82271/*
82272** An instance of this object serves as a cursor into the rollback journal.
82273** The cursor can be either for reading or writing.
82274*/
82275struct FilePoint {
82276  sqlite3_int64 iOffset;          /* Offset from the beginning of the file */
82277  FileChunk *pChunk;              /* Specific chunk into which cursor points */
82278};
82279
82280/*
82281** This subclass is a subclass of sqlite3_file.  Each open memory-journal
82282** is an instance of this class.
82283*/
82284struct MemJournal {
82285  sqlite3_io_methods *pMethod;    /* Parent class. MUST BE FIRST */
82286  FileChunk *pFirst;              /* Head of in-memory chunk-list */
82287  FilePoint endpoint;             /* Pointer to the end of the file */
82288  FilePoint readpoint;            /* Pointer to the end of the last xRead() */
82289};
82290
82291/*
82292** Read data from the in-memory journal file.  This is the implementation
82293** of the sqlite3_vfs.xRead method.
82294*/
82295static int memjrnlRead(
82296  sqlite3_file *pJfd,    /* The journal file from which to read */
82297  void *zBuf,            /* Put the results here */
82298  int iAmt,              /* Number of bytes to read */
82299  sqlite_int64 iOfst     /* Begin reading at this offset */
82300){
82301  MemJournal *p = (MemJournal *)pJfd;
82302  u8 *zOut = zBuf;
82303  int nRead = iAmt;
82304  int iChunkOffset;
82305  FileChunk *pChunk;
82306
82307  /* SQLite never tries to read past the end of a rollback journal file */
82308  assert( iOfst+iAmt<=p->endpoint.iOffset );
82309
82310  if( p->readpoint.iOffset!=iOfst || iOfst==0 ){
82311    sqlite3_int64 iOff = 0;
82312    for(pChunk=p->pFirst;
82313        ALWAYS(pChunk) && (iOff+JOURNAL_CHUNKSIZE)<=iOfst;
82314        pChunk=pChunk->pNext
82315    ){
82316      iOff += JOURNAL_CHUNKSIZE;
82317    }
82318  }else{
82319    pChunk = p->readpoint.pChunk;
82320  }
82321
82322  iChunkOffset = (int)(iOfst%JOURNAL_CHUNKSIZE);
82323  do {
82324    int iSpace = JOURNAL_CHUNKSIZE - iChunkOffset;
82325    int nCopy = MIN(nRead, (JOURNAL_CHUNKSIZE - iChunkOffset));
82326    memcpy(zOut, &pChunk->zChunk[iChunkOffset], nCopy);
82327    zOut += nCopy;
82328    nRead -= iSpace;
82329    iChunkOffset = 0;
82330  } while( nRead>=0 && (pChunk=pChunk->pNext)!=0 && nRead>0 );
82331  p->readpoint.iOffset = iOfst+iAmt;
82332  p->readpoint.pChunk = pChunk;
82333
82334  return SQLITE_OK;
82335}
82336
82337/*
82338** Write data to the file.
82339*/
82340static int memjrnlWrite(
82341  sqlite3_file *pJfd,    /* The journal file into which to write */
82342  const void *zBuf,      /* Take data to be written from here */
82343  int iAmt,              /* Number of bytes to write */
82344  sqlite_int64 iOfst     /* Begin writing at this offset into the file */
82345){
82346  MemJournal *p = (MemJournal *)pJfd;
82347  int nWrite = iAmt;
82348  u8 *zWrite = (u8 *)zBuf;
82349
82350  /* An in-memory journal file should only ever be appended to. Random
82351  ** access writes are not required by sqlite.
82352  */
82353  assert( iOfst==p->endpoint.iOffset );
82354  UNUSED_PARAMETER(iOfst);
82355
82356  while( nWrite>0 ){
82357    FileChunk *pChunk = p->endpoint.pChunk;
82358    int iChunkOffset = (int)(p->endpoint.iOffset%JOURNAL_CHUNKSIZE);
82359    int iSpace = MIN(nWrite, JOURNAL_CHUNKSIZE - iChunkOffset);
82360
82361    if( iChunkOffset==0 ){
82362      /* New chunk is required to extend the file. */
82363      FileChunk *pNew = sqlite3_malloc(sizeof(FileChunk));
82364      if( !pNew ){
82365        return SQLITE_IOERR_NOMEM;
82366      }
82367      pNew->pNext = 0;
82368      if( pChunk ){
82369        assert( p->pFirst );
82370        pChunk->pNext = pNew;
82371      }else{
82372        assert( !p->pFirst );
82373        p->pFirst = pNew;
82374      }
82375      p->endpoint.pChunk = pNew;
82376    }
82377
82378    memcpy(&p->endpoint.pChunk->zChunk[iChunkOffset], zWrite, iSpace);
82379    zWrite += iSpace;
82380    nWrite -= iSpace;
82381    p->endpoint.iOffset += iSpace;
82382  }
82383
82384  return SQLITE_OK;
82385}
82386
82387/*
82388** Truncate the file.
82389*/
82390static int memjrnlTruncate(sqlite3_file *pJfd, sqlite_int64 size){
82391  MemJournal *p = (MemJournal *)pJfd;
82392  FileChunk *pChunk;
82393  assert(size==0);
82394  UNUSED_PARAMETER(size);
82395  pChunk = p->pFirst;
82396  while( pChunk ){
82397    FileChunk *pTmp = pChunk;
82398    pChunk = pChunk->pNext;
82399    sqlite3_free(pTmp);
82400  }
82401  sqlite3MemJournalOpen(pJfd);
82402  return SQLITE_OK;
82403}
82404
82405/*
82406** Close the file.
82407*/
82408static int memjrnlClose(sqlite3_file *pJfd){
82409  memjrnlTruncate(pJfd, 0);
82410  return SQLITE_OK;
82411}
82412
82413
82414/*
82415** Sync the file.
82416**
82417** Syncing an in-memory journal is a no-op.  And, in fact, this routine
82418** is never called in a working implementation.  This implementation
82419** exists purely as a contingency, in case some malfunction in some other
82420** part of SQLite causes Sync to be called by mistake.
82421*/
82422static int memjrnlSync(sqlite3_file *NotUsed, int NotUsed2){
82423  UNUSED_PARAMETER2(NotUsed, NotUsed2);
82424  return SQLITE_OK;
82425}
82426
82427/*
82428** Query the size of the file in bytes.
82429*/
82430static int memjrnlFileSize(sqlite3_file *pJfd, sqlite_int64 *pSize){
82431  MemJournal *p = (MemJournal *)pJfd;
82432  *pSize = (sqlite_int64) p->endpoint.iOffset;
82433  return SQLITE_OK;
82434}
82435
82436/*
82437** Table of methods for MemJournal sqlite3_file object.
82438*/
82439static const struct sqlite3_io_methods MemJournalMethods = {
82440  1,                /* iVersion */
82441  memjrnlClose,     /* xClose */
82442  memjrnlRead,      /* xRead */
82443  memjrnlWrite,     /* xWrite */
82444  memjrnlTruncate,  /* xTruncate */
82445  memjrnlSync,      /* xSync */
82446  memjrnlFileSize,  /* xFileSize */
82447  0,                /* xLock */
82448  0,                /* xUnlock */
82449  0,                /* xCheckReservedLock */
82450  0,                /* xFileControl */
82451  0,                /* xSectorSize */
82452  0,                /* xDeviceCharacteristics */
82453  0,                /* xShmMap */
82454  0,                /* xShmLock */
82455  0,                /* xShmBarrier */
82456  0,                /* xShmUnmap */
82457  0,                /* xFetch */
82458  0                 /* xUnfetch */
82459};
82460
82461/*
82462** Open a journal file.
82463*/
82464SQLITE_PRIVATE void sqlite3MemJournalOpen(sqlite3_file *pJfd){
82465  MemJournal *p = (MemJournal *)pJfd;
82466  assert( EIGHT_BYTE_ALIGNMENT(p) );
82467  memset(p, 0, sqlite3MemJournalSize());
82468  p->pMethod = (sqlite3_io_methods*)&MemJournalMethods;
82469}
82470
82471/*
82472** Return true if the file-handle passed as an argument is
82473** an in-memory journal
82474*/
82475SQLITE_PRIVATE int sqlite3IsMemJournal(sqlite3_file *pJfd){
82476  return pJfd->pMethods==&MemJournalMethods;
82477}
82478
82479/*
82480** Return the number of bytes required to store a MemJournal file descriptor.
82481*/
82482SQLITE_PRIVATE int sqlite3MemJournalSize(void){
82483  return sizeof(MemJournal);
82484}
82485
82486/************** End of memjournal.c ******************************************/
82487/************** Begin file walker.c ******************************************/
82488/*
82489** 2008 August 16
82490**
82491** The author disclaims copyright to this source code.  In place of
82492** a legal notice, here is a blessing:
82493**
82494**    May you do good and not evil.
82495**    May you find forgiveness for yourself and forgive others.
82496**    May you share freely, never taking more than you give.
82497**
82498*************************************************************************
82499** This file contains routines used for walking the parser tree for
82500** an SQL statement.
82501*/
82502/* #include "sqliteInt.h" */
82503/* #include <stdlib.h> */
82504/* #include <string.h> */
82505
82506
82507/*
82508** Walk an expression tree.  Invoke the callback once for each node
82509** of the expression, while descending.  (In other words, the callback
82510** is invoked before visiting children.)
82511**
82512** The return value from the callback should be one of the WRC_*
82513** constants to specify how to proceed with the walk.
82514**
82515**    WRC_Continue      Continue descending down the tree.
82516**
82517**    WRC_Prune         Do not descend into child nodes.  But allow
82518**                      the walk to continue with sibling nodes.
82519**
82520**    WRC_Abort         Do no more callbacks.  Unwind the stack and
82521**                      return the top-level walk call.
82522**
82523** The return value from this routine is WRC_Abort to abandon the tree walk
82524** and WRC_Continue to continue.
82525*/
82526SQLITE_PRIVATE int sqlite3WalkExpr(Walker *pWalker, Expr *pExpr){
82527  int rc;
82528  if( pExpr==0 ) return WRC_Continue;
82529  testcase( ExprHasProperty(pExpr, EP_TokenOnly) );
82530  testcase( ExprHasProperty(pExpr, EP_Reduced) );
82531  rc = pWalker->xExprCallback(pWalker, pExpr);
82532  if( rc==WRC_Continue
82533              && !ExprHasProperty(pExpr,EP_TokenOnly) ){
82534    if( sqlite3WalkExpr(pWalker, pExpr->pLeft) ) return WRC_Abort;
82535    if( sqlite3WalkExpr(pWalker, pExpr->pRight) ) return WRC_Abort;
82536    if( ExprHasProperty(pExpr, EP_xIsSelect) ){
82537      if( sqlite3WalkSelect(pWalker, pExpr->x.pSelect) ) return WRC_Abort;
82538    }else{
82539      if( sqlite3WalkExprList(pWalker, pExpr->x.pList) ) return WRC_Abort;
82540    }
82541  }
82542  return rc & WRC_Abort;
82543}
82544
82545/*
82546** Call sqlite3WalkExpr() for every expression in list p or until
82547** an abort request is seen.
82548*/
82549SQLITE_PRIVATE int sqlite3WalkExprList(Walker *pWalker, ExprList *p){
82550  int i;
82551  struct ExprList_item *pItem;
82552  if( p ){
82553    for(i=p->nExpr, pItem=p->a; i>0; i--, pItem++){
82554      if( sqlite3WalkExpr(pWalker, pItem->pExpr) ) return WRC_Abort;
82555    }
82556  }
82557  return WRC_Continue;
82558}
82559
82560/*
82561** Walk all expressions associated with SELECT statement p.  Do
82562** not invoke the SELECT callback on p, but do (of course) invoke
82563** any expr callbacks and SELECT callbacks that come from subqueries.
82564** Return WRC_Abort or WRC_Continue.
82565*/
82566SQLITE_PRIVATE int sqlite3WalkSelectExpr(Walker *pWalker, Select *p){
82567  if( sqlite3WalkExprList(pWalker, p->pEList) ) return WRC_Abort;
82568  if( sqlite3WalkExpr(pWalker, p->pWhere) ) return WRC_Abort;
82569  if( sqlite3WalkExprList(pWalker, p->pGroupBy) ) return WRC_Abort;
82570  if( sqlite3WalkExpr(pWalker, p->pHaving) ) return WRC_Abort;
82571  if( sqlite3WalkExprList(pWalker, p->pOrderBy) ) return WRC_Abort;
82572  if( sqlite3WalkExpr(pWalker, p->pLimit) ) return WRC_Abort;
82573  if( sqlite3WalkExpr(pWalker, p->pOffset) ) return WRC_Abort;
82574  return WRC_Continue;
82575}
82576
82577/*
82578** Walk the parse trees associated with all subqueries in the
82579** FROM clause of SELECT statement p.  Do not invoke the select
82580** callback on p, but do invoke it on each FROM clause subquery
82581** and on any subqueries further down in the tree.  Return
82582** WRC_Abort or WRC_Continue;
82583*/
82584SQLITE_PRIVATE int sqlite3WalkSelectFrom(Walker *pWalker, Select *p){
82585  SrcList *pSrc;
82586  int i;
82587  struct SrcList_item *pItem;
82588
82589  pSrc = p->pSrc;
82590  if( ALWAYS(pSrc) ){
82591    for(i=pSrc->nSrc, pItem=pSrc->a; i>0; i--, pItem++){
82592      if( sqlite3WalkSelect(pWalker, pItem->pSelect) ){
82593        return WRC_Abort;
82594      }
82595    }
82596  }
82597  return WRC_Continue;
82598}
82599
82600/*
82601** Call sqlite3WalkExpr() for every expression in Select statement p.
82602** Invoke sqlite3WalkSelect() for subqueries in the FROM clause and
82603** on the compound select chain, p->pPrior.
82604**
82605** If it is not NULL, the xSelectCallback() callback is invoked before
82606** the walk of the expressions and FROM clause. The xSelectCallback2()
82607** method, if it is not NULL, is invoked following the walk of the
82608** expressions and FROM clause.
82609**
82610** Return WRC_Continue under normal conditions.  Return WRC_Abort if
82611** there is an abort request.
82612**
82613** If the Walker does not have an xSelectCallback() then this routine
82614** is a no-op returning WRC_Continue.
82615*/
82616SQLITE_PRIVATE int sqlite3WalkSelect(Walker *pWalker, Select *p){
82617  int rc;
82618  if( p==0 || (pWalker->xSelectCallback==0 && pWalker->xSelectCallback2==0) ){
82619    return WRC_Continue;
82620  }
82621  rc = WRC_Continue;
82622  pWalker->walkerDepth++;
82623  while( p ){
82624    if( pWalker->xSelectCallback ){
82625       rc = pWalker->xSelectCallback(pWalker, p);
82626       if( rc ) break;
82627    }
82628    if( sqlite3WalkSelectExpr(pWalker, p)
82629     || sqlite3WalkSelectFrom(pWalker, p)
82630    ){
82631      pWalker->walkerDepth--;
82632      return WRC_Abort;
82633    }
82634    if( pWalker->xSelectCallback2 ){
82635      pWalker->xSelectCallback2(pWalker, p);
82636    }
82637    p = p->pPrior;
82638  }
82639  pWalker->walkerDepth--;
82640  return rc & WRC_Abort;
82641}
82642
82643/************** End of walker.c **********************************************/
82644/************** Begin file resolve.c *****************************************/
82645/*
82646** 2008 August 18
82647**
82648** The author disclaims copyright to this source code.  In place of
82649** a legal notice, here is a blessing:
82650**
82651**    May you do good and not evil.
82652**    May you find forgiveness for yourself and forgive others.
82653**    May you share freely, never taking more than you give.
82654**
82655*************************************************************************
82656**
82657** This file contains routines used for walking the parser tree and
82658** resolve all identifiers by associating them with a particular
82659** table and column.
82660*/
82661/* #include "sqliteInt.h" */
82662/* #include <stdlib.h> */
82663/* #include <string.h> */
82664
82665/*
82666** Walk the expression tree pExpr and increase the aggregate function
82667** depth (the Expr.op2 field) by N on every TK_AGG_FUNCTION node.
82668** This needs to occur when copying a TK_AGG_FUNCTION node from an
82669** outer query into an inner subquery.
82670**
82671** incrAggFunctionDepth(pExpr,n) is the main routine.  incrAggDepth(..)
82672** is a helper function - a callback for the tree walker.
82673*/
82674static int incrAggDepth(Walker *pWalker, Expr *pExpr){
82675  if( pExpr->op==TK_AGG_FUNCTION ) pExpr->op2 += pWalker->u.n;
82676  return WRC_Continue;
82677}
82678static void incrAggFunctionDepth(Expr *pExpr, int N){
82679  if( N>0 ){
82680    Walker w;
82681    memset(&w, 0, sizeof(w));
82682    w.xExprCallback = incrAggDepth;
82683    w.u.n = N;
82684    sqlite3WalkExpr(&w, pExpr);
82685  }
82686}
82687
82688/*
82689** Turn the pExpr expression into an alias for the iCol-th column of the
82690** result set in pEList.
82691**
82692** If the result set column is a simple column reference, then this routine
82693** makes an exact copy.  But for any other kind of expression, this
82694** routine make a copy of the result set column as the argument to the
82695** TK_AS operator.  The TK_AS operator causes the expression to be
82696** evaluated just once and then reused for each alias.
82697**
82698** The reason for suppressing the TK_AS term when the expression is a simple
82699** column reference is so that the column reference will be recognized as
82700** usable by indices within the WHERE clause processing logic.
82701**
82702** The TK_AS operator is inhibited if zType[0]=='G'.  This means
82703** that in a GROUP BY clause, the expression is evaluated twice.  Hence:
82704**
82705**     SELECT random()%5 AS x, count(*) FROM tab GROUP BY x
82706**
82707** Is equivalent to:
82708**
82709**     SELECT random()%5 AS x, count(*) FROM tab GROUP BY random()%5
82710**
82711** The result of random()%5 in the GROUP BY clause is probably different
82712** from the result in the result-set.  On the other hand Standard SQL does
82713** not allow the GROUP BY clause to contain references to result-set columns.
82714** So this should never come up in well-formed queries.
82715**
82716** If the reference is followed by a COLLATE operator, then make sure
82717** the COLLATE operator is preserved.  For example:
82718**
82719**     SELECT a+b, c+d FROM t1 ORDER BY 1 COLLATE nocase;
82720**
82721** Should be transformed into:
82722**
82723**     SELECT a+b, c+d FROM t1 ORDER BY (a+b) COLLATE nocase;
82724**
82725** The nSubquery parameter specifies how many levels of subquery the
82726** alias is removed from the original expression.  The usual value is
82727** zero but it might be more if the alias is contained within a subquery
82728** of the original expression.  The Expr.op2 field of TK_AGG_FUNCTION
82729** structures must be increased by the nSubquery amount.
82730*/
82731static void resolveAlias(
82732  Parse *pParse,         /* Parsing context */
82733  ExprList *pEList,      /* A result set */
82734  int iCol,              /* A column in the result set.  0..pEList->nExpr-1 */
82735  Expr *pExpr,           /* Transform this into an alias to the result set */
82736  const char *zType,     /* "GROUP" or "ORDER" or "" */
82737  int nSubquery          /* Number of subqueries that the label is moving */
82738){
82739  Expr *pOrig;           /* The iCol-th column of the result set */
82740  Expr *pDup;            /* Copy of pOrig */
82741  sqlite3 *db;           /* The database connection */
82742
82743  assert( iCol>=0 && iCol<pEList->nExpr );
82744  pOrig = pEList->a[iCol].pExpr;
82745  assert( pOrig!=0 );
82746  db = pParse->db;
82747  pDup = sqlite3ExprDup(db, pOrig, 0);
82748  if( pDup==0 ) return;
82749  if( pOrig->op!=TK_COLUMN && zType[0]!='G' ){
82750    incrAggFunctionDepth(pDup, nSubquery);
82751    pDup = sqlite3PExpr(pParse, TK_AS, pDup, 0, 0);
82752    if( pDup==0 ) return;
82753    ExprSetProperty(pDup, EP_Skip);
82754    if( pEList->a[iCol].u.x.iAlias==0 ){
82755      pEList->a[iCol].u.x.iAlias = (u16)(++pParse->nAlias);
82756    }
82757    pDup->iTable = pEList->a[iCol].u.x.iAlias;
82758  }
82759  if( pExpr->op==TK_COLLATE ){
82760    pDup = sqlite3ExprAddCollateString(pParse, pDup, pExpr->u.zToken);
82761  }
82762
82763  /* Before calling sqlite3ExprDelete(), set the EP_Static flag. This
82764  ** prevents ExprDelete() from deleting the Expr structure itself,
82765  ** allowing it to be repopulated by the memcpy() on the following line.
82766  ** The pExpr->u.zToken might point into memory that will be freed by the
82767  ** sqlite3DbFree(db, pDup) on the last line of this block, so be sure to
82768  ** make a copy of the token before doing the sqlite3DbFree().
82769  */
82770  ExprSetProperty(pExpr, EP_Static);
82771  sqlite3ExprDelete(db, pExpr);
82772  memcpy(pExpr, pDup, sizeof(*pExpr));
82773  if( !ExprHasProperty(pExpr, EP_IntValue) && pExpr->u.zToken!=0 ){
82774    assert( (pExpr->flags & (EP_Reduced|EP_TokenOnly))==0 );
82775    pExpr->u.zToken = sqlite3DbStrDup(db, pExpr->u.zToken);
82776    pExpr->flags |= EP_MemToken;
82777  }
82778  sqlite3DbFree(db, pDup);
82779}
82780
82781
82782/*
82783** Return TRUE if the name zCol occurs anywhere in the USING clause.
82784**
82785** Return FALSE if the USING clause is NULL or if it does not contain
82786** zCol.
82787*/
82788static int nameInUsingClause(IdList *pUsing, const char *zCol){
82789  if( pUsing ){
82790    int k;
82791    for(k=0; k<pUsing->nId; k++){
82792      if( sqlite3StrICmp(pUsing->a[k].zName, zCol)==0 ) return 1;
82793    }
82794  }
82795  return 0;
82796}
82797
82798/*
82799** Subqueries stores the original database, table and column names for their
82800** result sets in ExprList.a[].zSpan, in the form "DATABASE.TABLE.COLUMN".
82801** Check to see if the zSpan given to this routine matches the zDb, zTab,
82802** and zCol.  If any of zDb, zTab, and zCol are NULL then those fields will
82803** match anything.
82804*/
82805SQLITE_PRIVATE int sqlite3MatchSpanName(
82806  const char *zSpan,
82807  const char *zCol,
82808  const char *zTab,
82809  const char *zDb
82810){
82811  int n;
82812  for(n=0; ALWAYS(zSpan[n]) && zSpan[n]!='.'; n++){}
82813  if( zDb && (sqlite3StrNICmp(zSpan, zDb, n)!=0 || zDb[n]!=0) ){
82814    return 0;
82815  }
82816  zSpan += n+1;
82817  for(n=0; ALWAYS(zSpan[n]) && zSpan[n]!='.'; n++){}
82818  if( zTab && (sqlite3StrNICmp(zSpan, zTab, n)!=0 || zTab[n]!=0) ){
82819    return 0;
82820  }
82821  zSpan += n+1;
82822  if( zCol && sqlite3StrICmp(zSpan, zCol)!=0 ){
82823    return 0;
82824  }
82825  return 1;
82826}
82827
82828/*
82829** Given the name of a column of the form X.Y.Z or Y.Z or just Z, look up
82830** that name in the set of source tables in pSrcList and make the pExpr
82831** expression node refer back to that source column.  The following changes
82832** are made to pExpr:
82833**
82834**    pExpr->iDb           Set the index in db->aDb[] of the database X
82835**                         (even if X is implied).
82836**    pExpr->iTable        Set to the cursor number for the table obtained
82837**                         from pSrcList.
82838**    pExpr->pTab          Points to the Table structure of X.Y (even if
82839**                         X and/or Y are implied.)
82840**    pExpr->iColumn       Set to the column number within the table.
82841**    pExpr->op            Set to TK_COLUMN.
82842**    pExpr->pLeft         Any expression this points to is deleted
82843**    pExpr->pRight        Any expression this points to is deleted.
82844**
82845** The zDb variable is the name of the database (the "X").  This value may be
82846** NULL meaning that name is of the form Y.Z or Z.  Any available database
82847** can be used.  The zTable variable is the name of the table (the "Y").  This
82848** value can be NULL if zDb is also NULL.  If zTable is NULL it
82849** means that the form of the name is Z and that columns from any table
82850** can be used.
82851**
82852** If the name cannot be resolved unambiguously, leave an error message
82853** in pParse and return WRC_Abort.  Return WRC_Prune on success.
82854*/
82855static int lookupName(
82856  Parse *pParse,       /* The parsing context */
82857  const char *zDb,     /* Name of the database containing table, or NULL */
82858  const char *zTab,    /* Name of table containing column, or NULL */
82859  const char *zCol,    /* Name of the column. */
82860  NameContext *pNC,    /* The name context used to resolve the name */
82861  Expr *pExpr          /* Make this EXPR node point to the selected column */
82862){
82863  int i, j;                         /* Loop counters */
82864  int cnt = 0;                      /* Number of matching column names */
82865  int cntTab = 0;                   /* Number of matching table names */
82866  int nSubquery = 0;                /* How many levels of subquery */
82867  sqlite3 *db = pParse->db;         /* The database connection */
82868  struct SrcList_item *pItem;       /* Use for looping over pSrcList items */
82869  struct SrcList_item *pMatch = 0;  /* The matching pSrcList item */
82870  NameContext *pTopNC = pNC;        /* First namecontext in the list */
82871  Schema *pSchema = 0;              /* Schema of the expression */
82872  int isTrigger = 0;                /* True if resolved to a trigger column */
82873  Table *pTab = 0;                  /* Table hold the row */
82874  Column *pCol;                     /* A column of pTab */
82875
82876  assert( pNC );     /* the name context cannot be NULL. */
82877  assert( zCol );    /* The Z in X.Y.Z cannot be NULL */
82878  assert( !ExprHasProperty(pExpr, EP_TokenOnly|EP_Reduced) );
82879
82880  /* Initialize the node to no-match */
82881  pExpr->iTable = -1;
82882  pExpr->pTab = 0;
82883  ExprSetVVAProperty(pExpr, EP_NoReduce);
82884
82885  /* Translate the schema name in zDb into a pointer to the corresponding
82886  ** schema.  If not found, pSchema will remain NULL and nothing will match
82887  ** resulting in an appropriate error message toward the end of this routine
82888  */
82889  if( zDb ){
82890    testcase( pNC->ncFlags & NC_PartIdx );
82891    testcase( pNC->ncFlags & NC_IsCheck );
82892    if( (pNC->ncFlags & (NC_PartIdx|NC_IsCheck))!=0 ){
82893      /* Silently ignore database qualifiers inside CHECK constraints and
82894      ** partial indices.  Do not raise errors because that might break
82895      ** legacy and because it does not hurt anything to just ignore the
82896      ** database name. */
82897      zDb = 0;
82898    }else{
82899      for(i=0; i<db->nDb; i++){
82900        assert( db->aDb[i].zName );
82901        if( sqlite3StrICmp(db->aDb[i].zName,zDb)==0 ){
82902          pSchema = db->aDb[i].pSchema;
82903          break;
82904        }
82905      }
82906    }
82907  }
82908
82909  /* Start at the inner-most context and move outward until a match is found */
82910  while( pNC && cnt==0 ){
82911    ExprList *pEList;
82912    SrcList *pSrcList = pNC->pSrcList;
82913
82914    if( pSrcList ){
82915      for(i=0, pItem=pSrcList->a; i<pSrcList->nSrc; i++, pItem++){
82916        pTab = pItem->pTab;
82917        assert( pTab!=0 && pTab->zName!=0 );
82918        assert( pTab->nCol>0 );
82919        if( pItem->pSelect && (pItem->pSelect->selFlags & SF_NestedFrom)!=0 ){
82920          int hit = 0;
82921          pEList = pItem->pSelect->pEList;
82922          for(j=0; j<pEList->nExpr; j++){
82923            if( sqlite3MatchSpanName(pEList->a[j].zSpan, zCol, zTab, zDb) ){
82924              cnt++;
82925              cntTab = 2;
82926              pMatch = pItem;
82927              pExpr->iColumn = j;
82928              hit = 1;
82929            }
82930          }
82931          if( hit || zTab==0 ) continue;
82932        }
82933        if( zDb && pTab->pSchema!=pSchema ){
82934          continue;
82935        }
82936        if( zTab ){
82937          const char *zTabName = pItem->zAlias ? pItem->zAlias : pTab->zName;
82938          assert( zTabName!=0 );
82939          if( sqlite3StrICmp(zTabName, zTab)!=0 ){
82940            continue;
82941          }
82942        }
82943        if( 0==(cntTab++) ){
82944          pMatch = pItem;
82945        }
82946        for(j=0, pCol=pTab->aCol; j<pTab->nCol; j++, pCol++){
82947          if( sqlite3StrICmp(pCol->zName, zCol)==0 ){
82948            /* If there has been exactly one prior match and this match
82949            ** is for the right-hand table of a NATURAL JOIN or is in a
82950            ** USING clause, then skip this match.
82951            */
82952            if( cnt==1 ){
82953              if( pItem->jointype & JT_NATURAL ) continue;
82954              if( nameInUsingClause(pItem->pUsing, zCol) ) continue;
82955            }
82956            cnt++;
82957            pMatch = pItem;
82958            /* Substitute the rowid (column -1) for the INTEGER PRIMARY KEY */
82959            pExpr->iColumn = j==pTab->iPKey ? -1 : (i16)j;
82960            break;
82961          }
82962        }
82963      }
82964      if( pMatch ){
82965        pExpr->iTable = pMatch->iCursor;
82966        pExpr->pTab = pMatch->pTab;
82967        /* RIGHT JOIN not (yet) supported */
82968        assert( (pMatch->jointype & JT_RIGHT)==0 );
82969        if( (pMatch->jointype & JT_LEFT)!=0 ){
82970          ExprSetProperty(pExpr, EP_CanBeNull);
82971        }
82972        pSchema = pExpr->pTab->pSchema;
82973      }
82974    } /* if( pSrcList ) */
82975
82976#ifndef SQLITE_OMIT_TRIGGER
82977    /* If we have not already resolved the name, then maybe
82978    ** it is a new.* or old.* trigger argument reference
82979    */
82980    if( zDb==0 && zTab!=0 && cntTab==0 && pParse->pTriggerTab!=0 ){
82981      int op = pParse->eTriggerOp;
82982      assert( op==TK_DELETE || op==TK_UPDATE || op==TK_INSERT );
82983      if( op!=TK_DELETE && sqlite3StrICmp("new",zTab) == 0 ){
82984        pExpr->iTable = 1;
82985        pTab = pParse->pTriggerTab;
82986      }else if( op!=TK_INSERT && sqlite3StrICmp("old",zTab)==0 ){
82987        pExpr->iTable = 0;
82988        pTab = pParse->pTriggerTab;
82989      }else{
82990        pTab = 0;
82991      }
82992
82993      if( pTab ){
82994        int iCol;
82995        pSchema = pTab->pSchema;
82996        cntTab++;
82997        for(iCol=0, pCol=pTab->aCol; iCol<pTab->nCol; iCol++, pCol++){
82998          if( sqlite3StrICmp(pCol->zName, zCol)==0 ){
82999            if( iCol==pTab->iPKey ){
83000              iCol = -1;
83001            }
83002            break;
83003          }
83004        }
83005        if( iCol>=pTab->nCol && sqlite3IsRowid(zCol) && VisibleRowid(pTab) ){
83006          /* IMP: R-51414-32910 */
83007          /* IMP: R-44911-55124 */
83008          iCol = -1;
83009        }
83010        if( iCol<pTab->nCol ){
83011          cnt++;
83012          if( iCol<0 ){
83013            pExpr->affinity = SQLITE_AFF_INTEGER;
83014          }else if( pExpr->iTable==0 ){
83015            testcase( iCol==31 );
83016            testcase( iCol==32 );
83017            pParse->oldmask |= (iCol>=32 ? 0xffffffff : (((u32)1)<<iCol));
83018          }else{
83019            testcase( iCol==31 );
83020            testcase( iCol==32 );
83021            pParse->newmask |= (iCol>=32 ? 0xffffffff : (((u32)1)<<iCol));
83022          }
83023          pExpr->iColumn = (i16)iCol;
83024          pExpr->pTab = pTab;
83025          isTrigger = 1;
83026        }
83027      }
83028    }
83029#endif /* !defined(SQLITE_OMIT_TRIGGER) */
83030
83031    /*
83032    ** Perhaps the name is a reference to the ROWID
83033    */
83034    if( cnt==0 && cntTab==1 && pMatch && sqlite3IsRowid(zCol)
83035     && VisibleRowid(pMatch->pTab) ){
83036      cnt = 1;
83037      pExpr->iColumn = -1;     /* IMP: R-44911-55124 */
83038      pExpr->affinity = SQLITE_AFF_INTEGER;
83039    }
83040
83041    /*
83042    ** If the input is of the form Z (not Y.Z or X.Y.Z) then the name Z
83043    ** might refer to an result-set alias.  This happens, for example, when
83044    ** we are resolving names in the WHERE clause of the following command:
83045    **
83046    **     SELECT a+b AS x FROM table WHERE x<10;
83047    **
83048    ** In cases like this, replace pExpr with a copy of the expression that
83049    ** forms the result set entry ("a+b" in the example) and return immediately.
83050    ** Note that the expression in the result set should have already been
83051    ** resolved by the time the WHERE clause is resolved.
83052    **
83053    ** The ability to use an output result-set column in the WHERE, GROUP BY,
83054    ** or HAVING clauses, or as part of a larger expression in the ORDRE BY
83055    ** clause is not standard SQL.  This is a (goofy) SQLite extension, that
83056    ** is supported for backwards compatibility only.  TO DO: Issue a warning
83057    ** on sqlite3_log() whenever the capability is used.
83058    */
83059    if( (pEList = pNC->pEList)!=0
83060     && zTab==0
83061     && cnt==0
83062    ){
83063      for(j=0; j<pEList->nExpr; j++){
83064        char *zAs = pEList->a[j].zName;
83065        if( zAs!=0 && sqlite3StrICmp(zAs, zCol)==0 ){
83066          Expr *pOrig;
83067          assert( pExpr->pLeft==0 && pExpr->pRight==0 );
83068          assert( pExpr->x.pList==0 );
83069          assert( pExpr->x.pSelect==0 );
83070          pOrig = pEList->a[j].pExpr;
83071          if( (pNC->ncFlags&NC_AllowAgg)==0 && ExprHasProperty(pOrig, EP_Agg) ){
83072            sqlite3ErrorMsg(pParse, "misuse of aliased aggregate %s", zAs);
83073            return WRC_Abort;
83074          }
83075          resolveAlias(pParse, pEList, j, pExpr, "", nSubquery);
83076          cnt = 1;
83077          pMatch = 0;
83078          assert( zTab==0 && zDb==0 );
83079          goto lookupname_end;
83080        }
83081      }
83082    }
83083
83084    /* Advance to the next name context.  The loop will exit when either
83085    ** we have a match (cnt>0) or when we run out of name contexts.
83086    */
83087    if( cnt==0 ){
83088      pNC = pNC->pNext;
83089      nSubquery++;
83090    }
83091  }
83092
83093  /*
83094  ** If X and Y are NULL (in other words if only the column name Z is
83095  ** supplied) and the value of Z is enclosed in double-quotes, then
83096  ** Z is a string literal if it doesn't match any column names.  In that
83097  ** case, we need to return right away and not make any changes to
83098  ** pExpr.
83099  **
83100  ** Because no reference was made to outer contexts, the pNC->nRef
83101  ** fields are not changed in any context.
83102  */
83103  if( cnt==0 && zTab==0 && ExprHasProperty(pExpr,EP_DblQuoted) ){
83104    pExpr->op = TK_STRING;
83105    pExpr->pTab = 0;
83106    return WRC_Prune;
83107  }
83108
83109  /*
83110  ** cnt==0 means there was not match.  cnt>1 means there were two or
83111  ** more matches.  Either way, we have an error.
83112  */
83113  if( cnt!=1 ){
83114    const char *zErr;
83115    zErr = cnt==0 ? "no such column" : "ambiguous column name";
83116    if( zDb ){
83117      sqlite3ErrorMsg(pParse, "%s: %s.%s.%s", zErr, zDb, zTab, zCol);
83118    }else if( zTab ){
83119      sqlite3ErrorMsg(pParse, "%s: %s.%s", zErr, zTab, zCol);
83120    }else{
83121      sqlite3ErrorMsg(pParse, "%s: %s", zErr, zCol);
83122    }
83123    pParse->checkSchema = 1;
83124    pTopNC->nErr++;
83125  }
83126
83127  /* If a column from a table in pSrcList is referenced, then record
83128  ** this fact in the pSrcList.a[].colUsed bitmask.  Column 0 causes
83129  ** bit 0 to be set.  Column 1 sets bit 1.  And so forth.  If the
83130  ** column number is greater than the number of bits in the bitmask
83131  ** then set the high-order bit of the bitmask.
83132  */
83133  if( pExpr->iColumn>=0 && pMatch!=0 ){
83134    int n = pExpr->iColumn;
83135    testcase( n==BMS-1 );
83136    if( n>=BMS ){
83137      n = BMS-1;
83138    }
83139    assert( pMatch->iCursor==pExpr->iTable );
83140    pMatch->colUsed |= ((Bitmask)1)<<n;
83141  }
83142
83143  /* Clean up and return
83144  */
83145  sqlite3ExprDelete(db, pExpr->pLeft);
83146  pExpr->pLeft = 0;
83147  sqlite3ExprDelete(db, pExpr->pRight);
83148  pExpr->pRight = 0;
83149  pExpr->op = (isTrigger ? TK_TRIGGER : TK_COLUMN);
83150lookupname_end:
83151  if( cnt==1 ){
83152    assert( pNC!=0 );
83153    if( pExpr->op!=TK_AS ){
83154      sqlite3AuthRead(pParse, pExpr, pSchema, pNC->pSrcList);
83155    }
83156    /* Increment the nRef value on all name contexts from TopNC up to
83157    ** the point where the name matched. */
83158    for(;;){
83159      assert( pTopNC!=0 );
83160      pTopNC->nRef++;
83161      if( pTopNC==pNC ) break;
83162      pTopNC = pTopNC->pNext;
83163    }
83164    return WRC_Prune;
83165  } else {
83166    return WRC_Abort;
83167  }
83168}
83169
83170/*
83171** Allocate and return a pointer to an expression to load the column iCol
83172** from datasource iSrc in SrcList pSrc.
83173*/
83174SQLITE_PRIVATE Expr *sqlite3CreateColumnExpr(sqlite3 *db, SrcList *pSrc, int iSrc, int iCol){
83175  Expr *p = sqlite3ExprAlloc(db, TK_COLUMN, 0, 0);
83176  if( p ){
83177    struct SrcList_item *pItem = &pSrc->a[iSrc];
83178    p->pTab = pItem->pTab;
83179    p->iTable = pItem->iCursor;
83180    if( p->pTab->iPKey==iCol ){
83181      p->iColumn = -1;
83182    }else{
83183      p->iColumn = (ynVar)iCol;
83184      testcase( iCol==BMS );
83185      testcase( iCol==BMS-1 );
83186      pItem->colUsed |= ((Bitmask)1)<<(iCol>=BMS ? BMS-1 : iCol);
83187    }
83188    ExprSetProperty(p, EP_Resolved);
83189  }
83190  return p;
83191}
83192
83193/*
83194** Report an error that an expression is not valid for a partial index WHERE
83195** clause.
83196*/
83197static void notValidPartIdxWhere(
83198  Parse *pParse,       /* Leave error message here */
83199  NameContext *pNC,    /* The name context */
83200  const char *zMsg     /* Type of error */
83201){
83202  if( (pNC->ncFlags & NC_PartIdx)!=0 ){
83203    sqlite3ErrorMsg(pParse, "%s prohibited in partial index WHERE clauses",
83204                    zMsg);
83205  }
83206}
83207
83208#ifndef SQLITE_OMIT_CHECK
83209/*
83210** Report an error that an expression is not valid for a CHECK constraint.
83211*/
83212static void notValidCheckConstraint(
83213  Parse *pParse,       /* Leave error message here */
83214  NameContext *pNC,    /* The name context */
83215  const char *zMsg     /* Type of error */
83216){
83217  if( (pNC->ncFlags & NC_IsCheck)!=0 ){
83218    sqlite3ErrorMsg(pParse,"%s prohibited in CHECK constraints", zMsg);
83219  }
83220}
83221#else
83222# define notValidCheckConstraint(P,N,M)
83223#endif
83224
83225/*
83226** Expression p should encode a floating point value between 1.0 and 0.0.
83227** Return 1024 times this value.  Or return -1 if p is not a floating point
83228** value between 1.0 and 0.0.
83229*/
83230static int exprProbability(Expr *p){
83231  double r = -1.0;
83232  if( p->op!=TK_FLOAT ) return -1;
83233  sqlite3AtoF(p->u.zToken, &r, sqlite3Strlen30(p->u.zToken), SQLITE_UTF8);
83234  assert( r>=0.0 );
83235  if( r>1.0 ) return -1;
83236  return (int)(r*134217728.0);
83237}
83238
83239/*
83240** This routine is callback for sqlite3WalkExpr().
83241**
83242** Resolve symbolic names into TK_COLUMN operators for the current
83243** node in the expression tree.  Return 0 to continue the search down
83244** the tree or 2 to abort the tree walk.
83245**
83246** This routine also does error checking and name resolution for
83247** function names.  The operator for aggregate functions is changed
83248** to TK_AGG_FUNCTION.
83249*/
83250static int resolveExprStep(Walker *pWalker, Expr *pExpr){
83251  NameContext *pNC;
83252  Parse *pParse;
83253
83254  pNC = pWalker->u.pNC;
83255  assert( pNC!=0 );
83256  pParse = pNC->pParse;
83257  assert( pParse==pWalker->pParse );
83258
83259  if( ExprHasProperty(pExpr, EP_Resolved) ) return WRC_Prune;
83260  ExprSetProperty(pExpr, EP_Resolved);
83261#ifndef NDEBUG
83262  if( pNC->pSrcList && pNC->pSrcList->nAlloc>0 ){
83263    SrcList *pSrcList = pNC->pSrcList;
83264    int i;
83265    for(i=0; i<pNC->pSrcList->nSrc; i++){
83266      assert( pSrcList->a[i].iCursor>=0 && pSrcList->a[i].iCursor<pParse->nTab);
83267    }
83268  }
83269#endif
83270  switch( pExpr->op ){
83271
83272#if defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT) && !defined(SQLITE_OMIT_SUBQUERY)
83273    /* The special operator TK_ROW means use the rowid for the first
83274    ** column in the FROM clause.  This is used by the LIMIT and ORDER BY
83275    ** clause processing on UPDATE and DELETE statements.
83276    */
83277    case TK_ROW: {
83278      SrcList *pSrcList = pNC->pSrcList;
83279      struct SrcList_item *pItem;
83280      assert( pSrcList && pSrcList->nSrc==1 );
83281      pItem = pSrcList->a;
83282      pExpr->op = TK_COLUMN;
83283      pExpr->pTab = pItem->pTab;
83284      pExpr->iTable = pItem->iCursor;
83285      pExpr->iColumn = -1;
83286      pExpr->affinity = SQLITE_AFF_INTEGER;
83287      break;
83288    }
83289#endif /* defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT)
83290          && !defined(SQLITE_OMIT_SUBQUERY) */
83291
83292    /* A lone identifier is the name of a column.
83293    */
83294    case TK_ID: {
83295      return lookupName(pParse, 0, 0, pExpr->u.zToken, pNC, pExpr);
83296    }
83297
83298    /* A table name and column name:     ID.ID
83299    ** Or a database, table and column:  ID.ID.ID
83300    */
83301    case TK_DOT: {
83302      const char *zColumn;
83303      const char *zTable;
83304      const char *zDb;
83305      Expr *pRight;
83306
83307      /* if( pSrcList==0 ) break; */
83308      pRight = pExpr->pRight;
83309      if( pRight->op==TK_ID ){
83310        zDb = 0;
83311        zTable = pExpr->pLeft->u.zToken;
83312        zColumn = pRight->u.zToken;
83313      }else{
83314        assert( pRight->op==TK_DOT );
83315        zDb = pExpr->pLeft->u.zToken;
83316        zTable = pRight->pLeft->u.zToken;
83317        zColumn = pRight->pRight->u.zToken;
83318      }
83319      return lookupName(pParse, zDb, zTable, zColumn, pNC, pExpr);
83320    }
83321
83322    /* Resolve function names
83323    */
83324    case TK_FUNCTION: {
83325      ExprList *pList = pExpr->x.pList;    /* The argument list */
83326      int n = pList ? pList->nExpr : 0;    /* Number of arguments */
83327      int no_such_func = 0;       /* True if no such function exists */
83328      int wrong_num_args = 0;     /* True if wrong number of arguments */
83329      int is_agg = 0;             /* True if is an aggregate function */
83330      int auth;                   /* Authorization to use the function */
83331      int nId;                    /* Number of characters in function name */
83332      const char *zId;            /* The function name. */
83333      FuncDef *pDef;              /* Information about the function */
83334      u8 enc = ENC(pParse->db);   /* The database encoding */
83335
83336      assert( !ExprHasProperty(pExpr, EP_xIsSelect) );
83337      notValidPartIdxWhere(pParse, pNC, "functions");
83338      zId = pExpr->u.zToken;
83339      nId = sqlite3Strlen30(zId);
83340      pDef = sqlite3FindFunction(pParse->db, zId, nId, n, enc, 0);
83341      if( pDef==0 ){
83342        pDef = sqlite3FindFunction(pParse->db, zId, nId, -2, enc, 0);
83343        if( pDef==0 ){
83344          no_such_func = 1;
83345        }else{
83346          wrong_num_args = 1;
83347        }
83348      }else{
83349        is_agg = pDef->xFunc==0;
83350        if( pDef->funcFlags & SQLITE_FUNC_UNLIKELY ){
83351          ExprSetProperty(pExpr, EP_Unlikely|EP_Skip);
83352          if( n==2 ){
83353            pExpr->iTable = exprProbability(pList->a[1].pExpr);
83354            if( pExpr->iTable<0 ){
83355              sqlite3ErrorMsg(pParse,
83356                "second argument to likelihood() must be a "
83357                "constant between 0.0 and 1.0");
83358              pNC->nErr++;
83359            }
83360          }else{
83361            /* EVIDENCE-OF: R-61304-29449 The unlikely(X) function is
83362            ** equivalent to likelihood(X, 0.0625).
83363            ** EVIDENCE-OF: R-01283-11636 The unlikely(X) function is
83364            ** short-hand for likelihood(X,0.0625).
83365            ** EVIDENCE-OF: R-36850-34127 The likely(X) function is short-hand
83366            ** for likelihood(X,0.9375).
83367            ** EVIDENCE-OF: R-53436-40973 The likely(X) function is equivalent
83368            ** to likelihood(X,0.9375). */
83369            /* TUNING: unlikely() probability is 0.0625.  likely() is 0.9375 */
83370            pExpr->iTable = pDef->zName[0]=='u' ? 8388608 : 125829120;
83371          }
83372        }
83373#ifndef SQLITE_OMIT_AUTHORIZATION
83374        auth = sqlite3AuthCheck(pParse, SQLITE_FUNCTION, 0, pDef->zName, 0);
83375        if( auth!=SQLITE_OK ){
83376          if( auth==SQLITE_DENY ){
83377            sqlite3ErrorMsg(pParse, "not authorized to use function: %s",
83378                                    pDef->zName);
83379            pNC->nErr++;
83380          }
83381          pExpr->op = TK_NULL;
83382          return WRC_Prune;
83383        }
83384#endif
83385        if( pDef->funcFlags & SQLITE_FUNC_CONSTANT ){
83386          ExprSetProperty(pExpr,EP_ConstFunc);
83387        }
83388      }
83389      if( is_agg && (pNC->ncFlags & NC_AllowAgg)==0 ){
83390        sqlite3ErrorMsg(pParse, "misuse of aggregate function %.*s()", nId,zId);
83391        pNC->nErr++;
83392        is_agg = 0;
83393      }else if( no_such_func && pParse->db->init.busy==0 ){
83394        sqlite3ErrorMsg(pParse, "no such function: %.*s", nId, zId);
83395        pNC->nErr++;
83396      }else if( wrong_num_args ){
83397        sqlite3ErrorMsg(pParse,"wrong number of arguments to function %.*s()",
83398             nId, zId);
83399        pNC->nErr++;
83400      }
83401      if( is_agg ) pNC->ncFlags &= ~NC_AllowAgg;
83402      sqlite3WalkExprList(pWalker, pList);
83403      if( is_agg ){
83404        NameContext *pNC2 = pNC;
83405        pExpr->op = TK_AGG_FUNCTION;
83406        pExpr->op2 = 0;
83407        while( pNC2 && !sqlite3FunctionUsesThisSrc(pExpr, pNC2->pSrcList) ){
83408          pExpr->op2++;
83409          pNC2 = pNC2->pNext;
83410        }
83411        assert( pDef!=0 );
83412        if( pNC2 ){
83413          assert( SQLITE_FUNC_MINMAX==NC_MinMaxAgg );
83414          testcase( (pDef->funcFlags & SQLITE_FUNC_MINMAX)!=0 );
83415          pNC2->ncFlags |= NC_HasAgg | (pDef->funcFlags & SQLITE_FUNC_MINMAX);
83416
83417        }
83418        pNC->ncFlags |= NC_AllowAgg;
83419      }
83420      /* FIX ME:  Compute pExpr->affinity based on the expected return
83421      ** type of the function
83422      */
83423      return WRC_Prune;
83424    }
83425#ifndef SQLITE_OMIT_SUBQUERY
83426    case TK_SELECT:
83427    case TK_EXISTS:  testcase( pExpr->op==TK_EXISTS );
83428#endif
83429    case TK_IN: {
83430      testcase( pExpr->op==TK_IN );
83431      if( ExprHasProperty(pExpr, EP_xIsSelect) ){
83432        int nRef = pNC->nRef;
83433        notValidCheckConstraint(pParse, pNC, "subqueries");
83434        notValidPartIdxWhere(pParse, pNC, "subqueries");
83435        sqlite3WalkSelect(pWalker, pExpr->x.pSelect);
83436        assert( pNC->nRef>=nRef );
83437        if( nRef!=pNC->nRef ){
83438          ExprSetProperty(pExpr, EP_VarSelect);
83439        }
83440      }
83441      break;
83442    }
83443    case TK_VARIABLE: {
83444      notValidCheckConstraint(pParse, pNC, "parameters");
83445      notValidPartIdxWhere(pParse, pNC, "parameters");
83446      break;
83447    }
83448  }
83449  return (pParse->nErr || pParse->db->mallocFailed) ? WRC_Abort : WRC_Continue;
83450}
83451
83452/*
83453** pEList is a list of expressions which are really the result set of the
83454** a SELECT statement.  pE is a term in an ORDER BY or GROUP BY clause.
83455** This routine checks to see if pE is a simple identifier which corresponds
83456** to the AS-name of one of the terms of the expression list.  If it is,
83457** this routine return an integer between 1 and N where N is the number of
83458** elements in pEList, corresponding to the matching entry.  If there is
83459** no match, or if pE is not a simple identifier, then this routine
83460** return 0.
83461**
83462** pEList has been resolved.  pE has not.
83463*/
83464static int resolveAsName(
83465  Parse *pParse,     /* Parsing context for error messages */
83466  ExprList *pEList,  /* List of expressions to scan */
83467  Expr *pE           /* Expression we are trying to match */
83468){
83469  int i;             /* Loop counter */
83470
83471  UNUSED_PARAMETER(pParse);
83472
83473  if( pE->op==TK_ID ){
83474    char *zCol = pE->u.zToken;
83475    for(i=0; i<pEList->nExpr; i++){
83476      char *zAs = pEList->a[i].zName;
83477      if( zAs!=0 && sqlite3StrICmp(zAs, zCol)==0 ){
83478        return i+1;
83479      }
83480    }
83481  }
83482  return 0;
83483}
83484
83485/*
83486** pE is a pointer to an expression which is a single term in the
83487** ORDER BY of a compound SELECT.  The expression has not been
83488** name resolved.
83489**
83490** At the point this routine is called, we already know that the
83491** ORDER BY term is not an integer index into the result set.  That
83492** case is handled by the calling routine.
83493**
83494** Attempt to match pE against result set columns in the left-most
83495** SELECT statement.  Return the index i of the matching column,
83496** as an indication to the caller that it should sort by the i-th column.
83497** The left-most column is 1.  In other words, the value returned is the
83498** same integer value that would be used in the SQL statement to indicate
83499** the column.
83500**
83501** If there is no match, return 0.  Return -1 if an error occurs.
83502*/
83503static int resolveOrderByTermToExprList(
83504  Parse *pParse,     /* Parsing context for error messages */
83505  Select *pSelect,   /* The SELECT statement with the ORDER BY clause */
83506  Expr *pE           /* The specific ORDER BY term */
83507){
83508  int i;             /* Loop counter */
83509  ExprList *pEList;  /* The columns of the result set */
83510  NameContext nc;    /* Name context for resolving pE */
83511  sqlite3 *db;       /* Database connection */
83512  int rc;            /* Return code from subprocedures */
83513  u8 savedSuppErr;   /* Saved value of db->suppressErr */
83514
83515  assert( sqlite3ExprIsInteger(pE, &i)==0 );
83516  pEList = pSelect->pEList;
83517
83518  /* Resolve all names in the ORDER BY term expression
83519  */
83520  memset(&nc, 0, sizeof(nc));
83521  nc.pParse = pParse;
83522  nc.pSrcList = pSelect->pSrc;
83523  nc.pEList = pEList;
83524  nc.ncFlags = NC_AllowAgg;
83525  nc.nErr = 0;
83526  db = pParse->db;
83527  savedSuppErr = db->suppressErr;
83528  db->suppressErr = 1;
83529  rc = sqlite3ResolveExprNames(&nc, pE);
83530  db->suppressErr = savedSuppErr;
83531  if( rc ) return 0;
83532
83533  /* Try to match the ORDER BY expression against an expression
83534  ** in the result set.  Return an 1-based index of the matching
83535  ** result-set entry.
83536  */
83537  for(i=0; i<pEList->nExpr; i++){
83538    if( sqlite3ExprCompare(pEList->a[i].pExpr, pE, -1)<2 ){
83539      return i+1;
83540    }
83541  }
83542
83543  /* If no match, return 0. */
83544  return 0;
83545}
83546
83547/*
83548** Generate an ORDER BY or GROUP BY term out-of-range error.
83549*/
83550static void resolveOutOfRangeError(
83551  Parse *pParse,         /* The error context into which to write the error */
83552  const char *zType,     /* "ORDER" or "GROUP" */
83553  int i,                 /* The index (1-based) of the term out of range */
83554  int mx                 /* Largest permissible value of i */
83555){
83556  sqlite3ErrorMsg(pParse,
83557    "%r %s BY term out of range - should be "
83558    "between 1 and %d", i, zType, mx);
83559}
83560
83561/*
83562** Analyze the ORDER BY clause in a compound SELECT statement.   Modify
83563** each term of the ORDER BY clause is a constant integer between 1
83564** and N where N is the number of columns in the compound SELECT.
83565**
83566** ORDER BY terms that are already an integer between 1 and N are
83567** unmodified.  ORDER BY terms that are integers outside the range of
83568** 1 through N generate an error.  ORDER BY terms that are expressions
83569** are matched against result set expressions of compound SELECT
83570** beginning with the left-most SELECT and working toward the right.
83571** At the first match, the ORDER BY expression is transformed into
83572** the integer column number.
83573**
83574** Return the number of errors seen.
83575*/
83576static int resolveCompoundOrderBy(
83577  Parse *pParse,        /* Parsing context.  Leave error messages here */
83578  Select *pSelect       /* The SELECT statement containing the ORDER BY */
83579){
83580  int i;
83581  ExprList *pOrderBy;
83582  ExprList *pEList;
83583  sqlite3 *db;
83584  int moreToDo = 1;
83585
83586  pOrderBy = pSelect->pOrderBy;
83587  if( pOrderBy==0 ) return 0;
83588  db = pParse->db;
83589#if SQLITE_MAX_COLUMN
83590  if( pOrderBy->nExpr>db->aLimit[SQLITE_LIMIT_COLUMN] ){
83591    sqlite3ErrorMsg(pParse, "too many terms in ORDER BY clause");
83592    return 1;
83593  }
83594#endif
83595  for(i=0; i<pOrderBy->nExpr; i++){
83596    pOrderBy->a[i].done = 0;
83597  }
83598  pSelect->pNext = 0;
83599  while( pSelect->pPrior ){
83600    pSelect->pPrior->pNext = pSelect;
83601    pSelect = pSelect->pPrior;
83602  }
83603  while( pSelect && moreToDo ){
83604    struct ExprList_item *pItem;
83605    moreToDo = 0;
83606    pEList = pSelect->pEList;
83607    assert( pEList!=0 );
83608    for(i=0, pItem=pOrderBy->a; i<pOrderBy->nExpr; i++, pItem++){
83609      int iCol = -1;
83610      Expr *pE, *pDup;
83611      if( pItem->done ) continue;
83612      pE = sqlite3ExprSkipCollate(pItem->pExpr);
83613      if( sqlite3ExprIsInteger(pE, &iCol) ){
83614        if( iCol<=0 || iCol>pEList->nExpr ){
83615          resolveOutOfRangeError(pParse, "ORDER", i+1, pEList->nExpr);
83616          return 1;
83617        }
83618      }else{
83619        iCol = resolveAsName(pParse, pEList, pE);
83620        if( iCol==0 ){
83621          pDup = sqlite3ExprDup(db, pE, 0);
83622          if( !db->mallocFailed ){
83623            assert(pDup);
83624            iCol = resolveOrderByTermToExprList(pParse, pSelect, pDup);
83625          }
83626          sqlite3ExprDelete(db, pDup);
83627        }
83628      }
83629      if( iCol>0 ){
83630        /* Convert the ORDER BY term into an integer column number iCol,
83631        ** taking care to preserve the COLLATE clause if it exists */
83632        Expr *pNew = sqlite3Expr(db, TK_INTEGER, 0);
83633        if( pNew==0 ) return 1;
83634        pNew->flags |= EP_IntValue;
83635        pNew->u.iValue = iCol;
83636        if( pItem->pExpr==pE ){
83637          pItem->pExpr = pNew;
83638        }else{
83639          Expr *pParent = pItem->pExpr;
83640          assert( pParent->op==TK_COLLATE );
83641          while( pParent->pLeft->op==TK_COLLATE ) pParent = pParent->pLeft;
83642          assert( pParent->pLeft==pE );
83643          pParent->pLeft = pNew;
83644        }
83645        sqlite3ExprDelete(db, pE);
83646        pItem->u.x.iOrderByCol = (u16)iCol;
83647        pItem->done = 1;
83648      }else{
83649        moreToDo = 1;
83650      }
83651    }
83652    pSelect = pSelect->pNext;
83653  }
83654  for(i=0; i<pOrderBy->nExpr; i++){
83655    if( pOrderBy->a[i].done==0 ){
83656      sqlite3ErrorMsg(pParse, "%r ORDER BY term does not match any "
83657            "column in the result set", i+1);
83658      return 1;
83659    }
83660  }
83661  return 0;
83662}
83663
83664/*
83665** Check every term in the ORDER BY or GROUP BY clause pOrderBy of
83666** the SELECT statement pSelect.  If any term is reference to a
83667** result set expression (as determined by the ExprList.a.u.x.iOrderByCol
83668** field) then convert that term into a copy of the corresponding result set
83669** column.
83670**
83671** If any errors are detected, add an error message to pParse and
83672** return non-zero.  Return zero if no errors are seen.
83673*/
83674SQLITE_PRIVATE int sqlite3ResolveOrderGroupBy(
83675  Parse *pParse,        /* Parsing context.  Leave error messages here */
83676  Select *pSelect,      /* The SELECT statement containing the clause */
83677  ExprList *pOrderBy,   /* The ORDER BY or GROUP BY clause to be processed */
83678  const char *zType     /* "ORDER" or "GROUP" */
83679){
83680  int i;
83681  sqlite3 *db = pParse->db;
83682  ExprList *pEList;
83683  struct ExprList_item *pItem;
83684
83685  if( pOrderBy==0 || pParse->db->mallocFailed ) return 0;
83686#if SQLITE_MAX_COLUMN
83687  if( pOrderBy->nExpr>db->aLimit[SQLITE_LIMIT_COLUMN] ){
83688    sqlite3ErrorMsg(pParse, "too many terms in %s BY clause", zType);
83689    return 1;
83690  }
83691#endif
83692  pEList = pSelect->pEList;
83693  assert( pEList!=0 );  /* sqlite3SelectNew() guarantees this */
83694  for(i=0, pItem=pOrderBy->a; i<pOrderBy->nExpr; i++, pItem++){
83695    if( pItem->u.x.iOrderByCol ){
83696      if( pItem->u.x.iOrderByCol>pEList->nExpr ){
83697        resolveOutOfRangeError(pParse, zType, i+1, pEList->nExpr);
83698        return 1;
83699      }
83700      resolveAlias(pParse, pEList, pItem->u.x.iOrderByCol-1, pItem->pExpr,
83701                   zType,0);
83702    }
83703  }
83704  return 0;
83705}
83706
83707/*
83708** pOrderBy is an ORDER BY or GROUP BY clause in SELECT statement pSelect.
83709** The Name context of the SELECT statement is pNC.  zType is either
83710** "ORDER" or "GROUP" depending on which type of clause pOrderBy is.
83711**
83712** This routine resolves each term of the clause into an expression.
83713** If the order-by term is an integer I between 1 and N (where N is the
83714** number of columns in the result set of the SELECT) then the expression
83715** in the resolution is a copy of the I-th result-set expression.  If
83716** the order-by term is an identifier that corresponds to the AS-name of
83717** a result-set expression, then the term resolves to a copy of the
83718** result-set expression.  Otherwise, the expression is resolved in
83719** the usual way - using sqlite3ResolveExprNames().
83720**
83721** This routine returns the number of errors.  If errors occur, then
83722** an appropriate error message might be left in pParse.  (OOM errors
83723** excepted.)
83724*/
83725static int resolveOrderGroupBy(
83726  NameContext *pNC,     /* The name context of the SELECT statement */
83727  Select *pSelect,      /* The SELECT statement holding pOrderBy */
83728  ExprList *pOrderBy,   /* An ORDER BY or GROUP BY clause to resolve */
83729  const char *zType     /* Either "ORDER" or "GROUP", as appropriate */
83730){
83731  int i, j;                      /* Loop counters */
83732  int iCol;                      /* Column number */
83733  struct ExprList_item *pItem;   /* A term of the ORDER BY clause */
83734  Parse *pParse;                 /* Parsing context */
83735  int nResult;                   /* Number of terms in the result set */
83736
83737  if( pOrderBy==0 ) return 0;
83738  nResult = pSelect->pEList->nExpr;
83739  pParse = pNC->pParse;
83740  for(i=0, pItem=pOrderBy->a; i<pOrderBy->nExpr; i++, pItem++){
83741    Expr *pE = pItem->pExpr;
83742    Expr *pE2 = sqlite3ExprSkipCollate(pE);
83743    if( zType[0]!='G' ){
83744      iCol = resolveAsName(pParse, pSelect->pEList, pE2);
83745      if( iCol>0 ){
83746        /* If an AS-name match is found, mark this ORDER BY column as being
83747        ** a copy of the iCol-th result-set column.  The subsequent call to
83748        ** sqlite3ResolveOrderGroupBy() will convert the expression to a
83749        ** copy of the iCol-th result-set expression. */
83750        pItem->u.x.iOrderByCol = (u16)iCol;
83751        continue;
83752      }
83753    }
83754    if( sqlite3ExprIsInteger(pE2, &iCol) ){
83755      /* The ORDER BY term is an integer constant.  Again, set the column
83756      ** number so that sqlite3ResolveOrderGroupBy() will convert the
83757      ** order-by term to a copy of the result-set expression */
83758      if( iCol<1 || iCol>0xffff ){
83759        resolveOutOfRangeError(pParse, zType, i+1, nResult);
83760        return 1;
83761      }
83762      pItem->u.x.iOrderByCol = (u16)iCol;
83763      continue;
83764    }
83765
83766    /* Otherwise, treat the ORDER BY term as an ordinary expression */
83767    pItem->u.x.iOrderByCol = 0;
83768    if( sqlite3ResolveExprNames(pNC, pE) ){
83769      return 1;
83770    }
83771    for(j=0; j<pSelect->pEList->nExpr; j++){
83772      if( sqlite3ExprCompare(pE, pSelect->pEList->a[j].pExpr, -1)==0 ){
83773        pItem->u.x.iOrderByCol = j+1;
83774      }
83775    }
83776  }
83777  return sqlite3ResolveOrderGroupBy(pParse, pSelect, pOrderBy, zType);
83778}
83779
83780/*
83781** Resolve names in the SELECT statement p and all of its descendants.
83782*/
83783static int resolveSelectStep(Walker *pWalker, Select *p){
83784  NameContext *pOuterNC;  /* Context that contains this SELECT */
83785  NameContext sNC;        /* Name context of this SELECT */
83786  int isCompound;         /* True if p is a compound select */
83787  int nCompound;          /* Number of compound terms processed so far */
83788  Parse *pParse;          /* Parsing context */
83789  ExprList *pEList;       /* Result set expression list */
83790  int i;                  /* Loop counter */
83791  ExprList *pGroupBy;     /* The GROUP BY clause */
83792  Select *pLeftmost;      /* Left-most of SELECT of a compound */
83793  sqlite3 *db;            /* Database connection */
83794
83795
83796  assert( p!=0 );
83797  if( p->selFlags & SF_Resolved ){
83798    return WRC_Prune;
83799  }
83800  pOuterNC = pWalker->u.pNC;
83801  pParse = pWalker->pParse;
83802  db = pParse->db;
83803
83804  /* Normally sqlite3SelectExpand() will be called first and will have
83805  ** already expanded this SELECT.  However, if this is a subquery within
83806  ** an expression, sqlite3ResolveExprNames() will be called without a
83807  ** prior call to sqlite3SelectExpand().  When that happens, let
83808  ** sqlite3SelectPrep() do all of the processing for this SELECT.
83809  ** sqlite3SelectPrep() will invoke both sqlite3SelectExpand() and
83810  ** this routine in the correct order.
83811  */
83812  if( (p->selFlags & SF_Expanded)==0 ){
83813    sqlite3SelectPrep(pParse, p, pOuterNC);
83814    return (pParse->nErr || db->mallocFailed) ? WRC_Abort : WRC_Prune;
83815  }
83816
83817  isCompound = p->pPrior!=0;
83818  nCompound = 0;
83819  pLeftmost = p;
83820  while( p ){
83821    assert( (p->selFlags & SF_Expanded)!=0 );
83822    assert( (p->selFlags & SF_Resolved)==0 );
83823    p->selFlags |= SF_Resolved;
83824
83825    /* Resolve the expressions in the LIMIT and OFFSET clauses. These
83826    ** are not allowed to refer to any names, so pass an empty NameContext.
83827    */
83828    memset(&sNC, 0, sizeof(sNC));
83829    sNC.pParse = pParse;
83830    if( sqlite3ResolveExprNames(&sNC, p->pLimit) ||
83831        sqlite3ResolveExprNames(&sNC, p->pOffset) ){
83832      return WRC_Abort;
83833    }
83834
83835    /* If the SF_Converted flags is set, then this Select object was
83836    ** was created by the convertCompoundSelectToSubquery() function.
83837    ** In this case the ORDER BY clause (p->pOrderBy) should be resolved
83838    ** as if it were part of the sub-query, not the parent. This block
83839    ** moves the pOrderBy down to the sub-query. It will be moved back
83840    ** after the names have been resolved.  */
83841    if( p->selFlags & SF_Converted ){
83842      Select *pSub = p->pSrc->a[0].pSelect;
83843      assert( p->pSrc->nSrc==1 && p->pOrderBy );
83844      assert( pSub->pPrior && pSub->pOrderBy==0 );
83845      pSub->pOrderBy = p->pOrderBy;
83846      p->pOrderBy = 0;
83847    }
83848
83849    /* Recursively resolve names in all subqueries
83850    */
83851    for(i=0; i<p->pSrc->nSrc; i++){
83852      struct SrcList_item *pItem = &p->pSrc->a[i];
83853      if( pItem->pSelect ){
83854        NameContext *pNC;         /* Used to iterate name contexts */
83855        int nRef = 0;             /* Refcount for pOuterNC and outer contexts */
83856        const char *zSavedContext = pParse->zAuthContext;
83857
83858        /* Count the total number of references to pOuterNC and all of its
83859        ** parent contexts. After resolving references to expressions in
83860        ** pItem->pSelect, check if this value has changed. If so, then
83861        ** SELECT statement pItem->pSelect must be correlated. Set the
83862        ** pItem->isCorrelated flag if this is the case. */
83863        for(pNC=pOuterNC; pNC; pNC=pNC->pNext) nRef += pNC->nRef;
83864
83865        if( pItem->zName ) pParse->zAuthContext = pItem->zName;
83866        sqlite3ResolveSelectNames(pParse, pItem->pSelect, pOuterNC);
83867        pParse->zAuthContext = zSavedContext;
83868        if( pParse->nErr || db->mallocFailed ) return WRC_Abort;
83869
83870        for(pNC=pOuterNC; pNC; pNC=pNC->pNext) nRef -= pNC->nRef;
83871        assert( pItem->isCorrelated==0 && nRef<=0 );
83872        pItem->isCorrelated = (nRef!=0);
83873      }
83874    }
83875
83876    /* Set up the local name-context to pass to sqlite3ResolveExprNames() to
83877    ** resolve the result-set expression list.
83878    */
83879    sNC.ncFlags = NC_AllowAgg;
83880    sNC.pSrcList = p->pSrc;
83881    sNC.pNext = pOuterNC;
83882
83883    /* Resolve names in the result set. */
83884    pEList = p->pEList;
83885    assert( pEList!=0 );
83886    for(i=0; i<pEList->nExpr; i++){
83887      Expr *pX = pEList->a[i].pExpr;
83888      if( sqlite3ResolveExprNames(&sNC, pX) ){
83889        return WRC_Abort;
83890      }
83891    }
83892
83893    /* If there are no aggregate functions in the result-set, and no GROUP BY
83894    ** expression, do not allow aggregates in any of the other expressions.
83895    */
83896    assert( (p->selFlags & SF_Aggregate)==0 );
83897    pGroupBy = p->pGroupBy;
83898    if( pGroupBy || (sNC.ncFlags & NC_HasAgg)!=0 ){
83899      assert( NC_MinMaxAgg==SF_MinMaxAgg );
83900      p->selFlags |= SF_Aggregate | (sNC.ncFlags&NC_MinMaxAgg);
83901    }else{
83902      sNC.ncFlags &= ~NC_AllowAgg;
83903    }
83904
83905    /* If a HAVING clause is present, then there must be a GROUP BY clause.
83906    */
83907    if( p->pHaving && !pGroupBy ){
83908      sqlite3ErrorMsg(pParse, "a GROUP BY clause is required before HAVING");
83909      return WRC_Abort;
83910    }
83911
83912    /* Add the output column list to the name-context before parsing the
83913    ** other expressions in the SELECT statement. This is so that
83914    ** expressions in the WHERE clause (etc.) can refer to expressions by
83915    ** aliases in the result set.
83916    **
83917    ** Minor point: If this is the case, then the expression will be
83918    ** re-evaluated for each reference to it.
83919    */
83920    sNC.pEList = p->pEList;
83921    if( sqlite3ResolveExprNames(&sNC, p->pHaving) ) return WRC_Abort;
83922    if( sqlite3ResolveExprNames(&sNC, p->pWhere) ) return WRC_Abort;
83923
83924    /* The ORDER BY and GROUP BY clauses may not refer to terms in
83925    ** outer queries
83926    */
83927    sNC.pNext = 0;
83928    sNC.ncFlags |= NC_AllowAgg;
83929
83930    /* If this is a converted compound query, move the ORDER BY clause from
83931    ** the sub-query back to the parent query. At this point each term
83932    ** within the ORDER BY clause has been transformed to an integer value.
83933    ** These integers will be replaced by copies of the corresponding result
83934    ** set expressions by the call to resolveOrderGroupBy() below.  */
83935    if( p->selFlags & SF_Converted ){
83936      Select *pSub = p->pSrc->a[0].pSelect;
83937      p->pOrderBy = pSub->pOrderBy;
83938      pSub->pOrderBy = 0;
83939    }
83940
83941    /* Process the ORDER BY clause for singleton SELECT statements.
83942    ** The ORDER BY clause for compounds SELECT statements is handled
83943    ** below, after all of the result-sets for all of the elements of
83944    ** the compound have been resolved.
83945    **
83946    ** If there is an ORDER BY clause on a term of a compound-select other
83947    ** than the right-most term, then that is a syntax error.  But the error
83948    ** is not detected until much later, and so we need to go ahead and
83949    ** resolve those symbols on the incorrect ORDER BY for consistency.
83950    */
83951    if( isCompound<=nCompound  /* Defer right-most ORDER BY of a compound */
83952     && resolveOrderGroupBy(&sNC, p, p->pOrderBy, "ORDER")
83953    ){
83954      return WRC_Abort;
83955    }
83956    if( db->mallocFailed ){
83957      return WRC_Abort;
83958    }
83959
83960    /* Resolve the GROUP BY clause.  At the same time, make sure
83961    ** the GROUP BY clause does not contain aggregate functions.
83962    */
83963    if( pGroupBy ){
83964      struct ExprList_item *pItem;
83965
83966      if( resolveOrderGroupBy(&sNC, p, pGroupBy, "GROUP") || db->mallocFailed ){
83967        return WRC_Abort;
83968      }
83969      for(i=0, pItem=pGroupBy->a; i<pGroupBy->nExpr; i++, pItem++){
83970        if( ExprHasProperty(pItem->pExpr, EP_Agg) ){
83971          sqlite3ErrorMsg(pParse, "aggregate functions are not allowed in "
83972              "the GROUP BY clause");
83973          return WRC_Abort;
83974        }
83975      }
83976    }
83977
83978    /* If this is part of a compound SELECT, check that it has the right
83979    ** number of expressions in the select list. */
83980    if( p->pNext && p->pEList->nExpr!=p->pNext->pEList->nExpr ){
83981      sqlite3SelectWrongNumTermsError(pParse, p->pNext);
83982      return WRC_Abort;
83983    }
83984
83985    /* Advance to the next term of the compound
83986    */
83987    p = p->pPrior;
83988    nCompound++;
83989  }
83990
83991  /* Resolve the ORDER BY on a compound SELECT after all terms of
83992  ** the compound have been resolved.
83993  */
83994  if( isCompound && resolveCompoundOrderBy(pParse, pLeftmost) ){
83995    return WRC_Abort;
83996  }
83997
83998  return WRC_Prune;
83999}
84000
84001/*
84002** This routine walks an expression tree and resolves references to
84003** table columns and result-set columns.  At the same time, do error
84004** checking on function usage and set a flag if any aggregate functions
84005** are seen.
84006**
84007** To resolve table columns references we look for nodes (or subtrees) of the
84008** form X.Y.Z or Y.Z or just Z where
84009**
84010**      X:   The name of a database.  Ex:  "main" or "temp" or
84011**           the symbolic name assigned to an ATTACH-ed database.
84012**
84013**      Y:   The name of a table in a FROM clause.  Or in a trigger
84014**           one of the special names "old" or "new".
84015**
84016**      Z:   The name of a column in table Y.
84017**
84018** The node at the root of the subtree is modified as follows:
84019**
84020**    Expr.op        Changed to TK_COLUMN
84021**    Expr.pTab      Points to the Table object for X.Y
84022**    Expr.iColumn   The column index in X.Y.  -1 for the rowid.
84023**    Expr.iTable    The VDBE cursor number for X.Y
84024**
84025**
84026** To resolve result-set references, look for expression nodes of the
84027** form Z (with no X and Y prefix) where the Z matches the right-hand
84028** size of an AS clause in the result-set of a SELECT.  The Z expression
84029** is replaced by a copy of the left-hand side of the result-set expression.
84030** Table-name and function resolution occurs on the substituted expression
84031** tree.  For example, in:
84032**
84033**      SELECT a+b AS x, c+d AS y FROM t1 ORDER BY x;
84034**
84035** The "x" term of the order by is replaced by "a+b" to render:
84036**
84037**      SELECT a+b AS x, c+d AS y FROM t1 ORDER BY a+b;
84038**
84039** Function calls are checked to make sure that the function is
84040** defined and that the correct number of arguments are specified.
84041** If the function is an aggregate function, then the NC_HasAgg flag is
84042** set and the opcode is changed from TK_FUNCTION to TK_AGG_FUNCTION.
84043** If an expression contains aggregate functions then the EP_Agg
84044** property on the expression is set.
84045**
84046** An error message is left in pParse if anything is amiss.  The number
84047** if errors is returned.
84048*/
84049SQLITE_PRIVATE int sqlite3ResolveExprNames(
84050  NameContext *pNC,       /* Namespace to resolve expressions in. */
84051  Expr *pExpr             /* The expression to be analyzed. */
84052){
84053  u16 savedHasAgg;
84054  Walker w;
84055
84056  if( pExpr==0 ) return 0;
84057#if SQLITE_MAX_EXPR_DEPTH>0
84058  {
84059    Parse *pParse = pNC->pParse;
84060    if( sqlite3ExprCheckHeight(pParse, pExpr->nHeight+pNC->pParse->nHeight) ){
84061      return 1;
84062    }
84063    pParse->nHeight += pExpr->nHeight;
84064  }
84065#endif
84066  savedHasAgg = pNC->ncFlags & (NC_HasAgg|NC_MinMaxAgg);
84067  pNC->ncFlags &= ~(NC_HasAgg|NC_MinMaxAgg);
84068  memset(&w, 0, sizeof(w));
84069  w.xExprCallback = resolveExprStep;
84070  w.xSelectCallback = resolveSelectStep;
84071  w.pParse = pNC->pParse;
84072  w.u.pNC = pNC;
84073  sqlite3WalkExpr(&w, pExpr);
84074#if SQLITE_MAX_EXPR_DEPTH>0
84075  pNC->pParse->nHeight -= pExpr->nHeight;
84076#endif
84077  if( pNC->nErr>0 || w.pParse->nErr>0 ){
84078    ExprSetProperty(pExpr, EP_Error);
84079  }
84080  if( pNC->ncFlags & NC_HasAgg ){
84081    ExprSetProperty(pExpr, EP_Agg);
84082  }
84083  pNC->ncFlags |= savedHasAgg;
84084  return ExprHasProperty(pExpr, EP_Error);
84085}
84086
84087
84088/*
84089** Resolve all names in all expressions of a SELECT and in all
84090** decendents of the SELECT, including compounds off of p->pPrior,
84091** subqueries in expressions, and subqueries used as FROM clause
84092** terms.
84093**
84094** See sqlite3ResolveExprNames() for a description of the kinds of
84095** transformations that occur.
84096**
84097** All SELECT statements should have been expanded using
84098** sqlite3SelectExpand() prior to invoking this routine.
84099*/
84100SQLITE_PRIVATE void sqlite3ResolveSelectNames(
84101  Parse *pParse,         /* The parser context */
84102  Select *p,             /* The SELECT statement being coded. */
84103  NameContext *pOuterNC  /* Name context for parent SELECT statement */
84104){
84105  Walker w;
84106
84107  assert( p!=0 );
84108  memset(&w, 0, sizeof(w));
84109  w.xExprCallback = resolveExprStep;
84110  w.xSelectCallback = resolveSelectStep;
84111  w.pParse = pParse;
84112  w.u.pNC = pOuterNC;
84113  sqlite3WalkSelect(&w, p);
84114}
84115
84116/*
84117** Resolve names in expressions that can only reference a single table:
84118**
84119**    *   CHECK constraints
84120**    *   WHERE clauses on partial indices
84121**
84122** The Expr.iTable value for Expr.op==TK_COLUMN nodes of the expression
84123** is set to -1 and the Expr.iColumn value is set to the column number.
84124**
84125** Any errors cause an error message to be set in pParse.
84126*/
84127SQLITE_PRIVATE void sqlite3ResolveSelfReference(
84128  Parse *pParse,      /* Parsing context */
84129  Table *pTab,        /* The table being referenced */
84130  int type,           /* NC_IsCheck or NC_PartIdx */
84131  Expr *pExpr,        /* Expression to resolve.  May be NULL. */
84132  ExprList *pList     /* Expression list to resolve.  May be NUL. */
84133){
84134  SrcList sSrc;                   /* Fake SrcList for pParse->pNewTable */
84135  NameContext sNC;                /* Name context for pParse->pNewTable */
84136  int i;                          /* Loop counter */
84137
84138  assert( type==NC_IsCheck || type==NC_PartIdx );
84139  memset(&sNC, 0, sizeof(sNC));
84140  memset(&sSrc, 0, sizeof(sSrc));
84141  sSrc.nSrc = 1;
84142  sSrc.a[0].zName = pTab->zName;
84143  sSrc.a[0].pTab = pTab;
84144  sSrc.a[0].iCursor = -1;
84145  sNC.pParse = pParse;
84146  sNC.pSrcList = &sSrc;
84147  sNC.ncFlags = type;
84148  if( sqlite3ResolveExprNames(&sNC, pExpr) ) return;
84149  if( pList ){
84150    for(i=0; i<pList->nExpr; i++){
84151      if( sqlite3ResolveExprNames(&sNC, pList->a[i].pExpr) ){
84152        return;
84153      }
84154    }
84155  }
84156}
84157
84158/************** End of resolve.c *********************************************/
84159/************** Begin file expr.c ********************************************/
84160/*
84161** 2001 September 15
84162**
84163** The author disclaims copyright to this source code.  In place of
84164** a legal notice, here is a blessing:
84165**
84166**    May you do good and not evil.
84167**    May you find forgiveness for yourself and forgive others.
84168**    May you share freely, never taking more than you give.
84169**
84170*************************************************************************
84171** This file contains routines used for analyzing expressions and
84172** for generating VDBE code that evaluates expressions in SQLite.
84173*/
84174/* #include "sqliteInt.h" */
84175
84176/*
84177** Return the 'affinity' of the expression pExpr if any.
84178**
84179** If pExpr is a column, a reference to a column via an 'AS' alias,
84180** or a sub-select with a column as the return value, then the
84181** affinity of that column is returned. Otherwise, 0x00 is returned,
84182** indicating no affinity for the expression.
84183**
84184** i.e. the WHERE clause expressions in the following statements all
84185** have an affinity:
84186**
84187** CREATE TABLE t1(a);
84188** SELECT * FROM t1 WHERE a;
84189** SELECT a AS b FROM t1 WHERE b;
84190** SELECT * FROM t1 WHERE (select a from t1);
84191*/
84192SQLITE_PRIVATE char sqlite3ExprAffinity(Expr *pExpr){
84193  int op;
84194  pExpr = sqlite3ExprSkipCollate(pExpr);
84195  if( pExpr->flags & EP_Generic ) return 0;
84196  op = pExpr->op;
84197  if( op==TK_SELECT ){
84198    assert( pExpr->flags&EP_xIsSelect );
84199    return sqlite3ExprAffinity(pExpr->x.pSelect->pEList->a[0].pExpr);
84200  }
84201#ifndef SQLITE_OMIT_CAST
84202  if( op==TK_CAST ){
84203    assert( !ExprHasProperty(pExpr, EP_IntValue) );
84204    return sqlite3AffinityType(pExpr->u.zToken, 0);
84205  }
84206#endif
84207  if( (op==TK_AGG_COLUMN || op==TK_COLUMN || op==TK_REGISTER)
84208   && pExpr->pTab!=0
84209  ){
84210    /* op==TK_REGISTER && pExpr->pTab!=0 happens when pExpr was originally
84211    ** a TK_COLUMN but was previously evaluated and cached in a register */
84212    int j = pExpr->iColumn;
84213    if( j<0 ) return SQLITE_AFF_INTEGER;
84214    assert( pExpr->pTab && j<pExpr->pTab->nCol );
84215    return pExpr->pTab->aCol[j].affinity;
84216  }
84217  return pExpr->affinity;
84218}
84219
84220/*
84221** Set the collating sequence for expression pExpr to be the collating
84222** sequence named by pToken.   Return a pointer to a new Expr node that
84223** implements the COLLATE operator.
84224**
84225** If a memory allocation error occurs, that fact is recorded in pParse->db
84226** and the pExpr parameter is returned unchanged.
84227*/
84228SQLITE_PRIVATE Expr *sqlite3ExprAddCollateToken(
84229  Parse *pParse,           /* Parsing context */
84230  Expr *pExpr,             /* Add the "COLLATE" clause to this expression */
84231  const Token *pCollName,  /* Name of collating sequence */
84232  int dequote              /* True to dequote pCollName */
84233){
84234  if( pCollName->n>0 ){
84235    Expr *pNew = sqlite3ExprAlloc(pParse->db, TK_COLLATE, pCollName, dequote);
84236    if( pNew ){
84237      pNew->pLeft = pExpr;
84238      pNew->flags |= EP_Collate|EP_Skip;
84239      pExpr = pNew;
84240    }
84241  }
84242  return pExpr;
84243}
84244SQLITE_PRIVATE Expr *sqlite3ExprAddCollateString(Parse *pParse, Expr *pExpr, const char *zC){
84245  Token s;
84246  assert( zC!=0 );
84247  s.z = zC;
84248  s.n = sqlite3Strlen30(s.z);
84249  return sqlite3ExprAddCollateToken(pParse, pExpr, &s, 0);
84250}
84251
84252/*
84253** Skip over any TK_COLLATE or TK_AS operators and any unlikely()
84254** or likelihood() function at the root of an expression.
84255*/
84256SQLITE_PRIVATE Expr *sqlite3ExprSkipCollate(Expr *pExpr){
84257  while( pExpr && ExprHasProperty(pExpr, EP_Skip) ){
84258    if( ExprHasProperty(pExpr, EP_Unlikely) ){
84259      assert( !ExprHasProperty(pExpr, EP_xIsSelect) );
84260      assert( pExpr->x.pList->nExpr>0 );
84261      assert( pExpr->op==TK_FUNCTION );
84262      pExpr = pExpr->x.pList->a[0].pExpr;
84263    }else{
84264      assert( pExpr->op==TK_COLLATE || pExpr->op==TK_AS );
84265      pExpr = pExpr->pLeft;
84266    }
84267  }
84268  return pExpr;
84269}
84270
84271/*
84272** Return the collation sequence for the expression pExpr. If
84273** there is no defined collating sequence, return NULL.
84274**
84275** The collating sequence might be determined by a COLLATE operator
84276** or by the presence of a column with a defined collating sequence.
84277** COLLATE operators take first precedence.  Left operands take
84278** precedence over right operands.
84279*/
84280SQLITE_PRIVATE CollSeq *sqlite3ExprCollSeq(Parse *pParse, Expr *pExpr){
84281  sqlite3 *db = pParse->db;
84282  CollSeq *pColl = 0;
84283  Expr *p = pExpr;
84284  while( p ){
84285    int op = p->op;
84286    if( p->flags & EP_Generic ) break;
84287    if( op==TK_CAST || op==TK_UPLUS ){
84288      p = p->pLeft;
84289      continue;
84290    }
84291    if( op==TK_COLLATE || (op==TK_REGISTER && p->op2==TK_COLLATE) ){
84292      pColl = sqlite3GetCollSeq(pParse, ENC(db), 0, p->u.zToken);
84293      break;
84294    }
84295    if( (op==TK_AGG_COLUMN || op==TK_COLUMN
84296          || op==TK_REGISTER || op==TK_TRIGGER)
84297     && p->pTab!=0
84298    ){
84299      /* op==TK_REGISTER && p->pTab!=0 happens when pExpr was originally
84300      ** a TK_COLUMN but was previously evaluated and cached in a register */
84301      int j = p->iColumn;
84302      if( j>=0 ){
84303        const char *zColl = p->pTab->aCol[j].zColl;
84304        pColl = sqlite3FindCollSeq(db, ENC(db), zColl, 0);
84305      }
84306      break;
84307    }
84308    if( p->flags & EP_Collate ){
84309      if( p->pLeft && (p->pLeft->flags & EP_Collate)!=0 ){
84310        p = p->pLeft;
84311      }else{
84312        Expr *pNext  = p->pRight;
84313        /* The Expr.x union is never used at the same time as Expr.pRight */
84314        assert( p->x.pList==0 || p->pRight==0 );
84315        /* p->flags holds EP_Collate and p->pLeft->flags does not.  And
84316        ** p->x.pSelect cannot.  So if p->x.pLeft exists, it must hold at
84317        ** least one EP_Collate. Thus the following two ALWAYS. */
84318        if( p->x.pList!=0 && ALWAYS(!ExprHasProperty(p, EP_xIsSelect)) ){
84319          int i;
84320          for(i=0; ALWAYS(i<p->x.pList->nExpr); i++){
84321            if( ExprHasProperty(p->x.pList->a[i].pExpr, EP_Collate) ){
84322              pNext = p->x.pList->a[i].pExpr;
84323              break;
84324            }
84325          }
84326        }
84327        p = pNext;
84328      }
84329    }else{
84330      break;
84331    }
84332  }
84333  if( sqlite3CheckCollSeq(pParse, pColl) ){
84334    pColl = 0;
84335  }
84336  return pColl;
84337}
84338
84339/*
84340** pExpr is an operand of a comparison operator.  aff2 is the
84341** type affinity of the other operand.  This routine returns the
84342** type affinity that should be used for the comparison operator.
84343*/
84344SQLITE_PRIVATE char sqlite3CompareAffinity(Expr *pExpr, char aff2){
84345  char aff1 = sqlite3ExprAffinity(pExpr);
84346  if( aff1 && aff2 ){
84347    /* Both sides of the comparison are columns. If one has numeric
84348    ** affinity, use that. Otherwise use no affinity.
84349    */
84350    if( sqlite3IsNumericAffinity(aff1) || sqlite3IsNumericAffinity(aff2) ){
84351      return SQLITE_AFF_NUMERIC;
84352    }else{
84353      return SQLITE_AFF_BLOB;
84354    }
84355  }else if( !aff1 && !aff2 ){
84356    /* Neither side of the comparison is a column.  Compare the
84357    ** results directly.
84358    */
84359    return SQLITE_AFF_BLOB;
84360  }else{
84361    /* One side is a column, the other is not. Use the columns affinity. */
84362    assert( aff1==0 || aff2==0 );
84363    return (aff1 + aff2);
84364  }
84365}
84366
84367/*
84368** pExpr is a comparison operator.  Return the type affinity that should
84369** be applied to both operands prior to doing the comparison.
84370*/
84371static char comparisonAffinity(Expr *pExpr){
84372  char aff;
84373  assert( pExpr->op==TK_EQ || pExpr->op==TK_IN || pExpr->op==TK_LT ||
84374          pExpr->op==TK_GT || pExpr->op==TK_GE || pExpr->op==TK_LE ||
84375          pExpr->op==TK_NE || pExpr->op==TK_IS || pExpr->op==TK_ISNOT );
84376  assert( pExpr->pLeft );
84377  aff = sqlite3ExprAffinity(pExpr->pLeft);
84378  if( pExpr->pRight ){
84379    aff = sqlite3CompareAffinity(pExpr->pRight, aff);
84380  }else if( ExprHasProperty(pExpr, EP_xIsSelect) ){
84381    aff = sqlite3CompareAffinity(pExpr->x.pSelect->pEList->a[0].pExpr, aff);
84382  }else if( !aff ){
84383    aff = SQLITE_AFF_BLOB;
84384  }
84385  return aff;
84386}
84387
84388/*
84389** pExpr is a comparison expression, eg. '=', '<', IN(...) etc.
84390** idx_affinity is the affinity of an indexed column. Return true
84391** if the index with affinity idx_affinity may be used to implement
84392** the comparison in pExpr.
84393*/
84394SQLITE_PRIVATE int sqlite3IndexAffinityOk(Expr *pExpr, char idx_affinity){
84395  char aff = comparisonAffinity(pExpr);
84396  switch( aff ){
84397    case SQLITE_AFF_BLOB:
84398      return 1;
84399    case SQLITE_AFF_TEXT:
84400      return idx_affinity==SQLITE_AFF_TEXT;
84401    default:
84402      return sqlite3IsNumericAffinity(idx_affinity);
84403  }
84404}
84405
84406/*
84407** Return the P5 value that should be used for a binary comparison
84408** opcode (OP_Eq, OP_Ge etc.) used to compare pExpr1 and pExpr2.
84409*/
84410static u8 binaryCompareP5(Expr *pExpr1, Expr *pExpr2, int jumpIfNull){
84411  u8 aff = (char)sqlite3ExprAffinity(pExpr2);
84412  aff = (u8)sqlite3CompareAffinity(pExpr1, aff) | (u8)jumpIfNull;
84413  return aff;
84414}
84415
84416/*
84417** Return a pointer to the collation sequence that should be used by
84418** a binary comparison operator comparing pLeft and pRight.
84419**
84420** If the left hand expression has a collating sequence type, then it is
84421** used. Otherwise the collation sequence for the right hand expression
84422** is used, or the default (BINARY) if neither expression has a collating
84423** type.
84424**
84425** Argument pRight (but not pLeft) may be a null pointer. In this case,
84426** it is not considered.
84427*/
84428SQLITE_PRIVATE CollSeq *sqlite3BinaryCompareCollSeq(
84429  Parse *pParse,
84430  Expr *pLeft,
84431  Expr *pRight
84432){
84433  CollSeq *pColl;
84434  assert( pLeft );
84435  if( pLeft->flags & EP_Collate ){
84436    pColl = sqlite3ExprCollSeq(pParse, pLeft);
84437  }else if( pRight && (pRight->flags & EP_Collate)!=0 ){
84438    pColl = sqlite3ExprCollSeq(pParse, pRight);
84439  }else{
84440    pColl = sqlite3ExprCollSeq(pParse, pLeft);
84441    if( !pColl ){
84442      pColl = sqlite3ExprCollSeq(pParse, pRight);
84443    }
84444  }
84445  return pColl;
84446}
84447
84448/*
84449** Generate code for a comparison operator.
84450*/
84451static int codeCompare(
84452  Parse *pParse,    /* The parsing (and code generating) context */
84453  Expr *pLeft,      /* The left operand */
84454  Expr *pRight,     /* The right operand */
84455  int opcode,       /* The comparison opcode */
84456  int in1, int in2, /* Register holding operands */
84457  int dest,         /* Jump here if true.  */
84458  int jumpIfNull    /* If true, jump if either operand is NULL */
84459){
84460  int p5;
84461  int addr;
84462  CollSeq *p4;
84463
84464  p4 = sqlite3BinaryCompareCollSeq(pParse, pLeft, pRight);
84465  p5 = binaryCompareP5(pLeft, pRight, jumpIfNull);
84466  addr = sqlite3VdbeAddOp4(pParse->pVdbe, opcode, in2, dest, in1,
84467                           (void*)p4, P4_COLLSEQ);
84468  sqlite3VdbeChangeP5(pParse->pVdbe, (u8)p5);
84469  return addr;
84470}
84471
84472#if SQLITE_MAX_EXPR_DEPTH>0
84473/*
84474** Check that argument nHeight is less than or equal to the maximum
84475** expression depth allowed. If it is not, leave an error message in
84476** pParse.
84477*/
84478SQLITE_PRIVATE int sqlite3ExprCheckHeight(Parse *pParse, int nHeight){
84479  int rc = SQLITE_OK;
84480  int mxHeight = pParse->db->aLimit[SQLITE_LIMIT_EXPR_DEPTH];
84481  if( nHeight>mxHeight ){
84482    sqlite3ErrorMsg(pParse,
84483       "Expression tree is too large (maximum depth %d)", mxHeight
84484    );
84485    rc = SQLITE_ERROR;
84486  }
84487  return rc;
84488}
84489
84490/* The following three functions, heightOfExpr(), heightOfExprList()
84491** and heightOfSelect(), are used to determine the maximum height
84492** of any expression tree referenced by the structure passed as the
84493** first argument.
84494**
84495** If this maximum height is greater than the current value pointed
84496** to by pnHeight, the second parameter, then set *pnHeight to that
84497** value.
84498*/
84499static void heightOfExpr(Expr *p, int *pnHeight){
84500  if( p ){
84501    if( p->nHeight>*pnHeight ){
84502      *pnHeight = p->nHeight;
84503    }
84504  }
84505}
84506static void heightOfExprList(ExprList *p, int *pnHeight){
84507  if( p ){
84508    int i;
84509    for(i=0; i<p->nExpr; i++){
84510      heightOfExpr(p->a[i].pExpr, pnHeight);
84511    }
84512  }
84513}
84514static void heightOfSelect(Select *p, int *pnHeight){
84515  if( p ){
84516    heightOfExpr(p->pWhere, pnHeight);
84517    heightOfExpr(p->pHaving, pnHeight);
84518    heightOfExpr(p->pLimit, pnHeight);
84519    heightOfExpr(p->pOffset, pnHeight);
84520    heightOfExprList(p->pEList, pnHeight);
84521    heightOfExprList(p->pGroupBy, pnHeight);
84522    heightOfExprList(p->pOrderBy, pnHeight);
84523    heightOfSelect(p->pPrior, pnHeight);
84524  }
84525}
84526
84527/*
84528** Set the Expr.nHeight variable in the structure passed as an
84529** argument. An expression with no children, Expr.pList or
84530** Expr.pSelect member has a height of 1. Any other expression
84531** has a height equal to the maximum height of any other
84532** referenced Expr plus one.
84533**
84534** Also propagate EP_Propagate flags up from Expr.x.pList to Expr.flags,
84535** if appropriate.
84536*/
84537static void exprSetHeight(Expr *p){
84538  int nHeight = 0;
84539  heightOfExpr(p->pLeft, &nHeight);
84540  heightOfExpr(p->pRight, &nHeight);
84541  if( ExprHasProperty(p, EP_xIsSelect) ){
84542    heightOfSelect(p->x.pSelect, &nHeight);
84543  }else if( p->x.pList ){
84544    heightOfExprList(p->x.pList, &nHeight);
84545    p->flags |= EP_Propagate & sqlite3ExprListFlags(p->x.pList);
84546  }
84547  p->nHeight = nHeight + 1;
84548}
84549
84550/*
84551** Set the Expr.nHeight variable using the exprSetHeight() function. If
84552** the height is greater than the maximum allowed expression depth,
84553** leave an error in pParse.
84554**
84555** Also propagate all EP_Propagate flags from the Expr.x.pList into
84556** Expr.flags.
84557*/
84558SQLITE_PRIVATE void sqlite3ExprSetHeightAndFlags(Parse *pParse, Expr *p){
84559  if( pParse->nErr ) return;
84560  exprSetHeight(p);
84561  sqlite3ExprCheckHeight(pParse, p->nHeight);
84562}
84563
84564/*
84565** Return the maximum height of any expression tree referenced
84566** by the select statement passed as an argument.
84567*/
84568SQLITE_PRIVATE int sqlite3SelectExprHeight(Select *p){
84569  int nHeight = 0;
84570  heightOfSelect(p, &nHeight);
84571  return nHeight;
84572}
84573#else /* ABOVE:  Height enforcement enabled.  BELOW: Height enforcement off */
84574/*
84575** Propagate all EP_Propagate flags from the Expr.x.pList into
84576** Expr.flags.
84577*/
84578SQLITE_PRIVATE void sqlite3ExprSetHeightAndFlags(Parse *pParse, Expr *p){
84579  if( p && p->x.pList && !ExprHasProperty(p, EP_xIsSelect) ){
84580    p->flags |= EP_Propagate & sqlite3ExprListFlags(p->x.pList);
84581  }
84582}
84583#define exprSetHeight(y)
84584#endif /* SQLITE_MAX_EXPR_DEPTH>0 */
84585
84586/*
84587** This routine is the core allocator for Expr nodes.
84588**
84589** Construct a new expression node and return a pointer to it.  Memory
84590** for this node and for the pToken argument is a single allocation
84591** obtained from sqlite3DbMalloc().  The calling function
84592** is responsible for making sure the node eventually gets freed.
84593**
84594** If dequote is true, then the token (if it exists) is dequoted.
84595** If dequote is false, no dequoting is performance.  The deQuote
84596** parameter is ignored if pToken is NULL or if the token does not
84597** appear to be quoted.  If the quotes were of the form "..." (double-quotes)
84598** then the EP_DblQuoted flag is set on the expression node.
84599**
84600** Special case:  If op==TK_INTEGER and pToken points to a string that
84601** can be translated into a 32-bit integer, then the token is not
84602** stored in u.zToken.  Instead, the integer values is written
84603** into u.iValue and the EP_IntValue flag is set.  No extra storage
84604** is allocated to hold the integer text and the dequote flag is ignored.
84605*/
84606SQLITE_PRIVATE Expr *sqlite3ExprAlloc(
84607  sqlite3 *db,            /* Handle for sqlite3DbMallocZero() (may be null) */
84608  int op,                 /* Expression opcode */
84609  const Token *pToken,    /* Token argument.  Might be NULL */
84610  int dequote             /* True to dequote */
84611){
84612  Expr *pNew;
84613  int nExtra = 0;
84614  int iValue = 0;
84615
84616  if( pToken ){
84617    if( op!=TK_INTEGER || pToken->z==0
84618          || sqlite3GetInt32(pToken->z, &iValue)==0 ){
84619      nExtra = pToken->n+1;
84620      assert( iValue>=0 );
84621    }
84622  }
84623  pNew = sqlite3DbMallocZero(db, sizeof(Expr)+nExtra);
84624  if( pNew ){
84625    pNew->op = (u8)op;
84626    pNew->iAgg = -1;
84627    if( pToken ){
84628      if( nExtra==0 ){
84629        pNew->flags |= EP_IntValue;
84630        pNew->u.iValue = iValue;
84631      }else{
84632        int c;
84633        pNew->u.zToken = (char*)&pNew[1];
84634        assert( pToken->z!=0 || pToken->n==0 );
84635        if( pToken->n ) memcpy(pNew->u.zToken, pToken->z, pToken->n);
84636        pNew->u.zToken[pToken->n] = 0;
84637        if( dequote && nExtra>=3
84638             && ((c = pToken->z[0])=='\'' || c=='"' || c=='[' || c=='`') ){
84639          sqlite3Dequote(pNew->u.zToken);
84640          if( c=='"' ) pNew->flags |= EP_DblQuoted;
84641        }
84642      }
84643    }
84644#if SQLITE_MAX_EXPR_DEPTH>0
84645    pNew->nHeight = 1;
84646#endif
84647  }
84648  return pNew;
84649}
84650
84651/*
84652** Allocate a new expression node from a zero-terminated token that has
84653** already been dequoted.
84654*/
84655SQLITE_PRIVATE Expr *sqlite3Expr(
84656  sqlite3 *db,            /* Handle for sqlite3DbMallocZero() (may be null) */
84657  int op,                 /* Expression opcode */
84658  const char *zToken      /* Token argument.  Might be NULL */
84659){
84660  Token x;
84661  x.z = zToken;
84662  x.n = zToken ? sqlite3Strlen30(zToken) : 0;
84663  return sqlite3ExprAlloc(db, op, &x, 0);
84664}
84665
84666/*
84667** Attach subtrees pLeft and pRight to the Expr node pRoot.
84668**
84669** If pRoot==NULL that means that a memory allocation error has occurred.
84670** In that case, delete the subtrees pLeft and pRight.
84671*/
84672SQLITE_PRIVATE void sqlite3ExprAttachSubtrees(
84673  sqlite3 *db,
84674  Expr *pRoot,
84675  Expr *pLeft,
84676  Expr *pRight
84677){
84678  if( pRoot==0 ){
84679    assert( db->mallocFailed );
84680    sqlite3ExprDelete(db, pLeft);
84681    sqlite3ExprDelete(db, pRight);
84682  }else{
84683    if( pRight ){
84684      pRoot->pRight = pRight;
84685      pRoot->flags |= EP_Propagate & pRight->flags;
84686    }
84687    if( pLeft ){
84688      pRoot->pLeft = pLeft;
84689      pRoot->flags |= EP_Propagate & pLeft->flags;
84690    }
84691    exprSetHeight(pRoot);
84692  }
84693}
84694
84695/*
84696** Allocate an Expr node which joins as many as two subtrees.
84697**
84698** One or both of the subtrees can be NULL.  Return a pointer to the new
84699** Expr node.  Or, if an OOM error occurs, set pParse->db->mallocFailed,
84700** free the subtrees and return NULL.
84701*/
84702SQLITE_PRIVATE Expr *sqlite3PExpr(
84703  Parse *pParse,          /* Parsing context */
84704  int op,                 /* Expression opcode */
84705  Expr *pLeft,            /* Left operand */
84706  Expr *pRight,           /* Right operand */
84707  const Token *pToken     /* Argument token */
84708){
84709  Expr *p;
84710  if( op==TK_AND && pLeft && pRight && pParse->nErr==0 ){
84711    /* Take advantage of short-circuit false optimization for AND */
84712    p = sqlite3ExprAnd(pParse->db, pLeft, pRight);
84713  }else{
84714    p = sqlite3ExprAlloc(pParse->db, op, pToken, 1);
84715    sqlite3ExprAttachSubtrees(pParse->db, p, pLeft, pRight);
84716  }
84717  if( p ) {
84718    sqlite3ExprCheckHeight(pParse, p->nHeight);
84719  }
84720  return p;
84721}
84722
84723/*
84724** If the expression is always either TRUE or FALSE (respectively),
84725** then return 1.  If one cannot determine the truth value of the
84726** expression at compile-time return 0.
84727**
84728** This is an optimization.  If is OK to return 0 here even if
84729** the expression really is always false or false (a false negative).
84730** But it is a bug to return 1 if the expression might have different
84731** boolean values in different circumstances (a false positive.)
84732**
84733** Note that if the expression is part of conditional for a
84734** LEFT JOIN, then we cannot determine at compile-time whether or not
84735** is it true or false, so always return 0.
84736*/
84737static int exprAlwaysTrue(Expr *p){
84738  int v = 0;
84739  if( ExprHasProperty(p, EP_FromJoin) ) return 0;
84740  if( !sqlite3ExprIsInteger(p, &v) ) return 0;
84741  return v!=0;
84742}
84743static int exprAlwaysFalse(Expr *p){
84744  int v = 0;
84745  if( ExprHasProperty(p, EP_FromJoin) ) return 0;
84746  if( !sqlite3ExprIsInteger(p, &v) ) return 0;
84747  return v==0;
84748}
84749
84750/*
84751** Join two expressions using an AND operator.  If either expression is
84752** NULL, then just return the other expression.
84753**
84754** If one side or the other of the AND is known to be false, then instead
84755** of returning an AND expression, just return a constant expression with
84756** a value of false.
84757*/
84758SQLITE_PRIVATE Expr *sqlite3ExprAnd(sqlite3 *db, Expr *pLeft, Expr *pRight){
84759  if( pLeft==0 ){
84760    return pRight;
84761  }else if( pRight==0 ){
84762    return pLeft;
84763  }else if( exprAlwaysFalse(pLeft) || exprAlwaysFalse(pRight) ){
84764    sqlite3ExprDelete(db, pLeft);
84765    sqlite3ExprDelete(db, pRight);
84766    return sqlite3ExprAlloc(db, TK_INTEGER, &sqlite3IntTokens[0], 0);
84767  }else{
84768    Expr *pNew = sqlite3ExprAlloc(db, TK_AND, 0, 0);
84769    sqlite3ExprAttachSubtrees(db, pNew, pLeft, pRight);
84770    return pNew;
84771  }
84772}
84773
84774/*
84775** Construct a new expression node for a function with multiple
84776** arguments.
84777*/
84778SQLITE_PRIVATE Expr *sqlite3ExprFunction(Parse *pParse, ExprList *pList, Token *pToken){
84779  Expr *pNew;
84780  sqlite3 *db = pParse->db;
84781  assert( pToken );
84782  pNew = sqlite3ExprAlloc(db, TK_FUNCTION, pToken, 1);
84783  if( pNew==0 ){
84784    sqlite3ExprListDelete(db, pList); /* Avoid memory leak when malloc fails */
84785    return 0;
84786  }
84787  pNew->x.pList = pList;
84788  assert( !ExprHasProperty(pNew, EP_xIsSelect) );
84789  sqlite3ExprSetHeightAndFlags(pParse, pNew);
84790  return pNew;
84791}
84792
84793/*
84794** Assign a variable number to an expression that encodes a wildcard
84795** in the original SQL statement.
84796**
84797** Wildcards consisting of a single "?" are assigned the next sequential
84798** variable number.
84799**
84800** Wildcards of the form "?nnn" are assigned the number "nnn".  We make
84801** sure "nnn" is not too be to avoid a denial of service attack when
84802** the SQL statement comes from an external source.
84803**
84804** Wildcards of the form ":aaa", "@aaa", or "$aaa" are assigned the same number
84805** as the previous instance of the same wildcard.  Or if this is the first
84806** instance of the wildcard, the next sequential variable number is
84807** assigned.
84808*/
84809SQLITE_PRIVATE void sqlite3ExprAssignVarNumber(Parse *pParse, Expr *pExpr){
84810  sqlite3 *db = pParse->db;
84811  const char *z;
84812
84813  if( pExpr==0 ) return;
84814  assert( !ExprHasProperty(pExpr, EP_IntValue|EP_Reduced|EP_TokenOnly) );
84815  z = pExpr->u.zToken;
84816  assert( z!=0 );
84817  assert( z[0]!=0 );
84818  if( z[1]==0 ){
84819    /* Wildcard of the form "?".  Assign the next variable number */
84820    assert( z[0]=='?' );
84821    pExpr->iColumn = (ynVar)(++pParse->nVar);
84822  }else{
84823    ynVar x = 0;
84824    u32 n = sqlite3Strlen30(z);
84825    if( z[0]=='?' ){
84826      /* Wildcard of the form "?nnn".  Convert "nnn" to an integer and
84827      ** use it as the variable number */
84828      i64 i;
84829      int bOk = 0==sqlite3Atoi64(&z[1], &i, n-1, SQLITE_UTF8);
84830      pExpr->iColumn = x = (ynVar)i;
84831      testcase( i==0 );
84832      testcase( i==1 );
84833      testcase( i==db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER]-1 );
84834      testcase( i==db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER] );
84835      if( bOk==0 || i<1 || i>db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER] ){
84836        sqlite3ErrorMsg(pParse, "variable number must be between ?1 and ?%d",
84837            db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER]);
84838        x = 0;
84839      }
84840      if( i>pParse->nVar ){
84841        pParse->nVar = (int)i;
84842      }
84843    }else{
84844      /* Wildcards like ":aaa", "$aaa" or "@aaa".  Reuse the same variable
84845      ** number as the prior appearance of the same name, or if the name
84846      ** has never appeared before, reuse the same variable number
84847      */
84848      ynVar i;
84849      for(i=0; i<pParse->nzVar; i++){
84850        if( pParse->azVar[i] && strcmp(pParse->azVar[i],z)==0 ){
84851          pExpr->iColumn = x = (ynVar)i+1;
84852          break;
84853        }
84854      }
84855      if( x==0 ) x = pExpr->iColumn = (ynVar)(++pParse->nVar);
84856    }
84857    if( x>0 ){
84858      if( x>pParse->nzVar ){
84859        char **a;
84860        a = sqlite3DbRealloc(db, pParse->azVar, x*sizeof(a[0]));
84861        if( a==0 ) return;  /* Error reported through db->mallocFailed */
84862        pParse->azVar = a;
84863        memset(&a[pParse->nzVar], 0, (x-pParse->nzVar)*sizeof(a[0]));
84864        pParse->nzVar = x;
84865      }
84866      if( z[0]!='?' || pParse->azVar[x-1]==0 ){
84867        sqlite3DbFree(db, pParse->azVar[x-1]);
84868        pParse->azVar[x-1] = sqlite3DbStrNDup(db, z, n);
84869      }
84870    }
84871  }
84872  if( !pParse->nErr && pParse->nVar>db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER] ){
84873    sqlite3ErrorMsg(pParse, "too many SQL variables");
84874  }
84875}
84876
84877/*
84878** Recursively delete an expression tree.
84879*/
84880SQLITE_PRIVATE void sqlite3ExprDelete(sqlite3 *db, Expr *p){
84881  if( p==0 ) return;
84882  /* Sanity check: Assert that the IntValue is non-negative if it exists */
84883  assert( !ExprHasProperty(p, EP_IntValue) || p->u.iValue>=0 );
84884  if( !ExprHasProperty(p, EP_TokenOnly) ){
84885    /* The Expr.x union is never used at the same time as Expr.pRight */
84886    assert( p->x.pList==0 || p->pRight==0 );
84887    sqlite3ExprDelete(db, p->pLeft);
84888    sqlite3ExprDelete(db, p->pRight);
84889    if( ExprHasProperty(p, EP_MemToken) ) sqlite3DbFree(db, p->u.zToken);
84890    if( ExprHasProperty(p, EP_xIsSelect) ){
84891      sqlite3SelectDelete(db, p->x.pSelect);
84892    }else{
84893      sqlite3ExprListDelete(db, p->x.pList);
84894    }
84895  }
84896  if( !ExprHasProperty(p, EP_Static) ){
84897    sqlite3DbFree(db, p);
84898  }
84899}
84900
84901/*
84902** Return the number of bytes allocated for the expression structure
84903** passed as the first argument. This is always one of EXPR_FULLSIZE,
84904** EXPR_REDUCEDSIZE or EXPR_TOKENONLYSIZE.
84905*/
84906static int exprStructSize(Expr *p){
84907  if( ExprHasProperty(p, EP_TokenOnly) ) return EXPR_TOKENONLYSIZE;
84908  if( ExprHasProperty(p, EP_Reduced) ) return EXPR_REDUCEDSIZE;
84909  return EXPR_FULLSIZE;
84910}
84911
84912/*
84913** The dupedExpr*Size() routines each return the number of bytes required
84914** to store a copy of an expression or expression tree.  They differ in
84915** how much of the tree is measured.
84916**
84917**     dupedExprStructSize()     Size of only the Expr structure
84918**     dupedExprNodeSize()       Size of Expr + space for token
84919**     dupedExprSize()           Expr + token + subtree components
84920**
84921***************************************************************************
84922**
84923** The dupedExprStructSize() function returns two values OR-ed together:
84924** (1) the space required for a copy of the Expr structure only and
84925** (2) the EP_xxx flags that indicate what the structure size should be.
84926** The return values is always one of:
84927**
84928**      EXPR_FULLSIZE
84929**      EXPR_REDUCEDSIZE   | EP_Reduced
84930**      EXPR_TOKENONLYSIZE | EP_TokenOnly
84931**
84932** The size of the structure can be found by masking the return value
84933** of this routine with 0xfff.  The flags can be found by masking the
84934** return value with EP_Reduced|EP_TokenOnly.
84935**
84936** Note that with flags==EXPRDUP_REDUCE, this routines works on full-size
84937** (unreduced) Expr objects as they or originally constructed by the parser.
84938** During expression analysis, extra information is computed and moved into
84939** later parts of teh Expr object and that extra information might get chopped
84940** off if the expression is reduced.  Note also that it does not work to
84941** make an EXPRDUP_REDUCE copy of a reduced expression.  It is only legal
84942** to reduce a pristine expression tree from the parser.  The implementation
84943** of dupedExprStructSize() contain multiple assert() statements that attempt
84944** to enforce this constraint.
84945*/
84946static int dupedExprStructSize(Expr *p, int flags){
84947  int nSize;
84948  assert( flags==EXPRDUP_REDUCE || flags==0 ); /* Only one flag value allowed */
84949  assert( EXPR_FULLSIZE<=0xfff );
84950  assert( (0xfff & (EP_Reduced|EP_TokenOnly))==0 );
84951  if( 0==(flags&EXPRDUP_REDUCE) ){
84952    nSize = EXPR_FULLSIZE;
84953  }else{
84954    assert( !ExprHasProperty(p, EP_TokenOnly|EP_Reduced) );
84955    assert( !ExprHasProperty(p, EP_FromJoin) );
84956    assert( !ExprHasProperty(p, EP_MemToken) );
84957    assert( !ExprHasProperty(p, EP_NoReduce) );
84958    if( p->pLeft || p->x.pList ){
84959      nSize = EXPR_REDUCEDSIZE | EP_Reduced;
84960    }else{
84961      assert( p->pRight==0 );
84962      nSize = EXPR_TOKENONLYSIZE | EP_TokenOnly;
84963    }
84964  }
84965  return nSize;
84966}
84967
84968/*
84969** This function returns the space in bytes required to store the copy
84970** of the Expr structure and a copy of the Expr.u.zToken string (if that
84971** string is defined.)
84972*/
84973static int dupedExprNodeSize(Expr *p, int flags){
84974  int nByte = dupedExprStructSize(p, flags) & 0xfff;
84975  if( !ExprHasProperty(p, EP_IntValue) && p->u.zToken ){
84976    nByte += sqlite3Strlen30(p->u.zToken)+1;
84977  }
84978  return ROUND8(nByte);
84979}
84980
84981/*
84982** Return the number of bytes required to create a duplicate of the
84983** expression passed as the first argument. The second argument is a
84984** mask containing EXPRDUP_XXX flags.
84985**
84986** The value returned includes space to create a copy of the Expr struct
84987** itself and the buffer referred to by Expr.u.zToken, if any.
84988**
84989** If the EXPRDUP_REDUCE flag is set, then the return value includes
84990** space to duplicate all Expr nodes in the tree formed by Expr.pLeft
84991** and Expr.pRight variables (but not for any structures pointed to or
84992** descended from the Expr.x.pList or Expr.x.pSelect variables).
84993*/
84994static int dupedExprSize(Expr *p, int flags){
84995  int nByte = 0;
84996  if( p ){
84997    nByte = dupedExprNodeSize(p, flags);
84998    if( flags&EXPRDUP_REDUCE ){
84999      nByte += dupedExprSize(p->pLeft, flags) + dupedExprSize(p->pRight, flags);
85000    }
85001  }
85002  return nByte;
85003}
85004
85005/*
85006** This function is similar to sqlite3ExprDup(), except that if pzBuffer
85007** is not NULL then *pzBuffer is assumed to point to a buffer large enough
85008** to store the copy of expression p, the copies of p->u.zToken
85009** (if applicable), and the copies of the p->pLeft and p->pRight expressions,
85010** if any. Before returning, *pzBuffer is set to the first byte past the
85011** portion of the buffer copied into by this function.
85012*/
85013static Expr *exprDup(sqlite3 *db, Expr *p, int flags, u8 **pzBuffer){
85014  Expr *pNew = 0;                      /* Value to return */
85015  if( p ){
85016    const int isReduced = (flags&EXPRDUP_REDUCE);
85017    u8 *zAlloc;
85018    u32 staticFlag = 0;
85019
85020    assert( pzBuffer==0 || isReduced );
85021
85022    /* Figure out where to write the new Expr structure. */
85023    if( pzBuffer ){
85024      zAlloc = *pzBuffer;
85025      staticFlag = EP_Static;
85026    }else{
85027      zAlloc = sqlite3DbMallocRaw(db, dupedExprSize(p, flags));
85028    }
85029    pNew = (Expr *)zAlloc;
85030
85031    if( pNew ){
85032      /* Set nNewSize to the size allocated for the structure pointed to
85033      ** by pNew. This is either EXPR_FULLSIZE, EXPR_REDUCEDSIZE or
85034      ** EXPR_TOKENONLYSIZE. nToken is set to the number of bytes consumed
85035      ** by the copy of the p->u.zToken string (if any).
85036      */
85037      const unsigned nStructSize = dupedExprStructSize(p, flags);
85038      const int nNewSize = nStructSize & 0xfff;
85039      int nToken;
85040      if( !ExprHasProperty(p, EP_IntValue) && p->u.zToken ){
85041        nToken = sqlite3Strlen30(p->u.zToken) + 1;
85042      }else{
85043        nToken = 0;
85044      }
85045      if( isReduced ){
85046        assert( ExprHasProperty(p, EP_Reduced)==0 );
85047        memcpy(zAlloc, p, nNewSize);
85048      }else{
85049        int nSize = exprStructSize(p);
85050        memcpy(zAlloc, p, nSize);
85051        memset(&zAlloc[nSize], 0, EXPR_FULLSIZE-nSize);
85052      }
85053
85054      /* Set the EP_Reduced, EP_TokenOnly, and EP_Static flags appropriately. */
85055      pNew->flags &= ~(EP_Reduced|EP_TokenOnly|EP_Static|EP_MemToken);
85056      pNew->flags |= nStructSize & (EP_Reduced|EP_TokenOnly);
85057      pNew->flags |= staticFlag;
85058
85059      /* Copy the p->u.zToken string, if any. */
85060      if( nToken ){
85061        char *zToken = pNew->u.zToken = (char*)&zAlloc[nNewSize];
85062        memcpy(zToken, p->u.zToken, nToken);
85063      }
85064
85065      if( 0==((p->flags|pNew->flags) & EP_TokenOnly) ){
85066        /* Fill in the pNew->x.pSelect or pNew->x.pList member. */
85067        if( ExprHasProperty(p, EP_xIsSelect) ){
85068          pNew->x.pSelect = sqlite3SelectDup(db, p->x.pSelect, isReduced);
85069        }else{
85070          pNew->x.pList = sqlite3ExprListDup(db, p->x.pList, isReduced);
85071        }
85072      }
85073
85074      /* Fill in pNew->pLeft and pNew->pRight. */
85075      if( ExprHasProperty(pNew, EP_Reduced|EP_TokenOnly) ){
85076        zAlloc += dupedExprNodeSize(p, flags);
85077        if( ExprHasProperty(pNew, EP_Reduced) ){
85078          pNew->pLeft = exprDup(db, p->pLeft, EXPRDUP_REDUCE, &zAlloc);
85079          pNew->pRight = exprDup(db, p->pRight, EXPRDUP_REDUCE, &zAlloc);
85080        }
85081        if( pzBuffer ){
85082          *pzBuffer = zAlloc;
85083        }
85084      }else{
85085        if( !ExprHasProperty(p, EP_TokenOnly) ){
85086          pNew->pLeft = sqlite3ExprDup(db, p->pLeft, 0);
85087          pNew->pRight = sqlite3ExprDup(db, p->pRight, 0);
85088        }
85089      }
85090
85091    }
85092  }
85093  return pNew;
85094}
85095
85096/*
85097** Create and return a deep copy of the object passed as the second
85098** argument. If an OOM condition is encountered, NULL is returned
85099** and the db->mallocFailed flag set.
85100*/
85101#ifndef SQLITE_OMIT_CTE
85102static With *withDup(sqlite3 *db, With *p){
85103  With *pRet = 0;
85104  if( p ){
85105    int nByte = sizeof(*p) + sizeof(p->a[0]) * (p->nCte-1);
85106    pRet = sqlite3DbMallocZero(db, nByte);
85107    if( pRet ){
85108      int i;
85109      pRet->nCte = p->nCte;
85110      for(i=0; i<p->nCte; i++){
85111        pRet->a[i].pSelect = sqlite3SelectDup(db, p->a[i].pSelect, 0);
85112        pRet->a[i].pCols = sqlite3ExprListDup(db, p->a[i].pCols, 0);
85113        pRet->a[i].zName = sqlite3DbStrDup(db, p->a[i].zName);
85114      }
85115    }
85116  }
85117  return pRet;
85118}
85119#else
85120# define withDup(x,y) 0
85121#endif
85122
85123/*
85124** The following group of routines make deep copies of expressions,
85125** expression lists, ID lists, and select statements.  The copies can
85126** be deleted (by being passed to their respective ...Delete() routines)
85127** without effecting the originals.
85128**
85129** The expression list, ID, and source lists return by sqlite3ExprListDup(),
85130** sqlite3IdListDup(), and sqlite3SrcListDup() can not be further expanded
85131** by subsequent calls to sqlite*ListAppend() routines.
85132**
85133** Any tables that the SrcList might point to are not duplicated.
85134**
85135** The flags parameter contains a combination of the EXPRDUP_XXX flags.
85136** If the EXPRDUP_REDUCE flag is set, then the structure returned is a
85137** truncated version of the usual Expr structure that will be stored as
85138** part of the in-memory representation of the database schema.
85139*/
85140SQLITE_PRIVATE Expr *sqlite3ExprDup(sqlite3 *db, Expr *p, int flags){
85141  return exprDup(db, p, flags, 0);
85142}
85143SQLITE_PRIVATE ExprList *sqlite3ExprListDup(sqlite3 *db, ExprList *p, int flags){
85144  ExprList *pNew;
85145  struct ExprList_item *pItem, *pOldItem;
85146  int i;
85147  if( p==0 ) return 0;
85148  pNew = sqlite3DbMallocRaw(db, sizeof(*pNew) );
85149  if( pNew==0 ) return 0;
85150  pNew->nExpr = i = p->nExpr;
85151  if( (flags & EXPRDUP_REDUCE)==0 ) for(i=1; i<p->nExpr; i+=i){}
85152  pNew->a = pItem = sqlite3DbMallocRaw(db,  i*sizeof(p->a[0]) );
85153  if( pItem==0 ){
85154    sqlite3DbFree(db, pNew);
85155    return 0;
85156  }
85157  pOldItem = p->a;
85158  for(i=0; i<p->nExpr; i++, pItem++, pOldItem++){
85159    Expr *pOldExpr = pOldItem->pExpr;
85160    pItem->pExpr = sqlite3ExprDup(db, pOldExpr, flags);
85161    pItem->zName = sqlite3DbStrDup(db, pOldItem->zName);
85162    pItem->zSpan = sqlite3DbStrDup(db, pOldItem->zSpan);
85163    pItem->sortOrder = pOldItem->sortOrder;
85164    pItem->done = 0;
85165    pItem->bSpanIsTab = pOldItem->bSpanIsTab;
85166    pItem->u = pOldItem->u;
85167  }
85168  return pNew;
85169}
85170
85171/*
85172** If cursors, triggers, views and subqueries are all omitted from
85173** the build, then none of the following routines, except for
85174** sqlite3SelectDup(), can be called. sqlite3SelectDup() is sometimes
85175** called with a NULL argument.
85176*/
85177#if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_TRIGGER) \
85178 || !defined(SQLITE_OMIT_SUBQUERY)
85179SQLITE_PRIVATE SrcList *sqlite3SrcListDup(sqlite3 *db, SrcList *p, int flags){
85180  SrcList *pNew;
85181  int i;
85182  int nByte;
85183  if( p==0 ) return 0;
85184  nByte = sizeof(*p) + (p->nSrc>0 ? sizeof(p->a[0]) * (p->nSrc-1) : 0);
85185  pNew = sqlite3DbMallocRaw(db, nByte );
85186  if( pNew==0 ) return 0;
85187  pNew->nSrc = pNew->nAlloc = p->nSrc;
85188  for(i=0; i<p->nSrc; i++){
85189    struct SrcList_item *pNewItem = &pNew->a[i];
85190    struct SrcList_item *pOldItem = &p->a[i];
85191    Table *pTab;
85192    pNewItem->pSchema = pOldItem->pSchema;
85193    pNewItem->zDatabase = sqlite3DbStrDup(db, pOldItem->zDatabase);
85194    pNewItem->zName = sqlite3DbStrDup(db, pOldItem->zName);
85195    pNewItem->zAlias = sqlite3DbStrDup(db, pOldItem->zAlias);
85196    pNewItem->jointype = pOldItem->jointype;
85197    pNewItem->iCursor = pOldItem->iCursor;
85198    pNewItem->addrFillSub = pOldItem->addrFillSub;
85199    pNewItem->regReturn = pOldItem->regReturn;
85200    pNewItem->isCorrelated = pOldItem->isCorrelated;
85201    pNewItem->viaCoroutine = pOldItem->viaCoroutine;
85202    pNewItem->isRecursive = pOldItem->isRecursive;
85203    pNewItem->zIndexedBy = sqlite3DbStrDup(db, pOldItem->zIndexedBy);
85204    pNewItem->notIndexed = pOldItem->notIndexed;
85205    pNewItem->pIndex = pOldItem->pIndex;
85206    pTab = pNewItem->pTab = pOldItem->pTab;
85207    if( pTab ){
85208      pTab->nRef++;
85209    }
85210    pNewItem->pSelect = sqlite3SelectDup(db, pOldItem->pSelect, flags);
85211    pNewItem->pOn = sqlite3ExprDup(db, pOldItem->pOn, flags);
85212    pNewItem->pUsing = sqlite3IdListDup(db, pOldItem->pUsing);
85213    pNewItem->colUsed = pOldItem->colUsed;
85214  }
85215  return pNew;
85216}
85217SQLITE_PRIVATE IdList *sqlite3IdListDup(sqlite3 *db, IdList *p){
85218  IdList *pNew;
85219  int i;
85220  if( p==0 ) return 0;
85221  pNew = sqlite3DbMallocRaw(db, sizeof(*pNew) );
85222  if( pNew==0 ) return 0;
85223  pNew->nId = p->nId;
85224  pNew->a = sqlite3DbMallocRaw(db, p->nId*sizeof(p->a[0]) );
85225  if( pNew->a==0 ){
85226    sqlite3DbFree(db, pNew);
85227    return 0;
85228  }
85229  /* Note that because the size of the allocation for p->a[] is not
85230  ** necessarily a power of two, sqlite3IdListAppend() may not be called
85231  ** on the duplicate created by this function. */
85232  for(i=0; i<p->nId; i++){
85233    struct IdList_item *pNewItem = &pNew->a[i];
85234    struct IdList_item *pOldItem = &p->a[i];
85235    pNewItem->zName = sqlite3DbStrDup(db, pOldItem->zName);
85236    pNewItem->idx = pOldItem->idx;
85237  }
85238  return pNew;
85239}
85240SQLITE_PRIVATE Select *sqlite3SelectDup(sqlite3 *db, Select *p, int flags){
85241  Select *pNew, *pPrior;
85242  if( p==0 ) return 0;
85243  pNew = sqlite3DbMallocRaw(db, sizeof(*p) );
85244  if( pNew==0 ) return 0;
85245  pNew->pEList = sqlite3ExprListDup(db, p->pEList, flags);
85246  pNew->pSrc = sqlite3SrcListDup(db, p->pSrc, flags);
85247  pNew->pWhere = sqlite3ExprDup(db, p->pWhere, flags);
85248  pNew->pGroupBy = sqlite3ExprListDup(db, p->pGroupBy, flags);
85249  pNew->pHaving = sqlite3ExprDup(db, p->pHaving, flags);
85250  pNew->pOrderBy = sqlite3ExprListDup(db, p->pOrderBy, flags);
85251  pNew->op = p->op;
85252  pNew->pPrior = pPrior = sqlite3SelectDup(db, p->pPrior, flags);
85253  if( pPrior ) pPrior->pNext = pNew;
85254  pNew->pNext = 0;
85255  pNew->pLimit = sqlite3ExprDup(db, p->pLimit, flags);
85256  pNew->pOffset = sqlite3ExprDup(db, p->pOffset, flags);
85257  pNew->iLimit = 0;
85258  pNew->iOffset = 0;
85259  pNew->selFlags = p->selFlags & ~SF_UsesEphemeral;
85260  pNew->addrOpenEphm[0] = -1;
85261  pNew->addrOpenEphm[1] = -1;
85262  pNew->nSelectRow = p->nSelectRow;
85263  pNew->pWith = withDup(db, p->pWith);
85264  sqlite3SelectSetName(pNew, p->zSelName);
85265  return pNew;
85266}
85267#else
85268SQLITE_PRIVATE Select *sqlite3SelectDup(sqlite3 *db, Select *p, int flags){
85269  assert( p==0 );
85270  return 0;
85271}
85272#endif
85273
85274
85275/*
85276** Add a new element to the end of an expression list.  If pList is
85277** initially NULL, then create a new expression list.
85278**
85279** If a memory allocation error occurs, the entire list is freed and
85280** NULL is returned.  If non-NULL is returned, then it is guaranteed
85281** that the new entry was successfully appended.
85282*/
85283SQLITE_PRIVATE ExprList *sqlite3ExprListAppend(
85284  Parse *pParse,          /* Parsing context */
85285  ExprList *pList,        /* List to which to append. Might be NULL */
85286  Expr *pExpr             /* Expression to be appended. Might be NULL */
85287){
85288  sqlite3 *db = pParse->db;
85289  if( pList==0 ){
85290    pList = sqlite3DbMallocZero(db, sizeof(ExprList) );
85291    if( pList==0 ){
85292      goto no_mem;
85293    }
85294    pList->a = sqlite3DbMallocRaw(db, sizeof(pList->a[0]));
85295    if( pList->a==0 ) goto no_mem;
85296  }else if( (pList->nExpr & (pList->nExpr-1))==0 ){
85297    struct ExprList_item *a;
85298    assert( pList->nExpr>0 );
85299    a = sqlite3DbRealloc(db, pList->a, pList->nExpr*2*sizeof(pList->a[0]));
85300    if( a==0 ){
85301      goto no_mem;
85302    }
85303    pList->a = a;
85304  }
85305  assert( pList->a!=0 );
85306  if( 1 ){
85307    struct ExprList_item *pItem = &pList->a[pList->nExpr++];
85308    memset(pItem, 0, sizeof(*pItem));
85309    pItem->pExpr = pExpr;
85310  }
85311  return pList;
85312
85313no_mem:
85314  /* Avoid leaking memory if malloc has failed. */
85315  sqlite3ExprDelete(db, pExpr);
85316  sqlite3ExprListDelete(db, pList);
85317  return 0;
85318}
85319
85320/*
85321** Set the ExprList.a[].zName element of the most recently added item
85322** on the expression list.
85323**
85324** pList might be NULL following an OOM error.  But pName should never be
85325** NULL.  If a memory allocation fails, the pParse->db->mallocFailed flag
85326** is set.
85327*/
85328SQLITE_PRIVATE void sqlite3ExprListSetName(
85329  Parse *pParse,          /* Parsing context */
85330  ExprList *pList,        /* List to which to add the span. */
85331  Token *pName,           /* Name to be added */
85332  int dequote             /* True to cause the name to be dequoted */
85333){
85334  assert( pList!=0 || pParse->db->mallocFailed!=0 );
85335  if( pList ){
85336    struct ExprList_item *pItem;
85337    assert( pList->nExpr>0 );
85338    pItem = &pList->a[pList->nExpr-1];
85339    assert( pItem->zName==0 );
85340    pItem->zName = sqlite3DbStrNDup(pParse->db, pName->z, pName->n);
85341    if( dequote && pItem->zName ) sqlite3Dequote(pItem->zName);
85342  }
85343}
85344
85345/*
85346** Set the ExprList.a[].zSpan element of the most recently added item
85347** on the expression list.
85348**
85349** pList might be NULL following an OOM error.  But pSpan should never be
85350** NULL.  If a memory allocation fails, the pParse->db->mallocFailed flag
85351** is set.
85352*/
85353SQLITE_PRIVATE void sqlite3ExprListSetSpan(
85354  Parse *pParse,          /* Parsing context */
85355  ExprList *pList,        /* List to which to add the span. */
85356  ExprSpan *pSpan         /* The span to be added */
85357){
85358  sqlite3 *db = pParse->db;
85359  assert( pList!=0 || db->mallocFailed!=0 );
85360  if( pList ){
85361    struct ExprList_item *pItem = &pList->a[pList->nExpr-1];
85362    assert( pList->nExpr>0 );
85363    assert( db->mallocFailed || pItem->pExpr==pSpan->pExpr );
85364    sqlite3DbFree(db, pItem->zSpan);
85365    pItem->zSpan = sqlite3DbStrNDup(db, (char*)pSpan->zStart,
85366                                    (int)(pSpan->zEnd - pSpan->zStart));
85367  }
85368}
85369
85370/*
85371** If the expression list pEList contains more than iLimit elements,
85372** leave an error message in pParse.
85373*/
85374SQLITE_PRIVATE void sqlite3ExprListCheckLength(
85375  Parse *pParse,
85376  ExprList *pEList,
85377  const char *zObject
85378){
85379  int mx = pParse->db->aLimit[SQLITE_LIMIT_COLUMN];
85380  testcase( pEList && pEList->nExpr==mx );
85381  testcase( pEList && pEList->nExpr==mx+1 );
85382  if( pEList && pEList->nExpr>mx ){
85383    sqlite3ErrorMsg(pParse, "too many columns in %s", zObject);
85384  }
85385}
85386
85387/*
85388** Delete an entire expression list.
85389*/
85390SQLITE_PRIVATE void sqlite3ExprListDelete(sqlite3 *db, ExprList *pList){
85391  int i;
85392  struct ExprList_item *pItem;
85393  if( pList==0 ) return;
85394  assert( pList->a!=0 || pList->nExpr==0 );
85395  for(pItem=pList->a, i=0; i<pList->nExpr; i++, pItem++){
85396    sqlite3ExprDelete(db, pItem->pExpr);
85397    sqlite3DbFree(db, pItem->zName);
85398    sqlite3DbFree(db, pItem->zSpan);
85399  }
85400  sqlite3DbFree(db, pList->a);
85401  sqlite3DbFree(db, pList);
85402}
85403
85404/*
85405** Return the bitwise-OR of all Expr.flags fields in the given
85406** ExprList.
85407*/
85408SQLITE_PRIVATE u32 sqlite3ExprListFlags(const ExprList *pList){
85409  int i;
85410  u32 m = 0;
85411  if( pList ){
85412    for(i=0; i<pList->nExpr; i++){
85413       Expr *pExpr = pList->a[i].pExpr;
85414       if( ALWAYS(pExpr) ) m |= pExpr->flags;
85415    }
85416  }
85417  return m;
85418}
85419
85420/*
85421** These routines are Walker callbacks used to check expressions to
85422** see if they are "constant" for some definition of constant.  The
85423** Walker.eCode value determines the type of "constant" we are looking
85424** for.
85425**
85426** These callback routines are used to implement the following:
85427**
85428**     sqlite3ExprIsConstant()                  pWalker->eCode==1
85429**     sqlite3ExprIsConstantNotJoin()           pWalker->eCode==2
85430**     sqlite3ExprIsTableConstant()             pWalker->eCode==3
85431**     sqlite3ExprIsConstantOrFunction()        pWalker->eCode==4 or 5
85432**
85433** In all cases, the callbacks set Walker.eCode=0 and abort if the expression
85434** is found to not be a constant.
85435**
85436** The sqlite3ExprIsConstantOrFunction() is used for evaluating expressions
85437** in a CREATE TABLE statement.  The Walker.eCode value is 5 when parsing
85438** an existing schema and 4 when processing a new statement.  A bound
85439** parameter raises an error for new statements, but is silently converted
85440** to NULL for existing schemas.  This allows sqlite_master tables that
85441** contain a bound parameter because they were generated by older versions
85442** of SQLite to be parsed by newer versions of SQLite without raising a
85443** malformed schema error.
85444*/
85445static int exprNodeIsConstant(Walker *pWalker, Expr *pExpr){
85446
85447  /* If pWalker->eCode is 2 then any term of the expression that comes from
85448  ** the ON or USING clauses of a left join disqualifies the expression
85449  ** from being considered constant. */
85450  if( pWalker->eCode==2 && ExprHasProperty(pExpr, EP_FromJoin) ){
85451    pWalker->eCode = 0;
85452    return WRC_Abort;
85453  }
85454
85455  switch( pExpr->op ){
85456    /* Consider functions to be constant if all their arguments are constant
85457    ** and either pWalker->eCode==4 or 5 or the function has the
85458    ** SQLITE_FUNC_CONST flag. */
85459    case TK_FUNCTION:
85460      if( pWalker->eCode>=4 || ExprHasProperty(pExpr,EP_ConstFunc) ){
85461        return WRC_Continue;
85462      }else{
85463        pWalker->eCode = 0;
85464        return WRC_Abort;
85465      }
85466    case TK_ID:
85467    case TK_COLUMN:
85468    case TK_AGG_FUNCTION:
85469    case TK_AGG_COLUMN:
85470      testcase( pExpr->op==TK_ID );
85471      testcase( pExpr->op==TK_COLUMN );
85472      testcase( pExpr->op==TK_AGG_FUNCTION );
85473      testcase( pExpr->op==TK_AGG_COLUMN );
85474      if( pWalker->eCode==3 && pExpr->iTable==pWalker->u.iCur ){
85475        return WRC_Continue;
85476      }else{
85477        pWalker->eCode = 0;
85478        return WRC_Abort;
85479      }
85480    case TK_VARIABLE:
85481      if( pWalker->eCode==5 ){
85482        /* Silently convert bound parameters that appear inside of CREATE
85483        ** statements into a NULL when parsing the CREATE statement text out
85484        ** of the sqlite_master table */
85485        pExpr->op = TK_NULL;
85486      }else if( pWalker->eCode==4 ){
85487        /* A bound parameter in a CREATE statement that originates from
85488        ** sqlite3_prepare() causes an error */
85489        pWalker->eCode = 0;
85490        return WRC_Abort;
85491      }
85492      /* Fall through */
85493    default:
85494      testcase( pExpr->op==TK_SELECT ); /* selectNodeIsConstant will disallow */
85495      testcase( pExpr->op==TK_EXISTS ); /* selectNodeIsConstant will disallow */
85496      return WRC_Continue;
85497  }
85498}
85499static int selectNodeIsConstant(Walker *pWalker, Select *NotUsed){
85500  UNUSED_PARAMETER(NotUsed);
85501  pWalker->eCode = 0;
85502  return WRC_Abort;
85503}
85504static int exprIsConst(Expr *p, int initFlag, int iCur){
85505  Walker w;
85506  memset(&w, 0, sizeof(w));
85507  w.eCode = initFlag;
85508  w.xExprCallback = exprNodeIsConstant;
85509  w.xSelectCallback = selectNodeIsConstant;
85510  w.u.iCur = iCur;
85511  sqlite3WalkExpr(&w, p);
85512  return w.eCode;
85513}
85514
85515/*
85516** Walk an expression tree.  Return non-zero if the expression is constant
85517** and 0 if it involves variables or function calls.
85518**
85519** For the purposes of this function, a double-quoted string (ex: "abc")
85520** is considered a variable but a single-quoted string (ex: 'abc') is
85521** a constant.
85522*/
85523SQLITE_PRIVATE int sqlite3ExprIsConstant(Expr *p){
85524  return exprIsConst(p, 1, 0);
85525}
85526
85527/*
85528** Walk an expression tree.  Return non-zero if the expression is constant
85529** that does no originate from the ON or USING clauses of a join.
85530** Return 0 if it involves variables or function calls or terms from
85531** an ON or USING clause.
85532*/
85533SQLITE_PRIVATE int sqlite3ExprIsConstantNotJoin(Expr *p){
85534  return exprIsConst(p, 2, 0);
85535}
85536
85537/*
85538** Walk an expression tree.  Return non-zero if the expression is constant
85539** for any single row of the table with cursor iCur.  In other words, the
85540** expression must not refer to any non-deterministic function nor any
85541** table other than iCur.
85542*/
85543SQLITE_PRIVATE int sqlite3ExprIsTableConstant(Expr *p, int iCur){
85544  return exprIsConst(p, 3, iCur);
85545}
85546
85547/*
85548** Walk an expression tree.  Return non-zero if the expression is constant
85549** or a function call with constant arguments.  Return and 0 if there
85550** are any variables.
85551**
85552** For the purposes of this function, a double-quoted string (ex: "abc")
85553** is considered a variable but a single-quoted string (ex: 'abc') is
85554** a constant.
85555*/
85556SQLITE_PRIVATE int sqlite3ExprIsConstantOrFunction(Expr *p, u8 isInit){
85557  assert( isInit==0 || isInit==1 );
85558  return exprIsConst(p, 4+isInit, 0);
85559}
85560
85561/*
85562** If the expression p codes a constant integer that is small enough
85563** to fit in a 32-bit integer, return 1 and put the value of the integer
85564** in *pValue.  If the expression is not an integer or if it is too big
85565** to fit in a signed 32-bit integer, return 0 and leave *pValue unchanged.
85566*/
85567SQLITE_PRIVATE int sqlite3ExprIsInteger(Expr *p, int *pValue){
85568  int rc = 0;
85569
85570  /* If an expression is an integer literal that fits in a signed 32-bit
85571  ** integer, then the EP_IntValue flag will have already been set */
85572  assert( p->op!=TK_INTEGER || (p->flags & EP_IntValue)!=0
85573           || sqlite3GetInt32(p->u.zToken, &rc)==0 );
85574
85575  if( p->flags & EP_IntValue ){
85576    *pValue = p->u.iValue;
85577    return 1;
85578  }
85579  switch( p->op ){
85580    case TK_UPLUS: {
85581      rc = sqlite3ExprIsInteger(p->pLeft, pValue);
85582      break;
85583    }
85584    case TK_UMINUS: {
85585      int v;
85586      if( sqlite3ExprIsInteger(p->pLeft, &v) ){
85587        assert( v!=(-2147483647-1) );
85588        *pValue = -v;
85589        rc = 1;
85590      }
85591      break;
85592    }
85593    default: break;
85594  }
85595  return rc;
85596}
85597
85598/*
85599** Return FALSE if there is no chance that the expression can be NULL.
85600**
85601** If the expression might be NULL or if the expression is too complex
85602** to tell return TRUE.
85603**
85604** This routine is used as an optimization, to skip OP_IsNull opcodes
85605** when we know that a value cannot be NULL.  Hence, a false positive
85606** (returning TRUE when in fact the expression can never be NULL) might
85607** be a small performance hit but is otherwise harmless.  On the other
85608** hand, a false negative (returning FALSE when the result could be NULL)
85609** will likely result in an incorrect answer.  So when in doubt, return
85610** TRUE.
85611*/
85612SQLITE_PRIVATE int sqlite3ExprCanBeNull(const Expr *p){
85613  u8 op;
85614  while( p->op==TK_UPLUS || p->op==TK_UMINUS ){ p = p->pLeft; }
85615  op = p->op;
85616  if( op==TK_REGISTER ) op = p->op2;
85617  switch( op ){
85618    case TK_INTEGER:
85619    case TK_STRING:
85620    case TK_FLOAT:
85621    case TK_BLOB:
85622      return 0;
85623    case TK_COLUMN:
85624      assert( p->pTab!=0 );
85625      return ExprHasProperty(p, EP_CanBeNull) ||
85626             (p->iColumn>=0 && p->pTab->aCol[p->iColumn].notNull==0);
85627    default:
85628      return 1;
85629  }
85630}
85631
85632/*
85633** Return TRUE if the given expression is a constant which would be
85634** unchanged by OP_Affinity with the affinity given in the second
85635** argument.
85636**
85637** This routine is used to determine if the OP_Affinity operation
85638** can be omitted.  When in doubt return FALSE.  A false negative
85639** is harmless.  A false positive, however, can result in the wrong
85640** answer.
85641*/
85642SQLITE_PRIVATE int sqlite3ExprNeedsNoAffinityChange(const Expr *p, char aff){
85643  u8 op;
85644  if( aff==SQLITE_AFF_BLOB ) return 1;
85645  while( p->op==TK_UPLUS || p->op==TK_UMINUS ){ p = p->pLeft; }
85646  op = p->op;
85647  if( op==TK_REGISTER ) op = p->op2;
85648  switch( op ){
85649    case TK_INTEGER: {
85650      return aff==SQLITE_AFF_INTEGER || aff==SQLITE_AFF_NUMERIC;
85651    }
85652    case TK_FLOAT: {
85653      return aff==SQLITE_AFF_REAL || aff==SQLITE_AFF_NUMERIC;
85654    }
85655    case TK_STRING: {
85656      return aff==SQLITE_AFF_TEXT;
85657    }
85658    case TK_BLOB: {
85659      return 1;
85660    }
85661    case TK_COLUMN: {
85662      assert( p->iTable>=0 );  /* p cannot be part of a CHECK constraint */
85663      return p->iColumn<0
85664          && (aff==SQLITE_AFF_INTEGER || aff==SQLITE_AFF_NUMERIC);
85665    }
85666    default: {
85667      return 0;
85668    }
85669  }
85670}
85671
85672/*
85673** Return TRUE if the given string is a row-id column name.
85674*/
85675SQLITE_PRIVATE int sqlite3IsRowid(const char *z){
85676  if( sqlite3StrICmp(z, "_ROWID_")==0 ) return 1;
85677  if( sqlite3StrICmp(z, "ROWID")==0 ) return 1;
85678  if( sqlite3StrICmp(z, "OID")==0 ) return 1;
85679  return 0;
85680}
85681
85682/*
85683** Return true if we are able to the IN operator optimization on a
85684** query of the form
85685**
85686**       x IN (SELECT ...)
85687**
85688** Where the SELECT... clause is as specified by the parameter to this
85689** routine.
85690**
85691** The Select object passed in has already been preprocessed and no
85692** errors have been found.
85693*/
85694#ifndef SQLITE_OMIT_SUBQUERY
85695static int isCandidateForInOpt(Select *p){
85696  SrcList *pSrc;
85697  ExprList *pEList;
85698  Table *pTab;
85699  if( p==0 ) return 0;                   /* right-hand side of IN is SELECT */
85700  if( p->pPrior ) return 0;              /* Not a compound SELECT */
85701  if( p->selFlags & (SF_Distinct|SF_Aggregate) ){
85702    testcase( (p->selFlags & (SF_Distinct|SF_Aggregate))==SF_Distinct );
85703    testcase( (p->selFlags & (SF_Distinct|SF_Aggregate))==SF_Aggregate );
85704    return 0; /* No DISTINCT keyword and no aggregate functions */
85705  }
85706  assert( p->pGroupBy==0 );              /* Has no GROUP BY clause */
85707  if( p->pLimit ) return 0;              /* Has no LIMIT clause */
85708  assert( p->pOffset==0 );               /* No LIMIT means no OFFSET */
85709  if( p->pWhere ) return 0;              /* Has no WHERE clause */
85710  pSrc = p->pSrc;
85711  assert( pSrc!=0 );
85712  if( pSrc->nSrc!=1 ) return 0;          /* Single term in FROM clause */
85713  if( pSrc->a[0].pSelect ) return 0;     /* FROM is not a subquery or view */
85714  pTab = pSrc->a[0].pTab;
85715  if( NEVER(pTab==0) ) return 0;
85716  assert( pTab->pSelect==0 );            /* FROM clause is not a view */
85717  if( IsVirtual(pTab) ) return 0;        /* FROM clause not a virtual table */
85718  pEList = p->pEList;
85719  if( pEList->nExpr!=1 ) return 0;       /* One column in the result set */
85720  if( pEList->a[0].pExpr->op!=TK_COLUMN ) return 0; /* Result is a column */
85721  return 1;
85722}
85723#endif /* SQLITE_OMIT_SUBQUERY */
85724
85725/*
85726** Code an OP_Once instruction and allocate space for its flag. Return the
85727** address of the new instruction.
85728*/
85729SQLITE_PRIVATE int sqlite3CodeOnce(Parse *pParse){
85730  Vdbe *v = sqlite3GetVdbe(pParse);      /* Virtual machine being coded */
85731  return sqlite3VdbeAddOp1(v, OP_Once, pParse->nOnce++);
85732}
85733
85734/*
85735** Generate code that checks the left-most column of index table iCur to see if
85736** it contains any NULL entries.  Cause the register at regHasNull to be set
85737** to a non-NULL value if iCur contains no NULLs.  Cause register regHasNull
85738** to be set to NULL if iCur contains one or more NULL values.
85739*/
85740static void sqlite3SetHasNullFlag(Vdbe *v, int iCur, int regHasNull){
85741  int j1;
85742  sqlite3VdbeAddOp2(v, OP_Integer, 0, regHasNull);
85743  j1 = sqlite3VdbeAddOp1(v, OP_Rewind, iCur); VdbeCoverage(v);
85744  sqlite3VdbeAddOp3(v, OP_Column, iCur, 0, regHasNull);
85745  sqlite3VdbeChangeP5(v, OPFLAG_TYPEOFARG);
85746  VdbeComment((v, "first_entry_in(%d)", iCur));
85747  sqlite3VdbeJumpHere(v, j1);
85748}
85749
85750
85751#ifndef SQLITE_OMIT_SUBQUERY
85752/*
85753** The argument is an IN operator with a list (not a subquery) on the
85754** right-hand side.  Return TRUE if that list is constant.
85755*/
85756static int sqlite3InRhsIsConstant(Expr *pIn){
85757  Expr *pLHS;
85758  int res;
85759  assert( !ExprHasProperty(pIn, EP_xIsSelect) );
85760  pLHS = pIn->pLeft;
85761  pIn->pLeft = 0;
85762  res = sqlite3ExprIsConstant(pIn);
85763  pIn->pLeft = pLHS;
85764  return res;
85765}
85766#endif
85767
85768/*
85769** This function is used by the implementation of the IN (...) operator.
85770** The pX parameter is the expression on the RHS of the IN operator, which
85771** might be either a list of expressions or a subquery.
85772**
85773** The job of this routine is to find or create a b-tree object that can
85774** be used either to test for membership in the RHS set or to iterate through
85775** all members of the RHS set, skipping duplicates.
85776**
85777** A cursor is opened on the b-tree object that is the RHS of the IN operator
85778** and pX->iTable is set to the index of that cursor.
85779**
85780** The returned value of this function indicates the b-tree type, as follows:
85781**
85782**   IN_INDEX_ROWID      - The cursor was opened on a database table.
85783**   IN_INDEX_INDEX_ASC  - The cursor was opened on an ascending index.
85784**   IN_INDEX_INDEX_DESC - The cursor was opened on a descending index.
85785**   IN_INDEX_EPH        - The cursor was opened on a specially created and
85786**                         populated epheremal table.
85787**   IN_INDEX_NOOP       - No cursor was allocated.  The IN operator must be
85788**                         implemented as a sequence of comparisons.
85789**
85790** An existing b-tree might be used if the RHS expression pX is a simple
85791** subquery such as:
85792**
85793**     SELECT <column> FROM <table>
85794**
85795** If the RHS of the IN operator is a list or a more complex subquery, then
85796** an ephemeral table might need to be generated from the RHS and then
85797** pX->iTable made to point to the ephemeral table instead of an
85798** existing table.
85799**
85800** The inFlags parameter must contain exactly one of the bits
85801** IN_INDEX_MEMBERSHIP or IN_INDEX_LOOP.  If inFlags contains
85802** IN_INDEX_MEMBERSHIP, then the generated table will be used for a
85803** fast membership test.  When the IN_INDEX_LOOP bit is set, the
85804** IN index will be used to loop over all values of the RHS of the
85805** IN operator.
85806**
85807** When IN_INDEX_LOOP is used (and the b-tree will be used to iterate
85808** through the set members) then the b-tree must not contain duplicates.
85809** An epheremal table must be used unless the selected <column> is guaranteed
85810** to be unique - either because it is an INTEGER PRIMARY KEY or it
85811** has a UNIQUE constraint or UNIQUE index.
85812**
85813** When IN_INDEX_MEMBERSHIP is used (and the b-tree will be used
85814** for fast set membership tests) then an epheremal table must
85815** be used unless <column> is an INTEGER PRIMARY KEY or an index can
85816** be found with <column> as its left-most column.
85817**
85818** If the IN_INDEX_NOOP_OK and IN_INDEX_MEMBERSHIP are both set and
85819** if the RHS of the IN operator is a list (not a subquery) then this
85820** routine might decide that creating an ephemeral b-tree for membership
85821** testing is too expensive and return IN_INDEX_NOOP.  In that case, the
85822** calling routine should implement the IN operator using a sequence
85823** of Eq or Ne comparison operations.
85824**
85825** When the b-tree is being used for membership tests, the calling function
85826** might need to know whether or not the RHS side of the IN operator
85827** contains a NULL.  If prRhsHasNull is not a NULL pointer and
85828** if there is any chance that the (...) might contain a NULL value at
85829** runtime, then a register is allocated and the register number written
85830** to *prRhsHasNull. If there is no chance that the (...) contains a
85831** NULL value, then *prRhsHasNull is left unchanged.
85832**
85833** If a register is allocated and its location stored in *prRhsHasNull, then
85834** the value in that register will be NULL if the b-tree contains one or more
85835** NULL values, and it will be some non-NULL value if the b-tree contains no
85836** NULL values.
85837*/
85838#ifndef SQLITE_OMIT_SUBQUERY
85839SQLITE_PRIVATE int sqlite3FindInIndex(Parse *pParse, Expr *pX, u32 inFlags, int *prRhsHasNull){
85840  Select *p;                            /* SELECT to the right of IN operator */
85841  int eType = 0;                        /* Type of RHS table. IN_INDEX_* */
85842  int iTab = pParse->nTab++;            /* Cursor of the RHS table */
85843  int mustBeUnique;                     /* True if RHS must be unique */
85844  Vdbe *v = sqlite3GetVdbe(pParse);     /* Virtual machine being coded */
85845
85846  assert( pX->op==TK_IN );
85847  mustBeUnique = (inFlags & IN_INDEX_LOOP)!=0;
85848
85849  /* Check to see if an existing table or index can be used to
85850  ** satisfy the query.  This is preferable to generating a new
85851  ** ephemeral table.
85852  */
85853  p = (ExprHasProperty(pX, EP_xIsSelect) ? pX->x.pSelect : 0);
85854  if( pParse->nErr==0 && isCandidateForInOpt(p) ){
85855    sqlite3 *db = pParse->db;              /* Database connection */
85856    Table *pTab;                           /* Table <table>. */
85857    Expr *pExpr;                           /* Expression <column> */
85858    i16 iCol;                              /* Index of column <column> */
85859    i16 iDb;                               /* Database idx for pTab */
85860
85861    assert( p );                        /* Because of isCandidateForInOpt(p) */
85862    assert( p->pEList!=0 );             /* Because of isCandidateForInOpt(p) */
85863    assert( p->pEList->a[0].pExpr!=0 ); /* Because of isCandidateForInOpt(p) */
85864    assert( p->pSrc!=0 );               /* Because of isCandidateForInOpt(p) */
85865    pTab = p->pSrc->a[0].pTab;
85866    pExpr = p->pEList->a[0].pExpr;
85867    iCol = (i16)pExpr->iColumn;
85868
85869    /* Code an OP_Transaction and OP_TableLock for <table>. */
85870    iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
85871    sqlite3CodeVerifySchema(pParse, iDb);
85872    sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
85873
85874    /* This function is only called from two places. In both cases the vdbe
85875    ** has already been allocated. So assume sqlite3GetVdbe() is always
85876    ** successful here.
85877    */
85878    assert(v);
85879    if( iCol<0 ){
85880      int iAddr = sqlite3CodeOnce(pParse);
85881      VdbeCoverage(v);
85882
85883      sqlite3OpenTable(pParse, iTab, iDb, pTab, OP_OpenRead);
85884      eType = IN_INDEX_ROWID;
85885
85886      sqlite3VdbeJumpHere(v, iAddr);
85887    }else{
85888      Index *pIdx;                         /* Iterator variable */
85889
85890      /* The collation sequence used by the comparison. If an index is to
85891      ** be used in place of a temp-table, it must be ordered according
85892      ** to this collation sequence.  */
85893      CollSeq *pReq = sqlite3BinaryCompareCollSeq(pParse, pX->pLeft, pExpr);
85894
85895      /* Check that the affinity that will be used to perform the
85896      ** comparison is the same as the affinity of the column. If
85897      ** it is not, it is not possible to use any index.
85898      */
85899      int affinity_ok = sqlite3IndexAffinityOk(pX, pTab->aCol[iCol].affinity);
85900
85901      for(pIdx=pTab->pIndex; pIdx && eType==0 && affinity_ok; pIdx=pIdx->pNext){
85902        if( (pIdx->aiColumn[0]==iCol)
85903         && sqlite3FindCollSeq(db, ENC(db), pIdx->azColl[0], 0)==pReq
85904         && (!mustBeUnique || (pIdx->nKeyCol==1 && IsUniqueIndex(pIdx)))
85905        ){
85906          int iAddr = sqlite3CodeOnce(pParse); VdbeCoverage(v);
85907          sqlite3VdbeAddOp3(v, OP_OpenRead, iTab, pIdx->tnum, iDb);
85908          sqlite3VdbeSetP4KeyInfo(pParse, pIdx);
85909          VdbeComment((v, "%s", pIdx->zName));
85910          assert( IN_INDEX_INDEX_DESC == IN_INDEX_INDEX_ASC+1 );
85911          eType = IN_INDEX_INDEX_ASC + pIdx->aSortOrder[0];
85912
85913          if( prRhsHasNull && !pTab->aCol[iCol].notNull ){
85914            *prRhsHasNull = ++pParse->nMem;
85915            sqlite3SetHasNullFlag(v, iTab, *prRhsHasNull);
85916          }
85917          sqlite3VdbeJumpHere(v, iAddr);
85918        }
85919      }
85920    }
85921  }
85922
85923  /* If no preexisting index is available for the IN clause
85924  ** and IN_INDEX_NOOP is an allowed reply
85925  ** and the RHS of the IN operator is a list, not a subquery
85926  ** and the RHS is not contant or has two or fewer terms,
85927  ** then it is not worth creating an ephemeral table to evaluate
85928  ** the IN operator so return IN_INDEX_NOOP.
85929  */
85930  if( eType==0
85931   && (inFlags & IN_INDEX_NOOP_OK)
85932   && !ExprHasProperty(pX, EP_xIsSelect)
85933   && (!sqlite3InRhsIsConstant(pX) || pX->x.pList->nExpr<=2)
85934  ){
85935    eType = IN_INDEX_NOOP;
85936  }
85937
85938
85939  if( eType==0 ){
85940    /* Could not find an existing table or index to use as the RHS b-tree.
85941    ** We will have to generate an ephemeral table to do the job.
85942    */
85943    u32 savedNQueryLoop = pParse->nQueryLoop;
85944    int rMayHaveNull = 0;
85945    eType = IN_INDEX_EPH;
85946    if( inFlags & IN_INDEX_LOOP ){
85947      pParse->nQueryLoop = 0;
85948      if( pX->pLeft->iColumn<0 && !ExprHasProperty(pX, EP_xIsSelect) ){
85949        eType = IN_INDEX_ROWID;
85950      }
85951    }else if( prRhsHasNull ){
85952      *prRhsHasNull = rMayHaveNull = ++pParse->nMem;
85953    }
85954    sqlite3CodeSubselect(pParse, pX, rMayHaveNull, eType==IN_INDEX_ROWID);
85955    pParse->nQueryLoop = savedNQueryLoop;
85956  }else{
85957    pX->iTable = iTab;
85958  }
85959  return eType;
85960}
85961#endif
85962
85963/*
85964** Generate code for scalar subqueries used as a subquery expression, EXISTS,
85965** or IN operators.  Examples:
85966**
85967**     (SELECT a FROM b)          -- subquery
85968**     EXISTS (SELECT a FROM b)   -- EXISTS subquery
85969**     x IN (4,5,11)              -- IN operator with list on right-hand side
85970**     x IN (SELECT a FROM b)     -- IN operator with subquery on the right
85971**
85972** The pExpr parameter describes the expression that contains the IN
85973** operator or subquery.
85974**
85975** If parameter isRowid is non-zero, then expression pExpr is guaranteed
85976** to be of the form "<rowid> IN (?, ?, ?)", where <rowid> is a reference
85977** to some integer key column of a table B-Tree. In this case, use an
85978** intkey B-Tree to store the set of IN(...) values instead of the usual
85979** (slower) variable length keys B-Tree.
85980**
85981** If rMayHaveNull is non-zero, that means that the operation is an IN
85982** (not a SELECT or EXISTS) and that the RHS might contains NULLs.
85983** All this routine does is initialize the register given by rMayHaveNull
85984** to NULL.  Calling routines will take care of changing this register
85985** value to non-NULL if the RHS is NULL-free.
85986**
85987** For a SELECT or EXISTS operator, return the register that holds the
85988** result.  For IN operators or if an error occurs, the return value is 0.
85989*/
85990#ifndef SQLITE_OMIT_SUBQUERY
85991SQLITE_PRIVATE int sqlite3CodeSubselect(
85992  Parse *pParse,          /* Parsing context */
85993  Expr *pExpr,            /* The IN, SELECT, or EXISTS operator */
85994  int rHasNullFlag,       /* Register that records whether NULLs exist in RHS */
85995  int isRowid             /* If true, LHS of IN operator is a rowid */
85996){
85997  int jmpIfDynamic = -1;                      /* One-time test address */
85998  int rReg = 0;                           /* Register storing resulting */
85999  Vdbe *v = sqlite3GetVdbe(pParse);
86000  if( NEVER(v==0) ) return 0;
86001  sqlite3ExprCachePush(pParse);
86002
86003  /* This code must be run in its entirety every time it is encountered
86004  ** if any of the following is true:
86005  **
86006  **    *  The right-hand side is a correlated subquery
86007  **    *  The right-hand side is an expression list containing variables
86008  **    *  We are inside a trigger
86009  **
86010  ** If all of the above are false, then we can run this code just once
86011  ** save the results, and reuse the same result on subsequent invocations.
86012  */
86013  if( !ExprHasProperty(pExpr, EP_VarSelect) ){
86014    jmpIfDynamic = sqlite3CodeOnce(pParse); VdbeCoverage(v);
86015  }
86016
86017#ifndef SQLITE_OMIT_EXPLAIN
86018  if( pParse->explain==2 ){
86019    char *zMsg = sqlite3MPrintf(
86020        pParse->db, "EXECUTE %s%s SUBQUERY %d", jmpIfDynamic>=0?"":"CORRELATED ",
86021        pExpr->op==TK_IN?"LIST":"SCALAR", pParse->iNextSelectId
86022    );
86023    sqlite3VdbeAddOp4(v, OP_Explain, pParse->iSelectId, 0, 0, zMsg, P4_DYNAMIC);
86024  }
86025#endif
86026
86027  switch( pExpr->op ){
86028    case TK_IN: {
86029      char affinity;              /* Affinity of the LHS of the IN */
86030      int addr;                   /* Address of OP_OpenEphemeral instruction */
86031      Expr *pLeft = pExpr->pLeft; /* the LHS of the IN operator */
86032      KeyInfo *pKeyInfo = 0;      /* Key information */
86033
86034      affinity = sqlite3ExprAffinity(pLeft);
86035
86036      /* Whether this is an 'x IN(SELECT...)' or an 'x IN(<exprlist>)'
86037      ** expression it is handled the same way.  An ephemeral table is
86038      ** filled with single-field index keys representing the results
86039      ** from the SELECT or the <exprlist>.
86040      **
86041      ** If the 'x' expression is a column value, or the SELECT...
86042      ** statement returns a column value, then the affinity of that
86043      ** column is used to build the index keys. If both 'x' and the
86044      ** SELECT... statement are columns, then numeric affinity is used
86045      ** if either column has NUMERIC or INTEGER affinity. If neither
86046      ** 'x' nor the SELECT... statement are columns, then numeric affinity
86047      ** is used.
86048      */
86049      pExpr->iTable = pParse->nTab++;
86050      addr = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, pExpr->iTable, !isRowid);
86051      pKeyInfo = isRowid ? 0 : sqlite3KeyInfoAlloc(pParse->db, 1, 1);
86052
86053      if( ExprHasProperty(pExpr, EP_xIsSelect) ){
86054        /* Case 1:     expr IN (SELECT ...)
86055        **
86056        ** Generate code to write the results of the select into the temporary
86057        ** table allocated and opened above.
86058        */
86059        Select *pSelect = pExpr->x.pSelect;
86060        SelectDest dest;
86061        ExprList *pEList;
86062
86063        assert( !isRowid );
86064        sqlite3SelectDestInit(&dest, SRT_Set, pExpr->iTable);
86065        dest.affSdst = (u8)affinity;
86066        assert( (pExpr->iTable&0x0000FFFF)==pExpr->iTable );
86067        pSelect->iLimit = 0;
86068        testcase( pSelect->selFlags & SF_Distinct );
86069        testcase( pKeyInfo==0 ); /* Caused by OOM in sqlite3KeyInfoAlloc() */
86070        if( sqlite3Select(pParse, pSelect, &dest) ){
86071          sqlite3KeyInfoUnref(pKeyInfo);
86072          return 0;
86073        }
86074        pEList = pSelect->pEList;
86075        assert( pKeyInfo!=0 ); /* OOM will cause exit after sqlite3Select() */
86076        assert( pEList!=0 );
86077        assert( pEList->nExpr>0 );
86078        assert( sqlite3KeyInfoIsWriteable(pKeyInfo) );
86079        pKeyInfo->aColl[0] = sqlite3BinaryCompareCollSeq(pParse, pExpr->pLeft,
86080                                                         pEList->a[0].pExpr);
86081      }else if( ALWAYS(pExpr->x.pList!=0) ){
86082        /* Case 2:     expr IN (exprlist)
86083        **
86084        ** For each expression, build an index key from the evaluation and
86085        ** store it in the temporary table. If <expr> is a column, then use
86086        ** that columns affinity when building index keys. If <expr> is not
86087        ** a column, use numeric affinity.
86088        */
86089        int i;
86090        ExprList *pList = pExpr->x.pList;
86091        struct ExprList_item *pItem;
86092        int r1, r2, r3;
86093
86094        if( !affinity ){
86095          affinity = SQLITE_AFF_BLOB;
86096        }
86097        if( pKeyInfo ){
86098          assert( sqlite3KeyInfoIsWriteable(pKeyInfo) );
86099          pKeyInfo->aColl[0] = sqlite3ExprCollSeq(pParse, pExpr->pLeft);
86100        }
86101
86102        /* Loop through each expression in <exprlist>. */
86103        r1 = sqlite3GetTempReg(pParse);
86104        r2 = sqlite3GetTempReg(pParse);
86105        if( isRowid ) sqlite3VdbeAddOp2(v, OP_Null, 0, r2);
86106        for(i=pList->nExpr, pItem=pList->a; i>0; i--, pItem++){
86107          Expr *pE2 = pItem->pExpr;
86108          int iValToIns;
86109
86110          /* If the expression is not constant then we will need to
86111          ** disable the test that was generated above that makes sure
86112          ** this code only executes once.  Because for a non-constant
86113          ** expression we need to rerun this code each time.
86114          */
86115          if( jmpIfDynamic>=0 && !sqlite3ExprIsConstant(pE2) ){
86116            sqlite3VdbeChangeToNoop(v, jmpIfDynamic);
86117            jmpIfDynamic = -1;
86118          }
86119
86120          /* Evaluate the expression and insert it into the temp table */
86121          if( isRowid && sqlite3ExprIsInteger(pE2, &iValToIns) ){
86122            sqlite3VdbeAddOp3(v, OP_InsertInt, pExpr->iTable, r2, iValToIns);
86123          }else{
86124            r3 = sqlite3ExprCodeTarget(pParse, pE2, r1);
86125            if( isRowid ){
86126              sqlite3VdbeAddOp2(v, OP_MustBeInt, r3,
86127                                sqlite3VdbeCurrentAddr(v)+2);
86128              VdbeCoverage(v);
86129              sqlite3VdbeAddOp3(v, OP_Insert, pExpr->iTable, r2, r3);
86130            }else{
86131              sqlite3VdbeAddOp4(v, OP_MakeRecord, r3, 1, r2, &affinity, 1);
86132              sqlite3ExprCacheAffinityChange(pParse, r3, 1);
86133              sqlite3VdbeAddOp2(v, OP_IdxInsert, pExpr->iTable, r2);
86134            }
86135          }
86136        }
86137        sqlite3ReleaseTempReg(pParse, r1);
86138        sqlite3ReleaseTempReg(pParse, r2);
86139      }
86140      if( pKeyInfo ){
86141        sqlite3VdbeChangeP4(v, addr, (void *)pKeyInfo, P4_KEYINFO);
86142      }
86143      break;
86144    }
86145
86146    case TK_EXISTS:
86147    case TK_SELECT:
86148    default: {
86149      /* If this has to be a scalar SELECT.  Generate code to put the
86150      ** value of this select in a memory cell and record the number
86151      ** of the memory cell in iColumn.  If this is an EXISTS, write
86152      ** an integer 0 (not exists) or 1 (exists) into a memory cell
86153      ** and record that memory cell in iColumn.
86154      */
86155      Select *pSel;                         /* SELECT statement to encode */
86156      SelectDest dest;                      /* How to deal with SELECt result */
86157
86158      testcase( pExpr->op==TK_EXISTS );
86159      testcase( pExpr->op==TK_SELECT );
86160      assert( pExpr->op==TK_EXISTS || pExpr->op==TK_SELECT );
86161
86162      assert( ExprHasProperty(pExpr, EP_xIsSelect) );
86163      pSel = pExpr->x.pSelect;
86164      sqlite3SelectDestInit(&dest, 0, ++pParse->nMem);
86165      if( pExpr->op==TK_SELECT ){
86166        dest.eDest = SRT_Mem;
86167        dest.iSdst = dest.iSDParm;
86168        sqlite3VdbeAddOp2(v, OP_Null, 0, dest.iSDParm);
86169        VdbeComment((v, "Init subquery result"));
86170      }else{
86171        dest.eDest = SRT_Exists;
86172        sqlite3VdbeAddOp2(v, OP_Integer, 0, dest.iSDParm);
86173        VdbeComment((v, "Init EXISTS result"));
86174      }
86175      sqlite3ExprDelete(pParse->db, pSel->pLimit);
86176      pSel->pLimit = sqlite3PExpr(pParse, TK_INTEGER, 0, 0,
86177                                  &sqlite3IntTokens[1]);
86178      pSel->iLimit = 0;
86179      pSel->selFlags &= ~SF_MultiValue;
86180      if( sqlite3Select(pParse, pSel, &dest) ){
86181        return 0;
86182      }
86183      rReg = dest.iSDParm;
86184      ExprSetVVAProperty(pExpr, EP_NoReduce);
86185      break;
86186    }
86187  }
86188
86189  if( rHasNullFlag ){
86190    sqlite3SetHasNullFlag(v, pExpr->iTable, rHasNullFlag);
86191  }
86192
86193  if( jmpIfDynamic>=0 ){
86194    sqlite3VdbeJumpHere(v, jmpIfDynamic);
86195  }
86196  sqlite3ExprCachePop(pParse);
86197
86198  return rReg;
86199}
86200#endif /* SQLITE_OMIT_SUBQUERY */
86201
86202#ifndef SQLITE_OMIT_SUBQUERY
86203/*
86204** Generate code for an IN expression.
86205**
86206**      x IN (SELECT ...)
86207**      x IN (value, value, ...)
86208**
86209** The left-hand side (LHS) is a scalar expression.  The right-hand side (RHS)
86210** is an array of zero or more values.  The expression is true if the LHS is
86211** contained within the RHS.  The value of the expression is unknown (NULL)
86212** if the LHS is NULL or if the LHS is not contained within the RHS and the
86213** RHS contains one or more NULL values.
86214**
86215** This routine generates code that jumps to destIfFalse if the LHS is not
86216** contained within the RHS.  If due to NULLs we cannot determine if the LHS
86217** is contained in the RHS then jump to destIfNull.  If the LHS is contained
86218** within the RHS then fall through.
86219*/
86220static void sqlite3ExprCodeIN(
86221  Parse *pParse,        /* Parsing and code generating context */
86222  Expr *pExpr,          /* The IN expression */
86223  int destIfFalse,      /* Jump here if LHS is not contained in the RHS */
86224  int destIfNull        /* Jump here if the results are unknown due to NULLs */
86225){
86226  int rRhsHasNull = 0;  /* Register that is true if RHS contains NULL values */
86227  char affinity;        /* Comparison affinity to use */
86228  int eType;            /* Type of the RHS */
86229  int r1;               /* Temporary use register */
86230  Vdbe *v;              /* Statement under construction */
86231
86232  /* Compute the RHS.   After this step, the table with cursor
86233  ** pExpr->iTable will contains the values that make up the RHS.
86234  */
86235  v = pParse->pVdbe;
86236  assert( v!=0 );       /* OOM detected prior to this routine */
86237  VdbeNoopComment((v, "begin IN expr"));
86238  eType = sqlite3FindInIndex(pParse, pExpr,
86239                             IN_INDEX_MEMBERSHIP | IN_INDEX_NOOP_OK,
86240                             destIfFalse==destIfNull ? 0 : &rRhsHasNull);
86241
86242  /* Figure out the affinity to use to create a key from the results
86243  ** of the expression. affinityStr stores a static string suitable for
86244  ** P4 of OP_MakeRecord.
86245  */
86246  affinity = comparisonAffinity(pExpr);
86247
86248  /* Code the LHS, the <expr> from "<expr> IN (...)".
86249  */
86250  sqlite3ExprCachePush(pParse);
86251  r1 = sqlite3GetTempReg(pParse);
86252  sqlite3ExprCode(pParse, pExpr->pLeft, r1);
86253
86254  /* If sqlite3FindInIndex() did not find or create an index that is
86255  ** suitable for evaluating the IN operator, then evaluate using a
86256  ** sequence of comparisons.
86257  */
86258  if( eType==IN_INDEX_NOOP ){
86259    ExprList *pList = pExpr->x.pList;
86260    CollSeq *pColl = sqlite3ExprCollSeq(pParse, pExpr->pLeft);
86261    int labelOk = sqlite3VdbeMakeLabel(v);
86262    int r2, regToFree;
86263    int regCkNull = 0;
86264    int ii;
86265    assert( !ExprHasProperty(pExpr, EP_xIsSelect) );
86266    if( destIfNull!=destIfFalse ){
86267      regCkNull = sqlite3GetTempReg(pParse);
86268      sqlite3VdbeAddOp3(v, OP_BitAnd, r1, r1, regCkNull);
86269    }
86270    for(ii=0; ii<pList->nExpr; ii++){
86271      r2 = sqlite3ExprCodeTemp(pParse, pList->a[ii].pExpr, &regToFree);
86272      if( regCkNull && sqlite3ExprCanBeNull(pList->a[ii].pExpr) ){
86273        sqlite3VdbeAddOp3(v, OP_BitAnd, regCkNull, r2, regCkNull);
86274      }
86275      if( ii<pList->nExpr-1 || destIfNull!=destIfFalse ){
86276        sqlite3VdbeAddOp4(v, OP_Eq, r1, labelOk, r2,
86277                          (void*)pColl, P4_COLLSEQ);
86278        VdbeCoverageIf(v, ii<pList->nExpr-1);
86279        VdbeCoverageIf(v, ii==pList->nExpr-1);
86280        sqlite3VdbeChangeP5(v, affinity);
86281      }else{
86282        assert( destIfNull==destIfFalse );
86283        sqlite3VdbeAddOp4(v, OP_Ne, r1, destIfFalse, r2,
86284                          (void*)pColl, P4_COLLSEQ); VdbeCoverage(v);
86285        sqlite3VdbeChangeP5(v, affinity | SQLITE_JUMPIFNULL);
86286      }
86287      sqlite3ReleaseTempReg(pParse, regToFree);
86288    }
86289    if( regCkNull ){
86290      sqlite3VdbeAddOp2(v, OP_IsNull, regCkNull, destIfNull); VdbeCoverage(v);
86291      sqlite3VdbeAddOp2(v, OP_Goto, 0, destIfFalse);
86292    }
86293    sqlite3VdbeResolveLabel(v, labelOk);
86294    sqlite3ReleaseTempReg(pParse, regCkNull);
86295  }else{
86296
86297    /* If the LHS is NULL, then the result is either false or NULL depending
86298    ** on whether the RHS is empty or not, respectively.
86299    */
86300    if( sqlite3ExprCanBeNull(pExpr->pLeft) ){
86301      if( destIfNull==destIfFalse ){
86302        /* Shortcut for the common case where the false and NULL outcomes are
86303        ** the same. */
86304        sqlite3VdbeAddOp2(v, OP_IsNull, r1, destIfNull); VdbeCoverage(v);
86305      }else{
86306        int addr1 = sqlite3VdbeAddOp1(v, OP_NotNull, r1); VdbeCoverage(v);
86307        sqlite3VdbeAddOp2(v, OP_Rewind, pExpr->iTable, destIfFalse);
86308        VdbeCoverage(v);
86309        sqlite3VdbeAddOp2(v, OP_Goto, 0, destIfNull);
86310        sqlite3VdbeJumpHere(v, addr1);
86311      }
86312    }
86313
86314    if( eType==IN_INDEX_ROWID ){
86315      /* In this case, the RHS is the ROWID of table b-tree
86316      */
86317      sqlite3VdbeAddOp2(v, OP_MustBeInt, r1, destIfFalse); VdbeCoverage(v);
86318      sqlite3VdbeAddOp3(v, OP_NotExists, pExpr->iTable, destIfFalse, r1);
86319      VdbeCoverage(v);
86320    }else{
86321      /* In this case, the RHS is an index b-tree.
86322      */
86323      sqlite3VdbeAddOp4(v, OP_Affinity, r1, 1, 0, &affinity, 1);
86324
86325      /* If the set membership test fails, then the result of the
86326      ** "x IN (...)" expression must be either 0 or NULL. If the set
86327      ** contains no NULL values, then the result is 0. If the set
86328      ** contains one or more NULL values, then the result of the
86329      ** expression is also NULL.
86330      */
86331      assert( destIfFalse!=destIfNull || rRhsHasNull==0 );
86332      if( rRhsHasNull==0 ){
86333        /* This branch runs if it is known at compile time that the RHS
86334        ** cannot contain NULL values. This happens as the result
86335        ** of a "NOT NULL" constraint in the database schema.
86336        **
86337        ** Also run this branch if NULL is equivalent to FALSE
86338        ** for this particular IN operator.
86339        */
86340        sqlite3VdbeAddOp4Int(v, OP_NotFound, pExpr->iTable, destIfFalse, r1, 1);
86341        VdbeCoverage(v);
86342      }else{
86343        /* In this branch, the RHS of the IN might contain a NULL and
86344        ** the presence of a NULL on the RHS makes a difference in the
86345        ** outcome.
86346        */
86347        int j1;
86348
86349        /* First check to see if the LHS is contained in the RHS.  If so,
86350        ** then the answer is TRUE the presence of NULLs in the RHS does
86351        ** not matter.  If the LHS is not contained in the RHS, then the
86352        ** answer is NULL if the RHS contains NULLs and the answer is
86353        ** FALSE if the RHS is NULL-free.
86354        */
86355        j1 = sqlite3VdbeAddOp4Int(v, OP_Found, pExpr->iTable, 0, r1, 1);
86356        VdbeCoverage(v);
86357        sqlite3VdbeAddOp2(v, OP_IsNull, rRhsHasNull, destIfNull);
86358        VdbeCoverage(v);
86359        sqlite3VdbeAddOp2(v, OP_Goto, 0, destIfFalse);
86360        sqlite3VdbeJumpHere(v, j1);
86361      }
86362    }
86363  }
86364  sqlite3ReleaseTempReg(pParse, r1);
86365  sqlite3ExprCachePop(pParse);
86366  VdbeComment((v, "end IN expr"));
86367}
86368#endif /* SQLITE_OMIT_SUBQUERY */
86369
86370#ifndef SQLITE_OMIT_FLOATING_POINT
86371/*
86372** Generate an instruction that will put the floating point
86373** value described by z[0..n-1] into register iMem.
86374**
86375** The z[] string will probably not be zero-terminated.  But the
86376** z[n] character is guaranteed to be something that does not look
86377** like the continuation of the number.
86378*/
86379static void codeReal(Vdbe *v, const char *z, int negateFlag, int iMem){
86380  if( ALWAYS(z!=0) ){
86381    double value;
86382    sqlite3AtoF(z, &value, sqlite3Strlen30(z), SQLITE_UTF8);
86383    assert( !sqlite3IsNaN(value) ); /* The new AtoF never returns NaN */
86384    if( negateFlag ) value = -value;
86385    sqlite3VdbeAddOp4Dup8(v, OP_Real, 0, iMem, 0, (u8*)&value, P4_REAL);
86386  }
86387}
86388#endif
86389
86390
86391/*
86392** Generate an instruction that will put the integer describe by
86393** text z[0..n-1] into register iMem.
86394**
86395** Expr.u.zToken is always UTF8 and zero-terminated.
86396*/
86397static void codeInteger(Parse *pParse, Expr *pExpr, int negFlag, int iMem){
86398  Vdbe *v = pParse->pVdbe;
86399  if( pExpr->flags & EP_IntValue ){
86400    int i = pExpr->u.iValue;
86401    assert( i>=0 );
86402    if( negFlag ) i = -i;
86403    sqlite3VdbeAddOp2(v, OP_Integer, i, iMem);
86404  }else{
86405    int c;
86406    i64 value;
86407    const char *z = pExpr->u.zToken;
86408    assert( z!=0 );
86409    c = sqlite3DecOrHexToI64(z, &value);
86410    if( c==0 || (c==2 && negFlag) ){
86411      if( negFlag ){ value = c==2 ? SMALLEST_INT64 : -value; }
86412      sqlite3VdbeAddOp4Dup8(v, OP_Int64, 0, iMem, 0, (u8*)&value, P4_INT64);
86413    }else{
86414#ifdef SQLITE_OMIT_FLOATING_POINT
86415      sqlite3ErrorMsg(pParse, "oversized integer: %s%s", negFlag ? "-" : "", z);
86416#else
86417#ifndef SQLITE_OMIT_HEX_INTEGER
86418      if( sqlite3_strnicmp(z,"0x",2)==0 ){
86419        sqlite3ErrorMsg(pParse, "hex literal too big: %s", z);
86420      }else
86421#endif
86422      {
86423        codeReal(v, z, negFlag, iMem);
86424      }
86425#endif
86426    }
86427  }
86428}
86429
86430/*
86431** Clear a cache entry.
86432*/
86433static void cacheEntryClear(Parse *pParse, struct yColCache *p){
86434  if( p->tempReg ){
86435    if( pParse->nTempReg<ArraySize(pParse->aTempReg) ){
86436      pParse->aTempReg[pParse->nTempReg++] = p->iReg;
86437    }
86438    p->tempReg = 0;
86439  }
86440}
86441
86442
86443/*
86444** Record in the column cache that a particular column from a
86445** particular table is stored in a particular register.
86446*/
86447SQLITE_PRIVATE void sqlite3ExprCacheStore(Parse *pParse, int iTab, int iCol, int iReg){
86448  int i;
86449  int minLru;
86450  int idxLru;
86451  struct yColCache *p;
86452
86453  /* Unless an error has occurred, register numbers are always positive. */
86454  assert( iReg>0 || pParse->nErr || pParse->db->mallocFailed );
86455  assert( iCol>=-1 && iCol<32768 );  /* Finite column numbers */
86456
86457  /* The SQLITE_ColumnCache flag disables the column cache.  This is used
86458  ** for testing only - to verify that SQLite always gets the same answer
86459  ** with and without the column cache.
86460  */
86461  if( OptimizationDisabled(pParse->db, SQLITE_ColumnCache) ) return;
86462
86463  /* First replace any existing entry.
86464  **
86465  ** Actually, the way the column cache is currently used, we are guaranteed
86466  ** that the object will never already be in cache.  Verify this guarantee.
86467  */
86468#ifndef NDEBUG
86469  for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){
86470    assert( p->iReg==0 || p->iTable!=iTab || p->iColumn!=iCol );
86471  }
86472#endif
86473
86474  /* Find an empty slot and replace it */
86475  for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){
86476    if( p->iReg==0 ){
86477      p->iLevel = pParse->iCacheLevel;
86478      p->iTable = iTab;
86479      p->iColumn = iCol;
86480      p->iReg = iReg;
86481      p->tempReg = 0;
86482      p->lru = pParse->iCacheCnt++;
86483      return;
86484    }
86485  }
86486
86487  /* Replace the last recently used */
86488  minLru = 0x7fffffff;
86489  idxLru = -1;
86490  for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){
86491    if( p->lru<minLru ){
86492      idxLru = i;
86493      minLru = p->lru;
86494    }
86495  }
86496  if( ALWAYS(idxLru>=0) ){
86497    p = &pParse->aColCache[idxLru];
86498    p->iLevel = pParse->iCacheLevel;
86499    p->iTable = iTab;
86500    p->iColumn = iCol;
86501    p->iReg = iReg;
86502    p->tempReg = 0;
86503    p->lru = pParse->iCacheCnt++;
86504    return;
86505  }
86506}
86507
86508/*
86509** Indicate that registers between iReg..iReg+nReg-1 are being overwritten.
86510** Purge the range of registers from the column cache.
86511*/
86512SQLITE_PRIVATE void sqlite3ExprCacheRemove(Parse *pParse, int iReg, int nReg){
86513  int i;
86514  int iLast = iReg + nReg - 1;
86515  struct yColCache *p;
86516  for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){
86517    int r = p->iReg;
86518    if( r>=iReg && r<=iLast ){
86519      cacheEntryClear(pParse, p);
86520      p->iReg = 0;
86521    }
86522  }
86523}
86524
86525/*
86526** Remember the current column cache context.  Any new entries added
86527** added to the column cache after this call are removed when the
86528** corresponding pop occurs.
86529*/
86530SQLITE_PRIVATE void sqlite3ExprCachePush(Parse *pParse){
86531  pParse->iCacheLevel++;
86532#ifdef SQLITE_DEBUG
86533  if( pParse->db->flags & SQLITE_VdbeAddopTrace ){
86534    printf("PUSH to %d\n", pParse->iCacheLevel);
86535  }
86536#endif
86537}
86538
86539/*
86540** Remove from the column cache any entries that were added since the
86541** the previous sqlite3ExprCachePush operation.  In other words, restore
86542** the cache to the state it was in prior the most recent Push.
86543*/
86544SQLITE_PRIVATE void sqlite3ExprCachePop(Parse *pParse){
86545  int i;
86546  struct yColCache *p;
86547  assert( pParse->iCacheLevel>=1 );
86548  pParse->iCacheLevel--;
86549#ifdef SQLITE_DEBUG
86550  if( pParse->db->flags & SQLITE_VdbeAddopTrace ){
86551    printf("POP  to %d\n", pParse->iCacheLevel);
86552  }
86553#endif
86554  for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){
86555    if( p->iReg && p->iLevel>pParse->iCacheLevel ){
86556      cacheEntryClear(pParse, p);
86557      p->iReg = 0;
86558    }
86559  }
86560}
86561
86562/*
86563** When a cached column is reused, make sure that its register is
86564** no longer available as a temp register.  ticket #3879:  that same
86565** register might be in the cache in multiple places, so be sure to
86566** get them all.
86567*/
86568static void sqlite3ExprCachePinRegister(Parse *pParse, int iReg){
86569  int i;
86570  struct yColCache *p;
86571  for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){
86572    if( p->iReg==iReg ){
86573      p->tempReg = 0;
86574    }
86575  }
86576}
86577
86578/*
86579** Generate code to extract the value of the iCol-th column of a table.
86580*/
86581SQLITE_PRIVATE void sqlite3ExprCodeGetColumnOfTable(
86582  Vdbe *v,        /* The VDBE under construction */
86583  Table *pTab,    /* The table containing the value */
86584  int iTabCur,    /* The table cursor.  Or the PK cursor for WITHOUT ROWID */
86585  int iCol,       /* Index of the column to extract */
86586  int regOut      /* Extract the value into this register */
86587){
86588  if( iCol<0 || iCol==pTab->iPKey ){
86589    sqlite3VdbeAddOp2(v, OP_Rowid, iTabCur, regOut);
86590  }else{
86591    int op = IsVirtual(pTab) ? OP_VColumn : OP_Column;
86592    int x = iCol;
86593    if( !HasRowid(pTab) ){
86594      x = sqlite3ColumnOfIndex(sqlite3PrimaryKeyIndex(pTab), iCol);
86595    }
86596    sqlite3VdbeAddOp3(v, op, iTabCur, x, regOut);
86597  }
86598  if( iCol>=0 ){
86599    sqlite3ColumnDefault(v, pTab, iCol, regOut);
86600  }
86601}
86602
86603/*
86604** Generate code that will extract the iColumn-th column from
86605** table pTab and store the column value in a register.  An effort
86606** is made to store the column value in register iReg, but this is
86607** not guaranteed.  The location of the column value is returned.
86608**
86609** There must be an open cursor to pTab in iTable when this routine
86610** is called.  If iColumn<0 then code is generated that extracts the rowid.
86611*/
86612SQLITE_PRIVATE int sqlite3ExprCodeGetColumn(
86613  Parse *pParse,   /* Parsing and code generating context */
86614  Table *pTab,     /* Description of the table we are reading from */
86615  int iColumn,     /* Index of the table column */
86616  int iTable,      /* The cursor pointing to the table */
86617  int iReg,        /* Store results here */
86618  u8 p5            /* P5 value for OP_Column */
86619){
86620  Vdbe *v = pParse->pVdbe;
86621  int i;
86622  struct yColCache *p;
86623
86624  for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){
86625    if( p->iReg>0 && p->iTable==iTable && p->iColumn==iColumn ){
86626      p->lru = pParse->iCacheCnt++;
86627      sqlite3ExprCachePinRegister(pParse, p->iReg);
86628      return p->iReg;
86629    }
86630  }
86631  assert( v!=0 );
86632  sqlite3ExprCodeGetColumnOfTable(v, pTab, iTable, iColumn, iReg);
86633  if( p5 ){
86634    sqlite3VdbeChangeP5(v, p5);
86635  }else{
86636    sqlite3ExprCacheStore(pParse, iTable, iColumn, iReg);
86637  }
86638  return iReg;
86639}
86640
86641/*
86642** Clear all column cache entries.
86643*/
86644SQLITE_PRIVATE void sqlite3ExprCacheClear(Parse *pParse){
86645  int i;
86646  struct yColCache *p;
86647
86648#if SQLITE_DEBUG
86649  if( pParse->db->flags & SQLITE_VdbeAddopTrace ){
86650    printf("CLEAR\n");
86651  }
86652#endif
86653  for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){
86654    if( p->iReg ){
86655      cacheEntryClear(pParse, p);
86656      p->iReg = 0;
86657    }
86658  }
86659}
86660
86661/*
86662** Record the fact that an affinity change has occurred on iCount
86663** registers starting with iStart.
86664*/
86665SQLITE_PRIVATE void sqlite3ExprCacheAffinityChange(Parse *pParse, int iStart, int iCount){
86666  sqlite3ExprCacheRemove(pParse, iStart, iCount);
86667}
86668
86669/*
86670** Generate code to move content from registers iFrom...iFrom+nReg-1
86671** over to iTo..iTo+nReg-1. Keep the column cache up-to-date.
86672*/
86673SQLITE_PRIVATE void sqlite3ExprCodeMove(Parse *pParse, int iFrom, int iTo, int nReg){
86674  assert( iFrom>=iTo+nReg || iFrom+nReg<=iTo );
86675  sqlite3VdbeAddOp3(pParse->pVdbe, OP_Move, iFrom, iTo, nReg);
86676  sqlite3ExprCacheRemove(pParse, iFrom, nReg);
86677}
86678
86679#if defined(SQLITE_DEBUG) || defined(SQLITE_COVERAGE_TEST)
86680/*
86681** Return true if any register in the range iFrom..iTo (inclusive)
86682** is used as part of the column cache.
86683**
86684** This routine is used within assert() and testcase() macros only
86685** and does not appear in a normal build.
86686*/
86687static int usedAsColumnCache(Parse *pParse, int iFrom, int iTo){
86688  int i;
86689  struct yColCache *p;
86690  for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){
86691    int r = p->iReg;
86692    if( r>=iFrom && r<=iTo ) return 1;    /*NO_TEST*/
86693  }
86694  return 0;
86695}
86696#endif /* SQLITE_DEBUG || SQLITE_COVERAGE_TEST */
86697
86698/*
86699** Convert an expression node to a TK_REGISTER
86700*/
86701static void exprToRegister(Expr *p, int iReg){
86702  p->op2 = p->op;
86703  p->op = TK_REGISTER;
86704  p->iTable = iReg;
86705  ExprClearProperty(p, EP_Skip);
86706}
86707
86708/*
86709** Generate code into the current Vdbe to evaluate the given
86710** expression.  Attempt to store the results in register "target".
86711** Return the register where results are stored.
86712**
86713** With this routine, there is no guarantee that results will
86714** be stored in target.  The result might be stored in some other
86715** register if it is convenient to do so.  The calling function
86716** must check the return code and move the results to the desired
86717** register.
86718*/
86719SQLITE_PRIVATE int sqlite3ExprCodeTarget(Parse *pParse, Expr *pExpr, int target){
86720  Vdbe *v = pParse->pVdbe;  /* The VM under construction */
86721  int op;                   /* The opcode being coded */
86722  int inReg = target;       /* Results stored in register inReg */
86723  int regFree1 = 0;         /* If non-zero free this temporary register */
86724  int regFree2 = 0;         /* If non-zero free this temporary register */
86725  int r1, r2, r3, r4;       /* Various register numbers */
86726  sqlite3 *db = pParse->db; /* The database connection */
86727  Expr tempX;               /* Temporary expression node */
86728
86729  assert( target>0 && target<=pParse->nMem );
86730  if( v==0 ){
86731    assert( pParse->db->mallocFailed );
86732    return 0;
86733  }
86734
86735  if( pExpr==0 ){
86736    op = TK_NULL;
86737  }else{
86738    op = pExpr->op;
86739  }
86740  switch( op ){
86741    case TK_AGG_COLUMN: {
86742      AggInfo *pAggInfo = pExpr->pAggInfo;
86743      struct AggInfo_col *pCol = &pAggInfo->aCol[pExpr->iAgg];
86744      if( !pAggInfo->directMode ){
86745        assert( pCol->iMem>0 );
86746        inReg = pCol->iMem;
86747        break;
86748      }else if( pAggInfo->useSortingIdx ){
86749        sqlite3VdbeAddOp3(v, OP_Column, pAggInfo->sortingIdxPTab,
86750                              pCol->iSorterColumn, target);
86751        break;
86752      }
86753      /* Otherwise, fall thru into the TK_COLUMN case */
86754    }
86755    case TK_COLUMN: {
86756      int iTab = pExpr->iTable;
86757      if( iTab<0 ){
86758        if( pParse->ckBase>0 ){
86759          /* Generating CHECK constraints or inserting into partial index */
86760          inReg = pExpr->iColumn + pParse->ckBase;
86761          break;
86762        }else{
86763          /* Deleting from a partial index */
86764          iTab = pParse->iPartIdxTab;
86765        }
86766      }
86767      inReg = sqlite3ExprCodeGetColumn(pParse, pExpr->pTab,
86768                               pExpr->iColumn, iTab, target,
86769                               pExpr->op2);
86770      break;
86771    }
86772    case TK_INTEGER: {
86773      codeInteger(pParse, pExpr, 0, target);
86774      break;
86775    }
86776#ifndef SQLITE_OMIT_FLOATING_POINT
86777    case TK_FLOAT: {
86778      assert( !ExprHasProperty(pExpr, EP_IntValue) );
86779      codeReal(v, pExpr->u.zToken, 0, target);
86780      break;
86781    }
86782#endif
86783    case TK_STRING: {
86784      assert( !ExprHasProperty(pExpr, EP_IntValue) );
86785      sqlite3VdbeAddOp4(v, OP_String8, 0, target, 0, pExpr->u.zToken, 0);
86786      break;
86787    }
86788    case TK_NULL: {
86789      sqlite3VdbeAddOp2(v, OP_Null, 0, target);
86790      break;
86791    }
86792#ifndef SQLITE_OMIT_BLOB_LITERAL
86793    case TK_BLOB: {
86794      int n;
86795      const char *z;
86796      char *zBlob;
86797      assert( !ExprHasProperty(pExpr, EP_IntValue) );
86798      assert( pExpr->u.zToken[0]=='x' || pExpr->u.zToken[0]=='X' );
86799      assert( pExpr->u.zToken[1]=='\'' );
86800      z = &pExpr->u.zToken[2];
86801      n = sqlite3Strlen30(z) - 1;
86802      assert( z[n]=='\'' );
86803      zBlob = sqlite3HexToBlob(sqlite3VdbeDb(v), z, n);
86804      sqlite3VdbeAddOp4(v, OP_Blob, n/2, target, 0, zBlob, P4_DYNAMIC);
86805      break;
86806    }
86807#endif
86808    case TK_VARIABLE: {
86809      assert( !ExprHasProperty(pExpr, EP_IntValue) );
86810      assert( pExpr->u.zToken!=0 );
86811      assert( pExpr->u.zToken[0]!=0 );
86812      sqlite3VdbeAddOp2(v, OP_Variable, pExpr->iColumn, target);
86813      if( pExpr->u.zToken[1]!=0 ){
86814        assert( pExpr->u.zToken[0]=='?'
86815             || strcmp(pExpr->u.zToken, pParse->azVar[pExpr->iColumn-1])==0 );
86816        sqlite3VdbeChangeP4(v, -1, pParse->azVar[pExpr->iColumn-1], P4_STATIC);
86817      }
86818      break;
86819    }
86820    case TK_REGISTER: {
86821      inReg = pExpr->iTable;
86822      break;
86823    }
86824    case TK_AS: {
86825      inReg = sqlite3ExprCodeTarget(pParse, pExpr->pLeft, target);
86826      break;
86827    }
86828#ifndef SQLITE_OMIT_CAST
86829    case TK_CAST: {
86830      /* Expressions of the form:   CAST(pLeft AS token) */
86831      inReg = sqlite3ExprCodeTarget(pParse, pExpr->pLeft, target);
86832      if( inReg!=target ){
86833        sqlite3VdbeAddOp2(v, OP_SCopy, inReg, target);
86834        inReg = target;
86835      }
86836      sqlite3VdbeAddOp2(v, OP_Cast, target,
86837                        sqlite3AffinityType(pExpr->u.zToken, 0));
86838      testcase( usedAsColumnCache(pParse, inReg, inReg) );
86839      sqlite3ExprCacheAffinityChange(pParse, inReg, 1);
86840      break;
86841    }
86842#endif /* SQLITE_OMIT_CAST */
86843    case TK_LT:
86844    case TK_LE:
86845    case TK_GT:
86846    case TK_GE:
86847    case TK_NE:
86848    case TK_EQ: {
86849      r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
86850      r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, &regFree2);
86851      codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op,
86852                  r1, r2, inReg, SQLITE_STOREP2);
86853      assert(TK_LT==OP_Lt); testcase(op==OP_Lt); VdbeCoverageIf(v,op==OP_Lt);
86854      assert(TK_LE==OP_Le); testcase(op==OP_Le); VdbeCoverageIf(v,op==OP_Le);
86855      assert(TK_GT==OP_Gt); testcase(op==OP_Gt); VdbeCoverageIf(v,op==OP_Gt);
86856      assert(TK_GE==OP_Ge); testcase(op==OP_Ge); VdbeCoverageIf(v,op==OP_Ge);
86857      assert(TK_EQ==OP_Eq); testcase(op==OP_Eq); VdbeCoverageIf(v,op==OP_Eq);
86858      assert(TK_NE==OP_Ne); testcase(op==OP_Ne); VdbeCoverageIf(v,op==OP_Ne);
86859      testcase( regFree1==0 );
86860      testcase( regFree2==0 );
86861      break;
86862    }
86863    case TK_IS:
86864    case TK_ISNOT: {
86865      testcase( op==TK_IS );
86866      testcase( op==TK_ISNOT );
86867      r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
86868      r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, &regFree2);
86869      op = (op==TK_IS) ? TK_EQ : TK_NE;
86870      codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op,
86871                  r1, r2, inReg, SQLITE_STOREP2 | SQLITE_NULLEQ);
86872      VdbeCoverageIf(v, op==TK_EQ);
86873      VdbeCoverageIf(v, op==TK_NE);
86874      testcase( regFree1==0 );
86875      testcase( regFree2==0 );
86876      break;
86877    }
86878    case TK_AND:
86879    case TK_OR:
86880    case TK_PLUS:
86881    case TK_STAR:
86882    case TK_MINUS:
86883    case TK_REM:
86884    case TK_BITAND:
86885    case TK_BITOR:
86886    case TK_SLASH:
86887    case TK_LSHIFT:
86888    case TK_RSHIFT:
86889    case TK_CONCAT: {
86890      assert( TK_AND==OP_And );            testcase( op==TK_AND );
86891      assert( TK_OR==OP_Or );              testcase( op==TK_OR );
86892      assert( TK_PLUS==OP_Add );           testcase( op==TK_PLUS );
86893      assert( TK_MINUS==OP_Subtract );     testcase( op==TK_MINUS );
86894      assert( TK_REM==OP_Remainder );      testcase( op==TK_REM );
86895      assert( TK_BITAND==OP_BitAnd );      testcase( op==TK_BITAND );
86896      assert( TK_BITOR==OP_BitOr );        testcase( op==TK_BITOR );
86897      assert( TK_SLASH==OP_Divide );       testcase( op==TK_SLASH );
86898      assert( TK_LSHIFT==OP_ShiftLeft );   testcase( op==TK_LSHIFT );
86899      assert( TK_RSHIFT==OP_ShiftRight );  testcase( op==TK_RSHIFT );
86900      assert( TK_CONCAT==OP_Concat );      testcase( op==TK_CONCAT );
86901      r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
86902      r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, &regFree2);
86903      sqlite3VdbeAddOp3(v, op, r2, r1, target);
86904      testcase( regFree1==0 );
86905      testcase( regFree2==0 );
86906      break;
86907    }
86908    case TK_UMINUS: {
86909      Expr *pLeft = pExpr->pLeft;
86910      assert( pLeft );
86911      if( pLeft->op==TK_INTEGER ){
86912        codeInteger(pParse, pLeft, 1, target);
86913#ifndef SQLITE_OMIT_FLOATING_POINT
86914      }else if( pLeft->op==TK_FLOAT ){
86915        assert( !ExprHasProperty(pExpr, EP_IntValue) );
86916        codeReal(v, pLeft->u.zToken, 1, target);
86917#endif
86918      }else{
86919        tempX.op = TK_INTEGER;
86920        tempX.flags = EP_IntValue|EP_TokenOnly;
86921        tempX.u.iValue = 0;
86922        r1 = sqlite3ExprCodeTemp(pParse, &tempX, &regFree1);
86923        r2 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree2);
86924        sqlite3VdbeAddOp3(v, OP_Subtract, r2, r1, target);
86925        testcase( regFree2==0 );
86926      }
86927      inReg = target;
86928      break;
86929    }
86930    case TK_BITNOT:
86931    case TK_NOT: {
86932      assert( TK_BITNOT==OP_BitNot );   testcase( op==TK_BITNOT );
86933      assert( TK_NOT==OP_Not );         testcase( op==TK_NOT );
86934      r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
86935      testcase( regFree1==0 );
86936      inReg = target;
86937      sqlite3VdbeAddOp2(v, op, r1, inReg);
86938      break;
86939    }
86940    case TK_ISNULL:
86941    case TK_NOTNULL: {
86942      int addr;
86943      assert( TK_ISNULL==OP_IsNull );   testcase( op==TK_ISNULL );
86944      assert( TK_NOTNULL==OP_NotNull ); testcase( op==TK_NOTNULL );
86945      sqlite3VdbeAddOp2(v, OP_Integer, 1, target);
86946      r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
86947      testcase( regFree1==0 );
86948      addr = sqlite3VdbeAddOp1(v, op, r1);
86949      VdbeCoverageIf(v, op==TK_ISNULL);
86950      VdbeCoverageIf(v, op==TK_NOTNULL);
86951      sqlite3VdbeAddOp2(v, OP_Integer, 0, target);
86952      sqlite3VdbeJumpHere(v, addr);
86953      break;
86954    }
86955    case TK_AGG_FUNCTION: {
86956      AggInfo *pInfo = pExpr->pAggInfo;
86957      if( pInfo==0 ){
86958        assert( !ExprHasProperty(pExpr, EP_IntValue) );
86959        sqlite3ErrorMsg(pParse, "misuse of aggregate: %s()", pExpr->u.zToken);
86960      }else{
86961        inReg = pInfo->aFunc[pExpr->iAgg].iMem;
86962      }
86963      break;
86964    }
86965    case TK_FUNCTION: {
86966      ExprList *pFarg;       /* List of function arguments */
86967      int nFarg;             /* Number of function arguments */
86968      FuncDef *pDef;         /* The function definition object */
86969      int nId;               /* Length of the function name in bytes */
86970      const char *zId;       /* The function name */
86971      u32 constMask = 0;     /* Mask of function arguments that are constant */
86972      int i;                 /* Loop counter */
86973      u8 enc = ENC(db);      /* The text encoding used by this database */
86974      CollSeq *pColl = 0;    /* A collating sequence */
86975
86976      assert( !ExprHasProperty(pExpr, EP_xIsSelect) );
86977      if( ExprHasProperty(pExpr, EP_TokenOnly) ){
86978        pFarg = 0;
86979      }else{
86980        pFarg = pExpr->x.pList;
86981      }
86982      nFarg = pFarg ? pFarg->nExpr : 0;
86983      assert( !ExprHasProperty(pExpr, EP_IntValue) );
86984      zId = pExpr->u.zToken;
86985      nId = sqlite3Strlen30(zId);
86986      pDef = sqlite3FindFunction(db, zId, nId, nFarg, enc, 0);
86987      if( pDef==0 || pDef->xFunc==0 ){
86988        sqlite3ErrorMsg(pParse, "unknown function: %.*s()", nId, zId);
86989        break;
86990      }
86991
86992      /* Attempt a direct implementation of the built-in COALESCE() and
86993      ** IFNULL() functions.  This avoids unnecessary evaluation of
86994      ** arguments past the first non-NULL argument.
86995      */
86996      if( pDef->funcFlags & SQLITE_FUNC_COALESCE ){
86997        int endCoalesce = sqlite3VdbeMakeLabel(v);
86998        assert( nFarg>=2 );
86999        sqlite3ExprCode(pParse, pFarg->a[0].pExpr, target);
87000        for(i=1; i<nFarg; i++){
87001          sqlite3VdbeAddOp2(v, OP_NotNull, target, endCoalesce);
87002          VdbeCoverage(v);
87003          sqlite3ExprCacheRemove(pParse, target, 1);
87004          sqlite3ExprCachePush(pParse);
87005          sqlite3ExprCode(pParse, pFarg->a[i].pExpr, target);
87006          sqlite3ExprCachePop(pParse);
87007        }
87008        sqlite3VdbeResolveLabel(v, endCoalesce);
87009        break;
87010      }
87011
87012      /* The UNLIKELY() function is a no-op.  The result is the value
87013      ** of the first argument.
87014      */
87015      if( pDef->funcFlags & SQLITE_FUNC_UNLIKELY ){
87016        assert( nFarg>=1 );
87017        inReg = sqlite3ExprCodeTarget(pParse, pFarg->a[0].pExpr, target);
87018        break;
87019      }
87020
87021      for(i=0; i<nFarg; i++){
87022        if( i<32 && sqlite3ExprIsConstant(pFarg->a[i].pExpr) ){
87023          testcase( i==31 );
87024          constMask |= MASKBIT32(i);
87025        }
87026        if( (pDef->funcFlags & SQLITE_FUNC_NEEDCOLL)!=0 && !pColl ){
87027          pColl = sqlite3ExprCollSeq(pParse, pFarg->a[i].pExpr);
87028        }
87029      }
87030      if( pFarg ){
87031        if( constMask ){
87032          r1 = pParse->nMem+1;
87033          pParse->nMem += nFarg;
87034        }else{
87035          r1 = sqlite3GetTempRange(pParse, nFarg);
87036        }
87037
87038        /* For length() and typeof() functions with a column argument,
87039        ** set the P5 parameter to the OP_Column opcode to OPFLAG_LENGTHARG
87040        ** or OPFLAG_TYPEOFARG respectively, to avoid unnecessary data
87041        ** loading.
87042        */
87043        if( (pDef->funcFlags & (SQLITE_FUNC_LENGTH|SQLITE_FUNC_TYPEOF))!=0 ){
87044          u8 exprOp;
87045          assert( nFarg==1 );
87046          assert( pFarg->a[0].pExpr!=0 );
87047          exprOp = pFarg->a[0].pExpr->op;
87048          if( exprOp==TK_COLUMN || exprOp==TK_AGG_COLUMN ){
87049            assert( SQLITE_FUNC_LENGTH==OPFLAG_LENGTHARG );
87050            assert( SQLITE_FUNC_TYPEOF==OPFLAG_TYPEOFARG );
87051            testcase( pDef->funcFlags & OPFLAG_LENGTHARG );
87052            pFarg->a[0].pExpr->op2 =
87053                  pDef->funcFlags & (OPFLAG_LENGTHARG|OPFLAG_TYPEOFARG);
87054          }
87055        }
87056
87057        sqlite3ExprCachePush(pParse);     /* Ticket 2ea2425d34be */
87058        sqlite3ExprCodeExprList(pParse, pFarg, r1,
87059                                SQLITE_ECEL_DUP|SQLITE_ECEL_FACTOR);
87060        sqlite3ExprCachePop(pParse);      /* Ticket 2ea2425d34be */
87061      }else{
87062        r1 = 0;
87063      }
87064#ifndef SQLITE_OMIT_VIRTUALTABLE
87065      /* Possibly overload the function if the first argument is
87066      ** a virtual table column.
87067      **
87068      ** For infix functions (LIKE, GLOB, REGEXP, and MATCH) use the
87069      ** second argument, not the first, as the argument to test to
87070      ** see if it is a column in a virtual table.  This is done because
87071      ** the left operand of infix functions (the operand we want to
87072      ** control overloading) ends up as the second argument to the
87073      ** function.  The expression "A glob B" is equivalent to
87074      ** "glob(B,A).  We want to use the A in "A glob B" to test
87075      ** for function overloading.  But we use the B term in "glob(B,A)".
87076      */
87077      if( nFarg>=2 && (pExpr->flags & EP_InfixFunc) ){
87078        pDef = sqlite3VtabOverloadFunction(db, pDef, nFarg, pFarg->a[1].pExpr);
87079      }else if( nFarg>0 ){
87080        pDef = sqlite3VtabOverloadFunction(db, pDef, nFarg, pFarg->a[0].pExpr);
87081      }
87082#endif
87083      if( pDef->funcFlags & SQLITE_FUNC_NEEDCOLL ){
87084        if( !pColl ) pColl = db->pDfltColl;
87085        sqlite3VdbeAddOp4(v, OP_CollSeq, 0, 0, 0, (char *)pColl, P4_COLLSEQ);
87086      }
87087      sqlite3VdbeAddOp4(v, OP_Function0, constMask, r1, target,
87088                        (char*)pDef, P4_FUNCDEF);
87089      sqlite3VdbeChangeP5(v, (u8)nFarg);
87090      if( nFarg && constMask==0 ){
87091        sqlite3ReleaseTempRange(pParse, r1, nFarg);
87092      }
87093      break;
87094    }
87095#ifndef SQLITE_OMIT_SUBQUERY
87096    case TK_EXISTS:
87097    case TK_SELECT: {
87098      testcase( op==TK_EXISTS );
87099      testcase( op==TK_SELECT );
87100      inReg = sqlite3CodeSubselect(pParse, pExpr, 0, 0);
87101      break;
87102    }
87103    case TK_IN: {
87104      int destIfFalse = sqlite3VdbeMakeLabel(v);
87105      int destIfNull = sqlite3VdbeMakeLabel(v);
87106      sqlite3VdbeAddOp2(v, OP_Null, 0, target);
87107      sqlite3ExprCodeIN(pParse, pExpr, destIfFalse, destIfNull);
87108      sqlite3VdbeAddOp2(v, OP_Integer, 1, target);
87109      sqlite3VdbeResolveLabel(v, destIfFalse);
87110      sqlite3VdbeAddOp2(v, OP_AddImm, target, 0);
87111      sqlite3VdbeResolveLabel(v, destIfNull);
87112      break;
87113    }
87114#endif /* SQLITE_OMIT_SUBQUERY */
87115
87116
87117    /*
87118    **    x BETWEEN y AND z
87119    **
87120    ** This is equivalent to
87121    **
87122    **    x>=y AND x<=z
87123    **
87124    ** X is stored in pExpr->pLeft.
87125    ** Y is stored in pExpr->pList->a[0].pExpr.
87126    ** Z is stored in pExpr->pList->a[1].pExpr.
87127    */
87128    case TK_BETWEEN: {
87129      Expr *pLeft = pExpr->pLeft;
87130      struct ExprList_item *pLItem = pExpr->x.pList->a;
87131      Expr *pRight = pLItem->pExpr;
87132
87133      r1 = sqlite3ExprCodeTemp(pParse, pLeft, &regFree1);
87134      r2 = sqlite3ExprCodeTemp(pParse, pRight, &regFree2);
87135      testcase( regFree1==0 );
87136      testcase( regFree2==0 );
87137      r3 = sqlite3GetTempReg(pParse);
87138      r4 = sqlite3GetTempReg(pParse);
87139      codeCompare(pParse, pLeft, pRight, OP_Ge,
87140                  r1, r2, r3, SQLITE_STOREP2);  VdbeCoverage(v);
87141      pLItem++;
87142      pRight = pLItem->pExpr;
87143      sqlite3ReleaseTempReg(pParse, regFree2);
87144      r2 = sqlite3ExprCodeTemp(pParse, pRight, &regFree2);
87145      testcase( regFree2==0 );
87146      codeCompare(pParse, pLeft, pRight, OP_Le, r1, r2, r4, SQLITE_STOREP2);
87147      VdbeCoverage(v);
87148      sqlite3VdbeAddOp3(v, OP_And, r3, r4, target);
87149      sqlite3ReleaseTempReg(pParse, r3);
87150      sqlite3ReleaseTempReg(pParse, r4);
87151      break;
87152    }
87153    case TK_COLLATE:
87154    case TK_UPLUS: {
87155      inReg = sqlite3ExprCodeTarget(pParse, pExpr->pLeft, target);
87156      break;
87157    }
87158
87159    case TK_TRIGGER: {
87160      /* If the opcode is TK_TRIGGER, then the expression is a reference
87161      ** to a column in the new.* or old.* pseudo-tables available to
87162      ** trigger programs. In this case Expr.iTable is set to 1 for the
87163      ** new.* pseudo-table, or 0 for the old.* pseudo-table. Expr.iColumn
87164      ** is set to the column of the pseudo-table to read, or to -1 to
87165      ** read the rowid field.
87166      **
87167      ** The expression is implemented using an OP_Param opcode. The p1
87168      ** parameter is set to 0 for an old.rowid reference, or to (i+1)
87169      ** to reference another column of the old.* pseudo-table, where
87170      ** i is the index of the column. For a new.rowid reference, p1 is
87171      ** set to (n+1), where n is the number of columns in each pseudo-table.
87172      ** For a reference to any other column in the new.* pseudo-table, p1
87173      ** is set to (n+2+i), where n and i are as defined previously. For
87174      ** example, if the table on which triggers are being fired is
87175      ** declared as:
87176      **
87177      **   CREATE TABLE t1(a, b);
87178      **
87179      ** Then p1 is interpreted as follows:
87180      **
87181      **   p1==0   ->    old.rowid     p1==3   ->    new.rowid
87182      **   p1==1   ->    old.a         p1==4   ->    new.a
87183      **   p1==2   ->    old.b         p1==5   ->    new.b
87184      */
87185      Table *pTab = pExpr->pTab;
87186      int p1 = pExpr->iTable * (pTab->nCol+1) + 1 + pExpr->iColumn;
87187
87188      assert( pExpr->iTable==0 || pExpr->iTable==1 );
87189      assert( pExpr->iColumn>=-1 && pExpr->iColumn<pTab->nCol );
87190      assert( pTab->iPKey<0 || pExpr->iColumn!=pTab->iPKey );
87191      assert( p1>=0 && p1<(pTab->nCol*2+2) );
87192
87193      sqlite3VdbeAddOp2(v, OP_Param, p1, target);
87194      VdbeComment((v, "%s.%s -> $%d",
87195        (pExpr->iTable ? "new" : "old"),
87196        (pExpr->iColumn<0 ? "rowid" : pExpr->pTab->aCol[pExpr->iColumn].zName),
87197        target
87198      ));
87199
87200#ifndef SQLITE_OMIT_FLOATING_POINT
87201      /* If the column has REAL affinity, it may currently be stored as an
87202      ** integer. Use OP_RealAffinity to make sure it is really real.
87203      **
87204      ** EVIDENCE-OF: R-60985-57662 SQLite will convert the value back to
87205      ** floating point when extracting it from the record.  */
87206      if( pExpr->iColumn>=0
87207       && pTab->aCol[pExpr->iColumn].affinity==SQLITE_AFF_REAL
87208      ){
87209        sqlite3VdbeAddOp1(v, OP_RealAffinity, target);
87210      }
87211#endif
87212      break;
87213    }
87214
87215
87216    /*
87217    ** Form A:
87218    **   CASE x WHEN e1 THEN r1 WHEN e2 THEN r2 ... WHEN eN THEN rN ELSE y END
87219    **
87220    ** Form B:
87221    **   CASE WHEN e1 THEN r1 WHEN e2 THEN r2 ... WHEN eN THEN rN ELSE y END
87222    **
87223    ** Form A is can be transformed into the equivalent form B as follows:
87224    **   CASE WHEN x=e1 THEN r1 WHEN x=e2 THEN r2 ...
87225    **        WHEN x=eN THEN rN ELSE y END
87226    **
87227    ** X (if it exists) is in pExpr->pLeft.
87228    ** Y is in the last element of pExpr->x.pList if pExpr->x.pList->nExpr is
87229    ** odd.  The Y is also optional.  If the number of elements in x.pList
87230    ** is even, then Y is omitted and the "otherwise" result is NULL.
87231    ** Ei is in pExpr->pList->a[i*2] and Ri is pExpr->pList->a[i*2+1].
87232    **
87233    ** The result of the expression is the Ri for the first matching Ei,
87234    ** or if there is no matching Ei, the ELSE term Y, or if there is
87235    ** no ELSE term, NULL.
87236    */
87237    default: assert( op==TK_CASE ); {
87238      int endLabel;                     /* GOTO label for end of CASE stmt */
87239      int nextCase;                     /* GOTO label for next WHEN clause */
87240      int nExpr;                        /* 2x number of WHEN terms */
87241      int i;                            /* Loop counter */
87242      ExprList *pEList;                 /* List of WHEN terms */
87243      struct ExprList_item *aListelem;  /* Array of WHEN terms */
87244      Expr opCompare;                   /* The X==Ei expression */
87245      Expr *pX;                         /* The X expression */
87246      Expr *pTest = 0;                  /* X==Ei (form A) or just Ei (form B) */
87247      VVA_ONLY( int iCacheLevel = pParse->iCacheLevel; )
87248
87249      assert( !ExprHasProperty(pExpr, EP_xIsSelect) && pExpr->x.pList );
87250      assert(pExpr->x.pList->nExpr > 0);
87251      pEList = pExpr->x.pList;
87252      aListelem = pEList->a;
87253      nExpr = pEList->nExpr;
87254      endLabel = sqlite3VdbeMakeLabel(v);
87255      if( (pX = pExpr->pLeft)!=0 ){
87256        tempX = *pX;
87257        testcase( pX->op==TK_COLUMN );
87258        exprToRegister(&tempX, sqlite3ExprCodeTemp(pParse, pX, &regFree1));
87259        testcase( regFree1==0 );
87260        opCompare.op = TK_EQ;
87261        opCompare.pLeft = &tempX;
87262        pTest = &opCompare;
87263        /* Ticket b351d95f9cd5ef17e9d9dbae18f5ca8611190001:
87264        ** The value in regFree1 might get SCopy-ed into the file result.
87265        ** So make sure that the regFree1 register is not reused for other
87266        ** purposes and possibly overwritten.  */
87267        regFree1 = 0;
87268      }
87269      for(i=0; i<nExpr-1; i=i+2){
87270        sqlite3ExprCachePush(pParse);
87271        if( pX ){
87272          assert( pTest!=0 );
87273          opCompare.pRight = aListelem[i].pExpr;
87274        }else{
87275          pTest = aListelem[i].pExpr;
87276        }
87277        nextCase = sqlite3VdbeMakeLabel(v);
87278        testcase( pTest->op==TK_COLUMN );
87279        sqlite3ExprIfFalse(pParse, pTest, nextCase, SQLITE_JUMPIFNULL);
87280        testcase( aListelem[i+1].pExpr->op==TK_COLUMN );
87281        sqlite3ExprCode(pParse, aListelem[i+1].pExpr, target);
87282        sqlite3VdbeAddOp2(v, OP_Goto, 0, endLabel);
87283        sqlite3ExprCachePop(pParse);
87284        sqlite3VdbeResolveLabel(v, nextCase);
87285      }
87286      if( (nExpr&1)!=0 ){
87287        sqlite3ExprCachePush(pParse);
87288        sqlite3ExprCode(pParse, pEList->a[nExpr-1].pExpr, target);
87289        sqlite3ExprCachePop(pParse);
87290      }else{
87291        sqlite3VdbeAddOp2(v, OP_Null, 0, target);
87292      }
87293      assert( db->mallocFailed || pParse->nErr>0
87294           || pParse->iCacheLevel==iCacheLevel );
87295      sqlite3VdbeResolveLabel(v, endLabel);
87296      break;
87297    }
87298#ifndef SQLITE_OMIT_TRIGGER
87299    case TK_RAISE: {
87300      assert( pExpr->affinity==OE_Rollback
87301           || pExpr->affinity==OE_Abort
87302           || pExpr->affinity==OE_Fail
87303           || pExpr->affinity==OE_Ignore
87304      );
87305      if( !pParse->pTriggerTab ){
87306        sqlite3ErrorMsg(pParse,
87307                       "RAISE() may only be used within a trigger-program");
87308        return 0;
87309      }
87310      if( pExpr->affinity==OE_Abort ){
87311        sqlite3MayAbort(pParse);
87312      }
87313      assert( !ExprHasProperty(pExpr, EP_IntValue) );
87314      if( pExpr->affinity==OE_Ignore ){
87315        sqlite3VdbeAddOp4(
87316            v, OP_Halt, SQLITE_OK, OE_Ignore, 0, pExpr->u.zToken,0);
87317        VdbeCoverage(v);
87318      }else{
87319        sqlite3HaltConstraint(pParse, SQLITE_CONSTRAINT_TRIGGER,
87320                              pExpr->affinity, pExpr->u.zToken, 0, 0);
87321      }
87322
87323      break;
87324    }
87325#endif
87326  }
87327  sqlite3ReleaseTempReg(pParse, regFree1);
87328  sqlite3ReleaseTempReg(pParse, regFree2);
87329  return inReg;
87330}
87331
87332/*
87333** Factor out the code of the given expression to initialization time.
87334*/
87335SQLITE_PRIVATE void sqlite3ExprCodeAtInit(
87336  Parse *pParse,    /* Parsing context */
87337  Expr *pExpr,      /* The expression to code when the VDBE initializes */
87338  int regDest,      /* Store the value in this register */
87339  u8 reusable       /* True if this expression is reusable */
87340){
87341  ExprList *p;
87342  assert( ConstFactorOk(pParse) );
87343  p = pParse->pConstExpr;
87344  pExpr = sqlite3ExprDup(pParse->db, pExpr, 0);
87345  p = sqlite3ExprListAppend(pParse, p, pExpr);
87346  if( p ){
87347     struct ExprList_item *pItem = &p->a[p->nExpr-1];
87348     pItem->u.iConstExprReg = regDest;
87349     pItem->reusable = reusable;
87350  }
87351  pParse->pConstExpr = p;
87352}
87353
87354/*
87355** Generate code to evaluate an expression and store the results
87356** into a register.  Return the register number where the results
87357** are stored.
87358**
87359** If the register is a temporary register that can be deallocated,
87360** then write its number into *pReg.  If the result register is not
87361** a temporary, then set *pReg to zero.
87362**
87363** If pExpr is a constant, then this routine might generate this
87364** code to fill the register in the initialization section of the
87365** VDBE program, in order to factor it out of the evaluation loop.
87366*/
87367SQLITE_PRIVATE int sqlite3ExprCodeTemp(Parse *pParse, Expr *pExpr, int *pReg){
87368  int r2;
87369  pExpr = sqlite3ExprSkipCollate(pExpr);
87370  if( ConstFactorOk(pParse)
87371   && pExpr->op!=TK_REGISTER
87372   && sqlite3ExprIsConstantNotJoin(pExpr)
87373  ){
87374    ExprList *p = pParse->pConstExpr;
87375    int i;
87376    *pReg  = 0;
87377    if( p ){
87378      struct ExprList_item *pItem;
87379      for(pItem=p->a, i=p->nExpr; i>0; pItem++, i--){
87380        if( pItem->reusable && sqlite3ExprCompare(pItem->pExpr,pExpr,-1)==0 ){
87381          return pItem->u.iConstExprReg;
87382        }
87383      }
87384    }
87385    r2 = ++pParse->nMem;
87386    sqlite3ExprCodeAtInit(pParse, pExpr, r2, 1);
87387  }else{
87388    int r1 = sqlite3GetTempReg(pParse);
87389    r2 = sqlite3ExprCodeTarget(pParse, pExpr, r1);
87390    if( r2==r1 ){
87391      *pReg = r1;
87392    }else{
87393      sqlite3ReleaseTempReg(pParse, r1);
87394      *pReg = 0;
87395    }
87396  }
87397  return r2;
87398}
87399
87400/*
87401** Generate code that will evaluate expression pExpr and store the
87402** results in register target.  The results are guaranteed to appear
87403** in register target.
87404*/
87405SQLITE_PRIVATE void sqlite3ExprCode(Parse *pParse, Expr *pExpr, int target){
87406  int inReg;
87407
87408  assert( target>0 && target<=pParse->nMem );
87409  if( pExpr && pExpr->op==TK_REGISTER ){
87410    sqlite3VdbeAddOp2(pParse->pVdbe, OP_Copy, pExpr->iTable, target);
87411  }else{
87412    inReg = sqlite3ExprCodeTarget(pParse, pExpr, target);
87413    assert( pParse->pVdbe || pParse->db->mallocFailed );
87414    if( inReg!=target && pParse->pVdbe ){
87415      sqlite3VdbeAddOp2(pParse->pVdbe, OP_SCopy, inReg, target);
87416    }
87417  }
87418}
87419
87420/*
87421** Generate code that will evaluate expression pExpr and store the
87422** results in register target.  The results are guaranteed to appear
87423** in register target.  If the expression is constant, then this routine
87424** might choose to code the expression at initialization time.
87425*/
87426SQLITE_PRIVATE void sqlite3ExprCodeFactorable(Parse *pParse, Expr *pExpr, int target){
87427  if( pParse->okConstFactor && sqlite3ExprIsConstant(pExpr) ){
87428    sqlite3ExprCodeAtInit(pParse, pExpr, target, 0);
87429  }else{
87430    sqlite3ExprCode(pParse, pExpr, target);
87431  }
87432}
87433
87434/*
87435** Generate code that evaluates the given expression and puts the result
87436** in register target.
87437**
87438** Also make a copy of the expression results into another "cache" register
87439** and modify the expression so that the next time it is evaluated,
87440** the result is a copy of the cache register.
87441**
87442** This routine is used for expressions that are used multiple
87443** times.  They are evaluated once and the results of the expression
87444** are reused.
87445*/
87446SQLITE_PRIVATE void sqlite3ExprCodeAndCache(Parse *pParse, Expr *pExpr, int target){
87447  Vdbe *v = pParse->pVdbe;
87448  int iMem;
87449
87450  assert( target>0 );
87451  assert( pExpr->op!=TK_REGISTER );
87452  sqlite3ExprCode(pParse, pExpr, target);
87453  iMem = ++pParse->nMem;
87454  sqlite3VdbeAddOp2(v, OP_Copy, target, iMem);
87455  exprToRegister(pExpr, iMem);
87456}
87457
87458/*
87459** Generate code that pushes the value of every element of the given
87460** expression list into a sequence of registers beginning at target.
87461**
87462** Return the number of elements evaluated.
87463**
87464** The SQLITE_ECEL_DUP flag prevents the arguments from being
87465** filled using OP_SCopy.  OP_Copy must be used instead.
87466**
87467** The SQLITE_ECEL_FACTOR argument allows constant arguments to be
87468** factored out into initialization code.
87469*/
87470SQLITE_PRIVATE int sqlite3ExprCodeExprList(
87471  Parse *pParse,     /* Parsing context */
87472  ExprList *pList,   /* The expression list to be coded */
87473  int target,        /* Where to write results */
87474  u8 flags           /* SQLITE_ECEL_* flags */
87475){
87476  struct ExprList_item *pItem;
87477  int i, n;
87478  u8 copyOp = (flags & SQLITE_ECEL_DUP) ? OP_Copy : OP_SCopy;
87479  assert( pList!=0 );
87480  assert( target>0 );
87481  assert( pParse->pVdbe!=0 );  /* Never gets this far otherwise */
87482  n = pList->nExpr;
87483  if( !ConstFactorOk(pParse) ) flags &= ~SQLITE_ECEL_FACTOR;
87484  for(pItem=pList->a, i=0; i<n; i++, pItem++){
87485    Expr *pExpr = pItem->pExpr;
87486    if( (flags & SQLITE_ECEL_FACTOR)!=0 && sqlite3ExprIsConstant(pExpr) ){
87487      sqlite3ExprCodeAtInit(pParse, pExpr, target+i, 0);
87488    }else{
87489      int inReg = sqlite3ExprCodeTarget(pParse, pExpr, target+i);
87490      if( inReg!=target+i ){
87491        VdbeOp *pOp;
87492        Vdbe *v = pParse->pVdbe;
87493        if( copyOp==OP_Copy
87494         && (pOp=sqlite3VdbeGetOp(v, -1))->opcode==OP_Copy
87495         && pOp->p1+pOp->p3+1==inReg
87496         && pOp->p2+pOp->p3+1==target+i
87497        ){
87498          pOp->p3++;
87499        }else{
87500          sqlite3VdbeAddOp2(v, copyOp, inReg, target+i);
87501        }
87502      }
87503    }
87504  }
87505  return n;
87506}
87507
87508/*
87509** Generate code for a BETWEEN operator.
87510**
87511**    x BETWEEN y AND z
87512**
87513** The above is equivalent to
87514**
87515**    x>=y AND x<=z
87516**
87517** Code it as such, taking care to do the common subexpression
87518** elimination of x.
87519*/
87520static void exprCodeBetween(
87521  Parse *pParse,    /* Parsing and code generating context */
87522  Expr *pExpr,      /* The BETWEEN expression */
87523  int dest,         /* Jump here if the jump is taken */
87524  int jumpIfTrue,   /* Take the jump if the BETWEEN is true */
87525  int jumpIfNull    /* Take the jump if the BETWEEN is NULL */
87526){
87527  Expr exprAnd;     /* The AND operator in  x>=y AND x<=z  */
87528  Expr compLeft;    /* The  x>=y  term */
87529  Expr compRight;   /* The  x<=z  term */
87530  Expr exprX;       /* The  x  subexpression */
87531  int regFree1 = 0; /* Temporary use register */
87532
87533  assert( !ExprHasProperty(pExpr, EP_xIsSelect) );
87534  exprX = *pExpr->pLeft;
87535  exprAnd.op = TK_AND;
87536  exprAnd.pLeft = &compLeft;
87537  exprAnd.pRight = &compRight;
87538  compLeft.op = TK_GE;
87539  compLeft.pLeft = &exprX;
87540  compLeft.pRight = pExpr->x.pList->a[0].pExpr;
87541  compRight.op = TK_LE;
87542  compRight.pLeft = &exprX;
87543  compRight.pRight = pExpr->x.pList->a[1].pExpr;
87544  exprToRegister(&exprX, sqlite3ExprCodeTemp(pParse, &exprX, &regFree1));
87545  if( jumpIfTrue ){
87546    sqlite3ExprIfTrue(pParse, &exprAnd, dest, jumpIfNull);
87547  }else{
87548    sqlite3ExprIfFalse(pParse, &exprAnd, dest, jumpIfNull);
87549  }
87550  sqlite3ReleaseTempReg(pParse, regFree1);
87551
87552  /* Ensure adequate test coverage */
87553  testcase( jumpIfTrue==0 && jumpIfNull==0 && regFree1==0 );
87554  testcase( jumpIfTrue==0 && jumpIfNull==0 && regFree1!=0 );
87555  testcase( jumpIfTrue==0 && jumpIfNull!=0 && regFree1==0 );
87556  testcase( jumpIfTrue==0 && jumpIfNull!=0 && regFree1!=0 );
87557  testcase( jumpIfTrue!=0 && jumpIfNull==0 && regFree1==0 );
87558  testcase( jumpIfTrue!=0 && jumpIfNull==0 && regFree1!=0 );
87559  testcase( jumpIfTrue!=0 && jumpIfNull!=0 && regFree1==0 );
87560  testcase( jumpIfTrue!=0 && jumpIfNull!=0 && regFree1!=0 );
87561}
87562
87563/*
87564** Generate code for a boolean expression such that a jump is made
87565** to the label "dest" if the expression is true but execution
87566** continues straight thru if the expression is false.
87567**
87568** If the expression evaluates to NULL (neither true nor false), then
87569** take the jump if the jumpIfNull flag is SQLITE_JUMPIFNULL.
87570**
87571** This code depends on the fact that certain token values (ex: TK_EQ)
87572** are the same as opcode values (ex: OP_Eq) that implement the corresponding
87573** operation.  Special comments in vdbe.c and the mkopcodeh.awk script in
87574** the make process cause these values to align.  Assert()s in the code
87575** below verify that the numbers are aligned correctly.
87576*/
87577SQLITE_PRIVATE void sqlite3ExprIfTrue(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
87578  Vdbe *v = pParse->pVdbe;
87579  int op = 0;
87580  int regFree1 = 0;
87581  int regFree2 = 0;
87582  int r1, r2;
87583
87584  assert( jumpIfNull==SQLITE_JUMPIFNULL || jumpIfNull==0 );
87585  if( NEVER(v==0) )     return;  /* Existence of VDBE checked by caller */
87586  if( NEVER(pExpr==0) ) return;  /* No way this can happen */
87587  op = pExpr->op;
87588  switch( op ){
87589    case TK_AND: {
87590      int d2 = sqlite3VdbeMakeLabel(v);
87591      testcase( jumpIfNull==0 );
87592      sqlite3ExprIfFalse(pParse, pExpr->pLeft, d2,jumpIfNull^SQLITE_JUMPIFNULL);
87593      sqlite3ExprCachePush(pParse);
87594      sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
87595      sqlite3VdbeResolveLabel(v, d2);
87596      sqlite3ExprCachePop(pParse);
87597      break;
87598    }
87599    case TK_OR: {
87600      testcase( jumpIfNull==0 );
87601      sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
87602      sqlite3ExprCachePush(pParse);
87603      sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
87604      sqlite3ExprCachePop(pParse);
87605      break;
87606    }
87607    case TK_NOT: {
87608      testcase( jumpIfNull==0 );
87609      sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
87610      break;
87611    }
87612    case TK_LT:
87613    case TK_LE:
87614    case TK_GT:
87615    case TK_GE:
87616    case TK_NE:
87617    case TK_EQ: {
87618      testcase( jumpIfNull==0 );
87619      r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
87620      r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, &regFree2);
87621      codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op,
87622                  r1, r2, dest, jumpIfNull);
87623      assert(TK_LT==OP_Lt); testcase(op==OP_Lt); VdbeCoverageIf(v,op==OP_Lt);
87624      assert(TK_LE==OP_Le); testcase(op==OP_Le); VdbeCoverageIf(v,op==OP_Le);
87625      assert(TK_GT==OP_Gt); testcase(op==OP_Gt); VdbeCoverageIf(v,op==OP_Gt);
87626      assert(TK_GE==OP_Ge); testcase(op==OP_Ge); VdbeCoverageIf(v,op==OP_Ge);
87627      assert(TK_EQ==OP_Eq); testcase(op==OP_Eq); VdbeCoverageIf(v,op==OP_Eq);
87628      assert(TK_NE==OP_Ne); testcase(op==OP_Ne); VdbeCoverageIf(v,op==OP_Ne);
87629      testcase( regFree1==0 );
87630      testcase( regFree2==0 );
87631      break;
87632    }
87633    case TK_IS:
87634    case TK_ISNOT: {
87635      testcase( op==TK_IS );
87636      testcase( op==TK_ISNOT );
87637      r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
87638      r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, &regFree2);
87639      op = (op==TK_IS) ? TK_EQ : TK_NE;
87640      codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op,
87641                  r1, r2, dest, SQLITE_NULLEQ);
87642      VdbeCoverageIf(v, op==TK_EQ);
87643      VdbeCoverageIf(v, op==TK_NE);
87644      testcase( regFree1==0 );
87645      testcase( regFree2==0 );
87646      break;
87647    }
87648    case TK_ISNULL:
87649    case TK_NOTNULL: {
87650      assert( TK_ISNULL==OP_IsNull );   testcase( op==TK_ISNULL );
87651      assert( TK_NOTNULL==OP_NotNull ); testcase( op==TK_NOTNULL );
87652      r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
87653      sqlite3VdbeAddOp2(v, op, r1, dest);
87654      VdbeCoverageIf(v, op==TK_ISNULL);
87655      VdbeCoverageIf(v, op==TK_NOTNULL);
87656      testcase( regFree1==0 );
87657      break;
87658    }
87659    case TK_BETWEEN: {
87660      testcase( jumpIfNull==0 );
87661      exprCodeBetween(pParse, pExpr, dest, 1, jumpIfNull);
87662      break;
87663    }
87664#ifndef SQLITE_OMIT_SUBQUERY
87665    case TK_IN: {
87666      int destIfFalse = sqlite3VdbeMakeLabel(v);
87667      int destIfNull = jumpIfNull ? dest : destIfFalse;
87668      sqlite3ExprCodeIN(pParse, pExpr, destIfFalse, destIfNull);
87669      sqlite3VdbeAddOp2(v, OP_Goto, 0, dest);
87670      sqlite3VdbeResolveLabel(v, destIfFalse);
87671      break;
87672    }
87673#endif
87674    default: {
87675      if( exprAlwaysTrue(pExpr) ){
87676        sqlite3VdbeAddOp2(v, OP_Goto, 0, dest);
87677      }else if( exprAlwaysFalse(pExpr) ){
87678        /* No-op */
87679      }else{
87680        r1 = sqlite3ExprCodeTemp(pParse, pExpr, &regFree1);
87681        sqlite3VdbeAddOp3(v, OP_If, r1, dest, jumpIfNull!=0);
87682        VdbeCoverage(v);
87683        testcase( regFree1==0 );
87684        testcase( jumpIfNull==0 );
87685      }
87686      break;
87687    }
87688  }
87689  sqlite3ReleaseTempReg(pParse, regFree1);
87690  sqlite3ReleaseTempReg(pParse, regFree2);
87691}
87692
87693/*
87694** Generate code for a boolean expression such that a jump is made
87695** to the label "dest" if the expression is false but execution
87696** continues straight thru if the expression is true.
87697**
87698** If the expression evaluates to NULL (neither true nor false) then
87699** jump if jumpIfNull is SQLITE_JUMPIFNULL or fall through if jumpIfNull
87700** is 0.
87701*/
87702SQLITE_PRIVATE void sqlite3ExprIfFalse(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
87703  Vdbe *v = pParse->pVdbe;
87704  int op = 0;
87705  int regFree1 = 0;
87706  int regFree2 = 0;
87707  int r1, r2;
87708
87709  assert( jumpIfNull==SQLITE_JUMPIFNULL || jumpIfNull==0 );
87710  if( NEVER(v==0) ) return; /* Existence of VDBE checked by caller */
87711  if( pExpr==0 )    return;
87712
87713  /* The value of pExpr->op and op are related as follows:
87714  **
87715  **       pExpr->op            op
87716  **       ---------          ----------
87717  **       TK_ISNULL          OP_NotNull
87718  **       TK_NOTNULL         OP_IsNull
87719  **       TK_NE              OP_Eq
87720  **       TK_EQ              OP_Ne
87721  **       TK_GT              OP_Le
87722  **       TK_LE              OP_Gt
87723  **       TK_GE              OP_Lt
87724  **       TK_LT              OP_Ge
87725  **
87726  ** For other values of pExpr->op, op is undefined and unused.
87727  ** The value of TK_ and OP_ constants are arranged such that we
87728  ** can compute the mapping above using the following expression.
87729  ** Assert()s verify that the computation is correct.
87730  */
87731  op = ((pExpr->op+(TK_ISNULL&1))^1)-(TK_ISNULL&1);
87732
87733  /* Verify correct alignment of TK_ and OP_ constants
87734  */
87735  assert( pExpr->op!=TK_ISNULL || op==OP_NotNull );
87736  assert( pExpr->op!=TK_NOTNULL || op==OP_IsNull );
87737  assert( pExpr->op!=TK_NE || op==OP_Eq );
87738  assert( pExpr->op!=TK_EQ || op==OP_Ne );
87739  assert( pExpr->op!=TK_LT || op==OP_Ge );
87740  assert( pExpr->op!=TK_LE || op==OP_Gt );
87741  assert( pExpr->op!=TK_GT || op==OP_Le );
87742  assert( pExpr->op!=TK_GE || op==OP_Lt );
87743
87744  switch( pExpr->op ){
87745    case TK_AND: {
87746      testcase( jumpIfNull==0 );
87747      sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
87748      sqlite3ExprCachePush(pParse);
87749      sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
87750      sqlite3ExprCachePop(pParse);
87751      break;
87752    }
87753    case TK_OR: {
87754      int d2 = sqlite3VdbeMakeLabel(v);
87755      testcase( jumpIfNull==0 );
87756      sqlite3ExprIfTrue(pParse, pExpr->pLeft, d2, jumpIfNull^SQLITE_JUMPIFNULL);
87757      sqlite3ExprCachePush(pParse);
87758      sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
87759      sqlite3VdbeResolveLabel(v, d2);
87760      sqlite3ExprCachePop(pParse);
87761      break;
87762    }
87763    case TK_NOT: {
87764      testcase( jumpIfNull==0 );
87765      sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
87766      break;
87767    }
87768    case TK_LT:
87769    case TK_LE:
87770    case TK_GT:
87771    case TK_GE:
87772    case TK_NE:
87773    case TK_EQ: {
87774      testcase( jumpIfNull==0 );
87775      r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
87776      r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, &regFree2);
87777      codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op,
87778                  r1, r2, dest, jumpIfNull);
87779      assert(TK_LT==OP_Lt); testcase(op==OP_Lt); VdbeCoverageIf(v,op==OP_Lt);
87780      assert(TK_LE==OP_Le); testcase(op==OP_Le); VdbeCoverageIf(v,op==OP_Le);
87781      assert(TK_GT==OP_Gt); testcase(op==OP_Gt); VdbeCoverageIf(v,op==OP_Gt);
87782      assert(TK_GE==OP_Ge); testcase(op==OP_Ge); VdbeCoverageIf(v,op==OP_Ge);
87783      assert(TK_EQ==OP_Eq); testcase(op==OP_Eq); VdbeCoverageIf(v,op==OP_Eq);
87784      assert(TK_NE==OP_Ne); testcase(op==OP_Ne); VdbeCoverageIf(v,op==OP_Ne);
87785      testcase( regFree1==0 );
87786      testcase( regFree2==0 );
87787      break;
87788    }
87789    case TK_IS:
87790    case TK_ISNOT: {
87791      testcase( pExpr->op==TK_IS );
87792      testcase( pExpr->op==TK_ISNOT );
87793      r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
87794      r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, &regFree2);
87795      op = (pExpr->op==TK_IS) ? TK_NE : TK_EQ;
87796      codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op,
87797                  r1, r2, dest, SQLITE_NULLEQ);
87798      VdbeCoverageIf(v, op==TK_EQ);
87799      VdbeCoverageIf(v, op==TK_NE);
87800      testcase( regFree1==0 );
87801      testcase( regFree2==0 );
87802      break;
87803    }
87804    case TK_ISNULL:
87805    case TK_NOTNULL: {
87806      r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
87807      sqlite3VdbeAddOp2(v, op, r1, dest);
87808      testcase( op==TK_ISNULL );   VdbeCoverageIf(v, op==TK_ISNULL);
87809      testcase( op==TK_NOTNULL );  VdbeCoverageIf(v, op==TK_NOTNULL);
87810      testcase( regFree1==0 );
87811      break;
87812    }
87813    case TK_BETWEEN: {
87814      testcase( jumpIfNull==0 );
87815      exprCodeBetween(pParse, pExpr, dest, 0, jumpIfNull);
87816      break;
87817    }
87818#ifndef SQLITE_OMIT_SUBQUERY
87819    case TK_IN: {
87820      if( jumpIfNull ){
87821        sqlite3ExprCodeIN(pParse, pExpr, dest, dest);
87822      }else{
87823        int destIfNull = sqlite3VdbeMakeLabel(v);
87824        sqlite3ExprCodeIN(pParse, pExpr, dest, destIfNull);
87825        sqlite3VdbeResolveLabel(v, destIfNull);
87826      }
87827      break;
87828    }
87829#endif
87830    default: {
87831      if( exprAlwaysFalse(pExpr) ){
87832        sqlite3VdbeAddOp2(v, OP_Goto, 0, dest);
87833      }else if( exprAlwaysTrue(pExpr) ){
87834        /* no-op */
87835      }else{
87836        r1 = sqlite3ExprCodeTemp(pParse, pExpr, &regFree1);
87837        sqlite3VdbeAddOp3(v, OP_IfNot, r1, dest, jumpIfNull!=0);
87838        VdbeCoverage(v);
87839        testcase( regFree1==0 );
87840        testcase( jumpIfNull==0 );
87841      }
87842      break;
87843    }
87844  }
87845  sqlite3ReleaseTempReg(pParse, regFree1);
87846  sqlite3ReleaseTempReg(pParse, regFree2);
87847}
87848
87849/*
87850** Like sqlite3ExprIfFalse() except that a copy is made of pExpr before
87851** code generation, and that copy is deleted after code generation. This
87852** ensures that the original pExpr is unchanged.
87853*/
87854SQLITE_PRIVATE void sqlite3ExprIfFalseDup(Parse *pParse, Expr *pExpr, int dest,int jumpIfNull){
87855  sqlite3 *db = pParse->db;
87856  Expr *pCopy = sqlite3ExprDup(db, pExpr, 0);
87857  if( db->mallocFailed==0 ){
87858    sqlite3ExprIfFalse(pParse, pCopy, dest, jumpIfNull);
87859  }
87860  sqlite3ExprDelete(db, pCopy);
87861}
87862
87863
87864/*
87865** Do a deep comparison of two expression trees.  Return 0 if the two
87866** expressions are completely identical.  Return 1 if they differ only
87867** by a COLLATE operator at the top level.  Return 2 if there are differences
87868** other than the top-level COLLATE operator.
87869**
87870** If any subelement of pB has Expr.iTable==(-1) then it is allowed
87871** to compare equal to an equivalent element in pA with Expr.iTable==iTab.
87872**
87873** The pA side might be using TK_REGISTER.  If that is the case and pB is
87874** not using TK_REGISTER but is otherwise equivalent, then still return 0.
87875**
87876** Sometimes this routine will return 2 even if the two expressions
87877** really are equivalent.  If we cannot prove that the expressions are
87878** identical, we return 2 just to be safe.  So if this routine
87879** returns 2, then you do not really know for certain if the two
87880** expressions are the same.  But if you get a 0 or 1 return, then you
87881** can be sure the expressions are the same.  In the places where
87882** this routine is used, it does not hurt to get an extra 2 - that
87883** just might result in some slightly slower code.  But returning
87884** an incorrect 0 or 1 could lead to a malfunction.
87885*/
87886SQLITE_PRIVATE int sqlite3ExprCompare(Expr *pA, Expr *pB, int iTab){
87887  u32 combinedFlags;
87888  if( pA==0 || pB==0 ){
87889    return pB==pA ? 0 : 2;
87890  }
87891  combinedFlags = pA->flags | pB->flags;
87892  if( combinedFlags & EP_IntValue ){
87893    if( (pA->flags&pB->flags&EP_IntValue)!=0 && pA->u.iValue==pB->u.iValue ){
87894      return 0;
87895    }
87896    return 2;
87897  }
87898  if( pA->op!=pB->op ){
87899    if( pA->op==TK_COLLATE && sqlite3ExprCompare(pA->pLeft, pB, iTab)<2 ){
87900      return 1;
87901    }
87902    if( pB->op==TK_COLLATE && sqlite3ExprCompare(pA, pB->pLeft, iTab)<2 ){
87903      return 1;
87904    }
87905    return 2;
87906  }
87907  if( pA->op!=TK_COLUMN && ALWAYS(pA->op!=TK_AGG_COLUMN) && pA->u.zToken ){
87908    if( strcmp(pA->u.zToken,pB->u.zToken)!=0 ){
87909      return pA->op==TK_COLLATE ? 1 : 2;
87910    }
87911  }
87912  if( (pA->flags & EP_Distinct)!=(pB->flags & EP_Distinct) ) return 2;
87913  if( ALWAYS((combinedFlags & EP_TokenOnly)==0) ){
87914    if( combinedFlags & EP_xIsSelect ) return 2;
87915    if( sqlite3ExprCompare(pA->pLeft, pB->pLeft, iTab) ) return 2;
87916    if( sqlite3ExprCompare(pA->pRight, pB->pRight, iTab) ) return 2;
87917    if( sqlite3ExprListCompare(pA->x.pList, pB->x.pList, iTab) ) return 2;
87918    if( ALWAYS((combinedFlags & EP_Reduced)==0) && pA->op!=TK_STRING ){
87919      if( pA->iColumn!=pB->iColumn ) return 2;
87920      if( pA->iTable!=pB->iTable
87921       && (pA->iTable!=iTab || NEVER(pB->iTable>=0)) ) return 2;
87922    }
87923  }
87924  return 0;
87925}
87926
87927/*
87928** Compare two ExprList objects.  Return 0 if they are identical and
87929** non-zero if they differ in any way.
87930**
87931** If any subelement of pB has Expr.iTable==(-1) then it is allowed
87932** to compare equal to an equivalent element in pA with Expr.iTable==iTab.
87933**
87934** This routine might return non-zero for equivalent ExprLists.  The
87935** only consequence will be disabled optimizations.  But this routine
87936** must never return 0 if the two ExprList objects are different, or
87937** a malfunction will result.
87938**
87939** Two NULL pointers are considered to be the same.  But a NULL pointer
87940** always differs from a non-NULL pointer.
87941*/
87942SQLITE_PRIVATE int sqlite3ExprListCompare(ExprList *pA, ExprList *pB, int iTab){
87943  int i;
87944  if( pA==0 && pB==0 ) return 0;
87945  if( pA==0 || pB==0 ) return 1;
87946  if( pA->nExpr!=pB->nExpr ) return 1;
87947  for(i=0; i<pA->nExpr; i++){
87948    Expr *pExprA = pA->a[i].pExpr;
87949    Expr *pExprB = pB->a[i].pExpr;
87950    if( pA->a[i].sortOrder!=pB->a[i].sortOrder ) return 1;
87951    if( sqlite3ExprCompare(pExprA, pExprB, iTab) ) return 1;
87952  }
87953  return 0;
87954}
87955
87956/*
87957** Return true if we can prove the pE2 will always be true if pE1 is
87958** true.  Return false if we cannot complete the proof or if pE2 might
87959** be false.  Examples:
87960**
87961**     pE1: x==5       pE2: x==5             Result: true
87962**     pE1: x>0        pE2: x==5             Result: false
87963**     pE1: x=21       pE2: x=21 OR y=43     Result: true
87964**     pE1: x!=123     pE2: x IS NOT NULL    Result: true
87965**     pE1: x!=?1      pE2: x IS NOT NULL    Result: true
87966**     pE1: x IS NULL  pE2: x IS NOT NULL    Result: false
87967**     pE1: x IS ?2    pE2: x IS NOT NULL    Reuslt: false
87968**
87969** When comparing TK_COLUMN nodes between pE1 and pE2, if pE2 has
87970** Expr.iTable<0 then assume a table number given by iTab.
87971**
87972** When in doubt, return false.  Returning true might give a performance
87973** improvement.  Returning false might cause a performance reduction, but
87974** it will always give the correct answer and is hence always safe.
87975*/
87976SQLITE_PRIVATE int sqlite3ExprImpliesExpr(Expr *pE1, Expr *pE2, int iTab){
87977  if( sqlite3ExprCompare(pE1, pE2, iTab)==0 ){
87978    return 1;
87979  }
87980  if( pE2->op==TK_OR
87981   && (sqlite3ExprImpliesExpr(pE1, pE2->pLeft, iTab)
87982             || sqlite3ExprImpliesExpr(pE1, pE2->pRight, iTab) )
87983  ){
87984    return 1;
87985  }
87986  if( pE2->op==TK_NOTNULL
87987   && sqlite3ExprCompare(pE1->pLeft, pE2->pLeft, iTab)==0
87988   && (pE1->op!=TK_ISNULL && pE1->op!=TK_IS)
87989  ){
87990    return 1;
87991  }
87992  return 0;
87993}
87994
87995/*
87996** An instance of the following structure is used by the tree walker
87997** to count references to table columns in the arguments of an
87998** aggregate function, in order to implement the
87999** sqlite3FunctionThisSrc() routine.
88000*/
88001struct SrcCount {
88002  SrcList *pSrc;   /* One particular FROM clause in a nested query */
88003  int nThis;       /* Number of references to columns in pSrcList */
88004  int nOther;      /* Number of references to columns in other FROM clauses */
88005};
88006
88007/*
88008** Count the number of references to columns.
88009*/
88010static int exprSrcCount(Walker *pWalker, Expr *pExpr){
88011  /* The NEVER() on the second term is because sqlite3FunctionUsesThisSrc()
88012  ** is always called before sqlite3ExprAnalyzeAggregates() and so the
88013  ** TK_COLUMNs have not yet been converted into TK_AGG_COLUMN.  If
88014  ** sqlite3FunctionUsesThisSrc() is used differently in the future, the
88015  ** NEVER() will need to be removed. */
88016  if( pExpr->op==TK_COLUMN || NEVER(pExpr->op==TK_AGG_COLUMN) ){
88017    int i;
88018    struct SrcCount *p = pWalker->u.pSrcCount;
88019    SrcList *pSrc = p->pSrc;
88020    int nSrc = pSrc ? pSrc->nSrc : 0;
88021    for(i=0; i<nSrc; i++){
88022      if( pExpr->iTable==pSrc->a[i].iCursor ) break;
88023    }
88024    if( i<nSrc ){
88025      p->nThis++;
88026    }else{
88027      p->nOther++;
88028    }
88029  }
88030  return WRC_Continue;
88031}
88032
88033/*
88034** Determine if any of the arguments to the pExpr Function reference
88035** pSrcList.  Return true if they do.  Also return true if the function
88036** has no arguments or has only constant arguments.  Return false if pExpr
88037** references columns but not columns of tables found in pSrcList.
88038*/
88039SQLITE_PRIVATE int sqlite3FunctionUsesThisSrc(Expr *pExpr, SrcList *pSrcList){
88040  Walker w;
88041  struct SrcCount cnt;
88042  assert( pExpr->op==TK_AGG_FUNCTION );
88043  memset(&w, 0, sizeof(w));
88044  w.xExprCallback = exprSrcCount;
88045  w.u.pSrcCount = &cnt;
88046  cnt.pSrc = pSrcList;
88047  cnt.nThis = 0;
88048  cnt.nOther = 0;
88049  sqlite3WalkExprList(&w, pExpr->x.pList);
88050  return cnt.nThis>0 || cnt.nOther==0;
88051}
88052
88053/*
88054** Add a new element to the pAggInfo->aCol[] array.  Return the index of
88055** the new element.  Return a negative number if malloc fails.
88056*/
88057static int addAggInfoColumn(sqlite3 *db, AggInfo *pInfo){
88058  int i;
88059  pInfo->aCol = sqlite3ArrayAllocate(
88060       db,
88061       pInfo->aCol,
88062       sizeof(pInfo->aCol[0]),
88063       &pInfo->nColumn,
88064       &i
88065  );
88066  return i;
88067}
88068
88069/*
88070** Add a new element to the pAggInfo->aFunc[] array.  Return the index of
88071** the new element.  Return a negative number if malloc fails.
88072*/
88073static int addAggInfoFunc(sqlite3 *db, AggInfo *pInfo){
88074  int i;
88075  pInfo->aFunc = sqlite3ArrayAllocate(
88076       db,
88077       pInfo->aFunc,
88078       sizeof(pInfo->aFunc[0]),
88079       &pInfo->nFunc,
88080       &i
88081  );
88082  return i;
88083}
88084
88085/*
88086** This is the xExprCallback for a tree walker.  It is used to
88087** implement sqlite3ExprAnalyzeAggregates().  See sqlite3ExprAnalyzeAggregates
88088** for additional information.
88089*/
88090static int analyzeAggregate(Walker *pWalker, Expr *pExpr){
88091  int i;
88092  NameContext *pNC = pWalker->u.pNC;
88093  Parse *pParse = pNC->pParse;
88094  SrcList *pSrcList = pNC->pSrcList;
88095  AggInfo *pAggInfo = pNC->pAggInfo;
88096
88097  switch( pExpr->op ){
88098    case TK_AGG_COLUMN:
88099    case TK_COLUMN: {
88100      testcase( pExpr->op==TK_AGG_COLUMN );
88101      testcase( pExpr->op==TK_COLUMN );
88102      /* Check to see if the column is in one of the tables in the FROM
88103      ** clause of the aggregate query */
88104      if( ALWAYS(pSrcList!=0) ){
88105        struct SrcList_item *pItem = pSrcList->a;
88106        for(i=0; i<pSrcList->nSrc; i++, pItem++){
88107          struct AggInfo_col *pCol;
88108          assert( !ExprHasProperty(pExpr, EP_TokenOnly|EP_Reduced) );
88109          if( pExpr->iTable==pItem->iCursor ){
88110            /* If we reach this point, it means that pExpr refers to a table
88111            ** that is in the FROM clause of the aggregate query.
88112            **
88113            ** Make an entry for the column in pAggInfo->aCol[] if there
88114            ** is not an entry there already.
88115            */
88116            int k;
88117            pCol = pAggInfo->aCol;
88118            for(k=0; k<pAggInfo->nColumn; k++, pCol++){
88119              if( pCol->iTable==pExpr->iTable &&
88120                  pCol->iColumn==pExpr->iColumn ){
88121                break;
88122              }
88123            }
88124            if( (k>=pAggInfo->nColumn)
88125             && (k = addAggInfoColumn(pParse->db, pAggInfo))>=0
88126            ){
88127              pCol = &pAggInfo->aCol[k];
88128              pCol->pTab = pExpr->pTab;
88129              pCol->iTable = pExpr->iTable;
88130              pCol->iColumn = pExpr->iColumn;
88131              pCol->iMem = ++pParse->nMem;
88132              pCol->iSorterColumn = -1;
88133              pCol->pExpr = pExpr;
88134              if( pAggInfo->pGroupBy ){
88135                int j, n;
88136                ExprList *pGB = pAggInfo->pGroupBy;
88137                struct ExprList_item *pTerm = pGB->a;
88138                n = pGB->nExpr;
88139                for(j=0; j<n; j++, pTerm++){
88140                  Expr *pE = pTerm->pExpr;
88141                  if( pE->op==TK_COLUMN && pE->iTable==pExpr->iTable &&
88142                      pE->iColumn==pExpr->iColumn ){
88143                    pCol->iSorterColumn = j;
88144                    break;
88145                  }
88146                }
88147              }
88148              if( pCol->iSorterColumn<0 ){
88149                pCol->iSorterColumn = pAggInfo->nSortingColumn++;
88150              }
88151            }
88152            /* There is now an entry for pExpr in pAggInfo->aCol[] (either
88153            ** because it was there before or because we just created it).
88154            ** Convert the pExpr to be a TK_AGG_COLUMN referring to that
88155            ** pAggInfo->aCol[] entry.
88156            */
88157            ExprSetVVAProperty(pExpr, EP_NoReduce);
88158            pExpr->pAggInfo = pAggInfo;
88159            pExpr->op = TK_AGG_COLUMN;
88160            pExpr->iAgg = (i16)k;
88161            break;
88162          } /* endif pExpr->iTable==pItem->iCursor */
88163        } /* end loop over pSrcList */
88164      }
88165      return WRC_Prune;
88166    }
88167    case TK_AGG_FUNCTION: {
88168      if( (pNC->ncFlags & NC_InAggFunc)==0
88169       && pWalker->walkerDepth==pExpr->op2
88170      ){
88171        /* Check to see if pExpr is a duplicate of another aggregate
88172        ** function that is already in the pAggInfo structure
88173        */
88174        struct AggInfo_func *pItem = pAggInfo->aFunc;
88175        for(i=0; i<pAggInfo->nFunc; i++, pItem++){
88176          if( sqlite3ExprCompare(pItem->pExpr, pExpr, -1)==0 ){
88177            break;
88178          }
88179        }
88180        if( i>=pAggInfo->nFunc ){
88181          /* pExpr is original.  Make a new entry in pAggInfo->aFunc[]
88182          */
88183          u8 enc = ENC(pParse->db);
88184          i = addAggInfoFunc(pParse->db, pAggInfo);
88185          if( i>=0 ){
88186            assert( !ExprHasProperty(pExpr, EP_xIsSelect) );
88187            pItem = &pAggInfo->aFunc[i];
88188            pItem->pExpr = pExpr;
88189            pItem->iMem = ++pParse->nMem;
88190            assert( !ExprHasProperty(pExpr, EP_IntValue) );
88191            pItem->pFunc = sqlite3FindFunction(pParse->db,
88192                   pExpr->u.zToken, sqlite3Strlen30(pExpr->u.zToken),
88193                   pExpr->x.pList ? pExpr->x.pList->nExpr : 0, enc, 0);
88194            if( pExpr->flags & EP_Distinct ){
88195              pItem->iDistinct = pParse->nTab++;
88196            }else{
88197              pItem->iDistinct = -1;
88198            }
88199          }
88200        }
88201        /* Make pExpr point to the appropriate pAggInfo->aFunc[] entry
88202        */
88203        assert( !ExprHasProperty(pExpr, EP_TokenOnly|EP_Reduced) );
88204        ExprSetVVAProperty(pExpr, EP_NoReduce);
88205        pExpr->iAgg = (i16)i;
88206        pExpr->pAggInfo = pAggInfo;
88207        return WRC_Prune;
88208      }else{
88209        return WRC_Continue;
88210      }
88211    }
88212  }
88213  return WRC_Continue;
88214}
88215static int analyzeAggregatesInSelect(Walker *pWalker, Select *pSelect){
88216  UNUSED_PARAMETER(pWalker);
88217  UNUSED_PARAMETER(pSelect);
88218  return WRC_Continue;
88219}
88220
88221/*
88222** Analyze the pExpr expression looking for aggregate functions and
88223** for variables that need to be added to AggInfo object that pNC->pAggInfo
88224** points to.  Additional entries are made on the AggInfo object as
88225** necessary.
88226**
88227** This routine should only be called after the expression has been
88228** analyzed by sqlite3ResolveExprNames().
88229*/
88230SQLITE_PRIVATE void sqlite3ExprAnalyzeAggregates(NameContext *pNC, Expr *pExpr){
88231  Walker w;
88232  memset(&w, 0, sizeof(w));
88233  w.xExprCallback = analyzeAggregate;
88234  w.xSelectCallback = analyzeAggregatesInSelect;
88235  w.u.pNC = pNC;
88236  assert( pNC->pSrcList!=0 );
88237  sqlite3WalkExpr(&w, pExpr);
88238}
88239
88240/*
88241** Call sqlite3ExprAnalyzeAggregates() for every expression in an
88242** expression list.  Return the number of errors.
88243**
88244** If an error is found, the analysis is cut short.
88245*/
88246SQLITE_PRIVATE void sqlite3ExprAnalyzeAggList(NameContext *pNC, ExprList *pList){
88247  struct ExprList_item *pItem;
88248  int i;
88249  if( pList ){
88250    for(pItem=pList->a, i=0; i<pList->nExpr; i++, pItem++){
88251      sqlite3ExprAnalyzeAggregates(pNC, pItem->pExpr);
88252    }
88253  }
88254}
88255
88256/*
88257** Allocate a single new register for use to hold some intermediate result.
88258*/
88259SQLITE_PRIVATE int sqlite3GetTempReg(Parse *pParse){
88260  if( pParse->nTempReg==0 ){
88261    return ++pParse->nMem;
88262  }
88263  return pParse->aTempReg[--pParse->nTempReg];
88264}
88265
88266/*
88267** Deallocate a register, making available for reuse for some other
88268** purpose.
88269**
88270** If a register is currently being used by the column cache, then
88271** the deallocation is deferred until the column cache line that uses
88272** the register becomes stale.
88273*/
88274SQLITE_PRIVATE void sqlite3ReleaseTempReg(Parse *pParse, int iReg){
88275  if( iReg && pParse->nTempReg<ArraySize(pParse->aTempReg) ){
88276    int i;
88277    struct yColCache *p;
88278    for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){
88279      if( p->iReg==iReg ){
88280        p->tempReg = 1;
88281        return;
88282      }
88283    }
88284    pParse->aTempReg[pParse->nTempReg++] = iReg;
88285  }
88286}
88287
88288/*
88289** Allocate or deallocate a block of nReg consecutive registers
88290*/
88291SQLITE_PRIVATE int sqlite3GetTempRange(Parse *pParse, int nReg){
88292  int i, n;
88293  i = pParse->iRangeReg;
88294  n = pParse->nRangeReg;
88295  if( nReg<=n ){
88296    assert( !usedAsColumnCache(pParse, i, i+n-1) );
88297    pParse->iRangeReg += nReg;
88298    pParse->nRangeReg -= nReg;
88299  }else{
88300    i = pParse->nMem+1;
88301    pParse->nMem += nReg;
88302  }
88303  return i;
88304}
88305SQLITE_PRIVATE void sqlite3ReleaseTempRange(Parse *pParse, int iReg, int nReg){
88306  sqlite3ExprCacheRemove(pParse, iReg, nReg);
88307  if( nReg>pParse->nRangeReg ){
88308    pParse->nRangeReg = nReg;
88309    pParse->iRangeReg = iReg;
88310  }
88311}
88312
88313/*
88314** Mark all temporary registers as being unavailable for reuse.
88315*/
88316SQLITE_PRIVATE void sqlite3ClearTempRegCache(Parse *pParse){
88317  pParse->nTempReg = 0;
88318  pParse->nRangeReg = 0;
88319}
88320
88321/************** End of expr.c ************************************************/
88322/************** Begin file alter.c *******************************************/
88323/*
88324** 2005 February 15
88325**
88326** The author disclaims copyright to this source code.  In place of
88327** a legal notice, here is a blessing:
88328**
88329**    May you do good and not evil.
88330**    May you find forgiveness for yourself and forgive others.
88331**    May you share freely, never taking more than you give.
88332**
88333*************************************************************************
88334** This file contains C code routines that used to generate VDBE code
88335** that implements the ALTER TABLE command.
88336*/
88337/* #include "sqliteInt.h" */
88338
88339/*
88340** The code in this file only exists if we are not omitting the
88341** ALTER TABLE logic from the build.
88342*/
88343#ifndef SQLITE_OMIT_ALTERTABLE
88344
88345
88346/*
88347** This function is used by SQL generated to implement the
88348** ALTER TABLE command. The first argument is the text of a CREATE TABLE or
88349** CREATE INDEX command. The second is a table name. The table name in
88350** the CREATE TABLE or CREATE INDEX statement is replaced with the third
88351** argument and the result returned. Examples:
88352**
88353** sqlite_rename_table('CREATE TABLE abc(a, b, c)', 'def')
88354**     -> 'CREATE TABLE def(a, b, c)'
88355**
88356** sqlite_rename_table('CREATE INDEX i ON abc(a)', 'def')
88357**     -> 'CREATE INDEX i ON def(a, b, c)'
88358*/
88359static void renameTableFunc(
88360  sqlite3_context *context,
88361  int NotUsed,
88362  sqlite3_value **argv
88363){
88364  unsigned char const *zSql = sqlite3_value_text(argv[0]);
88365  unsigned char const *zTableName = sqlite3_value_text(argv[1]);
88366
88367  int token;
88368  Token tname;
88369  unsigned char const *zCsr = zSql;
88370  int len = 0;
88371  char *zRet;
88372
88373  sqlite3 *db = sqlite3_context_db_handle(context);
88374
88375  UNUSED_PARAMETER(NotUsed);
88376
88377  /* The principle used to locate the table name in the CREATE TABLE
88378  ** statement is that the table name is the first non-space token that
88379  ** is immediately followed by a TK_LP or TK_USING token.
88380  */
88381  if( zSql ){
88382    do {
88383      if( !*zCsr ){
88384        /* Ran out of input before finding an opening bracket. Return NULL. */
88385        return;
88386      }
88387
88388      /* Store the token that zCsr points to in tname. */
88389      tname.z = (char*)zCsr;
88390      tname.n = len;
88391
88392      /* Advance zCsr to the next token. Store that token type in 'token',
88393      ** and its length in 'len' (to be used next iteration of this loop).
88394      */
88395      do {
88396        zCsr += len;
88397        len = sqlite3GetToken(zCsr, &token);
88398      } while( token==TK_SPACE );
88399      assert( len>0 );
88400    } while( token!=TK_LP && token!=TK_USING );
88401
88402    zRet = sqlite3MPrintf(db, "%.*s\"%w\"%s", (int)(((u8*)tname.z) - zSql),
88403       zSql, zTableName, tname.z+tname.n);
88404    sqlite3_result_text(context, zRet, -1, SQLITE_DYNAMIC);
88405  }
88406}
88407
88408/*
88409** This C function implements an SQL user function that is used by SQL code
88410** generated by the ALTER TABLE ... RENAME command to modify the definition
88411** of any foreign key constraints that use the table being renamed as the
88412** parent table. It is passed three arguments:
88413**
88414**   1) The complete text of the CREATE TABLE statement being modified,
88415**   2) The old name of the table being renamed, and
88416**   3) The new name of the table being renamed.
88417**
88418** It returns the new CREATE TABLE statement. For example:
88419**
88420**   sqlite_rename_parent('CREATE TABLE t1(a REFERENCES t2)', 't2', 't3')
88421**       -> 'CREATE TABLE t1(a REFERENCES t3)'
88422*/
88423#ifndef SQLITE_OMIT_FOREIGN_KEY
88424static void renameParentFunc(
88425  sqlite3_context *context,
88426  int NotUsed,
88427  sqlite3_value **argv
88428){
88429  sqlite3 *db = sqlite3_context_db_handle(context);
88430  char *zOutput = 0;
88431  char *zResult;
88432  unsigned char const *zInput = sqlite3_value_text(argv[0]);
88433  unsigned char const *zOld = sqlite3_value_text(argv[1]);
88434  unsigned char const *zNew = sqlite3_value_text(argv[2]);
88435
88436  unsigned const char *z;         /* Pointer to token */
88437  int n;                          /* Length of token z */
88438  int token;                      /* Type of token */
88439
88440  UNUSED_PARAMETER(NotUsed);
88441  if( zInput==0 || zOld==0 ) return;
88442  for(z=zInput; *z; z=z+n){
88443    n = sqlite3GetToken(z, &token);
88444    if( token==TK_REFERENCES ){
88445      char *zParent;
88446      do {
88447        z += n;
88448        n = sqlite3GetToken(z, &token);
88449      }while( token==TK_SPACE );
88450
88451      if( token==TK_ILLEGAL ) break;
88452      zParent = sqlite3DbStrNDup(db, (const char *)z, n);
88453      if( zParent==0 ) break;
88454      sqlite3Dequote(zParent);
88455      if( 0==sqlite3StrICmp((const char *)zOld, zParent) ){
88456        char *zOut = sqlite3MPrintf(db, "%s%.*s\"%w\"",
88457            (zOutput?zOutput:""), (int)(z-zInput), zInput, (const char *)zNew
88458        );
88459        sqlite3DbFree(db, zOutput);
88460        zOutput = zOut;
88461        zInput = &z[n];
88462      }
88463      sqlite3DbFree(db, zParent);
88464    }
88465  }
88466
88467  zResult = sqlite3MPrintf(db, "%s%s", (zOutput?zOutput:""), zInput),
88468  sqlite3_result_text(context, zResult, -1, SQLITE_DYNAMIC);
88469  sqlite3DbFree(db, zOutput);
88470}
88471#endif
88472
88473#ifndef SQLITE_OMIT_TRIGGER
88474/* This function is used by SQL generated to implement the
88475** ALTER TABLE command. The first argument is the text of a CREATE TRIGGER
88476** statement. The second is a table name. The table name in the CREATE
88477** TRIGGER statement is replaced with the third argument and the result
88478** returned. This is analagous to renameTableFunc() above, except for CREATE
88479** TRIGGER, not CREATE INDEX and CREATE TABLE.
88480*/
88481static void renameTriggerFunc(
88482  sqlite3_context *context,
88483  int NotUsed,
88484  sqlite3_value **argv
88485){
88486  unsigned char const *zSql = sqlite3_value_text(argv[0]);
88487  unsigned char const *zTableName = sqlite3_value_text(argv[1]);
88488
88489  int token;
88490  Token tname;
88491  int dist = 3;
88492  unsigned char const *zCsr = zSql;
88493  int len = 0;
88494  char *zRet;
88495  sqlite3 *db = sqlite3_context_db_handle(context);
88496
88497  UNUSED_PARAMETER(NotUsed);
88498
88499  /* The principle used to locate the table name in the CREATE TRIGGER
88500  ** statement is that the table name is the first token that is immediately
88501  ** preceded by either TK_ON or TK_DOT and immediately followed by one
88502  ** of TK_WHEN, TK_BEGIN or TK_FOR.
88503  */
88504  if( zSql ){
88505    do {
88506
88507      if( !*zCsr ){
88508        /* Ran out of input before finding the table name. Return NULL. */
88509        return;
88510      }
88511
88512      /* Store the token that zCsr points to in tname. */
88513      tname.z = (char*)zCsr;
88514      tname.n = len;
88515
88516      /* Advance zCsr to the next token. Store that token type in 'token',
88517      ** and its length in 'len' (to be used next iteration of this loop).
88518      */
88519      do {
88520        zCsr += len;
88521        len = sqlite3GetToken(zCsr, &token);
88522      }while( token==TK_SPACE );
88523      assert( len>0 );
88524
88525      /* Variable 'dist' stores the number of tokens read since the most
88526      ** recent TK_DOT or TK_ON. This means that when a WHEN, FOR or BEGIN
88527      ** token is read and 'dist' equals 2, the condition stated above
88528      ** to be met.
88529      **
88530      ** Note that ON cannot be a database, table or column name, so
88531      ** there is no need to worry about syntax like
88532      ** "CREATE TRIGGER ... ON ON.ON BEGIN ..." etc.
88533      */
88534      dist++;
88535      if( token==TK_DOT || token==TK_ON ){
88536        dist = 0;
88537      }
88538    } while( dist!=2 || (token!=TK_WHEN && token!=TK_FOR && token!=TK_BEGIN) );
88539
88540    /* Variable tname now contains the token that is the old table-name
88541    ** in the CREATE TRIGGER statement.
88542    */
88543    zRet = sqlite3MPrintf(db, "%.*s\"%w\"%s", (int)(((u8*)tname.z) - zSql),
88544       zSql, zTableName, tname.z+tname.n);
88545    sqlite3_result_text(context, zRet, -1, SQLITE_DYNAMIC);
88546  }
88547}
88548#endif   /* !SQLITE_OMIT_TRIGGER */
88549
88550/*
88551** Register built-in functions used to help implement ALTER TABLE
88552*/
88553SQLITE_PRIVATE void sqlite3AlterFunctions(void){
88554  static SQLITE_WSD FuncDef aAlterTableFuncs[] = {
88555    FUNCTION(sqlite_rename_table,   2, 0, 0, renameTableFunc),
88556#ifndef SQLITE_OMIT_TRIGGER
88557    FUNCTION(sqlite_rename_trigger, 2, 0, 0, renameTriggerFunc),
88558#endif
88559#ifndef SQLITE_OMIT_FOREIGN_KEY
88560    FUNCTION(sqlite_rename_parent,  3, 0, 0, renameParentFunc),
88561#endif
88562  };
88563  int i;
88564  FuncDefHash *pHash = &GLOBAL(FuncDefHash, sqlite3GlobalFunctions);
88565  FuncDef *aFunc = (FuncDef*)&GLOBAL(FuncDef, aAlterTableFuncs);
88566
88567  for(i=0; i<ArraySize(aAlterTableFuncs); i++){
88568    sqlite3FuncDefInsert(pHash, &aFunc[i]);
88569  }
88570}
88571
88572/*
88573** This function is used to create the text of expressions of the form:
88574**
88575**   name=<constant1> OR name=<constant2> OR ...
88576**
88577** If argument zWhere is NULL, then a pointer string containing the text
88578** "name=<constant>" is returned, where <constant> is the quoted version
88579** of the string passed as argument zConstant. The returned buffer is
88580** allocated using sqlite3DbMalloc(). It is the responsibility of the
88581** caller to ensure that it is eventually freed.
88582**
88583** If argument zWhere is not NULL, then the string returned is
88584** "<where> OR name=<constant>", where <where> is the contents of zWhere.
88585** In this case zWhere is passed to sqlite3DbFree() before returning.
88586**
88587*/
88588static char *whereOrName(sqlite3 *db, char *zWhere, char *zConstant){
88589  char *zNew;
88590  if( !zWhere ){
88591    zNew = sqlite3MPrintf(db, "name=%Q", zConstant);
88592  }else{
88593    zNew = sqlite3MPrintf(db, "%s OR name=%Q", zWhere, zConstant);
88594    sqlite3DbFree(db, zWhere);
88595  }
88596  return zNew;
88597}
88598
88599#if !defined(SQLITE_OMIT_FOREIGN_KEY) && !defined(SQLITE_OMIT_TRIGGER)
88600/*
88601** Generate the text of a WHERE expression which can be used to select all
88602** tables that have foreign key constraints that refer to table pTab (i.e.
88603** constraints for which pTab is the parent table) from the sqlite_master
88604** table.
88605*/
88606static char *whereForeignKeys(Parse *pParse, Table *pTab){
88607  FKey *p;
88608  char *zWhere = 0;
88609  for(p=sqlite3FkReferences(pTab); p; p=p->pNextTo){
88610    zWhere = whereOrName(pParse->db, zWhere, p->pFrom->zName);
88611  }
88612  return zWhere;
88613}
88614#endif
88615
88616/*
88617** Generate the text of a WHERE expression which can be used to select all
88618** temporary triggers on table pTab from the sqlite_temp_master table. If
88619** table pTab has no temporary triggers, or is itself stored in the
88620** temporary database, NULL is returned.
88621*/
88622static char *whereTempTriggers(Parse *pParse, Table *pTab){
88623  Trigger *pTrig;
88624  char *zWhere = 0;
88625  const Schema *pTempSchema = pParse->db->aDb[1].pSchema; /* Temp db schema */
88626
88627  /* If the table is not located in the temp-db (in which case NULL is
88628  ** returned, loop through the tables list of triggers. For each trigger
88629  ** that is not part of the temp-db schema, add a clause to the WHERE
88630  ** expression being built up in zWhere.
88631  */
88632  if( pTab->pSchema!=pTempSchema ){
88633    sqlite3 *db = pParse->db;
88634    for(pTrig=sqlite3TriggerList(pParse, pTab); pTrig; pTrig=pTrig->pNext){
88635      if( pTrig->pSchema==pTempSchema ){
88636        zWhere = whereOrName(db, zWhere, pTrig->zName);
88637      }
88638    }
88639  }
88640  if( zWhere ){
88641    char *zNew = sqlite3MPrintf(pParse->db, "type='trigger' AND (%s)", zWhere);
88642    sqlite3DbFree(pParse->db, zWhere);
88643    zWhere = zNew;
88644  }
88645  return zWhere;
88646}
88647
88648/*
88649** Generate code to drop and reload the internal representation of table
88650** pTab from the database, including triggers and temporary triggers.
88651** Argument zName is the name of the table in the database schema at
88652** the time the generated code is executed. This can be different from
88653** pTab->zName if this function is being called to code part of an
88654** "ALTER TABLE RENAME TO" statement.
88655*/
88656static void reloadTableSchema(Parse *pParse, Table *pTab, const char *zName){
88657  Vdbe *v;
88658  char *zWhere;
88659  int iDb;                   /* Index of database containing pTab */
88660#ifndef SQLITE_OMIT_TRIGGER
88661  Trigger *pTrig;
88662#endif
88663
88664  v = sqlite3GetVdbe(pParse);
88665  if( NEVER(v==0) ) return;
88666  assert( sqlite3BtreeHoldsAllMutexes(pParse->db) );
88667  iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
88668  assert( iDb>=0 );
88669
88670#ifndef SQLITE_OMIT_TRIGGER
88671  /* Drop any table triggers from the internal schema. */
88672  for(pTrig=sqlite3TriggerList(pParse, pTab); pTrig; pTrig=pTrig->pNext){
88673    int iTrigDb = sqlite3SchemaToIndex(pParse->db, pTrig->pSchema);
88674    assert( iTrigDb==iDb || iTrigDb==1 );
88675    sqlite3VdbeAddOp4(v, OP_DropTrigger, iTrigDb, 0, 0, pTrig->zName, 0);
88676  }
88677#endif
88678
88679  /* Drop the table and index from the internal schema.  */
88680  sqlite3VdbeAddOp4(v, OP_DropTable, iDb, 0, 0, pTab->zName, 0);
88681
88682  /* Reload the table, index and permanent trigger schemas. */
88683  zWhere = sqlite3MPrintf(pParse->db, "tbl_name=%Q", zName);
88684  if( !zWhere ) return;
88685  sqlite3VdbeAddParseSchemaOp(v, iDb, zWhere);
88686
88687#ifndef SQLITE_OMIT_TRIGGER
88688  /* Now, if the table is not stored in the temp database, reload any temp
88689  ** triggers. Don't use IN(...) in case SQLITE_OMIT_SUBQUERY is defined.
88690  */
88691  if( (zWhere=whereTempTriggers(pParse, pTab))!=0 ){
88692    sqlite3VdbeAddParseSchemaOp(v, 1, zWhere);
88693  }
88694#endif
88695}
88696
88697/*
88698** Parameter zName is the name of a table that is about to be altered
88699** (either with ALTER TABLE ... RENAME TO or ALTER TABLE ... ADD COLUMN).
88700** If the table is a system table, this function leaves an error message
88701** in pParse->zErr (system tables may not be altered) and returns non-zero.
88702**
88703** Or, if zName is not a system table, zero is returned.
88704*/
88705static int isSystemTable(Parse *pParse, const char *zName){
88706  if( sqlite3Strlen30(zName)>6 && 0==sqlite3StrNICmp(zName, "sqlite_", 7) ){
88707    sqlite3ErrorMsg(pParse, "table %s may not be altered", zName);
88708    return 1;
88709  }
88710  return 0;
88711}
88712
88713/*
88714** Generate code to implement the "ALTER TABLE xxx RENAME TO yyy"
88715** command.
88716*/
88717SQLITE_PRIVATE void sqlite3AlterRenameTable(
88718  Parse *pParse,            /* Parser context. */
88719  SrcList *pSrc,            /* The table to rename. */
88720  Token *pName              /* The new table name. */
88721){
88722  int iDb;                  /* Database that contains the table */
88723  char *zDb;                /* Name of database iDb */
88724  Table *pTab;              /* Table being renamed */
88725  char *zName = 0;          /* NULL-terminated version of pName */
88726  sqlite3 *db = pParse->db; /* Database connection */
88727  int nTabName;             /* Number of UTF-8 characters in zTabName */
88728  const char *zTabName;     /* Original name of the table */
88729  Vdbe *v;
88730#ifndef SQLITE_OMIT_TRIGGER
88731  char *zWhere = 0;         /* Where clause to locate temp triggers */
88732#endif
88733  VTable *pVTab = 0;        /* Non-zero if this is a v-tab with an xRename() */
88734  int savedDbFlags;         /* Saved value of db->flags */
88735
88736  savedDbFlags = db->flags;
88737  if( NEVER(db->mallocFailed) ) goto exit_rename_table;
88738  assert( pSrc->nSrc==1 );
88739  assert( sqlite3BtreeHoldsAllMutexes(pParse->db) );
88740
88741  pTab = sqlite3LocateTableItem(pParse, 0, &pSrc->a[0]);
88742  if( !pTab ) goto exit_rename_table;
88743  iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
88744  zDb = db->aDb[iDb].zName;
88745  db->flags |= SQLITE_PreferBuiltin;
88746
88747  /* Get a NULL terminated version of the new table name. */
88748  zName = sqlite3NameFromToken(db, pName);
88749  if( !zName ) goto exit_rename_table;
88750
88751  /* Check that a table or index named 'zName' does not already exist
88752  ** in database iDb. If so, this is an error.
88753  */
88754  if( sqlite3FindTable(db, zName, zDb) || sqlite3FindIndex(db, zName, zDb) ){
88755    sqlite3ErrorMsg(pParse,
88756        "there is already another table or index with this name: %s", zName);
88757    goto exit_rename_table;
88758  }
88759
88760  /* Make sure it is not a system table being altered, or a reserved name
88761  ** that the table is being renamed to.
88762  */
88763  if( SQLITE_OK!=isSystemTable(pParse, pTab->zName) ){
88764    goto exit_rename_table;
88765  }
88766  if( SQLITE_OK!=sqlite3CheckObjectName(pParse, zName) ){ goto
88767    exit_rename_table;
88768  }
88769
88770#ifndef SQLITE_OMIT_VIEW
88771  if( pTab->pSelect ){
88772    sqlite3ErrorMsg(pParse, "view %s may not be altered", pTab->zName);
88773    goto exit_rename_table;
88774  }
88775#endif
88776
88777#ifndef SQLITE_OMIT_AUTHORIZATION
88778  /* Invoke the authorization callback. */
88779  if( sqlite3AuthCheck(pParse, SQLITE_ALTER_TABLE, zDb, pTab->zName, 0) ){
88780    goto exit_rename_table;
88781  }
88782#endif
88783
88784#ifndef SQLITE_OMIT_VIRTUALTABLE
88785  if( sqlite3ViewGetColumnNames(pParse, pTab) ){
88786    goto exit_rename_table;
88787  }
88788  if( IsVirtual(pTab) ){
88789    pVTab = sqlite3GetVTable(db, pTab);
88790    if( pVTab->pVtab->pModule->xRename==0 ){
88791      pVTab = 0;
88792    }
88793  }
88794#endif
88795
88796  /* Begin a transaction for database iDb.
88797  ** Then modify the schema cookie (since the ALTER TABLE modifies the
88798  ** schema). Open a statement transaction if the table is a virtual
88799  ** table.
88800  */
88801  v = sqlite3GetVdbe(pParse);
88802  if( v==0 ){
88803    goto exit_rename_table;
88804  }
88805  sqlite3BeginWriteOperation(pParse, pVTab!=0, iDb);
88806  sqlite3ChangeCookie(pParse, iDb);
88807
88808  /* If this is a virtual table, invoke the xRename() function if
88809  ** one is defined. The xRename() callback will modify the names
88810  ** of any resources used by the v-table implementation (including other
88811  ** SQLite tables) that are identified by the name of the virtual table.
88812  */
88813#ifndef SQLITE_OMIT_VIRTUALTABLE
88814  if( pVTab ){
88815    int i = ++pParse->nMem;
88816    sqlite3VdbeAddOp4(v, OP_String8, 0, i, 0, zName, 0);
88817    sqlite3VdbeAddOp4(v, OP_VRename, i, 0, 0,(const char*)pVTab, P4_VTAB);
88818    sqlite3MayAbort(pParse);
88819  }
88820#endif
88821
88822  /* figure out how many UTF-8 characters are in zName */
88823  zTabName = pTab->zName;
88824  nTabName = sqlite3Utf8CharLen(zTabName, -1);
88825
88826#if !defined(SQLITE_OMIT_FOREIGN_KEY) && !defined(SQLITE_OMIT_TRIGGER)
88827  if( db->flags&SQLITE_ForeignKeys ){
88828    /* If foreign-key support is enabled, rewrite the CREATE TABLE
88829    ** statements corresponding to all child tables of foreign key constraints
88830    ** for which the renamed table is the parent table.  */
88831    if( (zWhere=whereForeignKeys(pParse, pTab))!=0 ){
88832      sqlite3NestedParse(pParse,
88833          "UPDATE \"%w\".%s SET "
88834              "sql = sqlite_rename_parent(sql, %Q, %Q) "
88835              "WHERE %s;", zDb, SCHEMA_TABLE(iDb), zTabName, zName, zWhere);
88836      sqlite3DbFree(db, zWhere);
88837    }
88838  }
88839#endif
88840
88841  /* Modify the sqlite_master table to use the new table name. */
88842  sqlite3NestedParse(pParse,
88843      "UPDATE %Q.%s SET "
88844#ifdef SQLITE_OMIT_TRIGGER
88845          "sql = sqlite_rename_table(sql, %Q), "
88846#else
88847          "sql = CASE "
88848            "WHEN type = 'trigger' THEN sqlite_rename_trigger(sql, %Q)"
88849            "ELSE sqlite_rename_table(sql, %Q) END, "
88850#endif
88851          "tbl_name = %Q, "
88852          "name = CASE "
88853            "WHEN type='table' THEN %Q "
88854            "WHEN name LIKE 'sqlite_autoindex%%' AND type='index' THEN "
88855             "'sqlite_autoindex_' || %Q || substr(name,%d+18) "
88856            "ELSE name END "
88857      "WHERE tbl_name=%Q COLLATE nocase AND "
88858          "(type='table' OR type='index' OR type='trigger');",
88859      zDb, SCHEMA_TABLE(iDb), zName, zName, zName,
88860#ifndef SQLITE_OMIT_TRIGGER
88861      zName,
88862#endif
88863      zName, nTabName, zTabName
88864  );
88865
88866#ifndef SQLITE_OMIT_AUTOINCREMENT
88867  /* If the sqlite_sequence table exists in this database, then update
88868  ** it with the new table name.
88869  */
88870  if( sqlite3FindTable(db, "sqlite_sequence", zDb) ){
88871    sqlite3NestedParse(pParse,
88872        "UPDATE \"%w\".sqlite_sequence set name = %Q WHERE name = %Q",
88873        zDb, zName, pTab->zName);
88874  }
88875#endif
88876
88877#ifndef SQLITE_OMIT_TRIGGER
88878  /* If there are TEMP triggers on this table, modify the sqlite_temp_master
88879  ** table. Don't do this if the table being ALTERed is itself located in
88880  ** the temp database.
88881  */
88882  if( (zWhere=whereTempTriggers(pParse, pTab))!=0 ){
88883    sqlite3NestedParse(pParse,
88884        "UPDATE sqlite_temp_master SET "
88885            "sql = sqlite_rename_trigger(sql, %Q), "
88886            "tbl_name = %Q "
88887            "WHERE %s;", zName, zName, zWhere);
88888    sqlite3DbFree(db, zWhere);
88889  }
88890#endif
88891
88892#if !defined(SQLITE_OMIT_FOREIGN_KEY) && !defined(SQLITE_OMIT_TRIGGER)
88893  if( db->flags&SQLITE_ForeignKeys ){
88894    FKey *p;
88895    for(p=sqlite3FkReferences(pTab); p; p=p->pNextTo){
88896      Table *pFrom = p->pFrom;
88897      if( pFrom!=pTab ){
88898        reloadTableSchema(pParse, p->pFrom, pFrom->zName);
88899      }
88900    }
88901  }
88902#endif
88903
88904  /* Drop and reload the internal table schema. */
88905  reloadTableSchema(pParse, pTab, zName);
88906
88907exit_rename_table:
88908  sqlite3SrcListDelete(db, pSrc);
88909  sqlite3DbFree(db, zName);
88910  db->flags = savedDbFlags;
88911}
88912
88913
88914/*
88915** Generate code to make sure the file format number is at least minFormat.
88916** The generated code will increase the file format number if necessary.
88917*/
88918SQLITE_PRIVATE void sqlite3MinimumFileFormat(Parse *pParse, int iDb, int minFormat){
88919  Vdbe *v;
88920  v = sqlite3GetVdbe(pParse);
88921  /* The VDBE should have been allocated before this routine is called.
88922  ** If that allocation failed, we would have quit before reaching this
88923  ** point */
88924  if( ALWAYS(v) ){
88925    int r1 = sqlite3GetTempReg(pParse);
88926    int r2 = sqlite3GetTempReg(pParse);
88927    int j1;
88928    sqlite3VdbeAddOp3(v, OP_ReadCookie, iDb, r1, BTREE_FILE_FORMAT);
88929    sqlite3VdbeUsesBtree(v, iDb);
88930    sqlite3VdbeAddOp2(v, OP_Integer, minFormat, r2);
88931    j1 = sqlite3VdbeAddOp3(v, OP_Ge, r2, 0, r1);
88932    sqlite3VdbeChangeP5(v, SQLITE_NOTNULL); VdbeCoverage(v);
88933    sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, BTREE_FILE_FORMAT, r2);
88934    sqlite3VdbeJumpHere(v, j1);
88935    sqlite3ReleaseTempReg(pParse, r1);
88936    sqlite3ReleaseTempReg(pParse, r2);
88937  }
88938}
88939
88940/*
88941** This function is called after an "ALTER TABLE ... ADD" statement
88942** has been parsed. Argument pColDef contains the text of the new
88943** column definition.
88944**
88945** The Table structure pParse->pNewTable was extended to include
88946** the new column during parsing.
88947*/
88948SQLITE_PRIVATE void sqlite3AlterFinishAddColumn(Parse *pParse, Token *pColDef){
88949  Table *pNew;              /* Copy of pParse->pNewTable */
88950  Table *pTab;              /* Table being altered */
88951  int iDb;                  /* Database number */
88952  const char *zDb;          /* Database name */
88953  const char *zTab;         /* Table name */
88954  char *zCol;               /* Null-terminated column definition */
88955  Column *pCol;             /* The new column */
88956  Expr *pDflt;              /* Default value for the new column */
88957  sqlite3 *db;              /* The database connection; */
88958
88959  db = pParse->db;
88960  if( pParse->nErr || db->mallocFailed ) return;
88961  pNew = pParse->pNewTable;
88962  assert( pNew );
88963
88964  assert( sqlite3BtreeHoldsAllMutexes(db) );
88965  iDb = sqlite3SchemaToIndex(db, pNew->pSchema);
88966  zDb = db->aDb[iDb].zName;
88967  zTab = &pNew->zName[16];  /* Skip the "sqlite_altertab_" prefix on the name */
88968  pCol = &pNew->aCol[pNew->nCol-1];
88969  pDflt = pCol->pDflt;
88970  pTab = sqlite3FindTable(db, zTab, zDb);
88971  assert( pTab );
88972
88973#ifndef SQLITE_OMIT_AUTHORIZATION
88974  /* Invoke the authorization callback. */
88975  if( sqlite3AuthCheck(pParse, SQLITE_ALTER_TABLE, zDb, pTab->zName, 0) ){
88976    return;
88977  }
88978#endif
88979
88980  /* If the default value for the new column was specified with a
88981  ** literal NULL, then set pDflt to 0. This simplifies checking
88982  ** for an SQL NULL default below.
88983  */
88984  if( pDflt && pDflt->op==TK_NULL ){
88985    pDflt = 0;
88986  }
88987
88988  /* Check that the new column is not specified as PRIMARY KEY or UNIQUE.
88989  ** If there is a NOT NULL constraint, then the default value for the
88990  ** column must not be NULL.
88991  */
88992  if( pCol->colFlags & COLFLAG_PRIMKEY ){
88993    sqlite3ErrorMsg(pParse, "Cannot add a PRIMARY KEY column");
88994    return;
88995  }
88996  if( pNew->pIndex ){
88997    sqlite3ErrorMsg(pParse, "Cannot add a UNIQUE column");
88998    return;
88999  }
89000  if( (db->flags&SQLITE_ForeignKeys) && pNew->pFKey && pDflt ){
89001    sqlite3ErrorMsg(pParse,
89002        "Cannot add a REFERENCES column with non-NULL default value");
89003    return;
89004  }
89005  if( pCol->notNull && !pDflt ){
89006    sqlite3ErrorMsg(pParse,
89007        "Cannot add a NOT NULL column with default value NULL");
89008    return;
89009  }
89010
89011  /* Ensure the default expression is something that sqlite3ValueFromExpr()
89012  ** can handle (i.e. not CURRENT_TIME etc.)
89013  */
89014  if( pDflt ){
89015    sqlite3_value *pVal = 0;
89016    int rc;
89017    rc = sqlite3ValueFromExpr(db, pDflt, SQLITE_UTF8, SQLITE_AFF_BLOB, &pVal);
89018    assert( rc==SQLITE_OK || rc==SQLITE_NOMEM );
89019    if( rc!=SQLITE_OK ){
89020      db->mallocFailed = 1;
89021      return;
89022    }
89023    if( !pVal ){
89024      sqlite3ErrorMsg(pParse, "Cannot add a column with non-constant default");
89025      return;
89026    }
89027    sqlite3ValueFree(pVal);
89028  }
89029
89030  /* Modify the CREATE TABLE statement. */
89031  zCol = sqlite3DbStrNDup(db, (char*)pColDef->z, pColDef->n);
89032  if( zCol ){
89033    char *zEnd = &zCol[pColDef->n-1];
89034    int savedDbFlags = db->flags;
89035    while( zEnd>zCol && (*zEnd==';' || sqlite3Isspace(*zEnd)) ){
89036      *zEnd-- = '\0';
89037    }
89038    db->flags |= SQLITE_PreferBuiltin;
89039    sqlite3NestedParse(pParse,
89040        "UPDATE \"%w\".%s SET "
89041          "sql = substr(sql,1,%d) || ', ' || %Q || substr(sql,%d) "
89042        "WHERE type = 'table' AND name = %Q",
89043      zDb, SCHEMA_TABLE(iDb), pNew->addColOffset, zCol, pNew->addColOffset+1,
89044      zTab
89045    );
89046    sqlite3DbFree(db, zCol);
89047    db->flags = savedDbFlags;
89048  }
89049
89050  /* If the default value of the new column is NULL, then set the file
89051  ** format to 2. If the default value of the new column is not NULL,
89052  ** the file format becomes 3.
89053  */
89054  sqlite3MinimumFileFormat(pParse, iDb, pDflt ? 3 : 2);
89055
89056  /* Reload the schema of the modified table. */
89057  reloadTableSchema(pParse, pTab, pTab->zName);
89058}
89059
89060/*
89061** This function is called by the parser after the table-name in
89062** an "ALTER TABLE <table-name> ADD" statement is parsed. Argument
89063** pSrc is the full-name of the table being altered.
89064**
89065** This routine makes a (partial) copy of the Table structure
89066** for the table being altered and sets Parse.pNewTable to point
89067** to it. Routines called by the parser as the column definition
89068** is parsed (i.e. sqlite3AddColumn()) add the new Column data to
89069** the copy. The copy of the Table structure is deleted by tokenize.c
89070** after parsing is finished.
89071**
89072** Routine sqlite3AlterFinishAddColumn() will be called to complete
89073** coding the "ALTER TABLE ... ADD" statement.
89074*/
89075SQLITE_PRIVATE void sqlite3AlterBeginAddColumn(Parse *pParse, SrcList *pSrc){
89076  Table *pNew;
89077  Table *pTab;
89078  Vdbe *v;
89079  int iDb;
89080  int i;
89081  int nAlloc;
89082  sqlite3 *db = pParse->db;
89083
89084  /* Look up the table being altered. */
89085  assert( pParse->pNewTable==0 );
89086  assert( sqlite3BtreeHoldsAllMutexes(db) );
89087  if( db->mallocFailed ) goto exit_begin_add_column;
89088  pTab = sqlite3LocateTableItem(pParse, 0, &pSrc->a[0]);
89089  if( !pTab ) goto exit_begin_add_column;
89090
89091#ifndef SQLITE_OMIT_VIRTUALTABLE
89092  if( IsVirtual(pTab) ){
89093    sqlite3ErrorMsg(pParse, "virtual tables may not be altered");
89094    goto exit_begin_add_column;
89095  }
89096#endif
89097
89098  /* Make sure this is not an attempt to ALTER a view. */
89099  if( pTab->pSelect ){
89100    sqlite3ErrorMsg(pParse, "Cannot add a column to a view");
89101    goto exit_begin_add_column;
89102  }
89103  if( SQLITE_OK!=isSystemTable(pParse, pTab->zName) ){
89104    goto exit_begin_add_column;
89105  }
89106
89107  assert( pTab->addColOffset>0 );
89108  iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
89109
89110  /* Put a copy of the Table struct in Parse.pNewTable for the
89111  ** sqlite3AddColumn() function and friends to modify.  But modify
89112  ** the name by adding an "sqlite_altertab_" prefix.  By adding this
89113  ** prefix, we insure that the name will not collide with an existing
89114  ** table because user table are not allowed to have the "sqlite_"
89115  ** prefix on their name.
89116  */
89117  pNew = (Table*)sqlite3DbMallocZero(db, sizeof(Table));
89118  if( !pNew ) goto exit_begin_add_column;
89119  pParse->pNewTable = pNew;
89120  pNew->nRef = 1;
89121  pNew->nCol = pTab->nCol;
89122  assert( pNew->nCol>0 );
89123  nAlloc = (((pNew->nCol-1)/8)*8)+8;
89124  assert( nAlloc>=pNew->nCol && nAlloc%8==0 && nAlloc-pNew->nCol<8 );
89125  pNew->aCol = (Column*)sqlite3DbMallocZero(db, sizeof(Column)*nAlloc);
89126  pNew->zName = sqlite3MPrintf(db, "sqlite_altertab_%s", pTab->zName);
89127  if( !pNew->aCol || !pNew->zName ){
89128    db->mallocFailed = 1;
89129    goto exit_begin_add_column;
89130  }
89131  memcpy(pNew->aCol, pTab->aCol, sizeof(Column)*pNew->nCol);
89132  for(i=0; i<pNew->nCol; i++){
89133    Column *pCol = &pNew->aCol[i];
89134    pCol->zName = sqlite3DbStrDup(db, pCol->zName);
89135    pCol->zColl = 0;
89136    pCol->zType = 0;
89137    pCol->pDflt = 0;
89138    pCol->zDflt = 0;
89139  }
89140  pNew->pSchema = db->aDb[iDb].pSchema;
89141  pNew->addColOffset = pTab->addColOffset;
89142  pNew->nRef = 1;
89143
89144  /* Begin a transaction and increment the schema cookie.  */
89145  sqlite3BeginWriteOperation(pParse, 0, iDb);
89146  v = sqlite3GetVdbe(pParse);
89147  if( !v ) goto exit_begin_add_column;
89148  sqlite3ChangeCookie(pParse, iDb);
89149
89150exit_begin_add_column:
89151  sqlite3SrcListDelete(db, pSrc);
89152  return;
89153}
89154#endif  /* SQLITE_ALTER_TABLE */
89155
89156/************** End of alter.c ***********************************************/
89157/************** Begin file analyze.c *****************************************/
89158/*
89159** 2005-07-08
89160**
89161** The author disclaims copyright to this source code.  In place of
89162** a legal notice, here is a blessing:
89163**
89164**    May you do good and not evil.
89165**    May you find forgiveness for yourself and forgive others.
89166**    May you share freely, never taking more than you give.
89167**
89168*************************************************************************
89169** This file contains code associated with the ANALYZE command.
89170**
89171** The ANALYZE command gather statistics about the content of tables
89172** and indices.  These statistics are made available to the query planner
89173** to help it make better decisions about how to perform queries.
89174**
89175** The following system tables are or have been supported:
89176**
89177**    CREATE TABLE sqlite_stat1(tbl, idx, stat);
89178**    CREATE TABLE sqlite_stat2(tbl, idx, sampleno, sample);
89179**    CREATE TABLE sqlite_stat3(tbl, idx, nEq, nLt, nDLt, sample);
89180**    CREATE TABLE sqlite_stat4(tbl, idx, nEq, nLt, nDLt, sample);
89181**
89182** Additional tables might be added in future releases of SQLite.
89183** The sqlite_stat2 table is not created or used unless the SQLite version
89184** is between 3.6.18 and 3.7.8, inclusive, and unless SQLite is compiled
89185** with SQLITE_ENABLE_STAT2.  The sqlite_stat2 table is deprecated.
89186** The sqlite_stat2 table is superseded by sqlite_stat3, which is only
89187** created and used by SQLite versions 3.7.9 and later and with
89188** SQLITE_ENABLE_STAT3 defined.  The functionality of sqlite_stat3
89189** is a superset of sqlite_stat2.  The sqlite_stat4 is an enhanced
89190** version of sqlite_stat3 and is only available when compiled with
89191** SQLITE_ENABLE_STAT4 and in SQLite versions 3.8.1 and later.  It is
89192** not possible to enable both STAT3 and STAT4 at the same time.  If they
89193** are both enabled, then STAT4 takes precedence.
89194**
89195** For most applications, sqlite_stat1 provides all the statistics required
89196** for the query planner to make good choices.
89197**
89198** Format of sqlite_stat1:
89199**
89200** There is normally one row per index, with the index identified by the
89201** name in the idx column.  The tbl column is the name of the table to
89202** which the index belongs.  In each such row, the stat column will be
89203** a string consisting of a list of integers.  The first integer in this
89204** list is the number of rows in the index.  (This is the same as the
89205** number of rows in the table, except for partial indices.)  The second
89206** integer is the average number of rows in the index that have the same
89207** value in the first column of the index.  The third integer is the average
89208** number of rows in the index that have the same value for the first two
89209** columns.  The N-th integer (for N>1) is the average number of rows in
89210** the index which have the same value for the first N-1 columns.  For
89211** a K-column index, there will be K+1 integers in the stat column.  If
89212** the index is unique, then the last integer will be 1.
89213**
89214** The list of integers in the stat column can optionally be followed
89215** by the keyword "unordered".  The "unordered" keyword, if it is present,
89216** must be separated from the last integer by a single space.  If the
89217** "unordered" keyword is present, then the query planner assumes that
89218** the index is unordered and will not use the index for a range query.
89219**
89220** If the sqlite_stat1.idx column is NULL, then the sqlite_stat1.stat
89221** column contains a single integer which is the (estimated) number of
89222** rows in the table identified by sqlite_stat1.tbl.
89223**
89224** Format of sqlite_stat2:
89225**
89226** The sqlite_stat2 is only created and is only used if SQLite is compiled
89227** with SQLITE_ENABLE_STAT2 and if the SQLite version number is between
89228** 3.6.18 and 3.7.8.  The "stat2" table contains additional information
89229** about the distribution of keys within an index.  The index is identified by
89230** the "idx" column and the "tbl" column is the name of the table to which
89231** the index belongs.  There are usually 10 rows in the sqlite_stat2
89232** table for each index.
89233**
89234** The sqlite_stat2 entries for an index that have sampleno between 0 and 9
89235** inclusive are samples of the left-most key value in the index taken at
89236** evenly spaced points along the index.  Let the number of samples be S
89237** (10 in the standard build) and let C be the number of rows in the index.
89238** Then the sampled rows are given by:
89239**
89240**     rownumber = (i*C*2 + C)/(S*2)
89241**
89242** For i between 0 and S-1.  Conceptually, the index space is divided into
89243** S uniform buckets and the samples are the middle row from each bucket.
89244**
89245** The format for sqlite_stat2 is recorded here for legacy reference.  This
89246** version of SQLite does not support sqlite_stat2.  It neither reads nor
89247** writes the sqlite_stat2 table.  This version of SQLite only supports
89248** sqlite_stat3.
89249**
89250** Format for sqlite_stat3:
89251**
89252** The sqlite_stat3 format is a subset of sqlite_stat4.  Hence, the
89253** sqlite_stat4 format will be described first.  Further information
89254** about sqlite_stat3 follows the sqlite_stat4 description.
89255**
89256** Format for sqlite_stat4:
89257**
89258** As with sqlite_stat2, the sqlite_stat4 table contains histogram data
89259** to aid the query planner in choosing good indices based on the values
89260** that indexed columns are compared against in the WHERE clauses of
89261** queries.
89262**
89263** The sqlite_stat4 table contains multiple entries for each index.
89264** The idx column names the index and the tbl column is the table of the
89265** index.  If the idx and tbl columns are the same, then the sample is
89266** of the INTEGER PRIMARY KEY.  The sample column is a blob which is the
89267** binary encoding of a key from the index.  The nEq column is a
89268** list of integers.  The first integer is the approximate number
89269** of entries in the index whose left-most column exactly matches
89270** the left-most column of the sample.  The second integer in nEq
89271** is the approximate number of entries in the index where the
89272** first two columns match the first two columns of the sample.
89273** And so forth.  nLt is another list of integers that show the approximate
89274** number of entries that are strictly less than the sample.  The first
89275** integer in nLt contains the number of entries in the index where the
89276** left-most column is less than the left-most column of the sample.
89277** The K-th integer in the nLt entry is the number of index entries
89278** where the first K columns are less than the first K columns of the
89279** sample.  The nDLt column is like nLt except that it contains the
89280** number of distinct entries in the index that are less than the
89281** sample.
89282**
89283** There can be an arbitrary number of sqlite_stat4 entries per index.
89284** The ANALYZE command will typically generate sqlite_stat4 tables
89285** that contain between 10 and 40 samples which are distributed across
89286** the key space, though not uniformly, and which include samples with
89287** large nEq values.
89288**
89289** Format for sqlite_stat3 redux:
89290**
89291** The sqlite_stat3 table is like sqlite_stat4 except that it only
89292** looks at the left-most column of the index.  The sqlite_stat3.sample
89293** column contains the actual value of the left-most column instead
89294** of a blob encoding of the complete index key as is found in
89295** sqlite_stat4.sample.  The nEq, nLt, and nDLt entries of sqlite_stat3
89296** all contain just a single integer which is the same as the first
89297** integer in the equivalent columns in sqlite_stat4.
89298*/
89299#ifndef SQLITE_OMIT_ANALYZE
89300/* #include "sqliteInt.h" */
89301
89302#if defined(SQLITE_ENABLE_STAT4)
89303# define IsStat4     1
89304# define IsStat3     0
89305#elif defined(SQLITE_ENABLE_STAT3)
89306# define IsStat4     0
89307# define IsStat3     1
89308#else
89309# define IsStat4     0
89310# define IsStat3     0
89311# undef SQLITE_STAT4_SAMPLES
89312# define SQLITE_STAT4_SAMPLES 1
89313#endif
89314#define IsStat34    (IsStat3+IsStat4)  /* 1 for STAT3 or STAT4. 0 otherwise */
89315
89316/*
89317** This routine generates code that opens the sqlite_statN tables.
89318** The sqlite_stat1 table is always relevant.  sqlite_stat2 is now
89319** obsolete.  sqlite_stat3 and sqlite_stat4 are only opened when
89320** appropriate compile-time options are provided.
89321**
89322** If the sqlite_statN tables do not previously exist, it is created.
89323**
89324** Argument zWhere may be a pointer to a buffer containing a table name,
89325** or it may be a NULL pointer. If it is not NULL, then all entries in
89326** the sqlite_statN tables associated with the named table are deleted.
89327** If zWhere==0, then code is generated to delete all stat table entries.
89328*/
89329static void openStatTable(
89330  Parse *pParse,          /* Parsing context */
89331  int iDb,                /* The database we are looking in */
89332  int iStatCur,           /* Open the sqlite_stat1 table on this cursor */
89333  const char *zWhere,     /* Delete entries for this table or index */
89334  const char *zWhereType  /* Either "tbl" or "idx" */
89335){
89336  static const struct {
89337    const char *zName;
89338    const char *zCols;
89339  } aTable[] = {
89340    { "sqlite_stat1", "tbl,idx,stat" },
89341#if defined(SQLITE_ENABLE_STAT4)
89342    { "sqlite_stat4", "tbl,idx,neq,nlt,ndlt,sample" },
89343    { "sqlite_stat3", 0 },
89344#elif defined(SQLITE_ENABLE_STAT3)
89345    { "sqlite_stat3", "tbl,idx,neq,nlt,ndlt,sample" },
89346    { "sqlite_stat4", 0 },
89347#else
89348    { "sqlite_stat3", 0 },
89349    { "sqlite_stat4", 0 },
89350#endif
89351  };
89352  int i;
89353  sqlite3 *db = pParse->db;
89354  Db *pDb;
89355  Vdbe *v = sqlite3GetVdbe(pParse);
89356  int aRoot[ArraySize(aTable)];
89357  u8 aCreateTbl[ArraySize(aTable)];
89358
89359  if( v==0 ) return;
89360  assert( sqlite3BtreeHoldsAllMutexes(db) );
89361  assert( sqlite3VdbeDb(v)==db );
89362  pDb = &db->aDb[iDb];
89363
89364  /* Create new statistic tables if they do not exist, or clear them
89365  ** if they do already exist.
89366  */
89367  for(i=0; i<ArraySize(aTable); i++){
89368    const char *zTab = aTable[i].zName;
89369    Table *pStat;
89370    if( (pStat = sqlite3FindTable(db, zTab, pDb->zName))==0 ){
89371      if( aTable[i].zCols ){
89372        /* The sqlite_statN table does not exist. Create it. Note that a
89373        ** side-effect of the CREATE TABLE statement is to leave the rootpage
89374        ** of the new table in register pParse->regRoot. This is important
89375        ** because the OpenWrite opcode below will be needing it. */
89376        sqlite3NestedParse(pParse,
89377            "CREATE TABLE %Q.%s(%s)", pDb->zName, zTab, aTable[i].zCols
89378        );
89379        aRoot[i] = pParse->regRoot;
89380        aCreateTbl[i] = OPFLAG_P2ISREG;
89381      }
89382    }else{
89383      /* The table already exists. If zWhere is not NULL, delete all entries
89384      ** associated with the table zWhere. If zWhere is NULL, delete the
89385      ** entire contents of the table. */
89386      aRoot[i] = pStat->tnum;
89387      aCreateTbl[i] = 0;
89388      sqlite3TableLock(pParse, iDb, aRoot[i], 1, zTab);
89389      if( zWhere ){
89390        sqlite3NestedParse(pParse,
89391           "DELETE FROM %Q.%s WHERE %s=%Q",
89392           pDb->zName, zTab, zWhereType, zWhere
89393        );
89394      }else{
89395        /* The sqlite_stat[134] table already exists.  Delete all rows. */
89396        sqlite3VdbeAddOp2(v, OP_Clear, aRoot[i], iDb);
89397      }
89398    }
89399  }
89400
89401  /* Open the sqlite_stat[134] tables for writing. */
89402  for(i=0; aTable[i].zCols; i++){
89403    assert( i<ArraySize(aTable) );
89404    sqlite3VdbeAddOp4Int(v, OP_OpenWrite, iStatCur+i, aRoot[i], iDb, 3);
89405    sqlite3VdbeChangeP5(v, aCreateTbl[i]);
89406    VdbeComment((v, aTable[i].zName));
89407  }
89408}
89409
89410/*
89411** Recommended number of samples for sqlite_stat4
89412*/
89413#ifndef SQLITE_STAT4_SAMPLES
89414# define SQLITE_STAT4_SAMPLES 24
89415#endif
89416
89417/*
89418** Three SQL functions - stat_init(), stat_push(), and stat_get() -
89419** share an instance of the following structure to hold their state
89420** information.
89421*/
89422typedef struct Stat4Accum Stat4Accum;
89423typedef struct Stat4Sample Stat4Sample;
89424struct Stat4Sample {
89425  tRowcnt *anEq;                  /* sqlite_stat4.nEq */
89426  tRowcnt *anDLt;                 /* sqlite_stat4.nDLt */
89427#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
89428  tRowcnt *anLt;                  /* sqlite_stat4.nLt */
89429  union {
89430    i64 iRowid;                     /* Rowid in main table of the key */
89431    u8 *aRowid;                     /* Key for WITHOUT ROWID tables */
89432  } u;
89433  u32 nRowid;                     /* Sizeof aRowid[] */
89434  u8 isPSample;                   /* True if a periodic sample */
89435  int iCol;                       /* If !isPSample, the reason for inclusion */
89436  u32 iHash;                      /* Tiebreaker hash */
89437#endif
89438};
89439struct Stat4Accum {
89440  tRowcnt nRow;             /* Number of rows in the entire table */
89441  tRowcnt nPSample;         /* How often to do a periodic sample */
89442  int nCol;                 /* Number of columns in index + pk/rowid */
89443  int nKeyCol;              /* Number of index columns w/o the pk/rowid */
89444  int mxSample;             /* Maximum number of samples to accumulate */
89445  Stat4Sample current;      /* Current row as a Stat4Sample */
89446  u32 iPrn;                 /* Pseudo-random number used for sampling */
89447  Stat4Sample *aBest;       /* Array of nCol best samples */
89448  int iMin;                 /* Index in a[] of entry with minimum score */
89449  int nSample;              /* Current number of samples */
89450  int iGet;                 /* Index of current sample accessed by stat_get() */
89451  Stat4Sample *a;           /* Array of mxSample Stat4Sample objects */
89452  sqlite3 *db;              /* Database connection, for malloc() */
89453};
89454
89455/* Reclaim memory used by a Stat4Sample
89456*/
89457#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
89458static void sampleClear(sqlite3 *db, Stat4Sample *p){
89459  assert( db!=0 );
89460  if( p->nRowid ){
89461    sqlite3DbFree(db, p->u.aRowid);
89462    p->nRowid = 0;
89463  }
89464}
89465#endif
89466
89467/* Initialize the BLOB value of a ROWID
89468*/
89469#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
89470static void sampleSetRowid(sqlite3 *db, Stat4Sample *p, int n, const u8 *pData){
89471  assert( db!=0 );
89472  if( p->nRowid ) sqlite3DbFree(db, p->u.aRowid);
89473  p->u.aRowid = sqlite3DbMallocRaw(db, n);
89474  if( p->u.aRowid ){
89475    p->nRowid = n;
89476    memcpy(p->u.aRowid, pData, n);
89477  }else{
89478    p->nRowid = 0;
89479  }
89480}
89481#endif
89482
89483/* Initialize the INTEGER value of a ROWID.
89484*/
89485#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
89486static void sampleSetRowidInt64(sqlite3 *db, Stat4Sample *p, i64 iRowid){
89487  assert( db!=0 );
89488  if( p->nRowid ) sqlite3DbFree(db, p->u.aRowid);
89489  p->nRowid = 0;
89490  p->u.iRowid = iRowid;
89491}
89492#endif
89493
89494
89495/*
89496** Copy the contents of object (*pFrom) into (*pTo).
89497*/
89498#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
89499static void sampleCopy(Stat4Accum *p, Stat4Sample *pTo, Stat4Sample *pFrom){
89500  pTo->isPSample = pFrom->isPSample;
89501  pTo->iCol = pFrom->iCol;
89502  pTo->iHash = pFrom->iHash;
89503  memcpy(pTo->anEq, pFrom->anEq, sizeof(tRowcnt)*p->nCol);
89504  memcpy(pTo->anLt, pFrom->anLt, sizeof(tRowcnt)*p->nCol);
89505  memcpy(pTo->anDLt, pFrom->anDLt, sizeof(tRowcnt)*p->nCol);
89506  if( pFrom->nRowid ){
89507    sampleSetRowid(p->db, pTo, pFrom->nRowid, pFrom->u.aRowid);
89508  }else{
89509    sampleSetRowidInt64(p->db, pTo, pFrom->u.iRowid);
89510  }
89511}
89512#endif
89513
89514/*
89515** Reclaim all memory of a Stat4Accum structure.
89516*/
89517static void stat4Destructor(void *pOld){
89518  Stat4Accum *p = (Stat4Accum*)pOld;
89519#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
89520  int i;
89521  for(i=0; i<p->nCol; i++) sampleClear(p->db, p->aBest+i);
89522  for(i=0; i<p->mxSample; i++) sampleClear(p->db, p->a+i);
89523  sampleClear(p->db, &p->current);
89524#endif
89525  sqlite3DbFree(p->db, p);
89526}
89527
89528/*
89529** Implementation of the stat_init(N,K,C) SQL function. The three parameters
89530** are:
89531**     N:    The number of columns in the index including the rowid/pk (note 1)
89532**     K:    The number of columns in the index excluding the rowid/pk.
89533**     C:    The number of rows in the index (note 2)
89534**
89535** Note 1:  In the special case of the covering index that implements a
89536** WITHOUT ROWID table, N is the number of PRIMARY KEY columns, not the
89537** total number of columns in the table.
89538**
89539** Note 2:  C is only used for STAT3 and STAT4.
89540**
89541** For indexes on ordinary rowid tables, N==K+1.  But for indexes on
89542** WITHOUT ROWID tables, N=K+P where P is the number of columns in the
89543** PRIMARY KEY of the table.  The covering index that implements the
89544** original WITHOUT ROWID table as N==K as a special case.
89545**
89546** This routine allocates the Stat4Accum object in heap memory. The return
89547** value is a pointer to the Stat4Accum object.  The datatype of the
89548** return value is BLOB, but it is really just a pointer to the Stat4Accum
89549** object.
89550*/
89551static void statInit(
89552  sqlite3_context *context,
89553  int argc,
89554  sqlite3_value **argv
89555){
89556  Stat4Accum *p;
89557  int nCol;                       /* Number of columns in index being sampled */
89558  int nKeyCol;                    /* Number of key columns */
89559  int nColUp;                     /* nCol rounded up for alignment */
89560  int n;                          /* Bytes of space to allocate */
89561  sqlite3 *db;                    /* Database connection */
89562#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
89563  int mxSample = SQLITE_STAT4_SAMPLES;
89564#endif
89565
89566  /* Decode the three function arguments */
89567  UNUSED_PARAMETER(argc);
89568  nCol = sqlite3_value_int(argv[0]);
89569  assert( nCol>0 );
89570  nColUp = sizeof(tRowcnt)<8 ? (nCol+1)&~1 : nCol;
89571  nKeyCol = sqlite3_value_int(argv[1]);
89572  assert( nKeyCol<=nCol );
89573  assert( nKeyCol>0 );
89574
89575  /* Allocate the space required for the Stat4Accum object */
89576  n = sizeof(*p)
89577    + sizeof(tRowcnt)*nColUp                  /* Stat4Accum.anEq */
89578    + sizeof(tRowcnt)*nColUp                  /* Stat4Accum.anDLt */
89579#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
89580    + sizeof(tRowcnt)*nColUp                  /* Stat4Accum.anLt */
89581    + sizeof(Stat4Sample)*(nCol+mxSample)     /* Stat4Accum.aBest[], a[] */
89582    + sizeof(tRowcnt)*3*nColUp*(nCol+mxSample)
89583#endif
89584  ;
89585  db = sqlite3_context_db_handle(context);
89586  p = sqlite3DbMallocZero(db, n);
89587  if( p==0 ){
89588    sqlite3_result_error_nomem(context);
89589    return;
89590  }
89591
89592  p->db = db;
89593  p->nRow = 0;
89594  p->nCol = nCol;
89595  p->nKeyCol = nKeyCol;
89596  p->current.anDLt = (tRowcnt*)&p[1];
89597  p->current.anEq = &p->current.anDLt[nColUp];
89598
89599#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
89600  {
89601    u8 *pSpace;                     /* Allocated space not yet assigned */
89602    int i;                          /* Used to iterate through p->aSample[] */
89603
89604    p->iGet = -1;
89605    p->mxSample = mxSample;
89606    p->nPSample = (tRowcnt)(sqlite3_value_int64(argv[2])/(mxSample/3+1) + 1);
89607    p->current.anLt = &p->current.anEq[nColUp];
89608    p->iPrn = 0x689e962d*(u32)nCol ^ 0xd0944565*(u32)sqlite3_value_int(argv[2]);
89609
89610    /* Set up the Stat4Accum.a[] and aBest[] arrays */
89611    p->a = (struct Stat4Sample*)&p->current.anLt[nColUp];
89612    p->aBest = &p->a[mxSample];
89613    pSpace = (u8*)(&p->a[mxSample+nCol]);
89614    for(i=0; i<(mxSample+nCol); i++){
89615      p->a[i].anEq = (tRowcnt *)pSpace; pSpace += (sizeof(tRowcnt) * nColUp);
89616      p->a[i].anLt = (tRowcnt *)pSpace; pSpace += (sizeof(tRowcnt) * nColUp);
89617      p->a[i].anDLt = (tRowcnt *)pSpace; pSpace += (sizeof(tRowcnt) * nColUp);
89618    }
89619    assert( (pSpace - (u8*)p)==n );
89620
89621    for(i=0; i<nCol; i++){
89622      p->aBest[i].iCol = i;
89623    }
89624  }
89625#endif
89626
89627  /* Return a pointer to the allocated object to the caller.  Note that
89628  ** only the pointer (the 2nd parameter) matters.  The size of the object
89629  ** (given by the 3rd parameter) is never used and can be any positive
89630  ** value. */
89631  sqlite3_result_blob(context, p, sizeof(*p), stat4Destructor);
89632}
89633static const FuncDef statInitFuncdef = {
89634  2+IsStat34,      /* nArg */
89635  SQLITE_UTF8,     /* funcFlags */
89636  0,               /* pUserData */
89637  0,               /* pNext */
89638  statInit,        /* xFunc */
89639  0,               /* xStep */
89640  0,               /* xFinalize */
89641  "stat_init",     /* zName */
89642  0,               /* pHash */
89643  0                /* pDestructor */
89644};
89645
89646#ifdef SQLITE_ENABLE_STAT4
89647/*
89648** pNew and pOld are both candidate non-periodic samples selected for
89649** the same column (pNew->iCol==pOld->iCol). Ignoring this column and
89650** considering only any trailing columns and the sample hash value, this
89651** function returns true if sample pNew is to be preferred over pOld.
89652** In other words, if we assume that the cardinalities of the selected
89653** column for pNew and pOld are equal, is pNew to be preferred over pOld.
89654**
89655** This function assumes that for each argument sample, the contents of
89656** the anEq[] array from pSample->anEq[pSample->iCol+1] onwards are valid.
89657*/
89658static int sampleIsBetterPost(
89659  Stat4Accum *pAccum,
89660  Stat4Sample *pNew,
89661  Stat4Sample *pOld
89662){
89663  int nCol = pAccum->nCol;
89664  int i;
89665  assert( pNew->iCol==pOld->iCol );
89666  for(i=pNew->iCol+1; i<nCol; i++){
89667    if( pNew->anEq[i]>pOld->anEq[i] ) return 1;
89668    if( pNew->anEq[i]<pOld->anEq[i] ) return 0;
89669  }
89670  if( pNew->iHash>pOld->iHash ) return 1;
89671  return 0;
89672}
89673#endif
89674
89675#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
89676/*
89677** Return true if pNew is to be preferred over pOld.
89678**
89679** This function assumes that for each argument sample, the contents of
89680** the anEq[] array from pSample->anEq[pSample->iCol] onwards are valid.
89681*/
89682static int sampleIsBetter(
89683  Stat4Accum *pAccum,
89684  Stat4Sample *pNew,
89685  Stat4Sample *pOld
89686){
89687  tRowcnt nEqNew = pNew->anEq[pNew->iCol];
89688  tRowcnt nEqOld = pOld->anEq[pOld->iCol];
89689
89690  assert( pOld->isPSample==0 && pNew->isPSample==0 );
89691  assert( IsStat4 || (pNew->iCol==0 && pOld->iCol==0) );
89692
89693  if( (nEqNew>nEqOld) ) return 1;
89694#ifdef SQLITE_ENABLE_STAT4
89695  if( nEqNew==nEqOld ){
89696    if( pNew->iCol<pOld->iCol ) return 1;
89697    return (pNew->iCol==pOld->iCol && sampleIsBetterPost(pAccum, pNew, pOld));
89698  }
89699  return 0;
89700#else
89701  return (nEqNew==nEqOld && pNew->iHash>pOld->iHash);
89702#endif
89703}
89704
89705/*
89706** Copy the contents of sample *pNew into the p->a[] array. If necessary,
89707** remove the least desirable sample from p->a[] to make room.
89708*/
89709static void sampleInsert(Stat4Accum *p, Stat4Sample *pNew, int nEqZero){
89710  Stat4Sample *pSample = 0;
89711  int i;
89712
89713  assert( IsStat4 || nEqZero==0 );
89714
89715#ifdef SQLITE_ENABLE_STAT4
89716  if( pNew->isPSample==0 ){
89717    Stat4Sample *pUpgrade = 0;
89718    assert( pNew->anEq[pNew->iCol]>0 );
89719
89720    /* This sample is being added because the prefix that ends in column
89721    ** iCol occurs many times in the table. However, if we have already
89722    ** added a sample that shares this prefix, there is no need to add
89723    ** this one. Instead, upgrade the priority of the highest priority
89724    ** existing sample that shares this prefix.  */
89725    for(i=p->nSample-1; i>=0; i--){
89726      Stat4Sample *pOld = &p->a[i];
89727      if( pOld->anEq[pNew->iCol]==0 ){
89728        if( pOld->isPSample ) return;
89729        assert( pOld->iCol>pNew->iCol );
89730        assert( sampleIsBetter(p, pNew, pOld) );
89731        if( pUpgrade==0 || sampleIsBetter(p, pOld, pUpgrade) ){
89732          pUpgrade = pOld;
89733        }
89734      }
89735    }
89736    if( pUpgrade ){
89737      pUpgrade->iCol = pNew->iCol;
89738      pUpgrade->anEq[pUpgrade->iCol] = pNew->anEq[pUpgrade->iCol];
89739      goto find_new_min;
89740    }
89741  }
89742#endif
89743
89744  /* If necessary, remove sample iMin to make room for the new sample. */
89745  if( p->nSample>=p->mxSample ){
89746    Stat4Sample *pMin = &p->a[p->iMin];
89747    tRowcnt *anEq = pMin->anEq;
89748    tRowcnt *anLt = pMin->anLt;
89749    tRowcnt *anDLt = pMin->anDLt;
89750    sampleClear(p->db, pMin);
89751    memmove(pMin, &pMin[1], sizeof(p->a[0])*(p->nSample-p->iMin-1));
89752    pSample = &p->a[p->nSample-1];
89753    pSample->nRowid = 0;
89754    pSample->anEq = anEq;
89755    pSample->anDLt = anDLt;
89756    pSample->anLt = anLt;
89757    p->nSample = p->mxSample-1;
89758  }
89759
89760  /* The "rows less-than" for the rowid column must be greater than that
89761  ** for the last sample in the p->a[] array. Otherwise, the samples would
89762  ** be out of order. */
89763#ifdef SQLITE_ENABLE_STAT4
89764  assert( p->nSample==0
89765       || pNew->anLt[p->nCol-1] > p->a[p->nSample-1].anLt[p->nCol-1] );
89766#endif
89767
89768  /* Insert the new sample */
89769  pSample = &p->a[p->nSample];
89770  sampleCopy(p, pSample, pNew);
89771  p->nSample++;
89772
89773  /* Zero the first nEqZero entries in the anEq[] array. */
89774  memset(pSample->anEq, 0, sizeof(tRowcnt)*nEqZero);
89775
89776#ifdef SQLITE_ENABLE_STAT4
89777 find_new_min:
89778#endif
89779  if( p->nSample>=p->mxSample ){
89780    int iMin = -1;
89781    for(i=0; i<p->mxSample; i++){
89782      if( p->a[i].isPSample ) continue;
89783      if( iMin<0 || sampleIsBetter(p, &p->a[iMin], &p->a[i]) ){
89784        iMin = i;
89785      }
89786    }
89787    assert( iMin>=0 );
89788    p->iMin = iMin;
89789  }
89790}
89791#endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */
89792
89793/*
89794** Field iChng of the index being scanned has changed. So at this point
89795** p->current contains a sample that reflects the previous row of the
89796** index. The value of anEq[iChng] and subsequent anEq[] elements are
89797** correct at this point.
89798*/
89799static void samplePushPrevious(Stat4Accum *p, int iChng){
89800#ifdef SQLITE_ENABLE_STAT4
89801  int i;
89802
89803  /* Check if any samples from the aBest[] array should be pushed
89804  ** into IndexSample.a[] at this point.  */
89805  for(i=(p->nCol-2); i>=iChng; i--){
89806    Stat4Sample *pBest = &p->aBest[i];
89807    pBest->anEq[i] = p->current.anEq[i];
89808    if( p->nSample<p->mxSample || sampleIsBetter(p, pBest, &p->a[p->iMin]) ){
89809      sampleInsert(p, pBest, i);
89810    }
89811  }
89812
89813  /* Update the anEq[] fields of any samples already collected. */
89814  for(i=p->nSample-1; i>=0; i--){
89815    int j;
89816    for(j=iChng; j<p->nCol; j++){
89817      if( p->a[i].anEq[j]==0 ) p->a[i].anEq[j] = p->current.anEq[j];
89818    }
89819  }
89820#endif
89821
89822#if defined(SQLITE_ENABLE_STAT3) && !defined(SQLITE_ENABLE_STAT4)
89823  if( iChng==0 ){
89824    tRowcnt nLt = p->current.anLt[0];
89825    tRowcnt nEq = p->current.anEq[0];
89826
89827    /* Check if this is to be a periodic sample. If so, add it. */
89828    if( (nLt/p->nPSample)!=(nLt+nEq)/p->nPSample ){
89829      p->current.isPSample = 1;
89830      sampleInsert(p, &p->current, 0);
89831      p->current.isPSample = 0;
89832    }else
89833
89834    /* Or if it is a non-periodic sample. Add it in this case too. */
89835    if( p->nSample<p->mxSample
89836     || sampleIsBetter(p, &p->current, &p->a[p->iMin])
89837    ){
89838      sampleInsert(p, &p->current, 0);
89839    }
89840  }
89841#endif
89842
89843#ifndef SQLITE_ENABLE_STAT3_OR_STAT4
89844  UNUSED_PARAMETER( p );
89845  UNUSED_PARAMETER( iChng );
89846#endif
89847}
89848
89849/*
89850** Implementation of the stat_push SQL function:  stat_push(P,C,R)
89851** Arguments:
89852**
89853**    P     Pointer to the Stat4Accum object created by stat_init()
89854**    C     Index of left-most column to differ from previous row
89855**    R     Rowid for the current row.  Might be a key record for
89856**          WITHOUT ROWID tables.
89857**
89858** This SQL function always returns NULL.  It's purpose it to accumulate
89859** statistical data and/or samples in the Stat4Accum object about the
89860** index being analyzed.  The stat_get() SQL function will later be used to
89861** extract relevant information for constructing the sqlite_statN tables.
89862**
89863** The R parameter is only used for STAT3 and STAT4
89864*/
89865static void statPush(
89866  sqlite3_context *context,
89867  int argc,
89868  sqlite3_value **argv
89869){
89870  int i;
89871
89872  /* The three function arguments */
89873  Stat4Accum *p = (Stat4Accum*)sqlite3_value_blob(argv[0]);
89874  int iChng = sqlite3_value_int(argv[1]);
89875
89876  UNUSED_PARAMETER( argc );
89877  UNUSED_PARAMETER( context );
89878  assert( p->nCol>0 );
89879  assert( iChng<p->nCol );
89880
89881  if( p->nRow==0 ){
89882    /* This is the first call to this function. Do initialization. */
89883    for(i=0; i<p->nCol; i++) p->current.anEq[i] = 1;
89884  }else{
89885    /* Second and subsequent calls get processed here */
89886    samplePushPrevious(p, iChng);
89887
89888    /* Update anDLt[], anLt[] and anEq[] to reflect the values that apply
89889    ** to the current row of the index. */
89890    for(i=0; i<iChng; i++){
89891      p->current.anEq[i]++;
89892    }
89893    for(i=iChng; i<p->nCol; i++){
89894      p->current.anDLt[i]++;
89895#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
89896      p->current.anLt[i] += p->current.anEq[i];
89897#endif
89898      p->current.anEq[i] = 1;
89899    }
89900  }
89901  p->nRow++;
89902#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
89903  if( sqlite3_value_type(argv[2])==SQLITE_INTEGER ){
89904    sampleSetRowidInt64(p->db, &p->current, sqlite3_value_int64(argv[2]));
89905  }else{
89906    sampleSetRowid(p->db, &p->current, sqlite3_value_bytes(argv[2]),
89907                                       sqlite3_value_blob(argv[2]));
89908  }
89909  p->current.iHash = p->iPrn = p->iPrn*1103515245 + 12345;
89910#endif
89911
89912#ifdef SQLITE_ENABLE_STAT4
89913  {
89914    tRowcnt nLt = p->current.anLt[p->nCol-1];
89915
89916    /* Check if this is to be a periodic sample. If so, add it. */
89917    if( (nLt/p->nPSample)!=(nLt+1)/p->nPSample ){
89918      p->current.isPSample = 1;
89919      p->current.iCol = 0;
89920      sampleInsert(p, &p->current, p->nCol-1);
89921      p->current.isPSample = 0;
89922    }
89923
89924    /* Update the aBest[] array. */
89925    for(i=0; i<(p->nCol-1); i++){
89926      p->current.iCol = i;
89927      if( i>=iChng || sampleIsBetterPost(p, &p->current, &p->aBest[i]) ){
89928        sampleCopy(p, &p->aBest[i], &p->current);
89929      }
89930    }
89931  }
89932#endif
89933}
89934static const FuncDef statPushFuncdef = {
89935  2+IsStat34,      /* nArg */
89936  SQLITE_UTF8,     /* funcFlags */
89937  0,               /* pUserData */
89938  0,               /* pNext */
89939  statPush,        /* xFunc */
89940  0,               /* xStep */
89941  0,               /* xFinalize */
89942  "stat_push",     /* zName */
89943  0,               /* pHash */
89944  0                /* pDestructor */
89945};
89946
89947#define STAT_GET_STAT1 0          /* "stat" column of stat1 table */
89948#define STAT_GET_ROWID 1          /* "rowid" column of stat[34] entry */
89949#define STAT_GET_NEQ   2          /* "neq" column of stat[34] entry */
89950#define STAT_GET_NLT   3          /* "nlt" column of stat[34] entry */
89951#define STAT_GET_NDLT  4          /* "ndlt" column of stat[34] entry */
89952
89953/*
89954** Implementation of the stat_get(P,J) SQL function.  This routine is
89955** used to query statistical information that has been gathered into
89956** the Stat4Accum object by prior calls to stat_push().  The P parameter
89957** has type BLOB but it is really just a pointer to the Stat4Accum object.
89958** The content to returned is determined by the parameter J
89959** which is one of the STAT_GET_xxxx values defined above.
89960**
89961** If neither STAT3 nor STAT4 are enabled, then J is always
89962** STAT_GET_STAT1 and is hence omitted and this routine becomes
89963** a one-parameter function, stat_get(P), that always returns the
89964** stat1 table entry information.
89965*/
89966static void statGet(
89967  sqlite3_context *context,
89968  int argc,
89969  sqlite3_value **argv
89970){
89971  Stat4Accum *p = (Stat4Accum*)sqlite3_value_blob(argv[0]);
89972#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
89973  /* STAT3 and STAT4 have a parameter on this routine. */
89974  int eCall = sqlite3_value_int(argv[1]);
89975  assert( argc==2 );
89976  assert( eCall==STAT_GET_STAT1 || eCall==STAT_GET_NEQ
89977       || eCall==STAT_GET_ROWID || eCall==STAT_GET_NLT
89978       || eCall==STAT_GET_NDLT
89979  );
89980  if( eCall==STAT_GET_STAT1 )
89981#else
89982  assert( argc==1 );
89983#endif
89984  {
89985    /* Return the value to store in the "stat" column of the sqlite_stat1
89986    ** table for this index.
89987    **
89988    ** The value is a string composed of a list of integers describing
89989    ** the index. The first integer in the list is the total number of
89990    ** entries in the index. There is one additional integer in the list
89991    ** for each indexed column. This additional integer is an estimate of
89992    ** the number of rows matched by a stabbing query on the index using
89993    ** a key with the corresponding number of fields. In other words,
89994    ** if the index is on columns (a,b) and the sqlite_stat1 value is
89995    ** "100 10 2", then SQLite estimates that:
89996    **
89997    **   * the index contains 100 rows,
89998    **   * "WHERE a=?" matches 10 rows, and
89999    **   * "WHERE a=? AND b=?" matches 2 rows.
90000    **
90001    ** If D is the count of distinct values and K is the total number of
90002    ** rows, then each estimate is computed as:
90003    **
90004    **        I = (K+D-1)/D
90005    */
90006    char *z;
90007    int i;
90008
90009    char *zRet = sqlite3MallocZero( (p->nKeyCol+1)*25 );
90010    if( zRet==0 ){
90011      sqlite3_result_error_nomem(context);
90012      return;
90013    }
90014
90015    sqlite3_snprintf(24, zRet, "%llu", (u64)p->nRow);
90016    z = zRet + sqlite3Strlen30(zRet);
90017    for(i=0; i<p->nKeyCol; i++){
90018      u64 nDistinct = p->current.anDLt[i] + 1;
90019      u64 iVal = (p->nRow + nDistinct - 1) / nDistinct;
90020      sqlite3_snprintf(24, z, " %llu", iVal);
90021      z += sqlite3Strlen30(z);
90022      assert( p->current.anEq[i] );
90023    }
90024    assert( z[0]=='\0' && z>zRet );
90025
90026    sqlite3_result_text(context, zRet, -1, sqlite3_free);
90027  }
90028#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
90029  else if( eCall==STAT_GET_ROWID ){
90030    if( p->iGet<0 ){
90031      samplePushPrevious(p, 0);
90032      p->iGet = 0;
90033    }
90034    if( p->iGet<p->nSample ){
90035      Stat4Sample *pS = p->a + p->iGet;
90036      if( pS->nRowid==0 ){
90037        sqlite3_result_int64(context, pS->u.iRowid);
90038      }else{
90039        sqlite3_result_blob(context, pS->u.aRowid, pS->nRowid,
90040                            SQLITE_TRANSIENT);
90041      }
90042    }
90043  }else{
90044    tRowcnt *aCnt = 0;
90045
90046    assert( p->iGet<p->nSample );
90047    switch( eCall ){
90048      case STAT_GET_NEQ:  aCnt = p->a[p->iGet].anEq; break;
90049      case STAT_GET_NLT:  aCnt = p->a[p->iGet].anLt; break;
90050      default: {
90051        aCnt = p->a[p->iGet].anDLt;
90052        p->iGet++;
90053        break;
90054      }
90055    }
90056
90057    if( IsStat3 ){
90058      sqlite3_result_int64(context, (i64)aCnt[0]);
90059    }else{
90060      char *zRet = sqlite3MallocZero(p->nCol * 25);
90061      if( zRet==0 ){
90062        sqlite3_result_error_nomem(context);
90063      }else{
90064        int i;
90065        char *z = zRet;
90066        for(i=0; i<p->nCol; i++){
90067          sqlite3_snprintf(24, z, "%llu ", (u64)aCnt[i]);
90068          z += sqlite3Strlen30(z);
90069        }
90070        assert( z[0]=='\0' && z>zRet );
90071        z[-1] = '\0';
90072        sqlite3_result_text(context, zRet, -1, sqlite3_free);
90073      }
90074    }
90075  }
90076#endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */
90077#ifndef SQLITE_DEBUG
90078  UNUSED_PARAMETER( argc );
90079#endif
90080}
90081static const FuncDef statGetFuncdef = {
90082  1+IsStat34,      /* nArg */
90083  SQLITE_UTF8,     /* funcFlags */
90084  0,               /* pUserData */
90085  0,               /* pNext */
90086  statGet,         /* xFunc */
90087  0,               /* xStep */
90088  0,               /* xFinalize */
90089  "stat_get",      /* zName */
90090  0,               /* pHash */
90091  0                /* pDestructor */
90092};
90093
90094static void callStatGet(Vdbe *v, int regStat4, int iParam, int regOut){
90095  assert( regOut!=regStat4 && regOut!=regStat4+1 );
90096#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
90097  sqlite3VdbeAddOp2(v, OP_Integer, iParam, regStat4+1);
90098#elif SQLITE_DEBUG
90099  assert( iParam==STAT_GET_STAT1 );
90100#else
90101  UNUSED_PARAMETER( iParam );
90102#endif
90103  sqlite3VdbeAddOp3(v, OP_Function0, 0, regStat4, regOut);
90104  sqlite3VdbeChangeP4(v, -1, (char*)&statGetFuncdef, P4_FUNCDEF);
90105  sqlite3VdbeChangeP5(v, 1 + IsStat34);
90106}
90107
90108/*
90109** Generate code to do an analysis of all indices associated with
90110** a single table.
90111*/
90112static void analyzeOneTable(
90113  Parse *pParse,   /* Parser context */
90114  Table *pTab,     /* Table whose indices are to be analyzed */
90115  Index *pOnlyIdx, /* If not NULL, only analyze this one index */
90116  int iStatCur,    /* Index of VdbeCursor that writes the sqlite_stat1 table */
90117  int iMem,        /* Available memory locations begin here */
90118  int iTab         /* Next available cursor */
90119){
90120  sqlite3 *db = pParse->db;    /* Database handle */
90121  Index *pIdx;                 /* An index to being analyzed */
90122  int iIdxCur;                 /* Cursor open on index being analyzed */
90123  int iTabCur;                 /* Table cursor */
90124  Vdbe *v;                     /* The virtual machine being built up */
90125  int i;                       /* Loop counter */
90126  int jZeroRows = -1;          /* Jump from here if number of rows is zero */
90127  int iDb;                     /* Index of database containing pTab */
90128  u8 needTableCnt = 1;         /* True to count the table */
90129  int regNewRowid = iMem++;    /* Rowid for the inserted record */
90130  int regStat4 = iMem++;       /* Register to hold Stat4Accum object */
90131  int regChng = iMem++;        /* Index of changed index field */
90132#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
90133  int regRowid = iMem++;       /* Rowid argument passed to stat_push() */
90134#endif
90135  int regTemp = iMem++;        /* Temporary use register */
90136  int regTabname = iMem++;     /* Register containing table name */
90137  int regIdxname = iMem++;     /* Register containing index name */
90138  int regStat1 = iMem++;       /* Value for the stat column of sqlite_stat1 */
90139  int regPrev = iMem;          /* MUST BE LAST (see below) */
90140
90141  pParse->nMem = MAX(pParse->nMem, iMem);
90142  v = sqlite3GetVdbe(pParse);
90143  if( v==0 || NEVER(pTab==0) ){
90144    return;
90145  }
90146  if( pTab->tnum==0 ){
90147    /* Do not gather statistics on views or virtual tables */
90148    return;
90149  }
90150  if( sqlite3_strnicmp(pTab->zName, "sqlite_", 7)==0 ){
90151    /* Do not gather statistics on system tables */
90152    return;
90153  }
90154  assert( sqlite3BtreeHoldsAllMutexes(db) );
90155  iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
90156  assert( iDb>=0 );
90157  assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
90158#ifndef SQLITE_OMIT_AUTHORIZATION
90159  if( sqlite3AuthCheck(pParse, SQLITE_ANALYZE, pTab->zName, 0,
90160      db->aDb[iDb].zName ) ){
90161    return;
90162  }
90163#endif
90164
90165  /* Establish a read-lock on the table at the shared-cache level.
90166  ** Open a read-only cursor on the table. Also allocate a cursor number
90167  ** to use for scanning indexes (iIdxCur). No index cursor is opened at
90168  ** this time though.  */
90169  sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
90170  iTabCur = iTab++;
90171  iIdxCur = iTab++;
90172  pParse->nTab = MAX(pParse->nTab, iTab);
90173  sqlite3OpenTable(pParse, iTabCur, iDb, pTab, OP_OpenRead);
90174  sqlite3VdbeAddOp4(v, OP_String8, 0, regTabname, 0, pTab->zName, 0);
90175
90176  for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
90177    int nCol;                     /* Number of columns in pIdx. "N" */
90178    int addrRewind;               /* Address of "OP_Rewind iIdxCur" */
90179    int addrNextRow;              /* Address of "next_row:" */
90180    const char *zIdxName;         /* Name of the index */
90181    int nColTest;                 /* Number of columns to test for changes */
90182
90183    if( pOnlyIdx && pOnlyIdx!=pIdx ) continue;
90184    if( pIdx->pPartIdxWhere==0 ) needTableCnt = 0;
90185    if( !HasRowid(pTab) && IsPrimaryKeyIndex(pIdx) ){
90186      nCol = pIdx->nKeyCol;
90187      zIdxName = pTab->zName;
90188      nColTest = nCol - 1;
90189    }else{
90190      nCol = pIdx->nColumn;
90191      zIdxName = pIdx->zName;
90192      nColTest = pIdx->uniqNotNull ? pIdx->nKeyCol-1 : nCol-1;
90193    }
90194
90195    /* Populate the register containing the index name. */
90196    sqlite3VdbeAddOp4(v, OP_String8, 0, regIdxname, 0, zIdxName, 0);
90197    VdbeComment((v, "Analysis for %s.%s", pTab->zName, zIdxName));
90198
90199    /*
90200    ** Pseudo-code for loop that calls stat_push():
90201    **
90202    **   Rewind csr
90203    **   if eof(csr) goto end_of_scan;
90204    **   regChng = 0
90205    **   goto chng_addr_0;
90206    **
90207    **  next_row:
90208    **   regChng = 0
90209    **   if( idx(0) != regPrev(0) ) goto chng_addr_0
90210    **   regChng = 1
90211    **   if( idx(1) != regPrev(1) ) goto chng_addr_1
90212    **   ...
90213    **   regChng = N
90214    **   goto chng_addr_N
90215    **
90216    **  chng_addr_0:
90217    **   regPrev(0) = idx(0)
90218    **  chng_addr_1:
90219    **   regPrev(1) = idx(1)
90220    **  ...
90221    **
90222    **  endDistinctTest:
90223    **   regRowid = idx(rowid)
90224    **   stat_push(P, regChng, regRowid)
90225    **   Next csr
90226    **   if !eof(csr) goto next_row;
90227    **
90228    **  end_of_scan:
90229    */
90230
90231    /* Make sure there are enough memory cells allocated to accommodate
90232    ** the regPrev array and a trailing rowid (the rowid slot is required
90233    ** when building a record to insert into the sample column of
90234    ** the sqlite_stat4 table.  */
90235    pParse->nMem = MAX(pParse->nMem, regPrev+nColTest);
90236
90237    /* Open a read-only cursor on the index being analyzed. */
90238    assert( iDb==sqlite3SchemaToIndex(db, pIdx->pSchema) );
90239    sqlite3VdbeAddOp3(v, OP_OpenRead, iIdxCur, pIdx->tnum, iDb);
90240    sqlite3VdbeSetP4KeyInfo(pParse, pIdx);
90241    VdbeComment((v, "%s", pIdx->zName));
90242
90243    /* Invoke the stat_init() function. The arguments are:
90244    **
90245    **    (1) the number of columns in the index including the rowid
90246    **        (or for a WITHOUT ROWID table, the number of PK columns),
90247    **    (2) the number of columns in the key without the rowid/pk
90248    **    (3) the number of rows in the index,
90249    **
90250    **
90251    ** The third argument is only used for STAT3 and STAT4
90252    */
90253#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
90254    sqlite3VdbeAddOp2(v, OP_Count, iIdxCur, regStat4+3);
90255#endif
90256    sqlite3VdbeAddOp2(v, OP_Integer, nCol, regStat4+1);
90257    sqlite3VdbeAddOp2(v, OP_Integer, pIdx->nKeyCol, regStat4+2);
90258    sqlite3VdbeAddOp3(v, OP_Function0, 0, regStat4+1, regStat4);
90259    sqlite3VdbeChangeP4(v, -1, (char*)&statInitFuncdef, P4_FUNCDEF);
90260    sqlite3VdbeChangeP5(v, 2+IsStat34);
90261
90262    /* Implementation of the following:
90263    **
90264    **   Rewind csr
90265    **   if eof(csr) goto end_of_scan;
90266    **   regChng = 0
90267    **   goto next_push_0;
90268    **
90269    */
90270    addrRewind = sqlite3VdbeAddOp1(v, OP_Rewind, iIdxCur);
90271    VdbeCoverage(v);
90272    sqlite3VdbeAddOp2(v, OP_Integer, 0, regChng);
90273    addrNextRow = sqlite3VdbeCurrentAddr(v);
90274
90275    if( nColTest>0 ){
90276      int endDistinctTest = sqlite3VdbeMakeLabel(v);
90277      int *aGotoChng;               /* Array of jump instruction addresses */
90278      aGotoChng = sqlite3DbMallocRaw(db, sizeof(int)*nColTest);
90279      if( aGotoChng==0 ) continue;
90280
90281      /*
90282      **  next_row:
90283      **   regChng = 0
90284      **   if( idx(0) != regPrev(0) ) goto chng_addr_0
90285      **   regChng = 1
90286      **   if( idx(1) != regPrev(1) ) goto chng_addr_1
90287      **   ...
90288      **   regChng = N
90289      **   goto endDistinctTest
90290      */
90291      sqlite3VdbeAddOp0(v, OP_Goto);
90292      addrNextRow = sqlite3VdbeCurrentAddr(v);
90293      if( nColTest==1 && pIdx->nKeyCol==1 && IsUniqueIndex(pIdx) ){
90294        /* For a single-column UNIQUE index, once we have found a non-NULL
90295        ** row, we know that all the rest will be distinct, so skip
90296        ** subsequent distinctness tests. */
90297        sqlite3VdbeAddOp2(v, OP_NotNull, regPrev, endDistinctTest);
90298        VdbeCoverage(v);
90299      }
90300      for(i=0; i<nColTest; i++){
90301        char *pColl = (char*)sqlite3LocateCollSeq(pParse, pIdx->azColl[i]);
90302        sqlite3VdbeAddOp2(v, OP_Integer, i, regChng);
90303        sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, i, regTemp);
90304        aGotoChng[i] =
90305        sqlite3VdbeAddOp4(v, OP_Ne, regTemp, 0, regPrev+i, pColl, P4_COLLSEQ);
90306        sqlite3VdbeChangeP5(v, SQLITE_NULLEQ);
90307        VdbeCoverage(v);
90308      }
90309      sqlite3VdbeAddOp2(v, OP_Integer, nColTest, regChng);
90310      sqlite3VdbeAddOp2(v, OP_Goto, 0, endDistinctTest);
90311
90312
90313      /*
90314      **  chng_addr_0:
90315      **   regPrev(0) = idx(0)
90316      **  chng_addr_1:
90317      **   regPrev(1) = idx(1)
90318      **  ...
90319      */
90320      sqlite3VdbeJumpHere(v, addrNextRow-1);
90321      for(i=0; i<nColTest; i++){
90322        sqlite3VdbeJumpHere(v, aGotoChng[i]);
90323        sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, i, regPrev+i);
90324      }
90325      sqlite3VdbeResolveLabel(v, endDistinctTest);
90326      sqlite3DbFree(db, aGotoChng);
90327    }
90328
90329    /*
90330    **  chng_addr_N:
90331    **   regRowid = idx(rowid)            // STAT34 only
90332    **   stat_push(P, regChng, regRowid)  // 3rd parameter STAT34 only
90333    **   Next csr
90334    **   if !eof(csr) goto next_row;
90335    */
90336#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
90337    assert( regRowid==(regStat4+2) );
90338    if( HasRowid(pTab) ){
90339      sqlite3VdbeAddOp2(v, OP_IdxRowid, iIdxCur, regRowid);
90340    }else{
90341      Index *pPk = sqlite3PrimaryKeyIndex(pIdx->pTable);
90342      int j, k, regKey;
90343      regKey = sqlite3GetTempRange(pParse, pPk->nKeyCol);
90344      for(j=0; j<pPk->nKeyCol; j++){
90345        k = sqlite3ColumnOfIndex(pIdx, pPk->aiColumn[j]);
90346        sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, k, regKey+j);
90347        VdbeComment((v, "%s", pTab->aCol[pPk->aiColumn[j]].zName));
90348      }
90349      sqlite3VdbeAddOp3(v, OP_MakeRecord, regKey, pPk->nKeyCol, regRowid);
90350      sqlite3ReleaseTempRange(pParse, regKey, pPk->nKeyCol);
90351    }
90352#endif
90353    assert( regChng==(regStat4+1) );
90354    sqlite3VdbeAddOp3(v, OP_Function0, 1, regStat4, regTemp);
90355    sqlite3VdbeChangeP4(v, -1, (char*)&statPushFuncdef, P4_FUNCDEF);
90356    sqlite3VdbeChangeP5(v, 2+IsStat34);
90357    sqlite3VdbeAddOp2(v, OP_Next, iIdxCur, addrNextRow); VdbeCoverage(v);
90358
90359    /* Add the entry to the stat1 table. */
90360    callStatGet(v, regStat4, STAT_GET_STAT1, regStat1);
90361    assert( "BBB"[0]==SQLITE_AFF_TEXT );
90362    sqlite3VdbeAddOp4(v, OP_MakeRecord, regTabname, 3, regTemp, "BBB", 0);
90363    sqlite3VdbeAddOp2(v, OP_NewRowid, iStatCur, regNewRowid);
90364    sqlite3VdbeAddOp3(v, OP_Insert, iStatCur, regTemp, regNewRowid);
90365    sqlite3VdbeChangeP5(v, OPFLAG_APPEND);
90366
90367    /* Add the entries to the stat3 or stat4 table. */
90368#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
90369    {
90370      int regEq = regStat1;
90371      int regLt = regStat1+1;
90372      int regDLt = regStat1+2;
90373      int regSample = regStat1+3;
90374      int regCol = regStat1+4;
90375      int regSampleRowid = regCol + nCol;
90376      int addrNext;
90377      int addrIsNull;
90378      u8 seekOp = HasRowid(pTab) ? OP_NotExists : OP_NotFound;
90379
90380      pParse->nMem = MAX(pParse->nMem, regCol+nCol);
90381
90382      addrNext = sqlite3VdbeCurrentAddr(v);
90383      callStatGet(v, regStat4, STAT_GET_ROWID, regSampleRowid);
90384      addrIsNull = sqlite3VdbeAddOp1(v, OP_IsNull, regSampleRowid);
90385      VdbeCoverage(v);
90386      callStatGet(v, regStat4, STAT_GET_NEQ, regEq);
90387      callStatGet(v, regStat4, STAT_GET_NLT, regLt);
90388      callStatGet(v, regStat4, STAT_GET_NDLT, regDLt);
90389      sqlite3VdbeAddOp4Int(v, seekOp, iTabCur, addrNext, regSampleRowid, 0);
90390      /* We know that the regSampleRowid row exists because it was read by
90391      ** the previous loop.  Thus the not-found jump of seekOp will never
90392      ** be taken */
90393      VdbeCoverageNeverTaken(v);
90394#ifdef SQLITE_ENABLE_STAT3
90395      sqlite3ExprCodeGetColumnOfTable(v, pTab, iTabCur,
90396                                      pIdx->aiColumn[0], regSample);
90397#else
90398      for(i=0; i<nCol; i++){
90399        i16 iCol = pIdx->aiColumn[i];
90400        sqlite3ExprCodeGetColumnOfTable(v, pTab, iTabCur, iCol, regCol+i);
90401      }
90402      sqlite3VdbeAddOp3(v, OP_MakeRecord, regCol, nCol, regSample);
90403#endif
90404      sqlite3VdbeAddOp3(v, OP_MakeRecord, regTabname, 6, regTemp);
90405      sqlite3VdbeAddOp2(v, OP_NewRowid, iStatCur+1, regNewRowid);
90406      sqlite3VdbeAddOp3(v, OP_Insert, iStatCur+1, regTemp, regNewRowid);
90407      sqlite3VdbeAddOp2(v, OP_Goto, 1, addrNext); /* P1==1 for end-of-loop */
90408      sqlite3VdbeJumpHere(v, addrIsNull);
90409    }
90410#endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */
90411
90412    /* End of analysis */
90413    sqlite3VdbeJumpHere(v, addrRewind);
90414  }
90415
90416
90417  /* Create a single sqlite_stat1 entry containing NULL as the index
90418  ** name and the row count as the content.
90419  */
90420  if( pOnlyIdx==0 && needTableCnt ){
90421    VdbeComment((v, "%s", pTab->zName));
90422    sqlite3VdbeAddOp2(v, OP_Count, iTabCur, regStat1);
90423    jZeroRows = sqlite3VdbeAddOp1(v, OP_IfNot, regStat1); VdbeCoverage(v);
90424    sqlite3VdbeAddOp2(v, OP_Null, 0, regIdxname);
90425    assert( "BBB"[0]==SQLITE_AFF_TEXT );
90426    sqlite3VdbeAddOp4(v, OP_MakeRecord, regTabname, 3, regTemp, "BBB", 0);
90427    sqlite3VdbeAddOp2(v, OP_NewRowid, iStatCur, regNewRowid);
90428    sqlite3VdbeAddOp3(v, OP_Insert, iStatCur, regTemp, regNewRowid);
90429    sqlite3VdbeChangeP5(v, OPFLAG_APPEND);
90430    sqlite3VdbeJumpHere(v, jZeroRows);
90431  }
90432}
90433
90434
90435/*
90436** Generate code that will cause the most recent index analysis to
90437** be loaded into internal hash tables where is can be used.
90438*/
90439static void loadAnalysis(Parse *pParse, int iDb){
90440  Vdbe *v = sqlite3GetVdbe(pParse);
90441  if( v ){
90442    sqlite3VdbeAddOp1(v, OP_LoadAnalysis, iDb);
90443  }
90444}
90445
90446/*
90447** Generate code that will do an analysis of an entire database
90448*/
90449static void analyzeDatabase(Parse *pParse, int iDb){
90450  sqlite3 *db = pParse->db;
90451  Schema *pSchema = db->aDb[iDb].pSchema;    /* Schema of database iDb */
90452  HashElem *k;
90453  int iStatCur;
90454  int iMem;
90455  int iTab;
90456
90457  sqlite3BeginWriteOperation(pParse, 0, iDb);
90458  iStatCur = pParse->nTab;
90459  pParse->nTab += 3;
90460  openStatTable(pParse, iDb, iStatCur, 0, 0);
90461  iMem = pParse->nMem+1;
90462  iTab = pParse->nTab;
90463  assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
90464  for(k=sqliteHashFirst(&pSchema->tblHash); k; k=sqliteHashNext(k)){
90465    Table *pTab = (Table*)sqliteHashData(k);
90466    analyzeOneTable(pParse, pTab, 0, iStatCur, iMem, iTab);
90467  }
90468  loadAnalysis(pParse, iDb);
90469}
90470
90471/*
90472** Generate code that will do an analysis of a single table in
90473** a database.  If pOnlyIdx is not NULL then it is a single index
90474** in pTab that should be analyzed.
90475*/
90476static void analyzeTable(Parse *pParse, Table *pTab, Index *pOnlyIdx){
90477  int iDb;
90478  int iStatCur;
90479
90480  assert( pTab!=0 );
90481  assert( sqlite3BtreeHoldsAllMutexes(pParse->db) );
90482  iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
90483  sqlite3BeginWriteOperation(pParse, 0, iDb);
90484  iStatCur = pParse->nTab;
90485  pParse->nTab += 3;
90486  if( pOnlyIdx ){
90487    openStatTable(pParse, iDb, iStatCur, pOnlyIdx->zName, "idx");
90488  }else{
90489    openStatTable(pParse, iDb, iStatCur, pTab->zName, "tbl");
90490  }
90491  analyzeOneTable(pParse, pTab, pOnlyIdx, iStatCur,pParse->nMem+1,pParse->nTab);
90492  loadAnalysis(pParse, iDb);
90493}
90494
90495/*
90496** Generate code for the ANALYZE command.  The parser calls this routine
90497** when it recognizes an ANALYZE command.
90498**
90499**        ANALYZE                            -- 1
90500**        ANALYZE  <database>                -- 2
90501**        ANALYZE  ?<database>.?<tablename>  -- 3
90502**
90503** Form 1 causes all indices in all attached databases to be analyzed.
90504** Form 2 analyzes all indices the single database named.
90505** Form 3 analyzes all indices associated with the named table.
90506*/
90507SQLITE_PRIVATE void sqlite3Analyze(Parse *pParse, Token *pName1, Token *pName2){
90508  sqlite3 *db = pParse->db;
90509  int iDb;
90510  int i;
90511  char *z, *zDb;
90512  Table *pTab;
90513  Index *pIdx;
90514  Token *pTableName;
90515  Vdbe *v;
90516
90517  /* Read the database schema. If an error occurs, leave an error message
90518  ** and code in pParse and return NULL. */
90519  assert( sqlite3BtreeHoldsAllMutexes(pParse->db) );
90520  if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){
90521    return;
90522  }
90523
90524  assert( pName2!=0 || pName1==0 );
90525  if( pName1==0 ){
90526    /* Form 1:  Analyze everything */
90527    for(i=0; i<db->nDb; i++){
90528      if( i==1 ) continue;  /* Do not analyze the TEMP database */
90529      analyzeDatabase(pParse, i);
90530    }
90531  }else if( pName2->n==0 ){
90532    /* Form 2:  Analyze the database or table named */
90533    iDb = sqlite3FindDb(db, pName1);
90534    if( iDb>=0 ){
90535      analyzeDatabase(pParse, iDb);
90536    }else{
90537      z = sqlite3NameFromToken(db, pName1);
90538      if( z ){
90539        if( (pIdx = sqlite3FindIndex(db, z, 0))!=0 ){
90540          analyzeTable(pParse, pIdx->pTable, pIdx);
90541        }else if( (pTab = sqlite3LocateTable(pParse, 0, z, 0))!=0 ){
90542          analyzeTable(pParse, pTab, 0);
90543        }
90544        sqlite3DbFree(db, z);
90545      }
90546    }
90547  }else{
90548    /* Form 3: Analyze the fully qualified table name */
90549    iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pTableName);
90550    if( iDb>=0 ){
90551      zDb = db->aDb[iDb].zName;
90552      z = sqlite3NameFromToken(db, pTableName);
90553      if( z ){
90554        if( (pIdx = sqlite3FindIndex(db, z, zDb))!=0 ){
90555          analyzeTable(pParse, pIdx->pTable, pIdx);
90556        }else if( (pTab = sqlite3LocateTable(pParse, 0, z, zDb))!=0 ){
90557          analyzeTable(pParse, pTab, 0);
90558        }
90559        sqlite3DbFree(db, z);
90560      }
90561    }
90562  }
90563  v = sqlite3GetVdbe(pParse);
90564  if( v ) sqlite3VdbeAddOp0(v, OP_Expire);
90565}
90566
90567/*
90568** Used to pass information from the analyzer reader through to the
90569** callback routine.
90570*/
90571typedef struct analysisInfo analysisInfo;
90572struct analysisInfo {
90573  sqlite3 *db;
90574  const char *zDatabase;
90575};
90576
90577/*
90578** The first argument points to a nul-terminated string containing a
90579** list of space separated integers. Read the first nOut of these into
90580** the array aOut[].
90581*/
90582static void decodeIntArray(
90583  char *zIntArray,       /* String containing int array to decode */
90584  int nOut,              /* Number of slots in aOut[] */
90585  tRowcnt *aOut,         /* Store integers here */
90586  LogEst *aLog,          /* Or, if aOut==0, here */
90587  Index *pIndex          /* Handle extra flags for this index, if not NULL */
90588){
90589  char *z = zIntArray;
90590  int c;
90591  int i;
90592  tRowcnt v;
90593
90594#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
90595  if( z==0 ) z = "";
90596#else
90597  assert( z!=0 );
90598#endif
90599  for(i=0; *z && i<nOut; i++){
90600    v = 0;
90601    while( (c=z[0])>='0' && c<='9' ){
90602      v = v*10 + c - '0';
90603      z++;
90604    }
90605#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
90606    if( aOut ) aOut[i] = v;
90607    if( aLog ) aLog[i] = sqlite3LogEst(v);
90608#else
90609    assert( aOut==0 );
90610    UNUSED_PARAMETER(aOut);
90611    assert( aLog!=0 );
90612    aLog[i] = sqlite3LogEst(v);
90613#endif
90614    if( *z==' ' ) z++;
90615  }
90616#ifndef SQLITE_ENABLE_STAT3_OR_STAT4
90617  assert( pIndex!=0 ); {
90618#else
90619  if( pIndex ){
90620#endif
90621    pIndex->bUnordered = 0;
90622    pIndex->noSkipScan = 0;
90623    while( z[0] ){
90624      if( sqlite3_strglob("unordered*", z)==0 ){
90625        pIndex->bUnordered = 1;
90626      }else if( sqlite3_strglob("sz=[0-9]*", z)==0 ){
90627        pIndex->szIdxRow = sqlite3LogEst(sqlite3Atoi(z+3));
90628      }else if( sqlite3_strglob("noskipscan*", z)==0 ){
90629        pIndex->noSkipScan = 1;
90630      }
90631#ifdef SQLITE_ENABLE_COSTMULT
90632      else if( sqlite3_strglob("costmult=[0-9]*",z)==0 ){
90633        pIndex->pTable->costMult = sqlite3LogEst(sqlite3Atoi(z+9));
90634      }
90635#endif
90636      while( z[0]!=0 && z[0]!=' ' ) z++;
90637      while( z[0]==' ' ) z++;
90638    }
90639  }
90640}
90641
90642/*
90643** This callback is invoked once for each index when reading the
90644** sqlite_stat1 table.
90645**
90646**     argv[0] = name of the table
90647**     argv[1] = name of the index (might be NULL)
90648**     argv[2] = results of analysis - on integer for each column
90649**
90650** Entries for which argv[1]==NULL simply record the number of rows in
90651** the table.
90652*/
90653static int analysisLoader(void *pData, int argc, char **argv, char **NotUsed){
90654  analysisInfo *pInfo = (analysisInfo*)pData;
90655  Index *pIndex;
90656  Table *pTable;
90657  const char *z;
90658
90659  assert( argc==3 );
90660  UNUSED_PARAMETER2(NotUsed, argc);
90661
90662  if( argv==0 || argv[0]==0 || argv[2]==0 ){
90663    return 0;
90664  }
90665  pTable = sqlite3FindTable(pInfo->db, argv[0], pInfo->zDatabase);
90666  if( pTable==0 ){
90667    return 0;
90668  }
90669  if( argv[1]==0 ){
90670    pIndex = 0;
90671  }else if( sqlite3_stricmp(argv[0],argv[1])==0 ){
90672    pIndex = sqlite3PrimaryKeyIndex(pTable);
90673  }else{
90674    pIndex = sqlite3FindIndex(pInfo->db, argv[1], pInfo->zDatabase);
90675  }
90676  z = argv[2];
90677
90678  if( pIndex ){
90679    tRowcnt *aiRowEst = 0;
90680    int nCol = pIndex->nKeyCol+1;
90681#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
90682    /* Index.aiRowEst may already be set here if there are duplicate
90683    ** sqlite_stat1 entries for this index. In that case just clobber
90684    ** the old data with the new instead of allocating a new array.  */
90685    if( pIndex->aiRowEst==0 ){
90686      pIndex->aiRowEst = (tRowcnt*)sqlite3MallocZero(sizeof(tRowcnt) * nCol);
90687      if( pIndex->aiRowEst==0 ) pInfo->db->mallocFailed = 1;
90688    }
90689    aiRowEst = pIndex->aiRowEst;
90690#endif
90691    pIndex->bUnordered = 0;
90692    decodeIntArray((char*)z, nCol, aiRowEst, pIndex->aiRowLogEst, pIndex);
90693    if( pIndex->pPartIdxWhere==0 ) pTable->nRowLogEst = pIndex->aiRowLogEst[0];
90694  }else{
90695    Index fakeIdx;
90696    fakeIdx.szIdxRow = pTable->szTabRow;
90697#ifdef SQLITE_ENABLE_COSTMULT
90698    fakeIdx.pTable = pTable;
90699#endif
90700    decodeIntArray((char*)z, 1, 0, &pTable->nRowLogEst, &fakeIdx);
90701    pTable->szTabRow = fakeIdx.szIdxRow;
90702  }
90703
90704  return 0;
90705}
90706
90707/*
90708** If the Index.aSample variable is not NULL, delete the aSample[] array
90709** and its contents.
90710*/
90711SQLITE_PRIVATE void sqlite3DeleteIndexSamples(sqlite3 *db, Index *pIdx){
90712#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
90713  if( pIdx->aSample ){
90714    int j;
90715    for(j=0; j<pIdx->nSample; j++){
90716      IndexSample *p = &pIdx->aSample[j];
90717      sqlite3DbFree(db, p->p);
90718    }
90719    sqlite3DbFree(db, pIdx->aSample);
90720  }
90721  if( db && db->pnBytesFreed==0 ){
90722    pIdx->nSample = 0;
90723    pIdx->aSample = 0;
90724  }
90725#else
90726  UNUSED_PARAMETER(db);
90727  UNUSED_PARAMETER(pIdx);
90728#endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */
90729}
90730
90731#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
90732/*
90733** Populate the pIdx->aAvgEq[] array based on the samples currently
90734** stored in pIdx->aSample[].
90735*/
90736static void initAvgEq(Index *pIdx){
90737  if( pIdx ){
90738    IndexSample *aSample = pIdx->aSample;
90739    IndexSample *pFinal = &aSample[pIdx->nSample-1];
90740    int iCol;
90741    int nCol = 1;
90742    if( pIdx->nSampleCol>1 ){
90743      /* If this is stat4 data, then calculate aAvgEq[] values for all
90744      ** sample columns except the last. The last is always set to 1, as
90745      ** once the trailing PK fields are considered all index keys are
90746      ** unique.  */
90747      nCol = pIdx->nSampleCol-1;
90748      pIdx->aAvgEq[nCol] = 1;
90749    }
90750    for(iCol=0; iCol<nCol; iCol++){
90751      int nSample = pIdx->nSample;
90752      int i;                    /* Used to iterate through samples */
90753      tRowcnt sumEq = 0;        /* Sum of the nEq values */
90754      tRowcnt avgEq = 0;
90755      tRowcnt nRow;             /* Number of rows in index */
90756      i64 nSum100 = 0;          /* Number of terms contributing to sumEq */
90757      i64 nDist100;             /* Number of distinct values in index */
90758
90759      if( !pIdx->aiRowEst || iCol>=pIdx->nKeyCol || pIdx->aiRowEst[iCol+1]==0 ){
90760        nRow = pFinal->anLt[iCol];
90761        nDist100 = (i64)100 * pFinal->anDLt[iCol];
90762        nSample--;
90763      }else{
90764        nRow = pIdx->aiRowEst[0];
90765        nDist100 = ((i64)100 * pIdx->aiRowEst[0]) / pIdx->aiRowEst[iCol+1];
90766      }
90767      pIdx->nRowEst0 = nRow;
90768
90769      /* Set nSum to the number of distinct (iCol+1) field prefixes that
90770      ** occur in the stat4 table for this index. Set sumEq to the sum of
90771      ** the nEq values for column iCol for the same set (adding the value
90772      ** only once where there exist duplicate prefixes).  */
90773      for(i=0; i<nSample; i++){
90774        if( i==(pIdx->nSample-1)
90775         || aSample[i].anDLt[iCol]!=aSample[i+1].anDLt[iCol]
90776        ){
90777          sumEq += aSample[i].anEq[iCol];
90778          nSum100 += 100;
90779        }
90780      }
90781
90782      if( nDist100>nSum100 ){
90783        avgEq = ((i64)100 * (nRow - sumEq))/(nDist100 - nSum100);
90784      }
90785      if( avgEq==0 ) avgEq = 1;
90786      pIdx->aAvgEq[iCol] = avgEq;
90787    }
90788  }
90789}
90790
90791/*
90792** Look up an index by name.  Or, if the name of a WITHOUT ROWID table
90793** is supplied instead, find the PRIMARY KEY index for that table.
90794*/
90795static Index *findIndexOrPrimaryKey(
90796  sqlite3 *db,
90797  const char *zName,
90798  const char *zDb
90799){
90800  Index *pIdx = sqlite3FindIndex(db, zName, zDb);
90801  if( pIdx==0 ){
90802    Table *pTab = sqlite3FindTable(db, zName, zDb);
90803    if( pTab && !HasRowid(pTab) ) pIdx = sqlite3PrimaryKeyIndex(pTab);
90804  }
90805  return pIdx;
90806}
90807
90808/*
90809** Load the content from either the sqlite_stat4 or sqlite_stat3 table
90810** into the relevant Index.aSample[] arrays.
90811**
90812** Arguments zSql1 and zSql2 must point to SQL statements that return
90813** data equivalent to the following (statements are different for stat3,
90814** see the caller of this function for details):
90815**
90816**    zSql1: SELECT idx,count(*) FROM %Q.sqlite_stat4 GROUP BY idx
90817**    zSql2: SELECT idx,neq,nlt,ndlt,sample FROM %Q.sqlite_stat4
90818**
90819** where %Q is replaced with the database name before the SQL is executed.
90820*/
90821static int loadStatTbl(
90822  sqlite3 *db,                  /* Database handle */
90823  int bStat3,                   /* Assume single column records only */
90824  const char *zSql1,            /* SQL statement 1 (see above) */
90825  const char *zSql2,            /* SQL statement 2 (see above) */
90826  const char *zDb               /* Database name (e.g. "main") */
90827){
90828  int rc;                       /* Result codes from subroutines */
90829  sqlite3_stmt *pStmt = 0;      /* An SQL statement being run */
90830  char *zSql;                   /* Text of the SQL statement */
90831  Index *pPrevIdx = 0;          /* Previous index in the loop */
90832  IndexSample *pSample;         /* A slot in pIdx->aSample[] */
90833
90834  assert( db->lookaside.bEnabled==0 );
90835  zSql = sqlite3MPrintf(db, zSql1, zDb);
90836  if( !zSql ){
90837    return SQLITE_NOMEM;
90838  }
90839  rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0);
90840  sqlite3DbFree(db, zSql);
90841  if( rc ) return rc;
90842
90843  while( sqlite3_step(pStmt)==SQLITE_ROW ){
90844    int nIdxCol = 1;              /* Number of columns in stat4 records */
90845
90846    char *zIndex;   /* Index name */
90847    Index *pIdx;    /* Pointer to the index object */
90848    int nSample;    /* Number of samples */
90849    int nByte;      /* Bytes of space required */
90850    int i;          /* Bytes of space required */
90851    tRowcnt *pSpace;
90852
90853    zIndex = (char *)sqlite3_column_text(pStmt, 0);
90854    if( zIndex==0 ) continue;
90855    nSample = sqlite3_column_int(pStmt, 1);
90856    pIdx = findIndexOrPrimaryKey(db, zIndex, zDb);
90857    assert( pIdx==0 || bStat3 || pIdx->nSample==0 );
90858    /* Index.nSample is non-zero at this point if data has already been
90859    ** loaded from the stat4 table. In this case ignore stat3 data.  */
90860    if( pIdx==0 || pIdx->nSample ) continue;
90861    if( bStat3==0 ){
90862      assert( !HasRowid(pIdx->pTable) || pIdx->nColumn==pIdx->nKeyCol+1 );
90863      if( !HasRowid(pIdx->pTable) && IsPrimaryKeyIndex(pIdx) ){
90864        nIdxCol = pIdx->nKeyCol;
90865      }else{
90866        nIdxCol = pIdx->nColumn;
90867      }
90868    }
90869    pIdx->nSampleCol = nIdxCol;
90870    nByte = sizeof(IndexSample) * nSample;
90871    nByte += sizeof(tRowcnt) * nIdxCol * 3 * nSample;
90872    nByte += nIdxCol * sizeof(tRowcnt);     /* Space for Index.aAvgEq[] */
90873
90874    pIdx->aSample = sqlite3DbMallocZero(db, nByte);
90875    if( pIdx->aSample==0 ){
90876      sqlite3_finalize(pStmt);
90877      return SQLITE_NOMEM;
90878    }
90879    pSpace = (tRowcnt*)&pIdx->aSample[nSample];
90880    pIdx->aAvgEq = pSpace; pSpace += nIdxCol;
90881    for(i=0; i<nSample; i++){
90882      pIdx->aSample[i].anEq = pSpace; pSpace += nIdxCol;
90883      pIdx->aSample[i].anLt = pSpace; pSpace += nIdxCol;
90884      pIdx->aSample[i].anDLt = pSpace; pSpace += nIdxCol;
90885    }
90886    assert( ((u8*)pSpace)-nByte==(u8*)(pIdx->aSample) );
90887  }
90888  rc = sqlite3_finalize(pStmt);
90889  if( rc ) return rc;
90890
90891  zSql = sqlite3MPrintf(db, zSql2, zDb);
90892  if( !zSql ){
90893    return SQLITE_NOMEM;
90894  }
90895  rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0);
90896  sqlite3DbFree(db, zSql);
90897  if( rc ) return rc;
90898
90899  while( sqlite3_step(pStmt)==SQLITE_ROW ){
90900    char *zIndex;                 /* Index name */
90901    Index *pIdx;                  /* Pointer to the index object */
90902    int nCol = 1;                 /* Number of columns in index */
90903
90904    zIndex = (char *)sqlite3_column_text(pStmt, 0);
90905    if( zIndex==0 ) continue;
90906    pIdx = findIndexOrPrimaryKey(db, zIndex, zDb);
90907    if( pIdx==0 ) continue;
90908    /* This next condition is true if data has already been loaded from
90909    ** the sqlite_stat4 table. In this case ignore stat3 data.  */
90910    nCol = pIdx->nSampleCol;
90911    if( bStat3 && nCol>1 ) continue;
90912    if( pIdx!=pPrevIdx ){
90913      initAvgEq(pPrevIdx);
90914      pPrevIdx = pIdx;
90915    }
90916    pSample = &pIdx->aSample[pIdx->nSample];
90917    decodeIntArray((char*)sqlite3_column_text(pStmt,1),nCol,pSample->anEq,0,0);
90918    decodeIntArray((char*)sqlite3_column_text(pStmt,2),nCol,pSample->anLt,0,0);
90919    decodeIntArray((char*)sqlite3_column_text(pStmt,3),nCol,pSample->anDLt,0,0);
90920
90921    /* Take a copy of the sample. Add two 0x00 bytes the end of the buffer.
90922    ** This is in case the sample record is corrupted. In that case, the
90923    ** sqlite3VdbeRecordCompare() may read up to two varints past the
90924    ** end of the allocated buffer before it realizes it is dealing with
90925    ** a corrupt record. Adding the two 0x00 bytes prevents this from causing
90926    ** a buffer overread.  */
90927    pSample->n = sqlite3_column_bytes(pStmt, 4);
90928    pSample->p = sqlite3DbMallocZero(db, pSample->n + 2);
90929    if( pSample->p==0 ){
90930      sqlite3_finalize(pStmt);
90931      return SQLITE_NOMEM;
90932    }
90933    memcpy(pSample->p, sqlite3_column_blob(pStmt, 4), pSample->n);
90934    pIdx->nSample++;
90935  }
90936  rc = sqlite3_finalize(pStmt);
90937  if( rc==SQLITE_OK ) initAvgEq(pPrevIdx);
90938  return rc;
90939}
90940
90941/*
90942** Load content from the sqlite_stat4 and sqlite_stat3 tables into
90943** the Index.aSample[] arrays of all indices.
90944*/
90945static int loadStat4(sqlite3 *db, const char *zDb){
90946  int rc = SQLITE_OK;             /* Result codes from subroutines */
90947
90948  assert( db->lookaside.bEnabled==0 );
90949  if( sqlite3FindTable(db, "sqlite_stat4", zDb) ){
90950    rc = loadStatTbl(db, 0,
90951      "SELECT idx,count(*) FROM %Q.sqlite_stat4 GROUP BY idx",
90952      "SELECT idx,neq,nlt,ndlt,sample FROM %Q.sqlite_stat4",
90953      zDb
90954    );
90955  }
90956
90957  if( rc==SQLITE_OK && sqlite3FindTable(db, "sqlite_stat3", zDb) ){
90958    rc = loadStatTbl(db, 1,
90959      "SELECT idx,count(*) FROM %Q.sqlite_stat3 GROUP BY idx",
90960      "SELECT idx,neq,nlt,ndlt,sqlite_record(sample) FROM %Q.sqlite_stat3",
90961      zDb
90962    );
90963  }
90964
90965  return rc;
90966}
90967#endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */
90968
90969/*
90970** Load the content of the sqlite_stat1 and sqlite_stat3/4 tables. The
90971** contents of sqlite_stat1 are used to populate the Index.aiRowEst[]
90972** arrays. The contents of sqlite_stat3/4 are used to populate the
90973** Index.aSample[] arrays.
90974**
90975** If the sqlite_stat1 table is not present in the database, SQLITE_ERROR
90976** is returned. In this case, even if SQLITE_ENABLE_STAT3/4 was defined
90977** during compilation and the sqlite_stat3/4 table is present, no data is
90978** read from it.
90979**
90980** If SQLITE_ENABLE_STAT3/4 was defined during compilation and the
90981** sqlite_stat4 table is not present in the database, SQLITE_ERROR is
90982** returned. However, in this case, data is read from the sqlite_stat1
90983** table (if it is present) before returning.
90984**
90985** If an OOM error occurs, this function always sets db->mallocFailed.
90986** This means if the caller does not care about other errors, the return
90987** code may be ignored.
90988*/
90989SQLITE_PRIVATE int sqlite3AnalysisLoad(sqlite3 *db, int iDb){
90990  analysisInfo sInfo;
90991  HashElem *i;
90992  char *zSql;
90993  int rc;
90994
90995  assert( iDb>=0 && iDb<db->nDb );
90996  assert( db->aDb[iDb].pBt!=0 );
90997
90998  /* Clear any prior statistics */
90999  assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
91000  for(i=sqliteHashFirst(&db->aDb[iDb].pSchema->idxHash);i;i=sqliteHashNext(i)){
91001    Index *pIdx = sqliteHashData(i);
91002    sqlite3DefaultRowEst(pIdx);
91003#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
91004    sqlite3DeleteIndexSamples(db, pIdx);
91005    pIdx->aSample = 0;
91006#endif
91007  }
91008
91009  /* Check to make sure the sqlite_stat1 table exists */
91010  sInfo.db = db;
91011  sInfo.zDatabase = db->aDb[iDb].zName;
91012  if( sqlite3FindTable(db, "sqlite_stat1", sInfo.zDatabase)==0 ){
91013    return SQLITE_ERROR;
91014  }
91015
91016  /* Load new statistics out of the sqlite_stat1 table */
91017  zSql = sqlite3MPrintf(db,
91018      "SELECT tbl,idx,stat FROM %Q.sqlite_stat1", sInfo.zDatabase);
91019  if( zSql==0 ){
91020    rc = SQLITE_NOMEM;
91021  }else{
91022    rc = sqlite3_exec(db, zSql, analysisLoader, &sInfo, 0);
91023    sqlite3DbFree(db, zSql);
91024  }
91025
91026
91027  /* Load the statistics from the sqlite_stat4 table. */
91028#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
91029  if( rc==SQLITE_OK && OptimizationEnabled(db, SQLITE_Stat34) ){
91030    int lookasideEnabled = db->lookaside.bEnabled;
91031    db->lookaside.bEnabled = 0;
91032    rc = loadStat4(db, sInfo.zDatabase);
91033    db->lookaside.bEnabled = lookasideEnabled;
91034  }
91035  for(i=sqliteHashFirst(&db->aDb[iDb].pSchema->idxHash);i;i=sqliteHashNext(i)){
91036    Index *pIdx = sqliteHashData(i);
91037    sqlite3_free(pIdx->aiRowEst);
91038    pIdx->aiRowEst = 0;
91039  }
91040#endif
91041
91042  if( rc==SQLITE_NOMEM ){
91043    db->mallocFailed = 1;
91044  }
91045  return rc;
91046}
91047
91048
91049#endif /* SQLITE_OMIT_ANALYZE */
91050
91051/************** End of analyze.c *********************************************/
91052/************** Begin file attach.c ******************************************/
91053/*
91054** 2003 April 6
91055**
91056** The author disclaims copyright to this source code.  In place of
91057** a legal notice, here is a blessing:
91058**
91059**    May you do good and not evil.
91060**    May you find forgiveness for yourself and forgive others.
91061**    May you share freely, never taking more than you give.
91062**
91063*************************************************************************
91064** This file contains code used to implement the ATTACH and DETACH commands.
91065*/
91066/* #include "sqliteInt.h" */
91067
91068#ifndef SQLITE_OMIT_ATTACH
91069/*
91070** Resolve an expression that was part of an ATTACH or DETACH statement. This
91071** is slightly different from resolving a normal SQL expression, because simple
91072** identifiers are treated as strings, not possible column names or aliases.
91073**
91074** i.e. if the parser sees:
91075**
91076**     ATTACH DATABASE abc AS def
91077**
91078** it treats the two expressions as literal strings 'abc' and 'def' instead of
91079** looking for columns of the same name.
91080**
91081** This only applies to the root node of pExpr, so the statement:
91082**
91083**     ATTACH DATABASE abc||def AS 'db2'
91084**
91085** will fail because neither abc or def can be resolved.
91086*/
91087static int resolveAttachExpr(NameContext *pName, Expr *pExpr)
91088{
91089  int rc = SQLITE_OK;
91090  if( pExpr ){
91091    if( pExpr->op!=TK_ID ){
91092      rc = sqlite3ResolveExprNames(pName, pExpr);
91093    }else{
91094      pExpr->op = TK_STRING;
91095    }
91096  }
91097  return rc;
91098}
91099
91100/*
91101** An SQL user-function registered to do the work of an ATTACH statement. The
91102** three arguments to the function come directly from an attach statement:
91103**
91104**     ATTACH DATABASE x AS y KEY z
91105**
91106**     SELECT sqlite_attach(x, y, z)
91107**
91108** If the optional "KEY z" syntax is omitted, an SQL NULL is passed as the
91109** third argument.
91110*/
91111static void attachFunc(
91112  sqlite3_context *context,
91113  int NotUsed,
91114  sqlite3_value **argv
91115){
91116  int i;
91117  int rc = 0;
91118  sqlite3 *db = sqlite3_context_db_handle(context);
91119  const char *zName;
91120  const char *zFile;
91121  char *zPath = 0;
91122  char *zErr = 0;
91123  unsigned int flags;
91124  Db *aNew;
91125  char *zErrDyn = 0;
91126  sqlite3_vfs *pVfs;
91127
91128  UNUSED_PARAMETER(NotUsed);
91129
91130  zFile = (const char *)sqlite3_value_text(argv[0]);
91131  zName = (const char *)sqlite3_value_text(argv[1]);
91132  if( zFile==0 ) zFile = "";
91133  if( zName==0 ) zName = "";
91134
91135  /* Check for the following errors:
91136  **
91137  **     * Too many attached databases,
91138  **     * Transaction currently open
91139  **     * Specified database name already being used.
91140  */
91141  if( db->nDb>=db->aLimit[SQLITE_LIMIT_ATTACHED]+2 ){
91142    zErrDyn = sqlite3MPrintf(db, "too many attached databases - max %d",
91143      db->aLimit[SQLITE_LIMIT_ATTACHED]
91144    );
91145    goto attach_error;
91146  }
91147  if( !db->autoCommit ){
91148    zErrDyn = sqlite3MPrintf(db, "cannot ATTACH database within transaction");
91149    goto attach_error;
91150  }
91151  for(i=0; i<db->nDb; i++){
91152    char *z = db->aDb[i].zName;
91153    assert( z && zName );
91154    if( sqlite3StrICmp(z, zName)==0 ){
91155      zErrDyn = sqlite3MPrintf(db, "database %s is already in use", zName);
91156      goto attach_error;
91157    }
91158  }
91159
91160  /* Allocate the new entry in the db->aDb[] array and initialize the schema
91161  ** hash tables.
91162  */
91163  if( db->aDb==db->aDbStatic ){
91164    aNew = sqlite3DbMallocRaw(db, sizeof(db->aDb[0])*3 );
91165    if( aNew==0 ) return;
91166    memcpy(aNew, db->aDb, sizeof(db->aDb[0])*2);
91167  }else{
91168    aNew = sqlite3DbRealloc(db, db->aDb, sizeof(db->aDb[0])*(db->nDb+1) );
91169    if( aNew==0 ) return;
91170  }
91171  db->aDb = aNew;
91172  aNew = &db->aDb[db->nDb];
91173  memset(aNew, 0, sizeof(*aNew));
91174
91175  /* Open the database file. If the btree is successfully opened, use
91176  ** it to obtain the database schema. At this point the schema may
91177  ** or may not be initialized.
91178  */
91179  flags = db->openFlags;
91180  rc = sqlite3ParseUri(db->pVfs->zName, zFile, &flags, &pVfs, &zPath, &zErr);
91181  if( rc!=SQLITE_OK ){
91182    if( rc==SQLITE_NOMEM ) db->mallocFailed = 1;
91183    sqlite3_result_error(context, zErr, -1);
91184    sqlite3_free(zErr);
91185    return;
91186  }
91187  assert( pVfs );
91188  flags |= SQLITE_OPEN_MAIN_DB;
91189  rc = sqlite3BtreeOpen(pVfs, zPath, db, &aNew->pBt, 0, flags);
91190  sqlite3_free( zPath );
91191  db->nDb++;
91192  if( rc==SQLITE_CONSTRAINT ){
91193    rc = SQLITE_ERROR;
91194    zErrDyn = sqlite3MPrintf(db, "database is already attached");
91195  }else if( rc==SQLITE_OK ){
91196    Pager *pPager;
91197    aNew->pSchema = sqlite3SchemaGet(db, aNew->pBt);
91198    if( !aNew->pSchema ){
91199      rc = SQLITE_NOMEM;
91200    }else if( aNew->pSchema->file_format && aNew->pSchema->enc!=ENC(db) ){
91201      zErrDyn = sqlite3MPrintf(db,
91202        "attached databases must use the same text encoding as main database");
91203      rc = SQLITE_ERROR;
91204    }
91205    sqlite3BtreeEnter(aNew->pBt);
91206    pPager = sqlite3BtreePager(aNew->pBt);
91207    sqlite3PagerLockingMode(pPager, db->dfltLockMode);
91208    sqlite3BtreeSecureDelete(aNew->pBt,
91209                             sqlite3BtreeSecureDelete(db->aDb[0].pBt,-1) );
91210#ifndef SQLITE_OMIT_PAGER_PRAGMAS
91211    sqlite3BtreeSetPagerFlags(aNew->pBt, 3 | (db->flags & PAGER_FLAGS_MASK));
91212#endif
91213    sqlite3BtreeLeave(aNew->pBt);
91214  }
91215  aNew->safety_level = 3;
91216  aNew->zName = sqlite3DbStrDup(db, zName);
91217  if( rc==SQLITE_OK && aNew->zName==0 ){
91218    rc = SQLITE_NOMEM;
91219  }
91220
91221
91222#ifdef SQLITE_HAS_CODEC
91223  if( rc==SQLITE_OK ){
91224    extern int sqlite3CodecAttach(sqlite3*, int, const void*, int);
91225    extern void sqlite3CodecGetKey(sqlite3*, int, void**, int*);
91226    int nKey;
91227    char *zKey;
91228    int t = sqlite3_value_type(argv[2]);
91229    switch( t ){
91230      case SQLITE_INTEGER:
91231      case SQLITE_FLOAT:
91232        zErrDyn = sqlite3DbStrDup(db, "Invalid key value");
91233        rc = SQLITE_ERROR;
91234        break;
91235
91236      case SQLITE_TEXT:
91237      case SQLITE_BLOB:
91238        nKey = sqlite3_value_bytes(argv[2]);
91239        zKey = (char *)sqlite3_value_blob(argv[2]);
91240        rc = sqlite3CodecAttach(db, db->nDb-1, zKey, nKey);
91241        break;
91242
91243      case SQLITE_NULL:
91244        /* No key specified.  Use the key from the main database */
91245        sqlite3CodecGetKey(db, 0, (void**)&zKey, &nKey);
91246        if( nKey>0 || sqlite3BtreeGetOptimalReserve(db->aDb[0].pBt)>0 ){
91247          rc = sqlite3CodecAttach(db, db->nDb-1, zKey, nKey);
91248        }
91249        break;
91250    }
91251  }
91252#endif
91253
91254  /* If the file was opened successfully, read the schema for the new database.
91255  ** If this fails, or if opening the file failed, then close the file and
91256  ** remove the entry from the db->aDb[] array. i.e. put everything back the way
91257  ** we found it.
91258  */
91259  if( rc==SQLITE_OK ){
91260    sqlite3BtreeEnterAll(db);
91261    rc = sqlite3Init(db, &zErrDyn);
91262    sqlite3BtreeLeaveAll(db);
91263  }
91264#ifdef SQLITE_USER_AUTHENTICATION
91265  if( rc==SQLITE_OK ){
91266    u8 newAuth = 0;
91267    rc = sqlite3UserAuthCheckLogin(db, zName, &newAuth);
91268    if( newAuth<db->auth.authLevel ){
91269      rc = SQLITE_AUTH_USER;
91270    }
91271  }
91272#endif
91273  if( rc ){
91274    int iDb = db->nDb - 1;
91275    assert( iDb>=2 );
91276    if( db->aDb[iDb].pBt ){
91277      sqlite3BtreeClose(db->aDb[iDb].pBt);
91278      db->aDb[iDb].pBt = 0;
91279      db->aDb[iDb].pSchema = 0;
91280    }
91281    sqlite3ResetAllSchemasOfConnection(db);
91282    db->nDb = iDb;
91283    if( rc==SQLITE_NOMEM || rc==SQLITE_IOERR_NOMEM ){
91284      db->mallocFailed = 1;
91285      sqlite3DbFree(db, zErrDyn);
91286      zErrDyn = sqlite3MPrintf(db, "out of memory");
91287    }else if( zErrDyn==0 ){
91288      zErrDyn = sqlite3MPrintf(db, "unable to open database: %s", zFile);
91289    }
91290    goto attach_error;
91291  }
91292
91293  return;
91294
91295attach_error:
91296  /* Return an error if we get here */
91297  if( zErrDyn ){
91298    sqlite3_result_error(context, zErrDyn, -1);
91299    sqlite3DbFree(db, zErrDyn);
91300  }
91301  if( rc ) sqlite3_result_error_code(context, rc);
91302}
91303
91304/*
91305** An SQL user-function registered to do the work of an DETACH statement. The
91306** three arguments to the function come directly from a detach statement:
91307**
91308**     DETACH DATABASE x
91309**
91310**     SELECT sqlite_detach(x)
91311*/
91312static void detachFunc(
91313  sqlite3_context *context,
91314  int NotUsed,
91315  sqlite3_value **argv
91316){
91317  const char *zName = (const char *)sqlite3_value_text(argv[0]);
91318  sqlite3 *db = sqlite3_context_db_handle(context);
91319  int i;
91320  Db *pDb = 0;
91321  char zErr[128];
91322
91323  UNUSED_PARAMETER(NotUsed);
91324
91325  if( zName==0 ) zName = "";
91326  for(i=0; i<db->nDb; i++){
91327    pDb = &db->aDb[i];
91328    if( pDb->pBt==0 ) continue;
91329    if( sqlite3StrICmp(pDb->zName, zName)==0 ) break;
91330  }
91331
91332  if( i>=db->nDb ){
91333    sqlite3_snprintf(sizeof(zErr),zErr, "no such database: %s", zName);
91334    goto detach_error;
91335  }
91336  if( i<2 ){
91337    sqlite3_snprintf(sizeof(zErr),zErr, "cannot detach database %s", zName);
91338    goto detach_error;
91339  }
91340  if( !db->autoCommit ){
91341    sqlite3_snprintf(sizeof(zErr), zErr,
91342                     "cannot DETACH database within transaction");
91343    goto detach_error;
91344  }
91345  if( sqlite3BtreeIsInReadTrans(pDb->pBt) || sqlite3BtreeIsInBackup(pDb->pBt) ){
91346    sqlite3_snprintf(sizeof(zErr),zErr, "database %s is locked", zName);
91347    goto detach_error;
91348  }
91349
91350  sqlite3BtreeClose(pDb->pBt);
91351  pDb->pBt = 0;
91352  pDb->pSchema = 0;
91353  sqlite3CollapseDatabaseArray(db);
91354  return;
91355
91356detach_error:
91357  sqlite3_result_error(context, zErr, -1);
91358}
91359
91360/*
91361** This procedure generates VDBE code for a single invocation of either the
91362** sqlite_detach() or sqlite_attach() SQL user functions.
91363*/
91364static void codeAttach(
91365  Parse *pParse,       /* The parser context */
91366  int type,            /* Either SQLITE_ATTACH or SQLITE_DETACH */
91367  FuncDef const *pFunc,/* FuncDef wrapper for detachFunc() or attachFunc() */
91368  Expr *pAuthArg,      /* Expression to pass to authorization callback */
91369  Expr *pFilename,     /* Name of database file */
91370  Expr *pDbname,       /* Name of the database to use internally */
91371  Expr *pKey           /* Database key for encryption extension */
91372){
91373  int rc;
91374  NameContext sName;
91375  Vdbe *v;
91376  sqlite3* db = pParse->db;
91377  int regArgs;
91378
91379  memset(&sName, 0, sizeof(NameContext));
91380  sName.pParse = pParse;
91381
91382  if(
91383      SQLITE_OK!=(rc = resolveAttachExpr(&sName, pFilename)) ||
91384      SQLITE_OK!=(rc = resolveAttachExpr(&sName, pDbname)) ||
91385      SQLITE_OK!=(rc = resolveAttachExpr(&sName, pKey))
91386  ){
91387    goto attach_end;
91388  }
91389
91390#ifndef SQLITE_OMIT_AUTHORIZATION
91391  if( pAuthArg ){
91392    char *zAuthArg;
91393    if( pAuthArg->op==TK_STRING ){
91394      zAuthArg = pAuthArg->u.zToken;
91395    }else{
91396      zAuthArg = 0;
91397    }
91398    rc = sqlite3AuthCheck(pParse, type, zAuthArg, 0, 0);
91399    if(rc!=SQLITE_OK ){
91400      goto attach_end;
91401    }
91402  }
91403#endif /* SQLITE_OMIT_AUTHORIZATION */
91404
91405
91406  v = sqlite3GetVdbe(pParse);
91407  regArgs = sqlite3GetTempRange(pParse, 4);
91408  sqlite3ExprCode(pParse, pFilename, regArgs);
91409  sqlite3ExprCode(pParse, pDbname, regArgs+1);
91410  sqlite3ExprCode(pParse, pKey, regArgs+2);
91411
91412  assert( v || db->mallocFailed );
91413  if( v ){
91414    sqlite3VdbeAddOp3(v, OP_Function0, 0, regArgs+3-pFunc->nArg, regArgs+3);
91415    assert( pFunc->nArg==-1 || (pFunc->nArg&0xff)==pFunc->nArg );
91416    sqlite3VdbeChangeP5(v, (u8)(pFunc->nArg));
91417    sqlite3VdbeChangeP4(v, -1, (char *)pFunc, P4_FUNCDEF);
91418
91419    /* Code an OP_Expire. For an ATTACH statement, set P1 to true (expire this
91420    ** statement only). For DETACH, set it to false (expire all existing
91421    ** statements).
91422    */
91423    sqlite3VdbeAddOp1(v, OP_Expire, (type==SQLITE_ATTACH));
91424  }
91425
91426attach_end:
91427  sqlite3ExprDelete(db, pFilename);
91428  sqlite3ExprDelete(db, pDbname);
91429  sqlite3ExprDelete(db, pKey);
91430}
91431
91432/*
91433** Called by the parser to compile a DETACH statement.
91434**
91435**     DETACH pDbname
91436*/
91437SQLITE_PRIVATE void sqlite3Detach(Parse *pParse, Expr *pDbname){
91438  static const FuncDef detach_func = {
91439    1,                /* nArg */
91440    SQLITE_UTF8,      /* funcFlags */
91441    0,                /* pUserData */
91442    0,                /* pNext */
91443    detachFunc,       /* xFunc */
91444    0,                /* xStep */
91445    0,                /* xFinalize */
91446    "sqlite_detach",  /* zName */
91447    0,                /* pHash */
91448    0                 /* pDestructor */
91449  };
91450  codeAttach(pParse, SQLITE_DETACH, &detach_func, pDbname, 0, 0, pDbname);
91451}
91452
91453/*
91454** Called by the parser to compile an ATTACH statement.
91455**
91456**     ATTACH p AS pDbname KEY pKey
91457*/
91458SQLITE_PRIVATE void sqlite3Attach(Parse *pParse, Expr *p, Expr *pDbname, Expr *pKey){
91459  static const FuncDef attach_func = {
91460    3,                /* nArg */
91461    SQLITE_UTF8,      /* funcFlags */
91462    0,                /* pUserData */
91463    0,                /* pNext */
91464    attachFunc,       /* xFunc */
91465    0,                /* xStep */
91466    0,                /* xFinalize */
91467    "sqlite_attach",  /* zName */
91468    0,                /* pHash */
91469    0                 /* pDestructor */
91470  };
91471  codeAttach(pParse, SQLITE_ATTACH, &attach_func, p, p, pDbname, pKey);
91472}
91473#endif /* SQLITE_OMIT_ATTACH */
91474
91475/*
91476** Initialize a DbFixer structure.  This routine must be called prior
91477** to passing the structure to one of the sqliteFixAAAA() routines below.
91478*/
91479SQLITE_PRIVATE void sqlite3FixInit(
91480  DbFixer *pFix,      /* The fixer to be initialized */
91481  Parse *pParse,      /* Error messages will be written here */
91482  int iDb,            /* This is the database that must be used */
91483  const char *zType,  /* "view", "trigger", or "index" */
91484  const Token *pName  /* Name of the view, trigger, or index */
91485){
91486  sqlite3 *db;
91487
91488  db = pParse->db;
91489  assert( db->nDb>iDb );
91490  pFix->pParse = pParse;
91491  pFix->zDb = db->aDb[iDb].zName;
91492  pFix->pSchema = db->aDb[iDb].pSchema;
91493  pFix->zType = zType;
91494  pFix->pName = pName;
91495  pFix->bVarOnly = (iDb==1);
91496}
91497
91498/*
91499** The following set of routines walk through the parse tree and assign
91500** a specific database to all table references where the database name
91501** was left unspecified in the original SQL statement.  The pFix structure
91502** must have been initialized by a prior call to sqlite3FixInit().
91503**
91504** These routines are used to make sure that an index, trigger, or
91505** view in one database does not refer to objects in a different database.
91506** (Exception: indices, triggers, and views in the TEMP database are
91507** allowed to refer to anything.)  If a reference is explicitly made
91508** to an object in a different database, an error message is added to
91509** pParse->zErrMsg and these routines return non-zero.  If everything
91510** checks out, these routines return 0.
91511*/
91512SQLITE_PRIVATE int sqlite3FixSrcList(
91513  DbFixer *pFix,       /* Context of the fixation */
91514  SrcList *pList       /* The Source list to check and modify */
91515){
91516  int i;
91517  const char *zDb;
91518  struct SrcList_item *pItem;
91519
91520  if( NEVER(pList==0) ) return 0;
91521  zDb = pFix->zDb;
91522  for(i=0, pItem=pList->a; i<pList->nSrc; i++, pItem++){
91523    if( pFix->bVarOnly==0 ){
91524      if( pItem->zDatabase && sqlite3StrICmp(pItem->zDatabase, zDb) ){
91525        sqlite3ErrorMsg(pFix->pParse,
91526            "%s %T cannot reference objects in database %s",
91527            pFix->zType, pFix->pName, pItem->zDatabase);
91528        return 1;
91529      }
91530      sqlite3DbFree(pFix->pParse->db, pItem->zDatabase);
91531      pItem->zDatabase = 0;
91532      pItem->pSchema = pFix->pSchema;
91533    }
91534#if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_TRIGGER)
91535    if( sqlite3FixSelect(pFix, pItem->pSelect) ) return 1;
91536    if( sqlite3FixExpr(pFix, pItem->pOn) ) return 1;
91537#endif
91538  }
91539  return 0;
91540}
91541#if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_TRIGGER)
91542SQLITE_PRIVATE int sqlite3FixSelect(
91543  DbFixer *pFix,       /* Context of the fixation */
91544  Select *pSelect      /* The SELECT statement to be fixed to one database */
91545){
91546  while( pSelect ){
91547    if( sqlite3FixExprList(pFix, pSelect->pEList) ){
91548      return 1;
91549    }
91550    if( sqlite3FixSrcList(pFix, pSelect->pSrc) ){
91551      return 1;
91552    }
91553    if( sqlite3FixExpr(pFix, pSelect->pWhere) ){
91554      return 1;
91555    }
91556    if( sqlite3FixExprList(pFix, pSelect->pGroupBy) ){
91557      return 1;
91558    }
91559    if( sqlite3FixExpr(pFix, pSelect->pHaving) ){
91560      return 1;
91561    }
91562    if( sqlite3FixExprList(pFix, pSelect->pOrderBy) ){
91563      return 1;
91564    }
91565    if( sqlite3FixExpr(pFix, pSelect->pLimit) ){
91566      return 1;
91567    }
91568    if( sqlite3FixExpr(pFix, pSelect->pOffset) ){
91569      return 1;
91570    }
91571    pSelect = pSelect->pPrior;
91572  }
91573  return 0;
91574}
91575SQLITE_PRIVATE int sqlite3FixExpr(
91576  DbFixer *pFix,     /* Context of the fixation */
91577  Expr *pExpr        /* The expression to be fixed to one database */
91578){
91579  while( pExpr ){
91580    if( pExpr->op==TK_VARIABLE ){
91581      if( pFix->pParse->db->init.busy ){
91582        pExpr->op = TK_NULL;
91583      }else{
91584        sqlite3ErrorMsg(pFix->pParse, "%s cannot use variables", pFix->zType);
91585        return 1;
91586      }
91587    }
91588    if( ExprHasProperty(pExpr, EP_TokenOnly) ) break;
91589    if( ExprHasProperty(pExpr, EP_xIsSelect) ){
91590      if( sqlite3FixSelect(pFix, pExpr->x.pSelect) ) return 1;
91591    }else{
91592      if( sqlite3FixExprList(pFix, pExpr->x.pList) ) return 1;
91593    }
91594    if( sqlite3FixExpr(pFix, pExpr->pRight) ){
91595      return 1;
91596    }
91597    pExpr = pExpr->pLeft;
91598  }
91599  return 0;
91600}
91601SQLITE_PRIVATE int sqlite3FixExprList(
91602  DbFixer *pFix,     /* Context of the fixation */
91603  ExprList *pList    /* The expression to be fixed to one database */
91604){
91605  int i;
91606  struct ExprList_item *pItem;
91607  if( pList==0 ) return 0;
91608  for(i=0, pItem=pList->a; i<pList->nExpr; i++, pItem++){
91609    if( sqlite3FixExpr(pFix, pItem->pExpr) ){
91610      return 1;
91611    }
91612  }
91613  return 0;
91614}
91615#endif
91616
91617#ifndef SQLITE_OMIT_TRIGGER
91618SQLITE_PRIVATE int sqlite3FixTriggerStep(
91619  DbFixer *pFix,     /* Context of the fixation */
91620  TriggerStep *pStep /* The trigger step be fixed to one database */
91621){
91622  while( pStep ){
91623    if( sqlite3FixSelect(pFix, pStep->pSelect) ){
91624      return 1;
91625    }
91626    if( sqlite3FixExpr(pFix, pStep->pWhere) ){
91627      return 1;
91628    }
91629    if( sqlite3FixExprList(pFix, pStep->pExprList) ){
91630      return 1;
91631    }
91632    pStep = pStep->pNext;
91633  }
91634  return 0;
91635}
91636#endif
91637
91638/************** End of attach.c **********************************************/
91639/************** Begin file auth.c ********************************************/
91640/*
91641** 2003 January 11
91642**
91643** The author disclaims copyright to this source code.  In place of
91644** a legal notice, here is a blessing:
91645**
91646**    May you do good and not evil.
91647**    May you find forgiveness for yourself and forgive others.
91648**    May you share freely, never taking more than you give.
91649**
91650*************************************************************************
91651** This file contains code used to implement the sqlite3_set_authorizer()
91652** API.  This facility is an optional feature of the library.  Embedded
91653** systems that do not need this facility may omit it by recompiling
91654** the library with -DSQLITE_OMIT_AUTHORIZATION=1
91655*/
91656/* #include "sqliteInt.h" */
91657
91658/*
91659** All of the code in this file may be omitted by defining a single
91660** macro.
91661*/
91662#ifndef SQLITE_OMIT_AUTHORIZATION
91663
91664/*
91665** Set or clear the access authorization function.
91666**
91667** The access authorization function is be called during the compilation
91668** phase to verify that the user has read and/or write access permission on
91669** various fields of the database.  The first argument to the auth function
91670** is a copy of the 3rd argument to this routine.  The second argument
91671** to the auth function is one of these constants:
91672**
91673**       SQLITE_CREATE_INDEX
91674**       SQLITE_CREATE_TABLE
91675**       SQLITE_CREATE_TEMP_INDEX
91676**       SQLITE_CREATE_TEMP_TABLE
91677**       SQLITE_CREATE_TEMP_TRIGGER
91678**       SQLITE_CREATE_TEMP_VIEW
91679**       SQLITE_CREATE_TRIGGER
91680**       SQLITE_CREATE_VIEW
91681**       SQLITE_DELETE
91682**       SQLITE_DROP_INDEX
91683**       SQLITE_DROP_TABLE
91684**       SQLITE_DROP_TEMP_INDEX
91685**       SQLITE_DROP_TEMP_TABLE
91686**       SQLITE_DROP_TEMP_TRIGGER
91687**       SQLITE_DROP_TEMP_VIEW
91688**       SQLITE_DROP_TRIGGER
91689**       SQLITE_DROP_VIEW
91690**       SQLITE_INSERT
91691**       SQLITE_PRAGMA
91692**       SQLITE_READ
91693**       SQLITE_SELECT
91694**       SQLITE_TRANSACTION
91695**       SQLITE_UPDATE
91696**
91697** The third and fourth arguments to the auth function are the name of
91698** the table and the column that are being accessed.  The auth function
91699** should return either SQLITE_OK, SQLITE_DENY, or SQLITE_IGNORE.  If
91700** SQLITE_OK is returned, it means that access is allowed.  SQLITE_DENY
91701** means that the SQL statement will never-run - the sqlite3_exec() call
91702** will return with an error.  SQLITE_IGNORE means that the SQL statement
91703** should run but attempts to read the specified column will return NULL
91704** and attempts to write the column will be ignored.
91705**
91706** Setting the auth function to NULL disables this hook.  The default
91707** setting of the auth function is NULL.
91708*/
91709SQLITE_API int SQLITE_STDCALL sqlite3_set_authorizer(
91710  sqlite3 *db,
91711  int (*xAuth)(void*,int,const char*,const char*,const char*,const char*),
91712  void *pArg
91713){
91714#ifdef SQLITE_ENABLE_API_ARMOR
91715  if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT;
91716#endif
91717  sqlite3_mutex_enter(db->mutex);
91718  db->xAuth = (sqlite3_xauth)xAuth;
91719  db->pAuthArg = pArg;
91720  sqlite3ExpirePreparedStatements(db);
91721  sqlite3_mutex_leave(db->mutex);
91722  return SQLITE_OK;
91723}
91724
91725/*
91726** Write an error message into pParse->zErrMsg that explains that the
91727** user-supplied authorization function returned an illegal value.
91728*/
91729static void sqliteAuthBadReturnCode(Parse *pParse){
91730  sqlite3ErrorMsg(pParse, "authorizer malfunction");
91731  pParse->rc = SQLITE_ERROR;
91732}
91733
91734/*
91735** Invoke the authorization callback for permission to read column zCol from
91736** table zTab in database zDb. This function assumes that an authorization
91737** callback has been registered (i.e. that sqlite3.xAuth is not NULL).
91738**
91739** If SQLITE_IGNORE is returned and pExpr is not NULL, then pExpr is changed
91740** to an SQL NULL expression. Otherwise, if pExpr is NULL, then SQLITE_IGNORE
91741** is treated as SQLITE_DENY. In this case an error is left in pParse.
91742*/
91743SQLITE_PRIVATE int sqlite3AuthReadCol(
91744  Parse *pParse,                  /* The parser context */
91745  const char *zTab,               /* Table name */
91746  const char *zCol,               /* Column name */
91747  int iDb                         /* Index of containing database. */
91748){
91749  sqlite3 *db = pParse->db;       /* Database handle */
91750  char *zDb = db->aDb[iDb].zName; /* Name of attached database */
91751  int rc;                         /* Auth callback return code */
91752
91753  rc = db->xAuth(db->pAuthArg, SQLITE_READ, zTab,zCol,zDb,pParse->zAuthContext
91754#ifdef SQLITE_USER_AUTHENTICATION
91755                 ,db->auth.zAuthUser
91756#endif
91757                );
91758  if( rc==SQLITE_DENY ){
91759    if( db->nDb>2 || iDb!=0 ){
91760      sqlite3ErrorMsg(pParse, "access to %s.%s.%s is prohibited",zDb,zTab,zCol);
91761    }else{
91762      sqlite3ErrorMsg(pParse, "access to %s.%s is prohibited", zTab, zCol);
91763    }
91764    pParse->rc = SQLITE_AUTH;
91765  }else if( rc!=SQLITE_IGNORE && rc!=SQLITE_OK ){
91766    sqliteAuthBadReturnCode(pParse);
91767  }
91768  return rc;
91769}
91770
91771/*
91772** The pExpr should be a TK_COLUMN expression.  The table referred to
91773** is in pTabList or else it is the NEW or OLD table of a trigger.
91774** Check to see if it is OK to read this particular column.
91775**
91776** If the auth function returns SQLITE_IGNORE, change the TK_COLUMN
91777** instruction into a TK_NULL.  If the auth function returns SQLITE_DENY,
91778** then generate an error.
91779*/
91780SQLITE_PRIVATE void sqlite3AuthRead(
91781  Parse *pParse,        /* The parser context */
91782  Expr *pExpr,          /* The expression to check authorization on */
91783  Schema *pSchema,      /* The schema of the expression */
91784  SrcList *pTabList     /* All table that pExpr might refer to */
91785){
91786  sqlite3 *db = pParse->db;
91787  Table *pTab = 0;      /* The table being read */
91788  const char *zCol;     /* Name of the column of the table */
91789  int iSrc;             /* Index in pTabList->a[] of table being read */
91790  int iDb;              /* The index of the database the expression refers to */
91791  int iCol;             /* Index of column in table */
91792
91793  if( db->xAuth==0 ) return;
91794  iDb = sqlite3SchemaToIndex(pParse->db, pSchema);
91795  if( iDb<0 ){
91796    /* An attempt to read a column out of a subquery or other
91797    ** temporary table. */
91798    return;
91799  }
91800
91801  assert( pExpr->op==TK_COLUMN || pExpr->op==TK_TRIGGER );
91802  if( pExpr->op==TK_TRIGGER ){
91803    pTab = pParse->pTriggerTab;
91804  }else{
91805    assert( pTabList );
91806    for(iSrc=0; ALWAYS(iSrc<pTabList->nSrc); iSrc++){
91807      if( pExpr->iTable==pTabList->a[iSrc].iCursor ){
91808        pTab = pTabList->a[iSrc].pTab;
91809        break;
91810      }
91811    }
91812  }
91813  iCol = pExpr->iColumn;
91814  if( NEVER(pTab==0) ) return;
91815
91816  if( iCol>=0 ){
91817    assert( iCol<pTab->nCol );
91818    zCol = pTab->aCol[iCol].zName;
91819  }else if( pTab->iPKey>=0 ){
91820    assert( pTab->iPKey<pTab->nCol );
91821    zCol = pTab->aCol[pTab->iPKey].zName;
91822  }else{
91823    zCol = "ROWID";
91824  }
91825  assert( iDb>=0 && iDb<db->nDb );
91826  if( SQLITE_IGNORE==sqlite3AuthReadCol(pParse, pTab->zName, zCol, iDb) ){
91827    pExpr->op = TK_NULL;
91828  }
91829}
91830
91831/*
91832** Do an authorization check using the code and arguments given.  Return
91833** either SQLITE_OK (zero) or SQLITE_IGNORE or SQLITE_DENY.  If SQLITE_DENY
91834** is returned, then the error count and error message in pParse are
91835** modified appropriately.
91836*/
91837SQLITE_PRIVATE int sqlite3AuthCheck(
91838  Parse *pParse,
91839  int code,
91840  const char *zArg1,
91841  const char *zArg2,
91842  const char *zArg3
91843){
91844  sqlite3 *db = pParse->db;
91845  int rc;
91846
91847  /* Don't do any authorization checks if the database is initialising
91848  ** or if the parser is being invoked from within sqlite3_declare_vtab.
91849  */
91850  if( db->init.busy || IN_DECLARE_VTAB ){
91851    return SQLITE_OK;
91852  }
91853
91854  if( db->xAuth==0 ){
91855    return SQLITE_OK;
91856  }
91857  rc = db->xAuth(db->pAuthArg, code, zArg1, zArg2, zArg3, pParse->zAuthContext
91858#ifdef SQLITE_USER_AUTHENTICATION
91859                 ,db->auth.zAuthUser
91860#endif
91861                );
91862  if( rc==SQLITE_DENY ){
91863    sqlite3ErrorMsg(pParse, "not authorized");
91864    pParse->rc = SQLITE_AUTH;
91865  }else if( rc!=SQLITE_OK && rc!=SQLITE_IGNORE ){
91866    rc = SQLITE_DENY;
91867    sqliteAuthBadReturnCode(pParse);
91868  }
91869  return rc;
91870}
91871
91872/*
91873** Push an authorization context.  After this routine is called, the
91874** zArg3 argument to authorization callbacks will be zContext until
91875** popped.  Or if pParse==0, this routine is a no-op.
91876*/
91877SQLITE_PRIVATE void sqlite3AuthContextPush(
91878  Parse *pParse,
91879  AuthContext *pContext,
91880  const char *zContext
91881){
91882  assert( pParse );
91883  pContext->pParse = pParse;
91884  pContext->zAuthContext = pParse->zAuthContext;
91885  pParse->zAuthContext = zContext;
91886}
91887
91888/*
91889** Pop an authorization context that was previously pushed
91890** by sqlite3AuthContextPush
91891*/
91892SQLITE_PRIVATE void sqlite3AuthContextPop(AuthContext *pContext){
91893  if( pContext->pParse ){
91894    pContext->pParse->zAuthContext = pContext->zAuthContext;
91895    pContext->pParse = 0;
91896  }
91897}
91898
91899#endif /* SQLITE_OMIT_AUTHORIZATION */
91900
91901/************** End of auth.c ************************************************/
91902/************** Begin file build.c *******************************************/
91903/*
91904** 2001 September 15
91905**
91906** The author disclaims copyright to this source code.  In place of
91907** a legal notice, here is a blessing:
91908**
91909**    May you do good and not evil.
91910**    May you find forgiveness for yourself and forgive others.
91911**    May you share freely, never taking more than you give.
91912**
91913*************************************************************************
91914** This file contains C code routines that are called by the SQLite parser
91915** when syntax rules are reduced.  The routines in this file handle the
91916** following kinds of SQL syntax:
91917**
91918**     CREATE TABLE
91919**     DROP TABLE
91920**     CREATE INDEX
91921**     DROP INDEX
91922**     creating ID lists
91923**     BEGIN TRANSACTION
91924**     COMMIT
91925**     ROLLBACK
91926*/
91927/* #include "sqliteInt.h" */
91928
91929/*
91930** This routine is called when a new SQL statement is beginning to
91931** be parsed.  Initialize the pParse structure as needed.
91932*/
91933SQLITE_PRIVATE void sqlite3BeginParse(Parse *pParse, int explainFlag){
91934  pParse->explain = (u8)explainFlag;
91935  pParse->nVar = 0;
91936}
91937
91938#ifndef SQLITE_OMIT_SHARED_CACHE
91939/*
91940** The TableLock structure is only used by the sqlite3TableLock() and
91941** codeTableLocks() functions.
91942*/
91943struct TableLock {
91944  int iDb;             /* The database containing the table to be locked */
91945  int iTab;            /* The root page of the table to be locked */
91946  u8 isWriteLock;      /* True for write lock.  False for a read lock */
91947  const char *zName;   /* Name of the table */
91948};
91949
91950/*
91951** Record the fact that we want to lock a table at run-time.
91952**
91953** The table to be locked has root page iTab and is found in database iDb.
91954** A read or a write lock can be taken depending on isWritelock.
91955**
91956** This routine just records the fact that the lock is desired.  The
91957** code to make the lock occur is generated by a later call to
91958** codeTableLocks() which occurs during sqlite3FinishCoding().
91959*/
91960SQLITE_PRIVATE void sqlite3TableLock(
91961  Parse *pParse,     /* Parsing context */
91962  int iDb,           /* Index of the database containing the table to lock */
91963  int iTab,          /* Root page number of the table to be locked */
91964  u8 isWriteLock,    /* True for a write lock */
91965  const char *zName  /* Name of the table to be locked */
91966){
91967  Parse *pToplevel = sqlite3ParseToplevel(pParse);
91968  int i;
91969  int nBytes;
91970  TableLock *p;
91971  assert( iDb>=0 );
91972
91973  for(i=0; i<pToplevel->nTableLock; i++){
91974    p = &pToplevel->aTableLock[i];
91975    if( p->iDb==iDb && p->iTab==iTab ){
91976      p->isWriteLock = (p->isWriteLock || isWriteLock);
91977      return;
91978    }
91979  }
91980
91981  nBytes = sizeof(TableLock) * (pToplevel->nTableLock+1);
91982  pToplevel->aTableLock =
91983      sqlite3DbReallocOrFree(pToplevel->db, pToplevel->aTableLock, nBytes);
91984  if( pToplevel->aTableLock ){
91985    p = &pToplevel->aTableLock[pToplevel->nTableLock++];
91986    p->iDb = iDb;
91987    p->iTab = iTab;
91988    p->isWriteLock = isWriteLock;
91989    p->zName = zName;
91990  }else{
91991    pToplevel->nTableLock = 0;
91992    pToplevel->db->mallocFailed = 1;
91993  }
91994}
91995
91996/*
91997** Code an OP_TableLock instruction for each table locked by the
91998** statement (configured by calls to sqlite3TableLock()).
91999*/
92000static void codeTableLocks(Parse *pParse){
92001  int i;
92002  Vdbe *pVdbe;
92003
92004  pVdbe = sqlite3GetVdbe(pParse);
92005  assert( pVdbe!=0 ); /* sqlite3GetVdbe cannot fail: VDBE already allocated */
92006
92007  for(i=0; i<pParse->nTableLock; i++){
92008    TableLock *p = &pParse->aTableLock[i];
92009    int p1 = p->iDb;
92010    sqlite3VdbeAddOp4(pVdbe, OP_TableLock, p1, p->iTab, p->isWriteLock,
92011                      p->zName, P4_STATIC);
92012  }
92013}
92014#else
92015  #define codeTableLocks(x)
92016#endif
92017
92018/*
92019** Return TRUE if the given yDbMask object is empty - if it contains no
92020** 1 bits.  This routine is used by the DbMaskAllZero() and DbMaskNotZero()
92021** macros when SQLITE_MAX_ATTACHED is greater than 30.
92022*/
92023#if SQLITE_MAX_ATTACHED>30
92024SQLITE_PRIVATE int sqlite3DbMaskAllZero(yDbMask m){
92025  int i;
92026  for(i=0; i<sizeof(yDbMask); i++) if( m[i] ) return 0;
92027  return 1;
92028}
92029#endif
92030
92031/*
92032** This routine is called after a single SQL statement has been
92033** parsed and a VDBE program to execute that statement has been
92034** prepared.  This routine puts the finishing touches on the
92035** VDBE program and resets the pParse structure for the next
92036** parse.
92037**
92038** Note that if an error occurred, it might be the case that
92039** no VDBE code was generated.
92040*/
92041SQLITE_PRIVATE void sqlite3FinishCoding(Parse *pParse){
92042  sqlite3 *db;
92043  Vdbe *v;
92044
92045  assert( pParse->pToplevel==0 );
92046  db = pParse->db;
92047  if( pParse->nested ) return;
92048  if( db->mallocFailed || pParse->nErr ){
92049    if( pParse->rc==SQLITE_OK ) pParse->rc = SQLITE_ERROR;
92050    return;
92051  }
92052
92053  /* Begin by generating some termination code at the end of the
92054  ** vdbe program
92055  */
92056  v = sqlite3GetVdbe(pParse);
92057  assert( !pParse->isMultiWrite
92058       || sqlite3VdbeAssertMayAbort(v, pParse->mayAbort));
92059  if( v ){
92060    while( sqlite3VdbeDeletePriorOpcode(v, OP_Close) ){}
92061    sqlite3VdbeAddOp0(v, OP_Halt);
92062
92063#if SQLITE_USER_AUTHENTICATION
92064    if( pParse->nTableLock>0 && db->init.busy==0 ){
92065      sqlite3UserAuthInit(db);
92066      if( db->auth.authLevel<UAUTH_User ){
92067        pParse->rc = SQLITE_AUTH_USER;
92068        sqlite3ErrorMsg(pParse, "user not authenticated");
92069        return;
92070      }
92071    }
92072#endif
92073
92074    /* The cookie mask contains one bit for each database file open.
92075    ** (Bit 0 is for main, bit 1 is for temp, and so forth.)  Bits are
92076    ** set for each database that is used.  Generate code to start a
92077    ** transaction on each used database and to verify the schema cookie
92078    ** on each used database.
92079    */
92080    if( db->mallocFailed==0
92081     && (DbMaskNonZero(pParse->cookieMask) || pParse->pConstExpr)
92082    ){
92083      int iDb, i;
92084      assert( sqlite3VdbeGetOp(v, 0)->opcode==OP_Init );
92085      sqlite3VdbeJumpHere(v, 0);
92086      for(iDb=0; iDb<db->nDb; iDb++){
92087        if( DbMaskTest(pParse->cookieMask, iDb)==0 ) continue;
92088        sqlite3VdbeUsesBtree(v, iDb);
92089        sqlite3VdbeAddOp4Int(v,
92090          OP_Transaction,                    /* Opcode */
92091          iDb,                               /* P1 */
92092          DbMaskTest(pParse->writeMask,iDb), /* P2 */
92093          pParse->cookieValue[iDb],          /* P3 */
92094          db->aDb[iDb].pSchema->iGeneration  /* P4 */
92095        );
92096        if( db->init.busy==0 ) sqlite3VdbeChangeP5(v, 1);
92097      }
92098#ifndef SQLITE_OMIT_VIRTUALTABLE
92099      for(i=0; i<pParse->nVtabLock; i++){
92100        char *vtab = (char *)sqlite3GetVTable(db, pParse->apVtabLock[i]);
92101        sqlite3VdbeAddOp4(v, OP_VBegin, 0, 0, 0, vtab, P4_VTAB);
92102      }
92103      pParse->nVtabLock = 0;
92104#endif
92105
92106      /* Once all the cookies have been verified and transactions opened,
92107      ** obtain the required table-locks. This is a no-op unless the
92108      ** shared-cache feature is enabled.
92109      */
92110      codeTableLocks(pParse);
92111
92112      /* Initialize any AUTOINCREMENT data structures required.
92113      */
92114      sqlite3AutoincrementBegin(pParse);
92115
92116      /* Code constant expressions that where factored out of inner loops */
92117      if( pParse->pConstExpr ){
92118        ExprList *pEL = pParse->pConstExpr;
92119        pParse->okConstFactor = 0;
92120        for(i=0; i<pEL->nExpr; i++){
92121          sqlite3ExprCode(pParse, pEL->a[i].pExpr, pEL->a[i].u.iConstExprReg);
92122        }
92123      }
92124
92125      /* Finally, jump back to the beginning of the executable code. */
92126      sqlite3VdbeAddOp2(v, OP_Goto, 0, 1);
92127    }
92128  }
92129
92130
92131  /* Get the VDBE program ready for execution
92132  */
92133  if( v && pParse->nErr==0 && !db->mallocFailed ){
92134    assert( pParse->iCacheLevel==0 );  /* Disables and re-enables match */
92135    /* A minimum of one cursor is required if autoincrement is used
92136    *  See ticket [a696379c1f08866] */
92137    if( pParse->pAinc!=0 && pParse->nTab==0 ) pParse->nTab = 1;
92138    sqlite3VdbeMakeReady(v, pParse);
92139    pParse->rc = SQLITE_DONE;
92140    pParse->colNamesSet = 0;
92141  }else{
92142    pParse->rc = SQLITE_ERROR;
92143  }
92144  pParse->nTab = 0;
92145  pParse->nMem = 0;
92146  pParse->nSet = 0;
92147  pParse->nVar = 0;
92148  DbMaskZero(pParse->cookieMask);
92149}
92150
92151/*
92152** Run the parser and code generator recursively in order to generate
92153** code for the SQL statement given onto the end of the pParse context
92154** currently under construction.  When the parser is run recursively
92155** this way, the final OP_Halt is not appended and other initialization
92156** and finalization steps are omitted because those are handling by the
92157** outermost parser.
92158**
92159** Not everything is nestable.  This facility is designed to permit
92160** INSERT, UPDATE, and DELETE operations against SQLITE_MASTER.  Use
92161** care if you decide to try to use this routine for some other purposes.
92162*/
92163SQLITE_PRIVATE void sqlite3NestedParse(Parse *pParse, const char *zFormat, ...){
92164  va_list ap;
92165  char *zSql;
92166  char *zErrMsg = 0;
92167  sqlite3 *db = pParse->db;
92168# define SAVE_SZ  (sizeof(Parse) - offsetof(Parse,nVar))
92169  char saveBuf[SAVE_SZ];
92170
92171  if( pParse->nErr ) return;
92172  assert( pParse->nested<10 );  /* Nesting should only be of limited depth */
92173  va_start(ap, zFormat);
92174  zSql = sqlite3VMPrintf(db, zFormat, ap);
92175  va_end(ap);
92176  if( zSql==0 ){
92177    return;   /* A malloc must have failed */
92178  }
92179  pParse->nested++;
92180  memcpy(saveBuf, &pParse->nVar, SAVE_SZ);
92181  memset(&pParse->nVar, 0, SAVE_SZ);
92182  sqlite3RunParser(pParse, zSql, &zErrMsg);
92183  sqlite3DbFree(db, zErrMsg);
92184  sqlite3DbFree(db, zSql);
92185  memcpy(&pParse->nVar, saveBuf, SAVE_SZ);
92186  pParse->nested--;
92187}
92188
92189#if SQLITE_USER_AUTHENTICATION
92190/*
92191** Return TRUE if zTable is the name of the system table that stores the
92192** list of users and their access credentials.
92193*/
92194SQLITE_PRIVATE int sqlite3UserAuthTable(const char *zTable){
92195  return sqlite3_stricmp(zTable, "sqlite_user")==0;
92196}
92197#endif
92198
92199/*
92200** Locate the in-memory structure that describes a particular database
92201** table given the name of that table and (optionally) the name of the
92202** database containing the table.  Return NULL if not found.
92203**
92204** If zDatabase is 0, all databases are searched for the table and the
92205** first matching table is returned.  (No checking for duplicate table
92206** names is done.)  The search order is TEMP first, then MAIN, then any
92207** auxiliary databases added using the ATTACH command.
92208**
92209** See also sqlite3LocateTable().
92210*/
92211SQLITE_PRIVATE Table *sqlite3FindTable(sqlite3 *db, const char *zName, const char *zDatabase){
92212  Table *p = 0;
92213  int i;
92214
92215  /* All mutexes are required for schema access.  Make sure we hold them. */
92216  assert( zDatabase!=0 || sqlite3BtreeHoldsAllMutexes(db) );
92217#if SQLITE_USER_AUTHENTICATION
92218  /* Only the admin user is allowed to know that the sqlite_user table
92219  ** exists */
92220  if( db->auth.authLevel<UAUTH_Admin && sqlite3UserAuthTable(zName)!=0 ){
92221    return 0;
92222  }
92223#endif
92224  for(i=OMIT_TEMPDB; i<db->nDb; i++){
92225    int j = (i<2) ? i^1 : i;   /* Search TEMP before MAIN */
92226    if( zDatabase!=0 && sqlite3StrICmp(zDatabase, db->aDb[j].zName) ) continue;
92227    assert( sqlite3SchemaMutexHeld(db, j, 0) );
92228    p = sqlite3HashFind(&db->aDb[j].pSchema->tblHash, zName);
92229    if( p ) break;
92230  }
92231  return p;
92232}
92233
92234/*
92235** Locate the in-memory structure that describes a particular database
92236** table given the name of that table and (optionally) the name of the
92237** database containing the table.  Return NULL if not found.  Also leave an
92238** error message in pParse->zErrMsg.
92239**
92240** The difference between this routine and sqlite3FindTable() is that this
92241** routine leaves an error message in pParse->zErrMsg where
92242** sqlite3FindTable() does not.
92243*/
92244SQLITE_PRIVATE Table *sqlite3LocateTable(
92245  Parse *pParse,         /* context in which to report errors */
92246  int isView,            /* True if looking for a VIEW rather than a TABLE */
92247  const char *zName,     /* Name of the table we are looking for */
92248  const char *zDbase     /* Name of the database.  Might be NULL */
92249){
92250  Table *p;
92251
92252  /* Read the database schema. If an error occurs, leave an error message
92253  ** and code in pParse and return NULL. */
92254  if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){
92255    return 0;
92256  }
92257
92258  p = sqlite3FindTable(pParse->db, zName, zDbase);
92259  if( p==0 ){
92260    const char *zMsg = isView ? "no such view" : "no such table";
92261    if( zDbase ){
92262      sqlite3ErrorMsg(pParse, "%s: %s.%s", zMsg, zDbase, zName);
92263    }else{
92264      sqlite3ErrorMsg(pParse, "%s: %s", zMsg, zName);
92265    }
92266    pParse->checkSchema = 1;
92267  }
92268#if SQLITE_USER_AUTHENICATION
92269  else if( pParse->db->auth.authLevel<UAUTH_User ){
92270    sqlite3ErrorMsg(pParse, "user not authenticated");
92271    p = 0;
92272  }
92273#endif
92274  return p;
92275}
92276
92277/*
92278** Locate the table identified by *p.
92279**
92280** This is a wrapper around sqlite3LocateTable(). The difference between
92281** sqlite3LocateTable() and this function is that this function restricts
92282** the search to schema (p->pSchema) if it is not NULL. p->pSchema may be
92283** non-NULL if it is part of a view or trigger program definition. See
92284** sqlite3FixSrcList() for details.
92285*/
92286SQLITE_PRIVATE Table *sqlite3LocateTableItem(
92287  Parse *pParse,
92288  int isView,
92289  struct SrcList_item *p
92290){
92291  const char *zDb;
92292  assert( p->pSchema==0 || p->zDatabase==0 );
92293  if( p->pSchema ){
92294    int iDb = sqlite3SchemaToIndex(pParse->db, p->pSchema);
92295    zDb = pParse->db->aDb[iDb].zName;
92296  }else{
92297    zDb = p->zDatabase;
92298  }
92299  return sqlite3LocateTable(pParse, isView, p->zName, zDb);
92300}
92301
92302/*
92303** Locate the in-memory structure that describes
92304** a particular index given the name of that index
92305** and the name of the database that contains the index.
92306** Return NULL if not found.
92307**
92308** If zDatabase is 0, all databases are searched for the
92309** table and the first matching index is returned.  (No checking
92310** for duplicate index names is done.)  The search order is
92311** TEMP first, then MAIN, then any auxiliary databases added
92312** using the ATTACH command.
92313*/
92314SQLITE_PRIVATE Index *sqlite3FindIndex(sqlite3 *db, const char *zName, const char *zDb){
92315  Index *p = 0;
92316  int i;
92317  /* All mutexes are required for schema access.  Make sure we hold them. */
92318  assert( zDb!=0 || sqlite3BtreeHoldsAllMutexes(db) );
92319  for(i=OMIT_TEMPDB; i<db->nDb; i++){
92320    int j = (i<2) ? i^1 : i;  /* Search TEMP before MAIN */
92321    Schema *pSchema = db->aDb[j].pSchema;
92322    assert( pSchema );
92323    if( zDb && sqlite3StrICmp(zDb, db->aDb[j].zName) ) continue;
92324    assert( sqlite3SchemaMutexHeld(db, j, 0) );
92325    p = sqlite3HashFind(&pSchema->idxHash, zName);
92326    if( p ) break;
92327  }
92328  return p;
92329}
92330
92331/*
92332** Reclaim the memory used by an index
92333*/
92334static void freeIndex(sqlite3 *db, Index *p){
92335#ifndef SQLITE_OMIT_ANALYZE
92336  sqlite3DeleteIndexSamples(db, p);
92337#endif
92338  sqlite3ExprDelete(db, p->pPartIdxWhere);
92339  sqlite3DbFree(db, p->zColAff);
92340  if( p->isResized ) sqlite3DbFree(db, p->azColl);
92341#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
92342  sqlite3_free(p->aiRowEst);
92343#endif
92344  sqlite3DbFree(db, p);
92345}
92346
92347/*
92348** For the index called zIdxName which is found in the database iDb,
92349** unlike that index from its Table then remove the index from
92350** the index hash table and free all memory structures associated
92351** with the index.
92352*/
92353SQLITE_PRIVATE void sqlite3UnlinkAndDeleteIndex(sqlite3 *db, int iDb, const char *zIdxName){
92354  Index *pIndex;
92355  Hash *pHash;
92356
92357  assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
92358  pHash = &db->aDb[iDb].pSchema->idxHash;
92359  pIndex = sqlite3HashInsert(pHash, zIdxName, 0);
92360  if( ALWAYS(pIndex) ){
92361    if( pIndex->pTable->pIndex==pIndex ){
92362      pIndex->pTable->pIndex = pIndex->pNext;
92363    }else{
92364      Index *p;
92365      /* Justification of ALWAYS();  The index must be on the list of
92366      ** indices. */
92367      p = pIndex->pTable->pIndex;
92368      while( ALWAYS(p) && p->pNext!=pIndex ){ p = p->pNext; }
92369      if( ALWAYS(p && p->pNext==pIndex) ){
92370        p->pNext = pIndex->pNext;
92371      }
92372    }
92373    freeIndex(db, pIndex);
92374  }
92375  db->flags |= SQLITE_InternChanges;
92376}
92377
92378/*
92379** Look through the list of open database files in db->aDb[] and if
92380** any have been closed, remove them from the list.  Reallocate the
92381** db->aDb[] structure to a smaller size, if possible.
92382**
92383** Entry 0 (the "main" database) and entry 1 (the "temp" database)
92384** are never candidates for being collapsed.
92385*/
92386SQLITE_PRIVATE void sqlite3CollapseDatabaseArray(sqlite3 *db){
92387  int i, j;
92388  for(i=j=2; i<db->nDb; i++){
92389    struct Db *pDb = &db->aDb[i];
92390    if( pDb->pBt==0 ){
92391      sqlite3DbFree(db, pDb->zName);
92392      pDb->zName = 0;
92393      continue;
92394    }
92395    if( j<i ){
92396      db->aDb[j] = db->aDb[i];
92397    }
92398    j++;
92399  }
92400  memset(&db->aDb[j], 0, (db->nDb-j)*sizeof(db->aDb[j]));
92401  db->nDb = j;
92402  if( db->nDb<=2 && db->aDb!=db->aDbStatic ){
92403    memcpy(db->aDbStatic, db->aDb, 2*sizeof(db->aDb[0]));
92404    sqlite3DbFree(db, db->aDb);
92405    db->aDb = db->aDbStatic;
92406  }
92407}
92408
92409/*
92410** Reset the schema for the database at index iDb.  Also reset the
92411** TEMP schema.
92412*/
92413SQLITE_PRIVATE void sqlite3ResetOneSchema(sqlite3 *db, int iDb){
92414  Db *pDb;
92415  assert( iDb<db->nDb );
92416
92417  /* Case 1:  Reset the single schema identified by iDb */
92418  pDb = &db->aDb[iDb];
92419  assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
92420  assert( pDb->pSchema!=0 );
92421  sqlite3SchemaClear(pDb->pSchema);
92422
92423  /* If any database other than TEMP is reset, then also reset TEMP
92424  ** since TEMP might be holding triggers that reference tables in the
92425  ** other database.
92426  */
92427  if( iDb!=1 ){
92428    pDb = &db->aDb[1];
92429    assert( pDb->pSchema!=0 );
92430    sqlite3SchemaClear(pDb->pSchema);
92431  }
92432  return;
92433}
92434
92435/*
92436** Erase all schema information from all attached databases (including
92437** "main" and "temp") for a single database connection.
92438*/
92439SQLITE_PRIVATE void sqlite3ResetAllSchemasOfConnection(sqlite3 *db){
92440  int i;
92441  sqlite3BtreeEnterAll(db);
92442  for(i=0; i<db->nDb; i++){
92443    Db *pDb = &db->aDb[i];
92444    if( pDb->pSchema ){
92445      sqlite3SchemaClear(pDb->pSchema);
92446    }
92447  }
92448  db->flags &= ~SQLITE_InternChanges;
92449  sqlite3VtabUnlockList(db);
92450  sqlite3BtreeLeaveAll(db);
92451  sqlite3CollapseDatabaseArray(db);
92452}
92453
92454/*
92455** This routine is called when a commit occurs.
92456*/
92457SQLITE_PRIVATE void sqlite3CommitInternalChanges(sqlite3 *db){
92458  db->flags &= ~SQLITE_InternChanges;
92459}
92460
92461/*
92462** Delete memory allocated for the column names of a table or view (the
92463** Table.aCol[] array).
92464*/
92465static void sqliteDeleteColumnNames(sqlite3 *db, Table *pTable){
92466  int i;
92467  Column *pCol;
92468  assert( pTable!=0 );
92469  if( (pCol = pTable->aCol)!=0 ){
92470    for(i=0; i<pTable->nCol; i++, pCol++){
92471      sqlite3DbFree(db, pCol->zName);
92472      sqlite3ExprDelete(db, pCol->pDflt);
92473      sqlite3DbFree(db, pCol->zDflt);
92474      sqlite3DbFree(db, pCol->zType);
92475      sqlite3DbFree(db, pCol->zColl);
92476    }
92477    sqlite3DbFree(db, pTable->aCol);
92478  }
92479}
92480
92481/*
92482** Remove the memory data structures associated with the given
92483** Table.  No changes are made to disk by this routine.
92484**
92485** This routine just deletes the data structure.  It does not unlink
92486** the table data structure from the hash table.  But it does destroy
92487** memory structures of the indices and foreign keys associated with
92488** the table.
92489**
92490** The db parameter is optional.  It is needed if the Table object
92491** contains lookaside memory.  (Table objects in the schema do not use
92492** lookaside memory, but some ephemeral Table objects do.)  Or the
92493** db parameter can be used with db->pnBytesFreed to measure the memory
92494** used by the Table object.
92495*/
92496SQLITE_PRIVATE void sqlite3DeleteTable(sqlite3 *db, Table *pTable){
92497  Index *pIndex, *pNext;
92498  TESTONLY( int nLookaside; ) /* Used to verify lookaside not used for schema */
92499
92500  assert( !pTable || pTable->nRef>0 );
92501
92502  /* Do not delete the table until the reference count reaches zero. */
92503  if( !pTable ) return;
92504  if( ((!db || db->pnBytesFreed==0) && (--pTable->nRef)>0) ) return;
92505
92506  /* Record the number of outstanding lookaside allocations in schema Tables
92507  ** prior to doing any free() operations.  Since schema Tables do not use
92508  ** lookaside, this number should not change. */
92509  TESTONLY( nLookaside = (db && (pTable->tabFlags & TF_Ephemeral)==0) ?
92510                         db->lookaside.nOut : 0 );
92511
92512  /* Delete all indices associated with this table. */
92513  for(pIndex = pTable->pIndex; pIndex; pIndex=pNext){
92514    pNext = pIndex->pNext;
92515    assert( pIndex->pSchema==pTable->pSchema );
92516    if( !db || db->pnBytesFreed==0 ){
92517      char *zName = pIndex->zName;
92518      TESTONLY ( Index *pOld = ) sqlite3HashInsert(
92519         &pIndex->pSchema->idxHash, zName, 0
92520      );
92521      assert( db==0 || sqlite3SchemaMutexHeld(db, 0, pIndex->pSchema) );
92522      assert( pOld==pIndex || pOld==0 );
92523    }
92524    freeIndex(db, pIndex);
92525  }
92526
92527  /* Delete any foreign keys attached to this table. */
92528  sqlite3FkDelete(db, pTable);
92529
92530  /* Delete the Table structure itself.
92531  */
92532  sqliteDeleteColumnNames(db, pTable);
92533  sqlite3DbFree(db, pTable->zName);
92534  sqlite3DbFree(db, pTable->zColAff);
92535  sqlite3SelectDelete(db, pTable->pSelect);
92536#ifndef SQLITE_OMIT_CHECK
92537  sqlite3ExprListDelete(db, pTable->pCheck);
92538#endif
92539#ifndef SQLITE_OMIT_VIRTUALTABLE
92540  sqlite3VtabClear(db, pTable);
92541#endif
92542  sqlite3DbFree(db, pTable);
92543
92544  /* Verify that no lookaside memory was used by schema tables */
92545  assert( nLookaside==0 || nLookaside==db->lookaside.nOut );
92546}
92547
92548/*
92549** Unlink the given table from the hash tables and the delete the
92550** table structure with all its indices and foreign keys.
92551*/
92552SQLITE_PRIVATE void sqlite3UnlinkAndDeleteTable(sqlite3 *db, int iDb, const char *zTabName){
92553  Table *p;
92554  Db *pDb;
92555
92556  assert( db!=0 );
92557  assert( iDb>=0 && iDb<db->nDb );
92558  assert( zTabName );
92559  assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
92560  testcase( zTabName[0]==0 );  /* Zero-length table names are allowed */
92561  pDb = &db->aDb[iDb];
92562  p = sqlite3HashInsert(&pDb->pSchema->tblHash, zTabName, 0);
92563  sqlite3DeleteTable(db, p);
92564  db->flags |= SQLITE_InternChanges;
92565}
92566
92567/*
92568** Given a token, return a string that consists of the text of that
92569** token.  Space to hold the returned string
92570** is obtained from sqliteMalloc() and must be freed by the calling
92571** function.
92572**
92573** Any quotation marks (ex:  "name", 'name', [name], or `name`) that
92574** surround the body of the token are removed.
92575**
92576** Tokens are often just pointers into the original SQL text and so
92577** are not \000 terminated and are not persistent.  The returned string
92578** is \000 terminated and is persistent.
92579*/
92580SQLITE_PRIVATE char *sqlite3NameFromToken(sqlite3 *db, Token *pName){
92581  char *zName;
92582  if( pName ){
92583    zName = sqlite3DbStrNDup(db, (char*)pName->z, pName->n);
92584    sqlite3Dequote(zName);
92585  }else{
92586    zName = 0;
92587  }
92588  return zName;
92589}
92590
92591/*
92592** Open the sqlite_master table stored in database number iDb for
92593** writing. The table is opened using cursor 0.
92594*/
92595SQLITE_PRIVATE void sqlite3OpenMasterTable(Parse *p, int iDb){
92596  Vdbe *v = sqlite3GetVdbe(p);
92597  sqlite3TableLock(p, iDb, MASTER_ROOT, 1, SCHEMA_TABLE(iDb));
92598  sqlite3VdbeAddOp4Int(v, OP_OpenWrite, 0, MASTER_ROOT, iDb, 5);
92599  if( p->nTab==0 ){
92600    p->nTab = 1;
92601  }
92602}
92603
92604/*
92605** Parameter zName points to a nul-terminated buffer containing the name
92606** of a database ("main", "temp" or the name of an attached db). This
92607** function returns the index of the named database in db->aDb[], or
92608** -1 if the named db cannot be found.
92609*/
92610SQLITE_PRIVATE int sqlite3FindDbName(sqlite3 *db, const char *zName){
92611  int i = -1;         /* Database number */
92612  if( zName ){
92613    Db *pDb;
92614    int n = sqlite3Strlen30(zName);
92615    for(i=(db->nDb-1), pDb=&db->aDb[i]; i>=0; i--, pDb--){
92616      if( (!OMIT_TEMPDB || i!=1 ) && n==sqlite3Strlen30(pDb->zName) &&
92617          0==sqlite3StrICmp(pDb->zName, zName) ){
92618        break;
92619      }
92620    }
92621  }
92622  return i;
92623}
92624
92625/*
92626** The token *pName contains the name of a database (either "main" or
92627** "temp" or the name of an attached db). This routine returns the
92628** index of the named database in db->aDb[], or -1 if the named db
92629** does not exist.
92630*/
92631SQLITE_PRIVATE int sqlite3FindDb(sqlite3 *db, Token *pName){
92632  int i;                               /* Database number */
92633  char *zName;                         /* Name we are searching for */
92634  zName = sqlite3NameFromToken(db, pName);
92635  i = sqlite3FindDbName(db, zName);
92636  sqlite3DbFree(db, zName);
92637  return i;
92638}
92639
92640/* The table or view or trigger name is passed to this routine via tokens
92641** pName1 and pName2. If the table name was fully qualified, for example:
92642**
92643** CREATE TABLE xxx.yyy (...);
92644**
92645** Then pName1 is set to "xxx" and pName2 "yyy". On the other hand if
92646** the table name is not fully qualified, i.e.:
92647**
92648** CREATE TABLE yyy(...);
92649**
92650** Then pName1 is set to "yyy" and pName2 is "".
92651**
92652** This routine sets the *ppUnqual pointer to point at the token (pName1 or
92653** pName2) that stores the unqualified table name.  The index of the
92654** database "xxx" is returned.
92655*/
92656SQLITE_PRIVATE int sqlite3TwoPartName(
92657  Parse *pParse,      /* Parsing and code generating context */
92658  Token *pName1,      /* The "xxx" in the name "xxx.yyy" or "xxx" */
92659  Token *pName2,      /* The "yyy" in the name "xxx.yyy" */
92660  Token **pUnqual     /* Write the unqualified object name here */
92661){
92662  int iDb;                    /* Database holding the object */
92663  sqlite3 *db = pParse->db;
92664
92665  if( ALWAYS(pName2!=0) && pName2->n>0 ){
92666    if( db->init.busy ) {
92667      sqlite3ErrorMsg(pParse, "corrupt database");
92668      return -1;
92669    }
92670    *pUnqual = pName2;
92671    iDb = sqlite3FindDb(db, pName1);
92672    if( iDb<0 ){
92673      sqlite3ErrorMsg(pParse, "unknown database %T", pName1);
92674      return -1;
92675    }
92676  }else{
92677    assert( db->init.iDb==0 || db->init.busy );
92678    iDb = db->init.iDb;
92679    *pUnqual = pName1;
92680  }
92681  return iDb;
92682}
92683
92684/*
92685** This routine is used to check if the UTF-8 string zName is a legal
92686** unqualified name for a new schema object (table, index, view or
92687** trigger). All names are legal except those that begin with the string
92688** "sqlite_" (in upper, lower or mixed case). This portion of the namespace
92689** is reserved for internal use.
92690*/
92691SQLITE_PRIVATE int sqlite3CheckObjectName(Parse *pParse, const char *zName){
92692  if( !pParse->db->init.busy && pParse->nested==0
92693          && (pParse->db->flags & SQLITE_WriteSchema)==0
92694          && 0==sqlite3StrNICmp(zName, "sqlite_", 7) ){
92695    sqlite3ErrorMsg(pParse, "object name reserved for internal use: %s", zName);
92696    return SQLITE_ERROR;
92697  }
92698  return SQLITE_OK;
92699}
92700
92701/*
92702** Return the PRIMARY KEY index of a table
92703*/
92704SQLITE_PRIVATE Index *sqlite3PrimaryKeyIndex(Table *pTab){
92705  Index *p;
92706  for(p=pTab->pIndex; p && !IsPrimaryKeyIndex(p); p=p->pNext){}
92707  return p;
92708}
92709
92710/*
92711** Return the column of index pIdx that corresponds to table
92712** column iCol.  Return -1 if not found.
92713*/
92714SQLITE_PRIVATE i16 sqlite3ColumnOfIndex(Index *pIdx, i16 iCol){
92715  int i;
92716  for(i=0; i<pIdx->nColumn; i++){
92717    if( iCol==pIdx->aiColumn[i] ) return i;
92718  }
92719  return -1;
92720}
92721
92722/*
92723** Begin constructing a new table representation in memory.  This is
92724** the first of several action routines that get called in response
92725** to a CREATE TABLE statement.  In particular, this routine is called
92726** after seeing tokens "CREATE" and "TABLE" and the table name. The isTemp
92727** flag is true if the table should be stored in the auxiliary database
92728** file instead of in the main database file.  This is normally the case
92729** when the "TEMP" or "TEMPORARY" keyword occurs in between
92730** CREATE and TABLE.
92731**
92732** The new table record is initialized and put in pParse->pNewTable.
92733** As more of the CREATE TABLE statement is parsed, additional action
92734** routines will be called to add more information to this record.
92735** At the end of the CREATE TABLE statement, the sqlite3EndTable() routine
92736** is called to complete the construction of the new table record.
92737*/
92738SQLITE_PRIVATE void sqlite3StartTable(
92739  Parse *pParse,   /* Parser context */
92740  Token *pName1,   /* First part of the name of the table or view */
92741  Token *pName2,   /* Second part of the name of the table or view */
92742  int isTemp,      /* True if this is a TEMP table */
92743  int isView,      /* True if this is a VIEW */
92744  int isVirtual,   /* True if this is a VIRTUAL table */
92745  int noErr        /* Do nothing if table already exists */
92746){
92747  Table *pTable;
92748  char *zName = 0; /* The name of the new table */
92749  sqlite3 *db = pParse->db;
92750  Vdbe *v;
92751  int iDb;         /* Database number to create the table in */
92752  Token *pName;    /* Unqualified name of the table to create */
92753
92754  /* The table or view name to create is passed to this routine via tokens
92755  ** pName1 and pName2. If the table name was fully qualified, for example:
92756  **
92757  ** CREATE TABLE xxx.yyy (...);
92758  **
92759  ** Then pName1 is set to "xxx" and pName2 "yyy". On the other hand if
92760  ** the table name is not fully qualified, i.e.:
92761  **
92762  ** CREATE TABLE yyy(...);
92763  **
92764  ** Then pName1 is set to "yyy" and pName2 is "".
92765  **
92766  ** The call below sets the pName pointer to point at the token (pName1 or
92767  ** pName2) that stores the unqualified table name. The variable iDb is
92768  ** set to the index of the database that the table or view is to be
92769  ** created in.
92770  */
92771  iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pName);
92772  if( iDb<0 ) return;
92773  if( !OMIT_TEMPDB && isTemp && pName2->n>0 && iDb!=1 ){
92774    /* If creating a temp table, the name may not be qualified. Unless
92775    ** the database name is "temp" anyway.  */
92776    sqlite3ErrorMsg(pParse, "temporary table name must be unqualified");
92777    return;
92778  }
92779  if( !OMIT_TEMPDB && isTemp ) iDb = 1;
92780
92781  pParse->sNameToken = *pName;
92782  zName = sqlite3NameFromToken(db, pName);
92783  if( zName==0 ) return;
92784  if( SQLITE_OK!=sqlite3CheckObjectName(pParse, zName) ){
92785    goto begin_table_error;
92786  }
92787  if( db->init.iDb==1 ) isTemp = 1;
92788#ifndef SQLITE_OMIT_AUTHORIZATION
92789  assert( (isTemp & 1)==isTemp );
92790  {
92791    int code;
92792    char *zDb = db->aDb[iDb].zName;
92793    if( sqlite3AuthCheck(pParse, SQLITE_INSERT, SCHEMA_TABLE(isTemp), 0, zDb) ){
92794      goto begin_table_error;
92795    }
92796    if( isView ){
92797      if( !OMIT_TEMPDB && isTemp ){
92798        code = SQLITE_CREATE_TEMP_VIEW;
92799      }else{
92800        code = SQLITE_CREATE_VIEW;
92801      }
92802    }else{
92803      if( !OMIT_TEMPDB && isTemp ){
92804        code = SQLITE_CREATE_TEMP_TABLE;
92805      }else{
92806        code = SQLITE_CREATE_TABLE;
92807      }
92808    }
92809    if( !isVirtual && sqlite3AuthCheck(pParse, code, zName, 0, zDb) ){
92810      goto begin_table_error;
92811    }
92812  }
92813#endif
92814
92815  /* Make sure the new table name does not collide with an existing
92816  ** index or table name in the same database.  Issue an error message if
92817  ** it does. The exception is if the statement being parsed was passed
92818  ** to an sqlite3_declare_vtab() call. In that case only the column names
92819  ** and types will be used, so there is no need to test for namespace
92820  ** collisions.
92821  */
92822  if( !IN_DECLARE_VTAB ){
92823    char *zDb = db->aDb[iDb].zName;
92824    if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){
92825      goto begin_table_error;
92826    }
92827    pTable = sqlite3FindTable(db, zName, zDb);
92828    if( pTable ){
92829      if( !noErr ){
92830        sqlite3ErrorMsg(pParse, "table %T already exists", pName);
92831      }else{
92832        assert( !db->init.busy || CORRUPT_DB );
92833        sqlite3CodeVerifySchema(pParse, iDb);
92834      }
92835      goto begin_table_error;
92836    }
92837    if( sqlite3FindIndex(db, zName, zDb)!=0 ){
92838      sqlite3ErrorMsg(pParse, "there is already an index named %s", zName);
92839      goto begin_table_error;
92840    }
92841  }
92842
92843  pTable = sqlite3DbMallocZero(db, sizeof(Table));
92844  if( pTable==0 ){
92845    db->mallocFailed = 1;
92846    pParse->rc = SQLITE_NOMEM;
92847    pParse->nErr++;
92848    goto begin_table_error;
92849  }
92850  pTable->zName = zName;
92851  pTable->iPKey = -1;
92852  pTable->pSchema = db->aDb[iDb].pSchema;
92853  pTable->nRef = 1;
92854  pTable->nRowLogEst = 200; assert( 200==sqlite3LogEst(1048576) );
92855  assert( pParse->pNewTable==0 );
92856  pParse->pNewTable = pTable;
92857
92858  /* If this is the magic sqlite_sequence table used by autoincrement,
92859  ** then record a pointer to this table in the main database structure
92860  ** so that INSERT can find the table easily.
92861  */
92862#ifndef SQLITE_OMIT_AUTOINCREMENT
92863  if( !pParse->nested && strcmp(zName, "sqlite_sequence")==0 ){
92864    assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
92865    pTable->pSchema->pSeqTab = pTable;
92866  }
92867#endif
92868
92869  /* Begin generating the code that will insert the table record into
92870  ** the SQLITE_MASTER table.  Note in particular that we must go ahead
92871  ** and allocate the record number for the table entry now.  Before any
92872  ** PRIMARY KEY or UNIQUE keywords are parsed.  Those keywords will cause
92873  ** indices to be created and the table record must come before the
92874  ** indices.  Hence, the record number for the table must be allocated
92875  ** now.
92876  */
92877  if( !db->init.busy && (v = sqlite3GetVdbe(pParse))!=0 ){
92878    int j1;
92879    int fileFormat;
92880    int reg1, reg2, reg3;
92881    sqlite3BeginWriteOperation(pParse, 1, iDb);
92882
92883#ifndef SQLITE_OMIT_VIRTUALTABLE
92884    if( isVirtual ){
92885      sqlite3VdbeAddOp0(v, OP_VBegin);
92886    }
92887#endif
92888
92889    /* If the file format and encoding in the database have not been set,
92890    ** set them now.
92891    */
92892    reg1 = pParse->regRowid = ++pParse->nMem;
92893    reg2 = pParse->regRoot = ++pParse->nMem;
92894    reg3 = ++pParse->nMem;
92895    sqlite3VdbeAddOp3(v, OP_ReadCookie, iDb, reg3, BTREE_FILE_FORMAT);
92896    sqlite3VdbeUsesBtree(v, iDb);
92897    j1 = sqlite3VdbeAddOp1(v, OP_If, reg3); VdbeCoverage(v);
92898    fileFormat = (db->flags & SQLITE_LegacyFileFmt)!=0 ?
92899                  1 : SQLITE_MAX_FILE_FORMAT;
92900    sqlite3VdbeAddOp2(v, OP_Integer, fileFormat, reg3);
92901    sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, BTREE_FILE_FORMAT, reg3);
92902    sqlite3VdbeAddOp2(v, OP_Integer, ENC(db), reg3);
92903    sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, BTREE_TEXT_ENCODING, reg3);
92904    sqlite3VdbeJumpHere(v, j1);
92905
92906    /* This just creates a place-holder record in the sqlite_master table.
92907    ** The record created does not contain anything yet.  It will be replaced
92908    ** by the real entry in code generated at sqlite3EndTable().
92909    **
92910    ** The rowid for the new entry is left in register pParse->regRowid.
92911    ** The root page number of the new table is left in reg pParse->regRoot.
92912    ** The rowid and root page number values are needed by the code that
92913    ** sqlite3EndTable will generate.
92914    */
92915#if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_VIRTUALTABLE)
92916    if( isView || isVirtual ){
92917      sqlite3VdbeAddOp2(v, OP_Integer, 0, reg2);
92918    }else
92919#endif
92920    {
92921      pParse->addrCrTab = sqlite3VdbeAddOp2(v, OP_CreateTable, iDb, reg2);
92922    }
92923    sqlite3OpenMasterTable(pParse, iDb);
92924    sqlite3VdbeAddOp2(v, OP_NewRowid, 0, reg1);
92925    sqlite3VdbeAddOp2(v, OP_Null, 0, reg3);
92926    sqlite3VdbeAddOp3(v, OP_Insert, 0, reg3, reg1);
92927    sqlite3VdbeChangeP5(v, OPFLAG_APPEND);
92928    sqlite3VdbeAddOp0(v, OP_Close);
92929  }
92930
92931  /* Normal (non-error) return. */
92932  return;
92933
92934  /* If an error occurs, we jump here */
92935begin_table_error:
92936  sqlite3DbFree(db, zName);
92937  return;
92938}
92939
92940/*
92941** This macro is used to compare two strings in a case-insensitive manner.
92942** It is slightly faster than calling sqlite3StrICmp() directly, but
92943** produces larger code.
92944**
92945** WARNING: This macro is not compatible with the strcmp() family. It
92946** returns true if the two strings are equal, otherwise false.
92947*/
92948#define STRICMP(x, y) (\
92949sqlite3UpperToLower[*(unsigned char *)(x)]==   \
92950sqlite3UpperToLower[*(unsigned char *)(y)]     \
92951&& sqlite3StrICmp((x)+1,(y)+1)==0 )
92952
92953/*
92954** Add a new column to the table currently being constructed.
92955**
92956** The parser calls this routine once for each column declaration
92957** in a CREATE TABLE statement.  sqlite3StartTable() gets called
92958** first to get things going.  Then this routine is called for each
92959** column.
92960*/
92961SQLITE_PRIVATE void sqlite3AddColumn(Parse *pParse, Token *pName){
92962  Table *p;
92963  int i;
92964  char *z;
92965  Column *pCol;
92966  sqlite3 *db = pParse->db;
92967  if( (p = pParse->pNewTable)==0 ) return;
92968#if SQLITE_MAX_COLUMN
92969  if( p->nCol+1>db->aLimit[SQLITE_LIMIT_COLUMN] ){
92970    sqlite3ErrorMsg(pParse, "too many columns on %s", p->zName);
92971    return;
92972  }
92973#endif
92974  z = sqlite3NameFromToken(db, pName);
92975  if( z==0 ) return;
92976  for(i=0; i<p->nCol; i++){
92977    if( STRICMP(z, p->aCol[i].zName) ){
92978      sqlite3ErrorMsg(pParse, "duplicate column name: %s", z);
92979      sqlite3DbFree(db, z);
92980      return;
92981    }
92982  }
92983  if( (p->nCol & 0x7)==0 ){
92984    Column *aNew;
92985    aNew = sqlite3DbRealloc(db,p->aCol,(p->nCol+8)*sizeof(p->aCol[0]));
92986    if( aNew==0 ){
92987      sqlite3DbFree(db, z);
92988      return;
92989    }
92990    p->aCol = aNew;
92991  }
92992  pCol = &p->aCol[p->nCol];
92993  memset(pCol, 0, sizeof(p->aCol[0]));
92994  pCol->zName = z;
92995
92996  /* If there is no type specified, columns have the default affinity
92997  ** 'BLOB'. If there is a type specified, then sqlite3AddColumnType() will
92998  ** be called next to set pCol->affinity correctly.
92999  */
93000  pCol->affinity = SQLITE_AFF_BLOB;
93001  pCol->szEst = 1;
93002  p->nCol++;
93003}
93004
93005/*
93006** This routine is called by the parser while in the middle of
93007** parsing a CREATE TABLE statement.  A "NOT NULL" constraint has
93008** been seen on a column.  This routine sets the notNull flag on
93009** the column currently under construction.
93010*/
93011SQLITE_PRIVATE void sqlite3AddNotNull(Parse *pParse, int onError){
93012  Table *p;
93013  p = pParse->pNewTable;
93014  if( p==0 || NEVER(p->nCol<1) ) return;
93015  p->aCol[p->nCol-1].notNull = (u8)onError;
93016}
93017
93018/*
93019** Scan the column type name zType (length nType) and return the
93020** associated affinity type.
93021**
93022** This routine does a case-independent search of zType for the
93023** substrings in the following table. If one of the substrings is
93024** found, the corresponding affinity is returned. If zType contains
93025** more than one of the substrings, entries toward the top of
93026** the table take priority. For example, if zType is 'BLOBINT',
93027** SQLITE_AFF_INTEGER is returned.
93028**
93029** Substring     | Affinity
93030** --------------------------------
93031** 'INT'         | SQLITE_AFF_INTEGER
93032** 'CHAR'        | SQLITE_AFF_TEXT
93033** 'CLOB'        | SQLITE_AFF_TEXT
93034** 'TEXT'        | SQLITE_AFF_TEXT
93035** 'BLOB'        | SQLITE_AFF_BLOB
93036** 'REAL'        | SQLITE_AFF_REAL
93037** 'FLOA'        | SQLITE_AFF_REAL
93038** 'DOUB'        | SQLITE_AFF_REAL
93039**
93040** If none of the substrings in the above table are found,
93041** SQLITE_AFF_NUMERIC is returned.
93042*/
93043SQLITE_PRIVATE char sqlite3AffinityType(const char *zIn, u8 *pszEst){
93044  u32 h = 0;
93045  char aff = SQLITE_AFF_NUMERIC;
93046  const char *zChar = 0;
93047
93048  if( zIn==0 ) return aff;
93049  while( zIn[0] ){
93050    h = (h<<8) + sqlite3UpperToLower[(*zIn)&0xff];
93051    zIn++;
93052    if( h==(('c'<<24)+('h'<<16)+('a'<<8)+'r') ){             /* CHAR */
93053      aff = SQLITE_AFF_TEXT;
93054      zChar = zIn;
93055    }else if( h==(('c'<<24)+('l'<<16)+('o'<<8)+'b') ){       /* CLOB */
93056      aff = SQLITE_AFF_TEXT;
93057    }else if( h==(('t'<<24)+('e'<<16)+('x'<<8)+'t') ){       /* TEXT */
93058      aff = SQLITE_AFF_TEXT;
93059    }else if( h==(('b'<<24)+('l'<<16)+('o'<<8)+'b')          /* BLOB */
93060        && (aff==SQLITE_AFF_NUMERIC || aff==SQLITE_AFF_REAL) ){
93061      aff = SQLITE_AFF_BLOB;
93062      if( zIn[0]=='(' ) zChar = zIn;
93063#ifndef SQLITE_OMIT_FLOATING_POINT
93064    }else if( h==(('r'<<24)+('e'<<16)+('a'<<8)+'l')          /* REAL */
93065        && aff==SQLITE_AFF_NUMERIC ){
93066      aff = SQLITE_AFF_REAL;
93067    }else if( h==(('f'<<24)+('l'<<16)+('o'<<8)+'a')          /* FLOA */
93068        && aff==SQLITE_AFF_NUMERIC ){
93069      aff = SQLITE_AFF_REAL;
93070    }else if( h==(('d'<<24)+('o'<<16)+('u'<<8)+'b')          /* DOUB */
93071        && aff==SQLITE_AFF_NUMERIC ){
93072      aff = SQLITE_AFF_REAL;
93073#endif
93074    }else if( (h&0x00FFFFFF)==(('i'<<16)+('n'<<8)+'t') ){    /* INT */
93075      aff = SQLITE_AFF_INTEGER;
93076      break;
93077    }
93078  }
93079
93080  /* If pszEst is not NULL, store an estimate of the field size.  The
93081  ** estimate is scaled so that the size of an integer is 1.  */
93082  if( pszEst ){
93083    *pszEst = 1;   /* default size is approx 4 bytes */
93084    if( aff<SQLITE_AFF_NUMERIC ){
93085      if( zChar ){
93086        while( zChar[0] ){
93087          if( sqlite3Isdigit(zChar[0]) ){
93088            int v = 0;
93089            sqlite3GetInt32(zChar, &v);
93090            v = v/4 + 1;
93091            if( v>255 ) v = 255;
93092            *pszEst = v; /* BLOB(k), VARCHAR(k), CHAR(k) -> r=(k/4+1) */
93093            break;
93094          }
93095          zChar++;
93096        }
93097      }else{
93098        *pszEst = 5;   /* BLOB, TEXT, CLOB -> r=5  (approx 20 bytes)*/
93099      }
93100    }
93101  }
93102  return aff;
93103}
93104
93105/*
93106** This routine is called by the parser while in the middle of
93107** parsing a CREATE TABLE statement.  The pFirst token is the first
93108** token in the sequence of tokens that describe the type of the
93109** column currently under construction.   pLast is the last token
93110** in the sequence.  Use this information to construct a string
93111** that contains the typename of the column and store that string
93112** in zType.
93113*/
93114SQLITE_PRIVATE void sqlite3AddColumnType(Parse *pParse, Token *pType){
93115  Table *p;
93116  Column *pCol;
93117
93118  p = pParse->pNewTable;
93119  if( p==0 || NEVER(p->nCol<1) ) return;
93120  pCol = &p->aCol[p->nCol-1];
93121  assert( pCol->zType==0 || CORRUPT_DB );
93122  sqlite3DbFree(pParse->db, pCol->zType);
93123  pCol->zType = sqlite3NameFromToken(pParse->db, pType);
93124  pCol->affinity = sqlite3AffinityType(pCol->zType, &pCol->szEst);
93125}
93126
93127/*
93128** The expression is the default value for the most recently added column
93129** of the table currently under construction.
93130**
93131** Default value expressions must be constant.  Raise an exception if this
93132** is not the case.
93133**
93134** This routine is called by the parser while in the middle of
93135** parsing a CREATE TABLE statement.
93136*/
93137SQLITE_PRIVATE void sqlite3AddDefaultValue(Parse *pParse, ExprSpan *pSpan){
93138  Table *p;
93139  Column *pCol;
93140  sqlite3 *db = pParse->db;
93141  p = pParse->pNewTable;
93142  if( p!=0 ){
93143    pCol = &(p->aCol[p->nCol-1]);
93144    if( !sqlite3ExprIsConstantOrFunction(pSpan->pExpr, db->init.busy) ){
93145      sqlite3ErrorMsg(pParse, "default value of column [%s] is not constant",
93146          pCol->zName);
93147    }else{
93148      /* A copy of pExpr is used instead of the original, as pExpr contains
93149      ** tokens that point to volatile memory. The 'span' of the expression
93150      ** is required by pragma table_info.
93151      */
93152      sqlite3ExprDelete(db, pCol->pDflt);
93153      pCol->pDflt = sqlite3ExprDup(db, pSpan->pExpr, EXPRDUP_REDUCE);
93154      sqlite3DbFree(db, pCol->zDflt);
93155      pCol->zDflt = sqlite3DbStrNDup(db, (char*)pSpan->zStart,
93156                                     (int)(pSpan->zEnd - pSpan->zStart));
93157    }
93158  }
93159  sqlite3ExprDelete(db, pSpan->pExpr);
93160}
93161
93162/*
93163** Designate the PRIMARY KEY for the table.  pList is a list of names
93164** of columns that form the primary key.  If pList is NULL, then the
93165** most recently added column of the table is the primary key.
93166**
93167** A table can have at most one primary key.  If the table already has
93168** a primary key (and this is the second primary key) then create an
93169** error.
93170**
93171** If the PRIMARY KEY is on a single column whose datatype is INTEGER,
93172** then we will try to use that column as the rowid.  Set the Table.iPKey
93173** field of the table under construction to be the index of the
93174** INTEGER PRIMARY KEY column.  Table.iPKey is set to -1 if there is
93175** no INTEGER PRIMARY KEY.
93176**
93177** If the key is not an INTEGER PRIMARY KEY, then create a unique
93178** index for the key.  No index is created for INTEGER PRIMARY KEYs.
93179*/
93180SQLITE_PRIVATE void sqlite3AddPrimaryKey(
93181  Parse *pParse,    /* Parsing context */
93182  ExprList *pList,  /* List of field names to be indexed */
93183  int onError,      /* What to do with a uniqueness conflict */
93184  int autoInc,      /* True if the AUTOINCREMENT keyword is present */
93185  int sortOrder     /* SQLITE_SO_ASC or SQLITE_SO_DESC */
93186){
93187  Table *pTab = pParse->pNewTable;
93188  char *zType = 0;
93189  int iCol = -1, i;
93190  int nTerm;
93191  if( pTab==0 || IN_DECLARE_VTAB ) goto primary_key_exit;
93192  if( pTab->tabFlags & TF_HasPrimaryKey ){
93193    sqlite3ErrorMsg(pParse,
93194      "table \"%s\" has more than one primary key", pTab->zName);
93195    goto primary_key_exit;
93196  }
93197  pTab->tabFlags |= TF_HasPrimaryKey;
93198  if( pList==0 ){
93199    iCol = pTab->nCol - 1;
93200    pTab->aCol[iCol].colFlags |= COLFLAG_PRIMKEY;
93201    zType = pTab->aCol[iCol].zType;
93202    nTerm = 1;
93203  }else{
93204    nTerm = pList->nExpr;
93205    for(i=0; i<nTerm; i++){
93206      for(iCol=0; iCol<pTab->nCol; iCol++){
93207        if( sqlite3StrICmp(pList->a[i].zName, pTab->aCol[iCol].zName)==0 ){
93208          pTab->aCol[iCol].colFlags |= COLFLAG_PRIMKEY;
93209          zType = pTab->aCol[iCol].zType;
93210          break;
93211        }
93212      }
93213    }
93214  }
93215  if( nTerm==1
93216   && zType && sqlite3StrICmp(zType, "INTEGER")==0
93217   && sortOrder==SQLITE_SO_ASC
93218  ){
93219    pTab->iPKey = iCol;
93220    pTab->keyConf = (u8)onError;
93221    assert( autoInc==0 || autoInc==1 );
93222    pTab->tabFlags |= autoInc*TF_Autoincrement;
93223    if( pList ) pParse->iPkSortOrder = pList->a[0].sortOrder;
93224  }else if( autoInc ){
93225#ifndef SQLITE_OMIT_AUTOINCREMENT
93226    sqlite3ErrorMsg(pParse, "AUTOINCREMENT is only allowed on an "
93227       "INTEGER PRIMARY KEY");
93228#endif
93229  }else{
93230    Index *p;
93231    p = sqlite3CreateIndex(pParse, 0, 0, 0, pList, onError, 0,
93232                           0, sortOrder, 0);
93233    if( p ){
93234      p->idxType = SQLITE_IDXTYPE_PRIMARYKEY;
93235    }
93236    pList = 0;
93237  }
93238
93239primary_key_exit:
93240  sqlite3ExprListDelete(pParse->db, pList);
93241  return;
93242}
93243
93244/*
93245** Add a new CHECK constraint to the table currently under construction.
93246*/
93247SQLITE_PRIVATE void sqlite3AddCheckConstraint(
93248  Parse *pParse,    /* Parsing context */
93249  Expr *pCheckExpr  /* The check expression */
93250){
93251#ifndef SQLITE_OMIT_CHECK
93252  Table *pTab = pParse->pNewTable;
93253  sqlite3 *db = pParse->db;
93254  if( pTab && !IN_DECLARE_VTAB
93255   && !sqlite3BtreeIsReadonly(db->aDb[db->init.iDb].pBt)
93256  ){
93257    pTab->pCheck = sqlite3ExprListAppend(pParse, pTab->pCheck, pCheckExpr);
93258    if( pParse->constraintName.n ){
93259      sqlite3ExprListSetName(pParse, pTab->pCheck, &pParse->constraintName, 1);
93260    }
93261  }else
93262#endif
93263  {
93264    sqlite3ExprDelete(pParse->db, pCheckExpr);
93265  }
93266}
93267
93268/*
93269** Set the collation function of the most recently parsed table column
93270** to the CollSeq given.
93271*/
93272SQLITE_PRIVATE void sqlite3AddCollateType(Parse *pParse, Token *pToken){
93273  Table *p;
93274  int i;
93275  char *zColl;              /* Dequoted name of collation sequence */
93276  sqlite3 *db;
93277
93278  if( (p = pParse->pNewTable)==0 ) return;
93279  i = p->nCol-1;
93280  db = pParse->db;
93281  zColl = sqlite3NameFromToken(db, pToken);
93282  if( !zColl ) return;
93283
93284  if( sqlite3LocateCollSeq(pParse, zColl) ){
93285    Index *pIdx;
93286    sqlite3DbFree(db, p->aCol[i].zColl);
93287    p->aCol[i].zColl = zColl;
93288
93289    /* If the column is declared as "<name> PRIMARY KEY COLLATE <type>",
93290    ** then an index may have been created on this column before the
93291    ** collation type was added. Correct this if it is the case.
93292    */
93293    for(pIdx=p->pIndex; pIdx; pIdx=pIdx->pNext){
93294      assert( pIdx->nKeyCol==1 );
93295      if( pIdx->aiColumn[0]==i ){
93296        pIdx->azColl[0] = p->aCol[i].zColl;
93297      }
93298    }
93299  }else{
93300    sqlite3DbFree(db, zColl);
93301  }
93302}
93303
93304/*
93305** This function returns the collation sequence for database native text
93306** encoding identified by the string zName, length nName.
93307**
93308** If the requested collation sequence is not available, or not available
93309** in the database native encoding, the collation factory is invoked to
93310** request it. If the collation factory does not supply such a sequence,
93311** and the sequence is available in another text encoding, then that is
93312** returned instead.
93313**
93314** If no versions of the requested collations sequence are available, or
93315** another error occurs, NULL is returned and an error message written into
93316** pParse.
93317**
93318** This routine is a wrapper around sqlite3FindCollSeq().  This routine
93319** invokes the collation factory if the named collation cannot be found
93320** and generates an error message.
93321**
93322** See also: sqlite3FindCollSeq(), sqlite3GetCollSeq()
93323*/
93324SQLITE_PRIVATE CollSeq *sqlite3LocateCollSeq(Parse *pParse, const char *zName){
93325  sqlite3 *db = pParse->db;
93326  u8 enc = ENC(db);
93327  u8 initbusy = db->init.busy;
93328  CollSeq *pColl;
93329
93330  pColl = sqlite3FindCollSeq(db, enc, zName, initbusy);
93331  if( !initbusy && (!pColl || !pColl->xCmp) ){
93332    pColl = sqlite3GetCollSeq(pParse, enc, pColl, zName);
93333  }
93334
93335  return pColl;
93336}
93337
93338
93339/*
93340** Generate code that will increment the schema cookie.
93341**
93342** The schema cookie is used to determine when the schema for the
93343** database changes.  After each schema change, the cookie value
93344** changes.  When a process first reads the schema it records the
93345** cookie.  Thereafter, whenever it goes to access the database,
93346** it checks the cookie to make sure the schema has not changed
93347** since it was last read.
93348**
93349** This plan is not completely bullet-proof.  It is possible for
93350** the schema to change multiple times and for the cookie to be
93351** set back to prior value.  But schema changes are infrequent
93352** and the probability of hitting the same cookie value is only
93353** 1 chance in 2^32.  So we're safe enough.
93354*/
93355SQLITE_PRIVATE void sqlite3ChangeCookie(Parse *pParse, int iDb){
93356  int r1 = sqlite3GetTempReg(pParse);
93357  sqlite3 *db = pParse->db;
93358  Vdbe *v = pParse->pVdbe;
93359  assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
93360  sqlite3VdbeAddOp2(v, OP_Integer, db->aDb[iDb].pSchema->schema_cookie+1, r1);
93361  sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, BTREE_SCHEMA_VERSION, r1);
93362  sqlite3ReleaseTempReg(pParse, r1);
93363}
93364
93365/*
93366** Measure the number of characters needed to output the given
93367** identifier.  The number returned includes any quotes used
93368** but does not include the null terminator.
93369**
93370** The estimate is conservative.  It might be larger that what is
93371** really needed.
93372*/
93373static int identLength(const char *z){
93374  int n;
93375  for(n=0; *z; n++, z++){
93376    if( *z=='"' ){ n++; }
93377  }
93378  return n + 2;
93379}
93380
93381/*
93382** The first parameter is a pointer to an output buffer. The second
93383** parameter is a pointer to an integer that contains the offset at
93384** which to write into the output buffer. This function copies the
93385** nul-terminated string pointed to by the third parameter, zSignedIdent,
93386** to the specified offset in the buffer and updates *pIdx to refer
93387** to the first byte after the last byte written before returning.
93388**
93389** If the string zSignedIdent consists entirely of alpha-numeric
93390** characters, does not begin with a digit and is not an SQL keyword,
93391** then it is copied to the output buffer exactly as it is. Otherwise,
93392** it is quoted using double-quotes.
93393*/
93394static void identPut(char *z, int *pIdx, char *zSignedIdent){
93395  unsigned char *zIdent = (unsigned char*)zSignedIdent;
93396  int i, j, needQuote;
93397  i = *pIdx;
93398
93399  for(j=0; zIdent[j]; j++){
93400    if( !sqlite3Isalnum(zIdent[j]) && zIdent[j]!='_' ) break;
93401  }
93402  needQuote = sqlite3Isdigit(zIdent[0])
93403            || sqlite3KeywordCode(zIdent, j)!=TK_ID
93404            || zIdent[j]!=0
93405            || j==0;
93406
93407  if( needQuote ) z[i++] = '"';
93408  for(j=0; zIdent[j]; j++){
93409    z[i++] = zIdent[j];
93410    if( zIdent[j]=='"' ) z[i++] = '"';
93411  }
93412  if( needQuote ) z[i++] = '"';
93413  z[i] = 0;
93414  *pIdx = i;
93415}
93416
93417/*
93418** Generate a CREATE TABLE statement appropriate for the given
93419** table.  Memory to hold the text of the statement is obtained
93420** from sqliteMalloc() and must be freed by the calling function.
93421*/
93422static char *createTableStmt(sqlite3 *db, Table *p){
93423  int i, k, n;
93424  char *zStmt;
93425  char *zSep, *zSep2, *zEnd;
93426  Column *pCol;
93427  n = 0;
93428  for(pCol = p->aCol, i=0; i<p->nCol; i++, pCol++){
93429    n += identLength(pCol->zName) + 5;
93430  }
93431  n += identLength(p->zName);
93432  if( n<50 ){
93433    zSep = "";
93434    zSep2 = ",";
93435    zEnd = ")";
93436  }else{
93437    zSep = "\n  ";
93438    zSep2 = ",\n  ";
93439    zEnd = "\n)";
93440  }
93441  n += 35 + 6*p->nCol;
93442  zStmt = sqlite3DbMallocRaw(0, n);
93443  if( zStmt==0 ){
93444    db->mallocFailed = 1;
93445    return 0;
93446  }
93447  sqlite3_snprintf(n, zStmt, "CREATE TABLE ");
93448  k = sqlite3Strlen30(zStmt);
93449  identPut(zStmt, &k, p->zName);
93450  zStmt[k++] = '(';
93451  for(pCol=p->aCol, i=0; i<p->nCol; i++, pCol++){
93452    static const char * const azType[] = {
93453        /* SQLITE_AFF_BLOB    */ "",
93454        /* SQLITE_AFF_TEXT    */ " TEXT",
93455        /* SQLITE_AFF_NUMERIC */ " NUM",
93456        /* SQLITE_AFF_INTEGER */ " INT",
93457        /* SQLITE_AFF_REAL    */ " REAL"
93458    };
93459    int len;
93460    const char *zType;
93461
93462    sqlite3_snprintf(n-k, &zStmt[k], zSep);
93463    k += sqlite3Strlen30(&zStmt[k]);
93464    zSep = zSep2;
93465    identPut(zStmt, &k, pCol->zName);
93466    assert( pCol->affinity-SQLITE_AFF_BLOB >= 0 );
93467    assert( pCol->affinity-SQLITE_AFF_BLOB < ArraySize(azType) );
93468    testcase( pCol->affinity==SQLITE_AFF_BLOB );
93469    testcase( pCol->affinity==SQLITE_AFF_TEXT );
93470    testcase( pCol->affinity==SQLITE_AFF_NUMERIC );
93471    testcase( pCol->affinity==SQLITE_AFF_INTEGER );
93472    testcase( pCol->affinity==SQLITE_AFF_REAL );
93473
93474    zType = azType[pCol->affinity - SQLITE_AFF_BLOB];
93475    len = sqlite3Strlen30(zType);
93476    assert( pCol->affinity==SQLITE_AFF_BLOB
93477            || pCol->affinity==sqlite3AffinityType(zType, 0) );
93478    memcpy(&zStmt[k], zType, len);
93479    k += len;
93480    assert( k<=n );
93481  }
93482  sqlite3_snprintf(n-k, &zStmt[k], "%s", zEnd);
93483  return zStmt;
93484}
93485
93486/*
93487** Resize an Index object to hold N columns total.  Return SQLITE_OK
93488** on success and SQLITE_NOMEM on an OOM error.
93489*/
93490static int resizeIndexObject(sqlite3 *db, Index *pIdx, int N){
93491  char *zExtra;
93492  int nByte;
93493  if( pIdx->nColumn>=N ) return SQLITE_OK;
93494  assert( pIdx->isResized==0 );
93495  nByte = (sizeof(char*) + sizeof(i16) + 1)*N;
93496  zExtra = sqlite3DbMallocZero(db, nByte);
93497  if( zExtra==0 ) return SQLITE_NOMEM;
93498  memcpy(zExtra, pIdx->azColl, sizeof(char*)*pIdx->nColumn);
93499  pIdx->azColl = (char**)zExtra;
93500  zExtra += sizeof(char*)*N;
93501  memcpy(zExtra, pIdx->aiColumn, sizeof(i16)*pIdx->nColumn);
93502  pIdx->aiColumn = (i16*)zExtra;
93503  zExtra += sizeof(i16)*N;
93504  memcpy(zExtra, pIdx->aSortOrder, pIdx->nColumn);
93505  pIdx->aSortOrder = (u8*)zExtra;
93506  pIdx->nColumn = N;
93507  pIdx->isResized = 1;
93508  return SQLITE_OK;
93509}
93510
93511/*
93512** Estimate the total row width for a table.
93513*/
93514static void estimateTableWidth(Table *pTab){
93515  unsigned wTable = 0;
93516  const Column *pTabCol;
93517  int i;
93518  for(i=pTab->nCol, pTabCol=pTab->aCol; i>0; i--, pTabCol++){
93519    wTable += pTabCol->szEst;
93520  }
93521  if( pTab->iPKey<0 ) wTable++;
93522  pTab->szTabRow = sqlite3LogEst(wTable*4);
93523}
93524
93525/*
93526** Estimate the average size of a row for an index.
93527*/
93528static void estimateIndexWidth(Index *pIdx){
93529  unsigned wIndex = 0;
93530  int i;
93531  const Column *aCol = pIdx->pTable->aCol;
93532  for(i=0; i<pIdx->nColumn; i++){
93533    i16 x = pIdx->aiColumn[i];
93534    assert( x<pIdx->pTable->nCol );
93535    wIndex += x<0 ? 1 : aCol[pIdx->aiColumn[i]].szEst;
93536  }
93537  pIdx->szIdxRow = sqlite3LogEst(wIndex*4);
93538}
93539
93540/* Return true if value x is found any of the first nCol entries of aiCol[]
93541*/
93542static int hasColumn(const i16 *aiCol, int nCol, int x){
93543  while( nCol-- > 0 ) if( x==*(aiCol++) ) return 1;
93544  return 0;
93545}
93546
93547/*
93548** This routine runs at the end of parsing a CREATE TABLE statement that
93549** has a WITHOUT ROWID clause.  The job of this routine is to convert both
93550** internal schema data structures and the generated VDBE code so that they
93551** are appropriate for a WITHOUT ROWID table instead of a rowid table.
93552** Changes include:
93553**
93554**     (1)  Convert the OP_CreateTable into an OP_CreateIndex.  There is
93555**          no rowid btree for a WITHOUT ROWID.  Instead, the canonical
93556**          data storage is a covering index btree.
93557**     (2)  Bypass the creation of the sqlite_master table entry
93558**          for the PRIMARY KEY as the primary key index is now
93559**          identified by the sqlite_master table entry of the table itself.
93560**     (3)  Set the Index.tnum of the PRIMARY KEY Index object in the
93561**          schema to the rootpage from the main table.
93562**     (4)  Set all columns of the PRIMARY KEY schema object to be NOT NULL.
93563**     (5)  Add all table columns to the PRIMARY KEY Index object
93564**          so that the PRIMARY KEY is a covering index.  The surplus
93565**          columns are part of KeyInfo.nXField and are not used for
93566**          sorting or lookup or uniqueness checks.
93567**     (6)  Replace the rowid tail on all automatically generated UNIQUE
93568**          indices with the PRIMARY KEY columns.
93569*/
93570static void convertToWithoutRowidTable(Parse *pParse, Table *pTab){
93571  Index *pIdx;
93572  Index *pPk;
93573  int nPk;
93574  int i, j;
93575  sqlite3 *db = pParse->db;
93576  Vdbe *v = pParse->pVdbe;
93577
93578  /* Convert the OP_CreateTable opcode that would normally create the
93579  ** root-page for the table into an OP_CreateIndex opcode.  The index
93580  ** created will become the PRIMARY KEY index.
93581  */
93582  if( pParse->addrCrTab ){
93583    assert( v );
93584    sqlite3VdbeGetOp(v, pParse->addrCrTab)->opcode = OP_CreateIndex;
93585  }
93586
93587  /* Locate the PRIMARY KEY index.  Or, if this table was originally
93588  ** an INTEGER PRIMARY KEY table, create a new PRIMARY KEY index.
93589  */
93590  if( pTab->iPKey>=0 ){
93591    ExprList *pList;
93592    pList = sqlite3ExprListAppend(pParse, 0, 0);
93593    if( pList==0 ) return;
93594    pList->a[0].zName = sqlite3DbStrDup(pParse->db,
93595                                        pTab->aCol[pTab->iPKey].zName);
93596    pList->a[0].sortOrder = pParse->iPkSortOrder;
93597    assert( pParse->pNewTable==pTab );
93598    pPk = sqlite3CreateIndex(pParse, 0, 0, 0, pList, pTab->keyConf, 0, 0, 0, 0);
93599    if( pPk==0 ) return;
93600    pPk->idxType = SQLITE_IDXTYPE_PRIMARYKEY;
93601    pTab->iPKey = -1;
93602  }else{
93603    pPk = sqlite3PrimaryKeyIndex(pTab);
93604
93605    /* Bypass the creation of the PRIMARY KEY btree and the sqlite_master
93606    ** table entry. This is only required if currently generating VDBE
93607    ** code for a CREATE TABLE (not when parsing one as part of reading
93608    ** a database schema).  */
93609    if( v ){
93610      assert( db->init.busy==0 );
93611      sqlite3VdbeGetOp(v, pPk->tnum)->opcode = OP_Goto;
93612    }
93613
93614    /*
93615    ** Remove all redundant columns from the PRIMARY KEY.  For example, change
93616    ** "PRIMARY KEY(a,b,a,b,c,b,c,d)" into just "PRIMARY KEY(a,b,c,d)".  Later
93617    ** code assumes the PRIMARY KEY contains no repeated columns.
93618    */
93619    for(i=j=1; i<pPk->nKeyCol; i++){
93620      if( hasColumn(pPk->aiColumn, j, pPk->aiColumn[i]) ){
93621        pPk->nColumn--;
93622      }else{
93623        pPk->aiColumn[j++] = pPk->aiColumn[i];
93624      }
93625    }
93626    pPk->nKeyCol = j;
93627  }
93628  pPk->isCovering = 1;
93629  assert( pPk!=0 );
93630  nPk = pPk->nKeyCol;
93631
93632  /* Make sure every column of the PRIMARY KEY is NOT NULL.  (Except,
93633  ** do not enforce this for imposter tables.) */
93634  if( !db->init.imposterTable ){
93635    for(i=0; i<nPk; i++){
93636      pTab->aCol[pPk->aiColumn[i]].notNull = 1;
93637    }
93638    pPk->uniqNotNull = 1;
93639  }
93640
93641  /* The root page of the PRIMARY KEY is the table root page */
93642  pPk->tnum = pTab->tnum;
93643
93644  /* Update the in-memory representation of all UNIQUE indices by converting
93645  ** the final rowid column into one or more columns of the PRIMARY KEY.
93646  */
93647  for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
93648    int n;
93649    if( IsPrimaryKeyIndex(pIdx) ) continue;
93650    for(i=n=0; i<nPk; i++){
93651      if( !hasColumn(pIdx->aiColumn, pIdx->nKeyCol, pPk->aiColumn[i]) ) n++;
93652    }
93653    if( n==0 ){
93654      /* This index is a superset of the primary key */
93655      pIdx->nColumn = pIdx->nKeyCol;
93656      continue;
93657    }
93658    if( resizeIndexObject(db, pIdx, pIdx->nKeyCol+n) ) return;
93659    for(i=0, j=pIdx->nKeyCol; i<nPk; i++){
93660      if( !hasColumn(pIdx->aiColumn, pIdx->nKeyCol, pPk->aiColumn[i]) ){
93661        pIdx->aiColumn[j] = pPk->aiColumn[i];
93662        pIdx->azColl[j] = pPk->azColl[i];
93663        j++;
93664      }
93665    }
93666    assert( pIdx->nColumn>=pIdx->nKeyCol+n );
93667    assert( pIdx->nColumn>=j );
93668  }
93669
93670  /* Add all table columns to the PRIMARY KEY index
93671  */
93672  if( nPk<pTab->nCol ){
93673    if( resizeIndexObject(db, pPk, pTab->nCol) ) return;
93674    for(i=0, j=nPk; i<pTab->nCol; i++){
93675      if( !hasColumn(pPk->aiColumn, j, i) ){
93676        assert( j<pPk->nColumn );
93677        pPk->aiColumn[j] = i;
93678        pPk->azColl[j] = "BINARY";
93679        j++;
93680      }
93681    }
93682    assert( pPk->nColumn==j );
93683    assert( pTab->nCol==j );
93684  }else{
93685    pPk->nColumn = pTab->nCol;
93686  }
93687}
93688
93689/*
93690** This routine is called to report the final ")" that terminates
93691** a CREATE TABLE statement.
93692**
93693** The table structure that other action routines have been building
93694** is added to the internal hash tables, assuming no errors have
93695** occurred.
93696**
93697** An entry for the table is made in the master table on disk, unless
93698** this is a temporary table or db->init.busy==1.  When db->init.busy==1
93699** it means we are reading the sqlite_master table because we just
93700** connected to the database or because the sqlite_master table has
93701** recently changed, so the entry for this table already exists in
93702** the sqlite_master table.  We do not want to create it again.
93703**
93704** If the pSelect argument is not NULL, it means that this routine
93705** was called to create a table generated from a
93706** "CREATE TABLE ... AS SELECT ..." statement.  The column names of
93707** the new table will match the result set of the SELECT.
93708*/
93709SQLITE_PRIVATE void sqlite3EndTable(
93710  Parse *pParse,          /* Parse context */
93711  Token *pCons,           /* The ',' token after the last column defn. */
93712  Token *pEnd,            /* The ')' before options in the CREATE TABLE */
93713  u8 tabOpts,             /* Extra table options. Usually 0. */
93714  Select *pSelect         /* Select from a "CREATE ... AS SELECT" */
93715){
93716  Table *p;                 /* The new table */
93717  sqlite3 *db = pParse->db; /* The database connection */
93718  int iDb;                  /* Database in which the table lives */
93719  Index *pIdx;              /* An implied index of the table */
93720
93721  if( (pEnd==0 && pSelect==0) || db->mallocFailed ){
93722    return;
93723  }
93724  p = pParse->pNewTable;
93725  if( p==0 ) return;
93726
93727  assert( !db->init.busy || !pSelect );
93728
93729  /* If the db->init.busy is 1 it means we are reading the SQL off the
93730  ** "sqlite_master" or "sqlite_temp_master" table on the disk.
93731  ** So do not write to the disk again.  Extract the root page number
93732  ** for the table from the db->init.newTnum field.  (The page number
93733  ** should have been put there by the sqliteOpenCb routine.)
93734  */
93735  if( db->init.busy ){
93736    p->tnum = db->init.newTnum;
93737  }
93738
93739  /* Special processing for WITHOUT ROWID Tables */
93740  if( tabOpts & TF_WithoutRowid ){
93741    if( (p->tabFlags & TF_Autoincrement) ){
93742      sqlite3ErrorMsg(pParse,
93743          "AUTOINCREMENT not allowed on WITHOUT ROWID tables");
93744      return;
93745    }
93746    if( (p->tabFlags & TF_HasPrimaryKey)==0 ){
93747      sqlite3ErrorMsg(pParse, "PRIMARY KEY missing on table %s", p->zName);
93748    }else{
93749      p->tabFlags |= TF_WithoutRowid | TF_NoVisibleRowid;
93750      convertToWithoutRowidTable(pParse, p);
93751    }
93752  }
93753
93754  iDb = sqlite3SchemaToIndex(db, p->pSchema);
93755
93756#ifndef SQLITE_OMIT_CHECK
93757  /* Resolve names in all CHECK constraint expressions.
93758  */
93759  if( p->pCheck ){
93760    sqlite3ResolveSelfReference(pParse, p, NC_IsCheck, 0, p->pCheck);
93761  }
93762#endif /* !defined(SQLITE_OMIT_CHECK) */
93763
93764  /* Estimate the average row size for the table and for all implied indices */
93765  estimateTableWidth(p);
93766  for(pIdx=p->pIndex; pIdx; pIdx=pIdx->pNext){
93767    estimateIndexWidth(pIdx);
93768  }
93769
93770  /* If not initializing, then create a record for the new table
93771  ** in the SQLITE_MASTER table of the database.
93772  **
93773  ** If this is a TEMPORARY table, write the entry into the auxiliary
93774  ** file instead of into the main database file.
93775  */
93776  if( !db->init.busy ){
93777    int n;
93778    Vdbe *v;
93779    char *zType;    /* "view" or "table" */
93780    char *zType2;   /* "VIEW" or "TABLE" */
93781    char *zStmt;    /* Text of the CREATE TABLE or CREATE VIEW statement */
93782
93783    v = sqlite3GetVdbe(pParse);
93784    if( NEVER(v==0) ) return;
93785
93786    sqlite3VdbeAddOp1(v, OP_Close, 0);
93787
93788    /*
93789    ** Initialize zType for the new view or table.
93790    */
93791    if( p->pSelect==0 ){
93792      /* A regular table */
93793      zType = "table";
93794      zType2 = "TABLE";
93795#ifndef SQLITE_OMIT_VIEW
93796    }else{
93797      /* A view */
93798      zType = "view";
93799      zType2 = "VIEW";
93800#endif
93801    }
93802
93803    /* If this is a CREATE TABLE xx AS SELECT ..., execute the SELECT
93804    ** statement to populate the new table. The root-page number for the
93805    ** new table is in register pParse->regRoot.
93806    **
93807    ** Once the SELECT has been coded by sqlite3Select(), it is in a
93808    ** suitable state to query for the column names and types to be used
93809    ** by the new table.
93810    **
93811    ** A shared-cache write-lock is not required to write to the new table,
93812    ** as a schema-lock must have already been obtained to create it. Since
93813    ** a schema-lock excludes all other database users, the write-lock would
93814    ** be redundant.
93815    */
93816    if( pSelect ){
93817      SelectDest dest;    /* Where the SELECT should store results */
93818      int regYield;       /* Register holding co-routine entry-point */
93819      int addrTop;        /* Top of the co-routine */
93820      int regRec;         /* A record to be insert into the new table */
93821      int regRowid;       /* Rowid of the next row to insert */
93822      int addrInsLoop;    /* Top of the loop for inserting rows */
93823      Table *pSelTab;     /* A table that describes the SELECT results */
93824
93825      regYield = ++pParse->nMem;
93826      regRec = ++pParse->nMem;
93827      regRowid = ++pParse->nMem;
93828      assert(pParse->nTab==1);
93829      sqlite3MayAbort(pParse);
93830      sqlite3VdbeAddOp3(v, OP_OpenWrite, 1, pParse->regRoot, iDb);
93831      sqlite3VdbeChangeP5(v, OPFLAG_P2ISREG);
93832      pParse->nTab = 2;
93833      addrTop = sqlite3VdbeCurrentAddr(v) + 1;
93834      sqlite3VdbeAddOp3(v, OP_InitCoroutine, regYield, 0, addrTop);
93835      sqlite3SelectDestInit(&dest, SRT_Coroutine, regYield);
93836      sqlite3Select(pParse, pSelect, &dest);
93837      sqlite3VdbeAddOp1(v, OP_EndCoroutine, regYield);
93838      sqlite3VdbeJumpHere(v, addrTop - 1);
93839      if( pParse->nErr ) return;
93840      pSelTab = sqlite3ResultSetOfSelect(pParse, pSelect);
93841      if( pSelTab==0 ) return;
93842      assert( p->aCol==0 );
93843      p->nCol = pSelTab->nCol;
93844      p->aCol = pSelTab->aCol;
93845      pSelTab->nCol = 0;
93846      pSelTab->aCol = 0;
93847      sqlite3DeleteTable(db, pSelTab);
93848      addrInsLoop = sqlite3VdbeAddOp1(v, OP_Yield, dest.iSDParm);
93849      VdbeCoverage(v);
93850      sqlite3VdbeAddOp3(v, OP_MakeRecord, dest.iSdst, dest.nSdst, regRec);
93851      sqlite3TableAffinity(v, p, 0);
93852      sqlite3VdbeAddOp2(v, OP_NewRowid, 1, regRowid);
93853      sqlite3VdbeAddOp3(v, OP_Insert, 1, regRec, regRowid);
93854      sqlite3VdbeAddOp2(v, OP_Goto, 0, addrInsLoop);
93855      sqlite3VdbeJumpHere(v, addrInsLoop);
93856      sqlite3VdbeAddOp1(v, OP_Close, 1);
93857    }
93858
93859    /* Compute the complete text of the CREATE statement */
93860    if( pSelect ){
93861      zStmt = createTableStmt(db, p);
93862    }else{
93863      Token *pEnd2 = tabOpts ? &pParse->sLastToken : pEnd;
93864      n = (int)(pEnd2->z - pParse->sNameToken.z);
93865      if( pEnd2->z[0]!=';' ) n += pEnd2->n;
93866      zStmt = sqlite3MPrintf(db,
93867          "CREATE %s %.*s", zType2, n, pParse->sNameToken.z
93868      );
93869    }
93870
93871    /* A slot for the record has already been allocated in the
93872    ** SQLITE_MASTER table.  We just need to update that slot with all
93873    ** the information we've collected.
93874    */
93875    sqlite3NestedParse(pParse,
93876      "UPDATE %Q.%s "
93877         "SET type='%s', name=%Q, tbl_name=%Q, rootpage=#%d, sql=%Q "
93878       "WHERE rowid=#%d",
93879      db->aDb[iDb].zName, SCHEMA_TABLE(iDb),
93880      zType,
93881      p->zName,
93882      p->zName,
93883      pParse->regRoot,
93884      zStmt,
93885      pParse->regRowid
93886    );
93887    sqlite3DbFree(db, zStmt);
93888    sqlite3ChangeCookie(pParse, iDb);
93889
93890#ifndef SQLITE_OMIT_AUTOINCREMENT
93891    /* Check to see if we need to create an sqlite_sequence table for
93892    ** keeping track of autoincrement keys.
93893    */
93894    if( p->tabFlags & TF_Autoincrement ){
93895      Db *pDb = &db->aDb[iDb];
93896      assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
93897      if( pDb->pSchema->pSeqTab==0 ){
93898        sqlite3NestedParse(pParse,
93899          "CREATE TABLE %Q.sqlite_sequence(name,seq)",
93900          pDb->zName
93901        );
93902      }
93903    }
93904#endif
93905
93906    /* Reparse everything to update our internal data structures */
93907    sqlite3VdbeAddParseSchemaOp(v, iDb,
93908           sqlite3MPrintf(db, "tbl_name='%q' AND type!='trigger'", p->zName));
93909  }
93910
93911
93912  /* Add the table to the in-memory representation of the database.
93913  */
93914  if( db->init.busy ){
93915    Table *pOld;
93916    Schema *pSchema = p->pSchema;
93917    assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
93918    pOld = sqlite3HashInsert(&pSchema->tblHash, p->zName, p);
93919    if( pOld ){
93920      assert( p==pOld );  /* Malloc must have failed inside HashInsert() */
93921      db->mallocFailed = 1;
93922      return;
93923    }
93924    pParse->pNewTable = 0;
93925    db->flags |= SQLITE_InternChanges;
93926
93927#ifndef SQLITE_OMIT_ALTERTABLE
93928    if( !p->pSelect ){
93929      const char *zName = (const char *)pParse->sNameToken.z;
93930      int nName;
93931      assert( !pSelect && pCons && pEnd );
93932      if( pCons->z==0 ){
93933        pCons = pEnd;
93934      }
93935      nName = (int)((const char *)pCons->z - zName);
93936      p->addColOffset = 13 + sqlite3Utf8CharLen(zName, nName);
93937    }
93938#endif
93939  }
93940}
93941
93942#ifndef SQLITE_OMIT_VIEW
93943/*
93944** The parser calls this routine in order to create a new VIEW
93945*/
93946SQLITE_PRIVATE void sqlite3CreateView(
93947  Parse *pParse,     /* The parsing context */
93948  Token *pBegin,     /* The CREATE token that begins the statement */
93949  Token *pName1,     /* The token that holds the name of the view */
93950  Token *pName2,     /* The token that holds the name of the view */
93951  Select *pSelect,   /* A SELECT statement that will become the new view */
93952  int isTemp,        /* TRUE for a TEMPORARY view */
93953  int noErr          /* Suppress error messages if VIEW already exists */
93954){
93955  Table *p;
93956  int n;
93957  const char *z;
93958  Token sEnd;
93959  DbFixer sFix;
93960  Token *pName = 0;
93961  int iDb;
93962  sqlite3 *db = pParse->db;
93963
93964  if( pParse->nVar>0 ){
93965    sqlite3ErrorMsg(pParse, "parameters are not allowed in views");
93966    sqlite3SelectDelete(db, pSelect);
93967    return;
93968  }
93969  sqlite3StartTable(pParse, pName1, pName2, isTemp, 1, 0, noErr);
93970  p = pParse->pNewTable;
93971  if( p==0 || pParse->nErr ){
93972    sqlite3SelectDelete(db, pSelect);
93973    return;
93974  }
93975  sqlite3TwoPartName(pParse, pName1, pName2, &pName);
93976  iDb = sqlite3SchemaToIndex(db, p->pSchema);
93977  sqlite3FixInit(&sFix, pParse, iDb, "view", pName);
93978  if( sqlite3FixSelect(&sFix, pSelect) ){
93979    sqlite3SelectDelete(db, pSelect);
93980    return;
93981  }
93982
93983  /* Make a copy of the entire SELECT statement that defines the view.
93984  ** This will force all the Expr.token.z values to be dynamically
93985  ** allocated rather than point to the input string - which means that
93986  ** they will persist after the current sqlite3_exec() call returns.
93987  */
93988  p->pSelect = sqlite3SelectDup(db, pSelect, EXPRDUP_REDUCE);
93989  sqlite3SelectDelete(db, pSelect);
93990  if( db->mallocFailed ){
93991    return;
93992  }
93993  if( !db->init.busy ){
93994    sqlite3ViewGetColumnNames(pParse, p);
93995  }
93996
93997  /* Locate the end of the CREATE VIEW statement.  Make sEnd point to
93998  ** the end.
93999  */
94000  sEnd = pParse->sLastToken;
94001  if( ALWAYS(sEnd.z[0]!=0) && sEnd.z[0]!=';' ){
94002    sEnd.z += sEnd.n;
94003  }
94004  sEnd.n = 0;
94005  n = (int)(sEnd.z - pBegin->z);
94006  z = pBegin->z;
94007  while( ALWAYS(n>0) && sqlite3Isspace(z[n-1]) ){ n--; }
94008  sEnd.z = &z[n-1];
94009  sEnd.n = 1;
94010
94011  /* Use sqlite3EndTable() to add the view to the SQLITE_MASTER table */
94012  sqlite3EndTable(pParse, 0, &sEnd, 0, 0);
94013  return;
94014}
94015#endif /* SQLITE_OMIT_VIEW */
94016
94017#if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_VIRTUALTABLE)
94018/*
94019** The Table structure pTable is really a VIEW.  Fill in the names of
94020** the columns of the view in the pTable structure.  Return the number
94021** of errors.  If an error is seen leave an error message in pParse->zErrMsg.
94022*/
94023SQLITE_PRIVATE int sqlite3ViewGetColumnNames(Parse *pParse, Table *pTable){
94024  Table *pSelTab;   /* A fake table from which we get the result set */
94025  Select *pSel;     /* Copy of the SELECT that implements the view */
94026  int nErr = 0;     /* Number of errors encountered */
94027  int n;            /* Temporarily holds the number of cursors assigned */
94028  sqlite3 *db = pParse->db;  /* Database connection for malloc errors */
94029  sqlite3_xauth xAuth;       /* Saved xAuth pointer */
94030
94031  assert( pTable );
94032
94033#ifndef SQLITE_OMIT_VIRTUALTABLE
94034  if( sqlite3VtabCallConnect(pParse, pTable) ){
94035    return SQLITE_ERROR;
94036  }
94037  if( IsVirtual(pTable) ) return 0;
94038#endif
94039
94040#ifndef SQLITE_OMIT_VIEW
94041  /* A positive nCol means the columns names for this view are
94042  ** already known.
94043  */
94044  if( pTable->nCol>0 ) return 0;
94045
94046  /* A negative nCol is a special marker meaning that we are currently
94047  ** trying to compute the column names.  If we enter this routine with
94048  ** a negative nCol, it means two or more views form a loop, like this:
94049  **
94050  **     CREATE VIEW one AS SELECT * FROM two;
94051  **     CREATE VIEW two AS SELECT * FROM one;
94052  **
94053  ** Actually, the error above is now caught prior to reaching this point.
94054  ** But the following test is still important as it does come up
94055  ** in the following:
94056  **
94057  **     CREATE TABLE main.ex1(a);
94058  **     CREATE TEMP VIEW ex1 AS SELECT a FROM ex1;
94059  **     SELECT * FROM temp.ex1;
94060  */
94061  if( pTable->nCol<0 ){
94062    sqlite3ErrorMsg(pParse, "view %s is circularly defined", pTable->zName);
94063    return 1;
94064  }
94065  assert( pTable->nCol>=0 );
94066
94067  /* If we get this far, it means we need to compute the table names.
94068  ** Note that the call to sqlite3ResultSetOfSelect() will expand any
94069  ** "*" elements in the results set of the view and will assign cursors
94070  ** to the elements of the FROM clause.  But we do not want these changes
94071  ** to be permanent.  So the computation is done on a copy of the SELECT
94072  ** statement that defines the view.
94073  */
94074  assert( pTable->pSelect );
94075  pSel = sqlite3SelectDup(db, pTable->pSelect, 0);
94076  if( pSel ){
94077    u8 enableLookaside = db->lookaside.bEnabled;
94078    n = pParse->nTab;
94079    sqlite3SrcListAssignCursors(pParse, pSel->pSrc);
94080    pTable->nCol = -1;
94081    db->lookaside.bEnabled = 0;
94082#ifndef SQLITE_OMIT_AUTHORIZATION
94083    xAuth = db->xAuth;
94084    db->xAuth = 0;
94085    pSelTab = sqlite3ResultSetOfSelect(pParse, pSel);
94086    db->xAuth = xAuth;
94087#else
94088    pSelTab = sqlite3ResultSetOfSelect(pParse, pSel);
94089#endif
94090    db->lookaside.bEnabled = enableLookaside;
94091    pParse->nTab = n;
94092    if( pSelTab ){
94093      assert( pTable->aCol==0 );
94094      pTable->nCol = pSelTab->nCol;
94095      pTable->aCol = pSelTab->aCol;
94096      pSelTab->nCol = 0;
94097      pSelTab->aCol = 0;
94098      sqlite3DeleteTable(db, pSelTab);
94099      assert( sqlite3SchemaMutexHeld(db, 0, pTable->pSchema) );
94100      pTable->pSchema->schemaFlags |= DB_UnresetViews;
94101    }else{
94102      pTable->nCol = 0;
94103      nErr++;
94104    }
94105    sqlite3SelectDelete(db, pSel);
94106  } else {
94107    nErr++;
94108  }
94109#endif /* SQLITE_OMIT_VIEW */
94110  return nErr;
94111}
94112#endif /* !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_VIRTUALTABLE) */
94113
94114#ifndef SQLITE_OMIT_VIEW
94115/*
94116** Clear the column names from every VIEW in database idx.
94117*/
94118static void sqliteViewResetAll(sqlite3 *db, int idx){
94119  HashElem *i;
94120  assert( sqlite3SchemaMutexHeld(db, idx, 0) );
94121  if( !DbHasProperty(db, idx, DB_UnresetViews) ) return;
94122  for(i=sqliteHashFirst(&db->aDb[idx].pSchema->tblHash); i;i=sqliteHashNext(i)){
94123    Table *pTab = sqliteHashData(i);
94124    if( pTab->pSelect ){
94125      sqliteDeleteColumnNames(db, pTab);
94126      pTab->aCol = 0;
94127      pTab->nCol = 0;
94128    }
94129  }
94130  DbClearProperty(db, idx, DB_UnresetViews);
94131}
94132#else
94133# define sqliteViewResetAll(A,B)
94134#endif /* SQLITE_OMIT_VIEW */
94135
94136/*
94137** This function is called by the VDBE to adjust the internal schema
94138** used by SQLite when the btree layer moves a table root page. The
94139** root-page of a table or index in database iDb has changed from iFrom
94140** to iTo.
94141**
94142** Ticket #1728:  The symbol table might still contain information
94143** on tables and/or indices that are the process of being deleted.
94144** If you are unlucky, one of those deleted indices or tables might
94145** have the same rootpage number as the real table or index that is
94146** being moved.  So we cannot stop searching after the first match
94147** because the first match might be for one of the deleted indices
94148** or tables and not the table/index that is actually being moved.
94149** We must continue looping until all tables and indices with
94150** rootpage==iFrom have been converted to have a rootpage of iTo
94151** in order to be certain that we got the right one.
94152*/
94153#ifndef SQLITE_OMIT_AUTOVACUUM
94154SQLITE_PRIVATE void sqlite3RootPageMoved(sqlite3 *db, int iDb, int iFrom, int iTo){
94155  HashElem *pElem;
94156  Hash *pHash;
94157  Db *pDb;
94158
94159  assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
94160  pDb = &db->aDb[iDb];
94161  pHash = &pDb->pSchema->tblHash;
94162  for(pElem=sqliteHashFirst(pHash); pElem; pElem=sqliteHashNext(pElem)){
94163    Table *pTab = sqliteHashData(pElem);
94164    if( pTab->tnum==iFrom ){
94165      pTab->tnum = iTo;
94166    }
94167  }
94168  pHash = &pDb->pSchema->idxHash;
94169  for(pElem=sqliteHashFirst(pHash); pElem; pElem=sqliteHashNext(pElem)){
94170    Index *pIdx = sqliteHashData(pElem);
94171    if( pIdx->tnum==iFrom ){
94172      pIdx->tnum = iTo;
94173    }
94174  }
94175}
94176#endif
94177
94178/*
94179** Write code to erase the table with root-page iTable from database iDb.
94180** Also write code to modify the sqlite_master table and internal schema
94181** if a root-page of another table is moved by the btree-layer whilst
94182** erasing iTable (this can happen with an auto-vacuum database).
94183*/
94184static void destroyRootPage(Parse *pParse, int iTable, int iDb){
94185  Vdbe *v = sqlite3GetVdbe(pParse);
94186  int r1 = sqlite3GetTempReg(pParse);
94187  sqlite3VdbeAddOp3(v, OP_Destroy, iTable, r1, iDb);
94188  sqlite3MayAbort(pParse);
94189#ifndef SQLITE_OMIT_AUTOVACUUM
94190  /* OP_Destroy stores an in integer r1. If this integer
94191  ** is non-zero, then it is the root page number of a table moved to
94192  ** location iTable. The following code modifies the sqlite_master table to
94193  ** reflect this.
94194  **
94195  ** The "#NNN" in the SQL is a special constant that means whatever value
94196  ** is in register NNN.  See grammar rules associated with the TK_REGISTER
94197  ** token for additional information.
94198  */
94199  sqlite3NestedParse(pParse,
94200     "UPDATE %Q.%s SET rootpage=%d WHERE #%d AND rootpage=#%d",
94201     pParse->db->aDb[iDb].zName, SCHEMA_TABLE(iDb), iTable, r1, r1);
94202#endif
94203  sqlite3ReleaseTempReg(pParse, r1);
94204}
94205
94206/*
94207** Write VDBE code to erase table pTab and all associated indices on disk.
94208** Code to update the sqlite_master tables and internal schema definitions
94209** in case a root-page belonging to another table is moved by the btree layer
94210** is also added (this can happen with an auto-vacuum database).
94211*/
94212static void destroyTable(Parse *pParse, Table *pTab){
94213#ifdef SQLITE_OMIT_AUTOVACUUM
94214  Index *pIdx;
94215  int iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
94216  destroyRootPage(pParse, pTab->tnum, iDb);
94217  for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
94218    destroyRootPage(pParse, pIdx->tnum, iDb);
94219  }
94220#else
94221  /* If the database may be auto-vacuum capable (if SQLITE_OMIT_AUTOVACUUM
94222  ** is not defined), then it is important to call OP_Destroy on the
94223  ** table and index root-pages in order, starting with the numerically
94224  ** largest root-page number. This guarantees that none of the root-pages
94225  ** to be destroyed is relocated by an earlier OP_Destroy. i.e. if the
94226  ** following were coded:
94227  **
94228  ** OP_Destroy 4 0
94229  ** ...
94230  ** OP_Destroy 5 0
94231  **
94232  ** and root page 5 happened to be the largest root-page number in the
94233  ** database, then root page 5 would be moved to page 4 by the
94234  ** "OP_Destroy 4 0" opcode. The subsequent "OP_Destroy 5 0" would hit
94235  ** a free-list page.
94236  */
94237  int iTab = pTab->tnum;
94238  int iDestroyed = 0;
94239
94240  while( 1 ){
94241    Index *pIdx;
94242    int iLargest = 0;
94243
94244    if( iDestroyed==0 || iTab<iDestroyed ){
94245      iLargest = iTab;
94246    }
94247    for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
94248      int iIdx = pIdx->tnum;
94249      assert( pIdx->pSchema==pTab->pSchema );
94250      if( (iDestroyed==0 || (iIdx<iDestroyed)) && iIdx>iLargest ){
94251        iLargest = iIdx;
94252      }
94253    }
94254    if( iLargest==0 ){
94255      return;
94256    }else{
94257      int iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
94258      assert( iDb>=0 && iDb<pParse->db->nDb );
94259      destroyRootPage(pParse, iLargest, iDb);
94260      iDestroyed = iLargest;
94261    }
94262  }
94263#endif
94264}
94265
94266/*
94267** Remove entries from the sqlite_statN tables (for N in (1,2,3))
94268** after a DROP INDEX or DROP TABLE command.
94269*/
94270static void sqlite3ClearStatTables(
94271  Parse *pParse,         /* The parsing context */
94272  int iDb,               /* The database number */
94273  const char *zType,     /* "idx" or "tbl" */
94274  const char *zName      /* Name of index or table */
94275){
94276  int i;
94277  const char *zDbName = pParse->db->aDb[iDb].zName;
94278  for(i=1; i<=4; i++){
94279    char zTab[24];
94280    sqlite3_snprintf(sizeof(zTab),zTab,"sqlite_stat%d",i);
94281    if( sqlite3FindTable(pParse->db, zTab, zDbName) ){
94282      sqlite3NestedParse(pParse,
94283        "DELETE FROM %Q.%s WHERE %s=%Q",
94284        zDbName, zTab, zType, zName
94285      );
94286    }
94287  }
94288}
94289
94290/*
94291** Generate code to drop a table.
94292*/
94293SQLITE_PRIVATE void sqlite3CodeDropTable(Parse *pParse, Table *pTab, int iDb, int isView){
94294  Vdbe *v;
94295  sqlite3 *db = pParse->db;
94296  Trigger *pTrigger;
94297  Db *pDb = &db->aDb[iDb];
94298
94299  v = sqlite3GetVdbe(pParse);
94300  assert( v!=0 );
94301  sqlite3BeginWriteOperation(pParse, 1, iDb);
94302
94303#ifndef SQLITE_OMIT_VIRTUALTABLE
94304  if( IsVirtual(pTab) ){
94305    sqlite3VdbeAddOp0(v, OP_VBegin);
94306  }
94307#endif
94308
94309  /* Drop all triggers associated with the table being dropped. Code
94310  ** is generated to remove entries from sqlite_master and/or
94311  ** sqlite_temp_master if required.
94312  */
94313  pTrigger = sqlite3TriggerList(pParse, pTab);
94314  while( pTrigger ){
94315    assert( pTrigger->pSchema==pTab->pSchema ||
94316        pTrigger->pSchema==db->aDb[1].pSchema );
94317    sqlite3DropTriggerPtr(pParse, pTrigger);
94318    pTrigger = pTrigger->pNext;
94319  }
94320
94321#ifndef SQLITE_OMIT_AUTOINCREMENT
94322  /* Remove any entries of the sqlite_sequence table associated with
94323  ** the table being dropped. This is done before the table is dropped
94324  ** at the btree level, in case the sqlite_sequence table needs to
94325  ** move as a result of the drop (can happen in auto-vacuum mode).
94326  */
94327  if( pTab->tabFlags & TF_Autoincrement ){
94328    sqlite3NestedParse(pParse,
94329      "DELETE FROM %Q.sqlite_sequence WHERE name=%Q",
94330      pDb->zName, pTab->zName
94331    );
94332  }
94333#endif
94334
94335  /* Drop all SQLITE_MASTER table and index entries that refer to the
94336  ** table. The program name loops through the master table and deletes
94337  ** every row that refers to a table of the same name as the one being
94338  ** dropped. Triggers are handled separately because a trigger can be
94339  ** created in the temp database that refers to a table in another
94340  ** database.
94341  */
94342  sqlite3NestedParse(pParse,
94343      "DELETE FROM %Q.%s WHERE tbl_name=%Q and type!='trigger'",
94344      pDb->zName, SCHEMA_TABLE(iDb), pTab->zName);
94345  if( !isView && !IsVirtual(pTab) ){
94346    destroyTable(pParse, pTab);
94347  }
94348
94349  /* Remove the table entry from SQLite's internal schema and modify
94350  ** the schema cookie.
94351  */
94352  if( IsVirtual(pTab) ){
94353    sqlite3VdbeAddOp4(v, OP_VDestroy, iDb, 0, 0, pTab->zName, 0);
94354  }
94355  sqlite3VdbeAddOp4(v, OP_DropTable, iDb, 0, 0, pTab->zName, 0);
94356  sqlite3ChangeCookie(pParse, iDb);
94357  sqliteViewResetAll(db, iDb);
94358}
94359
94360/*
94361** This routine is called to do the work of a DROP TABLE statement.
94362** pName is the name of the table to be dropped.
94363*/
94364SQLITE_PRIVATE void sqlite3DropTable(Parse *pParse, SrcList *pName, int isView, int noErr){
94365  Table *pTab;
94366  Vdbe *v;
94367  sqlite3 *db = pParse->db;
94368  int iDb;
94369
94370  if( db->mallocFailed ){
94371    goto exit_drop_table;
94372  }
94373  assert( pParse->nErr==0 );
94374  assert( pName->nSrc==1 );
94375  if( sqlite3ReadSchema(pParse) ) goto exit_drop_table;
94376  if( noErr ) db->suppressErr++;
94377  pTab = sqlite3LocateTableItem(pParse, isView, &pName->a[0]);
94378  if( noErr ) db->suppressErr--;
94379
94380  if( pTab==0 ){
94381    if( noErr ) sqlite3CodeVerifyNamedSchema(pParse, pName->a[0].zDatabase);
94382    goto exit_drop_table;
94383  }
94384  iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
94385  assert( iDb>=0 && iDb<db->nDb );
94386
94387  /* If pTab is a virtual table, call ViewGetColumnNames() to ensure
94388  ** it is initialized.
94389  */
94390  if( IsVirtual(pTab) && sqlite3ViewGetColumnNames(pParse, pTab) ){
94391    goto exit_drop_table;
94392  }
94393#ifndef SQLITE_OMIT_AUTHORIZATION
94394  {
94395    int code;
94396    const char *zTab = SCHEMA_TABLE(iDb);
94397    const char *zDb = db->aDb[iDb].zName;
94398    const char *zArg2 = 0;
94399    if( sqlite3AuthCheck(pParse, SQLITE_DELETE, zTab, 0, zDb)){
94400      goto exit_drop_table;
94401    }
94402    if( isView ){
94403      if( !OMIT_TEMPDB && iDb==1 ){
94404        code = SQLITE_DROP_TEMP_VIEW;
94405      }else{
94406        code = SQLITE_DROP_VIEW;
94407      }
94408#ifndef SQLITE_OMIT_VIRTUALTABLE
94409    }else if( IsVirtual(pTab) ){
94410      code = SQLITE_DROP_VTABLE;
94411      zArg2 = sqlite3GetVTable(db, pTab)->pMod->zName;
94412#endif
94413    }else{
94414      if( !OMIT_TEMPDB && iDb==1 ){
94415        code = SQLITE_DROP_TEMP_TABLE;
94416      }else{
94417        code = SQLITE_DROP_TABLE;
94418      }
94419    }
94420    if( sqlite3AuthCheck(pParse, code, pTab->zName, zArg2, zDb) ){
94421      goto exit_drop_table;
94422    }
94423    if( sqlite3AuthCheck(pParse, SQLITE_DELETE, pTab->zName, 0, zDb) ){
94424      goto exit_drop_table;
94425    }
94426  }
94427#endif
94428  if( sqlite3StrNICmp(pTab->zName, "sqlite_", 7)==0
94429    && sqlite3StrNICmp(pTab->zName, "sqlite_stat", 11)!=0 ){
94430    sqlite3ErrorMsg(pParse, "table %s may not be dropped", pTab->zName);
94431    goto exit_drop_table;
94432  }
94433
94434#ifndef SQLITE_OMIT_VIEW
94435  /* Ensure DROP TABLE is not used on a view, and DROP VIEW is not used
94436  ** on a table.
94437  */
94438  if( isView && pTab->pSelect==0 ){
94439    sqlite3ErrorMsg(pParse, "use DROP TABLE to delete table %s", pTab->zName);
94440    goto exit_drop_table;
94441  }
94442  if( !isView && pTab->pSelect ){
94443    sqlite3ErrorMsg(pParse, "use DROP VIEW to delete view %s", pTab->zName);
94444    goto exit_drop_table;
94445  }
94446#endif
94447
94448  /* Generate code to remove the table from the master table
94449  ** on disk.
94450  */
94451  v = sqlite3GetVdbe(pParse);
94452  if( v ){
94453    sqlite3BeginWriteOperation(pParse, 1, iDb);
94454    sqlite3ClearStatTables(pParse, iDb, "tbl", pTab->zName);
94455    sqlite3FkDropTable(pParse, pName, pTab);
94456    sqlite3CodeDropTable(pParse, pTab, iDb, isView);
94457  }
94458
94459exit_drop_table:
94460  sqlite3SrcListDelete(db, pName);
94461}
94462
94463/*
94464** This routine is called to create a new foreign key on the table
94465** currently under construction.  pFromCol determines which columns
94466** in the current table point to the foreign key.  If pFromCol==0 then
94467** connect the key to the last column inserted.  pTo is the name of
94468** the table referred to (a.k.a the "parent" table).  pToCol is a list
94469** of tables in the parent pTo table.  flags contains all
94470** information about the conflict resolution algorithms specified
94471** in the ON DELETE, ON UPDATE and ON INSERT clauses.
94472**
94473** An FKey structure is created and added to the table currently
94474** under construction in the pParse->pNewTable field.
94475**
94476** The foreign key is set for IMMEDIATE processing.  A subsequent call
94477** to sqlite3DeferForeignKey() might change this to DEFERRED.
94478*/
94479SQLITE_PRIVATE void sqlite3CreateForeignKey(
94480  Parse *pParse,       /* Parsing context */
94481  ExprList *pFromCol,  /* Columns in this table that point to other table */
94482  Token *pTo,          /* Name of the other table */
94483  ExprList *pToCol,    /* Columns in the other table */
94484  int flags            /* Conflict resolution algorithms. */
94485){
94486  sqlite3 *db = pParse->db;
94487#ifndef SQLITE_OMIT_FOREIGN_KEY
94488  FKey *pFKey = 0;
94489  FKey *pNextTo;
94490  Table *p = pParse->pNewTable;
94491  int nByte;
94492  int i;
94493  int nCol;
94494  char *z;
94495
94496  assert( pTo!=0 );
94497  if( p==0 || IN_DECLARE_VTAB ) goto fk_end;
94498  if( pFromCol==0 ){
94499    int iCol = p->nCol-1;
94500    if( NEVER(iCol<0) ) goto fk_end;
94501    if( pToCol && pToCol->nExpr!=1 ){
94502      sqlite3ErrorMsg(pParse, "foreign key on %s"
94503         " should reference only one column of table %T",
94504         p->aCol[iCol].zName, pTo);
94505      goto fk_end;
94506    }
94507    nCol = 1;
94508  }else if( pToCol && pToCol->nExpr!=pFromCol->nExpr ){
94509    sqlite3ErrorMsg(pParse,
94510        "number of columns in foreign key does not match the number of "
94511        "columns in the referenced table");
94512    goto fk_end;
94513  }else{
94514    nCol = pFromCol->nExpr;
94515  }
94516  nByte = sizeof(*pFKey) + (nCol-1)*sizeof(pFKey->aCol[0]) + pTo->n + 1;
94517  if( pToCol ){
94518    for(i=0; i<pToCol->nExpr; i++){
94519      nByte += sqlite3Strlen30(pToCol->a[i].zName) + 1;
94520    }
94521  }
94522  pFKey = sqlite3DbMallocZero(db, nByte );
94523  if( pFKey==0 ){
94524    goto fk_end;
94525  }
94526  pFKey->pFrom = p;
94527  pFKey->pNextFrom = p->pFKey;
94528  z = (char*)&pFKey->aCol[nCol];
94529  pFKey->zTo = z;
94530  memcpy(z, pTo->z, pTo->n);
94531  z[pTo->n] = 0;
94532  sqlite3Dequote(z);
94533  z += pTo->n+1;
94534  pFKey->nCol = nCol;
94535  if( pFromCol==0 ){
94536    pFKey->aCol[0].iFrom = p->nCol-1;
94537  }else{
94538    for(i=0; i<nCol; i++){
94539      int j;
94540      for(j=0; j<p->nCol; j++){
94541        if( sqlite3StrICmp(p->aCol[j].zName, pFromCol->a[i].zName)==0 ){
94542          pFKey->aCol[i].iFrom = j;
94543          break;
94544        }
94545      }
94546      if( j>=p->nCol ){
94547        sqlite3ErrorMsg(pParse,
94548          "unknown column \"%s\" in foreign key definition",
94549          pFromCol->a[i].zName);
94550        goto fk_end;
94551      }
94552    }
94553  }
94554  if( pToCol ){
94555    for(i=0; i<nCol; i++){
94556      int n = sqlite3Strlen30(pToCol->a[i].zName);
94557      pFKey->aCol[i].zCol = z;
94558      memcpy(z, pToCol->a[i].zName, n);
94559      z[n] = 0;
94560      z += n+1;
94561    }
94562  }
94563  pFKey->isDeferred = 0;
94564  pFKey->aAction[0] = (u8)(flags & 0xff);            /* ON DELETE action */
94565  pFKey->aAction[1] = (u8)((flags >> 8 ) & 0xff);    /* ON UPDATE action */
94566
94567  assert( sqlite3SchemaMutexHeld(db, 0, p->pSchema) );
94568  pNextTo = (FKey *)sqlite3HashInsert(&p->pSchema->fkeyHash,
94569      pFKey->zTo, (void *)pFKey
94570  );
94571  if( pNextTo==pFKey ){
94572    db->mallocFailed = 1;
94573    goto fk_end;
94574  }
94575  if( pNextTo ){
94576    assert( pNextTo->pPrevTo==0 );
94577    pFKey->pNextTo = pNextTo;
94578    pNextTo->pPrevTo = pFKey;
94579  }
94580
94581  /* Link the foreign key to the table as the last step.
94582  */
94583  p->pFKey = pFKey;
94584  pFKey = 0;
94585
94586fk_end:
94587  sqlite3DbFree(db, pFKey);
94588#endif /* !defined(SQLITE_OMIT_FOREIGN_KEY) */
94589  sqlite3ExprListDelete(db, pFromCol);
94590  sqlite3ExprListDelete(db, pToCol);
94591}
94592
94593/*
94594** This routine is called when an INITIALLY IMMEDIATE or INITIALLY DEFERRED
94595** clause is seen as part of a foreign key definition.  The isDeferred
94596** parameter is 1 for INITIALLY DEFERRED and 0 for INITIALLY IMMEDIATE.
94597** The behavior of the most recently created foreign key is adjusted
94598** accordingly.
94599*/
94600SQLITE_PRIVATE void sqlite3DeferForeignKey(Parse *pParse, int isDeferred){
94601#ifndef SQLITE_OMIT_FOREIGN_KEY
94602  Table *pTab;
94603  FKey *pFKey;
94604  if( (pTab = pParse->pNewTable)==0 || (pFKey = pTab->pFKey)==0 ) return;
94605  assert( isDeferred==0 || isDeferred==1 ); /* EV: R-30323-21917 */
94606  pFKey->isDeferred = (u8)isDeferred;
94607#endif
94608}
94609
94610/*
94611** Generate code that will erase and refill index *pIdx.  This is
94612** used to initialize a newly created index or to recompute the
94613** content of an index in response to a REINDEX command.
94614**
94615** if memRootPage is not negative, it means that the index is newly
94616** created.  The register specified by memRootPage contains the
94617** root page number of the index.  If memRootPage is negative, then
94618** the index already exists and must be cleared before being refilled and
94619** the root page number of the index is taken from pIndex->tnum.
94620*/
94621static void sqlite3RefillIndex(Parse *pParse, Index *pIndex, int memRootPage){
94622  Table *pTab = pIndex->pTable;  /* The table that is indexed */
94623  int iTab = pParse->nTab++;     /* Btree cursor used for pTab */
94624  int iIdx = pParse->nTab++;     /* Btree cursor used for pIndex */
94625  int iSorter;                   /* Cursor opened by OpenSorter (if in use) */
94626  int addr1;                     /* Address of top of loop */
94627  int addr2;                     /* Address to jump to for next iteration */
94628  int tnum;                      /* Root page of index */
94629  int iPartIdxLabel;             /* Jump to this label to skip a row */
94630  Vdbe *v;                       /* Generate code into this virtual machine */
94631  KeyInfo *pKey;                 /* KeyInfo for index */
94632  int regRecord;                 /* Register holding assembled index record */
94633  sqlite3 *db = pParse->db;      /* The database connection */
94634  int iDb = sqlite3SchemaToIndex(db, pIndex->pSchema);
94635
94636#ifndef SQLITE_OMIT_AUTHORIZATION
94637  if( sqlite3AuthCheck(pParse, SQLITE_REINDEX, pIndex->zName, 0,
94638      db->aDb[iDb].zName ) ){
94639    return;
94640  }
94641#endif
94642
94643  /* Require a write-lock on the table to perform this operation */
94644  sqlite3TableLock(pParse, iDb, pTab->tnum, 1, pTab->zName);
94645
94646  v = sqlite3GetVdbe(pParse);
94647  if( v==0 ) return;
94648  if( memRootPage>=0 ){
94649    tnum = memRootPage;
94650  }else{
94651    tnum = pIndex->tnum;
94652  }
94653  pKey = sqlite3KeyInfoOfIndex(pParse, pIndex);
94654
94655  /* Open the sorter cursor if we are to use one. */
94656  iSorter = pParse->nTab++;
94657  sqlite3VdbeAddOp4(v, OP_SorterOpen, iSorter, 0, pIndex->nKeyCol, (char*)
94658                    sqlite3KeyInfoRef(pKey), P4_KEYINFO);
94659
94660  /* Open the table. Loop through all rows of the table, inserting index
94661  ** records into the sorter. */
94662  sqlite3OpenTable(pParse, iTab, iDb, pTab, OP_OpenRead);
94663  addr1 = sqlite3VdbeAddOp2(v, OP_Rewind, iTab, 0); VdbeCoverage(v);
94664  regRecord = sqlite3GetTempReg(pParse);
94665
94666  sqlite3GenerateIndexKey(pParse,pIndex,iTab,regRecord,0,&iPartIdxLabel,0,0);
94667  sqlite3VdbeAddOp2(v, OP_SorterInsert, iSorter, regRecord);
94668  sqlite3ResolvePartIdxLabel(pParse, iPartIdxLabel);
94669  sqlite3VdbeAddOp2(v, OP_Next, iTab, addr1+1); VdbeCoverage(v);
94670  sqlite3VdbeJumpHere(v, addr1);
94671  if( memRootPage<0 ) sqlite3VdbeAddOp2(v, OP_Clear, tnum, iDb);
94672  sqlite3VdbeAddOp4(v, OP_OpenWrite, iIdx, tnum, iDb,
94673                    (char *)pKey, P4_KEYINFO);
94674  sqlite3VdbeChangeP5(v, OPFLAG_BULKCSR|((memRootPage>=0)?OPFLAG_P2ISREG:0));
94675
94676  addr1 = sqlite3VdbeAddOp2(v, OP_SorterSort, iSorter, 0); VdbeCoverage(v);
94677  assert( pKey!=0 || db->mallocFailed || pParse->nErr );
94678  if( IsUniqueIndex(pIndex) && pKey!=0 ){
94679    int j2 = sqlite3VdbeCurrentAddr(v) + 3;
94680    sqlite3VdbeAddOp2(v, OP_Goto, 0, j2);
94681    addr2 = sqlite3VdbeCurrentAddr(v);
94682    sqlite3VdbeAddOp4Int(v, OP_SorterCompare, iSorter, j2, regRecord,
94683                         pIndex->nKeyCol); VdbeCoverage(v);
94684    sqlite3UniqueConstraint(pParse, OE_Abort, pIndex);
94685  }else{
94686    addr2 = sqlite3VdbeCurrentAddr(v);
94687  }
94688  sqlite3VdbeAddOp3(v, OP_SorterData, iSorter, regRecord, iIdx);
94689  sqlite3VdbeAddOp3(v, OP_Last, iIdx, 0, -1);
94690  sqlite3VdbeAddOp3(v, OP_IdxInsert, iIdx, regRecord, 0);
94691  sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT);
94692  sqlite3ReleaseTempReg(pParse, regRecord);
94693  sqlite3VdbeAddOp2(v, OP_SorterNext, iSorter, addr2); VdbeCoverage(v);
94694  sqlite3VdbeJumpHere(v, addr1);
94695
94696  sqlite3VdbeAddOp1(v, OP_Close, iTab);
94697  sqlite3VdbeAddOp1(v, OP_Close, iIdx);
94698  sqlite3VdbeAddOp1(v, OP_Close, iSorter);
94699}
94700
94701/*
94702** Allocate heap space to hold an Index object with nCol columns.
94703**
94704** Increase the allocation size to provide an extra nExtra bytes
94705** of 8-byte aligned space after the Index object and return a
94706** pointer to this extra space in *ppExtra.
94707*/
94708SQLITE_PRIVATE Index *sqlite3AllocateIndexObject(
94709  sqlite3 *db,         /* Database connection */
94710  i16 nCol,            /* Total number of columns in the index */
94711  int nExtra,          /* Number of bytes of extra space to alloc */
94712  char **ppExtra       /* Pointer to the "extra" space */
94713){
94714  Index *p;            /* Allocated index object */
94715  int nByte;           /* Bytes of space for Index object + arrays */
94716
94717  nByte = ROUND8(sizeof(Index)) +              /* Index structure  */
94718          ROUND8(sizeof(char*)*nCol) +         /* Index.azColl     */
94719          ROUND8(sizeof(LogEst)*(nCol+1) +     /* Index.aiRowLogEst   */
94720                 sizeof(i16)*nCol +            /* Index.aiColumn   */
94721                 sizeof(u8)*nCol);             /* Index.aSortOrder */
94722  p = sqlite3DbMallocZero(db, nByte + nExtra);
94723  if( p ){
94724    char *pExtra = ((char*)p)+ROUND8(sizeof(Index));
94725    p->azColl = (char**)pExtra;       pExtra += ROUND8(sizeof(char*)*nCol);
94726    p->aiRowLogEst = (LogEst*)pExtra; pExtra += sizeof(LogEst)*(nCol+1);
94727    p->aiColumn = (i16*)pExtra;       pExtra += sizeof(i16)*nCol;
94728    p->aSortOrder = (u8*)pExtra;
94729    p->nColumn = nCol;
94730    p->nKeyCol = nCol - 1;
94731    *ppExtra = ((char*)p) + nByte;
94732  }
94733  return p;
94734}
94735
94736/*
94737** Create a new index for an SQL table.  pName1.pName2 is the name of the index
94738** and pTblList is the name of the table that is to be indexed.  Both will
94739** be NULL for a primary key or an index that is created to satisfy a
94740** UNIQUE constraint.  If pTable and pIndex are NULL, use pParse->pNewTable
94741** as the table to be indexed.  pParse->pNewTable is a table that is
94742** currently being constructed by a CREATE TABLE statement.
94743**
94744** pList is a list of columns to be indexed.  pList will be NULL if this
94745** is a primary key or unique-constraint on the most recent column added
94746** to the table currently under construction.
94747**
94748** If the index is created successfully, return a pointer to the new Index
94749** structure. This is used by sqlite3AddPrimaryKey() to mark the index
94750** as the tables primary key (Index.idxType==SQLITE_IDXTYPE_PRIMARYKEY)
94751*/
94752SQLITE_PRIVATE Index *sqlite3CreateIndex(
94753  Parse *pParse,     /* All information about this parse */
94754  Token *pName1,     /* First part of index name. May be NULL */
94755  Token *pName2,     /* Second part of index name. May be NULL */
94756  SrcList *pTblName, /* Table to index. Use pParse->pNewTable if 0 */
94757  ExprList *pList,   /* A list of columns to be indexed */
94758  int onError,       /* OE_Abort, OE_Ignore, OE_Replace, or OE_None */
94759  Token *pStart,     /* The CREATE token that begins this statement */
94760  Expr *pPIWhere,    /* WHERE clause for partial indices */
94761  int sortOrder,     /* Sort order of primary key when pList==NULL */
94762  int ifNotExist     /* Omit error if index already exists */
94763){
94764  Index *pRet = 0;     /* Pointer to return */
94765  Table *pTab = 0;     /* Table to be indexed */
94766  Index *pIndex = 0;   /* The index to be created */
94767  char *zName = 0;     /* Name of the index */
94768  int nName;           /* Number of characters in zName */
94769  int i, j;
94770  DbFixer sFix;        /* For assigning database names to pTable */
94771  int sortOrderMask;   /* 1 to honor DESC in index.  0 to ignore. */
94772  sqlite3 *db = pParse->db;
94773  Db *pDb;             /* The specific table containing the indexed database */
94774  int iDb;             /* Index of the database that is being written */
94775  Token *pName = 0;    /* Unqualified name of the index to create */
94776  struct ExprList_item *pListItem; /* For looping over pList */
94777  const Column *pTabCol;           /* A column in the table */
94778  int nExtra = 0;                  /* Space allocated for zExtra[] */
94779  int nExtraCol;                   /* Number of extra columns needed */
94780  char *zExtra = 0;                /* Extra space after the Index object */
94781  Index *pPk = 0;      /* PRIMARY KEY index for WITHOUT ROWID tables */
94782
94783  if( db->mallocFailed || IN_DECLARE_VTAB || pParse->nErr>0 ){
94784    goto exit_create_index;
94785  }
94786  if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){
94787    goto exit_create_index;
94788  }
94789
94790  /*
94791  ** Find the table that is to be indexed.  Return early if not found.
94792  */
94793  if( pTblName!=0 ){
94794
94795    /* Use the two-part index name to determine the database
94796    ** to search for the table. 'Fix' the table name to this db
94797    ** before looking up the table.
94798    */
94799    assert( pName1 && pName2 );
94800    iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pName);
94801    if( iDb<0 ) goto exit_create_index;
94802    assert( pName && pName->z );
94803
94804#ifndef SQLITE_OMIT_TEMPDB
94805    /* If the index name was unqualified, check if the table
94806    ** is a temp table. If so, set the database to 1. Do not do this
94807    ** if initialising a database schema.
94808    */
94809    if( !db->init.busy ){
94810      pTab = sqlite3SrcListLookup(pParse, pTblName);
94811      if( pName2->n==0 && pTab && pTab->pSchema==db->aDb[1].pSchema ){
94812        iDb = 1;
94813      }
94814    }
94815#endif
94816
94817    sqlite3FixInit(&sFix, pParse, iDb, "index", pName);
94818    if( sqlite3FixSrcList(&sFix, pTblName) ){
94819      /* Because the parser constructs pTblName from a single identifier,
94820      ** sqlite3FixSrcList can never fail. */
94821      assert(0);
94822    }
94823    pTab = sqlite3LocateTableItem(pParse, 0, &pTblName->a[0]);
94824    assert( db->mallocFailed==0 || pTab==0 );
94825    if( pTab==0 ) goto exit_create_index;
94826    if( iDb==1 && db->aDb[iDb].pSchema!=pTab->pSchema ){
94827      sqlite3ErrorMsg(pParse,
94828           "cannot create a TEMP index on non-TEMP table \"%s\"",
94829           pTab->zName);
94830      goto exit_create_index;
94831    }
94832    if( !HasRowid(pTab) ) pPk = sqlite3PrimaryKeyIndex(pTab);
94833  }else{
94834    assert( pName==0 );
94835    assert( pStart==0 );
94836    pTab = pParse->pNewTable;
94837    if( !pTab ) goto exit_create_index;
94838    iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
94839  }
94840  pDb = &db->aDb[iDb];
94841
94842  assert( pTab!=0 );
94843  assert( pParse->nErr==0 );
94844  if( sqlite3StrNICmp(pTab->zName, "sqlite_", 7)==0
94845       && db->init.busy==0
94846#if SQLITE_USER_AUTHENTICATION
94847       && sqlite3UserAuthTable(pTab->zName)==0
94848#endif
94849       && sqlite3StrNICmp(&pTab->zName[7],"altertab_",9)!=0 ){
94850    sqlite3ErrorMsg(pParse, "table %s may not be indexed", pTab->zName);
94851    goto exit_create_index;
94852  }
94853#ifndef SQLITE_OMIT_VIEW
94854  if( pTab->pSelect ){
94855    sqlite3ErrorMsg(pParse, "views may not be indexed");
94856    goto exit_create_index;
94857  }
94858#endif
94859#ifndef SQLITE_OMIT_VIRTUALTABLE
94860  if( IsVirtual(pTab) ){
94861    sqlite3ErrorMsg(pParse, "virtual tables may not be indexed");
94862    goto exit_create_index;
94863  }
94864#endif
94865
94866  /*
94867  ** Find the name of the index.  Make sure there is not already another
94868  ** index or table with the same name.
94869  **
94870  ** Exception:  If we are reading the names of permanent indices from the
94871  ** sqlite_master table (because some other process changed the schema) and
94872  ** one of the index names collides with the name of a temporary table or
94873  ** index, then we will continue to process this index.
94874  **
94875  ** If pName==0 it means that we are
94876  ** dealing with a primary key or UNIQUE constraint.  We have to invent our
94877  ** own name.
94878  */
94879  if( pName ){
94880    zName = sqlite3NameFromToken(db, pName);
94881    if( zName==0 ) goto exit_create_index;
94882    assert( pName->z!=0 );
94883    if( SQLITE_OK!=sqlite3CheckObjectName(pParse, zName) ){
94884      goto exit_create_index;
94885    }
94886    if( !db->init.busy ){
94887      if( sqlite3FindTable(db, zName, 0)!=0 ){
94888        sqlite3ErrorMsg(pParse, "there is already a table named %s", zName);
94889        goto exit_create_index;
94890      }
94891    }
94892    if( sqlite3FindIndex(db, zName, pDb->zName)!=0 ){
94893      if( !ifNotExist ){
94894        sqlite3ErrorMsg(pParse, "index %s already exists", zName);
94895      }else{
94896        assert( !db->init.busy );
94897        sqlite3CodeVerifySchema(pParse, iDb);
94898      }
94899      goto exit_create_index;
94900    }
94901  }else{
94902    int n;
94903    Index *pLoop;
94904    for(pLoop=pTab->pIndex, n=1; pLoop; pLoop=pLoop->pNext, n++){}
94905    zName = sqlite3MPrintf(db, "sqlite_autoindex_%s_%d", pTab->zName, n);
94906    if( zName==0 ){
94907      goto exit_create_index;
94908    }
94909  }
94910
94911  /* Check for authorization to create an index.
94912  */
94913#ifndef SQLITE_OMIT_AUTHORIZATION
94914  {
94915    const char *zDb = pDb->zName;
94916    if( sqlite3AuthCheck(pParse, SQLITE_INSERT, SCHEMA_TABLE(iDb), 0, zDb) ){
94917      goto exit_create_index;
94918    }
94919    i = SQLITE_CREATE_INDEX;
94920    if( !OMIT_TEMPDB && iDb==1 ) i = SQLITE_CREATE_TEMP_INDEX;
94921    if( sqlite3AuthCheck(pParse, i, zName, pTab->zName, zDb) ){
94922      goto exit_create_index;
94923    }
94924  }
94925#endif
94926
94927  /* If pList==0, it means this routine was called to make a primary
94928  ** key out of the last column added to the table under construction.
94929  ** So create a fake list to simulate this.
94930  */
94931  if( pList==0 ){
94932    pList = sqlite3ExprListAppend(pParse, 0, 0);
94933    if( pList==0 ) goto exit_create_index;
94934    pList->a[0].zName = sqlite3DbStrDup(pParse->db,
94935                                        pTab->aCol[pTab->nCol-1].zName);
94936    pList->a[0].sortOrder = (u8)sortOrder;
94937  }
94938
94939  /* Figure out how many bytes of space are required to store explicitly
94940  ** specified collation sequence names.
94941  */
94942  for(i=0; i<pList->nExpr; i++){
94943    Expr *pExpr = pList->a[i].pExpr;
94944    if( pExpr ){
94945      assert( pExpr->op==TK_COLLATE );
94946      nExtra += (1 + sqlite3Strlen30(pExpr->u.zToken));
94947    }
94948  }
94949
94950  /*
94951  ** Allocate the index structure.
94952  */
94953  nName = sqlite3Strlen30(zName);
94954  nExtraCol = pPk ? pPk->nKeyCol : 1;
94955  pIndex = sqlite3AllocateIndexObject(db, pList->nExpr + nExtraCol,
94956                                      nName + nExtra + 1, &zExtra);
94957  if( db->mallocFailed ){
94958    goto exit_create_index;
94959  }
94960  assert( EIGHT_BYTE_ALIGNMENT(pIndex->aiRowLogEst) );
94961  assert( EIGHT_BYTE_ALIGNMENT(pIndex->azColl) );
94962  pIndex->zName = zExtra;
94963  zExtra += nName + 1;
94964  memcpy(pIndex->zName, zName, nName+1);
94965  pIndex->pTable = pTab;
94966  pIndex->onError = (u8)onError;
94967  pIndex->uniqNotNull = onError!=OE_None;
94968  pIndex->idxType = pName ? SQLITE_IDXTYPE_APPDEF : SQLITE_IDXTYPE_UNIQUE;
94969  pIndex->pSchema = db->aDb[iDb].pSchema;
94970  pIndex->nKeyCol = pList->nExpr;
94971  if( pPIWhere ){
94972    sqlite3ResolveSelfReference(pParse, pTab, NC_PartIdx, pPIWhere, 0);
94973    pIndex->pPartIdxWhere = pPIWhere;
94974    pPIWhere = 0;
94975  }
94976  assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
94977
94978  /* Check to see if we should honor DESC requests on index columns
94979  */
94980  if( pDb->pSchema->file_format>=4 ){
94981    sortOrderMask = -1;   /* Honor DESC */
94982  }else{
94983    sortOrderMask = 0;    /* Ignore DESC */
94984  }
94985
94986  /* Scan the names of the columns of the table to be indexed and
94987  ** load the column indices into the Index structure.  Report an error
94988  ** if any column is not found.
94989  **
94990  ** TODO:  Add a test to make sure that the same column is not named
94991  ** more than once within the same index.  Only the first instance of
94992  ** the column will ever be used by the optimizer.  Note that using the
94993  ** same column more than once cannot be an error because that would
94994  ** break backwards compatibility - it needs to be a warning.
94995  */
94996  for(i=0, pListItem=pList->a; i<pList->nExpr; i++, pListItem++){
94997    const char *zColName = pListItem->zName;
94998    int requestedSortOrder;
94999    char *zColl;                   /* Collation sequence name */
95000
95001    for(j=0, pTabCol=pTab->aCol; j<pTab->nCol; j++, pTabCol++){
95002      if( sqlite3StrICmp(zColName, pTabCol->zName)==0 ) break;
95003    }
95004    if( j>=pTab->nCol ){
95005      sqlite3ErrorMsg(pParse, "table %s has no column named %s",
95006        pTab->zName, zColName);
95007      pParse->checkSchema = 1;
95008      goto exit_create_index;
95009    }
95010    assert( j<=0x7fff );
95011    pIndex->aiColumn[i] = (i16)j;
95012    if( pListItem->pExpr ){
95013      int nColl;
95014      assert( pListItem->pExpr->op==TK_COLLATE );
95015      zColl = pListItem->pExpr->u.zToken;
95016      nColl = sqlite3Strlen30(zColl) + 1;
95017      assert( nExtra>=nColl );
95018      memcpy(zExtra, zColl, nColl);
95019      zColl = zExtra;
95020      zExtra += nColl;
95021      nExtra -= nColl;
95022    }else{
95023      zColl = pTab->aCol[j].zColl;
95024      if( !zColl ) zColl = "BINARY";
95025    }
95026    if( !db->init.busy && !sqlite3LocateCollSeq(pParse, zColl) ){
95027      goto exit_create_index;
95028    }
95029    pIndex->azColl[i] = zColl;
95030    requestedSortOrder = pListItem->sortOrder & sortOrderMask;
95031    pIndex->aSortOrder[i] = (u8)requestedSortOrder;
95032    if( pTab->aCol[j].notNull==0 ) pIndex->uniqNotNull = 0;
95033  }
95034  if( pPk ){
95035    for(j=0; j<pPk->nKeyCol; j++){
95036      int x = pPk->aiColumn[j];
95037      if( hasColumn(pIndex->aiColumn, pIndex->nKeyCol, x) ){
95038        pIndex->nColumn--;
95039      }else{
95040        pIndex->aiColumn[i] = x;
95041        pIndex->azColl[i] = pPk->azColl[j];
95042        pIndex->aSortOrder[i] = pPk->aSortOrder[j];
95043        i++;
95044      }
95045    }
95046    assert( i==pIndex->nColumn );
95047  }else{
95048    pIndex->aiColumn[i] = -1;
95049    pIndex->azColl[i] = "BINARY";
95050  }
95051  sqlite3DefaultRowEst(pIndex);
95052  if( pParse->pNewTable==0 ) estimateIndexWidth(pIndex);
95053
95054  if( pTab==pParse->pNewTable ){
95055    /* This routine has been called to create an automatic index as a
95056    ** result of a PRIMARY KEY or UNIQUE clause on a column definition, or
95057    ** a PRIMARY KEY or UNIQUE clause following the column definitions.
95058    ** i.e. one of:
95059    **
95060    ** CREATE TABLE t(x PRIMARY KEY, y);
95061    ** CREATE TABLE t(x, y, UNIQUE(x, y));
95062    **
95063    ** Either way, check to see if the table already has such an index. If
95064    ** so, don't bother creating this one. This only applies to
95065    ** automatically created indices. Users can do as they wish with
95066    ** explicit indices.
95067    **
95068    ** Two UNIQUE or PRIMARY KEY constraints are considered equivalent
95069    ** (and thus suppressing the second one) even if they have different
95070    ** sort orders.
95071    **
95072    ** If there are different collating sequences or if the columns of
95073    ** the constraint occur in different orders, then the constraints are
95074    ** considered distinct and both result in separate indices.
95075    */
95076    Index *pIdx;
95077    for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
95078      int k;
95079      assert( IsUniqueIndex(pIdx) );
95080      assert( pIdx->idxType!=SQLITE_IDXTYPE_APPDEF );
95081      assert( IsUniqueIndex(pIndex) );
95082
95083      if( pIdx->nKeyCol!=pIndex->nKeyCol ) continue;
95084      for(k=0; k<pIdx->nKeyCol; k++){
95085        const char *z1;
95086        const char *z2;
95087        if( pIdx->aiColumn[k]!=pIndex->aiColumn[k] ) break;
95088        z1 = pIdx->azColl[k];
95089        z2 = pIndex->azColl[k];
95090        if( z1!=z2 && sqlite3StrICmp(z1, z2) ) break;
95091      }
95092      if( k==pIdx->nKeyCol ){
95093        if( pIdx->onError!=pIndex->onError ){
95094          /* This constraint creates the same index as a previous
95095          ** constraint specified somewhere in the CREATE TABLE statement.
95096          ** However the ON CONFLICT clauses are different. If both this
95097          ** constraint and the previous equivalent constraint have explicit
95098          ** ON CONFLICT clauses this is an error. Otherwise, use the
95099          ** explicitly specified behavior for the index.
95100          */
95101          if( !(pIdx->onError==OE_Default || pIndex->onError==OE_Default) ){
95102            sqlite3ErrorMsg(pParse,
95103                "conflicting ON CONFLICT clauses specified", 0);
95104          }
95105          if( pIdx->onError==OE_Default ){
95106            pIdx->onError = pIndex->onError;
95107          }
95108        }
95109        pRet = pIdx;
95110        goto exit_create_index;
95111      }
95112    }
95113  }
95114
95115  /* Link the new Index structure to its table and to the other
95116  ** in-memory database structures.
95117  */
95118  if( db->init.busy ){
95119    Index *p;
95120    assert( sqlite3SchemaMutexHeld(db, 0, pIndex->pSchema) );
95121    p = sqlite3HashInsert(&pIndex->pSchema->idxHash,
95122                          pIndex->zName, pIndex);
95123    if( p ){
95124      assert( p==pIndex );  /* Malloc must have failed */
95125      db->mallocFailed = 1;
95126      goto exit_create_index;
95127    }
95128    db->flags |= SQLITE_InternChanges;
95129    if( pTblName!=0 ){
95130      pIndex->tnum = db->init.newTnum;
95131    }
95132  }
95133
95134  /* If this is the initial CREATE INDEX statement (or CREATE TABLE if the
95135  ** index is an implied index for a UNIQUE or PRIMARY KEY constraint) then
95136  ** emit code to allocate the index rootpage on disk and make an entry for
95137  ** the index in the sqlite_master table and populate the index with
95138  ** content.  But, do not do this if we are simply reading the sqlite_master
95139  ** table to parse the schema, or if this index is the PRIMARY KEY index
95140  ** of a WITHOUT ROWID table.
95141  **
95142  ** If pTblName==0 it means this index is generated as an implied PRIMARY KEY
95143  ** or UNIQUE index in a CREATE TABLE statement.  Since the table
95144  ** has just been created, it contains no data and the index initialization
95145  ** step can be skipped.
95146  */
95147  else if( pParse->nErr==0 && (HasRowid(pTab) || pTblName!=0) ){
95148    Vdbe *v;
95149    char *zStmt;
95150    int iMem = ++pParse->nMem;
95151
95152    v = sqlite3GetVdbe(pParse);
95153    if( v==0 ) goto exit_create_index;
95154
95155    sqlite3BeginWriteOperation(pParse, 1, iDb);
95156
95157    /* Create the rootpage for the index using CreateIndex. But before
95158    ** doing so, code a Noop instruction and store its address in
95159    ** Index.tnum. This is required in case this index is actually a
95160    ** PRIMARY KEY and the table is actually a WITHOUT ROWID table. In
95161    ** that case the convertToWithoutRowidTable() routine will replace
95162    ** the Noop with a Goto to jump over the VDBE code generated below. */
95163    pIndex->tnum = sqlite3VdbeAddOp0(v, OP_Noop);
95164    sqlite3VdbeAddOp2(v, OP_CreateIndex, iDb, iMem);
95165
95166    /* Gather the complete text of the CREATE INDEX statement into
95167    ** the zStmt variable
95168    */
95169    if( pStart ){
95170      int n = (int)(pParse->sLastToken.z - pName->z) + pParse->sLastToken.n;
95171      if( pName->z[n-1]==';' ) n--;
95172      /* A named index with an explicit CREATE INDEX statement */
95173      zStmt = sqlite3MPrintf(db, "CREATE%s INDEX %.*s",
95174        onError==OE_None ? "" : " UNIQUE", n, pName->z);
95175    }else{
95176      /* An automatic index created by a PRIMARY KEY or UNIQUE constraint */
95177      /* zStmt = sqlite3MPrintf(""); */
95178      zStmt = 0;
95179    }
95180
95181    /* Add an entry in sqlite_master for this index
95182    */
95183    sqlite3NestedParse(pParse,
95184        "INSERT INTO %Q.%s VALUES('index',%Q,%Q,#%d,%Q);",
95185        db->aDb[iDb].zName, SCHEMA_TABLE(iDb),
95186        pIndex->zName,
95187        pTab->zName,
95188        iMem,
95189        zStmt
95190    );
95191    sqlite3DbFree(db, zStmt);
95192
95193    /* Fill the index with data and reparse the schema. Code an OP_Expire
95194    ** to invalidate all pre-compiled statements.
95195    */
95196    if( pTblName ){
95197      sqlite3RefillIndex(pParse, pIndex, iMem);
95198      sqlite3ChangeCookie(pParse, iDb);
95199      sqlite3VdbeAddParseSchemaOp(v, iDb,
95200         sqlite3MPrintf(db, "name='%q' AND type='index'", pIndex->zName));
95201      sqlite3VdbeAddOp1(v, OP_Expire, 0);
95202    }
95203
95204    sqlite3VdbeJumpHere(v, pIndex->tnum);
95205  }
95206
95207  /* When adding an index to the list of indices for a table, make
95208  ** sure all indices labeled OE_Replace come after all those labeled
95209  ** OE_Ignore.  This is necessary for the correct constraint check
95210  ** processing (in sqlite3GenerateConstraintChecks()) as part of
95211  ** UPDATE and INSERT statements.
95212  */
95213  if( db->init.busy || pTblName==0 ){
95214    if( onError!=OE_Replace || pTab->pIndex==0
95215         || pTab->pIndex->onError==OE_Replace){
95216      pIndex->pNext = pTab->pIndex;
95217      pTab->pIndex = pIndex;
95218    }else{
95219      Index *pOther = pTab->pIndex;
95220      while( pOther->pNext && pOther->pNext->onError!=OE_Replace ){
95221        pOther = pOther->pNext;
95222      }
95223      pIndex->pNext = pOther->pNext;
95224      pOther->pNext = pIndex;
95225    }
95226    pRet = pIndex;
95227    pIndex = 0;
95228  }
95229
95230  /* Clean up before exiting */
95231exit_create_index:
95232  if( pIndex ) freeIndex(db, pIndex);
95233  sqlite3ExprDelete(db, pPIWhere);
95234  sqlite3ExprListDelete(db, pList);
95235  sqlite3SrcListDelete(db, pTblName);
95236  sqlite3DbFree(db, zName);
95237  return pRet;
95238}
95239
95240/*
95241** Fill the Index.aiRowEst[] array with default information - information
95242** to be used when we have not run the ANALYZE command.
95243**
95244** aiRowEst[0] is supposed to contain the number of elements in the index.
95245** Since we do not know, guess 1 million.  aiRowEst[1] is an estimate of the
95246** number of rows in the table that match any particular value of the
95247** first column of the index.  aiRowEst[2] is an estimate of the number
95248** of rows that match any particular combination of the first 2 columns
95249** of the index.  And so forth.  It must always be the case that
95250*
95251**           aiRowEst[N]<=aiRowEst[N-1]
95252**           aiRowEst[N]>=1
95253**
95254** Apart from that, we have little to go on besides intuition as to
95255** how aiRowEst[] should be initialized.  The numbers generated here
95256** are based on typical values found in actual indices.
95257*/
95258SQLITE_PRIVATE void sqlite3DefaultRowEst(Index *pIdx){
95259  /*                10,  9,  8,  7,  6 */
95260  LogEst aVal[] = { 33, 32, 30, 28, 26 };
95261  LogEst *a = pIdx->aiRowLogEst;
95262  int nCopy = MIN(ArraySize(aVal), pIdx->nKeyCol);
95263  int i;
95264
95265  /* Set the first entry (number of rows in the index) to the estimated
95266  ** number of rows in the table. Or 10, if the estimated number of rows
95267  ** in the table is less than that.  */
95268  a[0] = pIdx->pTable->nRowLogEst;
95269  if( a[0]<33 ) a[0] = 33;        assert( 33==sqlite3LogEst(10) );
95270
95271  /* Estimate that a[1] is 10, a[2] is 9, a[3] is 8, a[4] is 7, a[5] is
95272  ** 6 and each subsequent value (if any) is 5.  */
95273  memcpy(&a[1], aVal, nCopy*sizeof(LogEst));
95274  for(i=nCopy+1; i<=pIdx->nKeyCol; i++){
95275    a[i] = 23;                    assert( 23==sqlite3LogEst(5) );
95276  }
95277
95278  assert( 0==sqlite3LogEst(1) );
95279  if( IsUniqueIndex(pIdx) ) a[pIdx->nKeyCol] = 0;
95280}
95281
95282/*
95283** This routine will drop an existing named index.  This routine
95284** implements the DROP INDEX statement.
95285*/
95286SQLITE_PRIVATE void sqlite3DropIndex(Parse *pParse, SrcList *pName, int ifExists){
95287  Index *pIndex;
95288  Vdbe *v;
95289  sqlite3 *db = pParse->db;
95290  int iDb;
95291
95292  assert( pParse->nErr==0 );   /* Never called with prior errors */
95293  if( db->mallocFailed ){
95294    goto exit_drop_index;
95295  }
95296  assert( pName->nSrc==1 );
95297  if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){
95298    goto exit_drop_index;
95299  }
95300  pIndex = sqlite3FindIndex(db, pName->a[0].zName, pName->a[0].zDatabase);
95301  if( pIndex==0 ){
95302    if( !ifExists ){
95303      sqlite3ErrorMsg(pParse, "no such index: %S", pName, 0);
95304    }else{
95305      sqlite3CodeVerifyNamedSchema(pParse, pName->a[0].zDatabase);
95306    }
95307    pParse->checkSchema = 1;
95308    goto exit_drop_index;
95309  }
95310  if( pIndex->idxType!=SQLITE_IDXTYPE_APPDEF ){
95311    sqlite3ErrorMsg(pParse, "index associated with UNIQUE "
95312      "or PRIMARY KEY constraint cannot be dropped", 0);
95313    goto exit_drop_index;
95314  }
95315  iDb = sqlite3SchemaToIndex(db, pIndex->pSchema);
95316#ifndef SQLITE_OMIT_AUTHORIZATION
95317  {
95318    int code = SQLITE_DROP_INDEX;
95319    Table *pTab = pIndex->pTable;
95320    const char *zDb = db->aDb[iDb].zName;
95321    const char *zTab = SCHEMA_TABLE(iDb);
95322    if( sqlite3AuthCheck(pParse, SQLITE_DELETE, zTab, 0, zDb) ){
95323      goto exit_drop_index;
95324    }
95325    if( !OMIT_TEMPDB && iDb ) code = SQLITE_DROP_TEMP_INDEX;
95326    if( sqlite3AuthCheck(pParse, code, pIndex->zName, pTab->zName, zDb) ){
95327      goto exit_drop_index;
95328    }
95329  }
95330#endif
95331
95332  /* Generate code to remove the index and from the master table */
95333  v = sqlite3GetVdbe(pParse);
95334  if( v ){
95335    sqlite3BeginWriteOperation(pParse, 1, iDb);
95336    sqlite3NestedParse(pParse,
95337       "DELETE FROM %Q.%s WHERE name=%Q AND type='index'",
95338       db->aDb[iDb].zName, SCHEMA_TABLE(iDb), pIndex->zName
95339    );
95340    sqlite3ClearStatTables(pParse, iDb, "idx", pIndex->zName);
95341    sqlite3ChangeCookie(pParse, iDb);
95342    destroyRootPage(pParse, pIndex->tnum, iDb);
95343    sqlite3VdbeAddOp4(v, OP_DropIndex, iDb, 0, 0, pIndex->zName, 0);
95344  }
95345
95346exit_drop_index:
95347  sqlite3SrcListDelete(db, pName);
95348}
95349
95350/*
95351** pArray is a pointer to an array of objects. Each object in the
95352** array is szEntry bytes in size. This routine uses sqlite3DbRealloc()
95353** to extend the array so that there is space for a new object at the end.
95354**
95355** When this function is called, *pnEntry contains the current size of
95356** the array (in entries - so the allocation is ((*pnEntry) * szEntry) bytes
95357** in total).
95358**
95359** If the realloc() is successful (i.e. if no OOM condition occurs), the
95360** space allocated for the new object is zeroed, *pnEntry updated to
95361** reflect the new size of the array and a pointer to the new allocation
95362** returned. *pIdx is set to the index of the new array entry in this case.
95363**
95364** Otherwise, if the realloc() fails, *pIdx is set to -1, *pnEntry remains
95365** unchanged and a copy of pArray returned.
95366*/
95367SQLITE_PRIVATE void *sqlite3ArrayAllocate(
95368  sqlite3 *db,      /* Connection to notify of malloc failures */
95369  void *pArray,     /* Array of objects.  Might be reallocated */
95370  int szEntry,      /* Size of each object in the array */
95371  int *pnEntry,     /* Number of objects currently in use */
95372  int *pIdx         /* Write the index of a new slot here */
95373){
95374  char *z;
95375  int n = *pnEntry;
95376  if( (n & (n-1))==0 ){
95377    int sz = (n==0) ? 1 : 2*n;
95378    void *pNew = sqlite3DbRealloc(db, pArray, sz*szEntry);
95379    if( pNew==0 ){
95380      *pIdx = -1;
95381      return pArray;
95382    }
95383    pArray = pNew;
95384  }
95385  z = (char*)pArray;
95386  memset(&z[n * szEntry], 0, szEntry);
95387  *pIdx = n;
95388  ++*pnEntry;
95389  return pArray;
95390}
95391
95392/*
95393** Append a new element to the given IdList.  Create a new IdList if
95394** need be.
95395**
95396** A new IdList is returned, or NULL if malloc() fails.
95397*/
95398SQLITE_PRIVATE IdList *sqlite3IdListAppend(sqlite3 *db, IdList *pList, Token *pToken){
95399  int i;
95400  if( pList==0 ){
95401    pList = sqlite3DbMallocZero(db, sizeof(IdList) );
95402    if( pList==0 ) return 0;
95403  }
95404  pList->a = sqlite3ArrayAllocate(
95405      db,
95406      pList->a,
95407      sizeof(pList->a[0]),
95408      &pList->nId,
95409      &i
95410  );
95411  if( i<0 ){
95412    sqlite3IdListDelete(db, pList);
95413    return 0;
95414  }
95415  pList->a[i].zName = sqlite3NameFromToken(db, pToken);
95416  return pList;
95417}
95418
95419/*
95420** Delete an IdList.
95421*/
95422SQLITE_PRIVATE void sqlite3IdListDelete(sqlite3 *db, IdList *pList){
95423  int i;
95424  if( pList==0 ) return;
95425  for(i=0; i<pList->nId; i++){
95426    sqlite3DbFree(db, pList->a[i].zName);
95427  }
95428  sqlite3DbFree(db, pList->a);
95429  sqlite3DbFree(db, pList);
95430}
95431
95432/*
95433** Return the index in pList of the identifier named zId.  Return -1
95434** if not found.
95435*/
95436SQLITE_PRIVATE int sqlite3IdListIndex(IdList *pList, const char *zName){
95437  int i;
95438  if( pList==0 ) return -1;
95439  for(i=0; i<pList->nId; i++){
95440    if( sqlite3StrICmp(pList->a[i].zName, zName)==0 ) return i;
95441  }
95442  return -1;
95443}
95444
95445/*
95446** Expand the space allocated for the given SrcList object by
95447** creating nExtra new slots beginning at iStart.  iStart is zero based.
95448** New slots are zeroed.
95449**
95450** For example, suppose a SrcList initially contains two entries: A,B.
95451** To append 3 new entries onto the end, do this:
95452**
95453**    sqlite3SrcListEnlarge(db, pSrclist, 3, 2);
95454**
95455** After the call above it would contain:  A, B, nil, nil, nil.
95456** If the iStart argument had been 1 instead of 2, then the result
95457** would have been:  A, nil, nil, nil, B.  To prepend the new slots,
95458** the iStart value would be 0.  The result then would
95459** be: nil, nil, nil, A, B.
95460**
95461** If a memory allocation fails the SrcList is unchanged.  The
95462** db->mallocFailed flag will be set to true.
95463*/
95464SQLITE_PRIVATE SrcList *sqlite3SrcListEnlarge(
95465  sqlite3 *db,       /* Database connection to notify of OOM errors */
95466  SrcList *pSrc,     /* The SrcList to be enlarged */
95467  int nExtra,        /* Number of new slots to add to pSrc->a[] */
95468  int iStart         /* Index in pSrc->a[] of first new slot */
95469){
95470  int i;
95471
95472  /* Sanity checking on calling parameters */
95473  assert( iStart>=0 );
95474  assert( nExtra>=1 );
95475  assert( pSrc!=0 );
95476  assert( iStart<=pSrc->nSrc );
95477
95478  /* Allocate additional space if needed */
95479  if( (u32)pSrc->nSrc+nExtra>pSrc->nAlloc ){
95480    SrcList *pNew;
95481    int nAlloc = pSrc->nSrc+nExtra;
95482    int nGot;
95483    pNew = sqlite3DbRealloc(db, pSrc,
95484               sizeof(*pSrc) + (nAlloc-1)*sizeof(pSrc->a[0]) );
95485    if( pNew==0 ){
95486      assert( db->mallocFailed );
95487      return pSrc;
95488    }
95489    pSrc = pNew;
95490    nGot = (sqlite3DbMallocSize(db, pNew) - sizeof(*pSrc))/sizeof(pSrc->a[0])+1;
95491    pSrc->nAlloc = nGot;
95492  }
95493
95494  /* Move existing slots that come after the newly inserted slots
95495  ** out of the way */
95496  for(i=pSrc->nSrc-1; i>=iStart; i--){
95497    pSrc->a[i+nExtra] = pSrc->a[i];
95498  }
95499  pSrc->nSrc += nExtra;
95500
95501  /* Zero the newly allocated slots */
95502  memset(&pSrc->a[iStart], 0, sizeof(pSrc->a[0])*nExtra);
95503  for(i=iStart; i<iStart+nExtra; i++){
95504    pSrc->a[i].iCursor = -1;
95505  }
95506
95507  /* Return a pointer to the enlarged SrcList */
95508  return pSrc;
95509}
95510
95511
95512/*
95513** Append a new table name to the given SrcList.  Create a new SrcList if
95514** need be.  A new entry is created in the SrcList even if pTable is NULL.
95515**
95516** A SrcList is returned, or NULL if there is an OOM error.  The returned
95517** SrcList might be the same as the SrcList that was input or it might be
95518** a new one.  If an OOM error does occurs, then the prior value of pList
95519** that is input to this routine is automatically freed.
95520**
95521** If pDatabase is not null, it means that the table has an optional
95522** database name prefix.  Like this:  "database.table".  The pDatabase
95523** points to the table name and the pTable points to the database name.
95524** The SrcList.a[].zName field is filled with the table name which might
95525** come from pTable (if pDatabase is NULL) or from pDatabase.
95526** SrcList.a[].zDatabase is filled with the database name from pTable,
95527** or with NULL if no database is specified.
95528**
95529** In other words, if call like this:
95530**
95531**         sqlite3SrcListAppend(D,A,B,0);
95532**
95533** Then B is a table name and the database name is unspecified.  If called
95534** like this:
95535**
95536**         sqlite3SrcListAppend(D,A,B,C);
95537**
95538** Then C is the table name and B is the database name.  If C is defined
95539** then so is B.  In other words, we never have a case where:
95540**
95541**         sqlite3SrcListAppend(D,A,0,C);
95542**
95543** Both pTable and pDatabase are assumed to be quoted.  They are dequoted
95544** before being added to the SrcList.
95545*/
95546SQLITE_PRIVATE SrcList *sqlite3SrcListAppend(
95547  sqlite3 *db,        /* Connection to notify of malloc failures */
95548  SrcList *pList,     /* Append to this SrcList. NULL creates a new SrcList */
95549  Token *pTable,      /* Table to append */
95550  Token *pDatabase    /* Database of the table */
95551){
95552  struct SrcList_item *pItem;
95553  assert( pDatabase==0 || pTable!=0 );  /* Cannot have C without B */
95554  if( pList==0 ){
95555    pList = sqlite3DbMallocZero(db, sizeof(SrcList) );
95556    if( pList==0 ) return 0;
95557    pList->nAlloc = 1;
95558  }
95559  pList = sqlite3SrcListEnlarge(db, pList, 1, pList->nSrc);
95560  if( db->mallocFailed ){
95561    sqlite3SrcListDelete(db, pList);
95562    return 0;
95563  }
95564  pItem = &pList->a[pList->nSrc-1];
95565  if( pDatabase && pDatabase->z==0 ){
95566    pDatabase = 0;
95567  }
95568  if( pDatabase ){
95569    Token *pTemp = pDatabase;
95570    pDatabase = pTable;
95571    pTable = pTemp;
95572  }
95573  pItem->zName = sqlite3NameFromToken(db, pTable);
95574  pItem->zDatabase = sqlite3NameFromToken(db, pDatabase);
95575  return pList;
95576}
95577
95578/*
95579** Assign VdbeCursor index numbers to all tables in a SrcList
95580*/
95581SQLITE_PRIVATE void sqlite3SrcListAssignCursors(Parse *pParse, SrcList *pList){
95582  int i;
95583  struct SrcList_item *pItem;
95584  assert(pList || pParse->db->mallocFailed );
95585  if( pList ){
95586    for(i=0, pItem=pList->a; i<pList->nSrc; i++, pItem++){
95587      if( pItem->iCursor>=0 ) break;
95588      pItem->iCursor = pParse->nTab++;
95589      if( pItem->pSelect ){
95590        sqlite3SrcListAssignCursors(pParse, pItem->pSelect->pSrc);
95591      }
95592    }
95593  }
95594}
95595
95596/*
95597** Delete an entire SrcList including all its substructure.
95598*/
95599SQLITE_PRIVATE void sqlite3SrcListDelete(sqlite3 *db, SrcList *pList){
95600  int i;
95601  struct SrcList_item *pItem;
95602  if( pList==0 ) return;
95603  for(pItem=pList->a, i=0; i<pList->nSrc; i++, pItem++){
95604    sqlite3DbFree(db, pItem->zDatabase);
95605    sqlite3DbFree(db, pItem->zName);
95606    sqlite3DbFree(db, pItem->zAlias);
95607    sqlite3DbFree(db, pItem->zIndexedBy);
95608    sqlite3DeleteTable(db, pItem->pTab);
95609    sqlite3SelectDelete(db, pItem->pSelect);
95610    sqlite3ExprDelete(db, pItem->pOn);
95611    sqlite3IdListDelete(db, pItem->pUsing);
95612  }
95613  sqlite3DbFree(db, pList);
95614}
95615
95616/*
95617** This routine is called by the parser to add a new term to the
95618** end of a growing FROM clause.  The "p" parameter is the part of
95619** the FROM clause that has already been constructed.  "p" is NULL
95620** if this is the first term of the FROM clause.  pTable and pDatabase
95621** are the name of the table and database named in the FROM clause term.
95622** pDatabase is NULL if the database name qualifier is missing - the
95623** usual case.  If the term has an alias, then pAlias points to the
95624** alias token.  If the term is a subquery, then pSubquery is the
95625** SELECT statement that the subquery encodes.  The pTable and
95626** pDatabase parameters are NULL for subqueries.  The pOn and pUsing
95627** parameters are the content of the ON and USING clauses.
95628**
95629** Return a new SrcList which encodes is the FROM with the new
95630** term added.
95631*/
95632SQLITE_PRIVATE SrcList *sqlite3SrcListAppendFromTerm(
95633  Parse *pParse,          /* Parsing context */
95634  SrcList *p,             /* The left part of the FROM clause already seen */
95635  Token *pTable,          /* Name of the table to add to the FROM clause */
95636  Token *pDatabase,       /* Name of the database containing pTable */
95637  Token *pAlias,          /* The right-hand side of the AS subexpression */
95638  Select *pSubquery,      /* A subquery used in place of a table name */
95639  Expr *pOn,              /* The ON clause of a join */
95640  IdList *pUsing          /* The USING clause of a join */
95641){
95642  struct SrcList_item *pItem;
95643  sqlite3 *db = pParse->db;
95644  if( !p && (pOn || pUsing) ){
95645    sqlite3ErrorMsg(pParse, "a JOIN clause is required before %s",
95646      (pOn ? "ON" : "USING")
95647    );
95648    goto append_from_error;
95649  }
95650  p = sqlite3SrcListAppend(db, p, pTable, pDatabase);
95651  if( p==0 || NEVER(p->nSrc==0) ){
95652    goto append_from_error;
95653  }
95654  pItem = &p->a[p->nSrc-1];
95655  assert( pAlias!=0 );
95656  if( pAlias->n ){
95657    pItem->zAlias = sqlite3NameFromToken(db, pAlias);
95658  }
95659  pItem->pSelect = pSubquery;
95660  pItem->pOn = pOn;
95661  pItem->pUsing = pUsing;
95662  return p;
95663
95664 append_from_error:
95665  assert( p==0 );
95666  sqlite3ExprDelete(db, pOn);
95667  sqlite3IdListDelete(db, pUsing);
95668  sqlite3SelectDelete(db, pSubquery);
95669  return 0;
95670}
95671
95672/*
95673** Add an INDEXED BY or NOT INDEXED clause to the most recently added
95674** element of the source-list passed as the second argument.
95675*/
95676SQLITE_PRIVATE void sqlite3SrcListIndexedBy(Parse *pParse, SrcList *p, Token *pIndexedBy){
95677  assert( pIndexedBy!=0 );
95678  if( p && ALWAYS(p->nSrc>0) ){
95679    struct SrcList_item *pItem = &p->a[p->nSrc-1];
95680    assert( pItem->notIndexed==0 && pItem->zIndexedBy==0 );
95681    if( pIndexedBy->n==1 && !pIndexedBy->z ){
95682      /* A "NOT INDEXED" clause was supplied. See parse.y
95683      ** construct "indexed_opt" for details. */
95684      pItem->notIndexed = 1;
95685    }else{
95686      pItem->zIndexedBy = sqlite3NameFromToken(pParse->db, pIndexedBy);
95687    }
95688  }
95689}
95690
95691/*
95692** When building up a FROM clause in the parser, the join operator
95693** is initially attached to the left operand.  But the code generator
95694** expects the join operator to be on the right operand.  This routine
95695** Shifts all join operators from left to right for an entire FROM
95696** clause.
95697**
95698** Example: Suppose the join is like this:
95699**
95700**           A natural cross join B
95701**
95702** The operator is "natural cross join".  The A and B operands are stored
95703** in p->a[0] and p->a[1], respectively.  The parser initially stores the
95704** operator with A.  This routine shifts that operator over to B.
95705*/
95706SQLITE_PRIVATE void sqlite3SrcListShiftJoinType(SrcList *p){
95707  if( p ){
95708    int i;
95709    for(i=p->nSrc-1; i>0; i--){
95710      p->a[i].jointype = p->a[i-1].jointype;
95711    }
95712    p->a[0].jointype = 0;
95713  }
95714}
95715
95716/*
95717** Begin a transaction
95718*/
95719SQLITE_PRIVATE void sqlite3BeginTransaction(Parse *pParse, int type){
95720  sqlite3 *db;
95721  Vdbe *v;
95722  int i;
95723
95724  assert( pParse!=0 );
95725  db = pParse->db;
95726  assert( db!=0 );
95727/*  if( db->aDb[0].pBt==0 ) return; */
95728  if( sqlite3AuthCheck(pParse, SQLITE_TRANSACTION, "BEGIN", 0, 0) ){
95729    return;
95730  }
95731  v = sqlite3GetVdbe(pParse);
95732  if( !v ) return;
95733  if( type!=TK_DEFERRED ){
95734    for(i=0; i<db->nDb; i++){
95735      sqlite3VdbeAddOp2(v, OP_Transaction, i, (type==TK_EXCLUSIVE)+1);
95736      sqlite3VdbeUsesBtree(v, i);
95737    }
95738  }
95739  sqlite3VdbeAddOp2(v, OP_AutoCommit, 0, 0);
95740}
95741
95742/*
95743** Commit a transaction
95744*/
95745SQLITE_PRIVATE void sqlite3CommitTransaction(Parse *pParse){
95746  Vdbe *v;
95747
95748  assert( pParse!=0 );
95749  assert( pParse->db!=0 );
95750  if( sqlite3AuthCheck(pParse, SQLITE_TRANSACTION, "COMMIT", 0, 0) ){
95751    return;
95752  }
95753  v = sqlite3GetVdbe(pParse);
95754  if( v ){
95755    sqlite3VdbeAddOp2(v, OP_AutoCommit, 1, 0);
95756  }
95757}
95758
95759/*
95760** Rollback a transaction
95761*/
95762SQLITE_PRIVATE void sqlite3RollbackTransaction(Parse *pParse){
95763  Vdbe *v;
95764
95765  assert( pParse!=0 );
95766  assert( pParse->db!=0 );
95767  if( sqlite3AuthCheck(pParse, SQLITE_TRANSACTION, "ROLLBACK", 0, 0) ){
95768    return;
95769  }
95770  v = sqlite3GetVdbe(pParse);
95771  if( v ){
95772    sqlite3VdbeAddOp2(v, OP_AutoCommit, 1, 1);
95773  }
95774}
95775
95776/*
95777** This function is called by the parser when it parses a command to create,
95778** release or rollback an SQL savepoint.
95779*/
95780SQLITE_PRIVATE void sqlite3Savepoint(Parse *pParse, int op, Token *pName){
95781  char *zName = sqlite3NameFromToken(pParse->db, pName);
95782  if( zName ){
95783    Vdbe *v = sqlite3GetVdbe(pParse);
95784#ifndef SQLITE_OMIT_AUTHORIZATION
95785    static const char * const az[] = { "BEGIN", "RELEASE", "ROLLBACK" };
95786    assert( !SAVEPOINT_BEGIN && SAVEPOINT_RELEASE==1 && SAVEPOINT_ROLLBACK==2 );
95787#endif
95788    if( !v || sqlite3AuthCheck(pParse, SQLITE_SAVEPOINT, az[op], zName, 0) ){
95789      sqlite3DbFree(pParse->db, zName);
95790      return;
95791    }
95792    sqlite3VdbeAddOp4(v, OP_Savepoint, op, 0, 0, zName, P4_DYNAMIC);
95793  }
95794}
95795
95796/*
95797** Make sure the TEMP database is open and available for use.  Return
95798** the number of errors.  Leave any error messages in the pParse structure.
95799*/
95800SQLITE_PRIVATE int sqlite3OpenTempDatabase(Parse *pParse){
95801  sqlite3 *db = pParse->db;
95802  if( db->aDb[1].pBt==0 && !pParse->explain ){
95803    int rc;
95804    Btree *pBt;
95805    static const int flags =
95806          SQLITE_OPEN_READWRITE |
95807          SQLITE_OPEN_CREATE |
95808          SQLITE_OPEN_EXCLUSIVE |
95809          SQLITE_OPEN_DELETEONCLOSE |
95810          SQLITE_OPEN_TEMP_DB;
95811
95812    rc = sqlite3BtreeOpen(db->pVfs, 0, db, &pBt, 0, flags);
95813    if( rc!=SQLITE_OK ){
95814      sqlite3ErrorMsg(pParse, "unable to open a temporary database "
95815        "file for storing temporary tables");
95816      pParse->rc = rc;
95817      return 1;
95818    }
95819    db->aDb[1].pBt = pBt;
95820    assert( db->aDb[1].pSchema );
95821    if( SQLITE_NOMEM==sqlite3BtreeSetPageSize(pBt, db->nextPagesize, -1, 0) ){
95822      db->mallocFailed = 1;
95823      return 1;
95824    }
95825  }
95826  return 0;
95827}
95828
95829/*
95830** Record the fact that the schema cookie will need to be verified
95831** for database iDb.  The code to actually verify the schema cookie
95832** will occur at the end of the top-level VDBE and will be generated
95833** later, by sqlite3FinishCoding().
95834*/
95835SQLITE_PRIVATE void sqlite3CodeVerifySchema(Parse *pParse, int iDb){
95836  Parse *pToplevel = sqlite3ParseToplevel(pParse);
95837  sqlite3 *db = pToplevel->db;
95838
95839  assert( iDb>=0 && iDb<db->nDb );
95840  assert( db->aDb[iDb].pBt!=0 || iDb==1 );
95841  assert( iDb<SQLITE_MAX_ATTACHED+2 );
95842  assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
95843  if( DbMaskTest(pToplevel->cookieMask, iDb)==0 ){
95844    DbMaskSet(pToplevel->cookieMask, iDb);
95845    pToplevel->cookieValue[iDb] = db->aDb[iDb].pSchema->schema_cookie;
95846    if( !OMIT_TEMPDB && iDb==1 ){
95847      sqlite3OpenTempDatabase(pToplevel);
95848    }
95849  }
95850}
95851
95852/*
95853** If argument zDb is NULL, then call sqlite3CodeVerifySchema() for each
95854** attached database. Otherwise, invoke it for the database named zDb only.
95855*/
95856SQLITE_PRIVATE void sqlite3CodeVerifyNamedSchema(Parse *pParse, const char *zDb){
95857  sqlite3 *db = pParse->db;
95858  int i;
95859  for(i=0; i<db->nDb; i++){
95860    Db *pDb = &db->aDb[i];
95861    if( pDb->pBt && (!zDb || 0==sqlite3StrICmp(zDb, pDb->zName)) ){
95862      sqlite3CodeVerifySchema(pParse, i);
95863    }
95864  }
95865}
95866
95867/*
95868** Generate VDBE code that prepares for doing an operation that
95869** might change the database.
95870**
95871** This routine starts a new transaction if we are not already within
95872** a transaction.  If we are already within a transaction, then a checkpoint
95873** is set if the setStatement parameter is true.  A checkpoint should
95874** be set for operations that might fail (due to a constraint) part of
95875** the way through and which will need to undo some writes without having to
95876** rollback the whole transaction.  For operations where all constraints
95877** can be checked before any changes are made to the database, it is never
95878** necessary to undo a write and the checkpoint should not be set.
95879*/
95880SQLITE_PRIVATE void sqlite3BeginWriteOperation(Parse *pParse, int setStatement, int iDb){
95881  Parse *pToplevel = sqlite3ParseToplevel(pParse);
95882  sqlite3CodeVerifySchema(pParse, iDb);
95883  DbMaskSet(pToplevel->writeMask, iDb);
95884  pToplevel->isMultiWrite |= setStatement;
95885}
95886
95887/*
95888** Indicate that the statement currently under construction might write
95889** more than one entry (example: deleting one row then inserting another,
95890** inserting multiple rows in a table, or inserting a row and index entries.)
95891** If an abort occurs after some of these writes have completed, then it will
95892** be necessary to undo the completed writes.
95893*/
95894SQLITE_PRIVATE void sqlite3MultiWrite(Parse *pParse){
95895  Parse *pToplevel = sqlite3ParseToplevel(pParse);
95896  pToplevel->isMultiWrite = 1;
95897}
95898
95899/*
95900** The code generator calls this routine if is discovers that it is
95901** possible to abort a statement prior to completion.  In order to
95902** perform this abort without corrupting the database, we need to make
95903** sure that the statement is protected by a statement transaction.
95904**
95905** Technically, we only need to set the mayAbort flag if the
95906** isMultiWrite flag was previously set.  There is a time dependency
95907** such that the abort must occur after the multiwrite.  This makes
95908** some statements involving the REPLACE conflict resolution algorithm
95909** go a little faster.  But taking advantage of this time dependency
95910** makes it more difficult to prove that the code is correct (in
95911** particular, it prevents us from writing an effective
95912** implementation of sqlite3AssertMayAbort()) and so we have chosen
95913** to take the safe route and skip the optimization.
95914*/
95915SQLITE_PRIVATE void sqlite3MayAbort(Parse *pParse){
95916  Parse *pToplevel = sqlite3ParseToplevel(pParse);
95917  pToplevel->mayAbort = 1;
95918}
95919
95920/*
95921** Code an OP_Halt that causes the vdbe to return an SQLITE_CONSTRAINT
95922** error. The onError parameter determines which (if any) of the statement
95923** and/or current transaction is rolled back.
95924*/
95925SQLITE_PRIVATE void sqlite3HaltConstraint(
95926  Parse *pParse,    /* Parsing context */
95927  int errCode,      /* extended error code */
95928  int onError,      /* Constraint type */
95929  char *p4,         /* Error message */
95930  i8 p4type,        /* P4_STATIC or P4_TRANSIENT */
95931  u8 p5Errmsg       /* P5_ErrMsg type */
95932){
95933  Vdbe *v = sqlite3GetVdbe(pParse);
95934  assert( (errCode&0xff)==SQLITE_CONSTRAINT );
95935  if( onError==OE_Abort ){
95936    sqlite3MayAbort(pParse);
95937  }
95938  sqlite3VdbeAddOp4(v, OP_Halt, errCode, onError, 0, p4, p4type);
95939  if( p5Errmsg ) sqlite3VdbeChangeP5(v, p5Errmsg);
95940}
95941
95942/*
95943** Code an OP_Halt due to UNIQUE or PRIMARY KEY constraint violation.
95944*/
95945SQLITE_PRIVATE void sqlite3UniqueConstraint(
95946  Parse *pParse,    /* Parsing context */
95947  int onError,      /* Constraint type */
95948  Index *pIdx       /* The index that triggers the constraint */
95949){
95950  char *zErr;
95951  int j;
95952  StrAccum errMsg;
95953  Table *pTab = pIdx->pTable;
95954
95955  sqlite3StrAccumInit(&errMsg, pParse->db, 0, 0, 200);
95956  for(j=0; j<pIdx->nKeyCol; j++){
95957    char *zCol = pTab->aCol[pIdx->aiColumn[j]].zName;
95958    if( j ) sqlite3StrAccumAppend(&errMsg, ", ", 2);
95959    sqlite3StrAccumAppendAll(&errMsg, pTab->zName);
95960    sqlite3StrAccumAppend(&errMsg, ".", 1);
95961    sqlite3StrAccumAppendAll(&errMsg, zCol);
95962  }
95963  zErr = sqlite3StrAccumFinish(&errMsg);
95964  sqlite3HaltConstraint(pParse,
95965    IsPrimaryKeyIndex(pIdx) ? SQLITE_CONSTRAINT_PRIMARYKEY
95966                            : SQLITE_CONSTRAINT_UNIQUE,
95967    onError, zErr, P4_DYNAMIC, P5_ConstraintUnique);
95968}
95969
95970
95971/*
95972** Code an OP_Halt due to non-unique rowid.
95973*/
95974SQLITE_PRIVATE void sqlite3RowidConstraint(
95975  Parse *pParse,    /* Parsing context */
95976  int onError,      /* Conflict resolution algorithm */
95977  Table *pTab       /* The table with the non-unique rowid */
95978){
95979  char *zMsg;
95980  int rc;
95981  if( pTab->iPKey>=0 ){
95982    zMsg = sqlite3MPrintf(pParse->db, "%s.%s", pTab->zName,
95983                          pTab->aCol[pTab->iPKey].zName);
95984    rc = SQLITE_CONSTRAINT_PRIMARYKEY;
95985  }else{
95986    zMsg = sqlite3MPrintf(pParse->db, "%s.rowid", pTab->zName);
95987    rc = SQLITE_CONSTRAINT_ROWID;
95988  }
95989  sqlite3HaltConstraint(pParse, rc, onError, zMsg, P4_DYNAMIC,
95990                        P5_ConstraintUnique);
95991}
95992
95993/*
95994** Check to see if pIndex uses the collating sequence pColl.  Return
95995** true if it does and false if it does not.
95996*/
95997#ifndef SQLITE_OMIT_REINDEX
95998static int collationMatch(const char *zColl, Index *pIndex){
95999  int i;
96000  assert( zColl!=0 );
96001  for(i=0; i<pIndex->nColumn; i++){
96002    const char *z = pIndex->azColl[i];
96003    assert( z!=0 || pIndex->aiColumn[i]<0 );
96004    if( pIndex->aiColumn[i]>=0 && 0==sqlite3StrICmp(z, zColl) ){
96005      return 1;
96006    }
96007  }
96008  return 0;
96009}
96010#endif
96011
96012/*
96013** Recompute all indices of pTab that use the collating sequence pColl.
96014** If pColl==0 then recompute all indices of pTab.
96015*/
96016#ifndef SQLITE_OMIT_REINDEX
96017static void reindexTable(Parse *pParse, Table *pTab, char const *zColl){
96018  Index *pIndex;              /* An index associated with pTab */
96019
96020  for(pIndex=pTab->pIndex; pIndex; pIndex=pIndex->pNext){
96021    if( zColl==0 || collationMatch(zColl, pIndex) ){
96022      int iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
96023      sqlite3BeginWriteOperation(pParse, 0, iDb);
96024      sqlite3RefillIndex(pParse, pIndex, -1);
96025    }
96026  }
96027}
96028#endif
96029
96030/*
96031** Recompute all indices of all tables in all databases where the
96032** indices use the collating sequence pColl.  If pColl==0 then recompute
96033** all indices everywhere.
96034*/
96035#ifndef SQLITE_OMIT_REINDEX
96036static void reindexDatabases(Parse *pParse, char const *zColl){
96037  Db *pDb;                    /* A single database */
96038  int iDb;                    /* The database index number */
96039  sqlite3 *db = pParse->db;   /* The database connection */
96040  HashElem *k;                /* For looping over tables in pDb */
96041  Table *pTab;                /* A table in the database */
96042
96043  assert( sqlite3BtreeHoldsAllMutexes(db) );  /* Needed for schema access */
96044  for(iDb=0, pDb=db->aDb; iDb<db->nDb; iDb++, pDb++){
96045    assert( pDb!=0 );
96046    for(k=sqliteHashFirst(&pDb->pSchema->tblHash);  k; k=sqliteHashNext(k)){
96047      pTab = (Table*)sqliteHashData(k);
96048      reindexTable(pParse, pTab, zColl);
96049    }
96050  }
96051}
96052#endif
96053
96054/*
96055** Generate code for the REINDEX command.
96056**
96057**        REINDEX                            -- 1
96058**        REINDEX  <collation>               -- 2
96059**        REINDEX  ?<database>.?<tablename>  -- 3
96060**        REINDEX  ?<database>.?<indexname>  -- 4
96061**
96062** Form 1 causes all indices in all attached databases to be rebuilt.
96063** Form 2 rebuilds all indices in all databases that use the named
96064** collating function.  Forms 3 and 4 rebuild the named index or all
96065** indices associated with the named table.
96066*/
96067#ifndef SQLITE_OMIT_REINDEX
96068SQLITE_PRIVATE void sqlite3Reindex(Parse *pParse, Token *pName1, Token *pName2){
96069  CollSeq *pColl;             /* Collating sequence to be reindexed, or NULL */
96070  char *z;                    /* Name of a table or index */
96071  const char *zDb;            /* Name of the database */
96072  Table *pTab;                /* A table in the database */
96073  Index *pIndex;              /* An index associated with pTab */
96074  int iDb;                    /* The database index number */
96075  sqlite3 *db = pParse->db;   /* The database connection */
96076  Token *pObjName;            /* Name of the table or index to be reindexed */
96077
96078  /* Read the database schema. If an error occurs, leave an error message
96079  ** and code in pParse and return NULL. */
96080  if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){
96081    return;
96082  }
96083
96084  if( pName1==0 ){
96085    reindexDatabases(pParse, 0);
96086    return;
96087  }else if( NEVER(pName2==0) || pName2->z==0 ){
96088    char *zColl;
96089    assert( pName1->z );
96090    zColl = sqlite3NameFromToken(pParse->db, pName1);
96091    if( !zColl ) return;
96092    pColl = sqlite3FindCollSeq(db, ENC(db), zColl, 0);
96093    if( pColl ){
96094      reindexDatabases(pParse, zColl);
96095      sqlite3DbFree(db, zColl);
96096      return;
96097    }
96098    sqlite3DbFree(db, zColl);
96099  }
96100  iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pObjName);
96101  if( iDb<0 ) return;
96102  z = sqlite3NameFromToken(db, pObjName);
96103  if( z==0 ) return;
96104  zDb = db->aDb[iDb].zName;
96105  pTab = sqlite3FindTable(db, z, zDb);
96106  if( pTab ){
96107    reindexTable(pParse, pTab, 0);
96108    sqlite3DbFree(db, z);
96109    return;
96110  }
96111  pIndex = sqlite3FindIndex(db, z, zDb);
96112  sqlite3DbFree(db, z);
96113  if( pIndex ){
96114    sqlite3BeginWriteOperation(pParse, 0, iDb);
96115    sqlite3RefillIndex(pParse, pIndex, -1);
96116    return;
96117  }
96118  sqlite3ErrorMsg(pParse, "unable to identify the object to be reindexed");
96119}
96120#endif
96121
96122/*
96123** Return a KeyInfo structure that is appropriate for the given Index.
96124**
96125** The KeyInfo structure for an index is cached in the Index object.
96126** So there might be multiple references to the returned pointer.  The
96127** caller should not try to modify the KeyInfo object.
96128**
96129** The caller should invoke sqlite3KeyInfoUnref() on the returned object
96130** when it has finished using it.
96131*/
96132SQLITE_PRIVATE KeyInfo *sqlite3KeyInfoOfIndex(Parse *pParse, Index *pIdx){
96133  int i;
96134  int nCol = pIdx->nColumn;
96135  int nKey = pIdx->nKeyCol;
96136  KeyInfo *pKey;
96137  if( pParse->nErr ) return 0;
96138  if( pIdx->uniqNotNull ){
96139    pKey = sqlite3KeyInfoAlloc(pParse->db, nKey, nCol-nKey);
96140  }else{
96141    pKey = sqlite3KeyInfoAlloc(pParse->db, nCol, 0);
96142  }
96143  if( pKey ){
96144    assert( sqlite3KeyInfoIsWriteable(pKey) );
96145    for(i=0; i<nCol; i++){
96146      char *zColl = pIdx->azColl[i];
96147      assert( zColl!=0 );
96148      pKey->aColl[i] = strcmp(zColl,"BINARY")==0 ? 0 :
96149                        sqlite3LocateCollSeq(pParse, zColl);
96150      pKey->aSortOrder[i] = pIdx->aSortOrder[i];
96151    }
96152    if( pParse->nErr ){
96153      sqlite3KeyInfoUnref(pKey);
96154      pKey = 0;
96155    }
96156  }
96157  return pKey;
96158}
96159
96160#ifndef SQLITE_OMIT_CTE
96161/*
96162** This routine is invoked once per CTE by the parser while parsing a
96163** WITH clause.
96164*/
96165SQLITE_PRIVATE With *sqlite3WithAdd(
96166  Parse *pParse,          /* Parsing context */
96167  With *pWith,            /* Existing WITH clause, or NULL */
96168  Token *pName,           /* Name of the common-table */
96169  ExprList *pArglist,     /* Optional column name list for the table */
96170  Select *pQuery          /* Query used to initialize the table */
96171){
96172  sqlite3 *db = pParse->db;
96173  With *pNew;
96174  char *zName;
96175
96176  /* Check that the CTE name is unique within this WITH clause. If
96177  ** not, store an error in the Parse structure. */
96178  zName = sqlite3NameFromToken(pParse->db, pName);
96179  if( zName && pWith ){
96180    int i;
96181    for(i=0; i<pWith->nCte; i++){
96182      if( sqlite3StrICmp(zName, pWith->a[i].zName)==0 ){
96183        sqlite3ErrorMsg(pParse, "duplicate WITH table name: %s", zName);
96184      }
96185    }
96186  }
96187
96188  if( pWith ){
96189    int nByte = sizeof(*pWith) + (sizeof(pWith->a[1]) * pWith->nCte);
96190    pNew = sqlite3DbRealloc(db, pWith, nByte);
96191  }else{
96192    pNew = sqlite3DbMallocZero(db, sizeof(*pWith));
96193  }
96194  assert( zName!=0 || pNew==0 );
96195  assert( db->mallocFailed==0 || pNew==0 );
96196
96197  if( pNew==0 ){
96198    sqlite3ExprListDelete(db, pArglist);
96199    sqlite3SelectDelete(db, pQuery);
96200    sqlite3DbFree(db, zName);
96201    pNew = pWith;
96202  }else{
96203    pNew->a[pNew->nCte].pSelect = pQuery;
96204    pNew->a[pNew->nCte].pCols = pArglist;
96205    pNew->a[pNew->nCte].zName = zName;
96206    pNew->a[pNew->nCte].zErr = 0;
96207    pNew->nCte++;
96208  }
96209
96210  return pNew;
96211}
96212
96213/*
96214** Free the contents of the With object passed as the second argument.
96215*/
96216SQLITE_PRIVATE void sqlite3WithDelete(sqlite3 *db, With *pWith){
96217  if( pWith ){
96218    int i;
96219    for(i=0; i<pWith->nCte; i++){
96220      struct Cte *pCte = &pWith->a[i];
96221      sqlite3ExprListDelete(db, pCte->pCols);
96222      sqlite3SelectDelete(db, pCte->pSelect);
96223      sqlite3DbFree(db, pCte->zName);
96224    }
96225    sqlite3DbFree(db, pWith);
96226  }
96227}
96228#endif /* !defined(SQLITE_OMIT_CTE) */
96229
96230/************** End of build.c ***********************************************/
96231/************** Begin file callback.c ****************************************/
96232/*
96233** 2005 May 23
96234**
96235** The author disclaims copyright to this source code.  In place of
96236** a legal notice, here is a blessing:
96237**
96238**    May you do good and not evil.
96239**    May you find forgiveness for yourself and forgive others.
96240**    May you share freely, never taking more than you give.
96241**
96242*************************************************************************
96243**
96244** This file contains functions used to access the internal hash tables
96245** of user defined functions and collation sequences.
96246*/
96247
96248/* #include "sqliteInt.h" */
96249
96250/*
96251** Invoke the 'collation needed' callback to request a collation sequence
96252** in the encoding enc of name zName, length nName.
96253*/
96254static void callCollNeeded(sqlite3 *db, int enc, const char *zName){
96255  assert( !db->xCollNeeded || !db->xCollNeeded16 );
96256  if( db->xCollNeeded ){
96257    char *zExternal = sqlite3DbStrDup(db, zName);
96258    if( !zExternal ) return;
96259    db->xCollNeeded(db->pCollNeededArg, db, enc, zExternal);
96260    sqlite3DbFree(db, zExternal);
96261  }
96262#ifndef SQLITE_OMIT_UTF16
96263  if( db->xCollNeeded16 ){
96264    char const *zExternal;
96265    sqlite3_value *pTmp = sqlite3ValueNew(db);
96266    sqlite3ValueSetStr(pTmp, -1, zName, SQLITE_UTF8, SQLITE_STATIC);
96267    zExternal = sqlite3ValueText(pTmp, SQLITE_UTF16NATIVE);
96268    if( zExternal ){
96269      db->xCollNeeded16(db->pCollNeededArg, db, (int)ENC(db), zExternal);
96270    }
96271    sqlite3ValueFree(pTmp);
96272  }
96273#endif
96274}
96275
96276/*
96277** This routine is called if the collation factory fails to deliver a
96278** collation function in the best encoding but there may be other versions
96279** of this collation function (for other text encodings) available. Use one
96280** of these instead if they exist. Avoid a UTF-8 <-> UTF-16 conversion if
96281** possible.
96282*/
96283static int synthCollSeq(sqlite3 *db, CollSeq *pColl){
96284  CollSeq *pColl2;
96285  char *z = pColl->zName;
96286  int i;
96287  static const u8 aEnc[] = { SQLITE_UTF16BE, SQLITE_UTF16LE, SQLITE_UTF8 };
96288  for(i=0; i<3; i++){
96289    pColl2 = sqlite3FindCollSeq(db, aEnc[i], z, 0);
96290    if( pColl2->xCmp!=0 ){
96291      memcpy(pColl, pColl2, sizeof(CollSeq));
96292      pColl->xDel = 0;         /* Do not copy the destructor */
96293      return SQLITE_OK;
96294    }
96295  }
96296  return SQLITE_ERROR;
96297}
96298
96299/*
96300** This function is responsible for invoking the collation factory callback
96301** or substituting a collation sequence of a different encoding when the
96302** requested collation sequence is not available in the desired encoding.
96303**
96304** If it is not NULL, then pColl must point to the database native encoding
96305** collation sequence with name zName, length nName.
96306**
96307** The return value is either the collation sequence to be used in database
96308** db for collation type name zName, length nName, or NULL, if no collation
96309** sequence can be found.  If no collation is found, leave an error message.
96310**
96311** See also: sqlite3LocateCollSeq(), sqlite3FindCollSeq()
96312*/
96313SQLITE_PRIVATE CollSeq *sqlite3GetCollSeq(
96314  Parse *pParse,        /* Parsing context */
96315  u8 enc,               /* The desired encoding for the collating sequence */
96316  CollSeq *pColl,       /* Collating sequence with native encoding, or NULL */
96317  const char *zName     /* Collating sequence name */
96318){
96319  CollSeq *p;
96320  sqlite3 *db = pParse->db;
96321
96322  p = pColl;
96323  if( !p ){
96324    p = sqlite3FindCollSeq(db, enc, zName, 0);
96325  }
96326  if( !p || !p->xCmp ){
96327    /* No collation sequence of this type for this encoding is registered.
96328    ** Call the collation factory to see if it can supply us with one.
96329    */
96330    callCollNeeded(db, enc, zName);
96331    p = sqlite3FindCollSeq(db, enc, zName, 0);
96332  }
96333  if( p && !p->xCmp && synthCollSeq(db, p) ){
96334    p = 0;
96335  }
96336  assert( !p || p->xCmp );
96337  if( p==0 ){
96338    sqlite3ErrorMsg(pParse, "no such collation sequence: %s", zName);
96339  }
96340  return p;
96341}
96342
96343/*
96344** This routine is called on a collation sequence before it is used to
96345** check that it is defined. An undefined collation sequence exists when
96346** a database is loaded that contains references to collation sequences
96347** that have not been defined by sqlite3_create_collation() etc.
96348**
96349** If required, this routine calls the 'collation needed' callback to
96350** request a definition of the collating sequence. If this doesn't work,
96351** an equivalent collating sequence that uses a text encoding different
96352** from the main database is substituted, if one is available.
96353*/
96354SQLITE_PRIVATE int sqlite3CheckCollSeq(Parse *pParse, CollSeq *pColl){
96355  if( pColl ){
96356    const char *zName = pColl->zName;
96357    sqlite3 *db = pParse->db;
96358    CollSeq *p = sqlite3GetCollSeq(pParse, ENC(db), pColl, zName);
96359    if( !p ){
96360      return SQLITE_ERROR;
96361    }
96362    assert( p==pColl );
96363  }
96364  return SQLITE_OK;
96365}
96366
96367
96368
96369/*
96370** Locate and return an entry from the db.aCollSeq hash table. If the entry
96371** specified by zName and nName is not found and parameter 'create' is
96372** true, then create a new entry. Otherwise return NULL.
96373**
96374** Each pointer stored in the sqlite3.aCollSeq hash table contains an
96375** array of three CollSeq structures. The first is the collation sequence
96376** preferred for UTF-8, the second UTF-16le, and the third UTF-16be.
96377**
96378** Stored immediately after the three collation sequences is a copy of
96379** the collation sequence name. A pointer to this string is stored in
96380** each collation sequence structure.
96381*/
96382static CollSeq *findCollSeqEntry(
96383  sqlite3 *db,          /* Database connection */
96384  const char *zName,    /* Name of the collating sequence */
96385  int create            /* Create a new entry if true */
96386){
96387  CollSeq *pColl;
96388  pColl = sqlite3HashFind(&db->aCollSeq, zName);
96389
96390  if( 0==pColl && create ){
96391    int nName = sqlite3Strlen30(zName);
96392    pColl = sqlite3DbMallocZero(db, 3*sizeof(*pColl) + nName + 1);
96393    if( pColl ){
96394      CollSeq *pDel = 0;
96395      pColl[0].zName = (char*)&pColl[3];
96396      pColl[0].enc = SQLITE_UTF8;
96397      pColl[1].zName = (char*)&pColl[3];
96398      pColl[1].enc = SQLITE_UTF16LE;
96399      pColl[2].zName = (char*)&pColl[3];
96400      pColl[2].enc = SQLITE_UTF16BE;
96401      memcpy(pColl[0].zName, zName, nName);
96402      pColl[0].zName[nName] = 0;
96403      pDel = sqlite3HashInsert(&db->aCollSeq, pColl[0].zName, pColl);
96404
96405      /* If a malloc() failure occurred in sqlite3HashInsert(), it will
96406      ** return the pColl pointer to be deleted (because it wasn't added
96407      ** to the hash table).
96408      */
96409      assert( pDel==0 || pDel==pColl );
96410      if( pDel!=0 ){
96411        db->mallocFailed = 1;
96412        sqlite3DbFree(db, pDel);
96413        pColl = 0;
96414      }
96415    }
96416  }
96417  return pColl;
96418}
96419
96420/*
96421** Parameter zName points to a UTF-8 encoded string nName bytes long.
96422** Return the CollSeq* pointer for the collation sequence named zName
96423** for the encoding 'enc' from the database 'db'.
96424**
96425** If the entry specified is not found and 'create' is true, then create a
96426** new entry.  Otherwise return NULL.
96427**
96428** A separate function sqlite3LocateCollSeq() is a wrapper around
96429** this routine.  sqlite3LocateCollSeq() invokes the collation factory
96430** if necessary and generates an error message if the collating sequence
96431** cannot be found.
96432**
96433** See also: sqlite3LocateCollSeq(), sqlite3GetCollSeq()
96434*/
96435SQLITE_PRIVATE CollSeq *sqlite3FindCollSeq(
96436  sqlite3 *db,
96437  u8 enc,
96438  const char *zName,
96439  int create
96440){
96441  CollSeq *pColl;
96442  if( zName ){
96443    pColl = findCollSeqEntry(db, zName, create);
96444  }else{
96445    pColl = db->pDfltColl;
96446  }
96447  assert( SQLITE_UTF8==1 && SQLITE_UTF16LE==2 && SQLITE_UTF16BE==3 );
96448  assert( enc>=SQLITE_UTF8 && enc<=SQLITE_UTF16BE );
96449  if( pColl ) pColl += enc-1;
96450  return pColl;
96451}
96452
96453/* During the search for the best function definition, this procedure
96454** is called to test how well the function passed as the first argument
96455** matches the request for a function with nArg arguments in a system
96456** that uses encoding enc. The value returned indicates how well the
96457** request is matched. A higher value indicates a better match.
96458**
96459** If nArg is -1 that means to only return a match (non-zero) if p->nArg
96460** is also -1.  In other words, we are searching for a function that
96461** takes a variable number of arguments.
96462**
96463** If nArg is -2 that means that we are searching for any function
96464** regardless of the number of arguments it uses, so return a positive
96465** match score for any
96466**
96467** The returned value is always between 0 and 6, as follows:
96468**
96469** 0: Not a match.
96470** 1: UTF8/16 conversion required and function takes any number of arguments.
96471** 2: UTF16 byte order change required and function takes any number of args.
96472** 3: encoding matches and function takes any number of arguments
96473** 4: UTF8/16 conversion required - argument count matches exactly
96474** 5: UTF16 byte order conversion required - argument count matches exactly
96475** 6: Perfect match:  encoding and argument count match exactly.
96476**
96477** If nArg==(-2) then any function with a non-null xStep or xFunc is
96478** a perfect match and any function with both xStep and xFunc NULL is
96479** a non-match.
96480*/
96481#define FUNC_PERFECT_MATCH 6  /* The score for a perfect match */
96482static int matchQuality(
96483  FuncDef *p,     /* The function we are evaluating for match quality */
96484  int nArg,       /* Desired number of arguments.  (-1)==any */
96485  u8 enc          /* Desired text encoding */
96486){
96487  int match;
96488
96489  /* nArg of -2 is a special case */
96490  if( nArg==(-2) ) return (p->xFunc==0 && p->xStep==0) ? 0 : FUNC_PERFECT_MATCH;
96491
96492  /* Wrong number of arguments means "no match" */
96493  if( p->nArg!=nArg && p->nArg>=0 ) return 0;
96494
96495  /* Give a better score to a function with a specific number of arguments
96496  ** than to function that accepts any number of arguments. */
96497  if( p->nArg==nArg ){
96498    match = 4;
96499  }else{
96500    match = 1;
96501  }
96502
96503  /* Bonus points if the text encoding matches */
96504  if( enc==(p->funcFlags & SQLITE_FUNC_ENCMASK) ){
96505    match += 2;  /* Exact encoding match */
96506  }else if( (enc & p->funcFlags & 2)!=0 ){
96507    match += 1;  /* Both are UTF16, but with different byte orders */
96508  }
96509
96510  return match;
96511}
96512
96513/*
96514** Search a FuncDefHash for a function with the given name.  Return
96515** a pointer to the matching FuncDef if found, or 0 if there is no match.
96516*/
96517static FuncDef *functionSearch(
96518  FuncDefHash *pHash,  /* Hash table to search */
96519  int h,               /* Hash of the name */
96520  const char *zFunc,   /* Name of function */
96521  int nFunc            /* Number of bytes in zFunc */
96522){
96523  FuncDef *p;
96524  for(p=pHash->a[h]; p; p=p->pHash){
96525    if( sqlite3StrNICmp(p->zName, zFunc, nFunc)==0 && p->zName[nFunc]==0 ){
96526      return p;
96527    }
96528  }
96529  return 0;
96530}
96531
96532/*
96533** Insert a new FuncDef into a FuncDefHash hash table.
96534*/
96535SQLITE_PRIVATE void sqlite3FuncDefInsert(
96536  FuncDefHash *pHash,  /* The hash table into which to insert */
96537  FuncDef *pDef        /* The function definition to insert */
96538){
96539  FuncDef *pOther;
96540  int nName = sqlite3Strlen30(pDef->zName);
96541  u8 c1 = (u8)pDef->zName[0];
96542  int h = (sqlite3UpperToLower[c1] + nName) % ArraySize(pHash->a);
96543  pOther = functionSearch(pHash, h, pDef->zName, nName);
96544  if( pOther ){
96545    assert( pOther!=pDef && pOther->pNext!=pDef );
96546    pDef->pNext = pOther->pNext;
96547    pOther->pNext = pDef;
96548  }else{
96549    pDef->pNext = 0;
96550    pDef->pHash = pHash->a[h];
96551    pHash->a[h] = pDef;
96552  }
96553}
96554
96555
96556
96557/*
96558** Locate a user function given a name, a number of arguments and a flag
96559** indicating whether the function prefers UTF-16 over UTF-8.  Return a
96560** pointer to the FuncDef structure that defines that function, or return
96561** NULL if the function does not exist.
96562**
96563** If the createFlag argument is true, then a new (blank) FuncDef
96564** structure is created and liked into the "db" structure if a
96565** no matching function previously existed.
96566**
96567** If nArg is -2, then the first valid function found is returned.  A
96568** function is valid if either xFunc or xStep is non-zero.  The nArg==(-2)
96569** case is used to see if zName is a valid function name for some number
96570** of arguments.  If nArg is -2, then createFlag must be 0.
96571**
96572** If createFlag is false, then a function with the required name and
96573** number of arguments may be returned even if the eTextRep flag does not
96574** match that requested.
96575*/
96576SQLITE_PRIVATE FuncDef *sqlite3FindFunction(
96577  sqlite3 *db,       /* An open database */
96578  const char *zName, /* Name of the function.  Not null-terminated */
96579  int nName,         /* Number of characters in the name */
96580  int nArg,          /* Number of arguments.  -1 means any number */
96581  u8 enc,            /* Preferred text encoding */
96582  u8 createFlag      /* Create new entry if true and does not otherwise exist */
96583){
96584  FuncDef *p;         /* Iterator variable */
96585  FuncDef *pBest = 0; /* Best match found so far */
96586  int bestScore = 0;  /* Score of best match */
96587  int h;              /* Hash value */
96588
96589  assert( nArg>=(-2) );
96590  assert( nArg>=(-1) || createFlag==0 );
96591  h = (sqlite3UpperToLower[(u8)zName[0]] + nName) % ArraySize(db->aFunc.a);
96592
96593  /* First search for a match amongst the application-defined functions.
96594  */
96595  p = functionSearch(&db->aFunc, h, zName, nName);
96596  while( p ){
96597    int score = matchQuality(p, nArg, enc);
96598    if( score>bestScore ){
96599      pBest = p;
96600      bestScore = score;
96601    }
96602    p = p->pNext;
96603  }
96604
96605  /* If no match is found, search the built-in functions.
96606  **
96607  ** If the SQLITE_PreferBuiltin flag is set, then search the built-in
96608  ** functions even if a prior app-defined function was found.  And give
96609  ** priority to built-in functions.
96610  **
96611  ** Except, if createFlag is true, that means that we are trying to
96612  ** install a new function.  Whatever FuncDef structure is returned it will
96613  ** have fields overwritten with new information appropriate for the
96614  ** new function.  But the FuncDefs for built-in functions are read-only.
96615  ** So we must not search for built-ins when creating a new function.
96616  */
96617  if( !createFlag && (pBest==0 || (db->flags & SQLITE_PreferBuiltin)!=0) ){
96618    FuncDefHash *pHash = &GLOBAL(FuncDefHash, sqlite3GlobalFunctions);
96619    bestScore = 0;
96620    p = functionSearch(pHash, h, zName, nName);
96621    while( p ){
96622      int score = matchQuality(p, nArg, enc);
96623      if( score>bestScore ){
96624        pBest = p;
96625        bestScore = score;
96626      }
96627      p = p->pNext;
96628    }
96629  }
96630
96631  /* If the createFlag parameter is true and the search did not reveal an
96632  ** exact match for the name, number of arguments and encoding, then add a
96633  ** new entry to the hash table and return it.
96634  */
96635  if( createFlag && bestScore<FUNC_PERFECT_MATCH &&
96636      (pBest = sqlite3DbMallocZero(db, sizeof(*pBest)+nName+1))!=0 ){
96637    pBest->zName = (char *)&pBest[1];
96638    pBest->nArg = (u16)nArg;
96639    pBest->funcFlags = enc;
96640    memcpy(pBest->zName, zName, nName);
96641    pBest->zName[nName] = 0;
96642    sqlite3FuncDefInsert(&db->aFunc, pBest);
96643  }
96644
96645  if( pBest && (pBest->xStep || pBest->xFunc || createFlag) ){
96646    return pBest;
96647  }
96648  return 0;
96649}
96650
96651/*
96652** Free all resources held by the schema structure. The void* argument points
96653** at a Schema struct. This function does not call sqlite3DbFree(db, ) on the
96654** pointer itself, it just cleans up subsidiary resources (i.e. the contents
96655** of the schema hash tables).
96656**
96657** The Schema.cache_size variable is not cleared.
96658*/
96659SQLITE_PRIVATE void sqlite3SchemaClear(void *p){
96660  Hash temp1;
96661  Hash temp2;
96662  HashElem *pElem;
96663  Schema *pSchema = (Schema *)p;
96664
96665  temp1 = pSchema->tblHash;
96666  temp2 = pSchema->trigHash;
96667  sqlite3HashInit(&pSchema->trigHash);
96668  sqlite3HashClear(&pSchema->idxHash);
96669  for(pElem=sqliteHashFirst(&temp2); pElem; pElem=sqliteHashNext(pElem)){
96670    sqlite3DeleteTrigger(0, (Trigger*)sqliteHashData(pElem));
96671  }
96672  sqlite3HashClear(&temp2);
96673  sqlite3HashInit(&pSchema->tblHash);
96674  for(pElem=sqliteHashFirst(&temp1); pElem; pElem=sqliteHashNext(pElem)){
96675    Table *pTab = sqliteHashData(pElem);
96676    sqlite3DeleteTable(0, pTab);
96677  }
96678  sqlite3HashClear(&temp1);
96679  sqlite3HashClear(&pSchema->fkeyHash);
96680  pSchema->pSeqTab = 0;
96681  if( pSchema->schemaFlags & DB_SchemaLoaded ){
96682    pSchema->iGeneration++;
96683    pSchema->schemaFlags &= ~DB_SchemaLoaded;
96684  }
96685}
96686
96687/*
96688** Find and return the schema associated with a BTree.  Create
96689** a new one if necessary.
96690*/
96691SQLITE_PRIVATE Schema *sqlite3SchemaGet(sqlite3 *db, Btree *pBt){
96692  Schema * p;
96693  if( pBt ){
96694    p = (Schema *)sqlite3BtreeSchema(pBt, sizeof(Schema), sqlite3SchemaClear);
96695  }else{
96696    p = (Schema *)sqlite3DbMallocZero(0, sizeof(Schema));
96697  }
96698  if( !p ){
96699    db->mallocFailed = 1;
96700  }else if ( 0==p->file_format ){
96701    sqlite3HashInit(&p->tblHash);
96702    sqlite3HashInit(&p->idxHash);
96703    sqlite3HashInit(&p->trigHash);
96704    sqlite3HashInit(&p->fkeyHash);
96705    p->enc = SQLITE_UTF8;
96706  }
96707  return p;
96708}
96709
96710/************** End of callback.c ********************************************/
96711/************** Begin file delete.c ******************************************/
96712/*
96713** 2001 September 15
96714**
96715** The author disclaims copyright to this source code.  In place of
96716** a legal notice, here is a blessing:
96717**
96718**    May you do good and not evil.
96719**    May you find forgiveness for yourself and forgive others.
96720**    May you share freely, never taking more than you give.
96721**
96722*************************************************************************
96723** This file contains C code routines that are called by the parser
96724** in order to generate code for DELETE FROM statements.
96725*/
96726/* #include "sqliteInt.h" */
96727
96728/*
96729** While a SrcList can in general represent multiple tables and subqueries
96730** (as in the FROM clause of a SELECT statement) in this case it contains
96731** the name of a single table, as one might find in an INSERT, DELETE,
96732** or UPDATE statement.  Look up that table in the symbol table and
96733** return a pointer.  Set an error message and return NULL if the table
96734** name is not found or if any other error occurs.
96735**
96736** The following fields are initialized appropriate in pSrc:
96737**
96738**    pSrc->a[0].pTab       Pointer to the Table object
96739**    pSrc->a[0].pIndex     Pointer to the INDEXED BY index, if there is one
96740**
96741*/
96742SQLITE_PRIVATE Table *sqlite3SrcListLookup(Parse *pParse, SrcList *pSrc){
96743  struct SrcList_item *pItem = pSrc->a;
96744  Table *pTab;
96745  assert( pItem && pSrc->nSrc==1 );
96746  pTab = sqlite3LocateTableItem(pParse, 0, pItem);
96747  sqlite3DeleteTable(pParse->db, pItem->pTab);
96748  pItem->pTab = pTab;
96749  if( pTab ){
96750    pTab->nRef++;
96751  }
96752  if( sqlite3IndexedByLookup(pParse, pItem) ){
96753    pTab = 0;
96754  }
96755  return pTab;
96756}
96757
96758/*
96759** Check to make sure the given table is writable.  If it is not
96760** writable, generate an error message and return 1.  If it is
96761** writable return 0;
96762*/
96763SQLITE_PRIVATE int sqlite3IsReadOnly(Parse *pParse, Table *pTab, int viewOk){
96764  /* A table is not writable under the following circumstances:
96765  **
96766  **   1) It is a virtual table and no implementation of the xUpdate method
96767  **      has been provided, or
96768  **   2) It is a system table (i.e. sqlite_master), this call is not
96769  **      part of a nested parse and writable_schema pragma has not
96770  **      been specified.
96771  **
96772  ** In either case leave an error message in pParse and return non-zero.
96773  */
96774  if( ( IsVirtual(pTab)
96775     && sqlite3GetVTable(pParse->db, pTab)->pMod->pModule->xUpdate==0 )
96776   || ( (pTab->tabFlags & TF_Readonly)!=0
96777     && (pParse->db->flags & SQLITE_WriteSchema)==0
96778     && pParse->nested==0 )
96779  ){
96780    sqlite3ErrorMsg(pParse, "table %s may not be modified", pTab->zName);
96781    return 1;
96782  }
96783
96784#ifndef SQLITE_OMIT_VIEW
96785  if( !viewOk && pTab->pSelect ){
96786    sqlite3ErrorMsg(pParse,"cannot modify %s because it is a view",pTab->zName);
96787    return 1;
96788  }
96789#endif
96790  return 0;
96791}
96792
96793
96794#if !defined(SQLITE_OMIT_VIEW) && !defined(SQLITE_OMIT_TRIGGER)
96795/*
96796** Evaluate a view and store its result in an ephemeral table.  The
96797** pWhere argument is an optional WHERE clause that restricts the
96798** set of rows in the view that are to be added to the ephemeral table.
96799*/
96800SQLITE_PRIVATE void sqlite3MaterializeView(
96801  Parse *pParse,       /* Parsing context */
96802  Table *pView,        /* View definition */
96803  Expr *pWhere,        /* Optional WHERE clause to be added */
96804  int iCur             /* Cursor number for ephemeral table */
96805){
96806  SelectDest dest;
96807  Select *pSel;
96808  SrcList *pFrom;
96809  sqlite3 *db = pParse->db;
96810  int iDb = sqlite3SchemaToIndex(db, pView->pSchema);
96811  pWhere = sqlite3ExprDup(db, pWhere, 0);
96812  pFrom = sqlite3SrcListAppend(db, 0, 0, 0);
96813  if( pFrom ){
96814    assert( pFrom->nSrc==1 );
96815    pFrom->a[0].zName = sqlite3DbStrDup(db, pView->zName);
96816    pFrom->a[0].zDatabase = sqlite3DbStrDup(db, db->aDb[iDb].zName);
96817    assert( pFrom->a[0].pOn==0 );
96818    assert( pFrom->a[0].pUsing==0 );
96819  }
96820  pSel = sqlite3SelectNew(pParse, 0, pFrom, pWhere, 0, 0, 0, 0, 0, 0);
96821  sqlite3SelectDestInit(&dest, SRT_EphemTab, iCur);
96822  sqlite3Select(pParse, pSel, &dest);
96823  sqlite3SelectDelete(db, pSel);
96824}
96825#endif /* !defined(SQLITE_OMIT_VIEW) && !defined(SQLITE_OMIT_TRIGGER) */
96826
96827#if defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT) && !defined(SQLITE_OMIT_SUBQUERY)
96828/*
96829** Generate an expression tree to implement the WHERE, ORDER BY,
96830** and LIMIT/OFFSET portion of DELETE and UPDATE statements.
96831**
96832**     DELETE FROM table_wxyz WHERE a<5 ORDER BY a LIMIT 1;
96833**                            \__________________________/
96834**                               pLimitWhere (pInClause)
96835*/
96836SQLITE_PRIVATE Expr *sqlite3LimitWhere(
96837  Parse *pParse,               /* The parser context */
96838  SrcList *pSrc,               /* the FROM clause -- which tables to scan */
96839  Expr *pWhere,                /* The WHERE clause.  May be null */
96840  ExprList *pOrderBy,          /* The ORDER BY clause.  May be null */
96841  Expr *pLimit,                /* The LIMIT clause.  May be null */
96842  Expr *pOffset,               /* The OFFSET clause.  May be null */
96843  char *zStmtType              /* Either DELETE or UPDATE.  For err msgs. */
96844){
96845  Expr *pWhereRowid = NULL;    /* WHERE rowid .. */
96846  Expr *pInClause = NULL;      /* WHERE rowid IN ( select ) */
96847  Expr *pSelectRowid = NULL;   /* SELECT rowid ... */
96848  ExprList *pEList = NULL;     /* Expression list contaning only pSelectRowid */
96849  SrcList *pSelectSrc = NULL;  /* SELECT rowid FROM x ... (dup of pSrc) */
96850  Select *pSelect = NULL;      /* Complete SELECT tree */
96851
96852  /* Check that there isn't an ORDER BY without a LIMIT clause.
96853  */
96854  if( pOrderBy && (pLimit == 0) ) {
96855    sqlite3ErrorMsg(pParse, "ORDER BY without LIMIT on %s", zStmtType);
96856    goto limit_where_cleanup_2;
96857  }
96858
96859  /* We only need to generate a select expression if there
96860  ** is a limit/offset term to enforce.
96861  */
96862  if( pLimit == 0 ) {
96863    /* if pLimit is null, pOffset will always be null as well. */
96864    assert( pOffset == 0 );
96865    return pWhere;
96866  }
96867
96868  /* Generate a select expression tree to enforce the limit/offset
96869  ** term for the DELETE or UPDATE statement.  For example:
96870  **   DELETE FROM table_a WHERE col1=1 ORDER BY col2 LIMIT 1 OFFSET 1
96871  ** becomes:
96872  **   DELETE FROM table_a WHERE rowid IN (
96873  **     SELECT rowid FROM table_a WHERE col1=1 ORDER BY col2 LIMIT 1 OFFSET 1
96874  **   );
96875  */
96876
96877  pSelectRowid = sqlite3PExpr(pParse, TK_ROW, 0, 0, 0);
96878  if( pSelectRowid == 0 ) goto limit_where_cleanup_2;
96879  pEList = sqlite3ExprListAppend(pParse, 0, pSelectRowid);
96880  if( pEList == 0 ) goto limit_where_cleanup_2;
96881
96882  /* duplicate the FROM clause as it is needed by both the DELETE/UPDATE tree
96883  ** and the SELECT subtree. */
96884  pSelectSrc = sqlite3SrcListDup(pParse->db, pSrc, 0);
96885  if( pSelectSrc == 0 ) {
96886    sqlite3ExprListDelete(pParse->db, pEList);
96887    goto limit_where_cleanup_2;
96888  }
96889
96890  /* generate the SELECT expression tree. */
96891  pSelect = sqlite3SelectNew(pParse,pEList,pSelectSrc,pWhere,0,0,
96892                             pOrderBy,0,pLimit,pOffset);
96893  if( pSelect == 0 ) return 0;
96894
96895  /* now generate the new WHERE rowid IN clause for the DELETE/UDPATE */
96896  pWhereRowid = sqlite3PExpr(pParse, TK_ROW, 0, 0, 0);
96897  if( pWhereRowid == 0 ) goto limit_where_cleanup_1;
96898  pInClause = sqlite3PExpr(pParse, TK_IN, pWhereRowid, 0, 0);
96899  if( pInClause == 0 ) goto limit_where_cleanup_1;
96900
96901  pInClause->x.pSelect = pSelect;
96902  pInClause->flags |= EP_xIsSelect;
96903  sqlite3ExprSetHeightAndFlags(pParse, pInClause);
96904  return pInClause;
96905
96906  /* something went wrong. clean up anything allocated. */
96907limit_where_cleanup_1:
96908  sqlite3SelectDelete(pParse->db, pSelect);
96909  return 0;
96910
96911limit_where_cleanup_2:
96912  sqlite3ExprDelete(pParse->db, pWhere);
96913  sqlite3ExprListDelete(pParse->db, pOrderBy);
96914  sqlite3ExprDelete(pParse->db, pLimit);
96915  sqlite3ExprDelete(pParse->db, pOffset);
96916  return 0;
96917}
96918#endif /* defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT) */
96919       /*      && !defined(SQLITE_OMIT_SUBQUERY) */
96920
96921/*
96922** Generate code for a DELETE FROM statement.
96923**
96924**     DELETE FROM table_wxyz WHERE a<5 AND b NOT NULL;
96925**                 \________/       \________________/
96926**                  pTabList              pWhere
96927*/
96928SQLITE_PRIVATE void sqlite3DeleteFrom(
96929  Parse *pParse,         /* The parser context */
96930  SrcList *pTabList,     /* The table from which we should delete things */
96931  Expr *pWhere           /* The WHERE clause.  May be null */
96932){
96933  Vdbe *v;               /* The virtual database engine */
96934  Table *pTab;           /* The table from which records will be deleted */
96935  const char *zDb;       /* Name of database holding pTab */
96936  int i;                 /* Loop counter */
96937  WhereInfo *pWInfo;     /* Information about the WHERE clause */
96938  Index *pIdx;           /* For looping over indices of the table */
96939  int iTabCur;           /* Cursor number for the table */
96940  int iDataCur = 0;      /* VDBE cursor for the canonical data source */
96941  int iIdxCur = 0;       /* Cursor number of the first index */
96942  int nIdx;              /* Number of indices */
96943  sqlite3 *db;           /* Main database structure */
96944  AuthContext sContext;  /* Authorization context */
96945  NameContext sNC;       /* Name context to resolve expressions in */
96946  int iDb;               /* Database number */
96947  int memCnt = -1;       /* Memory cell used for change counting */
96948  int rcauth;            /* Value returned by authorization callback */
96949  int okOnePass;         /* True for one-pass algorithm without the FIFO */
96950  int aiCurOnePass[2];   /* The write cursors opened by WHERE_ONEPASS */
96951  u8 *aToOpen = 0;       /* Open cursor iTabCur+j if aToOpen[j] is true */
96952  Index *pPk;            /* The PRIMARY KEY index on the table */
96953  int iPk = 0;           /* First of nPk registers holding PRIMARY KEY value */
96954  i16 nPk = 1;           /* Number of columns in the PRIMARY KEY */
96955  int iKey;              /* Memory cell holding key of row to be deleted */
96956  i16 nKey;              /* Number of memory cells in the row key */
96957  int iEphCur = 0;       /* Ephemeral table holding all primary key values */
96958  int iRowSet = 0;       /* Register for rowset of rows to delete */
96959  int addrBypass = 0;    /* Address of jump over the delete logic */
96960  int addrLoop = 0;      /* Top of the delete loop */
96961  int addrDelete = 0;    /* Jump directly to the delete logic */
96962  int addrEphOpen = 0;   /* Instruction to open the Ephemeral table */
96963
96964#ifndef SQLITE_OMIT_TRIGGER
96965  int isView;                  /* True if attempting to delete from a view */
96966  Trigger *pTrigger;           /* List of table triggers, if required */
96967#endif
96968
96969  memset(&sContext, 0, sizeof(sContext));
96970  db = pParse->db;
96971  if( pParse->nErr || db->mallocFailed ){
96972    goto delete_from_cleanup;
96973  }
96974  assert( pTabList->nSrc==1 );
96975
96976  /* Locate the table which we want to delete.  This table has to be
96977  ** put in an SrcList structure because some of the subroutines we
96978  ** will be calling are designed to work with multiple tables and expect
96979  ** an SrcList* parameter instead of just a Table* parameter.
96980  */
96981  pTab = sqlite3SrcListLookup(pParse, pTabList);
96982  if( pTab==0 )  goto delete_from_cleanup;
96983
96984  /* Figure out if we have any triggers and if the table being
96985  ** deleted from is a view
96986  */
96987#ifndef SQLITE_OMIT_TRIGGER
96988  pTrigger = sqlite3TriggersExist(pParse, pTab, TK_DELETE, 0, 0);
96989  isView = pTab->pSelect!=0;
96990#else
96991# define pTrigger 0
96992# define isView 0
96993#endif
96994#ifdef SQLITE_OMIT_VIEW
96995# undef isView
96996# define isView 0
96997#endif
96998
96999  /* If pTab is really a view, make sure it has been initialized.
97000  */
97001  if( sqlite3ViewGetColumnNames(pParse, pTab) ){
97002    goto delete_from_cleanup;
97003  }
97004
97005  if( sqlite3IsReadOnly(pParse, pTab, (pTrigger?1:0)) ){
97006    goto delete_from_cleanup;
97007  }
97008  iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
97009  assert( iDb<db->nDb );
97010  zDb = db->aDb[iDb].zName;
97011  rcauth = sqlite3AuthCheck(pParse, SQLITE_DELETE, pTab->zName, 0, zDb);
97012  assert( rcauth==SQLITE_OK || rcauth==SQLITE_DENY || rcauth==SQLITE_IGNORE );
97013  if( rcauth==SQLITE_DENY ){
97014    goto delete_from_cleanup;
97015  }
97016  assert(!isView || pTrigger);
97017
97018  /* Assign cursor numbers to the table and all its indices.
97019  */
97020  assert( pTabList->nSrc==1 );
97021  iTabCur = pTabList->a[0].iCursor = pParse->nTab++;
97022  for(nIdx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nIdx++){
97023    pParse->nTab++;
97024  }
97025
97026  /* Start the view context
97027  */
97028  if( isView ){
97029    sqlite3AuthContextPush(pParse, &sContext, pTab->zName);
97030  }
97031
97032  /* Begin generating code.
97033  */
97034  v = sqlite3GetVdbe(pParse);
97035  if( v==0 ){
97036    goto delete_from_cleanup;
97037  }
97038  if( pParse->nested==0 ) sqlite3VdbeCountChanges(v);
97039  sqlite3BeginWriteOperation(pParse, 1, iDb);
97040
97041  /* If we are trying to delete from a view, realize that view into
97042  ** an ephemeral table.
97043  */
97044#if !defined(SQLITE_OMIT_VIEW) && !defined(SQLITE_OMIT_TRIGGER)
97045  if( isView ){
97046    sqlite3MaterializeView(pParse, pTab, pWhere, iTabCur);
97047    iDataCur = iIdxCur = iTabCur;
97048  }
97049#endif
97050
97051  /* Resolve the column names in the WHERE clause.
97052  */
97053  memset(&sNC, 0, sizeof(sNC));
97054  sNC.pParse = pParse;
97055  sNC.pSrcList = pTabList;
97056  if( sqlite3ResolveExprNames(&sNC, pWhere) ){
97057    goto delete_from_cleanup;
97058  }
97059
97060  /* Initialize the counter of the number of rows deleted, if
97061  ** we are counting rows.
97062  */
97063  if( db->flags & SQLITE_CountRows ){
97064    memCnt = ++pParse->nMem;
97065    sqlite3VdbeAddOp2(v, OP_Integer, 0, memCnt);
97066  }
97067
97068#ifndef SQLITE_OMIT_TRUNCATE_OPTIMIZATION
97069  /* Special case: A DELETE without a WHERE clause deletes everything.
97070  ** It is easier just to erase the whole table. Prior to version 3.6.5,
97071  ** this optimization caused the row change count (the value returned by
97072  ** API function sqlite3_count_changes) to be set incorrectly.  */
97073  if( rcauth==SQLITE_OK && pWhere==0 && !pTrigger && !IsVirtual(pTab)
97074   && 0==sqlite3FkRequired(pParse, pTab, 0, 0)
97075  ){
97076    assert( !isView );
97077    sqlite3TableLock(pParse, iDb, pTab->tnum, 1, pTab->zName);
97078    if( HasRowid(pTab) ){
97079      sqlite3VdbeAddOp4(v, OP_Clear, pTab->tnum, iDb, memCnt,
97080                        pTab->zName, P4_STATIC);
97081    }
97082    for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
97083      assert( pIdx->pSchema==pTab->pSchema );
97084      sqlite3VdbeAddOp2(v, OP_Clear, pIdx->tnum, iDb);
97085    }
97086  }else
97087#endif /* SQLITE_OMIT_TRUNCATE_OPTIMIZATION */
97088  {
97089    if( HasRowid(pTab) ){
97090      /* For a rowid table, initialize the RowSet to an empty set */
97091      pPk = 0;
97092      nPk = 1;
97093      iRowSet = ++pParse->nMem;
97094      sqlite3VdbeAddOp2(v, OP_Null, 0, iRowSet);
97095    }else{
97096      /* For a WITHOUT ROWID table, create an ephemeral table used to
97097      ** hold all primary keys for rows to be deleted. */
97098      pPk = sqlite3PrimaryKeyIndex(pTab);
97099      assert( pPk!=0 );
97100      nPk = pPk->nKeyCol;
97101      iPk = pParse->nMem+1;
97102      pParse->nMem += nPk;
97103      iEphCur = pParse->nTab++;
97104      addrEphOpen = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, iEphCur, nPk);
97105      sqlite3VdbeSetP4KeyInfo(pParse, pPk);
97106    }
97107
97108    /* Construct a query to find the rowid or primary key for every row
97109    ** to be deleted, based on the WHERE clause.
97110    */
97111    pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, 0, 0,
97112                               WHERE_ONEPASS_DESIRED|WHERE_DUPLICATES_OK,
97113                               iTabCur+1);
97114    if( pWInfo==0 ) goto delete_from_cleanup;
97115    okOnePass = sqlite3WhereOkOnePass(pWInfo, aiCurOnePass);
97116
97117    /* Keep track of the number of rows to be deleted */
97118    if( db->flags & SQLITE_CountRows ){
97119      sqlite3VdbeAddOp2(v, OP_AddImm, memCnt, 1);
97120    }
97121
97122    /* Extract the rowid or primary key for the current row */
97123    if( pPk ){
97124      for(i=0; i<nPk; i++){
97125        sqlite3ExprCodeGetColumnOfTable(v, pTab, iTabCur,
97126                                        pPk->aiColumn[i], iPk+i);
97127      }
97128      iKey = iPk;
97129    }else{
97130      iKey = pParse->nMem + 1;
97131      iKey = sqlite3ExprCodeGetColumn(pParse, pTab, -1, iTabCur, iKey, 0);
97132      if( iKey>pParse->nMem ) pParse->nMem = iKey;
97133    }
97134
97135    if( okOnePass ){
97136      /* For ONEPASS, no need to store the rowid/primary-key.  There is only
97137      ** one, so just keep it in its register(s) and fall through to the
97138      ** delete code.
97139      */
97140      nKey = nPk; /* OP_Found will use an unpacked key */
97141      aToOpen = sqlite3DbMallocRaw(db, nIdx+2);
97142      if( aToOpen==0 ){
97143        sqlite3WhereEnd(pWInfo);
97144        goto delete_from_cleanup;
97145      }
97146      memset(aToOpen, 1, nIdx+1);
97147      aToOpen[nIdx+1] = 0;
97148      if( aiCurOnePass[0]>=0 ) aToOpen[aiCurOnePass[0]-iTabCur] = 0;
97149      if( aiCurOnePass[1]>=0 ) aToOpen[aiCurOnePass[1]-iTabCur] = 0;
97150      if( addrEphOpen ) sqlite3VdbeChangeToNoop(v, addrEphOpen);
97151      addrDelete = sqlite3VdbeAddOp0(v, OP_Goto); /* Jump to DELETE logic */
97152    }else if( pPk ){
97153      /* Construct a composite key for the row to be deleted and remember it */
97154      iKey = ++pParse->nMem;
97155      nKey = 0;   /* Zero tells OP_Found to use a composite key */
97156      sqlite3VdbeAddOp4(v, OP_MakeRecord, iPk, nPk, iKey,
97157                        sqlite3IndexAffinityStr(v, pPk), nPk);
97158      sqlite3VdbeAddOp2(v, OP_IdxInsert, iEphCur, iKey);
97159    }else{
97160      /* Get the rowid of the row to be deleted and remember it in the RowSet */
97161      nKey = 1;  /* OP_Seek always uses a single rowid */
97162      sqlite3VdbeAddOp2(v, OP_RowSetAdd, iRowSet, iKey);
97163    }
97164
97165    /* End of the WHERE loop */
97166    sqlite3WhereEnd(pWInfo);
97167    if( okOnePass ){
97168      /* Bypass the delete logic below if the WHERE loop found zero rows */
97169      addrBypass = sqlite3VdbeMakeLabel(v);
97170      sqlite3VdbeAddOp2(v, OP_Goto, 0, addrBypass);
97171      sqlite3VdbeJumpHere(v, addrDelete);
97172    }
97173
97174    /* Unless this is a view, open cursors for the table we are
97175    ** deleting from and all its indices. If this is a view, then the
97176    ** only effect this statement has is to fire the INSTEAD OF
97177    ** triggers.
97178    */
97179    if( !isView ){
97180      testcase( IsVirtual(pTab) );
97181      sqlite3OpenTableAndIndices(pParse, pTab, OP_OpenWrite, iTabCur, aToOpen,
97182                                 &iDataCur, &iIdxCur);
97183      assert( pPk || IsVirtual(pTab) || iDataCur==iTabCur );
97184      assert( pPk || IsVirtual(pTab) || iIdxCur==iDataCur+1 );
97185    }
97186
97187    /* Set up a loop over the rowids/primary-keys that were found in the
97188    ** where-clause loop above.
97189    */
97190    if( okOnePass ){
97191      /* Just one row.  Hence the top-of-loop is a no-op */
97192      assert( nKey==nPk );  /* OP_Found will use an unpacked key */
97193      assert( !IsVirtual(pTab) );
97194      if( aToOpen[iDataCur-iTabCur] ){
97195        assert( pPk!=0 || pTab->pSelect!=0 );
97196        sqlite3VdbeAddOp4Int(v, OP_NotFound, iDataCur, addrBypass, iKey, nKey);
97197        VdbeCoverage(v);
97198      }
97199    }else if( pPk ){
97200      addrLoop = sqlite3VdbeAddOp1(v, OP_Rewind, iEphCur); VdbeCoverage(v);
97201      sqlite3VdbeAddOp2(v, OP_RowKey, iEphCur, iKey);
97202      assert( nKey==0 );  /* OP_Found will use a composite key */
97203    }else{
97204      addrLoop = sqlite3VdbeAddOp3(v, OP_RowSetRead, iRowSet, 0, iKey);
97205      VdbeCoverage(v);
97206      assert( nKey==1 );
97207    }
97208
97209    /* Delete the row */
97210#ifndef SQLITE_OMIT_VIRTUALTABLE
97211    if( IsVirtual(pTab) ){
97212      const char *pVTab = (const char *)sqlite3GetVTable(db, pTab);
97213      sqlite3VtabMakeWritable(pParse, pTab);
97214      sqlite3VdbeAddOp4(v, OP_VUpdate, 0, 1, iKey, pVTab, P4_VTAB);
97215      sqlite3VdbeChangeP5(v, OE_Abort);
97216      sqlite3MayAbort(pParse);
97217    }else
97218#endif
97219    {
97220      int count = (pParse->nested==0);    /* True to count changes */
97221      sqlite3GenerateRowDelete(pParse, pTab, pTrigger, iDataCur, iIdxCur,
97222                               iKey, nKey, count, OE_Default, okOnePass);
97223    }
97224
97225    /* End of the loop over all rowids/primary-keys. */
97226    if( okOnePass ){
97227      sqlite3VdbeResolveLabel(v, addrBypass);
97228    }else if( pPk ){
97229      sqlite3VdbeAddOp2(v, OP_Next, iEphCur, addrLoop+1); VdbeCoverage(v);
97230      sqlite3VdbeJumpHere(v, addrLoop);
97231    }else{
97232      sqlite3VdbeAddOp2(v, OP_Goto, 0, addrLoop);
97233      sqlite3VdbeJumpHere(v, addrLoop);
97234    }
97235
97236    /* Close the cursors open on the table and its indexes. */
97237    if( !isView && !IsVirtual(pTab) ){
97238      if( !pPk ) sqlite3VdbeAddOp1(v, OP_Close, iDataCur);
97239      for(i=0, pIdx=pTab->pIndex; pIdx; i++, pIdx=pIdx->pNext){
97240        sqlite3VdbeAddOp1(v, OP_Close, iIdxCur + i);
97241      }
97242    }
97243  } /* End non-truncate path */
97244
97245  /* Update the sqlite_sequence table by storing the content of the
97246  ** maximum rowid counter values recorded while inserting into
97247  ** autoincrement tables.
97248  */
97249  if( pParse->nested==0 && pParse->pTriggerTab==0 ){
97250    sqlite3AutoincrementEnd(pParse);
97251  }
97252
97253  /* Return the number of rows that were deleted. If this routine is
97254  ** generating code because of a call to sqlite3NestedParse(), do not
97255  ** invoke the callback function.
97256  */
97257  if( (db->flags&SQLITE_CountRows) && !pParse->nested && !pParse->pTriggerTab ){
97258    sqlite3VdbeAddOp2(v, OP_ResultRow, memCnt, 1);
97259    sqlite3VdbeSetNumCols(v, 1);
97260    sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "rows deleted", SQLITE_STATIC);
97261  }
97262
97263delete_from_cleanup:
97264  sqlite3AuthContextPop(&sContext);
97265  sqlite3SrcListDelete(db, pTabList);
97266  sqlite3ExprDelete(db, pWhere);
97267  sqlite3DbFree(db, aToOpen);
97268  return;
97269}
97270/* Make sure "isView" and other macros defined above are undefined. Otherwise
97271** they may interfere with compilation of other functions in this file
97272** (or in another file, if this file becomes part of the amalgamation).  */
97273#ifdef isView
97274 #undef isView
97275#endif
97276#ifdef pTrigger
97277 #undef pTrigger
97278#endif
97279
97280/*
97281** This routine generates VDBE code that causes a single row of a
97282** single table to be deleted.  Both the original table entry and
97283** all indices are removed.
97284**
97285** Preconditions:
97286**
97287**   1.  iDataCur is an open cursor on the btree that is the canonical data
97288**       store for the table.  (This will be either the table itself,
97289**       in the case of a rowid table, or the PRIMARY KEY index in the case
97290**       of a WITHOUT ROWID table.)
97291**
97292**   2.  Read/write cursors for all indices of pTab must be open as
97293**       cursor number iIdxCur+i for the i-th index.
97294**
97295**   3.  The primary key for the row to be deleted must be stored in a
97296**       sequence of nPk memory cells starting at iPk.  If nPk==0 that means
97297**       that a search record formed from OP_MakeRecord is contained in the
97298**       single memory location iPk.
97299*/
97300SQLITE_PRIVATE void sqlite3GenerateRowDelete(
97301  Parse *pParse,     /* Parsing context */
97302  Table *pTab,       /* Table containing the row to be deleted */
97303  Trigger *pTrigger, /* List of triggers to (potentially) fire */
97304  int iDataCur,      /* Cursor from which column data is extracted */
97305  int iIdxCur,       /* First index cursor */
97306  int iPk,           /* First memory cell containing the PRIMARY KEY */
97307  i16 nPk,           /* Number of PRIMARY KEY memory cells */
97308  u8 count,          /* If non-zero, increment the row change counter */
97309  u8 onconf,         /* Default ON CONFLICT policy for triggers */
97310  u8 bNoSeek         /* iDataCur is already pointing to the row to delete */
97311){
97312  Vdbe *v = pParse->pVdbe;        /* Vdbe */
97313  int iOld = 0;                   /* First register in OLD.* array */
97314  int iLabel;                     /* Label resolved to end of generated code */
97315  u8 opSeek;                      /* Seek opcode */
97316
97317  /* Vdbe is guaranteed to have been allocated by this stage. */
97318  assert( v );
97319  VdbeModuleComment((v, "BEGIN: GenRowDel(%d,%d,%d,%d)",
97320                         iDataCur, iIdxCur, iPk, (int)nPk));
97321
97322  /* Seek cursor iCur to the row to delete. If this row no longer exists
97323  ** (this can happen if a trigger program has already deleted it), do
97324  ** not attempt to delete it or fire any DELETE triggers.  */
97325  iLabel = sqlite3VdbeMakeLabel(v);
97326  opSeek = HasRowid(pTab) ? OP_NotExists : OP_NotFound;
97327  if( !bNoSeek ){
97328    sqlite3VdbeAddOp4Int(v, opSeek, iDataCur, iLabel, iPk, nPk);
97329    VdbeCoverageIf(v, opSeek==OP_NotExists);
97330    VdbeCoverageIf(v, opSeek==OP_NotFound);
97331  }
97332
97333  /* If there are any triggers to fire, allocate a range of registers to
97334  ** use for the old.* references in the triggers.  */
97335  if( sqlite3FkRequired(pParse, pTab, 0, 0) || pTrigger ){
97336    u32 mask;                     /* Mask of OLD.* columns in use */
97337    int iCol;                     /* Iterator used while populating OLD.* */
97338    int addrStart;                /* Start of BEFORE trigger programs */
97339
97340    /* TODO: Could use temporary registers here. Also could attempt to
97341    ** avoid copying the contents of the rowid register.  */
97342    mask = sqlite3TriggerColmask(
97343        pParse, pTrigger, 0, 0, TRIGGER_BEFORE|TRIGGER_AFTER, pTab, onconf
97344    );
97345    mask |= sqlite3FkOldmask(pParse, pTab);
97346    iOld = pParse->nMem+1;
97347    pParse->nMem += (1 + pTab->nCol);
97348
97349    /* Populate the OLD.* pseudo-table register array. These values will be
97350    ** used by any BEFORE and AFTER triggers that exist.  */
97351    sqlite3VdbeAddOp2(v, OP_Copy, iPk, iOld);
97352    for(iCol=0; iCol<pTab->nCol; iCol++){
97353      testcase( mask!=0xffffffff && iCol==31 );
97354      testcase( mask!=0xffffffff && iCol==32 );
97355      if( mask==0xffffffff || (iCol<=31 && (mask & MASKBIT32(iCol))!=0) ){
97356        sqlite3ExprCodeGetColumnOfTable(v, pTab, iDataCur, iCol, iOld+iCol+1);
97357      }
97358    }
97359
97360    /* Invoke BEFORE DELETE trigger programs. */
97361    addrStart = sqlite3VdbeCurrentAddr(v);
97362    sqlite3CodeRowTrigger(pParse, pTrigger,
97363        TK_DELETE, 0, TRIGGER_BEFORE, pTab, iOld, onconf, iLabel
97364    );
97365
97366    /* If any BEFORE triggers were coded, then seek the cursor to the
97367    ** row to be deleted again. It may be that the BEFORE triggers moved
97368    ** the cursor or of already deleted the row that the cursor was
97369    ** pointing to.
97370    */
97371    if( addrStart<sqlite3VdbeCurrentAddr(v) ){
97372      sqlite3VdbeAddOp4Int(v, opSeek, iDataCur, iLabel, iPk, nPk);
97373      VdbeCoverageIf(v, opSeek==OP_NotExists);
97374      VdbeCoverageIf(v, opSeek==OP_NotFound);
97375    }
97376
97377    /* Do FK processing. This call checks that any FK constraints that
97378    ** refer to this table (i.e. constraints attached to other tables)
97379    ** are not violated by deleting this row.  */
97380    sqlite3FkCheck(pParse, pTab, iOld, 0, 0, 0);
97381  }
97382
97383  /* Delete the index and table entries. Skip this step if pTab is really
97384  ** a view (in which case the only effect of the DELETE statement is to
97385  ** fire the INSTEAD OF triggers).  */
97386  if( pTab->pSelect==0 ){
97387    sqlite3GenerateRowIndexDelete(pParse, pTab, iDataCur, iIdxCur, 0);
97388    sqlite3VdbeAddOp2(v, OP_Delete, iDataCur, (count?OPFLAG_NCHANGE:0));
97389    if( count ){
97390      sqlite3VdbeChangeP4(v, -1, pTab->zName, P4_TRANSIENT);
97391    }
97392  }
97393
97394  /* Do any ON CASCADE, SET NULL or SET DEFAULT operations required to
97395  ** handle rows (possibly in other tables) that refer via a foreign key
97396  ** to the row just deleted. */
97397  sqlite3FkActions(pParse, pTab, 0, iOld, 0, 0);
97398
97399  /* Invoke AFTER DELETE trigger programs. */
97400  sqlite3CodeRowTrigger(pParse, pTrigger,
97401      TK_DELETE, 0, TRIGGER_AFTER, pTab, iOld, onconf, iLabel
97402  );
97403
97404  /* Jump here if the row had already been deleted before any BEFORE
97405  ** trigger programs were invoked. Or if a trigger program throws a
97406  ** RAISE(IGNORE) exception.  */
97407  sqlite3VdbeResolveLabel(v, iLabel);
97408  VdbeModuleComment((v, "END: GenRowDel()"));
97409}
97410
97411/*
97412** This routine generates VDBE code that causes the deletion of all
97413** index entries associated with a single row of a single table, pTab
97414**
97415** Preconditions:
97416**
97417**   1.  A read/write cursor "iDataCur" must be open on the canonical storage
97418**       btree for the table pTab.  (This will be either the table itself
97419**       for rowid tables or to the primary key index for WITHOUT ROWID
97420**       tables.)
97421**
97422**   2.  Read/write cursors for all indices of pTab must be open as
97423**       cursor number iIdxCur+i for the i-th index.  (The pTab->pIndex
97424**       index is the 0-th index.)
97425**
97426**   3.  The "iDataCur" cursor must be already be positioned on the row
97427**       that is to be deleted.
97428*/
97429SQLITE_PRIVATE void sqlite3GenerateRowIndexDelete(
97430  Parse *pParse,     /* Parsing and code generating context */
97431  Table *pTab,       /* Table containing the row to be deleted */
97432  int iDataCur,      /* Cursor of table holding data. */
97433  int iIdxCur,       /* First index cursor */
97434  int *aRegIdx       /* Only delete if aRegIdx!=0 && aRegIdx[i]>0 */
97435){
97436  int i;             /* Index loop counter */
97437  int r1 = -1;       /* Register holding an index key */
97438  int iPartIdxLabel; /* Jump destination for skipping partial index entries */
97439  Index *pIdx;       /* Current index */
97440  Index *pPrior = 0; /* Prior index */
97441  Vdbe *v;           /* The prepared statement under construction */
97442  Index *pPk;        /* PRIMARY KEY index, or NULL for rowid tables */
97443
97444  v = pParse->pVdbe;
97445  pPk = HasRowid(pTab) ? 0 : sqlite3PrimaryKeyIndex(pTab);
97446  for(i=0, pIdx=pTab->pIndex; pIdx; i++, pIdx=pIdx->pNext){
97447    assert( iIdxCur+i!=iDataCur || pPk==pIdx );
97448    if( aRegIdx!=0 && aRegIdx[i]==0 ) continue;
97449    if( pIdx==pPk ) continue;
97450    VdbeModuleComment((v, "GenRowIdxDel for %s", pIdx->zName));
97451    r1 = sqlite3GenerateIndexKey(pParse, pIdx, iDataCur, 0, 1,
97452                                 &iPartIdxLabel, pPrior, r1);
97453    sqlite3VdbeAddOp3(v, OP_IdxDelete, iIdxCur+i, r1,
97454                      pIdx->uniqNotNull ? pIdx->nKeyCol : pIdx->nColumn);
97455    sqlite3ResolvePartIdxLabel(pParse, iPartIdxLabel);
97456    pPrior = pIdx;
97457  }
97458}
97459
97460/*
97461** Generate code that will assemble an index key and stores it in register
97462** regOut.  The key with be for index pIdx which is an index on pTab.
97463** iCur is the index of a cursor open on the pTab table and pointing to
97464** the entry that needs indexing.  If pTab is a WITHOUT ROWID table, then
97465** iCur must be the cursor of the PRIMARY KEY index.
97466**
97467** Return a register number which is the first in a block of
97468** registers that holds the elements of the index key.  The
97469** block of registers has already been deallocated by the time
97470** this routine returns.
97471**
97472** If *piPartIdxLabel is not NULL, fill it in with a label and jump
97473** to that label if pIdx is a partial index that should be skipped.
97474** The label should be resolved using sqlite3ResolvePartIdxLabel().
97475** A partial index should be skipped if its WHERE clause evaluates
97476** to false or null.  If pIdx is not a partial index, *piPartIdxLabel
97477** will be set to zero which is an empty label that is ignored by
97478** sqlite3ResolvePartIdxLabel().
97479**
97480** The pPrior and regPrior parameters are used to implement a cache to
97481** avoid unnecessary register loads.  If pPrior is not NULL, then it is
97482** a pointer to a different index for which an index key has just been
97483** computed into register regPrior.  If the current pIdx index is generating
97484** its key into the same sequence of registers and if pPrior and pIdx share
97485** a column in common, then the register corresponding to that column already
97486** holds the correct value and the loading of that register is skipped.
97487** This optimization is helpful when doing a DELETE or an INTEGRITY_CHECK
97488** on a table with multiple indices, and especially with the ROWID or
97489** PRIMARY KEY columns of the index.
97490*/
97491SQLITE_PRIVATE int sqlite3GenerateIndexKey(
97492  Parse *pParse,       /* Parsing context */
97493  Index *pIdx,         /* The index for which to generate a key */
97494  int iDataCur,        /* Cursor number from which to take column data */
97495  int regOut,          /* Put the new key into this register if not 0 */
97496  int prefixOnly,      /* Compute only a unique prefix of the key */
97497  int *piPartIdxLabel, /* OUT: Jump to this label to skip partial index */
97498  Index *pPrior,       /* Previously generated index key */
97499  int regPrior         /* Register holding previous generated key */
97500){
97501  Vdbe *v = pParse->pVdbe;
97502  int j;
97503  Table *pTab = pIdx->pTable;
97504  int regBase;
97505  int nCol;
97506
97507  if( piPartIdxLabel ){
97508    if( pIdx->pPartIdxWhere ){
97509      *piPartIdxLabel = sqlite3VdbeMakeLabel(v);
97510      pParse->iPartIdxTab = iDataCur;
97511      sqlite3ExprCachePush(pParse);
97512      sqlite3ExprIfFalseDup(pParse, pIdx->pPartIdxWhere, *piPartIdxLabel,
97513                            SQLITE_JUMPIFNULL);
97514    }else{
97515      *piPartIdxLabel = 0;
97516    }
97517  }
97518  nCol = (prefixOnly && pIdx->uniqNotNull) ? pIdx->nKeyCol : pIdx->nColumn;
97519  regBase = sqlite3GetTempRange(pParse, nCol);
97520  if( pPrior && (regBase!=regPrior || pPrior->pPartIdxWhere) ) pPrior = 0;
97521  for(j=0; j<nCol; j++){
97522    if( pPrior && pPrior->aiColumn[j]==pIdx->aiColumn[j] ) continue;
97523    sqlite3ExprCodeGetColumnOfTable(v, pTab, iDataCur, pIdx->aiColumn[j],
97524                                    regBase+j);
97525    /* If the column affinity is REAL but the number is an integer, then it
97526    ** might be stored in the table as an integer (using a compact
97527    ** representation) then converted to REAL by an OP_RealAffinity opcode.
97528    ** But we are getting ready to store this value back into an index, where
97529    ** it should be converted by to INTEGER again.  So omit the OP_RealAffinity
97530    ** opcode if it is present */
97531    sqlite3VdbeDeletePriorOpcode(v, OP_RealAffinity);
97532  }
97533  if( regOut ){
97534    sqlite3VdbeAddOp3(v, OP_MakeRecord, regBase, nCol, regOut);
97535  }
97536  sqlite3ReleaseTempRange(pParse, regBase, nCol);
97537  return regBase;
97538}
97539
97540/*
97541** If a prior call to sqlite3GenerateIndexKey() generated a jump-over label
97542** because it was a partial index, then this routine should be called to
97543** resolve that label.
97544*/
97545SQLITE_PRIVATE void sqlite3ResolvePartIdxLabel(Parse *pParse, int iLabel){
97546  if( iLabel ){
97547    sqlite3VdbeResolveLabel(pParse->pVdbe, iLabel);
97548    sqlite3ExprCachePop(pParse);
97549  }
97550}
97551
97552/************** End of delete.c **********************************************/
97553/************** Begin file func.c ********************************************/
97554/*
97555** 2002 February 23
97556**
97557** The author disclaims copyright to this source code.  In place of
97558** a legal notice, here is a blessing:
97559**
97560**    May you do good and not evil.
97561**    May you find forgiveness for yourself and forgive others.
97562**    May you share freely, never taking more than you give.
97563**
97564*************************************************************************
97565** This file contains the C-language implementations for many of the SQL
97566** functions of SQLite.  (Some function, and in particular the date and
97567** time functions, are implemented separately.)
97568*/
97569/* #include "sqliteInt.h" */
97570/* #include <stdlib.h> */
97571/* #include <assert.h> */
97572/* #include "vdbeInt.h" */
97573
97574/*
97575** Return the collating function associated with a function.
97576*/
97577static CollSeq *sqlite3GetFuncCollSeq(sqlite3_context *context){
97578  VdbeOp *pOp;
97579  assert( context->pVdbe!=0 );
97580  pOp = &context->pVdbe->aOp[context->iOp-1];
97581  assert( pOp->opcode==OP_CollSeq );
97582  assert( pOp->p4type==P4_COLLSEQ );
97583  return pOp->p4.pColl;
97584}
97585
97586/*
97587** Indicate that the accumulator load should be skipped on this
97588** iteration of the aggregate loop.
97589*/
97590static void sqlite3SkipAccumulatorLoad(sqlite3_context *context){
97591  context->skipFlag = 1;
97592}
97593
97594/*
97595** Implementation of the non-aggregate min() and max() functions
97596*/
97597static void minmaxFunc(
97598  sqlite3_context *context,
97599  int argc,
97600  sqlite3_value **argv
97601){
97602  int i;
97603  int mask;    /* 0 for min() or 0xffffffff for max() */
97604  int iBest;
97605  CollSeq *pColl;
97606
97607  assert( argc>1 );
97608  mask = sqlite3_user_data(context)==0 ? 0 : -1;
97609  pColl = sqlite3GetFuncCollSeq(context);
97610  assert( pColl );
97611  assert( mask==-1 || mask==0 );
97612  iBest = 0;
97613  if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return;
97614  for(i=1; i<argc; i++){
97615    if( sqlite3_value_type(argv[i])==SQLITE_NULL ) return;
97616    if( (sqlite3MemCompare(argv[iBest], argv[i], pColl)^mask)>=0 ){
97617      testcase( mask==0 );
97618      iBest = i;
97619    }
97620  }
97621  sqlite3_result_value(context, argv[iBest]);
97622}
97623
97624/*
97625** Return the type of the argument.
97626*/
97627static void typeofFunc(
97628  sqlite3_context *context,
97629  int NotUsed,
97630  sqlite3_value **argv
97631){
97632  const char *z = 0;
97633  UNUSED_PARAMETER(NotUsed);
97634  switch( sqlite3_value_type(argv[0]) ){
97635    case SQLITE_INTEGER: z = "integer"; break;
97636    case SQLITE_TEXT:    z = "text";    break;
97637    case SQLITE_FLOAT:   z = "real";    break;
97638    case SQLITE_BLOB:    z = "blob";    break;
97639    default:             z = "null";    break;
97640  }
97641  sqlite3_result_text(context, z, -1, SQLITE_STATIC);
97642}
97643
97644
97645/*
97646** Implementation of the length() function
97647*/
97648static void lengthFunc(
97649  sqlite3_context *context,
97650  int argc,
97651  sqlite3_value **argv
97652){
97653  int len;
97654
97655  assert( argc==1 );
97656  UNUSED_PARAMETER(argc);
97657  switch( sqlite3_value_type(argv[0]) ){
97658    case SQLITE_BLOB:
97659    case SQLITE_INTEGER:
97660    case SQLITE_FLOAT: {
97661      sqlite3_result_int(context, sqlite3_value_bytes(argv[0]));
97662      break;
97663    }
97664    case SQLITE_TEXT: {
97665      const unsigned char *z = sqlite3_value_text(argv[0]);
97666      if( z==0 ) return;
97667      len = 0;
97668      while( *z ){
97669        len++;
97670        SQLITE_SKIP_UTF8(z);
97671      }
97672      sqlite3_result_int(context, len);
97673      break;
97674    }
97675    default: {
97676      sqlite3_result_null(context);
97677      break;
97678    }
97679  }
97680}
97681
97682/*
97683** Implementation of the abs() function.
97684**
97685** IMP: R-23979-26855 The abs(X) function returns the absolute value of
97686** the numeric argument X.
97687*/
97688static void absFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
97689  assert( argc==1 );
97690  UNUSED_PARAMETER(argc);
97691  switch( sqlite3_value_type(argv[0]) ){
97692    case SQLITE_INTEGER: {
97693      i64 iVal = sqlite3_value_int64(argv[0]);
97694      if( iVal<0 ){
97695        if( iVal==SMALLEST_INT64 ){
97696          /* IMP: R-31676-45509 If X is the integer -9223372036854775808
97697          ** then abs(X) throws an integer overflow error since there is no
97698          ** equivalent positive 64-bit two complement value. */
97699          sqlite3_result_error(context, "integer overflow", -1);
97700          return;
97701        }
97702        iVal = -iVal;
97703      }
97704      sqlite3_result_int64(context, iVal);
97705      break;
97706    }
97707    case SQLITE_NULL: {
97708      /* IMP: R-37434-19929 Abs(X) returns NULL if X is NULL. */
97709      sqlite3_result_null(context);
97710      break;
97711    }
97712    default: {
97713      /* Because sqlite3_value_double() returns 0.0 if the argument is not
97714      ** something that can be converted into a number, we have:
97715      ** IMP: R-01992-00519 Abs(X) returns 0.0 if X is a string or blob
97716      ** that cannot be converted to a numeric value.
97717      */
97718      double rVal = sqlite3_value_double(argv[0]);
97719      if( rVal<0 ) rVal = -rVal;
97720      sqlite3_result_double(context, rVal);
97721      break;
97722    }
97723  }
97724}
97725
97726/*
97727** Implementation of the instr() function.
97728**
97729** instr(haystack,needle) finds the first occurrence of needle
97730** in haystack and returns the number of previous characters plus 1,
97731** or 0 if needle does not occur within haystack.
97732**
97733** If both haystack and needle are BLOBs, then the result is one more than
97734** the number of bytes in haystack prior to the first occurrence of needle,
97735** or 0 if needle never occurs in haystack.
97736*/
97737static void instrFunc(
97738  sqlite3_context *context,
97739  int argc,
97740  sqlite3_value **argv
97741){
97742  const unsigned char *zHaystack;
97743  const unsigned char *zNeedle;
97744  int nHaystack;
97745  int nNeedle;
97746  int typeHaystack, typeNeedle;
97747  int N = 1;
97748  int isText;
97749
97750  UNUSED_PARAMETER(argc);
97751  typeHaystack = sqlite3_value_type(argv[0]);
97752  typeNeedle = sqlite3_value_type(argv[1]);
97753  if( typeHaystack==SQLITE_NULL || typeNeedle==SQLITE_NULL ) return;
97754  nHaystack = sqlite3_value_bytes(argv[0]);
97755  nNeedle = sqlite3_value_bytes(argv[1]);
97756  if( typeHaystack==SQLITE_BLOB && typeNeedle==SQLITE_BLOB ){
97757    zHaystack = sqlite3_value_blob(argv[0]);
97758    zNeedle = sqlite3_value_blob(argv[1]);
97759    isText = 0;
97760  }else{
97761    zHaystack = sqlite3_value_text(argv[0]);
97762    zNeedle = sqlite3_value_text(argv[1]);
97763    isText = 1;
97764  }
97765  while( nNeedle<=nHaystack && memcmp(zHaystack, zNeedle, nNeedle)!=0 ){
97766    N++;
97767    do{
97768      nHaystack--;
97769      zHaystack++;
97770    }while( isText && (zHaystack[0]&0xc0)==0x80 );
97771  }
97772  if( nNeedle>nHaystack ) N = 0;
97773  sqlite3_result_int(context, N);
97774}
97775
97776/*
97777** Implementation of the printf() function.
97778*/
97779static void printfFunc(
97780  sqlite3_context *context,
97781  int argc,
97782  sqlite3_value **argv
97783){
97784  PrintfArguments x;
97785  StrAccum str;
97786  const char *zFormat;
97787  int n;
97788  sqlite3 *db = sqlite3_context_db_handle(context);
97789
97790  if( argc>=1 && (zFormat = (const char*)sqlite3_value_text(argv[0]))!=0 ){
97791    x.nArg = argc-1;
97792    x.nUsed = 0;
97793    x.apArg = argv+1;
97794    sqlite3StrAccumInit(&str, db, 0, 0, db->aLimit[SQLITE_LIMIT_LENGTH]);
97795    sqlite3XPrintf(&str, SQLITE_PRINTF_SQLFUNC, zFormat, &x);
97796    n = str.nChar;
97797    sqlite3_result_text(context, sqlite3StrAccumFinish(&str), n,
97798                        SQLITE_DYNAMIC);
97799  }
97800}
97801
97802/*
97803** Implementation of the substr() function.
97804**
97805** substr(x,p1,p2)  returns p2 characters of x[] beginning with p1.
97806** p1 is 1-indexed.  So substr(x,1,1) returns the first character
97807** of x.  If x is text, then we actually count UTF-8 characters.
97808** If x is a blob, then we count bytes.
97809**
97810** If p1 is negative, then we begin abs(p1) from the end of x[].
97811**
97812** If p2 is negative, return the p2 characters preceding p1.
97813*/
97814static void substrFunc(
97815  sqlite3_context *context,
97816  int argc,
97817  sqlite3_value **argv
97818){
97819  const unsigned char *z;
97820  const unsigned char *z2;
97821  int len;
97822  int p0type;
97823  i64 p1, p2;
97824  int negP2 = 0;
97825
97826  assert( argc==3 || argc==2 );
97827  if( sqlite3_value_type(argv[1])==SQLITE_NULL
97828   || (argc==3 && sqlite3_value_type(argv[2])==SQLITE_NULL)
97829  ){
97830    return;
97831  }
97832  p0type = sqlite3_value_type(argv[0]);
97833  p1 = sqlite3_value_int(argv[1]);
97834  if( p0type==SQLITE_BLOB ){
97835    len = sqlite3_value_bytes(argv[0]);
97836    z = sqlite3_value_blob(argv[0]);
97837    if( z==0 ) return;
97838    assert( len==sqlite3_value_bytes(argv[0]) );
97839  }else{
97840    z = sqlite3_value_text(argv[0]);
97841    if( z==0 ) return;
97842    len = 0;
97843    if( p1<0 ){
97844      for(z2=z; *z2; len++){
97845        SQLITE_SKIP_UTF8(z2);
97846      }
97847    }
97848  }
97849#ifdef SQLITE_SUBSTR_COMPATIBILITY
97850  /* If SUBSTR_COMPATIBILITY is defined then substr(X,0,N) work the same as
97851  ** as substr(X,1,N) - it returns the first N characters of X.  This
97852  ** is essentially a back-out of the bug-fix in check-in [5fc125d362df4b8]
97853  ** from 2009-02-02 for compatibility of applications that exploited the
97854  ** old buggy behavior. */
97855  if( p1==0 ) p1 = 1; /* <rdar://problem/6778339> */
97856#endif
97857  if( argc==3 ){
97858    p2 = sqlite3_value_int(argv[2]);
97859    if( p2<0 ){
97860      p2 = -p2;
97861      negP2 = 1;
97862    }
97863  }else{
97864    p2 = sqlite3_context_db_handle(context)->aLimit[SQLITE_LIMIT_LENGTH];
97865  }
97866  if( p1<0 ){
97867    p1 += len;
97868    if( p1<0 ){
97869      p2 += p1;
97870      if( p2<0 ) p2 = 0;
97871      p1 = 0;
97872    }
97873  }else if( p1>0 ){
97874    p1--;
97875  }else if( p2>0 ){
97876    p2--;
97877  }
97878  if( negP2 ){
97879    p1 -= p2;
97880    if( p1<0 ){
97881      p2 += p1;
97882      p1 = 0;
97883    }
97884  }
97885  assert( p1>=0 && p2>=0 );
97886  if( p0type!=SQLITE_BLOB ){
97887    while( *z && p1 ){
97888      SQLITE_SKIP_UTF8(z);
97889      p1--;
97890    }
97891    for(z2=z; *z2 && p2; p2--){
97892      SQLITE_SKIP_UTF8(z2);
97893    }
97894    sqlite3_result_text64(context, (char*)z, z2-z, SQLITE_TRANSIENT,
97895                          SQLITE_UTF8);
97896  }else{
97897    if( p1+p2>len ){
97898      p2 = len-p1;
97899      if( p2<0 ) p2 = 0;
97900    }
97901    sqlite3_result_blob64(context, (char*)&z[p1], (u64)p2, SQLITE_TRANSIENT);
97902  }
97903}
97904
97905/*
97906** Implementation of the round() function
97907*/
97908#ifndef SQLITE_OMIT_FLOATING_POINT
97909static void roundFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
97910  int n = 0;
97911  double r;
97912  char *zBuf;
97913  assert( argc==1 || argc==2 );
97914  if( argc==2 ){
97915    if( SQLITE_NULL==sqlite3_value_type(argv[1]) ) return;
97916    n = sqlite3_value_int(argv[1]);
97917    if( n>30 ) n = 30;
97918    if( n<0 ) n = 0;
97919  }
97920  if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return;
97921  r = sqlite3_value_double(argv[0]);
97922  /* If Y==0 and X will fit in a 64-bit int,
97923  ** handle the rounding directly,
97924  ** otherwise use printf.
97925  */
97926  if( n==0 && r>=0 && r<LARGEST_INT64-1 ){
97927    r = (double)((sqlite_int64)(r+0.5));
97928  }else if( n==0 && r<0 && (-r)<LARGEST_INT64-1 ){
97929    r = -(double)((sqlite_int64)((-r)+0.5));
97930  }else{
97931    zBuf = sqlite3_mprintf("%.*f",n,r);
97932    if( zBuf==0 ){
97933      sqlite3_result_error_nomem(context);
97934      return;
97935    }
97936    sqlite3AtoF(zBuf, &r, sqlite3Strlen30(zBuf), SQLITE_UTF8);
97937    sqlite3_free(zBuf);
97938  }
97939  sqlite3_result_double(context, r);
97940}
97941#endif
97942
97943/*
97944** Allocate nByte bytes of space using sqlite3Malloc(). If the
97945** allocation fails, call sqlite3_result_error_nomem() to notify
97946** the database handle that malloc() has failed and return NULL.
97947** If nByte is larger than the maximum string or blob length, then
97948** raise an SQLITE_TOOBIG exception and return NULL.
97949*/
97950static void *contextMalloc(sqlite3_context *context, i64 nByte){
97951  char *z;
97952  sqlite3 *db = sqlite3_context_db_handle(context);
97953  assert( nByte>0 );
97954  testcase( nByte==db->aLimit[SQLITE_LIMIT_LENGTH] );
97955  testcase( nByte==db->aLimit[SQLITE_LIMIT_LENGTH]+1 );
97956  if( nByte>db->aLimit[SQLITE_LIMIT_LENGTH] ){
97957    sqlite3_result_error_toobig(context);
97958    z = 0;
97959  }else{
97960    z = sqlite3Malloc(nByte);
97961    if( !z ){
97962      sqlite3_result_error_nomem(context);
97963    }
97964  }
97965  return z;
97966}
97967
97968/*
97969** Implementation of the upper() and lower() SQL functions.
97970*/
97971static void upperFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
97972  char *z1;
97973  const char *z2;
97974  int i, n;
97975  UNUSED_PARAMETER(argc);
97976  z2 = (char*)sqlite3_value_text(argv[0]);
97977  n = sqlite3_value_bytes(argv[0]);
97978  /* Verify that the call to _bytes() does not invalidate the _text() pointer */
97979  assert( z2==(char*)sqlite3_value_text(argv[0]) );
97980  if( z2 ){
97981    z1 = contextMalloc(context, ((i64)n)+1);
97982    if( z1 ){
97983      for(i=0; i<n; i++){
97984        z1[i] = (char)sqlite3Toupper(z2[i]);
97985      }
97986      sqlite3_result_text(context, z1, n, sqlite3_free);
97987    }
97988  }
97989}
97990static void lowerFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
97991  char *z1;
97992  const char *z2;
97993  int i, n;
97994  UNUSED_PARAMETER(argc);
97995  z2 = (char*)sqlite3_value_text(argv[0]);
97996  n = sqlite3_value_bytes(argv[0]);
97997  /* Verify that the call to _bytes() does not invalidate the _text() pointer */
97998  assert( z2==(char*)sqlite3_value_text(argv[0]) );
97999  if( z2 ){
98000    z1 = contextMalloc(context, ((i64)n)+1);
98001    if( z1 ){
98002      for(i=0; i<n; i++){
98003        z1[i] = sqlite3Tolower(z2[i]);
98004      }
98005      sqlite3_result_text(context, z1, n, sqlite3_free);
98006    }
98007  }
98008}
98009
98010/*
98011** Some functions like COALESCE() and IFNULL() and UNLIKELY() are implemented
98012** as VDBE code so that unused argument values do not have to be computed.
98013** However, we still need some kind of function implementation for this
98014** routines in the function table.  The noopFunc macro provides this.
98015** noopFunc will never be called so it doesn't matter what the implementation
98016** is.  We might as well use the "version()" function as a substitute.
98017*/
98018#define noopFunc versionFunc   /* Substitute function - never called */
98019
98020/*
98021** Implementation of random().  Return a random integer.
98022*/
98023static void randomFunc(
98024  sqlite3_context *context,
98025  int NotUsed,
98026  sqlite3_value **NotUsed2
98027){
98028  sqlite_int64 r;
98029  UNUSED_PARAMETER2(NotUsed, NotUsed2);
98030  sqlite3_randomness(sizeof(r), &r);
98031  if( r<0 ){
98032    /* We need to prevent a random number of 0x8000000000000000
98033    ** (or -9223372036854775808) since when you do abs() of that
98034    ** number of you get the same value back again.  To do this
98035    ** in a way that is testable, mask the sign bit off of negative
98036    ** values, resulting in a positive value.  Then take the
98037    ** 2s complement of that positive value.  The end result can
98038    ** therefore be no less than -9223372036854775807.
98039    */
98040    r = -(r & LARGEST_INT64);
98041  }
98042  sqlite3_result_int64(context, r);
98043}
98044
98045/*
98046** Implementation of randomblob(N).  Return a random blob
98047** that is N bytes long.
98048*/
98049static void randomBlob(
98050  sqlite3_context *context,
98051  int argc,
98052  sqlite3_value **argv
98053){
98054  int n;
98055  unsigned char *p;
98056  assert( argc==1 );
98057  UNUSED_PARAMETER(argc);
98058  n = sqlite3_value_int(argv[0]);
98059  if( n<1 ){
98060    n = 1;
98061  }
98062  p = contextMalloc(context, n);
98063  if( p ){
98064    sqlite3_randomness(n, p);
98065    sqlite3_result_blob(context, (char*)p, n, sqlite3_free);
98066  }
98067}
98068
98069/*
98070** Implementation of the last_insert_rowid() SQL function.  The return
98071** value is the same as the sqlite3_last_insert_rowid() API function.
98072*/
98073static void last_insert_rowid(
98074  sqlite3_context *context,
98075  int NotUsed,
98076  sqlite3_value **NotUsed2
98077){
98078  sqlite3 *db = sqlite3_context_db_handle(context);
98079  UNUSED_PARAMETER2(NotUsed, NotUsed2);
98080  /* IMP: R-51513-12026 The last_insert_rowid() SQL function is a
98081  ** wrapper around the sqlite3_last_insert_rowid() C/C++ interface
98082  ** function. */
98083  sqlite3_result_int64(context, sqlite3_last_insert_rowid(db));
98084}
98085
98086/*
98087** Implementation of the changes() SQL function.
98088**
98089** IMP: R-62073-11209 The changes() SQL function is a wrapper
98090** around the sqlite3_changes() C/C++ function and hence follows the same
98091** rules for counting changes.
98092*/
98093static void changes(
98094  sqlite3_context *context,
98095  int NotUsed,
98096  sqlite3_value **NotUsed2
98097){
98098  sqlite3 *db = sqlite3_context_db_handle(context);
98099  UNUSED_PARAMETER2(NotUsed, NotUsed2);
98100  sqlite3_result_int(context, sqlite3_changes(db));
98101}
98102
98103/*
98104** Implementation of the total_changes() SQL function.  The return value is
98105** the same as the sqlite3_total_changes() API function.
98106*/
98107static void total_changes(
98108  sqlite3_context *context,
98109  int NotUsed,
98110  sqlite3_value **NotUsed2
98111){
98112  sqlite3 *db = sqlite3_context_db_handle(context);
98113  UNUSED_PARAMETER2(NotUsed, NotUsed2);
98114  /* IMP: R-52756-41993 This function is a wrapper around the
98115  ** sqlite3_total_changes() C/C++ interface. */
98116  sqlite3_result_int(context, sqlite3_total_changes(db));
98117}
98118
98119/*
98120** A structure defining how to do GLOB-style comparisons.
98121*/
98122struct compareInfo {
98123  u8 matchAll;
98124  u8 matchOne;
98125  u8 matchSet;
98126  u8 noCase;
98127};
98128
98129/*
98130** For LIKE and GLOB matching on EBCDIC machines, assume that every
98131** character is exactly one byte in size.  Also, provde the Utf8Read()
98132** macro for fast reading of the next character in the common case where
98133** the next character is ASCII.
98134*/
98135#if defined(SQLITE_EBCDIC)
98136# define sqlite3Utf8Read(A)        (*((*A)++))
98137# define Utf8Read(A)               (*(A++))
98138#else
98139# define Utf8Read(A)               (A[0]<0x80?*(A++):sqlite3Utf8Read(&A))
98140#endif
98141
98142static const struct compareInfo globInfo = { '*', '?', '[', 0 };
98143/* The correct SQL-92 behavior is for the LIKE operator to ignore
98144** case.  Thus  'a' LIKE 'A' would be true. */
98145static const struct compareInfo likeInfoNorm = { '%', '_',   0, 1 };
98146/* If SQLITE_CASE_SENSITIVE_LIKE is defined, then the LIKE operator
98147** is case sensitive causing 'a' LIKE 'A' to be false */
98148static const struct compareInfo likeInfoAlt = { '%', '_',   0, 0 };
98149
98150/*
98151** Compare two UTF-8 strings for equality where the first string can
98152** potentially be a "glob" or "like" expression.  Return true (1) if they
98153** are the same and false (0) if they are different.
98154**
98155** Globbing rules:
98156**
98157**      '*'       Matches any sequence of zero or more characters.
98158**
98159**      '?'       Matches exactly one character.
98160**
98161**     [...]      Matches one character from the enclosed list of
98162**                characters.
98163**
98164**     [^...]     Matches one character not in the enclosed list.
98165**
98166** With the [...] and [^...] matching, a ']' character can be included
98167** in the list by making it the first character after '[' or '^'.  A
98168** range of characters can be specified using '-'.  Example:
98169** "[a-z]" matches any single lower-case letter.  To match a '-', make
98170** it the last character in the list.
98171**
98172** Like matching rules:
98173**
98174**      '%'       Matches any sequence of zero or more characters
98175**
98176***     '_'       Matches any one character
98177**
98178**      Ec        Where E is the "esc" character and c is any other
98179**                character, including '%', '_', and esc, match exactly c.
98180**
98181** The comments within this routine usually assume glob matching.
98182**
98183** This routine is usually quick, but can be N**2 in the worst case.
98184*/
98185static int patternCompare(
98186  const u8 *zPattern,              /* The glob pattern */
98187  const u8 *zString,               /* The string to compare against the glob */
98188  const struct compareInfo *pInfo, /* Information about how to do the compare */
98189  u32 esc                          /* The escape character */
98190){
98191  u32 c, c2;                       /* Next pattern and input string chars */
98192  u32 matchOne = pInfo->matchOne;  /* "?" or "_" */
98193  u32 matchAll = pInfo->matchAll;  /* "*" or "%" */
98194  u32 matchOther;                  /* "[" or the escape character */
98195  u8 noCase = pInfo->noCase;       /* True if uppercase==lowercase */
98196  const u8 *zEscaped = 0;          /* One past the last escaped input char */
98197
98198  /* The GLOB operator does not have an ESCAPE clause.  And LIKE does not
98199  ** have the matchSet operator.  So we either have to look for one or
98200  ** the other, never both.  Hence the single variable matchOther is used
98201  ** to store the one we have to look for.
98202  */
98203  matchOther = esc ? esc : pInfo->matchSet;
98204
98205  while( (c = Utf8Read(zPattern))!=0 ){
98206    if( c==matchAll ){  /* Match "*" */
98207      /* Skip over multiple "*" characters in the pattern.  If there
98208      ** are also "?" characters, skip those as well, but consume a
98209      ** single character of the input string for each "?" skipped */
98210      while( (c=Utf8Read(zPattern)) == matchAll || c == matchOne ){
98211        if( c==matchOne && sqlite3Utf8Read(&zString)==0 ){
98212          return 0;
98213        }
98214      }
98215      if( c==0 ){
98216        return 1;   /* "*" at the end of the pattern matches */
98217      }else if( c==matchOther ){
98218        if( esc ){
98219          c = sqlite3Utf8Read(&zPattern);
98220          if( c==0 ) return 0;
98221        }else{
98222          /* "[...]" immediately follows the "*".  We have to do a slow
98223          ** recursive search in this case, but it is an unusual case. */
98224          assert( matchOther<0x80 );  /* '[' is a single-byte character */
98225          while( *zString
98226                 && patternCompare(&zPattern[-1],zString,pInfo,esc)==0 ){
98227            SQLITE_SKIP_UTF8(zString);
98228          }
98229          return *zString!=0;
98230        }
98231      }
98232
98233      /* At this point variable c contains the first character of the
98234      ** pattern string past the "*".  Search in the input string for the
98235      ** first matching character and recursively contine the match from
98236      ** that point.
98237      **
98238      ** For a case-insensitive search, set variable cx to be the same as
98239      ** c but in the other case and search the input string for either
98240      ** c or cx.
98241      */
98242      if( c<=0x80 ){
98243        u32 cx;
98244        if( noCase ){
98245          cx = sqlite3Toupper(c);
98246          c = sqlite3Tolower(c);
98247        }else{
98248          cx = c;
98249        }
98250        while( (c2 = *(zString++))!=0 ){
98251          if( c2!=c && c2!=cx ) continue;
98252          if( patternCompare(zPattern,zString,pInfo,esc) ) return 1;
98253        }
98254      }else{
98255        while( (c2 = Utf8Read(zString))!=0 ){
98256          if( c2!=c ) continue;
98257          if( patternCompare(zPattern,zString,pInfo,esc) ) return 1;
98258        }
98259      }
98260      return 0;
98261    }
98262    if( c==matchOther ){
98263      if( esc ){
98264        c = sqlite3Utf8Read(&zPattern);
98265        if( c==0 ) return 0;
98266        zEscaped = zPattern;
98267      }else{
98268        u32 prior_c = 0;
98269        int seen = 0;
98270        int invert = 0;
98271        c = sqlite3Utf8Read(&zString);
98272        if( c==0 ) return 0;
98273        c2 = sqlite3Utf8Read(&zPattern);
98274        if( c2=='^' ){
98275          invert = 1;
98276          c2 = sqlite3Utf8Read(&zPattern);
98277        }
98278        if( c2==']' ){
98279          if( c==']' ) seen = 1;
98280          c2 = sqlite3Utf8Read(&zPattern);
98281        }
98282        while( c2 && c2!=']' ){
98283          if( c2=='-' && zPattern[0]!=']' && zPattern[0]!=0 && prior_c>0 ){
98284            c2 = sqlite3Utf8Read(&zPattern);
98285            if( c>=prior_c && c<=c2 ) seen = 1;
98286            prior_c = 0;
98287          }else{
98288            if( c==c2 ){
98289              seen = 1;
98290            }
98291            prior_c = c2;
98292          }
98293          c2 = sqlite3Utf8Read(&zPattern);
98294        }
98295        if( c2==0 || (seen ^ invert)==0 ){
98296          return 0;
98297        }
98298        continue;
98299      }
98300    }
98301    c2 = Utf8Read(zString);
98302    if( c==c2 ) continue;
98303    if( noCase && c<0x80 && c2<0x80 && sqlite3Tolower(c)==sqlite3Tolower(c2) ){
98304      continue;
98305    }
98306    if( c==matchOne && zPattern!=zEscaped && c2!=0 ) continue;
98307    return 0;
98308  }
98309  return *zString==0;
98310}
98311
98312/*
98313** The sqlite3_strglob() interface.
98314*/
98315SQLITE_API int SQLITE_STDCALL sqlite3_strglob(const char *zGlobPattern, const char *zString){
98316  return patternCompare((u8*)zGlobPattern, (u8*)zString, &globInfo, 0)==0;
98317}
98318
98319/*
98320** Count the number of times that the LIKE operator (or GLOB which is
98321** just a variation of LIKE) gets called.  This is used for testing
98322** only.
98323*/
98324#ifdef SQLITE_TEST
98325SQLITE_API int sqlite3_like_count = 0;
98326#endif
98327
98328
98329/*
98330** Implementation of the like() SQL function.  This function implements
98331** the build-in LIKE operator.  The first argument to the function is the
98332** pattern and the second argument is the string.  So, the SQL statements:
98333**
98334**       A LIKE B
98335**
98336** is implemented as like(B,A).
98337**
98338** This same function (with a different compareInfo structure) computes
98339** the GLOB operator.
98340*/
98341static void likeFunc(
98342  sqlite3_context *context,
98343  int argc,
98344  sqlite3_value **argv
98345){
98346  const unsigned char *zA, *zB;
98347  u32 escape = 0;
98348  int nPat;
98349  sqlite3 *db = sqlite3_context_db_handle(context);
98350
98351  zB = sqlite3_value_text(argv[0]);
98352  zA = sqlite3_value_text(argv[1]);
98353
98354  /* Limit the length of the LIKE or GLOB pattern to avoid problems
98355  ** of deep recursion and N*N behavior in patternCompare().
98356  */
98357  nPat = sqlite3_value_bytes(argv[0]);
98358  testcase( nPat==db->aLimit[SQLITE_LIMIT_LIKE_PATTERN_LENGTH] );
98359  testcase( nPat==db->aLimit[SQLITE_LIMIT_LIKE_PATTERN_LENGTH]+1 );
98360  if( nPat > db->aLimit[SQLITE_LIMIT_LIKE_PATTERN_LENGTH] ){
98361    sqlite3_result_error(context, "LIKE or GLOB pattern too complex", -1);
98362    return;
98363  }
98364  assert( zB==sqlite3_value_text(argv[0]) );  /* Encoding did not change */
98365
98366  if( argc==3 ){
98367    /* The escape character string must consist of a single UTF-8 character.
98368    ** Otherwise, return an error.
98369    */
98370    const unsigned char *zEsc = sqlite3_value_text(argv[2]);
98371    if( zEsc==0 ) return;
98372    if( sqlite3Utf8CharLen((char*)zEsc, -1)!=1 ){
98373      sqlite3_result_error(context,
98374          "ESCAPE expression must be a single character", -1);
98375      return;
98376    }
98377    escape = sqlite3Utf8Read(&zEsc);
98378  }
98379  if( zA && zB ){
98380    struct compareInfo *pInfo = sqlite3_user_data(context);
98381#ifdef SQLITE_TEST
98382    sqlite3_like_count++;
98383#endif
98384
98385    sqlite3_result_int(context, patternCompare(zB, zA, pInfo, escape));
98386  }
98387}
98388
98389/*
98390** Implementation of the NULLIF(x,y) function.  The result is the first
98391** argument if the arguments are different.  The result is NULL if the
98392** arguments are equal to each other.
98393*/
98394static void nullifFunc(
98395  sqlite3_context *context,
98396  int NotUsed,
98397  sqlite3_value **argv
98398){
98399  CollSeq *pColl = sqlite3GetFuncCollSeq(context);
98400  UNUSED_PARAMETER(NotUsed);
98401  if( sqlite3MemCompare(argv[0], argv[1], pColl)!=0 ){
98402    sqlite3_result_value(context, argv[0]);
98403  }
98404}
98405
98406/*
98407** Implementation of the sqlite_version() function.  The result is the version
98408** of the SQLite library that is running.
98409*/
98410static void versionFunc(
98411  sqlite3_context *context,
98412  int NotUsed,
98413  sqlite3_value **NotUsed2
98414){
98415  UNUSED_PARAMETER2(NotUsed, NotUsed2);
98416  /* IMP: R-48699-48617 This function is an SQL wrapper around the
98417  ** sqlite3_libversion() C-interface. */
98418  sqlite3_result_text(context, sqlite3_libversion(), -1, SQLITE_STATIC);
98419}
98420
98421/*
98422** Implementation of the sqlite_source_id() function. The result is a string
98423** that identifies the particular version of the source code used to build
98424** SQLite.
98425*/
98426static void sourceidFunc(
98427  sqlite3_context *context,
98428  int NotUsed,
98429  sqlite3_value **NotUsed2
98430){
98431  UNUSED_PARAMETER2(NotUsed, NotUsed2);
98432  /* IMP: R-24470-31136 This function is an SQL wrapper around the
98433  ** sqlite3_sourceid() C interface. */
98434  sqlite3_result_text(context, sqlite3_sourceid(), -1, SQLITE_STATIC);
98435}
98436
98437/*
98438** Implementation of the sqlite_log() function.  This is a wrapper around
98439** sqlite3_log().  The return value is NULL.  The function exists purely for
98440** its side-effects.
98441*/
98442static void errlogFunc(
98443  sqlite3_context *context,
98444  int argc,
98445  sqlite3_value **argv
98446){
98447  UNUSED_PARAMETER(argc);
98448  UNUSED_PARAMETER(context);
98449  sqlite3_log(sqlite3_value_int(argv[0]), "%s", sqlite3_value_text(argv[1]));
98450}
98451
98452/*
98453** Implementation of the sqlite_compileoption_used() function.
98454** The result is an integer that identifies if the compiler option
98455** was used to build SQLite.
98456*/
98457#ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
98458static void compileoptionusedFunc(
98459  sqlite3_context *context,
98460  int argc,
98461  sqlite3_value **argv
98462){
98463  const char *zOptName;
98464  assert( argc==1 );
98465  UNUSED_PARAMETER(argc);
98466  /* IMP: R-39564-36305 The sqlite_compileoption_used() SQL
98467  ** function is a wrapper around the sqlite3_compileoption_used() C/C++
98468  ** function.
98469  */
98470  if( (zOptName = (const char*)sqlite3_value_text(argv[0]))!=0 ){
98471    sqlite3_result_int(context, sqlite3_compileoption_used(zOptName));
98472  }
98473}
98474#endif /* SQLITE_OMIT_COMPILEOPTION_DIAGS */
98475
98476/*
98477** Implementation of the sqlite_compileoption_get() function.
98478** The result is a string that identifies the compiler options
98479** used to build SQLite.
98480*/
98481#ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
98482static void compileoptiongetFunc(
98483  sqlite3_context *context,
98484  int argc,
98485  sqlite3_value **argv
98486){
98487  int n;
98488  assert( argc==1 );
98489  UNUSED_PARAMETER(argc);
98490  /* IMP: R-04922-24076 The sqlite_compileoption_get() SQL function
98491  ** is a wrapper around the sqlite3_compileoption_get() C/C++ function.
98492  */
98493  n = sqlite3_value_int(argv[0]);
98494  sqlite3_result_text(context, sqlite3_compileoption_get(n), -1, SQLITE_STATIC);
98495}
98496#endif /* SQLITE_OMIT_COMPILEOPTION_DIAGS */
98497
98498/* Array for converting from half-bytes (nybbles) into ASCII hex
98499** digits. */
98500static const char hexdigits[] = {
98501  '0', '1', '2', '3', '4', '5', '6', '7',
98502  '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'
98503};
98504
98505/*
98506** Implementation of the QUOTE() function.  This function takes a single
98507** argument.  If the argument is numeric, the return value is the same as
98508** the argument.  If the argument is NULL, the return value is the string
98509** "NULL".  Otherwise, the argument is enclosed in single quotes with
98510** single-quote escapes.
98511*/
98512static void quoteFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
98513  assert( argc==1 );
98514  UNUSED_PARAMETER(argc);
98515  switch( sqlite3_value_type(argv[0]) ){
98516    case SQLITE_FLOAT: {
98517      double r1, r2;
98518      char zBuf[50];
98519      r1 = sqlite3_value_double(argv[0]);
98520      sqlite3_snprintf(sizeof(zBuf), zBuf, "%!.15g", r1);
98521      sqlite3AtoF(zBuf, &r2, 20, SQLITE_UTF8);
98522      if( r1!=r2 ){
98523        sqlite3_snprintf(sizeof(zBuf), zBuf, "%!.20e", r1);
98524      }
98525      sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT);
98526      break;
98527    }
98528    case SQLITE_INTEGER: {
98529      sqlite3_result_value(context, argv[0]);
98530      break;
98531    }
98532    case SQLITE_BLOB: {
98533      char *zText = 0;
98534      char const *zBlob = sqlite3_value_blob(argv[0]);
98535      int nBlob = sqlite3_value_bytes(argv[0]);
98536      assert( zBlob==sqlite3_value_blob(argv[0]) ); /* No encoding change */
98537      zText = (char *)contextMalloc(context, (2*(i64)nBlob)+4);
98538      if( zText ){
98539        int i;
98540        for(i=0; i<nBlob; i++){
98541          zText[(i*2)+2] = hexdigits[(zBlob[i]>>4)&0x0F];
98542          zText[(i*2)+3] = hexdigits[(zBlob[i])&0x0F];
98543        }
98544        zText[(nBlob*2)+2] = '\'';
98545        zText[(nBlob*2)+3] = '\0';
98546        zText[0] = 'X';
98547        zText[1] = '\'';
98548        sqlite3_result_text(context, zText, -1, SQLITE_TRANSIENT);
98549        sqlite3_free(zText);
98550      }
98551      break;
98552    }
98553    case SQLITE_TEXT: {
98554      int i,j;
98555      u64 n;
98556      const unsigned char *zArg = sqlite3_value_text(argv[0]);
98557      char *z;
98558
98559      if( zArg==0 ) return;
98560      for(i=0, n=0; zArg[i]; i++){ if( zArg[i]=='\'' ) n++; }
98561      z = contextMalloc(context, ((i64)i)+((i64)n)+3);
98562      if( z ){
98563        z[0] = '\'';
98564        for(i=0, j=1; zArg[i]; i++){
98565          z[j++] = zArg[i];
98566          if( zArg[i]=='\'' ){
98567            z[j++] = '\'';
98568          }
98569        }
98570        z[j++] = '\'';
98571        z[j] = 0;
98572        sqlite3_result_text(context, z, j, sqlite3_free);
98573      }
98574      break;
98575    }
98576    default: {
98577      assert( sqlite3_value_type(argv[0])==SQLITE_NULL );
98578      sqlite3_result_text(context, "NULL", 4, SQLITE_STATIC);
98579      break;
98580    }
98581  }
98582}
98583
98584/*
98585** The unicode() function.  Return the integer unicode code-point value
98586** for the first character of the input string.
98587*/
98588static void unicodeFunc(
98589  sqlite3_context *context,
98590  int argc,
98591  sqlite3_value **argv
98592){
98593  const unsigned char *z = sqlite3_value_text(argv[0]);
98594  (void)argc;
98595  if( z && z[0] ) sqlite3_result_int(context, sqlite3Utf8Read(&z));
98596}
98597
98598/*
98599** The char() function takes zero or more arguments, each of which is
98600** an integer.  It constructs a string where each character of the string
98601** is the unicode character for the corresponding integer argument.
98602*/
98603static void charFunc(
98604  sqlite3_context *context,
98605  int argc,
98606  sqlite3_value **argv
98607){
98608  unsigned char *z, *zOut;
98609  int i;
98610  zOut = z = sqlite3_malloc64( argc*4+1 );
98611  if( z==0 ){
98612    sqlite3_result_error_nomem(context);
98613    return;
98614  }
98615  for(i=0; i<argc; i++){
98616    sqlite3_int64 x;
98617    unsigned c;
98618    x = sqlite3_value_int64(argv[i]);
98619    if( x<0 || x>0x10ffff ) x = 0xfffd;
98620    c = (unsigned)(x & 0x1fffff);
98621    if( c<0x00080 ){
98622      *zOut++ = (u8)(c&0xFF);
98623    }else if( c<0x00800 ){
98624      *zOut++ = 0xC0 + (u8)((c>>6)&0x1F);
98625      *zOut++ = 0x80 + (u8)(c & 0x3F);
98626    }else if( c<0x10000 ){
98627      *zOut++ = 0xE0 + (u8)((c>>12)&0x0F);
98628      *zOut++ = 0x80 + (u8)((c>>6) & 0x3F);
98629      *zOut++ = 0x80 + (u8)(c & 0x3F);
98630    }else{
98631      *zOut++ = 0xF0 + (u8)((c>>18) & 0x07);
98632      *zOut++ = 0x80 + (u8)((c>>12) & 0x3F);
98633      *zOut++ = 0x80 + (u8)((c>>6) & 0x3F);
98634      *zOut++ = 0x80 + (u8)(c & 0x3F);
98635    }                                                    \
98636  }
98637  sqlite3_result_text64(context, (char*)z, zOut-z, sqlite3_free, SQLITE_UTF8);
98638}
98639
98640/*
98641** The hex() function.  Interpret the argument as a blob.  Return
98642** a hexadecimal rendering as text.
98643*/
98644static void hexFunc(
98645  sqlite3_context *context,
98646  int argc,
98647  sqlite3_value **argv
98648){
98649  int i, n;
98650  const unsigned char *pBlob;
98651  char *zHex, *z;
98652  assert( argc==1 );
98653  UNUSED_PARAMETER(argc);
98654  pBlob = sqlite3_value_blob(argv[0]);
98655  n = sqlite3_value_bytes(argv[0]);
98656  assert( pBlob==sqlite3_value_blob(argv[0]) );  /* No encoding change */
98657  z = zHex = contextMalloc(context, ((i64)n)*2 + 1);
98658  if( zHex ){
98659    for(i=0; i<n; i++, pBlob++){
98660      unsigned char c = *pBlob;
98661      *(z++) = hexdigits[(c>>4)&0xf];
98662      *(z++) = hexdigits[c&0xf];
98663    }
98664    *z = 0;
98665    sqlite3_result_text(context, zHex, n*2, sqlite3_free);
98666  }
98667}
98668
98669/*
98670** The zeroblob(N) function returns a zero-filled blob of size N bytes.
98671*/
98672static void zeroblobFunc(
98673  sqlite3_context *context,
98674  int argc,
98675  sqlite3_value **argv
98676){
98677  i64 n;
98678  int rc;
98679  assert( argc==1 );
98680  UNUSED_PARAMETER(argc);
98681  n = sqlite3_value_int64(argv[0]);
98682  if( n<0 ) n = 0;
98683  rc = sqlite3_result_zeroblob64(context, n); /* IMP: R-00293-64994 */
98684  if( rc ){
98685    sqlite3_result_error_code(context, rc);
98686  }
98687}
98688
98689/*
98690** The replace() function.  Three arguments are all strings: call
98691** them A, B, and C. The result is also a string which is derived
98692** from A by replacing every occurrence of B with C.  The match
98693** must be exact.  Collating sequences are not used.
98694*/
98695static void replaceFunc(
98696  sqlite3_context *context,
98697  int argc,
98698  sqlite3_value **argv
98699){
98700  const unsigned char *zStr;        /* The input string A */
98701  const unsigned char *zPattern;    /* The pattern string B */
98702  const unsigned char *zRep;        /* The replacement string C */
98703  unsigned char *zOut;              /* The output */
98704  int nStr;                /* Size of zStr */
98705  int nPattern;            /* Size of zPattern */
98706  int nRep;                /* Size of zRep */
98707  i64 nOut;                /* Maximum size of zOut */
98708  int loopLimit;           /* Last zStr[] that might match zPattern[] */
98709  int i, j;                /* Loop counters */
98710
98711  assert( argc==3 );
98712  UNUSED_PARAMETER(argc);
98713  zStr = sqlite3_value_text(argv[0]);
98714  if( zStr==0 ) return;
98715  nStr = sqlite3_value_bytes(argv[0]);
98716  assert( zStr==sqlite3_value_text(argv[0]) );  /* No encoding change */
98717  zPattern = sqlite3_value_text(argv[1]);
98718  if( zPattern==0 ){
98719    assert( sqlite3_value_type(argv[1])==SQLITE_NULL
98720            || sqlite3_context_db_handle(context)->mallocFailed );
98721    return;
98722  }
98723  if( zPattern[0]==0 ){
98724    assert( sqlite3_value_type(argv[1])!=SQLITE_NULL );
98725    sqlite3_result_value(context, argv[0]);
98726    return;
98727  }
98728  nPattern = sqlite3_value_bytes(argv[1]);
98729  assert( zPattern==sqlite3_value_text(argv[1]) );  /* No encoding change */
98730  zRep = sqlite3_value_text(argv[2]);
98731  if( zRep==0 ) return;
98732  nRep = sqlite3_value_bytes(argv[2]);
98733  assert( zRep==sqlite3_value_text(argv[2]) );
98734  nOut = nStr + 1;
98735  assert( nOut<SQLITE_MAX_LENGTH );
98736  zOut = contextMalloc(context, (i64)nOut);
98737  if( zOut==0 ){
98738    return;
98739  }
98740  loopLimit = nStr - nPattern;
98741  for(i=j=0; i<=loopLimit; i++){
98742    if( zStr[i]!=zPattern[0] || memcmp(&zStr[i], zPattern, nPattern) ){
98743      zOut[j++] = zStr[i];
98744    }else{
98745      u8 *zOld;
98746      sqlite3 *db = sqlite3_context_db_handle(context);
98747      nOut += nRep - nPattern;
98748      testcase( nOut-1==db->aLimit[SQLITE_LIMIT_LENGTH] );
98749      testcase( nOut-2==db->aLimit[SQLITE_LIMIT_LENGTH] );
98750      if( nOut-1>db->aLimit[SQLITE_LIMIT_LENGTH] ){
98751        sqlite3_result_error_toobig(context);
98752        sqlite3_free(zOut);
98753        return;
98754      }
98755      zOld = zOut;
98756      zOut = sqlite3_realloc64(zOut, (int)nOut);
98757      if( zOut==0 ){
98758        sqlite3_result_error_nomem(context);
98759        sqlite3_free(zOld);
98760        return;
98761      }
98762      memcpy(&zOut[j], zRep, nRep);
98763      j += nRep;
98764      i += nPattern-1;
98765    }
98766  }
98767  assert( j+nStr-i+1==nOut );
98768  memcpy(&zOut[j], &zStr[i], nStr-i);
98769  j += nStr - i;
98770  assert( j<=nOut );
98771  zOut[j] = 0;
98772  sqlite3_result_text(context, (char*)zOut, j, sqlite3_free);
98773}
98774
98775/*
98776** Implementation of the TRIM(), LTRIM(), and RTRIM() functions.
98777** The userdata is 0x1 for left trim, 0x2 for right trim, 0x3 for both.
98778*/
98779static void trimFunc(
98780  sqlite3_context *context,
98781  int argc,
98782  sqlite3_value **argv
98783){
98784  const unsigned char *zIn;         /* Input string */
98785  const unsigned char *zCharSet;    /* Set of characters to trim */
98786  int nIn;                          /* Number of bytes in input */
98787  int flags;                        /* 1: trimleft  2: trimright  3: trim */
98788  int i;                            /* Loop counter */
98789  unsigned char *aLen = 0;          /* Length of each character in zCharSet */
98790  unsigned char **azChar = 0;       /* Individual characters in zCharSet */
98791  int nChar;                        /* Number of characters in zCharSet */
98792
98793  if( sqlite3_value_type(argv[0])==SQLITE_NULL ){
98794    return;
98795  }
98796  zIn = sqlite3_value_text(argv[0]);
98797  if( zIn==0 ) return;
98798  nIn = sqlite3_value_bytes(argv[0]);
98799  assert( zIn==sqlite3_value_text(argv[0]) );
98800  if( argc==1 ){
98801    static const unsigned char lenOne[] = { 1 };
98802    static unsigned char * const azOne[] = { (u8*)" " };
98803    nChar = 1;
98804    aLen = (u8*)lenOne;
98805    azChar = (unsigned char **)azOne;
98806    zCharSet = 0;
98807  }else if( (zCharSet = sqlite3_value_text(argv[1]))==0 ){
98808    return;
98809  }else{
98810    const unsigned char *z;
98811    for(z=zCharSet, nChar=0; *z; nChar++){
98812      SQLITE_SKIP_UTF8(z);
98813    }
98814    if( nChar>0 ){
98815      azChar = contextMalloc(context, ((i64)nChar)*(sizeof(char*)+1));
98816      if( azChar==0 ){
98817        return;
98818      }
98819      aLen = (unsigned char*)&azChar[nChar];
98820      for(z=zCharSet, nChar=0; *z; nChar++){
98821        azChar[nChar] = (unsigned char *)z;
98822        SQLITE_SKIP_UTF8(z);
98823        aLen[nChar] = (u8)(z - azChar[nChar]);
98824      }
98825    }
98826  }
98827  if( nChar>0 ){
98828    flags = SQLITE_PTR_TO_INT(sqlite3_user_data(context));
98829    if( flags & 1 ){
98830      while( nIn>0 ){
98831        int len = 0;
98832        for(i=0; i<nChar; i++){
98833          len = aLen[i];
98834          if( len<=nIn && memcmp(zIn, azChar[i], len)==0 ) break;
98835        }
98836        if( i>=nChar ) break;
98837        zIn += len;
98838        nIn -= len;
98839      }
98840    }
98841    if( flags & 2 ){
98842      while( nIn>0 ){
98843        int len = 0;
98844        for(i=0; i<nChar; i++){
98845          len = aLen[i];
98846          if( len<=nIn && memcmp(&zIn[nIn-len],azChar[i],len)==0 ) break;
98847        }
98848        if( i>=nChar ) break;
98849        nIn -= len;
98850      }
98851    }
98852    if( zCharSet ){
98853      sqlite3_free(azChar);
98854    }
98855  }
98856  sqlite3_result_text(context, (char*)zIn, nIn, SQLITE_TRANSIENT);
98857}
98858
98859
98860/* IMP: R-25361-16150 This function is omitted from SQLite by default. It
98861** is only available if the SQLITE_SOUNDEX compile-time option is used
98862** when SQLite is built.
98863*/
98864#ifdef SQLITE_SOUNDEX
98865/*
98866** Compute the soundex encoding of a word.
98867**
98868** IMP: R-59782-00072 The soundex(X) function returns a string that is the
98869** soundex encoding of the string X.
98870*/
98871static void soundexFunc(
98872  sqlite3_context *context,
98873  int argc,
98874  sqlite3_value **argv
98875){
98876  char zResult[8];
98877  const u8 *zIn;
98878  int i, j;
98879  static const unsigned char iCode[] = {
98880    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
98881    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
98882    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
98883    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
98884    0, 0, 1, 2, 3, 0, 1, 2, 0, 0, 2, 2, 4, 5, 5, 0,
98885    1, 2, 6, 2, 3, 0, 1, 0, 2, 0, 2, 0, 0, 0, 0, 0,
98886    0, 0, 1, 2, 3, 0, 1, 2, 0, 0, 2, 2, 4, 5, 5, 0,
98887    1, 2, 6, 2, 3, 0, 1, 0, 2, 0, 2, 0, 0, 0, 0, 0,
98888  };
98889  assert( argc==1 );
98890  zIn = (u8*)sqlite3_value_text(argv[0]);
98891  if( zIn==0 ) zIn = (u8*)"";
98892  for(i=0; zIn[i] && !sqlite3Isalpha(zIn[i]); i++){}
98893  if( zIn[i] ){
98894    u8 prevcode = iCode[zIn[i]&0x7f];
98895    zResult[0] = sqlite3Toupper(zIn[i]);
98896    for(j=1; j<4 && zIn[i]; i++){
98897      int code = iCode[zIn[i]&0x7f];
98898      if( code>0 ){
98899        if( code!=prevcode ){
98900          prevcode = code;
98901          zResult[j++] = code + '0';
98902        }
98903      }else{
98904        prevcode = 0;
98905      }
98906    }
98907    while( j<4 ){
98908      zResult[j++] = '0';
98909    }
98910    zResult[j] = 0;
98911    sqlite3_result_text(context, zResult, 4, SQLITE_TRANSIENT);
98912  }else{
98913    /* IMP: R-64894-50321 The string "?000" is returned if the argument
98914    ** is NULL or contains no ASCII alphabetic characters. */
98915    sqlite3_result_text(context, "?000", 4, SQLITE_STATIC);
98916  }
98917}
98918#endif /* SQLITE_SOUNDEX */
98919
98920#ifndef SQLITE_OMIT_LOAD_EXTENSION
98921/*
98922** A function that loads a shared-library extension then returns NULL.
98923*/
98924static void loadExt(sqlite3_context *context, int argc, sqlite3_value **argv){
98925  const char *zFile = (const char *)sqlite3_value_text(argv[0]);
98926  const char *zProc;
98927  sqlite3 *db = sqlite3_context_db_handle(context);
98928  char *zErrMsg = 0;
98929
98930  if( argc==2 ){
98931    zProc = (const char *)sqlite3_value_text(argv[1]);
98932  }else{
98933    zProc = 0;
98934  }
98935  if( zFile && sqlite3_load_extension(db, zFile, zProc, &zErrMsg) ){
98936    sqlite3_result_error(context, zErrMsg, -1);
98937    sqlite3_free(zErrMsg);
98938  }
98939}
98940#endif
98941
98942
98943/*
98944** An instance of the following structure holds the context of a
98945** sum() or avg() aggregate computation.
98946*/
98947typedef struct SumCtx SumCtx;
98948struct SumCtx {
98949  double rSum;      /* Floating point sum */
98950  i64 iSum;         /* Integer sum */
98951  i64 cnt;          /* Number of elements summed */
98952  u8 overflow;      /* True if integer overflow seen */
98953  u8 approx;        /* True if non-integer value was input to the sum */
98954};
98955
98956/*
98957** Routines used to compute the sum, average, and total.
98958**
98959** The SUM() function follows the (broken) SQL standard which means
98960** that it returns NULL if it sums over no inputs.  TOTAL returns
98961** 0.0 in that case.  In addition, TOTAL always returns a float where
98962** SUM might return an integer if it never encounters a floating point
98963** value.  TOTAL never fails, but SUM might through an exception if
98964** it overflows an integer.
98965*/
98966static void sumStep(sqlite3_context *context, int argc, sqlite3_value **argv){
98967  SumCtx *p;
98968  int type;
98969  assert( argc==1 );
98970  UNUSED_PARAMETER(argc);
98971  p = sqlite3_aggregate_context(context, sizeof(*p));
98972  type = sqlite3_value_numeric_type(argv[0]);
98973  if( p && type!=SQLITE_NULL ){
98974    p->cnt++;
98975    if( type==SQLITE_INTEGER ){
98976      i64 v = sqlite3_value_int64(argv[0]);
98977      p->rSum += v;
98978      if( (p->approx|p->overflow)==0 && sqlite3AddInt64(&p->iSum, v) ){
98979        p->overflow = 1;
98980      }
98981    }else{
98982      p->rSum += sqlite3_value_double(argv[0]);
98983      p->approx = 1;
98984    }
98985  }
98986}
98987static void sumFinalize(sqlite3_context *context){
98988  SumCtx *p;
98989  p = sqlite3_aggregate_context(context, 0);
98990  if( p && p->cnt>0 ){
98991    if( p->overflow ){
98992      sqlite3_result_error(context,"integer overflow",-1);
98993    }else if( p->approx ){
98994      sqlite3_result_double(context, p->rSum);
98995    }else{
98996      sqlite3_result_int64(context, p->iSum);
98997    }
98998  }
98999}
99000static void avgFinalize(sqlite3_context *context){
99001  SumCtx *p;
99002  p = sqlite3_aggregate_context(context, 0);
99003  if( p && p->cnt>0 ){
99004    sqlite3_result_double(context, p->rSum/(double)p->cnt);
99005  }
99006}
99007static void totalFinalize(sqlite3_context *context){
99008  SumCtx *p;
99009  p = sqlite3_aggregate_context(context, 0);
99010  /* (double)0 In case of SQLITE_OMIT_FLOATING_POINT... */
99011  sqlite3_result_double(context, p ? p->rSum : (double)0);
99012}
99013
99014/*
99015** The following structure keeps track of state information for the
99016** count() aggregate function.
99017*/
99018typedef struct CountCtx CountCtx;
99019struct CountCtx {
99020  i64 n;
99021};
99022
99023/*
99024** Routines to implement the count() aggregate function.
99025*/
99026static void countStep(sqlite3_context *context, int argc, sqlite3_value **argv){
99027  CountCtx *p;
99028  p = sqlite3_aggregate_context(context, sizeof(*p));
99029  if( (argc==0 || SQLITE_NULL!=sqlite3_value_type(argv[0])) && p ){
99030    p->n++;
99031  }
99032
99033#ifndef SQLITE_OMIT_DEPRECATED
99034  /* The sqlite3_aggregate_count() function is deprecated.  But just to make
99035  ** sure it still operates correctly, verify that its count agrees with our
99036  ** internal count when using count(*) and when the total count can be
99037  ** expressed as a 32-bit integer. */
99038  assert( argc==1 || p==0 || p->n>0x7fffffff
99039          || p->n==sqlite3_aggregate_count(context) );
99040#endif
99041}
99042static void countFinalize(sqlite3_context *context){
99043  CountCtx *p;
99044  p = sqlite3_aggregate_context(context, 0);
99045  sqlite3_result_int64(context, p ? p->n : 0);
99046}
99047
99048/*
99049** Routines to implement min() and max() aggregate functions.
99050*/
99051static void minmaxStep(
99052  sqlite3_context *context,
99053  int NotUsed,
99054  sqlite3_value **argv
99055){
99056  Mem *pArg  = (Mem *)argv[0];
99057  Mem *pBest;
99058  UNUSED_PARAMETER(NotUsed);
99059
99060  pBest = (Mem *)sqlite3_aggregate_context(context, sizeof(*pBest));
99061  if( !pBest ) return;
99062
99063  if( sqlite3_value_type(argv[0])==SQLITE_NULL ){
99064    if( pBest->flags ) sqlite3SkipAccumulatorLoad(context);
99065  }else if( pBest->flags ){
99066    int max;
99067    int cmp;
99068    CollSeq *pColl = sqlite3GetFuncCollSeq(context);
99069    /* This step function is used for both the min() and max() aggregates,
99070    ** the only difference between the two being that the sense of the
99071    ** comparison is inverted. For the max() aggregate, the
99072    ** sqlite3_user_data() function returns (void *)-1. For min() it
99073    ** returns (void *)db, where db is the sqlite3* database pointer.
99074    ** Therefore the next statement sets variable 'max' to 1 for the max()
99075    ** aggregate, or 0 for min().
99076    */
99077    max = sqlite3_user_data(context)!=0;
99078    cmp = sqlite3MemCompare(pBest, pArg, pColl);
99079    if( (max && cmp<0) || (!max && cmp>0) ){
99080      sqlite3VdbeMemCopy(pBest, pArg);
99081    }else{
99082      sqlite3SkipAccumulatorLoad(context);
99083    }
99084  }else{
99085    pBest->db = sqlite3_context_db_handle(context);
99086    sqlite3VdbeMemCopy(pBest, pArg);
99087  }
99088}
99089static void minMaxFinalize(sqlite3_context *context){
99090  sqlite3_value *pRes;
99091  pRes = (sqlite3_value *)sqlite3_aggregate_context(context, 0);
99092  if( pRes ){
99093    if( pRes->flags ){
99094      sqlite3_result_value(context, pRes);
99095    }
99096    sqlite3VdbeMemRelease(pRes);
99097  }
99098}
99099
99100/*
99101** group_concat(EXPR, ?SEPARATOR?)
99102*/
99103static void groupConcatStep(
99104  sqlite3_context *context,
99105  int argc,
99106  sqlite3_value **argv
99107){
99108  const char *zVal;
99109  StrAccum *pAccum;
99110  const char *zSep;
99111  int nVal, nSep;
99112  assert( argc==1 || argc==2 );
99113  if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return;
99114  pAccum = (StrAccum*)sqlite3_aggregate_context(context, sizeof(*pAccum));
99115
99116  if( pAccum ){
99117    sqlite3 *db = sqlite3_context_db_handle(context);
99118    int firstTerm = pAccum->mxAlloc==0;
99119    pAccum->mxAlloc = db->aLimit[SQLITE_LIMIT_LENGTH];
99120    if( !firstTerm ){
99121      if( argc==2 ){
99122        zSep = (char*)sqlite3_value_text(argv[1]);
99123        nSep = sqlite3_value_bytes(argv[1]);
99124      }else{
99125        zSep = ",";
99126        nSep = 1;
99127      }
99128      if( nSep ) sqlite3StrAccumAppend(pAccum, zSep, nSep);
99129    }
99130    zVal = (char*)sqlite3_value_text(argv[0]);
99131    nVal = sqlite3_value_bytes(argv[0]);
99132    if( zVal ) sqlite3StrAccumAppend(pAccum, zVal, nVal);
99133  }
99134}
99135static void groupConcatFinalize(sqlite3_context *context){
99136  StrAccum *pAccum;
99137  pAccum = sqlite3_aggregate_context(context, 0);
99138  if( pAccum ){
99139    if( pAccum->accError==STRACCUM_TOOBIG ){
99140      sqlite3_result_error_toobig(context);
99141    }else if( pAccum->accError==STRACCUM_NOMEM ){
99142      sqlite3_result_error_nomem(context);
99143    }else{
99144      sqlite3_result_text(context, sqlite3StrAccumFinish(pAccum), -1,
99145                          sqlite3_free);
99146    }
99147  }
99148}
99149
99150/*
99151** This routine does per-connection function registration.  Most
99152** of the built-in functions above are part of the global function set.
99153** This routine only deals with those that are not global.
99154*/
99155SQLITE_PRIVATE void sqlite3RegisterBuiltinFunctions(sqlite3 *db){
99156  int rc = sqlite3_overload_function(db, "MATCH", 2);
99157  assert( rc==SQLITE_NOMEM || rc==SQLITE_OK );
99158  if( rc==SQLITE_NOMEM ){
99159    db->mallocFailed = 1;
99160  }
99161}
99162
99163/*
99164** Set the LIKEOPT flag on the 2-argument function with the given name.
99165*/
99166static void setLikeOptFlag(sqlite3 *db, const char *zName, u8 flagVal){
99167  FuncDef *pDef;
99168  pDef = sqlite3FindFunction(db, zName, sqlite3Strlen30(zName),
99169                             2, SQLITE_UTF8, 0);
99170  if( ALWAYS(pDef) ){
99171    pDef->funcFlags |= flagVal;
99172  }
99173}
99174
99175/*
99176** Register the built-in LIKE and GLOB functions.  The caseSensitive
99177** parameter determines whether or not the LIKE operator is case
99178** sensitive.  GLOB is always case sensitive.
99179*/
99180SQLITE_PRIVATE void sqlite3RegisterLikeFunctions(sqlite3 *db, int caseSensitive){
99181  struct compareInfo *pInfo;
99182  if( caseSensitive ){
99183    pInfo = (struct compareInfo*)&likeInfoAlt;
99184  }else{
99185    pInfo = (struct compareInfo*)&likeInfoNorm;
99186  }
99187  sqlite3CreateFunc(db, "like", 2, SQLITE_UTF8, pInfo, likeFunc, 0, 0, 0);
99188  sqlite3CreateFunc(db, "like", 3, SQLITE_UTF8, pInfo, likeFunc, 0, 0, 0);
99189  sqlite3CreateFunc(db, "glob", 2, SQLITE_UTF8,
99190      (struct compareInfo*)&globInfo, likeFunc, 0, 0, 0);
99191  setLikeOptFlag(db, "glob", SQLITE_FUNC_LIKE | SQLITE_FUNC_CASE);
99192  setLikeOptFlag(db, "like",
99193      caseSensitive ? (SQLITE_FUNC_LIKE | SQLITE_FUNC_CASE) : SQLITE_FUNC_LIKE);
99194}
99195
99196/*
99197** pExpr points to an expression which implements a function.  If
99198** it is appropriate to apply the LIKE optimization to that function
99199** then set aWc[0] through aWc[2] to the wildcard characters and
99200** return TRUE.  If the function is not a LIKE-style function then
99201** return FALSE.
99202**
99203** *pIsNocase is set to true if uppercase and lowercase are equivalent for
99204** the function (default for LIKE).  If the function makes the distinction
99205** between uppercase and lowercase (as does GLOB) then *pIsNocase is set to
99206** false.
99207*/
99208SQLITE_PRIVATE int sqlite3IsLikeFunction(sqlite3 *db, Expr *pExpr, int *pIsNocase, char *aWc){
99209  FuncDef *pDef;
99210  if( pExpr->op!=TK_FUNCTION
99211   || !pExpr->x.pList
99212   || pExpr->x.pList->nExpr!=2
99213  ){
99214    return 0;
99215  }
99216  assert( !ExprHasProperty(pExpr, EP_xIsSelect) );
99217  pDef = sqlite3FindFunction(db, pExpr->u.zToken,
99218                             sqlite3Strlen30(pExpr->u.zToken),
99219                             2, SQLITE_UTF8, 0);
99220  if( NEVER(pDef==0) || (pDef->funcFlags & SQLITE_FUNC_LIKE)==0 ){
99221    return 0;
99222  }
99223
99224  /* The memcpy() statement assumes that the wildcard characters are
99225  ** the first three statements in the compareInfo structure.  The
99226  ** asserts() that follow verify that assumption
99227  */
99228  memcpy(aWc, pDef->pUserData, 3);
99229  assert( (char*)&likeInfoAlt == (char*)&likeInfoAlt.matchAll );
99230  assert( &((char*)&likeInfoAlt)[1] == (char*)&likeInfoAlt.matchOne );
99231  assert( &((char*)&likeInfoAlt)[2] == (char*)&likeInfoAlt.matchSet );
99232  *pIsNocase = (pDef->funcFlags & SQLITE_FUNC_CASE)==0;
99233  return 1;
99234}
99235
99236/*
99237** All of the FuncDef structures in the aBuiltinFunc[] array above
99238** to the global function hash table.  This occurs at start-time (as
99239** a consequence of calling sqlite3_initialize()).
99240**
99241** After this routine runs
99242*/
99243SQLITE_PRIVATE void sqlite3RegisterGlobalFunctions(void){
99244  /*
99245  ** The following array holds FuncDef structures for all of the functions
99246  ** defined in this file.
99247  **
99248  ** The array cannot be constant since changes are made to the
99249  ** FuncDef.pHash elements at start-time.  The elements of this array
99250  ** are read-only after initialization is complete.
99251  */
99252  static SQLITE_WSD FuncDef aBuiltinFunc[] = {
99253    FUNCTION(ltrim,              1, 1, 0, trimFunc         ),
99254    FUNCTION(ltrim,              2, 1, 0, trimFunc         ),
99255    FUNCTION(rtrim,              1, 2, 0, trimFunc         ),
99256    FUNCTION(rtrim,              2, 2, 0, trimFunc         ),
99257    FUNCTION(trim,               1, 3, 0, trimFunc         ),
99258    FUNCTION(trim,               2, 3, 0, trimFunc         ),
99259    FUNCTION(min,               -1, 0, 1, minmaxFunc       ),
99260    FUNCTION(min,                0, 0, 1, 0                ),
99261    AGGREGATE2(min,              1, 0, 1, minmaxStep,      minMaxFinalize,
99262                                          SQLITE_FUNC_MINMAX ),
99263    FUNCTION(max,               -1, 1, 1, minmaxFunc       ),
99264    FUNCTION(max,                0, 1, 1, 0                ),
99265    AGGREGATE2(max,              1, 1, 1, minmaxStep,      minMaxFinalize,
99266                                          SQLITE_FUNC_MINMAX ),
99267    FUNCTION2(typeof,            1, 0, 0, typeofFunc,  SQLITE_FUNC_TYPEOF),
99268    FUNCTION2(length,            1, 0, 0, lengthFunc,  SQLITE_FUNC_LENGTH),
99269    FUNCTION(instr,              2, 0, 0, instrFunc        ),
99270    FUNCTION(substr,             2, 0, 0, substrFunc       ),
99271    FUNCTION(substr,             3, 0, 0, substrFunc       ),
99272    FUNCTION(printf,            -1, 0, 0, printfFunc       ),
99273    FUNCTION(unicode,            1, 0, 0, unicodeFunc      ),
99274    FUNCTION(char,              -1, 0, 0, charFunc         ),
99275    FUNCTION(abs,                1, 0, 0, absFunc          ),
99276#ifndef SQLITE_OMIT_FLOATING_POINT
99277    FUNCTION(round,              1, 0, 0, roundFunc        ),
99278    FUNCTION(round,              2, 0, 0, roundFunc        ),
99279#endif
99280    FUNCTION(upper,              1, 0, 0, upperFunc        ),
99281    FUNCTION(lower,              1, 0, 0, lowerFunc        ),
99282    FUNCTION(coalesce,           1, 0, 0, 0                ),
99283    FUNCTION(coalesce,           0, 0, 0, 0                ),
99284    FUNCTION2(coalesce,         -1, 0, 0, noopFunc,  SQLITE_FUNC_COALESCE),
99285    FUNCTION(hex,                1, 0, 0, hexFunc          ),
99286    FUNCTION2(ifnull,            2, 0, 0, noopFunc,  SQLITE_FUNC_COALESCE),
99287    FUNCTION2(unlikely,          1, 0, 0, noopFunc,  SQLITE_FUNC_UNLIKELY),
99288    FUNCTION2(likelihood,        2, 0, 0, noopFunc,  SQLITE_FUNC_UNLIKELY),
99289    FUNCTION2(likely,            1, 0, 0, noopFunc,  SQLITE_FUNC_UNLIKELY),
99290    VFUNCTION(random,            0, 0, 0, randomFunc       ),
99291    VFUNCTION(randomblob,        1, 0, 0, randomBlob       ),
99292    FUNCTION(nullif,             2, 0, 1, nullifFunc       ),
99293    FUNCTION(sqlite_version,     0, 0, 0, versionFunc      ),
99294    FUNCTION(sqlite_source_id,   0, 0, 0, sourceidFunc     ),
99295    FUNCTION(sqlite_log,         2, 0, 0, errlogFunc       ),
99296#if SQLITE_USER_AUTHENTICATION
99297    FUNCTION(sqlite_crypt,       2, 0, 0, sqlite3CryptFunc ),
99298#endif
99299#ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
99300    FUNCTION(sqlite_compileoption_used,1, 0, 0, compileoptionusedFunc  ),
99301    FUNCTION(sqlite_compileoption_get, 1, 0, 0, compileoptiongetFunc  ),
99302#endif /* SQLITE_OMIT_COMPILEOPTION_DIAGS */
99303    FUNCTION(quote,              1, 0, 0, quoteFunc        ),
99304    VFUNCTION(last_insert_rowid, 0, 0, 0, last_insert_rowid),
99305    VFUNCTION(changes,           0, 0, 0, changes          ),
99306    VFUNCTION(total_changes,     0, 0, 0, total_changes    ),
99307    FUNCTION(replace,            3, 0, 0, replaceFunc      ),
99308    FUNCTION(zeroblob,           1, 0, 0, zeroblobFunc     ),
99309  #ifdef SQLITE_SOUNDEX
99310    FUNCTION(soundex,            1, 0, 0, soundexFunc      ),
99311  #endif
99312  #ifndef SQLITE_OMIT_LOAD_EXTENSION
99313    FUNCTION(load_extension,     1, 0, 0, loadExt          ),
99314    FUNCTION(load_extension,     2, 0, 0, loadExt          ),
99315  #endif
99316    AGGREGATE(sum,               1, 0, 0, sumStep,         sumFinalize    ),
99317    AGGREGATE(total,             1, 0, 0, sumStep,         totalFinalize    ),
99318    AGGREGATE(avg,               1, 0, 0, sumStep,         avgFinalize    ),
99319    AGGREGATE2(count,            0, 0, 0, countStep,       countFinalize,
99320               SQLITE_FUNC_COUNT  ),
99321    AGGREGATE(count,             1, 0, 0, countStep,       countFinalize  ),
99322    AGGREGATE(group_concat,      1, 0, 0, groupConcatStep, groupConcatFinalize),
99323    AGGREGATE(group_concat,      2, 0, 0, groupConcatStep, groupConcatFinalize),
99324
99325    LIKEFUNC(glob, 2, &globInfo, SQLITE_FUNC_LIKE|SQLITE_FUNC_CASE),
99326  #ifdef SQLITE_CASE_SENSITIVE_LIKE
99327    LIKEFUNC(like, 2, &likeInfoAlt, SQLITE_FUNC_LIKE|SQLITE_FUNC_CASE),
99328    LIKEFUNC(like, 3, &likeInfoAlt, SQLITE_FUNC_LIKE|SQLITE_FUNC_CASE),
99329  #else
99330    LIKEFUNC(like, 2, &likeInfoNorm, SQLITE_FUNC_LIKE),
99331    LIKEFUNC(like, 3, &likeInfoNorm, SQLITE_FUNC_LIKE),
99332  #endif
99333  };
99334
99335  int i;
99336  FuncDefHash *pHash = &GLOBAL(FuncDefHash, sqlite3GlobalFunctions);
99337  FuncDef *aFunc = (FuncDef*)&GLOBAL(FuncDef, aBuiltinFunc);
99338
99339  for(i=0; i<ArraySize(aBuiltinFunc); i++){
99340    sqlite3FuncDefInsert(pHash, &aFunc[i]);
99341  }
99342  sqlite3RegisterDateTimeFunctions();
99343#ifndef SQLITE_OMIT_ALTERTABLE
99344  sqlite3AlterFunctions();
99345#endif
99346#if defined(SQLITE_ENABLE_STAT3) || defined(SQLITE_ENABLE_STAT4)
99347  sqlite3AnalyzeFunctions();
99348#endif
99349}
99350
99351/************** End of func.c ************************************************/
99352/************** Begin file fkey.c ********************************************/
99353/*
99354**
99355** The author disclaims copyright to this source code.  In place of
99356** a legal notice, here is a blessing:
99357**
99358**    May you do good and not evil.
99359**    May you find forgiveness for yourself and forgive others.
99360**    May you share freely, never taking more than you give.
99361**
99362*************************************************************************
99363** This file contains code used by the compiler to add foreign key
99364** support to compiled SQL statements.
99365*/
99366/* #include "sqliteInt.h" */
99367
99368#ifndef SQLITE_OMIT_FOREIGN_KEY
99369#ifndef SQLITE_OMIT_TRIGGER
99370
99371/*
99372** Deferred and Immediate FKs
99373** --------------------------
99374**
99375** Foreign keys in SQLite come in two flavours: deferred and immediate.
99376** If an immediate foreign key constraint is violated,
99377** SQLITE_CONSTRAINT_FOREIGNKEY is returned and the current
99378** statement transaction rolled back. If a
99379** deferred foreign key constraint is violated, no action is taken
99380** immediately. However if the application attempts to commit the
99381** transaction before fixing the constraint violation, the attempt fails.
99382**
99383** Deferred constraints are implemented using a simple counter associated
99384** with the database handle. The counter is set to zero each time a
99385** database transaction is opened. Each time a statement is executed
99386** that causes a foreign key violation, the counter is incremented. Each
99387** time a statement is executed that removes an existing violation from
99388** the database, the counter is decremented. When the transaction is
99389** committed, the commit fails if the current value of the counter is
99390** greater than zero. This scheme has two big drawbacks:
99391**
99392**   * When a commit fails due to a deferred foreign key constraint,
99393**     there is no way to tell which foreign constraint is not satisfied,
99394**     or which row it is not satisfied for.
99395**
99396**   * If the database contains foreign key violations when the
99397**     transaction is opened, this may cause the mechanism to malfunction.
99398**
99399** Despite these problems, this approach is adopted as it seems simpler
99400** than the alternatives.
99401**
99402** INSERT operations:
99403**
99404**   I.1) For each FK for which the table is the child table, search
99405**        the parent table for a match. If none is found increment the
99406**        constraint counter.
99407**
99408**   I.2) For each FK for which the table is the parent table,
99409**        search the child table for rows that correspond to the new
99410**        row in the parent table. Decrement the counter for each row
99411**        found (as the constraint is now satisfied).
99412**
99413** DELETE operations:
99414**
99415**   D.1) For each FK for which the table is the child table,
99416**        search the parent table for a row that corresponds to the
99417**        deleted row in the child table. If such a row is not found,
99418**        decrement the counter.
99419**
99420**   D.2) For each FK for which the table is the parent table, search
99421**        the child table for rows that correspond to the deleted row
99422**        in the parent table. For each found increment the counter.
99423**
99424** UPDATE operations:
99425**
99426**   An UPDATE command requires that all 4 steps above are taken, but only
99427**   for FK constraints for which the affected columns are actually
99428**   modified (values must be compared at runtime).
99429**
99430** Note that I.1 and D.1 are very similar operations, as are I.2 and D.2.
99431** This simplifies the implementation a bit.
99432**
99433** For the purposes of immediate FK constraints, the OR REPLACE conflict
99434** resolution is considered to delete rows before the new row is inserted.
99435** If a delete caused by OR REPLACE violates an FK constraint, an exception
99436** is thrown, even if the FK constraint would be satisfied after the new
99437** row is inserted.
99438**
99439** Immediate constraints are usually handled similarly. The only difference
99440** is that the counter used is stored as part of each individual statement
99441** object (struct Vdbe). If, after the statement has run, its immediate
99442** constraint counter is greater than zero,
99443** it returns SQLITE_CONSTRAINT_FOREIGNKEY
99444** and the statement transaction is rolled back. An exception is an INSERT
99445** statement that inserts a single row only (no triggers). In this case,
99446** instead of using a counter, an exception is thrown immediately if the
99447** INSERT violates a foreign key constraint. This is necessary as such
99448** an INSERT does not open a statement transaction.
99449**
99450** TODO: How should dropping a table be handled? How should renaming a
99451** table be handled?
99452**
99453**
99454** Query API Notes
99455** ---------------
99456**
99457** Before coding an UPDATE or DELETE row operation, the code-generator
99458** for those two operations needs to know whether or not the operation
99459** requires any FK processing and, if so, which columns of the original
99460** row are required by the FK processing VDBE code (i.e. if FKs were
99461** implemented using triggers, which of the old.* columns would be
99462** accessed). No information is required by the code-generator before
99463** coding an INSERT operation. The functions used by the UPDATE/DELETE
99464** generation code to query for this information are:
99465**
99466**   sqlite3FkRequired() - Test to see if FK processing is required.
99467**   sqlite3FkOldmask()  - Query for the set of required old.* columns.
99468**
99469**
99470** Externally accessible module functions
99471** --------------------------------------
99472**
99473**   sqlite3FkCheck()    - Check for foreign key violations.
99474**   sqlite3FkActions()  - Code triggers for ON UPDATE/ON DELETE actions.
99475**   sqlite3FkDelete()   - Delete an FKey structure.
99476*/
99477
99478/*
99479** VDBE Calling Convention
99480** -----------------------
99481**
99482** Example:
99483**
99484**   For the following INSERT statement:
99485**
99486**     CREATE TABLE t1(a, b INTEGER PRIMARY KEY, c);
99487**     INSERT INTO t1 VALUES(1, 2, 3.1);
99488**
99489**   Register (x):        2    (type integer)
99490**   Register (x+1):      1    (type integer)
99491**   Register (x+2):      NULL (type NULL)
99492**   Register (x+3):      3.1  (type real)
99493*/
99494
99495/*
99496** A foreign key constraint requires that the key columns in the parent
99497** table are collectively subject to a UNIQUE or PRIMARY KEY constraint.
99498** Given that pParent is the parent table for foreign key constraint pFKey,
99499** search the schema for a unique index on the parent key columns.
99500**
99501** If successful, zero is returned. If the parent key is an INTEGER PRIMARY
99502** KEY column, then output variable *ppIdx is set to NULL. Otherwise, *ppIdx
99503** is set to point to the unique index.
99504**
99505** If the parent key consists of a single column (the foreign key constraint
99506** is not a composite foreign key), output variable *paiCol is set to NULL.
99507** Otherwise, it is set to point to an allocated array of size N, where
99508** N is the number of columns in the parent key. The first element of the
99509** array is the index of the child table column that is mapped by the FK
99510** constraint to the parent table column stored in the left-most column
99511** of index *ppIdx. The second element of the array is the index of the
99512** child table column that corresponds to the second left-most column of
99513** *ppIdx, and so on.
99514**
99515** If the required index cannot be found, either because:
99516**
99517**   1) The named parent key columns do not exist, or
99518**
99519**   2) The named parent key columns do exist, but are not subject to a
99520**      UNIQUE or PRIMARY KEY constraint, or
99521**
99522**   3) No parent key columns were provided explicitly as part of the
99523**      foreign key definition, and the parent table does not have a
99524**      PRIMARY KEY, or
99525**
99526**   4) No parent key columns were provided explicitly as part of the
99527**      foreign key definition, and the PRIMARY KEY of the parent table
99528**      consists of a different number of columns to the child key in
99529**      the child table.
99530**
99531** then non-zero is returned, and a "foreign key mismatch" error loaded
99532** into pParse. If an OOM error occurs, non-zero is returned and the
99533** pParse->db->mallocFailed flag is set.
99534*/
99535SQLITE_PRIVATE int sqlite3FkLocateIndex(
99536  Parse *pParse,                  /* Parse context to store any error in */
99537  Table *pParent,                 /* Parent table of FK constraint pFKey */
99538  FKey *pFKey,                    /* Foreign key to find index for */
99539  Index **ppIdx,                  /* OUT: Unique index on parent table */
99540  int **paiCol                    /* OUT: Map of index columns in pFKey */
99541){
99542  Index *pIdx = 0;                    /* Value to return via *ppIdx */
99543  int *aiCol = 0;                     /* Value to return via *paiCol */
99544  int nCol = pFKey->nCol;             /* Number of columns in parent key */
99545  char *zKey = pFKey->aCol[0].zCol;   /* Name of left-most parent key column */
99546
99547  /* The caller is responsible for zeroing output parameters. */
99548  assert( ppIdx && *ppIdx==0 );
99549  assert( !paiCol || *paiCol==0 );
99550  assert( pParse );
99551
99552  /* If this is a non-composite (single column) foreign key, check if it
99553  ** maps to the INTEGER PRIMARY KEY of table pParent. If so, leave *ppIdx
99554  ** and *paiCol set to zero and return early.
99555  **
99556  ** Otherwise, for a composite foreign key (more than one column), allocate
99557  ** space for the aiCol array (returned via output parameter *paiCol).
99558  ** Non-composite foreign keys do not require the aiCol array.
99559  */
99560  if( nCol==1 ){
99561    /* The FK maps to the IPK if any of the following are true:
99562    **
99563    **   1) There is an INTEGER PRIMARY KEY column and the FK is implicitly
99564    **      mapped to the primary key of table pParent, or
99565    **   2) The FK is explicitly mapped to a column declared as INTEGER
99566    **      PRIMARY KEY.
99567    */
99568    if( pParent->iPKey>=0 ){
99569      if( !zKey ) return 0;
99570      if( !sqlite3StrICmp(pParent->aCol[pParent->iPKey].zName, zKey) ) return 0;
99571    }
99572  }else if( paiCol ){
99573    assert( nCol>1 );
99574    aiCol = (int *)sqlite3DbMallocRaw(pParse->db, nCol*sizeof(int));
99575    if( !aiCol ) return 1;
99576    *paiCol = aiCol;
99577  }
99578
99579  for(pIdx=pParent->pIndex; pIdx; pIdx=pIdx->pNext){
99580    if( pIdx->nKeyCol==nCol && IsUniqueIndex(pIdx) ){
99581      /* pIdx is a UNIQUE index (or a PRIMARY KEY) and has the right number
99582      ** of columns. If each indexed column corresponds to a foreign key
99583      ** column of pFKey, then this index is a winner.  */
99584
99585      if( zKey==0 ){
99586        /* If zKey is NULL, then this foreign key is implicitly mapped to
99587        ** the PRIMARY KEY of table pParent. The PRIMARY KEY index may be
99588        ** identified by the test.  */
99589        if( IsPrimaryKeyIndex(pIdx) ){
99590          if( aiCol ){
99591            int i;
99592            for(i=0; i<nCol; i++) aiCol[i] = pFKey->aCol[i].iFrom;
99593          }
99594          break;
99595        }
99596      }else{
99597        /* If zKey is non-NULL, then this foreign key was declared to
99598        ** map to an explicit list of columns in table pParent. Check if this
99599        ** index matches those columns. Also, check that the index uses
99600        ** the default collation sequences for each column. */
99601        int i, j;
99602        for(i=0; i<nCol; i++){
99603          i16 iCol = pIdx->aiColumn[i];     /* Index of column in parent tbl */
99604          char *zDfltColl;                  /* Def. collation for column */
99605          char *zIdxCol;                    /* Name of indexed column */
99606
99607          /* If the index uses a collation sequence that is different from
99608          ** the default collation sequence for the column, this index is
99609          ** unusable. Bail out early in this case.  */
99610          zDfltColl = pParent->aCol[iCol].zColl;
99611          if( !zDfltColl ){
99612            zDfltColl = "BINARY";
99613          }
99614          if( sqlite3StrICmp(pIdx->azColl[i], zDfltColl) ) break;
99615
99616          zIdxCol = pParent->aCol[iCol].zName;
99617          for(j=0; j<nCol; j++){
99618            if( sqlite3StrICmp(pFKey->aCol[j].zCol, zIdxCol)==0 ){
99619              if( aiCol ) aiCol[i] = pFKey->aCol[j].iFrom;
99620              break;
99621            }
99622          }
99623          if( j==nCol ) break;
99624        }
99625        if( i==nCol ) break;      /* pIdx is usable */
99626      }
99627    }
99628  }
99629
99630  if( !pIdx ){
99631    if( !pParse->disableTriggers ){
99632      sqlite3ErrorMsg(pParse,
99633           "foreign key mismatch - \"%w\" referencing \"%w\"",
99634           pFKey->pFrom->zName, pFKey->zTo);
99635    }
99636    sqlite3DbFree(pParse->db, aiCol);
99637    return 1;
99638  }
99639
99640  *ppIdx = pIdx;
99641  return 0;
99642}
99643
99644/*
99645** This function is called when a row is inserted into or deleted from the
99646** child table of foreign key constraint pFKey. If an SQL UPDATE is executed
99647** on the child table of pFKey, this function is invoked twice for each row
99648** affected - once to "delete" the old row, and then again to "insert" the
99649** new row.
99650**
99651** Each time it is called, this function generates VDBE code to locate the
99652** row in the parent table that corresponds to the row being inserted into
99653** or deleted from the child table. If the parent row can be found, no
99654** special action is taken. Otherwise, if the parent row can *not* be
99655** found in the parent table:
99656**
99657**   Operation | FK type   | Action taken
99658**   --------------------------------------------------------------------------
99659**   INSERT      immediate   Increment the "immediate constraint counter".
99660**
99661**   DELETE      immediate   Decrement the "immediate constraint counter".
99662**
99663**   INSERT      deferred    Increment the "deferred constraint counter".
99664**
99665**   DELETE      deferred    Decrement the "deferred constraint counter".
99666**
99667** These operations are identified in the comment at the top of this file
99668** (fkey.c) as "I.1" and "D.1".
99669*/
99670static void fkLookupParent(
99671  Parse *pParse,        /* Parse context */
99672  int iDb,              /* Index of database housing pTab */
99673  Table *pTab,          /* Parent table of FK pFKey */
99674  Index *pIdx,          /* Unique index on parent key columns in pTab */
99675  FKey *pFKey,          /* Foreign key constraint */
99676  int *aiCol,           /* Map from parent key columns to child table columns */
99677  int regData,          /* Address of array containing child table row */
99678  int nIncr,            /* Increment constraint counter by this */
99679  int isIgnore          /* If true, pretend pTab contains all NULL values */
99680){
99681  int i;                                    /* Iterator variable */
99682  Vdbe *v = sqlite3GetVdbe(pParse);         /* Vdbe to add code to */
99683  int iCur = pParse->nTab - 1;              /* Cursor number to use */
99684  int iOk = sqlite3VdbeMakeLabel(v);        /* jump here if parent key found */
99685
99686  /* If nIncr is less than zero, then check at runtime if there are any
99687  ** outstanding constraints to resolve. If there are not, there is no need
99688  ** to check if deleting this row resolves any outstanding violations.
99689  **
99690  ** Check if any of the key columns in the child table row are NULL. If
99691  ** any are, then the constraint is considered satisfied. No need to
99692  ** search for a matching row in the parent table.  */
99693  if( nIncr<0 ){
99694    sqlite3VdbeAddOp2(v, OP_FkIfZero, pFKey->isDeferred, iOk);
99695    VdbeCoverage(v);
99696  }
99697  for(i=0; i<pFKey->nCol; i++){
99698    int iReg = aiCol[i] + regData + 1;
99699    sqlite3VdbeAddOp2(v, OP_IsNull, iReg, iOk); VdbeCoverage(v);
99700  }
99701
99702  if( isIgnore==0 ){
99703    if( pIdx==0 ){
99704      /* If pIdx is NULL, then the parent key is the INTEGER PRIMARY KEY
99705      ** column of the parent table (table pTab).  */
99706      int iMustBeInt;               /* Address of MustBeInt instruction */
99707      int regTemp = sqlite3GetTempReg(pParse);
99708
99709      /* Invoke MustBeInt to coerce the child key value to an integer (i.e.
99710      ** apply the affinity of the parent key). If this fails, then there
99711      ** is no matching parent key. Before using MustBeInt, make a copy of
99712      ** the value. Otherwise, the value inserted into the child key column
99713      ** will have INTEGER affinity applied to it, which may not be correct.  */
99714      sqlite3VdbeAddOp2(v, OP_SCopy, aiCol[0]+1+regData, regTemp);
99715      iMustBeInt = sqlite3VdbeAddOp2(v, OP_MustBeInt, regTemp, 0);
99716      VdbeCoverage(v);
99717
99718      /* If the parent table is the same as the child table, and we are about
99719      ** to increment the constraint-counter (i.e. this is an INSERT operation),
99720      ** then check if the row being inserted matches itself. If so, do not
99721      ** increment the constraint-counter.  */
99722      if( pTab==pFKey->pFrom && nIncr==1 ){
99723        sqlite3VdbeAddOp3(v, OP_Eq, regData, iOk, regTemp); VdbeCoverage(v);
99724        sqlite3VdbeChangeP5(v, SQLITE_NOTNULL);
99725      }
99726
99727      sqlite3OpenTable(pParse, iCur, iDb, pTab, OP_OpenRead);
99728      sqlite3VdbeAddOp3(v, OP_NotExists, iCur, 0, regTemp); VdbeCoverage(v);
99729      sqlite3VdbeAddOp2(v, OP_Goto, 0, iOk);
99730      sqlite3VdbeJumpHere(v, sqlite3VdbeCurrentAddr(v)-2);
99731      sqlite3VdbeJumpHere(v, iMustBeInt);
99732      sqlite3ReleaseTempReg(pParse, regTemp);
99733    }else{
99734      int nCol = pFKey->nCol;
99735      int regTemp = sqlite3GetTempRange(pParse, nCol);
99736      int regRec = sqlite3GetTempReg(pParse);
99737
99738      sqlite3VdbeAddOp3(v, OP_OpenRead, iCur, pIdx->tnum, iDb);
99739      sqlite3VdbeSetP4KeyInfo(pParse, pIdx);
99740      for(i=0; i<nCol; i++){
99741        sqlite3VdbeAddOp2(v, OP_Copy, aiCol[i]+1+regData, regTemp+i);
99742      }
99743
99744      /* If the parent table is the same as the child table, and we are about
99745      ** to increment the constraint-counter (i.e. this is an INSERT operation),
99746      ** then check if the row being inserted matches itself. If so, do not
99747      ** increment the constraint-counter.
99748      **
99749      ** If any of the parent-key values are NULL, then the row cannot match
99750      ** itself. So set JUMPIFNULL to make sure we do the OP_Found if any
99751      ** of the parent-key values are NULL (at this point it is known that
99752      ** none of the child key values are).
99753      */
99754      if( pTab==pFKey->pFrom && nIncr==1 ){
99755        int iJump = sqlite3VdbeCurrentAddr(v) + nCol + 1;
99756        for(i=0; i<nCol; i++){
99757          int iChild = aiCol[i]+1+regData;
99758          int iParent = pIdx->aiColumn[i]+1+regData;
99759          assert( aiCol[i]!=pTab->iPKey );
99760          if( pIdx->aiColumn[i]==pTab->iPKey ){
99761            /* The parent key is a composite key that includes the IPK column */
99762            iParent = regData;
99763          }
99764          sqlite3VdbeAddOp3(v, OP_Ne, iChild, iJump, iParent); VdbeCoverage(v);
99765          sqlite3VdbeChangeP5(v, SQLITE_JUMPIFNULL);
99766        }
99767        sqlite3VdbeAddOp2(v, OP_Goto, 0, iOk);
99768      }
99769
99770      sqlite3VdbeAddOp4(v, OP_MakeRecord, regTemp, nCol, regRec,
99771                        sqlite3IndexAffinityStr(v,pIdx), nCol);
99772      sqlite3VdbeAddOp4Int(v, OP_Found, iCur, iOk, regRec, 0); VdbeCoverage(v);
99773
99774      sqlite3ReleaseTempReg(pParse, regRec);
99775      sqlite3ReleaseTempRange(pParse, regTemp, nCol);
99776    }
99777  }
99778
99779  if( !pFKey->isDeferred && !(pParse->db->flags & SQLITE_DeferFKs)
99780   && !pParse->pToplevel
99781   && !pParse->isMultiWrite
99782  ){
99783    /* Special case: If this is an INSERT statement that will insert exactly
99784    ** one row into the table, raise a constraint immediately instead of
99785    ** incrementing a counter. This is necessary as the VM code is being
99786    ** generated for will not open a statement transaction.  */
99787    assert( nIncr==1 );
99788    sqlite3HaltConstraint(pParse, SQLITE_CONSTRAINT_FOREIGNKEY,
99789        OE_Abort, 0, P4_STATIC, P5_ConstraintFK);
99790  }else{
99791    if( nIncr>0 && pFKey->isDeferred==0 ){
99792      sqlite3MayAbort(pParse);
99793    }
99794    sqlite3VdbeAddOp2(v, OP_FkCounter, pFKey->isDeferred, nIncr);
99795  }
99796
99797  sqlite3VdbeResolveLabel(v, iOk);
99798  sqlite3VdbeAddOp1(v, OP_Close, iCur);
99799}
99800
99801
99802/*
99803** Return an Expr object that refers to a memory register corresponding
99804** to column iCol of table pTab.
99805**
99806** regBase is the first of an array of register that contains the data
99807** for pTab.  regBase itself holds the rowid.  regBase+1 holds the first
99808** column.  regBase+2 holds the second column, and so forth.
99809*/
99810static Expr *exprTableRegister(
99811  Parse *pParse,     /* Parsing and code generating context */
99812  Table *pTab,       /* The table whose content is at r[regBase]... */
99813  int regBase,       /* Contents of table pTab */
99814  i16 iCol           /* Which column of pTab is desired */
99815){
99816  Expr *pExpr;
99817  Column *pCol;
99818  const char *zColl;
99819  sqlite3 *db = pParse->db;
99820
99821  pExpr = sqlite3Expr(db, TK_REGISTER, 0);
99822  if( pExpr ){
99823    if( iCol>=0 && iCol!=pTab->iPKey ){
99824      pCol = &pTab->aCol[iCol];
99825      pExpr->iTable = regBase + iCol + 1;
99826      pExpr->affinity = pCol->affinity;
99827      zColl = pCol->zColl;
99828      if( zColl==0 ) zColl = db->pDfltColl->zName;
99829      pExpr = sqlite3ExprAddCollateString(pParse, pExpr, zColl);
99830    }else{
99831      pExpr->iTable = regBase;
99832      pExpr->affinity = SQLITE_AFF_INTEGER;
99833    }
99834  }
99835  return pExpr;
99836}
99837
99838/*
99839** Return an Expr object that refers to column iCol of table pTab which
99840** has cursor iCur.
99841*/
99842static Expr *exprTableColumn(
99843  sqlite3 *db,      /* The database connection */
99844  Table *pTab,      /* The table whose column is desired */
99845  int iCursor,      /* The open cursor on the table */
99846  i16 iCol          /* The column that is wanted */
99847){
99848  Expr *pExpr = sqlite3Expr(db, TK_COLUMN, 0);
99849  if( pExpr ){
99850    pExpr->pTab = pTab;
99851    pExpr->iTable = iCursor;
99852    pExpr->iColumn = iCol;
99853  }
99854  return pExpr;
99855}
99856
99857/*
99858** This function is called to generate code executed when a row is deleted
99859** from the parent table of foreign key constraint pFKey and, if pFKey is
99860** deferred, when a row is inserted into the same table. When generating
99861** code for an SQL UPDATE operation, this function may be called twice -
99862** once to "delete" the old row and once to "insert" the new row.
99863**
99864** Parameter nIncr is passed -1 when inserting a row (as this may decrease
99865** the number of FK violations in the db) or +1 when deleting one (as this
99866** may increase the number of FK constraint problems).
99867**
99868** The code generated by this function scans through the rows in the child
99869** table that correspond to the parent table row being deleted or inserted.
99870** For each child row found, one of the following actions is taken:
99871**
99872**   Operation | FK type   | Action taken
99873**   --------------------------------------------------------------------------
99874**   DELETE      immediate   Increment the "immediate constraint counter".
99875**                           Or, if the ON (UPDATE|DELETE) action is RESTRICT,
99876**                           throw a "FOREIGN KEY constraint failed" exception.
99877**
99878**   INSERT      immediate   Decrement the "immediate constraint counter".
99879**
99880**   DELETE      deferred    Increment the "deferred constraint counter".
99881**                           Or, if the ON (UPDATE|DELETE) action is RESTRICT,
99882**                           throw a "FOREIGN KEY constraint failed" exception.
99883**
99884**   INSERT      deferred    Decrement the "deferred constraint counter".
99885**
99886** These operations are identified in the comment at the top of this file
99887** (fkey.c) as "I.2" and "D.2".
99888*/
99889static void fkScanChildren(
99890  Parse *pParse,                  /* Parse context */
99891  SrcList *pSrc,                  /* The child table to be scanned */
99892  Table *pTab,                    /* The parent table */
99893  Index *pIdx,                    /* Index on parent covering the foreign key */
99894  FKey *pFKey,                    /* The foreign key linking pSrc to pTab */
99895  int *aiCol,                     /* Map from pIdx cols to child table cols */
99896  int regData,                    /* Parent row data starts here */
99897  int nIncr                       /* Amount to increment deferred counter by */
99898){
99899  sqlite3 *db = pParse->db;       /* Database handle */
99900  int i;                          /* Iterator variable */
99901  Expr *pWhere = 0;               /* WHERE clause to scan with */
99902  NameContext sNameContext;       /* Context used to resolve WHERE clause */
99903  WhereInfo *pWInfo;              /* Context used by sqlite3WhereXXX() */
99904  int iFkIfZero = 0;              /* Address of OP_FkIfZero */
99905  Vdbe *v = sqlite3GetVdbe(pParse);
99906
99907  assert( pIdx==0 || pIdx->pTable==pTab );
99908  assert( pIdx==0 || pIdx->nKeyCol==pFKey->nCol );
99909  assert( pIdx!=0 || pFKey->nCol==1 );
99910  assert( pIdx!=0 || HasRowid(pTab) );
99911
99912  if( nIncr<0 ){
99913    iFkIfZero = sqlite3VdbeAddOp2(v, OP_FkIfZero, pFKey->isDeferred, 0);
99914    VdbeCoverage(v);
99915  }
99916
99917  /* Create an Expr object representing an SQL expression like:
99918  **
99919  **   <parent-key1> = <child-key1> AND <parent-key2> = <child-key2> ...
99920  **
99921  ** The collation sequence used for the comparison should be that of
99922  ** the parent key columns. The affinity of the parent key column should
99923  ** be applied to each child key value before the comparison takes place.
99924  */
99925  for(i=0; i<pFKey->nCol; i++){
99926    Expr *pLeft;                  /* Value from parent table row */
99927    Expr *pRight;                 /* Column ref to child table */
99928    Expr *pEq;                    /* Expression (pLeft = pRight) */
99929    i16 iCol;                     /* Index of column in child table */
99930    const char *zCol;             /* Name of column in child table */
99931
99932    iCol = pIdx ? pIdx->aiColumn[i] : -1;
99933    pLeft = exprTableRegister(pParse, pTab, regData, iCol);
99934    iCol = aiCol ? aiCol[i] : pFKey->aCol[0].iFrom;
99935    assert( iCol>=0 );
99936    zCol = pFKey->pFrom->aCol[iCol].zName;
99937    pRight = sqlite3Expr(db, TK_ID, zCol);
99938    pEq = sqlite3PExpr(pParse, TK_EQ, pLeft, pRight, 0);
99939    pWhere = sqlite3ExprAnd(db, pWhere, pEq);
99940  }
99941
99942  /* If the child table is the same as the parent table, then add terms
99943  ** to the WHERE clause that prevent this entry from being scanned.
99944  ** The added WHERE clause terms are like this:
99945  **
99946  **     $current_rowid!=rowid
99947  **     NOT( $current_a==a AND $current_b==b AND ... )
99948  **
99949  ** The first form is used for rowid tables.  The second form is used
99950  ** for WITHOUT ROWID tables.  In the second form, the primary key is
99951  ** (a,b,...)
99952  */
99953  if( pTab==pFKey->pFrom && nIncr>0 ){
99954    Expr *pNe;                    /* Expression (pLeft != pRight) */
99955    Expr *pLeft;                  /* Value from parent table row */
99956    Expr *pRight;                 /* Column ref to child table */
99957    if( HasRowid(pTab) ){
99958      pLeft = exprTableRegister(pParse, pTab, regData, -1);
99959      pRight = exprTableColumn(db, pTab, pSrc->a[0].iCursor, -1);
99960      pNe = sqlite3PExpr(pParse, TK_NE, pLeft, pRight, 0);
99961    }else{
99962      Expr *pEq, *pAll = 0;
99963      Index *pPk = sqlite3PrimaryKeyIndex(pTab);
99964      assert( pIdx!=0 );
99965      for(i=0; i<pPk->nKeyCol; i++){
99966        i16 iCol = pIdx->aiColumn[i];
99967        pLeft = exprTableRegister(pParse, pTab, regData, iCol);
99968        pRight = exprTableColumn(db, pTab, pSrc->a[0].iCursor, iCol);
99969        pEq = sqlite3PExpr(pParse, TK_EQ, pLeft, pRight, 0);
99970        pAll = sqlite3ExprAnd(db, pAll, pEq);
99971      }
99972      pNe = sqlite3PExpr(pParse, TK_NOT, pAll, 0, 0);
99973    }
99974    pWhere = sqlite3ExprAnd(db, pWhere, pNe);
99975  }
99976
99977  /* Resolve the references in the WHERE clause. */
99978  memset(&sNameContext, 0, sizeof(NameContext));
99979  sNameContext.pSrcList = pSrc;
99980  sNameContext.pParse = pParse;
99981  sqlite3ResolveExprNames(&sNameContext, pWhere);
99982
99983  /* Create VDBE to loop through the entries in pSrc that match the WHERE
99984  ** clause. For each row found, increment either the deferred or immediate
99985  ** foreign key constraint counter. */
99986  pWInfo = sqlite3WhereBegin(pParse, pSrc, pWhere, 0, 0, 0, 0);
99987  sqlite3VdbeAddOp2(v, OP_FkCounter, pFKey->isDeferred, nIncr);
99988  if( pWInfo ){
99989    sqlite3WhereEnd(pWInfo);
99990  }
99991
99992  /* Clean up the WHERE clause constructed above. */
99993  sqlite3ExprDelete(db, pWhere);
99994  if( iFkIfZero ){
99995    sqlite3VdbeJumpHere(v, iFkIfZero);
99996  }
99997}
99998
99999/*
100000** This function returns a linked list of FKey objects (connected by
100001** FKey.pNextTo) holding all children of table pTab.  For example,
100002** given the following schema:
100003**
100004**   CREATE TABLE t1(a PRIMARY KEY);
100005**   CREATE TABLE t2(b REFERENCES t1(a);
100006**
100007** Calling this function with table "t1" as an argument returns a pointer
100008** to the FKey structure representing the foreign key constraint on table
100009** "t2". Calling this function with "t2" as the argument would return a
100010** NULL pointer (as there are no FK constraints for which t2 is the parent
100011** table).
100012*/
100013SQLITE_PRIVATE FKey *sqlite3FkReferences(Table *pTab){
100014  return (FKey *)sqlite3HashFind(&pTab->pSchema->fkeyHash, pTab->zName);
100015}
100016
100017/*
100018** The second argument is a Trigger structure allocated by the
100019** fkActionTrigger() routine. This function deletes the Trigger structure
100020** and all of its sub-components.
100021**
100022** The Trigger structure or any of its sub-components may be allocated from
100023** the lookaside buffer belonging to database handle dbMem.
100024*/
100025static void fkTriggerDelete(sqlite3 *dbMem, Trigger *p){
100026  if( p ){
100027    TriggerStep *pStep = p->step_list;
100028    sqlite3ExprDelete(dbMem, pStep->pWhere);
100029    sqlite3ExprListDelete(dbMem, pStep->pExprList);
100030    sqlite3SelectDelete(dbMem, pStep->pSelect);
100031    sqlite3ExprDelete(dbMem, p->pWhen);
100032    sqlite3DbFree(dbMem, p);
100033  }
100034}
100035
100036/*
100037** This function is called to generate code that runs when table pTab is
100038** being dropped from the database. The SrcList passed as the second argument
100039** to this function contains a single entry guaranteed to resolve to
100040** table pTab.
100041**
100042** Normally, no code is required. However, if either
100043**
100044**   (a) The table is the parent table of a FK constraint, or
100045**   (b) The table is the child table of a deferred FK constraint and it is
100046**       determined at runtime that there are outstanding deferred FK
100047**       constraint violations in the database,
100048**
100049** then the equivalent of "DELETE FROM <tbl>" is executed before dropping
100050** the table from the database. Triggers are disabled while running this
100051** DELETE, but foreign key actions are not.
100052*/
100053SQLITE_PRIVATE void sqlite3FkDropTable(Parse *pParse, SrcList *pName, Table *pTab){
100054  sqlite3 *db = pParse->db;
100055  if( (db->flags&SQLITE_ForeignKeys) && !IsVirtual(pTab) && !pTab->pSelect ){
100056    int iSkip = 0;
100057    Vdbe *v = sqlite3GetVdbe(pParse);
100058
100059    assert( v );                  /* VDBE has already been allocated */
100060    if( sqlite3FkReferences(pTab)==0 ){
100061      /* Search for a deferred foreign key constraint for which this table
100062      ** is the child table. If one cannot be found, return without
100063      ** generating any VDBE code. If one can be found, then jump over
100064      ** the entire DELETE if there are no outstanding deferred constraints
100065      ** when this statement is run.  */
100066      FKey *p;
100067      for(p=pTab->pFKey; p; p=p->pNextFrom){
100068        if( p->isDeferred || (db->flags & SQLITE_DeferFKs) ) break;
100069      }
100070      if( !p ) return;
100071      iSkip = sqlite3VdbeMakeLabel(v);
100072      sqlite3VdbeAddOp2(v, OP_FkIfZero, 1, iSkip); VdbeCoverage(v);
100073    }
100074
100075    pParse->disableTriggers = 1;
100076    sqlite3DeleteFrom(pParse, sqlite3SrcListDup(db, pName, 0), 0);
100077    pParse->disableTriggers = 0;
100078
100079    /* If the DELETE has generated immediate foreign key constraint
100080    ** violations, halt the VDBE and return an error at this point, before
100081    ** any modifications to the schema are made. This is because statement
100082    ** transactions are not able to rollback schema changes.
100083    **
100084    ** If the SQLITE_DeferFKs flag is set, then this is not required, as
100085    ** the statement transaction will not be rolled back even if FK
100086    ** constraints are violated.
100087    */
100088    if( (db->flags & SQLITE_DeferFKs)==0 ){
100089      sqlite3VdbeAddOp2(v, OP_FkIfZero, 0, sqlite3VdbeCurrentAddr(v)+2);
100090      VdbeCoverage(v);
100091      sqlite3HaltConstraint(pParse, SQLITE_CONSTRAINT_FOREIGNKEY,
100092          OE_Abort, 0, P4_STATIC, P5_ConstraintFK);
100093    }
100094
100095    if( iSkip ){
100096      sqlite3VdbeResolveLabel(v, iSkip);
100097    }
100098  }
100099}
100100
100101
100102/*
100103** The second argument points to an FKey object representing a foreign key
100104** for which pTab is the child table. An UPDATE statement against pTab
100105** is currently being processed. For each column of the table that is
100106** actually updated, the corresponding element in the aChange[] array
100107** is zero or greater (if a column is unmodified the corresponding element
100108** is set to -1). If the rowid column is modified by the UPDATE statement
100109** the bChngRowid argument is non-zero.
100110**
100111** This function returns true if any of the columns that are part of the
100112** child key for FK constraint *p are modified.
100113*/
100114static int fkChildIsModified(
100115  Table *pTab,                    /* Table being updated */
100116  FKey *p,                        /* Foreign key for which pTab is the child */
100117  int *aChange,                   /* Array indicating modified columns */
100118  int bChngRowid                  /* True if rowid is modified by this update */
100119){
100120  int i;
100121  for(i=0; i<p->nCol; i++){
100122    int iChildKey = p->aCol[i].iFrom;
100123    if( aChange[iChildKey]>=0 ) return 1;
100124    if( iChildKey==pTab->iPKey && bChngRowid ) return 1;
100125  }
100126  return 0;
100127}
100128
100129/*
100130** The second argument points to an FKey object representing a foreign key
100131** for which pTab is the parent table. An UPDATE statement against pTab
100132** is currently being processed. For each column of the table that is
100133** actually updated, the corresponding element in the aChange[] array
100134** is zero or greater (if a column is unmodified the corresponding element
100135** is set to -1). If the rowid column is modified by the UPDATE statement
100136** the bChngRowid argument is non-zero.
100137**
100138** This function returns true if any of the columns that are part of the
100139** parent key for FK constraint *p are modified.
100140*/
100141static int fkParentIsModified(
100142  Table *pTab,
100143  FKey *p,
100144  int *aChange,
100145  int bChngRowid
100146){
100147  int i;
100148  for(i=0; i<p->nCol; i++){
100149    char *zKey = p->aCol[i].zCol;
100150    int iKey;
100151    for(iKey=0; iKey<pTab->nCol; iKey++){
100152      if( aChange[iKey]>=0 || (iKey==pTab->iPKey && bChngRowid) ){
100153        Column *pCol = &pTab->aCol[iKey];
100154        if( zKey ){
100155          if( 0==sqlite3StrICmp(pCol->zName, zKey) ) return 1;
100156        }else if( pCol->colFlags & COLFLAG_PRIMKEY ){
100157          return 1;
100158        }
100159      }
100160    }
100161  }
100162  return 0;
100163}
100164
100165/*
100166** Return true if the parser passed as the first argument is being
100167** used to code a trigger that is really a "SET NULL" action belonging
100168** to trigger pFKey.
100169*/
100170static int isSetNullAction(Parse *pParse, FKey *pFKey){
100171  Parse *pTop = sqlite3ParseToplevel(pParse);
100172  if( pTop->pTriggerPrg ){
100173    Trigger *p = pTop->pTriggerPrg->pTrigger;
100174    if( (p==pFKey->apTrigger[0] && pFKey->aAction[0]==OE_SetNull)
100175     || (p==pFKey->apTrigger[1] && pFKey->aAction[1]==OE_SetNull)
100176    ){
100177      return 1;
100178    }
100179  }
100180  return 0;
100181}
100182
100183/*
100184** This function is called when inserting, deleting or updating a row of
100185** table pTab to generate VDBE code to perform foreign key constraint
100186** processing for the operation.
100187**
100188** For a DELETE operation, parameter regOld is passed the index of the
100189** first register in an array of (pTab->nCol+1) registers containing the
100190** rowid of the row being deleted, followed by each of the column values
100191** of the row being deleted, from left to right. Parameter regNew is passed
100192** zero in this case.
100193**
100194** For an INSERT operation, regOld is passed zero and regNew is passed the
100195** first register of an array of (pTab->nCol+1) registers containing the new
100196** row data.
100197**
100198** For an UPDATE operation, this function is called twice. Once before
100199** the original record is deleted from the table using the calling convention
100200** described for DELETE. Then again after the original record is deleted
100201** but before the new record is inserted using the INSERT convention.
100202*/
100203SQLITE_PRIVATE void sqlite3FkCheck(
100204  Parse *pParse,                  /* Parse context */
100205  Table *pTab,                    /* Row is being deleted from this table */
100206  int regOld,                     /* Previous row data is stored here */
100207  int regNew,                     /* New row data is stored here */
100208  int *aChange,                   /* Array indicating UPDATEd columns (or 0) */
100209  int bChngRowid                  /* True if rowid is UPDATEd */
100210){
100211  sqlite3 *db = pParse->db;       /* Database handle */
100212  FKey *pFKey;                    /* Used to iterate through FKs */
100213  int iDb;                        /* Index of database containing pTab */
100214  const char *zDb;                /* Name of database containing pTab */
100215  int isIgnoreErrors = pParse->disableTriggers;
100216
100217  /* Exactly one of regOld and regNew should be non-zero. */
100218  assert( (regOld==0)!=(regNew==0) );
100219
100220  /* If foreign-keys are disabled, this function is a no-op. */
100221  if( (db->flags&SQLITE_ForeignKeys)==0 ) return;
100222
100223  iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
100224  zDb = db->aDb[iDb].zName;
100225
100226  /* Loop through all the foreign key constraints for which pTab is the
100227  ** child table (the table that the foreign key definition is part of).  */
100228  for(pFKey=pTab->pFKey; pFKey; pFKey=pFKey->pNextFrom){
100229    Table *pTo;                   /* Parent table of foreign key pFKey */
100230    Index *pIdx = 0;              /* Index on key columns in pTo */
100231    int *aiFree = 0;
100232    int *aiCol;
100233    int iCol;
100234    int i;
100235    int bIgnore = 0;
100236
100237    if( aChange
100238     && sqlite3_stricmp(pTab->zName, pFKey->zTo)!=0
100239     && fkChildIsModified(pTab, pFKey, aChange, bChngRowid)==0
100240    ){
100241      continue;
100242    }
100243
100244    /* Find the parent table of this foreign key. Also find a unique index
100245    ** on the parent key columns in the parent table. If either of these
100246    ** schema items cannot be located, set an error in pParse and return
100247    ** early.  */
100248    if( pParse->disableTriggers ){
100249      pTo = sqlite3FindTable(db, pFKey->zTo, zDb);
100250    }else{
100251      pTo = sqlite3LocateTable(pParse, 0, pFKey->zTo, zDb);
100252    }
100253    if( !pTo || sqlite3FkLocateIndex(pParse, pTo, pFKey, &pIdx, &aiFree) ){
100254      assert( isIgnoreErrors==0 || (regOld!=0 && regNew==0) );
100255      if( !isIgnoreErrors || db->mallocFailed ) return;
100256      if( pTo==0 ){
100257        /* If isIgnoreErrors is true, then a table is being dropped. In this
100258        ** case SQLite runs a "DELETE FROM xxx" on the table being dropped
100259        ** before actually dropping it in order to check FK constraints.
100260        ** If the parent table of an FK constraint on the current table is
100261        ** missing, behave as if it is empty. i.e. decrement the relevant
100262        ** FK counter for each row of the current table with non-NULL keys.
100263        */
100264        Vdbe *v = sqlite3GetVdbe(pParse);
100265        int iJump = sqlite3VdbeCurrentAddr(v) + pFKey->nCol + 1;
100266        for(i=0; i<pFKey->nCol; i++){
100267          int iReg = pFKey->aCol[i].iFrom + regOld + 1;
100268          sqlite3VdbeAddOp2(v, OP_IsNull, iReg, iJump); VdbeCoverage(v);
100269        }
100270        sqlite3VdbeAddOp2(v, OP_FkCounter, pFKey->isDeferred, -1);
100271      }
100272      continue;
100273    }
100274    assert( pFKey->nCol==1 || (aiFree && pIdx) );
100275
100276    if( aiFree ){
100277      aiCol = aiFree;
100278    }else{
100279      iCol = pFKey->aCol[0].iFrom;
100280      aiCol = &iCol;
100281    }
100282    for(i=0; i<pFKey->nCol; i++){
100283      if( aiCol[i]==pTab->iPKey ){
100284        aiCol[i] = -1;
100285      }
100286#ifndef SQLITE_OMIT_AUTHORIZATION
100287      /* Request permission to read the parent key columns. If the
100288      ** authorization callback returns SQLITE_IGNORE, behave as if any
100289      ** values read from the parent table are NULL. */
100290      if( db->xAuth ){
100291        int rcauth;
100292        char *zCol = pTo->aCol[pIdx ? pIdx->aiColumn[i] : pTo->iPKey].zName;
100293        rcauth = sqlite3AuthReadCol(pParse, pTo->zName, zCol, iDb);
100294        bIgnore = (rcauth==SQLITE_IGNORE);
100295      }
100296#endif
100297    }
100298
100299    /* Take a shared-cache advisory read-lock on the parent table. Allocate
100300    ** a cursor to use to search the unique index on the parent key columns
100301    ** in the parent table.  */
100302    sqlite3TableLock(pParse, iDb, pTo->tnum, 0, pTo->zName);
100303    pParse->nTab++;
100304
100305    if( regOld!=0 ){
100306      /* A row is being removed from the child table. Search for the parent.
100307      ** If the parent does not exist, removing the child row resolves an
100308      ** outstanding foreign key constraint violation. */
100309      fkLookupParent(pParse, iDb, pTo, pIdx, pFKey, aiCol, regOld, -1, bIgnore);
100310    }
100311    if( regNew!=0 && !isSetNullAction(pParse, pFKey) ){
100312      /* A row is being added to the child table. If a parent row cannot
100313      ** be found, adding the child row has violated the FK constraint.
100314      **
100315      ** If this operation is being performed as part of a trigger program
100316      ** that is actually a "SET NULL" action belonging to this very
100317      ** foreign key, then omit this scan altogether. As all child key
100318      ** values are guaranteed to be NULL, it is not possible for adding
100319      ** this row to cause an FK violation.  */
100320      fkLookupParent(pParse, iDb, pTo, pIdx, pFKey, aiCol, regNew, +1, bIgnore);
100321    }
100322
100323    sqlite3DbFree(db, aiFree);
100324  }
100325
100326  /* Loop through all the foreign key constraints that refer to this table.
100327  ** (the "child" constraints) */
100328  for(pFKey = sqlite3FkReferences(pTab); pFKey; pFKey=pFKey->pNextTo){
100329    Index *pIdx = 0;              /* Foreign key index for pFKey */
100330    SrcList *pSrc;
100331    int *aiCol = 0;
100332
100333    if( aChange && fkParentIsModified(pTab, pFKey, aChange, bChngRowid)==0 ){
100334      continue;
100335    }
100336
100337    if( !pFKey->isDeferred && !(db->flags & SQLITE_DeferFKs)
100338     && !pParse->pToplevel && !pParse->isMultiWrite
100339    ){
100340      assert( regOld==0 && regNew!=0 );
100341      /* Inserting a single row into a parent table cannot cause (or fix)
100342      ** an immediate foreign key violation. So do nothing in this case.  */
100343      continue;
100344    }
100345
100346    if( sqlite3FkLocateIndex(pParse, pTab, pFKey, &pIdx, &aiCol) ){
100347      if( !isIgnoreErrors || db->mallocFailed ) return;
100348      continue;
100349    }
100350    assert( aiCol || pFKey->nCol==1 );
100351
100352    /* Create a SrcList structure containing the child table.  We need the
100353    ** child table as a SrcList for sqlite3WhereBegin() */
100354    pSrc = sqlite3SrcListAppend(db, 0, 0, 0);
100355    if( pSrc ){
100356      struct SrcList_item *pItem = pSrc->a;
100357      pItem->pTab = pFKey->pFrom;
100358      pItem->zName = pFKey->pFrom->zName;
100359      pItem->pTab->nRef++;
100360      pItem->iCursor = pParse->nTab++;
100361
100362      if( regNew!=0 ){
100363        fkScanChildren(pParse, pSrc, pTab, pIdx, pFKey, aiCol, regNew, -1);
100364      }
100365      if( regOld!=0 ){
100366        int eAction = pFKey->aAction[aChange!=0];
100367        fkScanChildren(pParse, pSrc, pTab, pIdx, pFKey, aiCol, regOld, 1);
100368        /* If this is a deferred FK constraint, or a CASCADE or SET NULL
100369        ** action applies, then any foreign key violations caused by
100370        ** removing the parent key will be rectified by the action trigger.
100371        ** So do not set the "may-abort" flag in this case.
100372        **
100373        ** Note 1: If the FK is declared "ON UPDATE CASCADE", then the
100374        ** may-abort flag will eventually be set on this statement anyway
100375        ** (when this function is called as part of processing the UPDATE
100376        ** within the action trigger).
100377        **
100378        ** Note 2: At first glance it may seem like SQLite could simply omit
100379        ** all OP_FkCounter related scans when either CASCADE or SET NULL
100380        ** applies. The trouble starts if the CASCADE or SET NULL action
100381        ** trigger causes other triggers or action rules attached to the
100382        ** child table to fire. In these cases the fk constraint counters
100383        ** might be set incorrectly if any OP_FkCounter related scans are
100384        ** omitted.  */
100385        if( !pFKey->isDeferred && eAction!=OE_Cascade && eAction!=OE_SetNull ){
100386          sqlite3MayAbort(pParse);
100387        }
100388      }
100389      pItem->zName = 0;
100390      sqlite3SrcListDelete(db, pSrc);
100391    }
100392    sqlite3DbFree(db, aiCol);
100393  }
100394}
100395
100396#define COLUMN_MASK(x) (((x)>31) ? 0xffffffff : ((u32)1<<(x)))
100397
100398/*
100399** This function is called before generating code to update or delete a
100400** row contained in table pTab.
100401*/
100402SQLITE_PRIVATE u32 sqlite3FkOldmask(
100403  Parse *pParse,                  /* Parse context */
100404  Table *pTab                     /* Table being modified */
100405){
100406  u32 mask = 0;
100407  if( pParse->db->flags&SQLITE_ForeignKeys ){
100408    FKey *p;
100409    int i;
100410    for(p=pTab->pFKey; p; p=p->pNextFrom){
100411      for(i=0; i<p->nCol; i++) mask |= COLUMN_MASK(p->aCol[i].iFrom);
100412    }
100413    for(p=sqlite3FkReferences(pTab); p; p=p->pNextTo){
100414      Index *pIdx = 0;
100415      sqlite3FkLocateIndex(pParse, pTab, p, &pIdx, 0);
100416      if( pIdx ){
100417        for(i=0; i<pIdx->nKeyCol; i++) mask |= COLUMN_MASK(pIdx->aiColumn[i]);
100418      }
100419    }
100420  }
100421  return mask;
100422}
100423
100424
100425/*
100426** This function is called before generating code to update or delete a
100427** row contained in table pTab. If the operation is a DELETE, then
100428** parameter aChange is passed a NULL value. For an UPDATE, aChange points
100429** to an array of size N, where N is the number of columns in table pTab.
100430** If the i'th column is not modified by the UPDATE, then the corresponding
100431** entry in the aChange[] array is set to -1. If the column is modified,
100432** the value is 0 or greater. Parameter chngRowid is set to true if the
100433** UPDATE statement modifies the rowid fields of the table.
100434**
100435** If any foreign key processing will be required, this function returns
100436** true. If there is no foreign key related processing, this function
100437** returns false.
100438*/
100439SQLITE_PRIVATE int sqlite3FkRequired(
100440  Parse *pParse,                  /* Parse context */
100441  Table *pTab,                    /* Table being modified */
100442  int *aChange,                   /* Non-NULL for UPDATE operations */
100443  int chngRowid                   /* True for UPDATE that affects rowid */
100444){
100445  if( pParse->db->flags&SQLITE_ForeignKeys ){
100446    if( !aChange ){
100447      /* A DELETE operation. Foreign key processing is required if the
100448      ** table in question is either the child or parent table for any
100449      ** foreign key constraint.  */
100450      return (sqlite3FkReferences(pTab) || pTab->pFKey);
100451    }else{
100452      /* This is an UPDATE. Foreign key processing is only required if the
100453      ** operation modifies one or more child or parent key columns. */
100454      FKey *p;
100455
100456      /* Check if any child key columns are being modified. */
100457      for(p=pTab->pFKey; p; p=p->pNextFrom){
100458        if( fkChildIsModified(pTab, p, aChange, chngRowid) ) return 1;
100459      }
100460
100461      /* Check if any parent key columns are being modified. */
100462      for(p=sqlite3FkReferences(pTab); p; p=p->pNextTo){
100463        if( fkParentIsModified(pTab, p, aChange, chngRowid) ) return 1;
100464      }
100465    }
100466  }
100467  return 0;
100468}
100469
100470/*
100471** This function is called when an UPDATE or DELETE operation is being
100472** compiled on table pTab, which is the parent table of foreign-key pFKey.
100473** If the current operation is an UPDATE, then the pChanges parameter is
100474** passed a pointer to the list of columns being modified. If it is a
100475** DELETE, pChanges is passed a NULL pointer.
100476**
100477** It returns a pointer to a Trigger structure containing a trigger
100478** equivalent to the ON UPDATE or ON DELETE action specified by pFKey.
100479** If the action is "NO ACTION" or "RESTRICT", then a NULL pointer is
100480** returned (these actions require no special handling by the triggers
100481** sub-system, code for them is created by fkScanChildren()).
100482**
100483** For example, if pFKey is the foreign key and pTab is table "p" in
100484** the following schema:
100485**
100486**   CREATE TABLE p(pk PRIMARY KEY);
100487**   CREATE TABLE c(ck REFERENCES p ON DELETE CASCADE);
100488**
100489** then the returned trigger structure is equivalent to:
100490**
100491**   CREATE TRIGGER ... DELETE ON p BEGIN
100492**     DELETE FROM c WHERE ck = old.pk;
100493**   END;
100494**
100495** The returned pointer is cached as part of the foreign key object. It
100496** is eventually freed along with the rest of the foreign key object by
100497** sqlite3FkDelete().
100498*/
100499static Trigger *fkActionTrigger(
100500  Parse *pParse,                  /* Parse context */
100501  Table *pTab,                    /* Table being updated or deleted from */
100502  FKey *pFKey,                    /* Foreign key to get action for */
100503  ExprList *pChanges              /* Change-list for UPDATE, NULL for DELETE */
100504){
100505  sqlite3 *db = pParse->db;       /* Database handle */
100506  int action;                     /* One of OE_None, OE_Cascade etc. */
100507  Trigger *pTrigger;              /* Trigger definition to return */
100508  int iAction = (pChanges!=0);    /* 1 for UPDATE, 0 for DELETE */
100509
100510  action = pFKey->aAction[iAction];
100511  pTrigger = pFKey->apTrigger[iAction];
100512
100513  if( action!=OE_None && !pTrigger ){
100514    u8 enableLookaside;           /* Copy of db->lookaside.bEnabled */
100515    char const *zFrom;            /* Name of child table */
100516    int nFrom;                    /* Length in bytes of zFrom */
100517    Index *pIdx = 0;              /* Parent key index for this FK */
100518    int *aiCol = 0;               /* child table cols -> parent key cols */
100519    TriggerStep *pStep = 0;        /* First (only) step of trigger program */
100520    Expr *pWhere = 0;             /* WHERE clause of trigger step */
100521    ExprList *pList = 0;          /* Changes list if ON UPDATE CASCADE */
100522    Select *pSelect = 0;          /* If RESTRICT, "SELECT RAISE(...)" */
100523    int i;                        /* Iterator variable */
100524    Expr *pWhen = 0;              /* WHEN clause for the trigger */
100525
100526    if( sqlite3FkLocateIndex(pParse, pTab, pFKey, &pIdx, &aiCol) ) return 0;
100527    assert( aiCol || pFKey->nCol==1 );
100528
100529    for(i=0; i<pFKey->nCol; i++){
100530      Token tOld = { "old", 3 };  /* Literal "old" token */
100531      Token tNew = { "new", 3 };  /* Literal "new" token */
100532      Token tFromCol;             /* Name of column in child table */
100533      Token tToCol;               /* Name of column in parent table */
100534      int iFromCol;               /* Idx of column in child table */
100535      Expr *pEq;                  /* tFromCol = OLD.tToCol */
100536
100537      iFromCol = aiCol ? aiCol[i] : pFKey->aCol[0].iFrom;
100538      assert( iFromCol>=0 );
100539      assert( pIdx!=0 || (pTab->iPKey>=0 && pTab->iPKey<pTab->nCol) );
100540      tToCol.z = pTab->aCol[pIdx ? pIdx->aiColumn[i] : pTab->iPKey].zName;
100541      tFromCol.z = pFKey->pFrom->aCol[iFromCol].zName;
100542
100543      tToCol.n = sqlite3Strlen30(tToCol.z);
100544      tFromCol.n = sqlite3Strlen30(tFromCol.z);
100545
100546      /* Create the expression "OLD.zToCol = zFromCol". It is important
100547      ** that the "OLD.zToCol" term is on the LHS of the = operator, so
100548      ** that the affinity and collation sequence associated with the
100549      ** parent table are used for the comparison. */
100550      pEq = sqlite3PExpr(pParse, TK_EQ,
100551          sqlite3PExpr(pParse, TK_DOT,
100552            sqlite3ExprAlloc(db, TK_ID, &tOld, 0),
100553            sqlite3ExprAlloc(db, TK_ID, &tToCol, 0)
100554          , 0),
100555          sqlite3ExprAlloc(db, TK_ID, &tFromCol, 0)
100556      , 0);
100557      pWhere = sqlite3ExprAnd(db, pWhere, pEq);
100558
100559      /* For ON UPDATE, construct the next term of the WHEN clause.
100560      ** The final WHEN clause will be like this:
100561      **
100562      **    WHEN NOT(old.col1 IS new.col1 AND ... AND old.colN IS new.colN)
100563      */
100564      if( pChanges ){
100565        pEq = sqlite3PExpr(pParse, TK_IS,
100566            sqlite3PExpr(pParse, TK_DOT,
100567              sqlite3ExprAlloc(db, TK_ID, &tOld, 0),
100568              sqlite3ExprAlloc(db, TK_ID, &tToCol, 0),
100569              0),
100570            sqlite3PExpr(pParse, TK_DOT,
100571              sqlite3ExprAlloc(db, TK_ID, &tNew, 0),
100572              sqlite3ExprAlloc(db, TK_ID, &tToCol, 0),
100573              0),
100574            0);
100575        pWhen = sqlite3ExprAnd(db, pWhen, pEq);
100576      }
100577
100578      if( action!=OE_Restrict && (action!=OE_Cascade || pChanges) ){
100579        Expr *pNew;
100580        if( action==OE_Cascade ){
100581          pNew = sqlite3PExpr(pParse, TK_DOT,
100582            sqlite3ExprAlloc(db, TK_ID, &tNew, 0),
100583            sqlite3ExprAlloc(db, TK_ID, &tToCol, 0)
100584          , 0);
100585        }else if( action==OE_SetDflt ){
100586          Expr *pDflt = pFKey->pFrom->aCol[iFromCol].pDflt;
100587          if( pDflt ){
100588            pNew = sqlite3ExprDup(db, pDflt, 0);
100589          }else{
100590            pNew = sqlite3PExpr(pParse, TK_NULL, 0, 0, 0);
100591          }
100592        }else{
100593          pNew = sqlite3PExpr(pParse, TK_NULL, 0, 0, 0);
100594        }
100595        pList = sqlite3ExprListAppend(pParse, pList, pNew);
100596        sqlite3ExprListSetName(pParse, pList, &tFromCol, 0);
100597      }
100598    }
100599    sqlite3DbFree(db, aiCol);
100600
100601    zFrom = pFKey->pFrom->zName;
100602    nFrom = sqlite3Strlen30(zFrom);
100603
100604    if( action==OE_Restrict ){
100605      Token tFrom;
100606      Expr *pRaise;
100607
100608      tFrom.z = zFrom;
100609      tFrom.n = nFrom;
100610      pRaise = sqlite3Expr(db, TK_RAISE, "FOREIGN KEY constraint failed");
100611      if( pRaise ){
100612        pRaise->affinity = OE_Abort;
100613      }
100614      pSelect = sqlite3SelectNew(pParse,
100615          sqlite3ExprListAppend(pParse, 0, pRaise),
100616          sqlite3SrcListAppend(db, 0, &tFrom, 0),
100617          pWhere,
100618          0, 0, 0, 0, 0, 0
100619      );
100620      pWhere = 0;
100621    }
100622
100623    /* Disable lookaside memory allocation */
100624    enableLookaside = db->lookaside.bEnabled;
100625    db->lookaside.bEnabled = 0;
100626
100627    pTrigger = (Trigger *)sqlite3DbMallocZero(db,
100628        sizeof(Trigger) +         /* struct Trigger */
100629        sizeof(TriggerStep) +     /* Single step in trigger program */
100630        nFrom + 1                 /* Space for pStep->zTarget */
100631    );
100632    if( pTrigger ){
100633      pStep = pTrigger->step_list = (TriggerStep *)&pTrigger[1];
100634      pStep->zTarget = (char *)&pStep[1];
100635      memcpy((char *)pStep->zTarget, zFrom, nFrom);
100636
100637      pStep->pWhere = sqlite3ExprDup(db, pWhere, EXPRDUP_REDUCE);
100638      pStep->pExprList = sqlite3ExprListDup(db, pList, EXPRDUP_REDUCE);
100639      pStep->pSelect = sqlite3SelectDup(db, pSelect, EXPRDUP_REDUCE);
100640      if( pWhen ){
100641        pWhen = sqlite3PExpr(pParse, TK_NOT, pWhen, 0, 0);
100642        pTrigger->pWhen = sqlite3ExprDup(db, pWhen, EXPRDUP_REDUCE);
100643      }
100644    }
100645
100646    /* Re-enable the lookaside buffer, if it was disabled earlier. */
100647    db->lookaside.bEnabled = enableLookaside;
100648
100649    sqlite3ExprDelete(db, pWhere);
100650    sqlite3ExprDelete(db, pWhen);
100651    sqlite3ExprListDelete(db, pList);
100652    sqlite3SelectDelete(db, pSelect);
100653    if( db->mallocFailed==1 ){
100654      fkTriggerDelete(db, pTrigger);
100655      return 0;
100656    }
100657    assert( pStep!=0 );
100658
100659    switch( action ){
100660      case OE_Restrict:
100661        pStep->op = TK_SELECT;
100662        break;
100663      case OE_Cascade:
100664        if( !pChanges ){
100665          pStep->op = TK_DELETE;
100666          break;
100667        }
100668      default:
100669        pStep->op = TK_UPDATE;
100670    }
100671    pStep->pTrig = pTrigger;
100672    pTrigger->pSchema = pTab->pSchema;
100673    pTrigger->pTabSchema = pTab->pSchema;
100674    pFKey->apTrigger[iAction] = pTrigger;
100675    pTrigger->op = (pChanges ? TK_UPDATE : TK_DELETE);
100676  }
100677
100678  return pTrigger;
100679}
100680
100681/*
100682** This function is called when deleting or updating a row to implement
100683** any required CASCADE, SET NULL or SET DEFAULT actions.
100684*/
100685SQLITE_PRIVATE void sqlite3FkActions(
100686  Parse *pParse,                  /* Parse context */
100687  Table *pTab,                    /* Table being updated or deleted from */
100688  ExprList *pChanges,             /* Change-list for UPDATE, NULL for DELETE */
100689  int regOld,                     /* Address of array containing old row */
100690  int *aChange,                   /* Array indicating UPDATEd columns (or 0) */
100691  int bChngRowid                  /* True if rowid is UPDATEd */
100692){
100693  /* If foreign-key support is enabled, iterate through all FKs that
100694  ** refer to table pTab. If there is an action associated with the FK
100695  ** for this operation (either update or delete), invoke the associated
100696  ** trigger sub-program.  */
100697  if( pParse->db->flags&SQLITE_ForeignKeys ){
100698    FKey *pFKey;                  /* Iterator variable */
100699    for(pFKey = sqlite3FkReferences(pTab); pFKey; pFKey=pFKey->pNextTo){
100700      if( aChange==0 || fkParentIsModified(pTab, pFKey, aChange, bChngRowid) ){
100701        Trigger *pAct = fkActionTrigger(pParse, pTab, pFKey, pChanges);
100702        if( pAct ){
100703          sqlite3CodeRowTriggerDirect(pParse, pAct, pTab, regOld, OE_Abort, 0);
100704        }
100705      }
100706    }
100707  }
100708}
100709
100710#endif /* ifndef SQLITE_OMIT_TRIGGER */
100711
100712/*
100713** Free all memory associated with foreign key definitions attached to
100714** table pTab. Remove the deleted foreign keys from the Schema.fkeyHash
100715** hash table.
100716*/
100717SQLITE_PRIVATE void sqlite3FkDelete(sqlite3 *db, Table *pTab){
100718  FKey *pFKey;                    /* Iterator variable */
100719  FKey *pNext;                    /* Copy of pFKey->pNextFrom */
100720
100721  assert( db==0 || sqlite3SchemaMutexHeld(db, 0, pTab->pSchema) );
100722  for(pFKey=pTab->pFKey; pFKey; pFKey=pNext){
100723
100724    /* Remove the FK from the fkeyHash hash table. */
100725    if( !db || db->pnBytesFreed==0 ){
100726      if( pFKey->pPrevTo ){
100727        pFKey->pPrevTo->pNextTo = pFKey->pNextTo;
100728      }else{
100729        void *p = (void *)pFKey->pNextTo;
100730        const char *z = (p ? pFKey->pNextTo->zTo : pFKey->zTo);
100731        sqlite3HashInsert(&pTab->pSchema->fkeyHash, z, p);
100732      }
100733      if( pFKey->pNextTo ){
100734        pFKey->pNextTo->pPrevTo = pFKey->pPrevTo;
100735      }
100736    }
100737
100738    /* EV: R-30323-21917 Each foreign key constraint in SQLite is
100739    ** classified as either immediate or deferred.
100740    */
100741    assert( pFKey->isDeferred==0 || pFKey->isDeferred==1 );
100742
100743    /* Delete any triggers created to implement actions for this FK. */
100744#ifndef SQLITE_OMIT_TRIGGER
100745    fkTriggerDelete(db, pFKey->apTrigger[0]);
100746    fkTriggerDelete(db, pFKey->apTrigger[1]);
100747#endif
100748
100749    pNext = pFKey->pNextFrom;
100750    sqlite3DbFree(db, pFKey);
100751  }
100752}
100753#endif /* ifndef SQLITE_OMIT_FOREIGN_KEY */
100754
100755/************** End of fkey.c ************************************************/
100756/************** Begin file insert.c ******************************************/
100757/*
100758** 2001 September 15
100759**
100760** The author disclaims copyright to this source code.  In place of
100761** a legal notice, here is a blessing:
100762**
100763**    May you do good and not evil.
100764**    May you find forgiveness for yourself and forgive others.
100765**    May you share freely, never taking more than you give.
100766**
100767*************************************************************************
100768** This file contains C code routines that are called by the parser
100769** to handle INSERT statements in SQLite.
100770*/
100771/* #include "sqliteInt.h" */
100772
100773/*
100774** Generate code that will
100775**
100776**   (1) acquire a lock for table pTab then
100777**   (2) open pTab as cursor iCur.
100778**
100779** If pTab is a WITHOUT ROWID table, then it is the PRIMARY KEY index
100780** for that table that is actually opened.
100781*/
100782SQLITE_PRIVATE void sqlite3OpenTable(
100783  Parse *pParse,  /* Generate code into this VDBE */
100784  int iCur,       /* The cursor number of the table */
100785  int iDb,        /* The database index in sqlite3.aDb[] */
100786  Table *pTab,    /* The table to be opened */
100787  int opcode      /* OP_OpenRead or OP_OpenWrite */
100788){
100789  Vdbe *v;
100790  assert( !IsVirtual(pTab) );
100791  v = sqlite3GetVdbe(pParse);
100792  assert( opcode==OP_OpenWrite || opcode==OP_OpenRead );
100793  sqlite3TableLock(pParse, iDb, pTab->tnum,
100794                   (opcode==OP_OpenWrite)?1:0, pTab->zName);
100795  if( HasRowid(pTab) ){
100796    sqlite3VdbeAddOp4Int(v, opcode, iCur, pTab->tnum, iDb, pTab->nCol);
100797    VdbeComment((v, "%s", pTab->zName));
100798  }else{
100799    Index *pPk = sqlite3PrimaryKeyIndex(pTab);
100800    assert( pPk!=0 );
100801    assert( pPk->tnum==pTab->tnum );
100802    sqlite3VdbeAddOp3(v, opcode, iCur, pPk->tnum, iDb);
100803    sqlite3VdbeSetP4KeyInfo(pParse, pPk);
100804    VdbeComment((v, "%s", pTab->zName));
100805  }
100806}
100807
100808/*
100809** Return a pointer to the column affinity string associated with index
100810** pIdx. A column affinity string has one character for each column in
100811** the table, according to the affinity of the column:
100812**
100813**  Character      Column affinity
100814**  ------------------------------
100815**  'A'            BLOB
100816**  'B'            TEXT
100817**  'C'            NUMERIC
100818**  'D'            INTEGER
100819**  'F'            REAL
100820**
100821** An extra 'D' is appended to the end of the string to cover the
100822** rowid that appears as the last column in every index.
100823**
100824** Memory for the buffer containing the column index affinity string
100825** is managed along with the rest of the Index structure. It will be
100826** released when sqlite3DeleteIndex() is called.
100827*/
100828SQLITE_PRIVATE const char *sqlite3IndexAffinityStr(Vdbe *v, Index *pIdx){
100829  if( !pIdx->zColAff ){
100830    /* The first time a column affinity string for a particular index is
100831    ** required, it is allocated and populated here. It is then stored as
100832    ** a member of the Index structure for subsequent use.
100833    **
100834    ** The column affinity string will eventually be deleted by
100835    ** sqliteDeleteIndex() when the Index structure itself is cleaned
100836    ** up.
100837    */
100838    int n;
100839    Table *pTab = pIdx->pTable;
100840    sqlite3 *db = sqlite3VdbeDb(v);
100841    pIdx->zColAff = (char *)sqlite3DbMallocRaw(0, pIdx->nColumn+1);
100842    if( !pIdx->zColAff ){
100843      db->mallocFailed = 1;
100844      return 0;
100845    }
100846    for(n=0; n<pIdx->nColumn; n++){
100847      i16 x = pIdx->aiColumn[n];
100848      pIdx->zColAff[n] = x<0 ? SQLITE_AFF_INTEGER : pTab->aCol[x].affinity;
100849    }
100850    pIdx->zColAff[n] = 0;
100851  }
100852
100853  return pIdx->zColAff;
100854}
100855
100856/*
100857** Compute the affinity string for table pTab, if it has not already been
100858** computed.  As an optimization, omit trailing SQLITE_AFF_BLOB affinities.
100859**
100860** If the affinity exists (if it is no entirely SQLITE_AFF_BLOB values) and
100861** if iReg>0 then code an OP_Affinity opcode that will set the affinities
100862** for register iReg and following.  Or if affinities exists and iReg==0,
100863** then just set the P4 operand of the previous opcode (which should  be
100864** an OP_MakeRecord) to the affinity string.
100865**
100866** A column affinity string has one character per column:
100867**
100868**  Character      Column affinity
100869**  ------------------------------
100870**  'A'            BLOB
100871**  'B'            TEXT
100872**  'C'            NUMERIC
100873**  'D'            INTEGER
100874**  'E'            REAL
100875*/
100876SQLITE_PRIVATE void sqlite3TableAffinity(Vdbe *v, Table *pTab, int iReg){
100877  int i;
100878  char *zColAff = pTab->zColAff;
100879  if( zColAff==0 ){
100880    sqlite3 *db = sqlite3VdbeDb(v);
100881    zColAff = (char *)sqlite3DbMallocRaw(0, pTab->nCol+1);
100882    if( !zColAff ){
100883      db->mallocFailed = 1;
100884      return;
100885    }
100886
100887    for(i=0; i<pTab->nCol; i++){
100888      zColAff[i] = pTab->aCol[i].affinity;
100889    }
100890    do{
100891      zColAff[i--] = 0;
100892    }while( i>=0 && zColAff[i]==SQLITE_AFF_BLOB );
100893    pTab->zColAff = zColAff;
100894  }
100895  i = sqlite3Strlen30(zColAff);
100896  if( i ){
100897    if( iReg ){
100898      sqlite3VdbeAddOp4(v, OP_Affinity, iReg, i, 0, zColAff, i);
100899    }else{
100900      sqlite3VdbeChangeP4(v, -1, zColAff, i);
100901    }
100902  }
100903}
100904
100905/*
100906** Return non-zero if the table pTab in database iDb or any of its indices
100907** have been opened at any point in the VDBE program. This is used to see if
100908** a statement of the form  "INSERT INTO <iDb, pTab> SELECT ..." can
100909** run without using a temporary table for the results of the SELECT.
100910*/
100911static int readsTable(Parse *p, int iDb, Table *pTab){
100912  Vdbe *v = sqlite3GetVdbe(p);
100913  int i;
100914  int iEnd = sqlite3VdbeCurrentAddr(v);
100915#ifndef SQLITE_OMIT_VIRTUALTABLE
100916  VTable *pVTab = IsVirtual(pTab) ? sqlite3GetVTable(p->db, pTab) : 0;
100917#endif
100918
100919  for(i=1; i<iEnd; i++){
100920    VdbeOp *pOp = sqlite3VdbeGetOp(v, i);
100921    assert( pOp!=0 );
100922    if( pOp->opcode==OP_OpenRead && pOp->p3==iDb ){
100923      Index *pIndex;
100924      int tnum = pOp->p2;
100925      if( tnum==pTab->tnum ){
100926        return 1;
100927      }
100928      for(pIndex=pTab->pIndex; pIndex; pIndex=pIndex->pNext){
100929        if( tnum==pIndex->tnum ){
100930          return 1;
100931        }
100932      }
100933    }
100934#ifndef SQLITE_OMIT_VIRTUALTABLE
100935    if( pOp->opcode==OP_VOpen && pOp->p4.pVtab==pVTab ){
100936      assert( pOp->p4.pVtab!=0 );
100937      assert( pOp->p4type==P4_VTAB );
100938      return 1;
100939    }
100940#endif
100941  }
100942  return 0;
100943}
100944
100945#ifndef SQLITE_OMIT_AUTOINCREMENT
100946/*
100947** Locate or create an AutoincInfo structure associated with table pTab
100948** which is in database iDb.  Return the register number for the register
100949** that holds the maximum rowid.
100950**
100951** There is at most one AutoincInfo structure per table even if the
100952** same table is autoincremented multiple times due to inserts within
100953** triggers.  A new AutoincInfo structure is created if this is the
100954** first use of table pTab.  On 2nd and subsequent uses, the original
100955** AutoincInfo structure is used.
100956**
100957** Three memory locations are allocated:
100958**
100959**   (1)  Register to hold the name of the pTab table.
100960**   (2)  Register to hold the maximum ROWID of pTab.
100961**   (3)  Register to hold the rowid in sqlite_sequence of pTab
100962**
100963** The 2nd register is the one that is returned.  That is all the
100964** insert routine needs to know about.
100965*/
100966static int autoIncBegin(
100967  Parse *pParse,      /* Parsing context */
100968  int iDb,            /* Index of the database holding pTab */
100969  Table *pTab         /* The table we are writing to */
100970){
100971  int memId = 0;      /* Register holding maximum rowid */
100972  if( pTab->tabFlags & TF_Autoincrement ){
100973    Parse *pToplevel = sqlite3ParseToplevel(pParse);
100974    AutoincInfo *pInfo;
100975
100976    pInfo = pToplevel->pAinc;
100977    while( pInfo && pInfo->pTab!=pTab ){ pInfo = pInfo->pNext; }
100978    if( pInfo==0 ){
100979      pInfo = sqlite3DbMallocRaw(pParse->db, sizeof(*pInfo));
100980      if( pInfo==0 ) return 0;
100981      pInfo->pNext = pToplevel->pAinc;
100982      pToplevel->pAinc = pInfo;
100983      pInfo->pTab = pTab;
100984      pInfo->iDb = iDb;
100985      pToplevel->nMem++;                  /* Register to hold name of table */
100986      pInfo->regCtr = ++pToplevel->nMem;  /* Max rowid register */
100987      pToplevel->nMem++;                  /* Rowid in sqlite_sequence */
100988    }
100989    memId = pInfo->regCtr;
100990  }
100991  return memId;
100992}
100993
100994/*
100995** This routine generates code that will initialize all of the
100996** register used by the autoincrement tracker.
100997*/
100998SQLITE_PRIVATE void sqlite3AutoincrementBegin(Parse *pParse){
100999  AutoincInfo *p;            /* Information about an AUTOINCREMENT */
101000  sqlite3 *db = pParse->db;  /* The database connection */
101001  Db *pDb;                   /* Database only autoinc table */
101002  int memId;                 /* Register holding max rowid */
101003  int addr;                  /* A VDBE address */
101004  Vdbe *v = pParse->pVdbe;   /* VDBE under construction */
101005
101006  /* This routine is never called during trigger-generation.  It is
101007  ** only called from the top-level */
101008  assert( pParse->pTriggerTab==0 );
101009  assert( pParse==sqlite3ParseToplevel(pParse) );
101010
101011  assert( v );   /* We failed long ago if this is not so */
101012  for(p = pParse->pAinc; p; p = p->pNext){
101013    pDb = &db->aDb[p->iDb];
101014    memId = p->regCtr;
101015    assert( sqlite3SchemaMutexHeld(db, 0, pDb->pSchema) );
101016    sqlite3OpenTable(pParse, 0, p->iDb, pDb->pSchema->pSeqTab, OP_OpenRead);
101017    sqlite3VdbeAddOp3(v, OP_Null, 0, memId, memId+1);
101018    addr = sqlite3VdbeCurrentAddr(v);
101019    sqlite3VdbeAddOp4(v, OP_String8, 0, memId-1, 0, p->pTab->zName, 0);
101020    sqlite3VdbeAddOp2(v, OP_Rewind, 0, addr+9); VdbeCoverage(v);
101021    sqlite3VdbeAddOp3(v, OP_Column, 0, 0, memId);
101022    sqlite3VdbeAddOp3(v, OP_Ne, memId-1, addr+7, memId); VdbeCoverage(v);
101023    sqlite3VdbeChangeP5(v, SQLITE_JUMPIFNULL);
101024    sqlite3VdbeAddOp2(v, OP_Rowid, 0, memId+1);
101025    sqlite3VdbeAddOp3(v, OP_Column, 0, 1, memId);
101026    sqlite3VdbeAddOp2(v, OP_Goto, 0, addr+9);
101027    sqlite3VdbeAddOp2(v, OP_Next, 0, addr+2); VdbeCoverage(v);
101028    sqlite3VdbeAddOp2(v, OP_Integer, 0, memId);
101029    sqlite3VdbeAddOp0(v, OP_Close);
101030  }
101031}
101032
101033/*
101034** Update the maximum rowid for an autoincrement calculation.
101035**
101036** This routine should be called when the top of the stack holds a
101037** new rowid that is about to be inserted.  If that new rowid is
101038** larger than the maximum rowid in the memId memory cell, then the
101039** memory cell is updated.  The stack is unchanged.
101040*/
101041static void autoIncStep(Parse *pParse, int memId, int regRowid){
101042  if( memId>0 ){
101043    sqlite3VdbeAddOp2(pParse->pVdbe, OP_MemMax, memId, regRowid);
101044  }
101045}
101046
101047/*
101048** This routine generates the code needed to write autoincrement
101049** maximum rowid values back into the sqlite_sequence register.
101050** Every statement that might do an INSERT into an autoincrement
101051** table (either directly or through triggers) needs to call this
101052** routine just before the "exit" code.
101053*/
101054SQLITE_PRIVATE void sqlite3AutoincrementEnd(Parse *pParse){
101055  AutoincInfo *p;
101056  Vdbe *v = pParse->pVdbe;
101057  sqlite3 *db = pParse->db;
101058
101059  assert( v );
101060  for(p = pParse->pAinc; p; p = p->pNext){
101061    Db *pDb = &db->aDb[p->iDb];
101062    int j1;
101063    int iRec;
101064    int memId = p->regCtr;
101065
101066    iRec = sqlite3GetTempReg(pParse);
101067    assert( sqlite3SchemaMutexHeld(db, 0, pDb->pSchema) );
101068    sqlite3OpenTable(pParse, 0, p->iDb, pDb->pSchema->pSeqTab, OP_OpenWrite);
101069    j1 = sqlite3VdbeAddOp1(v, OP_NotNull, memId+1); VdbeCoverage(v);
101070    sqlite3VdbeAddOp2(v, OP_NewRowid, 0, memId+1);
101071    sqlite3VdbeJumpHere(v, j1);
101072    sqlite3VdbeAddOp3(v, OP_MakeRecord, memId-1, 2, iRec);
101073    sqlite3VdbeAddOp3(v, OP_Insert, 0, iRec, memId+1);
101074    sqlite3VdbeChangeP5(v, OPFLAG_APPEND);
101075    sqlite3VdbeAddOp0(v, OP_Close);
101076    sqlite3ReleaseTempReg(pParse, iRec);
101077  }
101078}
101079#else
101080/*
101081** If SQLITE_OMIT_AUTOINCREMENT is defined, then the three routines
101082** above are all no-ops
101083*/
101084# define autoIncBegin(A,B,C) (0)
101085# define autoIncStep(A,B,C)
101086#endif /* SQLITE_OMIT_AUTOINCREMENT */
101087
101088
101089/* Forward declaration */
101090static int xferOptimization(
101091  Parse *pParse,        /* Parser context */
101092  Table *pDest,         /* The table we are inserting into */
101093  Select *pSelect,      /* A SELECT statement to use as the data source */
101094  int onError,          /* How to handle constraint errors */
101095  int iDbDest           /* The database of pDest */
101096);
101097
101098/*
101099** This routine is called to handle SQL of the following forms:
101100**
101101**    insert into TABLE (IDLIST) values(EXPRLIST),(EXPRLIST),...
101102**    insert into TABLE (IDLIST) select
101103**    insert into TABLE (IDLIST) default values
101104**
101105** The IDLIST following the table name is always optional.  If omitted,
101106** then a list of all (non-hidden) columns for the table is substituted.
101107** The IDLIST appears in the pColumn parameter.  pColumn is NULL if IDLIST
101108** is omitted.
101109**
101110** For the pSelect parameter holds the values to be inserted for the
101111** first two forms shown above.  A VALUES clause is really just short-hand
101112** for a SELECT statement that omits the FROM clause and everything else
101113** that follows.  If the pSelect parameter is NULL, that means that the
101114** DEFAULT VALUES form of the INSERT statement is intended.
101115**
101116** The code generated follows one of four templates.  For a simple
101117** insert with data coming from a single-row VALUES clause, the code executes
101118** once straight down through.  Pseudo-code follows (we call this
101119** the "1st template"):
101120**
101121**         open write cursor to <table> and its indices
101122**         put VALUES clause expressions into registers
101123**         write the resulting record into <table>
101124**         cleanup
101125**
101126** The three remaining templates assume the statement is of the form
101127**
101128**   INSERT INTO <table> SELECT ...
101129**
101130** If the SELECT clause is of the restricted form "SELECT * FROM <table2>" -
101131** in other words if the SELECT pulls all columns from a single table
101132** and there is no WHERE or LIMIT or GROUP BY or ORDER BY clauses, and
101133** if <table2> and <table1> are distinct tables but have identical
101134** schemas, including all the same indices, then a special optimization
101135** is invoked that copies raw records from <table2> over to <table1>.
101136** See the xferOptimization() function for the implementation of this
101137** template.  This is the 2nd template.
101138**
101139**         open a write cursor to <table>
101140**         open read cursor on <table2>
101141**         transfer all records in <table2> over to <table>
101142**         close cursors
101143**         foreach index on <table>
101144**           open a write cursor on the <table> index
101145**           open a read cursor on the corresponding <table2> index
101146**           transfer all records from the read to the write cursors
101147**           close cursors
101148**         end foreach
101149**
101150** The 3rd template is for when the second template does not apply
101151** and the SELECT clause does not read from <table> at any time.
101152** The generated code follows this template:
101153**
101154**         X <- A
101155**         goto B
101156**      A: setup for the SELECT
101157**         loop over the rows in the SELECT
101158**           load values into registers R..R+n
101159**           yield X
101160**         end loop
101161**         cleanup after the SELECT
101162**         end-coroutine X
101163**      B: open write cursor to <table> and its indices
101164**      C: yield X, at EOF goto D
101165**         insert the select result into <table> from R..R+n
101166**         goto C
101167**      D: cleanup
101168**
101169** The 4th template is used if the insert statement takes its
101170** values from a SELECT but the data is being inserted into a table
101171** that is also read as part of the SELECT.  In the third form,
101172** we have to use an intermediate table to store the results of
101173** the select.  The template is like this:
101174**
101175**         X <- A
101176**         goto B
101177**      A: setup for the SELECT
101178**         loop over the tables in the SELECT
101179**           load value into register R..R+n
101180**           yield X
101181**         end loop
101182**         cleanup after the SELECT
101183**         end co-routine R
101184**      B: open temp table
101185**      L: yield X, at EOF goto M
101186**         insert row from R..R+n into temp table
101187**         goto L
101188**      M: open write cursor to <table> and its indices
101189**         rewind temp table
101190**      C: loop over rows of intermediate table
101191**           transfer values form intermediate table into <table>
101192**         end loop
101193**      D: cleanup
101194*/
101195SQLITE_PRIVATE void sqlite3Insert(
101196  Parse *pParse,        /* Parser context */
101197  SrcList *pTabList,    /* Name of table into which we are inserting */
101198  Select *pSelect,      /* A SELECT statement to use as the data source */
101199  IdList *pColumn,      /* Column names corresponding to IDLIST. */
101200  int onError           /* How to handle constraint errors */
101201){
101202  sqlite3 *db;          /* The main database structure */
101203  Table *pTab;          /* The table to insert into.  aka TABLE */
101204  char *zTab;           /* Name of the table into which we are inserting */
101205  const char *zDb;      /* Name of the database holding this table */
101206  int i, j, idx;        /* Loop counters */
101207  Vdbe *v;              /* Generate code into this virtual machine */
101208  Index *pIdx;          /* For looping over indices of the table */
101209  int nColumn;          /* Number of columns in the data */
101210  int nHidden = 0;      /* Number of hidden columns if TABLE is virtual */
101211  int iDataCur = 0;     /* VDBE cursor that is the main data repository */
101212  int iIdxCur = 0;      /* First index cursor */
101213  int ipkColumn = -1;   /* Column that is the INTEGER PRIMARY KEY */
101214  int endOfLoop;        /* Label for the end of the insertion loop */
101215  int srcTab = 0;       /* Data comes from this temporary cursor if >=0 */
101216  int addrInsTop = 0;   /* Jump to label "D" */
101217  int addrCont = 0;     /* Top of insert loop. Label "C" in templates 3 and 4 */
101218  SelectDest dest;      /* Destination for SELECT on rhs of INSERT */
101219  int iDb;              /* Index of database holding TABLE */
101220  Db *pDb;              /* The database containing table being inserted into */
101221  u8 useTempTable = 0;  /* Store SELECT results in intermediate table */
101222  u8 appendFlag = 0;    /* True if the insert is likely to be an append */
101223  u8 withoutRowid;      /* 0 for normal table.  1 for WITHOUT ROWID table */
101224  u8 bIdListInOrder;    /* True if IDLIST is in table order */
101225  ExprList *pList = 0;  /* List of VALUES() to be inserted  */
101226
101227  /* Register allocations */
101228  int regFromSelect = 0;/* Base register for data coming from SELECT */
101229  int regAutoinc = 0;   /* Register holding the AUTOINCREMENT counter */
101230  int regRowCount = 0;  /* Memory cell used for the row counter */
101231  int regIns;           /* Block of regs holding rowid+data being inserted */
101232  int regRowid;         /* registers holding insert rowid */
101233  int regData;          /* register holding first column to insert */
101234  int *aRegIdx = 0;     /* One register allocated to each index */
101235
101236#ifndef SQLITE_OMIT_TRIGGER
101237  int isView;                 /* True if attempting to insert into a view */
101238  Trigger *pTrigger;          /* List of triggers on pTab, if required */
101239  int tmask;                  /* Mask of trigger times */
101240#endif
101241
101242  db = pParse->db;
101243  memset(&dest, 0, sizeof(dest));
101244  if( pParse->nErr || db->mallocFailed ){
101245    goto insert_cleanup;
101246  }
101247
101248  /* If the Select object is really just a simple VALUES() list with a
101249  ** single row (the common case) then keep that one row of values
101250  ** and discard the other (unused) parts of the pSelect object
101251  */
101252  if( pSelect && (pSelect->selFlags & SF_Values)!=0 && pSelect->pPrior==0 ){
101253    pList = pSelect->pEList;
101254    pSelect->pEList = 0;
101255    sqlite3SelectDelete(db, pSelect);
101256    pSelect = 0;
101257  }
101258
101259  /* Locate the table into which we will be inserting new information.
101260  */
101261  assert( pTabList->nSrc==1 );
101262  zTab = pTabList->a[0].zName;
101263  if( NEVER(zTab==0) ) goto insert_cleanup;
101264  pTab = sqlite3SrcListLookup(pParse, pTabList);
101265  if( pTab==0 ){
101266    goto insert_cleanup;
101267  }
101268  iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
101269  assert( iDb<db->nDb );
101270  pDb = &db->aDb[iDb];
101271  zDb = pDb->zName;
101272  if( sqlite3AuthCheck(pParse, SQLITE_INSERT, pTab->zName, 0, zDb) ){
101273    goto insert_cleanup;
101274  }
101275  withoutRowid = !HasRowid(pTab);
101276
101277  /* Figure out if we have any triggers and if the table being
101278  ** inserted into is a view
101279  */
101280#ifndef SQLITE_OMIT_TRIGGER
101281  pTrigger = sqlite3TriggersExist(pParse, pTab, TK_INSERT, 0, &tmask);
101282  isView = pTab->pSelect!=0;
101283#else
101284# define pTrigger 0
101285# define tmask 0
101286# define isView 0
101287#endif
101288#ifdef SQLITE_OMIT_VIEW
101289# undef isView
101290# define isView 0
101291#endif
101292  assert( (pTrigger && tmask) || (pTrigger==0 && tmask==0) );
101293
101294  /* If pTab is really a view, make sure it has been initialized.
101295  ** ViewGetColumnNames() is a no-op if pTab is not a view.
101296  */
101297  if( sqlite3ViewGetColumnNames(pParse, pTab) ){
101298    goto insert_cleanup;
101299  }
101300
101301  /* Cannot insert into a read-only table.
101302  */
101303  if( sqlite3IsReadOnly(pParse, pTab, tmask) ){
101304    goto insert_cleanup;
101305  }
101306
101307  /* Allocate a VDBE
101308  */
101309  v = sqlite3GetVdbe(pParse);
101310  if( v==0 ) goto insert_cleanup;
101311  if( pParse->nested==0 ) sqlite3VdbeCountChanges(v);
101312  sqlite3BeginWriteOperation(pParse, pSelect || pTrigger, iDb);
101313
101314#ifndef SQLITE_OMIT_XFER_OPT
101315  /* If the statement is of the form
101316  **
101317  **       INSERT INTO <table1> SELECT * FROM <table2>;
101318  **
101319  ** Then special optimizations can be applied that make the transfer
101320  ** very fast and which reduce fragmentation of indices.
101321  **
101322  ** This is the 2nd template.
101323  */
101324  if( pColumn==0 && xferOptimization(pParse, pTab, pSelect, onError, iDb) ){
101325    assert( !pTrigger );
101326    assert( pList==0 );
101327    goto insert_end;
101328  }
101329#endif /* SQLITE_OMIT_XFER_OPT */
101330
101331  /* If this is an AUTOINCREMENT table, look up the sequence number in the
101332  ** sqlite_sequence table and store it in memory cell regAutoinc.
101333  */
101334  regAutoinc = autoIncBegin(pParse, iDb, pTab);
101335
101336  /* Allocate registers for holding the rowid of the new row,
101337  ** the content of the new row, and the assembled row record.
101338  */
101339  regRowid = regIns = pParse->nMem+1;
101340  pParse->nMem += pTab->nCol + 1;
101341  if( IsVirtual(pTab) ){
101342    regRowid++;
101343    pParse->nMem++;
101344  }
101345  regData = regRowid+1;
101346
101347  /* If the INSERT statement included an IDLIST term, then make sure
101348  ** all elements of the IDLIST really are columns of the table and
101349  ** remember the column indices.
101350  **
101351  ** If the table has an INTEGER PRIMARY KEY column and that column
101352  ** is named in the IDLIST, then record in the ipkColumn variable
101353  ** the index into IDLIST of the primary key column.  ipkColumn is
101354  ** the index of the primary key as it appears in IDLIST, not as
101355  ** is appears in the original table.  (The index of the INTEGER
101356  ** PRIMARY KEY in the original table is pTab->iPKey.)
101357  */
101358  bIdListInOrder = (pTab->tabFlags & TF_OOOHidden)==0;
101359  if( pColumn ){
101360    for(i=0; i<pColumn->nId; i++){
101361      pColumn->a[i].idx = -1;
101362    }
101363    for(i=0; i<pColumn->nId; i++){
101364      for(j=0; j<pTab->nCol; j++){
101365        if( sqlite3StrICmp(pColumn->a[i].zName, pTab->aCol[j].zName)==0 ){
101366          pColumn->a[i].idx = j;
101367          if( i!=j ) bIdListInOrder = 0;
101368          if( j==pTab->iPKey ){
101369            ipkColumn = i;  assert( !withoutRowid );
101370          }
101371          break;
101372        }
101373      }
101374      if( j>=pTab->nCol ){
101375        if( sqlite3IsRowid(pColumn->a[i].zName) && !withoutRowid ){
101376          ipkColumn = i;
101377          bIdListInOrder = 0;
101378        }else{
101379          sqlite3ErrorMsg(pParse, "table %S has no column named %s",
101380              pTabList, 0, pColumn->a[i].zName);
101381          pParse->checkSchema = 1;
101382          goto insert_cleanup;
101383        }
101384      }
101385    }
101386  }
101387
101388  /* Figure out how many columns of data are supplied.  If the data
101389  ** is coming from a SELECT statement, then generate a co-routine that
101390  ** produces a single row of the SELECT on each invocation.  The
101391  ** co-routine is the common header to the 3rd and 4th templates.
101392  */
101393  if( pSelect ){
101394    /* Data is coming from a SELECT or from a multi-row VALUES clause.
101395    ** Generate a co-routine to run the SELECT. */
101396    int regYield;       /* Register holding co-routine entry-point */
101397    int addrTop;        /* Top of the co-routine */
101398    int rc;             /* Result code */
101399
101400    regYield = ++pParse->nMem;
101401    addrTop = sqlite3VdbeCurrentAddr(v) + 1;
101402    sqlite3VdbeAddOp3(v, OP_InitCoroutine, regYield, 0, addrTop);
101403    sqlite3SelectDestInit(&dest, SRT_Coroutine, regYield);
101404    dest.iSdst = bIdListInOrder ? regData : 0;
101405    dest.nSdst = pTab->nCol;
101406    rc = sqlite3Select(pParse, pSelect, &dest);
101407    regFromSelect = dest.iSdst;
101408    if( rc || db->mallocFailed || pParse->nErr ) goto insert_cleanup;
101409    sqlite3VdbeAddOp1(v, OP_EndCoroutine, regYield);
101410    sqlite3VdbeJumpHere(v, addrTop - 1);                       /* label B: */
101411    assert( pSelect->pEList );
101412    nColumn = pSelect->pEList->nExpr;
101413
101414    /* Set useTempTable to TRUE if the result of the SELECT statement
101415    ** should be written into a temporary table (template 4).  Set to
101416    ** FALSE if each output row of the SELECT can be written directly into
101417    ** the destination table (template 3).
101418    **
101419    ** A temp table must be used if the table being updated is also one
101420    ** of the tables being read by the SELECT statement.  Also use a
101421    ** temp table in the case of row triggers.
101422    */
101423    if( pTrigger || readsTable(pParse, iDb, pTab) ){
101424      useTempTable = 1;
101425    }
101426
101427    if( useTempTable ){
101428      /* Invoke the coroutine to extract information from the SELECT
101429      ** and add it to a transient table srcTab.  The code generated
101430      ** here is from the 4th template:
101431      **
101432      **      B: open temp table
101433      **      L: yield X, goto M at EOF
101434      **         insert row from R..R+n into temp table
101435      **         goto L
101436      **      M: ...
101437      */
101438      int regRec;          /* Register to hold packed record */
101439      int regTempRowid;    /* Register to hold temp table ROWID */
101440      int addrL;           /* Label "L" */
101441
101442      srcTab = pParse->nTab++;
101443      regRec = sqlite3GetTempReg(pParse);
101444      regTempRowid = sqlite3GetTempReg(pParse);
101445      sqlite3VdbeAddOp2(v, OP_OpenEphemeral, srcTab, nColumn);
101446      addrL = sqlite3VdbeAddOp1(v, OP_Yield, dest.iSDParm); VdbeCoverage(v);
101447      sqlite3VdbeAddOp3(v, OP_MakeRecord, regFromSelect, nColumn, regRec);
101448      sqlite3VdbeAddOp2(v, OP_NewRowid, srcTab, regTempRowid);
101449      sqlite3VdbeAddOp3(v, OP_Insert, srcTab, regRec, regTempRowid);
101450      sqlite3VdbeAddOp2(v, OP_Goto, 0, addrL);
101451      sqlite3VdbeJumpHere(v, addrL);
101452      sqlite3ReleaseTempReg(pParse, regRec);
101453      sqlite3ReleaseTempReg(pParse, regTempRowid);
101454    }
101455  }else{
101456    /* This is the case if the data for the INSERT is coming from a
101457    ** single-row VALUES clause
101458    */
101459    NameContext sNC;
101460    memset(&sNC, 0, sizeof(sNC));
101461    sNC.pParse = pParse;
101462    srcTab = -1;
101463    assert( useTempTable==0 );
101464    nColumn = pList ? pList->nExpr : 0;
101465    for(i=0; i<nColumn; i++){
101466      if( sqlite3ResolveExprNames(&sNC, pList->a[i].pExpr) ){
101467        goto insert_cleanup;
101468      }
101469    }
101470  }
101471
101472  /* If there is no IDLIST term but the table has an integer primary
101473  ** key, the set the ipkColumn variable to the integer primary key
101474  ** column index in the original table definition.
101475  */
101476  if( pColumn==0 && nColumn>0 ){
101477    ipkColumn = pTab->iPKey;
101478  }
101479
101480  /* Make sure the number of columns in the source data matches the number
101481  ** of columns to be inserted into the table.
101482  */
101483  if( IsVirtual(pTab) ){
101484    for(i=0; i<pTab->nCol; i++){
101485      nHidden += (IsHiddenColumn(&pTab->aCol[i]) ? 1 : 0);
101486    }
101487  }
101488  if( pColumn==0 && nColumn && nColumn!=(pTab->nCol-nHidden) ){
101489    sqlite3ErrorMsg(pParse,
101490       "table %S has %d columns but %d values were supplied",
101491       pTabList, 0, pTab->nCol-nHidden, nColumn);
101492    goto insert_cleanup;
101493  }
101494  if( pColumn!=0 && nColumn!=pColumn->nId ){
101495    sqlite3ErrorMsg(pParse, "%d values for %d columns", nColumn, pColumn->nId);
101496    goto insert_cleanup;
101497  }
101498
101499  /* Initialize the count of rows to be inserted
101500  */
101501  if( db->flags & SQLITE_CountRows ){
101502    regRowCount = ++pParse->nMem;
101503    sqlite3VdbeAddOp2(v, OP_Integer, 0, regRowCount);
101504  }
101505
101506  /* If this is not a view, open the table and and all indices */
101507  if( !isView ){
101508    int nIdx;
101509    nIdx = sqlite3OpenTableAndIndices(pParse, pTab, OP_OpenWrite, -1, 0,
101510                                      &iDataCur, &iIdxCur);
101511    aRegIdx = sqlite3DbMallocRaw(db, sizeof(int)*(nIdx+1));
101512    if( aRegIdx==0 ){
101513      goto insert_cleanup;
101514    }
101515    for(i=0; i<nIdx; i++){
101516      aRegIdx[i] = ++pParse->nMem;
101517    }
101518  }
101519
101520  /* This is the top of the main insertion loop */
101521  if( useTempTable ){
101522    /* This block codes the top of loop only.  The complete loop is the
101523    ** following pseudocode (template 4):
101524    **
101525    **         rewind temp table, if empty goto D
101526    **      C: loop over rows of intermediate table
101527    **           transfer values form intermediate table into <table>
101528    **         end loop
101529    **      D: ...
101530    */
101531    addrInsTop = sqlite3VdbeAddOp1(v, OP_Rewind, srcTab); VdbeCoverage(v);
101532    addrCont = sqlite3VdbeCurrentAddr(v);
101533  }else if( pSelect ){
101534    /* This block codes the top of loop only.  The complete loop is the
101535    ** following pseudocode (template 3):
101536    **
101537    **      C: yield X, at EOF goto D
101538    **         insert the select result into <table> from R..R+n
101539    **         goto C
101540    **      D: ...
101541    */
101542    addrInsTop = addrCont = sqlite3VdbeAddOp1(v, OP_Yield, dest.iSDParm);
101543    VdbeCoverage(v);
101544  }
101545
101546  /* Run the BEFORE and INSTEAD OF triggers, if there are any
101547  */
101548  endOfLoop = sqlite3VdbeMakeLabel(v);
101549  if( tmask & TRIGGER_BEFORE ){
101550    int regCols = sqlite3GetTempRange(pParse, pTab->nCol+1);
101551
101552    /* build the NEW.* reference row.  Note that if there is an INTEGER
101553    ** PRIMARY KEY into which a NULL is being inserted, that NULL will be
101554    ** translated into a unique ID for the row.  But on a BEFORE trigger,
101555    ** we do not know what the unique ID will be (because the insert has
101556    ** not happened yet) so we substitute a rowid of -1
101557    */
101558    if( ipkColumn<0 ){
101559      sqlite3VdbeAddOp2(v, OP_Integer, -1, regCols);
101560    }else{
101561      int j1;
101562      assert( !withoutRowid );
101563      if( useTempTable ){
101564        sqlite3VdbeAddOp3(v, OP_Column, srcTab, ipkColumn, regCols);
101565      }else{
101566        assert( pSelect==0 );  /* Otherwise useTempTable is true */
101567        sqlite3ExprCode(pParse, pList->a[ipkColumn].pExpr, regCols);
101568      }
101569      j1 = sqlite3VdbeAddOp1(v, OP_NotNull, regCols); VdbeCoverage(v);
101570      sqlite3VdbeAddOp2(v, OP_Integer, -1, regCols);
101571      sqlite3VdbeJumpHere(v, j1);
101572      sqlite3VdbeAddOp1(v, OP_MustBeInt, regCols); VdbeCoverage(v);
101573    }
101574
101575    /* Cannot have triggers on a virtual table. If it were possible,
101576    ** this block would have to account for hidden column.
101577    */
101578    assert( !IsVirtual(pTab) );
101579
101580    /* Create the new column data
101581    */
101582    for(i=0; i<pTab->nCol; i++){
101583      if( pColumn==0 ){
101584        j = i;
101585      }else{
101586        for(j=0; j<pColumn->nId; j++){
101587          if( pColumn->a[j].idx==i ) break;
101588        }
101589      }
101590      if( (!useTempTable && !pList) || (pColumn && j>=pColumn->nId) ){
101591        sqlite3ExprCode(pParse, pTab->aCol[i].pDflt, regCols+i+1);
101592      }else if( useTempTable ){
101593        sqlite3VdbeAddOp3(v, OP_Column, srcTab, j, regCols+i+1);
101594      }else{
101595        assert( pSelect==0 ); /* Otherwise useTempTable is true */
101596        sqlite3ExprCodeAndCache(pParse, pList->a[j].pExpr, regCols+i+1);
101597      }
101598    }
101599
101600    /* If this is an INSERT on a view with an INSTEAD OF INSERT trigger,
101601    ** do not attempt any conversions before assembling the record.
101602    ** If this is a real table, attempt conversions as required by the
101603    ** table column affinities.
101604    */
101605    if( !isView ){
101606      sqlite3TableAffinity(v, pTab, regCols+1);
101607    }
101608
101609    /* Fire BEFORE or INSTEAD OF triggers */
101610    sqlite3CodeRowTrigger(pParse, pTrigger, TK_INSERT, 0, TRIGGER_BEFORE,
101611        pTab, regCols-pTab->nCol-1, onError, endOfLoop);
101612
101613    sqlite3ReleaseTempRange(pParse, regCols, pTab->nCol+1);
101614  }
101615
101616  /* Compute the content of the next row to insert into a range of
101617  ** registers beginning at regIns.
101618  */
101619  if( !isView ){
101620    if( IsVirtual(pTab) ){
101621      /* The row that the VUpdate opcode will delete: none */
101622      sqlite3VdbeAddOp2(v, OP_Null, 0, regIns);
101623    }
101624    if( ipkColumn>=0 ){
101625      if( useTempTable ){
101626        sqlite3VdbeAddOp3(v, OP_Column, srcTab, ipkColumn, regRowid);
101627      }else if( pSelect ){
101628        sqlite3VdbeAddOp2(v, OP_Copy, regFromSelect+ipkColumn, regRowid);
101629      }else{
101630        VdbeOp *pOp;
101631        sqlite3ExprCode(pParse, pList->a[ipkColumn].pExpr, regRowid);
101632        pOp = sqlite3VdbeGetOp(v, -1);
101633        if( ALWAYS(pOp) && pOp->opcode==OP_Null && !IsVirtual(pTab) ){
101634          appendFlag = 1;
101635          pOp->opcode = OP_NewRowid;
101636          pOp->p1 = iDataCur;
101637          pOp->p2 = regRowid;
101638          pOp->p3 = regAutoinc;
101639        }
101640      }
101641      /* If the PRIMARY KEY expression is NULL, then use OP_NewRowid
101642      ** to generate a unique primary key value.
101643      */
101644      if( !appendFlag ){
101645        int j1;
101646        if( !IsVirtual(pTab) ){
101647          j1 = sqlite3VdbeAddOp1(v, OP_NotNull, regRowid); VdbeCoverage(v);
101648          sqlite3VdbeAddOp3(v, OP_NewRowid, iDataCur, regRowid, regAutoinc);
101649          sqlite3VdbeJumpHere(v, j1);
101650        }else{
101651          j1 = sqlite3VdbeCurrentAddr(v);
101652          sqlite3VdbeAddOp2(v, OP_IsNull, regRowid, j1+2); VdbeCoverage(v);
101653        }
101654        sqlite3VdbeAddOp1(v, OP_MustBeInt, regRowid); VdbeCoverage(v);
101655      }
101656    }else if( IsVirtual(pTab) || withoutRowid ){
101657      sqlite3VdbeAddOp2(v, OP_Null, 0, regRowid);
101658    }else{
101659      sqlite3VdbeAddOp3(v, OP_NewRowid, iDataCur, regRowid, regAutoinc);
101660      appendFlag = 1;
101661    }
101662    autoIncStep(pParse, regAutoinc, regRowid);
101663
101664    /* Compute data for all columns of the new entry, beginning
101665    ** with the first column.
101666    */
101667    nHidden = 0;
101668    for(i=0; i<pTab->nCol; i++){
101669      int iRegStore = regRowid+1+i;
101670      if( i==pTab->iPKey ){
101671        /* The value of the INTEGER PRIMARY KEY column is always a NULL.
101672        ** Whenever this column is read, the rowid will be substituted
101673        ** in its place.  Hence, fill this column with a NULL to avoid
101674        ** taking up data space with information that will never be used.
101675        ** As there may be shallow copies of this value, make it a soft-NULL */
101676        sqlite3VdbeAddOp1(v, OP_SoftNull, iRegStore);
101677        continue;
101678      }
101679      if( pColumn==0 ){
101680        if( IsHiddenColumn(&pTab->aCol[i]) ){
101681          assert( IsVirtual(pTab) );
101682          j = -1;
101683          nHidden++;
101684        }else{
101685          j = i - nHidden;
101686        }
101687      }else{
101688        for(j=0; j<pColumn->nId; j++){
101689          if( pColumn->a[j].idx==i ) break;
101690        }
101691      }
101692      if( j<0 || nColumn==0 || (pColumn && j>=pColumn->nId) ){
101693        sqlite3ExprCodeFactorable(pParse, pTab->aCol[i].pDflt, iRegStore);
101694      }else if( useTempTable ){
101695        sqlite3VdbeAddOp3(v, OP_Column, srcTab, j, iRegStore);
101696      }else if( pSelect ){
101697        if( regFromSelect!=regData ){
101698          sqlite3VdbeAddOp2(v, OP_SCopy, regFromSelect+j, iRegStore);
101699        }
101700      }else{
101701        sqlite3ExprCode(pParse, pList->a[j].pExpr, iRegStore);
101702      }
101703    }
101704
101705    /* Generate code to check constraints and generate index keys and
101706    ** do the insertion.
101707    */
101708#ifndef SQLITE_OMIT_VIRTUALTABLE
101709    if( IsVirtual(pTab) ){
101710      const char *pVTab = (const char *)sqlite3GetVTable(db, pTab);
101711      sqlite3VtabMakeWritable(pParse, pTab);
101712      sqlite3VdbeAddOp4(v, OP_VUpdate, 1, pTab->nCol+2, regIns, pVTab, P4_VTAB);
101713      sqlite3VdbeChangeP5(v, onError==OE_Default ? OE_Abort : onError);
101714      sqlite3MayAbort(pParse);
101715    }else
101716#endif
101717    {
101718      int isReplace;    /* Set to true if constraints may cause a replace */
101719      sqlite3GenerateConstraintChecks(pParse, pTab, aRegIdx, iDataCur, iIdxCur,
101720          regIns, 0, ipkColumn>=0, onError, endOfLoop, &isReplace
101721      );
101722      sqlite3FkCheck(pParse, pTab, 0, regIns, 0, 0);
101723      sqlite3CompleteInsertion(pParse, pTab, iDataCur, iIdxCur,
101724                               regIns, aRegIdx, 0, appendFlag, isReplace==0);
101725    }
101726  }
101727
101728  /* Update the count of rows that are inserted
101729  */
101730  if( (db->flags & SQLITE_CountRows)!=0 ){
101731    sqlite3VdbeAddOp2(v, OP_AddImm, regRowCount, 1);
101732  }
101733
101734  if( pTrigger ){
101735    /* Code AFTER triggers */
101736    sqlite3CodeRowTrigger(pParse, pTrigger, TK_INSERT, 0, TRIGGER_AFTER,
101737        pTab, regData-2-pTab->nCol, onError, endOfLoop);
101738  }
101739
101740  /* The bottom of the main insertion loop, if the data source
101741  ** is a SELECT statement.
101742  */
101743  sqlite3VdbeResolveLabel(v, endOfLoop);
101744  if( useTempTable ){
101745    sqlite3VdbeAddOp2(v, OP_Next, srcTab, addrCont); VdbeCoverage(v);
101746    sqlite3VdbeJumpHere(v, addrInsTop);
101747    sqlite3VdbeAddOp1(v, OP_Close, srcTab);
101748  }else if( pSelect ){
101749    sqlite3VdbeAddOp2(v, OP_Goto, 0, addrCont);
101750    sqlite3VdbeJumpHere(v, addrInsTop);
101751  }
101752
101753  if( !IsVirtual(pTab) && !isView ){
101754    /* Close all tables opened */
101755    if( iDataCur<iIdxCur ) sqlite3VdbeAddOp1(v, OP_Close, iDataCur);
101756    for(idx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, idx++){
101757      sqlite3VdbeAddOp1(v, OP_Close, idx+iIdxCur);
101758    }
101759  }
101760
101761insert_end:
101762  /* Update the sqlite_sequence table by storing the content of the
101763  ** maximum rowid counter values recorded while inserting into
101764  ** autoincrement tables.
101765  */
101766  if( pParse->nested==0 && pParse->pTriggerTab==0 ){
101767    sqlite3AutoincrementEnd(pParse);
101768  }
101769
101770  /*
101771  ** Return the number of rows inserted. If this routine is
101772  ** generating code because of a call to sqlite3NestedParse(), do not
101773  ** invoke the callback function.
101774  */
101775  if( (db->flags&SQLITE_CountRows) && !pParse->nested && !pParse->pTriggerTab ){
101776    sqlite3VdbeAddOp2(v, OP_ResultRow, regRowCount, 1);
101777    sqlite3VdbeSetNumCols(v, 1);
101778    sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "rows inserted", SQLITE_STATIC);
101779  }
101780
101781insert_cleanup:
101782  sqlite3SrcListDelete(db, pTabList);
101783  sqlite3ExprListDelete(db, pList);
101784  sqlite3SelectDelete(db, pSelect);
101785  sqlite3IdListDelete(db, pColumn);
101786  sqlite3DbFree(db, aRegIdx);
101787}
101788
101789/* Make sure "isView" and other macros defined above are undefined. Otherwise
101790** they may interfere with compilation of other functions in this file
101791** (or in another file, if this file becomes part of the amalgamation).  */
101792#ifdef isView
101793 #undef isView
101794#endif
101795#ifdef pTrigger
101796 #undef pTrigger
101797#endif
101798#ifdef tmask
101799 #undef tmask
101800#endif
101801
101802/*
101803** Generate code to do constraint checks prior to an INSERT or an UPDATE
101804** on table pTab.
101805**
101806** The regNewData parameter is the first register in a range that contains
101807** the data to be inserted or the data after the update.  There will be
101808** pTab->nCol+1 registers in this range.  The first register (the one
101809** that regNewData points to) will contain the new rowid, or NULL in the
101810** case of a WITHOUT ROWID table.  The second register in the range will
101811** contain the content of the first table column.  The third register will
101812** contain the content of the second table column.  And so forth.
101813**
101814** The regOldData parameter is similar to regNewData except that it contains
101815** the data prior to an UPDATE rather than afterwards.  regOldData is zero
101816** for an INSERT.  This routine can distinguish between UPDATE and INSERT by
101817** checking regOldData for zero.
101818**
101819** For an UPDATE, the pkChng boolean is true if the true primary key (the
101820** rowid for a normal table or the PRIMARY KEY for a WITHOUT ROWID table)
101821** might be modified by the UPDATE.  If pkChng is false, then the key of
101822** the iDataCur content table is guaranteed to be unchanged by the UPDATE.
101823**
101824** For an INSERT, the pkChng boolean indicates whether or not the rowid
101825** was explicitly specified as part of the INSERT statement.  If pkChng
101826** is zero, it means that the either rowid is computed automatically or
101827** that the table is a WITHOUT ROWID table and has no rowid.  On an INSERT,
101828** pkChng will only be true if the INSERT statement provides an integer
101829** value for either the rowid column or its INTEGER PRIMARY KEY alias.
101830**
101831** The code generated by this routine will store new index entries into
101832** registers identified by aRegIdx[].  No index entry is created for
101833** indices where aRegIdx[i]==0.  The order of indices in aRegIdx[] is
101834** the same as the order of indices on the linked list of indices
101835** at pTab->pIndex.
101836**
101837** The caller must have already opened writeable cursors on the main
101838** table and all applicable indices (that is to say, all indices for which
101839** aRegIdx[] is not zero).  iDataCur is the cursor for the main table when
101840** inserting or updating a rowid table, or the cursor for the PRIMARY KEY
101841** index when operating on a WITHOUT ROWID table.  iIdxCur is the cursor
101842** for the first index in the pTab->pIndex list.  Cursors for other indices
101843** are at iIdxCur+N for the N-th element of the pTab->pIndex list.
101844**
101845** This routine also generates code to check constraints.  NOT NULL,
101846** CHECK, and UNIQUE constraints are all checked.  If a constraint fails,
101847** then the appropriate action is performed.  There are five possible
101848** actions: ROLLBACK, ABORT, FAIL, REPLACE, and IGNORE.
101849**
101850**  Constraint type  Action       What Happens
101851**  ---------------  ----------   ----------------------------------------
101852**  any              ROLLBACK     The current transaction is rolled back and
101853**                                sqlite3_step() returns immediately with a
101854**                                return code of SQLITE_CONSTRAINT.
101855**
101856**  any              ABORT        Back out changes from the current command
101857**                                only (do not do a complete rollback) then
101858**                                cause sqlite3_step() to return immediately
101859**                                with SQLITE_CONSTRAINT.
101860**
101861**  any              FAIL         Sqlite3_step() returns immediately with a
101862**                                return code of SQLITE_CONSTRAINT.  The
101863**                                transaction is not rolled back and any
101864**                                changes to prior rows are retained.
101865**
101866**  any              IGNORE       The attempt in insert or update the current
101867**                                row is skipped, without throwing an error.
101868**                                Processing continues with the next row.
101869**                                (There is an immediate jump to ignoreDest.)
101870**
101871**  NOT NULL         REPLACE      The NULL value is replace by the default
101872**                                value for that column.  If the default value
101873**                                is NULL, the action is the same as ABORT.
101874**
101875**  UNIQUE           REPLACE      The other row that conflicts with the row
101876**                                being inserted is removed.
101877**
101878**  CHECK            REPLACE      Illegal.  The results in an exception.
101879**
101880** Which action to take is determined by the overrideError parameter.
101881** Or if overrideError==OE_Default, then the pParse->onError parameter
101882** is used.  Or if pParse->onError==OE_Default then the onError value
101883** for the constraint is used.
101884*/
101885SQLITE_PRIVATE void sqlite3GenerateConstraintChecks(
101886  Parse *pParse,       /* The parser context */
101887  Table *pTab,         /* The table being inserted or updated */
101888  int *aRegIdx,        /* Use register aRegIdx[i] for index i.  0 for unused */
101889  int iDataCur,        /* Canonical data cursor (main table or PK index) */
101890  int iIdxCur,         /* First index cursor */
101891  int regNewData,      /* First register in a range holding values to insert */
101892  int regOldData,      /* Previous content.  0 for INSERTs */
101893  u8 pkChng,           /* Non-zero if the rowid or PRIMARY KEY changed */
101894  u8 overrideError,    /* Override onError to this if not OE_Default */
101895  int ignoreDest,      /* Jump to this label on an OE_Ignore resolution */
101896  int *pbMayReplace    /* OUT: Set to true if constraint may cause a replace */
101897){
101898  Vdbe *v;             /* VDBE under constrution */
101899  Index *pIdx;         /* Pointer to one of the indices */
101900  Index *pPk = 0;      /* The PRIMARY KEY index */
101901  sqlite3 *db;         /* Database connection */
101902  int i;               /* loop counter */
101903  int ix;              /* Index loop counter */
101904  int nCol;            /* Number of columns */
101905  int onError;         /* Conflict resolution strategy */
101906  int j1;              /* Address of jump instruction */
101907  int seenReplace = 0; /* True if REPLACE is used to resolve INT PK conflict */
101908  int nPkField;        /* Number of fields in PRIMARY KEY. 1 for ROWID tables */
101909  int ipkTop = 0;      /* Top of the rowid change constraint check */
101910  int ipkBottom = 0;   /* Bottom of the rowid change constraint check */
101911  u8 isUpdate;         /* True if this is an UPDATE operation */
101912  u8 bAffinityDone = 0;  /* True if the OP_Affinity operation has been run */
101913  int regRowid = -1;   /* Register holding ROWID value */
101914
101915  isUpdate = regOldData!=0;
101916  db = pParse->db;
101917  v = sqlite3GetVdbe(pParse);
101918  assert( v!=0 );
101919  assert( pTab->pSelect==0 );  /* This table is not a VIEW */
101920  nCol = pTab->nCol;
101921
101922  /* pPk is the PRIMARY KEY index for WITHOUT ROWID tables and NULL for
101923  ** normal rowid tables.  nPkField is the number of key fields in the
101924  ** pPk index or 1 for a rowid table.  In other words, nPkField is the
101925  ** number of fields in the true primary key of the table. */
101926  if( HasRowid(pTab) ){
101927    pPk = 0;
101928    nPkField = 1;
101929  }else{
101930    pPk = sqlite3PrimaryKeyIndex(pTab);
101931    nPkField = pPk->nKeyCol;
101932  }
101933
101934  /* Record that this module has started */
101935  VdbeModuleComment((v, "BEGIN: GenCnstCks(%d,%d,%d,%d,%d)",
101936                     iDataCur, iIdxCur, regNewData, regOldData, pkChng));
101937
101938  /* Test all NOT NULL constraints.
101939  */
101940  for(i=0; i<nCol; i++){
101941    if( i==pTab->iPKey ){
101942      continue;
101943    }
101944    onError = pTab->aCol[i].notNull;
101945    if( onError==OE_None ) continue;
101946    if( overrideError!=OE_Default ){
101947      onError = overrideError;
101948    }else if( onError==OE_Default ){
101949      onError = OE_Abort;
101950    }
101951    if( onError==OE_Replace && pTab->aCol[i].pDflt==0 ){
101952      onError = OE_Abort;
101953    }
101954    assert( onError==OE_Rollback || onError==OE_Abort || onError==OE_Fail
101955        || onError==OE_Ignore || onError==OE_Replace );
101956    switch( onError ){
101957      case OE_Abort:
101958        sqlite3MayAbort(pParse);
101959        /* Fall through */
101960      case OE_Rollback:
101961      case OE_Fail: {
101962        char *zMsg = sqlite3MPrintf(db, "%s.%s", pTab->zName,
101963                                    pTab->aCol[i].zName);
101964        sqlite3VdbeAddOp4(v, OP_HaltIfNull, SQLITE_CONSTRAINT_NOTNULL, onError,
101965                          regNewData+1+i, zMsg, P4_DYNAMIC);
101966        sqlite3VdbeChangeP5(v, P5_ConstraintNotNull);
101967        VdbeCoverage(v);
101968        break;
101969      }
101970      case OE_Ignore: {
101971        sqlite3VdbeAddOp2(v, OP_IsNull, regNewData+1+i, ignoreDest);
101972        VdbeCoverage(v);
101973        break;
101974      }
101975      default: {
101976        assert( onError==OE_Replace );
101977        j1 = sqlite3VdbeAddOp1(v, OP_NotNull, regNewData+1+i); VdbeCoverage(v);
101978        sqlite3ExprCode(pParse, pTab->aCol[i].pDflt, regNewData+1+i);
101979        sqlite3VdbeJumpHere(v, j1);
101980        break;
101981      }
101982    }
101983  }
101984
101985  /* Test all CHECK constraints
101986  */
101987#ifndef SQLITE_OMIT_CHECK
101988  if( pTab->pCheck && (db->flags & SQLITE_IgnoreChecks)==0 ){
101989    ExprList *pCheck = pTab->pCheck;
101990    pParse->ckBase = regNewData+1;
101991    onError = overrideError!=OE_Default ? overrideError : OE_Abort;
101992    for(i=0; i<pCheck->nExpr; i++){
101993      int allOk = sqlite3VdbeMakeLabel(v);
101994      sqlite3ExprIfTrue(pParse, pCheck->a[i].pExpr, allOk, SQLITE_JUMPIFNULL);
101995      if( onError==OE_Ignore ){
101996        sqlite3VdbeAddOp2(v, OP_Goto, 0, ignoreDest);
101997      }else{
101998        char *zName = pCheck->a[i].zName;
101999        if( zName==0 ) zName = pTab->zName;
102000        if( onError==OE_Replace ) onError = OE_Abort; /* IMP: R-15569-63625 */
102001        sqlite3HaltConstraint(pParse, SQLITE_CONSTRAINT_CHECK,
102002                              onError, zName, P4_TRANSIENT,
102003                              P5_ConstraintCheck);
102004      }
102005      sqlite3VdbeResolveLabel(v, allOk);
102006    }
102007  }
102008#endif /* !defined(SQLITE_OMIT_CHECK) */
102009
102010  /* If rowid is changing, make sure the new rowid does not previously
102011  ** exist in the table.
102012  */
102013  if( pkChng && pPk==0 ){
102014    int addrRowidOk = sqlite3VdbeMakeLabel(v);
102015
102016    /* Figure out what action to take in case of a rowid collision */
102017    onError = pTab->keyConf;
102018    if( overrideError!=OE_Default ){
102019      onError = overrideError;
102020    }else if( onError==OE_Default ){
102021      onError = OE_Abort;
102022    }
102023
102024    if( isUpdate ){
102025      /* pkChng!=0 does not mean that the rowid has change, only that
102026      ** it might have changed.  Skip the conflict logic below if the rowid
102027      ** is unchanged. */
102028      sqlite3VdbeAddOp3(v, OP_Eq, regNewData, addrRowidOk, regOldData);
102029      sqlite3VdbeChangeP5(v, SQLITE_NOTNULL);
102030      VdbeCoverage(v);
102031    }
102032
102033    /* If the response to a rowid conflict is REPLACE but the response
102034    ** to some other UNIQUE constraint is FAIL or IGNORE, then we need
102035    ** to defer the running of the rowid conflict checking until after
102036    ** the UNIQUE constraints have run.
102037    */
102038    if( onError==OE_Replace && overrideError!=OE_Replace ){
102039      for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
102040        if( pIdx->onError==OE_Ignore || pIdx->onError==OE_Fail ){
102041          ipkTop = sqlite3VdbeAddOp0(v, OP_Goto);
102042          break;
102043        }
102044      }
102045    }
102046
102047    /* Check to see if the new rowid already exists in the table.  Skip
102048    ** the following conflict logic if it does not. */
102049    sqlite3VdbeAddOp3(v, OP_NotExists, iDataCur, addrRowidOk, regNewData);
102050    VdbeCoverage(v);
102051
102052    /* Generate code that deals with a rowid collision */
102053    switch( onError ){
102054      default: {
102055        onError = OE_Abort;
102056        /* Fall thru into the next case */
102057      }
102058      case OE_Rollback:
102059      case OE_Abort:
102060      case OE_Fail: {
102061        sqlite3RowidConstraint(pParse, onError, pTab);
102062        break;
102063      }
102064      case OE_Replace: {
102065        /* If there are DELETE triggers on this table and the
102066        ** recursive-triggers flag is set, call GenerateRowDelete() to
102067        ** remove the conflicting row from the table. This will fire
102068        ** the triggers and remove both the table and index b-tree entries.
102069        **
102070        ** Otherwise, if there are no triggers or the recursive-triggers
102071        ** flag is not set, but the table has one or more indexes, call
102072        ** GenerateRowIndexDelete(). This removes the index b-tree entries
102073        ** only. The table b-tree entry will be replaced by the new entry
102074        ** when it is inserted.
102075        **
102076        ** If either GenerateRowDelete() or GenerateRowIndexDelete() is called,
102077        ** also invoke MultiWrite() to indicate that this VDBE may require
102078        ** statement rollback (if the statement is aborted after the delete
102079        ** takes place). Earlier versions called sqlite3MultiWrite() regardless,
102080        ** but being more selective here allows statements like:
102081        **
102082        **   REPLACE INTO t(rowid) VALUES($newrowid)
102083        **
102084        ** to run without a statement journal if there are no indexes on the
102085        ** table.
102086        */
102087        Trigger *pTrigger = 0;
102088        if( db->flags&SQLITE_RecTriggers ){
102089          pTrigger = sqlite3TriggersExist(pParse, pTab, TK_DELETE, 0, 0);
102090        }
102091        if( pTrigger || sqlite3FkRequired(pParse, pTab, 0, 0) ){
102092          sqlite3MultiWrite(pParse);
102093          sqlite3GenerateRowDelete(pParse, pTab, pTrigger, iDataCur, iIdxCur,
102094                                   regNewData, 1, 0, OE_Replace, 1);
102095        }else if( pTab->pIndex ){
102096          sqlite3MultiWrite(pParse);
102097          sqlite3GenerateRowIndexDelete(pParse, pTab, iDataCur, iIdxCur, 0);
102098        }
102099        seenReplace = 1;
102100        break;
102101      }
102102      case OE_Ignore: {
102103        /*assert( seenReplace==0 );*/
102104        sqlite3VdbeAddOp2(v, OP_Goto, 0, ignoreDest);
102105        break;
102106      }
102107    }
102108    sqlite3VdbeResolveLabel(v, addrRowidOk);
102109    if( ipkTop ){
102110      ipkBottom = sqlite3VdbeAddOp0(v, OP_Goto);
102111      sqlite3VdbeJumpHere(v, ipkTop);
102112    }
102113  }
102114
102115  /* Test all UNIQUE constraints by creating entries for each UNIQUE
102116  ** index and making sure that duplicate entries do not already exist.
102117  ** Compute the revised record entries for indices as we go.
102118  **
102119  ** This loop also handles the case of the PRIMARY KEY index for a
102120  ** WITHOUT ROWID table.
102121  */
102122  for(ix=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, ix++){
102123    int regIdx;          /* Range of registers hold conent for pIdx */
102124    int regR;            /* Range of registers holding conflicting PK */
102125    int iThisCur;        /* Cursor for this UNIQUE index */
102126    int addrUniqueOk;    /* Jump here if the UNIQUE constraint is satisfied */
102127
102128    if( aRegIdx[ix]==0 ) continue;  /* Skip indices that do not change */
102129    if( bAffinityDone==0 ){
102130      sqlite3TableAffinity(v, pTab, regNewData+1);
102131      bAffinityDone = 1;
102132    }
102133    iThisCur = iIdxCur+ix;
102134    addrUniqueOk = sqlite3VdbeMakeLabel(v);
102135
102136    /* Skip partial indices for which the WHERE clause is not true */
102137    if( pIdx->pPartIdxWhere ){
102138      sqlite3VdbeAddOp2(v, OP_Null, 0, aRegIdx[ix]);
102139      pParse->ckBase = regNewData+1;
102140      sqlite3ExprIfFalseDup(pParse, pIdx->pPartIdxWhere, addrUniqueOk,
102141                            SQLITE_JUMPIFNULL);
102142      pParse->ckBase = 0;
102143    }
102144
102145    /* Create a record for this index entry as it should appear after
102146    ** the insert or update.  Store that record in the aRegIdx[ix] register
102147    */
102148    regIdx = sqlite3GetTempRange(pParse, pIdx->nColumn);
102149    for(i=0; i<pIdx->nColumn; i++){
102150      int iField = pIdx->aiColumn[i];
102151      int x;
102152      if( iField<0 || iField==pTab->iPKey ){
102153        if( regRowid==regIdx+i ) continue; /* ROWID already in regIdx+i */
102154        x = regNewData;
102155        regRowid =  pIdx->pPartIdxWhere ? -1 : regIdx+i;
102156      }else{
102157        x = iField + regNewData + 1;
102158      }
102159      sqlite3VdbeAddOp2(v, OP_SCopy, x, regIdx+i);
102160      VdbeComment((v, "%s", iField<0 ? "rowid" : pTab->aCol[iField].zName));
102161    }
102162    sqlite3VdbeAddOp3(v, OP_MakeRecord, regIdx, pIdx->nColumn, aRegIdx[ix]);
102163    VdbeComment((v, "for %s", pIdx->zName));
102164    sqlite3ExprCacheAffinityChange(pParse, regIdx, pIdx->nColumn);
102165
102166    /* In an UPDATE operation, if this index is the PRIMARY KEY index
102167    ** of a WITHOUT ROWID table and there has been no change the
102168    ** primary key, then no collision is possible.  The collision detection
102169    ** logic below can all be skipped. */
102170    if( isUpdate && pPk==pIdx && pkChng==0 ){
102171      sqlite3VdbeResolveLabel(v, addrUniqueOk);
102172      continue;
102173    }
102174
102175    /* Find out what action to take in case there is a uniqueness conflict */
102176    onError = pIdx->onError;
102177    if( onError==OE_None ){
102178      sqlite3ReleaseTempRange(pParse, regIdx, pIdx->nColumn);
102179      sqlite3VdbeResolveLabel(v, addrUniqueOk);
102180      continue;  /* pIdx is not a UNIQUE index */
102181    }
102182    if( overrideError!=OE_Default ){
102183      onError = overrideError;
102184    }else if( onError==OE_Default ){
102185      onError = OE_Abort;
102186    }
102187
102188    /* Check to see if the new index entry will be unique */
102189    sqlite3VdbeAddOp4Int(v, OP_NoConflict, iThisCur, addrUniqueOk,
102190                         regIdx, pIdx->nKeyCol); VdbeCoverage(v);
102191
102192    /* Generate code to handle collisions */
102193    regR = (pIdx==pPk) ? regIdx : sqlite3GetTempRange(pParse, nPkField);
102194    if( isUpdate || onError==OE_Replace ){
102195      if( HasRowid(pTab) ){
102196        sqlite3VdbeAddOp2(v, OP_IdxRowid, iThisCur, regR);
102197        /* Conflict only if the rowid of the existing index entry
102198        ** is different from old-rowid */
102199        if( isUpdate ){
102200          sqlite3VdbeAddOp3(v, OP_Eq, regR, addrUniqueOk, regOldData);
102201          sqlite3VdbeChangeP5(v, SQLITE_NOTNULL);
102202          VdbeCoverage(v);
102203        }
102204      }else{
102205        int x;
102206        /* Extract the PRIMARY KEY from the end of the index entry and
102207        ** store it in registers regR..regR+nPk-1 */
102208        if( pIdx!=pPk ){
102209          for(i=0; i<pPk->nKeyCol; i++){
102210            x = sqlite3ColumnOfIndex(pIdx, pPk->aiColumn[i]);
102211            sqlite3VdbeAddOp3(v, OP_Column, iThisCur, x, regR+i);
102212            VdbeComment((v, "%s.%s", pTab->zName,
102213                         pTab->aCol[pPk->aiColumn[i]].zName));
102214          }
102215        }
102216        if( isUpdate ){
102217          /* If currently processing the PRIMARY KEY of a WITHOUT ROWID
102218          ** table, only conflict if the new PRIMARY KEY values are actually
102219          ** different from the old.
102220          **
102221          ** For a UNIQUE index, only conflict if the PRIMARY KEY values
102222          ** of the matched index row are different from the original PRIMARY
102223          ** KEY values of this row before the update.  */
102224          int addrJump = sqlite3VdbeCurrentAddr(v)+pPk->nKeyCol;
102225          int op = OP_Ne;
102226          int regCmp = (IsPrimaryKeyIndex(pIdx) ? regIdx : regR);
102227
102228          for(i=0; i<pPk->nKeyCol; i++){
102229            char *p4 = (char*)sqlite3LocateCollSeq(pParse, pPk->azColl[i]);
102230            x = pPk->aiColumn[i];
102231            if( i==(pPk->nKeyCol-1) ){
102232              addrJump = addrUniqueOk;
102233              op = OP_Eq;
102234            }
102235            sqlite3VdbeAddOp4(v, op,
102236                regOldData+1+x, addrJump, regCmp+i, p4, P4_COLLSEQ
102237            );
102238            sqlite3VdbeChangeP5(v, SQLITE_NOTNULL);
102239            VdbeCoverageIf(v, op==OP_Eq);
102240            VdbeCoverageIf(v, op==OP_Ne);
102241          }
102242        }
102243      }
102244    }
102245
102246    /* Generate code that executes if the new index entry is not unique */
102247    assert( onError==OE_Rollback || onError==OE_Abort || onError==OE_Fail
102248        || onError==OE_Ignore || onError==OE_Replace );
102249    switch( onError ){
102250      case OE_Rollback:
102251      case OE_Abort:
102252      case OE_Fail: {
102253        sqlite3UniqueConstraint(pParse, onError, pIdx);
102254        break;
102255      }
102256      case OE_Ignore: {
102257        sqlite3VdbeAddOp2(v, OP_Goto, 0, ignoreDest);
102258        break;
102259      }
102260      default: {
102261        Trigger *pTrigger = 0;
102262        assert( onError==OE_Replace );
102263        sqlite3MultiWrite(pParse);
102264        if( db->flags&SQLITE_RecTriggers ){
102265          pTrigger = sqlite3TriggersExist(pParse, pTab, TK_DELETE, 0, 0);
102266        }
102267        sqlite3GenerateRowDelete(pParse, pTab, pTrigger, iDataCur, iIdxCur,
102268                                 regR, nPkField, 0, OE_Replace, pIdx==pPk);
102269        seenReplace = 1;
102270        break;
102271      }
102272    }
102273    sqlite3VdbeResolveLabel(v, addrUniqueOk);
102274    sqlite3ReleaseTempRange(pParse, regIdx, pIdx->nColumn);
102275    if( regR!=regIdx ) sqlite3ReleaseTempRange(pParse, regR, nPkField);
102276  }
102277  if( ipkTop ){
102278    sqlite3VdbeAddOp2(v, OP_Goto, 0, ipkTop+1);
102279    sqlite3VdbeJumpHere(v, ipkBottom);
102280  }
102281
102282  *pbMayReplace = seenReplace;
102283  VdbeModuleComment((v, "END: GenCnstCks(%d)", seenReplace));
102284}
102285
102286/*
102287** This routine generates code to finish the INSERT or UPDATE operation
102288** that was started by a prior call to sqlite3GenerateConstraintChecks.
102289** A consecutive range of registers starting at regNewData contains the
102290** rowid and the content to be inserted.
102291**
102292** The arguments to this routine should be the same as the first six
102293** arguments to sqlite3GenerateConstraintChecks.
102294*/
102295SQLITE_PRIVATE void sqlite3CompleteInsertion(
102296  Parse *pParse,      /* The parser context */
102297  Table *pTab,        /* the table into which we are inserting */
102298  int iDataCur,       /* Cursor of the canonical data source */
102299  int iIdxCur,        /* First index cursor */
102300  int regNewData,     /* Range of content */
102301  int *aRegIdx,       /* Register used by each index.  0 for unused indices */
102302  int isUpdate,       /* True for UPDATE, False for INSERT */
102303  int appendBias,     /* True if this is likely to be an append */
102304  int useSeekResult   /* True to set the USESEEKRESULT flag on OP_[Idx]Insert */
102305){
102306  Vdbe *v;            /* Prepared statements under construction */
102307  Index *pIdx;        /* An index being inserted or updated */
102308  u8 pik_flags;       /* flag values passed to the btree insert */
102309  int regData;        /* Content registers (after the rowid) */
102310  int regRec;         /* Register holding assembled record for the table */
102311  int i;              /* Loop counter */
102312  u8 bAffinityDone = 0; /* True if OP_Affinity has been run already */
102313
102314  v = sqlite3GetVdbe(pParse);
102315  assert( v!=0 );
102316  assert( pTab->pSelect==0 );  /* This table is not a VIEW */
102317  for(i=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){
102318    if( aRegIdx[i]==0 ) continue;
102319    bAffinityDone = 1;
102320    if( pIdx->pPartIdxWhere ){
102321      sqlite3VdbeAddOp2(v, OP_IsNull, aRegIdx[i], sqlite3VdbeCurrentAddr(v)+2);
102322      VdbeCoverage(v);
102323    }
102324    sqlite3VdbeAddOp2(v, OP_IdxInsert, iIdxCur+i, aRegIdx[i]);
102325    pik_flags = 0;
102326    if( useSeekResult ) pik_flags = OPFLAG_USESEEKRESULT;
102327    if( IsPrimaryKeyIndex(pIdx) && !HasRowid(pTab) ){
102328      assert( pParse->nested==0 );
102329      pik_flags |= OPFLAG_NCHANGE;
102330    }
102331    if( pik_flags )  sqlite3VdbeChangeP5(v, pik_flags);
102332  }
102333  if( !HasRowid(pTab) ) return;
102334  regData = regNewData + 1;
102335  regRec = sqlite3GetTempReg(pParse);
102336  sqlite3VdbeAddOp3(v, OP_MakeRecord, regData, pTab->nCol, regRec);
102337  if( !bAffinityDone ) sqlite3TableAffinity(v, pTab, 0);
102338  sqlite3ExprCacheAffinityChange(pParse, regData, pTab->nCol);
102339  if( pParse->nested ){
102340    pik_flags = 0;
102341  }else{
102342    pik_flags = OPFLAG_NCHANGE;
102343    pik_flags |= (isUpdate?OPFLAG_ISUPDATE:OPFLAG_LASTROWID);
102344  }
102345  if( appendBias ){
102346    pik_flags |= OPFLAG_APPEND;
102347  }
102348  if( useSeekResult ){
102349    pik_flags |= OPFLAG_USESEEKRESULT;
102350  }
102351  sqlite3VdbeAddOp3(v, OP_Insert, iDataCur, regRec, regNewData);
102352  if( !pParse->nested ){
102353    sqlite3VdbeChangeP4(v, -1, pTab->zName, P4_TRANSIENT);
102354  }
102355  sqlite3VdbeChangeP5(v, pik_flags);
102356}
102357
102358/*
102359** Allocate cursors for the pTab table and all its indices and generate
102360** code to open and initialized those cursors.
102361**
102362** The cursor for the object that contains the complete data (normally
102363** the table itself, but the PRIMARY KEY index in the case of a WITHOUT
102364** ROWID table) is returned in *piDataCur.  The first index cursor is
102365** returned in *piIdxCur.  The number of indices is returned.
102366**
102367** Use iBase as the first cursor (either the *piDataCur for rowid tables
102368** or the first index for WITHOUT ROWID tables) if it is non-negative.
102369** If iBase is negative, then allocate the next available cursor.
102370**
102371** For a rowid table, *piDataCur will be exactly one less than *piIdxCur.
102372** For a WITHOUT ROWID table, *piDataCur will be somewhere in the range
102373** of *piIdxCurs, depending on where the PRIMARY KEY index appears on the
102374** pTab->pIndex list.
102375**
102376** If pTab is a virtual table, then this routine is a no-op and the
102377** *piDataCur and *piIdxCur values are left uninitialized.
102378*/
102379SQLITE_PRIVATE int sqlite3OpenTableAndIndices(
102380  Parse *pParse,   /* Parsing context */
102381  Table *pTab,     /* Table to be opened */
102382  int op,          /* OP_OpenRead or OP_OpenWrite */
102383  int iBase,       /* Use this for the table cursor, if there is one */
102384  u8 *aToOpen,     /* If not NULL: boolean for each table and index */
102385  int *piDataCur,  /* Write the database source cursor number here */
102386  int *piIdxCur    /* Write the first index cursor number here */
102387){
102388  int i;
102389  int iDb;
102390  int iDataCur;
102391  Index *pIdx;
102392  Vdbe *v;
102393
102394  assert( op==OP_OpenRead || op==OP_OpenWrite );
102395  if( IsVirtual(pTab) ){
102396    /* This routine is a no-op for virtual tables. Leave the output
102397    ** variables *piDataCur and *piIdxCur uninitialized so that valgrind
102398    ** can detect if they are used by mistake in the caller. */
102399    return 0;
102400  }
102401  iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
102402  v = sqlite3GetVdbe(pParse);
102403  assert( v!=0 );
102404  if( iBase<0 ) iBase = pParse->nTab;
102405  iDataCur = iBase++;
102406  if( piDataCur ) *piDataCur = iDataCur;
102407  if( HasRowid(pTab) && (aToOpen==0 || aToOpen[0]) ){
102408    sqlite3OpenTable(pParse, iDataCur, iDb, pTab, op);
102409  }else{
102410    sqlite3TableLock(pParse, iDb, pTab->tnum, op==OP_OpenWrite, pTab->zName);
102411  }
102412  if( piIdxCur ) *piIdxCur = iBase;
102413  for(i=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){
102414    int iIdxCur = iBase++;
102415    assert( pIdx->pSchema==pTab->pSchema );
102416    if( IsPrimaryKeyIndex(pIdx) && !HasRowid(pTab) && piDataCur ){
102417      *piDataCur = iIdxCur;
102418    }
102419    if( aToOpen==0 || aToOpen[i+1] ){
102420      sqlite3VdbeAddOp3(v, op, iIdxCur, pIdx->tnum, iDb);
102421      sqlite3VdbeSetP4KeyInfo(pParse, pIdx);
102422      VdbeComment((v, "%s", pIdx->zName));
102423    }
102424  }
102425  if( iBase>pParse->nTab ) pParse->nTab = iBase;
102426  return i;
102427}
102428
102429
102430#ifdef SQLITE_TEST
102431/*
102432** The following global variable is incremented whenever the
102433** transfer optimization is used.  This is used for testing
102434** purposes only - to make sure the transfer optimization really
102435** is happening when it is supposed to.
102436*/
102437SQLITE_API int sqlite3_xferopt_count;
102438#endif /* SQLITE_TEST */
102439
102440
102441#ifndef SQLITE_OMIT_XFER_OPT
102442/*
102443** Check to collation names to see if they are compatible.
102444*/
102445static int xferCompatibleCollation(const char *z1, const char *z2){
102446  if( z1==0 ){
102447    return z2==0;
102448  }
102449  if( z2==0 ){
102450    return 0;
102451  }
102452  return sqlite3StrICmp(z1, z2)==0;
102453}
102454
102455
102456/*
102457** Check to see if index pSrc is compatible as a source of data
102458** for index pDest in an insert transfer optimization.  The rules
102459** for a compatible index:
102460**
102461**    *   The index is over the same set of columns
102462**    *   The same DESC and ASC markings occurs on all columns
102463**    *   The same onError processing (OE_Abort, OE_Ignore, etc)
102464**    *   The same collating sequence on each column
102465**    *   The index has the exact same WHERE clause
102466*/
102467static int xferCompatibleIndex(Index *pDest, Index *pSrc){
102468  int i;
102469  assert( pDest && pSrc );
102470  assert( pDest->pTable!=pSrc->pTable );
102471  if( pDest->nKeyCol!=pSrc->nKeyCol ){
102472    return 0;   /* Different number of columns */
102473  }
102474  if( pDest->onError!=pSrc->onError ){
102475    return 0;   /* Different conflict resolution strategies */
102476  }
102477  for(i=0; i<pSrc->nKeyCol; i++){
102478    if( pSrc->aiColumn[i]!=pDest->aiColumn[i] ){
102479      return 0;   /* Different columns indexed */
102480    }
102481    if( pSrc->aSortOrder[i]!=pDest->aSortOrder[i] ){
102482      return 0;   /* Different sort orders */
102483    }
102484    if( !xferCompatibleCollation(pSrc->azColl[i],pDest->azColl[i]) ){
102485      return 0;   /* Different collating sequences */
102486    }
102487  }
102488  if( sqlite3ExprCompare(pSrc->pPartIdxWhere, pDest->pPartIdxWhere, -1) ){
102489    return 0;     /* Different WHERE clauses */
102490  }
102491
102492  /* If no test above fails then the indices must be compatible */
102493  return 1;
102494}
102495
102496/*
102497** Attempt the transfer optimization on INSERTs of the form
102498**
102499**     INSERT INTO tab1 SELECT * FROM tab2;
102500**
102501** The xfer optimization transfers raw records from tab2 over to tab1.
102502** Columns are not decoded and reassembled, which greatly improves
102503** performance.  Raw index records are transferred in the same way.
102504**
102505** The xfer optimization is only attempted if tab1 and tab2 are compatible.
102506** There are lots of rules for determining compatibility - see comments
102507** embedded in the code for details.
102508**
102509** This routine returns TRUE if the optimization is guaranteed to be used.
102510** Sometimes the xfer optimization will only work if the destination table
102511** is empty - a factor that can only be determined at run-time.  In that
102512** case, this routine generates code for the xfer optimization but also
102513** does a test to see if the destination table is empty and jumps over the
102514** xfer optimization code if the test fails.  In that case, this routine
102515** returns FALSE so that the caller will know to go ahead and generate
102516** an unoptimized transfer.  This routine also returns FALSE if there
102517** is no chance that the xfer optimization can be applied.
102518**
102519** This optimization is particularly useful at making VACUUM run faster.
102520*/
102521static int xferOptimization(
102522  Parse *pParse,        /* Parser context */
102523  Table *pDest,         /* The table we are inserting into */
102524  Select *pSelect,      /* A SELECT statement to use as the data source */
102525  int onError,          /* How to handle constraint errors */
102526  int iDbDest           /* The database of pDest */
102527){
102528  sqlite3 *db = pParse->db;
102529  ExprList *pEList;                /* The result set of the SELECT */
102530  Table *pSrc;                     /* The table in the FROM clause of SELECT */
102531  Index *pSrcIdx, *pDestIdx;       /* Source and destination indices */
102532  struct SrcList_item *pItem;      /* An element of pSelect->pSrc */
102533  int i;                           /* Loop counter */
102534  int iDbSrc;                      /* The database of pSrc */
102535  int iSrc, iDest;                 /* Cursors from source and destination */
102536  int addr1, addr2;                /* Loop addresses */
102537  int emptyDestTest = 0;           /* Address of test for empty pDest */
102538  int emptySrcTest = 0;            /* Address of test for empty pSrc */
102539  Vdbe *v;                         /* The VDBE we are building */
102540  int regAutoinc;                  /* Memory register used by AUTOINC */
102541  int destHasUniqueIdx = 0;        /* True if pDest has a UNIQUE index */
102542  int regData, regRowid;           /* Registers holding data and rowid */
102543
102544  if( pSelect==0 ){
102545    return 0;   /* Must be of the form  INSERT INTO ... SELECT ... */
102546  }
102547  if( pParse->pWith || pSelect->pWith ){
102548    /* Do not attempt to process this query if there are an WITH clauses
102549    ** attached to it. Proceeding may generate a false "no such table: xxx"
102550    ** error if pSelect reads from a CTE named "xxx".  */
102551    return 0;
102552  }
102553  if( sqlite3TriggerList(pParse, pDest) ){
102554    return 0;   /* tab1 must not have triggers */
102555  }
102556#ifndef SQLITE_OMIT_VIRTUALTABLE
102557  if( pDest->tabFlags & TF_Virtual ){
102558    return 0;   /* tab1 must not be a virtual table */
102559  }
102560#endif
102561  if( onError==OE_Default ){
102562    if( pDest->iPKey>=0 ) onError = pDest->keyConf;
102563    if( onError==OE_Default ) onError = OE_Abort;
102564  }
102565  assert(pSelect->pSrc);   /* allocated even if there is no FROM clause */
102566  if( pSelect->pSrc->nSrc!=1 ){
102567    return 0;   /* FROM clause must have exactly one term */
102568  }
102569  if( pSelect->pSrc->a[0].pSelect ){
102570    return 0;   /* FROM clause cannot contain a subquery */
102571  }
102572  if( pSelect->pWhere ){
102573    return 0;   /* SELECT may not have a WHERE clause */
102574  }
102575  if( pSelect->pOrderBy ){
102576    return 0;   /* SELECT may not have an ORDER BY clause */
102577  }
102578  /* Do not need to test for a HAVING clause.  If HAVING is present but
102579  ** there is no ORDER BY, we will get an error. */
102580  if( pSelect->pGroupBy ){
102581    return 0;   /* SELECT may not have a GROUP BY clause */
102582  }
102583  if( pSelect->pLimit ){
102584    return 0;   /* SELECT may not have a LIMIT clause */
102585  }
102586  assert( pSelect->pOffset==0 );  /* Must be so if pLimit==0 */
102587  if( pSelect->pPrior ){
102588    return 0;   /* SELECT may not be a compound query */
102589  }
102590  if( pSelect->selFlags & SF_Distinct ){
102591    return 0;   /* SELECT may not be DISTINCT */
102592  }
102593  pEList = pSelect->pEList;
102594  assert( pEList!=0 );
102595  if( pEList->nExpr!=1 ){
102596    return 0;   /* The result set must have exactly one column */
102597  }
102598  assert( pEList->a[0].pExpr );
102599  if( pEList->a[0].pExpr->op!=TK_ALL ){
102600    return 0;   /* The result set must be the special operator "*" */
102601  }
102602
102603  /* At this point we have established that the statement is of the
102604  ** correct syntactic form to participate in this optimization.  Now
102605  ** we have to check the semantics.
102606  */
102607  pItem = pSelect->pSrc->a;
102608  pSrc = sqlite3LocateTableItem(pParse, 0, pItem);
102609  if( pSrc==0 ){
102610    return 0;   /* FROM clause does not contain a real table */
102611  }
102612  if( pSrc==pDest ){
102613    return 0;   /* tab1 and tab2 may not be the same table */
102614  }
102615  if( HasRowid(pDest)!=HasRowid(pSrc) ){
102616    return 0;   /* source and destination must both be WITHOUT ROWID or not */
102617  }
102618#ifndef SQLITE_OMIT_VIRTUALTABLE
102619  if( pSrc->tabFlags & TF_Virtual ){
102620    return 0;   /* tab2 must not be a virtual table */
102621  }
102622#endif
102623  if( pSrc->pSelect ){
102624    return 0;   /* tab2 may not be a view */
102625  }
102626  if( pDest->nCol!=pSrc->nCol ){
102627    return 0;   /* Number of columns must be the same in tab1 and tab2 */
102628  }
102629  if( pDest->iPKey!=pSrc->iPKey ){
102630    return 0;   /* Both tables must have the same INTEGER PRIMARY KEY */
102631  }
102632  for(i=0; i<pDest->nCol; i++){
102633    Column *pDestCol = &pDest->aCol[i];
102634    Column *pSrcCol = &pSrc->aCol[i];
102635    if( pDestCol->affinity!=pSrcCol->affinity ){
102636      return 0;    /* Affinity must be the same on all columns */
102637    }
102638    if( !xferCompatibleCollation(pDestCol->zColl, pSrcCol->zColl) ){
102639      return 0;    /* Collating sequence must be the same on all columns */
102640    }
102641    if( pDestCol->notNull && !pSrcCol->notNull ){
102642      return 0;    /* tab2 must be NOT NULL if tab1 is */
102643    }
102644    /* Default values for second and subsequent columns need to match. */
102645    if( i>0
102646     && ((pDestCol->zDflt==0)!=(pSrcCol->zDflt==0)
102647         || (pDestCol->zDflt && strcmp(pDestCol->zDflt, pSrcCol->zDflt)!=0))
102648    ){
102649      return 0;    /* Default values must be the same for all columns */
102650    }
102651  }
102652  for(pDestIdx=pDest->pIndex; pDestIdx; pDestIdx=pDestIdx->pNext){
102653    if( IsUniqueIndex(pDestIdx) ){
102654      destHasUniqueIdx = 1;
102655    }
102656    for(pSrcIdx=pSrc->pIndex; pSrcIdx; pSrcIdx=pSrcIdx->pNext){
102657      if( xferCompatibleIndex(pDestIdx, pSrcIdx) ) break;
102658    }
102659    if( pSrcIdx==0 ){
102660      return 0;    /* pDestIdx has no corresponding index in pSrc */
102661    }
102662  }
102663#ifndef SQLITE_OMIT_CHECK
102664  if( pDest->pCheck && sqlite3ExprListCompare(pSrc->pCheck,pDest->pCheck,-1) ){
102665    return 0;   /* Tables have different CHECK constraints.  Ticket #2252 */
102666  }
102667#endif
102668#ifndef SQLITE_OMIT_FOREIGN_KEY
102669  /* Disallow the transfer optimization if the destination table constains
102670  ** any foreign key constraints.  This is more restrictive than necessary.
102671  ** But the main beneficiary of the transfer optimization is the VACUUM
102672  ** command, and the VACUUM command disables foreign key constraints.  So
102673  ** the extra complication to make this rule less restrictive is probably
102674  ** not worth the effort.  Ticket [6284df89debdfa61db8073e062908af0c9b6118e]
102675  */
102676  if( (db->flags & SQLITE_ForeignKeys)!=0 && pDest->pFKey!=0 ){
102677    return 0;
102678  }
102679#endif
102680  if( (db->flags & SQLITE_CountRows)!=0 ){
102681    return 0;  /* xfer opt does not play well with PRAGMA count_changes */
102682  }
102683
102684  /* If we get this far, it means that the xfer optimization is at
102685  ** least a possibility, though it might only work if the destination
102686  ** table (tab1) is initially empty.
102687  */
102688#ifdef SQLITE_TEST
102689  sqlite3_xferopt_count++;
102690#endif
102691  iDbSrc = sqlite3SchemaToIndex(db, pSrc->pSchema);
102692  v = sqlite3GetVdbe(pParse);
102693  sqlite3CodeVerifySchema(pParse, iDbSrc);
102694  iSrc = pParse->nTab++;
102695  iDest = pParse->nTab++;
102696  regAutoinc = autoIncBegin(pParse, iDbDest, pDest);
102697  regData = sqlite3GetTempReg(pParse);
102698  regRowid = sqlite3GetTempReg(pParse);
102699  sqlite3OpenTable(pParse, iDest, iDbDest, pDest, OP_OpenWrite);
102700  assert( HasRowid(pDest) || destHasUniqueIdx );
102701  if( (db->flags & SQLITE_Vacuum)==0 && (
102702      (pDest->iPKey<0 && pDest->pIndex!=0)          /* (1) */
102703   || destHasUniqueIdx                              /* (2) */
102704   || (onError!=OE_Abort && onError!=OE_Rollback)   /* (3) */
102705  )){
102706    /* In some circumstances, we are able to run the xfer optimization
102707    ** only if the destination table is initially empty. Unless the
102708    ** SQLITE_Vacuum flag is set, this block generates code to make
102709    ** that determination. If SQLITE_Vacuum is set, then the destination
102710    ** table is always empty.
102711    **
102712    ** Conditions under which the destination must be empty:
102713    **
102714    ** (1) There is no INTEGER PRIMARY KEY but there are indices.
102715    **     (If the destination is not initially empty, the rowid fields
102716    **     of index entries might need to change.)
102717    **
102718    ** (2) The destination has a unique index.  (The xfer optimization
102719    **     is unable to test uniqueness.)
102720    **
102721    ** (3) onError is something other than OE_Abort and OE_Rollback.
102722    */
102723    addr1 = sqlite3VdbeAddOp2(v, OP_Rewind, iDest, 0); VdbeCoverage(v);
102724    emptyDestTest = sqlite3VdbeAddOp2(v, OP_Goto, 0, 0);
102725    sqlite3VdbeJumpHere(v, addr1);
102726  }
102727  if( HasRowid(pSrc) ){
102728    sqlite3OpenTable(pParse, iSrc, iDbSrc, pSrc, OP_OpenRead);
102729    emptySrcTest = sqlite3VdbeAddOp2(v, OP_Rewind, iSrc, 0); VdbeCoverage(v);
102730    if( pDest->iPKey>=0 ){
102731      addr1 = sqlite3VdbeAddOp2(v, OP_Rowid, iSrc, regRowid);
102732      addr2 = sqlite3VdbeAddOp3(v, OP_NotExists, iDest, 0, regRowid);
102733      VdbeCoverage(v);
102734      sqlite3RowidConstraint(pParse, onError, pDest);
102735      sqlite3VdbeJumpHere(v, addr2);
102736      autoIncStep(pParse, regAutoinc, regRowid);
102737    }else if( pDest->pIndex==0 ){
102738      addr1 = sqlite3VdbeAddOp2(v, OP_NewRowid, iDest, regRowid);
102739    }else{
102740      addr1 = sqlite3VdbeAddOp2(v, OP_Rowid, iSrc, regRowid);
102741      assert( (pDest->tabFlags & TF_Autoincrement)==0 );
102742    }
102743    sqlite3VdbeAddOp2(v, OP_RowData, iSrc, regData);
102744    sqlite3VdbeAddOp3(v, OP_Insert, iDest, regData, regRowid);
102745    sqlite3VdbeChangeP5(v, OPFLAG_NCHANGE|OPFLAG_LASTROWID|OPFLAG_APPEND);
102746    sqlite3VdbeChangeP4(v, -1, pDest->zName, 0);
102747    sqlite3VdbeAddOp2(v, OP_Next, iSrc, addr1); VdbeCoverage(v);
102748    sqlite3VdbeAddOp2(v, OP_Close, iSrc, 0);
102749    sqlite3VdbeAddOp2(v, OP_Close, iDest, 0);
102750  }else{
102751    sqlite3TableLock(pParse, iDbDest, pDest->tnum, 1, pDest->zName);
102752    sqlite3TableLock(pParse, iDbSrc, pSrc->tnum, 0, pSrc->zName);
102753  }
102754  for(pDestIdx=pDest->pIndex; pDestIdx; pDestIdx=pDestIdx->pNext){
102755    u8 idxInsFlags = 0;
102756    for(pSrcIdx=pSrc->pIndex; ALWAYS(pSrcIdx); pSrcIdx=pSrcIdx->pNext){
102757      if( xferCompatibleIndex(pDestIdx, pSrcIdx) ) break;
102758    }
102759    assert( pSrcIdx );
102760    sqlite3VdbeAddOp3(v, OP_OpenRead, iSrc, pSrcIdx->tnum, iDbSrc);
102761    sqlite3VdbeSetP4KeyInfo(pParse, pSrcIdx);
102762    VdbeComment((v, "%s", pSrcIdx->zName));
102763    sqlite3VdbeAddOp3(v, OP_OpenWrite, iDest, pDestIdx->tnum, iDbDest);
102764    sqlite3VdbeSetP4KeyInfo(pParse, pDestIdx);
102765    sqlite3VdbeChangeP5(v, OPFLAG_BULKCSR);
102766    VdbeComment((v, "%s", pDestIdx->zName));
102767    addr1 = sqlite3VdbeAddOp2(v, OP_Rewind, iSrc, 0); VdbeCoverage(v);
102768    sqlite3VdbeAddOp2(v, OP_RowKey, iSrc, regData);
102769    if( db->flags & SQLITE_Vacuum ){
102770      /* This INSERT command is part of a VACUUM operation, which guarantees
102771      ** that the destination table is empty. If all indexed columns use
102772      ** collation sequence BINARY, then it can also be assumed that the
102773      ** index will be populated by inserting keys in strictly sorted
102774      ** order. In this case, instead of seeking within the b-tree as part
102775      ** of every OP_IdxInsert opcode, an OP_Last is added before the
102776      ** OP_IdxInsert to seek to the point within the b-tree where each key
102777      ** should be inserted. This is faster.
102778      **
102779      ** If any of the indexed columns use a collation sequence other than
102780      ** BINARY, this optimization is disabled. This is because the user
102781      ** might change the definition of a collation sequence and then run
102782      ** a VACUUM command. In that case keys may not be written in strictly
102783      ** sorted order.  */
102784      for(i=0; i<pSrcIdx->nColumn; i++){
102785        char *zColl = pSrcIdx->azColl[i];
102786        assert( zColl!=0 );
102787        if( sqlite3_stricmp("BINARY", zColl) ) break;
102788      }
102789      if( i==pSrcIdx->nColumn ){
102790        idxInsFlags = OPFLAG_USESEEKRESULT;
102791        sqlite3VdbeAddOp3(v, OP_Last, iDest, 0, -1);
102792      }
102793    }
102794    if( !HasRowid(pSrc) && pDestIdx->idxType==2 ){
102795      idxInsFlags |= OPFLAG_NCHANGE;
102796    }
102797    sqlite3VdbeAddOp3(v, OP_IdxInsert, iDest, regData, 1);
102798    sqlite3VdbeChangeP5(v, idxInsFlags);
102799    sqlite3VdbeAddOp2(v, OP_Next, iSrc, addr1+1); VdbeCoverage(v);
102800    sqlite3VdbeJumpHere(v, addr1);
102801    sqlite3VdbeAddOp2(v, OP_Close, iSrc, 0);
102802    sqlite3VdbeAddOp2(v, OP_Close, iDest, 0);
102803  }
102804  if( emptySrcTest ) sqlite3VdbeJumpHere(v, emptySrcTest);
102805  sqlite3ReleaseTempReg(pParse, regRowid);
102806  sqlite3ReleaseTempReg(pParse, regData);
102807  if( emptyDestTest ){
102808    sqlite3VdbeAddOp2(v, OP_Halt, SQLITE_OK, 0);
102809    sqlite3VdbeJumpHere(v, emptyDestTest);
102810    sqlite3VdbeAddOp2(v, OP_Close, iDest, 0);
102811    return 0;
102812  }else{
102813    return 1;
102814  }
102815}
102816#endif /* SQLITE_OMIT_XFER_OPT */
102817
102818/************** End of insert.c **********************************************/
102819/************** Begin file legacy.c ******************************************/
102820/*
102821** 2001 September 15
102822**
102823** The author disclaims copyright to this source code.  In place of
102824** a legal notice, here is a blessing:
102825**
102826**    May you do good and not evil.
102827**    May you find forgiveness for yourself and forgive others.
102828**    May you share freely, never taking more than you give.
102829**
102830*************************************************************************
102831** Main file for the SQLite library.  The routines in this file
102832** implement the programmer interface to the library.  Routines in
102833** other files are for internal use by SQLite and should not be
102834** accessed by users of the library.
102835*/
102836
102837/* #include "sqliteInt.h" */
102838
102839/*
102840** Execute SQL code.  Return one of the SQLITE_ success/failure
102841** codes.  Also write an error message into memory obtained from
102842** malloc() and make *pzErrMsg point to that message.
102843**
102844** If the SQL is a query, then for each row in the query result
102845** the xCallback() function is called.  pArg becomes the first
102846** argument to xCallback().  If xCallback=NULL then no callback
102847** is invoked, even for queries.
102848*/
102849SQLITE_API int SQLITE_STDCALL sqlite3_exec(
102850  sqlite3 *db,                /* The database on which the SQL executes */
102851  const char *zSql,           /* The SQL to be executed */
102852  sqlite3_callback xCallback, /* Invoke this callback routine */
102853  void *pArg,                 /* First argument to xCallback() */
102854  char **pzErrMsg             /* Write error messages here */
102855){
102856  int rc = SQLITE_OK;         /* Return code */
102857  const char *zLeftover;      /* Tail of unprocessed SQL */
102858  sqlite3_stmt *pStmt = 0;    /* The current SQL statement */
102859  char **azCols = 0;          /* Names of result columns */
102860  int callbackIsInit;         /* True if callback data is initialized */
102861
102862  if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT;
102863  if( zSql==0 ) zSql = "";
102864
102865  sqlite3_mutex_enter(db->mutex);
102866  sqlite3Error(db, SQLITE_OK);
102867  while( rc==SQLITE_OK && zSql[0] ){
102868    int nCol;
102869    char **azVals = 0;
102870
102871    pStmt = 0;
102872    rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, &zLeftover);
102873    assert( rc==SQLITE_OK || pStmt==0 );
102874    if( rc!=SQLITE_OK ){
102875      continue;
102876    }
102877    if( !pStmt ){
102878      /* this happens for a comment or white-space */
102879      zSql = zLeftover;
102880      continue;
102881    }
102882
102883    callbackIsInit = 0;
102884    nCol = sqlite3_column_count(pStmt);
102885
102886    while( 1 ){
102887      int i;
102888      rc = sqlite3_step(pStmt);
102889
102890      /* Invoke the callback function if required */
102891      if( xCallback && (SQLITE_ROW==rc ||
102892          (SQLITE_DONE==rc && !callbackIsInit
102893                           && db->flags&SQLITE_NullCallback)) ){
102894        if( !callbackIsInit ){
102895          azCols = sqlite3DbMallocZero(db, 2*nCol*sizeof(const char*) + 1);
102896          if( azCols==0 ){
102897            goto exec_out;
102898          }
102899          for(i=0; i<nCol; i++){
102900            azCols[i] = (char *)sqlite3_column_name(pStmt, i);
102901            /* sqlite3VdbeSetColName() installs column names as UTF8
102902            ** strings so there is no way for sqlite3_column_name() to fail. */
102903            assert( azCols[i]!=0 );
102904          }
102905          callbackIsInit = 1;
102906        }
102907        if( rc==SQLITE_ROW ){
102908          azVals = &azCols[nCol];
102909          for(i=0; i<nCol; i++){
102910            azVals[i] = (char *)sqlite3_column_text(pStmt, i);
102911            if( !azVals[i] && sqlite3_column_type(pStmt, i)!=SQLITE_NULL ){
102912              db->mallocFailed = 1;
102913              goto exec_out;
102914            }
102915          }
102916        }
102917        if( xCallback(pArg, nCol, azVals, azCols) ){
102918          /* EVIDENCE-OF: R-38229-40159 If the callback function to
102919          ** sqlite3_exec() returns non-zero, then sqlite3_exec() will
102920          ** return SQLITE_ABORT. */
102921          rc = SQLITE_ABORT;
102922          sqlite3VdbeFinalize((Vdbe *)pStmt);
102923          pStmt = 0;
102924          sqlite3Error(db, SQLITE_ABORT);
102925          goto exec_out;
102926        }
102927      }
102928
102929      if( rc!=SQLITE_ROW ){
102930        rc = sqlite3VdbeFinalize((Vdbe *)pStmt);
102931        pStmt = 0;
102932        zSql = zLeftover;
102933        while( sqlite3Isspace(zSql[0]) ) zSql++;
102934        break;
102935      }
102936    }
102937
102938    sqlite3DbFree(db, azCols);
102939    azCols = 0;
102940  }
102941
102942exec_out:
102943  if( pStmt ) sqlite3VdbeFinalize((Vdbe *)pStmt);
102944  sqlite3DbFree(db, azCols);
102945
102946  rc = sqlite3ApiExit(db, rc);
102947  if( rc!=SQLITE_OK && pzErrMsg ){
102948    int nErrMsg = 1 + sqlite3Strlen30(sqlite3_errmsg(db));
102949    *pzErrMsg = sqlite3Malloc(nErrMsg);
102950    if( *pzErrMsg ){
102951      memcpy(*pzErrMsg, sqlite3_errmsg(db), nErrMsg);
102952    }else{
102953      rc = SQLITE_NOMEM;
102954      sqlite3Error(db, SQLITE_NOMEM);
102955    }
102956  }else if( pzErrMsg ){
102957    *pzErrMsg = 0;
102958  }
102959
102960  assert( (rc&db->errMask)==rc );
102961  sqlite3_mutex_leave(db->mutex);
102962  return rc;
102963}
102964
102965/************** End of legacy.c **********************************************/
102966/************** Begin file loadext.c *****************************************/
102967/*
102968** 2006 June 7
102969**
102970** The author disclaims copyright to this source code.  In place of
102971** a legal notice, here is a blessing:
102972**
102973**    May you do good and not evil.
102974**    May you find forgiveness for yourself and forgive others.
102975**    May you share freely, never taking more than you give.
102976**
102977*************************************************************************
102978** This file contains code used to dynamically load extensions into
102979** the SQLite library.
102980*/
102981
102982#ifndef SQLITE_CORE
102983  #define SQLITE_CORE 1  /* Disable the API redefinition in sqlite3ext.h */
102984#endif
102985/************** Include sqlite3ext.h in the middle of loadext.c **************/
102986/************** Begin file sqlite3ext.h **************************************/
102987/*
102988** 2006 June 7
102989**
102990** The author disclaims copyright to this source code.  In place of
102991** a legal notice, here is a blessing:
102992**
102993**    May you do good and not evil.
102994**    May you find forgiveness for yourself and forgive others.
102995**    May you share freely, never taking more than you give.
102996**
102997*************************************************************************
102998** This header file defines the SQLite interface for use by
102999** shared libraries that want to be imported as extensions into
103000** an SQLite instance.  Shared libraries that intend to be loaded
103001** as extensions by SQLite should #include this file instead of
103002** sqlite3.h.
103003*/
103004#ifndef _SQLITE3EXT_H_
103005#define _SQLITE3EXT_H_
103006/* #include "sqlite3.h" */
103007
103008typedef struct sqlite3_api_routines sqlite3_api_routines;
103009
103010/*
103011** The following structure holds pointers to all of the SQLite API
103012** routines.
103013**
103014** WARNING:  In order to maintain backwards compatibility, add new
103015** interfaces to the end of this structure only.  If you insert new
103016** interfaces in the middle of this structure, then older different
103017** versions of SQLite will not be able to load each other's shared
103018** libraries!
103019*/
103020struct sqlite3_api_routines {
103021  void * (*aggregate_context)(sqlite3_context*,int nBytes);
103022  int  (*aggregate_count)(sqlite3_context*);
103023  int  (*bind_blob)(sqlite3_stmt*,int,const void*,int n,void(*)(void*));
103024  int  (*bind_double)(sqlite3_stmt*,int,double);
103025  int  (*bind_int)(sqlite3_stmt*,int,int);
103026  int  (*bind_int64)(sqlite3_stmt*,int,sqlite_int64);
103027  int  (*bind_null)(sqlite3_stmt*,int);
103028  int  (*bind_parameter_count)(sqlite3_stmt*);
103029  int  (*bind_parameter_index)(sqlite3_stmt*,const char*zName);
103030  const char * (*bind_parameter_name)(sqlite3_stmt*,int);
103031  int  (*bind_text)(sqlite3_stmt*,int,const char*,int n,void(*)(void*));
103032  int  (*bind_text16)(sqlite3_stmt*,int,const void*,int,void(*)(void*));
103033  int  (*bind_value)(sqlite3_stmt*,int,const sqlite3_value*);
103034  int  (*busy_handler)(sqlite3*,int(*)(void*,int),void*);
103035  int  (*busy_timeout)(sqlite3*,int ms);
103036  int  (*changes)(sqlite3*);
103037  int  (*close)(sqlite3*);
103038  int  (*collation_needed)(sqlite3*,void*,void(*)(void*,sqlite3*,
103039                           int eTextRep,const char*));
103040  int  (*collation_needed16)(sqlite3*,void*,void(*)(void*,sqlite3*,
103041                             int eTextRep,const void*));
103042  const void * (*column_blob)(sqlite3_stmt*,int iCol);
103043  int  (*column_bytes)(sqlite3_stmt*,int iCol);
103044  int  (*column_bytes16)(sqlite3_stmt*,int iCol);
103045  int  (*column_count)(sqlite3_stmt*pStmt);
103046  const char * (*column_database_name)(sqlite3_stmt*,int);
103047  const void * (*column_database_name16)(sqlite3_stmt*,int);
103048  const char * (*column_decltype)(sqlite3_stmt*,int i);
103049  const void * (*column_decltype16)(sqlite3_stmt*,int);
103050  double  (*column_double)(sqlite3_stmt*,int iCol);
103051  int  (*column_int)(sqlite3_stmt*,int iCol);
103052  sqlite_int64  (*column_int64)(sqlite3_stmt*,int iCol);
103053  const char * (*column_name)(sqlite3_stmt*,int);
103054  const void * (*column_name16)(sqlite3_stmt*,int);
103055  const char * (*column_origin_name)(sqlite3_stmt*,int);
103056  const void * (*column_origin_name16)(sqlite3_stmt*,int);
103057  const char * (*column_table_name)(sqlite3_stmt*,int);
103058  const void * (*column_table_name16)(sqlite3_stmt*,int);
103059  const unsigned char * (*column_text)(sqlite3_stmt*,int iCol);
103060  const void * (*column_text16)(sqlite3_stmt*,int iCol);
103061  int  (*column_type)(sqlite3_stmt*,int iCol);
103062  sqlite3_value* (*column_value)(sqlite3_stmt*,int iCol);
103063  void * (*commit_hook)(sqlite3*,int(*)(void*),void*);
103064  int  (*complete)(const char*sql);
103065  int  (*complete16)(const void*sql);
103066  int  (*create_collation)(sqlite3*,const char*,int,void*,
103067                           int(*)(void*,int,const void*,int,const void*));
103068  int  (*create_collation16)(sqlite3*,const void*,int,void*,
103069                             int(*)(void*,int,const void*,int,const void*));
103070  int  (*create_function)(sqlite3*,const char*,int,int,void*,
103071                          void (*xFunc)(sqlite3_context*,int,sqlite3_value**),
103072                          void (*xStep)(sqlite3_context*,int,sqlite3_value**),
103073                          void (*xFinal)(sqlite3_context*));
103074  int  (*create_function16)(sqlite3*,const void*,int,int,void*,
103075                            void (*xFunc)(sqlite3_context*,int,sqlite3_value**),
103076                            void (*xStep)(sqlite3_context*,int,sqlite3_value**),
103077                            void (*xFinal)(sqlite3_context*));
103078  int (*create_module)(sqlite3*,const char*,const sqlite3_module*,void*);
103079  int  (*data_count)(sqlite3_stmt*pStmt);
103080  sqlite3 * (*db_handle)(sqlite3_stmt*);
103081  int (*declare_vtab)(sqlite3*,const char*);
103082  int  (*enable_shared_cache)(int);
103083  int  (*errcode)(sqlite3*db);
103084  const char * (*errmsg)(sqlite3*);
103085  const void * (*errmsg16)(sqlite3*);
103086  int  (*exec)(sqlite3*,const char*,sqlite3_callback,void*,char**);
103087  int  (*expired)(sqlite3_stmt*);
103088  int  (*finalize)(sqlite3_stmt*pStmt);
103089  void  (*free)(void*);
103090  void  (*free_table)(char**result);
103091  int  (*get_autocommit)(sqlite3*);
103092  void * (*get_auxdata)(sqlite3_context*,int);
103093  int  (*get_table)(sqlite3*,const char*,char***,int*,int*,char**);
103094  int  (*global_recover)(void);
103095  void  (*interruptx)(sqlite3*);
103096  sqlite_int64  (*last_insert_rowid)(sqlite3*);
103097  const char * (*libversion)(void);
103098  int  (*libversion_number)(void);
103099  void *(*malloc)(int);
103100  char * (*mprintf)(const char*,...);
103101  int  (*open)(const char*,sqlite3**);
103102  int  (*open16)(const void*,sqlite3**);
103103  int  (*prepare)(sqlite3*,const char*,int,sqlite3_stmt**,const char**);
103104  int  (*prepare16)(sqlite3*,const void*,int,sqlite3_stmt**,const void**);
103105  void * (*profile)(sqlite3*,void(*)(void*,const char*,sqlite_uint64),void*);
103106  void  (*progress_handler)(sqlite3*,int,int(*)(void*),void*);
103107  void *(*realloc)(void*,int);
103108  int  (*reset)(sqlite3_stmt*pStmt);
103109  void  (*result_blob)(sqlite3_context*,const void*,int,void(*)(void*));
103110  void  (*result_double)(sqlite3_context*,double);
103111  void  (*result_error)(sqlite3_context*,const char*,int);
103112  void  (*result_error16)(sqlite3_context*,const void*,int);
103113  void  (*result_int)(sqlite3_context*,int);
103114  void  (*result_int64)(sqlite3_context*,sqlite_int64);
103115  void  (*result_null)(sqlite3_context*);
103116  void  (*result_text)(sqlite3_context*,const char*,int,void(*)(void*));
103117  void  (*result_text16)(sqlite3_context*,const void*,int,void(*)(void*));
103118  void  (*result_text16be)(sqlite3_context*,const void*,int,void(*)(void*));
103119  void  (*result_text16le)(sqlite3_context*,const void*,int,void(*)(void*));
103120  void  (*result_value)(sqlite3_context*,sqlite3_value*);
103121  void * (*rollback_hook)(sqlite3*,void(*)(void*),void*);
103122  int  (*set_authorizer)(sqlite3*,int(*)(void*,int,const char*,const char*,
103123                         const char*,const char*),void*);
103124  void  (*set_auxdata)(sqlite3_context*,int,void*,void (*)(void*));
103125  char * (*snprintf)(int,char*,const char*,...);
103126  int  (*step)(sqlite3_stmt*);
103127  int  (*table_column_metadata)(sqlite3*,const char*,const char*,const char*,
103128                                char const**,char const**,int*,int*,int*);
103129  void  (*thread_cleanup)(void);
103130  int  (*total_changes)(sqlite3*);
103131  void * (*trace)(sqlite3*,void(*xTrace)(void*,const char*),void*);
103132  int  (*transfer_bindings)(sqlite3_stmt*,sqlite3_stmt*);
103133  void * (*update_hook)(sqlite3*,void(*)(void*,int ,char const*,char const*,
103134                                         sqlite_int64),void*);
103135  void * (*user_data)(sqlite3_context*);
103136  const void * (*value_blob)(sqlite3_value*);
103137  int  (*value_bytes)(sqlite3_value*);
103138  int  (*value_bytes16)(sqlite3_value*);
103139  double  (*value_double)(sqlite3_value*);
103140  int  (*value_int)(sqlite3_value*);
103141  sqlite_int64  (*value_int64)(sqlite3_value*);
103142  int  (*value_numeric_type)(sqlite3_value*);
103143  const unsigned char * (*value_text)(sqlite3_value*);
103144  const void * (*value_text16)(sqlite3_value*);
103145  const void * (*value_text16be)(sqlite3_value*);
103146  const void * (*value_text16le)(sqlite3_value*);
103147  int  (*value_type)(sqlite3_value*);
103148  char *(*vmprintf)(const char*,va_list);
103149  /* Added ??? */
103150  int (*overload_function)(sqlite3*, const char *zFuncName, int nArg);
103151  /* Added by 3.3.13 */
103152  int (*prepare_v2)(sqlite3*,const char*,int,sqlite3_stmt**,const char**);
103153  int (*prepare16_v2)(sqlite3*,const void*,int,sqlite3_stmt**,const void**);
103154  int (*clear_bindings)(sqlite3_stmt*);
103155  /* Added by 3.4.1 */
103156  int (*create_module_v2)(sqlite3*,const char*,const sqlite3_module*,void*,
103157                          void (*xDestroy)(void *));
103158  /* Added by 3.5.0 */
103159  int (*bind_zeroblob)(sqlite3_stmt*,int,int);
103160  int (*blob_bytes)(sqlite3_blob*);
103161  int (*blob_close)(sqlite3_blob*);
103162  int (*blob_open)(sqlite3*,const char*,const char*,const char*,sqlite3_int64,
103163                   int,sqlite3_blob**);
103164  int (*blob_read)(sqlite3_blob*,void*,int,int);
103165  int (*blob_write)(sqlite3_blob*,const void*,int,int);
103166  int (*create_collation_v2)(sqlite3*,const char*,int,void*,
103167                             int(*)(void*,int,const void*,int,const void*),
103168                             void(*)(void*));
103169  int (*file_control)(sqlite3*,const char*,int,void*);
103170  sqlite3_int64 (*memory_highwater)(int);
103171  sqlite3_int64 (*memory_used)(void);
103172  sqlite3_mutex *(*mutex_alloc)(int);
103173  void (*mutex_enter)(sqlite3_mutex*);
103174  void (*mutex_free)(sqlite3_mutex*);
103175  void (*mutex_leave)(sqlite3_mutex*);
103176  int (*mutex_try)(sqlite3_mutex*);
103177  int (*open_v2)(const char*,sqlite3**,int,const char*);
103178  int (*release_memory)(int);
103179  void (*result_error_nomem)(sqlite3_context*);
103180  void (*result_error_toobig)(sqlite3_context*);
103181  int (*sleep)(int);
103182  void (*soft_heap_limit)(int);
103183  sqlite3_vfs *(*vfs_find)(const char*);
103184  int (*vfs_register)(sqlite3_vfs*,int);
103185  int (*vfs_unregister)(sqlite3_vfs*);
103186  int (*xthreadsafe)(void);
103187  void (*result_zeroblob)(sqlite3_context*,int);
103188  void (*result_error_code)(sqlite3_context*,int);
103189  int (*test_control)(int, ...);
103190  void (*randomness)(int,void*);
103191  sqlite3 *(*context_db_handle)(sqlite3_context*);
103192  int (*extended_result_codes)(sqlite3*,int);
103193  int (*limit)(sqlite3*,int,int);
103194  sqlite3_stmt *(*next_stmt)(sqlite3*,sqlite3_stmt*);
103195  const char *(*sql)(sqlite3_stmt*);
103196  int (*status)(int,int*,int*,int);
103197  int (*backup_finish)(sqlite3_backup*);
103198  sqlite3_backup *(*backup_init)(sqlite3*,const char*,sqlite3*,const char*);
103199  int (*backup_pagecount)(sqlite3_backup*);
103200  int (*backup_remaining)(sqlite3_backup*);
103201  int (*backup_step)(sqlite3_backup*,int);
103202  const char *(*compileoption_get)(int);
103203  int (*compileoption_used)(const char*);
103204  int (*create_function_v2)(sqlite3*,const char*,int,int,void*,
103205                            void (*xFunc)(sqlite3_context*,int,sqlite3_value**),
103206                            void (*xStep)(sqlite3_context*,int,sqlite3_value**),
103207                            void (*xFinal)(sqlite3_context*),
103208                            void(*xDestroy)(void*));
103209  int (*db_config)(sqlite3*,int,...);
103210  sqlite3_mutex *(*db_mutex)(sqlite3*);
103211  int (*db_status)(sqlite3*,int,int*,int*,int);
103212  int (*extended_errcode)(sqlite3*);
103213  void (*log)(int,const char*,...);
103214  sqlite3_int64 (*soft_heap_limit64)(sqlite3_int64);
103215  const char *(*sourceid)(void);
103216  int (*stmt_status)(sqlite3_stmt*,int,int);
103217  int (*strnicmp)(const char*,const char*,int);
103218  int (*unlock_notify)(sqlite3*,void(*)(void**,int),void*);
103219  int (*wal_autocheckpoint)(sqlite3*,int);
103220  int (*wal_checkpoint)(sqlite3*,const char*);
103221  void *(*wal_hook)(sqlite3*,int(*)(void*,sqlite3*,const char*,int),void*);
103222  int (*blob_reopen)(sqlite3_blob*,sqlite3_int64);
103223  int (*vtab_config)(sqlite3*,int op,...);
103224  int (*vtab_on_conflict)(sqlite3*);
103225  /* Version 3.7.16 and later */
103226  int (*close_v2)(sqlite3*);
103227  const char *(*db_filename)(sqlite3*,const char*);
103228  int (*db_readonly)(sqlite3*,const char*);
103229  int (*db_release_memory)(sqlite3*);
103230  const char *(*errstr)(int);
103231  int (*stmt_busy)(sqlite3_stmt*);
103232  int (*stmt_readonly)(sqlite3_stmt*);
103233  int (*stricmp)(const char*,const char*);
103234  int (*uri_boolean)(const char*,const char*,int);
103235  sqlite3_int64 (*uri_int64)(const char*,const char*,sqlite3_int64);
103236  const char *(*uri_parameter)(const char*,const char*);
103237  char *(*vsnprintf)(int,char*,const char*,va_list);
103238  int (*wal_checkpoint_v2)(sqlite3*,const char*,int,int*,int*);
103239  /* Version 3.8.7 and later */
103240  int (*auto_extension)(void(*)(void));
103241  int (*bind_blob64)(sqlite3_stmt*,int,const void*,sqlite3_uint64,
103242                     void(*)(void*));
103243  int (*bind_text64)(sqlite3_stmt*,int,const char*,sqlite3_uint64,
103244                      void(*)(void*),unsigned char);
103245  int (*cancel_auto_extension)(void(*)(void));
103246  int (*load_extension)(sqlite3*,const char*,const char*,char**);
103247  void *(*malloc64)(sqlite3_uint64);
103248  sqlite3_uint64 (*msize)(void*);
103249  void *(*realloc64)(void*,sqlite3_uint64);
103250  void (*reset_auto_extension)(void);
103251  void (*result_blob64)(sqlite3_context*,const void*,sqlite3_uint64,
103252                        void(*)(void*));
103253  void (*result_text64)(sqlite3_context*,const char*,sqlite3_uint64,
103254                         void(*)(void*), unsigned char);
103255  int (*strglob)(const char*,const char*);
103256  /* Version 3.8.11 and later */
103257  sqlite3_value *(*value_dup)(const sqlite3_value*);
103258  void (*value_free)(sqlite3_value*);
103259  int (*result_zeroblob64)(sqlite3_context*,sqlite3_uint64);
103260  int (*bind_zeroblob64)(sqlite3_stmt*, int, sqlite3_uint64);
103261};
103262
103263/*
103264** The following macros redefine the API routines so that they are
103265** redirected through the global sqlite3_api structure.
103266**
103267** This header file is also used by the loadext.c source file
103268** (part of the main SQLite library - not an extension) so that
103269** it can get access to the sqlite3_api_routines structure
103270** definition.  But the main library does not want to redefine
103271** the API.  So the redefinition macros are only valid if the
103272** SQLITE_CORE macros is undefined.
103273*/
103274#ifndef SQLITE_CORE
103275#define sqlite3_aggregate_context      sqlite3_api->aggregate_context
103276#ifndef SQLITE_OMIT_DEPRECATED
103277#define sqlite3_aggregate_count        sqlite3_api->aggregate_count
103278#endif
103279#define sqlite3_bind_blob              sqlite3_api->bind_blob
103280#define sqlite3_bind_double            sqlite3_api->bind_double
103281#define sqlite3_bind_int               sqlite3_api->bind_int
103282#define sqlite3_bind_int64             sqlite3_api->bind_int64
103283#define sqlite3_bind_null              sqlite3_api->bind_null
103284#define sqlite3_bind_parameter_count   sqlite3_api->bind_parameter_count
103285#define sqlite3_bind_parameter_index   sqlite3_api->bind_parameter_index
103286#define sqlite3_bind_parameter_name    sqlite3_api->bind_parameter_name
103287#define sqlite3_bind_text              sqlite3_api->bind_text
103288#define sqlite3_bind_text16            sqlite3_api->bind_text16
103289#define sqlite3_bind_value             sqlite3_api->bind_value
103290#define sqlite3_busy_handler           sqlite3_api->busy_handler
103291#define sqlite3_busy_timeout           sqlite3_api->busy_timeout
103292#define sqlite3_changes                sqlite3_api->changes
103293#define sqlite3_close                  sqlite3_api->close
103294#define sqlite3_collation_needed       sqlite3_api->collation_needed
103295#define sqlite3_collation_needed16     sqlite3_api->collation_needed16
103296#define sqlite3_column_blob            sqlite3_api->column_blob
103297#define sqlite3_column_bytes           sqlite3_api->column_bytes
103298#define sqlite3_column_bytes16         sqlite3_api->column_bytes16
103299#define sqlite3_column_count           sqlite3_api->column_count
103300#define sqlite3_column_database_name   sqlite3_api->column_database_name
103301#define sqlite3_column_database_name16 sqlite3_api->column_database_name16
103302#define sqlite3_column_decltype        sqlite3_api->column_decltype
103303#define sqlite3_column_decltype16      sqlite3_api->column_decltype16
103304#define sqlite3_column_double          sqlite3_api->column_double
103305#define sqlite3_column_int             sqlite3_api->column_int
103306#define sqlite3_column_int64           sqlite3_api->column_int64
103307#define sqlite3_column_name            sqlite3_api->column_name
103308#define sqlite3_column_name16          sqlite3_api->column_name16
103309#define sqlite3_column_origin_name     sqlite3_api->column_origin_name
103310#define sqlite3_column_origin_name16   sqlite3_api->column_origin_name16
103311#define sqlite3_column_table_name      sqlite3_api->column_table_name
103312#define sqlite3_column_table_name16    sqlite3_api->column_table_name16
103313#define sqlite3_column_text            sqlite3_api->column_text
103314#define sqlite3_column_text16          sqlite3_api->column_text16
103315#define sqlite3_column_type            sqlite3_api->column_type
103316#define sqlite3_column_value           sqlite3_api->column_value
103317#define sqlite3_commit_hook            sqlite3_api->commit_hook
103318#define sqlite3_complete               sqlite3_api->complete
103319#define sqlite3_complete16             sqlite3_api->complete16
103320#define sqlite3_create_collation       sqlite3_api->create_collation
103321#define sqlite3_create_collation16     sqlite3_api->create_collation16
103322#define sqlite3_create_function        sqlite3_api->create_function
103323#define sqlite3_create_function16      sqlite3_api->create_function16
103324#define sqlite3_create_module          sqlite3_api->create_module
103325#define sqlite3_create_module_v2       sqlite3_api->create_module_v2
103326#define sqlite3_data_count             sqlite3_api->data_count
103327#define sqlite3_db_handle              sqlite3_api->db_handle
103328#define sqlite3_declare_vtab           sqlite3_api->declare_vtab
103329#define sqlite3_enable_shared_cache    sqlite3_api->enable_shared_cache
103330#define sqlite3_errcode                sqlite3_api->errcode
103331#define sqlite3_errmsg                 sqlite3_api->errmsg
103332#define sqlite3_errmsg16               sqlite3_api->errmsg16
103333#define sqlite3_exec                   sqlite3_api->exec
103334#ifndef SQLITE_OMIT_DEPRECATED
103335#define sqlite3_expired                sqlite3_api->expired
103336#endif
103337#define sqlite3_finalize               sqlite3_api->finalize
103338#define sqlite3_free                   sqlite3_api->free
103339#define sqlite3_free_table             sqlite3_api->free_table
103340#define sqlite3_get_autocommit         sqlite3_api->get_autocommit
103341#define sqlite3_get_auxdata            sqlite3_api->get_auxdata
103342#define sqlite3_get_table              sqlite3_api->get_table
103343#ifndef SQLITE_OMIT_DEPRECATED
103344#define sqlite3_global_recover         sqlite3_api->global_recover
103345#endif
103346#define sqlite3_interrupt              sqlite3_api->interruptx
103347#define sqlite3_last_insert_rowid      sqlite3_api->last_insert_rowid
103348#define sqlite3_libversion             sqlite3_api->libversion
103349#define sqlite3_libversion_number      sqlite3_api->libversion_number
103350#define sqlite3_malloc                 sqlite3_api->malloc
103351#define sqlite3_mprintf                sqlite3_api->mprintf
103352#define sqlite3_open                   sqlite3_api->open
103353#define sqlite3_open16                 sqlite3_api->open16
103354#define sqlite3_prepare                sqlite3_api->prepare
103355#define sqlite3_prepare16              sqlite3_api->prepare16
103356#define sqlite3_prepare_v2             sqlite3_api->prepare_v2
103357#define sqlite3_prepare16_v2           sqlite3_api->prepare16_v2
103358#define sqlite3_profile                sqlite3_api->profile
103359#define sqlite3_progress_handler       sqlite3_api->progress_handler
103360#define sqlite3_realloc                sqlite3_api->realloc
103361#define sqlite3_reset                  sqlite3_api->reset
103362#define sqlite3_result_blob            sqlite3_api->result_blob
103363#define sqlite3_result_double          sqlite3_api->result_double
103364#define sqlite3_result_error           sqlite3_api->result_error
103365#define sqlite3_result_error16         sqlite3_api->result_error16
103366#define sqlite3_result_int             sqlite3_api->result_int
103367#define sqlite3_result_int64           sqlite3_api->result_int64
103368#define sqlite3_result_null            sqlite3_api->result_null
103369#define sqlite3_result_text            sqlite3_api->result_text
103370#define sqlite3_result_text16          sqlite3_api->result_text16
103371#define sqlite3_result_text16be        sqlite3_api->result_text16be
103372#define sqlite3_result_text16le        sqlite3_api->result_text16le
103373#define sqlite3_result_value           sqlite3_api->result_value
103374#define sqlite3_rollback_hook          sqlite3_api->rollback_hook
103375#define sqlite3_set_authorizer         sqlite3_api->set_authorizer
103376#define sqlite3_set_auxdata            sqlite3_api->set_auxdata
103377#define sqlite3_snprintf               sqlite3_api->snprintf
103378#define sqlite3_step                   sqlite3_api->step
103379#define sqlite3_table_column_metadata  sqlite3_api->table_column_metadata
103380#define sqlite3_thread_cleanup         sqlite3_api->thread_cleanup
103381#define sqlite3_total_changes          sqlite3_api->total_changes
103382#define sqlite3_trace                  sqlite3_api->trace
103383#ifndef SQLITE_OMIT_DEPRECATED
103384#define sqlite3_transfer_bindings      sqlite3_api->transfer_bindings
103385#endif
103386#define sqlite3_update_hook            sqlite3_api->update_hook
103387#define sqlite3_user_data              sqlite3_api->user_data
103388#define sqlite3_value_blob             sqlite3_api->value_blob
103389#define sqlite3_value_bytes            sqlite3_api->value_bytes
103390#define sqlite3_value_bytes16          sqlite3_api->value_bytes16
103391#define sqlite3_value_double           sqlite3_api->value_double
103392#define sqlite3_value_int              sqlite3_api->value_int
103393#define sqlite3_value_int64            sqlite3_api->value_int64
103394#define sqlite3_value_numeric_type     sqlite3_api->value_numeric_type
103395#define sqlite3_value_text             sqlite3_api->value_text
103396#define sqlite3_value_text16           sqlite3_api->value_text16
103397#define sqlite3_value_text16be         sqlite3_api->value_text16be
103398#define sqlite3_value_text16le         sqlite3_api->value_text16le
103399#define sqlite3_value_type             sqlite3_api->value_type
103400#define sqlite3_vmprintf               sqlite3_api->vmprintf
103401#define sqlite3_overload_function      sqlite3_api->overload_function
103402#define sqlite3_prepare_v2             sqlite3_api->prepare_v2
103403#define sqlite3_prepare16_v2           sqlite3_api->prepare16_v2
103404#define sqlite3_clear_bindings         sqlite3_api->clear_bindings
103405#define sqlite3_bind_zeroblob          sqlite3_api->bind_zeroblob
103406#define sqlite3_blob_bytes             sqlite3_api->blob_bytes
103407#define sqlite3_blob_close             sqlite3_api->blob_close
103408#define sqlite3_blob_open              sqlite3_api->blob_open
103409#define sqlite3_blob_read              sqlite3_api->blob_read
103410#define sqlite3_blob_write             sqlite3_api->blob_write
103411#define sqlite3_create_collation_v2    sqlite3_api->create_collation_v2
103412#define sqlite3_file_control           sqlite3_api->file_control
103413#define sqlite3_memory_highwater       sqlite3_api->memory_highwater
103414#define sqlite3_memory_used            sqlite3_api->memory_used
103415#define sqlite3_mutex_alloc            sqlite3_api->mutex_alloc
103416#define sqlite3_mutex_enter            sqlite3_api->mutex_enter
103417#define sqlite3_mutex_free             sqlite3_api->mutex_free
103418#define sqlite3_mutex_leave            sqlite3_api->mutex_leave
103419#define sqlite3_mutex_try              sqlite3_api->mutex_try
103420#define sqlite3_open_v2                sqlite3_api->open_v2
103421#define sqlite3_release_memory         sqlite3_api->release_memory
103422#define sqlite3_result_error_nomem     sqlite3_api->result_error_nomem
103423#define sqlite3_result_error_toobig    sqlite3_api->result_error_toobig
103424#define sqlite3_sleep                  sqlite3_api->sleep
103425#define sqlite3_soft_heap_limit        sqlite3_api->soft_heap_limit
103426#define sqlite3_vfs_find               sqlite3_api->vfs_find
103427#define sqlite3_vfs_register           sqlite3_api->vfs_register
103428#define sqlite3_vfs_unregister         sqlite3_api->vfs_unregister
103429#define sqlite3_threadsafe             sqlite3_api->xthreadsafe
103430#define sqlite3_result_zeroblob        sqlite3_api->result_zeroblob
103431#define sqlite3_result_error_code      sqlite3_api->result_error_code
103432#define sqlite3_test_control           sqlite3_api->test_control
103433#define sqlite3_randomness             sqlite3_api->randomness
103434#define sqlite3_context_db_handle      sqlite3_api->context_db_handle
103435#define sqlite3_extended_result_codes  sqlite3_api->extended_result_codes
103436#define sqlite3_limit                  sqlite3_api->limit
103437#define sqlite3_next_stmt              sqlite3_api->next_stmt
103438#define sqlite3_sql                    sqlite3_api->sql
103439#define sqlite3_status                 sqlite3_api->status
103440#define sqlite3_backup_finish          sqlite3_api->backup_finish
103441#define sqlite3_backup_init            sqlite3_api->backup_init
103442#define sqlite3_backup_pagecount       sqlite3_api->backup_pagecount
103443#define sqlite3_backup_remaining       sqlite3_api->backup_remaining
103444#define sqlite3_backup_step            sqlite3_api->backup_step
103445#define sqlite3_compileoption_get      sqlite3_api->compileoption_get
103446#define sqlite3_compileoption_used     sqlite3_api->compileoption_used
103447#define sqlite3_create_function_v2     sqlite3_api->create_function_v2
103448#define sqlite3_db_config              sqlite3_api->db_config
103449#define sqlite3_db_mutex               sqlite3_api->db_mutex
103450#define sqlite3_db_status              sqlite3_api->db_status
103451#define sqlite3_extended_errcode       sqlite3_api->extended_errcode
103452#define sqlite3_log                    sqlite3_api->log
103453#define sqlite3_soft_heap_limit64      sqlite3_api->soft_heap_limit64
103454#define sqlite3_sourceid               sqlite3_api->sourceid
103455#define sqlite3_stmt_status            sqlite3_api->stmt_status
103456#define sqlite3_strnicmp               sqlite3_api->strnicmp
103457#define sqlite3_unlock_notify          sqlite3_api->unlock_notify
103458#define sqlite3_wal_autocheckpoint     sqlite3_api->wal_autocheckpoint
103459#define sqlite3_wal_checkpoint         sqlite3_api->wal_checkpoint
103460#define sqlite3_wal_hook               sqlite3_api->wal_hook
103461#define sqlite3_blob_reopen            sqlite3_api->blob_reopen
103462#define sqlite3_vtab_config            sqlite3_api->vtab_config
103463#define sqlite3_vtab_on_conflict       sqlite3_api->vtab_on_conflict
103464/* Version 3.7.16 and later */
103465#define sqlite3_close_v2               sqlite3_api->close_v2
103466#define sqlite3_db_filename            sqlite3_api->db_filename
103467#define sqlite3_db_readonly            sqlite3_api->db_readonly
103468#define sqlite3_db_release_memory      sqlite3_api->db_release_memory
103469#define sqlite3_errstr                 sqlite3_api->errstr
103470#define sqlite3_stmt_busy              sqlite3_api->stmt_busy
103471#define sqlite3_stmt_readonly          sqlite3_api->stmt_readonly
103472#define sqlite3_stricmp                sqlite3_api->stricmp
103473#define sqlite3_uri_boolean            sqlite3_api->uri_boolean
103474#define sqlite3_uri_int64              sqlite3_api->uri_int64
103475#define sqlite3_uri_parameter          sqlite3_api->uri_parameter
103476#define sqlite3_uri_vsnprintf          sqlite3_api->vsnprintf
103477#define sqlite3_wal_checkpoint_v2      sqlite3_api->wal_checkpoint_v2
103478/* Version 3.8.7 and later */
103479#define sqlite3_auto_extension         sqlite3_api->auto_extension
103480#define sqlite3_bind_blob64            sqlite3_api->bind_blob64
103481#define sqlite3_bind_text64            sqlite3_api->bind_text64
103482#define sqlite3_cancel_auto_extension  sqlite3_api->cancel_auto_extension
103483#define sqlite3_load_extension         sqlite3_api->load_extension
103484#define sqlite3_malloc64               sqlite3_api->malloc64
103485#define sqlite3_msize                  sqlite3_api->msize
103486#define sqlite3_realloc64              sqlite3_api->realloc64
103487#define sqlite3_reset_auto_extension   sqlite3_api->reset_auto_extension
103488#define sqlite3_result_blob64          sqlite3_api->result_blob64
103489#define sqlite3_result_text64          sqlite3_api->result_text64
103490#define sqlite3_strglob                sqlite3_api->strglob
103491/* Version 3.8.11 and later */
103492#define sqlite3_value_dup              sqlite3_api->value_dup
103493#define sqlite3_value_free             sqlite3_api->value_free
103494#define sqlite3_result_zeroblob64      sqlite3_api->result_zeroblob64
103495#define sqlite3_bind_zeroblob64        sqlite3_api->bind_zeroblob64
103496#endif /* SQLITE_CORE */
103497
103498#ifndef SQLITE_CORE
103499  /* This case when the file really is being compiled as a loadable
103500  ** extension */
103501# define SQLITE_EXTENSION_INIT1     const sqlite3_api_routines *sqlite3_api=0;
103502# define SQLITE_EXTENSION_INIT2(v)  sqlite3_api=v;
103503# define SQLITE_EXTENSION_INIT3     \
103504    extern const sqlite3_api_routines *sqlite3_api;
103505#else
103506  /* This case when the file is being statically linked into the
103507  ** application */
103508# define SQLITE_EXTENSION_INIT1     /*no-op*/
103509# define SQLITE_EXTENSION_INIT2(v)  (void)v; /* unused parameter */
103510# define SQLITE_EXTENSION_INIT3     /*no-op*/
103511#endif
103512
103513#endif /* _SQLITE3EXT_H_ */
103514
103515/************** End of sqlite3ext.h ******************************************/
103516/************** Continuing where we left off in loadext.c ********************/
103517/* #include "sqliteInt.h" */
103518/* #include <string.h> */
103519
103520#ifndef SQLITE_OMIT_LOAD_EXTENSION
103521
103522/*
103523** Some API routines are omitted when various features are
103524** excluded from a build of SQLite.  Substitute a NULL pointer
103525** for any missing APIs.
103526*/
103527#ifndef SQLITE_ENABLE_COLUMN_METADATA
103528# define sqlite3_column_database_name   0
103529# define sqlite3_column_database_name16 0
103530# define sqlite3_column_table_name      0
103531# define sqlite3_column_table_name16    0
103532# define sqlite3_column_origin_name     0
103533# define sqlite3_column_origin_name16   0
103534#endif
103535
103536#ifdef SQLITE_OMIT_AUTHORIZATION
103537# define sqlite3_set_authorizer         0
103538#endif
103539
103540#ifdef SQLITE_OMIT_UTF16
103541# define sqlite3_bind_text16            0
103542# define sqlite3_collation_needed16     0
103543# define sqlite3_column_decltype16      0
103544# define sqlite3_column_name16          0
103545# define sqlite3_column_text16          0
103546# define sqlite3_complete16             0
103547# define sqlite3_create_collation16     0
103548# define sqlite3_create_function16      0
103549# define sqlite3_errmsg16               0
103550# define sqlite3_open16                 0
103551# define sqlite3_prepare16              0
103552# define sqlite3_prepare16_v2           0
103553# define sqlite3_result_error16         0
103554# define sqlite3_result_text16          0
103555# define sqlite3_result_text16be        0
103556# define sqlite3_result_text16le        0
103557# define sqlite3_value_text16           0
103558# define sqlite3_value_text16be         0
103559# define sqlite3_value_text16le         0
103560# define sqlite3_column_database_name16 0
103561# define sqlite3_column_table_name16    0
103562# define sqlite3_column_origin_name16   0
103563#endif
103564
103565#ifdef SQLITE_OMIT_COMPLETE
103566# define sqlite3_complete 0
103567# define sqlite3_complete16 0
103568#endif
103569
103570#ifdef SQLITE_OMIT_DECLTYPE
103571# define sqlite3_column_decltype16      0
103572# define sqlite3_column_decltype        0
103573#endif
103574
103575#ifdef SQLITE_OMIT_PROGRESS_CALLBACK
103576# define sqlite3_progress_handler 0
103577#endif
103578
103579#ifdef SQLITE_OMIT_VIRTUALTABLE
103580# define sqlite3_create_module 0
103581# define sqlite3_create_module_v2 0
103582# define sqlite3_declare_vtab 0
103583# define sqlite3_vtab_config 0
103584# define sqlite3_vtab_on_conflict 0
103585#endif
103586
103587#ifdef SQLITE_OMIT_SHARED_CACHE
103588# define sqlite3_enable_shared_cache 0
103589#endif
103590
103591#ifdef SQLITE_OMIT_TRACE
103592# define sqlite3_profile       0
103593# define sqlite3_trace         0
103594#endif
103595
103596#ifdef SQLITE_OMIT_GET_TABLE
103597# define sqlite3_free_table    0
103598# define sqlite3_get_table     0
103599#endif
103600
103601#ifdef SQLITE_OMIT_INCRBLOB
103602#define sqlite3_bind_zeroblob  0
103603#define sqlite3_blob_bytes     0
103604#define sqlite3_blob_close     0
103605#define sqlite3_blob_open      0
103606#define sqlite3_blob_read      0
103607#define sqlite3_blob_write     0
103608#define sqlite3_blob_reopen    0
103609#endif
103610
103611/*
103612** The following structure contains pointers to all SQLite API routines.
103613** A pointer to this structure is passed into extensions when they are
103614** loaded so that the extension can make calls back into the SQLite
103615** library.
103616**
103617** When adding new APIs, add them to the bottom of this structure
103618** in order to preserve backwards compatibility.
103619**
103620** Extensions that use newer APIs should first call the
103621** sqlite3_libversion_number() to make sure that the API they
103622** intend to use is supported by the library.  Extensions should
103623** also check to make sure that the pointer to the function is
103624** not NULL before calling it.
103625*/
103626static const sqlite3_api_routines sqlite3Apis = {
103627  sqlite3_aggregate_context,
103628#ifndef SQLITE_OMIT_DEPRECATED
103629  sqlite3_aggregate_count,
103630#else
103631  0,
103632#endif
103633  sqlite3_bind_blob,
103634  sqlite3_bind_double,
103635  sqlite3_bind_int,
103636  sqlite3_bind_int64,
103637  sqlite3_bind_null,
103638  sqlite3_bind_parameter_count,
103639  sqlite3_bind_parameter_index,
103640  sqlite3_bind_parameter_name,
103641  sqlite3_bind_text,
103642  sqlite3_bind_text16,
103643  sqlite3_bind_value,
103644  sqlite3_busy_handler,
103645  sqlite3_busy_timeout,
103646  sqlite3_changes,
103647  sqlite3_close,
103648  sqlite3_collation_needed,
103649  sqlite3_collation_needed16,
103650  sqlite3_column_blob,
103651  sqlite3_column_bytes,
103652  sqlite3_column_bytes16,
103653  sqlite3_column_count,
103654  sqlite3_column_database_name,
103655  sqlite3_column_database_name16,
103656  sqlite3_column_decltype,
103657  sqlite3_column_decltype16,
103658  sqlite3_column_double,
103659  sqlite3_column_int,
103660  sqlite3_column_int64,
103661  sqlite3_column_name,
103662  sqlite3_column_name16,
103663  sqlite3_column_origin_name,
103664  sqlite3_column_origin_name16,
103665  sqlite3_column_table_name,
103666  sqlite3_column_table_name16,
103667  sqlite3_column_text,
103668  sqlite3_column_text16,
103669  sqlite3_column_type,
103670  sqlite3_column_value,
103671  sqlite3_commit_hook,
103672  sqlite3_complete,
103673  sqlite3_complete16,
103674  sqlite3_create_collation,
103675  sqlite3_create_collation16,
103676  sqlite3_create_function,
103677  sqlite3_create_function16,
103678  sqlite3_create_module,
103679  sqlite3_data_count,
103680  sqlite3_db_handle,
103681  sqlite3_declare_vtab,
103682  sqlite3_enable_shared_cache,
103683  sqlite3_errcode,
103684  sqlite3_errmsg,
103685  sqlite3_errmsg16,
103686  sqlite3_exec,
103687#ifndef SQLITE_OMIT_DEPRECATED
103688  sqlite3_expired,
103689#else
103690  0,
103691#endif
103692  sqlite3_finalize,
103693  sqlite3_free,
103694  sqlite3_free_table,
103695  sqlite3_get_autocommit,
103696  sqlite3_get_auxdata,
103697  sqlite3_get_table,
103698  0,     /* Was sqlite3_global_recover(), but that function is deprecated */
103699  sqlite3_interrupt,
103700  sqlite3_last_insert_rowid,
103701  sqlite3_libversion,
103702  sqlite3_libversion_number,
103703  sqlite3_malloc,
103704  sqlite3_mprintf,
103705  sqlite3_open,
103706  sqlite3_open16,
103707  sqlite3_prepare,
103708  sqlite3_prepare16,
103709  sqlite3_profile,
103710  sqlite3_progress_handler,
103711  sqlite3_realloc,
103712  sqlite3_reset,
103713  sqlite3_result_blob,
103714  sqlite3_result_double,
103715  sqlite3_result_error,
103716  sqlite3_result_error16,
103717  sqlite3_result_int,
103718  sqlite3_result_int64,
103719  sqlite3_result_null,
103720  sqlite3_result_text,
103721  sqlite3_result_text16,
103722  sqlite3_result_text16be,
103723  sqlite3_result_text16le,
103724  sqlite3_result_value,
103725  sqlite3_rollback_hook,
103726  sqlite3_set_authorizer,
103727  sqlite3_set_auxdata,
103728  sqlite3_snprintf,
103729  sqlite3_step,
103730  sqlite3_table_column_metadata,
103731#ifndef SQLITE_OMIT_DEPRECATED
103732  sqlite3_thread_cleanup,
103733#else
103734  0,
103735#endif
103736  sqlite3_total_changes,
103737  sqlite3_trace,
103738#ifndef SQLITE_OMIT_DEPRECATED
103739  sqlite3_transfer_bindings,
103740#else
103741  0,
103742#endif
103743  sqlite3_update_hook,
103744  sqlite3_user_data,
103745  sqlite3_value_blob,
103746  sqlite3_value_bytes,
103747  sqlite3_value_bytes16,
103748  sqlite3_value_double,
103749  sqlite3_value_int,
103750  sqlite3_value_int64,
103751  sqlite3_value_numeric_type,
103752  sqlite3_value_text,
103753  sqlite3_value_text16,
103754  sqlite3_value_text16be,
103755  sqlite3_value_text16le,
103756  sqlite3_value_type,
103757  sqlite3_vmprintf,
103758  /*
103759  ** The original API set ends here.  All extensions can call any
103760  ** of the APIs above provided that the pointer is not NULL.  But
103761  ** before calling APIs that follow, extension should check the
103762  ** sqlite3_libversion_number() to make sure they are dealing with
103763  ** a library that is new enough to support that API.
103764  *************************************************************************
103765  */
103766  sqlite3_overload_function,
103767
103768  /*
103769  ** Added after 3.3.13
103770  */
103771  sqlite3_prepare_v2,
103772  sqlite3_prepare16_v2,
103773  sqlite3_clear_bindings,
103774
103775  /*
103776  ** Added for 3.4.1
103777  */
103778  sqlite3_create_module_v2,
103779
103780  /*
103781  ** Added for 3.5.0
103782  */
103783  sqlite3_bind_zeroblob,
103784  sqlite3_blob_bytes,
103785  sqlite3_blob_close,
103786  sqlite3_blob_open,
103787  sqlite3_blob_read,
103788  sqlite3_blob_write,
103789  sqlite3_create_collation_v2,
103790  sqlite3_file_control,
103791  sqlite3_memory_highwater,
103792  sqlite3_memory_used,
103793#ifdef SQLITE_MUTEX_OMIT
103794  0,
103795  0,
103796  0,
103797  0,
103798  0,
103799#else
103800  sqlite3_mutex_alloc,
103801  sqlite3_mutex_enter,
103802  sqlite3_mutex_free,
103803  sqlite3_mutex_leave,
103804  sqlite3_mutex_try,
103805#endif
103806  sqlite3_open_v2,
103807  sqlite3_release_memory,
103808  sqlite3_result_error_nomem,
103809  sqlite3_result_error_toobig,
103810  sqlite3_sleep,
103811  sqlite3_soft_heap_limit,
103812  sqlite3_vfs_find,
103813  sqlite3_vfs_register,
103814  sqlite3_vfs_unregister,
103815
103816  /*
103817  ** Added for 3.5.8
103818  */
103819  sqlite3_threadsafe,
103820  sqlite3_result_zeroblob,
103821  sqlite3_result_error_code,
103822  sqlite3_test_control,
103823  sqlite3_randomness,
103824  sqlite3_context_db_handle,
103825
103826  /*
103827  ** Added for 3.6.0
103828  */
103829  sqlite3_extended_result_codes,
103830  sqlite3_limit,
103831  sqlite3_next_stmt,
103832  sqlite3_sql,
103833  sqlite3_status,
103834
103835  /*
103836  ** Added for 3.7.4
103837  */
103838  sqlite3_backup_finish,
103839  sqlite3_backup_init,
103840  sqlite3_backup_pagecount,
103841  sqlite3_backup_remaining,
103842  sqlite3_backup_step,
103843#ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
103844  sqlite3_compileoption_get,
103845  sqlite3_compileoption_used,
103846#else
103847  0,
103848  0,
103849#endif
103850  sqlite3_create_function_v2,
103851  sqlite3_db_config,
103852  sqlite3_db_mutex,
103853  sqlite3_db_status,
103854  sqlite3_extended_errcode,
103855  sqlite3_log,
103856  sqlite3_soft_heap_limit64,
103857  sqlite3_sourceid,
103858  sqlite3_stmt_status,
103859  sqlite3_strnicmp,
103860#ifdef SQLITE_ENABLE_UNLOCK_NOTIFY
103861  sqlite3_unlock_notify,
103862#else
103863  0,
103864#endif
103865#ifndef SQLITE_OMIT_WAL
103866  sqlite3_wal_autocheckpoint,
103867  sqlite3_wal_checkpoint,
103868  sqlite3_wal_hook,
103869#else
103870  0,
103871  0,
103872  0,
103873#endif
103874  sqlite3_blob_reopen,
103875  sqlite3_vtab_config,
103876  sqlite3_vtab_on_conflict,
103877  sqlite3_close_v2,
103878  sqlite3_db_filename,
103879  sqlite3_db_readonly,
103880  sqlite3_db_release_memory,
103881  sqlite3_errstr,
103882  sqlite3_stmt_busy,
103883  sqlite3_stmt_readonly,
103884  sqlite3_stricmp,
103885  sqlite3_uri_boolean,
103886  sqlite3_uri_int64,
103887  sqlite3_uri_parameter,
103888  sqlite3_vsnprintf,
103889  sqlite3_wal_checkpoint_v2,
103890  /* Version 3.8.7 and later */
103891  sqlite3_auto_extension,
103892  sqlite3_bind_blob64,
103893  sqlite3_bind_text64,
103894  sqlite3_cancel_auto_extension,
103895  sqlite3_load_extension,
103896  sqlite3_malloc64,
103897  sqlite3_msize,
103898  sqlite3_realloc64,
103899  sqlite3_reset_auto_extension,
103900  sqlite3_result_blob64,
103901  sqlite3_result_text64,
103902  sqlite3_strglob,
103903  /* Version 3.8.11 and later */
103904  (sqlite3_value*(*)(const sqlite3_value*))sqlite3_value_dup,
103905  sqlite3_value_free,
103906  sqlite3_result_zeroblob64,
103907  sqlite3_bind_zeroblob64
103908};
103909
103910/*
103911** Attempt to load an SQLite extension library contained in the file
103912** zFile.  The entry point is zProc.  zProc may be 0 in which case a
103913** default entry point name (sqlite3_extension_init) is used.  Use
103914** of the default name is recommended.
103915**
103916** Return SQLITE_OK on success and SQLITE_ERROR if something goes wrong.
103917**
103918** If an error occurs and pzErrMsg is not 0, then fill *pzErrMsg with
103919** error message text.  The calling function should free this memory
103920** by calling sqlite3DbFree(db, ).
103921*/
103922static int sqlite3LoadExtension(
103923  sqlite3 *db,          /* Load the extension into this database connection */
103924  const char *zFile,    /* Name of the shared library containing extension */
103925  const char *zProc,    /* Entry point.  Use "sqlite3_extension_init" if 0 */
103926  char **pzErrMsg       /* Put error message here if not 0 */
103927){
103928  sqlite3_vfs *pVfs = db->pVfs;
103929  void *handle;
103930  int (*xInit)(sqlite3*,char**,const sqlite3_api_routines*);
103931  char *zErrmsg = 0;
103932  const char *zEntry;
103933  char *zAltEntry = 0;
103934  void **aHandle;
103935  u64 nMsg = 300 + sqlite3Strlen30(zFile);
103936  int ii;
103937
103938  /* Shared library endings to try if zFile cannot be loaded as written */
103939  static const char *azEndings[] = {
103940#if SQLITE_OS_WIN
103941     "dll"
103942#elif defined(__APPLE__)
103943     "dylib"
103944#else
103945     "so"
103946#endif
103947  };
103948
103949
103950  if( pzErrMsg ) *pzErrMsg = 0;
103951
103952  /* Ticket #1863.  To avoid a creating security problems for older
103953  ** applications that relink against newer versions of SQLite, the
103954  ** ability to run load_extension is turned off by default.  One
103955  ** must call sqlite3_enable_load_extension() to turn on extension
103956  ** loading.  Otherwise you get the following error.
103957  */
103958  if( (db->flags & SQLITE_LoadExtension)==0 ){
103959    if( pzErrMsg ){
103960      *pzErrMsg = sqlite3_mprintf("not authorized");
103961    }
103962    return SQLITE_ERROR;
103963  }
103964
103965  zEntry = zProc ? zProc : "sqlite3_extension_init";
103966
103967  handle = sqlite3OsDlOpen(pVfs, zFile);
103968#if SQLITE_OS_UNIX || SQLITE_OS_WIN
103969  for(ii=0; ii<ArraySize(azEndings) && handle==0; ii++){
103970    char *zAltFile = sqlite3_mprintf("%s.%s", zFile, azEndings[ii]);
103971    if( zAltFile==0 ) return SQLITE_NOMEM;
103972    handle = sqlite3OsDlOpen(pVfs, zAltFile);
103973    sqlite3_free(zAltFile);
103974  }
103975#endif
103976  if( handle==0 ){
103977    if( pzErrMsg ){
103978      *pzErrMsg = zErrmsg = sqlite3_malloc64(nMsg);
103979      if( zErrmsg ){
103980        sqlite3_snprintf(nMsg, zErrmsg,
103981            "unable to open shared library [%s]", zFile);
103982        sqlite3OsDlError(pVfs, nMsg-1, zErrmsg);
103983      }
103984    }
103985    return SQLITE_ERROR;
103986  }
103987  xInit = (int(*)(sqlite3*,char**,const sqlite3_api_routines*))
103988                   sqlite3OsDlSym(pVfs, handle, zEntry);
103989
103990  /* If no entry point was specified and the default legacy
103991  ** entry point name "sqlite3_extension_init" was not found, then
103992  ** construct an entry point name "sqlite3_X_init" where the X is
103993  ** replaced by the lowercase value of every ASCII alphabetic
103994  ** character in the filename after the last "/" upto the first ".",
103995  ** and eliding the first three characters if they are "lib".
103996  ** Examples:
103997  **
103998  **    /usr/local/lib/libExample5.4.3.so ==>  sqlite3_example_init
103999  **    C:/lib/mathfuncs.dll              ==>  sqlite3_mathfuncs_init
104000  */
104001  if( xInit==0 && zProc==0 ){
104002    int iFile, iEntry, c;
104003    int ncFile = sqlite3Strlen30(zFile);
104004    zAltEntry = sqlite3_malloc64(ncFile+30);
104005    if( zAltEntry==0 ){
104006      sqlite3OsDlClose(pVfs, handle);
104007      return SQLITE_NOMEM;
104008    }
104009    memcpy(zAltEntry, "sqlite3_", 8);
104010    for(iFile=ncFile-1; iFile>=0 && zFile[iFile]!='/'; iFile--){}
104011    iFile++;
104012    if( sqlite3_strnicmp(zFile+iFile, "lib", 3)==0 ) iFile += 3;
104013    for(iEntry=8; (c = zFile[iFile])!=0 && c!='.'; iFile++){
104014      if( sqlite3Isalpha(c) ){
104015        zAltEntry[iEntry++] = (char)sqlite3UpperToLower[(unsigned)c];
104016      }
104017    }
104018    memcpy(zAltEntry+iEntry, "_init", 6);
104019    zEntry = zAltEntry;
104020    xInit = (int(*)(sqlite3*,char**,const sqlite3_api_routines*))
104021                     sqlite3OsDlSym(pVfs, handle, zEntry);
104022  }
104023  if( xInit==0 ){
104024    if( pzErrMsg ){
104025      nMsg += sqlite3Strlen30(zEntry);
104026      *pzErrMsg = zErrmsg = sqlite3_malloc64(nMsg);
104027      if( zErrmsg ){
104028        sqlite3_snprintf(nMsg, zErrmsg,
104029            "no entry point [%s] in shared library [%s]", zEntry, zFile);
104030        sqlite3OsDlError(pVfs, nMsg-1, zErrmsg);
104031      }
104032    }
104033    sqlite3OsDlClose(pVfs, handle);
104034    sqlite3_free(zAltEntry);
104035    return SQLITE_ERROR;
104036  }
104037  sqlite3_free(zAltEntry);
104038  if( xInit(db, &zErrmsg, &sqlite3Apis) ){
104039    if( pzErrMsg ){
104040      *pzErrMsg = sqlite3_mprintf("error during initialization: %s", zErrmsg);
104041    }
104042    sqlite3_free(zErrmsg);
104043    sqlite3OsDlClose(pVfs, handle);
104044    return SQLITE_ERROR;
104045  }
104046
104047  /* Append the new shared library handle to the db->aExtension array. */
104048  aHandle = sqlite3DbMallocZero(db, sizeof(handle)*(db->nExtension+1));
104049  if( aHandle==0 ){
104050    return SQLITE_NOMEM;
104051  }
104052  if( db->nExtension>0 ){
104053    memcpy(aHandle, db->aExtension, sizeof(handle)*db->nExtension);
104054  }
104055  sqlite3DbFree(db, db->aExtension);
104056  db->aExtension = aHandle;
104057
104058  db->aExtension[db->nExtension++] = handle;
104059  return SQLITE_OK;
104060}
104061SQLITE_API int SQLITE_STDCALL sqlite3_load_extension(
104062  sqlite3 *db,          /* Load the extension into this database connection */
104063  const char *zFile,    /* Name of the shared library containing extension */
104064  const char *zProc,    /* Entry point.  Use "sqlite3_extension_init" if 0 */
104065  char **pzErrMsg       /* Put error message here if not 0 */
104066){
104067  int rc;
104068  sqlite3_mutex_enter(db->mutex);
104069  rc = sqlite3LoadExtension(db, zFile, zProc, pzErrMsg);
104070  rc = sqlite3ApiExit(db, rc);
104071  sqlite3_mutex_leave(db->mutex);
104072  return rc;
104073}
104074
104075/*
104076** Call this routine when the database connection is closing in order
104077** to clean up loaded extensions
104078*/
104079SQLITE_PRIVATE void sqlite3CloseExtensions(sqlite3 *db){
104080  int i;
104081  assert( sqlite3_mutex_held(db->mutex) );
104082  for(i=0; i<db->nExtension; i++){
104083    sqlite3OsDlClose(db->pVfs, db->aExtension[i]);
104084  }
104085  sqlite3DbFree(db, db->aExtension);
104086}
104087
104088/*
104089** Enable or disable extension loading.  Extension loading is disabled by
104090** default so as not to open security holes in older applications.
104091*/
104092SQLITE_API int SQLITE_STDCALL sqlite3_enable_load_extension(sqlite3 *db, int onoff){
104093  sqlite3_mutex_enter(db->mutex);
104094  if( onoff ){
104095    db->flags |= SQLITE_LoadExtension;
104096  }else{
104097    db->flags &= ~SQLITE_LoadExtension;
104098  }
104099  sqlite3_mutex_leave(db->mutex);
104100  return SQLITE_OK;
104101}
104102
104103#endif /* SQLITE_OMIT_LOAD_EXTENSION */
104104
104105/*
104106** The auto-extension code added regardless of whether or not extension
104107** loading is supported.  We need a dummy sqlite3Apis pointer for that
104108** code if regular extension loading is not available.  This is that
104109** dummy pointer.
104110*/
104111#ifdef SQLITE_OMIT_LOAD_EXTENSION
104112static const sqlite3_api_routines sqlite3Apis = { 0 };
104113#endif
104114
104115
104116/*
104117** The following object holds the list of automatically loaded
104118** extensions.
104119**
104120** This list is shared across threads.  The SQLITE_MUTEX_STATIC_MASTER
104121** mutex must be held while accessing this list.
104122*/
104123typedef struct sqlite3AutoExtList sqlite3AutoExtList;
104124static SQLITE_WSD struct sqlite3AutoExtList {
104125  u32 nExt;              /* Number of entries in aExt[] */
104126  void (**aExt)(void);   /* Pointers to the extension init functions */
104127} sqlite3Autoext = { 0, 0 };
104128
104129/* The "wsdAutoext" macro will resolve to the autoextension
104130** state vector.  If writable static data is unsupported on the target,
104131** we have to locate the state vector at run-time.  In the more common
104132** case where writable static data is supported, wsdStat can refer directly
104133** to the "sqlite3Autoext" state vector declared above.
104134*/
104135#ifdef SQLITE_OMIT_WSD
104136# define wsdAutoextInit \
104137  sqlite3AutoExtList *x = &GLOBAL(sqlite3AutoExtList,sqlite3Autoext)
104138# define wsdAutoext x[0]
104139#else
104140# define wsdAutoextInit
104141# define wsdAutoext sqlite3Autoext
104142#endif
104143
104144
104145/*
104146** Register a statically linked extension that is automatically
104147** loaded by every new database connection.
104148*/
104149SQLITE_API int SQLITE_STDCALL sqlite3_auto_extension(void (*xInit)(void)){
104150  int rc = SQLITE_OK;
104151#ifndef SQLITE_OMIT_AUTOINIT
104152  rc = sqlite3_initialize();
104153  if( rc ){
104154    return rc;
104155  }else
104156#endif
104157  {
104158    u32 i;
104159#if SQLITE_THREADSAFE
104160    sqlite3_mutex *mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);
104161#endif
104162    wsdAutoextInit;
104163    sqlite3_mutex_enter(mutex);
104164    for(i=0; i<wsdAutoext.nExt; i++){
104165      if( wsdAutoext.aExt[i]==xInit ) break;
104166    }
104167    if( i==wsdAutoext.nExt ){
104168      u64 nByte = (wsdAutoext.nExt+1)*sizeof(wsdAutoext.aExt[0]);
104169      void (**aNew)(void);
104170      aNew = sqlite3_realloc64(wsdAutoext.aExt, nByte);
104171      if( aNew==0 ){
104172        rc = SQLITE_NOMEM;
104173      }else{
104174        wsdAutoext.aExt = aNew;
104175        wsdAutoext.aExt[wsdAutoext.nExt] = xInit;
104176        wsdAutoext.nExt++;
104177      }
104178    }
104179    sqlite3_mutex_leave(mutex);
104180    assert( (rc&0xff)==rc );
104181    return rc;
104182  }
104183}
104184
104185/*
104186** Cancel a prior call to sqlite3_auto_extension.  Remove xInit from the
104187** set of routines that is invoked for each new database connection, if it
104188** is currently on the list.  If xInit is not on the list, then this
104189** routine is a no-op.
104190**
104191** Return 1 if xInit was found on the list and removed.  Return 0 if xInit
104192** was not on the list.
104193*/
104194SQLITE_API int SQLITE_STDCALL sqlite3_cancel_auto_extension(void (*xInit)(void)){
104195#if SQLITE_THREADSAFE
104196  sqlite3_mutex *mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);
104197#endif
104198  int i;
104199  int n = 0;
104200  wsdAutoextInit;
104201  sqlite3_mutex_enter(mutex);
104202  for(i=(int)wsdAutoext.nExt-1; i>=0; i--){
104203    if( wsdAutoext.aExt[i]==xInit ){
104204      wsdAutoext.nExt--;
104205      wsdAutoext.aExt[i] = wsdAutoext.aExt[wsdAutoext.nExt];
104206      n++;
104207      break;
104208    }
104209  }
104210  sqlite3_mutex_leave(mutex);
104211  return n;
104212}
104213
104214/*
104215** Reset the automatic extension loading mechanism.
104216*/
104217SQLITE_API void SQLITE_STDCALL sqlite3_reset_auto_extension(void){
104218#ifndef SQLITE_OMIT_AUTOINIT
104219  if( sqlite3_initialize()==SQLITE_OK )
104220#endif
104221  {
104222#if SQLITE_THREADSAFE
104223    sqlite3_mutex *mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);
104224#endif
104225    wsdAutoextInit;
104226    sqlite3_mutex_enter(mutex);
104227    sqlite3_free(wsdAutoext.aExt);
104228    wsdAutoext.aExt = 0;
104229    wsdAutoext.nExt = 0;
104230    sqlite3_mutex_leave(mutex);
104231  }
104232}
104233
104234/*
104235** Load all automatic extensions.
104236**
104237** If anything goes wrong, set an error in the database connection.
104238*/
104239SQLITE_PRIVATE void sqlite3AutoLoadExtensions(sqlite3 *db){
104240  u32 i;
104241  int go = 1;
104242  int rc;
104243  int (*xInit)(sqlite3*,char**,const sqlite3_api_routines*);
104244
104245  wsdAutoextInit;
104246  if( wsdAutoext.nExt==0 ){
104247    /* Common case: early out without every having to acquire a mutex */
104248    return;
104249  }
104250  for(i=0; go; i++){
104251    char *zErrmsg;
104252#if SQLITE_THREADSAFE
104253    sqlite3_mutex *mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);
104254#endif
104255    sqlite3_mutex_enter(mutex);
104256    if( i>=wsdAutoext.nExt ){
104257      xInit = 0;
104258      go = 0;
104259    }else{
104260      xInit = (int(*)(sqlite3*,char**,const sqlite3_api_routines*))
104261              wsdAutoext.aExt[i];
104262    }
104263    sqlite3_mutex_leave(mutex);
104264    zErrmsg = 0;
104265    if( xInit && (rc = xInit(db, &zErrmsg, &sqlite3Apis))!=0 ){
104266      sqlite3ErrorWithMsg(db, rc,
104267            "automatic extension loading failed: %s", zErrmsg);
104268      go = 0;
104269    }
104270    sqlite3_free(zErrmsg);
104271  }
104272}
104273
104274/************** End of loadext.c *********************************************/
104275/************** Begin file pragma.c ******************************************/
104276/*
104277** 2003 April 6
104278**
104279** The author disclaims copyright to this source code.  In place of
104280** a legal notice, here is a blessing:
104281**
104282**    May you do good and not evil.
104283**    May you find forgiveness for yourself and forgive others.
104284**    May you share freely, never taking more than you give.
104285**
104286*************************************************************************
104287** This file contains code used to implement the PRAGMA command.
104288*/
104289/* #include "sqliteInt.h" */
104290
104291#if !defined(SQLITE_ENABLE_LOCKING_STYLE)
104292#  if defined(__APPLE__)
104293#    define SQLITE_ENABLE_LOCKING_STYLE 1
104294#  else
104295#    define SQLITE_ENABLE_LOCKING_STYLE 0
104296#  endif
104297#endif
104298
104299/***************************************************************************
104300** The "pragma.h" include file is an automatically generated file that
104301** that includes the PragType_XXXX macro definitions and the aPragmaName[]
104302** object.  This ensures that the aPragmaName[] table is arranged in
104303** lexicographical order to facility a binary search of the pragma name.
104304** Do not edit pragma.h directly.  Edit and rerun the script in at
104305** ../tool/mkpragmatab.tcl. */
104306/************** Include pragma.h in the middle of pragma.c *******************/
104307/************** Begin file pragma.h ******************************************/
104308/* DO NOT EDIT!
104309** This file is automatically generated by the script at
104310** ../tool/mkpragmatab.tcl.  To update the set of pragmas, edit
104311** that script and rerun it.
104312*/
104313#define PragTyp_HEADER_VALUE                   0
104314#define PragTyp_AUTO_VACUUM                    1
104315#define PragTyp_FLAG                           2
104316#define PragTyp_BUSY_TIMEOUT                   3
104317#define PragTyp_CACHE_SIZE                     4
104318#define PragTyp_CASE_SENSITIVE_LIKE            5
104319#define PragTyp_COLLATION_LIST                 6
104320#define PragTyp_COMPILE_OPTIONS                7
104321#define PragTyp_DATA_STORE_DIRECTORY           8
104322#define PragTyp_DATABASE_LIST                  9
104323#define PragTyp_DEFAULT_CACHE_SIZE            10
104324#define PragTyp_ENCODING                      11
104325#define PragTyp_FOREIGN_KEY_CHECK             12
104326#define PragTyp_FOREIGN_KEY_LIST              13
104327#define PragTyp_INCREMENTAL_VACUUM            14
104328#define PragTyp_INDEX_INFO                    15
104329#define PragTyp_INDEX_LIST                    16
104330#define PragTyp_INTEGRITY_CHECK               17
104331#define PragTyp_JOURNAL_MODE                  18
104332#define PragTyp_JOURNAL_SIZE_LIMIT            19
104333#define PragTyp_LOCK_PROXY_FILE               20
104334#define PragTyp_LOCKING_MODE                  21
104335#define PragTyp_PAGE_COUNT                    22
104336#define PragTyp_MMAP_SIZE                     23
104337#define PragTyp_PAGE_SIZE                     24
104338#define PragTyp_SECURE_DELETE                 25
104339#define PragTyp_SHRINK_MEMORY                 26
104340#define PragTyp_SOFT_HEAP_LIMIT               27
104341#define PragTyp_STATS                         28
104342#define PragTyp_SYNCHRONOUS                   29
104343#define PragTyp_TABLE_INFO                    30
104344#define PragTyp_TEMP_STORE                    31
104345#define PragTyp_TEMP_STORE_DIRECTORY          32
104346#define PragTyp_THREADS                       33
104347#define PragTyp_WAL_AUTOCHECKPOINT            34
104348#define PragTyp_WAL_CHECKPOINT                35
104349#define PragTyp_ACTIVATE_EXTENSIONS           36
104350#define PragTyp_HEXKEY                        37
104351#define PragTyp_KEY                           38
104352#define PragTyp_REKEY                         39
104353#define PragTyp_LOCK_STATUS                   40
104354#define PragTyp_PARSER_TRACE                  41
104355#define PragFlag_NeedSchema           0x01
104356#define PragFlag_ReadOnly             0x02
104357static const struct sPragmaNames {
104358  const char *const zName;  /* Name of pragma */
104359  u8 ePragTyp;              /* PragTyp_XXX value */
104360  u8 mPragFlag;             /* Zero or more PragFlag_XXX values */
104361  u32 iArg;                 /* Extra argument */
104362} aPragmaNames[] = {
104363#if defined(SQLITE_HAS_CODEC) || defined(SQLITE_ENABLE_CEROD)
104364  { /* zName:     */ "activate_extensions",
104365    /* ePragTyp:  */ PragTyp_ACTIVATE_EXTENSIONS,
104366    /* ePragFlag: */ 0,
104367    /* iArg:      */ 0 },
104368#endif
104369#if !defined(SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS)
104370  { /* zName:     */ "application_id",
104371    /* ePragTyp:  */ PragTyp_HEADER_VALUE,
104372    /* ePragFlag: */ 0,
104373    /* iArg:      */ BTREE_APPLICATION_ID },
104374#endif
104375#if !defined(SQLITE_OMIT_AUTOVACUUM)
104376  { /* zName:     */ "auto_vacuum",
104377    /* ePragTyp:  */ PragTyp_AUTO_VACUUM,
104378    /* ePragFlag: */ PragFlag_NeedSchema,
104379    /* iArg:      */ 0 },
104380#endif
104381#if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
104382#if !defined(SQLITE_OMIT_AUTOMATIC_INDEX)
104383  { /* zName:     */ "automatic_index",
104384    /* ePragTyp:  */ PragTyp_FLAG,
104385    /* ePragFlag: */ 0,
104386    /* iArg:      */ SQLITE_AutoIndex },
104387#endif
104388#endif
104389  { /* zName:     */ "busy_timeout",
104390    /* ePragTyp:  */ PragTyp_BUSY_TIMEOUT,
104391    /* ePragFlag: */ 0,
104392    /* iArg:      */ 0 },
104393#if !defined(SQLITE_OMIT_PAGER_PRAGMAS)
104394  { /* zName:     */ "cache_size",
104395    /* ePragTyp:  */ PragTyp_CACHE_SIZE,
104396    /* ePragFlag: */ 0,
104397    /* iArg:      */ 0 },
104398#endif
104399#if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
104400  { /* zName:     */ "cache_spill",
104401    /* ePragTyp:  */ PragTyp_FLAG,
104402    /* ePragFlag: */ 0,
104403    /* iArg:      */ SQLITE_CacheSpill },
104404#endif
104405  { /* zName:     */ "case_sensitive_like",
104406    /* ePragTyp:  */ PragTyp_CASE_SENSITIVE_LIKE,
104407    /* ePragFlag: */ 0,
104408    /* iArg:      */ 0 },
104409  { /* zName:     */ "cell_size_check",
104410    /* ePragTyp:  */ PragTyp_FLAG,
104411    /* ePragFlag: */ 0,
104412    /* iArg:      */ SQLITE_CellSizeCk },
104413#if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
104414  { /* zName:     */ "checkpoint_fullfsync",
104415    /* ePragTyp:  */ PragTyp_FLAG,
104416    /* ePragFlag: */ 0,
104417    /* iArg:      */ SQLITE_CkptFullFSync },
104418#endif
104419#if !defined(SQLITE_OMIT_SCHEMA_PRAGMAS)
104420  { /* zName:     */ "collation_list",
104421    /* ePragTyp:  */ PragTyp_COLLATION_LIST,
104422    /* ePragFlag: */ 0,
104423    /* iArg:      */ 0 },
104424#endif
104425#if !defined(SQLITE_OMIT_COMPILEOPTION_DIAGS)
104426  { /* zName:     */ "compile_options",
104427    /* ePragTyp:  */ PragTyp_COMPILE_OPTIONS,
104428    /* ePragFlag: */ 0,
104429    /* iArg:      */ 0 },
104430#endif
104431#if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
104432  { /* zName:     */ "count_changes",
104433    /* ePragTyp:  */ PragTyp_FLAG,
104434    /* ePragFlag: */ 0,
104435    /* iArg:      */ SQLITE_CountRows },
104436#endif
104437#if !defined(SQLITE_OMIT_PAGER_PRAGMAS) && SQLITE_OS_WIN
104438  { /* zName:     */ "data_store_directory",
104439    /* ePragTyp:  */ PragTyp_DATA_STORE_DIRECTORY,
104440    /* ePragFlag: */ 0,
104441    /* iArg:      */ 0 },
104442#endif
104443#if !defined(SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS)
104444  { /* zName:     */ "data_version",
104445    /* ePragTyp:  */ PragTyp_HEADER_VALUE,
104446    /* ePragFlag: */ PragFlag_ReadOnly,
104447    /* iArg:      */ BTREE_DATA_VERSION },
104448#endif
104449#if !defined(SQLITE_OMIT_SCHEMA_PRAGMAS)
104450  { /* zName:     */ "database_list",
104451    /* ePragTyp:  */ PragTyp_DATABASE_LIST,
104452    /* ePragFlag: */ PragFlag_NeedSchema,
104453    /* iArg:      */ 0 },
104454#endif
104455#if !defined(SQLITE_OMIT_PAGER_PRAGMAS) && !defined(SQLITE_OMIT_DEPRECATED)
104456  { /* zName:     */ "default_cache_size",
104457    /* ePragTyp:  */ PragTyp_DEFAULT_CACHE_SIZE,
104458    /* ePragFlag: */ PragFlag_NeedSchema,
104459    /* iArg:      */ 0 },
104460#endif
104461#if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
104462#if !defined(SQLITE_OMIT_FOREIGN_KEY) && !defined(SQLITE_OMIT_TRIGGER)
104463  { /* zName:     */ "defer_foreign_keys",
104464    /* ePragTyp:  */ PragTyp_FLAG,
104465    /* ePragFlag: */ 0,
104466    /* iArg:      */ SQLITE_DeferFKs },
104467#endif
104468#endif
104469#if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
104470  { /* zName:     */ "empty_result_callbacks",
104471    /* ePragTyp:  */ PragTyp_FLAG,
104472    /* ePragFlag: */ 0,
104473    /* iArg:      */ SQLITE_NullCallback },
104474#endif
104475#if !defined(SQLITE_OMIT_UTF16)
104476  { /* zName:     */ "encoding",
104477    /* ePragTyp:  */ PragTyp_ENCODING,
104478    /* ePragFlag: */ 0,
104479    /* iArg:      */ 0 },
104480#endif
104481#if !defined(SQLITE_OMIT_FOREIGN_KEY) && !defined(SQLITE_OMIT_TRIGGER)
104482  { /* zName:     */ "foreign_key_check",
104483    /* ePragTyp:  */ PragTyp_FOREIGN_KEY_CHECK,
104484    /* ePragFlag: */ PragFlag_NeedSchema,
104485    /* iArg:      */ 0 },
104486#endif
104487#if !defined(SQLITE_OMIT_FOREIGN_KEY)
104488  { /* zName:     */ "foreign_key_list",
104489    /* ePragTyp:  */ PragTyp_FOREIGN_KEY_LIST,
104490    /* ePragFlag: */ PragFlag_NeedSchema,
104491    /* iArg:      */ 0 },
104492#endif
104493#if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
104494#if !defined(SQLITE_OMIT_FOREIGN_KEY) && !defined(SQLITE_OMIT_TRIGGER)
104495  { /* zName:     */ "foreign_keys",
104496    /* ePragTyp:  */ PragTyp_FLAG,
104497    /* ePragFlag: */ 0,
104498    /* iArg:      */ SQLITE_ForeignKeys },
104499#endif
104500#endif
104501#if !defined(SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS)
104502  { /* zName:     */ "freelist_count",
104503    /* ePragTyp:  */ PragTyp_HEADER_VALUE,
104504    /* ePragFlag: */ PragFlag_ReadOnly,
104505    /* iArg:      */ BTREE_FREE_PAGE_COUNT },
104506#endif
104507#if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
104508  { /* zName:     */ "full_column_names",
104509    /* ePragTyp:  */ PragTyp_FLAG,
104510    /* ePragFlag: */ 0,
104511    /* iArg:      */ SQLITE_FullColNames },
104512  { /* zName:     */ "fullfsync",
104513    /* ePragTyp:  */ PragTyp_FLAG,
104514    /* ePragFlag: */ 0,
104515    /* iArg:      */ SQLITE_FullFSync },
104516#endif
104517#if defined(SQLITE_HAS_CODEC)
104518  { /* zName:     */ "hexkey",
104519    /* ePragTyp:  */ PragTyp_HEXKEY,
104520    /* ePragFlag: */ 0,
104521    /* iArg:      */ 0 },
104522  { /* zName:     */ "hexrekey",
104523    /* ePragTyp:  */ PragTyp_HEXKEY,
104524    /* ePragFlag: */ 0,
104525    /* iArg:      */ 0 },
104526#endif
104527#if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
104528#if !defined(SQLITE_OMIT_CHECK)
104529  { /* zName:     */ "ignore_check_constraints",
104530    /* ePragTyp:  */ PragTyp_FLAG,
104531    /* ePragFlag: */ 0,
104532    /* iArg:      */ SQLITE_IgnoreChecks },
104533#endif
104534#endif
104535#if !defined(SQLITE_OMIT_AUTOVACUUM)
104536  { /* zName:     */ "incremental_vacuum",
104537    /* ePragTyp:  */ PragTyp_INCREMENTAL_VACUUM,
104538    /* ePragFlag: */ PragFlag_NeedSchema,
104539    /* iArg:      */ 0 },
104540#endif
104541#if !defined(SQLITE_OMIT_SCHEMA_PRAGMAS)
104542  { /* zName:     */ "index_info",
104543    /* ePragTyp:  */ PragTyp_INDEX_INFO,
104544    /* ePragFlag: */ PragFlag_NeedSchema,
104545    /* iArg:      */ 0 },
104546  { /* zName:     */ "index_list",
104547    /* ePragTyp:  */ PragTyp_INDEX_LIST,
104548    /* ePragFlag: */ PragFlag_NeedSchema,
104549    /* iArg:      */ 0 },
104550  { /* zName:     */ "index_xinfo",
104551    /* ePragTyp:  */ PragTyp_INDEX_INFO,
104552    /* ePragFlag: */ PragFlag_NeedSchema,
104553    /* iArg:      */ 1 },
104554#endif
104555#if !defined(SQLITE_OMIT_INTEGRITY_CHECK)
104556  { /* zName:     */ "integrity_check",
104557    /* ePragTyp:  */ PragTyp_INTEGRITY_CHECK,
104558    /* ePragFlag: */ PragFlag_NeedSchema,
104559    /* iArg:      */ 0 },
104560#endif
104561#if !defined(SQLITE_OMIT_PAGER_PRAGMAS)
104562  { /* zName:     */ "journal_mode",
104563    /* ePragTyp:  */ PragTyp_JOURNAL_MODE,
104564    /* ePragFlag: */ PragFlag_NeedSchema,
104565    /* iArg:      */ 0 },
104566  { /* zName:     */ "journal_size_limit",
104567    /* ePragTyp:  */ PragTyp_JOURNAL_SIZE_LIMIT,
104568    /* ePragFlag: */ 0,
104569    /* iArg:      */ 0 },
104570#endif
104571#if defined(SQLITE_HAS_CODEC)
104572  { /* zName:     */ "key",
104573    /* ePragTyp:  */ PragTyp_KEY,
104574    /* ePragFlag: */ 0,
104575    /* iArg:      */ 0 },
104576#endif
104577#if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
104578  { /* zName:     */ "legacy_file_format",
104579    /* ePragTyp:  */ PragTyp_FLAG,
104580    /* ePragFlag: */ 0,
104581    /* iArg:      */ SQLITE_LegacyFileFmt },
104582#endif
104583#if !defined(SQLITE_OMIT_PAGER_PRAGMAS) && SQLITE_ENABLE_LOCKING_STYLE
104584  { /* zName:     */ "lock_proxy_file",
104585    /* ePragTyp:  */ PragTyp_LOCK_PROXY_FILE,
104586    /* ePragFlag: */ 0,
104587    /* iArg:      */ 0 },
104588#endif
104589#if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
104590  { /* zName:     */ "lock_status",
104591    /* ePragTyp:  */ PragTyp_LOCK_STATUS,
104592    /* ePragFlag: */ 0,
104593    /* iArg:      */ 0 },
104594#endif
104595#if !defined(SQLITE_OMIT_PAGER_PRAGMAS)
104596  { /* zName:     */ "locking_mode",
104597    /* ePragTyp:  */ PragTyp_LOCKING_MODE,
104598    /* ePragFlag: */ 0,
104599    /* iArg:      */ 0 },
104600  { /* zName:     */ "max_page_count",
104601    /* ePragTyp:  */ PragTyp_PAGE_COUNT,
104602    /* ePragFlag: */ PragFlag_NeedSchema,
104603    /* iArg:      */ 0 },
104604  { /* zName:     */ "mmap_size",
104605    /* ePragTyp:  */ PragTyp_MMAP_SIZE,
104606    /* ePragFlag: */ 0,
104607    /* iArg:      */ 0 },
104608  { /* zName:     */ "page_count",
104609    /* ePragTyp:  */ PragTyp_PAGE_COUNT,
104610    /* ePragFlag: */ PragFlag_NeedSchema,
104611    /* iArg:      */ 0 },
104612  { /* zName:     */ "page_size",
104613    /* ePragTyp:  */ PragTyp_PAGE_SIZE,
104614    /* ePragFlag: */ 0,
104615    /* iArg:      */ 0 },
104616#endif
104617#if defined(SQLITE_DEBUG)
104618  { /* zName:     */ "parser_trace",
104619    /* ePragTyp:  */ PragTyp_PARSER_TRACE,
104620    /* ePragFlag: */ 0,
104621    /* iArg:      */ 0 },
104622#endif
104623#if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
104624  { /* zName:     */ "query_only",
104625    /* ePragTyp:  */ PragTyp_FLAG,
104626    /* ePragFlag: */ 0,
104627    /* iArg:      */ SQLITE_QueryOnly },
104628#endif
104629#if !defined(SQLITE_OMIT_INTEGRITY_CHECK)
104630  { /* zName:     */ "quick_check",
104631    /* ePragTyp:  */ PragTyp_INTEGRITY_CHECK,
104632    /* ePragFlag: */ PragFlag_NeedSchema,
104633    /* iArg:      */ 0 },
104634#endif
104635#if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
104636  { /* zName:     */ "read_uncommitted",
104637    /* ePragTyp:  */ PragTyp_FLAG,
104638    /* ePragFlag: */ 0,
104639    /* iArg:      */ SQLITE_ReadUncommitted },
104640  { /* zName:     */ "recursive_triggers",
104641    /* ePragTyp:  */ PragTyp_FLAG,
104642    /* ePragFlag: */ 0,
104643    /* iArg:      */ SQLITE_RecTriggers },
104644#endif
104645#if defined(SQLITE_HAS_CODEC)
104646  { /* zName:     */ "rekey",
104647    /* ePragTyp:  */ PragTyp_REKEY,
104648    /* ePragFlag: */ 0,
104649    /* iArg:      */ 0 },
104650#endif
104651#if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
104652  { /* zName:     */ "reverse_unordered_selects",
104653    /* ePragTyp:  */ PragTyp_FLAG,
104654    /* ePragFlag: */ 0,
104655    /* iArg:      */ SQLITE_ReverseOrder },
104656#endif
104657#if !defined(SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS)
104658  { /* zName:     */ "schema_version",
104659    /* ePragTyp:  */ PragTyp_HEADER_VALUE,
104660    /* ePragFlag: */ 0,
104661    /* iArg:      */ BTREE_SCHEMA_VERSION },
104662#endif
104663#if !defined(SQLITE_OMIT_PAGER_PRAGMAS)
104664  { /* zName:     */ "secure_delete",
104665    /* ePragTyp:  */ PragTyp_SECURE_DELETE,
104666    /* ePragFlag: */ 0,
104667    /* iArg:      */ 0 },
104668#endif
104669#if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
104670  { /* zName:     */ "short_column_names",
104671    /* ePragTyp:  */ PragTyp_FLAG,
104672    /* ePragFlag: */ 0,
104673    /* iArg:      */ SQLITE_ShortColNames },
104674#endif
104675  { /* zName:     */ "shrink_memory",
104676    /* ePragTyp:  */ PragTyp_SHRINK_MEMORY,
104677    /* ePragFlag: */ 0,
104678    /* iArg:      */ 0 },
104679  { /* zName:     */ "soft_heap_limit",
104680    /* ePragTyp:  */ PragTyp_SOFT_HEAP_LIMIT,
104681    /* ePragFlag: */ 0,
104682    /* iArg:      */ 0 },
104683#if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
104684#if defined(SQLITE_DEBUG)
104685  { /* zName:     */ "sql_trace",
104686    /* ePragTyp:  */ PragTyp_FLAG,
104687    /* ePragFlag: */ 0,
104688    /* iArg:      */ SQLITE_SqlTrace },
104689#endif
104690#endif
104691#if !defined(SQLITE_OMIT_SCHEMA_PRAGMAS)
104692  { /* zName:     */ "stats",
104693    /* ePragTyp:  */ PragTyp_STATS,
104694    /* ePragFlag: */ PragFlag_NeedSchema,
104695    /* iArg:      */ 0 },
104696#endif
104697#if !defined(SQLITE_OMIT_PAGER_PRAGMAS)
104698  { /* zName:     */ "synchronous",
104699    /* ePragTyp:  */ PragTyp_SYNCHRONOUS,
104700    /* ePragFlag: */ PragFlag_NeedSchema,
104701    /* iArg:      */ 0 },
104702#endif
104703#if !defined(SQLITE_OMIT_SCHEMA_PRAGMAS)
104704  { /* zName:     */ "table_info",
104705    /* ePragTyp:  */ PragTyp_TABLE_INFO,
104706    /* ePragFlag: */ PragFlag_NeedSchema,
104707    /* iArg:      */ 0 },
104708#endif
104709#if !defined(SQLITE_OMIT_PAGER_PRAGMAS)
104710  { /* zName:     */ "temp_store",
104711    /* ePragTyp:  */ PragTyp_TEMP_STORE,
104712    /* ePragFlag: */ 0,
104713    /* iArg:      */ 0 },
104714  { /* zName:     */ "temp_store_directory",
104715    /* ePragTyp:  */ PragTyp_TEMP_STORE_DIRECTORY,
104716    /* ePragFlag: */ 0,
104717    /* iArg:      */ 0 },
104718#endif
104719  { /* zName:     */ "threads",
104720    /* ePragTyp:  */ PragTyp_THREADS,
104721    /* ePragFlag: */ 0,
104722    /* iArg:      */ 0 },
104723#if !defined(SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS)
104724  { /* zName:     */ "user_version",
104725    /* ePragTyp:  */ PragTyp_HEADER_VALUE,
104726    /* ePragFlag: */ 0,
104727    /* iArg:      */ BTREE_USER_VERSION },
104728#endif
104729#if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
104730#if defined(SQLITE_DEBUG)
104731  { /* zName:     */ "vdbe_addoptrace",
104732    /* ePragTyp:  */ PragTyp_FLAG,
104733    /* ePragFlag: */ 0,
104734    /* iArg:      */ SQLITE_VdbeAddopTrace },
104735  { /* zName:     */ "vdbe_debug",
104736    /* ePragTyp:  */ PragTyp_FLAG,
104737    /* ePragFlag: */ 0,
104738    /* iArg:      */ SQLITE_SqlTrace|SQLITE_VdbeListing|SQLITE_VdbeTrace },
104739  { /* zName:     */ "vdbe_eqp",
104740    /* ePragTyp:  */ PragTyp_FLAG,
104741    /* ePragFlag: */ 0,
104742    /* iArg:      */ SQLITE_VdbeEQP },
104743  { /* zName:     */ "vdbe_listing",
104744    /* ePragTyp:  */ PragTyp_FLAG,
104745    /* ePragFlag: */ 0,
104746    /* iArg:      */ SQLITE_VdbeListing },
104747  { /* zName:     */ "vdbe_trace",
104748    /* ePragTyp:  */ PragTyp_FLAG,
104749    /* ePragFlag: */ 0,
104750    /* iArg:      */ SQLITE_VdbeTrace },
104751#endif
104752#endif
104753#if !defined(SQLITE_OMIT_WAL)
104754  { /* zName:     */ "wal_autocheckpoint",
104755    /* ePragTyp:  */ PragTyp_WAL_AUTOCHECKPOINT,
104756    /* ePragFlag: */ 0,
104757    /* iArg:      */ 0 },
104758  { /* zName:     */ "wal_checkpoint",
104759    /* ePragTyp:  */ PragTyp_WAL_CHECKPOINT,
104760    /* ePragFlag: */ PragFlag_NeedSchema,
104761    /* iArg:      */ 0 },
104762#endif
104763#if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
104764  { /* zName:     */ "writable_schema",
104765    /* ePragTyp:  */ PragTyp_FLAG,
104766    /* ePragFlag: */ 0,
104767    /* iArg:      */ SQLITE_WriteSchema|SQLITE_RecoveryMode },
104768#endif
104769};
104770/* Number of pragmas: 60 on by default, 73 total. */
104771
104772/************** End of pragma.h **********************************************/
104773/************** Continuing where we left off in pragma.c *********************/
104774
104775/*
104776** Interpret the given string as a safety level.  Return 0 for OFF,
104777** 1 for ON or NORMAL and 2 for FULL.  Return 1 for an empty or
104778** unrecognized string argument.  The FULL option is disallowed
104779** if the omitFull parameter it 1.
104780**
104781** Note that the values returned are one less that the values that
104782** should be passed into sqlite3BtreeSetSafetyLevel().  The is done
104783** to support legacy SQL code.  The safety level used to be boolean
104784** and older scripts may have used numbers 0 for OFF and 1 for ON.
104785*/
104786static u8 getSafetyLevel(const char *z, int omitFull, u8 dflt){
104787                             /* 123456789 123456789 */
104788  static const char zText[] = "onoffalseyestruefull";
104789  static const u8 iOffset[] = {0, 1, 2, 4, 9, 12, 16};
104790  static const u8 iLength[] = {2, 2, 3, 5, 3, 4, 4};
104791  static const u8 iValue[] =  {1, 0, 0, 0, 1, 1, 2};
104792  int i, n;
104793  if( sqlite3Isdigit(*z) ){
104794    return (u8)sqlite3Atoi(z);
104795  }
104796  n = sqlite3Strlen30(z);
104797  for(i=0; i<ArraySize(iLength)-omitFull; i++){
104798    if( iLength[i]==n && sqlite3StrNICmp(&zText[iOffset[i]],z,n)==0 ){
104799      return iValue[i];
104800    }
104801  }
104802  return dflt;
104803}
104804
104805/*
104806** Interpret the given string as a boolean value.
104807*/
104808SQLITE_PRIVATE u8 sqlite3GetBoolean(const char *z, u8 dflt){
104809  return getSafetyLevel(z,1,dflt)!=0;
104810}
104811
104812/* The sqlite3GetBoolean() function is used by other modules but the
104813** remainder of this file is specific to PRAGMA processing.  So omit
104814** the rest of the file if PRAGMAs are omitted from the build.
104815*/
104816#if !defined(SQLITE_OMIT_PRAGMA)
104817
104818/*
104819** Interpret the given string as a locking mode value.
104820*/
104821static int getLockingMode(const char *z){
104822  if( z ){
104823    if( 0==sqlite3StrICmp(z, "exclusive") ) return PAGER_LOCKINGMODE_EXCLUSIVE;
104824    if( 0==sqlite3StrICmp(z, "normal") ) return PAGER_LOCKINGMODE_NORMAL;
104825  }
104826  return PAGER_LOCKINGMODE_QUERY;
104827}
104828
104829#ifndef SQLITE_OMIT_AUTOVACUUM
104830/*
104831** Interpret the given string as an auto-vacuum mode value.
104832**
104833** The following strings, "none", "full" and "incremental" are
104834** acceptable, as are their numeric equivalents: 0, 1 and 2 respectively.
104835*/
104836static int getAutoVacuum(const char *z){
104837  int i;
104838  if( 0==sqlite3StrICmp(z, "none") ) return BTREE_AUTOVACUUM_NONE;
104839  if( 0==sqlite3StrICmp(z, "full") ) return BTREE_AUTOVACUUM_FULL;
104840  if( 0==sqlite3StrICmp(z, "incremental") ) return BTREE_AUTOVACUUM_INCR;
104841  i = sqlite3Atoi(z);
104842  return (u8)((i>=0&&i<=2)?i:0);
104843}
104844#endif /* ifndef SQLITE_OMIT_AUTOVACUUM */
104845
104846#ifndef SQLITE_OMIT_PAGER_PRAGMAS
104847/*
104848** Interpret the given string as a temp db location. Return 1 for file
104849** backed temporary databases, 2 for the Red-Black tree in memory database
104850** and 0 to use the compile-time default.
104851*/
104852static int getTempStore(const char *z){
104853  if( z[0]>='0' && z[0]<='2' ){
104854    return z[0] - '0';
104855  }else if( sqlite3StrICmp(z, "file")==0 ){
104856    return 1;
104857  }else if( sqlite3StrICmp(z, "memory")==0 ){
104858    return 2;
104859  }else{
104860    return 0;
104861  }
104862}
104863#endif /* SQLITE_PAGER_PRAGMAS */
104864
104865#ifndef SQLITE_OMIT_PAGER_PRAGMAS
104866/*
104867** Invalidate temp storage, either when the temp storage is changed
104868** from default, or when 'file' and the temp_store_directory has changed
104869*/
104870static int invalidateTempStorage(Parse *pParse){
104871  sqlite3 *db = pParse->db;
104872  if( db->aDb[1].pBt!=0 ){
104873    if( !db->autoCommit || sqlite3BtreeIsInReadTrans(db->aDb[1].pBt) ){
104874      sqlite3ErrorMsg(pParse, "temporary storage cannot be changed "
104875        "from within a transaction");
104876      return SQLITE_ERROR;
104877    }
104878    sqlite3BtreeClose(db->aDb[1].pBt);
104879    db->aDb[1].pBt = 0;
104880    sqlite3ResetAllSchemasOfConnection(db);
104881  }
104882  return SQLITE_OK;
104883}
104884#endif /* SQLITE_PAGER_PRAGMAS */
104885
104886#ifndef SQLITE_OMIT_PAGER_PRAGMAS
104887/*
104888** If the TEMP database is open, close it and mark the database schema
104889** as needing reloading.  This must be done when using the SQLITE_TEMP_STORE
104890** or DEFAULT_TEMP_STORE pragmas.
104891*/
104892static int changeTempStorage(Parse *pParse, const char *zStorageType){
104893  int ts = getTempStore(zStorageType);
104894  sqlite3 *db = pParse->db;
104895  if( db->temp_store==ts ) return SQLITE_OK;
104896  if( invalidateTempStorage( pParse ) != SQLITE_OK ){
104897    return SQLITE_ERROR;
104898  }
104899  db->temp_store = (u8)ts;
104900  return SQLITE_OK;
104901}
104902#endif /* SQLITE_PAGER_PRAGMAS */
104903
104904/*
104905** Generate code to return a single integer value.
104906*/
104907static void returnSingleInt(Parse *pParse, const char *zLabel, i64 value){
104908  Vdbe *v = sqlite3GetVdbe(pParse);
104909  int nMem = ++pParse->nMem;
104910  i64 *pI64 = sqlite3DbMallocRaw(pParse->db, sizeof(value));
104911  if( pI64 ){
104912    memcpy(pI64, &value, sizeof(value));
104913  }
104914  sqlite3VdbeAddOp4(v, OP_Int64, 0, nMem, 0, (char*)pI64, P4_INT64);
104915  sqlite3VdbeSetNumCols(v, 1);
104916  sqlite3VdbeSetColName(v, 0, COLNAME_NAME, zLabel, SQLITE_STATIC);
104917  sqlite3VdbeAddOp2(v, OP_ResultRow, nMem, 1);
104918}
104919
104920
104921/*
104922** Set the safety_level and pager flags for pager iDb.  Or if iDb<0
104923** set these values for all pagers.
104924*/
104925#ifndef SQLITE_OMIT_PAGER_PRAGMAS
104926static void setAllPagerFlags(sqlite3 *db){
104927  if( db->autoCommit ){
104928    Db *pDb = db->aDb;
104929    int n = db->nDb;
104930    assert( SQLITE_FullFSync==PAGER_FULLFSYNC );
104931    assert( SQLITE_CkptFullFSync==PAGER_CKPT_FULLFSYNC );
104932    assert( SQLITE_CacheSpill==PAGER_CACHESPILL );
104933    assert( (PAGER_FULLFSYNC | PAGER_CKPT_FULLFSYNC | PAGER_CACHESPILL)
104934             ==  PAGER_FLAGS_MASK );
104935    assert( (pDb->safety_level & PAGER_SYNCHRONOUS_MASK)==pDb->safety_level );
104936    while( (n--) > 0 ){
104937      if( pDb->pBt ){
104938        sqlite3BtreeSetPagerFlags(pDb->pBt,
104939                 pDb->safety_level | (db->flags & PAGER_FLAGS_MASK) );
104940      }
104941      pDb++;
104942    }
104943  }
104944}
104945#else
104946# define setAllPagerFlags(X)  /* no-op */
104947#endif
104948
104949
104950/*
104951** Return a human-readable name for a constraint resolution action.
104952*/
104953#ifndef SQLITE_OMIT_FOREIGN_KEY
104954static const char *actionName(u8 action){
104955  const char *zName;
104956  switch( action ){
104957    case OE_SetNull:  zName = "SET NULL";        break;
104958    case OE_SetDflt:  zName = "SET DEFAULT";     break;
104959    case OE_Cascade:  zName = "CASCADE";         break;
104960    case OE_Restrict: zName = "RESTRICT";        break;
104961    default:          zName = "NO ACTION";
104962                      assert( action==OE_None ); break;
104963  }
104964  return zName;
104965}
104966#endif
104967
104968
104969/*
104970** Parameter eMode must be one of the PAGER_JOURNALMODE_XXX constants
104971** defined in pager.h. This function returns the associated lowercase
104972** journal-mode name.
104973*/
104974SQLITE_PRIVATE const char *sqlite3JournalModename(int eMode){
104975  static char * const azModeName[] = {
104976    "delete", "persist", "off", "truncate", "memory"
104977#ifndef SQLITE_OMIT_WAL
104978     , "wal"
104979#endif
104980  };
104981  assert( PAGER_JOURNALMODE_DELETE==0 );
104982  assert( PAGER_JOURNALMODE_PERSIST==1 );
104983  assert( PAGER_JOURNALMODE_OFF==2 );
104984  assert( PAGER_JOURNALMODE_TRUNCATE==3 );
104985  assert( PAGER_JOURNALMODE_MEMORY==4 );
104986  assert( PAGER_JOURNALMODE_WAL==5 );
104987  assert( eMode>=0 && eMode<=ArraySize(azModeName) );
104988
104989  if( eMode==ArraySize(azModeName) ) return 0;
104990  return azModeName[eMode];
104991}
104992
104993/*
104994** Process a pragma statement.
104995**
104996** Pragmas are of this form:
104997**
104998**      PRAGMA [database.]id [= value]
104999**
105000** The identifier might also be a string.  The value is a string, and
105001** identifier, or a number.  If minusFlag is true, then the value is
105002** a number that was preceded by a minus sign.
105003**
105004** If the left side is "database.id" then pId1 is the database name
105005** and pId2 is the id.  If the left side is just "id" then pId1 is the
105006** id and pId2 is any empty string.
105007*/
105008SQLITE_PRIVATE void sqlite3Pragma(
105009  Parse *pParse,
105010  Token *pId1,        /* First part of [database.]id field */
105011  Token *pId2,        /* Second part of [database.]id field, or NULL */
105012  Token *pValue,      /* Token for <value>, or NULL */
105013  int minusFlag       /* True if a '-' sign preceded <value> */
105014){
105015  char *zLeft = 0;       /* Nul-terminated UTF-8 string <id> */
105016  char *zRight = 0;      /* Nul-terminated UTF-8 string <value>, or NULL */
105017  const char *zDb = 0;   /* The database name */
105018  Token *pId;            /* Pointer to <id> token */
105019  char *aFcntl[4];       /* Argument to SQLITE_FCNTL_PRAGMA */
105020  int iDb;               /* Database index for <database> */
105021  int lwr, upr, mid = 0;       /* Binary search bounds */
105022  int rc;                      /* return value form SQLITE_FCNTL_PRAGMA */
105023  sqlite3 *db = pParse->db;    /* The database connection */
105024  Db *pDb;                     /* The specific database being pragmaed */
105025  Vdbe *v = sqlite3GetVdbe(pParse);  /* Prepared statement */
105026  const struct sPragmaNames *pPragma;
105027
105028  if( v==0 ) return;
105029  sqlite3VdbeRunOnlyOnce(v);
105030  pParse->nMem = 2;
105031
105032  /* Interpret the [database.] part of the pragma statement. iDb is the
105033  ** index of the database this pragma is being applied to in db.aDb[]. */
105034  iDb = sqlite3TwoPartName(pParse, pId1, pId2, &pId);
105035  if( iDb<0 ) return;
105036  pDb = &db->aDb[iDb];
105037
105038  /* If the temp database has been explicitly named as part of the
105039  ** pragma, make sure it is open.
105040  */
105041  if( iDb==1 && sqlite3OpenTempDatabase(pParse) ){
105042    return;
105043  }
105044
105045  zLeft = sqlite3NameFromToken(db, pId);
105046  if( !zLeft ) return;
105047  if( minusFlag ){
105048    zRight = sqlite3MPrintf(db, "-%T", pValue);
105049  }else{
105050    zRight = sqlite3NameFromToken(db, pValue);
105051  }
105052
105053  assert( pId2 );
105054  zDb = pId2->n>0 ? pDb->zName : 0;
105055  if( sqlite3AuthCheck(pParse, SQLITE_PRAGMA, zLeft, zRight, zDb) ){
105056    goto pragma_out;
105057  }
105058
105059  /* Send an SQLITE_FCNTL_PRAGMA file-control to the underlying VFS
105060  ** connection.  If it returns SQLITE_OK, then assume that the VFS
105061  ** handled the pragma and generate a no-op prepared statement.
105062  **
105063  ** IMPLEMENTATION-OF: R-12238-55120 Whenever a PRAGMA statement is parsed,
105064  ** an SQLITE_FCNTL_PRAGMA file control is sent to the open sqlite3_file
105065  ** object corresponding to the database file to which the pragma
105066  ** statement refers.
105067  **
105068  ** IMPLEMENTATION-OF: R-29875-31678 The argument to the SQLITE_FCNTL_PRAGMA
105069  ** file control is an array of pointers to strings (char**) in which the
105070  ** second element of the array is the name of the pragma and the third
105071  ** element is the argument to the pragma or NULL if the pragma has no
105072  ** argument.
105073  */
105074  aFcntl[0] = 0;
105075  aFcntl[1] = zLeft;
105076  aFcntl[2] = zRight;
105077  aFcntl[3] = 0;
105078  db->busyHandler.nBusy = 0;
105079  rc = sqlite3_file_control(db, zDb, SQLITE_FCNTL_PRAGMA, (void*)aFcntl);
105080  if( rc==SQLITE_OK ){
105081    if( aFcntl[0] ){
105082      int nMem = ++pParse->nMem;
105083      sqlite3VdbeAddOp4(v, OP_String8, 0, nMem, 0, aFcntl[0], 0);
105084      sqlite3VdbeSetNumCols(v, 1);
105085      sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "result", SQLITE_STATIC);
105086      sqlite3VdbeAddOp2(v, OP_ResultRow, nMem, 1);
105087      sqlite3_free(aFcntl[0]);
105088    }
105089    goto pragma_out;
105090  }
105091  if( rc!=SQLITE_NOTFOUND ){
105092    if( aFcntl[0] ){
105093      sqlite3ErrorMsg(pParse, "%s", aFcntl[0]);
105094      sqlite3_free(aFcntl[0]);
105095    }
105096    pParse->nErr++;
105097    pParse->rc = rc;
105098    goto pragma_out;
105099  }
105100
105101  /* Locate the pragma in the lookup table */
105102  lwr = 0;
105103  upr = ArraySize(aPragmaNames)-1;
105104  while( lwr<=upr ){
105105    mid = (lwr+upr)/2;
105106    rc = sqlite3_stricmp(zLeft, aPragmaNames[mid].zName);
105107    if( rc==0 ) break;
105108    if( rc<0 ){
105109      upr = mid - 1;
105110    }else{
105111      lwr = mid + 1;
105112    }
105113  }
105114  if( lwr>upr ) goto pragma_out;
105115  pPragma = &aPragmaNames[mid];
105116
105117  /* Make sure the database schema is loaded if the pragma requires that */
105118  if( (pPragma->mPragFlag & PragFlag_NeedSchema)!=0 ){
105119    if( sqlite3ReadSchema(pParse) ) goto pragma_out;
105120  }
105121
105122  /* Jump to the appropriate pragma handler */
105123  switch( pPragma->ePragTyp ){
105124
105125#if !defined(SQLITE_OMIT_PAGER_PRAGMAS) && !defined(SQLITE_OMIT_DEPRECATED)
105126  /*
105127  **  PRAGMA [database.]default_cache_size
105128  **  PRAGMA [database.]default_cache_size=N
105129  **
105130  ** The first form reports the current persistent setting for the
105131  ** page cache size.  The value returned is the maximum number of
105132  ** pages in the page cache.  The second form sets both the current
105133  ** page cache size value and the persistent page cache size value
105134  ** stored in the database file.
105135  **
105136  ** Older versions of SQLite would set the default cache size to a
105137  ** negative number to indicate synchronous=OFF.  These days, synchronous
105138  ** is always on by default regardless of the sign of the default cache
105139  ** size.  But continue to take the absolute value of the default cache
105140  ** size of historical compatibility.
105141  */
105142  case PragTyp_DEFAULT_CACHE_SIZE: {
105143    static const int iLn = VDBE_OFFSET_LINENO(2);
105144    static const VdbeOpList getCacheSize[] = {
105145      { OP_Transaction, 0, 0,        0},                         /* 0 */
105146      { OP_ReadCookie,  0, 1,        BTREE_DEFAULT_CACHE_SIZE},  /* 1 */
105147      { OP_IfPos,       1, 8,        0},
105148      { OP_Integer,     0, 2,        0},
105149      { OP_Subtract,    1, 2,        1},
105150      { OP_IfPos,       1, 8,        0},
105151      { OP_Integer,     0, 1,        0},                         /* 6 */
105152      { OP_Noop,        0, 0,        0},
105153      { OP_ResultRow,   1, 1,        0},
105154    };
105155    int addr;
105156    sqlite3VdbeUsesBtree(v, iDb);
105157    if( !zRight ){
105158      sqlite3VdbeSetNumCols(v, 1);
105159      sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "cache_size", SQLITE_STATIC);
105160      pParse->nMem += 2;
105161      addr = sqlite3VdbeAddOpList(v, ArraySize(getCacheSize), getCacheSize,iLn);
105162      sqlite3VdbeChangeP1(v, addr, iDb);
105163      sqlite3VdbeChangeP1(v, addr+1, iDb);
105164      sqlite3VdbeChangeP1(v, addr+6, SQLITE_DEFAULT_CACHE_SIZE);
105165    }else{
105166      int size = sqlite3AbsInt32(sqlite3Atoi(zRight));
105167      sqlite3BeginWriteOperation(pParse, 0, iDb);
105168      sqlite3VdbeAddOp2(v, OP_Integer, size, 1);
105169      sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, BTREE_DEFAULT_CACHE_SIZE, 1);
105170      assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
105171      pDb->pSchema->cache_size = size;
105172      sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size);
105173    }
105174    break;
105175  }
105176#endif /* !SQLITE_OMIT_PAGER_PRAGMAS && !SQLITE_OMIT_DEPRECATED */
105177
105178#if !defined(SQLITE_OMIT_PAGER_PRAGMAS)
105179  /*
105180  **  PRAGMA [database.]page_size
105181  **  PRAGMA [database.]page_size=N
105182  **
105183  ** The first form reports the current setting for the
105184  ** database page size in bytes.  The second form sets the
105185  ** database page size value.  The value can only be set if
105186  ** the database has not yet been created.
105187  */
105188  case PragTyp_PAGE_SIZE: {
105189    Btree *pBt = pDb->pBt;
105190    assert( pBt!=0 );
105191    if( !zRight ){
105192      int size = ALWAYS(pBt) ? sqlite3BtreeGetPageSize(pBt) : 0;
105193      returnSingleInt(pParse, "page_size", size);
105194    }else{
105195      /* Malloc may fail when setting the page-size, as there is an internal
105196      ** buffer that the pager module resizes using sqlite3_realloc().
105197      */
105198      db->nextPagesize = sqlite3Atoi(zRight);
105199      if( SQLITE_NOMEM==sqlite3BtreeSetPageSize(pBt, db->nextPagesize,-1,0) ){
105200        db->mallocFailed = 1;
105201      }
105202    }
105203    break;
105204  }
105205
105206  /*
105207  **  PRAGMA [database.]secure_delete
105208  **  PRAGMA [database.]secure_delete=ON/OFF
105209  **
105210  ** The first form reports the current setting for the
105211  ** secure_delete flag.  The second form changes the secure_delete
105212  ** flag setting and reports thenew value.
105213  */
105214  case PragTyp_SECURE_DELETE: {
105215    Btree *pBt = pDb->pBt;
105216    int b = -1;
105217    assert( pBt!=0 );
105218    if( zRight ){
105219      b = sqlite3GetBoolean(zRight, 0);
105220    }
105221    if( pId2->n==0 && b>=0 ){
105222      int ii;
105223      for(ii=0; ii<db->nDb; ii++){
105224        sqlite3BtreeSecureDelete(db->aDb[ii].pBt, b);
105225      }
105226    }
105227    b = sqlite3BtreeSecureDelete(pBt, b);
105228    returnSingleInt(pParse, "secure_delete", b);
105229    break;
105230  }
105231
105232  /*
105233  **  PRAGMA [database.]max_page_count
105234  **  PRAGMA [database.]max_page_count=N
105235  **
105236  ** The first form reports the current setting for the
105237  ** maximum number of pages in the database file.  The
105238  ** second form attempts to change this setting.  Both
105239  ** forms return the current setting.
105240  **
105241  ** The absolute value of N is used.  This is undocumented and might
105242  ** change.  The only purpose is to provide an easy way to test
105243  ** the sqlite3AbsInt32() function.
105244  **
105245  **  PRAGMA [database.]page_count
105246  **
105247  ** Return the number of pages in the specified database.
105248  */
105249  case PragTyp_PAGE_COUNT: {
105250    int iReg;
105251    sqlite3CodeVerifySchema(pParse, iDb);
105252    iReg = ++pParse->nMem;
105253    if( sqlite3Tolower(zLeft[0])=='p' ){
105254      sqlite3VdbeAddOp2(v, OP_Pagecount, iDb, iReg);
105255    }else{
105256      sqlite3VdbeAddOp3(v, OP_MaxPgcnt, iDb, iReg,
105257                        sqlite3AbsInt32(sqlite3Atoi(zRight)));
105258    }
105259    sqlite3VdbeAddOp2(v, OP_ResultRow, iReg, 1);
105260    sqlite3VdbeSetNumCols(v, 1);
105261    sqlite3VdbeSetColName(v, 0, COLNAME_NAME, zLeft, SQLITE_TRANSIENT);
105262    break;
105263  }
105264
105265  /*
105266  **  PRAGMA [database.]locking_mode
105267  **  PRAGMA [database.]locking_mode = (normal|exclusive)
105268  */
105269  case PragTyp_LOCKING_MODE: {
105270    const char *zRet = "normal";
105271    int eMode = getLockingMode(zRight);
105272
105273    if( pId2->n==0 && eMode==PAGER_LOCKINGMODE_QUERY ){
105274      /* Simple "PRAGMA locking_mode;" statement. This is a query for
105275      ** the current default locking mode (which may be different to
105276      ** the locking-mode of the main database).
105277      */
105278      eMode = db->dfltLockMode;
105279    }else{
105280      Pager *pPager;
105281      if( pId2->n==0 ){
105282        /* This indicates that no database name was specified as part
105283        ** of the PRAGMA command. In this case the locking-mode must be
105284        ** set on all attached databases, as well as the main db file.
105285        **
105286        ** Also, the sqlite3.dfltLockMode variable is set so that
105287        ** any subsequently attached databases also use the specified
105288        ** locking mode.
105289        */
105290        int ii;
105291        assert(pDb==&db->aDb[0]);
105292        for(ii=2; ii<db->nDb; ii++){
105293          pPager = sqlite3BtreePager(db->aDb[ii].pBt);
105294          sqlite3PagerLockingMode(pPager, eMode);
105295        }
105296        db->dfltLockMode = (u8)eMode;
105297      }
105298      pPager = sqlite3BtreePager(pDb->pBt);
105299      eMode = sqlite3PagerLockingMode(pPager, eMode);
105300    }
105301
105302    assert( eMode==PAGER_LOCKINGMODE_NORMAL
105303            || eMode==PAGER_LOCKINGMODE_EXCLUSIVE );
105304    if( eMode==PAGER_LOCKINGMODE_EXCLUSIVE ){
105305      zRet = "exclusive";
105306    }
105307    sqlite3VdbeSetNumCols(v, 1);
105308    sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "locking_mode", SQLITE_STATIC);
105309    sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, zRet, 0);
105310    sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
105311    break;
105312  }
105313
105314  /*
105315  **  PRAGMA [database.]journal_mode
105316  **  PRAGMA [database.]journal_mode =
105317  **                      (delete|persist|off|truncate|memory|wal|off)
105318  */
105319  case PragTyp_JOURNAL_MODE: {
105320    int eMode;        /* One of the PAGER_JOURNALMODE_XXX symbols */
105321    int ii;           /* Loop counter */
105322
105323    sqlite3VdbeSetNumCols(v, 1);
105324    sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "journal_mode", SQLITE_STATIC);
105325
105326    if( zRight==0 ){
105327      /* If there is no "=MODE" part of the pragma, do a query for the
105328      ** current mode */
105329      eMode = PAGER_JOURNALMODE_QUERY;
105330    }else{
105331      const char *zMode;
105332      int n = sqlite3Strlen30(zRight);
105333      for(eMode=0; (zMode = sqlite3JournalModename(eMode))!=0; eMode++){
105334        if( sqlite3StrNICmp(zRight, zMode, n)==0 ) break;
105335      }
105336      if( !zMode ){
105337        /* If the "=MODE" part does not match any known journal mode,
105338        ** then do a query */
105339        eMode = PAGER_JOURNALMODE_QUERY;
105340      }
105341    }
105342    if( eMode==PAGER_JOURNALMODE_QUERY && pId2->n==0 ){
105343      /* Convert "PRAGMA journal_mode" into "PRAGMA main.journal_mode" */
105344      iDb = 0;
105345      pId2->n = 1;
105346    }
105347    for(ii=db->nDb-1; ii>=0; ii--){
105348      if( db->aDb[ii].pBt && (ii==iDb || pId2->n==0) ){
105349        sqlite3VdbeUsesBtree(v, ii);
105350        sqlite3VdbeAddOp3(v, OP_JournalMode, ii, 1, eMode);
105351      }
105352    }
105353    sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
105354    break;
105355  }
105356
105357  /*
105358  **  PRAGMA [database.]journal_size_limit
105359  **  PRAGMA [database.]journal_size_limit=N
105360  **
105361  ** Get or set the size limit on rollback journal files.
105362  */
105363  case PragTyp_JOURNAL_SIZE_LIMIT: {
105364    Pager *pPager = sqlite3BtreePager(pDb->pBt);
105365    i64 iLimit = -2;
105366    if( zRight ){
105367      sqlite3DecOrHexToI64(zRight, &iLimit);
105368      if( iLimit<-1 ) iLimit = -1;
105369    }
105370    iLimit = sqlite3PagerJournalSizeLimit(pPager, iLimit);
105371    returnSingleInt(pParse, "journal_size_limit", iLimit);
105372    break;
105373  }
105374
105375#endif /* SQLITE_OMIT_PAGER_PRAGMAS */
105376
105377  /*
105378  **  PRAGMA [database.]auto_vacuum
105379  **  PRAGMA [database.]auto_vacuum=N
105380  **
105381  ** Get or set the value of the database 'auto-vacuum' parameter.
105382  ** The value is one of:  0 NONE 1 FULL 2 INCREMENTAL
105383  */
105384#ifndef SQLITE_OMIT_AUTOVACUUM
105385  case PragTyp_AUTO_VACUUM: {
105386    Btree *pBt = pDb->pBt;
105387    assert( pBt!=0 );
105388    if( !zRight ){
105389      returnSingleInt(pParse, "auto_vacuum", sqlite3BtreeGetAutoVacuum(pBt));
105390    }else{
105391      int eAuto = getAutoVacuum(zRight);
105392      assert( eAuto>=0 && eAuto<=2 );
105393      db->nextAutovac = (u8)eAuto;
105394      /* Call SetAutoVacuum() to set initialize the internal auto and
105395      ** incr-vacuum flags. This is required in case this connection
105396      ** creates the database file. It is important that it is created
105397      ** as an auto-vacuum capable db.
105398      */
105399      rc = sqlite3BtreeSetAutoVacuum(pBt, eAuto);
105400      if( rc==SQLITE_OK && (eAuto==1 || eAuto==2) ){
105401        /* When setting the auto_vacuum mode to either "full" or
105402        ** "incremental", write the value of meta[6] in the database
105403        ** file. Before writing to meta[6], check that meta[3] indicates
105404        ** that this really is an auto-vacuum capable database.
105405        */
105406        static const int iLn = VDBE_OFFSET_LINENO(2);
105407        static const VdbeOpList setMeta6[] = {
105408          { OP_Transaction,    0,         1,                 0},    /* 0 */
105409          { OP_ReadCookie,     0,         1,         BTREE_LARGEST_ROOT_PAGE},
105410          { OP_If,             1,         0,                 0},    /* 2 */
105411          { OP_Halt,           SQLITE_OK, OE_Abort,          0},    /* 3 */
105412          { OP_Integer,        0,         1,                 0},    /* 4 */
105413          { OP_SetCookie,      0,         BTREE_INCR_VACUUM, 1},    /* 5 */
105414        };
105415        int iAddr;
105416        iAddr = sqlite3VdbeAddOpList(v, ArraySize(setMeta6), setMeta6, iLn);
105417        sqlite3VdbeChangeP1(v, iAddr, iDb);
105418        sqlite3VdbeChangeP1(v, iAddr+1, iDb);
105419        sqlite3VdbeChangeP2(v, iAddr+2, iAddr+4);
105420        sqlite3VdbeChangeP1(v, iAddr+4, eAuto-1);
105421        sqlite3VdbeChangeP1(v, iAddr+5, iDb);
105422        sqlite3VdbeUsesBtree(v, iDb);
105423      }
105424    }
105425    break;
105426  }
105427#endif
105428
105429  /*
105430  **  PRAGMA [database.]incremental_vacuum(N)
105431  **
105432  ** Do N steps of incremental vacuuming on a database.
105433  */
105434#ifndef SQLITE_OMIT_AUTOVACUUM
105435  case PragTyp_INCREMENTAL_VACUUM: {
105436    int iLimit, addr;
105437    if( zRight==0 || !sqlite3GetInt32(zRight, &iLimit) || iLimit<=0 ){
105438      iLimit = 0x7fffffff;
105439    }
105440    sqlite3BeginWriteOperation(pParse, 0, iDb);
105441    sqlite3VdbeAddOp2(v, OP_Integer, iLimit, 1);
105442    addr = sqlite3VdbeAddOp1(v, OP_IncrVacuum, iDb); VdbeCoverage(v);
105443    sqlite3VdbeAddOp1(v, OP_ResultRow, 1);
105444    sqlite3VdbeAddOp2(v, OP_AddImm, 1, -1);
105445    sqlite3VdbeAddOp2(v, OP_IfPos, 1, addr); VdbeCoverage(v);
105446    sqlite3VdbeJumpHere(v, addr);
105447    break;
105448  }
105449#endif
105450
105451#ifndef SQLITE_OMIT_PAGER_PRAGMAS
105452  /*
105453  **  PRAGMA [database.]cache_size
105454  **  PRAGMA [database.]cache_size=N
105455  **
105456  ** The first form reports the current local setting for the
105457  ** page cache size. The second form sets the local
105458  ** page cache size value.  If N is positive then that is the
105459  ** number of pages in the cache.  If N is negative, then the
105460  ** number of pages is adjusted so that the cache uses -N kibibytes
105461  ** of memory.
105462  */
105463  case PragTyp_CACHE_SIZE: {
105464    assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
105465    if( !zRight ){
105466      if( sqlite3ReadSchema(pParse) ) goto pragma_out;
105467      returnSingleInt(pParse, "cache_size", pDb->pSchema->cache_size);
105468    }else{
105469      int size = sqlite3Atoi(zRight);
105470      pDb->pSchema->cache_size = size;
105471      sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size);
105472      if( sqlite3ReadSchema(pParse) ) goto pragma_out;
105473    }
105474    break;
105475  }
105476
105477  /*
105478  **  PRAGMA [database.]mmap_size(N)
105479  **
105480  ** Used to set mapping size limit. The mapping size limit is
105481  ** used to limit the aggregate size of all memory mapped regions of the
105482  ** database file. If this parameter is set to zero, then memory mapping
105483  ** is not used at all.  If N is negative, then the default memory map
105484  ** limit determined by sqlite3_config(SQLITE_CONFIG_MMAP_SIZE) is set.
105485  ** The parameter N is measured in bytes.
105486  **
105487  ** This value is advisory.  The underlying VFS is free to memory map
105488  ** as little or as much as it wants.  Except, if N is set to 0 then the
105489  ** upper layers will never invoke the xFetch interfaces to the VFS.
105490  */
105491  case PragTyp_MMAP_SIZE: {
105492    sqlite3_int64 sz;
105493#if SQLITE_MAX_MMAP_SIZE>0
105494    assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
105495    if( zRight ){
105496      int ii;
105497      sqlite3DecOrHexToI64(zRight, &sz);
105498      if( sz<0 ) sz = sqlite3GlobalConfig.szMmap;
105499      if( pId2->n==0 ) db->szMmap = sz;
105500      for(ii=db->nDb-1; ii>=0; ii--){
105501        if( db->aDb[ii].pBt && (ii==iDb || pId2->n==0) ){
105502          sqlite3BtreeSetMmapLimit(db->aDb[ii].pBt, sz);
105503        }
105504      }
105505    }
105506    sz = -1;
105507    rc = sqlite3_file_control(db, zDb, SQLITE_FCNTL_MMAP_SIZE, &sz);
105508#else
105509    sz = 0;
105510    rc = SQLITE_OK;
105511#endif
105512    if( rc==SQLITE_OK ){
105513      returnSingleInt(pParse, "mmap_size", sz);
105514    }else if( rc!=SQLITE_NOTFOUND ){
105515      pParse->nErr++;
105516      pParse->rc = rc;
105517    }
105518    break;
105519  }
105520
105521  /*
105522  **   PRAGMA temp_store
105523  **   PRAGMA temp_store = "default"|"memory"|"file"
105524  **
105525  ** Return or set the local value of the temp_store flag.  Changing
105526  ** the local value does not make changes to the disk file and the default
105527  ** value will be restored the next time the database is opened.
105528  **
105529  ** Note that it is possible for the library compile-time options to
105530  ** override this setting
105531  */
105532  case PragTyp_TEMP_STORE: {
105533    if( !zRight ){
105534      returnSingleInt(pParse, "temp_store", db->temp_store);
105535    }else{
105536      changeTempStorage(pParse, zRight);
105537    }
105538    break;
105539  }
105540
105541  /*
105542  **   PRAGMA temp_store_directory
105543  **   PRAGMA temp_store_directory = ""|"directory_name"
105544  **
105545  ** Return or set the local value of the temp_store_directory flag.  Changing
105546  ** the value sets a specific directory to be used for temporary files.
105547  ** Setting to a null string reverts to the default temporary directory search.
105548  ** If temporary directory is changed, then invalidateTempStorage.
105549  **
105550  */
105551  case PragTyp_TEMP_STORE_DIRECTORY: {
105552    if( !zRight ){
105553      if( sqlite3_temp_directory ){
105554        sqlite3VdbeSetNumCols(v, 1);
105555        sqlite3VdbeSetColName(v, 0, COLNAME_NAME,
105556            "temp_store_directory", SQLITE_STATIC);
105557        sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, sqlite3_temp_directory, 0);
105558        sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
105559      }
105560    }else{
105561#ifndef SQLITE_OMIT_WSD
105562      if( zRight[0] ){
105563        int res;
105564        rc = sqlite3OsAccess(db->pVfs, zRight, SQLITE_ACCESS_READWRITE, &res);
105565        if( rc!=SQLITE_OK || res==0 ){
105566          sqlite3ErrorMsg(pParse, "not a writable directory");
105567          goto pragma_out;
105568        }
105569      }
105570      if( SQLITE_TEMP_STORE==0
105571       || (SQLITE_TEMP_STORE==1 && db->temp_store<=1)
105572       || (SQLITE_TEMP_STORE==2 && db->temp_store==1)
105573      ){
105574        invalidateTempStorage(pParse);
105575      }
105576      sqlite3_free(sqlite3_temp_directory);
105577      if( zRight[0] ){
105578        sqlite3_temp_directory = sqlite3_mprintf("%s", zRight);
105579      }else{
105580        sqlite3_temp_directory = 0;
105581      }
105582#endif /* SQLITE_OMIT_WSD */
105583    }
105584    break;
105585  }
105586
105587#if SQLITE_OS_WIN
105588  /*
105589  **   PRAGMA data_store_directory
105590  **   PRAGMA data_store_directory = ""|"directory_name"
105591  **
105592  ** Return or set the local value of the data_store_directory flag.  Changing
105593  ** the value sets a specific directory to be used for database files that
105594  ** were specified with a relative pathname.  Setting to a null string reverts
105595  ** to the default database directory, which for database files specified with
105596  ** a relative path will probably be based on the current directory for the
105597  ** process.  Database file specified with an absolute path are not impacted
105598  ** by this setting, regardless of its value.
105599  **
105600  */
105601  case PragTyp_DATA_STORE_DIRECTORY: {
105602    if( !zRight ){
105603      if( sqlite3_data_directory ){
105604        sqlite3VdbeSetNumCols(v, 1);
105605        sqlite3VdbeSetColName(v, 0, COLNAME_NAME,
105606            "data_store_directory", SQLITE_STATIC);
105607        sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, sqlite3_data_directory, 0);
105608        sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
105609      }
105610    }else{
105611#ifndef SQLITE_OMIT_WSD
105612      if( zRight[0] ){
105613        int res;
105614        rc = sqlite3OsAccess(db->pVfs, zRight, SQLITE_ACCESS_READWRITE, &res);
105615        if( rc!=SQLITE_OK || res==0 ){
105616          sqlite3ErrorMsg(pParse, "not a writable directory");
105617          goto pragma_out;
105618        }
105619      }
105620      sqlite3_free(sqlite3_data_directory);
105621      if( zRight[0] ){
105622        sqlite3_data_directory = sqlite3_mprintf("%s", zRight);
105623      }else{
105624        sqlite3_data_directory = 0;
105625      }
105626#endif /* SQLITE_OMIT_WSD */
105627    }
105628    break;
105629  }
105630#endif
105631
105632#if SQLITE_ENABLE_LOCKING_STYLE
105633  /*
105634  **   PRAGMA [database.]lock_proxy_file
105635  **   PRAGMA [database.]lock_proxy_file = ":auto:"|"lock_file_path"
105636  **
105637  ** Return or set the value of the lock_proxy_file flag.  Changing
105638  ** the value sets a specific file to be used for database access locks.
105639  **
105640  */
105641  case PragTyp_LOCK_PROXY_FILE: {
105642    if( !zRight ){
105643      Pager *pPager = sqlite3BtreePager(pDb->pBt);
105644      char *proxy_file_path = NULL;
105645      sqlite3_file *pFile = sqlite3PagerFile(pPager);
105646      sqlite3OsFileControlHint(pFile, SQLITE_GET_LOCKPROXYFILE,
105647                           &proxy_file_path);
105648
105649      if( proxy_file_path ){
105650        sqlite3VdbeSetNumCols(v, 1);
105651        sqlite3VdbeSetColName(v, 0, COLNAME_NAME,
105652                              "lock_proxy_file", SQLITE_STATIC);
105653        sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, proxy_file_path, 0);
105654        sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
105655      }
105656    }else{
105657      Pager *pPager = sqlite3BtreePager(pDb->pBt);
105658      sqlite3_file *pFile = sqlite3PagerFile(pPager);
105659      int res;
105660      if( zRight[0] ){
105661        res=sqlite3OsFileControl(pFile, SQLITE_SET_LOCKPROXYFILE,
105662                                     zRight);
105663      } else {
105664        res=sqlite3OsFileControl(pFile, SQLITE_SET_LOCKPROXYFILE,
105665                                     NULL);
105666      }
105667      if( res!=SQLITE_OK ){
105668        sqlite3ErrorMsg(pParse, "failed to set lock proxy file");
105669        goto pragma_out;
105670      }
105671    }
105672    break;
105673  }
105674#endif /* SQLITE_ENABLE_LOCKING_STYLE */
105675
105676  /*
105677  **   PRAGMA [database.]synchronous
105678  **   PRAGMA [database.]synchronous=OFF|ON|NORMAL|FULL
105679  **
105680  ** Return or set the local value of the synchronous flag.  Changing
105681  ** the local value does not make changes to the disk file and the
105682  ** default value will be restored the next time the database is
105683  ** opened.
105684  */
105685  case PragTyp_SYNCHRONOUS: {
105686    if( !zRight ){
105687      returnSingleInt(pParse, "synchronous", pDb->safety_level-1);
105688    }else{
105689      if( !db->autoCommit ){
105690        sqlite3ErrorMsg(pParse,
105691            "Safety level may not be changed inside a transaction");
105692      }else{
105693        int iLevel = (getSafetyLevel(zRight,0,1)+1) & PAGER_SYNCHRONOUS_MASK;
105694        if( iLevel==0 ) iLevel = 1;
105695        pDb->safety_level = iLevel;
105696        setAllPagerFlags(db);
105697      }
105698    }
105699    break;
105700  }
105701#endif /* SQLITE_OMIT_PAGER_PRAGMAS */
105702
105703#ifndef SQLITE_OMIT_FLAG_PRAGMAS
105704  case PragTyp_FLAG: {
105705    if( zRight==0 ){
105706      returnSingleInt(pParse, pPragma->zName, (db->flags & pPragma->iArg)!=0 );
105707    }else{
105708      int mask = pPragma->iArg;    /* Mask of bits to set or clear. */
105709      if( db->autoCommit==0 ){
105710        /* Foreign key support may not be enabled or disabled while not
105711        ** in auto-commit mode.  */
105712        mask &= ~(SQLITE_ForeignKeys);
105713      }
105714#if SQLITE_USER_AUTHENTICATION
105715      if( db->auth.authLevel==UAUTH_User ){
105716        /* Do not allow non-admin users to modify the schema arbitrarily */
105717        mask &= ~(SQLITE_WriteSchema);
105718      }
105719#endif
105720
105721      if( sqlite3GetBoolean(zRight, 0) ){
105722        db->flags |= mask;
105723      }else{
105724        db->flags &= ~mask;
105725        if( mask==SQLITE_DeferFKs ) db->nDeferredImmCons = 0;
105726      }
105727
105728      /* Many of the flag-pragmas modify the code generated by the SQL
105729      ** compiler (eg. count_changes). So add an opcode to expire all
105730      ** compiled SQL statements after modifying a pragma value.
105731      */
105732      sqlite3VdbeAddOp2(v, OP_Expire, 0, 0);
105733      setAllPagerFlags(db);
105734    }
105735    break;
105736  }
105737#endif /* SQLITE_OMIT_FLAG_PRAGMAS */
105738
105739#ifndef SQLITE_OMIT_SCHEMA_PRAGMAS
105740  /*
105741  **   PRAGMA table_info(<table>)
105742  **
105743  ** Return a single row for each column of the named table. The columns of
105744  ** the returned data set are:
105745  **
105746  ** cid:        Column id (numbered from left to right, starting at 0)
105747  ** name:       Column name
105748  ** type:       Column declaration type.
105749  ** notnull:    True if 'NOT NULL' is part of column declaration
105750  ** dflt_value: The default value for the column, if any.
105751  */
105752  case PragTyp_TABLE_INFO: if( zRight ){
105753    Table *pTab;
105754    pTab = sqlite3FindTable(db, zRight, zDb);
105755    if( pTab ){
105756      int i, k;
105757      int nHidden = 0;
105758      Column *pCol;
105759      Index *pPk = sqlite3PrimaryKeyIndex(pTab);
105760      sqlite3VdbeSetNumCols(v, 6);
105761      pParse->nMem = 6;
105762      sqlite3CodeVerifySchema(pParse, iDb);
105763      sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "cid", SQLITE_STATIC);
105764      sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "name", SQLITE_STATIC);
105765      sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "type", SQLITE_STATIC);
105766      sqlite3VdbeSetColName(v, 3, COLNAME_NAME, "notnull", SQLITE_STATIC);
105767      sqlite3VdbeSetColName(v, 4, COLNAME_NAME, "dflt_value", SQLITE_STATIC);
105768      sqlite3VdbeSetColName(v, 5, COLNAME_NAME, "pk", SQLITE_STATIC);
105769      sqlite3ViewGetColumnNames(pParse, pTab);
105770      for(i=0, pCol=pTab->aCol; i<pTab->nCol; i++, pCol++){
105771        if( IsHiddenColumn(pCol) ){
105772          nHidden++;
105773          continue;
105774        }
105775        sqlite3VdbeAddOp2(v, OP_Integer, i-nHidden, 1);
105776        sqlite3VdbeAddOp4(v, OP_String8, 0, 2, 0, pCol->zName, 0);
105777        sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0,
105778           pCol->zType ? pCol->zType : "", 0);
105779        sqlite3VdbeAddOp2(v, OP_Integer, (pCol->notNull ? 1 : 0), 4);
105780        if( pCol->zDflt ){
105781          sqlite3VdbeAddOp4(v, OP_String8, 0, 5, 0, (char*)pCol->zDflt, 0);
105782        }else{
105783          sqlite3VdbeAddOp2(v, OP_Null, 0, 5);
105784        }
105785        if( (pCol->colFlags & COLFLAG_PRIMKEY)==0 ){
105786          k = 0;
105787        }else if( pPk==0 ){
105788          k = 1;
105789        }else{
105790          for(k=1; k<=pTab->nCol && pPk->aiColumn[k-1]!=i; k++){}
105791        }
105792        sqlite3VdbeAddOp2(v, OP_Integer, k, 6);
105793        sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 6);
105794      }
105795    }
105796  }
105797  break;
105798
105799  case PragTyp_STATS: {
105800    Index *pIdx;
105801    HashElem *i;
105802    v = sqlite3GetVdbe(pParse);
105803    sqlite3VdbeSetNumCols(v, 4);
105804    pParse->nMem = 4;
105805    sqlite3CodeVerifySchema(pParse, iDb);
105806    sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "table", SQLITE_STATIC);
105807    sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "index", SQLITE_STATIC);
105808    sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "width", SQLITE_STATIC);
105809    sqlite3VdbeSetColName(v, 3, COLNAME_NAME, "height", SQLITE_STATIC);
105810    for(i=sqliteHashFirst(&pDb->pSchema->tblHash); i; i=sqliteHashNext(i)){
105811      Table *pTab = sqliteHashData(i);
105812      sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, pTab->zName, 0);
105813      sqlite3VdbeAddOp2(v, OP_Null, 0, 2);
105814      sqlite3VdbeAddOp2(v, OP_Integer,
105815                           (int)sqlite3LogEstToInt(pTab->szTabRow), 3);
105816      sqlite3VdbeAddOp2(v, OP_Integer,
105817          (int)sqlite3LogEstToInt(pTab->nRowLogEst), 4);
105818      sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 4);
105819      for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
105820        sqlite3VdbeAddOp4(v, OP_String8, 0, 2, 0, pIdx->zName, 0);
105821        sqlite3VdbeAddOp2(v, OP_Integer,
105822                             (int)sqlite3LogEstToInt(pIdx->szIdxRow), 3);
105823        sqlite3VdbeAddOp2(v, OP_Integer,
105824            (int)sqlite3LogEstToInt(pIdx->aiRowLogEst[0]), 4);
105825        sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 4);
105826      }
105827    }
105828  }
105829  break;
105830
105831  case PragTyp_INDEX_INFO: if( zRight ){
105832    Index *pIdx;
105833    Table *pTab;
105834    pIdx = sqlite3FindIndex(db, zRight, zDb);
105835    if( pIdx ){
105836      int i;
105837      int mx;
105838      if( pPragma->iArg ){
105839        /* PRAGMA index_xinfo (newer version with more rows and columns) */
105840        mx = pIdx->nColumn;
105841        pParse->nMem = 6;
105842      }else{
105843        /* PRAGMA index_info (legacy version) */
105844        mx = pIdx->nKeyCol;
105845        pParse->nMem = 3;
105846      }
105847      pTab = pIdx->pTable;
105848      sqlite3VdbeSetNumCols(v, pParse->nMem);
105849      sqlite3CodeVerifySchema(pParse, iDb);
105850      sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "seqno", SQLITE_STATIC);
105851      sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "cid", SQLITE_STATIC);
105852      sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "name", SQLITE_STATIC);
105853      if( pPragma->iArg ){
105854        sqlite3VdbeSetColName(v, 3, COLNAME_NAME, "desc", SQLITE_STATIC);
105855        sqlite3VdbeSetColName(v, 4, COLNAME_NAME, "coll", SQLITE_STATIC);
105856        sqlite3VdbeSetColName(v, 5, COLNAME_NAME, "key", SQLITE_STATIC);
105857      }
105858      for(i=0; i<mx; i++){
105859        i16 cnum = pIdx->aiColumn[i];
105860        sqlite3VdbeAddOp2(v, OP_Integer, i, 1);
105861        sqlite3VdbeAddOp2(v, OP_Integer, cnum, 2);
105862        if( cnum<0 ){
105863          sqlite3VdbeAddOp2(v, OP_Null, 0, 3);
105864        }else{
105865          sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, pTab->aCol[cnum].zName, 0);
105866        }
105867        if( pPragma->iArg ){
105868          sqlite3VdbeAddOp2(v, OP_Integer, pIdx->aSortOrder[i], 4);
105869          sqlite3VdbeAddOp4(v, OP_String8, 0, 5, 0, pIdx->azColl[i], 0);
105870          sqlite3VdbeAddOp2(v, OP_Integer, i<pIdx->nKeyCol, 6);
105871        }
105872        sqlite3VdbeAddOp2(v, OP_ResultRow, 1, pParse->nMem);
105873      }
105874    }
105875  }
105876  break;
105877
105878  case PragTyp_INDEX_LIST: if( zRight ){
105879    Index *pIdx;
105880    Table *pTab;
105881    int i;
105882    pTab = sqlite3FindTable(db, zRight, zDb);
105883    if( pTab ){
105884      v = sqlite3GetVdbe(pParse);
105885      sqlite3VdbeSetNumCols(v, 5);
105886      pParse->nMem = 5;
105887      sqlite3CodeVerifySchema(pParse, iDb);
105888      sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "seq", SQLITE_STATIC);
105889      sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "name", SQLITE_STATIC);
105890      sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "unique", SQLITE_STATIC);
105891      sqlite3VdbeSetColName(v, 3, COLNAME_NAME, "origin", SQLITE_STATIC);
105892      sqlite3VdbeSetColName(v, 4, COLNAME_NAME, "partial", SQLITE_STATIC);
105893      for(pIdx=pTab->pIndex, i=0; pIdx; pIdx=pIdx->pNext, i++){
105894        const char *azOrigin[] = { "c", "u", "pk" };
105895        sqlite3VdbeAddOp2(v, OP_Integer, i, 1);
105896        sqlite3VdbeAddOp4(v, OP_String8, 0, 2, 0, pIdx->zName, 0);
105897        sqlite3VdbeAddOp2(v, OP_Integer, IsUniqueIndex(pIdx), 3);
105898        sqlite3VdbeAddOp4(v, OP_String8, 0, 4, 0, azOrigin[pIdx->idxType], 0);
105899        sqlite3VdbeAddOp2(v, OP_Integer, pIdx->pPartIdxWhere!=0, 5);
105900        sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 5);
105901      }
105902    }
105903  }
105904  break;
105905
105906  case PragTyp_DATABASE_LIST: {
105907    int i;
105908    sqlite3VdbeSetNumCols(v, 3);
105909    pParse->nMem = 3;
105910    sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "seq", SQLITE_STATIC);
105911    sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "name", SQLITE_STATIC);
105912    sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "file", SQLITE_STATIC);
105913    for(i=0; i<db->nDb; i++){
105914      if( db->aDb[i].pBt==0 ) continue;
105915      assert( db->aDb[i].zName!=0 );
105916      sqlite3VdbeAddOp2(v, OP_Integer, i, 1);
105917      sqlite3VdbeAddOp4(v, OP_String8, 0, 2, 0, db->aDb[i].zName, 0);
105918      sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0,
105919           sqlite3BtreeGetFilename(db->aDb[i].pBt), 0);
105920      sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 3);
105921    }
105922  }
105923  break;
105924
105925  case PragTyp_COLLATION_LIST: {
105926    int i = 0;
105927    HashElem *p;
105928    sqlite3VdbeSetNumCols(v, 2);
105929    pParse->nMem = 2;
105930    sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "seq", SQLITE_STATIC);
105931    sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "name", SQLITE_STATIC);
105932    for(p=sqliteHashFirst(&db->aCollSeq); p; p=sqliteHashNext(p)){
105933      CollSeq *pColl = (CollSeq *)sqliteHashData(p);
105934      sqlite3VdbeAddOp2(v, OP_Integer, i++, 1);
105935      sqlite3VdbeAddOp4(v, OP_String8, 0, 2, 0, pColl->zName, 0);
105936      sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 2);
105937    }
105938  }
105939  break;
105940#endif /* SQLITE_OMIT_SCHEMA_PRAGMAS */
105941
105942#ifndef SQLITE_OMIT_FOREIGN_KEY
105943  case PragTyp_FOREIGN_KEY_LIST: if( zRight ){
105944    FKey *pFK;
105945    Table *pTab;
105946    pTab = sqlite3FindTable(db, zRight, zDb);
105947    if( pTab ){
105948      v = sqlite3GetVdbe(pParse);
105949      pFK = pTab->pFKey;
105950      if( pFK ){
105951        int i = 0;
105952        sqlite3VdbeSetNumCols(v, 8);
105953        pParse->nMem = 8;
105954        sqlite3CodeVerifySchema(pParse, iDb);
105955        sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "id", SQLITE_STATIC);
105956        sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "seq", SQLITE_STATIC);
105957        sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "table", SQLITE_STATIC);
105958        sqlite3VdbeSetColName(v, 3, COLNAME_NAME, "from", SQLITE_STATIC);
105959        sqlite3VdbeSetColName(v, 4, COLNAME_NAME, "to", SQLITE_STATIC);
105960        sqlite3VdbeSetColName(v, 5, COLNAME_NAME, "on_update", SQLITE_STATIC);
105961        sqlite3VdbeSetColName(v, 6, COLNAME_NAME, "on_delete", SQLITE_STATIC);
105962        sqlite3VdbeSetColName(v, 7, COLNAME_NAME, "match", SQLITE_STATIC);
105963        while(pFK){
105964          int j;
105965          for(j=0; j<pFK->nCol; j++){
105966            char *zCol = pFK->aCol[j].zCol;
105967            char *zOnDelete = (char *)actionName(pFK->aAction[0]);
105968            char *zOnUpdate = (char *)actionName(pFK->aAction[1]);
105969            sqlite3VdbeAddOp2(v, OP_Integer, i, 1);
105970            sqlite3VdbeAddOp2(v, OP_Integer, j, 2);
105971            sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, pFK->zTo, 0);
105972            sqlite3VdbeAddOp4(v, OP_String8, 0, 4, 0,
105973                              pTab->aCol[pFK->aCol[j].iFrom].zName, 0);
105974            sqlite3VdbeAddOp4(v, zCol ? OP_String8 : OP_Null, 0, 5, 0, zCol, 0);
105975            sqlite3VdbeAddOp4(v, OP_String8, 0, 6, 0, zOnUpdate, 0);
105976            sqlite3VdbeAddOp4(v, OP_String8, 0, 7, 0, zOnDelete, 0);
105977            sqlite3VdbeAddOp4(v, OP_String8, 0, 8, 0, "NONE", 0);
105978            sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 8);
105979          }
105980          ++i;
105981          pFK = pFK->pNextFrom;
105982        }
105983      }
105984    }
105985  }
105986  break;
105987#endif /* !defined(SQLITE_OMIT_FOREIGN_KEY) */
105988
105989#ifndef SQLITE_OMIT_FOREIGN_KEY
105990#ifndef SQLITE_OMIT_TRIGGER
105991  case PragTyp_FOREIGN_KEY_CHECK: {
105992    FKey *pFK;             /* A foreign key constraint */
105993    Table *pTab;           /* Child table contain "REFERENCES" keyword */
105994    Table *pParent;        /* Parent table that child points to */
105995    Index *pIdx;           /* Index in the parent table */
105996    int i;                 /* Loop counter:  Foreign key number for pTab */
105997    int j;                 /* Loop counter:  Field of the foreign key */
105998    HashElem *k;           /* Loop counter:  Next table in schema */
105999    int x;                 /* result variable */
106000    int regResult;         /* 3 registers to hold a result row */
106001    int regKey;            /* Register to hold key for checking the FK */
106002    int regRow;            /* Registers to hold a row from pTab */
106003    int addrTop;           /* Top of a loop checking foreign keys */
106004    int addrOk;            /* Jump here if the key is OK */
106005    int *aiCols;           /* child to parent column mapping */
106006
106007    regResult = pParse->nMem+1;
106008    pParse->nMem += 4;
106009    regKey = ++pParse->nMem;
106010    regRow = ++pParse->nMem;
106011    v = sqlite3GetVdbe(pParse);
106012    sqlite3VdbeSetNumCols(v, 4);
106013    sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "table", SQLITE_STATIC);
106014    sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "rowid", SQLITE_STATIC);
106015    sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "parent", SQLITE_STATIC);
106016    sqlite3VdbeSetColName(v, 3, COLNAME_NAME, "fkid", SQLITE_STATIC);
106017    sqlite3CodeVerifySchema(pParse, iDb);
106018    k = sqliteHashFirst(&db->aDb[iDb].pSchema->tblHash);
106019    while( k ){
106020      if( zRight ){
106021        pTab = sqlite3LocateTable(pParse, 0, zRight, zDb);
106022        k = 0;
106023      }else{
106024        pTab = (Table*)sqliteHashData(k);
106025        k = sqliteHashNext(k);
106026      }
106027      if( pTab==0 || pTab->pFKey==0 ) continue;
106028      sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
106029      if( pTab->nCol+regRow>pParse->nMem ) pParse->nMem = pTab->nCol + regRow;
106030      sqlite3OpenTable(pParse, 0, iDb, pTab, OP_OpenRead);
106031      sqlite3VdbeAddOp4(v, OP_String8, 0, regResult, 0, pTab->zName,
106032                        P4_TRANSIENT);
106033      for(i=1, pFK=pTab->pFKey; pFK; i++, pFK=pFK->pNextFrom){
106034        pParent = sqlite3FindTable(db, pFK->zTo, zDb);
106035        if( pParent==0 ) continue;
106036        pIdx = 0;
106037        sqlite3TableLock(pParse, iDb, pParent->tnum, 0, pParent->zName);
106038        x = sqlite3FkLocateIndex(pParse, pParent, pFK, &pIdx, 0);
106039        if( x==0 ){
106040          if( pIdx==0 ){
106041            sqlite3OpenTable(pParse, i, iDb, pParent, OP_OpenRead);
106042          }else{
106043            sqlite3VdbeAddOp3(v, OP_OpenRead, i, pIdx->tnum, iDb);
106044            sqlite3VdbeSetP4KeyInfo(pParse, pIdx);
106045          }
106046        }else{
106047          k = 0;
106048          break;
106049        }
106050      }
106051      assert( pParse->nErr>0 || pFK==0 );
106052      if( pFK ) break;
106053      if( pParse->nTab<i ) pParse->nTab = i;
106054      addrTop = sqlite3VdbeAddOp1(v, OP_Rewind, 0); VdbeCoverage(v);
106055      for(i=1, pFK=pTab->pFKey; pFK; i++, pFK=pFK->pNextFrom){
106056        pParent = sqlite3FindTable(db, pFK->zTo, zDb);
106057        pIdx = 0;
106058        aiCols = 0;
106059        if( pParent ){
106060          x = sqlite3FkLocateIndex(pParse, pParent, pFK, &pIdx, &aiCols);
106061          assert( x==0 );
106062        }
106063        addrOk = sqlite3VdbeMakeLabel(v);
106064        if( pParent && pIdx==0 ){
106065          int iKey = pFK->aCol[0].iFrom;
106066          assert( iKey>=0 && iKey<pTab->nCol );
106067          if( iKey!=pTab->iPKey ){
106068            sqlite3VdbeAddOp3(v, OP_Column, 0, iKey, regRow);
106069            sqlite3ColumnDefault(v, pTab, iKey, regRow);
106070            sqlite3VdbeAddOp2(v, OP_IsNull, regRow, addrOk); VdbeCoverage(v);
106071            sqlite3VdbeAddOp2(v, OP_MustBeInt, regRow,
106072               sqlite3VdbeCurrentAddr(v)+3); VdbeCoverage(v);
106073          }else{
106074            sqlite3VdbeAddOp2(v, OP_Rowid, 0, regRow);
106075          }
106076          sqlite3VdbeAddOp3(v, OP_NotExists, i, 0, regRow); VdbeCoverage(v);
106077          sqlite3VdbeAddOp2(v, OP_Goto, 0, addrOk);
106078          sqlite3VdbeJumpHere(v, sqlite3VdbeCurrentAddr(v)-2);
106079        }else{
106080          for(j=0; j<pFK->nCol; j++){
106081            sqlite3ExprCodeGetColumnOfTable(v, pTab, 0,
106082                            aiCols ? aiCols[j] : pFK->aCol[j].iFrom, regRow+j);
106083            sqlite3VdbeAddOp2(v, OP_IsNull, regRow+j, addrOk); VdbeCoverage(v);
106084          }
106085          if( pParent ){
106086            sqlite3VdbeAddOp4(v, OP_MakeRecord, regRow, pFK->nCol, regKey,
106087                              sqlite3IndexAffinityStr(v,pIdx), pFK->nCol);
106088            sqlite3VdbeAddOp4Int(v, OP_Found, i, addrOk, regKey, 0);
106089            VdbeCoverage(v);
106090          }
106091        }
106092        sqlite3VdbeAddOp2(v, OP_Rowid, 0, regResult+1);
106093        sqlite3VdbeAddOp4(v, OP_String8, 0, regResult+2, 0,
106094                          pFK->zTo, P4_TRANSIENT);
106095        sqlite3VdbeAddOp2(v, OP_Integer, i-1, regResult+3);
106096        sqlite3VdbeAddOp2(v, OP_ResultRow, regResult, 4);
106097        sqlite3VdbeResolveLabel(v, addrOk);
106098        sqlite3DbFree(db, aiCols);
106099      }
106100      sqlite3VdbeAddOp2(v, OP_Next, 0, addrTop+1); VdbeCoverage(v);
106101      sqlite3VdbeJumpHere(v, addrTop);
106102    }
106103  }
106104  break;
106105#endif /* !defined(SQLITE_OMIT_TRIGGER) */
106106#endif /* !defined(SQLITE_OMIT_FOREIGN_KEY) */
106107
106108#ifndef NDEBUG
106109  case PragTyp_PARSER_TRACE: {
106110    if( zRight ){
106111      if( sqlite3GetBoolean(zRight, 0) ){
106112        sqlite3ParserTrace(stderr, "parser: ");
106113      }else{
106114        sqlite3ParserTrace(0, 0);
106115      }
106116    }
106117  }
106118  break;
106119#endif
106120
106121  /* Reinstall the LIKE and GLOB functions.  The variant of LIKE
106122  ** used will be case sensitive or not depending on the RHS.
106123  */
106124  case PragTyp_CASE_SENSITIVE_LIKE: {
106125    if( zRight ){
106126      sqlite3RegisterLikeFunctions(db, sqlite3GetBoolean(zRight, 0));
106127    }
106128  }
106129  break;
106130
106131#ifndef SQLITE_INTEGRITY_CHECK_ERROR_MAX
106132# define SQLITE_INTEGRITY_CHECK_ERROR_MAX 100
106133#endif
106134
106135#ifndef SQLITE_OMIT_INTEGRITY_CHECK
106136  /* Pragma "quick_check" is reduced version of
106137  ** integrity_check designed to detect most database corruption
106138  ** without most of the overhead of a full integrity-check.
106139  */
106140  case PragTyp_INTEGRITY_CHECK: {
106141    int i, j, addr, mxErr;
106142
106143    /* Code that appears at the end of the integrity check.  If no error
106144    ** messages have been generated, output OK.  Otherwise output the
106145    ** error message
106146    */
106147    static const int iLn = VDBE_OFFSET_LINENO(2);
106148    static const VdbeOpList endCode[] = {
106149      { OP_IfNeg,       1, 0,        0},    /* 0 */
106150      { OP_String8,     0, 3,        0},    /* 1 */
106151      { OP_ResultRow,   3, 1,        0},
106152    };
106153
106154    int isQuick = (sqlite3Tolower(zLeft[0])=='q');
106155
106156    /* If the PRAGMA command was of the form "PRAGMA <db>.integrity_check",
106157    ** then iDb is set to the index of the database identified by <db>.
106158    ** In this case, the integrity of database iDb only is verified by
106159    ** the VDBE created below.
106160    **
106161    ** Otherwise, if the command was simply "PRAGMA integrity_check" (or
106162    ** "PRAGMA quick_check"), then iDb is set to 0. In this case, set iDb
106163    ** to -1 here, to indicate that the VDBE should verify the integrity
106164    ** of all attached databases.  */
106165    assert( iDb>=0 );
106166    assert( iDb==0 || pId2->z );
106167    if( pId2->z==0 ) iDb = -1;
106168
106169    /* Initialize the VDBE program */
106170    pParse->nMem = 6;
106171    sqlite3VdbeSetNumCols(v, 1);
106172    sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "integrity_check", SQLITE_STATIC);
106173
106174    /* Set the maximum error count */
106175    mxErr = SQLITE_INTEGRITY_CHECK_ERROR_MAX;
106176    if( zRight ){
106177      sqlite3GetInt32(zRight, &mxErr);
106178      if( mxErr<=0 ){
106179        mxErr = SQLITE_INTEGRITY_CHECK_ERROR_MAX;
106180      }
106181    }
106182    sqlite3VdbeAddOp2(v, OP_Integer, mxErr, 1);  /* reg[1] holds errors left */
106183
106184    /* Do an integrity check on each database file */
106185    for(i=0; i<db->nDb; i++){
106186      HashElem *x;
106187      Hash *pTbls;
106188      int cnt = 0;
106189
106190      if( OMIT_TEMPDB && i==1 ) continue;
106191      if( iDb>=0 && i!=iDb ) continue;
106192
106193      sqlite3CodeVerifySchema(pParse, i);
106194      addr = sqlite3VdbeAddOp1(v, OP_IfPos, 1); /* Halt if out of errors */
106195      VdbeCoverage(v);
106196      sqlite3VdbeAddOp2(v, OP_Halt, 0, 0);
106197      sqlite3VdbeJumpHere(v, addr);
106198
106199      /* Do an integrity check of the B-Tree
106200      **
106201      ** Begin by filling registers 2, 3, ... with the root pages numbers
106202      ** for all tables and indices in the database.
106203      */
106204      assert( sqlite3SchemaMutexHeld(db, i, 0) );
106205      pTbls = &db->aDb[i].pSchema->tblHash;
106206      for(x=sqliteHashFirst(pTbls); x; x=sqliteHashNext(x)){
106207        Table *pTab = sqliteHashData(x);
106208        Index *pIdx;
106209        if( HasRowid(pTab) ){
106210          sqlite3VdbeAddOp2(v, OP_Integer, pTab->tnum, 2+cnt);
106211          VdbeComment((v, "%s", pTab->zName));
106212          cnt++;
106213        }
106214        for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
106215          sqlite3VdbeAddOp2(v, OP_Integer, pIdx->tnum, 2+cnt);
106216          VdbeComment((v, "%s", pIdx->zName));
106217          cnt++;
106218        }
106219      }
106220
106221      /* Make sure sufficient number of registers have been allocated */
106222      pParse->nMem = MAX( pParse->nMem, cnt+8 );
106223
106224      /* Do the b-tree integrity checks */
106225      sqlite3VdbeAddOp3(v, OP_IntegrityCk, 2, cnt, 1);
106226      sqlite3VdbeChangeP5(v, (u8)i);
106227      addr = sqlite3VdbeAddOp1(v, OP_IsNull, 2); VdbeCoverage(v);
106228      sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0,
106229         sqlite3MPrintf(db, "*** in database %s ***\n", db->aDb[i].zName),
106230         P4_DYNAMIC);
106231      sqlite3VdbeAddOp3(v, OP_Move, 2, 4, 1);
106232      sqlite3VdbeAddOp3(v, OP_Concat, 4, 3, 2);
106233      sqlite3VdbeAddOp2(v, OP_ResultRow, 2, 1);
106234      sqlite3VdbeJumpHere(v, addr);
106235
106236      /* Make sure all the indices are constructed correctly.
106237      */
106238      for(x=sqliteHashFirst(pTbls); x && !isQuick; x=sqliteHashNext(x)){
106239        Table *pTab = sqliteHashData(x);
106240        Index *pIdx, *pPk;
106241        Index *pPrior = 0;
106242        int loopTop;
106243        int iDataCur, iIdxCur;
106244        int r1 = -1;
106245
106246        if( pTab->pIndex==0 ) continue;
106247        pPk = HasRowid(pTab) ? 0 : sqlite3PrimaryKeyIndex(pTab);
106248        addr = sqlite3VdbeAddOp1(v, OP_IfPos, 1);  /* Stop if out of errors */
106249        VdbeCoverage(v);
106250        sqlite3VdbeAddOp2(v, OP_Halt, 0, 0);
106251        sqlite3VdbeJumpHere(v, addr);
106252        sqlite3ExprCacheClear(pParse);
106253        sqlite3OpenTableAndIndices(pParse, pTab, OP_OpenRead,
106254                                   1, 0, &iDataCur, &iIdxCur);
106255        sqlite3VdbeAddOp2(v, OP_Integer, 0, 7);
106256        for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
106257          sqlite3VdbeAddOp2(v, OP_Integer, 0, 8+j); /* index entries counter */
106258        }
106259        pParse->nMem = MAX(pParse->nMem, 8+j);
106260        sqlite3VdbeAddOp2(v, OP_Rewind, iDataCur, 0); VdbeCoverage(v);
106261        loopTop = sqlite3VdbeAddOp2(v, OP_AddImm, 7, 1);
106262        /* Verify that all NOT NULL columns really are NOT NULL */
106263        for(j=0; j<pTab->nCol; j++){
106264          char *zErr;
106265          int jmp2, jmp3;
106266          if( j==pTab->iPKey ) continue;
106267          if( pTab->aCol[j].notNull==0 ) continue;
106268          sqlite3ExprCodeGetColumnOfTable(v, pTab, iDataCur, j, 3);
106269          sqlite3VdbeChangeP5(v, OPFLAG_TYPEOFARG);
106270          jmp2 = sqlite3VdbeAddOp1(v, OP_NotNull, 3); VdbeCoverage(v);
106271          sqlite3VdbeAddOp2(v, OP_AddImm, 1, -1); /* Decrement error limit */
106272          zErr = sqlite3MPrintf(db, "NULL value in %s.%s", pTab->zName,
106273                              pTab->aCol[j].zName);
106274          sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, zErr, P4_DYNAMIC);
106275          sqlite3VdbeAddOp2(v, OP_ResultRow, 3, 1);
106276          jmp3 = sqlite3VdbeAddOp1(v, OP_IfPos, 1); VdbeCoverage(v);
106277          sqlite3VdbeAddOp0(v, OP_Halt);
106278          sqlite3VdbeJumpHere(v, jmp2);
106279          sqlite3VdbeJumpHere(v, jmp3);
106280        }
106281        /* Validate index entries for the current row */
106282        for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
106283          int jmp2, jmp3, jmp4, jmp5;
106284          int ckUniq = sqlite3VdbeMakeLabel(v);
106285          if( pPk==pIdx ) continue;
106286          r1 = sqlite3GenerateIndexKey(pParse, pIdx, iDataCur, 0, 0, &jmp3,
106287                                       pPrior, r1);
106288          pPrior = pIdx;
106289          sqlite3VdbeAddOp2(v, OP_AddImm, 8+j, 1);  /* increment entry count */
106290          /* Verify that an index entry exists for the current table row */
106291          jmp2 = sqlite3VdbeAddOp4Int(v, OP_Found, iIdxCur+j, ckUniq, r1,
106292                                      pIdx->nColumn); VdbeCoverage(v);
106293          sqlite3VdbeAddOp2(v, OP_AddImm, 1, -1); /* Decrement error limit */
106294          sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, "row ", P4_STATIC);
106295          sqlite3VdbeAddOp3(v, OP_Concat, 7, 3, 3);
106296          sqlite3VdbeAddOp4(v, OP_String8, 0, 4, 0,
106297                            " missing from index ", P4_STATIC);
106298          sqlite3VdbeAddOp3(v, OP_Concat, 4, 3, 3);
106299          jmp5 = sqlite3VdbeAddOp4(v, OP_String8, 0, 4, 0,
106300                                   pIdx->zName, P4_TRANSIENT);
106301          sqlite3VdbeAddOp3(v, OP_Concat, 4, 3, 3);
106302          sqlite3VdbeAddOp2(v, OP_ResultRow, 3, 1);
106303          jmp4 = sqlite3VdbeAddOp1(v, OP_IfPos, 1); VdbeCoverage(v);
106304          sqlite3VdbeAddOp0(v, OP_Halt);
106305          sqlite3VdbeJumpHere(v, jmp2);
106306          /* For UNIQUE indexes, verify that only one entry exists with the
106307          ** current key.  The entry is unique if (1) any column is NULL
106308          ** or (2) the next entry has a different key */
106309          if( IsUniqueIndex(pIdx) ){
106310            int uniqOk = sqlite3VdbeMakeLabel(v);
106311            int jmp6;
106312            int kk;
106313            for(kk=0; kk<pIdx->nKeyCol; kk++){
106314              int iCol = pIdx->aiColumn[kk];
106315              assert( iCol>=0 && iCol<pTab->nCol );
106316              if( pTab->aCol[iCol].notNull ) continue;
106317              sqlite3VdbeAddOp2(v, OP_IsNull, r1+kk, uniqOk);
106318              VdbeCoverage(v);
106319            }
106320            jmp6 = sqlite3VdbeAddOp1(v, OP_Next, iIdxCur+j); VdbeCoverage(v);
106321            sqlite3VdbeAddOp2(v, OP_Goto, 0, uniqOk);
106322            sqlite3VdbeJumpHere(v, jmp6);
106323            sqlite3VdbeAddOp4Int(v, OP_IdxGT, iIdxCur+j, uniqOk, r1,
106324                                 pIdx->nKeyCol); VdbeCoverage(v);
106325            sqlite3VdbeAddOp2(v, OP_AddImm, 1, -1); /* Decrement error limit */
106326            sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0,
106327                              "non-unique entry in index ", P4_STATIC);
106328            sqlite3VdbeAddOp2(v, OP_Goto, 0, jmp5);
106329            sqlite3VdbeResolveLabel(v, uniqOk);
106330          }
106331          sqlite3VdbeJumpHere(v, jmp4);
106332          sqlite3ResolvePartIdxLabel(pParse, jmp3);
106333        }
106334        sqlite3VdbeAddOp2(v, OP_Next, iDataCur, loopTop); VdbeCoverage(v);
106335        sqlite3VdbeJumpHere(v, loopTop-1);
106336#ifndef SQLITE_OMIT_BTREECOUNT
106337        sqlite3VdbeAddOp4(v, OP_String8, 0, 2, 0,
106338                     "wrong # of entries in index ", P4_STATIC);
106339        for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
106340          if( pPk==pIdx ) continue;
106341          addr = sqlite3VdbeCurrentAddr(v);
106342          sqlite3VdbeAddOp2(v, OP_IfPos, 1, addr+2); VdbeCoverage(v);
106343          sqlite3VdbeAddOp2(v, OP_Halt, 0, 0);
106344          sqlite3VdbeAddOp2(v, OP_Count, iIdxCur+j, 3);
106345          sqlite3VdbeAddOp3(v, OP_Eq, 8+j, addr+8, 3); VdbeCoverage(v);
106346          sqlite3VdbeChangeP5(v, SQLITE_NOTNULL);
106347          sqlite3VdbeAddOp2(v, OP_AddImm, 1, -1);
106348          sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, pIdx->zName, P4_TRANSIENT);
106349          sqlite3VdbeAddOp3(v, OP_Concat, 3, 2, 7);
106350          sqlite3VdbeAddOp2(v, OP_ResultRow, 7, 1);
106351        }
106352#endif /* SQLITE_OMIT_BTREECOUNT */
106353      }
106354    }
106355    addr = sqlite3VdbeAddOpList(v, ArraySize(endCode), endCode, iLn);
106356    sqlite3VdbeChangeP3(v, addr, -mxErr);
106357    sqlite3VdbeJumpHere(v, addr);
106358    sqlite3VdbeChangeP4(v, addr+1, "ok", P4_STATIC);
106359  }
106360  break;
106361#endif /* SQLITE_OMIT_INTEGRITY_CHECK */
106362
106363#ifndef SQLITE_OMIT_UTF16
106364  /*
106365  **   PRAGMA encoding
106366  **   PRAGMA encoding = "utf-8"|"utf-16"|"utf-16le"|"utf-16be"
106367  **
106368  ** In its first form, this pragma returns the encoding of the main
106369  ** database. If the database is not initialized, it is initialized now.
106370  **
106371  ** The second form of this pragma is a no-op if the main database file
106372  ** has not already been initialized. In this case it sets the default
106373  ** encoding that will be used for the main database file if a new file
106374  ** is created. If an existing main database file is opened, then the
106375  ** default text encoding for the existing database is used.
106376  **
106377  ** In all cases new databases created using the ATTACH command are
106378  ** created to use the same default text encoding as the main database. If
106379  ** the main database has not been initialized and/or created when ATTACH
106380  ** is executed, this is done before the ATTACH operation.
106381  **
106382  ** In the second form this pragma sets the text encoding to be used in
106383  ** new database files created using this database handle. It is only
106384  ** useful if invoked immediately after the main database i
106385  */
106386  case PragTyp_ENCODING: {
106387    static const struct EncName {
106388      char *zName;
106389      u8 enc;
106390    } encnames[] = {
106391      { "UTF8",     SQLITE_UTF8        },
106392      { "UTF-8",    SQLITE_UTF8        },  /* Must be element [1] */
106393      { "UTF-16le", SQLITE_UTF16LE     },  /* Must be element [2] */
106394      { "UTF-16be", SQLITE_UTF16BE     },  /* Must be element [3] */
106395      { "UTF16le",  SQLITE_UTF16LE     },
106396      { "UTF16be",  SQLITE_UTF16BE     },
106397      { "UTF-16",   0                  }, /* SQLITE_UTF16NATIVE */
106398      { "UTF16",    0                  }, /* SQLITE_UTF16NATIVE */
106399      { 0, 0 }
106400    };
106401    const struct EncName *pEnc;
106402    if( !zRight ){    /* "PRAGMA encoding" */
106403      if( sqlite3ReadSchema(pParse) ) goto pragma_out;
106404      sqlite3VdbeSetNumCols(v, 1);
106405      sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "encoding", SQLITE_STATIC);
106406      sqlite3VdbeAddOp2(v, OP_String8, 0, 1);
106407      assert( encnames[SQLITE_UTF8].enc==SQLITE_UTF8 );
106408      assert( encnames[SQLITE_UTF16LE].enc==SQLITE_UTF16LE );
106409      assert( encnames[SQLITE_UTF16BE].enc==SQLITE_UTF16BE );
106410      sqlite3VdbeChangeP4(v, -1, encnames[ENC(pParse->db)].zName, P4_STATIC);
106411      sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
106412    }else{                        /* "PRAGMA encoding = XXX" */
106413      /* Only change the value of sqlite.enc if the database handle is not
106414      ** initialized. If the main database exists, the new sqlite.enc value
106415      ** will be overwritten when the schema is next loaded. If it does not
106416      ** already exists, it will be created to use the new encoding value.
106417      */
106418      if(
106419        !(DbHasProperty(db, 0, DB_SchemaLoaded)) ||
106420        DbHasProperty(db, 0, DB_Empty)
106421      ){
106422        for(pEnc=&encnames[0]; pEnc->zName; pEnc++){
106423          if( 0==sqlite3StrICmp(zRight, pEnc->zName) ){
106424            SCHEMA_ENC(db) = ENC(db) =
106425                pEnc->enc ? pEnc->enc : SQLITE_UTF16NATIVE;
106426            break;
106427          }
106428        }
106429        if( !pEnc->zName ){
106430          sqlite3ErrorMsg(pParse, "unsupported encoding: %s", zRight);
106431        }
106432      }
106433    }
106434  }
106435  break;
106436#endif /* SQLITE_OMIT_UTF16 */
106437
106438#ifndef SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS
106439  /*
106440  **   PRAGMA [database.]schema_version
106441  **   PRAGMA [database.]schema_version = <integer>
106442  **
106443  **   PRAGMA [database.]user_version
106444  **   PRAGMA [database.]user_version = <integer>
106445  **
106446  **   PRAGMA [database.]freelist_count = <integer>
106447  **
106448  **   PRAGMA [database.]application_id
106449  **   PRAGMA [database.]application_id = <integer>
106450  **
106451  ** The pragma's schema_version and user_version are used to set or get
106452  ** the value of the schema-version and user-version, respectively. Both
106453  ** the schema-version and the user-version are 32-bit signed integers
106454  ** stored in the database header.
106455  **
106456  ** The schema-cookie is usually only manipulated internally by SQLite. It
106457  ** is incremented by SQLite whenever the database schema is modified (by
106458  ** creating or dropping a table or index). The schema version is used by
106459  ** SQLite each time a query is executed to ensure that the internal cache
106460  ** of the schema used when compiling the SQL query matches the schema of
106461  ** the database against which the compiled query is actually executed.
106462  ** Subverting this mechanism by using "PRAGMA schema_version" to modify
106463  ** the schema-version is potentially dangerous and may lead to program
106464  ** crashes or database corruption. Use with caution!
106465  **
106466  ** The user-version is not used internally by SQLite. It may be used by
106467  ** applications for any purpose.
106468  */
106469  case PragTyp_HEADER_VALUE: {
106470    int iCookie = pPragma->iArg;  /* Which cookie to read or write */
106471    sqlite3VdbeUsesBtree(v, iDb);
106472    if( zRight && (pPragma->mPragFlag & PragFlag_ReadOnly)==0 ){
106473      /* Write the specified cookie value */
106474      static const VdbeOpList setCookie[] = {
106475        { OP_Transaction,    0,  1,  0},    /* 0 */
106476        { OP_Integer,        0,  1,  0},    /* 1 */
106477        { OP_SetCookie,      0,  0,  1},    /* 2 */
106478      };
106479      int addr = sqlite3VdbeAddOpList(v, ArraySize(setCookie), setCookie, 0);
106480      sqlite3VdbeChangeP1(v, addr, iDb);
106481      sqlite3VdbeChangeP1(v, addr+1, sqlite3Atoi(zRight));
106482      sqlite3VdbeChangeP1(v, addr+2, iDb);
106483      sqlite3VdbeChangeP2(v, addr+2, iCookie);
106484    }else{
106485      /* Read the specified cookie value */
106486      static const VdbeOpList readCookie[] = {
106487        { OP_Transaction,     0,  0,  0},    /* 0 */
106488        { OP_ReadCookie,      0,  1,  0},    /* 1 */
106489        { OP_ResultRow,       1,  1,  0}
106490      };
106491      int addr = sqlite3VdbeAddOpList(v, ArraySize(readCookie), readCookie, 0);
106492      sqlite3VdbeChangeP1(v, addr, iDb);
106493      sqlite3VdbeChangeP1(v, addr+1, iDb);
106494      sqlite3VdbeChangeP3(v, addr+1, iCookie);
106495      sqlite3VdbeSetNumCols(v, 1);
106496      sqlite3VdbeSetColName(v, 0, COLNAME_NAME, zLeft, SQLITE_TRANSIENT);
106497    }
106498  }
106499  break;
106500#endif /* SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS */
106501
106502#ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
106503  /*
106504  **   PRAGMA compile_options
106505  **
106506  ** Return the names of all compile-time options used in this build,
106507  ** one option per row.
106508  */
106509  case PragTyp_COMPILE_OPTIONS: {
106510    int i = 0;
106511    const char *zOpt;
106512    sqlite3VdbeSetNumCols(v, 1);
106513    pParse->nMem = 1;
106514    sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "compile_option", SQLITE_STATIC);
106515    while( (zOpt = sqlite3_compileoption_get(i++))!=0 ){
106516      sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, zOpt, 0);
106517      sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
106518    }
106519  }
106520  break;
106521#endif /* SQLITE_OMIT_COMPILEOPTION_DIAGS */
106522
106523#ifndef SQLITE_OMIT_WAL
106524  /*
106525  **   PRAGMA [database.]wal_checkpoint = passive|full|restart|truncate
106526  **
106527  ** Checkpoint the database.
106528  */
106529  case PragTyp_WAL_CHECKPOINT: {
106530    int iBt = (pId2->z?iDb:SQLITE_MAX_ATTACHED);
106531    int eMode = SQLITE_CHECKPOINT_PASSIVE;
106532    if( zRight ){
106533      if( sqlite3StrICmp(zRight, "full")==0 ){
106534        eMode = SQLITE_CHECKPOINT_FULL;
106535      }else if( sqlite3StrICmp(zRight, "restart")==0 ){
106536        eMode = SQLITE_CHECKPOINT_RESTART;
106537      }else if( sqlite3StrICmp(zRight, "truncate")==0 ){
106538        eMode = SQLITE_CHECKPOINT_TRUNCATE;
106539      }
106540    }
106541    sqlite3VdbeSetNumCols(v, 3);
106542    pParse->nMem = 3;
106543    sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "busy", SQLITE_STATIC);
106544    sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "log", SQLITE_STATIC);
106545    sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "checkpointed", SQLITE_STATIC);
106546
106547    sqlite3VdbeAddOp3(v, OP_Checkpoint, iBt, eMode, 1);
106548    sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 3);
106549  }
106550  break;
106551
106552  /*
106553  **   PRAGMA wal_autocheckpoint
106554  **   PRAGMA wal_autocheckpoint = N
106555  **
106556  ** Configure a database connection to automatically checkpoint a database
106557  ** after accumulating N frames in the log. Or query for the current value
106558  ** of N.
106559  */
106560  case PragTyp_WAL_AUTOCHECKPOINT: {
106561    if( zRight ){
106562      sqlite3_wal_autocheckpoint(db, sqlite3Atoi(zRight));
106563    }
106564    returnSingleInt(pParse, "wal_autocheckpoint",
106565       db->xWalCallback==sqlite3WalDefaultHook ?
106566           SQLITE_PTR_TO_INT(db->pWalArg) : 0);
106567  }
106568  break;
106569#endif
106570
106571  /*
106572  **  PRAGMA shrink_memory
106573  **
106574  ** IMPLEMENTATION-OF: R-23445-46109 This pragma causes the database
106575  ** connection on which it is invoked to free up as much memory as it
106576  ** can, by calling sqlite3_db_release_memory().
106577  */
106578  case PragTyp_SHRINK_MEMORY: {
106579    sqlite3_db_release_memory(db);
106580    break;
106581  }
106582
106583  /*
106584  **   PRAGMA busy_timeout
106585  **   PRAGMA busy_timeout = N
106586  **
106587  ** Call sqlite3_busy_timeout(db, N).  Return the current timeout value
106588  ** if one is set.  If no busy handler or a different busy handler is set
106589  ** then 0 is returned.  Setting the busy_timeout to 0 or negative
106590  ** disables the timeout.
106591  */
106592  /*case PragTyp_BUSY_TIMEOUT*/ default: {
106593    assert( pPragma->ePragTyp==PragTyp_BUSY_TIMEOUT );
106594    if( zRight ){
106595      sqlite3_busy_timeout(db, sqlite3Atoi(zRight));
106596    }
106597    returnSingleInt(pParse, "timeout",  db->busyTimeout);
106598    break;
106599  }
106600
106601  /*
106602  **   PRAGMA soft_heap_limit
106603  **   PRAGMA soft_heap_limit = N
106604  **
106605  ** IMPLEMENTATION-OF: R-26343-45930 This pragma invokes the
106606  ** sqlite3_soft_heap_limit64() interface with the argument N, if N is
106607  ** specified and is a non-negative integer.
106608  ** IMPLEMENTATION-OF: R-64451-07163 The soft_heap_limit pragma always
106609  ** returns the same integer that would be returned by the
106610  ** sqlite3_soft_heap_limit64(-1) C-language function.
106611  */
106612  case PragTyp_SOFT_HEAP_LIMIT: {
106613    sqlite3_int64 N;
106614    if( zRight && sqlite3DecOrHexToI64(zRight, &N)==SQLITE_OK ){
106615      sqlite3_soft_heap_limit64(N);
106616    }
106617    returnSingleInt(pParse, "soft_heap_limit",  sqlite3_soft_heap_limit64(-1));
106618    break;
106619  }
106620
106621  /*
106622  **   PRAGMA threads
106623  **   PRAGMA threads = N
106624  **
106625  ** Configure the maximum number of worker threads.  Return the new
106626  ** maximum, which might be less than requested.
106627  */
106628  case PragTyp_THREADS: {
106629    sqlite3_int64 N;
106630    if( zRight
106631     && sqlite3DecOrHexToI64(zRight, &N)==SQLITE_OK
106632     && N>=0
106633    ){
106634      sqlite3_limit(db, SQLITE_LIMIT_WORKER_THREADS, (int)(N&0x7fffffff));
106635    }
106636    returnSingleInt(pParse, "threads",
106637                    sqlite3_limit(db, SQLITE_LIMIT_WORKER_THREADS, -1));
106638    break;
106639  }
106640
106641#if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
106642  /*
106643  ** Report the current state of file logs for all databases
106644  */
106645  case PragTyp_LOCK_STATUS: {
106646    static const char *const azLockName[] = {
106647      "unlocked", "shared", "reserved", "pending", "exclusive"
106648    };
106649    int i;
106650    sqlite3VdbeSetNumCols(v, 2);
106651    pParse->nMem = 2;
106652    sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "database", SQLITE_STATIC);
106653    sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "status", SQLITE_STATIC);
106654    for(i=0; i<db->nDb; i++){
106655      Btree *pBt;
106656      const char *zState = "unknown";
106657      int j;
106658      if( db->aDb[i].zName==0 ) continue;
106659      sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, db->aDb[i].zName, P4_STATIC);
106660      pBt = db->aDb[i].pBt;
106661      if( pBt==0 || sqlite3BtreePager(pBt)==0 ){
106662        zState = "closed";
106663      }else if( sqlite3_file_control(db, i ? db->aDb[i].zName : 0,
106664                                     SQLITE_FCNTL_LOCKSTATE, &j)==SQLITE_OK ){
106665         zState = azLockName[j];
106666      }
106667      sqlite3VdbeAddOp4(v, OP_String8, 0, 2, 0, zState, P4_STATIC);
106668      sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 2);
106669    }
106670    break;
106671  }
106672#endif
106673
106674#ifdef SQLITE_HAS_CODEC
106675  case PragTyp_KEY: {
106676    if( zRight ) sqlite3_key_v2(db, zDb, zRight, sqlite3Strlen30(zRight));
106677    break;
106678  }
106679  case PragTyp_REKEY: {
106680    if( zRight ) sqlite3_rekey_v2(db, zDb, zRight, sqlite3Strlen30(zRight));
106681    break;
106682  }
106683  case PragTyp_HEXKEY: {
106684    if( zRight ){
106685      u8 iByte;
106686      int i;
106687      char zKey[40];
106688      for(i=0, iByte=0; i<sizeof(zKey)*2 && sqlite3Isxdigit(zRight[i]); i++){
106689        iByte = (iByte<<4) + sqlite3HexToInt(zRight[i]);
106690        if( (i&1)!=0 ) zKey[i/2] = iByte;
106691      }
106692      if( (zLeft[3] & 0xf)==0xb ){
106693        sqlite3_key_v2(db, zDb, zKey, i/2);
106694      }else{
106695        sqlite3_rekey_v2(db, zDb, zKey, i/2);
106696      }
106697    }
106698    break;
106699  }
106700#endif
106701#if defined(SQLITE_HAS_CODEC) || defined(SQLITE_ENABLE_CEROD)
106702  case PragTyp_ACTIVATE_EXTENSIONS: if( zRight ){
106703#ifdef SQLITE_HAS_CODEC
106704    if( sqlite3StrNICmp(zRight, "see-", 4)==0 ){
106705      sqlite3_activate_see(&zRight[4]);
106706    }
106707#endif
106708#ifdef SQLITE_ENABLE_CEROD
106709    if( sqlite3StrNICmp(zRight, "cerod-", 6)==0 ){
106710      sqlite3_activate_cerod(&zRight[6]);
106711    }
106712#endif
106713  }
106714  break;
106715#endif
106716
106717  } /* End of the PRAGMA switch */
106718
106719pragma_out:
106720  sqlite3DbFree(db, zLeft);
106721  sqlite3DbFree(db, zRight);
106722}
106723
106724#endif /* SQLITE_OMIT_PRAGMA */
106725
106726/************** End of pragma.c **********************************************/
106727/************** Begin file prepare.c *****************************************/
106728/*
106729** 2005 May 25
106730**
106731** The author disclaims copyright to this source code.  In place of
106732** a legal notice, here is a blessing:
106733**
106734**    May you do good and not evil.
106735**    May you find forgiveness for yourself and forgive others.
106736**    May you share freely, never taking more than you give.
106737**
106738*************************************************************************
106739** This file contains the implementation of the sqlite3_prepare()
106740** interface, and routines that contribute to loading the database schema
106741** from disk.
106742*/
106743/* #include "sqliteInt.h" */
106744
106745/*
106746** Fill the InitData structure with an error message that indicates
106747** that the database is corrupt.
106748*/
106749static void corruptSchema(
106750  InitData *pData,     /* Initialization context */
106751  const char *zObj,    /* Object being parsed at the point of error */
106752  const char *zExtra   /* Error information */
106753){
106754  sqlite3 *db = pData->db;
106755  if( !db->mallocFailed && (db->flags & SQLITE_RecoveryMode)==0 ){
106756    char *z;
106757    if( zObj==0 ) zObj = "?";
106758    z = sqlite3_mprintf("malformed database schema (%s)", zObj);
106759    if( z && zExtra ) z = sqlite3_mprintf("%z - %s", z, zExtra);
106760    sqlite3DbFree(db, *pData->pzErrMsg);
106761    *pData->pzErrMsg = z;
106762    if( z==0 ) db->mallocFailed = 1;
106763  }
106764  pData->rc = db->mallocFailed ? SQLITE_NOMEM : SQLITE_CORRUPT_BKPT;
106765}
106766
106767/*
106768** This is the callback routine for the code that initializes the
106769** database.  See sqlite3Init() below for additional information.
106770** This routine is also called from the OP_ParseSchema opcode of the VDBE.
106771**
106772** Each callback contains the following information:
106773**
106774**     argv[0] = name of thing being created
106775**     argv[1] = root page number for table or index. 0 for trigger or view.
106776**     argv[2] = SQL text for the CREATE statement.
106777**
106778*/
106779SQLITE_PRIVATE int sqlite3InitCallback(void *pInit, int argc, char **argv, char **NotUsed){
106780  InitData *pData = (InitData*)pInit;
106781  sqlite3 *db = pData->db;
106782  int iDb = pData->iDb;
106783
106784  assert( argc==3 );
106785  UNUSED_PARAMETER2(NotUsed, argc);
106786  assert( sqlite3_mutex_held(db->mutex) );
106787  DbClearProperty(db, iDb, DB_Empty);
106788  if( db->mallocFailed ){
106789    corruptSchema(pData, argv[0], 0);
106790    return 1;
106791  }
106792
106793  assert( iDb>=0 && iDb<db->nDb );
106794  if( argv==0 ) return 0;   /* Might happen if EMPTY_RESULT_CALLBACKS are on */
106795  if( argv[1]==0 ){
106796    corruptSchema(pData, argv[0], 0);
106797  }else if( sqlite3_strnicmp(argv[2],"create ",7)==0 ){
106798    /* Call the parser to process a CREATE TABLE, INDEX or VIEW.
106799    ** But because db->init.busy is set to 1, no VDBE code is generated
106800    ** or executed.  All the parser does is build the internal data
106801    ** structures that describe the table, index, or view.
106802    */
106803    int rc;
106804    sqlite3_stmt *pStmt;
106805    TESTONLY(int rcp);            /* Return code from sqlite3_prepare() */
106806
106807    assert( db->init.busy );
106808    db->init.iDb = iDb;
106809    db->init.newTnum = sqlite3Atoi(argv[1]);
106810    db->init.orphanTrigger = 0;
106811    TESTONLY(rcp = ) sqlite3_prepare(db, argv[2], -1, &pStmt, 0);
106812    rc = db->errCode;
106813    assert( (rc&0xFF)==(rcp&0xFF) );
106814    db->init.iDb = 0;
106815    if( SQLITE_OK!=rc ){
106816      if( db->init.orphanTrigger ){
106817        assert( iDb==1 );
106818      }else{
106819        pData->rc = rc;
106820        if( rc==SQLITE_NOMEM ){
106821          db->mallocFailed = 1;
106822        }else if( rc!=SQLITE_INTERRUPT && (rc&0xFF)!=SQLITE_LOCKED ){
106823          corruptSchema(pData, argv[0], sqlite3_errmsg(db));
106824        }
106825      }
106826    }
106827    sqlite3_finalize(pStmt);
106828  }else if( argv[0]==0 || (argv[2]!=0 && argv[2][0]!=0) ){
106829    corruptSchema(pData, argv[0], 0);
106830  }else{
106831    /* If the SQL column is blank it means this is an index that
106832    ** was created to be the PRIMARY KEY or to fulfill a UNIQUE
106833    ** constraint for a CREATE TABLE.  The index should have already
106834    ** been created when we processed the CREATE TABLE.  All we have
106835    ** to do here is record the root page number for that index.
106836    */
106837    Index *pIndex;
106838    pIndex = sqlite3FindIndex(db, argv[0], db->aDb[iDb].zName);
106839    if( pIndex==0 ){
106840      /* This can occur if there exists an index on a TEMP table which
106841      ** has the same name as another index on a permanent index.  Since
106842      ** the permanent table is hidden by the TEMP table, we can also
106843      ** safely ignore the index on the permanent table.
106844      */
106845      /* Do Nothing */;
106846    }else if( sqlite3GetInt32(argv[1], &pIndex->tnum)==0 ){
106847      corruptSchema(pData, argv[0], "invalid rootpage");
106848    }
106849  }
106850  return 0;
106851}
106852
106853/*
106854** Attempt to read the database schema and initialize internal
106855** data structures for a single database file.  The index of the
106856** database file is given by iDb.  iDb==0 is used for the main
106857** database.  iDb==1 should never be used.  iDb>=2 is used for
106858** auxiliary databases.  Return one of the SQLITE_ error codes to
106859** indicate success or failure.
106860*/
106861static int sqlite3InitOne(sqlite3 *db, int iDb, char **pzErrMsg){
106862  int rc;
106863  int i;
106864#ifndef SQLITE_OMIT_DEPRECATED
106865  int size;
106866#endif
106867  Table *pTab;
106868  Db *pDb;
106869  char const *azArg[4];
106870  int meta[5];
106871  InitData initData;
106872  char const *zMasterSchema;
106873  char const *zMasterName;
106874  int openedTransaction = 0;
106875
106876  /*
106877  ** The master database table has a structure like this
106878  */
106879  static const char master_schema[] =
106880     "CREATE TABLE sqlite_master(\n"
106881     "  type text,\n"
106882     "  name text,\n"
106883     "  tbl_name text,\n"
106884     "  rootpage integer,\n"
106885     "  sql text\n"
106886     ")"
106887  ;
106888#ifndef SQLITE_OMIT_TEMPDB
106889  static const char temp_master_schema[] =
106890     "CREATE TEMP TABLE sqlite_temp_master(\n"
106891     "  type text,\n"
106892     "  name text,\n"
106893     "  tbl_name text,\n"
106894     "  rootpage integer,\n"
106895     "  sql text\n"
106896     ")"
106897  ;
106898#else
106899  #define temp_master_schema 0
106900#endif
106901
106902  assert( iDb>=0 && iDb<db->nDb );
106903  assert( db->aDb[iDb].pSchema );
106904  assert( sqlite3_mutex_held(db->mutex) );
106905  assert( iDb==1 || sqlite3BtreeHoldsMutex(db->aDb[iDb].pBt) );
106906
106907  /* zMasterSchema and zInitScript are set to point at the master schema
106908  ** and initialisation script appropriate for the database being
106909  ** initialized. zMasterName is the name of the master table.
106910  */
106911  if( !OMIT_TEMPDB && iDb==1 ){
106912    zMasterSchema = temp_master_schema;
106913  }else{
106914    zMasterSchema = master_schema;
106915  }
106916  zMasterName = SCHEMA_TABLE(iDb);
106917
106918  /* Construct the schema tables.  */
106919  azArg[0] = zMasterName;
106920  azArg[1] = "1";
106921  azArg[2] = zMasterSchema;
106922  azArg[3] = 0;
106923  initData.db = db;
106924  initData.iDb = iDb;
106925  initData.rc = SQLITE_OK;
106926  initData.pzErrMsg = pzErrMsg;
106927  sqlite3InitCallback(&initData, 3, (char **)azArg, 0);
106928  if( initData.rc ){
106929    rc = initData.rc;
106930    goto error_out;
106931  }
106932  pTab = sqlite3FindTable(db, zMasterName, db->aDb[iDb].zName);
106933  if( ALWAYS(pTab) ){
106934    pTab->tabFlags |= TF_Readonly;
106935  }
106936
106937  /* Create a cursor to hold the database open
106938  */
106939  pDb = &db->aDb[iDb];
106940  if( pDb->pBt==0 ){
106941    if( !OMIT_TEMPDB && ALWAYS(iDb==1) ){
106942      DbSetProperty(db, 1, DB_SchemaLoaded);
106943    }
106944    return SQLITE_OK;
106945  }
106946
106947  /* If there is not already a read-only (or read-write) transaction opened
106948  ** on the b-tree database, open one now. If a transaction is opened, it
106949  ** will be closed before this function returns.  */
106950  sqlite3BtreeEnter(pDb->pBt);
106951  if( !sqlite3BtreeIsInReadTrans(pDb->pBt) ){
106952    rc = sqlite3BtreeBeginTrans(pDb->pBt, 0);
106953    if( rc!=SQLITE_OK ){
106954      sqlite3SetString(pzErrMsg, db, sqlite3ErrStr(rc));
106955      goto initone_error_out;
106956    }
106957    openedTransaction = 1;
106958  }
106959
106960  /* Get the database meta information.
106961  **
106962  ** Meta values are as follows:
106963  **    meta[0]   Schema cookie.  Changes with each schema change.
106964  **    meta[1]   File format of schema layer.
106965  **    meta[2]   Size of the page cache.
106966  **    meta[3]   Largest rootpage (auto/incr_vacuum mode)
106967  **    meta[4]   Db text encoding. 1:UTF-8 2:UTF-16LE 3:UTF-16BE
106968  **    meta[5]   User version
106969  **    meta[6]   Incremental vacuum mode
106970  **    meta[7]   unused
106971  **    meta[8]   unused
106972  **    meta[9]   unused
106973  **
106974  ** Note: The #defined SQLITE_UTF* symbols in sqliteInt.h correspond to
106975  ** the possible values of meta[4].
106976  */
106977  for(i=0; i<ArraySize(meta); i++){
106978    sqlite3BtreeGetMeta(pDb->pBt, i+1, (u32 *)&meta[i]);
106979  }
106980  pDb->pSchema->schema_cookie = meta[BTREE_SCHEMA_VERSION-1];
106981
106982  /* If opening a non-empty database, check the text encoding. For the
106983  ** main database, set sqlite3.enc to the encoding of the main database.
106984  ** For an attached db, it is an error if the encoding is not the same
106985  ** as sqlite3.enc.
106986  */
106987  if( meta[BTREE_TEXT_ENCODING-1] ){  /* text encoding */
106988    if( iDb==0 ){
106989#ifndef SQLITE_OMIT_UTF16
106990      u8 encoding;
106991      /* If opening the main database, set ENC(db). */
106992      encoding = (u8)meta[BTREE_TEXT_ENCODING-1] & 3;
106993      if( encoding==0 ) encoding = SQLITE_UTF8;
106994      ENC(db) = encoding;
106995#else
106996      ENC(db) = SQLITE_UTF8;
106997#endif
106998    }else{
106999      /* If opening an attached database, the encoding much match ENC(db) */
107000      if( meta[BTREE_TEXT_ENCODING-1]!=ENC(db) ){
107001        sqlite3SetString(pzErrMsg, db, "attached databases must use the same"
107002            " text encoding as main database");
107003        rc = SQLITE_ERROR;
107004        goto initone_error_out;
107005      }
107006    }
107007  }else{
107008    DbSetProperty(db, iDb, DB_Empty);
107009  }
107010  pDb->pSchema->enc = ENC(db);
107011
107012  if( pDb->pSchema->cache_size==0 ){
107013#ifndef SQLITE_OMIT_DEPRECATED
107014    size = sqlite3AbsInt32(meta[BTREE_DEFAULT_CACHE_SIZE-1]);
107015    if( size==0 ){ size = SQLITE_DEFAULT_CACHE_SIZE; }
107016    pDb->pSchema->cache_size = size;
107017#else
107018    pDb->pSchema->cache_size = SQLITE_DEFAULT_CACHE_SIZE;
107019#endif
107020    sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size);
107021  }
107022
107023  /*
107024  ** file_format==1    Version 3.0.0.
107025  ** file_format==2    Version 3.1.3.  // ALTER TABLE ADD COLUMN
107026  ** file_format==3    Version 3.1.4.  // ditto but with non-NULL defaults
107027  ** file_format==4    Version 3.3.0.  // DESC indices.  Boolean constants
107028  */
107029  pDb->pSchema->file_format = (u8)meta[BTREE_FILE_FORMAT-1];
107030  if( pDb->pSchema->file_format==0 ){
107031    pDb->pSchema->file_format = 1;
107032  }
107033  if( pDb->pSchema->file_format>SQLITE_MAX_FILE_FORMAT ){
107034    sqlite3SetString(pzErrMsg, db, "unsupported file format");
107035    rc = SQLITE_ERROR;
107036    goto initone_error_out;
107037  }
107038
107039  /* Ticket #2804:  When we open a database in the newer file format,
107040  ** clear the legacy_file_format pragma flag so that a VACUUM will
107041  ** not downgrade the database and thus invalidate any descending
107042  ** indices that the user might have created.
107043  */
107044  if( iDb==0 && meta[BTREE_FILE_FORMAT-1]>=4 ){
107045    db->flags &= ~SQLITE_LegacyFileFmt;
107046  }
107047
107048  /* Read the schema information out of the schema tables
107049  */
107050  assert( db->init.busy );
107051  {
107052    char *zSql;
107053    zSql = sqlite3MPrintf(db,
107054        "SELECT name, rootpage, sql FROM '%q'.%s ORDER BY rowid",
107055        db->aDb[iDb].zName, zMasterName);
107056#ifndef SQLITE_OMIT_AUTHORIZATION
107057    {
107058      sqlite3_xauth xAuth;
107059      xAuth = db->xAuth;
107060      db->xAuth = 0;
107061#endif
107062      rc = sqlite3_exec(db, zSql, sqlite3InitCallback, &initData, 0);
107063#ifndef SQLITE_OMIT_AUTHORIZATION
107064      db->xAuth = xAuth;
107065    }
107066#endif
107067    if( rc==SQLITE_OK ) rc = initData.rc;
107068    sqlite3DbFree(db, zSql);
107069#ifndef SQLITE_OMIT_ANALYZE
107070    if( rc==SQLITE_OK ){
107071      sqlite3AnalysisLoad(db, iDb);
107072    }
107073#endif
107074  }
107075  if( db->mallocFailed ){
107076    rc = SQLITE_NOMEM;
107077    sqlite3ResetAllSchemasOfConnection(db);
107078  }
107079  if( rc==SQLITE_OK || (db->flags&SQLITE_RecoveryMode)){
107080    /* Black magic: If the SQLITE_RecoveryMode flag is set, then consider
107081    ** the schema loaded, even if errors occurred. In this situation the
107082    ** current sqlite3_prepare() operation will fail, but the following one
107083    ** will attempt to compile the supplied statement against whatever subset
107084    ** of the schema was loaded before the error occurred. The primary
107085    ** purpose of this is to allow access to the sqlite_master table
107086    ** even when its contents have been corrupted.
107087    */
107088    DbSetProperty(db, iDb, DB_SchemaLoaded);
107089    rc = SQLITE_OK;
107090  }
107091
107092  /* Jump here for an error that occurs after successfully allocating
107093  ** curMain and calling sqlite3BtreeEnter(). For an error that occurs
107094  ** before that point, jump to error_out.
107095  */
107096initone_error_out:
107097  if( openedTransaction ){
107098    sqlite3BtreeCommit(pDb->pBt);
107099  }
107100  sqlite3BtreeLeave(pDb->pBt);
107101
107102error_out:
107103  if( rc==SQLITE_NOMEM || rc==SQLITE_IOERR_NOMEM ){
107104    db->mallocFailed = 1;
107105  }
107106  return rc;
107107}
107108
107109/*
107110** Initialize all database files - the main database file, the file
107111** used to store temporary tables, and any additional database files
107112** created using ATTACH statements.  Return a success code.  If an
107113** error occurs, write an error message into *pzErrMsg.
107114**
107115** After a database is initialized, the DB_SchemaLoaded bit is set
107116** bit is set in the flags field of the Db structure. If the database
107117** file was of zero-length, then the DB_Empty flag is also set.
107118*/
107119SQLITE_PRIVATE int sqlite3Init(sqlite3 *db, char **pzErrMsg){
107120  int i, rc;
107121  int commit_internal = !(db->flags&SQLITE_InternChanges);
107122
107123  assert( sqlite3_mutex_held(db->mutex) );
107124  assert( sqlite3BtreeHoldsMutex(db->aDb[0].pBt) );
107125  assert( db->init.busy==0 );
107126  rc = SQLITE_OK;
107127  db->init.busy = 1;
107128  ENC(db) = SCHEMA_ENC(db);
107129  for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
107130    if( DbHasProperty(db, i, DB_SchemaLoaded) || i==1 ) continue;
107131    rc = sqlite3InitOne(db, i, pzErrMsg);
107132    if( rc ){
107133      sqlite3ResetOneSchema(db, i);
107134    }
107135  }
107136
107137  /* Once all the other databases have been initialized, load the schema
107138  ** for the TEMP database. This is loaded last, as the TEMP database
107139  ** schema may contain references to objects in other databases.
107140  */
107141#ifndef SQLITE_OMIT_TEMPDB
107142  assert( db->nDb>1 );
107143  if( rc==SQLITE_OK && !DbHasProperty(db, 1, DB_SchemaLoaded) ){
107144    rc = sqlite3InitOne(db, 1, pzErrMsg);
107145    if( rc ){
107146      sqlite3ResetOneSchema(db, 1);
107147    }
107148  }
107149#endif
107150
107151  db->init.busy = 0;
107152  if( rc==SQLITE_OK && commit_internal ){
107153    sqlite3CommitInternalChanges(db);
107154  }
107155
107156  return rc;
107157}
107158
107159/*
107160** This routine is a no-op if the database schema is already initialized.
107161** Otherwise, the schema is loaded. An error code is returned.
107162*/
107163SQLITE_PRIVATE int sqlite3ReadSchema(Parse *pParse){
107164  int rc = SQLITE_OK;
107165  sqlite3 *db = pParse->db;
107166  assert( sqlite3_mutex_held(db->mutex) );
107167  if( !db->init.busy ){
107168    rc = sqlite3Init(db, &pParse->zErrMsg);
107169  }
107170  if( rc!=SQLITE_OK ){
107171    pParse->rc = rc;
107172    pParse->nErr++;
107173  }
107174  return rc;
107175}
107176
107177
107178/*
107179** Check schema cookies in all databases.  If any cookie is out
107180** of date set pParse->rc to SQLITE_SCHEMA.  If all schema cookies
107181** make no changes to pParse->rc.
107182*/
107183static void schemaIsValid(Parse *pParse){
107184  sqlite3 *db = pParse->db;
107185  int iDb;
107186  int rc;
107187  int cookie;
107188
107189  assert( pParse->checkSchema );
107190  assert( sqlite3_mutex_held(db->mutex) );
107191  for(iDb=0; iDb<db->nDb; iDb++){
107192    int openedTransaction = 0;         /* True if a transaction is opened */
107193    Btree *pBt = db->aDb[iDb].pBt;     /* Btree database to read cookie from */
107194    if( pBt==0 ) continue;
107195
107196    /* If there is not already a read-only (or read-write) transaction opened
107197    ** on the b-tree database, open one now. If a transaction is opened, it
107198    ** will be closed immediately after reading the meta-value. */
107199    if( !sqlite3BtreeIsInReadTrans(pBt) ){
107200      rc = sqlite3BtreeBeginTrans(pBt, 0);
107201      if( rc==SQLITE_NOMEM || rc==SQLITE_IOERR_NOMEM ){
107202        db->mallocFailed = 1;
107203      }
107204      if( rc!=SQLITE_OK ) return;
107205      openedTransaction = 1;
107206    }
107207
107208    /* Read the schema cookie from the database. If it does not match the
107209    ** value stored as part of the in-memory schema representation,
107210    ** set Parse.rc to SQLITE_SCHEMA. */
107211    sqlite3BtreeGetMeta(pBt, BTREE_SCHEMA_VERSION, (u32 *)&cookie);
107212    assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
107213    if( cookie!=db->aDb[iDb].pSchema->schema_cookie ){
107214      sqlite3ResetOneSchema(db, iDb);
107215      pParse->rc = SQLITE_SCHEMA;
107216    }
107217
107218    /* Close the transaction, if one was opened. */
107219    if( openedTransaction ){
107220      sqlite3BtreeCommit(pBt);
107221    }
107222  }
107223}
107224
107225/*
107226** Convert a schema pointer into the iDb index that indicates
107227** which database file in db->aDb[] the schema refers to.
107228**
107229** If the same database is attached more than once, the first
107230** attached database is returned.
107231*/
107232SQLITE_PRIVATE int sqlite3SchemaToIndex(sqlite3 *db, Schema *pSchema){
107233  int i = -1000000;
107234
107235  /* If pSchema is NULL, then return -1000000. This happens when code in
107236  ** expr.c is trying to resolve a reference to a transient table (i.e. one
107237  ** created by a sub-select). In this case the return value of this
107238  ** function should never be used.
107239  **
107240  ** We return -1000000 instead of the more usual -1 simply because using
107241  ** -1000000 as the incorrect index into db->aDb[] is much
107242  ** more likely to cause a segfault than -1 (of course there are assert()
107243  ** statements too, but it never hurts to play the odds).
107244  */
107245  assert( sqlite3_mutex_held(db->mutex) );
107246  if( pSchema ){
107247    for(i=0; ALWAYS(i<db->nDb); i++){
107248      if( db->aDb[i].pSchema==pSchema ){
107249        break;
107250      }
107251    }
107252    assert( i>=0 && i<db->nDb );
107253  }
107254  return i;
107255}
107256
107257/*
107258** Free all memory allocations in the pParse object
107259*/
107260SQLITE_PRIVATE void sqlite3ParserReset(Parse *pParse){
107261  if( pParse ){
107262    sqlite3 *db = pParse->db;
107263    sqlite3DbFree(db, pParse->aLabel);
107264    sqlite3ExprListDelete(db, pParse->pConstExpr);
107265  }
107266}
107267
107268/*
107269** Compile the UTF-8 encoded SQL statement zSql into a statement handle.
107270*/
107271static int sqlite3Prepare(
107272  sqlite3 *db,              /* Database handle. */
107273  const char *zSql,         /* UTF-8 encoded SQL statement. */
107274  int nBytes,               /* Length of zSql in bytes. */
107275  int saveSqlFlag,          /* True to copy SQL text into the sqlite3_stmt */
107276  Vdbe *pReprepare,         /* VM being reprepared */
107277  sqlite3_stmt **ppStmt,    /* OUT: A pointer to the prepared statement */
107278  const char **pzTail       /* OUT: End of parsed string */
107279){
107280  Parse *pParse;            /* Parsing context */
107281  char *zErrMsg = 0;        /* Error message */
107282  int rc = SQLITE_OK;       /* Result code */
107283  int i;                    /* Loop counter */
107284
107285  /* Allocate the parsing context */
107286  pParse = sqlite3StackAllocZero(db, sizeof(*pParse));
107287  if( pParse==0 ){
107288    rc = SQLITE_NOMEM;
107289    goto end_prepare;
107290  }
107291  pParse->pReprepare = pReprepare;
107292  assert( ppStmt && *ppStmt==0 );
107293  assert( !db->mallocFailed );
107294  assert( sqlite3_mutex_held(db->mutex) );
107295
107296  /* Check to verify that it is possible to get a read lock on all
107297  ** database schemas.  The inability to get a read lock indicates that
107298  ** some other database connection is holding a write-lock, which in
107299  ** turn means that the other connection has made uncommitted changes
107300  ** to the schema.
107301  **
107302  ** Were we to proceed and prepare the statement against the uncommitted
107303  ** schema changes and if those schema changes are subsequently rolled
107304  ** back and different changes are made in their place, then when this
107305  ** prepared statement goes to run the schema cookie would fail to detect
107306  ** the schema change.  Disaster would follow.
107307  **
107308  ** This thread is currently holding mutexes on all Btrees (because
107309  ** of the sqlite3BtreeEnterAll() in sqlite3LockAndPrepare()) so it
107310  ** is not possible for another thread to start a new schema change
107311  ** while this routine is running.  Hence, we do not need to hold
107312  ** locks on the schema, we just need to make sure nobody else is
107313  ** holding them.
107314  **
107315  ** Note that setting READ_UNCOMMITTED overrides most lock detection,
107316  ** but it does *not* override schema lock detection, so this all still
107317  ** works even if READ_UNCOMMITTED is set.
107318  */
107319  for(i=0; i<db->nDb; i++) {
107320    Btree *pBt = db->aDb[i].pBt;
107321    if( pBt ){
107322      assert( sqlite3BtreeHoldsMutex(pBt) );
107323      rc = sqlite3BtreeSchemaLocked(pBt);
107324      if( rc ){
107325        const char *zDb = db->aDb[i].zName;
107326        sqlite3ErrorWithMsg(db, rc, "database schema is locked: %s", zDb);
107327        testcase( db->flags & SQLITE_ReadUncommitted );
107328        goto end_prepare;
107329      }
107330    }
107331  }
107332
107333  sqlite3VtabUnlockList(db);
107334
107335  pParse->db = db;
107336  pParse->nQueryLoop = 0;  /* Logarithmic, so 0 really means 1 */
107337  if( nBytes>=0 && (nBytes==0 || zSql[nBytes-1]!=0) ){
107338    char *zSqlCopy;
107339    int mxLen = db->aLimit[SQLITE_LIMIT_SQL_LENGTH];
107340    testcase( nBytes==mxLen );
107341    testcase( nBytes==mxLen+1 );
107342    if( nBytes>mxLen ){
107343      sqlite3ErrorWithMsg(db, SQLITE_TOOBIG, "statement too long");
107344      rc = sqlite3ApiExit(db, SQLITE_TOOBIG);
107345      goto end_prepare;
107346    }
107347    zSqlCopy = sqlite3DbStrNDup(db, zSql, nBytes);
107348    if( zSqlCopy ){
107349      sqlite3RunParser(pParse, zSqlCopy, &zErrMsg);
107350      sqlite3DbFree(db, zSqlCopy);
107351      pParse->zTail = &zSql[pParse->zTail-zSqlCopy];
107352    }else{
107353      pParse->zTail = &zSql[nBytes];
107354    }
107355  }else{
107356    sqlite3RunParser(pParse, zSql, &zErrMsg);
107357  }
107358  assert( 0==pParse->nQueryLoop );
107359
107360  if( db->mallocFailed ){
107361    pParse->rc = SQLITE_NOMEM;
107362  }
107363  if( pParse->rc==SQLITE_DONE ) pParse->rc = SQLITE_OK;
107364  if( pParse->checkSchema ){
107365    schemaIsValid(pParse);
107366  }
107367  if( db->mallocFailed ){
107368    pParse->rc = SQLITE_NOMEM;
107369  }
107370  if( pzTail ){
107371    *pzTail = pParse->zTail;
107372  }
107373  rc = pParse->rc;
107374
107375#ifndef SQLITE_OMIT_EXPLAIN
107376  if( rc==SQLITE_OK && pParse->pVdbe && pParse->explain ){
107377    static const char * const azColName[] = {
107378       "addr", "opcode", "p1", "p2", "p3", "p4", "p5", "comment",
107379       "selectid", "order", "from", "detail"
107380    };
107381    int iFirst, mx;
107382    if( pParse->explain==2 ){
107383      sqlite3VdbeSetNumCols(pParse->pVdbe, 4);
107384      iFirst = 8;
107385      mx = 12;
107386    }else{
107387      sqlite3VdbeSetNumCols(pParse->pVdbe, 8);
107388      iFirst = 0;
107389      mx = 8;
107390    }
107391    for(i=iFirst; i<mx; i++){
107392      sqlite3VdbeSetColName(pParse->pVdbe, i-iFirst, COLNAME_NAME,
107393                            azColName[i], SQLITE_STATIC);
107394    }
107395  }
107396#endif
107397
107398  if( db->init.busy==0 ){
107399    Vdbe *pVdbe = pParse->pVdbe;
107400    sqlite3VdbeSetSql(pVdbe, zSql, (int)(pParse->zTail-zSql), saveSqlFlag);
107401  }
107402  if( pParse->pVdbe && (rc!=SQLITE_OK || db->mallocFailed) ){
107403    sqlite3VdbeFinalize(pParse->pVdbe);
107404    assert(!(*ppStmt));
107405  }else{
107406    *ppStmt = (sqlite3_stmt*)pParse->pVdbe;
107407  }
107408
107409  if( zErrMsg ){
107410    sqlite3ErrorWithMsg(db, rc, "%s", zErrMsg);
107411    sqlite3DbFree(db, zErrMsg);
107412  }else{
107413    sqlite3Error(db, rc);
107414  }
107415
107416  /* Delete any TriggerPrg structures allocated while parsing this statement. */
107417  while( pParse->pTriggerPrg ){
107418    TriggerPrg *pT = pParse->pTriggerPrg;
107419    pParse->pTriggerPrg = pT->pNext;
107420    sqlite3DbFree(db, pT);
107421  }
107422
107423end_prepare:
107424
107425  sqlite3ParserReset(pParse);
107426  sqlite3StackFree(db, pParse);
107427  rc = sqlite3ApiExit(db, rc);
107428  assert( (rc&db->errMask)==rc );
107429  return rc;
107430}
107431static int sqlite3LockAndPrepare(
107432  sqlite3 *db,              /* Database handle. */
107433  const char *zSql,         /* UTF-8 encoded SQL statement. */
107434  int nBytes,               /* Length of zSql in bytes. */
107435  int saveSqlFlag,          /* True to copy SQL text into the sqlite3_stmt */
107436  Vdbe *pOld,               /* VM being reprepared */
107437  sqlite3_stmt **ppStmt,    /* OUT: A pointer to the prepared statement */
107438  const char **pzTail       /* OUT: End of parsed string */
107439){
107440  int rc;
107441
107442#ifdef SQLITE_ENABLE_API_ARMOR
107443  if( ppStmt==0 ) return SQLITE_MISUSE_BKPT;
107444#endif
107445  *ppStmt = 0;
107446  if( !sqlite3SafetyCheckOk(db)||zSql==0 ){
107447    return SQLITE_MISUSE_BKPT;
107448  }
107449  sqlite3_mutex_enter(db->mutex);
107450  sqlite3BtreeEnterAll(db);
107451  rc = sqlite3Prepare(db, zSql, nBytes, saveSqlFlag, pOld, ppStmt, pzTail);
107452  if( rc==SQLITE_SCHEMA ){
107453    sqlite3_finalize(*ppStmt);
107454    rc = sqlite3Prepare(db, zSql, nBytes, saveSqlFlag, pOld, ppStmt, pzTail);
107455  }
107456  sqlite3BtreeLeaveAll(db);
107457  sqlite3_mutex_leave(db->mutex);
107458  assert( rc==SQLITE_OK || *ppStmt==0 );
107459  return rc;
107460}
107461
107462/*
107463** Rerun the compilation of a statement after a schema change.
107464**
107465** If the statement is successfully recompiled, return SQLITE_OK. Otherwise,
107466** if the statement cannot be recompiled because another connection has
107467** locked the sqlite3_master table, return SQLITE_LOCKED. If any other error
107468** occurs, return SQLITE_SCHEMA.
107469*/
107470SQLITE_PRIVATE int sqlite3Reprepare(Vdbe *p){
107471  int rc;
107472  sqlite3_stmt *pNew;
107473  const char *zSql;
107474  sqlite3 *db;
107475
107476  assert( sqlite3_mutex_held(sqlite3VdbeDb(p)->mutex) );
107477  zSql = sqlite3_sql((sqlite3_stmt *)p);
107478  assert( zSql!=0 );  /* Reprepare only called for prepare_v2() statements */
107479  db = sqlite3VdbeDb(p);
107480  assert( sqlite3_mutex_held(db->mutex) );
107481  rc = sqlite3LockAndPrepare(db, zSql, -1, 0, p, &pNew, 0);
107482  if( rc ){
107483    if( rc==SQLITE_NOMEM ){
107484      db->mallocFailed = 1;
107485    }
107486    assert( pNew==0 );
107487    return rc;
107488  }else{
107489    assert( pNew!=0 );
107490  }
107491  sqlite3VdbeSwap((Vdbe*)pNew, p);
107492  sqlite3TransferBindings(pNew, (sqlite3_stmt*)p);
107493  sqlite3VdbeResetStepResult((Vdbe*)pNew);
107494  sqlite3VdbeFinalize((Vdbe*)pNew);
107495  return SQLITE_OK;
107496}
107497
107498
107499/*
107500** Two versions of the official API.  Legacy and new use.  In the legacy
107501** version, the original SQL text is not saved in the prepared statement
107502** and so if a schema change occurs, SQLITE_SCHEMA is returned by
107503** sqlite3_step().  In the new version, the original SQL text is retained
107504** and the statement is automatically recompiled if an schema change
107505** occurs.
107506*/
107507SQLITE_API int SQLITE_STDCALL sqlite3_prepare(
107508  sqlite3 *db,              /* Database handle. */
107509  const char *zSql,         /* UTF-8 encoded SQL statement. */
107510  int nBytes,               /* Length of zSql in bytes. */
107511  sqlite3_stmt **ppStmt,    /* OUT: A pointer to the prepared statement */
107512  const char **pzTail       /* OUT: End of parsed string */
107513){
107514  int rc;
107515  rc = sqlite3LockAndPrepare(db,zSql,nBytes,0,0,ppStmt,pzTail);
107516  assert( rc==SQLITE_OK || ppStmt==0 || *ppStmt==0 );  /* VERIFY: F13021 */
107517  return rc;
107518}
107519SQLITE_API int SQLITE_STDCALL sqlite3_prepare_v2(
107520  sqlite3 *db,              /* Database handle. */
107521  const char *zSql,         /* UTF-8 encoded SQL statement. */
107522  int nBytes,               /* Length of zSql in bytes. */
107523  sqlite3_stmt **ppStmt,    /* OUT: A pointer to the prepared statement */
107524  const char **pzTail       /* OUT: End of parsed string */
107525){
107526  int rc;
107527  rc = sqlite3LockAndPrepare(db,zSql,nBytes,1,0,ppStmt,pzTail);
107528  assert( rc==SQLITE_OK || ppStmt==0 || *ppStmt==0 );  /* VERIFY: F13021 */
107529  return rc;
107530}
107531
107532
107533#ifndef SQLITE_OMIT_UTF16
107534/*
107535** Compile the UTF-16 encoded SQL statement zSql into a statement handle.
107536*/
107537static int sqlite3Prepare16(
107538  sqlite3 *db,              /* Database handle. */
107539  const void *zSql,         /* UTF-16 encoded SQL statement. */
107540  int nBytes,               /* Length of zSql in bytes. */
107541  int saveSqlFlag,          /* True to save SQL text into the sqlite3_stmt */
107542  sqlite3_stmt **ppStmt,    /* OUT: A pointer to the prepared statement */
107543  const void **pzTail       /* OUT: End of parsed string */
107544){
107545  /* This function currently works by first transforming the UTF-16
107546  ** encoded string to UTF-8, then invoking sqlite3_prepare(). The
107547  ** tricky bit is figuring out the pointer to return in *pzTail.
107548  */
107549  char *zSql8;
107550  const char *zTail8 = 0;
107551  int rc = SQLITE_OK;
107552
107553#ifdef SQLITE_ENABLE_API_ARMOR
107554  if( ppStmt==0 ) return SQLITE_MISUSE_BKPT;
107555#endif
107556  *ppStmt = 0;
107557  if( !sqlite3SafetyCheckOk(db)||zSql==0 ){
107558    return SQLITE_MISUSE_BKPT;
107559  }
107560  if( nBytes>=0 ){
107561    int sz;
107562    const char *z = (const char*)zSql;
107563    for(sz=0; sz<nBytes && (z[sz]!=0 || z[sz+1]!=0); sz += 2){}
107564    nBytes = sz;
107565  }
107566  sqlite3_mutex_enter(db->mutex);
107567  zSql8 = sqlite3Utf16to8(db, zSql, nBytes, SQLITE_UTF16NATIVE);
107568  if( zSql8 ){
107569    rc = sqlite3LockAndPrepare(db, zSql8, -1, saveSqlFlag, 0, ppStmt, &zTail8);
107570  }
107571
107572  if( zTail8 && pzTail ){
107573    /* If sqlite3_prepare returns a tail pointer, we calculate the
107574    ** equivalent pointer into the UTF-16 string by counting the unicode
107575    ** characters between zSql8 and zTail8, and then returning a pointer
107576    ** the same number of characters into the UTF-16 string.
107577    */
107578    int chars_parsed = sqlite3Utf8CharLen(zSql8, (int)(zTail8-zSql8));
107579    *pzTail = (u8 *)zSql + sqlite3Utf16ByteLen(zSql, chars_parsed);
107580  }
107581  sqlite3DbFree(db, zSql8);
107582  rc = sqlite3ApiExit(db, rc);
107583  sqlite3_mutex_leave(db->mutex);
107584  return rc;
107585}
107586
107587/*
107588** Two versions of the official API.  Legacy and new use.  In the legacy
107589** version, the original SQL text is not saved in the prepared statement
107590** and so if a schema change occurs, SQLITE_SCHEMA is returned by
107591** sqlite3_step().  In the new version, the original SQL text is retained
107592** and the statement is automatically recompiled if an schema change
107593** occurs.
107594*/
107595SQLITE_API int SQLITE_STDCALL sqlite3_prepare16(
107596  sqlite3 *db,              /* Database handle. */
107597  const void *zSql,         /* UTF-16 encoded SQL statement. */
107598  int nBytes,               /* Length of zSql in bytes. */
107599  sqlite3_stmt **ppStmt,    /* OUT: A pointer to the prepared statement */
107600  const void **pzTail       /* OUT: End of parsed string */
107601){
107602  int rc;
107603  rc = sqlite3Prepare16(db,zSql,nBytes,0,ppStmt,pzTail);
107604  assert( rc==SQLITE_OK || ppStmt==0 || *ppStmt==0 );  /* VERIFY: F13021 */
107605  return rc;
107606}
107607SQLITE_API int SQLITE_STDCALL sqlite3_prepare16_v2(
107608  sqlite3 *db,              /* Database handle. */
107609  const void *zSql,         /* UTF-16 encoded SQL statement. */
107610  int nBytes,               /* Length of zSql in bytes. */
107611  sqlite3_stmt **ppStmt,    /* OUT: A pointer to the prepared statement */
107612  const void **pzTail       /* OUT: End of parsed string */
107613){
107614  int rc;
107615  rc = sqlite3Prepare16(db,zSql,nBytes,1,ppStmt,pzTail);
107616  assert( rc==SQLITE_OK || ppStmt==0 || *ppStmt==0 );  /* VERIFY: F13021 */
107617  return rc;
107618}
107619
107620#endif /* SQLITE_OMIT_UTF16 */
107621
107622/************** End of prepare.c *********************************************/
107623/************** Begin file select.c ******************************************/
107624/*
107625** 2001 September 15
107626**
107627** The author disclaims copyright to this source code.  In place of
107628** a legal notice, here is a blessing:
107629**
107630**    May you do good and not evil.
107631**    May you find forgiveness for yourself and forgive others.
107632**    May you share freely, never taking more than you give.
107633**
107634*************************************************************************
107635** This file contains C code routines that are called by the parser
107636** to handle SELECT statements in SQLite.
107637*/
107638/* #include "sqliteInt.h" */
107639
107640/*
107641** Trace output macros
107642*/
107643#if SELECTTRACE_ENABLED
107644/***/ int sqlite3SelectTrace = 0;
107645# define SELECTTRACE(K,P,S,X)  \
107646  if(sqlite3SelectTrace&(K))   \
107647    sqlite3DebugPrintf("%*s%s.%p: ",(P)->nSelectIndent*2-2,"",\
107648        (S)->zSelName,(S)),\
107649    sqlite3DebugPrintf X
107650#else
107651# define SELECTTRACE(K,P,S,X)
107652#endif
107653
107654
107655/*
107656** An instance of the following object is used to record information about
107657** how to process the DISTINCT keyword, to simplify passing that information
107658** into the selectInnerLoop() routine.
107659*/
107660typedef struct DistinctCtx DistinctCtx;
107661struct DistinctCtx {
107662  u8 isTnct;      /* True if the DISTINCT keyword is present */
107663  u8 eTnctType;   /* One of the WHERE_DISTINCT_* operators */
107664  int tabTnct;    /* Ephemeral table used for DISTINCT processing */
107665  int addrTnct;   /* Address of OP_OpenEphemeral opcode for tabTnct */
107666};
107667
107668/*
107669** An instance of the following object is used to record information about
107670** the ORDER BY (or GROUP BY) clause of query is being coded.
107671*/
107672typedef struct SortCtx SortCtx;
107673struct SortCtx {
107674  ExprList *pOrderBy;   /* The ORDER BY (or GROUP BY clause) */
107675  int nOBSat;           /* Number of ORDER BY terms satisfied by indices */
107676  int iECursor;         /* Cursor number for the sorter */
107677  int regReturn;        /* Register holding block-output return address */
107678  int labelBkOut;       /* Start label for the block-output subroutine */
107679  int addrSortIndex;    /* Address of the OP_SorterOpen or OP_OpenEphemeral */
107680  u8 sortFlags;         /* Zero or more SORTFLAG_* bits */
107681};
107682#define SORTFLAG_UseSorter  0x01   /* Use SorterOpen instead of OpenEphemeral */
107683
107684/*
107685** Delete all the content of a Select structure.  Deallocate the structure
107686** itself only if bFree is true.
107687*/
107688static void clearSelect(sqlite3 *db, Select *p, int bFree){
107689  while( p ){
107690    Select *pPrior = p->pPrior;
107691    sqlite3ExprListDelete(db, p->pEList);
107692    sqlite3SrcListDelete(db, p->pSrc);
107693    sqlite3ExprDelete(db, p->pWhere);
107694    sqlite3ExprListDelete(db, p->pGroupBy);
107695    sqlite3ExprDelete(db, p->pHaving);
107696    sqlite3ExprListDelete(db, p->pOrderBy);
107697    sqlite3ExprDelete(db, p->pLimit);
107698    sqlite3ExprDelete(db, p->pOffset);
107699    sqlite3WithDelete(db, p->pWith);
107700    if( bFree ) sqlite3DbFree(db, p);
107701    p = pPrior;
107702    bFree = 1;
107703  }
107704}
107705
107706/*
107707** Initialize a SelectDest structure.
107708*/
107709SQLITE_PRIVATE void sqlite3SelectDestInit(SelectDest *pDest, int eDest, int iParm){
107710  pDest->eDest = (u8)eDest;
107711  pDest->iSDParm = iParm;
107712  pDest->affSdst = 0;
107713  pDest->iSdst = 0;
107714  pDest->nSdst = 0;
107715}
107716
107717
107718/*
107719** Allocate a new Select structure and return a pointer to that
107720** structure.
107721*/
107722SQLITE_PRIVATE Select *sqlite3SelectNew(
107723  Parse *pParse,        /* Parsing context */
107724  ExprList *pEList,     /* which columns to include in the result */
107725  SrcList *pSrc,        /* the FROM clause -- which tables to scan */
107726  Expr *pWhere,         /* the WHERE clause */
107727  ExprList *pGroupBy,   /* the GROUP BY clause */
107728  Expr *pHaving,        /* the HAVING clause */
107729  ExprList *pOrderBy,   /* the ORDER BY clause */
107730  u16 selFlags,         /* Flag parameters, such as SF_Distinct */
107731  Expr *pLimit,         /* LIMIT value.  NULL means not used */
107732  Expr *pOffset         /* OFFSET value.  NULL means no offset */
107733){
107734  Select *pNew;
107735  Select standin;
107736  sqlite3 *db = pParse->db;
107737  pNew = sqlite3DbMallocZero(db, sizeof(*pNew) );
107738  if( pNew==0 ){
107739    assert( db->mallocFailed );
107740    pNew = &standin;
107741    memset(pNew, 0, sizeof(*pNew));
107742  }
107743  if( pEList==0 ){
107744    pEList = sqlite3ExprListAppend(pParse, 0, sqlite3Expr(db,TK_ALL,0));
107745  }
107746  pNew->pEList = pEList;
107747  if( pSrc==0 ) pSrc = sqlite3DbMallocZero(db, sizeof(*pSrc));
107748  pNew->pSrc = pSrc;
107749  pNew->pWhere = pWhere;
107750  pNew->pGroupBy = pGroupBy;
107751  pNew->pHaving = pHaving;
107752  pNew->pOrderBy = pOrderBy;
107753  pNew->selFlags = selFlags;
107754  pNew->op = TK_SELECT;
107755  pNew->pLimit = pLimit;
107756  pNew->pOffset = pOffset;
107757  assert( pOffset==0 || pLimit!=0 || pParse->nErr>0 || db->mallocFailed!=0 );
107758  pNew->addrOpenEphm[0] = -1;
107759  pNew->addrOpenEphm[1] = -1;
107760  if( db->mallocFailed ) {
107761    clearSelect(db, pNew, pNew!=&standin);
107762    pNew = 0;
107763  }else{
107764    assert( pNew->pSrc!=0 || pParse->nErr>0 );
107765  }
107766  assert( pNew!=&standin );
107767  return pNew;
107768}
107769
107770#if SELECTTRACE_ENABLED
107771/*
107772** Set the name of a Select object
107773*/
107774SQLITE_PRIVATE void sqlite3SelectSetName(Select *p, const char *zName){
107775  if( p && zName ){
107776    sqlite3_snprintf(sizeof(p->zSelName), p->zSelName, "%s", zName);
107777  }
107778}
107779#endif
107780
107781
107782/*
107783** Delete the given Select structure and all of its substructures.
107784*/
107785SQLITE_PRIVATE void sqlite3SelectDelete(sqlite3 *db, Select *p){
107786  clearSelect(db, p, 1);
107787}
107788
107789/*
107790** Return a pointer to the right-most SELECT statement in a compound.
107791*/
107792static Select *findRightmost(Select *p){
107793  while( p->pNext ) p = p->pNext;
107794  return p;
107795}
107796
107797/*
107798** Given 1 to 3 identifiers preceding the JOIN keyword, determine the
107799** type of join.  Return an integer constant that expresses that type
107800** in terms of the following bit values:
107801**
107802**     JT_INNER
107803**     JT_CROSS
107804**     JT_OUTER
107805**     JT_NATURAL
107806**     JT_LEFT
107807**     JT_RIGHT
107808**
107809** A full outer join is the combination of JT_LEFT and JT_RIGHT.
107810**
107811** If an illegal or unsupported join type is seen, then still return
107812** a join type, but put an error in the pParse structure.
107813*/
107814SQLITE_PRIVATE int sqlite3JoinType(Parse *pParse, Token *pA, Token *pB, Token *pC){
107815  int jointype = 0;
107816  Token *apAll[3];
107817  Token *p;
107818                             /*   0123456789 123456789 123456789 123 */
107819  static const char zKeyText[] = "naturaleftouterightfullinnercross";
107820  static const struct {
107821    u8 i;        /* Beginning of keyword text in zKeyText[] */
107822    u8 nChar;    /* Length of the keyword in characters */
107823    u8 code;     /* Join type mask */
107824  } aKeyword[] = {
107825    /* natural */ { 0,  7, JT_NATURAL                },
107826    /* left    */ { 6,  4, JT_LEFT|JT_OUTER          },
107827    /* outer   */ { 10, 5, JT_OUTER                  },
107828    /* right   */ { 14, 5, JT_RIGHT|JT_OUTER         },
107829    /* full    */ { 19, 4, JT_LEFT|JT_RIGHT|JT_OUTER },
107830    /* inner   */ { 23, 5, JT_INNER                  },
107831    /* cross   */ { 28, 5, JT_INNER|JT_CROSS         },
107832  };
107833  int i, j;
107834  apAll[0] = pA;
107835  apAll[1] = pB;
107836  apAll[2] = pC;
107837  for(i=0; i<3 && apAll[i]; i++){
107838    p = apAll[i];
107839    for(j=0; j<ArraySize(aKeyword); j++){
107840      if( p->n==aKeyword[j].nChar
107841          && sqlite3StrNICmp((char*)p->z, &zKeyText[aKeyword[j].i], p->n)==0 ){
107842        jointype |= aKeyword[j].code;
107843        break;
107844      }
107845    }
107846    testcase( j==0 || j==1 || j==2 || j==3 || j==4 || j==5 || j==6 );
107847    if( j>=ArraySize(aKeyword) ){
107848      jointype |= JT_ERROR;
107849      break;
107850    }
107851  }
107852  if(
107853     (jointype & (JT_INNER|JT_OUTER))==(JT_INNER|JT_OUTER) ||
107854     (jointype & JT_ERROR)!=0
107855  ){
107856    const char *zSp = " ";
107857    assert( pB!=0 );
107858    if( pC==0 ){ zSp++; }
107859    sqlite3ErrorMsg(pParse, "unknown or unsupported join type: "
107860       "%T %T%s%T", pA, pB, zSp, pC);
107861    jointype = JT_INNER;
107862  }else if( (jointype & JT_OUTER)!=0
107863         && (jointype & (JT_LEFT|JT_RIGHT))!=JT_LEFT ){
107864    sqlite3ErrorMsg(pParse,
107865      "RIGHT and FULL OUTER JOINs are not currently supported");
107866    jointype = JT_INNER;
107867  }
107868  return jointype;
107869}
107870
107871/*
107872** Return the index of a column in a table.  Return -1 if the column
107873** is not contained in the table.
107874*/
107875static int columnIndex(Table *pTab, const char *zCol){
107876  int i;
107877  for(i=0; i<pTab->nCol; i++){
107878    if( sqlite3StrICmp(pTab->aCol[i].zName, zCol)==0 ) return i;
107879  }
107880  return -1;
107881}
107882
107883/*
107884** Search the first N tables in pSrc, from left to right, looking for a
107885** table that has a column named zCol.
107886**
107887** When found, set *piTab and *piCol to the table index and column index
107888** of the matching column and return TRUE.
107889**
107890** If not found, return FALSE.
107891*/
107892static int tableAndColumnIndex(
107893  SrcList *pSrc,       /* Array of tables to search */
107894  int N,               /* Number of tables in pSrc->a[] to search */
107895  const char *zCol,    /* Name of the column we are looking for */
107896  int *piTab,          /* Write index of pSrc->a[] here */
107897  int *piCol           /* Write index of pSrc->a[*piTab].pTab->aCol[] here */
107898){
107899  int i;               /* For looping over tables in pSrc */
107900  int iCol;            /* Index of column matching zCol */
107901
107902  assert( (piTab==0)==(piCol==0) );  /* Both or neither are NULL */
107903  for(i=0; i<N; i++){
107904    iCol = columnIndex(pSrc->a[i].pTab, zCol);
107905    if( iCol>=0 ){
107906      if( piTab ){
107907        *piTab = i;
107908        *piCol = iCol;
107909      }
107910      return 1;
107911    }
107912  }
107913  return 0;
107914}
107915
107916/*
107917** This function is used to add terms implied by JOIN syntax to the
107918** WHERE clause expression of a SELECT statement. The new term, which
107919** is ANDed with the existing WHERE clause, is of the form:
107920**
107921**    (tab1.col1 = tab2.col2)
107922**
107923** where tab1 is the iSrc'th table in SrcList pSrc and tab2 is the
107924** (iSrc+1)'th. Column col1 is column iColLeft of tab1, and col2 is
107925** column iColRight of tab2.
107926*/
107927static void addWhereTerm(
107928  Parse *pParse,                  /* Parsing context */
107929  SrcList *pSrc,                  /* List of tables in FROM clause */
107930  int iLeft,                      /* Index of first table to join in pSrc */
107931  int iColLeft,                   /* Index of column in first table */
107932  int iRight,                     /* Index of second table in pSrc */
107933  int iColRight,                  /* Index of column in second table */
107934  int isOuterJoin,                /* True if this is an OUTER join */
107935  Expr **ppWhere                  /* IN/OUT: The WHERE clause to add to */
107936){
107937  sqlite3 *db = pParse->db;
107938  Expr *pE1;
107939  Expr *pE2;
107940  Expr *pEq;
107941
107942  assert( iLeft<iRight );
107943  assert( pSrc->nSrc>iRight );
107944  assert( pSrc->a[iLeft].pTab );
107945  assert( pSrc->a[iRight].pTab );
107946
107947  pE1 = sqlite3CreateColumnExpr(db, pSrc, iLeft, iColLeft);
107948  pE2 = sqlite3CreateColumnExpr(db, pSrc, iRight, iColRight);
107949
107950  pEq = sqlite3PExpr(pParse, TK_EQ, pE1, pE2, 0);
107951  if( pEq && isOuterJoin ){
107952    ExprSetProperty(pEq, EP_FromJoin);
107953    assert( !ExprHasProperty(pEq, EP_TokenOnly|EP_Reduced) );
107954    ExprSetVVAProperty(pEq, EP_NoReduce);
107955    pEq->iRightJoinTable = (i16)pE2->iTable;
107956  }
107957  *ppWhere = sqlite3ExprAnd(db, *ppWhere, pEq);
107958}
107959
107960/*
107961** Set the EP_FromJoin property on all terms of the given expression.
107962** And set the Expr.iRightJoinTable to iTable for every term in the
107963** expression.
107964**
107965** The EP_FromJoin property is used on terms of an expression to tell
107966** the LEFT OUTER JOIN processing logic that this term is part of the
107967** join restriction specified in the ON or USING clause and not a part
107968** of the more general WHERE clause.  These terms are moved over to the
107969** WHERE clause during join processing but we need to remember that they
107970** originated in the ON or USING clause.
107971**
107972** The Expr.iRightJoinTable tells the WHERE clause processing that the
107973** expression depends on table iRightJoinTable even if that table is not
107974** explicitly mentioned in the expression.  That information is needed
107975** for cases like this:
107976**
107977**    SELECT * FROM t1 LEFT JOIN t2 ON t1.a=t2.b AND t1.x=5
107978**
107979** The where clause needs to defer the handling of the t1.x=5
107980** term until after the t2 loop of the join.  In that way, a
107981** NULL t2 row will be inserted whenever t1.x!=5.  If we do not
107982** defer the handling of t1.x=5, it will be processed immediately
107983** after the t1 loop and rows with t1.x!=5 will never appear in
107984** the output, which is incorrect.
107985*/
107986static void setJoinExpr(Expr *p, int iTable){
107987  while( p ){
107988    ExprSetProperty(p, EP_FromJoin);
107989    assert( !ExprHasProperty(p, EP_TokenOnly|EP_Reduced) );
107990    ExprSetVVAProperty(p, EP_NoReduce);
107991    p->iRightJoinTable = (i16)iTable;
107992    if( p->op==TK_FUNCTION && p->x.pList ){
107993      int i;
107994      for(i=0; i<p->x.pList->nExpr; i++){
107995        setJoinExpr(p->x.pList->a[i].pExpr, iTable);
107996      }
107997    }
107998    setJoinExpr(p->pLeft, iTable);
107999    p = p->pRight;
108000  }
108001}
108002
108003/*
108004** This routine processes the join information for a SELECT statement.
108005** ON and USING clauses are converted into extra terms of the WHERE clause.
108006** NATURAL joins also create extra WHERE clause terms.
108007**
108008** The terms of a FROM clause are contained in the Select.pSrc structure.
108009** The left most table is the first entry in Select.pSrc.  The right-most
108010** table is the last entry.  The join operator is held in the entry to
108011** the left.  Thus entry 0 contains the join operator for the join between
108012** entries 0 and 1.  Any ON or USING clauses associated with the join are
108013** also attached to the left entry.
108014**
108015** This routine returns the number of errors encountered.
108016*/
108017static int sqliteProcessJoin(Parse *pParse, Select *p){
108018  SrcList *pSrc;                  /* All tables in the FROM clause */
108019  int i, j;                       /* Loop counters */
108020  struct SrcList_item *pLeft;     /* Left table being joined */
108021  struct SrcList_item *pRight;    /* Right table being joined */
108022
108023  pSrc = p->pSrc;
108024  pLeft = &pSrc->a[0];
108025  pRight = &pLeft[1];
108026  for(i=0; i<pSrc->nSrc-1; i++, pRight++, pLeft++){
108027    Table *pLeftTab = pLeft->pTab;
108028    Table *pRightTab = pRight->pTab;
108029    int isOuter;
108030
108031    if( NEVER(pLeftTab==0 || pRightTab==0) ) continue;
108032    isOuter = (pRight->jointype & JT_OUTER)!=0;
108033
108034    /* When the NATURAL keyword is present, add WHERE clause terms for
108035    ** every column that the two tables have in common.
108036    */
108037    if( pRight->jointype & JT_NATURAL ){
108038      if( pRight->pOn || pRight->pUsing ){
108039        sqlite3ErrorMsg(pParse, "a NATURAL join may not have "
108040           "an ON or USING clause", 0);
108041        return 1;
108042      }
108043      for(j=0; j<pRightTab->nCol; j++){
108044        char *zName;   /* Name of column in the right table */
108045        int iLeft;     /* Matching left table */
108046        int iLeftCol;  /* Matching column in the left table */
108047
108048        zName = pRightTab->aCol[j].zName;
108049        if( tableAndColumnIndex(pSrc, i+1, zName, &iLeft, &iLeftCol) ){
108050          addWhereTerm(pParse, pSrc, iLeft, iLeftCol, i+1, j,
108051                       isOuter, &p->pWhere);
108052        }
108053      }
108054    }
108055
108056    /* Disallow both ON and USING clauses in the same join
108057    */
108058    if( pRight->pOn && pRight->pUsing ){
108059      sqlite3ErrorMsg(pParse, "cannot have both ON and USING "
108060        "clauses in the same join");
108061      return 1;
108062    }
108063
108064    /* Add the ON clause to the end of the WHERE clause, connected by
108065    ** an AND operator.
108066    */
108067    if( pRight->pOn ){
108068      if( isOuter ) setJoinExpr(pRight->pOn, pRight->iCursor);
108069      p->pWhere = sqlite3ExprAnd(pParse->db, p->pWhere, pRight->pOn);
108070      pRight->pOn = 0;
108071    }
108072
108073    /* Create extra terms on the WHERE clause for each column named
108074    ** in the USING clause.  Example: If the two tables to be joined are
108075    ** A and B and the USING clause names X, Y, and Z, then add this
108076    ** to the WHERE clause:    A.X=B.X AND A.Y=B.Y AND A.Z=B.Z
108077    ** Report an error if any column mentioned in the USING clause is
108078    ** not contained in both tables to be joined.
108079    */
108080    if( pRight->pUsing ){
108081      IdList *pList = pRight->pUsing;
108082      for(j=0; j<pList->nId; j++){
108083        char *zName;     /* Name of the term in the USING clause */
108084        int iLeft;       /* Table on the left with matching column name */
108085        int iLeftCol;    /* Column number of matching column on the left */
108086        int iRightCol;   /* Column number of matching column on the right */
108087
108088        zName = pList->a[j].zName;
108089        iRightCol = columnIndex(pRightTab, zName);
108090        if( iRightCol<0
108091         || !tableAndColumnIndex(pSrc, i+1, zName, &iLeft, &iLeftCol)
108092        ){
108093          sqlite3ErrorMsg(pParse, "cannot join using column %s - column "
108094            "not present in both tables", zName);
108095          return 1;
108096        }
108097        addWhereTerm(pParse, pSrc, iLeft, iLeftCol, i+1, iRightCol,
108098                     isOuter, &p->pWhere);
108099      }
108100    }
108101  }
108102  return 0;
108103}
108104
108105/* Forward reference */
108106static KeyInfo *keyInfoFromExprList(
108107  Parse *pParse,       /* Parsing context */
108108  ExprList *pList,     /* Form the KeyInfo object from this ExprList */
108109  int iStart,          /* Begin with this column of pList */
108110  int nExtra           /* Add this many extra columns to the end */
108111);
108112
108113/*
108114** Generate code that will push the record in registers regData
108115** through regData+nData-1 onto the sorter.
108116*/
108117static void pushOntoSorter(
108118  Parse *pParse,         /* Parser context */
108119  SortCtx *pSort,        /* Information about the ORDER BY clause */
108120  Select *pSelect,       /* The whole SELECT statement */
108121  int regData,           /* First register holding data to be sorted */
108122  int nData,             /* Number of elements in the data array */
108123  int nPrefixReg         /* No. of reg prior to regData available for use */
108124){
108125  Vdbe *v = pParse->pVdbe;                         /* Stmt under construction */
108126  int bSeq = ((pSort->sortFlags & SORTFLAG_UseSorter)==0);
108127  int nExpr = pSort->pOrderBy->nExpr;              /* No. of ORDER BY terms */
108128  int nBase = nExpr + bSeq + nData;                /* Fields in sorter record */
108129  int regBase;                                     /* Regs for sorter record */
108130  int regRecord = ++pParse->nMem;                  /* Assembled sorter record */
108131  int nOBSat = pSort->nOBSat;                      /* ORDER BY terms to skip */
108132  int op;                            /* Opcode to add sorter record to sorter */
108133
108134  assert( bSeq==0 || bSeq==1 );
108135  if( nPrefixReg ){
108136    assert( nPrefixReg==nExpr+bSeq );
108137    regBase = regData - nExpr - bSeq;
108138  }else{
108139    regBase = pParse->nMem + 1;
108140    pParse->nMem += nBase;
108141  }
108142  sqlite3ExprCodeExprList(pParse, pSort->pOrderBy, regBase, SQLITE_ECEL_DUP);
108143  if( bSeq ){
108144    sqlite3VdbeAddOp2(v, OP_Sequence, pSort->iECursor, regBase+nExpr);
108145  }
108146  if( nPrefixReg==0 ){
108147    sqlite3ExprCodeMove(pParse, regData, regBase+nExpr+bSeq, nData);
108148  }
108149
108150  sqlite3VdbeAddOp3(v, OP_MakeRecord, regBase+nOBSat, nBase-nOBSat, regRecord);
108151  if( nOBSat>0 ){
108152    int regPrevKey;   /* The first nOBSat columns of the previous row */
108153    int addrFirst;    /* Address of the OP_IfNot opcode */
108154    int addrJmp;      /* Address of the OP_Jump opcode */
108155    VdbeOp *pOp;      /* Opcode that opens the sorter */
108156    int nKey;         /* Number of sorting key columns, including OP_Sequence */
108157    KeyInfo *pKI;     /* Original KeyInfo on the sorter table */
108158
108159    regPrevKey = pParse->nMem+1;
108160    pParse->nMem += pSort->nOBSat;
108161    nKey = nExpr - pSort->nOBSat + bSeq;
108162    if( bSeq ){
108163      addrFirst = sqlite3VdbeAddOp1(v, OP_IfNot, regBase+nExpr);
108164    }else{
108165      addrFirst = sqlite3VdbeAddOp1(v, OP_SequenceTest, pSort->iECursor);
108166    }
108167    VdbeCoverage(v);
108168    sqlite3VdbeAddOp3(v, OP_Compare, regPrevKey, regBase, pSort->nOBSat);
108169    pOp = sqlite3VdbeGetOp(v, pSort->addrSortIndex);
108170    if( pParse->db->mallocFailed ) return;
108171    pOp->p2 = nKey + nData;
108172    pKI = pOp->p4.pKeyInfo;
108173    memset(pKI->aSortOrder, 0, pKI->nField); /* Makes OP_Jump below testable */
108174    sqlite3VdbeChangeP4(v, -1, (char*)pKI, P4_KEYINFO);
108175    testcase( pKI->nXField>2 );
108176    pOp->p4.pKeyInfo = keyInfoFromExprList(pParse, pSort->pOrderBy, nOBSat,
108177                                           pKI->nXField-1);
108178    addrJmp = sqlite3VdbeCurrentAddr(v);
108179    sqlite3VdbeAddOp3(v, OP_Jump, addrJmp+1, 0, addrJmp+1); VdbeCoverage(v);
108180    pSort->labelBkOut = sqlite3VdbeMakeLabel(v);
108181    pSort->regReturn = ++pParse->nMem;
108182    sqlite3VdbeAddOp2(v, OP_Gosub, pSort->regReturn, pSort->labelBkOut);
108183    sqlite3VdbeAddOp1(v, OP_ResetSorter, pSort->iECursor);
108184    sqlite3VdbeJumpHere(v, addrFirst);
108185    sqlite3ExprCodeMove(pParse, regBase, regPrevKey, pSort->nOBSat);
108186    sqlite3VdbeJumpHere(v, addrJmp);
108187  }
108188  if( pSort->sortFlags & SORTFLAG_UseSorter ){
108189    op = OP_SorterInsert;
108190  }else{
108191    op = OP_IdxInsert;
108192  }
108193  sqlite3VdbeAddOp2(v, op, pSort->iECursor, regRecord);
108194  if( pSelect->iLimit ){
108195    int addr;
108196    int iLimit;
108197    if( pSelect->iOffset ){
108198      iLimit = pSelect->iOffset+1;
108199    }else{
108200      iLimit = pSelect->iLimit;
108201    }
108202    addr = sqlite3VdbeAddOp3(v, OP_IfNotZero, iLimit, 0, -1); VdbeCoverage(v);
108203    sqlite3VdbeAddOp1(v, OP_Last, pSort->iECursor);
108204    sqlite3VdbeAddOp1(v, OP_Delete, pSort->iECursor);
108205    sqlite3VdbeJumpHere(v, addr);
108206  }
108207}
108208
108209/*
108210** Add code to implement the OFFSET
108211*/
108212static void codeOffset(
108213  Vdbe *v,          /* Generate code into this VM */
108214  int iOffset,      /* Register holding the offset counter */
108215  int iContinue     /* Jump here to skip the current record */
108216){
108217  if( iOffset>0 ){
108218    int addr;
108219    addr = sqlite3VdbeAddOp3(v, OP_IfNeg, iOffset, 0, -1); VdbeCoverage(v);
108220    sqlite3VdbeAddOp2(v, OP_Goto, 0, iContinue);
108221    VdbeComment((v, "skip OFFSET records"));
108222    sqlite3VdbeJumpHere(v, addr);
108223  }
108224}
108225
108226/*
108227** Add code that will check to make sure the N registers starting at iMem
108228** form a distinct entry.  iTab is a sorting index that holds previously
108229** seen combinations of the N values.  A new entry is made in iTab
108230** if the current N values are new.
108231**
108232** A jump to addrRepeat is made and the N+1 values are popped from the
108233** stack if the top N elements are not distinct.
108234*/
108235static void codeDistinct(
108236  Parse *pParse,     /* Parsing and code generating context */
108237  int iTab,          /* A sorting index used to test for distinctness */
108238  int addrRepeat,    /* Jump to here if not distinct */
108239  int N,             /* Number of elements */
108240  int iMem           /* First element */
108241){
108242  Vdbe *v;
108243  int r1;
108244
108245  v = pParse->pVdbe;
108246  r1 = sqlite3GetTempReg(pParse);
108247  sqlite3VdbeAddOp4Int(v, OP_Found, iTab, addrRepeat, iMem, N); VdbeCoverage(v);
108248  sqlite3VdbeAddOp3(v, OP_MakeRecord, iMem, N, r1);
108249  sqlite3VdbeAddOp2(v, OP_IdxInsert, iTab, r1);
108250  sqlite3ReleaseTempReg(pParse, r1);
108251}
108252
108253#ifndef SQLITE_OMIT_SUBQUERY
108254/*
108255** Generate an error message when a SELECT is used within a subexpression
108256** (example:  "a IN (SELECT * FROM table)") but it has more than 1 result
108257** column.  We do this in a subroutine because the error used to occur
108258** in multiple places.  (The error only occurs in one place now, but we
108259** retain the subroutine to minimize code disruption.)
108260*/
108261static int checkForMultiColumnSelectError(
108262  Parse *pParse,       /* Parse context. */
108263  SelectDest *pDest,   /* Destination of SELECT results */
108264  int nExpr            /* Number of result columns returned by SELECT */
108265){
108266  int eDest = pDest->eDest;
108267  if( nExpr>1 && (eDest==SRT_Mem || eDest==SRT_Set) ){
108268    sqlite3ErrorMsg(pParse, "only a single result allowed for "
108269       "a SELECT that is part of an expression");
108270    return 1;
108271  }else{
108272    return 0;
108273  }
108274}
108275#endif
108276
108277/*
108278** This routine generates the code for the inside of the inner loop
108279** of a SELECT.
108280**
108281** If srcTab is negative, then the pEList expressions
108282** are evaluated in order to get the data for this row.  If srcTab is
108283** zero or more, then data is pulled from srcTab and pEList is used only
108284** to get number columns and the datatype for each column.
108285*/
108286static void selectInnerLoop(
108287  Parse *pParse,          /* The parser context */
108288  Select *p,              /* The complete select statement being coded */
108289  ExprList *pEList,       /* List of values being extracted */
108290  int srcTab,             /* Pull data from this table */
108291  SortCtx *pSort,         /* If not NULL, info on how to process ORDER BY */
108292  DistinctCtx *pDistinct, /* If not NULL, info on how to process DISTINCT */
108293  SelectDest *pDest,      /* How to dispose of the results */
108294  int iContinue,          /* Jump here to continue with next row */
108295  int iBreak              /* Jump here to break out of the inner loop */
108296){
108297  Vdbe *v = pParse->pVdbe;
108298  int i;
108299  int hasDistinct;        /* True if the DISTINCT keyword is present */
108300  int regResult;              /* Start of memory holding result set */
108301  int eDest = pDest->eDest;   /* How to dispose of results */
108302  int iParm = pDest->iSDParm; /* First argument to disposal method */
108303  int nResultCol;             /* Number of result columns */
108304  int nPrefixReg = 0;         /* Number of extra registers before regResult */
108305
108306  assert( v );
108307  assert( pEList!=0 );
108308  hasDistinct = pDistinct ? pDistinct->eTnctType : WHERE_DISTINCT_NOOP;
108309  if( pSort && pSort->pOrderBy==0 ) pSort = 0;
108310  if( pSort==0 && !hasDistinct ){
108311    assert( iContinue!=0 );
108312    codeOffset(v, p->iOffset, iContinue);
108313  }
108314
108315  /* Pull the requested columns.
108316  */
108317  nResultCol = pEList->nExpr;
108318
108319  if( pDest->iSdst==0 ){
108320    if( pSort ){
108321      nPrefixReg = pSort->pOrderBy->nExpr;
108322      if( !(pSort->sortFlags & SORTFLAG_UseSorter) ) nPrefixReg++;
108323      pParse->nMem += nPrefixReg;
108324    }
108325    pDest->iSdst = pParse->nMem+1;
108326    pParse->nMem += nResultCol;
108327  }else if( pDest->iSdst+nResultCol > pParse->nMem ){
108328    /* This is an error condition that can result, for example, when a SELECT
108329    ** on the right-hand side of an INSERT contains more result columns than
108330    ** there are columns in the table on the left.  The error will be caught
108331    ** and reported later.  But we need to make sure enough memory is allocated
108332    ** to avoid other spurious errors in the meantime. */
108333    pParse->nMem += nResultCol;
108334  }
108335  pDest->nSdst = nResultCol;
108336  regResult = pDest->iSdst;
108337  if( srcTab>=0 ){
108338    for(i=0; i<nResultCol; i++){
108339      sqlite3VdbeAddOp3(v, OP_Column, srcTab, i, regResult+i);
108340      VdbeComment((v, "%s", pEList->a[i].zName));
108341    }
108342  }else if( eDest!=SRT_Exists ){
108343    /* If the destination is an EXISTS(...) expression, the actual
108344    ** values returned by the SELECT are not required.
108345    */
108346    u8 ecelFlags;
108347    if( eDest==SRT_Mem || eDest==SRT_Output || eDest==SRT_Coroutine ){
108348      ecelFlags = SQLITE_ECEL_DUP;
108349    }else{
108350      ecelFlags = 0;
108351    }
108352    sqlite3ExprCodeExprList(pParse, pEList, regResult, ecelFlags);
108353  }
108354
108355  /* If the DISTINCT keyword was present on the SELECT statement
108356  ** and this row has been seen before, then do not make this row
108357  ** part of the result.
108358  */
108359  if( hasDistinct ){
108360    switch( pDistinct->eTnctType ){
108361      case WHERE_DISTINCT_ORDERED: {
108362        VdbeOp *pOp;            /* No longer required OpenEphemeral instr. */
108363        int iJump;              /* Jump destination */
108364        int regPrev;            /* Previous row content */
108365
108366        /* Allocate space for the previous row */
108367        regPrev = pParse->nMem+1;
108368        pParse->nMem += nResultCol;
108369
108370        /* Change the OP_OpenEphemeral coded earlier to an OP_Null
108371        ** sets the MEM_Cleared bit on the first register of the
108372        ** previous value.  This will cause the OP_Ne below to always
108373        ** fail on the first iteration of the loop even if the first
108374        ** row is all NULLs.
108375        */
108376        sqlite3VdbeChangeToNoop(v, pDistinct->addrTnct);
108377        pOp = sqlite3VdbeGetOp(v, pDistinct->addrTnct);
108378        pOp->opcode = OP_Null;
108379        pOp->p1 = 1;
108380        pOp->p2 = regPrev;
108381
108382        iJump = sqlite3VdbeCurrentAddr(v) + nResultCol;
108383        for(i=0; i<nResultCol; i++){
108384          CollSeq *pColl = sqlite3ExprCollSeq(pParse, pEList->a[i].pExpr);
108385          if( i<nResultCol-1 ){
108386            sqlite3VdbeAddOp3(v, OP_Ne, regResult+i, iJump, regPrev+i);
108387            VdbeCoverage(v);
108388          }else{
108389            sqlite3VdbeAddOp3(v, OP_Eq, regResult+i, iContinue, regPrev+i);
108390            VdbeCoverage(v);
108391           }
108392          sqlite3VdbeChangeP4(v, -1, (const char *)pColl, P4_COLLSEQ);
108393          sqlite3VdbeChangeP5(v, SQLITE_NULLEQ);
108394        }
108395        assert( sqlite3VdbeCurrentAddr(v)==iJump || pParse->db->mallocFailed );
108396        sqlite3VdbeAddOp3(v, OP_Copy, regResult, regPrev, nResultCol-1);
108397        break;
108398      }
108399
108400      case WHERE_DISTINCT_UNIQUE: {
108401        sqlite3VdbeChangeToNoop(v, pDistinct->addrTnct);
108402        break;
108403      }
108404
108405      default: {
108406        assert( pDistinct->eTnctType==WHERE_DISTINCT_UNORDERED );
108407        codeDistinct(pParse, pDistinct->tabTnct, iContinue, nResultCol,
108408                     regResult);
108409        break;
108410      }
108411    }
108412    if( pSort==0 ){
108413      codeOffset(v, p->iOffset, iContinue);
108414    }
108415  }
108416
108417  switch( eDest ){
108418    /* In this mode, write each query result to the key of the temporary
108419    ** table iParm.
108420    */
108421#ifndef SQLITE_OMIT_COMPOUND_SELECT
108422    case SRT_Union: {
108423      int r1;
108424      r1 = sqlite3GetTempReg(pParse);
108425      sqlite3VdbeAddOp3(v, OP_MakeRecord, regResult, nResultCol, r1);
108426      sqlite3VdbeAddOp2(v, OP_IdxInsert, iParm, r1);
108427      sqlite3ReleaseTempReg(pParse, r1);
108428      break;
108429    }
108430
108431    /* Construct a record from the query result, but instead of
108432    ** saving that record, use it as a key to delete elements from
108433    ** the temporary table iParm.
108434    */
108435    case SRT_Except: {
108436      sqlite3VdbeAddOp3(v, OP_IdxDelete, iParm, regResult, nResultCol);
108437      break;
108438    }
108439#endif /* SQLITE_OMIT_COMPOUND_SELECT */
108440
108441    /* Store the result as data using a unique key.
108442    */
108443    case SRT_Fifo:
108444    case SRT_DistFifo:
108445    case SRT_Table:
108446    case SRT_EphemTab: {
108447      int r1 = sqlite3GetTempRange(pParse, nPrefixReg+1);
108448      testcase( eDest==SRT_Table );
108449      testcase( eDest==SRT_EphemTab );
108450      testcase( eDest==SRT_Fifo );
108451      testcase( eDest==SRT_DistFifo );
108452      sqlite3VdbeAddOp3(v, OP_MakeRecord, regResult, nResultCol, r1+nPrefixReg);
108453#ifndef SQLITE_OMIT_CTE
108454      if( eDest==SRT_DistFifo ){
108455        /* If the destination is DistFifo, then cursor (iParm+1) is open
108456        ** on an ephemeral index. If the current row is already present
108457        ** in the index, do not write it to the output. If not, add the
108458        ** current row to the index and proceed with writing it to the
108459        ** output table as well.  */
108460        int addr = sqlite3VdbeCurrentAddr(v) + 4;
108461        sqlite3VdbeAddOp4Int(v, OP_Found, iParm+1, addr, r1, 0);
108462        VdbeCoverage(v);
108463        sqlite3VdbeAddOp2(v, OP_IdxInsert, iParm+1, r1);
108464        assert( pSort==0 );
108465      }
108466#endif
108467      if( pSort ){
108468        pushOntoSorter(pParse, pSort, p, r1+nPrefixReg, 1, nPrefixReg);
108469      }else{
108470        int r2 = sqlite3GetTempReg(pParse);
108471        sqlite3VdbeAddOp2(v, OP_NewRowid, iParm, r2);
108472        sqlite3VdbeAddOp3(v, OP_Insert, iParm, r1, r2);
108473        sqlite3VdbeChangeP5(v, OPFLAG_APPEND);
108474        sqlite3ReleaseTempReg(pParse, r2);
108475      }
108476      sqlite3ReleaseTempRange(pParse, r1, nPrefixReg+1);
108477      break;
108478    }
108479
108480#ifndef SQLITE_OMIT_SUBQUERY
108481    /* If we are creating a set for an "expr IN (SELECT ...)" construct,
108482    ** then there should be a single item on the stack.  Write this
108483    ** item into the set table with bogus data.
108484    */
108485    case SRT_Set: {
108486      assert( nResultCol==1 );
108487      pDest->affSdst =
108488                  sqlite3CompareAffinity(pEList->a[0].pExpr, pDest->affSdst);
108489      if( pSort ){
108490        /* At first glance you would think we could optimize out the
108491        ** ORDER BY in this case since the order of entries in the set
108492        ** does not matter.  But there might be a LIMIT clause, in which
108493        ** case the order does matter */
108494        pushOntoSorter(pParse, pSort, p, regResult, 1, nPrefixReg);
108495      }else{
108496        int r1 = sqlite3GetTempReg(pParse);
108497        sqlite3VdbeAddOp4(v, OP_MakeRecord, regResult,1,r1, &pDest->affSdst, 1);
108498        sqlite3ExprCacheAffinityChange(pParse, regResult, 1);
108499        sqlite3VdbeAddOp2(v, OP_IdxInsert, iParm, r1);
108500        sqlite3ReleaseTempReg(pParse, r1);
108501      }
108502      break;
108503    }
108504
108505    /* If any row exist in the result set, record that fact and abort.
108506    */
108507    case SRT_Exists: {
108508      sqlite3VdbeAddOp2(v, OP_Integer, 1, iParm);
108509      /* The LIMIT clause will terminate the loop for us */
108510      break;
108511    }
108512
108513    /* If this is a scalar select that is part of an expression, then
108514    ** store the results in the appropriate memory cell and break out
108515    ** of the scan loop.
108516    */
108517    case SRT_Mem: {
108518      assert( nResultCol==1 );
108519      if( pSort ){
108520        pushOntoSorter(pParse, pSort, p, regResult, 1, nPrefixReg);
108521      }else{
108522        assert( regResult==iParm );
108523        /* The LIMIT clause will jump out of the loop for us */
108524      }
108525      break;
108526    }
108527#endif /* #ifndef SQLITE_OMIT_SUBQUERY */
108528
108529    case SRT_Coroutine:       /* Send data to a co-routine */
108530    case SRT_Output: {        /* Return the results */
108531      testcase( eDest==SRT_Coroutine );
108532      testcase( eDest==SRT_Output );
108533      if( pSort ){
108534        pushOntoSorter(pParse, pSort, p, regResult, nResultCol, nPrefixReg);
108535      }else if( eDest==SRT_Coroutine ){
108536        sqlite3VdbeAddOp1(v, OP_Yield, pDest->iSDParm);
108537      }else{
108538        sqlite3VdbeAddOp2(v, OP_ResultRow, regResult, nResultCol);
108539        sqlite3ExprCacheAffinityChange(pParse, regResult, nResultCol);
108540      }
108541      break;
108542    }
108543
108544#ifndef SQLITE_OMIT_CTE
108545    /* Write the results into a priority queue that is order according to
108546    ** pDest->pOrderBy (in pSO).  pDest->iSDParm (in iParm) is the cursor for an
108547    ** index with pSO->nExpr+2 columns.  Build a key using pSO for the first
108548    ** pSO->nExpr columns, then make sure all keys are unique by adding a
108549    ** final OP_Sequence column.  The last column is the record as a blob.
108550    */
108551    case SRT_DistQueue:
108552    case SRT_Queue: {
108553      int nKey;
108554      int r1, r2, r3;
108555      int addrTest = 0;
108556      ExprList *pSO;
108557      pSO = pDest->pOrderBy;
108558      assert( pSO );
108559      nKey = pSO->nExpr;
108560      r1 = sqlite3GetTempReg(pParse);
108561      r2 = sqlite3GetTempRange(pParse, nKey+2);
108562      r3 = r2+nKey+1;
108563      if( eDest==SRT_DistQueue ){
108564        /* If the destination is DistQueue, then cursor (iParm+1) is open
108565        ** on a second ephemeral index that holds all values every previously
108566        ** added to the queue. */
108567        addrTest = sqlite3VdbeAddOp4Int(v, OP_Found, iParm+1, 0,
108568                                        regResult, nResultCol);
108569        VdbeCoverage(v);
108570      }
108571      sqlite3VdbeAddOp3(v, OP_MakeRecord, regResult, nResultCol, r3);
108572      if( eDest==SRT_DistQueue ){
108573        sqlite3VdbeAddOp2(v, OP_IdxInsert, iParm+1, r3);
108574        sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT);
108575      }
108576      for(i=0; i<nKey; i++){
108577        sqlite3VdbeAddOp2(v, OP_SCopy,
108578                          regResult + pSO->a[i].u.x.iOrderByCol - 1,
108579                          r2+i);
108580      }
108581      sqlite3VdbeAddOp2(v, OP_Sequence, iParm, r2+nKey);
108582      sqlite3VdbeAddOp3(v, OP_MakeRecord, r2, nKey+2, r1);
108583      sqlite3VdbeAddOp2(v, OP_IdxInsert, iParm, r1);
108584      if( addrTest ) sqlite3VdbeJumpHere(v, addrTest);
108585      sqlite3ReleaseTempReg(pParse, r1);
108586      sqlite3ReleaseTempRange(pParse, r2, nKey+2);
108587      break;
108588    }
108589#endif /* SQLITE_OMIT_CTE */
108590
108591
108592
108593#if !defined(SQLITE_OMIT_TRIGGER)
108594    /* Discard the results.  This is used for SELECT statements inside
108595    ** the body of a TRIGGER.  The purpose of such selects is to call
108596    ** user-defined functions that have side effects.  We do not care
108597    ** about the actual results of the select.
108598    */
108599    default: {
108600      assert( eDest==SRT_Discard );
108601      break;
108602    }
108603#endif
108604  }
108605
108606  /* Jump to the end of the loop if the LIMIT is reached.  Except, if
108607  ** there is a sorter, in which case the sorter has already limited
108608  ** the output for us.
108609  */
108610  if( pSort==0 && p->iLimit ){
108611    sqlite3VdbeAddOp2(v, OP_DecrJumpZero, p->iLimit, iBreak); VdbeCoverage(v);
108612  }
108613}
108614
108615/*
108616** Allocate a KeyInfo object sufficient for an index of N key columns and
108617** X extra columns.
108618*/
108619SQLITE_PRIVATE KeyInfo *sqlite3KeyInfoAlloc(sqlite3 *db, int N, int X){
108620  KeyInfo *p = sqlite3DbMallocZero(0,
108621                   sizeof(KeyInfo) + (N+X)*(sizeof(CollSeq*)+1));
108622  if( p ){
108623    p->aSortOrder = (u8*)&p->aColl[N+X];
108624    p->nField = (u16)N;
108625    p->nXField = (u16)X;
108626    p->enc = ENC(db);
108627    p->db = db;
108628    p->nRef = 1;
108629  }else{
108630    db->mallocFailed = 1;
108631  }
108632  return p;
108633}
108634
108635/*
108636** Deallocate a KeyInfo object
108637*/
108638SQLITE_PRIVATE void sqlite3KeyInfoUnref(KeyInfo *p){
108639  if( p ){
108640    assert( p->nRef>0 );
108641    p->nRef--;
108642    if( p->nRef==0 ) sqlite3DbFree(0, p);
108643  }
108644}
108645
108646/*
108647** Make a new pointer to a KeyInfo object
108648*/
108649SQLITE_PRIVATE KeyInfo *sqlite3KeyInfoRef(KeyInfo *p){
108650  if( p ){
108651    assert( p->nRef>0 );
108652    p->nRef++;
108653  }
108654  return p;
108655}
108656
108657#ifdef SQLITE_DEBUG
108658/*
108659** Return TRUE if a KeyInfo object can be change.  The KeyInfo object
108660** can only be changed if this is just a single reference to the object.
108661**
108662** This routine is used only inside of assert() statements.
108663*/
108664SQLITE_PRIVATE int sqlite3KeyInfoIsWriteable(KeyInfo *p){ return p->nRef==1; }
108665#endif /* SQLITE_DEBUG */
108666
108667/*
108668** Given an expression list, generate a KeyInfo structure that records
108669** the collating sequence for each expression in that expression list.
108670**
108671** If the ExprList is an ORDER BY or GROUP BY clause then the resulting
108672** KeyInfo structure is appropriate for initializing a virtual index to
108673** implement that clause.  If the ExprList is the result set of a SELECT
108674** then the KeyInfo structure is appropriate for initializing a virtual
108675** index to implement a DISTINCT test.
108676**
108677** Space to hold the KeyInfo structure is obtained from malloc.  The calling
108678** function is responsible for seeing that this structure is eventually
108679** freed.
108680*/
108681static KeyInfo *keyInfoFromExprList(
108682  Parse *pParse,       /* Parsing context */
108683  ExprList *pList,     /* Form the KeyInfo object from this ExprList */
108684  int iStart,          /* Begin with this column of pList */
108685  int nExtra           /* Add this many extra columns to the end */
108686){
108687  int nExpr;
108688  KeyInfo *pInfo;
108689  struct ExprList_item *pItem;
108690  sqlite3 *db = pParse->db;
108691  int i;
108692
108693  nExpr = pList->nExpr;
108694  pInfo = sqlite3KeyInfoAlloc(db, nExpr-iStart, nExtra+1);
108695  if( pInfo ){
108696    assert( sqlite3KeyInfoIsWriteable(pInfo) );
108697    for(i=iStart, pItem=pList->a+iStart; i<nExpr; i++, pItem++){
108698      CollSeq *pColl;
108699      pColl = sqlite3ExprCollSeq(pParse, pItem->pExpr);
108700      if( !pColl ) pColl = db->pDfltColl;
108701      pInfo->aColl[i-iStart] = pColl;
108702      pInfo->aSortOrder[i-iStart] = pItem->sortOrder;
108703    }
108704  }
108705  return pInfo;
108706}
108707
108708/*
108709** Name of the connection operator, used for error messages.
108710*/
108711static const char *selectOpName(int id){
108712  char *z;
108713  switch( id ){
108714    case TK_ALL:       z = "UNION ALL";   break;
108715    case TK_INTERSECT: z = "INTERSECT";   break;
108716    case TK_EXCEPT:    z = "EXCEPT";      break;
108717    default:           z = "UNION";       break;
108718  }
108719  return z;
108720}
108721
108722#ifndef SQLITE_OMIT_EXPLAIN
108723/*
108724** Unless an "EXPLAIN QUERY PLAN" command is being processed, this function
108725** is a no-op. Otherwise, it adds a single row of output to the EQP result,
108726** where the caption is of the form:
108727**
108728**   "USE TEMP B-TREE FOR xxx"
108729**
108730** where xxx is one of "DISTINCT", "ORDER BY" or "GROUP BY". Exactly which
108731** is determined by the zUsage argument.
108732*/
108733static void explainTempTable(Parse *pParse, const char *zUsage){
108734  if( pParse->explain==2 ){
108735    Vdbe *v = pParse->pVdbe;
108736    char *zMsg = sqlite3MPrintf(pParse->db, "USE TEMP B-TREE FOR %s", zUsage);
108737    sqlite3VdbeAddOp4(v, OP_Explain, pParse->iSelectId, 0, 0, zMsg, P4_DYNAMIC);
108738  }
108739}
108740
108741/*
108742** Assign expression b to lvalue a. A second, no-op, version of this macro
108743** is provided when SQLITE_OMIT_EXPLAIN is defined. This allows the code
108744** in sqlite3Select() to assign values to structure member variables that
108745** only exist if SQLITE_OMIT_EXPLAIN is not defined without polluting the
108746** code with #ifndef directives.
108747*/
108748# define explainSetInteger(a, b) a = b
108749
108750#else
108751/* No-op versions of the explainXXX() functions and macros. */
108752# define explainTempTable(y,z)
108753# define explainSetInteger(y,z)
108754#endif
108755
108756#if !defined(SQLITE_OMIT_EXPLAIN) && !defined(SQLITE_OMIT_COMPOUND_SELECT)
108757/*
108758** Unless an "EXPLAIN QUERY PLAN" command is being processed, this function
108759** is a no-op. Otherwise, it adds a single row of output to the EQP result,
108760** where the caption is of one of the two forms:
108761**
108762**   "COMPOSITE SUBQUERIES iSub1 and iSub2 (op)"
108763**   "COMPOSITE SUBQUERIES iSub1 and iSub2 USING TEMP B-TREE (op)"
108764**
108765** where iSub1 and iSub2 are the integers passed as the corresponding
108766** function parameters, and op is the text representation of the parameter
108767** of the same name. The parameter "op" must be one of TK_UNION, TK_EXCEPT,
108768** TK_INTERSECT or TK_ALL. The first form is used if argument bUseTmp is
108769** false, or the second form if it is true.
108770*/
108771static void explainComposite(
108772  Parse *pParse,                  /* Parse context */
108773  int op,                         /* One of TK_UNION, TK_EXCEPT etc. */
108774  int iSub1,                      /* Subquery id 1 */
108775  int iSub2,                      /* Subquery id 2 */
108776  int bUseTmp                     /* True if a temp table was used */
108777){
108778  assert( op==TK_UNION || op==TK_EXCEPT || op==TK_INTERSECT || op==TK_ALL );
108779  if( pParse->explain==2 ){
108780    Vdbe *v = pParse->pVdbe;
108781    char *zMsg = sqlite3MPrintf(
108782        pParse->db, "COMPOUND SUBQUERIES %d AND %d %s(%s)", iSub1, iSub2,
108783        bUseTmp?"USING TEMP B-TREE ":"", selectOpName(op)
108784    );
108785    sqlite3VdbeAddOp4(v, OP_Explain, pParse->iSelectId, 0, 0, zMsg, P4_DYNAMIC);
108786  }
108787}
108788#else
108789/* No-op versions of the explainXXX() functions and macros. */
108790# define explainComposite(v,w,x,y,z)
108791#endif
108792
108793/*
108794** If the inner loop was generated using a non-null pOrderBy argument,
108795** then the results were placed in a sorter.  After the loop is terminated
108796** we need to run the sorter and output the results.  The following
108797** routine generates the code needed to do that.
108798*/
108799static void generateSortTail(
108800  Parse *pParse,    /* Parsing context */
108801  Select *p,        /* The SELECT statement */
108802  SortCtx *pSort,   /* Information on the ORDER BY clause */
108803  int nColumn,      /* Number of columns of data */
108804  SelectDest *pDest /* Write the sorted results here */
108805){
108806  Vdbe *v = pParse->pVdbe;                     /* The prepared statement */
108807  int addrBreak = sqlite3VdbeMakeLabel(v);     /* Jump here to exit loop */
108808  int addrContinue = sqlite3VdbeMakeLabel(v);  /* Jump here for next cycle */
108809  int addr;
108810  int addrOnce = 0;
108811  int iTab;
108812  ExprList *pOrderBy = pSort->pOrderBy;
108813  int eDest = pDest->eDest;
108814  int iParm = pDest->iSDParm;
108815  int regRow;
108816  int regRowid;
108817  int nKey;
108818  int iSortTab;                   /* Sorter cursor to read from */
108819  int nSortData;                  /* Trailing values to read from sorter */
108820  int i;
108821  int bSeq;                       /* True if sorter record includes seq. no. */
108822#ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
108823  struct ExprList_item *aOutEx = p->pEList->a;
108824#endif
108825
108826  if( pSort->labelBkOut ){
108827    sqlite3VdbeAddOp2(v, OP_Gosub, pSort->regReturn, pSort->labelBkOut);
108828    sqlite3VdbeAddOp2(v, OP_Goto, 0, addrBreak);
108829    sqlite3VdbeResolveLabel(v, pSort->labelBkOut);
108830  }
108831  iTab = pSort->iECursor;
108832  if( eDest==SRT_Output || eDest==SRT_Coroutine ){
108833    regRowid = 0;
108834    regRow = pDest->iSdst;
108835    nSortData = nColumn;
108836  }else{
108837    regRowid = sqlite3GetTempReg(pParse);
108838    regRow = sqlite3GetTempReg(pParse);
108839    nSortData = 1;
108840  }
108841  nKey = pOrderBy->nExpr - pSort->nOBSat;
108842  if( pSort->sortFlags & SORTFLAG_UseSorter ){
108843    int regSortOut = ++pParse->nMem;
108844    iSortTab = pParse->nTab++;
108845    if( pSort->labelBkOut ){
108846      addrOnce = sqlite3CodeOnce(pParse); VdbeCoverage(v);
108847    }
108848    sqlite3VdbeAddOp3(v, OP_OpenPseudo, iSortTab, regSortOut, nKey+1+nSortData);
108849    if( addrOnce ) sqlite3VdbeJumpHere(v, addrOnce);
108850    addr = 1 + sqlite3VdbeAddOp2(v, OP_SorterSort, iTab, addrBreak);
108851    VdbeCoverage(v);
108852    codeOffset(v, p->iOffset, addrContinue);
108853    sqlite3VdbeAddOp3(v, OP_SorterData, iTab, regSortOut, iSortTab);
108854    bSeq = 0;
108855  }else{
108856    addr = 1 + sqlite3VdbeAddOp2(v, OP_Sort, iTab, addrBreak); VdbeCoverage(v);
108857    codeOffset(v, p->iOffset, addrContinue);
108858    iSortTab = iTab;
108859    bSeq = 1;
108860  }
108861  for(i=0; i<nSortData; i++){
108862    sqlite3VdbeAddOp3(v, OP_Column, iSortTab, nKey+bSeq+i, regRow+i);
108863    VdbeComment((v, "%s", aOutEx[i].zName ? aOutEx[i].zName : aOutEx[i].zSpan));
108864  }
108865  switch( eDest ){
108866    case SRT_EphemTab: {
108867      sqlite3VdbeAddOp2(v, OP_NewRowid, iParm, regRowid);
108868      sqlite3VdbeAddOp3(v, OP_Insert, iParm, regRow, regRowid);
108869      sqlite3VdbeChangeP5(v, OPFLAG_APPEND);
108870      break;
108871    }
108872#ifndef SQLITE_OMIT_SUBQUERY
108873    case SRT_Set: {
108874      assert( nColumn==1 );
108875      sqlite3VdbeAddOp4(v, OP_MakeRecord, regRow, 1, regRowid,
108876                        &pDest->affSdst, 1);
108877      sqlite3ExprCacheAffinityChange(pParse, regRow, 1);
108878      sqlite3VdbeAddOp2(v, OP_IdxInsert, iParm, regRowid);
108879      break;
108880    }
108881    case SRT_Mem: {
108882      assert( nColumn==1 );
108883      sqlite3ExprCodeMove(pParse, regRow, iParm, 1);
108884      /* The LIMIT clause will terminate the loop for us */
108885      break;
108886    }
108887#endif
108888    default: {
108889      assert( eDest==SRT_Output || eDest==SRT_Coroutine );
108890      testcase( eDest==SRT_Output );
108891      testcase( eDest==SRT_Coroutine );
108892      if( eDest==SRT_Output ){
108893        sqlite3VdbeAddOp2(v, OP_ResultRow, pDest->iSdst, nColumn);
108894        sqlite3ExprCacheAffinityChange(pParse, pDest->iSdst, nColumn);
108895      }else{
108896        sqlite3VdbeAddOp1(v, OP_Yield, pDest->iSDParm);
108897      }
108898      break;
108899    }
108900  }
108901  if( regRowid ){
108902    sqlite3ReleaseTempReg(pParse, regRow);
108903    sqlite3ReleaseTempReg(pParse, regRowid);
108904  }
108905  /* The bottom of the loop
108906  */
108907  sqlite3VdbeResolveLabel(v, addrContinue);
108908  if( pSort->sortFlags & SORTFLAG_UseSorter ){
108909    sqlite3VdbeAddOp2(v, OP_SorterNext, iTab, addr); VdbeCoverage(v);
108910  }else{
108911    sqlite3VdbeAddOp2(v, OP_Next, iTab, addr); VdbeCoverage(v);
108912  }
108913  if( pSort->regReturn ) sqlite3VdbeAddOp1(v, OP_Return, pSort->regReturn);
108914  sqlite3VdbeResolveLabel(v, addrBreak);
108915}
108916
108917/*
108918** Return a pointer to a string containing the 'declaration type' of the
108919** expression pExpr. The string may be treated as static by the caller.
108920**
108921** Also try to estimate the size of the returned value and return that
108922** result in *pEstWidth.
108923**
108924** The declaration type is the exact datatype definition extracted from the
108925** original CREATE TABLE statement if the expression is a column. The
108926** declaration type for a ROWID field is INTEGER. Exactly when an expression
108927** is considered a column can be complex in the presence of subqueries. The
108928** result-set expression in all of the following SELECT statements is
108929** considered a column by this function.
108930**
108931**   SELECT col FROM tbl;
108932**   SELECT (SELECT col FROM tbl;
108933**   SELECT (SELECT col FROM tbl);
108934**   SELECT abc FROM (SELECT col AS abc FROM tbl);
108935**
108936** The declaration type for any expression other than a column is NULL.
108937**
108938** This routine has either 3 or 6 parameters depending on whether or not
108939** the SQLITE_ENABLE_COLUMN_METADATA compile-time option is used.
108940*/
108941#ifdef SQLITE_ENABLE_COLUMN_METADATA
108942# define columnType(A,B,C,D,E,F) columnTypeImpl(A,B,C,D,E,F)
108943#else /* if !defined(SQLITE_ENABLE_COLUMN_METADATA) */
108944# define columnType(A,B,C,D,E,F) columnTypeImpl(A,B,F)
108945#endif
108946static const char *columnTypeImpl(
108947  NameContext *pNC,
108948  Expr *pExpr,
108949#ifdef SQLITE_ENABLE_COLUMN_METADATA
108950  const char **pzOrigDb,
108951  const char **pzOrigTab,
108952  const char **pzOrigCol,
108953#endif
108954  u8 *pEstWidth
108955){
108956  char const *zType = 0;
108957  int j;
108958  u8 estWidth = 1;
108959#ifdef SQLITE_ENABLE_COLUMN_METADATA
108960  char const *zOrigDb = 0;
108961  char const *zOrigTab = 0;
108962  char const *zOrigCol = 0;
108963#endif
108964
108965  if( NEVER(pExpr==0) || pNC->pSrcList==0 ) return 0;
108966  switch( pExpr->op ){
108967    case TK_AGG_COLUMN:
108968    case TK_COLUMN: {
108969      /* The expression is a column. Locate the table the column is being
108970      ** extracted from in NameContext.pSrcList. This table may be real
108971      ** database table or a subquery.
108972      */
108973      Table *pTab = 0;            /* Table structure column is extracted from */
108974      Select *pS = 0;             /* Select the column is extracted from */
108975      int iCol = pExpr->iColumn;  /* Index of column in pTab */
108976      testcase( pExpr->op==TK_AGG_COLUMN );
108977      testcase( pExpr->op==TK_COLUMN );
108978      while( pNC && !pTab ){
108979        SrcList *pTabList = pNC->pSrcList;
108980        for(j=0;j<pTabList->nSrc && pTabList->a[j].iCursor!=pExpr->iTable;j++);
108981        if( j<pTabList->nSrc ){
108982          pTab = pTabList->a[j].pTab;
108983          pS = pTabList->a[j].pSelect;
108984        }else{
108985          pNC = pNC->pNext;
108986        }
108987      }
108988
108989      if( pTab==0 ){
108990        /* At one time, code such as "SELECT new.x" within a trigger would
108991        ** cause this condition to run.  Since then, we have restructured how
108992        ** trigger code is generated and so this condition is no longer
108993        ** possible. However, it can still be true for statements like
108994        ** the following:
108995        **
108996        **   CREATE TABLE t1(col INTEGER);
108997        **   SELECT (SELECT t1.col) FROM FROM t1;
108998        **
108999        ** when columnType() is called on the expression "t1.col" in the
109000        ** sub-select. In this case, set the column type to NULL, even
109001        ** though it should really be "INTEGER".
109002        **
109003        ** This is not a problem, as the column type of "t1.col" is never
109004        ** used. When columnType() is called on the expression
109005        ** "(SELECT t1.col)", the correct type is returned (see the TK_SELECT
109006        ** branch below.  */
109007        break;
109008      }
109009
109010      assert( pTab && pExpr->pTab==pTab );
109011      if( pS ){
109012        /* The "table" is actually a sub-select or a view in the FROM clause
109013        ** of the SELECT statement. Return the declaration type and origin
109014        ** data for the result-set column of the sub-select.
109015        */
109016        if( iCol>=0 && ALWAYS(iCol<pS->pEList->nExpr) ){
109017          /* If iCol is less than zero, then the expression requests the
109018          ** rowid of the sub-select or view. This expression is legal (see
109019          ** test case misc2.2.2) - it always evaluates to NULL.
109020          **
109021          ** The ALWAYS() is because iCol>=pS->pEList->nExpr will have been
109022          ** caught already by name resolution.
109023          */
109024          NameContext sNC;
109025          Expr *p = pS->pEList->a[iCol].pExpr;
109026          sNC.pSrcList = pS->pSrc;
109027          sNC.pNext = pNC;
109028          sNC.pParse = pNC->pParse;
109029          zType = columnType(&sNC, p,&zOrigDb,&zOrigTab,&zOrigCol, &estWidth);
109030        }
109031      }else if( pTab->pSchema ){
109032        /* A real table */
109033        assert( !pS );
109034        if( iCol<0 ) iCol = pTab->iPKey;
109035        assert( iCol==-1 || (iCol>=0 && iCol<pTab->nCol) );
109036#ifdef SQLITE_ENABLE_COLUMN_METADATA
109037        if( iCol<0 ){
109038          zType = "INTEGER";
109039          zOrigCol = "rowid";
109040        }else{
109041          zType = pTab->aCol[iCol].zType;
109042          zOrigCol = pTab->aCol[iCol].zName;
109043          estWidth = pTab->aCol[iCol].szEst;
109044        }
109045        zOrigTab = pTab->zName;
109046        if( pNC->pParse ){
109047          int iDb = sqlite3SchemaToIndex(pNC->pParse->db, pTab->pSchema);
109048          zOrigDb = pNC->pParse->db->aDb[iDb].zName;
109049        }
109050#else
109051        if( iCol<0 ){
109052          zType = "INTEGER";
109053        }else{
109054          zType = pTab->aCol[iCol].zType;
109055          estWidth = pTab->aCol[iCol].szEst;
109056        }
109057#endif
109058      }
109059      break;
109060    }
109061#ifndef SQLITE_OMIT_SUBQUERY
109062    case TK_SELECT: {
109063      /* The expression is a sub-select. Return the declaration type and
109064      ** origin info for the single column in the result set of the SELECT
109065      ** statement.
109066      */
109067      NameContext sNC;
109068      Select *pS = pExpr->x.pSelect;
109069      Expr *p = pS->pEList->a[0].pExpr;
109070      assert( ExprHasProperty(pExpr, EP_xIsSelect) );
109071      sNC.pSrcList = pS->pSrc;
109072      sNC.pNext = pNC;
109073      sNC.pParse = pNC->pParse;
109074      zType = columnType(&sNC, p, &zOrigDb, &zOrigTab, &zOrigCol, &estWidth);
109075      break;
109076    }
109077#endif
109078  }
109079
109080#ifdef SQLITE_ENABLE_COLUMN_METADATA
109081  if( pzOrigDb ){
109082    assert( pzOrigTab && pzOrigCol );
109083    *pzOrigDb = zOrigDb;
109084    *pzOrigTab = zOrigTab;
109085    *pzOrigCol = zOrigCol;
109086  }
109087#endif
109088  if( pEstWidth ) *pEstWidth = estWidth;
109089  return zType;
109090}
109091
109092/*
109093** Generate code that will tell the VDBE the declaration types of columns
109094** in the result set.
109095*/
109096static void generateColumnTypes(
109097  Parse *pParse,      /* Parser context */
109098  SrcList *pTabList,  /* List of tables */
109099  ExprList *pEList    /* Expressions defining the result set */
109100){
109101#ifndef SQLITE_OMIT_DECLTYPE
109102  Vdbe *v = pParse->pVdbe;
109103  int i;
109104  NameContext sNC;
109105  sNC.pSrcList = pTabList;
109106  sNC.pParse = pParse;
109107  for(i=0; i<pEList->nExpr; i++){
109108    Expr *p = pEList->a[i].pExpr;
109109    const char *zType;
109110#ifdef SQLITE_ENABLE_COLUMN_METADATA
109111    const char *zOrigDb = 0;
109112    const char *zOrigTab = 0;
109113    const char *zOrigCol = 0;
109114    zType = columnType(&sNC, p, &zOrigDb, &zOrigTab, &zOrigCol, 0);
109115
109116    /* The vdbe must make its own copy of the column-type and other
109117    ** column specific strings, in case the schema is reset before this
109118    ** virtual machine is deleted.
109119    */
109120    sqlite3VdbeSetColName(v, i, COLNAME_DATABASE, zOrigDb, SQLITE_TRANSIENT);
109121    sqlite3VdbeSetColName(v, i, COLNAME_TABLE, zOrigTab, SQLITE_TRANSIENT);
109122    sqlite3VdbeSetColName(v, i, COLNAME_COLUMN, zOrigCol, SQLITE_TRANSIENT);
109123#else
109124    zType = columnType(&sNC, p, 0, 0, 0, 0);
109125#endif
109126    sqlite3VdbeSetColName(v, i, COLNAME_DECLTYPE, zType, SQLITE_TRANSIENT);
109127  }
109128#endif /* !defined(SQLITE_OMIT_DECLTYPE) */
109129}
109130
109131/*
109132** Generate code that will tell the VDBE the names of columns
109133** in the result set.  This information is used to provide the
109134** azCol[] values in the callback.
109135*/
109136static void generateColumnNames(
109137  Parse *pParse,      /* Parser context */
109138  SrcList *pTabList,  /* List of tables */
109139  ExprList *pEList    /* Expressions defining the result set */
109140){
109141  Vdbe *v = pParse->pVdbe;
109142  int i, j;
109143  sqlite3 *db = pParse->db;
109144  int fullNames, shortNames;
109145
109146#ifndef SQLITE_OMIT_EXPLAIN
109147  /* If this is an EXPLAIN, skip this step */
109148  if( pParse->explain ){
109149    return;
109150  }
109151#endif
109152
109153  if( pParse->colNamesSet || NEVER(v==0) || db->mallocFailed ) return;
109154  pParse->colNamesSet = 1;
109155  fullNames = (db->flags & SQLITE_FullColNames)!=0;
109156  shortNames = (db->flags & SQLITE_ShortColNames)!=0;
109157  sqlite3VdbeSetNumCols(v, pEList->nExpr);
109158  for(i=0; i<pEList->nExpr; i++){
109159    Expr *p;
109160    p = pEList->a[i].pExpr;
109161    if( NEVER(p==0) ) continue;
109162    if( pEList->a[i].zName ){
109163      char *zName = pEList->a[i].zName;
109164      sqlite3VdbeSetColName(v, i, COLNAME_NAME, zName, SQLITE_TRANSIENT);
109165    }else if( (p->op==TK_COLUMN || p->op==TK_AGG_COLUMN) && pTabList ){
109166      Table *pTab;
109167      char *zCol;
109168      int iCol = p->iColumn;
109169      for(j=0; ALWAYS(j<pTabList->nSrc); j++){
109170        if( pTabList->a[j].iCursor==p->iTable ) break;
109171      }
109172      assert( j<pTabList->nSrc );
109173      pTab = pTabList->a[j].pTab;
109174      if( iCol<0 ) iCol = pTab->iPKey;
109175      assert( iCol==-1 || (iCol>=0 && iCol<pTab->nCol) );
109176      if( iCol<0 ){
109177        zCol = "rowid";
109178      }else{
109179        zCol = pTab->aCol[iCol].zName;
109180      }
109181      if( !shortNames && !fullNames ){
109182        sqlite3VdbeSetColName(v, i, COLNAME_NAME,
109183            sqlite3DbStrDup(db, pEList->a[i].zSpan), SQLITE_DYNAMIC);
109184      }else if( fullNames ){
109185        char *zName = 0;
109186        zName = sqlite3MPrintf(db, "%s.%s", pTab->zName, zCol);
109187        sqlite3VdbeSetColName(v, i, COLNAME_NAME, zName, SQLITE_DYNAMIC);
109188      }else{
109189        sqlite3VdbeSetColName(v, i, COLNAME_NAME, zCol, SQLITE_TRANSIENT);
109190      }
109191    }else{
109192      const char *z = pEList->a[i].zSpan;
109193      z = z==0 ? sqlite3MPrintf(db, "column%d", i+1) : sqlite3DbStrDup(db, z);
109194      sqlite3VdbeSetColName(v, i, COLNAME_NAME, z, SQLITE_DYNAMIC);
109195    }
109196  }
109197  generateColumnTypes(pParse, pTabList, pEList);
109198}
109199
109200/*
109201** Given an expression list (which is really the list of expressions
109202** that form the result set of a SELECT statement) compute appropriate
109203** column names for a table that would hold the expression list.
109204**
109205** All column names will be unique.
109206**
109207** Only the column names are computed.  Column.zType, Column.zColl,
109208** and other fields of Column are zeroed.
109209**
109210** Return SQLITE_OK on success.  If a memory allocation error occurs,
109211** store NULL in *paCol and 0 in *pnCol and return SQLITE_NOMEM.
109212*/
109213static int selectColumnsFromExprList(
109214  Parse *pParse,          /* Parsing context */
109215  ExprList *pEList,       /* Expr list from which to derive column names */
109216  i16 *pnCol,             /* Write the number of columns here */
109217  Column **paCol          /* Write the new column list here */
109218){
109219  sqlite3 *db = pParse->db;   /* Database connection */
109220  int i, j;                   /* Loop counters */
109221  int cnt;                    /* Index added to make the name unique */
109222  Column *aCol, *pCol;        /* For looping over result columns */
109223  int nCol;                   /* Number of columns in the result set */
109224  Expr *p;                    /* Expression for a single result column */
109225  char *zName;                /* Column name */
109226  int nName;                  /* Size of name in zName[] */
109227
109228  if( pEList ){
109229    nCol = pEList->nExpr;
109230    aCol = sqlite3DbMallocZero(db, sizeof(aCol[0])*nCol);
109231    testcase( aCol==0 );
109232  }else{
109233    nCol = 0;
109234    aCol = 0;
109235  }
109236  *pnCol = nCol;
109237  *paCol = aCol;
109238
109239  for(i=0, pCol=aCol; i<nCol; i++, pCol++){
109240    /* Get an appropriate name for the column
109241    */
109242    p = sqlite3ExprSkipCollate(pEList->a[i].pExpr);
109243    if( (zName = pEList->a[i].zName)!=0 ){
109244      /* If the column contains an "AS <name>" phrase, use <name> as the name */
109245      zName = sqlite3DbStrDup(db, zName);
109246    }else{
109247      Expr *pColExpr = p;  /* The expression that is the result column name */
109248      Table *pTab;         /* Table associated with this expression */
109249      while( pColExpr->op==TK_DOT ){
109250        pColExpr = pColExpr->pRight;
109251        assert( pColExpr!=0 );
109252      }
109253      if( pColExpr->op==TK_COLUMN && ALWAYS(pColExpr->pTab!=0) ){
109254        /* For columns use the column name name */
109255        int iCol = pColExpr->iColumn;
109256        pTab = pColExpr->pTab;
109257        if( iCol<0 ) iCol = pTab->iPKey;
109258        zName = sqlite3MPrintf(db, "%s",
109259                 iCol>=0 ? pTab->aCol[iCol].zName : "rowid");
109260      }else if( pColExpr->op==TK_ID ){
109261        assert( !ExprHasProperty(pColExpr, EP_IntValue) );
109262        zName = sqlite3MPrintf(db, "%s", pColExpr->u.zToken);
109263      }else{
109264        /* Use the original text of the column expression as its name */
109265        zName = sqlite3MPrintf(db, "%s", pEList->a[i].zSpan);
109266      }
109267    }
109268    if( db->mallocFailed ){
109269      sqlite3DbFree(db, zName);
109270      break;
109271    }
109272
109273    /* Make sure the column name is unique.  If the name is not unique,
109274    ** append an integer to the name so that it becomes unique.
109275    */
109276    nName = sqlite3Strlen30(zName);
109277    for(j=cnt=0; j<i; j++){
109278      if( sqlite3StrICmp(aCol[j].zName, zName)==0 ){
109279        char *zNewName;
109280        int k;
109281        for(k=nName-1; k>1 && sqlite3Isdigit(zName[k]); k--){}
109282        if( k>=0 && zName[k]==':' ) nName = k;
109283        zName[nName] = 0;
109284        zNewName = sqlite3MPrintf(db, "%s:%d", zName, ++cnt);
109285        sqlite3DbFree(db, zName);
109286        zName = zNewName;
109287        j = -1;
109288        if( zName==0 ) break;
109289      }
109290    }
109291    pCol->zName = zName;
109292  }
109293  if( db->mallocFailed ){
109294    for(j=0; j<i; j++){
109295      sqlite3DbFree(db, aCol[j].zName);
109296    }
109297    sqlite3DbFree(db, aCol);
109298    *paCol = 0;
109299    *pnCol = 0;
109300    return SQLITE_NOMEM;
109301  }
109302  return SQLITE_OK;
109303}
109304
109305/*
109306** Add type and collation information to a column list based on
109307** a SELECT statement.
109308**
109309** The column list presumably came from selectColumnNamesFromExprList().
109310** The column list has only names, not types or collations.  This
109311** routine goes through and adds the types and collations.
109312**
109313** This routine requires that all identifiers in the SELECT
109314** statement be resolved.
109315*/
109316static void selectAddColumnTypeAndCollation(
109317  Parse *pParse,        /* Parsing contexts */
109318  Table *pTab,          /* Add column type information to this table */
109319  Select *pSelect       /* SELECT used to determine types and collations */
109320){
109321  sqlite3 *db = pParse->db;
109322  NameContext sNC;
109323  Column *pCol;
109324  CollSeq *pColl;
109325  int i;
109326  Expr *p;
109327  struct ExprList_item *a;
109328  u64 szAll = 0;
109329
109330  assert( pSelect!=0 );
109331  assert( (pSelect->selFlags & SF_Resolved)!=0 );
109332  assert( pTab->nCol==pSelect->pEList->nExpr || db->mallocFailed );
109333  if( db->mallocFailed ) return;
109334  memset(&sNC, 0, sizeof(sNC));
109335  sNC.pSrcList = pSelect->pSrc;
109336  a = pSelect->pEList->a;
109337  for(i=0, pCol=pTab->aCol; i<pTab->nCol; i++, pCol++){
109338    p = a[i].pExpr;
109339    if( pCol->zType==0 ){
109340      pCol->zType = sqlite3DbStrDup(db,
109341                        columnType(&sNC, p,0,0,0, &pCol->szEst));
109342    }
109343    szAll += pCol->szEst;
109344    pCol->affinity = sqlite3ExprAffinity(p);
109345    if( pCol->affinity==0 ) pCol->affinity = SQLITE_AFF_BLOB;
109346    pColl = sqlite3ExprCollSeq(pParse, p);
109347    if( pColl && pCol->zColl==0 ){
109348      pCol->zColl = sqlite3DbStrDup(db, pColl->zName);
109349    }
109350  }
109351  pTab->szTabRow = sqlite3LogEst(szAll*4);
109352}
109353
109354/*
109355** Given a SELECT statement, generate a Table structure that describes
109356** the result set of that SELECT.
109357*/
109358SQLITE_PRIVATE Table *sqlite3ResultSetOfSelect(Parse *pParse, Select *pSelect){
109359  Table *pTab;
109360  sqlite3 *db = pParse->db;
109361  int savedFlags;
109362
109363  savedFlags = db->flags;
109364  db->flags &= ~SQLITE_FullColNames;
109365  db->flags |= SQLITE_ShortColNames;
109366  sqlite3SelectPrep(pParse, pSelect, 0);
109367  if( pParse->nErr ) return 0;
109368  while( pSelect->pPrior ) pSelect = pSelect->pPrior;
109369  db->flags = savedFlags;
109370  pTab = sqlite3DbMallocZero(db, sizeof(Table) );
109371  if( pTab==0 ){
109372    return 0;
109373  }
109374  /* The sqlite3ResultSetOfSelect() is only used n contexts where lookaside
109375  ** is disabled */
109376  assert( db->lookaside.bEnabled==0 );
109377  pTab->nRef = 1;
109378  pTab->zName = 0;
109379  pTab->nRowLogEst = 200; assert( 200==sqlite3LogEst(1048576) );
109380  selectColumnsFromExprList(pParse, pSelect->pEList, &pTab->nCol, &pTab->aCol);
109381  selectAddColumnTypeAndCollation(pParse, pTab, pSelect);
109382  pTab->iPKey = -1;
109383  if( db->mallocFailed ){
109384    sqlite3DeleteTable(db, pTab);
109385    return 0;
109386  }
109387  return pTab;
109388}
109389
109390/*
109391** Get a VDBE for the given parser context.  Create a new one if necessary.
109392** If an error occurs, return NULL and leave a message in pParse.
109393*/
109394SQLITE_PRIVATE Vdbe *sqlite3GetVdbe(Parse *pParse){
109395  Vdbe *v = pParse->pVdbe;
109396  if( v==0 ){
109397    v = pParse->pVdbe = sqlite3VdbeCreate(pParse);
109398    if( v ) sqlite3VdbeAddOp0(v, OP_Init);
109399    if( pParse->pToplevel==0
109400     && OptimizationEnabled(pParse->db,SQLITE_FactorOutConst)
109401    ){
109402      pParse->okConstFactor = 1;
109403    }
109404
109405  }
109406  return v;
109407}
109408
109409
109410/*
109411** Compute the iLimit and iOffset fields of the SELECT based on the
109412** pLimit and pOffset expressions.  pLimit and pOffset hold the expressions
109413** that appear in the original SQL statement after the LIMIT and OFFSET
109414** keywords.  Or NULL if those keywords are omitted. iLimit and iOffset
109415** are the integer memory register numbers for counters used to compute
109416** the limit and offset.  If there is no limit and/or offset, then
109417** iLimit and iOffset are negative.
109418**
109419** This routine changes the values of iLimit and iOffset only if
109420** a limit or offset is defined by pLimit and pOffset.  iLimit and
109421** iOffset should have been preset to appropriate default values (zero)
109422** prior to calling this routine.
109423**
109424** The iOffset register (if it exists) is initialized to the value
109425** of the OFFSET.  The iLimit register is initialized to LIMIT.  Register
109426** iOffset+1 is initialized to LIMIT+OFFSET.
109427**
109428** Only if pLimit!=0 or pOffset!=0 do the limit registers get
109429** redefined.  The UNION ALL operator uses this property to force
109430** the reuse of the same limit and offset registers across multiple
109431** SELECT statements.
109432*/
109433static void computeLimitRegisters(Parse *pParse, Select *p, int iBreak){
109434  Vdbe *v = 0;
109435  int iLimit = 0;
109436  int iOffset;
109437  int addr1, n;
109438  if( p->iLimit ) return;
109439
109440  /*
109441  ** "LIMIT -1" always shows all rows.  There is some
109442  ** controversy about what the correct behavior should be.
109443  ** The current implementation interprets "LIMIT 0" to mean
109444  ** no rows.
109445  */
109446  sqlite3ExprCacheClear(pParse);
109447  assert( p->pOffset==0 || p->pLimit!=0 );
109448  if( p->pLimit ){
109449    p->iLimit = iLimit = ++pParse->nMem;
109450    v = sqlite3GetVdbe(pParse);
109451    assert( v!=0 );
109452    if( sqlite3ExprIsInteger(p->pLimit, &n) ){
109453      sqlite3VdbeAddOp2(v, OP_Integer, n, iLimit);
109454      VdbeComment((v, "LIMIT counter"));
109455      if( n==0 ){
109456        sqlite3VdbeAddOp2(v, OP_Goto, 0, iBreak);
109457      }else if( n>=0 && p->nSelectRow>(u64)n ){
109458        p->nSelectRow = n;
109459      }
109460    }else{
109461      sqlite3ExprCode(pParse, p->pLimit, iLimit);
109462      sqlite3VdbeAddOp1(v, OP_MustBeInt, iLimit); VdbeCoverage(v);
109463      VdbeComment((v, "LIMIT counter"));
109464      sqlite3VdbeAddOp2(v, OP_IfNot, iLimit, iBreak); VdbeCoverage(v);
109465    }
109466    if( p->pOffset ){
109467      p->iOffset = iOffset = ++pParse->nMem;
109468      pParse->nMem++;   /* Allocate an extra register for limit+offset */
109469      sqlite3ExprCode(pParse, p->pOffset, iOffset);
109470      sqlite3VdbeAddOp1(v, OP_MustBeInt, iOffset); VdbeCoverage(v);
109471      VdbeComment((v, "OFFSET counter"));
109472      addr1 = sqlite3VdbeAddOp1(v, OP_IfPos, iOffset); VdbeCoverage(v);
109473      sqlite3VdbeAddOp2(v, OP_Integer, 0, iOffset);
109474      sqlite3VdbeJumpHere(v, addr1);
109475      sqlite3VdbeAddOp3(v, OP_Add, iLimit, iOffset, iOffset+1);
109476      VdbeComment((v, "LIMIT+OFFSET"));
109477      addr1 = sqlite3VdbeAddOp1(v, OP_IfPos, iLimit); VdbeCoverage(v);
109478      sqlite3VdbeAddOp2(v, OP_Integer, -1, iOffset+1);
109479      sqlite3VdbeJumpHere(v, addr1);
109480    }
109481  }
109482}
109483
109484#ifndef SQLITE_OMIT_COMPOUND_SELECT
109485/*
109486** Return the appropriate collating sequence for the iCol-th column of
109487** the result set for the compound-select statement "p".  Return NULL if
109488** the column has no default collating sequence.
109489**
109490** The collating sequence for the compound select is taken from the
109491** left-most term of the select that has a collating sequence.
109492*/
109493static CollSeq *multiSelectCollSeq(Parse *pParse, Select *p, int iCol){
109494  CollSeq *pRet;
109495  if( p->pPrior ){
109496    pRet = multiSelectCollSeq(pParse, p->pPrior, iCol);
109497  }else{
109498    pRet = 0;
109499  }
109500  assert( iCol>=0 );
109501  /* iCol must be less than p->pEList->nExpr.  Otherwise an error would
109502  ** have been thrown during name resolution and we would not have gotten
109503  ** this far */
109504  if( pRet==0 && ALWAYS(iCol<p->pEList->nExpr) ){
109505    pRet = sqlite3ExprCollSeq(pParse, p->pEList->a[iCol].pExpr);
109506  }
109507  return pRet;
109508}
109509
109510/*
109511** The select statement passed as the second parameter is a compound SELECT
109512** with an ORDER BY clause. This function allocates and returns a KeyInfo
109513** structure suitable for implementing the ORDER BY.
109514**
109515** Space to hold the KeyInfo structure is obtained from malloc. The calling
109516** function is responsible for ensuring that this structure is eventually
109517** freed.
109518*/
109519static KeyInfo *multiSelectOrderByKeyInfo(Parse *pParse, Select *p, int nExtra){
109520  ExprList *pOrderBy = p->pOrderBy;
109521  int nOrderBy = p->pOrderBy->nExpr;
109522  sqlite3 *db = pParse->db;
109523  KeyInfo *pRet = sqlite3KeyInfoAlloc(db, nOrderBy+nExtra, 1);
109524  if( pRet ){
109525    int i;
109526    for(i=0; i<nOrderBy; i++){
109527      struct ExprList_item *pItem = &pOrderBy->a[i];
109528      Expr *pTerm = pItem->pExpr;
109529      CollSeq *pColl;
109530
109531      if( pTerm->flags & EP_Collate ){
109532        pColl = sqlite3ExprCollSeq(pParse, pTerm);
109533      }else{
109534        pColl = multiSelectCollSeq(pParse, p, pItem->u.x.iOrderByCol-1);
109535        if( pColl==0 ) pColl = db->pDfltColl;
109536        pOrderBy->a[i].pExpr =
109537          sqlite3ExprAddCollateString(pParse, pTerm, pColl->zName);
109538      }
109539      assert( sqlite3KeyInfoIsWriteable(pRet) );
109540      pRet->aColl[i] = pColl;
109541      pRet->aSortOrder[i] = pOrderBy->a[i].sortOrder;
109542    }
109543  }
109544
109545  return pRet;
109546}
109547
109548#ifndef SQLITE_OMIT_CTE
109549/*
109550** This routine generates VDBE code to compute the content of a WITH RECURSIVE
109551** query of the form:
109552**
109553**   <recursive-table> AS (<setup-query> UNION [ALL] <recursive-query>)
109554**                         \___________/             \_______________/
109555**                           p->pPrior                      p
109556**
109557**
109558** There is exactly one reference to the recursive-table in the FROM clause
109559** of recursive-query, marked with the SrcList->a[].isRecursive flag.
109560**
109561** The setup-query runs once to generate an initial set of rows that go
109562** into a Queue table.  Rows are extracted from the Queue table one by
109563** one.  Each row extracted from Queue is output to pDest.  Then the single
109564** extracted row (now in the iCurrent table) becomes the content of the
109565** recursive-table for a recursive-query run.  The output of the recursive-query
109566** is added back into the Queue table.  Then another row is extracted from Queue
109567** and the iteration continues until the Queue table is empty.
109568**
109569** If the compound query operator is UNION then no duplicate rows are ever
109570** inserted into the Queue table.  The iDistinct table keeps a copy of all rows
109571** that have ever been inserted into Queue and causes duplicates to be
109572** discarded.  If the operator is UNION ALL, then duplicates are allowed.
109573**
109574** If the query has an ORDER BY, then entries in the Queue table are kept in
109575** ORDER BY order and the first entry is extracted for each cycle.  Without
109576** an ORDER BY, the Queue table is just a FIFO.
109577**
109578** If a LIMIT clause is provided, then the iteration stops after LIMIT rows
109579** have been output to pDest.  A LIMIT of zero means to output no rows and a
109580** negative LIMIT means to output all rows.  If there is also an OFFSET clause
109581** with a positive value, then the first OFFSET outputs are discarded rather
109582** than being sent to pDest.  The LIMIT count does not begin until after OFFSET
109583** rows have been skipped.
109584*/
109585static void generateWithRecursiveQuery(
109586  Parse *pParse,        /* Parsing context */
109587  Select *p,            /* The recursive SELECT to be coded */
109588  SelectDest *pDest     /* What to do with query results */
109589){
109590  SrcList *pSrc = p->pSrc;      /* The FROM clause of the recursive query */
109591  int nCol = p->pEList->nExpr;  /* Number of columns in the recursive table */
109592  Vdbe *v = pParse->pVdbe;      /* The prepared statement under construction */
109593  Select *pSetup = p->pPrior;   /* The setup query */
109594  int addrTop;                  /* Top of the loop */
109595  int addrCont, addrBreak;      /* CONTINUE and BREAK addresses */
109596  int iCurrent = 0;             /* The Current table */
109597  int regCurrent;               /* Register holding Current table */
109598  int iQueue;                   /* The Queue table */
109599  int iDistinct = 0;            /* To ensure unique results if UNION */
109600  int eDest = SRT_Fifo;         /* How to write to Queue */
109601  SelectDest destQueue;         /* SelectDest targetting the Queue table */
109602  int i;                        /* Loop counter */
109603  int rc;                       /* Result code */
109604  ExprList *pOrderBy;           /* The ORDER BY clause */
109605  Expr *pLimit, *pOffset;       /* Saved LIMIT and OFFSET */
109606  int regLimit, regOffset;      /* Registers used by LIMIT and OFFSET */
109607
109608  /* Obtain authorization to do a recursive query */
109609  if( sqlite3AuthCheck(pParse, SQLITE_RECURSIVE, 0, 0, 0) ) return;
109610
109611  /* Process the LIMIT and OFFSET clauses, if they exist */
109612  addrBreak = sqlite3VdbeMakeLabel(v);
109613  computeLimitRegisters(pParse, p, addrBreak);
109614  pLimit = p->pLimit;
109615  pOffset = p->pOffset;
109616  regLimit = p->iLimit;
109617  regOffset = p->iOffset;
109618  p->pLimit = p->pOffset = 0;
109619  p->iLimit = p->iOffset = 0;
109620  pOrderBy = p->pOrderBy;
109621
109622  /* Locate the cursor number of the Current table */
109623  for(i=0; ALWAYS(i<pSrc->nSrc); i++){
109624    if( pSrc->a[i].isRecursive ){
109625      iCurrent = pSrc->a[i].iCursor;
109626      break;
109627    }
109628  }
109629
109630  /* Allocate cursors numbers for Queue and Distinct.  The cursor number for
109631  ** the Distinct table must be exactly one greater than Queue in order
109632  ** for the SRT_DistFifo and SRT_DistQueue destinations to work. */
109633  iQueue = pParse->nTab++;
109634  if( p->op==TK_UNION ){
109635    eDest = pOrderBy ? SRT_DistQueue : SRT_DistFifo;
109636    iDistinct = pParse->nTab++;
109637  }else{
109638    eDest = pOrderBy ? SRT_Queue : SRT_Fifo;
109639  }
109640  sqlite3SelectDestInit(&destQueue, eDest, iQueue);
109641
109642  /* Allocate cursors for Current, Queue, and Distinct. */
109643  regCurrent = ++pParse->nMem;
109644  sqlite3VdbeAddOp3(v, OP_OpenPseudo, iCurrent, regCurrent, nCol);
109645  if( pOrderBy ){
109646    KeyInfo *pKeyInfo = multiSelectOrderByKeyInfo(pParse, p, 1);
109647    sqlite3VdbeAddOp4(v, OP_OpenEphemeral, iQueue, pOrderBy->nExpr+2, 0,
109648                      (char*)pKeyInfo, P4_KEYINFO);
109649    destQueue.pOrderBy = pOrderBy;
109650  }else{
109651    sqlite3VdbeAddOp2(v, OP_OpenEphemeral, iQueue, nCol);
109652  }
109653  VdbeComment((v, "Queue table"));
109654  if( iDistinct ){
109655    p->addrOpenEphm[0] = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, iDistinct, 0);
109656    p->selFlags |= SF_UsesEphemeral;
109657  }
109658
109659  /* Detach the ORDER BY clause from the compound SELECT */
109660  p->pOrderBy = 0;
109661
109662  /* Store the results of the setup-query in Queue. */
109663  pSetup->pNext = 0;
109664  rc = sqlite3Select(pParse, pSetup, &destQueue);
109665  pSetup->pNext = p;
109666  if( rc ) goto end_of_recursive_query;
109667
109668  /* Find the next row in the Queue and output that row */
109669  addrTop = sqlite3VdbeAddOp2(v, OP_Rewind, iQueue, addrBreak); VdbeCoverage(v);
109670
109671  /* Transfer the next row in Queue over to Current */
109672  sqlite3VdbeAddOp1(v, OP_NullRow, iCurrent); /* To reset column cache */
109673  if( pOrderBy ){
109674    sqlite3VdbeAddOp3(v, OP_Column, iQueue, pOrderBy->nExpr+1, regCurrent);
109675  }else{
109676    sqlite3VdbeAddOp2(v, OP_RowData, iQueue, regCurrent);
109677  }
109678  sqlite3VdbeAddOp1(v, OP_Delete, iQueue);
109679
109680  /* Output the single row in Current */
109681  addrCont = sqlite3VdbeMakeLabel(v);
109682  codeOffset(v, regOffset, addrCont);
109683  selectInnerLoop(pParse, p, p->pEList, iCurrent,
109684      0, 0, pDest, addrCont, addrBreak);
109685  if( regLimit ){
109686    sqlite3VdbeAddOp2(v, OP_DecrJumpZero, regLimit, addrBreak);
109687    VdbeCoverage(v);
109688  }
109689  sqlite3VdbeResolveLabel(v, addrCont);
109690
109691  /* Execute the recursive SELECT taking the single row in Current as
109692  ** the value for the recursive-table. Store the results in the Queue.
109693  */
109694  if( p->selFlags & SF_Aggregate ){
109695    sqlite3ErrorMsg(pParse, "recursive aggregate queries not supported");
109696  }else{
109697    p->pPrior = 0;
109698    sqlite3Select(pParse, p, &destQueue);
109699    assert( p->pPrior==0 );
109700    p->pPrior = pSetup;
109701  }
109702
109703  /* Keep running the loop until the Queue is empty */
109704  sqlite3VdbeAddOp2(v, OP_Goto, 0, addrTop);
109705  sqlite3VdbeResolveLabel(v, addrBreak);
109706
109707end_of_recursive_query:
109708  sqlite3ExprListDelete(pParse->db, p->pOrderBy);
109709  p->pOrderBy = pOrderBy;
109710  p->pLimit = pLimit;
109711  p->pOffset = pOffset;
109712  return;
109713}
109714#endif /* SQLITE_OMIT_CTE */
109715
109716/* Forward references */
109717static int multiSelectOrderBy(
109718  Parse *pParse,        /* Parsing context */
109719  Select *p,            /* The right-most of SELECTs to be coded */
109720  SelectDest *pDest     /* What to do with query results */
109721);
109722
109723/*
109724** Handle the special case of a compound-select that originates from a
109725** VALUES clause.  By handling this as a special case, we avoid deep
109726** recursion, and thus do not need to enforce the SQLITE_LIMIT_COMPOUND_SELECT
109727** on a VALUES clause.
109728**
109729** Because the Select object originates from a VALUES clause:
109730**   (1) It has no LIMIT or OFFSET
109731**   (2) All terms are UNION ALL
109732**   (3) There is no ORDER BY clause
109733*/
109734static int multiSelectValues(
109735  Parse *pParse,        /* Parsing context */
109736  Select *p,            /* The right-most of SELECTs to be coded */
109737  SelectDest *pDest     /* What to do with query results */
109738){
109739  Select *pPrior;
109740  int nRow = 1;
109741  int rc = 0;
109742  assert( p->selFlags & SF_MultiValue );
109743  do{
109744    assert( p->selFlags & SF_Values );
109745    assert( p->op==TK_ALL || (p->op==TK_SELECT && p->pPrior==0) );
109746    assert( p->pLimit==0 );
109747    assert( p->pOffset==0 );
109748    assert( p->pNext==0 || p->pEList->nExpr==p->pNext->pEList->nExpr );
109749    if( p->pPrior==0 ) break;
109750    assert( p->pPrior->pNext==p );
109751    p = p->pPrior;
109752    nRow++;
109753  }while(1);
109754  while( p ){
109755    pPrior = p->pPrior;
109756    p->pPrior = 0;
109757    rc = sqlite3Select(pParse, p, pDest);
109758    p->pPrior = pPrior;
109759    if( rc ) break;
109760    p->nSelectRow = nRow;
109761    p = p->pNext;
109762  }
109763  return rc;
109764}
109765
109766/*
109767** This routine is called to process a compound query form from
109768** two or more separate queries using UNION, UNION ALL, EXCEPT, or
109769** INTERSECT
109770**
109771** "p" points to the right-most of the two queries.  the query on the
109772** left is p->pPrior.  The left query could also be a compound query
109773** in which case this routine will be called recursively.
109774**
109775** The results of the total query are to be written into a destination
109776** of type eDest with parameter iParm.
109777**
109778** Example 1:  Consider a three-way compound SQL statement.
109779**
109780**     SELECT a FROM t1 UNION SELECT b FROM t2 UNION SELECT c FROM t3
109781**
109782** This statement is parsed up as follows:
109783**
109784**     SELECT c FROM t3
109785**      |
109786**      `----->  SELECT b FROM t2
109787**                |
109788**                `------>  SELECT a FROM t1
109789**
109790** The arrows in the diagram above represent the Select.pPrior pointer.
109791** So if this routine is called with p equal to the t3 query, then
109792** pPrior will be the t2 query.  p->op will be TK_UNION in this case.
109793**
109794** Notice that because of the way SQLite parses compound SELECTs, the
109795** individual selects always group from left to right.
109796*/
109797static int multiSelect(
109798  Parse *pParse,        /* Parsing context */
109799  Select *p,            /* The right-most of SELECTs to be coded */
109800  SelectDest *pDest     /* What to do with query results */
109801){
109802  int rc = SQLITE_OK;   /* Success code from a subroutine */
109803  Select *pPrior;       /* Another SELECT immediately to our left */
109804  Vdbe *v;              /* Generate code to this VDBE */
109805  SelectDest dest;      /* Alternative data destination */
109806  Select *pDelete = 0;  /* Chain of simple selects to delete */
109807  sqlite3 *db;          /* Database connection */
109808#ifndef SQLITE_OMIT_EXPLAIN
109809  int iSub1 = 0;        /* EQP id of left-hand query */
109810  int iSub2 = 0;        /* EQP id of right-hand query */
109811#endif
109812
109813  /* Make sure there is no ORDER BY or LIMIT clause on prior SELECTs.  Only
109814  ** the last (right-most) SELECT in the series may have an ORDER BY or LIMIT.
109815  */
109816  assert( p && p->pPrior );  /* Calling function guarantees this much */
109817  assert( (p->selFlags & SF_Recursive)==0 || p->op==TK_ALL || p->op==TK_UNION );
109818  db = pParse->db;
109819  pPrior = p->pPrior;
109820  dest = *pDest;
109821  if( pPrior->pOrderBy ){
109822    sqlite3ErrorMsg(pParse,"ORDER BY clause should come after %s not before",
109823      selectOpName(p->op));
109824    rc = 1;
109825    goto multi_select_end;
109826  }
109827  if( pPrior->pLimit ){
109828    sqlite3ErrorMsg(pParse,"LIMIT clause should come after %s not before",
109829      selectOpName(p->op));
109830    rc = 1;
109831    goto multi_select_end;
109832  }
109833
109834  v = sqlite3GetVdbe(pParse);
109835  assert( v!=0 );  /* The VDBE already created by calling function */
109836
109837  /* Create the destination temporary table if necessary
109838  */
109839  if( dest.eDest==SRT_EphemTab ){
109840    assert( p->pEList );
109841    sqlite3VdbeAddOp2(v, OP_OpenEphemeral, dest.iSDParm, p->pEList->nExpr);
109842    sqlite3VdbeChangeP5(v, BTREE_UNORDERED);
109843    dest.eDest = SRT_Table;
109844  }
109845
109846  /* Special handling for a compound-select that originates as a VALUES clause.
109847  */
109848  if( p->selFlags & SF_MultiValue ){
109849    rc = multiSelectValues(pParse, p, &dest);
109850    goto multi_select_end;
109851  }
109852
109853  /* Make sure all SELECTs in the statement have the same number of elements
109854  ** in their result sets.
109855  */
109856  assert( p->pEList && pPrior->pEList );
109857  assert( p->pEList->nExpr==pPrior->pEList->nExpr );
109858
109859#ifndef SQLITE_OMIT_CTE
109860  if( p->selFlags & SF_Recursive ){
109861    generateWithRecursiveQuery(pParse, p, &dest);
109862  }else
109863#endif
109864
109865  /* Compound SELECTs that have an ORDER BY clause are handled separately.
109866  */
109867  if( p->pOrderBy ){
109868    return multiSelectOrderBy(pParse, p, pDest);
109869  }else
109870
109871  /* Generate code for the left and right SELECT statements.
109872  */
109873  switch( p->op ){
109874    case TK_ALL: {
109875      int addr = 0;
109876      int nLimit;
109877      assert( !pPrior->pLimit );
109878      pPrior->iLimit = p->iLimit;
109879      pPrior->iOffset = p->iOffset;
109880      pPrior->pLimit = p->pLimit;
109881      pPrior->pOffset = p->pOffset;
109882      explainSetInteger(iSub1, pParse->iNextSelectId);
109883      rc = sqlite3Select(pParse, pPrior, &dest);
109884      p->pLimit = 0;
109885      p->pOffset = 0;
109886      if( rc ){
109887        goto multi_select_end;
109888      }
109889      p->pPrior = 0;
109890      p->iLimit = pPrior->iLimit;
109891      p->iOffset = pPrior->iOffset;
109892      if( p->iLimit ){
109893        addr = sqlite3VdbeAddOp1(v, OP_IfNot, p->iLimit); VdbeCoverage(v);
109894        VdbeComment((v, "Jump ahead if LIMIT reached"));
109895      }
109896      explainSetInteger(iSub2, pParse->iNextSelectId);
109897      rc = sqlite3Select(pParse, p, &dest);
109898      testcase( rc!=SQLITE_OK );
109899      pDelete = p->pPrior;
109900      p->pPrior = pPrior;
109901      p->nSelectRow += pPrior->nSelectRow;
109902      if( pPrior->pLimit
109903       && sqlite3ExprIsInteger(pPrior->pLimit, &nLimit)
109904       && nLimit>0 && p->nSelectRow > (u64)nLimit
109905      ){
109906        p->nSelectRow = nLimit;
109907      }
109908      if( addr ){
109909        sqlite3VdbeJumpHere(v, addr);
109910      }
109911      break;
109912    }
109913    case TK_EXCEPT:
109914    case TK_UNION: {
109915      int unionTab;    /* Cursor number of the temporary table holding result */
109916      u8 op = 0;       /* One of the SRT_ operations to apply to self */
109917      int priorOp;     /* The SRT_ operation to apply to prior selects */
109918      Expr *pLimit, *pOffset; /* Saved values of p->nLimit and p->nOffset */
109919      int addr;
109920      SelectDest uniondest;
109921
109922      testcase( p->op==TK_EXCEPT );
109923      testcase( p->op==TK_UNION );
109924      priorOp = SRT_Union;
109925      if( dest.eDest==priorOp ){
109926        /* We can reuse a temporary table generated by a SELECT to our
109927        ** right.
109928        */
109929        assert( p->pLimit==0 );      /* Not allowed on leftward elements */
109930        assert( p->pOffset==0 );     /* Not allowed on leftward elements */
109931        unionTab = dest.iSDParm;
109932      }else{
109933        /* We will need to create our own temporary table to hold the
109934        ** intermediate results.
109935        */
109936        unionTab = pParse->nTab++;
109937        assert( p->pOrderBy==0 );
109938        addr = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, unionTab, 0);
109939        assert( p->addrOpenEphm[0] == -1 );
109940        p->addrOpenEphm[0] = addr;
109941        findRightmost(p)->selFlags |= SF_UsesEphemeral;
109942        assert( p->pEList );
109943      }
109944
109945      /* Code the SELECT statements to our left
109946      */
109947      assert( !pPrior->pOrderBy );
109948      sqlite3SelectDestInit(&uniondest, priorOp, unionTab);
109949      explainSetInteger(iSub1, pParse->iNextSelectId);
109950      rc = sqlite3Select(pParse, pPrior, &uniondest);
109951      if( rc ){
109952        goto multi_select_end;
109953      }
109954
109955      /* Code the current SELECT statement
109956      */
109957      if( p->op==TK_EXCEPT ){
109958        op = SRT_Except;
109959      }else{
109960        assert( p->op==TK_UNION );
109961        op = SRT_Union;
109962      }
109963      p->pPrior = 0;
109964      pLimit = p->pLimit;
109965      p->pLimit = 0;
109966      pOffset = p->pOffset;
109967      p->pOffset = 0;
109968      uniondest.eDest = op;
109969      explainSetInteger(iSub2, pParse->iNextSelectId);
109970      rc = sqlite3Select(pParse, p, &uniondest);
109971      testcase( rc!=SQLITE_OK );
109972      /* Query flattening in sqlite3Select() might refill p->pOrderBy.
109973      ** Be sure to delete p->pOrderBy, therefore, to avoid a memory leak. */
109974      sqlite3ExprListDelete(db, p->pOrderBy);
109975      pDelete = p->pPrior;
109976      p->pPrior = pPrior;
109977      p->pOrderBy = 0;
109978      if( p->op==TK_UNION ) p->nSelectRow += pPrior->nSelectRow;
109979      sqlite3ExprDelete(db, p->pLimit);
109980      p->pLimit = pLimit;
109981      p->pOffset = pOffset;
109982      p->iLimit = 0;
109983      p->iOffset = 0;
109984
109985      /* Convert the data in the temporary table into whatever form
109986      ** it is that we currently need.
109987      */
109988      assert( unionTab==dest.iSDParm || dest.eDest!=priorOp );
109989      if( dest.eDest!=priorOp ){
109990        int iCont, iBreak, iStart;
109991        assert( p->pEList );
109992        if( dest.eDest==SRT_Output ){
109993          Select *pFirst = p;
109994          while( pFirst->pPrior ) pFirst = pFirst->pPrior;
109995          generateColumnNames(pParse, 0, pFirst->pEList);
109996        }
109997        iBreak = sqlite3VdbeMakeLabel(v);
109998        iCont = sqlite3VdbeMakeLabel(v);
109999        computeLimitRegisters(pParse, p, iBreak);
110000        sqlite3VdbeAddOp2(v, OP_Rewind, unionTab, iBreak); VdbeCoverage(v);
110001        iStart = sqlite3VdbeCurrentAddr(v);
110002        selectInnerLoop(pParse, p, p->pEList, unionTab,
110003                        0, 0, &dest, iCont, iBreak);
110004        sqlite3VdbeResolveLabel(v, iCont);
110005        sqlite3VdbeAddOp2(v, OP_Next, unionTab, iStart); VdbeCoverage(v);
110006        sqlite3VdbeResolveLabel(v, iBreak);
110007        sqlite3VdbeAddOp2(v, OP_Close, unionTab, 0);
110008      }
110009      break;
110010    }
110011    default: assert( p->op==TK_INTERSECT ); {
110012      int tab1, tab2;
110013      int iCont, iBreak, iStart;
110014      Expr *pLimit, *pOffset;
110015      int addr;
110016      SelectDest intersectdest;
110017      int r1;
110018
110019      /* INTERSECT is different from the others since it requires
110020      ** two temporary tables.  Hence it has its own case.  Begin
110021      ** by allocating the tables we will need.
110022      */
110023      tab1 = pParse->nTab++;
110024      tab2 = pParse->nTab++;
110025      assert( p->pOrderBy==0 );
110026
110027      addr = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, tab1, 0);
110028      assert( p->addrOpenEphm[0] == -1 );
110029      p->addrOpenEphm[0] = addr;
110030      findRightmost(p)->selFlags |= SF_UsesEphemeral;
110031      assert( p->pEList );
110032
110033      /* Code the SELECTs to our left into temporary table "tab1".
110034      */
110035      sqlite3SelectDestInit(&intersectdest, SRT_Union, tab1);
110036      explainSetInteger(iSub1, pParse->iNextSelectId);
110037      rc = sqlite3Select(pParse, pPrior, &intersectdest);
110038      if( rc ){
110039        goto multi_select_end;
110040      }
110041
110042      /* Code the current SELECT into temporary table "tab2"
110043      */
110044      addr = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, tab2, 0);
110045      assert( p->addrOpenEphm[1] == -1 );
110046      p->addrOpenEphm[1] = addr;
110047      p->pPrior = 0;
110048      pLimit = p->pLimit;
110049      p->pLimit = 0;
110050      pOffset = p->pOffset;
110051      p->pOffset = 0;
110052      intersectdest.iSDParm = tab2;
110053      explainSetInteger(iSub2, pParse->iNextSelectId);
110054      rc = sqlite3Select(pParse, p, &intersectdest);
110055      testcase( rc!=SQLITE_OK );
110056      pDelete = p->pPrior;
110057      p->pPrior = pPrior;
110058      if( p->nSelectRow>pPrior->nSelectRow ) p->nSelectRow = pPrior->nSelectRow;
110059      sqlite3ExprDelete(db, p->pLimit);
110060      p->pLimit = pLimit;
110061      p->pOffset = pOffset;
110062
110063      /* Generate code to take the intersection of the two temporary
110064      ** tables.
110065      */
110066      assert( p->pEList );
110067      if( dest.eDest==SRT_Output ){
110068        Select *pFirst = p;
110069        while( pFirst->pPrior ) pFirst = pFirst->pPrior;
110070        generateColumnNames(pParse, 0, pFirst->pEList);
110071      }
110072      iBreak = sqlite3VdbeMakeLabel(v);
110073      iCont = sqlite3VdbeMakeLabel(v);
110074      computeLimitRegisters(pParse, p, iBreak);
110075      sqlite3VdbeAddOp2(v, OP_Rewind, tab1, iBreak); VdbeCoverage(v);
110076      r1 = sqlite3GetTempReg(pParse);
110077      iStart = sqlite3VdbeAddOp2(v, OP_RowKey, tab1, r1);
110078      sqlite3VdbeAddOp4Int(v, OP_NotFound, tab2, iCont, r1, 0); VdbeCoverage(v);
110079      sqlite3ReleaseTempReg(pParse, r1);
110080      selectInnerLoop(pParse, p, p->pEList, tab1,
110081                      0, 0, &dest, iCont, iBreak);
110082      sqlite3VdbeResolveLabel(v, iCont);
110083      sqlite3VdbeAddOp2(v, OP_Next, tab1, iStart); VdbeCoverage(v);
110084      sqlite3VdbeResolveLabel(v, iBreak);
110085      sqlite3VdbeAddOp2(v, OP_Close, tab2, 0);
110086      sqlite3VdbeAddOp2(v, OP_Close, tab1, 0);
110087      break;
110088    }
110089  }
110090
110091  explainComposite(pParse, p->op, iSub1, iSub2, p->op!=TK_ALL);
110092
110093  /* Compute collating sequences used by
110094  ** temporary tables needed to implement the compound select.
110095  ** Attach the KeyInfo structure to all temporary tables.
110096  **
110097  ** This section is run by the right-most SELECT statement only.
110098  ** SELECT statements to the left always skip this part.  The right-most
110099  ** SELECT might also skip this part if it has no ORDER BY clause and
110100  ** no temp tables are required.
110101  */
110102  if( p->selFlags & SF_UsesEphemeral ){
110103    int i;                        /* Loop counter */
110104    KeyInfo *pKeyInfo;            /* Collating sequence for the result set */
110105    Select *pLoop;                /* For looping through SELECT statements */
110106    CollSeq **apColl;             /* For looping through pKeyInfo->aColl[] */
110107    int nCol;                     /* Number of columns in result set */
110108
110109    assert( p->pNext==0 );
110110    nCol = p->pEList->nExpr;
110111    pKeyInfo = sqlite3KeyInfoAlloc(db, nCol, 1);
110112    if( !pKeyInfo ){
110113      rc = SQLITE_NOMEM;
110114      goto multi_select_end;
110115    }
110116    for(i=0, apColl=pKeyInfo->aColl; i<nCol; i++, apColl++){
110117      *apColl = multiSelectCollSeq(pParse, p, i);
110118      if( 0==*apColl ){
110119        *apColl = db->pDfltColl;
110120      }
110121    }
110122
110123    for(pLoop=p; pLoop; pLoop=pLoop->pPrior){
110124      for(i=0; i<2; i++){
110125        int addr = pLoop->addrOpenEphm[i];
110126        if( addr<0 ){
110127          /* If [0] is unused then [1] is also unused.  So we can
110128          ** always safely abort as soon as the first unused slot is found */
110129          assert( pLoop->addrOpenEphm[1]<0 );
110130          break;
110131        }
110132        sqlite3VdbeChangeP2(v, addr, nCol);
110133        sqlite3VdbeChangeP4(v, addr, (char*)sqlite3KeyInfoRef(pKeyInfo),
110134                            P4_KEYINFO);
110135        pLoop->addrOpenEphm[i] = -1;
110136      }
110137    }
110138    sqlite3KeyInfoUnref(pKeyInfo);
110139  }
110140
110141multi_select_end:
110142  pDest->iSdst = dest.iSdst;
110143  pDest->nSdst = dest.nSdst;
110144  sqlite3SelectDelete(db, pDelete);
110145  return rc;
110146}
110147#endif /* SQLITE_OMIT_COMPOUND_SELECT */
110148
110149/*
110150** Error message for when two or more terms of a compound select have different
110151** size result sets.
110152*/
110153SQLITE_PRIVATE void sqlite3SelectWrongNumTermsError(Parse *pParse, Select *p){
110154  if( p->selFlags & SF_Values ){
110155    sqlite3ErrorMsg(pParse, "all VALUES must have the same number of terms");
110156  }else{
110157    sqlite3ErrorMsg(pParse, "SELECTs to the left and right of %s"
110158      " do not have the same number of result columns", selectOpName(p->op));
110159  }
110160}
110161
110162/*
110163** Code an output subroutine for a coroutine implementation of a
110164** SELECT statment.
110165**
110166** The data to be output is contained in pIn->iSdst.  There are
110167** pIn->nSdst columns to be output.  pDest is where the output should
110168** be sent.
110169**
110170** regReturn is the number of the register holding the subroutine
110171** return address.
110172**
110173** If regPrev>0 then it is the first register in a vector that
110174** records the previous output.  mem[regPrev] is a flag that is false
110175** if there has been no previous output.  If regPrev>0 then code is
110176** generated to suppress duplicates.  pKeyInfo is used for comparing
110177** keys.
110178**
110179** If the LIMIT found in p->iLimit is reached, jump immediately to
110180** iBreak.
110181*/
110182static int generateOutputSubroutine(
110183  Parse *pParse,          /* Parsing context */
110184  Select *p,              /* The SELECT statement */
110185  SelectDest *pIn,        /* Coroutine supplying data */
110186  SelectDest *pDest,      /* Where to send the data */
110187  int regReturn,          /* The return address register */
110188  int regPrev,            /* Previous result register.  No uniqueness if 0 */
110189  KeyInfo *pKeyInfo,      /* For comparing with previous entry */
110190  int iBreak              /* Jump here if we hit the LIMIT */
110191){
110192  Vdbe *v = pParse->pVdbe;
110193  int iContinue;
110194  int addr;
110195
110196  addr = sqlite3VdbeCurrentAddr(v);
110197  iContinue = sqlite3VdbeMakeLabel(v);
110198
110199  /* Suppress duplicates for UNION, EXCEPT, and INTERSECT
110200  */
110201  if( regPrev ){
110202    int j1, j2;
110203    j1 = sqlite3VdbeAddOp1(v, OP_IfNot, regPrev); VdbeCoverage(v);
110204    j2 = sqlite3VdbeAddOp4(v, OP_Compare, pIn->iSdst, regPrev+1, pIn->nSdst,
110205                              (char*)sqlite3KeyInfoRef(pKeyInfo), P4_KEYINFO);
110206    sqlite3VdbeAddOp3(v, OP_Jump, j2+2, iContinue, j2+2); VdbeCoverage(v);
110207    sqlite3VdbeJumpHere(v, j1);
110208    sqlite3VdbeAddOp3(v, OP_Copy, pIn->iSdst, regPrev+1, pIn->nSdst-1);
110209    sqlite3VdbeAddOp2(v, OP_Integer, 1, regPrev);
110210  }
110211  if( pParse->db->mallocFailed ) return 0;
110212
110213  /* Suppress the first OFFSET entries if there is an OFFSET clause
110214  */
110215  codeOffset(v, p->iOffset, iContinue);
110216
110217  assert( pDest->eDest!=SRT_Exists );
110218  assert( pDest->eDest!=SRT_Table );
110219  switch( pDest->eDest ){
110220    /* Store the result as data using a unique key.
110221    */
110222    case SRT_EphemTab: {
110223      int r1 = sqlite3GetTempReg(pParse);
110224      int r2 = sqlite3GetTempReg(pParse);
110225      sqlite3VdbeAddOp3(v, OP_MakeRecord, pIn->iSdst, pIn->nSdst, r1);
110226      sqlite3VdbeAddOp2(v, OP_NewRowid, pDest->iSDParm, r2);
110227      sqlite3VdbeAddOp3(v, OP_Insert, pDest->iSDParm, r1, r2);
110228      sqlite3VdbeChangeP5(v, OPFLAG_APPEND);
110229      sqlite3ReleaseTempReg(pParse, r2);
110230      sqlite3ReleaseTempReg(pParse, r1);
110231      break;
110232    }
110233
110234#ifndef SQLITE_OMIT_SUBQUERY
110235    /* If we are creating a set for an "expr IN (SELECT ...)" construct,
110236    ** then there should be a single item on the stack.  Write this
110237    ** item into the set table with bogus data.
110238    */
110239    case SRT_Set: {
110240      int r1;
110241      assert( pIn->nSdst==1 || pParse->nErr>0 );
110242      pDest->affSdst =
110243         sqlite3CompareAffinity(p->pEList->a[0].pExpr, pDest->affSdst);
110244      r1 = sqlite3GetTempReg(pParse);
110245      sqlite3VdbeAddOp4(v, OP_MakeRecord, pIn->iSdst, 1, r1, &pDest->affSdst,1);
110246      sqlite3ExprCacheAffinityChange(pParse, pIn->iSdst, 1);
110247      sqlite3VdbeAddOp2(v, OP_IdxInsert, pDest->iSDParm, r1);
110248      sqlite3ReleaseTempReg(pParse, r1);
110249      break;
110250    }
110251
110252    /* If this is a scalar select that is part of an expression, then
110253    ** store the results in the appropriate memory cell and break out
110254    ** of the scan loop.
110255    */
110256    case SRT_Mem: {
110257      assert( pIn->nSdst==1 || pParse->nErr>0 );  testcase( pIn->nSdst!=1 );
110258      sqlite3ExprCodeMove(pParse, pIn->iSdst, pDest->iSDParm, 1);
110259      /* The LIMIT clause will jump out of the loop for us */
110260      break;
110261    }
110262#endif /* #ifndef SQLITE_OMIT_SUBQUERY */
110263
110264    /* The results are stored in a sequence of registers
110265    ** starting at pDest->iSdst.  Then the co-routine yields.
110266    */
110267    case SRT_Coroutine: {
110268      if( pDest->iSdst==0 ){
110269        pDest->iSdst = sqlite3GetTempRange(pParse, pIn->nSdst);
110270        pDest->nSdst = pIn->nSdst;
110271      }
110272      sqlite3ExprCodeMove(pParse, pIn->iSdst, pDest->iSdst, pIn->nSdst);
110273      sqlite3VdbeAddOp1(v, OP_Yield, pDest->iSDParm);
110274      break;
110275    }
110276
110277    /* If none of the above, then the result destination must be
110278    ** SRT_Output.  This routine is never called with any other
110279    ** destination other than the ones handled above or SRT_Output.
110280    **
110281    ** For SRT_Output, results are stored in a sequence of registers.
110282    ** Then the OP_ResultRow opcode is used to cause sqlite3_step() to
110283    ** return the next row of result.
110284    */
110285    default: {
110286      assert( pDest->eDest==SRT_Output );
110287      sqlite3VdbeAddOp2(v, OP_ResultRow, pIn->iSdst, pIn->nSdst);
110288      sqlite3ExprCacheAffinityChange(pParse, pIn->iSdst, pIn->nSdst);
110289      break;
110290    }
110291  }
110292
110293  /* Jump to the end of the loop if the LIMIT is reached.
110294  */
110295  if( p->iLimit ){
110296    sqlite3VdbeAddOp2(v, OP_DecrJumpZero, p->iLimit, iBreak); VdbeCoverage(v);
110297  }
110298
110299  /* Generate the subroutine return
110300  */
110301  sqlite3VdbeResolveLabel(v, iContinue);
110302  sqlite3VdbeAddOp1(v, OP_Return, regReturn);
110303
110304  return addr;
110305}
110306
110307/*
110308** Alternative compound select code generator for cases when there
110309** is an ORDER BY clause.
110310**
110311** We assume a query of the following form:
110312**
110313**      <selectA>  <operator>  <selectB>  ORDER BY <orderbylist>
110314**
110315** <operator> is one of UNION ALL, UNION, EXCEPT, or INTERSECT.  The idea
110316** is to code both <selectA> and <selectB> with the ORDER BY clause as
110317** co-routines.  Then run the co-routines in parallel and merge the results
110318** into the output.  In addition to the two coroutines (called selectA and
110319** selectB) there are 7 subroutines:
110320**
110321**    outA:    Move the output of the selectA coroutine into the output
110322**             of the compound query.
110323**
110324**    outB:    Move the output of the selectB coroutine into the output
110325**             of the compound query.  (Only generated for UNION and
110326**             UNION ALL.  EXCEPT and INSERTSECT never output a row that
110327**             appears only in B.)
110328**
110329**    AltB:    Called when there is data from both coroutines and A<B.
110330**
110331**    AeqB:    Called when there is data from both coroutines and A==B.
110332**
110333**    AgtB:    Called when there is data from both coroutines and A>B.
110334**
110335**    EofA:    Called when data is exhausted from selectA.
110336**
110337**    EofB:    Called when data is exhausted from selectB.
110338**
110339** The implementation of the latter five subroutines depend on which
110340** <operator> is used:
110341**
110342**
110343**             UNION ALL         UNION            EXCEPT          INTERSECT
110344**          -------------  -----------------  --------------  -----------------
110345**   AltB:   outA, nextA      outA, nextA       outA, nextA         nextA
110346**
110347**   AeqB:   outA, nextA         nextA             nextA         outA, nextA
110348**
110349**   AgtB:   outB, nextB      outB, nextB          nextB            nextB
110350**
110351**   EofA:   outB, nextB      outB, nextB          halt             halt
110352**
110353**   EofB:   outA, nextA      outA, nextA       outA, nextA         halt
110354**
110355** In the AltB, AeqB, and AgtB subroutines, an EOF on A following nextA
110356** causes an immediate jump to EofA and an EOF on B following nextB causes
110357** an immediate jump to EofB.  Within EofA and EofB, and EOF on entry or
110358** following nextX causes a jump to the end of the select processing.
110359**
110360** Duplicate removal in the UNION, EXCEPT, and INTERSECT cases is handled
110361** within the output subroutine.  The regPrev register set holds the previously
110362** output value.  A comparison is made against this value and the output
110363** is skipped if the next results would be the same as the previous.
110364**
110365** The implementation plan is to implement the two coroutines and seven
110366** subroutines first, then put the control logic at the bottom.  Like this:
110367**
110368**          goto Init
110369**     coA: coroutine for left query (A)
110370**     coB: coroutine for right query (B)
110371**    outA: output one row of A
110372**    outB: output one row of B (UNION and UNION ALL only)
110373**    EofA: ...
110374**    EofB: ...
110375**    AltB: ...
110376**    AeqB: ...
110377**    AgtB: ...
110378**    Init: initialize coroutine registers
110379**          yield coA
110380**          if eof(A) goto EofA
110381**          yield coB
110382**          if eof(B) goto EofB
110383**    Cmpr: Compare A, B
110384**          Jump AltB, AeqB, AgtB
110385**     End: ...
110386**
110387** We call AltB, AeqB, AgtB, EofA, and EofB "subroutines" but they are not
110388** actually called using Gosub and they do not Return.  EofA and EofB loop
110389** until all data is exhausted then jump to the "end" labe.  AltB, AeqB,
110390** and AgtB jump to either L2 or to one of EofA or EofB.
110391*/
110392#ifndef SQLITE_OMIT_COMPOUND_SELECT
110393static int multiSelectOrderBy(
110394  Parse *pParse,        /* Parsing context */
110395  Select *p,            /* The right-most of SELECTs to be coded */
110396  SelectDest *pDest     /* What to do with query results */
110397){
110398  int i, j;             /* Loop counters */
110399  Select *pPrior;       /* Another SELECT immediately to our left */
110400  Vdbe *v;              /* Generate code to this VDBE */
110401  SelectDest destA;     /* Destination for coroutine A */
110402  SelectDest destB;     /* Destination for coroutine B */
110403  int regAddrA;         /* Address register for select-A coroutine */
110404  int regAddrB;         /* Address register for select-B coroutine */
110405  int addrSelectA;      /* Address of the select-A coroutine */
110406  int addrSelectB;      /* Address of the select-B coroutine */
110407  int regOutA;          /* Address register for the output-A subroutine */
110408  int regOutB;          /* Address register for the output-B subroutine */
110409  int addrOutA;         /* Address of the output-A subroutine */
110410  int addrOutB = 0;     /* Address of the output-B subroutine */
110411  int addrEofA;         /* Address of the select-A-exhausted subroutine */
110412  int addrEofA_noB;     /* Alternate addrEofA if B is uninitialized */
110413  int addrEofB;         /* Address of the select-B-exhausted subroutine */
110414  int addrAltB;         /* Address of the A<B subroutine */
110415  int addrAeqB;         /* Address of the A==B subroutine */
110416  int addrAgtB;         /* Address of the A>B subroutine */
110417  int regLimitA;        /* Limit register for select-A */
110418  int regLimitB;        /* Limit register for select-A */
110419  int regPrev;          /* A range of registers to hold previous output */
110420  int savedLimit;       /* Saved value of p->iLimit */
110421  int savedOffset;      /* Saved value of p->iOffset */
110422  int labelCmpr;        /* Label for the start of the merge algorithm */
110423  int labelEnd;         /* Label for the end of the overall SELECT stmt */
110424  int j1;               /* Jump instructions that get retargetted */
110425  int op;               /* One of TK_ALL, TK_UNION, TK_EXCEPT, TK_INTERSECT */
110426  KeyInfo *pKeyDup = 0; /* Comparison information for duplicate removal */
110427  KeyInfo *pKeyMerge;   /* Comparison information for merging rows */
110428  sqlite3 *db;          /* Database connection */
110429  ExprList *pOrderBy;   /* The ORDER BY clause */
110430  int nOrderBy;         /* Number of terms in the ORDER BY clause */
110431  int *aPermute;        /* Mapping from ORDER BY terms to result set columns */
110432#ifndef SQLITE_OMIT_EXPLAIN
110433  int iSub1;            /* EQP id of left-hand query */
110434  int iSub2;            /* EQP id of right-hand query */
110435#endif
110436
110437  assert( p->pOrderBy!=0 );
110438  assert( pKeyDup==0 ); /* "Managed" code needs this.  Ticket #3382. */
110439  db = pParse->db;
110440  v = pParse->pVdbe;
110441  assert( v!=0 );       /* Already thrown the error if VDBE alloc failed */
110442  labelEnd = sqlite3VdbeMakeLabel(v);
110443  labelCmpr = sqlite3VdbeMakeLabel(v);
110444
110445
110446  /* Patch up the ORDER BY clause
110447  */
110448  op = p->op;
110449  pPrior = p->pPrior;
110450  assert( pPrior->pOrderBy==0 );
110451  pOrderBy = p->pOrderBy;
110452  assert( pOrderBy );
110453  nOrderBy = pOrderBy->nExpr;
110454
110455  /* For operators other than UNION ALL we have to make sure that
110456  ** the ORDER BY clause covers every term of the result set.  Add
110457  ** terms to the ORDER BY clause as necessary.
110458  */
110459  if( op!=TK_ALL ){
110460    for(i=1; db->mallocFailed==0 && i<=p->pEList->nExpr; i++){
110461      struct ExprList_item *pItem;
110462      for(j=0, pItem=pOrderBy->a; j<nOrderBy; j++, pItem++){
110463        assert( pItem->u.x.iOrderByCol>0 );
110464        if( pItem->u.x.iOrderByCol==i ) break;
110465      }
110466      if( j==nOrderBy ){
110467        Expr *pNew = sqlite3Expr(db, TK_INTEGER, 0);
110468        if( pNew==0 ) return SQLITE_NOMEM;
110469        pNew->flags |= EP_IntValue;
110470        pNew->u.iValue = i;
110471        pOrderBy = sqlite3ExprListAppend(pParse, pOrderBy, pNew);
110472        if( pOrderBy ) pOrderBy->a[nOrderBy++].u.x.iOrderByCol = (u16)i;
110473      }
110474    }
110475  }
110476
110477  /* Compute the comparison permutation and keyinfo that is used with
110478  ** the permutation used to determine if the next
110479  ** row of results comes from selectA or selectB.  Also add explicit
110480  ** collations to the ORDER BY clause terms so that when the subqueries
110481  ** to the right and the left are evaluated, they use the correct
110482  ** collation.
110483  */
110484  aPermute = sqlite3DbMallocRaw(db, sizeof(int)*nOrderBy);
110485  if( aPermute ){
110486    struct ExprList_item *pItem;
110487    for(i=0, pItem=pOrderBy->a; i<nOrderBy; i++, pItem++){
110488      assert( pItem->u.x.iOrderByCol>0 );
110489      assert( pItem->u.x.iOrderByCol<=p->pEList->nExpr );
110490      aPermute[i] = pItem->u.x.iOrderByCol - 1;
110491    }
110492    pKeyMerge = multiSelectOrderByKeyInfo(pParse, p, 1);
110493  }else{
110494    pKeyMerge = 0;
110495  }
110496
110497  /* Reattach the ORDER BY clause to the query.
110498  */
110499  p->pOrderBy = pOrderBy;
110500  pPrior->pOrderBy = sqlite3ExprListDup(pParse->db, pOrderBy, 0);
110501
110502  /* Allocate a range of temporary registers and the KeyInfo needed
110503  ** for the logic that removes duplicate result rows when the
110504  ** operator is UNION, EXCEPT, or INTERSECT (but not UNION ALL).
110505  */
110506  if( op==TK_ALL ){
110507    regPrev = 0;
110508  }else{
110509    int nExpr = p->pEList->nExpr;
110510    assert( nOrderBy>=nExpr || db->mallocFailed );
110511    regPrev = pParse->nMem+1;
110512    pParse->nMem += nExpr+1;
110513    sqlite3VdbeAddOp2(v, OP_Integer, 0, regPrev);
110514    pKeyDup = sqlite3KeyInfoAlloc(db, nExpr, 1);
110515    if( pKeyDup ){
110516      assert( sqlite3KeyInfoIsWriteable(pKeyDup) );
110517      for(i=0; i<nExpr; i++){
110518        pKeyDup->aColl[i] = multiSelectCollSeq(pParse, p, i);
110519        pKeyDup->aSortOrder[i] = 0;
110520      }
110521    }
110522  }
110523
110524  /* Separate the left and the right query from one another
110525  */
110526  p->pPrior = 0;
110527  pPrior->pNext = 0;
110528  sqlite3ResolveOrderGroupBy(pParse, p, p->pOrderBy, "ORDER");
110529  if( pPrior->pPrior==0 ){
110530    sqlite3ResolveOrderGroupBy(pParse, pPrior, pPrior->pOrderBy, "ORDER");
110531  }
110532
110533  /* Compute the limit registers */
110534  computeLimitRegisters(pParse, p, labelEnd);
110535  if( p->iLimit && op==TK_ALL ){
110536    regLimitA = ++pParse->nMem;
110537    regLimitB = ++pParse->nMem;
110538    sqlite3VdbeAddOp2(v, OP_Copy, p->iOffset ? p->iOffset+1 : p->iLimit,
110539                                  regLimitA);
110540    sqlite3VdbeAddOp2(v, OP_Copy, regLimitA, regLimitB);
110541  }else{
110542    regLimitA = regLimitB = 0;
110543  }
110544  sqlite3ExprDelete(db, p->pLimit);
110545  p->pLimit = 0;
110546  sqlite3ExprDelete(db, p->pOffset);
110547  p->pOffset = 0;
110548
110549  regAddrA = ++pParse->nMem;
110550  regAddrB = ++pParse->nMem;
110551  regOutA = ++pParse->nMem;
110552  regOutB = ++pParse->nMem;
110553  sqlite3SelectDestInit(&destA, SRT_Coroutine, regAddrA);
110554  sqlite3SelectDestInit(&destB, SRT_Coroutine, regAddrB);
110555
110556  /* Generate a coroutine to evaluate the SELECT statement to the
110557  ** left of the compound operator - the "A" select.
110558  */
110559  addrSelectA = sqlite3VdbeCurrentAddr(v) + 1;
110560  j1 = sqlite3VdbeAddOp3(v, OP_InitCoroutine, regAddrA, 0, addrSelectA);
110561  VdbeComment((v, "left SELECT"));
110562  pPrior->iLimit = regLimitA;
110563  explainSetInteger(iSub1, pParse->iNextSelectId);
110564  sqlite3Select(pParse, pPrior, &destA);
110565  sqlite3VdbeAddOp1(v, OP_EndCoroutine, regAddrA);
110566  sqlite3VdbeJumpHere(v, j1);
110567
110568  /* Generate a coroutine to evaluate the SELECT statement on
110569  ** the right - the "B" select
110570  */
110571  addrSelectB = sqlite3VdbeCurrentAddr(v) + 1;
110572  j1 = sqlite3VdbeAddOp3(v, OP_InitCoroutine, regAddrB, 0, addrSelectB);
110573  VdbeComment((v, "right SELECT"));
110574  savedLimit = p->iLimit;
110575  savedOffset = p->iOffset;
110576  p->iLimit = regLimitB;
110577  p->iOffset = 0;
110578  explainSetInteger(iSub2, pParse->iNextSelectId);
110579  sqlite3Select(pParse, p, &destB);
110580  p->iLimit = savedLimit;
110581  p->iOffset = savedOffset;
110582  sqlite3VdbeAddOp1(v, OP_EndCoroutine, regAddrB);
110583
110584  /* Generate a subroutine that outputs the current row of the A
110585  ** select as the next output row of the compound select.
110586  */
110587  VdbeNoopComment((v, "Output routine for A"));
110588  addrOutA = generateOutputSubroutine(pParse,
110589                 p, &destA, pDest, regOutA,
110590                 regPrev, pKeyDup, labelEnd);
110591
110592  /* Generate a subroutine that outputs the current row of the B
110593  ** select as the next output row of the compound select.
110594  */
110595  if( op==TK_ALL || op==TK_UNION ){
110596    VdbeNoopComment((v, "Output routine for B"));
110597    addrOutB = generateOutputSubroutine(pParse,
110598                 p, &destB, pDest, regOutB,
110599                 regPrev, pKeyDup, labelEnd);
110600  }
110601  sqlite3KeyInfoUnref(pKeyDup);
110602
110603  /* Generate a subroutine to run when the results from select A
110604  ** are exhausted and only data in select B remains.
110605  */
110606  if( op==TK_EXCEPT || op==TK_INTERSECT ){
110607    addrEofA_noB = addrEofA = labelEnd;
110608  }else{
110609    VdbeNoopComment((v, "eof-A subroutine"));
110610    addrEofA = sqlite3VdbeAddOp2(v, OP_Gosub, regOutB, addrOutB);
110611    addrEofA_noB = sqlite3VdbeAddOp2(v, OP_Yield, regAddrB, labelEnd);
110612                                     VdbeCoverage(v);
110613    sqlite3VdbeAddOp2(v, OP_Goto, 0, addrEofA);
110614    p->nSelectRow += pPrior->nSelectRow;
110615  }
110616
110617  /* Generate a subroutine to run when the results from select B
110618  ** are exhausted and only data in select A remains.
110619  */
110620  if( op==TK_INTERSECT ){
110621    addrEofB = addrEofA;
110622    if( p->nSelectRow > pPrior->nSelectRow ) p->nSelectRow = pPrior->nSelectRow;
110623  }else{
110624    VdbeNoopComment((v, "eof-B subroutine"));
110625    addrEofB = sqlite3VdbeAddOp2(v, OP_Gosub, regOutA, addrOutA);
110626    sqlite3VdbeAddOp2(v, OP_Yield, regAddrA, labelEnd); VdbeCoverage(v);
110627    sqlite3VdbeAddOp2(v, OP_Goto, 0, addrEofB);
110628  }
110629
110630  /* Generate code to handle the case of A<B
110631  */
110632  VdbeNoopComment((v, "A-lt-B subroutine"));
110633  addrAltB = sqlite3VdbeAddOp2(v, OP_Gosub, regOutA, addrOutA);
110634  sqlite3VdbeAddOp2(v, OP_Yield, regAddrA, addrEofA); VdbeCoverage(v);
110635  sqlite3VdbeAddOp2(v, OP_Goto, 0, labelCmpr);
110636
110637  /* Generate code to handle the case of A==B
110638  */
110639  if( op==TK_ALL ){
110640    addrAeqB = addrAltB;
110641  }else if( op==TK_INTERSECT ){
110642    addrAeqB = addrAltB;
110643    addrAltB++;
110644  }else{
110645    VdbeNoopComment((v, "A-eq-B subroutine"));
110646    addrAeqB =
110647    sqlite3VdbeAddOp2(v, OP_Yield, regAddrA, addrEofA); VdbeCoverage(v);
110648    sqlite3VdbeAddOp2(v, OP_Goto, 0, labelCmpr);
110649  }
110650
110651  /* Generate code to handle the case of A>B
110652  */
110653  VdbeNoopComment((v, "A-gt-B subroutine"));
110654  addrAgtB = sqlite3VdbeCurrentAddr(v);
110655  if( op==TK_ALL || op==TK_UNION ){
110656    sqlite3VdbeAddOp2(v, OP_Gosub, regOutB, addrOutB);
110657  }
110658  sqlite3VdbeAddOp2(v, OP_Yield, regAddrB, addrEofB); VdbeCoverage(v);
110659  sqlite3VdbeAddOp2(v, OP_Goto, 0, labelCmpr);
110660
110661  /* This code runs once to initialize everything.
110662  */
110663  sqlite3VdbeJumpHere(v, j1);
110664  sqlite3VdbeAddOp2(v, OP_Yield, regAddrA, addrEofA_noB); VdbeCoverage(v);
110665  sqlite3VdbeAddOp2(v, OP_Yield, regAddrB, addrEofB); VdbeCoverage(v);
110666
110667  /* Implement the main merge loop
110668  */
110669  sqlite3VdbeResolveLabel(v, labelCmpr);
110670  sqlite3VdbeAddOp4(v, OP_Permutation, 0, 0, 0, (char*)aPermute, P4_INTARRAY);
110671  sqlite3VdbeAddOp4(v, OP_Compare, destA.iSdst, destB.iSdst, nOrderBy,
110672                         (char*)pKeyMerge, P4_KEYINFO);
110673  sqlite3VdbeChangeP5(v, OPFLAG_PERMUTE);
110674  sqlite3VdbeAddOp3(v, OP_Jump, addrAltB, addrAeqB, addrAgtB); VdbeCoverage(v);
110675
110676  /* Jump to the this point in order to terminate the query.
110677  */
110678  sqlite3VdbeResolveLabel(v, labelEnd);
110679
110680  /* Set the number of output columns
110681  */
110682  if( pDest->eDest==SRT_Output ){
110683    Select *pFirst = pPrior;
110684    while( pFirst->pPrior ) pFirst = pFirst->pPrior;
110685    generateColumnNames(pParse, 0, pFirst->pEList);
110686  }
110687
110688  /* Reassembly the compound query so that it will be freed correctly
110689  ** by the calling function */
110690  if( p->pPrior ){
110691    sqlite3SelectDelete(db, p->pPrior);
110692  }
110693  p->pPrior = pPrior;
110694  pPrior->pNext = p;
110695
110696  /*** TBD:  Insert subroutine calls to close cursors on incomplete
110697  **** subqueries ****/
110698  explainComposite(pParse, p->op, iSub1, iSub2, 0);
110699  return pParse->nErr!=0;
110700}
110701#endif
110702
110703#if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW)
110704/* Forward Declarations */
110705static void substExprList(sqlite3*, ExprList*, int, ExprList*);
110706static void substSelect(sqlite3*, Select *, int, ExprList *);
110707
110708/*
110709** Scan through the expression pExpr.  Replace every reference to
110710** a column in table number iTable with a copy of the iColumn-th
110711** entry in pEList.  (But leave references to the ROWID column
110712** unchanged.)
110713**
110714** This routine is part of the flattening procedure.  A subquery
110715** whose result set is defined by pEList appears as entry in the
110716** FROM clause of a SELECT such that the VDBE cursor assigned to that
110717** FORM clause entry is iTable.  This routine make the necessary
110718** changes to pExpr so that it refers directly to the source table
110719** of the subquery rather the result set of the subquery.
110720*/
110721static Expr *substExpr(
110722  sqlite3 *db,        /* Report malloc errors to this connection */
110723  Expr *pExpr,        /* Expr in which substitution occurs */
110724  int iTable,         /* Table to be substituted */
110725  ExprList *pEList    /* Substitute expressions */
110726){
110727  if( pExpr==0 ) return 0;
110728  if( pExpr->op==TK_COLUMN && pExpr->iTable==iTable ){
110729    if( pExpr->iColumn<0 ){
110730      pExpr->op = TK_NULL;
110731    }else{
110732      Expr *pNew;
110733      assert( pEList!=0 && pExpr->iColumn<pEList->nExpr );
110734      assert( pExpr->pLeft==0 && pExpr->pRight==0 );
110735      pNew = sqlite3ExprDup(db, pEList->a[pExpr->iColumn].pExpr, 0);
110736      sqlite3ExprDelete(db, pExpr);
110737      pExpr = pNew;
110738    }
110739  }else{
110740    pExpr->pLeft = substExpr(db, pExpr->pLeft, iTable, pEList);
110741    pExpr->pRight = substExpr(db, pExpr->pRight, iTable, pEList);
110742    if( ExprHasProperty(pExpr, EP_xIsSelect) ){
110743      substSelect(db, pExpr->x.pSelect, iTable, pEList);
110744    }else{
110745      substExprList(db, pExpr->x.pList, iTable, pEList);
110746    }
110747  }
110748  return pExpr;
110749}
110750static void substExprList(
110751  sqlite3 *db,         /* Report malloc errors here */
110752  ExprList *pList,     /* List to scan and in which to make substitutes */
110753  int iTable,          /* Table to be substituted */
110754  ExprList *pEList     /* Substitute values */
110755){
110756  int i;
110757  if( pList==0 ) return;
110758  for(i=0; i<pList->nExpr; i++){
110759    pList->a[i].pExpr = substExpr(db, pList->a[i].pExpr, iTable, pEList);
110760  }
110761}
110762static void substSelect(
110763  sqlite3 *db,         /* Report malloc errors here */
110764  Select *p,           /* SELECT statement in which to make substitutions */
110765  int iTable,          /* Table to be replaced */
110766  ExprList *pEList     /* Substitute values */
110767){
110768  SrcList *pSrc;
110769  struct SrcList_item *pItem;
110770  int i;
110771  if( !p ) return;
110772  substExprList(db, p->pEList, iTable, pEList);
110773  substExprList(db, p->pGroupBy, iTable, pEList);
110774  substExprList(db, p->pOrderBy, iTable, pEList);
110775  p->pHaving = substExpr(db, p->pHaving, iTable, pEList);
110776  p->pWhere = substExpr(db, p->pWhere, iTable, pEList);
110777  substSelect(db, p->pPrior, iTable, pEList);
110778  pSrc = p->pSrc;
110779  assert( pSrc );  /* Even for (SELECT 1) we have: pSrc!=0 but pSrc->nSrc==0 */
110780  if( ALWAYS(pSrc) ){
110781    for(i=pSrc->nSrc, pItem=pSrc->a; i>0; i--, pItem++){
110782      substSelect(db, pItem->pSelect, iTable, pEList);
110783    }
110784  }
110785}
110786#endif /* !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW) */
110787
110788#if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW)
110789/*
110790** This routine attempts to flatten subqueries as a performance optimization.
110791** This routine returns 1 if it makes changes and 0 if no flattening occurs.
110792**
110793** To understand the concept of flattening, consider the following
110794** query:
110795**
110796**     SELECT a FROM (SELECT x+y AS a FROM t1 WHERE z<100) WHERE a>5
110797**
110798** The default way of implementing this query is to execute the
110799** subquery first and store the results in a temporary table, then
110800** run the outer query on that temporary table.  This requires two
110801** passes over the data.  Furthermore, because the temporary table
110802** has no indices, the WHERE clause on the outer query cannot be
110803** optimized.
110804**
110805** This routine attempts to rewrite queries such as the above into
110806** a single flat select, like this:
110807**
110808**     SELECT x+y AS a FROM t1 WHERE z<100 AND a>5
110809**
110810** The code generated for this simplification gives the same result
110811** but only has to scan the data once.  And because indices might
110812** exist on the table t1, a complete scan of the data might be
110813** avoided.
110814**
110815** Flattening is only attempted if all of the following are true:
110816**
110817**   (1)  The subquery and the outer query do not both use aggregates.
110818**
110819**   (2)  The subquery is not an aggregate or (2a) the outer query is not a join
110820**        and (2b) the outer query does not use subqueries other than the one
110821**        FROM-clause subquery that is a candidate for flattening.  (2b is
110822**        due to ticket [2f7170d73bf9abf80] from 2015-02-09.)
110823**
110824**   (3)  The subquery is not the right operand of a left outer join
110825**        (Originally ticket #306.  Strengthened by ticket #3300)
110826**
110827**   (4)  The subquery is not DISTINCT.
110828**
110829**  (**)  At one point restrictions (4) and (5) defined a subset of DISTINCT
110830**        sub-queries that were excluded from this optimization. Restriction
110831**        (4) has since been expanded to exclude all DISTINCT subqueries.
110832**
110833**   (6)  The subquery does not use aggregates or the outer query is not
110834**        DISTINCT.
110835**
110836**   (7)  The subquery has a FROM clause.  TODO:  For subqueries without
110837**        A FROM clause, consider adding a FROM close with the special
110838**        table sqlite_once that consists of a single row containing a
110839**        single NULL.
110840**
110841**   (8)  The subquery does not use LIMIT or the outer query is not a join.
110842**
110843**   (9)  The subquery does not use LIMIT or the outer query does not use
110844**        aggregates.
110845**
110846**  (**)  Restriction (10) was removed from the code on 2005-02-05 but we
110847**        accidently carried the comment forward until 2014-09-15.  Original
110848**        text: "The subquery does not use aggregates or the outer query
110849**        does not use LIMIT."
110850**
110851**  (11)  The subquery and the outer query do not both have ORDER BY clauses.
110852**
110853**  (**)  Not implemented.  Subsumed into restriction (3).  Was previously
110854**        a separate restriction deriving from ticket #350.
110855**
110856**  (13)  The subquery and outer query do not both use LIMIT.
110857**
110858**  (14)  The subquery does not use OFFSET.
110859**
110860**  (15)  The outer query is not part of a compound select or the
110861**        subquery does not have a LIMIT clause.
110862**        (See ticket #2339 and ticket [02a8e81d44]).
110863**
110864**  (16)  The outer query is not an aggregate or the subquery does
110865**        not contain ORDER BY.  (Ticket #2942)  This used to not matter
110866**        until we introduced the group_concat() function.
110867**
110868**  (17)  The sub-query is not a compound select, or it is a UNION ALL
110869**        compound clause made up entirely of non-aggregate queries, and
110870**        the parent query:
110871**
110872**          * is not itself part of a compound select,
110873**          * is not an aggregate or DISTINCT query, and
110874**          * is not a join
110875**
110876**        The parent and sub-query may contain WHERE clauses. Subject to
110877**        rules (11), (13) and (14), they may also contain ORDER BY,
110878**        LIMIT and OFFSET clauses.  The subquery cannot use any compound
110879**        operator other than UNION ALL because all the other compound
110880**        operators have an implied DISTINCT which is disallowed by
110881**        restriction (4).
110882**
110883**        Also, each component of the sub-query must return the same number
110884**        of result columns. This is actually a requirement for any compound
110885**        SELECT statement, but all the code here does is make sure that no
110886**        such (illegal) sub-query is flattened. The caller will detect the
110887**        syntax error and return a detailed message.
110888**
110889**  (18)  If the sub-query is a compound select, then all terms of the
110890**        ORDER by clause of the parent must be simple references to
110891**        columns of the sub-query.
110892**
110893**  (19)  The subquery does not use LIMIT or the outer query does not
110894**        have a WHERE clause.
110895**
110896**  (20)  If the sub-query is a compound select, then it must not use
110897**        an ORDER BY clause.  Ticket #3773.  We could relax this constraint
110898**        somewhat by saying that the terms of the ORDER BY clause must
110899**        appear as unmodified result columns in the outer query.  But we
110900**        have other optimizations in mind to deal with that case.
110901**
110902**  (21)  The subquery does not use LIMIT or the outer query is not
110903**        DISTINCT.  (See ticket [752e1646fc]).
110904**
110905**  (22)  The subquery is not a recursive CTE.
110906**
110907**  (23)  The parent is not a recursive CTE, or the sub-query is not a
110908**        compound query. This restriction is because transforming the
110909**        parent to a compound query confuses the code that handles
110910**        recursive queries in multiSelect().
110911**
110912**  (24)  The subquery is not an aggregate that uses the built-in min() or
110913**        or max() functions.  (Without this restriction, a query like:
110914**        "SELECT x FROM (SELECT max(y), x FROM t1)" would not necessarily
110915**        return the value X for which Y was maximal.)
110916**
110917**
110918** In this routine, the "p" parameter is a pointer to the outer query.
110919** The subquery is p->pSrc->a[iFrom].  isAgg is true if the outer query
110920** uses aggregates and subqueryIsAgg is true if the subquery uses aggregates.
110921**
110922** If flattening is not attempted, this routine is a no-op and returns 0.
110923** If flattening is attempted this routine returns 1.
110924**
110925** All of the expression analysis must occur on both the outer query and
110926** the subquery before this routine runs.
110927*/
110928static int flattenSubquery(
110929  Parse *pParse,       /* Parsing context */
110930  Select *p,           /* The parent or outer SELECT statement */
110931  int iFrom,           /* Index in p->pSrc->a[] of the inner subquery */
110932  int isAgg,           /* True if outer SELECT uses aggregate functions */
110933  int subqueryIsAgg    /* True if the subquery uses aggregate functions */
110934){
110935  const char *zSavedAuthContext = pParse->zAuthContext;
110936  Select *pParent;
110937  Select *pSub;       /* The inner query or "subquery" */
110938  Select *pSub1;      /* Pointer to the rightmost select in sub-query */
110939  SrcList *pSrc;      /* The FROM clause of the outer query */
110940  SrcList *pSubSrc;   /* The FROM clause of the subquery */
110941  ExprList *pList;    /* The result set of the outer query */
110942  int iParent;        /* VDBE cursor number of the pSub result set temp table */
110943  int i;              /* Loop counter */
110944  Expr *pWhere;                    /* The WHERE clause */
110945  struct SrcList_item *pSubitem;   /* The subquery */
110946  sqlite3 *db = pParse->db;
110947
110948  /* Check to see if flattening is permitted.  Return 0 if not.
110949  */
110950  assert( p!=0 );
110951  assert( p->pPrior==0 );  /* Unable to flatten compound queries */
110952  if( OptimizationDisabled(db, SQLITE_QueryFlattener) ) return 0;
110953  pSrc = p->pSrc;
110954  assert( pSrc && iFrom>=0 && iFrom<pSrc->nSrc );
110955  pSubitem = &pSrc->a[iFrom];
110956  iParent = pSubitem->iCursor;
110957  pSub = pSubitem->pSelect;
110958  assert( pSub!=0 );
110959  if( subqueryIsAgg ){
110960    if( isAgg ) return 0;                                /* Restriction (1)   */
110961    if( pSrc->nSrc>1 ) return 0;                         /* Restriction (2a)  */
110962    if( (p->pWhere && ExprHasProperty(p->pWhere,EP_Subquery))
110963     || (sqlite3ExprListFlags(p->pEList) & EP_Subquery)!=0
110964     || (sqlite3ExprListFlags(p->pOrderBy) & EP_Subquery)!=0
110965    ){
110966      return 0;                                          /* Restriction (2b)  */
110967    }
110968  }
110969
110970  pSubSrc = pSub->pSrc;
110971  assert( pSubSrc );
110972  /* Prior to version 3.1.2, when LIMIT and OFFSET had to be simple constants,
110973  ** not arbitrary expressions, we allowed some combining of LIMIT and OFFSET
110974  ** because they could be computed at compile-time.  But when LIMIT and OFFSET
110975  ** became arbitrary expressions, we were forced to add restrictions (13)
110976  ** and (14). */
110977  if( pSub->pLimit && p->pLimit ) return 0;              /* Restriction (13) */
110978  if( pSub->pOffset ) return 0;                          /* Restriction (14) */
110979  if( (p->selFlags & SF_Compound)!=0 && pSub->pLimit ){
110980    return 0;                                            /* Restriction (15) */
110981  }
110982  if( pSubSrc->nSrc==0 ) return 0;                       /* Restriction (7)  */
110983  if( pSub->selFlags & SF_Distinct ) return 0;           /* Restriction (5)  */
110984  if( pSub->pLimit && (pSrc->nSrc>1 || isAgg) ){
110985     return 0;         /* Restrictions (8)(9) */
110986  }
110987  if( (p->selFlags & SF_Distinct)!=0 && subqueryIsAgg ){
110988     return 0;         /* Restriction (6)  */
110989  }
110990  if( p->pOrderBy && pSub->pOrderBy ){
110991     return 0;                                           /* Restriction (11) */
110992  }
110993  if( isAgg && pSub->pOrderBy ) return 0;                /* Restriction (16) */
110994  if( pSub->pLimit && p->pWhere ) return 0;              /* Restriction (19) */
110995  if( pSub->pLimit && (p->selFlags & SF_Distinct)!=0 ){
110996     return 0;         /* Restriction (21) */
110997  }
110998  testcase( pSub->selFlags & SF_Recursive );
110999  testcase( pSub->selFlags & SF_MinMaxAgg );
111000  if( pSub->selFlags & (SF_Recursive|SF_MinMaxAgg) ){
111001    return 0; /* Restrictions (22) and (24) */
111002  }
111003  if( (p->selFlags & SF_Recursive) && pSub->pPrior ){
111004    return 0; /* Restriction (23) */
111005  }
111006
111007  /* OBSOLETE COMMENT 1:
111008  ** Restriction 3:  If the subquery is a join, make sure the subquery is
111009  ** not used as the right operand of an outer join.  Examples of why this
111010  ** is not allowed:
111011  **
111012  **         t1 LEFT OUTER JOIN (t2 JOIN t3)
111013  **
111014  ** If we flatten the above, we would get
111015  **
111016  **         (t1 LEFT OUTER JOIN t2) JOIN t3
111017  **
111018  ** which is not at all the same thing.
111019  **
111020  ** OBSOLETE COMMENT 2:
111021  ** Restriction 12:  If the subquery is the right operand of a left outer
111022  ** join, make sure the subquery has no WHERE clause.
111023  ** An examples of why this is not allowed:
111024  **
111025  **         t1 LEFT OUTER JOIN (SELECT * FROM t2 WHERE t2.x>0)
111026  **
111027  ** If we flatten the above, we would get
111028  **
111029  **         (t1 LEFT OUTER JOIN t2) WHERE t2.x>0
111030  **
111031  ** But the t2.x>0 test will always fail on a NULL row of t2, which
111032  ** effectively converts the OUTER JOIN into an INNER JOIN.
111033  **
111034  ** THIS OVERRIDES OBSOLETE COMMENTS 1 AND 2 ABOVE:
111035  ** Ticket #3300 shows that flattening the right term of a LEFT JOIN
111036  ** is fraught with danger.  Best to avoid the whole thing.  If the
111037  ** subquery is the right term of a LEFT JOIN, then do not flatten.
111038  */
111039  if( (pSubitem->jointype & JT_OUTER)!=0 ){
111040    return 0;
111041  }
111042
111043  /* Restriction 17: If the sub-query is a compound SELECT, then it must
111044  ** use only the UNION ALL operator. And none of the simple select queries
111045  ** that make up the compound SELECT are allowed to be aggregate or distinct
111046  ** queries.
111047  */
111048  if( pSub->pPrior ){
111049    if( pSub->pOrderBy ){
111050      return 0;  /* Restriction 20 */
111051    }
111052    if( isAgg || (p->selFlags & SF_Distinct)!=0 || pSrc->nSrc!=1 ){
111053      return 0;
111054    }
111055    for(pSub1=pSub; pSub1; pSub1=pSub1->pPrior){
111056      testcase( (pSub1->selFlags & (SF_Distinct|SF_Aggregate))==SF_Distinct );
111057      testcase( (pSub1->selFlags & (SF_Distinct|SF_Aggregate))==SF_Aggregate );
111058      assert( pSub->pSrc!=0 );
111059      assert( pSub->pEList->nExpr==pSub1->pEList->nExpr );
111060      if( (pSub1->selFlags & (SF_Distinct|SF_Aggregate))!=0
111061       || (pSub1->pPrior && pSub1->op!=TK_ALL)
111062       || pSub1->pSrc->nSrc<1
111063      ){
111064        return 0;
111065      }
111066      testcase( pSub1->pSrc->nSrc>1 );
111067    }
111068
111069    /* Restriction 18. */
111070    if( p->pOrderBy ){
111071      int ii;
111072      for(ii=0; ii<p->pOrderBy->nExpr; ii++){
111073        if( p->pOrderBy->a[ii].u.x.iOrderByCol==0 ) return 0;
111074      }
111075    }
111076  }
111077
111078  /***** If we reach this point, flattening is permitted. *****/
111079  SELECTTRACE(1,pParse,p,("flatten %s.%p from term %d\n",
111080                   pSub->zSelName, pSub, iFrom));
111081
111082  /* Authorize the subquery */
111083  pParse->zAuthContext = pSubitem->zName;
111084  TESTONLY(i =) sqlite3AuthCheck(pParse, SQLITE_SELECT, 0, 0, 0);
111085  testcase( i==SQLITE_DENY );
111086  pParse->zAuthContext = zSavedAuthContext;
111087
111088  /* If the sub-query is a compound SELECT statement, then (by restrictions
111089  ** 17 and 18 above) it must be a UNION ALL and the parent query must
111090  ** be of the form:
111091  **
111092  **     SELECT <expr-list> FROM (<sub-query>) <where-clause>
111093  **
111094  ** followed by any ORDER BY, LIMIT and/or OFFSET clauses. This block
111095  ** creates N-1 copies of the parent query without any ORDER BY, LIMIT or
111096  ** OFFSET clauses and joins them to the left-hand-side of the original
111097  ** using UNION ALL operators. In this case N is the number of simple
111098  ** select statements in the compound sub-query.
111099  **
111100  ** Example:
111101  **
111102  **     SELECT a+1 FROM (
111103  **        SELECT x FROM tab
111104  **        UNION ALL
111105  **        SELECT y FROM tab
111106  **        UNION ALL
111107  **        SELECT abs(z*2) FROM tab2
111108  **     ) WHERE a!=5 ORDER BY 1
111109  **
111110  ** Transformed into:
111111  **
111112  **     SELECT x+1 FROM tab WHERE x+1!=5
111113  **     UNION ALL
111114  **     SELECT y+1 FROM tab WHERE y+1!=5
111115  **     UNION ALL
111116  **     SELECT abs(z*2)+1 FROM tab2 WHERE abs(z*2)+1!=5
111117  **     ORDER BY 1
111118  **
111119  ** We call this the "compound-subquery flattening".
111120  */
111121  for(pSub=pSub->pPrior; pSub; pSub=pSub->pPrior){
111122    Select *pNew;
111123    ExprList *pOrderBy = p->pOrderBy;
111124    Expr *pLimit = p->pLimit;
111125    Expr *pOffset = p->pOffset;
111126    Select *pPrior = p->pPrior;
111127    p->pOrderBy = 0;
111128    p->pSrc = 0;
111129    p->pPrior = 0;
111130    p->pLimit = 0;
111131    p->pOffset = 0;
111132    pNew = sqlite3SelectDup(db, p, 0);
111133    sqlite3SelectSetName(pNew, pSub->zSelName);
111134    p->pOffset = pOffset;
111135    p->pLimit = pLimit;
111136    p->pOrderBy = pOrderBy;
111137    p->pSrc = pSrc;
111138    p->op = TK_ALL;
111139    if( pNew==0 ){
111140      p->pPrior = pPrior;
111141    }else{
111142      pNew->pPrior = pPrior;
111143      if( pPrior ) pPrior->pNext = pNew;
111144      pNew->pNext = p;
111145      p->pPrior = pNew;
111146      SELECTTRACE(2,pParse,p,
111147         ("compound-subquery flattener creates %s.%p as peer\n",
111148         pNew->zSelName, pNew));
111149    }
111150    if( db->mallocFailed ) return 1;
111151  }
111152
111153  /* Begin flattening the iFrom-th entry of the FROM clause
111154  ** in the outer query.
111155  */
111156  pSub = pSub1 = pSubitem->pSelect;
111157
111158  /* Delete the transient table structure associated with the
111159  ** subquery
111160  */
111161  sqlite3DbFree(db, pSubitem->zDatabase);
111162  sqlite3DbFree(db, pSubitem->zName);
111163  sqlite3DbFree(db, pSubitem->zAlias);
111164  pSubitem->zDatabase = 0;
111165  pSubitem->zName = 0;
111166  pSubitem->zAlias = 0;
111167  pSubitem->pSelect = 0;
111168
111169  /* Defer deleting the Table object associated with the
111170  ** subquery until code generation is
111171  ** complete, since there may still exist Expr.pTab entries that
111172  ** refer to the subquery even after flattening.  Ticket #3346.
111173  **
111174  ** pSubitem->pTab is always non-NULL by test restrictions and tests above.
111175  */
111176  if( ALWAYS(pSubitem->pTab!=0) ){
111177    Table *pTabToDel = pSubitem->pTab;
111178    if( pTabToDel->nRef==1 ){
111179      Parse *pToplevel = sqlite3ParseToplevel(pParse);
111180      pTabToDel->pNextZombie = pToplevel->pZombieTab;
111181      pToplevel->pZombieTab = pTabToDel;
111182    }else{
111183      pTabToDel->nRef--;
111184    }
111185    pSubitem->pTab = 0;
111186  }
111187
111188  /* The following loop runs once for each term in a compound-subquery
111189  ** flattening (as described above).  If we are doing a different kind
111190  ** of flattening - a flattening other than a compound-subquery flattening -
111191  ** then this loop only runs once.
111192  **
111193  ** This loop moves all of the FROM elements of the subquery into the
111194  ** the FROM clause of the outer query.  Before doing this, remember
111195  ** the cursor number for the original outer query FROM element in
111196  ** iParent.  The iParent cursor will never be used.  Subsequent code
111197  ** will scan expressions looking for iParent references and replace
111198  ** those references with expressions that resolve to the subquery FROM
111199  ** elements we are now copying in.
111200  */
111201  for(pParent=p; pParent; pParent=pParent->pPrior, pSub=pSub->pPrior){
111202    int nSubSrc;
111203    u8 jointype = 0;
111204    pSubSrc = pSub->pSrc;     /* FROM clause of subquery */
111205    nSubSrc = pSubSrc->nSrc;  /* Number of terms in subquery FROM clause */
111206    pSrc = pParent->pSrc;     /* FROM clause of the outer query */
111207
111208    if( pSrc ){
111209      assert( pParent==p );  /* First time through the loop */
111210      jointype = pSubitem->jointype;
111211    }else{
111212      assert( pParent!=p );  /* 2nd and subsequent times through the loop */
111213      pSrc = pParent->pSrc = sqlite3SrcListAppend(db, 0, 0, 0);
111214      if( pSrc==0 ){
111215        assert( db->mallocFailed );
111216        break;
111217      }
111218    }
111219
111220    /* The subquery uses a single slot of the FROM clause of the outer
111221    ** query.  If the subquery has more than one element in its FROM clause,
111222    ** then expand the outer query to make space for it to hold all elements
111223    ** of the subquery.
111224    **
111225    ** Example:
111226    **
111227    **    SELECT * FROM tabA, (SELECT * FROM sub1, sub2), tabB;
111228    **
111229    ** The outer query has 3 slots in its FROM clause.  One slot of the
111230    ** outer query (the middle slot) is used by the subquery.  The next
111231    ** block of code will expand the out query to 4 slots.  The middle
111232    ** slot is expanded to two slots in order to make space for the
111233    ** two elements in the FROM clause of the subquery.
111234    */
111235    if( nSubSrc>1 ){
111236      pParent->pSrc = pSrc = sqlite3SrcListEnlarge(db, pSrc, nSubSrc-1,iFrom+1);
111237      if( db->mallocFailed ){
111238        break;
111239      }
111240    }
111241
111242    /* Transfer the FROM clause terms from the subquery into the
111243    ** outer query.
111244    */
111245    for(i=0; i<nSubSrc; i++){
111246      sqlite3IdListDelete(db, pSrc->a[i+iFrom].pUsing);
111247      pSrc->a[i+iFrom] = pSubSrc->a[i];
111248      memset(&pSubSrc->a[i], 0, sizeof(pSubSrc->a[i]));
111249    }
111250    pSrc->a[iFrom].jointype = jointype;
111251
111252    /* Now begin substituting subquery result set expressions for
111253    ** references to the iParent in the outer query.
111254    **
111255    ** Example:
111256    **
111257    **   SELECT a+5, b*10 FROM (SELECT x*3 AS a, y+10 AS b FROM t1) WHERE a>b;
111258    **   \                     \_____________ subquery __________/          /
111259    **    \_____________________ outer query ______________________________/
111260    **
111261    ** We look at every expression in the outer query and every place we see
111262    ** "a" we substitute "x*3" and every place we see "b" we substitute "y+10".
111263    */
111264    pList = pParent->pEList;
111265    for(i=0; i<pList->nExpr; i++){
111266      if( pList->a[i].zName==0 ){
111267        char *zName = sqlite3DbStrDup(db, pList->a[i].zSpan);
111268        sqlite3Dequote(zName);
111269        pList->a[i].zName = zName;
111270      }
111271    }
111272    substExprList(db, pParent->pEList, iParent, pSub->pEList);
111273    if( isAgg ){
111274      substExprList(db, pParent->pGroupBy, iParent, pSub->pEList);
111275      pParent->pHaving = substExpr(db, pParent->pHaving, iParent, pSub->pEList);
111276    }
111277    if( pSub->pOrderBy ){
111278      /* At this point, any non-zero iOrderByCol values indicate that the
111279      ** ORDER BY column expression is identical to the iOrderByCol'th
111280      ** expression returned by SELECT statement pSub. Since these values
111281      ** do not necessarily correspond to columns in SELECT statement pParent,
111282      ** zero them before transfering the ORDER BY clause.
111283      **
111284      ** Not doing this may cause an error if a subsequent call to this
111285      ** function attempts to flatten a compound sub-query into pParent
111286      ** (the only way this can happen is if the compound sub-query is
111287      ** currently part of pSub->pSrc). See ticket [d11a6e908f].  */
111288      ExprList *pOrderBy = pSub->pOrderBy;
111289      for(i=0; i<pOrderBy->nExpr; i++){
111290        pOrderBy->a[i].u.x.iOrderByCol = 0;
111291      }
111292      assert( pParent->pOrderBy==0 );
111293      assert( pSub->pPrior==0 );
111294      pParent->pOrderBy = pOrderBy;
111295      pSub->pOrderBy = 0;
111296    }else if( pParent->pOrderBy ){
111297      substExprList(db, pParent->pOrderBy, iParent, pSub->pEList);
111298    }
111299    if( pSub->pWhere ){
111300      pWhere = sqlite3ExprDup(db, pSub->pWhere, 0);
111301    }else{
111302      pWhere = 0;
111303    }
111304    if( subqueryIsAgg ){
111305      assert( pParent->pHaving==0 );
111306      pParent->pHaving = pParent->pWhere;
111307      pParent->pWhere = pWhere;
111308      pParent->pHaving = substExpr(db, pParent->pHaving, iParent, pSub->pEList);
111309      pParent->pHaving = sqlite3ExprAnd(db, pParent->pHaving,
111310                                  sqlite3ExprDup(db, pSub->pHaving, 0));
111311      assert( pParent->pGroupBy==0 );
111312      pParent->pGroupBy = sqlite3ExprListDup(db, pSub->pGroupBy, 0);
111313    }else{
111314      pParent->pWhere = substExpr(db, pParent->pWhere, iParent, pSub->pEList);
111315      pParent->pWhere = sqlite3ExprAnd(db, pParent->pWhere, pWhere);
111316    }
111317
111318    /* The flattened query is distinct if either the inner or the
111319    ** outer query is distinct.
111320    */
111321    pParent->selFlags |= pSub->selFlags & SF_Distinct;
111322
111323    /*
111324    ** SELECT ... FROM (SELECT ... LIMIT a OFFSET b) LIMIT x OFFSET y;
111325    **
111326    ** One is tempted to try to add a and b to combine the limits.  But this
111327    ** does not work if either limit is negative.
111328    */
111329    if( pSub->pLimit ){
111330      pParent->pLimit = pSub->pLimit;
111331      pSub->pLimit = 0;
111332    }
111333  }
111334
111335  /* Finially, delete what is left of the subquery and return
111336  ** success.
111337  */
111338  sqlite3SelectDelete(db, pSub1);
111339
111340#if SELECTTRACE_ENABLED
111341  if( sqlite3SelectTrace & 0x100 ){
111342    SELECTTRACE(0x100,pParse,p,("After flattening:\n"));
111343    sqlite3TreeViewSelect(0, p, 0);
111344  }
111345#endif
111346
111347  return 1;
111348}
111349#endif /* !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW) */
111350
111351
111352
111353#if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW)
111354/*
111355** Make copies of relevant WHERE clause terms of the outer query into
111356** the WHERE clause of subquery.  Example:
111357**
111358**    SELECT * FROM (SELECT a AS x, c-d AS y FROM t1) WHERE x=5 AND y=10;
111359**
111360** Transformed into:
111361**
111362**    SELECT * FROM (SELECT a AS x, c-d AS y FROM t1 WHERE a=5 AND c-d=10)
111363**     WHERE x=5 AND y=10;
111364**
111365** The hope is that the terms added to the inner query will make it more
111366** efficient.
111367**
111368** Do not attempt this optimization if:
111369**
111370**   (1) The inner query is an aggregate.  (In that case, we'd really want
111371**       to copy the outer WHERE-clause terms onto the HAVING clause of the
111372**       inner query.  But they probably won't help there so do not bother.)
111373**
111374**   (2) The inner query is the recursive part of a common table expression.
111375**
111376**   (3) The inner query has a LIMIT clause (since the changes to the WHERE
111377**       close would change the meaning of the LIMIT).
111378**
111379**   (4) The inner query is the right operand of a LEFT JOIN.  (The caller
111380**       enforces this restriction since this routine does not have enough
111381**       information to know.)
111382**
111383** Return 0 if no changes are made and non-zero if one or more WHERE clause
111384** terms are duplicated into the subquery.
111385*/
111386static int pushDownWhereTerms(
111387  sqlite3 *db,          /* The database connection (for malloc()) */
111388  Select *pSubq,        /* The subquery whose WHERE clause is to be augmented */
111389  Expr *pWhere,         /* The WHERE clause of the outer query */
111390  int iCursor           /* Cursor number of the subquery */
111391){
111392  Expr *pNew;
111393  int nChng = 0;
111394  if( pWhere==0 ) return 0;
111395  if( (pSubq->selFlags & (SF_Aggregate|SF_Recursive))!=0 ){
111396     return 0; /* restrictions (1) and (2) */
111397  }
111398  if( pSubq->pLimit!=0 ){
111399     return 0; /* restriction (3) */
111400  }
111401  while( pWhere->op==TK_AND ){
111402    nChng += pushDownWhereTerms(db, pSubq, pWhere->pRight, iCursor);
111403    pWhere = pWhere->pLeft;
111404  }
111405  if( sqlite3ExprIsTableConstant(pWhere, iCursor) ){
111406    nChng++;
111407    while( pSubq ){
111408      pNew = sqlite3ExprDup(db, pWhere, 0);
111409      pNew = substExpr(db, pNew, iCursor, pSubq->pEList);
111410      pSubq->pWhere = sqlite3ExprAnd(db, pSubq->pWhere, pNew);
111411      pSubq = pSubq->pPrior;
111412    }
111413  }
111414  return nChng;
111415}
111416#endif /* !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW) */
111417
111418/*
111419** Based on the contents of the AggInfo structure indicated by the first
111420** argument, this function checks if the following are true:
111421**
111422**    * the query contains just a single aggregate function,
111423**    * the aggregate function is either min() or max(), and
111424**    * the argument to the aggregate function is a column value.
111425**
111426** If all of the above are true, then WHERE_ORDERBY_MIN or WHERE_ORDERBY_MAX
111427** is returned as appropriate. Also, *ppMinMax is set to point to the
111428** list of arguments passed to the aggregate before returning.
111429**
111430** Or, if the conditions above are not met, *ppMinMax is set to 0 and
111431** WHERE_ORDERBY_NORMAL is returned.
111432*/
111433static u8 minMaxQuery(AggInfo *pAggInfo, ExprList **ppMinMax){
111434  int eRet = WHERE_ORDERBY_NORMAL;          /* Return value */
111435
111436  *ppMinMax = 0;
111437  if( pAggInfo->nFunc==1 ){
111438    Expr *pExpr = pAggInfo->aFunc[0].pExpr; /* Aggregate function */
111439    ExprList *pEList = pExpr->x.pList;      /* Arguments to agg function */
111440
111441    assert( pExpr->op==TK_AGG_FUNCTION );
111442    if( pEList && pEList->nExpr==1 && pEList->a[0].pExpr->op==TK_AGG_COLUMN ){
111443      const char *zFunc = pExpr->u.zToken;
111444      if( sqlite3StrICmp(zFunc, "min")==0 ){
111445        eRet = WHERE_ORDERBY_MIN;
111446        *ppMinMax = pEList;
111447      }else if( sqlite3StrICmp(zFunc, "max")==0 ){
111448        eRet = WHERE_ORDERBY_MAX;
111449        *ppMinMax = pEList;
111450      }
111451    }
111452  }
111453
111454  assert( *ppMinMax==0 || (*ppMinMax)->nExpr==1 );
111455  return eRet;
111456}
111457
111458/*
111459** The select statement passed as the first argument is an aggregate query.
111460** The second argument is the associated aggregate-info object. This
111461** function tests if the SELECT is of the form:
111462**
111463**   SELECT count(*) FROM <tbl>
111464**
111465** where table is a database table, not a sub-select or view. If the query
111466** does match this pattern, then a pointer to the Table object representing
111467** <tbl> is returned. Otherwise, 0 is returned.
111468*/
111469static Table *isSimpleCount(Select *p, AggInfo *pAggInfo){
111470  Table *pTab;
111471  Expr *pExpr;
111472
111473  assert( !p->pGroupBy );
111474
111475  if( p->pWhere || p->pEList->nExpr!=1
111476   || p->pSrc->nSrc!=1 || p->pSrc->a[0].pSelect
111477  ){
111478    return 0;
111479  }
111480  pTab = p->pSrc->a[0].pTab;
111481  pExpr = p->pEList->a[0].pExpr;
111482  assert( pTab && !pTab->pSelect && pExpr );
111483
111484  if( IsVirtual(pTab) ) return 0;
111485  if( pExpr->op!=TK_AGG_FUNCTION ) return 0;
111486  if( NEVER(pAggInfo->nFunc==0) ) return 0;
111487  if( (pAggInfo->aFunc[0].pFunc->funcFlags&SQLITE_FUNC_COUNT)==0 ) return 0;
111488  if( pExpr->flags&EP_Distinct ) return 0;
111489
111490  return pTab;
111491}
111492
111493/*
111494** If the source-list item passed as an argument was augmented with an
111495** INDEXED BY clause, then try to locate the specified index. If there
111496** was such a clause and the named index cannot be found, return
111497** SQLITE_ERROR and leave an error in pParse. Otherwise, populate
111498** pFrom->pIndex and return SQLITE_OK.
111499*/
111500SQLITE_PRIVATE int sqlite3IndexedByLookup(Parse *pParse, struct SrcList_item *pFrom){
111501  if( pFrom->pTab && pFrom->zIndexedBy ){
111502    Table *pTab = pFrom->pTab;
111503    char *zIndexedBy = pFrom->zIndexedBy;
111504    Index *pIdx;
111505    for(pIdx=pTab->pIndex;
111506        pIdx && sqlite3StrICmp(pIdx->zName, zIndexedBy);
111507        pIdx=pIdx->pNext
111508    );
111509    if( !pIdx ){
111510      sqlite3ErrorMsg(pParse, "no such index: %s", zIndexedBy, 0);
111511      pParse->checkSchema = 1;
111512      return SQLITE_ERROR;
111513    }
111514    pFrom->pIndex = pIdx;
111515  }
111516  return SQLITE_OK;
111517}
111518/*
111519** Detect compound SELECT statements that use an ORDER BY clause with
111520** an alternative collating sequence.
111521**
111522**    SELECT ... FROM t1 EXCEPT SELECT ... FROM t2 ORDER BY .. COLLATE ...
111523**
111524** These are rewritten as a subquery:
111525**
111526**    SELECT * FROM (SELECT ... FROM t1 EXCEPT SELECT ... FROM t2)
111527**     ORDER BY ... COLLATE ...
111528**
111529** This transformation is necessary because the multiSelectOrderBy() routine
111530** above that generates the code for a compound SELECT with an ORDER BY clause
111531** uses a merge algorithm that requires the same collating sequence on the
111532** result columns as on the ORDER BY clause.  See ticket
111533** http://www.sqlite.org/src/info/6709574d2a
111534**
111535** This transformation is only needed for EXCEPT, INTERSECT, and UNION.
111536** The UNION ALL operator works fine with multiSelectOrderBy() even when
111537** there are COLLATE terms in the ORDER BY.
111538*/
111539static int convertCompoundSelectToSubquery(Walker *pWalker, Select *p){
111540  int i;
111541  Select *pNew;
111542  Select *pX;
111543  sqlite3 *db;
111544  struct ExprList_item *a;
111545  SrcList *pNewSrc;
111546  Parse *pParse;
111547  Token dummy;
111548
111549  if( p->pPrior==0 ) return WRC_Continue;
111550  if( p->pOrderBy==0 ) return WRC_Continue;
111551  for(pX=p; pX && (pX->op==TK_ALL || pX->op==TK_SELECT); pX=pX->pPrior){}
111552  if( pX==0 ) return WRC_Continue;
111553  a = p->pOrderBy->a;
111554  for(i=p->pOrderBy->nExpr-1; i>=0; i--){
111555    if( a[i].pExpr->flags & EP_Collate ) break;
111556  }
111557  if( i<0 ) return WRC_Continue;
111558
111559  /* If we reach this point, that means the transformation is required. */
111560
111561  pParse = pWalker->pParse;
111562  db = pParse->db;
111563  pNew = sqlite3DbMallocZero(db, sizeof(*pNew) );
111564  if( pNew==0 ) return WRC_Abort;
111565  memset(&dummy, 0, sizeof(dummy));
111566  pNewSrc = sqlite3SrcListAppendFromTerm(pParse,0,0,0,&dummy,pNew,0,0);
111567  if( pNewSrc==0 ) return WRC_Abort;
111568  *pNew = *p;
111569  p->pSrc = pNewSrc;
111570  p->pEList = sqlite3ExprListAppend(pParse, 0, sqlite3Expr(db, TK_ALL, 0));
111571  p->op = TK_SELECT;
111572  p->pWhere = 0;
111573  pNew->pGroupBy = 0;
111574  pNew->pHaving = 0;
111575  pNew->pOrderBy = 0;
111576  p->pPrior = 0;
111577  p->pNext = 0;
111578  p->pWith = 0;
111579  p->selFlags &= ~SF_Compound;
111580  assert( (p->selFlags & SF_Converted)==0 );
111581  p->selFlags |= SF_Converted;
111582  assert( pNew->pPrior!=0 );
111583  pNew->pPrior->pNext = pNew;
111584  pNew->pLimit = 0;
111585  pNew->pOffset = 0;
111586  return WRC_Continue;
111587}
111588
111589#ifndef SQLITE_OMIT_CTE
111590/*
111591** Argument pWith (which may be NULL) points to a linked list of nested
111592** WITH contexts, from inner to outermost. If the table identified by
111593** FROM clause element pItem is really a common-table-expression (CTE)
111594** then return a pointer to the CTE definition for that table. Otherwise
111595** return NULL.
111596**
111597** If a non-NULL value is returned, set *ppContext to point to the With
111598** object that the returned CTE belongs to.
111599*/
111600static struct Cte *searchWith(
111601  With *pWith,                    /* Current outermost WITH clause */
111602  struct SrcList_item *pItem,     /* FROM clause element to resolve */
111603  With **ppContext                /* OUT: WITH clause return value belongs to */
111604){
111605  const char *zName;
111606  if( pItem->zDatabase==0 && (zName = pItem->zName)!=0 ){
111607    With *p;
111608    for(p=pWith; p; p=p->pOuter){
111609      int i;
111610      for(i=0; i<p->nCte; i++){
111611        if( sqlite3StrICmp(zName, p->a[i].zName)==0 ){
111612          *ppContext = p;
111613          return &p->a[i];
111614        }
111615      }
111616    }
111617  }
111618  return 0;
111619}
111620
111621/* The code generator maintains a stack of active WITH clauses
111622** with the inner-most WITH clause being at the top of the stack.
111623**
111624** This routine pushes the WITH clause passed as the second argument
111625** onto the top of the stack. If argument bFree is true, then this
111626** WITH clause will never be popped from the stack. In this case it
111627** should be freed along with the Parse object. In other cases, when
111628** bFree==0, the With object will be freed along with the SELECT
111629** statement with which it is associated.
111630*/
111631SQLITE_PRIVATE void sqlite3WithPush(Parse *pParse, With *pWith, u8 bFree){
111632  assert( bFree==0 || pParse->pWith==0 );
111633  if( pWith ){
111634    pWith->pOuter = pParse->pWith;
111635    pParse->pWith = pWith;
111636    pParse->bFreeWith = bFree;
111637  }
111638}
111639
111640/*
111641** This function checks if argument pFrom refers to a CTE declared by
111642** a WITH clause on the stack currently maintained by the parser. And,
111643** if currently processing a CTE expression, if it is a recursive
111644** reference to the current CTE.
111645**
111646** If pFrom falls into either of the two categories above, pFrom->pTab
111647** and other fields are populated accordingly. The caller should check
111648** (pFrom->pTab!=0) to determine whether or not a successful match
111649** was found.
111650**
111651** Whether or not a match is found, SQLITE_OK is returned if no error
111652** occurs. If an error does occur, an error message is stored in the
111653** parser and some error code other than SQLITE_OK returned.
111654*/
111655static int withExpand(
111656  Walker *pWalker,
111657  struct SrcList_item *pFrom
111658){
111659  Parse *pParse = pWalker->pParse;
111660  sqlite3 *db = pParse->db;
111661  struct Cte *pCte;               /* Matched CTE (or NULL if no match) */
111662  With *pWith;                    /* WITH clause that pCte belongs to */
111663
111664  assert( pFrom->pTab==0 );
111665
111666  pCte = searchWith(pParse->pWith, pFrom, &pWith);
111667  if( pCte ){
111668    Table *pTab;
111669    ExprList *pEList;
111670    Select *pSel;
111671    Select *pLeft;                /* Left-most SELECT statement */
111672    int bMayRecursive;            /* True if compound joined by UNION [ALL] */
111673    With *pSavedWith;             /* Initial value of pParse->pWith */
111674
111675    /* If pCte->zErr is non-NULL at this point, then this is an illegal
111676    ** recursive reference to CTE pCte. Leave an error in pParse and return
111677    ** early. If pCte->zErr is NULL, then this is not a recursive reference.
111678    ** In this case, proceed.  */
111679    if( pCte->zErr ){
111680      sqlite3ErrorMsg(pParse, pCte->zErr, pCte->zName);
111681      return SQLITE_ERROR;
111682    }
111683
111684    assert( pFrom->pTab==0 );
111685    pFrom->pTab = pTab = sqlite3DbMallocZero(db, sizeof(Table));
111686    if( pTab==0 ) return WRC_Abort;
111687    pTab->nRef = 1;
111688    pTab->zName = sqlite3DbStrDup(db, pCte->zName);
111689    pTab->iPKey = -1;
111690    pTab->nRowLogEst = 200; assert( 200==sqlite3LogEst(1048576) );
111691    pTab->tabFlags |= TF_Ephemeral | TF_NoVisibleRowid;
111692    pFrom->pSelect = sqlite3SelectDup(db, pCte->pSelect, 0);
111693    if( db->mallocFailed ) return SQLITE_NOMEM;
111694    assert( pFrom->pSelect );
111695
111696    /* Check if this is a recursive CTE. */
111697    pSel = pFrom->pSelect;
111698    bMayRecursive = ( pSel->op==TK_ALL || pSel->op==TK_UNION );
111699    if( bMayRecursive ){
111700      int i;
111701      SrcList *pSrc = pFrom->pSelect->pSrc;
111702      for(i=0; i<pSrc->nSrc; i++){
111703        struct SrcList_item *pItem = &pSrc->a[i];
111704        if( pItem->zDatabase==0
111705         && pItem->zName!=0
111706         && 0==sqlite3StrICmp(pItem->zName, pCte->zName)
111707          ){
111708          pItem->pTab = pTab;
111709          pItem->isRecursive = 1;
111710          pTab->nRef++;
111711          pSel->selFlags |= SF_Recursive;
111712        }
111713      }
111714    }
111715
111716    /* Only one recursive reference is permitted. */
111717    if( pTab->nRef>2 ){
111718      sqlite3ErrorMsg(
111719          pParse, "multiple references to recursive table: %s", pCte->zName
111720      );
111721      return SQLITE_ERROR;
111722    }
111723    assert( pTab->nRef==1 || ((pSel->selFlags&SF_Recursive) && pTab->nRef==2 ));
111724
111725    pCte->zErr = "circular reference: %s";
111726    pSavedWith = pParse->pWith;
111727    pParse->pWith = pWith;
111728    sqlite3WalkSelect(pWalker, bMayRecursive ? pSel->pPrior : pSel);
111729
111730    for(pLeft=pSel; pLeft->pPrior; pLeft=pLeft->pPrior);
111731    pEList = pLeft->pEList;
111732    if( pCte->pCols ){
111733      if( pEList && pEList->nExpr!=pCte->pCols->nExpr ){
111734        sqlite3ErrorMsg(pParse, "table %s has %d values for %d columns",
111735            pCte->zName, pEList->nExpr, pCte->pCols->nExpr
111736        );
111737        pParse->pWith = pSavedWith;
111738        return SQLITE_ERROR;
111739      }
111740      pEList = pCte->pCols;
111741    }
111742
111743    selectColumnsFromExprList(pParse, pEList, &pTab->nCol, &pTab->aCol);
111744    if( bMayRecursive ){
111745      if( pSel->selFlags & SF_Recursive ){
111746        pCte->zErr = "multiple recursive references: %s";
111747      }else{
111748        pCte->zErr = "recursive reference in a subquery: %s";
111749      }
111750      sqlite3WalkSelect(pWalker, pSel);
111751    }
111752    pCte->zErr = 0;
111753    pParse->pWith = pSavedWith;
111754  }
111755
111756  return SQLITE_OK;
111757}
111758#endif
111759
111760#ifndef SQLITE_OMIT_CTE
111761/*
111762** If the SELECT passed as the second argument has an associated WITH
111763** clause, pop it from the stack stored as part of the Parse object.
111764**
111765** This function is used as the xSelectCallback2() callback by
111766** sqlite3SelectExpand() when walking a SELECT tree to resolve table
111767** names and other FROM clause elements.
111768*/
111769static void selectPopWith(Walker *pWalker, Select *p){
111770  Parse *pParse = pWalker->pParse;
111771  With *pWith = findRightmost(p)->pWith;
111772  if( pWith!=0 ){
111773    assert( pParse->pWith==pWith );
111774    pParse->pWith = pWith->pOuter;
111775  }
111776}
111777#else
111778#define selectPopWith 0
111779#endif
111780
111781/*
111782** This routine is a Walker callback for "expanding" a SELECT statement.
111783** "Expanding" means to do the following:
111784**
111785**    (1)  Make sure VDBE cursor numbers have been assigned to every
111786**         element of the FROM clause.
111787**
111788**    (2)  Fill in the pTabList->a[].pTab fields in the SrcList that
111789**         defines FROM clause.  When views appear in the FROM clause,
111790**         fill pTabList->a[].pSelect with a copy of the SELECT statement
111791**         that implements the view.  A copy is made of the view's SELECT
111792**         statement so that we can freely modify or delete that statement
111793**         without worrying about messing up the persistent representation
111794**         of the view.
111795**
111796**    (3)  Add terms to the WHERE clause to accommodate the NATURAL keyword
111797**         on joins and the ON and USING clause of joins.
111798**
111799**    (4)  Scan the list of columns in the result set (pEList) looking
111800**         for instances of the "*" operator or the TABLE.* operator.
111801**         If found, expand each "*" to be every column in every table
111802**         and TABLE.* to be every column in TABLE.
111803**
111804*/
111805static int selectExpander(Walker *pWalker, Select *p){
111806  Parse *pParse = pWalker->pParse;
111807  int i, j, k;
111808  SrcList *pTabList;
111809  ExprList *pEList;
111810  struct SrcList_item *pFrom;
111811  sqlite3 *db = pParse->db;
111812  Expr *pE, *pRight, *pExpr;
111813  u16 selFlags = p->selFlags;
111814
111815  p->selFlags |= SF_Expanded;
111816  if( db->mallocFailed  ){
111817    return WRC_Abort;
111818  }
111819  if( NEVER(p->pSrc==0) || (selFlags & SF_Expanded)!=0 ){
111820    return WRC_Prune;
111821  }
111822  pTabList = p->pSrc;
111823  pEList = p->pEList;
111824  if( pWalker->xSelectCallback2==selectPopWith ){
111825    sqlite3WithPush(pParse, findRightmost(p)->pWith, 0);
111826  }
111827
111828  /* Make sure cursor numbers have been assigned to all entries in
111829  ** the FROM clause of the SELECT statement.
111830  */
111831  sqlite3SrcListAssignCursors(pParse, pTabList);
111832
111833  /* Look up every table named in the FROM clause of the select.  If
111834  ** an entry of the FROM clause is a subquery instead of a table or view,
111835  ** then create a transient table structure to describe the subquery.
111836  */
111837  for(i=0, pFrom=pTabList->a; i<pTabList->nSrc; i++, pFrom++){
111838    Table *pTab;
111839    assert( pFrom->isRecursive==0 || pFrom->pTab );
111840    if( pFrom->isRecursive ) continue;
111841    if( pFrom->pTab!=0 ){
111842      /* This statement has already been prepared.  There is no need
111843      ** to go further. */
111844      assert( i==0 );
111845#ifndef SQLITE_OMIT_CTE
111846      selectPopWith(pWalker, p);
111847#endif
111848      return WRC_Prune;
111849    }
111850#ifndef SQLITE_OMIT_CTE
111851    if( withExpand(pWalker, pFrom) ) return WRC_Abort;
111852    if( pFrom->pTab ) {} else
111853#endif
111854    if( pFrom->zName==0 ){
111855#ifndef SQLITE_OMIT_SUBQUERY
111856      Select *pSel = pFrom->pSelect;
111857      /* A sub-query in the FROM clause of a SELECT */
111858      assert( pSel!=0 );
111859      assert( pFrom->pTab==0 );
111860      if( sqlite3WalkSelect(pWalker, pSel) ) return WRC_Abort;
111861      pFrom->pTab = pTab = sqlite3DbMallocZero(db, sizeof(Table));
111862      if( pTab==0 ) return WRC_Abort;
111863      pTab->nRef = 1;
111864      pTab->zName = sqlite3MPrintf(db, "sqlite_sq_%p", (void*)pTab);
111865      while( pSel->pPrior ){ pSel = pSel->pPrior; }
111866      selectColumnsFromExprList(pParse, pSel->pEList, &pTab->nCol, &pTab->aCol);
111867      pTab->iPKey = -1;
111868      pTab->nRowLogEst = 200; assert( 200==sqlite3LogEst(1048576) );
111869      pTab->tabFlags |= TF_Ephemeral;
111870#endif
111871    }else{
111872      /* An ordinary table or view name in the FROM clause */
111873      assert( pFrom->pTab==0 );
111874      pFrom->pTab = pTab = sqlite3LocateTableItem(pParse, 0, pFrom);
111875      if( pTab==0 ) return WRC_Abort;
111876      if( pTab->nRef==0xffff ){
111877        sqlite3ErrorMsg(pParse, "too many references to \"%s\": max 65535",
111878           pTab->zName);
111879        pFrom->pTab = 0;
111880        return WRC_Abort;
111881      }
111882      pTab->nRef++;
111883#if !defined(SQLITE_OMIT_VIEW) || !defined (SQLITE_OMIT_VIRTUALTABLE)
111884      if( pTab->pSelect || IsVirtual(pTab) ){
111885        /* We reach here if the named table is a really a view */
111886        if( sqlite3ViewGetColumnNames(pParse, pTab) ) return WRC_Abort;
111887        assert( pFrom->pSelect==0 );
111888        pFrom->pSelect = sqlite3SelectDup(db, pTab->pSelect, 0);
111889        sqlite3SelectSetName(pFrom->pSelect, pTab->zName);
111890        sqlite3WalkSelect(pWalker, pFrom->pSelect);
111891      }
111892#endif
111893    }
111894
111895    /* Locate the index named by the INDEXED BY clause, if any. */
111896    if( sqlite3IndexedByLookup(pParse, pFrom) ){
111897      return WRC_Abort;
111898    }
111899  }
111900
111901  /* Process NATURAL keywords, and ON and USING clauses of joins.
111902  */
111903  if( db->mallocFailed || sqliteProcessJoin(pParse, p) ){
111904    return WRC_Abort;
111905  }
111906
111907  /* For every "*" that occurs in the column list, insert the names of
111908  ** all columns in all tables.  And for every TABLE.* insert the names
111909  ** of all columns in TABLE.  The parser inserted a special expression
111910  ** with the TK_ALL operator for each "*" that it found in the column list.
111911  ** The following code just has to locate the TK_ALL expressions and expand
111912  ** each one to the list of all columns in all tables.
111913  **
111914  ** The first loop just checks to see if there are any "*" operators
111915  ** that need expanding.
111916  */
111917  for(k=0; k<pEList->nExpr; k++){
111918    pE = pEList->a[k].pExpr;
111919    if( pE->op==TK_ALL ) break;
111920    assert( pE->op!=TK_DOT || pE->pRight!=0 );
111921    assert( pE->op!=TK_DOT || (pE->pLeft!=0 && pE->pLeft->op==TK_ID) );
111922    if( pE->op==TK_DOT && pE->pRight->op==TK_ALL ) break;
111923  }
111924  if( k<pEList->nExpr ){
111925    /*
111926    ** If we get here it means the result set contains one or more "*"
111927    ** operators that need to be expanded.  Loop through each expression
111928    ** in the result set and expand them one by one.
111929    */
111930    struct ExprList_item *a = pEList->a;
111931    ExprList *pNew = 0;
111932    int flags = pParse->db->flags;
111933    int longNames = (flags & SQLITE_FullColNames)!=0
111934                      && (flags & SQLITE_ShortColNames)==0;
111935
111936    for(k=0; k<pEList->nExpr; k++){
111937      pE = a[k].pExpr;
111938      pRight = pE->pRight;
111939      assert( pE->op!=TK_DOT || pRight!=0 );
111940      if( pE->op!=TK_ALL && (pE->op!=TK_DOT || pRight->op!=TK_ALL) ){
111941        /* This particular expression does not need to be expanded.
111942        */
111943        pNew = sqlite3ExprListAppend(pParse, pNew, a[k].pExpr);
111944        if( pNew ){
111945          pNew->a[pNew->nExpr-1].zName = a[k].zName;
111946          pNew->a[pNew->nExpr-1].zSpan = a[k].zSpan;
111947          a[k].zName = 0;
111948          a[k].zSpan = 0;
111949        }
111950        a[k].pExpr = 0;
111951      }else{
111952        /* This expression is a "*" or a "TABLE.*" and needs to be
111953        ** expanded. */
111954        int tableSeen = 0;      /* Set to 1 when TABLE matches */
111955        char *zTName = 0;       /* text of name of TABLE */
111956        if( pE->op==TK_DOT ){
111957          assert( pE->pLeft!=0 );
111958          assert( !ExprHasProperty(pE->pLeft, EP_IntValue) );
111959          zTName = pE->pLeft->u.zToken;
111960        }
111961        for(i=0, pFrom=pTabList->a; i<pTabList->nSrc; i++, pFrom++){
111962          Table *pTab = pFrom->pTab;
111963          Select *pSub = pFrom->pSelect;
111964          char *zTabName = pFrom->zAlias;
111965          const char *zSchemaName = 0;
111966          int iDb;
111967          if( zTabName==0 ){
111968            zTabName = pTab->zName;
111969          }
111970          if( db->mallocFailed ) break;
111971          if( pSub==0 || (pSub->selFlags & SF_NestedFrom)==0 ){
111972            pSub = 0;
111973            if( zTName && sqlite3StrICmp(zTName, zTabName)!=0 ){
111974              continue;
111975            }
111976            iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
111977            zSchemaName = iDb>=0 ? db->aDb[iDb].zName : "*";
111978          }
111979          for(j=0; j<pTab->nCol; j++){
111980            char *zName = pTab->aCol[j].zName;
111981            char *zColname;  /* The computed column name */
111982            char *zToFree;   /* Malloced string that needs to be freed */
111983            Token sColname;  /* Computed column name as a token */
111984
111985            assert( zName );
111986            if( zTName && pSub
111987             && sqlite3MatchSpanName(pSub->pEList->a[j].zSpan, 0, zTName, 0)==0
111988            ){
111989              continue;
111990            }
111991
111992            /* If a column is marked as 'hidden' (currently only possible
111993            ** for virtual tables), do not include it in the expanded
111994            ** result-set list.
111995            */
111996            if( IsHiddenColumn(&pTab->aCol[j]) ){
111997              assert(IsVirtual(pTab));
111998              continue;
111999            }
112000            tableSeen = 1;
112001
112002            if( i>0 && zTName==0 ){
112003              if( (pFrom->jointype & JT_NATURAL)!=0
112004                && tableAndColumnIndex(pTabList, i, zName, 0, 0)
112005              ){
112006                /* In a NATURAL join, omit the join columns from the
112007                ** table to the right of the join */
112008                continue;
112009              }
112010              if( sqlite3IdListIndex(pFrom->pUsing, zName)>=0 ){
112011                /* In a join with a USING clause, omit columns in the
112012                ** using clause from the table on the right. */
112013                continue;
112014              }
112015            }
112016            pRight = sqlite3Expr(db, TK_ID, zName);
112017            zColname = zName;
112018            zToFree = 0;
112019            if( longNames || pTabList->nSrc>1 ){
112020              Expr *pLeft;
112021              pLeft = sqlite3Expr(db, TK_ID, zTabName);
112022              pExpr = sqlite3PExpr(pParse, TK_DOT, pLeft, pRight, 0);
112023              if( zSchemaName ){
112024                pLeft = sqlite3Expr(db, TK_ID, zSchemaName);
112025                pExpr = sqlite3PExpr(pParse, TK_DOT, pLeft, pExpr, 0);
112026              }
112027              if( longNames ){
112028                zColname = sqlite3MPrintf(db, "%s.%s", zTabName, zName);
112029                zToFree = zColname;
112030              }
112031            }else{
112032              pExpr = pRight;
112033            }
112034            pNew = sqlite3ExprListAppend(pParse, pNew, pExpr);
112035            sColname.z = zColname;
112036            sColname.n = sqlite3Strlen30(zColname);
112037            sqlite3ExprListSetName(pParse, pNew, &sColname, 0);
112038            if( pNew && (p->selFlags & SF_NestedFrom)!=0 ){
112039              struct ExprList_item *pX = &pNew->a[pNew->nExpr-1];
112040              if( pSub ){
112041                pX->zSpan = sqlite3DbStrDup(db, pSub->pEList->a[j].zSpan);
112042                testcase( pX->zSpan==0 );
112043              }else{
112044                pX->zSpan = sqlite3MPrintf(db, "%s.%s.%s",
112045                                           zSchemaName, zTabName, zColname);
112046                testcase( pX->zSpan==0 );
112047              }
112048              pX->bSpanIsTab = 1;
112049            }
112050            sqlite3DbFree(db, zToFree);
112051          }
112052        }
112053        if( !tableSeen ){
112054          if( zTName ){
112055            sqlite3ErrorMsg(pParse, "no such table: %s", zTName);
112056          }else{
112057            sqlite3ErrorMsg(pParse, "no tables specified");
112058          }
112059        }
112060      }
112061    }
112062    sqlite3ExprListDelete(db, pEList);
112063    p->pEList = pNew;
112064  }
112065#if SQLITE_MAX_COLUMN
112066  if( p->pEList && p->pEList->nExpr>db->aLimit[SQLITE_LIMIT_COLUMN] ){
112067    sqlite3ErrorMsg(pParse, "too many columns in result set");
112068  }
112069#endif
112070  return WRC_Continue;
112071}
112072
112073/*
112074** No-op routine for the parse-tree walker.
112075**
112076** When this routine is the Walker.xExprCallback then expression trees
112077** are walked without any actions being taken at each node.  Presumably,
112078** when this routine is used for Walker.xExprCallback then
112079** Walker.xSelectCallback is set to do something useful for every
112080** subquery in the parser tree.
112081*/
112082static int exprWalkNoop(Walker *NotUsed, Expr *NotUsed2){
112083  UNUSED_PARAMETER2(NotUsed, NotUsed2);
112084  return WRC_Continue;
112085}
112086
112087/*
112088** This routine "expands" a SELECT statement and all of its subqueries.
112089** For additional information on what it means to "expand" a SELECT
112090** statement, see the comment on the selectExpand worker callback above.
112091**
112092** Expanding a SELECT statement is the first step in processing a
112093** SELECT statement.  The SELECT statement must be expanded before
112094** name resolution is performed.
112095**
112096** If anything goes wrong, an error message is written into pParse.
112097** The calling function can detect the problem by looking at pParse->nErr
112098** and/or pParse->db->mallocFailed.
112099*/
112100static void sqlite3SelectExpand(Parse *pParse, Select *pSelect){
112101  Walker w;
112102  memset(&w, 0, sizeof(w));
112103  w.xExprCallback = exprWalkNoop;
112104  w.pParse = pParse;
112105  if( pParse->hasCompound ){
112106    w.xSelectCallback = convertCompoundSelectToSubquery;
112107    sqlite3WalkSelect(&w, pSelect);
112108  }
112109  w.xSelectCallback = selectExpander;
112110  if( (pSelect->selFlags & SF_MultiValue)==0 ){
112111    w.xSelectCallback2 = selectPopWith;
112112  }
112113  sqlite3WalkSelect(&w, pSelect);
112114}
112115
112116
112117#ifndef SQLITE_OMIT_SUBQUERY
112118/*
112119** This is a Walker.xSelectCallback callback for the sqlite3SelectTypeInfo()
112120** interface.
112121**
112122** For each FROM-clause subquery, add Column.zType and Column.zColl
112123** information to the Table structure that represents the result set
112124** of that subquery.
112125**
112126** The Table structure that represents the result set was constructed
112127** by selectExpander() but the type and collation information was omitted
112128** at that point because identifiers had not yet been resolved.  This
112129** routine is called after identifier resolution.
112130*/
112131static void selectAddSubqueryTypeInfo(Walker *pWalker, Select *p){
112132  Parse *pParse;
112133  int i;
112134  SrcList *pTabList;
112135  struct SrcList_item *pFrom;
112136
112137  assert( p->selFlags & SF_Resolved );
112138  if( (p->selFlags & SF_HasTypeInfo)==0 ){
112139    p->selFlags |= SF_HasTypeInfo;
112140    pParse = pWalker->pParse;
112141    pTabList = p->pSrc;
112142    for(i=0, pFrom=pTabList->a; i<pTabList->nSrc; i++, pFrom++){
112143      Table *pTab = pFrom->pTab;
112144      if( ALWAYS(pTab!=0) && (pTab->tabFlags & TF_Ephemeral)!=0 ){
112145        /* A sub-query in the FROM clause of a SELECT */
112146        Select *pSel = pFrom->pSelect;
112147        if( pSel ){
112148          while( pSel->pPrior ) pSel = pSel->pPrior;
112149          selectAddColumnTypeAndCollation(pParse, pTab, pSel);
112150        }
112151      }
112152    }
112153  }
112154}
112155#endif
112156
112157
112158/*
112159** This routine adds datatype and collating sequence information to
112160** the Table structures of all FROM-clause subqueries in a
112161** SELECT statement.
112162**
112163** Use this routine after name resolution.
112164*/
112165static void sqlite3SelectAddTypeInfo(Parse *pParse, Select *pSelect){
112166#ifndef SQLITE_OMIT_SUBQUERY
112167  Walker w;
112168  memset(&w, 0, sizeof(w));
112169  w.xSelectCallback2 = selectAddSubqueryTypeInfo;
112170  w.xExprCallback = exprWalkNoop;
112171  w.pParse = pParse;
112172  sqlite3WalkSelect(&w, pSelect);
112173#endif
112174}
112175
112176
112177/*
112178** This routine sets up a SELECT statement for processing.  The
112179** following is accomplished:
112180**
112181**     *  VDBE Cursor numbers are assigned to all FROM-clause terms.
112182**     *  Ephemeral Table objects are created for all FROM-clause subqueries.
112183**     *  ON and USING clauses are shifted into WHERE statements
112184**     *  Wildcards "*" and "TABLE.*" in result sets are expanded.
112185**     *  Identifiers in expression are matched to tables.
112186**
112187** This routine acts recursively on all subqueries within the SELECT.
112188*/
112189SQLITE_PRIVATE void sqlite3SelectPrep(
112190  Parse *pParse,         /* The parser context */
112191  Select *p,             /* The SELECT statement being coded. */
112192  NameContext *pOuterNC  /* Name context for container */
112193){
112194  sqlite3 *db;
112195  if( NEVER(p==0) ) return;
112196  db = pParse->db;
112197  if( db->mallocFailed ) return;
112198  if( p->selFlags & SF_HasTypeInfo ) return;
112199  sqlite3SelectExpand(pParse, p);
112200  if( pParse->nErr || db->mallocFailed ) return;
112201  sqlite3ResolveSelectNames(pParse, p, pOuterNC);
112202  if( pParse->nErr || db->mallocFailed ) return;
112203  sqlite3SelectAddTypeInfo(pParse, p);
112204}
112205
112206/*
112207** Reset the aggregate accumulator.
112208**
112209** The aggregate accumulator is a set of memory cells that hold
112210** intermediate results while calculating an aggregate.  This
112211** routine generates code that stores NULLs in all of those memory
112212** cells.
112213*/
112214static void resetAccumulator(Parse *pParse, AggInfo *pAggInfo){
112215  Vdbe *v = pParse->pVdbe;
112216  int i;
112217  struct AggInfo_func *pFunc;
112218  int nReg = pAggInfo->nFunc + pAggInfo->nColumn;
112219  if( nReg==0 ) return;
112220#ifdef SQLITE_DEBUG
112221  /* Verify that all AggInfo registers are within the range specified by
112222  ** AggInfo.mnReg..AggInfo.mxReg */
112223  assert( nReg==pAggInfo->mxReg-pAggInfo->mnReg+1 );
112224  for(i=0; i<pAggInfo->nColumn; i++){
112225    assert( pAggInfo->aCol[i].iMem>=pAggInfo->mnReg
112226         && pAggInfo->aCol[i].iMem<=pAggInfo->mxReg );
112227  }
112228  for(i=0; i<pAggInfo->nFunc; i++){
112229    assert( pAggInfo->aFunc[i].iMem>=pAggInfo->mnReg
112230         && pAggInfo->aFunc[i].iMem<=pAggInfo->mxReg );
112231  }
112232#endif
112233  sqlite3VdbeAddOp3(v, OP_Null, 0, pAggInfo->mnReg, pAggInfo->mxReg);
112234  for(pFunc=pAggInfo->aFunc, i=0; i<pAggInfo->nFunc; i++, pFunc++){
112235    if( pFunc->iDistinct>=0 ){
112236      Expr *pE = pFunc->pExpr;
112237      assert( !ExprHasProperty(pE, EP_xIsSelect) );
112238      if( pE->x.pList==0 || pE->x.pList->nExpr!=1 ){
112239        sqlite3ErrorMsg(pParse, "DISTINCT aggregates must have exactly one "
112240           "argument");
112241        pFunc->iDistinct = -1;
112242      }else{
112243        KeyInfo *pKeyInfo = keyInfoFromExprList(pParse, pE->x.pList, 0, 0);
112244        sqlite3VdbeAddOp4(v, OP_OpenEphemeral, pFunc->iDistinct, 0, 0,
112245                          (char*)pKeyInfo, P4_KEYINFO);
112246      }
112247    }
112248  }
112249}
112250
112251/*
112252** Invoke the OP_AggFinalize opcode for every aggregate function
112253** in the AggInfo structure.
112254*/
112255static void finalizeAggFunctions(Parse *pParse, AggInfo *pAggInfo){
112256  Vdbe *v = pParse->pVdbe;
112257  int i;
112258  struct AggInfo_func *pF;
112259  for(i=0, pF=pAggInfo->aFunc; i<pAggInfo->nFunc; i++, pF++){
112260    ExprList *pList = pF->pExpr->x.pList;
112261    assert( !ExprHasProperty(pF->pExpr, EP_xIsSelect) );
112262    sqlite3VdbeAddOp4(v, OP_AggFinal, pF->iMem, pList ? pList->nExpr : 0, 0,
112263                      (void*)pF->pFunc, P4_FUNCDEF);
112264  }
112265}
112266
112267/*
112268** Update the accumulator memory cells for an aggregate based on
112269** the current cursor position.
112270*/
112271static void updateAccumulator(Parse *pParse, AggInfo *pAggInfo){
112272  Vdbe *v = pParse->pVdbe;
112273  int i;
112274  int regHit = 0;
112275  int addrHitTest = 0;
112276  struct AggInfo_func *pF;
112277  struct AggInfo_col *pC;
112278
112279  pAggInfo->directMode = 1;
112280  for(i=0, pF=pAggInfo->aFunc; i<pAggInfo->nFunc; i++, pF++){
112281    int nArg;
112282    int addrNext = 0;
112283    int regAgg;
112284    ExprList *pList = pF->pExpr->x.pList;
112285    assert( !ExprHasProperty(pF->pExpr, EP_xIsSelect) );
112286    if( pList ){
112287      nArg = pList->nExpr;
112288      regAgg = sqlite3GetTempRange(pParse, nArg);
112289      sqlite3ExprCodeExprList(pParse, pList, regAgg, SQLITE_ECEL_DUP);
112290    }else{
112291      nArg = 0;
112292      regAgg = 0;
112293    }
112294    if( pF->iDistinct>=0 ){
112295      addrNext = sqlite3VdbeMakeLabel(v);
112296      testcase( nArg==0 );  /* Error condition */
112297      testcase( nArg>1 );   /* Also an error */
112298      codeDistinct(pParse, pF->iDistinct, addrNext, 1, regAgg);
112299    }
112300    if( pF->pFunc->funcFlags & SQLITE_FUNC_NEEDCOLL ){
112301      CollSeq *pColl = 0;
112302      struct ExprList_item *pItem;
112303      int j;
112304      assert( pList!=0 );  /* pList!=0 if pF->pFunc has NEEDCOLL */
112305      for(j=0, pItem=pList->a; !pColl && j<nArg; j++, pItem++){
112306        pColl = sqlite3ExprCollSeq(pParse, pItem->pExpr);
112307      }
112308      if( !pColl ){
112309        pColl = pParse->db->pDfltColl;
112310      }
112311      if( regHit==0 && pAggInfo->nAccumulator ) regHit = ++pParse->nMem;
112312      sqlite3VdbeAddOp4(v, OP_CollSeq, regHit, 0, 0, (char *)pColl, P4_COLLSEQ);
112313    }
112314    sqlite3VdbeAddOp4(v, OP_AggStep0, 0, regAgg, pF->iMem,
112315                      (void*)pF->pFunc, P4_FUNCDEF);
112316    sqlite3VdbeChangeP5(v, (u8)nArg);
112317    sqlite3ExprCacheAffinityChange(pParse, regAgg, nArg);
112318    sqlite3ReleaseTempRange(pParse, regAgg, nArg);
112319    if( addrNext ){
112320      sqlite3VdbeResolveLabel(v, addrNext);
112321      sqlite3ExprCacheClear(pParse);
112322    }
112323  }
112324
112325  /* Before populating the accumulator registers, clear the column cache.
112326  ** Otherwise, if any of the required column values are already present
112327  ** in registers, sqlite3ExprCode() may use OP_SCopy to copy the value
112328  ** to pC->iMem. But by the time the value is used, the original register
112329  ** may have been used, invalidating the underlying buffer holding the
112330  ** text or blob value. See ticket [883034dcb5].
112331  **
112332  ** Another solution would be to change the OP_SCopy used to copy cached
112333  ** values to an OP_Copy.
112334  */
112335  if( regHit ){
112336    addrHitTest = sqlite3VdbeAddOp1(v, OP_If, regHit); VdbeCoverage(v);
112337  }
112338  sqlite3ExprCacheClear(pParse);
112339  for(i=0, pC=pAggInfo->aCol; i<pAggInfo->nAccumulator; i++, pC++){
112340    sqlite3ExprCode(pParse, pC->pExpr, pC->iMem);
112341  }
112342  pAggInfo->directMode = 0;
112343  sqlite3ExprCacheClear(pParse);
112344  if( addrHitTest ){
112345    sqlite3VdbeJumpHere(v, addrHitTest);
112346  }
112347}
112348
112349/*
112350** Add a single OP_Explain instruction to the VDBE to explain a simple
112351** count(*) query ("SELECT count(*) FROM pTab").
112352*/
112353#ifndef SQLITE_OMIT_EXPLAIN
112354static void explainSimpleCount(
112355  Parse *pParse,                  /* Parse context */
112356  Table *pTab,                    /* Table being queried */
112357  Index *pIdx                     /* Index used to optimize scan, or NULL */
112358){
112359  if( pParse->explain==2 ){
112360    int bCover = (pIdx!=0 && (HasRowid(pTab) || !IsPrimaryKeyIndex(pIdx)));
112361    char *zEqp = sqlite3MPrintf(pParse->db, "SCAN TABLE %s%s%s",
112362        pTab->zName,
112363        bCover ? " USING COVERING INDEX " : "",
112364        bCover ? pIdx->zName : ""
112365    );
112366    sqlite3VdbeAddOp4(
112367        pParse->pVdbe, OP_Explain, pParse->iSelectId, 0, 0, zEqp, P4_DYNAMIC
112368    );
112369  }
112370}
112371#else
112372# define explainSimpleCount(a,b,c)
112373#endif
112374
112375/*
112376** Generate code for the SELECT statement given in the p argument.
112377**
112378** The results are returned according to the SelectDest structure.
112379** See comments in sqliteInt.h for further information.
112380**
112381** This routine returns the number of errors.  If any errors are
112382** encountered, then an appropriate error message is left in
112383** pParse->zErrMsg.
112384**
112385** This routine does NOT free the Select structure passed in.  The
112386** calling function needs to do that.
112387*/
112388SQLITE_PRIVATE int sqlite3Select(
112389  Parse *pParse,         /* The parser context */
112390  Select *p,             /* The SELECT statement being coded. */
112391  SelectDest *pDest      /* What to do with the query results */
112392){
112393  int i, j;              /* Loop counters */
112394  WhereInfo *pWInfo;     /* Return from sqlite3WhereBegin() */
112395  Vdbe *v;               /* The virtual machine under construction */
112396  int isAgg;             /* True for select lists like "count(*)" */
112397  ExprList *pEList = 0;  /* List of columns to extract. */
112398  SrcList *pTabList;     /* List of tables to select from */
112399  Expr *pWhere;          /* The WHERE clause.  May be NULL */
112400  ExprList *pGroupBy;    /* The GROUP BY clause.  May be NULL */
112401  Expr *pHaving;         /* The HAVING clause.  May be NULL */
112402  int rc = 1;            /* Value to return from this function */
112403  DistinctCtx sDistinct; /* Info on how to code the DISTINCT keyword */
112404  SortCtx sSort;         /* Info on how to code the ORDER BY clause */
112405  AggInfo sAggInfo;      /* Information used by aggregate queries */
112406  int iEnd;              /* Address of the end of the query */
112407  sqlite3 *db;           /* The database connection */
112408
112409#ifndef SQLITE_OMIT_EXPLAIN
112410  int iRestoreSelectId = pParse->iSelectId;
112411  pParse->iSelectId = pParse->iNextSelectId++;
112412#endif
112413
112414  db = pParse->db;
112415  if( p==0 || db->mallocFailed || pParse->nErr ){
112416    return 1;
112417  }
112418  if( sqlite3AuthCheck(pParse, SQLITE_SELECT, 0, 0, 0) ) return 1;
112419  memset(&sAggInfo, 0, sizeof(sAggInfo));
112420#if SELECTTRACE_ENABLED
112421  pParse->nSelectIndent++;
112422  SELECTTRACE(1,pParse,p, ("begin processing:\n"));
112423  if( sqlite3SelectTrace & 0x100 ){
112424    sqlite3TreeViewSelect(0, p, 0);
112425  }
112426#endif
112427
112428  assert( p->pOrderBy==0 || pDest->eDest!=SRT_DistFifo );
112429  assert( p->pOrderBy==0 || pDest->eDest!=SRT_Fifo );
112430  assert( p->pOrderBy==0 || pDest->eDest!=SRT_DistQueue );
112431  assert( p->pOrderBy==0 || pDest->eDest!=SRT_Queue );
112432  if( IgnorableOrderby(pDest) ){
112433    assert(pDest->eDest==SRT_Exists || pDest->eDest==SRT_Union ||
112434           pDest->eDest==SRT_Except || pDest->eDest==SRT_Discard ||
112435           pDest->eDest==SRT_Queue  || pDest->eDest==SRT_DistFifo ||
112436           pDest->eDest==SRT_DistQueue || pDest->eDest==SRT_Fifo);
112437    /* If ORDER BY makes no difference in the output then neither does
112438    ** DISTINCT so it can be removed too. */
112439    sqlite3ExprListDelete(db, p->pOrderBy);
112440    p->pOrderBy = 0;
112441    p->selFlags &= ~SF_Distinct;
112442  }
112443  sqlite3SelectPrep(pParse, p, 0);
112444  memset(&sSort, 0, sizeof(sSort));
112445  sSort.pOrderBy = p->pOrderBy;
112446  pTabList = p->pSrc;
112447  if( pParse->nErr || db->mallocFailed ){
112448    goto select_end;
112449  }
112450  assert( p->pEList!=0 );
112451  isAgg = (p->selFlags & SF_Aggregate)!=0;
112452#if SELECTTRACE_ENABLED
112453  if( sqlite3SelectTrace & 0x100 ){
112454    SELECTTRACE(0x100,pParse,p, ("after name resolution:\n"));
112455    sqlite3TreeViewSelect(0, p, 0);
112456  }
112457#endif
112458
112459
112460  /* If writing to memory or generating a set
112461  ** only a single column may be output.
112462  */
112463#ifndef SQLITE_OMIT_SUBQUERY
112464  if( checkForMultiColumnSelectError(pParse, pDest, p->pEList->nExpr) ){
112465    goto select_end;
112466  }
112467#endif
112468
112469  /* Try to flatten subqueries in the FROM clause up into the main query
112470  */
112471#if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW)
112472  for(i=0; !p->pPrior && i<pTabList->nSrc; i++){
112473    struct SrcList_item *pItem = &pTabList->a[i];
112474    Select *pSub = pItem->pSelect;
112475    int isAggSub;
112476    if( pSub==0 ) continue;
112477    isAggSub = (pSub->selFlags & SF_Aggregate)!=0;
112478    if( flattenSubquery(pParse, p, i, isAgg, isAggSub) ){
112479      /* This subquery can be absorbed into its parent. */
112480      if( isAggSub ){
112481        isAgg = 1;
112482        p->selFlags |= SF_Aggregate;
112483      }
112484      i = -1;
112485    }
112486    pTabList = p->pSrc;
112487    if( db->mallocFailed ) goto select_end;
112488    if( !IgnorableOrderby(pDest) ){
112489      sSort.pOrderBy = p->pOrderBy;
112490    }
112491  }
112492#endif
112493
112494  /* Get a pointer the VDBE under construction, allocating a new VDBE if one
112495  ** does not already exist */
112496  v = sqlite3GetVdbe(pParse);
112497  if( v==0 ) goto select_end;
112498
112499#ifndef SQLITE_OMIT_COMPOUND_SELECT
112500  /* Handle compound SELECT statements using the separate multiSelect()
112501  ** procedure.
112502  */
112503  if( p->pPrior ){
112504    rc = multiSelect(pParse, p, pDest);
112505    explainSetInteger(pParse->iSelectId, iRestoreSelectId);
112506#if SELECTTRACE_ENABLED
112507    SELECTTRACE(1,pParse,p,("end compound-select processing\n"));
112508    pParse->nSelectIndent--;
112509#endif
112510    return rc;
112511  }
112512#endif
112513
112514  /* Generate code for all sub-queries in the FROM clause
112515  */
112516#if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW)
112517  for(i=0; i<pTabList->nSrc; i++){
112518    struct SrcList_item *pItem = &pTabList->a[i];
112519    SelectDest dest;
112520    Select *pSub = pItem->pSelect;
112521    if( pSub==0 ) continue;
112522
112523    /* Sometimes the code for a subquery will be generated more than
112524    ** once, if the subquery is part of the WHERE clause in a LEFT JOIN,
112525    ** for example.  In that case, do not regenerate the code to manifest
112526    ** a view or the co-routine to implement a view.  The first instance
112527    ** is sufficient, though the subroutine to manifest the view does need
112528    ** to be invoked again. */
112529    if( pItem->addrFillSub ){
112530      if( pItem->viaCoroutine==0 ){
112531        sqlite3VdbeAddOp2(v, OP_Gosub, pItem->regReturn, pItem->addrFillSub);
112532      }
112533      continue;
112534    }
112535
112536    /* Increment Parse.nHeight by the height of the largest expression
112537    ** tree referred to by this, the parent select. The child select
112538    ** may contain expression trees of at most
112539    ** (SQLITE_MAX_EXPR_DEPTH-Parse.nHeight) height. This is a bit
112540    ** more conservative than necessary, but much easier than enforcing
112541    ** an exact limit.
112542    */
112543    pParse->nHeight += sqlite3SelectExprHeight(p);
112544
112545    /* Make copies of constant WHERE-clause terms in the outer query down
112546    ** inside the subquery.  This can help the subquery to run more efficiently.
112547    */
112548    if( (pItem->jointype & JT_OUTER)==0
112549     && pushDownWhereTerms(db, pSub, p->pWhere, pItem->iCursor)
112550    ){
112551#if SELECTTRACE_ENABLED
112552      if( sqlite3SelectTrace & 0x100 ){
112553        SELECTTRACE(0x100,pParse,p,("After WHERE-clause push-down:\n"));
112554        sqlite3TreeViewSelect(0, p, 0);
112555      }
112556#endif
112557    }
112558
112559    /* Generate code to implement the subquery
112560    */
112561    if( pTabList->nSrc==1
112562     && (p->selFlags & SF_All)==0
112563     && OptimizationEnabled(db, SQLITE_SubqCoroutine)
112564    ){
112565      /* Implement a co-routine that will return a single row of the result
112566      ** set on each invocation.
112567      */
112568      int addrTop = sqlite3VdbeCurrentAddr(v)+1;
112569      pItem->regReturn = ++pParse->nMem;
112570      sqlite3VdbeAddOp3(v, OP_InitCoroutine, pItem->regReturn, 0, addrTop);
112571      VdbeComment((v, "%s", pItem->pTab->zName));
112572      pItem->addrFillSub = addrTop;
112573      sqlite3SelectDestInit(&dest, SRT_Coroutine, pItem->regReturn);
112574      explainSetInteger(pItem->iSelectId, (u8)pParse->iNextSelectId);
112575      sqlite3Select(pParse, pSub, &dest);
112576      pItem->pTab->nRowLogEst = sqlite3LogEst(pSub->nSelectRow);
112577      pItem->viaCoroutine = 1;
112578      pItem->regResult = dest.iSdst;
112579      sqlite3VdbeAddOp1(v, OP_EndCoroutine, pItem->regReturn);
112580      sqlite3VdbeJumpHere(v, addrTop-1);
112581      sqlite3ClearTempRegCache(pParse);
112582    }else{
112583      /* Generate a subroutine that will fill an ephemeral table with
112584      ** the content of this subquery.  pItem->addrFillSub will point
112585      ** to the address of the generated subroutine.  pItem->regReturn
112586      ** is a register allocated to hold the subroutine return address
112587      */
112588      int topAddr;
112589      int onceAddr = 0;
112590      int retAddr;
112591      assert( pItem->addrFillSub==0 );
112592      pItem->regReturn = ++pParse->nMem;
112593      topAddr = sqlite3VdbeAddOp2(v, OP_Integer, 0, pItem->regReturn);
112594      pItem->addrFillSub = topAddr+1;
112595      if( pItem->isCorrelated==0 ){
112596        /* If the subquery is not correlated and if we are not inside of
112597        ** a trigger, then we only need to compute the value of the subquery
112598        ** once. */
112599        onceAddr = sqlite3CodeOnce(pParse); VdbeCoverage(v);
112600        VdbeComment((v, "materialize \"%s\"", pItem->pTab->zName));
112601      }else{
112602        VdbeNoopComment((v, "materialize \"%s\"", pItem->pTab->zName));
112603      }
112604      sqlite3SelectDestInit(&dest, SRT_EphemTab, pItem->iCursor);
112605      explainSetInteger(pItem->iSelectId, (u8)pParse->iNextSelectId);
112606      sqlite3Select(pParse, pSub, &dest);
112607      pItem->pTab->nRowLogEst = sqlite3LogEst(pSub->nSelectRow);
112608      if( onceAddr ) sqlite3VdbeJumpHere(v, onceAddr);
112609      retAddr = sqlite3VdbeAddOp1(v, OP_Return, pItem->regReturn);
112610      VdbeComment((v, "end %s", pItem->pTab->zName));
112611      sqlite3VdbeChangeP1(v, topAddr, retAddr);
112612      sqlite3ClearTempRegCache(pParse);
112613    }
112614    if( db->mallocFailed ) goto select_end;
112615    pParse->nHeight -= sqlite3SelectExprHeight(p);
112616  }
112617#endif
112618
112619  /* Various elements of the SELECT copied into local variables for
112620  ** convenience */
112621  pEList = p->pEList;
112622  pWhere = p->pWhere;
112623  pGroupBy = p->pGroupBy;
112624  pHaving = p->pHaving;
112625  sDistinct.isTnct = (p->selFlags & SF_Distinct)!=0;
112626
112627#if SELECTTRACE_ENABLED
112628  if( sqlite3SelectTrace & 0x400 ){
112629    SELECTTRACE(0x400,pParse,p,("After all FROM-clause analysis:\n"));
112630    sqlite3TreeViewSelect(0, p, 0);
112631  }
112632#endif
112633
112634  /* If the query is DISTINCT with an ORDER BY but is not an aggregate, and
112635  ** if the select-list is the same as the ORDER BY list, then this query
112636  ** can be rewritten as a GROUP BY. In other words, this:
112637  **
112638  **     SELECT DISTINCT xyz FROM ... ORDER BY xyz
112639  **
112640  ** is transformed to:
112641  **
112642  **     SELECT xyz FROM ... GROUP BY xyz ORDER BY xyz
112643  **
112644  ** The second form is preferred as a single index (or temp-table) may be
112645  ** used for both the ORDER BY and DISTINCT processing. As originally
112646  ** written the query must use a temp-table for at least one of the ORDER
112647  ** BY and DISTINCT, and an index or separate temp-table for the other.
112648  */
112649  if( (p->selFlags & (SF_Distinct|SF_Aggregate))==SF_Distinct
112650   && sqlite3ExprListCompare(sSort.pOrderBy, pEList, -1)==0
112651  ){
112652    p->selFlags &= ~SF_Distinct;
112653    pGroupBy = p->pGroupBy = sqlite3ExprListDup(db, pEList, 0);
112654    /* Notice that even thought SF_Distinct has been cleared from p->selFlags,
112655    ** the sDistinct.isTnct is still set.  Hence, isTnct represents the
112656    ** original setting of the SF_Distinct flag, not the current setting */
112657    assert( sDistinct.isTnct );
112658  }
112659
112660  /* If there is an ORDER BY clause, then create an ephemeral index to
112661  ** do the sorting.  But this sorting ephemeral index might end up
112662  ** being unused if the data can be extracted in pre-sorted order.
112663  ** If that is the case, then the OP_OpenEphemeral instruction will be
112664  ** changed to an OP_Noop once we figure out that the sorting index is
112665  ** not needed.  The sSort.addrSortIndex variable is used to facilitate
112666  ** that change.
112667  */
112668  if( sSort.pOrderBy ){
112669    KeyInfo *pKeyInfo;
112670    pKeyInfo = keyInfoFromExprList(pParse, sSort.pOrderBy, 0, pEList->nExpr);
112671    sSort.iECursor = pParse->nTab++;
112672    sSort.addrSortIndex =
112673      sqlite3VdbeAddOp4(v, OP_OpenEphemeral,
112674          sSort.iECursor, sSort.pOrderBy->nExpr+1+pEList->nExpr, 0,
112675          (char*)pKeyInfo, P4_KEYINFO
112676      );
112677  }else{
112678    sSort.addrSortIndex = -1;
112679  }
112680
112681  /* If the output is destined for a temporary table, open that table.
112682  */
112683  if( pDest->eDest==SRT_EphemTab ){
112684    sqlite3VdbeAddOp2(v, OP_OpenEphemeral, pDest->iSDParm, pEList->nExpr);
112685  }
112686
112687  /* Set the limiter.
112688  */
112689  iEnd = sqlite3VdbeMakeLabel(v);
112690  p->nSelectRow = LARGEST_INT64;
112691  computeLimitRegisters(pParse, p, iEnd);
112692  if( p->iLimit==0 && sSort.addrSortIndex>=0 ){
112693    sqlite3VdbeGetOp(v, sSort.addrSortIndex)->opcode = OP_SorterOpen;
112694    sSort.sortFlags |= SORTFLAG_UseSorter;
112695  }
112696
112697  /* Open an ephemeral index to use for the distinct set.
112698  */
112699  if( p->selFlags & SF_Distinct ){
112700    sDistinct.tabTnct = pParse->nTab++;
112701    sDistinct.addrTnct = sqlite3VdbeAddOp4(v, OP_OpenEphemeral,
112702                             sDistinct.tabTnct, 0, 0,
112703                             (char*)keyInfoFromExprList(pParse, p->pEList,0,0),
112704                             P4_KEYINFO);
112705    sqlite3VdbeChangeP5(v, BTREE_UNORDERED);
112706    sDistinct.eTnctType = WHERE_DISTINCT_UNORDERED;
112707  }else{
112708    sDistinct.eTnctType = WHERE_DISTINCT_NOOP;
112709  }
112710
112711  if( !isAgg && pGroupBy==0 ){
112712    /* No aggregate functions and no GROUP BY clause */
112713    u16 wctrlFlags = (sDistinct.isTnct ? WHERE_WANT_DISTINCT : 0);
112714
112715    /* Begin the database scan. */
112716    pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, sSort.pOrderBy,
112717                               p->pEList, wctrlFlags, 0);
112718    if( pWInfo==0 ) goto select_end;
112719    if( sqlite3WhereOutputRowCount(pWInfo) < p->nSelectRow ){
112720      p->nSelectRow = sqlite3WhereOutputRowCount(pWInfo);
112721    }
112722    if( sDistinct.isTnct && sqlite3WhereIsDistinct(pWInfo) ){
112723      sDistinct.eTnctType = sqlite3WhereIsDistinct(pWInfo);
112724    }
112725    if( sSort.pOrderBy ){
112726      sSort.nOBSat = sqlite3WhereIsOrdered(pWInfo);
112727      if( sSort.nOBSat==sSort.pOrderBy->nExpr ){
112728        sSort.pOrderBy = 0;
112729      }
112730    }
112731
112732    /* If sorting index that was created by a prior OP_OpenEphemeral
112733    ** instruction ended up not being needed, then change the OP_OpenEphemeral
112734    ** into an OP_Noop.
112735    */
112736    if( sSort.addrSortIndex>=0 && sSort.pOrderBy==0 ){
112737      sqlite3VdbeChangeToNoop(v, sSort.addrSortIndex);
112738    }
112739
112740    /* Use the standard inner loop. */
112741    selectInnerLoop(pParse, p, pEList, -1, &sSort, &sDistinct, pDest,
112742                    sqlite3WhereContinueLabel(pWInfo),
112743                    sqlite3WhereBreakLabel(pWInfo));
112744
112745    /* End the database scan loop.
112746    */
112747    sqlite3WhereEnd(pWInfo);
112748  }else{
112749    /* This case when there exist aggregate functions or a GROUP BY clause
112750    ** or both */
112751    NameContext sNC;    /* Name context for processing aggregate information */
112752    int iAMem;          /* First Mem address for storing current GROUP BY */
112753    int iBMem;          /* First Mem address for previous GROUP BY */
112754    int iUseFlag;       /* Mem address holding flag indicating that at least
112755                        ** one row of the input to the aggregator has been
112756                        ** processed */
112757    int iAbortFlag;     /* Mem address which causes query abort if positive */
112758    int groupBySort;    /* Rows come from source in GROUP BY order */
112759    int addrEnd;        /* End of processing for this SELECT */
112760    int sortPTab = 0;   /* Pseudotable used to decode sorting results */
112761    int sortOut = 0;    /* Output register from the sorter */
112762    int orderByGrp = 0; /* True if the GROUP BY and ORDER BY are the same */
112763
112764    /* Remove any and all aliases between the result set and the
112765    ** GROUP BY clause.
112766    */
112767    if( pGroupBy ){
112768      int k;                        /* Loop counter */
112769      struct ExprList_item *pItem;  /* For looping over expression in a list */
112770
112771      for(k=p->pEList->nExpr, pItem=p->pEList->a; k>0; k--, pItem++){
112772        pItem->u.x.iAlias = 0;
112773      }
112774      for(k=pGroupBy->nExpr, pItem=pGroupBy->a; k>0; k--, pItem++){
112775        pItem->u.x.iAlias = 0;
112776      }
112777      if( p->nSelectRow>100 ) p->nSelectRow = 100;
112778    }else{
112779      p->nSelectRow = 1;
112780    }
112781
112782    /* If there is both a GROUP BY and an ORDER BY clause and they are
112783    ** identical, then it may be possible to disable the ORDER BY clause
112784    ** on the grounds that the GROUP BY will cause elements to come out
112785    ** in the correct order. It also may not - the GROUP BY might use a
112786    ** database index that causes rows to be grouped together as required
112787    ** but not actually sorted. Either way, record the fact that the
112788    ** ORDER BY and GROUP BY clauses are the same by setting the orderByGrp
112789    ** variable.  */
112790    if( sqlite3ExprListCompare(pGroupBy, sSort.pOrderBy, -1)==0 ){
112791      orderByGrp = 1;
112792    }
112793
112794    /* Create a label to jump to when we want to abort the query */
112795    addrEnd = sqlite3VdbeMakeLabel(v);
112796
112797    /* Convert TK_COLUMN nodes into TK_AGG_COLUMN and make entries in
112798    ** sAggInfo for all TK_AGG_FUNCTION nodes in expressions of the
112799    ** SELECT statement.
112800    */
112801    memset(&sNC, 0, sizeof(sNC));
112802    sNC.pParse = pParse;
112803    sNC.pSrcList = pTabList;
112804    sNC.pAggInfo = &sAggInfo;
112805    sAggInfo.mnReg = pParse->nMem+1;
112806    sAggInfo.nSortingColumn = pGroupBy ? pGroupBy->nExpr : 0;
112807    sAggInfo.pGroupBy = pGroupBy;
112808    sqlite3ExprAnalyzeAggList(&sNC, pEList);
112809    sqlite3ExprAnalyzeAggList(&sNC, sSort.pOrderBy);
112810    if( pHaving ){
112811      sqlite3ExprAnalyzeAggregates(&sNC, pHaving);
112812    }
112813    sAggInfo.nAccumulator = sAggInfo.nColumn;
112814    for(i=0; i<sAggInfo.nFunc; i++){
112815      assert( !ExprHasProperty(sAggInfo.aFunc[i].pExpr, EP_xIsSelect) );
112816      sNC.ncFlags |= NC_InAggFunc;
112817      sqlite3ExprAnalyzeAggList(&sNC, sAggInfo.aFunc[i].pExpr->x.pList);
112818      sNC.ncFlags &= ~NC_InAggFunc;
112819    }
112820    sAggInfo.mxReg = pParse->nMem;
112821    if( db->mallocFailed ) goto select_end;
112822
112823    /* Processing for aggregates with GROUP BY is very different and
112824    ** much more complex than aggregates without a GROUP BY.
112825    */
112826    if( pGroupBy ){
112827      KeyInfo *pKeyInfo;  /* Keying information for the group by clause */
112828      int j1;             /* A-vs-B comparision jump */
112829      int addrOutputRow;  /* Start of subroutine that outputs a result row */
112830      int regOutputRow;   /* Return address register for output subroutine */
112831      int addrSetAbort;   /* Set the abort flag and return */
112832      int addrTopOfLoop;  /* Top of the input loop */
112833      int addrSortingIdx; /* The OP_OpenEphemeral for the sorting index */
112834      int addrReset;      /* Subroutine for resetting the accumulator */
112835      int regReset;       /* Return address register for reset subroutine */
112836
112837      /* If there is a GROUP BY clause we might need a sorting index to
112838      ** implement it.  Allocate that sorting index now.  If it turns out
112839      ** that we do not need it after all, the OP_SorterOpen instruction
112840      ** will be converted into a Noop.
112841      */
112842      sAggInfo.sortingIdx = pParse->nTab++;
112843      pKeyInfo = keyInfoFromExprList(pParse, pGroupBy, 0, sAggInfo.nColumn);
112844      addrSortingIdx = sqlite3VdbeAddOp4(v, OP_SorterOpen,
112845          sAggInfo.sortingIdx, sAggInfo.nSortingColumn,
112846          0, (char*)pKeyInfo, P4_KEYINFO);
112847
112848      /* Initialize memory locations used by GROUP BY aggregate processing
112849      */
112850      iUseFlag = ++pParse->nMem;
112851      iAbortFlag = ++pParse->nMem;
112852      regOutputRow = ++pParse->nMem;
112853      addrOutputRow = sqlite3VdbeMakeLabel(v);
112854      regReset = ++pParse->nMem;
112855      addrReset = sqlite3VdbeMakeLabel(v);
112856      iAMem = pParse->nMem + 1;
112857      pParse->nMem += pGroupBy->nExpr;
112858      iBMem = pParse->nMem + 1;
112859      pParse->nMem += pGroupBy->nExpr;
112860      sqlite3VdbeAddOp2(v, OP_Integer, 0, iAbortFlag);
112861      VdbeComment((v, "clear abort flag"));
112862      sqlite3VdbeAddOp2(v, OP_Integer, 0, iUseFlag);
112863      VdbeComment((v, "indicate accumulator empty"));
112864      sqlite3VdbeAddOp3(v, OP_Null, 0, iAMem, iAMem+pGroupBy->nExpr-1);
112865
112866      /* Begin a loop that will extract all source rows in GROUP BY order.
112867      ** This might involve two separate loops with an OP_Sort in between, or
112868      ** it might be a single loop that uses an index to extract information
112869      ** in the right order to begin with.
112870      */
112871      sqlite3VdbeAddOp2(v, OP_Gosub, regReset, addrReset);
112872      pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, pGroupBy, 0,
112873          WHERE_GROUPBY | (orderByGrp ? WHERE_SORTBYGROUP : 0), 0
112874      );
112875      if( pWInfo==0 ) goto select_end;
112876      if( sqlite3WhereIsOrdered(pWInfo)==pGroupBy->nExpr ){
112877        /* The optimizer is able to deliver rows in group by order so
112878        ** we do not have to sort.  The OP_OpenEphemeral table will be
112879        ** cancelled later because we still need to use the pKeyInfo
112880        */
112881        groupBySort = 0;
112882      }else{
112883        /* Rows are coming out in undetermined order.  We have to push
112884        ** each row into a sorting index, terminate the first loop,
112885        ** then loop over the sorting index in order to get the output
112886        ** in sorted order
112887        */
112888        int regBase;
112889        int regRecord;
112890        int nCol;
112891        int nGroupBy;
112892
112893        explainTempTable(pParse,
112894            (sDistinct.isTnct && (p->selFlags&SF_Distinct)==0) ?
112895                    "DISTINCT" : "GROUP BY");
112896
112897        groupBySort = 1;
112898        nGroupBy = pGroupBy->nExpr;
112899        nCol = nGroupBy;
112900        j = nGroupBy;
112901        for(i=0; i<sAggInfo.nColumn; i++){
112902          if( sAggInfo.aCol[i].iSorterColumn>=j ){
112903            nCol++;
112904            j++;
112905          }
112906        }
112907        regBase = sqlite3GetTempRange(pParse, nCol);
112908        sqlite3ExprCacheClear(pParse);
112909        sqlite3ExprCodeExprList(pParse, pGroupBy, regBase, 0);
112910        j = nGroupBy;
112911        for(i=0; i<sAggInfo.nColumn; i++){
112912          struct AggInfo_col *pCol = &sAggInfo.aCol[i];
112913          if( pCol->iSorterColumn>=j ){
112914            int r1 = j + regBase;
112915            int r2;
112916
112917            r2 = sqlite3ExprCodeGetColumn(pParse,
112918                               pCol->pTab, pCol->iColumn, pCol->iTable, r1, 0);
112919            if( r1!=r2 ){
112920              sqlite3VdbeAddOp2(v, OP_SCopy, r2, r1);
112921            }
112922            j++;
112923          }
112924        }
112925        regRecord = sqlite3GetTempReg(pParse);
112926        sqlite3VdbeAddOp3(v, OP_MakeRecord, regBase, nCol, regRecord);
112927        sqlite3VdbeAddOp2(v, OP_SorterInsert, sAggInfo.sortingIdx, regRecord);
112928        sqlite3ReleaseTempReg(pParse, regRecord);
112929        sqlite3ReleaseTempRange(pParse, regBase, nCol);
112930        sqlite3WhereEnd(pWInfo);
112931        sAggInfo.sortingIdxPTab = sortPTab = pParse->nTab++;
112932        sortOut = sqlite3GetTempReg(pParse);
112933        sqlite3VdbeAddOp3(v, OP_OpenPseudo, sortPTab, sortOut, nCol);
112934        sqlite3VdbeAddOp2(v, OP_SorterSort, sAggInfo.sortingIdx, addrEnd);
112935        VdbeComment((v, "GROUP BY sort")); VdbeCoverage(v);
112936        sAggInfo.useSortingIdx = 1;
112937        sqlite3ExprCacheClear(pParse);
112938
112939      }
112940
112941      /* If the index or temporary table used by the GROUP BY sort
112942      ** will naturally deliver rows in the order required by the ORDER BY
112943      ** clause, cancel the ephemeral table open coded earlier.
112944      **
112945      ** This is an optimization - the correct answer should result regardless.
112946      ** Use the SQLITE_GroupByOrder flag with SQLITE_TESTCTRL_OPTIMIZER to
112947      ** disable this optimization for testing purposes.  */
112948      if( orderByGrp && OptimizationEnabled(db, SQLITE_GroupByOrder)
112949       && (groupBySort || sqlite3WhereIsSorted(pWInfo))
112950      ){
112951        sSort.pOrderBy = 0;
112952        sqlite3VdbeChangeToNoop(v, sSort.addrSortIndex);
112953      }
112954
112955      /* Evaluate the current GROUP BY terms and store in b0, b1, b2...
112956      ** (b0 is memory location iBMem+0, b1 is iBMem+1, and so forth)
112957      ** Then compare the current GROUP BY terms against the GROUP BY terms
112958      ** from the previous row currently stored in a0, a1, a2...
112959      */
112960      addrTopOfLoop = sqlite3VdbeCurrentAddr(v);
112961      sqlite3ExprCacheClear(pParse);
112962      if( groupBySort ){
112963        sqlite3VdbeAddOp3(v, OP_SorterData, sAggInfo.sortingIdx,
112964                          sortOut, sortPTab);
112965      }
112966      for(j=0; j<pGroupBy->nExpr; j++){
112967        if( groupBySort ){
112968          sqlite3VdbeAddOp3(v, OP_Column, sortPTab, j, iBMem+j);
112969        }else{
112970          sAggInfo.directMode = 1;
112971          sqlite3ExprCode(pParse, pGroupBy->a[j].pExpr, iBMem+j);
112972        }
112973      }
112974      sqlite3VdbeAddOp4(v, OP_Compare, iAMem, iBMem, pGroupBy->nExpr,
112975                          (char*)sqlite3KeyInfoRef(pKeyInfo), P4_KEYINFO);
112976      j1 = sqlite3VdbeCurrentAddr(v);
112977      sqlite3VdbeAddOp3(v, OP_Jump, j1+1, 0, j1+1); VdbeCoverage(v);
112978
112979      /* Generate code that runs whenever the GROUP BY changes.
112980      ** Changes in the GROUP BY are detected by the previous code
112981      ** block.  If there were no changes, this block is skipped.
112982      **
112983      ** This code copies current group by terms in b0,b1,b2,...
112984      ** over to a0,a1,a2.  It then calls the output subroutine
112985      ** and resets the aggregate accumulator registers in preparation
112986      ** for the next GROUP BY batch.
112987      */
112988      sqlite3ExprCodeMove(pParse, iBMem, iAMem, pGroupBy->nExpr);
112989      sqlite3VdbeAddOp2(v, OP_Gosub, regOutputRow, addrOutputRow);
112990      VdbeComment((v, "output one row"));
112991      sqlite3VdbeAddOp2(v, OP_IfPos, iAbortFlag, addrEnd); VdbeCoverage(v);
112992      VdbeComment((v, "check abort flag"));
112993      sqlite3VdbeAddOp2(v, OP_Gosub, regReset, addrReset);
112994      VdbeComment((v, "reset accumulator"));
112995
112996      /* Update the aggregate accumulators based on the content of
112997      ** the current row
112998      */
112999      sqlite3VdbeJumpHere(v, j1);
113000      updateAccumulator(pParse, &sAggInfo);
113001      sqlite3VdbeAddOp2(v, OP_Integer, 1, iUseFlag);
113002      VdbeComment((v, "indicate data in accumulator"));
113003
113004      /* End of the loop
113005      */
113006      if( groupBySort ){
113007        sqlite3VdbeAddOp2(v, OP_SorterNext, sAggInfo.sortingIdx, addrTopOfLoop);
113008        VdbeCoverage(v);
113009      }else{
113010        sqlite3WhereEnd(pWInfo);
113011        sqlite3VdbeChangeToNoop(v, addrSortingIdx);
113012      }
113013
113014      /* Output the final row of result
113015      */
113016      sqlite3VdbeAddOp2(v, OP_Gosub, regOutputRow, addrOutputRow);
113017      VdbeComment((v, "output final row"));
113018
113019      /* Jump over the subroutines
113020      */
113021      sqlite3VdbeAddOp2(v, OP_Goto, 0, addrEnd);
113022
113023      /* Generate a subroutine that outputs a single row of the result
113024      ** set.  This subroutine first looks at the iUseFlag.  If iUseFlag
113025      ** is less than or equal to zero, the subroutine is a no-op.  If
113026      ** the processing calls for the query to abort, this subroutine
113027      ** increments the iAbortFlag memory location before returning in
113028      ** order to signal the caller to abort.
113029      */
113030      addrSetAbort = sqlite3VdbeCurrentAddr(v);
113031      sqlite3VdbeAddOp2(v, OP_Integer, 1, iAbortFlag);
113032      VdbeComment((v, "set abort flag"));
113033      sqlite3VdbeAddOp1(v, OP_Return, regOutputRow);
113034      sqlite3VdbeResolveLabel(v, addrOutputRow);
113035      addrOutputRow = sqlite3VdbeCurrentAddr(v);
113036      sqlite3VdbeAddOp2(v, OP_IfPos, iUseFlag, addrOutputRow+2);
113037      VdbeCoverage(v);
113038      VdbeComment((v, "Groupby result generator entry point"));
113039      sqlite3VdbeAddOp1(v, OP_Return, regOutputRow);
113040      finalizeAggFunctions(pParse, &sAggInfo);
113041      sqlite3ExprIfFalse(pParse, pHaving, addrOutputRow+1, SQLITE_JUMPIFNULL);
113042      selectInnerLoop(pParse, p, p->pEList, -1, &sSort,
113043                      &sDistinct, pDest,
113044                      addrOutputRow+1, addrSetAbort);
113045      sqlite3VdbeAddOp1(v, OP_Return, regOutputRow);
113046      VdbeComment((v, "end groupby result generator"));
113047
113048      /* Generate a subroutine that will reset the group-by accumulator
113049      */
113050      sqlite3VdbeResolveLabel(v, addrReset);
113051      resetAccumulator(pParse, &sAggInfo);
113052      sqlite3VdbeAddOp1(v, OP_Return, regReset);
113053
113054    } /* endif pGroupBy.  Begin aggregate queries without GROUP BY: */
113055    else {
113056      ExprList *pDel = 0;
113057#ifndef SQLITE_OMIT_BTREECOUNT
113058      Table *pTab;
113059      if( (pTab = isSimpleCount(p, &sAggInfo))!=0 ){
113060        /* If isSimpleCount() returns a pointer to a Table structure, then
113061        ** the SQL statement is of the form:
113062        **
113063        **   SELECT count(*) FROM <tbl>
113064        **
113065        ** where the Table structure returned represents table <tbl>.
113066        **
113067        ** This statement is so common that it is optimized specially. The
113068        ** OP_Count instruction is executed either on the intkey table that
113069        ** contains the data for table <tbl> or on one of its indexes. It
113070        ** is better to execute the op on an index, as indexes are almost
113071        ** always spread across less pages than their corresponding tables.
113072        */
113073        const int iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
113074        const int iCsr = pParse->nTab++;     /* Cursor to scan b-tree */
113075        Index *pIdx;                         /* Iterator variable */
113076        KeyInfo *pKeyInfo = 0;               /* Keyinfo for scanned index */
113077        Index *pBest = 0;                    /* Best index found so far */
113078        int iRoot = pTab->tnum;              /* Root page of scanned b-tree */
113079
113080        sqlite3CodeVerifySchema(pParse, iDb);
113081        sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
113082
113083        /* Search for the index that has the lowest scan cost.
113084        **
113085        ** (2011-04-15) Do not do a full scan of an unordered index.
113086        **
113087        ** (2013-10-03) Do not count the entries in a partial index.
113088        **
113089        ** In practice the KeyInfo structure will not be used. It is only
113090        ** passed to keep OP_OpenRead happy.
113091        */
113092        if( !HasRowid(pTab) ) pBest = sqlite3PrimaryKeyIndex(pTab);
113093        for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
113094          if( pIdx->bUnordered==0
113095           && pIdx->szIdxRow<pTab->szTabRow
113096           && pIdx->pPartIdxWhere==0
113097           && (!pBest || pIdx->szIdxRow<pBest->szIdxRow)
113098          ){
113099            pBest = pIdx;
113100          }
113101        }
113102        if( pBest ){
113103          iRoot = pBest->tnum;
113104          pKeyInfo = sqlite3KeyInfoOfIndex(pParse, pBest);
113105        }
113106
113107        /* Open a read-only cursor, execute the OP_Count, close the cursor. */
113108        sqlite3VdbeAddOp4Int(v, OP_OpenRead, iCsr, iRoot, iDb, 1);
113109        if( pKeyInfo ){
113110          sqlite3VdbeChangeP4(v, -1, (char *)pKeyInfo, P4_KEYINFO);
113111        }
113112        sqlite3VdbeAddOp2(v, OP_Count, iCsr, sAggInfo.aFunc[0].iMem);
113113        sqlite3VdbeAddOp1(v, OP_Close, iCsr);
113114        explainSimpleCount(pParse, pTab, pBest);
113115      }else
113116#endif /* SQLITE_OMIT_BTREECOUNT */
113117      {
113118        /* Check if the query is of one of the following forms:
113119        **
113120        **   SELECT min(x) FROM ...
113121        **   SELECT max(x) FROM ...
113122        **
113123        ** If it is, then ask the code in where.c to attempt to sort results
113124        ** as if there was an "ORDER ON x" or "ORDER ON x DESC" clause.
113125        ** If where.c is able to produce results sorted in this order, then
113126        ** add vdbe code to break out of the processing loop after the
113127        ** first iteration (since the first iteration of the loop is
113128        ** guaranteed to operate on the row with the minimum or maximum
113129        ** value of x, the only row required).
113130        **
113131        ** A special flag must be passed to sqlite3WhereBegin() to slightly
113132        ** modify behavior as follows:
113133        **
113134        **   + If the query is a "SELECT min(x)", then the loop coded by
113135        **     where.c should not iterate over any values with a NULL value
113136        **     for x.
113137        **
113138        **   + The optimizer code in where.c (the thing that decides which
113139        **     index or indices to use) should place a different priority on
113140        **     satisfying the 'ORDER BY' clause than it does in other cases.
113141        **     Refer to code and comments in where.c for details.
113142        */
113143        ExprList *pMinMax = 0;
113144        u8 flag = WHERE_ORDERBY_NORMAL;
113145
113146        assert( p->pGroupBy==0 );
113147        assert( flag==0 );
113148        if( p->pHaving==0 ){
113149          flag = minMaxQuery(&sAggInfo, &pMinMax);
113150        }
113151        assert( flag==0 || (pMinMax!=0 && pMinMax->nExpr==1) );
113152
113153        if( flag ){
113154          pMinMax = sqlite3ExprListDup(db, pMinMax, 0);
113155          pDel = pMinMax;
113156          if( pMinMax && !db->mallocFailed ){
113157            pMinMax->a[0].sortOrder = flag!=WHERE_ORDERBY_MIN ?1:0;
113158            pMinMax->a[0].pExpr->op = TK_COLUMN;
113159          }
113160        }
113161
113162        /* This case runs if the aggregate has no GROUP BY clause.  The
113163        ** processing is much simpler since there is only a single row
113164        ** of output.
113165        */
113166        resetAccumulator(pParse, &sAggInfo);
113167        pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, pMinMax,0,flag,0);
113168        if( pWInfo==0 ){
113169          sqlite3ExprListDelete(db, pDel);
113170          goto select_end;
113171        }
113172        updateAccumulator(pParse, &sAggInfo);
113173        assert( pMinMax==0 || pMinMax->nExpr==1 );
113174        if( sqlite3WhereIsOrdered(pWInfo)>0 ){
113175          sqlite3VdbeAddOp2(v, OP_Goto, 0, sqlite3WhereBreakLabel(pWInfo));
113176          VdbeComment((v, "%s() by index",
113177                (flag==WHERE_ORDERBY_MIN?"min":"max")));
113178        }
113179        sqlite3WhereEnd(pWInfo);
113180        finalizeAggFunctions(pParse, &sAggInfo);
113181      }
113182
113183      sSort.pOrderBy = 0;
113184      sqlite3ExprIfFalse(pParse, pHaving, addrEnd, SQLITE_JUMPIFNULL);
113185      selectInnerLoop(pParse, p, p->pEList, -1, 0, 0,
113186                      pDest, addrEnd, addrEnd);
113187      sqlite3ExprListDelete(db, pDel);
113188    }
113189    sqlite3VdbeResolveLabel(v, addrEnd);
113190
113191  } /* endif aggregate query */
113192
113193  if( sDistinct.eTnctType==WHERE_DISTINCT_UNORDERED ){
113194    explainTempTable(pParse, "DISTINCT");
113195  }
113196
113197  /* If there is an ORDER BY clause, then we need to sort the results
113198  ** and send them to the callback one by one.
113199  */
113200  if( sSort.pOrderBy ){
113201    explainTempTable(pParse,
113202                     sSort.nOBSat>0 ? "RIGHT PART OF ORDER BY":"ORDER BY");
113203    generateSortTail(pParse, p, &sSort, pEList->nExpr, pDest);
113204  }
113205
113206  /* Jump here to skip this query
113207  */
113208  sqlite3VdbeResolveLabel(v, iEnd);
113209
113210  /* The SELECT has been coded. If there is an error in the Parse structure,
113211  ** set the return code to 1. Otherwise 0. */
113212  rc = (pParse->nErr>0);
113213
113214  /* Control jumps to here if an error is encountered above, or upon
113215  ** successful coding of the SELECT.
113216  */
113217select_end:
113218  explainSetInteger(pParse->iSelectId, iRestoreSelectId);
113219
113220  /* Identify column names if results of the SELECT are to be output.
113221  */
113222  if( rc==SQLITE_OK && pDest->eDest==SRT_Output ){
113223    generateColumnNames(pParse, pTabList, pEList);
113224  }
113225
113226  sqlite3DbFree(db, sAggInfo.aCol);
113227  sqlite3DbFree(db, sAggInfo.aFunc);
113228#if SELECTTRACE_ENABLED
113229  SELECTTRACE(1,pParse,p,("end processing\n"));
113230  pParse->nSelectIndent--;
113231#endif
113232  return rc;
113233}
113234
113235/************** End of select.c **********************************************/
113236/************** Begin file table.c *******************************************/
113237/*
113238** 2001 September 15
113239**
113240** The author disclaims copyright to this source code.  In place of
113241** a legal notice, here is a blessing:
113242**
113243**    May you do good and not evil.
113244**    May you find forgiveness for yourself and forgive others.
113245**    May you share freely, never taking more than you give.
113246**
113247*************************************************************************
113248** This file contains the sqlite3_get_table() and sqlite3_free_table()
113249** interface routines.  These are just wrappers around the main
113250** interface routine of sqlite3_exec().
113251**
113252** These routines are in a separate files so that they will not be linked
113253** if they are not used.
113254*/
113255/* #include "sqliteInt.h" */
113256/* #include <stdlib.h> */
113257/* #include <string.h> */
113258
113259#ifndef SQLITE_OMIT_GET_TABLE
113260
113261/*
113262** This structure is used to pass data from sqlite3_get_table() through
113263** to the callback function is uses to build the result.
113264*/
113265typedef struct TabResult {
113266  char **azResult;   /* Accumulated output */
113267  char *zErrMsg;     /* Error message text, if an error occurs */
113268  u32 nAlloc;        /* Slots allocated for azResult[] */
113269  u32 nRow;          /* Number of rows in the result */
113270  u32 nColumn;       /* Number of columns in the result */
113271  u32 nData;         /* Slots used in azResult[].  (nRow+1)*nColumn */
113272  int rc;            /* Return code from sqlite3_exec() */
113273} TabResult;
113274
113275/*
113276** This routine is called once for each row in the result table.  Its job
113277** is to fill in the TabResult structure appropriately, allocating new
113278** memory as necessary.
113279*/
113280static int sqlite3_get_table_cb(void *pArg, int nCol, char **argv, char **colv){
113281  TabResult *p = (TabResult*)pArg;  /* Result accumulator */
113282  int need;                         /* Slots needed in p->azResult[] */
113283  int i;                            /* Loop counter */
113284  char *z;                          /* A single column of result */
113285
113286  /* Make sure there is enough space in p->azResult to hold everything
113287  ** we need to remember from this invocation of the callback.
113288  */
113289  if( p->nRow==0 && argv!=0 ){
113290    need = nCol*2;
113291  }else{
113292    need = nCol;
113293  }
113294  if( p->nData + need > p->nAlloc ){
113295    char **azNew;
113296    p->nAlloc = p->nAlloc*2 + need;
113297    azNew = sqlite3_realloc64( p->azResult, sizeof(char*)*p->nAlloc );
113298    if( azNew==0 ) goto malloc_failed;
113299    p->azResult = azNew;
113300  }
113301
113302  /* If this is the first row, then generate an extra row containing
113303  ** the names of all columns.
113304  */
113305  if( p->nRow==0 ){
113306    p->nColumn = nCol;
113307    for(i=0; i<nCol; i++){
113308      z = sqlite3_mprintf("%s", colv[i]);
113309      if( z==0 ) goto malloc_failed;
113310      p->azResult[p->nData++] = z;
113311    }
113312  }else if( (int)p->nColumn!=nCol ){
113313    sqlite3_free(p->zErrMsg);
113314    p->zErrMsg = sqlite3_mprintf(
113315       "sqlite3_get_table() called with two or more incompatible queries"
113316    );
113317    p->rc = SQLITE_ERROR;
113318    return 1;
113319  }
113320
113321  /* Copy over the row data
113322  */
113323  if( argv!=0 ){
113324    for(i=0; i<nCol; i++){
113325      if( argv[i]==0 ){
113326        z = 0;
113327      }else{
113328        int n = sqlite3Strlen30(argv[i])+1;
113329        z = sqlite3_malloc64( n );
113330        if( z==0 ) goto malloc_failed;
113331        memcpy(z, argv[i], n);
113332      }
113333      p->azResult[p->nData++] = z;
113334    }
113335    p->nRow++;
113336  }
113337  return 0;
113338
113339malloc_failed:
113340  p->rc = SQLITE_NOMEM;
113341  return 1;
113342}
113343
113344/*
113345** Query the database.  But instead of invoking a callback for each row,
113346** malloc() for space to hold the result and return the entire results
113347** at the conclusion of the call.
113348**
113349** The result that is written to ***pazResult is held in memory obtained
113350** from malloc().  But the caller cannot free this memory directly.
113351** Instead, the entire table should be passed to sqlite3_free_table() when
113352** the calling procedure is finished using it.
113353*/
113354SQLITE_API int SQLITE_STDCALL sqlite3_get_table(
113355  sqlite3 *db,                /* The database on which the SQL executes */
113356  const char *zSql,           /* The SQL to be executed */
113357  char ***pazResult,          /* Write the result table here */
113358  int *pnRow,                 /* Write the number of rows in the result here */
113359  int *pnColumn,              /* Write the number of columns of result here */
113360  char **pzErrMsg             /* Write error messages here */
113361){
113362  int rc;
113363  TabResult res;
113364
113365#ifdef SQLITE_ENABLE_API_ARMOR
113366  if( !sqlite3SafetyCheckOk(db) || pazResult==0 ) return SQLITE_MISUSE_BKPT;
113367#endif
113368  *pazResult = 0;
113369  if( pnColumn ) *pnColumn = 0;
113370  if( pnRow ) *pnRow = 0;
113371  if( pzErrMsg ) *pzErrMsg = 0;
113372  res.zErrMsg = 0;
113373  res.nRow = 0;
113374  res.nColumn = 0;
113375  res.nData = 1;
113376  res.nAlloc = 20;
113377  res.rc = SQLITE_OK;
113378  res.azResult = sqlite3_malloc64(sizeof(char*)*res.nAlloc );
113379  if( res.azResult==0 ){
113380     db->errCode = SQLITE_NOMEM;
113381     return SQLITE_NOMEM;
113382  }
113383  res.azResult[0] = 0;
113384  rc = sqlite3_exec(db, zSql, sqlite3_get_table_cb, &res, pzErrMsg);
113385  assert( sizeof(res.azResult[0])>= sizeof(res.nData) );
113386  res.azResult[0] = SQLITE_INT_TO_PTR(res.nData);
113387  if( (rc&0xff)==SQLITE_ABORT ){
113388    sqlite3_free_table(&res.azResult[1]);
113389    if( res.zErrMsg ){
113390      if( pzErrMsg ){
113391        sqlite3_free(*pzErrMsg);
113392        *pzErrMsg = sqlite3_mprintf("%s",res.zErrMsg);
113393      }
113394      sqlite3_free(res.zErrMsg);
113395    }
113396    db->errCode = res.rc;  /* Assume 32-bit assignment is atomic */
113397    return res.rc;
113398  }
113399  sqlite3_free(res.zErrMsg);
113400  if( rc!=SQLITE_OK ){
113401    sqlite3_free_table(&res.azResult[1]);
113402    return rc;
113403  }
113404  if( res.nAlloc>res.nData ){
113405    char **azNew;
113406    azNew = sqlite3_realloc64( res.azResult, sizeof(char*)*res.nData );
113407    if( azNew==0 ){
113408      sqlite3_free_table(&res.azResult[1]);
113409      db->errCode = SQLITE_NOMEM;
113410      return SQLITE_NOMEM;
113411    }
113412    res.azResult = azNew;
113413  }
113414  *pazResult = &res.azResult[1];
113415  if( pnColumn ) *pnColumn = res.nColumn;
113416  if( pnRow ) *pnRow = res.nRow;
113417  return rc;
113418}
113419
113420/*
113421** This routine frees the space the sqlite3_get_table() malloced.
113422*/
113423SQLITE_API void SQLITE_STDCALL sqlite3_free_table(
113424  char **azResult            /* Result returned from sqlite3_get_table() */
113425){
113426  if( azResult ){
113427    int i, n;
113428    azResult--;
113429    assert( azResult!=0 );
113430    n = SQLITE_PTR_TO_INT(azResult[0]);
113431    for(i=1; i<n; i++){ if( azResult[i] ) sqlite3_free(azResult[i]); }
113432    sqlite3_free(azResult);
113433  }
113434}
113435
113436#endif /* SQLITE_OMIT_GET_TABLE */
113437
113438/************** End of table.c ***********************************************/
113439/************** Begin file trigger.c *****************************************/
113440/*
113441**
113442** The author disclaims copyright to this source code.  In place of
113443** a legal notice, here is a blessing:
113444**
113445**    May you do good and not evil.
113446**    May you find forgiveness for yourself and forgive others.
113447**    May you share freely, never taking more than you give.
113448**
113449*************************************************************************
113450** This file contains the implementation for TRIGGERs
113451*/
113452/* #include "sqliteInt.h" */
113453
113454#ifndef SQLITE_OMIT_TRIGGER
113455/*
113456** Delete a linked list of TriggerStep structures.
113457*/
113458SQLITE_PRIVATE void sqlite3DeleteTriggerStep(sqlite3 *db, TriggerStep *pTriggerStep){
113459  while( pTriggerStep ){
113460    TriggerStep * pTmp = pTriggerStep;
113461    pTriggerStep = pTriggerStep->pNext;
113462
113463    sqlite3ExprDelete(db, pTmp->pWhere);
113464    sqlite3ExprListDelete(db, pTmp->pExprList);
113465    sqlite3SelectDelete(db, pTmp->pSelect);
113466    sqlite3IdListDelete(db, pTmp->pIdList);
113467
113468    sqlite3DbFree(db, pTmp);
113469  }
113470}
113471
113472/*
113473** Given table pTab, return a list of all the triggers attached to
113474** the table. The list is connected by Trigger.pNext pointers.
113475**
113476** All of the triggers on pTab that are in the same database as pTab
113477** are already attached to pTab->pTrigger.  But there might be additional
113478** triggers on pTab in the TEMP schema.  This routine prepends all
113479** TEMP triggers on pTab to the beginning of the pTab->pTrigger list
113480** and returns the combined list.
113481**
113482** To state it another way:  This routine returns a list of all triggers
113483** that fire off of pTab.  The list will include any TEMP triggers on
113484** pTab as well as the triggers lised in pTab->pTrigger.
113485*/
113486SQLITE_PRIVATE Trigger *sqlite3TriggerList(Parse *pParse, Table *pTab){
113487  Schema * const pTmpSchema = pParse->db->aDb[1].pSchema;
113488  Trigger *pList = 0;                  /* List of triggers to return */
113489
113490  if( pParse->disableTriggers ){
113491    return 0;
113492  }
113493
113494  if( pTmpSchema!=pTab->pSchema ){
113495    HashElem *p;
113496    assert( sqlite3SchemaMutexHeld(pParse->db, 0, pTmpSchema) );
113497    for(p=sqliteHashFirst(&pTmpSchema->trigHash); p; p=sqliteHashNext(p)){
113498      Trigger *pTrig = (Trigger *)sqliteHashData(p);
113499      if( pTrig->pTabSchema==pTab->pSchema
113500       && 0==sqlite3StrICmp(pTrig->table, pTab->zName)
113501      ){
113502        pTrig->pNext = (pList ? pList : pTab->pTrigger);
113503        pList = pTrig;
113504      }
113505    }
113506  }
113507
113508  return (pList ? pList : pTab->pTrigger);
113509}
113510
113511/*
113512** This is called by the parser when it sees a CREATE TRIGGER statement
113513** up to the point of the BEGIN before the trigger actions.  A Trigger
113514** structure is generated based on the information available and stored
113515** in pParse->pNewTrigger.  After the trigger actions have been parsed, the
113516** sqlite3FinishTrigger() function is called to complete the trigger
113517** construction process.
113518*/
113519SQLITE_PRIVATE void sqlite3BeginTrigger(
113520  Parse *pParse,      /* The parse context of the CREATE TRIGGER statement */
113521  Token *pName1,      /* The name of the trigger */
113522  Token *pName2,      /* The name of the trigger */
113523  int tr_tm,          /* One of TK_BEFORE, TK_AFTER, TK_INSTEAD */
113524  int op,             /* One of TK_INSERT, TK_UPDATE, TK_DELETE */
113525  IdList *pColumns,   /* column list if this is an UPDATE OF trigger */
113526  SrcList *pTableName,/* The name of the table/view the trigger applies to */
113527  Expr *pWhen,        /* WHEN clause */
113528  int isTemp,         /* True if the TEMPORARY keyword is present */
113529  int noErr           /* Suppress errors if the trigger already exists */
113530){
113531  Trigger *pTrigger = 0;  /* The new trigger */
113532  Table *pTab;            /* Table that the trigger fires off of */
113533  char *zName = 0;        /* Name of the trigger */
113534  sqlite3 *db = pParse->db;  /* The database connection */
113535  int iDb;                /* The database to store the trigger in */
113536  Token *pName;           /* The unqualified db name */
113537  DbFixer sFix;           /* State vector for the DB fixer */
113538  int iTabDb;             /* Index of the database holding pTab */
113539
113540  assert( pName1!=0 );   /* pName1->z might be NULL, but not pName1 itself */
113541  assert( pName2!=0 );
113542  assert( op==TK_INSERT || op==TK_UPDATE || op==TK_DELETE );
113543  assert( op>0 && op<0xff );
113544  if( isTemp ){
113545    /* If TEMP was specified, then the trigger name may not be qualified. */
113546    if( pName2->n>0 ){
113547      sqlite3ErrorMsg(pParse, "temporary trigger may not have qualified name");
113548      goto trigger_cleanup;
113549    }
113550    iDb = 1;
113551    pName = pName1;
113552  }else{
113553    /* Figure out the db that the trigger will be created in */
113554    iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pName);
113555    if( iDb<0 ){
113556      goto trigger_cleanup;
113557    }
113558  }
113559  if( !pTableName || db->mallocFailed ){
113560    goto trigger_cleanup;
113561  }
113562
113563  /* A long-standing parser bug is that this syntax was allowed:
113564  **
113565  **    CREATE TRIGGER attached.demo AFTER INSERT ON attached.tab ....
113566  **                                                 ^^^^^^^^
113567  **
113568  ** To maintain backwards compatibility, ignore the database
113569  ** name on pTableName if we are reparsing out of SQLITE_MASTER.
113570  */
113571  if( db->init.busy && iDb!=1 ){
113572    sqlite3DbFree(db, pTableName->a[0].zDatabase);
113573    pTableName->a[0].zDatabase = 0;
113574  }
113575
113576  /* If the trigger name was unqualified, and the table is a temp table,
113577  ** then set iDb to 1 to create the trigger in the temporary database.
113578  ** If sqlite3SrcListLookup() returns 0, indicating the table does not
113579  ** exist, the error is caught by the block below.
113580  */
113581  pTab = sqlite3SrcListLookup(pParse, pTableName);
113582  if( db->init.busy==0 && pName2->n==0 && pTab
113583        && pTab->pSchema==db->aDb[1].pSchema ){
113584    iDb = 1;
113585  }
113586
113587  /* Ensure the table name matches database name and that the table exists */
113588  if( db->mallocFailed ) goto trigger_cleanup;
113589  assert( pTableName->nSrc==1 );
113590  sqlite3FixInit(&sFix, pParse, iDb, "trigger", pName);
113591  if( sqlite3FixSrcList(&sFix, pTableName) ){
113592    goto trigger_cleanup;
113593  }
113594  pTab = sqlite3SrcListLookup(pParse, pTableName);
113595  if( !pTab ){
113596    /* The table does not exist. */
113597    if( db->init.iDb==1 ){
113598      /* Ticket #3810.
113599      ** Normally, whenever a table is dropped, all associated triggers are
113600      ** dropped too.  But if a TEMP trigger is created on a non-TEMP table
113601      ** and the table is dropped by a different database connection, the
113602      ** trigger is not visible to the database connection that does the
113603      ** drop so the trigger cannot be dropped.  This results in an
113604      ** "orphaned trigger" - a trigger whose associated table is missing.
113605      */
113606      db->init.orphanTrigger = 1;
113607    }
113608    goto trigger_cleanup;
113609  }
113610  if( IsVirtual(pTab) ){
113611    sqlite3ErrorMsg(pParse, "cannot create triggers on virtual tables");
113612    goto trigger_cleanup;
113613  }
113614
113615  /* Check that the trigger name is not reserved and that no trigger of the
113616  ** specified name exists */
113617  zName = sqlite3NameFromToken(db, pName);
113618  if( !zName || SQLITE_OK!=sqlite3CheckObjectName(pParse, zName) ){
113619    goto trigger_cleanup;
113620  }
113621  assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
113622  if( sqlite3HashFind(&(db->aDb[iDb].pSchema->trigHash),zName) ){
113623    if( !noErr ){
113624      sqlite3ErrorMsg(pParse, "trigger %T already exists", pName);
113625    }else{
113626      assert( !db->init.busy );
113627      sqlite3CodeVerifySchema(pParse, iDb);
113628    }
113629    goto trigger_cleanup;
113630  }
113631
113632  /* Do not create a trigger on a system table */
113633  if( sqlite3StrNICmp(pTab->zName, "sqlite_", 7)==0 ){
113634    sqlite3ErrorMsg(pParse, "cannot create trigger on system table");
113635    goto trigger_cleanup;
113636  }
113637
113638  /* INSTEAD of triggers are only for views and views only support INSTEAD
113639  ** of triggers.
113640  */
113641  if( pTab->pSelect && tr_tm!=TK_INSTEAD ){
113642    sqlite3ErrorMsg(pParse, "cannot create %s trigger on view: %S",
113643        (tr_tm == TK_BEFORE)?"BEFORE":"AFTER", pTableName, 0);
113644    goto trigger_cleanup;
113645  }
113646  if( !pTab->pSelect && tr_tm==TK_INSTEAD ){
113647    sqlite3ErrorMsg(pParse, "cannot create INSTEAD OF"
113648        " trigger on table: %S", pTableName, 0);
113649    goto trigger_cleanup;
113650  }
113651  iTabDb = sqlite3SchemaToIndex(db, pTab->pSchema);
113652
113653#ifndef SQLITE_OMIT_AUTHORIZATION
113654  {
113655    int code = SQLITE_CREATE_TRIGGER;
113656    const char *zDb = db->aDb[iTabDb].zName;
113657    const char *zDbTrig = isTemp ? db->aDb[1].zName : zDb;
113658    if( iTabDb==1 || isTemp ) code = SQLITE_CREATE_TEMP_TRIGGER;
113659    if( sqlite3AuthCheck(pParse, code, zName, pTab->zName, zDbTrig) ){
113660      goto trigger_cleanup;
113661    }
113662    if( sqlite3AuthCheck(pParse, SQLITE_INSERT, SCHEMA_TABLE(iTabDb),0,zDb)){
113663      goto trigger_cleanup;
113664    }
113665  }
113666#endif
113667
113668  /* INSTEAD OF triggers can only appear on views and BEFORE triggers
113669  ** cannot appear on views.  So we might as well translate every
113670  ** INSTEAD OF trigger into a BEFORE trigger.  It simplifies code
113671  ** elsewhere.
113672  */
113673  if (tr_tm == TK_INSTEAD){
113674    tr_tm = TK_BEFORE;
113675  }
113676
113677  /* Build the Trigger object */
113678  pTrigger = (Trigger*)sqlite3DbMallocZero(db, sizeof(Trigger));
113679  if( pTrigger==0 ) goto trigger_cleanup;
113680  pTrigger->zName = zName;
113681  zName = 0;
113682  pTrigger->table = sqlite3DbStrDup(db, pTableName->a[0].zName);
113683  pTrigger->pSchema = db->aDb[iDb].pSchema;
113684  pTrigger->pTabSchema = pTab->pSchema;
113685  pTrigger->op = (u8)op;
113686  pTrigger->tr_tm = tr_tm==TK_BEFORE ? TRIGGER_BEFORE : TRIGGER_AFTER;
113687  pTrigger->pWhen = sqlite3ExprDup(db, pWhen, EXPRDUP_REDUCE);
113688  pTrigger->pColumns = sqlite3IdListDup(db, pColumns);
113689  assert( pParse->pNewTrigger==0 );
113690  pParse->pNewTrigger = pTrigger;
113691
113692trigger_cleanup:
113693  sqlite3DbFree(db, zName);
113694  sqlite3SrcListDelete(db, pTableName);
113695  sqlite3IdListDelete(db, pColumns);
113696  sqlite3ExprDelete(db, pWhen);
113697  if( !pParse->pNewTrigger ){
113698    sqlite3DeleteTrigger(db, pTrigger);
113699  }else{
113700    assert( pParse->pNewTrigger==pTrigger );
113701  }
113702}
113703
113704/*
113705** This routine is called after all of the trigger actions have been parsed
113706** in order to complete the process of building the trigger.
113707*/
113708SQLITE_PRIVATE void sqlite3FinishTrigger(
113709  Parse *pParse,          /* Parser context */
113710  TriggerStep *pStepList, /* The triggered program */
113711  Token *pAll             /* Token that describes the complete CREATE TRIGGER */
113712){
113713  Trigger *pTrig = pParse->pNewTrigger;   /* Trigger being finished */
113714  char *zName;                            /* Name of trigger */
113715  sqlite3 *db = pParse->db;               /* The database */
113716  DbFixer sFix;                           /* Fixer object */
113717  int iDb;                                /* Database containing the trigger */
113718  Token nameToken;                        /* Trigger name for error reporting */
113719
113720  pParse->pNewTrigger = 0;
113721  if( NEVER(pParse->nErr) || !pTrig ) goto triggerfinish_cleanup;
113722  zName = pTrig->zName;
113723  iDb = sqlite3SchemaToIndex(pParse->db, pTrig->pSchema);
113724  pTrig->step_list = pStepList;
113725  while( pStepList ){
113726    pStepList->pTrig = pTrig;
113727    pStepList = pStepList->pNext;
113728  }
113729  nameToken.z = pTrig->zName;
113730  nameToken.n = sqlite3Strlen30(nameToken.z);
113731  sqlite3FixInit(&sFix, pParse, iDb, "trigger", &nameToken);
113732  if( sqlite3FixTriggerStep(&sFix, pTrig->step_list)
113733   || sqlite3FixExpr(&sFix, pTrig->pWhen)
113734  ){
113735    goto triggerfinish_cleanup;
113736  }
113737
113738  /* if we are not initializing,
113739  ** build the sqlite_master entry
113740  */
113741  if( !db->init.busy ){
113742    Vdbe *v;
113743    char *z;
113744
113745    /* Make an entry in the sqlite_master table */
113746    v = sqlite3GetVdbe(pParse);
113747    if( v==0 ) goto triggerfinish_cleanup;
113748    sqlite3BeginWriteOperation(pParse, 0, iDb);
113749    z = sqlite3DbStrNDup(db, (char*)pAll->z, pAll->n);
113750    sqlite3NestedParse(pParse,
113751       "INSERT INTO %Q.%s VALUES('trigger',%Q,%Q,0,'CREATE TRIGGER %q')",
113752       db->aDb[iDb].zName, SCHEMA_TABLE(iDb), zName,
113753       pTrig->table, z);
113754    sqlite3DbFree(db, z);
113755    sqlite3ChangeCookie(pParse, iDb);
113756    sqlite3VdbeAddParseSchemaOp(v, iDb,
113757        sqlite3MPrintf(db, "type='trigger' AND name='%q'", zName));
113758  }
113759
113760  if( db->init.busy ){
113761    Trigger *pLink = pTrig;
113762    Hash *pHash = &db->aDb[iDb].pSchema->trigHash;
113763    assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
113764    pTrig = sqlite3HashInsert(pHash, zName, pTrig);
113765    if( pTrig ){
113766      db->mallocFailed = 1;
113767    }else if( pLink->pSchema==pLink->pTabSchema ){
113768      Table *pTab;
113769      pTab = sqlite3HashFind(&pLink->pTabSchema->tblHash, pLink->table);
113770      assert( pTab!=0 );
113771      pLink->pNext = pTab->pTrigger;
113772      pTab->pTrigger = pLink;
113773    }
113774  }
113775
113776triggerfinish_cleanup:
113777  sqlite3DeleteTrigger(db, pTrig);
113778  assert( !pParse->pNewTrigger );
113779  sqlite3DeleteTriggerStep(db, pStepList);
113780}
113781
113782/*
113783** Turn a SELECT statement (that the pSelect parameter points to) into
113784** a trigger step.  Return a pointer to a TriggerStep structure.
113785**
113786** The parser calls this routine when it finds a SELECT statement in
113787** body of a TRIGGER.
113788*/
113789SQLITE_PRIVATE TriggerStep *sqlite3TriggerSelectStep(sqlite3 *db, Select *pSelect){
113790  TriggerStep *pTriggerStep = sqlite3DbMallocZero(db, sizeof(TriggerStep));
113791  if( pTriggerStep==0 ) {
113792    sqlite3SelectDelete(db, pSelect);
113793    return 0;
113794  }
113795  pTriggerStep->op = TK_SELECT;
113796  pTriggerStep->pSelect = pSelect;
113797  pTriggerStep->orconf = OE_Default;
113798  return pTriggerStep;
113799}
113800
113801/*
113802** Allocate space to hold a new trigger step.  The allocated space
113803** holds both the TriggerStep object and the TriggerStep.target.z string.
113804**
113805** If an OOM error occurs, NULL is returned and db->mallocFailed is set.
113806*/
113807static TriggerStep *triggerStepAllocate(
113808  sqlite3 *db,                /* Database connection */
113809  u8 op,                      /* Trigger opcode */
113810  Token *pName                /* The target name */
113811){
113812  TriggerStep *pTriggerStep;
113813
113814  pTriggerStep = sqlite3DbMallocZero(db, sizeof(TriggerStep) + pName->n + 1);
113815  if( pTriggerStep ){
113816    char *z = (char*)&pTriggerStep[1];
113817    memcpy(z, pName->z, pName->n);
113818    sqlite3Dequote(z);
113819    pTriggerStep->zTarget = z;
113820    pTriggerStep->op = op;
113821  }
113822  return pTriggerStep;
113823}
113824
113825/*
113826** Build a trigger step out of an INSERT statement.  Return a pointer
113827** to the new trigger step.
113828**
113829** The parser calls this routine when it sees an INSERT inside the
113830** body of a trigger.
113831*/
113832SQLITE_PRIVATE TriggerStep *sqlite3TriggerInsertStep(
113833  sqlite3 *db,        /* The database connection */
113834  Token *pTableName,  /* Name of the table into which we insert */
113835  IdList *pColumn,    /* List of columns in pTableName to insert into */
113836  Select *pSelect,    /* A SELECT statement that supplies values */
113837  u8 orconf           /* The conflict algorithm (OE_Abort, OE_Replace, etc.) */
113838){
113839  TriggerStep *pTriggerStep;
113840
113841  assert(pSelect != 0 || db->mallocFailed);
113842
113843  pTriggerStep = triggerStepAllocate(db, TK_INSERT, pTableName);
113844  if( pTriggerStep ){
113845    pTriggerStep->pSelect = sqlite3SelectDup(db, pSelect, EXPRDUP_REDUCE);
113846    pTriggerStep->pIdList = pColumn;
113847    pTriggerStep->orconf = orconf;
113848  }else{
113849    sqlite3IdListDelete(db, pColumn);
113850  }
113851  sqlite3SelectDelete(db, pSelect);
113852
113853  return pTriggerStep;
113854}
113855
113856/*
113857** Construct a trigger step that implements an UPDATE statement and return
113858** a pointer to that trigger step.  The parser calls this routine when it
113859** sees an UPDATE statement inside the body of a CREATE TRIGGER.
113860*/
113861SQLITE_PRIVATE TriggerStep *sqlite3TriggerUpdateStep(
113862  sqlite3 *db,         /* The database connection */
113863  Token *pTableName,   /* Name of the table to be updated */
113864  ExprList *pEList,    /* The SET clause: list of column and new values */
113865  Expr *pWhere,        /* The WHERE clause */
113866  u8 orconf            /* The conflict algorithm. (OE_Abort, OE_Ignore, etc) */
113867){
113868  TriggerStep *pTriggerStep;
113869
113870  pTriggerStep = triggerStepAllocate(db, TK_UPDATE, pTableName);
113871  if( pTriggerStep ){
113872    pTriggerStep->pExprList = sqlite3ExprListDup(db, pEList, EXPRDUP_REDUCE);
113873    pTriggerStep->pWhere = sqlite3ExprDup(db, pWhere, EXPRDUP_REDUCE);
113874    pTriggerStep->orconf = orconf;
113875  }
113876  sqlite3ExprListDelete(db, pEList);
113877  sqlite3ExprDelete(db, pWhere);
113878  return pTriggerStep;
113879}
113880
113881/*
113882** Construct a trigger step that implements a DELETE statement and return
113883** a pointer to that trigger step.  The parser calls this routine when it
113884** sees a DELETE statement inside the body of a CREATE TRIGGER.
113885*/
113886SQLITE_PRIVATE TriggerStep *sqlite3TriggerDeleteStep(
113887  sqlite3 *db,            /* Database connection */
113888  Token *pTableName,      /* The table from which rows are deleted */
113889  Expr *pWhere            /* The WHERE clause */
113890){
113891  TriggerStep *pTriggerStep;
113892
113893  pTriggerStep = triggerStepAllocate(db, TK_DELETE, pTableName);
113894  if( pTriggerStep ){
113895    pTriggerStep->pWhere = sqlite3ExprDup(db, pWhere, EXPRDUP_REDUCE);
113896    pTriggerStep->orconf = OE_Default;
113897  }
113898  sqlite3ExprDelete(db, pWhere);
113899  return pTriggerStep;
113900}
113901
113902/*
113903** Recursively delete a Trigger structure
113904*/
113905SQLITE_PRIVATE void sqlite3DeleteTrigger(sqlite3 *db, Trigger *pTrigger){
113906  if( pTrigger==0 ) return;
113907  sqlite3DeleteTriggerStep(db, pTrigger->step_list);
113908  sqlite3DbFree(db, pTrigger->zName);
113909  sqlite3DbFree(db, pTrigger->table);
113910  sqlite3ExprDelete(db, pTrigger->pWhen);
113911  sqlite3IdListDelete(db, pTrigger->pColumns);
113912  sqlite3DbFree(db, pTrigger);
113913}
113914
113915/*
113916** This function is called to drop a trigger from the database schema.
113917**
113918** This may be called directly from the parser and therefore identifies
113919** the trigger by name.  The sqlite3DropTriggerPtr() routine does the
113920** same job as this routine except it takes a pointer to the trigger
113921** instead of the trigger name.
113922**/
113923SQLITE_PRIVATE void sqlite3DropTrigger(Parse *pParse, SrcList *pName, int noErr){
113924  Trigger *pTrigger = 0;
113925  int i;
113926  const char *zDb;
113927  const char *zName;
113928  sqlite3 *db = pParse->db;
113929
113930  if( db->mallocFailed ) goto drop_trigger_cleanup;
113931  if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){
113932    goto drop_trigger_cleanup;
113933  }
113934
113935  assert( pName->nSrc==1 );
113936  zDb = pName->a[0].zDatabase;
113937  zName = pName->a[0].zName;
113938  assert( zDb!=0 || sqlite3BtreeHoldsAllMutexes(db) );
113939  for(i=OMIT_TEMPDB; i<db->nDb; i++){
113940    int j = (i<2) ? i^1 : i;  /* Search TEMP before MAIN */
113941    if( zDb && sqlite3StrICmp(db->aDb[j].zName, zDb) ) continue;
113942    assert( sqlite3SchemaMutexHeld(db, j, 0) );
113943    pTrigger = sqlite3HashFind(&(db->aDb[j].pSchema->trigHash), zName);
113944    if( pTrigger ) break;
113945  }
113946  if( !pTrigger ){
113947    if( !noErr ){
113948      sqlite3ErrorMsg(pParse, "no such trigger: %S", pName, 0);
113949    }else{
113950      sqlite3CodeVerifyNamedSchema(pParse, zDb);
113951    }
113952    pParse->checkSchema = 1;
113953    goto drop_trigger_cleanup;
113954  }
113955  sqlite3DropTriggerPtr(pParse, pTrigger);
113956
113957drop_trigger_cleanup:
113958  sqlite3SrcListDelete(db, pName);
113959}
113960
113961/*
113962** Return a pointer to the Table structure for the table that a trigger
113963** is set on.
113964*/
113965static Table *tableOfTrigger(Trigger *pTrigger){
113966  return sqlite3HashFind(&pTrigger->pTabSchema->tblHash, pTrigger->table);
113967}
113968
113969
113970/*
113971** Drop a trigger given a pointer to that trigger.
113972*/
113973SQLITE_PRIVATE void sqlite3DropTriggerPtr(Parse *pParse, Trigger *pTrigger){
113974  Table   *pTable;
113975  Vdbe *v;
113976  sqlite3 *db = pParse->db;
113977  int iDb;
113978
113979  iDb = sqlite3SchemaToIndex(pParse->db, pTrigger->pSchema);
113980  assert( iDb>=0 && iDb<db->nDb );
113981  pTable = tableOfTrigger(pTrigger);
113982  assert( pTable );
113983  assert( pTable->pSchema==pTrigger->pSchema || iDb==1 );
113984#ifndef SQLITE_OMIT_AUTHORIZATION
113985  {
113986    int code = SQLITE_DROP_TRIGGER;
113987    const char *zDb = db->aDb[iDb].zName;
113988    const char *zTab = SCHEMA_TABLE(iDb);
113989    if( iDb==1 ) code = SQLITE_DROP_TEMP_TRIGGER;
113990    if( sqlite3AuthCheck(pParse, code, pTrigger->zName, pTable->zName, zDb) ||
113991      sqlite3AuthCheck(pParse, SQLITE_DELETE, zTab, 0, zDb) ){
113992      return;
113993    }
113994  }
113995#endif
113996
113997  /* Generate code to destroy the database record of the trigger.
113998  */
113999  assert( pTable!=0 );
114000  if( (v = sqlite3GetVdbe(pParse))!=0 ){
114001    int base;
114002    static const int iLn = VDBE_OFFSET_LINENO(2);
114003    static const VdbeOpList dropTrigger[] = {
114004      { OP_Rewind,     0, ADDR(9),  0},
114005      { OP_String8,    0, 1,        0}, /* 1 */
114006      { OP_Column,     0, 1,        2},
114007      { OP_Ne,         2, ADDR(8),  1},
114008      { OP_String8,    0, 1,        0}, /* 4: "trigger" */
114009      { OP_Column,     0, 0,        2},
114010      { OP_Ne,         2, ADDR(8),  1},
114011      { OP_Delete,     0, 0,        0},
114012      { OP_Next,       0, ADDR(1),  0}, /* 8 */
114013    };
114014
114015    sqlite3BeginWriteOperation(pParse, 0, iDb);
114016    sqlite3OpenMasterTable(pParse, iDb);
114017    base = sqlite3VdbeAddOpList(v,  ArraySize(dropTrigger), dropTrigger, iLn);
114018    sqlite3VdbeChangeP4(v, base+1, pTrigger->zName, P4_TRANSIENT);
114019    sqlite3VdbeChangeP4(v, base+4, "trigger", P4_STATIC);
114020    sqlite3ChangeCookie(pParse, iDb);
114021    sqlite3VdbeAddOp2(v, OP_Close, 0, 0);
114022    sqlite3VdbeAddOp4(v, OP_DropTrigger, iDb, 0, 0, pTrigger->zName, 0);
114023    if( pParse->nMem<3 ){
114024      pParse->nMem = 3;
114025    }
114026  }
114027}
114028
114029/*
114030** Remove a trigger from the hash tables of the sqlite* pointer.
114031*/
114032SQLITE_PRIVATE void sqlite3UnlinkAndDeleteTrigger(sqlite3 *db, int iDb, const char *zName){
114033  Trigger *pTrigger;
114034  Hash *pHash;
114035
114036  assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
114037  pHash = &(db->aDb[iDb].pSchema->trigHash);
114038  pTrigger = sqlite3HashInsert(pHash, zName, 0);
114039  if( ALWAYS(pTrigger) ){
114040    if( pTrigger->pSchema==pTrigger->pTabSchema ){
114041      Table *pTab = tableOfTrigger(pTrigger);
114042      Trigger **pp;
114043      for(pp=&pTab->pTrigger; *pp!=pTrigger; pp=&((*pp)->pNext));
114044      *pp = (*pp)->pNext;
114045    }
114046    sqlite3DeleteTrigger(db, pTrigger);
114047    db->flags |= SQLITE_InternChanges;
114048  }
114049}
114050
114051/*
114052** pEList is the SET clause of an UPDATE statement.  Each entry
114053** in pEList is of the format <id>=<expr>.  If any of the entries
114054** in pEList have an <id> which matches an identifier in pIdList,
114055** then return TRUE.  If pIdList==NULL, then it is considered a
114056** wildcard that matches anything.  Likewise if pEList==NULL then
114057** it matches anything so always return true.  Return false only
114058** if there is no match.
114059*/
114060static int checkColumnOverlap(IdList *pIdList, ExprList *pEList){
114061  int e;
114062  if( pIdList==0 || NEVER(pEList==0) ) return 1;
114063  for(e=0; e<pEList->nExpr; e++){
114064    if( sqlite3IdListIndex(pIdList, pEList->a[e].zName)>=0 ) return 1;
114065  }
114066  return 0;
114067}
114068
114069/*
114070** Return a list of all triggers on table pTab if there exists at least
114071** one trigger that must be fired when an operation of type 'op' is
114072** performed on the table, and, if that operation is an UPDATE, if at
114073** least one of the columns in pChanges is being modified.
114074*/
114075SQLITE_PRIVATE Trigger *sqlite3TriggersExist(
114076  Parse *pParse,          /* Parse context */
114077  Table *pTab,            /* The table the contains the triggers */
114078  int op,                 /* one of TK_DELETE, TK_INSERT, TK_UPDATE */
114079  ExprList *pChanges,     /* Columns that change in an UPDATE statement */
114080  int *pMask              /* OUT: Mask of TRIGGER_BEFORE|TRIGGER_AFTER */
114081){
114082  int mask = 0;
114083  Trigger *pList = 0;
114084  Trigger *p;
114085
114086  if( (pParse->db->flags & SQLITE_EnableTrigger)!=0 ){
114087    pList = sqlite3TriggerList(pParse, pTab);
114088  }
114089  assert( pList==0 || IsVirtual(pTab)==0 );
114090  for(p=pList; p; p=p->pNext){
114091    if( p->op==op && checkColumnOverlap(p->pColumns, pChanges) ){
114092      mask |= p->tr_tm;
114093    }
114094  }
114095  if( pMask ){
114096    *pMask = mask;
114097  }
114098  return (mask ? pList : 0);
114099}
114100
114101/*
114102** Convert the pStep->zTarget string into a SrcList and return a pointer
114103** to that SrcList.
114104**
114105** This routine adds a specific database name, if needed, to the target when
114106** forming the SrcList.  This prevents a trigger in one database from
114107** referring to a target in another database.  An exception is when the
114108** trigger is in TEMP in which case it can refer to any other database it
114109** wants.
114110*/
114111static SrcList *targetSrcList(
114112  Parse *pParse,       /* The parsing context */
114113  TriggerStep *pStep   /* The trigger containing the target token */
114114){
114115  sqlite3 *db = pParse->db;
114116  int iDb;             /* Index of the database to use */
114117  SrcList *pSrc;       /* SrcList to be returned */
114118
114119  pSrc = sqlite3SrcListAppend(db, 0, 0, 0);
114120  if( pSrc ){
114121    assert( pSrc->nSrc>0 );
114122    pSrc->a[pSrc->nSrc-1].zName = sqlite3DbStrDup(db, pStep->zTarget);
114123    iDb = sqlite3SchemaToIndex(db, pStep->pTrig->pSchema);
114124    if( iDb==0 || iDb>=2 ){
114125      assert( iDb<db->nDb );
114126      pSrc->a[pSrc->nSrc-1].zDatabase = sqlite3DbStrDup(db, db->aDb[iDb].zName);
114127    }
114128  }
114129  return pSrc;
114130}
114131
114132/*
114133** Generate VDBE code for the statements inside the body of a single
114134** trigger.
114135*/
114136static int codeTriggerProgram(
114137  Parse *pParse,            /* The parser context */
114138  TriggerStep *pStepList,   /* List of statements inside the trigger body */
114139  int orconf                /* Conflict algorithm. (OE_Abort, etc) */
114140){
114141  TriggerStep *pStep;
114142  Vdbe *v = pParse->pVdbe;
114143  sqlite3 *db = pParse->db;
114144
114145  assert( pParse->pTriggerTab && pParse->pToplevel );
114146  assert( pStepList );
114147  assert( v!=0 );
114148  for(pStep=pStepList; pStep; pStep=pStep->pNext){
114149    /* Figure out the ON CONFLICT policy that will be used for this step
114150    ** of the trigger program. If the statement that caused this trigger
114151    ** to fire had an explicit ON CONFLICT, then use it. Otherwise, use
114152    ** the ON CONFLICT policy that was specified as part of the trigger
114153    ** step statement. Example:
114154    **
114155    **   CREATE TRIGGER AFTER INSERT ON t1 BEGIN;
114156    **     INSERT OR REPLACE INTO t2 VALUES(new.a, new.b);
114157    **   END;
114158    **
114159    **   INSERT INTO t1 ... ;            -- insert into t2 uses REPLACE policy
114160    **   INSERT OR IGNORE INTO t1 ... ;  -- insert into t2 uses IGNORE policy
114161    */
114162    pParse->eOrconf = (orconf==OE_Default)?pStep->orconf:(u8)orconf;
114163    assert( pParse->okConstFactor==0 );
114164
114165    switch( pStep->op ){
114166      case TK_UPDATE: {
114167        sqlite3Update(pParse,
114168          targetSrcList(pParse, pStep),
114169          sqlite3ExprListDup(db, pStep->pExprList, 0),
114170          sqlite3ExprDup(db, pStep->pWhere, 0),
114171          pParse->eOrconf
114172        );
114173        break;
114174      }
114175      case TK_INSERT: {
114176        sqlite3Insert(pParse,
114177          targetSrcList(pParse, pStep),
114178          sqlite3SelectDup(db, pStep->pSelect, 0),
114179          sqlite3IdListDup(db, pStep->pIdList),
114180          pParse->eOrconf
114181        );
114182        break;
114183      }
114184      case TK_DELETE: {
114185        sqlite3DeleteFrom(pParse,
114186          targetSrcList(pParse, pStep),
114187          sqlite3ExprDup(db, pStep->pWhere, 0)
114188        );
114189        break;
114190      }
114191      default: assert( pStep->op==TK_SELECT ); {
114192        SelectDest sDest;
114193        Select *pSelect = sqlite3SelectDup(db, pStep->pSelect, 0);
114194        sqlite3SelectDestInit(&sDest, SRT_Discard, 0);
114195        sqlite3Select(pParse, pSelect, &sDest);
114196        sqlite3SelectDelete(db, pSelect);
114197        break;
114198      }
114199    }
114200    if( pStep->op!=TK_SELECT ){
114201      sqlite3VdbeAddOp0(v, OP_ResetCount);
114202    }
114203  }
114204
114205  return 0;
114206}
114207
114208#ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
114209/*
114210** This function is used to add VdbeComment() annotations to a VDBE
114211** program. It is not used in production code, only for debugging.
114212*/
114213static const char *onErrorText(int onError){
114214  switch( onError ){
114215    case OE_Abort:    return "abort";
114216    case OE_Rollback: return "rollback";
114217    case OE_Fail:     return "fail";
114218    case OE_Replace:  return "replace";
114219    case OE_Ignore:   return "ignore";
114220    case OE_Default:  return "default";
114221  }
114222  return "n/a";
114223}
114224#endif
114225
114226/*
114227** Parse context structure pFrom has just been used to create a sub-vdbe
114228** (trigger program). If an error has occurred, transfer error information
114229** from pFrom to pTo.
114230*/
114231static void transferParseError(Parse *pTo, Parse *pFrom){
114232  assert( pFrom->zErrMsg==0 || pFrom->nErr );
114233  assert( pTo->zErrMsg==0 || pTo->nErr );
114234  if( pTo->nErr==0 ){
114235    pTo->zErrMsg = pFrom->zErrMsg;
114236    pTo->nErr = pFrom->nErr;
114237    pTo->rc = pFrom->rc;
114238  }else{
114239    sqlite3DbFree(pFrom->db, pFrom->zErrMsg);
114240  }
114241}
114242
114243/*
114244** Create and populate a new TriggerPrg object with a sub-program
114245** implementing trigger pTrigger with ON CONFLICT policy orconf.
114246*/
114247static TriggerPrg *codeRowTrigger(
114248  Parse *pParse,       /* Current parse context */
114249  Trigger *pTrigger,   /* Trigger to code */
114250  Table *pTab,         /* The table pTrigger is attached to */
114251  int orconf           /* ON CONFLICT policy to code trigger program with */
114252){
114253  Parse *pTop = sqlite3ParseToplevel(pParse);
114254  sqlite3 *db = pParse->db;   /* Database handle */
114255  TriggerPrg *pPrg;           /* Value to return */
114256  Expr *pWhen = 0;            /* Duplicate of trigger WHEN expression */
114257  Vdbe *v;                    /* Temporary VM */
114258  NameContext sNC;            /* Name context for sub-vdbe */
114259  SubProgram *pProgram = 0;   /* Sub-vdbe for trigger program */
114260  Parse *pSubParse;           /* Parse context for sub-vdbe */
114261  int iEndTrigger = 0;        /* Label to jump to if WHEN is false */
114262
114263  assert( pTrigger->zName==0 || pTab==tableOfTrigger(pTrigger) );
114264  assert( pTop->pVdbe );
114265
114266  /* Allocate the TriggerPrg and SubProgram objects. To ensure that they
114267  ** are freed if an error occurs, link them into the Parse.pTriggerPrg
114268  ** list of the top-level Parse object sooner rather than later.  */
114269  pPrg = sqlite3DbMallocZero(db, sizeof(TriggerPrg));
114270  if( !pPrg ) return 0;
114271  pPrg->pNext = pTop->pTriggerPrg;
114272  pTop->pTriggerPrg = pPrg;
114273  pPrg->pProgram = pProgram = sqlite3DbMallocZero(db, sizeof(SubProgram));
114274  if( !pProgram ) return 0;
114275  sqlite3VdbeLinkSubProgram(pTop->pVdbe, pProgram);
114276  pPrg->pTrigger = pTrigger;
114277  pPrg->orconf = orconf;
114278  pPrg->aColmask[0] = 0xffffffff;
114279  pPrg->aColmask[1] = 0xffffffff;
114280
114281  /* Allocate and populate a new Parse context to use for coding the
114282  ** trigger sub-program.  */
114283  pSubParse = sqlite3StackAllocZero(db, sizeof(Parse));
114284  if( !pSubParse ) return 0;
114285  memset(&sNC, 0, sizeof(sNC));
114286  sNC.pParse = pSubParse;
114287  pSubParse->db = db;
114288  pSubParse->pTriggerTab = pTab;
114289  pSubParse->pToplevel = pTop;
114290  pSubParse->zAuthContext = pTrigger->zName;
114291  pSubParse->eTriggerOp = pTrigger->op;
114292  pSubParse->nQueryLoop = pParse->nQueryLoop;
114293
114294  v = sqlite3GetVdbe(pSubParse);
114295  if( v ){
114296    VdbeComment((v, "Start: %s.%s (%s %s%s%s ON %s)",
114297      pTrigger->zName, onErrorText(orconf),
114298      (pTrigger->tr_tm==TRIGGER_BEFORE ? "BEFORE" : "AFTER"),
114299        (pTrigger->op==TK_UPDATE ? "UPDATE" : ""),
114300        (pTrigger->op==TK_INSERT ? "INSERT" : ""),
114301        (pTrigger->op==TK_DELETE ? "DELETE" : ""),
114302      pTab->zName
114303    ));
114304#ifndef SQLITE_OMIT_TRACE
114305    sqlite3VdbeChangeP4(v, -1,
114306      sqlite3MPrintf(db, "-- TRIGGER %s", pTrigger->zName), P4_DYNAMIC
114307    );
114308#endif
114309
114310    /* If one was specified, code the WHEN clause. If it evaluates to false
114311    ** (or NULL) the sub-vdbe is immediately halted by jumping to the
114312    ** OP_Halt inserted at the end of the program.  */
114313    if( pTrigger->pWhen ){
114314      pWhen = sqlite3ExprDup(db, pTrigger->pWhen, 0);
114315      if( SQLITE_OK==sqlite3ResolveExprNames(&sNC, pWhen)
114316       && db->mallocFailed==0
114317      ){
114318        iEndTrigger = sqlite3VdbeMakeLabel(v);
114319        sqlite3ExprIfFalse(pSubParse, pWhen, iEndTrigger, SQLITE_JUMPIFNULL);
114320      }
114321      sqlite3ExprDelete(db, pWhen);
114322    }
114323
114324    /* Code the trigger program into the sub-vdbe. */
114325    codeTriggerProgram(pSubParse, pTrigger->step_list, orconf);
114326
114327    /* Insert an OP_Halt at the end of the sub-program. */
114328    if( iEndTrigger ){
114329      sqlite3VdbeResolveLabel(v, iEndTrigger);
114330    }
114331    sqlite3VdbeAddOp0(v, OP_Halt);
114332    VdbeComment((v, "End: %s.%s", pTrigger->zName, onErrorText(orconf)));
114333
114334    transferParseError(pParse, pSubParse);
114335    if( db->mallocFailed==0 ){
114336      pProgram->aOp = sqlite3VdbeTakeOpArray(v, &pProgram->nOp, &pTop->nMaxArg);
114337    }
114338    pProgram->nMem = pSubParse->nMem;
114339    pProgram->nCsr = pSubParse->nTab;
114340    pProgram->nOnce = pSubParse->nOnce;
114341    pProgram->token = (void *)pTrigger;
114342    pPrg->aColmask[0] = pSubParse->oldmask;
114343    pPrg->aColmask[1] = pSubParse->newmask;
114344    sqlite3VdbeDelete(v);
114345  }
114346
114347  assert( !pSubParse->pAinc       && !pSubParse->pZombieTab );
114348  assert( !pSubParse->pTriggerPrg && !pSubParse->nMaxArg );
114349  sqlite3ParserReset(pSubParse);
114350  sqlite3StackFree(db, pSubParse);
114351
114352  return pPrg;
114353}
114354
114355/*
114356** Return a pointer to a TriggerPrg object containing the sub-program for
114357** trigger pTrigger with default ON CONFLICT algorithm orconf. If no such
114358** TriggerPrg object exists, a new object is allocated and populated before
114359** being returned.
114360*/
114361static TriggerPrg *getRowTrigger(
114362  Parse *pParse,       /* Current parse context */
114363  Trigger *pTrigger,   /* Trigger to code */
114364  Table *pTab,         /* The table trigger pTrigger is attached to */
114365  int orconf           /* ON CONFLICT algorithm. */
114366){
114367  Parse *pRoot = sqlite3ParseToplevel(pParse);
114368  TriggerPrg *pPrg;
114369
114370  assert( pTrigger->zName==0 || pTab==tableOfTrigger(pTrigger) );
114371
114372  /* It may be that this trigger has already been coded (or is in the
114373  ** process of being coded). If this is the case, then an entry with
114374  ** a matching TriggerPrg.pTrigger field will be present somewhere
114375  ** in the Parse.pTriggerPrg list. Search for such an entry.  */
114376  for(pPrg=pRoot->pTriggerPrg;
114377      pPrg && (pPrg->pTrigger!=pTrigger || pPrg->orconf!=orconf);
114378      pPrg=pPrg->pNext
114379  );
114380
114381  /* If an existing TriggerPrg could not be located, create a new one. */
114382  if( !pPrg ){
114383    pPrg = codeRowTrigger(pParse, pTrigger, pTab, orconf);
114384  }
114385
114386  return pPrg;
114387}
114388
114389/*
114390** Generate code for the trigger program associated with trigger p on
114391** table pTab. The reg, orconf and ignoreJump parameters passed to this
114392** function are the same as those described in the header function for
114393** sqlite3CodeRowTrigger()
114394*/
114395SQLITE_PRIVATE void sqlite3CodeRowTriggerDirect(
114396  Parse *pParse,       /* Parse context */
114397  Trigger *p,          /* Trigger to code */
114398  Table *pTab,         /* The table to code triggers from */
114399  int reg,             /* Reg array containing OLD.* and NEW.* values */
114400  int orconf,          /* ON CONFLICT policy */
114401  int ignoreJump       /* Instruction to jump to for RAISE(IGNORE) */
114402){
114403  Vdbe *v = sqlite3GetVdbe(pParse); /* Main VM */
114404  TriggerPrg *pPrg;
114405  pPrg = getRowTrigger(pParse, p, pTab, orconf);
114406  assert( pPrg || pParse->nErr || pParse->db->mallocFailed );
114407
114408  /* Code the OP_Program opcode in the parent VDBE. P4 of the OP_Program
114409  ** is a pointer to the sub-vdbe containing the trigger program.  */
114410  if( pPrg ){
114411    int bRecursive = (p->zName && 0==(pParse->db->flags&SQLITE_RecTriggers));
114412
114413    sqlite3VdbeAddOp3(v, OP_Program, reg, ignoreJump, ++pParse->nMem);
114414    sqlite3VdbeChangeP4(v, -1, (const char *)pPrg->pProgram, P4_SUBPROGRAM);
114415    VdbeComment(
114416        (v, "Call: %s.%s", (p->zName?p->zName:"fkey"), onErrorText(orconf)));
114417
114418    /* Set the P5 operand of the OP_Program instruction to non-zero if
114419    ** recursive invocation of this trigger program is disallowed. Recursive
114420    ** invocation is disallowed if (a) the sub-program is really a trigger,
114421    ** not a foreign key action, and (b) the flag to enable recursive triggers
114422    ** is clear.  */
114423    sqlite3VdbeChangeP5(v, (u8)bRecursive);
114424  }
114425}
114426
114427/*
114428** This is called to code the required FOR EACH ROW triggers for an operation
114429** on table pTab. The operation to code triggers for (INSERT, UPDATE or DELETE)
114430** is given by the op parameter. The tr_tm parameter determines whether the
114431** BEFORE or AFTER triggers are coded. If the operation is an UPDATE, then
114432** parameter pChanges is passed the list of columns being modified.
114433**
114434** If there are no triggers that fire at the specified time for the specified
114435** operation on pTab, this function is a no-op.
114436**
114437** The reg argument is the address of the first in an array of registers
114438** that contain the values substituted for the new.* and old.* references
114439** in the trigger program. If N is the number of columns in table pTab
114440** (a copy of pTab->nCol), then registers are populated as follows:
114441**
114442**   Register       Contains
114443**   ------------------------------------------------------
114444**   reg+0          OLD.rowid
114445**   reg+1          OLD.* value of left-most column of pTab
114446**   ...            ...
114447**   reg+N          OLD.* value of right-most column of pTab
114448**   reg+N+1        NEW.rowid
114449**   reg+N+2        OLD.* value of left-most column of pTab
114450**   ...            ...
114451**   reg+N+N+1      NEW.* value of right-most column of pTab
114452**
114453** For ON DELETE triggers, the registers containing the NEW.* values will
114454** never be accessed by the trigger program, so they are not allocated or
114455** populated by the caller (there is no data to populate them with anyway).
114456** Similarly, for ON INSERT triggers the values stored in the OLD.* registers
114457** are never accessed, and so are not allocated by the caller. So, for an
114458** ON INSERT trigger, the value passed to this function as parameter reg
114459** is not a readable register, although registers (reg+N) through
114460** (reg+N+N+1) are.
114461**
114462** Parameter orconf is the default conflict resolution algorithm for the
114463** trigger program to use (REPLACE, IGNORE etc.). Parameter ignoreJump
114464** is the instruction that control should jump to if a trigger program
114465** raises an IGNORE exception.
114466*/
114467SQLITE_PRIVATE void sqlite3CodeRowTrigger(
114468  Parse *pParse,       /* Parse context */
114469  Trigger *pTrigger,   /* List of triggers on table pTab */
114470  int op,              /* One of TK_UPDATE, TK_INSERT, TK_DELETE */
114471  ExprList *pChanges,  /* Changes list for any UPDATE OF triggers */
114472  int tr_tm,           /* One of TRIGGER_BEFORE, TRIGGER_AFTER */
114473  Table *pTab,         /* The table to code triggers from */
114474  int reg,             /* The first in an array of registers (see above) */
114475  int orconf,          /* ON CONFLICT policy */
114476  int ignoreJump       /* Instruction to jump to for RAISE(IGNORE) */
114477){
114478  Trigger *p;          /* Used to iterate through pTrigger list */
114479
114480  assert( op==TK_UPDATE || op==TK_INSERT || op==TK_DELETE );
114481  assert( tr_tm==TRIGGER_BEFORE || tr_tm==TRIGGER_AFTER );
114482  assert( (op==TK_UPDATE)==(pChanges!=0) );
114483
114484  for(p=pTrigger; p; p=p->pNext){
114485
114486    /* Sanity checking:  The schema for the trigger and for the table are
114487    ** always defined.  The trigger must be in the same schema as the table
114488    ** or else it must be a TEMP trigger. */
114489    assert( p->pSchema!=0 );
114490    assert( p->pTabSchema!=0 );
114491    assert( p->pSchema==p->pTabSchema
114492         || p->pSchema==pParse->db->aDb[1].pSchema );
114493
114494    /* Determine whether we should code this trigger */
114495    if( p->op==op
114496     && p->tr_tm==tr_tm
114497     && checkColumnOverlap(p->pColumns, pChanges)
114498    ){
114499      sqlite3CodeRowTriggerDirect(pParse, p, pTab, reg, orconf, ignoreJump);
114500    }
114501  }
114502}
114503
114504/*
114505** Triggers may access values stored in the old.* or new.* pseudo-table.
114506** This function returns a 32-bit bitmask indicating which columns of the
114507** old.* or new.* tables actually are used by triggers. This information
114508** may be used by the caller, for example, to avoid having to load the entire
114509** old.* record into memory when executing an UPDATE or DELETE command.
114510**
114511** Bit 0 of the returned mask is set if the left-most column of the
114512** table may be accessed using an [old|new].<col> reference. Bit 1 is set if
114513** the second leftmost column value is required, and so on. If there
114514** are more than 32 columns in the table, and at least one of the columns
114515** with an index greater than 32 may be accessed, 0xffffffff is returned.
114516**
114517** It is not possible to determine if the old.rowid or new.rowid column is
114518** accessed by triggers. The caller must always assume that it is.
114519**
114520** Parameter isNew must be either 1 or 0. If it is 0, then the mask returned
114521** applies to the old.* table. If 1, the new.* table.
114522**
114523** Parameter tr_tm must be a mask with one or both of the TRIGGER_BEFORE
114524** and TRIGGER_AFTER bits set. Values accessed by BEFORE triggers are only
114525** included in the returned mask if the TRIGGER_BEFORE bit is set in the
114526** tr_tm parameter. Similarly, values accessed by AFTER triggers are only
114527** included in the returned mask if the TRIGGER_AFTER bit is set in tr_tm.
114528*/
114529SQLITE_PRIVATE u32 sqlite3TriggerColmask(
114530  Parse *pParse,       /* Parse context */
114531  Trigger *pTrigger,   /* List of triggers on table pTab */
114532  ExprList *pChanges,  /* Changes list for any UPDATE OF triggers */
114533  int isNew,           /* 1 for new.* ref mask, 0 for old.* ref mask */
114534  int tr_tm,           /* Mask of TRIGGER_BEFORE|TRIGGER_AFTER */
114535  Table *pTab,         /* The table to code triggers from */
114536  int orconf           /* Default ON CONFLICT policy for trigger steps */
114537){
114538  const int op = pChanges ? TK_UPDATE : TK_DELETE;
114539  u32 mask = 0;
114540  Trigger *p;
114541
114542  assert( isNew==1 || isNew==0 );
114543  for(p=pTrigger; p; p=p->pNext){
114544    if( p->op==op && (tr_tm&p->tr_tm)
114545     && checkColumnOverlap(p->pColumns,pChanges)
114546    ){
114547      TriggerPrg *pPrg;
114548      pPrg = getRowTrigger(pParse, p, pTab, orconf);
114549      if( pPrg ){
114550        mask |= pPrg->aColmask[isNew];
114551      }
114552    }
114553  }
114554
114555  return mask;
114556}
114557
114558#endif /* !defined(SQLITE_OMIT_TRIGGER) */
114559
114560/************** End of trigger.c *********************************************/
114561/************** Begin file update.c ******************************************/
114562/*
114563** 2001 September 15
114564**
114565** The author disclaims copyright to this source code.  In place of
114566** a legal notice, here is a blessing:
114567**
114568**    May you do good and not evil.
114569**    May you find forgiveness for yourself and forgive others.
114570**    May you share freely, never taking more than you give.
114571**
114572*************************************************************************
114573** This file contains C code routines that are called by the parser
114574** to handle UPDATE statements.
114575*/
114576/* #include "sqliteInt.h" */
114577
114578#ifndef SQLITE_OMIT_VIRTUALTABLE
114579/* Forward declaration */
114580static void updateVirtualTable(
114581  Parse *pParse,       /* The parsing context */
114582  SrcList *pSrc,       /* The virtual table to be modified */
114583  Table *pTab,         /* The virtual table */
114584  ExprList *pChanges,  /* The columns to change in the UPDATE statement */
114585  Expr *pRowidExpr,    /* Expression used to recompute the rowid */
114586  int *aXRef,          /* Mapping from columns of pTab to entries in pChanges */
114587  Expr *pWhere,        /* WHERE clause of the UPDATE statement */
114588  int onError          /* ON CONFLICT strategy */
114589);
114590#endif /* SQLITE_OMIT_VIRTUALTABLE */
114591
114592/*
114593** The most recently coded instruction was an OP_Column to retrieve the
114594** i-th column of table pTab. This routine sets the P4 parameter of the
114595** OP_Column to the default value, if any.
114596**
114597** The default value of a column is specified by a DEFAULT clause in the
114598** column definition. This was either supplied by the user when the table
114599** was created, or added later to the table definition by an ALTER TABLE
114600** command. If the latter, then the row-records in the table btree on disk
114601** may not contain a value for the column and the default value, taken
114602** from the P4 parameter of the OP_Column instruction, is returned instead.
114603** If the former, then all row-records are guaranteed to include a value
114604** for the column and the P4 value is not required.
114605**
114606** Column definitions created by an ALTER TABLE command may only have
114607** literal default values specified: a number, null or a string. (If a more
114608** complicated default expression value was provided, it is evaluated
114609** when the ALTER TABLE is executed and one of the literal values written
114610** into the sqlite_master table.)
114611**
114612** Therefore, the P4 parameter is only required if the default value for
114613** the column is a literal number, string or null. The sqlite3ValueFromExpr()
114614** function is capable of transforming these types of expressions into
114615** sqlite3_value objects.
114616**
114617** If parameter iReg is not negative, code an OP_RealAffinity instruction
114618** on register iReg. This is used when an equivalent integer value is
114619** stored in place of an 8-byte floating point value in order to save
114620** space.
114621*/
114622SQLITE_PRIVATE void sqlite3ColumnDefault(Vdbe *v, Table *pTab, int i, int iReg){
114623  assert( pTab!=0 );
114624  if( !pTab->pSelect ){
114625    sqlite3_value *pValue = 0;
114626    u8 enc = ENC(sqlite3VdbeDb(v));
114627    Column *pCol = &pTab->aCol[i];
114628    VdbeComment((v, "%s.%s", pTab->zName, pCol->zName));
114629    assert( i<pTab->nCol );
114630    sqlite3ValueFromExpr(sqlite3VdbeDb(v), pCol->pDflt, enc,
114631                         pCol->affinity, &pValue);
114632    if( pValue ){
114633      sqlite3VdbeChangeP4(v, -1, (const char *)pValue, P4_MEM);
114634    }
114635#ifndef SQLITE_OMIT_FLOATING_POINT
114636    if( pTab->aCol[i].affinity==SQLITE_AFF_REAL ){
114637      sqlite3VdbeAddOp1(v, OP_RealAffinity, iReg);
114638    }
114639#endif
114640  }
114641}
114642
114643/*
114644** Process an UPDATE statement.
114645**
114646**   UPDATE OR IGNORE table_wxyz SET a=b, c=d WHERE e<5 AND f NOT NULL;
114647**          \_______/ \________/     \______/       \________________/
114648*            onError   pTabList      pChanges             pWhere
114649*/
114650SQLITE_PRIVATE void sqlite3Update(
114651  Parse *pParse,         /* The parser context */
114652  SrcList *pTabList,     /* The table in which we should change things */
114653  ExprList *pChanges,    /* Things to be changed */
114654  Expr *pWhere,          /* The WHERE clause.  May be null */
114655  int onError            /* How to handle constraint errors */
114656){
114657  int i, j;              /* Loop counters */
114658  Table *pTab;           /* The table to be updated */
114659  int addrTop = 0;       /* VDBE instruction address of the start of the loop */
114660  WhereInfo *pWInfo;     /* Information about the WHERE clause */
114661  Vdbe *v;               /* The virtual database engine */
114662  Index *pIdx;           /* For looping over indices */
114663  Index *pPk;            /* The PRIMARY KEY index for WITHOUT ROWID tables */
114664  int nIdx;              /* Number of indices that need updating */
114665  int iBaseCur;          /* Base cursor number */
114666  int iDataCur;          /* Cursor for the canonical data btree */
114667  int iIdxCur;           /* Cursor for the first index */
114668  sqlite3 *db;           /* The database structure */
114669  int *aRegIdx = 0;      /* One register assigned to each index to be updated */
114670  int *aXRef = 0;        /* aXRef[i] is the index in pChanges->a[] of the
114671                         ** an expression for the i-th column of the table.
114672                         ** aXRef[i]==-1 if the i-th column is not changed. */
114673  u8 *aToOpen;           /* 1 for tables and indices to be opened */
114674  u8 chngPk;             /* PRIMARY KEY changed in a WITHOUT ROWID table */
114675  u8 chngRowid;          /* Rowid changed in a normal table */
114676  u8 chngKey;            /* Either chngPk or chngRowid */
114677  Expr *pRowidExpr = 0;  /* Expression defining the new record number */
114678  AuthContext sContext;  /* The authorization context */
114679  NameContext sNC;       /* The name-context to resolve expressions in */
114680  int iDb;               /* Database containing the table being updated */
114681  int okOnePass;         /* True for one-pass algorithm without the FIFO */
114682  int hasFK;             /* True if foreign key processing is required */
114683  int labelBreak;        /* Jump here to break out of UPDATE loop */
114684  int labelContinue;     /* Jump here to continue next step of UPDATE loop */
114685
114686#ifndef SQLITE_OMIT_TRIGGER
114687  int isView;            /* True when updating a view (INSTEAD OF trigger) */
114688  Trigger *pTrigger;     /* List of triggers on pTab, if required */
114689  int tmask;             /* Mask of TRIGGER_BEFORE|TRIGGER_AFTER */
114690#endif
114691  int newmask;           /* Mask of NEW.* columns accessed by BEFORE triggers */
114692  int iEph = 0;          /* Ephemeral table holding all primary key values */
114693  int nKey = 0;          /* Number of elements in regKey for WITHOUT ROWID */
114694  int aiCurOnePass[2];   /* The write cursors opened by WHERE_ONEPASS */
114695
114696  /* Register Allocations */
114697  int regRowCount = 0;   /* A count of rows changed */
114698  int regOldRowid;       /* The old rowid */
114699  int regNewRowid;       /* The new rowid */
114700  int regNew;            /* Content of the NEW.* table in triggers */
114701  int regOld = 0;        /* Content of OLD.* table in triggers */
114702  int regRowSet = 0;     /* Rowset of rows to be updated */
114703  int regKey = 0;        /* composite PRIMARY KEY value */
114704
114705  memset(&sContext, 0, sizeof(sContext));
114706  db = pParse->db;
114707  if( pParse->nErr || db->mallocFailed ){
114708    goto update_cleanup;
114709  }
114710  assert( pTabList->nSrc==1 );
114711
114712  /* Locate the table which we want to update.
114713  */
114714  pTab = sqlite3SrcListLookup(pParse, pTabList);
114715  if( pTab==0 ) goto update_cleanup;
114716  iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
114717
114718  /* Figure out if we have any triggers and if the table being
114719  ** updated is a view.
114720  */
114721#ifndef SQLITE_OMIT_TRIGGER
114722  pTrigger = sqlite3TriggersExist(pParse, pTab, TK_UPDATE, pChanges, &tmask);
114723  isView = pTab->pSelect!=0;
114724  assert( pTrigger || tmask==0 );
114725#else
114726# define pTrigger 0
114727# define isView 0
114728# define tmask 0
114729#endif
114730#ifdef SQLITE_OMIT_VIEW
114731# undef isView
114732# define isView 0
114733#endif
114734
114735  if( sqlite3ViewGetColumnNames(pParse, pTab) ){
114736    goto update_cleanup;
114737  }
114738  if( sqlite3IsReadOnly(pParse, pTab, tmask) ){
114739    goto update_cleanup;
114740  }
114741
114742  /* Allocate a cursors for the main database table and for all indices.
114743  ** The index cursors might not be used, but if they are used they
114744  ** need to occur right after the database cursor.  So go ahead and
114745  ** allocate enough space, just in case.
114746  */
114747  pTabList->a[0].iCursor = iBaseCur = iDataCur = pParse->nTab++;
114748  iIdxCur = iDataCur+1;
114749  pPk = HasRowid(pTab) ? 0 : sqlite3PrimaryKeyIndex(pTab);
114750  for(nIdx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nIdx++){
114751    if( IsPrimaryKeyIndex(pIdx) && pPk!=0 ){
114752      iDataCur = pParse->nTab;
114753      pTabList->a[0].iCursor = iDataCur;
114754    }
114755    pParse->nTab++;
114756  }
114757
114758  /* Allocate space for aXRef[], aRegIdx[], and aToOpen[].
114759  ** Initialize aXRef[] and aToOpen[] to their default values.
114760  */
114761  aXRef = sqlite3DbMallocRaw(db, sizeof(int) * (pTab->nCol+nIdx) + nIdx+2 );
114762  if( aXRef==0 ) goto update_cleanup;
114763  aRegIdx = aXRef+pTab->nCol;
114764  aToOpen = (u8*)(aRegIdx+nIdx);
114765  memset(aToOpen, 1, nIdx+1);
114766  aToOpen[nIdx+1] = 0;
114767  for(i=0; i<pTab->nCol; i++) aXRef[i] = -1;
114768
114769  /* Initialize the name-context */
114770  memset(&sNC, 0, sizeof(sNC));
114771  sNC.pParse = pParse;
114772  sNC.pSrcList = pTabList;
114773
114774  /* Resolve the column names in all the expressions of the
114775  ** of the UPDATE statement.  Also find the column index
114776  ** for each column to be updated in the pChanges array.  For each
114777  ** column to be updated, make sure we have authorization to change
114778  ** that column.
114779  */
114780  chngRowid = chngPk = 0;
114781  for(i=0; i<pChanges->nExpr; i++){
114782    if( sqlite3ResolveExprNames(&sNC, pChanges->a[i].pExpr) ){
114783      goto update_cleanup;
114784    }
114785    for(j=0; j<pTab->nCol; j++){
114786      if( sqlite3StrICmp(pTab->aCol[j].zName, pChanges->a[i].zName)==0 ){
114787        if( j==pTab->iPKey ){
114788          chngRowid = 1;
114789          pRowidExpr = pChanges->a[i].pExpr;
114790        }else if( pPk && (pTab->aCol[j].colFlags & COLFLAG_PRIMKEY)!=0 ){
114791          chngPk = 1;
114792        }
114793        aXRef[j] = i;
114794        break;
114795      }
114796    }
114797    if( j>=pTab->nCol ){
114798      if( pPk==0 && sqlite3IsRowid(pChanges->a[i].zName) ){
114799        j = -1;
114800        chngRowid = 1;
114801        pRowidExpr = pChanges->a[i].pExpr;
114802      }else{
114803        sqlite3ErrorMsg(pParse, "no such column: %s", pChanges->a[i].zName);
114804        pParse->checkSchema = 1;
114805        goto update_cleanup;
114806      }
114807    }
114808#ifndef SQLITE_OMIT_AUTHORIZATION
114809    {
114810      int rc;
114811      rc = sqlite3AuthCheck(pParse, SQLITE_UPDATE, pTab->zName,
114812                            j<0 ? "ROWID" : pTab->aCol[j].zName,
114813                            db->aDb[iDb].zName);
114814      if( rc==SQLITE_DENY ){
114815        goto update_cleanup;
114816      }else if( rc==SQLITE_IGNORE ){
114817        aXRef[j] = -1;
114818      }
114819    }
114820#endif
114821  }
114822  assert( (chngRowid & chngPk)==0 );
114823  assert( chngRowid==0 || chngRowid==1 );
114824  assert( chngPk==0 || chngPk==1 );
114825  chngKey = chngRowid + chngPk;
114826
114827  /* The SET expressions are not actually used inside the WHERE loop.
114828  ** So reset the colUsed mask
114829  */
114830  pTabList->a[0].colUsed = 0;
114831
114832  hasFK = sqlite3FkRequired(pParse, pTab, aXRef, chngKey);
114833
114834  /* There is one entry in the aRegIdx[] array for each index on the table
114835  ** being updated.  Fill in aRegIdx[] with a register number that will hold
114836  ** the key for accessing each index.
114837  */
114838  for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
114839    int reg;
114840    if( chngKey || hasFK || pIdx->pPartIdxWhere || pIdx==pPk ){
114841      reg = ++pParse->nMem;
114842    }else{
114843      reg = 0;
114844      for(i=0; i<pIdx->nKeyCol; i++){
114845        if( aXRef[pIdx->aiColumn[i]]>=0 ){
114846          reg = ++pParse->nMem;
114847          break;
114848        }
114849      }
114850    }
114851    if( reg==0 ) aToOpen[j+1] = 0;
114852    aRegIdx[j] = reg;
114853  }
114854
114855  /* Begin generating code. */
114856  v = sqlite3GetVdbe(pParse);
114857  if( v==0 ) goto update_cleanup;
114858  if( pParse->nested==0 ) sqlite3VdbeCountChanges(v);
114859  sqlite3BeginWriteOperation(pParse, 1, iDb);
114860
114861#ifndef SQLITE_OMIT_VIRTUALTABLE
114862  /* Virtual tables must be handled separately */
114863  if( IsVirtual(pTab) ){
114864    updateVirtualTable(pParse, pTabList, pTab, pChanges, pRowidExpr, aXRef,
114865                       pWhere, onError);
114866    pWhere = 0;
114867    pTabList = 0;
114868    goto update_cleanup;
114869  }
114870#endif
114871
114872  /* Allocate required registers. */
114873  regRowSet = ++pParse->nMem;
114874  regOldRowid = regNewRowid = ++pParse->nMem;
114875  if( chngPk || pTrigger || hasFK ){
114876    regOld = pParse->nMem + 1;
114877    pParse->nMem += pTab->nCol;
114878  }
114879  if( chngKey || pTrigger || hasFK ){
114880    regNewRowid = ++pParse->nMem;
114881  }
114882  regNew = pParse->nMem + 1;
114883  pParse->nMem += pTab->nCol;
114884
114885  /* Start the view context. */
114886  if( isView ){
114887    sqlite3AuthContextPush(pParse, &sContext, pTab->zName);
114888  }
114889
114890  /* If we are trying to update a view, realize that view into
114891  ** an ephemeral table.
114892  */
114893#if !defined(SQLITE_OMIT_VIEW) && !defined(SQLITE_OMIT_TRIGGER)
114894  if( isView ){
114895    sqlite3MaterializeView(pParse, pTab, pWhere, iDataCur);
114896  }
114897#endif
114898
114899  /* Resolve the column names in all the expressions in the
114900  ** WHERE clause.
114901  */
114902  if( sqlite3ResolveExprNames(&sNC, pWhere) ){
114903    goto update_cleanup;
114904  }
114905
114906  /* Begin the database scan
114907  */
114908  if( HasRowid(pTab) ){
114909    sqlite3VdbeAddOp3(v, OP_Null, 0, regRowSet, regOldRowid);
114910    pWInfo = sqlite3WhereBegin(
114911        pParse, pTabList, pWhere, 0, 0, WHERE_ONEPASS_DESIRED, iIdxCur
114912    );
114913    if( pWInfo==0 ) goto update_cleanup;
114914    okOnePass = sqlite3WhereOkOnePass(pWInfo, aiCurOnePass);
114915
114916    /* Remember the rowid of every item to be updated.
114917    */
114918    sqlite3VdbeAddOp2(v, OP_Rowid, iDataCur, regOldRowid);
114919    if( !okOnePass ){
114920      sqlite3VdbeAddOp2(v, OP_RowSetAdd, regRowSet, regOldRowid);
114921    }
114922
114923    /* End the database scan loop.
114924    */
114925    sqlite3WhereEnd(pWInfo);
114926  }else{
114927    int iPk;         /* First of nPk memory cells holding PRIMARY KEY value */
114928    i16 nPk;         /* Number of components of the PRIMARY KEY */
114929    int addrOpen;    /* Address of the OpenEphemeral instruction */
114930
114931    assert( pPk!=0 );
114932    nPk = pPk->nKeyCol;
114933    iPk = pParse->nMem+1;
114934    pParse->nMem += nPk;
114935    regKey = ++pParse->nMem;
114936    iEph = pParse->nTab++;
114937    sqlite3VdbeAddOp2(v, OP_Null, 0, iPk);
114938    addrOpen = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, iEph, nPk);
114939    sqlite3VdbeSetP4KeyInfo(pParse, pPk);
114940    pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, 0, 0,
114941                               WHERE_ONEPASS_DESIRED, iIdxCur);
114942    if( pWInfo==0 ) goto update_cleanup;
114943    okOnePass = sqlite3WhereOkOnePass(pWInfo, aiCurOnePass);
114944    for(i=0; i<nPk; i++){
114945      sqlite3ExprCodeGetColumnOfTable(v, pTab, iDataCur, pPk->aiColumn[i],
114946                                      iPk+i);
114947    }
114948    if( okOnePass ){
114949      sqlite3VdbeChangeToNoop(v, addrOpen);
114950      nKey = nPk;
114951      regKey = iPk;
114952    }else{
114953      sqlite3VdbeAddOp4(v, OP_MakeRecord, iPk, nPk, regKey,
114954                        sqlite3IndexAffinityStr(v, pPk), nPk);
114955      sqlite3VdbeAddOp2(v, OP_IdxInsert, iEph, regKey);
114956    }
114957    sqlite3WhereEnd(pWInfo);
114958  }
114959
114960  /* Initialize the count of updated rows
114961  */
114962  if( (db->flags & SQLITE_CountRows) && !pParse->pTriggerTab ){
114963    regRowCount = ++pParse->nMem;
114964    sqlite3VdbeAddOp2(v, OP_Integer, 0, regRowCount);
114965  }
114966
114967  labelBreak = sqlite3VdbeMakeLabel(v);
114968  if( !isView ){
114969    /*
114970    ** Open every index that needs updating.  Note that if any
114971    ** index could potentially invoke a REPLACE conflict resolution
114972    ** action, then we need to open all indices because we might need
114973    ** to be deleting some records.
114974    */
114975    if( onError==OE_Replace ){
114976      memset(aToOpen, 1, nIdx+1);
114977    }else{
114978      for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
114979        if( pIdx->onError==OE_Replace ){
114980          memset(aToOpen, 1, nIdx+1);
114981          break;
114982        }
114983      }
114984    }
114985    if( okOnePass ){
114986      if( aiCurOnePass[0]>=0 ) aToOpen[aiCurOnePass[0]-iBaseCur] = 0;
114987      if( aiCurOnePass[1]>=0 ) aToOpen[aiCurOnePass[1]-iBaseCur] = 0;
114988    }
114989    sqlite3OpenTableAndIndices(pParse, pTab, OP_OpenWrite, iBaseCur, aToOpen,
114990                               0, 0);
114991  }
114992
114993  /* Top of the update loop */
114994  if( okOnePass ){
114995    if( aToOpen[iDataCur-iBaseCur] && !isView ){
114996      assert( pPk );
114997      sqlite3VdbeAddOp4Int(v, OP_NotFound, iDataCur, labelBreak, regKey, nKey);
114998      VdbeCoverageNeverTaken(v);
114999    }
115000    labelContinue = labelBreak;
115001    sqlite3VdbeAddOp2(v, OP_IsNull, pPk ? regKey : regOldRowid, labelBreak);
115002    VdbeCoverageIf(v, pPk==0);
115003    VdbeCoverageIf(v, pPk!=0);
115004  }else if( pPk ){
115005    labelContinue = sqlite3VdbeMakeLabel(v);
115006    sqlite3VdbeAddOp2(v, OP_Rewind, iEph, labelBreak); VdbeCoverage(v);
115007    addrTop = sqlite3VdbeAddOp2(v, OP_RowKey, iEph, regKey);
115008    sqlite3VdbeAddOp4Int(v, OP_NotFound, iDataCur, labelContinue, regKey, 0);
115009    VdbeCoverage(v);
115010  }else{
115011    labelContinue = sqlite3VdbeAddOp3(v, OP_RowSetRead, regRowSet, labelBreak,
115012                             regOldRowid);
115013    VdbeCoverage(v);
115014    sqlite3VdbeAddOp3(v, OP_NotExists, iDataCur, labelContinue, regOldRowid);
115015    VdbeCoverage(v);
115016  }
115017
115018  /* If the record number will change, set register regNewRowid to
115019  ** contain the new value. If the record number is not being modified,
115020  ** then regNewRowid is the same register as regOldRowid, which is
115021  ** already populated.  */
115022  assert( chngKey || pTrigger || hasFK || regOldRowid==regNewRowid );
115023  if( chngRowid ){
115024    sqlite3ExprCode(pParse, pRowidExpr, regNewRowid);
115025    sqlite3VdbeAddOp1(v, OP_MustBeInt, regNewRowid); VdbeCoverage(v);
115026  }
115027
115028  /* Compute the old pre-UPDATE content of the row being changed, if that
115029  ** information is needed */
115030  if( chngPk || hasFK || pTrigger ){
115031    u32 oldmask = (hasFK ? sqlite3FkOldmask(pParse, pTab) : 0);
115032    oldmask |= sqlite3TriggerColmask(pParse,
115033        pTrigger, pChanges, 0, TRIGGER_BEFORE|TRIGGER_AFTER, pTab, onError
115034    );
115035    for(i=0; i<pTab->nCol; i++){
115036      if( oldmask==0xffffffff
115037       || (i<32 && (oldmask & MASKBIT32(i))!=0)
115038       || (pTab->aCol[i].colFlags & COLFLAG_PRIMKEY)!=0
115039      ){
115040        testcase(  oldmask!=0xffffffff && i==31 );
115041        sqlite3ExprCodeGetColumnOfTable(v, pTab, iDataCur, i, regOld+i);
115042      }else{
115043        sqlite3VdbeAddOp2(v, OP_Null, 0, regOld+i);
115044      }
115045    }
115046    if( chngRowid==0 && pPk==0 ){
115047      sqlite3VdbeAddOp2(v, OP_Copy, regOldRowid, regNewRowid);
115048    }
115049  }
115050
115051  /* Populate the array of registers beginning at regNew with the new
115052  ** row data. This array is used to check constants, create the new
115053  ** table and index records, and as the values for any new.* references
115054  ** made by triggers.
115055  **
115056  ** If there are one or more BEFORE triggers, then do not populate the
115057  ** registers associated with columns that are (a) not modified by
115058  ** this UPDATE statement and (b) not accessed by new.* references. The
115059  ** values for registers not modified by the UPDATE must be reloaded from
115060  ** the database after the BEFORE triggers are fired anyway (as the trigger
115061  ** may have modified them). So not loading those that are not going to
115062  ** be used eliminates some redundant opcodes.
115063  */
115064  newmask = sqlite3TriggerColmask(
115065      pParse, pTrigger, pChanges, 1, TRIGGER_BEFORE, pTab, onError
115066  );
115067  /*sqlite3VdbeAddOp3(v, OP_Null, 0, regNew, regNew+pTab->nCol-1);*/
115068  for(i=0; i<pTab->nCol; i++){
115069    if( i==pTab->iPKey ){
115070      sqlite3VdbeAddOp2(v, OP_Null, 0, regNew+i);
115071    }else{
115072      j = aXRef[i];
115073      if( j>=0 ){
115074        sqlite3ExprCode(pParse, pChanges->a[j].pExpr, regNew+i);
115075      }else if( 0==(tmask&TRIGGER_BEFORE) || i>31 || (newmask & MASKBIT32(i)) ){
115076        /* This branch loads the value of a column that will not be changed
115077        ** into a register. This is done if there are no BEFORE triggers, or
115078        ** if there are one or more BEFORE triggers that use this value via
115079        ** a new.* reference in a trigger program.
115080        */
115081        testcase( i==31 );
115082        testcase( i==32 );
115083        sqlite3ExprCodeGetColumnOfTable(v, pTab, iDataCur, i, regNew+i);
115084      }else{
115085        sqlite3VdbeAddOp2(v, OP_Null, 0, regNew+i);
115086      }
115087    }
115088  }
115089
115090  /* Fire any BEFORE UPDATE triggers. This happens before constraints are
115091  ** verified. One could argue that this is wrong.
115092  */
115093  if( tmask&TRIGGER_BEFORE ){
115094    sqlite3TableAffinity(v, pTab, regNew);
115095    sqlite3CodeRowTrigger(pParse, pTrigger, TK_UPDATE, pChanges,
115096        TRIGGER_BEFORE, pTab, regOldRowid, onError, labelContinue);
115097
115098    /* The row-trigger may have deleted the row being updated. In this
115099    ** case, jump to the next row. No updates or AFTER triggers are
115100    ** required. This behavior - what happens when the row being updated
115101    ** is deleted or renamed by a BEFORE trigger - is left undefined in the
115102    ** documentation.
115103    */
115104    if( pPk ){
115105      sqlite3VdbeAddOp4Int(v, OP_NotFound, iDataCur, labelContinue,regKey,nKey);
115106      VdbeCoverage(v);
115107    }else{
115108      sqlite3VdbeAddOp3(v, OP_NotExists, iDataCur, labelContinue, regOldRowid);
115109      VdbeCoverage(v);
115110    }
115111
115112    /* If it did not delete it, the row-trigger may still have modified
115113    ** some of the columns of the row being updated. Load the values for
115114    ** all columns not modified by the update statement into their
115115    ** registers in case this has happened.
115116    */
115117    for(i=0; i<pTab->nCol; i++){
115118      if( aXRef[i]<0 && i!=pTab->iPKey ){
115119        sqlite3ExprCodeGetColumnOfTable(v, pTab, iDataCur, i, regNew+i);
115120      }
115121    }
115122  }
115123
115124  if( !isView ){
115125    int j1 = 0;           /* Address of jump instruction */
115126    int bReplace = 0;     /* True if REPLACE conflict resolution might happen */
115127
115128    /* Do constraint checks. */
115129    assert( regOldRowid>0 );
115130    sqlite3GenerateConstraintChecks(pParse, pTab, aRegIdx, iDataCur, iIdxCur,
115131        regNewRowid, regOldRowid, chngKey, onError, labelContinue, &bReplace);
115132
115133    /* Do FK constraint checks. */
115134    if( hasFK ){
115135      sqlite3FkCheck(pParse, pTab, regOldRowid, 0, aXRef, chngKey);
115136    }
115137
115138    /* Delete the index entries associated with the current record.  */
115139    if( bReplace || chngKey ){
115140      if( pPk ){
115141        j1 = sqlite3VdbeAddOp4Int(v, OP_NotFound, iDataCur, 0, regKey, nKey);
115142      }else{
115143        j1 = sqlite3VdbeAddOp3(v, OP_NotExists, iDataCur, 0, regOldRowid);
115144      }
115145      VdbeCoverageNeverTaken(v);
115146    }
115147    sqlite3GenerateRowIndexDelete(pParse, pTab, iDataCur, iIdxCur, aRegIdx);
115148
115149    /* If changing the record number, delete the old record.  */
115150    if( hasFK || chngKey || pPk!=0 ){
115151      sqlite3VdbeAddOp2(v, OP_Delete, iDataCur, 0);
115152    }
115153    if( bReplace || chngKey ){
115154      sqlite3VdbeJumpHere(v, j1);
115155    }
115156
115157    if( hasFK ){
115158      sqlite3FkCheck(pParse, pTab, 0, regNewRowid, aXRef, chngKey);
115159    }
115160
115161    /* Insert the new index entries and the new record. */
115162    sqlite3CompleteInsertion(pParse, pTab, iDataCur, iIdxCur,
115163                             regNewRowid, aRegIdx, 1, 0, 0);
115164
115165    /* Do any ON CASCADE, SET NULL or SET DEFAULT operations required to
115166    ** handle rows (possibly in other tables) that refer via a foreign key
115167    ** to the row just updated. */
115168    if( hasFK ){
115169      sqlite3FkActions(pParse, pTab, pChanges, regOldRowid, aXRef, chngKey);
115170    }
115171  }
115172
115173  /* Increment the row counter
115174  */
115175  if( (db->flags & SQLITE_CountRows) && !pParse->pTriggerTab){
115176    sqlite3VdbeAddOp2(v, OP_AddImm, regRowCount, 1);
115177  }
115178
115179  sqlite3CodeRowTrigger(pParse, pTrigger, TK_UPDATE, pChanges,
115180      TRIGGER_AFTER, pTab, regOldRowid, onError, labelContinue);
115181
115182  /* Repeat the above with the next record to be updated, until
115183  ** all record selected by the WHERE clause have been updated.
115184  */
115185  if( okOnePass ){
115186    /* Nothing to do at end-of-loop for a single-pass */
115187  }else if( pPk ){
115188    sqlite3VdbeResolveLabel(v, labelContinue);
115189    sqlite3VdbeAddOp2(v, OP_Next, iEph, addrTop); VdbeCoverage(v);
115190  }else{
115191    sqlite3VdbeAddOp2(v, OP_Goto, 0, labelContinue);
115192  }
115193  sqlite3VdbeResolveLabel(v, labelBreak);
115194
115195  /* Close all tables */
115196  for(i=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){
115197    assert( aRegIdx );
115198    if( aToOpen[i+1] ){
115199      sqlite3VdbeAddOp2(v, OP_Close, iIdxCur+i, 0);
115200    }
115201  }
115202  if( iDataCur<iIdxCur ) sqlite3VdbeAddOp2(v, OP_Close, iDataCur, 0);
115203
115204  /* Update the sqlite_sequence table by storing the content of the
115205  ** maximum rowid counter values recorded while inserting into
115206  ** autoincrement tables.
115207  */
115208  if( pParse->nested==0 && pParse->pTriggerTab==0 ){
115209    sqlite3AutoincrementEnd(pParse);
115210  }
115211
115212  /*
115213  ** Return the number of rows that were changed. If this routine is
115214  ** generating code because of a call to sqlite3NestedParse(), do not
115215  ** invoke the callback function.
115216  */
115217  if( (db->flags&SQLITE_CountRows) && !pParse->pTriggerTab && !pParse->nested ){
115218    sqlite3VdbeAddOp2(v, OP_ResultRow, regRowCount, 1);
115219    sqlite3VdbeSetNumCols(v, 1);
115220    sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "rows updated", SQLITE_STATIC);
115221  }
115222
115223update_cleanup:
115224  sqlite3AuthContextPop(&sContext);
115225  sqlite3DbFree(db, aXRef); /* Also frees aRegIdx[] and aToOpen[] */
115226  sqlite3SrcListDelete(db, pTabList);
115227  sqlite3ExprListDelete(db, pChanges);
115228  sqlite3ExprDelete(db, pWhere);
115229  return;
115230}
115231/* Make sure "isView" and other macros defined above are undefined. Otherwise
115232** they may interfere with compilation of other functions in this file
115233** (or in another file, if this file becomes part of the amalgamation).  */
115234#ifdef isView
115235 #undef isView
115236#endif
115237#ifdef pTrigger
115238 #undef pTrigger
115239#endif
115240
115241#ifndef SQLITE_OMIT_VIRTUALTABLE
115242/*
115243** Generate code for an UPDATE of a virtual table.
115244**
115245** The strategy is that we create an ephemeral table that contains
115246** for each row to be changed:
115247**
115248**   (A)  The original rowid of that row.
115249**   (B)  The revised rowid for the row. (note1)
115250**   (C)  The content of every column in the row.
115251**
115252** Then we loop over this ephemeral table and for each row in
115253** the ephemeral table call VUpdate.
115254**
115255** When finished, drop the ephemeral table.
115256**
115257** (note1) Actually, if we know in advance that (A) is always the same
115258** as (B) we only store (A), then duplicate (A) when pulling
115259** it out of the ephemeral table before calling VUpdate.
115260*/
115261static void updateVirtualTable(
115262  Parse *pParse,       /* The parsing context */
115263  SrcList *pSrc,       /* The virtual table to be modified */
115264  Table *pTab,         /* The virtual table */
115265  ExprList *pChanges,  /* The columns to change in the UPDATE statement */
115266  Expr *pRowid,        /* Expression used to recompute the rowid */
115267  int *aXRef,          /* Mapping from columns of pTab to entries in pChanges */
115268  Expr *pWhere,        /* WHERE clause of the UPDATE statement */
115269  int onError          /* ON CONFLICT strategy */
115270){
115271  Vdbe *v = pParse->pVdbe;  /* Virtual machine under construction */
115272  ExprList *pEList = 0;     /* The result set of the SELECT statement */
115273  Select *pSelect = 0;      /* The SELECT statement */
115274  Expr *pExpr;              /* Temporary expression */
115275  int ephemTab;             /* Table holding the result of the SELECT */
115276  int i;                    /* Loop counter */
115277  int addr;                 /* Address of top of loop */
115278  int iReg;                 /* First register in set passed to OP_VUpdate */
115279  sqlite3 *db = pParse->db; /* Database connection */
115280  const char *pVTab = (const char*)sqlite3GetVTable(db, pTab);
115281  SelectDest dest;
115282
115283  /* Construct the SELECT statement that will find the new values for
115284  ** all updated rows.
115285  */
115286  pEList = sqlite3ExprListAppend(pParse, 0, sqlite3Expr(db, TK_ID, "_rowid_"));
115287  if( pRowid ){
115288    pEList = sqlite3ExprListAppend(pParse, pEList,
115289                                   sqlite3ExprDup(db, pRowid, 0));
115290  }
115291  assert( pTab->iPKey<0 );
115292  for(i=0; i<pTab->nCol; i++){
115293    if( aXRef[i]>=0 ){
115294      pExpr = sqlite3ExprDup(db, pChanges->a[aXRef[i]].pExpr, 0);
115295    }else{
115296      pExpr = sqlite3Expr(db, TK_ID, pTab->aCol[i].zName);
115297    }
115298    pEList = sqlite3ExprListAppend(pParse, pEList, pExpr);
115299  }
115300  pSelect = sqlite3SelectNew(pParse, pEList, pSrc, pWhere, 0, 0, 0, 0, 0, 0);
115301
115302  /* Create the ephemeral table into which the update results will
115303  ** be stored.
115304  */
115305  assert( v );
115306  ephemTab = pParse->nTab++;
115307
115308  /* fill the ephemeral table
115309  */
115310  sqlite3SelectDestInit(&dest, SRT_EphemTab, ephemTab);
115311  sqlite3Select(pParse, pSelect, &dest);
115312
115313  /* Generate code to scan the ephemeral table and call VUpdate. */
115314  iReg = ++pParse->nMem;
115315  pParse->nMem += pTab->nCol+1;
115316  addr = sqlite3VdbeAddOp2(v, OP_Rewind, ephemTab, 0); VdbeCoverage(v);
115317  sqlite3VdbeAddOp3(v, OP_Column,  ephemTab, 0, iReg);
115318  sqlite3VdbeAddOp3(v, OP_Column, ephemTab, (pRowid?1:0), iReg+1);
115319  for(i=0; i<pTab->nCol; i++){
115320    sqlite3VdbeAddOp3(v, OP_Column, ephemTab, i+1+(pRowid!=0), iReg+2+i);
115321  }
115322  sqlite3VtabMakeWritable(pParse, pTab);
115323  sqlite3VdbeAddOp4(v, OP_VUpdate, 0, pTab->nCol+2, iReg, pVTab, P4_VTAB);
115324  sqlite3VdbeChangeP5(v, onError==OE_Default ? OE_Abort : onError);
115325  sqlite3MayAbort(pParse);
115326  sqlite3VdbeAddOp2(v, OP_Next, ephemTab, addr+1); VdbeCoverage(v);
115327  sqlite3VdbeJumpHere(v, addr);
115328  sqlite3VdbeAddOp2(v, OP_Close, ephemTab, 0);
115329
115330  /* Cleanup */
115331  sqlite3SelectDelete(db, pSelect);
115332}
115333#endif /* SQLITE_OMIT_VIRTUALTABLE */
115334
115335/************** End of update.c **********************************************/
115336/************** Begin file vacuum.c ******************************************/
115337/*
115338** 2003 April 6
115339**
115340** The author disclaims copyright to this source code.  In place of
115341** a legal notice, here is a blessing:
115342**
115343**    May you do good and not evil.
115344**    May you find forgiveness for yourself and forgive others.
115345**    May you share freely, never taking more than you give.
115346**
115347*************************************************************************
115348** This file contains code used to implement the VACUUM command.
115349**
115350** Most of the code in this file may be omitted by defining the
115351** SQLITE_OMIT_VACUUM macro.
115352*/
115353/* #include "sqliteInt.h" */
115354/* #include "vdbeInt.h" */
115355
115356#if !defined(SQLITE_OMIT_VACUUM) && !defined(SQLITE_OMIT_ATTACH)
115357/*
115358** Finalize a prepared statement.  If there was an error, store the
115359** text of the error message in *pzErrMsg.  Return the result code.
115360*/
115361static int vacuumFinalize(sqlite3 *db, sqlite3_stmt *pStmt, char **pzErrMsg){
115362  int rc;
115363  rc = sqlite3VdbeFinalize((Vdbe*)pStmt);
115364  if( rc ){
115365    sqlite3SetString(pzErrMsg, db, sqlite3_errmsg(db));
115366  }
115367  return rc;
115368}
115369
115370/*
115371** Execute zSql on database db. Return an error code.
115372*/
115373static int execSql(sqlite3 *db, char **pzErrMsg, const char *zSql){
115374  sqlite3_stmt *pStmt;
115375  VVA_ONLY( int rc; )
115376  if( !zSql ){
115377    return SQLITE_NOMEM;
115378  }
115379  if( SQLITE_OK!=sqlite3_prepare(db, zSql, -1, &pStmt, 0) ){
115380    sqlite3SetString(pzErrMsg, db, sqlite3_errmsg(db));
115381    return sqlite3_errcode(db);
115382  }
115383  VVA_ONLY( rc = ) sqlite3_step(pStmt);
115384  assert( rc!=SQLITE_ROW || (db->flags&SQLITE_CountRows) );
115385  return vacuumFinalize(db, pStmt, pzErrMsg);
115386}
115387
115388/*
115389** Execute zSql on database db. The statement returns exactly
115390** one column. Execute this as SQL on the same database.
115391*/
115392static int execExecSql(sqlite3 *db, char **pzErrMsg, const char *zSql){
115393  sqlite3_stmt *pStmt;
115394  int rc;
115395
115396  rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0);
115397  if( rc!=SQLITE_OK ) return rc;
115398
115399  while( SQLITE_ROW==sqlite3_step(pStmt) ){
115400    rc = execSql(db, pzErrMsg, (char*)sqlite3_column_text(pStmt, 0));
115401    if( rc!=SQLITE_OK ){
115402      vacuumFinalize(db, pStmt, pzErrMsg);
115403      return rc;
115404    }
115405  }
115406
115407  return vacuumFinalize(db, pStmt, pzErrMsg);
115408}
115409
115410/*
115411** The VACUUM command is used to clean up the database,
115412** collapse free space, etc.  It is modelled after the VACUUM command
115413** in PostgreSQL.  The VACUUM command works as follows:
115414**
115415**   (1)  Create a new transient database file
115416**   (2)  Copy all content from the database being vacuumed into
115417**        the new transient database file
115418**   (3)  Copy content from the transient database back into the
115419**        original database.
115420**
115421** The transient database requires temporary disk space approximately
115422** equal to the size of the original database.  The copy operation of
115423** step (3) requires additional temporary disk space approximately equal
115424** to the size of the original database for the rollback journal.
115425** Hence, temporary disk space that is approximately 2x the size of the
115426** original database is required.  Every page of the database is written
115427** approximately 3 times:  Once for step (2) and twice for step (3).
115428** Two writes per page are required in step (3) because the original
115429** database content must be written into the rollback journal prior to
115430** overwriting the database with the vacuumed content.
115431**
115432** Only 1x temporary space and only 1x writes would be required if
115433** the copy of step (3) were replaced by deleting the original database
115434** and renaming the transient database as the original.  But that will
115435** not work if other processes are attached to the original database.
115436** And a power loss in between deleting the original and renaming the
115437** transient would cause the database file to appear to be deleted
115438** following reboot.
115439*/
115440SQLITE_PRIVATE void sqlite3Vacuum(Parse *pParse){
115441  Vdbe *v = sqlite3GetVdbe(pParse);
115442  if( v ){
115443    sqlite3VdbeAddOp2(v, OP_Vacuum, 0, 0);
115444    sqlite3VdbeUsesBtree(v, 0);
115445  }
115446  return;
115447}
115448
115449/*
115450** This routine implements the OP_Vacuum opcode of the VDBE.
115451*/
115452SQLITE_PRIVATE int sqlite3RunVacuum(char **pzErrMsg, sqlite3 *db){
115453  int rc = SQLITE_OK;     /* Return code from service routines */
115454  Btree *pMain;           /* The database being vacuumed */
115455  Btree *pTemp;           /* The temporary database we vacuum into */
115456  char *zSql = 0;         /* SQL statements */
115457  int saved_flags;        /* Saved value of the db->flags */
115458  int saved_nChange;      /* Saved value of db->nChange */
115459  int saved_nTotalChange; /* Saved value of db->nTotalChange */
115460  void (*saved_xTrace)(void*,const char*);  /* Saved db->xTrace */
115461  Db *pDb = 0;            /* Database to detach at end of vacuum */
115462  int isMemDb;            /* True if vacuuming a :memory: database */
115463  int nRes;               /* Bytes of reserved space at the end of each page */
115464  int nDb;                /* Number of attached databases */
115465
115466  if( !db->autoCommit ){
115467    sqlite3SetString(pzErrMsg, db, "cannot VACUUM from within a transaction");
115468    return SQLITE_ERROR;
115469  }
115470  if( db->nVdbeActive>1 ){
115471    sqlite3SetString(pzErrMsg, db,"cannot VACUUM - SQL statements in progress");
115472    return SQLITE_ERROR;
115473  }
115474
115475  /* Save the current value of the database flags so that it can be
115476  ** restored before returning. Then set the writable-schema flag, and
115477  ** disable CHECK and foreign key constraints.  */
115478  saved_flags = db->flags;
115479  saved_nChange = db->nChange;
115480  saved_nTotalChange = db->nTotalChange;
115481  saved_xTrace = db->xTrace;
115482  db->flags |= SQLITE_WriteSchema | SQLITE_IgnoreChecks | SQLITE_PreferBuiltin;
115483  db->flags &= ~(SQLITE_ForeignKeys | SQLITE_ReverseOrder);
115484  db->xTrace = 0;
115485
115486  pMain = db->aDb[0].pBt;
115487  isMemDb = sqlite3PagerIsMemdb(sqlite3BtreePager(pMain));
115488
115489  /* Attach the temporary database as 'vacuum_db'. The synchronous pragma
115490  ** can be set to 'off' for this file, as it is not recovered if a crash
115491  ** occurs anyway. The integrity of the database is maintained by a
115492  ** (possibly synchronous) transaction opened on the main database before
115493  ** sqlite3BtreeCopyFile() is called.
115494  **
115495  ** An optimisation would be to use a non-journaled pager.
115496  ** (Later:) I tried setting "PRAGMA vacuum_db.journal_mode=OFF" but
115497  ** that actually made the VACUUM run slower.  Very little journalling
115498  ** actually occurs when doing a vacuum since the vacuum_db is initially
115499  ** empty.  Only the journal header is written.  Apparently it takes more
115500  ** time to parse and run the PRAGMA to turn journalling off than it does
115501  ** to write the journal header file.
115502  */
115503  nDb = db->nDb;
115504  if( sqlite3TempInMemory(db) ){
115505    zSql = "ATTACH ':memory:' AS vacuum_db;";
115506  }else{
115507    zSql = "ATTACH '' AS vacuum_db;";
115508  }
115509  rc = execSql(db, pzErrMsg, zSql);
115510  if( db->nDb>nDb ){
115511    pDb = &db->aDb[db->nDb-1];
115512    assert( strcmp(pDb->zName,"vacuum_db")==0 );
115513  }
115514  if( rc!=SQLITE_OK ) goto end_of_vacuum;
115515  pTemp = db->aDb[db->nDb-1].pBt;
115516
115517  /* The call to execSql() to attach the temp database has left the file
115518  ** locked (as there was more than one active statement when the transaction
115519  ** to read the schema was concluded. Unlock it here so that this doesn't
115520  ** cause problems for the call to BtreeSetPageSize() below.  */
115521  sqlite3BtreeCommit(pTemp);
115522
115523  nRes = sqlite3BtreeGetOptimalReserve(pMain);
115524
115525  /* A VACUUM cannot change the pagesize of an encrypted database. */
115526#ifdef SQLITE_HAS_CODEC
115527  if( db->nextPagesize ){
115528    extern void sqlite3CodecGetKey(sqlite3*, int, void**, int*);
115529    int nKey;
115530    char *zKey;
115531    sqlite3CodecGetKey(db, 0, (void**)&zKey, &nKey);
115532    if( nKey ) db->nextPagesize = 0;
115533  }
115534#endif
115535
115536  rc = execSql(db, pzErrMsg, "PRAGMA vacuum_db.synchronous=OFF");
115537  if( rc!=SQLITE_OK ) goto end_of_vacuum;
115538
115539  /* Begin a transaction and take an exclusive lock on the main database
115540  ** file. This is done before the sqlite3BtreeGetPageSize(pMain) call below,
115541  ** to ensure that we do not try to change the page-size on a WAL database.
115542  */
115543  rc = execSql(db, pzErrMsg, "BEGIN;");
115544  if( rc!=SQLITE_OK ) goto end_of_vacuum;
115545  rc = sqlite3BtreeBeginTrans(pMain, 2);
115546  if( rc!=SQLITE_OK ) goto end_of_vacuum;
115547
115548  /* Do not attempt to change the page size for a WAL database */
115549  if( sqlite3PagerGetJournalMode(sqlite3BtreePager(pMain))
115550                                               ==PAGER_JOURNALMODE_WAL ){
115551    db->nextPagesize = 0;
115552  }
115553
115554  if( sqlite3BtreeSetPageSize(pTemp, sqlite3BtreeGetPageSize(pMain), nRes, 0)
115555   || (!isMemDb && sqlite3BtreeSetPageSize(pTemp, db->nextPagesize, nRes, 0))
115556   || NEVER(db->mallocFailed)
115557  ){
115558    rc = SQLITE_NOMEM;
115559    goto end_of_vacuum;
115560  }
115561
115562#ifndef SQLITE_OMIT_AUTOVACUUM
115563  sqlite3BtreeSetAutoVacuum(pTemp, db->nextAutovac>=0 ? db->nextAutovac :
115564                                           sqlite3BtreeGetAutoVacuum(pMain));
115565#endif
115566
115567  /* Query the schema of the main database. Create a mirror schema
115568  ** in the temporary database.
115569  */
115570  rc = execExecSql(db, pzErrMsg,
115571      "SELECT 'CREATE TABLE vacuum_db.' || substr(sql,14) "
115572      "  FROM sqlite_master WHERE type='table' AND name!='sqlite_sequence'"
115573      "   AND coalesce(rootpage,1)>0"
115574  );
115575  if( rc!=SQLITE_OK ) goto end_of_vacuum;
115576  rc = execExecSql(db, pzErrMsg,
115577      "SELECT 'CREATE INDEX vacuum_db.' || substr(sql,14)"
115578      "  FROM sqlite_master WHERE sql LIKE 'CREATE INDEX %' ");
115579  if( rc!=SQLITE_OK ) goto end_of_vacuum;
115580  rc = execExecSql(db, pzErrMsg,
115581      "SELECT 'CREATE UNIQUE INDEX vacuum_db.' || substr(sql,21) "
115582      "  FROM sqlite_master WHERE sql LIKE 'CREATE UNIQUE INDEX %'");
115583  if( rc!=SQLITE_OK ) goto end_of_vacuum;
115584
115585  /* Loop through the tables in the main database. For each, do
115586  ** an "INSERT INTO vacuum_db.xxx SELECT * FROM main.xxx;" to copy
115587  ** the contents to the temporary database.
115588  */
115589  assert( (db->flags & SQLITE_Vacuum)==0 );
115590  db->flags |= SQLITE_Vacuum;
115591  rc = execExecSql(db, pzErrMsg,
115592      "SELECT 'INSERT INTO vacuum_db.' || quote(name) "
115593      "|| ' SELECT * FROM main.' || quote(name) || ';'"
115594      "FROM main.sqlite_master "
115595      "WHERE type = 'table' AND name!='sqlite_sequence' "
115596      "  AND coalesce(rootpage,1)>0"
115597  );
115598  assert( (db->flags & SQLITE_Vacuum)!=0 );
115599  db->flags &= ~SQLITE_Vacuum;
115600  if( rc!=SQLITE_OK ) goto end_of_vacuum;
115601
115602  /* Copy over the sequence table
115603  */
115604  rc = execExecSql(db, pzErrMsg,
115605      "SELECT 'DELETE FROM vacuum_db.' || quote(name) || ';' "
115606      "FROM vacuum_db.sqlite_master WHERE name='sqlite_sequence' "
115607  );
115608  if( rc!=SQLITE_OK ) goto end_of_vacuum;
115609  rc = execExecSql(db, pzErrMsg,
115610      "SELECT 'INSERT INTO vacuum_db.' || quote(name) "
115611      "|| ' SELECT * FROM main.' || quote(name) || ';' "
115612      "FROM vacuum_db.sqlite_master WHERE name=='sqlite_sequence';"
115613  );
115614  if( rc!=SQLITE_OK ) goto end_of_vacuum;
115615
115616
115617  /* Copy the triggers, views, and virtual tables from the main database
115618  ** over to the temporary database.  None of these objects has any
115619  ** associated storage, so all we have to do is copy their entries
115620  ** from the SQLITE_MASTER table.
115621  */
115622  rc = execSql(db, pzErrMsg,
115623      "INSERT INTO vacuum_db.sqlite_master "
115624      "  SELECT type, name, tbl_name, rootpage, sql"
115625      "    FROM main.sqlite_master"
115626      "   WHERE type='view' OR type='trigger'"
115627      "      OR (type='table' AND rootpage=0)"
115628  );
115629  if( rc ) goto end_of_vacuum;
115630
115631  /* At this point, there is a write transaction open on both the
115632  ** vacuum database and the main database. Assuming no error occurs,
115633  ** both transactions are closed by this block - the main database
115634  ** transaction by sqlite3BtreeCopyFile() and the other by an explicit
115635  ** call to sqlite3BtreeCommit().
115636  */
115637  {
115638    u32 meta;
115639    int i;
115640
115641    /* This array determines which meta meta values are preserved in the
115642    ** vacuum.  Even entries are the meta value number and odd entries
115643    ** are an increment to apply to the meta value after the vacuum.
115644    ** The increment is used to increase the schema cookie so that other
115645    ** connections to the same database will know to reread the schema.
115646    */
115647    static const unsigned char aCopy[] = {
115648       BTREE_SCHEMA_VERSION,     1,  /* Add one to the old schema cookie */
115649       BTREE_DEFAULT_CACHE_SIZE, 0,  /* Preserve the default page cache size */
115650       BTREE_TEXT_ENCODING,      0,  /* Preserve the text encoding */
115651       BTREE_USER_VERSION,       0,  /* Preserve the user version */
115652       BTREE_APPLICATION_ID,     0,  /* Preserve the application id */
115653    };
115654
115655    assert( 1==sqlite3BtreeIsInTrans(pTemp) );
115656    assert( 1==sqlite3BtreeIsInTrans(pMain) );
115657
115658    /* Copy Btree meta values */
115659    for(i=0; i<ArraySize(aCopy); i+=2){
115660      /* GetMeta() and UpdateMeta() cannot fail in this context because
115661      ** we already have page 1 loaded into cache and marked dirty. */
115662      sqlite3BtreeGetMeta(pMain, aCopy[i], &meta);
115663      rc = sqlite3BtreeUpdateMeta(pTemp, aCopy[i], meta+aCopy[i+1]);
115664      if( NEVER(rc!=SQLITE_OK) ) goto end_of_vacuum;
115665    }
115666
115667    rc = sqlite3BtreeCopyFile(pMain, pTemp);
115668    if( rc!=SQLITE_OK ) goto end_of_vacuum;
115669    rc = sqlite3BtreeCommit(pTemp);
115670    if( rc!=SQLITE_OK ) goto end_of_vacuum;
115671#ifndef SQLITE_OMIT_AUTOVACUUM
115672    sqlite3BtreeSetAutoVacuum(pMain, sqlite3BtreeGetAutoVacuum(pTemp));
115673#endif
115674  }
115675
115676  assert( rc==SQLITE_OK );
115677  rc = sqlite3BtreeSetPageSize(pMain, sqlite3BtreeGetPageSize(pTemp), nRes,1);
115678
115679end_of_vacuum:
115680  /* Restore the original value of db->flags */
115681  db->flags = saved_flags;
115682  db->nChange = saved_nChange;
115683  db->nTotalChange = saved_nTotalChange;
115684  db->xTrace = saved_xTrace;
115685  sqlite3BtreeSetPageSize(pMain, -1, -1, 1);
115686
115687  /* Currently there is an SQL level transaction open on the vacuum
115688  ** database. No locks are held on any other files (since the main file
115689  ** was committed at the btree level). So it safe to end the transaction
115690  ** by manually setting the autoCommit flag to true and detaching the
115691  ** vacuum database. The vacuum_db journal file is deleted when the pager
115692  ** is closed by the DETACH.
115693  */
115694  db->autoCommit = 1;
115695
115696  if( pDb ){
115697    sqlite3BtreeClose(pDb->pBt);
115698    pDb->pBt = 0;
115699    pDb->pSchema = 0;
115700  }
115701
115702  /* This both clears the schemas and reduces the size of the db->aDb[]
115703  ** array. */
115704  sqlite3ResetAllSchemasOfConnection(db);
115705
115706  return rc;
115707}
115708
115709#endif  /* SQLITE_OMIT_VACUUM && SQLITE_OMIT_ATTACH */
115710
115711/************** End of vacuum.c **********************************************/
115712/************** Begin file vtab.c ********************************************/
115713/*
115714** 2006 June 10
115715**
115716** The author disclaims copyright to this source code.  In place of
115717** a legal notice, here is a blessing:
115718**
115719**    May you do good and not evil.
115720**    May you find forgiveness for yourself and forgive others.
115721**    May you share freely, never taking more than you give.
115722**
115723*************************************************************************
115724** This file contains code used to help implement virtual tables.
115725*/
115726#ifndef SQLITE_OMIT_VIRTUALTABLE
115727/* #include "sqliteInt.h" */
115728
115729/*
115730** Before a virtual table xCreate() or xConnect() method is invoked, the
115731** sqlite3.pVtabCtx member variable is set to point to an instance of
115732** this struct allocated on the stack. It is used by the implementation of
115733** the sqlite3_declare_vtab() and sqlite3_vtab_config() APIs, both of which
115734** are invoked only from within xCreate and xConnect methods.
115735*/
115736struct VtabCtx {
115737  VTable *pVTable;    /* The virtual table being constructed */
115738  Table *pTab;        /* The Table object to which the virtual table belongs */
115739  VtabCtx *pPrior;    /* Parent context (if any) */
115740  int bDeclared;      /* True after sqlite3_declare_vtab() is called */
115741};
115742
115743/*
115744** The actual function that does the work of creating a new module.
115745** This function implements the sqlite3_create_module() and
115746** sqlite3_create_module_v2() interfaces.
115747*/
115748static int createModule(
115749  sqlite3 *db,                    /* Database in which module is registered */
115750  const char *zName,              /* Name assigned to this module */
115751  const sqlite3_module *pModule,  /* The definition of the module */
115752  void *pAux,                     /* Context pointer for xCreate/xConnect */
115753  void (*xDestroy)(void *)        /* Module destructor function */
115754){
115755  int rc = SQLITE_OK;
115756  int nName;
115757
115758  sqlite3_mutex_enter(db->mutex);
115759  nName = sqlite3Strlen30(zName);
115760  if( sqlite3HashFind(&db->aModule, zName) ){
115761    rc = SQLITE_MISUSE_BKPT;
115762  }else{
115763    Module *pMod;
115764    pMod = (Module *)sqlite3DbMallocRaw(db, sizeof(Module) + nName + 1);
115765    if( pMod ){
115766      Module *pDel;
115767      char *zCopy = (char *)(&pMod[1]);
115768      memcpy(zCopy, zName, nName+1);
115769      pMod->zName = zCopy;
115770      pMod->pModule = pModule;
115771      pMod->pAux = pAux;
115772      pMod->xDestroy = xDestroy;
115773      pDel = (Module *)sqlite3HashInsert(&db->aModule,zCopy,(void*)pMod);
115774      assert( pDel==0 || pDel==pMod );
115775      if( pDel ){
115776        db->mallocFailed = 1;
115777        sqlite3DbFree(db, pDel);
115778      }
115779    }
115780  }
115781  rc = sqlite3ApiExit(db, rc);
115782  if( rc!=SQLITE_OK && xDestroy ) xDestroy(pAux);
115783
115784  sqlite3_mutex_leave(db->mutex);
115785  return rc;
115786}
115787
115788
115789/*
115790** External API function used to create a new virtual-table module.
115791*/
115792SQLITE_API int SQLITE_STDCALL sqlite3_create_module(
115793  sqlite3 *db,                    /* Database in which module is registered */
115794  const char *zName,              /* Name assigned to this module */
115795  const sqlite3_module *pModule,  /* The definition of the module */
115796  void *pAux                      /* Context pointer for xCreate/xConnect */
115797){
115798#ifdef SQLITE_ENABLE_API_ARMOR
115799  if( !sqlite3SafetyCheckOk(db) || zName==0 ) return SQLITE_MISUSE_BKPT;
115800#endif
115801  return createModule(db, zName, pModule, pAux, 0);
115802}
115803
115804/*
115805** External API function used to create a new virtual-table module.
115806*/
115807SQLITE_API int SQLITE_STDCALL sqlite3_create_module_v2(
115808  sqlite3 *db,                    /* Database in which module is registered */
115809  const char *zName,              /* Name assigned to this module */
115810  const sqlite3_module *pModule,  /* The definition of the module */
115811  void *pAux,                     /* Context pointer for xCreate/xConnect */
115812  void (*xDestroy)(void *)        /* Module destructor function */
115813){
115814#ifdef SQLITE_ENABLE_API_ARMOR
115815  if( !sqlite3SafetyCheckOk(db) || zName==0 ) return SQLITE_MISUSE_BKPT;
115816#endif
115817  return createModule(db, zName, pModule, pAux, xDestroy);
115818}
115819
115820/*
115821** Lock the virtual table so that it cannot be disconnected.
115822** Locks nest.  Every lock should have a corresponding unlock.
115823** If an unlock is omitted, resources leaks will occur.
115824**
115825** If a disconnect is attempted while a virtual table is locked,
115826** the disconnect is deferred until all locks have been removed.
115827*/
115828SQLITE_PRIVATE void sqlite3VtabLock(VTable *pVTab){
115829  pVTab->nRef++;
115830}
115831
115832
115833/*
115834** pTab is a pointer to a Table structure representing a virtual-table.
115835** Return a pointer to the VTable object used by connection db to access
115836** this virtual-table, if one has been created, or NULL otherwise.
115837*/
115838SQLITE_PRIVATE VTable *sqlite3GetVTable(sqlite3 *db, Table *pTab){
115839  VTable *pVtab;
115840  assert( IsVirtual(pTab) );
115841  for(pVtab=pTab->pVTable; pVtab && pVtab->db!=db; pVtab=pVtab->pNext);
115842  return pVtab;
115843}
115844
115845/*
115846** Decrement the ref-count on a virtual table object. When the ref-count
115847** reaches zero, call the xDisconnect() method to delete the object.
115848*/
115849SQLITE_PRIVATE void sqlite3VtabUnlock(VTable *pVTab){
115850  sqlite3 *db = pVTab->db;
115851
115852  assert( db );
115853  assert( pVTab->nRef>0 );
115854  assert( db->magic==SQLITE_MAGIC_OPEN || db->magic==SQLITE_MAGIC_ZOMBIE );
115855
115856  pVTab->nRef--;
115857  if( pVTab->nRef==0 ){
115858    sqlite3_vtab *p = pVTab->pVtab;
115859    if( p ){
115860      p->pModule->xDisconnect(p);
115861    }
115862    sqlite3DbFree(db, pVTab);
115863  }
115864}
115865
115866/*
115867** Table p is a virtual table. This function moves all elements in the
115868** p->pVTable list to the sqlite3.pDisconnect lists of their associated
115869** database connections to be disconnected at the next opportunity.
115870** Except, if argument db is not NULL, then the entry associated with
115871** connection db is left in the p->pVTable list.
115872*/
115873static VTable *vtabDisconnectAll(sqlite3 *db, Table *p){
115874  VTable *pRet = 0;
115875  VTable *pVTable = p->pVTable;
115876  p->pVTable = 0;
115877
115878  /* Assert that the mutex (if any) associated with the BtShared database
115879  ** that contains table p is held by the caller. See header comments
115880  ** above function sqlite3VtabUnlockList() for an explanation of why
115881  ** this makes it safe to access the sqlite3.pDisconnect list of any
115882  ** database connection that may have an entry in the p->pVTable list.
115883  */
115884  assert( db==0 || sqlite3SchemaMutexHeld(db, 0, p->pSchema) );
115885
115886  while( pVTable ){
115887    sqlite3 *db2 = pVTable->db;
115888    VTable *pNext = pVTable->pNext;
115889    assert( db2 );
115890    if( db2==db ){
115891      pRet = pVTable;
115892      p->pVTable = pRet;
115893      pRet->pNext = 0;
115894    }else{
115895      pVTable->pNext = db2->pDisconnect;
115896      db2->pDisconnect = pVTable;
115897    }
115898    pVTable = pNext;
115899  }
115900
115901  assert( !db || pRet );
115902  return pRet;
115903}
115904
115905/*
115906** Table *p is a virtual table. This function removes the VTable object
115907** for table *p associated with database connection db from the linked
115908** list in p->pVTab. It also decrements the VTable ref count. This is
115909** used when closing database connection db to free all of its VTable
115910** objects without disturbing the rest of the Schema object (which may
115911** be being used by other shared-cache connections).
115912*/
115913SQLITE_PRIVATE void sqlite3VtabDisconnect(sqlite3 *db, Table *p){
115914  VTable **ppVTab;
115915
115916  assert( IsVirtual(p) );
115917  assert( sqlite3BtreeHoldsAllMutexes(db) );
115918  assert( sqlite3_mutex_held(db->mutex) );
115919
115920  for(ppVTab=&p->pVTable; *ppVTab; ppVTab=&(*ppVTab)->pNext){
115921    if( (*ppVTab)->db==db  ){
115922      VTable *pVTab = *ppVTab;
115923      *ppVTab = pVTab->pNext;
115924      sqlite3VtabUnlock(pVTab);
115925      break;
115926    }
115927  }
115928}
115929
115930
115931/*
115932** Disconnect all the virtual table objects in the sqlite3.pDisconnect list.
115933**
115934** This function may only be called when the mutexes associated with all
115935** shared b-tree databases opened using connection db are held by the
115936** caller. This is done to protect the sqlite3.pDisconnect list. The
115937** sqlite3.pDisconnect list is accessed only as follows:
115938**
115939**   1) By this function. In this case, all BtShared mutexes and the mutex
115940**      associated with the database handle itself must be held.
115941**
115942**   2) By function vtabDisconnectAll(), when it adds a VTable entry to
115943**      the sqlite3.pDisconnect list. In this case either the BtShared mutex
115944**      associated with the database the virtual table is stored in is held
115945**      or, if the virtual table is stored in a non-sharable database, then
115946**      the database handle mutex is held.
115947**
115948** As a result, a sqlite3.pDisconnect cannot be accessed simultaneously
115949** by multiple threads. It is thread-safe.
115950*/
115951SQLITE_PRIVATE void sqlite3VtabUnlockList(sqlite3 *db){
115952  VTable *p = db->pDisconnect;
115953  db->pDisconnect = 0;
115954
115955  assert( sqlite3BtreeHoldsAllMutexes(db) );
115956  assert( sqlite3_mutex_held(db->mutex) );
115957
115958  if( p ){
115959    sqlite3ExpirePreparedStatements(db);
115960    do {
115961      VTable *pNext = p->pNext;
115962      sqlite3VtabUnlock(p);
115963      p = pNext;
115964    }while( p );
115965  }
115966}
115967
115968/*
115969** Clear any and all virtual-table information from the Table record.
115970** This routine is called, for example, just before deleting the Table
115971** record.
115972**
115973** Since it is a virtual-table, the Table structure contains a pointer
115974** to the head of a linked list of VTable structures. Each VTable
115975** structure is associated with a single sqlite3* user of the schema.
115976** The reference count of the VTable structure associated with database
115977** connection db is decremented immediately (which may lead to the
115978** structure being xDisconnected and free). Any other VTable structures
115979** in the list are moved to the sqlite3.pDisconnect list of the associated
115980** database connection.
115981*/
115982SQLITE_PRIVATE void sqlite3VtabClear(sqlite3 *db, Table *p){
115983  if( !db || db->pnBytesFreed==0 ) vtabDisconnectAll(0, p);
115984  if( p->azModuleArg ){
115985    int i;
115986    for(i=0; i<p->nModuleArg; i++){
115987      if( i!=1 ) sqlite3DbFree(db, p->azModuleArg[i]);
115988    }
115989    sqlite3DbFree(db, p->azModuleArg);
115990  }
115991}
115992
115993/*
115994** Add a new module argument to pTable->azModuleArg[].
115995** The string is not copied - the pointer is stored.  The
115996** string will be freed automatically when the table is
115997** deleted.
115998*/
115999static void addModuleArgument(sqlite3 *db, Table *pTable, char *zArg){
116000  int i = pTable->nModuleArg++;
116001  int nBytes = sizeof(char *)*(1+pTable->nModuleArg);
116002  char **azModuleArg;
116003  azModuleArg = sqlite3DbRealloc(db, pTable->azModuleArg, nBytes);
116004  if( azModuleArg==0 ){
116005    int j;
116006    for(j=0; j<i; j++){
116007      sqlite3DbFree(db, pTable->azModuleArg[j]);
116008    }
116009    sqlite3DbFree(db, zArg);
116010    sqlite3DbFree(db, pTable->azModuleArg);
116011    pTable->nModuleArg = 0;
116012  }else{
116013    azModuleArg[i] = zArg;
116014    azModuleArg[i+1] = 0;
116015  }
116016  pTable->azModuleArg = azModuleArg;
116017}
116018
116019/*
116020** The parser calls this routine when it first sees a CREATE VIRTUAL TABLE
116021** statement.  The module name has been parsed, but the optional list
116022** of parameters that follow the module name are still pending.
116023*/
116024SQLITE_PRIVATE void sqlite3VtabBeginParse(
116025  Parse *pParse,        /* Parsing context */
116026  Token *pName1,        /* Name of new table, or database name */
116027  Token *pName2,        /* Name of new table or NULL */
116028  Token *pModuleName,   /* Name of the module for the virtual table */
116029  int ifNotExists       /* No error if the table already exists */
116030){
116031  int iDb;              /* The database the table is being created in */
116032  Table *pTable;        /* The new virtual table */
116033  sqlite3 *db;          /* Database connection */
116034
116035  sqlite3StartTable(pParse, pName1, pName2, 0, 0, 1, ifNotExists);
116036  pTable = pParse->pNewTable;
116037  if( pTable==0 ) return;
116038  assert( 0==pTable->pIndex );
116039
116040  db = pParse->db;
116041  iDb = sqlite3SchemaToIndex(db, pTable->pSchema);
116042  assert( iDb>=0 );
116043
116044  pTable->tabFlags |= TF_Virtual;
116045  pTable->nModuleArg = 0;
116046  addModuleArgument(db, pTable, sqlite3NameFromToken(db, pModuleName));
116047  addModuleArgument(db, pTable, 0);
116048  addModuleArgument(db, pTable, sqlite3DbStrDup(db, pTable->zName));
116049  assert( (pParse->sNameToken.z==pName2->z && pName2->z!=0)
116050       || (pParse->sNameToken.z==pName1->z && pName2->z==0)
116051  );
116052  pParse->sNameToken.n = (int)(
116053      &pModuleName->z[pModuleName->n] - pParse->sNameToken.z
116054  );
116055
116056#ifndef SQLITE_OMIT_AUTHORIZATION
116057  /* Creating a virtual table invokes the authorization callback twice.
116058  ** The first invocation, to obtain permission to INSERT a row into the
116059  ** sqlite_master table, has already been made by sqlite3StartTable().
116060  ** The second call, to obtain permission to create the table, is made now.
116061  */
116062  if( pTable->azModuleArg ){
116063    sqlite3AuthCheck(pParse, SQLITE_CREATE_VTABLE, pTable->zName,
116064            pTable->azModuleArg[0], pParse->db->aDb[iDb].zName);
116065  }
116066#endif
116067}
116068
116069/*
116070** This routine takes the module argument that has been accumulating
116071** in pParse->zArg[] and appends it to the list of arguments on the
116072** virtual table currently under construction in pParse->pTable.
116073*/
116074static void addArgumentToVtab(Parse *pParse){
116075  if( pParse->sArg.z && pParse->pNewTable ){
116076    const char *z = (const char*)pParse->sArg.z;
116077    int n = pParse->sArg.n;
116078    sqlite3 *db = pParse->db;
116079    addModuleArgument(db, pParse->pNewTable, sqlite3DbStrNDup(db, z, n));
116080  }
116081}
116082
116083/*
116084** The parser calls this routine after the CREATE VIRTUAL TABLE statement
116085** has been completely parsed.
116086*/
116087SQLITE_PRIVATE void sqlite3VtabFinishParse(Parse *pParse, Token *pEnd){
116088  Table *pTab = pParse->pNewTable;  /* The table being constructed */
116089  sqlite3 *db = pParse->db;         /* The database connection */
116090
116091  if( pTab==0 ) return;
116092  addArgumentToVtab(pParse);
116093  pParse->sArg.z = 0;
116094  if( pTab->nModuleArg<1 ) return;
116095
116096  /* If the CREATE VIRTUAL TABLE statement is being entered for the
116097  ** first time (in other words if the virtual table is actually being
116098  ** created now instead of just being read out of sqlite_master) then
116099  ** do additional initialization work and store the statement text
116100  ** in the sqlite_master table.
116101  */
116102  if( !db->init.busy ){
116103    char *zStmt;
116104    char *zWhere;
116105    int iDb;
116106    int iReg;
116107    Vdbe *v;
116108
116109    /* Compute the complete text of the CREATE VIRTUAL TABLE statement */
116110    if( pEnd ){
116111      pParse->sNameToken.n = (int)(pEnd->z - pParse->sNameToken.z) + pEnd->n;
116112    }
116113    zStmt = sqlite3MPrintf(db, "CREATE VIRTUAL TABLE %T", &pParse->sNameToken);
116114
116115    /* A slot for the record has already been allocated in the
116116    ** SQLITE_MASTER table.  We just need to update that slot with all
116117    ** the information we've collected.
116118    **
116119    ** The VM register number pParse->regRowid holds the rowid of an
116120    ** entry in the sqlite_master table tht was created for this vtab
116121    ** by sqlite3StartTable().
116122    */
116123    iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
116124    sqlite3NestedParse(pParse,
116125      "UPDATE %Q.%s "
116126         "SET type='table', name=%Q, tbl_name=%Q, rootpage=0, sql=%Q "
116127       "WHERE rowid=#%d",
116128      db->aDb[iDb].zName, SCHEMA_TABLE(iDb),
116129      pTab->zName,
116130      pTab->zName,
116131      zStmt,
116132      pParse->regRowid
116133    );
116134    sqlite3DbFree(db, zStmt);
116135    v = sqlite3GetVdbe(pParse);
116136    sqlite3ChangeCookie(pParse, iDb);
116137
116138    sqlite3VdbeAddOp2(v, OP_Expire, 0, 0);
116139    zWhere = sqlite3MPrintf(db, "name='%q' AND type='table'", pTab->zName);
116140    sqlite3VdbeAddParseSchemaOp(v, iDb, zWhere);
116141
116142    iReg = ++pParse->nMem;
116143    sqlite3VdbeAddOp4(v, OP_String8, 0, iReg, 0, pTab->zName, 0);
116144    sqlite3VdbeAddOp2(v, OP_VCreate, iDb, iReg);
116145  }
116146
116147  /* If we are rereading the sqlite_master table create the in-memory
116148  ** record of the table. The xConnect() method is not called until
116149  ** the first time the virtual table is used in an SQL statement. This
116150  ** allows a schema that contains virtual tables to be loaded before
116151  ** the required virtual table implementations are registered.  */
116152  else {
116153    Table *pOld;
116154    Schema *pSchema = pTab->pSchema;
116155    const char *zName = pTab->zName;
116156    assert( sqlite3SchemaMutexHeld(db, 0, pSchema) );
116157    pOld = sqlite3HashInsert(&pSchema->tblHash, zName, pTab);
116158    if( pOld ){
116159      db->mallocFailed = 1;
116160      assert( pTab==pOld );  /* Malloc must have failed inside HashInsert() */
116161      return;
116162    }
116163    pParse->pNewTable = 0;
116164  }
116165}
116166
116167/*
116168** The parser calls this routine when it sees the first token
116169** of an argument to the module name in a CREATE VIRTUAL TABLE statement.
116170*/
116171SQLITE_PRIVATE void sqlite3VtabArgInit(Parse *pParse){
116172  addArgumentToVtab(pParse);
116173  pParse->sArg.z = 0;
116174  pParse->sArg.n = 0;
116175}
116176
116177/*
116178** The parser calls this routine for each token after the first token
116179** in an argument to the module name in a CREATE VIRTUAL TABLE statement.
116180*/
116181SQLITE_PRIVATE void sqlite3VtabArgExtend(Parse *pParse, Token *p){
116182  Token *pArg = &pParse->sArg;
116183  if( pArg->z==0 ){
116184    pArg->z = p->z;
116185    pArg->n = p->n;
116186  }else{
116187    assert(pArg->z <= p->z);
116188    pArg->n = (int)(&p->z[p->n] - pArg->z);
116189  }
116190}
116191
116192/*
116193** Invoke a virtual table constructor (either xCreate or xConnect). The
116194** pointer to the function to invoke is passed as the fourth parameter
116195** to this procedure.
116196*/
116197static int vtabCallConstructor(
116198  sqlite3 *db,
116199  Table *pTab,
116200  Module *pMod,
116201  int (*xConstruct)(sqlite3*,void*,int,const char*const*,sqlite3_vtab**,char**),
116202  char **pzErr
116203){
116204  VtabCtx sCtx;
116205  VTable *pVTable;
116206  int rc;
116207  const char *const*azArg = (const char *const*)pTab->azModuleArg;
116208  int nArg = pTab->nModuleArg;
116209  char *zErr = 0;
116210  char *zModuleName;
116211  int iDb;
116212  VtabCtx *pCtx;
116213
116214  /* Check that the virtual-table is not already being initialized */
116215  for(pCtx=db->pVtabCtx; pCtx; pCtx=pCtx->pPrior){
116216    if( pCtx->pTab==pTab ){
116217      *pzErr = sqlite3MPrintf(db,
116218          "vtable constructor called recursively: %s", pTab->zName
116219      );
116220      return SQLITE_LOCKED;
116221    }
116222  }
116223
116224  zModuleName = sqlite3MPrintf(db, "%s", pTab->zName);
116225  if( !zModuleName ){
116226    return SQLITE_NOMEM;
116227  }
116228
116229  pVTable = sqlite3DbMallocZero(db, sizeof(VTable));
116230  if( !pVTable ){
116231    sqlite3DbFree(db, zModuleName);
116232    return SQLITE_NOMEM;
116233  }
116234  pVTable->db = db;
116235  pVTable->pMod = pMod;
116236
116237  iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
116238  pTab->azModuleArg[1] = db->aDb[iDb].zName;
116239
116240  /* Invoke the virtual table constructor */
116241  assert( &db->pVtabCtx );
116242  assert( xConstruct );
116243  sCtx.pTab = pTab;
116244  sCtx.pVTable = pVTable;
116245  sCtx.pPrior = db->pVtabCtx;
116246  sCtx.bDeclared = 0;
116247  db->pVtabCtx = &sCtx;
116248  rc = xConstruct(db, pMod->pAux, nArg, azArg, &pVTable->pVtab, &zErr);
116249  db->pVtabCtx = sCtx.pPrior;
116250  if( rc==SQLITE_NOMEM ) db->mallocFailed = 1;
116251  assert( sCtx.pTab==pTab );
116252
116253  if( SQLITE_OK!=rc ){
116254    if( zErr==0 ){
116255      *pzErr = sqlite3MPrintf(db, "vtable constructor failed: %s", zModuleName);
116256    }else {
116257      *pzErr = sqlite3MPrintf(db, "%s", zErr);
116258      sqlite3_free(zErr);
116259    }
116260    sqlite3DbFree(db, pVTable);
116261  }else if( ALWAYS(pVTable->pVtab) ){
116262    /* Justification of ALWAYS():  A correct vtab constructor must allocate
116263    ** the sqlite3_vtab object if successful.  */
116264    memset(pVTable->pVtab, 0, sizeof(pVTable->pVtab[0]));
116265    pVTable->pVtab->pModule = pMod->pModule;
116266    pVTable->nRef = 1;
116267    if( sCtx.bDeclared==0 ){
116268      const char *zFormat = "vtable constructor did not declare schema: %s";
116269      *pzErr = sqlite3MPrintf(db, zFormat, pTab->zName);
116270      sqlite3VtabUnlock(pVTable);
116271      rc = SQLITE_ERROR;
116272    }else{
116273      int iCol;
116274      u8 oooHidden = 0;
116275      /* If everything went according to plan, link the new VTable structure
116276      ** into the linked list headed by pTab->pVTable. Then loop through the
116277      ** columns of the table to see if any of them contain the token "hidden".
116278      ** If so, set the Column COLFLAG_HIDDEN flag and remove the token from
116279      ** the type string.  */
116280      pVTable->pNext = pTab->pVTable;
116281      pTab->pVTable = pVTable;
116282
116283      for(iCol=0; iCol<pTab->nCol; iCol++){
116284        char *zType = pTab->aCol[iCol].zType;
116285        int nType;
116286        int i = 0;
116287        if( !zType ){
116288          pTab->tabFlags |= oooHidden;
116289          continue;
116290        }
116291        nType = sqlite3Strlen30(zType);
116292        if( sqlite3StrNICmp("hidden", zType, 6)||(zType[6] && zType[6]!=' ') ){
116293          for(i=0; i<nType; i++){
116294            if( (0==sqlite3StrNICmp(" hidden", &zType[i], 7))
116295             && (zType[i+7]=='\0' || zType[i+7]==' ')
116296            ){
116297              i++;
116298              break;
116299            }
116300          }
116301        }
116302        if( i<nType ){
116303          int j;
116304          int nDel = 6 + (zType[i+6] ? 1 : 0);
116305          for(j=i; (j+nDel)<=nType; j++){
116306            zType[j] = zType[j+nDel];
116307          }
116308          if( zType[i]=='\0' && i>0 ){
116309            assert(zType[i-1]==' ');
116310            zType[i-1] = '\0';
116311          }
116312          pTab->aCol[iCol].colFlags |= COLFLAG_HIDDEN;
116313          oooHidden = TF_OOOHidden;
116314        }else{
116315          pTab->tabFlags |= oooHidden;
116316        }
116317      }
116318    }
116319  }
116320
116321  sqlite3DbFree(db, zModuleName);
116322  return rc;
116323}
116324
116325/*
116326** This function is invoked by the parser to call the xConnect() method
116327** of the virtual table pTab. If an error occurs, an error code is returned
116328** and an error left in pParse.
116329**
116330** This call is a no-op if table pTab is not a virtual table.
116331*/
116332SQLITE_PRIVATE int sqlite3VtabCallConnect(Parse *pParse, Table *pTab){
116333  sqlite3 *db = pParse->db;
116334  const char *zMod;
116335  Module *pMod;
116336  int rc;
116337
116338  assert( pTab );
116339  if( (pTab->tabFlags & TF_Virtual)==0 || sqlite3GetVTable(db, pTab) ){
116340    return SQLITE_OK;
116341  }
116342
116343  /* Locate the required virtual table module */
116344  zMod = pTab->azModuleArg[0];
116345  pMod = (Module*)sqlite3HashFind(&db->aModule, zMod);
116346
116347  if( !pMod ){
116348    const char *zModule = pTab->azModuleArg[0];
116349    sqlite3ErrorMsg(pParse, "no such module: %s", zModule);
116350    rc = SQLITE_ERROR;
116351  }else{
116352    char *zErr = 0;
116353    rc = vtabCallConstructor(db, pTab, pMod, pMod->pModule->xConnect, &zErr);
116354    if( rc!=SQLITE_OK ){
116355      sqlite3ErrorMsg(pParse, "%s", zErr);
116356    }
116357    sqlite3DbFree(db, zErr);
116358  }
116359
116360  return rc;
116361}
116362/*
116363** Grow the db->aVTrans[] array so that there is room for at least one
116364** more v-table. Return SQLITE_NOMEM if a malloc fails, or SQLITE_OK otherwise.
116365*/
116366static int growVTrans(sqlite3 *db){
116367  const int ARRAY_INCR = 5;
116368
116369  /* Grow the sqlite3.aVTrans array if required */
116370  if( (db->nVTrans%ARRAY_INCR)==0 ){
116371    VTable **aVTrans;
116372    int nBytes = sizeof(sqlite3_vtab *) * (db->nVTrans + ARRAY_INCR);
116373    aVTrans = sqlite3DbRealloc(db, (void *)db->aVTrans, nBytes);
116374    if( !aVTrans ){
116375      return SQLITE_NOMEM;
116376    }
116377    memset(&aVTrans[db->nVTrans], 0, sizeof(sqlite3_vtab *)*ARRAY_INCR);
116378    db->aVTrans = aVTrans;
116379  }
116380
116381  return SQLITE_OK;
116382}
116383
116384/*
116385** Add the virtual table pVTab to the array sqlite3.aVTrans[]. Space should
116386** have already been reserved using growVTrans().
116387*/
116388static void addToVTrans(sqlite3 *db, VTable *pVTab){
116389  /* Add pVtab to the end of sqlite3.aVTrans */
116390  db->aVTrans[db->nVTrans++] = pVTab;
116391  sqlite3VtabLock(pVTab);
116392}
116393
116394/*
116395** This function is invoked by the vdbe to call the xCreate method
116396** of the virtual table named zTab in database iDb.
116397**
116398** If an error occurs, *pzErr is set to point an an English language
116399** description of the error and an SQLITE_XXX error code is returned.
116400** In this case the caller must call sqlite3DbFree(db, ) on *pzErr.
116401*/
116402SQLITE_PRIVATE int sqlite3VtabCallCreate(sqlite3 *db, int iDb, const char *zTab, char **pzErr){
116403  int rc = SQLITE_OK;
116404  Table *pTab;
116405  Module *pMod;
116406  const char *zMod;
116407
116408  pTab = sqlite3FindTable(db, zTab, db->aDb[iDb].zName);
116409  assert( pTab && (pTab->tabFlags & TF_Virtual)!=0 && !pTab->pVTable );
116410
116411  /* Locate the required virtual table module */
116412  zMod = pTab->azModuleArg[0];
116413  pMod = (Module*)sqlite3HashFind(&db->aModule, zMod);
116414
116415  /* If the module has been registered and includes a Create method,
116416  ** invoke it now. If the module has not been registered, return an
116417  ** error. Otherwise, do nothing.
116418  */
116419  if( !pMod ){
116420    *pzErr = sqlite3MPrintf(db, "no such module: %s", zMod);
116421    rc = SQLITE_ERROR;
116422  }else{
116423    rc = vtabCallConstructor(db, pTab, pMod, pMod->pModule->xCreate, pzErr);
116424  }
116425
116426  /* Justification of ALWAYS():  The xConstructor method is required to
116427  ** create a valid sqlite3_vtab if it returns SQLITE_OK. */
116428  if( rc==SQLITE_OK && ALWAYS(sqlite3GetVTable(db, pTab)) ){
116429    rc = growVTrans(db);
116430    if( rc==SQLITE_OK ){
116431      addToVTrans(db, sqlite3GetVTable(db, pTab));
116432    }
116433  }
116434
116435  return rc;
116436}
116437
116438/*
116439** This function is used to set the schema of a virtual table.  It is only
116440** valid to call this function from within the xCreate() or xConnect() of a
116441** virtual table module.
116442*/
116443SQLITE_API int SQLITE_STDCALL sqlite3_declare_vtab(sqlite3 *db, const char *zCreateTable){
116444  VtabCtx *pCtx;
116445  Parse *pParse;
116446  int rc = SQLITE_OK;
116447  Table *pTab;
116448  char *zErr = 0;
116449
116450#ifdef SQLITE_ENABLE_API_ARMOR
116451  if( !sqlite3SafetyCheckOk(db) || zCreateTable==0 ){
116452    return SQLITE_MISUSE_BKPT;
116453  }
116454#endif
116455  sqlite3_mutex_enter(db->mutex);
116456  pCtx = db->pVtabCtx;
116457  if( !pCtx || pCtx->bDeclared ){
116458    sqlite3Error(db, SQLITE_MISUSE);
116459    sqlite3_mutex_leave(db->mutex);
116460    return SQLITE_MISUSE_BKPT;
116461  }
116462  pTab = pCtx->pTab;
116463  assert( (pTab->tabFlags & TF_Virtual)!=0 );
116464
116465  pParse = sqlite3StackAllocZero(db, sizeof(*pParse));
116466  if( pParse==0 ){
116467    rc = SQLITE_NOMEM;
116468  }else{
116469    pParse->declareVtab = 1;
116470    pParse->db = db;
116471    pParse->nQueryLoop = 1;
116472
116473    if( SQLITE_OK==sqlite3RunParser(pParse, zCreateTable, &zErr)
116474     && pParse->pNewTable
116475     && !db->mallocFailed
116476     && !pParse->pNewTable->pSelect
116477     && (pParse->pNewTable->tabFlags & TF_Virtual)==0
116478    ){
116479      if( !pTab->aCol ){
116480        pTab->aCol = pParse->pNewTable->aCol;
116481        pTab->nCol = pParse->pNewTable->nCol;
116482        pParse->pNewTable->nCol = 0;
116483        pParse->pNewTable->aCol = 0;
116484      }
116485      pCtx->bDeclared = 1;
116486    }else{
116487      sqlite3ErrorWithMsg(db, SQLITE_ERROR, (zErr ? "%s" : 0), zErr);
116488      sqlite3DbFree(db, zErr);
116489      rc = SQLITE_ERROR;
116490    }
116491    pParse->declareVtab = 0;
116492
116493    if( pParse->pVdbe ){
116494      sqlite3VdbeFinalize(pParse->pVdbe);
116495    }
116496    sqlite3DeleteTable(db, pParse->pNewTable);
116497    sqlite3ParserReset(pParse);
116498    sqlite3StackFree(db, pParse);
116499  }
116500
116501  assert( (rc&0xff)==rc );
116502  rc = sqlite3ApiExit(db, rc);
116503  sqlite3_mutex_leave(db->mutex);
116504  return rc;
116505}
116506
116507/*
116508** This function is invoked by the vdbe to call the xDestroy method
116509** of the virtual table named zTab in database iDb. This occurs
116510** when a DROP TABLE is mentioned.
116511**
116512** This call is a no-op if zTab is not a virtual table.
116513*/
116514SQLITE_PRIVATE int sqlite3VtabCallDestroy(sqlite3 *db, int iDb, const char *zTab){
116515  int rc = SQLITE_OK;
116516  Table *pTab;
116517
116518  pTab = sqlite3FindTable(db, zTab, db->aDb[iDb].zName);
116519  if( ALWAYS(pTab!=0 && pTab->pVTable!=0) ){
116520    VTable *p;
116521    for(p=pTab->pVTable; p; p=p->pNext){
116522      assert( p->pVtab );
116523      if( p->pVtab->nRef>0 ){
116524        return SQLITE_LOCKED;
116525      }
116526    }
116527    p = vtabDisconnectAll(db, pTab);
116528    rc = p->pMod->pModule->xDestroy(p->pVtab);
116529    /* Remove the sqlite3_vtab* from the aVTrans[] array, if applicable */
116530    if( rc==SQLITE_OK ){
116531      assert( pTab->pVTable==p && p->pNext==0 );
116532      p->pVtab = 0;
116533      pTab->pVTable = 0;
116534      sqlite3VtabUnlock(p);
116535    }
116536  }
116537
116538  return rc;
116539}
116540
116541/*
116542** This function invokes either the xRollback or xCommit method
116543** of each of the virtual tables in the sqlite3.aVTrans array. The method
116544** called is identified by the second argument, "offset", which is
116545** the offset of the method to call in the sqlite3_module structure.
116546**
116547** The array is cleared after invoking the callbacks.
116548*/
116549static void callFinaliser(sqlite3 *db, int offset){
116550  int i;
116551  if( db->aVTrans ){
116552    VTable **aVTrans = db->aVTrans;
116553    db->aVTrans = 0;
116554    for(i=0; i<db->nVTrans; i++){
116555      VTable *pVTab = aVTrans[i];
116556      sqlite3_vtab *p = pVTab->pVtab;
116557      if( p ){
116558        int (*x)(sqlite3_vtab *);
116559        x = *(int (**)(sqlite3_vtab *))((char *)p->pModule + offset);
116560        if( x ) x(p);
116561      }
116562      pVTab->iSavepoint = 0;
116563      sqlite3VtabUnlock(pVTab);
116564    }
116565    sqlite3DbFree(db, aVTrans);
116566    db->nVTrans = 0;
116567  }
116568}
116569
116570/*
116571** Invoke the xSync method of all virtual tables in the sqlite3.aVTrans
116572** array. Return the error code for the first error that occurs, or
116573** SQLITE_OK if all xSync operations are successful.
116574**
116575** If an error message is available, leave it in p->zErrMsg.
116576*/
116577SQLITE_PRIVATE int sqlite3VtabSync(sqlite3 *db, Vdbe *p){
116578  int i;
116579  int rc = SQLITE_OK;
116580  VTable **aVTrans = db->aVTrans;
116581
116582  db->aVTrans = 0;
116583  for(i=0; rc==SQLITE_OK && i<db->nVTrans; i++){
116584    int (*x)(sqlite3_vtab *);
116585    sqlite3_vtab *pVtab = aVTrans[i]->pVtab;
116586    if( pVtab && (x = pVtab->pModule->xSync)!=0 ){
116587      rc = x(pVtab);
116588      sqlite3VtabImportErrmsg(p, pVtab);
116589    }
116590  }
116591  db->aVTrans = aVTrans;
116592  return rc;
116593}
116594
116595/*
116596** Invoke the xRollback method of all virtual tables in the
116597** sqlite3.aVTrans array. Then clear the array itself.
116598*/
116599SQLITE_PRIVATE int sqlite3VtabRollback(sqlite3 *db){
116600  callFinaliser(db, offsetof(sqlite3_module,xRollback));
116601  return SQLITE_OK;
116602}
116603
116604/*
116605** Invoke the xCommit method of all virtual tables in the
116606** sqlite3.aVTrans array. Then clear the array itself.
116607*/
116608SQLITE_PRIVATE int sqlite3VtabCommit(sqlite3 *db){
116609  callFinaliser(db, offsetof(sqlite3_module,xCommit));
116610  return SQLITE_OK;
116611}
116612
116613/*
116614** If the virtual table pVtab supports the transaction interface
116615** (xBegin/xRollback/xCommit and optionally xSync) and a transaction is
116616** not currently open, invoke the xBegin method now.
116617**
116618** If the xBegin call is successful, place the sqlite3_vtab pointer
116619** in the sqlite3.aVTrans array.
116620*/
116621SQLITE_PRIVATE int sqlite3VtabBegin(sqlite3 *db, VTable *pVTab){
116622  int rc = SQLITE_OK;
116623  const sqlite3_module *pModule;
116624
116625  /* Special case: If db->aVTrans is NULL and db->nVTrans is greater
116626  ** than zero, then this function is being called from within a
116627  ** virtual module xSync() callback. It is illegal to write to
116628  ** virtual module tables in this case, so return SQLITE_LOCKED.
116629  */
116630  if( sqlite3VtabInSync(db) ){
116631    return SQLITE_LOCKED;
116632  }
116633  if( !pVTab ){
116634    return SQLITE_OK;
116635  }
116636  pModule = pVTab->pVtab->pModule;
116637
116638  if( pModule->xBegin ){
116639    int i;
116640
116641    /* If pVtab is already in the aVTrans array, return early */
116642    for(i=0; i<db->nVTrans; i++){
116643      if( db->aVTrans[i]==pVTab ){
116644        return SQLITE_OK;
116645      }
116646    }
116647
116648    /* Invoke the xBegin method. If successful, add the vtab to the
116649    ** sqlite3.aVTrans[] array. */
116650    rc = growVTrans(db);
116651    if( rc==SQLITE_OK ){
116652      rc = pModule->xBegin(pVTab->pVtab);
116653      if( rc==SQLITE_OK ){
116654        addToVTrans(db, pVTab);
116655      }
116656    }
116657  }
116658  return rc;
116659}
116660
116661/*
116662** Invoke either the xSavepoint, xRollbackTo or xRelease method of all
116663** virtual tables that currently have an open transaction. Pass iSavepoint
116664** as the second argument to the virtual table method invoked.
116665**
116666** If op is SAVEPOINT_BEGIN, the xSavepoint method is invoked. If it is
116667** SAVEPOINT_ROLLBACK, the xRollbackTo method. Otherwise, if op is
116668** SAVEPOINT_RELEASE, then the xRelease method of each virtual table with
116669** an open transaction is invoked.
116670**
116671** If any virtual table method returns an error code other than SQLITE_OK,
116672** processing is abandoned and the error returned to the caller of this
116673** function immediately. If all calls to virtual table methods are successful,
116674** SQLITE_OK is returned.
116675*/
116676SQLITE_PRIVATE int sqlite3VtabSavepoint(sqlite3 *db, int op, int iSavepoint){
116677  int rc = SQLITE_OK;
116678
116679  assert( op==SAVEPOINT_RELEASE||op==SAVEPOINT_ROLLBACK||op==SAVEPOINT_BEGIN );
116680  assert( iSavepoint>=-1 );
116681  if( db->aVTrans ){
116682    int i;
116683    for(i=0; rc==SQLITE_OK && i<db->nVTrans; i++){
116684      VTable *pVTab = db->aVTrans[i];
116685      const sqlite3_module *pMod = pVTab->pMod->pModule;
116686      if( pVTab->pVtab && pMod->iVersion>=2 ){
116687        int (*xMethod)(sqlite3_vtab *, int);
116688        switch( op ){
116689          case SAVEPOINT_BEGIN:
116690            xMethod = pMod->xSavepoint;
116691            pVTab->iSavepoint = iSavepoint+1;
116692            break;
116693          case SAVEPOINT_ROLLBACK:
116694            xMethod = pMod->xRollbackTo;
116695            break;
116696          default:
116697            xMethod = pMod->xRelease;
116698            break;
116699        }
116700        if( xMethod && pVTab->iSavepoint>iSavepoint ){
116701          rc = xMethod(pVTab->pVtab, iSavepoint);
116702        }
116703      }
116704    }
116705  }
116706  return rc;
116707}
116708
116709/*
116710** The first parameter (pDef) is a function implementation.  The
116711** second parameter (pExpr) is the first argument to this function.
116712** If pExpr is a column in a virtual table, then let the virtual
116713** table implementation have an opportunity to overload the function.
116714**
116715** This routine is used to allow virtual table implementations to
116716** overload MATCH, LIKE, GLOB, and REGEXP operators.
116717**
116718** Return either the pDef argument (indicating no change) or a
116719** new FuncDef structure that is marked as ephemeral using the
116720** SQLITE_FUNC_EPHEM flag.
116721*/
116722SQLITE_PRIVATE FuncDef *sqlite3VtabOverloadFunction(
116723  sqlite3 *db,    /* Database connection for reporting malloc problems */
116724  FuncDef *pDef,  /* Function to possibly overload */
116725  int nArg,       /* Number of arguments to the function */
116726  Expr *pExpr     /* First argument to the function */
116727){
116728  Table *pTab;
116729  sqlite3_vtab *pVtab;
116730  sqlite3_module *pMod;
116731  void (*xFunc)(sqlite3_context*,int,sqlite3_value**) = 0;
116732  void *pArg = 0;
116733  FuncDef *pNew;
116734  int rc = 0;
116735  char *zLowerName;
116736  unsigned char *z;
116737
116738
116739  /* Check to see the left operand is a column in a virtual table */
116740  if( NEVER(pExpr==0) ) return pDef;
116741  if( pExpr->op!=TK_COLUMN ) return pDef;
116742  pTab = pExpr->pTab;
116743  if( NEVER(pTab==0) ) return pDef;
116744  if( (pTab->tabFlags & TF_Virtual)==0 ) return pDef;
116745  pVtab = sqlite3GetVTable(db, pTab)->pVtab;
116746  assert( pVtab!=0 );
116747  assert( pVtab->pModule!=0 );
116748  pMod = (sqlite3_module *)pVtab->pModule;
116749  if( pMod->xFindFunction==0 ) return pDef;
116750
116751  /* Call the xFindFunction method on the virtual table implementation
116752  ** to see if the implementation wants to overload this function
116753  */
116754  zLowerName = sqlite3DbStrDup(db, pDef->zName);
116755  if( zLowerName ){
116756    for(z=(unsigned char*)zLowerName; *z; z++){
116757      *z = sqlite3UpperToLower[*z];
116758    }
116759    rc = pMod->xFindFunction(pVtab, nArg, zLowerName, &xFunc, &pArg);
116760    sqlite3DbFree(db, zLowerName);
116761  }
116762  if( rc==0 ){
116763    return pDef;
116764  }
116765
116766  /* Create a new ephemeral function definition for the overloaded
116767  ** function */
116768  pNew = sqlite3DbMallocZero(db, sizeof(*pNew)
116769                             + sqlite3Strlen30(pDef->zName) + 1);
116770  if( pNew==0 ){
116771    return pDef;
116772  }
116773  *pNew = *pDef;
116774  pNew->zName = (char *)&pNew[1];
116775  memcpy(pNew->zName, pDef->zName, sqlite3Strlen30(pDef->zName)+1);
116776  pNew->xFunc = xFunc;
116777  pNew->pUserData = pArg;
116778  pNew->funcFlags |= SQLITE_FUNC_EPHEM;
116779  return pNew;
116780}
116781
116782/*
116783** Make sure virtual table pTab is contained in the pParse->apVirtualLock[]
116784** array so that an OP_VBegin will get generated for it.  Add pTab to the
116785** array if it is missing.  If pTab is already in the array, this routine
116786** is a no-op.
116787*/
116788SQLITE_PRIVATE void sqlite3VtabMakeWritable(Parse *pParse, Table *pTab){
116789  Parse *pToplevel = sqlite3ParseToplevel(pParse);
116790  int i, n;
116791  Table **apVtabLock;
116792
116793  assert( IsVirtual(pTab) );
116794  for(i=0; i<pToplevel->nVtabLock; i++){
116795    if( pTab==pToplevel->apVtabLock[i] ) return;
116796  }
116797  n = (pToplevel->nVtabLock+1)*sizeof(pToplevel->apVtabLock[0]);
116798  apVtabLock = sqlite3_realloc64(pToplevel->apVtabLock, n);
116799  if( apVtabLock ){
116800    pToplevel->apVtabLock = apVtabLock;
116801    pToplevel->apVtabLock[pToplevel->nVtabLock++] = pTab;
116802  }else{
116803    pToplevel->db->mallocFailed = 1;
116804  }
116805}
116806
116807/*
116808** Return the ON CONFLICT resolution mode in effect for the virtual
116809** table update operation currently in progress.
116810**
116811** The results of this routine are undefined unless it is called from
116812** within an xUpdate method.
116813*/
116814SQLITE_API int SQLITE_STDCALL sqlite3_vtab_on_conflict(sqlite3 *db){
116815  static const unsigned char aMap[] = {
116816    SQLITE_ROLLBACK, SQLITE_ABORT, SQLITE_FAIL, SQLITE_IGNORE, SQLITE_REPLACE
116817  };
116818#ifdef SQLITE_ENABLE_API_ARMOR
116819  if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT;
116820#endif
116821  assert( OE_Rollback==1 && OE_Abort==2 && OE_Fail==3 );
116822  assert( OE_Ignore==4 && OE_Replace==5 );
116823  assert( db->vtabOnConflict>=1 && db->vtabOnConflict<=5 );
116824  return (int)aMap[db->vtabOnConflict-1];
116825}
116826
116827/*
116828** Call from within the xCreate() or xConnect() methods to provide
116829** the SQLite core with additional information about the behavior
116830** of the virtual table being implemented.
116831*/
116832SQLITE_API int SQLITE_CDECL sqlite3_vtab_config(sqlite3 *db, int op, ...){
116833  va_list ap;
116834  int rc = SQLITE_OK;
116835
116836#ifdef SQLITE_ENABLE_API_ARMOR
116837  if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT;
116838#endif
116839  sqlite3_mutex_enter(db->mutex);
116840  va_start(ap, op);
116841  switch( op ){
116842    case SQLITE_VTAB_CONSTRAINT_SUPPORT: {
116843      VtabCtx *p = db->pVtabCtx;
116844      if( !p ){
116845        rc = SQLITE_MISUSE_BKPT;
116846      }else{
116847        assert( p->pTab==0 || (p->pTab->tabFlags & TF_Virtual)!=0 );
116848        p->pVTable->bConstraint = (u8)va_arg(ap, int);
116849      }
116850      break;
116851    }
116852    default:
116853      rc = SQLITE_MISUSE_BKPT;
116854      break;
116855  }
116856  va_end(ap);
116857
116858  if( rc!=SQLITE_OK ) sqlite3Error(db, rc);
116859  sqlite3_mutex_leave(db->mutex);
116860  return rc;
116861}
116862
116863#endif /* SQLITE_OMIT_VIRTUALTABLE */
116864
116865/************** End of vtab.c ************************************************/
116866/************** Begin file wherecode.c ***************************************/
116867/*
116868** 2015-06-06
116869**
116870** The author disclaims copyright to this source code.  In place of
116871** a legal notice, here is a blessing:
116872**
116873**    May you do good and not evil.
116874**    May you find forgiveness for yourself and forgive others.
116875**    May you share freely, never taking more than you give.
116876**
116877*************************************************************************
116878** This module contains C code that generates VDBE code used to process
116879** the WHERE clause of SQL statements.
116880**
116881** This file was split off from where.c on 2015-06-06 in order to reduce the
116882** size of where.c and make it easier to edit.  This file contains the routines
116883** that actually generate the bulk of the WHERE loop code.  The original where.c
116884** file retains the code that does query planning and analysis.
116885*/
116886/* #include "sqliteInt.h" */
116887/************** Include whereInt.h in the middle of wherecode.c **************/
116888/************** Begin file whereInt.h ****************************************/
116889/*
116890** 2013-11-12
116891**
116892** The author disclaims copyright to this source code.  In place of
116893** a legal notice, here is a blessing:
116894**
116895**    May you do good and not evil.
116896**    May you find forgiveness for yourself and forgive others.
116897**    May you share freely, never taking more than you give.
116898**
116899*************************************************************************
116900**
116901** This file contains structure and macro definitions for the query
116902** planner logic in "where.c".  These definitions are broken out into
116903** a separate source file for easier editing.
116904*/
116905
116906/*
116907** Trace output macros
116908*/
116909#if defined(SQLITE_TEST) || defined(SQLITE_DEBUG)
116910/***/ int sqlite3WhereTrace;
116911#endif
116912#if defined(SQLITE_DEBUG) \
116913    && (defined(SQLITE_TEST) || defined(SQLITE_ENABLE_WHERETRACE))
116914# define WHERETRACE(K,X)  if(sqlite3WhereTrace&(K)) sqlite3DebugPrintf X
116915# define WHERETRACE_ENABLED 1
116916#else
116917# define WHERETRACE(K,X)
116918#endif
116919
116920/* Forward references
116921*/
116922typedef struct WhereClause WhereClause;
116923typedef struct WhereMaskSet WhereMaskSet;
116924typedef struct WhereOrInfo WhereOrInfo;
116925typedef struct WhereAndInfo WhereAndInfo;
116926typedef struct WhereLevel WhereLevel;
116927typedef struct WhereLoop WhereLoop;
116928typedef struct WherePath WherePath;
116929typedef struct WhereTerm WhereTerm;
116930typedef struct WhereLoopBuilder WhereLoopBuilder;
116931typedef struct WhereScan WhereScan;
116932typedef struct WhereOrCost WhereOrCost;
116933typedef struct WhereOrSet WhereOrSet;
116934
116935/*
116936** This object contains information needed to implement a single nested
116937** loop in WHERE clause.
116938**
116939** Contrast this object with WhereLoop.  This object describes the
116940** implementation of the loop.  WhereLoop describes the algorithm.
116941** This object contains a pointer to the WhereLoop algorithm as one of
116942** its elements.
116943**
116944** The WhereInfo object contains a single instance of this object for
116945** each term in the FROM clause (which is to say, for each of the
116946** nested loops as implemented).  The order of WhereLevel objects determines
116947** the loop nested order, with WhereInfo.a[0] being the outer loop and
116948** WhereInfo.a[WhereInfo.nLevel-1] being the inner loop.
116949*/
116950struct WhereLevel {
116951  int iLeftJoin;        /* Memory cell used to implement LEFT OUTER JOIN */
116952  int iTabCur;          /* The VDBE cursor used to access the table */
116953  int iIdxCur;          /* The VDBE cursor used to access pIdx */
116954  int addrBrk;          /* Jump here to break out of the loop */
116955  int addrNxt;          /* Jump here to start the next IN combination */
116956  int addrSkip;         /* Jump here for next iteration of skip-scan */
116957  int addrCont;         /* Jump here to continue with the next loop cycle */
116958  int addrFirst;        /* First instruction of interior of the loop */
116959  int addrBody;         /* Beginning of the body of this loop */
116960  int iLikeRepCntr;     /* LIKE range processing counter register */
116961  int addrLikeRep;      /* LIKE range processing address */
116962  u8 iFrom;             /* Which entry in the FROM clause */
116963  u8 op, p3, p5;        /* Opcode, P3 & P5 of the opcode that ends the loop */
116964  int p1, p2;           /* Operands of the opcode used to ends the loop */
116965  union {               /* Information that depends on pWLoop->wsFlags */
116966    struct {
116967      int nIn;              /* Number of entries in aInLoop[] */
116968      struct InLoop {
116969        int iCur;              /* The VDBE cursor used by this IN operator */
116970        int addrInTop;         /* Top of the IN loop */
116971        u8 eEndLoopOp;         /* IN Loop terminator. OP_Next or OP_Prev */
116972      } *aInLoop;           /* Information about each nested IN operator */
116973    } in;                 /* Used when pWLoop->wsFlags&WHERE_IN_ABLE */
116974    Index *pCovidx;       /* Possible covering index for WHERE_MULTI_OR */
116975  } u;
116976  struct WhereLoop *pWLoop;  /* The selected WhereLoop object */
116977  Bitmask notReady;          /* FROM entries not usable at this level */
116978#ifdef SQLITE_ENABLE_STMT_SCANSTATUS
116979  int addrVisit;        /* Address at which row is visited */
116980#endif
116981};
116982
116983/*
116984** Each instance of this object represents an algorithm for evaluating one
116985** term of a join.  Every term of the FROM clause will have at least
116986** one corresponding WhereLoop object (unless INDEXED BY constraints
116987** prevent a query solution - which is an error) and many terms of the
116988** FROM clause will have multiple WhereLoop objects, each describing a
116989** potential way of implementing that FROM-clause term, together with
116990** dependencies and cost estimates for using the chosen algorithm.
116991**
116992** Query planning consists of building up a collection of these WhereLoop
116993** objects, then computing a particular sequence of WhereLoop objects, with
116994** one WhereLoop object per FROM clause term, that satisfy all dependencies
116995** and that minimize the overall cost.
116996*/
116997struct WhereLoop {
116998  Bitmask prereq;       /* Bitmask of other loops that must run first */
116999  Bitmask maskSelf;     /* Bitmask identifying table iTab */
117000#ifdef SQLITE_DEBUG
117001  char cId;             /* Symbolic ID of this loop for debugging use */
117002#endif
117003  u8 iTab;              /* Position in FROM clause of table for this loop */
117004  u8 iSortIdx;          /* Sorting index number.  0==None */
117005  LogEst rSetup;        /* One-time setup cost (ex: create transient index) */
117006  LogEst rRun;          /* Cost of running each loop */
117007  LogEst nOut;          /* Estimated number of output rows */
117008  union {
117009    struct {               /* Information for internal btree tables */
117010      u16 nEq;               /* Number of equality constraints */
117011      Index *pIndex;         /* Index used, or NULL */
117012    } btree;
117013    struct {               /* Information for virtual tables */
117014      int idxNum;            /* Index number */
117015      u8 needFree;           /* True if sqlite3_free(idxStr) is needed */
117016      i8 isOrdered;          /* True if satisfies ORDER BY */
117017      u16 omitMask;          /* Terms that may be omitted */
117018      char *idxStr;          /* Index identifier string */
117019    } vtab;
117020  } u;
117021  u32 wsFlags;          /* WHERE_* flags describing the plan */
117022  u16 nLTerm;           /* Number of entries in aLTerm[] */
117023  u16 nSkip;            /* Number of NULL aLTerm[] entries */
117024  /**** whereLoopXfer() copies fields above ***********************/
117025# define WHERE_LOOP_XFER_SZ offsetof(WhereLoop,nLSlot)
117026  u16 nLSlot;           /* Number of slots allocated for aLTerm[] */
117027  WhereTerm **aLTerm;   /* WhereTerms used */
117028  WhereLoop *pNextLoop; /* Next WhereLoop object in the WhereClause */
117029  WhereTerm *aLTermSpace[3];  /* Initial aLTerm[] space */
117030};
117031
117032/* This object holds the prerequisites and the cost of running a
117033** subquery on one operand of an OR operator in the WHERE clause.
117034** See WhereOrSet for additional information
117035*/
117036struct WhereOrCost {
117037  Bitmask prereq;     /* Prerequisites */
117038  LogEst rRun;        /* Cost of running this subquery */
117039  LogEst nOut;        /* Number of outputs for this subquery */
117040};
117041
117042/* The WhereOrSet object holds a set of possible WhereOrCosts that
117043** correspond to the subquery(s) of OR-clause processing.  Only the
117044** best N_OR_COST elements are retained.
117045*/
117046#define N_OR_COST 3
117047struct WhereOrSet {
117048  u16 n;                      /* Number of valid a[] entries */
117049  WhereOrCost a[N_OR_COST];   /* Set of best costs */
117050};
117051
117052/*
117053** Each instance of this object holds a sequence of WhereLoop objects
117054** that implement some or all of a query plan.
117055**
117056** Think of each WhereLoop object as a node in a graph with arcs
117057** showing dependencies and costs for travelling between nodes.  (That is
117058** not a completely accurate description because WhereLoop costs are a
117059** vector, not a scalar, and because dependencies are many-to-one, not
117060** one-to-one as are graph nodes.  But it is a useful visualization aid.)
117061** Then a WherePath object is a path through the graph that visits some
117062** or all of the WhereLoop objects once.
117063**
117064** The "solver" works by creating the N best WherePath objects of length
117065** 1.  Then using those as a basis to compute the N best WherePath objects
117066** of length 2.  And so forth until the length of WherePaths equals the
117067** number of nodes in the FROM clause.  The best (lowest cost) WherePath
117068** at the end is the chosen query plan.
117069*/
117070struct WherePath {
117071  Bitmask maskLoop;     /* Bitmask of all WhereLoop objects in this path */
117072  Bitmask revLoop;      /* aLoop[]s that should be reversed for ORDER BY */
117073  LogEst nRow;          /* Estimated number of rows generated by this path */
117074  LogEst rCost;         /* Total cost of this path */
117075  LogEst rUnsorted;     /* Total cost of this path ignoring sorting costs */
117076  i8 isOrdered;         /* No. of ORDER BY terms satisfied. -1 for unknown */
117077  WhereLoop **aLoop;    /* Array of WhereLoop objects implementing this path */
117078};
117079
117080/*
117081** The query generator uses an array of instances of this structure to
117082** help it analyze the subexpressions of the WHERE clause.  Each WHERE
117083** clause subexpression is separated from the others by AND operators,
117084** usually, or sometimes subexpressions separated by OR.
117085**
117086** All WhereTerms are collected into a single WhereClause structure.
117087** The following identity holds:
117088**
117089**        WhereTerm.pWC->a[WhereTerm.idx] == WhereTerm
117090**
117091** When a term is of the form:
117092**
117093**              X <op> <expr>
117094**
117095** where X is a column name and <op> is one of certain operators,
117096** then WhereTerm.leftCursor and WhereTerm.u.leftColumn record the
117097** cursor number and column number for X.  WhereTerm.eOperator records
117098** the <op> using a bitmask encoding defined by WO_xxx below.  The
117099** use of a bitmask encoding for the operator allows us to search
117100** quickly for terms that match any of several different operators.
117101**
117102** A WhereTerm might also be two or more subterms connected by OR:
117103**
117104**         (t1.X <op> <expr>) OR (t1.Y <op> <expr>) OR ....
117105**
117106** In this second case, wtFlag has the TERM_ORINFO bit set and eOperator==WO_OR
117107** and the WhereTerm.u.pOrInfo field points to auxiliary information that
117108** is collected about the OR clause.
117109**
117110** If a term in the WHERE clause does not match either of the two previous
117111** categories, then eOperator==0.  The WhereTerm.pExpr field is still set
117112** to the original subexpression content and wtFlags is set up appropriately
117113** but no other fields in the WhereTerm object are meaningful.
117114**
117115** When eOperator!=0, prereqRight and prereqAll record sets of cursor numbers,
117116** but they do so indirectly.  A single WhereMaskSet structure translates
117117** cursor number into bits and the translated bit is stored in the prereq
117118** fields.  The translation is used in order to maximize the number of
117119** bits that will fit in a Bitmask.  The VDBE cursor numbers might be
117120** spread out over the non-negative integers.  For example, the cursor
117121** numbers might be 3, 8, 9, 10, 20, 23, 41, and 45.  The WhereMaskSet
117122** translates these sparse cursor numbers into consecutive integers
117123** beginning with 0 in order to make the best possible use of the available
117124** bits in the Bitmask.  So, in the example above, the cursor numbers
117125** would be mapped into integers 0 through 7.
117126**
117127** The number of terms in a join is limited by the number of bits
117128** in prereqRight and prereqAll.  The default is 64 bits, hence SQLite
117129** is only able to process joins with 64 or fewer tables.
117130*/
117131struct WhereTerm {
117132  Expr *pExpr;            /* Pointer to the subexpression that is this term */
117133  int iParent;            /* Disable pWC->a[iParent] when this term disabled */
117134  int leftCursor;         /* Cursor number of X in "X <op> <expr>" */
117135  union {
117136    int leftColumn;         /* Column number of X in "X <op> <expr>" */
117137    WhereOrInfo *pOrInfo;   /* Extra information if (eOperator & WO_OR)!=0 */
117138    WhereAndInfo *pAndInfo; /* Extra information if (eOperator& WO_AND)!=0 */
117139  } u;
117140  LogEst truthProb;       /* Probability of truth for this expression */
117141  u16 eOperator;          /* A WO_xx value describing <op> */
117142  u16 wtFlags;            /* TERM_xxx bit flags.  See below */
117143  u8 nChild;              /* Number of children that must disable us */
117144  WhereClause *pWC;       /* The clause this term is part of */
117145  Bitmask prereqRight;    /* Bitmask of tables used by pExpr->pRight */
117146  Bitmask prereqAll;      /* Bitmask of tables referenced by pExpr */
117147};
117148
117149/*
117150** Allowed values of WhereTerm.wtFlags
117151*/
117152#define TERM_DYNAMIC    0x01   /* Need to call sqlite3ExprDelete(db, pExpr) */
117153#define TERM_VIRTUAL    0x02   /* Added by the optimizer.  Do not code */
117154#define TERM_CODED      0x04   /* This term is already coded */
117155#define TERM_COPIED     0x08   /* Has a child */
117156#define TERM_ORINFO     0x10   /* Need to free the WhereTerm.u.pOrInfo object */
117157#define TERM_ANDINFO    0x20   /* Need to free the WhereTerm.u.pAndInfo obj */
117158#define TERM_OR_OK      0x40   /* Used during OR-clause processing */
117159#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
117160#  define TERM_VNULL    0x80   /* Manufactured x>NULL or x<=NULL term */
117161#else
117162#  define TERM_VNULL    0x00   /* Disabled if not using stat3 */
117163#endif
117164#define TERM_LIKEOPT    0x100  /* Virtual terms from the LIKE optimization */
117165#define TERM_LIKECOND   0x200  /* Conditionally this LIKE operator term */
117166#define TERM_LIKE       0x400  /* The original LIKE operator */
117167#define TERM_IS         0x800  /* Term.pExpr is an IS operator */
117168
117169/*
117170** An instance of the WhereScan object is used as an iterator for locating
117171** terms in the WHERE clause that are useful to the query planner.
117172*/
117173struct WhereScan {
117174  WhereClause *pOrigWC;      /* Original, innermost WhereClause */
117175  WhereClause *pWC;          /* WhereClause currently being scanned */
117176  char *zCollName;           /* Required collating sequence, if not NULL */
117177  char idxaff;               /* Must match this affinity, if zCollName!=NULL */
117178  unsigned char nEquiv;      /* Number of entries in aEquiv[] */
117179  unsigned char iEquiv;      /* Next unused slot in aEquiv[] */
117180  u32 opMask;                /* Acceptable operators */
117181  int k;                     /* Resume scanning at this->pWC->a[this->k] */
117182  int aEquiv[22];            /* Cursor,Column pairs for equivalence classes */
117183};
117184
117185/*
117186** An instance of the following structure holds all information about a
117187** WHERE clause.  Mostly this is a container for one or more WhereTerms.
117188**
117189** Explanation of pOuter:  For a WHERE clause of the form
117190**
117191**           a AND ((b AND c) OR (d AND e)) AND f
117192**
117193** There are separate WhereClause objects for the whole clause and for
117194** the subclauses "(b AND c)" and "(d AND e)".  The pOuter field of the
117195** subclauses points to the WhereClause object for the whole clause.
117196*/
117197struct WhereClause {
117198  WhereInfo *pWInfo;       /* WHERE clause processing context */
117199  WhereClause *pOuter;     /* Outer conjunction */
117200  u8 op;                   /* Split operator.  TK_AND or TK_OR */
117201  int nTerm;               /* Number of terms */
117202  int nSlot;               /* Number of entries in a[] */
117203  WhereTerm *a;            /* Each a[] describes a term of the WHERE cluase */
117204#if defined(SQLITE_SMALL_STACK)
117205  WhereTerm aStatic[1];    /* Initial static space for a[] */
117206#else
117207  WhereTerm aStatic[8];    /* Initial static space for a[] */
117208#endif
117209};
117210
117211/*
117212** A WhereTerm with eOperator==WO_OR has its u.pOrInfo pointer set to
117213** a dynamically allocated instance of the following structure.
117214*/
117215struct WhereOrInfo {
117216  WhereClause wc;          /* Decomposition into subterms */
117217  Bitmask indexable;       /* Bitmask of all indexable tables in the clause */
117218};
117219
117220/*
117221** A WhereTerm with eOperator==WO_AND has its u.pAndInfo pointer set to
117222** a dynamically allocated instance of the following structure.
117223*/
117224struct WhereAndInfo {
117225  WhereClause wc;          /* The subexpression broken out */
117226};
117227
117228/*
117229** An instance of the following structure keeps track of a mapping
117230** between VDBE cursor numbers and bits of the bitmasks in WhereTerm.
117231**
117232** The VDBE cursor numbers are small integers contained in
117233** SrcList_item.iCursor and Expr.iTable fields.  For any given WHERE
117234** clause, the cursor numbers might not begin with 0 and they might
117235** contain gaps in the numbering sequence.  But we want to make maximum
117236** use of the bits in our bitmasks.  This structure provides a mapping
117237** from the sparse cursor numbers into consecutive integers beginning
117238** with 0.
117239**
117240** If WhereMaskSet.ix[A]==B it means that The A-th bit of a Bitmask
117241** corresponds VDBE cursor number B.  The A-th bit of a bitmask is 1<<A.
117242**
117243** For example, if the WHERE clause expression used these VDBE
117244** cursors:  4, 5, 8, 29, 57, 73.  Then the  WhereMaskSet structure
117245** would map those cursor numbers into bits 0 through 5.
117246**
117247** Note that the mapping is not necessarily ordered.  In the example
117248** above, the mapping might go like this:  4->3, 5->1, 8->2, 29->0,
117249** 57->5, 73->4.  Or one of 719 other combinations might be used. It
117250** does not really matter.  What is important is that sparse cursor
117251** numbers all get mapped into bit numbers that begin with 0 and contain
117252** no gaps.
117253*/
117254struct WhereMaskSet {
117255  int n;                        /* Number of assigned cursor values */
117256  int ix[BMS];                  /* Cursor assigned to each bit */
117257};
117258
117259/*
117260** Initialize a WhereMaskSet object
117261*/
117262#define initMaskSet(P)  (P)->n=0
117263
117264/*
117265** This object is a convenience wrapper holding all information needed
117266** to construct WhereLoop objects for a particular query.
117267*/
117268struct WhereLoopBuilder {
117269  WhereInfo *pWInfo;        /* Information about this WHERE */
117270  WhereClause *pWC;         /* WHERE clause terms */
117271  ExprList *pOrderBy;       /* ORDER BY clause */
117272  WhereLoop *pNew;          /* Template WhereLoop */
117273  WhereOrSet *pOrSet;       /* Record best loops here, if not NULL */
117274#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
117275  UnpackedRecord *pRec;     /* Probe for stat4 (if required) */
117276  int nRecValid;            /* Number of valid fields currently in pRec */
117277#endif
117278};
117279
117280/*
117281** The WHERE clause processing routine has two halves.  The
117282** first part does the start of the WHERE loop and the second
117283** half does the tail of the WHERE loop.  An instance of
117284** this structure is returned by the first half and passed
117285** into the second half to give some continuity.
117286**
117287** An instance of this object holds the complete state of the query
117288** planner.
117289*/
117290struct WhereInfo {
117291  Parse *pParse;            /* Parsing and code generating context */
117292  SrcList *pTabList;        /* List of tables in the join */
117293  ExprList *pOrderBy;       /* The ORDER BY clause or NULL */
117294  ExprList *pResultSet;     /* Result set. DISTINCT operates on these */
117295  WhereLoop *pLoops;        /* List of all WhereLoop objects */
117296  Bitmask revMask;          /* Mask of ORDER BY terms that need reversing */
117297  LogEst nRowOut;           /* Estimated number of output rows */
117298  u16 wctrlFlags;           /* Flags originally passed to sqlite3WhereBegin() */
117299  i8 nOBSat;                /* Number of ORDER BY terms satisfied by indices */
117300  u8 sorted;                /* True if really sorted (not just grouped) */
117301  u8 okOnePass;             /* Ok to use one-pass algorithm for UPDATE/DELETE */
117302  u8 untestedTerms;         /* Not all WHERE terms resolved by outer loop */
117303  u8 eDistinct;             /* One of the WHERE_DISTINCT_* values below */
117304  u8 nLevel;                /* Number of nested loop */
117305  int iTop;                 /* The very beginning of the WHERE loop */
117306  int iContinue;            /* Jump here to continue with next record */
117307  int iBreak;               /* Jump here to break out of the loop */
117308  int savedNQueryLoop;      /* pParse->nQueryLoop outside the WHERE loop */
117309  int aiCurOnePass[2];      /* OP_OpenWrite cursors for the ONEPASS opt */
117310  WhereMaskSet sMaskSet;    /* Map cursor numbers to bitmasks */
117311  WhereClause sWC;          /* Decomposition of the WHERE clause */
117312  WhereLevel a[1];          /* Information about each nest loop in WHERE */
117313};
117314
117315/*
117316** Private interfaces - callable only by other where.c routines.
117317**
117318** where.c:
117319*/
117320SQLITE_PRIVATE Bitmask sqlite3WhereGetMask(WhereMaskSet*,int);
117321SQLITE_PRIVATE WhereTerm *sqlite3WhereFindTerm(
117322  WhereClause *pWC,     /* The WHERE clause to be searched */
117323  int iCur,             /* Cursor number of LHS */
117324  int iColumn,          /* Column number of LHS */
117325  Bitmask notReady,     /* RHS must not overlap with this mask */
117326  u32 op,               /* Mask of WO_xx values describing operator */
117327  Index *pIdx           /* Must be compatible with this index, if not NULL */
117328);
117329
117330/* wherecode.c: */
117331#ifndef SQLITE_OMIT_EXPLAIN
117332SQLITE_PRIVATE int sqlite3WhereExplainOneScan(
117333  Parse *pParse,                  /* Parse context */
117334  SrcList *pTabList,              /* Table list this loop refers to */
117335  WhereLevel *pLevel,             /* Scan to write OP_Explain opcode for */
117336  int iLevel,                     /* Value for "level" column of output */
117337  int iFrom,                      /* Value for "from" column of output */
117338  u16 wctrlFlags                  /* Flags passed to sqlite3WhereBegin() */
117339);
117340#else
117341# define sqlite3WhereExplainOneScan(u,v,w,x,y,z) 0
117342#endif /* SQLITE_OMIT_EXPLAIN */
117343#ifdef SQLITE_ENABLE_STMT_SCANSTATUS
117344SQLITE_PRIVATE void sqlite3WhereAddScanStatus(
117345  Vdbe *v,                        /* Vdbe to add scanstatus entry to */
117346  SrcList *pSrclist,              /* FROM clause pLvl reads data from */
117347  WhereLevel *pLvl,               /* Level to add scanstatus() entry for */
117348  int addrExplain                 /* Address of OP_Explain (or 0) */
117349);
117350#else
117351# define sqlite3WhereAddScanStatus(a, b, c, d) ((void)d)
117352#endif
117353SQLITE_PRIVATE Bitmask sqlite3WhereCodeOneLoopStart(
117354  WhereInfo *pWInfo,   /* Complete information about the WHERE clause */
117355  int iLevel,          /* Which level of pWInfo->a[] should be coded */
117356  Bitmask notReady     /* Which tables are currently available */
117357);
117358
117359/* whereexpr.c: */
117360SQLITE_PRIVATE void sqlite3WhereClauseInit(WhereClause*,WhereInfo*);
117361SQLITE_PRIVATE void sqlite3WhereClauseClear(WhereClause*);
117362SQLITE_PRIVATE void sqlite3WhereSplit(WhereClause*,Expr*,u8);
117363SQLITE_PRIVATE Bitmask sqlite3WhereExprUsage(WhereMaskSet*, Expr*);
117364SQLITE_PRIVATE Bitmask sqlite3WhereExprListUsage(WhereMaskSet*, ExprList*);
117365SQLITE_PRIVATE void sqlite3WhereExprAnalyze(SrcList*, WhereClause*);
117366
117367
117368
117369
117370
117371/*
117372** Bitmasks for the operators on WhereTerm objects.  These are all
117373** operators that are of interest to the query planner.  An
117374** OR-ed combination of these values can be used when searching for
117375** particular WhereTerms within a WhereClause.
117376*/
117377#define WO_IN     0x0001
117378#define WO_EQ     0x0002
117379#define WO_LT     (WO_EQ<<(TK_LT-TK_EQ))
117380#define WO_LE     (WO_EQ<<(TK_LE-TK_EQ))
117381#define WO_GT     (WO_EQ<<(TK_GT-TK_EQ))
117382#define WO_GE     (WO_EQ<<(TK_GE-TK_EQ))
117383#define WO_MATCH  0x0040
117384#define WO_IS     0x0080
117385#define WO_ISNULL 0x0100
117386#define WO_OR     0x0200       /* Two or more OR-connected terms */
117387#define WO_AND    0x0400       /* Two or more AND-connected terms */
117388#define WO_EQUIV  0x0800       /* Of the form A==B, both columns */
117389#define WO_NOOP   0x1000       /* This term does not restrict search space */
117390
117391#define WO_ALL    0x1fff       /* Mask of all possible WO_* values */
117392#define WO_SINGLE 0x01ff       /* Mask of all non-compound WO_* values */
117393
117394/*
117395** These are definitions of bits in the WhereLoop.wsFlags field.
117396** The particular combination of bits in each WhereLoop help to
117397** determine the algorithm that WhereLoop represents.
117398*/
117399#define WHERE_COLUMN_EQ    0x00000001  /* x=EXPR */
117400#define WHERE_COLUMN_RANGE 0x00000002  /* x<EXPR and/or x>EXPR */
117401#define WHERE_COLUMN_IN    0x00000004  /* x IN (...) */
117402#define WHERE_COLUMN_NULL  0x00000008  /* x IS NULL */
117403#define WHERE_CONSTRAINT   0x0000000f  /* Any of the WHERE_COLUMN_xxx values */
117404#define WHERE_TOP_LIMIT    0x00000010  /* x<EXPR or x<=EXPR constraint */
117405#define WHERE_BTM_LIMIT    0x00000020  /* x>EXPR or x>=EXPR constraint */
117406#define WHERE_BOTH_LIMIT   0x00000030  /* Both x>EXPR and x<EXPR */
117407#define WHERE_IDX_ONLY     0x00000040  /* Use index only - omit table */
117408#define WHERE_IPK          0x00000100  /* x is the INTEGER PRIMARY KEY */
117409#define WHERE_INDEXED      0x00000200  /* WhereLoop.u.btree.pIndex is valid */
117410#define WHERE_VIRTUALTABLE 0x00000400  /* WhereLoop.u.vtab is valid */
117411#define WHERE_IN_ABLE      0x00000800  /* Able to support an IN operator */
117412#define WHERE_ONEROW       0x00001000  /* Selects no more than one row */
117413#define WHERE_MULTI_OR     0x00002000  /* OR using multiple indices */
117414#define WHERE_AUTO_INDEX   0x00004000  /* Uses an ephemeral index */
117415#define WHERE_SKIPSCAN     0x00008000  /* Uses the skip-scan algorithm */
117416#define WHERE_UNQ_WANTED   0x00010000  /* WHERE_ONEROW would have been helpful*/
117417#define WHERE_PARTIALIDX   0x00020000  /* The automatic index is partial */
117418
117419/************** End of whereInt.h ********************************************/
117420/************** Continuing where we left off in wherecode.c ******************/
117421
117422#ifndef SQLITE_OMIT_EXPLAIN
117423/*
117424** This routine is a helper for explainIndexRange() below
117425**
117426** pStr holds the text of an expression that we are building up one term
117427** at a time.  This routine adds a new term to the end of the expression.
117428** Terms are separated by AND so add the "AND" text for second and subsequent
117429** terms only.
117430*/
117431static void explainAppendTerm(
117432  StrAccum *pStr,             /* The text expression being built */
117433  int iTerm,                  /* Index of this term.  First is zero */
117434  const char *zColumn,        /* Name of the column */
117435  const char *zOp             /* Name of the operator */
117436){
117437  if( iTerm ) sqlite3StrAccumAppend(pStr, " AND ", 5);
117438  sqlite3StrAccumAppendAll(pStr, zColumn);
117439  sqlite3StrAccumAppend(pStr, zOp, 1);
117440  sqlite3StrAccumAppend(pStr, "?", 1);
117441}
117442
117443/*
117444** Argument pLevel describes a strategy for scanning table pTab. This
117445** function appends text to pStr that describes the subset of table
117446** rows scanned by the strategy in the form of an SQL expression.
117447**
117448** For example, if the query:
117449**
117450**   SELECT * FROM t1 WHERE a=1 AND b>2;
117451**
117452** is run and there is an index on (a, b), then this function returns a
117453** string similar to:
117454**
117455**   "a=? AND b>?"
117456*/
117457static void explainIndexRange(StrAccum *pStr, WhereLoop *pLoop, Table *pTab){
117458  Index *pIndex = pLoop->u.btree.pIndex;
117459  u16 nEq = pLoop->u.btree.nEq;
117460  u16 nSkip = pLoop->nSkip;
117461  int i, j;
117462  Column *aCol = pTab->aCol;
117463  i16 *aiColumn = pIndex->aiColumn;
117464
117465  if( nEq==0 && (pLoop->wsFlags&(WHERE_BTM_LIMIT|WHERE_TOP_LIMIT))==0 ) return;
117466  sqlite3StrAccumAppend(pStr, " (", 2);
117467  for(i=0; i<nEq; i++){
117468    char *z = aiColumn[i] < 0 ? "rowid" : aCol[aiColumn[i]].zName;
117469    if( i>=nSkip ){
117470      explainAppendTerm(pStr, i, z, "=");
117471    }else{
117472      if( i ) sqlite3StrAccumAppend(pStr, " AND ", 5);
117473      sqlite3XPrintf(pStr, 0, "ANY(%s)", z);
117474    }
117475  }
117476
117477  j = i;
117478  if( pLoop->wsFlags&WHERE_BTM_LIMIT ){
117479    char *z = aiColumn[j] < 0 ? "rowid" : aCol[aiColumn[j]].zName;
117480    explainAppendTerm(pStr, i++, z, ">");
117481  }
117482  if( pLoop->wsFlags&WHERE_TOP_LIMIT ){
117483    char *z = aiColumn[j] < 0 ? "rowid" : aCol[aiColumn[j]].zName;
117484    explainAppendTerm(pStr, i, z, "<");
117485  }
117486  sqlite3StrAccumAppend(pStr, ")", 1);
117487}
117488
117489/*
117490** This function is a no-op unless currently processing an EXPLAIN QUERY PLAN
117491** command, or if either SQLITE_DEBUG or SQLITE_ENABLE_STMT_SCANSTATUS was
117492** defined at compile-time. If it is not a no-op, a single OP_Explain opcode
117493** is added to the output to describe the table scan strategy in pLevel.
117494**
117495** If an OP_Explain opcode is added to the VM, its address is returned.
117496** Otherwise, if no OP_Explain is coded, zero is returned.
117497*/
117498SQLITE_PRIVATE int sqlite3WhereExplainOneScan(
117499  Parse *pParse,                  /* Parse context */
117500  SrcList *pTabList,              /* Table list this loop refers to */
117501  WhereLevel *pLevel,             /* Scan to write OP_Explain opcode for */
117502  int iLevel,                     /* Value for "level" column of output */
117503  int iFrom,                      /* Value for "from" column of output */
117504  u16 wctrlFlags                  /* Flags passed to sqlite3WhereBegin() */
117505){
117506  int ret = 0;
117507#if !defined(SQLITE_DEBUG) && !defined(SQLITE_ENABLE_STMT_SCANSTATUS)
117508  if( pParse->explain==2 )
117509#endif
117510  {
117511    struct SrcList_item *pItem = &pTabList->a[pLevel->iFrom];
117512    Vdbe *v = pParse->pVdbe;      /* VM being constructed */
117513    sqlite3 *db = pParse->db;     /* Database handle */
117514    int iId = pParse->iSelectId;  /* Select id (left-most output column) */
117515    int isSearch;                 /* True for a SEARCH. False for SCAN. */
117516    WhereLoop *pLoop;             /* The controlling WhereLoop object */
117517    u32 flags;                    /* Flags that describe this loop */
117518    char *zMsg;                   /* Text to add to EQP output */
117519    StrAccum str;                 /* EQP output string */
117520    char zBuf[100];               /* Initial space for EQP output string */
117521
117522    pLoop = pLevel->pWLoop;
117523    flags = pLoop->wsFlags;
117524    if( (flags&WHERE_MULTI_OR) || (wctrlFlags&WHERE_ONETABLE_ONLY) ) return 0;
117525
117526    isSearch = (flags&(WHERE_BTM_LIMIT|WHERE_TOP_LIMIT))!=0
117527            || ((flags&WHERE_VIRTUALTABLE)==0 && (pLoop->u.btree.nEq>0))
117528            || (wctrlFlags&(WHERE_ORDERBY_MIN|WHERE_ORDERBY_MAX));
117529
117530    sqlite3StrAccumInit(&str, db, zBuf, sizeof(zBuf), SQLITE_MAX_LENGTH);
117531    sqlite3StrAccumAppendAll(&str, isSearch ? "SEARCH" : "SCAN");
117532    if( pItem->pSelect ){
117533      sqlite3XPrintf(&str, 0, " SUBQUERY %d", pItem->iSelectId);
117534    }else{
117535      sqlite3XPrintf(&str, 0, " TABLE %s", pItem->zName);
117536    }
117537
117538    if( pItem->zAlias ){
117539      sqlite3XPrintf(&str, 0, " AS %s", pItem->zAlias);
117540    }
117541    if( (flags & (WHERE_IPK|WHERE_VIRTUALTABLE))==0 ){
117542      const char *zFmt = 0;
117543      Index *pIdx;
117544
117545      assert( pLoop->u.btree.pIndex!=0 );
117546      pIdx = pLoop->u.btree.pIndex;
117547      assert( !(flags&WHERE_AUTO_INDEX) || (flags&WHERE_IDX_ONLY) );
117548      if( !HasRowid(pItem->pTab) && IsPrimaryKeyIndex(pIdx) ){
117549        if( isSearch ){
117550          zFmt = "PRIMARY KEY";
117551        }
117552      }else if( flags & WHERE_PARTIALIDX ){
117553        zFmt = "AUTOMATIC PARTIAL COVERING INDEX";
117554      }else if( flags & WHERE_AUTO_INDEX ){
117555        zFmt = "AUTOMATIC COVERING INDEX";
117556      }else if( flags & WHERE_IDX_ONLY ){
117557        zFmt = "COVERING INDEX %s";
117558      }else{
117559        zFmt = "INDEX %s";
117560      }
117561      if( zFmt ){
117562        sqlite3StrAccumAppend(&str, " USING ", 7);
117563        sqlite3XPrintf(&str, 0, zFmt, pIdx->zName);
117564        explainIndexRange(&str, pLoop, pItem->pTab);
117565      }
117566    }else if( (flags & WHERE_IPK)!=0 && (flags & WHERE_CONSTRAINT)!=0 ){
117567      const char *zRange;
117568      if( flags&(WHERE_COLUMN_EQ|WHERE_COLUMN_IN) ){
117569        zRange = "(rowid=?)";
117570      }else if( (flags&WHERE_BOTH_LIMIT)==WHERE_BOTH_LIMIT ){
117571        zRange = "(rowid>? AND rowid<?)";
117572      }else if( flags&WHERE_BTM_LIMIT ){
117573        zRange = "(rowid>?)";
117574      }else{
117575        assert( flags&WHERE_TOP_LIMIT);
117576        zRange = "(rowid<?)";
117577      }
117578      sqlite3StrAccumAppendAll(&str, " USING INTEGER PRIMARY KEY ");
117579      sqlite3StrAccumAppendAll(&str, zRange);
117580    }
117581#ifndef SQLITE_OMIT_VIRTUALTABLE
117582    else if( (flags & WHERE_VIRTUALTABLE)!=0 ){
117583      sqlite3XPrintf(&str, 0, " VIRTUAL TABLE INDEX %d:%s",
117584                  pLoop->u.vtab.idxNum, pLoop->u.vtab.idxStr);
117585    }
117586#endif
117587#ifdef SQLITE_EXPLAIN_ESTIMATED_ROWS
117588    if( pLoop->nOut>=10 ){
117589      sqlite3XPrintf(&str, 0, " (~%llu rows)", sqlite3LogEstToInt(pLoop->nOut));
117590    }else{
117591      sqlite3StrAccumAppend(&str, " (~1 row)", 9);
117592    }
117593#endif
117594    zMsg = sqlite3StrAccumFinish(&str);
117595    ret = sqlite3VdbeAddOp4(v, OP_Explain, iId, iLevel, iFrom, zMsg,P4_DYNAMIC);
117596  }
117597  return ret;
117598}
117599#endif /* SQLITE_OMIT_EXPLAIN */
117600
117601#ifdef SQLITE_ENABLE_STMT_SCANSTATUS
117602/*
117603** Configure the VM passed as the first argument with an
117604** sqlite3_stmt_scanstatus() entry corresponding to the scan used to
117605** implement level pLvl. Argument pSrclist is a pointer to the FROM
117606** clause that the scan reads data from.
117607**
117608** If argument addrExplain is not 0, it must be the address of an
117609** OP_Explain instruction that describes the same loop.
117610*/
117611SQLITE_PRIVATE void sqlite3WhereAddScanStatus(
117612  Vdbe *v,                        /* Vdbe to add scanstatus entry to */
117613  SrcList *pSrclist,              /* FROM clause pLvl reads data from */
117614  WhereLevel *pLvl,               /* Level to add scanstatus() entry for */
117615  int addrExplain                 /* Address of OP_Explain (or 0) */
117616){
117617  const char *zObj = 0;
117618  WhereLoop *pLoop = pLvl->pWLoop;
117619  if( (pLoop->wsFlags & WHERE_VIRTUALTABLE)==0  &&  pLoop->u.btree.pIndex!=0 ){
117620    zObj = pLoop->u.btree.pIndex->zName;
117621  }else{
117622    zObj = pSrclist->a[pLvl->iFrom].zName;
117623  }
117624  sqlite3VdbeScanStatus(
117625      v, addrExplain, pLvl->addrBody, pLvl->addrVisit, pLoop->nOut, zObj
117626  );
117627}
117628#endif
117629
117630
117631/*
117632** Disable a term in the WHERE clause.  Except, do not disable the term
117633** if it controls a LEFT OUTER JOIN and it did not originate in the ON
117634** or USING clause of that join.
117635**
117636** Consider the term t2.z='ok' in the following queries:
117637**
117638**   (1)  SELECT * FROM t1 LEFT JOIN t2 ON t1.a=t2.x WHERE t2.z='ok'
117639**   (2)  SELECT * FROM t1 LEFT JOIN t2 ON t1.a=t2.x AND t2.z='ok'
117640**   (3)  SELECT * FROM t1, t2 WHERE t1.a=t2.x AND t2.z='ok'
117641**
117642** The t2.z='ok' is disabled in the in (2) because it originates
117643** in the ON clause.  The term is disabled in (3) because it is not part
117644** of a LEFT OUTER JOIN.  In (1), the term is not disabled.
117645**
117646** Disabling a term causes that term to not be tested in the inner loop
117647** of the join.  Disabling is an optimization.  When terms are satisfied
117648** by indices, we disable them to prevent redundant tests in the inner
117649** loop.  We would get the correct results if nothing were ever disabled,
117650** but joins might run a little slower.  The trick is to disable as much
117651** as we can without disabling too much.  If we disabled in (1), we'd get
117652** the wrong answer.  See ticket #813.
117653**
117654** If all the children of a term are disabled, then that term is also
117655** automatically disabled.  In this way, terms get disabled if derived
117656** virtual terms are tested first.  For example:
117657**
117658**      x GLOB 'abc*' AND x>='abc' AND x<'acd'
117659**      \___________/     \______/     \_____/
117660**         parent          child1       child2
117661**
117662** Only the parent term was in the original WHERE clause.  The child1
117663** and child2 terms were added by the LIKE optimization.  If both of
117664** the virtual child terms are valid, then testing of the parent can be
117665** skipped.
117666**
117667** Usually the parent term is marked as TERM_CODED.  But if the parent
117668** term was originally TERM_LIKE, then the parent gets TERM_LIKECOND instead.
117669** The TERM_LIKECOND marking indicates that the term should be coded inside
117670** a conditional such that is only evaluated on the second pass of a
117671** LIKE-optimization loop, when scanning BLOBs instead of strings.
117672*/
117673static void disableTerm(WhereLevel *pLevel, WhereTerm *pTerm){
117674  int nLoop = 0;
117675  while( pTerm
117676      && (pTerm->wtFlags & TERM_CODED)==0
117677      && (pLevel->iLeftJoin==0 || ExprHasProperty(pTerm->pExpr, EP_FromJoin))
117678      && (pLevel->notReady & pTerm->prereqAll)==0
117679  ){
117680    if( nLoop && (pTerm->wtFlags & TERM_LIKE)!=0 ){
117681      pTerm->wtFlags |= TERM_LIKECOND;
117682    }else{
117683      pTerm->wtFlags |= TERM_CODED;
117684    }
117685    if( pTerm->iParent<0 ) break;
117686    pTerm = &pTerm->pWC->a[pTerm->iParent];
117687    pTerm->nChild--;
117688    if( pTerm->nChild!=0 ) break;
117689    nLoop++;
117690  }
117691}
117692
117693/*
117694** Code an OP_Affinity opcode to apply the column affinity string zAff
117695** to the n registers starting at base.
117696**
117697** As an optimization, SQLITE_AFF_BLOB entries (which are no-ops) at the
117698** beginning and end of zAff are ignored.  If all entries in zAff are
117699** SQLITE_AFF_BLOB, then no code gets generated.
117700**
117701** This routine makes its own copy of zAff so that the caller is free
117702** to modify zAff after this routine returns.
117703*/
117704static void codeApplyAffinity(Parse *pParse, int base, int n, char *zAff){
117705  Vdbe *v = pParse->pVdbe;
117706  if( zAff==0 ){
117707    assert( pParse->db->mallocFailed );
117708    return;
117709  }
117710  assert( v!=0 );
117711
117712  /* Adjust base and n to skip over SQLITE_AFF_BLOB entries at the beginning
117713  ** and end of the affinity string.
117714  */
117715  while( n>0 && zAff[0]==SQLITE_AFF_BLOB ){
117716    n--;
117717    base++;
117718    zAff++;
117719  }
117720  while( n>1 && zAff[n-1]==SQLITE_AFF_BLOB ){
117721    n--;
117722  }
117723
117724  /* Code the OP_Affinity opcode if there is anything left to do. */
117725  if( n>0 ){
117726    sqlite3VdbeAddOp2(v, OP_Affinity, base, n);
117727    sqlite3VdbeChangeP4(v, -1, zAff, n);
117728    sqlite3ExprCacheAffinityChange(pParse, base, n);
117729  }
117730}
117731
117732
117733/*
117734** Generate code for a single equality term of the WHERE clause.  An equality
117735** term can be either X=expr or X IN (...).   pTerm is the term to be
117736** coded.
117737**
117738** The current value for the constraint is left in register iReg.
117739**
117740** For a constraint of the form X=expr, the expression is evaluated and its
117741** result is left on the stack.  For constraints of the form X IN (...)
117742** this routine sets up a loop that will iterate over all values of X.
117743*/
117744static int codeEqualityTerm(
117745  Parse *pParse,      /* The parsing context */
117746  WhereTerm *pTerm,   /* The term of the WHERE clause to be coded */
117747  WhereLevel *pLevel, /* The level of the FROM clause we are working on */
117748  int iEq,            /* Index of the equality term within this level */
117749  int bRev,           /* True for reverse-order IN operations */
117750  int iTarget         /* Attempt to leave results in this register */
117751){
117752  Expr *pX = pTerm->pExpr;
117753  Vdbe *v = pParse->pVdbe;
117754  int iReg;                  /* Register holding results */
117755
117756  assert( iTarget>0 );
117757  if( pX->op==TK_EQ || pX->op==TK_IS ){
117758    iReg = sqlite3ExprCodeTarget(pParse, pX->pRight, iTarget);
117759  }else if( pX->op==TK_ISNULL ){
117760    iReg = iTarget;
117761    sqlite3VdbeAddOp2(v, OP_Null, 0, iReg);
117762#ifndef SQLITE_OMIT_SUBQUERY
117763  }else{
117764    int eType;
117765    int iTab;
117766    struct InLoop *pIn;
117767    WhereLoop *pLoop = pLevel->pWLoop;
117768
117769    if( (pLoop->wsFlags & WHERE_VIRTUALTABLE)==0
117770      && pLoop->u.btree.pIndex!=0
117771      && pLoop->u.btree.pIndex->aSortOrder[iEq]
117772    ){
117773      testcase( iEq==0 );
117774      testcase( bRev );
117775      bRev = !bRev;
117776    }
117777    assert( pX->op==TK_IN );
117778    iReg = iTarget;
117779    eType = sqlite3FindInIndex(pParse, pX, IN_INDEX_LOOP, 0);
117780    if( eType==IN_INDEX_INDEX_DESC ){
117781      testcase( bRev );
117782      bRev = !bRev;
117783    }
117784    iTab = pX->iTable;
117785    sqlite3VdbeAddOp2(v, bRev ? OP_Last : OP_Rewind, iTab, 0);
117786    VdbeCoverageIf(v, bRev);
117787    VdbeCoverageIf(v, !bRev);
117788    assert( (pLoop->wsFlags & WHERE_MULTI_OR)==0 );
117789    pLoop->wsFlags |= WHERE_IN_ABLE;
117790    if( pLevel->u.in.nIn==0 ){
117791      pLevel->addrNxt = sqlite3VdbeMakeLabel(v);
117792    }
117793    pLevel->u.in.nIn++;
117794    pLevel->u.in.aInLoop =
117795       sqlite3DbReallocOrFree(pParse->db, pLevel->u.in.aInLoop,
117796                              sizeof(pLevel->u.in.aInLoop[0])*pLevel->u.in.nIn);
117797    pIn = pLevel->u.in.aInLoop;
117798    if( pIn ){
117799      pIn += pLevel->u.in.nIn - 1;
117800      pIn->iCur = iTab;
117801      if( eType==IN_INDEX_ROWID ){
117802        pIn->addrInTop = sqlite3VdbeAddOp2(v, OP_Rowid, iTab, iReg);
117803      }else{
117804        pIn->addrInTop = sqlite3VdbeAddOp3(v, OP_Column, iTab, 0, iReg);
117805      }
117806      pIn->eEndLoopOp = bRev ? OP_PrevIfOpen : OP_NextIfOpen;
117807      sqlite3VdbeAddOp1(v, OP_IsNull, iReg); VdbeCoverage(v);
117808    }else{
117809      pLevel->u.in.nIn = 0;
117810    }
117811#endif
117812  }
117813  disableTerm(pLevel, pTerm);
117814  return iReg;
117815}
117816
117817/*
117818** Generate code that will evaluate all == and IN constraints for an
117819** index scan.
117820**
117821** For example, consider table t1(a,b,c,d,e,f) with index i1(a,b,c).
117822** Suppose the WHERE clause is this:  a==5 AND b IN (1,2,3) AND c>5 AND c<10
117823** The index has as many as three equality constraints, but in this
117824** example, the third "c" value is an inequality.  So only two
117825** constraints are coded.  This routine will generate code to evaluate
117826** a==5 and b IN (1,2,3).  The current values for a and b will be stored
117827** in consecutive registers and the index of the first register is returned.
117828**
117829** In the example above nEq==2.  But this subroutine works for any value
117830** of nEq including 0.  If nEq==0, this routine is nearly a no-op.
117831** The only thing it does is allocate the pLevel->iMem memory cell and
117832** compute the affinity string.
117833**
117834** The nExtraReg parameter is 0 or 1.  It is 0 if all WHERE clause constraints
117835** are == or IN and are covered by the nEq.  nExtraReg is 1 if there is
117836** an inequality constraint (such as the "c>=5 AND c<10" in the example) that
117837** occurs after the nEq quality constraints.
117838**
117839** This routine allocates a range of nEq+nExtraReg memory cells and returns
117840** the index of the first memory cell in that range. The code that
117841** calls this routine will use that memory range to store keys for
117842** start and termination conditions of the loop.
117843** key value of the loop.  If one or more IN operators appear, then
117844** this routine allocates an additional nEq memory cells for internal
117845** use.
117846**
117847** Before returning, *pzAff is set to point to a buffer containing a
117848** copy of the column affinity string of the index allocated using
117849** sqlite3DbMalloc(). Except, entries in the copy of the string associated
117850** with equality constraints that use BLOB or NONE affinity are set to
117851** SQLITE_AFF_BLOB. This is to deal with SQL such as the following:
117852**
117853**   CREATE TABLE t1(a TEXT PRIMARY KEY, b);
117854**   SELECT ... FROM t1 AS t2, t1 WHERE t1.a = t2.b;
117855**
117856** In the example above, the index on t1(a) has TEXT affinity. But since
117857** the right hand side of the equality constraint (t2.b) has BLOB/NONE affinity,
117858** no conversion should be attempted before using a t2.b value as part of
117859** a key to search the index. Hence the first byte in the returned affinity
117860** string in this example would be set to SQLITE_AFF_BLOB.
117861*/
117862static int codeAllEqualityTerms(
117863  Parse *pParse,        /* Parsing context */
117864  WhereLevel *pLevel,   /* Which nested loop of the FROM we are coding */
117865  int bRev,             /* Reverse the order of IN operators */
117866  int nExtraReg,        /* Number of extra registers to allocate */
117867  char **pzAff          /* OUT: Set to point to affinity string */
117868){
117869  u16 nEq;                      /* The number of == or IN constraints to code */
117870  u16 nSkip;                    /* Number of left-most columns to skip */
117871  Vdbe *v = pParse->pVdbe;      /* The vm under construction */
117872  Index *pIdx;                  /* The index being used for this loop */
117873  WhereTerm *pTerm;             /* A single constraint term */
117874  WhereLoop *pLoop;             /* The WhereLoop object */
117875  int j;                        /* Loop counter */
117876  int regBase;                  /* Base register */
117877  int nReg;                     /* Number of registers to allocate */
117878  char *zAff;                   /* Affinity string to return */
117879
117880  /* This module is only called on query plans that use an index. */
117881  pLoop = pLevel->pWLoop;
117882  assert( (pLoop->wsFlags & WHERE_VIRTUALTABLE)==0 );
117883  nEq = pLoop->u.btree.nEq;
117884  nSkip = pLoop->nSkip;
117885  pIdx = pLoop->u.btree.pIndex;
117886  assert( pIdx!=0 );
117887
117888  /* Figure out how many memory cells we will need then allocate them.
117889  */
117890  regBase = pParse->nMem + 1;
117891  nReg = pLoop->u.btree.nEq + nExtraReg;
117892  pParse->nMem += nReg;
117893
117894  zAff = sqlite3DbStrDup(pParse->db, sqlite3IndexAffinityStr(v, pIdx));
117895  if( !zAff ){
117896    pParse->db->mallocFailed = 1;
117897  }
117898
117899  if( nSkip ){
117900    int iIdxCur = pLevel->iIdxCur;
117901    sqlite3VdbeAddOp1(v, (bRev?OP_Last:OP_Rewind), iIdxCur);
117902    VdbeCoverageIf(v, bRev==0);
117903    VdbeCoverageIf(v, bRev!=0);
117904    VdbeComment((v, "begin skip-scan on %s", pIdx->zName));
117905    j = sqlite3VdbeAddOp0(v, OP_Goto);
117906    pLevel->addrSkip = sqlite3VdbeAddOp4Int(v, (bRev?OP_SeekLT:OP_SeekGT),
117907                            iIdxCur, 0, regBase, nSkip);
117908    VdbeCoverageIf(v, bRev==0);
117909    VdbeCoverageIf(v, bRev!=0);
117910    sqlite3VdbeJumpHere(v, j);
117911    for(j=0; j<nSkip; j++){
117912      sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, j, regBase+j);
117913      assert( pIdx->aiColumn[j]>=0 );
117914      VdbeComment((v, "%s", pIdx->pTable->aCol[pIdx->aiColumn[j]].zName));
117915    }
117916  }
117917
117918  /* Evaluate the equality constraints
117919  */
117920  assert( zAff==0 || (int)strlen(zAff)>=nEq );
117921  for(j=nSkip; j<nEq; j++){
117922    int r1;
117923    pTerm = pLoop->aLTerm[j];
117924    assert( pTerm!=0 );
117925    /* The following testcase is true for indices with redundant columns.
117926    ** Ex: CREATE INDEX i1 ON t1(a,b,a); SELECT * FROM t1 WHERE a=0 AND b=0; */
117927    testcase( (pTerm->wtFlags & TERM_CODED)!=0 );
117928    testcase( pTerm->wtFlags & TERM_VIRTUAL );
117929    r1 = codeEqualityTerm(pParse, pTerm, pLevel, j, bRev, regBase+j);
117930    if( r1!=regBase+j ){
117931      if( nReg==1 ){
117932        sqlite3ReleaseTempReg(pParse, regBase);
117933        regBase = r1;
117934      }else{
117935        sqlite3VdbeAddOp2(v, OP_SCopy, r1, regBase+j);
117936      }
117937    }
117938    testcase( pTerm->eOperator & WO_ISNULL );
117939    testcase( pTerm->eOperator & WO_IN );
117940    if( (pTerm->eOperator & (WO_ISNULL|WO_IN))==0 ){
117941      Expr *pRight = pTerm->pExpr->pRight;
117942      if( (pTerm->wtFlags & TERM_IS)==0 && sqlite3ExprCanBeNull(pRight) ){
117943        sqlite3VdbeAddOp2(v, OP_IsNull, regBase+j, pLevel->addrBrk);
117944        VdbeCoverage(v);
117945      }
117946      if( zAff ){
117947        if( sqlite3CompareAffinity(pRight, zAff[j])==SQLITE_AFF_BLOB ){
117948          zAff[j] = SQLITE_AFF_BLOB;
117949        }
117950        if( sqlite3ExprNeedsNoAffinityChange(pRight, zAff[j]) ){
117951          zAff[j] = SQLITE_AFF_BLOB;
117952        }
117953      }
117954    }
117955  }
117956  *pzAff = zAff;
117957  return regBase;
117958}
117959
117960/*
117961** If the most recently coded instruction is a constant range contraint
117962** that originated from the LIKE optimization, then change the P3 to be
117963** pLoop->iLikeRepCntr and set P5.
117964**
117965** The LIKE optimization trys to evaluate "x LIKE 'abc%'" as a range
117966** expression: "x>='ABC' AND x<'abd'".  But this requires that the range
117967** scan loop run twice, once for strings and a second time for BLOBs.
117968** The OP_String opcodes on the second pass convert the upper and lower
117969** bound string contants to blobs.  This routine makes the necessary changes
117970** to the OP_String opcodes for that to happen.
117971*/
117972static void whereLikeOptimizationStringFixup(
117973  Vdbe *v,                /* prepared statement under construction */
117974  WhereLevel *pLevel,     /* The loop that contains the LIKE operator */
117975  WhereTerm *pTerm        /* The upper or lower bound just coded */
117976){
117977  if( pTerm->wtFlags & TERM_LIKEOPT ){
117978    VdbeOp *pOp;
117979    assert( pLevel->iLikeRepCntr>0 );
117980    pOp = sqlite3VdbeGetOp(v, -1);
117981    assert( pOp!=0 );
117982    assert( pOp->opcode==OP_String8
117983            || pTerm->pWC->pWInfo->pParse->db->mallocFailed );
117984    pOp->p3 = pLevel->iLikeRepCntr;
117985    pOp->p5 = 1;
117986  }
117987}
117988
117989
117990/*
117991** Generate code for the start of the iLevel-th loop in the WHERE clause
117992** implementation described by pWInfo.
117993*/
117994SQLITE_PRIVATE Bitmask sqlite3WhereCodeOneLoopStart(
117995  WhereInfo *pWInfo,   /* Complete information about the WHERE clause */
117996  int iLevel,          /* Which level of pWInfo->a[] should be coded */
117997  Bitmask notReady     /* Which tables are currently available */
117998){
117999  int j, k;            /* Loop counters */
118000  int iCur;            /* The VDBE cursor for the table */
118001  int addrNxt;         /* Where to jump to continue with the next IN case */
118002  int omitTable;       /* True if we use the index only */
118003  int bRev;            /* True if we need to scan in reverse order */
118004  WhereLevel *pLevel;  /* The where level to be coded */
118005  WhereLoop *pLoop;    /* The WhereLoop object being coded */
118006  WhereClause *pWC;    /* Decomposition of the entire WHERE clause */
118007  WhereTerm *pTerm;               /* A WHERE clause term */
118008  Parse *pParse;                  /* Parsing context */
118009  sqlite3 *db;                    /* Database connection */
118010  Vdbe *v;                        /* The prepared stmt under constructions */
118011  struct SrcList_item *pTabItem;  /* FROM clause term being coded */
118012  int addrBrk;                    /* Jump here to break out of the loop */
118013  int addrCont;                   /* Jump here to continue with next cycle */
118014  int iRowidReg = 0;        /* Rowid is stored in this register, if not zero */
118015  int iReleaseReg = 0;      /* Temp register to free before returning */
118016
118017  pParse = pWInfo->pParse;
118018  v = pParse->pVdbe;
118019  pWC = &pWInfo->sWC;
118020  db = pParse->db;
118021  pLevel = &pWInfo->a[iLevel];
118022  pLoop = pLevel->pWLoop;
118023  pTabItem = &pWInfo->pTabList->a[pLevel->iFrom];
118024  iCur = pTabItem->iCursor;
118025  pLevel->notReady = notReady & ~sqlite3WhereGetMask(&pWInfo->sMaskSet, iCur);
118026  bRev = (pWInfo->revMask>>iLevel)&1;
118027  omitTable = (pLoop->wsFlags & WHERE_IDX_ONLY)!=0
118028           && (pWInfo->wctrlFlags & WHERE_FORCE_TABLE)==0;
118029  VdbeModuleComment((v, "Begin WHERE-loop%d: %s",iLevel,pTabItem->pTab->zName));
118030
118031  /* Create labels for the "break" and "continue" instructions
118032  ** for the current loop.  Jump to addrBrk to break out of a loop.
118033  ** Jump to cont to go immediately to the next iteration of the
118034  ** loop.
118035  **
118036  ** When there is an IN operator, we also have a "addrNxt" label that
118037  ** means to continue with the next IN value combination.  When
118038  ** there are no IN operators in the constraints, the "addrNxt" label
118039  ** is the same as "addrBrk".
118040  */
118041  addrBrk = pLevel->addrBrk = pLevel->addrNxt = sqlite3VdbeMakeLabel(v);
118042  addrCont = pLevel->addrCont = sqlite3VdbeMakeLabel(v);
118043
118044  /* If this is the right table of a LEFT OUTER JOIN, allocate and
118045  ** initialize a memory cell that records if this table matches any
118046  ** row of the left table of the join.
118047  */
118048  if( pLevel->iFrom>0 && (pTabItem[0].jointype & JT_LEFT)!=0 ){
118049    pLevel->iLeftJoin = ++pParse->nMem;
118050    sqlite3VdbeAddOp2(v, OP_Integer, 0, pLevel->iLeftJoin);
118051    VdbeComment((v, "init LEFT JOIN no-match flag"));
118052  }
118053
118054  /* Special case of a FROM clause subquery implemented as a co-routine */
118055  if( pTabItem->viaCoroutine ){
118056    int regYield = pTabItem->regReturn;
118057    sqlite3VdbeAddOp3(v, OP_InitCoroutine, regYield, 0, pTabItem->addrFillSub);
118058    pLevel->p2 =  sqlite3VdbeAddOp2(v, OP_Yield, regYield, addrBrk);
118059    VdbeCoverage(v);
118060    VdbeComment((v, "next row of \"%s\"", pTabItem->pTab->zName));
118061    pLevel->op = OP_Goto;
118062  }else
118063
118064#ifndef SQLITE_OMIT_VIRTUALTABLE
118065  if(  (pLoop->wsFlags & WHERE_VIRTUALTABLE)!=0 ){
118066    /* Case 1:  The table is a virtual-table.  Use the VFilter and VNext
118067    **          to access the data.
118068    */
118069    int iReg;   /* P3 Value for OP_VFilter */
118070    int addrNotFound;
118071    int nConstraint = pLoop->nLTerm;
118072
118073    sqlite3ExprCachePush(pParse);
118074    iReg = sqlite3GetTempRange(pParse, nConstraint+2);
118075    addrNotFound = pLevel->addrBrk;
118076    for(j=0; j<nConstraint; j++){
118077      int iTarget = iReg+j+2;
118078      pTerm = pLoop->aLTerm[j];
118079      if( pTerm==0 ) continue;
118080      if( pTerm->eOperator & WO_IN ){
118081        codeEqualityTerm(pParse, pTerm, pLevel, j, bRev, iTarget);
118082        addrNotFound = pLevel->addrNxt;
118083      }else{
118084        sqlite3ExprCode(pParse, pTerm->pExpr->pRight, iTarget);
118085      }
118086    }
118087    sqlite3VdbeAddOp2(v, OP_Integer, pLoop->u.vtab.idxNum, iReg);
118088    sqlite3VdbeAddOp2(v, OP_Integer, nConstraint, iReg+1);
118089    sqlite3VdbeAddOp4(v, OP_VFilter, iCur, addrNotFound, iReg,
118090                      pLoop->u.vtab.idxStr,
118091                      pLoop->u.vtab.needFree ? P4_MPRINTF : P4_STATIC);
118092    VdbeCoverage(v);
118093    pLoop->u.vtab.needFree = 0;
118094    for(j=0; j<nConstraint && j<16; j++){
118095      if( (pLoop->u.vtab.omitMask>>j)&1 ){
118096        disableTerm(pLevel, pLoop->aLTerm[j]);
118097      }
118098    }
118099    pLevel->op = OP_VNext;
118100    pLevel->p1 = iCur;
118101    pLevel->p2 = sqlite3VdbeCurrentAddr(v);
118102    sqlite3ReleaseTempRange(pParse, iReg, nConstraint+2);
118103    sqlite3ExprCachePop(pParse);
118104  }else
118105#endif /* SQLITE_OMIT_VIRTUALTABLE */
118106
118107  if( (pLoop->wsFlags & WHERE_IPK)!=0
118108   && (pLoop->wsFlags & (WHERE_COLUMN_IN|WHERE_COLUMN_EQ))!=0
118109  ){
118110    /* Case 2:  We can directly reference a single row using an
118111    **          equality comparison against the ROWID field.  Or
118112    **          we reference multiple rows using a "rowid IN (...)"
118113    **          construct.
118114    */
118115    assert( pLoop->u.btree.nEq==1 );
118116    pTerm = pLoop->aLTerm[0];
118117    assert( pTerm!=0 );
118118    assert( pTerm->pExpr!=0 );
118119    assert( omitTable==0 );
118120    testcase( pTerm->wtFlags & TERM_VIRTUAL );
118121    iReleaseReg = ++pParse->nMem;
118122    iRowidReg = codeEqualityTerm(pParse, pTerm, pLevel, 0, bRev, iReleaseReg);
118123    if( iRowidReg!=iReleaseReg ) sqlite3ReleaseTempReg(pParse, iReleaseReg);
118124    addrNxt = pLevel->addrNxt;
118125    sqlite3VdbeAddOp2(v, OP_MustBeInt, iRowidReg, addrNxt); VdbeCoverage(v);
118126    sqlite3VdbeAddOp3(v, OP_NotExists, iCur, addrNxt, iRowidReg);
118127    VdbeCoverage(v);
118128    sqlite3ExprCacheAffinityChange(pParse, iRowidReg, 1);
118129    sqlite3ExprCacheStore(pParse, iCur, -1, iRowidReg);
118130    VdbeComment((v, "pk"));
118131    pLevel->op = OP_Noop;
118132  }else if( (pLoop->wsFlags & WHERE_IPK)!=0
118133         && (pLoop->wsFlags & WHERE_COLUMN_RANGE)!=0
118134  ){
118135    /* Case 3:  We have an inequality comparison against the ROWID field.
118136    */
118137    int testOp = OP_Noop;
118138    int start;
118139    int memEndValue = 0;
118140    WhereTerm *pStart, *pEnd;
118141
118142    assert( omitTable==0 );
118143    j = 0;
118144    pStart = pEnd = 0;
118145    if( pLoop->wsFlags & WHERE_BTM_LIMIT ) pStart = pLoop->aLTerm[j++];
118146    if( pLoop->wsFlags & WHERE_TOP_LIMIT ) pEnd = pLoop->aLTerm[j++];
118147    assert( pStart!=0 || pEnd!=0 );
118148    if( bRev ){
118149      pTerm = pStart;
118150      pStart = pEnd;
118151      pEnd = pTerm;
118152    }
118153    if( pStart ){
118154      Expr *pX;             /* The expression that defines the start bound */
118155      int r1, rTemp;        /* Registers for holding the start boundary */
118156
118157      /* The following constant maps TK_xx codes into corresponding
118158      ** seek opcodes.  It depends on a particular ordering of TK_xx
118159      */
118160      const u8 aMoveOp[] = {
118161           /* TK_GT */  OP_SeekGT,
118162           /* TK_LE */  OP_SeekLE,
118163           /* TK_LT */  OP_SeekLT,
118164           /* TK_GE */  OP_SeekGE
118165      };
118166      assert( TK_LE==TK_GT+1 );      /* Make sure the ordering.. */
118167      assert( TK_LT==TK_GT+2 );      /*  ... of the TK_xx values... */
118168      assert( TK_GE==TK_GT+3 );      /*  ... is correcct. */
118169
118170      assert( (pStart->wtFlags & TERM_VNULL)==0 );
118171      testcase( pStart->wtFlags & TERM_VIRTUAL );
118172      pX = pStart->pExpr;
118173      assert( pX!=0 );
118174      testcase( pStart->leftCursor!=iCur ); /* transitive constraints */
118175      r1 = sqlite3ExprCodeTemp(pParse, pX->pRight, &rTemp);
118176      sqlite3VdbeAddOp3(v, aMoveOp[pX->op-TK_GT], iCur, addrBrk, r1);
118177      VdbeComment((v, "pk"));
118178      VdbeCoverageIf(v, pX->op==TK_GT);
118179      VdbeCoverageIf(v, pX->op==TK_LE);
118180      VdbeCoverageIf(v, pX->op==TK_LT);
118181      VdbeCoverageIf(v, pX->op==TK_GE);
118182      sqlite3ExprCacheAffinityChange(pParse, r1, 1);
118183      sqlite3ReleaseTempReg(pParse, rTemp);
118184      disableTerm(pLevel, pStart);
118185    }else{
118186      sqlite3VdbeAddOp2(v, bRev ? OP_Last : OP_Rewind, iCur, addrBrk);
118187      VdbeCoverageIf(v, bRev==0);
118188      VdbeCoverageIf(v, bRev!=0);
118189    }
118190    if( pEnd ){
118191      Expr *pX;
118192      pX = pEnd->pExpr;
118193      assert( pX!=0 );
118194      assert( (pEnd->wtFlags & TERM_VNULL)==0 );
118195      testcase( pEnd->leftCursor!=iCur ); /* Transitive constraints */
118196      testcase( pEnd->wtFlags & TERM_VIRTUAL );
118197      memEndValue = ++pParse->nMem;
118198      sqlite3ExprCode(pParse, pX->pRight, memEndValue);
118199      if( pX->op==TK_LT || pX->op==TK_GT ){
118200        testOp = bRev ? OP_Le : OP_Ge;
118201      }else{
118202        testOp = bRev ? OP_Lt : OP_Gt;
118203      }
118204      disableTerm(pLevel, pEnd);
118205    }
118206    start = sqlite3VdbeCurrentAddr(v);
118207    pLevel->op = bRev ? OP_Prev : OP_Next;
118208    pLevel->p1 = iCur;
118209    pLevel->p2 = start;
118210    assert( pLevel->p5==0 );
118211    if( testOp!=OP_Noop ){
118212      iRowidReg = ++pParse->nMem;
118213      sqlite3VdbeAddOp2(v, OP_Rowid, iCur, iRowidReg);
118214      sqlite3ExprCacheStore(pParse, iCur, -1, iRowidReg);
118215      sqlite3VdbeAddOp3(v, testOp, memEndValue, addrBrk, iRowidReg);
118216      VdbeCoverageIf(v, testOp==OP_Le);
118217      VdbeCoverageIf(v, testOp==OP_Lt);
118218      VdbeCoverageIf(v, testOp==OP_Ge);
118219      VdbeCoverageIf(v, testOp==OP_Gt);
118220      sqlite3VdbeChangeP5(v, SQLITE_AFF_NUMERIC | SQLITE_JUMPIFNULL);
118221    }
118222  }else if( pLoop->wsFlags & WHERE_INDEXED ){
118223    /* Case 4: A scan using an index.
118224    **
118225    **         The WHERE clause may contain zero or more equality
118226    **         terms ("==" or "IN" operators) that refer to the N
118227    **         left-most columns of the index. It may also contain
118228    **         inequality constraints (>, <, >= or <=) on the indexed
118229    **         column that immediately follows the N equalities. Only
118230    **         the right-most column can be an inequality - the rest must
118231    **         use the "==" and "IN" operators. For example, if the
118232    **         index is on (x,y,z), then the following clauses are all
118233    **         optimized:
118234    **
118235    **            x=5
118236    **            x=5 AND y=10
118237    **            x=5 AND y<10
118238    **            x=5 AND y>5 AND y<10
118239    **            x=5 AND y=5 AND z<=10
118240    **
118241    **         The z<10 term of the following cannot be used, only
118242    **         the x=5 term:
118243    **
118244    **            x=5 AND z<10
118245    **
118246    **         N may be zero if there are inequality constraints.
118247    **         If there are no inequality constraints, then N is at
118248    **         least one.
118249    **
118250    **         This case is also used when there are no WHERE clause
118251    **         constraints but an index is selected anyway, in order
118252    **         to force the output order to conform to an ORDER BY.
118253    */
118254    static const u8 aStartOp[] = {
118255      0,
118256      0,
118257      OP_Rewind,           /* 2: (!start_constraints && startEq &&  !bRev) */
118258      OP_Last,             /* 3: (!start_constraints && startEq &&   bRev) */
118259      OP_SeekGT,           /* 4: (start_constraints  && !startEq && !bRev) */
118260      OP_SeekLT,           /* 5: (start_constraints  && !startEq &&  bRev) */
118261      OP_SeekGE,           /* 6: (start_constraints  &&  startEq && !bRev) */
118262      OP_SeekLE            /* 7: (start_constraints  &&  startEq &&  bRev) */
118263    };
118264    static const u8 aEndOp[] = {
118265      OP_IdxGE,            /* 0: (end_constraints && !bRev && !endEq) */
118266      OP_IdxGT,            /* 1: (end_constraints && !bRev &&  endEq) */
118267      OP_IdxLE,            /* 2: (end_constraints &&  bRev && !endEq) */
118268      OP_IdxLT,            /* 3: (end_constraints &&  bRev &&  endEq) */
118269    };
118270    u16 nEq = pLoop->u.btree.nEq;     /* Number of == or IN terms */
118271    int regBase;                 /* Base register holding constraint values */
118272    WhereTerm *pRangeStart = 0;  /* Inequality constraint at range start */
118273    WhereTerm *pRangeEnd = 0;    /* Inequality constraint at range end */
118274    int startEq;                 /* True if range start uses ==, >= or <= */
118275    int endEq;                   /* True if range end uses ==, >= or <= */
118276    int start_constraints;       /* Start of range is constrained */
118277    int nConstraint;             /* Number of constraint terms */
118278    Index *pIdx;                 /* The index we will be using */
118279    int iIdxCur;                 /* The VDBE cursor for the index */
118280    int nExtraReg = 0;           /* Number of extra registers needed */
118281    int op;                      /* Instruction opcode */
118282    char *zStartAff;             /* Affinity for start of range constraint */
118283    char cEndAff = 0;            /* Affinity for end of range constraint */
118284    u8 bSeekPastNull = 0;        /* True to seek past initial nulls */
118285    u8 bStopAtNull = 0;          /* Add condition to terminate at NULLs */
118286
118287    pIdx = pLoop->u.btree.pIndex;
118288    iIdxCur = pLevel->iIdxCur;
118289    assert( nEq>=pLoop->nSkip );
118290
118291    /* If this loop satisfies a sort order (pOrderBy) request that
118292    ** was passed to this function to implement a "SELECT min(x) ..."
118293    ** query, then the caller will only allow the loop to run for
118294    ** a single iteration. This means that the first row returned
118295    ** should not have a NULL value stored in 'x'. If column 'x' is
118296    ** the first one after the nEq equality constraints in the index,
118297    ** this requires some special handling.
118298    */
118299    assert( pWInfo->pOrderBy==0
118300         || pWInfo->pOrderBy->nExpr==1
118301         || (pWInfo->wctrlFlags&WHERE_ORDERBY_MIN)==0 );
118302    if( (pWInfo->wctrlFlags&WHERE_ORDERBY_MIN)!=0
118303     && pWInfo->nOBSat>0
118304     && (pIdx->nKeyCol>nEq)
118305    ){
118306      assert( pLoop->nSkip==0 );
118307      bSeekPastNull = 1;
118308      nExtraReg = 1;
118309    }
118310
118311    /* Find any inequality constraint terms for the start and end
118312    ** of the range.
118313    */
118314    j = nEq;
118315    if( pLoop->wsFlags & WHERE_BTM_LIMIT ){
118316      pRangeStart = pLoop->aLTerm[j++];
118317      nExtraReg = 1;
118318      /* Like optimization range constraints always occur in pairs */
118319      assert( (pRangeStart->wtFlags & TERM_LIKEOPT)==0 ||
118320              (pLoop->wsFlags & WHERE_TOP_LIMIT)!=0 );
118321    }
118322    if( pLoop->wsFlags & WHERE_TOP_LIMIT ){
118323      pRangeEnd = pLoop->aLTerm[j++];
118324      nExtraReg = 1;
118325      if( (pRangeEnd->wtFlags & TERM_LIKEOPT)!=0 ){
118326        assert( pRangeStart!=0 );                     /* LIKE opt constraints */
118327        assert( pRangeStart->wtFlags & TERM_LIKEOPT );   /* occur in pairs */
118328        pLevel->iLikeRepCntr = ++pParse->nMem;
118329        testcase( bRev );
118330        testcase( pIdx->aSortOrder[nEq]==SQLITE_SO_DESC );
118331        sqlite3VdbeAddOp2(v, OP_Integer,
118332                          bRev ^ (pIdx->aSortOrder[nEq]==SQLITE_SO_DESC),
118333                          pLevel->iLikeRepCntr);
118334        VdbeComment((v, "LIKE loop counter"));
118335        pLevel->addrLikeRep = sqlite3VdbeCurrentAddr(v);
118336      }
118337      if( pRangeStart==0
118338       && (j = pIdx->aiColumn[nEq])>=0
118339       && pIdx->pTable->aCol[j].notNull==0
118340      ){
118341        bSeekPastNull = 1;
118342      }
118343    }
118344    assert( pRangeEnd==0 || (pRangeEnd->wtFlags & TERM_VNULL)==0 );
118345
118346    /* Generate code to evaluate all constraint terms using == or IN
118347    ** and store the values of those terms in an array of registers
118348    ** starting at regBase.
118349    */
118350    regBase = codeAllEqualityTerms(pParse,pLevel,bRev,nExtraReg,&zStartAff);
118351    assert( zStartAff==0 || sqlite3Strlen30(zStartAff)>=nEq );
118352    if( zStartAff ) cEndAff = zStartAff[nEq];
118353    addrNxt = pLevel->addrNxt;
118354
118355    /* If we are doing a reverse order scan on an ascending index, or
118356    ** a forward order scan on a descending index, interchange the
118357    ** start and end terms (pRangeStart and pRangeEnd).
118358    */
118359    if( (nEq<pIdx->nKeyCol && bRev==(pIdx->aSortOrder[nEq]==SQLITE_SO_ASC))
118360     || (bRev && pIdx->nKeyCol==nEq)
118361    ){
118362      SWAP(WhereTerm *, pRangeEnd, pRangeStart);
118363      SWAP(u8, bSeekPastNull, bStopAtNull);
118364    }
118365
118366    testcase( pRangeStart && (pRangeStart->eOperator & WO_LE)!=0 );
118367    testcase( pRangeStart && (pRangeStart->eOperator & WO_GE)!=0 );
118368    testcase( pRangeEnd && (pRangeEnd->eOperator & WO_LE)!=0 );
118369    testcase( pRangeEnd && (pRangeEnd->eOperator & WO_GE)!=0 );
118370    startEq = !pRangeStart || pRangeStart->eOperator & (WO_LE|WO_GE);
118371    endEq =   !pRangeEnd || pRangeEnd->eOperator & (WO_LE|WO_GE);
118372    start_constraints = pRangeStart || nEq>0;
118373
118374    /* Seek the index cursor to the start of the range. */
118375    nConstraint = nEq;
118376    if( pRangeStart ){
118377      Expr *pRight = pRangeStart->pExpr->pRight;
118378      sqlite3ExprCode(pParse, pRight, regBase+nEq);
118379      whereLikeOptimizationStringFixup(v, pLevel, pRangeStart);
118380      if( (pRangeStart->wtFlags & TERM_VNULL)==0
118381       && sqlite3ExprCanBeNull(pRight)
118382      ){
118383        sqlite3VdbeAddOp2(v, OP_IsNull, regBase+nEq, addrNxt);
118384        VdbeCoverage(v);
118385      }
118386      if( zStartAff ){
118387        if( sqlite3CompareAffinity(pRight, zStartAff[nEq])==SQLITE_AFF_BLOB){
118388          /* Since the comparison is to be performed with no conversions
118389          ** applied to the operands, set the affinity to apply to pRight to
118390          ** SQLITE_AFF_BLOB.  */
118391          zStartAff[nEq] = SQLITE_AFF_BLOB;
118392        }
118393        if( sqlite3ExprNeedsNoAffinityChange(pRight, zStartAff[nEq]) ){
118394          zStartAff[nEq] = SQLITE_AFF_BLOB;
118395        }
118396      }
118397      nConstraint++;
118398      testcase( pRangeStart->wtFlags & TERM_VIRTUAL );
118399    }else if( bSeekPastNull ){
118400      sqlite3VdbeAddOp2(v, OP_Null, 0, regBase+nEq);
118401      nConstraint++;
118402      startEq = 0;
118403      start_constraints = 1;
118404    }
118405    codeApplyAffinity(pParse, regBase, nConstraint - bSeekPastNull, zStartAff);
118406    op = aStartOp[(start_constraints<<2) + (startEq<<1) + bRev];
118407    assert( op!=0 );
118408    sqlite3VdbeAddOp4Int(v, op, iIdxCur, addrNxt, regBase, nConstraint);
118409    VdbeCoverage(v);
118410    VdbeCoverageIf(v, op==OP_Rewind);  testcase( op==OP_Rewind );
118411    VdbeCoverageIf(v, op==OP_Last);    testcase( op==OP_Last );
118412    VdbeCoverageIf(v, op==OP_SeekGT);  testcase( op==OP_SeekGT );
118413    VdbeCoverageIf(v, op==OP_SeekGE);  testcase( op==OP_SeekGE );
118414    VdbeCoverageIf(v, op==OP_SeekLE);  testcase( op==OP_SeekLE );
118415    VdbeCoverageIf(v, op==OP_SeekLT);  testcase( op==OP_SeekLT );
118416
118417    /* Load the value for the inequality constraint at the end of the
118418    ** range (if any).
118419    */
118420    nConstraint = nEq;
118421    if( pRangeEnd ){
118422      Expr *pRight = pRangeEnd->pExpr->pRight;
118423      sqlite3ExprCacheRemove(pParse, regBase+nEq, 1);
118424      sqlite3ExprCode(pParse, pRight, regBase+nEq);
118425      whereLikeOptimizationStringFixup(v, pLevel, pRangeEnd);
118426      if( (pRangeEnd->wtFlags & TERM_VNULL)==0
118427       && sqlite3ExprCanBeNull(pRight)
118428      ){
118429        sqlite3VdbeAddOp2(v, OP_IsNull, regBase+nEq, addrNxt);
118430        VdbeCoverage(v);
118431      }
118432      if( sqlite3CompareAffinity(pRight, cEndAff)!=SQLITE_AFF_BLOB
118433       && !sqlite3ExprNeedsNoAffinityChange(pRight, cEndAff)
118434      ){
118435        codeApplyAffinity(pParse, regBase+nEq, 1, &cEndAff);
118436      }
118437      nConstraint++;
118438      testcase( pRangeEnd->wtFlags & TERM_VIRTUAL );
118439    }else if( bStopAtNull ){
118440      sqlite3VdbeAddOp2(v, OP_Null, 0, regBase+nEq);
118441      endEq = 0;
118442      nConstraint++;
118443    }
118444    sqlite3DbFree(db, zStartAff);
118445
118446    /* Top of the loop body */
118447    pLevel->p2 = sqlite3VdbeCurrentAddr(v);
118448
118449    /* Check if the index cursor is past the end of the range. */
118450    if( nConstraint ){
118451      op = aEndOp[bRev*2 + endEq];
118452      sqlite3VdbeAddOp4Int(v, op, iIdxCur, addrNxt, regBase, nConstraint);
118453      testcase( op==OP_IdxGT );  VdbeCoverageIf(v, op==OP_IdxGT );
118454      testcase( op==OP_IdxGE );  VdbeCoverageIf(v, op==OP_IdxGE );
118455      testcase( op==OP_IdxLT );  VdbeCoverageIf(v, op==OP_IdxLT );
118456      testcase( op==OP_IdxLE );  VdbeCoverageIf(v, op==OP_IdxLE );
118457    }
118458
118459    /* Seek the table cursor, if required */
118460    disableTerm(pLevel, pRangeStart);
118461    disableTerm(pLevel, pRangeEnd);
118462    if( omitTable ){
118463      /* pIdx is a covering index.  No need to access the main table. */
118464    }else if( HasRowid(pIdx->pTable) ){
118465      iRowidReg = ++pParse->nMem;
118466      sqlite3VdbeAddOp2(v, OP_IdxRowid, iIdxCur, iRowidReg);
118467      sqlite3ExprCacheStore(pParse, iCur, -1, iRowidReg);
118468      sqlite3VdbeAddOp2(v, OP_Seek, iCur, iRowidReg);  /* Deferred seek */
118469    }else if( iCur!=iIdxCur ){
118470      Index *pPk = sqlite3PrimaryKeyIndex(pIdx->pTable);
118471      iRowidReg = sqlite3GetTempRange(pParse, pPk->nKeyCol);
118472      for(j=0; j<pPk->nKeyCol; j++){
118473        k = sqlite3ColumnOfIndex(pIdx, pPk->aiColumn[j]);
118474        sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, k, iRowidReg+j);
118475      }
118476      sqlite3VdbeAddOp4Int(v, OP_NotFound, iCur, addrCont,
118477                           iRowidReg, pPk->nKeyCol); VdbeCoverage(v);
118478    }
118479
118480    /* Record the instruction used to terminate the loop. Disable
118481    ** WHERE clause terms made redundant by the index range scan.
118482    */
118483    if( pLoop->wsFlags & WHERE_ONEROW ){
118484      pLevel->op = OP_Noop;
118485    }else if( bRev ){
118486      pLevel->op = OP_Prev;
118487    }else{
118488      pLevel->op = OP_Next;
118489    }
118490    pLevel->p1 = iIdxCur;
118491    pLevel->p3 = (pLoop->wsFlags&WHERE_UNQ_WANTED)!=0 ? 1:0;
118492    if( (pLoop->wsFlags & WHERE_CONSTRAINT)==0 ){
118493      pLevel->p5 = SQLITE_STMTSTATUS_FULLSCAN_STEP;
118494    }else{
118495      assert( pLevel->p5==0 );
118496    }
118497  }else
118498
118499#ifndef SQLITE_OMIT_OR_OPTIMIZATION
118500  if( pLoop->wsFlags & WHERE_MULTI_OR ){
118501    /* Case 5:  Two or more separately indexed terms connected by OR
118502    **
118503    ** Example:
118504    **
118505    **   CREATE TABLE t1(a,b,c,d);
118506    **   CREATE INDEX i1 ON t1(a);
118507    **   CREATE INDEX i2 ON t1(b);
118508    **   CREATE INDEX i3 ON t1(c);
118509    **
118510    **   SELECT * FROM t1 WHERE a=5 OR b=7 OR (c=11 AND d=13)
118511    **
118512    ** In the example, there are three indexed terms connected by OR.
118513    ** The top of the loop looks like this:
118514    **
118515    **          Null       1                # Zero the rowset in reg 1
118516    **
118517    ** Then, for each indexed term, the following. The arguments to
118518    ** RowSetTest are such that the rowid of the current row is inserted
118519    ** into the RowSet. If it is already present, control skips the
118520    ** Gosub opcode and jumps straight to the code generated by WhereEnd().
118521    **
118522    **        sqlite3WhereBegin(<term>)
118523    **          RowSetTest                  # Insert rowid into rowset
118524    **          Gosub      2 A
118525    **        sqlite3WhereEnd()
118526    **
118527    ** Following the above, code to terminate the loop. Label A, the target
118528    ** of the Gosub above, jumps to the instruction right after the Goto.
118529    **
118530    **          Null       1                # Zero the rowset in reg 1
118531    **          Goto       B                # The loop is finished.
118532    **
118533    **       A: <loop body>                 # Return data, whatever.
118534    **
118535    **          Return     2                # Jump back to the Gosub
118536    **
118537    **       B: <after the loop>
118538    **
118539    ** Added 2014-05-26: If the table is a WITHOUT ROWID table, then
118540    ** use an ephemeral index instead of a RowSet to record the primary
118541    ** keys of the rows we have already seen.
118542    **
118543    */
118544    WhereClause *pOrWc;    /* The OR-clause broken out into subterms */
118545    SrcList *pOrTab;       /* Shortened table list or OR-clause generation */
118546    Index *pCov = 0;             /* Potential covering index (or NULL) */
118547    int iCovCur = pParse->nTab++;  /* Cursor used for index scans (if any) */
118548
118549    int regReturn = ++pParse->nMem;           /* Register used with OP_Gosub */
118550    int regRowset = 0;                        /* Register for RowSet object */
118551    int regRowid = 0;                         /* Register holding rowid */
118552    int iLoopBody = sqlite3VdbeMakeLabel(v);  /* Start of loop body */
118553    int iRetInit;                             /* Address of regReturn init */
118554    int untestedTerms = 0;             /* Some terms not completely tested */
118555    int ii;                            /* Loop counter */
118556    u16 wctrlFlags;                    /* Flags for sub-WHERE clause */
118557    Expr *pAndExpr = 0;                /* An ".. AND (...)" expression */
118558    Table *pTab = pTabItem->pTab;
118559
118560    pTerm = pLoop->aLTerm[0];
118561    assert( pTerm!=0 );
118562    assert( pTerm->eOperator & WO_OR );
118563    assert( (pTerm->wtFlags & TERM_ORINFO)!=0 );
118564    pOrWc = &pTerm->u.pOrInfo->wc;
118565    pLevel->op = OP_Return;
118566    pLevel->p1 = regReturn;
118567
118568    /* Set up a new SrcList in pOrTab containing the table being scanned
118569    ** by this loop in the a[0] slot and all notReady tables in a[1..] slots.
118570    ** This becomes the SrcList in the recursive call to sqlite3WhereBegin().
118571    */
118572    if( pWInfo->nLevel>1 ){
118573      int nNotReady;                 /* The number of notReady tables */
118574      struct SrcList_item *origSrc;     /* Original list of tables */
118575      nNotReady = pWInfo->nLevel - iLevel - 1;
118576      pOrTab = sqlite3StackAllocRaw(db,
118577                            sizeof(*pOrTab)+ nNotReady*sizeof(pOrTab->a[0]));
118578      if( pOrTab==0 ) return notReady;
118579      pOrTab->nAlloc = (u8)(nNotReady + 1);
118580      pOrTab->nSrc = pOrTab->nAlloc;
118581      memcpy(pOrTab->a, pTabItem, sizeof(*pTabItem));
118582      origSrc = pWInfo->pTabList->a;
118583      for(k=1; k<=nNotReady; k++){
118584        memcpy(&pOrTab->a[k], &origSrc[pLevel[k].iFrom], sizeof(pOrTab->a[k]));
118585      }
118586    }else{
118587      pOrTab = pWInfo->pTabList;
118588    }
118589
118590    /* Initialize the rowset register to contain NULL. An SQL NULL is
118591    ** equivalent to an empty rowset.  Or, create an ephemeral index
118592    ** capable of holding primary keys in the case of a WITHOUT ROWID.
118593    **
118594    ** Also initialize regReturn to contain the address of the instruction
118595    ** immediately following the OP_Return at the bottom of the loop. This
118596    ** is required in a few obscure LEFT JOIN cases where control jumps
118597    ** over the top of the loop into the body of it. In this case the
118598    ** correct response for the end-of-loop code (the OP_Return) is to
118599    ** fall through to the next instruction, just as an OP_Next does if
118600    ** called on an uninitialized cursor.
118601    */
118602    if( (pWInfo->wctrlFlags & WHERE_DUPLICATES_OK)==0 ){
118603      if( HasRowid(pTab) ){
118604        regRowset = ++pParse->nMem;
118605        sqlite3VdbeAddOp2(v, OP_Null, 0, regRowset);
118606      }else{
118607        Index *pPk = sqlite3PrimaryKeyIndex(pTab);
118608        regRowset = pParse->nTab++;
118609        sqlite3VdbeAddOp2(v, OP_OpenEphemeral, regRowset, pPk->nKeyCol);
118610        sqlite3VdbeSetP4KeyInfo(pParse, pPk);
118611      }
118612      regRowid = ++pParse->nMem;
118613    }
118614    iRetInit = sqlite3VdbeAddOp2(v, OP_Integer, 0, regReturn);
118615
118616    /* If the original WHERE clause is z of the form:  (x1 OR x2 OR ...) AND y
118617    ** Then for every term xN, evaluate as the subexpression: xN AND z
118618    ** That way, terms in y that are factored into the disjunction will
118619    ** be picked up by the recursive calls to sqlite3WhereBegin() below.
118620    **
118621    ** Actually, each subexpression is converted to "xN AND w" where w is
118622    ** the "interesting" terms of z - terms that did not originate in the
118623    ** ON or USING clause of a LEFT JOIN, and terms that are usable as
118624    ** indices.
118625    **
118626    ** This optimization also only applies if the (x1 OR x2 OR ...) term
118627    ** is not contained in the ON clause of a LEFT JOIN.
118628    ** See ticket http://www.sqlite.org/src/info/f2369304e4
118629    */
118630    if( pWC->nTerm>1 ){
118631      int iTerm;
118632      for(iTerm=0; iTerm<pWC->nTerm; iTerm++){
118633        Expr *pExpr = pWC->a[iTerm].pExpr;
118634        if( &pWC->a[iTerm] == pTerm ) continue;
118635        if( ExprHasProperty(pExpr, EP_FromJoin) ) continue;
118636        if( (pWC->a[iTerm].wtFlags & TERM_VIRTUAL)!=0 ) continue;
118637        if( (pWC->a[iTerm].eOperator & WO_ALL)==0 ) continue;
118638        testcase( pWC->a[iTerm].wtFlags & TERM_ORINFO );
118639        pExpr = sqlite3ExprDup(db, pExpr, 0);
118640        pAndExpr = sqlite3ExprAnd(db, pAndExpr, pExpr);
118641      }
118642      if( pAndExpr ){
118643        pAndExpr = sqlite3PExpr(pParse, TK_AND, 0, pAndExpr, 0);
118644      }
118645    }
118646
118647    /* Run a separate WHERE clause for each term of the OR clause.  After
118648    ** eliminating duplicates from other WHERE clauses, the action for each
118649    ** sub-WHERE clause is to to invoke the main loop body as a subroutine.
118650    */
118651    wctrlFlags =  WHERE_OMIT_OPEN_CLOSE
118652                | WHERE_FORCE_TABLE
118653                | WHERE_ONETABLE_ONLY
118654                | WHERE_NO_AUTOINDEX;
118655    for(ii=0; ii<pOrWc->nTerm; ii++){
118656      WhereTerm *pOrTerm = &pOrWc->a[ii];
118657      if( pOrTerm->leftCursor==iCur || (pOrTerm->eOperator & WO_AND)!=0 ){
118658        WhereInfo *pSubWInfo;           /* Info for single OR-term scan */
118659        Expr *pOrExpr = pOrTerm->pExpr; /* Current OR clause term */
118660        int j1 = 0;                     /* Address of jump operation */
118661        if( pAndExpr && !ExprHasProperty(pOrExpr, EP_FromJoin) ){
118662          pAndExpr->pLeft = pOrExpr;
118663          pOrExpr = pAndExpr;
118664        }
118665        /* Loop through table entries that match term pOrTerm. */
118666        WHERETRACE(0xffff, ("Subplan for OR-clause:\n"));
118667        pSubWInfo = sqlite3WhereBegin(pParse, pOrTab, pOrExpr, 0, 0,
118668                                      wctrlFlags, iCovCur);
118669        assert( pSubWInfo || pParse->nErr || db->mallocFailed );
118670        if( pSubWInfo ){
118671          WhereLoop *pSubLoop;
118672          int addrExplain = sqlite3WhereExplainOneScan(
118673              pParse, pOrTab, &pSubWInfo->a[0], iLevel, pLevel->iFrom, 0
118674          );
118675          sqlite3WhereAddScanStatus(v, pOrTab, &pSubWInfo->a[0], addrExplain);
118676
118677          /* This is the sub-WHERE clause body.  First skip over
118678          ** duplicate rows from prior sub-WHERE clauses, and record the
118679          ** rowid (or PRIMARY KEY) for the current row so that the same
118680          ** row will be skipped in subsequent sub-WHERE clauses.
118681          */
118682          if( (pWInfo->wctrlFlags & WHERE_DUPLICATES_OK)==0 ){
118683            int r;
118684            int iSet = ((ii==pOrWc->nTerm-1)?-1:ii);
118685            if( HasRowid(pTab) ){
118686              r = sqlite3ExprCodeGetColumn(pParse, pTab, -1, iCur, regRowid, 0);
118687              j1 = sqlite3VdbeAddOp4Int(v, OP_RowSetTest, regRowset, 0, r,iSet);
118688              VdbeCoverage(v);
118689            }else{
118690              Index *pPk = sqlite3PrimaryKeyIndex(pTab);
118691              int nPk = pPk->nKeyCol;
118692              int iPk;
118693
118694              /* Read the PK into an array of temp registers. */
118695              r = sqlite3GetTempRange(pParse, nPk);
118696              for(iPk=0; iPk<nPk; iPk++){
118697                int iCol = pPk->aiColumn[iPk];
118698                int rx;
118699                rx = sqlite3ExprCodeGetColumn(pParse, pTab, iCol, iCur,r+iPk,0);
118700                if( rx!=r+iPk ){
118701                  sqlite3VdbeAddOp2(v, OP_SCopy, rx, r+iPk);
118702                }
118703              }
118704
118705              /* Check if the temp table already contains this key. If so,
118706              ** the row has already been included in the result set and
118707              ** can be ignored (by jumping past the Gosub below). Otherwise,
118708              ** insert the key into the temp table and proceed with processing
118709              ** the row.
118710              **
118711              ** Use some of the same optimizations as OP_RowSetTest: If iSet
118712              ** is zero, assume that the key cannot already be present in
118713              ** the temp table. And if iSet is -1, assume that there is no
118714              ** need to insert the key into the temp table, as it will never
118715              ** be tested for.  */
118716              if( iSet ){
118717                j1 = sqlite3VdbeAddOp4Int(v, OP_Found, regRowset, 0, r, nPk);
118718                VdbeCoverage(v);
118719              }
118720              if( iSet>=0 ){
118721                sqlite3VdbeAddOp3(v, OP_MakeRecord, r, nPk, regRowid);
118722                sqlite3VdbeAddOp3(v, OP_IdxInsert, regRowset, regRowid, 0);
118723                if( iSet ) sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT);
118724              }
118725
118726              /* Release the array of temp registers */
118727              sqlite3ReleaseTempRange(pParse, r, nPk);
118728            }
118729          }
118730
118731          /* Invoke the main loop body as a subroutine */
118732          sqlite3VdbeAddOp2(v, OP_Gosub, regReturn, iLoopBody);
118733
118734          /* Jump here (skipping the main loop body subroutine) if the
118735          ** current sub-WHERE row is a duplicate from prior sub-WHEREs. */
118736          if( j1 ) sqlite3VdbeJumpHere(v, j1);
118737
118738          /* The pSubWInfo->untestedTerms flag means that this OR term
118739          ** contained one or more AND term from a notReady table.  The
118740          ** terms from the notReady table could not be tested and will
118741          ** need to be tested later.
118742          */
118743          if( pSubWInfo->untestedTerms ) untestedTerms = 1;
118744
118745          /* If all of the OR-connected terms are optimized using the same
118746          ** index, and the index is opened using the same cursor number
118747          ** by each call to sqlite3WhereBegin() made by this loop, it may
118748          ** be possible to use that index as a covering index.
118749          **
118750          ** If the call to sqlite3WhereBegin() above resulted in a scan that
118751          ** uses an index, and this is either the first OR-connected term
118752          ** processed or the index is the same as that used by all previous
118753          ** terms, set pCov to the candidate covering index. Otherwise, set
118754          ** pCov to NULL to indicate that no candidate covering index will
118755          ** be available.
118756          */
118757          pSubLoop = pSubWInfo->a[0].pWLoop;
118758          assert( (pSubLoop->wsFlags & WHERE_AUTO_INDEX)==0 );
118759          if( (pSubLoop->wsFlags & WHERE_INDEXED)!=0
118760           && (ii==0 || pSubLoop->u.btree.pIndex==pCov)
118761           && (HasRowid(pTab) || !IsPrimaryKeyIndex(pSubLoop->u.btree.pIndex))
118762          ){
118763            assert( pSubWInfo->a[0].iIdxCur==iCovCur );
118764            pCov = pSubLoop->u.btree.pIndex;
118765            wctrlFlags |= WHERE_REOPEN_IDX;
118766          }else{
118767            pCov = 0;
118768          }
118769
118770          /* Finish the loop through table entries that match term pOrTerm. */
118771          sqlite3WhereEnd(pSubWInfo);
118772        }
118773      }
118774    }
118775    pLevel->u.pCovidx = pCov;
118776    if( pCov ) pLevel->iIdxCur = iCovCur;
118777    if( pAndExpr ){
118778      pAndExpr->pLeft = 0;
118779      sqlite3ExprDelete(db, pAndExpr);
118780    }
118781    sqlite3VdbeChangeP1(v, iRetInit, sqlite3VdbeCurrentAddr(v));
118782    sqlite3VdbeAddOp2(v, OP_Goto, 0, pLevel->addrBrk);
118783    sqlite3VdbeResolveLabel(v, iLoopBody);
118784
118785    if( pWInfo->nLevel>1 ) sqlite3StackFree(db, pOrTab);
118786    if( !untestedTerms ) disableTerm(pLevel, pTerm);
118787  }else
118788#endif /* SQLITE_OMIT_OR_OPTIMIZATION */
118789
118790  {
118791    /* Case 6:  There is no usable index.  We must do a complete
118792    **          scan of the entire table.
118793    */
118794    static const u8 aStep[] = { OP_Next, OP_Prev };
118795    static const u8 aStart[] = { OP_Rewind, OP_Last };
118796    assert( bRev==0 || bRev==1 );
118797    if( pTabItem->isRecursive ){
118798      /* Tables marked isRecursive have only a single row that is stored in
118799      ** a pseudo-cursor.  No need to Rewind or Next such cursors. */
118800      pLevel->op = OP_Noop;
118801    }else{
118802      pLevel->op = aStep[bRev];
118803      pLevel->p1 = iCur;
118804      pLevel->p2 = 1 + sqlite3VdbeAddOp2(v, aStart[bRev], iCur, addrBrk);
118805      VdbeCoverageIf(v, bRev==0);
118806      VdbeCoverageIf(v, bRev!=0);
118807      pLevel->p5 = SQLITE_STMTSTATUS_FULLSCAN_STEP;
118808    }
118809  }
118810
118811#ifdef SQLITE_ENABLE_STMT_SCANSTATUS
118812  pLevel->addrVisit = sqlite3VdbeCurrentAddr(v);
118813#endif
118814
118815  /* Insert code to test every subexpression that can be completely
118816  ** computed using the current set of tables.
118817  */
118818  for(pTerm=pWC->a, j=pWC->nTerm; j>0; j--, pTerm++){
118819    Expr *pE;
118820    int skipLikeAddr = 0;
118821    testcase( pTerm->wtFlags & TERM_VIRTUAL );
118822    testcase( pTerm->wtFlags & TERM_CODED );
118823    if( pTerm->wtFlags & (TERM_VIRTUAL|TERM_CODED) ) continue;
118824    if( (pTerm->prereqAll & pLevel->notReady)!=0 ){
118825      testcase( pWInfo->untestedTerms==0
118826               && (pWInfo->wctrlFlags & WHERE_ONETABLE_ONLY)!=0 );
118827      pWInfo->untestedTerms = 1;
118828      continue;
118829    }
118830    pE = pTerm->pExpr;
118831    assert( pE!=0 );
118832    if( pLevel->iLeftJoin && !ExprHasProperty(pE, EP_FromJoin) ){
118833      continue;
118834    }
118835    if( pTerm->wtFlags & TERM_LIKECOND ){
118836      assert( pLevel->iLikeRepCntr>0 );
118837      skipLikeAddr = sqlite3VdbeAddOp1(v, OP_IfNot, pLevel->iLikeRepCntr);
118838      VdbeCoverage(v);
118839    }
118840    sqlite3ExprIfFalse(pParse, pE, addrCont, SQLITE_JUMPIFNULL);
118841    if( skipLikeAddr ) sqlite3VdbeJumpHere(v, skipLikeAddr);
118842    pTerm->wtFlags |= TERM_CODED;
118843  }
118844
118845  /* Insert code to test for implied constraints based on transitivity
118846  ** of the "==" operator.
118847  **
118848  ** Example: If the WHERE clause contains "t1.a=t2.b" and "t2.b=123"
118849  ** and we are coding the t1 loop and the t2 loop has not yet coded,
118850  ** then we cannot use the "t1.a=t2.b" constraint, but we can code
118851  ** the implied "t1.a=123" constraint.
118852  */
118853  for(pTerm=pWC->a, j=pWC->nTerm; j>0; j--, pTerm++){
118854    Expr *pE, *pEAlt;
118855    WhereTerm *pAlt;
118856    if( pTerm->wtFlags & (TERM_VIRTUAL|TERM_CODED) ) continue;
118857    if( (pTerm->eOperator & (WO_EQ|WO_IS))==0 ) continue;
118858    if( (pTerm->eOperator & WO_EQUIV)==0 ) continue;
118859    if( pTerm->leftCursor!=iCur ) continue;
118860    if( pLevel->iLeftJoin ) continue;
118861    pE = pTerm->pExpr;
118862    assert( !ExprHasProperty(pE, EP_FromJoin) );
118863    assert( (pTerm->prereqRight & pLevel->notReady)!=0 );
118864    pAlt = sqlite3WhereFindTerm(pWC, iCur, pTerm->u.leftColumn, notReady,
118865                    WO_EQ|WO_IN|WO_IS, 0);
118866    if( pAlt==0 ) continue;
118867    if( pAlt->wtFlags & (TERM_CODED) ) continue;
118868    testcase( pAlt->eOperator & WO_EQ );
118869    testcase( pAlt->eOperator & WO_IS );
118870    testcase( pAlt->eOperator & WO_IN );
118871    VdbeModuleComment((v, "begin transitive constraint"));
118872    pEAlt = sqlite3StackAllocRaw(db, sizeof(*pEAlt));
118873    if( pEAlt ){
118874      *pEAlt = *pAlt->pExpr;
118875      pEAlt->pLeft = pE->pLeft;
118876      sqlite3ExprIfFalse(pParse, pEAlt, addrCont, SQLITE_JUMPIFNULL);
118877      sqlite3StackFree(db, pEAlt);
118878    }
118879  }
118880
118881  /* For a LEFT OUTER JOIN, generate code that will record the fact that
118882  ** at least one row of the right table has matched the left table.
118883  */
118884  if( pLevel->iLeftJoin ){
118885    pLevel->addrFirst = sqlite3VdbeCurrentAddr(v);
118886    sqlite3VdbeAddOp2(v, OP_Integer, 1, pLevel->iLeftJoin);
118887    VdbeComment((v, "record LEFT JOIN hit"));
118888    sqlite3ExprCacheClear(pParse);
118889    for(pTerm=pWC->a, j=0; j<pWC->nTerm; j++, pTerm++){
118890      testcase( pTerm->wtFlags & TERM_VIRTUAL );
118891      testcase( pTerm->wtFlags & TERM_CODED );
118892      if( pTerm->wtFlags & (TERM_VIRTUAL|TERM_CODED) ) continue;
118893      if( (pTerm->prereqAll & pLevel->notReady)!=0 ){
118894        assert( pWInfo->untestedTerms );
118895        continue;
118896      }
118897      assert( pTerm->pExpr );
118898      sqlite3ExprIfFalse(pParse, pTerm->pExpr, addrCont, SQLITE_JUMPIFNULL);
118899      pTerm->wtFlags |= TERM_CODED;
118900    }
118901  }
118902
118903  return pLevel->notReady;
118904}
118905
118906/************** End of wherecode.c *******************************************/
118907/************** Begin file whereexpr.c ***************************************/
118908/*
118909** 2015-06-08
118910**
118911** The author disclaims copyright to this source code.  In place of
118912** a legal notice, here is a blessing:
118913**
118914**    May you do good and not evil.
118915**    May you find forgiveness for yourself and forgive others.
118916**    May you share freely, never taking more than you give.
118917**
118918*************************************************************************
118919** This module contains C code that generates VDBE code used to process
118920** the WHERE clause of SQL statements.
118921**
118922** This file was originally part of where.c but was split out to improve
118923** readability and editabiliity.  This file contains utility routines for
118924** analyzing Expr objects in the WHERE clause.
118925*/
118926/* #include "sqliteInt.h" */
118927/* #include "whereInt.h" */
118928
118929/* Forward declarations */
118930static void exprAnalyze(SrcList*, WhereClause*, int);
118931
118932/*
118933** Deallocate all memory associated with a WhereOrInfo object.
118934*/
118935static void whereOrInfoDelete(sqlite3 *db, WhereOrInfo *p){
118936  sqlite3WhereClauseClear(&p->wc);
118937  sqlite3DbFree(db, p);
118938}
118939
118940/*
118941** Deallocate all memory associated with a WhereAndInfo object.
118942*/
118943static void whereAndInfoDelete(sqlite3 *db, WhereAndInfo *p){
118944  sqlite3WhereClauseClear(&p->wc);
118945  sqlite3DbFree(db, p);
118946}
118947
118948/*
118949** Add a single new WhereTerm entry to the WhereClause object pWC.
118950** The new WhereTerm object is constructed from Expr p and with wtFlags.
118951** The index in pWC->a[] of the new WhereTerm is returned on success.
118952** 0 is returned if the new WhereTerm could not be added due to a memory
118953** allocation error.  The memory allocation failure will be recorded in
118954** the db->mallocFailed flag so that higher-level functions can detect it.
118955**
118956** This routine will increase the size of the pWC->a[] array as necessary.
118957**
118958** If the wtFlags argument includes TERM_DYNAMIC, then responsibility
118959** for freeing the expression p is assumed by the WhereClause object pWC.
118960** This is true even if this routine fails to allocate a new WhereTerm.
118961**
118962** WARNING:  This routine might reallocate the space used to store
118963** WhereTerms.  All pointers to WhereTerms should be invalidated after
118964** calling this routine.  Such pointers may be reinitialized by referencing
118965** the pWC->a[] array.
118966*/
118967static int whereClauseInsert(WhereClause *pWC, Expr *p, u16 wtFlags){
118968  WhereTerm *pTerm;
118969  int idx;
118970  testcase( wtFlags & TERM_VIRTUAL );
118971  if( pWC->nTerm>=pWC->nSlot ){
118972    WhereTerm *pOld = pWC->a;
118973    sqlite3 *db = pWC->pWInfo->pParse->db;
118974    pWC->a = sqlite3DbMallocRaw(db, sizeof(pWC->a[0])*pWC->nSlot*2 );
118975    if( pWC->a==0 ){
118976      if( wtFlags & TERM_DYNAMIC ){
118977        sqlite3ExprDelete(db, p);
118978      }
118979      pWC->a = pOld;
118980      return 0;
118981    }
118982    memcpy(pWC->a, pOld, sizeof(pWC->a[0])*pWC->nTerm);
118983    if( pOld!=pWC->aStatic ){
118984      sqlite3DbFree(db, pOld);
118985    }
118986    pWC->nSlot = sqlite3DbMallocSize(db, pWC->a)/sizeof(pWC->a[0]);
118987    memset(&pWC->a[pWC->nTerm], 0, sizeof(pWC->a[0])*(pWC->nSlot-pWC->nTerm));
118988  }
118989  pTerm = &pWC->a[idx = pWC->nTerm++];
118990  if( p && ExprHasProperty(p, EP_Unlikely) ){
118991    pTerm->truthProb = sqlite3LogEst(p->iTable) - 270;
118992  }else{
118993    pTerm->truthProb = 1;
118994  }
118995  pTerm->pExpr = sqlite3ExprSkipCollate(p);
118996  pTerm->wtFlags = wtFlags;
118997  pTerm->pWC = pWC;
118998  pTerm->iParent = -1;
118999  return idx;
119000}
119001
119002/*
119003** Return TRUE if the given operator is one of the operators that is
119004** allowed for an indexable WHERE clause term.  The allowed operators are
119005** "=", "<", ">", "<=", ">=", "IN", and "IS NULL"
119006*/
119007static int allowedOp(int op){
119008  assert( TK_GT>TK_EQ && TK_GT<TK_GE );
119009  assert( TK_LT>TK_EQ && TK_LT<TK_GE );
119010  assert( TK_LE>TK_EQ && TK_LE<TK_GE );
119011  assert( TK_GE==TK_EQ+4 );
119012  return op==TK_IN || (op>=TK_EQ && op<=TK_GE) || op==TK_ISNULL || op==TK_IS;
119013}
119014
119015/*
119016** Commute a comparison operator.  Expressions of the form "X op Y"
119017** are converted into "Y op X".
119018**
119019** If left/right precedence rules come into play when determining the
119020** collating sequence, then COLLATE operators are adjusted to ensure
119021** that the collating sequence does not change.  For example:
119022** "Y collate NOCASE op X" becomes "X op Y" because any collation sequence on
119023** the left hand side of a comparison overrides any collation sequence
119024** attached to the right. For the same reason the EP_Collate flag
119025** is not commuted.
119026*/
119027static void exprCommute(Parse *pParse, Expr *pExpr){
119028  u16 expRight = (pExpr->pRight->flags & EP_Collate);
119029  u16 expLeft = (pExpr->pLeft->flags & EP_Collate);
119030  assert( allowedOp(pExpr->op) && pExpr->op!=TK_IN );
119031  if( expRight==expLeft ){
119032    /* Either X and Y both have COLLATE operator or neither do */
119033    if( expRight ){
119034      /* Both X and Y have COLLATE operators.  Make sure X is always
119035      ** used by clearing the EP_Collate flag from Y. */
119036      pExpr->pRight->flags &= ~EP_Collate;
119037    }else if( sqlite3ExprCollSeq(pParse, pExpr->pLeft)!=0 ){
119038      /* Neither X nor Y have COLLATE operators, but X has a non-default
119039      ** collating sequence.  So add the EP_Collate marker on X to cause
119040      ** it to be searched first. */
119041      pExpr->pLeft->flags |= EP_Collate;
119042    }
119043  }
119044  SWAP(Expr*,pExpr->pRight,pExpr->pLeft);
119045  if( pExpr->op>=TK_GT ){
119046    assert( TK_LT==TK_GT+2 );
119047    assert( TK_GE==TK_LE+2 );
119048    assert( TK_GT>TK_EQ );
119049    assert( TK_GT<TK_LE );
119050    assert( pExpr->op>=TK_GT && pExpr->op<=TK_GE );
119051    pExpr->op = ((pExpr->op-TK_GT)^2)+TK_GT;
119052  }
119053}
119054
119055/*
119056** Translate from TK_xx operator to WO_xx bitmask.
119057*/
119058static u16 operatorMask(int op){
119059  u16 c;
119060  assert( allowedOp(op) );
119061  if( op==TK_IN ){
119062    c = WO_IN;
119063  }else if( op==TK_ISNULL ){
119064    c = WO_ISNULL;
119065  }else if( op==TK_IS ){
119066    c = WO_IS;
119067  }else{
119068    assert( (WO_EQ<<(op-TK_EQ)) < 0x7fff );
119069    c = (u16)(WO_EQ<<(op-TK_EQ));
119070  }
119071  assert( op!=TK_ISNULL || c==WO_ISNULL );
119072  assert( op!=TK_IN || c==WO_IN );
119073  assert( op!=TK_EQ || c==WO_EQ );
119074  assert( op!=TK_LT || c==WO_LT );
119075  assert( op!=TK_LE || c==WO_LE );
119076  assert( op!=TK_GT || c==WO_GT );
119077  assert( op!=TK_GE || c==WO_GE );
119078  assert( op!=TK_IS || c==WO_IS );
119079  return c;
119080}
119081
119082
119083#ifndef SQLITE_OMIT_LIKE_OPTIMIZATION
119084/*
119085** Check to see if the given expression is a LIKE or GLOB operator that
119086** can be optimized using inequality constraints.  Return TRUE if it is
119087** so and false if not.
119088**
119089** In order for the operator to be optimizible, the RHS must be a string
119090** literal that does not begin with a wildcard.  The LHS must be a column
119091** that may only be NULL, a string, or a BLOB, never a number. (This means
119092** that virtual tables cannot participate in the LIKE optimization.)  The
119093** collating sequence for the column on the LHS must be appropriate for
119094** the operator.
119095*/
119096static int isLikeOrGlob(
119097  Parse *pParse,    /* Parsing and code generating context */
119098  Expr *pExpr,      /* Test this expression */
119099  Expr **ppPrefix,  /* Pointer to TK_STRING expression with pattern prefix */
119100  int *pisComplete, /* True if the only wildcard is % in the last character */
119101  int *pnoCase      /* True if uppercase is equivalent to lowercase */
119102){
119103  const char *z = 0;         /* String on RHS of LIKE operator */
119104  Expr *pRight, *pLeft;      /* Right and left size of LIKE operator */
119105  ExprList *pList;           /* List of operands to the LIKE operator */
119106  int c;                     /* One character in z[] */
119107  int cnt;                   /* Number of non-wildcard prefix characters */
119108  char wc[3];                /* Wildcard characters */
119109  sqlite3 *db = pParse->db;  /* Database connection */
119110  sqlite3_value *pVal = 0;
119111  int op;                    /* Opcode of pRight */
119112
119113  if( !sqlite3IsLikeFunction(db, pExpr, pnoCase, wc) ){
119114    return 0;
119115  }
119116#ifdef SQLITE_EBCDIC
119117  if( *pnoCase ) return 0;
119118#endif
119119  pList = pExpr->x.pList;
119120  pLeft = pList->a[1].pExpr;
119121  if( pLeft->op!=TK_COLUMN
119122   || sqlite3ExprAffinity(pLeft)!=SQLITE_AFF_TEXT
119123   || IsVirtual(pLeft->pTab)  /* Value might be numeric */
119124  ){
119125    /* IMP: R-02065-49465 The left-hand side of the LIKE or GLOB operator must
119126    ** be the name of an indexed column with TEXT affinity. */
119127    return 0;
119128  }
119129  assert( pLeft->iColumn!=(-1) ); /* Because IPK never has AFF_TEXT */
119130
119131  pRight = sqlite3ExprSkipCollate(pList->a[0].pExpr);
119132  op = pRight->op;
119133  if( op==TK_VARIABLE ){
119134    Vdbe *pReprepare = pParse->pReprepare;
119135    int iCol = pRight->iColumn;
119136    pVal = sqlite3VdbeGetBoundValue(pReprepare, iCol, SQLITE_AFF_BLOB);
119137    if( pVal && sqlite3_value_type(pVal)==SQLITE_TEXT ){
119138      z = (char *)sqlite3_value_text(pVal);
119139    }
119140    sqlite3VdbeSetVarmask(pParse->pVdbe, iCol);
119141    assert( pRight->op==TK_VARIABLE || pRight->op==TK_REGISTER );
119142  }else if( op==TK_STRING ){
119143    z = pRight->u.zToken;
119144  }
119145  if( z ){
119146    cnt = 0;
119147    while( (c=z[cnt])!=0 && c!=wc[0] && c!=wc[1] && c!=wc[2] ){
119148      cnt++;
119149    }
119150    if( cnt!=0 && 255!=(u8)z[cnt-1] ){
119151      Expr *pPrefix;
119152      *pisComplete = c==wc[0] && z[cnt+1]==0;
119153      pPrefix = sqlite3Expr(db, TK_STRING, z);
119154      if( pPrefix ) pPrefix->u.zToken[cnt] = 0;
119155      *ppPrefix = pPrefix;
119156      if( op==TK_VARIABLE ){
119157        Vdbe *v = pParse->pVdbe;
119158        sqlite3VdbeSetVarmask(v, pRight->iColumn);
119159        if( *pisComplete && pRight->u.zToken[1] ){
119160          /* If the rhs of the LIKE expression is a variable, and the current
119161          ** value of the variable means there is no need to invoke the LIKE
119162          ** function, then no OP_Variable will be added to the program.
119163          ** This causes problems for the sqlite3_bind_parameter_name()
119164          ** API. To work around them, add a dummy OP_Variable here.
119165          */
119166          int r1 = sqlite3GetTempReg(pParse);
119167          sqlite3ExprCodeTarget(pParse, pRight, r1);
119168          sqlite3VdbeChangeP3(v, sqlite3VdbeCurrentAddr(v)-1, 0);
119169          sqlite3ReleaseTempReg(pParse, r1);
119170        }
119171      }
119172    }else{
119173      z = 0;
119174    }
119175  }
119176
119177  sqlite3ValueFree(pVal);
119178  return (z!=0);
119179}
119180#endif /* SQLITE_OMIT_LIKE_OPTIMIZATION */
119181
119182
119183#ifndef SQLITE_OMIT_VIRTUALTABLE
119184/*
119185** Check to see if the given expression is of the form
119186**
119187**         column MATCH expr
119188**
119189** If it is then return TRUE.  If not, return FALSE.
119190*/
119191static int isMatchOfColumn(
119192  Expr *pExpr      /* Test this expression */
119193){
119194  ExprList *pList;
119195
119196  if( pExpr->op!=TK_FUNCTION ){
119197    return 0;
119198  }
119199  if( sqlite3StrICmp(pExpr->u.zToken,"match")!=0 ){
119200    return 0;
119201  }
119202  pList = pExpr->x.pList;
119203  if( pList->nExpr!=2 ){
119204    return 0;
119205  }
119206  if( pList->a[1].pExpr->op != TK_COLUMN ){
119207    return 0;
119208  }
119209  return 1;
119210}
119211#endif /* SQLITE_OMIT_VIRTUALTABLE */
119212
119213/*
119214** If the pBase expression originated in the ON or USING clause of
119215** a join, then transfer the appropriate markings over to derived.
119216*/
119217static void transferJoinMarkings(Expr *pDerived, Expr *pBase){
119218  if( pDerived ){
119219    pDerived->flags |= pBase->flags & EP_FromJoin;
119220    pDerived->iRightJoinTable = pBase->iRightJoinTable;
119221  }
119222}
119223
119224/*
119225** Mark term iChild as being a child of term iParent
119226*/
119227static void markTermAsChild(WhereClause *pWC, int iChild, int iParent){
119228  pWC->a[iChild].iParent = iParent;
119229  pWC->a[iChild].truthProb = pWC->a[iParent].truthProb;
119230  pWC->a[iParent].nChild++;
119231}
119232
119233/*
119234** Return the N-th AND-connected subterm of pTerm.  Or if pTerm is not
119235** a conjunction, then return just pTerm when N==0.  If N is exceeds
119236** the number of available subterms, return NULL.
119237*/
119238static WhereTerm *whereNthSubterm(WhereTerm *pTerm, int N){
119239  if( pTerm->eOperator!=WO_AND ){
119240    return N==0 ? pTerm : 0;
119241  }
119242  if( N<pTerm->u.pAndInfo->wc.nTerm ){
119243    return &pTerm->u.pAndInfo->wc.a[N];
119244  }
119245  return 0;
119246}
119247
119248/*
119249** Subterms pOne and pTwo are contained within WHERE clause pWC.  The
119250** two subterms are in disjunction - they are OR-ed together.
119251**
119252** If these two terms are both of the form:  "A op B" with the same
119253** A and B values but different operators and if the operators are
119254** compatible (if one is = and the other is <, for example) then
119255** add a new virtual AND term to pWC that is the combination of the
119256** two.
119257**
119258** Some examples:
119259**
119260**    x<y OR x=y    -->     x<=y
119261**    x=y OR x=y    -->     x=y
119262**    x<=y OR x<y   -->     x<=y
119263**
119264** The following is NOT generated:
119265**
119266**    x<y OR x>y    -->     x!=y
119267*/
119268static void whereCombineDisjuncts(
119269  SrcList *pSrc,         /* the FROM clause */
119270  WhereClause *pWC,      /* The complete WHERE clause */
119271  WhereTerm *pOne,       /* First disjunct */
119272  WhereTerm *pTwo        /* Second disjunct */
119273){
119274  u16 eOp = pOne->eOperator | pTwo->eOperator;
119275  sqlite3 *db;           /* Database connection (for malloc) */
119276  Expr *pNew;            /* New virtual expression */
119277  int op;                /* Operator for the combined expression */
119278  int idxNew;            /* Index in pWC of the next virtual term */
119279
119280  if( (pOne->eOperator & (WO_EQ|WO_LT|WO_LE|WO_GT|WO_GE))==0 ) return;
119281  if( (pTwo->eOperator & (WO_EQ|WO_LT|WO_LE|WO_GT|WO_GE))==0 ) return;
119282  if( (eOp & (WO_EQ|WO_LT|WO_LE))!=eOp
119283   && (eOp & (WO_EQ|WO_GT|WO_GE))!=eOp ) return;
119284  assert( pOne->pExpr->pLeft!=0 && pOne->pExpr->pRight!=0 );
119285  assert( pTwo->pExpr->pLeft!=0 && pTwo->pExpr->pRight!=0 );
119286  if( sqlite3ExprCompare(pOne->pExpr->pLeft, pTwo->pExpr->pLeft, -1) ) return;
119287  if( sqlite3ExprCompare(pOne->pExpr->pRight, pTwo->pExpr->pRight, -1) )return;
119288  /* If we reach this point, it means the two subterms can be combined */
119289  if( (eOp & (eOp-1))!=0 ){
119290    if( eOp & (WO_LT|WO_LE) ){
119291      eOp = WO_LE;
119292    }else{
119293      assert( eOp & (WO_GT|WO_GE) );
119294      eOp = WO_GE;
119295    }
119296  }
119297  db = pWC->pWInfo->pParse->db;
119298  pNew = sqlite3ExprDup(db, pOne->pExpr, 0);
119299  if( pNew==0 ) return;
119300  for(op=TK_EQ; eOp!=(WO_EQ<<(op-TK_EQ)); op++){ assert( op<TK_GE ); }
119301  pNew->op = op;
119302  idxNew = whereClauseInsert(pWC, pNew, TERM_VIRTUAL|TERM_DYNAMIC);
119303  exprAnalyze(pSrc, pWC, idxNew);
119304}
119305
119306#if !defined(SQLITE_OMIT_OR_OPTIMIZATION) && !defined(SQLITE_OMIT_SUBQUERY)
119307/*
119308** Analyze a term that consists of two or more OR-connected
119309** subterms.  So in:
119310**
119311**     ... WHERE  (a=5) AND (b=7 OR c=9 OR d=13) AND (d=13)
119312**                          ^^^^^^^^^^^^^^^^^^^^
119313**
119314** This routine analyzes terms such as the middle term in the above example.
119315** A WhereOrTerm object is computed and attached to the term under
119316** analysis, regardless of the outcome of the analysis.  Hence:
119317**
119318**     WhereTerm.wtFlags   |=  TERM_ORINFO
119319**     WhereTerm.u.pOrInfo  =  a dynamically allocated WhereOrTerm object
119320**
119321** The term being analyzed must have two or more of OR-connected subterms.
119322** A single subterm might be a set of AND-connected sub-subterms.
119323** Examples of terms under analysis:
119324**
119325**     (A)     t1.x=t2.y OR t1.x=t2.z OR t1.y=15 OR t1.z=t3.a+5
119326**     (B)     x=expr1 OR expr2=x OR x=expr3
119327**     (C)     t1.x=t2.y OR (t1.x=t2.z AND t1.y=15)
119328**     (D)     x=expr1 OR (y>11 AND y<22 AND z LIKE '*hello*')
119329**     (E)     (p.a=1 AND q.b=2 AND r.c=3) OR (p.x=4 AND q.y=5 AND r.z=6)
119330**     (F)     x>A OR (x=A AND y>=B)
119331**
119332** CASE 1:
119333**
119334** If all subterms are of the form T.C=expr for some single column of C and
119335** a single table T (as shown in example B above) then create a new virtual
119336** term that is an equivalent IN expression.  In other words, if the term
119337** being analyzed is:
119338**
119339**      x = expr1  OR  expr2 = x  OR  x = expr3
119340**
119341** then create a new virtual term like this:
119342**
119343**      x IN (expr1,expr2,expr3)
119344**
119345** CASE 2:
119346**
119347** If there are exactly two disjuncts and one side has x>A and the other side
119348** has x=A (for the same x and A) then add a new virtual conjunct term to the
119349** WHERE clause of the form "x>=A".  Example:
119350**
119351**      x>A OR (x=A AND y>B)    adds:    x>=A
119352**
119353** The added conjunct can sometimes be helpful in query planning.
119354**
119355** CASE 3:
119356**
119357** If all subterms are indexable by a single table T, then set
119358**
119359**     WhereTerm.eOperator              =  WO_OR
119360**     WhereTerm.u.pOrInfo->indexable  |=  the cursor number for table T
119361**
119362** A subterm is "indexable" if it is of the form
119363** "T.C <op> <expr>" where C is any column of table T and
119364** <op> is one of "=", "<", "<=", ">", ">=", "IS NULL", or "IN".
119365** A subterm is also indexable if it is an AND of two or more
119366** subsubterms at least one of which is indexable.  Indexable AND
119367** subterms have their eOperator set to WO_AND and they have
119368** u.pAndInfo set to a dynamically allocated WhereAndTerm object.
119369**
119370** From another point of view, "indexable" means that the subterm could
119371** potentially be used with an index if an appropriate index exists.
119372** This analysis does not consider whether or not the index exists; that
119373** is decided elsewhere.  This analysis only looks at whether subterms
119374** appropriate for indexing exist.
119375**
119376** All examples A through E above satisfy case 3.  But if a term
119377** also satisfies case 1 (such as B) we know that the optimizer will
119378** always prefer case 1, so in that case we pretend that case 3 is not
119379** satisfied.
119380**
119381** It might be the case that multiple tables are indexable.  For example,
119382** (E) above is indexable on tables P, Q, and R.
119383**
119384** Terms that satisfy case 3 are candidates for lookup by using
119385** separate indices to find rowids for each subterm and composing
119386** the union of all rowids using a RowSet object.  This is similar
119387** to "bitmap indices" in other database engines.
119388**
119389** OTHERWISE:
119390**
119391** If none of cases 1, 2, or 3 apply, then leave the eOperator set to
119392** zero.  This term is not useful for search.
119393*/
119394static void exprAnalyzeOrTerm(
119395  SrcList *pSrc,            /* the FROM clause */
119396  WhereClause *pWC,         /* the complete WHERE clause */
119397  int idxTerm               /* Index of the OR-term to be analyzed */
119398){
119399  WhereInfo *pWInfo = pWC->pWInfo;        /* WHERE clause processing context */
119400  Parse *pParse = pWInfo->pParse;         /* Parser context */
119401  sqlite3 *db = pParse->db;               /* Database connection */
119402  WhereTerm *pTerm = &pWC->a[idxTerm];    /* The term to be analyzed */
119403  Expr *pExpr = pTerm->pExpr;             /* The expression of the term */
119404  int i;                                  /* Loop counters */
119405  WhereClause *pOrWc;       /* Breakup of pTerm into subterms */
119406  WhereTerm *pOrTerm;       /* A Sub-term within the pOrWc */
119407  WhereOrInfo *pOrInfo;     /* Additional information associated with pTerm */
119408  Bitmask chngToIN;         /* Tables that might satisfy case 1 */
119409  Bitmask indexable;        /* Tables that are indexable, satisfying case 2 */
119410
119411  /*
119412  ** Break the OR clause into its separate subterms.  The subterms are
119413  ** stored in a WhereClause structure containing within the WhereOrInfo
119414  ** object that is attached to the original OR clause term.
119415  */
119416  assert( (pTerm->wtFlags & (TERM_DYNAMIC|TERM_ORINFO|TERM_ANDINFO))==0 );
119417  assert( pExpr->op==TK_OR );
119418  pTerm->u.pOrInfo = pOrInfo = sqlite3DbMallocZero(db, sizeof(*pOrInfo));
119419  if( pOrInfo==0 ) return;
119420  pTerm->wtFlags |= TERM_ORINFO;
119421  pOrWc = &pOrInfo->wc;
119422  sqlite3WhereClauseInit(pOrWc, pWInfo);
119423  sqlite3WhereSplit(pOrWc, pExpr, TK_OR);
119424  sqlite3WhereExprAnalyze(pSrc, pOrWc);
119425  if( db->mallocFailed ) return;
119426  assert( pOrWc->nTerm>=2 );
119427
119428  /*
119429  ** Compute the set of tables that might satisfy cases 1 or 3.
119430  */
119431  indexable = ~(Bitmask)0;
119432  chngToIN = ~(Bitmask)0;
119433  for(i=pOrWc->nTerm-1, pOrTerm=pOrWc->a; i>=0 && indexable; i--, pOrTerm++){
119434    if( (pOrTerm->eOperator & WO_SINGLE)==0 ){
119435      WhereAndInfo *pAndInfo;
119436      assert( (pOrTerm->wtFlags & (TERM_ANDINFO|TERM_ORINFO))==0 );
119437      chngToIN = 0;
119438      pAndInfo = sqlite3DbMallocRaw(db, sizeof(*pAndInfo));
119439      if( pAndInfo ){
119440        WhereClause *pAndWC;
119441        WhereTerm *pAndTerm;
119442        int j;
119443        Bitmask b = 0;
119444        pOrTerm->u.pAndInfo = pAndInfo;
119445        pOrTerm->wtFlags |= TERM_ANDINFO;
119446        pOrTerm->eOperator = WO_AND;
119447        pAndWC = &pAndInfo->wc;
119448        sqlite3WhereClauseInit(pAndWC, pWC->pWInfo);
119449        sqlite3WhereSplit(pAndWC, pOrTerm->pExpr, TK_AND);
119450        sqlite3WhereExprAnalyze(pSrc, pAndWC);
119451        pAndWC->pOuter = pWC;
119452        testcase( db->mallocFailed );
119453        if( !db->mallocFailed ){
119454          for(j=0, pAndTerm=pAndWC->a; j<pAndWC->nTerm; j++, pAndTerm++){
119455            assert( pAndTerm->pExpr );
119456            if( allowedOp(pAndTerm->pExpr->op) ){
119457              b |= sqlite3WhereGetMask(&pWInfo->sMaskSet, pAndTerm->leftCursor);
119458            }
119459          }
119460        }
119461        indexable &= b;
119462      }
119463    }else if( pOrTerm->wtFlags & TERM_COPIED ){
119464      /* Skip this term for now.  We revisit it when we process the
119465      ** corresponding TERM_VIRTUAL term */
119466    }else{
119467      Bitmask b;
119468      b = sqlite3WhereGetMask(&pWInfo->sMaskSet, pOrTerm->leftCursor);
119469      if( pOrTerm->wtFlags & TERM_VIRTUAL ){
119470        WhereTerm *pOther = &pOrWc->a[pOrTerm->iParent];
119471        b |= sqlite3WhereGetMask(&pWInfo->sMaskSet, pOther->leftCursor);
119472      }
119473      indexable &= b;
119474      if( (pOrTerm->eOperator & WO_EQ)==0 ){
119475        chngToIN = 0;
119476      }else{
119477        chngToIN &= b;
119478      }
119479    }
119480  }
119481
119482  /*
119483  ** Record the set of tables that satisfy case 3.  The set might be
119484  ** empty.
119485  */
119486  pOrInfo->indexable = indexable;
119487  pTerm->eOperator = indexable==0 ? 0 : WO_OR;
119488
119489  /* For a two-way OR, attempt to implementation case 2.
119490  */
119491  if( indexable && pOrWc->nTerm==2 ){
119492    int iOne = 0;
119493    WhereTerm *pOne;
119494    while( (pOne = whereNthSubterm(&pOrWc->a[0],iOne++))!=0 ){
119495      int iTwo = 0;
119496      WhereTerm *pTwo;
119497      while( (pTwo = whereNthSubterm(&pOrWc->a[1],iTwo++))!=0 ){
119498        whereCombineDisjuncts(pSrc, pWC, pOne, pTwo);
119499      }
119500    }
119501  }
119502
119503  /*
119504  ** chngToIN holds a set of tables that *might* satisfy case 1.  But
119505  ** we have to do some additional checking to see if case 1 really
119506  ** is satisfied.
119507  **
119508  ** chngToIN will hold either 0, 1, or 2 bits.  The 0-bit case means
119509  ** that there is no possibility of transforming the OR clause into an
119510  ** IN operator because one or more terms in the OR clause contain
119511  ** something other than == on a column in the single table.  The 1-bit
119512  ** case means that every term of the OR clause is of the form
119513  ** "table.column=expr" for some single table.  The one bit that is set
119514  ** will correspond to the common table.  We still need to check to make
119515  ** sure the same column is used on all terms.  The 2-bit case is when
119516  ** the all terms are of the form "table1.column=table2.column".  It
119517  ** might be possible to form an IN operator with either table1.column
119518  ** or table2.column as the LHS if either is common to every term of
119519  ** the OR clause.
119520  **
119521  ** Note that terms of the form "table.column1=table.column2" (the
119522  ** same table on both sizes of the ==) cannot be optimized.
119523  */
119524  if( chngToIN ){
119525    int okToChngToIN = 0;     /* True if the conversion to IN is valid */
119526    int iColumn = -1;         /* Column index on lhs of IN operator */
119527    int iCursor = -1;         /* Table cursor common to all terms */
119528    int j = 0;                /* Loop counter */
119529
119530    /* Search for a table and column that appears on one side or the
119531    ** other of the == operator in every subterm.  That table and column
119532    ** will be recorded in iCursor and iColumn.  There might not be any
119533    ** such table and column.  Set okToChngToIN if an appropriate table
119534    ** and column is found but leave okToChngToIN false if not found.
119535    */
119536    for(j=0; j<2 && !okToChngToIN; j++){
119537      pOrTerm = pOrWc->a;
119538      for(i=pOrWc->nTerm-1; i>=0; i--, pOrTerm++){
119539        assert( pOrTerm->eOperator & WO_EQ );
119540        pOrTerm->wtFlags &= ~TERM_OR_OK;
119541        if( pOrTerm->leftCursor==iCursor ){
119542          /* This is the 2-bit case and we are on the second iteration and
119543          ** current term is from the first iteration.  So skip this term. */
119544          assert( j==1 );
119545          continue;
119546        }
119547        if( (chngToIN & sqlite3WhereGetMask(&pWInfo->sMaskSet,
119548                                            pOrTerm->leftCursor))==0 ){
119549          /* This term must be of the form t1.a==t2.b where t2 is in the
119550          ** chngToIN set but t1 is not.  This term will be either preceded
119551          ** or follwed by an inverted copy (t2.b==t1.a).  Skip this term
119552          ** and use its inversion. */
119553          testcase( pOrTerm->wtFlags & TERM_COPIED );
119554          testcase( pOrTerm->wtFlags & TERM_VIRTUAL );
119555          assert( pOrTerm->wtFlags & (TERM_COPIED|TERM_VIRTUAL) );
119556          continue;
119557        }
119558        iColumn = pOrTerm->u.leftColumn;
119559        iCursor = pOrTerm->leftCursor;
119560        break;
119561      }
119562      if( i<0 ){
119563        /* No candidate table+column was found.  This can only occur
119564        ** on the second iteration */
119565        assert( j==1 );
119566        assert( IsPowerOfTwo(chngToIN) );
119567        assert( chngToIN==sqlite3WhereGetMask(&pWInfo->sMaskSet, iCursor) );
119568        break;
119569      }
119570      testcase( j==1 );
119571
119572      /* We have found a candidate table and column.  Check to see if that
119573      ** table and column is common to every term in the OR clause */
119574      okToChngToIN = 1;
119575      for(; i>=0 && okToChngToIN; i--, pOrTerm++){
119576        assert( pOrTerm->eOperator & WO_EQ );
119577        if( pOrTerm->leftCursor!=iCursor ){
119578          pOrTerm->wtFlags &= ~TERM_OR_OK;
119579        }else if( pOrTerm->u.leftColumn!=iColumn ){
119580          okToChngToIN = 0;
119581        }else{
119582          int affLeft, affRight;
119583          /* If the right-hand side is also a column, then the affinities
119584          ** of both right and left sides must be such that no type
119585          ** conversions are required on the right.  (Ticket #2249)
119586          */
119587          affRight = sqlite3ExprAffinity(pOrTerm->pExpr->pRight);
119588          affLeft = sqlite3ExprAffinity(pOrTerm->pExpr->pLeft);
119589          if( affRight!=0 && affRight!=affLeft ){
119590            okToChngToIN = 0;
119591          }else{
119592            pOrTerm->wtFlags |= TERM_OR_OK;
119593          }
119594        }
119595      }
119596    }
119597
119598    /* At this point, okToChngToIN is true if original pTerm satisfies
119599    ** case 1.  In that case, construct a new virtual term that is
119600    ** pTerm converted into an IN operator.
119601    */
119602    if( okToChngToIN ){
119603      Expr *pDup;            /* A transient duplicate expression */
119604      ExprList *pList = 0;   /* The RHS of the IN operator */
119605      Expr *pLeft = 0;       /* The LHS of the IN operator */
119606      Expr *pNew;            /* The complete IN operator */
119607
119608      for(i=pOrWc->nTerm-1, pOrTerm=pOrWc->a; i>=0; i--, pOrTerm++){
119609        if( (pOrTerm->wtFlags & TERM_OR_OK)==0 ) continue;
119610        assert( pOrTerm->eOperator & WO_EQ );
119611        assert( pOrTerm->leftCursor==iCursor );
119612        assert( pOrTerm->u.leftColumn==iColumn );
119613        pDup = sqlite3ExprDup(db, pOrTerm->pExpr->pRight, 0);
119614        pList = sqlite3ExprListAppend(pWInfo->pParse, pList, pDup);
119615        pLeft = pOrTerm->pExpr->pLeft;
119616      }
119617      assert( pLeft!=0 );
119618      pDup = sqlite3ExprDup(db, pLeft, 0);
119619      pNew = sqlite3PExpr(pParse, TK_IN, pDup, 0, 0);
119620      if( pNew ){
119621        int idxNew;
119622        transferJoinMarkings(pNew, pExpr);
119623        assert( !ExprHasProperty(pNew, EP_xIsSelect) );
119624        pNew->x.pList = pList;
119625        idxNew = whereClauseInsert(pWC, pNew, TERM_VIRTUAL|TERM_DYNAMIC);
119626        testcase( idxNew==0 );
119627        exprAnalyze(pSrc, pWC, idxNew);
119628        pTerm = &pWC->a[idxTerm];
119629        markTermAsChild(pWC, idxNew, idxTerm);
119630      }else{
119631        sqlite3ExprListDelete(db, pList);
119632      }
119633      pTerm->eOperator = WO_NOOP;  /* case 1 trumps case 3 */
119634    }
119635  }
119636}
119637#endif /* !SQLITE_OMIT_OR_OPTIMIZATION && !SQLITE_OMIT_SUBQUERY */
119638
119639/*
119640** We already know that pExpr is a binary operator where both operands are
119641** column references.  This routine checks to see if pExpr is an equivalence
119642** relation:
119643**   1.  The SQLITE_Transitive optimization must be enabled
119644**   2.  Must be either an == or an IS operator
119645**   3.  Not originating in the ON clause of an OUTER JOIN
119646**   4.  The affinities of A and B must be compatible
119647**   5a. Both operands use the same collating sequence OR
119648**   5b. The overall collating sequence is BINARY
119649** If this routine returns TRUE, that means that the RHS can be substituted
119650** for the LHS anyplace else in the WHERE clause where the LHS column occurs.
119651** This is an optimization.  No harm comes from returning 0.  But if 1 is
119652** returned when it should not be, then incorrect answers might result.
119653*/
119654static int termIsEquivalence(Parse *pParse, Expr *pExpr){
119655  char aff1, aff2;
119656  CollSeq *pColl;
119657  const char *zColl1, *zColl2;
119658  if( !OptimizationEnabled(pParse->db, SQLITE_Transitive) ) return 0;
119659  if( pExpr->op!=TK_EQ && pExpr->op!=TK_IS ) return 0;
119660  if( ExprHasProperty(pExpr, EP_FromJoin) ) return 0;
119661  aff1 = sqlite3ExprAffinity(pExpr->pLeft);
119662  aff2 = sqlite3ExprAffinity(pExpr->pRight);
119663  if( aff1!=aff2
119664   && (!sqlite3IsNumericAffinity(aff1) || !sqlite3IsNumericAffinity(aff2))
119665  ){
119666    return 0;
119667  }
119668  pColl = sqlite3BinaryCompareCollSeq(pParse, pExpr->pLeft, pExpr->pRight);
119669  if( pColl==0 || sqlite3StrICmp(pColl->zName, "BINARY")==0 ) return 1;
119670  pColl = sqlite3ExprCollSeq(pParse, pExpr->pLeft);
119671  /* Since pLeft and pRight are both a column references, their collating
119672  ** sequence should always be defined. */
119673  zColl1 = ALWAYS(pColl) ? pColl->zName : 0;
119674  pColl = sqlite3ExprCollSeq(pParse, pExpr->pRight);
119675  zColl2 = ALWAYS(pColl) ? pColl->zName : 0;
119676  return sqlite3StrICmp(zColl1, zColl2)==0;
119677}
119678
119679/*
119680** Recursively walk the expressions of a SELECT statement and generate
119681** a bitmask indicating which tables are used in that expression
119682** tree.
119683*/
119684static Bitmask exprSelectUsage(WhereMaskSet *pMaskSet, Select *pS){
119685  Bitmask mask = 0;
119686  while( pS ){
119687    SrcList *pSrc = pS->pSrc;
119688    mask |= sqlite3WhereExprListUsage(pMaskSet, pS->pEList);
119689    mask |= sqlite3WhereExprListUsage(pMaskSet, pS->pGroupBy);
119690    mask |= sqlite3WhereExprListUsage(pMaskSet, pS->pOrderBy);
119691    mask |= sqlite3WhereExprUsage(pMaskSet, pS->pWhere);
119692    mask |= sqlite3WhereExprUsage(pMaskSet, pS->pHaving);
119693    if( ALWAYS(pSrc!=0) ){
119694      int i;
119695      for(i=0; i<pSrc->nSrc; i++){
119696        mask |= exprSelectUsage(pMaskSet, pSrc->a[i].pSelect);
119697        mask |= sqlite3WhereExprUsage(pMaskSet, pSrc->a[i].pOn);
119698      }
119699    }
119700    pS = pS->pPrior;
119701  }
119702  return mask;
119703}
119704
119705/*
119706** The input to this routine is an WhereTerm structure with only the
119707** "pExpr" field filled in.  The job of this routine is to analyze the
119708** subexpression and populate all the other fields of the WhereTerm
119709** structure.
119710**
119711** If the expression is of the form "<expr> <op> X" it gets commuted
119712** to the standard form of "X <op> <expr>".
119713**
119714** If the expression is of the form "X <op> Y" where both X and Y are
119715** columns, then the original expression is unchanged and a new virtual
119716** term of the form "Y <op> X" is added to the WHERE clause and
119717** analyzed separately.  The original term is marked with TERM_COPIED
119718** and the new term is marked with TERM_DYNAMIC (because it's pExpr
119719** needs to be freed with the WhereClause) and TERM_VIRTUAL (because it
119720** is a commuted copy of a prior term.)  The original term has nChild=1
119721** and the copy has idxParent set to the index of the original term.
119722*/
119723static void exprAnalyze(
119724  SrcList *pSrc,            /* the FROM clause */
119725  WhereClause *pWC,         /* the WHERE clause */
119726  int idxTerm               /* Index of the term to be analyzed */
119727){
119728  WhereInfo *pWInfo = pWC->pWInfo; /* WHERE clause processing context */
119729  WhereTerm *pTerm;                /* The term to be analyzed */
119730  WhereMaskSet *pMaskSet;          /* Set of table index masks */
119731  Expr *pExpr;                     /* The expression to be analyzed */
119732  Bitmask prereqLeft;              /* Prerequesites of the pExpr->pLeft */
119733  Bitmask prereqAll;               /* Prerequesites of pExpr */
119734  Bitmask extraRight = 0;          /* Extra dependencies on LEFT JOIN */
119735  Expr *pStr1 = 0;                 /* RHS of LIKE/GLOB operator */
119736  int isComplete = 0;              /* RHS of LIKE/GLOB ends with wildcard */
119737  int noCase = 0;                  /* uppercase equivalent to lowercase */
119738  int op;                          /* Top-level operator.  pExpr->op */
119739  Parse *pParse = pWInfo->pParse;  /* Parsing context */
119740  sqlite3 *db = pParse->db;        /* Database connection */
119741
119742  if( db->mallocFailed ){
119743    return;
119744  }
119745  pTerm = &pWC->a[idxTerm];
119746  pMaskSet = &pWInfo->sMaskSet;
119747  pExpr = pTerm->pExpr;
119748  assert( pExpr->op!=TK_AS && pExpr->op!=TK_COLLATE );
119749  prereqLeft = sqlite3WhereExprUsage(pMaskSet, pExpr->pLeft);
119750  op = pExpr->op;
119751  if( op==TK_IN ){
119752    assert( pExpr->pRight==0 );
119753    if( ExprHasProperty(pExpr, EP_xIsSelect) ){
119754      pTerm->prereqRight = exprSelectUsage(pMaskSet, pExpr->x.pSelect);
119755    }else{
119756      pTerm->prereqRight = sqlite3WhereExprListUsage(pMaskSet, pExpr->x.pList);
119757    }
119758  }else if( op==TK_ISNULL ){
119759    pTerm->prereqRight = 0;
119760  }else{
119761    pTerm->prereqRight = sqlite3WhereExprUsage(pMaskSet, pExpr->pRight);
119762  }
119763  prereqAll = sqlite3WhereExprUsage(pMaskSet, pExpr);
119764  if( ExprHasProperty(pExpr, EP_FromJoin) ){
119765    Bitmask x = sqlite3WhereGetMask(pMaskSet, pExpr->iRightJoinTable);
119766    prereqAll |= x;
119767    extraRight = x-1;  /* ON clause terms may not be used with an index
119768                       ** on left table of a LEFT JOIN.  Ticket #3015 */
119769  }
119770  pTerm->prereqAll = prereqAll;
119771  pTerm->leftCursor = -1;
119772  pTerm->iParent = -1;
119773  pTerm->eOperator = 0;
119774  if( allowedOp(op) ){
119775    Expr *pLeft = sqlite3ExprSkipCollate(pExpr->pLeft);
119776    Expr *pRight = sqlite3ExprSkipCollate(pExpr->pRight);
119777    u16 opMask = (pTerm->prereqRight & prereqLeft)==0 ? WO_ALL : WO_EQUIV;
119778    if( pLeft->op==TK_COLUMN ){
119779      pTerm->leftCursor = pLeft->iTable;
119780      pTerm->u.leftColumn = pLeft->iColumn;
119781      pTerm->eOperator = operatorMask(op) & opMask;
119782    }
119783    if( op==TK_IS ) pTerm->wtFlags |= TERM_IS;
119784    if( pRight && pRight->op==TK_COLUMN ){
119785      WhereTerm *pNew;
119786      Expr *pDup;
119787      u16 eExtraOp = 0;        /* Extra bits for pNew->eOperator */
119788      if( pTerm->leftCursor>=0 ){
119789        int idxNew;
119790        pDup = sqlite3ExprDup(db, pExpr, 0);
119791        if( db->mallocFailed ){
119792          sqlite3ExprDelete(db, pDup);
119793          return;
119794        }
119795        idxNew = whereClauseInsert(pWC, pDup, TERM_VIRTUAL|TERM_DYNAMIC);
119796        if( idxNew==0 ) return;
119797        pNew = &pWC->a[idxNew];
119798        markTermAsChild(pWC, idxNew, idxTerm);
119799        if( op==TK_IS ) pNew->wtFlags |= TERM_IS;
119800        pTerm = &pWC->a[idxTerm];
119801        pTerm->wtFlags |= TERM_COPIED;
119802
119803        if( termIsEquivalence(pParse, pDup) ){
119804          pTerm->eOperator |= WO_EQUIV;
119805          eExtraOp = WO_EQUIV;
119806        }
119807      }else{
119808        pDup = pExpr;
119809        pNew = pTerm;
119810      }
119811      exprCommute(pParse, pDup);
119812      pLeft = sqlite3ExprSkipCollate(pDup->pLeft);
119813      pNew->leftCursor = pLeft->iTable;
119814      pNew->u.leftColumn = pLeft->iColumn;
119815      testcase( (prereqLeft | extraRight) != prereqLeft );
119816      pNew->prereqRight = prereqLeft | extraRight;
119817      pNew->prereqAll = prereqAll;
119818      pNew->eOperator = (operatorMask(pDup->op) + eExtraOp) & opMask;
119819    }
119820  }
119821
119822#ifndef SQLITE_OMIT_BETWEEN_OPTIMIZATION
119823  /* If a term is the BETWEEN operator, create two new virtual terms
119824  ** that define the range that the BETWEEN implements.  For example:
119825  **
119826  **      a BETWEEN b AND c
119827  **
119828  ** is converted into:
119829  **
119830  **      (a BETWEEN b AND c) AND (a>=b) AND (a<=c)
119831  **
119832  ** The two new terms are added onto the end of the WhereClause object.
119833  ** The new terms are "dynamic" and are children of the original BETWEEN
119834  ** term.  That means that if the BETWEEN term is coded, the children are
119835  ** skipped.  Or, if the children are satisfied by an index, the original
119836  ** BETWEEN term is skipped.
119837  */
119838  else if( pExpr->op==TK_BETWEEN && pWC->op==TK_AND ){
119839    ExprList *pList = pExpr->x.pList;
119840    int i;
119841    static const u8 ops[] = {TK_GE, TK_LE};
119842    assert( pList!=0 );
119843    assert( pList->nExpr==2 );
119844    for(i=0; i<2; i++){
119845      Expr *pNewExpr;
119846      int idxNew;
119847      pNewExpr = sqlite3PExpr(pParse, ops[i],
119848                             sqlite3ExprDup(db, pExpr->pLeft, 0),
119849                             sqlite3ExprDup(db, pList->a[i].pExpr, 0), 0);
119850      transferJoinMarkings(pNewExpr, pExpr);
119851      idxNew = whereClauseInsert(pWC, pNewExpr, TERM_VIRTUAL|TERM_DYNAMIC);
119852      testcase( idxNew==0 );
119853      exprAnalyze(pSrc, pWC, idxNew);
119854      pTerm = &pWC->a[idxTerm];
119855      markTermAsChild(pWC, idxNew, idxTerm);
119856    }
119857  }
119858#endif /* SQLITE_OMIT_BETWEEN_OPTIMIZATION */
119859
119860#if !defined(SQLITE_OMIT_OR_OPTIMIZATION) && !defined(SQLITE_OMIT_SUBQUERY)
119861  /* Analyze a term that is composed of two or more subterms connected by
119862  ** an OR operator.
119863  */
119864  else if( pExpr->op==TK_OR ){
119865    assert( pWC->op==TK_AND );
119866    exprAnalyzeOrTerm(pSrc, pWC, idxTerm);
119867    pTerm = &pWC->a[idxTerm];
119868  }
119869#endif /* SQLITE_OMIT_OR_OPTIMIZATION */
119870
119871#ifndef SQLITE_OMIT_LIKE_OPTIMIZATION
119872  /* Add constraints to reduce the search space on a LIKE or GLOB
119873  ** operator.
119874  **
119875  ** A like pattern of the form "x LIKE 'aBc%'" is changed into constraints
119876  **
119877  **          x>='ABC' AND x<'abd' AND x LIKE 'aBc%'
119878  **
119879  ** The last character of the prefix "abc" is incremented to form the
119880  ** termination condition "abd".  If case is not significant (the default
119881  ** for LIKE) then the lower-bound is made all uppercase and the upper-
119882  ** bound is made all lowercase so that the bounds also work when comparing
119883  ** BLOBs.
119884  */
119885  if( pWC->op==TK_AND
119886   && isLikeOrGlob(pParse, pExpr, &pStr1, &isComplete, &noCase)
119887  ){
119888    Expr *pLeft;       /* LHS of LIKE/GLOB operator */
119889    Expr *pStr2;       /* Copy of pStr1 - RHS of LIKE/GLOB operator */
119890    Expr *pNewExpr1;
119891    Expr *pNewExpr2;
119892    int idxNew1;
119893    int idxNew2;
119894    const char *zCollSeqName;     /* Name of collating sequence */
119895    const u16 wtFlags = TERM_LIKEOPT | TERM_VIRTUAL | TERM_DYNAMIC;
119896
119897    pLeft = pExpr->x.pList->a[1].pExpr;
119898    pStr2 = sqlite3ExprDup(db, pStr1, 0);
119899
119900    /* Convert the lower bound to upper-case and the upper bound to
119901    ** lower-case (upper-case is less than lower-case in ASCII) so that
119902    ** the range constraints also work for BLOBs
119903    */
119904    if( noCase && !pParse->db->mallocFailed ){
119905      int i;
119906      char c;
119907      pTerm->wtFlags |= TERM_LIKE;
119908      for(i=0; (c = pStr1->u.zToken[i])!=0; i++){
119909        pStr1->u.zToken[i] = sqlite3Toupper(c);
119910        pStr2->u.zToken[i] = sqlite3Tolower(c);
119911      }
119912    }
119913
119914    if( !db->mallocFailed ){
119915      u8 c, *pC;       /* Last character before the first wildcard */
119916      pC = (u8*)&pStr2->u.zToken[sqlite3Strlen30(pStr2->u.zToken)-1];
119917      c = *pC;
119918      if( noCase ){
119919        /* The point is to increment the last character before the first
119920        ** wildcard.  But if we increment '@', that will push it into the
119921        ** alphabetic range where case conversions will mess up the
119922        ** inequality.  To avoid this, make sure to also run the full
119923        ** LIKE on all candidate expressions by clearing the isComplete flag
119924        */
119925        if( c=='A'-1 ) isComplete = 0;
119926        c = sqlite3UpperToLower[c];
119927      }
119928      *pC = c + 1;
119929    }
119930    zCollSeqName = noCase ? "NOCASE" : "BINARY";
119931    pNewExpr1 = sqlite3ExprDup(db, pLeft, 0);
119932    pNewExpr1 = sqlite3PExpr(pParse, TK_GE,
119933           sqlite3ExprAddCollateString(pParse,pNewExpr1,zCollSeqName),
119934           pStr1, 0);
119935    transferJoinMarkings(pNewExpr1, pExpr);
119936    idxNew1 = whereClauseInsert(pWC, pNewExpr1, wtFlags);
119937    testcase( idxNew1==0 );
119938    exprAnalyze(pSrc, pWC, idxNew1);
119939    pNewExpr2 = sqlite3ExprDup(db, pLeft, 0);
119940    pNewExpr2 = sqlite3PExpr(pParse, TK_LT,
119941           sqlite3ExprAddCollateString(pParse,pNewExpr2,zCollSeqName),
119942           pStr2, 0);
119943    transferJoinMarkings(pNewExpr2, pExpr);
119944    idxNew2 = whereClauseInsert(pWC, pNewExpr2, wtFlags);
119945    testcase( idxNew2==0 );
119946    exprAnalyze(pSrc, pWC, idxNew2);
119947    pTerm = &pWC->a[idxTerm];
119948    if( isComplete ){
119949      markTermAsChild(pWC, idxNew1, idxTerm);
119950      markTermAsChild(pWC, idxNew2, idxTerm);
119951    }
119952  }
119953#endif /* SQLITE_OMIT_LIKE_OPTIMIZATION */
119954
119955#ifndef SQLITE_OMIT_VIRTUALTABLE
119956  /* Add a WO_MATCH auxiliary term to the constraint set if the
119957  ** current expression is of the form:  column MATCH expr.
119958  ** This information is used by the xBestIndex methods of
119959  ** virtual tables.  The native query optimizer does not attempt
119960  ** to do anything with MATCH functions.
119961  */
119962  if( isMatchOfColumn(pExpr) ){
119963    int idxNew;
119964    Expr *pRight, *pLeft;
119965    WhereTerm *pNewTerm;
119966    Bitmask prereqColumn, prereqExpr;
119967
119968    pRight = pExpr->x.pList->a[0].pExpr;
119969    pLeft = pExpr->x.pList->a[1].pExpr;
119970    prereqExpr = sqlite3WhereExprUsage(pMaskSet, pRight);
119971    prereqColumn = sqlite3WhereExprUsage(pMaskSet, pLeft);
119972    if( (prereqExpr & prereqColumn)==0 ){
119973      Expr *pNewExpr;
119974      pNewExpr = sqlite3PExpr(pParse, TK_MATCH,
119975                              0, sqlite3ExprDup(db, pRight, 0), 0);
119976      idxNew = whereClauseInsert(pWC, pNewExpr, TERM_VIRTUAL|TERM_DYNAMIC);
119977      testcase( idxNew==0 );
119978      pNewTerm = &pWC->a[idxNew];
119979      pNewTerm->prereqRight = prereqExpr;
119980      pNewTerm->leftCursor = pLeft->iTable;
119981      pNewTerm->u.leftColumn = pLeft->iColumn;
119982      pNewTerm->eOperator = WO_MATCH;
119983      markTermAsChild(pWC, idxNew, idxTerm);
119984      pTerm = &pWC->a[idxTerm];
119985      pTerm->wtFlags |= TERM_COPIED;
119986      pNewTerm->prereqAll = pTerm->prereqAll;
119987    }
119988  }
119989#endif /* SQLITE_OMIT_VIRTUALTABLE */
119990
119991#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
119992  /* When sqlite_stat3 histogram data is available an operator of the
119993  ** form "x IS NOT NULL" can sometimes be evaluated more efficiently
119994  ** as "x>NULL" if x is not an INTEGER PRIMARY KEY.  So construct a
119995  ** virtual term of that form.
119996  **
119997  ** Note that the virtual term must be tagged with TERM_VNULL.
119998  */
119999  if( pExpr->op==TK_NOTNULL
120000   && pExpr->pLeft->op==TK_COLUMN
120001   && pExpr->pLeft->iColumn>=0
120002   && OptimizationEnabled(db, SQLITE_Stat34)
120003  ){
120004    Expr *pNewExpr;
120005    Expr *pLeft = pExpr->pLeft;
120006    int idxNew;
120007    WhereTerm *pNewTerm;
120008
120009    pNewExpr = sqlite3PExpr(pParse, TK_GT,
120010                            sqlite3ExprDup(db, pLeft, 0),
120011                            sqlite3PExpr(pParse, TK_NULL, 0, 0, 0), 0);
120012
120013    idxNew = whereClauseInsert(pWC, pNewExpr,
120014                              TERM_VIRTUAL|TERM_DYNAMIC|TERM_VNULL);
120015    if( idxNew ){
120016      pNewTerm = &pWC->a[idxNew];
120017      pNewTerm->prereqRight = 0;
120018      pNewTerm->leftCursor = pLeft->iTable;
120019      pNewTerm->u.leftColumn = pLeft->iColumn;
120020      pNewTerm->eOperator = WO_GT;
120021      markTermAsChild(pWC, idxNew, idxTerm);
120022      pTerm = &pWC->a[idxTerm];
120023      pTerm->wtFlags |= TERM_COPIED;
120024      pNewTerm->prereqAll = pTerm->prereqAll;
120025    }
120026  }
120027#endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */
120028
120029  /* Prevent ON clause terms of a LEFT JOIN from being used to drive
120030  ** an index for tables to the left of the join.
120031  */
120032  pTerm->prereqRight |= extraRight;
120033}
120034
120035/***************************************************************************
120036** Routines with file scope above.  Interface to the rest of the where.c
120037** subsystem follows.
120038***************************************************************************/
120039
120040/*
120041** This routine identifies subexpressions in the WHERE clause where
120042** each subexpression is separated by the AND operator or some other
120043** operator specified in the op parameter.  The WhereClause structure
120044** is filled with pointers to subexpressions.  For example:
120045**
120046**    WHERE  a=='hello' AND coalesce(b,11)<10 AND (c+12!=d OR c==22)
120047**           \________/     \_______________/     \________________/
120048**            slot[0]            slot[1]               slot[2]
120049**
120050** The original WHERE clause in pExpr is unaltered.  All this routine
120051** does is make slot[] entries point to substructure within pExpr.
120052**
120053** In the previous sentence and in the diagram, "slot[]" refers to
120054** the WhereClause.a[] array.  The slot[] array grows as needed to contain
120055** all terms of the WHERE clause.
120056*/
120057SQLITE_PRIVATE void sqlite3WhereSplit(WhereClause *pWC, Expr *pExpr, u8 op){
120058  Expr *pE2 = sqlite3ExprSkipCollate(pExpr);
120059  pWC->op = op;
120060  if( pE2==0 ) return;
120061  if( pE2->op!=op ){
120062    whereClauseInsert(pWC, pExpr, 0);
120063  }else{
120064    sqlite3WhereSplit(pWC, pE2->pLeft, op);
120065    sqlite3WhereSplit(pWC, pE2->pRight, op);
120066  }
120067}
120068
120069/*
120070** Initialize a preallocated WhereClause structure.
120071*/
120072SQLITE_PRIVATE void sqlite3WhereClauseInit(
120073  WhereClause *pWC,        /* The WhereClause to be initialized */
120074  WhereInfo *pWInfo        /* The WHERE processing context */
120075){
120076  pWC->pWInfo = pWInfo;
120077  pWC->pOuter = 0;
120078  pWC->nTerm = 0;
120079  pWC->nSlot = ArraySize(pWC->aStatic);
120080  pWC->a = pWC->aStatic;
120081}
120082
120083/*
120084** Deallocate a WhereClause structure.  The WhereClause structure
120085** itself is not freed.  This routine is the inverse of sqlite3WhereClauseInit().
120086*/
120087SQLITE_PRIVATE void sqlite3WhereClauseClear(WhereClause *pWC){
120088  int i;
120089  WhereTerm *a;
120090  sqlite3 *db = pWC->pWInfo->pParse->db;
120091  for(i=pWC->nTerm-1, a=pWC->a; i>=0; i--, a++){
120092    if( a->wtFlags & TERM_DYNAMIC ){
120093      sqlite3ExprDelete(db, a->pExpr);
120094    }
120095    if( a->wtFlags & TERM_ORINFO ){
120096      whereOrInfoDelete(db, a->u.pOrInfo);
120097    }else if( a->wtFlags & TERM_ANDINFO ){
120098      whereAndInfoDelete(db, a->u.pAndInfo);
120099    }
120100  }
120101  if( pWC->a!=pWC->aStatic ){
120102    sqlite3DbFree(db, pWC->a);
120103  }
120104}
120105
120106
120107/*
120108** These routines walk (recursively) an expression tree and generate
120109** a bitmask indicating which tables are used in that expression
120110** tree.
120111*/
120112SQLITE_PRIVATE Bitmask sqlite3WhereExprUsage(WhereMaskSet *pMaskSet, Expr *p){
120113  Bitmask mask = 0;
120114  if( p==0 ) return 0;
120115  if( p->op==TK_COLUMN ){
120116    mask = sqlite3WhereGetMask(pMaskSet, p->iTable);
120117    return mask;
120118  }
120119  mask = sqlite3WhereExprUsage(pMaskSet, p->pRight);
120120  mask |= sqlite3WhereExprUsage(pMaskSet, p->pLeft);
120121  if( ExprHasProperty(p, EP_xIsSelect) ){
120122    mask |= exprSelectUsage(pMaskSet, p->x.pSelect);
120123  }else{
120124    mask |= sqlite3WhereExprListUsage(pMaskSet, p->x.pList);
120125  }
120126  return mask;
120127}
120128SQLITE_PRIVATE Bitmask sqlite3WhereExprListUsage(WhereMaskSet *pMaskSet, ExprList *pList){
120129  int i;
120130  Bitmask mask = 0;
120131  if( pList ){
120132    for(i=0; i<pList->nExpr; i++){
120133      mask |= sqlite3WhereExprUsage(pMaskSet, pList->a[i].pExpr);
120134    }
120135  }
120136  return mask;
120137}
120138
120139
120140/*
120141** Call exprAnalyze on all terms in a WHERE clause.
120142**
120143** Note that exprAnalyze() might add new virtual terms onto the
120144** end of the WHERE clause.  We do not want to analyze these new
120145** virtual terms, so start analyzing at the end and work forward
120146** so that the added virtual terms are never processed.
120147*/
120148SQLITE_PRIVATE void sqlite3WhereExprAnalyze(
120149  SrcList *pTabList,       /* the FROM clause */
120150  WhereClause *pWC         /* the WHERE clause to be analyzed */
120151){
120152  int i;
120153  for(i=pWC->nTerm-1; i>=0; i--){
120154    exprAnalyze(pTabList, pWC, i);
120155  }
120156}
120157
120158/************** End of whereexpr.c *******************************************/
120159/************** Begin file where.c *******************************************/
120160/*
120161** 2001 September 15
120162**
120163** The author disclaims copyright to this source code.  In place of
120164** a legal notice, here is a blessing:
120165**
120166**    May you do good and not evil.
120167**    May you find forgiveness for yourself and forgive others.
120168**    May you share freely, never taking more than you give.
120169**
120170*************************************************************************
120171** This module contains C code that generates VDBE code used to process
120172** the WHERE clause of SQL statements.  This module is responsible for
120173** generating the code that loops through a table looking for applicable
120174** rows.  Indices are selected and used to speed the search when doing
120175** so is applicable.  Because this module is responsible for selecting
120176** indices, you might also think of this module as the "query optimizer".
120177*/
120178/* #include "sqliteInt.h" */
120179/* #include "whereInt.h" */
120180
120181/* Forward declaration of methods */
120182static int whereLoopResize(sqlite3*, WhereLoop*, int);
120183
120184/* Test variable that can be set to enable WHERE tracing */
120185#if defined(SQLITE_TEST) || defined(SQLITE_DEBUG)
120186/***/ int sqlite3WhereTrace = 0;
120187#endif
120188
120189
120190/*
120191** Return the estimated number of output rows from a WHERE clause
120192*/
120193SQLITE_PRIVATE u64 sqlite3WhereOutputRowCount(WhereInfo *pWInfo){
120194  return sqlite3LogEstToInt(pWInfo->nRowOut);
120195}
120196
120197/*
120198** Return one of the WHERE_DISTINCT_xxxxx values to indicate how this
120199** WHERE clause returns outputs for DISTINCT processing.
120200*/
120201SQLITE_PRIVATE int sqlite3WhereIsDistinct(WhereInfo *pWInfo){
120202  return pWInfo->eDistinct;
120203}
120204
120205/*
120206** Return TRUE if the WHERE clause returns rows in ORDER BY order.
120207** Return FALSE if the output needs to be sorted.
120208*/
120209SQLITE_PRIVATE int sqlite3WhereIsOrdered(WhereInfo *pWInfo){
120210  return pWInfo->nOBSat;
120211}
120212
120213/*
120214** Return the VDBE address or label to jump to in order to continue
120215** immediately with the next row of a WHERE clause.
120216*/
120217SQLITE_PRIVATE int sqlite3WhereContinueLabel(WhereInfo *pWInfo){
120218  assert( pWInfo->iContinue!=0 );
120219  return pWInfo->iContinue;
120220}
120221
120222/*
120223** Return the VDBE address or label to jump to in order to break
120224** out of a WHERE loop.
120225*/
120226SQLITE_PRIVATE int sqlite3WhereBreakLabel(WhereInfo *pWInfo){
120227  return pWInfo->iBreak;
120228}
120229
120230/*
120231** Return TRUE if an UPDATE or DELETE statement can operate directly on
120232** the rowids returned by a WHERE clause.  Return FALSE if doing an
120233** UPDATE or DELETE might change subsequent WHERE clause results.
120234**
120235** If the ONEPASS optimization is used (if this routine returns true)
120236** then also write the indices of open cursors used by ONEPASS
120237** into aiCur[0] and aiCur[1].  iaCur[0] gets the cursor of the data
120238** table and iaCur[1] gets the cursor used by an auxiliary index.
120239** Either value may be -1, indicating that cursor is not used.
120240** Any cursors returned will have been opened for writing.
120241**
120242** aiCur[0] and aiCur[1] both get -1 if the where-clause logic is
120243** unable to use the ONEPASS optimization.
120244*/
120245SQLITE_PRIVATE int sqlite3WhereOkOnePass(WhereInfo *pWInfo, int *aiCur){
120246  memcpy(aiCur, pWInfo->aiCurOnePass, sizeof(int)*2);
120247  return pWInfo->okOnePass;
120248}
120249
120250/*
120251** Move the content of pSrc into pDest
120252*/
120253static void whereOrMove(WhereOrSet *pDest, WhereOrSet *pSrc){
120254  pDest->n = pSrc->n;
120255  memcpy(pDest->a, pSrc->a, pDest->n*sizeof(pDest->a[0]));
120256}
120257
120258/*
120259** Try to insert a new prerequisite/cost entry into the WhereOrSet pSet.
120260**
120261** The new entry might overwrite an existing entry, or it might be
120262** appended, or it might be discarded.  Do whatever is the right thing
120263** so that pSet keeps the N_OR_COST best entries seen so far.
120264*/
120265static int whereOrInsert(
120266  WhereOrSet *pSet,      /* The WhereOrSet to be updated */
120267  Bitmask prereq,        /* Prerequisites of the new entry */
120268  LogEst rRun,           /* Run-cost of the new entry */
120269  LogEst nOut            /* Number of outputs for the new entry */
120270){
120271  u16 i;
120272  WhereOrCost *p;
120273  for(i=pSet->n, p=pSet->a; i>0; i--, p++){
120274    if( rRun<=p->rRun && (prereq & p->prereq)==prereq ){
120275      goto whereOrInsert_done;
120276    }
120277    if( p->rRun<=rRun && (p->prereq & prereq)==p->prereq ){
120278      return 0;
120279    }
120280  }
120281  if( pSet->n<N_OR_COST ){
120282    p = &pSet->a[pSet->n++];
120283    p->nOut = nOut;
120284  }else{
120285    p = pSet->a;
120286    for(i=1; i<pSet->n; i++){
120287      if( p->rRun>pSet->a[i].rRun ) p = pSet->a + i;
120288    }
120289    if( p->rRun<=rRun ) return 0;
120290  }
120291whereOrInsert_done:
120292  p->prereq = prereq;
120293  p->rRun = rRun;
120294  if( p->nOut>nOut ) p->nOut = nOut;
120295  return 1;
120296}
120297
120298/*
120299** Return the bitmask for the given cursor number.  Return 0 if
120300** iCursor is not in the set.
120301*/
120302SQLITE_PRIVATE Bitmask sqlite3WhereGetMask(WhereMaskSet *pMaskSet, int iCursor){
120303  int i;
120304  assert( pMaskSet->n<=(int)sizeof(Bitmask)*8 );
120305  for(i=0; i<pMaskSet->n; i++){
120306    if( pMaskSet->ix[i]==iCursor ){
120307      return MASKBIT(i);
120308    }
120309  }
120310  return 0;
120311}
120312
120313/*
120314** Create a new mask for cursor iCursor.
120315**
120316** There is one cursor per table in the FROM clause.  The number of
120317** tables in the FROM clause is limited by a test early in the
120318** sqlite3WhereBegin() routine.  So we know that the pMaskSet->ix[]
120319** array will never overflow.
120320*/
120321static void createMask(WhereMaskSet *pMaskSet, int iCursor){
120322  assert( pMaskSet->n < ArraySize(pMaskSet->ix) );
120323  pMaskSet->ix[pMaskSet->n++] = iCursor;
120324}
120325
120326/*
120327** Advance to the next WhereTerm that matches according to the criteria
120328** established when the pScan object was initialized by whereScanInit().
120329** Return NULL if there are no more matching WhereTerms.
120330*/
120331static WhereTerm *whereScanNext(WhereScan *pScan){
120332  int iCur;            /* The cursor on the LHS of the term */
120333  int iColumn;         /* The column on the LHS of the term.  -1 for IPK */
120334  Expr *pX;            /* An expression being tested */
120335  WhereClause *pWC;    /* Shorthand for pScan->pWC */
120336  WhereTerm *pTerm;    /* The term being tested */
120337  int k = pScan->k;    /* Where to start scanning */
120338
120339  while( pScan->iEquiv<=pScan->nEquiv ){
120340    iCur = pScan->aEquiv[pScan->iEquiv-2];
120341    iColumn = pScan->aEquiv[pScan->iEquiv-1];
120342    while( (pWC = pScan->pWC)!=0 ){
120343      for(pTerm=pWC->a+k; k<pWC->nTerm; k++, pTerm++){
120344        if( pTerm->leftCursor==iCur
120345         && pTerm->u.leftColumn==iColumn
120346         && (pScan->iEquiv<=2 || !ExprHasProperty(pTerm->pExpr, EP_FromJoin))
120347        ){
120348          if( (pTerm->eOperator & WO_EQUIV)!=0
120349           && pScan->nEquiv<ArraySize(pScan->aEquiv)
120350          ){
120351            int j;
120352            pX = sqlite3ExprSkipCollate(pTerm->pExpr->pRight);
120353            assert( pX->op==TK_COLUMN );
120354            for(j=0; j<pScan->nEquiv; j+=2){
120355              if( pScan->aEquiv[j]==pX->iTable
120356               && pScan->aEquiv[j+1]==pX->iColumn ){
120357                  break;
120358              }
120359            }
120360            if( j==pScan->nEquiv ){
120361              pScan->aEquiv[j] = pX->iTable;
120362              pScan->aEquiv[j+1] = pX->iColumn;
120363              pScan->nEquiv += 2;
120364            }
120365          }
120366          if( (pTerm->eOperator & pScan->opMask)!=0 ){
120367            /* Verify the affinity and collating sequence match */
120368            if( pScan->zCollName && (pTerm->eOperator & WO_ISNULL)==0 ){
120369              CollSeq *pColl;
120370              Parse *pParse = pWC->pWInfo->pParse;
120371              pX = pTerm->pExpr;
120372              if( !sqlite3IndexAffinityOk(pX, pScan->idxaff) ){
120373                continue;
120374              }
120375              assert(pX->pLeft);
120376              pColl = sqlite3BinaryCompareCollSeq(pParse,
120377                                                  pX->pLeft, pX->pRight);
120378              if( pColl==0 ) pColl = pParse->db->pDfltColl;
120379              if( sqlite3StrICmp(pColl->zName, pScan->zCollName) ){
120380                continue;
120381              }
120382            }
120383            if( (pTerm->eOperator & (WO_EQ|WO_IS))!=0
120384             && (pX = pTerm->pExpr->pRight)->op==TK_COLUMN
120385             && pX->iTable==pScan->aEquiv[0]
120386             && pX->iColumn==pScan->aEquiv[1]
120387            ){
120388              testcase( pTerm->eOperator & WO_IS );
120389              continue;
120390            }
120391            pScan->k = k+1;
120392            return pTerm;
120393          }
120394        }
120395      }
120396      pScan->pWC = pScan->pWC->pOuter;
120397      k = 0;
120398    }
120399    pScan->pWC = pScan->pOrigWC;
120400    k = 0;
120401    pScan->iEquiv += 2;
120402  }
120403  return 0;
120404}
120405
120406/*
120407** Initialize a WHERE clause scanner object.  Return a pointer to the
120408** first match.  Return NULL if there are no matches.
120409**
120410** The scanner will be searching the WHERE clause pWC.  It will look
120411** for terms of the form "X <op> <expr>" where X is column iColumn of table
120412** iCur.  The <op> must be one of the operators described by opMask.
120413**
120414** If the search is for X and the WHERE clause contains terms of the
120415** form X=Y then this routine might also return terms of the form
120416** "Y <op> <expr>".  The number of levels of transitivity is limited,
120417** but is enough to handle most commonly occurring SQL statements.
120418**
120419** If X is not the INTEGER PRIMARY KEY then X must be compatible with
120420** index pIdx.
120421*/
120422static WhereTerm *whereScanInit(
120423  WhereScan *pScan,       /* The WhereScan object being initialized */
120424  WhereClause *pWC,       /* The WHERE clause to be scanned */
120425  int iCur,               /* Cursor to scan for */
120426  int iColumn,            /* Column to scan for */
120427  u32 opMask,             /* Operator(s) to scan for */
120428  Index *pIdx             /* Must be compatible with this index */
120429){
120430  int j;
120431
120432  /* memset(pScan, 0, sizeof(*pScan)); */
120433  pScan->pOrigWC = pWC;
120434  pScan->pWC = pWC;
120435  if( pIdx && iColumn>=0 ){
120436    pScan->idxaff = pIdx->pTable->aCol[iColumn].affinity;
120437    for(j=0; pIdx->aiColumn[j]!=iColumn; j++){
120438      if( NEVER(j>pIdx->nColumn) ) return 0;
120439    }
120440    pScan->zCollName = pIdx->azColl[j];
120441  }else{
120442    pScan->idxaff = 0;
120443    pScan->zCollName = 0;
120444  }
120445  pScan->opMask = opMask;
120446  pScan->k = 0;
120447  pScan->aEquiv[0] = iCur;
120448  pScan->aEquiv[1] = iColumn;
120449  pScan->nEquiv = 2;
120450  pScan->iEquiv = 2;
120451  return whereScanNext(pScan);
120452}
120453
120454/*
120455** Search for a term in the WHERE clause that is of the form "X <op> <expr>"
120456** where X is a reference to the iColumn of table iCur and <op> is one of
120457** the WO_xx operator codes specified by the op parameter.
120458** Return a pointer to the term.  Return 0 if not found.
120459**
120460** The term returned might by Y=<expr> if there is another constraint in
120461** the WHERE clause that specifies that X=Y.  Any such constraints will be
120462** identified by the WO_EQUIV bit in the pTerm->eOperator field.  The
120463** aEquiv[] array holds X and all its equivalents, with each SQL variable
120464** taking up two slots in aEquiv[].  The first slot is for the cursor number
120465** and the second is for the column number.  There are 22 slots in aEquiv[]
120466** so that means we can look for X plus up to 10 other equivalent values.
120467** Hence a search for X will return <expr> if X=A1 and A1=A2 and A2=A3
120468** and ... and A9=A10 and A10=<expr>.
120469**
120470** If there are multiple terms in the WHERE clause of the form "X <op> <expr>"
120471** then try for the one with no dependencies on <expr> - in other words where
120472** <expr> is a constant expression of some kind.  Only return entries of
120473** the form "X <op> Y" where Y is a column in another table if no terms of
120474** the form "X <op> <const-expr>" exist.   If no terms with a constant RHS
120475** exist, try to return a term that does not use WO_EQUIV.
120476*/
120477SQLITE_PRIVATE WhereTerm *sqlite3WhereFindTerm(
120478  WhereClause *pWC,     /* The WHERE clause to be searched */
120479  int iCur,             /* Cursor number of LHS */
120480  int iColumn,          /* Column number of LHS */
120481  Bitmask notReady,     /* RHS must not overlap with this mask */
120482  u32 op,               /* Mask of WO_xx values describing operator */
120483  Index *pIdx           /* Must be compatible with this index, if not NULL */
120484){
120485  WhereTerm *pResult = 0;
120486  WhereTerm *p;
120487  WhereScan scan;
120488
120489  p = whereScanInit(&scan, pWC, iCur, iColumn, op, pIdx);
120490  op &= WO_EQ|WO_IS;
120491  while( p ){
120492    if( (p->prereqRight & notReady)==0 ){
120493      if( p->prereqRight==0 && (p->eOperator&op)!=0 ){
120494        testcase( p->eOperator & WO_IS );
120495        return p;
120496      }
120497      if( pResult==0 ) pResult = p;
120498    }
120499    p = whereScanNext(&scan);
120500  }
120501  return pResult;
120502}
120503
120504/*
120505** This function searches pList for an entry that matches the iCol-th column
120506** of index pIdx.
120507**
120508** If such an expression is found, its index in pList->a[] is returned. If
120509** no expression is found, -1 is returned.
120510*/
120511static int findIndexCol(
120512  Parse *pParse,                  /* Parse context */
120513  ExprList *pList,                /* Expression list to search */
120514  int iBase,                      /* Cursor for table associated with pIdx */
120515  Index *pIdx,                    /* Index to match column of */
120516  int iCol                        /* Column of index to match */
120517){
120518  int i;
120519  const char *zColl = pIdx->azColl[iCol];
120520
120521  for(i=0; i<pList->nExpr; i++){
120522    Expr *p = sqlite3ExprSkipCollate(pList->a[i].pExpr);
120523    if( p->op==TK_COLUMN
120524     && p->iColumn==pIdx->aiColumn[iCol]
120525     && p->iTable==iBase
120526    ){
120527      CollSeq *pColl = sqlite3ExprCollSeq(pParse, pList->a[i].pExpr);
120528      if( pColl && 0==sqlite3StrICmp(pColl->zName, zColl) ){
120529        return i;
120530      }
120531    }
120532  }
120533
120534  return -1;
120535}
120536
120537/*
120538** Return true if the DISTINCT expression-list passed as the third argument
120539** is redundant.
120540**
120541** A DISTINCT list is redundant if any subset of the columns in the
120542** DISTINCT list are collectively unique and individually non-null.
120543*/
120544static int isDistinctRedundant(
120545  Parse *pParse,            /* Parsing context */
120546  SrcList *pTabList,        /* The FROM clause */
120547  WhereClause *pWC,         /* The WHERE clause */
120548  ExprList *pDistinct       /* The result set that needs to be DISTINCT */
120549){
120550  Table *pTab;
120551  Index *pIdx;
120552  int i;
120553  int iBase;
120554
120555  /* If there is more than one table or sub-select in the FROM clause of
120556  ** this query, then it will not be possible to show that the DISTINCT
120557  ** clause is redundant. */
120558  if( pTabList->nSrc!=1 ) return 0;
120559  iBase = pTabList->a[0].iCursor;
120560  pTab = pTabList->a[0].pTab;
120561
120562  /* If any of the expressions is an IPK column on table iBase, then return
120563  ** true. Note: The (p->iTable==iBase) part of this test may be false if the
120564  ** current SELECT is a correlated sub-query.
120565  */
120566  for(i=0; i<pDistinct->nExpr; i++){
120567    Expr *p = sqlite3ExprSkipCollate(pDistinct->a[i].pExpr);
120568    if( p->op==TK_COLUMN && p->iTable==iBase && p->iColumn<0 ) return 1;
120569  }
120570
120571  /* Loop through all indices on the table, checking each to see if it makes
120572  ** the DISTINCT qualifier redundant. It does so if:
120573  **
120574  **   1. The index is itself UNIQUE, and
120575  **
120576  **   2. All of the columns in the index are either part of the pDistinct
120577  **      list, or else the WHERE clause contains a term of the form "col=X",
120578  **      where X is a constant value. The collation sequences of the
120579  **      comparison and select-list expressions must match those of the index.
120580  **
120581  **   3. All of those index columns for which the WHERE clause does not
120582  **      contain a "col=X" term are subject to a NOT NULL constraint.
120583  */
120584  for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
120585    if( !IsUniqueIndex(pIdx) ) continue;
120586    for(i=0; i<pIdx->nKeyCol; i++){
120587      i16 iCol = pIdx->aiColumn[i];
120588      if( 0==sqlite3WhereFindTerm(pWC, iBase, iCol, ~(Bitmask)0, WO_EQ, pIdx) ){
120589        int iIdxCol = findIndexCol(pParse, pDistinct, iBase, pIdx, i);
120590        if( iIdxCol<0 || pTab->aCol[iCol].notNull==0 ){
120591          break;
120592        }
120593      }
120594    }
120595    if( i==pIdx->nKeyCol ){
120596      /* This index implies that the DISTINCT qualifier is redundant. */
120597      return 1;
120598    }
120599  }
120600
120601  return 0;
120602}
120603
120604
120605/*
120606** Estimate the logarithm of the input value to base 2.
120607*/
120608static LogEst estLog(LogEst N){
120609  return N<=10 ? 0 : sqlite3LogEst(N) - 33;
120610}
120611
120612/*
120613** Convert OP_Column opcodes to OP_Copy in previously generated code.
120614**
120615** This routine runs over generated VDBE code and translates OP_Column
120616** opcodes into OP_Copy, and OP_Rowid into OP_Null, when the table is being
120617** accessed via co-routine instead of via table lookup.
120618*/
120619static void translateColumnToCopy(
120620  Vdbe *v,            /* The VDBE containing code to translate */
120621  int iStart,         /* Translate from this opcode to the end */
120622  int iTabCur,        /* OP_Column/OP_Rowid references to this table */
120623  int iRegister       /* The first column is in this register */
120624){
120625  VdbeOp *pOp = sqlite3VdbeGetOp(v, iStart);
120626  int iEnd = sqlite3VdbeCurrentAddr(v);
120627  for(; iStart<iEnd; iStart++, pOp++){
120628    if( pOp->p1!=iTabCur ) continue;
120629    if( pOp->opcode==OP_Column ){
120630      pOp->opcode = OP_Copy;
120631      pOp->p1 = pOp->p2 + iRegister;
120632      pOp->p2 = pOp->p3;
120633      pOp->p3 = 0;
120634    }else if( pOp->opcode==OP_Rowid ){
120635      pOp->opcode = OP_Null;
120636      pOp->p1 = 0;
120637      pOp->p3 = 0;
120638    }
120639  }
120640}
120641
120642/*
120643** Two routines for printing the content of an sqlite3_index_info
120644** structure.  Used for testing and debugging only.  If neither
120645** SQLITE_TEST or SQLITE_DEBUG are defined, then these routines
120646** are no-ops.
120647*/
120648#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(WHERETRACE_ENABLED)
120649static void TRACE_IDX_INPUTS(sqlite3_index_info *p){
120650  int i;
120651  if( !sqlite3WhereTrace ) return;
120652  for(i=0; i<p->nConstraint; i++){
120653    sqlite3DebugPrintf("  constraint[%d]: col=%d termid=%d op=%d usabled=%d\n",
120654       i,
120655       p->aConstraint[i].iColumn,
120656       p->aConstraint[i].iTermOffset,
120657       p->aConstraint[i].op,
120658       p->aConstraint[i].usable);
120659  }
120660  for(i=0; i<p->nOrderBy; i++){
120661    sqlite3DebugPrintf("  orderby[%d]: col=%d desc=%d\n",
120662       i,
120663       p->aOrderBy[i].iColumn,
120664       p->aOrderBy[i].desc);
120665  }
120666}
120667static void TRACE_IDX_OUTPUTS(sqlite3_index_info *p){
120668  int i;
120669  if( !sqlite3WhereTrace ) return;
120670  for(i=0; i<p->nConstraint; i++){
120671    sqlite3DebugPrintf("  usage[%d]: argvIdx=%d omit=%d\n",
120672       i,
120673       p->aConstraintUsage[i].argvIndex,
120674       p->aConstraintUsage[i].omit);
120675  }
120676  sqlite3DebugPrintf("  idxNum=%d\n", p->idxNum);
120677  sqlite3DebugPrintf("  idxStr=%s\n", p->idxStr);
120678  sqlite3DebugPrintf("  orderByConsumed=%d\n", p->orderByConsumed);
120679  sqlite3DebugPrintf("  estimatedCost=%g\n", p->estimatedCost);
120680  sqlite3DebugPrintf("  estimatedRows=%lld\n", p->estimatedRows);
120681}
120682#else
120683#define TRACE_IDX_INPUTS(A)
120684#define TRACE_IDX_OUTPUTS(A)
120685#endif
120686
120687#ifndef SQLITE_OMIT_AUTOMATIC_INDEX
120688/*
120689** Return TRUE if the WHERE clause term pTerm is of a form where it
120690** could be used with an index to access pSrc, assuming an appropriate
120691** index existed.
120692*/
120693static int termCanDriveIndex(
120694  WhereTerm *pTerm,              /* WHERE clause term to check */
120695  struct SrcList_item *pSrc,     /* Table we are trying to access */
120696  Bitmask notReady               /* Tables in outer loops of the join */
120697){
120698  char aff;
120699  if( pTerm->leftCursor!=pSrc->iCursor ) return 0;
120700  if( (pTerm->eOperator & (WO_EQ|WO_IS))==0 ) return 0;
120701  if( (pTerm->prereqRight & notReady)!=0 ) return 0;
120702  if( pTerm->u.leftColumn<0 ) return 0;
120703  aff = pSrc->pTab->aCol[pTerm->u.leftColumn].affinity;
120704  if( !sqlite3IndexAffinityOk(pTerm->pExpr, aff) ) return 0;
120705  testcase( pTerm->pExpr->op==TK_IS );
120706  return 1;
120707}
120708#endif
120709
120710
120711#ifndef SQLITE_OMIT_AUTOMATIC_INDEX
120712/*
120713** Generate code to construct the Index object for an automatic index
120714** and to set up the WhereLevel object pLevel so that the code generator
120715** makes use of the automatic index.
120716*/
120717static void constructAutomaticIndex(
120718  Parse *pParse,              /* The parsing context */
120719  WhereClause *pWC,           /* The WHERE clause */
120720  struct SrcList_item *pSrc,  /* The FROM clause term to get the next index */
120721  Bitmask notReady,           /* Mask of cursors that are not available */
120722  WhereLevel *pLevel          /* Write new index here */
120723){
120724  int nKeyCol;                /* Number of columns in the constructed index */
120725  WhereTerm *pTerm;           /* A single term of the WHERE clause */
120726  WhereTerm *pWCEnd;          /* End of pWC->a[] */
120727  Index *pIdx;                /* Object describing the transient index */
120728  Vdbe *v;                    /* Prepared statement under construction */
120729  int addrInit;               /* Address of the initialization bypass jump */
120730  Table *pTable;              /* The table being indexed */
120731  int addrTop;                /* Top of the index fill loop */
120732  int regRecord;              /* Register holding an index record */
120733  int n;                      /* Column counter */
120734  int i;                      /* Loop counter */
120735  int mxBitCol;               /* Maximum column in pSrc->colUsed */
120736  CollSeq *pColl;             /* Collating sequence to on a column */
120737  WhereLoop *pLoop;           /* The Loop object */
120738  char *zNotUsed;             /* Extra space on the end of pIdx */
120739  Bitmask idxCols;            /* Bitmap of columns used for indexing */
120740  Bitmask extraCols;          /* Bitmap of additional columns */
120741  u8 sentWarning = 0;         /* True if a warnning has been issued */
120742  Expr *pPartial = 0;         /* Partial Index Expression */
120743  int iContinue = 0;          /* Jump here to skip excluded rows */
120744  struct SrcList_item *pTabItem;  /* FROM clause term being indexed */
120745
120746  /* Generate code to skip over the creation and initialization of the
120747  ** transient index on 2nd and subsequent iterations of the loop. */
120748  v = pParse->pVdbe;
120749  assert( v!=0 );
120750  addrInit = sqlite3CodeOnce(pParse); VdbeCoverage(v);
120751
120752  /* Count the number of columns that will be added to the index
120753  ** and used to match WHERE clause constraints */
120754  nKeyCol = 0;
120755  pTable = pSrc->pTab;
120756  pWCEnd = &pWC->a[pWC->nTerm];
120757  pLoop = pLevel->pWLoop;
120758  idxCols = 0;
120759  for(pTerm=pWC->a; pTerm<pWCEnd; pTerm++){
120760    Expr *pExpr = pTerm->pExpr;
120761    assert( !ExprHasProperty(pExpr, EP_FromJoin)    /* prereq always non-zero */
120762         || pExpr->iRightJoinTable!=pSrc->iCursor   /*   for the right-hand   */
120763         || pLoop->prereq!=0 );                     /*   table of a LEFT JOIN */
120764    if( pLoop->prereq==0
120765     && (pTerm->wtFlags & TERM_VIRTUAL)==0
120766     && !ExprHasProperty(pExpr, EP_FromJoin)
120767     && sqlite3ExprIsTableConstant(pExpr, pSrc->iCursor) ){
120768      pPartial = sqlite3ExprAnd(pParse->db, pPartial,
120769                                sqlite3ExprDup(pParse->db, pExpr, 0));
120770    }
120771    if( termCanDriveIndex(pTerm, pSrc, notReady) ){
120772      int iCol = pTerm->u.leftColumn;
120773      Bitmask cMask = iCol>=BMS ? MASKBIT(BMS-1) : MASKBIT(iCol);
120774      testcase( iCol==BMS );
120775      testcase( iCol==BMS-1 );
120776      if( !sentWarning ){
120777        sqlite3_log(SQLITE_WARNING_AUTOINDEX,
120778            "automatic index on %s(%s)", pTable->zName,
120779            pTable->aCol[iCol].zName);
120780        sentWarning = 1;
120781      }
120782      if( (idxCols & cMask)==0 ){
120783        if( whereLoopResize(pParse->db, pLoop, nKeyCol+1) ){
120784          goto end_auto_index_create;
120785        }
120786        pLoop->aLTerm[nKeyCol++] = pTerm;
120787        idxCols |= cMask;
120788      }
120789    }
120790  }
120791  assert( nKeyCol>0 );
120792  pLoop->u.btree.nEq = pLoop->nLTerm = nKeyCol;
120793  pLoop->wsFlags = WHERE_COLUMN_EQ | WHERE_IDX_ONLY | WHERE_INDEXED
120794                     | WHERE_AUTO_INDEX;
120795
120796  /* Count the number of additional columns needed to create a
120797  ** covering index.  A "covering index" is an index that contains all
120798  ** columns that are needed by the query.  With a covering index, the
120799  ** original table never needs to be accessed.  Automatic indices must
120800  ** be a covering index because the index will not be updated if the
120801  ** original table changes and the index and table cannot both be used
120802  ** if they go out of sync.
120803  */
120804  extraCols = pSrc->colUsed & (~idxCols | MASKBIT(BMS-1));
120805  mxBitCol = MIN(BMS-1,pTable->nCol);
120806  testcase( pTable->nCol==BMS-1 );
120807  testcase( pTable->nCol==BMS-2 );
120808  for(i=0; i<mxBitCol; i++){
120809    if( extraCols & MASKBIT(i) ) nKeyCol++;
120810  }
120811  if( pSrc->colUsed & MASKBIT(BMS-1) ){
120812    nKeyCol += pTable->nCol - BMS + 1;
120813  }
120814
120815  /* Construct the Index object to describe this index */
120816  pIdx = sqlite3AllocateIndexObject(pParse->db, nKeyCol+1, 0, &zNotUsed);
120817  if( pIdx==0 ) goto end_auto_index_create;
120818  pLoop->u.btree.pIndex = pIdx;
120819  pIdx->zName = "auto-index";
120820  pIdx->pTable = pTable;
120821  n = 0;
120822  idxCols = 0;
120823  for(pTerm=pWC->a; pTerm<pWCEnd; pTerm++){
120824    if( termCanDriveIndex(pTerm, pSrc, notReady) ){
120825      int iCol = pTerm->u.leftColumn;
120826      Bitmask cMask = iCol>=BMS ? MASKBIT(BMS-1) : MASKBIT(iCol);
120827      testcase( iCol==BMS-1 );
120828      testcase( iCol==BMS );
120829      if( (idxCols & cMask)==0 ){
120830        Expr *pX = pTerm->pExpr;
120831        idxCols |= cMask;
120832        pIdx->aiColumn[n] = pTerm->u.leftColumn;
120833        pColl = sqlite3BinaryCompareCollSeq(pParse, pX->pLeft, pX->pRight);
120834        pIdx->azColl[n] = pColl ? pColl->zName : "BINARY";
120835        n++;
120836      }
120837    }
120838  }
120839  assert( (u32)n==pLoop->u.btree.nEq );
120840
120841  /* Add additional columns needed to make the automatic index into
120842  ** a covering index */
120843  for(i=0; i<mxBitCol; i++){
120844    if( extraCols & MASKBIT(i) ){
120845      pIdx->aiColumn[n] = i;
120846      pIdx->azColl[n] = "BINARY";
120847      n++;
120848    }
120849  }
120850  if( pSrc->colUsed & MASKBIT(BMS-1) ){
120851    for(i=BMS-1; i<pTable->nCol; i++){
120852      pIdx->aiColumn[n] = i;
120853      pIdx->azColl[n] = "BINARY";
120854      n++;
120855    }
120856  }
120857  assert( n==nKeyCol );
120858  pIdx->aiColumn[n] = -1;
120859  pIdx->azColl[n] = "BINARY";
120860
120861  /* Create the automatic index */
120862  assert( pLevel->iIdxCur>=0 );
120863  pLevel->iIdxCur = pParse->nTab++;
120864  sqlite3VdbeAddOp2(v, OP_OpenAutoindex, pLevel->iIdxCur, nKeyCol+1);
120865  sqlite3VdbeSetP4KeyInfo(pParse, pIdx);
120866  VdbeComment((v, "for %s", pTable->zName));
120867
120868  /* Fill the automatic index with content */
120869  sqlite3ExprCachePush(pParse);
120870  pTabItem = &pWC->pWInfo->pTabList->a[pLevel->iFrom];
120871  if( pTabItem->viaCoroutine ){
120872    int regYield = pTabItem->regReturn;
120873    sqlite3VdbeAddOp3(v, OP_InitCoroutine, regYield, 0, pTabItem->addrFillSub);
120874    addrTop =  sqlite3VdbeAddOp1(v, OP_Yield, regYield);
120875    VdbeCoverage(v);
120876    VdbeComment((v, "next row of \"%s\"", pTabItem->pTab->zName));
120877  }else{
120878    addrTop = sqlite3VdbeAddOp1(v, OP_Rewind, pLevel->iTabCur); VdbeCoverage(v);
120879  }
120880  if( pPartial ){
120881    iContinue = sqlite3VdbeMakeLabel(v);
120882    sqlite3ExprIfFalse(pParse, pPartial, iContinue, SQLITE_JUMPIFNULL);
120883    pLoop->wsFlags |= WHERE_PARTIALIDX;
120884  }
120885  regRecord = sqlite3GetTempReg(pParse);
120886  sqlite3GenerateIndexKey(pParse, pIdx, pLevel->iTabCur, regRecord, 0, 0, 0, 0);
120887  sqlite3VdbeAddOp2(v, OP_IdxInsert, pLevel->iIdxCur, regRecord);
120888  sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT);
120889  if( pPartial ) sqlite3VdbeResolveLabel(v, iContinue);
120890  if( pTabItem->viaCoroutine ){
120891    translateColumnToCopy(v, addrTop, pLevel->iTabCur, pTabItem->regResult);
120892    sqlite3VdbeAddOp2(v, OP_Goto, 0, addrTop);
120893    pTabItem->viaCoroutine = 0;
120894  }else{
120895    sqlite3VdbeAddOp2(v, OP_Next, pLevel->iTabCur, addrTop+1); VdbeCoverage(v);
120896  }
120897  sqlite3VdbeChangeP5(v, SQLITE_STMTSTATUS_AUTOINDEX);
120898  sqlite3VdbeJumpHere(v, addrTop);
120899  sqlite3ReleaseTempReg(pParse, regRecord);
120900  sqlite3ExprCachePop(pParse);
120901
120902  /* Jump here when skipping the initialization */
120903  sqlite3VdbeJumpHere(v, addrInit);
120904
120905end_auto_index_create:
120906  sqlite3ExprDelete(pParse->db, pPartial);
120907}
120908#endif /* SQLITE_OMIT_AUTOMATIC_INDEX */
120909
120910#ifndef SQLITE_OMIT_VIRTUALTABLE
120911/*
120912** Allocate and populate an sqlite3_index_info structure. It is the
120913** responsibility of the caller to eventually release the structure
120914** by passing the pointer returned by this function to sqlite3_free().
120915*/
120916static sqlite3_index_info *allocateIndexInfo(
120917  Parse *pParse,
120918  WhereClause *pWC,
120919  Bitmask mUnusable,              /* Ignore terms with these prereqs */
120920  struct SrcList_item *pSrc,
120921  ExprList *pOrderBy
120922){
120923  int i, j;
120924  int nTerm;
120925  struct sqlite3_index_constraint *pIdxCons;
120926  struct sqlite3_index_orderby *pIdxOrderBy;
120927  struct sqlite3_index_constraint_usage *pUsage;
120928  WhereTerm *pTerm;
120929  int nOrderBy;
120930  sqlite3_index_info *pIdxInfo;
120931
120932  /* Count the number of possible WHERE clause constraints referring
120933  ** to this virtual table */
120934  for(i=nTerm=0, pTerm=pWC->a; i<pWC->nTerm; i++, pTerm++){
120935    if( pTerm->leftCursor != pSrc->iCursor ) continue;
120936    if( pTerm->prereqRight & mUnusable ) continue;
120937    assert( IsPowerOfTwo(pTerm->eOperator & ~WO_EQUIV) );
120938    testcase( pTerm->eOperator & WO_IN );
120939    testcase( pTerm->eOperator & WO_ISNULL );
120940    testcase( pTerm->eOperator & WO_IS );
120941    testcase( pTerm->eOperator & WO_ALL );
120942    if( (pTerm->eOperator & ~(WO_ISNULL|WO_EQUIV|WO_IS))==0 ) continue;
120943    if( pTerm->wtFlags & TERM_VNULL ) continue;
120944    nTerm++;
120945  }
120946
120947  /* If the ORDER BY clause contains only columns in the current
120948  ** virtual table then allocate space for the aOrderBy part of
120949  ** the sqlite3_index_info structure.
120950  */
120951  nOrderBy = 0;
120952  if( pOrderBy ){
120953    int n = pOrderBy->nExpr;
120954    for(i=0; i<n; i++){
120955      Expr *pExpr = pOrderBy->a[i].pExpr;
120956      if( pExpr->op!=TK_COLUMN || pExpr->iTable!=pSrc->iCursor ) break;
120957    }
120958    if( i==n){
120959      nOrderBy = n;
120960    }
120961  }
120962
120963  /* Allocate the sqlite3_index_info structure
120964  */
120965  pIdxInfo = sqlite3DbMallocZero(pParse->db, sizeof(*pIdxInfo)
120966                           + (sizeof(*pIdxCons) + sizeof(*pUsage))*nTerm
120967                           + sizeof(*pIdxOrderBy)*nOrderBy );
120968  if( pIdxInfo==0 ){
120969    sqlite3ErrorMsg(pParse, "out of memory");
120970    return 0;
120971  }
120972
120973  /* Initialize the structure.  The sqlite3_index_info structure contains
120974  ** many fields that are declared "const" to prevent xBestIndex from
120975  ** changing them.  We have to do some funky casting in order to
120976  ** initialize those fields.
120977  */
120978  pIdxCons = (struct sqlite3_index_constraint*)&pIdxInfo[1];
120979  pIdxOrderBy = (struct sqlite3_index_orderby*)&pIdxCons[nTerm];
120980  pUsage = (struct sqlite3_index_constraint_usage*)&pIdxOrderBy[nOrderBy];
120981  *(int*)&pIdxInfo->nConstraint = nTerm;
120982  *(int*)&pIdxInfo->nOrderBy = nOrderBy;
120983  *(struct sqlite3_index_constraint**)&pIdxInfo->aConstraint = pIdxCons;
120984  *(struct sqlite3_index_orderby**)&pIdxInfo->aOrderBy = pIdxOrderBy;
120985  *(struct sqlite3_index_constraint_usage**)&pIdxInfo->aConstraintUsage =
120986                                                                   pUsage;
120987
120988  for(i=j=0, pTerm=pWC->a; i<pWC->nTerm; i++, pTerm++){
120989    u8 op;
120990    if( pTerm->leftCursor != pSrc->iCursor ) continue;
120991    if( pTerm->prereqRight & mUnusable ) continue;
120992    assert( IsPowerOfTwo(pTerm->eOperator & ~WO_EQUIV) );
120993    testcase( pTerm->eOperator & WO_IN );
120994    testcase( pTerm->eOperator & WO_IS );
120995    testcase( pTerm->eOperator & WO_ISNULL );
120996    testcase( pTerm->eOperator & WO_ALL );
120997    if( (pTerm->eOperator & ~(WO_ISNULL|WO_EQUIV|WO_IS))==0 ) continue;
120998    if( pTerm->wtFlags & TERM_VNULL ) continue;
120999    pIdxCons[j].iColumn = pTerm->u.leftColumn;
121000    pIdxCons[j].iTermOffset = i;
121001    op = (u8)pTerm->eOperator & WO_ALL;
121002    if( op==WO_IN ) op = WO_EQ;
121003    pIdxCons[j].op = op;
121004    /* The direct assignment in the previous line is possible only because
121005    ** the WO_ and SQLITE_INDEX_CONSTRAINT_ codes are identical.  The
121006    ** following asserts verify this fact. */
121007    assert( WO_EQ==SQLITE_INDEX_CONSTRAINT_EQ );
121008    assert( WO_LT==SQLITE_INDEX_CONSTRAINT_LT );
121009    assert( WO_LE==SQLITE_INDEX_CONSTRAINT_LE );
121010    assert( WO_GT==SQLITE_INDEX_CONSTRAINT_GT );
121011    assert( WO_GE==SQLITE_INDEX_CONSTRAINT_GE );
121012    assert( WO_MATCH==SQLITE_INDEX_CONSTRAINT_MATCH );
121013    assert( pTerm->eOperator & (WO_IN|WO_EQ|WO_LT|WO_LE|WO_GT|WO_GE|WO_MATCH) );
121014    j++;
121015  }
121016  for(i=0; i<nOrderBy; i++){
121017    Expr *pExpr = pOrderBy->a[i].pExpr;
121018    pIdxOrderBy[i].iColumn = pExpr->iColumn;
121019    pIdxOrderBy[i].desc = pOrderBy->a[i].sortOrder;
121020  }
121021
121022  return pIdxInfo;
121023}
121024
121025/*
121026** The table object reference passed as the second argument to this function
121027** must represent a virtual table. This function invokes the xBestIndex()
121028** method of the virtual table with the sqlite3_index_info object that
121029** comes in as the 3rd argument to this function.
121030**
121031** If an error occurs, pParse is populated with an error message and a
121032** non-zero value is returned. Otherwise, 0 is returned and the output
121033** part of the sqlite3_index_info structure is left populated.
121034**
121035** Whether or not an error is returned, it is the responsibility of the
121036** caller to eventually free p->idxStr if p->needToFreeIdxStr indicates
121037** that this is required.
121038*/
121039static int vtabBestIndex(Parse *pParse, Table *pTab, sqlite3_index_info *p){
121040  sqlite3_vtab *pVtab = sqlite3GetVTable(pParse->db, pTab)->pVtab;
121041  int i;
121042  int rc;
121043
121044  TRACE_IDX_INPUTS(p);
121045  rc = pVtab->pModule->xBestIndex(pVtab, p);
121046  TRACE_IDX_OUTPUTS(p);
121047
121048  if( rc!=SQLITE_OK ){
121049    if( rc==SQLITE_NOMEM ){
121050      pParse->db->mallocFailed = 1;
121051    }else if( !pVtab->zErrMsg ){
121052      sqlite3ErrorMsg(pParse, "%s", sqlite3ErrStr(rc));
121053    }else{
121054      sqlite3ErrorMsg(pParse, "%s", pVtab->zErrMsg);
121055    }
121056  }
121057  sqlite3_free(pVtab->zErrMsg);
121058  pVtab->zErrMsg = 0;
121059
121060  for(i=0; i<p->nConstraint; i++){
121061    if( !p->aConstraint[i].usable && p->aConstraintUsage[i].argvIndex>0 ){
121062      sqlite3ErrorMsg(pParse,
121063          "table %s: xBestIndex returned an invalid plan", pTab->zName);
121064    }
121065  }
121066
121067  return pParse->nErr;
121068}
121069#endif /* !defined(SQLITE_OMIT_VIRTUALTABLE) */
121070
121071#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
121072/*
121073** Estimate the location of a particular key among all keys in an
121074** index.  Store the results in aStat as follows:
121075**
121076**    aStat[0]      Est. number of rows less than pRec
121077**    aStat[1]      Est. number of rows equal to pRec
121078**
121079** Return the index of the sample that is the smallest sample that
121080** is greater than or equal to pRec. Note that this index is not an index
121081** into the aSample[] array - it is an index into a virtual set of samples
121082** based on the contents of aSample[] and the number of fields in record
121083** pRec.
121084*/
121085static int whereKeyStats(
121086  Parse *pParse,              /* Database connection */
121087  Index *pIdx,                /* Index to consider domain of */
121088  UnpackedRecord *pRec,       /* Vector of values to consider */
121089  int roundUp,                /* Round up if true.  Round down if false */
121090  tRowcnt *aStat              /* OUT: stats written here */
121091){
121092  IndexSample *aSample = pIdx->aSample;
121093  int iCol;                   /* Index of required stats in anEq[] etc. */
121094  int i;                      /* Index of first sample >= pRec */
121095  int iSample;                /* Smallest sample larger than or equal to pRec */
121096  int iMin = 0;               /* Smallest sample not yet tested */
121097  int iTest;                  /* Next sample to test */
121098  int res;                    /* Result of comparison operation */
121099  int nField;                 /* Number of fields in pRec */
121100  tRowcnt iLower = 0;         /* anLt[] + anEq[] of largest sample pRec is > */
121101
121102#ifndef SQLITE_DEBUG
121103  UNUSED_PARAMETER( pParse );
121104#endif
121105  assert( pRec!=0 );
121106  assert( pIdx->nSample>0 );
121107  assert( pRec->nField>0 && pRec->nField<=pIdx->nSampleCol );
121108
121109  /* Do a binary search to find the first sample greater than or equal
121110  ** to pRec. If pRec contains a single field, the set of samples to search
121111  ** is simply the aSample[] array. If the samples in aSample[] contain more
121112  ** than one fields, all fields following the first are ignored.
121113  **
121114  ** If pRec contains N fields, where N is more than one, then as well as the
121115  ** samples in aSample[] (truncated to N fields), the search also has to
121116  ** consider prefixes of those samples. For example, if the set of samples
121117  ** in aSample is:
121118  **
121119  **     aSample[0] = (a, 5)
121120  **     aSample[1] = (a, 10)
121121  **     aSample[2] = (b, 5)
121122  **     aSample[3] = (c, 100)
121123  **     aSample[4] = (c, 105)
121124  **
121125  ** Then the search space should ideally be the samples above and the
121126  ** unique prefixes [a], [b] and [c]. But since that is hard to organize,
121127  ** the code actually searches this set:
121128  **
121129  **     0: (a)
121130  **     1: (a, 5)
121131  **     2: (a, 10)
121132  **     3: (a, 10)
121133  **     4: (b)
121134  **     5: (b, 5)
121135  **     6: (c)
121136  **     7: (c, 100)
121137  **     8: (c, 105)
121138  **     9: (c, 105)
121139  **
121140  ** For each sample in the aSample[] array, N samples are present in the
121141  ** effective sample array. In the above, samples 0 and 1 are based on
121142  ** sample aSample[0]. Samples 2 and 3 on aSample[1] etc.
121143  **
121144  ** Often, sample i of each block of N effective samples has (i+1) fields.
121145  ** Except, each sample may be extended to ensure that it is greater than or
121146  ** equal to the previous sample in the array. For example, in the above,
121147  ** sample 2 is the first sample of a block of N samples, so at first it
121148  ** appears that it should be 1 field in size. However, that would make it
121149  ** smaller than sample 1, so the binary search would not work. As a result,
121150  ** it is extended to two fields. The duplicates that this creates do not
121151  ** cause any problems.
121152  */
121153  nField = pRec->nField;
121154  iCol = 0;
121155  iSample = pIdx->nSample * nField;
121156  do{
121157    int iSamp;                    /* Index in aSample[] of test sample */
121158    int n;                        /* Number of fields in test sample */
121159
121160    iTest = (iMin+iSample)/2;
121161    iSamp = iTest / nField;
121162    if( iSamp>0 ){
121163      /* The proposed effective sample is a prefix of sample aSample[iSamp].
121164      ** Specifically, the shortest prefix of at least (1 + iTest%nField)
121165      ** fields that is greater than the previous effective sample.  */
121166      for(n=(iTest % nField) + 1; n<nField; n++){
121167        if( aSample[iSamp-1].anLt[n-1]!=aSample[iSamp].anLt[n-1] ) break;
121168      }
121169    }else{
121170      n = iTest + 1;
121171    }
121172
121173    pRec->nField = n;
121174    res = sqlite3VdbeRecordCompare(aSample[iSamp].n, aSample[iSamp].p, pRec);
121175    if( res<0 ){
121176      iLower = aSample[iSamp].anLt[n-1] + aSample[iSamp].anEq[n-1];
121177      iMin = iTest+1;
121178    }else if( res==0 && n<nField ){
121179      iLower = aSample[iSamp].anLt[n-1];
121180      iMin = iTest+1;
121181      res = -1;
121182    }else{
121183      iSample = iTest;
121184      iCol = n-1;
121185    }
121186  }while( res && iMin<iSample );
121187  i = iSample / nField;
121188
121189#ifdef SQLITE_DEBUG
121190  /* The following assert statements check that the binary search code
121191  ** above found the right answer. This block serves no purpose other
121192  ** than to invoke the asserts.  */
121193  if( pParse->db->mallocFailed==0 ){
121194    if( res==0 ){
121195      /* If (res==0) is true, then pRec must be equal to sample i. */
121196      assert( i<pIdx->nSample );
121197      assert( iCol==nField-1 );
121198      pRec->nField = nField;
121199      assert( 0==sqlite3VdbeRecordCompare(aSample[i].n, aSample[i].p, pRec)
121200           || pParse->db->mallocFailed
121201      );
121202    }else{
121203      /* Unless i==pIdx->nSample, indicating that pRec is larger than
121204      ** all samples in the aSample[] array, pRec must be smaller than the
121205      ** (iCol+1) field prefix of sample i.  */
121206      assert( i<=pIdx->nSample && i>=0 );
121207      pRec->nField = iCol+1;
121208      assert( i==pIdx->nSample
121209           || sqlite3VdbeRecordCompare(aSample[i].n, aSample[i].p, pRec)>0
121210           || pParse->db->mallocFailed );
121211
121212      /* if i==0 and iCol==0, then record pRec is smaller than all samples
121213      ** in the aSample[] array. Otherwise, if (iCol>0) then pRec must
121214      ** be greater than or equal to the (iCol) field prefix of sample i.
121215      ** If (i>0), then pRec must also be greater than sample (i-1).  */
121216      if( iCol>0 ){
121217        pRec->nField = iCol;
121218        assert( sqlite3VdbeRecordCompare(aSample[i].n, aSample[i].p, pRec)<=0
121219             || pParse->db->mallocFailed );
121220      }
121221      if( i>0 ){
121222        pRec->nField = nField;
121223        assert( sqlite3VdbeRecordCompare(aSample[i-1].n, aSample[i-1].p, pRec)<0
121224             || pParse->db->mallocFailed );
121225      }
121226    }
121227  }
121228#endif /* ifdef SQLITE_DEBUG */
121229
121230  if( res==0 ){
121231    /* Record pRec is equal to sample i */
121232    assert( iCol==nField-1 );
121233    aStat[0] = aSample[i].anLt[iCol];
121234    aStat[1] = aSample[i].anEq[iCol];
121235  }else{
121236    /* At this point, the (iCol+1) field prefix of aSample[i] is the first
121237    ** sample that is greater than pRec. Or, if i==pIdx->nSample then pRec
121238    ** is larger than all samples in the array. */
121239    tRowcnt iUpper, iGap;
121240    if( i>=pIdx->nSample ){
121241      iUpper = sqlite3LogEstToInt(pIdx->aiRowLogEst[0]);
121242    }else{
121243      iUpper = aSample[i].anLt[iCol];
121244    }
121245
121246    if( iLower>=iUpper ){
121247      iGap = 0;
121248    }else{
121249      iGap = iUpper - iLower;
121250    }
121251    if( roundUp ){
121252      iGap = (iGap*2)/3;
121253    }else{
121254      iGap = iGap/3;
121255    }
121256    aStat[0] = iLower + iGap;
121257    aStat[1] = pIdx->aAvgEq[iCol];
121258  }
121259
121260  /* Restore the pRec->nField value before returning.  */
121261  pRec->nField = nField;
121262  return i;
121263}
121264#endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */
121265
121266/*
121267** If it is not NULL, pTerm is a term that provides an upper or lower
121268** bound on a range scan. Without considering pTerm, it is estimated
121269** that the scan will visit nNew rows. This function returns the number
121270** estimated to be visited after taking pTerm into account.
121271**
121272** If the user explicitly specified a likelihood() value for this term,
121273** then the return value is the likelihood multiplied by the number of
121274** input rows. Otherwise, this function assumes that an "IS NOT NULL" term
121275** has a likelihood of 0.50, and any other term a likelihood of 0.25.
121276*/
121277static LogEst whereRangeAdjust(WhereTerm *pTerm, LogEst nNew){
121278  LogEst nRet = nNew;
121279  if( pTerm ){
121280    if( pTerm->truthProb<=0 ){
121281      nRet += pTerm->truthProb;
121282    }else if( (pTerm->wtFlags & TERM_VNULL)==0 ){
121283      nRet -= 20;        assert( 20==sqlite3LogEst(4) );
121284    }
121285  }
121286  return nRet;
121287}
121288
121289#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
121290/*
121291** This function is called to estimate the number of rows visited by a
121292** range-scan on a skip-scan index. For example:
121293**
121294**   CREATE INDEX i1 ON t1(a, b, c);
121295**   SELECT * FROM t1 WHERE a=? AND c BETWEEN ? AND ?;
121296**
121297** Value pLoop->nOut is currently set to the estimated number of rows
121298** visited for scanning (a=? AND b=?). This function reduces that estimate
121299** by some factor to account for the (c BETWEEN ? AND ?) expression based
121300** on the stat4 data for the index. this scan will be peformed multiple
121301** times (once for each (a,b) combination that matches a=?) is dealt with
121302** by the caller.
121303**
121304** It does this by scanning through all stat4 samples, comparing values
121305** extracted from pLower and pUpper with the corresponding column in each
121306** sample. If L and U are the number of samples found to be less than or
121307** equal to the values extracted from pLower and pUpper respectively, and
121308** N is the total number of samples, the pLoop->nOut value is adjusted
121309** as follows:
121310**
121311**   nOut = nOut * ( min(U - L, 1) / N )
121312**
121313** If pLower is NULL, or a value cannot be extracted from the term, L is
121314** set to zero. If pUpper is NULL, or a value cannot be extracted from it,
121315** U is set to N.
121316**
121317** Normally, this function sets *pbDone to 1 before returning. However,
121318** if no value can be extracted from either pLower or pUpper (and so the
121319** estimate of the number of rows delivered remains unchanged), *pbDone
121320** is left as is.
121321**
121322** If an error occurs, an SQLite error code is returned. Otherwise,
121323** SQLITE_OK.
121324*/
121325static int whereRangeSkipScanEst(
121326  Parse *pParse,       /* Parsing & code generating context */
121327  WhereTerm *pLower,   /* Lower bound on the range. ex: "x>123" Might be NULL */
121328  WhereTerm *pUpper,   /* Upper bound on the range. ex: "x<455" Might be NULL */
121329  WhereLoop *pLoop,    /* Update the .nOut value of this loop */
121330  int *pbDone          /* Set to true if at least one expr. value extracted */
121331){
121332  Index *p = pLoop->u.btree.pIndex;
121333  int nEq = pLoop->u.btree.nEq;
121334  sqlite3 *db = pParse->db;
121335  int nLower = -1;
121336  int nUpper = p->nSample+1;
121337  int rc = SQLITE_OK;
121338  int iCol = p->aiColumn[nEq];
121339  u8 aff = iCol>=0 ? p->pTable->aCol[iCol].affinity : SQLITE_AFF_INTEGER;
121340  CollSeq *pColl;
121341
121342  sqlite3_value *p1 = 0;          /* Value extracted from pLower */
121343  sqlite3_value *p2 = 0;          /* Value extracted from pUpper */
121344  sqlite3_value *pVal = 0;        /* Value extracted from record */
121345
121346  pColl = sqlite3LocateCollSeq(pParse, p->azColl[nEq]);
121347  if( pLower ){
121348    rc = sqlite3Stat4ValueFromExpr(pParse, pLower->pExpr->pRight, aff, &p1);
121349    nLower = 0;
121350  }
121351  if( pUpper && rc==SQLITE_OK ){
121352    rc = sqlite3Stat4ValueFromExpr(pParse, pUpper->pExpr->pRight, aff, &p2);
121353    nUpper = p2 ? 0 : p->nSample;
121354  }
121355
121356  if( p1 || p2 ){
121357    int i;
121358    int nDiff;
121359    for(i=0; rc==SQLITE_OK && i<p->nSample; i++){
121360      rc = sqlite3Stat4Column(db, p->aSample[i].p, p->aSample[i].n, nEq, &pVal);
121361      if( rc==SQLITE_OK && p1 ){
121362        int res = sqlite3MemCompare(p1, pVal, pColl);
121363        if( res>=0 ) nLower++;
121364      }
121365      if( rc==SQLITE_OK && p2 ){
121366        int res = sqlite3MemCompare(p2, pVal, pColl);
121367        if( res>=0 ) nUpper++;
121368      }
121369    }
121370    nDiff = (nUpper - nLower);
121371    if( nDiff<=0 ) nDiff = 1;
121372
121373    /* If there is both an upper and lower bound specified, and the
121374    ** comparisons indicate that they are close together, use the fallback
121375    ** method (assume that the scan visits 1/64 of the rows) for estimating
121376    ** the number of rows visited. Otherwise, estimate the number of rows
121377    ** using the method described in the header comment for this function. */
121378    if( nDiff!=1 || pUpper==0 || pLower==0 ){
121379      int nAdjust = (sqlite3LogEst(p->nSample) - sqlite3LogEst(nDiff));
121380      pLoop->nOut -= nAdjust;
121381      *pbDone = 1;
121382      WHERETRACE(0x10, ("range skip-scan regions: %u..%u  adjust=%d est=%d\n",
121383                           nLower, nUpper, nAdjust*-1, pLoop->nOut));
121384    }
121385
121386  }else{
121387    assert( *pbDone==0 );
121388  }
121389
121390  sqlite3ValueFree(p1);
121391  sqlite3ValueFree(p2);
121392  sqlite3ValueFree(pVal);
121393
121394  return rc;
121395}
121396#endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */
121397
121398/*
121399** This function is used to estimate the number of rows that will be visited
121400** by scanning an index for a range of values. The range may have an upper
121401** bound, a lower bound, or both. The WHERE clause terms that set the upper
121402** and lower bounds are represented by pLower and pUpper respectively. For
121403** example, assuming that index p is on t1(a):
121404**
121405**   ... FROM t1 WHERE a > ? AND a < ? ...
121406**                    |_____|   |_____|
121407**                       |         |
121408**                     pLower    pUpper
121409**
121410** If either of the upper or lower bound is not present, then NULL is passed in
121411** place of the corresponding WhereTerm.
121412**
121413** The value in (pBuilder->pNew->u.btree.nEq) is the number of the index
121414** column subject to the range constraint. Or, equivalently, the number of
121415** equality constraints optimized by the proposed index scan. For example,
121416** assuming index p is on t1(a, b), and the SQL query is:
121417**
121418**   ... FROM t1 WHERE a = ? AND b > ? AND b < ? ...
121419**
121420** then nEq is set to 1 (as the range restricted column, b, is the second
121421** left-most column of the index). Or, if the query is:
121422**
121423**   ... FROM t1 WHERE a > ? AND a < ? ...
121424**
121425** then nEq is set to 0.
121426**
121427** When this function is called, *pnOut is set to the sqlite3LogEst() of the
121428** number of rows that the index scan is expected to visit without
121429** considering the range constraints. If nEq is 0, then *pnOut is the number of
121430** rows in the index. Assuming no error occurs, *pnOut is adjusted (reduced)
121431** to account for the range constraints pLower and pUpper.
121432**
121433** In the absence of sqlite_stat4 ANALYZE data, or if such data cannot be
121434** used, a single range inequality reduces the search space by a factor of 4.
121435** and a pair of constraints (x>? AND x<?) reduces the expected number of
121436** rows visited by a factor of 64.
121437*/
121438static int whereRangeScanEst(
121439  Parse *pParse,       /* Parsing & code generating context */
121440  WhereLoopBuilder *pBuilder,
121441  WhereTerm *pLower,   /* Lower bound on the range. ex: "x>123" Might be NULL */
121442  WhereTerm *pUpper,   /* Upper bound on the range. ex: "x<455" Might be NULL */
121443  WhereLoop *pLoop     /* Modify the .nOut and maybe .rRun fields */
121444){
121445  int rc = SQLITE_OK;
121446  int nOut = pLoop->nOut;
121447  LogEst nNew;
121448
121449#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
121450  Index *p = pLoop->u.btree.pIndex;
121451  int nEq = pLoop->u.btree.nEq;
121452
121453  if( p->nSample>0 && nEq<p->nSampleCol ){
121454    if( nEq==pBuilder->nRecValid ){
121455      UnpackedRecord *pRec = pBuilder->pRec;
121456      tRowcnt a[2];
121457      u8 aff;
121458
121459      /* Variable iLower will be set to the estimate of the number of rows in
121460      ** the index that are less than the lower bound of the range query. The
121461      ** lower bound being the concatenation of $P and $L, where $P is the
121462      ** key-prefix formed by the nEq values matched against the nEq left-most
121463      ** columns of the index, and $L is the value in pLower.
121464      **
121465      ** Or, if pLower is NULL or $L cannot be extracted from it (because it
121466      ** is not a simple variable or literal value), the lower bound of the
121467      ** range is $P. Due to a quirk in the way whereKeyStats() works, even
121468      ** if $L is available, whereKeyStats() is called for both ($P) and
121469      ** ($P:$L) and the larger of the two returned values is used.
121470      **
121471      ** Similarly, iUpper is to be set to the estimate of the number of rows
121472      ** less than the upper bound of the range query. Where the upper bound
121473      ** is either ($P) or ($P:$U). Again, even if $U is available, both values
121474      ** of iUpper are requested of whereKeyStats() and the smaller used.
121475      **
121476      ** The number of rows between the two bounds is then just iUpper-iLower.
121477      */
121478      tRowcnt iLower;     /* Rows less than the lower bound */
121479      tRowcnt iUpper;     /* Rows less than the upper bound */
121480      int iLwrIdx = -2;   /* aSample[] for the lower bound */
121481      int iUprIdx = -1;   /* aSample[] for the upper bound */
121482
121483      if( pRec ){
121484        testcase( pRec->nField!=pBuilder->nRecValid );
121485        pRec->nField = pBuilder->nRecValid;
121486      }
121487      if( nEq==p->nKeyCol ){
121488        aff = SQLITE_AFF_INTEGER;
121489      }else{
121490        aff = p->pTable->aCol[p->aiColumn[nEq]].affinity;
121491      }
121492      /* Determine iLower and iUpper using ($P) only. */
121493      if( nEq==0 ){
121494        iLower = 0;
121495        iUpper = p->nRowEst0;
121496      }else{
121497        /* Note: this call could be optimized away - since the same values must
121498        ** have been requested when testing key $P in whereEqualScanEst().  */
121499        whereKeyStats(pParse, p, pRec, 0, a);
121500        iLower = a[0];
121501        iUpper = a[0] + a[1];
121502      }
121503
121504      assert( pLower==0 || (pLower->eOperator & (WO_GT|WO_GE))!=0 );
121505      assert( pUpper==0 || (pUpper->eOperator & (WO_LT|WO_LE))!=0 );
121506      assert( p->aSortOrder!=0 );
121507      if( p->aSortOrder[nEq] ){
121508        /* The roles of pLower and pUpper are swapped for a DESC index */
121509        SWAP(WhereTerm*, pLower, pUpper);
121510      }
121511
121512      /* If possible, improve on the iLower estimate using ($P:$L). */
121513      if( pLower ){
121514        int bOk;                    /* True if value is extracted from pExpr */
121515        Expr *pExpr = pLower->pExpr->pRight;
121516        rc = sqlite3Stat4ProbeSetValue(pParse, p, &pRec, pExpr, aff, nEq, &bOk);
121517        if( rc==SQLITE_OK && bOk ){
121518          tRowcnt iNew;
121519          iLwrIdx = whereKeyStats(pParse, p, pRec, 0, a);
121520          iNew = a[0] + ((pLower->eOperator & (WO_GT|WO_LE)) ? a[1] : 0);
121521          if( iNew>iLower ) iLower = iNew;
121522          nOut--;
121523          pLower = 0;
121524        }
121525      }
121526
121527      /* If possible, improve on the iUpper estimate using ($P:$U). */
121528      if( pUpper ){
121529        int bOk;                    /* True if value is extracted from pExpr */
121530        Expr *pExpr = pUpper->pExpr->pRight;
121531        rc = sqlite3Stat4ProbeSetValue(pParse, p, &pRec, pExpr, aff, nEq, &bOk);
121532        if( rc==SQLITE_OK && bOk ){
121533          tRowcnt iNew;
121534          iUprIdx = whereKeyStats(pParse, p, pRec, 1, a);
121535          iNew = a[0] + ((pUpper->eOperator & (WO_GT|WO_LE)) ? a[1] : 0);
121536          if( iNew<iUpper ) iUpper = iNew;
121537          nOut--;
121538          pUpper = 0;
121539        }
121540      }
121541
121542      pBuilder->pRec = pRec;
121543      if( rc==SQLITE_OK ){
121544        if( iUpper>iLower ){
121545          nNew = sqlite3LogEst(iUpper - iLower);
121546          /* TUNING:  If both iUpper and iLower are derived from the same
121547          ** sample, then assume they are 4x more selective.  This brings
121548          ** the estimated selectivity more in line with what it would be
121549          ** if estimated without the use of STAT3/4 tables. */
121550          if( iLwrIdx==iUprIdx ) nNew -= 20;  assert( 20==sqlite3LogEst(4) );
121551        }else{
121552          nNew = 10;        assert( 10==sqlite3LogEst(2) );
121553        }
121554        if( nNew<nOut ){
121555          nOut = nNew;
121556        }
121557        WHERETRACE(0x10, ("STAT4 range scan: %u..%u  est=%d\n",
121558                           (u32)iLower, (u32)iUpper, nOut));
121559      }
121560    }else{
121561      int bDone = 0;
121562      rc = whereRangeSkipScanEst(pParse, pLower, pUpper, pLoop, &bDone);
121563      if( bDone ) return rc;
121564    }
121565  }
121566#else
121567  UNUSED_PARAMETER(pParse);
121568  UNUSED_PARAMETER(pBuilder);
121569  assert( pLower || pUpper );
121570#endif
121571  assert( pUpper==0 || (pUpper->wtFlags & TERM_VNULL)==0 );
121572  nNew = whereRangeAdjust(pLower, nOut);
121573  nNew = whereRangeAdjust(pUpper, nNew);
121574
121575  /* TUNING: If there is both an upper and lower limit and neither limit
121576  ** has an application-defined likelihood(), assume the range is
121577  ** reduced by an additional 75%. This means that, by default, an open-ended
121578  ** range query (e.g. col > ?) is assumed to match 1/4 of the rows in the
121579  ** index. While a closed range (e.g. col BETWEEN ? AND ?) is estimated to
121580  ** match 1/64 of the index. */
121581  if( pLower && pLower->truthProb>0 && pUpper && pUpper->truthProb>0 ){
121582    nNew -= 20;
121583  }
121584
121585  nOut -= (pLower!=0) + (pUpper!=0);
121586  if( nNew<10 ) nNew = 10;
121587  if( nNew<nOut ) nOut = nNew;
121588#if defined(WHERETRACE_ENABLED)
121589  if( pLoop->nOut>nOut ){
121590    WHERETRACE(0x10,("Range scan lowers nOut from %d to %d\n",
121591                    pLoop->nOut, nOut));
121592  }
121593#endif
121594  pLoop->nOut = (LogEst)nOut;
121595  return rc;
121596}
121597
121598#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
121599/*
121600** Estimate the number of rows that will be returned based on
121601** an equality constraint x=VALUE and where that VALUE occurs in
121602** the histogram data.  This only works when x is the left-most
121603** column of an index and sqlite_stat3 histogram data is available
121604** for that index.  When pExpr==NULL that means the constraint is
121605** "x IS NULL" instead of "x=VALUE".
121606**
121607** Write the estimated row count into *pnRow and return SQLITE_OK.
121608** If unable to make an estimate, leave *pnRow unchanged and return
121609** non-zero.
121610**
121611** This routine can fail if it is unable to load a collating sequence
121612** required for string comparison, or if unable to allocate memory
121613** for a UTF conversion required for comparison.  The error is stored
121614** in the pParse structure.
121615*/
121616static int whereEqualScanEst(
121617  Parse *pParse,       /* Parsing & code generating context */
121618  WhereLoopBuilder *pBuilder,
121619  Expr *pExpr,         /* Expression for VALUE in the x=VALUE constraint */
121620  tRowcnt *pnRow       /* Write the revised row estimate here */
121621){
121622  Index *p = pBuilder->pNew->u.btree.pIndex;
121623  int nEq = pBuilder->pNew->u.btree.nEq;
121624  UnpackedRecord *pRec = pBuilder->pRec;
121625  u8 aff;                   /* Column affinity */
121626  int rc;                   /* Subfunction return code */
121627  tRowcnt a[2];             /* Statistics */
121628  int bOk;
121629
121630  assert( nEq>=1 );
121631  assert( nEq<=p->nColumn );
121632  assert( p->aSample!=0 );
121633  assert( p->nSample>0 );
121634  assert( pBuilder->nRecValid<nEq );
121635
121636  /* If values are not available for all fields of the index to the left
121637  ** of this one, no estimate can be made. Return SQLITE_NOTFOUND. */
121638  if( pBuilder->nRecValid<(nEq-1) ){
121639    return SQLITE_NOTFOUND;
121640  }
121641
121642  /* This is an optimization only. The call to sqlite3Stat4ProbeSetValue()
121643  ** below would return the same value.  */
121644  if( nEq>=p->nColumn ){
121645    *pnRow = 1;
121646    return SQLITE_OK;
121647  }
121648
121649  aff = p->pTable->aCol[p->aiColumn[nEq-1]].affinity;
121650  rc = sqlite3Stat4ProbeSetValue(pParse, p, &pRec, pExpr, aff, nEq-1, &bOk);
121651  pBuilder->pRec = pRec;
121652  if( rc!=SQLITE_OK ) return rc;
121653  if( bOk==0 ) return SQLITE_NOTFOUND;
121654  pBuilder->nRecValid = nEq;
121655
121656  whereKeyStats(pParse, p, pRec, 0, a);
121657  WHERETRACE(0x10,("equality scan regions: %d\n", (int)a[1]));
121658  *pnRow = a[1];
121659
121660  return rc;
121661}
121662#endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */
121663
121664#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
121665/*
121666** Estimate the number of rows that will be returned based on
121667** an IN constraint where the right-hand side of the IN operator
121668** is a list of values.  Example:
121669**
121670**        WHERE x IN (1,2,3,4)
121671**
121672** Write the estimated row count into *pnRow and return SQLITE_OK.
121673** If unable to make an estimate, leave *pnRow unchanged and return
121674** non-zero.
121675**
121676** This routine can fail if it is unable to load a collating sequence
121677** required for string comparison, or if unable to allocate memory
121678** for a UTF conversion required for comparison.  The error is stored
121679** in the pParse structure.
121680*/
121681static int whereInScanEst(
121682  Parse *pParse,       /* Parsing & code generating context */
121683  WhereLoopBuilder *pBuilder,
121684  ExprList *pList,     /* The value list on the RHS of "x IN (v1,v2,v3,...)" */
121685  tRowcnt *pnRow       /* Write the revised row estimate here */
121686){
121687  Index *p = pBuilder->pNew->u.btree.pIndex;
121688  i64 nRow0 = sqlite3LogEstToInt(p->aiRowLogEst[0]);
121689  int nRecValid = pBuilder->nRecValid;
121690  int rc = SQLITE_OK;     /* Subfunction return code */
121691  tRowcnt nEst;           /* Number of rows for a single term */
121692  tRowcnt nRowEst = 0;    /* New estimate of the number of rows */
121693  int i;                  /* Loop counter */
121694
121695  assert( p->aSample!=0 );
121696  for(i=0; rc==SQLITE_OK && i<pList->nExpr; i++){
121697    nEst = nRow0;
121698    rc = whereEqualScanEst(pParse, pBuilder, pList->a[i].pExpr, &nEst);
121699    nRowEst += nEst;
121700    pBuilder->nRecValid = nRecValid;
121701  }
121702
121703  if( rc==SQLITE_OK ){
121704    if( nRowEst > nRow0 ) nRowEst = nRow0;
121705    *pnRow = nRowEst;
121706    WHERETRACE(0x10,("IN row estimate: est=%d\n", nRowEst));
121707  }
121708  assert( pBuilder->nRecValid==nRecValid );
121709  return rc;
121710}
121711#endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */
121712
121713
121714#ifdef WHERETRACE_ENABLED
121715/*
121716** Print the content of a WhereTerm object
121717*/
121718static void whereTermPrint(WhereTerm *pTerm, int iTerm){
121719  if( pTerm==0 ){
121720    sqlite3DebugPrintf("TERM-%-3d NULL\n", iTerm);
121721  }else{
121722    char zType[4];
121723    memcpy(zType, "...", 4);
121724    if( pTerm->wtFlags & TERM_VIRTUAL ) zType[0] = 'V';
121725    if( pTerm->eOperator & WO_EQUIV  ) zType[1] = 'E';
121726    if( ExprHasProperty(pTerm->pExpr, EP_FromJoin) ) zType[2] = 'L';
121727    sqlite3DebugPrintf(
121728       "TERM-%-3d %p %s cursor=%-3d prob=%-3d op=0x%03x wtFlags=0x%04x\n",
121729       iTerm, pTerm, zType, pTerm->leftCursor, pTerm->truthProb,
121730       pTerm->eOperator, pTerm->wtFlags);
121731    sqlite3TreeViewExpr(0, pTerm->pExpr, 0);
121732  }
121733}
121734#endif
121735
121736#ifdef WHERETRACE_ENABLED
121737/*
121738** Print a WhereLoop object for debugging purposes
121739*/
121740static void whereLoopPrint(WhereLoop *p, WhereClause *pWC){
121741  WhereInfo *pWInfo = pWC->pWInfo;
121742  int nb = 1+(pWInfo->pTabList->nSrc+7)/8;
121743  struct SrcList_item *pItem = pWInfo->pTabList->a + p->iTab;
121744  Table *pTab = pItem->pTab;
121745  sqlite3DebugPrintf("%c%2d.%0*llx.%0*llx", p->cId,
121746                     p->iTab, nb, p->maskSelf, nb, p->prereq);
121747  sqlite3DebugPrintf(" %12s",
121748                     pItem->zAlias ? pItem->zAlias : pTab->zName);
121749  if( (p->wsFlags & WHERE_VIRTUALTABLE)==0 ){
121750    const char *zName;
121751    if( p->u.btree.pIndex && (zName = p->u.btree.pIndex->zName)!=0 ){
121752      if( strncmp(zName, "sqlite_autoindex_", 17)==0 ){
121753        int i = sqlite3Strlen30(zName) - 1;
121754        while( zName[i]!='_' ) i--;
121755        zName += i;
121756      }
121757      sqlite3DebugPrintf(".%-16s %2d", zName, p->u.btree.nEq);
121758    }else{
121759      sqlite3DebugPrintf("%20s","");
121760    }
121761  }else{
121762    char *z;
121763    if( p->u.vtab.idxStr ){
121764      z = sqlite3_mprintf("(%d,\"%s\",%x)",
121765                p->u.vtab.idxNum, p->u.vtab.idxStr, p->u.vtab.omitMask);
121766    }else{
121767      z = sqlite3_mprintf("(%d,%x)", p->u.vtab.idxNum, p->u.vtab.omitMask);
121768    }
121769    sqlite3DebugPrintf(" %-19s", z);
121770    sqlite3_free(z);
121771  }
121772  if( p->wsFlags & WHERE_SKIPSCAN ){
121773    sqlite3DebugPrintf(" f %05x %d-%d", p->wsFlags, p->nLTerm,p->nSkip);
121774  }else{
121775    sqlite3DebugPrintf(" f %05x N %d", p->wsFlags, p->nLTerm);
121776  }
121777  sqlite3DebugPrintf(" cost %d,%d,%d\n", p->rSetup, p->rRun, p->nOut);
121778  if( p->nLTerm && (sqlite3WhereTrace & 0x100)!=0 ){
121779    int i;
121780    for(i=0; i<p->nLTerm; i++){
121781      whereTermPrint(p->aLTerm[i], i);
121782    }
121783  }
121784}
121785#endif
121786
121787/*
121788** Convert bulk memory into a valid WhereLoop that can be passed
121789** to whereLoopClear harmlessly.
121790*/
121791static void whereLoopInit(WhereLoop *p){
121792  p->aLTerm = p->aLTermSpace;
121793  p->nLTerm = 0;
121794  p->nLSlot = ArraySize(p->aLTermSpace);
121795  p->wsFlags = 0;
121796}
121797
121798/*
121799** Clear the WhereLoop.u union.  Leave WhereLoop.pLTerm intact.
121800*/
121801static void whereLoopClearUnion(sqlite3 *db, WhereLoop *p){
121802  if( p->wsFlags & (WHERE_VIRTUALTABLE|WHERE_AUTO_INDEX) ){
121803    if( (p->wsFlags & WHERE_VIRTUALTABLE)!=0 && p->u.vtab.needFree ){
121804      sqlite3_free(p->u.vtab.idxStr);
121805      p->u.vtab.needFree = 0;
121806      p->u.vtab.idxStr = 0;
121807    }else if( (p->wsFlags & WHERE_AUTO_INDEX)!=0 && p->u.btree.pIndex!=0 ){
121808      sqlite3DbFree(db, p->u.btree.pIndex->zColAff);
121809      sqlite3DbFree(db, p->u.btree.pIndex);
121810      p->u.btree.pIndex = 0;
121811    }
121812  }
121813}
121814
121815/*
121816** Deallocate internal memory used by a WhereLoop object
121817*/
121818static void whereLoopClear(sqlite3 *db, WhereLoop *p){
121819  if( p->aLTerm!=p->aLTermSpace ) sqlite3DbFree(db, p->aLTerm);
121820  whereLoopClearUnion(db, p);
121821  whereLoopInit(p);
121822}
121823
121824/*
121825** Increase the memory allocation for pLoop->aLTerm[] to be at least n.
121826*/
121827static int whereLoopResize(sqlite3 *db, WhereLoop *p, int n){
121828  WhereTerm **paNew;
121829  if( p->nLSlot>=n ) return SQLITE_OK;
121830  n = (n+7)&~7;
121831  paNew = sqlite3DbMallocRaw(db, sizeof(p->aLTerm[0])*n);
121832  if( paNew==0 ) return SQLITE_NOMEM;
121833  memcpy(paNew, p->aLTerm, sizeof(p->aLTerm[0])*p->nLSlot);
121834  if( p->aLTerm!=p->aLTermSpace ) sqlite3DbFree(db, p->aLTerm);
121835  p->aLTerm = paNew;
121836  p->nLSlot = n;
121837  return SQLITE_OK;
121838}
121839
121840/*
121841** Transfer content from the second pLoop into the first.
121842*/
121843static int whereLoopXfer(sqlite3 *db, WhereLoop *pTo, WhereLoop *pFrom){
121844  whereLoopClearUnion(db, pTo);
121845  if( whereLoopResize(db, pTo, pFrom->nLTerm) ){
121846    memset(&pTo->u, 0, sizeof(pTo->u));
121847    return SQLITE_NOMEM;
121848  }
121849  memcpy(pTo, pFrom, WHERE_LOOP_XFER_SZ);
121850  memcpy(pTo->aLTerm, pFrom->aLTerm, pTo->nLTerm*sizeof(pTo->aLTerm[0]));
121851  if( pFrom->wsFlags & WHERE_VIRTUALTABLE ){
121852    pFrom->u.vtab.needFree = 0;
121853  }else if( (pFrom->wsFlags & WHERE_AUTO_INDEX)!=0 ){
121854    pFrom->u.btree.pIndex = 0;
121855  }
121856  return SQLITE_OK;
121857}
121858
121859/*
121860** Delete a WhereLoop object
121861*/
121862static void whereLoopDelete(sqlite3 *db, WhereLoop *p){
121863  whereLoopClear(db, p);
121864  sqlite3DbFree(db, p);
121865}
121866
121867/*
121868** Free a WhereInfo structure
121869*/
121870static void whereInfoFree(sqlite3 *db, WhereInfo *pWInfo){
121871  if( ALWAYS(pWInfo) ){
121872    int i;
121873    for(i=0; i<pWInfo->nLevel; i++){
121874      WhereLevel *pLevel = &pWInfo->a[i];
121875      if( pLevel->pWLoop && (pLevel->pWLoop->wsFlags & WHERE_IN_ABLE) ){
121876        sqlite3DbFree(db, pLevel->u.in.aInLoop);
121877      }
121878    }
121879    sqlite3WhereClauseClear(&pWInfo->sWC);
121880    while( pWInfo->pLoops ){
121881      WhereLoop *p = pWInfo->pLoops;
121882      pWInfo->pLoops = p->pNextLoop;
121883      whereLoopDelete(db, p);
121884    }
121885    sqlite3DbFree(db, pWInfo);
121886  }
121887}
121888
121889/*
121890** Return TRUE if all of the following are true:
121891**
121892**   (1)  X has the same or lower cost that Y
121893**   (2)  X is a proper subset of Y
121894**   (3)  X skips at least as many columns as Y
121895**
121896** By "proper subset" we mean that X uses fewer WHERE clause terms
121897** than Y and that every WHERE clause term used by X is also used
121898** by Y.
121899**
121900** If X is a proper subset of Y then Y is a better choice and ought
121901** to have a lower cost.  This routine returns TRUE when that cost
121902** relationship is inverted and needs to be adjusted.  The third rule
121903** was added because if X uses skip-scan less than Y it still might
121904** deserve a lower cost even if it is a proper subset of Y.
121905*/
121906static int whereLoopCheaperProperSubset(
121907  const WhereLoop *pX,       /* First WhereLoop to compare */
121908  const WhereLoop *pY        /* Compare against this WhereLoop */
121909){
121910  int i, j;
121911  if( pX->nLTerm-pX->nSkip >= pY->nLTerm-pY->nSkip ){
121912    return 0; /* X is not a subset of Y */
121913  }
121914  if( pY->nSkip > pX->nSkip ) return 0;
121915  if( pX->rRun >= pY->rRun ){
121916    if( pX->rRun > pY->rRun ) return 0;    /* X costs more than Y */
121917    if( pX->nOut > pY->nOut ) return 0;    /* X costs more than Y */
121918  }
121919  for(i=pX->nLTerm-1; i>=0; i--){
121920    if( pX->aLTerm[i]==0 ) continue;
121921    for(j=pY->nLTerm-1; j>=0; j--){
121922      if( pY->aLTerm[j]==pX->aLTerm[i] ) break;
121923    }
121924    if( j<0 ) return 0;  /* X not a subset of Y since term X[i] not used by Y */
121925  }
121926  return 1;  /* All conditions meet */
121927}
121928
121929/*
121930** Try to adjust the cost of WhereLoop pTemplate upwards or downwards so
121931** that:
121932**
121933**   (1) pTemplate costs less than any other WhereLoops that are a proper
121934**       subset of pTemplate
121935**
121936**   (2) pTemplate costs more than any other WhereLoops for which pTemplate
121937**       is a proper subset.
121938**
121939** To say "WhereLoop X is a proper subset of Y" means that X uses fewer
121940** WHERE clause terms than Y and that every WHERE clause term used by X is
121941** also used by Y.
121942*/
121943static void whereLoopAdjustCost(const WhereLoop *p, WhereLoop *pTemplate){
121944  if( (pTemplate->wsFlags & WHERE_INDEXED)==0 ) return;
121945  for(; p; p=p->pNextLoop){
121946    if( p->iTab!=pTemplate->iTab ) continue;
121947    if( (p->wsFlags & WHERE_INDEXED)==0 ) continue;
121948    if( whereLoopCheaperProperSubset(p, pTemplate) ){
121949      /* Adjust pTemplate cost downward so that it is cheaper than its
121950      ** subset p. */
121951      WHERETRACE(0x80,("subset cost adjustment %d,%d to %d,%d\n",
121952                       pTemplate->rRun, pTemplate->nOut, p->rRun, p->nOut-1));
121953      pTemplate->rRun = p->rRun;
121954      pTemplate->nOut = p->nOut - 1;
121955    }else if( whereLoopCheaperProperSubset(pTemplate, p) ){
121956      /* Adjust pTemplate cost upward so that it is costlier than p since
121957      ** pTemplate is a proper subset of p */
121958      WHERETRACE(0x80,("subset cost adjustment %d,%d to %d,%d\n",
121959                       pTemplate->rRun, pTemplate->nOut, p->rRun, p->nOut+1));
121960      pTemplate->rRun = p->rRun;
121961      pTemplate->nOut = p->nOut + 1;
121962    }
121963  }
121964}
121965
121966/*
121967** Search the list of WhereLoops in *ppPrev looking for one that can be
121968** supplanted by pTemplate.
121969**
121970** Return NULL if the WhereLoop list contains an entry that can supplant
121971** pTemplate, in other words if pTemplate does not belong on the list.
121972**
121973** If pX is a WhereLoop that pTemplate can supplant, then return the
121974** link that points to pX.
121975**
121976** If pTemplate cannot supplant any existing element of the list but needs
121977** to be added to the list, then return a pointer to the tail of the list.
121978*/
121979static WhereLoop **whereLoopFindLesser(
121980  WhereLoop **ppPrev,
121981  const WhereLoop *pTemplate
121982){
121983  WhereLoop *p;
121984  for(p=(*ppPrev); p; ppPrev=&p->pNextLoop, p=*ppPrev){
121985    if( p->iTab!=pTemplate->iTab || p->iSortIdx!=pTemplate->iSortIdx ){
121986      /* If either the iTab or iSortIdx values for two WhereLoop are different
121987      ** then those WhereLoops need to be considered separately.  Neither is
121988      ** a candidate to replace the other. */
121989      continue;
121990    }
121991    /* In the current implementation, the rSetup value is either zero
121992    ** or the cost of building an automatic index (NlogN) and the NlogN
121993    ** is the same for compatible WhereLoops. */
121994    assert( p->rSetup==0 || pTemplate->rSetup==0
121995                 || p->rSetup==pTemplate->rSetup );
121996
121997    /* whereLoopAddBtree() always generates and inserts the automatic index
121998    ** case first.  Hence compatible candidate WhereLoops never have a larger
121999    ** rSetup. Call this SETUP-INVARIANT */
122000    assert( p->rSetup>=pTemplate->rSetup );
122001
122002    /* Any loop using an appliation-defined index (or PRIMARY KEY or
122003    ** UNIQUE constraint) with one or more == constraints is better
122004    ** than an automatic index. Unless it is a skip-scan. */
122005    if( (p->wsFlags & WHERE_AUTO_INDEX)!=0
122006     && (pTemplate->nSkip)==0
122007     && (pTemplate->wsFlags & WHERE_INDEXED)!=0
122008     && (pTemplate->wsFlags & WHERE_COLUMN_EQ)!=0
122009     && (p->prereq & pTemplate->prereq)==pTemplate->prereq
122010    ){
122011      break;
122012    }
122013
122014    /* If existing WhereLoop p is better than pTemplate, pTemplate can be
122015    ** discarded.  WhereLoop p is better if:
122016    **   (1)  p has no more dependencies than pTemplate, and
122017    **   (2)  p has an equal or lower cost than pTemplate
122018    */
122019    if( (p->prereq & pTemplate->prereq)==p->prereq    /* (1)  */
122020     && p->rSetup<=pTemplate->rSetup                  /* (2a) */
122021     && p->rRun<=pTemplate->rRun                      /* (2b) */
122022     && p->nOut<=pTemplate->nOut                      /* (2c) */
122023    ){
122024      return 0;  /* Discard pTemplate */
122025    }
122026
122027    /* If pTemplate is always better than p, then cause p to be overwritten
122028    ** with pTemplate.  pTemplate is better than p if:
122029    **   (1)  pTemplate has no more dependences than p, and
122030    **   (2)  pTemplate has an equal or lower cost than p.
122031    */
122032    if( (p->prereq & pTemplate->prereq)==pTemplate->prereq   /* (1)  */
122033     && p->rRun>=pTemplate->rRun                             /* (2a) */
122034     && p->nOut>=pTemplate->nOut                             /* (2b) */
122035    ){
122036      assert( p->rSetup>=pTemplate->rSetup ); /* SETUP-INVARIANT above */
122037      break;   /* Cause p to be overwritten by pTemplate */
122038    }
122039  }
122040  return ppPrev;
122041}
122042
122043/*
122044** Insert or replace a WhereLoop entry using the template supplied.
122045**
122046** An existing WhereLoop entry might be overwritten if the new template
122047** is better and has fewer dependencies.  Or the template will be ignored
122048** and no insert will occur if an existing WhereLoop is faster and has
122049** fewer dependencies than the template.  Otherwise a new WhereLoop is
122050** added based on the template.
122051**
122052** If pBuilder->pOrSet is not NULL then we care about only the
122053** prerequisites and rRun and nOut costs of the N best loops.  That
122054** information is gathered in the pBuilder->pOrSet object.  This special
122055** processing mode is used only for OR clause processing.
122056**
122057** When accumulating multiple loops (when pBuilder->pOrSet is NULL) we
122058** still might overwrite similar loops with the new template if the
122059** new template is better.  Loops may be overwritten if the following
122060** conditions are met:
122061**
122062**    (1)  They have the same iTab.
122063**    (2)  They have the same iSortIdx.
122064**    (3)  The template has same or fewer dependencies than the current loop
122065**    (4)  The template has the same or lower cost than the current loop
122066*/
122067static int whereLoopInsert(WhereLoopBuilder *pBuilder, WhereLoop *pTemplate){
122068  WhereLoop **ppPrev, *p;
122069  WhereInfo *pWInfo = pBuilder->pWInfo;
122070  sqlite3 *db = pWInfo->pParse->db;
122071
122072  /* If pBuilder->pOrSet is defined, then only keep track of the costs
122073  ** and prereqs.
122074  */
122075  if( pBuilder->pOrSet!=0 ){
122076#if WHERETRACE_ENABLED
122077    u16 n = pBuilder->pOrSet->n;
122078    int x =
122079#endif
122080    whereOrInsert(pBuilder->pOrSet, pTemplate->prereq, pTemplate->rRun,
122081                                    pTemplate->nOut);
122082#if WHERETRACE_ENABLED /* 0x8 */
122083    if( sqlite3WhereTrace & 0x8 ){
122084      sqlite3DebugPrintf(x?"   or-%d:  ":"   or-X:  ", n);
122085      whereLoopPrint(pTemplate, pBuilder->pWC);
122086    }
122087#endif
122088    return SQLITE_OK;
122089  }
122090
122091  /* Look for an existing WhereLoop to replace with pTemplate
122092  */
122093  whereLoopAdjustCost(pWInfo->pLoops, pTemplate);
122094  ppPrev = whereLoopFindLesser(&pWInfo->pLoops, pTemplate);
122095
122096  if( ppPrev==0 ){
122097    /* There already exists a WhereLoop on the list that is better
122098    ** than pTemplate, so just ignore pTemplate */
122099#if WHERETRACE_ENABLED /* 0x8 */
122100    if( sqlite3WhereTrace & 0x8 ){
122101      sqlite3DebugPrintf("   skip: ");
122102      whereLoopPrint(pTemplate, pBuilder->pWC);
122103    }
122104#endif
122105    return SQLITE_OK;
122106  }else{
122107    p = *ppPrev;
122108  }
122109
122110  /* If we reach this point it means that either p[] should be overwritten
122111  ** with pTemplate[] if p[] exists, or if p==NULL then allocate a new
122112  ** WhereLoop and insert it.
122113  */
122114#if WHERETRACE_ENABLED /* 0x8 */
122115  if( sqlite3WhereTrace & 0x8 ){
122116    if( p!=0 ){
122117      sqlite3DebugPrintf("replace: ");
122118      whereLoopPrint(p, pBuilder->pWC);
122119    }
122120    sqlite3DebugPrintf("    add: ");
122121    whereLoopPrint(pTemplate, pBuilder->pWC);
122122  }
122123#endif
122124  if( p==0 ){
122125    /* Allocate a new WhereLoop to add to the end of the list */
122126    *ppPrev = p = sqlite3DbMallocRaw(db, sizeof(WhereLoop));
122127    if( p==0 ) return SQLITE_NOMEM;
122128    whereLoopInit(p);
122129    p->pNextLoop = 0;
122130  }else{
122131    /* We will be overwriting WhereLoop p[].  But before we do, first
122132    ** go through the rest of the list and delete any other entries besides
122133    ** p[] that are also supplated by pTemplate */
122134    WhereLoop **ppTail = &p->pNextLoop;
122135    WhereLoop *pToDel;
122136    while( *ppTail ){
122137      ppTail = whereLoopFindLesser(ppTail, pTemplate);
122138      if( ppTail==0 ) break;
122139      pToDel = *ppTail;
122140      if( pToDel==0 ) break;
122141      *ppTail = pToDel->pNextLoop;
122142#if WHERETRACE_ENABLED /* 0x8 */
122143      if( sqlite3WhereTrace & 0x8 ){
122144        sqlite3DebugPrintf(" delete: ");
122145        whereLoopPrint(pToDel, pBuilder->pWC);
122146      }
122147#endif
122148      whereLoopDelete(db, pToDel);
122149    }
122150  }
122151  whereLoopXfer(db, p, pTemplate);
122152  if( (p->wsFlags & WHERE_VIRTUALTABLE)==0 ){
122153    Index *pIndex = p->u.btree.pIndex;
122154    if( pIndex && pIndex->tnum==0 ){
122155      p->u.btree.pIndex = 0;
122156    }
122157  }
122158  return SQLITE_OK;
122159}
122160
122161/*
122162** Adjust the WhereLoop.nOut value downward to account for terms of the
122163** WHERE clause that reference the loop but which are not used by an
122164** index.
122165*
122166** For every WHERE clause term that is not used by the index
122167** and which has a truth probability assigned by one of the likelihood(),
122168** likely(), or unlikely() SQL functions, reduce the estimated number
122169** of output rows by the probability specified.
122170**
122171** TUNING:  For every WHERE clause term that is not used by the index
122172** and which does not have an assigned truth probability, heuristics
122173** described below are used to try to estimate the truth probability.
122174** TODO --> Perhaps this is something that could be improved by better
122175** table statistics.
122176**
122177** Heuristic 1:  Estimate the truth probability as 93.75%.  The 93.75%
122178** value corresponds to -1 in LogEst notation, so this means decrement
122179** the WhereLoop.nOut field for every such WHERE clause term.
122180**
122181** Heuristic 2:  If there exists one or more WHERE clause terms of the
122182** form "x==EXPR" and EXPR is not a constant 0 or 1, then make sure the
122183** final output row estimate is no greater than 1/4 of the total number
122184** of rows in the table.  In other words, assume that x==EXPR will filter
122185** out at least 3 out of 4 rows.  If EXPR is -1 or 0 or 1, then maybe the
122186** "x" column is boolean or else -1 or 0 or 1 is a common default value
122187** on the "x" column and so in that case only cap the output row estimate
122188** at 1/2 instead of 1/4.
122189*/
122190static void whereLoopOutputAdjust(
122191  WhereClause *pWC,      /* The WHERE clause */
122192  WhereLoop *pLoop,      /* The loop to adjust downward */
122193  LogEst nRow            /* Number of rows in the entire table */
122194){
122195  WhereTerm *pTerm, *pX;
122196  Bitmask notAllowed = ~(pLoop->prereq|pLoop->maskSelf);
122197  int i, j, k;
122198  LogEst iReduce = 0;    /* pLoop->nOut should not exceed nRow-iReduce */
122199
122200  assert( (pLoop->wsFlags & WHERE_AUTO_INDEX)==0 );
122201  for(i=pWC->nTerm, pTerm=pWC->a; i>0; i--, pTerm++){
122202    if( (pTerm->wtFlags & TERM_VIRTUAL)!=0 ) break;
122203    if( (pTerm->prereqAll & pLoop->maskSelf)==0 ) continue;
122204    if( (pTerm->prereqAll & notAllowed)!=0 ) continue;
122205    for(j=pLoop->nLTerm-1; j>=0; j--){
122206      pX = pLoop->aLTerm[j];
122207      if( pX==0 ) continue;
122208      if( pX==pTerm ) break;
122209      if( pX->iParent>=0 && (&pWC->a[pX->iParent])==pTerm ) break;
122210    }
122211    if( j<0 ){
122212      if( pTerm->truthProb<=0 ){
122213        /* If a truth probability is specified using the likelihood() hints,
122214        ** then use the probability provided by the application. */
122215        pLoop->nOut += pTerm->truthProb;
122216      }else{
122217        /* In the absence of explicit truth probabilities, use heuristics to
122218        ** guess a reasonable truth probability. */
122219        pLoop->nOut--;
122220        if( pTerm->eOperator&(WO_EQ|WO_IS) ){
122221          Expr *pRight = pTerm->pExpr->pRight;
122222          testcase( pTerm->pExpr->op==TK_IS );
122223          if( sqlite3ExprIsInteger(pRight, &k) && k>=(-1) && k<=1 ){
122224            k = 10;
122225          }else{
122226            k = 20;
122227          }
122228          if( iReduce<k ) iReduce = k;
122229        }
122230      }
122231    }
122232  }
122233  if( pLoop->nOut > nRow-iReduce )  pLoop->nOut = nRow - iReduce;
122234}
122235
122236/*
122237** Adjust the cost C by the costMult facter T.  This only occurs if
122238** compiled with -DSQLITE_ENABLE_COSTMULT
122239*/
122240#ifdef SQLITE_ENABLE_COSTMULT
122241# define ApplyCostMultiplier(C,T)  C += T
122242#else
122243# define ApplyCostMultiplier(C,T)
122244#endif
122245
122246/*
122247** We have so far matched pBuilder->pNew->u.btree.nEq terms of the
122248** index pIndex. Try to match one more.
122249**
122250** When this function is called, pBuilder->pNew->nOut contains the
122251** number of rows expected to be visited by filtering using the nEq
122252** terms only. If it is modified, this value is restored before this
122253** function returns.
122254**
122255** If pProbe->tnum==0, that means pIndex is a fake index used for the
122256** INTEGER PRIMARY KEY.
122257*/
122258static int whereLoopAddBtreeIndex(
122259  WhereLoopBuilder *pBuilder,     /* The WhereLoop factory */
122260  struct SrcList_item *pSrc,      /* FROM clause term being analyzed */
122261  Index *pProbe,                  /* An index on pSrc */
122262  LogEst nInMul                   /* log(Number of iterations due to IN) */
122263){
122264  WhereInfo *pWInfo = pBuilder->pWInfo;  /* WHERE analyse context */
122265  Parse *pParse = pWInfo->pParse;        /* Parsing context */
122266  sqlite3 *db = pParse->db;       /* Database connection malloc context */
122267  WhereLoop *pNew;                /* Template WhereLoop under construction */
122268  WhereTerm *pTerm;               /* A WhereTerm under consideration */
122269  int opMask;                     /* Valid operators for constraints */
122270  WhereScan scan;                 /* Iterator for WHERE terms */
122271  Bitmask saved_prereq;           /* Original value of pNew->prereq */
122272  u16 saved_nLTerm;               /* Original value of pNew->nLTerm */
122273  u16 saved_nEq;                  /* Original value of pNew->u.btree.nEq */
122274  u16 saved_nSkip;                /* Original value of pNew->nSkip */
122275  u32 saved_wsFlags;              /* Original value of pNew->wsFlags */
122276  LogEst saved_nOut;              /* Original value of pNew->nOut */
122277  int iCol;                       /* Index of the column in the table */
122278  int rc = SQLITE_OK;             /* Return code */
122279  LogEst rSize;                   /* Number of rows in the table */
122280  LogEst rLogSize;                /* Logarithm of table size */
122281  WhereTerm *pTop = 0, *pBtm = 0; /* Top and bottom range constraints */
122282
122283  pNew = pBuilder->pNew;
122284  if( db->mallocFailed ) return SQLITE_NOMEM;
122285
122286  assert( (pNew->wsFlags & WHERE_VIRTUALTABLE)==0 );
122287  assert( (pNew->wsFlags & WHERE_TOP_LIMIT)==0 );
122288  if( pNew->wsFlags & WHERE_BTM_LIMIT ){
122289    opMask = WO_LT|WO_LE;
122290  }else if( /*pProbe->tnum<=0 ||*/ (pSrc->jointype & JT_LEFT)!=0 ){
122291    opMask = WO_EQ|WO_IN|WO_GT|WO_GE|WO_LT|WO_LE;
122292  }else{
122293    opMask = WO_EQ|WO_IN|WO_GT|WO_GE|WO_LT|WO_LE|WO_ISNULL|WO_IS;
122294  }
122295  if( pProbe->bUnordered ) opMask &= ~(WO_GT|WO_GE|WO_LT|WO_LE);
122296
122297  assert( pNew->u.btree.nEq<pProbe->nColumn );
122298  iCol = pProbe->aiColumn[pNew->u.btree.nEq];
122299
122300  pTerm = whereScanInit(&scan, pBuilder->pWC, pSrc->iCursor, iCol,
122301                        opMask, pProbe);
122302  saved_nEq = pNew->u.btree.nEq;
122303  saved_nSkip = pNew->nSkip;
122304  saved_nLTerm = pNew->nLTerm;
122305  saved_wsFlags = pNew->wsFlags;
122306  saved_prereq = pNew->prereq;
122307  saved_nOut = pNew->nOut;
122308  pNew->rSetup = 0;
122309  rSize = pProbe->aiRowLogEst[0];
122310  rLogSize = estLog(rSize);
122311  for(; rc==SQLITE_OK && pTerm!=0; pTerm = whereScanNext(&scan)){
122312    u16 eOp = pTerm->eOperator;   /* Shorthand for pTerm->eOperator */
122313    LogEst rCostIdx;
122314    LogEst nOutUnadjusted;        /* nOut before IN() and WHERE adjustments */
122315    int nIn = 0;
122316#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
122317    int nRecValid = pBuilder->nRecValid;
122318#endif
122319    if( (eOp==WO_ISNULL || (pTerm->wtFlags&TERM_VNULL)!=0)
122320     && (iCol<0 || pSrc->pTab->aCol[iCol].notNull)
122321    ){
122322      continue; /* ignore IS [NOT] NULL constraints on NOT NULL columns */
122323    }
122324    if( pTerm->prereqRight & pNew->maskSelf ) continue;
122325
122326    /* Do not allow the upper bound of a LIKE optimization range constraint
122327    ** to mix with a lower range bound from some other source */
122328    if( pTerm->wtFlags & TERM_LIKEOPT && pTerm->eOperator==WO_LT ) continue;
122329
122330    pNew->wsFlags = saved_wsFlags;
122331    pNew->u.btree.nEq = saved_nEq;
122332    pNew->nLTerm = saved_nLTerm;
122333    if( whereLoopResize(db, pNew, pNew->nLTerm+1) ) break; /* OOM */
122334    pNew->aLTerm[pNew->nLTerm++] = pTerm;
122335    pNew->prereq = (saved_prereq | pTerm->prereqRight) & ~pNew->maskSelf;
122336
122337    assert( nInMul==0
122338        || (pNew->wsFlags & WHERE_COLUMN_NULL)!=0
122339        || (pNew->wsFlags & WHERE_COLUMN_IN)!=0
122340        || (pNew->wsFlags & WHERE_SKIPSCAN)!=0
122341    );
122342
122343    if( eOp & WO_IN ){
122344      Expr *pExpr = pTerm->pExpr;
122345      pNew->wsFlags |= WHERE_COLUMN_IN;
122346      if( ExprHasProperty(pExpr, EP_xIsSelect) ){
122347        /* "x IN (SELECT ...)":  TUNING: the SELECT returns 25 rows */
122348        nIn = 46;  assert( 46==sqlite3LogEst(25) );
122349      }else if( ALWAYS(pExpr->x.pList && pExpr->x.pList->nExpr) ){
122350        /* "x IN (value, value, ...)" */
122351        nIn = sqlite3LogEst(pExpr->x.pList->nExpr);
122352      }
122353      assert( nIn>0 );  /* RHS always has 2 or more terms...  The parser
122354                        ** changes "x IN (?)" into "x=?". */
122355
122356    }else if( eOp & (WO_EQ|WO_IS) ){
122357      pNew->wsFlags |= WHERE_COLUMN_EQ;
122358      if( iCol<0 || (nInMul==0 && pNew->u.btree.nEq==pProbe->nKeyCol-1) ){
122359        if( iCol>=0 && pProbe->uniqNotNull==0 ){
122360          pNew->wsFlags |= WHERE_UNQ_WANTED;
122361        }else{
122362          pNew->wsFlags |= WHERE_ONEROW;
122363        }
122364      }
122365    }else if( eOp & WO_ISNULL ){
122366      pNew->wsFlags |= WHERE_COLUMN_NULL;
122367    }else if( eOp & (WO_GT|WO_GE) ){
122368      testcase( eOp & WO_GT );
122369      testcase( eOp & WO_GE );
122370      pNew->wsFlags |= WHERE_COLUMN_RANGE|WHERE_BTM_LIMIT;
122371      pBtm = pTerm;
122372      pTop = 0;
122373      if( pTerm->wtFlags & TERM_LIKEOPT ){
122374        /* Range contraints that come from the LIKE optimization are
122375        ** always used in pairs. */
122376        pTop = &pTerm[1];
122377        assert( (pTop-(pTerm->pWC->a))<pTerm->pWC->nTerm );
122378        assert( pTop->wtFlags & TERM_LIKEOPT );
122379        assert( pTop->eOperator==WO_LT );
122380        if( whereLoopResize(db, pNew, pNew->nLTerm+1) ) break; /* OOM */
122381        pNew->aLTerm[pNew->nLTerm++] = pTop;
122382        pNew->wsFlags |= WHERE_TOP_LIMIT;
122383      }
122384    }else{
122385      assert( eOp & (WO_LT|WO_LE) );
122386      testcase( eOp & WO_LT );
122387      testcase( eOp & WO_LE );
122388      pNew->wsFlags |= WHERE_COLUMN_RANGE|WHERE_TOP_LIMIT;
122389      pTop = pTerm;
122390      pBtm = (pNew->wsFlags & WHERE_BTM_LIMIT)!=0 ?
122391                     pNew->aLTerm[pNew->nLTerm-2] : 0;
122392    }
122393
122394    /* At this point pNew->nOut is set to the number of rows expected to
122395    ** be visited by the index scan before considering term pTerm, or the
122396    ** values of nIn and nInMul. In other words, assuming that all
122397    ** "x IN(...)" terms are replaced with "x = ?". This block updates
122398    ** the value of pNew->nOut to account for pTerm (but not nIn/nInMul).  */
122399    assert( pNew->nOut==saved_nOut );
122400    if( pNew->wsFlags & WHERE_COLUMN_RANGE ){
122401      /* Adjust nOut using stat3/stat4 data. Or, if there is no stat3/stat4
122402      ** data, using some other estimate.  */
122403      whereRangeScanEst(pParse, pBuilder, pBtm, pTop, pNew);
122404    }else{
122405      int nEq = ++pNew->u.btree.nEq;
122406      assert( eOp & (WO_ISNULL|WO_EQ|WO_IN|WO_IS) );
122407
122408      assert( pNew->nOut==saved_nOut );
122409      if( pTerm->truthProb<=0 && iCol>=0 ){
122410        assert( (eOp & WO_IN) || nIn==0 );
122411        testcase( eOp & WO_IN );
122412        pNew->nOut += pTerm->truthProb;
122413        pNew->nOut -= nIn;
122414      }else{
122415#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
122416        tRowcnt nOut = 0;
122417        if( nInMul==0
122418         && pProbe->nSample
122419         && pNew->u.btree.nEq<=pProbe->nSampleCol
122420         && ((eOp & WO_IN)==0 || !ExprHasProperty(pTerm->pExpr, EP_xIsSelect))
122421        ){
122422          Expr *pExpr = pTerm->pExpr;
122423          if( (eOp & (WO_EQ|WO_ISNULL|WO_IS))!=0 ){
122424            testcase( eOp & WO_EQ );
122425            testcase( eOp & WO_IS );
122426            testcase( eOp & WO_ISNULL );
122427            rc = whereEqualScanEst(pParse, pBuilder, pExpr->pRight, &nOut);
122428          }else{
122429            rc = whereInScanEst(pParse, pBuilder, pExpr->x.pList, &nOut);
122430          }
122431          if( rc==SQLITE_NOTFOUND ) rc = SQLITE_OK;
122432          if( rc!=SQLITE_OK ) break;          /* Jump out of the pTerm loop */
122433          if( nOut ){
122434            pNew->nOut = sqlite3LogEst(nOut);
122435            if( pNew->nOut>saved_nOut ) pNew->nOut = saved_nOut;
122436            pNew->nOut -= nIn;
122437          }
122438        }
122439        if( nOut==0 )
122440#endif
122441        {
122442          pNew->nOut += (pProbe->aiRowLogEst[nEq] - pProbe->aiRowLogEst[nEq-1]);
122443          if( eOp & WO_ISNULL ){
122444            /* TUNING: If there is no likelihood() value, assume that a
122445            ** "col IS NULL" expression matches twice as many rows
122446            ** as (col=?). */
122447            pNew->nOut += 10;
122448          }
122449        }
122450      }
122451    }
122452
122453    /* Set rCostIdx to the cost of visiting selected rows in index. Add
122454    ** it to pNew->rRun, which is currently set to the cost of the index
122455    ** seek only. Then, if this is a non-covering index, add the cost of
122456    ** visiting the rows in the main table.  */
122457    rCostIdx = pNew->nOut + 1 + (15*pProbe->szIdxRow)/pSrc->pTab->szTabRow;
122458    pNew->rRun = sqlite3LogEstAdd(rLogSize, rCostIdx);
122459    if( (pNew->wsFlags & (WHERE_IDX_ONLY|WHERE_IPK))==0 ){
122460      pNew->rRun = sqlite3LogEstAdd(pNew->rRun, pNew->nOut + 16);
122461    }
122462    ApplyCostMultiplier(pNew->rRun, pProbe->pTable->costMult);
122463
122464    nOutUnadjusted = pNew->nOut;
122465    pNew->rRun += nInMul + nIn;
122466    pNew->nOut += nInMul + nIn;
122467    whereLoopOutputAdjust(pBuilder->pWC, pNew, rSize);
122468    rc = whereLoopInsert(pBuilder, pNew);
122469
122470    if( pNew->wsFlags & WHERE_COLUMN_RANGE ){
122471      pNew->nOut = saved_nOut;
122472    }else{
122473      pNew->nOut = nOutUnadjusted;
122474    }
122475
122476    if( (pNew->wsFlags & WHERE_TOP_LIMIT)==0
122477     && pNew->u.btree.nEq<pProbe->nColumn
122478    ){
122479      whereLoopAddBtreeIndex(pBuilder, pSrc, pProbe, nInMul+nIn);
122480    }
122481    pNew->nOut = saved_nOut;
122482#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
122483    pBuilder->nRecValid = nRecValid;
122484#endif
122485  }
122486  pNew->prereq = saved_prereq;
122487  pNew->u.btree.nEq = saved_nEq;
122488  pNew->nSkip = saved_nSkip;
122489  pNew->wsFlags = saved_wsFlags;
122490  pNew->nOut = saved_nOut;
122491  pNew->nLTerm = saved_nLTerm;
122492
122493  /* Consider using a skip-scan if there are no WHERE clause constraints
122494  ** available for the left-most terms of the index, and if the average
122495  ** number of repeats in the left-most terms is at least 18.
122496  **
122497  ** The magic number 18 is selected on the basis that scanning 17 rows
122498  ** is almost always quicker than an index seek (even though if the index
122499  ** contains fewer than 2^17 rows we assume otherwise in other parts of
122500  ** the code). And, even if it is not, it should not be too much slower.
122501  ** On the other hand, the extra seeks could end up being significantly
122502  ** more expensive.  */
122503  assert( 42==sqlite3LogEst(18) );
122504  if( saved_nEq==saved_nSkip
122505   && saved_nEq+1<pProbe->nKeyCol
122506   && pProbe->noSkipScan==0
122507   && pProbe->aiRowLogEst[saved_nEq+1]>=42  /* TUNING: Minimum for skip-scan */
122508   && (rc = whereLoopResize(db, pNew, pNew->nLTerm+1))==SQLITE_OK
122509  ){
122510    LogEst nIter;
122511    pNew->u.btree.nEq++;
122512    pNew->nSkip++;
122513    pNew->aLTerm[pNew->nLTerm++] = 0;
122514    pNew->wsFlags |= WHERE_SKIPSCAN;
122515    nIter = pProbe->aiRowLogEst[saved_nEq] - pProbe->aiRowLogEst[saved_nEq+1];
122516    pNew->nOut -= nIter;
122517    /* TUNING:  Because uncertainties in the estimates for skip-scan queries,
122518    ** add a 1.375 fudge factor to make skip-scan slightly less likely. */
122519    nIter += 5;
122520    whereLoopAddBtreeIndex(pBuilder, pSrc, pProbe, nIter + nInMul);
122521    pNew->nOut = saved_nOut;
122522    pNew->u.btree.nEq = saved_nEq;
122523    pNew->nSkip = saved_nSkip;
122524    pNew->wsFlags = saved_wsFlags;
122525  }
122526
122527  return rc;
122528}
122529
122530/*
122531** Return True if it is possible that pIndex might be useful in
122532** implementing the ORDER BY clause in pBuilder.
122533**
122534** Return False if pBuilder does not contain an ORDER BY clause or
122535** if there is no way for pIndex to be useful in implementing that
122536** ORDER BY clause.
122537*/
122538static int indexMightHelpWithOrderBy(
122539  WhereLoopBuilder *pBuilder,
122540  Index *pIndex,
122541  int iCursor
122542){
122543  ExprList *pOB;
122544  int ii, jj;
122545
122546  if( pIndex->bUnordered ) return 0;
122547  if( (pOB = pBuilder->pWInfo->pOrderBy)==0 ) return 0;
122548  for(ii=0; ii<pOB->nExpr; ii++){
122549    Expr *pExpr = sqlite3ExprSkipCollate(pOB->a[ii].pExpr);
122550    if( pExpr->op!=TK_COLUMN ) return 0;
122551    if( pExpr->iTable==iCursor ){
122552      if( pExpr->iColumn<0 ) return 1;
122553      for(jj=0; jj<pIndex->nKeyCol; jj++){
122554        if( pExpr->iColumn==pIndex->aiColumn[jj] ) return 1;
122555      }
122556    }
122557  }
122558  return 0;
122559}
122560
122561/*
122562** Return a bitmask where 1s indicate that the corresponding column of
122563** the table is used by an index.  Only the first 63 columns are considered.
122564*/
122565static Bitmask columnsInIndex(Index *pIdx){
122566  Bitmask m = 0;
122567  int j;
122568  for(j=pIdx->nColumn-1; j>=0; j--){
122569    int x = pIdx->aiColumn[j];
122570    if( x>=0 ){
122571      testcase( x==BMS-1 );
122572      testcase( x==BMS-2 );
122573      if( x<BMS-1 ) m |= MASKBIT(x);
122574    }
122575  }
122576  return m;
122577}
122578
122579/* Check to see if a partial index with pPartIndexWhere can be used
122580** in the current query.  Return true if it can be and false if not.
122581*/
122582static int whereUsablePartialIndex(int iTab, WhereClause *pWC, Expr *pWhere){
122583  int i;
122584  WhereTerm *pTerm;
122585  for(i=0, pTerm=pWC->a; i<pWC->nTerm; i++, pTerm++){
122586    Expr *pExpr = pTerm->pExpr;
122587    if( sqlite3ExprImpliesExpr(pExpr, pWhere, iTab)
122588     && (!ExprHasProperty(pExpr, EP_FromJoin) || pExpr->iRightJoinTable==iTab)
122589    ){
122590      return 1;
122591    }
122592  }
122593  return 0;
122594}
122595
122596/*
122597** Add all WhereLoop objects for a single table of the join where the table
122598** is idenfied by pBuilder->pNew->iTab.  That table is guaranteed to be
122599** a b-tree table, not a virtual table.
122600**
122601** The costs (WhereLoop.rRun) of the b-tree loops added by this function
122602** are calculated as follows:
122603**
122604** For a full scan, assuming the table (or index) contains nRow rows:
122605**
122606**     cost = nRow * 3.0                    // full-table scan
122607**     cost = nRow * K                      // scan of covering index
122608**     cost = nRow * (K+3.0)                // scan of non-covering index
122609**
122610** where K is a value between 1.1 and 3.0 set based on the relative
122611** estimated average size of the index and table records.
122612**
122613** For an index scan, where nVisit is the number of index rows visited
122614** by the scan, and nSeek is the number of seek operations required on
122615** the index b-tree:
122616**
122617**     cost = nSeek * (log(nRow) + K * nVisit)          // covering index
122618**     cost = nSeek * (log(nRow) + (K+3.0) * nVisit)    // non-covering index
122619**
122620** Normally, nSeek is 1. nSeek values greater than 1 come about if the
122621** WHERE clause includes "x IN (....)" terms used in place of "x=?". Or when
122622** implicit "x IN (SELECT x FROM tbl)" terms are added for skip-scans.
122623**
122624** The estimated values (nRow, nVisit, nSeek) often contain a large amount
122625** of uncertainty.  For this reason, scoring is designed to pick plans that
122626** "do the least harm" if the estimates are inaccurate.  For example, a
122627** log(nRow) factor is omitted from a non-covering index scan in order to
122628** bias the scoring in favor of using an index, since the worst-case
122629** performance of using an index is far better than the worst-case performance
122630** of a full table scan.
122631*/
122632static int whereLoopAddBtree(
122633  WhereLoopBuilder *pBuilder, /* WHERE clause information */
122634  Bitmask mExtra              /* Extra prerequesites for using this table */
122635){
122636  WhereInfo *pWInfo;          /* WHERE analysis context */
122637  Index *pProbe;              /* An index we are evaluating */
122638  Index sPk;                  /* A fake index object for the primary key */
122639  LogEst aiRowEstPk[2];       /* The aiRowLogEst[] value for the sPk index */
122640  i16 aiColumnPk = -1;        /* The aColumn[] value for the sPk index */
122641  SrcList *pTabList;          /* The FROM clause */
122642  struct SrcList_item *pSrc;  /* The FROM clause btree term to add */
122643  WhereLoop *pNew;            /* Template WhereLoop object */
122644  int rc = SQLITE_OK;         /* Return code */
122645  int iSortIdx = 1;           /* Index number */
122646  int b;                      /* A boolean value */
122647  LogEst rSize;               /* number of rows in the table */
122648  LogEst rLogSize;            /* Logarithm of the number of rows in the table */
122649  WhereClause *pWC;           /* The parsed WHERE clause */
122650  Table *pTab;                /* Table being queried */
122651
122652  pNew = pBuilder->pNew;
122653  pWInfo = pBuilder->pWInfo;
122654  pTabList = pWInfo->pTabList;
122655  pSrc = pTabList->a + pNew->iTab;
122656  pTab = pSrc->pTab;
122657  pWC = pBuilder->pWC;
122658  assert( !IsVirtual(pSrc->pTab) );
122659
122660  if( pSrc->pIndex ){
122661    /* An INDEXED BY clause specifies a particular index to use */
122662    pProbe = pSrc->pIndex;
122663  }else if( !HasRowid(pTab) ){
122664    pProbe = pTab->pIndex;
122665  }else{
122666    /* There is no INDEXED BY clause.  Create a fake Index object in local
122667    ** variable sPk to represent the rowid primary key index.  Make this
122668    ** fake index the first in a chain of Index objects with all of the real
122669    ** indices to follow */
122670    Index *pFirst;                  /* First of real indices on the table */
122671    memset(&sPk, 0, sizeof(Index));
122672    sPk.nKeyCol = 1;
122673    sPk.nColumn = 1;
122674    sPk.aiColumn = &aiColumnPk;
122675    sPk.aiRowLogEst = aiRowEstPk;
122676    sPk.onError = OE_Replace;
122677    sPk.pTable = pTab;
122678    sPk.szIdxRow = pTab->szTabRow;
122679    aiRowEstPk[0] = pTab->nRowLogEst;
122680    aiRowEstPk[1] = 0;
122681    pFirst = pSrc->pTab->pIndex;
122682    if( pSrc->notIndexed==0 ){
122683      /* The real indices of the table are only considered if the
122684      ** NOT INDEXED qualifier is omitted from the FROM clause */
122685      sPk.pNext = pFirst;
122686    }
122687    pProbe = &sPk;
122688  }
122689  rSize = pTab->nRowLogEst;
122690  rLogSize = estLog(rSize);
122691
122692#ifndef SQLITE_OMIT_AUTOMATIC_INDEX
122693  /* Automatic indexes */
122694  if( !pBuilder->pOrSet   /* Not part of an OR optimization */
122695   && (pWInfo->wctrlFlags & WHERE_NO_AUTOINDEX)==0
122696   && (pWInfo->pParse->db->flags & SQLITE_AutoIndex)!=0
122697   && pSrc->pIndex==0     /* Has no INDEXED BY clause */
122698   && !pSrc->notIndexed   /* Has no NOT INDEXED clause */
122699   && HasRowid(pTab)      /* Is not a WITHOUT ROWID table. (FIXME: Why not?) */
122700   && !pSrc->isCorrelated /* Not a correlated subquery */
122701   && !pSrc->isRecursive  /* Not a recursive common table expression. */
122702  ){
122703    /* Generate auto-index WhereLoops */
122704    WhereTerm *pTerm;
122705    WhereTerm *pWCEnd = pWC->a + pWC->nTerm;
122706    for(pTerm=pWC->a; rc==SQLITE_OK && pTerm<pWCEnd; pTerm++){
122707      if( pTerm->prereqRight & pNew->maskSelf ) continue;
122708      if( termCanDriveIndex(pTerm, pSrc, 0) ){
122709        pNew->u.btree.nEq = 1;
122710        pNew->nSkip = 0;
122711        pNew->u.btree.pIndex = 0;
122712        pNew->nLTerm = 1;
122713        pNew->aLTerm[0] = pTerm;
122714        /* TUNING: One-time cost for computing the automatic index is
122715        ** estimated to be X*N*log2(N) where N is the number of rows in
122716        ** the table being indexed and where X is 7 (LogEst=28) for normal
122717        ** tables or 1.375 (LogEst=4) for views and subqueries.  The value
122718        ** of X is smaller for views and subqueries so that the query planner
122719        ** will be more aggressive about generating automatic indexes for
122720        ** those objects, since there is no opportunity to add schema
122721        ** indexes on subqueries and views. */
122722        pNew->rSetup = rLogSize + rSize + 4;
122723        if( pTab->pSelect==0 && (pTab->tabFlags & TF_Ephemeral)==0 ){
122724          pNew->rSetup += 24;
122725        }
122726        ApplyCostMultiplier(pNew->rSetup, pTab->costMult);
122727        /* TUNING: Each index lookup yields 20 rows in the table.  This
122728        ** is more than the usual guess of 10 rows, since we have no way
122729        ** of knowing how selective the index will ultimately be.  It would
122730        ** not be unreasonable to make this value much larger. */
122731        pNew->nOut = 43;  assert( 43==sqlite3LogEst(20) );
122732        pNew->rRun = sqlite3LogEstAdd(rLogSize,pNew->nOut);
122733        pNew->wsFlags = WHERE_AUTO_INDEX;
122734        pNew->prereq = mExtra | pTerm->prereqRight;
122735        rc = whereLoopInsert(pBuilder, pNew);
122736      }
122737    }
122738  }
122739#endif /* SQLITE_OMIT_AUTOMATIC_INDEX */
122740
122741  /* Loop over all indices
122742  */
122743  for(; rc==SQLITE_OK && pProbe; pProbe=pProbe->pNext, iSortIdx++){
122744    if( pProbe->pPartIdxWhere!=0
122745     && !whereUsablePartialIndex(pSrc->iCursor, pWC, pProbe->pPartIdxWhere) ){
122746      testcase( pNew->iTab!=pSrc->iCursor );  /* See ticket [98d973b8f5] */
122747      continue;  /* Partial index inappropriate for this query */
122748    }
122749    rSize = pProbe->aiRowLogEst[0];
122750    pNew->u.btree.nEq = 0;
122751    pNew->nSkip = 0;
122752    pNew->nLTerm = 0;
122753    pNew->iSortIdx = 0;
122754    pNew->rSetup = 0;
122755    pNew->prereq = mExtra;
122756    pNew->nOut = rSize;
122757    pNew->u.btree.pIndex = pProbe;
122758    b = indexMightHelpWithOrderBy(pBuilder, pProbe, pSrc->iCursor);
122759    /* The ONEPASS_DESIRED flags never occurs together with ORDER BY */
122760    assert( (pWInfo->wctrlFlags & WHERE_ONEPASS_DESIRED)==0 || b==0 );
122761    if( pProbe->tnum<=0 ){
122762      /* Integer primary key index */
122763      pNew->wsFlags = WHERE_IPK;
122764
122765      /* Full table scan */
122766      pNew->iSortIdx = b ? iSortIdx : 0;
122767      /* TUNING: Cost of full table scan is (N*3.0). */
122768      pNew->rRun = rSize + 16;
122769      ApplyCostMultiplier(pNew->rRun, pTab->costMult);
122770      whereLoopOutputAdjust(pWC, pNew, rSize);
122771      rc = whereLoopInsert(pBuilder, pNew);
122772      pNew->nOut = rSize;
122773      if( rc ) break;
122774    }else{
122775      Bitmask m;
122776      if( pProbe->isCovering ){
122777        pNew->wsFlags = WHERE_IDX_ONLY | WHERE_INDEXED;
122778        m = 0;
122779      }else{
122780        m = pSrc->colUsed & ~columnsInIndex(pProbe);
122781        pNew->wsFlags = (m==0) ? (WHERE_IDX_ONLY|WHERE_INDEXED) : WHERE_INDEXED;
122782      }
122783
122784      /* Full scan via index */
122785      if( b
122786       || !HasRowid(pTab)
122787       || ( m==0
122788         && pProbe->bUnordered==0
122789         && (pProbe->szIdxRow<pTab->szTabRow)
122790         && (pWInfo->wctrlFlags & WHERE_ONEPASS_DESIRED)==0
122791         && sqlite3GlobalConfig.bUseCis
122792         && OptimizationEnabled(pWInfo->pParse->db, SQLITE_CoverIdxScan)
122793          )
122794      ){
122795        pNew->iSortIdx = b ? iSortIdx : 0;
122796
122797        /* The cost of visiting the index rows is N*K, where K is
122798        ** between 1.1 and 3.0, depending on the relative sizes of the
122799        ** index and table rows. If this is a non-covering index scan,
122800        ** also add the cost of visiting table rows (N*3.0).  */
122801        pNew->rRun = rSize + 1 + (15*pProbe->szIdxRow)/pTab->szTabRow;
122802        if( m!=0 ){
122803          pNew->rRun = sqlite3LogEstAdd(pNew->rRun, rSize+16);
122804        }
122805        ApplyCostMultiplier(pNew->rRun, pTab->costMult);
122806        whereLoopOutputAdjust(pWC, pNew, rSize);
122807        rc = whereLoopInsert(pBuilder, pNew);
122808        pNew->nOut = rSize;
122809        if( rc ) break;
122810      }
122811    }
122812
122813    rc = whereLoopAddBtreeIndex(pBuilder, pSrc, pProbe, 0);
122814#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
122815    sqlite3Stat4ProbeFree(pBuilder->pRec);
122816    pBuilder->nRecValid = 0;
122817    pBuilder->pRec = 0;
122818#endif
122819
122820    /* If there was an INDEXED BY clause, then only that one index is
122821    ** considered. */
122822    if( pSrc->pIndex ) break;
122823  }
122824  return rc;
122825}
122826
122827#ifndef SQLITE_OMIT_VIRTUALTABLE
122828/*
122829** Add all WhereLoop objects for a table of the join identified by
122830** pBuilder->pNew->iTab.  That table is guaranteed to be a virtual table.
122831**
122832** If there are no LEFT or CROSS JOIN joins in the query, both mExtra and
122833** mUnusable are set to 0. Otherwise, mExtra is a mask of all FROM clause
122834** entries that occur before the virtual table in the FROM clause and are
122835** separated from it by at least one LEFT or CROSS JOIN. Similarly, the
122836** mUnusable mask contains all FROM clause entries that occur after the
122837** virtual table and are separated from it by at least one LEFT or
122838** CROSS JOIN.
122839**
122840** For example, if the query were:
122841**
122842**   ... FROM t1, t2 LEFT JOIN t3, t4, vt CROSS JOIN t5, t6;
122843**
122844** then mExtra corresponds to (t1, t2) and mUnusable to (t5, t6).
122845**
122846** All the tables in mExtra must be scanned before the current virtual
122847** table. So any terms for which all prerequisites are satisfied by
122848** mExtra may be specified as "usable" in all calls to xBestIndex.
122849** Conversely, all tables in mUnusable must be scanned after the current
122850** virtual table, so any terms for which the prerequisites overlap with
122851** mUnusable should always be configured as "not-usable" for xBestIndex.
122852*/
122853static int whereLoopAddVirtual(
122854  WhereLoopBuilder *pBuilder,  /* WHERE clause information */
122855  Bitmask mExtra,              /* Tables that must be scanned before this one */
122856  Bitmask mUnusable            /* Tables that must be scanned after this one */
122857){
122858  WhereInfo *pWInfo;           /* WHERE analysis context */
122859  Parse *pParse;               /* The parsing context */
122860  WhereClause *pWC;            /* The WHERE clause */
122861  struct SrcList_item *pSrc;   /* The FROM clause term to search */
122862  Table *pTab;
122863  sqlite3 *db;
122864  sqlite3_index_info *pIdxInfo;
122865  struct sqlite3_index_constraint *pIdxCons;
122866  struct sqlite3_index_constraint_usage *pUsage;
122867  WhereTerm *pTerm;
122868  int i, j;
122869  int iTerm, mxTerm;
122870  int nConstraint;
122871  int seenIn = 0;              /* True if an IN operator is seen */
122872  int seenVar = 0;             /* True if a non-constant constraint is seen */
122873  int iPhase;                  /* 0: const w/o IN, 1: const, 2: no IN,  2: IN */
122874  WhereLoop *pNew;
122875  int rc = SQLITE_OK;
122876
122877  assert( (mExtra & mUnusable)==0 );
122878  pWInfo = pBuilder->pWInfo;
122879  pParse = pWInfo->pParse;
122880  db = pParse->db;
122881  pWC = pBuilder->pWC;
122882  pNew = pBuilder->pNew;
122883  pSrc = &pWInfo->pTabList->a[pNew->iTab];
122884  pTab = pSrc->pTab;
122885  assert( IsVirtual(pTab) );
122886  pIdxInfo = allocateIndexInfo(pParse, pWC, mUnusable, pSrc,pBuilder->pOrderBy);
122887  if( pIdxInfo==0 ) return SQLITE_NOMEM;
122888  pNew->prereq = 0;
122889  pNew->rSetup = 0;
122890  pNew->wsFlags = WHERE_VIRTUALTABLE;
122891  pNew->nLTerm = 0;
122892  pNew->u.vtab.needFree = 0;
122893  pUsage = pIdxInfo->aConstraintUsage;
122894  nConstraint = pIdxInfo->nConstraint;
122895  if( whereLoopResize(db, pNew, nConstraint) ){
122896    sqlite3DbFree(db, pIdxInfo);
122897    return SQLITE_NOMEM;
122898  }
122899
122900  for(iPhase=0; iPhase<=3; iPhase++){
122901    if( !seenIn && (iPhase&1)!=0 ){
122902      iPhase++;
122903      if( iPhase>3 ) break;
122904    }
122905    if( !seenVar && iPhase>1 ) break;
122906    pIdxCons = *(struct sqlite3_index_constraint**)&pIdxInfo->aConstraint;
122907    for(i=0; i<pIdxInfo->nConstraint; i++, pIdxCons++){
122908      j = pIdxCons->iTermOffset;
122909      pTerm = &pWC->a[j];
122910      switch( iPhase ){
122911        case 0:    /* Constants without IN operator */
122912          pIdxCons->usable = 0;
122913          if( (pTerm->eOperator & WO_IN)!=0 ){
122914            seenIn = 1;
122915          }
122916          if( (pTerm->prereqRight & ~mExtra)!=0 ){
122917            seenVar = 1;
122918          }else if( (pTerm->eOperator & WO_IN)==0 ){
122919            pIdxCons->usable = 1;
122920          }
122921          break;
122922        case 1:    /* Constants with IN operators */
122923          assert( seenIn );
122924          pIdxCons->usable = (pTerm->prereqRight & ~mExtra)==0;
122925          break;
122926        case 2:    /* Variables without IN */
122927          assert( seenVar );
122928          pIdxCons->usable = (pTerm->eOperator & WO_IN)==0;
122929          break;
122930        default:   /* Variables with IN */
122931          assert( seenVar && seenIn );
122932          pIdxCons->usable = 1;
122933          break;
122934      }
122935    }
122936    memset(pUsage, 0, sizeof(pUsage[0])*pIdxInfo->nConstraint);
122937    if( pIdxInfo->needToFreeIdxStr ) sqlite3_free(pIdxInfo->idxStr);
122938    pIdxInfo->idxStr = 0;
122939    pIdxInfo->idxNum = 0;
122940    pIdxInfo->needToFreeIdxStr = 0;
122941    pIdxInfo->orderByConsumed = 0;
122942    pIdxInfo->estimatedCost = SQLITE_BIG_DBL / (double)2;
122943    pIdxInfo->estimatedRows = 25;
122944    rc = vtabBestIndex(pParse, pTab, pIdxInfo);
122945    if( rc ) goto whereLoopAddVtab_exit;
122946    pIdxCons = *(struct sqlite3_index_constraint**)&pIdxInfo->aConstraint;
122947    pNew->prereq = mExtra;
122948    mxTerm = -1;
122949    assert( pNew->nLSlot>=nConstraint );
122950    for(i=0; i<nConstraint; i++) pNew->aLTerm[i] = 0;
122951    pNew->u.vtab.omitMask = 0;
122952    for(i=0; i<nConstraint; i++, pIdxCons++){
122953      if( (iTerm = pUsage[i].argvIndex - 1)>=0 ){
122954        j = pIdxCons->iTermOffset;
122955        if( iTerm>=nConstraint
122956         || j<0
122957         || j>=pWC->nTerm
122958         || pNew->aLTerm[iTerm]!=0
122959        ){
122960          rc = SQLITE_ERROR;
122961          sqlite3ErrorMsg(pParse, "%s.xBestIndex() malfunction", pTab->zName);
122962          goto whereLoopAddVtab_exit;
122963        }
122964        testcase( iTerm==nConstraint-1 );
122965        testcase( j==0 );
122966        testcase( j==pWC->nTerm-1 );
122967        pTerm = &pWC->a[j];
122968        pNew->prereq |= pTerm->prereqRight;
122969        assert( iTerm<pNew->nLSlot );
122970        pNew->aLTerm[iTerm] = pTerm;
122971        if( iTerm>mxTerm ) mxTerm = iTerm;
122972        testcase( iTerm==15 );
122973        testcase( iTerm==16 );
122974        if( iTerm<16 && pUsage[i].omit ) pNew->u.vtab.omitMask |= 1<<iTerm;
122975        if( (pTerm->eOperator & WO_IN)!=0 ){
122976          if( pUsage[i].omit==0 ){
122977            /* Do not attempt to use an IN constraint if the virtual table
122978            ** says that the equivalent EQ constraint cannot be safely omitted.
122979            ** If we do attempt to use such a constraint, some rows might be
122980            ** repeated in the output. */
122981            break;
122982          }
122983          /* A virtual table that is constrained by an IN clause may not
122984          ** consume the ORDER BY clause because (1) the order of IN terms
122985          ** is not necessarily related to the order of output terms and
122986          ** (2) Multiple outputs from a single IN value will not merge
122987          ** together.  */
122988          pIdxInfo->orderByConsumed = 0;
122989        }
122990      }
122991    }
122992    if( i>=nConstraint ){
122993      pNew->nLTerm = mxTerm+1;
122994      assert( pNew->nLTerm<=pNew->nLSlot );
122995      pNew->u.vtab.idxNum = pIdxInfo->idxNum;
122996      pNew->u.vtab.needFree = pIdxInfo->needToFreeIdxStr;
122997      pIdxInfo->needToFreeIdxStr = 0;
122998      pNew->u.vtab.idxStr = pIdxInfo->idxStr;
122999      pNew->u.vtab.isOrdered = (i8)(pIdxInfo->orderByConsumed ?
123000                                      pIdxInfo->nOrderBy : 0);
123001      pNew->rSetup = 0;
123002      pNew->rRun = sqlite3LogEstFromDouble(pIdxInfo->estimatedCost);
123003      pNew->nOut = sqlite3LogEst(pIdxInfo->estimatedRows);
123004      whereLoopInsert(pBuilder, pNew);
123005      if( pNew->u.vtab.needFree ){
123006        sqlite3_free(pNew->u.vtab.idxStr);
123007        pNew->u.vtab.needFree = 0;
123008      }
123009    }
123010  }
123011
123012whereLoopAddVtab_exit:
123013  if( pIdxInfo->needToFreeIdxStr ) sqlite3_free(pIdxInfo->idxStr);
123014  sqlite3DbFree(db, pIdxInfo);
123015  return rc;
123016}
123017#endif /* SQLITE_OMIT_VIRTUALTABLE */
123018
123019/*
123020** Add WhereLoop entries to handle OR terms.  This works for either
123021** btrees or virtual tables.
123022*/
123023static int whereLoopAddOr(
123024  WhereLoopBuilder *pBuilder,
123025  Bitmask mExtra,
123026  Bitmask mUnusable
123027){
123028  WhereInfo *pWInfo = pBuilder->pWInfo;
123029  WhereClause *pWC;
123030  WhereLoop *pNew;
123031  WhereTerm *pTerm, *pWCEnd;
123032  int rc = SQLITE_OK;
123033  int iCur;
123034  WhereClause tempWC;
123035  WhereLoopBuilder sSubBuild;
123036  WhereOrSet sSum, sCur;
123037  struct SrcList_item *pItem;
123038
123039  pWC = pBuilder->pWC;
123040  pWCEnd = pWC->a + pWC->nTerm;
123041  pNew = pBuilder->pNew;
123042  memset(&sSum, 0, sizeof(sSum));
123043  pItem = pWInfo->pTabList->a + pNew->iTab;
123044  iCur = pItem->iCursor;
123045
123046  for(pTerm=pWC->a; pTerm<pWCEnd && rc==SQLITE_OK; pTerm++){
123047    if( (pTerm->eOperator & WO_OR)!=0
123048     && (pTerm->u.pOrInfo->indexable & pNew->maskSelf)!=0
123049    ){
123050      WhereClause * const pOrWC = &pTerm->u.pOrInfo->wc;
123051      WhereTerm * const pOrWCEnd = &pOrWC->a[pOrWC->nTerm];
123052      WhereTerm *pOrTerm;
123053      int once = 1;
123054      int i, j;
123055
123056      sSubBuild = *pBuilder;
123057      sSubBuild.pOrderBy = 0;
123058      sSubBuild.pOrSet = &sCur;
123059
123060      WHERETRACE(0x200, ("Begin processing OR-clause %p\n", pTerm));
123061      for(pOrTerm=pOrWC->a; pOrTerm<pOrWCEnd; pOrTerm++){
123062        if( (pOrTerm->eOperator & WO_AND)!=0 ){
123063          sSubBuild.pWC = &pOrTerm->u.pAndInfo->wc;
123064        }else if( pOrTerm->leftCursor==iCur ){
123065          tempWC.pWInfo = pWC->pWInfo;
123066          tempWC.pOuter = pWC;
123067          tempWC.op = TK_AND;
123068          tempWC.nTerm = 1;
123069          tempWC.a = pOrTerm;
123070          sSubBuild.pWC = &tempWC;
123071        }else{
123072          continue;
123073        }
123074        sCur.n = 0;
123075#ifdef WHERETRACE_ENABLED
123076        WHERETRACE(0x200, ("OR-term %d of %p has %d subterms:\n",
123077                   (int)(pOrTerm-pOrWC->a), pTerm, sSubBuild.pWC->nTerm));
123078        if( sqlite3WhereTrace & 0x400 ){
123079          for(i=0; i<sSubBuild.pWC->nTerm; i++){
123080            whereTermPrint(&sSubBuild.pWC->a[i], i);
123081          }
123082        }
123083#endif
123084#ifndef SQLITE_OMIT_VIRTUALTABLE
123085        if( IsVirtual(pItem->pTab) ){
123086          rc = whereLoopAddVirtual(&sSubBuild, mExtra, mUnusable);
123087        }else
123088#endif
123089        {
123090          rc = whereLoopAddBtree(&sSubBuild, mExtra);
123091        }
123092        if( rc==SQLITE_OK ){
123093          rc = whereLoopAddOr(&sSubBuild, mExtra, mUnusable);
123094        }
123095        assert( rc==SQLITE_OK || sCur.n==0 );
123096        if( sCur.n==0 ){
123097          sSum.n = 0;
123098          break;
123099        }else if( once ){
123100          whereOrMove(&sSum, &sCur);
123101          once = 0;
123102        }else{
123103          WhereOrSet sPrev;
123104          whereOrMove(&sPrev, &sSum);
123105          sSum.n = 0;
123106          for(i=0; i<sPrev.n; i++){
123107            for(j=0; j<sCur.n; j++){
123108              whereOrInsert(&sSum, sPrev.a[i].prereq | sCur.a[j].prereq,
123109                            sqlite3LogEstAdd(sPrev.a[i].rRun, sCur.a[j].rRun),
123110                            sqlite3LogEstAdd(sPrev.a[i].nOut, sCur.a[j].nOut));
123111            }
123112          }
123113        }
123114      }
123115      pNew->nLTerm = 1;
123116      pNew->aLTerm[0] = pTerm;
123117      pNew->wsFlags = WHERE_MULTI_OR;
123118      pNew->rSetup = 0;
123119      pNew->iSortIdx = 0;
123120      memset(&pNew->u, 0, sizeof(pNew->u));
123121      for(i=0; rc==SQLITE_OK && i<sSum.n; i++){
123122        /* TUNING: Currently sSum.a[i].rRun is set to the sum of the costs
123123        ** of all sub-scans required by the OR-scan. However, due to rounding
123124        ** errors, it may be that the cost of the OR-scan is equal to its
123125        ** most expensive sub-scan. Add the smallest possible penalty
123126        ** (equivalent to multiplying the cost by 1.07) to ensure that
123127        ** this does not happen. Otherwise, for WHERE clauses such as the
123128        ** following where there is an index on "y":
123129        **
123130        **     WHERE likelihood(x=?, 0.99) OR y=?
123131        **
123132        ** the planner may elect to "OR" together a full-table scan and an
123133        ** index lookup. And other similarly odd results.  */
123134        pNew->rRun = sSum.a[i].rRun + 1;
123135        pNew->nOut = sSum.a[i].nOut;
123136        pNew->prereq = sSum.a[i].prereq;
123137        rc = whereLoopInsert(pBuilder, pNew);
123138      }
123139      WHERETRACE(0x200, ("End processing OR-clause %p\n", pTerm));
123140    }
123141  }
123142  return rc;
123143}
123144
123145/*
123146** Add all WhereLoop objects for all tables
123147*/
123148static int whereLoopAddAll(WhereLoopBuilder *pBuilder){
123149  WhereInfo *pWInfo = pBuilder->pWInfo;
123150  Bitmask mExtra = 0;
123151  Bitmask mPrior = 0;
123152  int iTab;
123153  SrcList *pTabList = pWInfo->pTabList;
123154  struct SrcList_item *pItem;
123155  struct SrcList_item *pEnd = &pTabList->a[pWInfo->nLevel];
123156  sqlite3 *db = pWInfo->pParse->db;
123157  int rc = SQLITE_OK;
123158  WhereLoop *pNew;
123159  u8 priorJointype = 0;
123160
123161  /* Loop over the tables in the join, from left to right */
123162  pNew = pBuilder->pNew;
123163  whereLoopInit(pNew);
123164  for(iTab=0, pItem=pTabList->a; pItem<pEnd; iTab++, pItem++){
123165    Bitmask mUnusable = 0;
123166    pNew->iTab = iTab;
123167    pNew->maskSelf = sqlite3WhereGetMask(&pWInfo->sMaskSet, pItem->iCursor);
123168    if( ((pItem->jointype|priorJointype) & (JT_LEFT|JT_CROSS))!=0 ){
123169      /* This condition is true when pItem is the FROM clause term on the
123170      ** right-hand-side of a LEFT or CROSS JOIN.  */
123171      mExtra = mPrior;
123172    }
123173    priorJointype = pItem->jointype;
123174    if( IsVirtual(pItem->pTab) ){
123175      struct SrcList_item *p;
123176      for(p=&pItem[1]; p<pEnd; p++){
123177        if( mUnusable || (p->jointype & (JT_LEFT|JT_CROSS)) ){
123178          mUnusable |= sqlite3WhereGetMask(&pWInfo->sMaskSet, p->iCursor);
123179        }
123180      }
123181      rc = whereLoopAddVirtual(pBuilder, mExtra, mUnusable);
123182    }else{
123183      rc = whereLoopAddBtree(pBuilder, mExtra);
123184    }
123185    if( rc==SQLITE_OK ){
123186      rc = whereLoopAddOr(pBuilder, mExtra, mUnusable);
123187    }
123188    mPrior |= pNew->maskSelf;
123189    if( rc || db->mallocFailed ) break;
123190  }
123191
123192  whereLoopClear(db, pNew);
123193  return rc;
123194}
123195
123196/*
123197** Examine a WherePath (with the addition of the extra WhereLoop of the 5th
123198** parameters) to see if it outputs rows in the requested ORDER BY
123199** (or GROUP BY) without requiring a separate sort operation.  Return N:
123200**
123201**   N>0:   N terms of the ORDER BY clause are satisfied
123202**   N==0:  No terms of the ORDER BY clause are satisfied
123203**   N<0:   Unknown yet how many terms of ORDER BY might be satisfied.
123204**
123205** Note that processing for WHERE_GROUPBY and WHERE_DISTINCTBY is not as
123206** strict.  With GROUP BY and DISTINCT the only requirement is that
123207** equivalent rows appear immediately adjacent to one another.  GROUP BY
123208** and DISTINCT do not require rows to appear in any particular order as long
123209** as equivalent rows are grouped together.  Thus for GROUP BY and DISTINCT
123210** the pOrderBy terms can be matched in any order.  With ORDER BY, the
123211** pOrderBy terms must be matched in strict left-to-right order.
123212*/
123213static i8 wherePathSatisfiesOrderBy(
123214  WhereInfo *pWInfo,    /* The WHERE clause */
123215  ExprList *pOrderBy,   /* ORDER BY or GROUP BY or DISTINCT clause to check */
123216  WherePath *pPath,     /* The WherePath to check */
123217  u16 wctrlFlags,       /* Might contain WHERE_GROUPBY or WHERE_DISTINCTBY */
123218  u16 nLoop,            /* Number of entries in pPath->aLoop[] */
123219  WhereLoop *pLast,     /* Add this WhereLoop to the end of pPath->aLoop[] */
123220  Bitmask *pRevMask     /* OUT: Mask of WhereLoops to run in reverse order */
123221){
123222  u8 revSet;            /* True if rev is known */
123223  u8 rev;               /* Composite sort order */
123224  u8 revIdx;            /* Index sort order */
123225  u8 isOrderDistinct;   /* All prior WhereLoops are order-distinct */
123226  u8 distinctColumns;   /* True if the loop has UNIQUE NOT NULL columns */
123227  u8 isMatch;           /* iColumn matches a term of the ORDER BY clause */
123228  u16 nKeyCol;          /* Number of key columns in pIndex */
123229  u16 nColumn;          /* Total number of ordered columns in the index */
123230  u16 nOrderBy;         /* Number terms in the ORDER BY clause */
123231  int iLoop;            /* Index of WhereLoop in pPath being processed */
123232  int i, j;             /* Loop counters */
123233  int iCur;             /* Cursor number for current WhereLoop */
123234  int iColumn;          /* A column number within table iCur */
123235  WhereLoop *pLoop = 0; /* Current WhereLoop being processed. */
123236  WhereTerm *pTerm;     /* A single term of the WHERE clause */
123237  Expr *pOBExpr;        /* An expression from the ORDER BY clause */
123238  CollSeq *pColl;       /* COLLATE function from an ORDER BY clause term */
123239  Index *pIndex;        /* The index associated with pLoop */
123240  sqlite3 *db = pWInfo->pParse->db;  /* Database connection */
123241  Bitmask obSat = 0;    /* Mask of ORDER BY terms satisfied so far */
123242  Bitmask obDone;       /* Mask of all ORDER BY terms */
123243  Bitmask orderDistinctMask;  /* Mask of all well-ordered loops */
123244  Bitmask ready;              /* Mask of inner loops */
123245
123246  /*
123247  ** We say the WhereLoop is "one-row" if it generates no more than one
123248  ** row of output.  A WhereLoop is one-row if all of the following are true:
123249  **  (a) All index columns match with WHERE_COLUMN_EQ.
123250  **  (b) The index is unique
123251  ** Any WhereLoop with an WHERE_COLUMN_EQ constraint on the rowid is one-row.
123252  ** Every one-row WhereLoop will have the WHERE_ONEROW bit set in wsFlags.
123253  **
123254  ** We say the WhereLoop is "order-distinct" if the set of columns from
123255  ** that WhereLoop that are in the ORDER BY clause are different for every
123256  ** row of the WhereLoop.  Every one-row WhereLoop is automatically
123257  ** order-distinct.   A WhereLoop that has no columns in the ORDER BY clause
123258  ** is not order-distinct. To be order-distinct is not quite the same as being
123259  ** UNIQUE since a UNIQUE column or index can have multiple rows that
123260  ** are NULL and NULL values are equivalent for the purpose of order-distinct.
123261  ** To be order-distinct, the columns must be UNIQUE and NOT NULL.
123262  **
123263  ** The rowid for a table is always UNIQUE and NOT NULL so whenever the
123264  ** rowid appears in the ORDER BY clause, the corresponding WhereLoop is
123265  ** automatically order-distinct.
123266  */
123267
123268  assert( pOrderBy!=0 );
123269  if( nLoop && OptimizationDisabled(db, SQLITE_OrderByIdxJoin) ) return 0;
123270
123271  nOrderBy = pOrderBy->nExpr;
123272  testcase( nOrderBy==BMS-1 );
123273  if( nOrderBy>BMS-1 ) return 0;  /* Cannot optimize overly large ORDER BYs */
123274  isOrderDistinct = 1;
123275  obDone = MASKBIT(nOrderBy)-1;
123276  orderDistinctMask = 0;
123277  ready = 0;
123278  for(iLoop=0; isOrderDistinct && obSat<obDone && iLoop<=nLoop; iLoop++){
123279    if( iLoop>0 ) ready |= pLoop->maskSelf;
123280    pLoop = iLoop<nLoop ? pPath->aLoop[iLoop] : pLast;
123281    if( pLoop->wsFlags & WHERE_VIRTUALTABLE ){
123282      if( pLoop->u.vtab.isOrdered ) obSat = obDone;
123283      break;
123284    }
123285    iCur = pWInfo->pTabList->a[pLoop->iTab].iCursor;
123286
123287    /* Mark off any ORDER BY term X that is a column in the table of
123288    ** the current loop for which there is term in the WHERE
123289    ** clause of the form X IS NULL or X=? that reference only outer
123290    ** loops.
123291    */
123292    for(i=0; i<nOrderBy; i++){
123293      if( MASKBIT(i) & obSat ) continue;
123294      pOBExpr = sqlite3ExprSkipCollate(pOrderBy->a[i].pExpr);
123295      if( pOBExpr->op!=TK_COLUMN ) continue;
123296      if( pOBExpr->iTable!=iCur ) continue;
123297      pTerm = sqlite3WhereFindTerm(&pWInfo->sWC, iCur, pOBExpr->iColumn,
123298                       ~ready, WO_EQ|WO_ISNULL|WO_IS, 0);
123299      if( pTerm==0 ) continue;
123300      if( (pTerm->eOperator&(WO_EQ|WO_IS))!=0 && pOBExpr->iColumn>=0 ){
123301        const char *z1, *z2;
123302        pColl = sqlite3ExprCollSeq(pWInfo->pParse, pOrderBy->a[i].pExpr);
123303        if( !pColl ) pColl = db->pDfltColl;
123304        z1 = pColl->zName;
123305        pColl = sqlite3ExprCollSeq(pWInfo->pParse, pTerm->pExpr);
123306        if( !pColl ) pColl = db->pDfltColl;
123307        z2 = pColl->zName;
123308        if( sqlite3StrICmp(z1, z2)!=0 ) continue;
123309        testcase( pTerm->pExpr->op==TK_IS );
123310      }
123311      obSat |= MASKBIT(i);
123312    }
123313
123314    if( (pLoop->wsFlags & WHERE_ONEROW)==0 ){
123315      if( pLoop->wsFlags & WHERE_IPK ){
123316        pIndex = 0;
123317        nKeyCol = 0;
123318        nColumn = 1;
123319      }else if( (pIndex = pLoop->u.btree.pIndex)==0 || pIndex->bUnordered ){
123320        return 0;
123321      }else{
123322        nKeyCol = pIndex->nKeyCol;
123323        nColumn = pIndex->nColumn;
123324        assert( nColumn==nKeyCol+1 || !HasRowid(pIndex->pTable) );
123325        assert( pIndex->aiColumn[nColumn-1]==(-1) || !HasRowid(pIndex->pTable));
123326        isOrderDistinct = IsUniqueIndex(pIndex);
123327      }
123328
123329      /* Loop through all columns of the index and deal with the ones
123330      ** that are not constrained by == or IN.
123331      */
123332      rev = revSet = 0;
123333      distinctColumns = 0;
123334      for(j=0; j<nColumn; j++){
123335        u8 bOnce;   /* True to run the ORDER BY search loop */
123336
123337        /* Skip over == and IS NULL terms */
123338        if( j<pLoop->u.btree.nEq
123339         && pLoop->nSkip==0
123340         && ((i = pLoop->aLTerm[j]->eOperator) & (WO_EQ|WO_ISNULL|WO_IS))!=0
123341        ){
123342          if( i & WO_ISNULL ){
123343            testcase( isOrderDistinct );
123344            isOrderDistinct = 0;
123345          }
123346          continue;
123347        }
123348
123349        /* Get the column number in the table (iColumn) and sort order
123350        ** (revIdx) for the j-th column of the index.
123351        */
123352        if( pIndex ){
123353          iColumn = pIndex->aiColumn[j];
123354          revIdx = pIndex->aSortOrder[j];
123355          if( iColumn==pIndex->pTable->iPKey ) iColumn = -1;
123356        }else{
123357          iColumn = -1;
123358          revIdx = 0;
123359        }
123360
123361        /* An unconstrained column that might be NULL means that this
123362        ** WhereLoop is not well-ordered
123363        */
123364        if( isOrderDistinct
123365         && iColumn>=0
123366         && j>=pLoop->u.btree.nEq
123367         && pIndex->pTable->aCol[iColumn].notNull==0
123368        ){
123369          isOrderDistinct = 0;
123370        }
123371
123372        /* Find the ORDER BY term that corresponds to the j-th column
123373        ** of the index and mark that ORDER BY term off
123374        */
123375        bOnce = 1;
123376        isMatch = 0;
123377        for(i=0; bOnce && i<nOrderBy; i++){
123378          if( MASKBIT(i) & obSat ) continue;
123379          pOBExpr = sqlite3ExprSkipCollate(pOrderBy->a[i].pExpr);
123380          testcase( wctrlFlags & WHERE_GROUPBY );
123381          testcase( wctrlFlags & WHERE_DISTINCTBY );
123382          if( (wctrlFlags & (WHERE_GROUPBY|WHERE_DISTINCTBY))==0 ) bOnce = 0;
123383          if( pOBExpr->op!=TK_COLUMN ) continue;
123384          if( pOBExpr->iTable!=iCur ) continue;
123385          if( pOBExpr->iColumn!=iColumn ) continue;
123386          if( iColumn>=0 ){
123387            pColl = sqlite3ExprCollSeq(pWInfo->pParse, pOrderBy->a[i].pExpr);
123388            if( !pColl ) pColl = db->pDfltColl;
123389            if( sqlite3StrICmp(pColl->zName, pIndex->azColl[j])!=0 ) continue;
123390          }
123391          isMatch = 1;
123392          break;
123393        }
123394        if( isMatch && (wctrlFlags & WHERE_GROUPBY)==0 ){
123395          /* Make sure the sort order is compatible in an ORDER BY clause.
123396          ** Sort order is irrelevant for a GROUP BY clause. */
123397          if( revSet ){
123398            if( (rev ^ revIdx)!=pOrderBy->a[i].sortOrder ) isMatch = 0;
123399          }else{
123400            rev = revIdx ^ pOrderBy->a[i].sortOrder;
123401            if( rev ) *pRevMask |= MASKBIT(iLoop);
123402            revSet = 1;
123403          }
123404        }
123405        if( isMatch ){
123406          if( iColumn<0 ){
123407            testcase( distinctColumns==0 );
123408            distinctColumns = 1;
123409          }
123410          obSat |= MASKBIT(i);
123411        }else{
123412          /* No match found */
123413          if( j==0 || j<nKeyCol ){
123414            testcase( isOrderDistinct!=0 );
123415            isOrderDistinct = 0;
123416          }
123417          break;
123418        }
123419      } /* end Loop over all index columns */
123420      if( distinctColumns ){
123421        testcase( isOrderDistinct==0 );
123422        isOrderDistinct = 1;
123423      }
123424    } /* end-if not one-row */
123425
123426    /* Mark off any other ORDER BY terms that reference pLoop */
123427    if( isOrderDistinct ){
123428      orderDistinctMask |= pLoop->maskSelf;
123429      for(i=0; i<nOrderBy; i++){
123430        Expr *p;
123431        Bitmask mTerm;
123432        if( MASKBIT(i) & obSat ) continue;
123433        p = pOrderBy->a[i].pExpr;
123434        mTerm = sqlite3WhereExprUsage(&pWInfo->sMaskSet,p);
123435        if( mTerm==0 && !sqlite3ExprIsConstant(p) ) continue;
123436        if( (mTerm&~orderDistinctMask)==0 ){
123437          obSat |= MASKBIT(i);
123438        }
123439      }
123440    }
123441  } /* End the loop over all WhereLoops from outer-most down to inner-most */
123442  if( obSat==obDone ) return (i8)nOrderBy;
123443  if( !isOrderDistinct ){
123444    for(i=nOrderBy-1; i>0; i--){
123445      Bitmask m = MASKBIT(i) - 1;
123446      if( (obSat&m)==m ) return i;
123447    }
123448    return 0;
123449  }
123450  return -1;
123451}
123452
123453
123454/*
123455** If the WHERE_GROUPBY flag is set in the mask passed to sqlite3WhereBegin(),
123456** the planner assumes that the specified pOrderBy list is actually a GROUP
123457** BY clause - and so any order that groups rows as required satisfies the
123458** request.
123459**
123460** Normally, in this case it is not possible for the caller to determine
123461** whether or not the rows are really being delivered in sorted order, or
123462** just in some other order that provides the required grouping. However,
123463** if the WHERE_SORTBYGROUP flag is also passed to sqlite3WhereBegin(), then
123464** this function may be called on the returned WhereInfo object. It returns
123465** true if the rows really will be sorted in the specified order, or false
123466** otherwise.
123467**
123468** For example, assuming:
123469**
123470**   CREATE INDEX i1 ON t1(x, Y);
123471**
123472** then
123473**
123474**   SELECT * FROM t1 GROUP BY x,y ORDER BY x,y;   -- IsSorted()==1
123475**   SELECT * FROM t1 GROUP BY y,x ORDER BY y,x;   -- IsSorted()==0
123476*/
123477SQLITE_PRIVATE int sqlite3WhereIsSorted(WhereInfo *pWInfo){
123478  assert( pWInfo->wctrlFlags & WHERE_GROUPBY );
123479  assert( pWInfo->wctrlFlags & WHERE_SORTBYGROUP );
123480  return pWInfo->sorted;
123481}
123482
123483#ifdef WHERETRACE_ENABLED
123484/* For debugging use only: */
123485static const char *wherePathName(WherePath *pPath, int nLoop, WhereLoop *pLast){
123486  static char zName[65];
123487  int i;
123488  for(i=0; i<nLoop; i++){ zName[i] = pPath->aLoop[i]->cId; }
123489  if( pLast ) zName[i++] = pLast->cId;
123490  zName[i] = 0;
123491  return zName;
123492}
123493#endif
123494
123495/*
123496** Return the cost of sorting nRow rows, assuming that the keys have
123497** nOrderby columns and that the first nSorted columns are already in
123498** order.
123499*/
123500static LogEst whereSortingCost(
123501  WhereInfo *pWInfo,
123502  LogEst nRow,
123503  int nOrderBy,
123504  int nSorted
123505){
123506  /* TUNING: Estimated cost of a full external sort, where N is
123507  ** the number of rows to sort is:
123508  **
123509  **   cost = (3.0 * N * log(N)).
123510  **
123511  ** Or, if the order-by clause has X terms but only the last Y
123512  ** terms are out of order, then block-sorting will reduce the
123513  ** sorting cost to:
123514  **
123515  **   cost = (3.0 * N * log(N)) * (Y/X)
123516  **
123517  ** The (Y/X) term is implemented using stack variable rScale
123518  ** below.  */
123519  LogEst rScale, rSortCost;
123520  assert( nOrderBy>0 && 66==sqlite3LogEst(100) );
123521  rScale = sqlite3LogEst((nOrderBy-nSorted)*100/nOrderBy) - 66;
123522  rSortCost = nRow + estLog(nRow) + rScale + 16;
123523
123524  /* TUNING: The cost of implementing DISTINCT using a B-TREE is
123525  ** similar but with a larger constant of proportionality.
123526  ** Multiply by an additional factor of 3.0.  */
123527  if( pWInfo->wctrlFlags & WHERE_WANT_DISTINCT ){
123528    rSortCost += 16;
123529  }
123530
123531  return rSortCost;
123532}
123533
123534/*
123535** Given the list of WhereLoop objects at pWInfo->pLoops, this routine
123536** attempts to find the lowest cost path that visits each WhereLoop
123537** once.  This path is then loaded into the pWInfo->a[].pWLoop fields.
123538**
123539** Assume that the total number of output rows that will need to be sorted
123540** will be nRowEst (in the 10*log2 representation).  Or, ignore sorting
123541** costs if nRowEst==0.
123542**
123543** Return SQLITE_OK on success or SQLITE_NOMEM of a memory allocation
123544** error occurs.
123545*/
123546static int wherePathSolver(WhereInfo *pWInfo, LogEst nRowEst){
123547  int mxChoice;             /* Maximum number of simultaneous paths tracked */
123548  int nLoop;                /* Number of terms in the join */
123549  Parse *pParse;            /* Parsing context */
123550  sqlite3 *db;              /* The database connection */
123551  int iLoop;                /* Loop counter over the terms of the join */
123552  int ii, jj;               /* Loop counters */
123553  int mxI = 0;              /* Index of next entry to replace */
123554  int nOrderBy;             /* Number of ORDER BY clause terms */
123555  LogEst mxCost = 0;        /* Maximum cost of a set of paths */
123556  LogEst mxUnsorted = 0;    /* Maximum unsorted cost of a set of path */
123557  int nTo, nFrom;           /* Number of valid entries in aTo[] and aFrom[] */
123558  WherePath *aFrom;         /* All nFrom paths at the previous level */
123559  WherePath *aTo;           /* The nTo best paths at the current level */
123560  WherePath *pFrom;         /* An element of aFrom[] that we are working on */
123561  WherePath *pTo;           /* An element of aTo[] that we are working on */
123562  WhereLoop *pWLoop;        /* One of the WhereLoop objects */
123563  WhereLoop **pX;           /* Used to divy up the pSpace memory */
123564  LogEst *aSortCost = 0;    /* Sorting and partial sorting costs */
123565  char *pSpace;             /* Temporary memory used by this routine */
123566  int nSpace;               /* Bytes of space allocated at pSpace */
123567
123568  pParse = pWInfo->pParse;
123569  db = pParse->db;
123570  nLoop = pWInfo->nLevel;
123571  /* TUNING: For simple queries, only the best path is tracked.
123572  ** For 2-way joins, the 5 best paths are followed.
123573  ** For joins of 3 or more tables, track the 10 best paths */
123574  mxChoice = (nLoop<=1) ? 1 : (nLoop==2 ? 5 : 10);
123575  assert( nLoop<=pWInfo->pTabList->nSrc );
123576  WHERETRACE(0x002, ("---- begin solver.  (nRowEst=%d)\n", nRowEst));
123577
123578  /* If nRowEst is zero and there is an ORDER BY clause, ignore it. In this
123579  ** case the purpose of this call is to estimate the number of rows returned
123580  ** by the overall query. Once this estimate has been obtained, the caller
123581  ** will invoke this function a second time, passing the estimate as the
123582  ** nRowEst parameter.  */
123583  if( pWInfo->pOrderBy==0 || nRowEst==0 ){
123584    nOrderBy = 0;
123585  }else{
123586    nOrderBy = pWInfo->pOrderBy->nExpr;
123587  }
123588
123589  /* Allocate and initialize space for aTo, aFrom and aSortCost[] */
123590  nSpace = (sizeof(WherePath)+sizeof(WhereLoop*)*nLoop)*mxChoice*2;
123591  nSpace += sizeof(LogEst) * nOrderBy;
123592  pSpace = sqlite3DbMallocRaw(db, nSpace);
123593  if( pSpace==0 ) return SQLITE_NOMEM;
123594  aTo = (WherePath*)pSpace;
123595  aFrom = aTo+mxChoice;
123596  memset(aFrom, 0, sizeof(aFrom[0]));
123597  pX = (WhereLoop**)(aFrom+mxChoice);
123598  for(ii=mxChoice*2, pFrom=aTo; ii>0; ii--, pFrom++, pX += nLoop){
123599    pFrom->aLoop = pX;
123600  }
123601  if( nOrderBy ){
123602    /* If there is an ORDER BY clause and it is not being ignored, set up
123603    ** space for the aSortCost[] array. Each element of the aSortCost array
123604    ** is either zero - meaning it has not yet been initialized - or the
123605    ** cost of sorting nRowEst rows of data where the first X terms of
123606    ** the ORDER BY clause are already in order, where X is the array
123607    ** index.  */
123608    aSortCost = (LogEst*)pX;
123609    memset(aSortCost, 0, sizeof(LogEst) * nOrderBy);
123610  }
123611  assert( aSortCost==0 || &pSpace[nSpace]==(char*)&aSortCost[nOrderBy] );
123612  assert( aSortCost!=0 || &pSpace[nSpace]==(char*)pX );
123613
123614  /* Seed the search with a single WherePath containing zero WhereLoops.
123615  **
123616  ** TUNING: Do not let the number of iterations go above 28.  If the cost
123617  ** of computing an automatic index is not paid back within the first 28
123618  ** rows, then do not use the automatic index. */
123619  aFrom[0].nRow = MIN(pParse->nQueryLoop, 48);  assert( 48==sqlite3LogEst(28) );
123620  nFrom = 1;
123621  assert( aFrom[0].isOrdered==0 );
123622  if( nOrderBy ){
123623    /* If nLoop is zero, then there are no FROM terms in the query. Since
123624    ** in this case the query may return a maximum of one row, the results
123625    ** are already in the requested order. Set isOrdered to nOrderBy to
123626    ** indicate this. Or, if nLoop is greater than zero, set isOrdered to
123627    ** -1, indicating that the result set may or may not be ordered,
123628    ** depending on the loops added to the current plan.  */
123629    aFrom[0].isOrdered = nLoop>0 ? -1 : nOrderBy;
123630  }
123631
123632  /* Compute successively longer WherePaths using the previous generation
123633  ** of WherePaths as the basis for the next.  Keep track of the mxChoice
123634  ** best paths at each generation */
123635  for(iLoop=0; iLoop<nLoop; iLoop++){
123636    nTo = 0;
123637    for(ii=0, pFrom=aFrom; ii<nFrom; ii++, pFrom++){
123638      for(pWLoop=pWInfo->pLoops; pWLoop; pWLoop=pWLoop->pNextLoop){
123639        LogEst nOut;                      /* Rows visited by (pFrom+pWLoop) */
123640        LogEst rCost;                     /* Cost of path (pFrom+pWLoop) */
123641        LogEst rUnsorted;                 /* Unsorted cost of (pFrom+pWLoop) */
123642        i8 isOrdered = pFrom->isOrdered;  /* isOrdered for (pFrom+pWLoop) */
123643        Bitmask maskNew;                  /* Mask of src visited by (..) */
123644        Bitmask revMask = 0;              /* Mask of rev-order loops for (..) */
123645
123646        if( (pWLoop->prereq & ~pFrom->maskLoop)!=0 ) continue;
123647        if( (pWLoop->maskSelf & pFrom->maskLoop)!=0 ) continue;
123648        /* At this point, pWLoop is a candidate to be the next loop.
123649        ** Compute its cost */
123650        rUnsorted = sqlite3LogEstAdd(pWLoop->rSetup,pWLoop->rRun + pFrom->nRow);
123651        rUnsorted = sqlite3LogEstAdd(rUnsorted, pFrom->rUnsorted);
123652        nOut = pFrom->nRow + pWLoop->nOut;
123653        maskNew = pFrom->maskLoop | pWLoop->maskSelf;
123654        if( isOrdered<0 ){
123655          isOrdered = wherePathSatisfiesOrderBy(pWInfo,
123656                       pWInfo->pOrderBy, pFrom, pWInfo->wctrlFlags,
123657                       iLoop, pWLoop, &revMask);
123658        }else{
123659          revMask = pFrom->revLoop;
123660        }
123661        if( isOrdered>=0 && isOrdered<nOrderBy ){
123662          if( aSortCost[isOrdered]==0 ){
123663            aSortCost[isOrdered] = whereSortingCost(
123664                pWInfo, nRowEst, nOrderBy, isOrdered
123665            );
123666          }
123667          rCost = sqlite3LogEstAdd(rUnsorted, aSortCost[isOrdered]);
123668
123669          WHERETRACE(0x002,
123670              ("---- sort cost=%-3d (%d/%d) increases cost %3d to %-3d\n",
123671               aSortCost[isOrdered], (nOrderBy-isOrdered), nOrderBy,
123672               rUnsorted, rCost));
123673        }else{
123674          rCost = rUnsorted;
123675        }
123676
123677        /* Check to see if pWLoop should be added to the set of
123678        ** mxChoice best-so-far paths.
123679        **
123680        ** First look for an existing path among best-so-far paths
123681        ** that covers the same set of loops and has the same isOrdered
123682        ** setting as the current path candidate.
123683        **
123684        ** The term "((pTo->isOrdered^isOrdered)&0x80)==0" is equivalent
123685        ** to (pTo->isOrdered==(-1))==(isOrdered==(-1))" for the range
123686        ** of legal values for isOrdered, -1..64.
123687        */
123688        for(jj=0, pTo=aTo; jj<nTo; jj++, pTo++){
123689          if( pTo->maskLoop==maskNew
123690           && ((pTo->isOrdered^isOrdered)&0x80)==0
123691          ){
123692            testcase( jj==nTo-1 );
123693            break;
123694          }
123695        }
123696        if( jj>=nTo ){
123697          /* None of the existing best-so-far paths match the candidate. */
123698          if( nTo>=mxChoice
123699           && (rCost>mxCost || (rCost==mxCost && rUnsorted>=mxUnsorted))
123700          ){
123701            /* The current candidate is no better than any of the mxChoice
123702            ** paths currently in the best-so-far buffer.  So discard
123703            ** this candidate as not viable. */
123704#ifdef WHERETRACE_ENABLED /* 0x4 */
123705            if( sqlite3WhereTrace&0x4 ){
123706              sqlite3DebugPrintf("Skip   %s cost=%-3d,%3d order=%c\n",
123707                  wherePathName(pFrom, iLoop, pWLoop), rCost, nOut,
123708                  isOrdered>=0 ? isOrdered+'0' : '?');
123709            }
123710#endif
123711            continue;
123712          }
123713          /* If we reach this points it means that the new candidate path
123714          ** needs to be added to the set of best-so-far paths. */
123715          if( nTo<mxChoice ){
123716            /* Increase the size of the aTo set by one */
123717            jj = nTo++;
123718          }else{
123719            /* New path replaces the prior worst to keep count below mxChoice */
123720            jj = mxI;
123721          }
123722          pTo = &aTo[jj];
123723#ifdef WHERETRACE_ENABLED /* 0x4 */
123724          if( sqlite3WhereTrace&0x4 ){
123725            sqlite3DebugPrintf("New    %s cost=%-3d,%3d order=%c\n",
123726                wherePathName(pFrom, iLoop, pWLoop), rCost, nOut,
123727                isOrdered>=0 ? isOrdered+'0' : '?');
123728          }
123729#endif
123730        }else{
123731          /* Control reaches here if best-so-far path pTo=aTo[jj] covers the
123732          ** same set of loops and has the sam isOrdered setting as the
123733          ** candidate path.  Check to see if the candidate should replace
123734          ** pTo or if the candidate should be skipped */
123735          if( pTo->rCost<rCost || (pTo->rCost==rCost && pTo->nRow<=nOut) ){
123736#ifdef WHERETRACE_ENABLED /* 0x4 */
123737            if( sqlite3WhereTrace&0x4 ){
123738              sqlite3DebugPrintf(
123739                  "Skip   %s cost=%-3d,%3d order=%c",
123740                  wherePathName(pFrom, iLoop, pWLoop), rCost, nOut,
123741                  isOrdered>=0 ? isOrdered+'0' : '?');
123742              sqlite3DebugPrintf("   vs %s cost=%-3d,%d order=%c\n",
123743                  wherePathName(pTo, iLoop+1, 0), pTo->rCost, pTo->nRow,
123744                  pTo->isOrdered>=0 ? pTo->isOrdered+'0' : '?');
123745            }
123746#endif
123747            /* Discard the candidate path from further consideration */
123748            testcase( pTo->rCost==rCost );
123749            continue;
123750          }
123751          testcase( pTo->rCost==rCost+1 );
123752          /* Control reaches here if the candidate path is better than the
123753          ** pTo path.  Replace pTo with the candidate. */
123754#ifdef WHERETRACE_ENABLED /* 0x4 */
123755          if( sqlite3WhereTrace&0x4 ){
123756            sqlite3DebugPrintf(
123757                "Update %s cost=%-3d,%3d order=%c",
123758                wherePathName(pFrom, iLoop, pWLoop), rCost, nOut,
123759                isOrdered>=0 ? isOrdered+'0' : '?');
123760            sqlite3DebugPrintf("  was %s cost=%-3d,%3d order=%c\n",
123761                wherePathName(pTo, iLoop+1, 0), pTo->rCost, pTo->nRow,
123762                pTo->isOrdered>=0 ? pTo->isOrdered+'0' : '?');
123763          }
123764#endif
123765        }
123766        /* pWLoop is a winner.  Add it to the set of best so far */
123767        pTo->maskLoop = pFrom->maskLoop | pWLoop->maskSelf;
123768        pTo->revLoop = revMask;
123769        pTo->nRow = nOut;
123770        pTo->rCost = rCost;
123771        pTo->rUnsorted = rUnsorted;
123772        pTo->isOrdered = isOrdered;
123773        memcpy(pTo->aLoop, pFrom->aLoop, sizeof(WhereLoop*)*iLoop);
123774        pTo->aLoop[iLoop] = pWLoop;
123775        if( nTo>=mxChoice ){
123776          mxI = 0;
123777          mxCost = aTo[0].rCost;
123778          mxUnsorted = aTo[0].nRow;
123779          for(jj=1, pTo=&aTo[1]; jj<mxChoice; jj++, pTo++){
123780            if( pTo->rCost>mxCost
123781             || (pTo->rCost==mxCost && pTo->rUnsorted>mxUnsorted)
123782            ){
123783              mxCost = pTo->rCost;
123784              mxUnsorted = pTo->rUnsorted;
123785              mxI = jj;
123786            }
123787          }
123788        }
123789      }
123790    }
123791
123792#ifdef WHERETRACE_ENABLED  /* >=2 */
123793    if( sqlite3WhereTrace & 0x02 ){
123794      sqlite3DebugPrintf("---- after round %d ----\n", iLoop);
123795      for(ii=0, pTo=aTo; ii<nTo; ii++, pTo++){
123796        sqlite3DebugPrintf(" %s cost=%-3d nrow=%-3d order=%c",
123797           wherePathName(pTo, iLoop+1, 0), pTo->rCost, pTo->nRow,
123798           pTo->isOrdered>=0 ? (pTo->isOrdered+'0') : '?');
123799        if( pTo->isOrdered>0 ){
123800          sqlite3DebugPrintf(" rev=0x%llx\n", pTo->revLoop);
123801        }else{
123802          sqlite3DebugPrintf("\n");
123803        }
123804      }
123805    }
123806#endif
123807
123808    /* Swap the roles of aFrom and aTo for the next generation */
123809    pFrom = aTo;
123810    aTo = aFrom;
123811    aFrom = pFrom;
123812    nFrom = nTo;
123813  }
123814
123815  if( nFrom==0 ){
123816    sqlite3ErrorMsg(pParse, "no query solution");
123817    sqlite3DbFree(db, pSpace);
123818    return SQLITE_ERROR;
123819  }
123820
123821  /* Find the lowest cost path.  pFrom will be left pointing to that path */
123822  pFrom = aFrom;
123823  for(ii=1; ii<nFrom; ii++){
123824    if( pFrom->rCost>aFrom[ii].rCost ) pFrom = &aFrom[ii];
123825  }
123826  assert( pWInfo->nLevel==nLoop );
123827  /* Load the lowest cost path into pWInfo */
123828  for(iLoop=0; iLoop<nLoop; iLoop++){
123829    WhereLevel *pLevel = pWInfo->a + iLoop;
123830    pLevel->pWLoop = pWLoop = pFrom->aLoop[iLoop];
123831    pLevel->iFrom = pWLoop->iTab;
123832    pLevel->iTabCur = pWInfo->pTabList->a[pLevel->iFrom].iCursor;
123833  }
123834  if( (pWInfo->wctrlFlags & WHERE_WANT_DISTINCT)!=0
123835   && (pWInfo->wctrlFlags & WHERE_DISTINCTBY)==0
123836   && pWInfo->eDistinct==WHERE_DISTINCT_NOOP
123837   && nRowEst
123838  ){
123839    Bitmask notUsed;
123840    int rc = wherePathSatisfiesOrderBy(pWInfo, pWInfo->pResultSet, pFrom,
123841                 WHERE_DISTINCTBY, nLoop-1, pFrom->aLoop[nLoop-1], &notUsed);
123842    if( rc==pWInfo->pResultSet->nExpr ){
123843      pWInfo->eDistinct = WHERE_DISTINCT_ORDERED;
123844    }
123845  }
123846  if( pWInfo->pOrderBy ){
123847    if( pWInfo->wctrlFlags & WHERE_DISTINCTBY ){
123848      if( pFrom->isOrdered==pWInfo->pOrderBy->nExpr ){
123849        pWInfo->eDistinct = WHERE_DISTINCT_ORDERED;
123850      }
123851    }else{
123852      pWInfo->nOBSat = pFrom->isOrdered;
123853      if( pWInfo->nOBSat<0 ) pWInfo->nOBSat = 0;
123854      pWInfo->revMask = pFrom->revLoop;
123855    }
123856    if( (pWInfo->wctrlFlags & WHERE_SORTBYGROUP)
123857        && pWInfo->nOBSat==pWInfo->pOrderBy->nExpr && nLoop>0
123858    ){
123859      Bitmask revMask = 0;
123860      int nOrder = wherePathSatisfiesOrderBy(pWInfo, pWInfo->pOrderBy,
123861          pFrom, 0, nLoop-1, pFrom->aLoop[nLoop-1], &revMask
123862      );
123863      assert( pWInfo->sorted==0 );
123864      if( nOrder==pWInfo->pOrderBy->nExpr ){
123865        pWInfo->sorted = 1;
123866        pWInfo->revMask = revMask;
123867      }
123868    }
123869  }
123870
123871
123872  pWInfo->nRowOut = pFrom->nRow;
123873
123874  /* Free temporary memory and return success */
123875  sqlite3DbFree(db, pSpace);
123876  return SQLITE_OK;
123877}
123878
123879/*
123880** Most queries use only a single table (they are not joins) and have
123881** simple == constraints against indexed fields.  This routine attempts
123882** to plan those simple cases using much less ceremony than the
123883** general-purpose query planner, and thereby yield faster sqlite3_prepare()
123884** times for the common case.
123885**
123886** Return non-zero on success, if this query can be handled by this
123887** no-frills query planner.  Return zero if this query needs the
123888** general-purpose query planner.
123889*/
123890static int whereShortCut(WhereLoopBuilder *pBuilder){
123891  WhereInfo *pWInfo;
123892  struct SrcList_item *pItem;
123893  WhereClause *pWC;
123894  WhereTerm *pTerm;
123895  WhereLoop *pLoop;
123896  int iCur;
123897  int j;
123898  Table *pTab;
123899  Index *pIdx;
123900
123901  pWInfo = pBuilder->pWInfo;
123902  if( pWInfo->wctrlFlags & WHERE_FORCE_TABLE ) return 0;
123903  assert( pWInfo->pTabList->nSrc>=1 );
123904  pItem = pWInfo->pTabList->a;
123905  pTab = pItem->pTab;
123906  if( IsVirtual(pTab) ) return 0;
123907  if( pItem->zIndexedBy ) return 0;
123908  iCur = pItem->iCursor;
123909  pWC = &pWInfo->sWC;
123910  pLoop = pBuilder->pNew;
123911  pLoop->wsFlags = 0;
123912  pLoop->nSkip = 0;
123913  pTerm = sqlite3WhereFindTerm(pWC, iCur, -1, 0, WO_EQ|WO_IS, 0);
123914  if( pTerm ){
123915    testcase( pTerm->eOperator & WO_IS );
123916    pLoop->wsFlags = WHERE_COLUMN_EQ|WHERE_IPK|WHERE_ONEROW;
123917    pLoop->aLTerm[0] = pTerm;
123918    pLoop->nLTerm = 1;
123919    pLoop->u.btree.nEq = 1;
123920    /* TUNING: Cost of a rowid lookup is 10 */
123921    pLoop->rRun = 33;  /* 33==sqlite3LogEst(10) */
123922  }else{
123923    for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
123924      int opMask;
123925      assert( pLoop->aLTermSpace==pLoop->aLTerm );
123926      if( !IsUniqueIndex(pIdx)
123927       || pIdx->pPartIdxWhere!=0
123928       || pIdx->nKeyCol>ArraySize(pLoop->aLTermSpace)
123929      ) continue;
123930      opMask = pIdx->uniqNotNull ? (WO_EQ|WO_IS) : WO_EQ;
123931      for(j=0; j<pIdx->nKeyCol; j++){
123932        pTerm = sqlite3WhereFindTerm(pWC, iCur, pIdx->aiColumn[j], 0, opMask, pIdx);
123933        if( pTerm==0 ) break;
123934        testcase( pTerm->eOperator & WO_IS );
123935        pLoop->aLTerm[j] = pTerm;
123936      }
123937      if( j!=pIdx->nKeyCol ) continue;
123938      pLoop->wsFlags = WHERE_COLUMN_EQ|WHERE_ONEROW|WHERE_INDEXED;
123939      if( pIdx->isCovering || (pItem->colUsed & ~columnsInIndex(pIdx))==0 ){
123940        pLoop->wsFlags |= WHERE_IDX_ONLY;
123941      }
123942      pLoop->nLTerm = j;
123943      pLoop->u.btree.nEq = j;
123944      pLoop->u.btree.pIndex = pIdx;
123945      /* TUNING: Cost of a unique index lookup is 15 */
123946      pLoop->rRun = 39;  /* 39==sqlite3LogEst(15) */
123947      break;
123948    }
123949  }
123950  if( pLoop->wsFlags ){
123951    pLoop->nOut = (LogEst)1;
123952    pWInfo->a[0].pWLoop = pLoop;
123953    pLoop->maskSelf = sqlite3WhereGetMask(&pWInfo->sMaskSet, iCur);
123954    pWInfo->a[0].iTabCur = iCur;
123955    pWInfo->nRowOut = 1;
123956    if( pWInfo->pOrderBy ) pWInfo->nOBSat =  pWInfo->pOrderBy->nExpr;
123957    if( pWInfo->wctrlFlags & WHERE_WANT_DISTINCT ){
123958      pWInfo->eDistinct = WHERE_DISTINCT_UNIQUE;
123959    }
123960#ifdef SQLITE_DEBUG
123961    pLoop->cId = '0';
123962#endif
123963    return 1;
123964  }
123965  return 0;
123966}
123967
123968/*
123969** Generate the beginning of the loop used for WHERE clause processing.
123970** The return value is a pointer to an opaque structure that contains
123971** information needed to terminate the loop.  Later, the calling routine
123972** should invoke sqlite3WhereEnd() with the return value of this function
123973** in order to complete the WHERE clause processing.
123974**
123975** If an error occurs, this routine returns NULL.
123976**
123977** The basic idea is to do a nested loop, one loop for each table in
123978** the FROM clause of a select.  (INSERT and UPDATE statements are the
123979** same as a SELECT with only a single table in the FROM clause.)  For
123980** example, if the SQL is this:
123981**
123982**       SELECT * FROM t1, t2, t3 WHERE ...;
123983**
123984** Then the code generated is conceptually like the following:
123985**
123986**      foreach row1 in t1 do       \    Code generated
123987**        foreach row2 in t2 do      |-- by sqlite3WhereBegin()
123988**          foreach row3 in t3 do   /
123989**            ...
123990**          end                     \    Code generated
123991**        end                        |-- by sqlite3WhereEnd()
123992**      end                         /
123993**
123994** Note that the loops might not be nested in the order in which they
123995** appear in the FROM clause if a different order is better able to make
123996** use of indices.  Note also that when the IN operator appears in
123997** the WHERE clause, it might result in additional nested loops for
123998** scanning through all values on the right-hand side of the IN.
123999**
124000** There are Btree cursors associated with each table.  t1 uses cursor
124001** number pTabList->a[0].iCursor.  t2 uses the cursor pTabList->a[1].iCursor.
124002** And so forth.  This routine generates code to open those VDBE cursors
124003** and sqlite3WhereEnd() generates the code to close them.
124004**
124005** The code that sqlite3WhereBegin() generates leaves the cursors named
124006** in pTabList pointing at their appropriate entries.  The [...] code
124007** can use OP_Column and OP_Rowid opcodes on these cursors to extract
124008** data from the various tables of the loop.
124009**
124010** If the WHERE clause is empty, the foreach loops must each scan their
124011** entire tables.  Thus a three-way join is an O(N^3) operation.  But if
124012** the tables have indices and there are terms in the WHERE clause that
124013** refer to those indices, a complete table scan can be avoided and the
124014** code will run much faster.  Most of the work of this routine is checking
124015** to see if there are indices that can be used to speed up the loop.
124016**
124017** Terms of the WHERE clause are also used to limit which rows actually
124018** make it to the "..." in the middle of the loop.  After each "foreach",
124019** terms of the WHERE clause that use only terms in that loop and outer
124020** loops are evaluated and if false a jump is made around all subsequent
124021** inner loops (or around the "..." if the test occurs within the inner-
124022** most loop)
124023**
124024** OUTER JOINS
124025**
124026** An outer join of tables t1 and t2 is conceptally coded as follows:
124027**
124028**    foreach row1 in t1 do
124029**      flag = 0
124030**      foreach row2 in t2 do
124031**        start:
124032**          ...
124033**          flag = 1
124034**      end
124035**      if flag==0 then
124036**        move the row2 cursor to a null row
124037**        goto start
124038**      fi
124039**    end
124040**
124041** ORDER BY CLAUSE PROCESSING
124042**
124043** pOrderBy is a pointer to the ORDER BY clause (or the GROUP BY clause
124044** if the WHERE_GROUPBY flag is set in wctrlFlags) of a SELECT statement
124045** if there is one.  If there is no ORDER BY clause or if this routine
124046** is called from an UPDATE or DELETE statement, then pOrderBy is NULL.
124047**
124048** The iIdxCur parameter is the cursor number of an index.  If
124049** WHERE_ONETABLE_ONLY is set, iIdxCur is the cursor number of an index
124050** to use for OR clause processing.  The WHERE clause should use this
124051** specific cursor.  If WHERE_ONEPASS_DESIRED is set, then iIdxCur is
124052** the first cursor in an array of cursors for all indices.  iIdxCur should
124053** be used to compute the appropriate cursor depending on which index is
124054** used.
124055*/
124056SQLITE_PRIVATE WhereInfo *sqlite3WhereBegin(
124057  Parse *pParse,        /* The parser context */
124058  SrcList *pTabList,    /* FROM clause: A list of all tables to be scanned */
124059  Expr *pWhere,         /* The WHERE clause */
124060  ExprList *pOrderBy,   /* An ORDER BY (or GROUP BY) clause, or NULL */
124061  ExprList *pResultSet, /* Result set of the query */
124062  u16 wctrlFlags,       /* One of the WHERE_* flags defined in sqliteInt.h */
124063  int iIdxCur           /* If WHERE_ONETABLE_ONLY is set, index cursor number */
124064){
124065  int nByteWInfo;            /* Num. bytes allocated for WhereInfo struct */
124066  int nTabList;              /* Number of elements in pTabList */
124067  WhereInfo *pWInfo;         /* Will become the return value of this function */
124068  Vdbe *v = pParse->pVdbe;   /* The virtual database engine */
124069  Bitmask notReady;          /* Cursors that are not yet positioned */
124070  WhereLoopBuilder sWLB;     /* The WhereLoop builder */
124071  WhereMaskSet *pMaskSet;    /* The expression mask set */
124072  WhereLevel *pLevel;        /* A single level in pWInfo->a[] */
124073  WhereLoop *pLoop;          /* Pointer to a single WhereLoop object */
124074  int ii;                    /* Loop counter */
124075  sqlite3 *db;               /* Database connection */
124076  int rc;                    /* Return code */
124077
124078
124079  /* Variable initialization */
124080  db = pParse->db;
124081  memset(&sWLB, 0, sizeof(sWLB));
124082
124083  /* An ORDER/GROUP BY clause of more than 63 terms cannot be optimized */
124084  testcase( pOrderBy && pOrderBy->nExpr==BMS-1 );
124085  if( pOrderBy && pOrderBy->nExpr>=BMS ) pOrderBy = 0;
124086  sWLB.pOrderBy = pOrderBy;
124087
124088  /* Disable the DISTINCT optimization if SQLITE_DistinctOpt is set via
124089  ** sqlite3_test_ctrl(SQLITE_TESTCTRL_OPTIMIZATIONS,...) */
124090  if( OptimizationDisabled(db, SQLITE_DistinctOpt) ){
124091    wctrlFlags &= ~WHERE_WANT_DISTINCT;
124092  }
124093
124094  /* The number of tables in the FROM clause is limited by the number of
124095  ** bits in a Bitmask
124096  */
124097  testcase( pTabList->nSrc==BMS );
124098  if( pTabList->nSrc>BMS ){
124099    sqlite3ErrorMsg(pParse, "at most %d tables in a join", BMS);
124100    return 0;
124101  }
124102
124103  /* This function normally generates a nested loop for all tables in
124104  ** pTabList.  But if the WHERE_ONETABLE_ONLY flag is set, then we should
124105  ** only generate code for the first table in pTabList and assume that
124106  ** any cursors associated with subsequent tables are uninitialized.
124107  */
124108  nTabList = (wctrlFlags & WHERE_ONETABLE_ONLY) ? 1 : pTabList->nSrc;
124109
124110  /* Allocate and initialize the WhereInfo structure that will become the
124111  ** return value. A single allocation is used to store the WhereInfo
124112  ** struct, the contents of WhereInfo.a[], the WhereClause structure
124113  ** and the WhereMaskSet structure. Since WhereClause contains an 8-byte
124114  ** field (type Bitmask) it must be aligned on an 8-byte boundary on
124115  ** some architectures. Hence the ROUND8() below.
124116  */
124117  nByteWInfo = ROUND8(sizeof(WhereInfo)+(nTabList-1)*sizeof(WhereLevel));
124118  pWInfo = sqlite3DbMallocZero(db, nByteWInfo + sizeof(WhereLoop));
124119  if( db->mallocFailed ){
124120    sqlite3DbFree(db, pWInfo);
124121    pWInfo = 0;
124122    goto whereBeginError;
124123  }
124124  pWInfo->aiCurOnePass[0] = pWInfo->aiCurOnePass[1] = -1;
124125  pWInfo->nLevel = nTabList;
124126  pWInfo->pParse = pParse;
124127  pWInfo->pTabList = pTabList;
124128  pWInfo->pOrderBy = pOrderBy;
124129  pWInfo->pResultSet = pResultSet;
124130  pWInfo->iBreak = pWInfo->iContinue = sqlite3VdbeMakeLabel(v);
124131  pWInfo->wctrlFlags = wctrlFlags;
124132  pWInfo->savedNQueryLoop = pParse->nQueryLoop;
124133  pMaskSet = &pWInfo->sMaskSet;
124134  sWLB.pWInfo = pWInfo;
124135  sWLB.pWC = &pWInfo->sWC;
124136  sWLB.pNew = (WhereLoop*)(((char*)pWInfo)+nByteWInfo);
124137  assert( EIGHT_BYTE_ALIGNMENT(sWLB.pNew) );
124138  whereLoopInit(sWLB.pNew);
124139#ifdef SQLITE_DEBUG
124140  sWLB.pNew->cId = '*';
124141#endif
124142
124143  /* Split the WHERE clause into separate subexpressions where each
124144  ** subexpression is separated by an AND operator.
124145  */
124146  initMaskSet(pMaskSet);
124147  sqlite3WhereClauseInit(&pWInfo->sWC, pWInfo);
124148  sqlite3WhereSplit(&pWInfo->sWC, pWhere, TK_AND);
124149
124150  /* Special case: a WHERE clause that is constant.  Evaluate the
124151  ** expression and either jump over all of the code or fall thru.
124152  */
124153  for(ii=0; ii<sWLB.pWC->nTerm; ii++){
124154    if( nTabList==0 || sqlite3ExprIsConstantNotJoin(sWLB.pWC->a[ii].pExpr) ){
124155      sqlite3ExprIfFalse(pParse, sWLB.pWC->a[ii].pExpr, pWInfo->iBreak,
124156                         SQLITE_JUMPIFNULL);
124157      sWLB.pWC->a[ii].wtFlags |= TERM_CODED;
124158    }
124159  }
124160
124161  /* Special case: No FROM clause
124162  */
124163  if( nTabList==0 ){
124164    if( pOrderBy ) pWInfo->nOBSat = pOrderBy->nExpr;
124165    if( wctrlFlags & WHERE_WANT_DISTINCT ){
124166      pWInfo->eDistinct = WHERE_DISTINCT_UNIQUE;
124167    }
124168  }
124169
124170  /* Assign a bit from the bitmask to every term in the FROM clause.
124171  **
124172  ** When assigning bitmask values to FROM clause cursors, it must be
124173  ** the case that if X is the bitmask for the N-th FROM clause term then
124174  ** the bitmask for all FROM clause terms to the left of the N-th term
124175  ** is (X-1).   An expression from the ON clause of a LEFT JOIN can use
124176  ** its Expr.iRightJoinTable value to find the bitmask of the right table
124177  ** of the join.  Subtracting one from the right table bitmask gives a
124178  ** bitmask for all tables to the left of the join.  Knowing the bitmask
124179  ** for all tables to the left of a left join is important.  Ticket #3015.
124180  **
124181  ** Note that bitmasks are created for all pTabList->nSrc tables in
124182  ** pTabList, not just the first nTabList tables.  nTabList is normally
124183  ** equal to pTabList->nSrc but might be shortened to 1 if the
124184  ** WHERE_ONETABLE_ONLY flag is set.
124185  */
124186  for(ii=0; ii<pTabList->nSrc; ii++){
124187    createMask(pMaskSet, pTabList->a[ii].iCursor);
124188  }
124189#ifndef NDEBUG
124190  {
124191    Bitmask toTheLeft = 0;
124192    for(ii=0; ii<pTabList->nSrc; ii++){
124193      Bitmask m = sqlite3WhereGetMask(pMaskSet, pTabList->a[ii].iCursor);
124194      assert( (m-1)==toTheLeft );
124195      toTheLeft |= m;
124196    }
124197  }
124198#endif
124199
124200  /* Analyze all of the subexpressions. */
124201  sqlite3WhereExprAnalyze(pTabList, &pWInfo->sWC);
124202  if( db->mallocFailed ) goto whereBeginError;
124203
124204  if( wctrlFlags & WHERE_WANT_DISTINCT ){
124205    if( isDistinctRedundant(pParse, pTabList, &pWInfo->sWC, pResultSet) ){
124206      /* The DISTINCT marking is pointless.  Ignore it. */
124207      pWInfo->eDistinct = WHERE_DISTINCT_UNIQUE;
124208    }else if( pOrderBy==0 ){
124209      /* Try to ORDER BY the result set to make distinct processing easier */
124210      pWInfo->wctrlFlags |= WHERE_DISTINCTBY;
124211      pWInfo->pOrderBy = pResultSet;
124212    }
124213  }
124214
124215  /* Construct the WhereLoop objects */
124216  WHERETRACE(0xffff,("*** Optimizer Start ***\n"));
124217#if defined(WHERETRACE_ENABLED)
124218  if( sqlite3WhereTrace & 0x100 ){ /* Display all terms of the WHERE clause */
124219    int i;
124220    for(i=0; i<sWLB.pWC->nTerm; i++){
124221      whereTermPrint(&sWLB.pWC->a[i], i);
124222    }
124223  }
124224#endif
124225
124226  if( nTabList!=1 || whereShortCut(&sWLB)==0 ){
124227    rc = whereLoopAddAll(&sWLB);
124228    if( rc ) goto whereBeginError;
124229
124230#ifdef WHERETRACE_ENABLED
124231    if( sqlite3WhereTrace ){    /* Display all of the WhereLoop objects */
124232      WhereLoop *p;
124233      int i;
124234      static const char zLabel[] = "0123456789abcdefghijklmnopqrstuvwyxz"
124235                                             "ABCDEFGHIJKLMNOPQRSTUVWYXZ";
124236      for(p=pWInfo->pLoops, i=0; p; p=p->pNextLoop, i++){
124237        p->cId = zLabel[i%sizeof(zLabel)];
124238        whereLoopPrint(p, sWLB.pWC);
124239      }
124240    }
124241#endif
124242
124243    wherePathSolver(pWInfo, 0);
124244    if( db->mallocFailed ) goto whereBeginError;
124245    if( pWInfo->pOrderBy ){
124246       wherePathSolver(pWInfo, pWInfo->nRowOut+1);
124247       if( db->mallocFailed ) goto whereBeginError;
124248    }
124249  }
124250  if( pWInfo->pOrderBy==0 && (db->flags & SQLITE_ReverseOrder)!=0 ){
124251     pWInfo->revMask = (Bitmask)(-1);
124252  }
124253  if( pParse->nErr || NEVER(db->mallocFailed) ){
124254    goto whereBeginError;
124255  }
124256#ifdef WHERETRACE_ENABLED
124257  if( sqlite3WhereTrace ){
124258    sqlite3DebugPrintf("---- Solution nRow=%d", pWInfo->nRowOut);
124259    if( pWInfo->nOBSat>0 ){
124260      sqlite3DebugPrintf(" ORDERBY=%d,0x%llx", pWInfo->nOBSat, pWInfo->revMask);
124261    }
124262    switch( pWInfo->eDistinct ){
124263      case WHERE_DISTINCT_UNIQUE: {
124264        sqlite3DebugPrintf("  DISTINCT=unique");
124265        break;
124266      }
124267      case WHERE_DISTINCT_ORDERED: {
124268        sqlite3DebugPrintf("  DISTINCT=ordered");
124269        break;
124270      }
124271      case WHERE_DISTINCT_UNORDERED: {
124272        sqlite3DebugPrintf("  DISTINCT=unordered");
124273        break;
124274      }
124275    }
124276    sqlite3DebugPrintf("\n");
124277    for(ii=0; ii<pWInfo->nLevel; ii++){
124278      whereLoopPrint(pWInfo->a[ii].pWLoop, sWLB.pWC);
124279    }
124280  }
124281#endif
124282  /* Attempt to omit tables from the join that do not effect the result */
124283  if( pWInfo->nLevel>=2
124284   && pResultSet!=0
124285   && OptimizationEnabled(db, SQLITE_OmitNoopJoin)
124286  ){
124287    Bitmask tabUsed = sqlite3WhereExprListUsage(pMaskSet, pResultSet);
124288    if( sWLB.pOrderBy ){
124289      tabUsed |= sqlite3WhereExprListUsage(pMaskSet, sWLB.pOrderBy);
124290    }
124291    while( pWInfo->nLevel>=2 ){
124292      WhereTerm *pTerm, *pEnd;
124293      pLoop = pWInfo->a[pWInfo->nLevel-1].pWLoop;
124294      if( (pWInfo->pTabList->a[pLoop->iTab].jointype & JT_LEFT)==0 ) break;
124295      if( (wctrlFlags & WHERE_WANT_DISTINCT)==0
124296       && (pLoop->wsFlags & WHERE_ONEROW)==0
124297      ){
124298        break;
124299      }
124300      if( (tabUsed & pLoop->maskSelf)!=0 ) break;
124301      pEnd = sWLB.pWC->a + sWLB.pWC->nTerm;
124302      for(pTerm=sWLB.pWC->a; pTerm<pEnd; pTerm++){
124303        if( (pTerm->prereqAll & pLoop->maskSelf)!=0
124304         && !ExprHasProperty(pTerm->pExpr, EP_FromJoin)
124305        ){
124306          break;
124307        }
124308      }
124309      if( pTerm<pEnd ) break;
124310      WHERETRACE(0xffff, ("-> drop loop %c not used\n", pLoop->cId));
124311      pWInfo->nLevel--;
124312      nTabList--;
124313    }
124314  }
124315  WHERETRACE(0xffff,("*** Optimizer Finished ***\n"));
124316  pWInfo->pParse->nQueryLoop += pWInfo->nRowOut;
124317
124318  /* If the caller is an UPDATE or DELETE statement that is requesting
124319  ** to use a one-pass algorithm, determine if this is appropriate.
124320  ** The one-pass algorithm only works if the WHERE clause constrains
124321  ** the statement to update or delete a single row.
124322  */
124323  assert( (wctrlFlags & WHERE_ONEPASS_DESIRED)==0 || pWInfo->nLevel==1 );
124324  if( (wctrlFlags & WHERE_ONEPASS_DESIRED)!=0
124325   && (pWInfo->a[0].pWLoop->wsFlags & WHERE_ONEROW)!=0 ){
124326    pWInfo->okOnePass = 1;
124327    if( HasRowid(pTabList->a[0].pTab) ){
124328      pWInfo->a[0].pWLoop->wsFlags &= ~WHERE_IDX_ONLY;
124329    }
124330  }
124331
124332  /* Open all tables in the pTabList and any indices selected for
124333  ** searching those tables.
124334  */
124335  for(ii=0, pLevel=pWInfo->a; ii<nTabList; ii++, pLevel++){
124336    Table *pTab;     /* Table to open */
124337    int iDb;         /* Index of database containing table/index */
124338    struct SrcList_item *pTabItem;
124339
124340    pTabItem = &pTabList->a[pLevel->iFrom];
124341    pTab = pTabItem->pTab;
124342    iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
124343    pLoop = pLevel->pWLoop;
124344    if( (pTab->tabFlags & TF_Ephemeral)!=0 || pTab->pSelect ){
124345      /* Do nothing */
124346    }else
124347#ifndef SQLITE_OMIT_VIRTUALTABLE
124348    if( (pLoop->wsFlags & WHERE_VIRTUALTABLE)!=0 ){
124349      const char *pVTab = (const char *)sqlite3GetVTable(db, pTab);
124350      int iCur = pTabItem->iCursor;
124351      sqlite3VdbeAddOp4(v, OP_VOpen, iCur, 0, 0, pVTab, P4_VTAB);
124352    }else if( IsVirtual(pTab) ){
124353      /* noop */
124354    }else
124355#endif
124356    if( (pLoop->wsFlags & WHERE_IDX_ONLY)==0
124357         && (wctrlFlags & WHERE_OMIT_OPEN_CLOSE)==0 ){
124358      int op = OP_OpenRead;
124359      if( pWInfo->okOnePass ){
124360        op = OP_OpenWrite;
124361        pWInfo->aiCurOnePass[0] = pTabItem->iCursor;
124362      };
124363      sqlite3OpenTable(pParse, pTabItem->iCursor, iDb, pTab, op);
124364      assert( pTabItem->iCursor==pLevel->iTabCur );
124365      testcase( !pWInfo->okOnePass && pTab->nCol==BMS-1 );
124366      testcase( !pWInfo->okOnePass && pTab->nCol==BMS );
124367      if( !pWInfo->okOnePass && pTab->nCol<BMS && HasRowid(pTab) ){
124368        Bitmask b = pTabItem->colUsed;
124369        int n = 0;
124370        for(; b; b=b>>1, n++){}
124371        sqlite3VdbeChangeP4(v, sqlite3VdbeCurrentAddr(v)-1,
124372                            SQLITE_INT_TO_PTR(n), P4_INT32);
124373        assert( n<=pTab->nCol );
124374      }
124375#ifdef SQLITE_ENABLE_COLUMN_USED_MASK
124376      sqlite3VdbeAddOp4Dup8(v, OP_ColumnsUsed, pTabItem->iCursor, 0, 0,
124377                            (const u8*)&pTabItem->colUsed, P4_INT64);
124378#endif
124379    }else{
124380      sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
124381    }
124382    if( pLoop->wsFlags & WHERE_INDEXED ){
124383      Index *pIx = pLoop->u.btree.pIndex;
124384      int iIndexCur;
124385      int op = OP_OpenRead;
124386      /* iIdxCur is always set if to a positive value if ONEPASS is possible */
124387      assert( iIdxCur!=0 || (pWInfo->wctrlFlags & WHERE_ONEPASS_DESIRED)==0 );
124388      if( !HasRowid(pTab) && IsPrimaryKeyIndex(pIx)
124389       && (wctrlFlags & WHERE_ONETABLE_ONLY)!=0
124390      ){
124391        /* This is one term of an OR-optimization using the PRIMARY KEY of a
124392        ** WITHOUT ROWID table.  No need for a separate index */
124393        iIndexCur = pLevel->iTabCur;
124394        op = 0;
124395      }else if( pWInfo->okOnePass ){
124396        Index *pJ = pTabItem->pTab->pIndex;
124397        iIndexCur = iIdxCur;
124398        assert( wctrlFlags & WHERE_ONEPASS_DESIRED );
124399        while( ALWAYS(pJ) && pJ!=pIx ){
124400          iIndexCur++;
124401          pJ = pJ->pNext;
124402        }
124403        op = OP_OpenWrite;
124404        pWInfo->aiCurOnePass[1] = iIndexCur;
124405      }else if( iIdxCur && (wctrlFlags & WHERE_ONETABLE_ONLY)!=0 ){
124406        iIndexCur = iIdxCur;
124407        if( wctrlFlags & WHERE_REOPEN_IDX ) op = OP_ReopenIdx;
124408      }else{
124409        iIndexCur = pParse->nTab++;
124410      }
124411      pLevel->iIdxCur = iIndexCur;
124412      assert( pIx->pSchema==pTab->pSchema );
124413      assert( iIndexCur>=0 );
124414      if( op ){
124415        sqlite3VdbeAddOp3(v, op, iIndexCur, pIx->tnum, iDb);
124416        sqlite3VdbeSetP4KeyInfo(pParse, pIx);
124417        if( (pLoop->wsFlags & WHERE_CONSTRAINT)!=0
124418         && (pLoop->wsFlags & (WHERE_COLUMN_RANGE|WHERE_SKIPSCAN))==0
124419         && (pWInfo->wctrlFlags&WHERE_ORDERBY_MIN)==0
124420        ){
124421          sqlite3VdbeChangeP5(v, OPFLAG_SEEKEQ); /* Hint to COMDB2 */
124422        }
124423        VdbeComment((v, "%s", pIx->zName));
124424#ifdef SQLITE_ENABLE_COLUMN_USED_MASK
124425        {
124426          u64 colUsed = 0;
124427          int ii, jj;
124428          for(ii=0; ii<pIx->nColumn; ii++){
124429            jj = pIx->aiColumn[ii];
124430            if( jj<0 ) continue;
124431            if( jj>63 ) jj = 63;
124432            if( (pTabItem->colUsed & MASKBIT(jj))==0 ) continue;
124433            colUsed |= ((u64)1)<<(ii<63 ? ii : 63);
124434          }
124435          sqlite3VdbeAddOp4Dup8(v, OP_ColumnsUsed, iIndexCur, 0, 0,
124436                                (u8*)&colUsed, P4_INT64);
124437        }
124438#endif /* SQLITE_ENABLE_COLUMN_USED_MASK */
124439      }
124440    }
124441    if( iDb>=0 ) sqlite3CodeVerifySchema(pParse, iDb);
124442  }
124443  pWInfo->iTop = sqlite3VdbeCurrentAddr(v);
124444  if( db->mallocFailed ) goto whereBeginError;
124445
124446  /* Generate the code to do the search.  Each iteration of the for
124447  ** loop below generates code for a single nested loop of the VM
124448  ** program.
124449  */
124450  notReady = ~(Bitmask)0;
124451  for(ii=0; ii<nTabList; ii++){
124452    int addrExplain;
124453    int wsFlags;
124454    pLevel = &pWInfo->a[ii];
124455    wsFlags = pLevel->pWLoop->wsFlags;
124456#ifndef SQLITE_OMIT_AUTOMATIC_INDEX
124457    if( (pLevel->pWLoop->wsFlags & WHERE_AUTO_INDEX)!=0 ){
124458      constructAutomaticIndex(pParse, &pWInfo->sWC,
124459                &pTabList->a[pLevel->iFrom], notReady, pLevel);
124460      if( db->mallocFailed ) goto whereBeginError;
124461    }
124462#endif
124463    addrExplain = sqlite3WhereExplainOneScan(
124464        pParse, pTabList, pLevel, ii, pLevel->iFrom, wctrlFlags
124465    );
124466    pLevel->addrBody = sqlite3VdbeCurrentAddr(v);
124467    notReady = sqlite3WhereCodeOneLoopStart(pWInfo, ii, notReady);
124468    pWInfo->iContinue = pLevel->addrCont;
124469    if( (wsFlags&WHERE_MULTI_OR)==0 && (wctrlFlags&WHERE_ONETABLE_ONLY)==0 ){
124470      sqlite3WhereAddScanStatus(v, pTabList, pLevel, addrExplain);
124471    }
124472  }
124473
124474  /* Done. */
124475  VdbeModuleComment((v, "Begin WHERE-core"));
124476  return pWInfo;
124477
124478  /* Jump here if malloc fails */
124479whereBeginError:
124480  if( pWInfo ){
124481    pParse->nQueryLoop = pWInfo->savedNQueryLoop;
124482    whereInfoFree(db, pWInfo);
124483  }
124484  return 0;
124485}
124486
124487/*
124488** Generate the end of the WHERE loop.  See comments on
124489** sqlite3WhereBegin() for additional information.
124490*/
124491SQLITE_PRIVATE void sqlite3WhereEnd(WhereInfo *pWInfo){
124492  Parse *pParse = pWInfo->pParse;
124493  Vdbe *v = pParse->pVdbe;
124494  int i;
124495  WhereLevel *pLevel;
124496  WhereLoop *pLoop;
124497  SrcList *pTabList = pWInfo->pTabList;
124498  sqlite3 *db = pParse->db;
124499
124500  /* Generate loop termination code.
124501  */
124502  VdbeModuleComment((v, "End WHERE-core"));
124503  sqlite3ExprCacheClear(pParse);
124504  for(i=pWInfo->nLevel-1; i>=0; i--){
124505    int addr;
124506    pLevel = &pWInfo->a[i];
124507    pLoop = pLevel->pWLoop;
124508    sqlite3VdbeResolveLabel(v, pLevel->addrCont);
124509    if( pLevel->op!=OP_Noop ){
124510      sqlite3VdbeAddOp3(v, pLevel->op, pLevel->p1, pLevel->p2, pLevel->p3);
124511      sqlite3VdbeChangeP5(v, pLevel->p5);
124512      VdbeCoverage(v);
124513      VdbeCoverageIf(v, pLevel->op==OP_Next);
124514      VdbeCoverageIf(v, pLevel->op==OP_Prev);
124515      VdbeCoverageIf(v, pLevel->op==OP_VNext);
124516    }
124517    if( pLoop->wsFlags & WHERE_IN_ABLE && pLevel->u.in.nIn>0 ){
124518      struct InLoop *pIn;
124519      int j;
124520      sqlite3VdbeResolveLabel(v, pLevel->addrNxt);
124521      for(j=pLevel->u.in.nIn, pIn=&pLevel->u.in.aInLoop[j-1]; j>0; j--, pIn--){
124522        sqlite3VdbeJumpHere(v, pIn->addrInTop+1);
124523        sqlite3VdbeAddOp2(v, pIn->eEndLoopOp, pIn->iCur, pIn->addrInTop);
124524        VdbeCoverage(v);
124525        VdbeCoverageIf(v, pIn->eEndLoopOp==OP_PrevIfOpen);
124526        VdbeCoverageIf(v, pIn->eEndLoopOp==OP_NextIfOpen);
124527        sqlite3VdbeJumpHere(v, pIn->addrInTop-1);
124528      }
124529    }
124530    sqlite3VdbeResolveLabel(v, pLevel->addrBrk);
124531    if( pLevel->addrSkip ){
124532      sqlite3VdbeAddOp2(v, OP_Goto, 0, pLevel->addrSkip);
124533      VdbeComment((v, "next skip-scan on %s", pLoop->u.btree.pIndex->zName));
124534      sqlite3VdbeJumpHere(v, pLevel->addrSkip);
124535      sqlite3VdbeJumpHere(v, pLevel->addrSkip-2);
124536    }
124537    if( pLevel->addrLikeRep ){
124538      int op;
124539      if( sqlite3VdbeGetOp(v, pLevel->addrLikeRep-1)->p1 ){
124540        op = OP_DecrJumpZero;
124541      }else{
124542        op = OP_JumpZeroIncr;
124543      }
124544      sqlite3VdbeAddOp2(v, op, pLevel->iLikeRepCntr, pLevel->addrLikeRep);
124545      VdbeCoverage(v);
124546    }
124547    if( pLevel->iLeftJoin ){
124548      addr = sqlite3VdbeAddOp1(v, OP_IfPos, pLevel->iLeftJoin); VdbeCoverage(v);
124549      assert( (pLoop->wsFlags & WHERE_IDX_ONLY)==0
124550           || (pLoop->wsFlags & WHERE_INDEXED)!=0 );
124551      if( (pLoop->wsFlags & WHERE_IDX_ONLY)==0 ){
124552        sqlite3VdbeAddOp1(v, OP_NullRow, pTabList->a[i].iCursor);
124553      }
124554      if( pLoop->wsFlags & WHERE_INDEXED ){
124555        sqlite3VdbeAddOp1(v, OP_NullRow, pLevel->iIdxCur);
124556      }
124557      if( pLevel->op==OP_Return ){
124558        sqlite3VdbeAddOp2(v, OP_Gosub, pLevel->p1, pLevel->addrFirst);
124559      }else{
124560        sqlite3VdbeAddOp2(v, OP_Goto, 0, pLevel->addrFirst);
124561      }
124562      sqlite3VdbeJumpHere(v, addr);
124563    }
124564    VdbeModuleComment((v, "End WHERE-loop%d: %s", i,
124565                     pWInfo->pTabList->a[pLevel->iFrom].pTab->zName));
124566  }
124567
124568  /* The "break" point is here, just past the end of the outer loop.
124569  ** Set it.
124570  */
124571  sqlite3VdbeResolveLabel(v, pWInfo->iBreak);
124572
124573  assert( pWInfo->nLevel<=pTabList->nSrc );
124574  for(i=0, pLevel=pWInfo->a; i<pWInfo->nLevel; i++, pLevel++){
124575    int k, last;
124576    VdbeOp *pOp;
124577    Index *pIdx = 0;
124578    struct SrcList_item *pTabItem = &pTabList->a[pLevel->iFrom];
124579    Table *pTab = pTabItem->pTab;
124580    assert( pTab!=0 );
124581    pLoop = pLevel->pWLoop;
124582
124583    /* For a co-routine, change all OP_Column references to the table of
124584    ** the co-routine into OP_Copy of result contained in a register.
124585    ** OP_Rowid becomes OP_Null.
124586    */
124587    if( pTabItem->viaCoroutine && !db->mallocFailed ){
124588      translateColumnToCopy(v, pLevel->addrBody, pLevel->iTabCur,
124589                            pTabItem->regResult);
124590      continue;
124591    }
124592
124593    /* Close all of the cursors that were opened by sqlite3WhereBegin.
124594    ** Except, do not close cursors that will be reused by the OR optimization
124595    ** (WHERE_OMIT_OPEN_CLOSE).  And do not close the OP_OpenWrite cursors
124596    ** created for the ONEPASS optimization.
124597    */
124598    if( (pTab->tabFlags & TF_Ephemeral)==0
124599     && pTab->pSelect==0
124600     && (pWInfo->wctrlFlags & WHERE_OMIT_OPEN_CLOSE)==0
124601    ){
124602      int ws = pLoop->wsFlags;
124603      if( !pWInfo->okOnePass && (ws & WHERE_IDX_ONLY)==0 ){
124604        sqlite3VdbeAddOp1(v, OP_Close, pTabItem->iCursor);
124605      }
124606      if( (ws & WHERE_INDEXED)!=0
124607       && (ws & (WHERE_IPK|WHERE_AUTO_INDEX))==0
124608       && pLevel->iIdxCur!=pWInfo->aiCurOnePass[1]
124609      ){
124610        sqlite3VdbeAddOp1(v, OP_Close, pLevel->iIdxCur);
124611      }
124612    }
124613
124614    /* If this scan uses an index, make VDBE code substitutions to read data
124615    ** from the index instead of from the table where possible.  In some cases
124616    ** this optimization prevents the table from ever being read, which can
124617    ** yield a significant performance boost.
124618    **
124619    ** Calls to the code generator in between sqlite3WhereBegin and
124620    ** sqlite3WhereEnd will have created code that references the table
124621    ** directly.  This loop scans all that code looking for opcodes
124622    ** that reference the table and converts them into opcodes that
124623    ** reference the index.
124624    */
124625    if( pLoop->wsFlags & (WHERE_INDEXED|WHERE_IDX_ONLY) ){
124626      pIdx = pLoop->u.btree.pIndex;
124627    }else if( pLoop->wsFlags & WHERE_MULTI_OR ){
124628      pIdx = pLevel->u.pCovidx;
124629    }
124630    if( pIdx && !db->mallocFailed ){
124631      last = sqlite3VdbeCurrentAddr(v);
124632      k = pLevel->addrBody;
124633      pOp = sqlite3VdbeGetOp(v, k);
124634      for(; k<last; k++, pOp++){
124635        if( pOp->p1!=pLevel->iTabCur ) continue;
124636        if( pOp->opcode==OP_Column ){
124637          int x = pOp->p2;
124638          assert( pIdx->pTable==pTab );
124639          if( !HasRowid(pTab) ){
124640            Index *pPk = sqlite3PrimaryKeyIndex(pTab);
124641            x = pPk->aiColumn[x];
124642          }
124643          x = sqlite3ColumnOfIndex(pIdx, x);
124644          if( x>=0 ){
124645            pOp->p2 = x;
124646            pOp->p1 = pLevel->iIdxCur;
124647          }
124648          assert( (pLoop->wsFlags & WHERE_IDX_ONLY)==0 || x>=0 );
124649        }else if( pOp->opcode==OP_Rowid ){
124650          pOp->p1 = pLevel->iIdxCur;
124651          pOp->opcode = OP_IdxRowid;
124652        }
124653      }
124654    }
124655  }
124656
124657  /* Final cleanup
124658  */
124659  pParse->nQueryLoop = pWInfo->savedNQueryLoop;
124660  whereInfoFree(db, pWInfo);
124661  return;
124662}
124663
124664/************** End of where.c ***********************************************/
124665/************** Begin file parse.c *******************************************/
124666/* Driver template for the LEMON parser generator.
124667** The author disclaims copyright to this source code.
124668**
124669** This version of "lempar.c" is modified, slightly, for use by SQLite.
124670** The only modifications are the addition of a couple of NEVER()
124671** macros to disable tests that are needed in the case of a general
124672** LALR(1) grammar but which are always false in the
124673** specific grammar used by SQLite.
124674*/
124675/* First off, code is included that follows the "include" declaration
124676** in the input grammar file. */
124677/* #include <stdio.h> */
124678
124679/* #include "sqliteInt.h" */
124680
124681/*
124682** Disable all error recovery processing in the parser push-down
124683** automaton.
124684*/
124685#define YYNOERRORRECOVERY 1
124686
124687/*
124688** Make yytestcase() the same as testcase()
124689*/
124690#define yytestcase(X) testcase(X)
124691
124692/*
124693** An instance of this structure holds information about the
124694** LIMIT clause of a SELECT statement.
124695*/
124696struct LimitVal {
124697  Expr *pLimit;    /* The LIMIT expression.  NULL if there is no limit */
124698  Expr *pOffset;   /* The OFFSET expression.  NULL if there is none */
124699};
124700
124701/*
124702** An instance of this structure is used to store the LIKE,
124703** GLOB, NOT LIKE, and NOT GLOB operators.
124704*/
124705struct LikeOp {
124706  Token eOperator;  /* "like" or "glob" or "regexp" */
124707  int bNot;         /* True if the NOT keyword is present */
124708};
124709
124710/*
124711** An instance of the following structure describes the event of a
124712** TRIGGER.  "a" is the event type, one of TK_UPDATE, TK_INSERT,
124713** TK_DELETE, or TK_INSTEAD.  If the event is of the form
124714**
124715**      UPDATE ON (a,b,c)
124716**
124717** Then the "b" IdList records the list "a,b,c".
124718*/
124719struct TrigEvent { int a; IdList * b; };
124720
124721/*
124722** An instance of this structure holds the ATTACH key and the key type.
124723*/
124724struct AttachKey { int type;  Token key; };
124725
124726
124727  /*
124728  ** For a compound SELECT statement, make sure p->pPrior->pNext==p for
124729  ** all elements in the list.  And make sure list length does not exceed
124730  ** SQLITE_LIMIT_COMPOUND_SELECT.
124731  */
124732  static void parserDoubleLinkSelect(Parse *pParse, Select *p){
124733    if( p->pPrior ){
124734      Select *pNext = 0, *pLoop;
124735      int mxSelect, cnt = 0;
124736      for(pLoop=p; pLoop; pNext=pLoop, pLoop=pLoop->pPrior, cnt++){
124737        pLoop->pNext = pNext;
124738        pLoop->selFlags |= SF_Compound;
124739      }
124740      if( (p->selFlags & SF_MultiValue)==0 &&
124741        (mxSelect = pParse->db->aLimit[SQLITE_LIMIT_COMPOUND_SELECT])>0 &&
124742        cnt>mxSelect
124743      ){
124744        sqlite3ErrorMsg(pParse, "too many terms in compound SELECT");
124745      }
124746    }
124747  }
124748
124749  /* This is a utility routine used to set the ExprSpan.zStart and
124750  ** ExprSpan.zEnd values of pOut so that the span covers the complete
124751  ** range of text beginning with pStart and going to the end of pEnd.
124752  */
124753  static void spanSet(ExprSpan *pOut, Token *pStart, Token *pEnd){
124754    pOut->zStart = pStart->z;
124755    pOut->zEnd = &pEnd->z[pEnd->n];
124756  }
124757
124758  /* Construct a new Expr object from a single identifier.  Use the
124759  ** new Expr to populate pOut.  Set the span of pOut to be the identifier
124760  ** that created the expression.
124761  */
124762  static void spanExpr(ExprSpan *pOut, Parse *pParse, int op, Token *pValue){
124763    pOut->pExpr = sqlite3PExpr(pParse, op, 0, 0, pValue);
124764    pOut->zStart = pValue->z;
124765    pOut->zEnd = &pValue->z[pValue->n];
124766  }
124767
124768  /* This routine constructs a binary expression node out of two ExprSpan
124769  ** objects and uses the result to populate a new ExprSpan object.
124770  */
124771  static void spanBinaryExpr(
124772    ExprSpan *pOut,     /* Write the result here */
124773    Parse *pParse,      /* The parsing context.  Errors accumulate here */
124774    int op,             /* The binary operation */
124775    ExprSpan *pLeft,    /* The left operand */
124776    ExprSpan *pRight    /* The right operand */
124777  ){
124778    pOut->pExpr = sqlite3PExpr(pParse, op, pLeft->pExpr, pRight->pExpr, 0);
124779    pOut->zStart = pLeft->zStart;
124780    pOut->zEnd = pRight->zEnd;
124781  }
124782
124783  /* Construct an expression node for a unary postfix operator
124784  */
124785  static void spanUnaryPostfix(
124786    ExprSpan *pOut,        /* Write the new expression node here */
124787    Parse *pParse,         /* Parsing context to record errors */
124788    int op,                /* The operator */
124789    ExprSpan *pOperand,    /* The operand */
124790    Token *pPostOp         /* The operand token for setting the span */
124791  ){
124792    pOut->pExpr = sqlite3PExpr(pParse, op, pOperand->pExpr, 0, 0);
124793    pOut->zStart = pOperand->zStart;
124794    pOut->zEnd = &pPostOp->z[pPostOp->n];
124795  }
124796
124797  /* A routine to convert a binary TK_IS or TK_ISNOT expression into a
124798  ** unary TK_ISNULL or TK_NOTNULL expression. */
124799  static void binaryToUnaryIfNull(Parse *pParse, Expr *pY, Expr *pA, int op){
124800    sqlite3 *db = pParse->db;
124801    if( pY && pA && pY->op==TK_NULL ){
124802      pA->op = (u8)op;
124803      sqlite3ExprDelete(db, pA->pRight);
124804      pA->pRight = 0;
124805    }
124806  }
124807
124808  /* Construct an expression node for a unary prefix operator
124809  */
124810  static void spanUnaryPrefix(
124811    ExprSpan *pOut,        /* Write the new expression node here */
124812    Parse *pParse,         /* Parsing context to record errors */
124813    int op,                /* The operator */
124814    ExprSpan *pOperand,    /* The operand */
124815    Token *pPreOp         /* The operand token for setting the span */
124816  ){
124817    pOut->pExpr = sqlite3PExpr(pParse, op, pOperand->pExpr, 0, 0);
124818    pOut->zStart = pPreOp->z;
124819    pOut->zEnd = pOperand->zEnd;
124820  }
124821/* Next is all token values, in a form suitable for use by makeheaders.
124822** This section will be null unless lemon is run with the -m switch.
124823*/
124824/*
124825** These constants (all generated automatically by the parser generator)
124826** specify the various kinds of tokens (terminals) that the parser
124827** understands.
124828**
124829** Each symbol here is a terminal symbol in the grammar.
124830*/
124831/* Make sure the INTERFACE macro is defined.
124832*/
124833#ifndef INTERFACE
124834# define INTERFACE 1
124835#endif
124836/* The next thing included is series of defines which control
124837** various aspects of the generated parser.
124838**    YYCODETYPE         is the data type used for storing terminal
124839**                       and nonterminal numbers.  "unsigned char" is
124840**                       used if there are fewer than 250 terminals
124841**                       and nonterminals.  "int" is used otherwise.
124842**    YYNOCODE           is a number of type YYCODETYPE which corresponds
124843**                       to no legal terminal or nonterminal number.  This
124844**                       number is used to fill in empty slots of the hash
124845**                       table.
124846**    YYFALLBACK         If defined, this indicates that one or more tokens
124847**                       have fall-back values which should be used if the
124848**                       original value of the token will not parse.
124849**    YYACTIONTYPE       is the data type used for storing terminal
124850**                       and nonterminal numbers.  "unsigned char" is
124851**                       used if there are fewer than 250 rules and
124852**                       states combined.  "int" is used otherwise.
124853**    sqlite3ParserTOKENTYPE     is the data type used for minor tokens given
124854**                       directly to the parser from the tokenizer.
124855**    YYMINORTYPE        is the data type used for all minor tokens.
124856**                       This is typically a union of many types, one of
124857**                       which is sqlite3ParserTOKENTYPE.  The entry in the union
124858**                       for base tokens is called "yy0".
124859**    YYSTACKDEPTH       is the maximum depth of the parser's stack.  If
124860**                       zero the stack is dynamically sized using realloc()
124861**    sqlite3ParserARG_SDECL     A static variable declaration for the %extra_argument
124862**    sqlite3ParserARG_PDECL     A parameter declaration for the %extra_argument
124863**    sqlite3ParserARG_STORE     Code to store %extra_argument into yypParser
124864**    sqlite3ParserARG_FETCH     Code to extract %extra_argument from yypParser
124865**    YYNSTATE           the combined number of states.
124866**    YYNRULE            the number of rules in the grammar
124867**    YYERRORSYMBOL      is the code number of the error symbol.  If not
124868**                       defined, then do no error processing.
124869*/
124870#define YYCODETYPE unsigned char
124871#define YYNOCODE 254
124872#define YYACTIONTYPE unsigned short int
124873#define YYWILDCARD 70
124874#define sqlite3ParserTOKENTYPE Token
124875typedef union {
124876  int yyinit;
124877  sqlite3ParserTOKENTYPE yy0;
124878  Select* yy3;
124879  ExprList* yy14;
124880  With* yy59;
124881  SrcList* yy65;
124882  struct LikeOp yy96;
124883  Expr* yy132;
124884  u8 yy186;
124885  int yy328;
124886  ExprSpan yy346;
124887  struct TrigEvent yy378;
124888  u16 yy381;
124889  IdList* yy408;
124890  struct {int value; int mask;} yy429;
124891  TriggerStep* yy473;
124892  struct LimitVal yy476;
124893} YYMINORTYPE;
124894#ifndef YYSTACKDEPTH
124895#define YYSTACKDEPTH 100
124896#endif
124897#define sqlite3ParserARG_SDECL Parse *pParse;
124898#define sqlite3ParserARG_PDECL ,Parse *pParse
124899#define sqlite3ParserARG_FETCH Parse *pParse = yypParser->pParse
124900#define sqlite3ParserARG_STORE yypParser->pParse = pParse
124901#define YYNSTATE 642
124902#define YYNRULE 327
124903#define YYFALLBACK 1
124904#define YY_NO_ACTION      (YYNSTATE+YYNRULE+2)
124905#define YY_ACCEPT_ACTION  (YYNSTATE+YYNRULE+1)
124906#define YY_ERROR_ACTION   (YYNSTATE+YYNRULE)
124907
124908/* The yyzerominor constant is used to initialize instances of
124909** YYMINORTYPE objects to zero. */
124910static const YYMINORTYPE yyzerominor = { 0 };
124911
124912/* Define the yytestcase() macro to be a no-op if is not already defined
124913** otherwise.
124914**
124915** Applications can choose to define yytestcase() in the %include section
124916** to a macro that can assist in verifying code coverage.  For production
124917** code the yytestcase() macro should be turned off.  But it is useful
124918** for testing.
124919*/
124920#ifndef yytestcase
124921# define yytestcase(X)
124922#endif
124923
124924
124925/* Next are the tables used to determine what action to take based on the
124926** current state and lookahead token.  These tables are used to implement
124927** functions that take a state number and lookahead value and return an
124928** action integer.
124929**
124930** Suppose the action integer is N.  Then the action is determined as
124931** follows
124932**
124933**   0 <= N < YYNSTATE                  Shift N.  That is, push the lookahead
124934**                                      token onto the stack and goto state N.
124935**
124936**   YYNSTATE <= N < YYNSTATE+YYNRULE   Reduce by rule N-YYNSTATE.
124937**
124938**   N == YYNSTATE+YYNRULE              A syntax error has occurred.
124939**
124940**   N == YYNSTATE+YYNRULE+1            The parser accepts its input.
124941**
124942**   N == YYNSTATE+YYNRULE+2            No such action.  Denotes unused
124943**                                      slots in the yy_action[] table.
124944**
124945** The action table is constructed as a single large table named yy_action[].
124946** Given state S and lookahead X, the action is computed as
124947**
124948**      yy_action[ yy_shift_ofst[S] + X ]
124949**
124950** If the index value yy_shift_ofst[S]+X is out of range or if the value
124951** yy_lookahead[yy_shift_ofst[S]+X] is not equal to X or if yy_shift_ofst[S]
124952** is equal to YY_SHIFT_USE_DFLT, it means that the action is not in the table
124953** and that yy_default[S] should be used instead.
124954**
124955** The formula above is for computing the action when the lookahead is
124956** a terminal symbol.  If the lookahead is a non-terminal (as occurs after
124957** a reduce action) then the yy_reduce_ofst[] array is used in place of
124958** the yy_shift_ofst[] array and YY_REDUCE_USE_DFLT is used in place of
124959** YY_SHIFT_USE_DFLT.
124960**
124961** The following are the tables generated in this section:
124962**
124963**  yy_action[]        A single table containing all actions.
124964**  yy_lookahead[]     A table containing the lookahead for each entry in
124965**                     yy_action.  Used to detect hash collisions.
124966**  yy_shift_ofst[]    For each state, the offset into yy_action for
124967**                     shifting terminals.
124968**  yy_reduce_ofst[]   For each state, the offset into yy_action for
124969**                     shifting non-terminals after a reduce.
124970**  yy_default[]       Default action for each state.
124971*/
124972#define YY_ACTTAB_COUNT (1497)
124973static const YYACTIONTYPE yy_action[] = {
124974 /*     0 */   306,  212,  432,  955,  639,  191,  955,  295,  559,   88,
124975 /*    10 */    88,   88,   88,   81,   86,   86,   86,   86,   85,   85,
124976 /*    20 */    84,   84,   84,   83,  330,  185,  184,  183,  635,  635,
124977 /*    30 */   292,  606,  606,   88,   88,   88,   88,  683,   86,   86,
124978 /*    40 */    86,   86,   85,   85,   84,   84,   84,   83,  330,   16,
124979 /*    50 */   436,  597,   89,   90,   80,  600,  599,  601,  601,   87,
124980 /*    60 */    87,   88,   88,   88,   88,  684,   86,   86,   86,   86,
124981 /*    70 */    85,   85,   84,   84,   84,   83,  330,  306,  559,   84,
124982 /*    80 */    84,   84,   83,  330,   65,   86,   86,   86,   86,   85,
124983 /*    90 */    85,   84,   84,   84,   83,  330,  635,  635,  634,  633,
124984 /*   100 */   182,  682,  550,  379,  376,  375,   17,  322,  606,  606,
124985 /*   110 */   371,  198,  479,   91,  374,   82,   79,  165,   85,   85,
124986 /*   120 */    84,   84,   84,   83,  330,  598,  635,  635,  107,   89,
124987 /*   130 */    90,   80,  600,  599,  601,  601,   87,   87,   88,   88,
124988 /*   140 */    88,   88,  186,   86,   86,   86,   86,   85,   85,   84,
124989 /*   150 */    84,   84,   83,  330,  306,  594,  594,  142,  328,  327,
124990 /*   160 */   484,  249,  344,  238,  635,  635,  634,  633,  585,  448,
124991 /*   170 */   526,  525,  229,  388,    1,  394,  450,  584,  449,  635,
124992 /*   180 */   635,  635,  635,  319,  395,  606,  606,  199,  157,  273,
124993 /*   190 */   382,  268,  381,  187,  635,  635,  634,  633,  311,  555,
124994 /*   200 */   266,  593,  593,  266,  347,  588,   89,   90,   80,  600,
124995 /*   210 */   599,  601,  601,   87,   87,   88,   88,   88,   88,  478,
124996 /*   220 */    86,   86,   86,   86,   85,   85,   84,   84,   84,   83,
124997 /*   230 */   330,  306,  272,  536,  634,  633,  146,  610,  197,  310,
124998 /*   240 */   575,  182,  482,  271,  379,  376,  375,  506,   21,  634,
124999 /*   250 */   633,  634,  633,  635,  635,  374,  611,  574,  548,  440,
125000 /*   260 */   111,  563,  606,  606,  634,  633,  324,  479,  608,  608,
125001 /*   270 */   608,  300,  435,  573,  119,  407,  210,  162,  562,  883,
125002 /*   280 */   592,  592,  306,   89,   90,   80,  600,  599,  601,  601,
125003 /*   290 */    87,   87,   88,   88,   88,   88,  506,   86,   86,   86,
125004 /*   300 */    86,   85,   85,   84,   84,   84,   83,  330,  620,  111,
125005 /*   310 */   635,  635,  361,  606,  606,  358,  249,  349,  248,  433,
125006 /*   320 */   243,  479,  586,  634,  633,  195,  611,   93,  119,  221,
125007 /*   330 */   575,  497,  534,  534,   89,   90,   80,  600,  599,  601,
125008 /*   340 */   601,   87,   87,   88,   88,   88,   88,  574,   86,   86,
125009 /*   350 */    86,   86,   85,   85,   84,   84,   84,   83,  330,  306,
125010 /*   360 */    77,  429,  638,  573,  589,  530,  240,  230,  242,  105,
125011 /*   370 */   249,  349,  248,  515,  588,  208,  460,  529,  564,  173,
125012 /*   380 */   634,  633,  970,  144,  430,    2,  424,  228,  380,  557,
125013 /*   390 */   606,  606,  190,  153,  159,  158,  514,   51,  632,  631,
125014 /*   400 */   630,   71,  536,  432,  954,  196,  610,  954,  614,   45,
125015 /*   410 */    18,   89,   90,   80,  600,  599,  601,  601,   87,   87,
125016 /*   420 */    88,   88,   88,   88,  261,   86,   86,   86,   86,   85,
125017 /*   430 */    85,   84,   84,   84,   83,  330,  306,  608,  608,  608,
125018 /*   440 */   542,  424,  402,  385,  241,  506,  451,  320,  211,  543,
125019 /*   450 */   164,  436,  386,  293,  451,  587,  108,  496,  111,  334,
125020 /*   460 */   391,  591,  424,  614,   27,  452,  453,  606,  606,   72,
125021 /*   470 */   257,   70,  259,  452,  339,  342,  564,  582,   68,  415,
125022 /*   480 */   469,  328,  327,   62,  614,   45,  110,  393,   89,   90,
125023 /*   490 */    80,  600,  599,  601,  601,   87,   87,   88,   88,   88,
125024 /*   500 */    88,  152,   86,   86,   86,   86,   85,   85,   84,   84,
125025 /*   510 */    84,   83,  330,  306,  110,  499,  520,  538,  402,  389,
125026 /*   520 */   424,  110,  566,  500,  593,  593,  454,   82,   79,  165,
125027 /*   530 */   424,  591,  384,  564,  340,  615,  188,  162,  424,  350,
125028 /*   540 */   616,  424,  614,   44,  606,  606,  445,  582,  300,  434,
125029 /*   550 */   151,   19,  614,    9,  568,  580,  348,  615,  469,  567,
125030 /*   560 */   614,   26,  616,  614,   45,   89,   90,   80,  600,  599,
125031 /*   570 */   601,  601,   87,   87,   88,   88,   88,   88,  411,   86,
125032 /*   580 */    86,   86,   86,   85,   85,   84,   84,   84,   83,  330,
125033 /*   590 */   306,  579,  110,  578,  521,  282,  433,  398,  400,  255,
125034 /*   600 */   486,   82,   79,  165,  487,  164,   82,   79,  165,  488,
125035 /*   610 */   488,  364,  387,  424,  544,  544,  509,  350,  362,  155,
125036 /*   620 */   191,  606,  606,  559,  642,  640,  333,   82,   79,  165,
125037 /*   630 */   305,  564,  507,  312,  357,  614,   45,  329,  596,  595,
125038 /*   640 */   194,  337,   89,   90,   80,  600,  599,  601,  601,   87,
125039 /*   650 */    87,   88,   88,   88,   88,  424,   86,   86,   86,   86,
125040 /*   660 */    85,   85,   84,   84,   84,   83,  330,  306,   20,  323,
125041 /*   670 */   150,  263,  211,  543,  421,  596,  595,  614,   22,  424,
125042 /*   680 */   193,  424,  284,  424,  391,  424,  509,  424,  577,  424,
125043 /*   690 */   186,  335,  424,  559,  424,  313,  120,  546,  606,  606,
125044 /*   700 */    67,  614,   47,  614,   50,  614,   48,  614,  100,  614,
125045 /*   710 */    99,  614,  101,  576,  614,  102,  614,  109,  326,   89,
125046 /*   720 */    90,   80,  600,  599,  601,  601,   87,   87,   88,   88,
125047 /*   730 */    88,   88,  424,   86,   86,   86,   86,   85,   85,   84,
125048 /*   740 */    84,   84,   83,  330,  306,  424,  311,  424,  585,   54,
125049 /*   750 */   424,  516,  517,  590,  614,  112,  424,  584,  424,  572,
125050 /*   760 */   424,  195,  424,  571,  424,   67,  424,  614,   94,  614,
125051 /*   770 */    98,  424,  614,   97,  264,  606,  606,  195,  614,   46,
125052 /*   780 */   614,   96,  614,   30,  614,   49,  614,  115,  614,  114,
125053 /*   790 */   418,  229,  388,  614,  113,  306,   89,   90,   80,  600,
125054 /*   800 */   599,  601,  601,   87,   87,   88,   88,   88,   88,  424,
125055 /*   810 */    86,   86,   86,   86,   85,   85,   84,   84,   84,   83,
125056 /*   820 */   330,  119,  424,  590,  110,  372,  606,  606,  195,   53,
125057 /*   830 */   250,  614,   29,  195,  472,  438,  729,  190,  302,  498,
125058 /*   840 */    14,  523,  641,    2,  614,   43,  306,   89,   90,   80,
125059 /*   850 */   600,  599,  601,  601,   87,   87,   88,   88,   88,   88,
125060 /*   860 */   424,   86,   86,   86,   86,   85,   85,   84,   84,   84,
125061 /*   870 */    83,  330,  424,  613,  964,  964,  354,  606,  606,  420,
125062 /*   880 */   312,   64,  614,   42,  391,  355,  283,  437,  301,  255,
125063 /*   890 */   414,  410,  495,  492,  614,   28,  471,  306,   89,   90,
125064 /*   900 */    80,  600,  599,  601,  601,   87,   87,   88,   88,   88,
125065 /*   910 */    88,  424,   86,   86,   86,   86,   85,   85,   84,   84,
125066 /*   920 */    84,   83,  330,  424,  110,  110,  110,  110,  606,  606,
125067 /*   930 */   110,  254,   13,  614,   41,  532,  531,  283,  481,  531,
125068 /*   940 */   457,  284,  119,  561,  356,  614,   40,  284,  306,   89,
125069 /*   950 */    78,   80,  600,  599,  601,  601,   87,   87,   88,   88,
125070 /*   960 */    88,   88,  424,   86,   86,   86,   86,   85,   85,   84,
125071 /*   970 */    84,   84,   83,  330,  110,  424,  341,  220,  555,  606,
125072 /*   980 */   606,  351,  555,  318,  614,   95,  413,  255,   83,  330,
125073 /*   990 */   284,  284,  255,  640,  333,  356,  255,  614,   39,  306,
125074 /*  1000 */   356,   90,   80,  600,  599,  601,  601,   87,   87,   88,
125075 /*  1010 */    88,   88,   88,  424,   86,   86,   86,   86,   85,   85,
125076 /*  1020 */    84,   84,   84,   83,  330,  424,  317,  316,  141,  465,
125077 /*  1030 */   606,  606,  219,  619,  463,  614,   10,  417,  462,  255,
125078 /*  1040 */   189,  510,  553,  351,  207,  363,  161,  614,   38,  315,
125079 /*  1050 */   218,  255,  255,   80,  600,  599,  601,  601,   87,   87,
125080 /*  1060 */    88,   88,   88,   88,  424,   86,   86,   86,   86,   85,
125081 /*  1070 */    85,   84,   84,   84,   83,  330,   76,  419,  255,    3,
125082 /*  1080 */   878,  461,  424,  247,  331,  331,  614,   37,  217,   76,
125083 /*  1090 */   419,  390,    3,  216,  215,  422,    4,  331,  331,  424,
125084 /*  1100 */   547,   12,  424,  545,  614,   36,  424,  541,  422,  424,
125085 /*  1110 */   540,  424,  214,  424,  408,  424,  539,  403,  605,  605,
125086 /*  1120 */   237,  614,   25,  119,  614,   24,  588,  408,  614,   45,
125087 /*  1130 */   118,  614,   35,  614,   34,  614,   33,  614,   23,  588,
125088 /*  1140 */    60,  223,  603,  602,  513,  378,   73,   74,  140,  139,
125089 /*  1150 */   424,  110,  265,   75,  426,  425,   59,  424,  610,   73,
125090 /*  1160 */    74,  549,  402,  404,  424,  373,   75,  426,  425,  604,
125091 /*  1170 */   138,  610,  614,   11,  392,   76,  419,  181,    3,  614,
125092 /*  1180 */    32,  271,  369,  331,  331,  493,  614,   31,  149,  608,
125093 /*  1190 */   608,  608,  607,   15,  422,  365,  614,    8,  137,  489,
125094 /*  1200 */   136,  190,  608,  608,  608,  607,   15,  485,  176,  135,
125095 /*  1210 */     7,  252,  477,  408,  174,  133,  175,  474,   57,   56,
125096 /*  1220 */   132,  130,  119,   76,  419,  588,    3,  468,  245,  464,
125097 /*  1230 */   171,  331,  331,  125,  123,  456,  447,  122,  446,  104,
125098 /*  1240 */   336,  231,  422,  166,  154,   73,   74,  332,  116,  431,
125099 /*  1250 */   121,  309,   75,  426,  425,  222,  106,  610,  308,  637,
125100 /*  1260 */   204,  408,  629,  627,  628,    6,  200,  428,  427,  290,
125101 /*  1270 */   203,  622,  201,  588,   62,   63,  289,   66,  419,  399,
125102 /*  1280 */     3,  401,  288,   92,  143,  331,  331,  287,  608,  608,
125103 /*  1290 */   608,  607,   15,   73,   74,  227,  422,  325,   69,  416,
125104 /*  1300 */    75,  426,  425,  612,  412,  610,  192,   61,  569,  209,
125105 /*  1310 */   396,  226,  278,  225,  383,  408,  527,  558,  276,  533,
125106 /*  1320 */   552,  528,  321,  523,  370,  508,  180,  588,  494,  179,
125107 /*  1330 */   366,  117,  253,  269,  522,  503,  608,  608,  608,  607,
125108 /*  1340 */    15,  551,  502,   58,  274,  524,  178,   73,   74,  304,
125109 /*  1350 */   501,  368,  303,  206,   75,  426,  425,  491,  360,  610,
125110 /*  1360 */   213,  177,  483,  131,  345,  298,  297,  296,  202,  294,
125111 /*  1370 */   480,  490,  466,  134,  172,  129,  444,  346,  470,  128,
125112 /*  1380 */   314,  459,  103,  127,  126,  148,  124,  167,  443,  235,
125113 /*  1390 */   608,  608,  608,  607,   15,  442,  439,  623,  234,  299,
125114 /*  1400 */   145,  583,  291,  377,  581,  160,  119,  156,  270,  636,
125115 /*  1410 */   971,  169,  279,  626,  520,  625,  473,  624,  170,  621,
125116 /*  1420 */   618,  119,  168,   55,  409,  423,  537,  609,  286,  285,
125117 /*  1430 */   405,  570,  560,  556,    5,   52,  458,  554,  147,  267,
125118 /*  1440 */   519,  504,  518,  406,  262,  239,  260,  512,  343,  511,
125119 /*  1450 */   258,  353,  565,  256,  224,  251,  359,  277,  275,  476,
125120 /*  1460 */   475,  246,  352,  244,  467,  455,  236,  233,  232,  307,
125121 /*  1470 */   441,  281,  205,  163,  397,  280,  535,  505,  330,  617,
125122 /*  1480 */   971,  971,  971,  971,  367,  971,  971,  971,  971,  971,
125123 /*  1490 */   971,  971,  971,  971,  971,  971,  338,
125124};
125125static const YYCODETYPE yy_lookahead[] = {
125126 /*     0 */    19,   22,   22,   23,    1,   24,   26,   15,   27,   80,
125127 /*    10 */    81,   82,   83,   84,   85,   86,   87,   88,   89,   90,
125128 /*    20 */    91,   92,   93,   94,   95,  108,  109,  110,   27,   28,
125129 /*    30 */    23,   50,   51,   80,   81,   82,   83,  122,   85,   86,
125130 /*    40 */    87,   88,   89,   90,   91,   92,   93,   94,   95,   22,
125131 /*    50 */    70,   23,   71,   72,   73,   74,   75,   76,   77,   78,
125132 /*    60 */    79,   80,   81,   82,   83,  122,   85,   86,   87,   88,
125133 /*    70 */    89,   90,   91,   92,   93,   94,   95,   19,   97,   91,
125134 /*    80 */    92,   93,   94,   95,   26,   85,   86,   87,   88,   89,
125135 /*    90 */    90,   91,   92,   93,   94,   95,   27,   28,   97,   98,
125136 /*   100 */    99,  122,  211,  102,  103,  104,   79,   19,   50,   51,
125137 /*   110 */    19,  122,   59,   55,  113,  224,  225,  226,   89,   90,
125138 /*   120 */    91,   92,   93,   94,   95,   23,   27,   28,   26,   71,
125139 /*   130 */    72,   73,   74,   75,   76,   77,   78,   79,   80,   81,
125140 /*   140 */    82,   83,   51,   85,   86,   87,   88,   89,   90,   91,
125141 /*   150 */    92,   93,   94,   95,   19,  132,  133,   58,   89,   90,
125142 /*   160 */    21,  108,  109,  110,   27,   28,   97,   98,   33,  100,
125143 /*   170 */     7,    8,  119,  120,   22,   19,  107,   42,  109,   27,
125144 /*   180 */    28,   27,   28,   95,   28,   50,   51,   99,  100,  101,
125145 /*   190 */   102,  103,  104,  105,   27,   28,   97,   98,  107,  152,
125146 /*   200 */   112,  132,  133,  112,   65,   69,   71,   72,   73,   74,
125147 /*   210 */    75,   76,   77,   78,   79,   80,   81,   82,   83,   11,
125148 /*   220 */    85,   86,   87,   88,   89,   90,   91,   92,   93,   94,
125149 /*   230 */    95,   19,  101,   97,   97,   98,   24,  101,  122,  157,
125150 /*   240 */    12,   99,  103,  112,  102,  103,  104,  152,   22,   97,
125151 /*   250 */    98,   97,   98,   27,   28,  113,   27,   29,   91,  164,
125152 /*   260 */   165,  124,   50,   51,   97,   98,  219,   59,  132,  133,
125153 /*   270 */   134,   22,   23,   45,   66,   47,  212,  213,  124,  140,
125154 /*   280 */   132,  133,   19,   71,   72,   73,   74,   75,   76,   77,
125155 /*   290 */    78,   79,   80,   81,   82,   83,  152,   85,   86,   87,
125156 /*   300 */    88,   89,   90,   91,   92,   93,   94,   95,  164,  165,
125157 /*   310 */    27,   28,  230,   50,   51,  233,  108,  109,  110,   70,
125158 /*   320 */    16,   59,   23,   97,   98,   26,   97,   22,   66,  185,
125159 /*   330 */    12,  187,   27,   28,   71,   72,   73,   74,   75,   76,
125160 /*   340 */    77,   78,   79,   80,   81,   82,   83,   29,   85,   86,
125161 /*   350 */    87,   88,   89,   90,   91,   92,   93,   94,   95,   19,
125162 /*   360 */    22,  148,  149,   45,   23,   47,   62,  154,   64,  156,
125163 /*   370 */   108,  109,  110,   37,   69,   23,  163,   59,   26,   26,
125164 /*   380 */    97,   98,  144,  145,  146,  147,  152,  200,   52,   23,
125165 /*   390 */    50,   51,   26,   22,   89,   90,   60,  210,    7,    8,
125166 /*   400 */     9,  138,   97,   22,   23,   26,  101,   26,  174,  175,
125167 /*   410 */   197,   71,   72,   73,   74,   75,   76,   77,   78,   79,
125168 /*   420 */    80,   81,   82,   83,   16,   85,   86,   87,   88,   89,
125169 /*   430 */    90,   91,   92,   93,   94,   95,   19,  132,  133,  134,
125170 /*   440 */    23,  152,  208,  209,  140,  152,  152,  111,  195,  196,
125171 /*   450 */    98,   70,  163,  160,  152,   23,   22,  164,  165,  246,
125172 /*   460 */   207,   27,  152,  174,  175,  171,  172,   50,   51,  137,
125173 /*   470 */    62,  139,   64,  171,  172,  222,  124,   27,  138,   24,
125174 /*   480 */   163,   89,   90,  130,  174,  175,  197,  163,   71,   72,
125175 /*   490 */    73,   74,   75,   76,   77,   78,   79,   80,   81,   82,
125176 /*   500 */    83,   22,   85,   86,   87,   88,   89,   90,   91,   92,
125177 /*   510 */    93,   94,   95,   19,  197,  181,  182,   23,  208,  209,
125178 /*   520 */   152,  197,   26,  189,  132,  133,  232,  224,  225,  226,
125179 /*   530 */   152,   97,   91,   26,  232,  116,  212,  213,  152,  222,
125180 /*   540 */   121,  152,  174,  175,   50,   51,  243,   97,   22,   23,
125181 /*   550 */    22,  234,  174,  175,  177,   23,  239,  116,  163,  177,
125182 /*   560 */   174,  175,  121,  174,  175,   71,   72,   73,   74,   75,
125183 /*   570 */    76,   77,   78,   79,   80,   81,   82,   83,   24,   85,
125184 /*   580 */    86,   87,   88,   89,   90,   91,   92,   93,   94,   95,
125185 /*   590 */    19,   23,  197,   11,   23,  227,   70,  208,  220,  152,
125186 /*   600 */    31,  224,  225,  226,   35,   98,  224,  225,  226,  108,
125187 /*   610 */   109,  110,  115,  152,  117,  118,   27,  222,   49,  123,
125188 /*   620 */    24,   50,   51,   27,    0,    1,    2,  224,  225,  226,
125189 /*   630 */   166,  124,  168,  169,  239,  174,  175,  170,  171,  172,
125190 /*   640 */    22,  194,   71,   72,   73,   74,   75,   76,   77,   78,
125191 /*   650 */    79,   80,   81,   82,   83,  152,   85,   86,   87,   88,
125192 /*   660 */    89,   90,   91,   92,   93,   94,   95,   19,   22,  208,
125193 /*   670 */    24,   23,  195,  196,  170,  171,  172,  174,  175,  152,
125194 /*   680 */    26,  152,  152,  152,  207,  152,   97,  152,   23,  152,
125195 /*   690 */    51,  244,  152,   97,  152,  247,  248,   23,   50,   51,
125196 /*   700 */    26,  174,  175,  174,  175,  174,  175,  174,  175,  174,
125197 /*   710 */   175,  174,  175,   23,  174,  175,  174,  175,  188,   71,
125198 /*   720 */    72,   73,   74,   75,   76,   77,   78,   79,   80,   81,
125199 /*   730 */    82,   83,  152,   85,   86,   87,   88,   89,   90,   91,
125200 /*   740 */    92,   93,   94,   95,   19,  152,  107,  152,   33,   24,
125201 /*   750 */   152,  100,  101,   27,  174,  175,  152,   42,  152,   23,
125202 /*   760 */   152,   26,  152,   23,  152,   26,  152,  174,  175,  174,
125203 /*   770 */   175,  152,  174,  175,   23,   50,   51,   26,  174,  175,
125204 /*   780 */   174,  175,  174,  175,  174,  175,  174,  175,  174,  175,
125205 /*   790 */   163,  119,  120,  174,  175,   19,   71,   72,   73,   74,
125206 /*   800 */    75,   76,   77,   78,   79,   80,   81,   82,   83,  152,
125207 /*   810 */    85,   86,   87,   88,   89,   90,   91,   92,   93,   94,
125208 /*   820 */    95,   66,  152,   97,  197,   23,   50,   51,   26,   53,
125209 /*   830 */    23,  174,  175,   26,   23,   23,   23,   26,   26,   26,
125210 /*   840 */    36,  106,  146,  147,  174,  175,   19,   71,   72,   73,
125211 /*   850 */    74,   75,   76,   77,   78,   79,   80,   81,   82,   83,
125212 /*   860 */   152,   85,   86,   87,   88,   89,   90,   91,   92,   93,
125213 /*   870 */    94,   95,  152,  196,  119,  120,   19,   50,   51,  168,
125214 /*   880 */   169,   26,  174,  175,  207,   28,  152,  249,  250,  152,
125215 /*   890 */   163,  163,  163,  163,  174,  175,  163,   19,   71,   72,
125216 /*   900 */    73,   74,   75,   76,   77,   78,   79,   80,   81,   82,
125217 /*   910 */    83,  152,   85,   86,   87,   88,   89,   90,   91,   92,
125218 /*   920 */    93,   94,   95,  152,  197,  197,  197,  197,   50,   51,
125219 /*   930 */   197,  194,   36,  174,  175,  191,  192,  152,  191,  192,
125220 /*   940 */   163,  152,   66,  124,  152,  174,  175,  152,   19,   71,
125221 /*   950 */    72,   73,   74,   75,   76,   77,   78,   79,   80,   81,
125222 /*   960 */    82,   83,  152,   85,   86,   87,   88,   89,   90,   91,
125223 /*   970 */    92,   93,   94,   95,  197,  152,  100,  188,  152,   50,
125224 /*   980 */    51,  152,  152,  188,  174,  175,  252,  152,   94,   95,
125225 /*   990 */   152,  152,  152,    1,    2,  152,  152,  174,  175,   19,
125226 /*  1000 */   152,   72,   73,   74,   75,   76,   77,   78,   79,   80,
125227 /*  1010 */    81,   82,   83,  152,   85,   86,   87,   88,   89,   90,
125228 /*  1020 */    91,   92,   93,   94,   95,  152,  188,  188,   22,  194,
125229 /*  1030 */    50,   51,  240,  173,  194,  174,  175,  252,  194,  152,
125230 /*  1040 */    36,  181,   28,  152,   23,  219,  122,  174,  175,  219,
125231 /*  1050 */   221,  152,  152,   73,   74,   75,   76,   77,   78,   79,
125232 /*  1060 */    80,   81,   82,   83,  152,   85,   86,   87,   88,   89,
125233 /*  1070 */    90,   91,   92,   93,   94,   95,   19,   20,  152,   22,
125234 /*  1080 */    23,  194,  152,  240,   27,   28,  174,  175,  240,   19,
125235 /*  1090 */    20,   26,   22,  194,  194,   38,   22,   27,   28,  152,
125236 /*  1100 */    23,   22,  152,  116,  174,  175,  152,   23,   38,  152,
125237 /*  1110 */    23,  152,  221,  152,   57,  152,   23,  163,   50,   51,
125238 /*  1120 */   194,  174,  175,   66,  174,  175,   69,   57,  174,  175,
125239 /*  1130 */    40,  174,  175,  174,  175,  174,  175,  174,  175,   69,
125240 /*  1140 */    22,   53,   74,   75,   30,   53,   89,   90,   22,   22,
125241 /*  1150 */   152,  197,   23,   96,   97,   98,   22,  152,  101,   89,
125242 /*  1160 */    90,   91,  208,  209,  152,   53,   96,   97,   98,  101,
125243 /*  1170 */    22,  101,  174,  175,  152,   19,   20,  105,   22,  174,
125244 /*  1180 */   175,  112,   19,   27,   28,   20,  174,  175,   24,  132,
125245 /*  1190 */   133,  134,  135,  136,   38,   44,  174,  175,  107,   61,
125246 /*  1200 */    54,   26,  132,  133,  134,  135,  136,   54,  107,   22,
125247 /*  1210 */     5,  140,    1,   57,   36,  111,  122,   28,   79,   79,
125248 /*  1220 */   131,  123,   66,   19,   20,   69,   22,    1,   16,   20,
125249 /*  1230 */   125,   27,   28,  123,  111,  120,   23,  131,   23,   16,
125250 /*  1240 */    68,  142,   38,   15,   22,   89,   90,    3,  167,    4,
125251 /*  1250 */   248,  251,   96,   97,   98,  180,  180,  101,  251,  151,
125252 /*  1260 */     6,   57,  151,   13,  151,   26,   25,  151,  161,  202,
125253 /*  1270 */   153,  162,  153,   69,  130,  128,  203,   19,   20,  127,
125254 /*  1280 */    22,  126,  204,  129,   22,   27,   28,  205,  132,  133,
125255 /*  1290 */   134,  135,  136,   89,   90,  231,   38,   95,  137,  179,
125256 /*  1300 */    96,   97,   98,  206,  179,  101,  122,  107,  159,  159,
125257 /*  1310 */   125,  231,  216,  228,  107,   57,  184,  217,  216,  176,
125258 /*  1320 */   217,  176,   48,  106,   18,  184,  158,   69,  159,  158,
125259 /*  1330 */    46,   71,  237,  176,  176,  176,  132,  133,  134,  135,
125260 /*  1340 */   136,  217,  176,  137,  216,  178,  158,   89,   90,  179,
125261 /*  1350 */   176,  159,  179,  159,   96,   97,   98,  159,  159,  101,
125262 /*  1360 */     5,  158,  202,   22,   18,   10,   11,   12,   13,   14,
125263 /*  1370 */   190,  238,   17,  190,  158,  193,   41,  159,  202,  193,
125264 /*  1380 */   159,  202,  245,  193,  193,  223,  190,   32,  159,   34,
125265 /*  1390 */   132,  133,  134,  135,  136,  159,   39,  155,   43,  150,
125266 /*  1400 */   223,  177,  201,  178,  177,  186,   66,  199,  177,  152,
125267 /*  1410 */   253,   56,  215,  152,  182,  152,  202,  152,   63,  152,
125268 /*  1420 */   152,   66,   67,  242,  229,  152,  174,  152,  152,  152,
125269 /*  1430 */   152,  152,  152,  152,  199,  242,  202,  152,  198,  152,
125270 /*  1440 */   152,  152,  183,  192,  152,  215,  152,  183,  215,  183,
125271 /*  1450 */   152,  241,  214,  152,  211,  152,  152,  211,  211,  152,
125272 /*  1460 */   152,  241,  152,  152,  152,  152,  152,  152,  152,  114,
125273 /*  1470 */   152,  152,  235,  152,  152,  152,  174,  187,   95,  174,
125274 /*  1480 */   253,  253,  253,  253,  236,  253,  253,  253,  253,  253,
125275 /*  1490 */   253,  253,  253,  253,  253,  253,  141,
125276};
125277#define YY_SHIFT_USE_DFLT (-86)
125278#define YY_SHIFT_COUNT (429)
125279#define YY_SHIFT_MIN   (-85)
125280#define YY_SHIFT_MAX   (1383)
125281static const short yy_shift_ofst[] = {
125282 /*     0 */   992, 1057, 1355, 1156, 1204, 1204,    1,  262,  -19,  135,
125283 /*    10 */   135,  776, 1204, 1204, 1204, 1204,   69,   69,   53,  208,
125284 /*    20 */   283,  755,   58,  725,  648,  571,  494,  417,  340,  263,
125285 /*    30 */   212,  827,  827,  827,  827,  827,  827,  827,  827,  827,
125286 /*    40 */   827,  827,  827,  827,  827,  827,  878,  827,  929,  980,
125287 /*    50 */   980, 1070, 1204, 1204, 1204, 1204, 1204, 1204, 1204, 1204,
125288 /*    60 */  1204, 1204, 1204, 1204, 1204, 1204, 1204, 1204, 1204, 1204,
125289 /*    70 */  1204, 1204, 1204, 1204, 1204, 1204, 1204, 1204, 1204, 1204,
125290 /*    80 */  1258, 1204, 1204, 1204, 1204, 1204, 1204, 1204, 1204, 1204,
125291 /*    90 */  1204, 1204, 1204, 1204,  -71,  -47,  -47,  -47,  -47,  -47,
125292 /*   100 */     0,   29,  -12,  283,  283,  139,   91,  392,  392,  894,
125293 /*   110 */   672,  726, 1383,  -86,  -86,  -86,   88,  318,  318,   99,
125294 /*   120 */   381,  -20,  283,  283,  283,  283,  283,  283,  283,  283,
125295 /*   130 */   283,  283,  283,  283,  283,  283,  283,  283,  283,  283,
125296 /*   140 */   283,  283,  283,  283,  624,  876,  726,  672, 1340, 1340,
125297 /*   150 */  1340, 1340, 1340, 1340,  -86,  -86,  -86,  305,  136,  136,
125298 /*   160 */   142,  167,  226,  154,  137,  152,  283,  283,  283,  283,
125299 /*   170 */   283,  283,  283,  283,  283,  283,  283,  283,  283,  283,
125300 /*   180 */   283,  283,  283,  336,  336,  336,  283,  283,  352,  283,
125301 /*   190 */   283,  283,  283,  283,  228,  283,  283,  283,  283,  283,
125302 /*   200 */   283,  283,  283,  283,  283,  501,  569,  596,  596,  596,
125303 /*   210 */   507,  497,  441,  391,  353,  156,  156,  857,  353,  857,
125304 /*   220 */   735,  813,  639,  715,  156,  332,  715,  715,  496,  419,
125305 /*   230 */   646, 1357, 1184, 1184, 1335, 1335, 1184, 1341, 1260, 1144,
125306 /*   240 */  1346, 1346, 1346, 1346, 1184, 1306, 1144, 1341, 1260, 1260,
125307 /*   250 */  1144, 1184, 1306, 1206, 1284, 1184, 1184, 1306, 1184, 1306,
125308 /*   260 */  1184, 1306, 1262, 1207, 1207, 1207, 1274, 1262, 1207, 1217,
125309 /*   270 */  1207, 1274, 1207, 1207, 1185, 1200, 1185, 1200, 1185, 1200,
125310 /*   280 */  1184, 1184, 1161, 1262, 1202, 1202, 1262, 1154, 1155, 1147,
125311 /*   290 */  1152, 1144, 1241, 1239, 1250, 1250, 1254, 1254, 1254, 1254,
125312 /*   300 */   -86,  -86,  -86,  -86,  -86,  -86, 1068,  304,  526,  249,
125313 /*   310 */   408,  -83,  434,  812,   27,  811,  807,  802,  751,  589,
125314 /*   320 */   651,  163,  131,  674,  366,  450,  299,  148,   23,  102,
125315 /*   330 */   229,  -21, 1245, 1244, 1222, 1099, 1228, 1172, 1223, 1215,
125316 /*   340 */  1213, 1115, 1106, 1123, 1110, 1209, 1105, 1212, 1226, 1098,
125317 /*   350 */  1089, 1140, 1139, 1104, 1189, 1178, 1094, 1211, 1205, 1187,
125318 /*   360 */  1101, 1071, 1153, 1175, 1146, 1138, 1151, 1091, 1164, 1165,
125319 /*   370 */  1163, 1069, 1072, 1148, 1112, 1134, 1127, 1129, 1126, 1092,
125320 /*   380 */  1114, 1118, 1088, 1090, 1093, 1087, 1084,  987, 1079, 1077,
125321 /*   390 */  1074, 1065,  924, 1021, 1014, 1004, 1006,  819,  739,  896,
125322 /*   400 */   855,  804,  739,  740,  736,  690,  654,  665,  618,  582,
125323 /*   410 */   568,  528,  554,  379,  532,  479,  455,  379,  432,  371,
125324 /*   420 */   341,   28,  338,  116,  -11,  -57,  -85,    7,   -8,    3,
125325};
125326#define YY_REDUCE_USE_DFLT (-110)
125327#define YY_REDUCE_COUNT (305)
125328#define YY_REDUCE_MIN   (-109)
125329#define YY_REDUCE_MAX   (1323)
125330static const short yy_reduce_ofst[] = {
125331 /*     0 */   238,  954,  213,  289,  310,  234,  144,  317, -109,  382,
125332 /*    10 */   377,  303,  461,  389,  378,  368,  302,  294,  253,  395,
125333 /*    20 */   293,  324,  403,  403,  403,  403,  403,  403,  403,  403,
125334 /*    30 */   403,  403,  403,  403,  403,  403,  403,  403,  403,  403,
125335 /*    40 */   403,  403,  403,  403,  403,  403,  403,  403,  403,  403,
125336 /*    50 */   403, 1022, 1012, 1005,  998,  963,  961,  959,  957,  950,
125337 /*    60 */   947,  930,  912,  873,  861,  823,  810,  771,  759,  720,
125338 /*    70 */   708,  670,  657,  619,  614,  612,  610,  608,  606,  604,
125339 /*    80 */   598,  595,  593,  580,  542,  540,  537,  535,  533,  531,
125340 /*    90 */   529,  527,  503,  386,  403,  403,  403,  403,  403,  403,
125341 /*   100 */   403,  403,  403,   95,  447,   82,  334,  504,  467,  403,
125342 /*   110 */   477,  464,  403,  403,  403,  403,  860,  747,  744,  785,
125343 /*   120 */   638,  638,  926,  891,  900,  899,  887,  844,  840,  835,
125344 /*   130 */   848,  830,  843,  829,  792,  839,  826,  737,  838,  795,
125345 /*   140 */   789,   47,  734,  530,  696,  777,  711,  677,  733,  730,
125346 /*   150 */   729,  728,  727,  627,  448,   64,  187, 1305, 1302, 1252,
125347 /*   160 */  1290, 1273, 1323, 1322, 1321, 1319, 1318, 1316, 1315, 1314,
125348 /*   170 */  1313, 1312, 1311, 1310, 1308, 1307, 1304, 1303, 1301, 1298,
125349 /*   180 */  1294, 1292, 1289, 1266, 1264, 1259, 1288, 1287, 1238, 1285,
125350 /*   190 */  1281, 1280, 1279, 1278, 1251, 1277, 1276, 1275, 1273, 1268,
125351 /*   200 */  1267, 1265, 1263, 1261, 1257, 1248, 1237, 1247, 1246, 1243,
125352 /*   210 */  1238, 1240, 1235, 1249, 1234, 1233, 1230, 1220, 1214, 1210,
125353 /*   220 */  1225, 1219, 1232, 1231, 1197, 1195, 1227, 1224, 1201, 1208,
125354 /*   230 */  1242, 1137, 1236, 1229, 1193, 1181, 1221, 1177, 1196, 1179,
125355 /*   240 */  1191, 1190, 1186, 1182, 1218, 1216, 1176, 1162, 1183, 1180,
125356 /*   250 */  1160, 1199, 1203, 1133, 1095, 1198, 1194, 1188, 1192, 1171,
125357 /*   260 */  1169, 1168, 1173, 1174, 1166, 1159, 1141, 1170, 1158, 1167,
125358 /*   270 */  1157, 1132, 1145, 1143, 1124, 1128, 1103, 1102, 1100, 1096,
125359 /*   280 */  1150, 1149, 1085, 1125, 1080, 1064, 1120, 1097, 1082, 1078,
125360 /*   290 */  1073, 1067, 1109, 1107, 1119, 1117, 1116, 1113, 1111, 1108,
125361 /*   300 */  1007, 1000, 1002, 1076, 1075, 1081,
125362};
125363static const YYACTIONTYPE yy_default[] = {
125364 /*     0 */   647,  964,  964,  964,  878,  878,  969,  964,  774,  802,
125365 /*    10 */   802,  938,  969,  969,  969,  876,  969,  969,  969,  964,
125366 /*    20 */   969,  778,  808,  969,  969,  969,  969,  969,  969,  969,
125367 /*    30 */   969,  937,  939,  816,  815,  918,  789,  813,  806,  810,
125368 /*    40 */   879,  872,  873,  871,  875,  880,  969,  809,  841,  856,
125369 /*    50 */   840,  969,  969,  969,  969,  969,  969,  969,  969,  969,
125370 /*    60 */   969,  969,  969,  969,  969,  969,  969,  969,  969,  969,
125371 /*    70 */   969,  969,  969,  969,  969,  969,  969,  969,  969,  969,
125372 /*    80 */   969,  969,  969,  969,  969,  969,  969,  969,  969,  969,
125373 /*    90 */   969,  969,  969,  969,  850,  855,  862,  854,  851,  843,
125374 /*   100 */   842,  844,  845,  969,  969,  673,  739,  969,  969,  846,
125375 /*   110 */   969,  685,  847,  859,  858,  857,  680,  969,  969,  969,
125376 /*   120 */   969,  969,  969,  969,  969,  969,  969,  969,  969,  969,
125377 /*   130 */   969,  969,  969,  969,  969,  969,  969,  969,  969,  969,
125378 /*   140 */   969,  969,  969,  969,  647,  964,  969,  969,  964,  964,
125379 /*   150 */   964,  964,  964,  964,  956,  778,  768,  969,  969,  969,
125380 /*   160 */   969,  969,  969,  969,  969,  969,  969,  944,  942,  969,
125381 /*   170 */   891,  969,  969,  969,  969,  969,  969,  969,  969,  969,
125382 /*   180 */   969,  969,  969,  969,  969,  969,  969,  969,  969,  969,
125383 /*   190 */   969,  969,  969,  969,  969,  969,  969,  969,  969,  969,
125384 /*   200 */   969,  969,  969,  969,  653,  969,  911,  774,  774,  774,
125385 /*   210 */   776,  754,  766,  655,  812,  791,  791,  923,  812,  923,
125386 /*   220 */   710,  733,  707,  802,  791,  874,  802,  802,  775,  766,
125387 /*   230 */   969,  949,  782,  782,  941,  941,  782,  821,  743,  812,
125388 /*   240 */   750,  750,  750,  750,  782,  670,  812,  821,  743,  743,
125389 /*   250 */   812,  782,  670,  917,  915,  782,  782,  670,  782,  670,
125390 /*   260 */   782,  670,  884,  741,  741,  741,  725,  884,  741,  710,
125391 /*   270 */   741,  725,  741,  741,  795,  790,  795,  790,  795,  790,
125392 /*   280 */   782,  782,  969,  884,  888,  888,  884,  807,  796,  805,
125393 /*   290 */   803,  812,  676,  728,  663,  663,  652,  652,  652,  652,
125394 /*   300 */   961,  961,  956,  712,  712,  695,  969,  969,  969,  969,
125395 /*   310 */   969,  969,  687,  969,  893,  969,  969,  969,  969,  969,
125396 /*   320 */   969,  969,  969,  969,  969,  969,  969,  969,  969,  969,
125397 /*   330 */   969,  828,  969,  648,  951,  969,  969,  948,  969,  969,
125398 /*   340 */   969,  969,  969,  969,  969,  969,  969,  969,  969,  969,
125399 /*   350 */   969,  969,  969,  969,  969,  969,  921,  969,  969,  969,
125400 /*   360 */   969,  969,  969,  914,  913,  969,  969,  969,  969,  969,
125401 /*   370 */   969,  969,  969,  969,  969,  969,  969,  969,  969,  969,
125402 /*   380 */   969,  969,  969,  969,  969,  969,  969,  757,  969,  969,
125403 /*   390 */   969,  761,  969,  969,  969,  969,  969,  969,  804,  969,
125404 /*   400 */   797,  969,  877,  969,  969,  969,  969,  969,  969,  969,
125405 /*   410 */   969,  969,  969,  966,  969,  969,  969,  965,  969,  969,
125406 /*   420 */   969,  969,  969,  830,  969,  829,  833,  969,  661,  969,
125407 /*   430 */   644,  649,  960,  963,  962,  959,  958,  957,  952,  950,
125408 /*   440 */   947,  946,  945,  943,  940,  936,  897,  895,  902,  901,
125409 /*   450 */   900,  899,  898,  896,  894,  892,  818,  817,  814,  811,
125410 /*   460 */   753,  935,  890,  752,  749,  748,  669,  953,  920,  929,
125411 /*   470 */   928,  927,  822,  926,  925,  924,  922,  919,  906,  820,
125412 /*   480 */   819,  744,  882,  881,  672,  910,  909,  908,  912,  916,
125413 /*   490 */   907,  784,  751,  671,  668,  675,  679,  731,  732,  740,
125414 /*   500 */   738,  737,  736,  735,  734,  730,  681,  686,  724,  709,
125415 /*   510 */   708,  717,  716,  722,  721,  720,  719,  718,  715,  714,
125416 /*   520 */   713,  706,  705,  711,  704,  727,  726,  723,  703,  747,
125417 /*   530 */   746,  745,  742,  702,  701,  700,  833,  699,  698,  838,
125418 /*   540 */   837,  866,  826,  755,  759,  758,  762,  763,  771,  770,
125419 /*   550 */   769,  780,  781,  793,  792,  824,  823,  794,  779,  773,
125420 /*   560 */   772,  788,  787,  786,  785,  777,  767,  799,  798,  868,
125421 /*   570 */   783,  867,  865,  934,  933,  932,  931,  930,  870,  967,
125422 /*   580 */   968,  887,  889,  886,  801,  800,  885,  869,  839,  836,
125423 /*   590 */   690,  691,  905,  904,  903,  693,  692,  689,  688,  863,
125424 /*   600 */   860,  852,  864,  861,  853,  849,  848,  834,  832,  831,
125425 /*   610 */   827,  835,  760,  756,  825,  765,  764,  697,  696,  694,
125426 /*   620 */   678,  677,  674,  667,  665,  664,  666,  662,  660,  659,
125427 /*   630 */   658,  657,  656,  684,  683,  682,  654,  651,  650,  646,
125428 /*   640 */   645,  643,
125429};
125430
125431/* The next table maps tokens into fallback tokens.  If a construct
125432** like the following:
125433**
125434**      %fallback ID X Y Z.
125435**
125436** appears in the grammar, then ID becomes a fallback token for X, Y,
125437** and Z.  Whenever one of the tokens X, Y, or Z is input to the parser
125438** but it does not parse, the type of the token is changed to ID and
125439** the parse is retried before an error is thrown.
125440*/
125441#ifdef YYFALLBACK
125442static const YYCODETYPE yyFallback[] = {
125443    0,  /*          $ => nothing */
125444    0,  /*       SEMI => nothing */
125445   27,  /*    EXPLAIN => ID */
125446   27,  /*      QUERY => ID */
125447   27,  /*       PLAN => ID */
125448   27,  /*      BEGIN => ID */
125449    0,  /* TRANSACTION => nothing */
125450   27,  /*   DEFERRED => ID */
125451   27,  /*  IMMEDIATE => ID */
125452   27,  /*  EXCLUSIVE => ID */
125453    0,  /*     COMMIT => nothing */
125454   27,  /*        END => ID */
125455   27,  /*   ROLLBACK => ID */
125456   27,  /*  SAVEPOINT => ID */
125457   27,  /*    RELEASE => ID */
125458    0,  /*         TO => nothing */
125459    0,  /*      TABLE => nothing */
125460    0,  /*     CREATE => nothing */
125461   27,  /*         IF => ID */
125462    0,  /*        NOT => nothing */
125463    0,  /*     EXISTS => nothing */
125464   27,  /*       TEMP => ID */
125465    0,  /*         LP => nothing */
125466    0,  /*         RP => nothing */
125467    0,  /*         AS => nothing */
125468   27,  /*    WITHOUT => ID */
125469    0,  /*      COMMA => nothing */
125470    0,  /*         ID => nothing */
125471    0,  /*    INDEXED => nothing */
125472   27,  /*      ABORT => ID */
125473   27,  /*     ACTION => ID */
125474   27,  /*      AFTER => ID */
125475   27,  /*    ANALYZE => ID */
125476   27,  /*        ASC => ID */
125477   27,  /*     ATTACH => ID */
125478   27,  /*     BEFORE => ID */
125479   27,  /*         BY => ID */
125480   27,  /*    CASCADE => ID */
125481   27,  /*       CAST => ID */
125482   27,  /*   COLUMNKW => ID */
125483   27,  /*   CONFLICT => ID */
125484   27,  /*   DATABASE => ID */
125485   27,  /*       DESC => ID */
125486   27,  /*     DETACH => ID */
125487   27,  /*       EACH => ID */
125488   27,  /*       FAIL => ID */
125489   27,  /*        FOR => ID */
125490   27,  /*     IGNORE => ID */
125491   27,  /*  INITIALLY => ID */
125492   27,  /*    INSTEAD => ID */
125493   27,  /*    LIKE_KW => ID */
125494   27,  /*      MATCH => ID */
125495   27,  /*         NO => ID */
125496   27,  /*        KEY => ID */
125497   27,  /*         OF => ID */
125498   27,  /*     OFFSET => ID */
125499   27,  /*     PRAGMA => ID */
125500   27,  /*      RAISE => ID */
125501   27,  /*  RECURSIVE => ID */
125502   27,  /*    REPLACE => ID */
125503   27,  /*   RESTRICT => ID */
125504   27,  /*        ROW => ID */
125505   27,  /*    TRIGGER => ID */
125506   27,  /*     VACUUM => ID */
125507   27,  /*       VIEW => ID */
125508   27,  /*    VIRTUAL => ID */
125509   27,  /*       WITH => ID */
125510   27,  /*    REINDEX => ID */
125511   27,  /*     RENAME => ID */
125512   27,  /*   CTIME_KW => ID */
125513};
125514#endif /* YYFALLBACK */
125515
125516/* The following structure represents a single element of the
125517** parser's stack.  Information stored includes:
125518**
125519**   +  The state number for the parser at this level of the stack.
125520**
125521**   +  The value of the token stored at this level of the stack.
125522**      (In other words, the "major" token.)
125523**
125524**   +  The semantic value stored at this level of the stack.  This is
125525**      the information used by the action routines in the grammar.
125526**      It is sometimes called the "minor" token.
125527*/
125528struct yyStackEntry {
125529  YYACTIONTYPE stateno;  /* The state-number */
125530  YYCODETYPE major;      /* The major token value.  This is the code
125531                         ** number for the token at this stack level */
125532  YYMINORTYPE minor;     /* The user-supplied minor token value.  This
125533                         ** is the value of the token  */
125534};
125535typedef struct yyStackEntry yyStackEntry;
125536
125537/* The state of the parser is completely contained in an instance of
125538** the following structure */
125539struct yyParser {
125540  int yyidx;                    /* Index of top element in stack */
125541#ifdef YYTRACKMAXSTACKDEPTH
125542  int yyidxMax;                 /* Maximum value of yyidx */
125543#endif
125544  int yyerrcnt;                 /* Shifts left before out of the error */
125545  sqlite3ParserARG_SDECL                /* A place to hold %extra_argument */
125546#if YYSTACKDEPTH<=0
125547  int yystksz;                  /* Current side of the stack */
125548  yyStackEntry *yystack;        /* The parser's stack */
125549#else
125550  yyStackEntry yystack[YYSTACKDEPTH];  /* The parser's stack */
125551#endif
125552};
125553typedef struct yyParser yyParser;
125554
125555#ifndef NDEBUG
125556/* #include <stdio.h> */
125557static FILE *yyTraceFILE = 0;
125558static char *yyTracePrompt = 0;
125559#endif /* NDEBUG */
125560
125561#ifndef NDEBUG
125562/*
125563** Turn parser tracing on by giving a stream to which to write the trace
125564** and a prompt to preface each trace message.  Tracing is turned off
125565** by making either argument NULL
125566**
125567** Inputs:
125568** <ul>
125569** <li> A FILE* to which trace output should be written.
125570**      If NULL, then tracing is turned off.
125571** <li> A prefix string written at the beginning of every
125572**      line of trace output.  If NULL, then tracing is
125573**      turned off.
125574** </ul>
125575**
125576** Outputs:
125577** None.
125578*/
125579SQLITE_PRIVATE void sqlite3ParserTrace(FILE *TraceFILE, char *zTracePrompt){
125580  yyTraceFILE = TraceFILE;
125581  yyTracePrompt = zTracePrompt;
125582  if( yyTraceFILE==0 ) yyTracePrompt = 0;
125583  else if( yyTracePrompt==0 ) yyTraceFILE = 0;
125584}
125585#endif /* NDEBUG */
125586
125587#ifndef NDEBUG
125588/* For tracing shifts, the names of all terminals and nonterminals
125589** are required.  The following table supplies these names */
125590static const char *const yyTokenName[] = {
125591  "$",             "SEMI",          "EXPLAIN",       "QUERY",
125592  "PLAN",          "BEGIN",         "TRANSACTION",   "DEFERRED",
125593  "IMMEDIATE",     "EXCLUSIVE",     "COMMIT",        "END",
125594  "ROLLBACK",      "SAVEPOINT",     "RELEASE",       "TO",
125595  "TABLE",         "CREATE",        "IF",            "NOT",
125596  "EXISTS",        "TEMP",          "LP",            "RP",
125597  "AS",            "WITHOUT",       "COMMA",         "ID",
125598  "INDEXED",       "ABORT",         "ACTION",        "AFTER",
125599  "ANALYZE",       "ASC",           "ATTACH",        "BEFORE",
125600  "BY",            "CASCADE",       "CAST",          "COLUMNKW",
125601  "CONFLICT",      "DATABASE",      "DESC",          "DETACH",
125602  "EACH",          "FAIL",          "FOR",           "IGNORE",
125603  "INITIALLY",     "INSTEAD",       "LIKE_KW",       "MATCH",
125604  "NO",            "KEY",           "OF",            "OFFSET",
125605  "PRAGMA",        "RAISE",         "RECURSIVE",     "REPLACE",
125606  "RESTRICT",      "ROW",           "TRIGGER",       "VACUUM",
125607  "VIEW",          "VIRTUAL",       "WITH",          "REINDEX",
125608  "RENAME",        "CTIME_KW",      "ANY",           "OR",
125609  "AND",           "IS",            "BETWEEN",       "IN",
125610  "ISNULL",        "NOTNULL",       "NE",            "EQ",
125611  "GT",            "LE",            "LT",            "GE",
125612  "ESCAPE",        "BITAND",        "BITOR",         "LSHIFT",
125613  "RSHIFT",        "PLUS",          "MINUS",         "STAR",
125614  "SLASH",         "REM",           "CONCAT",        "COLLATE",
125615  "BITNOT",        "STRING",        "JOIN_KW",       "CONSTRAINT",
125616  "DEFAULT",       "NULL",          "PRIMARY",       "UNIQUE",
125617  "CHECK",         "REFERENCES",    "AUTOINCR",      "ON",
125618  "INSERT",        "DELETE",        "UPDATE",        "SET",
125619  "DEFERRABLE",    "FOREIGN",       "DROP",          "UNION",
125620  "ALL",           "EXCEPT",        "INTERSECT",     "SELECT",
125621  "VALUES",        "DISTINCT",      "DOT",           "FROM",
125622  "JOIN",          "USING",         "ORDER",         "GROUP",
125623  "HAVING",        "LIMIT",         "WHERE",         "INTO",
125624  "INTEGER",       "FLOAT",         "BLOB",          "VARIABLE",
125625  "CASE",          "WHEN",          "THEN",          "ELSE",
125626  "INDEX",         "ALTER",         "ADD",           "error",
125627  "input",         "cmdlist",       "ecmd",          "explain",
125628  "cmdx",          "cmd",           "transtype",     "trans_opt",
125629  "nm",            "savepoint_opt",  "create_table",  "create_table_args",
125630  "createkw",      "temp",          "ifnotexists",   "dbnm",
125631  "columnlist",    "conslist_opt",  "table_options",  "select",
125632  "column",        "columnid",      "type",          "carglist",
125633  "typetoken",     "typename",      "signed",        "plus_num",
125634  "minus_num",     "ccons",         "term",          "expr",
125635  "onconf",        "sortorder",     "autoinc",       "idxlist_opt",
125636  "refargs",       "defer_subclause",  "refarg",        "refact",
125637  "init_deferred_pred_opt",  "conslist",      "tconscomma",    "tcons",
125638  "idxlist",       "defer_subclause_opt",  "orconf",        "resolvetype",
125639  "raisetype",     "ifexists",      "fullname",      "selectnowith",
125640  "oneselect",     "with",          "multiselect_op",  "distinct",
125641  "selcollist",    "from",          "where_opt",     "groupby_opt",
125642  "having_opt",    "orderby_opt",   "limit_opt",     "values",
125643  "nexprlist",     "exprlist",      "sclp",          "as",
125644  "seltablist",    "stl_prefix",    "joinop",        "indexed_opt",
125645  "on_opt",        "using_opt",     "joinop2",       "idlist",
125646  "sortlist",      "setlist",       "insert_cmd",    "inscollist_opt",
125647  "likeop",        "between_op",    "in_op",         "case_operand",
125648  "case_exprlist",  "case_else",     "uniqueflag",    "collate",
125649  "nmnum",         "trigger_decl",  "trigger_cmd_list",  "trigger_time",
125650  "trigger_event",  "foreach_clause",  "when_clause",   "trigger_cmd",
125651  "trnm",          "tridxby",       "database_kw_opt",  "key_opt",
125652  "add_column_fullname",  "kwcolumn_opt",  "create_vtab",   "vtabarglist",
125653  "vtabarg",       "vtabargtoken",  "lp",            "anylist",
125654  "wqlist",
125655};
125656#endif /* NDEBUG */
125657
125658#ifndef NDEBUG
125659/* For tracing reduce actions, the names of all rules are required.
125660*/
125661static const char *const yyRuleName[] = {
125662 /*   0 */ "input ::= cmdlist",
125663 /*   1 */ "cmdlist ::= cmdlist ecmd",
125664 /*   2 */ "cmdlist ::= ecmd",
125665 /*   3 */ "ecmd ::= SEMI",
125666 /*   4 */ "ecmd ::= explain cmdx SEMI",
125667 /*   5 */ "explain ::=",
125668 /*   6 */ "explain ::= EXPLAIN",
125669 /*   7 */ "explain ::= EXPLAIN QUERY PLAN",
125670 /*   8 */ "cmdx ::= cmd",
125671 /*   9 */ "cmd ::= BEGIN transtype trans_opt",
125672 /*  10 */ "trans_opt ::=",
125673 /*  11 */ "trans_opt ::= TRANSACTION",
125674 /*  12 */ "trans_opt ::= TRANSACTION nm",
125675 /*  13 */ "transtype ::=",
125676 /*  14 */ "transtype ::= DEFERRED",
125677 /*  15 */ "transtype ::= IMMEDIATE",
125678 /*  16 */ "transtype ::= EXCLUSIVE",
125679 /*  17 */ "cmd ::= COMMIT trans_opt",
125680 /*  18 */ "cmd ::= END trans_opt",
125681 /*  19 */ "cmd ::= ROLLBACK trans_opt",
125682 /*  20 */ "savepoint_opt ::= SAVEPOINT",
125683 /*  21 */ "savepoint_opt ::=",
125684 /*  22 */ "cmd ::= SAVEPOINT nm",
125685 /*  23 */ "cmd ::= RELEASE savepoint_opt nm",
125686 /*  24 */ "cmd ::= ROLLBACK trans_opt TO savepoint_opt nm",
125687 /*  25 */ "cmd ::= create_table create_table_args",
125688 /*  26 */ "create_table ::= createkw temp TABLE ifnotexists nm dbnm",
125689 /*  27 */ "createkw ::= CREATE",
125690 /*  28 */ "ifnotexists ::=",
125691 /*  29 */ "ifnotexists ::= IF NOT EXISTS",
125692 /*  30 */ "temp ::= TEMP",
125693 /*  31 */ "temp ::=",
125694 /*  32 */ "create_table_args ::= LP columnlist conslist_opt RP table_options",
125695 /*  33 */ "create_table_args ::= AS select",
125696 /*  34 */ "table_options ::=",
125697 /*  35 */ "table_options ::= WITHOUT nm",
125698 /*  36 */ "columnlist ::= columnlist COMMA column",
125699 /*  37 */ "columnlist ::= column",
125700 /*  38 */ "column ::= columnid type carglist",
125701 /*  39 */ "columnid ::= nm",
125702 /*  40 */ "nm ::= ID|INDEXED",
125703 /*  41 */ "nm ::= STRING",
125704 /*  42 */ "nm ::= JOIN_KW",
125705 /*  43 */ "type ::=",
125706 /*  44 */ "type ::= typetoken",
125707 /*  45 */ "typetoken ::= typename",
125708 /*  46 */ "typetoken ::= typename LP signed RP",
125709 /*  47 */ "typetoken ::= typename LP signed COMMA signed RP",
125710 /*  48 */ "typename ::= ID|STRING",
125711 /*  49 */ "typename ::= typename ID|STRING",
125712 /*  50 */ "signed ::= plus_num",
125713 /*  51 */ "signed ::= minus_num",
125714 /*  52 */ "carglist ::= carglist ccons",
125715 /*  53 */ "carglist ::=",
125716 /*  54 */ "ccons ::= CONSTRAINT nm",
125717 /*  55 */ "ccons ::= DEFAULT term",
125718 /*  56 */ "ccons ::= DEFAULT LP expr RP",
125719 /*  57 */ "ccons ::= DEFAULT PLUS term",
125720 /*  58 */ "ccons ::= DEFAULT MINUS term",
125721 /*  59 */ "ccons ::= DEFAULT ID|INDEXED",
125722 /*  60 */ "ccons ::= NULL onconf",
125723 /*  61 */ "ccons ::= NOT NULL onconf",
125724 /*  62 */ "ccons ::= PRIMARY KEY sortorder onconf autoinc",
125725 /*  63 */ "ccons ::= UNIQUE onconf",
125726 /*  64 */ "ccons ::= CHECK LP expr RP",
125727 /*  65 */ "ccons ::= REFERENCES nm idxlist_opt refargs",
125728 /*  66 */ "ccons ::= defer_subclause",
125729 /*  67 */ "ccons ::= COLLATE ID|STRING",
125730 /*  68 */ "autoinc ::=",
125731 /*  69 */ "autoinc ::= AUTOINCR",
125732 /*  70 */ "refargs ::=",
125733 /*  71 */ "refargs ::= refargs refarg",
125734 /*  72 */ "refarg ::= MATCH nm",
125735 /*  73 */ "refarg ::= ON INSERT refact",
125736 /*  74 */ "refarg ::= ON DELETE refact",
125737 /*  75 */ "refarg ::= ON UPDATE refact",
125738 /*  76 */ "refact ::= SET NULL",
125739 /*  77 */ "refact ::= SET DEFAULT",
125740 /*  78 */ "refact ::= CASCADE",
125741 /*  79 */ "refact ::= RESTRICT",
125742 /*  80 */ "refact ::= NO ACTION",
125743 /*  81 */ "defer_subclause ::= NOT DEFERRABLE init_deferred_pred_opt",
125744 /*  82 */ "defer_subclause ::= DEFERRABLE init_deferred_pred_opt",
125745 /*  83 */ "init_deferred_pred_opt ::=",
125746 /*  84 */ "init_deferred_pred_opt ::= INITIALLY DEFERRED",
125747 /*  85 */ "init_deferred_pred_opt ::= INITIALLY IMMEDIATE",
125748 /*  86 */ "conslist_opt ::=",
125749 /*  87 */ "conslist_opt ::= COMMA conslist",
125750 /*  88 */ "conslist ::= conslist tconscomma tcons",
125751 /*  89 */ "conslist ::= tcons",
125752 /*  90 */ "tconscomma ::= COMMA",
125753 /*  91 */ "tconscomma ::=",
125754 /*  92 */ "tcons ::= CONSTRAINT nm",
125755 /*  93 */ "tcons ::= PRIMARY KEY LP idxlist autoinc RP onconf",
125756 /*  94 */ "tcons ::= UNIQUE LP idxlist RP onconf",
125757 /*  95 */ "tcons ::= CHECK LP expr RP onconf",
125758 /*  96 */ "tcons ::= FOREIGN KEY LP idxlist RP REFERENCES nm idxlist_opt refargs defer_subclause_opt",
125759 /*  97 */ "defer_subclause_opt ::=",
125760 /*  98 */ "defer_subclause_opt ::= defer_subclause",
125761 /*  99 */ "onconf ::=",
125762 /* 100 */ "onconf ::= ON CONFLICT resolvetype",
125763 /* 101 */ "orconf ::=",
125764 /* 102 */ "orconf ::= OR resolvetype",
125765 /* 103 */ "resolvetype ::= raisetype",
125766 /* 104 */ "resolvetype ::= IGNORE",
125767 /* 105 */ "resolvetype ::= REPLACE",
125768 /* 106 */ "cmd ::= DROP TABLE ifexists fullname",
125769 /* 107 */ "ifexists ::= IF EXISTS",
125770 /* 108 */ "ifexists ::=",
125771 /* 109 */ "cmd ::= createkw temp VIEW ifnotexists nm dbnm AS select",
125772 /* 110 */ "cmd ::= DROP VIEW ifexists fullname",
125773 /* 111 */ "cmd ::= select",
125774 /* 112 */ "select ::= with selectnowith",
125775 /* 113 */ "selectnowith ::= oneselect",
125776 /* 114 */ "selectnowith ::= selectnowith multiselect_op oneselect",
125777 /* 115 */ "multiselect_op ::= UNION",
125778 /* 116 */ "multiselect_op ::= UNION ALL",
125779 /* 117 */ "multiselect_op ::= EXCEPT|INTERSECT",
125780 /* 118 */ "oneselect ::= SELECT distinct selcollist from where_opt groupby_opt having_opt orderby_opt limit_opt",
125781 /* 119 */ "oneselect ::= values",
125782 /* 120 */ "values ::= VALUES LP nexprlist RP",
125783 /* 121 */ "values ::= values COMMA LP exprlist RP",
125784 /* 122 */ "distinct ::= DISTINCT",
125785 /* 123 */ "distinct ::= ALL",
125786 /* 124 */ "distinct ::=",
125787 /* 125 */ "sclp ::= selcollist COMMA",
125788 /* 126 */ "sclp ::=",
125789 /* 127 */ "selcollist ::= sclp expr as",
125790 /* 128 */ "selcollist ::= sclp STAR",
125791 /* 129 */ "selcollist ::= sclp nm DOT STAR",
125792 /* 130 */ "as ::= AS nm",
125793 /* 131 */ "as ::= ID|STRING",
125794 /* 132 */ "as ::=",
125795 /* 133 */ "from ::=",
125796 /* 134 */ "from ::= FROM seltablist",
125797 /* 135 */ "stl_prefix ::= seltablist joinop",
125798 /* 136 */ "stl_prefix ::=",
125799 /* 137 */ "seltablist ::= stl_prefix nm dbnm as indexed_opt on_opt using_opt",
125800 /* 138 */ "seltablist ::= stl_prefix LP select RP as on_opt using_opt",
125801 /* 139 */ "seltablist ::= stl_prefix LP seltablist RP as on_opt using_opt",
125802 /* 140 */ "dbnm ::=",
125803 /* 141 */ "dbnm ::= DOT nm",
125804 /* 142 */ "fullname ::= nm dbnm",
125805 /* 143 */ "joinop ::= COMMA|JOIN",
125806 /* 144 */ "joinop ::= JOIN_KW JOIN",
125807 /* 145 */ "joinop ::= JOIN_KW nm JOIN",
125808 /* 146 */ "joinop ::= JOIN_KW nm nm JOIN",
125809 /* 147 */ "on_opt ::= ON expr",
125810 /* 148 */ "on_opt ::=",
125811 /* 149 */ "indexed_opt ::=",
125812 /* 150 */ "indexed_opt ::= INDEXED BY nm",
125813 /* 151 */ "indexed_opt ::= NOT INDEXED",
125814 /* 152 */ "using_opt ::= USING LP idlist RP",
125815 /* 153 */ "using_opt ::=",
125816 /* 154 */ "orderby_opt ::=",
125817 /* 155 */ "orderby_opt ::= ORDER BY sortlist",
125818 /* 156 */ "sortlist ::= sortlist COMMA expr sortorder",
125819 /* 157 */ "sortlist ::= expr sortorder",
125820 /* 158 */ "sortorder ::= ASC",
125821 /* 159 */ "sortorder ::= DESC",
125822 /* 160 */ "sortorder ::=",
125823 /* 161 */ "groupby_opt ::=",
125824 /* 162 */ "groupby_opt ::= GROUP BY nexprlist",
125825 /* 163 */ "having_opt ::=",
125826 /* 164 */ "having_opt ::= HAVING expr",
125827 /* 165 */ "limit_opt ::=",
125828 /* 166 */ "limit_opt ::= LIMIT expr",
125829 /* 167 */ "limit_opt ::= LIMIT expr OFFSET expr",
125830 /* 168 */ "limit_opt ::= LIMIT expr COMMA expr",
125831 /* 169 */ "cmd ::= with DELETE FROM fullname indexed_opt where_opt",
125832 /* 170 */ "where_opt ::=",
125833 /* 171 */ "where_opt ::= WHERE expr",
125834 /* 172 */ "cmd ::= with UPDATE orconf fullname indexed_opt SET setlist where_opt",
125835 /* 173 */ "setlist ::= setlist COMMA nm EQ expr",
125836 /* 174 */ "setlist ::= nm EQ expr",
125837 /* 175 */ "cmd ::= with insert_cmd INTO fullname inscollist_opt select",
125838 /* 176 */ "cmd ::= with insert_cmd INTO fullname inscollist_opt DEFAULT VALUES",
125839 /* 177 */ "insert_cmd ::= INSERT orconf",
125840 /* 178 */ "insert_cmd ::= REPLACE",
125841 /* 179 */ "inscollist_opt ::=",
125842 /* 180 */ "inscollist_opt ::= LP idlist RP",
125843 /* 181 */ "idlist ::= idlist COMMA nm",
125844 /* 182 */ "idlist ::= nm",
125845 /* 183 */ "expr ::= term",
125846 /* 184 */ "expr ::= LP expr RP",
125847 /* 185 */ "term ::= NULL",
125848 /* 186 */ "expr ::= ID|INDEXED",
125849 /* 187 */ "expr ::= JOIN_KW",
125850 /* 188 */ "expr ::= nm DOT nm",
125851 /* 189 */ "expr ::= nm DOT nm DOT nm",
125852 /* 190 */ "term ::= INTEGER|FLOAT|BLOB",
125853 /* 191 */ "term ::= STRING",
125854 /* 192 */ "expr ::= VARIABLE",
125855 /* 193 */ "expr ::= expr COLLATE ID|STRING",
125856 /* 194 */ "expr ::= CAST LP expr AS typetoken RP",
125857 /* 195 */ "expr ::= ID|INDEXED LP distinct exprlist RP",
125858 /* 196 */ "expr ::= ID|INDEXED LP STAR RP",
125859 /* 197 */ "term ::= CTIME_KW",
125860 /* 198 */ "expr ::= expr AND expr",
125861 /* 199 */ "expr ::= expr OR expr",
125862 /* 200 */ "expr ::= expr LT|GT|GE|LE expr",
125863 /* 201 */ "expr ::= expr EQ|NE expr",
125864 /* 202 */ "expr ::= expr BITAND|BITOR|LSHIFT|RSHIFT expr",
125865 /* 203 */ "expr ::= expr PLUS|MINUS expr",
125866 /* 204 */ "expr ::= expr STAR|SLASH|REM expr",
125867 /* 205 */ "expr ::= expr CONCAT expr",
125868 /* 206 */ "likeop ::= LIKE_KW|MATCH",
125869 /* 207 */ "likeop ::= NOT LIKE_KW|MATCH",
125870 /* 208 */ "expr ::= expr likeop expr",
125871 /* 209 */ "expr ::= expr likeop expr ESCAPE expr",
125872 /* 210 */ "expr ::= expr ISNULL|NOTNULL",
125873 /* 211 */ "expr ::= expr NOT NULL",
125874 /* 212 */ "expr ::= expr IS expr",
125875 /* 213 */ "expr ::= expr IS NOT expr",
125876 /* 214 */ "expr ::= NOT expr",
125877 /* 215 */ "expr ::= BITNOT expr",
125878 /* 216 */ "expr ::= MINUS expr",
125879 /* 217 */ "expr ::= PLUS expr",
125880 /* 218 */ "between_op ::= BETWEEN",
125881 /* 219 */ "between_op ::= NOT BETWEEN",
125882 /* 220 */ "expr ::= expr between_op expr AND expr",
125883 /* 221 */ "in_op ::= IN",
125884 /* 222 */ "in_op ::= NOT IN",
125885 /* 223 */ "expr ::= expr in_op LP exprlist RP",
125886 /* 224 */ "expr ::= LP select RP",
125887 /* 225 */ "expr ::= expr in_op LP select RP",
125888 /* 226 */ "expr ::= expr in_op nm dbnm",
125889 /* 227 */ "expr ::= EXISTS LP select RP",
125890 /* 228 */ "expr ::= CASE case_operand case_exprlist case_else END",
125891 /* 229 */ "case_exprlist ::= case_exprlist WHEN expr THEN expr",
125892 /* 230 */ "case_exprlist ::= WHEN expr THEN expr",
125893 /* 231 */ "case_else ::= ELSE expr",
125894 /* 232 */ "case_else ::=",
125895 /* 233 */ "case_operand ::= expr",
125896 /* 234 */ "case_operand ::=",
125897 /* 235 */ "exprlist ::= nexprlist",
125898 /* 236 */ "exprlist ::=",
125899 /* 237 */ "nexprlist ::= nexprlist COMMA expr",
125900 /* 238 */ "nexprlist ::= expr",
125901 /* 239 */ "cmd ::= createkw uniqueflag INDEX ifnotexists nm dbnm ON nm LP idxlist RP where_opt",
125902 /* 240 */ "uniqueflag ::= UNIQUE",
125903 /* 241 */ "uniqueflag ::=",
125904 /* 242 */ "idxlist_opt ::=",
125905 /* 243 */ "idxlist_opt ::= LP idxlist RP",
125906 /* 244 */ "idxlist ::= idxlist COMMA nm collate sortorder",
125907 /* 245 */ "idxlist ::= nm collate sortorder",
125908 /* 246 */ "collate ::=",
125909 /* 247 */ "collate ::= COLLATE ID|STRING",
125910 /* 248 */ "cmd ::= DROP INDEX ifexists fullname",
125911 /* 249 */ "cmd ::= VACUUM",
125912 /* 250 */ "cmd ::= VACUUM nm",
125913 /* 251 */ "cmd ::= PRAGMA nm dbnm",
125914 /* 252 */ "cmd ::= PRAGMA nm dbnm EQ nmnum",
125915 /* 253 */ "cmd ::= PRAGMA nm dbnm LP nmnum RP",
125916 /* 254 */ "cmd ::= PRAGMA nm dbnm EQ minus_num",
125917 /* 255 */ "cmd ::= PRAGMA nm dbnm LP minus_num RP",
125918 /* 256 */ "nmnum ::= plus_num",
125919 /* 257 */ "nmnum ::= nm",
125920 /* 258 */ "nmnum ::= ON",
125921 /* 259 */ "nmnum ::= DELETE",
125922 /* 260 */ "nmnum ::= DEFAULT",
125923 /* 261 */ "plus_num ::= PLUS INTEGER|FLOAT",
125924 /* 262 */ "plus_num ::= INTEGER|FLOAT",
125925 /* 263 */ "minus_num ::= MINUS INTEGER|FLOAT",
125926 /* 264 */ "cmd ::= createkw trigger_decl BEGIN trigger_cmd_list END",
125927 /* 265 */ "trigger_decl ::= temp TRIGGER ifnotexists nm dbnm trigger_time trigger_event ON fullname foreach_clause when_clause",
125928 /* 266 */ "trigger_time ::= BEFORE",
125929 /* 267 */ "trigger_time ::= AFTER",
125930 /* 268 */ "trigger_time ::= INSTEAD OF",
125931 /* 269 */ "trigger_time ::=",
125932 /* 270 */ "trigger_event ::= DELETE|INSERT",
125933 /* 271 */ "trigger_event ::= UPDATE",
125934 /* 272 */ "trigger_event ::= UPDATE OF idlist",
125935 /* 273 */ "foreach_clause ::=",
125936 /* 274 */ "foreach_clause ::= FOR EACH ROW",
125937 /* 275 */ "when_clause ::=",
125938 /* 276 */ "when_clause ::= WHEN expr",
125939 /* 277 */ "trigger_cmd_list ::= trigger_cmd_list trigger_cmd SEMI",
125940 /* 278 */ "trigger_cmd_list ::= trigger_cmd SEMI",
125941 /* 279 */ "trnm ::= nm",
125942 /* 280 */ "trnm ::= nm DOT nm",
125943 /* 281 */ "tridxby ::=",
125944 /* 282 */ "tridxby ::= INDEXED BY nm",
125945 /* 283 */ "tridxby ::= NOT INDEXED",
125946 /* 284 */ "trigger_cmd ::= UPDATE orconf trnm tridxby SET setlist where_opt",
125947 /* 285 */ "trigger_cmd ::= insert_cmd INTO trnm inscollist_opt select",
125948 /* 286 */ "trigger_cmd ::= DELETE FROM trnm tridxby where_opt",
125949 /* 287 */ "trigger_cmd ::= select",
125950 /* 288 */ "expr ::= RAISE LP IGNORE RP",
125951 /* 289 */ "expr ::= RAISE LP raisetype COMMA nm RP",
125952 /* 290 */ "raisetype ::= ROLLBACK",
125953 /* 291 */ "raisetype ::= ABORT",
125954 /* 292 */ "raisetype ::= FAIL",
125955 /* 293 */ "cmd ::= DROP TRIGGER ifexists fullname",
125956 /* 294 */ "cmd ::= ATTACH database_kw_opt expr AS expr key_opt",
125957 /* 295 */ "cmd ::= DETACH database_kw_opt expr",
125958 /* 296 */ "key_opt ::=",
125959 /* 297 */ "key_opt ::= KEY expr",
125960 /* 298 */ "database_kw_opt ::= DATABASE",
125961 /* 299 */ "database_kw_opt ::=",
125962 /* 300 */ "cmd ::= REINDEX",
125963 /* 301 */ "cmd ::= REINDEX nm dbnm",
125964 /* 302 */ "cmd ::= ANALYZE",
125965 /* 303 */ "cmd ::= ANALYZE nm dbnm",
125966 /* 304 */ "cmd ::= ALTER TABLE fullname RENAME TO nm",
125967 /* 305 */ "cmd ::= ALTER TABLE add_column_fullname ADD kwcolumn_opt column",
125968 /* 306 */ "add_column_fullname ::= fullname",
125969 /* 307 */ "kwcolumn_opt ::=",
125970 /* 308 */ "kwcolumn_opt ::= COLUMNKW",
125971 /* 309 */ "cmd ::= create_vtab",
125972 /* 310 */ "cmd ::= create_vtab LP vtabarglist RP",
125973 /* 311 */ "create_vtab ::= createkw VIRTUAL TABLE ifnotexists nm dbnm USING nm",
125974 /* 312 */ "vtabarglist ::= vtabarg",
125975 /* 313 */ "vtabarglist ::= vtabarglist COMMA vtabarg",
125976 /* 314 */ "vtabarg ::=",
125977 /* 315 */ "vtabarg ::= vtabarg vtabargtoken",
125978 /* 316 */ "vtabargtoken ::= ANY",
125979 /* 317 */ "vtabargtoken ::= lp anylist RP",
125980 /* 318 */ "lp ::= LP",
125981 /* 319 */ "anylist ::=",
125982 /* 320 */ "anylist ::= anylist LP anylist RP",
125983 /* 321 */ "anylist ::= anylist ANY",
125984 /* 322 */ "with ::=",
125985 /* 323 */ "with ::= WITH wqlist",
125986 /* 324 */ "with ::= WITH RECURSIVE wqlist",
125987 /* 325 */ "wqlist ::= nm idxlist_opt AS LP select RP",
125988 /* 326 */ "wqlist ::= wqlist COMMA nm idxlist_opt AS LP select RP",
125989};
125990#endif /* NDEBUG */
125991
125992
125993#if YYSTACKDEPTH<=0
125994/*
125995** Try to increase the size of the parser stack.
125996*/
125997static void yyGrowStack(yyParser *p){
125998  int newSize;
125999  yyStackEntry *pNew;
126000
126001  newSize = p->yystksz*2 + 100;
126002  pNew = realloc(p->yystack, newSize*sizeof(pNew[0]));
126003  if( pNew ){
126004    p->yystack = pNew;
126005    p->yystksz = newSize;
126006#ifndef NDEBUG
126007    if( yyTraceFILE ){
126008      fprintf(yyTraceFILE,"%sStack grows to %d entries!\n",
126009              yyTracePrompt, p->yystksz);
126010    }
126011#endif
126012  }
126013}
126014#endif
126015
126016/*
126017** This function allocates a new parser.
126018** The only argument is a pointer to a function which works like
126019** malloc.
126020**
126021** Inputs:
126022** A pointer to the function used to allocate memory.
126023**
126024** Outputs:
126025** A pointer to a parser.  This pointer is used in subsequent calls
126026** to sqlite3Parser and sqlite3ParserFree.
126027*/
126028SQLITE_PRIVATE void *sqlite3ParserAlloc(void *(*mallocProc)(u64)){
126029  yyParser *pParser;
126030  pParser = (yyParser*)(*mallocProc)( (u64)sizeof(yyParser) );
126031  if( pParser ){
126032    pParser->yyidx = -1;
126033#ifdef YYTRACKMAXSTACKDEPTH
126034    pParser->yyidxMax = 0;
126035#endif
126036#if YYSTACKDEPTH<=0
126037    pParser->yystack = NULL;
126038    pParser->yystksz = 0;
126039    yyGrowStack(pParser);
126040#endif
126041  }
126042  return pParser;
126043}
126044
126045/* The following function deletes the value associated with a
126046** symbol.  The symbol can be either a terminal or nonterminal.
126047** "yymajor" is the symbol code, and "yypminor" is a pointer to
126048** the value.
126049*/
126050static void yy_destructor(
126051  yyParser *yypParser,    /* The parser */
126052  YYCODETYPE yymajor,     /* Type code for object to destroy */
126053  YYMINORTYPE *yypminor   /* The object to be destroyed */
126054){
126055  sqlite3ParserARG_FETCH;
126056  switch( yymajor ){
126057    /* Here is inserted the actions which take place when a
126058    ** terminal or non-terminal is destroyed.  This can happen
126059    ** when the symbol is popped from the stack during a
126060    ** reduce or during error processing or when a parser is
126061    ** being destroyed before it is finished parsing.
126062    **
126063    ** Note: during a reduce, the only symbols destroyed are those
126064    ** which appear on the RHS of the rule, but which are not used
126065    ** inside the C code.
126066    */
126067    case 163: /* select */
126068    case 195: /* selectnowith */
126069    case 196: /* oneselect */
126070    case 207: /* values */
126071{
126072sqlite3SelectDelete(pParse->db, (yypminor->yy3));
126073}
126074      break;
126075    case 174: /* term */
126076    case 175: /* expr */
126077{
126078sqlite3ExprDelete(pParse->db, (yypminor->yy346).pExpr);
126079}
126080      break;
126081    case 179: /* idxlist_opt */
126082    case 188: /* idxlist */
126083    case 200: /* selcollist */
126084    case 203: /* groupby_opt */
126085    case 205: /* orderby_opt */
126086    case 208: /* nexprlist */
126087    case 209: /* exprlist */
126088    case 210: /* sclp */
126089    case 220: /* sortlist */
126090    case 221: /* setlist */
126091    case 228: /* case_exprlist */
126092{
126093sqlite3ExprListDelete(pParse->db, (yypminor->yy14));
126094}
126095      break;
126096    case 194: /* fullname */
126097    case 201: /* from */
126098    case 212: /* seltablist */
126099    case 213: /* stl_prefix */
126100{
126101sqlite3SrcListDelete(pParse->db, (yypminor->yy65));
126102}
126103      break;
126104    case 197: /* with */
126105    case 252: /* wqlist */
126106{
126107sqlite3WithDelete(pParse->db, (yypminor->yy59));
126108}
126109      break;
126110    case 202: /* where_opt */
126111    case 204: /* having_opt */
126112    case 216: /* on_opt */
126113    case 227: /* case_operand */
126114    case 229: /* case_else */
126115    case 238: /* when_clause */
126116    case 243: /* key_opt */
126117{
126118sqlite3ExprDelete(pParse->db, (yypminor->yy132));
126119}
126120      break;
126121    case 217: /* using_opt */
126122    case 219: /* idlist */
126123    case 223: /* inscollist_opt */
126124{
126125sqlite3IdListDelete(pParse->db, (yypminor->yy408));
126126}
126127      break;
126128    case 234: /* trigger_cmd_list */
126129    case 239: /* trigger_cmd */
126130{
126131sqlite3DeleteTriggerStep(pParse->db, (yypminor->yy473));
126132}
126133      break;
126134    case 236: /* trigger_event */
126135{
126136sqlite3IdListDelete(pParse->db, (yypminor->yy378).b);
126137}
126138      break;
126139    default:  break;   /* If no destructor action specified: do nothing */
126140  }
126141}
126142
126143/*
126144** Pop the parser's stack once.
126145**
126146** If there is a destructor routine associated with the token which
126147** is popped from the stack, then call it.
126148**
126149** Return the major token number for the symbol popped.
126150*/
126151static int yy_pop_parser_stack(yyParser *pParser){
126152  YYCODETYPE yymajor;
126153  yyStackEntry *yytos = &pParser->yystack[pParser->yyidx];
126154
126155  /* There is no mechanism by which the parser stack can be popped below
126156  ** empty in SQLite.  */
126157  assert( pParser->yyidx>=0 );
126158#ifndef NDEBUG
126159  if( yyTraceFILE && pParser->yyidx>=0 ){
126160    fprintf(yyTraceFILE,"%sPopping %s\n",
126161      yyTracePrompt,
126162      yyTokenName[yytos->major]);
126163  }
126164#endif
126165  yymajor = yytos->major;
126166  yy_destructor(pParser, yymajor, &yytos->minor);
126167  pParser->yyidx--;
126168  return yymajor;
126169}
126170
126171/*
126172** Deallocate and destroy a parser.  Destructors are all called for
126173** all stack elements before shutting the parser down.
126174**
126175** Inputs:
126176** <ul>
126177** <li>  A pointer to the parser.  This should be a pointer
126178**       obtained from sqlite3ParserAlloc.
126179** <li>  A pointer to a function used to reclaim memory obtained
126180**       from malloc.
126181** </ul>
126182*/
126183SQLITE_PRIVATE void sqlite3ParserFree(
126184  void *p,                    /* The parser to be deleted */
126185  void (*freeProc)(void*)     /* Function used to reclaim memory */
126186){
126187  yyParser *pParser = (yyParser*)p;
126188  /* In SQLite, we never try to destroy a parser that was not successfully
126189  ** created in the first place. */
126190  if( NEVER(pParser==0) ) return;
126191  while( pParser->yyidx>=0 ) yy_pop_parser_stack(pParser);
126192#if YYSTACKDEPTH<=0
126193  free(pParser->yystack);
126194#endif
126195  (*freeProc)((void*)pParser);
126196}
126197
126198/*
126199** Return the peak depth of the stack for a parser.
126200*/
126201#ifdef YYTRACKMAXSTACKDEPTH
126202SQLITE_PRIVATE int sqlite3ParserStackPeak(void *p){
126203  yyParser *pParser = (yyParser*)p;
126204  return pParser->yyidxMax;
126205}
126206#endif
126207
126208/*
126209** Find the appropriate action for a parser given the terminal
126210** look-ahead token iLookAhead.
126211**
126212** If the look-ahead token is YYNOCODE, then check to see if the action is
126213** independent of the look-ahead.  If it is, return the action, otherwise
126214** return YY_NO_ACTION.
126215*/
126216static int yy_find_shift_action(
126217  yyParser *pParser,        /* The parser */
126218  YYCODETYPE iLookAhead     /* The look-ahead token */
126219){
126220  int i;
126221  int stateno = pParser->yystack[pParser->yyidx].stateno;
126222
126223  if( stateno>YY_SHIFT_COUNT
126224   || (i = yy_shift_ofst[stateno])==YY_SHIFT_USE_DFLT ){
126225    return yy_default[stateno];
126226  }
126227  assert( iLookAhead!=YYNOCODE );
126228  i += iLookAhead;
126229  if( i<0 || i>=YY_ACTTAB_COUNT || yy_lookahead[i]!=iLookAhead ){
126230    if( iLookAhead>0 ){
126231#ifdef YYFALLBACK
126232      YYCODETYPE iFallback;            /* Fallback token */
126233      if( iLookAhead<sizeof(yyFallback)/sizeof(yyFallback[0])
126234             && (iFallback = yyFallback[iLookAhead])!=0 ){
126235#ifndef NDEBUG
126236        if( yyTraceFILE ){
126237          fprintf(yyTraceFILE, "%sFALLBACK %s => %s\n",
126238             yyTracePrompt, yyTokenName[iLookAhead], yyTokenName[iFallback]);
126239        }
126240#endif
126241        return yy_find_shift_action(pParser, iFallback);
126242      }
126243#endif
126244#ifdef YYWILDCARD
126245      {
126246        int j = i - iLookAhead + YYWILDCARD;
126247        if(
126248#if YY_SHIFT_MIN+YYWILDCARD<0
126249          j>=0 &&
126250#endif
126251#if YY_SHIFT_MAX+YYWILDCARD>=YY_ACTTAB_COUNT
126252          j<YY_ACTTAB_COUNT &&
126253#endif
126254          yy_lookahead[j]==YYWILDCARD
126255        ){
126256#ifndef NDEBUG
126257          if( yyTraceFILE ){
126258            fprintf(yyTraceFILE, "%sWILDCARD %s => %s\n",
126259               yyTracePrompt, yyTokenName[iLookAhead], yyTokenName[YYWILDCARD]);
126260          }
126261#endif /* NDEBUG */
126262          return yy_action[j];
126263        }
126264      }
126265#endif /* YYWILDCARD */
126266    }
126267    return yy_default[stateno];
126268  }else{
126269    return yy_action[i];
126270  }
126271}
126272
126273/*
126274** Find the appropriate action for a parser given the non-terminal
126275** look-ahead token iLookAhead.
126276**
126277** If the look-ahead token is YYNOCODE, then check to see if the action is
126278** independent of the look-ahead.  If it is, return the action, otherwise
126279** return YY_NO_ACTION.
126280*/
126281static int yy_find_reduce_action(
126282  int stateno,              /* Current state number */
126283  YYCODETYPE iLookAhead     /* The look-ahead token */
126284){
126285  int i;
126286#ifdef YYERRORSYMBOL
126287  if( stateno>YY_REDUCE_COUNT ){
126288    return yy_default[stateno];
126289  }
126290#else
126291  assert( stateno<=YY_REDUCE_COUNT );
126292#endif
126293  i = yy_reduce_ofst[stateno];
126294  assert( i!=YY_REDUCE_USE_DFLT );
126295  assert( iLookAhead!=YYNOCODE );
126296  i += iLookAhead;
126297#ifdef YYERRORSYMBOL
126298  if( i<0 || i>=YY_ACTTAB_COUNT || yy_lookahead[i]!=iLookAhead ){
126299    return yy_default[stateno];
126300  }
126301#else
126302  assert( i>=0 && i<YY_ACTTAB_COUNT );
126303  assert( yy_lookahead[i]==iLookAhead );
126304#endif
126305  return yy_action[i];
126306}
126307
126308/*
126309** The following routine is called if the stack overflows.
126310*/
126311static void yyStackOverflow(yyParser *yypParser, YYMINORTYPE *yypMinor){
126312   sqlite3ParserARG_FETCH;
126313   yypParser->yyidx--;
126314#ifndef NDEBUG
126315   if( yyTraceFILE ){
126316     fprintf(yyTraceFILE,"%sStack Overflow!\n",yyTracePrompt);
126317   }
126318#endif
126319   while( yypParser->yyidx>=0 ) yy_pop_parser_stack(yypParser);
126320   /* Here code is inserted which will execute if the parser
126321   ** stack every overflows */
126322
126323  UNUSED_PARAMETER(yypMinor); /* Silence some compiler warnings */
126324  sqlite3ErrorMsg(pParse, "parser stack overflow");
126325   sqlite3ParserARG_STORE; /* Suppress warning about unused %extra_argument var */
126326}
126327
126328/*
126329** Perform a shift action.
126330*/
126331static void yy_shift(
126332  yyParser *yypParser,          /* The parser to be shifted */
126333  int yyNewState,               /* The new state to shift in */
126334  int yyMajor,                  /* The major token to shift in */
126335  YYMINORTYPE *yypMinor         /* Pointer to the minor token to shift in */
126336){
126337  yyStackEntry *yytos;
126338  yypParser->yyidx++;
126339#ifdef YYTRACKMAXSTACKDEPTH
126340  if( yypParser->yyidx>yypParser->yyidxMax ){
126341    yypParser->yyidxMax = yypParser->yyidx;
126342  }
126343#endif
126344#if YYSTACKDEPTH>0
126345  if( yypParser->yyidx>=YYSTACKDEPTH ){
126346    yyStackOverflow(yypParser, yypMinor);
126347    return;
126348  }
126349#else
126350  if( yypParser->yyidx>=yypParser->yystksz ){
126351    yyGrowStack(yypParser);
126352    if( yypParser->yyidx>=yypParser->yystksz ){
126353      yyStackOverflow(yypParser, yypMinor);
126354      return;
126355    }
126356  }
126357#endif
126358  yytos = &yypParser->yystack[yypParser->yyidx];
126359  yytos->stateno = (YYACTIONTYPE)yyNewState;
126360  yytos->major = (YYCODETYPE)yyMajor;
126361  yytos->minor = *yypMinor;
126362#ifndef NDEBUG
126363  if( yyTraceFILE && yypParser->yyidx>0 ){
126364    int i;
126365    fprintf(yyTraceFILE,"%sShift %d\n",yyTracePrompt,yyNewState);
126366    fprintf(yyTraceFILE,"%sStack:",yyTracePrompt);
126367    for(i=1; i<=yypParser->yyidx; i++)
126368      fprintf(yyTraceFILE," %s",yyTokenName[yypParser->yystack[i].major]);
126369    fprintf(yyTraceFILE,"\n");
126370  }
126371#endif
126372}
126373
126374/* The following table contains information about every rule that
126375** is used during the reduce.
126376*/
126377static const struct {
126378  YYCODETYPE lhs;         /* Symbol on the left-hand side of the rule */
126379  unsigned char nrhs;     /* Number of right-hand side symbols in the rule */
126380} yyRuleInfo[] = {
126381  { 144, 1 },
126382  { 145, 2 },
126383  { 145, 1 },
126384  { 146, 1 },
126385  { 146, 3 },
126386  { 147, 0 },
126387  { 147, 1 },
126388  { 147, 3 },
126389  { 148, 1 },
126390  { 149, 3 },
126391  { 151, 0 },
126392  { 151, 1 },
126393  { 151, 2 },
126394  { 150, 0 },
126395  { 150, 1 },
126396  { 150, 1 },
126397  { 150, 1 },
126398  { 149, 2 },
126399  { 149, 2 },
126400  { 149, 2 },
126401  { 153, 1 },
126402  { 153, 0 },
126403  { 149, 2 },
126404  { 149, 3 },
126405  { 149, 5 },
126406  { 149, 2 },
126407  { 154, 6 },
126408  { 156, 1 },
126409  { 158, 0 },
126410  { 158, 3 },
126411  { 157, 1 },
126412  { 157, 0 },
126413  { 155, 5 },
126414  { 155, 2 },
126415  { 162, 0 },
126416  { 162, 2 },
126417  { 160, 3 },
126418  { 160, 1 },
126419  { 164, 3 },
126420  { 165, 1 },
126421  { 152, 1 },
126422  { 152, 1 },
126423  { 152, 1 },
126424  { 166, 0 },
126425  { 166, 1 },
126426  { 168, 1 },
126427  { 168, 4 },
126428  { 168, 6 },
126429  { 169, 1 },
126430  { 169, 2 },
126431  { 170, 1 },
126432  { 170, 1 },
126433  { 167, 2 },
126434  { 167, 0 },
126435  { 173, 2 },
126436  { 173, 2 },
126437  { 173, 4 },
126438  { 173, 3 },
126439  { 173, 3 },
126440  { 173, 2 },
126441  { 173, 2 },
126442  { 173, 3 },
126443  { 173, 5 },
126444  { 173, 2 },
126445  { 173, 4 },
126446  { 173, 4 },
126447  { 173, 1 },
126448  { 173, 2 },
126449  { 178, 0 },
126450  { 178, 1 },
126451  { 180, 0 },
126452  { 180, 2 },
126453  { 182, 2 },
126454  { 182, 3 },
126455  { 182, 3 },
126456  { 182, 3 },
126457  { 183, 2 },
126458  { 183, 2 },
126459  { 183, 1 },
126460  { 183, 1 },
126461  { 183, 2 },
126462  { 181, 3 },
126463  { 181, 2 },
126464  { 184, 0 },
126465  { 184, 2 },
126466  { 184, 2 },
126467  { 161, 0 },
126468  { 161, 2 },
126469  { 185, 3 },
126470  { 185, 1 },
126471  { 186, 1 },
126472  { 186, 0 },
126473  { 187, 2 },
126474  { 187, 7 },
126475  { 187, 5 },
126476  { 187, 5 },
126477  { 187, 10 },
126478  { 189, 0 },
126479  { 189, 1 },
126480  { 176, 0 },
126481  { 176, 3 },
126482  { 190, 0 },
126483  { 190, 2 },
126484  { 191, 1 },
126485  { 191, 1 },
126486  { 191, 1 },
126487  { 149, 4 },
126488  { 193, 2 },
126489  { 193, 0 },
126490  { 149, 8 },
126491  { 149, 4 },
126492  { 149, 1 },
126493  { 163, 2 },
126494  { 195, 1 },
126495  { 195, 3 },
126496  { 198, 1 },
126497  { 198, 2 },
126498  { 198, 1 },
126499  { 196, 9 },
126500  { 196, 1 },
126501  { 207, 4 },
126502  { 207, 5 },
126503  { 199, 1 },
126504  { 199, 1 },
126505  { 199, 0 },
126506  { 210, 2 },
126507  { 210, 0 },
126508  { 200, 3 },
126509  { 200, 2 },
126510  { 200, 4 },
126511  { 211, 2 },
126512  { 211, 1 },
126513  { 211, 0 },
126514  { 201, 0 },
126515  { 201, 2 },
126516  { 213, 2 },
126517  { 213, 0 },
126518  { 212, 7 },
126519  { 212, 7 },
126520  { 212, 7 },
126521  { 159, 0 },
126522  { 159, 2 },
126523  { 194, 2 },
126524  { 214, 1 },
126525  { 214, 2 },
126526  { 214, 3 },
126527  { 214, 4 },
126528  { 216, 2 },
126529  { 216, 0 },
126530  { 215, 0 },
126531  { 215, 3 },
126532  { 215, 2 },
126533  { 217, 4 },
126534  { 217, 0 },
126535  { 205, 0 },
126536  { 205, 3 },
126537  { 220, 4 },
126538  { 220, 2 },
126539  { 177, 1 },
126540  { 177, 1 },
126541  { 177, 0 },
126542  { 203, 0 },
126543  { 203, 3 },
126544  { 204, 0 },
126545  { 204, 2 },
126546  { 206, 0 },
126547  { 206, 2 },
126548  { 206, 4 },
126549  { 206, 4 },
126550  { 149, 6 },
126551  { 202, 0 },
126552  { 202, 2 },
126553  { 149, 8 },
126554  { 221, 5 },
126555  { 221, 3 },
126556  { 149, 6 },
126557  { 149, 7 },
126558  { 222, 2 },
126559  { 222, 1 },
126560  { 223, 0 },
126561  { 223, 3 },
126562  { 219, 3 },
126563  { 219, 1 },
126564  { 175, 1 },
126565  { 175, 3 },
126566  { 174, 1 },
126567  { 175, 1 },
126568  { 175, 1 },
126569  { 175, 3 },
126570  { 175, 5 },
126571  { 174, 1 },
126572  { 174, 1 },
126573  { 175, 1 },
126574  { 175, 3 },
126575  { 175, 6 },
126576  { 175, 5 },
126577  { 175, 4 },
126578  { 174, 1 },
126579  { 175, 3 },
126580  { 175, 3 },
126581  { 175, 3 },
126582  { 175, 3 },
126583  { 175, 3 },
126584  { 175, 3 },
126585  { 175, 3 },
126586  { 175, 3 },
126587  { 224, 1 },
126588  { 224, 2 },
126589  { 175, 3 },
126590  { 175, 5 },
126591  { 175, 2 },
126592  { 175, 3 },
126593  { 175, 3 },
126594  { 175, 4 },
126595  { 175, 2 },
126596  { 175, 2 },
126597  { 175, 2 },
126598  { 175, 2 },
126599  { 225, 1 },
126600  { 225, 2 },
126601  { 175, 5 },
126602  { 226, 1 },
126603  { 226, 2 },
126604  { 175, 5 },
126605  { 175, 3 },
126606  { 175, 5 },
126607  { 175, 4 },
126608  { 175, 4 },
126609  { 175, 5 },
126610  { 228, 5 },
126611  { 228, 4 },
126612  { 229, 2 },
126613  { 229, 0 },
126614  { 227, 1 },
126615  { 227, 0 },
126616  { 209, 1 },
126617  { 209, 0 },
126618  { 208, 3 },
126619  { 208, 1 },
126620  { 149, 12 },
126621  { 230, 1 },
126622  { 230, 0 },
126623  { 179, 0 },
126624  { 179, 3 },
126625  { 188, 5 },
126626  { 188, 3 },
126627  { 231, 0 },
126628  { 231, 2 },
126629  { 149, 4 },
126630  { 149, 1 },
126631  { 149, 2 },
126632  { 149, 3 },
126633  { 149, 5 },
126634  { 149, 6 },
126635  { 149, 5 },
126636  { 149, 6 },
126637  { 232, 1 },
126638  { 232, 1 },
126639  { 232, 1 },
126640  { 232, 1 },
126641  { 232, 1 },
126642  { 171, 2 },
126643  { 171, 1 },
126644  { 172, 2 },
126645  { 149, 5 },
126646  { 233, 11 },
126647  { 235, 1 },
126648  { 235, 1 },
126649  { 235, 2 },
126650  { 235, 0 },
126651  { 236, 1 },
126652  { 236, 1 },
126653  { 236, 3 },
126654  { 237, 0 },
126655  { 237, 3 },
126656  { 238, 0 },
126657  { 238, 2 },
126658  { 234, 3 },
126659  { 234, 2 },
126660  { 240, 1 },
126661  { 240, 3 },
126662  { 241, 0 },
126663  { 241, 3 },
126664  { 241, 2 },
126665  { 239, 7 },
126666  { 239, 5 },
126667  { 239, 5 },
126668  { 239, 1 },
126669  { 175, 4 },
126670  { 175, 6 },
126671  { 192, 1 },
126672  { 192, 1 },
126673  { 192, 1 },
126674  { 149, 4 },
126675  { 149, 6 },
126676  { 149, 3 },
126677  { 243, 0 },
126678  { 243, 2 },
126679  { 242, 1 },
126680  { 242, 0 },
126681  { 149, 1 },
126682  { 149, 3 },
126683  { 149, 1 },
126684  { 149, 3 },
126685  { 149, 6 },
126686  { 149, 6 },
126687  { 244, 1 },
126688  { 245, 0 },
126689  { 245, 1 },
126690  { 149, 1 },
126691  { 149, 4 },
126692  { 246, 8 },
126693  { 247, 1 },
126694  { 247, 3 },
126695  { 248, 0 },
126696  { 248, 2 },
126697  { 249, 1 },
126698  { 249, 3 },
126699  { 250, 1 },
126700  { 251, 0 },
126701  { 251, 4 },
126702  { 251, 2 },
126703  { 197, 0 },
126704  { 197, 2 },
126705  { 197, 3 },
126706  { 252, 6 },
126707  { 252, 8 },
126708};
126709
126710static void yy_accept(yyParser*);  /* Forward Declaration */
126711
126712/*
126713** Perform a reduce action and the shift that must immediately
126714** follow the reduce.
126715*/
126716static void yy_reduce(
126717  yyParser *yypParser,         /* The parser */
126718  int yyruleno                 /* Number of the rule by which to reduce */
126719){
126720  int yygoto;                     /* The next state */
126721  int yyact;                      /* The next action */
126722  YYMINORTYPE yygotominor;        /* The LHS of the rule reduced */
126723  yyStackEntry *yymsp;            /* The top of the parser's stack */
126724  int yysize;                     /* Amount to pop the stack */
126725  sqlite3ParserARG_FETCH;
126726  yymsp = &yypParser->yystack[yypParser->yyidx];
126727#ifndef NDEBUG
126728  if( yyTraceFILE && yyruleno>=0
126729        && yyruleno<(int)(sizeof(yyRuleName)/sizeof(yyRuleName[0])) ){
126730    fprintf(yyTraceFILE, "%sReduce [%s].\n", yyTracePrompt,
126731      yyRuleName[yyruleno]);
126732  }
126733#endif /* NDEBUG */
126734
126735  /* Silence complaints from purify about yygotominor being uninitialized
126736  ** in some cases when it is copied into the stack after the following
126737  ** switch.  yygotominor is uninitialized when a rule reduces that does
126738  ** not set the value of its left-hand side nonterminal.  Leaving the
126739  ** value of the nonterminal uninitialized is utterly harmless as long
126740  ** as the value is never used.  So really the only thing this code
126741  ** accomplishes is to quieten purify.
126742  **
126743  ** 2007-01-16:  The wireshark project (www.wireshark.org) reports that
126744  ** without this code, their parser segfaults.  I'm not sure what there
126745  ** parser is doing to make this happen.  This is the second bug report
126746  ** from wireshark this week.  Clearly they are stressing Lemon in ways
126747  ** that it has not been previously stressed...  (SQLite ticket #2172)
126748  */
126749  /*memset(&yygotominor, 0, sizeof(yygotominor));*/
126750  yygotominor = yyzerominor;
126751
126752
126753  switch( yyruleno ){
126754  /* Beginning here are the reduction cases.  A typical example
126755  ** follows:
126756  **   case 0:
126757  **  #line <lineno> <grammarfile>
126758  **     { ... }           // User supplied code
126759  **  #line <lineno> <thisfile>
126760  **     break;
126761  */
126762      case 5: /* explain ::= */
126763{ sqlite3BeginParse(pParse, 0); }
126764        break;
126765      case 6: /* explain ::= EXPLAIN */
126766{ sqlite3BeginParse(pParse, 1); }
126767        break;
126768      case 7: /* explain ::= EXPLAIN QUERY PLAN */
126769{ sqlite3BeginParse(pParse, 2); }
126770        break;
126771      case 8: /* cmdx ::= cmd */
126772{ sqlite3FinishCoding(pParse); }
126773        break;
126774      case 9: /* cmd ::= BEGIN transtype trans_opt */
126775{sqlite3BeginTransaction(pParse, yymsp[-1].minor.yy328);}
126776        break;
126777      case 13: /* transtype ::= */
126778{yygotominor.yy328 = TK_DEFERRED;}
126779        break;
126780      case 14: /* transtype ::= DEFERRED */
126781      case 15: /* transtype ::= IMMEDIATE */ yytestcase(yyruleno==15);
126782      case 16: /* transtype ::= EXCLUSIVE */ yytestcase(yyruleno==16);
126783      case 115: /* multiselect_op ::= UNION */ yytestcase(yyruleno==115);
126784      case 117: /* multiselect_op ::= EXCEPT|INTERSECT */ yytestcase(yyruleno==117);
126785{yygotominor.yy328 = yymsp[0].major;}
126786        break;
126787      case 17: /* cmd ::= COMMIT trans_opt */
126788      case 18: /* cmd ::= END trans_opt */ yytestcase(yyruleno==18);
126789{sqlite3CommitTransaction(pParse);}
126790        break;
126791      case 19: /* cmd ::= ROLLBACK trans_opt */
126792{sqlite3RollbackTransaction(pParse);}
126793        break;
126794      case 22: /* cmd ::= SAVEPOINT nm */
126795{
126796  sqlite3Savepoint(pParse, SAVEPOINT_BEGIN, &yymsp[0].minor.yy0);
126797}
126798        break;
126799      case 23: /* cmd ::= RELEASE savepoint_opt nm */
126800{
126801  sqlite3Savepoint(pParse, SAVEPOINT_RELEASE, &yymsp[0].minor.yy0);
126802}
126803        break;
126804      case 24: /* cmd ::= ROLLBACK trans_opt TO savepoint_opt nm */
126805{
126806  sqlite3Savepoint(pParse, SAVEPOINT_ROLLBACK, &yymsp[0].minor.yy0);
126807}
126808        break;
126809      case 26: /* create_table ::= createkw temp TABLE ifnotexists nm dbnm */
126810{
126811   sqlite3StartTable(pParse,&yymsp[-1].minor.yy0,&yymsp[0].minor.yy0,yymsp[-4].minor.yy328,0,0,yymsp[-2].minor.yy328);
126812}
126813        break;
126814      case 27: /* createkw ::= CREATE */
126815{
126816  pParse->db->lookaside.bEnabled = 0;
126817  yygotominor.yy0 = yymsp[0].minor.yy0;
126818}
126819        break;
126820      case 28: /* ifnotexists ::= */
126821      case 31: /* temp ::= */ yytestcase(yyruleno==31);
126822      case 68: /* autoinc ::= */ yytestcase(yyruleno==68);
126823      case 81: /* defer_subclause ::= NOT DEFERRABLE init_deferred_pred_opt */ yytestcase(yyruleno==81);
126824      case 83: /* init_deferred_pred_opt ::= */ yytestcase(yyruleno==83);
126825      case 85: /* init_deferred_pred_opt ::= INITIALLY IMMEDIATE */ yytestcase(yyruleno==85);
126826      case 97: /* defer_subclause_opt ::= */ yytestcase(yyruleno==97);
126827      case 108: /* ifexists ::= */ yytestcase(yyruleno==108);
126828      case 218: /* between_op ::= BETWEEN */ yytestcase(yyruleno==218);
126829      case 221: /* in_op ::= IN */ yytestcase(yyruleno==221);
126830{yygotominor.yy328 = 0;}
126831        break;
126832      case 29: /* ifnotexists ::= IF NOT EXISTS */
126833      case 30: /* temp ::= TEMP */ yytestcase(yyruleno==30);
126834      case 69: /* autoinc ::= AUTOINCR */ yytestcase(yyruleno==69);
126835      case 84: /* init_deferred_pred_opt ::= INITIALLY DEFERRED */ yytestcase(yyruleno==84);
126836      case 107: /* ifexists ::= IF EXISTS */ yytestcase(yyruleno==107);
126837      case 219: /* between_op ::= NOT BETWEEN */ yytestcase(yyruleno==219);
126838      case 222: /* in_op ::= NOT IN */ yytestcase(yyruleno==222);
126839{yygotominor.yy328 = 1;}
126840        break;
126841      case 32: /* create_table_args ::= LP columnlist conslist_opt RP table_options */
126842{
126843  sqlite3EndTable(pParse,&yymsp[-2].minor.yy0,&yymsp[-1].minor.yy0,yymsp[0].minor.yy186,0);
126844}
126845        break;
126846      case 33: /* create_table_args ::= AS select */
126847{
126848  sqlite3EndTable(pParse,0,0,0,yymsp[0].minor.yy3);
126849  sqlite3SelectDelete(pParse->db, yymsp[0].minor.yy3);
126850}
126851        break;
126852      case 34: /* table_options ::= */
126853{yygotominor.yy186 = 0;}
126854        break;
126855      case 35: /* table_options ::= WITHOUT nm */
126856{
126857  if( yymsp[0].minor.yy0.n==5 && sqlite3_strnicmp(yymsp[0].minor.yy0.z,"rowid",5)==0 ){
126858    yygotominor.yy186 = TF_WithoutRowid | TF_NoVisibleRowid;
126859  }else{
126860    yygotominor.yy186 = 0;
126861    sqlite3ErrorMsg(pParse, "unknown table option: %.*s", yymsp[0].minor.yy0.n, yymsp[0].minor.yy0.z);
126862  }
126863}
126864        break;
126865      case 38: /* column ::= columnid type carglist */
126866{
126867  yygotominor.yy0.z = yymsp[-2].minor.yy0.z;
126868  yygotominor.yy0.n = (int)(pParse->sLastToken.z-yymsp[-2].minor.yy0.z) + pParse->sLastToken.n;
126869}
126870        break;
126871      case 39: /* columnid ::= nm */
126872{
126873  sqlite3AddColumn(pParse,&yymsp[0].minor.yy0);
126874  yygotominor.yy0 = yymsp[0].minor.yy0;
126875  pParse->constraintName.n = 0;
126876}
126877        break;
126878      case 40: /* nm ::= ID|INDEXED */
126879      case 41: /* nm ::= STRING */ yytestcase(yyruleno==41);
126880      case 42: /* nm ::= JOIN_KW */ yytestcase(yyruleno==42);
126881      case 45: /* typetoken ::= typename */ yytestcase(yyruleno==45);
126882      case 48: /* typename ::= ID|STRING */ yytestcase(yyruleno==48);
126883      case 130: /* as ::= AS nm */ yytestcase(yyruleno==130);
126884      case 131: /* as ::= ID|STRING */ yytestcase(yyruleno==131);
126885      case 141: /* dbnm ::= DOT nm */ yytestcase(yyruleno==141);
126886      case 150: /* indexed_opt ::= INDEXED BY nm */ yytestcase(yyruleno==150);
126887      case 247: /* collate ::= COLLATE ID|STRING */ yytestcase(yyruleno==247);
126888      case 256: /* nmnum ::= plus_num */ yytestcase(yyruleno==256);
126889      case 257: /* nmnum ::= nm */ yytestcase(yyruleno==257);
126890      case 258: /* nmnum ::= ON */ yytestcase(yyruleno==258);
126891      case 259: /* nmnum ::= DELETE */ yytestcase(yyruleno==259);
126892      case 260: /* nmnum ::= DEFAULT */ yytestcase(yyruleno==260);
126893      case 261: /* plus_num ::= PLUS INTEGER|FLOAT */ yytestcase(yyruleno==261);
126894      case 262: /* plus_num ::= INTEGER|FLOAT */ yytestcase(yyruleno==262);
126895      case 263: /* minus_num ::= MINUS INTEGER|FLOAT */ yytestcase(yyruleno==263);
126896      case 279: /* trnm ::= nm */ yytestcase(yyruleno==279);
126897{yygotominor.yy0 = yymsp[0].minor.yy0;}
126898        break;
126899      case 44: /* type ::= typetoken */
126900{sqlite3AddColumnType(pParse,&yymsp[0].minor.yy0);}
126901        break;
126902      case 46: /* typetoken ::= typename LP signed RP */
126903{
126904  yygotominor.yy0.z = yymsp[-3].minor.yy0.z;
126905  yygotominor.yy0.n = (int)(&yymsp[0].minor.yy0.z[yymsp[0].minor.yy0.n] - yymsp[-3].minor.yy0.z);
126906}
126907        break;
126908      case 47: /* typetoken ::= typename LP signed COMMA signed RP */
126909{
126910  yygotominor.yy0.z = yymsp[-5].minor.yy0.z;
126911  yygotominor.yy0.n = (int)(&yymsp[0].minor.yy0.z[yymsp[0].minor.yy0.n] - yymsp[-5].minor.yy0.z);
126912}
126913        break;
126914      case 49: /* typename ::= typename ID|STRING */
126915{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);}
126916        break;
126917      case 54: /* ccons ::= CONSTRAINT nm */
126918      case 92: /* tcons ::= CONSTRAINT nm */ yytestcase(yyruleno==92);
126919{pParse->constraintName = yymsp[0].minor.yy0;}
126920        break;
126921      case 55: /* ccons ::= DEFAULT term */
126922      case 57: /* ccons ::= DEFAULT PLUS term */ yytestcase(yyruleno==57);
126923{sqlite3AddDefaultValue(pParse,&yymsp[0].minor.yy346);}
126924        break;
126925      case 56: /* ccons ::= DEFAULT LP expr RP */
126926{sqlite3AddDefaultValue(pParse,&yymsp[-1].minor.yy346);}
126927        break;
126928      case 58: /* ccons ::= DEFAULT MINUS term */
126929{
126930  ExprSpan v;
126931  v.pExpr = sqlite3PExpr(pParse, TK_UMINUS, yymsp[0].minor.yy346.pExpr, 0, 0);
126932  v.zStart = yymsp[-1].minor.yy0.z;
126933  v.zEnd = yymsp[0].minor.yy346.zEnd;
126934  sqlite3AddDefaultValue(pParse,&v);
126935}
126936        break;
126937      case 59: /* ccons ::= DEFAULT ID|INDEXED */
126938{
126939  ExprSpan v;
126940  spanExpr(&v, pParse, TK_STRING, &yymsp[0].minor.yy0);
126941  sqlite3AddDefaultValue(pParse,&v);
126942}
126943        break;
126944      case 61: /* ccons ::= NOT NULL onconf */
126945{sqlite3AddNotNull(pParse, yymsp[0].minor.yy328);}
126946        break;
126947      case 62: /* ccons ::= PRIMARY KEY sortorder onconf autoinc */
126948{sqlite3AddPrimaryKey(pParse,0,yymsp[-1].minor.yy328,yymsp[0].minor.yy328,yymsp[-2].minor.yy328);}
126949        break;
126950      case 63: /* ccons ::= UNIQUE onconf */
126951{sqlite3CreateIndex(pParse,0,0,0,0,yymsp[0].minor.yy328,0,0,0,0);}
126952        break;
126953      case 64: /* ccons ::= CHECK LP expr RP */
126954{sqlite3AddCheckConstraint(pParse,yymsp[-1].minor.yy346.pExpr);}
126955        break;
126956      case 65: /* ccons ::= REFERENCES nm idxlist_opt refargs */
126957{sqlite3CreateForeignKey(pParse,0,&yymsp[-2].minor.yy0,yymsp[-1].minor.yy14,yymsp[0].minor.yy328);}
126958        break;
126959      case 66: /* ccons ::= defer_subclause */
126960{sqlite3DeferForeignKey(pParse,yymsp[0].minor.yy328);}
126961        break;
126962      case 67: /* ccons ::= COLLATE ID|STRING */
126963{sqlite3AddCollateType(pParse, &yymsp[0].minor.yy0);}
126964        break;
126965      case 70: /* refargs ::= */
126966{ yygotominor.yy328 = OE_None*0x0101; /* EV: R-19803-45884 */}
126967        break;
126968      case 71: /* refargs ::= refargs refarg */
126969{ yygotominor.yy328 = (yymsp[-1].minor.yy328 & ~yymsp[0].minor.yy429.mask) | yymsp[0].minor.yy429.value; }
126970        break;
126971      case 72: /* refarg ::= MATCH nm */
126972      case 73: /* refarg ::= ON INSERT refact */ yytestcase(yyruleno==73);
126973{ yygotominor.yy429.value = 0;     yygotominor.yy429.mask = 0x000000; }
126974        break;
126975      case 74: /* refarg ::= ON DELETE refact */
126976{ yygotominor.yy429.value = yymsp[0].minor.yy328;     yygotominor.yy429.mask = 0x0000ff; }
126977        break;
126978      case 75: /* refarg ::= ON UPDATE refact */
126979{ yygotominor.yy429.value = yymsp[0].minor.yy328<<8;  yygotominor.yy429.mask = 0x00ff00; }
126980        break;
126981      case 76: /* refact ::= SET NULL */
126982{ yygotominor.yy328 = OE_SetNull;  /* EV: R-33326-45252 */}
126983        break;
126984      case 77: /* refact ::= SET DEFAULT */
126985{ yygotominor.yy328 = OE_SetDflt;  /* EV: R-33326-45252 */}
126986        break;
126987      case 78: /* refact ::= CASCADE */
126988{ yygotominor.yy328 = OE_Cascade;  /* EV: R-33326-45252 */}
126989        break;
126990      case 79: /* refact ::= RESTRICT */
126991{ yygotominor.yy328 = OE_Restrict; /* EV: R-33326-45252 */}
126992        break;
126993      case 80: /* refact ::= NO ACTION */
126994{ yygotominor.yy328 = OE_None;     /* EV: R-33326-45252 */}
126995        break;
126996      case 82: /* defer_subclause ::= DEFERRABLE init_deferred_pred_opt */
126997      case 98: /* defer_subclause_opt ::= defer_subclause */ yytestcase(yyruleno==98);
126998      case 100: /* onconf ::= ON CONFLICT resolvetype */ yytestcase(yyruleno==100);
126999      case 103: /* resolvetype ::= raisetype */ yytestcase(yyruleno==103);
127000{yygotominor.yy328 = yymsp[0].minor.yy328;}
127001        break;
127002      case 86: /* conslist_opt ::= */
127003{yygotominor.yy0.n = 0; yygotominor.yy0.z = 0;}
127004        break;
127005      case 87: /* conslist_opt ::= COMMA conslist */
127006{yygotominor.yy0 = yymsp[-1].minor.yy0;}
127007        break;
127008      case 90: /* tconscomma ::= COMMA */
127009{pParse->constraintName.n = 0;}
127010        break;
127011      case 93: /* tcons ::= PRIMARY KEY LP idxlist autoinc RP onconf */
127012{sqlite3AddPrimaryKey(pParse,yymsp[-3].minor.yy14,yymsp[0].minor.yy328,yymsp[-2].minor.yy328,0);}
127013        break;
127014      case 94: /* tcons ::= UNIQUE LP idxlist RP onconf */
127015{sqlite3CreateIndex(pParse,0,0,0,yymsp[-2].minor.yy14,yymsp[0].minor.yy328,0,0,0,0);}
127016        break;
127017      case 95: /* tcons ::= CHECK LP expr RP onconf */
127018{sqlite3AddCheckConstraint(pParse,yymsp[-2].minor.yy346.pExpr);}
127019        break;
127020      case 96: /* tcons ::= FOREIGN KEY LP idxlist RP REFERENCES nm idxlist_opt refargs defer_subclause_opt */
127021{
127022    sqlite3CreateForeignKey(pParse, yymsp[-6].minor.yy14, &yymsp[-3].minor.yy0, yymsp[-2].minor.yy14, yymsp[-1].minor.yy328);
127023    sqlite3DeferForeignKey(pParse, yymsp[0].minor.yy328);
127024}
127025        break;
127026      case 99: /* onconf ::= */
127027{yygotominor.yy328 = OE_Default;}
127028        break;
127029      case 101: /* orconf ::= */
127030{yygotominor.yy186 = OE_Default;}
127031        break;
127032      case 102: /* orconf ::= OR resolvetype */
127033{yygotominor.yy186 = (u8)yymsp[0].minor.yy328;}
127034        break;
127035      case 104: /* resolvetype ::= IGNORE */
127036{yygotominor.yy328 = OE_Ignore;}
127037        break;
127038      case 105: /* resolvetype ::= REPLACE */
127039{yygotominor.yy328 = OE_Replace;}
127040        break;
127041      case 106: /* cmd ::= DROP TABLE ifexists fullname */
127042{
127043  sqlite3DropTable(pParse, yymsp[0].minor.yy65, 0, yymsp[-1].minor.yy328);
127044}
127045        break;
127046      case 109: /* cmd ::= createkw temp VIEW ifnotexists nm dbnm AS select */
127047{
127048  sqlite3CreateView(pParse, &yymsp[-7].minor.yy0, &yymsp[-3].minor.yy0, &yymsp[-2].minor.yy0, yymsp[0].minor.yy3, yymsp[-6].minor.yy328, yymsp[-4].minor.yy328);
127049}
127050        break;
127051      case 110: /* cmd ::= DROP VIEW ifexists fullname */
127052{
127053  sqlite3DropTable(pParse, yymsp[0].minor.yy65, 1, yymsp[-1].minor.yy328);
127054}
127055        break;
127056      case 111: /* cmd ::= select */
127057{
127058  SelectDest dest = {SRT_Output, 0, 0, 0, 0, 0};
127059  sqlite3Select(pParse, yymsp[0].minor.yy3, &dest);
127060  sqlite3SelectDelete(pParse->db, yymsp[0].minor.yy3);
127061}
127062        break;
127063      case 112: /* select ::= with selectnowith */
127064{
127065  Select *p = yymsp[0].minor.yy3;
127066  if( p ){
127067    p->pWith = yymsp[-1].minor.yy59;
127068    parserDoubleLinkSelect(pParse, p);
127069  }else{
127070    sqlite3WithDelete(pParse->db, yymsp[-1].minor.yy59);
127071  }
127072  yygotominor.yy3 = p;
127073}
127074        break;
127075      case 113: /* selectnowith ::= oneselect */
127076      case 119: /* oneselect ::= values */ yytestcase(yyruleno==119);
127077{yygotominor.yy3 = yymsp[0].minor.yy3;}
127078        break;
127079      case 114: /* selectnowith ::= selectnowith multiselect_op oneselect */
127080{
127081  Select *pRhs = yymsp[0].minor.yy3;
127082  Select *pLhs = yymsp[-2].minor.yy3;
127083  if( pRhs && pRhs->pPrior ){
127084    SrcList *pFrom;
127085    Token x;
127086    x.n = 0;
127087    parserDoubleLinkSelect(pParse, pRhs);
127088    pFrom = sqlite3SrcListAppendFromTerm(pParse,0,0,0,&x,pRhs,0,0);
127089    pRhs = sqlite3SelectNew(pParse,0,pFrom,0,0,0,0,0,0,0);
127090  }
127091  if( pRhs ){
127092    pRhs->op = (u8)yymsp[-1].minor.yy328;
127093    pRhs->pPrior = pLhs;
127094    if( ALWAYS(pLhs) ) pLhs->selFlags &= ~SF_MultiValue;
127095    pRhs->selFlags &= ~SF_MultiValue;
127096    if( yymsp[-1].minor.yy328!=TK_ALL ) pParse->hasCompound = 1;
127097  }else{
127098    sqlite3SelectDelete(pParse->db, pLhs);
127099  }
127100  yygotominor.yy3 = pRhs;
127101}
127102        break;
127103      case 116: /* multiselect_op ::= UNION ALL */
127104{yygotominor.yy328 = TK_ALL;}
127105        break;
127106      case 118: /* oneselect ::= SELECT distinct selcollist from where_opt groupby_opt having_opt orderby_opt limit_opt */
127107{
127108  yygotominor.yy3 = sqlite3SelectNew(pParse,yymsp[-6].minor.yy14,yymsp[-5].minor.yy65,yymsp[-4].minor.yy132,yymsp[-3].minor.yy14,yymsp[-2].minor.yy132,yymsp[-1].minor.yy14,yymsp[-7].minor.yy381,yymsp[0].minor.yy476.pLimit,yymsp[0].minor.yy476.pOffset);
127109#if SELECTTRACE_ENABLED
127110  /* Populate the Select.zSelName[] string that is used to help with
127111  ** query planner debugging, to differentiate between multiple Select
127112  ** objects in a complex query.
127113  **
127114  ** If the SELECT keyword is immediately followed by a C-style comment
127115  ** then extract the first few alphanumeric characters from within that
127116  ** comment to be the zSelName value.  Otherwise, the label is #N where
127117  ** is an integer that is incremented with each SELECT statement seen.
127118  */
127119  if( yygotominor.yy3!=0 ){
127120    const char *z = yymsp[-8].minor.yy0.z+6;
127121    int i;
127122    sqlite3_snprintf(sizeof(yygotominor.yy3->zSelName), yygotominor.yy3->zSelName, "#%d",
127123                     ++pParse->nSelect);
127124    while( z[0]==' ' ) z++;
127125    if( z[0]=='/' && z[1]=='*' ){
127126      z += 2;
127127      while( z[0]==' ' ) z++;
127128      for(i=0; sqlite3Isalnum(z[i]); i++){}
127129      sqlite3_snprintf(sizeof(yygotominor.yy3->zSelName), yygotominor.yy3->zSelName, "%.*s", i, z);
127130    }
127131  }
127132#endif /* SELECTRACE_ENABLED */
127133}
127134        break;
127135      case 120: /* values ::= VALUES LP nexprlist RP */
127136{
127137  yygotominor.yy3 = sqlite3SelectNew(pParse,yymsp[-1].minor.yy14,0,0,0,0,0,SF_Values,0,0);
127138}
127139        break;
127140      case 121: /* values ::= values COMMA LP exprlist RP */
127141{
127142  Select *pRight, *pLeft = yymsp[-4].minor.yy3;
127143  pRight = sqlite3SelectNew(pParse,yymsp[-1].minor.yy14,0,0,0,0,0,SF_Values|SF_MultiValue,0,0);
127144  if( ALWAYS(pLeft) ) pLeft->selFlags &= ~SF_MultiValue;
127145  if( pRight ){
127146    pRight->op = TK_ALL;
127147    pLeft = yymsp[-4].minor.yy3;
127148    pRight->pPrior = pLeft;
127149    yygotominor.yy3 = pRight;
127150  }else{
127151    yygotominor.yy3 = pLeft;
127152  }
127153}
127154        break;
127155      case 122: /* distinct ::= DISTINCT */
127156{yygotominor.yy381 = SF_Distinct;}
127157        break;
127158      case 123: /* distinct ::= ALL */
127159{yygotominor.yy381 = SF_All;}
127160        break;
127161      case 124: /* distinct ::= */
127162{yygotominor.yy381 = 0;}
127163        break;
127164      case 125: /* sclp ::= selcollist COMMA */
127165      case 243: /* idxlist_opt ::= LP idxlist RP */ yytestcase(yyruleno==243);
127166{yygotominor.yy14 = yymsp[-1].minor.yy14;}
127167        break;
127168      case 126: /* sclp ::= */
127169      case 154: /* orderby_opt ::= */ yytestcase(yyruleno==154);
127170      case 161: /* groupby_opt ::= */ yytestcase(yyruleno==161);
127171      case 236: /* exprlist ::= */ yytestcase(yyruleno==236);
127172      case 242: /* idxlist_opt ::= */ yytestcase(yyruleno==242);
127173{yygotominor.yy14 = 0;}
127174        break;
127175      case 127: /* selcollist ::= sclp expr as */
127176{
127177   yygotominor.yy14 = sqlite3ExprListAppend(pParse, yymsp[-2].minor.yy14, yymsp[-1].minor.yy346.pExpr);
127178   if( yymsp[0].minor.yy0.n>0 ) sqlite3ExprListSetName(pParse, yygotominor.yy14, &yymsp[0].minor.yy0, 1);
127179   sqlite3ExprListSetSpan(pParse,yygotominor.yy14,&yymsp[-1].minor.yy346);
127180}
127181        break;
127182      case 128: /* selcollist ::= sclp STAR */
127183{
127184  Expr *p = sqlite3Expr(pParse->db, TK_ALL, 0);
127185  yygotominor.yy14 = sqlite3ExprListAppend(pParse, yymsp[-1].minor.yy14, p);
127186}
127187        break;
127188      case 129: /* selcollist ::= sclp nm DOT STAR */
127189{
127190  Expr *pRight = sqlite3PExpr(pParse, TK_ALL, 0, 0, &yymsp[0].minor.yy0);
127191  Expr *pLeft = sqlite3PExpr(pParse, TK_ID, 0, 0, &yymsp[-2].minor.yy0);
127192  Expr *pDot = sqlite3PExpr(pParse, TK_DOT, pLeft, pRight, 0);
127193  yygotominor.yy14 = sqlite3ExprListAppend(pParse,yymsp[-3].minor.yy14, pDot);
127194}
127195        break;
127196      case 132: /* as ::= */
127197{yygotominor.yy0.n = 0;}
127198        break;
127199      case 133: /* from ::= */
127200{yygotominor.yy65 = sqlite3DbMallocZero(pParse->db, sizeof(*yygotominor.yy65));}
127201        break;
127202      case 134: /* from ::= FROM seltablist */
127203{
127204  yygotominor.yy65 = yymsp[0].minor.yy65;
127205  sqlite3SrcListShiftJoinType(yygotominor.yy65);
127206}
127207        break;
127208      case 135: /* stl_prefix ::= seltablist joinop */
127209{
127210   yygotominor.yy65 = yymsp[-1].minor.yy65;
127211   if( ALWAYS(yygotominor.yy65 && yygotominor.yy65->nSrc>0) ) yygotominor.yy65->a[yygotominor.yy65->nSrc-1].jointype = (u8)yymsp[0].minor.yy328;
127212}
127213        break;
127214      case 136: /* stl_prefix ::= */
127215{yygotominor.yy65 = 0;}
127216        break;
127217      case 137: /* seltablist ::= stl_prefix nm dbnm as indexed_opt on_opt using_opt */
127218{
127219  yygotominor.yy65 = sqlite3SrcListAppendFromTerm(pParse,yymsp[-6].minor.yy65,&yymsp[-5].minor.yy0,&yymsp[-4].minor.yy0,&yymsp[-3].minor.yy0,0,yymsp[-1].minor.yy132,yymsp[0].minor.yy408);
127220  sqlite3SrcListIndexedBy(pParse, yygotominor.yy65, &yymsp[-2].minor.yy0);
127221}
127222        break;
127223      case 138: /* seltablist ::= stl_prefix LP select RP as on_opt using_opt */
127224{
127225    yygotominor.yy65 = sqlite3SrcListAppendFromTerm(pParse,yymsp[-6].minor.yy65,0,0,&yymsp[-2].minor.yy0,yymsp[-4].minor.yy3,yymsp[-1].minor.yy132,yymsp[0].minor.yy408);
127226  }
127227        break;
127228      case 139: /* seltablist ::= stl_prefix LP seltablist RP as on_opt using_opt */
127229{
127230    if( yymsp[-6].minor.yy65==0 && yymsp[-2].minor.yy0.n==0 && yymsp[-1].minor.yy132==0 && yymsp[0].minor.yy408==0 ){
127231      yygotominor.yy65 = yymsp[-4].minor.yy65;
127232    }else if( yymsp[-4].minor.yy65->nSrc==1 ){
127233      yygotominor.yy65 = sqlite3SrcListAppendFromTerm(pParse,yymsp[-6].minor.yy65,0,0,&yymsp[-2].minor.yy0,0,yymsp[-1].minor.yy132,yymsp[0].minor.yy408);
127234      if( yygotominor.yy65 ){
127235        struct SrcList_item *pNew = &yygotominor.yy65->a[yygotominor.yy65->nSrc-1];
127236        struct SrcList_item *pOld = yymsp[-4].minor.yy65->a;
127237        pNew->zName = pOld->zName;
127238        pNew->zDatabase = pOld->zDatabase;
127239        pNew->pSelect = pOld->pSelect;
127240        pOld->zName = pOld->zDatabase = 0;
127241        pOld->pSelect = 0;
127242      }
127243      sqlite3SrcListDelete(pParse->db, yymsp[-4].minor.yy65);
127244    }else{
127245      Select *pSubquery;
127246      sqlite3SrcListShiftJoinType(yymsp[-4].minor.yy65);
127247      pSubquery = sqlite3SelectNew(pParse,0,yymsp[-4].minor.yy65,0,0,0,0,SF_NestedFrom,0,0);
127248      yygotominor.yy65 = sqlite3SrcListAppendFromTerm(pParse,yymsp[-6].minor.yy65,0,0,&yymsp[-2].minor.yy0,pSubquery,yymsp[-1].minor.yy132,yymsp[0].minor.yy408);
127249    }
127250  }
127251        break;
127252      case 140: /* dbnm ::= */
127253      case 149: /* indexed_opt ::= */ yytestcase(yyruleno==149);
127254{yygotominor.yy0.z=0; yygotominor.yy0.n=0;}
127255        break;
127256      case 142: /* fullname ::= nm dbnm */
127257{yygotominor.yy65 = sqlite3SrcListAppend(pParse->db,0,&yymsp[-1].minor.yy0,&yymsp[0].minor.yy0);}
127258        break;
127259      case 143: /* joinop ::= COMMA|JOIN */
127260{ yygotominor.yy328 = JT_INNER; }
127261        break;
127262      case 144: /* joinop ::= JOIN_KW JOIN */
127263{ yygotominor.yy328 = sqlite3JoinType(pParse,&yymsp[-1].minor.yy0,0,0); }
127264        break;
127265      case 145: /* joinop ::= JOIN_KW nm JOIN */
127266{ yygotominor.yy328 = sqlite3JoinType(pParse,&yymsp[-2].minor.yy0,&yymsp[-1].minor.yy0,0); }
127267        break;
127268      case 146: /* joinop ::= JOIN_KW nm nm JOIN */
127269{ yygotominor.yy328 = sqlite3JoinType(pParse,&yymsp[-3].minor.yy0,&yymsp[-2].minor.yy0,&yymsp[-1].minor.yy0); }
127270        break;
127271      case 147: /* on_opt ::= ON expr */
127272      case 164: /* having_opt ::= HAVING expr */ yytestcase(yyruleno==164);
127273      case 171: /* where_opt ::= WHERE expr */ yytestcase(yyruleno==171);
127274      case 231: /* case_else ::= ELSE expr */ yytestcase(yyruleno==231);
127275      case 233: /* case_operand ::= expr */ yytestcase(yyruleno==233);
127276{yygotominor.yy132 = yymsp[0].minor.yy346.pExpr;}
127277        break;
127278      case 148: /* on_opt ::= */
127279      case 163: /* having_opt ::= */ yytestcase(yyruleno==163);
127280      case 170: /* where_opt ::= */ yytestcase(yyruleno==170);
127281      case 232: /* case_else ::= */ yytestcase(yyruleno==232);
127282      case 234: /* case_operand ::= */ yytestcase(yyruleno==234);
127283{yygotominor.yy132 = 0;}
127284        break;
127285      case 151: /* indexed_opt ::= NOT INDEXED */
127286{yygotominor.yy0.z=0; yygotominor.yy0.n=1;}
127287        break;
127288      case 152: /* using_opt ::= USING LP idlist RP */
127289      case 180: /* inscollist_opt ::= LP idlist RP */ yytestcase(yyruleno==180);
127290{yygotominor.yy408 = yymsp[-1].minor.yy408;}
127291        break;
127292      case 153: /* using_opt ::= */
127293      case 179: /* inscollist_opt ::= */ yytestcase(yyruleno==179);
127294{yygotominor.yy408 = 0;}
127295        break;
127296      case 155: /* orderby_opt ::= ORDER BY sortlist */
127297      case 162: /* groupby_opt ::= GROUP BY nexprlist */ yytestcase(yyruleno==162);
127298      case 235: /* exprlist ::= nexprlist */ yytestcase(yyruleno==235);
127299{yygotominor.yy14 = yymsp[0].minor.yy14;}
127300        break;
127301      case 156: /* sortlist ::= sortlist COMMA expr sortorder */
127302{
127303  yygotominor.yy14 = sqlite3ExprListAppend(pParse,yymsp[-3].minor.yy14,yymsp[-1].minor.yy346.pExpr);
127304  if( yygotominor.yy14 ) yygotominor.yy14->a[yygotominor.yy14->nExpr-1].sortOrder = (u8)yymsp[0].minor.yy328;
127305}
127306        break;
127307      case 157: /* sortlist ::= expr sortorder */
127308{
127309  yygotominor.yy14 = sqlite3ExprListAppend(pParse,0,yymsp[-1].minor.yy346.pExpr);
127310  if( yygotominor.yy14 && ALWAYS(yygotominor.yy14->a) ) yygotominor.yy14->a[0].sortOrder = (u8)yymsp[0].minor.yy328;
127311}
127312        break;
127313      case 158: /* sortorder ::= ASC */
127314      case 160: /* sortorder ::= */ yytestcase(yyruleno==160);
127315{yygotominor.yy328 = SQLITE_SO_ASC;}
127316        break;
127317      case 159: /* sortorder ::= DESC */
127318{yygotominor.yy328 = SQLITE_SO_DESC;}
127319        break;
127320      case 165: /* limit_opt ::= */
127321{yygotominor.yy476.pLimit = 0; yygotominor.yy476.pOffset = 0;}
127322        break;
127323      case 166: /* limit_opt ::= LIMIT expr */
127324{yygotominor.yy476.pLimit = yymsp[0].minor.yy346.pExpr; yygotominor.yy476.pOffset = 0;}
127325        break;
127326      case 167: /* limit_opt ::= LIMIT expr OFFSET expr */
127327{yygotominor.yy476.pLimit = yymsp[-2].minor.yy346.pExpr; yygotominor.yy476.pOffset = yymsp[0].minor.yy346.pExpr;}
127328        break;
127329      case 168: /* limit_opt ::= LIMIT expr COMMA expr */
127330{yygotominor.yy476.pOffset = yymsp[-2].minor.yy346.pExpr; yygotominor.yy476.pLimit = yymsp[0].minor.yy346.pExpr;}
127331        break;
127332      case 169: /* cmd ::= with DELETE FROM fullname indexed_opt where_opt */
127333{
127334  sqlite3WithPush(pParse, yymsp[-5].minor.yy59, 1);
127335  sqlite3SrcListIndexedBy(pParse, yymsp[-2].minor.yy65, &yymsp[-1].minor.yy0);
127336  sqlite3DeleteFrom(pParse,yymsp[-2].minor.yy65,yymsp[0].minor.yy132);
127337}
127338        break;
127339      case 172: /* cmd ::= with UPDATE orconf fullname indexed_opt SET setlist where_opt */
127340{
127341  sqlite3WithPush(pParse, yymsp[-7].minor.yy59, 1);
127342  sqlite3SrcListIndexedBy(pParse, yymsp[-4].minor.yy65, &yymsp[-3].minor.yy0);
127343  sqlite3ExprListCheckLength(pParse,yymsp[-1].minor.yy14,"set list");
127344  sqlite3Update(pParse,yymsp[-4].minor.yy65,yymsp[-1].minor.yy14,yymsp[0].minor.yy132,yymsp[-5].minor.yy186);
127345}
127346        break;
127347      case 173: /* setlist ::= setlist COMMA nm EQ expr */
127348{
127349  yygotominor.yy14 = sqlite3ExprListAppend(pParse, yymsp[-4].minor.yy14, yymsp[0].minor.yy346.pExpr);
127350  sqlite3ExprListSetName(pParse, yygotominor.yy14, &yymsp[-2].minor.yy0, 1);
127351}
127352        break;
127353      case 174: /* setlist ::= nm EQ expr */
127354{
127355  yygotominor.yy14 = sqlite3ExprListAppend(pParse, 0, yymsp[0].minor.yy346.pExpr);
127356  sqlite3ExprListSetName(pParse, yygotominor.yy14, &yymsp[-2].minor.yy0, 1);
127357}
127358        break;
127359      case 175: /* cmd ::= with insert_cmd INTO fullname inscollist_opt select */
127360{
127361  sqlite3WithPush(pParse, yymsp[-5].minor.yy59, 1);
127362  sqlite3Insert(pParse, yymsp[-2].minor.yy65, yymsp[0].minor.yy3, yymsp[-1].minor.yy408, yymsp[-4].minor.yy186);
127363}
127364        break;
127365      case 176: /* cmd ::= with insert_cmd INTO fullname inscollist_opt DEFAULT VALUES */
127366{
127367  sqlite3WithPush(pParse, yymsp[-6].minor.yy59, 1);
127368  sqlite3Insert(pParse, yymsp[-3].minor.yy65, 0, yymsp[-2].minor.yy408, yymsp[-5].minor.yy186);
127369}
127370        break;
127371      case 177: /* insert_cmd ::= INSERT orconf */
127372{yygotominor.yy186 = yymsp[0].minor.yy186;}
127373        break;
127374      case 178: /* insert_cmd ::= REPLACE */
127375{yygotominor.yy186 = OE_Replace;}
127376        break;
127377      case 181: /* idlist ::= idlist COMMA nm */
127378{yygotominor.yy408 = sqlite3IdListAppend(pParse->db,yymsp[-2].minor.yy408,&yymsp[0].minor.yy0);}
127379        break;
127380      case 182: /* idlist ::= nm */
127381{yygotominor.yy408 = sqlite3IdListAppend(pParse->db,0,&yymsp[0].minor.yy0);}
127382        break;
127383      case 183: /* expr ::= term */
127384{yygotominor.yy346 = yymsp[0].minor.yy346;}
127385        break;
127386      case 184: /* expr ::= LP expr RP */
127387{yygotominor.yy346.pExpr = yymsp[-1].minor.yy346.pExpr; spanSet(&yygotominor.yy346,&yymsp[-2].minor.yy0,&yymsp[0].minor.yy0);}
127388        break;
127389      case 185: /* term ::= NULL */
127390      case 190: /* term ::= INTEGER|FLOAT|BLOB */ yytestcase(yyruleno==190);
127391      case 191: /* term ::= STRING */ yytestcase(yyruleno==191);
127392{spanExpr(&yygotominor.yy346, pParse, yymsp[0].major, &yymsp[0].minor.yy0);}
127393        break;
127394      case 186: /* expr ::= ID|INDEXED */
127395      case 187: /* expr ::= JOIN_KW */ yytestcase(yyruleno==187);
127396{spanExpr(&yygotominor.yy346, pParse, TK_ID, &yymsp[0].minor.yy0);}
127397        break;
127398      case 188: /* expr ::= nm DOT nm */
127399{
127400  Expr *temp1 = sqlite3PExpr(pParse, TK_ID, 0, 0, &yymsp[-2].minor.yy0);
127401  Expr *temp2 = sqlite3PExpr(pParse, TK_ID, 0, 0, &yymsp[0].minor.yy0);
127402  yygotominor.yy346.pExpr = sqlite3PExpr(pParse, TK_DOT, temp1, temp2, 0);
127403  spanSet(&yygotominor.yy346,&yymsp[-2].minor.yy0,&yymsp[0].minor.yy0);
127404}
127405        break;
127406      case 189: /* expr ::= nm DOT nm DOT nm */
127407{
127408  Expr *temp1 = sqlite3PExpr(pParse, TK_ID, 0, 0, &yymsp[-4].minor.yy0);
127409  Expr *temp2 = sqlite3PExpr(pParse, TK_ID, 0, 0, &yymsp[-2].minor.yy0);
127410  Expr *temp3 = sqlite3PExpr(pParse, TK_ID, 0, 0, &yymsp[0].minor.yy0);
127411  Expr *temp4 = sqlite3PExpr(pParse, TK_DOT, temp2, temp3, 0);
127412  yygotominor.yy346.pExpr = sqlite3PExpr(pParse, TK_DOT, temp1, temp4, 0);
127413  spanSet(&yygotominor.yy346,&yymsp[-4].minor.yy0,&yymsp[0].minor.yy0);
127414}
127415        break;
127416      case 192: /* expr ::= VARIABLE */
127417{
127418  if( yymsp[0].minor.yy0.n>=2 && yymsp[0].minor.yy0.z[0]=='#' && sqlite3Isdigit(yymsp[0].minor.yy0.z[1]) ){
127419    /* When doing a nested parse, one can include terms in an expression
127420    ** that look like this:   #1 #2 ...  These terms refer to registers
127421    ** in the virtual machine.  #N is the N-th register. */
127422    if( pParse->nested==0 ){
127423      sqlite3ErrorMsg(pParse, "near \"%T\": syntax error", &yymsp[0].minor.yy0);
127424      yygotominor.yy346.pExpr = 0;
127425    }else{
127426      yygotominor.yy346.pExpr = sqlite3PExpr(pParse, TK_REGISTER, 0, 0, &yymsp[0].minor.yy0);
127427      if( yygotominor.yy346.pExpr ) sqlite3GetInt32(&yymsp[0].minor.yy0.z[1], &yygotominor.yy346.pExpr->iTable);
127428    }
127429  }else{
127430    spanExpr(&yygotominor.yy346, pParse, TK_VARIABLE, &yymsp[0].minor.yy0);
127431    sqlite3ExprAssignVarNumber(pParse, yygotominor.yy346.pExpr);
127432  }
127433  spanSet(&yygotominor.yy346, &yymsp[0].minor.yy0, &yymsp[0].minor.yy0);
127434}
127435        break;
127436      case 193: /* expr ::= expr COLLATE ID|STRING */
127437{
127438  yygotominor.yy346.pExpr = sqlite3ExprAddCollateToken(pParse, yymsp[-2].minor.yy346.pExpr, &yymsp[0].minor.yy0, 1);
127439  yygotominor.yy346.zStart = yymsp[-2].minor.yy346.zStart;
127440  yygotominor.yy346.zEnd = &yymsp[0].minor.yy0.z[yymsp[0].minor.yy0.n];
127441}
127442        break;
127443      case 194: /* expr ::= CAST LP expr AS typetoken RP */
127444{
127445  yygotominor.yy346.pExpr = sqlite3PExpr(pParse, TK_CAST, yymsp[-3].minor.yy346.pExpr, 0, &yymsp[-1].minor.yy0);
127446  spanSet(&yygotominor.yy346,&yymsp[-5].minor.yy0,&yymsp[0].minor.yy0);
127447}
127448        break;
127449      case 195: /* expr ::= ID|INDEXED LP distinct exprlist RP */
127450{
127451  if( yymsp[-1].minor.yy14 && yymsp[-1].minor.yy14->nExpr>pParse->db->aLimit[SQLITE_LIMIT_FUNCTION_ARG] ){
127452    sqlite3ErrorMsg(pParse, "too many arguments on function %T", &yymsp[-4].minor.yy0);
127453  }
127454  yygotominor.yy346.pExpr = sqlite3ExprFunction(pParse, yymsp[-1].minor.yy14, &yymsp[-4].minor.yy0);
127455  spanSet(&yygotominor.yy346,&yymsp[-4].minor.yy0,&yymsp[0].minor.yy0);
127456  if( yymsp[-2].minor.yy381==SF_Distinct && yygotominor.yy346.pExpr ){
127457    yygotominor.yy346.pExpr->flags |= EP_Distinct;
127458  }
127459}
127460        break;
127461      case 196: /* expr ::= ID|INDEXED LP STAR RP */
127462{
127463  yygotominor.yy346.pExpr = sqlite3ExprFunction(pParse, 0, &yymsp[-3].minor.yy0);
127464  spanSet(&yygotominor.yy346,&yymsp[-3].minor.yy0,&yymsp[0].minor.yy0);
127465}
127466        break;
127467      case 197: /* term ::= CTIME_KW */
127468{
127469  yygotominor.yy346.pExpr = sqlite3ExprFunction(pParse, 0, &yymsp[0].minor.yy0);
127470  spanSet(&yygotominor.yy346, &yymsp[0].minor.yy0, &yymsp[0].minor.yy0);
127471}
127472        break;
127473      case 198: /* expr ::= expr AND expr */
127474      case 199: /* expr ::= expr OR expr */ yytestcase(yyruleno==199);
127475      case 200: /* expr ::= expr LT|GT|GE|LE expr */ yytestcase(yyruleno==200);
127476      case 201: /* expr ::= expr EQ|NE expr */ yytestcase(yyruleno==201);
127477      case 202: /* expr ::= expr BITAND|BITOR|LSHIFT|RSHIFT expr */ yytestcase(yyruleno==202);
127478      case 203: /* expr ::= expr PLUS|MINUS expr */ yytestcase(yyruleno==203);
127479      case 204: /* expr ::= expr STAR|SLASH|REM expr */ yytestcase(yyruleno==204);
127480      case 205: /* expr ::= expr CONCAT expr */ yytestcase(yyruleno==205);
127481{spanBinaryExpr(&yygotominor.yy346,pParse,yymsp[-1].major,&yymsp[-2].minor.yy346,&yymsp[0].minor.yy346);}
127482        break;
127483      case 206: /* likeop ::= LIKE_KW|MATCH */
127484{yygotominor.yy96.eOperator = yymsp[0].minor.yy0; yygotominor.yy96.bNot = 0;}
127485        break;
127486      case 207: /* likeop ::= NOT LIKE_KW|MATCH */
127487{yygotominor.yy96.eOperator = yymsp[0].minor.yy0; yygotominor.yy96.bNot = 1;}
127488        break;
127489      case 208: /* expr ::= expr likeop expr */
127490{
127491  ExprList *pList;
127492  pList = sqlite3ExprListAppend(pParse,0, yymsp[0].minor.yy346.pExpr);
127493  pList = sqlite3ExprListAppend(pParse,pList, yymsp[-2].minor.yy346.pExpr);
127494  yygotominor.yy346.pExpr = sqlite3ExprFunction(pParse, pList, &yymsp[-1].minor.yy96.eOperator);
127495  if( yymsp[-1].minor.yy96.bNot ) yygotominor.yy346.pExpr = sqlite3PExpr(pParse, TK_NOT, yygotominor.yy346.pExpr, 0, 0);
127496  yygotominor.yy346.zStart = yymsp[-2].minor.yy346.zStart;
127497  yygotominor.yy346.zEnd = yymsp[0].minor.yy346.zEnd;
127498  if( yygotominor.yy346.pExpr ) yygotominor.yy346.pExpr->flags |= EP_InfixFunc;
127499}
127500        break;
127501      case 209: /* expr ::= expr likeop expr ESCAPE expr */
127502{
127503  ExprList *pList;
127504  pList = sqlite3ExprListAppend(pParse,0, yymsp[-2].minor.yy346.pExpr);
127505  pList = sqlite3ExprListAppend(pParse,pList, yymsp[-4].minor.yy346.pExpr);
127506  pList = sqlite3ExprListAppend(pParse,pList, yymsp[0].minor.yy346.pExpr);
127507  yygotominor.yy346.pExpr = sqlite3ExprFunction(pParse, pList, &yymsp[-3].minor.yy96.eOperator);
127508  if( yymsp[-3].minor.yy96.bNot ) yygotominor.yy346.pExpr = sqlite3PExpr(pParse, TK_NOT, yygotominor.yy346.pExpr, 0, 0);
127509  yygotominor.yy346.zStart = yymsp[-4].minor.yy346.zStart;
127510  yygotominor.yy346.zEnd = yymsp[0].minor.yy346.zEnd;
127511  if( yygotominor.yy346.pExpr ) yygotominor.yy346.pExpr->flags |= EP_InfixFunc;
127512}
127513        break;
127514      case 210: /* expr ::= expr ISNULL|NOTNULL */
127515{spanUnaryPostfix(&yygotominor.yy346,pParse,yymsp[0].major,&yymsp[-1].minor.yy346,&yymsp[0].minor.yy0);}
127516        break;
127517      case 211: /* expr ::= expr NOT NULL */
127518{spanUnaryPostfix(&yygotominor.yy346,pParse,TK_NOTNULL,&yymsp[-2].minor.yy346,&yymsp[0].minor.yy0);}
127519        break;
127520      case 212: /* expr ::= expr IS expr */
127521{
127522  spanBinaryExpr(&yygotominor.yy346,pParse,TK_IS,&yymsp[-2].minor.yy346,&yymsp[0].minor.yy346);
127523  binaryToUnaryIfNull(pParse, yymsp[0].minor.yy346.pExpr, yygotominor.yy346.pExpr, TK_ISNULL);
127524}
127525        break;
127526      case 213: /* expr ::= expr IS NOT expr */
127527{
127528  spanBinaryExpr(&yygotominor.yy346,pParse,TK_ISNOT,&yymsp[-3].minor.yy346,&yymsp[0].minor.yy346);
127529  binaryToUnaryIfNull(pParse, yymsp[0].minor.yy346.pExpr, yygotominor.yy346.pExpr, TK_NOTNULL);
127530}
127531        break;
127532      case 214: /* expr ::= NOT expr */
127533      case 215: /* expr ::= BITNOT expr */ yytestcase(yyruleno==215);
127534{spanUnaryPrefix(&yygotominor.yy346,pParse,yymsp[-1].major,&yymsp[0].minor.yy346,&yymsp[-1].minor.yy0);}
127535        break;
127536      case 216: /* expr ::= MINUS expr */
127537{spanUnaryPrefix(&yygotominor.yy346,pParse,TK_UMINUS,&yymsp[0].minor.yy346,&yymsp[-1].minor.yy0);}
127538        break;
127539      case 217: /* expr ::= PLUS expr */
127540{spanUnaryPrefix(&yygotominor.yy346,pParse,TK_UPLUS,&yymsp[0].minor.yy346,&yymsp[-1].minor.yy0);}
127541        break;
127542      case 220: /* expr ::= expr between_op expr AND expr */
127543{
127544  ExprList *pList = sqlite3ExprListAppend(pParse,0, yymsp[-2].minor.yy346.pExpr);
127545  pList = sqlite3ExprListAppend(pParse,pList, yymsp[0].minor.yy346.pExpr);
127546  yygotominor.yy346.pExpr = sqlite3PExpr(pParse, TK_BETWEEN, yymsp[-4].minor.yy346.pExpr, 0, 0);
127547  if( yygotominor.yy346.pExpr ){
127548    yygotominor.yy346.pExpr->x.pList = pList;
127549  }else{
127550    sqlite3ExprListDelete(pParse->db, pList);
127551  }
127552  if( yymsp[-3].minor.yy328 ) yygotominor.yy346.pExpr = sqlite3PExpr(pParse, TK_NOT, yygotominor.yy346.pExpr, 0, 0);
127553  yygotominor.yy346.zStart = yymsp[-4].minor.yy346.zStart;
127554  yygotominor.yy346.zEnd = yymsp[0].minor.yy346.zEnd;
127555}
127556        break;
127557      case 223: /* expr ::= expr in_op LP exprlist RP */
127558{
127559    if( yymsp[-1].minor.yy14==0 ){
127560      /* Expressions of the form
127561      **
127562      **      expr1 IN ()
127563      **      expr1 NOT IN ()
127564      **
127565      ** simplify to constants 0 (false) and 1 (true), respectively,
127566      ** regardless of the value of expr1.
127567      */
127568      yygotominor.yy346.pExpr = sqlite3PExpr(pParse, TK_INTEGER, 0, 0, &sqlite3IntTokens[yymsp[-3].minor.yy328]);
127569      sqlite3ExprDelete(pParse->db, yymsp[-4].minor.yy346.pExpr);
127570    }else if( yymsp[-1].minor.yy14->nExpr==1 ){
127571      /* Expressions of the form:
127572      **
127573      **      expr1 IN (?1)
127574      **      expr1 NOT IN (?2)
127575      **
127576      ** with exactly one value on the RHS can be simplified to something
127577      ** like this:
127578      **
127579      **      expr1 == ?1
127580      **      expr1 <> ?2
127581      **
127582      ** But, the RHS of the == or <> is marked with the EP_Generic flag
127583      ** so that it may not contribute to the computation of comparison
127584      ** affinity or the collating sequence to use for comparison.  Otherwise,
127585      ** the semantics would be subtly different from IN or NOT IN.
127586      */
127587      Expr *pRHS = yymsp[-1].minor.yy14->a[0].pExpr;
127588      yymsp[-1].minor.yy14->a[0].pExpr = 0;
127589      sqlite3ExprListDelete(pParse->db, yymsp[-1].minor.yy14);
127590      /* pRHS cannot be NULL because a malloc error would have been detected
127591      ** before now and control would have never reached this point */
127592      if( ALWAYS(pRHS) ){
127593        pRHS->flags &= ~EP_Collate;
127594        pRHS->flags |= EP_Generic;
127595      }
127596      yygotominor.yy346.pExpr = sqlite3PExpr(pParse, yymsp[-3].minor.yy328 ? TK_NE : TK_EQ, yymsp[-4].minor.yy346.pExpr, pRHS, 0);
127597    }else{
127598      yygotominor.yy346.pExpr = sqlite3PExpr(pParse, TK_IN, yymsp[-4].minor.yy346.pExpr, 0, 0);
127599      if( yygotominor.yy346.pExpr ){
127600        yygotominor.yy346.pExpr->x.pList = yymsp[-1].minor.yy14;
127601        sqlite3ExprSetHeightAndFlags(pParse, yygotominor.yy346.pExpr);
127602      }else{
127603        sqlite3ExprListDelete(pParse->db, yymsp[-1].minor.yy14);
127604      }
127605      if( yymsp[-3].minor.yy328 ) yygotominor.yy346.pExpr = sqlite3PExpr(pParse, TK_NOT, yygotominor.yy346.pExpr, 0, 0);
127606    }
127607    yygotominor.yy346.zStart = yymsp[-4].minor.yy346.zStart;
127608    yygotominor.yy346.zEnd = &yymsp[0].minor.yy0.z[yymsp[0].minor.yy0.n];
127609  }
127610        break;
127611      case 224: /* expr ::= LP select RP */
127612{
127613    yygotominor.yy346.pExpr = sqlite3PExpr(pParse, TK_SELECT, 0, 0, 0);
127614    if( yygotominor.yy346.pExpr ){
127615      yygotominor.yy346.pExpr->x.pSelect = yymsp[-1].minor.yy3;
127616      ExprSetProperty(yygotominor.yy346.pExpr, EP_xIsSelect|EP_Subquery);
127617      sqlite3ExprSetHeightAndFlags(pParse, yygotominor.yy346.pExpr);
127618    }else{
127619      sqlite3SelectDelete(pParse->db, yymsp[-1].minor.yy3);
127620    }
127621    yygotominor.yy346.zStart = yymsp[-2].minor.yy0.z;
127622    yygotominor.yy346.zEnd = &yymsp[0].minor.yy0.z[yymsp[0].minor.yy0.n];
127623  }
127624        break;
127625      case 225: /* expr ::= expr in_op LP select RP */
127626{
127627    yygotominor.yy346.pExpr = sqlite3PExpr(pParse, TK_IN, yymsp[-4].minor.yy346.pExpr, 0, 0);
127628    if( yygotominor.yy346.pExpr ){
127629      yygotominor.yy346.pExpr->x.pSelect = yymsp[-1].minor.yy3;
127630      ExprSetProperty(yygotominor.yy346.pExpr, EP_xIsSelect|EP_Subquery);
127631      sqlite3ExprSetHeightAndFlags(pParse, yygotominor.yy346.pExpr);
127632    }else{
127633      sqlite3SelectDelete(pParse->db, yymsp[-1].minor.yy3);
127634    }
127635    if( yymsp[-3].minor.yy328 ) yygotominor.yy346.pExpr = sqlite3PExpr(pParse, TK_NOT, yygotominor.yy346.pExpr, 0, 0);
127636    yygotominor.yy346.zStart = yymsp[-4].minor.yy346.zStart;
127637    yygotominor.yy346.zEnd = &yymsp[0].minor.yy0.z[yymsp[0].minor.yy0.n];
127638  }
127639        break;
127640      case 226: /* expr ::= expr in_op nm dbnm */
127641{
127642    SrcList *pSrc = sqlite3SrcListAppend(pParse->db, 0,&yymsp[-1].minor.yy0,&yymsp[0].minor.yy0);
127643    yygotominor.yy346.pExpr = sqlite3PExpr(pParse, TK_IN, yymsp[-3].minor.yy346.pExpr, 0, 0);
127644    if( yygotominor.yy346.pExpr ){
127645      yygotominor.yy346.pExpr->x.pSelect = sqlite3SelectNew(pParse, 0,pSrc,0,0,0,0,0,0,0);
127646      ExprSetProperty(yygotominor.yy346.pExpr, EP_xIsSelect|EP_Subquery);
127647      sqlite3ExprSetHeightAndFlags(pParse, yygotominor.yy346.pExpr);
127648    }else{
127649      sqlite3SrcListDelete(pParse->db, pSrc);
127650    }
127651    if( yymsp[-2].minor.yy328 ) yygotominor.yy346.pExpr = sqlite3PExpr(pParse, TK_NOT, yygotominor.yy346.pExpr, 0, 0);
127652    yygotominor.yy346.zStart = yymsp[-3].minor.yy346.zStart;
127653    yygotominor.yy346.zEnd = yymsp[0].minor.yy0.z ? &yymsp[0].minor.yy0.z[yymsp[0].minor.yy0.n] : &yymsp[-1].minor.yy0.z[yymsp[-1].minor.yy0.n];
127654  }
127655        break;
127656      case 227: /* expr ::= EXISTS LP select RP */
127657{
127658    Expr *p = yygotominor.yy346.pExpr = sqlite3PExpr(pParse, TK_EXISTS, 0, 0, 0);
127659    if( p ){
127660      p->x.pSelect = yymsp[-1].minor.yy3;
127661      ExprSetProperty(p, EP_xIsSelect|EP_Subquery);
127662      sqlite3ExprSetHeightAndFlags(pParse, p);
127663    }else{
127664      sqlite3SelectDelete(pParse->db, yymsp[-1].minor.yy3);
127665    }
127666    yygotominor.yy346.zStart = yymsp[-3].minor.yy0.z;
127667    yygotominor.yy346.zEnd = &yymsp[0].minor.yy0.z[yymsp[0].minor.yy0.n];
127668  }
127669        break;
127670      case 228: /* expr ::= CASE case_operand case_exprlist case_else END */
127671{
127672  yygotominor.yy346.pExpr = sqlite3PExpr(pParse, TK_CASE, yymsp[-3].minor.yy132, 0, 0);
127673  if( yygotominor.yy346.pExpr ){
127674    yygotominor.yy346.pExpr->x.pList = yymsp[-1].minor.yy132 ? sqlite3ExprListAppend(pParse,yymsp[-2].minor.yy14,yymsp[-1].minor.yy132) : yymsp[-2].minor.yy14;
127675    sqlite3ExprSetHeightAndFlags(pParse, yygotominor.yy346.pExpr);
127676  }else{
127677    sqlite3ExprListDelete(pParse->db, yymsp[-2].minor.yy14);
127678    sqlite3ExprDelete(pParse->db, yymsp[-1].minor.yy132);
127679  }
127680  yygotominor.yy346.zStart = yymsp[-4].minor.yy0.z;
127681  yygotominor.yy346.zEnd = &yymsp[0].minor.yy0.z[yymsp[0].minor.yy0.n];
127682}
127683        break;
127684      case 229: /* case_exprlist ::= case_exprlist WHEN expr THEN expr */
127685{
127686  yygotominor.yy14 = sqlite3ExprListAppend(pParse,yymsp[-4].minor.yy14, yymsp[-2].minor.yy346.pExpr);
127687  yygotominor.yy14 = sqlite3ExprListAppend(pParse,yygotominor.yy14, yymsp[0].minor.yy346.pExpr);
127688}
127689        break;
127690      case 230: /* case_exprlist ::= WHEN expr THEN expr */
127691{
127692  yygotominor.yy14 = sqlite3ExprListAppend(pParse,0, yymsp[-2].minor.yy346.pExpr);
127693  yygotominor.yy14 = sqlite3ExprListAppend(pParse,yygotominor.yy14, yymsp[0].minor.yy346.pExpr);
127694}
127695        break;
127696      case 237: /* nexprlist ::= nexprlist COMMA expr */
127697{yygotominor.yy14 = sqlite3ExprListAppend(pParse,yymsp[-2].minor.yy14,yymsp[0].minor.yy346.pExpr);}
127698        break;
127699      case 238: /* nexprlist ::= expr */
127700{yygotominor.yy14 = sqlite3ExprListAppend(pParse,0,yymsp[0].minor.yy346.pExpr);}
127701        break;
127702      case 239: /* cmd ::= createkw uniqueflag INDEX ifnotexists nm dbnm ON nm LP idxlist RP where_opt */
127703{
127704  sqlite3CreateIndex(pParse, &yymsp[-7].minor.yy0, &yymsp[-6].minor.yy0,
127705                     sqlite3SrcListAppend(pParse->db,0,&yymsp[-4].minor.yy0,0), yymsp[-2].minor.yy14, yymsp[-10].minor.yy328,
127706                      &yymsp[-11].minor.yy0, yymsp[0].minor.yy132, SQLITE_SO_ASC, yymsp[-8].minor.yy328);
127707}
127708        break;
127709      case 240: /* uniqueflag ::= UNIQUE */
127710      case 291: /* raisetype ::= ABORT */ yytestcase(yyruleno==291);
127711{yygotominor.yy328 = OE_Abort;}
127712        break;
127713      case 241: /* uniqueflag ::= */
127714{yygotominor.yy328 = OE_None;}
127715        break;
127716      case 244: /* idxlist ::= idxlist COMMA nm collate sortorder */
127717{
127718  Expr *p = sqlite3ExprAddCollateToken(pParse, 0, &yymsp[-1].minor.yy0, 1);
127719  yygotominor.yy14 = sqlite3ExprListAppend(pParse,yymsp[-4].minor.yy14, p);
127720  sqlite3ExprListSetName(pParse,yygotominor.yy14,&yymsp[-2].minor.yy0,1);
127721  sqlite3ExprListCheckLength(pParse, yygotominor.yy14, "index");
127722  if( yygotominor.yy14 ) yygotominor.yy14->a[yygotominor.yy14->nExpr-1].sortOrder = (u8)yymsp[0].minor.yy328;
127723}
127724        break;
127725      case 245: /* idxlist ::= nm collate sortorder */
127726{
127727  Expr *p = sqlite3ExprAddCollateToken(pParse, 0, &yymsp[-1].minor.yy0, 1);
127728  yygotominor.yy14 = sqlite3ExprListAppend(pParse,0, p);
127729  sqlite3ExprListSetName(pParse, yygotominor.yy14, &yymsp[-2].minor.yy0, 1);
127730  sqlite3ExprListCheckLength(pParse, yygotominor.yy14, "index");
127731  if( yygotominor.yy14 ) yygotominor.yy14->a[yygotominor.yy14->nExpr-1].sortOrder = (u8)yymsp[0].minor.yy328;
127732}
127733        break;
127734      case 246: /* collate ::= */
127735{yygotominor.yy0.z = 0; yygotominor.yy0.n = 0;}
127736        break;
127737      case 248: /* cmd ::= DROP INDEX ifexists fullname */
127738{sqlite3DropIndex(pParse, yymsp[0].minor.yy65, yymsp[-1].minor.yy328);}
127739        break;
127740      case 249: /* cmd ::= VACUUM */
127741      case 250: /* cmd ::= VACUUM nm */ yytestcase(yyruleno==250);
127742{sqlite3Vacuum(pParse);}
127743        break;
127744      case 251: /* cmd ::= PRAGMA nm dbnm */
127745{sqlite3Pragma(pParse,&yymsp[-1].minor.yy0,&yymsp[0].minor.yy0,0,0);}
127746        break;
127747      case 252: /* cmd ::= PRAGMA nm dbnm EQ nmnum */
127748{sqlite3Pragma(pParse,&yymsp[-3].minor.yy0,&yymsp[-2].minor.yy0,&yymsp[0].minor.yy0,0);}
127749        break;
127750      case 253: /* cmd ::= PRAGMA nm dbnm LP nmnum RP */
127751{sqlite3Pragma(pParse,&yymsp[-4].minor.yy0,&yymsp[-3].minor.yy0,&yymsp[-1].minor.yy0,0);}
127752        break;
127753      case 254: /* cmd ::= PRAGMA nm dbnm EQ minus_num */
127754{sqlite3Pragma(pParse,&yymsp[-3].minor.yy0,&yymsp[-2].minor.yy0,&yymsp[0].minor.yy0,1);}
127755        break;
127756      case 255: /* cmd ::= PRAGMA nm dbnm LP minus_num RP */
127757{sqlite3Pragma(pParse,&yymsp[-4].minor.yy0,&yymsp[-3].minor.yy0,&yymsp[-1].minor.yy0,1);}
127758        break;
127759      case 264: /* cmd ::= createkw trigger_decl BEGIN trigger_cmd_list END */
127760{
127761  Token all;
127762  all.z = yymsp[-3].minor.yy0.z;
127763  all.n = (int)(yymsp[0].minor.yy0.z - yymsp[-3].minor.yy0.z) + yymsp[0].minor.yy0.n;
127764  sqlite3FinishTrigger(pParse, yymsp[-1].minor.yy473, &all);
127765}
127766        break;
127767      case 265: /* trigger_decl ::= temp TRIGGER ifnotexists nm dbnm trigger_time trigger_event ON fullname foreach_clause when_clause */
127768{
127769  sqlite3BeginTrigger(pParse, &yymsp[-7].minor.yy0, &yymsp[-6].minor.yy0, yymsp[-5].minor.yy328, yymsp[-4].minor.yy378.a, yymsp[-4].minor.yy378.b, yymsp[-2].minor.yy65, yymsp[0].minor.yy132, yymsp[-10].minor.yy328, yymsp[-8].minor.yy328);
127770  yygotominor.yy0 = (yymsp[-6].minor.yy0.n==0?yymsp[-7].minor.yy0:yymsp[-6].minor.yy0);
127771}
127772        break;
127773      case 266: /* trigger_time ::= BEFORE */
127774      case 269: /* trigger_time ::= */ yytestcase(yyruleno==269);
127775{ yygotominor.yy328 = TK_BEFORE; }
127776        break;
127777      case 267: /* trigger_time ::= AFTER */
127778{ yygotominor.yy328 = TK_AFTER;  }
127779        break;
127780      case 268: /* trigger_time ::= INSTEAD OF */
127781{ yygotominor.yy328 = TK_INSTEAD;}
127782        break;
127783      case 270: /* trigger_event ::= DELETE|INSERT */
127784      case 271: /* trigger_event ::= UPDATE */ yytestcase(yyruleno==271);
127785{yygotominor.yy378.a = yymsp[0].major; yygotominor.yy378.b = 0;}
127786        break;
127787      case 272: /* trigger_event ::= UPDATE OF idlist */
127788{yygotominor.yy378.a = TK_UPDATE; yygotominor.yy378.b = yymsp[0].minor.yy408;}
127789        break;
127790      case 275: /* when_clause ::= */
127791      case 296: /* key_opt ::= */ yytestcase(yyruleno==296);
127792{ yygotominor.yy132 = 0; }
127793        break;
127794      case 276: /* when_clause ::= WHEN expr */
127795      case 297: /* key_opt ::= KEY expr */ yytestcase(yyruleno==297);
127796{ yygotominor.yy132 = yymsp[0].minor.yy346.pExpr; }
127797        break;
127798      case 277: /* trigger_cmd_list ::= trigger_cmd_list trigger_cmd SEMI */
127799{
127800  assert( yymsp[-2].minor.yy473!=0 );
127801  yymsp[-2].minor.yy473->pLast->pNext = yymsp[-1].minor.yy473;
127802  yymsp[-2].minor.yy473->pLast = yymsp[-1].minor.yy473;
127803  yygotominor.yy473 = yymsp[-2].minor.yy473;
127804}
127805        break;
127806      case 278: /* trigger_cmd_list ::= trigger_cmd SEMI */
127807{
127808  assert( yymsp[-1].minor.yy473!=0 );
127809  yymsp[-1].minor.yy473->pLast = yymsp[-1].minor.yy473;
127810  yygotominor.yy473 = yymsp[-1].minor.yy473;
127811}
127812        break;
127813      case 280: /* trnm ::= nm DOT nm */
127814{
127815  yygotominor.yy0 = yymsp[0].minor.yy0;
127816  sqlite3ErrorMsg(pParse,
127817        "qualified table names are not allowed on INSERT, UPDATE, and DELETE "
127818        "statements within triggers");
127819}
127820        break;
127821      case 282: /* tridxby ::= INDEXED BY nm */
127822{
127823  sqlite3ErrorMsg(pParse,
127824        "the INDEXED BY clause is not allowed on UPDATE or DELETE statements "
127825        "within triggers");
127826}
127827        break;
127828      case 283: /* tridxby ::= NOT INDEXED */
127829{
127830  sqlite3ErrorMsg(pParse,
127831        "the NOT INDEXED clause is not allowed on UPDATE or DELETE statements "
127832        "within triggers");
127833}
127834        break;
127835      case 284: /* trigger_cmd ::= UPDATE orconf trnm tridxby SET setlist where_opt */
127836{ yygotominor.yy473 = sqlite3TriggerUpdateStep(pParse->db, &yymsp[-4].minor.yy0, yymsp[-1].minor.yy14, yymsp[0].minor.yy132, yymsp[-5].minor.yy186); }
127837        break;
127838      case 285: /* trigger_cmd ::= insert_cmd INTO trnm inscollist_opt select */
127839{yygotominor.yy473 = sqlite3TriggerInsertStep(pParse->db, &yymsp[-2].minor.yy0, yymsp[-1].minor.yy408, yymsp[0].minor.yy3, yymsp[-4].minor.yy186);}
127840        break;
127841      case 286: /* trigger_cmd ::= DELETE FROM trnm tridxby where_opt */
127842{yygotominor.yy473 = sqlite3TriggerDeleteStep(pParse->db, &yymsp[-2].minor.yy0, yymsp[0].minor.yy132);}
127843        break;
127844      case 287: /* trigger_cmd ::= select */
127845{yygotominor.yy473 = sqlite3TriggerSelectStep(pParse->db, yymsp[0].minor.yy3); }
127846        break;
127847      case 288: /* expr ::= RAISE LP IGNORE RP */
127848{
127849  yygotominor.yy346.pExpr = sqlite3PExpr(pParse, TK_RAISE, 0, 0, 0);
127850  if( yygotominor.yy346.pExpr ){
127851    yygotominor.yy346.pExpr->affinity = OE_Ignore;
127852  }
127853  yygotominor.yy346.zStart = yymsp[-3].minor.yy0.z;
127854  yygotominor.yy346.zEnd = &yymsp[0].minor.yy0.z[yymsp[0].minor.yy0.n];
127855}
127856        break;
127857      case 289: /* expr ::= RAISE LP raisetype COMMA nm RP */
127858{
127859  yygotominor.yy346.pExpr = sqlite3PExpr(pParse, TK_RAISE, 0, 0, &yymsp[-1].minor.yy0);
127860  if( yygotominor.yy346.pExpr ) {
127861    yygotominor.yy346.pExpr->affinity = (char)yymsp[-3].minor.yy328;
127862  }
127863  yygotominor.yy346.zStart = yymsp[-5].minor.yy0.z;
127864  yygotominor.yy346.zEnd = &yymsp[0].minor.yy0.z[yymsp[0].minor.yy0.n];
127865}
127866        break;
127867      case 290: /* raisetype ::= ROLLBACK */
127868{yygotominor.yy328 = OE_Rollback;}
127869        break;
127870      case 292: /* raisetype ::= FAIL */
127871{yygotominor.yy328 = OE_Fail;}
127872        break;
127873      case 293: /* cmd ::= DROP TRIGGER ifexists fullname */
127874{
127875  sqlite3DropTrigger(pParse,yymsp[0].minor.yy65,yymsp[-1].minor.yy328);
127876}
127877        break;
127878      case 294: /* cmd ::= ATTACH database_kw_opt expr AS expr key_opt */
127879{
127880  sqlite3Attach(pParse, yymsp[-3].minor.yy346.pExpr, yymsp[-1].minor.yy346.pExpr, yymsp[0].minor.yy132);
127881}
127882        break;
127883      case 295: /* cmd ::= DETACH database_kw_opt expr */
127884{
127885  sqlite3Detach(pParse, yymsp[0].minor.yy346.pExpr);
127886}
127887        break;
127888      case 300: /* cmd ::= REINDEX */
127889{sqlite3Reindex(pParse, 0, 0);}
127890        break;
127891      case 301: /* cmd ::= REINDEX nm dbnm */
127892{sqlite3Reindex(pParse, &yymsp[-1].minor.yy0, &yymsp[0].minor.yy0);}
127893        break;
127894      case 302: /* cmd ::= ANALYZE */
127895{sqlite3Analyze(pParse, 0, 0);}
127896        break;
127897      case 303: /* cmd ::= ANALYZE nm dbnm */
127898{sqlite3Analyze(pParse, &yymsp[-1].minor.yy0, &yymsp[0].minor.yy0);}
127899        break;
127900      case 304: /* cmd ::= ALTER TABLE fullname RENAME TO nm */
127901{
127902  sqlite3AlterRenameTable(pParse,yymsp[-3].minor.yy65,&yymsp[0].minor.yy0);
127903}
127904        break;
127905      case 305: /* cmd ::= ALTER TABLE add_column_fullname ADD kwcolumn_opt column */
127906{
127907  sqlite3AlterFinishAddColumn(pParse, &yymsp[0].minor.yy0);
127908}
127909        break;
127910      case 306: /* add_column_fullname ::= fullname */
127911{
127912  pParse->db->lookaside.bEnabled = 0;
127913  sqlite3AlterBeginAddColumn(pParse, yymsp[0].minor.yy65);
127914}
127915        break;
127916      case 309: /* cmd ::= create_vtab */
127917{sqlite3VtabFinishParse(pParse,0);}
127918        break;
127919      case 310: /* cmd ::= create_vtab LP vtabarglist RP */
127920{sqlite3VtabFinishParse(pParse,&yymsp[0].minor.yy0);}
127921        break;
127922      case 311: /* create_vtab ::= createkw VIRTUAL TABLE ifnotexists nm dbnm USING nm */
127923{
127924    sqlite3VtabBeginParse(pParse, &yymsp[-3].minor.yy0, &yymsp[-2].minor.yy0, &yymsp[0].minor.yy0, yymsp[-4].minor.yy328);
127925}
127926        break;
127927      case 314: /* vtabarg ::= */
127928{sqlite3VtabArgInit(pParse);}
127929        break;
127930      case 316: /* vtabargtoken ::= ANY */
127931      case 317: /* vtabargtoken ::= lp anylist RP */ yytestcase(yyruleno==317);
127932      case 318: /* lp ::= LP */ yytestcase(yyruleno==318);
127933{sqlite3VtabArgExtend(pParse,&yymsp[0].minor.yy0);}
127934        break;
127935      case 322: /* with ::= */
127936{yygotominor.yy59 = 0;}
127937        break;
127938      case 323: /* with ::= WITH wqlist */
127939      case 324: /* with ::= WITH RECURSIVE wqlist */ yytestcase(yyruleno==324);
127940{ yygotominor.yy59 = yymsp[0].minor.yy59; }
127941        break;
127942      case 325: /* wqlist ::= nm idxlist_opt AS LP select RP */
127943{
127944  yygotominor.yy59 = sqlite3WithAdd(pParse, 0, &yymsp[-5].minor.yy0, yymsp[-4].minor.yy14, yymsp[-1].minor.yy3);
127945}
127946        break;
127947      case 326: /* wqlist ::= wqlist COMMA nm idxlist_opt AS LP select RP */
127948{
127949  yygotominor.yy59 = sqlite3WithAdd(pParse, yymsp[-7].minor.yy59, &yymsp[-5].minor.yy0, yymsp[-4].minor.yy14, yymsp[-1].minor.yy3);
127950}
127951        break;
127952      default:
127953      /* (0) input ::= cmdlist */ yytestcase(yyruleno==0);
127954      /* (1) cmdlist ::= cmdlist ecmd */ yytestcase(yyruleno==1);
127955      /* (2) cmdlist ::= ecmd */ yytestcase(yyruleno==2);
127956      /* (3) ecmd ::= SEMI */ yytestcase(yyruleno==3);
127957      /* (4) ecmd ::= explain cmdx SEMI */ yytestcase(yyruleno==4);
127958      /* (10) trans_opt ::= */ yytestcase(yyruleno==10);
127959      /* (11) trans_opt ::= TRANSACTION */ yytestcase(yyruleno==11);
127960      /* (12) trans_opt ::= TRANSACTION nm */ yytestcase(yyruleno==12);
127961      /* (20) savepoint_opt ::= SAVEPOINT */ yytestcase(yyruleno==20);
127962      /* (21) savepoint_opt ::= */ yytestcase(yyruleno==21);
127963      /* (25) cmd ::= create_table create_table_args */ yytestcase(yyruleno==25);
127964      /* (36) columnlist ::= columnlist COMMA column */ yytestcase(yyruleno==36);
127965      /* (37) columnlist ::= column */ yytestcase(yyruleno==37);
127966      /* (43) type ::= */ yytestcase(yyruleno==43);
127967      /* (50) signed ::= plus_num */ yytestcase(yyruleno==50);
127968      /* (51) signed ::= minus_num */ yytestcase(yyruleno==51);
127969      /* (52) carglist ::= carglist ccons */ yytestcase(yyruleno==52);
127970      /* (53) carglist ::= */ yytestcase(yyruleno==53);
127971      /* (60) ccons ::= NULL onconf */ yytestcase(yyruleno==60);
127972      /* (88) conslist ::= conslist tconscomma tcons */ yytestcase(yyruleno==88);
127973      /* (89) conslist ::= tcons */ yytestcase(yyruleno==89);
127974      /* (91) tconscomma ::= */ yytestcase(yyruleno==91);
127975      /* (273) foreach_clause ::= */ yytestcase(yyruleno==273);
127976      /* (274) foreach_clause ::= FOR EACH ROW */ yytestcase(yyruleno==274);
127977      /* (281) tridxby ::= */ yytestcase(yyruleno==281);
127978      /* (298) database_kw_opt ::= DATABASE */ yytestcase(yyruleno==298);
127979      /* (299) database_kw_opt ::= */ yytestcase(yyruleno==299);
127980      /* (307) kwcolumn_opt ::= */ yytestcase(yyruleno==307);
127981      /* (308) kwcolumn_opt ::= COLUMNKW */ yytestcase(yyruleno==308);
127982      /* (312) vtabarglist ::= vtabarg */ yytestcase(yyruleno==312);
127983      /* (313) vtabarglist ::= vtabarglist COMMA vtabarg */ yytestcase(yyruleno==313);
127984      /* (315) vtabarg ::= vtabarg vtabargtoken */ yytestcase(yyruleno==315);
127985      /* (319) anylist ::= */ yytestcase(yyruleno==319);
127986      /* (320) anylist ::= anylist LP anylist RP */ yytestcase(yyruleno==320);
127987      /* (321) anylist ::= anylist ANY */ yytestcase(yyruleno==321);
127988        break;
127989  };
127990  assert( yyruleno>=0 && yyruleno<sizeof(yyRuleInfo)/sizeof(yyRuleInfo[0]) );
127991  yygoto = yyRuleInfo[yyruleno].lhs;
127992  yysize = yyRuleInfo[yyruleno].nrhs;
127993  yypParser->yyidx -= yysize;
127994  yyact = yy_find_reduce_action(yymsp[-yysize].stateno,(YYCODETYPE)yygoto);
127995  if( yyact < YYNSTATE ){
127996#ifdef NDEBUG
127997    /* If we are not debugging and the reduce action popped at least
127998    ** one element off the stack, then we can push the new element back
127999    ** onto the stack here, and skip the stack overflow test in yy_shift().
128000    ** That gives a significant speed improvement. */
128001    if( yysize ){
128002      yypParser->yyidx++;
128003      yymsp -= yysize-1;
128004      yymsp->stateno = (YYACTIONTYPE)yyact;
128005      yymsp->major = (YYCODETYPE)yygoto;
128006      yymsp->minor = yygotominor;
128007    }else
128008#endif
128009    {
128010      yy_shift(yypParser,yyact,yygoto,&yygotominor);
128011    }
128012  }else{
128013    assert( yyact == YYNSTATE + YYNRULE + 1 );
128014    yy_accept(yypParser);
128015  }
128016}
128017
128018/*
128019** The following code executes when the parse fails
128020*/
128021#ifndef YYNOERRORRECOVERY
128022static void yy_parse_failed(
128023  yyParser *yypParser           /* The parser */
128024){
128025  sqlite3ParserARG_FETCH;
128026#ifndef NDEBUG
128027  if( yyTraceFILE ){
128028    fprintf(yyTraceFILE,"%sFail!\n",yyTracePrompt);
128029  }
128030#endif
128031  while( yypParser->yyidx>=0 ) yy_pop_parser_stack(yypParser);
128032  /* Here code is inserted which will be executed whenever the
128033  ** parser fails */
128034  sqlite3ParserARG_STORE; /* Suppress warning about unused %extra_argument variable */
128035}
128036#endif /* YYNOERRORRECOVERY */
128037
128038/*
128039** The following code executes when a syntax error first occurs.
128040*/
128041static void yy_syntax_error(
128042  yyParser *yypParser,           /* The parser */
128043  int yymajor,                   /* The major type of the error token */
128044  YYMINORTYPE yyminor            /* The minor type of the error token */
128045){
128046  sqlite3ParserARG_FETCH;
128047#define TOKEN (yyminor.yy0)
128048
128049  UNUSED_PARAMETER(yymajor);  /* Silence some compiler warnings */
128050  assert( TOKEN.z[0] );  /* The tokenizer always gives us a token */
128051  sqlite3ErrorMsg(pParse, "near \"%T\": syntax error", &TOKEN);
128052  sqlite3ParserARG_STORE; /* Suppress warning about unused %extra_argument variable */
128053}
128054
128055/*
128056** The following is executed when the parser accepts
128057*/
128058static void yy_accept(
128059  yyParser *yypParser           /* The parser */
128060){
128061  sqlite3ParserARG_FETCH;
128062#ifndef NDEBUG
128063  if( yyTraceFILE ){
128064    fprintf(yyTraceFILE,"%sAccept!\n",yyTracePrompt);
128065  }
128066#endif
128067  while( yypParser->yyidx>=0 ) yy_pop_parser_stack(yypParser);
128068  /* Here code is inserted which will be executed whenever the
128069  ** parser accepts */
128070  sqlite3ParserARG_STORE; /* Suppress warning about unused %extra_argument variable */
128071}
128072
128073/* The main parser program.
128074** The first argument is a pointer to a structure obtained from
128075** "sqlite3ParserAlloc" which describes the current state of the parser.
128076** The second argument is the major token number.  The third is
128077** the minor token.  The fourth optional argument is whatever the
128078** user wants (and specified in the grammar) and is available for
128079** use by the action routines.
128080**
128081** Inputs:
128082** <ul>
128083** <li> A pointer to the parser (an opaque structure.)
128084** <li> The major token number.
128085** <li> The minor token number.
128086** <li> An option argument of a grammar-specified type.
128087** </ul>
128088**
128089** Outputs:
128090** None.
128091*/
128092SQLITE_PRIVATE void sqlite3Parser(
128093  void *yyp,                   /* The parser */
128094  int yymajor,                 /* The major token code number */
128095  sqlite3ParserTOKENTYPE yyminor       /* The value for the token */
128096  sqlite3ParserARG_PDECL               /* Optional %extra_argument parameter */
128097){
128098  YYMINORTYPE yyminorunion;
128099  int yyact;            /* The parser action. */
128100#if !defined(YYERRORSYMBOL) && !defined(YYNOERRORRECOVERY)
128101  int yyendofinput;     /* True if we are at the end of input */
128102#endif
128103#ifdef YYERRORSYMBOL
128104  int yyerrorhit = 0;   /* True if yymajor has invoked an error */
128105#endif
128106  yyParser *yypParser;  /* The parser */
128107
128108  /* (re)initialize the parser, if necessary */
128109  yypParser = (yyParser*)yyp;
128110  if( yypParser->yyidx<0 ){
128111#if YYSTACKDEPTH<=0
128112    if( yypParser->yystksz <=0 ){
128113      /*memset(&yyminorunion, 0, sizeof(yyminorunion));*/
128114      yyminorunion = yyzerominor;
128115      yyStackOverflow(yypParser, &yyminorunion);
128116      return;
128117    }
128118#endif
128119    yypParser->yyidx = 0;
128120    yypParser->yyerrcnt = -1;
128121    yypParser->yystack[0].stateno = 0;
128122    yypParser->yystack[0].major = 0;
128123  }
128124  yyminorunion.yy0 = yyminor;
128125#if !defined(YYERRORSYMBOL) && !defined(YYNOERRORRECOVERY)
128126  yyendofinput = (yymajor==0);
128127#endif
128128  sqlite3ParserARG_STORE;
128129
128130#ifndef NDEBUG
128131  if( yyTraceFILE ){
128132    fprintf(yyTraceFILE,"%sInput %s\n",yyTracePrompt,yyTokenName[yymajor]);
128133  }
128134#endif
128135
128136  do{
128137    yyact = yy_find_shift_action(yypParser,(YYCODETYPE)yymajor);
128138    if( yyact<YYNSTATE ){
128139      yy_shift(yypParser,yyact,yymajor,&yyminorunion);
128140      yypParser->yyerrcnt--;
128141      yymajor = YYNOCODE;
128142    }else if( yyact < YYNSTATE + YYNRULE ){
128143      yy_reduce(yypParser,yyact-YYNSTATE);
128144    }else{
128145      assert( yyact == YY_ERROR_ACTION );
128146#ifdef YYERRORSYMBOL
128147      int yymx;
128148#endif
128149#ifndef NDEBUG
128150      if( yyTraceFILE ){
128151        fprintf(yyTraceFILE,"%sSyntax Error!\n",yyTracePrompt);
128152      }
128153#endif
128154#ifdef YYERRORSYMBOL
128155      /* A syntax error has occurred.
128156      ** The response to an error depends upon whether or not the
128157      ** grammar defines an error token "ERROR".
128158      **
128159      ** This is what we do if the grammar does define ERROR:
128160      **
128161      **  * Call the %syntax_error function.
128162      **
128163      **  * Begin popping the stack until we enter a state where
128164      **    it is legal to shift the error symbol, then shift
128165      **    the error symbol.
128166      **
128167      **  * Set the error count to three.
128168      **
128169      **  * Begin accepting and shifting new tokens.  No new error
128170      **    processing will occur until three tokens have been
128171      **    shifted successfully.
128172      **
128173      */
128174      if( yypParser->yyerrcnt<0 ){
128175        yy_syntax_error(yypParser,yymajor,yyminorunion);
128176      }
128177      yymx = yypParser->yystack[yypParser->yyidx].major;
128178      if( yymx==YYERRORSYMBOL || yyerrorhit ){
128179#ifndef NDEBUG
128180        if( yyTraceFILE ){
128181          fprintf(yyTraceFILE,"%sDiscard input token %s\n",
128182             yyTracePrompt,yyTokenName[yymajor]);
128183        }
128184#endif
128185        yy_destructor(yypParser, (YYCODETYPE)yymajor,&yyminorunion);
128186        yymajor = YYNOCODE;
128187      }else{
128188         while(
128189          yypParser->yyidx >= 0 &&
128190          yymx != YYERRORSYMBOL &&
128191          (yyact = yy_find_reduce_action(
128192                        yypParser->yystack[yypParser->yyidx].stateno,
128193                        YYERRORSYMBOL)) >= YYNSTATE
128194        ){
128195          yy_pop_parser_stack(yypParser);
128196        }
128197        if( yypParser->yyidx < 0 || yymajor==0 ){
128198          yy_destructor(yypParser,(YYCODETYPE)yymajor,&yyminorunion);
128199          yy_parse_failed(yypParser);
128200          yymajor = YYNOCODE;
128201        }else if( yymx!=YYERRORSYMBOL ){
128202          YYMINORTYPE u2;
128203          u2.YYERRSYMDT = 0;
128204          yy_shift(yypParser,yyact,YYERRORSYMBOL,&u2);
128205        }
128206      }
128207      yypParser->yyerrcnt = 3;
128208      yyerrorhit = 1;
128209#elif defined(YYNOERRORRECOVERY)
128210      /* If the YYNOERRORRECOVERY macro is defined, then do not attempt to
128211      ** do any kind of error recovery.  Instead, simply invoke the syntax
128212      ** error routine and continue going as if nothing had happened.
128213      **
128214      ** Applications can set this macro (for example inside %include) if
128215      ** they intend to abandon the parse upon the first syntax error seen.
128216      */
128217      yy_syntax_error(yypParser,yymajor,yyminorunion);
128218      yy_destructor(yypParser,(YYCODETYPE)yymajor,&yyminorunion);
128219      yymajor = YYNOCODE;
128220
128221#else  /* YYERRORSYMBOL is not defined */
128222      /* This is what we do if the grammar does not define ERROR:
128223      **
128224      **  * Report an error message, and throw away the input token.
128225      **
128226      **  * If the input token is $, then fail the parse.
128227      **
128228      ** As before, subsequent error messages are suppressed until
128229      ** three input tokens have been successfully shifted.
128230      */
128231      if( yypParser->yyerrcnt<=0 ){
128232        yy_syntax_error(yypParser,yymajor,yyminorunion);
128233      }
128234      yypParser->yyerrcnt = 3;
128235      yy_destructor(yypParser,(YYCODETYPE)yymajor,&yyminorunion);
128236      if( yyendofinput ){
128237        yy_parse_failed(yypParser);
128238      }
128239      yymajor = YYNOCODE;
128240#endif
128241    }
128242  }while( yymajor!=YYNOCODE && yypParser->yyidx>=0 );
128243  return;
128244}
128245
128246/************** End of parse.c ***********************************************/
128247/************** Begin file tokenize.c ****************************************/
128248/*
128249** 2001 September 15
128250**
128251** The author disclaims copyright to this source code.  In place of
128252** a legal notice, here is a blessing:
128253**
128254**    May you do good and not evil.
128255**    May you find forgiveness for yourself and forgive others.
128256**    May you share freely, never taking more than you give.
128257**
128258*************************************************************************
128259** An tokenizer for SQL
128260**
128261** This file contains C code that splits an SQL input string up into
128262** individual tokens and sends those tokens one-by-one over to the
128263** parser for analysis.
128264*/
128265/* #include "sqliteInt.h" */
128266/* #include <stdlib.h> */
128267
128268/*
128269** The charMap() macro maps alphabetic characters into their
128270** lower-case ASCII equivalent.  On ASCII machines, this is just
128271** an upper-to-lower case map.  On EBCDIC machines we also need
128272** to adjust the encoding.  Only alphabetic characters and underscores
128273** need to be translated.
128274*/
128275#ifdef SQLITE_ASCII
128276# define charMap(X) sqlite3UpperToLower[(unsigned char)X]
128277#endif
128278#ifdef SQLITE_EBCDIC
128279# define charMap(X) ebcdicToAscii[(unsigned char)X]
128280const unsigned char ebcdicToAscii[] = {
128281/* 0   1   2   3   4   5   6   7   8   9   A   B   C   D   E   F */
128282   0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  /* 0x */
128283   0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  /* 1x */
128284   0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  /* 2x */
128285   0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  /* 3x */
128286   0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  /* 4x */
128287   0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  /* 5x */
128288   0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0, 95,  0,  0,  /* 6x */
128289   0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  /* 7x */
128290   0, 97, 98, 99,100,101,102,103,104,105,  0,  0,  0,  0,  0,  0,  /* 8x */
128291   0,106,107,108,109,110,111,112,113,114,  0,  0,  0,  0,  0,  0,  /* 9x */
128292   0,  0,115,116,117,118,119,120,121,122,  0,  0,  0,  0,  0,  0,  /* Ax */
128293   0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  /* Bx */
128294   0, 97, 98, 99,100,101,102,103,104,105,  0,  0,  0,  0,  0,  0,  /* Cx */
128295   0,106,107,108,109,110,111,112,113,114,  0,  0,  0,  0,  0,  0,  /* Dx */
128296   0,  0,115,116,117,118,119,120,121,122,  0,  0,  0,  0,  0,  0,  /* Ex */
128297   0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  /* Fx */
128298};
128299#endif
128300
128301/*
128302** The sqlite3KeywordCode function looks up an identifier to determine if
128303** it is a keyword.  If it is a keyword, the token code of that keyword is
128304** returned.  If the input is not a keyword, TK_ID is returned.
128305**
128306** The implementation of this routine was generated by a program,
128307** mkkeywordhash.h, located in the tool subdirectory of the distribution.
128308** The output of the mkkeywordhash.c program is written into a file
128309** named keywordhash.h and then included into this source file by
128310** the #include below.
128311*/
128312/************** Include keywordhash.h in the middle of tokenize.c ************/
128313/************** Begin file keywordhash.h *************************************/
128314/***** This file contains automatically generated code ******
128315**
128316** The code in this file has been automatically generated by
128317**
128318**   sqlite/tool/mkkeywordhash.c
128319**
128320** The code in this file implements a function that determines whether
128321** or not a given identifier is really an SQL keyword.  The same thing
128322** might be implemented more directly using a hand-written hash table.
128323** But by using this automatically generated code, the size of the code
128324** is substantially reduced.  This is important for embedded applications
128325** on platforms with limited memory.
128326*/
128327/* Hash score: 182 */
128328static int keywordCode(const char *z, int n){
128329  /* zText[] encodes 834 bytes of keywords in 554 bytes */
128330  /*   REINDEXEDESCAPEACHECKEYBEFOREIGNOREGEXPLAINSTEADDATABASELECT       */
128331  /*   ABLEFTHENDEFERRABLELSEXCEPTRANSACTIONATURALTERAISEXCLUSIVE         */
128332  /*   XISTSAVEPOINTERSECTRIGGEREFERENCESCONSTRAINTOFFSETEMPORARY         */
128333  /*   UNIQUERYWITHOUTERELEASEATTACHAVINGROUPDATEBEGINNERECURSIVE         */
128334  /*   BETWEENOTNULLIKECASCADELETECASECOLLATECREATECURRENT_DATEDETACH     */
128335  /*   IMMEDIATEJOINSERTMATCHPLANALYZEPRAGMABORTVALUESVIRTUALIMITWHEN     */
128336  /*   WHERENAMEAFTEREPLACEANDEFAULTAUTOINCREMENTCASTCOLUMNCOMMIT         */
128337  /*   CONFLICTCROSSCURRENT_TIMESTAMPRIMARYDEFERREDISTINCTDROPFAIL        */
128338  /*   FROMFULLGLOBYIFISNULLORDERESTRICTRIGHTROLLBACKROWUNIONUSING        */
128339  /*   VACUUMVIEWINITIALLY                                                */
128340  static const char zText[553] = {
128341    'R','E','I','N','D','E','X','E','D','E','S','C','A','P','E','A','C','H',
128342    'E','C','K','E','Y','B','E','F','O','R','E','I','G','N','O','R','E','G',
128343    'E','X','P','L','A','I','N','S','T','E','A','D','D','A','T','A','B','A',
128344    'S','E','L','E','C','T','A','B','L','E','F','T','H','E','N','D','E','F',
128345    'E','R','R','A','B','L','E','L','S','E','X','C','E','P','T','R','A','N',
128346    'S','A','C','T','I','O','N','A','T','U','R','A','L','T','E','R','A','I',
128347    'S','E','X','C','L','U','S','I','V','E','X','I','S','T','S','A','V','E',
128348    'P','O','I','N','T','E','R','S','E','C','T','R','I','G','G','E','R','E',
128349    'F','E','R','E','N','C','E','S','C','O','N','S','T','R','A','I','N','T',
128350    'O','F','F','S','E','T','E','M','P','O','R','A','R','Y','U','N','I','Q',
128351    'U','E','R','Y','W','I','T','H','O','U','T','E','R','E','L','E','A','S',
128352    'E','A','T','T','A','C','H','A','V','I','N','G','R','O','U','P','D','A',
128353    'T','E','B','E','G','I','N','N','E','R','E','C','U','R','S','I','V','E',
128354    'B','E','T','W','E','E','N','O','T','N','U','L','L','I','K','E','C','A',
128355    'S','C','A','D','E','L','E','T','E','C','A','S','E','C','O','L','L','A',
128356    'T','E','C','R','E','A','T','E','C','U','R','R','E','N','T','_','D','A',
128357    'T','E','D','E','T','A','C','H','I','M','M','E','D','I','A','T','E','J',
128358    'O','I','N','S','E','R','T','M','A','T','C','H','P','L','A','N','A','L',
128359    'Y','Z','E','P','R','A','G','M','A','B','O','R','T','V','A','L','U','E',
128360    'S','V','I','R','T','U','A','L','I','M','I','T','W','H','E','N','W','H',
128361    'E','R','E','N','A','M','E','A','F','T','E','R','E','P','L','A','C','E',
128362    'A','N','D','E','F','A','U','L','T','A','U','T','O','I','N','C','R','E',
128363    'M','E','N','T','C','A','S','T','C','O','L','U','M','N','C','O','M','M',
128364    'I','T','C','O','N','F','L','I','C','T','C','R','O','S','S','C','U','R',
128365    'R','E','N','T','_','T','I','M','E','S','T','A','M','P','R','I','M','A',
128366    'R','Y','D','E','F','E','R','R','E','D','I','S','T','I','N','C','T','D',
128367    'R','O','P','F','A','I','L','F','R','O','M','F','U','L','L','G','L','O',
128368    'B','Y','I','F','I','S','N','U','L','L','O','R','D','E','R','E','S','T',
128369    'R','I','C','T','R','I','G','H','T','R','O','L','L','B','A','C','K','R',
128370    'O','W','U','N','I','O','N','U','S','I','N','G','V','A','C','U','U','M',
128371    'V','I','E','W','I','N','I','T','I','A','L','L','Y',
128372  };
128373  static const unsigned char aHash[127] = {
128374      76, 105, 117,  74,   0,  45,   0,   0,  82,   0,  77,   0,   0,
128375      42,  12,  78,  15,   0, 116,  85,  54, 112,   0,  19,   0,   0,
128376     121,   0, 119, 115,   0,  22,  93,   0,   9,   0,   0,  70,  71,
128377       0,  69,   6,   0,  48,  90, 102,   0, 118, 101,   0,   0,  44,
128378       0, 103,  24,   0,  17,   0, 122,  53,  23,   0,   5, 110,  25,
128379      96,   0,   0, 124, 106,  60, 123,  57,  28,  55,   0,  91,   0,
128380     100,  26,   0,  99,   0,   0,   0,  95,  92,  97,  88, 109,  14,
128381      39, 108,   0,  81,   0,  18,  89, 111,  32,   0, 120,  80, 113,
128382      62,  46,  84,   0,   0,  94,  40,  59, 114,   0,  36,   0,   0,
128383      29,   0,  86,  63,  64,   0,  20,  61,   0,  56,
128384  };
128385  static const unsigned char aNext[124] = {
128386       0,   0,   0,   0,   4,   0,   0,   0,   0,   0,   0,   0,   0,
128387       0,   2,   0,   0,   0,   0,   0,   0,  13,   0,   0,   0,   0,
128388       0,   7,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
128389       0,   0,   0,   0,  33,   0,  21,   0,   0,   0,   0,   0,  50,
128390       0,  43,   3,  47,   0,   0,   0,   0,  30,   0,  58,   0,  38,
128391       0,   0,   0,   1,  66,   0,   0,  67,   0,  41,   0,   0,   0,
128392       0,   0,   0,  49,  65,   0,   0,   0,   0,  31,  52,  16,  34,
128393      10,   0,   0,   0,   0,   0,   0,   0,  11,  72,  79,   0,   8,
128394       0, 104,  98,   0, 107,   0,  87,   0,  75,  51,   0,  27,  37,
128395      73,  83,   0,  35,  68,   0,   0,
128396  };
128397  static const unsigned char aLen[124] = {
128398       7,   7,   5,   4,   6,   4,   5,   3,   6,   7,   3,   6,   6,
128399       7,   7,   3,   8,   2,   6,   5,   4,   4,   3,  10,   4,   6,
128400      11,   6,   2,   7,   5,   5,   9,   6,   9,   9,   7,  10,  10,
128401       4,   6,   2,   3,   9,   4,   2,   6,   5,   7,   4,   5,   7,
128402       6,   6,   5,   6,   5,   5,   9,   7,   7,   3,   2,   4,   4,
128403       7,   3,   6,   4,   7,   6,  12,   6,   9,   4,   6,   5,   4,
128404       7,   6,   5,   6,   7,   5,   4,   5,   6,   5,   7,   3,   7,
128405      13,   2,   2,   4,   6,   6,   8,   5,  17,  12,   7,   8,   8,
128406       2,   4,   4,   4,   4,   4,   2,   2,   6,   5,   8,   5,   8,
128407       3,   5,   5,   6,   4,   9,   3,
128408  };
128409  static const unsigned short int aOffset[124] = {
128410       0,   2,   2,   8,   9,  14,  16,  20,  23,  25,  25,  29,  33,
128411      36,  41,  46,  48,  53,  54,  59,  62,  65,  67,  69,  78,  81,
128412      86,  91,  95,  96, 101, 105, 109, 117, 122, 128, 136, 142, 152,
128413     159, 162, 162, 165, 167, 167, 171, 176, 179, 184, 184, 188, 192,
128414     199, 204, 209, 212, 218, 221, 225, 234, 240, 240, 240, 243, 246,
128415     250, 251, 255, 261, 265, 272, 278, 290, 296, 305, 307, 313, 318,
128416     320, 327, 332, 337, 343, 349, 354, 358, 361, 367, 371, 378, 380,
128417     387, 389, 391, 400, 404, 410, 416, 424, 429, 429, 445, 452, 459,
128418     460, 467, 471, 475, 479, 483, 486, 488, 490, 496, 500, 508, 513,
128419     521, 524, 529, 534, 540, 544, 549,
128420  };
128421  static const unsigned char aCode[124] = {
128422    TK_REINDEX,    TK_INDEXED,    TK_INDEX,      TK_DESC,       TK_ESCAPE,
128423    TK_EACH,       TK_CHECK,      TK_KEY,        TK_BEFORE,     TK_FOREIGN,
128424    TK_FOR,        TK_IGNORE,     TK_LIKE_KW,    TK_EXPLAIN,    TK_INSTEAD,
128425    TK_ADD,        TK_DATABASE,   TK_AS,         TK_SELECT,     TK_TABLE,
128426    TK_JOIN_KW,    TK_THEN,       TK_END,        TK_DEFERRABLE, TK_ELSE,
128427    TK_EXCEPT,     TK_TRANSACTION,TK_ACTION,     TK_ON,         TK_JOIN_KW,
128428    TK_ALTER,      TK_RAISE,      TK_EXCLUSIVE,  TK_EXISTS,     TK_SAVEPOINT,
128429    TK_INTERSECT,  TK_TRIGGER,    TK_REFERENCES, TK_CONSTRAINT, TK_INTO,
128430    TK_OFFSET,     TK_OF,         TK_SET,        TK_TEMP,       TK_TEMP,
128431    TK_OR,         TK_UNIQUE,     TK_QUERY,      TK_WITHOUT,    TK_WITH,
128432    TK_JOIN_KW,    TK_RELEASE,    TK_ATTACH,     TK_HAVING,     TK_GROUP,
128433    TK_UPDATE,     TK_BEGIN,      TK_JOIN_KW,    TK_RECURSIVE,  TK_BETWEEN,
128434    TK_NOTNULL,    TK_NOT,        TK_NO,         TK_NULL,       TK_LIKE_KW,
128435    TK_CASCADE,    TK_ASC,        TK_DELETE,     TK_CASE,       TK_COLLATE,
128436    TK_CREATE,     TK_CTIME_KW,   TK_DETACH,     TK_IMMEDIATE,  TK_JOIN,
128437    TK_INSERT,     TK_MATCH,      TK_PLAN,       TK_ANALYZE,    TK_PRAGMA,
128438    TK_ABORT,      TK_VALUES,     TK_VIRTUAL,    TK_LIMIT,      TK_WHEN,
128439    TK_WHERE,      TK_RENAME,     TK_AFTER,      TK_REPLACE,    TK_AND,
128440    TK_DEFAULT,    TK_AUTOINCR,   TK_TO,         TK_IN,         TK_CAST,
128441    TK_COLUMNKW,   TK_COMMIT,     TK_CONFLICT,   TK_JOIN_KW,    TK_CTIME_KW,
128442    TK_CTIME_KW,   TK_PRIMARY,    TK_DEFERRED,   TK_DISTINCT,   TK_IS,
128443    TK_DROP,       TK_FAIL,       TK_FROM,       TK_JOIN_KW,    TK_LIKE_KW,
128444    TK_BY,         TK_IF,         TK_ISNULL,     TK_ORDER,      TK_RESTRICT,
128445    TK_JOIN_KW,    TK_ROLLBACK,   TK_ROW,        TK_UNION,      TK_USING,
128446    TK_VACUUM,     TK_VIEW,       TK_INITIALLY,  TK_ALL,
128447  };
128448  int h, i;
128449  if( n<2 ) return TK_ID;
128450  h = ((charMap(z[0])*4) ^
128451      (charMap(z[n-1])*3) ^
128452      n) % 127;
128453  for(i=((int)aHash[h])-1; i>=0; i=((int)aNext[i])-1){
128454    if( aLen[i]==n && sqlite3StrNICmp(&zText[aOffset[i]],z,n)==0 ){
128455      testcase( i==0 ); /* REINDEX */
128456      testcase( i==1 ); /* INDEXED */
128457      testcase( i==2 ); /* INDEX */
128458      testcase( i==3 ); /* DESC */
128459      testcase( i==4 ); /* ESCAPE */
128460      testcase( i==5 ); /* EACH */
128461      testcase( i==6 ); /* CHECK */
128462      testcase( i==7 ); /* KEY */
128463      testcase( i==8 ); /* BEFORE */
128464      testcase( i==9 ); /* FOREIGN */
128465      testcase( i==10 ); /* FOR */
128466      testcase( i==11 ); /* IGNORE */
128467      testcase( i==12 ); /* REGEXP */
128468      testcase( i==13 ); /* EXPLAIN */
128469      testcase( i==14 ); /* INSTEAD */
128470      testcase( i==15 ); /* ADD */
128471      testcase( i==16 ); /* DATABASE */
128472      testcase( i==17 ); /* AS */
128473      testcase( i==18 ); /* SELECT */
128474      testcase( i==19 ); /* TABLE */
128475      testcase( i==20 ); /* LEFT */
128476      testcase( i==21 ); /* THEN */
128477      testcase( i==22 ); /* END */
128478      testcase( i==23 ); /* DEFERRABLE */
128479      testcase( i==24 ); /* ELSE */
128480      testcase( i==25 ); /* EXCEPT */
128481      testcase( i==26 ); /* TRANSACTION */
128482      testcase( i==27 ); /* ACTION */
128483      testcase( i==28 ); /* ON */
128484      testcase( i==29 ); /* NATURAL */
128485      testcase( i==30 ); /* ALTER */
128486      testcase( i==31 ); /* RAISE */
128487      testcase( i==32 ); /* EXCLUSIVE */
128488      testcase( i==33 ); /* EXISTS */
128489      testcase( i==34 ); /* SAVEPOINT */
128490      testcase( i==35 ); /* INTERSECT */
128491      testcase( i==36 ); /* TRIGGER */
128492      testcase( i==37 ); /* REFERENCES */
128493      testcase( i==38 ); /* CONSTRAINT */
128494      testcase( i==39 ); /* INTO */
128495      testcase( i==40 ); /* OFFSET */
128496      testcase( i==41 ); /* OF */
128497      testcase( i==42 ); /* SET */
128498      testcase( i==43 ); /* TEMPORARY */
128499      testcase( i==44 ); /* TEMP */
128500      testcase( i==45 ); /* OR */
128501      testcase( i==46 ); /* UNIQUE */
128502      testcase( i==47 ); /* QUERY */
128503      testcase( i==48 ); /* WITHOUT */
128504      testcase( i==49 ); /* WITH */
128505      testcase( i==50 ); /* OUTER */
128506      testcase( i==51 ); /* RELEASE */
128507      testcase( i==52 ); /* ATTACH */
128508      testcase( i==53 ); /* HAVING */
128509      testcase( i==54 ); /* GROUP */
128510      testcase( i==55 ); /* UPDATE */
128511      testcase( i==56 ); /* BEGIN */
128512      testcase( i==57 ); /* INNER */
128513      testcase( i==58 ); /* RECURSIVE */
128514      testcase( i==59 ); /* BETWEEN */
128515      testcase( i==60 ); /* NOTNULL */
128516      testcase( i==61 ); /* NOT */
128517      testcase( i==62 ); /* NO */
128518      testcase( i==63 ); /* NULL */
128519      testcase( i==64 ); /* LIKE */
128520      testcase( i==65 ); /* CASCADE */
128521      testcase( i==66 ); /* ASC */
128522      testcase( i==67 ); /* DELETE */
128523      testcase( i==68 ); /* CASE */
128524      testcase( i==69 ); /* COLLATE */
128525      testcase( i==70 ); /* CREATE */
128526      testcase( i==71 ); /* CURRENT_DATE */
128527      testcase( i==72 ); /* DETACH */
128528      testcase( i==73 ); /* IMMEDIATE */
128529      testcase( i==74 ); /* JOIN */
128530      testcase( i==75 ); /* INSERT */
128531      testcase( i==76 ); /* MATCH */
128532      testcase( i==77 ); /* PLAN */
128533      testcase( i==78 ); /* ANALYZE */
128534      testcase( i==79 ); /* PRAGMA */
128535      testcase( i==80 ); /* ABORT */
128536      testcase( i==81 ); /* VALUES */
128537      testcase( i==82 ); /* VIRTUAL */
128538      testcase( i==83 ); /* LIMIT */
128539      testcase( i==84 ); /* WHEN */
128540      testcase( i==85 ); /* WHERE */
128541      testcase( i==86 ); /* RENAME */
128542      testcase( i==87 ); /* AFTER */
128543      testcase( i==88 ); /* REPLACE */
128544      testcase( i==89 ); /* AND */
128545      testcase( i==90 ); /* DEFAULT */
128546      testcase( i==91 ); /* AUTOINCREMENT */
128547      testcase( i==92 ); /* TO */
128548      testcase( i==93 ); /* IN */
128549      testcase( i==94 ); /* CAST */
128550      testcase( i==95 ); /* COLUMN */
128551      testcase( i==96 ); /* COMMIT */
128552      testcase( i==97 ); /* CONFLICT */
128553      testcase( i==98 ); /* CROSS */
128554      testcase( i==99 ); /* CURRENT_TIMESTAMP */
128555      testcase( i==100 ); /* CURRENT_TIME */
128556      testcase( i==101 ); /* PRIMARY */
128557      testcase( i==102 ); /* DEFERRED */
128558      testcase( i==103 ); /* DISTINCT */
128559      testcase( i==104 ); /* IS */
128560      testcase( i==105 ); /* DROP */
128561      testcase( i==106 ); /* FAIL */
128562      testcase( i==107 ); /* FROM */
128563      testcase( i==108 ); /* FULL */
128564      testcase( i==109 ); /* GLOB */
128565      testcase( i==110 ); /* BY */
128566      testcase( i==111 ); /* IF */
128567      testcase( i==112 ); /* ISNULL */
128568      testcase( i==113 ); /* ORDER */
128569      testcase( i==114 ); /* RESTRICT */
128570      testcase( i==115 ); /* RIGHT */
128571      testcase( i==116 ); /* ROLLBACK */
128572      testcase( i==117 ); /* ROW */
128573      testcase( i==118 ); /* UNION */
128574      testcase( i==119 ); /* USING */
128575      testcase( i==120 ); /* VACUUM */
128576      testcase( i==121 ); /* VIEW */
128577      testcase( i==122 ); /* INITIALLY */
128578      testcase( i==123 ); /* ALL */
128579      return aCode[i];
128580    }
128581  }
128582  return TK_ID;
128583}
128584SQLITE_PRIVATE int sqlite3KeywordCode(const unsigned char *z, int n){
128585  return keywordCode((char*)z, n);
128586}
128587#define SQLITE_N_KEYWORD 124
128588
128589/************** End of keywordhash.h *****************************************/
128590/************** Continuing where we left off in tokenize.c *******************/
128591
128592
128593/*
128594** If X is a character that can be used in an identifier then
128595** IdChar(X) will be true.  Otherwise it is false.
128596**
128597** For ASCII, any character with the high-order bit set is
128598** allowed in an identifier.  For 7-bit characters,
128599** sqlite3IsIdChar[X] must be 1.
128600**
128601** For EBCDIC, the rules are more complex but have the same
128602** end result.
128603**
128604** Ticket #1066.  the SQL standard does not allow '$' in the
128605** middle of identifiers.  But many SQL implementations do.
128606** SQLite will allow '$' in identifiers for compatibility.
128607** But the feature is undocumented.
128608*/
128609#ifdef SQLITE_ASCII
128610#define IdChar(C)  ((sqlite3CtypeMap[(unsigned char)C]&0x46)!=0)
128611#endif
128612#ifdef SQLITE_EBCDIC
128613SQLITE_PRIVATE const char sqlite3IsEbcdicIdChar[] = {
128614/* x0 x1 x2 x3 x4 x5 x6 x7 x8 x9 xA xB xC xD xE xF */
128615    0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0,  /* 4x */
128616    0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 0, 0, 0,  /* 5x */
128617    0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0,  /* 6x */
128618    0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0,  /* 7x */
128619    0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 0,  /* 8x */
128620    0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 0, 1, 0,  /* 9x */
128621    1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0,  /* Ax */
128622    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  /* Bx */
128623    0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1,  /* Cx */
128624    0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1,  /* Dx */
128625    0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1,  /* Ex */
128626    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0,  /* Fx */
128627};
128628#define IdChar(C)  (((c=C)>=0x42 && sqlite3IsEbcdicIdChar[c-0x40]))
128629#endif
128630
128631/* Make the IdChar function accessible from ctime.c */
128632#ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
128633SQLITE_PRIVATE int sqlite3IsIdChar(u8 c){ return IdChar(c); }
128634#endif
128635
128636
128637/*
128638** Return the length of the token that begins at z[0].
128639** Store the token type in *tokenType before returning.
128640*/
128641SQLITE_PRIVATE int sqlite3GetToken(const unsigned char *z, int *tokenType){
128642  int i, c;
128643  switch( *z ){
128644    case ' ': case '\t': case '\n': case '\f': case '\r': {
128645      testcase( z[0]==' ' );
128646      testcase( z[0]=='\t' );
128647      testcase( z[0]=='\n' );
128648      testcase( z[0]=='\f' );
128649      testcase( z[0]=='\r' );
128650      for(i=1; sqlite3Isspace(z[i]); i++){}
128651      *tokenType = TK_SPACE;
128652      return i;
128653    }
128654    case '-': {
128655      if( z[1]=='-' ){
128656        for(i=2; (c=z[i])!=0 && c!='\n'; i++){}
128657        *tokenType = TK_SPACE;   /* IMP: R-22934-25134 */
128658        return i;
128659      }
128660      *tokenType = TK_MINUS;
128661      return 1;
128662    }
128663    case '(': {
128664      *tokenType = TK_LP;
128665      return 1;
128666    }
128667    case ')': {
128668      *tokenType = TK_RP;
128669      return 1;
128670    }
128671    case ';': {
128672      *tokenType = TK_SEMI;
128673      return 1;
128674    }
128675    case '+': {
128676      *tokenType = TK_PLUS;
128677      return 1;
128678    }
128679    case '*': {
128680      *tokenType = TK_STAR;
128681      return 1;
128682    }
128683    case '/': {
128684      if( z[1]!='*' || z[2]==0 ){
128685        *tokenType = TK_SLASH;
128686        return 1;
128687      }
128688      for(i=3, c=z[2]; (c!='*' || z[i]!='/') && (c=z[i])!=0; i++){}
128689      if( c ) i++;
128690      *tokenType = TK_SPACE;   /* IMP: R-22934-25134 */
128691      return i;
128692    }
128693    case '%': {
128694      *tokenType = TK_REM;
128695      return 1;
128696    }
128697    case '=': {
128698      *tokenType = TK_EQ;
128699      return 1 + (z[1]=='=');
128700    }
128701    case '<': {
128702      if( (c=z[1])=='=' ){
128703        *tokenType = TK_LE;
128704        return 2;
128705      }else if( c=='>' ){
128706        *tokenType = TK_NE;
128707        return 2;
128708      }else if( c=='<' ){
128709        *tokenType = TK_LSHIFT;
128710        return 2;
128711      }else{
128712        *tokenType = TK_LT;
128713        return 1;
128714      }
128715    }
128716    case '>': {
128717      if( (c=z[1])=='=' ){
128718        *tokenType = TK_GE;
128719        return 2;
128720      }else if( c=='>' ){
128721        *tokenType = TK_RSHIFT;
128722        return 2;
128723      }else{
128724        *tokenType = TK_GT;
128725        return 1;
128726      }
128727    }
128728    case '!': {
128729      if( z[1]!='=' ){
128730        *tokenType = TK_ILLEGAL;
128731        return 2;
128732      }else{
128733        *tokenType = TK_NE;
128734        return 2;
128735      }
128736    }
128737    case '|': {
128738      if( z[1]!='|' ){
128739        *tokenType = TK_BITOR;
128740        return 1;
128741      }else{
128742        *tokenType = TK_CONCAT;
128743        return 2;
128744      }
128745    }
128746    case ',': {
128747      *tokenType = TK_COMMA;
128748      return 1;
128749    }
128750    case '&': {
128751      *tokenType = TK_BITAND;
128752      return 1;
128753    }
128754    case '~': {
128755      *tokenType = TK_BITNOT;
128756      return 1;
128757    }
128758    case '`':
128759    case '\'':
128760    case '"': {
128761      int delim = z[0];
128762      testcase( delim=='`' );
128763      testcase( delim=='\'' );
128764      testcase( delim=='"' );
128765      for(i=1; (c=z[i])!=0; i++){
128766        if( c==delim ){
128767          if( z[i+1]==delim ){
128768            i++;
128769          }else{
128770            break;
128771          }
128772        }
128773      }
128774      if( c=='\'' ){
128775        *tokenType = TK_STRING;
128776        return i+1;
128777      }else if( c!=0 ){
128778        *tokenType = TK_ID;
128779        return i+1;
128780      }else{
128781        *tokenType = TK_ILLEGAL;
128782        return i;
128783      }
128784    }
128785    case '.': {
128786#ifndef SQLITE_OMIT_FLOATING_POINT
128787      if( !sqlite3Isdigit(z[1]) )
128788#endif
128789      {
128790        *tokenType = TK_DOT;
128791        return 1;
128792      }
128793      /* If the next character is a digit, this is a floating point
128794      ** number that begins with ".".  Fall thru into the next case */
128795    }
128796    case '0': case '1': case '2': case '3': case '4':
128797    case '5': case '6': case '7': case '8': case '9': {
128798      testcase( z[0]=='0' );  testcase( z[0]=='1' );  testcase( z[0]=='2' );
128799      testcase( z[0]=='3' );  testcase( z[0]=='4' );  testcase( z[0]=='5' );
128800      testcase( z[0]=='6' );  testcase( z[0]=='7' );  testcase( z[0]=='8' );
128801      testcase( z[0]=='9' );
128802      *tokenType = TK_INTEGER;
128803#ifndef SQLITE_OMIT_HEX_INTEGER
128804      if( z[0]=='0' && (z[1]=='x' || z[1]=='X') && sqlite3Isxdigit(z[2]) ){
128805        for(i=3; sqlite3Isxdigit(z[i]); i++){}
128806        return i;
128807      }
128808#endif
128809      for(i=0; sqlite3Isdigit(z[i]); i++){}
128810#ifndef SQLITE_OMIT_FLOATING_POINT
128811      if( z[i]=='.' ){
128812        i++;
128813        while( sqlite3Isdigit(z[i]) ){ i++; }
128814        *tokenType = TK_FLOAT;
128815      }
128816      if( (z[i]=='e' || z[i]=='E') &&
128817           ( sqlite3Isdigit(z[i+1])
128818            || ((z[i+1]=='+' || z[i+1]=='-') && sqlite3Isdigit(z[i+2]))
128819           )
128820      ){
128821        i += 2;
128822        while( sqlite3Isdigit(z[i]) ){ i++; }
128823        *tokenType = TK_FLOAT;
128824      }
128825#endif
128826      while( IdChar(z[i]) ){
128827        *tokenType = TK_ILLEGAL;
128828        i++;
128829      }
128830      return i;
128831    }
128832    case '[': {
128833      for(i=1, c=z[0]; c!=']' && (c=z[i])!=0; i++){}
128834      *tokenType = c==']' ? TK_ID : TK_ILLEGAL;
128835      return i;
128836    }
128837    case '?': {
128838      *tokenType = TK_VARIABLE;
128839      for(i=1; sqlite3Isdigit(z[i]); i++){}
128840      return i;
128841    }
128842#ifndef SQLITE_OMIT_TCL_VARIABLE
128843    case '$':
128844#endif
128845    case '@':  /* For compatibility with MS SQL Server */
128846    case '#':
128847    case ':': {
128848      int n = 0;
128849      testcase( z[0]=='$' );  testcase( z[0]=='@' );
128850      testcase( z[0]==':' );  testcase( z[0]=='#' );
128851      *tokenType = TK_VARIABLE;
128852      for(i=1; (c=z[i])!=0; i++){
128853        if( IdChar(c) ){
128854          n++;
128855#ifndef SQLITE_OMIT_TCL_VARIABLE
128856        }else if( c=='(' && n>0 ){
128857          do{
128858            i++;
128859          }while( (c=z[i])!=0 && !sqlite3Isspace(c) && c!=')' );
128860          if( c==')' ){
128861            i++;
128862          }else{
128863            *tokenType = TK_ILLEGAL;
128864          }
128865          break;
128866        }else if( c==':' && z[i+1]==':' ){
128867          i++;
128868#endif
128869        }else{
128870          break;
128871        }
128872      }
128873      if( n==0 ) *tokenType = TK_ILLEGAL;
128874      return i;
128875    }
128876#ifndef SQLITE_OMIT_BLOB_LITERAL
128877    case 'x': case 'X': {
128878      testcase( z[0]=='x' ); testcase( z[0]=='X' );
128879      if( z[1]=='\'' ){
128880        *tokenType = TK_BLOB;
128881        for(i=2; sqlite3Isxdigit(z[i]); i++){}
128882        if( z[i]!='\'' || i%2 ){
128883          *tokenType = TK_ILLEGAL;
128884          while( z[i] && z[i]!='\'' ){ i++; }
128885        }
128886        if( z[i] ) i++;
128887        return i;
128888      }
128889      /* Otherwise fall through to the next case */
128890    }
128891#endif
128892    default: {
128893      if( !IdChar(*z) ){
128894        break;
128895      }
128896      for(i=1; IdChar(z[i]); i++){}
128897      *tokenType = keywordCode((char*)z, i);
128898      return i;
128899    }
128900  }
128901  *tokenType = TK_ILLEGAL;
128902  return 1;
128903}
128904
128905/*
128906** Run the parser on the given SQL string.  The parser structure is
128907** passed in.  An SQLITE_ status code is returned.  If an error occurs
128908** then an and attempt is made to write an error message into
128909** memory obtained from sqlite3_malloc() and to make *pzErrMsg point to that
128910** error message.
128911*/
128912SQLITE_PRIVATE int sqlite3RunParser(Parse *pParse, const char *zSql, char **pzErrMsg){
128913  int nErr = 0;                   /* Number of errors encountered */
128914  int i;                          /* Loop counter */
128915  void *pEngine;                  /* The LEMON-generated LALR(1) parser */
128916  int tokenType;                  /* type of the next token */
128917  int lastTokenParsed = -1;       /* type of the previous token */
128918  u8 enableLookaside;             /* Saved value of db->lookaside.bEnabled */
128919  sqlite3 *db = pParse->db;       /* The database connection */
128920  int mxSqlLen;                   /* Max length of an SQL string */
128921
128922  assert( zSql!=0 );
128923  mxSqlLen = db->aLimit[SQLITE_LIMIT_SQL_LENGTH];
128924  if( db->nVdbeActive==0 ){
128925    db->u1.isInterrupted = 0;
128926  }
128927  pParse->rc = SQLITE_OK;
128928  pParse->zTail = zSql;
128929  i = 0;
128930  assert( pzErrMsg!=0 );
128931  pEngine = sqlite3ParserAlloc(sqlite3Malloc);
128932  if( pEngine==0 ){
128933    db->mallocFailed = 1;
128934    return SQLITE_NOMEM;
128935  }
128936  assert( pParse->pNewTable==0 );
128937  assert( pParse->pNewTrigger==0 );
128938  assert( pParse->nVar==0 );
128939  assert( pParse->nzVar==0 );
128940  assert( pParse->azVar==0 );
128941  enableLookaside = db->lookaside.bEnabled;
128942  if( db->lookaside.pStart ) db->lookaside.bEnabled = 1;
128943  while( !db->mallocFailed && zSql[i]!=0 ){
128944    assert( i>=0 );
128945    pParse->sLastToken.z = &zSql[i];
128946    pParse->sLastToken.n = sqlite3GetToken((unsigned char*)&zSql[i],&tokenType);
128947    i += pParse->sLastToken.n;
128948    if( i>mxSqlLen ){
128949      pParse->rc = SQLITE_TOOBIG;
128950      break;
128951    }
128952    switch( tokenType ){
128953      case TK_SPACE: {
128954        if( db->u1.isInterrupted ){
128955          sqlite3ErrorMsg(pParse, "interrupt");
128956          pParse->rc = SQLITE_INTERRUPT;
128957          goto abort_parse;
128958        }
128959        break;
128960      }
128961      case TK_ILLEGAL: {
128962        sqlite3ErrorMsg(pParse, "unrecognized token: \"%T\"",
128963                        &pParse->sLastToken);
128964        goto abort_parse;
128965      }
128966      case TK_SEMI: {
128967        pParse->zTail = &zSql[i];
128968        /* Fall thru into the default case */
128969      }
128970      default: {
128971        sqlite3Parser(pEngine, tokenType, pParse->sLastToken, pParse);
128972        lastTokenParsed = tokenType;
128973        if( pParse->rc!=SQLITE_OK ){
128974          goto abort_parse;
128975        }
128976        break;
128977      }
128978    }
128979  }
128980abort_parse:
128981  assert( nErr==0 );
128982  if( pParse->rc==SQLITE_OK && db->mallocFailed==0 ){
128983    assert( zSql[i]==0 );
128984    if( lastTokenParsed!=TK_SEMI ){
128985      sqlite3Parser(pEngine, TK_SEMI, pParse->sLastToken, pParse);
128986      pParse->zTail = &zSql[i];
128987    }
128988    if( pParse->rc==SQLITE_OK && db->mallocFailed==0 ){
128989      sqlite3Parser(pEngine, 0, pParse->sLastToken, pParse);
128990    }
128991  }
128992#ifdef YYTRACKMAXSTACKDEPTH
128993  sqlite3_mutex_enter(sqlite3MallocMutex());
128994  sqlite3StatusSet(SQLITE_STATUS_PARSER_STACK,
128995      sqlite3ParserStackPeak(pEngine)
128996  );
128997  sqlite3_mutex_leave(sqlite3MallocMutex());
128998#endif /* YYDEBUG */
128999  sqlite3ParserFree(pEngine, sqlite3_free);
129000  db->lookaside.bEnabled = enableLookaside;
129001  if( db->mallocFailed ){
129002    pParse->rc = SQLITE_NOMEM;
129003  }
129004  if( pParse->rc!=SQLITE_OK && pParse->rc!=SQLITE_DONE && pParse->zErrMsg==0 ){
129005    pParse->zErrMsg = sqlite3MPrintf(db, "%s", sqlite3ErrStr(pParse->rc));
129006  }
129007  assert( pzErrMsg!=0 );
129008  if( pParse->zErrMsg ){
129009    *pzErrMsg = pParse->zErrMsg;
129010    sqlite3_log(pParse->rc, "%s", *pzErrMsg);
129011    pParse->zErrMsg = 0;
129012    nErr++;
129013  }
129014  if( pParse->pVdbe && pParse->nErr>0 && pParse->nested==0 ){
129015    sqlite3VdbeDelete(pParse->pVdbe);
129016    pParse->pVdbe = 0;
129017  }
129018#ifndef SQLITE_OMIT_SHARED_CACHE
129019  if( pParse->nested==0 ){
129020    sqlite3DbFree(db, pParse->aTableLock);
129021    pParse->aTableLock = 0;
129022    pParse->nTableLock = 0;
129023  }
129024#endif
129025#ifndef SQLITE_OMIT_VIRTUALTABLE
129026  sqlite3_free(pParse->apVtabLock);
129027#endif
129028
129029  if( !IN_DECLARE_VTAB ){
129030    /* If the pParse->declareVtab flag is set, do not delete any table
129031    ** structure built up in pParse->pNewTable. The calling code (see vtab.c)
129032    ** will take responsibility for freeing the Table structure.
129033    */
129034    sqlite3DeleteTable(db, pParse->pNewTable);
129035  }
129036
129037  if( pParse->bFreeWith ) sqlite3WithDelete(db, pParse->pWith);
129038  sqlite3DeleteTrigger(db, pParse->pNewTrigger);
129039  for(i=pParse->nzVar-1; i>=0; i--) sqlite3DbFree(db, pParse->azVar[i]);
129040  sqlite3DbFree(db, pParse->azVar);
129041  while( pParse->pAinc ){
129042    AutoincInfo *p = pParse->pAinc;
129043    pParse->pAinc = p->pNext;
129044    sqlite3DbFree(db, p);
129045  }
129046  while( pParse->pZombieTab ){
129047    Table *p = pParse->pZombieTab;
129048    pParse->pZombieTab = p->pNextZombie;
129049    sqlite3DeleteTable(db, p);
129050  }
129051  assert( nErr==0 || pParse->rc!=SQLITE_OK );
129052  return nErr;
129053}
129054
129055/************** End of tokenize.c ********************************************/
129056/************** Begin file complete.c ****************************************/
129057/*
129058** 2001 September 15
129059**
129060** The author disclaims copyright to this source code.  In place of
129061** a legal notice, here is a blessing:
129062**
129063**    May you do good and not evil.
129064**    May you find forgiveness for yourself and forgive others.
129065**    May you share freely, never taking more than you give.
129066**
129067*************************************************************************
129068** An tokenizer for SQL
129069**
129070** This file contains C code that implements the sqlite3_complete() API.
129071** This code used to be part of the tokenizer.c source file.  But by
129072** separating it out, the code will be automatically omitted from
129073** static links that do not use it.
129074*/
129075/* #include "sqliteInt.h" */
129076#ifndef SQLITE_OMIT_COMPLETE
129077
129078/*
129079** This is defined in tokenize.c.  We just have to import the definition.
129080*/
129081#ifndef SQLITE_AMALGAMATION
129082#ifdef SQLITE_ASCII
129083#define IdChar(C)  ((sqlite3CtypeMap[(unsigned char)C]&0x46)!=0)
129084#endif
129085#ifdef SQLITE_EBCDIC
129086SQLITE_PRIVATE const char sqlite3IsEbcdicIdChar[];
129087#define IdChar(C)  (((c=C)>=0x42 && sqlite3IsEbcdicIdChar[c-0x40]))
129088#endif
129089#endif /* SQLITE_AMALGAMATION */
129090
129091
129092/*
129093** Token types used by the sqlite3_complete() routine.  See the header
129094** comments on that procedure for additional information.
129095*/
129096#define tkSEMI    0
129097#define tkWS      1
129098#define tkOTHER   2
129099#ifndef SQLITE_OMIT_TRIGGER
129100#define tkEXPLAIN 3
129101#define tkCREATE  4
129102#define tkTEMP    5
129103#define tkTRIGGER 6
129104#define tkEND     7
129105#endif
129106
129107/*
129108** Return TRUE if the given SQL string ends in a semicolon.
129109**
129110** Special handling is require for CREATE TRIGGER statements.
129111** Whenever the CREATE TRIGGER keywords are seen, the statement
129112** must end with ";END;".
129113**
129114** This implementation uses a state machine with 8 states:
129115**
129116**   (0) INVALID   We have not yet seen a non-whitespace character.
129117**
129118**   (1) START     At the beginning or end of an SQL statement.  This routine
129119**                 returns 1 if it ends in the START state and 0 if it ends
129120**                 in any other state.
129121**
129122**   (2) NORMAL    We are in the middle of statement which ends with a single
129123**                 semicolon.
129124**
129125**   (3) EXPLAIN   The keyword EXPLAIN has been seen at the beginning of
129126**                 a statement.
129127**
129128**   (4) CREATE    The keyword CREATE has been seen at the beginning of a
129129**                 statement, possibly preceded by EXPLAIN and/or followed by
129130**                 TEMP or TEMPORARY
129131**
129132**   (5) TRIGGER   We are in the middle of a trigger definition that must be
129133**                 ended by a semicolon, the keyword END, and another semicolon.
129134**
129135**   (6) SEMI      We've seen the first semicolon in the ";END;" that occurs at
129136**                 the end of a trigger definition.
129137**
129138**   (7) END       We've seen the ";END" of the ";END;" that occurs at the end
129139**                 of a trigger definition.
129140**
129141** Transitions between states above are determined by tokens extracted
129142** from the input.  The following tokens are significant:
129143**
129144**   (0) tkSEMI      A semicolon.
129145**   (1) tkWS        Whitespace.
129146**   (2) tkOTHER     Any other SQL token.
129147**   (3) tkEXPLAIN   The "explain" keyword.
129148**   (4) tkCREATE    The "create" keyword.
129149**   (5) tkTEMP      The "temp" or "temporary" keyword.
129150**   (6) tkTRIGGER   The "trigger" keyword.
129151**   (7) tkEND       The "end" keyword.
129152**
129153** Whitespace never causes a state transition and is always ignored.
129154** This means that a SQL string of all whitespace is invalid.
129155**
129156** If we compile with SQLITE_OMIT_TRIGGER, all of the computation needed
129157** to recognize the end of a trigger can be omitted.  All we have to do
129158** is look for a semicolon that is not part of an string or comment.
129159*/
129160SQLITE_API int SQLITE_STDCALL sqlite3_complete(const char *zSql){
129161  u8 state = 0;   /* Current state, using numbers defined in header comment */
129162  u8 token;       /* Value of the next token */
129163
129164#ifndef SQLITE_OMIT_TRIGGER
129165  /* A complex statement machine used to detect the end of a CREATE TRIGGER
129166  ** statement.  This is the normal case.
129167  */
129168  static const u8 trans[8][8] = {
129169                     /* Token:                                                */
129170     /* State:       **  SEMI  WS  OTHER  EXPLAIN  CREATE  TEMP  TRIGGER  END */
129171     /* 0 INVALID: */ {    1,  0,     2,       3,      4,    2,       2,   2, },
129172     /* 1   START: */ {    1,  1,     2,       3,      4,    2,       2,   2, },
129173     /* 2  NORMAL: */ {    1,  2,     2,       2,      2,    2,       2,   2, },
129174     /* 3 EXPLAIN: */ {    1,  3,     3,       2,      4,    2,       2,   2, },
129175     /* 4  CREATE: */ {    1,  4,     2,       2,      2,    4,       5,   2, },
129176     /* 5 TRIGGER: */ {    6,  5,     5,       5,      5,    5,       5,   5, },
129177     /* 6    SEMI: */ {    6,  6,     5,       5,      5,    5,       5,   7, },
129178     /* 7     END: */ {    1,  7,     5,       5,      5,    5,       5,   5, },
129179  };
129180#else
129181  /* If triggers are not supported by this compile then the statement machine
129182  ** used to detect the end of a statement is much simpler
129183  */
129184  static const u8 trans[3][3] = {
129185                     /* Token:           */
129186     /* State:       **  SEMI  WS  OTHER */
129187     /* 0 INVALID: */ {    1,  0,     2, },
129188     /* 1   START: */ {    1,  1,     2, },
129189     /* 2  NORMAL: */ {    1,  2,     2, },
129190  };
129191#endif /* SQLITE_OMIT_TRIGGER */
129192
129193#ifdef SQLITE_ENABLE_API_ARMOR
129194  if( zSql==0 ){
129195    (void)SQLITE_MISUSE_BKPT;
129196    return 0;
129197  }
129198#endif
129199
129200  while( *zSql ){
129201    switch( *zSql ){
129202      case ';': {  /* A semicolon */
129203        token = tkSEMI;
129204        break;
129205      }
129206      case ' ':
129207      case '\r':
129208      case '\t':
129209      case '\n':
129210      case '\f': {  /* White space is ignored */
129211        token = tkWS;
129212        break;
129213      }
129214      case '/': {   /* C-style comments */
129215        if( zSql[1]!='*' ){
129216          token = tkOTHER;
129217          break;
129218        }
129219        zSql += 2;
129220        while( zSql[0] && (zSql[0]!='*' || zSql[1]!='/') ){ zSql++; }
129221        if( zSql[0]==0 ) return 0;
129222        zSql++;
129223        token = tkWS;
129224        break;
129225      }
129226      case '-': {   /* SQL-style comments from "--" to end of line */
129227        if( zSql[1]!='-' ){
129228          token = tkOTHER;
129229          break;
129230        }
129231        while( *zSql && *zSql!='\n' ){ zSql++; }
129232        if( *zSql==0 ) return state==1;
129233        token = tkWS;
129234        break;
129235      }
129236      case '[': {   /* Microsoft-style identifiers in [...] */
129237        zSql++;
129238        while( *zSql && *zSql!=']' ){ zSql++; }
129239        if( *zSql==0 ) return 0;
129240        token = tkOTHER;
129241        break;
129242      }
129243      case '`':     /* Grave-accent quoted symbols used by MySQL */
129244      case '"':     /* single- and double-quoted strings */
129245      case '\'': {
129246        int c = *zSql;
129247        zSql++;
129248        while( *zSql && *zSql!=c ){ zSql++; }
129249        if( *zSql==0 ) return 0;
129250        token = tkOTHER;
129251        break;
129252      }
129253      default: {
129254#ifdef SQLITE_EBCDIC
129255        unsigned char c;
129256#endif
129257        if( IdChar((u8)*zSql) ){
129258          /* Keywords and unquoted identifiers */
129259          int nId;
129260          for(nId=1; IdChar(zSql[nId]); nId++){}
129261#ifdef SQLITE_OMIT_TRIGGER
129262          token = tkOTHER;
129263#else
129264          switch( *zSql ){
129265            case 'c': case 'C': {
129266              if( nId==6 && sqlite3StrNICmp(zSql, "create", 6)==0 ){
129267                token = tkCREATE;
129268              }else{
129269                token = tkOTHER;
129270              }
129271              break;
129272            }
129273            case 't': case 'T': {
129274              if( nId==7 && sqlite3StrNICmp(zSql, "trigger", 7)==0 ){
129275                token = tkTRIGGER;
129276              }else if( nId==4 && sqlite3StrNICmp(zSql, "temp", 4)==0 ){
129277                token = tkTEMP;
129278              }else if( nId==9 && sqlite3StrNICmp(zSql, "temporary", 9)==0 ){
129279                token = tkTEMP;
129280              }else{
129281                token = tkOTHER;
129282              }
129283              break;
129284            }
129285            case 'e':  case 'E': {
129286              if( nId==3 && sqlite3StrNICmp(zSql, "end", 3)==0 ){
129287                token = tkEND;
129288              }else
129289#ifndef SQLITE_OMIT_EXPLAIN
129290              if( nId==7 && sqlite3StrNICmp(zSql, "explain", 7)==0 ){
129291                token = tkEXPLAIN;
129292              }else
129293#endif
129294              {
129295                token = tkOTHER;
129296              }
129297              break;
129298            }
129299            default: {
129300              token = tkOTHER;
129301              break;
129302            }
129303          }
129304#endif /* SQLITE_OMIT_TRIGGER */
129305          zSql += nId-1;
129306        }else{
129307          /* Operators and special symbols */
129308          token = tkOTHER;
129309        }
129310        break;
129311      }
129312    }
129313    state = trans[state][token];
129314    zSql++;
129315  }
129316  return state==1;
129317}
129318
129319#ifndef SQLITE_OMIT_UTF16
129320/*
129321** This routine is the same as the sqlite3_complete() routine described
129322** above, except that the parameter is required to be UTF-16 encoded, not
129323** UTF-8.
129324*/
129325SQLITE_API int SQLITE_STDCALL sqlite3_complete16(const void *zSql){
129326  sqlite3_value *pVal;
129327  char const *zSql8;
129328  int rc;
129329
129330#ifndef SQLITE_OMIT_AUTOINIT
129331  rc = sqlite3_initialize();
129332  if( rc ) return rc;
129333#endif
129334  pVal = sqlite3ValueNew(0);
129335  sqlite3ValueSetStr(pVal, -1, zSql, SQLITE_UTF16NATIVE, SQLITE_STATIC);
129336  zSql8 = sqlite3ValueText(pVal, SQLITE_UTF8);
129337  if( zSql8 ){
129338    rc = sqlite3_complete(zSql8);
129339  }else{
129340    rc = SQLITE_NOMEM;
129341  }
129342  sqlite3ValueFree(pVal);
129343  return rc & 0xff;
129344}
129345#endif /* SQLITE_OMIT_UTF16 */
129346#endif /* SQLITE_OMIT_COMPLETE */
129347
129348/************** End of complete.c ********************************************/
129349/************** Begin file main.c ********************************************/
129350/*
129351** 2001 September 15
129352**
129353** The author disclaims copyright to this source code.  In place of
129354** a legal notice, here is a blessing:
129355**
129356**    May you do good and not evil.
129357**    May you find forgiveness for yourself and forgive others.
129358**    May you share freely, never taking more than you give.
129359**
129360*************************************************************************
129361** Main file for the SQLite library.  The routines in this file
129362** implement the programmer interface to the library.  Routines in
129363** other files are for internal use by SQLite and should not be
129364** accessed by users of the library.
129365*/
129366/* #include "sqliteInt.h" */
129367
129368#ifdef SQLITE_ENABLE_FTS3
129369/************** Include fts3.h in the middle of main.c ***********************/
129370/************** Begin file fts3.h ********************************************/
129371/*
129372** 2006 Oct 10
129373**
129374** The author disclaims copyright to this source code.  In place of
129375** a legal notice, here is a blessing:
129376**
129377**    May you do good and not evil.
129378**    May you find forgiveness for yourself and forgive others.
129379**    May you share freely, never taking more than you give.
129380**
129381******************************************************************************
129382**
129383** This header file is used by programs that want to link against the
129384** FTS3 library.  All it does is declare the sqlite3Fts3Init() interface.
129385*/
129386/* #include "sqlite3.h" */
129387
129388#if 0
129389extern "C" {
129390#endif  /* __cplusplus */
129391
129392SQLITE_PRIVATE int sqlite3Fts3Init(sqlite3 *db);
129393
129394#if 0
129395}  /* extern "C" */
129396#endif  /* __cplusplus */
129397
129398/************** End of fts3.h ************************************************/
129399/************** Continuing where we left off in main.c ***********************/
129400#endif
129401#ifdef SQLITE_ENABLE_RTREE
129402/************** Include rtree.h in the middle of main.c **********************/
129403/************** Begin file rtree.h *******************************************/
129404/*
129405** 2008 May 26
129406**
129407** The author disclaims copyright to this source code.  In place of
129408** a legal notice, here is a blessing:
129409**
129410**    May you do good and not evil.
129411**    May you find forgiveness for yourself and forgive others.
129412**    May you share freely, never taking more than you give.
129413**
129414******************************************************************************
129415**
129416** This header file is used by programs that want to link against the
129417** RTREE library.  All it does is declare the sqlite3RtreeInit() interface.
129418*/
129419/* #include "sqlite3.h" */
129420
129421#if 0
129422extern "C" {
129423#endif  /* __cplusplus */
129424
129425SQLITE_PRIVATE int sqlite3RtreeInit(sqlite3 *db);
129426
129427#if 0
129428}  /* extern "C" */
129429#endif  /* __cplusplus */
129430
129431/************** End of rtree.h ***********************************************/
129432/************** Continuing where we left off in main.c ***********************/
129433#endif
129434#ifdef SQLITE_ENABLE_ICU
129435/************** Include sqliteicu.h in the middle of main.c ******************/
129436/************** Begin file sqliteicu.h ***************************************/
129437/*
129438** 2008 May 26
129439**
129440** The author disclaims copyright to this source code.  In place of
129441** a legal notice, here is a blessing:
129442**
129443**    May you do good and not evil.
129444**    May you find forgiveness for yourself and forgive others.
129445**    May you share freely, never taking more than you give.
129446**
129447******************************************************************************
129448**
129449** This header file is used by programs that want to link against the
129450** ICU extension.  All it does is declare the sqlite3IcuInit() interface.
129451*/
129452/* #include "sqlite3.h" */
129453
129454#if 0
129455extern "C" {
129456#endif  /* __cplusplus */
129457
129458SQLITE_PRIVATE int sqlite3IcuInit(sqlite3 *db);
129459
129460#if 0
129461}  /* extern "C" */
129462#endif  /* __cplusplus */
129463
129464
129465/************** End of sqliteicu.h *******************************************/
129466/************** Continuing where we left off in main.c ***********************/
129467#endif
129468
129469#ifndef SQLITE_AMALGAMATION
129470/* IMPLEMENTATION-OF: R-46656-45156 The sqlite3_version[] string constant
129471** contains the text of SQLITE_VERSION macro.
129472*/
129473SQLITE_API const char sqlite3_version[] = SQLITE_VERSION;
129474#endif
129475
129476/* IMPLEMENTATION-OF: R-53536-42575 The sqlite3_libversion() function returns
129477** a pointer to the to the sqlite3_version[] string constant.
129478*/
129479SQLITE_API const char *SQLITE_STDCALL sqlite3_libversion(void){ return sqlite3_version; }
129480
129481/* IMPLEMENTATION-OF: R-63124-39300 The sqlite3_sourceid() function returns a
129482** pointer to a string constant whose value is the same as the
129483** SQLITE_SOURCE_ID C preprocessor macro.
129484*/
129485SQLITE_API const char *SQLITE_STDCALL sqlite3_sourceid(void){ return SQLITE_SOURCE_ID; }
129486
129487/* IMPLEMENTATION-OF: R-35210-63508 The sqlite3_libversion_number() function
129488** returns an integer equal to SQLITE_VERSION_NUMBER.
129489*/
129490SQLITE_API int SQLITE_STDCALL sqlite3_libversion_number(void){ return SQLITE_VERSION_NUMBER; }
129491
129492/* IMPLEMENTATION-OF: R-20790-14025 The sqlite3_threadsafe() function returns
129493** zero if and only if SQLite was compiled with mutexing code omitted due to
129494** the SQLITE_THREADSAFE compile-time option being set to 0.
129495*/
129496SQLITE_API int SQLITE_STDCALL sqlite3_threadsafe(void){ return SQLITE_THREADSAFE; }
129497
129498/*
129499** When compiling the test fixture or with debugging enabled (on Win32),
129500** this variable being set to non-zero will cause OSTRACE macros to emit
129501** extra diagnostic information.
129502*/
129503#ifdef SQLITE_HAVE_OS_TRACE
129504# ifndef SQLITE_DEBUG_OS_TRACE
129505#   define SQLITE_DEBUG_OS_TRACE 0
129506# endif
129507  int sqlite3OSTrace = SQLITE_DEBUG_OS_TRACE;
129508#endif
129509
129510#if !defined(SQLITE_OMIT_TRACE) && defined(SQLITE_ENABLE_IOTRACE)
129511/*
129512** If the following function pointer is not NULL and if
129513** SQLITE_ENABLE_IOTRACE is enabled, then messages describing
129514** I/O active are written using this function.  These messages
129515** are intended for debugging activity only.
129516*/
129517SQLITE_API void (SQLITE_CDECL *sqlite3IoTrace)(const char*, ...) = 0;
129518#endif
129519
129520/*
129521** If the following global variable points to a string which is the
129522** name of a directory, then that directory will be used to store
129523** temporary files.
129524**
129525** See also the "PRAGMA temp_store_directory" SQL command.
129526*/
129527SQLITE_API char *sqlite3_temp_directory = 0;
129528
129529/*
129530** If the following global variable points to a string which is the
129531** name of a directory, then that directory will be used to store
129532** all database files specified with a relative pathname.
129533**
129534** See also the "PRAGMA data_store_directory" SQL command.
129535*/
129536SQLITE_API char *sqlite3_data_directory = 0;
129537
129538/*
129539** Initialize SQLite.
129540**
129541** This routine must be called to initialize the memory allocation,
129542** VFS, and mutex subsystems prior to doing any serious work with
129543** SQLite.  But as long as you do not compile with SQLITE_OMIT_AUTOINIT
129544** this routine will be called automatically by key routines such as
129545** sqlite3_open().
129546**
129547** This routine is a no-op except on its very first call for the process,
129548** or for the first call after a call to sqlite3_shutdown.
129549**
129550** The first thread to call this routine runs the initialization to
129551** completion.  If subsequent threads call this routine before the first
129552** thread has finished the initialization process, then the subsequent
129553** threads must block until the first thread finishes with the initialization.
129554**
129555** The first thread might call this routine recursively.  Recursive
129556** calls to this routine should not block, of course.  Otherwise the
129557** initialization process would never complete.
129558**
129559** Let X be the first thread to enter this routine.  Let Y be some other
129560** thread.  Then while the initial invocation of this routine by X is
129561** incomplete, it is required that:
129562**
129563**    *  Calls to this routine from Y must block until the outer-most
129564**       call by X completes.
129565**
129566**    *  Recursive calls to this routine from thread X return immediately
129567**       without blocking.
129568*/
129569SQLITE_API int SQLITE_STDCALL sqlite3_initialize(void){
129570  MUTEX_LOGIC( sqlite3_mutex *pMaster; )       /* The main static mutex */
129571  int rc;                                      /* Result code */
129572#ifdef SQLITE_EXTRA_INIT
129573  int bRunExtraInit = 0;                       /* Extra initialization needed */
129574#endif
129575
129576#ifdef SQLITE_OMIT_WSD
129577  rc = sqlite3_wsd_init(4096, 24);
129578  if( rc!=SQLITE_OK ){
129579    return rc;
129580  }
129581#endif
129582
129583  /* If the following assert() fails on some obscure processor/compiler
129584  ** combination, the work-around is to set the correct pointer
129585  ** size at compile-time using -DSQLITE_PTRSIZE=n compile-time option */
129586  assert( SQLITE_PTRSIZE==sizeof(char*) );
129587
129588  /* If SQLite is already completely initialized, then this call
129589  ** to sqlite3_initialize() should be a no-op.  But the initialization
129590  ** must be complete.  So isInit must not be set until the very end
129591  ** of this routine.
129592  */
129593  if( sqlite3GlobalConfig.isInit ) return SQLITE_OK;
129594
129595  /* Make sure the mutex subsystem is initialized.  If unable to
129596  ** initialize the mutex subsystem, return early with the error.
129597  ** If the system is so sick that we are unable to allocate a mutex,
129598  ** there is not much SQLite is going to be able to do.
129599  **
129600  ** The mutex subsystem must take care of serializing its own
129601  ** initialization.
129602  */
129603  rc = sqlite3MutexInit();
129604  if( rc ) return rc;
129605
129606  /* Initialize the malloc() system and the recursive pInitMutex mutex.
129607  ** This operation is protected by the STATIC_MASTER mutex.  Note that
129608  ** MutexAlloc() is called for a static mutex prior to initializing the
129609  ** malloc subsystem - this implies that the allocation of a static
129610  ** mutex must not require support from the malloc subsystem.
129611  */
129612  MUTEX_LOGIC( pMaster = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER); )
129613  sqlite3_mutex_enter(pMaster);
129614  sqlite3GlobalConfig.isMutexInit = 1;
129615  if( !sqlite3GlobalConfig.isMallocInit ){
129616    rc = sqlite3MallocInit();
129617  }
129618  if( rc==SQLITE_OK ){
129619    sqlite3GlobalConfig.isMallocInit = 1;
129620    if( !sqlite3GlobalConfig.pInitMutex ){
129621      sqlite3GlobalConfig.pInitMutex =
129622           sqlite3MutexAlloc(SQLITE_MUTEX_RECURSIVE);
129623      if( sqlite3GlobalConfig.bCoreMutex && !sqlite3GlobalConfig.pInitMutex ){
129624        rc = SQLITE_NOMEM;
129625      }
129626    }
129627  }
129628  if( rc==SQLITE_OK ){
129629    sqlite3GlobalConfig.nRefInitMutex++;
129630  }
129631  sqlite3_mutex_leave(pMaster);
129632
129633  /* If rc is not SQLITE_OK at this point, then either the malloc
129634  ** subsystem could not be initialized or the system failed to allocate
129635  ** the pInitMutex mutex. Return an error in either case.  */
129636  if( rc!=SQLITE_OK ){
129637    return rc;
129638  }
129639
129640  /* Do the rest of the initialization under the recursive mutex so
129641  ** that we will be able to handle recursive calls into
129642  ** sqlite3_initialize().  The recursive calls normally come through
129643  ** sqlite3_os_init() when it invokes sqlite3_vfs_register(), but other
129644  ** recursive calls might also be possible.
129645  **
129646  ** IMPLEMENTATION-OF: R-00140-37445 SQLite automatically serializes calls
129647  ** to the xInit method, so the xInit method need not be threadsafe.
129648  **
129649  ** The following mutex is what serializes access to the appdef pcache xInit
129650  ** methods.  The sqlite3_pcache_methods.xInit() all is embedded in the
129651  ** call to sqlite3PcacheInitialize().
129652  */
129653  sqlite3_mutex_enter(sqlite3GlobalConfig.pInitMutex);
129654  if( sqlite3GlobalConfig.isInit==0 && sqlite3GlobalConfig.inProgress==0 ){
129655    FuncDefHash *pHash = &GLOBAL(FuncDefHash, sqlite3GlobalFunctions);
129656    sqlite3GlobalConfig.inProgress = 1;
129657    memset(pHash, 0, sizeof(sqlite3GlobalFunctions));
129658    sqlite3RegisterGlobalFunctions();
129659    if( sqlite3GlobalConfig.isPCacheInit==0 ){
129660      rc = sqlite3PcacheInitialize();
129661    }
129662    if( rc==SQLITE_OK ){
129663      sqlite3GlobalConfig.isPCacheInit = 1;
129664      rc = sqlite3OsInit();
129665    }
129666    if( rc==SQLITE_OK ){
129667      sqlite3PCacheBufferSetup( sqlite3GlobalConfig.pPage,
129668          sqlite3GlobalConfig.szPage, sqlite3GlobalConfig.nPage);
129669      sqlite3GlobalConfig.isInit = 1;
129670#ifdef SQLITE_EXTRA_INIT
129671      bRunExtraInit = 1;
129672#endif
129673    }
129674    sqlite3GlobalConfig.inProgress = 0;
129675  }
129676  sqlite3_mutex_leave(sqlite3GlobalConfig.pInitMutex);
129677
129678  /* Go back under the static mutex and clean up the recursive
129679  ** mutex to prevent a resource leak.
129680  */
129681  sqlite3_mutex_enter(pMaster);
129682  sqlite3GlobalConfig.nRefInitMutex--;
129683  if( sqlite3GlobalConfig.nRefInitMutex<=0 ){
129684    assert( sqlite3GlobalConfig.nRefInitMutex==0 );
129685    sqlite3_mutex_free(sqlite3GlobalConfig.pInitMutex);
129686    sqlite3GlobalConfig.pInitMutex = 0;
129687  }
129688  sqlite3_mutex_leave(pMaster);
129689
129690  /* The following is just a sanity check to make sure SQLite has
129691  ** been compiled correctly.  It is important to run this code, but
129692  ** we don't want to run it too often and soak up CPU cycles for no
129693  ** reason.  So we run it once during initialization.
129694  */
129695#ifndef NDEBUG
129696#ifndef SQLITE_OMIT_FLOATING_POINT
129697  /* This section of code's only "output" is via assert() statements. */
129698  if ( rc==SQLITE_OK ){
129699    u64 x = (((u64)1)<<63)-1;
129700    double y;
129701    assert(sizeof(x)==8);
129702    assert(sizeof(x)==sizeof(y));
129703    memcpy(&y, &x, 8);
129704    assert( sqlite3IsNaN(y) );
129705  }
129706#endif
129707#endif
129708
129709  /* Do extra initialization steps requested by the SQLITE_EXTRA_INIT
129710  ** compile-time option.
129711  */
129712#ifdef SQLITE_EXTRA_INIT
129713  if( bRunExtraInit ){
129714    int SQLITE_EXTRA_INIT(const char*);
129715    rc = SQLITE_EXTRA_INIT(0);
129716  }
129717#endif
129718
129719  return rc;
129720}
129721
129722/*
129723** Undo the effects of sqlite3_initialize().  Must not be called while
129724** there are outstanding database connections or memory allocations or
129725** while any part of SQLite is otherwise in use in any thread.  This
129726** routine is not threadsafe.  But it is safe to invoke this routine
129727** on when SQLite is already shut down.  If SQLite is already shut down
129728** when this routine is invoked, then this routine is a harmless no-op.
129729*/
129730SQLITE_API int SQLITE_STDCALL sqlite3_shutdown(void){
129731#ifdef SQLITE_OMIT_WSD
129732  int rc = sqlite3_wsd_init(4096, 24);
129733  if( rc!=SQLITE_OK ){
129734    return rc;
129735  }
129736#endif
129737
129738  if( sqlite3GlobalConfig.isInit ){
129739#ifdef SQLITE_EXTRA_SHUTDOWN
129740    void SQLITE_EXTRA_SHUTDOWN(void);
129741    SQLITE_EXTRA_SHUTDOWN();
129742#endif
129743    sqlite3_os_end();
129744    sqlite3_reset_auto_extension();
129745    sqlite3GlobalConfig.isInit = 0;
129746  }
129747  if( sqlite3GlobalConfig.isPCacheInit ){
129748    sqlite3PcacheShutdown();
129749    sqlite3GlobalConfig.isPCacheInit = 0;
129750  }
129751  if( sqlite3GlobalConfig.isMallocInit ){
129752    sqlite3MallocEnd();
129753    sqlite3GlobalConfig.isMallocInit = 0;
129754
129755#ifndef SQLITE_OMIT_SHUTDOWN_DIRECTORIES
129756    /* The heap subsystem has now been shutdown and these values are supposed
129757    ** to be NULL or point to memory that was obtained from sqlite3_malloc(),
129758    ** which would rely on that heap subsystem; therefore, make sure these
129759    ** values cannot refer to heap memory that was just invalidated when the
129760    ** heap subsystem was shutdown.  This is only done if the current call to
129761    ** this function resulted in the heap subsystem actually being shutdown.
129762    */
129763    sqlite3_data_directory = 0;
129764    sqlite3_temp_directory = 0;
129765#endif
129766  }
129767  if( sqlite3GlobalConfig.isMutexInit ){
129768    sqlite3MutexEnd();
129769    sqlite3GlobalConfig.isMutexInit = 0;
129770  }
129771
129772  return SQLITE_OK;
129773}
129774
129775/*
129776** This API allows applications to modify the global configuration of
129777** the SQLite library at run-time.
129778**
129779** This routine should only be called when there are no outstanding
129780** database connections or memory allocations.  This routine is not
129781** threadsafe.  Failure to heed these warnings can lead to unpredictable
129782** behavior.
129783*/
129784SQLITE_API int SQLITE_CDECL sqlite3_config(int op, ...){
129785  va_list ap;
129786  int rc = SQLITE_OK;
129787
129788  /* sqlite3_config() shall return SQLITE_MISUSE if it is invoked while
129789  ** the SQLite library is in use. */
129790  if( sqlite3GlobalConfig.isInit ) return SQLITE_MISUSE_BKPT;
129791
129792  va_start(ap, op);
129793  switch( op ){
129794
129795    /* Mutex configuration options are only available in a threadsafe
129796    ** compile.
129797    */
129798#if defined(SQLITE_THREADSAFE) && SQLITE_THREADSAFE>0  /* IMP: R-54466-46756 */
129799    case SQLITE_CONFIG_SINGLETHREAD: {
129800      /* EVIDENCE-OF: R-02748-19096 This option sets the threading mode to
129801      ** Single-thread. */
129802      sqlite3GlobalConfig.bCoreMutex = 0;  /* Disable mutex on core */
129803      sqlite3GlobalConfig.bFullMutex = 0;  /* Disable mutex on connections */
129804      break;
129805    }
129806#endif
129807#if defined(SQLITE_THREADSAFE) && SQLITE_THREADSAFE>0 /* IMP: R-20520-54086 */
129808    case SQLITE_CONFIG_MULTITHREAD: {
129809      /* EVIDENCE-OF: R-14374-42468 This option sets the threading mode to
129810      ** Multi-thread. */
129811      sqlite3GlobalConfig.bCoreMutex = 1;  /* Enable mutex on core */
129812      sqlite3GlobalConfig.bFullMutex = 0;  /* Disable mutex on connections */
129813      break;
129814    }
129815#endif
129816#if defined(SQLITE_THREADSAFE) && SQLITE_THREADSAFE>0 /* IMP: R-59593-21810 */
129817    case SQLITE_CONFIG_SERIALIZED: {
129818      /* EVIDENCE-OF: R-41220-51800 This option sets the threading mode to
129819      ** Serialized. */
129820      sqlite3GlobalConfig.bCoreMutex = 1;  /* Enable mutex on core */
129821      sqlite3GlobalConfig.bFullMutex = 1;  /* Enable mutex on connections */
129822      break;
129823    }
129824#endif
129825#if defined(SQLITE_THREADSAFE) && SQLITE_THREADSAFE>0 /* IMP: R-63666-48755 */
129826    case SQLITE_CONFIG_MUTEX: {
129827      /* Specify an alternative mutex implementation */
129828      sqlite3GlobalConfig.mutex = *va_arg(ap, sqlite3_mutex_methods*);
129829      break;
129830    }
129831#endif
129832#if defined(SQLITE_THREADSAFE) && SQLITE_THREADSAFE>0 /* IMP: R-14450-37597 */
129833    case SQLITE_CONFIG_GETMUTEX: {
129834      /* Retrieve the current mutex implementation */
129835      *va_arg(ap, sqlite3_mutex_methods*) = sqlite3GlobalConfig.mutex;
129836      break;
129837    }
129838#endif
129839
129840    case SQLITE_CONFIG_MALLOC: {
129841      /* EVIDENCE-OF: R-55594-21030 The SQLITE_CONFIG_MALLOC option takes a
129842      ** single argument which is a pointer to an instance of the
129843      ** sqlite3_mem_methods structure. The argument specifies alternative
129844      ** low-level memory allocation routines to be used in place of the memory
129845      ** allocation routines built into SQLite. */
129846      sqlite3GlobalConfig.m = *va_arg(ap, sqlite3_mem_methods*);
129847      break;
129848    }
129849    case SQLITE_CONFIG_GETMALLOC: {
129850      /* EVIDENCE-OF: R-51213-46414 The SQLITE_CONFIG_GETMALLOC option takes a
129851      ** single argument which is a pointer to an instance of the
129852      ** sqlite3_mem_methods structure. The sqlite3_mem_methods structure is
129853      ** filled with the currently defined memory allocation routines. */
129854      if( sqlite3GlobalConfig.m.xMalloc==0 ) sqlite3MemSetDefault();
129855      *va_arg(ap, sqlite3_mem_methods*) = sqlite3GlobalConfig.m;
129856      break;
129857    }
129858    case SQLITE_CONFIG_MEMSTATUS: {
129859      /* EVIDENCE-OF: R-61275-35157 The SQLITE_CONFIG_MEMSTATUS option takes
129860      ** single argument of type int, interpreted as a boolean, which enables
129861      ** or disables the collection of memory allocation statistics. */
129862      sqlite3GlobalConfig.bMemstat = va_arg(ap, int);
129863      break;
129864    }
129865    case SQLITE_CONFIG_SCRATCH: {
129866      /* EVIDENCE-OF: R-08404-60887 There are three arguments to
129867      ** SQLITE_CONFIG_SCRATCH: A pointer an 8-byte aligned memory buffer from
129868      ** which the scratch allocations will be drawn, the size of each scratch
129869      ** allocation (sz), and the maximum number of scratch allocations (N). */
129870      sqlite3GlobalConfig.pScratch = va_arg(ap, void*);
129871      sqlite3GlobalConfig.szScratch = va_arg(ap, int);
129872      sqlite3GlobalConfig.nScratch = va_arg(ap, int);
129873      break;
129874    }
129875    case SQLITE_CONFIG_PAGECACHE: {
129876      /* EVIDENCE-OF: R-31408-40510 There are three arguments to
129877      ** SQLITE_CONFIG_PAGECACHE: A pointer to 8-byte aligned memory, the size
129878      ** of each page buffer (sz), and the number of pages (N). */
129879      sqlite3GlobalConfig.pPage = va_arg(ap, void*);
129880      sqlite3GlobalConfig.szPage = va_arg(ap, int);
129881      sqlite3GlobalConfig.nPage = va_arg(ap, int);
129882      break;
129883    }
129884    case SQLITE_CONFIG_PCACHE_HDRSZ: {
129885      /* EVIDENCE-OF: R-39100-27317 The SQLITE_CONFIG_PCACHE_HDRSZ option takes
129886      ** a single parameter which is a pointer to an integer and writes into
129887      ** that integer the number of extra bytes per page required for each page
129888      ** in SQLITE_CONFIG_PAGECACHE. */
129889      *va_arg(ap, int*) =
129890          sqlite3HeaderSizeBtree() +
129891          sqlite3HeaderSizePcache() +
129892          sqlite3HeaderSizePcache1();
129893      break;
129894    }
129895
129896    case SQLITE_CONFIG_PCACHE: {
129897      /* no-op */
129898      break;
129899    }
129900    case SQLITE_CONFIG_GETPCACHE: {
129901      /* now an error */
129902      rc = SQLITE_ERROR;
129903      break;
129904    }
129905
129906    case SQLITE_CONFIG_PCACHE2: {
129907      /* EVIDENCE-OF: R-63325-48378 The SQLITE_CONFIG_PCACHE2 option takes a
129908      ** single argument which is a pointer to an sqlite3_pcache_methods2
129909      ** object. This object specifies the interface to a custom page cache
129910      ** implementation. */
129911      sqlite3GlobalConfig.pcache2 = *va_arg(ap, sqlite3_pcache_methods2*);
129912      break;
129913    }
129914    case SQLITE_CONFIG_GETPCACHE2: {
129915      /* EVIDENCE-OF: R-22035-46182 The SQLITE_CONFIG_GETPCACHE2 option takes a
129916      ** single argument which is a pointer to an sqlite3_pcache_methods2
129917      ** object. SQLite copies of the current page cache implementation into
129918      ** that object. */
129919      if( sqlite3GlobalConfig.pcache2.xInit==0 ){
129920        sqlite3PCacheSetDefault();
129921      }
129922      *va_arg(ap, sqlite3_pcache_methods2*) = sqlite3GlobalConfig.pcache2;
129923      break;
129924    }
129925
129926/* EVIDENCE-OF: R-06626-12911 The SQLITE_CONFIG_HEAP option is only
129927** available if SQLite is compiled with either SQLITE_ENABLE_MEMSYS3 or
129928** SQLITE_ENABLE_MEMSYS5 and returns SQLITE_ERROR if invoked otherwise. */
129929#if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5)
129930    case SQLITE_CONFIG_HEAP: {
129931      /* EVIDENCE-OF: R-19854-42126 There are three arguments to
129932      ** SQLITE_CONFIG_HEAP: An 8-byte aligned pointer to the memory, the
129933      ** number of bytes in the memory buffer, and the minimum allocation size.
129934      */
129935      sqlite3GlobalConfig.pHeap = va_arg(ap, void*);
129936      sqlite3GlobalConfig.nHeap = va_arg(ap, int);
129937      sqlite3GlobalConfig.mnReq = va_arg(ap, int);
129938
129939      if( sqlite3GlobalConfig.mnReq<1 ){
129940        sqlite3GlobalConfig.mnReq = 1;
129941      }else if( sqlite3GlobalConfig.mnReq>(1<<12) ){
129942        /* cap min request size at 2^12 */
129943        sqlite3GlobalConfig.mnReq = (1<<12);
129944      }
129945
129946      if( sqlite3GlobalConfig.pHeap==0 ){
129947        /* EVIDENCE-OF: R-49920-60189 If the first pointer (the memory pointer)
129948        ** is NULL, then SQLite reverts to using its default memory allocator
129949        ** (the system malloc() implementation), undoing any prior invocation of
129950        ** SQLITE_CONFIG_MALLOC.
129951        **
129952        ** Setting sqlite3GlobalConfig.m to all zeros will cause malloc to
129953        ** revert to its default implementation when sqlite3_initialize() is run
129954        */
129955        memset(&sqlite3GlobalConfig.m, 0, sizeof(sqlite3GlobalConfig.m));
129956      }else{
129957        /* EVIDENCE-OF: R-61006-08918 If the memory pointer is not NULL then the
129958        ** alternative memory allocator is engaged to handle all of SQLites
129959        ** memory allocation needs. */
129960#ifdef SQLITE_ENABLE_MEMSYS3
129961        sqlite3GlobalConfig.m = *sqlite3MemGetMemsys3();
129962#endif
129963#ifdef SQLITE_ENABLE_MEMSYS5
129964        sqlite3GlobalConfig.m = *sqlite3MemGetMemsys5();
129965#endif
129966      }
129967      break;
129968    }
129969#endif
129970
129971    case SQLITE_CONFIG_LOOKASIDE: {
129972      sqlite3GlobalConfig.szLookaside = va_arg(ap, int);
129973      sqlite3GlobalConfig.nLookaside = va_arg(ap, int);
129974      break;
129975    }
129976
129977    /* Record a pointer to the logger function and its first argument.
129978    ** The default is NULL.  Logging is disabled if the function pointer is
129979    ** NULL.
129980    */
129981    case SQLITE_CONFIG_LOG: {
129982      /* MSVC is picky about pulling func ptrs from va lists.
129983      ** http://support.microsoft.com/kb/47961
129984      ** sqlite3GlobalConfig.xLog = va_arg(ap, void(*)(void*,int,const char*));
129985      */
129986      typedef void(*LOGFUNC_t)(void*,int,const char*);
129987      sqlite3GlobalConfig.xLog = va_arg(ap, LOGFUNC_t);
129988      sqlite3GlobalConfig.pLogArg = va_arg(ap, void*);
129989      break;
129990    }
129991
129992    /* EVIDENCE-OF: R-55548-33817 The compile-time setting for URI filenames
129993    ** can be changed at start-time using the
129994    ** sqlite3_config(SQLITE_CONFIG_URI,1) or
129995    ** sqlite3_config(SQLITE_CONFIG_URI,0) configuration calls.
129996    */
129997    case SQLITE_CONFIG_URI: {
129998      /* EVIDENCE-OF: R-25451-61125 The SQLITE_CONFIG_URI option takes a single
129999      ** argument of type int. If non-zero, then URI handling is globally
130000      ** enabled. If the parameter is zero, then URI handling is globally
130001      ** disabled. */
130002      sqlite3GlobalConfig.bOpenUri = va_arg(ap, int);
130003      break;
130004    }
130005
130006    case SQLITE_CONFIG_COVERING_INDEX_SCAN: {
130007      /* EVIDENCE-OF: R-36592-02772 The SQLITE_CONFIG_COVERING_INDEX_SCAN
130008      ** option takes a single integer argument which is interpreted as a
130009      ** boolean in order to enable or disable the use of covering indices for
130010      ** full table scans in the query optimizer. */
130011      sqlite3GlobalConfig.bUseCis = va_arg(ap, int);
130012      break;
130013    }
130014
130015#ifdef SQLITE_ENABLE_SQLLOG
130016    case SQLITE_CONFIG_SQLLOG: {
130017      typedef void(*SQLLOGFUNC_t)(void*, sqlite3*, const char*, int);
130018      sqlite3GlobalConfig.xSqllog = va_arg(ap, SQLLOGFUNC_t);
130019      sqlite3GlobalConfig.pSqllogArg = va_arg(ap, void *);
130020      break;
130021    }
130022#endif
130023
130024    case SQLITE_CONFIG_MMAP_SIZE: {
130025      /* EVIDENCE-OF: R-58063-38258 SQLITE_CONFIG_MMAP_SIZE takes two 64-bit
130026      ** integer (sqlite3_int64) values that are the default mmap size limit
130027      ** (the default setting for PRAGMA mmap_size) and the maximum allowed
130028      ** mmap size limit. */
130029      sqlite3_int64 szMmap = va_arg(ap, sqlite3_int64);
130030      sqlite3_int64 mxMmap = va_arg(ap, sqlite3_int64);
130031      /* EVIDENCE-OF: R-53367-43190 If either argument to this option is
130032      ** negative, then that argument is changed to its compile-time default.
130033      **
130034      ** EVIDENCE-OF: R-34993-45031 The maximum allowed mmap size will be
130035      ** silently truncated if necessary so that it does not exceed the
130036      ** compile-time maximum mmap size set by the SQLITE_MAX_MMAP_SIZE
130037      ** compile-time option.
130038      */
130039      if( mxMmap<0 || mxMmap>SQLITE_MAX_MMAP_SIZE ){
130040        mxMmap = SQLITE_MAX_MMAP_SIZE;
130041      }
130042      if( szMmap<0 ) szMmap = SQLITE_DEFAULT_MMAP_SIZE;
130043      if( szMmap>mxMmap) szMmap = mxMmap;
130044      sqlite3GlobalConfig.mxMmap = mxMmap;
130045      sqlite3GlobalConfig.szMmap = szMmap;
130046      break;
130047    }
130048
130049#if SQLITE_OS_WIN && defined(SQLITE_WIN32_MALLOC) /* IMP: R-04780-55815 */
130050    case SQLITE_CONFIG_WIN32_HEAPSIZE: {
130051      /* EVIDENCE-OF: R-34926-03360 SQLITE_CONFIG_WIN32_HEAPSIZE takes a 32-bit
130052      ** unsigned integer value that specifies the maximum size of the created
130053      ** heap. */
130054      sqlite3GlobalConfig.nHeap = va_arg(ap, int);
130055      break;
130056    }
130057#endif
130058
130059    case SQLITE_CONFIG_PMASZ: {
130060      sqlite3GlobalConfig.szPma = va_arg(ap, unsigned int);
130061      break;
130062    }
130063
130064    default: {
130065      rc = SQLITE_ERROR;
130066      break;
130067    }
130068  }
130069  va_end(ap);
130070  return rc;
130071}
130072
130073/*
130074** Set up the lookaside buffers for a database connection.
130075** Return SQLITE_OK on success.
130076** If lookaside is already active, return SQLITE_BUSY.
130077**
130078** The sz parameter is the number of bytes in each lookaside slot.
130079** The cnt parameter is the number of slots.  If pStart is NULL the
130080** space for the lookaside memory is obtained from sqlite3_malloc().
130081** If pStart is not NULL then it is sz*cnt bytes of memory to use for
130082** the lookaside memory.
130083*/
130084static int setupLookaside(sqlite3 *db, void *pBuf, int sz, int cnt){
130085#ifndef SQLITE_OMIT_LOOKASIDE
130086  void *pStart;
130087  if( db->lookaside.nOut ){
130088    return SQLITE_BUSY;
130089  }
130090  /* Free any existing lookaside buffer for this handle before
130091  ** allocating a new one so we don't have to have space for
130092  ** both at the same time.
130093  */
130094  if( db->lookaside.bMalloced ){
130095    sqlite3_free(db->lookaside.pStart);
130096  }
130097  /* The size of a lookaside slot after ROUNDDOWN8 needs to be larger
130098  ** than a pointer to be useful.
130099  */
130100  sz = ROUNDDOWN8(sz);  /* IMP: R-33038-09382 */
130101  if( sz<=(int)sizeof(LookasideSlot*) ) sz = 0;
130102  if( cnt<0 ) cnt = 0;
130103  if( sz==0 || cnt==0 ){
130104    sz = 0;
130105    pStart = 0;
130106  }else if( pBuf==0 ){
130107    sqlite3BeginBenignMalloc();
130108    pStart = sqlite3Malloc( sz*cnt );  /* IMP: R-61949-35727 */
130109    sqlite3EndBenignMalloc();
130110    if( pStart ) cnt = sqlite3MallocSize(pStart)/sz;
130111  }else{
130112    pStart = pBuf;
130113  }
130114  db->lookaside.pStart = pStart;
130115  db->lookaside.pFree = 0;
130116  db->lookaside.sz = (u16)sz;
130117  if( pStart ){
130118    int i;
130119    LookasideSlot *p;
130120    assert( sz > (int)sizeof(LookasideSlot*) );
130121    p = (LookasideSlot*)pStart;
130122    for(i=cnt-1; i>=0; i--){
130123      p->pNext = db->lookaside.pFree;
130124      db->lookaside.pFree = p;
130125      p = (LookasideSlot*)&((u8*)p)[sz];
130126    }
130127    db->lookaside.pEnd = p;
130128    db->lookaside.bEnabled = 1;
130129    db->lookaside.bMalloced = pBuf==0 ?1:0;
130130  }else{
130131    db->lookaside.pStart = db;
130132    db->lookaside.pEnd = db;
130133    db->lookaside.bEnabled = 0;
130134    db->lookaside.bMalloced = 0;
130135  }
130136#endif /* SQLITE_OMIT_LOOKASIDE */
130137  return SQLITE_OK;
130138}
130139
130140/*
130141** Return the mutex associated with a database connection.
130142*/
130143SQLITE_API sqlite3_mutex *SQLITE_STDCALL sqlite3_db_mutex(sqlite3 *db){
130144#ifdef SQLITE_ENABLE_API_ARMOR
130145  if( !sqlite3SafetyCheckOk(db) ){
130146    (void)SQLITE_MISUSE_BKPT;
130147    return 0;
130148  }
130149#endif
130150  return db->mutex;
130151}
130152
130153/*
130154** Free up as much memory as we can from the given database
130155** connection.
130156*/
130157SQLITE_API int SQLITE_STDCALL sqlite3_db_release_memory(sqlite3 *db){
130158  int i;
130159
130160#ifdef SQLITE_ENABLE_API_ARMOR
130161  if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT;
130162#endif
130163  sqlite3_mutex_enter(db->mutex);
130164  sqlite3BtreeEnterAll(db);
130165  for(i=0; i<db->nDb; i++){
130166    Btree *pBt = db->aDb[i].pBt;
130167    if( pBt ){
130168      Pager *pPager = sqlite3BtreePager(pBt);
130169      sqlite3PagerShrink(pPager);
130170    }
130171  }
130172  sqlite3BtreeLeaveAll(db);
130173  sqlite3_mutex_leave(db->mutex);
130174  return SQLITE_OK;
130175}
130176
130177/*
130178** Configuration settings for an individual database connection
130179*/
130180SQLITE_API int SQLITE_CDECL sqlite3_db_config(sqlite3 *db, int op, ...){
130181  va_list ap;
130182  int rc;
130183  va_start(ap, op);
130184  switch( op ){
130185    case SQLITE_DBCONFIG_LOOKASIDE: {
130186      void *pBuf = va_arg(ap, void*); /* IMP: R-26835-10964 */
130187      int sz = va_arg(ap, int);       /* IMP: R-47871-25994 */
130188      int cnt = va_arg(ap, int);      /* IMP: R-04460-53386 */
130189      rc = setupLookaside(db, pBuf, sz, cnt);
130190      break;
130191    }
130192    default: {
130193      static const struct {
130194        int op;      /* The opcode */
130195        u32 mask;    /* Mask of the bit in sqlite3.flags to set/clear */
130196      } aFlagOp[] = {
130197        { SQLITE_DBCONFIG_ENABLE_FKEY,    SQLITE_ForeignKeys    },
130198        { SQLITE_DBCONFIG_ENABLE_TRIGGER, SQLITE_EnableTrigger  },
130199      };
130200      unsigned int i;
130201      rc = SQLITE_ERROR; /* IMP: R-42790-23372 */
130202      for(i=0; i<ArraySize(aFlagOp); i++){
130203        if( aFlagOp[i].op==op ){
130204          int onoff = va_arg(ap, int);
130205          int *pRes = va_arg(ap, int*);
130206          int oldFlags = db->flags;
130207          if( onoff>0 ){
130208            db->flags |= aFlagOp[i].mask;
130209          }else if( onoff==0 ){
130210            db->flags &= ~aFlagOp[i].mask;
130211          }
130212          if( oldFlags!=db->flags ){
130213            sqlite3ExpirePreparedStatements(db);
130214          }
130215          if( pRes ){
130216            *pRes = (db->flags & aFlagOp[i].mask)!=0;
130217          }
130218          rc = SQLITE_OK;
130219          break;
130220        }
130221      }
130222      break;
130223    }
130224  }
130225  va_end(ap);
130226  return rc;
130227}
130228
130229
130230/*
130231** Return true if the buffer z[0..n-1] contains all spaces.
130232*/
130233static int allSpaces(const char *z, int n){
130234  while( n>0 && z[n-1]==' ' ){ n--; }
130235  return n==0;
130236}
130237
130238/*
130239** This is the default collating function named "BINARY" which is always
130240** available.
130241**
130242** If the padFlag argument is not NULL then space padding at the end
130243** of strings is ignored.  This implements the RTRIM collation.
130244*/
130245static int binCollFunc(
130246  void *padFlag,
130247  int nKey1, const void *pKey1,
130248  int nKey2, const void *pKey2
130249){
130250  int rc, n;
130251  n = nKey1<nKey2 ? nKey1 : nKey2;
130252  /* EVIDENCE-OF: R-65033-28449 The built-in BINARY collation compares
130253  ** strings byte by byte using the memcmp() function from the standard C
130254  ** library. */
130255  rc = memcmp(pKey1, pKey2, n);
130256  if( rc==0 ){
130257    if( padFlag
130258     && allSpaces(((char*)pKey1)+n, nKey1-n)
130259     && allSpaces(((char*)pKey2)+n, nKey2-n)
130260    ){
130261      /* EVIDENCE-OF: R-31624-24737 RTRIM is like BINARY except that extra
130262      ** spaces at the end of either string do not change the result. In other
130263      ** words, strings will compare equal to one another as long as they
130264      ** differ only in the number of spaces at the end.
130265      */
130266    }else{
130267      rc = nKey1 - nKey2;
130268    }
130269  }
130270  return rc;
130271}
130272
130273/*
130274** Another built-in collating sequence: NOCASE.
130275**
130276** This collating sequence is intended to be used for "case independent
130277** comparison". SQLite's knowledge of upper and lower case equivalents
130278** extends only to the 26 characters used in the English language.
130279**
130280** At the moment there is only a UTF-8 implementation.
130281*/
130282static int nocaseCollatingFunc(
130283  void *NotUsed,
130284  int nKey1, const void *pKey1,
130285  int nKey2, const void *pKey2
130286){
130287  int r = sqlite3StrNICmp(
130288      (const char *)pKey1, (const char *)pKey2, (nKey1<nKey2)?nKey1:nKey2);
130289  UNUSED_PARAMETER(NotUsed);
130290  if( 0==r ){
130291    r = nKey1-nKey2;
130292  }
130293  return r;
130294}
130295
130296/*
130297** Return the ROWID of the most recent insert
130298*/
130299SQLITE_API sqlite_int64 SQLITE_STDCALL sqlite3_last_insert_rowid(sqlite3 *db){
130300#ifdef SQLITE_ENABLE_API_ARMOR
130301  if( !sqlite3SafetyCheckOk(db) ){
130302    (void)SQLITE_MISUSE_BKPT;
130303    return 0;
130304  }
130305#endif
130306  return db->lastRowid;
130307}
130308
130309/*
130310** Return the number of changes in the most recent call to sqlite3_exec().
130311*/
130312SQLITE_API int SQLITE_STDCALL sqlite3_changes(sqlite3 *db){
130313#ifdef SQLITE_ENABLE_API_ARMOR
130314  if( !sqlite3SafetyCheckOk(db) ){
130315    (void)SQLITE_MISUSE_BKPT;
130316    return 0;
130317  }
130318#endif
130319  return db->nChange;
130320}
130321
130322/*
130323** Return the number of changes since the database handle was opened.
130324*/
130325SQLITE_API int SQLITE_STDCALL sqlite3_total_changes(sqlite3 *db){
130326#ifdef SQLITE_ENABLE_API_ARMOR
130327  if( !sqlite3SafetyCheckOk(db) ){
130328    (void)SQLITE_MISUSE_BKPT;
130329    return 0;
130330  }
130331#endif
130332  return db->nTotalChange;
130333}
130334
130335/*
130336** Close all open savepoints. This function only manipulates fields of the
130337** database handle object, it does not close any savepoints that may be open
130338** at the b-tree/pager level.
130339*/
130340SQLITE_PRIVATE void sqlite3CloseSavepoints(sqlite3 *db){
130341  while( db->pSavepoint ){
130342    Savepoint *pTmp = db->pSavepoint;
130343    db->pSavepoint = pTmp->pNext;
130344    sqlite3DbFree(db, pTmp);
130345  }
130346  db->nSavepoint = 0;
130347  db->nStatement = 0;
130348  db->isTransactionSavepoint = 0;
130349}
130350
130351/*
130352** Invoke the destructor function associated with FuncDef p, if any. Except,
130353** if this is not the last copy of the function, do not invoke it. Multiple
130354** copies of a single function are created when create_function() is called
130355** with SQLITE_ANY as the encoding.
130356*/
130357static void functionDestroy(sqlite3 *db, FuncDef *p){
130358  FuncDestructor *pDestructor = p->pDestructor;
130359  if( pDestructor ){
130360    pDestructor->nRef--;
130361    if( pDestructor->nRef==0 ){
130362      pDestructor->xDestroy(pDestructor->pUserData);
130363      sqlite3DbFree(db, pDestructor);
130364    }
130365  }
130366}
130367
130368/*
130369** Disconnect all sqlite3_vtab objects that belong to database connection
130370** db. This is called when db is being closed.
130371*/
130372static void disconnectAllVtab(sqlite3 *db){
130373#ifndef SQLITE_OMIT_VIRTUALTABLE
130374  int i;
130375  sqlite3BtreeEnterAll(db);
130376  for(i=0; i<db->nDb; i++){
130377    Schema *pSchema = db->aDb[i].pSchema;
130378    if( db->aDb[i].pSchema ){
130379      HashElem *p;
130380      for(p=sqliteHashFirst(&pSchema->tblHash); p; p=sqliteHashNext(p)){
130381        Table *pTab = (Table *)sqliteHashData(p);
130382        if( IsVirtual(pTab) ) sqlite3VtabDisconnect(db, pTab);
130383      }
130384    }
130385  }
130386  sqlite3VtabUnlockList(db);
130387  sqlite3BtreeLeaveAll(db);
130388#else
130389  UNUSED_PARAMETER(db);
130390#endif
130391}
130392
130393/*
130394** Return TRUE if database connection db has unfinalized prepared
130395** statements or unfinished sqlite3_backup objects.
130396*/
130397static int connectionIsBusy(sqlite3 *db){
130398  int j;
130399  assert( sqlite3_mutex_held(db->mutex) );
130400  if( db->pVdbe ) return 1;
130401  for(j=0; j<db->nDb; j++){
130402    Btree *pBt = db->aDb[j].pBt;
130403    if( pBt && sqlite3BtreeIsInBackup(pBt) ) return 1;
130404  }
130405  return 0;
130406}
130407
130408/*
130409** Close an existing SQLite database
130410*/
130411static int sqlite3Close(sqlite3 *db, int forceZombie){
130412  if( !db ){
130413    /* EVIDENCE-OF: R-63257-11740 Calling sqlite3_close() or
130414    ** sqlite3_close_v2() with a NULL pointer argument is a harmless no-op. */
130415    return SQLITE_OK;
130416  }
130417  if( !sqlite3SafetyCheckSickOrOk(db) ){
130418    return SQLITE_MISUSE_BKPT;
130419  }
130420  sqlite3_mutex_enter(db->mutex);
130421
130422  /* Force xDisconnect calls on all virtual tables */
130423  disconnectAllVtab(db);
130424
130425  /* If a transaction is open, the disconnectAllVtab() call above
130426  ** will not have called the xDisconnect() method on any virtual
130427  ** tables in the db->aVTrans[] array. The following sqlite3VtabRollback()
130428  ** call will do so. We need to do this before the check for active
130429  ** SQL statements below, as the v-table implementation may be storing
130430  ** some prepared statements internally.
130431  */
130432  sqlite3VtabRollback(db);
130433
130434  /* Legacy behavior (sqlite3_close() behavior) is to return
130435  ** SQLITE_BUSY if the connection can not be closed immediately.
130436  */
130437  if( !forceZombie && connectionIsBusy(db) ){
130438    sqlite3ErrorWithMsg(db, SQLITE_BUSY, "unable to close due to unfinalized "
130439       "statements or unfinished backups");
130440    sqlite3_mutex_leave(db->mutex);
130441    return SQLITE_BUSY;
130442  }
130443
130444#ifdef SQLITE_ENABLE_SQLLOG
130445  if( sqlite3GlobalConfig.xSqllog ){
130446    /* Closing the handle. Fourth parameter is passed the value 2. */
130447    sqlite3GlobalConfig.xSqllog(sqlite3GlobalConfig.pSqllogArg, db, 0, 2);
130448  }
130449#endif
130450
130451  /* Convert the connection into a zombie and then close it.
130452  */
130453  db->magic = SQLITE_MAGIC_ZOMBIE;
130454  sqlite3LeaveMutexAndCloseZombie(db);
130455  return SQLITE_OK;
130456}
130457
130458/*
130459** Two variations on the public interface for closing a database
130460** connection. The sqlite3_close() version returns SQLITE_BUSY and
130461** leaves the connection option if there are unfinalized prepared
130462** statements or unfinished sqlite3_backups.  The sqlite3_close_v2()
130463** version forces the connection to become a zombie if there are
130464** unclosed resources, and arranges for deallocation when the last
130465** prepare statement or sqlite3_backup closes.
130466*/
130467SQLITE_API int SQLITE_STDCALL sqlite3_close(sqlite3 *db){ return sqlite3Close(db,0); }
130468SQLITE_API int SQLITE_STDCALL sqlite3_close_v2(sqlite3 *db){ return sqlite3Close(db,1); }
130469
130470
130471/*
130472** Close the mutex on database connection db.
130473**
130474** Furthermore, if database connection db is a zombie (meaning that there
130475** has been a prior call to sqlite3_close(db) or sqlite3_close_v2(db)) and
130476** every sqlite3_stmt has now been finalized and every sqlite3_backup has
130477** finished, then free all resources.
130478*/
130479SQLITE_PRIVATE void sqlite3LeaveMutexAndCloseZombie(sqlite3 *db){
130480  HashElem *i;                    /* Hash table iterator */
130481  int j;
130482
130483  /* If there are outstanding sqlite3_stmt or sqlite3_backup objects
130484  ** or if the connection has not yet been closed by sqlite3_close_v2(),
130485  ** then just leave the mutex and return.
130486  */
130487  if( db->magic!=SQLITE_MAGIC_ZOMBIE || connectionIsBusy(db) ){
130488    sqlite3_mutex_leave(db->mutex);
130489    return;
130490  }
130491
130492  /* If we reach this point, it means that the database connection has
130493  ** closed all sqlite3_stmt and sqlite3_backup objects and has been
130494  ** passed to sqlite3_close (meaning that it is a zombie).  Therefore,
130495  ** go ahead and free all resources.
130496  */
130497
130498  /* If a transaction is open, roll it back. This also ensures that if
130499  ** any database schemas have been modified by an uncommitted transaction
130500  ** they are reset. And that the required b-tree mutex is held to make
130501  ** the pager rollback and schema reset an atomic operation. */
130502  sqlite3RollbackAll(db, SQLITE_OK);
130503
130504  /* Free any outstanding Savepoint structures. */
130505  sqlite3CloseSavepoints(db);
130506
130507  /* Close all database connections */
130508  for(j=0; j<db->nDb; j++){
130509    struct Db *pDb = &db->aDb[j];
130510    if( pDb->pBt ){
130511      sqlite3BtreeClose(pDb->pBt);
130512      pDb->pBt = 0;
130513      if( j!=1 ){
130514        pDb->pSchema = 0;
130515      }
130516    }
130517  }
130518  /* Clear the TEMP schema separately and last */
130519  if( db->aDb[1].pSchema ){
130520    sqlite3SchemaClear(db->aDb[1].pSchema);
130521  }
130522  sqlite3VtabUnlockList(db);
130523
130524  /* Free up the array of auxiliary databases */
130525  sqlite3CollapseDatabaseArray(db);
130526  assert( db->nDb<=2 );
130527  assert( db->aDb==db->aDbStatic );
130528
130529  /* Tell the code in notify.c that the connection no longer holds any
130530  ** locks and does not require any further unlock-notify callbacks.
130531  */
130532  sqlite3ConnectionClosed(db);
130533
130534  for(j=0; j<ArraySize(db->aFunc.a); j++){
130535    FuncDef *pNext, *pHash, *p;
130536    for(p=db->aFunc.a[j]; p; p=pHash){
130537      pHash = p->pHash;
130538      while( p ){
130539        functionDestroy(db, p);
130540        pNext = p->pNext;
130541        sqlite3DbFree(db, p);
130542        p = pNext;
130543      }
130544    }
130545  }
130546  for(i=sqliteHashFirst(&db->aCollSeq); i; i=sqliteHashNext(i)){
130547    CollSeq *pColl = (CollSeq *)sqliteHashData(i);
130548    /* Invoke any destructors registered for collation sequence user data. */
130549    for(j=0; j<3; j++){
130550      if( pColl[j].xDel ){
130551        pColl[j].xDel(pColl[j].pUser);
130552      }
130553    }
130554    sqlite3DbFree(db, pColl);
130555  }
130556  sqlite3HashClear(&db->aCollSeq);
130557#ifndef SQLITE_OMIT_VIRTUALTABLE
130558  for(i=sqliteHashFirst(&db->aModule); i; i=sqliteHashNext(i)){
130559    Module *pMod = (Module *)sqliteHashData(i);
130560    if( pMod->xDestroy ){
130561      pMod->xDestroy(pMod->pAux);
130562    }
130563    sqlite3DbFree(db, pMod);
130564  }
130565  sqlite3HashClear(&db->aModule);
130566#endif
130567
130568  sqlite3Error(db, SQLITE_OK); /* Deallocates any cached error strings. */
130569  sqlite3ValueFree(db->pErr);
130570  sqlite3CloseExtensions(db);
130571#if SQLITE_USER_AUTHENTICATION
130572  sqlite3_free(db->auth.zAuthUser);
130573  sqlite3_free(db->auth.zAuthPW);
130574#endif
130575
130576  db->magic = SQLITE_MAGIC_ERROR;
130577
130578  /* The temp-database schema is allocated differently from the other schema
130579  ** objects (using sqliteMalloc() directly, instead of sqlite3BtreeSchema()).
130580  ** So it needs to be freed here. Todo: Why not roll the temp schema into
130581  ** the same sqliteMalloc() as the one that allocates the database
130582  ** structure?
130583  */
130584  sqlite3DbFree(db, db->aDb[1].pSchema);
130585  sqlite3_mutex_leave(db->mutex);
130586  db->magic = SQLITE_MAGIC_CLOSED;
130587  sqlite3_mutex_free(db->mutex);
130588  assert( db->lookaside.nOut==0 );  /* Fails on a lookaside memory leak */
130589  if( db->lookaside.bMalloced ){
130590    sqlite3_free(db->lookaside.pStart);
130591  }
130592  sqlite3_free(db);
130593}
130594
130595/*
130596** Rollback all database files.  If tripCode is not SQLITE_OK, then
130597** any write cursors are invalidated ("tripped" - as in "tripping a circuit
130598** breaker") and made to return tripCode if there are any further
130599** attempts to use that cursor.  Read cursors remain open and valid
130600** but are "saved" in case the table pages are moved around.
130601*/
130602SQLITE_PRIVATE void sqlite3RollbackAll(sqlite3 *db, int tripCode){
130603  int i;
130604  int inTrans = 0;
130605  int schemaChange;
130606  assert( sqlite3_mutex_held(db->mutex) );
130607  sqlite3BeginBenignMalloc();
130608
130609  /* Obtain all b-tree mutexes before making any calls to BtreeRollback().
130610  ** This is important in case the transaction being rolled back has
130611  ** modified the database schema. If the b-tree mutexes are not taken
130612  ** here, then another shared-cache connection might sneak in between
130613  ** the database rollback and schema reset, which can cause false
130614  ** corruption reports in some cases.  */
130615  sqlite3BtreeEnterAll(db);
130616  schemaChange = (db->flags & SQLITE_InternChanges)!=0 && db->init.busy==0;
130617
130618  for(i=0; i<db->nDb; i++){
130619    Btree *p = db->aDb[i].pBt;
130620    if( p ){
130621      if( sqlite3BtreeIsInTrans(p) ){
130622        inTrans = 1;
130623      }
130624      sqlite3BtreeRollback(p, tripCode, !schemaChange);
130625    }
130626  }
130627  sqlite3VtabRollback(db);
130628  sqlite3EndBenignMalloc();
130629
130630  if( (db->flags&SQLITE_InternChanges)!=0 && db->init.busy==0 ){
130631    sqlite3ExpirePreparedStatements(db);
130632    sqlite3ResetAllSchemasOfConnection(db);
130633  }
130634  sqlite3BtreeLeaveAll(db);
130635
130636  /* Any deferred constraint violations have now been resolved. */
130637  db->nDeferredCons = 0;
130638  db->nDeferredImmCons = 0;
130639  db->flags &= ~SQLITE_DeferFKs;
130640
130641  /* If one has been configured, invoke the rollback-hook callback */
130642  if( db->xRollbackCallback && (inTrans || !db->autoCommit) ){
130643    db->xRollbackCallback(db->pRollbackArg);
130644  }
130645}
130646
130647/*
130648** Return a static string containing the name corresponding to the error code
130649** specified in the argument.
130650*/
130651#if defined(SQLITE_NEED_ERR_NAME)
130652SQLITE_PRIVATE const char *sqlite3ErrName(int rc){
130653  const char *zName = 0;
130654  int i, origRc = rc;
130655  for(i=0; i<2 && zName==0; i++, rc &= 0xff){
130656    switch( rc ){
130657      case SQLITE_OK:                 zName = "SQLITE_OK";                break;
130658      case SQLITE_ERROR:              zName = "SQLITE_ERROR";             break;
130659      case SQLITE_INTERNAL:           zName = "SQLITE_INTERNAL";          break;
130660      case SQLITE_PERM:               zName = "SQLITE_PERM";              break;
130661      case SQLITE_ABORT:              zName = "SQLITE_ABORT";             break;
130662      case SQLITE_ABORT_ROLLBACK:     zName = "SQLITE_ABORT_ROLLBACK";    break;
130663      case SQLITE_BUSY:               zName = "SQLITE_BUSY";              break;
130664      case SQLITE_BUSY_RECOVERY:      zName = "SQLITE_BUSY_RECOVERY";     break;
130665      case SQLITE_BUSY_SNAPSHOT:      zName = "SQLITE_BUSY_SNAPSHOT";     break;
130666      case SQLITE_LOCKED:             zName = "SQLITE_LOCKED";            break;
130667      case SQLITE_LOCKED_SHAREDCACHE: zName = "SQLITE_LOCKED_SHAREDCACHE";break;
130668      case SQLITE_NOMEM:              zName = "SQLITE_NOMEM";             break;
130669      case SQLITE_READONLY:           zName = "SQLITE_READONLY";          break;
130670      case SQLITE_READONLY_RECOVERY:  zName = "SQLITE_READONLY_RECOVERY"; break;
130671      case SQLITE_READONLY_CANTLOCK:  zName = "SQLITE_READONLY_CANTLOCK"; break;
130672      case SQLITE_READONLY_ROLLBACK:  zName = "SQLITE_READONLY_ROLLBACK"; break;
130673      case SQLITE_READONLY_DBMOVED:   zName = "SQLITE_READONLY_DBMOVED";  break;
130674      case SQLITE_INTERRUPT:          zName = "SQLITE_INTERRUPT";         break;
130675      case SQLITE_IOERR:              zName = "SQLITE_IOERR";             break;
130676      case SQLITE_IOERR_READ:         zName = "SQLITE_IOERR_READ";        break;
130677      case SQLITE_IOERR_SHORT_READ:   zName = "SQLITE_IOERR_SHORT_READ";  break;
130678      case SQLITE_IOERR_WRITE:        zName = "SQLITE_IOERR_WRITE";       break;
130679      case SQLITE_IOERR_FSYNC:        zName = "SQLITE_IOERR_FSYNC";       break;
130680      case SQLITE_IOERR_DIR_FSYNC:    zName = "SQLITE_IOERR_DIR_FSYNC";   break;
130681      case SQLITE_IOERR_TRUNCATE:     zName = "SQLITE_IOERR_TRUNCATE";    break;
130682      case SQLITE_IOERR_FSTAT:        zName = "SQLITE_IOERR_FSTAT";       break;
130683      case SQLITE_IOERR_UNLOCK:       zName = "SQLITE_IOERR_UNLOCK";      break;
130684      case SQLITE_IOERR_RDLOCK:       zName = "SQLITE_IOERR_RDLOCK";      break;
130685      case SQLITE_IOERR_DELETE:       zName = "SQLITE_IOERR_DELETE";      break;
130686      case SQLITE_IOERR_NOMEM:        zName = "SQLITE_IOERR_NOMEM";       break;
130687      case SQLITE_IOERR_ACCESS:       zName = "SQLITE_IOERR_ACCESS";      break;
130688      case SQLITE_IOERR_CHECKRESERVEDLOCK:
130689                                zName = "SQLITE_IOERR_CHECKRESERVEDLOCK"; break;
130690      case SQLITE_IOERR_LOCK:         zName = "SQLITE_IOERR_LOCK";        break;
130691      case SQLITE_IOERR_CLOSE:        zName = "SQLITE_IOERR_CLOSE";       break;
130692      case SQLITE_IOERR_DIR_CLOSE:    zName = "SQLITE_IOERR_DIR_CLOSE";   break;
130693      case SQLITE_IOERR_SHMOPEN:      zName = "SQLITE_IOERR_SHMOPEN";     break;
130694      case SQLITE_IOERR_SHMSIZE:      zName = "SQLITE_IOERR_SHMSIZE";     break;
130695      case SQLITE_IOERR_SHMLOCK:      zName = "SQLITE_IOERR_SHMLOCK";     break;
130696      case SQLITE_IOERR_SHMMAP:       zName = "SQLITE_IOERR_SHMMAP";      break;
130697      case SQLITE_IOERR_SEEK:         zName = "SQLITE_IOERR_SEEK";        break;
130698      case SQLITE_IOERR_DELETE_NOENT: zName = "SQLITE_IOERR_DELETE_NOENT";break;
130699      case SQLITE_IOERR_MMAP:         zName = "SQLITE_IOERR_MMAP";        break;
130700      case SQLITE_IOERR_GETTEMPPATH:  zName = "SQLITE_IOERR_GETTEMPPATH"; break;
130701      case SQLITE_IOERR_CONVPATH:     zName = "SQLITE_IOERR_CONVPATH";    break;
130702      case SQLITE_CORRUPT:            zName = "SQLITE_CORRUPT";           break;
130703      case SQLITE_CORRUPT_VTAB:       zName = "SQLITE_CORRUPT_VTAB";      break;
130704      case SQLITE_NOTFOUND:           zName = "SQLITE_NOTFOUND";          break;
130705      case SQLITE_FULL:               zName = "SQLITE_FULL";              break;
130706      case SQLITE_CANTOPEN:           zName = "SQLITE_CANTOPEN";          break;
130707      case SQLITE_CANTOPEN_NOTEMPDIR: zName = "SQLITE_CANTOPEN_NOTEMPDIR";break;
130708      case SQLITE_CANTOPEN_ISDIR:     zName = "SQLITE_CANTOPEN_ISDIR";    break;
130709      case SQLITE_CANTOPEN_FULLPATH:  zName = "SQLITE_CANTOPEN_FULLPATH"; break;
130710      case SQLITE_CANTOPEN_CONVPATH:  zName = "SQLITE_CANTOPEN_CONVPATH"; break;
130711      case SQLITE_PROTOCOL:           zName = "SQLITE_PROTOCOL";          break;
130712      case SQLITE_EMPTY:              zName = "SQLITE_EMPTY";             break;
130713      case SQLITE_SCHEMA:             zName = "SQLITE_SCHEMA";            break;
130714      case SQLITE_TOOBIG:             zName = "SQLITE_TOOBIG";            break;
130715      case SQLITE_CONSTRAINT:         zName = "SQLITE_CONSTRAINT";        break;
130716      case SQLITE_CONSTRAINT_UNIQUE:  zName = "SQLITE_CONSTRAINT_UNIQUE"; break;
130717      case SQLITE_CONSTRAINT_TRIGGER: zName = "SQLITE_CONSTRAINT_TRIGGER";break;
130718      case SQLITE_CONSTRAINT_FOREIGNKEY:
130719                                zName = "SQLITE_CONSTRAINT_FOREIGNKEY";   break;
130720      case SQLITE_CONSTRAINT_CHECK:   zName = "SQLITE_CONSTRAINT_CHECK";  break;
130721      case SQLITE_CONSTRAINT_PRIMARYKEY:
130722                                zName = "SQLITE_CONSTRAINT_PRIMARYKEY";   break;
130723      case SQLITE_CONSTRAINT_NOTNULL: zName = "SQLITE_CONSTRAINT_NOTNULL";break;
130724      case SQLITE_CONSTRAINT_COMMITHOOK:
130725                                zName = "SQLITE_CONSTRAINT_COMMITHOOK";   break;
130726      case SQLITE_CONSTRAINT_VTAB:    zName = "SQLITE_CONSTRAINT_VTAB";   break;
130727      case SQLITE_CONSTRAINT_FUNCTION:
130728                                zName = "SQLITE_CONSTRAINT_FUNCTION";     break;
130729      case SQLITE_CONSTRAINT_ROWID:   zName = "SQLITE_CONSTRAINT_ROWID";  break;
130730      case SQLITE_MISMATCH:           zName = "SQLITE_MISMATCH";          break;
130731      case SQLITE_MISUSE:             zName = "SQLITE_MISUSE";            break;
130732      case SQLITE_NOLFS:              zName = "SQLITE_NOLFS";             break;
130733      case SQLITE_AUTH:               zName = "SQLITE_AUTH";              break;
130734      case SQLITE_FORMAT:             zName = "SQLITE_FORMAT";            break;
130735      case SQLITE_RANGE:              zName = "SQLITE_RANGE";             break;
130736      case SQLITE_NOTADB:             zName = "SQLITE_NOTADB";            break;
130737      case SQLITE_ROW:                zName = "SQLITE_ROW";               break;
130738      case SQLITE_NOTICE:             zName = "SQLITE_NOTICE";            break;
130739      case SQLITE_NOTICE_RECOVER_WAL: zName = "SQLITE_NOTICE_RECOVER_WAL";break;
130740      case SQLITE_NOTICE_RECOVER_ROLLBACK:
130741                                zName = "SQLITE_NOTICE_RECOVER_ROLLBACK"; break;
130742      case SQLITE_WARNING:            zName = "SQLITE_WARNING";           break;
130743      case SQLITE_WARNING_AUTOINDEX:  zName = "SQLITE_WARNING_AUTOINDEX"; break;
130744      case SQLITE_DONE:               zName = "SQLITE_DONE";              break;
130745    }
130746  }
130747  if( zName==0 ){
130748    static char zBuf[50];
130749    sqlite3_snprintf(sizeof(zBuf), zBuf, "SQLITE_UNKNOWN(%d)", origRc);
130750    zName = zBuf;
130751  }
130752  return zName;
130753}
130754#endif
130755
130756/*
130757** Return a static string that describes the kind of error specified in the
130758** argument.
130759*/
130760SQLITE_PRIVATE const char *sqlite3ErrStr(int rc){
130761  static const char* const aMsg[] = {
130762    /* SQLITE_OK          */ "not an error",
130763    /* SQLITE_ERROR       */ "SQL logic error or missing database",
130764    /* SQLITE_INTERNAL    */ 0,
130765    /* SQLITE_PERM        */ "access permission denied",
130766    /* SQLITE_ABORT       */ "callback requested query abort",
130767    /* SQLITE_BUSY        */ "database is locked",
130768    /* SQLITE_LOCKED      */ "database table is locked",
130769    /* SQLITE_NOMEM       */ "out of memory",
130770    /* SQLITE_READONLY    */ "attempt to write a readonly database",
130771    /* SQLITE_INTERRUPT   */ "interrupted",
130772    /* SQLITE_IOERR       */ "disk I/O error",
130773    /* SQLITE_CORRUPT     */ "database disk image is malformed",
130774    /* SQLITE_NOTFOUND    */ "unknown operation",
130775    /* SQLITE_FULL        */ "database or disk is full",
130776    /* SQLITE_CANTOPEN    */ "unable to open database file",
130777    /* SQLITE_PROTOCOL    */ "locking protocol",
130778    /* SQLITE_EMPTY       */ "table contains no data",
130779    /* SQLITE_SCHEMA      */ "database schema has changed",
130780    /* SQLITE_TOOBIG      */ "string or blob too big",
130781    /* SQLITE_CONSTRAINT  */ "constraint failed",
130782    /* SQLITE_MISMATCH    */ "datatype mismatch",
130783    /* SQLITE_MISUSE      */ "library routine called out of sequence",
130784    /* SQLITE_NOLFS       */ "large file support is disabled",
130785    /* SQLITE_AUTH        */ "authorization denied",
130786    /* SQLITE_FORMAT      */ "auxiliary database format error",
130787    /* SQLITE_RANGE       */ "bind or column index out of range",
130788    /* SQLITE_NOTADB      */ "file is encrypted or is not a database",
130789  };
130790  const char *zErr = "unknown error";
130791  switch( rc ){
130792    case SQLITE_ABORT_ROLLBACK: {
130793      zErr = "abort due to ROLLBACK";
130794      break;
130795    }
130796    default: {
130797      rc &= 0xff;
130798      if( ALWAYS(rc>=0) && rc<ArraySize(aMsg) && aMsg[rc]!=0 ){
130799        zErr = aMsg[rc];
130800      }
130801      break;
130802    }
130803  }
130804  return zErr;
130805}
130806
130807/*
130808** This routine implements a busy callback that sleeps and tries
130809** again until a timeout value is reached.  The timeout value is
130810** an integer number of milliseconds passed in as the first
130811** argument.
130812*/
130813static int sqliteDefaultBusyCallback(
130814 void *ptr,               /* Database connection */
130815 int count                /* Number of times table has been busy */
130816){
130817#if SQLITE_OS_WIN || HAVE_USLEEP
130818  static const u8 delays[] =
130819     { 1, 2, 5, 10, 15, 20, 25, 25,  25,  50,  50, 100 };
130820  static const u8 totals[] =
130821     { 0, 1, 3,  8, 18, 33, 53, 78, 103, 128, 178, 228 };
130822# define NDELAY ArraySize(delays)
130823  sqlite3 *db = (sqlite3 *)ptr;
130824  int timeout = db->busyTimeout;
130825  int delay, prior;
130826
130827  assert( count>=0 );
130828  if( count < NDELAY ){
130829    delay = delays[count];
130830    prior = totals[count];
130831  }else{
130832    delay = delays[NDELAY-1];
130833    prior = totals[NDELAY-1] + delay*(count-(NDELAY-1));
130834  }
130835  if( prior + delay > timeout ){
130836    delay = timeout - prior;
130837    if( delay<=0 ) return 0;
130838  }
130839  sqlite3OsSleep(db->pVfs, delay*1000);
130840  return 1;
130841#else
130842  sqlite3 *db = (sqlite3 *)ptr;
130843  int timeout = ((sqlite3 *)ptr)->busyTimeout;
130844  if( (count+1)*1000 > timeout ){
130845    return 0;
130846  }
130847  sqlite3OsSleep(db->pVfs, 1000000);
130848  return 1;
130849#endif
130850}
130851
130852/*
130853** Invoke the given busy handler.
130854**
130855** This routine is called when an operation failed with a lock.
130856** If this routine returns non-zero, the lock is retried.  If it
130857** returns 0, the operation aborts with an SQLITE_BUSY error.
130858*/
130859SQLITE_PRIVATE int sqlite3InvokeBusyHandler(BusyHandler *p){
130860  int rc;
130861  if( NEVER(p==0) || p->xFunc==0 || p->nBusy<0 ) return 0;
130862  rc = p->xFunc(p->pArg, p->nBusy);
130863  if( rc==0 ){
130864    p->nBusy = -1;
130865  }else{
130866    p->nBusy++;
130867  }
130868  return rc;
130869}
130870
130871/*
130872** This routine sets the busy callback for an Sqlite database to the
130873** given callback function with the given argument.
130874*/
130875SQLITE_API int SQLITE_STDCALL sqlite3_busy_handler(
130876  sqlite3 *db,
130877  int (*xBusy)(void*,int),
130878  void *pArg
130879){
130880#ifdef SQLITE_ENABLE_API_ARMOR
130881  if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT;
130882#endif
130883  sqlite3_mutex_enter(db->mutex);
130884  db->busyHandler.xFunc = xBusy;
130885  db->busyHandler.pArg = pArg;
130886  db->busyHandler.nBusy = 0;
130887  db->busyTimeout = 0;
130888  sqlite3_mutex_leave(db->mutex);
130889  return SQLITE_OK;
130890}
130891
130892#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
130893/*
130894** This routine sets the progress callback for an Sqlite database to the
130895** given callback function with the given argument. The progress callback will
130896** be invoked every nOps opcodes.
130897*/
130898SQLITE_API void SQLITE_STDCALL sqlite3_progress_handler(
130899  sqlite3 *db,
130900  int nOps,
130901  int (*xProgress)(void*),
130902  void *pArg
130903){
130904#ifdef SQLITE_ENABLE_API_ARMOR
130905  if( !sqlite3SafetyCheckOk(db) ){
130906    (void)SQLITE_MISUSE_BKPT;
130907    return;
130908  }
130909#endif
130910  sqlite3_mutex_enter(db->mutex);
130911  if( nOps>0 ){
130912    db->xProgress = xProgress;
130913    db->nProgressOps = (unsigned)nOps;
130914    db->pProgressArg = pArg;
130915  }else{
130916    db->xProgress = 0;
130917    db->nProgressOps = 0;
130918    db->pProgressArg = 0;
130919  }
130920  sqlite3_mutex_leave(db->mutex);
130921}
130922#endif
130923
130924
130925/*
130926** This routine installs a default busy handler that waits for the
130927** specified number of milliseconds before returning 0.
130928*/
130929SQLITE_API int SQLITE_STDCALL sqlite3_busy_timeout(sqlite3 *db, int ms){
130930#ifdef SQLITE_ENABLE_API_ARMOR
130931  if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT;
130932#endif
130933  if( ms>0 ){
130934    sqlite3_busy_handler(db, sqliteDefaultBusyCallback, (void*)db);
130935    db->busyTimeout = ms;
130936  }else{
130937    sqlite3_busy_handler(db, 0, 0);
130938  }
130939  return SQLITE_OK;
130940}
130941
130942/*
130943** Cause any pending operation to stop at its earliest opportunity.
130944*/
130945SQLITE_API void SQLITE_STDCALL sqlite3_interrupt(sqlite3 *db){
130946#ifdef SQLITE_ENABLE_API_ARMOR
130947  if( !sqlite3SafetyCheckOk(db) ){
130948    (void)SQLITE_MISUSE_BKPT;
130949    return;
130950  }
130951#endif
130952  db->u1.isInterrupted = 1;
130953}
130954
130955
130956/*
130957** This function is exactly the same as sqlite3_create_function(), except
130958** that it is designed to be called by internal code. The difference is
130959** that if a malloc() fails in sqlite3_create_function(), an error code
130960** is returned and the mallocFailed flag cleared.
130961*/
130962SQLITE_PRIVATE int sqlite3CreateFunc(
130963  sqlite3 *db,
130964  const char *zFunctionName,
130965  int nArg,
130966  int enc,
130967  void *pUserData,
130968  void (*xFunc)(sqlite3_context*,int,sqlite3_value **),
130969  void (*xStep)(sqlite3_context*,int,sqlite3_value **),
130970  void (*xFinal)(sqlite3_context*),
130971  FuncDestructor *pDestructor
130972){
130973  FuncDef *p;
130974  int nName;
130975  int extraFlags;
130976
130977  assert( sqlite3_mutex_held(db->mutex) );
130978  if( zFunctionName==0 ||
130979      (xFunc && (xFinal || xStep)) ||
130980      (!xFunc && (xFinal && !xStep)) ||
130981      (!xFunc && (!xFinal && xStep)) ||
130982      (nArg<-1 || nArg>SQLITE_MAX_FUNCTION_ARG) ||
130983      (255<(nName = sqlite3Strlen30( zFunctionName))) ){
130984    return SQLITE_MISUSE_BKPT;
130985  }
130986
130987  assert( SQLITE_FUNC_CONSTANT==SQLITE_DETERMINISTIC );
130988  extraFlags = enc &  SQLITE_DETERMINISTIC;
130989  enc &= (SQLITE_FUNC_ENCMASK|SQLITE_ANY);
130990
130991#ifndef SQLITE_OMIT_UTF16
130992  /* If SQLITE_UTF16 is specified as the encoding type, transform this
130993  ** to one of SQLITE_UTF16LE or SQLITE_UTF16BE using the
130994  ** SQLITE_UTF16NATIVE macro. SQLITE_UTF16 is not used internally.
130995  **
130996  ** If SQLITE_ANY is specified, add three versions of the function
130997  ** to the hash table.
130998  */
130999  if( enc==SQLITE_UTF16 ){
131000    enc = SQLITE_UTF16NATIVE;
131001  }else if( enc==SQLITE_ANY ){
131002    int rc;
131003    rc = sqlite3CreateFunc(db, zFunctionName, nArg, SQLITE_UTF8|extraFlags,
131004         pUserData, xFunc, xStep, xFinal, pDestructor);
131005    if( rc==SQLITE_OK ){
131006      rc = sqlite3CreateFunc(db, zFunctionName, nArg, SQLITE_UTF16LE|extraFlags,
131007          pUserData, xFunc, xStep, xFinal, pDestructor);
131008    }
131009    if( rc!=SQLITE_OK ){
131010      return rc;
131011    }
131012    enc = SQLITE_UTF16BE;
131013  }
131014#else
131015  enc = SQLITE_UTF8;
131016#endif
131017
131018  /* Check if an existing function is being overridden or deleted. If so,
131019  ** and there are active VMs, then return SQLITE_BUSY. If a function
131020  ** is being overridden/deleted but there are no active VMs, allow the
131021  ** operation to continue but invalidate all precompiled statements.
131022  */
131023  p = sqlite3FindFunction(db, zFunctionName, nName, nArg, (u8)enc, 0);
131024  if( p && (p->funcFlags & SQLITE_FUNC_ENCMASK)==enc && p->nArg==nArg ){
131025    if( db->nVdbeActive ){
131026      sqlite3ErrorWithMsg(db, SQLITE_BUSY,
131027        "unable to delete/modify user-function due to active statements");
131028      assert( !db->mallocFailed );
131029      return SQLITE_BUSY;
131030    }else{
131031      sqlite3ExpirePreparedStatements(db);
131032    }
131033  }
131034
131035  p = sqlite3FindFunction(db, zFunctionName, nName, nArg, (u8)enc, 1);
131036  assert(p || db->mallocFailed);
131037  if( !p ){
131038    return SQLITE_NOMEM;
131039  }
131040
131041  /* If an older version of the function with a configured destructor is
131042  ** being replaced invoke the destructor function here. */
131043  functionDestroy(db, p);
131044
131045  if( pDestructor ){
131046    pDestructor->nRef++;
131047  }
131048  p->pDestructor = pDestructor;
131049  p->funcFlags = (p->funcFlags & SQLITE_FUNC_ENCMASK) | extraFlags;
131050  testcase( p->funcFlags & SQLITE_DETERMINISTIC );
131051  p->xFunc = xFunc;
131052  p->xStep = xStep;
131053  p->xFinalize = xFinal;
131054  p->pUserData = pUserData;
131055  p->nArg = (u16)nArg;
131056  return SQLITE_OK;
131057}
131058
131059/*
131060** Create new user functions.
131061*/
131062SQLITE_API int SQLITE_STDCALL sqlite3_create_function(
131063  sqlite3 *db,
131064  const char *zFunc,
131065  int nArg,
131066  int enc,
131067  void *p,
131068  void (*xFunc)(sqlite3_context*,int,sqlite3_value **),
131069  void (*xStep)(sqlite3_context*,int,sqlite3_value **),
131070  void (*xFinal)(sqlite3_context*)
131071){
131072  return sqlite3_create_function_v2(db, zFunc, nArg, enc, p, xFunc, xStep,
131073                                    xFinal, 0);
131074}
131075
131076SQLITE_API int SQLITE_STDCALL sqlite3_create_function_v2(
131077  sqlite3 *db,
131078  const char *zFunc,
131079  int nArg,
131080  int enc,
131081  void *p,
131082  void (*xFunc)(sqlite3_context*,int,sqlite3_value **),
131083  void (*xStep)(sqlite3_context*,int,sqlite3_value **),
131084  void (*xFinal)(sqlite3_context*),
131085  void (*xDestroy)(void *)
131086){
131087  int rc = SQLITE_ERROR;
131088  FuncDestructor *pArg = 0;
131089
131090#ifdef SQLITE_ENABLE_API_ARMOR
131091  if( !sqlite3SafetyCheckOk(db) ){
131092    return SQLITE_MISUSE_BKPT;
131093  }
131094#endif
131095  sqlite3_mutex_enter(db->mutex);
131096  if( xDestroy ){
131097    pArg = (FuncDestructor *)sqlite3DbMallocZero(db, sizeof(FuncDestructor));
131098    if( !pArg ){
131099      xDestroy(p);
131100      goto out;
131101    }
131102    pArg->xDestroy = xDestroy;
131103    pArg->pUserData = p;
131104  }
131105  rc = sqlite3CreateFunc(db, zFunc, nArg, enc, p, xFunc, xStep, xFinal, pArg);
131106  if( pArg && pArg->nRef==0 ){
131107    assert( rc!=SQLITE_OK );
131108    xDestroy(p);
131109    sqlite3DbFree(db, pArg);
131110  }
131111
131112 out:
131113  rc = sqlite3ApiExit(db, rc);
131114  sqlite3_mutex_leave(db->mutex);
131115  return rc;
131116}
131117
131118#ifndef SQLITE_OMIT_UTF16
131119SQLITE_API int SQLITE_STDCALL sqlite3_create_function16(
131120  sqlite3 *db,
131121  const void *zFunctionName,
131122  int nArg,
131123  int eTextRep,
131124  void *p,
131125  void (*xFunc)(sqlite3_context*,int,sqlite3_value**),
131126  void (*xStep)(sqlite3_context*,int,sqlite3_value**),
131127  void (*xFinal)(sqlite3_context*)
131128){
131129  int rc;
131130  char *zFunc8;
131131
131132#ifdef SQLITE_ENABLE_API_ARMOR
131133  if( !sqlite3SafetyCheckOk(db) || zFunctionName==0 ) return SQLITE_MISUSE_BKPT;
131134#endif
131135  sqlite3_mutex_enter(db->mutex);
131136  assert( !db->mallocFailed );
131137  zFunc8 = sqlite3Utf16to8(db, zFunctionName, -1, SQLITE_UTF16NATIVE);
131138  rc = sqlite3CreateFunc(db, zFunc8, nArg, eTextRep, p, xFunc, xStep, xFinal,0);
131139  sqlite3DbFree(db, zFunc8);
131140  rc = sqlite3ApiExit(db, rc);
131141  sqlite3_mutex_leave(db->mutex);
131142  return rc;
131143}
131144#endif
131145
131146
131147/*
131148** Declare that a function has been overloaded by a virtual table.
131149**
131150** If the function already exists as a regular global function, then
131151** this routine is a no-op.  If the function does not exist, then create
131152** a new one that always throws a run-time error.
131153**
131154** When virtual tables intend to provide an overloaded function, they
131155** should call this routine to make sure the global function exists.
131156** A global function must exist in order for name resolution to work
131157** properly.
131158*/
131159SQLITE_API int SQLITE_STDCALL sqlite3_overload_function(
131160  sqlite3 *db,
131161  const char *zName,
131162  int nArg
131163){
131164  int nName = sqlite3Strlen30(zName);
131165  int rc = SQLITE_OK;
131166
131167#ifdef SQLITE_ENABLE_API_ARMOR
131168  if( !sqlite3SafetyCheckOk(db) || zName==0 || nArg<-2 ){
131169    return SQLITE_MISUSE_BKPT;
131170  }
131171#endif
131172  sqlite3_mutex_enter(db->mutex);
131173  if( sqlite3FindFunction(db, zName, nName, nArg, SQLITE_UTF8, 0)==0 ){
131174    rc = sqlite3CreateFunc(db, zName, nArg, SQLITE_UTF8,
131175                           0, sqlite3InvalidFunction, 0, 0, 0);
131176  }
131177  rc = sqlite3ApiExit(db, rc);
131178  sqlite3_mutex_leave(db->mutex);
131179  return rc;
131180}
131181
131182#ifndef SQLITE_OMIT_TRACE
131183/*
131184** Register a trace function.  The pArg from the previously registered trace
131185** is returned.
131186**
131187** A NULL trace function means that no tracing is executes.  A non-NULL
131188** trace is a pointer to a function that is invoked at the start of each
131189** SQL statement.
131190*/
131191SQLITE_API void *SQLITE_STDCALL sqlite3_trace(sqlite3 *db, void (*xTrace)(void*,const char*), void *pArg){
131192  void *pOld;
131193
131194#ifdef SQLITE_ENABLE_API_ARMOR
131195  if( !sqlite3SafetyCheckOk(db) ){
131196    (void)SQLITE_MISUSE_BKPT;
131197    return 0;
131198  }
131199#endif
131200  sqlite3_mutex_enter(db->mutex);
131201  pOld = db->pTraceArg;
131202  db->xTrace = xTrace;
131203  db->pTraceArg = pArg;
131204  sqlite3_mutex_leave(db->mutex);
131205  return pOld;
131206}
131207/*
131208** Register a profile function.  The pArg from the previously registered
131209** profile function is returned.
131210**
131211** A NULL profile function means that no profiling is executes.  A non-NULL
131212** profile is a pointer to a function that is invoked at the conclusion of
131213** each SQL statement that is run.
131214*/
131215SQLITE_API void *SQLITE_STDCALL sqlite3_profile(
131216  sqlite3 *db,
131217  void (*xProfile)(void*,const char*,sqlite_uint64),
131218  void *pArg
131219){
131220  void *pOld;
131221
131222#ifdef SQLITE_ENABLE_API_ARMOR
131223  if( !sqlite3SafetyCheckOk(db) ){
131224    (void)SQLITE_MISUSE_BKPT;
131225    return 0;
131226  }
131227#endif
131228  sqlite3_mutex_enter(db->mutex);
131229  pOld = db->pProfileArg;
131230  db->xProfile = xProfile;
131231  db->pProfileArg = pArg;
131232  sqlite3_mutex_leave(db->mutex);
131233  return pOld;
131234}
131235#endif /* SQLITE_OMIT_TRACE */
131236
131237/*
131238** Register a function to be invoked when a transaction commits.
131239** If the invoked function returns non-zero, then the commit becomes a
131240** rollback.
131241*/
131242SQLITE_API void *SQLITE_STDCALL sqlite3_commit_hook(
131243  sqlite3 *db,              /* Attach the hook to this database */
131244  int (*xCallback)(void*),  /* Function to invoke on each commit */
131245  void *pArg                /* Argument to the function */
131246){
131247  void *pOld;
131248
131249#ifdef SQLITE_ENABLE_API_ARMOR
131250  if( !sqlite3SafetyCheckOk(db) ){
131251    (void)SQLITE_MISUSE_BKPT;
131252    return 0;
131253  }
131254#endif
131255  sqlite3_mutex_enter(db->mutex);
131256  pOld = db->pCommitArg;
131257  db->xCommitCallback = xCallback;
131258  db->pCommitArg = pArg;
131259  sqlite3_mutex_leave(db->mutex);
131260  return pOld;
131261}
131262
131263/*
131264** Register a callback to be invoked each time a row is updated,
131265** inserted or deleted using this database connection.
131266*/
131267SQLITE_API void *SQLITE_STDCALL sqlite3_update_hook(
131268  sqlite3 *db,              /* Attach the hook to this database */
131269  void (*xCallback)(void*,int,char const *,char const *,sqlite_int64),
131270  void *pArg                /* Argument to the function */
131271){
131272  void *pRet;
131273
131274#ifdef SQLITE_ENABLE_API_ARMOR
131275  if( !sqlite3SafetyCheckOk(db) ){
131276    (void)SQLITE_MISUSE_BKPT;
131277    return 0;
131278  }
131279#endif
131280  sqlite3_mutex_enter(db->mutex);
131281  pRet = db->pUpdateArg;
131282  db->xUpdateCallback = xCallback;
131283  db->pUpdateArg = pArg;
131284  sqlite3_mutex_leave(db->mutex);
131285  return pRet;
131286}
131287
131288/*
131289** Register a callback to be invoked each time a transaction is rolled
131290** back by this database connection.
131291*/
131292SQLITE_API void *SQLITE_STDCALL sqlite3_rollback_hook(
131293  sqlite3 *db,              /* Attach the hook to this database */
131294  void (*xCallback)(void*), /* Callback function */
131295  void *pArg                /* Argument to the function */
131296){
131297  void *pRet;
131298
131299#ifdef SQLITE_ENABLE_API_ARMOR
131300  if( !sqlite3SafetyCheckOk(db) ){
131301    (void)SQLITE_MISUSE_BKPT;
131302    return 0;
131303  }
131304#endif
131305  sqlite3_mutex_enter(db->mutex);
131306  pRet = db->pRollbackArg;
131307  db->xRollbackCallback = xCallback;
131308  db->pRollbackArg = pArg;
131309  sqlite3_mutex_leave(db->mutex);
131310  return pRet;
131311}
131312
131313#ifndef SQLITE_OMIT_WAL
131314/*
131315** The sqlite3_wal_hook() callback registered by sqlite3_wal_autocheckpoint().
131316** Invoke sqlite3_wal_checkpoint if the number of frames in the log file
131317** is greater than sqlite3.pWalArg cast to an integer (the value configured by
131318** wal_autocheckpoint()).
131319*/
131320SQLITE_PRIVATE int sqlite3WalDefaultHook(
131321  void *pClientData,     /* Argument */
131322  sqlite3 *db,           /* Connection */
131323  const char *zDb,       /* Database */
131324  int nFrame             /* Size of WAL */
131325){
131326  if( nFrame>=SQLITE_PTR_TO_INT(pClientData) ){
131327    sqlite3BeginBenignMalloc();
131328    sqlite3_wal_checkpoint(db, zDb);
131329    sqlite3EndBenignMalloc();
131330  }
131331  return SQLITE_OK;
131332}
131333#endif /* SQLITE_OMIT_WAL */
131334
131335/*
131336** Configure an sqlite3_wal_hook() callback to automatically checkpoint
131337** a database after committing a transaction if there are nFrame or
131338** more frames in the log file. Passing zero or a negative value as the
131339** nFrame parameter disables automatic checkpoints entirely.
131340**
131341** The callback registered by this function replaces any existing callback
131342** registered using sqlite3_wal_hook(). Likewise, registering a callback
131343** using sqlite3_wal_hook() disables the automatic checkpoint mechanism
131344** configured by this function.
131345*/
131346SQLITE_API int SQLITE_STDCALL sqlite3_wal_autocheckpoint(sqlite3 *db, int nFrame){
131347#ifdef SQLITE_OMIT_WAL
131348  UNUSED_PARAMETER(db);
131349  UNUSED_PARAMETER(nFrame);
131350#else
131351#ifdef SQLITE_ENABLE_API_ARMOR
131352  if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT;
131353#endif
131354  if( nFrame>0 ){
131355    sqlite3_wal_hook(db, sqlite3WalDefaultHook, SQLITE_INT_TO_PTR(nFrame));
131356  }else{
131357    sqlite3_wal_hook(db, 0, 0);
131358  }
131359#endif
131360  return SQLITE_OK;
131361}
131362
131363/*
131364** Register a callback to be invoked each time a transaction is written
131365** into the write-ahead-log by this database connection.
131366*/
131367SQLITE_API void *SQLITE_STDCALL sqlite3_wal_hook(
131368  sqlite3 *db,                    /* Attach the hook to this db handle */
131369  int(*xCallback)(void *, sqlite3*, const char*, int),
131370  void *pArg                      /* First argument passed to xCallback() */
131371){
131372#ifndef SQLITE_OMIT_WAL
131373  void *pRet;
131374#ifdef SQLITE_ENABLE_API_ARMOR
131375  if( !sqlite3SafetyCheckOk(db) ){
131376    (void)SQLITE_MISUSE_BKPT;
131377    return 0;
131378  }
131379#endif
131380  sqlite3_mutex_enter(db->mutex);
131381  pRet = db->pWalArg;
131382  db->xWalCallback = xCallback;
131383  db->pWalArg = pArg;
131384  sqlite3_mutex_leave(db->mutex);
131385  return pRet;
131386#else
131387  return 0;
131388#endif
131389}
131390
131391/*
131392** Checkpoint database zDb.
131393*/
131394SQLITE_API int SQLITE_STDCALL sqlite3_wal_checkpoint_v2(
131395  sqlite3 *db,                    /* Database handle */
131396  const char *zDb,                /* Name of attached database (or NULL) */
131397  int eMode,                      /* SQLITE_CHECKPOINT_* value */
131398  int *pnLog,                     /* OUT: Size of WAL log in frames */
131399  int *pnCkpt                     /* OUT: Total number of frames checkpointed */
131400){
131401#ifdef SQLITE_OMIT_WAL
131402  return SQLITE_OK;
131403#else
131404  int rc;                         /* Return code */
131405  int iDb = SQLITE_MAX_ATTACHED;  /* sqlite3.aDb[] index of db to checkpoint */
131406
131407#ifdef SQLITE_ENABLE_API_ARMOR
131408  if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT;
131409#endif
131410
131411  /* Initialize the output variables to -1 in case an error occurs. */
131412  if( pnLog ) *pnLog = -1;
131413  if( pnCkpt ) *pnCkpt = -1;
131414
131415  assert( SQLITE_CHECKPOINT_PASSIVE==0 );
131416  assert( SQLITE_CHECKPOINT_FULL==1 );
131417  assert( SQLITE_CHECKPOINT_RESTART==2 );
131418  assert( SQLITE_CHECKPOINT_TRUNCATE==3 );
131419  if( eMode<SQLITE_CHECKPOINT_PASSIVE || eMode>SQLITE_CHECKPOINT_TRUNCATE ){
131420    /* EVIDENCE-OF: R-03996-12088 The M parameter must be a valid checkpoint
131421    ** mode: */
131422    return SQLITE_MISUSE;
131423  }
131424
131425  sqlite3_mutex_enter(db->mutex);
131426  if( zDb && zDb[0] ){
131427    iDb = sqlite3FindDbName(db, zDb);
131428  }
131429  if( iDb<0 ){
131430    rc = SQLITE_ERROR;
131431    sqlite3ErrorWithMsg(db, SQLITE_ERROR, "unknown database: %s", zDb);
131432  }else{
131433    db->busyHandler.nBusy = 0;
131434    rc = sqlite3Checkpoint(db, iDb, eMode, pnLog, pnCkpt);
131435    sqlite3Error(db, rc);
131436  }
131437  rc = sqlite3ApiExit(db, rc);
131438  sqlite3_mutex_leave(db->mutex);
131439  return rc;
131440#endif
131441}
131442
131443
131444/*
131445** Checkpoint database zDb. If zDb is NULL, or if the buffer zDb points
131446** to contains a zero-length string, all attached databases are
131447** checkpointed.
131448*/
131449SQLITE_API int SQLITE_STDCALL sqlite3_wal_checkpoint(sqlite3 *db, const char *zDb){
131450  /* EVIDENCE-OF: R-41613-20553 The sqlite3_wal_checkpoint(D,X) is equivalent to
131451  ** sqlite3_wal_checkpoint_v2(D,X,SQLITE_CHECKPOINT_PASSIVE,0,0). */
131452  return sqlite3_wal_checkpoint_v2(db,zDb,SQLITE_CHECKPOINT_PASSIVE,0,0);
131453}
131454
131455#ifndef SQLITE_OMIT_WAL
131456/*
131457** Run a checkpoint on database iDb. This is a no-op if database iDb is
131458** not currently open in WAL mode.
131459**
131460** If a transaction is open on the database being checkpointed, this
131461** function returns SQLITE_LOCKED and a checkpoint is not attempted. If
131462** an error occurs while running the checkpoint, an SQLite error code is
131463** returned (i.e. SQLITE_IOERR). Otherwise, SQLITE_OK.
131464**
131465** The mutex on database handle db should be held by the caller. The mutex
131466** associated with the specific b-tree being checkpointed is taken by
131467** this function while the checkpoint is running.
131468**
131469** If iDb is passed SQLITE_MAX_ATTACHED, then all attached databases are
131470** checkpointed. If an error is encountered it is returned immediately -
131471** no attempt is made to checkpoint any remaining databases.
131472**
131473** Parameter eMode is one of SQLITE_CHECKPOINT_PASSIVE, FULL or RESTART.
131474*/
131475SQLITE_PRIVATE int sqlite3Checkpoint(sqlite3 *db, int iDb, int eMode, int *pnLog, int *pnCkpt){
131476  int rc = SQLITE_OK;             /* Return code */
131477  int i;                          /* Used to iterate through attached dbs */
131478  int bBusy = 0;                  /* True if SQLITE_BUSY has been encountered */
131479
131480  assert( sqlite3_mutex_held(db->mutex) );
131481  assert( !pnLog || *pnLog==-1 );
131482  assert( !pnCkpt || *pnCkpt==-1 );
131483
131484  for(i=0; i<db->nDb && rc==SQLITE_OK; i++){
131485    if( i==iDb || iDb==SQLITE_MAX_ATTACHED ){
131486      rc = sqlite3BtreeCheckpoint(db->aDb[i].pBt, eMode, pnLog, pnCkpt);
131487      pnLog = 0;
131488      pnCkpt = 0;
131489      if( rc==SQLITE_BUSY ){
131490        bBusy = 1;
131491        rc = SQLITE_OK;
131492      }
131493    }
131494  }
131495
131496  return (rc==SQLITE_OK && bBusy) ? SQLITE_BUSY : rc;
131497}
131498#endif /* SQLITE_OMIT_WAL */
131499
131500/*
131501** This function returns true if main-memory should be used instead of
131502** a temporary file for transient pager files and statement journals.
131503** The value returned depends on the value of db->temp_store (runtime
131504** parameter) and the compile time value of SQLITE_TEMP_STORE. The
131505** following table describes the relationship between these two values
131506** and this functions return value.
131507**
131508**   SQLITE_TEMP_STORE     db->temp_store     Location of temporary database
131509**   -----------------     --------------     ------------------------------
131510**   0                     any                file      (return 0)
131511**   1                     1                  file      (return 0)
131512**   1                     2                  memory    (return 1)
131513**   1                     0                  file      (return 0)
131514**   2                     1                  file      (return 0)
131515**   2                     2                  memory    (return 1)
131516**   2                     0                  memory    (return 1)
131517**   3                     any                memory    (return 1)
131518*/
131519SQLITE_PRIVATE int sqlite3TempInMemory(const sqlite3 *db){
131520#if SQLITE_TEMP_STORE==1
131521  return ( db->temp_store==2 );
131522#endif
131523#if SQLITE_TEMP_STORE==2
131524  return ( db->temp_store!=1 );
131525#endif
131526#if SQLITE_TEMP_STORE==3
131527  UNUSED_PARAMETER(db);
131528  return 1;
131529#endif
131530#if SQLITE_TEMP_STORE<1 || SQLITE_TEMP_STORE>3
131531  UNUSED_PARAMETER(db);
131532  return 0;
131533#endif
131534}
131535
131536/*
131537** Return UTF-8 encoded English language explanation of the most recent
131538** error.
131539*/
131540SQLITE_API const char *SQLITE_STDCALL sqlite3_errmsg(sqlite3 *db){
131541  const char *z;
131542  if( !db ){
131543    return sqlite3ErrStr(SQLITE_NOMEM);
131544  }
131545  if( !sqlite3SafetyCheckSickOrOk(db) ){
131546    return sqlite3ErrStr(SQLITE_MISUSE_BKPT);
131547  }
131548  sqlite3_mutex_enter(db->mutex);
131549  if( db->mallocFailed ){
131550    z = sqlite3ErrStr(SQLITE_NOMEM);
131551  }else{
131552    testcase( db->pErr==0 );
131553    z = (char*)sqlite3_value_text(db->pErr);
131554    assert( !db->mallocFailed );
131555    if( z==0 ){
131556      z = sqlite3ErrStr(db->errCode);
131557    }
131558  }
131559  sqlite3_mutex_leave(db->mutex);
131560  return z;
131561}
131562
131563#ifndef SQLITE_OMIT_UTF16
131564/*
131565** Return UTF-16 encoded English language explanation of the most recent
131566** error.
131567*/
131568SQLITE_API const void *SQLITE_STDCALL sqlite3_errmsg16(sqlite3 *db){
131569  static const u16 outOfMem[] = {
131570    'o', 'u', 't', ' ', 'o', 'f', ' ', 'm', 'e', 'm', 'o', 'r', 'y', 0
131571  };
131572  static const u16 misuse[] = {
131573    'l', 'i', 'b', 'r', 'a', 'r', 'y', ' ',
131574    'r', 'o', 'u', 't', 'i', 'n', 'e', ' ',
131575    'c', 'a', 'l', 'l', 'e', 'd', ' ',
131576    'o', 'u', 't', ' ',
131577    'o', 'f', ' ',
131578    's', 'e', 'q', 'u', 'e', 'n', 'c', 'e', 0
131579  };
131580
131581  const void *z;
131582  if( !db ){
131583    return (void *)outOfMem;
131584  }
131585  if( !sqlite3SafetyCheckSickOrOk(db) ){
131586    return (void *)misuse;
131587  }
131588  sqlite3_mutex_enter(db->mutex);
131589  if( db->mallocFailed ){
131590    z = (void *)outOfMem;
131591  }else{
131592    z = sqlite3_value_text16(db->pErr);
131593    if( z==0 ){
131594      sqlite3ErrorWithMsg(db, db->errCode, sqlite3ErrStr(db->errCode));
131595      z = sqlite3_value_text16(db->pErr);
131596    }
131597    /* A malloc() may have failed within the call to sqlite3_value_text16()
131598    ** above. If this is the case, then the db->mallocFailed flag needs to
131599    ** be cleared before returning. Do this directly, instead of via
131600    ** sqlite3ApiExit(), to avoid setting the database handle error message.
131601    */
131602    db->mallocFailed = 0;
131603  }
131604  sqlite3_mutex_leave(db->mutex);
131605  return z;
131606}
131607#endif /* SQLITE_OMIT_UTF16 */
131608
131609/*
131610** Return the most recent error code generated by an SQLite routine. If NULL is
131611** passed to this function, we assume a malloc() failed during sqlite3_open().
131612*/
131613SQLITE_API int SQLITE_STDCALL sqlite3_errcode(sqlite3 *db){
131614  if( db && !sqlite3SafetyCheckSickOrOk(db) ){
131615    return SQLITE_MISUSE_BKPT;
131616  }
131617  if( !db || db->mallocFailed ){
131618    return SQLITE_NOMEM;
131619  }
131620  return db->errCode & db->errMask;
131621}
131622SQLITE_API int SQLITE_STDCALL sqlite3_extended_errcode(sqlite3 *db){
131623  if( db && !sqlite3SafetyCheckSickOrOk(db) ){
131624    return SQLITE_MISUSE_BKPT;
131625  }
131626  if( !db || db->mallocFailed ){
131627    return SQLITE_NOMEM;
131628  }
131629  return db->errCode;
131630}
131631
131632/*
131633** Return a string that describes the kind of error specified in the
131634** argument.  For now, this simply calls the internal sqlite3ErrStr()
131635** function.
131636*/
131637SQLITE_API const char *SQLITE_STDCALL sqlite3_errstr(int rc){
131638  return sqlite3ErrStr(rc);
131639}
131640
131641/*
131642** Create a new collating function for database "db".  The name is zName
131643** and the encoding is enc.
131644*/
131645static int createCollation(
131646  sqlite3* db,
131647  const char *zName,
131648  u8 enc,
131649  void* pCtx,
131650  int(*xCompare)(void*,int,const void*,int,const void*),
131651  void(*xDel)(void*)
131652){
131653  CollSeq *pColl;
131654  int enc2;
131655
131656  assert( sqlite3_mutex_held(db->mutex) );
131657
131658  /* If SQLITE_UTF16 is specified as the encoding type, transform this
131659  ** to one of SQLITE_UTF16LE or SQLITE_UTF16BE using the
131660  ** SQLITE_UTF16NATIVE macro. SQLITE_UTF16 is not used internally.
131661  */
131662  enc2 = enc;
131663  testcase( enc2==SQLITE_UTF16 );
131664  testcase( enc2==SQLITE_UTF16_ALIGNED );
131665  if( enc2==SQLITE_UTF16 || enc2==SQLITE_UTF16_ALIGNED ){
131666    enc2 = SQLITE_UTF16NATIVE;
131667  }
131668  if( enc2<SQLITE_UTF8 || enc2>SQLITE_UTF16BE ){
131669    return SQLITE_MISUSE_BKPT;
131670  }
131671
131672  /* Check if this call is removing or replacing an existing collation
131673  ** sequence. If so, and there are active VMs, return busy. If there
131674  ** are no active VMs, invalidate any pre-compiled statements.
131675  */
131676  pColl = sqlite3FindCollSeq(db, (u8)enc2, zName, 0);
131677  if( pColl && pColl->xCmp ){
131678    if( db->nVdbeActive ){
131679      sqlite3ErrorWithMsg(db, SQLITE_BUSY,
131680        "unable to delete/modify collation sequence due to active statements");
131681      return SQLITE_BUSY;
131682    }
131683    sqlite3ExpirePreparedStatements(db);
131684
131685    /* If collation sequence pColl was created directly by a call to
131686    ** sqlite3_create_collation, and not generated by synthCollSeq(),
131687    ** then any copies made by synthCollSeq() need to be invalidated.
131688    ** Also, collation destructor - CollSeq.xDel() - function may need
131689    ** to be called.
131690    */
131691    if( (pColl->enc & ~SQLITE_UTF16_ALIGNED)==enc2 ){
131692      CollSeq *aColl = sqlite3HashFind(&db->aCollSeq, zName);
131693      int j;
131694      for(j=0; j<3; j++){
131695        CollSeq *p = &aColl[j];
131696        if( p->enc==pColl->enc ){
131697          if( p->xDel ){
131698            p->xDel(p->pUser);
131699          }
131700          p->xCmp = 0;
131701        }
131702      }
131703    }
131704  }
131705
131706  pColl = sqlite3FindCollSeq(db, (u8)enc2, zName, 1);
131707  if( pColl==0 ) return SQLITE_NOMEM;
131708  pColl->xCmp = xCompare;
131709  pColl->pUser = pCtx;
131710  pColl->xDel = xDel;
131711  pColl->enc = (u8)(enc2 | (enc & SQLITE_UTF16_ALIGNED));
131712  sqlite3Error(db, SQLITE_OK);
131713  return SQLITE_OK;
131714}
131715
131716
131717/*
131718** This array defines hard upper bounds on limit values.  The
131719** initializer must be kept in sync with the SQLITE_LIMIT_*
131720** #defines in sqlite3.h.
131721*/
131722static const int aHardLimit[] = {
131723  SQLITE_MAX_LENGTH,
131724  SQLITE_MAX_SQL_LENGTH,
131725  SQLITE_MAX_COLUMN,
131726  SQLITE_MAX_EXPR_DEPTH,
131727  SQLITE_MAX_COMPOUND_SELECT,
131728  SQLITE_MAX_VDBE_OP,
131729  SQLITE_MAX_FUNCTION_ARG,
131730  SQLITE_MAX_ATTACHED,
131731  SQLITE_MAX_LIKE_PATTERN_LENGTH,
131732  SQLITE_MAX_VARIABLE_NUMBER,      /* IMP: R-38091-32352 */
131733  SQLITE_MAX_TRIGGER_DEPTH,
131734  SQLITE_MAX_WORKER_THREADS,
131735};
131736
131737/*
131738** Make sure the hard limits are set to reasonable values
131739*/
131740#if SQLITE_MAX_LENGTH<100
131741# error SQLITE_MAX_LENGTH must be at least 100
131742#endif
131743#if SQLITE_MAX_SQL_LENGTH<100
131744# error SQLITE_MAX_SQL_LENGTH must be at least 100
131745#endif
131746#if SQLITE_MAX_SQL_LENGTH>SQLITE_MAX_LENGTH
131747# error SQLITE_MAX_SQL_LENGTH must not be greater than SQLITE_MAX_LENGTH
131748#endif
131749#if SQLITE_MAX_COMPOUND_SELECT<2
131750# error SQLITE_MAX_COMPOUND_SELECT must be at least 2
131751#endif
131752#if SQLITE_MAX_VDBE_OP<40
131753# error SQLITE_MAX_VDBE_OP must be at least 40
131754#endif
131755#if SQLITE_MAX_FUNCTION_ARG<0 || SQLITE_MAX_FUNCTION_ARG>1000
131756# error SQLITE_MAX_FUNCTION_ARG must be between 0 and 1000
131757#endif
131758#if SQLITE_MAX_ATTACHED<0 || SQLITE_MAX_ATTACHED>125
131759# error SQLITE_MAX_ATTACHED must be between 0 and 125
131760#endif
131761#if SQLITE_MAX_LIKE_PATTERN_LENGTH<1
131762# error SQLITE_MAX_LIKE_PATTERN_LENGTH must be at least 1
131763#endif
131764#if SQLITE_MAX_COLUMN>32767
131765# error SQLITE_MAX_COLUMN must not exceed 32767
131766#endif
131767#if SQLITE_MAX_TRIGGER_DEPTH<1
131768# error SQLITE_MAX_TRIGGER_DEPTH must be at least 1
131769#endif
131770#if SQLITE_MAX_WORKER_THREADS<0 || SQLITE_MAX_WORKER_THREADS>50
131771# error SQLITE_MAX_WORKER_THREADS must be between 0 and 50
131772#endif
131773
131774
131775/*
131776** Change the value of a limit.  Report the old value.
131777** If an invalid limit index is supplied, report -1.
131778** Make no changes but still report the old value if the
131779** new limit is negative.
131780**
131781** A new lower limit does not shrink existing constructs.
131782** It merely prevents new constructs that exceed the limit
131783** from forming.
131784*/
131785SQLITE_API int SQLITE_STDCALL sqlite3_limit(sqlite3 *db, int limitId, int newLimit){
131786  int oldLimit;
131787
131788#ifdef SQLITE_ENABLE_API_ARMOR
131789  if( !sqlite3SafetyCheckOk(db) ){
131790    (void)SQLITE_MISUSE_BKPT;
131791    return -1;
131792  }
131793#endif
131794
131795  /* EVIDENCE-OF: R-30189-54097 For each limit category SQLITE_LIMIT_NAME
131796  ** there is a hard upper bound set at compile-time by a C preprocessor
131797  ** macro called SQLITE_MAX_NAME. (The "_LIMIT_" in the name is changed to
131798  ** "_MAX_".)
131799  */
131800  assert( aHardLimit[SQLITE_LIMIT_LENGTH]==SQLITE_MAX_LENGTH );
131801  assert( aHardLimit[SQLITE_LIMIT_SQL_LENGTH]==SQLITE_MAX_SQL_LENGTH );
131802  assert( aHardLimit[SQLITE_LIMIT_COLUMN]==SQLITE_MAX_COLUMN );
131803  assert( aHardLimit[SQLITE_LIMIT_EXPR_DEPTH]==SQLITE_MAX_EXPR_DEPTH );
131804  assert( aHardLimit[SQLITE_LIMIT_COMPOUND_SELECT]==SQLITE_MAX_COMPOUND_SELECT);
131805  assert( aHardLimit[SQLITE_LIMIT_VDBE_OP]==SQLITE_MAX_VDBE_OP );
131806  assert( aHardLimit[SQLITE_LIMIT_FUNCTION_ARG]==SQLITE_MAX_FUNCTION_ARG );
131807  assert( aHardLimit[SQLITE_LIMIT_ATTACHED]==SQLITE_MAX_ATTACHED );
131808  assert( aHardLimit[SQLITE_LIMIT_LIKE_PATTERN_LENGTH]==
131809                                               SQLITE_MAX_LIKE_PATTERN_LENGTH );
131810  assert( aHardLimit[SQLITE_LIMIT_VARIABLE_NUMBER]==SQLITE_MAX_VARIABLE_NUMBER);
131811  assert( aHardLimit[SQLITE_LIMIT_TRIGGER_DEPTH]==SQLITE_MAX_TRIGGER_DEPTH );
131812  assert( aHardLimit[SQLITE_LIMIT_WORKER_THREADS]==SQLITE_MAX_WORKER_THREADS );
131813  assert( SQLITE_LIMIT_WORKER_THREADS==(SQLITE_N_LIMIT-1) );
131814
131815
131816  if( limitId<0 || limitId>=SQLITE_N_LIMIT ){
131817    return -1;
131818  }
131819  oldLimit = db->aLimit[limitId];
131820  if( newLimit>=0 ){                   /* IMP: R-52476-28732 */
131821    if( newLimit>aHardLimit[limitId] ){
131822      newLimit = aHardLimit[limitId];  /* IMP: R-51463-25634 */
131823    }
131824    db->aLimit[limitId] = newLimit;
131825  }
131826  return oldLimit;                     /* IMP: R-53341-35419 */
131827}
131828
131829/*
131830** This function is used to parse both URIs and non-URI filenames passed by the
131831** user to API functions sqlite3_open() or sqlite3_open_v2(), and for database
131832** URIs specified as part of ATTACH statements.
131833**
131834** The first argument to this function is the name of the VFS to use (or
131835** a NULL to signify the default VFS) if the URI does not contain a "vfs=xxx"
131836** query parameter. The second argument contains the URI (or non-URI filename)
131837** itself. When this function is called the *pFlags variable should contain
131838** the default flags to open the database handle with. The value stored in
131839** *pFlags may be updated before returning if the URI filename contains
131840** "cache=xxx" or "mode=xxx" query parameters.
131841**
131842** If successful, SQLITE_OK is returned. In this case *ppVfs is set to point to
131843** the VFS that should be used to open the database file. *pzFile is set to
131844** point to a buffer containing the name of the file to open. It is the
131845** responsibility of the caller to eventually call sqlite3_free() to release
131846** this buffer.
131847**
131848** If an error occurs, then an SQLite error code is returned and *pzErrMsg
131849** may be set to point to a buffer containing an English language error
131850** message. It is the responsibility of the caller to eventually release
131851** this buffer by calling sqlite3_free().
131852*/
131853SQLITE_PRIVATE int sqlite3ParseUri(
131854  const char *zDefaultVfs,        /* VFS to use if no "vfs=xxx" query option */
131855  const char *zUri,               /* Nul-terminated URI to parse */
131856  unsigned int *pFlags,           /* IN/OUT: SQLITE_OPEN_XXX flags */
131857  sqlite3_vfs **ppVfs,            /* OUT: VFS to use */
131858  char **pzFile,                  /* OUT: Filename component of URI */
131859  char **pzErrMsg                 /* OUT: Error message (if rc!=SQLITE_OK) */
131860){
131861  int rc = SQLITE_OK;
131862  unsigned int flags = *pFlags;
131863  const char *zVfs = zDefaultVfs;
131864  char *zFile;
131865  char c;
131866  int nUri = sqlite3Strlen30(zUri);
131867
131868  assert( *pzErrMsg==0 );
131869
131870  if( ((flags & SQLITE_OPEN_URI)             /* IMP: R-48725-32206 */
131871            || sqlite3GlobalConfig.bOpenUri) /* IMP: R-51689-46548 */
131872   && nUri>=5 && memcmp(zUri, "file:", 5)==0 /* IMP: R-57884-37496 */
131873  ){
131874    char *zOpt;
131875    int eState;                   /* Parser state when parsing URI */
131876    int iIn;                      /* Input character index */
131877    int iOut = 0;                 /* Output character index */
131878    u64 nByte = nUri+2;           /* Bytes of space to allocate */
131879
131880    /* Make sure the SQLITE_OPEN_URI flag is set to indicate to the VFS xOpen
131881    ** method that there may be extra parameters following the file-name.  */
131882    flags |= SQLITE_OPEN_URI;
131883
131884    for(iIn=0; iIn<nUri; iIn++) nByte += (zUri[iIn]=='&');
131885    zFile = sqlite3_malloc64(nByte);
131886    if( !zFile ) return SQLITE_NOMEM;
131887
131888    iIn = 5;
131889#ifdef SQLITE_ALLOW_URI_AUTHORITY
131890    if( strncmp(zUri+5, "///", 3)==0 ){
131891      iIn = 7;
131892      /* The following condition causes URIs with five leading / characters
131893      ** like file://///host/path to be converted into UNCs like //host/path.
131894      ** The correct URI for that UNC has only two or four leading / characters
131895      ** file://host/path or file:////host/path.  But 5 leading slashes is a
131896      ** common error, we are told, so we handle it as a special case. */
131897      if( strncmp(zUri+7, "///", 3)==0 ){ iIn++; }
131898    }else if( strncmp(zUri+5, "//localhost/", 12)==0 ){
131899      iIn = 16;
131900    }
131901#else
131902    /* Discard the scheme and authority segments of the URI. */
131903    if( zUri[5]=='/' && zUri[6]=='/' ){
131904      iIn = 7;
131905      while( zUri[iIn] && zUri[iIn]!='/' ) iIn++;
131906      if( iIn!=7 && (iIn!=16 || memcmp("localhost", &zUri[7], 9)) ){
131907        *pzErrMsg = sqlite3_mprintf("invalid uri authority: %.*s",
131908            iIn-7, &zUri[7]);
131909        rc = SQLITE_ERROR;
131910        goto parse_uri_out;
131911      }
131912    }
131913#endif
131914
131915    /* Copy the filename and any query parameters into the zFile buffer.
131916    ** Decode %HH escape codes along the way.
131917    **
131918    ** Within this loop, variable eState may be set to 0, 1 or 2, depending
131919    ** on the parsing context. As follows:
131920    **
131921    **   0: Parsing file-name.
131922    **   1: Parsing name section of a name=value query parameter.
131923    **   2: Parsing value section of a name=value query parameter.
131924    */
131925    eState = 0;
131926    while( (c = zUri[iIn])!=0 && c!='#' ){
131927      iIn++;
131928      if( c=='%'
131929       && sqlite3Isxdigit(zUri[iIn])
131930       && sqlite3Isxdigit(zUri[iIn+1])
131931      ){
131932        int octet = (sqlite3HexToInt(zUri[iIn++]) << 4);
131933        octet += sqlite3HexToInt(zUri[iIn++]);
131934
131935        assert( octet>=0 && octet<256 );
131936        if( octet==0 ){
131937          /* This branch is taken when "%00" appears within the URI. In this
131938          ** case we ignore all text in the remainder of the path, name or
131939          ** value currently being parsed. So ignore the current character
131940          ** and skip to the next "?", "=" or "&", as appropriate. */
131941          while( (c = zUri[iIn])!=0 && c!='#'
131942              && (eState!=0 || c!='?')
131943              && (eState!=1 || (c!='=' && c!='&'))
131944              && (eState!=2 || c!='&')
131945          ){
131946            iIn++;
131947          }
131948          continue;
131949        }
131950        c = octet;
131951      }else if( eState==1 && (c=='&' || c=='=') ){
131952        if( zFile[iOut-1]==0 ){
131953          /* An empty option name. Ignore this option altogether. */
131954          while( zUri[iIn] && zUri[iIn]!='#' && zUri[iIn-1]!='&' ) iIn++;
131955          continue;
131956        }
131957        if( c=='&' ){
131958          zFile[iOut++] = '\0';
131959        }else{
131960          eState = 2;
131961        }
131962        c = 0;
131963      }else if( (eState==0 && c=='?') || (eState==2 && c=='&') ){
131964        c = 0;
131965        eState = 1;
131966      }
131967      zFile[iOut++] = c;
131968    }
131969    if( eState==1 ) zFile[iOut++] = '\0';
131970    zFile[iOut++] = '\0';
131971    zFile[iOut++] = '\0';
131972
131973    /* Check if there were any options specified that should be interpreted
131974    ** here. Options that are interpreted here include "vfs" and those that
131975    ** correspond to flags that may be passed to the sqlite3_open_v2()
131976    ** method. */
131977    zOpt = &zFile[sqlite3Strlen30(zFile)+1];
131978    while( zOpt[0] ){
131979      int nOpt = sqlite3Strlen30(zOpt);
131980      char *zVal = &zOpt[nOpt+1];
131981      int nVal = sqlite3Strlen30(zVal);
131982
131983      if( nOpt==3 && memcmp("vfs", zOpt, 3)==0 ){
131984        zVfs = zVal;
131985      }else{
131986        struct OpenMode {
131987          const char *z;
131988          int mode;
131989        } *aMode = 0;
131990        char *zModeType = 0;
131991        int mask = 0;
131992        int limit = 0;
131993
131994        if( nOpt==5 && memcmp("cache", zOpt, 5)==0 ){
131995          static struct OpenMode aCacheMode[] = {
131996            { "shared",  SQLITE_OPEN_SHAREDCACHE },
131997            { "private", SQLITE_OPEN_PRIVATECACHE },
131998            { 0, 0 }
131999          };
132000
132001          mask = SQLITE_OPEN_SHAREDCACHE|SQLITE_OPEN_PRIVATECACHE;
132002          aMode = aCacheMode;
132003          limit = mask;
132004          zModeType = "cache";
132005        }
132006        if( nOpt==4 && memcmp("mode", zOpt, 4)==0 ){
132007          static struct OpenMode aOpenMode[] = {
132008            { "ro",  SQLITE_OPEN_READONLY },
132009            { "rw",  SQLITE_OPEN_READWRITE },
132010            { "rwc", SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE },
132011            { "memory", SQLITE_OPEN_MEMORY },
132012            { 0, 0 }
132013          };
132014
132015          mask = SQLITE_OPEN_READONLY | SQLITE_OPEN_READWRITE
132016                   | SQLITE_OPEN_CREATE | SQLITE_OPEN_MEMORY;
132017          aMode = aOpenMode;
132018          limit = mask & flags;
132019          zModeType = "access";
132020        }
132021
132022        if( aMode ){
132023          int i;
132024          int mode = 0;
132025          for(i=0; aMode[i].z; i++){
132026            const char *z = aMode[i].z;
132027            if( nVal==sqlite3Strlen30(z) && 0==memcmp(zVal, z, nVal) ){
132028              mode = aMode[i].mode;
132029              break;
132030            }
132031          }
132032          if( mode==0 ){
132033            *pzErrMsg = sqlite3_mprintf("no such %s mode: %s", zModeType, zVal);
132034            rc = SQLITE_ERROR;
132035            goto parse_uri_out;
132036          }
132037          if( (mode & ~SQLITE_OPEN_MEMORY)>limit ){
132038            *pzErrMsg = sqlite3_mprintf("%s mode not allowed: %s",
132039                                        zModeType, zVal);
132040            rc = SQLITE_PERM;
132041            goto parse_uri_out;
132042          }
132043          flags = (flags & ~mask) | mode;
132044        }
132045      }
132046
132047      zOpt = &zVal[nVal+1];
132048    }
132049
132050  }else{
132051    zFile = sqlite3_malloc64(nUri+2);
132052    if( !zFile ) return SQLITE_NOMEM;
132053    memcpy(zFile, zUri, nUri);
132054    zFile[nUri] = '\0';
132055    zFile[nUri+1] = '\0';
132056    flags &= ~SQLITE_OPEN_URI;
132057  }
132058
132059  *ppVfs = sqlite3_vfs_find(zVfs);
132060  if( *ppVfs==0 ){
132061    *pzErrMsg = sqlite3_mprintf("no such vfs: %s", zVfs);
132062    rc = SQLITE_ERROR;
132063  }
132064 parse_uri_out:
132065  if( rc!=SQLITE_OK ){
132066    sqlite3_free(zFile);
132067    zFile = 0;
132068  }
132069  *pFlags = flags;
132070  *pzFile = zFile;
132071  return rc;
132072}
132073
132074
132075/*
132076** This routine does the work of opening a database on behalf of
132077** sqlite3_open() and sqlite3_open16(). The database filename "zFilename"
132078** is UTF-8 encoded.
132079*/
132080static int openDatabase(
132081  const char *zFilename, /* Database filename UTF-8 encoded */
132082  sqlite3 **ppDb,        /* OUT: Returned database handle */
132083  unsigned int flags,    /* Operational flags */
132084  const char *zVfs       /* Name of the VFS to use */
132085){
132086  sqlite3 *db;                    /* Store allocated handle here */
132087  int rc;                         /* Return code */
132088  int isThreadsafe;               /* True for threadsafe connections */
132089  char *zOpen = 0;                /* Filename argument to pass to BtreeOpen() */
132090  char *zErrMsg = 0;              /* Error message from sqlite3ParseUri() */
132091
132092#ifdef SQLITE_ENABLE_API_ARMOR
132093  if( ppDb==0 ) return SQLITE_MISUSE_BKPT;
132094#endif
132095  *ppDb = 0;
132096#ifndef SQLITE_OMIT_AUTOINIT
132097  rc = sqlite3_initialize();
132098  if( rc ) return rc;
132099#endif
132100
132101  /* Only allow sensible combinations of bits in the flags argument.
132102  ** Throw an error if any non-sense combination is used.  If we
132103  ** do not block illegal combinations here, it could trigger
132104  ** assert() statements in deeper layers.  Sensible combinations
132105  ** are:
132106  **
132107  **  1:  SQLITE_OPEN_READONLY
132108  **  2:  SQLITE_OPEN_READWRITE
132109  **  6:  SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE
132110  */
132111  assert( SQLITE_OPEN_READONLY  == 0x01 );
132112  assert( SQLITE_OPEN_READWRITE == 0x02 );
132113  assert( SQLITE_OPEN_CREATE    == 0x04 );
132114  testcase( (1<<(flags&7))==0x02 ); /* READONLY */
132115  testcase( (1<<(flags&7))==0x04 ); /* READWRITE */
132116  testcase( (1<<(flags&7))==0x40 ); /* READWRITE | CREATE */
132117  if( ((1<<(flags&7)) & 0x46)==0 ){
132118    return SQLITE_MISUSE_BKPT;  /* IMP: R-65497-44594 */
132119  }
132120
132121  if( sqlite3GlobalConfig.bCoreMutex==0 ){
132122    isThreadsafe = 0;
132123  }else if( flags & SQLITE_OPEN_NOMUTEX ){
132124    isThreadsafe = 0;
132125  }else if( flags & SQLITE_OPEN_FULLMUTEX ){
132126    isThreadsafe = 1;
132127  }else{
132128    isThreadsafe = sqlite3GlobalConfig.bFullMutex;
132129  }
132130  if( flags & SQLITE_OPEN_PRIVATECACHE ){
132131    flags &= ~SQLITE_OPEN_SHAREDCACHE;
132132  }else if( sqlite3GlobalConfig.sharedCacheEnabled ){
132133    flags |= SQLITE_OPEN_SHAREDCACHE;
132134  }
132135
132136  /* Remove harmful bits from the flags parameter
132137  **
132138  ** The SQLITE_OPEN_NOMUTEX and SQLITE_OPEN_FULLMUTEX flags were
132139  ** dealt with in the previous code block.  Besides these, the only
132140  ** valid input flags for sqlite3_open_v2() are SQLITE_OPEN_READONLY,
132141  ** SQLITE_OPEN_READWRITE, SQLITE_OPEN_CREATE, SQLITE_OPEN_SHAREDCACHE,
132142  ** SQLITE_OPEN_PRIVATECACHE, and some reserved bits.  Silently mask
132143  ** off all other flags.
132144  */
132145  flags &=  ~( SQLITE_OPEN_DELETEONCLOSE |
132146               SQLITE_OPEN_EXCLUSIVE |
132147               SQLITE_OPEN_MAIN_DB |
132148               SQLITE_OPEN_TEMP_DB |
132149               SQLITE_OPEN_TRANSIENT_DB |
132150               SQLITE_OPEN_MAIN_JOURNAL |
132151               SQLITE_OPEN_TEMP_JOURNAL |
132152               SQLITE_OPEN_SUBJOURNAL |
132153               SQLITE_OPEN_MASTER_JOURNAL |
132154               SQLITE_OPEN_NOMUTEX |
132155               SQLITE_OPEN_FULLMUTEX |
132156               SQLITE_OPEN_WAL
132157             );
132158
132159  /* Allocate the sqlite data structure */
132160  db = sqlite3MallocZero( sizeof(sqlite3) );
132161  if( db==0 ) goto opendb_out;
132162  if( isThreadsafe ){
132163    db->mutex = sqlite3MutexAlloc(SQLITE_MUTEX_RECURSIVE);
132164    if( db->mutex==0 ){
132165      sqlite3_free(db);
132166      db = 0;
132167      goto opendb_out;
132168    }
132169  }
132170  sqlite3_mutex_enter(db->mutex);
132171  db->errMask = 0xff;
132172  db->nDb = 2;
132173  db->magic = SQLITE_MAGIC_BUSY;
132174  db->aDb = db->aDbStatic;
132175
132176  assert( sizeof(db->aLimit)==sizeof(aHardLimit) );
132177  memcpy(db->aLimit, aHardLimit, sizeof(db->aLimit));
132178  db->aLimit[SQLITE_LIMIT_WORKER_THREADS] = SQLITE_DEFAULT_WORKER_THREADS;
132179  db->autoCommit = 1;
132180  db->nextAutovac = -1;
132181  db->szMmap = sqlite3GlobalConfig.szMmap;
132182  db->nextPagesize = 0;
132183  db->nMaxSorterMmap = 0x7FFFFFFF;
132184  db->flags |= SQLITE_ShortColNames | SQLITE_EnableTrigger | SQLITE_CacheSpill
132185#if !defined(SQLITE_DEFAULT_AUTOMATIC_INDEX) || SQLITE_DEFAULT_AUTOMATIC_INDEX
132186                 | SQLITE_AutoIndex
132187#endif
132188#if SQLITE_DEFAULT_CKPTFULLFSYNC
132189                 | SQLITE_CkptFullFSync
132190#endif
132191#if SQLITE_DEFAULT_FILE_FORMAT<4
132192                 | SQLITE_LegacyFileFmt
132193#endif
132194#ifdef SQLITE_ENABLE_LOAD_EXTENSION
132195                 | SQLITE_LoadExtension
132196#endif
132197#if SQLITE_DEFAULT_RECURSIVE_TRIGGERS
132198                 | SQLITE_RecTriggers
132199#endif
132200#if defined(SQLITE_DEFAULT_FOREIGN_KEYS) && SQLITE_DEFAULT_FOREIGN_KEYS
132201                 | SQLITE_ForeignKeys
132202#endif
132203#if defined(SQLITE_REVERSE_UNORDERED_SELECTS)
132204                 | SQLITE_ReverseOrder
132205#endif
132206#if defined(SQLITE_ENABLE_OVERSIZE_CELL_CHECK)
132207                 | SQLITE_CellSizeCk
132208#endif
132209      ;
132210  sqlite3HashInit(&db->aCollSeq);
132211#ifndef SQLITE_OMIT_VIRTUALTABLE
132212  sqlite3HashInit(&db->aModule);
132213#endif
132214
132215  /* Add the default collation sequence BINARY. BINARY works for both UTF-8
132216  ** and UTF-16, so add a version for each to avoid any unnecessary
132217  ** conversions. The only error that can occur here is a malloc() failure.
132218  **
132219  ** EVIDENCE-OF: R-52786-44878 SQLite defines three built-in collating
132220  ** functions:
132221  */
132222  createCollation(db, "BINARY", SQLITE_UTF8, 0, binCollFunc, 0);
132223  createCollation(db, "BINARY", SQLITE_UTF16BE, 0, binCollFunc, 0);
132224  createCollation(db, "BINARY", SQLITE_UTF16LE, 0, binCollFunc, 0);
132225  createCollation(db, "NOCASE", SQLITE_UTF8, 0, nocaseCollatingFunc, 0);
132226  createCollation(db, "RTRIM", SQLITE_UTF8, (void*)1, binCollFunc, 0);
132227  if( db->mallocFailed ){
132228    goto opendb_out;
132229  }
132230  /* EVIDENCE-OF: R-08308-17224 The default collating function for all
132231  ** strings is BINARY.
132232  */
132233  db->pDfltColl = sqlite3FindCollSeq(db, SQLITE_UTF8, "BINARY", 0);
132234  assert( db->pDfltColl!=0 );
132235
132236  /* Parse the filename/URI argument. */
132237  db->openFlags = flags;
132238  rc = sqlite3ParseUri(zVfs, zFilename, &flags, &db->pVfs, &zOpen, &zErrMsg);
132239  if( rc!=SQLITE_OK ){
132240    if( rc==SQLITE_NOMEM ) db->mallocFailed = 1;
132241    sqlite3ErrorWithMsg(db, rc, zErrMsg ? "%s" : 0, zErrMsg);
132242    sqlite3_free(zErrMsg);
132243    goto opendb_out;
132244  }
132245
132246  /* Open the backend database driver */
132247  rc = sqlite3BtreeOpen(db->pVfs, zOpen, db, &db->aDb[0].pBt, 0,
132248                        flags | SQLITE_OPEN_MAIN_DB);
132249  if( rc!=SQLITE_OK ){
132250    if( rc==SQLITE_IOERR_NOMEM ){
132251      rc = SQLITE_NOMEM;
132252    }
132253    sqlite3Error(db, rc);
132254    goto opendb_out;
132255  }
132256  sqlite3BtreeEnter(db->aDb[0].pBt);
132257  db->aDb[0].pSchema = sqlite3SchemaGet(db, db->aDb[0].pBt);
132258  if( !db->mallocFailed ) ENC(db) = SCHEMA_ENC(db);
132259  sqlite3BtreeLeave(db->aDb[0].pBt);
132260  db->aDb[1].pSchema = sqlite3SchemaGet(db, 0);
132261
132262  /* The default safety_level for the main database is 'full'; for the temp
132263  ** database it is 'NONE'. This matches the pager layer defaults.
132264  */
132265  db->aDb[0].zName = "main";
132266  db->aDb[0].safety_level = 3;
132267  db->aDb[1].zName = "temp";
132268  db->aDb[1].safety_level = 1;
132269
132270  db->magic = SQLITE_MAGIC_OPEN;
132271  if( db->mallocFailed ){
132272    goto opendb_out;
132273  }
132274
132275  /* Register all built-in functions, but do not attempt to read the
132276  ** database schema yet. This is delayed until the first time the database
132277  ** is accessed.
132278  */
132279  sqlite3Error(db, SQLITE_OK);
132280  sqlite3RegisterBuiltinFunctions(db);
132281
132282  /* Load automatic extensions - extensions that have been registered
132283  ** using the sqlite3_automatic_extension() API.
132284  */
132285  rc = sqlite3_errcode(db);
132286  if( rc==SQLITE_OK ){
132287    sqlite3AutoLoadExtensions(db);
132288    rc = sqlite3_errcode(db);
132289    if( rc!=SQLITE_OK ){
132290      goto opendb_out;
132291    }
132292  }
132293
132294#ifdef SQLITE_ENABLE_FTS1
132295  if( !db->mallocFailed ){
132296    extern int sqlite3Fts1Init(sqlite3*);
132297    rc = sqlite3Fts1Init(db);
132298  }
132299#endif
132300
132301#ifdef SQLITE_ENABLE_FTS2
132302  if( !db->mallocFailed && rc==SQLITE_OK ){
132303    extern int sqlite3Fts2Init(sqlite3*);
132304    rc = sqlite3Fts2Init(db);
132305  }
132306#endif
132307
132308#ifdef SQLITE_ENABLE_FTS3
132309  if( !db->mallocFailed && rc==SQLITE_OK ){
132310    rc = sqlite3Fts3Init(db);
132311  }
132312#endif
132313
132314#ifdef SQLITE_ENABLE_ICU
132315  if( !db->mallocFailed && rc==SQLITE_OK ){
132316    rc = sqlite3IcuInit(db);
132317  }
132318#endif
132319
132320#ifdef SQLITE_ENABLE_RTREE
132321  if( !db->mallocFailed && rc==SQLITE_OK){
132322    rc = sqlite3RtreeInit(db);
132323  }
132324#endif
132325
132326#ifdef SQLITE_ENABLE_DBSTAT_VTAB
132327  if( !db->mallocFailed && rc==SQLITE_OK){
132328    rc = sqlite3DbstatRegister(db);
132329  }
132330#endif
132331
132332  /* -DSQLITE_DEFAULT_LOCKING_MODE=1 makes EXCLUSIVE the default locking
132333  ** mode.  -DSQLITE_DEFAULT_LOCKING_MODE=0 make NORMAL the default locking
132334  ** mode.  Doing nothing at all also makes NORMAL the default.
132335  */
132336#ifdef SQLITE_DEFAULT_LOCKING_MODE
132337  db->dfltLockMode = SQLITE_DEFAULT_LOCKING_MODE;
132338  sqlite3PagerLockingMode(sqlite3BtreePager(db->aDb[0].pBt),
132339                          SQLITE_DEFAULT_LOCKING_MODE);
132340#endif
132341
132342  if( rc ) sqlite3Error(db, rc);
132343
132344  /* Enable the lookaside-malloc subsystem */
132345  setupLookaside(db, 0, sqlite3GlobalConfig.szLookaside,
132346                        sqlite3GlobalConfig.nLookaside);
132347
132348  sqlite3_wal_autocheckpoint(db, SQLITE_DEFAULT_WAL_AUTOCHECKPOINT);
132349
132350opendb_out:
132351  sqlite3_free(zOpen);
132352  if( db ){
132353    assert( db->mutex!=0 || isThreadsafe==0
132354           || sqlite3GlobalConfig.bFullMutex==0 );
132355    sqlite3_mutex_leave(db->mutex);
132356  }
132357  rc = sqlite3_errcode(db);
132358  assert( db!=0 || rc==SQLITE_NOMEM );
132359  if( rc==SQLITE_NOMEM ){
132360    sqlite3_close(db);
132361    db = 0;
132362  }else if( rc!=SQLITE_OK ){
132363    db->magic = SQLITE_MAGIC_SICK;
132364  }
132365  *ppDb = db;
132366#ifdef SQLITE_ENABLE_SQLLOG
132367  if( sqlite3GlobalConfig.xSqllog ){
132368    /* Opening a db handle. Fourth parameter is passed 0. */
132369    void *pArg = sqlite3GlobalConfig.pSqllogArg;
132370    sqlite3GlobalConfig.xSqllog(pArg, db, zFilename, 0);
132371  }
132372#endif
132373  return rc & 0xff;
132374}
132375
132376/*
132377** Open a new database handle.
132378*/
132379SQLITE_API int SQLITE_STDCALL sqlite3_open(
132380  const char *zFilename,
132381  sqlite3 **ppDb
132382){
132383  return openDatabase(zFilename, ppDb,
132384                      SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, 0);
132385}
132386SQLITE_API int SQLITE_STDCALL sqlite3_open_v2(
132387  const char *filename,   /* Database filename (UTF-8) */
132388  sqlite3 **ppDb,         /* OUT: SQLite db handle */
132389  int flags,              /* Flags */
132390  const char *zVfs        /* Name of VFS module to use */
132391){
132392  return openDatabase(filename, ppDb, (unsigned int)flags, zVfs);
132393}
132394
132395#ifndef SQLITE_OMIT_UTF16
132396/*
132397** Open a new database handle.
132398*/
132399SQLITE_API int SQLITE_STDCALL sqlite3_open16(
132400  const void *zFilename,
132401  sqlite3 **ppDb
132402){
132403  char const *zFilename8;   /* zFilename encoded in UTF-8 instead of UTF-16 */
132404  sqlite3_value *pVal;
132405  int rc;
132406
132407#ifdef SQLITE_ENABLE_API_ARMOR
132408  if( ppDb==0 ) return SQLITE_MISUSE_BKPT;
132409#endif
132410  *ppDb = 0;
132411#ifndef SQLITE_OMIT_AUTOINIT
132412  rc = sqlite3_initialize();
132413  if( rc ) return rc;
132414#endif
132415  if( zFilename==0 ) zFilename = "\000\000";
132416  pVal = sqlite3ValueNew(0);
132417  sqlite3ValueSetStr(pVal, -1, zFilename, SQLITE_UTF16NATIVE, SQLITE_STATIC);
132418  zFilename8 = sqlite3ValueText(pVal, SQLITE_UTF8);
132419  if( zFilename8 ){
132420    rc = openDatabase(zFilename8, ppDb,
132421                      SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, 0);
132422    assert( *ppDb || rc==SQLITE_NOMEM );
132423    if( rc==SQLITE_OK && !DbHasProperty(*ppDb, 0, DB_SchemaLoaded) ){
132424      SCHEMA_ENC(*ppDb) = ENC(*ppDb) = SQLITE_UTF16NATIVE;
132425    }
132426  }else{
132427    rc = SQLITE_NOMEM;
132428  }
132429  sqlite3ValueFree(pVal);
132430
132431  return rc & 0xff;
132432}
132433#endif /* SQLITE_OMIT_UTF16 */
132434
132435/*
132436** Register a new collation sequence with the database handle db.
132437*/
132438SQLITE_API int SQLITE_STDCALL sqlite3_create_collation(
132439  sqlite3* db,
132440  const char *zName,
132441  int enc,
132442  void* pCtx,
132443  int(*xCompare)(void*,int,const void*,int,const void*)
132444){
132445  return sqlite3_create_collation_v2(db, zName, enc, pCtx, xCompare, 0);
132446}
132447
132448/*
132449** Register a new collation sequence with the database handle db.
132450*/
132451SQLITE_API int SQLITE_STDCALL sqlite3_create_collation_v2(
132452  sqlite3* db,
132453  const char *zName,
132454  int enc,
132455  void* pCtx,
132456  int(*xCompare)(void*,int,const void*,int,const void*),
132457  void(*xDel)(void*)
132458){
132459  int rc;
132460
132461#ifdef SQLITE_ENABLE_API_ARMOR
132462  if( !sqlite3SafetyCheckOk(db) || zName==0 ) return SQLITE_MISUSE_BKPT;
132463#endif
132464  sqlite3_mutex_enter(db->mutex);
132465  assert( !db->mallocFailed );
132466  rc = createCollation(db, zName, (u8)enc, pCtx, xCompare, xDel);
132467  rc = sqlite3ApiExit(db, rc);
132468  sqlite3_mutex_leave(db->mutex);
132469  return rc;
132470}
132471
132472#ifndef SQLITE_OMIT_UTF16
132473/*
132474** Register a new collation sequence with the database handle db.
132475*/
132476SQLITE_API int SQLITE_STDCALL sqlite3_create_collation16(
132477  sqlite3* db,
132478  const void *zName,
132479  int enc,
132480  void* pCtx,
132481  int(*xCompare)(void*,int,const void*,int,const void*)
132482){
132483  int rc = SQLITE_OK;
132484  char *zName8;
132485
132486#ifdef SQLITE_ENABLE_API_ARMOR
132487  if( !sqlite3SafetyCheckOk(db) || zName==0 ) return SQLITE_MISUSE_BKPT;
132488#endif
132489  sqlite3_mutex_enter(db->mutex);
132490  assert( !db->mallocFailed );
132491  zName8 = sqlite3Utf16to8(db, zName, -1, SQLITE_UTF16NATIVE);
132492  if( zName8 ){
132493    rc = createCollation(db, zName8, (u8)enc, pCtx, xCompare, 0);
132494    sqlite3DbFree(db, zName8);
132495  }
132496  rc = sqlite3ApiExit(db, rc);
132497  sqlite3_mutex_leave(db->mutex);
132498  return rc;
132499}
132500#endif /* SQLITE_OMIT_UTF16 */
132501
132502/*
132503** Register a collation sequence factory callback with the database handle
132504** db. Replace any previously installed collation sequence factory.
132505*/
132506SQLITE_API int SQLITE_STDCALL sqlite3_collation_needed(
132507  sqlite3 *db,
132508  void *pCollNeededArg,
132509  void(*xCollNeeded)(void*,sqlite3*,int eTextRep,const char*)
132510){
132511#ifdef SQLITE_ENABLE_API_ARMOR
132512  if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT;
132513#endif
132514  sqlite3_mutex_enter(db->mutex);
132515  db->xCollNeeded = xCollNeeded;
132516  db->xCollNeeded16 = 0;
132517  db->pCollNeededArg = pCollNeededArg;
132518  sqlite3_mutex_leave(db->mutex);
132519  return SQLITE_OK;
132520}
132521
132522#ifndef SQLITE_OMIT_UTF16
132523/*
132524** Register a collation sequence factory callback with the database handle
132525** db. Replace any previously installed collation sequence factory.
132526*/
132527SQLITE_API int SQLITE_STDCALL sqlite3_collation_needed16(
132528  sqlite3 *db,
132529  void *pCollNeededArg,
132530  void(*xCollNeeded16)(void*,sqlite3*,int eTextRep,const void*)
132531){
132532#ifdef SQLITE_ENABLE_API_ARMOR
132533  if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT;
132534#endif
132535  sqlite3_mutex_enter(db->mutex);
132536  db->xCollNeeded = 0;
132537  db->xCollNeeded16 = xCollNeeded16;
132538  db->pCollNeededArg = pCollNeededArg;
132539  sqlite3_mutex_leave(db->mutex);
132540  return SQLITE_OK;
132541}
132542#endif /* SQLITE_OMIT_UTF16 */
132543
132544#ifndef SQLITE_OMIT_DEPRECATED
132545/*
132546** This function is now an anachronism. It used to be used to recover from a
132547** malloc() failure, but SQLite now does this automatically.
132548*/
132549SQLITE_API int SQLITE_STDCALL sqlite3_global_recover(void){
132550  return SQLITE_OK;
132551}
132552#endif
132553
132554/*
132555** Test to see whether or not the database connection is in autocommit
132556** mode.  Return TRUE if it is and FALSE if not.  Autocommit mode is on
132557** by default.  Autocommit is disabled by a BEGIN statement and reenabled
132558** by the next COMMIT or ROLLBACK.
132559*/
132560SQLITE_API int SQLITE_STDCALL sqlite3_get_autocommit(sqlite3 *db){
132561#ifdef SQLITE_ENABLE_API_ARMOR
132562  if( !sqlite3SafetyCheckOk(db) ){
132563    (void)SQLITE_MISUSE_BKPT;
132564    return 0;
132565  }
132566#endif
132567  return db->autoCommit;
132568}
132569
132570/*
132571** The following routines are substitutes for constants SQLITE_CORRUPT,
132572** SQLITE_MISUSE, SQLITE_CANTOPEN, SQLITE_IOERR and possibly other error
132573** constants.  They serve two purposes:
132574**
132575**   1.  Serve as a convenient place to set a breakpoint in a debugger
132576**       to detect when version error conditions occurs.
132577**
132578**   2.  Invoke sqlite3_log() to provide the source code location where
132579**       a low-level error is first detected.
132580*/
132581SQLITE_PRIVATE int sqlite3CorruptError(int lineno){
132582  testcase( sqlite3GlobalConfig.xLog!=0 );
132583  sqlite3_log(SQLITE_CORRUPT,
132584              "database corruption at line %d of [%.10s]",
132585              lineno, 20+sqlite3_sourceid());
132586  return SQLITE_CORRUPT;
132587}
132588SQLITE_PRIVATE int sqlite3MisuseError(int lineno){
132589  testcase( sqlite3GlobalConfig.xLog!=0 );
132590  sqlite3_log(SQLITE_MISUSE,
132591              "misuse at line %d of [%.10s]",
132592              lineno, 20+sqlite3_sourceid());
132593  return SQLITE_MISUSE;
132594}
132595SQLITE_PRIVATE int sqlite3CantopenError(int lineno){
132596  testcase( sqlite3GlobalConfig.xLog!=0 );
132597  sqlite3_log(SQLITE_CANTOPEN,
132598              "cannot open file at line %d of [%.10s]",
132599              lineno, 20+sqlite3_sourceid());
132600  return SQLITE_CANTOPEN;
132601}
132602
132603
132604#ifndef SQLITE_OMIT_DEPRECATED
132605/*
132606** This is a convenience routine that makes sure that all thread-specific
132607** data for this thread has been deallocated.
132608**
132609** SQLite no longer uses thread-specific data so this routine is now a
132610** no-op.  It is retained for historical compatibility.
132611*/
132612SQLITE_API void SQLITE_STDCALL sqlite3_thread_cleanup(void){
132613}
132614#endif
132615
132616/*
132617** Return meta information about a specific column of a database table.
132618** See comment in sqlite3.h (sqlite.h.in) for details.
132619*/
132620SQLITE_API int SQLITE_STDCALL sqlite3_table_column_metadata(
132621  sqlite3 *db,                /* Connection handle */
132622  const char *zDbName,        /* Database name or NULL */
132623  const char *zTableName,     /* Table name */
132624  const char *zColumnName,    /* Column name */
132625  char const **pzDataType,    /* OUTPUT: Declared data type */
132626  char const **pzCollSeq,     /* OUTPUT: Collation sequence name */
132627  int *pNotNull,              /* OUTPUT: True if NOT NULL constraint exists */
132628  int *pPrimaryKey,           /* OUTPUT: True if column part of PK */
132629  int *pAutoinc               /* OUTPUT: True if column is auto-increment */
132630){
132631  int rc;
132632  char *zErrMsg = 0;
132633  Table *pTab = 0;
132634  Column *pCol = 0;
132635  int iCol = 0;
132636  char const *zDataType = 0;
132637  char const *zCollSeq = 0;
132638  int notnull = 0;
132639  int primarykey = 0;
132640  int autoinc = 0;
132641
132642
132643#ifdef SQLITE_ENABLE_API_ARMOR
132644  if( !sqlite3SafetyCheckOk(db) || zTableName==0 ){
132645    return SQLITE_MISUSE_BKPT;
132646  }
132647#endif
132648
132649  /* Ensure the database schema has been loaded */
132650  sqlite3_mutex_enter(db->mutex);
132651  sqlite3BtreeEnterAll(db);
132652  rc = sqlite3Init(db, &zErrMsg);
132653  if( SQLITE_OK!=rc ){
132654    goto error_out;
132655  }
132656
132657  /* Locate the table in question */
132658  pTab = sqlite3FindTable(db, zTableName, zDbName);
132659  if( !pTab || pTab->pSelect ){
132660    pTab = 0;
132661    goto error_out;
132662  }
132663
132664  /* Find the column for which info is requested */
132665  if( zColumnName==0 ){
132666    /* Query for existance of table only */
132667  }else{
132668    for(iCol=0; iCol<pTab->nCol; iCol++){
132669      pCol = &pTab->aCol[iCol];
132670      if( 0==sqlite3StrICmp(pCol->zName, zColumnName) ){
132671        break;
132672      }
132673    }
132674    if( iCol==pTab->nCol ){
132675      if( HasRowid(pTab) && sqlite3IsRowid(zColumnName) ){
132676        iCol = pTab->iPKey;
132677        pCol = iCol>=0 ? &pTab->aCol[iCol] : 0;
132678      }else{
132679        pTab = 0;
132680        goto error_out;
132681      }
132682    }
132683  }
132684
132685  /* The following block stores the meta information that will be returned
132686  ** to the caller in local variables zDataType, zCollSeq, notnull, primarykey
132687  ** and autoinc. At this point there are two possibilities:
132688  **
132689  **     1. The specified column name was rowid", "oid" or "_rowid_"
132690  **        and there is no explicitly declared IPK column.
132691  **
132692  **     2. The table is not a view and the column name identified an
132693  **        explicitly declared column. Copy meta information from *pCol.
132694  */
132695  if( pCol ){
132696    zDataType = pCol->zType;
132697    zCollSeq = pCol->zColl;
132698    notnull = pCol->notNull!=0;
132699    primarykey  = (pCol->colFlags & COLFLAG_PRIMKEY)!=0;
132700    autoinc = pTab->iPKey==iCol && (pTab->tabFlags & TF_Autoincrement)!=0;
132701  }else{
132702    zDataType = "INTEGER";
132703    primarykey = 1;
132704  }
132705  if( !zCollSeq ){
132706    zCollSeq = "BINARY";
132707  }
132708
132709error_out:
132710  sqlite3BtreeLeaveAll(db);
132711
132712  /* Whether the function call succeeded or failed, set the output parameters
132713  ** to whatever their local counterparts contain. If an error did occur,
132714  ** this has the effect of zeroing all output parameters.
132715  */
132716  if( pzDataType ) *pzDataType = zDataType;
132717  if( pzCollSeq ) *pzCollSeq = zCollSeq;
132718  if( pNotNull ) *pNotNull = notnull;
132719  if( pPrimaryKey ) *pPrimaryKey = primarykey;
132720  if( pAutoinc ) *pAutoinc = autoinc;
132721
132722  if( SQLITE_OK==rc && !pTab ){
132723    sqlite3DbFree(db, zErrMsg);
132724    zErrMsg = sqlite3MPrintf(db, "no such table column: %s.%s", zTableName,
132725        zColumnName);
132726    rc = SQLITE_ERROR;
132727  }
132728  sqlite3ErrorWithMsg(db, rc, (zErrMsg?"%s":0), zErrMsg);
132729  sqlite3DbFree(db, zErrMsg);
132730  rc = sqlite3ApiExit(db, rc);
132731  sqlite3_mutex_leave(db->mutex);
132732  return rc;
132733}
132734
132735/*
132736** Sleep for a little while.  Return the amount of time slept.
132737*/
132738SQLITE_API int SQLITE_STDCALL sqlite3_sleep(int ms){
132739  sqlite3_vfs *pVfs;
132740  int rc;
132741  pVfs = sqlite3_vfs_find(0);
132742  if( pVfs==0 ) return 0;
132743
132744  /* This function works in milliseconds, but the underlying OsSleep()
132745  ** API uses microseconds. Hence the 1000's.
132746  */
132747  rc = (sqlite3OsSleep(pVfs, 1000*ms)/1000);
132748  return rc;
132749}
132750
132751/*
132752** Enable or disable the extended result codes.
132753*/
132754SQLITE_API int SQLITE_STDCALL sqlite3_extended_result_codes(sqlite3 *db, int onoff){
132755#ifdef SQLITE_ENABLE_API_ARMOR
132756  if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT;
132757#endif
132758  sqlite3_mutex_enter(db->mutex);
132759  db->errMask = onoff ? 0xffffffff : 0xff;
132760  sqlite3_mutex_leave(db->mutex);
132761  return SQLITE_OK;
132762}
132763
132764/*
132765** Invoke the xFileControl method on a particular database.
132766*/
132767SQLITE_API int SQLITE_STDCALL sqlite3_file_control(sqlite3 *db, const char *zDbName, int op, void *pArg){
132768  int rc = SQLITE_ERROR;
132769  Btree *pBtree;
132770
132771#ifdef SQLITE_ENABLE_API_ARMOR
132772  if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT;
132773#endif
132774  sqlite3_mutex_enter(db->mutex);
132775  pBtree = sqlite3DbNameToBtree(db, zDbName);
132776  if( pBtree ){
132777    Pager *pPager;
132778    sqlite3_file *fd;
132779    sqlite3BtreeEnter(pBtree);
132780    pPager = sqlite3BtreePager(pBtree);
132781    assert( pPager!=0 );
132782    fd = sqlite3PagerFile(pPager);
132783    assert( fd!=0 );
132784    if( op==SQLITE_FCNTL_FILE_POINTER ){
132785      *(sqlite3_file**)pArg = fd;
132786      rc = SQLITE_OK;
132787    }else if( fd->pMethods ){
132788      rc = sqlite3OsFileControl(fd, op, pArg);
132789    }else{
132790      rc = SQLITE_NOTFOUND;
132791    }
132792    sqlite3BtreeLeave(pBtree);
132793  }
132794  sqlite3_mutex_leave(db->mutex);
132795  return rc;
132796}
132797
132798/*
132799** Interface to the testing logic.
132800*/
132801SQLITE_API int SQLITE_CDECL sqlite3_test_control(int op, ...){
132802  int rc = 0;
132803#ifdef SQLITE_OMIT_BUILTIN_TEST
132804  UNUSED_PARAMETER(op);
132805#else
132806  va_list ap;
132807  va_start(ap, op);
132808  switch( op ){
132809
132810    /*
132811    ** Save the current state of the PRNG.
132812    */
132813    case SQLITE_TESTCTRL_PRNG_SAVE: {
132814      sqlite3PrngSaveState();
132815      break;
132816    }
132817
132818    /*
132819    ** Restore the state of the PRNG to the last state saved using
132820    ** PRNG_SAVE.  If PRNG_SAVE has never before been called, then
132821    ** this verb acts like PRNG_RESET.
132822    */
132823    case SQLITE_TESTCTRL_PRNG_RESTORE: {
132824      sqlite3PrngRestoreState();
132825      break;
132826    }
132827
132828    /*
132829    ** Reset the PRNG back to its uninitialized state.  The next call
132830    ** to sqlite3_randomness() will reseed the PRNG using a single call
132831    ** to the xRandomness method of the default VFS.
132832    */
132833    case SQLITE_TESTCTRL_PRNG_RESET: {
132834      sqlite3_randomness(0,0);
132835      break;
132836    }
132837
132838    /*
132839    **  sqlite3_test_control(BITVEC_TEST, size, program)
132840    **
132841    ** Run a test against a Bitvec object of size.  The program argument
132842    ** is an array of integers that defines the test.  Return -1 on a
132843    ** memory allocation error, 0 on success, or non-zero for an error.
132844    ** See the sqlite3BitvecBuiltinTest() for additional information.
132845    */
132846    case SQLITE_TESTCTRL_BITVEC_TEST: {
132847      int sz = va_arg(ap, int);
132848      int *aProg = va_arg(ap, int*);
132849      rc = sqlite3BitvecBuiltinTest(sz, aProg);
132850      break;
132851    }
132852
132853    /*
132854    **  sqlite3_test_control(FAULT_INSTALL, xCallback)
132855    **
132856    ** Arrange to invoke xCallback() whenever sqlite3FaultSim() is called,
132857    ** if xCallback is not NULL.
132858    **
132859    ** As a test of the fault simulator mechanism itself, sqlite3FaultSim(0)
132860    ** is called immediately after installing the new callback and the return
132861    ** value from sqlite3FaultSim(0) becomes the return from
132862    ** sqlite3_test_control().
132863    */
132864    case SQLITE_TESTCTRL_FAULT_INSTALL: {
132865      /* MSVC is picky about pulling func ptrs from va lists.
132866      ** http://support.microsoft.com/kb/47961
132867      ** sqlite3GlobalConfig.xTestCallback = va_arg(ap, int(*)(int));
132868      */
132869      typedef int(*TESTCALLBACKFUNC_t)(int);
132870      sqlite3GlobalConfig.xTestCallback = va_arg(ap, TESTCALLBACKFUNC_t);
132871      rc = sqlite3FaultSim(0);
132872      break;
132873    }
132874
132875    /*
132876    **  sqlite3_test_control(BENIGN_MALLOC_HOOKS, xBegin, xEnd)
132877    **
132878    ** Register hooks to call to indicate which malloc() failures
132879    ** are benign.
132880    */
132881    case SQLITE_TESTCTRL_BENIGN_MALLOC_HOOKS: {
132882      typedef void (*void_function)(void);
132883      void_function xBenignBegin;
132884      void_function xBenignEnd;
132885      xBenignBegin = va_arg(ap, void_function);
132886      xBenignEnd = va_arg(ap, void_function);
132887      sqlite3BenignMallocHooks(xBenignBegin, xBenignEnd);
132888      break;
132889    }
132890
132891    /*
132892    **  sqlite3_test_control(SQLITE_TESTCTRL_PENDING_BYTE, unsigned int X)
132893    **
132894    ** Set the PENDING byte to the value in the argument, if X>0.
132895    ** Make no changes if X==0.  Return the value of the pending byte
132896    ** as it existing before this routine was called.
132897    **
132898    ** IMPORTANT:  Changing the PENDING byte from 0x40000000 results in
132899    ** an incompatible database file format.  Changing the PENDING byte
132900    ** while any database connection is open results in undefined and
132901    ** deleterious behavior.
132902    */
132903    case SQLITE_TESTCTRL_PENDING_BYTE: {
132904      rc = PENDING_BYTE;
132905#ifndef SQLITE_OMIT_WSD
132906      {
132907        unsigned int newVal = va_arg(ap, unsigned int);
132908        if( newVal ) sqlite3PendingByte = newVal;
132909      }
132910#endif
132911      break;
132912    }
132913
132914    /*
132915    **  sqlite3_test_control(SQLITE_TESTCTRL_ASSERT, int X)
132916    **
132917    ** This action provides a run-time test to see whether or not
132918    ** assert() was enabled at compile-time.  If X is true and assert()
132919    ** is enabled, then the return value is true.  If X is true and
132920    ** assert() is disabled, then the return value is zero.  If X is
132921    ** false and assert() is enabled, then the assertion fires and the
132922    ** process aborts.  If X is false and assert() is disabled, then the
132923    ** return value is zero.
132924    */
132925    case SQLITE_TESTCTRL_ASSERT: {
132926      volatile int x = 0;
132927      assert( (x = va_arg(ap,int))!=0 );
132928      rc = x;
132929      break;
132930    }
132931
132932
132933    /*
132934    **  sqlite3_test_control(SQLITE_TESTCTRL_ALWAYS, int X)
132935    **
132936    ** This action provides a run-time test to see how the ALWAYS and
132937    ** NEVER macros were defined at compile-time.
132938    **
132939    ** The return value is ALWAYS(X).
132940    **
132941    ** The recommended test is X==2.  If the return value is 2, that means
132942    ** ALWAYS() and NEVER() are both no-op pass-through macros, which is the
132943    ** default setting.  If the return value is 1, then ALWAYS() is either
132944    ** hard-coded to true or else it asserts if its argument is false.
132945    ** The first behavior (hard-coded to true) is the case if
132946    ** SQLITE_TESTCTRL_ASSERT shows that assert() is disabled and the second
132947    ** behavior (assert if the argument to ALWAYS() is false) is the case if
132948    ** SQLITE_TESTCTRL_ASSERT shows that assert() is enabled.
132949    **
132950    ** The run-time test procedure might look something like this:
132951    **
132952    **    if( sqlite3_test_control(SQLITE_TESTCTRL_ALWAYS, 2)==2 ){
132953    **      // ALWAYS() and NEVER() are no-op pass-through macros
132954    **    }else if( sqlite3_test_control(SQLITE_TESTCTRL_ASSERT, 1) ){
132955    **      // ALWAYS(x) asserts that x is true. NEVER(x) asserts x is false.
132956    **    }else{
132957    **      // ALWAYS(x) is a constant 1.  NEVER(x) is a constant 0.
132958    **    }
132959    */
132960    case SQLITE_TESTCTRL_ALWAYS: {
132961      int x = va_arg(ap,int);
132962      rc = ALWAYS(x);
132963      break;
132964    }
132965
132966    /*
132967    **   sqlite3_test_control(SQLITE_TESTCTRL_BYTEORDER);
132968    **
132969    ** The integer returned reveals the byte-order of the computer on which
132970    ** SQLite is running:
132971    **
132972    **       1     big-endian,    determined at run-time
132973    **      10     little-endian, determined at run-time
132974    **  432101     big-endian,    determined at compile-time
132975    **  123410     little-endian, determined at compile-time
132976    */
132977    case SQLITE_TESTCTRL_BYTEORDER: {
132978      rc = SQLITE_BYTEORDER*100 + SQLITE_LITTLEENDIAN*10 + SQLITE_BIGENDIAN;
132979      break;
132980    }
132981
132982    /*   sqlite3_test_control(SQLITE_TESTCTRL_RESERVE, sqlite3 *db, int N)
132983    **
132984    ** Set the nReserve size to N for the main database on the database
132985    ** connection db.
132986    */
132987    case SQLITE_TESTCTRL_RESERVE: {
132988      sqlite3 *db = va_arg(ap, sqlite3*);
132989      int x = va_arg(ap,int);
132990      sqlite3_mutex_enter(db->mutex);
132991      sqlite3BtreeSetPageSize(db->aDb[0].pBt, 0, x, 0);
132992      sqlite3_mutex_leave(db->mutex);
132993      break;
132994    }
132995
132996    /*  sqlite3_test_control(SQLITE_TESTCTRL_OPTIMIZATIONS, sqlite3 *db, int N)
132997    **
132998    ** Enable or disable various optimizations for testing purposes.  The
132999    ** argument N is a bitmask of optimizations to be disabled.  For normal
133000    ** operation N should be 0.  The idea is that a test program (like the
133001    ** SQL Logic Test or SLT test module) can run the same SQL multiple times
133002    ** with various optimizations disabled to verify that the same answer
133003    ** is obtained in every case.
133004    */
133005    case SQLITE_TESTCTRL_OPTIMIZATIONS: {
133006      sqlite3 *db = va_arg(ap, sqlite3*);
133007      db->dbOptFlags = (u16)(va_arg(ap, int) & 0xffff);
133008      break;
133009    }
133010
133011#ifdef SQLITE_N_KEYWORD
133012    /* sqlite3_test_control(SQLITE_TESTCTRL_ISKEYWORD, const char *zWord)
133013    **
133014    ** If zWord is a keyword recognized by the parser, then return the
133015    ** number of keywords.  Or if zWord is not a keyword, return 0.
133016    **
133017    ** This test feature is only available in the amalgamation since
133018    ** the SQLITE_N_KEYWORD macro is not defined in this file if SQLite
133019    ** is built using separate source files.
133020    */
133021    case SQLITE_TESTCTRL_ISKEYWORD: {
133022      const char *zWord = va_arg(ap, const char*);
133023      int n = sqlite3Strlen30(zWord);
133024      rc = (sqlite3KeywordCode((u8*)zWord, n)!=TK_ID) ? SQLITE_N_KEYWORD : 0;
133025      break;
133026    }
133027#endif
133028
133029    /* sqlite3_test_control(SQLITE_TESTCTRL_SCRATCHMALLOC, sz, &pNew, pFree);
133030    **
133031    ** Pass pFree into sqlite3ScratchFree().
133032    ** If sz>0 then allocate a scratch buffer into pNew.
133033    */
133034    case SQLITE_TESTCTRL_SCRATCHMALLOC: {
133035      void *pFree, **ppNew;
133036      int sz;
133037      sz = va_arg(ap, int);
133038      ppNew = va_arg(ap, void**);
133039      pFree = va_arg(ap, void*);
133040      if( sz ) *ppNew = sqlite3ScratchMalloc(sz);
133041      sqlite3ScratchFree(pFree);
133042      break;
133043    }
133044
133045    /*   sqlite3_test_control(SQLITE_TESTCTRL_LOCALTIME_FAULT, int onoff);
133046    **
133047    ** If parameter onoff is non-zero, configure the wrappers so that all
133048    ** subsequent calls to localtime() and variants fail. If onoff is zero,
133049    ** undo this setting.
133050    */
133051    case SQLITE_TESTCTRL_LOCALTIME_FAULT: {
133052      sqlite3GlobalConfig.bLocaltimeFault = va_arg(ap, int);
133053      break;
133054    }
133055
133056    /*   sqlite3_test_control(SQLITE_TESTCTRL_NEVER_CORRUPT, int);
133057    **
133058    ** Set or clear a flag that indicates that the database file is always well-
133059    ** formed and never corrupt.  This flag is clear by default, indicating that
133060    ** database files might have arbitrary corruption.  Setting the flag during
133061    ** testing causes certain assert() statements in the code to be activated
133062    ** that demonstrat invariants on well-formed database files.
133063    */
133064    case SQLITE_TESTCTRL_NEVER_CORRUPT: {
133065      sqlite3GlobalConfig.neverCorrupt = va_arg(ap, int);
133066      break;
133067    }
133068
133069
133070    /*   sqlite3_test_control(SQLITE_TESTCTRL_VDBE_COVERAGE, xCallback, ptr);
133071    **
133072    ** Set the VDBE coverage callback function to xCallback with context
133073    ** pointer ptr.
133074    */
133075    case SQLITE_TESTCTRL_VDBE_COVERAGE: {
133076#ifdef SQLITE_VDBE_COVERAGE
133077      typedef void (*branch_callback)(void*,int,u8,u8);
133078      sqlite3GlobalConfig.xVdbeBranch = va_arg(ap,branch_callback);
133079      sqlite3GlobalConfig.pVdbeBranchArg = va_arg(ap,void*);
133080#endif
133081      break;
133082    }
133083
133084    /*   sqlite3_test_control(SQLITE_TESTCTRL_SORTER_MMAP, db, nMax); */
133085    case SQLITE_TESTCTRL_SORTER_MMAP: {
133086      sqlite3 *db = va_arg(ap, sqlite3*);
133087      db->nMaxSorterMmap = va_arg(ap, int);
133088      break;
133089    }
133090
133091    /*   sqlite3_test_control(SQLITE_TESTCTRL_ISINIT);
133092    **
133093    ** Return SQLITE_OK if SQLite has been initialized and SQLITE_ERROR if
133094    ** not.
133095    */
133096    case SQLITE_TESTCTRL_ISINIT: {
133097      if( sqlite3GlobalConfig.isInit==0 ) rc = SQLITE_ERROR;
133098      break;
133099    }
133100
133101    /*  sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, db, dbName, onOff, tnum);
133102    **
133103    ** This test control is used to create imposter tables.  "db" is a pointer
133104    ** to the database connection.  dbName is the database name (ex: "main" or
133105    ** "temp") which will receive the imposter.  "onOff" turns imposter mode on
133106    ** or off.  "tnum" is the root page of the b-tree to which the imposter
133107    ** table should connect.
133108    **
133109    ** Enable imposter mode only when the schema has already been parsed.  Then
133110    ** run a single CREATE TABLE statement to construct the imposter table in
133111    ** the parsed schema.  Then turn imposter mode back off again.
133112    **
133113    ** If onOff==0 and tnum>0 then reset the schema for all databases, causing
133114    ** the schema to be reparsed the next time it is needed.  This has the
133115    ** effect of erasing all imposter tables.
133116    */
133117    case SQLITE_TESTCTRL_IMPOSTER: {
133118      sqlite3 *db = va_arg(ap, sqlite3*);
133119      sqlite3_mutex_enter(db->mutex);
133120      db->init.iDb = sqlite3FindDbName(db, va_arg(ap,const char*));
133121      db->init.busy = db->init.imposterTable = va_arg(ap,int);
133122      db->init.newTnum = va_arg(ap,int);
133123      if( db->init.busy==0 && db->init.newTnum>0 ){
133124        sqlite3ResetAllSchemasOfConnection(db);
133125      }
133126      sqlite3_mutex_leave(db->mutex);
133127      break;
133128    }
133129  }
133130  va_end(ap);
133131#endif /* SQLITE_OMIT_BUILTIN_TEST */
133132  return rc;
133133}
133134
133135/*
133136** This is a utility routine, useful to VFS implementations, that checks
133137** to see if a database file was a URI that contained a specific query
133138** parameter, and if so obtains the value of the query parameter.
133139**
133140** The zFilename argument is the filename pointer passed into the xOpen()
133141** method of a VFS implementation.  The zParam argument is the name of the
133142** query parameter we seek.  This routine returns the value of the zParam
133143** parameter if it exists.  If the parameter does not exist, this routine
133144** returns a NULL pointer.
133145*/
133146SQLITE_API const char *SQLITE_STDCALL sqlite3_uri_parameter(const char *zFilename, const char *zParam){
133147  if( zFilename==0 || zParam==0 ) return 0;
133148  zFilename += sqlite3Strlen30(zFilename) + 1;
133149  while( zFilename[0] ){
133150    int x = strcmp(zFilename, zParam);
133151    zFilename += sqlite3Strlen30(zFilename) + 1;
133152    if( x==0 ) return zFilename;
133153    zFilename += sqlite3Strlen30(zFilename) + 1;
133154  }
133155  return 0;
133156}
133157
133158/*
133159** Return a boolean value for a query parameter.
133160*/
133161SQLITE_API int SQLITE_STDCALL sqlite3_uri_boolean(const char *zFilename, const char *zParam, int bDflt){
133162  const char *z = sqlite3_uri_parameter(zFilename, zParam);
133163  bDflt = bDflt!=0;
133164  return z ? sqlite3GetBoolean(z, bDflt) : bDflt;
133165}
133166
133167/*
133168** Return a 64-bit integer value for a query parameter.
133169*/
133170SQLITE_API sqlite3_int64 SQLITE_STDCALL sqlite3_uri_int64(
133171  const char *zFilename,    /* Filename as passed to xOpen */
133172  const char *zParam,       /* URI parameter sought */
133173  sqlite3_int64 bDflt       /* return if parameter is missing */
133174){
133175  const char *z = sqlite3_uri_parameter(zFilename, zParam);
133176  sqlite3_int64 v;
133177  if( z && sqlite3DecOrHexToI64(z, &v)==SQLITE_OK ){
133178    bDflt = v;
133179  }
133180  return bDflt;
133181}
133182
133183/*
133184** Return the Btree pointer identified by zDbName.  Return NULL if not found.
133185*/
133186SQLITE_PRIVATE Btree *sqlite3DbNameToBtree(sqlite3 *db, const char *zDbName){
133187  int i;
133188  for(i=0; i<db->nDb; i++){
133189    if( db->aDb[i].pBt
133190     && (zDbName==0 || sqlite3StrICmp(zDbName, db->aDb[i].zName)==0)
133191    ){
133192      return db->aDb[i].pBt;
133193    }
133194  }
133195  return 0;
133196}
133197
133198/*
133199** Return the filename of the database associated with a database
133200** connection.
133201*/
133202SQLITE_API const char *SQLITE_STDCALL sqlite3_db_filename(sqlite3 *db, const char *zDbName){
133203  Btree *pBt;
133204#ifdef SQLITE_ENABLE_API_ARMOR
133205  if( !sqlite3SafetyCheckOk(db) ){
133206    (void)SQLITE_MISUSE_BKPT;
133207    return 0;
133208  }
133209#endif
133210  pBt = sqlite3DbNameToBtree(db, zDbName);
133211  return pBt ? sqlite3BtreeGetFilename(pBt) : 0;
133212}
133213
133214/*
133215** Return 1 if database is read-only or 0 if read/write.  Return -1 if
133216** no such database exists.
133217*/
133218SQLITE_API int SQLITE_STDCALL sqlite3_db_readonly(sqlite3 *db, const char *zDbName){
133219  Btree *pBt;
133220#ifdef SQLITE_ENABLE_API_ARMOR
133221  if( !sqlite3SafetyCheckOk(db) ){
133222    (void)SQLITE_MISUSE_BKPT;
133223    return -1;
133224  }
133225#endif
133226  pBt = sqlite3DbNameToBtree(db, zDbName);
133227  return pBt ? sqlite3BtreeIsReadonly(pBt) : -1;
133228}
133229
133230/************** End of main.c ************************************************/
133231/************** Begin file notify.c ******************************************/
133232/*
133233** 2009 March 3
133234**
133235** The author disclaims copyright to this source code.  In place of
133236** a legal notice, here is a blessing:
133237**
133238**    May you do good and not evil.
133239**    May you find forgiveness for yourself and forgive others.
133240**    May you share freely, never taking more than you give.
133241**
133242*************************************************************************
133243**
133244** This file contains the implementation of the sqlite3_unlock_notify()
133245** API method and its associated functionality.
133246*/
133247/* #include "sqliteInt.h" */
133248/* #include "btreeInt.h" */
133249
133250/* Omit this entire file if SQLITE_ENABLE_UNLOCK_NOTIFY is not defined. */
133251#ifdef SQLITE_ENABLE_UNLOCK_NOTIFY
133252
133253/*
133254** Public interfaces:
133255**
133256**   sqlite3ConnectionBlocked()
133257**   sqlite3ConnectionUnlocked()
133258**   sqlite3ConnectionClosed()
133259**   sqlite3_unlock_notify()
133260*/
133261
133262#define assertMutexHeld() \
133263  assert( sqlite3_mutex_held(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER)) )
133264
133265/*
133266** Head of a linked list of all sqlite3 objects created by this process
133267** for which either sqlite3.pBlockingConnection or sqlite3.pUnlockConnection
133268** is not NULL. This variable may only accessed while the STATIC_MASTER
133269** mutex is held.
133270*/
133271static sqlite3 *SQLITE_WSD sqlite3BlockedList = 0;
133272
133273#ifndef NDEBUG
133274/*
133275** This function is a complex assert() that verifies the following
133276** properties of the blocked connections list:
133277**
133278**   1) Each entry in the list has a non-NULL value for either
133279**      pUnlockConnection or pBlockingConnection, or both.
133280**
133281**   2) All entries in the list that share a common value for
133282**      xUnlockNotify are grouped together.
133283**
133284**   3) If the argument db is not NULL, then none of the entries in the
133285**      blocked connections list have pUnlockConnection or pBlockingConnection
133286**      set to db. This is used when closing connection db.
133287*/
133288static void checkListProperties(sqlite3 *db){
133289  sqlite3 *p;
133290  for(p=sqlite3BlockedList; p; p=p->pNextBlocked){
133291    int seen = 0;
133292    sqlite3 *p2;
133293
133294    /* Verify property (1) */
133295    assert( p->pUnlockConnection || p->pBlockingConnection );
133296
133297    /* Verify property (2) */
133298    for(p2=sqlite3BlockedList; p2!=p; p2=p2->pNextBlocked){
133299      if( p2->xUnlockNotify==p->xUnlockNotify ) seen = 1;
133300      assert( p2->xUnlockNotify==p->xUnlockNotify || !seen );
133301      assert( db==0 || p->pUnlockConnection!=db );
133302      assert( db==0 || p->pBlockingConnection!=db );
133303    }
133304  }
133305}
133306#else
133307# define checkListProperties(x)
133308#endif
133309
133310/*
133311** Remove connection db from the blocked connections list. If connection
133312** db is not currently a part of the list, this function is a no-op.
133313*/
133314static void removeFromBlockedList(sqlite3 *db){
133315  sqlite3 **pp;
133316  assertMutexHeld();
133317  for(pp=&sqlite3BlockedList; *pp; pp = &(*pp)->pNextBlocked){
133318    if( *pp==db ){
133319      *pp = (*pp)->pNextBlocked;
133320      break;
133321    }
133322  }
133323}
133324
133325/*
133326** Add connection db to the blocked connections list. It is assumed
133327** that it is not already a part of the list.
133328*/
133329static void addToBlockedList(sqlite3 *db){
133330  sqlite3 **pp;
133331  assertMutexHeld();
133332  for(
133333    pp=&sqlite3BlockedList;
133334    *pp && (*pp)->xUnlockNotify!=db->xUnlockNotify;
133335    pp=&(*pp)->pNextBlocked
133336  );
133337  db->pNextBlocked = *pp;
133338  *pp = db;
133339}
133340
133341/*
133342** Obtain the STATIC_MASTER mutex.
133343*/
133344static void enterMutex(void){
133345  sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
133346  checkListProperties(0);
133347}
133348
133349/*
133350** Release the STATIC_MASTER mutex.
133351*/
133352static void leaveMutex(void){
133353  assertMutexHeld();
133354  checkListProperties(0);
133355  sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
133356}
133357
133358/*
133359** Register an unlock-notify callback.
133360**
133361** This is called after connection "db" has attempted some operation
133362** but has received an SQLITE_LOCKED error because another connection
133363** (call it pOther) in the same process was busy using the same shared
133364** cache.  pOther is found by looking at db->pBlockingConnection.
133365**
133366** If there is no blocking connection, the callback is invoked immediately,
133367** before this routine returns.
133368**
133369** If pOther is already blocked on db, then report SQLITE_LOCKED, to indicate
133370** a deadlock.
133371**
133372** Otherwise, make arrangements to invoke xNotify when pOther drops
133373** its locks.
133374**
133375** Each call to this routine overrides any prior callbacks registered
133376** on the same "db".  If xNotify==0 then any prior callbacks are immediately
133377** cancelled.
133378*/
133379SQLITE_API int SQLITE_STDCALL sqlite3_unlock_notify(
133380  sqlite3 *db,
133381  void (*xNotify)(void **, int),
133382  void *pArg
133383){
133384  int rc = SQLITE_OK;
133385
133386  sqlite3_mutex_enter(db->mutex);
133387  enterMutex();
133388
133389  if( xNotify==0 ){
133390    removeFromBlockedList(db);
133391    db->pBlockingConnection = 0;
133392    db->pUnlockConnection = 0;
133393    db->xUnlockNotify = 0;
133394    db->pUnlockArg = 0;
133395  }else if( 0==db->pBlockingConnection ){
133396    /* The blocking transaction has been concluded. Or there never was a
133397    ** blocking transaction. In either case, invoke the notify callback
133398    ** immediately.
133399    */
133400    xNotify(&pArg, 1);
133401  }else{
133402    sqlite3 *p;
133403
133404    for(p=db->pBlockingConnection; p && p!=db; p=p->pUnlockConnection){}
133405    if( p ){
133406      rc = SQLITE_LOCKED;              /* Deadlock detected. */
133407    }else{
133408      db->pUnlockConnection = db->pBlockingConnection;
133409      db->xUnlockNotify = xNotify;
133410      db->pUnlockArg = pArg;
133411      removeFromBlockedList(db);
133412      addToBlockedList(db);
133413    }
133414  }
133415
133416  leaveMutex();
133417  assert( !db->mallocFailed );
133418  sqlite3ErrorWithMsg(db, rc, (rc?"database is deadlocked":0));
133419  sqlite3_mutex_leave(db->mutex);
133420  return rc;
133421}
133422
133423/*
133424** This function is called while stepping or preparing a statement
133425** associated with connection db. The operation will return SQLITE_LOCKED
133426** to the user because it requires a lock that will not be available
133427** until connection pBlocker concludes its current transaction.
133428*/
133429SQLITE_PRIVATE void sqlite3ConnectionBlocked(sqlite3 *db, sqlite3 *pBlocker){
133430  enterMutex();
133431  if( db->pBlockingConnection==0 && db->pUnlockConnection==0 ){
133432    addToBlockedList(db);
133433  }
133434  db->pBlockingConnection = pBlocker;
133435  leaveMutex();
133436}
133437
133438/*
133439** This function is called when
133440** the transaction opened by database db has just finished. Locks held
133441** by database connection db have been released.
133442**
133443** This function loops through each entry in the blocked connections
133444** list and does the following:
133445**
133446**   1) If the sqlite3.pBlockingConnection member of a list entry is
133447**      set to db, then set pBlockingConnection=0.
133448**
133449**   2) If the sqlite3.pUnlockConnection member of a list entry is
133450**      set to db, then invoke the configured unlock-notify callback and
133451**      set pUnlockConnection=0.
133452**
133453**   3) If the two steps above mean that pBlockingConnection==0 and
133454**      pUnlockConnection==0, remove the entry from the blocked connections
133455**      list.
133456*/
133457SQLITE_PRIVATE void sqlite3ConnectionUnlocked(sqlite3 *db){
133458  void (*xUnlockNotify)(void **, int) = 0; /* Unlock-notify cb to invoke */
133459  int nArg = 0;                            /* Number of entries in aArg[] */
133460  sqlite3 **pp;                            /* Iterator variable */
133461  void **aArg;               /* Arguments to the unlock callback */
133462  void **aDyn = 0;           /* Dynamically allocated space for aArg[] */
133463  void *aStatic[16];         /* Starter space for aArg[].  No malloc required */
133464
133465  aArg = aStatic;
133466  enterMutex();         /* Enter STATIC_MASTER mutex */
133467
133468  /* This loop runs once for each entry in the blocked-connections list. */
133469  for(pp=&sqlite3BlockedList; *pp; /* no-op */ ){
133470    sqlite3 *p = *pp;
133471
133472    /* Step 1. */
133473    if( p->pBlockingConnection==db ){
133474      p->pBlockingConnection = 0;
133475    }
133476
133477    /* Step 2. */
133478    if( p->pUnlockConnection==db ){
133479      assert( p->xUnlockNotify );
133480      if( p->xUnlockNotify!=xUnlockNotify && nArg!=0 ){
133481        xUnlockNotify(aArg, nArg);
133482        nArg = 0;
133483      }
133484
133485      sqlite3BeginBenignMalloc();
133486      assert( aArg==aDyn || (aDyn==0 && aArg==aStatic) );
133487      assert( nArg<=(int)ArraySize(aStatic) || aArg==aDyn );
133488      if( (!aDyn && nArg==(int)ArraySize(aStatic))
133489       || (aDyn && nArg==(int)(sqlite3MallocSize(aDyn)/sizeof(void*)))
133490      ){
133491        /* The aArg[] array needs to grow. */
133492        void **pNew = (void **)sqlite3Malloc(nArg*sizeof(void *)*2);
133493        if( pNew ){
133494          memcpy(pNew, aArg, nArg*sizeof(void *));
133495          sqlite3_free(aDyn);
133496          aDyn = aArg = pNew;
133497        }else{
133498          /* This occurs when the array of context pointers that need to
133499          ** be passed to the unlock-notify callback is larger than the
133500          ** aStatic[] array allocated on the stack and the attempt to
133501          ** allocate a larger array from the heap has failed.
133502          **
133503          ** This is a difficult situation to handle. Returning an error
133504          ** code to the caller is insufficient, as even if an error code
133505          ** is returned the transaction on connection db will still be
133506          ** closed and the unlock-notify callbacks on blocked connections
133507          ** will go unissued. This might cause the application to wait
133508          ** indefinitely for an unlock-notify callback that will never
133509          ** arrive.
133510          **
133511          ** Instead, invoke the unlock-notify callback with the context
133512          ** array already accumulated. We can then clear the array and
133513          ** begin accumulating any further context pointers without
133514          ** requiring any dynamic allocation. This is sub-optimal because
133515          ** it means that instead of one callback with a large array of
133516          ** context pointers the application will receive two or more
133517          ** callbacks with smaller arrays of context pointers, which will
133518          ** reduce the applications ability to prioritize multiple
133519          ** connections. But it is the best that can be done under the
133520          ** circumstances.
133521          */
133522          xUnlockNotify(aArg, nArg);
133523          nArg = 0;
133524        }
133525      }
133526      sqlite3EndBenignMalloc();
133527
133528      aArg[nArg++] = p->pUnlockArg;
133529      xUnlockNotify = p->xUnlockNotify;
133530      p->pUnlockConnection = 0;
133531      p->xUnlockNotify = 0;
133532      p->pUnlockArg = 0;
133533    }
133534
133535    /* Step 3. */
133536    if( p->pBlockingConnection==0 && p->pUnlockConnection==0 ){
133537      /* Remove connection p from the blocked connections list. */
133538      *pp = p->pNextBlocked;
133539      p->pNextBlocked = 0;
133540    }else{
133541      pp = &p->pNextBlocked;
133542    }
133543  }
133544
133545  if( nArg!=0 ){
133546    xUnlockNotify(aArg, nArg);
133547  }
133548  sqlite3_free(aDyn);
133549  leaveMutex();         /* Leave STATIC_MASTER mutex */
133550}
133551
133552/*
133553** This is called when the database connection passed as an argument is
133554** being closed. The connection is removed from the blocked list.
133555*/
133556SQLITE_PRIVATE void sqlite3ConnectionClosed(sqlite3 *db){
133557  sqlite3ConnectionUnlocked(db);
133558  enterMutex();
133559  removeFromBlockedList(db);
133560  checkListProperties(db);
133561  leaveMutex();
133562}
133563#endif
133564
133565/************** End of notify.c **********************************************/
133566/************** Begin file fts3.c ********************************************/
133567/*
133568** 2006 Oct 10
133569**
133570** The author disclaims copyright to this source code.  In place of
133571** a legal notice, here is a blessing:
133572**
133573**    May you do good and not evil.
133574**    May you find forgiveness for yourself and forgive others.
133575**    May you share freely, never taking more than you give.
133576**
133577******************************************************************************
133578**
133579** This is an SQLite module implementing full-text search.
133580*/
133581
133582/*
133583** The code in this file is only compiled if:
133584**
133585**     * The FTS3 module is being built as an extension
133586**       (in which case SQLITE_CORE is not defined), or
133587**
133588**     * The FTS3 module is being built into the core of
133589**       SQLite (in which case SQLITE_ENABLE_FTS3 is defined).
133590*/
133591
133592/* The full-text index is stored in a series of b+tree (-like)
133593** structures called segments which map terms to doclists.  The
133594** structures are like b+trees in layout, but are constructed from the
133595** bottom up in optimal fashion and are not updatable.  Since trees
133596** are built from the bottom up, things will be described from the
133597** bottom up.
133598**
133599**
133600**** Varints ****
133601** The basic unit of encoding is a variable-length integer called a
133602** varint.  We encode variable-length integers in little-endian order
133603** using seven bits * per byte as follows:
133604**
133605** KEY:
133606**         A = 0xxxxxxx    7 bits of data and one flag bit
133607**         B = 1xxxxxxx    7 bits of data and one flag bit
133608**
133609**  7 bits - A
133610** 14 bits - BA
133611** 21 bits - BBA
133612** and so on.
133613**
133614** This is similar in concept to how sqlite encodes "varints" but
133615** the encoding is not the same.  SQLite varints are big-endian
133616** are are limited to 9 bytes in length whereas FTS3 varints are
133617** little-endian and can be up to 10 bytes in length (in theory).
133618**
133619** Example encodings:
133620**
133621**     1:    0x01
133622**   127:    0x7f
133623**   128:    0x81 0x00
133624**
133625**
133626**** Document lists ****
133627** A doclist (document list) holds a docid-sorted list of hits for a
133628** given term.  Doclists hold docids and associated token positions.
133629** A docid is the unique integer identifier for a single document.
133630** A position is the index of a word within the document.  The first
133631** word of the document has a position of 0.
133632**
133633** FTS3 used to optionally store character offsets using a compile-time
133634** option.  But that functionality is no longer supported.
133635**
133636** A doclist is stored like this:
133637**
133638** array {
133639**   varint docid;          (delta from previous doclist)
133640**   array {                (position list for column 0)
133641**     varint position;     (2 more than the delta from previous position)
133642**   }
133643**   array {
133644**     varint POS_COLUMN;   (marks start of position list for new column)
133645**     varint column;       (index of new column)
133646**     array {
133647**       varint position;   (2 more than the delta from previous position)
133648**     }
133649**   }
133650**   varint POS_END;        (marks end of positions for this document.
133651** }
133652**
133653** Here, array { X } means zero or more occurrences of X, adjacent in
133654** memory.  A "position" is an index of a token in the token stream
133655** generated by the tokenizer. Note that POS_END and POS_COLUMN occur
133656** in the same logical place as the position element, and act as sentinals
133657** ending a position list array.  POS_END is 0.  POS_COLUMN is 1.
133658** The positions numbers are not stored literally but rather as two more
133659** than the difference from the prior position, or the just the position plus
133660** 2 for the first position.  Example:
133661**
133662**   label:       A B C D E  F  G H   I  J K
133663**   value:     123 5 9 1 1 14 35 0 234 72 0
133664**
133665** The 123 value is the first docid.  For column zero in this document
133666** there are two matches at positions 3 and 10 (5-2 and 9-2+3).  The 1
133667** at D signals the start of a new column; the 1 at E indicates that the
133668** new column is column number 1.  There are two positions at 12 and 45
133669** (14-2 and 35-2+12).  The 0 at H indicate the end-of-document.  The
133670** 234 at I is the delta to next docid (357).  It has one position 70
133671** (72-2) and then terminates with the 0 at K.
133672**
133673** A "position-list" is the list of positions for multiple columns for
133674** a single docid.  A "column-list" is the set of positions for a single
133675** column.  Hence, a position-list consists of one or more column-lists,
133676** a document record consists of a docid followed by a position-list and
133677** a doclist consists of one or more document records.
133678**
133679** A bare doclist omits the position information, becoming an
133680** array of varint-encoded docids.
133681**
133682**** Segment leaf nodes ****
133683** Segment leaf nodes store terms and doclists, ordered by term.  Leaf
133684** nodes are written using LeafWriter, and read using LeafReader (to
133685** iterate through a single leaf node's data) and LeavesReader (to
133686** iterate through a segment's entire leaf layer).  Leaf nodes have
133687** the format:
133688**
133689** varint iHeight;             (height from leaf level, always 0)
133690** varint nTerm;               (length of first term)
133691** char pTerm[nTerm];          (content of first term)
133692** varint nDoclist;            (length of term's associated doclist)
133693** char pDoclist[nDoclist];    (content of doclist)
133694** array {
133695**                             (further terms are delta-encoded)
133696**   varint nPrefix;           (length of prefix shared with previous term)
133697**   varint nSuffix;           (length of unshared suffix)
133698**   char pTermSuffix[nSuffix];(unshared suffix of next term)
133699**   varint nDoclist;          (length of term's associated doclist)
133700**   char pDoclist[nDoclist];  (content of doclist)
133701** }
133702**
133703** Here, array { X } means zero or more occurrences of X, adjacent in
133704** memory.
133705**
133706** Leaf nodes are broken into blocks which are stored contiguously in
133707** the %_segments table in sorted order.  This means that when the end
133708** of a node is reached, the next term is in the node with the next
133709** greater node id.
133710**
133711** New data is spilled to a new leaf node when the current node
133712** exceeds LEAF_MAX bytes (default 2048).  New data which itself is
133713** larger than STANDALONE_MIN (default 1024) is placed in a standalone
133714** node (a leaf node with a single term and doclist).  The goal of
133715** these settings is to pack together groups of small doclists while
133716** making it efficient to directly access large doclists.  The
133717** assumption is that large doclists represent terms which are more
133718** likely to be query targets.
133719**
133720** TODO(shess) It may be useful for blocking decisions to be more
133721** dynamic.  For instance, it may make more sense to have a 2.5k leaf
133722** node rather than splitting into 2k and .5k nodes.  My intuition is
133723** that this might extend through 2x or 4x the pagesize.
133724**
133725**
133726**** Segment interior nodes ****
133727** Segment interior nodes store blockids for subtree nodes and terms
133728** to describe what data is stored by the each subtree.  Interior
133729** nodes are written using InteriorWriter, and read using
133730** InteriorReader.  InteriorWriters are created as needed when
133731** SegmentWriter creates new leaf nodes, or when an interior node
133732** itself grows too big and must be split.  The format of interior
133733** nodes:
133734**
133735** varint iHeight;           (height from leaf level, always >0)
133736** varint iBlockid;          (block id of node's leftmost subtree)
133737** optional {
133738**   varint nTerm;           (length of first term)
133739**   char pTerm[nTerm];      (content of first term)
133740**   array {
133741**                                (further terms are delta-encoded)
133742**     varint nPrefix;            (length of shared prefix with previous term)
133743**     varint nSuffix;            (length of unshared suffix)
133744**     char pTermSuffix[nSuffix]; (unshared suffix of next term)
133745**   }
133746** }
133747**
133748** Here, optional { X } means an optional element, while array { X }
133749** means zero or more occurrences of X, adjacent in memory.
133750**
133751** An interior node encodes n terms separating n+1 subtrees.  The
133752** subtree blocks are contiguous, so only the first subtree's blockid
133753** is encoded.  The subtree at iBlockid will contain all terms less
133754** than the first term encoded (or all terms if no term is encoded).
133755** Otherwise, for terms greater than or equal to pTerm[i] but less
133756** than pTerm[i+1], the subtree for that term will be rooted at
133757** iBlockid+i.  Interior nodes only store enough term data to
133758** distinguish adjacent children (if the rightmost term of the left
133759** child is "something", and the leftmost term of the right child is
133760** "wicked", only "w" is stored).
133761**
133762** New data is spilled to a new interior node at the same height when
133763** the current node exceeds INTERIOR_MAX bytes (default 2048).
133764** INTERIOR_MIN_TERMS (default 7) keeps large terms from monopolizing
133765** interior nodes and making the tree too skinny.  The interior nodes
133766** at a given height are naturally tracked by interior nodes at
133767** height+1, and so on.
133768**
133769**
133770**** Segment directory ****
133771** The segment directory in table %_segdir stores meta-information for
133772** merging and deleting segments, and also the root node of the
133773** segment's tree.
133774**
133775** The root node is the top node of the segment's tree after encoding
133776** the entire segment, restricted to ROOT_MAX bytes (default 1024).
133777** This could be either a leaf node or an interior node.  If the top
133778** node requires more than ROOT_MAX bytes, it is flushed to %_segments
133779** and a new root interior node is generated (which should always fit
133780** within ROOT_MAX because it only needs space for 2 varints, the
133781** height and the blockid of the previous root).
133782**
133783** The meta-information in the segment directory is:
133784**   level               - segment level (see below)
133785**   idx                 - index within level
133786**                       - (level,idx uniquely identify a segment)
133787**   start_block         - first leaf node
133788**   leaves_end_block    - last leaf node
133789**   end_block           - last block (including interior nodes)
133790**   root                - contents of root node
133791**
133792** If the root node is a leaf node, then start_block,
133793** leaves_end_block, and end_block are all 0.
133794**
133795**
133796**** Segment merging ****
133797** To amortize update costs, segments are grouped into levels and
133798** merged in batches.  Each increase in level represents exponentially
133799** more documents.
133800**
133801** New documents (actually, document updates) are tokenized and
133802** written individually (using LeafWriter) to a level 0 segment, with
133803** incrementing idx.  When idx reaches MERGE_COUNT (default 16), all
133804** level 0 segments are merged into a single level 1 segment.  Level 1
133805** is populated like level 0, and eventually MERGE_COUNT level 1
133806** segments are merged to a single level 2 segment (representing
133807** MERGE_COUNT^2 updates), and so on.
133808**
133809** A segment merge traverses all segments at a given level in
133810** parallel, performing a straightforward sorted merge.  Since segment
133811** leaf nodes are written in to the %_segments table in order, this
133812** merge traverses the underlying sqlite disk structures efficiently.
133813** After the merge, all segment blocks from the merged level are
133814** deleted.
133815**
133816** MERGE_COUNT controls how often we merge segments.  16 seems to be
133817** somewhat of a sweet spot for insertion performance.  32 and 64 show
133818** very similar performance numbers to 16 on insertion, though they're
133819** a tiny bit slower (perhaps due to more overhead in merge-time
133820** sorting).  8 is about 20% slower than 16, 4 about 50% slower than
133821** 16, 2 about 66% slower than 16.
133822**
133823** At query time, high MERGE_COUNT increases the number of segments
133824** which need to be scanned and merged.  For instance, with 100k docs
133825** inserted:
133826**
133827**    MERGE_COUNT   segments
133828**       16           25
133829**        8           12
133830**        4           10
133831**        2            6
133832**
133833** This appears to have only a moderate impact on queries for very
133834** frequent terms (which are somewhat dominated by segment merge
133835** costs), and infrequent and non-existent terms still seem to be fast
133836** even with many segments.
133837**
133838** TODO(shess) That said, it would be nice to have a better query-side
133839** argument for MERGE_COUNT of 16.  Also, it is possible/likely that
133840** optimizations to things like doclist merging will swing the sweet
133841** spot around.
133842**
133843**
133844**
133845**** Handling of deletions and updates ****
133846** Since we're using a segmented structure, with no docid-oriented
133847** index into the term index, we clearly cannot simply update the term
133848** index when a document is deleted or updated.  For deletions, we
133849** write an empty doclist (varint(docid) varint(POS_END)), for updates
133850** we simply write the new doclist.  Segment merges overwrite older
133851** data for a particular docid with newer data, so deletes or updates
133852** will eventually overtake the earlier data and knock it out.  The
133853** query logic likewise merges doclists so that newer data knocks out
133854** older data.
133855*/
133856
133857/************** Include fts3Int.h in the middle of fts3.c ********************/
133858/************** Begin file fts3Int.h *****************************************/
133859/*
133860** 2009 Nov 12
133861**
133862** The author disclaims copyright to this source code.  In place of
133863** a legal notice, here is a blessing:
133864**
133865**    May you do good and not evil.
133866**    May you find forgiveness for yourself and forgive others.
133867**    May you share freely, never taking more than you give.
133868**
133869******************************************************************************
133870**
133871*/
133872#ifndef _FTSINT_H
133873#define _FTSINT_H
133874
133875#if !defined(NDEBUG) && !defined(SQLITE_DEBUG)
133876# define NDEBUG 1
133877#endif
133878
133879/*
133880** FTS4 is really an extension for FTS3.  It is enabled using the
133881** SQLITE_ENABLE_FTS3 macro.  But to avoid confusion we also all
133882** the SQLITE_ENABLE_FTS4 macro to serve as an alisse for SQLITE_ENABLE_FTS3.
133883*/
133884#if defined(SQLITE_ENABLE_FTS4) && !defined(SQLITE_ENABLE_FTS3)
133885# define SQLITE_ENABLE_FTS3
133886#endif
133887
133888#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
133889
133890/* If not building as part of the core, include sqlite3ext.h. */
133891#ifndef SQLITE_CORE
133892/* # include "sqlite3ext.h"  */
133893SQLITE_EXTENSION_INIT3
133894#endif
133895
133896/* #include "sqlite3.h" */
133897/************** Include fts3_tokenizer.h in the middle of fts3Int.h **********/
133898/************** Begin file fts3_tokenizer.h **********************************/
133899/*
133900** 2006 July 10
133901**
133902** The author disclaims copyright to this source code.
133903**
133904*************************************************************************
133905** Defines the interface to tokenizers used by fulltext-search.  There
133906** are three basic components:
133907**
133908** sqlite3_tokenizer_module is a singleton defining the tokenizer
133909** interface functions.  This is essentially the class structure for
133910** tokenizers.
133911**
133912** sqlite3_tokenizer is used to define a particular tokenizer, perhaps
133913** including customization information defined at creation time.
133914**
133915** sqlite3_tokenizer_cursor is generated by a tokenizer to generate
133916** tokens from a particular input.
133917*/
133918#ifndef _FTS3_TOKENIZER_H_
133919#define _FTS3_TOKENIZER_H_
133920
133921/* TODO(shess) Only used for SQLITE_OK and SQLITE_DONE at this time.
133922** If tokenizers are to be allowed to call sqlite3_*() functions, then
133923** we will need a way to register the API consistently.
133924*/
133925/* #include "sqlite3.h" */
133926
133927/*
133928** Structures used by the tokenizer interface. When a new tokenizer
133929** implementation is registered, the caller provides a pointer to
133930** an sqlite3_tokenizer_module containing pointers to the callback
133931** functions that make up an implementation.
133932**
133933** When an fts3 table is created, it passes any arguments passed to
133934** the tokenizer clause of the CREATE VIRTUAL TABLE statement to the
133935** sqlite3_tokenizer_module.xCreate() function of the requested tokenizer
133936** implementation. The xCreate() function in turn returns an
133937** sqlite3_tokenizer structure representing the specific tokenizer to
133938** be used for the fts3 table (customized by the tokenizer clause arguments).
133939**
133940** To tokenize an input buffer, the sqlite3_tokenizer_module.xOpen()
133941** method is called. It returns an sqlite3_tokenizer_cursor object
133942** that may be used to tokenize a specific input buffer based on
133943** the tokenization rules supplied by a specific sqlite3_tokenizer
133944** object.
133945*/
133946typedef struct sqlite3_tokenizer_module sqlite3_tokenizer_module;
133947typedef struct sqlite3_tokenizer sqlite3_tokenizer;
133948typedef struct sqlite3_tokenizer_cursor sqlite3_tokenizer_cursor;
133949
133950struct sqlite3_tokenizer_module {
133951
133952  /*
133953  ** Structure version. Should always be set to 0 or 1.
133954  */
133955  int iVersion;
133956
133957  /*
133958  ** Create a new tokenizer. The values in the argv[] array are the
133959  ** arguments passed to the "tokenizer" clause of the CREATE VIRTUAL
133960  ** TABLE statement that created the fts3 table. For example, if
133961  ** the following SQL is executed:
133962  **
133963  **   CREATE .. USING fts3( ... , tokenizer <tokenizer-name> arg1 arg2)
133964  **
133965  ** then argc is set to 2, and the argv[] array contains pointers
133966  ** to the strings "arg1" and "arg2".
133967  **
133968  ** This method should return either SQLITE_OK (0), or an SQLite error
133969  ** code. If SQLITE_OK is returned, then *ppTokenizer should be set
133970  ** to point at the newly created tokenizer structure. The generic
133971  ** sqlite3_tokenizer.pModule variable should not be initialized by
133972  ** this callback. The caller will do so.
133973  */
133974  int (*xCreate)(
133975    int argc,                           /* Size of argv array */
133976    const char *const*argv,             /* Tokenizer argument strings */
133977    sqlite3_tokenizer **ppTokenizer     /* OUT: Created tokenizer */
133978  );
133979
133980  /*
133981  ** Destroy an existing tokenizer. The fts3 module calls this method
133982  ** exactly once for each successful call to xCreate().
133983  */
133984  int (*xDestroy)(sqlite3_tokenizer *pTokenizer);
133985
133986  /*
133987  ** Create a tokenizer cursor to tokenize an input buffer. The caller
133988  ** is responsible for ensuring that the input buffer remains valid
133989  ** until the cursor is closed (using the xClose() method).
133990  */
133991  int (*xOpen)(
133992    sqlite3_tokenizer *pTokenizer,       /* Tokenizer object */
133993    const char *pInput, int nBytes,      /* Input buffer */
133994    sqlite3_tokenizer_cursor **ppCursor  /* OUT: Created tokenizer cursor */
133995  );
133996
133997  /*
133998  ** Destroy an existing tokenizer cursor. The fts3 module calls this
133999  ** method exactly once for each successful call to xOpen().
134000  */
134001  int (*xClose)(sqlite3_tokenizer_cursor *pCursor);
134002
134003  /*
134004  ** Retrieve the next token from the tokenizer cursor pCursor. This
134005  ** method should either return SQLITE_OK and set the values of the
134006  ** "OUT" variables identified below, or SQLITE_DONE to indicate that
134007  ** the end of the buffer has been reached, or an SQLite error code.
134008  **
134009  ** *ppToken should be set to point at a buffer containing the
134010  ** normalized version of the token (i.e. after any case-folding and/or
134011  ** stemming has been performed). *pnBytes should be set to the length
134012  ** of this buffer in bytes. The input text that generated the token is
134013  ** identified by the byte offsets returned in *piStartOffset and
134014  ** *piEndOffset. *piStartOffset should be set to the index of the first
134015  ** byte of the token in the input buffer. *piEndOffset should be set
134016  ** to the index of the first byte just past the end of the token in
134017  ** the input buffer.
134018  **
134019  ** The buffer *ppToken is set to point at is managed by the tokenizer
134020  ** implementation. It is only required to be valid until the next call
134021  ** to xNext() or xClose().
134022  */
134023  /* TODO(shess) current implementation requires pInput to be
134024  ** nul-terminated.  This should either be fixed, or pInput/nBytes
134025  ** should be converted to zInput.
134026  */
134027  int (*xNext)(
134028    sqlite3_tokenizer_cursor *pCursor,   /* Tokenizer cursor */
134029    const char **ppToken, int *pnBytes,  /* OUT: Normalized text for token */
134030    int *piStartOffset,  /* OUT: Byte offset of token in input buffer */
134031    int *piEndOffset,    /* OUT: Byte offset of end of token in input buffer */
134032    int *piPosition      /* OUT: Number of tokens returned before this one */
134033  );
134034
134035  /***********************************************************************
134036  ** Methods below this point are only available if iVersion>=1.
134037  */
134038
134039  /*
134040  ** Configure the language id of a tokenizer cursor.
134041  */
134042  int (*xLanguageid)(sqlite3_tokenizer_cursor *pCsr, int iLangid);
134043};
134044
134045struct sqlite3_tokenizer {
134046  const sqlite3_tokenizer_module *pModule;  /* The module for this tokenizer */
134047  /* Tokenizer implementations will typically add additional fields */
134048};
134049
134050struct sqlite3_tokenizer_cursor {
134051  sqlite3_tokenizer *pTokenizer;       /* Tokenizer for this cursor. */
134052  /* Tokenizer implementations will typically add additional fields */
134053};
134054
134055int fts3_global_term_cnt(int iTerm, int iCol);
134056int fts3_term_cnt(int iTerm, int iCol);
134057
134058
134059#endif /* _FTS3_TOKENIZER_H_ */
134060
134061/************** End of fts3_tokenizer.h **************************************/
134062/************** Continuing where we left off in fts3Int.h ********************/
134063/************** Include fts3_hash.h in the middle of fts3Int.h ***************/
134064/************** Begin file fts3_hash.h ***************************************/
134065/*
134066** 2001 September 22
134067**
134068** The author disclaims copyright to this source code.  In place of
134069** a legal notice, here is a blessing:
134070**
134071**    May you do good and not evil.
134072**    May you find forgiveness for yourself and forgive others.
134073**    May you share freely, never taking more than you give.
134074**
134075*************************************************************************
134076** This is the header file for the generic hash-table implementation
134077** used in SQLite.  We've modified it slightly to serve as a standalone
134078** hash table implementation for the full-text indexing module.
134079**
134080*/
134081#ifndef _FTS3_HASH_H_
134082#define _FTS3_HASH_H_
134083
134084/* Forward declarations of structures. */
134085typedef struct Fts3Hash Fts3Hash;
134086typedef struct Fts3HashElem Fts3HashElem;
134087
134088/* A complete hash table is an instance of the following structure.
134089** The internals of this structure are intended to be opaque -- client
134090** code should not attempt to access or modify the fields of this structure
134091** directly.  Change this structure only by using the routines below.
134092** However, many of the "procedures" and "functions" for modifying and
134093** accessing this structure are really macros, so we can't really make
134094** this structure opaque.
134095*/
134096struct Fts3Hash {
134097  char keyClass;          /* HASH_INT, _POINTER, _STRING, _BINARY */
134098  char copyKey;           /* True if copy of key made on insert */
134099  int count;              /* Number of entries in this table */
134100  Fts3HashElem *first;    /* The first element of the array */
134101  int htsize;             /* Number of buckets in the hash table */
134102  struct _fts3ht {        /* the hash table */
134103    int count;               /* Number of entries with this hash */
134104    Fts3HashElem *chain;     /* Pointer to first entry with this hash */
134105  } *ht;
134106};
134107
134108/* Each element in the hash table is an instance of the following
134109** structure.  All elements are stored on a single doubly-linked list.
134110**
134111** Again, this structure is intended to be opaque, but it can't really
134112** be opaque because it is used by macros.
134113*/
134114struct Fts3HashElem {
134115  Fts3HashElem *next, *prev; /* Next and previous elements in the table */
134116  void *data;                /* Data associated with this element */
134117  void *pKey; int nKey;      /* Key associated with this element */
134118};
134119
134120/*
134121** There are 2 different modes of operation for a hash table:
134122**
134123**   FTS3_HASH_STRING        pKey points to a string that is nKey bytes long
134124**                           (including the null-terminator, if any).  Case
134125**                           is respected in comparisons.
134126**
134127**   FTS3_HASH_BINARY        pKey points to binary data nKey bytes long.
134128**                           memcmp() is used to compare keys.
134129**
134130** A copy of the key is made if the copyKey parameter to fts3HashInit is 1.
134131*/
134132#define FTS3_HASH_STRING    1
134133#define FTS3_HASH_BINARY    2
134134
134135/*
134136** Access routines.  To delete, insert a NULL pointer.
134137*/
134138SQLITE_PRIVATE void sqlite3Fts3HashInit(Fts3Hash *pNew, char keyClass, char copyKey);
134139SQLITE_PRIVATE void *sqlite3Fts3HashInsert(Fts3Hash*, const void *pKey, int nKey, void *pData);
134140SQLITE_PRIVATE void *sqlite3Fts3HashFind(const Fts3Hash*, const void *pKey, int nKey);
134141SQLITE_PRIVATE void sqlite3Fts3HashClear(Fts3Hash*);
134142SQLITE_PRIVATE Fts3HashElem *sqlite3Fts3HashFindElem(const Fts3Hash *, const void *, int);
134143
134144/*
134145** Shorthand for the functions above
134146*/
134147#define fts3HashInit     sqlite3Fts3HashInit
134148#define fts3HashInsert   sqlite3Fts3HashInsert
134149#define fts3HashFind     sqlite3Fts3HashFind
134150#define fts3HashClear    sqlite3Fts3HashClear
134151#define fts3HashFindElem sqlite3Fts3HashFindElem
134152
134153/*
134154** Macros for looping over all elements of a hash table.  The idiom is
134155** like this:
134156**
134157**   Fts3Hash h;
134158**   Fts3HashElem *p;
134159**   ...
134160**   for(p=fts3HashFirst(&h); p; p=fts3HashNext(p)){
134161**     SomeStructure *pData = fts3HashData(p);
134162**     // do something with pData
134163**   }
134164*/
134165#define fts3HashFirst(H)  ((H)->first)
134166#define fts3HashNext(E)   ((E)->next)
134167#define fts3HashData(E)   ((E)->data)
134168#define fts3HashKey(E)    ((E)->pKey)
134169#define fts3HashKeysize(E) ((E)->nKey)
134170
134171/*
134172** Number of entries in a hash table
134173*/
134174#define fts3HashCount(H)  ((H)->count)
134175
134176#endif /* _FTS3_HASH_H_ */
134177
134178/************** End of fts3_hash.h *******************************************/
134179/************** Continuing where we left off in fts3Int.h ********************/
134180
134181/*
134182** This constant determines the maximum depth of an FTS expression tree
134183** that the library will create and use. FTS uses recursion to perform
134184** various operations on the query tree, so the disadvantage of a large
134185** limit is that it may allow very large queries to use large amounts
134186** of stack space (perhaps causing a stack overflow).
134187*/
134188#ifndef SQLITE_FTS3_MAX_EXPR_DEPTH
134189# define SQLITE_FTS3_MAX_EXPR_DEPTH 12
134190#endif
134191
134192
134193/*
134194** This constant controls how often segments are merged. Once there are
134195** FTS3_MERGE_COUNT segments of level N, they are merged into a single
134196** segment of level N+1.
134197*/
134198#define FTS3_MERGE_COUNT 16
134199
134200/*
134201** This is the maximum amount of data (in bytes) to store in the
134202** Fts3Table.pendingTerms hash table. Normally, the hash table is
134203** populated as documents are inserted/updated/deleted in a transaction
134204** and used to create a new segment when the transaction is committed.
134205** However if this limit is reached midway through a transaction, a new
134206** segment is created and the hash table cleared immediately.
134207*/
134208#define FTS3_MAX_PENDING_DATA (1*1024*1024)
134209
134210/*
134211** Macro to return the number of elements in an array. SQLite has a
134212** similar macro called ArraySize(). Use a different name to avoid
134213** a collision when building an amalgamation with built-in FTS3.
134214*/
134215#define SizeofArray(X) ((int)(sizeof(X)/sizeof(X[0])))
134216
134217
134218#ifndef MIN
134219# define MIN(x,y) ((x)<(y)?(x):(y))
134220#endif
134221#ifndef MAX
134222# define MAX(x,y) ((x)>(y)?(x):(y))
134223#endif
134224
134225/*
134226** Maximum length of a varint encoded integer. The varint format is different
134227** from that used by SQLite, so the maximum length is 10, not 9.
134228*/
134229#define FTS3_VARINT_MAX 10
134230
134231/*
134232** FTS4 virtual tables may maintain multiple indexes - one index of all terms
134233** in the document set and zero or more prefix indexes. All indexes are stored
134234** as one or more b+-trees in the %_segments and %_segdir tables.
134235**
134236** It is possible to determine which index a b+-tree belongs to based on the
134237** value stored in the "%_segdir.level" column. Given this value L, the index
134238** that the b+-tree belongs to is (L<<10). In other words, all b+-trees with
134239** level values between 0 and 1023 (inclusive) belong to index 0, all levels
134240** between 1024 and 2047 to index 1, and so on.
134241**
134242** It is considered impossible for an index to use more than 1024 levels. In
134243** theory though this may happen, but only after at least
134244** (FTS3_MERGE_COUNT^1024) separate flushes of the pending-terms tables.
134245*/
134246#define FTS3_SEGDIR_MAXLEVEL      1024
134247#define FTS3_SEGDIR_MAXLEVEL_STR "1024"
134248
134249/*
134250** The testcase() macro is only used by the amalgamation.  If undefined,
134251** make it a no-op.
134252*/
134253#ifndef testcase
134254# define testcase(X)
134255#endif
134256
134257/*
134258** Terminator values for position-lists and column-lists.
134259*/
134260#define POS_COLUMN  (1)     /* Column-list terminator */
134261#define POS_END     (0)     /* Position-list terminator */
134262
134263/*
134264** This section provides definitions to allow the
134265** FTS3 extension to be compiled outside of the
134266** amalgamation.
134267*/
134268#ifndef SQLITE_AMALGAMATION
134269/*
134270** Macros indicating that conditional expressions are always true or
134271** false.
134272*/
134273#ifdef SQLITE_COVERAGE_TEST
134274# define ALWAYS(x) (1)
134275# define NEVER(X)  (0)
134276#elif defined(SQLITE_DEBUG)
134277# define ALWAYS(x) sqlite3Fts3Always((x)!=0)
134278# define NEVER(x) sqlite3Fts3Never((x)!=0)
134279SQLITE_PRIVATE int sqlite3Fts3Always(int b);
134280SQLITE_PRIVATE int sqlite3Fts3Never(int b);
134281#else
134282# define ALWAYS(x) (x)
134283# define NEVER(x)  (x)
134284#endif
134285
134286/*
134287** Internal types used by SQLite.
134288*/
134289typedef unsigned char u8;         /* 1-byte (or larger) unsigned integer */
134290typedef short int i16;            /* 2-byte (or larger) signed integer */
134291typedef unsigned int u32;         /* 4-byte unsigned integer */
134292typedef sqlite3_uint64 u64;       /* 8-byte unsigned integer */
134293typedef sqlite3_int64 i64;        /* 8-byte signed integer */
134294
134295/*
134296** Macro used to suppress compiler warnings for unused parameters.
134297*/
134298#define UNUSED_PARAMETER(x) (void)(x)
134299
134300/*
134301** Activate assert() only if SQLITE_TEST is enabled.
134302*/
134303#if !defined(NDEBUG) && !defined(SQLITE_DEBUG)
134304# define NDEBUG 1
134305#endif
134306
134307/*
134308** The TESTONLY macro is used to enclose variable declarations or
134309** other bits of code that are needed to support the arguments
134310** within testcase() and assert() macros.
134311*/
134312#if defined(SQLITE_DEBUG) || defined(SQLITE_COVERAGE_TEST)
134313# define TESTONLY(X)  X
134314#else
134315# define TESTONLY(X)
134316#endif
134317
134318#endif /* SQLITE_AMALGAMATION */
134319
134320#ifdef SQLITE_DEBUG
134321SQLITE_PRIVATE int sqlite3Fts3Corrupt(void);
134322# define FTS_CORRUPT_VTAB sqlite3Fts3Corrupt()
134323#else
134324# define FTS_CORRUPT_VTAB SQLITE_CORRUPT_VTAB
134325#endif
134326
134327typedef struct Fts3Table Fts3Table;
134328typedef struct Fts3Cursor Fts3Cursor;
134329typedef struct Fts3Expr Fts3Expr;
134330typedef struct Fts3Phrase Fts3Phrase;
134331typedef struct Fts3PhraseToken Fts3PhraseToken;
134332
134333typedef struct Fts3Doclist Fts3Doclist;
134334typedef struct Fts3SegFilter Fts3SegFilter;
134335typedef struct Fts3DeferredToken Fts3DeferredToken;
134336typedef struct Fts3SegReader Fts3SegReader;
134337typedef struct Fts3MultiSegReader Fts3MultiSegReader;
134338
134339typedef struct MatchinfoBuffer MatchinfoBuffer;
134340
134341/*
134342** A connection to a fulltext index is an instance of the following
134343** structure. The xCreate and xConnect methods create an instance
134344** of this structure and xDestroy and xDisconnect free that instance.
134345** All other methods receive a pointer to the structure as one of their
134346** arguments.
134347*/
134348struct Fts3Table {
134349  sqlite3_vtab base;              /* Base class used by SQLite core */
134350  sqlite3 *db;                    /* The database connection */
134351  const char *zDb;                /* logical database name */
134352  const char *zName;              /* virtual table name */
134353  int nColumn;                    /* number of named columns in virtual table */
134354  char **azColumn;                /* column names.  malloced */
134355  u8 *abNotindexed;               /* True for 'notindexed' columns */
134356  sqlite3_tokenizer *pTokenizer;  /* tokenizer for inserts and queries */
134357  char *zContentTbl;              /* content=xxx option, or NULL */
134358  char *zLanguageid;              /* languageid=xxx option, or NULL */
134359  int nAutoincrmerge;             /* Value configured by 'automerge' */
134360  u32 nLeafAdd;                   /* Number of leaf blocks added this trans */
134361
134362  /* Precompiled statements used by the implementation. Each of these
134363  ** statements is run and reset within a single virtual table API call.
134364  */
134365  sqlite3_stmt *aStmt[40];
134366
134367  char *zReadExprlist;
134368  char *zWriteExprlist;
134369
134370  int nNodeSize;                  /* Soft limit for node size */
134371  u8 bFts4;                       /* True for FTS4, false for FTS3 */
134372  u8 bHasStat;                    /* True if %_stat table exists (2==unknown) */
134373  u8 bHasDocsize;                 /* True if %_docsize table exists */
134374  u8 bDescIdx;                    /* True if doclists are in reverse order */
134375  u8 bIgnoreSavepoint;            /* True to ignore xSavepoint invocations */
134376  int nPgsz;                      /* Page size for host database */
134377  char *zSegmentsTbl;             /* Name of %_segments table */
134378  sqlite3_blob *pSegments;        /* Blob handle open on %_segments table */
134379
134380  /*
134381  ** The following array of hash tables is used to buffer pending index
134382  ** updates during transactions. All pending updates buffered at any one
134383  ** time must share a common language-id (see the FTS4 langid= feature).
134384  ** The current language id is stored in variable iPrevLangid.
134385  **
134386  ** A single FTS4 table may have multiple full-text indexes. For each index
134387  ** there is an entry in the aIndex[] array. Index 0 is an index of all the
134388  ** terms that appear in the document set. Each subsequent index in aIndex[]
134389  ** is an index of prefixes of a specific length.
134390  **
134391  ** Variable nPendingData contains an estimate the memory consumed by the
134392  ** pending data structures, including hash table overhead, but not including
134393  ** malloc overhead.  When nPendingData exceeds nMaxPendingData, all hash
134394  ** tables are flushed to disk. Variable iPrevDocid is the docid of the most
134395  ** recently inserted record.
134396  */
134397  int nIndex;                     /* Size of aIndex[] */
134398  struct Fts3Index {
134399    int nPrefix;                  /* Prefix length (0 for main terms index) */
134400    Fts3Hash hPending;            /* Pending terms table for this index */
134401  } *aIndex;
134402  int nMaxPendingData;            /* Max pending data before flush to disk */
134403  int nPendingData;               /* Current bytes of pending data */
134404  sqlite_int64 iPrevDocid;        /* Docid of most recently inserted document */
134405  int iPrevLangid;                /* Langid of recently inserted document */
134406
134407#if defined(SQLITE_DEBUG) || defined(SQLITE_COVERAGE_TEST)
134408  /* State variables used for validating that the transaction control
134409  ** methods of the virtual table are called at appropriate times.  These
134410  ** values do not contribute to FTS functionality; they are used for
134411  ** verifying the operation of the SQLite core.
134412  */
134413  int inTransaction;     /* True after xBegin but before xCommit/xRollback */
134414  int mxSavepoint;       /* Largest valid xSavepoint integer */
134415#endif
134416
134417#ifdef SQLITE_TEST
134418  /* True to disable the incremental doclist optimization. This is controled
134419  ** by special insert command 'test-no-incr-doclist'.  */
134420  int bNoIncrDoclist;
134421#endif
134422};
134423
134424/*
134425** When the core wants to read from the virtual table, it creates a
134426** virtual table cursor (an instance of the following structure) using
134427** the xOpen method. Cursors are destroyed using the xClose method.
134428*/
134429struct Fts3Cursor {
134430  sqlite3_vtab_cursor base;       /* Base class used by SQLite core */
134431  i16 eSearch;                    /* Search strategy (see below) */
134432  u8 isEof;                       /* True if at End Of Results */
134433  u8 isRequireSeek;               /* True if must seek pStmt to %_content row */
134434  sqlite3_stmt *pStmt;            /* Prepared statement in use by the cursor */
134435  Fts3Expr *pExpr;                /* Parsed MATCH query string */
134436  int iLangid;                    /* Language being queried for */
134437  int nPhrase;                    /* Number of matchable phrases in query */
134438  Fts3DeferredToken *pDeferred;   /* Deferred search tokens, if any */
134439  sqlite3_int64 iPrevId;          /* Previous id read from aDoclist */
134440  char *pNextId;                  /* Pointer into the body of aDoclist */
134441  char *aDoclist;                 /* List of docids for full-text queries */
134442  int nDoclist;                   /* Size of buffer at aDoclist */
134443  u8 bDesc;                       /* True to sort in descending order */
134444  int eEvalmode;                  /* An FTS3_EVAL_XX constant */
134445  int nRowAvg;                    /* Average size of database rows, in pages */
134446  sqlite3_int64 nDoc;             /* Documents in table */
134447  i64 iMinDocid;                  /* Minimum docid to return */
134448  i64 iMaxDocid;                  /* Maximum docid to return */
134449  int isMatchinfoNeeded;          /* True when aMatchinfo[] needs filling in */
134450  MatchinfoBuffer *pMIBuffer;     /* Buffer for matchinfo data */
134451};
134452
134453#define FTS3_EVAL_FILTER    0
134454#define FTS3_EVAL_NEXT      1
134455#define FTS3_EVAL_MATCHINFO 2
134456
134457/*
134458** The Fts3Cursor.eSearch member is always set to one of the following.
134459** Actualy, Fts3Cursor.eSearch can be greater than or equal to
134460** FTS3_FULLTEXT_SEARCH.  If so, then Fts3Cursor.eSearch - 2 is the index
134461** of the column to be searched.  For example, in
134462**
134463**     CREATE VIRTUAL TABLE ex1 USING fts3(a,b,c,d);
134464**     SELECT docid FROM ex1 WHERE b MATCH 'one two three';
134465**
134466** Because the LHS of the MATCH operator is 2nd column "b",
134467** Fts3Cursor.eSearch will be set to FTS3_FULLTEXT_SEARCH+1.  (+0 for a,
134468** +1 for b, +2 for c, +3 for d.)  If the LHS of MATCH were "ex1"
134469** indicating that all columns should be searched,
134470** then eSearch would be set to FTS3_FULLTEXT_SEARCH+4.
134471*/
134472#define FTS3_FULLSCAN_SEARCH 0    /* Linear scan of %_content table */
134473#define FTS3_DOCID_SEARCH    1    /* Lookup by rowid on %_content table */
134474#define FTS3_FULLTEXT_SEARCH 2    /* Full-text index search */
134475
134476/*
134477** The lower 16-bits of the sqlite3_index_info.idxNum value set by
134478** the xBestIndex() method contains the Fts3Cursor.eSearch value described
134479** above. The upper 16-bits contain a combination of the following
134480** bits, used to describe extra constraints on full-text searches.
134481*/
134482#define FTS3_HAVE_LANGID    0x00010000      /* languageid=? */
134483#define FTS3_HAVE_DOCID_GE  0x00020000      /* docid>=? */
134484#define FTS3_HAVE_DOCID_LE  0x00040000      /* docid<=? */
134485
134486struct Fts3Doclist {
134487  char *aAll;                    /* Array containing doclist (or NULL) */
134488  int nAll;                      /* Size of a[] in bytes */
134489  char *pNextDocid;              /* Pointer to next docid */
134490
134491  sqlite3_int64 iDocid;          /* Current docid (if pList!=0) */
134492  int bFreeList;                 /* True if pList should be sqlite3_free()d */
134493  char *pList;                   /* Pointer to position list following iDocid */
134494  int nList;                     /* Length of position list */
134495};
134496
134497/*
134498** A "phrase" is a sequence of one or more tokens that must match in
134499** sequence.  A single token is the base case and the most common case.
134500** For a sequence of tokens contained in double-quotes (i.e. "one two three")
134501** nToken will be the number of tokens in the string.
134502*/
134503struct Fts3PhraseToken {
134504  char *z;                        /* Text of the token */
134505  int n;                          /* Number of bytes in buffer z */
134506  int isPrefix;                   /* True if token ends with a "*" character */
134507  int bFirst;                     /* True if token must appear at position 0 */
134508
134509  /* Variables above this point are populated when the expression is
134510  ** parsed (by code in fts3_expr.c). Below this point the variables are
134511  ** used when evaluating the expression. */
134512  Fts3DeferredToken *pDeferred;   /* Deferred token object for this token */
134513  Fts3MultiSegReader *pSegcsr;    /* Segment-reader for this token */
134514};
134515
134516struct Fts3Phrase {
134517  /* Cache of doclist for this phrase. */
134518  Fts3Doclist doclist;
134519  int bIncr;                 /* True if doclist is loaded incrementally */
134520  int iDoclistToken;
134521
134522  /* Used by sqlite3Fts3EvalPhrasePoslist() if this is a descendent of an
134523  ** OR condition.  */
134524  char *pOrPoslist;
134525  i64 iOrDocid;
134526
134527  /* Variables below this point are populated by fts3_expr.c when parsing
134528  ** a MATCH expression. Everything above is part of the evaluation phase.
134529  */
134530  int nToken;                /* Number of tokens in the phrase */
134531  int iColumn;               /* Index of column this phrase must match */
134532  Fts3PhraseToken aToken[1]; /* One entry for each token in the phrase */
134533};
134534
134535/*
134536** A tree of these objects forms the RHS of a MATCH operator.
134537**
134538** If Fts3Expr.eType is FTSQUERY_PHRASE and isLoaded is true, then aDoclist
134539** points to a malloced buffer, size nDoclist bytes, containing the results
134540** of this phrase query in FTS3 doclist format. As usual, the initial
134541** "Length" field found in doclists stored on disk is omitted from this
134542** buffer.
134543**
134544** Variable aMI is used only for FTSQUERY_NEAR nodes to store the global
134545** matchinfo data. If it is not NULL, it points to an array of size nCol*3,
134546** where nCol is the number of columns in the queried FTS table. The array
134547** is populated as follows:
134548**
134549**   aMI[iCol*3 + 0] = Undefined
134550**   aMI[iCol*3 + 1] = Number of occurrences
134551**   aMI[iCol*3 + 2] = Number of rows containing at least one instance
134552**
134553** The aMI array is allocated using sqlite3_malloc(). It should be freed
134554** when the expression node is.
134555*/
134556struct Fts3Expr {
134557  int eType;                 /* One of the FTSQUERY_XXX values defined below */
134558  int nNear;                 /* Valid if eType==FTSQUERY_NEAR */
134559  Fts3Expr *pParent;         /* pParent->pLeft==this or pParent->pRight==this */
134560  Fts3Expr *pLeft;           /* Left operand */
134561  Fts3Expr *pRight;          /* Right operand */
134562  Fts3Phrase *pPhrase;       /* Valid if eType==FTSQUERY_PHRASE */
134563
134564  /* The following are used by the fts3_eval.c module. */
134565  sqlite3_int64 iDocid;      /* Current docid */
134566  u8 bEof;                   /* True this expression is at EOF already */
134567  u8 bStart;                 /* True if iDocid is valid */
134568  u8 bDeferred;              /* True if this expression is entirely deferred */
134569
134570  /* The following are used by the fts3_snippet.c module. */
134571  int iPhrase;               /* Index of this phrase in matchinfo() results */
134572  u32 *aMI;                  /* See above */
134573};
134574
134575/*
134576** Candidate values for Fts3Query.eType. Note that the order of the first
134577** four values is in order of precedence when parsing expressions. For
134578** example, the following:
134579**
134580**   "a OR b AND c NOT d NEAR e"
134581**
134582** is equivalent to:
134583**
134584**   "a OR (b AND (c NOT (d NEAR e)))"
134585*/
134586#define FTSQUERY_NEAR   1
134587#define FTSQUERY_NOT    2
134588#define FTSQUERY_AND    3
134589#define FTSQUERY_OR     4
134590#define FTSQUERY_PHRASE 5
134591
134592
134593/* fts3_write.c */
134594SQLITE_PRIVATE int sqlite3Fts3UpdateMethod(sqlite3_vtab*,int,sqlite3_value**,sqlite3_int64*);
134595SQLITE_PRIVATE int sqlite3Fts3PendingTermsFlush(Fts3Table *);
134596SQLITE_PRIVATE void sqlite3Fts3PendingTermsClear(Fts3Table *);
134597SQLITE_PRIVATE int sqlite3Fts3Optimize(Fts3Table *);
134598SQLITE_PRIVATE int sqlite3Fts3SegReaderNew(int, int, sqlite3_int64,
134599  sqlite3_int64, sqlite3_int64, const char *, int, Fts3SegReader**);
134600SQLITE_PRIVATE int sqlite3Fts3SegReaderPending(
134601  Fts3Table*,int,const char*,int,int,Fts3SegReader**);
134602SQLITE_PRIVATE void sqlite3Fts3SegReaderFree(Fts3SegReader *);
134603SQLITE_PRIVATE int sqlite3Fts3AllSegdirs(Fts3Table*, int, int, int, sqlite3_stmt **);
134604SQLITE_PRIVATE int sqlite3Fts3ReadBlock(Fts3Table*, sqlite3_int64, char **, int*, int*);
134605
134606SQLITE_PRIVATE int sqlite3Fts3SelectDoctotal(Fts3Table *, sqlite3_stmt **);
134607SQLITE_PRIVATE int sqlite3Fts3SelectDocsize(Fts3Table *, sqlite3_int64, sqlite3_stmt **);
134608
134609#ifndef SQLITE_DISABLE_FTS4_DEFERRED
134610SQLITE_PRIVATE void sqlite3Fts3FreeDeferredTokens(Fts3Cursor *);
134611SQLITE_PRIVATE int sqlite3Fts3DeferToken(Fts3Cursor *, Fts3PhraseToken *, int);
134612SQLITE_PRIVATE int sqlite3Fts3CacheDeferredDoclists(Fts3Cursor *);
134613SQLITE_PRIVATE void sqlite3Fts3FreeDeferredDoclists(Fts3Cursor *);
134614SQLITE_PRIVATE int sqlite3Fts3DeferredTokenList(Fts3DeferredToken *, char **, int *);
134615#else
134616# define sqlite3Fts3FreeDeferredTokens(x)
134617# define sqlite3Fts3DeferToken(x,y,z) SQLITE_OK
134618# define sqlite3Fts3CacheDeferredDoclists(x) SQLITE_OK
134619# define sqlite3Fts3FreeDeferredDoclists(x)
134620# define sqlite3Fts3DeferredTokenList(x,y,z) SQLITE_OK
134621#endif
134622
134623SQLITE_PRIVATE void sqlite3Fts3SegmentsClose(Fts3Table *);
134624SQLITE_PRIVATE int sqlite3Fts3MaxLevel(Fts3Table *, int *);
134625
134626/* Special values interpreted by sqlite3SegReaderCursor() */
134627#define FTS3_SEGCURSOR_PENDING        -1
134628#define FTS3_SEGCURSOR_ALL            -2
134629
134630SQLITE_PRIVATE int sqlite3Fts3SegReaderStart(Fts3Table*, Fts3MultiSegReader*, Fts3SegFilter*);
134631SQLITE_PRIVATE int sqlite3Fts3SegReaderStep(Fts3Table *, Fts3MultiSegReader *);
134632SQLITE_PRIVATE void sqlite3Fts3SegReaderFinish(Fts3MultiSegReader *);
134633
134634SQLITE_PRIVATE int sqlite3Fts3SegReaderCursor(Fts3Table *,
134635    int, int, int, const char *, int, int, int, Fts3MultiSegReader *);
134636
134637/* Flags allowed as part of the 4th argument to SegmentReaderIterate() */
134638#define FTS3_SEGMENT_REQUIRE_POS   0x00000001
134639#define FTS3_SEGMENT_IGNORE_EMPTY  0x00000002
134640#define FTS3_SEGMENT_COLUMN_FILTER 0x00000004
134641#define FTS3_SEGMENT_PREFIX        0x00000008
134642#define FTS3_SEGMENT_SCAN          0x00000010
134643#define FTS3_SEGMENT_FIRST         0x00000020
134644
134645/* Type passed as 4th argument to SegmentReaderIterate() */
134646struct Fts3SegFilter {
134647  const char *zTerm;
134648  int nTerm;
134649  int iCol;
134650  int flags;
134651};
134652
134653struct Fts3MultiSegReader {
134654  /* Used internally by sqlite3Fts3SegReaderXXX() calls */
134655  Fts3SegReader **apSegment;      /* Array of Fts3SegReader objects */
134656  int nSegment;                   /* Size of apSegment array */
134657  int nAdvance;                   /* How many seg-readers to advance */
134658  Fts3SegFilter *pFilter;         /* Pointer to filter object */
134659  char *aBuffer;                  /* Buffer to merge doclists in */
134660  int nBuffer;                    /* Allocated size of aBuffer[] in bytes */
134661
134662  int iColFilter;                 /* If >=0, filter for this column */
134663  int bRestart;
134664
134665  /* Used by fts3.c only. */
134666  int nCost;                      /* Cost of running iterator */
134667  int bLookup;                    /* True if a lookup of a single entry. */
134668
134669  /* Output values. Valid only after Fts3SegReaderStep() returns SQLITE_ROW. */
134670  char *zTerm;                    /* Pointer to term buffer */
134671  int nTerm;                      /* Size of zTerm in bytes */
134672  char *aDoclist;                 /* Pointer to doclist buffer */
134673  int nDoclist;                   /* Size of aDoclist[] in bytes */
134674};
134675
134676SQLITE_PRIVATE int sqlite3Fts3Incrmerge(Fts3Table*,int,int);
134677
134678#define fts3GetVarint32(p, piVal) (                                           \
134679  (*(u8*)(p)&0x80) ? sqlite3Fts3GetVarint32(p, piVal) : (*piVal=*(u8*)(p), 1) \
134680)
134681
134682/* fts3.c */
134683SQLITE_PRIVATE void sqlite3Fts3ErrMsg(char**,const char*,...);
134684SQLITE_PRIVATE int sqlite3Fts3PutVarint(char *, sqlite3_int64);
134685SQLITE_PRIVATE int sqlite3Fts3GetVarint(const char *, sqlite_int64 *);
134686SQLITE_PRIVATE int sqlite3Fts3GetVarint32(const char *, int *);
134687SQLITE_PRIVATE int sqlite3Fts3VarintLen(sqlite3_uint64);
134688SQLITE_PRIVATE void sqlite3Fts3Dequote(char *);
134689SQLITE_PRIVATE void sqlite3Fts3DoclistPrev(int,char*,int,char**,sqlite3_int64*,int*,u8*);
134690SQLITE_PRIVATE int sqlite3Fts3EvalPhraseStats(Fts3Cursor *, Fts3Expr *, u32 *);
134691SQLITE_PRIVATE int sqlite3Fts3FirstFilter(sqlite3_int64, char *, int, char *);
134692SQLITE_PRIVATE void sqlite3Fts3CreateStatTable(int*, Fts3Table*);
134693SQLITE_PRIVATE int sqlite3Fts3EvalTestDeferred(Fts3Cursor *pCsr, int *pRc);
134694
134695/* fts3_tokenizer.c */
134696SQLITE_PRIVATE const char *sqlite3Fts3NextToken(const char *, int *);
134697SQLITE_PRIVATE int sqlite3Fts3InitHashTable(sqlite3 *, Fts3Hash *, const char *);
134698SQLITE_PRIVATE int sqlite3Fts3InitTokenizer(Fts3Hash *pHash, const char *,
134699    sqlite3_tokenizer **, char **
134700);
134701SQLITE_PRIVATE int sqlite3Fts3IsIdChar(char);
134702
134703/* fts3_snippet.c */
134704SQLITE_PRIVATE void sqlite3Fts3Offsets(sqlite3_context*, Fts3Cursor*);
134705SQLITE_PRIVATE void sqlite3Fts3Snippet(sqlite3_context *, Fts3Cursor *, const char *,
134706  const char *, const char *, int, int
134707);
134708SQLITE_PRIVATE void sqlite3Fts3Matchinfo(sqlite3_context *, Fts3Cursor *, const char *);
134709SQLITE_PRIVATE void sqlite3Fts3MIBufferFree(MatchinfoBuffer *p);
134710
134711/* fts3_expr.c */
134712SQLITE_PRIVATE int sqlite3Fts3ExprParse(sqlite3_tokenizer *, int,
134713  char **, int, int, int, const char *, int, Fts3Expr **, char **
134714);
134715SQLITE_PRIVATE void sqlite3Fts3ExprFree(Fts3Expr *);
134716#ifdef SQLITE_TEST
134717SQLITE_PRIVATE int sqlite3Fts3ExprInitTestInterface(sqlite3 *db);
134718SQLITE_PRIVATE int sqlite3Fts3InitTerm(sqlite3 *db);
134719#endif
134720
134721SQLITE_PRIVATE int sqlite3Fts3OpenTokenizer(sqlite3_tokenizer *, int, const char *, int,
134722  sqlite3_tokenizer_cursor **
134723);
134724
134725/* fts3_aux.c */
134726SQLITE_PRIVATE int sqlite3Fts3InitAux(sqlite3 *db);
134727
134728SQLITE_PRIVATE void sqlite3Fts3EvalPhraseCleanup(Fts3Phrase *);
134729
134730SQLITE_PRIVATE int sqlite3Fts3MsrIncrStart(
134731    Fts3Table*, Fts3MultiSegReader*, int, const char*, int);
134732SQLITE_PRIVATE int sqlite3Fts3MsrIncrNext(
134733    Fts3Table *, Fts3MultiSegReader *, sqlite3_int64 *, char **, int *);
134734SQLITE_PRIVATE int sqlite3Fts3EvalPhrasePoslist(Fts3Cursor *, Fts3Expr *, int iCol, char **);
134735SQLITE_PRIVATE int sqlite3Fts3MsrOvfl(Fts3Cursor *, Fts3MultiSegReader *, int *);
134736SQLITE_PRIVATE int sqlite3Fts3MsrIncrRestart(Fts3MultiSegReader *pCsr);
134737
134738/* fts3_tokenize_vtab.c */
134739SQLITE_PRIVATE int sqlite3Fts3InitTok(sqlite3*, Fts3Hash *);
134740
134741/* fts3_unicode2.c (functions generated by parsing unicode text files) */
134742#ifndef SQLITE_DISABLE_FTS3_UNICODE
134743SQLITE_PRIVATE int sqlite3FtsUnicodeFold(int, int);
134744SQLITE_PRIVATE int sqlite3FtsUnicodeIsalnum(int);
134745SQLITE_PRIVATE int sqlite3FtsUnicodeIsdiacritic(int);
134746#endif
134747
134748#endif /* !SQLITE_CORE || SQLITE_ENABLE_FTS3 */
134749#endif /* _FTSINT_H */
134750
134751/************** End of fts3Int.h *********************************************/
134752/************** Continuing where we left off in fts3.c ***********************/
134753#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
134754
134755#if defined(SQLITE_ENABLE_FTS3) && !defined(SQLITE_CORE)
134756# define SQLITE_CORE 1
134757#endif
134758
134759/* #include <assert.h> */
134760/* #include <stdlib.h> */
134761/* #include <stddef.h> */
134762/* #include <stdio.h> */
134763/* #include <string.h> */
134764/* #include <stdarg.h> */
134765
134766/* #include "fts3.h" */
134767#ifndef SQLITE_CORE
134768/* # include "sqlite3ext.h" */
134769  SQLITE_EXTENSION_INIT1
134770#endif
134771
134772static int fts3EvalNext(Fts3Cursor *pCsr);
134773static int fts3EvalStart(Fts3Cursor *pCsr);
134774static int fts3TermSegReaderCursor(
134775    Fts3Cursor *, const char *, int, int, Fts3MultiSegReader **);
134776
134777#ifndef SQLITE_AMALGAMATION
134778# if defined(SQLITE_DEBUG)
134779SQLITE_PRIVATE int sqlite3Fts3Always(int b) { assert( b ); return b; }
134780SQLITE_PRIVATE int sqlite3Fts3Never(int b)  { assert( !b ); return b; }
134781# endif
134782#endif
134783
134784/*
134785** Write a 64-bit variable-length integer to memory starting at p[0].
134786** The length of data written will be between 1 and FTS3_VARINT_MAX bytes.
134787** The number of bytes written is returned.
134788*/
134789SQLITE_PRIVATE int sqlite3Fts3PutVarint(char *p, sqlite_int64 v){
134790  unsigned char *q = (unsigned char *) p;
134791  sqlite_uint64 vu = v;
134792  do{
134793    *q++ = (unsigned char) ((vu & 0x7f) | 0x80);
134794    vu >>= 7;
134795  }while( vu!=0 );
134796  q[-1] &= 0x7f;  /* turn off high bit in final byte */
134797  assert( q - (unsigned char *)p <= FTS3_VARINT_MAX );
134798  return (int) (q - (unsigned char *)p);
134799}
134800
134801#define GETVARINT_STEP(v, ptr, shift, mask1, mask2, var, ret) \
134802  v = (v & mask1) | ( (*ptr++) << shift );                    \
134803  if( (v & mask2)==0 ){ var = v; return ret; }
134804#define GETVARINT_INIT(v, ptr, shift, mask1, mask2, var, ret) \
134805  v = (*ptr++);                                               \
134806  if( (v & mask2)==0 ){ var = v; return ret; }
134807
134808/*
134809** Read a 64-bit variable-length integer from memory starting at p[0].
134810** Return the number of bytes read, or 0 on error.
134811** The value is stored in *v.
134812*/
134813SQLITE_PRIVATE int sqlite3Fts3GetVarint(const char *p, sqlite_int64 *v){
134814  const char *pStart = p;
134815  u32 a;
134816  u64 b;
134817  int shift;
134818
134819  GETVARINT_INIT(a, p, 0,  0x00,     0x80, *v, 1);
134820  GETVARINT_STEP(a, p, 7,  0x7F,     0x4000, *v, 2);
134821  GETVARINT_STEP(a, p, 14, 0x3FFF,   0x200000, *v, 3);
134822  GETVARINT_STEP(a, p, 21, 0x1FFFFF, 0x10000000, *v, 4);
134823  b = (a & 0x0FFFFFFF );
134824
134825  for(shift=28; shift<=63; shift+=7){
134826    u64 c = *p++;
134827    b += (c&0x7F) << shift;
134828    if( (c & 0x80)==0 ) break;
134829  }
134830  *v = b;
134831  return (int)(p - pStart);
134832}
134833
134834/*
134835** Similar to sqlite3Fts3GetVarint(), except that the output is truncated to a
134836** 32-bit integer before it is returned.
134837*/
134838SQLITE_PRIVATE int sqlite3Fts3GetVarint32(const char *p, int *pi){
134839  u32 a;
134840
134841#ifndef fts3GetVarint32
134842  GETVARINT_INIT(a, p, 0,  0x00,     0x80, *pi, 1);
134843#else
134844  a = (*p++);
134845  assert( a & 0x80 );
134846#endif
134847
134848  GETVARINT_STEP(a, p, 7,  0x7F,     0x4000, *pi, 2);
134849  GETVARINT_STEP(a, p, 14, 0x3FFF,   0x200000, *pi, 3);
134850  GETVARINT_STEP(a, p, 21, 0x1FFFFF, 0x10000000, *pi, 4);
134851  a = (a & 0x0FFFFFFF );
134852  *pi = (int)(a | ((u32)(*p & 0x0F) << 28));
134853  return 5;
134854}
134855
134856/*
134857** Return the number of bytes required to encode v as a varint
134858*/
134859SQLITE_PRIVATE int sqlite3Fts3VarintLen(sqlite3_uint64 v){
134860  int i = 0;
134861  do{
134862    i++;
134863    v >>= 7;
134864  }while( v!=0 );
134865  return i;
134866}
134867
134868/*
134869** Convert an SQL-style quoted string into a normal string by removing
134870** the quote characters.  The conversion is done in-place.  If the
134871** input does not begin with a quote character, then this routine
134872** is a no-op.
134873**
134874** Examples:
134875**
134876**     "abc"   becomes   abc
134877**     'xyz'   becomes   xyz
134878**     [pqr]   becomes   pqr
134879**     `mno`   becomes   mno
134880**
134881*/
134882SQLITE_PRIVATE void sqlite3Fts3Dequote(char *z){
134883  char quote;                     /* Quote character (if any ) */
134884
134885  quote = z[0];
134886  if( quote=='[' || quote=='\'' || quote=='"' || quote=='`' ){
134887    int iIn = 1;                  /* Index of next byte to read from input */
134888    int iOut = 0;                 /* Index of next byte to write to output */
134889
134890    /* If the first byte was a '[', then the close-quote character is a ']' */
134891    if( quote=='[' ) quote = ']';
134892
134893    while( z[iIn] ){
134894      if( z[iIn]==quote ){
134895        if( z[iIn+1]!=quote ) break;
134896        z[iOut++] = quote;
134897        iIn += 2;
134898      }else{
134899        z[iOut++] = z[iIn++];
134900      }
134901    }
134902    z[iOut] = '\0';
134903  }
134904}
134905
134906/*
134907** Read a single varint from the doclist at *pp and advance *pp to point
134908** to the first byte past the end of the varint.  Add the value of the varint
134909** to *pVal.
134910*/
134911static void fts3GetDeltaVarint(char **pp, sqlite3_int64 *pVal){
134912  sqlite3_int64 iVal;
134913  *pp += sqlite3Fts3GetVarint(*pp, &iVal);
134914  *pVal += iVal;
134915}
134916
134917/*
134918** When this function is called, *pp points to the first byte following a
134919** varint that is part of a doclist (or position-list, or any other list
134920** of varints). This function moves *pp to point to the start of that varint,
134921** and sets *pVal by the varint value.
134922**
134923** Argument pStart points to the first byte of the doclist that the
134924** varint is part of.
134925*/
134926static void fts3GetReverseVarint(
134927  char **pp,
134928  char *pStart,
134929  sqlite3_int64 *pVal
134930){
134931  sqlite3_int64 iVal;
134932  char *p;
134933
134934  /* Pointer p now points at the first byte past the varint we are
134935  ** interested in. So, unless the doclist is corrupt, the 0x80 bit is
134936  ** clear on character p[-1]. */
134937  for(p = (*pp)-2; p>=pStart && *p&0x80; p--);
134938  p++;
134939  *pp = p;
134940
134941  sqlite3Fts3GetVarint(p, &iVal);
134942  *pVal = iVal;
134943}
134944
134945/*
134946** The xDisconnect() virtual table method.
134947*/
134948static int fts3DisconnectMethod(sqlite3_vtab *pVtab){
134949  Fts3Table *p = (Fts3Table *)pVtab;
134950  int i;
134951
134952  assert( p->nPendingData==0 );
134953  assert( p->pSegments==0 );
134954
134955  /* Free any prepared statements held */
134956  for(i=0; i<SizeofArray(p->aStmt); i++){
134957    sqlite3_finalize(p->aStmt[i]);
134958  }
134959  sqlite3_free(p->zSegmentsTbl);
134960  sqlite3_free(p->zReadExprlist);
134961  sqlite3_free(p->zWriteExprlist);
134962  sqlite3_free(p->zContentTbl);
134963  sqlite3_free(p->zLanguageid);
134964
134965  /* Invoke the tokenizer destructor to free the tokenizer. */
134966  p->pTokenizer->pModule->xDestroy(p->pTokenizer);
134967
134968  sqlite3_free(p);
134969  return SQLITE_OK;
134970}
134971
134972/*
134973** Write an error message into *pzErr
134974*/
134975SQLITE_PRIVATE void sqlite3Fts3ErrMsg(char **pzErr, const char *zFormat, ...){
134976  va_list ap;
134977  sqlite3_free(*pzErr);
134978  va_start(ap, zFormat);
134979  *pzErr = sqlite3_vmprintf(zFormat, ap);
134980  va_end(ap);
134981}
134982
134983/*
134984** Construct one or more SQL statements from the format string given
134985** and then evaluate those statements. The success code is written
134986** into *pRc.
134987**
134988** If *pRc is initially non-zero then this routine is a no-op.
134989*/
134990static void fts3DbExec(
134991  int *pRc,              /* Success code */
134992  sqlite3 *db,           /* Database in which to run SQL */
134993  const char *zFormat,   /* Format string for SQL */
134994  ...                    /* Arguments to the format string */
134995){
134996  va_list ap;
134997  char *zSql;
134998  if( *pRc ) return;
134999  va_start(ap, zFormat);
135000  zSql = sqlite3_vmprintf(zFormat, ap);
135001  va_end(ap);
135002  if( zSql==0 ){
135003    *pRc = SQLITE_NOMEM;
135004  }else{
135005    *pRc = sqlite3_exec(db, zSql, 0, 0, 0);
135006    sqlite3_free(zSql);
135007  }
135008}
135009
135010/*
135011** The xDestroy() virtual table method.
135012*/
135013static int fts3DestroyMethod(sqlite3_vtab *pVtab){
135014  Fts3Table *p = (Fts3Table *)pVtab;
135015  int rc = SQLITE_OK;              /* Return code */
135016  const char *zDb = p->zDb;        /* Name of database (e.g. "main", "temp") */
135017  sqlite3 *db = p->db;             /* Database handle */
135018
135019  /* Drop the shadow tables */
135020  if( p->zContentTbl==0 ){
135021    fts3DbExec(&rc, db, "DROP TABLE IF EXISTS %Q.'%q_content'", zDb, p->zName);
135022  }
135023  fts3DbExec(&rc, db, "DROP TABLE IF EXISTS %Q.'%q_segments'", zDb,p->zName);
135024  fts3DbExec(&rc, db, "DROP TABLE IF EXISTS %Q.'%q_segdir'", zDb, p->zName);
135025  fts3DbExec(&rc, db, "DROP TABLE IF EXISTS %Q.'%q_docsize'", zDb, p->zName);
135026  fts3DbExec(&rc, db, "DROP TABLE IF EXISTS %Q.'%q_stat'", zDb, p->zName);
135027
135028  /* If everything has worked, invoke fts3DisconnectMethod() to free the
135029  ** memory associated with the Fts3Table structure and return SQLITE_OK.
135030  ** Otherwise, return an SQLite error code.
135031  */
135032  return (rc==SQLITE_OK ? fts3DisconnectMethod(pVtab) : rc);
135033}
135034
135035
135036/*
135037** Invoke sqlite3_declare_vtab() to declare the schema for the FTS3 table
135038** passed as the first argument. This is done as part of the xConnect()
135039** and xCreate() methods.
135040**
135041** If *pRc is non-zero when this function is called, it is a no-op.
135042** Otherwise, if an error occurs, an SQLite error code is stored in *pRc
135043** before returning.
135044*/
135045static void fts3DeclareVtab(int *pRc, Fts3Table *p){
135046  if( *pRc==SQLITE_OK ){
135047    int i;                        /* Iterator variable */
135048    int rc;                       /* Return code */
135049    char *zSql;                   /* SQL statement passed to declare_vtab() */
135050    char *zCols;                  /* List of user defined columns */
135051    const char *zLanguageid;
135052
135053    zLanguageid = (p->zLanguageid ? p->zLanguageid : "__langid");
135054    sqlite3_vtab_config(p->db, SQLITE_VTAB_CONSTRAINT_SUPPORT, 1);
135055
135056    /* Create a list of user columns for the virtual table */
135057    zCols = sqlite3_mprintf("%Q, ", p->azColumn[0]);
135058    for(i=1; zCols && i<p->nColumn; i++){
135059      zCols = sqlite3_mprintf("%z%Q, ", zCols, p->azColumn[i]);
135060    }
135061
135062    /* Create the whole "CREATE TABLE" statement to pass to SQLite */
135063    zSql = sqlite3_mprintf(
135064        "CREATE TABLE x(%s %Q HIDDEN, docid HIDDEN, %Q HIDDEN)",
135065        zCols, p->zName, zLanguageid
135066    );
135067    if( !zCols || !zSql ){
135068      rc = SQLITE_NOMEM;
135069    }else{
135070      rc = sqlite3_declare_vtab(p->db, zSql);
135071    }
135072
135073    sqlite3_free(zSql);
135074    sqlite3_free(zCols);
135075    *pRc = rc;
135076  }
135077}
135078
135079/*
135080** Create the %_stat table if it does not already exist.
135081*/
135082SQLITE_PRIVATE void sqlite3Fts3CreateStatTable(int *pRc, Fts3Table *p){
135083  fts3DbExec(pRc, p->db,
135084      "CREATE TABLE IF NOT EXISTS %Q.'%q_stat'"
135085          "(id INTEGER PRIMARY KEY, value BLOB);",
135086      p->zDb, p->zName
135087  );
135088  if( (*pRc)==SQLITE_OK ) p->bHasStat = 1;
135089}
135090
135091/*
135092** Create the backing store tables (%_content, %_segments and %_segdir)
135093** required by the FTS3 table passed as the only argument. This is done
135094** as part of the vtab xCreate() method.
135095**
135096** If the p->bHasDocsize boolean is true (indicating that this is an
135097** FTS4 table, not an FTS3 table) then also create the %_docsize and
135098** %_stat tables required by FTS4.
135099*/
135100static int fts3CreateTables(Fts3Table *p){
135101  int rc = SQLITE_OK;             /* Return code */
135102  int i;                          /* Iterator variable */
135103  sqlite3 *db = p->db;            /* The database connection */
135104
135105  if( p->zContentTbl==0 ){
135106    const char *zLanguageid = p->zLanguageid;
135107    char *zContentCols;           /* Columns of %_content table */
135108
135109    /* Create a list of user columns for the content table */
135110    zContentCols = sqlite3_mprintf("docid INTEGER PRIMARY KEY");
135111    for(i=0; zContentCols && i<p->nColumn; i++){
135112      char *z = p->azColumn[i];
135113      zContentCols = sqlite3_mprintf("%z, 'c%d%q'", zContentCols, i, z);
135114    }
135115    if( zLanguageid && zContentCols ){
135116      zContentCols = sqlite3_mprintf("%z, langid", zContentCols, zLanguageid);
135117    }
135118    if( zContentCols==0 ) rc = SQLITE_NOMEM;
135119
135120    /* Create the content table */
135121    fts3DbExec(&rc, db,
135122       "CREATE TABLE %Q.'%q_content'(%s)",
135123       p->zDb, p->zName, zContentCols
135124    );
135125    sqlite3_free(zContentCols);
135126  }
135127
135128  /* Create other tables */
135129  fts3DbExec(&rc, db,
135130      "CREATE TABLE %Q.'%q_segments'(blockid INTEGER PRIMARY KEY, block BLOB);",
135131      p->zDb, p->zName
135132  );
135133  fts3DbExec(&rc, db,
135134      "CREATE TABLE %Q.'%q_segdir'("
135135        "level INTEGER,"
135136        "idx INTEGER,"
135137        "start_block INTEGER,"
135138        "leaves_end_block INTEGER,"
135139        "end_block INTEGER,"
135140        "root BLOB,"
135141        "PRIMARY KEY(level, idx)"
135142      ");",
135143      p->zDb, p->zName
135144  );
135145  if( p->bHasDocsize ){
135146    fts3DbExec(&rc, db,
135147        "CREATE TABLE %Q.'%q_docsize'(docid INTEGER PRIMARY KEY, size BLOB);",
135148        p->zDb, p->zName
135149    );
135150  }
135151  assert( p->bHasStat==p->bFts4 );
135152  if( p->bHasStat ){
135153    sqlite3Fts3CreateStatTable(&rc, p);
135154  }
135155  return rc;
135156}
135157
135158/*
135159** Store the current database page-size in bytes in p->nPgsz.
135160**
135161** If *pRc is non-zero when this function is called, it is a no-op.
135162** Otherwise, if an error occurs, an SQLite error code is stored in *pRc
135163** before returning.
135164*/
135165static void fts3DatabasePageSize(int *pRc, Fts3Table *p){
135166  if( *pRc==SQLITE_OK ){
135167    int rc;                       /* Return code */
135168    char *zSql;                   /* SQL text "PRAGMA %Q.page_size" */
135169    sqlite3_stmt *pStmt;          /* Compiled "PRAGMA %Q.page_size" statement */
135170
135171    zSql = sqlite3_mprintf("PRAGMA %Q.page_size", p->zDb);
135172    if( !zSql ){
135173      rc = SQLITE_NOMEM;
135174    }else{
135175      rc = sqlite3_prepare(p->db, zSql, -1, &pStmt, 0);
135176      if( rc==SQLITE_OK ){
135177        sqlite3_step(pStmt);
135178        p->nPgsz = sqlite3_column_int(pStmt, 0);
135179        rc = sqlite3_finalize(pStmt);
135180      }else if( rc==SQLITE_AUTH ){
135181        p->nPgsz = 1024;
135182        rc = SQLITE_OK;
135183      }
135184    }
135185    assert( p->nPgsz>0 || rc!=SQLITE_OK );
135186    sqlite3_free(zSql);
135187    *pRc = rc;
135188  }
135189}
135190
135191/*
135192** "Special" FTS4 arguments are column specifications of the following form:
135193**
135194**   <key> = <value>
135195**
135196** There may not be whitespace surrounding the "=" character. The <value>
135197** term may be quoted, but the <key> may not.
135198*/
135199static int fts3IsSpecialColumn(
135200  const char *z,
135201  int *pnKey,
135202  char **pzValue
135203){
135204  char *zValue;
135205  const char *zCsr = z;
135206
135207  while( *zCsr!='=' ){
135208    if( *zCsr=='\0' ) return 0;
135209    zCsr++;
135210  }
135211
135212  *pnKey = (int)(zCsr-z);
135213  zValue = sqlite3_mprintf("%s", &zCsr[1]);
135214  if( zValue ){
135215    sqlite3Fts3Dequote(zValue);
135216  }
135217  *pzValue = zValue;
135218  return 1;
135219}
135220
135221/*
135222** Append the output of a printf() style formatting to an existing string.
135223*/
135224static void fts3Appendf(
135225  int *pRc,                       /* IN/OUT: Error code */
135226  char **pz,                      /* IN/OUT: Pointer to string buffer */
135227  const char *zFormat,            /* Printf format string to append */
135228  ...                             /* Arguments for printf format string */
135229){
135230  if( *pRc==SQLITE_OK ){
135231    va_list ap;
135232    char *z;
135233    va_start(ap, zFormat);
135234    z = sqlite3_vmprintf(zFormat, ap);
135235    va_end(ap);
135236    if( z && *pz ){
135237      char *z2 = sqlite3_mprintf("%s%s", *pz, z);
135238      sqlite3_free(z);
135239      z = z2;
135240    }
135241    if( z==0 ) *pRc = SQLITE_NOMEM;
135242    sqlite3_free(*pz);
135243    *pz = z;
135244  }
135245}
135246
135247/*
135248** Return a copy of input string zInput enclosed in double-quotes (") and
135249** with all double quote characters escaped. For example:
135250**
135251**     fts3QuoteId("un \"zip\"")   ->    "un \"\"zip\"\""
135252**
135253** The pointer returned points to memory obtained from sqlite3_malloc(). It
135254** is the callers responsibility to call sqlite3_free() to release this
135255** memory.
135256*/
135257static char *fts3QuoteId(char const *zInput){
135258  int nRet;
135259  char *zRet;
135260  nRet = 2 + (int)strlen(zInput)*2 + 1;
135261  zRet = sqlite3_malloc(nRet);
135262  if( zRet ){
135263    int i;
135264    char *z = zRet;
135265    *(z++) = '"';
135266    for(i=0; zInput[i]; i++){
135267      if( zInput[i]=='"' ) *(z++) = '"';
135268      *(z++) = zInput[i];
135269    }
135270    *(z++) = '"';
135271    *(z++) = '\0';
135272  }
135273  return zRet;
135274}
135275
135276/*
135277** Return a list of comma separated SQL expressions and a FROM clause that
135278** could be used in a SELECT statement such as the following:
135279**
135280**     SELECT <list of expressions> FROM %_content AS x ...
135281**
135282** to return the docid, followed by each column of text data in order
135283** from left to write. If parameter zFunc is not NULL, then instead of
135284** being returned directly each column of text data is passed to an SQL
135285** function named zFunc first. For example, if zFunc is "unzip" and the
135286** table has the three user-defined columns "a", "b", and "c", the following
135287** string is returned:
135288**
135289**     "docid, unzip(x.'a'), unzip(x.'b'), unzip(x.'c') FROM %_content AS x"
135290**
135291** The pointer returned points to a buffer allocated by sqlite3_malloc(). It
135292** is the responsibility of the caller to eventually free it.
135293**
135294** If *pRc is not SQLITE_OK when this function is called, it is a no-op (and
135295** a NULL pointer is returned). Otherwise, if an OOM error is encountered
135296** by this function, NULL is returned and *pRc is set to SQLITE_NOMEM. If
135297** no error occurs, *pRc is left unmodified.
135298*/
135299static char *fts3ReadExprList(Fts3Table *p, const char *zFunc, int *pRc){
135300  char *zRet = 0;
135301  char *zFree = 0;
135302  char *zFunction;
135303  int i;
135304
135305  if( p->zContentTbl==0 ){
135306    if( !zFunc ){
135307      zFunction = "";
135308    }else{
135309      zFree = zFunction = fts3QuoteId(zFunc);
135310    }
135311    fts3Appendf(pRc, &zRet, "docid");
135312    for(i=0; i<p->nColumn; i++){
135313      fts3Appendf(pRc, &zRet, ",%s(x.'c%d%q')", zFunction, i, p->azColumn[i]);
135314    }
135315    if( p->zLanguageid ){
135316      fts3Appendf(pRc, &zRet, ", x.%Q", "langid");
135317    }
135318    sqlite3_free(zFree);
135319  }else{
135320    fts3Appendf(pRc, &zRet, "rowid");
135321    for(i=0; i<p->nColumn; i++){
135322      fts3Appendf(pRc, &zRet, ", x.'%q'", p->azColumn[i]);
135323    }
135324    if( p->zLanguageid ){
135325      fts3Appendf(pRc, &zRet, ", x.%Q", p->zLanguageid);
135326    }
135327  }
135328  fts3Appendf(pRc, &zRet, " FROM '%q'.'%q%s' AS x",
135329      p->zDb,
135330      (p->zContentTbl ? p->zContentTbl : p->zName),
135331      (p->zContentTbl ? "" : "_content")
135332  );
135333  return zRet;
135334}
135335
135336/*
135337** Return a list of N comma separated question marks, where N is the number
135338** of columns in the %_content table (one for the docid plus one for each
135339** user-defined text column).
135340**
135341** If argument zFunc is not NULL, then all but the first question mark
135342** is preceded by zFunc and an open bracket, and followed by a closed
135343** bracket. For example, if zFunc is "zip" and the FTS3 table has three
135344** user-defined text columns, the following string is returned:
135345**
135346**     "?, zip(?), zip(?), zip(?)"
135347**
135348** The pointer returned points to a buffer allocated by sqlite3_malloc(). It
135349** is the responsibility of the caller to eventually free it.
135350**
135351** If *pRc is not SQLITE_OK when this function is called, it is a no-op (and
135352** a NULL pointer is returned). Otherwise, if an OOM error is encountered
135353** by this function, NULL is returned and *pRc is set to SQLITE_NOMEM. If
135354** no error occurs, *pRc is left unmodified.
135355*/
135356static char *fts3WriteExprList(Fts3Table *p, const char *zFunc, int *pRc){
135357  char *zRet = 0;
135358  char *zFree = 0;
135359  char *zFunction;
135360  int i;
135361
135362  if( !zFunc ){
135363    zFunction = "";
135364  }else{
135365    zFree = zFunction = fts3QuoteId(zFunc);
135366  }
135367  fts3Appendf(pRc, &zRet, "?");
135368  for(i=0; i<p->nColumn; i++){
135369    fts3Appendf(pRc, &zRet, ",%s(?)", zFunction);
135370  }
135371  if( p->zLanguageid ){
135372    fts3Appendf(pRc, &zRet, ", ?");
135373  }
135374  sqlite3_free(zFree);
135375  return zRet;
135376}
135377
135378/*
135379** This function interprets the string at (*pp) as a non-negative integer
135380** value. It reads the integer and sets *pnOut to the value read, then
135381** sets *pp to point to the byte immediately following the last byte of
135382** the integer value.
135383**
135384** Only decimal digits ('0'..'9') may be part of an integer value.
135385**
135386** If *pp does not being with a decimal digit SQLITE_ERROR is returned and
135387** the output value undefined. Otherwise SQLITE_OK is returned.
135388**
135389** This function is used when parsing the "prefix=" FTS4 parameter.
135390*/
135391static int fts3GobbleInt(const char **pp, int *pnOut){
135392  const int MAX_NPREFIX = 10000000;
135393  const char *p;                  /* Iterator pointer */
135394  int nInt = 0;                   /* Output value */
135395
135396  for(p=*pp; p[0]>='0' && p[0]<='9'; p++){
135397    nInt = nInt * 10 + (p[0] - '0');
135398    if( nInt>MAX_NPREFIX ){
135399      nInt = 0;
135400      break;
135401    }
135402  }
135403  if( p==*pp ) return SQLITE_ERROR;
135404  *pnOut = nInt;
135405  *pp = p;
135406  return SQLITE_OK;
135407}
135408
135409/*
135410** This function is called to allocate an array of Fts3Index structures
135411** representing the indexes maintained by the current FTS table. FTS tables
135412** always maintain the main "terms" index, but may also maintain one or
135413** more "prefix" indexes, depending on the value of the "prefix=" parameter
135414** (if any) specified as part of the CREATE VIRTUAL TABLE statement.
135415**
135416** Argument zParam is passed the value of the "prefix=" option if one was
135417** specified, or NULL otherwise.
135418**
135419** If no error occurs, SQLITE_OK is returned and *apIndex set to point to
135420** the allocated array. *pnIndex is set to the number of elements in the
135421** array. If an error does occur, an SQLite error code is returned.
135422**
135423** Regardless of whether or not an error is returned, it is the responsibility
135424** of the caller to call sqlite3_free() on the output array to free it.
135425*/
135426static int fts3PrefixParameter(
135427  const char *zParam,             /* ABC in prefix=ABC parameter to parse */
135428  int *pnIndex,                   /* OUT: size of *apIndex[] array */
135429  struct Fts3Index **apIndex      /* OUT: Array of indexes for this table */
135430){
135431  struct Fts3Index *aIndex;       /* Allocated array */
135432  int nIndex = 1;                 /* Number of entries in array */
135433
135434  if( zParam && zParam[0] ){
135435    const char *p;
135436    nIndex++;
135437    for(p=zParam; *p; p++){
135438      if( *p==',' ) nIndex++;
135439    }
135440  }
135441
135442  aIndex = sqlite3_malloc(sizeof(struct Fts3Index) * nIndex);
135443  *apIndex = aIndex;
135444  if( !aIndex ){
135445    return SQLITE_NOMEM;
135446  }
135447
135448  memset(aIndex, 0, sizeof(struct Fts3Index) * nIndex);
135449  if( zParam ){
135450    const char *p = zParam;
135451    int i;
135452    for(i=1; i<nIndex; i++){
135453      int nPrefix = 0;
135454      if( fts3GobbleInt(&p, &nPrefix) ) return SQLITE_ERROR;
135455      assert( nPrefix>=0 );
135456      if( nPrefix==0 ){
135457        nIndex--;
135458        i--;
135459      }else{
135460        aIndex[i].nPrefix = nPrefix;
135461      }
135462      p++;
135463    }
135464  }
135465
135466  *pnIndex = nIndex;
135467  return SQLITE_OK;
135468}
135469
135470/*
135471** This function is called when initializing an FTS4 table that uses the
135472** content=xxx option. It determines the number of and names of the columns
135473** of the new FTS4 table.
135474**
135475** The third argument passed to this function is the value passed to the
135476** config=xxx option (i.e. "xxx"). This function queries the database for
135477** a table of that name. If found, the output variables are populated
135478** as follows:
135479**
135480**   *pnCol:   Set to the number of columns table xxx has,
135481**
135482**   *pnStr:   Set to the total amount of space required to store a copy
135483**             of each columns name, including the nul-terminator.
135484**
135485**   *pazCol:  Set to point to an array of *pnCol strings. Each string is
135486**             the name of the corresponding column in table xxx. The array
135487**             and its contents are allocated using a single allocation. It
135488**             is the responsibility of the caller to free this allocation
135489**             by eventually passing the *pazCol value to sqlite3_free().
135490**
135491** If the table cannot be found, an error code is returned and the output
135492** variables are undefined. Or, if an OOM is encountered, SQLITE_NOMEM is
135493** returned (and the output variables are undefined).
135494*/
135495static int fts3ContentColumns(
135496  sqlite3 *db,                    /* Database handle */
135497  const char *zDb,                /* Name of db (i.e. "main", "temp" etc.) */
135498  const char *zTbl,               /* Name of content table */
135499  const char ***pazCol,           /* OUT: Malloc'd array of column names */
135500  int *pnCol,                     /* OUT: Size of array *pazCol */
135501  int *pnStr,                     /* OUT: Bytes of string content */
135502  char **pzErr                    /* OUT: error message */
135503){
135504  int rc = SQLITE_OK;             /* Return code */
135505  char *zSql;                     /* "SELECT *" statement on zTbl */
135506  sqlite3_stmt *pStmt = 0;        /* Compiled version of zSql */
135507
135508  zSql = sqlite3_mprintf("SELECT * FROM %Q.%Q", zDb, zTbl);
135509  if( !zSql ){
135510    rc = SQLITE_NOMEM;
135511  }else{
135512    rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0);
135513    if( rc!=SQLITE_OK ){
135514      sqlite3Fts3ErrMsg(pzErr, "%s", sqlite3_errmsg(db));
135515    }
135516  }
135517  sqlite3_free(zSql);
135518
135519  if( rc==SQLITE_OK ){
135520    const char **azCol;           /* Output array */
135521    int nStr = 0;                 /* Size of all column names (incl. 0x00) */
135522    int nCol;                     /* Number of table columns */
135523    int i;                        /* Used to iterate through columns */
135524
135525    /* Loop through the returned columns. Set nStr to the number of bytes of
135526    ** space required to store a copy of each column name, including the
135527    ** nul-terminator byte.  */
135528    nCol = sqlite3_column_count(pStmt);
135529    for(i=0; i<nCol; i++){
135530      const char *zCol = sqlite3_column_name(pStmt, i);
135531      nStr += (int)strlen(zCol) + 1;
135532    }
135533
135534    /* Allocate and populate the array to return. */
135535    azCol = (const char **)sqlite3_malloc(sizeof(char *) * nCol + nStr);
135536    if( azCol==0 ){
135537      rc = SQLITE_NOMEM;
135538    }else{
135539      char *p = (char *)&azCol[nCol];
135540      for(i=0; i<nCol; i++){
135541        const char *zCol = sqlite3_column_name(pStmt, i);
135542        int n = (int)strlen(zCol)+1;
135543        memcpy(p, zCol, n);
135544        azCol[i] = p;
135545        p += n;
135546      }
135547    }
135548    sqlite3_finalize(pStmt);
135549
135550    /* Set the output variables. */
135551    *pnCol = nCol;
135552    *pnStr = nStr;
135553    *pazCol = azCol;
135554  }
135555
135556  return rc;
135557}
135558
135559/*
135560** This function is the implementation of both the xConnect and xCreate
135561** methods of the FTS3 virtual table.
135562**
135563** The argv[] array contains the following:
135564**
135565**   argv[0]   -> module name  ("fts3" or "fts4")
135566**   argv[1]   -> database name
135567**   argv[2]   -> table name
135568**   argv[...] -> "column name" and other module argument fields.
135569*/
135570static int fts3InitVtab(
135571  int isCreate,                   /* True for xCreate, false for xConnect */
135572  sqlite3 *db,                    /* The SQLite database connection */
135573  void *pAux,                     /* Hash table containing tokenizers */
135574  int argc,                       /* Number of elements in argv array */
135575  const char * const *argv,       /* xCreate/xConnect argument array */
135576  sqlite3_vtab **ppVTab,          /* Write the resulting vtab structure here */
135577  char **pzErr                    /* Write any error message here */
135578){
135579  Fts3Hash *pHash = (Fts3Hash *)pAux;
135580  Fts3Table *p = 0;               /* Pointer to allocated vtab */
135581  int rc = SQLITE_OK;             /* Return code */
135582  int i;                          /* Iterator variable */
135583  int nByte;                      /* Size of allocation used for *p */
135584  int iCol;                       /* Column index */
135585  int nString = 0;                /* Bytes required to hold all column names */
135586  int nCol = 0;                   /* Number of columns in the FTS table */
135587  char *zCsr;                     /* Space for holding column names */
135588  int nDb;                        /* Bytes required to hold database name */
135589  int nName;                      /* Bytes required to hold table name */
135590  int isFts4 = (argv[0][3]=='4'); /* True for FTS4, false for FTS3 */
135591  const char **aCol;              /* Array of column names */
135592  sqlite3_tokenizer *pTokenizer = 0;        /* Tokenizer for this table */
135593
135594  int nIndex = 0;                 /* Size of aIndex[] array */
135595  struct Fts3Index *aIndex = 0;   /* Array of indexes for this table */
135596
135597  /* The results of parsing supported FTS4 key=value options: */
135598  int bNoDocsize = 0;             /* True to omit %_docsize table */
135599  int bDescIdx = 0;               /* True to store descending indexes */
135600  char *zPrefix = 0;              /* Prefix parameter value (or NULL) */
135601  char *zCompress = 0;            /* compress=? parameter (or NULL) */
135602  char *zUncompress = 0;          /* uncompress=? parameter (or NULL) */
135603  char *zContent = 0;             /* content=? parameter (or NULL) */
135604  char *zLanguageid = 0;          /* languageid=? parameter (or NULL) */
135605  char **azNotindexed = 0;        /* The set of notindexed= columns */
135606  int nNotindexed = 0;            /* Size of azNotindexed[] array */
135607
135608  assert( strlen(argv[0])==4 );
135609  assert( (sqlite3_strnicmp(argv[0], "fts4", 4)==0 && isFts4)
135610       || (sqlite3_strnicmp(argv[0], "fts3", 4)==0 && !isFts4)
135611  );
135612
135613  nDb = (int)strlen(argv[1]) + 1;
135614  nName = (int)strlen(argv[2]) + 1;
135615
135616  nByte = sizeof(const char *) * (argc-2);
135617  aCol = (const char **)sqlite3_malloc(nByte);
135618  if( aCol ){
135619    memset((void*)aCol, 0, nByte);
135620    azNotindexed = (char **)sqlite3_malloc(nByte);
135621  }
135622  if( azNotindexed ){
135623    memset(azNotindexed, 0, nByte);
135624  }
135625  if( !aCol || !azNotindexed ){
135626    rc = SQLITE_NOMEM;
135627    goto fts3_init_out;
135628  }
135629
135630  /* Loop through all of the arguments passed by the user to the FTS3/4
135631  ** module (i.e. all the column names and special arguments). This loop
135632  ** does the following:
135633  **
135634  **   + Figures out the number of columns the FTSX table will have, and
135635  **     the number of bytes of space that must be allocated to store copies
135636  **     of the column names.
135637  **
135638  **   + If there is a tokenizer specification included in the arguments,
135639  **     initializes the tokenizer pTokenizer.
135640  */
135641  for(i=3; rc==SQLITE_OK && i<argc; i++){
135642    char const *z = argv[i];
135643    int nKey;
135644    char *zVal;
135645
135646    /* Check if this is a tokenizer specification */
135647    if( !pTokenizer
135648     && strlen(z)>8
135649     && 0==sqlite3_strnicmp(z, "tokenize", 8)
135650     && 0==sqlite3Fts3IsIdChar(z[8])
135651    ){
135652      rc = sqlite3Fts3InitTokenizer(pHash, &z[9], &pTokenizer, pzErr);
135653    }
135654
135655    /* Check if it is an FTS4 special argument. */
135656    else if( isFts4 && fts3IsSpecialColumn(z, &nKey, &zVal) ){
135657      struct Fts4Option {
135658        const char *zOpt;
135659        int nOpt;
135660      } aFts4Opt[] = {
135661        { "matchinfo",   9 },     /* 0 -> MATCHINFO */
135662        { "prefix",      6 },     /* 1 -> PREFIX */
135663        { "compress",    8 },     /* 2 -> COMPRESS */
135664        { "uncompress", 10 },     /* 3 -> UNCOMPRESS */
135665        { "order",       5 },     /* 4 -> ORDER */
135666        { "content",     7 },     /* 5 -> CONTENT */
135667        { "languageid", 10 },     /* 6 -> LANGUAGEID */
135668        { "notindexed", 10 }      /* 7 -> NOTINDEXED */
135669      };
135670
135671      int iOpt;
135672      if( !zVal ){
135673        rc = SQLITE_NOMEM;
135674      }else{
135675        for(iOpt=0; iOpt<SizeofArray(aFts4Opt); iOpt++){
135676          struct Fts4Option *pOp = &aFts4Opt[iOpt];
135677          if( nKey==pOp->nOpt && !sqlite3_strnicmp(z, pOp->zOpt, pOp->nOpt) ){
135678            break;
135679          }
135680        }
135681        if( iOpt==SizeofArray(aFts4Opt) ){
135682          sqlite3Fts3ErrMsg(pzErr, "unrecognized parameter: %s", z);
135683          rc = SQLITE_ERROR;
135684        }else{
135685          switch( iOpt ){
135686            case 0:               /* MATCHINFO */
135687              if( strlen(zVal)!=4 || sqlite3_strnicmp(zVal, "fts3", 4) ){
135688                sqlite3Fts3ErrMsg(pzErr, "unrecognized matchinfo: %s", zVal);
135689                rc = SQLITE_ERROR;
135690              }
135691              bNoDocsize = 1;
135692              break;
135693
135694            case 1:               /* PREFIX */
135695              sqlite3_free(zPrefix);
135696              zPrefix = zVal;
135697              zVal = 0;
135698              break;
135699
135700            case 2:               /* COMPRESS */
135701              sqlite3_free(zCompress);
135702              zCompress = zVal;
135703              zVal = 0;
135704              break;
135705
135706            case 3:               /* UNCOMPRESS */
135707              sqlite3_free(zUncompress);
135708              zUncompress = zVal;
135709              zVal = 0;
135710              break;
135711
135712            case 4:               /* ORDER */
135713              if( (strlen(zVal)!=3 || sqlite3_strnicmp(zVal, "asc", 3))
135714               && (strlen(zVal)!=4 || sqlite3_strnicmp(zVal, "desc", 4))
135715              ){
135716                sqlite3Fts3ErrMsg(pzErr, "unrecognized order: %s", zVal);
135717                rc = SQLITE_ERROR;
135718              }
135719              bDescIdx = (zVal[0]=='d' || zVal[0]=='D');
135720              break;
135721
135722            case 5:              /* CONTENT */
135723              sqlite3_free(zContent);
135724              zContent = zVal;
135725              zVal = 0;
135726              break;
135727
135728            case 6:              /* LANGUAGEID */
135729              assert( iOpt==6 );
135730              sqlite3_free(zLanguageid);
135731              zLanguageid = zVal;
135732              zVal = 0;
135733              break;
135734
135735            case 7:              /* NOTINDEXED */
135736              azNotindexed[nNotindexed++] = zVal;
135737              zVal = 0;
135738              break;
135739          }
135740        }
135741        sqlite3_free(zVal);
135742      }
135743    }
135744
135745    /* Otherwise, the argument is a column name. */
135746    else {
135747      nString += (int)(strlen(z) + 1);
135748      aCol[nCol++] = z;
135749    }
135750  }
135751
135752  /* If a content=xxx option was specified, the following:
135753  **
135754  **   1. Ignore any compress= and uncompress= options.
135755  **
135756  **   2. If no column names were specified as part of the CREATE VIRTUAL
135757  **      TABLE statement, use all columns from the content table.
135758  */
135759  if( rc==SQLITE_OK && zContent ){
135760    sqlite3_free(zCompress);
135761    sqlite3_free(zUncompress);
135762    zCompress = 0;
135763    zUncompress = 0;
135764    if( nCol==0 ){
135765      sqlite3_free((void*)aCol);
135766      aCol = 0;
135767      rc = fts3ContentColumns(db, argv[1], zContent,&aCol,&nCol,&nString,pzErr);
135768
135769      /* If a languageid= option was specified, remove the language id
135770      ** column from the aCol[] array. */
135771      if( rc==SQLITE_OK && zLanguageid ){
135772        int j;
135773        for(j=0; j<nCol; j++){
135774          if( sqlite3_stricmp(zLanguageid, aCol[j])==0 ){
135775            int k;
135776            for(k=j; k<nCol; k++) aCol[k] = aCol[k+1];
135777            nCol--;
135778            break;
135779          }
135780        }
135781      }
135782    }
135783  }
135784  if( rc!=SQLITE_OK ) goto fts3_init_out;
135785
135786  if( nCol==0 ){
135787    assert( nString==0 );
135788    aCol[0] = "content";
135789    nString = 8;
135790    nCol = 1;
135791  }
135792
135793  if( pTokenizer==0 ){
135794    rc = sqlite3Fts3InitTokenizer(pHash, "simple", &pTokenizer, pzErr);
135795    if( rc!=SQLITE_OK ) goto fts3_init_out;
135796  }
135797  assert( pTokenizer );
135798
135799  rc = fts3PrefixParameter(zPrefix, &nIndex, &aIndex);
135800  if( rc==SQLITE_ERROR ){
135801    assert( zPrefix );
135802    sqlite3Fts3ErrMsg(pzErr, "error parsing prefix parameter: %s", zPrefix);
135803  }
135804  if( rc!=SQLITE_OK ) goto fts3_init_out;
135805
135806  /* Allocate and populate the Fts3Table structure. */
135807  nByte = sizeof(Fts3Table) +                  /* Fts3Table */
135808          nCol * sizeof(char *) +              /* azColumn */
135809          nIndex * sizeof(struct Fts3Index) +  /* aIndex */
135810          nCol * sizeof(u8) +                  /* abNotindexed */
135811          nName +                              /* zName */
135812          nDb +                                /* zDb */
135813          nString;                             /* Space for azColumn strings */
135814  p = (Fts3Table*)sqlite3_malloc(nByte);
135815  if( p==0 ){
135816    rc = SQLITE_NOMEM;
135817    goto fts3_init_out;
135818  }
135819  memset(p, 0, nByte);
135820  p->db = db;
135821  p->nColumn = nCol;
135822  p->nPendingData = 0;
135823  p->azColumn = (char **)&p[1];
135824  p->pTokenizer = pTokenizer;
135825  p->nMaxPendingData = FTS3_MAX_PENDING_DATA;
135826  p->bHasDocsize = (isFts4 && bNoDocsize==0);
135827  p->bHasStat = isFts4;
135828  p->bFts4 = isFts4;
135829  p->bDescIdx = bDescIdx;
135830  p->nAutoincrmerge = 0xff;   /* 0xff means setting unknown */
135831  p->zContentTbl = zContent;
135832  p->zLanguageid = zLanguageid;
135833  zContent = 0;
135834  zLanguageid = 0;
135835  TESTONLY( p->inTransaction = -1 );
135836  TESTONLY( p->mxSavepoint = -1 );
135837
135838  p->aIndex = (struct Fts3Index *)&p->azColumn[nCol];
135839  memcpy(p->aIndex, aIndex, sizeof(struct Fts3Index) * nIndex);
135840  p->nIndex = nIndex;
135841  for(i=0; i<nIndex; i++){
135842    fts3HashInit(&p->aIndex[i].hPending, FTS3_HASH_STRING, 1);
135843  }
135844  p->abNotindexed = (u8 *)&p->aIndex[nIndex];
135845
135846  /* Fill in the zName and zDb fields of the vtab structure. */
135847  zCsr = (char *)&p->abNotindexed[nCol];
135848  p->zName = zCsr;
135849  memcpy(zCsr, argv[2], nName);
135850  zCsr += nName;
135851  p->zDb = zCsr;
135852  memcpy(zCsr, argv[1], nDb);
135853  zCsr += nDb;
135854
135855  /* Fill in the azColumn array */
135856  for(iCol=0; iCol<nCol; iCol++){
135857    char *z;
135858    int n = 0;
135859    z = (char *)sqlite3Fts3NextToken(aCol[iCol], &n);
135860    memcpy(zCsr, z, n);
135861    zCsr[n] = '\0';
135862    sqlite3Fts3Dequote(zCsr);
135863    p->azColumn[iCol] = zCsr;
135864    zCsr += n+1;
135865    assert( zCsr <= &((char *)p)[nByte] );
135866  }
135867
135868  /* Fill in the abNotindexed array */
135869  for(iCol=0; iCol<nCol; iCol++){
135870    int n = (int)strlen(p->azColumn[iCol]);
135871    for(i=0; i<nNotindexed; i++){
135872      char *zNot = azNotindexed[i];
135873      if( zNot && n==(int)strlen(zNot)
135874       && 0==sqlite3_strnicmp(p->azColumn[iCol], zNot, n)
135875      ){
135876        p->abNotindexed[iCol] = 1;
135877        sqlite3_free(zNot);
135878        azNotindexed[i] = 0;
135879      }
135880    }
135881  }
135882  for(i=0; i<nNotindexed; i++){
135883    if( azNotindexed[i] ){
135884      sqlite3Fts3ErrMsg(pzErr, "no such column: %s", azNotindexed[i]);
135885      rc = SQLITE_ERROR;
135886    }
135887  }
135888
135889  if( rc==SQLITE_OK && (zCompress==0)!=(zUncompress==0) ){
135890    char const *zMiss = (zCompress==0 ? "compress" : "uncompress");
135891    rc = SQLITE_ERROR;
135892    sqlite3Fts3ErrMsg(pzErr, "missing %s parameter in fts4 constructor", zMiss);
135893  }
135894  p->zReadExprlist = fts3ReadExprList(p, zUncompress, &rc);
135895  p->zWriteExprlist = fts3WriteExprList(p, zCompress, &rc);
135896  if( rc!=SQLITE_OK ) goto fts3_init_out;
135897
135898  /* If this is an xCreate call, create the underlying tables in the
135899  ** database. TODO: For xConnect(), it could verify that said tables exist.
135900  */
135901  if( isCreate ){
135902    rc = fts3CreateTables(p);
135903  }
135904
135905  /* Check to see if a legacy fts3 table has been "upgraded" by the
135906  ** addition of a %_stat table so that it can use incremental merge.
135907  */
135908  if( !isFts4 && !isCreate ){
135909    p->bHasStat = 2;
135910  }
135911
135912  /* Figure out the page-size for the database. This is required in order to
135913  ** estimate the cost of loading large doclists from the database.  */
135914  fts3DatabasePageSize(&rc, p);
135915  p->nNodeSize = p->nPgsz-35;
135916
135917  /* Declare the table schema to SQLite. */
135918  fts3DeclareVtab(&rc, p);
135919
135920fts3_init_out:
135921  sqlite3_free(zPrefix);
135922  sqlite3_free(aIndex);
135923  sqlite3_free(zCompress);
135924  sqlite3_free(zUncompress);
135925  sqlite3_free(zContent);
135926  sqlite3_free(zLanguageid);
135927  for(i=0; i<nNotindexed; i++) sqlite3_free(azNotindexed[i]);
135928  sqlite3_free((void *)aCol);
135929  sqlite3_free((void *)azNotindexed);
135930  if( rc!=SQLITE_OK ){
135931    if( p ){
135932      fts3DisconnectMethod((sqlite3_vtab *)p);
135933    }else if( pTokenizer ){
135934      pTokenizer->pModule->xDestroy(pTokenizer);
135935    }
135936  }else{
135937    assert( p->pSegments==0 );
135938    *ppVTab = &p->base;
135939  }
135940  return rc;
135941}
135942
135943/*
135944** The xConnect() and xCreate() methods for the virtual table. All the
135945** work is done in function fts3InitVtab().
135946*/
135947static int fts3ConnectMethod(
135948  sqlite3 *db,                    /* Database connection */
135949  void *pAux,                     /* Pointer to tokenizer hash table */
135950  int argc,                       /* Number of elements in argv array */
135951  const char * const *argv,       /* xCreate/xConnect argument array */
135952  sqlite3_vtab **ppVtab,          /* OUT: New sqlite3_vtab object */
135953  char **pzErr                    /* OUT: sqlite3_malloc'd error message */
135954){
135955  return fts3InitVtab(0, db, pAux, argc, argv, ppVtab, pzErr);
135956}
135957static int fts3CreateMethod(
135958  sqlite3 *db,                    /* Database connection */
135959  void *pAux,                     /* Pointer to tokenizer hash table */
135960  int argc,                       /* Number of elements in argv array */
135961  const char * const *argv,       /* xCreate/xConnect argument array */
135962  sqlite3_vtab **ppVtab,          /* OUT: New sqlite3_vtab object */
135963  char **pzErr                    /* OUT: sqlite3_malloc'd error message */
135964){
135965  return fts3InitVtab(1, db, pAux, argc, argv, ppVtab, pzErr);
135966}
135967
135968/*
135969** Set the pIdxInfo->estimatedRows variable to nRow. Unless this
135970** extension is currently being used by a version of SQLite too old to
135971** support estimatedRows. In that case this function is a no-op.
135972*/
135973static void fts3SetEstimatedRows(sqlite3_index_info *pIdxInfo, i64 nRow){
135974#if SQLITE_VERSION_NUMBER>=3008002
135975  if( sqlite3_libversion_number()>=3008002 ){
135976    pIdxInfo->estimatedRows = nRow;
135977  }
135978#endif
135979}
135980
135981/*
135982** Implementation of the xBestIndex method for FTS3 tables. There
135983** are three possible strategies, in order of preference:
135984**
135985**   1. Direct lookup by rowid or docid.
135986**   2. Full-text search using a MATCH operator on a non-docid column.
135987**   3. Linear scan of %_content table.
135988*/
135989static int fts3BestIndexMethod(sqlite3_vtab *pVTab, sqlite3_index_info *pInfo){
135990  Fts3Table *p = (Fts3Table *)pVTab;
135991  int i;                          /* Iterator variable */
135992  int iCons = -1;                 /* Index of constraint to use */
135993
135994  int iLangidCons = -1;           /* Index of langid=x constraint, if present */
135995  int iDocidGe = -1;              /* Index of docid>=x constraint, if present */
135996  int iDocidLe = -1;              /* Index of docid<=x constraint, if present */
135997  int iIdx;
135998
135999  /* By default use a full table scan. This is an expensive option,
136000  ** so search through the constraints to see if a more efficient
136001  ** strategy is possible.
136002  */
136003  pInfo->idxNum = FTS3_FULLSCAN_SEARCH;
136004  pInfo->estimatedCost = 5000000;
136005  for(i=0; i<pInfo->nConstraint; i++){
136006    int bDocid;                 /* True if this constraint is on docid */
136007    struct sqlite3_index_constraint *pCons = &pInfo->aConstraint[i];
136008    if( pCons->usable==0 ){
136009      if( pCons->op==SQLITE_INDEX_CONSTRAINT_MATCH ){
136010        /* There exists an unusable MATCH constraint. This means that if
136011        ** the planner does elect to use the results of this call as part
136012        ** of the overall query plan the user will see an "unable to use
136013        ** function MATCH in the requested context" error. To discourage
136014        ** this, return a very high cost here.  */
136015        pInfo->idxNum = FTS3_FULLSCAN_SEARCH;
136016        pInfo->estimatedCost = 1e50;
136017        fts3SetEstimatedRows(pInfo, ((sqlite3_int64)1) << 50);
136018        return SQLITE_OK;
136019      }
136020      continue;
136021    }
136022
136023    bDocid = (pCons->iColumn<0 || pCons->iColumn==p->nColumn+1);
136024
136025    /* A direct lookup on the rowid or docid column. Assign a cost of 1.0. */
136026    if( iCons<0 && pCons->op==SQLITE_INDEX_CONSTRAINT_EQ && bDocid ){
136027      pInfo->idxNum = FTS3_DOCID_SEARCH;
136028      pInfo->estimatedCost = 1.0;
136029      iCons = i;
136030    }
136031
136032    /* A MATCH constraint. Use a full-text search.
136033    **
136034    ** If there is more than one MATCH constraint available, use the first
136035    ** one encountered. If there is both a MATCH constraint and a direct
136036    ** rowid/docid lookup, prefer the MATCH strategy. This is done even
136037    ** though the rowid/docid lookup is faster than a MATCH query, selecting
136038    ** it would lead to an "unable to use function MATCH in the requested
136039    ** context" error.
136040    */
136041    if( pCons->op==SQLITE_INDEX_CONSTRAINT_MATCH
136042     && pCons->iColumn>=0 && pCons->iColumn<=p->nColumn
136043    ){
136044      pInfo->idxNum = FTS3_FULLTEXT_SEARCH + pCons->iColumn;
136045      pInfo->estimatedCost = 2.0;
136046      iCons = i;
136047    }
136048
136049    /* Equality constraint on the langid column */
136050    if( pCons->op==SQLITE_INDEX_CONSTRAINT_EQ
136051     && pCons->iColumn==p->nColumn + 2
136052    ){
136053      iLangidCons = i;
136054    }
136055
136056    if( bDocid ){
136057      switch( pCons->op ){
136058        case SQLITE_INDEX_CONSTRAINT_GE:
136059        case SQLITE_INDEX_CONSTRAINT_GT:
136060          iDocidGe = i;
136061          break;
136062
136063        case SQLITE_INDEX_CONSTRAINT_LE:
136064        case SQLITE_INDEX_CONSTRAINT_LT:
136065          iDocidLe = i;
136066          break;
136067      }
136068    }
136069  }
136070
136071  iIdx = 1;
136072  if( iCons>=0 ){
136073    pInfo->aConstraintUsage[iCons].argvIndex = iIdx++;
136074    pInfo->aConstraintUsage[iCons].omit = 1;
136075  }
136076  if( iLangidCons>=0 ){
136077    pInfo->idxNum |= FTS3_HAVE_LANGID;
136078    pInfo->aConstraintUsage[iLangidCons].argvIndex = iIdx++;
136079  }
136080  if( iDocidGe>=0 ){
136081    pInfo->idxNum |= FTS3_HAVE_DOCID_GE;
136082    pInfo->aConstraintUsage[iDocidGe].argvIndex = iIdx++;
136083  }
136084  if( iDocidLe>=0 ){
136085    pInfo->idxNum |= FTS3_HAVE_DOCID_LE;
136086    pInfo->aConstraintUsage[iDocidLe].argvIndex = iIdx++;
136087  }
136088
136089  /* Regardless of the strategy selected, FTS can deliver rows in rowid (or
136090  ** docid) order. Both ascending and descending are possible.
136091  */
136092  if( pInfo->nOrderBy==1 ){
136093    struct sqlite3_index_orderby *pOrder = &pInfo->aOrderBy[0];
136094    if( pOrder->iColumn<0 || pOrder->iColumn==p->nColumn+1 ){
136095      if( pOrder->desc ){
136096        pInfo->idxStr = "DESC";
136097      }else{
136098        pInfo->idxStr = "ASC";
136099      }
136100      pInfo->orderByConsumed = 1;
136101    }
136102  }
136103
136104  assert( p->pSegments==0 );
136105  return SQLITE_OK;
136106}
136107
136108/*
136109** Implementation of xOpen method.
136110*/
136111static int fts3OpenMethod(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCsr){
136112  sqlite3_vtab_cursor *pCsr;               /* Allocated cursor */
136113
136114  UNUSED_PARAMETER(pVTab);
136115
136116  /* Allocate a buffer large enough for an Fts3Cursor structure. If the
136117  ** allocation succeeds, zero it and return SQLITE_OK. Otherwise,
136118  ** if the allocation fails, return SQLITE_NOMEM.
136119  */
136120  *ppCsr = pCsr = (sqlite3_vtab_cursor *)sqlite3_malloc(sizeof(Fts3Cursor));
136121  if( !pCsr ){
136122    return SQLITE_NOMEM;
136123  }
136124  memset(pCsr, 0, sizeof(Fts3Cursor));
136125  return SQLITE_OK;
136126}
136127
136128/*
136129** Close the cursor.  For additional information see the documentation
136130** on the xClose method of the virtual table interface.
136131*/
136132static int fts3CloseMethod(sqlite3_vtab_cursor *pCursor){
136133  Fts3Cursor *pCsr = (Fts3Cursor *)pCursor;
136134  assert( ((Fts3Table *)pCsr->base.pVtab)->pSegments==0 );
136135  sqlite3_finalize(pCsr->pStmt);
136136  sqlite3Fts3ExprFree(pCsr->pExpr);
136137  sqlite3Fts3FreeDeferredTokens(pCsr);
136138  sqlite3_free(pCsr->aDoclist);
136139  sqlite3Fts3MIBufferFree(pCsr->pMIBuffer);
136140  assert( ((Fts3Table *)pCsr->base.pVtab)->pSegments==0 );
136141  sqlite3_free(pCsr);
136142  return SQLITE_OK;
136143}
136144
136145/*
136146** If pCsr->pStmt has not been prepared (i.e. if pCsr->pStmt==0), then
136147** compose and prepare an SQL statement of the form:
136148**
136149**    "SELECT <columns> FROM %_content WHERE rowid = ?"
136150**
136151** (or the equivalent for a content=xxx table) and set pCsr->pStmt to
136152** it. If an error occurs, return an SQLite error code.
136153**
136154** Otherwise, set *ppStmt to point to pCsr->pStmt and return SQLITE_OK.
136155*/
136156static int fts3CursorSeekStmt(Fts3Cursor *pCsr, sqlite3_stmt **ppStmt){
136157  int rc = SQLITE_OK;
136158  if( pCsr->pStmt==0 ){
136159    Fts3Table *p = (Fts3Table *)pCsr->base.pVtab;
136160    char *zSql;
136161    zSql = sqlite3_mprintf("SELECT %s WHERE rowid = ?", p->zReadExprlist);
136162    if( !zSql ) return SQLITE_NOMEM;
136163    rc = sqlite3_prepare_v2(p->db, zSql, -1, &pCsr->pStmt, 0);
136164    sqlite3_free(zSql);
136165  }
136166  *ppStmt = pCsr->pStmt;
136167  return rc;
136168}
136169
136170/*
136171** Position the pCsr->pStmt statement so that it is on the row
136172** of the %_content table that contains the last match.  Return
136173** SQLITE_OK on success.
136174*/
136175static int fts3CursorSeek(sqlite3_context *pContext, Fts3Cursor *pCsr){
136176  int rc = SQLITE_OK;
136177  if( pCsr->isRequireSeek ){
136178    sqlite3_stmt *pStmt = 0;
136179
136180    rc = fts3CursorSeekStmt(pCsr, &pStmt);
136181    if( rc==SQLITE_OK ){
136182      sqlite3_bind_int64(pCsr->pStmt, 1, pCsr->iPrevId);
136183      pCsr->isRequireSeek = 0;
136184      if( SQLITE_ROW==sqlite3_step(pCsr->pStmt) ){
136185        return SQLITE_OK;
136186      }else{
136187        rc = sqlite3_reset(pCsr->pStmt);
136188        if( rc==SQLITE_OK && ((Fts3Table *)pCsr->base.pVtab)->zContentTbl==0 ){
136189          /* If no row was found and no error has occurred, then the %_content
136190          ** table is missing a row that is present in the full-text index.
136191          ** The data structures are corrupt.  */
136192          rc = FTS_CORRUPT_VTAB;
136193          pCsr->isEof = 1;
136194        }
136195      }
136196    }
136197  }
136198
136199  if( rc!=SQLITE_OK && pContext ){
136200    sqlite3_result_error_code(pContext, rc);
136201  }
136202  return rc;
136203}
136204
136205/*
136206** This function is used to process a single interior node when searching
136207** a b-tree for a term or term prefix. The node data is passed to this
136208** function via the zNode/nNode parameters. The term to search for is
136209** passed in zTerm/nTerm.
136210**
136211** If piFirst is not NULL, then this function sets *piFirst to the blockid
136212** of the child node that heads the sub-tree that may contain the term.
136213**
136214** If piLast is not NULL, then *piLast is set to the right-most child node
136215** that heads a sub-tree that may contain a term for which zTerm/nTerm is
136216** a prefix.
136217**
136218** If an OOM error occurs, SQLITE_NOMEM is returned. Otherwise, SQLITE_OK.
136219*/
136220static int fts3ScanInteriorNode(
136221  const char *zTerm,              /* Term to select leaves for */
136222  int nTerm,                      /* Size of term zTerm in bytes */
136223  const char *zNode,              /* Buffer containing segment interior node */
136224  int nNode,                      /* Size of buffer at zNode */
136225  sqlite3_int64 *piFirst,         /* OUT: Selected child node */
136226  sqlite3_int64 *piLast           /* OUT: Selected child node */
136227){
136228  int rc = SQLITE_OK;             /* Return code */
136229  const char *zCsr = zNode;       /* Cursor to iterate through node */
136230  const char *zEnd = &zCsr[nNode];/* End of interior node buffer */
136231  char *zBuffer = 0;              /* Buffer to load terms into */
136232  int nAlloc = 0;                 /* Size of allocated buffer */
136233  int isFirstTerm = 1;            /* True when processing first term on page */
136234  sqlite3_int64 iChild;           /* Block id of child node to descend to */
136235
136236  /* Skip over the 'height' varint that occurs at the start of every
136237  ** interior node. Then load the blockid of the left-child of the b-tree
136238  ** node into variable iChild.
136239  **
136240  ** Even if the data structure on disk is corrupted, this (reading two
136241  ** varints from the buffer) does not risk an overread. If zNode is a
136242  ** root node, then the buffer comes from a SELECT statement. SQLite does
136243  ** not make this guarantee explicitly, but in practice there are always
136244  ** either more than 20 bytes of allocated space following the nNode bytes of
136245  ** contents, or two zero bytes. Or, if the node is read from the %_segments
136246  ** table, then there are always 20 bytes of zeroed padding following the
136247  ** nNode bytes of content (see sqlite3Fts3ReadBlock() for details).
136248  */
136249  zCsr += sqlite3Fts3GetVarint(zCsr, &iChild);
136250  zCsr += sqlite3Fts3GetVarint(zCsr, &iChild);
136251  if( zCsr>zEnd ){
136252    return FTS_CORRUPT_VTAB;
136253  }
136254
136255  while( zCsr<zEnd && (piFirst || piLast) ){
136256    int cmp;                      /* memcmp() result */
136257    int nSuffix;                  /* Size of term suffix */
136258    int nPrefix = 0;              /* Size of term prefix */
136259    int nBuffer;                  /* Total term size */
136260
136261    /* Load the next term on the node into zBuffer. Use realloc() to expand
136262    ** the size of zBuffer if required.  */
136263    if( !isFirstTerm ){
136264      zCsr += fts3GetVarint32(zCsr, &nPrefix);
136265    }
136266    isFirstTerm = 0;
136267    zCsr += fts3GetVarint32(zCsr, &nSuffix);
136268
136269    if( nPrefix<0 || nSuffix<0 || &zCsr[nSuffix]>zEnd ){
136270      rc = FTS_CORRUPT_VTAB;
136271      goto finish_scan;
136272    }
136273    if( nPrefix+nSuffix>nAlloc ){
136274      char *zNew;
136275      nAlloc = (nPrefix+nSuffix) * 2;
136276      zNew = (char *)sqlite3_realloc(zBuffer, nAlloc);
136277      if( !zNew ){
136278        rc = SQLITE_NOMEM;
136279        goto finish_scan;
136280      }
136281      zBuffer = zNew;
136282    }
136283    assert( zBuffer );
136284    memcpy(&zBuffer[nPrefix], zCsr, nSuffix);
136285    nBuffer = nPrefix + nSuffix;
136286    zCsr += nSuffix;
136287
136288    /* Compare the term we are searching for with the term just loaded from
136289    ** the interior node. If the specified term is greater than or equal
136290    ** to the term from the interior node, then all terms on the sub-tree
136291    ** headed by node iChild are smaller than zTerm. No need to search
136292    ** iChild.
136293    **
136294    ** If the interior node term is larger than the specified term, then
136295    ** the tree headed by iChild may contain the specified term.
136296    */
136297    cmp = memcmp(zTerm, zBuffer, (nBuffer>nTerm ? nTerm : nBuffer));
136298    if( piFirst && (cmp<0 || (cmp==0 && nBuffer>nTerm)) ){
136299      *piFirst = iChild;
136300      piFirst = 0;
136301    }
136302
136303    if( piLast && cmp<0 ){
136304      *piLast = iChild;
136305      piLast = 0;
136306    }
136307
136308    iChild++;
136309  };
136310
136311  if( piFirst ) *piFirst = iChild;
136312  if( piLast ) *piLast = iChild;
136313
136314 finish_scan:
136315  sqlite3_free(zBuffer);
136316  return rc;
136317}
136318
136319
136320/*
136321** The buffer pointed to by argument zNode (size nNode bytes) contains an
136322** interior node of a b-tree segment. The zTerm buffer (size nTerm bytes)
136323** contains a term. This function searches the sub-tree headed by the zNode
136324** node for the range of leaf nodes that may contain the specified term
136325** or terms for which the specified term is a prefix.
136326**
136327** If piLeaf is not NULL, then *piLeaf is set to the blockid of the
136328** left-most leaf node in the tree that may contain the specified term.
136329** If piLeaf2 is not NULL, then *piLeaf2 is set to the blockid of the
136330** right-most leaf node that may contain a term for which the specified
136331** term is a prefix.
136332**
136333** It is possible that the range of returned leaf nodes does not contain
136334** the specified term or any terms for which it is a prefix. However, if the
136335** segment does contain any such terms, they are stored within the identified
136336** range. Because this function only inspects interior segment nodes (and
136337** never loads leaf nodes into memory), it is not possible to be sure.
136338**
136339** If an error occurs, an error code other than SQLITE_OK is returned.
136340*/
136341static int fts3SelectLeaf(
136342  Fts3Table *p,                   /* Virtual table handle */
136343  const char *zTerm,              /* Term to select leaves for */
136344  int nTerm,                      /* Size of term zTerm in bytes */
136345  const char *zNode,              /* Buffer containing segment interior node */
136346  int nNode,                      /* Size of buffer at zNode */
136347  sqlite3_int64 *piLeaf,          /* Selected leaf node */
136348  sqlite3_int64 *piLeaf2          /* Selected leaf node */
136349){
136350  int rc = SQLITE_OK;             /* Return code */
136351  int iHeight;                    /* Height of this node in tree */
136352
136353  assert( piLeaf || piLeaf2 );
136354
136355  fts3GetVarint32(zNode, &iHeight);
136356  rc = fts3ScanInteriorNode(zTerm, nTerm, zNode, nNode, piLeaf, piLeaf2);
136357  assert( !piLeaf2 || !piLeaf || rc!=SQLITE_OK || (*piLeaf<=*piLeaf2) );
136358
136359  if( rc==SQLITE_OK && iHeight>1 ){
136360    char *zBlob = 0;              /* Blob read from %_segments table */
136361    int nBlob = 0;                /* Size of zBlob in bytes */
136362
136363    if( piLeaf && piLeaf2 && (*piLeaf!=*piLeaf2) ){
136364      rc = sqlite3Fts3ReadBlock(p, *piLeaf, &zBlob, &nBlob, 0);
136365      if( rc==SQLITE_OK ){
136366        rc = fts3SelectLeaf(p, zTerm, nTerm, zBlob, nBlob, piLeaf, 0);
136367      }
136368      sqlite3_free(zBlob);
136369      piLeaf = 0;
136370      zBlob = 0;
136371    }
136372
136373    if( rc==SQLITE_OK ){
136374      rc = sqlite3Fts3ReadBlock(p, piLeaf?*piLeaf:*piLeaf2, &zBlob, &nBlob, 0);
136375    }
136376    if( rc==SQLITE_OK ){
136377      rc = fts3SelectLeaf(p, zTerm, nTerm, zBlob, nBlob, piLeaf, piLeaf2);
136378    }
136379    sqlite3_free(zBlob);
136380  }
136381
136382  return rc;
136383}
136384
136385/*
136386** This function is used to create delta-encoded serialized lists of FTS3
136387** varints. Each call to this function appends a single varint to a list.
136388*/
136389static void fts3PutDeltaVarint(
136390  char **pp,                      /* IN/OUT: Output pointer */
136391  sqlite3_int64 *piPrev,          /* IN/OUT: Previous value written to list */
136392  sqlite3_int64 iVal              /* Write this value to the list */
136393){
136394  assert( iVal-*piPrev > 0 || (*piPrev==0 && iVal==0) );
136395  *pp += sqlite3Fts3PutVarint(*pp, iVal-*piPrev);
136396  *piPrev = iVal;
136397}
136398
136399/*
136400** When this function is called, *ppPoslist is assumed to point to the
136401** start of a position-list. After it returns, *ppPoslist points to the
136402** first byte after the position-list.
136403**
136404** A position list is list of positions (delta encoded) and columns for
136405** a single document record of a doclist.  So, in other words, this
136406** routine advances *ppPoslist so that it points to the next docid in
136407** the doclist, or to the first byte past the end of the doclist.
136408**
136409** If pp is not NULL, then the contents of the position list are copied
136410** to *pp. *pp is set to point to the first byte past the last byte copied
136411** before this function returns.
136412*/
136413static void fts3PoslistCopy(char **pp, char **ppPoslist){
136414  char *pEnd = *ppPoslist;
136415  char c = 0;
136416
136417  /* The end of a position list is marked by a zero encoded as an FTS3
136418  ** varint. A single POS_END (0) byte. Except, if the 0 byte is preceded by
136419  ** a byte with the 0x80 bit set, then it is not a varint 0, but the tail
136420  ** of some other, multi-byte, value.
136421  **
136422  ** The following while-loop moves pEnd to point to the first byte that is not
136423  ** immediately preceded by a byte with the 0x80 bit set. Then increments
136424  ** pEnd once more so that it points to the byte immediately following the
136425  ** last byte in the position-list.
136426  */
136427  while( *pEnd | c ){
136428    c = *pEnd++ & 0x80;
136429    testcase( c!=0 && (*pEnd)==0 );
136430  }
136431  pEnd++;  /* Advance past the POS_END terminator byte */
136432
136433  if( pp ){
136434    int n = (int)(pEnd - *ppPoslist);
136435    char *p = *pp;
136436    memcpy(p, *ppPoslist, n);
136437    p += n;
136438    *pp = p;
136439  }
136440  *ppPoslist = pEnd;
136441}
136442
136443/*
136444** When this function is called, *ppPoslist is assumed to point to the
136445** start of a column-list. After it returns, *ppPoslist points to the
136446** to the terminator (POS_COLUMN or POS_END) byte of the column-list.
136447**
136448** A column-list is list of delta-encoded positions for a single column
136449** within a single document within a doclist.
136450**
136451** The column-list is terminated either by a POS_COLUMN varint (1) or
136452** a POS_END varint (0).  This routine leaves *ppPoslist pointing to
136453** the POS_COLUMN or POS_END that terminates the column-list.
136454**
136455** If pp is not NULL, then the contents of the column-list are copied
136456** to *pp. *pp is set to point to the first byte past the last byte copied
136457** before this function returns.  The POS_COLUMN or POS_END terminator
136458** is not copied into *pp.
136459*/
136460static void fts3ColumnlistCopy(char **pp, char **ppPoslist){
136461  char *pEnd = *ppPoslist;
136462  char c = 0;
136463
136464  /* A column-list is terminated by either a 0x01 or 0x00 byte that is
136465  ** not part of a multi-byte varint.
136466  */
136467  while( 0xFE & (*pEnd | c) ){
136468    c = *pEnd++ & 0x80;
136469    testcase( c!=0 && ((*pEnd)&0xfe)==0 );
136470  }
136471  if( pp ){
136472    int n = (int)(pEnd - *ppPoslist);
136473    char *p = *pp;
136474    memcpy(p, *ppPoslist, n);
136475    p += n;
136476    *pp = p;
136477  }
136478  *ppPoslist = pEnd;
136479}
136480
136481/*
136482** Value used to signify the end of an position-list. This is safe because
136483** it is not possible to have a document with 2^31 terms.
136484*/
136485#define POSITION_LIST_END 0x7fffffff
136486
136487/*
136488** This function is used to help parse position-lists. When this function is
136489** called, *pp may point to the start of the next varint in the position-list
136490** being parsed, or it may point to 1 byte past the end of the position-list
136491** (in which case **pp will be a terminator bytes POS_END (0) or
136492** (1)).
136493**
136494** If *pp points past the end of the current position-list, set *pi to
136495** POSITION_LIST_END and return. Otherwise, read the next varint from *pp,
136496** increment the current value of *pi by the value read, and set *pp to
136497** point to the next value before returning.
136498**
136499** Before calling this routine *pi must be initialized to the value of
136500** the previous position, or zero if we are reading the first position
136501** in the position-list.  Because positions are delta-encoded, the value
136502** of the previous position is needed in order to compute the value of
136503** the next position.
136504*/
136505static void fts3ReadNextPos(
136506  char **pp,                    /* IN/OUT: Pointer into position-list buffer */
136507  sqlite3_int64 *pi             /* IN/OUT: Value read from position-list */
136508){
136509  if( (**pp)&0xFE ){
136510    fts3GetDeltaVarint(pp, pi);
136511    *pi -= 2;
136512  }else{
136513    *pi = POSITION_LIST_END;
136514  }
136515}
136516
136517/*
136518** If parameter iCol is not 0, write an POS_COLUMN (1) byte followed by
136519** the value of iCol encoded as a varint to *pp.   This will start a new
136520** column list.
136521**
136522** Set *pp to point to the byte just after the last byte written before
136523** returning (do not modify it if iCol==0). Return the total number of bytes
136524** written (0 if iCol==0).
136525*/
136526static int fts3PutColNumber(char **pp, int iCol){
136527  int n = 0;                      /* Number of bytes written */
136528  if( iCol ){
136529    char *p = *pp;                /* Output pointer */
136530    n = 1 + sqlite3Fts3PutVarint(&p[1], iCol);
136531    *p = 0x01;
136532    *pp = &p[n];
136533  }
136534  return n;
136535}
136536
136537/*
136538** Compute the union of two position lists.  The output written
136539** into *pp contains all positions of both *pp1 and *pp2 in sorted
136540** order and with any duplicates removed.  All pointers are
136541** updated appropriately.   The caller is responsible for insuring
136542** that there is enough space in *pp to hold the complete output.
136543*/
136544static void fts3PoslistMerge(
136545  char **pp,                      /* Output buffer */
136546  char **pp1,                     /* Left input list */
136547  char **pp2                      /* Right input list */
136548){
136549  char *p = *pp;
136550  char *p1 = *pp1;
136551  char *p2 = *pp2;
136552
136553  while( *p1 || *p2 ){
136554    int iCol1;         /* The current column index in pp1 */
136555    int iCol2;         /* The current column index in pp2 */
136556
136557    if( *p1==POS_COLUMN ) fts3GetVarint32(&p1[1], &iCol1);
136558    else if( *p1==POS_END ) iCol1 = POSITION_LIST_END;
136559    else iCol1 = 0;
136560
136561    if( *p2==POS_COLUMN ) fts3GetVarint32(&p2[1], &iCol2);
136562    else if( *p2==POS_END ) iCol2 = POSITION_LIST_END;
136563    else iCol2 = 0;
136564
136565    if( iCol1==iCol2 ){
136566      sqlite3_int64 i1 = 0;       /* Last position from pp1 */
136567      sqlite3_int64 i2 = 0;       /* Last position from pp2 */
136568      sqlite3_int64 iPrev = 0;
136569      int n = fts3PutColNumber(&p, iCol1);
136570      p1 += n;
136571      p2 += n;
136572
136573      /* At this point, both p1 and p2 point to the start of column-lists
136574      ** for the same column (the column with index iCol1 and iCol2).
136575      ** A column-list is a list of non-negative delta-encoded varints, each
136576      ** incremented by 2 before being stored. Each list is terminated by a
136577      ** POS_END (0) or POS_COLUMN (1). The following block merges the two lists
136578      ** and writes the results to buffer p. p is left pointing to the byte
136579      ** after the list written. No terminator (POS_END or POS_COLUMN) is
136580      ** written to the output.
136581      */
136582      fts3GetDeltaVarint(&p1, &i1);
136583      fts3GetDeltaVarint(&p2, &i2);
136584      do {
136585        fts3PutDeltaVarint(&p, &iPrev, (i1<i2) ? i1 : i2);
136586        iPrev -= 2;
136587        if( i1==i2 ){
136588          fts3ReadNextPos(&p1, &i1);
136589          fts3ReadNextPos(&p2, &i2);
136590        }else if( i1<i2 ){
136591          fts3ReadNextPos(&p1, &i1);
136592        }else{
136593          fts3ReadNextPos(&p2, &i2);
136594        }
136595      }while( i1!=POSITION_LIST_END || i2!=POSITION_LIST_END );
136596    }else if( iCol1<iCol2 ){
136597      p1 += fts3PutColNumber(&p, iCol1);
136598      fts3ColumnlistCopy(&p, &p1);
136599    }else{
136600      p2 += fts3PutColNumber(&p, iCol2);
136601      fts3ColumnlistCopy(&p, &p2);
136602    }
136603  }
136604
136605  *p++ = POS_END;
136606  *pp = p;
136607  *pp1 = p1 + 1;
136608  *pp2 = p2 + 1;
136609}
136610
136611/*
136612** This function is used to merge two position lists into one. When it is
136613** called, *pp1 and *pp2 must both point to position lists. A position-list is
136614** the part of a doclist that follows each document id. For example, if a row
136615** contains:
136616**
136617**     'a b c'|'x y z'|'a b b a'
136618**
136619** Then the position list for this row for token 'b' would consist of:
136620**
136621**     0x02 0x01 0x02 0x03 0x03 0x00
136622**
136623** When this function returns, both *pp1 and *pp2 are left pointing to the
136624** byte following the 0x00 terminator of their respective position lists.
136625**
136626** If isSaveLeft is 0, an entry is added to the output position list for
136627** each position in *pp2 for which there exists one or more positions in
136628** *pp1 so that (pos(*pp2)>pos(*pp1) && pos(*pp2)-pos(*pp1)<=nToken). i.e.
136629** when the *pp1 token appears before the *pp2 token, but not more than nToken
136630** slots before it.
136631**
136632** e.g. nToken==1 searches for adjacent positions.
136633*/
136634static int fts3PoslistPhraseMerge(
136635  char **pp,                      /* IN/OUT: Preallocated output buffer */
136636  int nToken,                     /* Maximum difference in token positions */
136637  int isSaveLeft,                 /* Save the left position */
136638  int isExact,                    /* If *pp1 is exactly nTokens before *pp2 */
136639  char **pp1,                     /* IN/OUT: Left input list */
136640  char **pp2                      /* IN/OUT: Right input list */
136641){
136642  char *p = *pp;
136643  char *p1 = *pp1;
136644  char *p2 = *pp2;
136645  int iCol1 = 0;
136646  int iCol2 = 0;
136647
136648  /* Never set both isSaveLeft and isExact for the same invocation. */
136649  assert( isSaveLeft==0 || isExact==0 );
136650
136651  assert( p!=0 && *p1!=0 && *p2!=0 );
136652  if( *p1==POS_COLUMN ){
136653    p1++;
136654    p1 += fts3GetVarint32(p1, &iCol1);
136655  }
136656  if( *p2==POS_COLUMN ){
136657    p2++;
136658    p2 += fts3GetVarint32(p2, &iCol2);
136659  }
136660
136661  while( 1 ){
136662    if( iCol1==iCol2 ){
136663      char *pSave = p;
136664      sqlite3_int64 iPrev = 0;
136665      sqlite3_int64 iPos1 = 0;
136666      sqlite3_int64 iPos2 = 0;
136667
136668      if( iCol1 ){
136669        *p++ = POS_COLUMN;
136670        p += sqlite3Fts3PutVarint(p, iCol1);
136671      }
136672
136673      assert( *p1!=POS_END && *p1!=POS_COLUMN );
136674      assert( *p2!=POS_END && *p2!=POS_COLUMN );
136675      fts3GetDeltaVarint(&p1, &iPos1); iPos1 -= 2;
136676      fts3GetDeltaVarint(&p2, &iPos2); iPos2 -= 2;
136677
136678      while( 1 ){
136679        if( iPos2==iPos1+nToken
136680         || (isExact==0 && iPos2>iPos1 && iPos2<=iPos1+nToken)
136681        ){
136682          sqlite3_int64 iSave;
136683          iSave = isSaveLeft ? iPos1 : iPos2;
136684          fts3PutDeltaVarint(&p, &iPrev, iSave+2); iPrev -= 2;
136685          pSave = 0;
136686          assert( p );
136687        }
136688        if( (!isSaveLeft && iPos2<=(iPos1+nToken)) || iPos2<=iPos1 ){
136689          if( (*p2&0xFE)==0 ) break;
136690          fts3GetDeltaVarint(&p2, &iPos2); iPos2 -= 2;
136691        }else{
136692          if( (*p1&0xFE)==0 ) break;
136693          fts3GetDeltaVarint(&p1, &iPos1); iPos1 -= 2;
136694        }
136695      }
136696
136697      if( pSave ){
136698        assert( pp && p );
136699        p = pSave;
136700      }
136701
136702      fts3ColumnlistCopy(0, &p1);
136703      fts3ColumnlistCopy(0, &p2);
136704      assert( (*p1&0xFE)==0 && (*p2&0xFE)==0 );
136705      if( 0==*p1 || 0==*p2 ) break;
136706
136707      p1++;
136708      p1 += fts3GetVarint32(p1, &iCol1);
136709      p2++;
136710      p2 += fts3GetVarint32(p2, &iCol2);
136711    }
136712
136713    /* Advance pointer p1 or p2 (whichever corresponds to the smaller of
136714    ** iCol1 and iCol2) so that it points to either the 0x00 that marks the
136715    ** end of the position list, or the 0x01 that precedes the next
136716    ** column-number in the position list.
136717    */
136718    else if( iCol1<iCol2 ){
136719      fts3ColumnlistCopy(0, &p1);
136720      if( 0==*p1 ) break;
136721      p1++;
136722      p1 += fts3GetVarint32(p1, &iCol1);
136723    }else{
136724      fts3ColumnlistCopy(0, &p2);
136725      if( 0==*p2 ) break;
136726      p2++;
136727      p2 += fts3GetVarint32(p2, &iCol2);
136728    }
136729  }
136730
136731  fts3PoslistCopy(0, &p2);
136732  fts3PoslistCopy(0, &p1);
136733  *pp1 = p1;
136734  *pp2 = p2;
136735  if( *pp==p ){
136736    return 0;
136737  }
136738  *p++ = 0x00;
136739  *pp = p;
136740  return 1;
136741}
136742
136743/*
136744** Merge two position-lists as required by the NEAR operator. The argument
136745** position lists correspond to the left and right phrases of an expression
136746** like:
136747**
136748**     "phrase 1" NEAR "phrase number 2"
136749**
136750** Position list *pp1 corresponds to the left-hand side of the NEAR
136751** expression and *pp2 to the right. As usual, the indexes in the position
136752** lists are the offsets of the last token in each phrase (tokens "1" and "2"
136753** in the example above).
136754**
136755** The output position list - written to *pp - is a copy of *pp2 with those
136756** entries that are not sufficiently NEAR entries in *pp1 removed.
136757*/
136758static int fts3PoslistNearMerge(
136759  char **pp,                      /* Output buffer */
136760  char *aTmp,                     /* Temporary buffer space */
136761  int nRight,                     /* Maximum difference in token positions */
136762  int nLeft,                      /* Maximum difference in token positions */
136763  char **pp1,                     /* IN/OUT: Left input list */
136764  char **pp2                      /* IN/OUT: Right input list */
136765){
136766  char *p1 = *pp1;
136767  char *p2 = *pp2;
136768
136769  char *pTmp1 = aTmp;
136770  char *pTmp2;
136771  char *aTmp2;
136772  int res = 1;
136773
136774  fts3PoslistPhraseMerge(&pTmp1, nRight, 0, 0, pp1, pp2);
136775  aTmp2 = pTmp2 = pTmp1;
136776  *pp1 = p1;
136777  *pp2 = p2;
136778  fts3PoslistPhraseMerge(&pTmp2, nLeft, 1, 0, pp2, pp1);
136779  if( pTmp1!=aTmp && pTmp2!=aTmp2 ){
136780    fts3PoslistMerge(pp, &aTmp, &aTmp2);
136781  }else if( pTmp1!=aTmp ){
136782    fts3PoslistCopy(pp, &aTmp);
136783  }else if( pTmp2!=aTmp2 ){
136784    fts3PoslistCopy(pp, &aTmp2);
136785  }else{
136786    res = 0;
136787  }
136788
136789  return res;
136790}
136791
136792/*
136793** An instance of this function is used to merge together the (potentially
136794** large number of) doclists for each term that matches a prefix query.
136795** See function fts3TermSelectMerge() for details.
136796*/
136797typedef struct TermSelect TermSelect;
136798struct TermSelect {
136799  char *aaOutput[16];             /* Malloc'd output buffers */
136800  int anOutput[16];               /* Size each output buffer in bytes */
136801};
136802
136803/*
136804** This function is used to read a single varint from a buffer. Parameter
136805** pEnd points 1 byte past the end of the buffer. When this function is
136806** called, if *pp points to pEnd or greater, then the end of the buffer
136807** has been reached. In this case *pp is set to 0 and the function returns.
136808**
136809** If *pp does not point to or past pEnd, then a single varint is read
136810** from *pp. *pp is then set to point 1 byte past the end of the read varint.
136811**
136812** If bDescIdx is false, the value read is added to *pVal before returning.
136813** If it is true, the value read is subtracted from *pVal before this
136814** function returns.
136815*/
136816static void fts3GetDeltaVarint3(
136817  char **pp,                      /* IN/OUT: Point to read varint from */
136818  char *pEnd,                     /* End of buffer */
136819  int bDescIdx,                   /* True if docids are descending */
136820  sqlite3_int64 *pVal             /* IN/OUT: Integer value */
136821){
136822  if( *pp>=pEnd ){
136823    *pp = 0;
136824  }else{
136825    sqlite3_int64 iVal;
136826    *pp += sqlite3Fts3GetVarint(*pp, &iVal);
136827    if( bDescIdx ){
136828      *pVal -= iVal;
136829    }else{
136830      *pVal += iVal;
136831    }
136832  }
136833}
136834
136835/*
136836** This function is used to write a single varint to a buffer. The varint
136837** is written to *pp. Before returning, *pp is set to point 1 byte past the
136838** end of the value written.
136839**
136840** If *pbFirst is zero when this function is called, the value written to
136841** the buffer is that of parameter iVal.
136842**
136843** If *pbFirst is non-zero when this function is called, then the value
136844** written is either (iVal-*piPrev) (if bDescIdx is zero) or (*piPrev-iVal)
136845** (if bDescIdx is non-zero).
136846**
136847** Before returning, this function always sets *pbFirst to 1 and *piPrev
136848** to the value of parameter iVal.
136849*/
136850static void fts3PutDeltaVarint3(
136851  char **pp,                      /* IN/OUT: Output pointer */
136852  int bDescIdx,                   /* True for descending docids */
136853  sqlite3_int64 *piPrev,          /* IN/OUT: Previous value written to list */
136854  int *pbFirst,                   /* IN/OUT: True after first int written */
136855  sqlite3_int64 iVal              /* Write this value to the list */
136856){
136857  sqlite3_int64 iWrite;
136858  if( bDescIdx==0 || *pbFirst==0 ){
136859    iWrite = iVal - *piPrev;
136860  }else{
136861    iWrite = *piPrev - iVal;
136862  }
136863  assert( *pbFirst || *piPrev==0 );
136864  assert( *pbFirst==0 || iWrite>0 );
136865  *pp += sqlite3Fts3PutVarint(*pp, iWrite);
136866  *piPrev = iVal;
136867  *pbFirst = 1;
136868}
136869
136870
136871/*
136872** This macro is used by various functions that merge doclists. The two
136873** arguments are 64-bit docid values. If the value of the stack variable
136874** bDescDoclist is 0 when this macro is invoked, then it returns (i1-i2).
136875** Otherwise, (i2-i1).
136876**
136877** Using this makes it easier to write code that can merge doclists that are
136878** sorted in either ascending or descending order.
136879*/
136880#define DOCID_CMP(i1, i2) ((bDescDoclist?-1:1) * (i1-i2))
136881
136882/*
136883** This function does an "OR" merge of two doclists (output contains all
136884** positions contained in either argument doclist). If the docids in the
136885** input doclists are sorted in ascending order, parameter bDescDoclist
136886** should be false. If they are sorted in ascending order, it should be
136887** passed a non-zero value.
136888**
136889** If no error occurs, *paOut is set to point at an sqlite3_malloc'd buffer
136890** containing the output doclist and SQLITE_OK is returned. In this case
136891** *pnOut is set to the number of bytes in the output doclist.
136892**
136893** If an error occurs, an SQLite error code is returned. The output values
136894** are undefined in this case.
136895*/
136896static int fts3DoclistOrMerge(
136897  int bDescDoclist,               /* True if arguments are desc */
136898  char *a1, int n1,               /* First doclist */
136899  char *a2, int n2,               /* Second doclist */
136900  char **paOut, int *pnOut        /* OUT: Malloc'd doclist */
136901){
136902  sqlite3_int64 i1 = 0;
136903  sqlite3_int64 i2 = 0;
136904  sqlite3_int64 iPrev = 0;
136905  char *pEnd1 = &a1[n1];
136906  char *pEnd2 = &a2[n2];
136907  char *p1 = a1;
136908  char *p2 = a2;
136909  char *p;
136910  char *aOut;
136911  int bFirstOut = 0;
136912
136913  *paOut = 0;
136914  *pnOut = 0;
136915
136916  /* Allocate space for the output. Both the input and output doclists
136917  ** are delta encoded. If they are in ascending order (bDescDoclist==0),
136918  ** then the first docid in each list is simply encoded as a varint. For
136919  ** each subsequent docid, the varint stored is the difference between the
136920  ** current and previous docid (a positive number - since the list is in
136921  ** ascending order).
136922  **
136923  ** The first docid written to the output is therefore encoded using the
136924  ** same number of bytes as it is in whichever of the input lists it is
136925  ** read from. And each subsequent docid read from the same input list
136926  ** consumes either the same or less bytes as it did in the input (since
136927  ** the difference between it and the previous value in the output must
136928  ** be a positive value less than or equal to the delta value read from
136929  ** the input list). The same argument applies to all but the first docid
136930  ** read from the 'other' list. And to the contents of all position lists
136931  ** that will be copied and merged from the input to the output.
136932  **
136933  ** However, if the first docid copied to the output is a negative number,
136934  ** then the encoding of the first docid from the 'other' input list may
136935  ** be larger in the output than it was in the input (since the delta value
136936  ** may be a larger positive integer than the actual docid).
136937  **
136938  ** The space required to store the output is therefore the sum of the
136939  ** sizes of the two inputs, plus enough space for exactly one of the input
136940  ** docids to grow.
136941  **
136942  ** A symetric argument may be made if the doclists are in descending
136943  ** order.
136944  */
136945  aOut = sqlite3_malloc(n1+n2+FTS3_VARINT_MAX-1);
136946  if( !aOut ) return SQLITE_NOMEM;
136947
136948  p = aOut;
136949  fts3GetDeltaVarint3(&p1, pEnd1, 0, &i1);
136950  fts3GetDeltaVarint3(&p2, pEnd2, 0, &i2);
136951  while( p1 || p2 ){
136952    sqlite3_int64 iDiff = DOCID_CMP(i1, i2);
136953
136954    if( p2 && p1 && iDiff==0 ){
136955      fts3PutDeltaVarint3(&p, bDescDoclist, &iPrev, &bFirstOut, i1);
136956      fts3PoslistMerge(&p, &p1, &p2);
136957      fts3GetDeltaVarint3(&p1, pEnd1, bDescDoclist, &i1);
136958      fts3GetDeltaVarint3(&p2, pEnd2, bDescDoclist, &i2);
136959    }else if( !p2 || (p1 && iDiff<0) ){
136960      fts3PutDeltaVarint3(&p, bDescDoclist, &iPrev, &bFirstOut, i1);
136961      fts3PoslistCopy(&p, &p1);
136962      fts3GetDeltaVarint3(&p1, pEnd1, bDescDoclist, &i1);
136963    }else{
136964      fts3PutDeltaVarint3(&p, bDescDoclist, &iPrev, &bFirstOut, i2);
136965      fts3PoslistCopy(&p, &p2);
136966      fts3GetDeltaVarint3(&p2, pEnd2, bDescDoclist, &i2);
136967    }
136968  }
136969
136970  *paOut = aOut;
136971  *pnOut = (int)(p-aOut);
136972  assert( *pnOut<=n1+n2+FTS3_VARINT_MAX-1 );
136973  return SQLITE_OK;
136974}
136975
136976/*
136977** This function does a "phrase" merge of two doclists. In a phrase merge,
136978** the output contains a copy of each position from the right-hand input
136979** doclist for which there is a position in the left-hand input doclist
136980** exactly nDist tokens before it.
136981**
136982** If the docids in the input doclists are sorted in ascending order,
136983** parameter bDescDoclist should be false. If they are sorted in ascending
136984** order, it should be passed a non-zero value.
136985**
136986** The right-hand input doclist is overwritten by this function.
136987*/
136988static int fts3DoclistPhraseMerge(
136989  int bDescDoclist,               /* True if arguments are desc */
136990  int nDist,                      /* Distance from left to right (1=adjacent) */
136991  char *aLeft, int nLeft,         /* Left doclist */
136992  char **paRight, int *pnRight    /* IN/OUT: Right/output doclist */
136993){
136994  sqlite3_int64 i1 = 0;
136995  sqlite3_int64 i2 = 0;
136996  sqlite3_int64 iPrev = 0;
136997  char *aRight = *paRight;
136998  char *pEnd1 = &aLeft[nLeft];
136999  char *pEnd2 = &aRight[*pnRight];
137000  char *p1 = aLeft;
137001  char *p2 = aRight;
137002  char *p;
137003  int bFirstOut = 0;
137004  char *aOut;
137005
137006  assert( nDist>0 );
137007  if( bDescDoclist ){
137008    aOut = sqlite3_malloc(*pnRight + FTS3_VARINT_MAX);
137009    if( aOut==0 ) return SQLITE_NOMEM;
137010  }else{
137011    aOut = aRight;
137012  }
137013  p = aOut;
137014
137015  fts3GetDeltaVarint3(&p1, pEnd1, 0, &i1);
137016  fts3GetDeltaVarint3(&p2, pEnd2, 0, &i2);
137017
137018  while( p1 && p2 ){
137019    sqlite3_int64 iDiff = DOCID_CMP(i1, i2);
137020    if( iDiff==0 ){
137021      char *pSave = p;
137022      sqlite3_int64 iPrevSave = iPrev;
137023      int bFirstOutSave = bFirstOut;
137024
137025      fts3PutDeltaVarint3(&p, bDescDoclist, &iPrev, &bFirstOut, i1);
137026      if( 0==fts3PoslistPhraseMerge(&p, nDist, 0, 1, &p1, &p2) ){
137027        p = pSave;
137028        iPrev = iPrevSave;
137029        bFirstOut = bFirstOutSave;
137030      }
137031      fts3GetDeltaVarint3(&p1, pEnd1, bDescDoclist, &i1);
137032      fts3GetDeltaVarint3(&p2, pEnd2, bDescDoclist, &i2);
137033    }else if( iDiff<0 ){
137034      fts3PoslistCopy(0, &p1);
137035      fts3GetDeltaVarint3(&p1, pEnd1, bDescDoclist, &i1);
137036    }else{
137037      fts3PoslistCopy(0, &p2);
137038      fts3GetDeltaVarint3(&p2, pEnd2, bDescDoclist, &i2);
137039    }
137040  }
137041
137042  *pnRight = (int)(p - aOut);
137043  if( bDescDoclist ){
137044    sqlite3_free(aRight);
137045    *paRight = aOut;
137046  }
137047
137048  return SQLITE_OK;
137049}
137050
137051/*
137052** Argument pList points to a position list nList bytes in size. This
137053** function checks to see if the position list contains any entries for
137054** a token in position 0 (of any column). If so, it writes argument iDelta
137055** to the output buffer pOut, followed by a position list consisting only
137056** of the entries from pList at position 0, and terminated by an 0x00 byte.
137057** The value returned is the number of bytes written to pOut (if any).
137058*/
137059SQLITE_PRIVATE int sqlite3Fts3FirstFilter(
137060  sqlite3_int64 iDelta,           /* Varint that may be written to pOut */
137061  char *pList,                    /* Position list (no 0x00 term) */
137062  int nList,                      /* Size of pList in bytes */
137063  char *pOut                      /* Write output here */
137064){
137065  int nOut = 0;
137066  int bWritten = 0;               /* True once iDelta has been written */
137067  char *p = pList;
137068  char *pEnd = &pList[nList];
137069
137070  if( *p!=0x01 ){
137071    if( *p==0x02 ){
137072      nOut += sqlite3Fts3PutVarint(&pOut[nOut], iDelta);
137073      pOut[nOut++] = 0x02;
137074      bWritten = 1;
137075    }
137076    fts3ColumnlistCopy(0, &p);
137077  }
137078
137079  while( p<pEnd && *p==0x01 ){
137080    sqlite3_int64 iCol;
137081    p++;
137082    p += sqlite3Fts3GetVarint(p, &iCol);
137083    if( *p==0x02 ){
137084      if( bWritten==0 ){
137085        nOut += sqlite3Fts3PutVarint(&pOut[nOut], iDelta);
137086        bWritten = 1;
137087      }
137088      pOut[nOut++] = 0x01;
137089      nOut += sqlite3Fts3PutVarint(&pOut[nOut], iCol);
137090      pOut[nOut++] = 0x02;
137091    }
137092    fts3ColumnlistCopy(0, &p);
137093  }
137094  if( bWritten ){
137095    pOut[nOut++] = 0x00;
137096  }
137097
137098  return nOut;
137099}
137100
137101
137102/*
137103** Merge all doclists in the TermSelect.aaOutput[] array into a single
137104** doclist stored in TermSelect.aaOutput[0]. If successful, delete all
137105** other doclists (except the aaOutput[0] one) and return SQLITE_OK.
137106**
137107** If an OOM error occurs, return SQLITE_NOMEM. In this case it is
137108** the responsibility of the caller to free any doclists left in the
137109** TermSelect.aaOutput[] array.
137110*/
137111static int fts3TermSelectFinishMerge(Fts3Table *p, TermSelect *pTS){
137112  char *aOut = 0;
137113  int nOut = 0;
137114  int i;
137115
137116  /* Loop through the doclists in the aaOutput[] array. Merge them all
137117  ** into a single doclist.
137118  */
137119  for(i=0; i<SizeofArray(pTS->aaOutput); i++){
137120    if( pTS->aaOutput[i] ){
137121      if( !aOut ){
137122        aOut = pTS->aaOutput[i];
137123        nOut = pTS->anOutput[i];
137124        pTS->aaOutput[i] = 0;
137125      }else{
137126        int nNew;
137127        char *aNew;
137128
137129        int rc = fts3DoclistOrMerge(p->bDescIdx,
137130            pTS->aaOutput[i], pTS->anOutput[i], aOut, nOut, &aNew, &nNew
137131        );
137132        if( rc!=SQLITE_OK ){
137133          sqlite3_free(aOut);
137134          return rc;
137135        }
137136
137137        sqlite3_free(pTS->aaOutput[i]);
137138        sqlite3_free(aOut);
137139        pTS->aaOutput[i] = 0;
137140        aOut = aNew;
137141        nOut = nNew;
137142      }
137143    }
137144  }
137145
137146  pTS->aaOutput[0] = aOut;
137147  pTS->anOutput[0] = nOut;
137148  return SQLITE_OK;
137149}
137150
137151/*
137152** Merge the doclist aDoclist/nDoclist into the TermSelect object passed
137153** as the first argument. The merge is an "OR" merge (see function
137154** fts3DoclistOrMerge() for details).
137155**
137156** This function is called with the doclist for each term that matches
137157** a queried prefix. It merges all these doclists into one, the doclist
137158** for the specified prefix. Since there can be a very large number of
137159** doclists to merge, the merging is done pair-wise using the TermSelect
137160** object.
137161**
137162** This function returns SQLITE_OK if the merge is successful, or an
137163** SQLite error code (SQLITE_NOMEM) if an error occurs.
137164*/
137165static int fts3TermSelectMerge(
137166  Fts3Table *p,                   /* FTS table handle */
137167  TermSelect *pTS,                /* TermSelect object to merge into */
137168  char *aDoclist,                 /* Pointer to doclist */
137169  int nDoclist                    /* Size of aDoclist in bytes */
137170){
137171  if( pTS->aaOutput[0]==0 ){
137172    /* If this is the first term selected, copy the doclist to the output
137173    ** buffer using memcpy().
137174    **
137175    ** Add FTS3_VARINT_MAX bytes of unused space to the end of the
137176    ** allocation. This is so as to ensure that the buffer is big enough
137177    ** to hold the current doclist AND'd with any other doclist. If the
137178    ** doclists are stored in order=ASC order, this padding would not be
137179    ** required (since the size of [doclistA AND doclistB] is always less
137180    ** than or equal to the size of [doclistA] in that case). But this is
137181    ** not true for order=DESC. For example, a doclist containing (1, -1)
137182    ** may be smaller than (-1), as in the first example the -1 may be stored
137183    ** as a single-byte delta, whereas in the second it must be stored as a
137184    ** FTS3_VARINT_MAX byte varint.
137185    **
137186    ** Similar padding is added in the fts3DoclistOrMerge() function.
137187    */
137188    pTS->aaOutput[0] = sqlite3_malloc(nDoclist + FTS3_VARINT_MAX + 1);
137189    pTS->anOutput[0] = nDoclist;
137190    if( pTS->aaOutput[0] ){
137191      memcpy(pTS->aaOutput[0], aDoclist, nDoclist);
137192    }else{
137193      return SQLITE_NOMEM;
137194    }
137195  }else{
137196    char *aMerge = aDoclist;
137197    int nMerge = nDoclist;
137198    int iOut;
137199
137200    for(iOut=0; iOut<SizeofArray(pTS->aaOutput); iOut++){
137201      if( pTS->aaOutput[iOut]==0 ){
137202        assert( iOut>0 );
137203        pTS->aaOutput[iOut] = aMerge;
137204        pTS->anOutput[iOut] = nMerge;
137205        break;
137206      }else{
137207        char *aNew;
137208        int nNew;
137209
137210        int rc = fts3DoclistOrMerge(p->bDescIdx, aMerge, nMerge,
137211            pTS->aaOutput[iOut], pTS->anOutput[iOut], &aNew, &nNew
137212        );
137213        if( rc!=SQLITE_OK ){
137214          if( aMerge!=aDoclist ) sqlite3_free(aMerge);
137215          return rc;
137216        }
137217
137218        if( aMerge!=aDoclist ) sqlite3_free(aMerge);
137219        sqlite3_free(pTS->aaOutput[iOut]);
137220        pTS->aaOutput[iOut] = 0;
137221
137222        aMerge = aNew;
137223        nMerge = nNew;
137224        if( (iOut+1)==SizeofArray(pTS->aaOutput) ){
137225          pTS->aaOutput[iOut] = aMerge;
137226          pTS->anOutput[iOut] = nMerge;
137227        }
137228      }
137229    }
137230  }
137231  return SQLITE_OK;
137232}
137233
137234/*
137235** Append SegReader object pNew to the end of the pCsr->apSegment[] array.
137236*/
137237static int fts3SegReaderCursorAppend(
137238  Fts3MultiSegReader *pCsr,
137239  Fts3SegReader *pNew
137240){
137241  if( (pCsr->nSegment%16)==0 ){
137242    Fts3SegReader **apNew;
137243    int nByte = (pCsr->nSegment + 16)*sizeof(Fts3SegReader*);
137244    apNew = (Fts3SegReader **)sqlite3_realloc(pCsr->apSegment, nByte);
137245    if( !apNew ){
137246      sqlite3Fts3SegReaderFree(pNew);
137247      return SQLITE_NOMEM;
137248    }
137249    pCsr->apSegment = apNew;
137250  }
137251  pCsr->apSegment[pCsr->nSegment++] = pNew;
137252  return SQLITE_OK;
137253}
137254
137255/*
137256** Add seg-reader objects to the Fts3MultiSegReader object passed as the
137257** 8th argument.
137258**
137259** This function returns SQLITE_OK if successful, or an SQLite error code
137260** otherwise.
137261*/
137262static int fts3SegReaderCursor(
137263  Fts3Table *p,                   /* FTS3 table handle */
137264  int iLangid,                    /* Language id */
137265  int iIndex,                     /* Index to search (from 0 to p->nIndex-1) */
137266  int iLevel,                     /* Level of segments to scan */
137267  const char *zTerm,              /* Term to query for */
137268  int nTerm,                      /* Size of zTerm in bytes */
137269  int isPrefix,                   /* True for a prefix search */
137270  int isScan,                     /* True to scan from zTerm to EOF */
137271  Fts3MultiSegReader *pCsr        /* Cursor object to populate */
137272){
137273  int rc = SQLITE_OK;             /* Error code */
137274  sqlite3_stmt *pStmt = 0;        /* Statement to iterate through segments */
137275  int rc2;                        /* Result of sqlite3_reset() */
137276
137277  /* If iLevel is less than 0 and this is not a scan, include a seg-reader
137278  ** for the pending-terms. If this is a scan, then this call must be being
137279  ** made by an fts4aux module, not an FTS table. In this case calling
137280  ** Fts3SegReaderPending might segfault, as the data structures used by
137281  ** fts4aux are not completely populated. So it's easiest to filter these
137282  ** calls out here.  */
137283  if( iLevel<0 && p->aIndex ){
137284    Fts3SegReader *pSeg = 0;
137285    rc = sqlite3Fts3SegReaderPending(p, iIndex, zTerm, nTerm, isPrefix||isScan, &pSeg);
137286    if( rc==SQLITE_OK && pSeg ){
137287      rc = fts3SegReaderCursorAppend(pCsr, pSeg);
137288    }
137289  }
137290
137291  if( iLevel!=FTS3_SEGCURSOR_PENDING ){
137292    if( rc==SQLITE_OK ){
137293      rc = sqlite3Fts3AllSegdirs(p, iLangid, iIndex, iLevel, &pStmt);
137294    }
137295
137296    while( rc==SQLITE_OK && SQLITE_ROW==(rc = sqlite3_step(pStmt)) ){
137297      Fts3SegReader *pSeg = 0;
137298
137299      /* Read the values returned by the SELECT into local variables. */
137300      sqlite3_int64 iStartBlock = sqlite3_column_int64(pStmt, 1);
137301      sqlite3_int64 iLeavesEndBlock = sqlite3_column_int64(pStmt, 2);
137302      sqlite3_int64 iEndBlock = sqlite3_column_int64(pStmt, 3);
137303      int nRoot = sqlite3_column_bytes(pStmt, 4);
137304      char const *zRoot = sqlite3_column_blob(pStmt, 4);
137305
137306      /* If zTerm is not NULL, and this segment is not stored entirely on its
137307      ** root node, the range of leaves scanned can be reduced. Do this. */
137308      if( iStartBlock && zTerm ){
137309        sqlite3_int64 *pi = (isPrefix ? &iLeavesEndBlock : 0);
137310        rc = fts3SelectLeaf(p, zTerm, nTerm, zRoot, nRoot, &iStartBlock, pi);
137311        if( rc!=SQLITE_OK ) goto finished;
137312        if( isPrefix==0 && isScan==0 ) iLeavesEndBlock = iStartBlock;
137313      }
137314
137315      rc = sqlite3Fts3SegReaderNew(pCsr->nSegment+1,
137316          (isPrefix==0 && isScan==0),
137317          iStartBlock, iLeavesEndBlock,
137318          iEndBlock, zRoot, nRoot, &pSeg
137319      );
137320      if( rc!=SQLITE_OK ) goto finished;
137321      rc = fts3SegReaderCursorAppend(pCsr, pSeg);
137322    }
137323  }
137324
137325 finished:
137326  rc2 = sqlite3_reset(pStmt);
137327  if( rc==SQLITE_DONE ) rc = rc2;
137328
137329  return rc;
137330}
137331
137332/*
137333** Set up a cursor object for iterating through a full-text index or a
137334** single level therein.
137335*/
137336SQLITE_PRIVATE int sqlite3Fts3SegReaderCursor(
137337  Fts3Table *p,                   /* FTS3 table handle */
137338  int iLangid,                    /* Language-id to search */
137339  int iIndex,                     /* Index to search (from 0 to p->nIndex-1) */
137340  int iLevel,                     /* Level of segments to scan */
137341  const char *zTerm,              /* Term to query for */
137342  int nTerm,                      /* Size of zTerm in bytes */
137343  int isPrefix,                   /* True for a prefix search */
137344  int isScan,                     /* True to scan from zTerm to EOF */
137345  Fts3MultiSegReader *pCsr       /* Cursor object to populate */
137346){
137347  assert( iIndex>=0 && iIndex<p->nIndex );
137348  assert( iLevel==FTS3_SEGCURSOR_ALL
137349      ||  iLevel==FTS3_SEGCURSOR_PENDING
137350      ||  iLevel>=0
137351  );
137352  assert( iLevel<FTS3_SEGDIR_MAXLEVEL );
137353  assert( FTS3_SEGCURSOR_ALL<0 && FTS3_SEGCURSOR_PENDING<0 );
137354  assert( isPrefix==0 || isScan==0 );
137355
137356  memset(pCsr, 0, sizeof(Fts3MultiSegReader));
137357  return fts3SegReaderCursor(
137358      p, iLangid, iIndex, iLevel, zTerm, nTerm, isPrefix, isScan, pCsr
137359  );
137360}
137361
137362/*
137363** In addition to its current configuration, have the Fts3MultiSegReader
137364** passed as the 4th argument also scan the doclist for term zTerm/nTerm.
137365**
137366** SQLITE_OK is returned if no error occurs, otherwise an SQLite error code.
137367*/
137368static int fts3SegReaderCursorAddZero(
137369  Fts3Table *p,                   /* FTS virtual table handle */
137370  int iLangid,
137371  const char *zTerm,              /* Term to scan doclist of */
137372  int nTerm,                      /* Number of bytes in zTerm */
137373  Fts3MultiSegReader *pCsr        /* Fts3MultiSegReader to modify */
137374){
137375  return fts3SegReaderCursor(p,
137376      iLangid, 0, FTS3_SEGCURSOR_ALL, zTerm, nTerm, 0, 0,pCsr
137377  );
137378}
137379
137380/*
137381** Open an Fts3MultiSegReader to scan the doclist for term zTerm/nTerm. Or,
137382** if isPrefix is true, to scan the doclist for all terms for which
137383** zTerm/nTerm is a prefix. If successful, return SQLITE_OK and write
137384** a pointer to the new Fts3MultiSegReader to *ppSegcsr. Otherwise, return
137385** an SQLite error code.
137386**
137387** It is the responsibility of the caller to free this object by eventually
137388** passing it to fts3SegReaderCursorFree()
137389**
137390** SQLITE_OK is returned if no error occurs, otherwise an SQLite error code.
137391** Output parameter *ppSegcsr is set to 0 if an error occurs.
137392*/
137393static int fts3TermSegReaderCursor(
137394  Fts3Cursor *pCsr,               /* Virtual table cursor handle */
137395  const char *zTerm,              /* Term to query for */
137396  int nTerm,                      /* Size of zTerm in bytes */
137397  int isPrefix,                   /* True for a prefix search */
137398  Fts3MultiSegReader **ppSegcsr   /* OUT: Allocated seg-reader cursor */
137399){
137400  Fts3MultiSegReader *pSegcsr;    /* Object to allocate and return */
137401  int rc = SQLITE_NOMEM;          /* Return code */
137402
137403  pSegcsr = sqlite3_malloc(sizeof(Fts3MultiSegReader));
137404  if( pSegcsr ){
137405    int i;
137406    int bFound = 0;               /* True once an index has been found */
137407    Fts3Table *p = (Fts3Table *)pCsr->base.pVtab;
137408
137409    if( isPrefix ){
137410      for(i=1; bFound==0 && i<p->nIndex; i++){
137411        if( p->aIndex[i].nPrefix==nTerm ){
137412          bFound = 1;
137413          rc = sqlite3Fts3SegReaderCursor(p, pCsr->iLangid,
137414              i, FTS3_SEGCURSOR_ALL, zTerm, nTerm, 0, 0, pSegcsr
137415          );
137416          pSegcsr->bLookup = 1;
137417        }
137418      }
137419
137420      for(i=1; bFound==0 && i<p->nIndex; i++){
137421        if( p->aIndex[i].nPrefix==nTerm+1 ){
137422          bFound = 1;
137423          rc = sqlite3Fts3SegReaderCursor(p, pCsr->iLangid,
137424              i, FTS3_SEGCURSOR_ALL, zTerm, nTerm, 1, 0, pSegcsr
137425          );
137426          if( rc==SQLITE_OK ){
137427            rc = fts3SegReaderCursorAddZero(
137428                p, pCsr->iLangid, zTerm, nTerm, pSegcsr
137429            );
137430          }
137431        }
137432      }
137433    }
137434
137435    if( bFound==0 ){
137436      rc = sqlite3Fts3SegReaderCursor(p, pCsr->iLangid,
137437          0, FTS3_SEGCURSOR_ALL, zTerm, nTerm, isPrefix, 0, pSegcsr
137438      );
137439      pSegcsr->bLookup = !isPrefix;
137440    }
137441  }
137442
137443  *ppSegcsr = pSegcsr;
137444  return rc;
137445}
137446
137447/*
137448** Free an Fts3MultiSegReader allocated by fts3TermSegReaderCursor().
137449*/
137450static void fts3SegReaderCursorFree(Fts3MultiSegReader *pSegcsr){
137451  sqlite3Fts3SegReaderFinish(pSegcsr);
137452  sqlite3_free(pSegcsr);
137453}
137454
137455/*
137456** This function retrieves the doclist for the specified term (or term
137457** prefix) from the database.
137458*/
137459static int fts3TermSelect(
137460  Fts3Table *p,                   /* Virtual table handle */
137461  Fts3PhraseToken *pTok,          /* Token to query for */
137462  int iColumn,                    /* Column to query (or -ve for all columns) */
137463  int *pnOut,                     /* OUT: Size of buffer at *ppOut */
137464  char **ppOut                    /* OUT: Malloced result buffer */
137465){
137466  int rc;                         /* Return code */
137467  Fts3MultiSegReader *pSegcsr;    /* Seg-reader cursor for this term */
137468  TermSelect tsc;                 /* Object for pair-wise doclist merging */
137469  Fts3SegFilter filter;           /* Segment term filter configuration */
137470
137471  pSegcsr = pTok->pSegcsr;
137472  memset(&tsc, 0, sizeof(TermSelect));
137473
137474  filter.flags = FTS3_SEGMENT_IGNORE_EMPTY | FTS3_SEGMENT_REQUIRE_POS
137475        | (pTok->isPrefix ? FTS3_SEGMENT_PREFIX : 0)
137476        | (pTok->bFirst ? FTS3_SEGMENT_FIRST : 0)
137477        | (iColumn<p->nColumn ? FTS3_SEGMENT_COLUMN_FILTER : 0);
137478  filter.iCol = iColumn;
137479  filter.zTerm = pTok->z;
137480  filter.nTerm = pTok->n;
137481
137482  rc = sqlite3Fts3SegReaderStart(p, pSegcsr, &filter);
137483  while( SQLITE_OK==rc
137484      && SQLITE_ROW==(rc = sqlite3Fts3SegReaderStep(p, pSegcsr))
137485  ){
137486    rc = fts3TermSelectMerge(p, &tsc, pSegcsr->aDoclist, pSegcsr->nDoclist);
137487  }
137488
137489  if( rc==SQLITE_OK ){
137490    rc = fts3TermSelectFinishMerge(p, &tsc);
137491  }
137492  if( rc==SQLITE_OK ){
137493    *ppOut = tsc.aaOutput[0];
137494    *pnOut = tsc.anOutput[0];
137495  }else{
137496    int i;
137497    for(i=0; i<SizeofArray(tsc.aaOutput); i++){
137498      sqlite3_free(tsc.aaOutput[i]);
137499    }
137500  }
137501
137502  fts3SegReaderCursorFree(pSegcsr);
137503  pTok->pSegcsr = 0;
137504  return rc;
137505}
137506
137507/*
137508** This function counts the total number of docids in the doclist stored
137509** in buffer aList[], size nList bytes.
137510**
137511** If the isPoslist argument is true, then it is assumed that the doclist
137512** contains a position-list following each docid. Otherwise, it is assumed
137513** that the doclist is simply a list of docids stored as delta encoded
137514** varints.
137515*/
137516static int fts3DoclistCountDocids(char *aList, int nList){
137517  int nDoc = 0;                   /* Return value */
137518  if( aList ){
137519    char *aEnd = &aList[nList];   /* Pointer to one byte after EOF */
137520    char *p = aList;              /* Cursor */
137521    while( p<aEnd ){
137522      nDoc++;
137523      while( (*p++)&0x80 );     /* Skip docid varint */
137524      fts3PoslistCopy(0, &p);   /* Skip over position list */
137525    }
137526  }
137527
137528  return nDoc;
137529}
137530
137531/*
137532** Advance the cursor to the next row in the %_content table that
137533** matches the search criteria.  For a MATCH search, this will be
137534** the next row that matches. For a full-table scan, this will be
137535** simply the next row in the %_content table.  For a docid lookup,
137536** this routine simply sets the EOF flag.
137537**
137538** Return SQLITE_OK if nothing goes wrong.  SQLITE_OK is returned
137539** even if we reach end-of-file.  The fts3EofMethod() will be called
137540** subsequently to determine whether or not an EOF was hit.
137541*/
137542static int fts3NextMethod(sqlite3_vtab_cursor *pCursor){
137543  int rc;
137544  Fts3Cursor *pCsr = (Fts3Cursor *)pCursor;
137545  if( pCsr->eSearch==FTS3_DOCID_SEARCH || pCsr->eSearch==FTS3_FULLSCAN_SEARCH ){
137546    if( SQLITE_ROW!=sqlite3_step(pCsr->pStmt) ){
137547      pCsr->isEof = 1;
137548      rc = sqlite3_reset(pCsr->pStmt);
137549    }else{
137550      pCsr->iPrevId = sqlite3_column_int64(pCsr->pStmt, 0);
137551      rc = SQLITE_OK;
137552    }
137553  }else{
137554    rc = fts3EvalNext((Fts3Cursor *)pCursor);
137555  }
137556  assert( ((Fts3Table *)pCsr->base.pVtab)->pSegments==0 );
137557  return rc;
137558}
137559
137560/*
137561** The following are copied from sqliteInt.h.
137562**
137563** Constants for the largest and smallest possible 64-bit signed integers.
137564** These macros are designed to work correctly on both 32-bit and 64-bit
137565** compilers.
137566*/
137567#ifndef SQLITE_AMALGAMATION
137568# define LARGEST_INT64  (0xffffffff|(((sqlite3_int64)0x7fffffff)<<32))
137569# define SMALLEST_INT64 (((sqlite3_int64)-1) - LARGEST_INT64)
137570#endif
137571
137572/*
137573** If the numeric type of argument pVal is "integer", then return it
137574** converted to a 64-bit signed integer. Otherwise, return a copy of
137575** the second parameter, iDefault.
137576*/
137577static sqlite3_int64 fts3DocidRange(sqlite3_value *pVal, i64 iDefault){
137578  if( pVal ){
137579    int eType = sqlite3_value_numeric_type(pVal);
137580    if( eType==SQLITE_INTEGER ){
137581      return sqlite3_value_int64(pVal);
137582    }
137583  }
137584  return iDefault;
137585}
137586
137587/*
137588** This is the xFilter interface for the virtual table.  See
137589** the virtual table xFilter method documentation for additional
137590** information.
137591**
137592** If idxNum==FTS3_FULLSCAN_SEARCH then do a full table scan against
137593** the %_content table.
137594**
137595** If idxNum==FTS3_DOCID_SEARCH then do a docid lookup for a single entry
137596** in the %_content table.
137597**
137598** If idxNum>=FTS3_FULLTEXT_SEARCH then use the full text index.  The
137599** column on the left-hand side of the MATCH operator is column
137600** number idxNum-FTS3_FULLTEXT_SEARCH, 0 indexed.  argv[0] is the right-hand
137601** side of the MATCH operator.
137602*/
137603static int fts3FilterMethod(
137604  sqlite3_vtab_cursor *pCursor,   /* The cursor used for this query */
137605  int idxNum,                     /* Strategy index */
137606  const char *idxStr,             /* Unused */
137607  int nVal,                       /* Number of elements in apVal */
137608  sqlite3_value **apVal           /* Arguments for the indexing scheme */
137609){
137610  int rc = SQLITE_OK;
137611  char *zSql;                     /* SQL statement used to access %_content */
137612  int eSearch;
137613  Fts3Table *p = (Fts3Table *)pCursor->pVtab;
137614  Fts3Cursor *pCsr = (Fts3Cursor *)pCursor;
137615
137616  sqlite3_value *pCons = 0;       /* The MATCH or rowid constraint, if any */
137617  sqlite3_value *pLangid = 0;     /* The "langid = ?" constraint, if any */
137618  sqlite3_value *pDocidGe = 0;    /* The "docid >= ?" constraint, if any */
137619  sqlite3_value *pDocidLe = 0;    /* The "docid <= ?" constraint, if any */
137620  int iIdx;
137621
137622  UNUSED_PARAMETER(idxStr);
137623  UNUSED_PARAMETER(nVal);
137624
137625  eSearch = (idxNum & 0x0000FFFF);
137626  assert( eSearch>=0 && eSearch<=(FTS3_FULLTEXT_SEARCH+p->nColumn) );
137627  assert( p->pSegments==0 );
137628
137629  /* Collect arguments into local variables */
137630  iIdx = 0;
137631  if( eSearch!=FTS3_FULLSCAN_SEARCH ) pCons = apVal[iIdx++];
137632  if( idxNum & FTS3_HAVE_LANGID ) pLangid = apVal[iIdx++];
137633  if( idxNum & FTS3_HAVE_DOCID_GE ) pDocidGe = apVal[iIdx++];
137634  if( idxNum & FTS3_HAVE_DOCID_LE ) pDocidLe = apVal[iIdx++];
137635  assert( iIdx==nVal );
137636
137637  /* In case the cursor has been used before, clear it now. */
137638  sqlite3_finalize(pCsr->pStmt);
137639  sqlite3_free(pCsr->aDoclist);
137640  sqlite3Fts3MIBufferFree(pCsr->pMIBuffer);
137641  sqlite3Fts3ExprFree(pCsr->pExpr);
137642  memset(&pCursor[1], 0, sizeof(Fts3Cursor)-sizeof(sqlite3_vtab_cursor));
137643
137644  /* Set the lower and upper bounds on docids to return */
137645  pCsr->iMinDocid = fts3DocidRange(pDocidGe, SMALLEST_INT64);
137646  pCsr->iMaxDocid = fts3DocidRange(pDocidLe, LARGEST_INT64);
137647
137648  if( idxStr ){
137649    pCsr->bDesc = (idxStr[0]=='D');
137650  }else{
137651    pCsr->bDesc = p->bDescIdx;
137652  }
137653  pCsr->eSearch = (i16)eSearch;
137654
137655  if( eSearch!=FTS3_DOCID_SEARCH && eSearch!=FTS3_FULLSCAN_SEARCH ){
137656    int iCol = eSearch-FTS3_FULLTEXT_SEARCH;
137657    const char *zQuery = (const char *)sqlite3_value_text(pCons);
137658
137659    if( zQuery==0 && sqlite3_value_type(pCons)!=SQLITE_NULL ){
137660      return SQLITE_NOMEM;
137661    }
137662
137663    pCsr->iLangid = 0;
137664    if( pLangid ) pCsr->iLangid = sqlite3_value_int(pLangid);
137665
137666    assert( p->base.zErrMsg==0 );
137667    rc = sqlite3Fts3ExprParse(p->pTokenizer, pCsr->iLangid,
137668        p->azColumn, p->bFts4, p->nColumn, iCol, zQuery, -1, &pCsr->pExpr,
137669        &p->base.zErrMsg
137670    );
137671    if( rc!=SQLITE_OK ){
137672      return rc;
137673    }
137674
137675    rc = fts3EvalStart(pCsr);
137676    sqlite3Fts3SegmentsClose(p);
137677    if( rc!=SQLITE_OK ) return rc;
137678    pCsr->pNextId = pCsr->aDoclist;
137679    pCsr->iPrevId = 0;
137680  }
137681
137682  /* Compile a SELECT statement for this cursor. For a full-table-scan, the
137683  ** statement loops through all rows of the %_content table. For a
137684  ** full-text query or docid lookup, the statement retrieves a single
137685  ** row by docid.
137686  */
137687  if( eSearch==FTS3_FULLSCAN_SEARCH ){
137688    if( pDocidGe || pDocidLe ){
137689      zSql = sqlite3_mprintf(
137690          "SELECT %s WHERE rowid BETWEEN %lld AND %lld ORDER BY rowid %s",
137691          p->zReadExprlist, pCsr->iMinDocid, pCsr->iMaxDocid,
137692          (pCsr->bDesc ? "DESC" : "ASC")
137693      );
137694    }else{
137695      zSql = sqlite3_mprintf("SELECT %s ORDER BY rowid %s",
137696          p->zReadExprlist, (pCsr->bDesc ? "DESC" : "ASC")
137697      );
137698    }
137699    if( zSql ){
137700      rc = sqlite3_prepare_v2(p->db, zSql, -1, &pCsr->pStmt, 0);
137701      sqlite3_free(zSql);
137702    }else{
137703      rc = SQLITE_NOMEM;
137704    }
137705  }else if( eSearch==FTS3_DOCID_SEARCH ){
137706    rc = fts3CursorSeekStmt(pCsr, &pCsr->pStmt);
137707    if( rc==SQLITE_OK ){
137708      rc = sqlite3_bind_value(pCsr->pStmt, 1, pCons);
137709    }
137710  }
137711  if( rc!=SQLITE_OK ) return rc;
137712
137713  return fts3NextMethod(pCursor);
137714}
137715
137716/*
137717** This is the xEof method of the virtual table. SQLite calls this
137718** routine to find out if it has reached the end of a result set.
137719*/
137720static int fts3EofMethod(sqlite3_vtab_cursor *pCursor){
137721  return ((Fts3Cursor *)pCursor)->isEof;
137722}
137723
137724/*
137725** This is the xRowid method. The SQLite core calls this routine to
137726** retrieve the rowid for the current row of the result set. fts3
137727** exposes %_content.docid as the rowid for the virtual table. The
137728** rowid should be written to *pRowid.
137729*/
137730static int fts3RowidMethod(sqlite3_vtab_cursor *pCursor, sqlite_int64 *pRowid){
137731  Fts3Cursor *pCsr = (Fts3Cursor *) pCursor;
137732  *pRowid = pCsr->iPrevId;
137733  return SQLITE_OK;
137734}
137735
137736/*
137737** This is the xColumn method, called by SQLite to request a value from
137738** the row that the supplied cursor currently points to.
137739**
137740** If:
137741**
137742**   (iCol <  p->nColumn)   -> The value of the iCol'th user column.
137743**   (iCol == p->nColumn)   -> Magic column with the same name as the table.
137744**   (iCol == p->nColumn+1) -> Docid column
137745**   (iCol == p->nColumn+2) -> Langid column
137746*/
137747static int fts3ColumnMethod(
137748  sqlite3_vtab_cursor *pCursor,   /* Cursor to retrieve value from */
137749  sqlite3_context *pCtx,          /* Context for sqlite3_result_xxx() calls */
137750  int iCol                        /* Index of column to read value from */
137751){
137752  int rc = SQLITE_OK;             /* Return Code */
137753  Fts3Cursor *pCsr = (Fts3Cursor *) pCursor;
137754  Fts3Table *p = (Fts3Table *)pCursor->pVtab;
137755
137756  /* The column value supplied by SQLite must be in range. */
137757  assert( iCol>=0 && iCol<=p->nColumn+2 );
137758
137759  if( iCol==p->nColumn+1 ){
137760    /* This call is a request for the "docid" column. Since "docid" is an
137761    ** alias for "rowid", use the xRowid() method to obtain the value.
137762    */
137763    sqlite3_result_int64(pCtx, pCsr->iPrevId);
137764  }else if( iCol==p->nColumn ){
137765    /* The extra column whose name is the same as the table.
137766    ** Return a blob which is a pointer to the cursor.  */
137767    sqlite3_result_blob(pCtx, &pCsr, sizeof(pCsr), SQLITE_TRANSIENT);
137768  }else if( iCol==p->nColumn+2 && pCsr->pExpr ){
137769    sqlite3_result_int64(pCtx, pCsr->iLangid);
137770  }else{
137771    /* The requested column is either a user column (one that contains
137772    ** indexed data), or the language-id column.  */
137773    rc = fts3CursorSeek(0, pCsr);
137774
137775    if( rc==SQLITE_OK ){
137776      if( iCol==p->nColumn+2 ){
137777        int iLangid = 0;
137778        if( p->zLanguageid ){
137779          iLangid = sqlite3_column_int(pCsr->pStmt, p->nColumn+1);
137780        }
137781        sqlite3_result_int(pCtx, iLangid);
137782      }else if( sqlite3_data_count(pCsr->pStmt)>(iCol+1) ){
137783        sqlite3_result_value(pCtx, sqlite3_column_value(pCsr->pStmt, iCol+1));
137784      }
137785    }
137786  }
137787
137788  assert( ((Fts3Table *)pCsr->base.pVtab)->pSegments==0 );
137789  return rc;
137790}
137791
137792/*
137793** This function is the implementation of the xUpdate callback used by
137794** FTS3 virtual tables. It is invoked by SQLite each time a row is to be
137795** inserted, updated or deleted.
137796*/
137797static int fts3UpdateMethod(
137798  sqlite3_vtab *pVtab,            /* Virtual table handle */
137799  int nArg,                       /* Size of argument array */
137800  sqlite3_value **apVal,          /* Array of arguments */
137801  sqlite_int64 *pRowid            /* OUT: The affected (or effected) rowid */
137802){
137803  return sqlite3Fts3UpdateMethod(pVtab, nArg, apVal, pRowid);
137804}
137805
137806/*
137807** Implementation of xSync() method. Flush the contents of the pending-terms
137808** hash-table to the database.
137809*/
137810static int fts3SyncMethod(sqlite3_vtab *pVtab){
137811
137812  /* Following an incremental-merge operation, assuming that the input
137813  ** segments are not completely consumed (the usual case), they are updated
137814  ** in place to remove the entries that have already been merged. This
137815  ** involves updating the leaf block that contains the smallest unmerged
137816  ** entry and each block (if any) between the leaf and the root node. So
137817  ** if the height of the input segment b-trees is N, and input segments
137818  ** are merged eight at a time, updating the input segments at the end
137819  ** of an incremental-merge requires writing (8*(1+N)) blocks. N is usually
137820  ** small - often between 0 and 2. So the overhead of the incremental
137821  ** merge is somewhere between 8 and 24 blocks. To avoid this overhead
137822  ** dwarfing the actual productive work accomplished, the incremental merge
137823  ** is only attempted if it will write at least 64 leaf blocks. Hence
137824  ** nMinMerge.
137825  **
137826  ** Of course, updating the input segments also involves deleting a bunch
137827  ** of blocks from the segments table. But this is not considered overhead
137828  ** as it would also be required by a crisis-merge that used the same input
137829  ** segments.
137830  */
137831  const u32 nMinMerge = 64;       /* Minimum amount of incr-merge work to do */
137832
137833  Fts3Table *p = (Fts3Table*)pVtab;
137834  int rc = sqlite3Fts3PendingTermsFlush(p);
137835
137836  if( rc==SQLITE_OK
137837   && p->nLeafAdd>(nMinMerge/16)
137838   && p->nAutoincrmerge && p->nAutoincrmerge!=0xff
137839  ){
137840    int mxLevel = 0;              /* Maximum relative level value in db */
137841    int A;                        /* Incr-merge parameter A */
137842
137843    rc = sqlite3Fts3MaxLevel(p, &mxLevel);
137844    assert( rc==SQLITE_OK || mxLevel==0 );
137845    A = p->nLeafAdd * mxLevel;
137846    A += (A/2);
137847    if( A>(int)nMinMerge ) rc = sqlite3Fts3Incrmerge(p, A, p->nAutoincrmerge);
137848  }
137849  sqlite3Fts3SegmentsClose(p);
137850  return rc;
137851}
137852
137853/*
137854** If it is currently unknown whether or not the FTS table has an %_stat
137855** table (if p->bHasStat==2), attempt to determine this (set p->bHasStat
137856** to 0 or 1). Return SQLITE_OK if successful, or an SQLite error code
137857** if an error occurs.
137858*/
137859static int fts3SetHasStat(Fts3Table *p){
137860  int rc = SQLITE_OK;
137861  if( p->bHasStat==2 ){
137862    const char *zFmt ="SELECT 1 FROM %Q.sqlite_master WHERE tbl_name='%q_stat'";
137863    char *zSql = sqlite3_mprintf(zFmt, p->zDb, p->zName);
137864    if( zSql ){
137865      sqlite3_stmt *pStmt = 0;
137866      rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
137867      if( rc==SQLITE_OK ){
137868        int bHasStat = (sqlite3_step(pStmt)==SQLITE_ROW);
137869        rc = sqlite3_finalize(pStmt);
137870        if( rc==SQLITE_OK ) p->bHasStat = bHasStat;
137871      }
137872      sqlite3_free(zSql);
137873    }else{
137874      rc = SQLITE_NOMEM;
137875    }
137876  }
137877  return rc;
137878}
137879
137880/*
137881** Implementation of xBegin() method.
137882*/
137883static int fts3BeginMethod(sqlite3_vtab *pVtab){
137884  Fts3Table *p = (Fts3Table*)pVtab;
137885  UNUSED_PARAMETER(pVtab);
137886  assert( p->pSegments==0 );
137887  assert( p->nPendingData==0 );
137888  assert( p->inTransaction!=1 );
137889  TESTONLY( p->inTransaction = 1 );
137890  TESTONLY( p->mxSavepoint = -1; );
137891  p->nLeafAdd = 0;
137892  return fts3SetHasStat(p);
137893}
137894
137895/*
137896** Implementation of xCommit() method. This is a no-op. The contents of
137897** the pending-terms hash-table have already been flushed into the database
137898** by fts3SyncMethod().
137899*/
137900static int fts3CommitMethod(sqlite3_vtab *pVtab){
137901  TESTONLY( Fts3Table *p = (Fts3Table*)pVtab );
137902  UNUSED_PARAMETER(pVtab);
137903  assert( p->nPendingData==0 );
137904  assert( p->inTransaction!=0 );
137905  assert( p->pSegments==0 );
137906  TESTONLY( p->inTransaction = 0 );
137907  TESTONLY( p->mxSavepoint = -1; );
137908  return SQLITE_OK;
137909}
137910
137911/*
137912** Implementation of xRollback(). Discard the contents of the pending-terms
137913** hash-table. Any changes made to the database are reverted by SQLite.
137914*/
137915static int fts3RollbackMethod(sqlite3_vtab *pVtab){
137916  Fts3Table *p = (Fts3Table*)pVtab;
137917  sqlite3Fts3PendingTermsClear(p);
137918  assert( p->inTransaction!=0 );
137919  TESTONLY( p->inTransaction = 0 );
137920  TESTONLY( p->mxSavepoint = -1; );
137921  return SQLITE_OK;
137922}
137923
137924/*
137925** When called, *ppPoslist must point to the byte immediately following the
137926** end of a position-list. i.e. ( (*ppPoslist)[-1]==POS_END ). This function
137927** moves *ppPoslist so that it instead points to the first byte of the
137928** same position list.
137929*/
137930static void fts3ReversePoslist(char *pStart, char **ppPoslist){
137931  char *p = &(*ppPoslist)[-2];
137932  char c = 0;
137933
137934  /* Skip backwards passed any trailing 0x00 bytes added by NearTrim() */
137935  while( p>pStart && (c=*p--)==0 );
137936
137937  /* Search backwards for a varint with value zero (the end of the previous
137938  ** poslist). This is an 0x00 byte preceded by some byte that does not
137939  ** have the 0x80 bit set.  */
137940  while( p>pStart && (*p & 0x80) | c ){
137941    c = *p--;
137942  }
137943  assert( p==pStart || c==0 );
137944
137945  /* At this point p points to that preceding byte without the 0x80 bit
137946  ** set. So to find the start of the poslist, skip forward 2 bytes then
137947  ** over a varint.
137948  **
137949  ** Normally. The other case is that p==pStart and the poslist to return
137950  ** is the first in the doclist. In this case do not skip forward 2 bytes.
137951  ** The second part of the if condition (c==0 && *ppPoslist>&p[2])
137952  ** is required for cases where the first byte of a doclist and the
137953  ** doclist is empty. For example, if the first docid is 10, a doclist
137954  ** that begins with:
137955  **
137956  **   0x0A 0x00 <next docid delta varint>
137957  */
137958  if( p>pStart || (c==0 && *ppPoslist>&p[2]) ){ p = &p[2]; }
137959  while( *p++&0x80 );
137960  *ppPoslist = p;
137961}
137962
137963/*
137964** Helper function used by the implementation of the overloaded snippet(),
137965** offsets() and optimize() SQL functions.
137966**
137967** If the value passed as the third argument is a blob of size
137968** sizeof(Fts3Cursor*), then the blob contents are copied to the
137969** output variable *ppCsr and SQLITE_OK is returned. Otherwise, an error
137970** message is written to context pContext and SQLITE_ERROR returned. The
137971** string passed via zFunc is used as part of the error message.
137972*/
137973static int fts3FunctionArg(
137974  sqlite3_context *pContext,      /* SQL function call context */
137975  const char *zFunc,              /* Function name */
137976  sqlite3_value *pVal,            /* argv[0] passed to function */
137977  Fts3Cursor **ppCsr              /* OUT: Store cursor handle here */
137978){
137979  Fts3Cursor *pRet;
137980  if( sqlite3_value_type(pVal)!=SQLITE_BLOB
137981   || sqlite3_value_bytes(pVal)!=sizeof(Fts3Cursor *)
137982  ){
137983    char *zErr = sqlite3_mprintf("illegal first argument to %s", zFunc);
137984    sqlite3_result_error(pContext, zErr, -1);
137985    sqlite3_free(zErr);
137986    return SQLITE_ERROR;
137987  }
137988  memcpy(&pRet, sqlite3_value_blob(pVal), sizeof(Fts3Cursor *));
137989  *ppCsr = pRet;
137990  return SQLITE_OK;
137991}
137992
137993/*
137994** Implementation of the snippet() function for FTS3
137995*/
137996static void fts3SnippetFunc(
137997  sqlite3_context *pContext,      /* SQLite function call context */
137998  int nVal,                       /* Size of apVal[] array */
137999  sqlite3_value **apVal           /* Array of arguments */
138000){
138001  Fts3Cursor *pCsr;               /* Cursor handle passed through apVal[0] */
138002  const char *zStart = "<b>";
138003  const char *zEnd = "</b>";
138004  const char *zEllipsis = "<b>...</b>";
138005  int iCol = -1;
138006  int nToken = 15;                /* Default number of tokens in snippet */
138007
138008  /* There must be at least one argument passed to this function (otherwise
138009  ** the non-overloaded version would have been called instead of this one).
138010  */
138011  assert( nVal>=1 );
138012
138013  if( nVal>6 ){
138014    sqlite3_result_error(pContext,
138015        "wrong number of arguments to function snippet()", -1);
138016    return;
138017  }
138018  if( fts3FunctionArg(pContext, "snippet", apVal[0], &pCsr) ) return;
138019
138020  switch( nVal ){
138021    case 6: nToken = sqlite3_value_int(apVal[5]);
138022    case 5: iCol = sqlite3_value_int(apVal[4]);
138023    case 4: zEllipsis = (const char*)sqlite3_value_text(apVal[3]);
138024    case 3: zEnd = (const char*)sqlite3_value_text(apVal[2]);
138025    case 2: zStart = (const char*)sqlite3_value_text(apVal[1]);
138026  }
138027  if( !zEllipsis || !zEnd || !zStart ){
138028    sqlite3_result_error_nomem(pContext);
138029  }else if( nToken==0 ){
138030    sqlite3_result_text(pContext, "", -1, SQLITE_STATIC);
138031  }else if( SQLITE_OK==fts3CursorSeek(pContext, pCsr) ){
138032    sqlite3Fts3Snippet(pContext, pCsr, zStart, zEnd, zEllipsis, iCol, nToken);
138033  }
138034}
138035
138036/*
138037** Implementation of the offsets() function for FTS3
138038*/
138039static void fts3OffsetsFunc(
138040  sqlite3_context *pContext,      /* SQLite function call context */
138041  int nVal,                       /* Size of argument array */
138042  sqlite3_value **apVal           /* Array of arguments */
138043){
138044  Fts3Cursor *pCsr;               /* Cursor handle passed through apVal[0] */
138045
138046  UNUSED_PARAMETER(nVal);
138047
138048  assert( nVal==1 );
138049  if( fts3FunctionArg(pContext, "offsets", apVal[0], &pCsr) ) return;
138050  assert( pCsr );
138051  if( SQLITE_OK==fts3CursorSeek(pContext, pCsr) ){
138052    sqlite3Fts3Offsets(pContext, pCsr);
138053  }
138054}
138055
138056/*
138057** Implementation of the special optimize() function for FTS3. This
138058** function merges all segments in the database to a single segment.
138059** Example usage is:
138060**
138061**   SELECT optimize(t) FROM t LIMIT 1;
138062**
138063** where 't' is the name of an FTS3 table.
138064*/
138065static void fts3OptimizeFunc(
138066  sqlite3_context *pContext,      /* SQLite function call context */
138067  int nVal,                       /* Size of argument array */
138068  sqlite3_value **apVal           /* Array of arguments */
138069){
138070  int rc;                         /* Return code */
138071  Fts3Table *p;                   /* Virtual table handle */
138072  Fts3Cursor *pCursor;            /* Cursor handle passed through apVal[0] */
138073
138074  UNUSED_PARAMETER(nVal);
138075
138076  assert( nVal==1 );
138077  if( fts3FunctionArg(pContext, "optimize", apVal[0], &pCursor) ) return;
138078  p = (Fts3Table *)pCursor->base.pVtab;
138079  assert( p );
138080
138081  rc = sqlite3Fts3Optimize(p);
138082
138083  switch( rc ){
138084    case SQLITE_OK:
138085      sqlite3_result_text(pContext, "Index optimized", -1, SQLITE_STATIC);
138086      break;
138087    case SQLITE_DONE:
138088      sqlite3_result_text(pContext, "Index already optimal", -1, SQLITE_STATIC);
138089      break;
138090    default:
138091      sqlite3_result_error_code(pContext, rc);
138092      break;
138093  }
138094}
138095
138096/*
138097** Implementation of the matchinfo() function for FTS3
138098*/
138099static void fts3MatchinfoFunc(
138100  sqlite3_context *pContext,      /* SQLite function call context */
138101  int nVal,                       /* Size of argument array */
138102  sqlite3_value **apVal           /* Array of arguments */
138103){
138104  Fts3Cursor *pCsr;               /* Cursor handle passed through apVal[0] */
138105  assert( nVal==1 || nVal==2 );
138106  if( SQLITE_OK==fts3FunctionArg(pContext, "matchinfo", apVal[0], &pCsr) ){
138107    const char *zArg = 0;
138108    if( nVal>1 ){
138109      zArg = (const char *)sqlite3_value_text(apVal[1]);
138110    }
138111    sqlite3Fts3Matchinfo(pContext, pCsr, zArg);
138112  }
138113}
138114
138115/*
138116** This routine implements the xFindFunction method for the FTS3
138117** virtual table.
138118*/
138119static int fts3FindFunctionMethod(
138120  sqlite3_vtab *pVtab,            /* Virtual table handle */
138121  int nArg,                       /* Number of SQL function arguments */
138122  const char *zName,              /* Name of SQL function */
138123  void (**pxFunc)(sqlite3_context*,int,sqlite3_value**), /* OUT: Result */
138124  void **ppArg                    /* Unused */
138125){
138126  struct Overloaded {
138127    const char *zName;
138128    void (*xFunc)(sqlite3_context*,int,sqlite3_value**);
138129  } aOverload[] = {
138130    { "snippet", fts3SnippetFunc },
138131    { "offsets", fts3OffsetsFunc },
138132    { "optimize", fts3OptimizeFunc },
138133    { "matchinfo", fts3MatchinfoFunc },
138134  };
138135  int i;                          /* Iterator variable */
138136
138137  UNUSED_PARAMETER(pVtab);
138138  UNUSED_PARAMETER(nArg);
138139  UNUSED_PARAMETER(ppArg);
138140
138141  for(i=0; i<SizeofArray(aOverload); i++){
138142    if( strcmp(zName, aOverload[i].zName)==0 ){
138143      *pxFunc = aOverload[i].xFunc;
138144      return 1;
138145    }
138146  }
138147
138148  /* No function of the specified name was found. Return 0. */
138149  return 0;
138150}
138151
138152/*
138153** Implementation of FTS3 xRename method. Rename an fts3 table.
138154*/
138155static int fts3RenameMethod(
138156  sqlite3_vtab *pVtab,            /* Virtual table handle */
138157  const char *zName               /* New name of table */
138158){
138159  Fts3Table *p = (Fts3Table *)pVtab;
138160  sqlite3 *db = p->db;            /* Database connection */
138161  int rc;                         /* Return Code */
138162
138163  /* At this point it must be known if the %_stat table exists or not.
138164  ** So bHasStat may not be 2.  */
138165  rc = fts3SetHasStat(p);
138166
138167  /* As it happens, the pending terms table is always empty here. This is
138168  ** because an "ALTER TABLE RENAME TABLE" statement inside a transaction
138169  ** always opens a savepoint transaction. And the xSavepoint() method
138170  ** flushes the pending terms table. But leave the (no-op) call to
138171  ** PendingTermsFlush() in in case that changes.
138172  */
138173  assert( p->nPendingData==0 );
138174  if( rc==SQLITE_OK ){
138175    rc = sqlite3Fts3PendingTermsFlush(p);
138176  }
138177
138178  if( p->zContentTbl==0 ){
138179    fts3DbExec(&rc, db,
138180      "ALTER TABLE %Q.'%q_content'  RENAME TO '%q_content';",
138181      p->zDb, p->zName, zName
138182    );
138183  }
138184
138185  if( p->bHasDocsize ){
138186    fts3DbExec(&rc, db,
138187      "ALTER TABLE %Q.'%q_docsize'  RENAME TO '%q_docsize';",
138188      p->zDb, p->zName, zName
138189    );
138190  }
138191  if( p->bHasStat ){
138192    fts3DbExec(&rc, db,
138193      "ALTER TABLE %Q.'%q_stat'  RENAME TO '%q_stat';",
138194      p->zDb, p->zName, zName
138195    );
138196  }
138197  fts3DbExec(&rc, db,
138198    "ALTER TABLE %Q.'%q_segments' RENAME TO '%q_segments';",
138199    p->zDb, p->zName, zName
138200  );
138201  fts3DbExec(&rc, db,
138202    "ALTER TABLE %Q.'%q_segdir'   RENAME TO '%q_segdir';",
138203    p->zDb, p->zName, zName
138204  );
138205  return rc;
138206}
138207
138208/*
138209** The xSavepoint() method.
138210**
138211** Flush the contents of the pending-terms table to disk.
138212*/
138213static int fts3SavepointMethod(sqlite3_vtab *pVtab, int iSavepoint){
138214  int rc = SQLITE_OK;
138215  UNUSED_PARAMETER(iSavepoint);
138216  assert( ((Fts3Table *)pVtab)->inTransaction );
138217  assert( ((Fts3Table *)pVtab)->mxSavepoint < iSavepoint );
138218  TESTONLY( ((Fts3Table *)pVtab)->mxSavepoint = iSavepoint );
138219  if( ((Fts3Table *)pVtab)->bIgnoreSavepoint==0 ){
138220    rc = fts3SyncMethod(pVtab);
138221  }
138222  return rc;
138223}
138224
138225/*
138226** The xRelease() method.
138227**
138228** This is a no-op.
138229*/
138230static int fts3ReleaseMethod(sqlite3_vtab *pVtab, int iSavepoint){
138231  TESTONLY( Fts3Table *p = (Fts3Table*)pVtab );
138232  UNUSED_PARAMETER(iSavepoint);
138233  UNUSED_PARAMETER(pVtab);
138234  assert( p->inTransaction );
138235  assert( p->mxSavepoint >= iSavepoint );
138236  TESTONLY( p->mxSavepoint = iSavepoint-1 );
138237  return SQLITE_OK;
138238}
138239
138240/*
138241** The xRollbackTo() method.
138242**
138243** Discard the contents of the pending terms table.
138244*/
138245static int fts3RollbackToMethod(sqlite3_vtab *pVtab, int iSavepoint){
138246  Fts3Table *p = (Fts3Table*)pVtab;
138247  UNUSED_PARAMETER(iSavepoint);
138248  assert( p->inTransaction );
138249  assert( p->mxSavepoint >= iSavepoint );
138250  TESTONLY( p->mxSavepoint = iSavepoint );
138251  sqlite3Fts3PendingTermsClear(p);
138252  return SQLITE_OK;
138253}
138254
138255static const sqlite3_module fts3Module = {
138256  /* iVersion      */ 2,
138257  /* xCreate       */ fts3CreateMethod,
138258  /* xConnect      */ fts3ConnectMethod,
138259  /* xBestIndex    */ fts3BestIndexMethod,
138260  /* xDisconnect   */ fts3DisconnectMethod,
138261  /* xDestroy      */ fts3DestroyMethod,
138262  /* xOpen         */ fts3OpenMethod,
138263  /* xClose        */ fts3CloseMethod,
138264  /* xFilter       */ fts3FilterMethod,
138265  /* xNext         */ fts3NextMethod,
138266  /* xEof          */ fts3EofMethod,
138267  /* xColumn       */ fts3ColumnMethod,
138268  /* xRowid        */ fts3RowidMethod,
138269  /* xUpdate       */ fts3UpdateMethod,
138270  /* xBegin        */ fts3BeginMethod,
138271  /* xSync         */ fts3SyncMethod,
138272  /* xCommit       */ fts3CommitMethod,
138273  /* xRollback     */ fts3RollbackMethod,
138274  /* xFindFunction */ fts3FindFunctionMethod,
138275  /* xRename */       fts3RenameMethod,
138276  /* xSavepoint    */ fts3SavepointMethod,
138277  /* xRelease      */ fts3ReleaseMethod,
138278  /* xRollbackTo   */ fts3RollbackToMethod,
138279};
138280
138281/*
138282** This function is registered as the module destructor (called when an
138283** FTS3 enabled database connection is closed). It frees the memory
138284** allocated for the tokenizer hash table.
138285*/
138286static void hashDestroy(void *p){
138287  Fts3Hash *pHash = (Fts3Hash *)p;
138288  sqlite3Fts3HashClear(pHash);
138289  sqlite3_free(pHash);
138290}
138291
138292/*
138293** The fts3 built-in tokenizers - "simple", "porter" and "icu"- are
138294** implemented in files fts3_tokenizer1.c, fts3_porter.c and fts3_icu.c
138295** respectively. The following three forward declarations are for functions
138296** declared in these files used to retrieve the respective implementations.
138297**
138298** Calling sqlite3Fts3SimpleTokenizerModule() sets the value pointed
138299** to by the argument to point to the "simple" tokenizer implementation.
138300** And so on.
138301*/
138302SQLITE_PRIVATE void sqlite3Fts3SimpleTokenizerModule(sqlite3_tokenizer_module const**ppModule);
138303SQLITE_PRIVATE void sqlite3Fts3PorterTokenizerModule(sqlite3_tokenizer_module const**ppModule);
138304#ifndef SQLITE_DISABLE_FTS3_UNICODE
138305SQLITE_PRIVATE void sqlite3Fts3UnicodeTokenizer(sqlite3_tokenizer_module const**ppModule);
138306#endif
138307#ifdef SQLITE_ENABLE_ICU
138308SQLITE_PRIVATE void sqlite3Fts3IcuTokenizerModule(sqlite3_tokenizer_module const**ppModule);
138309#endif
138310
138311/*
138312** Initialize the fts3 extension. If this extension is built as part
138313** of the sqlite library, then this function is called directly by
138314** SQLite. If fts3 is built as a dynamically loadable extension, this
138315** function is called by the sqlite3_extension_init() entry point.
138316*/
138317SQLITE_PRIVATE int sqlite3Fts3Init(sqlite3 *db){
138318  int rc = SQLITE_OK;
138319  Fts3Hash *pHash = 0;
138320  const sqlite3_tokenizer_module *pSimple = 0;
138321  const sqlite3_tokenizer_module *pPorter = 0;
138322#ifndef SQLITE_DISABLE_FTS3_UNICODE
138323  const sqlite3_tokenizer_module *pUnicode = 0;
138324#endif
138325
138326#ifdef SQLITE_ENABLE_ICU
138327  const sqlite3_tokenizer_module *pIcu = 0;
138328  sqlite3Fts3IcuTokenizerModule(&pIcu);
138329#endif
138330
138331#ifndef SQLITE_DISABLE_FTS3_UNICODE
138332  sqlite3Fts3UnicodeTokenizer(&pUnicode);
138333#endif
138334
138335#ifdef SQLITE_TEST
138336  rc = sqlite3Fts3InitTerm(db);
138337  if( rc!=SQLITE_OK ) return rc;
138338#endif
138339
138340  rc = sqlite3Fts3InitAux(db);
138341  if( rc!=SQLITE_OK ) return rc;
138342
138343  sqlite3Fts3SimpleTokenizerModule(&pSimple);
138344  sqlite3Fts3PorterTokenizerModule(&pPorter);
138345
138346  /* Allocate and initialize the hash-table used to store tokenizers. */
138347  pHash = sqlite3_malloc(sizeof(Fts3Hash));
138348  if( !pHash ){
138349    rc = SQLITE_NOMEM;
138350  }else{
138351    sqlite3Fts3HashInit(pHash, FTS3_HASH_STRING, 1);
138352  }
138353
138354  /* Load the built-in tokenizers into the hash table */
138355  if( rc==SQLITE_OK ){
138356    if( sqlite3Fts3HashInsert(pHash, "simple", 7, (void *)pSimple)
138357     || sqlite3Fts3HashInsert(pHash, "porter", 7, (void *)pPorter)
138358
138359#ifndef SQLITE_DISABLE_FTS3_UNICODE
138360     || sqlite3Fts3HashInsert(pHash, "unicode61", 10, (void *)pUnicode)
138361#endif
138362#ifdef SQLITE_ENABLE_ICU
138363     || (pIcu && sqlite3Fts3HashInsert(pHash, "icu", 4, (void *)pIcu))
138364#endif
138365    ){
138366      rc = SQLITE_NOMEM;
138367    }
138368  }
138369
138370#ifdef SQLITE_TEST
138371  if( rc==SQLITE_OK ){
138372    rc = sqlite3Fts3ExprInitTestInterface(db);
138373  }
138374#endif
138375
138376  /* Create the virtual table wrapper around the hash-table and overload
138377  ** the two scalar functions. If this is successful, register the
138378  ** module with sqlite.
138379  */
138380  if( SQLITE_OK==rc
138381   && SQLITE_OK==(rc = sqlite3Fts3InitHashTable(db, pHash, "fts3_tokenizer"))
138382   && SQLITE_OK==(rc = sqlite3_overload_function(db, "snippet", -1))
138383   && SQLITE_OK==(rc = sqlite3_overload_function(db, "offsets", 1))
138384   && SQLITE_OK==(rc = sqlite3_overload_function(db, "matchinfo", 1))
138385   && SQLITE_OK==(rc = sqlite3_overload_function(db, "matchinfo", 2))
138386   && SQLITE_OK==(rc = sqlite3_overload_function(db, "optimize", 1))
138387  ){
138388    rc = sqlite3_create_module_v2(
138389        db, "fts3", &fts3Module, (void *)pHash, hashDestroy
138390    );
138391    if( rc==SQLITE_OK ){
138392      rc = sqlite3_create_module_v2(
138393          db, "fts4", &fts3Module, (void *)pHash, 0
138394      );
138395    }
138396    if( rc==SQLITE_OK ){
138397      rc = sqlite3Fts3InitTok(db, (void *)pHash);
138398    }
138399    return rc;
138400  }
138401
138402
138403  /* An error has occurred. Delete the hash table and return the error code. */
138404  assert( rc!=SQLITE_OK );
138405  if( pHash ){
138406    sqlite3Fts3HashClear(pHash);
138407    sqlite3_free(pHash);
138408  }
138409  return rc;
138410}
138411
138412/*
138413** Allocate an Fts3MultiSegReader for each token in the expression headed
138414** by pExpr.
138415**
138416** An Fts3SegReader object is a cursor that can seek or scan a range of
138417** entries within a single segment b-tree. An Fts3MultiSegReader uses multiple
138418** Fts3SegReader objects internally to provide an interface to seek or scan
138419** within the union of all segments of a b-tree. Hence the name.
138420**
138421** If the allocated Fts3MultiSegReader just seeks to a single entry in a
138422** segment b-tree (if the term is not a prefix or it is a prefix for which
138423** there exists prefix b-tree of the right length) then it may be traversed
138424** and merged incrementally. Otherwise, it has to be merged into an in-memory
138425** doclist and then traversed.
138426*/
138427static void fts3EvalAllocateReaders(
138428  Fts3Cursor *pCsr,               /* FTS cursor handle */
138429  Fts3Expr *pExpr,                /* Allocate readers for this expression */
138430  int *pnToken,                   /* OUT: Total number of tokens in phrase. */
138431  int *pnOr,                      /* OUT: Total number of OR nodes in expr. */
138432  int *pRc                        /* IN/OUT: Error code */
138433){
138434  if( pExpr && SQLITE_OK==*pRc ){
138435    if( pExpr->eType==FTSQUERY_PHRASE ){
138436      int i;
138437      int nToken = pExpr->pPhrase->nToken;
138438      *pnToken += nToken;
138439      for(i=0; i<nToken; i++){
138440        Fts3PhraseToken *pToken = &pExpr->pPhrase->aToken[i];
138441        int rc = fts3TermSegReaderCursor(pCsr,
138442            pToken->z, pToken->n, pToken->isPrefix, &pToken->pSegcsr
138443        );
138444        if( rc!=SQLITE_OK ){
138445          *pRc = rc;
138446          return;
138447        }
138448      }
138449      assert( pExpr->pPhrase->iDoclistToken==0 );
138450      pExpr->pPhrase->iDoclistToken = -1;
138451    }else{
138452      *pnOr += (pExpr->eType==FTSQUERY_OR);
138453      fts3EvalAllocateReaders(pCsr, pExpr->pLeft, pnToken, pnOr, pRc);
138454      fts3EvalAllocateReaders(pCsr, pExpr->pRight, pnToken, pnOr, pRc);
138455    }
138456  }
138457}
138458
138459/*
138460** Arguments pList/nList contain the doclist for token iToken of phrase p.
138461** It is merged into the main doclist stored in p->doclist.aAll/nAll.
138462**
138463** This function assumes that pList points to a buffer allocated using
138464** sqlite3_malloc(). This function takes responsibility for eventually
138465** freeing the buffer.
138466**
138467** SQLITE_OK is returned if successful, or SQLITE_NOMEM if an error occurs.
138468*/
138469static int fts3EvalPhraseMergeToken(
138470  Fts3Table *pTab,                /* FTS Table pointer */
138471  Fts3Phrase *p,                  /* Phrase to merge pList/nList into */
138472  int iToken,                     /* Token pList/nList corresponds to */
138473  char *pList,                    /* Pointer to doclist */
138474  int nList                       /* Number of bytes in pList */
138475){
138476  int rc = SQLITE_OK;
138477  assert( iToken!=p->iDoclistToken );
138478
138479  if( pList==0 ){
138480    sqlite3_free(p->doclist.aAll);
138481    p->doclist.aAll = 0;
138482    p->doclist.nAll = 0;
138483  }
138484
138485  else if( p->iDoclistToken<0 ){
138486    p->doclist.aAll = pList;
138487    p->doclist.nAll = nList;
138488  }
138489
138490  else if( p->doclist.aAll==0 ){
138491    sqlite3_free(pList);
138492  }
138493
138494  else {
138495    char *pLeft;
138496    char *pRight;
138497    int nLeft;
138498    int nRight;
138499    int nDiff;
138500
138501    if( p->iDoclistToken<iToken ){
138502      pLeft = p->doclist.aAll;
138503      nLeft = p->doclist.nAll;
138504      pRight = pList;
138505      nRight = nList;
138506      nDiff = iToken - p->iDoclistToken;
138507    }else{
138508      pRight = p->doclist.aAll;
138509      nRight = p->doclist.nAll;
138510      pLeft = pList;
138511      nLeft = nList;
138512      nDiff = p->iDoclistToken - iToken;
138513    }
138514
138515    rc = fts3DoclistPhraseMerge(
138516        pTab->bDescIdx, nDiff, pLeft, nLeft, &pRight, &nRight
138517    );
138518    sqlite3_free(pLeft);
138519    p->doclist.aAll = pRight;
138520    p->doclist.nAll = nRight;
138521  }
138522
138523  if( iToken>p->iDoclistToken ) p->iDoclistToken = iToken;
138524  return rc;
138525}
138526
138527/*
138528** Load the doclist for phrase p into p->doclist.aAll/nAll. The loaded doclist
138529** does not take deferred tokens into account.
138530**
138531** SQLITE_OK is returned if no error occurs, otherwise an SQLite error code.
138532*/
138533static int fts3EvalPhraseLoad(
138534  Fts3Cursor *pCsr,               /* FTS Cursor handle */
138535  Fts3Phrase *p                   /* Phrase object */
138536){
138537  Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
138538  int iToken;
138539  int rc = SQLITE_OK;
138540
138541  for(iToken=0; rc==SQLITE_OK && iToken<p->nToken; iToken++){
138542    Fts3PhraseToken *pToken = &p->aToken[iToken];
138543    assert( pToken->pDeferred==0 || pToken->pSegcsr==0 );
138544
138545    if( pToken->pSegcsr ){
138546      int nThis = 0;
138547      char *pThis = 0;
138548      rc = fts3TermSelect(pTab, pToken, p->iColumn, &nThis, &pThis);
138549      if( rc==SQLITE_OK ){
138550        rc = fts3EvalPhraseMergeToken(pTab, p, iToken, pThis, nThis);
138551      }
138552    }
138553    assert( pToken->pSegcsr==0 );
138554  }
138555
138556  return rc;
138557}
138558
138559/*
138560** This function is called on each phrase after the position lists for
138561** any deferred tokens have been loaded into memory. It updates the phrases
138562** current position list to include only those positions that are really
138563** instances of the phrase (after considering deferred tokens). If this
138564** means that the phrase does not appear in the current row, doclist.pList
138565** and doclist.nList are both zeroed.
138566**
138567** SQLITE_OK is returned if no error occurs, otherwise an SQLite error code.
138568*/
138569static int fts3EvalDeferredPhrase(Fts3Cursor *pCsr, Fts3Phrase *pPhrase){
138570  int iToken;                     /* Used to iterate through phrase tokens */
138571  char *aPoslist = 0;             /* Position list for deferred tokens */
138572  int nPoslist = 0;               /* Number of bytes in aPoslist */
138573  int iPrev = -1;                 /* Token number of previous deferred token */
138574
138575  assert( pPhrase->doclist.bFreeList==0 );
138576
138577  for(iToken=0; iToken<pPhrase->nToken; iToken++){
138578    Fts3PhraseToken *pToken = &pPhrase->aToken[iToken];
138579    Fts3DeferredToken *pDeferred = pToken->pDeferred;
138580
138581    if( pDeferred ){
138582      char *pList;
138583      int nList;
138584      int rc = sqlite3Fts3DeferredTokenList(pDeferred, &pList, &nList);
138585      if( rc!=SQLITE_OK ) return rc;
138586
138587      if( pList==0 ){
138588        sqlite3_free(aPoslist);
138589        pPhrase->doclist.pList = 0;
138590        pPhrase->doclist.nList = 0;
138591        return SQLITE_OK;
138592
138593      }else if( aPoslist==0 ){
138594        aPoslist = pList;
138595        nPoslist = nList;
138596
138597      }else{
138598        char *aOut = pList;
138599        char *p1 = aPoslist;
138600        char *p2 = aOut;
138601
138602        assert( iPrev>=0 );
138603        fts3PoslistPhraseMerge(&aOut, iToken-iPrev, 0, 1, &p1, &p2);
138604        sqlite3_free(aPoslist);
138605        aPoslist = pList;
138606        nPoslist = (int)(aOut - aPoslist);
138607        if( nPoslist==0 ){
138608          sqlite3_free(aPoslist);
138609          pPhrase->doclist.pList = 0;
138610          pPhrase->doclist.nList = 0;
138611          return SQLITE_OK;
138612        }
138613      }
138614      iPrev = iToken;
138615    }
138616  }
138617
138618  if( iPrev>=0 ){
138619    int nMaxUndeferred = pPhrase->iDoclistToken;
138620    if( nMaxUndeferred<0 ){
138621      pPhrase->doclist.pList = aPoslist;
138622      pPhrase->doclist.nList = nPoslist;
138623      pPhrase->doclist.iDocid = pCsr->iPrevId;
138624      pPhrase->doclist.bFreeList = 1;
138625    }else{
138626      int nDistance;
138627      char *p1;
138628      char *p2;
138629      char *aOut;
138630
138631      if( nMaxUndeferred>iPrev ){
138632        p1 = aPoslist;
138633        p2 = pPhrase->doclist.pList;
138634        nDistance = nMaxUndeferred - iPrev;
138635      }else{
138636        p1 = pPhrase->doclist.pList;
138637        p2 = aPoslist;
138638        nDistance = iPrev - nMaxUndeferred;
138639      }
138640
138641      aOut = (char *)sqlite3_malloc(nPoslist+8);
138642      if( !aOut ){
138643        sqlite3_free(aPoslist);
138644        return SQLITE_NOMEM;
138645      }
138646
138647      pPhrase->doclist.pList = aOut;
138648      if( fts3PoslistPhraseMerge(&aOut, nDistance, 0, 1, &p1, &p2) ){
138649        pPhrase->doclist.bFreeList = 1;
138650        pPhrase->doclist.nList = (int)(aOut - pPhrase->doclist.pList);
138651      }else{
138652        sqlite3_free(aOut);
138653        pPhrase->doclist.pList = 0;
138654        pPhrase->doclist.nList = 0;
138655      }
138656      sqlite3_free(aPoslist);
138657    }
138658  }
138659
138660  return SQLITE_OK;
138661}
138662
138663/*
138664** Maximum number of tokens a phrase may have to be considered for the
138665** incremental doclists strategy.
138666*/
138667#define MAX_INCR_PHRASE_TOKENS 4
138668
138669/*
138670** This function is called for each Fts3Phrase in a full-text query
138671** expression to initialize the mechanism for returning rows. Once this
138672** function has been called successfully on an Fts3Phrase, it may be
138673** used with fts3EvalPhraseNext() to iterate through the matching docids.
138674**
138675** If parameter bOptOk is true, then the phrase may (or may not) use the
138676** incremental loading strategy. Otherwise, the entire doclist is loaded into
138677** memory within this call.
138678**
138679** SQLITE_OK is returned if no error occurs, otherwise an SQLite error code.
138680*/
138681static int fts3EvalPhraseStart(Fts3Cursor *pCsr, int bOptOk, Fts3Phrase *p){
138682  Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
138683  int rc = SQLITE_OK;             /* Error code */
138684  int i;
138685
138686  /* Determine if doclists may be loaded from disk incrementally. This is
138687  ** possible if the bOptOk argument is true, the FTS doclists will be
138688  ** scanned in forward order, and the phrase consists of
138689  ** MAX_INCR_PHRASE_TOKENS or fewer tokens, none of which are are "^first"
138690  ** tokens or prefix tokens that cannot use a prefix-index.  */
138691  int bHaveIncr = 0;
138692  int bIncrOk = (bOptOk
138693   && pCsr->bDesc==pTab->bDescIdx
138694   && p->nToken<=MAX_INCR_PHRASE_TOKENS && p->nToken>0
138695#ifdef SQLITE_TEST
138696   && pTab->bNoIncrDoclist==0
138697#endif
138698  );
138699  for(i=0; bIncrOk==1 && i<p->nToken; i++){
138700    Fts3PhraseToken *pToken = &p->aToken[i];
138701    if( pToken->bFirst || (pToken->pSegcsr!=0 && !pToken->pSegcsr->bLookup) ){
138702      bIncrOk = 0;
138703    }
138704    if( pToken->pSegcsr ) bHaveIncr = 1;
138705  }
138706
138707  if( bIncrOk && bHaveIncr ){
138708    /* Use the incremental approach. */
138709    int iCol = (p->iColumn >= pTab->nColumn ? -1 : p->iColumn);
138710    for(i=0; rc==SQLITE_OK && i<p->nToken; i++){
138711      Fts3PhraseToken *pToken = &p->aToken[i];
138712      Fts3MultiSegReader *pSegcsr = pToken->pSegcsr;
138713      if( pSegcsr ){
138714        rc = sqlite3Fts3MsrIncrStart(pTab, pSegcsr, iCol, pToken->z, pToken->n);
138715      }
138716    }
138717    p->bIncr = 1;
138718  }else{
138719    /* Load the full doclist for the phrase into memory. */
138720    rc = fts3EvalPhraseLoad(pCsr, p);
138721    p->bIncr = 0;
138722  }
138723
138724  assert( rc!=SQLITE_OK || p->nToken<1 || p->aToken[0].pSegcsr==0 || p->bIncr );
138725  return rc;
138726}
138727
138728/*
138729** This function is used to iterate backwards (from the end to start)
138730** through doclists. It is used by this module to iterate through phrase
138731** doclists in reverse and by the fts3_write.c module to iterate through
138732** pending-terms lists when writing to databases with "order=desc".
138733**
138734** The doclist may be sorted in ascending (parameter bDescIdx==0) or
138735** descending (parameter bDescIdx==1) order of docid. Regardless, this
138736** function iterates from the end of the doclist to the beginning.
138737*/
138738SQLITE_PRIVATE void sqlite3Fts3DoclistPrev(
138739  int bDescIdx,                   /* True if the doclist is desc */
138740  char *aDoclist,                 /* Pointer to entire doclist */
138741  int nDoclist,                   /* Length of aDoclist in bytes */
138742  char **ppIter,                  /* IN/OUT: Iterator pointer */
138743  sqlite3_int64 *piDocid,         /* IN/OUT: Docid pointer */
138744  int *pnList,                    /* OUT: List length pointer */
138745  u8 *pbEof                       /* OUT: End-of-file flag */
138746){
138747  char *p = *ppIter;
138748
138749  assert( nDoclist>0 );
138750  assert( *pbEof==0 );
138751  assert( p || *piDocid==0 );
138752  assert( !p || (p>aDoclist && p<&aDoclist[nDoclist]) );
138753
138754  if( p==0 ){
138755    sqlite3_int64 iDocid = 0;
138756    char *pNext = 0;
138757    char *pDocid = aDoclist;
138758    char *pEnd = &aDoclist[nDoclist];
138759    int iMul = 1;
138760
138761    while( pDocid<pEnd ){
138762      sqlite3_int64 iDelta;
138763      pDocid += sqlite3Fts3GetVarint(pDocid, &iDelta);
138764      iDocid += (iMul * iDelta);
138765      pNext = pDocid;
138766      fts3PoslistCopy(0, &pDocid);
138767      while( pDocid<pEnd && *pDocid==0 ) pDocid++;
138768      iMul = (bDescIdx ? -1 : 1);
138769    }
138770
138771    *pnList = (int)(pEnd - pNext);
138772    *ppIter = pNext;
138773    *piDocid = iDocid;
138774  }else{
138775    int iMul = (bDescIdx ? -1 : 1);
138776    sqlite3_int64 iDelta;
138777    fts3GetReverseVarint(&p, aDoclist, &iDelta);
138778    *piDocid -= (iMul * iDelta);
138779
138780    if( p==aDoclist ){
138781      *pbEof = 1;
138782    }else{
138783      char *pSave = p;
138784      fts3ReversePoslist(aDoclist, &p);
138785      *pnList = (int)(pSave - p);
138786    }
138787    *ppIter = p;
138788  }
138789}
138790
138791/*
138792** Iterate forwards through a doclist.
138793*/
138794SQLITE_PRIVATE void sqlite3Fts3DoclistNext(
138795  int bDescIdx,                   /* True if the doclist is desc */
138796  char *aDoclist,                 /* Pointer to entire doclist */
138797  int nDoclist,                   /* Length of aDoclist in bytes */
138798  char **ppIter,                  /* IN/OUT: Iterator pointer */
138799  sqlite3_int64 *piDocid,         /* IN/OUT: Docid pointer */
138800  u8 *pbEof                       /* OUT: End-of-file flag */
138801){
138802  char *p = *ppIter;
138803
138804  assert( nDoclist>0 );
138805  assert( *pbEof==0 );
138806  assert( p || *piDocid==0 );
138807  assert( !p || (p>=aDoclist && p<=&aDoclist[nDoclist]) );
138808
138809  if( p==0 ){
138810    p = aDoclist;
138811    p += sqlite3Fts3GetVarint(p, piDocid);
138812  }else{
138813    fts3PoslistCopy(0, &p);
138814    while( p<&aDoclist[nDoclist] && *p==0 ) p++;
138815    if( p>=&aDoclist[nDoclist] ){
138816      *pbEof = 1;
138817    }else{
138818      sqlite3_int64 iVar;
138819      p += sqlite3Fts3GetVarint(p, &iVar);
138820      *piDocid += ((bDescIdx ? -1 : 1) * iVar);
138821    }
138822  }
138823
138824  *ppIter = p;
138825}
138826
138827/*
138828** Advance the iterator pDL to the next entry in pDL->aAll/nAll. Set *pbEof
138829** to true if EOF is reached.
138830*/
138831static void fts3EvalDlPhraseNext(
138832  Fts3Table *pTab,
138833  Fts3Doclist *pDL,
138834  u8 *pbEof
138835){
138836  char *pIter;                            /* Used to iterate through aAll */
138837  char *pEnd = &pDL->aAll[pDL->nAll];     /* 1 byte past end of aAll */
138838
138839  if( pDL->pNextDocid ){
138840    pIter = pDL->pNextDocid;
138841  }else{
138842    pIter = pDL->aAll;
138843  }
138844
138845  if( pIter>=pEnd ){
138846    /* We have already reached the end of this doclist. EOF. */
138847    *pbEof = 1;
138848  }else{
138849    sqlite3_int64 iDelta;
138850    pIter += sqlite3Fts3GetVarint(pIter, &iDelta);
138851    if( pTab->bDescIdx==0 || pDL->pNextDocid==0 ){
138852      pDL->iDocid += iDelta;
138853    }else{
138854      pDL->iDocid -= iDelta;
138855    }
138856    pDL->pList = pIter;
138857    fts3PoslistCopy(0, &pIter);
138858    pDL->nList = (int)(pIter - pDL->pList);
138859
138860    /* pIter now points just past the 0x00 that terminates the position-
138861    ** list for document pDL->iDocid. However, if this position-list was
138862    ** edited in place by fts3EvalNearTrim(), then pIter may not actually
138863    ** point to the start of the next docid value. The following line deals
138864    ** with this case by advancing pIter past the zero-padding added by
138865    ** fts3EvalNearTrim().  */
138866    while( pIter<pEnd && *pIter==0 ) pIter++;
138867
138868    pDL->pNextDocid = pIter;
138869    assert( pIter>=&pDL->aAll[pDL->nAll] || *pIter );
138870    *pbEof = 0;
138871  }
138872}
138873
138874/*
138875** Helper type used by fts3EvalIncrPhraseNext() and incrPhraseTokenNext().
138876*/
138877typedef struct TokenDoclist TokenDoclist;
138878struct TokenDoclist {
138879  int bIgnore;
138880  sqlite3_int64 iDocid;
138881  char *pList;
138882  int nList;
138883};
138884
138885/*
138886** Token pToken is an incrementally loaded token that is part of a
138887** multi-token phrase. Advance it to the next matching document in the
138888** database and populate output variable *p with the details of the new
138889** entry. Or, if the iterator has reached EOF, set *pbEof to true.
138890**
138891** If an error occurs, return an SQLite error code. Otherwise, return
138892** SQLITE_OK.
138893*/
138894static int incrPhraseTokenNext(
138895  Fts3Table *pTab,                /* Virtual table handle */
138896  Fts3Phrase *pPhrase,            /* Phrase to advance token of */
138897  int iToken,                     /* Specific token to advance */
138898  TokenDoclist *p,                /* OUT: Docid and doclist for new entry */
138899  u8 *pbEof                       /* OUT: True if iterator is at EOF */
138900){
138901  int rc = SQLITE_OK;
138902
138903  if( pPhrase->iDoclistToken==iToken ){
138904    assert( p->bIgnore==0 );
138905    assert( pPhrase->aToken[iToken].pSegcsr==0 );
138906    fts3EvalDlPhraseNext(pTab, &pPhrase->doclist, pbEof);
138907    p->pList = pPhrase->doclist.pList;
138908    p->nList = pPhrase->doclist.nList;
138909    p->iDocid = pPhrase->doclist.iDocid;
138910  }else{
138911    Fts3PhraseToken *pToken = &pPhrase->aToken[iToken];
138912    assert( pToken->pDeferred==0 );
138913    assert( pToken->pSegcsr || pPhrase->iDoclistToken>=0 );
138914    if( pToken->pSegcsr ){
138915      assert( p->bIgnore==0 );
138916      rc = sqlite3Fts3MsrIncrNext(
138917          pTab, pToken->pSegcsr, &p->iDocid, &p->pList, &p->nList
138918      );
138919      if( p->pList==0 ) *pbEof = 1;
138920    }else{
138921      p->bIgnore = 1;
138922    }
138923  }
138924
138925  return rc;
138926}
138927
138928
138929/*
138930** The phrase iterator passed as the second argument:
138931**
138932**   * features at least one token that uses an incremental doclist, and
138933**
138934**   * does not contain any deferred tokens.
138935**
138936** Advance it to the next matching documnent in the database and populate
138937** the Fts3Doclist.pList and nList fields.
138938**
138939** If there is no "next" entry and no error occurs, then *pbEof is set to
138940** 1 before returning. Otherwise, if no error occurs and the iterator is
138941** successfully advanced, *pbEof is set to 0.
138942**
138943** If an error occurs, return an SQLite error code. Otherwise, return
138944** SQLITE_OK.
138945*/
138946static int fts3EvalIncrPhraseNext(
138947  Fts3Cursor *pCsr,               /* FTS Cursor handle */
138948  Fts3Phrase *p,                  /* Phrase object to advance to next docid */
138949  u8 *pbEof                       /* OUT: Set to 1 if EOF */
138950){
138951  int rc = SQLITE_OK;
138952  Fts3Doclist *pDL = &p->doclist;
138953  Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
138954  u8 bEof = 0;
138955
138956  /* This is only called if it is guaranteed that the phrase has at least
138957  ** one incremental token. In which case the bIncr flag is set. */
138958  assert( p->bIncr==1 );
138959
138960  if( p->nToken==1 && p->bIncr ){
138961    rc = sqlite3Fts3MsrIncrNext(pTab, p->aToken[0].pSegcsr,
138962        &pDL->iDocid, &pDL->pList, &pDL->nList
138963    );
138964    if( pDL->pList==0 ) bEof = 1;
138965  }else{
138966    int bDescDoclist = pCsr->bDesc;
138967    struct TokenDoclist a[MAX_INCR_PHRASE_TOKENS];
138968
138969    memset(a, 0, sizeof(a));
138970    assert( p->nToken<=MAX_INCR_PHRASE_TOKENS );
138971    assert( p->iDoclistToken<MAX_INCR_PHRASE_TOKENS );
138972
138973    while( bEof==0 ){
138974      int bMaxSet = 0;
138975      sqlite3_int64 iMax = 0;     /* Largest docid for all iterators */
138976      int i;                      /* Used to iterate through tokens */
138977
138978      /* Advance the iterator for each token in the phrase once. */
138979      for(i=0; rc==SQLITE_OK && i<p->nToken && bEof==0; i++){
138980        rc = incrPhraseTokenNext(pTab, p, i, &a[i], &bEof);
138981        if( a[i].bIgnore==0 && (bMaxSet==0 || DOCID_CMP(iMax, a[i].iDocid)<0) ){
138982          iMax = a[i].iDocid;
138983          bMaxSet = 1;
138984        }
138985      }
138986      assert( rc!=SQLITE_OK || (p->nToken>=1 && a[p->nToken-1].bIgnore==0) );
138987      assert( rc!=SQLITE_OK || bMaxSet );
138988
138989      /* Keep advancing iterators until they all point to the same document */
138990      for(i=0; i<p->nToken; i++){
138991        while( rc==SQLITE_OK && bEof==0
138992            && a[i].bIgnore==0 && DOCID_CMP(a[i].iDocid, iMax)<0
138993        ){
138994          rc = incrPhraseTokenNext(pTab, p, i, &a[i], &bEof);
138995          if( DOCID_CMP(a[i].iDocid, iMax)>0 ){
138996            iMax = a[i].iDocid;
138997            i = 0;
138998          }
138999        }
139000      }
139001
139002      /* Check if the current entries really are a phrase match */
139003      if( bEof==0 ){
139004        int nList = 0;
139005        int nByte = a[p->nToken-1].nList;
139006        char *aDoclist = sqlite3_malloc(nByte+1);
139007        if( !aDoclist ) return SQLITE_NOMEM;
139008        memcpy(aDoclist, a[p->nToken-1].pList, nByte+1);
139009
139010        for(i=0; i<(p->nToken-1); i++){
139011          if( a[i].bIgnore==0 ){
139012            char *pL = a[i].pList;
139013            char *pR = aDoclist;
139014            char *pOut = aDoclist;
139015            int nDist = p->nToken-1-i;
139016            int res = fts3PoslistPhraseMerge(&pOut, nDist, 0, 1, &pL, &pR);
139017            if( res==0 ) break;
139018            nList = (int)(pOut - aDoclist);
139019          }
139020        }
139021        if( i==(p->nToken-1) ){
139022          pDL->iDocid = iMax;
139023          pDL->pList = aDoclist;
139024          pDL->nList = nList;
139025          pDL->bFreeList = 1;
139026          break;
139027        }
139028        sqlite3_free(aDoclist);
139029      }
139030    }
139031  }
139032
139033  *pbEof = bEof;
139034  return rc;
139035}
139036
139037/*
139038** Attempt to move the phrase iterator to point to the next matching docid.
139039** If an error occurs, return an SQLite error code. Otherwise, return
139040** SQLITE_OK.
139041**
139042** If there is no "next" entry and no error occurs, then *pbEof is set to
139043** 1 before returning. Otherwise, if no error occurs and the iterator is
139044** successfully advanced, *pbEof is set to 0.
139045*/
139046static int fts3EvalPhraseNext(
139047  Fts3Cursor *pCsr,               /* FTS Cursor handle */
139048  Fts3Phrase *p,                  /* Phrase object to advance to next docid */
139049  u8 *pbEof                       /* OUT: Set to 1 if EOF */
139050){
139051  int rc = SQLITE_OK;
139052  Fts3Doclist *pDL = &p->doclist;
139053  Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
139054
139055  if( p->bIncr ){
139056    rc = fts3EvalIncrPhraseNext(pCsr, p, pbEof);
139057  }else if( pCsr->bDesc!=pTab->bDescIdx && pDL->nAll ){
139058    sqlite3Fts3DoclistPrev(pTab->bDescIdx, pDL->aAll, pDL->nAll,
139059        &pDL->pNextDocid, &pDL->iDocid, &pDL->nList, pbEof
139060    );
139061    pDL->pList = pDL->pNextDocid;
139062  }else{
139063    fts3EvalDlPhraseNext(pTab, pDL, pbEof);
139064  }
139065
139066  return rc;
139067}
139068
139069/*
139070**
139071** If *pRc is not SQLITE_OK when this function is called, it is a no-op.
139072** Otherwise, fts3EvalPhraseStart() is called on all phrases within the
139073** expression. Also the Fts3Expr.bDeferred variable is set to true for any
139074** expressions for which all descendent tokens are deferred.
139075**
139076** If parameter bOptOk is zero, then it is guaranteed that the
139077** Fts3Phrase.doclist.aAll/nAll variables contain the entire doclist for
139078** each phrase in the expression (subject to deferred token processing).
139079** Or, if bOptOk is non-zero, then one or more tokens within the expression
139080** may be loaded incrementally, meaning doclist.aAll/nAll is not available.
139081**
139082** If an error occurs within this function, *pRc is set to an SQLite error
139083** code before returning.
139084*/
139085static void fts3EvalStartReaders(
139086  Fts3Cursor *pCsr,               /* FTS Cursor handle */
139087  Fts3Expr *pExpr,                /* Expression to initialize phrases in */
139088  int *pRc                        /* IN/OUT: Error code */
139089){
139090  if( pExpr && SQLITE_OK==*pRc ){
139091    if( pExpr->eType==FTSQUERY_PHRASE ){
139092      int nToken = pExpr->pPhrase->nToken;
139093      if( nToken ){
139094        int i;
139095        for(i=0; i<nToken; i++){
139096          if( pExpr->pPhrase->aToken[i].pDeferred==0 ) break;
139097        }
139098        pExpr->bDeferred = (i==nToken);
139099      }
139100      *pRc = fts3EvalPhraseStart(pCsr, 1, pExpr->pPhrase);
139101    }else{
139102      fts3EvalStartReaders(pCsr, pExpr->pLeft, pRc);
139103      fts3EvalStartReaders(pCsr, pExpr->pRight, pRc);
139104      pExpr->bDeferred = (pExpr->pLeft->bDeferred && pExpr->pRight->bDeferred);
139105    }
139106  }
139107}
139108
139109/*
139110** An array of the following structures is assembled as part of the process
139111** of selecting tokens to defer before the query starts executing (as part
139112** of the xFilter() method). There is one element in the array for each
139113** token in the FTS expression.
139114**
139115** Tokens are divided into AND/NEAR clusters. All tokens in a cluster belong
139116** to phrases that are connected only by AND and NEAR operators (not OR or
139117** NOT). When determining tokens to defer, each AND/NEAR cluster is considered
139118** separately. The root of a tokens AND/NEAR cluster is stored in
139119** Fts3TokenAndCost.pRoot.
139120*/
139121typedef struct Fts3TokenAndCost Fts3TokenAndCost;
139122struct Fts3TokenAndCost {
139123  Fts3Phrase *pPhrase;            /* The phrase the token belongs to */
139124  int iToken;                     /* Position of token in phrase */
139125  Fts3PhraseToken *pToken;        /* The token itself */
139126  Fts3Expr *pRoot;                /* Root of NEAR/AND cluster */
139127  int nOvfl;                      /* Number of overflow pages to load doclist */
139128  int iCol;                       /* The column the token must match */
139129};
139130
139131/*
139132** This function is used to populate an allocated Fts3TokenAndCost array.
139133**
139134** If *pRc is not SQLITE_OK when this function is called, it is a no-op.
139135** Otherwise, if an error occurs during execution, *pRc is set to an
139136** SQLite error code.
139137*/
139138static void fts3EvalTokenCosts(
139139  Fts3Cursor *pCsr,               /* FTS Cursor handle */
139140  Fts3Expr *pRoot,                /* Root of current AND/NEAR cluster */
139141  Fts3Expr *pExpr,                /* Expression to consider */
139142  Fts3TokenAndCost **ppTC,        /* Write new entries to *(*ppTC)++ */
139143  Fts3Expr ***ppOr,               /* Write new OR root to *(*ppOr)++ */
139144  int *pRc                        /* IN/OUT: Error code */
139145){
139146  if( *pRc==SQLITE_OK ){
139147    if( pExpr->eType==FTSQUERY_PHRASE ){
139148      Fts3Phrase *pPhrase = pExpr->pPhrase;
139149      int i;
139150      for(i=0; *pRc==SQLITE_OK && i<pPhrase->nToken; i++){
139151        Fts3TokenAndCost *pTC = (*ppTC)++;
139152        pTC->pPhrase = pPhrase;
139153        pTC->iToken = i;
139154        pTC->pRoot = pRoot;
139155        pTC->pToken = &pPhrase->aToken[i];
139156        pTC->iCol = pPhrase->iColumn;
139157        *pRc = sqlite3Fts3MsrOvfl(pCsr, pTC->pToken->pSegcsr, &pTC->nOvfl);
139158      }
139159    }else if( pExpr->eType!=FTSQUERY_NOT ){
139160      assert( pExpr->eType==FTSQUERY_OR
139161           || pExpr->eType==FTSQUERY_AND
139162           || pExpr->eType==FTSQUERY_NEAR
139163      );
139164      assert( pExpr->pLeft && pExpr->pRight );
139165      if( pExpr->eType==FTSQUERY_OR ){
139166        pRoot = pExpr->pLeft;
139167        **ppOr = pRoot;
139168        (*ppOr)++;
139169      }
139170      fts3EvalTokenCosts(pCsr, pRoot, pExpr->pLeft, ppTC, ppOr, pRc);
139171      if( pExpr->eType==FTSQUERY_OR ){
139172        pRoot = pExpr->pRight;
139173        **ppOr = pRoot;
139174        (*ppOr)++;
139175      }
139176      fts3EvalTokenCosts(pCsr, pRoot, pExpr->pRight, ppTC, ppOr, pRc);
139177    }
139178  }
139179}
139180
139181/*
139182** Determine the average document (row) size in pages. If successful,
139183** write this value to *pnPage and return SQLITE_OK. Otherwise, return
139184** an SQLite error code.
139185**
139186** The average document size in pages is calculated by first calculating
139187** determining the average size in bytes, B. If B is less than the amount
139188** of data that will fit on a single leaf page of an intkey table in
139189** this database, then the average docsize is 1. Otherwise, it is 1 plus
139190** the number of overflow pages consumed by a record B bytes in size.
139191*/
139192static int fts3EvalAverageDocsize(Fts3Cursor *pCsr, int *pnPage){
139193  if( pCsr->nRowAvg==0 ){
139194    /* The average document size, which is required to calculate the cost
139195    ** of each doclist, has not yet been determined. Read the required
139196    ** data from the %_stat table to calculate it.
139197    **
139198    ** Entry 0 of the %_stat table is a blob containing (nCol+1) FTS3
139199    ** varints, where nCol is the number of columns in the FTS3 table.
139200    ** The first varint is the number of documents currently stored in
139201    ** the table. The following nCol varints contain the total amount of
139202    ** data stored in all rows of each column of the table, from left
139203    ** to right.
139204    */
139205    int rc;
139206    Fts3Table *p = (Fts3Table*)pCsr->base.pVtab;
139207    sqlite3_stmt *pStmt;
139208    sqlite3_int64 nDoc = 0;
139209    sqlite3_int64 nByte = 0;
139210    const char *pEnd;
139211    const char *a;
139212
139213    rc = sqlite3Fts3SelectDoctotal(p, &pStmt);
139214    if( rc!=SQLITE_OK ) return rc;
139215    a = sqlite3_column_blob(pStmt, 0);
139216    assert( a );
139217
139218    pEnd = &a[sqlite3_column_bytes(pStmt, 0)];
139219    a += sqlite3Fts3GetVarint(a, &nDoc);
139220    while( a<pEnd ){
139221      a += sqlite3Fts3GetVarint(a, &nByte);
139222    }
139223    if( nDoc==0 || nByte==0 ){
139224      sqlite3_reset(pStmt);
139225      return FTS_CORRUPT_VTAB;
139226    }
139227
139228    pCsr->nDoc = nDoc;
139229    pCsr->nRowAvg = (int)(((nByte / nDoc) + p->nPgsz) / p->nPgsz);
139230    assert( pCsr->nRowAvg>0 );
139231    rc = sqlite3_reset(pStmt);
139232    if( rc!=SQLITE_OK ) return rc;
139233  }
139234
139235  *pnPage = pCsr->nRowAvg;
139236  return SQLITE_OK;
139237}
139238
139239/*
139240** This function is called to select the tokens (if any) that will be
139241** deferred. The array aTC[] has already been populated when this is
139242** called.
139243**
139244** This function is called once for each AND/NEAR cluster in the
139245** expression. Each invocation determines which tokens to defer within
139246** the cluster with root node pRoot. See comments above the definition
139247** of struct Fts3TokenAndCost for more details.
139248**
139249** If no error occurs, SQLITE_OK is returned and sqlite3Fts3DeferToken()
139250** called on each token to defer. Otherwise, an SQLite error code is
139251** returned.
139252*/
139253static int fts3EvalSelectDeferred(
139254  Fts3Cursor *pCsr,               /* FTS Cursor handle */
139255  Fts3Expr *pRoot,                /* Consider tokens with this root node */
139256  Fts3TokenAndCost *aTC,          /* Array of expression tokens and costs */
139257  int nTC                         /* Number of entries in aTC[] */
139258){
139259  Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
139260  int nDocSize = 0;               /* Number of pages per doc loaded */
139261  int rc = SQLITE_OK;             /* Return code */
139262  int ii;                         /* Iterator variable for various purposes */
139263  int nOvfl = 0;                  /* Total overflow pages used by doclists */
139264  int nToken = 0;                 /* Total number of tokens in cluster */
139265
139266  int nMinEst = 0;                /* The minimum count for any phrase so far. */
139267  int nLoad4 = 1;                 /* (Phrases that will be loaded)^4. */
139268
139269  /* Tokens are never deferred for FTS tables created using the content=xxx
139270  ** option. The reason being that it is not guaranteed that the content
139271  ** table actually contains the same data as the index. To prevent this from
139272  ** causing any problems, the deferred token optimization is completely
139273  ** disabled for content=xxx tables. */
139274  if( pTab->zContentTbl ){
139275    return SQLITE_OK;
139276  }
139277
139278  /* Count the tokens in this AND/NEAR cluster. If none of the doclists
139279  ** associated with the tokens spill onto overflow pages, or if there is
139280  ** only 1 token, exit early. No tokens to defer in this case. */
139281  for(ii=0; ii<nTC; ii++){
139282    if( aTC[ii].pRoot==pRoot ){
139283      nOvfl += aTC[ii].nOvfl;
139284      nToken++;
139285    }
139286  }
139287  if( nOvfl==0 || nToken<2 ) return SQLITE_OK;
139288
139289  /* Obtain the average docsize (in pages). */
139290  rc = fts3EvalAverageDocsize(pCsr, &nDocSize);
139291  assert( rc!=SQLITE_OK || nDocSize>0 );
139292
139293
139294  /* Iterate through all tokens in this AND/NEAR cluster, in ascending order
139295  ** of the number of overflow pages that will be loaded by the pager layer
139296  ** to retrieve the entire doclist for the token from the full-text index.
139297  ** Load the doclists for tokens that are either:
139298  **
139299  **   a. The cheapest token in the entire query (i.e. the one visited by the
139300  **      first iteration of this loop), or
139301  **
139302  **   b. Part of a multi-token phrase.
139303  **
139304  ** After each token doclist is loaded, merge it with the others from the
139305  ** same phrase and count the number of documents that the merged doclist
139306  ** contains. Set variable "nMinEst" to the smallest number of documents in
139307  ** any phrase doclist for which 1 or more token doclists have been loaded.
139308  ** Let nOther be the number of other phrases for which it is certain that
139309  ** one or more tokens will not be deferred.
139310  **
139311  ** Then, for each token, defer it if loading the doclist would result in
139312  ** loading N or more overflow pages into memory, where N is computed as:
139313  **
139314  **    (nMinEst + 4^nOther - 1) / (4^nOther)
139315  */
139316  for(ii=0; ii<nToken && rc==SQLITE_OK; ii++){
139317    int iTC;                      /* Used to iterate through aTC[] array. */
139318    Fts3TokenAndCost *pTC = 0;    /* Set to cheapest remaining token. */
139319
139320    /* Set pTC to point to the cheapest remaining token. */
139321    for(iTC=0; iTC<nTC; iTC++){
139322      if( aTC[iTC].pToken && aTC[iTC].pRoot==pRoot
139323       && (!pTC || aTC[iTC].nOvfl<pTC->nOvfl)
139324      ){
139325        pTC = &aTC[iTC];
139326      }
139327    }
139328    assert( pTC );
139329
139330    if( ii && pTC->nOvfl>=((nMinEst+(nLoad4/4)-1)/(nLoad4/4))*nDocSize ){
139331      /* The number of overflow pages to load for this (and therefore all
139332      ** subsequent) tokens is greater than the estimated number of pages
139333      ** that will be loaded if all subsequent tokens are deferred.
139334      */
139335      Fts3PhraseToken *pToken = pTC->pToken;
139336      rc = sqlite3Fts3DeferToken(pCsr, pToken, pTC->iCol);
139337      fts3SegReaderCursorFree(pToken->pSegcsr);
139338      pToken->pSegcsr = 0;
139339    }else{
139340      /* Set nLoad4 to the value of (4^nOther) for the next iteration of the
139341      ** for-loop. Except, limit the value to 2^24 to prevent it from
139342      ** overflowing the 32-bit integer it is stored in. */
139343      if( ii<12 ) nLoad4 = nLoad4*4;
139344
139345      if( ii==0 || (pTC->pPhrase->nToken>1 && ii!=nToken-1) ){
139346        /* Either this is the cheapest token in the entire query, or it is
139347        ** part of a multi-token phrase. Either way, the entire doclist will
139348        ** (eventually) be loaded into memory. It may as well be now. */
139349        Fts3PhraseToken *pToken = pTC->pToken;
139350        int nList = 0;
139351        char *pList = 0;
139352        rc = fts3TermSelect(pTab, pToken, pTC->iCol, &nList, &pList);
139353        assert( rc==SQLITE_OK || pList==0 );
139354        if( rc==SQLITE_OK ){
139355          rc = fts3EvalPhraseMergeToken(
139356              pTab, pTC->pPhrase, pTC->iToken,pList,nList
139357          );
139358        }
139359        if( rc==SQLITE_OK ){
139360          int nCount;
139361          nCount = fts3DoclistCountDocids(
139362              pTC->pPhrase->doclist.aAll, pTC->pPhrase->doclist.nAll
139363          );
139364          if( ii==0 || nCount<nMinEst ) nMinEst = nCount;
139365        }
139366      }
139367    }
139368    pTC->pToken = 0;
139369  }
139370
139371  return rc;
139372}
139373
139374/*
139375** This function is called from within the xFilter method. It initializes
139376** the full-text query currently stored in pCsr->pExpr. To iterate through
139377** the results of a query, the caller does:
139378**
139379**    fts3EvalStart(pCsr);
139380**    while( 1 ){
139381**      fts3EvalNext(pCsr);
139382**      if( pCsr->bEof ) break;
139383**      ... return row pCsr->iPrevId to the caller ...
139384**    }
139385*/
139386static int fts3EvalStart(Fts3Cursor *pCsr){
139387  Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
139388  int rc = SQLITE_OK;
139389  int nToken = 0;
139390  int nOr = 0;
139391
139392  /* Allocate a MultiSegReader for each token in the expression. */
139393  fts3EvalAllocateReaders(pCsr, pCsr->pExpr, &nToken, &nOr, &rc);
139394
139395  /* Determine which, if any, tokens in the expression should be deferred. */
139396#ifndef SQLITE_DISABLE_FTS4_DEFERRED
139397  if( rc==SQLITE_OK && nToken>1 && pTab->bFts4 ){
139398    Fts3TokenAndCost *aTC;
139399    Fts3Expr **apOr;
139400    aTC = (Fts3TokenAndCost *)sqlite3_malloc(
139401        sizeof(Fts3TokenAndCost) * nToken
139402      + sizeof(Fts3Expr *) * nOr * 2
139403    );
139404    apOr = (Fts3Expr **)&aTC[nToken];
139405
139406    if( !aTC ){
139407      rc = SQLITE_NOMEM;
139408    }else{
139409      int ii;
139410      Fts3TokenAndCost *pTC = aTC;
139411      Fts3Expr **ppOr = apOr;
139412
139413      fts3EvalTokenCosts(pCsr, 0, pCsr->pExpr, &pTC, &ppOr, &rc);
139414      nToken = (int)(pTC-aTC);
139415      nOr = (int)(ppOr-apOr);
139416
139417      if( rc==SQLITE_OK ){
139418        rc = fts3EvalSelectDeferred(pCsr, 0, aTC, nToken);
139419        for(ii=0; rc==SQLITE_OK && ii<nOr; ii++){
139420          rc = fts3EvalSelectDeferred(pCsr, apOr[ii], aTC, nToken);
139421        }
139422      }
139423
139424      sqlite3_free(aTC);
139425    }
139426  }
139427#endif
139428
139429  fts3EvalStartReaders(pCsr, pCsr->pExpr, &rc);
139430  return rc;
139431}
139432
139433/*
139434** Invalidate the current position list for phrase pPhrase.
139435*/
139436static void fts3EvalInvalidatePoslist(Fts3Phrase *pPhrase){
139437  if( pPhrase->doclist.bFreeList ){
139438    sqlite3_free(pPhrase->doclist.pList);
139439  }
139440  pPhrase->doclist.pList = 0;
139441  pPhrase->doclist.nList = 0;
139442  pPhrase->doclist.bFreeList = 0;
139443}
139444
139445/*
139446** This function is called to edit the position list associated with
139447** the phrase object passed as the fifth argument according to a NEAR
139448** condition. For example:
139449**
139450**     abc NEAR/5 "def ghi"
139451**
139452** Parameter nNear is passed the NEAR distance of the expression (5 in
139453** the example above). When this function is called, *paPoslist points to
139454** the position list, and *pnToken is the number of phrase tokens in, the
139455** phrase on the other side of the NEAR operator to pPhrase. For example,
139456** if pPhrase refers to the "def ghi" phrase, then *paPoslist points to
139457** the position list associated with phrase "abc".
139458**
139459** All positions in the pPhrase position list that are not sufficiently
139460** close to a position in the *paPoslist position list are removed. If this
139461** leaves 0 positions, zero is returned. Otherwise, non-zero.
139462**
139463** Before returning, *paPoslist is set to point to the position lsit
139464** associated with pPhrase. And *pnToken is set to the number of tokens in
139465** pPhrase.
139466*/
139467static int fts3EvalNearTrim(
139468  int nNear,                      /* NEAR distance. As in "NEAR/nNear". */
139469  char *aTmp,                     /* Temporary space to use */
139470  char **paPoslist,               /* IN/OUT: Position list */
139471  int *pnToken,                   /* IN/OUT: Tokens in phrase of *paPoslist */
139472  Fts3Phrase *pPhrase             /* The phrase object to trim the doclist of */
139473){
139474  int nParam1 = nNear + pPhrase->nToken;
139475  int nParam2 = nNear + *pnToken;
139476  int nNew;
139477  char *p2;
139478  char *pOut;
139479  int res;
139480
139481  assert( pPhrase->doclist.pList );
139482
139483  p2 = pOut = pPhrase->doclist.pList;
139484  res = fts3PoslistNearMerge(
139485    &pOut, aTmp, nParam1, nParam2, paPoslist, &p2
139486  );
139487  if( res ){
139488    nNew = (int)(pOut - pPhrase->doclist.pList) - 1;
139489    assert( pPhrase->doclist.pList[nNew]=='\0' );
139490    assert( nNew<=pPhrase->doclist.nList && nNew>0 );
139491    memset(&pPhrase->doclist.pList[nNew], 0, pPhrase->doclist.nList - nNew);
139492    pPhrase->doclist.nList = nNew;
139493    *paPoslist = pPhrase->doclist.pList;
139494    *pnToken = pPhrase->nToken;
139495  }
139496
139497  return res;
139498}
139499
139500/*
139501** This function is a no-op if *pRc is other than SQLITE_OK when it is called.
139502** Otherwise, it advances the expression passed as the second argument to
139503** point to the next matching row in the database. Expressions iterate through
139504** matching rows in docid order. Ascending order if Fts3Cursor.bDesc is zero,
139505** or descending if it is non-zero.
139506**
139507** If an error occurs, *pRc is set to an SQLite error code. Otherwise, if
139508** successful, the following variables in pExpr are set:
139509**
139510**   Fts3Expr.bEof                (non-zero if EOF - there is no next row)
139511**   Fts3Expr.iDocid              (valid if bEof==0. The docid of the next row)
139512**
139513** If the expression is of type FTSQUERY_PHRASE, and the expression is not
139514** at EOF, then the following variables are populated with the position list
139515** for the phrase for the visited row:
139516**
139517**   FTs3Expr.pPhrase->doclist.nList        (length of pList in bytes)
139518**   FTs3Expr.pPhrase->doclist.pList        (pointer to position list)
139519**
139520** It says above that this function advances the expression to the next
139521** matching row. This is usually true, but there are the following exceptions:
139522**
139523**   1. Deferred tokens are not taken into account. If a phrase consists
139524**      entirely of deferred tokens, it is assumed to match every row in
139525**      the db. In this case the position-list is not populated at all.
139526**
139527**      Or, if a phrase contains one or more deferred tokens and one or
139528**      more non-deferred tokens, then the expression is advanced to the
139529**      next possible match, considering only non-deferred tokens. In other
139530**      words, if the phrase is "A B C", and "B" is deferred, the expression
139531**      is advanced to the next row that contains an instance of "A * C",
139532**      where "*" may match any single token. The position list in this case
139533**      is populated as for "A * C" before returning.
139534**
139535**   2. NEAR is treated as AND. If the expression is "x NEAR y", it is
139536**      advanced to point to the next row that matches "x AND y".
139537**
139538** See sqlite3Fts3EvalTestDeferred() for details on testing if a row is
139539** really a match, taking into account deferred tokens and NEAR operators.
139540*/
139541static void fts3EvalNextRow(
139542  Fts3Cursor *pCsr,               /* FTS Cursor handle */
139543  Fts3Expr *pExpr,                /* Expr. to advance to next matching row */
139544  int *pRc                        /* IN/OUT: Error code */
139545){
139546  if( *pRc==SQLITE_OK ){
139547    int bDescDoclist = pCsr->bDesc;         /* Used by DOCID_CMP() macro */
139548    assert( pExpr->bEof==0 );
139549    pExpr->bStart = 1;
139550
139551    switch( pExpr->eType ){
139552      case FTSQUERY_NEAR:
139553      case FTSQUERY_AND: {
139554        Fts3Expr *pLeft = pExpr->pLeft;
139555        Fts3Expr *pRight = pExpr->pRight;
139556        assert( !pLeft->bDeferred || !pRight->bDeferred );
139557
139558        if( pLeft->bDeferred ){
139559          /* LHS is entirely deferred. So we assume it matches every row.
139560          ** Advance the RHS iterator to find the next row visited. */
139561          fts3EvalNextRow(pCsr, pRight, pRc);
139562          pExpr->iDocid = pRight->iDocid;
139563          pExpr->bEof = pRight->bEof;
139564        }else if( pRight->bDeferred ){
139565          /* RHS is entirely deferred. So we assume it matches every row.
139566          ** Advance the LHS iterator to find the next row visited. */
139567          fts3EvalNextRow(pCsr, pLeft, pRc);
139568          pExpr->iDocid = pLeft->iDocid;
139569          pExpr->bEof = pLeft->bEof;
139570        }else{
139571          /* Neither the RHS or LHS are deferred. */
139572          fts3EvalNextRow(pCsr, pLeft, pRc);
139573          fts3EvalNextRow(pCsr, pRight, pRc);
139574          while( !pLeft->bEof && !pRight->bEof && *pRc==SQLITE_OK ){
139575            sqlite3_int64 iDiff = DOCID_CMP(pLeft->iDocid, pRight->iDocid);
139576            if( iDiff==0 ) break;
139577            if( iDiff<0 ){
139578              fts3EvalNextRow(pCsr, pLeft, pRc);
139579            }else{
139580              fts3EvalNextRow(pCsr, pRight, pRc);
139581            }
139582          }
139583          pExpr->iDocid = pLeft->iDocid;
139584          pExpr->bEof = (pLeft->bEof || pRight->bEof);
139585          if( pExpr->eType==FTSQUERY_NEAR && pExpr->bEof ){
139586            if( pRight->pPhrase && pRight->pPhrase->doclist.aAll ){
139587              Fts3Doclist *pDl = &pRight->pPhrase->doclist;
139588              while( *pRc==SQLITE_OK && pRight->bEof==0 ){
139589                memset(pDl->pList, 0, pDl->nList);
139590                fts3EvalNextRow(pCsr, pRight, pRc);
139591              }
139592            }
139593            if( pLeft->pPhrase && pLeft->pPhrase->doclist.aAll ){
139594              Fts3Doclist *pDl = &pLeft->pPhrase->doclist;
139595              while( *pRc==SQLITE_OK && pLeft->bEof==0 ){
139596                memset(pDl->pList, 0, pDl->nList);
139597                fts3EvalNextRow(pCsr, pLeft, pRc);
139598              }
139599            }
139600          }
139601        }
139602        break;
139603      }
139604
139605      case FTSQUERY_OR: {
139606        Fts3Expr *pLeft = pExpr->pLeft;
139607        Fts3Expr *pRight = pExpr->pRight;
139608        sqlite3_int64 iCmp = DOCID_CMP(pLeft->iDocid, pRight->iDocid);
139609
139610        assert( pLeft->bStart || pLeft->iDocid==pRight->iDocid );
139611        assert( pRight->bStart || pLeft->iDocid==pRight->iDocid );
139612
139613        if( pRight->bEof || (pLeft->bEof==0 && iCmp<0) ){
139614          fts3EvalNextRow(pCsr, pLeft, pRc);
139615        }else if( pLeft->bEof || (pRight->bEof==0 && iCmp>0) ){
139616          fts3EvalNextRow(pCsr, pRight, pRc);
139617        }else{
139618          fts3EvalNextRow(pCsr, pLeft, pRc);
139619          fts3EvalNextRow(pCsr, pRight, pRc);
139620        }
139621
139622        pExpr->bEof = (pLeft->bEof && pRight->bEof);
139623        iCmp = DOCID_CMP(pLeft->iDocid, pRight->iDocid);
139624        if( pRight->bEof || (pLeft->bEof==0 &&  iCmp<0) ){
139625          pExpr->iDocid = pLeft->iDocid;
139626        }else{
139627          pExpr->iDocid = pRight->iDocid;
139628        }
139629
139630        break;
139631      }
139632
139633      case FTSQUERY_NOT: {
139634        Fts3Expr *pLeft = pExpr->pLeft;
139635        Fts3Expr *pRight = pExpr->pRight;
139636
139637        if( pRight->bStart==0 ){
139638          fts3EvalNextRow(pCsr, pRight, pRc);
139639          assert( *pRc!=SQLITE_OK || pRight->bStart );
139640        }
139641
139642        fts3EvalNextRow(pCsr, pLeft, pRc);
139643        if( pLeft->bEof==0 ){
139644          while( !*pRc
139645              && !pRight->bEof
139646              && DOCID_CMP(pLeft->iDocid, pRight->iDocid)>0
139647          ){
139648            fts3EvalNextRow(pCsr, pRight, pRc);
139649          }
139650        }
139651        pExpr->iDocid = pLeft->iDocid;
139652        pExpr->bEof = pLeft->bEof;
139653        break;
139654      }
139655
139656      default: {
139657        Fts3Phrase *pPhrase = pExpr->pPhrase;
139658        fts3EvalInvalidatePoslist(pPhrase);
139659        *pRc = fts3EvalPhraseNext(pCsr, pPhrase, &pExpr->bEof);
139660        pExpr->iDocid = pPhrase->doclist.iDocid;
139661        break;
139662      }
139663    }
139664  }
139665}
139666
139667/*
139668** If *pRc is not SQLITE_OK, or if pExpr is not the root node of a NEAR
139669** cluster, then this function returns 1 immediately.
139670**
139671** Otherwise, it checks if the current row really does match the NEAR
139672** expression, using the data currently stored in the position lists
139673** (Fts3Expr->pPhrase.doclist.pList/nList) for each phrase in the expression.
139674**
139675** If the current row is a match, the position list associated with each
139676** phrase in the NEAR expression is edited in place to contain only those
139677** phrase instances sufficiently close to their peers to satisfy all NEAR
139678** constraints. In this case it returns 1. If the NEAR expression does not
139679** match the current row, 0 is returned. The position lists may or may not
139680** be edited if 0 is returned.
139681*/
139682static int fts3EvalNearTest(Fts3Expr *pExpr, int *pRc){
139683  int res = 1;
139684
139685  /* The following block runs if pExpr is the root of a NEAR query.
139686  ** For example, the query:
139687  **
139688  **         "w" NEAR "x" NEAR "y" NEAR "z"
139689  **
139690  ** which is represented in tree form as:
139691  **
139692  **                               |
139693  **                          +--NEAR--+      <-- root of NEAR query
139694  **                          |        |
139695  **                     +--NEAR--+   "z"
139696  **                     |        |
139697  **                +--NEAR--+   "y"
139698  **                |        |
139699  **               "w"      "x"
139700  **
139701  ** The right-hand child of a NEAR node is always a phrase. The
139702  ** left-hand child may be either a phrase or a NEAR node. There are
139703  ** no exceptions to this - it's the way the parser in fts3_expr.c works.
139704  */
139705  if( *pRc==SQLITE_OK
139706   && pExpr->eType==FTSQUERY_NEAR
139707   && pExpr->bEof==0
139708   && (pExpr->pParent==0 || pExpr->pParent->eType!=FTSQUERY_NEAR)
139709  ){
139710    Fts3Expr *p;
139711    int nTmp = 0;                 /* Bytes of temp space */
139712    char *aTmp;                   /* Temp space for PoslistNearMerge() */
139713
139714    /* Allocate temporary working space. */
139715    for(p=pExpr; p->pLeft; p=p->pLeft){
139716      nTmp += p->pRight->pPhrase->doclist.nList;
139717    }
139718    nTmp += p->pPhrase->doclist.nList;
139719    if( nTmp==0 ){
139720      res = 0;
139721    }else{
139722      aTmp = sqlite3_malloc(nTmp*2);
139723      if( !aTmp ){
139724        *pRc = SQLITE_NOMEM;
139725        res = 0;
139726      }else{
139727        char *aPoslist = p->pPhrase->doclist.pList;
139728        int nToken = p->pPhrase->nToken;
139729
139730        for(p=p->pParent;res && p && p->eType==FTSQUERY_NEAR; p=p->pParent){
139731          Fts3Phrase *pPhrase = p->pRight->pPhrase;
139732          int nNear = p->nNear;
139733          res = fts3EvalNearTrim(nNear, aTmp, &aPoslist, &nToken, pPhrase);
139734        }
139735
139736        aPoslist = pExpr->pRight->pPhrase->doclist.pList;
139737        nToken = pExpr->pRight->pPhrase->nToken;
139738        for(p=pExpr->pLeft; p && res; p=p->pLeft){
139739          int nNear;
139740          Fts3Phrase *pPhrase;
139741          assert( p->pParent && p->pParent->pLeft==p );
139742          nNear = p->pParent->nNear;
139743          pPhrase = (
139744              p->eType==FTSQUERY_NEAR ? p->pRight->pPhrase : p->pPhrase
139745              );
139746          res = fts3EvalNearTrim(nNear, aTmp, &aPoslist, &nToken, pPhrase);
139747        }
139748      }
139749
139750      sqlite3_free(aTmp);
139751    }
139752  }
139753
139754  return res;
139755}
139756
139757/*
139758** This function is a helper function for sqlite3Fts3EvalTestDeferred().
139759** Assuming no error occurs or has occurred, It returns non-zero if the
139760** expression passed as the second argument matches the row that pCsr
139761** currently points to, or zero if it does not.
139762**
139763** If *pRc is not SQLITE_OK when this function is called, it is a no-op.
139764** If an error occurs during execution of this function, *pRc is set to
139765** the appropriate SQLite error code. In this case the returned value is
139766** undefined.
139767*/
139768static int fts3EvalTestExpr(
139769  Fts3Cursor *pCsr,               /* FTS cursor handle */
139770  Fts3Expr *pExpr,                /* Expr to test. May or may not be root. */
139771  int *pRc                        /* IN/OUT: Error code */
139772){
139773  int bHit = 1;                   /* Return value */
139774  if( *pRc==SQLITE_OK ){
139775    switch( pExpr->eType ){
139776      case FTSQUERY_NEAR:
139777      case FTSQUERY_AND:
139778        bHit = (
139779            fts3EvalTestExpr(pCsr, pExpr->pLeft, pRc)
139780         && fts3EvalTestExpr(pCsr, pExpr->pRight, pRc)
139781         && fts3EvalNearTest(pExpr, pRc)
139782        );
139783
139784        /* If the NEAR expression does not match any rows, zero the doclist for
139785        ** all phrases involved in the NEAR. This is because the snippet(),
139786        ** offsets() and matchinfo() functions are not supposed to recognize
139787        ** any instances of phrases that are part of unmatched NEAR queries.
139788        ** For example if this expression:
139789        **
139790        **    ... MATCH 'a OR (b NEAR c)'
139791        **
139792        ** is matched against a row containing:
139793        **
139794        **        'a b d e'
139795        **
139796        ** then any snippet() should ony highlight the "a" term, not the "b"
139797        ** (as "b" is part of a non-matching NEAR clause).
139798        */
139799        if( bHit==0
139800         && pExpr->eType==FTSQUERY_NEAR
139801         && (pExpr->pParent==0 || pExpr->pParent->eType!=FTSQUERY_NEAR)
139802        ){
139803          Fts3Expr *p;
139804          for(p=pExpr; p->pPhrase==0; p=p->pLeft){
139805            if( p->pRight->iDocid==pCsr->iPrevId ){
139806              fts3EvalInvalidatePoslist(p->pRight->pPhrase);
139807            }
139808          }
139809          if( p->iDocid==pCsr->iPrevId ){
139810            fts3EvalInvalidatePoslist(p->pPhrase);
139811          }
139812        }
139813
139814        break;
139815
139816      case FTSQUERY_OR: {
139817        int bHit1 = fts3EvalTestExpr(pCsr, pExpr->pLeft, pRc);
139818        int bHit2 = fts3EvalTestExpr(pCsr, pExpr->pRight, pRc);
139819        bHit = bHit1 || bHit2;
139820        break;
139821      }
139822
139823      case FTSQUERY_NOT:
139824        bHit = (
139825            fts3EvalTestExpr(pCsr, pExpr->pLeft, pRc)
139826         && !fts3EvalTestExpr(pCsr, pExpr->pRight, pRc)
139827        );
139828        break;
139829
139830      default: {
139831#ifndef SQLITE_DISABLE_FTS4_DEFERRED
139832        if( pCsr->pDeferred
139833         && (pExpr->iDocid==pCsr->iPrevId || pExpr->bDeferred)
139834        ){
139835          Fts3Phrase *pPhrase = pExpr->pPhrase;
139836          assert( pExpr->bDeferred || pPhrase->doclist.bFreeList==0 );
139837          if( pExpr->bDeferred ){
139838            fts3EvalInvalidatePoslist(pPhrase);
139839          }
139840          *pRc = fts3EvalDeferredPhrase(pCsr, pPhrase);
139841          bHit = (pPhrase->doclist.pList!=0);
139842          pExpr->iDocid = pCsr->iPrevId;
139843        }else
139844#endif
139845        {
139846          bHit = (pExpr->bEof==0 && pExpr->iDocid==pCsr->iPrevId);
139847        }
139848        break;
139849      }
139850    }
139851  }
139852  return bHit;
139853}
139854
139855/*
139856** This function is called as the second part of each xNext operation when
139857** iterating through the results of a full-text query. At this point the
139858** cursor points to a row that matches the query expression, with the
139859** following caveats:
139860**
139861**   * Up until this point, "NEAR" operators in the expression have been
139862**     treated as "AND".
139863**
139864**   * Deferred tokens have not yet been considered.
139865**
139866** If *pRc is not SQLITE_OK when this function is called, it immediately
139867** returns 0. Otherwise, it tests whether or not after considering NEAR
139868** operators and deferred tokens the current row is still a match for the
139869** expression. It returns 1 if both of the following are true:
139870**
139871**   1. *pRc is SQLITE_OK when this function returns, and
139872**
139873**   2. After scanning the current FTS table row for the deferred tokens,
139874**      it is determined that the row does *not* match the query.
139875**
139876** Or, if no error occurs and it seems the current row does match the FTS
139877** query, return 0.
139878*/
139879SQLITE_PRIVATE int sqlite3Fts3EvalTestDeferred(Fts3Cursor *pCsr, int *pRc){
139880  int rc = *pRc;
139881  int bMiss = 0;
139882  if( rc==SQLITE_OK ){
139883
139884    /* If there are one or more deferred tokens, load the current row into
139885    ** memory and scan it to determine the position list for each deferred
139886    ** token. Then, see if this row is really a match, considering deferred
139887    ** tokens and NEAR operators (neither of which were taken into account
139888    ** earlier, by fts3EvalNextRow()).
139889    */
139890    if( pCsr->pDeferred ){
139891      rc = fts3CursorSeek(0, pCsr);
139892      if( rc==SQLITE_OK ){
139893        rc = sqlite3Fts3CacheDeferredDoclists(pCsr);
139894      }
139895    }
139896    bMiss = (0==fts3EvalTestExpr(pCsr, pCsr->pExpr, &rc));
139897
139898    /* Free the position-lists accumulated for each deferred token above. */
139899    sqlite3Fts3FreeDeferredDoclists(pCsr);
139900    *pRc = rc;
139901  }
139902  return (rc==SQLITE_OK && bMiss);
139903}
139904
139905/*
139906** Advance to the next document that matches the FTS expression in
139907** Fts3Cursor.pExpr.
139908*/
139909static int fts3EvalNext(Fts3Cursor *pCsr){
139910  int rc = SQLITE_OK;             /* Return Code */
139911  Fts3Expr *pExpr = pCsr->pExpr;
139912  assert( pCsr->isEof==0 );
139913  if( pExpr==0 ){
139914    pCsr->isEof = 1;
139915  }else{
139916    do {
139917      if( pCsr->isRequireSeek==0 ){
139918        sqlite3_reset(pCsr->pStmt);
139919      }
139920      assert( sqlite3_data_count(pCsr->pStmt)==0 );
139921      fts3EvalNextRow(pCsr, pExpr, &rc);
139922      pCsr->isEof = pExpr->bEof;
139923      pCsr->isRequireSeek = 1;
139924      pCsr->isMatchinfoNeeded = 1;
139925      pCsr->iPrevId = pExpr->iDocid;
139926    }while( pCsr->isEof==0 && sqlite3Fts3EvalTestDeferred(pCsr, &rc) );
139927  }
139928
139929  /* Check if the cursor is past the end of the docid range specified
139930  ** by Fts3Cursor.iMinDocid/iMaxDocid. If so, set the EOF flag.  */
139931  if( rc==SQLITE_OK && (
139932        (pCsr->bDesc==0 && pCsr->iPrevId>pCsr->iMaxDocid)
139933     || (pCsr->bDesc!=0 && pCsr->iPrevId<pCsr->iMinDocid)
139934  )){
139935    pCsr->isEof = 1;
139936  }
139937
139938  return rc;
139939}
139940
139941/*
139942** Restart interation for expression pExpr so that the next call to
139943** fts3EvalNext() visits the first row. Do not allow incremental
139944** loading or merging of phrase doclists for this iteration.
139945**
139946** If *pRc is other than SQLITE_OK when this function is called, it is
139947** a no-op. If an error occurs within this function, *pRc is set to an
139948** SQLite error code before returning.
139949*/
139950static void fts3EvalRestart(
139951  Fts3Cursor *pCsr,
139952  Fts3Expr *pExpr,
139953  int *pRc
139954){
139955  if( pExpr && *pRc==SQLITE_OK ){
139956    Fts3Phrase *pPhrase = pExpr->pPhrase;
139957
139958    if( pPhrase ){
139959      fts3EvalInvalidatePoslist(pPhrase);
139960      if( pPhrase->bIncr ){
139961        int i;
139962        for(i=0; i<pPhrase->nToken; i++){
139963          Fts3PhraseToken *pToken = &pPhrase->aToken[i];
139964          assert( pToken->pDeferred==0 );
139965          if( pToken->pSegcsr ){
139966            sqlite3Fts3MsrIncrRestart(pToken->pSegcsr);
139967          }
139968        }
139969        *pRc = fts3EvalPhraseStart(pCsr, 0, pPhrase);
139970      }
139971      pPhrase->doclist.pNextDocid = 0;
139972      pPhrase->doclist.iDocid = 0;
139973      pPhrase->pOrPoslist = 0;
139974    }
139975
139976    pExpr->iDocid = 0;
139977    pExpr->bEof = 0;
139978    pExpr->bStart = 0;
139979
139980    fts3EvalRestart(pCsr, pExpr->pLeft, pRc);
139981    fts3EvalRestart(pCsr, pExpr->pRight, pRc);
139982  }
139983}
139984
139985/*
139986** After allocating the Fts3Expr.aMI[] array for each phrase in the
139987** expression rooted at pExpr, the cursor iterates through all rows matched
139988** by pExpr, calling this function for each row. This function increments
139989** the values in Fts3Expr.aMI[] according to the position-list currently
139990** found in Fts3Expr.pPhrase->doclist.pList for each of the phrase
139991** expression nodes.
139992*/
139993static void fts3EvalUpdateCounts(Fts3Expr *pExpr){
139994  if( pExpr ){
139995    Fts3Phrase *pPhrase = pExpr->pPhrase;
139996    if( pPhrase && pPhrase->doclist.pList ){
139997      int iCol = 0;
139998      char *p = pPhrase->doclist.pList;
139999
140000      assert( *p );
140001      while( 1 ){
140002        u8 c = 0;
140003        int iCnt = 0;
140004        while( 0xFE & (*p | c) ){
140005          if( (c&0x80)==0 ) iCnt++;
140006          c = *p++ & 0x80;
140007        }
140008
140009        /* aMI[iCol*3 + 1] = Number of occurrences
140010        ** aMI[iCol*3 + 2] = Number of rows containing at least one instance
140011        */
140012        pExpr->aMI[iCol*3 + 1] += iCnt;
140013        pExpr->aMI[iCol*3 + 2] += (iCnt>0);
140014        if( *p==0x00 ) break;
140015        p++;
140016        p += fts3GetVarint32(p, &iCol);
140017      }
140018    }
140019
140020    fts3EvalUpdateCounts(pExpr->pLeft);
140021    fts3EvalUpdateCounts(pExpr->pRight);
140022  }
140023}
140024
140025/*
140026** Expression pExpr must be of type FTSQUERY_PHRASE.
140027**
140028** If it is not already allocated and populated, this function allocates and
140029** populates the Fts3Expr.aMI[] array for expression pExpr. If pExpr is part
140030** of a NEAR expression, then it also allocates and populates the same array
140031** for all other phrases that are part of the NEAR expression.
140032**
140033** SQLITE_OK is returned if the aMI[] array is successfully allocated and
140034** populated. Otherwise, if an error occurs, an SQLite error code is returned.
140035*/
140036static int fts3EvalGatherStats(
140037  Fts3Cursor *pCsr,               /* Cursor object */
140038  Fts3Expr *pExpr                 /* FTSQUERY_PHRASE expression */
140039){
140040  int rc = SQLITE_OK;             /* Return code */
140041
140042  assert( pExpr->eType==FTSQUERY_PHRASE );
140043  if( pExpr->aMI==0 ){
140044    Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
140045    Fts3Expr *pRoot;                /* Root of NEAR expression */
140046    Fts3Expr *p;                    /* Iterator used for several purposes */
140047
140048    sqlite3_int64 iPrevId = pCsr->iPrevId;
140049    sqlite3_int64 iDocid;
140050    u8 bEof;
140051
140052    /* Find the root of the NEAR expression */
140053    pRoot = pExpr;
140054    while( pRoot->pParent && pRoot->pParent->eType==FTSQUERY_NEAR ){
140055      pRoot = pRoot->pParent;
140056    }
140057    iDocid = pRoot->iDocid;
140058    bEof = pRoot->bEof;
140059    assert( pRoot->bStart );
140060
140061    /* Allocate space for the aMSI[] array of each FTSQUERY_PHRASE node */
140062    for(p=pRoot; p; p=p->pLeft){
140063      Fts3Expr *pE = (p->eType==FTSQUERY_PHRASE?p:p->pRight);
140064      assert( pE->aMI==0 );
140065      pE->aMI = (u32 *)sqlite3_malloc(pTab->nColumn * 3 * sizeof(u32));
140066      if( !pE->aMI ) return SQLITE_NOMEM;
140067      memset(pE->aMI, 0, pTab->nColumn * 3 * sizeof(u32));
140068    }
140069
140070    fts3EvalRestart(pCsr, pRoot, &rc);
140071
140072    while( pCsr->isEof==0 && rc==SQLITE_OK ){
140073
140074      do {
140075        /* Ensure the %_content statement is reset. */
140076        if( pCsr->isRequireSeek==0 ) sqlite3_reset(pCsr->pStmt);
140077        assert( sqlite3_data_count(pCsr->pStmt)==0 );
140078
140079        /* Advance to the next document */
140080        fts3EvalNextRow(pCsr, pRoot, &rc);
140081        pCsr->isEof = pRoot->bEof;
140082        pCsr->isRequireSeek = 1;
140083        pCsr->isMatchinfoNeeded = 1;
140084        pCsr->iPrevId = pRoot->iDocid;
140085      }while( pCsr->isEof==0
140086           && pRoot->eType==FTSQUERY_NEAR
140087           && sqlite3Fts3EvalTestDeferred(pCsr, &rc)
140088      );
140089
140090      if( rc==SQLITE_OK && pCsr->isEof==0 ){
140091        fts3EvalUpdateCounts(pRoot);
140092      }
140093    }
140094
140095    pCsr->isEof = 0;
140096    pCsr->iPrevId = iPrevId;
140097
140098    if( bEof ){
140099      pRoot->bEof = bEof;
140100    }else{
140101      /* Caution: pRoot may iterate through docids in ascending or descending
140102      ** order. For this reason, even though it seems more defensive, the
140103      ** do loop can not be written:
140104      **
140105      **   do {...} while( pRoot->iDocid<iDocid && rc==SQLITE_OK );
140106      */
140107      fts3EvalRestart(pCsr, pRoot, &rc);
140108      do {
140109        fts3EvalNextRow(pCsr, pRoot, &rc);
140110        assert( pRoot->bEof==0 );
140111      }while( pRoot->iDocid!=iDocid && rc==SQLITE_OK );
140112    }
140113  }
140114  return rc;
140115}
140116
140117/*
140118** This function is used by the matchinfo() module to query a phrase
140119** expression node for the following information:
140120**
140121**   1. The total number of occurrences of the phrase in each column of
140122**      the FTS table (considering all rows), and
140123**
140124**   2. For each column, the number of rows in the table for which the
140125**      column contains at least one instance of the phrase.
140126**
140127** If no error occurs, SQLITE_OK is returned and the values for each column
140128** written into the array aiOut as follows:
140129**
140130**   aiOut[iCol*3 + 1] = Number of occurrences
140131**   aiOut[iCol*3 + 2] = Number of rows containing at least one instance
140132**
140133** Caveats:
140134**
140135**   * If a phrase consists entirely of deferred tokens, then all output
140136**     values are set to the number of documents in the table. In other
140137**     words we assume that very common tokens occur exactly once in each
140138**     column of each row of the table.
140139**
140140**   * If a phrase contains some deferred tokens (and some non-deferred
140141**     tokens), count the potential occurrence identified by considering
140142**     the non-deferred tokens instead of actual phrase occurrences.
140143**
140144**   * If the phrase is part of a NEAR expression, then only phrase instances
140145**     that meet the NEAR constraint are included in the counts.
140146*/
140147SQLITE_PRIVATE int sqlite3Fts3EvalPhraseStats(
140148  Fts3Cursor *pCsr,               /* FTS cursor handle */
140149  Fts3Expr *pExpr,                /* Phrase expression */
140150  u32 *aiOut                      /* Array to write results into (see above) */
140151){
140152  Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
140153  int rc = SQLITE_OK;
140154  int iCol;
140155
140156  if( pExpr->bDeferred && pExpr->pParent->eType!=FTSQUERY_NEAR ){
140157    assert( pCsr->nDoc>0 );
140158    for(iCol=0; iCol<pTab->nColumn; iCol++){
140159      aiOut[iCol*3 + 1] = (u32)pCsr->nDoc;
140160      aiOut[iCol*3 + 2] = (u32)pCsr->nDoc;
140161    }
140162  }else{
140163    rc = fts3EvalGatherStats(pCsr, pExpr);
140164    if( rc==SQLITE_OK ){
140165      assert( pExpr->aMI );
140166      for(iCol=0; iCol<pTab->nColumn; iCol++){
140167        aiOut[iCol*3 + 1] = pExpr->aMI[iCol*3 + 1];
140168        aiOut[iCol*3 + 2] = pExpr->aMI[iCol*3 + 2];
140169      }
140170    }
140171  }
140172
140173  return rc;
140174}
140175
140176/*
140177** The expression pExpr passed as the second argument to this function
140178** must be of type FTSQUERY_PHRASE.
140179**
140180** The returned value is either NULL or a pointer to a buffer containing
140181** a position-list indicating the occurrences of the phrase in column iCol
140182** of the current row.
140183**
140184** More specifically, the returned buffer contains 1 varint for each
140185** occurrence of the phrase in the column, stored using the normal (delta+2)
140186** compression and is terminated by either an 0x01 or 0x00 byte. For example,
140187** if the requested column contains "a b X c d X X" and the position-list
140188** for 'X' is requested, the buffer returned may contain:
140189**
140190**     0x04 0x05 0x03 0x01   or   0x04 0x05 0x03 0x00
140191**
140192** This function works regardless of whether or not the phrase is deferred,
140193** incremental, or neither.
140194*/
140195SQLITE_PRIVATE int sqlite3Fts3EvalPhrasePoslist(
140196  Fts3Cursor *pCsr,               /* FTS3 cursor object */
140197  Fts3Expr *pExpr,                /* Phrase to return doclist for */
140198  int iCol,                       /* Column to return position list for */
140199  char **ppOut                    /* OUT: Pointer to position list */
140200){
140201  Fts3Phrase *pPhrase = pExpr->pPhrase;
140202  Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
140203  char *pIter;
140204  int iThis;
140205  sqlite3_int64 iDocid;
140206
140207  /* If this phrase is applies specifically to some column other than
140208  ** column iCol, return a NULL pointer.  */
140209  *ppOut = 0;
140210  assert( iCol>=0 && iCol<pTab->nColumn );
140211  if( (pPhrase->iColumn<pTab->nColumn && pPhrase->iColumn!=iCol) ){
140212    return SQLITE_OK;
140213  }
140214
140215  iDocid = pExpr->iDocid;
140216  pIter = pPhrase->doclist.pList;
140217  if( iDocid!=pCsr->iPrevId || pExpr->bEof ){
140218    int rc = SQLITE_OK;
140219    int bDescDoclist = pTab->bDescIdx;      /* For DOCID_CMP macro */
140220    int bOr = 0;
140221    u8 bTreeEof = 0;
140222    Fts3Expr *p;                  /* Used to iterate from pExpr to root */
140223    Fts3Expr *pNear;              /* Most senior NEAR ancestor (or pExpr) */
140224    int bMatch;
140225
140226    /* Check if this phrase descends from an OR expression node. If not,
140227    ** return NULL. Otherwise, the entry that corresponds to docid
140228    ** pCsr->iPrevId may lie earlier in the doclist buffer. Or, if the
140229    ** tree that the node is part of has been marked as EOF, but the node
140230    ** itself is not EOF, then it may point to an earlier entry. */
140231    pNear = pExpr;
140232    for(p=pExpr->pParent; p; p=p->pParent){
140233      if( p->eType==FTSQUERY_OR ) bOr = 1;
140234      if( p->eType==FTSQUERY_NEAR ) pNear = p;
140235      if( p->bEof ) bTreeEof = 1;
140236    }
140237    if( bOr==0 ) return SQLITE_OK;
140238
140239    /* This is the descendent of an OR node. In this case we cannot use
140240    ** an incremental phrase. Load the entire doclist for the phrase
140241    ** into memory in this case.  */
140242    if( pPhrase->bIncr ){
140243      int bEofSave = pNear->bEof;
140244      fts3EvalRestart(pCsr, pNear, &rc);
140245      while( rc==SQLITE_OK && !pNear->bEof ){
140246        fts3EvalNextRow(pCsr, pNear, &rc);
140247        if( bEofSave==0 && pNear->iDocid==iDocid ) break;
140248      }
140249      assert( rc!=SQLITE_OK || pPhrase->bIncr==0 );
140250    }
140251    if( bTreeEof ){
140252      while( rc==SQLITE_OK && !pNear->bEof ){
140253        fts3EvalNextRow(pCsr, pNear, &rc);
140254      }
140255    }
140256    if( rc!=SQLITE_OK ) return rc;
140257
140258    bMatch = 1;
140259    for(p=pNear; p; p=p->pLeft){
140260      u8 bEof = 0;
140261      Fts3Expr *pTest = p;
140262      Fts3Phrase *pPh;
140263      assert( pTest->eType==FTSQUERY_NEAR || pTest->eType==FTSQUERY_PHRASE );
140264      if( pTest->eType==FTSQUERY_NEAR ) pTest = pTest->pRight;
140265      assert( pTest->eType==FTSQUERY_PHRASE );
140266      pPh = pTest->pPhrase;
140267
140268      pIter = pPh->pOrPoslist;
140269      iDocid = pPh->iOrDocid;
140270      if( pCsr->bDesc==bDescDoclist ){
140271        bEof = !pPh->doclist.nAll ||
140272          (pIter >= (pPh->doclist.aAll + pPh->doclist.nAll));
140273        while( (pIter==0 || DOCID_CMP(iDocid, pCsr->iPrevId)<0 ) && bEof==0 ){
140274          sqlite3Fts3DoclistNext(
140275              bDescDoclist, pPh->doclist.aAll, pPh->doclist.nAll,
140276              &pIter, &iDocid, &bEof
140277          );
140278        }
140279      }else{
140280        bEof = !pPh->doclist.nAll || (pIter && pIter<=pPh->doclist.aAll);
140281        while( (pIter==0 || DOCID_CMP(iDocid, pCsr->iPrevId)>0 ) && bEof==0 ){
140282          int dummy;
140283          sqlite3Fts3DoclistPrev(
140284              bDescDoclist, pPh->doclist.aAll, pPh->doclist.nAll,
140285              &pIter, &iDocid, &dummy, &bEof
140286              );
140287        }
140288      }
140289      pPh->pOrPoslist = pIter;
140290      pPh->iOrDocid = iDocid;
140291      if( bEof || iDocid!=pCsr->iPrevId ) bMatch = 0;
140292    }
140293
140294    if( bMatch ){
140295      pIter = pPhrase->pOrPoslist;
140296    }else{
140297      pIter = 0;
140298    }
140299  }
140300  if( pIter==0 ) return SQLITE_OK;
140301
140302  if( *pIter==0x01 ){
140303    pIter++;
140304    pIter += fts3GetVarint32(pIter, &iThis);
140305  }else{
140306    iThis = 0;
140307  }
140308  while( iThis<iCol ){
140309    fts3ColumnlistCopy(0, &pIter);
140310    if( *pIter==0x00 ) return SQLITE_OK;
140311    pIter++;
140312    pIter += fts3GetVarint32(pIter, &iThis);
140313  }
140314  if( *pIter==0x00 ){
140315    pIter = 0;
140316  }
140317
140318  *ppOut = ((iCol==iThis)?pIter:0);
140319  return SQLITE_OK;
140320}
140321
140322/*
140323** Free all components of the Fts3Phrase structure that were allocated by
140324** the eval module. Specifically, this means to free:
140325**
140326**   * the contents of pPhrase->doclist, and
140327**   * any Fts3MultiSegReader objects held by phrase tokens.
140328*/
140329SQLITE_PRIVATE void sqlite3Fts3EvalPhraseCleanup(Fts3Phrase *pPhrase){
140330  if( pPhrase ){
140331    int i;
140332    sqlite3_free(pPhrase->doclist.aAll);
140333    fts3EvalInvalidatePoslist(pPhrase);
140334    memset(&pPhrase->doclist, 0, sizeof(Fts3Doclist));
140335    for(i=0; i<pPhrase->nToken; i++){
140336      fts3SegReaderCursorFree(pPhrase->aToken[i].pSegcsr);
140337      pPhrase->aToken[i].pSegcsr = 0;
140338    }
140339  }
140340}
140341
140342
140343/*
140344** Return SQLITE_CORRUPT_VTAB.
140345*/
140346#ifdef SQLITE_DEBUG
140347SQLITE_PRIVATE int sqlite3Fts3Corrupt(){
140348  return SQLITE_CORRUPT_VTAB;
140349}
140350#endif
140351
140352#if !SQLITE_CORE
140353/*
140354** Initialize API pointer table, if required.
140355*/
140356#ifdef _WIN32
140357__declspec(dllexport)
140358#endif
140359SQLITE_API int SQLITE_STDCALL sqlite3_fts3_init(
140360  sqlite3 *db,
140361  char **pzErrMsg,
140362  const sqlite3_api_routines *pApi
140363){
140364  SQLITE_EXTENSION_INIT2(pApi)
140365  return sqlite3Fts3Init(db);
140366}
140367#endif
140368
140369#endif
140370
140371/************** End of fts3.c ************************************************/
140372/************** Begin file fts3_aux.c ****************************************/
140373/*
140374** 2011 Jan 27
140375**
140376** The author disclaims copyright to this source code.  In place of
140377** a legal notice, here is a blessing:
140378**
140379**    May you do good and not evil.
140380**    May you find forgiveness for yourself and forgive others.
140381**    May you share freely, never taking more than you give.
140382**
140383******************************************************************************
140384**
140385*/
140386/* #include "fts3Int.h" */
140387#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
140388
140389/* #include <string.h> */
140390/* #include <assert.h> */
140391
140392typedef struct Fts3auxTable Fts3auxTable;
140393typedef struct Fts3auxCursor Fts3auxCursor;
140394
140395struct Fts3auxTable {
140396  sqlite3_vtab base;              /* Base class used by SQLite core */
140397  Fts3Table *pFts3Tab;
140398};
140399
140400struct Fts3auxCursor {
140401  sqlite3_vtab_cursor base;       /* Base class used by SQLite core */
140402  Fts3MultiSegReader csr;        /* Must be right after "base" */
140403  Fts3SegFilter filter;
140404  char *zStop;
140405  int nStop;                      /* Byte-length of string zStop */
140406  int iLangid;                    /* Language id to query */
140407  int isEof;                      /* True if cursor is at EOF */
140408  sqlite3_int64 iRowid;           /* Current rowid */
140409
140410  int iCol;                       /* Current value of 'col' column */
140411  int nStat;                      /* Size of aStat[] array */
140412  struct Fts3auxColstats {
140413    sqlite3_int64 nDoc;           /* 'documents' values for current csr row */
140414    sqlite3_int64 nOcc;           /* 'occurrences' values for current csr row */
140415  } *aStat;
140416};
140417
140418/*
140419** Schema of the terms table.
140420*/
140421#define FTS3_AUX_SCHEMA \
140422  "CREATE TABLE x(term, col, documents, occurrences, languageid HIDDEN)"
140423
140424/*
140425** This function does all the work for both the xConnect and xCreate methods.
140426** These tables have no persistent representation of their own, so xConnect
140427** and xCreate are identical operations.
140428*/
140429static int fts3auxConnectMethod(
140430  sqlite3 *db,                    /* Database connection */
140431  void *pUnused,                  /* Unused */
140432  int argc,                       /* Number of elements in argv array */
140433  const char * const *argv,       /* xCreate/xConnect argument array */
140434  sqlite3_vtab **ppVtab,          /* OUT: New sqlite3_vtab object */
140435  char **pzErr                    /* OUT: sqlite3_malloc'd error message */
140436){
140437  char const *zDb;                /* Name of database (e.g. "main") */
140438  char const *zFts3;              /* Name of fts3 table */
140439  int nDb;                        /* Result of strlen(zDb) */
140440  int nFts3;                      /* Result of strlen(zFts3) */
140441  int nByte;                      /* Bytes of space to allocate here */
140442  int rc;                         /* value returned by declare_vtab() */
140443  Fts3auxTable *p;                /* Virtual table object to return */
140444
140445  UNUSED_PARAMETER(pUnused);
140446
140447  /* The user should invoke this in one of two forms:
140448  **
140449  **     CREATE VIRTUAL TABLE xxx USING fts4aux(fts4-table);
140450  **     CREATE VIRTUAL TABLE xxx USING fts4aux(fts4-table-db, fts4-table);
140451  */
140452  if( argc!=4 && argc!=5 ) goto bad_args;
140453
140454  zDb = argv[1];
140455  nDb = (int)strlen(zDb);
140456  if( argc==5 ){
140457    if( nDb==4 && 0==sqlite3_strnicmp("temp", zDb, 4) ){
140458      zDb = argv[3];
140459      nDb = (int)strlen(zDb);
140460      zFts3 = argv[4];
140461    }else{
140462      goto bad_args;
140463    }
140464  }else{
140465    zFts3 = argv[3];
140466  }
140467  nFts3 = (int)strlen(zFts3);
140468
140469  rc = sqlite3_declare_vtab(db, FTS3_AUX_SCHEMA);
140470  if( rc!=SQLITE_OK ) return rc;
140471
140472  nByte = sizeof(Fts3auxTable) + sizeof(Fts3Table) + nDb + nFts3 + 2;
140473  p = (Fts3auxTable *)sqlite3_malloc(nByte);
140474  if( !p ) return SQLITE_NOMEM;
140475  memset(p, 0, nByte);
140476
140477  p->pFts3Tab = (Fts3Table *)&p[1];
140478  p->pFts3Tab->zDb = (char *)&p->pFts3Tab[1];
140479  p->pFts3Tab->zName = &p->pFts3Tab->zDb[nDb+1];
140480  p->pFts3Tab->db = db;
140481  p->pFts3Tab->nIndex = 1;
140482
140483  memcpy((char *)p->pFts3Tab->zDb, zDb, nDb);
140484  memcpy((char *)p->pFts3Tab->zName, zFts3, nFts3);
140485  sqlite3Fts3Dequote((char *)p->pFts3Tab->zName);
140486
140487  *ppVtab = (sqlite3_vtab *)p;
140488  return SQLITE_OK;
140489
140490 bad_args:
140491  sqlite3Fts3ErrMsg(pzErr, "invalid arguments to fts4aux constructor");
140492  return SQLITE_ERROR;
140493}
140494
140495/*
140496** This function does the work for both the xDisconnect and xDestroy methods.
140497** These tables have no persistent representation of their own, so xDisconnect
140498** and xDestroy are identical operations.
140499*/
140500static int fts3auxDisconnectMethod(sqlite3_vtab *pVtab){
140501  Fts3auxTable *p = (Fts3auxTable *)pVtab;
140502  Fts3Table *pFts3 = p->pFts3Tab;
140503  int i;
140504
140505  /* Free any prepared statements held */
140506  for(i=0; i<SizeofArray(pFts3->aStmt); i++){
140507    sqlite3_finalize(pFts3->aStmt[i]);
140508  }
140509  sqlite3_free(pFts3->zSegmentsTbl);
140510  sqlite3_free(p);
140511  return SQLITE_OK;
140512}
140513
140514#define FTS4AUX_EQ_CONSTRAINT 1
140515#define FTS4AUX_GE_CONSTRAINT 2
140516#define FTS4AUX_LE_CONSTRAINT 4
140517
140518/*
140519** xBestIndex - Analyze a WHERE and ORDER BY clause.
140520*/
140521static int fts3auxBestIndexMethod(
140522  sqlite3_vtab *pVTab,
140523  sqlite3_index_info *pInfo
140524){
140525  int i;
140526  int iEq = -1;
140527  int iGe = -1;
140528  int iLe = -1;
140529  int iLangid = -1;
140530  int iNext = 1;                  /* Next free argvIndex value */
140531
140532  UNUSED_PARAMETER(pVTab);
140533
140534  /* This vtab delivers always results in "ORDER BY term ASC" order. */
140535  if( pInfo->nOrderBy==1
140536   && pInfo->aOrderBy[0].iColumn==0
140537   && pInfo->aOrderBy[0].desc==0
140538  ){
140539    pInfo->orderByConsumed = 1;
140540  }
140541
140542  /* Search for equality and range constraints on the "term" column.
140543  ** And equality constraints on the hidden "languageid" column. */
140544  for(i=0; i<pInfo->nConstraint; i++){
140545    if( pInfo->aConstraint[i].usable ){
140546      int op = pInfo->aConstraint[i].op;
140547      int iCol = pInfo->aConstraint[i].iColumn;
140548
140549      if( iCol==0 ){
140550        if( op==SQLITE_INDEX_CONSTRAINT_EQ ) iEq = i;
140551        if( op==SQLITE_INDEX_CONSTRAINT_LT ) iLe = i;
140552        if( op==SQLITE_INDEX_CONSTRAINT_LE ) iLe = i;
140553        if( op==SQLITE_INDEX_CONSTRAINT_GT ) iGe = i;
140554        if( op==SQLITE_INDEX_CONSTRAINT_GE ) iGe = i;
140555      }
140556      if( iCol==4 ){
140557        if( op==SQLITE_INDEX_CONSTRAINT_EQ ) iLangid = i;
140558      }
140559    }
140560  }
140561
140562  if( iEq>=0 ){
140563    pInfo->idxNum = FTS4AUX_EQ_CONSTRAINT;
140564    pInfo->aConstraintUsage[iEq].argvIndex = iNext++;
140565    pInfo->estimatedCost = 5;
140566  }else{
140567    pInfo->idxNum = 0;
140568    pInfo->estimatedCost = 20000;
140569    if( iGe>=0 ){
140570      pInfo->idxNum += FTS4AUX_GE_CONSTRAINT;
140571      pInfo->aConstraintUsage[iGe].argvIndex = iNext++;
140572      pInfo->estimatedCost /= 2;
140573    }
140574    if( iLe>=0 ){
140575      pInfo->idxNum += FTS4AUX_LE_CONSTRAINT;
140576      pInfo->aConstraintUsage[iLe].argvIndex = iNext++;
140577      pInfo->estimatedCost /= 2;
140578    }
140579  }
140580  if( iLangid>=0 ){
140581    pInfo->aConstraintUsage[iLangid].argvIndex = iNext++;
140582    pInfo->estimatedCost--;
140583  }
140584
140585  return SQLITE_OK;
140586}
140587
140588/*
140589** xOpen - Open a cursor.
140590*/
140591static int fts3auxOpenMethod(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCsr){
140592  Fts3auxCursor *pCsr;            /* Pointer to cursor object to return */
140593
140594  UNUSED_PARAMETER(pVTab);
140595
140596  pCsr = (Fts3auxCursor *)sqlite3_malloc(sizeof(Fts3auxCursor));
140597  if( !pCsr ) return SQLITE_NOMEM;
140598  memset(pCsr, 0, sizeof(Fts3auxCursor));
140599
140600  *ppCsr = (sqlite3_vtab_cursor *)pCsr;
140601  return SQLITE_OK;
140602}
140603
140604/*
140605** xClose - Close a cursor.
140606*/
140607static int fts3auxCloseMethod(sqlite3_vtab_cursor *pCursor){
140608  Fts3Table *pFts3 = ((Fts3auxTable *)pCursor->pVtab)->pFts3Tab;
140609  Fts3auxCursor *pCsr = (Fts3auxCursor *)pCursor;
140610
140611  sqlite3Fts3SegmentsClose(pFts3);
140612  sqlite3Fts3SegReaderFinish(&pCsr->csr);
140613  sqlite3_free((void *)pCsr->filter.zTerm);
140614  sqlite3_free(pCsr->zStop);
140615  sqlite3_free(pCsr->aStat);
140616  sqlite3_free(pCsr);
140617  return SQLITE_OK;
140618}
140619
140620static int fts3auxGrowStatArray(Fts3auxCursor *pCsr, int nSize){
140621  if( nSize>pCsr->nStat ){
140622    struct Fts3auxColstats *aNew;
140623    aNew = (struct Fts3auxColstats *)sqlite3_realloc(pCsr->aStat,
140624        sizeof(struct Fts3auxColstats) * nSize
140625    );
140626    if( aNew==0 ) return SQLITE_NOMEM;
140627    memset(&aNew[pCsr->nStat], 0,
140628        sizeof(struct Fts3auxColstats) * (nSize - pCsr->nStat)
140629    );
140630    pCsr->aStat = aNew;
140631    pCsr->nStat = nSize;
140632  }
140633  return SQLITE_OK;
140634}
140635
140636/*
140637** xNext - Advance the cursor to the next row, if any.
140638*/
140639static int fts3auxNextMethod(sqlite3_vtab_cursor *pCursor){
140640  Fts3auxCursor *pCsr = (Fts3auxCursor *)pCursor;
140641  Fts3Table *pFts3 = ((Fts3auxTable *)pCursor->pVtab)->pFts3Tab;
140642  int rc;
140643
140644  /* Increment our pretend rowid value. */
140645  pCsr->iRowid++;
140646
140647  for(pCsr->iCol++; pCsr->iCol<pCsr->nStat; pCsr->iCol++){
140648    if( pCsr->aStat[pCsr->iCol].nDoc>0 ) return SQLITE_OK;
140649  }
140650
140651  rc = sqlite3Fts3SegReaderStep(pFts3, &pCsr->csr);
140652  if( rc==SQLITE_ROW ){
140653    int i = 0;
140654    int nDoclist = pCsr->csr.nDoclist;
140655    char *aDoclist = pCsr->csr.aDoclist;
140656    int iCol;
140657
140658    int eState = 0;
140659
140660    if( pCsr->zStop ){
140661      int n = (pCsr->nStop<pCsr->csr.nTerm) ? pCsr->nStop : pCsr->csr.nTerm;
140662      int mc = memcmp(pCsr->zStop, pCsr->csr.zTerm, n);
140663      if( mc<0 || (mc==0 && pCsr->csr.nTerm>pCsr->nStop) ){
140664        pCsr->isEof = 1;
140665        return SQLITE_OK;
140666      }
140667    }
140668
140669    if( fts3auxGrowStatArray(pCsr, 2) ) return SQLITE_NOMEM;
140670    memset(pCsr->aStat, 0, sizeof(struct Fts3auxColstats) * pCsr->nStat);
140671    iCol = 0;
140672
140673    while( i<nDoclist ){
140674      sqlite3_int64 v = 0;
140675
140676      i += sqlite3Fts3GetVarint(&aDoclist[i], &v);
140677      switch( eState ){
140678        /* State 0. In this state the integer just read was a docid. */
140679        case 0:
140680          pCsr->aStat[0].nDoc++;
140681          eState = 1;
140682          iCol = 0;
140683          break;
140684
140685        /* State 1. In this state we are expecting either a 1, indicating
140686        ** that the following integer will be a column number, or the
140687        ** start of a position list for column 0.
140688        **
140689        ** The only difference between state 1 and state 2 is that if the
140690        ** integer encountered in state 1 is not 0 or 1, then we need to
140691        ** increment the column 0 "nDoc" count for this term.
140692        */
140693        case 1:
140694          assert( iCol==0 );
140695          if( v>1 ){
140696            pCsr->aStat[1].nDoc++;
140697          }
140698          eState = 2;
140699          /* fall through */
140700
140701        case 2:
140702          if( v==0 ){       /* 0x00. Next integer will be a docid. */
140703            eState = 0;
140704          }else if( v==1 ){ /* 0x01. Next integer will be a column number. */
140705            eState = 3;
140706          }else{            /* 2 or greater. A position. */
140707            pCsr->aStat[iCol+1].nOcc++;
140708            pCsr->aStat[0].nOcc++;
140709          }
140710          break;
140711
140712        /* State 3. The integer just read is a column number. */
140713        default: assert( eState==3 );
140714          iCol = (int)v;
140715          if( fts3auxGrowStatArray(pCsr, iCol+2) ) return SQLITE_NOMEM;
140716          pCsr->aStat[iCol+1].nDoc++;
140717          eState = 2;
140718          break;
140719      }
140720    }
140721
140722    pCsr->iCol = 0;
140723    rc = SQLITE_OK;
140724  }else{
140725    pCsr->isEof = 1;
140726  }
140727  return rc;
140728}
140729
140730/*
140731** xFilter - Initialize a cursor to point at the start of its data.
140732*/
140733static int fts3auxFilterMethod(
140734  sqlite3_vtab_cursor *pCursor,   /* The cursor used for this query */
140735  int idxNum,                     /* Strategy index */
140736  const char *idxStr,             /* Unused */
140737  int nVal,                       /* Number of elements in apVal */
140738  sqlite3_value **apVal           /* Arguments for the indexing scheme */
140739){
140740  Fts3auxCursor *pCsr = (Fts3auxCursor *)pCursor;
140741  Fts3Table *pFts3 = ((Fts3auxTable *)pCursor->pVtab)->pFts3Tab;
140742  int rc;
140743  int isScan = 0;
140744  int iLangVal = 0;               /* Language id to query */
140745
140746  int iEq = -1;                   /* Index of term=? value in apVal */
140747  int iGe = -1;                   /* Index of term>=? value in apVal */
140748  int iLe = -1;                   /* Index of term<=? value in apVal */
140749  int iLangid = -1;               /* Index of languageid=? value in apVal */
140750  int iNext = 0;
140751
140752  UNUSED_PARAMETER(nVal);
140753  UNUSED_PARAMETER(idxStr);
140754
140755  assert( idxStr==0 );
140756  assert( idxNum==FTS4AUX_EQ_CONSTRAINT || idxNum==0
140757       || idxNum==FTS4AUX_LE_CONSTRAINT || idxNum==FTS4AUX_GE_CONSTRAINT
140758       || idxNum==(FTS4AUX_LE_CONSTRAINT|FTS4AUX_GE_CONSTRAINT)
140759  );
140760
140761  if( idxNum==FTS4AUX_EQ_CONSTRAINT ){
140762    iEq = iNext++;
140763  }else{
140764    isScan = 1;
140765    if( idxNum & FTS4AUX_GE_CONSTRAINT ){
140766      iGe = iNext++;
140767    }
140768    if( idxNum & FTS4AUX_LE_CONSTRAINT ){
140769      iLe = iNext++;
140770    }
140771  }
140772  if( iNext<nVal ){
140773    iLangid = iNext++;
140774  }
140775
140776  /* In case this cursor is being reused, close and zero it. */
140777  testcase(pCsr->filter.zTerm);
140778  sqlite3Fts3SegReaderFinish(&pCsr->csr);
140779  sqlite3_free((void *)pCsr->filter.zTerm);
140780  sqlite3_free(pCsr->aStat);
140781  memset(&pCsr->csr, 0, ((u8*)&pCsr[1]) - (u8*)&pCsr->csr);
140782
140783  pCsr->filter.flags = FTS3_SEGMENT_REQUIRE_POS|FTS3_SEGMENT_IGNORE_EMPTY;
140784  if( isScan ) pCsr->filter.flags |= FTS3_SEGMENT_SCAN;
140785
140786  if( iEq>=0 || iGe>=0 ){
140787    const unsigned char *zStr = sqlite3_value_text(apVal[0]);
140788    assert( (iEq==0 && iGe==-1) || (iEq==-1 && iGe==0) );
140789    if( zStr ){
140790      pCsr->filter.zTerm = sqlite3_mprintf("%s", zStr);
140791      pCsr->filter.nTerm = sqlite3_value_bytes(apVal[0]);
140792      if( pCsr->filter.zTerm==0 ) return SQLITE_NOMEM;
140793    }
140794  }
140795
140796  if( iLe>=0 ){
140797    pCsr->zStop = sqlite3_mprintf("%s", sqlite3_value_text(apVal[iLe]));
140798    pCsr->nStop = sqlite3_value_bytes(apVal[iLe]);
140799    if( pCsr->zStop==0 ) return SQLITE_NOMEM;
140800  }
140801
140802  if( iLangid>=0 ){
140803    iLangVal = sqlite3_value_int(apVal[iLangid]);
140804
140805    /* If the user specified a negative value for the languageid, use zero
140806    ** instead. This works, as the "languageid=?" constraint will also
140807    ** be tested by the VDBE layer. The test will always be false (since
140808    ** this module will not return a row with a negative languageid), and
140809    ** so the overall query will return zero rows.  */
140810    if( iLangVal<0 ) iLangVal = 0;
140811  }
140812  pCsr->iLangid = iLangVal;
140813
140814  rc = sqlite3Fts3SegReaderCursor(pFts3, iLangVal, 0, FTS3_SEGCURSOR_ALL,
140815      pCsr->filter.zTerm, pCsr->filter.nTerm, 0, isScan, &pCsr->csr
140816  );
140817  if( rc==SQLITE_OK ){
140818    rc = sqlite3Fts3SegReaderStart(pFts3, &pCsr->csr, &pCsr->filter);
140819  }
140820
140821  if( rc==SQLITE_OK ) rc = fts3auxNextMethod(pCursor);
140822  return rc;
140823}
140824
140825/*
140826** xEof - Return true if the cursor is at EOF, or false otherwise.
140827*/
140828static int fts3auxEofMethod(sqlite3_vtab_cursor *pCursor){
140829  Fts3auxCursor *pCsr = (Fts3auxCursor *)pCursor;
140830  return pCsr->isEof;
140831}
140832
140833/*
140834** xColumn - Return a column value.
140835*/
140836static int fts3auxColumnMethod(
140837  sqlite3_vtab_cursor *pCursor,   /* Cursor to retrieve value from */
140838  sqlite3_context *pCtx,          /* Context for sqlite3_result_xxx() calls */
140839  int iCol                        /* Index of column to read value from */
140840){
140841  Fts3auxCursor *p = (Fts3auxCursor *)pCursor;
140842
140843  assert( p->isEof==0 );
140844  switch( iCol ){
140845    case 0: /* term */
140846      sqlite3_result_text(pCtx, p->csr.zTerm, p->csr.nTerm, SQLITE_TRANSIENT);
140847      break;
140848
140849    case 1: /* col */
140850      if( p->iCol ){
140851        sqlite3_result_int(pCtx, p->iCol-1);
140852      }else{
140853        sqlite3_result_text(pCtx, "*", -1, SQLITE_STATIC);
140854      }
140855      break;
140856
140857    case 2: /* documents */
140858      sqlite3_result_int64(pCtx, p->aStat[p->iCol].nDoc);
140859      break;
140860
140861    case 3: /* occurrences */
140862      sqlite3_result_int64(pCtx, p->aStat[p->iCol].nOcc);
140863      break;
140864
140865    default: /* languageid */
140866      assert( iCol==4 );
140867      sqlite3_result_int(pCtx, p->iLangid);
140868      break;
140869  }
140870
140871  return SQLITE_OK;
140872}
140873
140874/*
140875** xRowid - Return the current rowid for the cursor.
140876*/
140877static int fts3auxRowidMethod(
140878  sqlite3_vtab_cursor *pCursor,   /* Cursor to retrieve value from */
140879  sqlite_int64 *pRowid            /* OUT: Rowid value */
140880){
140881  Fts3auxCursor *pCsr = (Fts3auxCursor *)pCursor;
140882  *pRowid = pCsr->iRowid;
140883  return SQLITE_OK;
140884}
140885
140886/*
140887** Register the fts3aux module with database connection db. Return SQLITE_OK
140888** if successful or an error code if sqlite3_create_module() fails.
140889*/
140890SQLITE_PRIVATE int sqlite3Fts3InitAux(sqlite3 *db){
140891  static const sqlite3_module fts3aux_module = {
140892     0,                           /* iVersion      */
140893     fts3auxConnectMethod,        /* xCreate       */
140894     fts3auxConnectMethod,        /* xConnect      */
140895     fts3auxBestIndexMethod,      /* xBestIndex    */
140896     fts3auxDisconnectMethod,     /* xDisconnect   */
140897     fts3auxDisconnectMethod,     /* xDestroy      */
140898     fts3auxOpenMethod,           /* xOpen         */
140899     fts3auxCloseMethod,          /* xClose        */
140900     fts3auxFilterMethod,         /* xFilter       */
140901     fts3auxNextMethod,           /* xNext         */
140902     fts3auxEofMethod,            /* xEof          */
140903     fts3auxColumnMethod,         /* xColumn       */
140904     fts3auxRowidMethod,          /* xRowid        */
140905     0,                           /* xUpdate       */
140906     0,                           /* xBegin        */
140907     0,                           /* xSync         */
140908     0,                           /* xCommit       */
140909     0,                           /* xRollback     */
140910     0,                           /* xFindFunction */
140911     0,                           /* xRename       */
140912     0,                           /* xSavepoint    */
140913     0,                           /* xRelease      */
140914     0                            /* xRollbackTo   */
140915  };
140916  int rc;                         /* Return code */
140917
140918  rc = sqlite3_create_module(db, "fts4aux", &fts3aux_module, 0);
140919  return rc;
140920}
140921
140922#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */
140923
140924/************** End of fts3_aux.c ********************************************/
140925/************** Begin file fts3_expr.c ***************************************/
140926/*
140927** 2008 Nov 28
140928**
140929** The author disclaims copyright to this source code.  In place of
140930** a legal notice, here is a blessing:
140931**
140932**    May you do good and not evil.
140933**    May you find forgiveness for yourself and forgive others.
140934**    May you share freely, never taking more than you give.
140935**
140936******************************************************************************
140937**
140938** This module contains code that implements a parser for fts3 query strings
140939** (the right-hand argument to the MATCH operator). Because the supported
140940** syntax is relatively simple, the whole tokenizer/parser system is
140941** hand-coded.
140942*/
140943/* #include "fts3Int.h" */
140944#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
140945
140946/*
140947** By default, this module parses the legacy syntax that has been
140948** traditionally used by fts3. Or, if SQLITE_ENABLE_FTS3_PARENTHESIS
140949** is defined, then it uses the new syntax. The differences between
140950** the new and the old syntaxes are:
140951**
140952**  a) The new syntax supports parenthesis. The old does not.
140953**
140954**  b) The new syntax supports the AND and NOT operators. The old does not.
140955**
140956**  c) The old syntax supports the "-" token qualifier. This is not
140957**     supported by the new syntax (it is replaced by the NOT operator).
140958**
140959**  d) When using the old syntax, the OR operator has a greater precedence
140960**     than an implicit AND. When using the new, both implicity and explicit
140961**     AND operators have a higher precedence than OR.
140962**
140963** If compiled with SQLITE_TEST defined, then this module exports the
140964** symbol "int sqlite3_fts3_enable_parentheses". Setting this variable
140965** to zero causes the module to use the old syntax. If it is set to
140966** non-zero the new syntax is activated. This is so both syntaxes can
140967** be tested using a single build of testfixture.
140968**
140969** The following describes the syntax supported by the fts3 MATCH
140970** operator in a similar format to that used by the lemon parser
140971** generator. This module does not use actually lemon, it uses a
140972** custom parser.
140973**
140974**   query ::= andexpr (OR andexpr)*.
140975**
140976**   andexpr ::= notexpr (AND? notexpr)*.
140977**
140978**   notexpr ::= nearexpr (NOT nearexpr|-TOKEN)*.
140979**   notexpr ::= LP query RP.
140980**
140981**   nearexpr ::= phrase (NEAR distance_opt nearexpr)*.
140982**
140983**   distance_opt ::= .
140984**   distance_opt ::= / INTEGER.
140985**
140986**   phrase ::= TOKEN.
140987**   phrase ::= COLUMN:TOKEN.
140988**   phrase ::= "TOKEN TOKEN TOKEN...".
140989*/
140990
140991#ifdef SQLITE_TEST
140992SQLITE_API int sqlite3_fts3_enable_parentheses = 0;
140993#else
140994# ifdef SQLITE_ENABLE_FTS3_PARENTHESIS
140995#  define sqlite3_fts3_enable_parentheses 1
140996# else
140997#  define sqlite3_fts3_enable_parentheses 0
140998# endif
140999#endif
141000
141001/*
141002** Default span for NEAR operators.
141003*/
141004#define SQLITE_FTS3_DEFAULT_NEAR_PARAM 10
141005
141006/* #include <string.h> */
141007/* #include <assert.h> */
141008
141009/*
141010** isNot:
141011**   This variable is used by function getNextNode(). When getNextNode() is
141012**   called, it sets ParseContext.isNot to true if the 'next node' is a
141013**   FTSQUERY_PHRASE with a unary "-" attached to it. i.e. "mysql" in the
141014**   FTS3 query "sqlite -mysql". Otherwise, ParseContext.isNot is set to
141015**   zero.
141016*/
141017typedef struct ParseContext ParseContext;
141018struct ParseContext {
141019  sqlite3_tokenizer *pTokenizer;      /* Tokenizer module */
141020  int iLangid;                        /* Language id used with tokenizer */
141021  const char **azCol;                 /* Array of column names for fts3 table */
141022  int bFts4;                          /* True to allow FTS4-only syntax */
141023  int nCol;                           /* Number of entries in azCol[] */
141024  int iDefaultCol;                    /* Default column to query */
141025  int isNot;                          /* True if getNextNode() sees a unary - */
141026  sqlite3_context *pCtx;              /* Write error message here */
141027  int nNest;                          /* Number of nested brackets */
141028};
141029
141030/*
141031** This function is equivalent to the standard isspace() function.
141032**
141033** The standard isspace() can be awkward to use safely, because although it
141034** is defined to accept an argument of type int, its behavior when passed
141035** an integer that falls outside of the range of the unsigned char type
141036** is undefined (and sometimes, "undefined" means segfault). This wrapper
141037** is defined to accept an argument of type char, and always returns 0 for
141038** any values that fall outside of the range of the unsigned char type (i.e.
141039** negative values).
141040*/
141041static int fts3isspace(char c){
141042  return c==' ' || c=='\t' || c=='\n' || c=='\r' || c=='\v' || c=='\f';
141043}
141044
141045/*
141046** Allocate nByte bytes of memory using sqlite3_malloc(). If successful,
141047** zero the memory before returning a pointer to it. If unsuccessful,
141048** return NULL.
141049*/
141050static void *fts3MallocZero(int nByte){
141051  void *pRet = sqlite3_malloc(nByte);
141052  if( pRet ) memset(pRet, 0, nByte);
141053  return pRet;
141054}
141055
141056SQLITE_PRIVATE int sqlite3Fts3OpenTokenizer(
141057  sqlite3_tokenizer *pTokenizer,
141058  int iLangid,
141059  const char *z,
141060  int n,
141061  sqlite3_tokenizer_cursor **ppCsr
141062){
141063  sqlite3_tokenizer_module const *pModule = pTokenizer->pModule;
141064  sqlite3_tokenizer_cursor *pCsr = 0;
141065  int rc;
141066
141067  rc = pModule->xOpen(pTokenizer, z, n, &pCsr);
141068  assert( rc==SQLITE_OK || pCsr==0 );
141069  if( rc==SQLITE_OK ){
141070    pCsr->pTokenizer = pTokenizer;
141071    if( pModule->iVersion>=1 ){
141072      rc = pModule->xLanguageid(pCsr, iLangid);
141073      if( rc!=SQLITE_OK ){
141074        pModule->xClose(pCsr);
141075        pCsr = 0;
141076      }
141077    }
141078  }
141079  *ppCsr = pCsr;
141080  return rc;
141081}
141082
141083/*
141084** Function getNextNode(), which is called by fts3ExprParse(), may itself
141085** call fts3ExprParse(). So this forward declaration is required.
141086*/
141087static int fts3ExprParse(ParseContext *, const char *, int, Fts3Expr **, int *);
141088
141089/*
141090** Extract the next token from buffer z (length n) using the tokenizer
141091** and other information (column names etc.) in pParse. Create an Fts3Expr
141092** structure of type FTSQUERY_PHRASE containing a phrase consisting of this
141093** single token and set *ppExpr to point to it. If the end of the buffer is
141094** reached before a token is found, set *ppExpr to zero. It is the
141095** responsibility of the caller to eventually deallocate the allocated
141096** Fts3Expr structure (if any) by passing it to sqlite3_free().
141097**
141098** Return SQLITE_OK if successful, or SQLITE_NOMEM if a memory allocation
141099** fails.
141100*/
141101static int getNextToken(
141102  ParseContext *pParse,                   /* fts3 query parse context */
141103  int iCol,                               /* Value for Fts3Phrase.iColumn */
141104  const char *z, int n,                   /* Input string */
141105  Fts3Expr **ppExpr,                      /* OUT: expression */
141106  int *pnConsumed                         /* OUT: Number of bytes consumed */
141107){
141108  sqlite3_tokenizer *pTokenizer = pParse->pTokenizer;
141109  sqlite3_tokenizer_module const *pModule = pTokenizer->pModule;
141110  int rc;
141111  sqlite3_tokenizer_cursor *pCursor;
141112  Fts3Expr *pRet = 0;
141113  int i = 0;
141114
141115  /* Set variable i to the maximum number of bytes of input to tokenize. */
141116  for(i=0; i<n; i++){
141117    if( sqlite3_fts3_enable_parentheses && (z[i]=='(' || z[i]==')') ) break;
141118    if( z[i]=='"' ) break;
141119  }
141120
141121  *pnConsumed = i;
141122  rc = sqlite3Fts3OpenTokenizer(pTokenizer, pParse->iLangid, z, i, &pCursor);
141123  if( rc==SQLITE_OK ){
141124    const char *zToken;
141125    int nToken = 0, iStart = 0, iEnd = 0, iPosition = 0;
141126    int nByte;                               /* total space to allocate */
141127
141128    rc = pModule->xNext(pCursor, &zToken, &nToken, &iStart, &iEnd, &iPosition);
141129    if( rc==SQLITE_OK ){
141130      nByte = sizeof(Fts3Expr) + sizeof(Fts3Phrase) + nToken;
141131      pRet = (Fts3Expr *)fts3MallocZero(nByte);
141132      if( !pRet ){
141133        rc = SQLITE_NOMEM;
141134      }else{
141135        pRet->eType = FTSQUERY_PHRASE;
141136        pRet->pPhrase = (Fts3Phrase *)&pRet[1];
141137        pRet->pPhrase->nToken = 1;
141138        pRet->pPhrase->iColumn = iCol;
141139        pRet->pPhrase->aToken[0].n = nToken;
141140        pRet->pPhrase->aToken[0].z = (char *)&pRet->pPhrase[1];
141141        memcpy(pRet->pPhrase->aToken[0].z, zToken, nToken);
141142
141143        if( iEnd<n && z[iEnd]=='*' ){
141144          pRet->pPhrase->aToken[0].isPrefix = 1;
141145          iEnd++;
141146        }
141147
141148        while( 1 ){
141149          if( !sqlite3_fts3_enable_parentheses
141150           && iStart>0 && z[iStart-1]=='-'
141151          ){
141152            pParse->isNot = 1;
141153            iStart--;
141154          }else if( pParse->bFts4 && iStart>0 && z[iStart-1]=='^' ){
141155            pRet->pPhrase->aToken[0].bFirst = 1;
141156            iStart--;
141157          }else{
141158            break;
141159          }
141160        }
141161
141162      }
141163      *pnConsumed = iEnd;
141164    }else if( i && rc==SQLITE_DONE ){
141165      rc = SQLITE_OK;
141166    }
141167
141168    pModule->xClose(pCursor);
141169  }
141170
141171  *ppExpr = pRet;
141172  return rc;
141173}
141174
141175
141176/*
141177** Enlarge a memory allocation.  If an out-of-memory allocation occurs,
141178** then free the old allocation.
141179*/
141180static void *fts3ReallocOrFree(void *pOrig, int nNew){
141181  void *pRet = sqlite3_realloc(pOrig, nNew);
141182  if( !pRet ){
141183    sqlite3_free(pOrig);
141184  }
141185  return pRet;
141186}
141187
141188/*
141189** Buffer zInput, length nInput, contains the contents of a quoted string
141190** that appeared as part of an fts3 query expression. Neither quote character
141191** is included in the buffer. This function attempts to tokenize the entire
141192** input buffer and create an Fts3Expr structure of type FTSQUERY_PHRASE
141193** containing the results.
141194**
141195** If successful, SQLITE_OK is returned and *ppExpr set to point at the
141196** allocated Fts3Expr structure. Otherwise, either SQLITE_NOMEM (out of memory
141197** error) or SQLITE_ERROR (tokenization error) is returned and *ppExpr set
141198** to 0.
141199*/
141200static int getNextString(
141201  ParseContext *pParse,                   /* fts3 query parse context */
141202  const char *zInput, int nInput,         /* Input string */
141203  Fts3Expr **ppExpr                       /* OUT: expression */
141204){
141205  sqlite3_tokenizer *pTokenizer = pParse->pTokenizer;
141206  sqlite3_tokenizer_module const *pModule = pTokenizer->pModule;
141207  int rc;
141208  Fts3Expr *p = 0;
141209  sqlite3_tokenizer_cursor *pCursor = 0;
141210  char *zTemp = 0;
141211  int nTemp = 0;
141212
141213  const int nSpace = sizeof(Fts3Expr) + sizeof(Fts3Phrase);
141214  int nToken = 0;
141215
141216  /* The final Fts3Expr data structure, including the Fts3Phrase,
141217  ** Fts3PhraseToken structures token buffers are all stored as a single
141218  ** allocation so that the expression can be freed with a single call to
141219  ** sqlite3_free(). Setting this up requires a two pass approach.
141220  **
141221  ** The first pass, in the block below, uses a tokenizer cursor to iterate
141222  ** through the tokens in the expression. This pass uses fts3ReallocOrFree()
141223  ** to assemble data in two dynamic buffers:
141224  **
141225  **   Buffer p: Points to the Fts3Expr structure, followed by the Fts3Phrase
141226  **             structure, followed by the array of Fts3PhraseToken
141227  **             structures. This pass only populates the Fts3PhraseToken array.
141228  **
141229  **   Buffer zTemp: Contains copies of all tokens.
141230  **
141231  ** The second pass, in the block that begins "if( rc==SQLITE_DONE )" below,
141232  ** appends buffer zTemp to buffer p, and fills in the Fts3Expr and Fts3Phrase
141233  ** structures.
141234  */
141235  rc = sqlite3Fts3OpenTokenizer(
141236      pTokenizer, pParse->iLangid, zInput, nInput, &pCursor);
141237  if( rc==SQLITE_OK ){
141238    int ii;
141239    for(ii=0; rc==SQLITE_OK; ii++){
141240      const char *zByte;
141241      int nByte = 0, iBegin = 0, iEnd = 0, iPos = 0;
141242      rc = pModule->xNext(pCursor, &zByte, &nByte, &iBegin, &iEnd, &iPos);
141243      if( rc==SQLITE_OK ){
141244        Fts3PhraseToken *pToken;
141245
141246        p = fts3ReallocOrFree(p, nSpace + ii*sizeof(Fts3PhraseToken));
141247        if( !p ) goto no_mem;
141248
141249        zTemp = fts3ReallocOrFree(zTemp, nTemp + nByte);
141250        if( !zTemp ) goto no_mem;
141251
141252        assert( nToken==ii );
141253        pToken = &((Fts3Phrase *)(&p[1]))->aToken[ii];
141254        memset(pToken, 0, sizeof(Fts3PhraseToken));
141255
141256        memcpy(&zTemp[nTemp], zByte, nByte);
141257        nTemp += nByte;
141258
141259        pToken->n = nByte;
141260        pToken->isPrefix = (iEnd<nInput && zInput[iEnd]=='*');
141261        pToken->bFirst = (iBegin>0 && zInput[iBegin-1]=='^');
141262        nToken = ii+1;
141263      }
141264    }
141265
141266    pModule->xClose(pCursor);
141267    pCursor = 0;
141268  }
141269
141270  if( rc==SQLITE_DONE ){
141271    int jj;
141272    char *zBuf = 0;
141273
141274    p = fts3ReallocOrFree(p, nSpace + nToken*sizeof(Fts3PhraseToken) + nTemp);
141275    if( !p ) goto no_mem;
141276    memset(p, 0, (char *)&(((Fts3Phrase *)&p[1])->aToken[0])-(char *)p);
141277    p->eType = FTSQUERY_PHRASE;
141278    p->pPhrase = (Fts3Phrase *)&p[1];
141279    p->pPhrase->iColumn = pParse->iDefaultCol;
141280    p->pPhrase->nToken = nToken;
141281
141282    zBuf = (char *)&p->pPhrase->aToken[nToken];
141283    if( zTemp ){
141284      memcpy(zBuf, zTemp, nTemp);
141285      sqlite3_free(zTemp);
141286    }else{
141287      assert( nTemp==0 );
141288    }
141289
141290    for(jj=0; jj<p->pPhrase->nToken; jj++){
141291      p->pPhrase->aToken[jj].z = zBuf;
141292      zBuf += p->pPhrase->aToken[jj].n;
141293    }
141294    rc = SQLITE_OK;
141295  }
141296
141297  *ppExpr = p;
141298  return rc;
141299no_mem:
141300
141301  if( pCursor ){
141302    pModule->xClose(pCursor);
141303  }
141304  sqlite3_free(zTemp);
141305  sqlite3_free(p);
141306  *ppExpr = 0;
141307  return SQLITE_NOMEM;
141308}
141309
141310/*
141311** The output variable *ppExpr is populated with an allocated Fts3Expr
141312** structure, or set to 0 if the end of the input buffer is reached.
141313**
141314** Returns an SQLite error code. SQLITE_OK if everything works, SQLITE_NOMEM
141315** if a malloc failure occurs, or SQLITE_ERROR if a parse error is encountered.
141316** If SQLITE_ERROR is returned, pContext is populated with an error message.
141317*/
141318static int getNextNode(
141319  ParseContext *pParse,                   /* fts3 query parse context */
141320  const char *z, int n,                   /* Input string */
141321  Fts3Expr **ppExpr,                      /* OUT: expression */
141322  int *pnConsumed                         /* OUT: Number of bytes consumed */
141323){
141324  static const struct Fts3Keyword {
141325    char *z;                              /* Keyword text */
141326    unsigned char n;                      /* Length of the keyword */
141327    unsigned char parenOnly;              /* Only valid in paren mode */
141328    unsigned char eType;                  /* Keyword code */
141329  } aKeyword[] = {
141330    { "OR" ,  2, 0, FTSQUERY_OR   },
141331    { "AND",  3, 1, FTSQUERY_AND  },
141332    { "NOT",  3, 1, FTSQUERY_NOT  },
141333    { "NEAR", 4, 0, FTSQUERY_NEAR }
141334  };
141335  int ii;
141336  int iCol;
141337  int iColLen;
141338  int rc;
141339  Fts3Expr *pRet = 0;
141340
141341  const char *zInput = z;
141342  int nInput = n;
141343
141344  pParse->isNot = 0;
141345
141346  /* Skip over any whitespace before checking for a keyword, an open or
141347  ** close bracket, or a quoted string.
141348  */
141349  while( nInput>0 && fts3isspace(*zInput) ){
141350    nInput--;
141351    zInput++;
141352  }
141353  if( nInput==0 ){
141354    return SQLITE_DONE;
141355  }
141356
141357  /* See if we are dealing with a keyword. */
141358  for(ii=0; ii<(int)(sizeof(aKeyword)/sizeof(struct Fts3Keyword)); ii++){
141359    const struct Fts3Keyword *pKey = &aKeyword[ii];
141360
141361    if( (pKey->parenOnly & ~sqlite3_fts3_enable_parentheses)!=0 ){
141362      continue;
141363    }
141364
141365    if( nInput>=pKey->n && 0==memcmp(zInput, pKey->z, pKey->n) ){
141366      int nNear = SQLITE_FTS3_DEFAULT_NEAR_PARAM;
141367      int nKey = pKey->n;
141368      char cNext;
141369
141370      /* If this is a "NEAR" keyword, check for an explicit nearness. */
141371      if( pKey->eType==FTSQUERY_NEAR ){
141372        assert( nKey==4 );
141373        if( zInput[4]=='/' && zInput[5]>='0' && zInput[5]<='9' ){
141374          nNear = 0;
141375          for(nKey=5; zInput[nKey]>='0' && zInput[nKey]<='9'; nKey++){
141376            nNear = nNear * 10 + (zInput[nKey] - '0');
141377          }
141378        }
141379      }
141380
141381      /* At this point this is probably a keyword. But for that to be true,
141382      ** the next byte must contain either whitespace, an open or close
141383      ** parenthesis, a quote character, or EOF.
141384      */
141385      cNext = zInput[nKey];
141386      if( fts3isspace(cNext)
141387       || cNext=='"' || cNext=='(' || cNext==')' || cNext==0
141388      ){
141389        pRet = (Fts3Expr *)fts3MallocZero(sizeof(Fts3Expr));
141390        if( !pRet ){
141391          return SQLITE_NOMEM;
141392        }
141393        pRet->eType = pKey->eType;
141394        pRet->nNear = nNear;
141395        *ppExpr = pRet;
141396        *pnConsumed = (int)((zInput - z) + nKey);
141397        return SQLITE_OK;
141398      }
141399
141400      /* Turns out that wasn't a keyword after all. This happens if the
141401      ** user has supplied a token such as "ORacle". Continue.
141402      */
141403    }
141404  }
141405
141406  /* See if we are dealing with a quoted phrase. If this is the case, then
141407  ** search for the closing quote and pass the whole string to getNextString()
141408  ** for processing. This is easy to do, as fts3 has no syntax for escaping
141409  ** a quote character embedded in a string.
141410  */
141411  if( *zInput=='"' ){
141412    for(ii=1; ii<nInput && zInput[ii]!='"'; ii++);
141413    *pnConsumed = (int)((zInput - z) + ii + 1);
141414    if( ii==nInput ){
141415      return SQLITE_ERROR;
141416    }
141417    return getNextString(pParse, &zInput[1], ii-1, ppExpr);
141418  }
141419
141420  if( sqlite3_fts3_enable_parentheses ){
141421    if( *zInput=='(' ){
141422      int nConsumed = 0;
141423      pParse->nNest++;
141424      rc = fts3ExprParse(pParse, zInput+1, nInput-1, ppExpr, &nConsumed);
141425      if( rc==SQLITE_OK && !*ppExpr ){ rc = SQLITE_DONE; }
141426      *pnConsumed = (int)(zInput - z) + 1 + nConsumed;
141427      return rc;
141428    }else if( *zInput==')' ){
141429      pParse->nNest--;
141430      *pnConsumed = (int)((zInput - z) + 1);
141431      *ppExpr = 0;
141432      return SQLITE_DONE;
141433    }
141434  }
141435
141436  /* If control flows to this point, this must be a regular token, or
141437  ** the end of the input. Read a regular token using the sqlite3_tokenizer
141438  ** interface. Before doing so, figure out if there is an explicit
141439  ** column specifier for the token.
141440  **
141441  ** TODO: Strangely, it is not possible to associate a column specifier
141442  ** with a quoted phrase, only with a single token. Not sure if this was
141443  ** an implementation artifact or an intentional decision when fts3 was
141444  ** first implemented. Whichever it was, this module duplicates the
141445  ** limitation.
141446  */
141447  iCol = pParse->iDefaultCol;
141448  iColLen = 0;
141449  for(ii=0; ii<pParse->nCol; ii++){
141450    const char *zStr = pParse->azCol[ii];
141451    int nStr = (int)strlen(zStr);
141452    if( nInput>nStr && zInput[nStr]==':'
141453     && sqlite3_strnicmp(zStr, zInput, nStr)==0
141454    ){
141455      iCol = ii;
141456      iColLen = (int)((zInput - z) + nStr + 1);
141457      break;
141458    }
141459  }
141460  rc = getNextToken(pParse, iCol, &z[iColLen], n-iColLen, ppExpr, pnConsumed);
141461  *pnConsumed += iColLen;
141462  return rc;
141463}
141464
141465/*
141466** The argument is an Fts3Expr structure for a binary operator (any type
141467** except an FTSQUERY_PHRASE). Return an integer value representing the
141468** precedence of the operator. Lower values have a higher precedence (i.e.
141469** group more tightly). For example, in the C language, the == operator
141470** groups more tightly than ||, and would therefore have a higher precedence.
141471**
141472** When using the new fts3 query syntax (when SQLITE_ENABLE_FTS3_PARENTHESIS
141473** is defined), the order of the operators in precedence from highest to
141474** lowest is:
141475**
141476**   NEAR
141477**   NOT
141478**   AND (including implicit ANDs)
141479**   OR
141480**
141481** Note that when using the old query syntax, the OR operator has a higher
141482** precedence than the AND operator.
141483*/
141484static int opPrecedence(Fts3Expr *p){
141485  assert( p->eType!=FTSQUERY_PHRASE );
141486  if( sqlite3_fts3_enable_parentheses ){
141487    return p->eType;
141488  }else if( p->eType==FTSQUERY_NEAR ){
141489    return 1;
141490  }else if( p->eType==FTSQUERY_OR ){
141491    return 2;
141492  }
141493  assert( p->eType==FTSQUERY_AND );
141494  return 3;
141495}
141496
141497/*
141498** Argument ppHead contains a pointer to the current head of a query
141499** expression tree being parsed. pPrev is the expression node most recently
141500** inserted into the tree. This function adds pNew, which is always a binary
141501** operator node, into the expression tree based on the relative precedence
141502** of pNew and the existing nodes of the tree. This may result in the head
141503** of the tree changing, in which case *ppHead is set to the new root node.
141504*/
141505static void insertBinaryOperator(
141506  Fts3Expr **ppHead,       /* Pointer to the root node of a tree */
141507  Fts3Expr *pPrev,         /* Node most recently inserted into the tree */
141508  Fts3Expr *pNew           /* New binary node to insert into expression tree */
141509){
141510  Fts3Expr *pSplit = pPrev;
141511  while( pSplit->pParent && opPrecedence(pSplit->pParent)<=opPrecedence(pNew) ){
141512    pSplit = pSplit->pParent;
141513  }
141514
141515  if( pSplit->pParent ){
141516    assert( pSplit->pParent->pRight==pSplit );
141517    pSplit->pParent->pRight = pNew;
141518    pNew->pParent = pSplit->pParent;
141519  }else{
141520    *ppHead = pNew;
141521  }
141522  pNew->pLeft = pSplit;
141523  pSplit->pParent = pNew;
141524}
141525
141526/*
141527** Parse the fts3 query expression found in buffer z, length n. This function
141528** returns either when the end of the buffer is reached or an unmatched
141529** closing bracket - ')' - is encountered.
141530**
141531** If successful, SQLITE_OK is returned, *ppExpr is set to point to the
141532** parsed form of the expression and *pnConsumed is set to the number of
141533** bytes read from buffer z. Otherwise, *ppExpr is set to 0 and SQLITE_NOMEM
141534** (out of memory error) or SQLITE_ERROR (parse error) is returned.
141535*/
141536static int fts3ExprParse(
141537  ParseContext *pParse,                   /* fts3 query parse context */
141538  const char *z, int n,                   /* Text of MATCH query */
141539  Fts3Expr **ppExpr,                      /* OUT: Parsed query structure */
141540  int *pnConsumed                         /* OUT: Number of bytes consumed */
141541){
141542  Fts3Expr *pRet = 0;
141543  Fts3Expr *pPrev = 0;
141544  Fts3Expr *pNotBranch = 0;               /* Only used in legacy parse mode */
141545  int nIn = n;
141546  const char *zIn = z;
141547  int rc = SQLITE_OK;
141548  int isRequirePhrase = 1;
141549
141550  while( rc==SQLITE_OK ){
141551    Fts3Expr *p = 0;
141552    int nByte = 0;
141553
141554    rc = getNextNode(pParse, zIn, nIn, &p, &nByte);
141555    assert( nByte>0 || (rc!=SQLITE_OK && p==0) );
141556    if( rc==SQLITE_OK ){
141557      if( p ){
141558        int isPhrase;
141559
141560        if( !sqlite3_fts3_enable_parentheses
141561            && p->eType==FTSQUERY_PHRASE && pParse->isNot
141562        ){
141563          /* Create an implicit NOT operator. */
141564          Fts3Expr *pNot = fts3MallocZero(sizeof(Fts3Expr));
141565          if( !pNot ){
141566            sqlite3Fts3ExprFree(p);
141567            rc = SQLITE_NOMEM;
141568            goto exprparse_out;
141569          }
141570          pNot->eType = FTSQUERY_NOT;
141571          pNot->pRight = p;
141572          p->pParent = pNot;
141573          if( pNotBranch ){
141574            pNot->pLeft = pNotBranch;
141575            pNotBranch->pParent = pNot;
141576          }
141577          pNotBranch = pNot;
141578          p = pPrev;
141579        }else{
141580          int eType = p->eType;
141581          isPhrase = (eType==FTSQUERY_PHRASE || p->pLeft);
141582
141583          /* The isRequirePhrase variable is set to true if a phrase or
141584          ** an expression contained in parenthesis is required. If a
141585          ** binary operator (AND, OR, NOT or NEAR) is encounted when
141586          ** isRequirePhrase is set, this is a syntax error.
141587          */
141588          if( !isPhrase && isRequirePhrase ){
141589            sqlite3Fts3ExprFree(p);
141590            rc = SQLITE_ERROR;
141591            goto exprparse_out;
141592          }
141593
141594          if( isPhrase && !isRequirePhrase ){
141595            /* Insert an implicit AND operator. */
141596            Fts3Expr *pAnd;
141597            assert( pRet && pPrev );
141598            pAnd = fts3MallocZero(sizeof(Fts3Expr));
141599            if( !pAnd ){
141600              sqlite3Fts3ExprFree(p);
141601              rc = SQLITE_NOMEM;
141602              goto exprparse_out;
141603            }
141604            pAnd->eType = FTSQUERY_AND;
141605            insertBinaryOperator(&pRet, pPrev, pAnd);
141606            pPrev = pAnd;
141607          }
141608
141609          /* This test catches attempts to make either operand of a NEAR
141610           ** operator something other than a phrase. For example, either of
141611           ** the following:
141612           **
141613           **    (bracketed expression) NEAR phrase
141614           **    phrase NEAR (bracketed expression)
141615           **
141616           ** Return an error in either case.
141617           */
141618          if( pPrev && (
141619            (eType==FTSQUERY_NEAR && !isPhrase && pPrev->eType!=FTSQUERY_PHRASE)
141620         || (eType!=FTSQUERY_PHRASE && isPhrase && pPrev->eType==FTSQUERY_NEAR)
141621          )){
141622            sqlite3Fts3ExprFree(p);
141623            rc = SQLITE_ERROR;
141624            goto exprparse_out;
141625          }
141626
141627          if( isPhrase ){
141628            if( pRet ){
141629              assert( pPrev && pPrev->pLeft && pPrev->pRight==0 );
141630              pPrev->pRight = p;
141631              p->pParent = pPrev;
141632            }else{
141633              pRet = p;
141634            }
141635          }else{
141636            insertBinaryOperator(&pRet, pPrev, p);
141637          }
141638          isRequirePhrase = !isPhrase;
141639        }
141640        pPrev = p;
141641      }
141642      assert( nByte>0 );
141643    }
141644    assert( rc!=SQLITE_OK || (nByte>0 && nByte<=nIn) );
141645    nIn -= nByte;
141646    zIn += nByte;
141647  }
141648
141649  if( rc==SQLITE_DONE && pRet && isRequirePhrase ){
141650    rc = SQLITE_ERROR;
141651  }
141652
141653  if( rc==SQLITE_DONE ){
141654    rc = SQLITE_OK;
141655    if( !sqlite3_fts3_enable_parentheses && pNotBranch ){
141656      if( !pRet ){
141657        rc = SQLITE_ERROR;
141658      }else{
141659        Fts3Expr *pIter = pNotBranch;
141660        while( pIter->pLeft ){
141661          pIter = pIter->pLeft;
141662        }
141663        pIter->pLeft = pRet;
141664        pRet->pParent = pIter;
141665        pRet = pNotBranch;
141666      }
141667    }
141668  }
141669  *pnConsumed = n - nIn;
141670
141671exprparse_out:
141672  if( rc!=SQLITE_OK ){
141673    sqlite3Fts3ExprFree(pRet);
141674    sqlite3Fts3ExprFree(pNotBranch);
141675    pRet = 0;
141676  }
141677  *ppExpr = pRet;
141678  return rc;
141679}
141680
141681/*
141682** Return SQLITE_ERROR if the maximum depth of the expression tree passed
141683** as the only argument is more than nMaxDepth.
141684*/
141685static int fts3ExprCheckDepth(Fts3Expr *p, int nMaxDepth){
141686  int rc = SQLITE_OK;
141687  if( p ){
141688    if( nMaxDepth<0 ){
141689      rc = SQLITE_TOOBIG;
141690    }else{
141691      rc = fts3ExprCheckDepth(p->pLeft, nMaxDepth-1);
141692      if( rc==SQLITE_OK ){
141693        rc = fts3ExprCheckDepth(p->pRight, nMaxDepth-1);
141694      }
141695    }
141696  }
141697  return rc;
141698}
141699
141700/*
141701** This function attempts to transform the expression tree at (*pp) to
141702** an equivalent but more balanced form. The tree is modified in place.
141703** If successful, SQLITE_OK is returned and (*pp) set to point to the
141704** new root expression node.
141705**
141706** nMaxDepth is the maximum allowable depth of the balanced sub-tree.
141707**
141708** Otherwise, if an error occurs, an SQLite error code is returned and
141709** expression (*pp) freed.
141710*/
141711static int fts3ExprBalance(Fts3Expr **pp, int nMaxDepth){
141712  int rc = SQLITE_OK;             /* Return code */
141713  Fts3Expr *pRoot = *pp;          /* Initial root node */
141714  Fts3Expr *pFree = 0;            /* List of free nodes. Linked by pParent. */
141715  int eType = pRoot->eType;       /* Type of node in this tree */
141716
141717  if( nMaxDepth==0 ){
141718    rc = SQLITE_ERROR;
141719  }
141720
141721  if( rc==SQLITE_OK && (eType==FTSQUERY_AND || eType==FTSQUERY_OR) ){
141722    Fts3Expr **apLeaf;
141723    apLeaf = (Fts3Expr **)sqlite3_malloc(sizeof(Fts3Expr *) * nMaxDepth);
141724    if( 0==apLeaf ){
141725      rc = SQLITE_NOMEM;
141726    }else{
141727      memset(apLeaf, 0, sizeof(Fts3Expr *) * nMaxDepth);
141728    }
141729
141730    if( rc==SQLITE_OK ){
141731      int i;
141732      Fts3Expr *p;
141733
141734      /* Set $p to point to the left-most leaf in the tree of eType nodes. */
141735      for(p=pRoot; p->eType==eType; p=p->pLeft){
141736        assert( p->pParent==0 || p->pParent->pLeft==p );
141737        assert( p->pLeft && p->pRight );
141738      }
141739
141740      /* This loop runs once for each leaf in the tree of eType nodes. */
141741      while( 1 ){
141742        int iLvl;
141743        Fts3Expr *pParent = p->pParent;     /* Current parent of p */
141744
141745        assert( pParent==0 || pParent->pLeft==p );
141746        p->pParent = 0;
141747        if( pParent ){
141748          pParent->pLeft = 0;
141749        }else{
141750          pRoot = 0;
141751        }
141752        rc = fts3ExprBalance(&p, nMaxDepth-1);
141753        if( rc!=SQLITE_OK ) break;
141754
141755        for(iLvl=0; p && iLvl<nMaxDepth; iLvl++){
141756          if( apLeaf[iLvl]==0 ){
141757            apLeaf[iLvl] = p;
141758            p = 0;
141759          }else{
141760            assert( pFree );
141761            pFree->pLeft = apLeaf[iLvl];
141762            pFree->pRight = p;
141763            pFree->pLeft->pParent = pFree;
141764            pFree->pRight->pParent = pFree;
141765
141766            p = pFree;
141767            pFree = pFree->pParent;
141768            p->pParent = 0;
141769            apLeaf[iLvl] = 0;
141770          }
141771        }
141772        if( p ){
141773          sqlite3Fts3ExprFree(p);
141774          rc = SQLITE_TOOBIG;
141775          break;
141776        }
141777
141778        /* If that was the last leaf node, break out of the loop */
141779        if( pParent==0 ) break;
141780
141781        /* Set $p to point to the next leaf in the tree of eType nodes */
141782        for(p=pParent->pRight; p->eType==eType; p=p->pLeft);
141783
141784        /* Remove pParent from the original tree. */
141785        assert( pParent->pParent==0 || pParent->pParent->pLeft==pParent );
141786        pParent->pRight->pParent = pParent->pParent;
141787        if( pParent->pParent ){
141788          pParent->pParent->pLeft = pParent->pRight;
141789        }else{
141790          assert( pParent==pRoot );
141791          pRoot = pParent->pRight;
141792        }
141793
141794        /* Link pParent into the free node list. It will be used as an
141795        ** internal node of the new tree.  */
141796        pParent->pParent = pFree;
141797        pFree = pParent;
141798      }
141799
141800      if( rc==SQLITE_OK ){
141801        p = 0;
141802        for(i=0; i<nMaxDepth; i++){
141803          if( apLeaf[i] ){
141804            if( p==0 ){
141805              p = apLeaf[i];
141806              p->pParent = 0;
141807            }else{
141808              assert( pFree!=0 );
141809              pFree->pRight = p;
141810              pFree->pLeft = apLeaf[i];
141811              pFree->pLeft->pParent = pFree;
141812              pFree->pRight->pParent = pFree;
141813
141814              p = pFree;
141815              pFree = pFree->pParent;
141816              p->pParent = 0;
141817            }
141818          }
141819        }
141820        pRoot = p;
141821      }else{
141822        /* An error occurred. Delete the contents of the apLeaf[] array
141823        ** and pFree list. Everything else is cleaned up by the call to
141824        ** sqlite3Fts3ExprFree(pRoot) below.  */
141825        Fts3Expr *pDel;
141826        for(i=0; i<nMaxDepth; i++){
141827          sqlite3Fts3ExprFree(apLeaf[i]);
141828        }
141829        while( (pDel=pFree)!=0 ){
141830          pFree = pDel->pParent;
141831          sqlite3_free(pDel);
141832        }
141833      }
141834
141835      assert( pFree==0 );
141836      sqlite3_free( apLeaf );
141837    }
141838  }
141839
141840  if( rc!=SQLITE_OK ){
141841    sqlite3Fts3ExprFree(pRoot);
141842    pRoot = 0;
141843  }
141844  *pp = pRoot;
141845  return rc;
141846}
141847
141848/*
141849** This function is similar to sqlite3Fts3ExprParse(), with the following
141850** differences:
141851**
141852**   1. It does not do expression rebalancing.
141853**   2. It does not check that the expression does not exceed the
141854**      maximum allowable depth.
141855**   3. Even if it fails, *ppExpr may still be set to point to an
141856**      expression tree. It should be deleted using sqlite3Fts3ExprFree()
141857**      in this case.
141858*/
141859static int fts3ExprParseUnbalanced(
141860  sqlite3_tokenizer *pTokenizer,      /* Tokenizer module */
141861  int iLangid,                        /* Language id for tokenizer */
141862  char **azCol,                       /* Array of column names for fts3 table */
141863  int bFts4,                          /* True to allow FTS4-only syntax */
141864  int nCol,                           /* Number of entries in azCol[] */
141865  int iDefaultCol,                    /* Default column to query */
141866  const char *z, int n,               /* Text of MATCH query */
141867  Fts3Expr **ppExpr                   /* OUT: Parsed query structure */
141868){
141869  int nParsed;
141870  int rc;
141871  ParseContext sParse;
141872
141873  memset(&sParse, 0, sizeof(ParseContext));
141874  sParse.pTokenizer = pTokenizer;
141875  sParse.iLangid = iLangid;
141876  sParse.azCol = (const char **)azCol;
141877  sParse.nCol = nCol;
141878  sParse.iDefaultCol = iDefaultCol;
141879  sParse.bFts4 = bFts4;
141880  if( z==0 ){
141881    *ppExpr = 0;
141882    return SQLITE_OK;
141883  }
141884  if( n<0 ){
141885    n = (int)strlen(z);
141886  }
141887  rc = fts3ExprParse(&sParse, z, n, ppExpr, &nParsed);
141888  assert( rc==SQLITE_OK || *ppExpr==0 );
141889
141890  /* Check for mismatched parenthesis */
141891  if( rc==SQLITE_OK && sParse.nNest ){
141892    rc = SQLITE_ERROR;
141893  }
141894
141895  return rc;
141896}
141897
141898/*
141899** Parameters z and n contain a pointer to and length of a buffer containing
141900** an fts3 query expression, respectively. This function attempts to parse the
141901** query expression and create a tree of Fts3Expr structures representing the
141902** parsed expression. If successful, *ppExpr is set to point to the head
141903** of the parsed expression tree and SQLITE_OK is returned. If an error
141904** occurs, either SQLITE_NOMEM (out-of-memory error) or SQLITE_ERROR (parse
141905** error) is returned and *ppExpr is set to 0.
141906**
141907** If parameter n is a negative number, then z is assumed to point to a
141908** nul-terminated string and the length is determined using strlen().
141909**
141910** The first parameter, pTokenizer, is passed the fts3 tokenizer module to
141911** use to normalize query tokens while parsing the expression. The azCol[]
141912** array, which is assumed to contain nCol entries, should contain the names
141913** of each column in the target fts3 table, in order from left to right.
141914** Column names must be nul-terminated strings.
141915**
141916** The iDefaultCol parameter should be passed the index of the table column
141917** that appears on the left-hand-side of the MATCH operator (the default
141918** column to match against for tokens for which a column name is not explicitly
141919** specified as part of the query string), or -1 if tokens may by default
141920** match any table column.
141921*/
141922SQLITE_PRIVATE int sqlite3Fts3ExprParse(
141923  sqlite3_tokenizer *pTokenizer,      /* Tokenizer module */
141924  int iLangid,                        /* Language id for tokenizer */
141925  char **azCol,                       /* Array of column names for fts3 table */
141926  int bFts4,                          /* True to allow FTS4-only syntax */
141927  int nCol,                           /* Number of entries in azCol[] */
141928  int iDefaultCol,                    /* Default column to query */
141929  const char *z, int n,               /* Text of MATCH query */
141930  Fts3Expr **ppExpr,                  /* OUT: Parsed query structure */
141931  char **pzErr                        /* OUT: Error message (sqlite3_malloc) */
141932){
141933  int rc = fts3ExprParseUnbalanced(
141934      pTokenizer, iLangid, azCol, bFts4, nCol, iDefaultCol, z, n, ppExpr
141935  );
141936
141937  /* Rebalance the expression. And check that its depth does not exceed
141938  ** SQLITE_FTS3_MAX_EXPR_DEPTH.  */
141939  if( rc==SQLITE_OK && *ppExpr ){
141940    rc = fts3ExprBalance(ppExpr, SQLITE_FTS3_MAX_EXPR_DEPTH);
141941    if( rc==SQLITE_OK ){
141942      rc = fts3ExprCheckDepth(*ppExpr, SQLITE_FTS3_MAX_EXPR_DEPTH);
141943    }
141944  }
141945
141946  if( rc!=SQLITE_OK ){
141947    sqlite3Fts3ExprFree(*ppExpr);
141948    *ppExpr = 0;
141949    if( rc==SQLITE_TOOBIG ){
141950      sqlite3Fts3ErrMsg(pzErr,
141951          "FTS expression tree is too large (maximum depth %d)",
141952          SQLITE_FTS3_MAX_EXPR_DEPTH
141953      );
141954      rc = SQLITE_ERROR;
141955    }else if( rc==SQLITE_ERROR ){
141956      sqlite3Fts3ErrMsg(pzErr, "malformed MATCH expression: [%s]", z);
141957    }
141958  }
141959
141960  return rc;
141961}
141962
141963/*
141964** Free a single node of an expression tree.
141965*/
141966static void fts3FreeExprNode(Fts3Expr *p){
141967  assert( p->eType==FTSQUERY_PHRASE || p->pPhrase==0 );
141968  sqlite3Fts3EvalPhraseCleanup(p->pPhrase);
141969  sqlite3_free(p->aMI);
141970  sqlite3_free(p);
141971}
141972
141973/*
141974** Free a parsed fts3 query expression allocated by sqlite3Fts3ExprParse().
141975**
141976** This function would be simpler if it recursively called itself. But
141977** that would mean passing a sufficiently large expression to ExprParse()
141978** could cause a stack overflow.
141979*/
141980SQLITE_PRIVATE void sqlite3Fts3ExprFree(Fts3Expr *pDel){
141981  Fts3Expr *p;
141982  assert( pDel==0 || pDel->pParent==0 );
141983  for(p=pDel; p && (p->pLeft||p->pRight); p=(p->pLeft ? p->pLeft : p->pRight)){
141984    assert( p->pParent==0 || p==p->pParent->pRight || p==p->pParent->pLeft );
141985  }
141986  while( p ){
141987    Fts3Expr *pParent = p->pParent;
141988    fts3FreeExprNode(p);
141989    if( pParent && p==pParent->pLeft && pParent->pRight ){
141990      p = pParent->pRight;
141991      while( p && (p->pLeft || p->pRight) ){
141992        assert( p==p->pParent->pRight || p==p->pParent->pLeft );
141993        p = (p->pLeft ? p->pLeft : p->pRight);
141994      }
141995    }else{
141996      p = pParent;
141997    }
141998  }
141999}
142000
142001/****************************************************************************
142002*****************************************************************************
142003** Everything after this point is just test code.
142004*/
142005
142006#ifdef SQLITE_TEST
142007
142008/* #include <stdio.h> */
142009
142010/*
142011** Function to query the hash-table of tokenizers (see README.tokenizers).
142012*/
142013static int queryTestTokenizer(
142014  sqlite3 *db,
142015  const char *zName,
142016  const sqlite3_tokenizer_module **pp
142017){
142018  int rc;
142019  sqlite3_stmt *pStmt;
142020  const char zSql[] = "SELECT fts3_tokenizer(?)";
142021
142022  *pp = 0;
142023  rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
142024  if( rc!=SQLITE_OK ){
142025    return rc;
142026  }
142027
142028  sqlite3_bind_text(pStmt, 1, zName, -1, SQLITE_STATIC);
142029  if( SQLITE_ROW==sqlite3_step(pStmt) ){
142030    if( sqlite3_column_type(pStmt, 0)==SQLITE_BLOB ){
142031      memcpy((void *)pp, sqlite3_column_blob(pStmt, 0), sizeof(*pp));
142032    }
142033  }
142034
142035  return sqlite3_finalize(pStmt);
142036}
142037
142038/*
142039** Return a pointer to a buffer containing a text representation of the
142040** expression passed as the first argument. The buffer is obtained from
142041** sqlite3_malloc(). It is the responsibility of the caller to use
142042** sqlite3_free() to release the memory. If an OOM condition is encountered,
142043** NULL is returned.
142044**
142045** If the second argument is not NULL, then its contents are prepended to
142046** the returned expression text and then freed using sqlite3_free().
142047*/
142048static char *exprToString(Fts3Expr *pExpr, char *zBuf){
142049  if( pExpr==0 ){
142050    return sqlite3_mprintf("");
142051  }
142052  switch( pExpr->eType ){
142053    case FTSQUERY_PHRASE: {
142054      Fts3Phrase *pPhrase = pExpr->pPhrase;
142055      int i;
142056      zBuf = sqlite3_mprintf(
142057          "%zPHRASE %d 0", zBuf, pPhrase->iColumn);
142058      for(i=0; zBuf && i<pPhrase->nToken; i++){
142059        zBuf = sqlite3_mprintf("%z %.*s%s", zBuf,
142060            pPhrase->aToken[i].n, pPhrase->aToken[i].z,
142061            (pPhrase->aToken[i].isPrefix?"+":"")
142062        );
142063      }
142064      return zBuf;
142065    }
142066
142067    case FTSQUERY_NEAR:
142068      zBuf = sqlite3_mprintf("%zNEAR/%d ", zBuf, pExpr->nNear);
142069      break;
142070    case FTSQUERY_NOT:
142071      zBuf = sqlite3_mprintf("%zNOT ", zBuf);
142072      break;
142073    case FTSQUERY_AND:
142074      zBuf = sqlite3_mprintf("%zAND ", zBuf);
142075      break;
142076    case FTSQUERY_OR:
142077      zBuf = sqlite3_mprintf("%zOR ", zBuf);
142078      break;
142079  }
142080
142081  if( zBuf ) zBuf = sqlite3_mprintf("%z{", zBuf);
142082  if( zBuf ) zBuf = exprToString(pExpr->pLeft, zBuf);
142083  if( zBuf ) zBuf = sqlite3_mprintf("%z} {", zBuf);
142084
142085  if( zBuf ) zBuf = exprToString(pExpr->pRight, zBuf);
142086  if( zBuf ) zBuf = sqlite3_mprintf("%z}", zBuf);
142087
142088  return zBuf;
142089}
142090
142091/*
142092** This is the implementation of a scalar SQL function used to test the
142093** expression parser. It should be called as follows:
142094**
142095**   fts3_exprtest(<tokenizer>, <expr>, <column 1>, ...);
142096**
142097** The first argument, <tokenizer>, is the name of the fts3 tokenizer used
142098** to parse the query expression (see README.tokenizers). The second argument
142099** is the query expression to parse. Each subsequent argument is the name
142100** of a column of the fts3 table that the query expression may refer to.
142101** For example:
142102**
142103**   SELECT fts3_exprtest('simple', 'Bill col2:Bloggs', 'col1', 'col2');
142104*/
142105static void fts3ExprTest(
142106  sqlite3_context *context,
142107  int argc,
142108  sqlite3_value **argv
142109){
142110  sqlite3_tokenizer_module const *pModule = 0;
142111  sqlite3_tokenizer *pTokenizer = 0;
142112  int rc;
142113  char **azCol = 0;
142114  const char *zExpr;
142115  int nExpr;
142116  int nCol;
142117  int ii;
142118  Fts3Expr *pExpr;
142119  char *zBuf = 0;
142120  sqlite3 *db = sqlite3_context_db_handle(context);
142121
142122  if( argc<3 ){
142123    sqlite3_result_error(context,
142124        "Usage: fts3_exprtest(tokenizer, expr, col1, ...", -1
142125    );
142126    return;
142127  }
142128
142129  rc = queryTestTokenizer(db,
142130                          (const char *)sqlite3_value_text(argv[0]), &pModule);
142131  if( rc==SQLITE_NOMEM ){
142132    sqlite3_result_error_nomem(context);
142133    goto exprtest_out;
142134  }else if( !pModule ){
142135    sqlite3_result_error(context, "No such tokenizer module", -1);
142136    goto exprtest_out;
142137  }
142138
142139  rc = pModule->xCreate(0, 0, &pTokenizer);
142140  assert( rc==SQLITE_NOMEM || rc==SQLITE_OK );
142141  if( rc==SQLITE_NOMEM ){
142142    sqlite3_result_error_nomem(context);
142143    goto exprtest_out;
142144  }
142145  pTokenizer->pModule = pModule;
142146
142147  zExpr = (const char *)sqlite3_value_text(argv[1]);
142148  nExpr = sqlite3_value_bytes(argv[1]);
142149  nCol = argc-2;
142150  azCol = (char **)sqlite3_malloc(nCol*sizeof(char *));
142151  if( !azCol ){
142152    sqlite3_result_error_nomem(context);
142153    goto exprtest_out;
142154  }
142155  for(ii=0; ii<nCol; ii++){
142156    azCol[ii] = (char *)sqlite3_value_text(argv[ii+2]);
142157  }
142158
142159  if( sqlite3_user_data(context) ){
142160    char *zDummy = 0;
142161    rc = sqlite3Fts3ExprParse(
142162        pTokenizer, 0, azCol, 0, nCol, nCol, zExpr, nExpr, &pExpr, &zDummy
142163    );
142164    assert( rc==SQLITE_OK || pExpr==0 );
142165    sqlite3_free(zDummy);
142166  }else{
142167    rc = fts3ExprParseUnbalanced(
142168        pTokenizer, 0, azCol, 0, nCol, nCol, zExpr, nExpr, &pExpr
142169    );
142170  }
142171
142172  if( rc!=SQLITE_OK && rc!=SQLITE_NOMEM ){
142173    sqlite3Fts3ExprFree(pExpr);
142174    sqlite3_result_error(context, "Error parsing expression", -1);
142175  }else if( rc==SQLITE_NOMEM || !(zBuf = exprToString(pExpr, 0)) ){
142176    sqlite3_result_error_nomem(context);
142177  }else{
142178    sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT);
142179    sqlite3_free(zBuf);
142180  }
142181
142182  sqlite3Fts3ExprFree(pExpr);
142183
142184exprtest_out:
142185  if( pModule && pTokenizer ){
142186    rc = pModule->xDestroy(pTokenizer);
142187  }
142188  sqlite3_free(azCol);
142189}
142190
142191/*
142192** Register the query expression parser test function fts3_exprtest()
142193** with database connection db.
142194*/
142195SQLITE_PRIVATE int sqlite3Fts3ExprInitTestInterface(sqlite3* db){
142196  int rc = sqlite3_create_function(
142197      db, "fts3_exprtest", -1, SQLITE_UTF8, 0, fts3ExprTest, 0, 0
142198  );
142199  if( rc==SQLITE_OK ){
142200    rc = sqlite3_create_function(db, "fts3_exprtest_rebalance",
142201        -1, SQLITE_UTF8, (void *)1, fts3ExprTest, 0, 0
142202    );
142203  }
142204  return rc;
142205}
142206
142207#endif
142208#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */
142209
142210/************** End of fts3_expr.c *******************************************/
142211/************** Begin file fts3_hash.c ***************************************/
142212/*
142213** 2001 September 22
142214**
142215** The author disclaims copyright to this source code.  In place of
142216** a legal notice, here is a blessing:
142217**
142218**    May you do good and not evil.
142219**    May you find forgiveness for yourself and forgive others.
142220**    May you share freely, never taking more than you give.
142221**
142222*************************************************************************
142223** This is the implementation of generic hash-tables used in SQLite.
142224** We've modified it slightly to serve as a standalone hash table
142225** implementation for the full-text indexing module.
142226*/
142227
142228/*
142229** The code in this file is only compiled if:
142230**
142231**     * The FTS3 module is being built as an extension
142232**       (in which case SQLITE_CORE is not defined), or
142233**
142234**     * The FTS3 module is being built into the core of
142235**       SQLite (in which case SQLITE_ENABLE_FTS3 is defined).
142236*/
142237/* #include "fts3Int.h" */
142238#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
142239
142240/* #include <assert.h> */
142241/* #include <stdlib.h> */
142242/* #include <string.h> */
142243
142244/* #include "fts3_hash.h" */
142245
142246/*
142247** Malloc and Free functions
142248*/
142249static void *fts3HashMalloc(int n){
142250  void *p = sqlite3_malloc(n);
142251  if( p ){
142252    memset(p, 0, n);
142253  }
142254  return p;
142255}
142256static void fts3HashFree(void *p){
142257  sqlite3_free(p);
142258}
142259
142260/* Turn bulk memory into a hash table object by initializing the
142261** fields of the Hash structure.
142262**
142263** "pNew" is a pointer to the hash table that is to be initialized.
142264** keyClass is one of the constants
142265** FTS3_HASH_BINARY or FTS3_HASH_STRING.  The value of keyClass
142266** determines what kind of key the hash table will use.  "copyKey" is
142267** true if the hash table should make its own private copy of keys and
142268** false if it should just use the supplied pointer.
142269*/
142270SQLITE_PRIVATE void sqlite3Fts3HashInit(Fts3Hash *pNew, char keyClass, char copyKey){
142271  assert( pNew!=0 );
142272  assert( keyClass>=FTS3_HASH_STRING && keyClass<=FTS3_HASH_BINARY );
142273  pNew->keyClass = keyClass;
142274  pNew->copyKey = copyKey;
142275  pNew->first = 0;
142276  pNew->count = 0;
142277  pNew->htsize = 0;
142278  pNew->ht = 0;
142279}
142280
142281/* Remove all entries from a hash table.  Reclaim all memory.
142282** Call this routine to delete a hash table or to reset a hash table
142283** to the empty state.
142284*/
142285SQLITE_PRIVATE void sqlite3Fts3HashClear(Fts3Hash *pH){
142286  Fts3HashElem *elem;         /* For looping over all elements of the table */
142287
142288  assert( pH!=0 );
142289  elem = pH->first;
142290  pH->first = 0;
142291  fts3HashFree(pH->ht);
142292  pH->ht = 0;
142293  pH->htsize = 0;
142294  while( elem ){
142295    Fts3HashElem *next_elem = elem->next;
142296    if( pH->copyKey && elem->pKey ){
142297      fts3HashFree(elem->pKey);
142298    }
142299    fts3HashFree(elem);
142300    elem = next_elem;
142301  }
142302  pH->count = 0;
142303}
142304
142305/*
142306** Hash and comparison functions when the mode is FTS3_HASH_STRING
142307*/
142308static int fts3StrHash(const void *pKey, int nKey){
142309  const char *z = (const char *)pKey;
142310  unsigned h = 0;
142311  if( nKey<=0 ) nKey = (int) strlen(z);
142312  while( nKey > 0  ){
142313    h = (h<<3) ^ h ^ *z++;
142314    nKey--;
142315  }
142316  return (int)(h & 0x7fffffff);
142317}
142318static int fts3StrCompare(const void *pKey1, int n1, const void *pKey2, int n2){
142319  if( n1!=n2 ) return 1;
142320  return strncmp((const char*)pKey1,(const char*)pKey2,n1);
142321}
142322
142323/*
142324** Hash and comparison functions when the mode is FTS3_HASH_BINARY
142325*/
142326static int fts3BinHash(const void *pKey, int nKey){
142327  int h = 0;
142328  const char *z = (const char *)pKey;
142329  while( nKey-- > 0 ){
142330    h = (h<<3) ^ h ^ *(z++);
142331  }
142332  return h & 0x7fffffff;
142333}
142334static int fts3BinCompare(const void *pKey1, int n1, const void *pKey2, int n2){
142335  if( n1!=n2 ) return 1;
142336  return memcmp(pKey1,pKey2,n1);
142337}
142338
142339/*
142340** Return a pointer to the appropriate hash function given the key class.
142341**
142342** The C syntax in this function definition may be unfamilar to some
142343** programmers, so we provide the following additional explanation:
142344**
142345** The name of the function is "ftsHashFunction".  The function takes a
142346** single parameter "keyClass".  The return value of ftsHashFunction()
142347** is a pointer to another function.  Specifically, the return value
142348** of ftsHashFunction() is a pointer to a function that takes two parameters
142349** with types "const void*" and "int" and returns an "int".
142350*/
142351static int (*ftsHashFunction(int keyClass))(const void*,int){
142352  if( keyClass==FTS3_HASH_STRING ){
142353    return &fts3StrHash;
142354  }else{
142355    assert( keyClass==FTS3_HASH_BINARY );
142356    return &fts3BinHash;
142357  }
142358}
142359
142360/*
142361** Return a pointer to the appropriate hash function given the key class.
142362**
142363** For help in interpreted the obscure C code in the function definition,
142364** see the header comment on the previous function.
142365*/
142366static int (*ftsCompareFunction(int keyClass))(const void*,int,const void*,int){
142367  if( keyClass==FTS3_HASH_STRING ){
142368    return &fts3StrCompare;
142369  }else{
142370    assert( keyClass==FTS3_HASH_BINARY );
142371    return &fts3BinCompare;
142372  }
142373}
142374
142375/* Link an element into the hash table
142376*/
142377static void fts3HashInsertElement(
142378  Fts3Hash *pH,            /* The complete hash table */
142379  struct _fts3ht *pEntry,  /* The entry into which pNew is inserted */
142380  Fts3HashElem *pNew       /* The element to be inserted */
142381){
142382  Fts3HashElem *pHead;     /* First element already in pEntry */
142383  pHead = pEntry->chain;
142384  if( pHead ){
142385    pNew->next = pHead;
142386    pNew->prev = pHead->prev;
142387    if( pHead->prev ){ pHead->prev->next = pNew; }
142388    else             { pH->first = pNew; }
142389    pHead->prev = pNew;
142390  }else{
142391    pNew->next = pH->first;
142392    if( pH->first ){ pH->first->prev = pNew; }
142393    pNew->prev = 0;
142394    pH->first = pNew;
142395  }
142396  pEntry->count++;
142397  pEntry->chain = pNew;
142398}
142399
142400
142401/* Resize the hash table so that it cantains "new_size" buckets.
142402** "new_size" must be a power of 2.  The hash table might fail
142403** to resize if sqliteMalloc() fails.
142404**
142405** Return non-zero if a memory allocation error occurs.
142406*/
142407static int fts3Rehash(Fts3Hash *pH, int new_size){
142408  struct _fts3ht *new_ht;          /* The new hash table */
142409  Fts3HashElem *elem, *next_elem;  /* For looping over existing elements */
142410  int (*xHash)(const void*,int);   /* The hash function */
142411
142412  assert( (new_size & (new_size-1))==0 );
142413  new_ht = (struct _fts3ht *)fts3HashMalloc( new_size*sizeof(struct _fts3ht) );
142414  if( new_ht==0 ) return 1;
142415  fts3HashFree(pH->ht);
142416  pH->ht = new_ht;
142417  pH->htsize = new_size;
142418  xHash = ftsHashFunction(pH->keyClass);
142419  for(elem=pH->first, pH->first=0; elem; elem = next_elem){
142420    int h = (*xHash)(elem->pKey, elem->nKey) & (new_size-1);
142421    next_elem = elem->next;
142422    fts3HashInsertElement(pH, &new_ht[h], elem);
142423  }
142424  return 0;
142425}
142426
142427/* This function (for internal use only) locates an element in an
142428** hash table that matches the given key.  The hash for this key has
142429** already been computed and is passed as the 4th parameter.
142430*/
142431static Fts3HashElem *fts3FindElementByHash(
142432  const Fts3Hash *pH, /* The pH to be searched */
142433  const void *pKey,   /* The key we are searching for */
142434  int nKey,
142435  int h               /* The hash for this key. */
142436){
142437  Fts3HashElem *elem;            /* Used to loop thru the element list */
142438  int count;                     /* Number of elements left to test */
142439  int (*xCompare)(const void*,int,const void*,int);  /* comparison function */
142440
142441  if( pH->ht ){
142442    struct _fts3ht *pEntry = &pH->ht[h];
142443    elem = pEntry->chain;
142444    count = pEntry->count;
142445    xCompare = ftsCompareFunction(pH->keyClass);
142446    while( count-- && elem ){
142447      if( (*xCompare)(elem->pKey,elem->nKey,pKey,nKey)==0 ){
142448        return elem;
142449      }
142450      elem = elem->next;
142451    }
142452  }
142453  return 0;
142454}
142455
142456/* Remove a single entry from the hash table given a pointer to that
142457** element and a hash on the element's key.
142458*/
142459static void fts3RemoveElementByHash(
142460  Fts3Hash *pH,         /* The pH containing "elem" */
142461  Fts3HashElem* elem,   /* The element to be removed from the pH */
142462  int h                 /* Hash value for the element */
142463){
142464  struct _fts3ht *pEntry;
142465  if( elem->prev ){
142466    elem->prev->next = elem->next;
142467  }else{
142468    pH->first = elem->next;
142469  }
142470  if( elem->next ){
142471    elem->next->prev = elem->prev;
142472  }
142473  pEntry = &pH->ht[h];
142474  if( pEntry->chain==elem ){
142475    pEntry->chain = elem->next;
142476  }
142477  pEntry->count--;
142478  if( pEntry->count<=0 ){
142479    pEntry->chain = 0;
142480  }
142481  if( pH->copyKey && elem->pKey ){
142482    fts3HashFree(elem->pKey);
142483  }
142484  fts3HashFree( elem );
142485  pH->count--;
142486  if( pH->count<=0 ){
142487    assert( pH->first==0 );
142488    assert( pH->count==0 );
142489    fts3HashClear(pH);
142490  }
142491}
142492
142493SQLITE_PRIVATE Fts3HashElem *sqlite3Fts3HashFindElem(
142494  const Fts3Hash *pH,
142495  const void *pKey,
142496  int nKey
142497){
142498  int h;                          /* A hash on key */
142499  int (*xHash)(const void*,int);  /* The hash function */
142500
142501  if( pH==0 || pH->ht==0 ) return 0;
142502  xHash = ftsHashFunction(pH->keyClass);
142503  assert( xHash!=0 );
142504  h = (*xHash)(pKey,nKey);
142505  assert( (pH->htsize & (pH->htsize-1))==0 );
142506  return fts3FindElementByHash(pH,pKey,nKey, h & (pH->htsize-1));
142507}
142508
142509/*
142510** Attempt to locate an element of the hash table pH with a key
142511** that matches pKey,nKey.  Return the data for this element if it is
142512** found, or NULL if there is no match.
142513*/
142514SQLITE_PRIVATE void *sqlite3Fts3HashFind(const Fts3Hash *pH, const void *pKey, int nKey){
142515  Fts3HashElem *pElem;            /* The element that matches key (if any) */
142516
142517  pElem = sqlite3Fts3HashFindElem(pH, pKey, nKey);
142518  return pElem ? pElem->data : 0;
142519}
142520
142521/* Insert an element into the hash table pH.  The key is pKey,nKey
142522** and the data is "data".
142523**
142524** If no element exists with a matching key, then a new
142525** element is created.  A copy of the key is made if the copyKey
142526** flag is set.  NULL is returned.
142527**
142528** If another element already exists with the same key, then the
142529** new data replaces the old data and the old data is returned.
142530** The key is not copied in this instance.  If a malloc fails, then
142531** the new data is returned and the hash table is unchanged.
142532**
142533** If the "data" parameter to this function is NULL, then the
142534** element corresponding to "key" is removed from the hash table.
142535*/
142536SQLITE_PRIVATE void *sqlite3Fts3HashInsert(
142537  Fts3Hash *pH,        /* The hash table to insert into */
142538  const void *pKey,    /* The key */
142539  int nKey,            /* Number of bytes in the key */
142540  void *data           /* The data */
142541){
142542  int hraw;                 /* Raw hash value of the key */
142543  int h;                    /* the hash of the key modulo hash table size */
142544  Fts3HashElem *elem;       /* Used to loop thru the element list */
142545  Fts3HashElem *new_elem;   /* New element added to the pH */
142546  int (*xHash)(const void*,int);  /* The hash function */
142547
142548  assert( pH!=0 );
142549  xHash = ftsHashFunction(pH->keyClass);
142550  assert( xHash!=0 );
142551  hraw = (*xHash)(pKey, nKey);
142552  assert( (pH->htsize & (pH->htsize-1))==0 );
142553  h = hraw & (pH->htsize-1);
142554  elem = fts3FindElementByHash(pH,pKey,nKey,h);
142555  if( elem ){
142556    void *old_data = elem->data;
142557    if( data==0 ){
142558      fts3RemoveElementByHash(pH,elem,h);
142559    }else{
142560      elem->data = data;
142561    }
142562    return old_data;
142563  }
142564  if( data==0 ) return 0;
142565  if( (pH->htsize==0 && fts3Rehash(pH,8))
142566   || (pH->count>=pH->htsize && fts3Rehash(pH, pH->htsize*2))
142567  ){
142568    pH->count = 0;
142569    return data;
142570  }
142571  assert( pH->htsize>0 );
142572  new_elem = (Fts3HashElem*)fts3HashMalloc( sizeof(Fts3HashElem) );
142573  if( new_elem==0 ) return data;
142574  if( pH->copyKey && pKey!=0 ){
142575    new_elem->pKey = fts3HashMalloc( nKey );
142576    if( new_elem->pKey==0 ){
142577      fts3HashFree(new_elem);
142578      return data;
142579    }
142580    memcpy((void*)new_elem->pKey, pKey, nKey);
142581  }else{
142582    new_elem->pKey = (void*)pKey;
142583  }
142584  new_elem->nKey = nKey;
142585  pH->count++;
142586  assert( pH->htsize>0 );
142587  assert( (pH->htsize & (pH->htsize-1))==0 );
142588  h = hraw & (pH->htsize-1);
142589  fts3HashInsertElement(pH, &pH->ht[h], new_elem);
142590  new_elem->data = data;
142591  return 0;
142592}
142593
142594#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */
142595
142596/************** End of fts3_hash.c *******************************************/
142597/************** Begin file fts3_porter.c *************************************/
142598/*
142599** 2006 September 30
142600**
142601** The author disclaims copyright to this source code.  In place of
142602** a legal notice, here is a blessing:
142603**
142604**    May you do good and not evil.
142605**    May you find forgiveness for yourself and forgive others.
142606**    May you share freely, never taking more than you give.
142607**
142608*************************************************************************
142609** Implementation of the full-text-search tokenizer that implements
142610** a Porter stemmer.
142611*/
142612
142613/*
142614** The code in this file is only compiled if:
142615**
142616**     * The FTS3 module is being built as an extension
142617**       (in which case SQLITE_CORE is not defined), or
142618**
142619**     * The FTS3 module is being built into the core of
142620**       SQLite (in which case SQLITE_ENABLE_FTS3 is defined).
142621*/
142622/* #include "fts3Int.h" */
142623#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
142624
142625/* #include <assert.h> */
142626/* #include <stdlib.h> */
142627/* #include <stdio.h> */
142628/* #include <string.h> */
142629
142630/* #include "fts3_tokenizer.h" */
142631
142632/*
142633** Class derived from sqlite3_tokenizer
142634*/
142635typedef struct porter_tokenizer {
142636  sqlite3_tokenizer base;      /* Base class */
142637} porter_tokenizer;
142638
142639/*
142640** Class derived from sqlite3_tokenizer_cursor
142641*/
142642typedef struct porter_tokenizer_cursor {
142643  sqlite3_tokenizer_cursor base;
142644  const char *zInput;          /* input we are tokenizing */
142645  int nInput;                  /* size of the input */
142646  int iOffset;                 /* current position in zInput */
142647  int iToken;                  /* index of next token to be returned */
142648  char *zToken;                /* storage for current token */
142649  int nAllocated;              /* space allocated to zToken buffer */
142650} porter_tokenizer_cursor;
142651
142652
142653/*
142654** Create a new tokenizer instance.
142655*/
142656static int porterCreate(
142657  int argc, const char * const *argv,
142658  sqlite3_tokenizer **ppTokenizer
142659){
142660  porter_tokenizer *t;
142661
142662  UNUSED_PARAMETER(argc);
142663  UNUSED_PARAMETER(argv);
142664
142665  t = (porter_tokenizer *) sqlite3_malloc(sizeof(*t));
142666  if( t==NULL ) return SQLITE_NOMEM;
142667  memset(t, 0, sizeof(*t));
142668  *ppTokenizer = &t->base;
142669  return SQLITE_OK;
142670}
142671
142672/*
142673** Destroy a tokenizer
142674*/
142675static int porterDestroy(sqlite3_tokenizer *pTokenizer){
142676  sqlite3_free(pTokenizer);
142677  return SQLITE_OK;
142678}
142679
142680/*
142681** Prepare to begin tokenizing a particular string.  The input
142682** string to be tokenized is zInput[0..nInput-1].  A cursor
142683** used to incrementally tokenize this string is returned in
142684** *ppCursor.
142685*/
142686static int porterOpen(
142687  sqlite3_tokenizer *pTokenizer,         /* The tokenizer */
142688  const char *zInput, int nInput,        /* String to be tokenized */
142689  sqlite3_tokenizer_cursor **ppCursor    /* OUT: Tokenization cursor */
142690){
142691  porter_tokenizer_cursor *c;
142692
142693  UNUSED_PARAMETER(pTokenizer);
142694
142695  c = (porter_tokenizer_cursor *) sqlite3_malloc(sizeof(*c));
142696  if( c==NULL ) return SQLITE_NOMEM;
142697
142698  c->zInput = zInput;
142699  if( zInput==0 ){
142700    c->nInput = 0;
142701  }else if( nInput<0 ){
142702    c->nInput = (int)strlen(zInput);
142703  }else{
142704    c->nInput = nInput;
142705  }
142706  c->iOffset = 0;                 /* start tokenizing at the beginning */
142707  c->iToken = 0;
142708  c->zToken = NULL;               /* no space allocated, yet. */
142709  c->nAllocated = 0;
142710
142711  *ppCursor = &c->base;
142712  return SQLITE_OK;
142713}
142714
142715/*
142716** Close a tokenization cursor previously opened by a call to
142717** porterOpen() above.
142718*/
142719static int porterClose(sqlite3_tokenizer_cursor *pCursor){
142720  porter_tokenizer_cursor *c = (porter_tokenizer_cursor *) pCursor;
142721  sqlite3_free(c->zToken);
142722  sqlite3_free(c);
142723  return SQLITE_OK;
142724}
142725/*
142726** Vowel or consonant
142727*/
142728static const char cType[] = {
142729   0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0,
142730   1, 1, 1, 2, 1
142731};
142732
142733/*
142734** isConsonant() and isVowel() determine if their first character in
142735** the string they point to is a consonant or a vowel, according
142736** to Porter ruls.
142737**
142738** A consonate is any letter other than 'a', 'e', 'i', 'o', or 'u'.
142739** 'Y' is a consonant unless it follows another consonant,
142740** in which case it is a vowel.
142741**
142742** In these routine, the letters are in reverse order.  So the 'y' rule
142743** is that 'y' is a consonant unless it is followed by another
142744** consonent.
142745*/
142746static int isVowel(const char*);
142747static int isConsonant(const char *z){
142748  int j;
142749  char x = *z;
142750  if( x==0 ) return 0;
142751  assert( x>='a' && x<='z' );
142752  j = cType[x-'a'];
142753  if( j<2 ) return j;
142754  return z[1]==0 || isVowel(z + 1);
142755}
142756static int isVowel(const char *z){
142757  int j;
142758  char x = *z;
142759  if( x==0 ) return 0;
142760  assert( x>='a' && x<='z' );
142761  j = cType[x-'a'];
142762  if( j<2 ) return 1-j;
142763  return isConsonant(z + 1);
142764}
142765
142766/*
142767** Let any sequence of one or more vowels be represented by V and let
142768** C be sequence of one or more consonants.  Then every word can be
142769** represented as:
142770**
142771**           [C] (VC){m} [V]
142772**
142773** In prose:  A word is an optional consonant followed by zero or
142774** vowel-consonant pairs followed by an optional vowel.  "m" is the
142775** number of vowel consonant pairs.  This routine computes the value
142776** of m for the first i bytes of a word.
142777**
142778** Return true if the m-value for z is 1 or more.  In other words,
142779** return true if z contains at least one vowel that is followed
142780** by a consonant.
142781**
142782** In this routine z[] is in reverse order.  So we are really looking
142783** for an instance of a consonant followed by a vowel.
142784*/
142785static int m_gt_0(const char *z){
142786  while( isVowel(z) ){ z++; }
142787  if( *z==0 ) return 0;
142788  while( isConsonant(z) ){ z++; }
142789  return *z!=0;
142790}
142791
142792/* Like mgt0 above except we are looking for a value of m which is
142793** exactly 1
142794*/
142795static int m_eq_1(const char *z){
142796  while( isVowel(z) ){ z++; }
142797  if( *z==0 ) return 0;
142798  while( isConsonant(z) ){ z++; }
142799  if( *z==0 ) return 0;
142800  while( isVowel(z) ){ z++; }
142801  if( *z==0 ) return 1;
142802  while( isConsonant(z) ){ z++; }
142803  return *z==0;
142804}
142805
142806/* Like mgt0 above except we are looking for a value of m>1 instead
142807** or m>0
142808*/
142809static int m_gt_1(const char *z){
142810  while( isVowel(z) ){ z++; }
142811  if( *z==0 ) return 0;
142812  while( isConsonant(z) ){ z++; }
142813  if( *z==0 ) return 0;
142814  while( isVowel(z) ){ z++; }
142815  if( *z==0 ) return 0;
142816  while( isConsonant(z) ){ z++; }
142817  return *z!=0;
142818}
142819
142820/*
142821** Return TRUE if there is a vowel anywhere within z[0..n-1]
142822*/
142823static int hasVowel(const char *z){
142824  while( isConsonant(z) ){ z++; }
142825  return *z!=0;
142826}
142827
142828/*
142829** Return TRUE if the word ends in a double consonant.
142830**
142831** The text is reversed here. So we are really looking at
142832** the first two characters of z[].
142833*/
142834static int doubleConsonant(const char *z){
142835  return isConsonant(z) && z[0]==z[1];
142836}
142837
142838/*
142839** Return TRUE if the word ends with three letters which
142840** are consonant-vowel-consonent and where the final consonant
142841** is not 'w', 'x', or 'y'.
142842**
142843** The word is reversed here.  So we are really checking the
142844** first three letters and the first one cannot be in [wxy].
142845*/
142846static int star_oh(const char *z){
142847  return
142848    isConsonant(z) &&
142849    z[0]!='w' && z[0]!='x' && z[0]!='y' &&
142850    isVowel(z+1) &&
142851    isConsonant(z+2);
142852}
142853
142854/*
142855** If the word ends with zFrom and xCond() is true for the stem
142856** of the word that preceeds the zFrom ending, then change the
142857** ending to zTo.
142858**
142859** The input word *pz and zFrom are both in reverse order.  zTo
142860** is in normal order.
142861**
142862** Return TRUE if zFrom matches.  Return FALSE if zFrom does not
142863** match.  Not that TRUE is returned even if xCond() fails and
142864** no substitution occurs.
142865*/
142866static int stem(
142867  char **pz,             /* The word being stemmed (Reversed) */
142868  const char *zFrom,     /* If the ending matches this... (Reversed) */
142869  const char *zTo,       /* ... change the ending to this (not reversed) */
142870  int (*xCond)(const char*)   /* Condition that must be true */
142871){
142872  char *z = *pz;
142873  while( *zFrom && *zFrom==*z ){ z++; zFrom++; }
142874  if( *zFrom!=0 ) return 0;
142875  if( xCond && !xCond(z) ) return 1;
142876  while( *zTo ){
142877    *(--z) = *(zTo++);
142878  }
142879  *pz = z;
142880  return 1;
142881}
142882
142883/*
142884** This is the fallback stemmer used when the porter stemmer is
142885** inappropriate.  The input word is copied into the output with
142886** US-ASCII case folding.  If the input word is too long (more
142887** than 20 bytes if it contains no digits or more than 6 bytes if
142888** it contains digits) then word is truncated to 20 or 6 bytes
142889** by taking 10 or 3 bytes from the beginning and end.
142890*/
142891static void copy_stemmer(const char *zIn, int nIn, char *zOut, int *pnOut){
142892  int i, mx, j;
142893  int hasDigit = 0;
142894  for(i=0; i<nIn; i++){
142895    char c = zIn[i];
142896    if( c>='A' && c<='Z' ){
142897      zOut[i] = c - 'A' + 'a';
142898    }else{
142899      if( c>='0' && c<='9' ) hasDigit = 1;
142900      zOut[i] = c;
142901    }
142902  }
142903  mx = hasDigit ? 3 : 10;
142904  if( nIn>mx*2 ){
142905    for(j=mx, i=nIn-mx; i<nIn; i++, j++){
142906      zOut[j] = zOut[i];
142907    }
142908    i = j;
142909  }
142910  zOut[i] = 0;
142911  *pnOut = i;
142912}
142913
142914
142915/*
142916** Stem the input word zIn[0..nIn-1].  Store the output in zOut.
142917** zOut is at least big enough to hold nIn bytes.  Write the actual
142918** size of the output word (exclusive of the '\0' terminator) into *pnOut.
142919**
142920** Any upper-case characters in the US-ASCII character set ([A-Z])
142921** are converted to lower case.  Upper-case UTF characters are
142922** unchanged.
142923**
142924** Words that are longer than about 20 bytes are stemmed by retaining
142925** a few bytes from the beginning and the end of the word.  If the
142926** word contains digits, 3 bytes are taken from the beginning and
142927** 3 bytes from the end.  For long words without digits, 10 bytes
142928** are taken from each end.  US-ASCII case folding still applies.
142929**
142930** If the input word contains not digits but does characters not
142931** in [a-zA-Z] then no stemming is attempted and this routine just
142932** copies the input into the input into the output with US-ASCII
142933** case folding.
142934**
142935** Stemming never increases the length of the word.  So there is
142936** no chance of overflowing the zOut buffer.
142937*/
142938static void porter_stemmer(const char *zIn, int nIn, char *zOut, int *pnOut){
142939  int i, j;
142940  char zReverse[28];
142941  char *z, *z2;
142942  if( nIn<3 || nIn>=(int)sizeof(zReverse)-7 ){
142943    /* The word is too big or too small for the porter stemmer.
142944    ** Fallback to the copy stemmer */
142945    copy_stemmer(zIn, nIn, zOut, pnOut);
142946    return;
142947  }
142948  for(i=0, j=sizeof(zReverse)-6; i<nIn; i++, j--){
142949    char c = zIn[i];
142950    if( c>='A' && c<='Z' ){
142951      zReverse[j] = c + 'a' - 'A';
142952    }else if( c>='a' && c<='z' ){
142953      zReverse[j] = c;
142954    }else{
142955      /* The use of a character not in [a-zA-Z] means that we fallback
142956      ** to the copy stemmer */
142957      copy_stemmer(zIn, nIn, zOut, pnOut);
142958      return;
142959    }
142960  }
142961  memset(&zReverse[sizeof(zReverse)-5], 0, 5);
142962  z = &zReverse[j+1];
142963
142964
142965  /* Step 1a */
142966  if( z[0]=='s' ){
142967    if(
142968     !stem(&z, "sess", "ss", 0) &&
142969     !stem(&z, "sei", "i", 0)  &&
142970     !stem(&z, "ss", "ss", 0)
142971    ){
142972      z++;
142973    }
142974  }
142975
142976  /* Step 1b */
142977  z2 = z;
142978  if( stem(&z, "dee", "ee", m_gt_0) ){
142979    /* Do nothing.  The work was all in the test */
142980  }else if(
142981     (stem(&z, "gni", "", hasVowel) || stem(&z, "de", "", hasVowel))
142982      && z!=z2
142983  ){
142984     if( stem(&z, "ta", "ate", 0) ||
142985         stem(&z, "lb", "ble", 0) ||
142986         stem(&z, "zi", "ize", 0) ){
142987       /* Do nothing.  The work was all in the test */
142988     }else if( doubleConsonant(z) && (*z!='l' && *z!='s' && *z!='z') ){
142989       z++;
142990     }else if( m_eq_1(z) && star_oh(z) ){
142991       *(--z) = 'e';
142992     }
142993  }
142994
142995  /* Step 1c */
142996  if( z[0]=='y' && hasVowel(z+1) ){
142997    z[0] = 'i';
142998  }
142999
143000  /* Step 2 */
143001  switch( z[1] ){
143002   case 'a':
143003     if( !stem(&z, "lanoita", "ate", m_gt_0) ){
143004       stem(&z, "lanoit", "tion", m_gt_0);
143005     }
143006     break;
143007   case 'c':
143008     if( !stem(&z, "icne", "ence", m_gt_0) ){
143009       stem(&z, "icna", "ance", m_gt_0);
143010     }
143011     break;
143012   case 'e':
143013     stem(&z, "rezi", "ize", m_gt_0);
143014     break;
143015   case 'g':
143016     stem(&z, "igol", "log", m_gt_0);
143017     break;
143018   case 'l':
143019     if( !stem(&z, "ilb", "ble", m_gt_0)
143020      && !stem(&z, "illa", "al", m_gt_0)
143021      && !stem(&z, "iltne", "ent", m_gt_0)
143022      && !stem(&z, "ile", "e", m_gt_0)
143023     ){
143024       stem(&z, "ilsuo", "ous", m_gt_0);
143025     }
143026     break;
143027   case 'o':
143028     if( !stem(&z, "noitazi", "ize", m_gt_0)
143029      && !stem(&z, "noita", "ate", m_gt_0)
143030     ){
143031       stem(&z, "rota", "ate", m_gt_0);
143032     }
143033     break;
143034   case 's':
143035     if( !stem(&z, "msila", "al", m_gt_0)
143036      && !stem(&z, "ssenevi", "ive", m_gt_0)
143037      && !stem(&z, "ssenluf", "ful", m_gt_0)
143038     ){
143039       stem(&z, "ssensuo", "ous", m_gt_0);
143040     }
143041     break;
143042   case 't':
143043     if( !stem(&z, "itila", "al", m_gt_0)
143044      && !stem(&z, "itivi", "ive", m_gt_0)
143045     ){
143046       stem(&z, "itilib", "ble", m_gt_0);
143047     }
143048     break;
143049  }
143050
143051  /* Step 3 */
143052  switch( z[0] ){
143053   case 'e':
143054     if( !stem(&z, "etaci", "ic", m_gt_0)
143055      && !stem(&z, "evita", "", m_gt_0)
143056     ){
143057       stem(&z, "ezila", "al", m_gt_0);
143058     }
143059     break;
143060   case 'i':
143061     stem(&z, "itici", "ic", m_gt_0);
143062     break;
143063   case 'l':
143064     if( !stem(&z, "laci", "ic", m_gt_0) ){
143065       stem(&z, "luf", "", m_gt_0);
143066     }
143067     break;
143068   case 's':
143069     stem(&z, "ssen", "", m_gt_0);
143070     break;
143071  }
143072
143073  /* Step 4 */
143074  switch( z[1] ){
143075   case 'a':
143076     if( z[0]=='l' && m_gt_1(z+2) ){
143077       z += 2;
143078     }
143079     break;
143080   case 'c':
143081     if( z[0]=='e' && z[2]=='n' && (z[3]=='a' || z[3]=='e')  && m_gt_1(z+4)  ){
143082       z += 4;
143083     }
143084     break;
143085   case 'e':
143086     if( z[0]=='r' && m_gt_1(z+2) ){
143087       z += 2;
143088     }
143089     break;
143090   case 'i':
143091     if( z[0]=='c' && m_gt_1(z+2) ){
143092       z += 2;
143093     }
143094     break;
143095   case 'l':
143096     if( z[0]=='e' && z[2]=='b' && (z[3]=='a' || z[3]=='i') && m_gt_1(z+4) ){
143097       z += 4;
143098     }
143099     break;
143100   case 'n':
143101     if( z[0]=='t' ){
143102       if( z[2]=='a' ){
143103         if( m_gt_1(z+3) ){
143104           z += 3;
143105         }
143106       }else if( z[2]=='e' ){
143107         if( !stem(&z, "tneme", "", m_gt_1)
143108          && !stem(&z, "tnem", "", m_gt_1)
143109         ){
143110           stem(&z, "tne", "", m_gt_1);
143111         }
143112       }
143113     }
143114     break;
143115   case 'o':
143116     if( z[0]=='u' ){
143117       if( m_gt_1(z+2) ){
143118         z += 2;
143119       }
143120     }else if( z[3]=='s' || z[3]=='t' ){
143121       stem(&z, "noi", "", m_gt_1);
143122     }
143123     break;
143124   case 's':
143125     if( z[0]=='m' && z[2]=='i' && m_gt_1(z+3) ){
143126       z += 3;
143127     }
143128     break;
143129   case 't':
143130     if( !stem(&z, "eta", "", m_gt_1) ){
143131       stem(&z, "iti", "", m_gt_1);
143132     }
143133     break;
143134   case 'u':
143135     if( z[0]=='s' && z[2]=='o' && m_gt_1(z+3) ){
143136       z += 3;
143137     }
143138     break;
143139   case 'v':
143140   case 'z':
143141     if( z[0]=='e' && z[2]=='i' && m_gt_1(z+3) ){
143142       z += 3;
143143     }
143144     break;
143145  }
143146
143147  /* Step 5a */
143148  if( z[0]=='e' ){
143149    if( m_gt_1(z+1) ){
143150      z++;
143151    }else if( m_eq_1(z+1) && !star_oh(z+1) ){
143152      z++;
143153    }
143154  }
143155
143156  /* Step 5b */
143157  if( m_gt_1(z) && z[0]=='l' && z[1]=='l' ){
143158    z++;
143159  }
143160
143161  /* z[] is now the stemmed word in reverse order.  Flip it back
143162  ** around into forward order and return.
143163  */
143164  *pnOut = i = (int)strlen(z);
143165  zOut[i] = 0;
143166  while( *z ){
143167    zOut[--i] = *(z++);
143168  }
143169}
143170
143171/*
143172** Characters that can be part of a token.  We assume any character
143173** whose value is greater than 0x80 (any UTF character) can be
143174** part of a token.  In other words, delimiters all must have
143175** values of 0x7f or lower.
143176*/
143177static const char porterIdChar[] = {
143178/* x0 x1 x2 x3 x4 x5 x6 x7 x8 x9 xA xB xC xD xE xF */
143179    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0,  /* 3x */
143180    0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,  /* 4x */
143181    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1,  /* 5x */
143182    0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,  /* 6x */
143183    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0,  /* 7x */
143184};
143185#define isDelim(C) (((ch=C)&0x80)==0 && (ch<0x30 || !porterIdChar[ch-0x30]))
143186
143187/*
143188** Extract the next token from a tokenization cursor.  The cursor must
143189** have been opened by a prior call to porterOpen().
143190*/
143191static int porterNext(
143192  sqlite3_tokenizer_cursor *pCursor,  /* Cursor returned by porterOpen */
143193  const char **pzToken,               /* OUT: *pzToken is the token text */
143194  int *pnBytes,                       /* OUT: Number of bytes in token */
143195  int *piStartOffset,                 /* OUT: Starting offset of token */
143196  int *piEndOffset,                   /* OUT: Ending offset of token */
143197  int *piPosition                     /* OUT: Position integer of token */
143198){
143199  porter_tokenizer_cursor *c = (porter_tokenizer_cursor *) pCursor;
143200  const char *z = c->zInput;
143201
143202  while( c->iOffset<c->nInput ){
143203    int iStartOffset, ch;
143204
143205    /* Scan past delimiter characters */
143206    while( c->iOffset<c->nInput && isDelim(z[c->iOffset]) ){
143207      c->iOffset++;
143208    }
143209
143210    /* Count non-delimiter characters. */
143211    iStartOffset = c->iOffset;
143212    while( c->iOffset<c->nInput && !isDelim(z[c->iOffset]) ){
143213      c->iOffset++;
143214    }
143215
143216    if( c->iOffset>iStartOffset ){
143217      int n = c->iOffset-iStartOffset;
143218      if( n>c->nAllocated ){
143219        char *pNew;
143220        c->nAllocated = n+20;
143221        pNew = sqlite3_realloc(c->zToken, c->nAllocated);
143222        if( !pNew ) return SQLITE_NOMEM;
143223        c->zToken = pNew;
143224      }
143225      porter_stemmer(&z[iStartOffset], n, c->zToken, pnBytes);
143226      *pzToken = c->zToken;
143227      *piStartOffset = iStartOffset;
143228      *piEndOffset = c->iOffset;
143229      *piPosition = c->iToken++;
143230      return SQLITE_OK;
143231    }
143232  }
143233  return SQLITE_DONE;
143234}
143235
143236/*
143237** The set of routines that implement the porter-stemmer tokenizer
143238*/
143239static const sqlite3_tokenizer_module porterTokenizerModule = {
143240  0,
143241  porterCreate,
143242  porterDestroy,
143243  porterOpen,
143244  porterClose,
143245  porterNext,
143246  0
143247};
143248
143249/*
143250** Allocate a new porter tokenizer.  Return a pointer to the new
143251** tokenizer in *ppModule
143252*/
143253SQLITE_PRIVATE void sqlite3Fts3PorterTokenizerModule(
143254  sqlite3_tokenizer_module const**ppModule
143255){
143256  *ppModule = &porterTokenizerModule;
143257}
143258
143259#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */
143260
143261/************** End of fts3_porter.c *****************************************/
143262/************** Begin file fts3_tokenizer.c **********************************/
143263/*
143264** 2007 June 22
143265**
143266** The author disclaims copyright to this source code.  In place of
143267** a legal notice, here is a blessing:
143268**
143269**    May you do good and not evil.
143270**    May you find forgiveness for yourself and forgive others.
143271**    May you share freely, never taking more than you give.
143272**
143273******************************************************************************
143274**
143275** This is part of an SQLite module implementing full-text search.
143276** This particular file implements the generic tokenizer interface.
143277*/
143278
143279/*
143280** The code in this file is only compiled if:
143281**
143282**     * The FTS3 module is being built as an extension
143283**       (in which case SQLITE_CORE is not defined), or
143284**
143285**     * The FTS3 module is being built into the core of
143286**       SQLite (in which case SQLITE_ENABLE_FTS3 is defined).
143287*/
143288/* #include "fts3Int.h" */
143289#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
143290
143291/* #include <assert.h> */
143292/* #include <string.h> */
143293
143294/*
143295** Implementation of the SQL scalar function for accessing the underlying
143296** hash table. This function may be called as follows:
143297**
143298**   SELECT <function-name>(<key-name>);
143299**   SELECT <function-name>(<key-name>, <pointer>);
143300**
143301** where <function-name> is the name passed as the second argument
143302** to the sqlite3Fts3InitHashTable() function (e.g. 'fts3_tokenizer').
143303**
143304** If the <pointer> argument is specified, it must be a blob value
143305** containing a pointer to be stored as the hash data corresponding
143306** to the string <key-name>. If <pointer> is not specified, then
143307** the string <key-name> must already exist in the has table. Otherwise,
143308** an error is returned.
143309**
143310** Whether or not the <pointer> argument is specified, the value returned
143311** is a blob containing the pointer stored as the hash data corresponding
143312** to string <key-name> (after the hash-table is updated, if applicable).
143313*/
143314static void scalarFunc(
143315  sqlite3_context *context,
143316  int argc,
143317  sqlite3_value **argv
143318){
143319  Fts3Hash *pHash;
143320  void *pPtr = 0;
143321  const unsigned char *zName;
143322  int nName;
143323
143324  assert( argc==1 || argc==2 );
143325
143326  pHash = (Fts3Hash *)sqlite3_user_data(context);
143327
143328  zName = sqlite3_value_text(argv[0]);
143329  nName = sqlite3_value_bytes(argv[0])+1;
143330
143331  if( argc==2 ){
143332    void *pOld;
143333    int n = sqlite3_value_bytes(argv[1]);
143334    if( zName==0 || n!=sizeof(pPtr) ){
143335      sqlite3_result_error(context, "argument type mismatch", -1);
143336      return;
143337    }
143338    pPtr = *(void **)sqlite3_value_blob(argv[1]);
143339    pOld = sqlite3Fts3HashInsert(pHash, (void *)zName, nName, pPtr);
143340    if( pOld==pPtr ){
143341      sqlite3_result_error(context, "out of memory", -1);
143342      return;
143343    }
143344  }else{
143345    if( zName ){
143346      pPtr = sqlite3Fts3HashFind(pHash, zName, nName);
143347    }
143348    if( !pPtr ){
143349      char *zErr = sqlite3_mprintf("unknown tokenizer: %s", zName);
143350      sqlite3_result_error(context, zErr, -1);
143351      sqlite3_free(zErr);
143352      return;
143353    }
143354  }
143355
143356  sqlite3_result_blob(context, (void *)&pPtr, sizeof(pPtr), SQLITE_TRANSIENT);
143357}
143358
143359SQLITE_PRIVATE int sqlite3Fts3IsIdChar(char c){
143360  static const char isFtsIdChar[] = {
143361      0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  /* 0x */
143362      0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  /* 1x */
143363      0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  /* 2x */
143364      1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0,  /* 3x */
143365      0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,  /* 4x */
143366      1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1,  /* 5x */
143367      0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,  /* 6x */
143368      1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0,  /* 7x */
143369  };
143370  return (c&0x80 || isFtsIdChar[(int)(c)]);
143371}
143372
143373SQLITE_PRIVATE const char *sqlite3Fts3NextToken(const char *zStr, int *pn){
143374  const char *z1;
143375  const char *z2 = 0;
143376
143377  /* Find the start of the next token. */
143378  z1 = zStr;
143379  while( z2==0 ){
143380    char c = *z1;
143381    switch( c ){
143382      case '\0': return 0;        /* No more tokens here */
143383      case '\'':
143384      case '"':
143385      case '`': {
143386        z2 = z1;
143387        while( *++z2 && (*z2!=c || *++z2==c) );
143388        break;
143389      }
143390      case '[':
143391        z2 = &z1[1];
143392        while( *z2 && z2[0]!=']' ) z2++;
143393        if( *z2 ) z2++;
143394        break;
143395
143396      default:
143397        if( sqlite3Fts3IsIdChar(*z1) ){
143398          z2 = &z1[1];
143399          while( sqlite3Fts3IsIdChar(*z2) ) z2++;
143400        }else{
143401          z1++;
143402        }
143403    }
143404  }
143405
143406  *pn = (int)(z2-z1);
143407  return z1;
143408}
143409
143410SQLITE_PRIVATE int sqlite3Fts3InitTokenizer(
143411  Fts3Hash *pHash,                /* Tokenizer hash table */
143412  const char *zArg,               /* Tokenizer name */
143413  sqlite3_tokenizer **ppTok,      /* OUT: Tokenizer (if applicable) */
143414  char **pzErr                    /* OUT: Set to malloced error message */
143415){
143416  int rc;
143417  char *z = (char *)zArg;
143418  int n = 0;
143419  char *zCopy;
143420  char *zEnd;                     /* Pointer to nul-term of zCopy */
143421  sqlite3_tokenizer_module *m;
143422
143423  zCopy = sqlite3_mprintf("%s", zArg);
143424  if( !zCopy ) return SQLITE_NOMEM;
143425  zEnd = &zCopy[strlen(zCopy)];
143426
143427  z = (char *)sqlite3Fts3NextToken(zCopy, &n);
143428  if( z==0 ){
143429    assert( n==0 );
143430    z = zCopy;
143431  }
143432  z[n] = '\0';
143433  sqlite3Fts3Dequote(z);
143434
143435  m = (sqlite3_tokenizer_module *)sqlite3Fts3HashFind(pHash,z,(int)strlen(z)+1);
143436  if( !m ){
143437    sqlite3Fts3ErrMsg(pzErr, "unknown tokenizer: %s", z);
143438    rc = SQLITE_ERROR;
143439  }else{
143440    char const **aArg = 0;
143441    int iArg = 0;
143442    z = &z[n+1];
143443    while( z<zEnd && (NULL!=(z = (char *)sqlite3Fts3NextToken(z, &n))) ){
143444      int nNew = sizeof(char *)*(iArg+1);
143445      char const **aNew = (const char **)sqlite3_realloc((void *)aArg, nNew);
143446      if( !aNew ){
143447        sqlite3_free(zCopy);
143448        sqlite3_free((void *)aArg);
143449        return SQLITE_NOMEM;
143450      }
143451      aArg = aNew;
143452      aArg[iArg++] = z;
143453      z[n] = '\0';
143454      sqlite3Fts3Dequote(z);
143455      z = &z[n+1];
143456    }
143457    rc = m->xCreate(iArg, aArg, ppTok);
143458    assert( rc!=SQLITE_OK || *ppTok );
143459    if( rc!=SQLITE_OK ){
143460      sqlite3Fts3ErrMsg(pzErr, "unknown tokenizer");
143461    }else{
143462      (*ppTok)->pModule = m;
143463    }
143464    sqlite3_free((void *)aArg);
143465  }
143466
143467  sqlite3_free(zCopy);
143468  return rc;
143469}
143470
143471
143472#ifdef SQLITE_TEST
143473
143474#include <tcl.h>
143475/* #include <string.h> */
143476
143477/*
143478** Implementation of a special SQL scalar function for testing tokenizers
143479** designed to be used in concert with the Tcl testing framework. This
143480** function must be called with two or more arguments:
143481**
143482**   SELECT <function-name>(<key-name>, ..., <input-string>);
143483**
143484** where <function-name> is the name passed as the second argument
143485** to the sqlite3Fts3InitHashTable() function (e.g. 'fts3_tokenizer')
143486** concatenated with the string '_test' (e.g. 'fts3_tokenizer_test').
143487**
143488** The return value is a string that may be interpreted as a Tcl
143489** list. For each token in the <input-string>, three elements are
143490** added to the returned list. The first is the token position, the
143491** second is the token text (folded, stemmed, etc.) and the third is the
143492** substring of <input-string> associated with the token. For example,
143493** using the built-in "simple" tokenizer:
143494**
143495**   SELECT fts_tokenizer_test('simple', 'I don't see how');
143496**
143497** will return the string:
143498**
143499**   "{0 i I 1 dont don't 2 see see 3 how how}"
143500**
143501*/
143502static void testFunc(
143503  sqlite3_context *context,
143504  int argc,
143505  sqlite3_value **argv
143506){
143507  Fts3Hash *pHash;
143508  sqlite3_tokenizer_module *p;
143509  sqlite3_tokenizer *pTokenizer = 0;
143510  sqlite3_tokenizer_cursor *pCsr = 0;
143511
143512  const char *zErr = 0;
143513
143514  const char *zName;
143515  int nName;
143516  const char *zInput;
143517  int nInput;
143518
143519  const char *azArg[64];
143520
143521  const char *zToken;
143522  int nToken = 0;
143523  int iStart = 0;
143524  int iEnd = 0;
143525  int iPos = 0;
143526  int i;
143527
143528  Tcl_Obj *pRet;
143529
143530  if( argc<2 ){
143531    sqlite3_result_error(context, "insufficient arguments", -1);
143532    return;
143533  }
143534
143535  nName = sqlite3_value_bytes(argv[0]);
143536  zName = (const char *)sqlite3_value_text(argv[0]);
143537  nInput = sqlite3_value_bytes(argv[argc-1]);
143538  zInput = (const char *)sqlite3_value_text(argv[argc-1]);
143539
143540  pHash = (Fts3Hash *)sqlite3_user_data(context);
143541  p = (sqlite3_tokenizer_module *)sqlite3Fts3HashFind(pHash, zName, nName+1);
143542
143543  if( !p ){
143544    char *zErr2 = sqlite3_mprintf("unknown tokenizer: %s", zName);
143545    sqlite3_result_error(context, zErr2, -1);
143546    sqlite3_free(zErr2);
143547    return;
143548  }
143549
143550  pRet = Tcl_NewObj();
143551  Tcl_IncrRefCount(pRet);
143552
143553  for(i=1; i<argc-1; i++){
143554    azArg[i-1] = (const char *)sqlite3_value_text(argv[i]);
143555  }
143556
143557  if( SQLITE_OK!=p->xCreate(argc-2, azArg, &pTokenizer) ){
143558    zErr = "error in xCreate()";
143559    goto finish;
143560  }
143561  pTokenizer->pModule = p;
143562  if( sqlite3Fts3OpenTokenizer(pTokenizer, 0, zInput, nInput, &pCsr) ){
143563    zErr = "error in xOpen()";
143564    goto finish;
143565  }
143566
143567  while( SQLITE_OK==p->xNext(pCsr, &zToken, &nToken, &iStart, &iEnd, &iPos) ){
143568    Tcl_ListObjAppendElement(0, pRet, Tcl_NewIntObj(iPos));
143569    Tcl_ListObjAppendElement(0, pRet, Tcl_NewStringObj(zToken, nToken));
143570    zToken = &zInput[iStart];
143571    nToken = iEnd-iStart;
143572    Tcl_ListObjAppendElement(0, pRet, Tcl_NewStringObj(zToken, nToken));
143573  }
143574
143575  if( SQLITE_OK!=p->xClose(pCsr) ){
143576    zErr = "error in xClose()";
143577    goto finish;
143578  }
143579  if( SQLITE_OK!=p->xDestroy(pTokenizer) ){
143580    zErr = "error in xDestroy()";
143581    goto finish;
143582  }
143583
143584finish:
143585  if( zErr ){
143586    sqlite3_result_error(context, zErr, -1);
143587  }else{
143588    sqlite3_result_text(context, Tcl_GetString(pRet), -1, SQLITE_TRANSIENT);
143589  }
143590  Tcl_DecrRefCount(pRet);
143591}
143592
143593static
143594int registerTokenizer(
143595  sqlite3 *db,
143596  char *zName,
143597  const sqlite3_tokenizer_module *p
143598){
143599  int rc;
143600  sqlite3_stmt *pStmt;
143601  const char zSql[] = "SELECT fts3_tokenizer(?, ?)";
143602
143603  rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
143604  if( rc!=SQLITE_OK ){
143605    return rc;
143606  }
143607
143608  sqlite3_bind_text(pStmt, 1, zName, -1, SQLITE_STATIC);
143609  sqlite3_bind_blob(pStmt, 2, &p, sizeof(p), SQLITE_STATIC);
143610  sqlite3_step(pStmt);
143611
143612  return sqlite3_finalize(pStmt);
143613}
143614
143615static
143616int queryTokenizer(
143617  sqlite3 *db,
143618  char *zName,
143619  const sqlite3_tokenizer_module **pp
143620){
143621  int rc;
143622  sqlite3_stmt *pStmt;
143623  const char zSql[] = "SELECT fts3_tokenizer(?)";
143624
143625  *pp = 0;
143626  rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
143627  if( rc!=SQLITE_OK ){
143628    return rc;
143629  }
143630
143631  sqlite3_bind_text(pStmt, 1, zName, -1, SQLITE_STATIC);
143632  if( SQLITE_ROW==sqlite3_step(pStmt) ){
143633    if( sqlite3_column_type(pStmt, 0)==SQLITE_BLOB ){
143634      memcpy((void *)pp, sqlite3_column_blob(pStmt, 0), sizeof(*pp));
143635    }
143636  }
143637
143638  return sqlite3_finalize(pStmt);
143639}
143640
143641SQLITE_PRIVATE void sqlite3Fts3SimpleTokenizerModule(sqlite3_tokenizer_module const**ppModule);
143642
143643/*
143644** Implementation of the scalar function fts3_tokenizer_internal_test().
143645** This function is used for testing only, it is not included in the
143646** build unless SQLITE_TEST is defined.
143647**
143648** The purpose of this is to test that the fts3_tokenizer() function
143649** can be used as designed by the C-code in the queryTokenizer and
143650** registerTokenizer() functions above. These two functions are repeated
143651** in the README.tokenizer file as an example, so it is important to
143652** test them.
143653**
143654** To run the tests, evaluate the fts3_tokenizer_internal_test() scalar
143655** function with no arguments. An assert() will fail if a problem is
143656** detected. i.e.:
143657**
143658**     SELECT fts3_tokenizer_internal_test();
143659**
143660*/
143661static void intTestFunc(
143662  sqlite3_context *context,
143663  int argc,
143664  sqlite3_value **argv
143665){
143666  int rc;
143667  const sqlite3_tokenizer_module *p1;
143668  const sqlite3_tokenizer_module *p2;
143669  sqlite3 *db = (sqlite3 *)sqlite3_user_data(context);
143670
143671  UNUSED_PARAMETER(argc);
143672  UNUSED_PARAMETER(argv);
143673
143674  /* Test the query function */
143675  sqlite3Fts3SimpleTokenizerModule(&p1);
143676  rc = queryTokenizer(db, "simple", &p2);
143677  assert( rc==SQLITE_OK );
143678  assert( p1==p2 );
143679  rc = queryTokenizer(db, "nosuchtokenizer", &p2);
143680  assert( rc==SQLITE_ERROR );
143681  assert( p2==0 );
143682  assert( 0==strcmp(sqlite3_errmsg(db), "unknown tokenizer: nosuchtokenizer") );
143683
143684  /* Test the storage function */
143685  rc = registerTokenizer(db, "nosuchtokenizer", p1);
143686  assert( rc==SQLITE_OK );
143687  rc = queryTokenizer(db, "nosuchtokenizer", &p2);
143688  assert( rc==SQLITE_OK );
143689  assert( p2==p1 );
143690
143691  sqlite3_result_text(context, "ok", -1, SQLITE_STATIC);
143692}
143693
143694#endif
143695
143696/*
143697** Set up SQL objects in database db used to access the contents of
143698** the hash table pointed to by argument pHash. The hash table must
143699** been initialized to use string keys, and to take a private copy
143700** of the key when a value is inserted. i.e. by a call similar to:
143701**
143702**    sqlite3Fts3HashInit(pHash, FTS3_HASH_STRING, 1);
143703**
143704** This function adds a scalar function (see header comment above
143705** scalarFunc() in this file for details) and, if ENABLE_TABLE is
143706** defined at compilation time, a temporary virtual table (see header
143707** comment above struct HashTableVtab) to the database schema. Both
143708** provide read/write access to the contents of *pHash.
143709**
143710** The third argument to this function, zName, is used as the name
143711** of both the scalar and, if created, the virtual table.
143712*/
143713SQLITE_PRIVATE int sqlite3Fts3InitHashTable(
143714  sqlite3 *db,
143715  Fts3Hash *pHash,
143716  const char *zName
143717){
143718  int rc = SQLITE_OK;
143719  void *p = (void *)pHash;
143720  const int any = SQLITE_ANY;
143721
143722#ifdef SQLITE_TEST
143723  char *zTest = 0;
143724  char *zTest2 = 0;
143725  void *pdb = (void *)db;
143726  zTest = sqlite3_mprintf("%s_test", zName);
143727  zTest2 = sqlite3_mprintf("%s_internal_test", zName);
143728  if( !zTest || !zTest2 ){
143729    rc = SQLITE_NOMEM;
143730  }
143731#endif
143732
143733  if( SQLITE_OK==rc ){
143734    rc = sqlite3_create_function(db, zName, 1, any, p, scalarFunc, 0, 0);
143735  }
143736  if( SQLITE_OK==rc ){
143737    rc = sqlite3_create_function(db, zName, 2, any, p, scalarFunc, 0, 0);
143738  }
143739#ifdef SQLITE_TEST
143740  if( SQLITE_OK==rc ){
143741    rc = sqlite3_create_function(db, zTest, -1, any, p, testFunc, 0, 0);
143742  }
143743  if( SQLITE_OK==rc ){
143744    rc = sqlite3_create_function(db, zTest2, 0, any, pdb, intTestFunc, 0, 0);
143745  }
143746#endif
143747
143748#ifdef SQLITE_TEST
143749  sqlite3_free(zTest);
143750  sqlite3_free(zTest2);
143751#endif
143752
143753  return rc;
143754}
143755
143756#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */
143757
143758/************** End of fts3_tokenizer.c **************************************/
143759/************** Begin file fts3_tokenizer1.c *********************************/
143760/*
143761** 2006 Oct 10
143762**
143763** The author disclaims copyright to this source code.  In place of
143764** a legal notice, here is a blessing:
143765**
143766**    May you do good and not evil.
143767**    May you find forgiveness for yourself and forgive others.
143768**    May you share freely, never taking more than you give.
143769**
143770******************************************************************************
143771**
143772** Implementation of the "simple" full-text-search tokenizer.
143773*/
143774
143775/*
143776** The code in this file is only compiled if:
143777**
143778**     * The FTS3 module is being built as an extension
143779**       (in which case SQLITE_CORE is not defined), or
143780**
143781**     * The FTS3 module is being built into the core of
143782**       SQLite (in which case SQLITE_ENABLE_FTS3 is defined).
143783*/
143784/* #include "fts3Int.h" */
143785#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
143786
143787/* #include <assert.h> */
143788/* #include <stdlib.h> */
143789/* #include <stdio.h> */
143790/* #include <string.h> */
143791
143792/* #include "fts3_tokenizer.h" */
143793
143794typedef struct simple_tokenizer {
143795  sqlite3_tokenizer base;
143796  char delim[128];             /* flag ASCII delimiters */
143797} simple_tokenizer;
143798
143799typedef struct simple_tokenizer_cursor {
143800  sqlite3_tokenizer_cursor base;
143801  const char *pInput;          /* input we are tokenizing */
143802  int nBytes;                  /* size of the input */
143803  int iOffset;                 /* current position in pInput */
143804  int iToken;                  /* index of next token to be returned */
143805  char *pToken;                /* storage for current token */
143806  int nTokenAllocated;         /* space allocated to zToken buffer */
143807} simple_tokenizer_cursor;
143808
143809
143810static int simpleDelim(simple_tokenizer *t, unsigned char c){
143811  return c<0x80 && t->delim[c];
143812}
143813static int fts3_isalnum(int x){
143814  return (x>='0' && x<='9') || (x>='A' && x<='Z') || (x>='a' && x<='z');
143815}
143816
143817/*
143818** Create a new tokenizer instance.
143819*/
143820static int simpleCreate(
143821  int argc, const char * const *argv,
143822  sqlite3_tokenizer **ppTokenizer
143823){
143824  simple_tokenizer *t;
143825
143826  t = (simple_tokenizer *) sqlite3_malloc(sizeof(*t));
143827  if( t==NULL ) return SQLITE_NOMEM;
143828  memset(t, 0, sizeof(*t));
143829
143830  /* TODO(shess) Delimiters need to remain the same from run to run,
143831  ** else we need to reindex.  One solution would be a meta-table to
143832  ** track such information in the database, then we'd only want this
143833  ** information on the initial create.
143834  */
143835  if( argc>1 ){
143836    int i, n = (int)strlen(argv[1]);
143837    for(i=0; i<n; i++){
143838      unsigned char ch = argv[1][i];
143839      /* We explicitly don't support UTF-8 delimiters for now. */
143840      if( ch>=0x80 ){
143841        sqlite3_free(t);
143842        return SQLITE_ERROR;
143843      }
143844      t->delim[ch] = 1;
143845    }
143846  } else {
143847    /* Mark non-alphanumeric ASCII characters as delimiters */
143848    int i;
143849    for(i=1; i<0x80; i++){
143850      t->delim[i] = !fts3_isalnum(i) ? -1 : 0;
143851    }
143852  }
143853
143854  *ppTokenizer = &t->base;
143855  return SQLITE_OK;
143856}
143857
143858/*
143859** Destroy a tokenizer
143860*/
143861static int simpleDestroy(sqlite3_tokenizer *pTokenizer){
143862  sqlite3_free(pTokenizer);
143863  return SQLITE_OK;
143864}
143865
143866/*
143867** Prepare to begin tokenizing a particular string.  The input
143868** string to be tokenized is pInput[0..nBytes-1].  A cursor
143869** used to incrementally tokenize this string is returned in
143870** *ppCursor.
143871*/
143872static int simpleOpen(
143873  sqlite3_tokenizer *pTokenizer,         /* The tokenizer */
143874  const char *pInput, int nBytes,        /* String to be tokenized */
143875  sqlite3_tokenizer_cursor **ppCursor    /* OUT: Tokenization cursor */
143876){
143877  simple_tokenizer_cursor *c;
143878
143879  UNUSED_PARAMETER(pTokenizer);
143880
143881  c = (simple_tokenizer_cursor *) sqlite3_malloc(sizeof(*c));
143882  if( c==NULL ) return SQLITE_NOMEM;
143883
143884  c->pInput = pInput;
143885  if( pInput==0 ){
143886    c->nBytes = 0;
143887  }else if( nBytes<0 ){
143888    c->nBytes = (int)strlen(pInput);
143889  }else{
143890    c->nBytes = nBytes;
143891  }
143892  c->iOffset = 0;                 /* start tokenizing at the beginning */
143893  c->iToken = 0;
143894  c->pToken = NULL;               /* no space allocated, yet. */
143895  c->nTokenAllocated = 0;
143896
143897  *ppCursor = &c->base;
143898  return SQLITE_OK;
143899}
143900
143901/*
143902** Close a tokenization cursor previously opened by a call to
143903** simpleOpen() above.
143904*/
143905static int simpleClose(sqlite3_tokenizer_cursor *pCursor){
143906  simple_tokenizer_cursor *c = (simple_tokenizer_cursor *) pCursor;
143907  sqlite3_free(c->pToken);
143908  sqlite3_free(c);
143909  return SQLITE_OK;
143910}
143911
143912/*
143913** Extract the next token from a tokenization cursor.  The cursor must
143914** have been opened by a prior call to simpleOpen().
143915*/
143916static int simpleNext(
143917  sqlite3_tokenizer_cursor *pCursor,  /* Cursor returned by simpleOpen */
143918  const char **ppToken,               /* OUT: *ppToken is the token text */
143919  int *pnBytes,                       /* OUT: Number of bytes in token */
143920  int *piStartOffset,                 /* OUT: Starting offset of token */
143921  int *piEndOffset,                   /* OUT: Ending offset of token */
143922  int *piPosition                     /* OUT: Position integer of token */
143923){
143924  simple_tokenizer_cursor *c = (simple_tokenizer_cursor *) pCursor;
143925  simple_tokenizer *t = (simple_tokenizer *) pCursor->pTokenizer;
143926  unsigned char *p = (unsigned char *)c->pInput;
143927
143928  while( c->iOffset<c->nBytes ){
143929    int iStartOffset;
143930
143931    /* Scan past delimiter characters */
143932    while( c->iOffset<c->nBytes && simpleDelim(t, p[c->iOffset]) ){
143933      c->iOffset++;
143934    }
143935
143936    /* Count non-delimiter characters. */
143937    iStartOffset = c->iOffset;
143938    while( c->iOffset<c->nBytes && !simpleDelim(t, p[c->iOffset]) ){
143939      c->iOffset++;
143940    }
143941
143942    if( c->iOffset>iStartOffset ){
143943      int i, n = c->iOffset-iStartOffset;
143944      if( n>c->nTokenAllocated ){
143945        char *pNew;
143946        c->nTokenAllocated = n+20;
143947        pNew = sqlite3_realloc(c->pToken, c->nTokenAllocated);
143948        if( !pNew ) return SQLITE_NOMEM;
143949        c->pToken = pNew;
143950      }
143951      for(i=0; i<n; i++){
143952        /* TODO(shess) This needs expansion to handle UTF-8
143953        ** case-insensitivity.
143954        */
143955        unsigned char ch = p[iStartOffset+i];
143956        c->pToken[i] = (char)((ch>='A' && ch<='Z') ? ch-'A'+'a' : ch);
143957      }
143958      *ppToken = c->pToken;
143959      *pnBytes = n;
143960      *piStartOffset = iStartOffset;
143961      *piEndOffset = c->iOffset;
143962      *piPosition = c->iToken++;
143963
143964      return SQLITE_OK;
143965    }
143966  }
143967  return SQLITE_DONE;
143968}
143969
143970/*
143971** The set of routines that implement the simple tokenizer
143972*/
143973static const sqlite3_tokenizer_module simpleTokenizerModule = {
143974  0,
143975  simpleCreate,
143976  simpleDestroy,
143977  simpleOpen,
143978  simpleClose,
143979  simpleNext,
143980  0,
143981};
143982
143983/*
143984** Allocate a new simple tokenizer.  Return a pointer to the new
143985** tokenizer in *ppModule
143986*/
143987SQLITE_PRIVATE void sqlite3Fts3SimpleTokenizerModule(
143988  sqlite3_tokenizer_module const**ppModule
143989){
143990  *ppModule = &simpleTokenizerModule;
143991}
143992
143993#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */
143994
143995/************** End of fts3_tokenizer1.c *************************************/
143996/************** Begin file fts3_tokenize_vtab.c ******************************/
143997/*
143998** 2013 Apr 22
143999**
144000** The author disclaims copyright to this source code.  In place of
144001** a legal notice, here is a blessing:
144002**
144003**    May you do good and not evil.
144004**    May you find forgiveness for yourself and forgive others.
144005**    May you share freely, never taking more than you give.
144006**
144007******************************************************************************
144008**
144009** This file contains code for the "fts3tokenize" virtual table module.
144010** An fts3tokenize virtual table is created as follows:
144011**
144012**   CREATE VIRTUAL TABLE <tbl> USING fts3tokenize(
144013**       <tokenizer-name>, <arg-1>, ...
144014**   );
144015**
144016** The table created has the following schema:
144017**
144018**   CREATE TABLE <tbl>(input, token, start, end, position)
144019**
144020** When queried, the query must include a WHERE clause of type:
144021**
144022**   input = <string>
144023**
144024** The virtual table module tokenizes this <string>, using the FTS3
144025** tokenizer specified by the arguments to the CREATE VIRTUAL TABLE
144026** statement and returns one row for each token in the result. With
144027** fields set as follows:
144028**
144029**   input:   Always set to a copy of <string>
144030**   token:   A token from the input.
144031**   start:   Byte offset of the token within the input <string>.
144032**   end:     Byte offset of the byte immediately following the end of the
144033**            token within the input string.
144034**   pos:     Token offset of token within input.
144035**
144036*/
144037/* #include "fts3Int.h" */
144038#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
144039
144040/* #include <string.h> */
144041/* #include <assert.h> */
144042
144043typedef struct Fts3tokTable Fts3tokTable;
144044typedef struct Fts3tokCursor Fts3tokCursor;
144045
144046/*
144047** Virtual table structure.
144048*/
144049struct Fts3tokTable {
144050  sqlite3_vtab base;              /* Base class used by SQLite core */
144051  const sqlite3_tokenizer_module *pMod;
144052  sqlite3_tokenizer *pTok;
144053};
144054
144055/*
144056** Virtual table cursor structure.
144057*/
144058struct Fts3tokCursor {
144059  sqlite3_vtab_cursor base;       /* Base class used by SQLite core */
144060  char *zInput;                   /* Input string */
144061  sqlite3_tokenizer_cursor *pCsr; /* Cursor to iterate through zInput */
144062  int iRowid;                     /* Current 'rowid' value */
144063  const char *zToken;             /* Current 'token' value */
144064  int nToken;                     /* Size of zToken in bytes */
144065  int iStart;                     /* Current 'start' value */
144066  int iEnd;                       /* Current 'end' value */
144067  int iPos;                       /* Current 'pos' value */
144068};
144069
144070/*
144071** Query FTS for the tokenizer implementation named zName.
144072*/
144073static int fts3tokQueryTokenizer(
144074  Fts3Hash *pHash,
144075  const char *zName,
144076  const sqlite3_tokenizer_module **pp,
144077  char **pzErr
144078){
144079  sqlite3_tokenizer_module *p;
144080  int nName = (int)strlen(zName);
144081
144082  p = (sqlite3_tokenizer_module *)sqlite3Fts3HashFind(pHash, zName, nName+1);
144083  if( !p ){
144084    sqlite3Fts3ErrMsg(pzErr, "unknown tokenizer: %s", zName);
144085    return SQLITE_ERROR;
144086  }
144087
144088  *pp = p;
144089  return SQLITE_OK;
144090}
144091
144092/*
144093** The second argument, argv[], is an array of pointers to nul-terminated
144094** strings. This function makes a copy of the array and strings into a
144095** single block of memory. It then dequotes any of the strings that appear
144096** to be quoted.
144097**
144098** If successful, output parameter *pazDequote is set to point at the
144099** array of dequoted strings and SQLITE_OK is returned. The caller is
144100** responsible for eventually calling sqlite3_free() to free the array
144101** in this case. Or, if an error occurs, an SQLite error code is returned.
144102** The final value of *pazDequote is undefined in this case.
144103*/
144104static int fts3tokDequoteArray(
144105  int argc,                       /* Number of elements in argv[] */
144106  const char * const *argv,       /* Input array */
144107  char ***pazDequote              /* Output array */
144108){
144109  int rc = SQLITE_OK;             /* Return code */
144110  if( argc==0 ){
144111    *pazDequote = 0;
144112  }else{
144113    int i;
144114    int nByte = 0;
144115    char **azDequote;
144116
144117    for(i=0; i<argc; i++){
144118      nByte += (int)(strlen(argv[i]) + 1);
144119    }
144120
144121    *pazDequote = azDequote = sqlite3_malloc(sizeof(char *)*argc + nByte);
144122    if( azDequote==0 ){
144123      rc = SQLITE_NOMEM;
144124    }else{
144125      char *pSpace = (char *)&azDequote[argc];
144126      for(i=0; i<argc; i++){
144127        int n = (int)strlen(argv[i]);
144128        azDequote[i] = pSpace;
144129        memcpy(pSpace, argv[i], n+1);
144130        sqlite3Fts3Dequote(pSpace);
144131        pSpace += (n+1);
144132      }
144133    }
144134  }
144135
144136  return rc;
144137}
144138
144139/*
144140** Schema of the tokenizer table.
144141*/
144142#define FTS3_TOK_SCHEMA "CREATE TABLE x(input, token, start, end, position)"
144143
144144/*
144145** This function does all the work for both the xConnect and xCreate methods.
144146** These tables have no persistent representation of their own, so xConnect
144147** and xCreate are identical operations.
144148**
144149**   argv[0]: module name
144150**   argv[1]: database name
144151**   argv[2]: table name
144152**   argv[3]: first argument (tokenizer name)
144153*/
144154static int fts3tokConnectMethod(
144155  sqlite3 *db,                    /* Database connection */
144156  void *pHash,                    /* Hash table of tokenizers */
144157  int argc,                       /* Number of elements in argv array */
144158  const char * const *argv,       /* xCreate/xConnect argument array */
144159  sqlite3_vtab **ppVtab,          /* OUT: New sqlite3_vtab object */
144160  char **pzErr                    /* OUT: sqlite3_malloc'd error message */
144161){
144162  Fts3tokTable *pTab = 0;
144163  const sqlite3_tokenizer_module *pMod = 0;
144164  sqlite3_tokenizer *pTok = 0;
144165  int rc;
144166  char **azDequote = 0;
144167  int nDequote;
144168
144169  rc = sqlite3_declare_vtab(db, FTS3_TOK_SCHEMA);
144170  if( rc!=SQLITE_OK ) return rc;
144171
144172  nDequote = argc-3;
144173  rc = fts3tokDequoteArray(nDequote, &argv[3], &azDequote);
144174
144175  if( rc==SQLITE_OK ){
144176    const char *zModule;
144177    if( nDequote<1 ){
144178      zModule = "simple";
144179    }else{
144180      zModule = azDequote[0];
144181    }
144182    rc = fts3tokQueryTokenizer((Fts3Hash*)pHash, zModule, &pMod, pzErr);
144183  }
144184
144185  assert( (rc==SQLITE_OK)==(pMod!=0) );
144186  if( rc==SQLITE_OK ){
144187    const char * const *azArg = (const char * const *)&azDequote[1];
144188    rc = pMod->xCreate((nDequote>1 ? nDequote-1 : 0), azArg, &pTok);
144189  }
144190
144191  if( rc==SQLITE_OK ){
144192    pTab = (Fts3tokTable *)sqlite3_malloc(sizeof(Fts3tokTable));
144193    if( pTab==0 ){
144194      rc = SQLITE_NOMEM;
144195    }
144196  }
144197
144198  if( rc==SQLITE_OK ){
144199    memset(pTab, 0, sizeof(Fts3tokTable));
144200    pTab->pMod = pMod;
144201    pTab->pTok = pTok;
144202    *ppVtab = &pTab->base;
144203  }else{
144204    if( pTok ){
144205      pMod->xDestroy(pTok);
144206    }
144207  }
144208
144209  sqlite3_free(azDequote);
144210  return rc;
144211}
144212
144213/*
144214** This function does the work for both the xDisconnect and xDestroy methods.
144215** These tables have no persistent representation of their own, so xDisconnect
144216** and xDestroy are identical operations.
144217*/
144218static int fts3tokDisconnectMethod(sqlite3_vtab *pVtab){
144219  Fts3tokTable *pTab = (Fts3tokTable *)pVtab;
144220
144221  pTab->pMod->xDestroy(pTab->pTok);
144222  sqlite3_free(pTab);
144223  return SQLITE_OK;
144224}
144225
144226/*
144227** xBestIndex - Analyze a WHERE and ORDER BY clause.
144228*/
144229static int fts3tokBestIndexMethod(
144230  sqlite3_vtab *pVTab,
144231  sqlite3_index_info *pInfo
144232){
144233  int i;
144234  UNUSED_PARAMETER(pVTab);
144235
144236  for(i=0; i<pInfo->nConstraint; i++){
144237    if( pInfo->aConstraint[i].usable
144238     && pInfo->aConstraint[i].iColumn==0
144239     && pInfo->aConstraint[i].op==SQLITE_INDEX_CONSTRAINT_EQ
144240    ){
144241      pInfo->idxNum = 1;
144242      pInfo->aConstraintUsage[i].argvIndex = 1;
144243      pInfo->aConstraintUsage[i].omit = 1;
144244      pInfo->estimatedCost = 1;
144245      return SQLITE_OK;
144246    }
144247  }
144248
144249  pInfo->idxNum = 0;
144250  assert( pInfo->estimatedCost>1000000.0 );
144251
144252  return SQLITE_OK;
144253}
144254
144255/*
144256** xOpen - Open a cursor.
144257*/
144258static int fts3tokOpenMethod(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCsr){
144259  Fts3tokCursor *pCsr;
144260  UNUSED_PARAMETER(pVTab);
144261
144262  pCsr = (Fts3tokCursor *)sqlite3_malloc(sizeof(Fts3tokCursor));
144263  if( pCsr==0 ){
144264    return SQLITE_NOMEM;
144265  }
144266  memset(pCsr, 0, sizeof(Fts3tokCursor));
144267
144268  *ppCsr = (sqlite3_vtab_cursor *)pCsr;
144269  return SQLITE_OK;
144270}
144271
144272/*
144273** Reset the tokenizer cursor passed as the only argument. As if it had
144274** just been returned by fts3tokOpenMethod().
144275*/
144276static void fts3tokResetCursor(Fts3tokCursor *pCsr){
144277  if( pCsr->pCsr ){
144278    Fts3tokTable *pTab = (Fts3tokTable *)(pCsr->base.pVtab);
144279    pTab->pMod->xClose(pCsr->pCsr);
144280    pCsr->pCsr = 0;
144281  }
144282  sqlite3_free(pCsr->zInput);
144283  pCsr->zInput = 0;
144284  pCsr->zToken = 0;
144285  pCsr->nToken = 0;
144286  pCsr->iStart = 0;
144287  pCsr->iEnd = 0;
144288  pCsr->iPos = 0;
144289  pCsr->iRowid = 0;
144290}
144291
144292/*
144293** xClose - Close a cursor.
144294*/
144295static int fts3tokCloseMethod(sqlite3_vtab_cursor *pCursor){
144296  Fts3tokCursor *pCsr = (Fts3tokCursor *)pCursor;
144297
144298  fts3tokResetCursor(pCsr);
144299  sqlite3_free(pCsr);
144300  return SQLITE_OK;
144301}
144302
144303/*
144304** xNext - Advance the cursor to the next row, if any.
144305*/
144306static int fts3tokNextMethod(sqlite3_vtab_cursor *pCursor){
144307  Fts3tokCursor *pCsr = (Fts3tokCursor *)pCursor;
144308  Fts3tokTable *pTab = (Fts3tokTable *)(pCursor->pVtab);
144309  int rc;                         /* Return code */
144310
144311  pCsr->iRowid++;
144312  rc = pTab->pMod->xNext(pCsr->pCsr,
144313      &pCsr->zToken, &pCsr->nToken,
144314      &pCsr->iStart, &pCsr->iEnd, &pCsr->iPos
144315  );
144316
144317  if( rc!=SQLITE_OK ){
144318    fts3tokResetCursor(pCsr);
144319    if( rc==SQLITE_DONE ) rc = SQLITE_OK;
144320  }
144321
144322  return rc;
144323}
144324
144325/*
144326** xFilter - Initialize a cursor to point at the start of its data.
144327*/
144328static int fts3tokFilterMethod(
144329  sqlite3_vtab_cursor *pCursor,   /* The cursor used for this query */
144330  int idxNum,                     /* Strategy index */
144331  const char *idxStr,             /* Unused */
144332  int nVal,                       /* Number of elements in apVal */
144333  sqlite3_value **apVal           /* Arguments for the indexing scheme */
144334){
144335  int rc = SQLITE_ERROR;
144336  Fts3tokCursor *pCsr = (Fts3tokCursor *)pCursor;
144337  Fts3tokTable *pTab = (Fts3tokTable *)(pCursor->pVtab);
144338  UNUSED_PARAMETER(idxStr);
144339  UNUSED_PARAMETER(nVal);
144340
144341  fts3tokResetCursor(pCsr);
144342  if( idxNum==1 ){
144343    const char *zByte = (const char *)sqlite3_value_text(apVal[0]);
144344    int nByte = sqlite3_value_bytes(apVal[0]);
144345    pCsr->zInput = sqlite3_malloc(nByte+1);
144346    if( pCsr->zInput==0 ){
144347      rc = SQLITE_NOMEM;
144348    }else{
144349      memcpy(pCsr->zInput, zByte, nByte);
144350      pCsr->zInput[nByte] = 0;
144351      rc = pTab->pMod->xOpen(pTab->pTok, pCsr->zInput, nByte, &pCsr->pCsr);
144352      if( rc==SQLITE_OK ){
144353        pCsr->pCsr->pTokenizer = pTab->pTok;
144354      }
144355    }
144356  }
144357
144358  if( rc!=SQLITE_OK ) return rc;
144359  return fts3tokNextMethod(pCursor);
144360}
144361
144362/*
144363** xEof - Return true if the cursor is at EOF, or false otherwise.
144364*/
144365static int fts3tokEofMethod(sqlite3_vtab_cursor *pCursor){
144366  Fts3tokCursor *pCsr = (Fts3tokCursor *)pCursor;
144367  return (pCsr->zToken==0);
144368}
144369
144370/*
144371** xColumn - Return a column value.
144372*/
144373static int fts3tokColumnMethod(
144374  sqlite3_vtab_cursor *pCursor,   /* Cursor to retrieve value from */
144375  sqlite3_context *pCtx,          /* Context for sqlite3_result_xxx() calls */
144376  int iCol                        /* Index of column to read value from */
144377){
144378  Fts3tokCursor *pCsr = (Fts3tokCursor *)pCursor;
144379
144380  /* CREATE TABLE x(input, token, start, end, position) */
144381  switch( iCol ){
144382    case 0:
144383      sqlite3_result_text(pCtx, pCsr->zInput, -1, SQLITE_TRANSIENT);
144384      break;
144385    case 1:
144386      sqlite3_result_text(pCtx, pCsr->zToken, pCsr->nToken, SQLITE_TRANSIENT);
144387      break;
144388    case 2:
144389      sqlite3_result_int(pCtx, pCsr->iStart);
144390      break;
144391    case 3:
144392      sqlite3_result_int(pCtx, pCsr->iEnd);
144393      break;
144394    default:
144395      assert( iCol==4 );
144396      sqlite3_result_int(pCtx, pCsr->iPos);
144397      break;
144398  }
144399  return SQLITE_OK;
144400}
144401
144402/*
144403** xRowid - Return the current rowid for the cursor.
144404*/
144405static int fts3tokRowidMethod(
144406  sqlite3_vtab_cursor *pCursor,   /* Cursor to retrieve value from */
144407  sqlite_int64 *pRowid            /* OUT: Rowid value */
144408){
144409  Fts3tokCursor *pCsr = (Fts3tokCursor *)pCursor;
144410  *pRowid = (sqlite3_int64)pCsr->iRowid;
144411  return SQLITE_OK;
144412}
144413
144414/*
144415** Register the fts3tok module with database connection db. Return SQLITE_OK
144416** if successful or an error code if sqlite3_create_module() fails.
144417*/
144418SQLITE_PRIVATE int sqlite3Fts3InitTok(sqlite3 *db, Fts3Hash *pHash){
144419  static const sqlite3_module fts3tok_module = {
144420     0,                           /* iVersion      */
144421     fts3tokConnectMethod,        /* xCreate       */
144422     fts3tokConnectMethod,        /* xConnect      */
144423     fts3tokBestIndexMethod,      /* xBestIndex    */
144424     fts3tokDisconnectMethod,     /* xDisconnect   */
144425     fts3tokDisconnectMethod,     /* xDestroy      */
144426     fts3tokOpenMethod,           /* xOpen         */
144427     fts3tokCloseMethod,          /* xClose        */
144428     fts3tokFilterMethod,         /* xFilter       */
144429     fts3tokNextMethod,           /* xNext         */
144430     fts3tokEofMethod,            /* xEof          */
144431     fts3tokColumnMethod,         /* xColumn       */
144432     fts3tokRowidMethod,          /* xRowid        */
144433     0,                           /* xUpdate       */
144434     0,                           /* xBegin        */
144435     0,                           /* xSync         */
144436     0,                           /* xCommit       */
144437     0,                           /* xRollback     */
144438     0,                           /* xFindFunction */
144439     0,                           /* xRename       */
144440     0,                           /* xSavepoint    */
144441     0,                           /* xRelease      */
144442     0                            /* xRollbackTo   */
144443  };
144444  int rc;                         /* Return code */
144445
144446  rc = sqlite3_create_module(db, "fts3tokenize", &fts3tok_module, (void*)pHash);
144447  return rc;
144448}
144449
144450#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */
144451
144452/************** End of fts3_tokenize_vtab.c **********************************/
144453/************** Begin file fts3_write.c **************************************/
144454/*
144455** 2009 Oct 23
144456**
144457** The author disclaims copyright to this source code.  In place of
144458** a legal notice, here is a blessing:
144459**
144460**    May you do good and not evil.
144461**    May you find forgiveness for yourself and forgive others.
144462**    May you share freely, never taking more than you give.
144463**
144464******************************************************************************
144465**
144466** This file is part of the SQLite FTS3 extension module. Specifically,
144467** this file contains code to insert, update and delete rows from FTS3
144468** tables. It also contains code to merge FTS3 b-tree segments. Some
144469** of the sub-routines used to merge segments are also used by the query
144470** code in fts3.c.
144471*/
144472
144473/* #include "fts3Int.h" */
144474#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
144475
144476/* #include <string.h> */
144477/* #include <assert.h> */
144478/* #include <stdlib.h> */
144479
144480
144481#define FTS_MAX_APPENDABLE_HEIGHT 16
144482
144483/*
144484** When full-text index nodes are loaded from disk, the buffer that they
144485** are loaded into has the following number of bytes of padding at the end
144486** of it. i.e. if a full-text index node is 900 bytes in size, then a buffer
144487** of 920 bytes is allocated for it.
144488**
144489** This means that if we have a pointer into a buffer containing node data,
144490** it is always safe to read up to two varints from it without risking an
144491** overread, even if the node data is corrupted.
144492*/
144493#define FTS3_NODE_PADDING (FTS3_VARINT_MAX*2)
144494
144495/*
144496** Under certain circumstances, b-tree nodes (doclists) can be loaded into
144497** memory incrementally instead of all at once. This can be a big performance
144498** win (reduced IO and CPU) if SQLite stops calling the virtual table xNext()
144499** method before retrieving all query results (as may happen, for example,
144500** if a query has a LIMIT clause).
144501**
144502** Incremental loading is used for b-tree nodes FTS3_NODE_CHUNK_THRESHOLD
144503** bytes and larger. Nodes are loaded in chunks of FTS3_NODE_CHUNKSIZE bytes.
144504** The code is written so that the hard lower-limit for each of these values
144505** is 1. Clearly such small values would be inefficient, but can be useful
144506** for testing purposes.
144507**
144508** If this module is built with SQLITE_TEST defined, these constants may
144509** be overridden at runtime for testing purposes. File fts3_test.c contains
144510** a Tcl interface to read and write the values.
144511*/
144512#ifdef SQLITE_TEST
144513int test_fts3_node_chunksize = (4*1024);
144514int test_fts3_node_chunk_threshold = (4*1024)*4;
144515# define FTS3_NODE_CHUNKSIZE       test_fts3_node_chunksize
144516# define FTS3_NODE_CHUNK_THRESHOLD test_fts3_node_chunk_threshold
144517#else
144518# define FTS3_NODE_CHUNKSIZE (4*1024)
144519# define FTS3_NODE_CHUNK_THRESHOLD (FTS3_NODE_CHUNKSIZE*4)
144520#endif
144521
144522/*
144523** The two values that may be meaningfully bound to the :1 parameter in
144524** statements SQL_REPLACE_STAT and SQL_SELECT_STAT.
144525*/
144526#define FTS_STAT_DOCTOTAL      0
144527#define FTS_STAT_INCRMERGEHINT 1
144528#define FTS_STAT_AUTOINCRMERGE 2
144529
144530/*
144531** If FTS_LOG_MERGES is defined, call sqlite3_log() to report each automatic
144532** and incremental merge operation that takes place. This is used for
144533** debugging FTS only, it should not usually be turned on in production
144534** systems.
144535*/
144536#ifdef FTS3_LOG_MERGES
144537static void fts3LogMerge(int nMerge, sqlite3_int64 iAbsLevel){
144538  sqlite3_log(SQLITE_OK, "%d-way merge from level %d", nMerge, (int)iAbsLevel);
144539}
144540#else
144541#define fts3LogMerge(x, y)
144542#endif
144543
144544
144545typedef struct PendingList PendingList;
144546typedef struct SegmentNode SegmentNode;
144547typedef struct SegmentWriter SegmentWriter;
144548
144549/*
144550** An instance of the following data structure is used to build doclists
144551** incrementally. See function fts3PendingListAppend() for details.
144552*/
144553struct PendingList {
144554  int nData;
144555  char *aData;
144556  int nSpace;
144557  sqlite3_int64 iLastDocid;
144558  sqlite3_int64 iLastCol;
144559  sqlite3_int64 iLastPos;
144560};
144561
144562
144563/*
144564** Each cursor has a (possibly empty) linked list of the following objects.
144565*/
144566struct Fts3DeferredToken {
144567  Fts3PhraseToken *pToken;        /* Pointer to corresponding expr token */
144568  int iCol;                       /* Column token must occur in */
144569  Fts3DeferredToken *pNext;       /* Next in list of deferred tokens */
144570  PendingList *pList;             /* Doclist is assembled here */
144571};
144572
144573/*
144574** An instance of this structure is used to iterate through the terms on
144575** a contiguous set of segment b-tree leaf nodes. Although the details of
144576** this structure are only manipulated by code in this file, opaque handles
144577** of type Fts3SegReader* are also used by code in fts3.c to iterate through
144578** terms when querying the full-text index. See functions:
144579**
144580**   sqlite3Fts3SegReaderNew()
144581**   sqlite3Fts3SegReaderFree()
144582**   sqlite3Fts3SegReaderIterate()
144583**
144584** Methods used to manipulate Fts3SegReader structures:
144585**
144586**   fts3SegReaderNext()
144587**   fts3SegReaderFirstDocid()
144588**   fts3SegReaderNextDocid()
144589*/
144590struct Fts3SegReader {
144591  int iIdx;                       /* Index within level, or 0x7FFFFFFF for PT */
144592  u8 bLookup;                     /* True for a lookup only */
144593  u8 rootOnly;                    /* True for a root-only reader */
144594
144595  sqlite3_int64 iStartBlock;      /* Rowid of first leaf block to traverse */
144596  sqlite3_int64 iLeafEndBlock;    /* Rowid of final leaf block to traverse */
144597  sqlite3_int64 iEndBlock;        /* Rowid of final block in segment (or 0) */
144598  sqlite3_int64 iCurrentBlock;    /* Current leaf block (or 0) */
144599
144600  char *aNode;                    /* Pointer to node data (or NULL) */
144601  int nNode;                      /* Size of buffer at aNode (or 0) */
144602  int nPopulate;                  /* If >0, bytes of buffer aNode[] loaded */
144603  sqlite3_blob *pBlob;            /* If not NULL, blob handle to read node */
144604
144605  Fts3HashElem **ppNextElem;
144606
144607  /* Variables set by fts3SegReaderNext(). These may be read directly
144608  ** by the caller. They are valid from the time SegmentReaderNew() returns
144609  ** until SegmentReaderNext() returns something other than SQLITE_OK
144610  ** (i.e. SQLITE_DONE).
144611  */
144612  int nTerm;                      /* Number of bytes in current term */
144613  char *zTerm;                    /* Pointer to current term */
144614  int nTermAlloc;                 /* Allocated size of zTerm buffer */
144615  char *aDoclist;                 /* Pointer to doclist of current entry */
144616  int nDoclist;                   /* Size of doclist in current entry */
144617
144618  /* The following variables are used by fts3SegReaderNextDocid() to iterate
144619  ** through the current doclist (aDoclist/nDoclist).
144620  */
144621  char *pOffsetList;
144622  int nOffsetList;                /* For descending pending seg-readers only */
144623  sqlite3_int64 iDocid;
144624};
144625
144626#define fts3SegReaderIsPending(p) ((p)->ppNextElem!=0)
144627#define fts3SegReaderIsRootOnly(p) ((p)->rootOnly!=0)
144628
144629/*
144630** An instance of this structure is used to create a segment b-tree in the
144631** database. The internal details of this type are only accessed by the
144632** following functions:
144633**
144634**   fts3SegWriterAdd()
144635**   fts3SegWriterFlush()
144636**   fts3SegWriterFree()
144637*/
144638struct SegmentWriter {
144639  SegmentNode *pTree;             /* Pointer to interior tree structure */
144640  sqlite3_int64 iFirst;           /* First slot in %_segments written */
144641  sqlite3_int64 iFree;            /* Next free slot in %_segments */
144642  char *zTerm;                    /* Pointer to previous term buffer */
144643  int nTerm;                      /* Number of bytes in zTerm */
144644  int nMalloc;                    /* Size of malloc'd buffer at zMalloc */
144645  char *zMalloc;                  /* Malloc'd space (possibly) used for zTerm */
144646  int nSize;                      /* Size of allocation at aData */
144647  int nData;                      /* Bytes of data in aData */
144648  char *aData;                    /* Pointer to block from malloc() */
144649  i64 nLeafData;                  /* Number of bytes of leaf data written */
144650};
144651
144652/*
144653** Type SegmentNode is used by the following three functions to create
144654** the interior part of the segment b+-tree structures (everything except
144655** the leaf nodes). These functions and type are only ever used by code
144656** within the fts3SegWriterXXX() family of functions described above.
144657**
144658**   fts3NodeAddTerm()
144659**   fts3NodeWrite()
144660**   fts3NodeFree()
144661**
144662** When a b+tree is written to the database (either as a result of a merge
144663** or the pending-terms table being flushed), leaves are written into the
144664** database file as soon as they are completely populated. The interior of
144665** the tree is assembled in memory and written out only once all leaves have
144666** been populated and stored. This is Ok, as the b+-tree fanout is usually
144667** very large, meaning that the interior of the tree consumes relatively
144668** little memory.
144669*/
144670struct SegmentNode {
144671  SegmentNode *pParent;           /* Parent node (or NULL for root node) */
144672  SegmentNode *pRight;            /* Pointer to right-sibling */
144673  SegmentNode *pLeftmost;         /* Pointer to left-most node of this depth */
144674  int nEntry;                     /* Number of terms written to node so far */
144675  char *zTerm;                    /* Pointer to previous term buffer */
144676  int nTerm;                      /* Number of bytes in zTerm */
144677  int nMalloc;                    /* Size of malloc'd buffer at zMalloc */
144678  char *zMalloc;                  /* Malloc'd space (possibly) used for zTerm */
144679  int nData;                      /* Bytes of valid data so far */
144680  char *aData;                    /* Node data */
144681};
144682
144683/*
144684** Valid values for the second argument to fts3SqlStmt().
144685*/
144686#define SQL_DELETE_CONTENT             0
144687#define SQL_IS_EMPTY                   1
144688#define SQL_DELETE_ALL_CONTENT         2
144689#define SQL_DELETE_ALL_SEGMENTS        3
144690#define SQL_DELETE_ALL_SEGDIR          4
144691#define SQL_DELETE_ALL_DOCSIZE         5
144692#define SQL_DELETE_ALL_STAT            6
144693#define SQL_SELECT_CONTENT_BY_ROWID    7
144694#define SQL_NEXT_SEGMENT_INDEX         8
144695#define SQL_INSERT_SEGMENTS            9
144696#define SQL_NEXT_SEGMENTS_ID          10
144697#define SQL_INSERT_SEGDIR             11
144698#define SQL_SELECT_LEVEL              12
144699#define SQL_SELECT_LEVEL_RANGE        13
144700#define SQL_SELECT_LEVEL_COUNT        14
144701#define SQL_SELECT_SEGDIR_MAX_LEVEL   15
144702#define SQL_DELETE_SEGDIR_LEVEL       16
144703#define SQL_DELETE_SEGMENTS_RANGE     17
144704#define SQL_CONTENT_INSERT            18
144705#define SQL_DELETE_DOCSIZE            19
144706#define SQL_REPLACE_DOCSIZE           20
144707#define SQL_SELECT_DOCSIZE            21
144708#define SQL_SELECT_STAT               22
144709#define SQL_REPLACE_STAT              23
144710
144711#define SQL_SELECT_ALL_PREFIX_LEVEL   24
144712#define SQL_DELETE_ALL_TERMS_SEGDIR   25
144713#define SQL_DELETE_SEGDIR_RANGE       26
144714#define SQL_SELECT_ALL_LANGID         27
144715#define SQL_FIND_MERGE_LEVEL          28
144716#define SQL_MAX_LEAF_NODE_ESTIMATE    29
144717#define SQL_DELETE_SEGDIR_ENTRY       30
144718#define SQL_SHIFT_SEGDIR_ENTRY        31
144719#define SQL_SELECT_SEGDIR             32
144720#define SQL_CHOMP_SEGDIR              33
144721#define SQL_SEGMENT_IS_APPENDABLE     34
144722#define SQL_SELECT_INDEXES            35
144723#define SQL_SELECT_MXLEVEL            36
144724
144725#define SQL_SELECT_LEVEL_RANGE2       37
144726#define SQL_UPDATE_LEVEL_IDX          38
144727#define SQL_UPDATE_LEVEL              39
144728
144729/*
144730** This function is used to obtain an SQLite prepared statement handle
144731** for the statement identified by the second argument. If successful,
144732** *pp is set to the requested statement handle and SQLITE_OK returned.
144733** Otherwise, an SQLite error code is returned and *pp is set to 0.
144734**
144735** If argument apVal is not NULL, then it must point to an array with
144736** at least as many entries as the requested statement has bound
144737** parameters. The values are bound to the statements parameters before
144738** returning.
144739*/
144740static int fts3SqlStmt(
144741  Fts3Table *p,                   /* Virtual table handle */
144742  int eStmt,                      /* One of the SQL_XXX constants above */
144743  sqlite3_stmt **pp,              /* OUT: Statement handle */
144744  sqlite3_value **apVal           /* Values to bind to statement */
144745){
144746  const char *azSql[] = {
144747/* 0  */  "DELETE FROM %Q.'%q_content' WHERE rowid = ?",
144748/* 1  */  "SELECT NOT EXISTS(SELECT docid FROM %Q.'%q_content' WHERE rowid!=?)",
144749/* 2  */  "DELETE FROM %Q.'%q_content'",
144750/* 3  */  "DELETE FROM %Q.'%q_segments'",
144751/* 4  */  "DELETE FROM %Q.'%q_segdir'",
144752/* 5  */  "DELETE FROM %Q.'%q_docsize'",
144753/* 6  */  "DELETE FROM %Q.'%q_stat'",
144754/* 7  */  "SELECT %s WHERE rowid=?",
144755/* 8  */  "SELECT (SELECT max(idx) FROM %Q.'%q_segdir' WHERE level = ?) + 1",
144756/* 9  */  "REPLACE INTO %Q.'%q_segments'(blockid, block) VALUES(?, ?)",
144757/* 10 */  "SELECT coalesce((SELECT max(blockid) FROM %Q.'%q_segments') + 1, 1)",
144758/* 11 */  "REPLACE INTO %Q.'%q_segdir' VALUES(?,?,?,?,?,?)",
144759
144760          /* Return segments in order from oldest to newest.*/
144761/* 12 */  "SELECT idx, start_block, leaves_end_block, end_block, root "
144762            "FROM %Q.'%q_segdir' WHERE level = ? ORDER BY idx ASC",
144763/* 13 */  "SELECT idx, start_block, leaves_end_block, end_block, root "
144764            "FROM %Q.'%q_segdir' WHERE level BETWEEN ? AND ?"
144765            "ORDER BY level DESC, idx ASC",
144766
144767/* 14 */  "SELECT count(*) FROM %Q.'%q_segdir' WHERE level = ?",
144768/* 15 */  "SELECT max(level) FROM %Q.'%q_segdir' WHERE level BETWEEN ? AND ?",
144769
144770/* 16 */  "DELETE FROM %Q.'%q_segdir' WHERE level = ?",
144771/* 17 */  "DELETE FROM %Q.'%q_segments' WHERE blockid BETWEEN ? AND ?",
144772/* 18 */  "INSERT INTO %Q.'%q_content' VALUES(%s)",
144773/* 19 */  "DELETE FROM %Q.'%q_docsize' WHERE docid = ?",
144774/* 20 */  "REPLACE INTO %Q.'%q_docsize' VALUES(?,?)",
144775/* 21 */  "SELECT size FROM %Q.'%q_docsize' WHERE docid=?",
144776/* 22 */  "SELECT value FROM %Q.'%q_stat' WHERE id=?",
144777/* 23 */  "REPLACE INTO %Q.'%q_stat' VALUES(?,?)",
144778/* 24 */  "",
144779/* 25 */  "",
144780
144781/* 26 */ "DELETE FROM %Q.'%q_segdir' WHERE level BETWEEN ? AND ?",
144782/* 27 */ "SELECT ? UNION SELECT level / (1024 * ?) FROM %Q.'%q_segdir'",
144783
144784/* This statement is used to determine which level to read the input from
144785** when performing an incremental merge. It returns the absolute level number
144786** of the oldest level in the db that contains at least ? segments. Or,
144787** if no level in the FTS index contains more than ? segments, the statement
144788** returns zero rows.  */
144789/* 28 */ "SELECT level FROM %Q.'%q_segdir' GROUP BY level HAVING count(*)>=?"
144790         "  ORDER BY (level %% 1024) ASC LIMIT 1",
144791
144792/* Estimate the upper limit on the number of leaf nodes in a new segment
144793** created by merging the oldest :2 segments from absolute level :1. See
144794** function sqlite3Fts3Incrmerge() for details.  */
144795/* 29 */ "SELECT 2 * total(1 + leaves_end_block - start_block) "
144796         "  FROM %Q.'%q_segdir' WHERE level = ? AND idx < ?",
144797
144798/* SQL_DELETE_SEGDIR_ENTRY
144799**   Delete the %_segdir entry on absolute level :1 with index :2.  */
144800/* 30 */ "DELETE FROM %Q.'%q_segdir' WHERE level = ? AND idx = ?",
144801
144802/* SQL_SHIFT_SEGDIR_ENTRY
144803**   Modify the idx value for the segment with idx=:3 on absolute level :2
144804**   to :1.  */
144805/* 31 */ "UPDATE %Q.'%q_segdir' SET idx = ? WHERE level=? AND idx=?",
144806
144807/* SQL_SELECT_SEGDIR
144808**   Read a single entry from the %_segdir table. The entry from absolute
144809**   level :1 with index value :2.  */
144810/* 32 */  "SELECT idx, start_block, leaves_end_block, end_block, root "
144811            "FROM %Q.'%q_segdir' WHERE level = ? AND idx = ?",
144812
144813/* SQL_CHOMP_SEGDIR
144814**   Update the start_block (:1) and root (:2) fields of the %_segdir
144815**   entry located on absolute level :3 with index :4.  */
144816/* 33 */  "UPDATE %Q.'%q_segdir' SET start_block = ?, root = ?"
144817            "WHERE level = ? AND idx = ?",
144818
144819/* SQL_SEGMENT_IS_APPENDABLE
144820**   Return a single row if the segment with end_block=? is appendable. Or
144821**   no rows otherwise.  */
144822/* 34 */  "SELECT 1 FROM %Q.'%q_segments' WHERE blockid=? AND block IS NULL",
144823
144824/* SQL_SELECT_INDEXES
144825**   Return the list of valid segment indexes for absolute level ?  */
144826/* 35 */  "SELECT idx FROM %Q.'%q_segdir' WHERE level=? ORDER BY 1 ASC",
144827
144828/* SQL_SELECT_MXLEVEL
144829**   Return the largest relative level in the FTS index or indexes.  */
144830/* 36 */  "SELECT max( level %% 1024 ) FROM %Q.'%q_segdir'",
144831
144832          /* Return segments in order from oldest to newest.*/
144833/* 37 */  "SELECT level, idx, end_block "
144834            "FROM %Q.'%q_segdir' WHERE level BETWEEN ? AND ? "
144835            "ORDER BY level DESC, idx ASC",
144836
144837          /* Update statements used while promoting segments */
144838/* 38 */  "UPDATE OR FAIL %Q.'%q_segdir' SET level=-1,idx=? "
144839            "WHERE level=? AND idx=?",
144840/* 39 */  "UPDATE OR FAIL %Q.'%q_segdir' SET level=? WHERE level=-1"
144841
144842  };
144843  int rc = SQLITE_OK;
144844  sqlite3_stmt *pStmt;
144845
144846  assert( SizeofArray(azSql)==SizeofArray(p->aStmt) );
144847  assert( eStmt<SizeofArray(azSql) && eStmt>=0 );
144848
144849  pStmt = p->aStmt[eStmt];
144850  if( !pStmt ){
144851    char *zSql;
144852    if( eStmt==SQL_CONTENT_INSERT ){
144853      zSql = sqlite3_mprintf(azSql[eStmt], p->zDb, p->zName, p->zWriteExprlist);
144854    }else if( eStmt==SQL_SELECT_CONTENT_BY_ROWID ){
144855      zSql = sqlite3_mprintf(azSql[eStmt], p->zReadExprlist);
144856    }else{
144857      zSql = sqlite3_mprintf(azSql[eStmt], p->zDb, p->zName);
144858    }
144859    if( !zSql ){
144860      rc = SQLITE_NOMEM;
144861    }else{
144862      rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, NULL);
144863      sqlite3_free(zSql);
144864      assert( rc==SQLITE_OK || pStmt==0 );
144865      p->aStmt[eStmt] = pStmt;
144866    }
144867  }
144868  if( apVal ){
144869    int i;
144870    int nParam = sqlite3_bind_parameter_count(pStmt);
144871    for(i=0; rc==SQLITE_OK && i<nParam; i++){
144872      rc = sqlite3_bind_value(pStmt, i+1, apVal[i]);
144873    }
144874  }
144875  *pp = pStmt;
144876  return rc;
144877}
144878
144879
144880static int fts3SelectDocsize(
144881  Fts3Table *pTab,                /* FTS3 table handle */
144882  sqlite3_int64 iDocid,           /* Docid to bind for SQL_SELECT_DOCSIZE */
144883  sqlite3_stmt **ppStmt           /* OUT: Statement handle */
144884){
144885  sqlite3_stmt *pStmt = 0;        /* Statement requested from fts3SqlStmt() */
144886  int rc;                         /* Return code */
144887
144888  rc = fts3SqlStmt(pTab, SQL_SELECT_DOCSIZE, &pStmt, 0);
144889  if( rc==SQLITE_OK ){
144890    sqlite3_bind_int64(pStmt, 1, iDocid);
144891    rc = sqlite3_step(pStmt);
144892    if( rc!=SQLITE_ROW || sqlite3_column_type(pStmt, 0)!=SQLITE_BLOB ){
144893      rc = sqlite3_reset(pStmt);
144894      if( rc==SQLITE_OK ) rc = FTS_CORRUPT_VTAB;
144895      pStmt = 0;
144896    }else{
144897      rc = SQLITE_OK;
144898    }
144899  }
144900
144901  *ppStmt = pStmt;
144902  return rc;
144903}
144904
144905SQLITE_PRIVATE int sqlite3Fts3SelectDoctotal(
144906  Fts3Table *pTab,                /* Fts3 table handle */
144907  sqlite3_stmt **ppStmt           /* OUT: Statement handle */
144908){
144909  sqlite3_stmt *pStmt = 0;
144910  int rc;
144911  rc = fts3SqlStmt(pTab, SQL_SELECT_STAT, &pStmt, 0);
144912  if( rc==SQLITE_OK ){
144913    sqlite3_bind_int(pStmt, 1, FTS_STAT_DOCTOTAL);
144914    if( sqlite3_step(pStmt)!=SQLITE_ROW
144915     || sqlite3_column_type(pStmt, 0)!=SQLITE_BLOB
144916    ){
144917      rc = sqlite3_reset(pStmt);
144918      if( rc==SQLITE_OK ) rc = FTS_CORRUPT_VTAB;
144919      pStmt = 0;
144920    }
144921  }
144922  *ppStmt = pStmt;
144923  return rc;
144924}
144925
144926SQLITE_PRIVATE int sqlite3Fts3SelectDocsize(
144927  Fts3Table *pTab,                /* Fts3 table handle */
144928  sqlite3_int64 iDocid,           /* Docid to read size data for */
144929  sqlite3_stmt **ppStmt           /* OUT: Statement handle */
144930){
144931  return fts3SelectDocsize(pTab, iDocid, ppStmt);
144932}
144933
144934/*
144935** Similar to fts3SqlStmt(). Except, after binding the parameters in
144936** array apVal[] to the SQL statement identified by eStmt, the statement
144937** is executed.
144938**
144939** Returns SQLITE_OK if the statement is successfully executed, or an
144940** SQLite error code otherwise.
144941*/
144942static void fts3SqlExec(
144943  int *pRC,                /* Result code */
144944  Fts3Table *p,            /* The FTS3 table */
144945  int eStmt,               /* Index of statement to evaluate */
144946  sqlite3_value **apVal    /* Parameters to bind */
144947){
144948  sqlite3_stmt *pStmt;
144949  int rc;
144950  if( *pRC ) return;
144951  rc = fts3SqlStmt(p, eStmt, &pStmt, apVal);
144952  if( rc==SQLITE_OK ){
144953    sqlite3_step(pStmt);
144954    rc = sqlite3_reset(pStmt);
144955  }
144956  *pRC = rc;
144957}
144958
144959
144960/*
144961** This function ensures that the caller has obtained an exclusive
144962** shared-cache table-lock on the %_segdir table. This is required before
144963** writing data to the fts3 table. If this lock is not acquired first, then
144964** the caller may end up attempting to take this lock as part of committing
144965** a transaction, causing SQLite to return SQLITE_LOCKED or
144966** LOCKED_SHAREDCACHEto a COMMIT command.
144967**
144968** It is best to avoid this because if FTS3 returns any error when
144969** committing a transaction, the whole transaction will be rolled back.
144970** And this is not what users expect when they get SQLITE_LOCKED_SHAREDCACHE.
144971** It can still happen if the user locks the underlying tables directly
144972** instead of accessing them via FTS.
144973*/
144974static int fts3Writelock(Fts3Table *p){
144975  int rc = SQLITE_OK;
144976
144977  if( p->nPendingData==0 ){
144978    sqlite3_stmt *pStmt;
144979    rc = fts3SqlStmt(p, SQL_DELETE_SEGDIR_LEVEL, &pStmt, 0);
144980    if( rc==SQLITE_OK ){
144981      sqlite3_bind_null(pStmt, 1);
144982      sqlite3_step(pStmt);
144983      rc = sqlite3_reset(pStmt);
144984    }
144985  }
144986
144987  return rc;
144988}
144989
144990/*
144991** FTS maintains a separate indexes for each language-id (a 32-bit integer).
144992** Within each language id, a separate index is maintained to store the
144993** document terms, and each configured prefix size (configured the FTS
144994** "prefix=" option). And each index consists of multiple levels ("relative
144995** levels").
144996**
144997** All three of these values (the language id, the specific index and the
144998** level within the index) are encoded in 64-bit integer values stored
144999** in the %_segdir table on disk. This function is used to convert three
145000** separate component values into the single 64-bit integer value that
145001** can be used to query the %_segdir table.
145002**
145003** Specifically, each language-id/index combination is allocated 1024
145004** 64-bit integer level values ("absolute levels"). The main terms index
145005** for language-id 0 is allocate values 0-1023. The first prefix index
145006** (if any) for language-id 0 is allocated values 1024-2047. And so on.
145007** Language 1 indexes are allocated immediately following language 0.
145008**
145009** So, for a system with nPrefix prefix indexes configured, the block of
145010** absolute levels that corresponds to language-id iLangid and index
145011** iIndex starts at absolute level ((iLangid * (nPrefix+1) + iIndex) * 1024).
145012*/
145013static sqlite3_int64 getAbsoluteLevel(
145014  Fts3Table *p,                   /* FTS3 table handle */
145015  int iLangid,                    /* Language id */
145016  int iIndex,                     /* Index in p->aIndex[] */
145017  int iLevel                      /* Level of segments */
145018){
145019  sqlite3_int64 iBase;            /* First absolute level for iLangid/iIndex */
145020  assert( iLangid>=0 );
145021  assert( p->nIndex>0 );
145022  assert( iIndex>=0 && iIndex<p->nIndex );
145023
145024  iBase = ((sqlite3_int64)iLangid * p->nIndex + iIndex) * FTS3_SEGDIR_MAXLEVEL;
145025  return iBase + iLevel;
145026}
145027
145028/*
145029** Set *ppStmt to a statement handle that may be used to iterate through
145030** all rows in the %_segdir table, from oldest to newest. If successful,
145031** return SQLITE_OK. If an error occurs while preparing the statement,
145032** return an SQLite error code.
145033**
145034** There is only ever one instance of this SQL statement compiled for
145035** each FTS3 table.
145036**
145037** The statement returns the following columns from the %_segdir table:
145038**
145039**   0: idx
145040**   1: start_block
145041**   2: leaves_end_block
145042**   3: end_block
145043**   4: root
145044*/
145045SQLITE_PRIVATE int sqlite3Fts3AllSegdirs(
145046  Fts3Table *p,                   /* FTS3 table */
145047  int iLangid,                    /* Language being queried */
145048  int iIndex,                     /* Index for p->aIndex[] */
145049  int iLevel,                     /* Level to select (relative level) */
145050  sqlite3_stmt **ppStmt           /* OUT: Compiled statement */
145051){
145052  int rc;
145053  sqlite3_stmt *pStmt = 0;
145054
145055  assert( iLevel==FTS3_SEGCURSOR_ALL || iLevel>=0 );
145056  assert( iLevel<FTS3_SEGDIR_MAXLEVEL );
145057  assert( iIndex>=0 && iIndex<p->nIndex );
145058
145059  if( iLevel<0 ){
145060    /* "SELECT * FROM %_segdir WHERE level BETWEEN ? AND ? ORDER BY ..." */
145061    rc = fts3SqlStmt(p, SQL_SELECT_LEVEL_RANGE, &pStmt, 0);
145062    if( rc==SQLITE_OK ){
145063      sqlite3_bind_int64(pStmt, 1, getAbsoluteLevel(p, iLangid, iIndex, 0));
145064      sqlite3_bind_int64(pStmt, 2,
145065          getAbsoluteLevel(p, iLangid, iIndex, FTS3_SEGDIR_MAXLEVEL-1)
145066      );
145067    }
145068  }else{
145069    /* "SELECT * FROM %_segdir WHERE level = ? ORDER BY ..." */
145070    rc = fts3SqlStmt(p, SQL_SELECT_LEVEL, &pStmt, 0);
145071    if( rc==SQLITE_OK ){
145072      sqlite3_bind_int64(pStmt, 1, getAbsoluteLevel(p, iLangid, iIndex,iLevel));
145073    }
145074  }
145075  *ppStmt = pStmt;
145076  return rc;
145077}
145078
145079
145080/*
145081** Append a single varint to a PendingList buffer. SQLITE_OK is returned
145082** if successful, or an SQLite error code otherwise.
145083**
145084** This function also serves to allocate the PendingList structure itself.
145085** For example, to create a new PendingList structure containing two
145086** varints:
145087**
145088**   PendingList *p = 0;
145089**   fts3PendingListAppendVarint(&p, 1);
145090**   fts3PendingListAppendVarint(&p, 2);
145091*/
145092static int fts3PendingListAppendVarint(
145093  PendingList **pp,               /* IN/OUT: Pointer to PendingList struct */
145094  sqlite3_int64 i                 /* Value to append to data */
145095){
145096  PendingList *p = *pp;
145097
145098  /* Allocate or grow the PendingList as required. */
145099  if( !p ){
145100    p = sqlite3_malloc(sizeof(*p) + 100);
145101    if( !p ){
145102      return SQLITE_NOMEM;
145103    }
145104    p->nSpace = 100;
145105    p->aData = (char *)&p[1];
145106    p->nData = 0;
145107  }
145108  else if( p->nData+FTS3_VARINT_MAX+1>p->nSpace ){
145109    int nNew = p->nSpace * 2;
145110    p = sqlite3_realloc(p, sizeof(*p) + nNew);
145111    if( !p ){
145112      sqlite3_free(*pp);
145113      *pp = 0;
145114      return SQLITE_NOMEM;
145115    }
145116    p->nSpace = nNew;
145117    p->aData = (char *)&p[1];
145118  }
145119
145120  /* Append the new serialized varint to the end of the list. */
145121  p->nData += sqlite3Fts3PutVarint(&p->aData[p->nData], i);
145122  p->aData[p->nData] = '\0';
145123  *pp = p;
145124  return SQLITE_OK;
145125}
145126
145127/*
145128** Add a docid/column/position entry to a PendingList structure. Non-zero
145129** is returned if the structure is sqlite3_realloced as part of adding
145130** the entry. Otherwise, zero.
145131**
145132** If an OOM error occurs, *pRc is set to SQLITE_NOMEM before returning.
145133** Zero is always returned in this case. Otherwise, if no OOM error occurs,
145134** it is set to SQLITE_OK.
145135*/
145136static int fts3PendingListAppend(
145137  PendingList **pp,               /* IN/OUT: PendingList structure */
145138  sqlite3_int64 iDocid,           /* Docid for entry to add */
145139  sqlite3_int64 iCol,             /* Column for entry to add */
145140  sqlite3_int64 iPos,             /* Position of term for entry to add */
145141  int *pRc                        /* OUT: Return code */
145142){
145143  PendingList *p = *pp;
145144  int rc = SQLITE_OK;
145145
145146  assert( !p || p->iLastDocid<=iDocid );
145147
145148  if( !p || p->iLastDocid!=iDocid ){
145149    sqlite3_int64 iDelta = iDocid - (p ? p->iLastDocid : 0);
145150    if( p ){
145151      assert( p->nData<p->nSpace );
145152      assert( p->aData[p->nData]==0 );
145153      p->nData++;
145154    }
145155    if( SQLITE_OK!=(rc = fts3PendingListAppendVarint(&p, iDelta)) ){
145156      goto pendinglistappend_out;
145157    }
145158    p->iLastCol = -1;
145159    p->iLastPos = 0;
145160    p->iLastDocid = iDocid;
145161  }
145162  if( iCol>0 && p->iLastCol!=iCol ){
145163    if( SQLITE_OK!=(rc = fts3PendingListAppendVarint(&p, 1))
145164     || SQLITE_OK!=(rc = fts3PendingListAppendVarint(&p, iCol))
145165    ){
145166      goto pendinglistappend_out;
145167    }
145168    p->iLastCol = iCol;
145169    p->iLastPos = 0;
145170  }
145171  if( iCol>=0 ){
145172    assert( iPos>p->iLastPos || (iPos==0 && p->iLastPos==0) );
145173    rc = fts3PendingListAppendVarint(&p, 2+iPos-p->iLastPos);
145174    if( rc==SQLITE_OK ){
145175      p->iLastPos = iPos;
145176    }
145177  }
145178
145179 pendinglistappend_out:
145180  *pRc = rc;
145181  if( p!=*pp ){
145182    *pp = p;
145183    return 1;
145184  }
145185  return 0;
145186}
145187
145188/*
145189** Free a PendingList object allocated by fts3PendingListAppend().
145190*/
145191static void fts3PendingListDelete(PendingList *pList){
145192  sqlite3_free(pList);
145193}
145194
145195/*
145196** Add an entry to one of the pending-terms hash tables.
145197*/
145198static int fts3PendingTermsAddOne(
145199  Fts3Table *p,
145200  int iCol,
145201  int iPos,
145202  Fts3Hash *pHash,                /* Pending terms hash table to add entry to */
145203  const char *zToken,
145204  int nToken
145205){
145206  PendingList *pList;
145207  int rc = SQLITE_OK;
145208
145209  pList = (PendingList *)fts3HashFind(pHash, zToken, nToken);
145210  if( pList ){
145211    p->nPendingData -= (pList->nData + nToken + sizeof(Fts3HashElem));
145212  }
145213  if( fts3PendingListAppend(&pList, p->iPrevDocid, iCol, iPos, &rc) ){
145214    if( pList==fts3HashInsert(pHash, zToken, nToken, pList) ){
145215      /* Malloc failed while inserting the new entry. This can only
145216      ** happen if there was no previous entry for this token.
145217      */
145218      assert( 0==fts3HashFind(pHash, zToken, nToken) );
145219      sqlite3_free(pList);
145220      rc = SQLITE_NOMEM;
145221    }
145222  }
145223  if( rc==SQLITE_OK ){
145224    p->nPendingData += (pList->nData + nToken + sizeof(Fts3HashElem));
145225  }
145226  return rc;
145227}
145228
145229/*
145230** Tokenize the nul-terminated string zText and add all tokens to the
145231** pending-terms hash-table. The docid used is that currently stored in
145232** p->iPrevDocid, and the column is specified by argument iCol.
145233**
145234** If successful, SQLITE_OK is returned. Otherwise, an SQLite error code.
145235*/
145236static int fts3PendingTermsAdd(
145237  Fts3Table *p,                   /* Table into which text will be inserted */
145238  int iLangid,                    /* Language id to use */
145239  const char *zText,              /* Text of document to be inserted */
145240  int iCol,                       /* Column into which text is being inserted */
145241  u32 *pnWord                     /* IN/OUT: Incr. by number tokens inserted */
145242){
145243  int rc;
145244  int iStart = 0;
145245  int iEnd = 0;
145246  int iPos = 0;
145247  int nWord = 0;
145248
145249  char const *zToken;
145250  int nToken = 0;
145251
145252  sqlite3_tokenizer *pTokenizer = p->pTokenizer;
145253  sqlite3_tokenizer_module const *pModule = pTokenizer->pModule;
145254  sqlite3_tokenizer_cursor *pCsr;
145255  int (*xNext)(sqlite3_tokenizer_cursor *pCursor,
145256      const char**,int*,int*,int*,int*);
145257
145258  assert( pTokenizer && pModule );
145259
145260  /* If the user has inserted a NULL value, this function may be called with
145261  ** zText==0. In this case, add zero token entries to the hash table and
145262  ** return early. */
145263  if( zText==0 ){
145264    *pnWord = 0;
145265    return SQLITE_OK;
145266  }
145267
145268  rc = sqlite3Fts3OpenTokenizer(pTokenizer, iLangid, zText, -1, &pCsr);
145269  if( rc!=SQLITE_OK ){
145270    return rc;
145271  }
145272
145273  xNext = pModule->xNext;
145274  while( SQLITE_OK==rc
145275      && SQLITE_OK==(rc = xNext(pCsr, &zToken, &nToken, &iStart, &iEnd, &iPos))
145276  ){
145277    int i;
145278    if( iPos>=nWord ) nWord = iPos+1;
145279
145280    /* Positions cannot be negative; we use -1 as a terminator internally.
145281    ** Tokens must have a non-zero length.
145282    */
145283    if( iPos<0 || !zToken || nToken<=0 ){
145284      rc = SQLITE_ERROR;
145285      break;
145286    }
145287
145288    /* Add the term to the terms index */
145289    rc = fts3PendingTermsAddOne(
145290        p, iCol, iPos, &p->aIndex[0].hPending, zToken, nToken
145291    );
145292
145293    /* Add the term to each of the prefix indexes that it is not too
145294    ** short for. */
145295    for(i=1; rc==SQLITE_OK && i<p->nIndex; i++){
145296      struct Fts3Index *pIndex = &p->aIndex[i];
145297      if( nToken<pIndex->nPrefix ) continue;
145298      rc = fts3PendingTermsAddOne(
145299          p, iCol, iPos, &pIndex->hPending, zToken, pIndex->nPrefix
145300      );
145301    }
145302  }
145303
145304  pModule->xClose(pCsr);
145305  *pnWord += nWord;
145306  return (rc==SQLITE_DONE ? SQLITE_OK : rc);
145307}
145308
145309/*
145310** Calling this function indicates that subsequent calls to
145311** fts3PendingTermsAdd() are to add term/position-list pairs for the
145312** contents of the document with docid iDocid.
145313*/
145314static int fts3PendingTermsDocid(
145315  Fts3Table *p,                   /* Full-text table handle */
145316  int iLangid,                    /* Language id of row being written */
145317  sqlite_int64 iDocid             /* Docid of row being written */
145318){
145319  assert( iLangid>=0 );
145320
145321  /* TODO(shess) Explore whether partially flushing the buffer on
145322  ** forced-flush would provide better performance.  I suspect that if
145323  ** we ordered the doclists by size and flushed the largest until the
145324  ** buffer was half empty, that would let the less frequent terms
145325  ** generate longer doclists.
145326  */
145327  if( iDocid<=p->iPrevDocid
145328   || p->iPrevLangid!=iLangid
145329   || p->nPendingData>p->nMaxPendingData
145330  ){
145331    int rc = sqlite3Fts3PendingTermsFlush(p);
145332    if( rc!=SQLITE_OK ) return rc;
145333  }
145334  p->iPrevDocid = iDocid;
145335  p->iPrevLangid = iLangid;
145336  return SQLITE_OK;
145337}
145338
145339/*
145340** Discard the contents of the pending-terms hash tables.
145341*/
145342SQLITE_PRIVATE void sqlite3Fts3PendingTermsClear(Fts3Table *p){
145343  int i;
145344  for(i=0; i<p->nIndex; i++){
145345    Fts3HashElem *pElem;
145346    Fts3Hash *pHash = &p->aIndex[i].hPending;
145347    for(pElem=fts3HashFirst(pHash); pElem; pElem=fts3HashNext(pElem)){
145348      PendingList *pList = (PendingList *)fts3HashData(pElem);
145349      fts3PendingListDelete(pList);
145350    }
145351    fts3HashClear(pHash);
145352  }
145353  p->nPendingData = 0;
145354}
145355
145356/*
145357** This function is called by the xUpdate() method as part of an INSERT
145358** operation. It adds entries for each term in the new record to the
145359** pendingTerms hash table.
145360**
145361** Argument apVal is the same as the similarly named argument passed to
145362** fts3InsertData(). Parameter iDocid is the docid of the new row.
145363*/
145364static int fts3InsertTerms(
145365  Fts3Table *p,
145366  int iLangid,
145367  sqlite3_value **apVal,
145368  u32 *aSz
145369){
145370  int i;                          /* Iterator variable */
145371  for(i=2; i<p->nColumn+2; i++){
145372    int iCol = i-2;
145373    if( p->abNotindexed[iCol]==0 ){
145374      const char *zText = (const char *)sqlite3_value_text(apVal[i]);
145375      int rc = fts3PendingTermsAdd(p, iLangid, zText, iCol, &aSz[iCol]);
145376      if( rc!=SQLITE_OK ){
145377        return rc;
145378      }
145379      aSz[p->nColumn] += sqlite3_value_bytes(apVal[i]);
145380    }
145381  }
145382  return SQLITE_OK;
145383}
145384
145385/*
145386** This function is called by the xUpdate() method for an INSERT operation.
145387** The apVal parameter is passed a copy of the apVal argument passed by
145388** SQLite to the xUpdate() method. i.e:
145389**
145390**   apVal[0]                Not used for INSERT.
145391**   apVal[1]                rowid
145392**   apVal[2]                Left-most user-defined column
145393**   ...
145394**   apVal[p->nColumn+1]     Right-most user-defined column
145395**   apVal[p->nColumn+2]     Hidden column with same name as table
145396**   apVal[p->nColumn+3]     Hidden "docid" column (alias for rowid)
145397**   apVal[p->nColumn+4]     Hidden languageid column
145398*/
145399static int fts3InsertData(
145400  Fts3Table *p,                   /* Full-text table */
145401  sqlite3_value **apVal,          /* Array of values to insert */
145402  sqlite3_int64 *piDocid          /* OUT: Docid for row just inserted */
145403){
145404  int rc;                         /* Return code */
145405  sqlite3_stmt *pContentInsert;   /* INSERT INTO %_content VALUES(...) */
145406
145407  if( p->zContentTbl ){
145408    sqlite3_value *pRowid = apVal[p->nColumn+3];
145409    if( sqlite3_value_type(pRowid)==SQLITE_NULL ){
145410      pRowid = apVal[1];
145411    }
145412    if( sqlite3_value_type(pRowid)!=SQLITE_INTEGER ){
145413      return SQLITE_CONSTRAINT;
145414    }
145415    *piDocid = sqlite3_value_int64(pRowid);
145416    return SQLITE_OK;
145417  }
145418
145419  /* Locate the statement handle used to insert data into the %_content
145420  ** table. The SQL for this statement is:
145421  **
145422  **   INSERT INTO %_content VALUES(?, ?, ?, ...)
145423  **
145424  ** The statement features N '?' variables, where N is the number of user
145425  ** defined columns in the FTS3 table, plus one for the docid field.
145426  */
145427  rc = fts3SqlStmt(p, SQL_CONTENT_INSERT, &pContentInsert, &apVal[1]);
145428  if( rc==SQLITE_OK && p->zLanguageid ){
145429    rc = sqlite3_bind_int(
145430        pContentInsert, p->nColumn+2,
145431        sqlite3_value_int(apVal[p->nColumn+4])
145432    );
145433  }
145434  if( rc!=SQLITE_OK ) return rc;
145435
145436  /* There is a quirk here. The users INSERT statement may have specified
145437  ** a value for the "rowid" field, for the "docid" field, or for both.
145438  ** Which is a problem, since "rowid" and "docid" are aliases for the
145439  ** same value. For example:
145440  **
145441  **   INSERT INTO fts3tbl(rowid, docid) VALUES(1, 2);
145442  **
145443  ** In FTS3, this is an error. It is an error to specify non-NULL values
145444  ** for both docid and some other rowid alias.
145445  */
145446  if( SQLITE_NULL!=sqlite3_value_type(apVal[3+p->nColumn]) ){
145447    if( SQLITE_NULL==sqlite3_value_type(apVal[0])
145448     && SQLITE_NULL!=sqlite3_value_type(apVal[1])
145449    ){
145450      /* A rowid/docid conflict. */
145451      return SQLITE_ERROR;
145452    }
145453    rc = sqlite3_bind_value(pContentInsert, 1, apVal[3+p->nColumn]);
145454    if( rc!=SQLITE_OK ) return rc;
145455  }
145456
145457  /* Execute the statement to insert the record. Set *piDocid to the
145458  ** new docid value.
145459  */
145460  sqlite3_step(pContentInsert);
145461  rc = sqlite3_reset(pContentInsert);
145462
145463  *piDocid = sqlite3_last_insert_rowid(p->db);
145464  return rc;
145465}
145466
145467
145468
145469/*
145470** Remove all data from the FTS3 table. Clear the hash table containing
145471** pending terms.
145472*/
145473static int fts3DeleteAll(Fts3Table *p, int bContent){
145474  int rc = SQLITE_OK;             /* Return code */
145475
145476  /* Discard the contents of the pending-terms hash table. */
145477  sqlite3Fts3PendingTermsClear(p);
145478
145479  /* Delete everything from the shadow tables. Except, leave %_content as
145480  ** is if bContent is false.  */
145481  assert( p->zContentTbl==0 || bContent==0 );
145482  if( bContent ) fts3SqlExec(&rc, p, SQL_DELETE_ALL_CONTENT, 0);
145483  fts3SqlExec(&rc, p, SQL_DELETE_ALL_SEGMENTS, 0);
145484  fts3SqlExec(&rc, p, SQL_DELETE_ALL_SEGDIR, 0);
145485  if( p->bHasDocsize ){
145486    fts3SqlExec(&rc, p, SQL_DELETE_ALL_DOCSIZE, 0);
145487  }
145488  if( p->bHasStat ){
145489    fts3SqlExec(&rc, p, SQL_DELETE_ALL_STAT, 0);
145490  }
145491  return rc;
145492}
145493
145494/*
145495**
145496*/
145497static int langidFromSelect(Fts3Table *p, sqlite3_stmt *pSelect){
145498  int iLangid = 0;
145499  if( p->zLanguageid ) iLangid = sqlite3_column_int(pSelect, p->nColumn+1);
145500  return iLangid;
145501}
145502
145503/*
145504** The first element in the apVal[] array is assumed to contain the docid
145505** (an integer) of a row about to be deleted. Remove all terms from the
145506** full-text index.
145507*/
145508static void fts3DeleteTerms(
145509  int *pRC,               /* Result code */
145510  Fts3Table *p,           /* The FTS table to delete from */
145511  sqlite3_value *pRowid,  /* The docid to be deleted */
145512  u32 *aSz,               /* Sizes of deleted document written here */
145513  int *pbFound            /* OUT: Set to true if row really does exist */
145514){
145515  int rc;
145516  sqlite3_stmt *pSelect;
145517
145518  assert( *pbFound==0 );
145519  if( *pRC ) return;
145520  rc = fts3SqlStmt(p, SQL_SELECT_CONTENT_BY_ROWID, &pSelect, &pRowid);
145521  if( rc==SQLITE_OK ){
145522    if( SQLITE_ROW==sqlite3_step(pSelect) ){
145523      int i;
145524      int iLangid = langidFromSelect(p, pSelect);
145525      rc = fts3PendingTermsDocid(p, iLangid, sqlite3_column_int64(pSelect, 0));
145526      for(i=1; rc==SQLITE_OK && i<=p->nColumn; i++){
145527        int iCol = i-1;
145528        if( p->abNotindexed[iCol]==0 ){
145529          const char *zText = (const char *)sqlite3_column_text(pSelect, i);
145530          rc = fts3PendingTermsAdd(p, iLangid, zText, -1, &aSz[iCol]);
145531          aSz[p->nColumn] += sqlite3_column_bytes(pSelect, i);
145532        }
145533      }
145534      if( rc!=SQLITE_OK ){
145535        sqlite3_reset(pSelect);
145536        *pRC = rc;
145537        return;
145538      }
145539      *pbFound = 1;
145540    }
145541    rc = sqlite3_reset(pSelect);
145542  }else{
145543    sqlite3_reset(pSelect);
145544  }
145545  *pRC = rc;
145546}
145547
145548/*
145549** Forward declaration to account for the circular dependency between
145550** functions fts3SegmentMerge() and fts3AllocateSegdirIdx().
145551*/
145552static int fts3SegmentMerge(Fts3Table *, int, int, int);
145553
145554/*
145555** This function allocates a new level iLevel index in the segdir table.
145556** Usually, indexes are allocated within a level sequentially starting
145557** with 0, so the allocated index is one greater than the value returned
145558** by:
145559**
145560**   SELECT max(idx) FROM %_segdir WHERE level = :iLevel
145561**
145562** However, if there are already FTS3_MERGE_COUNT indexes at the requested
145563** level, they are merged into a single level (iLevel+1) segment and the
145564** allocated index is 0.
145565**
145566** If successful, *piIdx is set to the allocated index slot and SQLITE_OK
145567** returned. Otherwise, an SQLite error code is returned.
145568*/
145569static int fts3AllocateSegdirIdx(
145570  Fts3Table *p,
145571  int iLangid,                    /* Language id */
145572  int iIndex,                     /* Index for p->aIndex */
145573  int iLevel,
145574  int *piIdx
145575){
145576  int rc;                         /* Return Code */
145577  sqlite3_stmt *pNextIdx;         /* Query for next idx at level iLevel */
145578  int iNext = 0;                  /* Result of query pNextIdx */
145579
145580  assert( iLangid>=0 );
145581  assert( p->nIndex>=1 );
145582
145583  /* Set variable iNext to the next available segdir index at level iLevel. */
145584  rc = fts3SqlStmt(p, SQL_NEXT_SEGMENT_INDEX, &pNextIdx, 0);
145585  if( rc==SQLITE_OK ){
145586    sqlite3_bind_int64(
145587        pNextIdx, 1, getAbsoluteLevel(p, iLangid, iIndex, iLevel)
145588    );
145589    if( SQLITE_ROW==sqlite3_step(pNextIdx) ){
145590      iNext = sqlite3_column_int(pNextIdx, 0);
145591    }
145592    rc = sqlite3_reset(pNextIdx);
145593  }
145594
145595  if( rc==SQLITE_OK ){
145596    /* If iNext is FTS3_MERGE_COUNT, indicating that level iLevel is already
145597    ** full, merge all segments in level iLevel into a single iLevel+1
145598    ** segment and allocate (newly freed) index 0 at level iLevel. Otherwise,
145599    ** if iNext is less than FTS3_MERGE_COUNT, allocate index iNext.
145600    */
145601    if( iNext>=FTS3_MERGE_COUNT ){
145602      fts3LogMerge(16, getAbsoluteLevel(p, iLangid, iIndex, iLevel));
145603      rc = fts3SegmentMerge(p, iLangid, iIndex, iLevel);
145604      *piIdx = 0;
145605    }else{
145606      *piIdx = iNext;
145607    }
145608  }
145609
145610  return rc;
145611}
145612
145613/*
145614** The %_segments table is declared as follows:
145615**
145616**   CREATE TABLE %_segments(blockid INTEGER PRIMARY KEY, block BLOB)
145617**
145618** This function reads data from a single row of the %_segments table. The
145619** specific row is identified by the iBlockid parameter. If paBlob is not
145620** NULL, then a buffer is allocated using sqlite3_malloc() and populated
145621** with the contents of the blob stored in the "block" column of the
145622** identified table row is. Whether or not paBlob is NULL, *pnBlob is set
145623** to the size of the blob in bytes before returning.
145624**
145625** If an error occurs, or the table does not contain the specified row,
145626** an SQLite error code is returned. Otherwise, SQLITE_OK is returned. If
145627** paBlob is non-NULL, then it is the responsibility of the caller to
145628** eventually free the returned buffer.
145629**
145630** This function may leave an open sqlite3_blob* handle in the
145631** Fts3Table.pSegments variable. This handle is reused by subsequent calls
145632** to this function. The handle may be closed by calling the
145633** sqlite3Fts3SegmentsClose() function. Reusing a blob handle is a handy
145634** performance improvement, but the blob handle should always be closed
145635** before control is returned to the user (to prevent a lock being held
145636** on the database file for longer than necessary). Thus, any virtual table
145637** method (xFilter etc.) that may directly or indirectly call this function
145638** must call sqlite3Fts3SegmentsClose() before returning.
145639*/
145640SQLITE_PRIVATE int sqlite3Fts3ReadBlock(
145641  Fts3Table *p,                   /* FTS3 table handle */
145642  sqlite3_int64 iBlockid,         /* Access the row with blockid=$iBlockid */
145643  char **paBlob,                  /* OUT: Blob data in malloc'd buffer */
145644  int *pnBlob,                    /* OUT: Size of blob data */
145645  int *pnLoad                     /* OUT: Bytes actually loaded */
145646){
145647  int rc;                         /* Return code */
145648
145649  /* pnBlob must be non-NULL. paBlob may be NULL or non-NULL. */
145650  assert( pnBlob );
145651
145652  if( p->pSegments ){
145653    rc = sqlite3_blob_reopen(p->pSegments, iBlockid);
145654  }else{
145655    if( 0==p->zSegmentsTbl ){
145656      p->zSegmentsTbl = sqlite3_mprintf("%s_segments", p->zName);
145657      if( 0==p->zSegmentsTbl ) return SQLITE_NOMEM;
145658    }
145659    rc = sqlite3_blob_open(
145660       p->db, p->zDb, p->zSegmentsTbl, "block", iBlockid, 0, &p->pSegments
145661    );
145662  }
145663
145664  if( rc==SQLITE_OK ){
145665    int nByte = sqlite3_blob_bytes(p->pSegments);
145666    *pnBlob = nByte;
145667    if( paBlob ){
145668      char *aByte = sqlite3_malloc(nByte + FTS3_NODE_PADDING);
145669      if( !aByte ){
145670        rc = SQLITE_NOMEM;
145671      }else{
145672        if( pnLoad && nByte>(FTS3_NODE_CHUNK_THRESHOLD) ){
145673          nByte = FTS3_NODE_CHUNKSIZE;
145674          *pnLoad = nByte;
145675        }
145676        rc = sqlite3_blob_read(p->pSegments, aByte, nByte, 0);
145677        memset(&aByte[nByte], 0, FTS3_NODE_PADDING);
145678        if( rc!=SQLITE_OK ){
145679          sqlite3_free(aByte);
145680          aByte = 0;
145681        }
145682      }
145683      *paBlob = aByte;
145684    }
145685  }
145686
145687  return rc;
145688}
145689
145690/*
145691** Close the blob handle at p->pSegments, if it is open. See comments above
145692** the sqlite3Fts3ReadBlock() function for details.
145693*/
145694SQLITE_PRIVATE void sqlite3Fts3SegmentsClose(Fts3Table *p){
145695  sqlite3_blob_close(p->pSegments);
145696  p->pSegments = 0;
145697}
145698
145699static int fts3SegReaderIncrRead(Fts3SegReader *pReader){
145700  int nRead;                      /* Number of bytes to read */
145701  int rc;                         /* Return code */
145702
145703  nRead = MIN(pReader->nNode - pReader->nPopulate, FTS3_NODE_CHUNKSIZE);
145704  rc = sqlite3_blob_read(
145705      pReader->pBlob,
145706      &pReader->aNode[pReader->nPopulate],
145707      nRead,
145708      pReader->nPopulate
145709  );
145710
145711  if( rc==SQLITE_OK ){
145712    pReader->nPopulate += nRead;
145713    memset(&pReader->aNode[pReader->nPopulate], 0, FTS3_NODE_PADDING);
145714    if( pReader->nPopulate==pReader->nNode ){
145715      sqlite3_blob_close(pReader->pBlob);
145716      pReader->pBlob = 0;
145717      pReader->nPopulate = 0;
145718    }
145719  }
145720  return rc;
145721}
145722
145723static int fts3SegReaderRequire(Fts3SegReader *pReader, char *pFrom, int nByte){
145724  int rc = SQLITE_OK;
145725  assert( !pReader->pBlob
145726       || (pFrom>=pReader->aNode && pFrom<&pReader->aNode[pReader->nNode])
145727  );
145728  while( pReader->pBlob && rc==SQLITE_OK
145729     &&  (pFrom - pReader->aNode + nByte)>pReader->nPopulate
145730  ){
145731    rc = fts3SegReaderIncrRead(pReader);
145732  }
145733  return rc;
145734}
145735
145736/*
145737** Set an Fts3SegReader cursor to point at EOF.
145738*/
145739static void fts3SegReaderSetEof(Fts3SegReader *pSeg){
145740  if( !fts3SegReaderIsRootOnly(pSeg) ){
145741    sqlite3_free(pSeg->aNode);
145742    sqlite3_blob_close(pSeg->pBlob);
145743    pSeg->pBlob = 0;
145744  }
145745  pSeg->aNode = 0;
145746}
145747
145748/*
145749** Move the iterator passed as the first argument to the next term in the
145750** segment. If successful, SQLITE_OK is returned. If there is no next term,
145751** SQLITE_DONE. Otherwise, an SQLite error code.
145752*/
145753static int fts3SegReaderNext(
145754  Fts3Table *p,
145755  Fts3SegReader *pReader,
145756  int bIncr
145757){
145758  int rc;                         /* Return code of various sub-routines */
145759  char *pNext;                    /* Cursor variable */
145760  int nPrefix;                    /* Number of bytes in term prefix */
145761  int nSuffix;                    /* Number of bytes in term suffix */
145762
145763  if( !pReader->aDoclist ){
145764    pNext = pReader->aNode;
145765  }else{
145766    pNext = &pReader->aDoclist[pReader->nDoclist];
145767  }
145768
145769  if( !pNext || pNext>=&pReader->aNode[pReader->nNode] ){
145770
145771    if( fts3SegReaderIsPending(pReader) ){
145772      Fts3HashElem *pElem = *(pReader->ppNextElem);
145773      if( pElem==0 ){
145774        pReader->aNode = 0;
145775      }else{
145776        PendingList *pList = (PendingList *)fts3HashData(pElem);
145777        pReader->zTerm = (char *)fts3HashKey(pElem);
145778        pReader->nTerm = fts3HashKeysize(pElem);
145779        pReader->nNode = pReader->nDoclist = pList->nData + 1;
145780        pReader->aNode = pReader->aDoclist = pList->aData;
145781        pReader->ppNextElem++;
145782        assert( pReader->aNode );
145783      }
145784      return SQLITE_OK;
145785    }
145786
145787    fts3SegReaderSetEof(pReader);
145788
145789    /* If iCurrentBlock>=iLeafEndBlock, this is an EOF condition. All leaf
145790    ** blocks have already been traversed.  */
145791    assert( pReader->iCurrentBlock<=pReader->iLeafEndBlock );
145792    if( pReader->iCurrentBlock>=pReader->iLeafEndBlock ){
145793      return SQLITE_OK;
145794    }
145795
145796    rc = sqlite3Fts3ReadBlock(
145797        p, ++pReader->iCurrentBlock, &pReader->aNode, &pReader->nNode,
145798        (bIncr ? &pReader->nPopulate : 0)
145799    );
145800    if( rc!=SQLITE_OK ) return rc;
145801    assert( pReader->pBlob==0 );
145802    if( bIncr && pReader->nPopulate<pReader->nNode ){
145803      pReader->pBlob = p->pSegments;
145804      p->pSegments = 0;
145805    }
145806    pNext = pReader->aNode;
145807  }
145808
145809  assert( !fts3SegReaderIsPending(pReader) );
145810
145811  rc = fts3SegReaderRequire(pReader, pNext, FTS3_VARINT_MAX*2);
145812  if( rc!=SQLITE_OK ) return rc;
145813
145814  /* Because of the FTS3_NODE_PADDING bytes of padding, the following is
145815  ** safe (no risk of overread) even if the node data is corrupted. */
145816  pNext += fts3GetVarint32(pNext, &nPrefix);
145817  pNext += fts3GetVarint32(pNext, &nSuffix);
145818  if( nPrefix<0 || nSuffix<=0
145819   || &pNext[nSuffix]>&pReader->aNode[pReader->nNode]
145820  ){
145821    return FTS_CORRUPT_VTAB;
145822  }
145823
145824  if( nPrefix+nSuffix>pReader->nTermAlloc ){
145825    int nNew = (nPrefix+nSuffix)*2;
145826    char *zNew = sqlite3_realloc(pReader->zTerm, nNew);
145827    if( !zNew ){
145828      return SQLITE_NOMEM;
145829    }
145830    pReader->zTerm = zNew;
145831    pReader->nTermAlloc = nNew;
145832  }
145833
145834  rc = fts3SegReaderRequire(pReader, pNext, nSuffix+FTS3_VARINT_MAX);
145835  if( rc!=SQLITE_OK ) return rc;
145836
145837  memcpy(&pReader->zTerm[nPrefix], pNext, nSuffix);
145838  pReader->nTerm = nPrefix+nSuffix;
145839  pNext += nSuffix;
145840  pNext += fts3GetVarint32(pNext, &pReader->nDoclist);
145841  pReader->aDoclist = pNext;
145842  pReader->pOffsetList = 0;
145843
145844  /* Check that the doclist does not appear to extend past the end of the
145845  ** b-tree node. And that the final byte of the doclist is 0x00. If either
145846  ** of these statements is untrue, then the data structure is corrupt.
145847  */
145848  if( &pReader->aDoclist[pReader->nDoclist]>&pReader->aNode[pReader->nNode]
145849   || (pReader->nPopulate==0 && pReader->aDoclist[pReader->nDoclist-1])
145850  ){
145851    return FTS_CORRUPT_VTAB;
145852  }
145853  return SQLITE_OK;
145854}
145855
145856/*
145857** Set the SegReader to point to the first docid in the doclist associated
145858** with the current term.
145859*/
145860static int fts3SegReaderFirstDocid(Fts3Table *pTab, Fts3SegReader *pReader){
145861  int rc = SQLITE_OK;
145862  assert( pReader->aDoclist );
145863  assert( !pReader->pOffsetList );
145864  if( pTab->bDescIdx && fts3SegReaderIsPending(pReader) ){
145865    u8 bEof = 0;
145866    pReader->iDocid = 0;
145867    pReader->nOffsetList = 0;
145868    sqlite3Fts3DoclistPrev(0,
145869        pReader->aDoclist, pReader->nDoclist, &pReader->pOffsetList,
145870        &pReader->iDocid, &pReader->nOffsetList, &bEof
145871    );
145872  }else{
145873    rc = fts3SegReaderRequire(pReader, pReader->aDoclist, FTS3_VARINT_MAX);
145874    if( rc==SQLITE_OK ){
145875      int n = sqlite3Fts3GetVarint(pReader->aDoclist, &pReader->iDocid);
145876      pReader->pOffsetList = &pReader->aDoclist[n];
145877    }
145878  }
145879  return rc;
145880}
145881
145882/*
145883** Advance the SegReader to point to the next docid in the doclist
145884** associated with the current term.
145885**
145886** If arguments ppOffsetList and pnOffsetList are not NULL, then
145887** *ppOffsetList is set to point to the first column-offset list
145888** in the doclist entry (i.e. immediately past the docid varint).
145889** *pnOffsetList is set to the length of the set of column-offset
145890** lists, not including the nul-terminator byte. For example:
145891*/
145892static int fts3SegReaderNextDocid(
145893  Fts3Table *pTab,
145894  Fts3SegReader *pReader,         /* Reader to advance to next docid */
145895  char **ppOffsetList,            /* OUT: Pointer to current position-list */
145896  int *pnOffsetList               /* OUT: Length of *ppOffsetList in bytes */
145897){
145898  int rc = SQLITE_OK;
145899  char *p = pReader->pOffsetList;
145900  char c = 0;
145901
145902  assert( p );
145903
145904  if( pTab->bDescIdx && fts3SegReaderIsPending(pReader) ){
145905    /* A pending-terms seg-reader for an FTS4 table that uses order=desc.
145906    ** Pending-terms doclists are always built up in ascending order, so
145907    ** we have to iterate through them backwards here. */
145908    u8 bEof = 0;
145909    if( ppOffsetList ){
145910      *ppOffsetList = pReader->pOffsetList;
145911      *pnOffsetList = pReader->nOffsetList - 1;
145912    }
145913    sqlite3Fts3DoclistPrev(0,
145914        pReader->aDoclist, pReader->nDoclist, &p, &pReader->iDocid,
145915        &pReader->nOffsetList, &bEof
145916    );
145917    if( bEof ){
145918      pReader->pOffsetList = 0;
145919    }else{
145920      pReader->pOffsetList = p;
145921    }
145922  }else{
145923    char *pEnd = &pReader->aDoclist[pReader->nDoclist];
145924
145925    /* Pointer p currently points at the first byte of an offset list. The
145926    ** following block advances it to point one byte past the end of
145927    ** the same offset list. */
145928    while( 1 ){
145929
145930      /* The following line of code (and the "p++" below the while() loop) is
145931      ** normally all that is required to move pointer p to the desired
145932      ** position. The exception is if this node is being loaded from disk
145933      ** incrementally and pointer "p" now points to the first byte past
145934      ** the populated part of pReader->aNode[].
145935      */
145936      while( *p | c ) c = *p++ & 0x80;
145937      assert( *p==0 );
145938
145939      if( pReader->pBlob==0 || p<&pReader->aNode[pReader->nPopulate] ) break;
145940      rc = fts3SegReaderIncrRead(pReader);
145941      if( rc!=SQLITE_OK ) return rc;
145942    }
145943    p++;
145944
145945    /* If required, populate the output variables with a pointer to and the
145946    ** size of the previous offset-list.
145947    */
145948    if( ppOffsetList ){
145949      *ppOffsetList = pReader->pOffsetList;
145950      *pnOffsetList = (int)(p - pReader->pOffsetList - 1);
145951    }
145952
145953    /* List may have been edited in place by fts3EvalNearTrim() */
145954    while( p<pEnd && *p==0 ) p++;
145955
145956    /* If there are no more entries in the doclist, set pOffsetList to
145957    ** NULL. Otherwise, set Fts3SegReader.iDocid to the next docid and
145958    ** Fts3SegReader.pOffsetList to point to the next offset list before
145959    ** returning.
145960    */
145961    if( p>=pEnd ){
145962      pReader->pOffsetList = 0;
145963    }else{
145964      rc = fts3SegReaderRequire(pReader, p, FTS3_VARINT_MAX);
145965      if( rc==SQLITE_OK ){
145966        sqlite3_int64 iDelta;
145967        pReader->pOffsetList = p + sqlite3Fts3GetVarint(p, &iDelta);
145968        if( pTab->bDescIdx ){
145969          pReader->iDocid -= iDelta;
145970        }else{
145971          pReader->iDocid += iDelta;
145972        }
145973      }
145974    }
145975  }
145976
145977  return SQLITE_OK;
145978}
145979
145980
145981SQLITE_PRIVATE int sqlite3Fts3MsrOvfl(
145982  Fts3Cursor *pCsr,
145983  Fts3MultiSegReader *pMsr,
145984  int *pnOvfl
145985){
145986  Fts3Table *p = (Fts3Table*)pCsr->base.pVtab;
145987  int nOvfl = 0;
145988  int ii;
145989  int rc = SQLITE_OK;
145990  int pgsz = p->nPgsz;
145991
145992  assert( p->bFts4 );
145993  assert( pgsz>0 );
145994
145995  for(ii=0; rc==SQLITE_OK && ii<pMsr->nSegment; ii++){
145996    Fts3SegReader *pReader = pMsr->apSegment[ii];
145997    if( !fts3SegReaderIsPending(pReader)
145998     && !fts3SegReaderIsRootOnly(pReader)
145999    ){
146000      sqlite3_int64 jj;
146001      for(jj=pReader->iStartBlock; jj<=pReader->iLeafEndBlock; jj++){
146002        int nBlob;
146003        rc = sqlite3Fts3ReadBlock(p, jj, 0, &nBlob, 0);
146004        if( rc!=SQLITE_OK ) break;
146005        if( (nBlob+35)>pgsz ){
146006          nOvfl += (nBlob + 34)/pgsz;
146007        }
146008      }
146009    }
146010  }
146011  *pnOvfl = nOvfl;
146012  return rc;
146013}
146014
146015/*
146016** Free all allocations associated with the iterator passed as the
146017** second argument.
146018*/
146019SQLITE_PRIVATE void sqlite3Fts3SegReaderFree(Fts3SegReader *pReader){
146020  if( pReader && !fts3SegReaderIsPending(pReader) ){
146021    sqlite3_free(pReader->zTerm);
146022    if( !fts3SegReaderIsRootOnly(pReader) ){
146023      sqlite3_free(pReader->aNode);
146024      sqlite3_blob_close(pReader->pBlob);
146025    }
146026  }
146027  sqlite3_free(pReader);
146028}
146029
146030/*
146031** Allocate a new SegReader object.
146032*/
146033SQLITE_PRIVATE int sqlite3Fts3SegReaderNew(
146034  int iAge,                       /* Segment "age". */
146035  int bLookup,                    /* True for a lookup only */
146036  sqlite3_int64 iStartLeaf,       /* First leaf to traverse */
146037  sqlite3_int64 iEndLeaf,         /* Final leaf to traverse */
146038  sqlite3_int64 iEndBlock,        /* Final block of segment */
146039  const char *zRoot,              /* Buffer containing root node */
146040  int nRoot,                      /* Size of buffer containing root node */
146041  Fts3SegReader **ppReader        /* OUT: Allocated Fts3SegReader */
146042){
146043  Fts3SegReader *pReader;         /* Newly allocated SegReader object */
146044  int nExtra = 0;                 /* Bytes to allocate segment root node */
146045
146046  assert( iStartLeaf<=iEndLeaf );
146047  if( iStartLeaf==0 ){
146048    nExtra = nRoot + FTS3_NODE_PADDING;
146049  }
146050
146051  pReader = (Fts3SegReader *)sqlite3_malloc(sizeof(Fts3SegReader) + nExtra);
146052  if( !pReader ){
146053    return SQLITE_NOMEM;
146054  }
146055  memset(pReader, 0, sizeof(Fts3SegReader));
146056  pReader->iIdx = iAge;
146057  pReader->bLookup = bLookup!=0;
146058  pReader->iStartBlock = iStartLeaf;
146059  pReader->iLeafEndBlock = iEndLeaf;
146060  pReader->iEndBlock = iEndBlock;
146061
146062  if( nExtra ){
146063    /* The entire segment is stored in the root node. */
146064    pReader->aNode = (char *)&pReader[1];
146065    pReader->rootOnly = 1;
146066    pReader->nNode = nRoot;
146067    memcpy(pReader->aNode, zRoot, nRoot);
146068    memset(&pReader->aNode[nRoot], 0, FTS3_NODE_PADDING);
146069  }else{
146070    pReader->iCurrentBlock = iStartLeaf-1;
146071  }
146072  *ppReader = pReader;
146073  return SQLITE_OK;
146074}
146075
146076/*
146077** This is a comparison function used as a qsort() callback when sorting
146078** an array of pending terms by term. This occurs as part of flushing
146079** the contents of the pending-terms hash table to the database.
146080*/
146081static int SQLITE_CDECL fts3CompareElemByTerm(
146082  const void *lhs,
146083  const void *rhs
146084){
146085  char *z1 = fts3HashKey(*(Fts3HashElem **)lhs);
146086  char *z2 = fts3HashKey(*(Fts3HashElem **)rhs);
146087  int n1 = fts3HashKeysize(*(Fts3HashElem **)lhs);
146088  int n2 = fts3HashKeysize(*(Fts3HashElem **)rhs);
146089
146090  int n = (n1<n2 ? n1 : n2);
146091  int c = memcmp(z1, z2, n);
146092  if( c==0 ){
146093    c = n1 - n2;
146094  }
146095  return c;
146096}
146097
146098/*
146099** This function is used to allocate an Fts3SegReader that iterates through
146100** a subset of the terms stored in the Fts3Table.pendingTerms array.
146101**
146102** If the isPrefixIter parameter is zero, then the returned SegReader iterates
146103** through each term in the pending-terms table. Or, if isPrefixIter is
146104** non-zero, it iterates through each term and its prefixes. For example, if
146105** the pending terms hash table contains the terms "sqlite", "mysql" and
146106** "firebird", then the iterator visits the following 'terms' (in the order
146107** shown):
146108**
146109**   f fi fir fire fireb firebi firebir firebird
146110**   m my mys mysq mysql
146111**   s sq sql sqli sqlit sqlite
146112**
146113** Whereas if isPrefixIter is zero, the terms visited are:
146114**
146115**   firebird mysql sqlite
146116*/
146117SQLITE_PRIVATE int sqlite3Fts3SegReaderPending(
146118  Fts3Table *p,                   /* Virtual table handle */
146119  int iIndex,                     /* Index for p->aIndex */
146120  const char *zTerm,              /* Term to search for */
146121  int nTerm,                      /* Size of buffer zTerm */
146122  int bPrefix,                    /* True for a prefix iterator */
146123  Fts3SegReader **ppReader        /* OUT: SegReader for pending-terms */
146124){
146125  Fts3SegReader *pReader = 0;     /* Fts3SegReader object to return */
146126  Fts3HashElem *pE;               /* Iterator variable */
146127  Fts3HashElem **aElem = 0;       /* Array of term hash entries to scan */
146128  int nElem = 0;                  /* Size of array at aElem */
146129  int rc = SQLITE_OK;             /* Return Code */
146130  Fts3Hash *pHash;
146131
146132  pHash = &p->aIndex[iIndex].hPending;
146133  if( bPrefix ){
146134    int nAlloc = 0;               /* Size of allocated array at aElem */
146135
146136    for(pE=fts3HashFirst(pHash); pE; pE=fts3HashNext(pE)){
146137      char *zKey = (char *)fts3HashKey(pE);
146138      int nKey = fts3HashKeysize(pE);
146139      if( nTerm==0 || (nKey>=nTerm && 0==memcmp(zKey, zTerm, nTerm)) ){
146140        if( nElem==nAlloc ){
146141          Fts3HashElem **aElem2;
146142          nAlloc += 16;
146143          aElem2 = (Fts3HashElem **)sqlite3_realloc(
146144              aElem, nAlloc*sizeof(Fts3HashElem *)
146145          );
146146          if( !aElem2 ){
146147            rc = SQLITE_NOMEM;
146148            nElem = 0;
146149            break;
146150          }
146151          aElem = aElem2;
146152        }
146153
146154        aElem[nElem++] = pE;
146155      }
146156    }
146157
146158    /* If more than one term matches the prefix, sort the Fts3HashElem
146159    ** objects in term order using qsort(). This uses the same comparison
146160    ** callback as is used when flushing terms to disk.
146161    */
146162    if( nElem>1 ){
146163      qsort(aElem, nElem, sizeof(Fts3HashElem *), fts3CompareElemByTerm);
146164    }
146165
146166  }else{
146167    /* The query is a simple term lookup that matches at most one term in
146168    ** the index. All that is required is a straight hash-lookup.
146169    **
146170    ** Because the stack address of pE may be accessed via the aElem pointer
146171    ** below, the "Fts3HashElem *pE" must be declared so that it is valid
146172    ** within this entire function, not just this "else{...}" block.
146173    */
146174    pE = fts3HashFindElem(pHash, zTerm, nTerm);
146175    if( pE ){
146176      aElem = &pE;
146177      nElem = 1;
146178    }
146179  }
146180
146181  if( nElem>0 ){
146182    int nByte = sizeof(Fts3SegReader) + (nElem+1)*sizeof(Fts3HashElem *);
146183    pReader = (Fts3SegReader *)sqlite3_malloc(nByte);
146184    if( !pReader ){
146185      rc = SQLITE_NOMEM;
146186    }else{
146187      memset(pReader, 0, nByte);
146188      pReader->iIdx = 0x7FFFFFFF;
146189      pReader->ppNextElem = (Fts3HashElem **)&pReader[1];
146190      memcpy(pReader->ppNextElem, aElem, nElem*sizeof(Fts3HashElem *));
146191    }
146192  }
146193
146194  if( bPrefix ){
146195    sqlite3_free(aElem);
146196  }
146197  *ppReader = pReader;
146198  return rc;
146199}
146200
146201/*
146202** Compare the entries pointed to by two Fts3SegReader structures.
146203** Comparison is as follows:
146204**
146205**   1) EOF is greater than not EOF.
146206**
146207**   2) The current terms (if any) are compared using memcmp(). If one
146208**      term is a prefix of another, the longer term is considered the
146209**      larger.
146210**
146211**   3) By segment age. An older segment is considered larger.
146212*/
146213static int fts3SegReaderCmp(Fts3SegReader *pLhs, Fts3SegReader *pRhs){
146214  int rc;
146215  if( pLhs->aNode && pRhs->aNode ){
146216    int rc2 = pLhs->nTerm - pRhs->nTerm;
146217    if( rc2<0 ){
146218      rc = memcmp(pLhs->zTerm, pRhs->zTerm, pLhs->nTerm);
146219    }else{
146220      rc = memcmp(pLhs->zTerm, pRhs->zTerm, pRhs->nTerm);
146221    }
146222    if( rc==0 ){
146223      rc = rc2;
146224    }
146225  }else{
146226    rc = (pLhs->aNode==0) - (pRhs->aNode==0);
146227  }
146228  if( rc==0 ){
146229    rc = pRhs->iIdx - pLhs->iIdx;
146230  }
146231  assert( rc!=0 );
146232  return rc;
146233}
146234
146235/*
146236** A different comparison function for SegReader structures. In this
146237** version, it is assumed that each SegReader points to an entry in
146238** a doclist for identical terms. Comparison is made as follows:
146239**
146240**   1) EOF (end of doclist in this case) is greater than not EOF.
146241**
146242**   2) By current docid.
146243**
146244**   3) By segment age. An older segment is considered larger.
146245*/
146246static int fts3SegReaderDoclistCmp(Fts3SegReader *pLhs, Fts3SegReader *pRhs){
146247  int rc = (pLhs->pOffsetList==0)-(pRhs->pOffsetList==0);
146248  if( rc==0 ){
146249    if( pLhs->iDocid==pRhs->iDocid ){
146250      rc = pRhs->iIdx - pLhs->iIdx;
146251    }else{
146252      rc = (pLhs->iDocid > pRhs->iDocid) ? 1 : -1;
146253    }
146254  }
146255  assert( pLhs->aNode && pRhs->aNode );
146256  return rc;
146257}
146258static int fts3SegReaderDoclistCmpRev(Fts3SegReader *pLhs, Fts3SegReader *pRhs){
146259  int rc = (pLhs->pOffsetList==0)-(pRhs->pOffsetList==0);
146260  if( rc==0 ){
146261    if( pLhs->iDocid==pRhs->iDocid ){
146262      rc = pRhs->iIdx - pLhs->iIdx;
146263    }else{
146264      rc = (pLhs->iDocid < pRhs->iDocid) ? 1 : -1;
146265    }
146266  }
146267  assert( pLhs->aNode && pRhs->aNode );
146268  return rc;
146269}
146270
146271/*
146272** Compare the term that the Fts3SegReader object passed as the first argument
146273** points to with the term specified by arguments zTerm and nTerm.
146274**
146275** If the pSeg iterator is already at EOF, return 0. Otherwise, return
146276** -ve if the pSeg term is less than zTerm/nTerm, 0 if the two terms are
146277** equal, or +ve if the pSeg term is greater than zTerm/nTerm.
146278*/
146279static int fts3SegReaderTermCmp(
146280  Fts3SegReader *pSeg,            /* Segment reader object */
146281  const char *zTerm,              /* Term to compare to */
146282  int nTerm                       /* Size of term zTerm in bytes */
146283){
146284  int res = 0;
146285  if( pSeg->aNode ){
146286    if( pSeg->nTerm>nTerm ){
146287      res = memcmp(pSeg->zTerm, zTerm, nTerm);
146288    }else{
146289      res = memcmp(pSeg->zTerm, zTerm, pSeg->nTerm);
146290    }
146291    if( res==0 ){
146292      res = pSeg->nTerm-nTerm;
146293    }
146294  }
146295  return res;
146296}
146297
146298/*
146299** Argument apSegment is an array of nSegment elements. It is known that
146300** the final (nSegment-nSuspect) members are already in sorted order
146301** (according to the comparison function provided). This function shuffles
146302** the array around until all entries are in sorted order.
146303*/
146304static void fts3SegReaderSort(
146305  Fts3SegReader **apSegment,                     /* Array to sort entries of */
146306  int nSegment,                                  /* Size of apSegment array */
146307  int nSuspect,                                  /* Unsorted entry count */
146308  int (*xCmp)(Fts3SegReader *, Fts3SegReader *)  /* Comparison function */
146309){
146310  int i;                          /* Iterator variable */
146311
146312  assert( nSuspect<=nSegment );
146313
146314  if( nSuspect==nSegment ) nSuspect--;
146315  for(i=nSuspect-1; i>=0; i--){
146316    int j;
146317    for(j=i; j<(nSegment-1); j++){
146318      Fts3SegReader *pTmp;
146319      if( xCmp(apSegment[j], apSegment[j+1])<0 ) break;
146320      pTmp = apSegment[j+1];
146321      apSegment[j+1] = apSegment[j];
146322      apSegment[j] = pTmp;
146323    }
146324  }
146325
146326#ifndef NDEBUG
146327  /* Check that the list really is sorted now. */
146328  for(i=0; i<(nSuspect-1); i++){
146329    assert( xCmp(apSegment[i], apSegment[i+1])<0 );
146330  }
146331#endif
146332}
146333
146334/*
146335** Insert a record into the %_segments table.
146336*/
146337static int fts3WriteSegment(
146338  Fts3Table *p,                   /* Virtual table handle */
146339  sqlite3_int64 iBlock,           /* Block id for new block */
146340  char *z,                        /* Pointer to buffer containing block data */
146341  int n                           /* Size of buffer z in bytes */
146342){
146343  sqlite3_stmt *pStmt;
146344  int rc = fts3SqlStmt(p, SQL_INSERT_SEGMENTS, &pStmt, 0);
146345  if( rc==SQLITE_OK ){
146346    sqlite3_bind_int64(pStmt, 1, iBlock);
146347    sqlite3_bind_blob(pStmt, 2, z, n, SQLITE_STATIC);
146348    sqlite3_step(pStmt);
146349    rc = sqlite3_reset(pStmt);
146350  }
146351  return rc;
146352}
146353
146354/*
146355** Find the largest relative level number in the table. If successful, set
146356** *pnMax to this value and return SQLITE_OK. Otherwise, if an error occurs,
146357** set *pnMax to zero and return an SQLite error code.
146358*/
146359SQLITE_PRIVATE int sqlite3Fts3MaxLevel(Fts3Table *p, int *pnMax){
146360  int rc;
146361  int mxLevel = 0;
146362  sqlite3_stmt *pStmt = 0;
146363
146364  rc = fts3SqlStmt(p, SQL_SELECT_MXLEVEL, &pStmt, 0);
146365  if( rc==SQLITE_OK ){
146366    if( SQLITE_ROW==sqlite3_step(pStmt) ){
146367      mxLevel = sqlite3_column_int(pStmt, 0);
146368    }
146369    rc = sqlite3_reset(pStmt);
146370  }
146371  *pnMax = mxLevel;
146372  return rc;
146373}
146374
146375/*
146376** Insert a record into the %_segdir table.
146377*/
146378static int fts3WriteSegdir(
146379  Fts3Table *p,                   /* Virtual table handle */
146380  sqlite3_int64 iLevel,           /* Value for "level" field (absolute level) */
146381  int iIdx,                       /* Value for "idx" field */
146382  sqlite3_int64 iStartBlock,      /* Value for "start_block" field */
146383  sqlite3_int64 iLeafEndBlock,    /* Value for "leaves_end_block" field */
146384  sqlite3_int64 iEndBlock,        /* Value for "end_block" field */
146385  sqlite3_int64 nLeafData,        /* Bytes of leaf data in segment */
146386  char *zRoot,                    /* Blob value for "root" field */
146387  int nRoot                       /* Number of bytes in buffer zRoot */
146388){
146389  sqlite3_stmt *pStmt;
146390  int rc = fts3SqlStmt(p, SQL_INSERT_SEGDIR, &pStmt, 0);
146391  if( rc==SQLITE_OK ){
146392    sqlite3_bind_int64(pStmt, 1, iLevel);
146393    sqlite3_bind_int(pStmt, 2, iIdx);
146394    sqlite3_bind_int64(pStmt, 3, iStartBlock);
146395    sqlite3_bind_int64(pStmt, 4, iLeafEndBlock);
146396    if( nLeafData==0 ){
146397      sqlite3_bind_int64(pStmt, 5, iEndBlock);
146398    }else{
146399      char *zEnd = sqlite3_mprintf("%lld %lld", iEndBlock, nLeafData);
146400      if( !zEnd ) return SQLITE_NOMEM;
146401      sqlite3_bind_text(pStmt, 5, zEnd, -1, sqlite3_free);
146402    }
146403    sqlite3_bind_blob(pStmt, 6, zRoot, nRoot, SQLITE_STATIC);
146404    sqlite3_step(pStmt);
146405    rc = sqlite3_reset(pStmt);
146406  }
146407  return rc;
146408}
146409
146410/*
146411** Return the size of the common prefix (if any) shared by zPrev and
146412** zNext, in bytes. For example,
146413**
146414**   fts3PrefixCompress("abc", 3, "abcdef", 6)   // returns 3
146415**   fts3PrefixCompress("abX", 3, "abcdef", 6)   // returns 2
146416**   fts3PrefixCompress("abX", 3, "Xbcdef", 6)   // returns 0
146417*/
146418static int fts3PrefixCompress(
146419  const char *zPrev,              /* Buffer containing previous term */
146420  int nPrev,                      /* Size of buffer zPrev in bytes */
146421  const char *zNext,              /* Buffer containing next term */
146422  int nNext                       /* Size of buffer zNext in bytes */
146423){
146424  int n;
146425  UNUSED_PARAMETER(nNext);
146426  for(n=0; n<nPrev && zPrev[n]==zNext[n]; n++);
146427  return n;
146428}
146429
146430/*
146431** Add term zTerm to the SegmentNode. It is guaranteed that zTerm is larger
146432** (according to memcmp) than the previous term.
146433*/
146434static int fts3NodeAddTerm(
146435  Fts3Table *p,                   /* Virtual table handle */
146436  SegmentNode **ppTree,           /* IN/OUT: SegmentNode handle */
146437  int isCopyTerm,                 /* True if zTerm/nTerm is transient */
146438  const char *zTerm,              /* Pointer to buffer containing term */
146439  int nTerm                       /* Size of term in bytes */
146440){
146441  SegmentNode *pTree = *ppTree;
146442  int rc;
146443  SegmentNode *pNew;
146444
146445  /* First try to append the term to the current node. Return early if
146446  ** this is possible.
146447  */
146448  if( pTree ){
146449    int nData = pTree->nData;     /* Current size of node in bytes */
146450    int nReq = nData;             /* Required space after adding zTerm */
146451    int nPrefix;                  /* Number of bytes of prefix compression */
146452    int nSuffix;                  /* Suffix length */
146453
146454    nPrefix = fts3PrefixCompress(pTree->zTerm, pTree->nTerm, zTerm, nTerm);
146455    nSuffix = nTerm-nPrefix;
146456
146457    nReq += sqlite3Fts3VarintLen(nPrefix)+sqlite3Fts3VarintLen(nSuffix)+nSuffix;
146458    if( nReq<=p->nNodeSize || !pTree->zTerm ){
146459
146460      if( nReq>p->nNodeSize ){
146461        /* An unusual case: this is the first term to be added to the node
146462        ** and the static node buffer (p->nNodeSize bytes) is not large
146463        ** enough. Use a separately malloced buffer instead This wastes
146464        ** p->nNodeSize bytes, but since this scenario only comes about when
146465        ** the database contain two terms that share a prefix of almost 2KB,
146466        ** this is not expected to be a serious problem.
146467        */
146468        assert( pTree->aData==(char *)&pTree[1] );
146469        pTree->aData = (char *)sqlite3_malloc(nReq);
146470        if( !pTree->aData ){
146471          return SQLITE_NOMEM;
146472        }
146473      }
146474
146475      if( pTree->zTerm ){
146476        /* There is no prefix-length field for first term in a node */
146477        nData += sqlite3Fts3PutVarint(&pTree->aData[nData], nPrefix);
146478      }
146479
146480      nData += sqlite3Fts3PutVarint(&pTree->aData[nData], nSuffix);
146481      memcpy(&pTree->aData[nData], &zTerm[nPrefix], nSuffix);
146482      pTree->nData = nData + nSuffix;
146483      pTree->nEntry++;
146484
146485      if( isCopyTerm ){
146486        if( pTree->nMalloc<nTerm ){
146487          char *zNew = sqlite3_realloc(pTree->zMalloc, nTerm*2);
146488          if( !zNew ){
146489            return SQLITE_NOMEM;
146490          }
146491          pTree->nMalloc = nTerm*2;
146492          pTree->zMalloc = zNew;
146493        }
146494        pTree->zTerm = pTree->zMalloc;
146495        memcpy(pTree->zTerm, zTerm, nTerm);
146496        pTree->nTerm = nTerm;
146497      }else{
146498        pTree->zTerm = (char *)zTerm;
146499        pTree->nTerm = nTerm;
146500      }
146501      return SQLITE_OK;
146502    }
146503  }
146504
146505  /* If control flows to here, it was not possible to append zTerm to the
146506  ** current node. Create a new node (a right-sibling of the current node).
146507  ** If this is the first node in the tree, the term is added to it.
146508  **
146509  ** Otherwise, the term is not added to the new node, it is left empty for
146510  ** now. Instead, the term is inserted into the parent of pTree. If pTree
146511  ** has no parent, one is created here.
146512  */
146513  pNew = (SegmentNode *)sqlite3_malloc(sizeof(SegmentNode) + p->nNodeSize);
146514  if( !pNew ){
146515    return SQLITE_NOMEM;
146516  }
146517  memset(pNew, 0, sizeof(SegmentNode));
146518  pNew->nData = 1 + FTS3_VARINT_MAX;
146519  pNew->aData = (char *)&pNew[1];
146520
146521  if( pTree ){
146522    SegmentNode *pParent = pTree->pParent;
146523    rc = fts3NodeAddTerm(p, &pParent, isCopyTerm, zTerm, nTerm);
146524    if( pTree->pParent==0 ){
146525      pTree->pParent = pParent;
146526    }
146527    pTree->pRight = pNew;
146528    pNew->pLeftmost = pTree->pLeftmost;
146529    pNew->pParent = pParent;
146530    pNew->zMalloc = pTree->zMalloc;
146531    pNew->nMalloc = pTree->nMalloc;
146532    pTree->zMalloc = 0;
146533  }else{
146534    pNew->pLeftmost = pNew;
146535    rc = fts3NodeAddTerm(p, &pNew, isCopyTerm, zTerm, nTerm);
146536  }
146537
146538  *ppTree = pNew;
146539  return rc;
146540}
146541
146542/*
146543** Helper function for fts3NodeWrite().
146544*/
146545static int fts3TreeFinishNode(
146546  SegmentNode *pTree,
146547  int iHeight,
146548  sqlite3_int64 iLeftChild
146549){
146550  int nStart;
146551  assert( iHeight>=1 && iHeight<128 );
146552  nStart = FTS3_VARINT_MAX - sqlite3Fts3VarintLen(iLeftChild);
146553  pTree->aData[nStart] = (char)iHeight;
146554  sqlite3Fts3PutVarint(&pTree->aData[nStart+1], iLeftChild);
146555  return nStart;
146556}
146557
146558/*
146559** Write the buffer for the segment node pTree and all of its peers to the
146560** database. Then call this function recursively to write the parent of
146561** pTree and its peers to the database.
146562**
146563** Except, if pTree is a root node, do not write it to the database. Instead,
146564** set output variables *paRoot and *pnRoot to contain the root node.
146565**
146566** If successful, SQLITE_OK is returned and output variable *piLast is
146567** set to the largest blockid written to the database (or zero if no
146568** blocks were written to the db). Otherwise, an SQLite error code is
146569** returned.
146570*/
146571static int fts3NodeWrite(
146572  Fts3Table *p,                   /* Virtual table handle */
146573  SegmentNode *pTree,             /* SegmentNode handle */
146574  int iHeight,                    /* Height of this node in tree */
146575  sqlite3_int64 iLeaf,            /* Block id of first leaf node */
146576  sqlite3_int64 iFree,            /* Block id of next free slot in %_segments */
146577  sqlite3_int64 *piLast,          /* OUT: Block id of last entry written */
146578  char **paRoot,                  /* OUT: Data for root node */
146579  int *pnRoot                     /* OUT: Size of root node in bytes */
146580){
146581  int rc = SQLITE_OK;
146582
146583  if( !pTree->pParent ){
146584    /* Root node of the tree. */
146585    int nStart = fts3TreeFinishNode(pTree, iHeight, iLeaf);
146586    *piLast = iFree-1;
146587    *pnRoot = pTree->nData - nStart;
146588    *paRoot = &pTree->aData[nStart];
146589  }else{
146590    SegmentNode *pIter;
146591    sqlite3_int64 iNextFree = iFree;
146592    sqlite3_int64 iNextLeaf = iLeaf;
146593    for(pIter=pTree->pLeftmost; pIter && rc==SQLITE_OK; pIter=pIter->pRight){
146594      int nStart = fts3TreeFinishNode(pIter, iHeight, iNextLeaf);
146595      int nWrite = pIter->nData - nStart;
146596
146597      rc = fts3WriteSegment(p, iNextFree, &pIter->aData[nStart], nWrite);
146598      iNextFree++;
146599      iNextLeaf += (pIter->nEntry+1);
146600    }
146601    if( rc==SQLITE_OK ){
146602      assert( iNextLeaf==iFree );
146603      rc = fts3NodeWrite(
146604          p, pTree->pParent, iHeight+1, iFree, iNextFree, piLast, paRoot, pnRoot
146605      );
146606    }
146607  }
146608
146609  return rc;
146610}
146611
146612/*
146613** Free all memory allocations associated with the tree pTree.
146614*/
146615static void fts3NodeFree(SegmentNode *pTree){
146616  if( pTree ){
146617    SegmentNode *p = pTree->pLeftmost;
146618    fts3NodeFree(p->pParent);
146619    while( p ){
146620      SegmentNode *pRight = p->pRight;
146621      if( p->aData!=(char *)&p[1] ){
146622        sqlite3_free(p->aData);
146623      }
146624      assert( pRight==0 || p->zMalloc==0 );
146625      sqlite3_free(p->zMalloc);
146626      sqlite3_free(p);
146627      p = pRight;
146628    }
146629  }
146630}
146631
146632/*
146633** Add a term to the segment being constructed by the SegmentWriter object
146634** *ppWriter. When adding the first term to a segment, *ppWriter should
146635** be passed NULL. This function will allocate a new SegmentWriter object
146636** and return it via the input/output variable *ppWriter in this case.
146637**
146638** If successful, SQLITE_OK is returned. Otherwise, an SQLite error code.
146639*/
146640static int fts3SegWriterAdd(
146641  Fts3Table *p,                   /* Virtual table handle */
146642  SegmentWriter **ppWriter,       /* IN/OUT: SegmentWriter handle */
146643  int isCopyTerm,                 /* True if buffer zTerm must be copied */
146644  const char *zTerm,              /* Pointer to buffer containing term */
146645  int nTerm,                      /* Size of term in bytes */
146646  const char *aDoclist,           /* Pointer to buffer containing doclist */
146647  int nDoclist                    /* Size of doclist in bytes */
146648){
146649  int nPrefix;                    /* Size of term prefix in bytes */
146650  int nSuffix;                    /* Size of term suffix in bytes */
146651  int nReq;                       /* Number of bytes required on leaf page */
146652  int nData;
146653  SegmentWriter *pWriter = *ppWriter;
146654
146655  if( !pWriter ){
146656    int rc;
146657    sqlite3_stmt *pStmt;
146658
146659    /* Allocate the SegmentWriter structure */
146660    pWriter = (SegmentWriter *)sqlite3_malloc(sizeof(SegmentWriter));
146661    if( !pWriter ) return SQLITE_NOMEM;
146662    memset(pWriter, 0, sizeof(SegmentWriter));
146663    *ppWriter = pWriter;
146664
146665    /* Allocate a buffer in which to accumulate data */
146666    pWriter->aData = (char *)sqlite3_malloc(p->nNodeSize);
146667    if( !pWriter->aData ) return SQLITE_NOMEM;
146668    pWriter->nSize = p->nNodeSize;
146669
146670    /* Find the next free blockid in the %_segments table */
146671    rc = fts3SqlStmt(p, SQL_NEXT_SEGMENTS_ID, &pStmt, 0);
146672    if( rc!=SQLITE_OK ) return rc;
146673    if( SQLITE_ROW==sqlite3_step(pStmt) ){
146674      pWriter->iFree = sqlite3_column_int64(pStmt, 0);
146675      pWriter->iFirst = pWriter->iFree;
146676    }
146677    rc = sqlite3_reset(pStmt);
146678    if( rc!=SQLITE_OK ) return rc;
146679  }
146680  nData = pWriter->nData;
146681
146682  nPrefix = fts3PrefixCompress(pWriter->zTerm, pWriter->nTerm, zTerm, nTerm);
146683  nSuffix = nTerm-nPrefix;
146684
146685  /* Figure out how many bytes are required by this new entry */
146686  nReq = sqlite3Fts3VarintLen(nPrefix) +    /* varint containing prefix size */
146687    sqlite3Fts3VarintLen(nSuffix) +         /* varint containing suffix size */
146688    nSuffix +                               /* Term suffix */
146689    sqlite3Fts3VarintLen(nDoclist) +        /* Size of doclist */
146690    nDoclist;                               /* Doclist data */
146691
146692  if( nData>0 && nData+nReq>p->nNodeSize ){
146693    int rc;
146694
146695    /* The current leaf node is full. Write it out to the database. */
146696    rc = fts3WriteSegment(p, pWriter->iFree++, pWriter->aData, nData);
146697    if( rc!=SQLITE_OK ) return rc;
146698    p->nLeafAdd++;
146699
146700    /* Add the current term to the interior node tree. The term added to
146701    ** the interior tree must:
146702    **
146703    **   a) be greater than the largest term on the leaf node just written
146704    **      to the database (still available in pWriter->zTerm), and
146705    **
146706    **   b) be less than or equal to the term about to be added to the new
146707    **      leaf node (zTerm/nTerm).
146708    **
146709    ** In other words, it must be the prefix of zTerm 1 byte longer than
146710    ** the common prefix (if any) of zTerm and pWriter->zTerm.
146711    */
146712    assert( nPrefix<nTerm );
146713    rc = fts3NodeAddTerm(p, &pWriter->pTree, isCopyTerm, zTerm, nPrefix+1);
146714    if( rc!=SQLITE_OK ) return rc;
146715
146716    nData = 0;
146717    pWriter->nTerm = 0;
146718
146719    nPrefix = 0;
146720    nSuffix = nTerm;
146721    nReq = 1 +                              /* varint containing prefix size */
146722      sqlite3Fts3VarintLen(nTerm) +         /* varint containing suffix size */
146723      nTerm +                               /* Term suffix */
146724      sqlite3Fts3VarintLen(nDoclist) +      /* Size of doclist */
146725      nDoclist;                             /* Doclist data */
146726  }
146727
146728  /* Increase the total number of bytes written to account for the new entry. */
146729  pWriter->nLeafData += nReq;
146730
146731  /* If the buffer currently allocated is too small for this entry, realloc
146732  ** the buffer to make it large enough.
146733  */
146734  if( nReq>pWriter->nSize ){
146735    char *aNew = sqlite3_realloc(pWriter->aData, nReq);
146736    if( !aNew ) return SQLITE_NOMEM;
146737    pWriter->aData = aNew;
146738    pWriter->nSize = nReq;
146739  }
146740  assert( nData+nReq<=pWriter->nSize );
146741
146742  /* Append the prefix-compressed term and doclist to the buffer. */
146743  nData += sqlite3Fts3PutVarint(&pWriter->aData[nData], nPrefix);
146744  nData += sqlite3Fts3PutVarint(&pWriter->aData[nData], nSuffix);
146745  memcpy(&pWriter->aData[nData], &zTerm[nPrefix], nSuffix);
146746  nData += nSuffix;
146747  nData += sqlite3Fts3PutVarint(&pWriter->aData[nData], nDoclist);
146748  memcpy(&pWriter->aData[nData], aDoclist, nDoclist);
146749  pWriter->nData = nData + nDoclist;
146750
146751  /* Save the current term so that it can be used to prefix-compress the next.
146752  ** If the isCopyTerm parameter is true, then the buffer pointed to by
146753  ** zTerm is transient, so take a copy of the term data. Otherwise, just
146754  ** store a copy of the pointer.
146755  */
146756  if( isCopyTerm ){
146757    if( nTerm>pWriter->nMalloc ){
146758      char *zNew = sqlite3_realloc(pWriter->zMalloc, nTerm*2);
146759      if( !zNew ){
146760        return SQLITE_NOMEM;
146761      }
146762      pWriter->nMalloc = nTerm*2;
146763      pWriter->zMalloc = zNew;
146764      pWriter->zTerm = zNew;
146765    }
146766    assert( pWriter->zTerm==pWriter->zMalloc );
146767    memcpy(pWriter->zTerm, zTerm, nTerm);
146768  }else{
146769    pWriter->zTerm = (char *)zTerm;
146770  }
146771  pWriter->nTerm = nTerm;
146772
146773  return SQLITE_OK;
146774}
146775
146776/*
146777** Flush all data associated with the SegmentWriter object pWriter to the
146778** database. This function must be called after all terms have been added
146779** to the segment using fts3SegWriterAdd(). If successful, SQLITE_OK is
146780** returned. Otherwise, an SQLite error code.
146781*/
146782static int fts3SegWriterFlush(
146783  Fts3Table *p,                   /* Virtual table handle */
146784  SegmentWriter *pWriter,         /* SegmentWriter to flush to the db */
146785  sqlite3_int64 iLevel,           /* Value for 'level' column of %_segdir */
146786  int iIdx                        /* Value for 'idx' column of %_segdir */
146787){
146788  int rc;                         /* Return code */
146789  if( pWriter->pTree ){
146790    sqlite3_int64 iLast = 0;      /* Largest block id written to database */
146791    sqlite3_int64 iLastLeaf;      /* Largest leaf block id written to db */
146792    char *zRoot = NULL;           /* Pointer to buffer containing root node */
146793    int nRoot = 0;                /* Size of buffer zRoot */
146794
146795    iLastLeaf = pWriter->iFree;
146796    rc = fts3WriteSegment(p, pWriter->iFree++, pWriter->aData, pWriter->nData);
146797    if( rc==SQLITE_OK ){
146798      rc = fts3NodeWrite(p, pWriter->pTree, 1,
146799          pWriter->iFirst, pWriter->iFree, &iLast, &zRoot, &nRoot);
146800    }
146801    if( rc==SQLITE_OK ){
146802      rc = fts3WriteSegdir(p, iLevel, iIdx,
146803          pWriter->iFirst, iLastLeaf, iLast, pWriter->nLeafData, zRoot, nRoot);
146804    }
146805  }else{
146806    /* The entire tree fits on the root node. Write it to the segdir table. */
146807    rc = fts3WriteSegdir(p, iLevel, iIdx,
146808        0, 0, 0, pWriter->nLeafData, pWriter->aData, pWriter->nData);
146809  }
146810  p->nLeafAdd++;
146811  return rc;
146812}
146813
146814/*
146815** Release all memory held by the SegmentWriter object passed as the
146816** first argument.
146817*/
146818static void fts3SegWriterFree(SegmentWriter *pWriter){
146819  if( pWriter ){
146820    sqlite3_free(pWriter->aData);
146821    sqlite3_free(pWriter->zMalloc);
146822    fts3NodeFree(pWriter->pTree);
146823    sqlite3_free(pWriter);
146824  }
146825}
146826
146827/*
146828** The first value in the apVal[] array is assumed to contain an integer.
146829** This function tests if there exist any documents with docid values that
146830** are different from that integer. i.e. if deleting the document with docid
146831** pRowid would mean the FTS3 table were empty.
146832**
146833** If successful, *pisEmpty is set to true if the table is empty except for
146834** document pRowid, or false otherwise, and SQLITE_OK is returned. If an
146835** error occurs, an SQLite error code is returned.
146836*/
146837static int fts3IsEmpty(Fts3Table *p, sqlite3_value *pRowid, int *pisEmpty){
146838  sqlite3_stmt *pStmt;
146839  int rc;
146840  if( p->zContentTbl ){
146841    /* If using the content=xxx option, assume the table is never empty */
146842    *pisEmpty = 0;
146843    rc = SQLITE_OK;
146844  }else{
146845    rc = fts3SqlStmt(p, SQL_IS_EMPTY, &pStmt, &pRowid);
146846    if( rc==SQLITE_OK ){
146847      if( SQLITE_ROW==sqlite3_step(pStmt) ){
146848        *pisEmpty = sqlite3_column_int(pStmt, 0);
146849      }
146850      rc = sqlite3_reset(pStmt);
146851    }
146852  }
146853  return rc;
146854}
146855
146856/*
146857** Set *pnMax to the largest segment level in the database for the index
146858** iIndex.
146859**
146860** Segment levels are stored in the 'level' column of the %_segdir table.
146861**
146862** Return SQLITE_OK if successful, or an SQLite error code if not.
146863*/
146864static int fts3SegmentMaxLevel(
146865  Fts3Table *p,
146866  int iLangid,
146867  int iIndex,
146868  sqlite3_int64 *pnMax
146869){
146870  sqlite3_stmt *pStmt;
146871  int rc;
146872  assert( iIndex>=0 && iIndex<p->nIndex );
146873
146874  /* Set pStmt to the compiled version of:
146875  **
146876  **   SELECT max(level) FROM %Q.'%q_segdir' WHERE level BETWEEN ? AND ?
146877  **
146878  ** (1024 is actually the value of macro FTS3_SEGDIR_PREFIXLEVEL_STR).
146879  */
146880  rc = fts3SqlStmt(p, SQL_SELECT_SEGDIR_MAX_LEVEL, &pStmt, 0);
146881  if( rc!=SQLITE_OK ) return rc;
146882  sqlite3_bind_int64(pStmt, 1, getAbsoluteLevel(p, iLangid, iIndex, 0));
146883  sqlite3_bind_int64(pStmt, 2,
146884      getAbsoluteLevel(p, iLangid, iIndex, FTS3_SEGDIR_MAXLEVEL-1)
146885  );
146886  if( SQLITE_ROW==sqlite3_step(pStmt) ){
146887    *pnMax = sqlite3_column_int64(pStmt, 0);
146888  }
146889  return sqlite3_reset(pStmt);
146890}
146891
146892/*
146893** iAbsLevel is an absolute level that may be assumed to exist within
146894** the database. This function checks if it is the largest level number
146895** within its index. Assuming no error occurs, *pbMax is set to 1 if
146896** iAbsLevel is indeed the largest level, or 0 otherwise, and SQLITE_OK
146897** is returned. If an error occurs, an error code is returned and the
146898** final value of *pbMax is undefined.
146899*/
146900static int fts3SegmentIsMaxLevel(Fts3Table *p, i64 iAbsLevel, int *pbMax){
146901
146902  /* Set pStmt to the compiled version of:
146903  **
146904  **   SELECT max(level) FROM %Q.'%q_segdir' WHERE level BETWEEN ? AND ?
146905  **
146906  ** (1024 is actually the value of macro FTS3_SEGDIR_PREFIXLEVEL_STR).
146907  */
146908  sqlite3_stmt *pStmt;
146909  int rc = fts3SqlStmt(p, SQL_SELECT_SEGDIR_MAX_LEVEL, &pStmt, 0);
146910  if( rc!=SQLITE_OK ) return rc;
146911  sqlite3_bind_int64(pStmt, 1, iAbsLevel+1);
146912  sqlite3_bind_int64(pStmt, 2,
146913      ((iAbsLevel/FTS3_SEGDIR_MAXLEVEL)+1) * FTS3_SEGDIR_MAXLEVEL
146914  );
146915
146916  *pbMax = 0;
146917  if( SQLITE_ROW==sqlite3_step(pStmt) ){
146918    *pbMax = sqlite3_column_type(pStmt, 0)==SQLITE_NULL;
146919  }
146920  return sqlite3_reset(pStmt);
146921}
146922
146923/*
146924** Delete all entries in the %_segments table associated with the segment
146925** opened with seg-reader pSeg. This function does not affect the contents
146926** of the %_segdir table.
146927*/
146928static int fts3DeleteSegment(
146929  Fts3Table *p,                   /* FTS table handle */
146930  Fts3SegReader *pSeg             /* Segment to delete */
146931){
146932  int rc = SQLITE_OK;             /* Return code */
146933  if( pSeg->iStartBlock ){
146934    sqlite3_stmt *pDelete;        /* SQL statement to delete rows */
146935    rc = fts3SqlStmt(p, SQL_DELETE_SEGMENTS_RANGE, &pDelete, 0);
146936    if( rc==SQLITE_OK ){
146937      sqlite3_bind_int64(pDelete, 1, pSeg->iStartBlock);
146938      sqlite3_bind_int64(pDelete, 2, pSeg->iEndBlock);
146939      sqlite3_step(pDelete);
146940      rc = sqlite3_reset(pDelete);
146941    }
146942  }
146943  return rc;
146944}
146945
146946/*
146947** This function is used after merging multiple segments into a single large
146948** segment to delete the old, now redundant, segment b-trees. Specifically,
146949** it:
146950**
146951**   1) Deletes all %_segments entries for the segments associated with
146952**      each of the SegReader objects in the array passed as the third
146953**      argument, and
146954**
146955**   2) deletes all %_segdir entries with level iLevel, or all %_segdir
146956**      entries regardless of level if (iLevel<0).
146957**
146958** SQLITE_OK is returned if successful, otherwise an SQLite error code.
146959*/
146960static int fts3DeleteSegdir(
146961  Fts3Table *p,                   /* Virtual table handle */
146962  int iLangid,                    /* Language id */
146963  int iIndex,                     /* Index for p->aIndex */
146964  int iLevel,                     /* Level of %_segdir entries to delete */
146965  Fts3SegReader **apSegment,      /* Array of SegReader objects */
146966  int nReader                     /* Size of array apSegment */
146967){
146968  int rc = SQLITE_OK;             /* Return Code */
146969  int i;                          /* Iterator variable */
146970  sqlite3_stmt *pDelete = 0;      /* SQL statement to delete rows */
146971
146972  for(i=0; rc==SQLITE_OK && i<nReader; i++){
146973    rc = fts3DeleteSegment(p, apSegment[i]);
146974  }
146975  if( rc!=SQLITE_OK ){
146976    return rc;
146977  }
146978
146979  assert( iLevel>=0 || iLevel==FTS3_SEGCURSOR_ALL );
146980  if( iLevel==FTS3_SEGCURSOR_ALL ){
146981    rc = fts3SqlStmt(p, SQL_DELETE_SEGDIR_RANGE, &pDelete, 0);
146982    if( rc==SQLITE_OK ){
146983      sqlite3_bind_int64(pDelete, 1, getAbsoluteLevel(p, iLangid, iIndex, 0));
146984      sqlite3_bind_int64(pDelete, 2,
146985          getAbsoluteLevel(p, iLangid, iIndex, FTS3_SEGDIR_MAXLEVEL-1)
146986      );
146987    }
146988  }else{
146989    rc = fts3SqlStmt(p, SQL_DELETE_SEGDIR_LEVEL, &pDelete, 0);
146990    if( rc==SQLITE_OK ){
146991      sqlite3_bind_int64(
146992          pDelete, 1, getAbsoluteLevel(p, iLangid, iIndex, iLevel)
146993      );
146994    }
146995  }
146996
146997  if( rc==SQLITE_OK ){
146998    sqlite3_step(pDelete);
146999    rc = sqlite3_reset(pDelete);
147000  }
147001
147002  return rc;
147003}
147004
147005/*
147006** When this function is called, buffer *ppList (size *pnList bytes) contains
147007** a position list that may (or may not) feature multiple columns. This
147008** function adjusts the pointer *ppList and the length *pnList so that they
147009** identify the subset of the position list that corresponds to column iCol.
147010**
147011** If there are no entries in the input position list for column iCol, then
147012** *pnList is set to zero before returning.
147013**
147014** If parameter bZero is non-zero, then any part of the input list following
147015** the end of the output list is zeroed before returning.
147016*/
147017static void fts3ColumnFilter(
147018  int iCol,                       /* Column to filter on */
147019  int bZero,                      /* Zero out anything following *ppList */
147020  char **ppList,                  /* IN/OUT: Pointer to position list */
147021  int *pnList                     /* IN/OUT: Size of buffer *ppList in bytes */
147022){
147023  char *pList = *ppList;
147024  int nList = *pnList;
147025  char *pEnd = &pList[nList];
147026  int iCurrent = 0;
147027  char *p = pList;
147028
147029  assert( iCol>=0 );
147030  while( 1 ){
147031    char c = 0;
147032    while( p<pEnd && (c | *p)&0xFE ) c = *p++ & 0x80;
147033
147034    if( iCol==iCurrent ){
147035      nList = (int)(p - pList);
147036      break;
147037    }
147038
147039    nList -= (int)(p - pList);
147040    pList = p;
147041    if( nList==0 ){
147042      break;
147043    }
147044    p = &pList[1];
147045    p += fts3GetVarint32(p, &iCurrent);
147046  }
147047
147048  if( bZero && &pList[nList]!=pEnd ){
147049    memset(&pList[nList], 0, pEnd - &pList[nList]);
147050  }
147051  *ppList = pList;
147052  *pnList = nList;
147053}
147054
147055/*
147056** Cache data in the Fts3MultiSegReader.aBuffer[] buffer (overwriting any
147057** existing data). Grow the buffer if required.
147058**
147059** If successful, return SQLITE_OK. Otherwise, if an OOM error is encountered
147060** trying to resize the buffer, return SQLITE_NOMEM.
147061*/
147062static int fts3MsrBufferData(
147063  Fts3MultiSegReader *pMsr,       /* Multi-segment-reader handle */
147064  char *pList,
147065  int nList
147066){
147067  if( nList>pMsr->nBuffer ){
147068    char *pNew;
147069    pMsr->nBuffer = nList*2;
147070    pNew = (char *)sqlite3_realloc(pMsr->aBuffer, pMsr->nBuffer);
147071    if( !pNew ) return SQLITE_NOMEM;
147072    pMsr->aBuffer = pNew;
147073  }
147074
147075  memcpy(pMsr->aBuffer, pList, nList);
147076  return SQLITE_OK;
147077}
147078
147079SQLITE_PRIVATE int sqlite3Fts3MsrIncrNext(
147080  Fts3Table *p,                   /* Virtual table handle */
147081  Fts3MultiSegReader *pMsr,       /* Multi-segment-reader handle */
147082  sqlite3_int64 *piDocid,         /* OUT: Docid value */
147083  char **paPoslist,               /* OUT: Pointer to position list */
147084  int *pnPoslist                  /* OUT: Size of position list in bytes */
147085){
147086  int nMerge = pMsr->nAdvance;
147087  Fts3SegReader **apSegment = pMsr->apSegment;
147088  int (*xCmp)(Fts3SegReader *, Fts3SegReader *) = (
147089    p->bDescIdx ? fts3SegReaderDoclistCmpRev : fts3SegReaderDoclistCmp
147090  );
147091
147092  if( nMerge==0 ){
147093    *paPoslist = 0;
147094    return SQLITE_OK;
147095  }
147096
147097  while( 1 ){
147098    Fts3SegReader *pSeg;
147099    pSeg = pMsr->apSegment[0];
147100
147101    if( pSeg->pOffsetList==0 ){
147102      *paPoslist = 0;
147103      break;
147104    }else{
147105      int rc;
147106      char *pList;
147107      int nList;
147108      int j;
147109      sqlite3_int64 iDocid = apSegment[0]->iDocid;
147110
147111      rc = fts3SegReaderNextDocid(p, apSegment[0], &pList, &nList);
147112      j = 1;
147113      while( rc==SQLITE_OK
147114        && j<nMerge
147115        && apSegment[j]->pOffsetList
147116        && apSegment[j]->iDocid==iDocid
147117      ){
147118        rc = fts3SegReaderNextDocid(p, apSegment[j], 0, 0);
147119        j++;
147120      }
147121      if( rc!=SQLITE_OK ) return rc;
147122      fts3SegReaderSort(pMsr->apSegment, nMerge, j, xCmp);
147123
147124      if( nList>0 && fts3SegReaderIsPending(apSegment[0]) ){
147125        rc = fts3MsrBufferData(pMsr, pList, nList+1);
147126        if( rc!=SQLITE_OK ) return rc;
147127        assert( (pMsr->aBuffer[nList] & 0xFE)==0x00 );
147128        pList = pMsr->aBuffer;
147129      }
147130
147131      if( pMsr->iColFilter>=0 ){
147132        fts3ColumnFilter(pMsr->iColFilter, 1, &pList, &nList);
147133      }
147134
147135      if( nList>0 ){
147136        *paPoslist = pList;
147137        *piDocid = iDocid;
147138        *pnPoslist = nList;
147139        break;
147140      }
147141    }
147142  }
147143
147144  return SQLITE_OK;
147145}
147146
147147static int fts3SegReaderStart(
147148  Fts3Table *p,                   /* Virtual table handle */
147149  Fts3MultiSegReader *pCsr,       /* Cursor object */
147150  const char *zTerm,              /* Term searched for (or NULL) */
147151  int nTerm                       /* Length of zTerm in bytes */
147152){
147153  int i;
147154  int nSeg = pCsr->nSegment;
147155
147156  /* If the Fts3SegFilter defines a specific term (or term prefix) to search
147157  ** for, then advance each segment iterator until it points to a term of
147158  ** equal or greater value than the specified term. This prevents many
147159  ** unnecessary merge/sort operations for the case where single segment
147160  ** b-tree leaf nodes contain more than one term.
147161  */
147162  for(i=0; pCsr->bRestart==0 && i<pCsr->nSegment; i++){
147163    int res = 0;
147164    Fts3SegReader *pSeg = pCsr->apSegment[i];
147165    do {
147166      int rc = fts3SegReaderNext(p, pSeg, 0);
147167      if( rc!=SQLITE_OK ) return rc;
147168    }while( zTerm && (res = fts3SegReaderTermCmp(pSeg, zTerm, nTerm))<0 );
147169
147170    if( pSeg->bLookup && res!=0 ){
147171      fts3SegReaderSetEof(pSeg);
147172    }
147173  }
147174  fts3SegReaderSort(pCsr->apSegment, nSeg, nSeg, fts3SegReaderCmp);
147175
147176  return SQLITE_OK;
147177}
147178
147179SQLITE_PRIVATE int sqlite3Fts3SegReaderStart(
147180  Fts3Table *p,                   /* Virtual table handle */
147181  Fts3MultiSegReader *pCsr,       /* Cursor object */
147182  Fts3SegFilter *pFilter          /* Restrictions on range of iteration */
147183){
147184  pCsr->pFilter = pFilter;
147185  return fts3SegReaderStart(p, pCsr, pFilter->zTerm, pFilter->nTerm);
147186}
147187
147188SQLITE_PRIVATE int sqlite3Fts3MsrIncrStart(
147189  Fts3Table *p,                   /* Virtual table handle */
147190  Fts3MultiSegReader *pCsr,       /* Cursor object */
147191  int iCol,                       /* Column to match on. */
147192  const char *zTerm,              /* Term to iterate through a doclist for */
147193  int nTerm                       /* Number of bytes in zTerm */
147194){
147195  int i;
147196  int rc;
147197  int nSegment = pCsr->nSegment;
147198  int (*xCmp)(Fts3SegReader *, Fts3SegReader *) = (
147199    p->bDescIdx ? fts3SegReaderDoclistCmpRev : fts3SegReaderDoclistCmp
147200  );
147201
147202  assert( pCsr->pFilter==0 );
147203  assert( zTerm && nTerm>0 );
147204
147205  /* Advance each segment iterator until it points to the term zTerm/nTerm. */
147206  rc = fts3SegReaderStart(p, pCsr, zTerm, nTerm);
147207  if( rc!=SQLITE_OK ) return rc;
147208
147209  /* Determine how many of the segments actually point to zTerm/nTerm. */
147210  for(i=0; i<nSegment; i++){
147211    Fts3SegReader *pSeg = pCsr->apSegment[i];
147212    if( !pSeg->aNode || fts3SegReaderTermCmp(pSeg, zTerm, nTerm) ){
147213      break;
147214    }
147215  }
147216  pCsr->nAdvance = i;
147217
147218  /* Advance each of the segments to point to the first docid. */
147219  for(i=0; i<pCsr->nAdvance; i++){
147220    rc = fts3SegReaderFirstDocid(p, pCsr->apSegment[i]);
147221    if( rc!=SQLITE_OK ) return rc;
147222  }
147223  fts3SegReaderSort(pCsr->apSegment, i, i, xCmp);
147224
147225  assert( iCol<0 || iCol<p->nColumn );
147226  pCsr->iColFilter = iCol;
147227
147228  return SQLITE_OK;
147229}
147230
147231/*
147232** This function is called on a MultiSegReader that has been started using
147233** sqlite3Fts3MsrIncrStart(). One or more calls to MsrIncrNext() may also
147234** have been made. Calling this function puts the MultiSegReader in such
147235** a state that if the next two calls are:
147236**
147237**   sqlite3Fts3SegReaderStart()
147238**   sqlite3Fts3SegReaderStep()
147239**
147240** then the entire doclist for the term is available in
147241** MultiSegReader.aDoclist/nDoclist.
147242*/
147243SQLITE_PRIVATE int sqlite3Fts3MsrIncrRestart(Fts3MultiSegReader *pCsr){
147244  int i;                          /* Used to iterate through segment-readers */
147245
147246  assert( pCsr->zTerm==0 );
147247  assert( pCsr->nTerm==0 );
147248  assert( pCsr->aDoclist==0 );
147249  assert( pCsr->nDoclist==0 );
147250
147251  pCsr->nAdvance = 0;
147252  pCsr->bRestart = 1;
147253  for(i=0; i<pCsr->nSegment; i++){
147254    pCsr->apSegment[i]->pOffsetList = 0;
147255    pCsr->apSegment[i]->nOffsetList = 0;
147256    pCsr->apSegment[i]->iDocid = 0;
147257  }
147258
147259  return SQLITE_OK;
147260}
147261
147262
147263SQLITE_PRIVATE int sqlite3Fts3SegReaderStep(
147264  Fts3Table *p,                   /* Virtual table handle */
147265  Fts3MultiSegReader *pCsr        /* Cursor object */
147266){
147267  int rc = SQLITE_OK;
147268
147269  int isIgnoreEmpty =  (pCsr->pFilter->flags & FTS3_SEGMENT_IGNORE_EMPTY);
147270  int isRequirePos =   (pCsr->pFilter->flags & FTS3_SEGMENT_REQUIRE_POS);
147271  int isColFilter =    (pCsr->pFilter->flags & FTS3_SEGMENT_COLUMN_FILTER);
147272  int isPrefix =       (pCsr->pFilter->flags & FTS3_SEGMENT_PREFIX);
147273  int isScan =         (pCsr->pFilter->flags & FTS3_SEGMENT_SCAN);
147274  int isFirst =        (pCsr->pFilter->flags & FTS3_SEGMENT_FIRST);
147275
147276  Fts3SegReader **apSegment = pCsr->apSegment;
147277  int nSegment = pCsr->nSegment;
147278  Fts3SegFilter *pFilter = pCsr->pFilter;
147279  int (*xCmp)(Fts3SegReader *, Fts3SegReader *) = (
147280    p->bDescIdx ? fts3SegReaderDoclistCmpRev : fts3SegReaderDoclistCmp
147281  );
147282
147283  if( pCsr->nSegment==0 ) return SQLITE_OK;
147284
147285  do {
147286    int nMerge;
147287    int i;
147288
147289    /* Advance the first pCsr->nAdvance entries in the apSegment[] array
147290    ** forward. Then sort the list in order of current term again.
147291    */
147292    for(i=0; i<pCsr->nAdvance; i++){
147293      Fts3SegReader *pSeg = apSegment[i];
147294      if( pSeg->bLookup ){
147295        fts3SegReaderSetEof(pSeg);
147296      }else{
147297        rc = fts3SegReaderNext(p, pSeg, 0);
147298      }
147299      if( rc!=SQLITE_OK ) return rc;
147300    }
147301    fts3SegReaderSort(apSegment, nSegment, pCsr->nAdvance, fts3SegReaderCmp);
147302    pCsr->nAdvance = 0;
147303
147304    /* If all the seg-readers are at EOF, we're finished. return SQLITE_OK. */
147305    assert( rc==SQLITE_OK );
147306    if( apSegment[0]->aNode==0 ) break;
147307
147308    pCsr->nTerm = apSegment[0]->nTerm;
147309    pCsr->zTerm = apSegment[0]->zTerm;
147310
147311    /* If this is a prefix-search, and if the term that apSegment[0] points
147312    ** to does not share a suffix with pFilter->zTerm/nTerm, then all
147313    ** required callbacks have been made. In this case exit early.
147314    **
147315    ** Similarly, if this is a search for an exact match, and the first term
147316    ** of segment apSegment[0] is not a match, exit early.
147317    */
147318    if( pFilter->zTerm && !isScan ){
147319      if( pCsr->nTerm<pFilter->nTerm
147320       || (!isPrefix && pCsr->nTerm>pFilter->nTerm)
147321       || memcmp(pCsr->zTerm, pFilter->zTerm, pFilter->nTerm)
147322      ){
147323        break;
147324      }
147325    }
147326
147327    nMerge = 1;
147328    while( nMerge<nSegment
147329        && apSegment[nMerge]->aNode
147330        && apSegment[nMerge]->nTerm==pCsr->nTerm
147331        && 0==memcmp(pCsr->zTerm, apSegment[nMerge]->zTerm, pCsr->nTerm)
147332    ){
147333      nMerge++;
147334    }
147335
147336    assert( isIgnoreEmpty || (isRequirePos && !isColFilter) );
147337    if( nMerge==1
147338     && !isIgnoreEmpty
147339     && !isFirst
147340     && (p->bDescIdx==0 || fts3SegReaderIsPending(apSegment[0])==0)
147341    ){
147342      pCsr->nDoclist = apSegment[0]->nDoclist;
147343      if( fts3SegReaderIsPending(apSegment[0]) ){
147344        rc = fts3MsrBufferData(pCsr, apSegment[0]->aDoclist, pCsr->nDoclist);
147345        pCsr->aDoclist = pCsr->aBuffer;
147346      }else{
147347        pCsr->aDoclist = apSegment[0]->aDoclist;
147348      }
147349      if( rc==SQLITE_OK ) rc = SQLITE_ROW;
147350    }else{
147351      int nDoclist = 0;           /* Size of doclist */
147352      sqlite3_int64 iPrev = 0;    /* Previous docid stored in doclist */
147353
147354      /* The current term of the first nMerge entries in the array
147355      ** of Fts3SegReader objects is the same. The doclists must be merged
147356      ** and a single term returned with the merged doclist.
147357      */
147358      for(i=0; i<nMerge; i++){
147359        fts3SegReaderFirstDocid(p, apSegment[i]);
147360      }
147361      fts3SegReaderSort(apSegment, nMerge, nMerge, xCmp);
147362      while( apSegment[0]->pOffsetList ){
147363        int j;                    /* Number of segments that share a docid */
147364        char *pList = 0;
147365        int nList = 0;
147366        int nByte;
147367        sqlite3_int64 iDocid = apSegment[0]->iDocid;
147368        fts3SegReaderNextDocid(p, apSegment[0], &pList, &nList);
147369        j = 1;
147370        while( j<nMerge
147371            && apSegment[j]->pOffsetList
147372            && apSegment[j]->iDocid==iDocid
147373        ){
147374          fts3SegReaderNextDocid(p, apSegment[j], 0, 0);
147375          j++;
147376        }
147377
147378        if( isColFilter ){
147379          fts3ColumnFilter(pFilter->iCol, 0, &pList, &nList);
147380        }
147381
147382        if( !isIgnoreEmpty || nList>0 ){
147383
147384          /* Calculate the 'docid' delta value to write into the merged
147385          ** doclist. */
147386          sqlite3_int64 iDelta;
147387          if( p->bDescIdx && nDoclist>0 ){
147388            iDelta = iPrev - iDocid;
147389          }else{
147390            iDelta = iDocid - iPrev;
147391          }
147392          assert( iDelta>0 || (nDoclist==0 && iDelta==iDocid) );
147393          assert( nDoclist>0 || iDelta==iDocid );
147394
147395          nByte = sqlite3Fts3VarintLen(iDelta) + (isRequirePos?nList+1:0);
147396          if( nDoclist+nByte>pCsr->nBuffer ){
147397            char *aNew;
147398            pCsr->nBuffer = (nDoclist+nByte)*2;
147399            aNew = sqlite3_realloc(pCsr->aBuffer, pCsr->nBuffer);
147400            if( !aNew ){
147401              return SQLITE_NOMEM;
147402            }
147403            pCsr->aBuffer = aNew;
147404          }
147405
147406          if( isFirst ){
147407            char *a = &pCsr->aBuffer[nDoclist];
147408            int nWrite;
147409
147410            nWrite = sqlite3Fts3FirstFilter(iDelta, pList, nList, a);
147411            if( nWrite ){
147412              iPrev = iDocid;
147413              nDoclist += nWrite;
147414            }
147415          }else{
147416            nDoclist += sqlite3Fts3PutVarint(&pCsr->aBuffer[nDoclist], iDelta);
147417            iPrev = iDocid;
147418            if( isRequirePos ){
147419              memcpy(&pCsr->aBuffer[nDoclist], pList, nList);
147420              nDoclist += nList;
147421              pCsr->aBuffer[nDoclist++] = '\0';
147422            }
147423          }
147424        }
147425
147426        fts3SegReaderSort(apSegment, nMerge, j, xCmp);
147427      }
147428      if( nDoclist>0 ){
147429        pCsr->aDoclist = pCsr->aBuffer;
147430        pCsr->nDoclist = nDoclist;
147431        rc = SQLITE_ROW;
147432      }
147433    }
147434    pCsr->nAdvance = nMerge;
147435  }while( rc==SQLITE_OK );
147436
147437  return rc;
147438}
147439
147440
147441SQLITE_PRIVATE void sqlite3Fts3SegReaderFinish(
147442  Fts3MultiSegReader *pCsr       /* Cursor object */
147443){
147444  if( pCsr ){
147445    int i;
147446    for(i=0; i<pCsr->nSegment; i++){
147447      sqlite3Fts3SegReaderFree(pCsr->apSegment[i]);
147448    }
147449    sqlite3_free(pCsr->apSegment);
147450    sqlite3_free(pCsr->aBuffer);
147451
147452    pCsr->nSegment = 0;
147453    pCsr->apSegment = 0;
147454    pCsr->aBuffer = 0;
147455  }
147456}
147457
147458/*
147459** Decode the "end_block" field, selected by column iCol of the SELECT
147460** statement passed as the first argument.
147461**
147462** The "end_block" field may contain either an integer, or a text field
147463** containing the text representation of two non-negative integers separated
147464** by one or more space (0x20) characters. In the first case, set *piEndBlock
147465** to the integer value and *pnByte to zero before returning. In the second,
147466** set *piEndBlock to the first value and *pnByte to the second.
147467*/
147468static void fts3ReadEndBlockField(
147469  sqlite3_stmt *pStmt,
147470  int iCol,
147471  i64 *piEndBlock,
147472  i64 *pnByte
147473){
147474  const unsigned char *zText = sqlite3_column_text(pStmt, iCol);
147475  if( zText ){
147476    int i;
147477    int iMul = 1;
147478    i64 iVal = 0;
147479    for(i=0; zText[i]>='0' && zText[i]<='9'; i++){
147480      iVal = iVal*10 + (zText[i] - '0');
147481    }
147482    *piEndBlock = iVal;
147483    while( zText[i]==' ' ) i++;
147484    iVal = 0;
147485    if( zText[i]=='-' ){
147486      i++;
147487      iMul = -1;
147488    }
147489    for(/* no-op */; zText[i]>='0' && zText[i]<='9'; i++){
147490      iVal = iVal*10 + (zText[i] - '0');
147491    }
147492    *pnByte = (iVal * (i64)iMul);
147493  }
147494}
147495
147496
147497/*
147498** A segment of size nByte bytes has just been written to absolute level
147499** iAbsLevel. Promote any segments that should be promoted as a result.
147500*/
147501static int fts3PromoteSegments(
147502  Fts3Table *p,                   /* FTS table handle */
147503  sqlite3_int64 iAbsLevel,        /* Absolute level just updated */
147504  sqlite3_int64 nByte             /* Size of new segment at iAbsLevel */
147505){
147506  int rc = SQLITE_OK;
147507  sqlite3_stmt *pRange;
147508
147509  rc = fts3SqlStmt(p, SQL_SELECT_LEVEL_RANGE2, &pRange, 0);
147510
147511  if( rc==SQLITE_OK ){
147512    int bOk = 0;
147513    i64 iLast = (iAbsLevel/FTS3_SEGDIR_MAXLEVEL + 1) * FTS3_SEGDIR_MAXLEVEL - 1;
147514    i64 nLimit = (nByte*3)/2;
147515
147516    /* Loop through all entries in the %_segdir table corresponding to
147517    ** segments in this index on levels greater than iAbsLevel. If there is
147518    ** at least one such segment, and it is possible to determine that all
147519    ** such segments are smaller than nLimit bytes in size, they will be
147520    ** promoted to level iAbsLevel.  */
147521    sqlite3_bind_int64(pRange, 1, iAbsLevel+1);
147522    sqlite3_bind_int64(pRange, 2, iLast);
147523    while( SQLITE_ROW==sqlite3_step(pRange) ){
147524      i64 nSize = 0, dummy;
147525      fts3ReadEndBlockField(pRange, 2, &dummy, &nSize);
147526      if( nSize<=0 || nSize>nLimit ){
147527        /* If nSize==0, then the %_segdir.end_block field does not not
147528        ** contain a size value. This happens if it was written by an
147529        ** old version of FTS. In this case it is not possible to determine
147530        ** the size of the segment, and so segment promotion does not
147531        ** take place.  */
147532        bOk = 0;
147533        break;
147534      }
147535      bOk = 1;
147536    }
147537    rc = sqlite3_reset(pRange);
147538
147539    if( bOk ){
147540      int iIdx = 0;
147541      sqlite3_stmt *pUpdate1 = 0;
147542      sqlite3_stmt *pUpdate2 = 0;
147543
147544      if( rc==SQLITE_OK ){
147545        rc = fts3SqlStmt(p, SQL_UPDATE_LEVEL_IDX, &pUpdate1, 0);
147546      }
147547      if( rc==SQLITE_OK ){
147548        rc = fts3SqlStmt(p, SQL_UPDATE_LEVEL, &pUpdate2, 0);
147549      }
147550
147551      if( rc==SQLITE_OK ){
147552
147553        /* Loop through all %_segdir entries for segments in this index with
147554        ** levels equal to or greater than iAbsLevel. As each entry is visited,
147555        ** updated it to set (level = -1) and (idx = N), where N is 0 for the
147556        ** oldest segment in the range, 1 for the next oldest, and so on.
147557        **
147558        ** In other words, move all segments being promoted to level -1,
147559        ** setting the "idx" fields as appropriate to keep them in the same
147560        ** order. The contents of level -1 (which is never used, except
147561        ** transiently here), will be moved back to level iAbsLevel below.  */
147562        sqlite3_bind_int64(pRange, 1, iAbsLevel);
147563        while( SQLITE_ROW==sqlite3_step(pRange) ){
147564          sqlite3_bind_int(pUpdate1, 1, iIdx++);
147565          sqlite3_bind_int(pUpdate1, 2, sqlite3_column_int(pRange, 0));
147566          sqlite3_bind_int(pUpdate1, 3, sqlite3_column_int(pRange, 1));
147567          sqlite3_step(pUpdate1);
147568          rc = sqlite3_reset(pUpdate1);
147569          if( rc!=SQLITE_OK ){
147570            sqlite3_reset(pRange);
147571            break;
147572          }
147573        }
147574      }
147575      if( rc==SQLITE_OK ){
147576        rc = sqlite3_reset(pRange);
147577      }
147578
147579      /* Move level -1 to level iAbsLevel */
147580      if( rc==SQLITE_OK ){
147581        sqlite3_bind_int64(pUpdate2, 1, iAbsLevel);
147582        sqlite3_step(pUpdate2);
147583        rc = sqlite3_reset(pUpdate2);
147584      }
147585    }
147586  }
147587
147588
147589  return rc;
147590}
147591
147592/*
147593** Merge all level iLevel segments in the database into a single
147594** iLevel+1 segment. Or, if iLevel<0, merge all segments into a
147595** single segment with a level equal to the numerically largest level
147596** currently present in the database.
147597**
147598** If this function is called with iLevel<0, but there is only one
147599** segment in the database, SQLITE_DONE is returned immediately.
147600** Otherwise, if successful, SQLITE_OK is returned. If an error occurs,
147601** an SQLite error code is returned.
147602*/
147603static int fts3SegmentMerge(
147604  Fts3Table *p,
147605  int iLangid,                    /* Language id to merge */
147606  int iIndex,                     /* Index in p->aIndex[] to merge */
147607  int iLevel                      /* Level to merge */
147608){
147609  int rc;                         /* Return code */
147610  int iIdx = 0;                   /* Index of new segment */
147611  sqlite3_int64 iNewLevel = 0;    /* Level/index to create new segment at */
147612  SegmentWriter *pWriter = 0;     /* Used to write the new, merged, segment */
147613  Fts3SegFilter filter;           /* Segment term filter condition */
147614  Fts3MultiSegReader csr;         /* Cursor to iterate through level(s) */
147615  int bIgnoreEmpty = 0;           /* True to ignore empty segments */
147616  i64 iMaxLevel = 0;              /* Max level number for this index/langid */
147617
147618  assert( iLevel==FTS3_SEGCURSOR_ALL
147619       || iLevel==FTS3_SEGCURSOR_PENDING
147620       || iLevel>=0
147621  );
147622  assert( iLevel<FTS3_SEGDIR_MAXLEVEL );
147623  assert( iIndex>=0 && iIndex<p->nIndex );
147624
147625  rc = sqlite3Fts3SegReaderCursor(p, iLangid, iIndex, iLevel, 0, 0, 1, 0, &csr);
147626  if( rc!=SQLITE_OK || csr.nSegment==0 ) goto finished;
147627
147628  if( iLevel!=FTS3_SEGCURSOR_PENDING ){
147629    rc = fts3SegmentMaxLevel(p, iLangid, iIndex, &iMaxLevel);
147630    if( rc!=SQLITE_OK ) goto finished;
147631  }
147632
147633  if( iLevel==FTS3_SEGCURSOR_ALL ){
147634    /* This call is to merge all segments in the database to a single
147635    ** segment. The level of the new segment is equal to the numerically
147636    ** greatest segment level currently present in the database for this
147637    ** index. The idx of the new segment is always 0.  */
147638    if( csr.nSegment==1 ){
147639      rc = SQLITE_DONE;
147640      goto finished;
147641    }
147642    iNewLevel = iMaxLevel;
147643    bIgnoreEmpty = 1;
147644
147645  }else{
147646    /* This call is to merge all segments at level iLevel. find the next
147647    ** available segment index at level iLevel+1. The call to
147648    ** fts3AllocateSegdirIdx() will merge the segments at level iLevel+1 to
147649    ** a single iLevel+2 segment if necessary.  */
147650    assert( FTS3_SEGCURSOR_PENDING==-1 );
147651    iNewLevel = getAbsoluteLevel(p, iLangid, iIndex, iLevel+1);
147652    rc = fts3AllocateSegdirIdx(p, iLangid, iIndex, iLevel+1, &iIdx);
147653    bIgnoreEmpty = (iLevel!=FTS3_SEGCURSOR_PENDING) && (iNewLevel>iMaxLevel);
147654  }
147655  if( rc!=SQLITE_OK ) goto finished;
147656
147657  assert( csr.nSegment>0 );
147658  assert( iNewLevel>=getAbsoluteLevel(p, iLangid, iIndex, 0) );
147659  assert( iNewLevel<getAbsoluteLevel(p, iLangid, iIndex,FTS3_SEGDIR_MAXLEVEL) );
147660
147661  memset(&filter, 0, sizeof(Fts3SegFilter));
147662  filter.flags = FTS3_SEGMENT_REQUIRE_POS;
147663  filter.flags |= (bIgnoreEmpty ? FTS3_SEGMENT_IGNORE_EMPTY : 0);
147664
147665  rc = sqlite3Fts3SegReaderStart(p, &csr, &filter);
147666  while( SQLITE_OK==rc ){
147667    rc = sqlite3Fts3SegReaderStep(p, &csr);
147668    if( rc!=SQLITE_ROW ) break;
147669    rc = fts3SegWriterAdd(p, &pWriter, 1,
147670        csr.zTerm, csr.nTerm, csr.aDoclist, csr.nDoclist);
147671  }
147672  if( rc!=SQLITE_OK ) goto finished;
147673  assert( pWriter || bIgnoreEmpty );
147674
147675  if( iLevel!=FTS3_SEGCURSOR_PENDING ){
147676    rc = fts3DeleteSegdir(
147677        p, iLangid, iIndex, iLevel, csr.apSegment, csr.nSegment
147678    );
147679    if( rc!=SQLITE_OK ) goto finished;
147680  }
147681  if( pWriter ){
147682    rc = fts3SegWriterFlush(p, pWriter, iNewLevel, iIdx);
147683    if( rc==SQLITE_OK ){
147684      if( iLevel==FTS3_SEGCURSOR_PENDING || iNewLevel<iMaxLevel ){
147685        rc = fts3PromoteSegments(p, iNewLevel, pWriter->nLeafData);
147686      }
147687    }
147688  }
147689
147690 finished:
147691  fts3SegWriterFree(pWriter);
147692  sqlite3Fts3SegReaderFinish(&csr);
147693  return rc;
147694}
147695
147696
147697/*
147698** Flush the contents of pendingTerms to level 0 segments.
147699*/
147700SQLITE_PRIVATE int sqlite3Fts3PendingTermsFlush(Fts3Table *p){
147701  int rc = SQLITE_OK;
147702  int i;
147703
147704  for(i=0; rc==SQLITE_OK && i<p->nIndex; i++){
147705    rc = fts3SegmentMerge(p, p->iPrevLangid, i, FTS3_SEGCURSOR_PENDING);
147706    if( rc==SQLITE_DONE ) rc = SQLITE_OK;
147707  }
147708  sqlite3Fts3PendingTermsClear(p);
147709
147710  /* Determine the auto-incr-merge setting if unknown.  If enabled,
147711  ** estimate the number of leaf blocks of content to be written
147712  */
147713  if( rc==SQLITE_OK && p->bHasStat
147714   && p->nAutoincrmerge==0xff && p->nLeafAdd>0
147715  ){
147716    sqlite3_stmt *pStmt = 0;
147717    rc = fts3SqlStmt(p, SQL_SELECT_STAT, &pStmt, 0);
147718    if( rc==SQLITE_OK ){
147719      sqlite3_bind_int(pStmt, 1, FTS_STAT_AUTOINCRMERGE);
147720      rc = sqlite3_step(pStmt);
147721      if( rc==SQLITE_ROW ){
147722        p->nAutoincrmerge = sqlite3_column_int(pStmt, 0);
147723        if( p->nAutoincrmerge==1 ) p->nAutoincrmerge = 8;
147724      }else if( rc==SQLITE_DONE ){
147725        p->nAutoincrmerge = 0;
147726      }
147727      rc = sqlite3_reset(pStmt);
147728    }
147729  }
147730  return rc;
147731}
147732
147733/*
147734** Encode N integers as varints into a blob.
147735*/
147736static void fts3EncodeIntArray(
147737  int N,             /* The number of integers to encode */
147738  u32 *a,            /* The integer values */
147739  char *zBuf,        /* Write the BLOB here */
147740  int *pNBuf         /* Write number of bytes if zBuf[] used here */
147741){
147742  int i, j;
147743  for(i=j=0; i<N; i++){
147744    j += sqlite3Fts3PutVarint(&zBuf[j], (sqlite3_int64)a[i]);
147745  }
147746  *pNBuf = j;
147747}
147748
147749/*
147750** Decode a blob of varints into N integers
147751*/
147752static void fts3DecodeIntArray(
147753  int N,             /* The number of integers to decode */
147754  u32 *a,            /* Write the integer values */
147755  const char *zBuf,  /* The BLOB containing the varints */
147756  int nBuf           /* size of the BLOB */
147757){
147758  int i, j;
147759  UNUSED_PARAMETER(nBuf);
147760  for(i=j=0; i<N; i++){
147761    sqlite3_int64 x;
147762    j += sqlite3Fts3GetVarint(&zBuf[j], &x);
147763    assert(j<=nBuf);
147764    a[i] = (u32)(x & 0xffffffff);
147765  }
147766}
147767
147768/*
147769** Insert the sizes (in tokens) for each column of the document
147770** with docid equal to p->iPrevDocid.  The sizes are encoded as
147771** a blob of varints.
147772*/
147773static void fts3InsertDocsize(
147774  int *pRC,                       /* Result code */
147775  Fts3Table *p,                   /* Table into which to insert */
147776  u32 *aSz                        /* Sizes of each column, in tokens */
147777){
147778  char *pBlob;             /* The BLOB encoding of the document size */
147779  int nBlob;               /* Number of bytes in the BLOB */
147780  sqlite3_stmt *pStmt;     /* Statement used to insert the encoding */
147781  int rc;                  /* Result code from subfunctions */
147782
147783  if( *pRC ) return;
147784  pBlob = sqlite3_malloc( 10*p->nColumn );
147785  if( pBlob==0 ){
147786    *pRC = SQLITE_NOMEM;
147787    return;
147788  }
147789  fts3EncodeIntArray(p->nColumn, aSz, pBlob, &nBlob);
147790  rc = fts3SqlStmt(p, SQL_REPLACE_DOCSIZE, &pStmt, 0);
147791  if( rc ){
147792    sqlite3_free(pBlob);
147793    *pRC = rc;
147794    return;
147795  }
147796  sqlite3_bind_int64(pStmt, 1, p->iPrevDocid);
147797  sqlite3_bind_blob(pStmt, 2, pBlob, nBlob, sqlite3_free);
147798  sqlite3_step(pStmt);
147799  *pRC = sqlite3_reset(pStmt);
147800}
147801
147802/*
147803** Record 0 of the %_stat table contains a blob consisting of N varints,
147804** where N is the number of user defined columns in the fts3 table plus
147805** two. If nCol is the number of user defined columns, then values of the
147806** varints are set as follows:
147807**
147808**   Varint 0:       Total number of rows in the table.
147809**
147810**   Varint 1..nCol: For each column, the total number of tokens stored in
147811**                   the column for all rows of the table.
147812**
147813**   Varint 1+nCol:  The total size, in bytes, of all text values in all
147814**                   columns of all rows of the table.
147815**
147816*/
147817static void fts3UpdateDocTotals(
147818  int *pRC,                       /* The result code */
147819  Fts3Table *p,                   /* Table being updated */
147820  u32 *aSzIns,                    /* Size increases */
147821  u32 *aSzDel,                    /* Size decreases */
147822  int nChng                       /* Change in the number of documents */
147823){
147824  char *pBlob;             /* Storage for BLOB written into %_stat */
147825  int nBlob;               /* Size of BLOB written into %_stat */
147826  u32 *a;                  /* Array of integers that becomes the BLOB */
147827  sqlite3_stmt *pStmt;     /* Statement for reading and writing */
147828  int i;                   /* Loop counter */
147829  int rc;                  /* Result code from subfunctions */
147830
147831  const int nStat = p->nColumn+2;
147832
147833  if( *pRC ) return;
147834  a = sqlite3_malloc( (sizeof(u32)+10)*nStat );
147835  if( a==0 ){
147836    *pRC = SQLITE_NOMEM;
147837    return;
147838  }
147839  pBlob = (char*)&a[nStat];
147840  rc = fts3SqlStmt(p, SQL_SELECT_STAT, &pStmt, 0);
147841  if( rc ){
147842    sqlite3_free(a);
147843    *pRC = rc;
147844    return;
147845  }
147846  sqlite3_bind_int(pStmt, 1, FTS_STAT_DOCTOTAL);
147847  if( sqlite3_step(pStmt)==SQLITE_ROW ){
147848    fts3DecodeIntArray(nStat, a,
147849         sqlite3_column_blob(pStmt, 0),
147850         sqlite3_column_bytes(pStmt, 0));
147851  }else{
147852    memset(a, 0, sizeof(u32)*(nStat) );
147853  }
147854  rc = sqlite3_reset(pStmt);
147855  if( rc!=SQLITE_OK ){
147856    sqlite3_free(a);
147857    *pRC = rc;
147858    return;
147859  }
147860  if( nChng<0 && a[0]<(u32)(-nChng) ){
147861    a[0] = 0;
147862  }else{
147863    a[0] += nChng;
147864  }
147865  for(i=0; i<p->nColumn+1; i++){
147866    u32 x = a[i+1];
147867    if( x+aSzIns[i] < aSzDel[i] ){
147868      x = 0;
147869    }else{
147870      x = x + aSzIns[i] - aSzDel[i];
147871    }
147872    a[i+1] = x;
147873  }
147874  fts3EncodeIntArray(nStat, a, pBlob, &nBlob);
147875  rc = fts3SqlStmt(p, SQL_REPLACE_STAT, &pStmt, 0);
147876  if( rc ){
147877    sqlite3_free(a);
147878    *pRC = rc;
147879    return;
147880  }
147881  sqlite3_bind_int(pStmt, 1, FTS_STAT_DOCTOTAL);
147882  sqlite3_bind_blob(pStmt, 2, pBlob, nBlob, SQLITE_STATIC);
147883  sqlite3_step(pStmt);
147884  *pRC = sqlite3_reset(pStmt);
147885  sqlite3_free(a);
147886}
147887
147888/*
147889** Merge the entire database so that there is one segment for each
147890** iIndex/iLangid combination.
147891*/
147892static int fts3DoOptimize(Fts3Table *p, int bReturnDone){
147893  int bSeenDone = 0;
147894  int rc;
147895  sqlite3_stmt *pAllLangid = 0;
147896
147897  rc = fts3SqlStmt(p, SQL_SELECT_ALL_LANGID, &pAllLangid, 0);
147898  if( rc==SQLITE_OK ){
147899    int rc2;
147900    sqlite3_bind_int(pAllLangid, 1, p->iPrevLangid);
147901    sqlite3_bind_int(pAllLangid, 2, p->nIndex);
147902    while( sqlite3_step(pAllLangid)==SQLITE_ROW ){
147903      int i;
147904      int iLangid = sqlite3_column_int(pAllLangid, 0);
147905      for(i=0; rc==SQLITE_OK && i<p->nIndex; i++){
147906        rc = fts3SegmentMerge(p, iLangid, i, FTS3_SEGCURSOR_ALL);
147907        if( rc==SQLITE_DONE ){
147908          bSeenDone = 1;
147909          rc = SQLITE_OK;
147910        }
147911      }
147912    }
147913    rc2 = sqlite3_reset(pAllLangid);
147914    if( rc==SQLITE_OK ) rc = rc2;
147915  }
147916
147917  sqlite3Fts3SegmentsClose(p);
147918  sqlite3Fts3PendingTermsClear(p);
147919
147920  return (rc==SQLITE_OK && bReturnDone && bSeenDone) ? SQLITE_DONE : rc;
147921}
147922
147923/*
147924** This function is called when the user executes the following statement:
147925**
147926**     INSERT INTO <tbl>(<tbl>) VALUES('rebuild');
147927**
147928** The entire FTS index is discarded and rebuilt. If the table is one
147929** created using the content=xxx option, then the new index is based on
147930** the current contents of the xxx table. Otherwise, it is rebuilt based
147931** on the contents of the %_content table.
147932*/
147933static int fts3DoRebuild(Fts3Table *p){
147934  int rc;                         /* Return Code */
147935
147936  rc = fts3DeleteAll(p, 0);
147937  if( rc==SQLITE_OK ){
147938    u32 *aSz = 0;
147939    u32 *aSzIns = 0;
147940    u32 *aSzDel = 0;
147941    sqlite3_stmt *pStmt = 0;
147942    int nEntry = 0;
147943
147944    /* Compose and prepare an SQL statement to loop through the content table */
147945    char *zSql = sqlite3_mprintf("SELECT %s" , p->zReadExprlist);
147946    if( !zSql ){
147947      rc = SQLITE_NOMEM;
147948    }else{
147949      rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
147950      sqlite3_free(zSql);
147951    }
147952
147953    if( rc==SQLITE_OK ){
147954      int nByte = sizeof(u32) * (p->nColumn+1)*3;
147955      aSz = (u32 *)sqlite3_malloc(nByte);
147956      if( aSz==0 ){
147957        rc = SQLITE_NOMEM;
147958      }else{
147959        memset(aSz, 0, nByte);
147960        aSzIns = &aSz[p->nColumn+1];
147961        aSzDel = &aSzIns[p->nColumn+1];
147962      }
147963    }
147964
147965    while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
147966      int iCol;
147967      int iLangid = langidFromSelect(p, pStmt);
147968      rc = fts3PendingTermsDocid(p, iLangid, sqlite3_column_int64(pStmt, 0));
147969      memset(aSz, 0, sizeof(aSz[0]) * (p->nColumn+1));
147970      for(iCol=0; rc==SQLITE_OK && iCol<p->nColumn; iCol++){
147971        if( p->abNotindexed[iCol]==0 ){
147972          const char *z = (const char *) sqlite3_column_text(pStmt, iCol+1);
147973          rc = fts3PendingTermsAdd(p, iLangid, z, iCol, &aSz[iCol]);
147974          aSz[p->nColumn] += sqlite3_column_bytes(pStmt, iCol+1);
147975        }
147976      }
147977      if( p->bHasDocsize ){
147978        fts3InsertDocsize(&rc, p, aSz);
147979      }
147980      if( rc!=SQLITE_OK ){
147981        sqlite3_finalize(pStmt);
147982        pStmt = 0;
147983      }else{
147984        nEntry++;
147985        for(iCol=0; iCol<=p->nColumn; iCol++){
147986          aSzIns[iCol] += aSz[iCol];
147987        }
147988      }
147989    }
147990    if( p->bFts4 ){
147991      fts3UpdateDocTotals(&rc, p, aSzIns, aSzDel, nEntry);
147992    }
147993    sqlite3_free(aSz);
147994
147995    if( pStmt ){
147996      int rc2 = sqlite3_finalize(pStmt);
147997      if( rc==SQLITE_OK ){
147998        rc = rc2;
147999      }
148000    }
148001  }
148002
148003  return rc;
148004}
148005
148006
148007/*
148008** This function opens a cursor used to read the input data for an
148009** incremental merge operation. Specifically, it opens a cursor to scan
148010** the oldest nSeg segments (idx=0 through idx=(nSeg-1)) in absolute
148011** level iAbsLevel.
148012*/
148013static int fts3IncrmergeCsr(
148014  Fts3Table *p,                   /* FTS3 table handle */
148015  sqlite3_int64 iAbsLevel,        /* Absolute level to open */
148016  int nSeg,                       /* Number of segments to merge */
148017  Fts3MultiSegReader *pCsr        /* Cursor object to populate */
148018){
148019  int rc;                         /* Return Code */
148020  sqlite3_stmt *pStmt = 0;        /* Statement used to read %_segdir entry */
148021  int nByte;                      /* Bytes allocated at pCsr->apSegment[] */
148022
148023  /* Allocate space for the Fts3MultiSegReader.aCsr[] array */
148024  memset(pCsr, 0, sizeof(*pCsr));
148025  nByte = sizeof(Fts3SegReader *) * nSeg;
148026  pCsr->apSegment = (Fts3SegReader **)sqlite3_malloc(nByte);
148027
148028  if( pCsr->apSegment==0 ){
148029    rc = SQLITE_NOMEM;
148030  }else{
148031    memset(pCsr->apSegment, 0, nByte);
148032    rc = fts3SqlStmt(p, SQL_SELECT_LEVEL, &pStmt, 0);
148033  }
148034  if( rc==SQLITE_OK ){
148035    int i;
148036    int rc2;
148037    sqlite3_bind_int64(pStmt, 1, iAbsLevel);
148038    assert( pCsr->nSegment==0 );
148039    for(i=0; rc==SQLITE_OK && sqlite3_step(pStmt)==SQLITE_ROW && i<nSeg; i++){
148040      rc = sqlite3Fts3SegReaderNew(i, 0,
148041          sqlite3_column_int64(pStmt, 1),        /* segdir.start_block */
148042          sqlite3_column_int64(pStmt, 2),        /* segdir.leaves_end_block */
148043          sqlite3_column_int64(pStmt, 3),        /* segdir.end_block */
148044          sqlite3_column_blob(pStmt, 4),         /* segdir.root */
148045          sqlite3_column_bytes(pStmt, 4),        /* segdir.root */
148046          &pCsr->apSegment[i]
148047      );
148048      pCsr->nSegment++;
148049    }
148050    rc2 = sqlite3_reset(pStmt);
148051    if( rc==SQLITE_OK ) rc = rc2;
148052  }
148053
148054  return rc;
148055}
148056
148057typedef struct IncrmergeWriter IncrmergeWriter;
148058typedef struct NodeWriter NodeWriter;
148059typedef struct Blob Blob;
148060typedef struct NodeReader NodeReader;
148061
148062/*
148063** An instance of the following structure is used as a dynamic buffer
148064** to build up nodes or other blobs of data in.
148065**
148066** The function blobGrowBuffer() is used to extend the allocation.
148067*/
148068struct Blob {
148069  char *a;                        /* Pointer to allocation */
148070  int n;                          /* Number of valid bytes of data in a[] */
148071  int nAlloc;                     /* Allocated size of a[] (nAlloc>=n) */
148072};
148073
148074/*
148075** This structure is used to build up buffers containing segment b-tree
148076** nodes (blocks).
148077*/
148078struct NodeWriter {
148079  sqlite3_int64 iBlock;           /* Current block id */
148080  Blob key;                       /* Last key written to the current block */
148081  Blob block;                     /* Current block image */
148082};
148083
148084/*
148085** An object of this type contains the state required to create or append
148086** to an appendable b-tree segment.
148087*/
148088struct IncrmergeWriter {
148089  int nLeafEst;                   /* Space allocated for leaf blocks */
148090  int nWork;                      /* Number of leaf pages flushed */
148091  sqlite3_int64 iAbsLevel;        /* Absolute level of input segments */
148092  int iIdx;                       /* Index of *output* segment in iAbsLevel+1 */
148093  sqlite3_int64 iStart;           /* Block number of first allocated block */
148094  sqlite3_int64 iEnd;             /* Block number of last allocated block */
148095  sqlite3_int64 nLeafData;        /* Bytes of leaf page data so far */
148096  u8 bNoLeafData;                 /* If true, store 0 for segment size */
148097  NodeWriter aNodeWriter[FTS_MAX_APPENDABLE_HEIGHT];
148098};
148099
148100/*
148101** An object of the following type is used to read data from a single
148102** FTS segment node. See the following functions:
148103**
148104**     nodeReaderInit()
148105**     nodeReaderNext()
148106**     nodeReaderRelease()
148107*/
148108struct NodeReader {
148109  const char *aNode;
148110  int nNode;
148111  int iOff;                       /* Current offset within aNode[] */
148112
148113  /* Output variables. Containing the current node entry. */
148114  sqlite3_int64 iChild;           /* Pointer to child node */
148115  Blob term;                      /* Current term */
148116  const char *aDoclist;           /* Pointer to doclist */
148117  int nDoclist;                   /* Size of doclist in bytes */
148118};
148119
148120/*
148121** If *pRc is not SQLITE_OK when this function is called, it is a no-op.
148122** Otherwise, if the allocation at pBlob->a is not already at least nMin
148123** bytes in size, extend (realloc) it to be so.
148124**
148125** If an OOM error occurs, set *pRc to SQLITE_NOMEM and leave pBlob->a
148126** unmodified. Otherwise, if the allocation succeeds, update pBlob->nAlloc
148127** to reflect the new size of the pBlob->a[] buffer.
148128*/
148129static void blobGrowBuffer(Blob *pBlob, int nMin, int *pRc){
148130  if( *pRc==SQLITE_OK && nMin>pBlob->nAlloc ){
148131    int nAlloc = nMin;
148132    char *a = (char *)sqlite3_realloc(pBlob->a, nAlloc);
148133    if( a ){
148134      pBlob->nAlloc = nAlloc;
148135      pBlob->a = a;
148136    }else{
148137      *pRc = SQLITE_NOMEM;
148138    }
148139  }
148140}
148141
148142/*
148143** Attempt to advance the node-reader object passed as the first argument to
148144** the next entry on the node.
148145**
148146** Return an error code if an error occurs (SQLITE_NOMEM is possible).
148147** Otherwise return SQLITE_OK. If there is no next entry on the node
148148** (e.g. because the current entry is the last) set NodeReader->aNode to
148149** NULL to indicate EOF. Otherwise, populate the NodeReader structure output
148150** variables for the new entry.
148151*/
148152static int nodeReaderNext(NodeReader *p){
148153  int bFirst = (p->term.n==0);    /* True for first term on the node */
148154  int nPrefix = 0;                /* Bytes to copy from previous term */
148155  int nSuffix = 0;                /* Bytes to append to the prefix */
148156  int rc = SQLITE_OK;             /* Return code */
148157
148158  assert( p->aNode );
148159  if( p->iChild && bFirst==0 ) p->iChild++;
148160  if( p->iOff>=p->nNode ){
148161    /* EOF */
148162    p->aNode = 0;
148163  }else{
148164    if( bFirst==0 ){
148165      p->iOff += fts3GetVarint32(&p->aNode[p->iOff], &nPrefix);
148166    }
148167    p->iOff += fts3GetVarint32(&p->aNode[p->iOff], &nSuffix);
148168
148169    blobGrowBuffer(&p->term, nPrefix+nSuffix, &rc);
148170    if( rc==SQLITE_OK ){
148171      memcpy(&p->term.a[nPrefix], &p->aNode[p->iOff], nSuffix);
148172      p->term.n = nPrefix+nSuffix;
148173      p->iOff += nSuffix;
148174      if( p->iChild==0 ){
148175        p->iOff += fts3GetVarint32(&p->aNode[p->iOff], &p->nDoclist);
148176        p->aDoclist = &p->aNode[p->iOff];
148177        p->iOff += p->nDoclist;
148178      }
148179    }
148180  }
148181
148182  assert( p->iOff<=p->nNode );
148183
148184  return rc;
148185}
148186
148187/*
148188** Release all dynamic resources held by node-reader object *p.
148189*/
148190static void nodeReaderRelease(NodeReader *p){
148191  sqlite3_free(p->term.a);
148192}
148193
148194/*
148195** Initialize a node-reader object to read the node in buffer aNode/nNode.
148196**
148197** If successful, SQLITE_OK is returned and the NodeReader object set to
148198** point to the first entry on the node (if any). Otherwise, an SQLite
148199** error code is returned.
148200*/
148201static int nodeReaderInit(NodeReader *p, const char *aNode, int nNode){
148202  memset(p, 0, sizeof(NodeReader));
148203  p->aNode = aNode;
148204  p->nNode = nNode;
148205
148206  /* Figure out if this is a leaf or an internal node. */
148207  if( p->aNode[0] ){
148208    /* An internal node. */
148209    p->iOff = 1 + sqlite3Fts3GetVarint(&p->aNode[1], &p->iChild);
148210  }else{
148211    p->iOff = 1;
148212  }
148213
148214  return nodeReaderNext(p);
148215}
148216
148217/*
148218** This function is called while writing an FTS segment each time a leaf o
148219** node is finished and written to disk. The key (zTerm/nTerm) is guaranteed
148220** to be greater than the largest key on the node just written, but smaller
148221** than or equal to the first key that will be written to the next leaf
148222** node.
148223**
148224** The block id of the leaf node just written to disk may be found in
148225** (pWriter->aNodeWriter[0].iBlock) when this function is called.
148226*/
148227static int fts3IncrmergePush(
148228  Fts3Table *p,                   /* Fts3 table handle */
148229  IncrmergeWriter *pWriter,       /* Writer object */
148230  const char *zTerm,              /* Term to write to internal node */
148231  int nTerm                       /* Bytes at zTerm */
148232){
148233  sqlite3_int64 iPtr = pWriter->aNodeWriter[0].iBlock;
148234  int iLayer;
148235
148236  assert( nTerm>0 );
148237  for(iLayer=1; ALWAYS(iLayer<FTS_MAX_APPENDABLE_HEIGHT); iLayer++){
148238    sqlite3_int64 iNextPtr = 0;
148239    NodeWriter *pNode = &pWriter->aNodeWriter[iLayer];
148240    int rc = SQLITE_OK;
148241    int nPrefix;
148242    int nSuffix;
148243    int nSpace;
148244
148245    /* Figure out how much space the key will consume if it is written to
148246    ** the current node of layer iLayer. Due to the prefix compression,
148247    ** the space required changes depending on which node the key is to
148248    ** be added to.  */
148249    nPrefix = fts3PrefixCompress(pNode->key.a, pNode->key.n, zTerm, nTerm);
148250    nSuffix = nTerm - nPrefix;
148251    nSpace  = sqlite3Fts3VarintLen(nPrefix);
148252    nSpace += sqlite3Fts3VarintLen(nSuffix) + nSuffix;
148253
148254    if( pNode->key.n==0 || (pNode->block.n + nSpace)<=p->nNodeSize ){
148255      /* If the current node of layer iLayer contains zero keys, or if adding
148256      ** the key to it will not cause it to grow to larger than nNodeSize
148257      ** bytes in size, write the key here.  */
148258
148259      Blob *pBlk = &pNode->block;
148260      if( pBlk->n==0 ){
148261        blobGrowBuffer(pBlk, p->nNodeSize, &rc);
148262        if( rc==SQLITE_OK ){
148263          pBlk->a[0] = (char)iLayer;
148264          pBlk->n = 1 + sqlite3Fts3PutVarint(&pBlk->a[1], iPtr);
148265        }
148266      }
148267      blobGrowBuffer(pBlk, pBlk->n + nSpace, &rc);
148268      blobGrowBuffer(&pNode->key, nTerm, &rc);
148269
148270      if( rc==SQLITE_OK ){
148271        if( pNode->key.n ){
148272          pBlk->n += sqlite3Fts3PutVarint(&pBlk->a[pBlk->n], nPrefix);
148273        }
148274        pBlk->n += sqlite3Fts3PutVarint(&pBlk->a[pBlk->n], nSuffix);
148275        memcpy(&pBlk->a[pBlk->n], &zTerm[nPrefix], nSuffix);
148276        pBlk->n += nSuffix;
148277
148278        memcpy(pNode->key.a, zTerm, nTerm);
148279        pNode->key.n = nTerm;
148280      }
148281    }else{
148282      /* Otherwise, flush the current node of layer iLayer to disk.
148283      ** Then allocate a new, empty sibling node. The key will be written
148284      ** into the parent of this node. */
148285      rc = fts3WriteSegment(p, pNode->iBlock, pNode->block.a, pNode->block.n);
148286
148287      assert( pNode->block.nAlloc>=p->nNodeSize );
148288      pNode->block.a[0] = (char)iLayer;
148289      pNode->block.n = 1 + sqlite3Fts3PutVarint(&pNode->block.a[1], iPtr+1);
148290
148291      iNextPtr = pNode->iBlock;
148292      pNode->iBlock++;
148293      pNode->key.n = 0;
148294    }
148295
148296    if( rc!=SQLITE_OK || iNextPtr==0 ) return rc;
148297    iPtr = iNextPtr;
148298  }
148299
148300  assert( 0 );
148301  return 0;
148302}
148303
148304/*
148305** Append a term and (optionally) doclist to the FTS segment node currently
148306** stored in blob *pNode. The node need not contain any terms, but the
148307** header must be written before this function is called.
148308**
148309** A node header is a single 0x00 byte for a leaf node, or a height varint
148310** followed by the left-hand-child varint for an internal node.
148311**
148312** The term to be appended is passed via arguments zTerm/nTerm. For a
148313** leaf node, the doclist is passed as aDoclist/nDoclist. For an internal
148314** node, both aDoclist and nDoclist must be passed 0.
148315**
148316** If the size of the value in blob pPrev is zero, then this is the first
148317** term written to the node. Otherwise, pPrev contains a copy of the
148318** previous term. Before this function returns, it is updated to contain a
148319** copy of zTerm/nTerm.
148320**
148321** It is assumed that the buffer associated with pNode is already large
148322** enough to accommodate the new entry. The buffer associated with pPrev
148323** is extended by this function if requrired.
148324**
148325** If an error (i.e. OOM condition) occurs, an SQLite error code is
148326** returned. Otherwise, SQLITE_OK.
148327*/
148328static int fts3AppendToNode(
148329  Blob *pNode,                    /* Current node image to append to */
148330  Blob *pPrev,                    /* Buffer containing previous term written */
148331  const char *zTerm,              /* New term to write */
148332  int nTerm,                      /* Size of zTerm in bytes */
148333  const char *aDoclist,           /* Doclist (or NULL) to write */
148334  int nDoclist                    /* Size of aDoclist in bytes */
148335){
148336  int rc = SQLITE_OK;             /* Return code */
148337  int bFirst = (pPrev->n==0);     /* True if this is the first term written */
148338  int nPrefix;                    /* Size of term prefix in bytes */
148339  int nSuffix;                    /* Size of term suffix in bytes */
148340
148341  /* Node must have already been started. There must be a doclist for a
148342  ** leaf node, and there must not be a doclist for an internal node.  */
148343  assert( pNode->n>0 );
148344  assert( (pNode->a[0]=='\0')==(aDoclist!=0) );
148345
148346  blobGrowBuffer(pPrev, nTerm, &rc);
148347  if( rc!=SQLITE_OK ) return rc;
148348
148349  nPrefix = fts3PrefixCompress(pPrev->a, pPrev->n, zTerm, nTerm);
148350  nSuffix = nTerm - nPrefix;
148351  memcpy(pPrev->a, zTerm, nTerm);
148352  pPrev->n = nTerm;
148353
148354  if( bFirst==0 ){
148355    pNode->n += sqlite3Fts3PutVarint(&pNode->a[pNode->n], nPrefix);
148356  }
148357  pNode->n += sqlite3Fts3PutVarint(&pNode->a[pNode->n], nSuffix);
148358  memcpy(&pNode->a[pNode->n], &zTerm[nPrefix], nSuffix);
148359  pNode->n += nSuffix;
148360
148361  if( aDoclist ){
148362    pNode->n += sqlite3Fts3PutVarint(&pNode->a[pNode->n], nDoclist);
148363    memcpy(&pNode->a[pNode->n], aDoclist, nDoclist);
148364    pNode->n += nDoclist;
148365  }
148366
148367  assert( pNode->n<=pNode->nAlloc );
148368
148369  return SQLITE_OK;
148370}
148371
148372/*
148373** Append the current term and doclist pointed to by cursor pCsr to the
148374** appendable b-tree segment opened for writing by pWriter.
148375**
148376** Return SQLITE_OK if successful, or an SQLite error code otherwise.
148377*/
148378static int fts3IncrmergeAppend(
148379  Fts3Table *p,                   /* Fts3 table handle */
148380  IncrmergeWriter *pWriter,       /* Writer object */
148381  Fts3MultiSegReader *pCsr        /* Cursor containing term and doclist */
148382){
148383  const char *zTerm = pCsr->zTerm;
148384  int nTerm = pCsr->nTerm;
148385  const char *aDoclist = pCsr->aDoclist;
148386  int nDoclist = pCsr->nDoclist;
148387  int rc = SQLITE_OK;           /* Return code */
148388  int nSpace;                   /* Total space in bytes required on leaf */
148389  int nPrefix;                  /* Size of prefix shared with previous term */
148390  int nSuffix;                  /* Size of suffix (nTerm - nPrefix) */
148391  NodeWriter *pLeaf;            /* Object used to write leaf nodes */
148392
148393  pLeaf = &pWriter->aNodeWriter[0];
148394  nPrefix = fts3PrefixCompress(pLeaf->key.a, pLeaf->key.n, zTerm, nTerm);
148395  nSuffix = nTerm - nPrefix;
148396
148397  nSpace  = sqlite3Fts3VarintLen(nPrefix);
148398  nSpace += sqlite3Fts3VarintLen(nSuffix) + nSuffix;
148399  nSpace += sqlite3Fts3VarintLen(nDoclist) + nDoclist;
148400
148401  /* If the current block is not empty, and if adding this term/doclist
148402  ** to the current block would make it larger than Fts3Table.nNodeSize
148403  ** bytes, write this block out to the database. */
148404  if( pLeaf->block.n>0 && (pLeaf->block.n + nSpace)>p->nNodeSize ){
148405    rc = fts3WriteSegment(p, pLeaf->iBlock, pLeaf->block.a, pLeaf->block.n);
148406    pWriter->nWork++;
148407
148408    /* Add the current term to the parent node. The term added to the
148409    ** parent must:
148410    **
148411    **   a) be greater than the largest term on the leaf node just written
148412    **      to the database (still available in pLeaf->key), and
148413    **
148414    **   b) be less than or equal to the term about to be added to the new
148415    **      leaf node (zTerm/nTerm).
148416    **
148417    ** In other words, it must be the prefix of zTerm 1 byte longer than
148418    ** the common prefix (if any) of zTerm and pWriter->zTerm.
148419    */
148420    if( rc==SQLITE_OK ){
148421      rc = fts3IncrmergePush(p, pWriter, zTerm, nPrefix+1);
148422    }
148423
148424    /* Advance to the next output block */
148425    pLeaf->iBlock++;
148426    pLeaf->key.n = 0;
148427    pLeaf->block.n = 0;
148428
148429    nSuffix = nTerm;
148430    nSpace  = 1;
148431    nSpace += sqlite3Fts3VarintLen(nSuffix) + nSuffix;
148432    nSpace += sqlite3Fts3VarintLen(nDoclist) + nDoclist;
148433  }
148434
148435  pWriter->nLeafData += nSpace;
148436  blobGrowBuffer(&pLeaf->block, pLeaf->block.n + nSpace, &rc);
148437  if( rc==SQLITE_OK ){
148438    if( pLeaf->block.n==0 ){
148439      pLeaf->block.n = 1;
148440      pLeaf->block.a[0] = '\0';
148441    }
148442    rc = fts3AppendToNode(
148443        &pLeaf->block, &pLeaf->key, zTerm, nTerm, aDoclist, nDoclist
148444    );
148445  }
148446
148447  return rc;
148448}
148449
148450/*
148451** This function is called to release all dynamic resources held by the
148452** merge-writer object pWriter, and if no error has occurred, to flush
148453** all outstanding node buffers held by pWriter to disk.
148454**
148455** If *pRc is not SQLITE_OK when this function is called, then no attempt
148456** is made to write any data to disk. Instead, this function serves only
148457** to release outstanding resources.
148458**
148459** Otherwise, if *pRc is initially SQLITE_OK and an error occurs while
148460** flushing buffers to disk, *pRc is set to an SQLite error code before
148461** returning.
148462*/
148463static void fts3IncrmergeRelease(
148464  Fts3Table *p,                   /* FTS3 table handle */
148465  IncrmergeWriter *pWriter,       /* Merge-writer object */
148466  int *pRc                        /* IN/OUT: Error code */
148467){
148468  int i;                          /* Used to iterate through non-root layers */
148469  int iRoot;                      /* Index of root in pWriter->aNodeWriter */
148470  NodeWriter *pRoot;              /* NodeWriter for root node */
148471  int rc = *pRc;                  /* Error code */
148472
148473  /* Set iRoot to the index in pWriter->aNodeWriter[] of the output segment
148474  ** root node. If the segment fits entirely on a single leaf node, iRoot
148475  ** will be set to 0. If the root node is the parent of the leaves, iRoot
148476  ** will be 1. And so on.  */
148477  for(iRoot=FTS_MAX_APPENDABLE_HEIGHT-1; iRoot>=0; iRoot--){
148478    NodeWriter *pNode = &pWriter->aNodeWriter[iRoot];
148479    if( pNode->block.n>0 ) break;
148480    assert( *pRc || pNode->block.nAlloc==0 );
148481    assert( *pRc || pNode->key.nAlloc==0 );
148482    sqlite3_free(pNode->block.a);
148483    sqlite3_free(pNode->key.a);
148484  }
148485
148486  /* Empty output segment. This is a no-op. */
148487  if( iRoot<0 ) return;
148488
148489  /* The entire output segment fits on a single node. Normally, this means
148490  ** the node would be stored as a blob in the "root" column of the %_segdir
148491  ** table. However, this is not permitted in this case. The problem is that
148492  ** space has already been reserved in the %_segments table, and so the
148493  ** start_block and end_block fields of the %_segdir table must be populated.
148494  ** And, by design or by accident, released versions of FTS cannot handle
148495  ** segments that fit entirely on the root node with start_block!=0.
148496  **
148497  ** Instead, create a synthetic root node that contains nothing but a
148498  ** pointer to the single content node. So that the segment consists of a
148499  ** single leaf and a single interior (root) node.
148500  **
148501  ** Todo: Better might be to defer allocating space in the %_segments
148502  ** table until we are sure it is needed.
148503  */
148504  if( iRoot==0 ){
148505    Blob *pBlock = &pWriter->aNodeWriter[1].block;
148506    blobGrowBuffer(pBlock, 1 + FTS3_VARINT_MAX, &rc);
148507    if( rc==SQLITE_OK ){
148508      pBlock->a[0] = 0x01;
148509      pBlock->n = 1 + sqlite3Fts3PutVarint(
148510          &pBlock->a[1], pWriter->aNodeWriter[0].iBlock
148511      );
148512    }
148513    iRoot = 1;
148514  }
148515  pRoot = &pWriter->aNodeWriter[iRoot];
148516
148517  /* Flush all currently outstanding nodes to disk. */
148518  for(i=0; i<iRoot; i++){
148519    NodeWriter *pNode = &pWriter->aNodeWriter[i];
148520    if( pNode->block.n>0 && rc==SQLITE_OK ){
148521      rc = fts3WriteSegment(p, pNode->iBlock, pNode->block.a, pNode->block.n);
148522    }
148523    sqlite3_free(pNode->block.a);
148524    sqlite3_free(pNode->key.a);
148525  }
148526
148527  /* Write the %_segdir record. */
148528  if( rc==SQLITE_OK ){
148529    rc = fts3WriteSegdir(p,
148530        pWriter->iAbsLevel+1,               /* level */
148531        pWriter->iIdx,                      /* idx */
148532        pWriter->iStart,                    /* start_block */
148533        pWriter->aNodeWriter[0].iBlock,     /* leaves_end_block */
148534        pWriter->iEnd,                      /* end_block */
148535        (pWriter->bNoLeafData==0 ? pWriter->nLeafData : 0),   /* end_block */
148536        pRoot->block.a, pRoot->block.n      /* root */
148537    );
148538  }
148539  sqlite3_free(pRoot->block.a);
148540  sqlite3_free(pRoot->key.a);
148541
148542  *pRc = rc;
148543}
148544
148545/*
148546** Compare the term in buffer zLhs (size in bytes nLhs) with that in
148547** zRhs (size in bytes nRhs) using memcmp. If one term is a prefix of
148548** the other, it is considered to be smaller than the other.
148549**
148550** Return -ve if zLhs is smaller than zRhs, 0 if it is equal, or +ve
148551** if it is greater.
148552*/
148553static int fts3TermCmp(
148554  const char *zLhs, int nLhs,     /* LHS of comparison */
148555  const char *zRhs, int nRhs      /* RHS of comparison */
148556){
148557  int nCmp = MIN(nLhs, nRhs);
148558  int res;
148559
148560  res = memcmp(zLhs, zRhs, nCmp);
148561  if( res==0 ) res = nLhs - nRhs;
148562
148563  return res;
148564}
148565
148566
148567/*
148568** Query to see if the entry in the %_segments table with blockid iEnd is
148569** NULL. If no error occurs and the entry is NULL, set *pbRes 1 before
148570** returning. Otherwise, set *pbRes to 0.
148571**
148572** Or, if an error occurs while querying the database, return an SQLite
148573** error code. The final value of *pbRes is undefined in this case.
148574**
148575** This is used to test if a segment is an "appendable" segment. If it
148576** is, then a NULL entry has been inserted into the %_segments table
148577** with blockid %_segdir.end_block.
148578*/
148579static int fts3IsAppendable(Fts3Table *p, sqlite3_int64 iEnd, int *pbRes){
148580  int bRes = 0;                   /* Result to set *pbRes to */
148581  sqlite3_stmt *pCheck = 0;       /* Statement to query database with */
148582  int rc;                         /* Return code */
148583
148584  rc = fts3SqlStmt(p, SQL_SEGMENT_IS_APPENDABLE, &pCheck, 0);
148585  if( rc==SQLITE_OK ){
148586    sqlite3_bind_int64(pCheck, 1, iEnd);
148587    if( SQLITE_ROW==sqlite3_step(pCheck) ) bRes = 1;
148588    rc = sqlite3_reset(pCheck);
148589  }
148590
148591  *pbRes = bRes;
148592  return rc;
148593}
148594
148595/*
148596** This function is called when initializing an incremental-merge operation.
148597** It checks if the existing segment with index value iIdx at absolute level
148598** (iAbsLevel+1) can be appended to by the incremental merge. If it can, the
148599** merge-writer object *pWriter is initialized to write to it.
148600**
148601** An existing segment can be appended to by an incremental merge if:
148602**
148603**   * It was initially created as an appendable segment (with all required
148604**     space pre-allocated), and
148605**
148606**   * The first key read from the input (arguments zKey and nKey) is
148607**     greater than the largest key currently stored in the potential
148608**     output segment.
148609*/
148610static int fts3IncrmergeLoad(
148611  Fts3Table *p,                   /* Fts3 table handle */
148612  sqlite3_int64 iAbsLevel,        /* Absolute level of input segments */
148613  int iIdx,                       /* Index of candidate output segment */
148614  const char *zKey,               /* First key to write */
148615  int nKey,                       /* Number of bytes in nKey */
148616  IncrmergeWriter *pWriter        /* Populate this object */
148617){
148618  int rc;                         /* Return code */
148619  sqlite3_stmt *pSelect = 0;      /* SELECT to read %_segdir entry */
148620
148621  rc = fts3SqlStmt(p, SQL_SELECT_SEGDIR, &pSelect, 0);
148622  if( rc==SQLITE_OK ){
148623    sqlite3_int64 iStart = 0;     /* Value of %_segdir.start_block */
148624    sqlite3_int64 iLeafEnd = 0;   /* Value of %_segdir.leaves_end_block */
148625    sqlite3_int64 iEnd = 0;       /* Value of %_segdir.end_block */
148626    const char *aRoot = 0;        /* Pointer to %_segdir.root buffer */
148627    int nRoot = 0;                /* Size of aRoot[] in bytes */
148628    int rc2;                      /* Return code from sqlite3_reset() */
148629    int bAppendable = 0;          /* Set to true if segment is appendable */
148630
148631    /* Read the %_segdir entry for index iIdx absolute level (iAbsLevel+1) */
148632    sqlite3_bind_int64(pSelect, 1, iAbsLevel+1);
148633    sqlite3_bind_int(pSelect, 2, iIdx);
148634    if( sqlite3_step(pSelect)==SQLITE_ROW ){
148635      iStart = sqlite3_column_int64(pSelect, 1);
148636      iLeafEnd = sqlite3_column_int64(pSelect, 2);
148637      fts3ReadEndBlockField(pSelect, 3, &iEnd, &pWriter->nLeafData);
148638      if( pWriter->nLeafData<0 ){
148639        pWriter->nLeafData = pWriter->nLeafData * -1;
148640      }
148641      pWriter->bNoLeafData = (pWriter->nLeafData==0);
148642      nRoot = sqlite3_column_bytes(pSelect, 4);
148643      aRoot = sqlite3_column_blob(pSelect, 4);
148644    }else{
148645      return sqlite3_reset(pSelect);
148646    }
148647
148648    /* Check for the zero-length marker in the %_segments table */
148649    rc = fts3IsAppendable(p, iEnd, &bAppendable);
148650
148651    /* Check that zKey/nKey is larger than the largest key the candidate */
148652    if( rc==SQLITE_OK && bAppendable ){
148653      char *aLeaf = 0;
148654      int nLeaf = 0;
148655
148656      rc = sqlite3Fts3ReadBlock(p, iLeafEnd, &aLeaf, &nLeaf, 0);
148657      if( rc==SQLITE_OK ){
148658        NodeReader reader;
148659        for(rc = nodeReaderInit(&reader, aLeaf, nLeaf);
148660            rc==SQLITE_OK && reader.aNode;
148661            rc = nodeReaderNext(&reader)
148662        ){
148663          assert( reader.aNode );
148664        }
148665        if( fts3TermCmp(zKey, nKey, reader.term.a, reader.term.n)<=0 ){
148666          bAppendable = 0;
148667        }
148668        nodeReaderRelease(&reader);
148669      }
148670      sqlite3_free(aLeaf);
148671    }
148672
148673    if( rc==SQLITE_OK && bAppendable ){
148674      /* It is possible to append to this segment. Set up the IncrmergeWriter
148675      ** object to do so.  */
148676      int i;
148677      int nHeight = (int)aRoot[0];
148678      NodeWriter *pNode;
148679
148680      pWriter->nLeafEst = (int)((iEnd - iStart) + 1)/FTS_MAX_APPENDABLE_HEIGHT;
148681      pWriter->iStart = iStart;
148682      pWriter->iEnd = iEnd;
148683      pWriter->iAbsLevel = iAbsLevel;
148684      pWriter->iIdx = iIdx;
148685
148686      for(i=nHeight+1; i<FTS_MAX_APPENDABLE_HEIGHT; i++){
148687        pWriter->aNodeWriter[i].iBlock = pWriter->iStart + i*pWriter->nLeafEst;
148688      }
148689
148690      pNode = &pWriter->aNodeWriter[nHeight];
148691      pNode->iBlock = pWriter->iStart + pWriter->nLeafEst*nHeight;
148692      blobGrowBuffer(&pNode->block, MAX(nRoot, p->nNodeSize), &rc);
148693      if( rc==SQLITE_OK ){
148694        memcpy(pNode->block.a, aRoot, nRoot);
148695        pNode->block.n = nRoot;
148696      }
148697
148698      for(i=nHeight; i>=0 && rc==SQLITE_OK; i--){
148699        NodeReader reader;
148700        pNode = &pWriter->aNodeWriter[i];
148701
148702        rc = nodeReaderInit(&reader, pNode->block.a, pNode->block.n);
148703        while( reader.aNode && rc==SQLITE_OK ) rc = nodeReaderNext(&reader);
148704        blobGrowBuffer(&pNode->key, reader.term.n, &rc);
148705        if( rc==SQLITE_OK ){
148706          memcpy(pNode->key.a, reader.term.a, reader.term.n);
148707          pNode->key.n = reader.term.n;
148708          if( i>0 ){
148709            char *aBlock = 0;
148710            int nBlock = 0;
148711            pNode = &pWriter->aNodeWriter[i-1];
148712            pNode->iBlock = reader.iChild;
148713            rc = sqlite3Fts3ReadBlock(p, reader.iChild, &aBlock, &nBlock, 0);
148714            blobGrowBuffer(&pNode->block, MAX(nBlock, p->nNodeSize), &rc);
148715            if( rc==SQLITE_OK ){
148716              memcpy(pNode->block.a, aBlock, nBlock);
148717              pNode->block.n = nBlock;
148718            }
148719            sqlite3_free(aBlock);
148720          }
148721        }
148722        nodeReaderRelease(&reader);
148723      }
148724    }
148725
148726    rc2 = sqlite3_reset(pSelect);
148727    if( rc==SQLITE_OK ) rc = rc2;
148728  }
148729
148730  return rc;
148731}
148732
148733/*
148734** Determine the largest segment index value that exists within absolute
148735** level iAbsLevel+1. If no error occurs, set *piIdx to this value plus
148736** one before returning SQLITE_OK. Or, if there are no segments at all
148737** within level iAbsLevel, set *piIdx to zero.
148738**
148739** If an error occurs, return an SQLite error code. The final value of
148740** *piIdx is undefined in this case.
148741*/
148742static int fts3IncrmergeOutputIdx(
148743  Fts3Table *p,                   /* FTS Table handle */
148744  sqlite3_int64 iAbsLevel,        /* Absolute index of input segments */
148745  int *piIdx                      /* OUT: Next free index at iAbsLevel+1 */
148746){
148747  int rc;
148748  sqlite3_stmt *pOutputIdx = 0;   /* SQL used to find output index */
148749
148750  rc = fts3SqlStmt(p, SQL_NEXT_SEGMENT_INDEX, &pOutputIdx, 0);
148751  if( rc==SQLITE_OK ){
148752    sqlite3_bind_int64(pOutputIdx, 1, iAbsLevel+1);
148753    sqlite3_step(pOutputIdx);
148754    *piIdx = sqlite3_column_int(pOutputIdx, 0);
148755    rc = sqlite3_reset(pOutputIdx);
148756  }
148757
148758  return rc;
148759}
148760
148761/*
148762** Allocate an appendable output segment on absolute level iAbsLevel+1
148763** with idx value iIdx.
148764**
148765** In the %_segdir table, a segment is defined by the values in three
148766** columns:
148767**
148768**     start_block
148769**     leaves_end_block
148770**     end_block
148771**
148772** When an appendable segment is allocated, it is estimated that the
148773** maximum number of leaf blocks that may be required is the sum of the
148774** number of leaf blocks consumed by the input segments, plus the number
148775** of input segments, multiplied by two. This value is stored in stack
148776** variable nLeafEst.
148777**
148778** A total of 16*nLeafEst blocks are allocated when an appendable segment
148779** is created ((1 + end_block - start_block)==16*nLeafEst). The contiguous
148780** array of leaf nodes starts at the first block allocated. The array
148781** of interior nodes that are parents of the leaf nodes start at block
148782** (start_block + (1 + end_block - start_block) / 16). And so on.
148783**
148784** In the actual code below, the value "16" is replaced with the
148785** pre-processor macro FTS_MAX_APPENDABLE_HEIGHT.
148786*/
148787static int fts3IncrmergeWriter(
148788  Fts3Table *p,                   /* Fts3 table handle */
148789  sqlite3_int64 iAbsLevel,        /* Absolute level of input segments */
148790  int iIdx,                       /* Index of new output segment */
148791  Fts3MultiSegReader *pCsr,       /* Cursor that data will be read from */
148792  IncrmergeWriter *pWriter        /* Populate this object */
148793){
148794  int rc;                         /* Return Code */
148795  int i;                          /* Iterator variable */
148796  int nLeafEst = 0;               /* Blocks allocated for leaf nodes */
148797  sqlite3_stmt *pLeafEst = 0;     /* SQL used to determine nLeafEst */
148798  sqlite3_stmt *pFirstBlock = 0;  /* SQL used to determine first block */
148799
148800  /* Calculate nLeafEst. */
148801  rc = fts3SqlStmt(p, SQL_MAX_LEAF_NODE_ESTIMATE, &pLeafEst, 0);
148802  if( rc==SQLITE_OK ){
148803    sqlite3_bind_int64(pLeafEst, 1, iAbsLevel);
148804    sqlite3_bind_int64(pLeafEst, 2, pCsr->nSegment);
148805    if( SQLITE_ROW==sqlite3_step(pLeafEst) ){
148806      nLeafEst = sqlite3_column_int(pLeafEst, 0);
148807    }
148808    rc = sqlite3_reset(pLeafEst);
148809  }
148810  if( rc!=SQLITE_OK ) return rc;
148811
148812  /* Calculate the first block to use in the output segment */
148813  rc = fts3SqlStmt(p, SQL_NEXT_SEGMENTS_ID, &pFirstBlock, 0);
148814  if( rc==SQLITE_OK ){
148815    if( SQLITE_ROW==sqlite3_step(pFirstBlock) ){
148816      pWriter->iStart = sqlite3_column_int64(pFirstBlock, 0);
148817      pWriter->iEnd = pWriter->iStart - 1;
148818      pWriter->iEnd += nLeafEst * FTS_MAX_APPENDABLE_HEIGHT;
148819    }
148820    rc = sqlite3_reset(pFirstBlock);
148821  }
148822  if( rc!=SQLITE_OK ) return rc;
148823
148824  /* Insert the marker in the %_segments table to make sure nobody tries
148825  ** to steal the space just allocated. This is also used to identify
148826  ** appendable segments.  */
148827  rc = fts3WriteSegment(p, pWriter->iEnd, 0, 0);
148828  if( rc!=SQLITE_OK ) return rc;
148829
148830  pWriter->iAbsLevel = iAbsLevel;
148831  pWriter->nLeafEst = nLeafEst;
148832  pWriter->iIdx = iIdx;
148833
148834  /* Set up the array of NodeWriter objects */
148835  for(i=0; i<FTS_MAX_APPENDABLE_HEIGHT; i++){
148836    pWriter->aNodeWriter[i].iBlock = pWriter->iStart + i*pWriter->nLeafEst;
148837  }
148838  return SQLITE_OK;
148839}
148840
148841/*
148842** Remove an entry from the %_segdir table. This involves running the
148843** following two statements:
148844**
148845**   DELETE FROM %_segdir WHERE level = :iAbsLevel AND idx = :iIdx
148846**   UPDATE %_segdir SET idx = idx - 1 WHERE level = :iAbsLevel AND idx > :iIdx
148847**
148848** The DELETE statement removes the specific %_segdir level. The UPDATE
148849** statement ensures that the remaining segments have contiguously allocated
148850** idx values.
148851*/
148852static int fts3RemoveSegdirEntry(
148853  Fts3Table *p,                   /* FTS3 table handle */
148854  sqlite3_int64 iAbsLevel,        /* Absolute level to delete from */
148855  int iIdx                        /* Index of %_segdir entry to delete */
148856){
148857  int rc;                         /* Return code */
148858  sqlite3_stmt *pDelete = 0;      /* DELETE statement */
148859
148860  rc = fts3SqlStmt(p, SQL_DELETE_SEGDIR_ENTRY, &pDelete, 0);
148861  if( rc==SQLITE_OK ){
148862    sqlite3_bind_int64(pDelete, 1, iAbsLevel);
148863    sqlite3_bind_int(pDelete, 2, iIdx);
148864    sqlite3_step(pDelete);
148865    rc = sqlite3_reset(pDelete);
148866  }
148867
148868  return rc;
148869}
148870
148871/*
148872** One or more segments have just been removed from absolute level iAbsLevel.
148873** Update the 'idx' values of the remaining segments in the level so that
148874** the idx values are a contiguous sequence starting from 0.
148875*/
148876static int fts3RepackSegdirLevel(
148877  Fts3Table *p,                   /* FTS3 table handle */
148878  sqlite3_int64 iAbsLevel         /* Absolute level to repack */
148879){
148880  int rc;                         /* Return code */
148881  int *aIdx = 0;                  /* Array of remaining idx values */
148882  int nIdx = 0;                   /* Valid entries in aIdx[] */
148883  int nAlloc = 0;                 /* Allocated size of aIdx[] */
148884  int i;                          /* Iterator variable */
148885  sqlite3_stmt *pSelect = 0;      /* Select statement to read idx values */
148886  sqlite3_stmt *pUpdate = 0;      /* Update statement to modify idx values */
148887
148888  rc = fts3SqlStmt(p, SQL_SELECT_INDEXES, &pSelect, 0);
148889  if( rc==SQLITE_OK ){
148890    int rc2;
148891    sqlite3_bind_int64(pSelect, 1, iAbsLevel);
148892    while( SQLITE_ROW==sqlite3_step(pSelect) ){
148893      if( nIdx>=nAlloc ){
148894        int *aNew;
148895        nAlloc += 16;
148896        aNew = sqlite3_realloc(aIdx, nAlloc*sizeof(int));
148897        if( !aNew ){
148898          rc = SQLITE_NOMEM;
148899          break;
148900        }
148901        aIdx = aNew;
148902      }
148903      aIdx[nIdx++] = sqlite3_column_int(pSelect, 0);
148904    }
148905    rc2 = sqlite3_reset(pSelect);
148906    if( rc==SQLITE_OK ) rc = rc2;
148907  }
148908
148909  if( rc==SQLITE_OK ){
148910    rc = fts3SqlStmt(p, SQL_SHIFT_SEGDIR_ENTRY, &pUpdate, 0);
148911  }
148912  if( rc==SQLITE_OK ){
148913    sqlite3_bind_int64(pUpdate, 2, iAbsLevel);
148914  }
148915
148916  assert( p->bIgnoreSavepoint==0 );
148917  p->bIgnoreSavepoint = 1;
148918  for(i=0; rc==SQLITE_OK && i<nIdx; i++){
148919    if( aIdx[i]!=i ){
148920      sqlite3_bind_int(pUpdate, 3, aIdx[i]);
148921      sqlite3_bind_int(pUpdate, 1, i);
148922      sqlite3_step(pUpdate);
148923      rc = sqlite3_reset(pUpdate);
148924    }
148925  }
148926  p->bIgnoreSavepoint = 0;
148927
148928  sqlite3_free(aIdx);
148929  return rc;
148930}
148931
148932static void fts3StartNode(Blob *pNode, int iHeight, sqlite3_int64 iChild){
148933  pNode->a[0] = (char)iHeight;
148934  if( iChild ){
148935    assert( pNode->nAlloc>=1+sqlite3Fts3VarintLen(iChild) );
148936    pNode->n = 1 + sqlite3Fts3PutVarint(&pNode->a[1], iChild);
148937  }else{
148938    assert( pNode->nAlloc>=1 );
148939    pNode->n = 1;
148940  }
148941}
148942
148943/*
148944** The first two arguments are a pointer to and the size of a segment b-tree
148945** node. The node may be a leaf or an internal node.
148946**
148947** This function creates a new node image in blob object *pNew by copying
148948** all terms that are greater than or equal to zTerm/nTerm (for leaf nodes)
148949** or greater than zTerm/nTerm (for internal nodes) from aNode/nNode.
148950*/
148951static int fts3TruncateNode(
148952  const char *aNode,              /* Current node image */
148953  int nNode,                      /* Size of aNode in bytes */
148954  Blob *pNew,                     /* OUT: Write new node image here */
148955  const char *zTerm,              /* Omit all terms smaller than this */
148956  int nTerm,                      /* Size of zTerm in bytes */
148957  sqlite3_int64 *piBlock          /* OUT: Block number in next layer down */
148958){
148959  NodeReader reader;              /* Reader object */
148960  Blob prev = {0, 0, 0};          /* Previous term written to new node */
148961  int rc = SQLITE_OK;             /* Return code */
148962  int bLeaf = aNode[0]=='\0';     /* True for a leaf node */
148963
148964  /* Allocate required output space */
148965  blobGrowBuffer(pNew, nNode, &rc);
148966  if( rc!=SQLITE_OK ) return rc;
148967  pNew->n = 0;
148968
148969  /* Populate new node buffer */
148970  for(rc = nodeReaderInit(&reader, aNode, nNode);
148971      rc==SQLITE_OK && reader.aNode;
148972      rc = nodeReaderNext(&reader)
148973  ){
148974    if( pNew->n==0 ){
148975      int res = fts3TermCmp(reader.term.a, reader.term.n, zTerm, nTerm);
148976      if( res<0 || (bLeaf==0 && res==0) ) continue;
148977      fts3StartNode(pNew, (int)aNode[0], reader.iChild);
148978      *piBlock = reader.iChild;
148979    }
148980    rc = fts3AppendToNode(
148981        pNew, &prev, reader.term.a, reader.term.n,
148982        reader.aDoclist, reader.nDoclist
148983    );
148984    if( rc!=SQLITE_OK ) break;
148985  }
148986  if( pNew->n==0 ){
148987    fts3StartNode(pNew, (int)aNode[0], reader.iChild);
148988    *piBlock = reader.iChild;
148989  }
148990  assert( pNew->n<=pNew->nAlloc );
148991
148992  nodeReaderRelease(&reader);
148993  sqlite3_free(prev.a);
148994  return rc;
148995}
148996
148997/*
148998** Remove all terms smaller than zTerm/nTerm from segment iIdx in absolute
148999** level iAbsLevel. This may involve deleting entries from the %_segments
149000** table, and modifying existing entries in both the %_segments and %_segdir
149001** tables.
149002**
149003** SQLITE_OK is returned if the segment is updated successfully. Or an
149004** SQLite error code otherwise.
149005*/
149006static int fts3TruncateSegment(
149007  Fts3Table *p,                   /* FTS3 table handle */
149008  sqlite3_int64 iAbsLevel,        /* Absolute level of segment to modify */
149009  int iIdx,                       /* Index within level of segment to modify */
149010  const char *zTerm,              /* Remove terms smaller than this */
149011  int nTerm                      /* Number of bytes in buffer zTerm */
149012){
149013  int rc = SQLITE_OK;             /* Return code */
149014  Blob root = {0,0,0};            /* New root page image */
149015  Blob block = {0,0,0};           /* Buffer used for any other block */
149016  sqlite3_int64 iBlock = 0;       /* Block id */
149017  sqlite3_int64 iNewStart = 0;    /* New value for iStartBlock */
149018  sqlite3_int64 iOldStart = 0;    /* Old value for iStartBlock */
149019  sqlite3_stmt *pFetch = 0;       /* Statement used to fetch segdir */
149020
149021  rc = fts3SqlStmt(p, SQL_SELECT_SEGDIR, &pFetch, 0);
149022  if( rc==SQLITE_OK ){
149023    int rc2;                      /* sqlite3_reset() return code */
149024    sqlite3_bind_int64(pFetch, 1, iAbsLevel);
149025    sqlite3_bind_int(pFetch, 2, iIdx);
149026    if( SQLITE_ROW==sqlite3_step(pFetch) ){
149027      const char *aRoot = sqlite3_column_blob(pFetch, 4);
149028      int nRoot = sqlite3_column_bytes(pFetch, 4);
149029      iOldStart = sqlite3_column_int64(pFetch, 1);
149030      rc = fts3TruncateNode(aRoot, nRoot, &root, zTerm, nTerm, &iBlock);
149031    }
149032    rc2 = sqlite3_reset(pFetch);
149033    if( rc==SQLITE_OK ) rc = rc2;
149034  }
149035
149036  while( rc==SQLITE_OK && iBlock ){
149037    char *aBlock = 0;
149038    int nBlock = 0;
149039    iNewStart = iBlock;
149040
149041    rc = sqlite3Fts3ReadBlock(p, iBlock, &aBlock, &nBlock, 0);
149042    if( rc==SQLITE_OK ){
149043      rc = fts3TruncateNode(aBlock, nBlock, &block, zTerm, nTerm, &iBlock);
149044    }
149045    if( rc==SQLITE_OK ){
149046      rc = fts3WriteSegment(p, iNewStart, block.a, block.n);
149047    }
149048    sqlite3_free(aBlock);
149049  }
149050
149051  /* Variable iNewStart now contains the first valid leaf node. */
149052  if( rc==SQLITE_OK && iNewStart ){
149053    sqlite3_stmt *pDel = 0;
149054    rc = fts3SqlStmt(p, SQL_DELETE_SEGMENTS_RANGE, &pDel, 0);
149055    if( rc==SQLITE_OK ){
149056      sqlite3_bind_int64(pDel, 1, iOldStart);
149057      sqlite3_bind_int64(pDel, 2, iNewStart-1);
149058      sqlite3_step(pDel);
149059      rc = sqlite3_reset(pDel);
149060    }
149061  }
149062
149063  if( rc==SQLITE_OK ){
149064    sqlite3_stmt *pChomp = 0;
149065    rc = fts3SqlStmt(p, SQL_CHOMP_SEGDIR, &pChomp, 0);
149066    if( rc==SQLITE_OK ){
149067      sqlite3_bind_int64(pChomp, 1, iNewStart);
149068      sqlite3_bind_blob(pChomp, 2, root.a, root.n, SQLITE_STATIC);
149069      sqlite3_bind_int64(pChomp, 3, iAbsLevel);
149070      sqlite3_bind_int(pChomp, 4, iIdx);
149071      sqlite3_step(pChomp);
149072      rc = sqlite3_reset(pChomp);
149073    }
149074  }
149075
149076  sqlite3_free(root.a);
149077  sqlite3_free(block.a);
149078  return rc;
149079}
149080
149081/*
149082** This function is called after an incrmental-merge operation has run to
149083** merge (or partially merge) two or more segments from absolute level
149084** iAbsLevel.
149085**
149086** Each input segment is either removed from the db completely (if all of
149087** its data was copied to the output segment by the incrmerge operation)
149088** or modified in place so that it no longer contains those entries that
149089** have been duplicated in the output segment.
149090*/
149091static int fts3IncrmergeChomp(
149092  Fts3Table *p,                   /* FTS table handle */
149093  sqlite3_int64 iAbsLevel,        /* Absolute level containing segments */
149094  Fts3MultiSegReader *pCsr,       /* Chomp all segments opened by this cursor */
149095  int *pnRem                      /* Number of segments not deleted */
149096){
149097  int i;
149098  int nRem = 0;
149099  int rc = SQLITE_OK;
149100
149101  for(i=pCsr->nSegment-1; i>=0 && rc==SQLITE_OK; i--){
149102    Fts3SegReader *pSeg = 0;
149103    int j;
149104
149105    /* Find the Fts3SegReader object with Fts3SegReader.iIdx==i. It is hiding
149106    ** somewhere in the pCsr->apSegment[] array.  */
149107    for(j=0; ALWAYS(j<pCsr->nSegment); j++){
149108      pSeg = pCsr->apSegment[j];
149109      if( pSeg->iIdx==i ) break;
149110    }
149111    assert( j<pCsr->nSegment && pSeg->iIdx==i );
149112
149113    if( pSeg->aNode==0 ){
149114      /* Seg-reader is at EOF. Remove the entire input segment. */
149115      rc = fts3DeleteSegment(p, pSeg);
149116      if( rc==SQLITE_OK ){
149117        rc = fts3RemoveSegdirEntry(p, iAbsLevel, pSeg->iIdx);
149118      }
149119      *pnRem = 0;
149120    }else{
149121      /* The incremental merge did not copy all the data from this
149122      ** segment to the upper level. The segment is modified in place
149123      ** so that it contains no keys smaller than zTerm/nTerm. */
149124      const char *zTerm = pSeg->zTerm;
149125      int nTerm = pSeg->nTerm;
149126      rc = fts3TruncateSegment(p, iAbsLevel, pSeg->iIdx, zTerm, nTerm);
149127      nRem++;
149128    }
149129  }
149130
149131  if( rc==SQLITE_OK && nRem!=pCsr->nSegment ){
149132    rc = fts3RepackSegdirLevel(p, iAbsLevel);
149133  }
149134
149135  *pnRem = nRem;
149136  return rc;
149137}
149138
149139/*
149140** Store an incr-merge hint in the database.
149141*/
149142static int fts3IncrmergeHintStore(Fts3Table *p, Blob *pHint){
149143  sqlite3_stmt *pReplace = 0;
149144  int rc;                         /* Return code */
149145
149146  rc = fts3SqlStmt(p, SQL_REPLACE_STAT, &pReplace, 0);
149147  if( rc==SQLITE_OK ){
149148    sqlite3_bind_int(pReplace, 1, FTS_STAT_INCRMERGEHINT);
149149    sqlite3_bind_blob(pReplace, 2, pHint->a, pHint->n, SQLITE_STATIC);
149150    sqlite3_step(pReplace);
149151    rc = sqlite3_reset(pReplace);
149152  }
149153
149154  return rc;
149155}
149156
149157/*
149158** Load an incr-merge hint from the database. The incr-merge hint, if one
149159** exists, is stored in the rowid==1 row of the %_stat table.
149160**
149161** If successful, populate blob *pHint with the value read from the %_stat
149162** table and return SQLITE_OK. Otherwise, if an error occurs, return an
149163** SQLite error code.
149164*/
149165static int fts3IncrmergeHintLoad(Fts3Table *p, Blob *pHint){
149166  sqlite3_stmt *pSelect = 0;
149167  int rc;
149168
149169  pHint->n = 0;
149170  rc = fts3SqlStmt(p, SQL_SELECT_STAT, &pSelect, 0);
149171  if( rc==SQLITE_OK ){
149172    int rc2;
149173    sqlite3_bind_int(pSelect, 1, FTS_STAT_INCRMERGEHINT);
149174    if( SQLITE_ROW==sqlite3_step(pSelect) ){
149175      const char *aHint = sqlite3_column_blob(pSelect, 0);
149176      int nHint = sqlite3_column_bytes(pSelect, 0);
149177      if( aHint ){
149178        blobGrowBuffer(pHint, nHint, &rc);
149179        if( rc==SQLITE_OK ){
149180          memcpy(pHint->a, aHint, nHint);
149181          pHint->n = nHint;
149182        }
149183      }
149184    }
149185    rc2 = sqlite3_reset(pSelect);
149186    if( rc==SQLITE_OK ) rc = rc2;
149187  }
149188
149189  return rc;
149190}
149191
149192/*
149193** If *pRc is not SQLITE_OK when this function is called, it is a no-op.
149194** Otherwise, append an entry to the hint stored in blob *pHint. Each entry
149195** consists of two varints, the absolute level number of the input segments
149196** and the number of input segments.
149197**
149198** If successful, leave *pRc set to SQLITE_OK and return. If an error occurs,
149199** set *pRc to an SQLite error code before returning.
149200*/
149201static void fts3IncrmergeHintPush(
149202  Blob *pHint,                    /* Hint blob to append to */
149203  i64 iAbsLevel,                  /* First varint to store in hint */
149204  int nInput,                     /* Second varint to store in hint */
149205  int *pRc                        /* IN/OUT: Error code */
149206){
149207  blobGrowBuffer(pHint, pHint->n + 2*FTS3_VARINT_MAX, pRc);
149208  if( *pRc==SQLITE_OK ){
149209    pHint->n += sqlite3Fts3PutVarint(&pHint->a[pHint->n], iAbsLevel);
149210    pHint->n += sqlite3Fts3PutVarint(&pHint->a[pHint->n], (i64)nInput);
149211  }
149212}
149213
149214/*
149215** Read the last entry (most recently pushed) from the hint blob *pHint
149216** and then remove the entry. Write the two values read to *piAbsLevel and
149217** *pnInput before returning.
149218**
149219** If no error occurs, return SQLITE_OK. If the hint blob in *pHint does
149220** not contain at least two valid varints, return SQLITE_CORRUPT_VTAB.
149221*/
149222static int fts3IncrmergeHintPop(Blob *pHint, i64 *piAbsLevel, int *pnInput){
149223  const int nHint = pHint->n;
149224  int i;
149225
149226  i = pHint->n-2;
149227  while( i>0 && (pHint->a[i-1] & 0x80) ) i--;
149228  while( i>0 && (pHint->a[i-1] & 0x80) ) i--;
149229
149230  pHint->n = i;
149231  i += sqlite3Fts3GetVarint(&pHint->a[i], piAbsLevel);
149232  i += fts3GetVarint32(&pHint->a[i], pnInput);
149233  if( i!=nHint ) return FTS_CORRUPT_VTAB;
149234
149235  return SQLITE_OK;
149236}
149237
149238
149239/*
149240** Attempt an incremental merge that writes nMerge leaf blocks.
149241**
149242** Incremental merges happen nMin segments at a time. The segments
149243** to be merged are the nMin oldest segments (the ones with the smallest
149244** values for the _segdir.idx field) in the highest level that contains
149245** at least nMin segments. Multiple merges might occur in an attempt to
149246** write the quota of nMerge leaf blocks.
149247*/
149248SQLITE_PRIVATE int sqlite3Fts3Incrmerge(Fts3Table *p, int nMerge, int nMin){
149249  int rc;                         /* Return code */
149250  int nRem = nMerge;              /* Number of leaf pages yet to  be written */
149251  Fts3MultiSegReader *pCsr;       /* Cursor used to read input data */
149252  Fts3SegFilter *pFilter;         /* Filter used with cursor pCsr */
149253  IncrmergeWriter *pWriter;       /* Writer object */
149254  int nSeg = 0;                   /* Number of input segments */
149255  sqlite3_int64 iAbsLevel = 0;    /* Absolute level number to work on */
149256  Blob hint = {0, 0, 0};          /* Hint read from %_stat table */
149257  int bDirtyHint = 0;             /* True if blob 'hint' has been modified */
149258
149259  /* Allocate space for the cursor, filter and writer objects */
149260  const int nAlloc = sizeof(*pCsr) + sizeof(*pFilter) + sizeof(*pWriter);
149261  pWriter = (IncrmergeWriter *)sqlite3_malloc(nAlloc);
149262  if( !pWriter ) return SQLITE_NOMEM;
149263  pFilter = (Fts3SegFilter *)&pWriter[1];
149264  pCsr = (Fts3MultiSegReader *)&pFilter[1];
149265
149266  rc = fts3IncrmergeHintLoad(p, &hint);
149267  while( rc==SQLITE_OK && nRem>0 ){
149268    const i64 nMod = FTS3_SEGDIR_MAXLEVEL * p->nIndex;
149269    sqlite3_stmt *pFindLevel = 0; /* SQL used to determine iAbsLevel */
149270    int bUseHint = 0;             /* True if attempting to append */
149271    int iIdx = 0;                 /* Largest idx in level (iAbsLevel+1) */
149272
149273    /* Search the %_segdir table for the absolute level with the smallest
149274    ** relative level number that contains at least nMin segments, if any.
149275    ** If one is found, set iAbsLevel to the absolute level number and
149276    ** nSeg to nMin. If no level with at least nMin segments can be found,
149277    ** set nSeg to -1.
149278    */
149279    rc = fts3SqlStmt(p, SQL_FIND_MERGE_LEVEL, &pFindLevel, 0);
149280    sqlite3_bind_int(pFindLevel, 1, nMin);
149281    if( sqlite3_step(pFindLevel)==SQLITE_ROW ){
149282      iAbsLevel = sqlite3_column_int64(pFindLevel, 0);
149283      nSeg = nMin;
149284    }else{
149285      nSeg = -1;
149286    }
149287    rc = sqlite3_reset(pFindLevel);
149288
149289    /* If the hint read from the %_stat table is not empty, check if the
149290    ** last entry in it specifies a relative level smaller than or equal
149291    ** to the level identified by the block above (if any). If so, this
149292    ** iteration of the loop will work on merging at the hinted level.
149293    */
149294    if( rc==SQLITE_OK && hint.n ){
149295      int nHint = hint.n;
149296      sqlite3_int64 iHintAbsLevel = 0;      /* Hint level */
149297      int nHintSeg = 0;                     /* Hint number of segments */
149298
149299      rc = fts3IncrmergeHintPop(&hint, &iHintAbsLevel, &nHintSeg);
149300      if( nSeg<0 || (iAbsLevel % nMod) >= (iHintAbsLevel % nMod) ){
149301        iAbsLevel = iHintAbsLevel;
149302        nSeg = nHintSeg;
149303        bUseHint = 1;
149304        bDirtyHint = 1;
149305      }else{
149306        /* This undoes the effect of the HintPop() above - so that no entry
149307        ** is removed from the hint blob.  */
149308        hint.n = nHint;
149309      }
149310    }
149311
149312    /* If nSeg is less that zero, then there is no level with at least
149313    ** nMin segments and no hint in the %_stat table. No work to do.
149314    ** Exit early in this case.  */
149315    if( nSeg<0 ) break;
149316
149317    /* Open a cursor to iterate through the contents of the oldest nSeg
149318    ** indexes of absolute level iAbsLevel. If this cursor is opened using
149319    ** the 'hint' parameters, it is possible that there are less than nSeg
149320    ** segments available in level iAbsLevel. In this case, no work is
149321    ** done on iAbsLevel - fall through to the next iteration of the loop
149322    ** to start work on some other level.  */
149323    memset(pWriter, 0, nAlloc);
149324    pFilter->flags = FTS3_SEGMENT_REQUIRE_POS;
149325
149326    if( rc==SQLITE_OK ){
149327      rc = fts3IncrmergeOutputIdx(p, iAbsLevel, &iIdx);
149328      assert( bUseHint==1 || bUseHint==0 );
149329      if( iIdx==0 || (bUseHint && iIdx==1) ){
149330        int bIgnore = 0;
149331        rc = fts3SegmentIsMaxLevel(p, iAbsLevel+1, &bIgnore);
149332        if( bIgnore ){
149333          pFilter->flags |= FTS3_SEGMENT_IGNORE_EMPTY;
149334        }
149335      }
149336    }
149337
149338    if( rc==SQLITE_OK ){
149339      rc = fts3IncrmergeCsr(p, iAbsLevel, nSeg, pCsr);
149340    }
149341    if( SQLITE_OK==rc && pCsr->nSegment==nSeg
149342     && SQLITE_OK==(rc = sqlite3Fts3SegReaderStart(p, pCsr, pFilter))
149343     && SQLITE_ROW==(rc = sqlite3Fts3SegReaderStep(p, pCsr))
149344    ){
149345      if( bUseHint && iIdx>0 ){
149346        const char *zKey = pCsr->zTerm;
149347        int nKey = pCsr->nTerm;
149348        rc = fts3IncrmergeLoad(p, iAbsLevel, iIdx-1, zKey, nKey, pWriter);
149349      }else{
149350        rc = fts3IncrmergeWriter(p, iAbsLevel, iIdx, pCsr, pWriter);
149351      }
149352
149353      if( rc==SQLITE_OK && pWriter->nLeafEst ){
149354        fts3LogMerge(nSeg, iAbsLevel);
149355        do {
149356          rc = fts3IncrmergeAppend(p, pWriter, pCsr);
149357          if( rc==SQLITE_OK ) rc = sqlite3Fts3SegReaderStep(p, pCsr);
149358          if( pWriter->nWork>=nRem && rc==SQLITE_ROW ) rc = SQLITE_OK;
149359        }while( rc==SQLITE_ROW );
149360
149361        /* Update or delete the input segments */
149362        if( rc==SQLITE_OK ){
149363          nRem -= (1 + pWriter->nWork);
149364          rc = fts3IncrmergeChomp(p, iAbsLevel, pCsr, &nSeg);
149365          if( nSeg!=0 ){
149366            bDirtyHint = 1;
149367            fts3IncrmergeHintPush(&hint, iAbsLevel, nSeg, &rc);
149368          }
149369        }
149370      }
149371
149372      if( nSeg!=0 ){
149373        pWriter->nLeafData = pWriter->nLeafData * -1;
149374      }
149375      fts3IncrmergeRelease(p, pWriter, &rc);
149376      if( nSeg==0 && pWriter->bNoLeafData==0 ){
149377        fts3PromoteSegments(p, iAbsLevel+1, pWriter->nLeafData);
149378      }
149379    }
149380
149381    sqlite3Fts3SegReaderFinish(pCsr);
149382  }
149383
149384  /* Write the hint values into the %_stat table for the next incr-merger */
149385  if( bDirtyHint && rc==SQLITE_OK ){
149386    rc = fts3IncrmergeHintStore(p, &hint);
149387  }
149388
149389  sqlite3_free(pWriter);
149390  sqlite3_free(hint.a);
149391  return rc;
149392}
149393
149394/*
149395** Convert the text beginning at *pz into an integer and return
149396** its value.  Advance *pz to point to the first character past
149397** the integer.
149398*/
149399static int fts3Getint(const char **pz){
149400  const char *z = *pz;
149401  int i = 0;
149402  while( (*z)>='0' && (*z)<='9' ) i = 10*i + *(z++) - '0';
149403  *pz = z;
149404  return i;
149405}
149406
149407/*
149408** Process statements of the form:
149409**
149410**    INSERT INTO table(table) VALUES('merge=A,B');
149411**
149412** A and B are integers that decode to be the number of leaf pages
149413** written for the merge, and the minimum number of segments on a level
149414** before it will be selected for a merge, respectively.
149415*/
149416static int fts3DoIncrmerge(
149417  Fts3Table *p,                   /* FTS3 table handle */
149418  const char *zParam              /* Nul-terminated string containing "A,B" */
149419){
149420  int rc;
149421  int nMin = (FTS3_MERGE_COUNT / 2);
149422  int nMerge = 0;
149423  const char *z = zParam;
149424
149425  /* Read the first integer value */
149426  nMerge = fts3Getint(&z);
149427
149428  /* If the first integer value is followed by a ',',  read the second
149429  ** integer value. */
149430  if( z[0]==',' && z[1]!='\0' ){
149431    z++;
149432    nMin = fts3Getint(&z);
149433  }
149434
149435  if( z[0]!='\0' || nMin<2 ){
149436    rc = SQLITE_ERROR;
149437  }else{
149438    rc = SQLITE_OK;
149439    if( !p->bHasStat ){
149440      assert( p->bFts4==0 );
149441      sqlite3Fts3CreateStatTable(&rc, p);
149442    }
149443    if( rc==SQLITE_OK ){
149444      rc = sqlite3Fts3Incrmerge(p, nMerge, nMin);
149445    }
149446    sqlite3Fts3SegmentsClose(p);
149447  }
149448  return rc;
149449}
149450
149451/*
149452** Process statements of the form:
149453**
149454**    INSERT INTO table(table) VALUES('automerge=X');
149455**
149456** where X is an integer.  X==0 means to turn automerge off.  X!=0 means
149457** turn it on.  The setting is persistent.
149458*/
149459static int fts3DoAutoincrmerge(
149460  Fts3Table *p,                   /* FTS3 table handle */
149461  const char *zParam              /* Nul-terminated string containing boolean */
149462){
149463  int rc = SQLITE_OK;
149464  sqlite3_stmt *pStmt = 0;
149465  p->nAutoincrmerge = fts3Getint(&zParam);
149466  if( p->nAutoincrmerge==1 || p->nAutoincrmerge>FTS3_MERGE_COUNT ){
149467    p->nAutoincrmerge = 8;
149468  }
149469  if( !p->bHasStat ){
149470    assert( p->bFts4==0 );
149471    sqlite3Fts3CreateStatTable(&rc, p);
149472    if( rc ) return rc;
149473  }
149474  rc = fts3SqlStmt(p, SQL_REPLACE_STAT, &pStmt, 0);
149475  if( rc ) return rc;
149476  sqlite3_bind_int(pStmt, 1, FTS_STAT_AUTOINCRMERGE);
149477  sqlite3_bind_int(pStmt, 2, p->nAutoincrmerge);
149478  sqlite3_step(pStmt);
149479  rc = sqlite3_reset(pStmt);
149480  return rc;
149481}
149482
149483/*
149484** Return a 64-bit checksum for the FTS index entry specified by the
149485** arguments to this function.
149486*/
149487static u64 fts3ChecksumEntry(
149488  const char *zTerm,              /* Pointer to buffer containing term */
149489  int nTerm,                      /* Size of zTerm in bytes */
149490  int iLangid,                    /* Language id for current row */
149491  int iIndex,                     /* Index (0..Fts3Table.nIndex-1) */
149492  i64 iDocid,                     /* Docid for current row. */
149493  int iCol,                       /* Column number */
149494  int iPos                        /* Position */
149495){
149496  int i;
149497  u64 ret = (u64)iDocid;
149498
149499  ret += (ret<<3) + iLangid;
149500  ret += (ret<<3) + iIndex;
149501  ret += (ret<<3) + iCol;
149502  ret += (ret<<3) + iPos;
149503  for(i=0; i<nTerm; i++) ret += (ret<<3) + zTerm[i];
149504
149505  return ret;
149506}
149507
149508/*
149509** Return a checksum of all entries in the FTS index that correspond to
149510** language id iLangid. The checksum is calculated by XORing the checksums
149511** of each individual entry (see fts3ChecksumEntry()) together.
149512**
149513** If successful, the checksum value is returned and *pRc set to SQLITE_OK.
149514** Otherwise, if an error occurs, *pRc is set to an SQLite error code. The
149515** return value is undefined in this case.
149516*/
149517static u64 fts3ChecksumIndex(
149518  Fts3Table *p,                   /* FTS3 table handle */
149519  int iLangid,                    /* Language id to return cksum for */
149520  int iIndex,                     /* Index to cksum (0..p->nIndex-1) */
149521  int *pRc                        /* OUT: Return code */
149522){
149523  Fts3SegFilter filter;
149524  Fts3MultiSegReader csr;
149525  int rc;
149526  u64 cksum = 0;
149527
149528  assert( *pRc==SQLITE_OK );
149529
149530  memset(&filter, 0, sizeof(filter));
149531  memset(&csr, 0, sizeof(csr));
149532  filter.flags =  FTS3_SEGMENT_REQUIRE_POS|FTS3_SEGMENT_IGNORE_EMPTY;
149533  filter.flags |= FTS3_SEGMENT_SCAN;
149534
149535  rc = sqlite3Fts3SegReaderCursor(
149536      p, iLangid, iIndex, FTS3_SEGCURSOR_ALL, 0, 0, 0, 1,&csr
149537  );
149538  if( rc==SQLITE_OK ){
149539    rc = sqlite3Fts3SegReaderStart(p, &csr, &filter);
149540  }
149541
149542  if( rc==SQLITE_OK ){
149543    while( SQLITE_ROW==(rc = sqlite3Fts3SegReaderStep(p, &csr)) ){
149544      char *pCsr = csr.aDoclist;
149545      char *pEnd = &pCsr[csr.nDoclist];
149546
149547      i64 iDocid = 0;
149548      i64 iCol = 0;
149549      i64 iPos = 0;
149550
149551      pCsr += sqlite3Fts3GetVarint(pCsr, &iDocid);
149552      while( pCsr<pEnd ){
149553        i64 iVal = 0;
149554        pCsr += sqlite3Fts3GetVarint(pCsr, &iVal);
149555        if( pCsr<pEnd ){
149556          if( iVal==0 || iVal==1 ){
149557            iCol = 0;
149558            iPos = 0;
149559            if( iVal ){
149560              pCsr += sqlite3Fts3GetVarint(pCsr, &iCol);
149561            }else{
149562              pCsr += sqlite3Fts3GetVarint(pCsr, &iVal);
149563              iDocid += iVal;
149564            }
149565          }else{
149566            iPos += (iVal - 2);
149567            cksum = cksum ^ fts3ChecksumEntry(
149568                csr.zTerm, csr.nTerm, iLangid, iIndex, iDocid,
149569                (int)iCol, (int)iPos
149570            );
149571          }
149572        }
149573      }
149574    }
149575  }
149576  sqlite3Fts3SegReaderFinish(&csr);
149577
149578  *pRc = rc;
149579  return cksum;
149580}
149581
149582/*
149583** Check if the contents of the FTS index match the current contents of the
149584** content table. If no error occurs and the contents do match, set *pbOk
149585** to true and return SQLITE_OK. Or if the contents do not match, set *pbOk
149586** to false before returning.
149587**
149588** If an error occurs (e.g. an OOM or IO error), return an SQLite error
149589** code. The final value of *pbOk is undefined in this case.
149590*/
149591static int fts3IntegrityCheck(Fts3Table *p, int *pbOk){
149592  int rc = SQLITE_OK;             /* Return code */
149593  u64 cksum1 = 0;                 /* Checksum based on FTS index contents */
149594  u64 cksum2 = 0;                 /* Checksum based on %_content contents */
149595  sqlite3_stmt *pAllLangid = 0;   /* Statement to return all language-ids */
149596
149597  /* This block calculates the checksum according to the FTS index. */
149598  rc = fts3SqlStmt(p, SQL_SELECT_ALL_LANGID, &pAllLangid, 0);
149599  if( rc==SQLITE_OK ){
149600    int rc2;
149601    sqlite3_bind_int(pAllLangid, 1, p->iPrevLangid);
149602    sqlite3_bind_int(pAllLangid, 2, p->nIndex);
149603    while( rc==SQLITE_OK && sqlite3_step(pAllLangid)==SQLITE_ROW ){
149604      int iLangid = sqlite3_column_int(pAllLangid, 0);
149605      int i;
149606      for(i=0; i<p->nIndex; i++){
149607        cksum1 = cksum1 ^ fts3ChecksumIndex(p, iLangid, i, &rc);
149608      }
149609    }
149610    rc2 = sqlite3_reset(pAllLangid);
149611    if( rc==SQLITE_OK ) rc = rc2;
149612  }
149613
149614  /* This block calculates the checksum according to the %_content table */
149615  if( rc==SQLITE_OK ){
149616    sqlite3_tokenizer_module const *pModule = p->pTokenizer->pModule;
149617    sqlite3_stmt *pStmt = 0;
149618    char *zSql;
149619
149620    zSql = sqlite3_mprintf("SELECT %s" , p->zReadExprlist);
149621    if( !zSql ){
149622      rc = SQLITE_NOMEM;
149623    }else{
149624      rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
149625      sqlite3_free(zSql);
149626    }
149627
149628    while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
149629      i64 iDocid = sqlite3_column_int64(pStmt, 0);
149630      int iLang = langidFromSelect(p, pStmt);
149631      int iCol;
149632
149633      for(iCol=0; rc==SQLITE_OK && iCol<p->nColumn; iCol++){
149634        if( p->abNotindexed[iCol]==0 ){
149635          const char *zText = (const char *)sqlite3_column_text(pStmt, iCol+1);
149636          int nText = sqlite3_column_bytes(pStmt, iCol+1);
149637          sqlite3_tokenizer_cursor *pT = 0;
149638
149639          rc = sqlite3Fts3OpenTokenizer(p->pTokenizer, iLang, zText, nText,&pT);
149640          while( rc==SQLITE_OK ){
149641            char const *zToken;       /* Buffer containing token */
149642            int nToken = 0;           /* Number of bytes in token */
149643            int iDum1 = 0, iDum2 = 0; /* Dummy variables */
149644            int iPos = 0;             /* Position of token in zText */
149645
149646            rc = pModule->xNext(pT, &zToken, &nToken, &iDum1, &iDum2, &iPos);
149647            if( rc==SQLITE_OK ){
149648              int i;
149649              cksum2 = cksum2 ^ fts3ChecksumEntry(
149650                  zToken, nToken, iLang, 0, iDocid, iCol, iPos
149651              );
149652              for(i=1; i<p->nIndex; i++){
149653                if( p->aIndex[i].nPrefix<=nToken ){
149654                  cksum2 = cksum2 ^ fts3ChecksumEntry(
149655                      zToken, p->aIndex[i].nPrefix, iLang, i, iDocid, iCol, iPos
149656                  );
149657                }
149658              }
149659            }
149660          }
149661          if( pT ) pModule->xClose(pT);
149662          if( rc==SQLITE_DONE ) rc = SQLITE_OK;
149663        }
149664      }
149665    }
149666
149667    sqlite3_finalize(pStmt);
149668  }
149669
149670  *pbOk = (cksum1==cksum2);
149671  return rc;
149672}
149673
149674/*
149675** Run the integrity-check. If no error occurs and the current contents of
149676** the FTS index are correct, return SQLITE_OK. Or, if the contents of the
149677** FTS index are incorrect, return SQLITE_CORRUPT_VTAB.
149678**
149679** Or, if an error (e.g. an OOM or IO error) occurs, return an SQLite
149680** error code.
149681**
149682** The integrity-check works as follows. For each token and indexed token
149683** prefix in the document set, a 64-bit checksum is calculated (by code
149684** in fts3ChecksumEntry()) based on the following:
149685**
149686**     + The index number (0 for the main index, 1 for the first prefix
149687**       index etc.),
149688**     + The token (or token prefix) text itself,
149689**     + The language-id of the row it appears in,
149690**     + The docid of the row it appears in,
149691**     + The column it appears in, and
149692**     + The tokens position within that column.
149693**
149694** The checksums for all entries in the index are XORed together to create
149695** a single checksum for the entire index.
149696**
149697** The integrity-check code calculates the same checksum in two ways:
149698**
149699**     1. By scanning the contents of the FTS index, and
149700**     2. By scanning and tokenizing the content table.
149701**
149702** If the two checksums are identical, the integrity-check is deemed to have
149703** passed.
149704*/
149705static int fts3DoIntegrityCheck(
149706  Fts3Table *p                    /* FTS3 table handle */
149707){
149708  int rc;
149709  int bOk = 0;
149710  rc = fts3IntegrityCheck(p, &bOk);
149711  if( rc==SQLITE_OK && bOk==0 ) rc = FTS_CORRUPT_VTAB;
149712  return rc;
149713}
149714
149715/*
149716** Handle a 'special' INSERT of the form:
149717**
149718**   "INSERT INTO tbl(tbl) VALUES(<expr>)"
149719**
149720** Argument pVal contains the result of <expr>. Currently the only
149721** meaningful value to insert is the text 'optimize'.
149722*/
149723static int fts3SpecialInsert(Fts3Table *p, sqlite3_value *pVal){
149724  int rc;                         /* Return Code */
149725  const char *zVal = (const char *)sqlite3_value_text(pVal);
149726  int nVal = sqlite3_value_bytes(pVal);
149727
149728  if( !zVal ){
149729    return SQLITE_NOMEM;
149730  }else if( nVal==8 && 0==sqlite3_strnicmp(zVal, "optimize", 8) ){
149731    rc = fts3DoOptimize(p, 0);
149732  }else if( nVal==7 && 0==sqlite3_strnicmp(zVal, "rebuild", 7) ){
149733    rc = fts3DoRebuild(p);
149734  }else if( nVal==15 && 0==sqlite3_strnicmp(zVal, "integrity-check", 15) ){
149735    rc = fts3DoIntegrityCheck(p);
149736  }else if( nVal>6 && 0==sqlite3_strnicmp(zVal, "merge=", 6) ){
149737    rc = fts3DoIncrmerge(p, &zVal[6]);
149738  }else if( nVal>10 && 0==sqlite3_strnicmp(zVal, "automerge=", 10) ){
149739    rc = fts3DoAutoincrmerge(p, &zVal[10]);
149740#ifdef SQLITE_TEST
149741  }else if( nVal>9 && 0==sqlite3_strnicmp(zVal, "nodesize=", 9) ){
149742    p->nNodeSize = atoi(&zVal[9]);
149743    rc = SQLITE_OK;
149744  }else if( nVal>11 && 0==sqlite3_strnicmp(zVal, "maxpending=", 9) ){
149745    p->nMaxPendingData = atoi(&zVal[11]);
149746    rc = SQLITE_OK;
149747  }else if( nVal>21 && 0==sqlite3_strnicmp(zVal, "test-no-incr-doclist=", 21) ){
149748    p->bNoIncrDoclist = atoi(&zVal[21]);
149749    rc = SQLITE_OK;
149750#endif
149751  }else{
149752    rc = SQLITE_ERROR;
149753  }
149754
149755  return rc;
149756}
149757
149758#ifndef SQLITE_DISABLE_FTS4_DEFERRED
149759/*
149760** Delete all cached deferred doclists. Deferred doclists are cached
149761** (allocated) by the sqlite3Fts3CacheDeferredDoclists() function.
149762*/
149763SQLITE_PRIVATE void sqlite3Fts3FreeDeferredDoclists(Fts3Cursor *pCsr){
149764  Fts3DeferredToken *pDef;
149765  for(pDef=pCsr->pDeferred; pDef; pDef=pDef->pNext){
149766    fts3PendingListDelete(pDef->pList);
149767    pDef->pList = 0;
149768  }
149769}
149770
149771/*
149772** Free all entries in the pCsr->pDeffered list. Entries are added to
149773** this list using sqlite3Fts3DeferToken().
149774*/
149775SQLITE_PRIVATE void sqlite3Fts3FreeDeferredTokens(Fts3Cursor *pCsr){
149776  Fts3DeferredToken *pDef;
149777  Fts3DeferredToken *pNext;
149778  for(pDef=pCsr->pDeferred; pDef; pDef=pNext){
149779    pNext = pDef->pNext;
149780    fts3PendingListDelete(pDef->pList);
149781    sqlite3_free(pDef);
149782  }
149783  pCsr->pDeferred = 0;
149784}
149785
149786/*
149787** Generate deferred-doclists for all tokens in the pCsr->pDeferred list
149788** based on the row that pCsr currently points to.
149789**
149790** A deferred-doclist is like any other doclist with position information
149791** included, except that it only contains entries for a single row of the
149792** table, not for all rows.
149793*/
149794SQLITE_PRIVATE int sqlite3Fts3CacheDeferredDoclists(Fts3Cursor *pCsr){
149795  int rc = SQLITE_OK;             /* Return code */
149796  if( pCsr->pDeferred ){
149797    int i;                        /* Used to iterate through table columns */
149798    sqlite3_int64 iDocid;         /* Docid of the row pCsr points to */
149799    Fts3DeferredToken *pDef;      /* Used to iterate through deferred tokens */
149800
149801    Fts3Table *p = (Fts3Table *)pCsr->base.pVtab;
149802    sqlite3_tokenizer *pT = p->pTokenizer;
149803    sqlite3_tokenizer_module const *pModule = pT->pModule;
149804
149805    assert( pCsr->isRequireSeek==0 );
149806    iDocid = sqlite3_column_int64(pCsr->pStmt, 0);
149807
149808    for(i=0; i<p->nColumn && rc==SQLITE_OK; i++){
149809      if( p->abNotindexed[i]==0 ){
149810        const char *zText = (const char *)sqlite3_column_text(pCsr->pStmt, i+1);
149811        sqlite3_tokenizer_cursor *pTC = 0;
149812
149813        rc = sqlite3Fts3OpenTokenizer(pT, pCsr->iLangid, zText, -1, &pTC);
149814        while( rc==SQLITE_OK ){
149815          char const *zToken;       /* Buffer containing token */
149816          int nToken = 0;           /* Number of bytes in token */
149817          int iDum1 = 0, iDum2 = 0; /* Dummy variables */
149818          int iPos = 0;             /* Position of token in zText */
149819
149820          rc = pModule->xNext(pTC, &zToken, &nToken, &iDum1, &iDum2, &iPos);
149821          for(pDef=pCsr->pDeferred; pDef && rc==SQLITE_OK; pDef=pDef->pNext){
149822            Fts3PhraseToken *pPT = pDef->pToken;
149823            if( (pDef->iCol>=p->nColumn || pDef->iCol==i)
149824                && (pPT->bFirst==0 || iPos==0)
149825                && (pPT->n==nToken || (pPT->isPrefix && pPT->n<nToken))
149826                && (0==memcmp(zToken, pPT->z, pPT->n))
149827              ){
149828              fts3PendingListAppend(&pDef->pList, iDocid, i, iPos, &rc);
149829            }
149830          }
149831        }
149832        if( pTC ) pModule->xClose(pTC);
149833        if( rc==SQLITE_DONE ) rc = SQLITE_OK;
149834      }
149835    }
149836
149837    for(pDef=pCsr->pDeferred; pDef && rc==SQLITE_OK; pDef=pDef->pNext){
149838      if( pDef->pList ){
149839        rc = fts3PendingListAppendVarint(&pDef->pList, 0);
149840      }
149841    }
149842  }
149843
149844  return rc;
149845}
149846
149847SQLITE_PRIVATE int sqlite3Fts3DeferredTokenList(
149848  Fts3DeferredToken *p,
149849  char **ppData,
149850  int *pnData
149851){
149852  char *pRet;
149853  int nSkip;
149854  sqlite3_int64 dummy;
149855
149856  *ppData = 0;
149857  *pnData = 0;
149858
149859  if( p->pList==0 ){
149860    return SQLITE_OK;
149861  }
149862
149863  pRet = (char *)sqlite3_malloc(p->pList->nData);
149864  if( !pRet ) return SQLITE_NOMEM;
149865
149866  nSkip = sqlite3Fts3GetVarint(p->pList->aData, &dummy);
149867  *pnData = p->pList->nData - nSkip;
149868  *ppData = pRet;
149869
149870  memcpy(pRet, &p->pList->aData[nSkip], *pnData);
149871  return SQLITE_OK;
149872}
149873
149874/*
149875** Add an entry for token pToken to the pCsr->pDeferred list.
149876*/
149877SQLITE_PRIVATE int sqlite3Fts3DeferToken(
149878  Fts3Cursor *pCsr,               /* Fts3 table cursor */
149879  Fts3PhraseToken *pToken,        /* Token to defer */
149880  int iCol                        /* Column that token must appear in (or -1) */
149881){
149882  Fts3DeferredToken *pDeferred;
149883  pDeferred = sqlite3_malloc(sizeof(*pDeferred));
149884  if( !pDeferred ){
149885    return SQLITE_NOMEM;
149886  }
149887  memset(pDeferred, 0, sizeof(*pDeferred));
149888  pDeferred->pToken = pToken;
149889  pDeferred->pNext = pCsr->pDeferred;
149890  pDeferred->iCol = iCol;
149891  pCsr->pDeferred = pDeferred;
149892
149893  assert( pToken->pDeferred==0 );
149894  pToken->pDeferred = pDeferred;
149895
149896  return SQLITE_OK;
149897}
149898#endif
149899
149900/*
149901** SQLite value pRowid contains the rowid of a row that may or may not be
149902** present in the FTS3 table. If it is, delete it and adjust the contents
149903** of subsiduary data structures accordingly.
149904*/
149905static int fts3DeleteByRowid(
149906  Fts3Table *p,
149907  sqlite3_value *pRowid,
149908  int *pnChng,                    /* IN/OUT: Decrement if row is deleted */
149909  u32 *aSzDel
149910){
149911  int rc = SQLITE_OK;             /* Return code */
149912  int bFound = 0;                 /* True if *pRowid really is in the table */
149913
149914  fts3DeleteTerms(&rc, p, pRowid, aSzDel, &bFound);
149915  if( bFound && rc==SQLITE_OK ){
149916    int isEmpty = 0;              /* Deleting *pRowid leaves the table empty */
149917    rc = fts3IsEmpty(p, pRowid, &isEmpty);
149918    if( rc==SQLITE_OK ){
149919      if( isEmpty ){
149920        /* Deleting this row means the whole table is empty. In this case
149921        ** delete the contents of all three tables and throw away any
149922        ** data in the pendingTerms hash table.  */
149923        rc = fts3DeleteAll(p, 1);
149924        *pnChng = 0;
149925        memset(aSzDel, 0, sizeof(u32) * (p->nColumn+1) * 2);
149926      }else{
149927        *pnChng = *pnChng - 1;
149928        if( p->zContentTbl==0 ){
149929          fts3SqlExec(&rc, p, SQL_DELETE_CONTENT, &pRowid);
149930        }
149931        if( p->bHasDocsize ){
149932          fts3SqlExec(&rc, p, SQL_DELETE_DOCSIZE, &pRowid);
149933        }
149934      }
149935    }
149936  }
149937
149938  return rc;
149939}
149940
149941/*
149942** This function does the work for the xUpdate method of FTS3 virtual
149943** tables. The schema of the virtual table being:
149944**
149945**     CREATE TABLE <table name>(
149946**       <user columns>,
149947**       <table name> HIDDEN,
149948**       docid HIDDEN,
149949**       <langid> HIDDEN
149950**     );
149951**
149952**
149953*/
149954SQLITE_PRIVATE int sqlite3Fts3UpdateMethod(
149955  sqlite3_vtab *pVtab,            /* FTS3 vtab object */
149956  int nArg,                       /* Size of argument array */
149957  sqlite3_value **apVal,          /* Array of arguments */
149958  sqlite_int64 *pRowid            /* OUT: The affected (or effected) rowid */
149959){
149960  Fts3Table *p = (Fts3Table *)pVtab;
149961  int rc = SQLITE_OK;             /* Return Code */
149962  int isRemove = 0;               /* True for an UPDATE or DELETE */
149963  u32 *aSzIns = 0;                /* Sizes of inserted documents */
149964  u32 *aSzDel = 0;                /* Sizes of deleted documents */
149965  int nChng = 0;                  /* Net change in number of documents */
149966  int bInsertDone = 0;
149967
149968  /* At this point it must be known if the %_stat table exists or not.
149969  ** So bHasStat may not be 2.  */
149970  assert( p->bHasStat==0 || p->bHasStat==1 );
149971
149972  assert( p->pSegments==0 );
149973  assert(
149974      nArg==1                     /* DELETE operations */
149975   || nArg==(2 + p->nColumn + 3)  /* INSERT or UPDATE operations */
149976  );
149977
149978  /* Check for a "special" INSERT operation. One of the form:
149979  **
149980  **   INSERT INTO xyz(xyz) VALUES('command');
149981  */
149982  if( nArg>1
149983   && sqlite3_value_type(apVal[0])==SQLITE_NULL
149984   && sqlite3_value_type(apVal[p->nColumn+2])!=SQLITE_NULL
149985  ){
149986    rc = fts3SpecialInsert(p, apVal[p->nColumn+2]);
149987    goto update_out;
149988  }
149989
149990  if( nArg>1 && sqlite3_value_int(apVal[2 + p->nColumn + 2])<0 ){
149991    rc = SQLITE_CONSTRAINT;
149992    goto update_out;
149993  }
149994
149995  /* Allocate space to hold the change in document sizes */
149996  aSzDel = sqlite3_malloc( sizeof(aSzDel[0])*(p->nColumn+1)*2 );
149997  if( aSzDel==0 ){
149998    rc = SQLITE_NOMEM;
149999    goto update_out;
150000  }
150001  aSzIns = &aSzDel[p->nColumn+1];
150002  memset(aSzDel, 0, sizeof(aSzDel[0])*(p->nColumn+1)*2);
150003
150004  rc = fts3Writelock(p);
150005  if( rc!=SQLITE_OK ) goto update_out;
150006
150007  /* If this is an INSERT operation, or an UPDATE that modifies the rowid
150008  ** value, then this operation requires constraint handling.
150009  **
150010  ** If the on-conflict mode is REPLACE, this means that the existing row
150011  ** should be deleted from the database before inserting the new row. Or,
150012  ** if the on-conflict mode is other than REPLACE, then this method must
150013  ** detect the conflict and return SQLITE_CONSTRAINT before beginning to
150014  ** modify the database file.
150015  */
150016  if( nArg>1 && p->zContentTbl==0 ){
150017    /* Find the value object that holds the new rowid value. */
150018    sqlite3_value *pNewRowid = apVal[3+p->nColumn];
150019    if( sqlite3_value_type(pNewRowid)==SQLITE_NULL ){
150020      pNewRowid = apVal[1];
150021    }
150022
150023    if( sqlite3_value_type(pNewRowid)!=SQLITE_NULL && (
150024        sqlite3_value_type(apVal[0])==SQLITE_NULL
150025     || sqlite3_value_int64(apVal[0])!=sqlite3_value_int64(pNewRowid)
150026    )){
150027      /* The new rowid is not NULL (in this case the rowid will be
150028      ** automatically assigned and there is no chance of a conflict), and
150029      ** the statement is either an INSERT or an UPDATE that modifies the
150030      ** rowid column. So if the conflict mode is REPLACE, then delete any
150031      ** existing row with rowid=pNewRowid.
150032      **
150033      ** Or, if the conflict mode is not REPLACE, insert the new record into
150034      ** the %_content table. If we hit the duplicate rowid constraint (or any
150035      ** other error) while doing so, return immediately.
150036      **
150037      ** This branch may also run if pNewRowid contains a value that cannot
150038      ** be losslessly converted to an integer. In this case, the eventual
150039      ** call to fts3InsertData() (either just below or further on in this
150040      ** function) will return SQLITE_MISMATCH. If fts3DeleteByRowid is
150041      ** invoked, it will delete zero rows (since no row will have
150042      ** docid=$pNewRowid if $pNewRowid is not an integer value).
150043      */
150044      if( sqlite3_vtab_on_conflict(p->db)==SQLITE_REPLACE ){
150045        rc = fts3DeleteByRowid(p, pNewRowid, &nChng, aSzDel);
150046      }else{
150047        rc = fts3InsertData(p, apVal, pRowid);
150048        bInsertDone = 1;
150049      }
150050    }
150051  }
150052  if( rc!=SQLITE_OK ){
150053    goto update_out;
150054  }
150055
150056  /* If this is a DELETE or UPDATE operation, remove the old record. */
150057  if( sqlite3_value_type(apVal[0])!=SQLITE_NULL ){
150058    assert( sqlite3_value_type(apVal[0])==SQLITE_INTEGER );
150059    rc = fts3DeleteByRowid(p, apVal[0], &nChng, aSzDel);
150060    isRemove = 1;
150061  }
150062
150063  /* If this is an INSERT or UPDATE operation, insert the new record. */
150064  if( nArg>1 && rc==SQLITE_OK ){
150065    int iLangid = sqlite3_value_int(apVal[2 + p->nColumn + 2]);
150066    if( bInsertDone==0 ){
150067      rc = fts3InsertData(p, apVal, pRowid);
150068      if( rc==SQLITE_CONSTRAINT && p->zContentTbl==0 ){
150069        rc = FTS_CORRUPT_VTAB;
150070      }
150071    }
150072    if( rc==SQLITE_OK && (!isRemove || *pRowid!=p->iPrevDocid ) ){
150073      rc = fts3PendingTermsDocid(p, iLangid, *pRowid);
150074    }
150075    if( rc==SQLITE_OK ){
150076      assert( p->iPrevDocid==*pRowid );
150077      rc = fts3InsertTerms(p, iLangid, apVal, aSzIns);
150078    }
150079    if( p->bHasDocsize ){
150080      fts3InsertDocsize(&rc, p, aSzIns);
150081    }
150082    nChng++;
150083  }
150084
150085  if( p->bFts4 ){
150086    fts3UpdateDocTotals(&rc, p, aSzIns, aSzDel, nChng);
150087  }
150088
150089 update_out:
150090  sqlite3_free(aSzDel);
150091  sqlite3Fts3SegmentsClose(p);
150092  return rc;
150093}
150094
150095/*
150096** Flush any data in the pending-terms hash table to disk. If successful,
150097** merge all segments in the database (including the new segment, if
150098** there was any data to flush) into a single segment.
150099*/
150100SQLITE_PRIVATE int sqlite3Fts3Optimize(Fts3Table *p){
150101  int rc;
150102  rc = sqlite3_exec(p->db, "SAVEPOINT fts3", 0, 0, 0);
150103  if( rc==SQLITE_OK ){
150104    rc = fts3DoOptimize(p, 1);
150105    if( rc==SQLITE_OK || rc==SQLITE_DONE ){
150106      int rc2 = sqlite3_exec(p->db, "RELEASE fts3", 0, 0, 0);
150107      if( rc2!=SQLITE_OK ) rc = rc2;
150108    }else{
150109      sqlite3_exec(p->db, "ROLLBACK TO fts3", 0, 0, 0);
150110      sqlite3_exec(p->db, "RELEASE fts3", 0, 0, 0);
150111    }
150112  }
150113  sqlite3Fts3SegmentsClose(p);
150114  return rc;
150115}
150116
150117#endif
150118
150119/************** End of fts3_write.c ******************************************/
150120/************** Begin file fts3_snippet.c ************************************/
150121/*
150122** 2009 Oct 23
150123**
150124** The author disclaims copyright to this source code.  In place of
150125** a legal notice, here is a blessing:
150126**
150127**    May you do good and not evil.
150128**    May you find forgiveness for yourself and forgive others.
150129**    May you share freely, never taking more than you give.
150130**
150131******************************************************************************
150132*/
150133
150134/* #include "fts3Int.h" */
150135#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
150136
150137/* #include <string.h> */
150138/* #include <assert.h> */
150139
150140/*
150141** Characters that may appear in the second argument to matchinfo().
150142*/
150143#define FTS3_MATCHINFO_NPHRASE   'p'        /* 1 value */
150144#define FTS3_MATCHINFO_NCOL      'c'        /* 1 value */
150145#define FTS3_MATCHINFO_NDOC      'n'        /* 1 value */
150146#define FTS3_MATCHINFO_AVGLENGTH 'a'        /* nCol values */
150147#define FTS3_MATCHINFO_LENGTH    'l'        /* nCol values */
150148#define FTS3_MATCHINFO_LCS       's'        /* nCol values */
150149#define FTS3_MATCHINFO_HITS      'x'        /* 3*nCol*nPhrase values */
150150#define FTS3_MATCHINFO_LHITS     'y'        /* nCol*nPhrase values */
150151#define FTS3_MATCHINFO_LHITS_BM  'b'        /* nCol*nPhrase values */
150152
150153/*
150154** The default value for the second argument to matchinfo().
150155*/
150156#define FTS3_MATCHINFO_DEFAULT   "pcx"
150157
150158
150159/*
150160** Used as an fts3ExprIterate() context when loading phrase doclists to
150161** Fts3Expr.aDoclist[]/nDoclist.
150162*/
150163typedef struct LoadDoclistCtx LoadDoclistCtx;
150164struct LoadDoclistCtx {
150165  Fts3Cursor *pCsr;               /* FTS3 Cursor */
150166  int nPhrase;                    /* Number of phrases seen so far */
150167  int nToken;                     /* Number of tokens seen so far */
150168};
150169
150170/*
150171** The following types are used as part of the implementation of the
150172** fts3BestSnippet() routine.
150173*/
150174typedef struct SnippetIter SnippetIter;
150175typedef struct SnippetPhrase SnippetPhrase;
150176typedef struct SnippetFragment SnippetFragment;
150177
150178struct SnippetIter {
150179  Fts3Cursor *pCsr;               /* Cursor snippet is being generated from */
150180  int iCol;                       /* Extract snippet from this column */
150181  int nSnippet;                   /* Requested snippet length (in tokens) */
150182  int nPhrase;                    /* Number of phrases in query */
150183  SnippetPhrase *aPhrase;         /* Array of size nPhrase */
150184  int iCurrent;                   /* First token of current snippet */
150185};
150186
150187struct SnippetPhrase {
150188  int nToken;                     /* Number of tokens in phrase */
150189  char *pList;                    /* Pointer to start of phrase position list */
150190  int iHead;                      /* Next value in position list */
150191  char *pHead;                    /* Position list data following iHead */
150192  int iTail;                      /* Next value in trailing position list */
150193  char *pTail;                    /* Position list data following iTail */
150194};
150195
150196struct SnippetFragment {
150197  int iCol;                       /* Column snippet is extracted from */
150198  int iPos;                       /* Index of first token in snippet */
150199  u64 covered;                    /* Mask of query phrases covered */
150200  u64 hlmask;                     /* Mask of snippet terms to highlight */
150201};
150202
150203/*
150204** This type is used as an fts3ExprIterate() context object while
150205** accumulating the data returned by the matchinfo() function.
150206*/
150207typedef struct MatchInfo MatchInfo;
150208struct MatchInfo {
150209  Fts3Cursor *pCursor;            /* FTS3 Cursor */
150210  int nCol;                       /* Number of columns in table */
150211  int nPhrase;                    /* Number of matchable phrases in query */
150212  sqlite3_int64 nDoc;             /* Number of docs in database */
150213  char flag;
150214  u32 *aMatchinfo;                /* Pre-allocated buffer */
150215};
150216
150217/*
150218** An instance of this structure is used to manage a pair of buffers, each
150219** (nElem * sizeof(u32)) bytes in size. See the MatchinfoBuffer code below
150220** for details.
150221*/
150222struct MatchinfoBuffer {
150223  u8 aRef[3];
150224  int nElem;
150225  int bGlobal;                    /* Set if global data is loaded */
150226  char *zMatchinfo;
150227  u32 aMatchinfo[1];
150228};
150229
150230
150231/*
150232** The snippet() and offsets() functions both return text values. An instance
150233** of the following structure is used to accumulate those values while the
150234** functions are running. See fts3StringAppend() for details.
150235*/
150236typedef struct StrBuffer StrBuffer;
150237struct StrBuffer {
150238  char *z;                        /* Pointer to buffer containing string */
150239  int n;                          /* Length of z in bytes (excl. nul-term) */
150240  int nAlloc;                     /* Allocated size of buffer z in bytes */
150241};
150242
150243
150244/*************************************************************************
150245** Start of MatchinfoBuffer code.
150246*/
150247
150248/*
150249** Allocate a two-slot MatchinfoBuffer object.
150250*/
150251static MatchinfoBuffer *fts3MIBufferNew(int nElem, const char *zMatchinfo){
150252  MatchinfoBuffer *pRet;
150253  int nByte = sizeof(u32) * (2*nElem + 1) + sizeof(MatchinfoBuffer);
150254  int nStr = (int)strlen(zMatchinfo);
150255
150256  pRet = sqlite3_malloc(nByte + nStr+1);
150257  if( pRet ){
150258    memset(pRet, 0, nByte);
150259    pRet->aMatchinfo[0] = (u8*)(&pRet->aMatchinfo[1]) - (u8*)pRet;
150260    pRet->aMatchinfo[1+nElem] = pRet->aMatchinfo[0] + sizeof(u32)*(nElem+1);
150261    pRet->nElem = nElem;
150262    pRet->zMatchinfo = ((char*)pRet) + nByte;
150263    memcpy(pRet->zMatchinfo, zMatchinfo, nStr+1);
150264    pRet->aRef[0] = 1;
150265  }
150266
150267  return pRet;
150268}
150269
150270static void fts3MIBufferFree(void *p){
150271  MatchinfoBuffer *pBuf = (MatchinfoBuffer*)((u8*)p - ((u32*)p)[-1]);
150272
150273  assert( (u32*)p==&pBuf->aMatchinfo[1]
150274       || (u32*)p==&pBuf->aMatchinfo[pBuf->nElem+2]
150275  );
150276  if( (u32*)p==&pBuf->aMatchinfo[1] ){
150277    pBuf->aRef[1] = 0;
150278  }else{
150279    pBuf->aRef[2] = 0;
150280  }
150281
150282  if( pBuf->aRef[0]==0 && pBuf->aRef[1]==0 && pBuf->aRef[2]==0 ){
150283    sqlite3_free(pBuf);
150284  }
150285}
150286
150287static void (*fts3MIBufferAlloc(MatchinfoBuffer *p, u32 **paOut))(void*){
150288  void (*xRet)(void*) = 0;
150289  u32 *aOut = 0;
150290
150291  if( p->aRef[1]==0 ){
150292    p->aRef[1] = 1;
150293    aOut = &p->aMatchinfo[1];
150294    xRet = fts3MIBufferFree;
150295  }
150296  else if( p->aRef[2]==0 ){
150297    p->aRef[2] = 1;
150298    aOut = &p->aMatchinfo[p->nElem+2];
150299    xRet = fts3MIBufferFree;
150300  }else{
150301    aOut = (u32*)sqlite3_malloc(p->nElem * sizeof(u32));
150302    if( aOut ){
150303      xRet = sqlite3_free;
150304      if( p->bGlobal ) memcpy(aOut, &p->aMatchinfo[1], p->nElem*sizeof(u32));
150305    }
150306  }
150307
150308  *paOut = aOut;
150309  return xRet;
150310}
150311
150312static void fts3MIBufferSetGlobal(MatchinfoBuffer *p){
150313  p->bGlobal = 1;
150314  memcpy(&p->aMatchinfo[2+p->nElem], &p->aMatchinfo[1], p->nElem*sizeof(u32));
150315}
150316
150317/*
150318** Free a MatchinfoBuffer object allocated using fts3MIBufferNew()
150319*/
150320SQLITE_PRIVATE void sqlite3Fts3MIBufferFree(MatchinfoBuffer *p){
150321  if( p ){
150322    assert( p->aRef[0]==1 );
150323    p->aRef[0] = 0;
150324    if( p->aRef[0]==0 && p->aRef[1]==0 && p->aRef[2]==0 ){
150325      sqlite3_free(p);
150326    }
150327  }
150328}
150329
150330/*
150331** End of MatchinfoBuffer code.
150332*************************************************************************/
150333
150334
150335/*
150336** This function is used to help iterate through a position-list. A position
150337** list is a list of unique integers, sorted from smallest to largest. Each
150338** element of the list is represented by an FTS3 varint that takes the value
150339** of the difference between the current element and the previous one plus
150340** two. For example, to store the position-list:
150341**
150342**     4 9 113
150343**
150344** the three varints:
150345**
150346**     6 7 106
150347**
150348** are encoded.
150349**
150350** When this function is called, *pp points to the start of an element of
150351** the list. *piPos contains the value of the previous entry in the list.
150352** After it returns, *piPos contains the value of the next element of the
150353** list and *pp is advanced to the following varint.
150354*/
150355static void fts3GetDeltaPosition(char **pp, int *piPos){
150356  int iVal;
150357  *pp += fts3GetVarint32(*pp, &iVal);
150358  *piPos += (iVal-2);
150359}
150360
150361/*
150362** Helper function for fts3ExprIterate() (see below).
150363*/
150364static int fts3ExprIterate2(
150365  Fts3Expr *pExpr,                /* Expression to iterate phrases of */
150366  int *piPhrase,                  /* Pointer to phrase counter */
150367  int (*x)(Fts3Expr*,int,void*),  /* Callback function to invoke for phrases */
150368  void *pCtx                      /* Second argument to pass to callback */
150369){
150370  int rc;                         /* Return code */
150371  int eType = pExpr->eType;     /* Type of expression node pExpr */
150372
150373  if( eType!=FTSQUERY_PHRASE ){
150374    assert( pExpr->pLeft && pExpr->pRight );
150375    rc = fts3ExprIterate2(pExpr->pLeft, piPhrase, x, pCtx);
150376    if( rc==SQLITE_OK && eType!=FTSQUERY_NOT ){
150377      rc = fts3ExprIterate2(pExpr->pRight, piPhrase, x, pCtx);
150378    }
150379  }else{
150380    rc = x(pExpr, *piPhrase, pCtx);
150381    (*piPhrase)++;
150382  }
150383  return rc;
150384}
150385
150386/*
150387** Iterate through all phrase nodes in an FTS3 query, except those that
150388** are part of a sub-tree that is the right-hand-side of a NOT operator.
150389** For each phrase node found, the supplied callback function is invoked.
150390**
150391** If the callback function returns anything other than SQLITE_OK,
150392** the iteration is abandoned and the error code returned immediately.
150393** Otherwise, SQLITE_OK is returned after a callback has been made for
150394** all eligible phrase nodes.
150395*/
150396static int fts3ExprIterate(
150397  Fts3Expr *pExpr,                /* Expression to iterate phrases of */
150398  int (*x)(Fts3Expr*,int,void*),  /* Callback function to invoke for phrases */
150399  void *pCtx                      /* Second argument to pass to callback */
150400){
150401  int iPhrase = 0;                /* Variable used as the phrase counter */
150402  return fts3ExprIterate2(pExpr, &iPhrase, x, pCtx);
150403}
150404
150405
150406/*
150407** This is an fts3ExprIterate() callback used while loading the doclists
150408** for each phrase into Fts3Expr.aDoclist[]/nDoclist. See also
150409** fts3ExprLoadDoclists().
150410*/
150411static int fts3ExprLoadDoclistsCb(Fts3Expr *pExpr, int iPhrase, void *ctx){
150412  int rc = SQLITE_OK;
150413  Fts3Phrase *pPhrase = pExpr->pPhrase;
150414  LoadDoclistCtx *p = (LoadDoclistCtx *)ctx;
150415
150416  UNUSED_PARAMETER(iPhrase);
150417
150418  p->nPhrase++;
150419  p->nToken += pPhrase->nToken;
150420
150421  return rc;
150422}
150423
150424/*
150425** Load the doclists for each phrase in the query associated with FTS3 cursor
150426** pCsr.
150427**
150428** If pnPhrase is not NULL, then *pnPhrase is set to the number of matchable
150429** phrases in the expression (all phrases except those directly or
150430** indirectly descended from the right-hand-side of a NOT operator). If
150431** pnToken is not NULL, then it is set to the number of tokens in all
150432** matchable phrases of the expression.
150433*/
150434static int fts3ExprLoadDoclists(
150435  Fts3Cursor *pCsr,               /* Fts3 cursor for current query */
150436  int *pnPhrase,                  /* OUT: Number of phrases in query */
150437  int *pnToken                    /* OUT: Number of tokens in query */
150438){
150439  int rc;                         /* Return Code */
150440  LoadDoclistCtx sCtx = {0,0,0};  /* Context for fts3ExprIterate() */
150441  sCtx.pCsr = pCsr;
150442  rc = fts3ExprIterate(pCsr->pExpr, fts3ExprLoadDoclistsCb, (void *)&sCtx);
150443  if( pnPhrase ) *pnPhrase = sCtx.nPhrase;
150444  if( pnToken ) *pnToken = sCtx.nToken;
150445  return rc;
150446}
150447
150448static int fts3ExprPhraseCountCb(Fts3Expr *pExpr, int iPhrase, void *ctx){
150449  (*(int *)ctx)++;
150450  pExpr->iPhrase = iPhrase;
150451  return SQLITE_OK;
150452}
150453static int fts3ExprPhraseCount(Fts3Expr *pExpr){
150454  int nPhrase = 0;
150455  (void)fts3ExprIterate(pExpr, fts3ExprPhraseCountCb, (void *)&nPhrase);
150456  return nPhrase;
150457}
150458
150459/*
150460** Advance the position list iterator specified by the first two
150461** arguments so that it points to the first element with a value greater
150462** than or equal to parameter iNext.
150463*/
150464static void fts3SnippetAdvance(char **ppIter, int *piIter, int iNext){
150465  char *pIter = *ppIter;
150466  if( pIter ){
150467    int iIter = *piIter;
150468
150469    while( iIter<iNext ){
150470      if( 0==(*pIter & 0xFE) ){
150471        iIter = -1;
150472        pIter = 0;
150473        break;
150474      }
150475      fts3GetDeltaPosition(&pIter, &iIter);
150476    }
150477
150478    *piIter = iIter;
150479    *ppIter = pIter;
150480  }
150481}
150482
150483/*
150484** Advance the snippet iterator to the next candidate snippet.
150485*/
150486static int fts3SnippetNextCandidate(SnippetIter *pIter){
150487  int i;                          /* Loop counter */
150488
150489  if( pIter->iCurrent<0 ){
150490    /* The SnippetIter object has just been initialized. The first snippet
150491    ** candidate always starts at offset 0 (even if this candidate has a
150492    ** score of 0.0).
150493    */
150494    pIter->iCurrent = 0;
150495
150496    /* Advance the 'head' iterator of each phrase to the first offset that
150497    ** is greater than or equal to (iNext+nSnippet).
150498    */
150499    for(i=0; i<pIter->nPhrase; i++){
150500      SnippetPhrase *pPhrase = &pIter->aPhrase[i];
150501      fts3SnippetAdvance(&pPhrase->pHead, &pPhrase->iHead, pIter->nSnippet);
150502    }
150503  }else{
150504    int iStart;
150505    int iEnd = 0x7FFFFFFF;
150506
150507    for(i=0; i<pIter->nPhrase; i++){
150508      SnippetPhrase *pPhrase = &pIter->aPhrase[i];
150509      if( pPhrase->pHead && pPhrase->iHead<iEnd ){
150510        iEnd = pPhrase->iHead;
150511      }
150512    }
150513    if( iEnd==0x7FFFFFFF ){
150514      return 1;
150515    }
150516
150517    pIter->iCurrent = iStart = iEnd - pIter->nSnippet + 1;
150518    for(i=0; i<pIter->nPhrase; i++){
150519      SnippetPhrase *pPhrase = &pIter->aPhrase[i];
150520      fts3SnippetAdvance(&pPhrase->pHead, &pPhrase->iHead, iEnd+1);
150521      fts3SnippetAdvance(&pPhrase->pTail, &pPhrase->iTail, iStart);
150522    }
150523  }
150524
150525  return 0;
150526}
150527
150528/*
150529** Retrieve information about the current candidate snippet of snippet
150530** iterator pIter.
150531*/
150532static void fts3SnippetDetails(
150533  SnippetIter *pIter,             /* Snippet iterator */
150534  u64 mCovered,                   /* Bitmask of phrases already covered */
150535  int *piToken,                   /* OUT: First token of proposed snippet */
150536  int *piScore,                   /* OUT: "Score" for this snippet */
150537  u64 *pmCover,                   /* OUT: Bitmask of phrases covered */
150538  u64 *pmHighlight                /* OUT: Bitmask of terms to highlight */
150539){
150540  int iStart = pIter->iCurrent;   /* First token of snippet */
150541  int iScore = 0;                 /* Score of this snippet */
150542  int i;                          /* Loop counter */
150543  u64 mCover = 0;                 /* Mask of phrases covered by this snippet */
150544  u64 mHighlight = 0;             /* Mask of tokens to highlight in snippet */
150545
150546  for(i=0; i<pIter->nPhrase; i++){
150547    SnippetPhrase *pPhrase = &pIter->aPhrase[i];
150548    if( pPhrase->pTail ){
150549      char *pCsr = pPhrase->pTail;
150550      int iCsr = pPhrase->iTail;
150551
150552      while( iCsr<(iStart+pIter->nSnippet) ){
150553        int j;
150554        u64 mPhrase = (u64)1 << i;
150555        u64 mPos = (u64)1 << (iCsr - iStart);
150556        assert( iCsr>=iStart );
150557        if( (mCover|mCovered)&mPhrase ){
150558          iScore++;
150559        }else{
150560          iScore += 1000;
150561        }
150562        mCover |= mPhrase;
150563
150564        for(j=0; j<pPhrase->nToken; j++){
150565          mHighlight |= (mPos>>j);
150566        }
150567
150568        if( 0==(*pCsr & 0x0FE) ) break;
150569        fts3GetDeltaPosition(&pCsr, &iCsr);
150570      }
150571    }
150572  }
150573
150574  /* Set the output variables before returning. */
150575  *piToken = iStart;
150576  *piScore = iScore;
150577  *pmCover = mCover;
150578  *pmHighlight = mHighlight;
150579}
150580
150581/*
150582** This function is an fts3ExprIterate() callback used by fts3BestSnippet().
150583** Each invocation populates an element of the SnippetIter.aPhrase[] array.
150584*/
150585static int fts3SnippetFindPositions(Fts3Expr *pExpr, int iPhrase, void *ctx){
150586  SnippetIter *p = (SnippetIter *)ctx;
150587  SnippetPhrase *pPhrase = &p->aPhrase[iPhrase];
150588  char *pCsr;
150589  int rc;
150590
150591  pPhrase->nToken = pExpr->pPhrase->nToken;
150592  rc = sqlite3Fts3EvalPhrasePoslist(p->pCsr, pExpr, p->iCol, &pCsr);
150593  assert( rc==SQLITE_OK || pCsr==0 );
150594  if( pCsr ){
150595    int iFirst = 0;
150596    pPhrase->pList = pCsr;
150597    fts3GetDeltaPosition(&pCsr, &iFirst);
150598    assert( iFirst>=0 );
150599    pPhrase->pHead = pCsr;
150600    pPhrase->pTail = pCsr;
150601    pPhrase->iHead = iFirst;
150602    pPhrase->iTail = iFirst;
150603  }else{
150604    assert( rc!=SQLITE_OK || (
150605       pPhrase->pList==0 && pPhrase->pHead==0 && pPhrase->pTail==0
150606    ));
150607  }
150608
150609  return rc;
150610}
150611
150612/*
150613** Select the fragment of text consisting of nFragment contiguous tokens
150614** from column iCol that represent the "best" snippet. The best snippet
150615** is the snippet with the highest score, where scores are calculated
150616** by adding:
150617**
150618**   (a) +1 point for each occurrence of a matchable phrase in the snippet.
150619**
150620**   (b) +1000 points for the first occurrence of each matchable phrase in
150621**       the snippet for which the corresponding mCovered bit is not set.
150622**
150623** The selected snippet parameters are stored in structure *pFragment before
150624** returning. The score of the selected snippet is stored in *piScore
150625** before returning.
150626*/
150627static int fts3BestSnippet(
150628  int nSnippet,                   /* Desired snippet length */
150629  Fts3Cursor *pCsr,               /* Cursor to create snippet for */
150630  int iCol,                       /* Index of column to create snippet from */
150631  u64 mCovered,                   /* Mask of phrases already covered */
150632  u64 *pmSeen,                    /* IN/OUT: Mask of phrases seen */
150633  SnippetFragment *pFragment,     /* OUT: Best snippet found */
150634  int *piScore                    /* OUT: Score of snippet pFragment */
150635){
150636  int rc;                         /* Return Code */
150637  int nList;                      /* Number of phrases in expression */
150638  SnippetIter sIter;              /* Iterates through snippet candidates */
150639  int nByte;                      /* Number of bytes of space to allocate */
150640  int iBestScore = -1;            /* Best snippet score found so far */
150641  int i;                          /* Loop counter */
150642
150643  memset(&sIter, 0, sizeof(sIter));
150644
150645  /* Iterate through the phrases in the expression to count them. The same
150646  ** callback makes sure the doclists are loaded for each phrase.
150647  */
150648  rc = fts3ExprLoadDoclists(pCsr, &nList, 0);
150649  if( rc!=SQLITE_OK ){
150650    return rc;
150651  }
150652
150653  /* Now that it is known how many phrases there are, allocate and zero
150654  ** the required space using malloc().
150655  */
150656  nByte = sizeof(SnippetPhrase) * nList;
150657  sIter.aPhrase = (SnippetPhrase *)sqlite3_malloc(nByte);
150658  if( !sIter.aPhrase ){
150659    return SQLITE_NOMEM;
150660  }
150661  memset(sIter.aPhrase, 0, nByte);
150662
150663  /* Initialize the contents of the SnippetIter object. Then iterate through
150664  ** the set of phrases in the expression to populate the aPhrase[] array.
150665  */
150666  sIter.pCsr = pCsr;
150667  sIter.iCol = iCol;
150668  sIter.nSnippet = nSnippet;
150669  sIter.nPhrase = nList;
150670  sIter.iCurrent = -1;
150671  rc = fts3ExprIterate(pCsr->pExpr, fts3SnippetFindPositions, (void*)&sIter);
150672  if( rc==SQLITE_OK ){
150673
150674    /* Set the *pmSeen output variable. */
150675    for(i=0; i<nList; i++){
150676      if( sIter.aPhrase[i].pHead ){
150677        *pmSeen |= (u64)1 << i;
150678      }
150679    }
150680
150681    /* Loop through all candidate snippets. Store the best snippet in
150682     ** *pFragment. Store its associated 'score' in iBestScore.
150683     */
150684    pFragment->iCol = iCol;
150685    while( !fts3SnippetNextCandidate(&sIter) ){
150686      int iPos;
150687      int iScore;
150688      u64 mCover;
150689      u64 mHighlite;
150690      fts3SnippetDetails(&sIter, mCovered, &iPos, &iScore, &mCover,&mHighlite);
150691      assert( iScore>=0 );
150692      if( iScore>iBestScore ){
150693        pFragment->iPos = iPos;
150694        pFragment->hlmask = mHighlite;
150695        pFragment->covered = mCover;
150696        iBestScore = iScore;
150697      }
150698    }
150699
150700    *piScore = iBestScore;
150701  }
150702  sqlite3_free(sIter.aPhrase);
150703  return rc;
150704}
150705
150706
150707/*
150708** Append a string to the string-buffer passed as the first argument.
150709**
150710** If nAppend is negative, then the length of the string zAppend is
150711** determined using strlen().
150712*/
150713static int fts3StringAppend(
150714  StrBuffer *pStr,                /* Buffer to append to */
150715  const char *zAppend,            /* Pointer to data to append to buffer */
150716  int nAppend                     /* Size of zAppend in bytes (or -1) */
150717){
150718  if( nAppend<0 ){
150719    nAppend = (int)strlen(zAppend);
150720  }
150721
150722  /* If there is insufficient space allocated at StrBuffer.z, use realloc()
150723  ** to grow the buffer until so that it is big enough to accomadate the
150724  ** appended data.
150725  */
150726  if( pStr->n+nAppend+1>=pStr->nAlloc ){
150727    int nAlloc = pStr->nAlloc+nAppend+100;
150728    char *zNew = sqlite3_realloc(pStr->z, nAlloc);
150729    if( !zNew ){
150730      return SQLITE_NOMEM;
150731    }
150732    pStr->z = zNew;
150733    pStr->nAlloc = nAlloc;
150734  }
150735  assert( pStr->z!=0 && (pStr->nAlloc >= pStr->n+nAppend+1) );
150736
150737  /* Append the data to the string buffer. */
150738  memcpy(&pStr->z[pStr->n], zAppend, nAppend);
150739  pStr->n += nAppend;
150740  pStr->z[pStr->n] = '\0';
150741
150742  return SQLITE_OK;
150743}
150744
150745/*
150746** The fts3BestSnippet() function often selects snippets that end with a
150747** query term. That is, the final term of the snippet is always a term
150748** that requires highlighting. For example, if 'X' is a highlighted term
150749** and '.' is a non-highlighted term, BestSnippet() may select:
150750**
150751**     ........X.....X
150752**
150753** This function "shifts" the beginning of the snippet forward in the
150754** document so that there are approximately the same number of
150755** non-highlighted terms to the right of the final highlighted term as there
150756** are to the left of the first highlighted term. For example, to this:
150757**
150758**     ....X.....X....
150759**
150760** This is done as part of extracting the snippet text, not when selecting
150761** the snippet. Snippet selection is done based on doclists only, so there
150762** is no way for fts3BestSnippet() to know whether or not the document
150763** actually contains terms that follow the final highlighted term.
150764*/
150765static int fts3SnippetShift(
150766  Fts3Table *pTab,                /* FTS3 table snippet comes from */
150767  int iLangid,                    /* Language id to use in tokenizing */
150768  int nSnippet,                   /* Number of tokens desired for snippet */
150769  const char *zDoc,               /* Document text to extract snippet from */
150770  int nDoc,                       /* Size of buffer zDoc in bytes */
150771  int *piPos,                     /* IN/OUT: First token of snippet */
150772  u64 *pHlmask                    /* IN/OUT: Mask of tokens to highlight */
150773){
150774  u64 hlmask = *pHlmask;          /* Local copy of initial highlight-mask */
150775
150776  if( hlmask ){
150777    int nLeft;                    /* Tokens to the left of first highlight */
150778    int nRight;                   /* Tokens to the right of last highlight */
150779    int nDesired;                 /* Ideal number of tokens to shift forward */
150780
150781    for(nLeft=0; !(hlmask & ((u64)1 << nLeft)); nLeft++);
150782    for(nRight=0; !(hlmask & ((u64)1 << (nSnippet-1-nRight))); nRight++);
150783    nDesired = (nLeft-nRight)/2;
150784
150785    /* Ideally, the start of the snippet should be pushed forward in the
150786    ** document nDesired tokens. This block checks if there are actually
150787    ** nDesired tokens to the right of the snippet. If so, *piPos and
150788    ** *pHlMask are updated to shift the snippet nDesired tokens to the
150789    ** right. Otherwise, the snippet is shifted by the number of tokens
150790    ** available.
150791    */
150792    if( nDesired>0 ){
150793      int nShift;                 /* Number of tokens to shift snippet by */
150794      int iCurrent = 0;           /* Token counter */
150795      int rc;                     /* Return Code */
150796      sqlite3_tokenizer_module *pMod;
150797      sqlite3_tokenizer_cursor *pC;
150798      pMod = (sqlite3_tokenizer_module *)pTab->pTokenizer->pModule;
150799
150800      /* Open a cursor on zDoc/nDoc. Check if there are (nSnippet+nDesired)
150801      ** or more tokens in zDoc/nDoc.
150802      */
150803      rc = sqlite3Fts3OpenTokenizer(pTab->pTokenizer, iLangid, zDoc, nDoc, &pC);
150804      if( rc!=SQLITE_OK ){
150805        return rc;
150806      }
150807      while( rc==SQLITE_OK && iCurrent<(nSnippet+nDesired) ){
150808        const char *ZDUMMY; int DUMMY1 = 0, DUMMY2 = 0, DUMMY3 = 0;
150809        rc = pMod->xNext(pC, &ZDUMMY, &DUMMY1, &DUMMY2, &DUMMY3, &iCurrent);
150810      }
150811      pMod->xClose(pC);
150812      if( rc!=SQLITE_OK && rc!=SQLITE_DONE ){ return rc; }
150813
150814      nShift = (rc==SQLITE_DONE)+iCurrent-nSnippet;
150815      assert( nShift<=nDesired );
150816      if( nShift>0 ){
150817        *piPos += nShift;
150818        *pHlmask = hlmask >> nShift;
150819      }
150820    }
150821  }
150822  return SQLITE_OK;
150823}
150824
150825/*
150826** Extract the snippet text for fragment pFragment from cursor pCsr and
150827** append it to string buffer pOut.
150828*/
150829static int fts3SnippetText(
150830  Fts3Cursor *pCsr,               /* FTS3 Cursor */
150831  SnippetFragment *pFragment,     /* Snippet to extract */
150832  int iFragment,                  /* Fragment number */
150833  int isLast,                     /* True for final fragment in snippet */
150834  int nSnippet,                   /* Number of tokens in extracted snippet */
150835  const char *zOpen,              /* String inserted before highlighted term */
150836  const char *zClose,             /* String inserted after highlighted term */
150837  const char *zEllipsis,          /* String inserted between snippets */
150838  StrBuffer *pOut                 /* Write output here */
150839){
150840  Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
150841  int rc;                         /* Return code */
150842  const char *zDoc;               /* Document text to extract snippet from */
150843  int nDoc;                       /* Size of zDoc in bytes */
150844  int iCurrent = 0;               /* Current token number of document */
150845  int iEnd = 0;                   /* Byte offset of end of current token */
150846  int isShiftDone = 0;            /* True after snippet is shifted */
150847  int iPos = pFragment->iPos;     /* First token of snippet */
150848  u64 hlmask = pFragment->hlmask; /* Highlight-mask for snippet */
150849  int iCol = pFragment->iCol+1;   /* Query column to extract text from */
150850  sqlite3_tokenizer_module *pMod; /* Tokenizer module methods object */
150851  sqlite3_tokenizer_cursor *pC;   /* Tokenizer cursor open on zDoc/nDoc */
150852
150853  zDoc = (const char *)sqlite3_column_text(pCsr->pStmt, iCol);
150854  if( zDoc==0 ){
150855    if( sqlite3_column_type(pCsr->pStmt, iCol)!=SQLITE_NULL ){
150856      return SQLITE_NOMEM;
150857    }
150858    return SQLITE_OK;
150859  }
150860  nDoc = sqlite3_column_bytes(pCsr->pStmt, iCol);
150861
150862  /* Open a token cursor on the document. */
150863  pMod = (sqlite3_tokenizer_module *)pTab->pTokenizer->pModule;
150864  rc = sqlite3Fts3OpenTokenizer(pTab->pTokenizer, pCsr->iLangid, zDoc,nDoc,&pC);
150865  if( rc!=SQLITE_OK ){
150866    return rc;
150867  }
150868
150869  while( rc==SQLITE_OK ){
150870    const char *ZDUMMY;           /* Dummy argument used with tokenizer */
150871    int DUMMY1 = -1;              /* Dummy argument used with tokenizer */
150872    int iBegin = 0;               /* Offset in zDoc of start of token */
150873    int iFin = 0;                 /* Offset in zDoc of end of token */
150874    int isHighlight = 0;          /* True for highlighted terms */
150875
150876    /* Variable DUMMY1 is initialized to a negative value above. Elsewhere
150877    ** in the FTS code the variable that the third argument to xNext points to
150878    ** is initialized to zero before the first (*but not necessarily
150879    ** subsequent*) call to xNext(). This is done for a particular application
150880    ** that needs to know whether or not the tokenizer is being used for
150881    ** snippet generation or for some other purpose.
150882    **
150883    ** Extreme care is required when writing code to depend on this
150884    ** initialization. It is not a documented part of the tokenizer interface.
150885    ** If a tokenizer is used directly by any code outside of FTS, this
150886    ** convention might not be respected.  */
150887    rc = pMod->xNext(pC, &ZDUMMY, &DUMMY1, &iBegin, &iFin, &iCurrent);
150888    if( rc!=SQLITE_OK ){
150889      if( rc==SQLITE_DONE ){
150890        /* Special case - the last token of the snippet is also the last token
150891        ** of the column. Append any punctuation that occurred between the end
150892        ** of the previous token and the end of the document to the output.
150893        ** Then break out of the loop. */
150894        rc = fts3StringAppend(pOut, &zDoc[iEnd], -1);
150895      }
150896      break;
150897    }
150898    if( iCurrent<iPos ){ continue; }
150899
150900    if( !isShiftDone ){
150901      int n = nDoc - iBegin;
150902      rc = fts3SnippetShift(
150903          pTab, pCsr->iLangid, nSnippet, &zDoc[iBegin], n, &iPos, &hlmask
150904      );
150905      isShiftDone = 1;
150906
150907      /* Now that the shift has been done, check if the initial "..." are
150908      ** required. They are required if (a) this is not the first fragment,
150909      ** or (b) this fragment does not begin at position 0 of its column.
150910      */
150911      if( rc==SQLITE_OK ){
150912        if( iPos>0 || iFragment>0 ){
150913          rc = fts3StringAppend(pOut, zEllipsis, -1);
150914        }else if( iBegin ){
150915          rc = fts3StringAppend(pOut, zDoc, iBegin);
150916        }
150917      }
150918      if( rc!=SQLITE_OK || iCurrent<iPos ) continue;
150919    }
150920
150921    if( iCurrent>=(iPos+nSnippet) ){
150922      if( isLast ){
150923        rc = fts3StringAppend(pOut, zEllipsis, -1);
150924      }
150925      break;
150926    }
150927
150928    /* Set isHighlight to true if this term should be highlighted. */
150929    isHighlight = (hlmask & ((u64)1 << (iCurrent-iPos)))!=0;
150930
150931    if( iCurrent>iPos ) rc = fts3StringAppend(pOut, &zDoc[iEnd], iBegin-iEnd);
150932    if( rc==SQLITE_OK && isHighlight ) rc = fts3StringAppend(pOut, zOpen, -1);
150933    if( rc==SQLITE_OK ) rc = fts3StringAppend(pOut, &zDoc[iBegin], iFin-iBegin);
150934    if( rc==SQLITE_OK && isHighlight ) rc = fts3StringAppend(pOut, zClose, -1);
150935
150936    iEnd = iFin;
150937  }
150938
150939  pMod->xClose(pC);
150940  return rc;
150941}
150942
150943
150944/*
150945** This function is used to count the entries in a column-list (a
150946** delta-encoded list of term offsets within a single column of a single
150947** row). When this function is called, *ppCollist should point to the
150948** beginning of the first varint in the column-list (the varint that
150949** contains the position of the first matching term in the column data).
150950** Before returning, *ppCollist is set to point to the first byte after
150951** the last varint in the column-list (either the 0x00 signifying the end
150952** of the position-list, or the 0x01 that precedes the column number of
150953** the next column in the position-list).
150954**
150955** The number of elements in the column-list is returned.
150956*/
150957static int fts3ColumnlistCount(char **ppCollist){
150958  char *pEnd = *ppCollist;
150959  char c = 0;
150960  int nEntry = 0;
150961
150962  /* A column-list is terminated by either a 0x01 or 0x00. */
150963  while( 0xFE & (*pEnd | c) ){
150964    c = *pEnd++ & 0x80;
150965    if( !c ) nEntry++;
150966  }
150967
150968  *ppCollist = pEnd;
150969  return nEntry;
150970}
150971
150972/*
150973** This function gathers 'y' or 'b' data for a single phrase.
150974*/
150975static void fts3ExprLHits(
150976  Fts3Expr *pExpr,                /* Phrase expression node */
150977  MatchInfo *p                    /* Matchinfo context */
150978){
150979  Fts3Table *pTab = (Fts3Table *)p->pCursor->base.pVtab;
150980  int iStart;
150981  Fts3Phrase *pPhrase = pExpr->pPhrase;
150982  char *pIter = pPhrase->doclist.pList;
150983  int iCol = 0;
150984
150985  assert( p->flag==FTS3_MATCHINFO_LHITS_BM || p->flag==FTS3_MATCHINFO_LHITS );
150986  if( p->flag==FTS3_MATCHINFO_LHITS ){
150987    iStart = pExpr->iPhrase * p->nCol;
150988  }else{
150989    iStart = pExpr->iPhrase * ((p->nCol + 31) / 32);
150990  }
150991
150992  while( 1 ){
150993    int nHit = fts3ColumnlistCount(&pIter);
150994    if( (pPhrase->iColumn>=pTab->nColumn || pPhrase->iColumn==iCol) ){
150995      if( p->flag==FTS3_MATCHINFO_LHITS ){
150996        p->aMatchinfo[iStart + iCol] = (u32)nHit;
150997      }else if( nHit ){
150998        p->aMatchinfo[iStart + (iCol+1)/32] |= (1 << (iCol&0x1F));
150999      }
151000    }
151001    assert( *pIter==0x00 || *pIter==0x01 );
151002    if( *pIter!=0x01 ) break;
151003    pIter++;
151004    pIter += fts3GetVarint32(pIter, &iCol);
151005  }
151006}
151007
151008/*
151009** Gather the results for matchinfo directives 'y' and 'b'.
151010*/
151011static void fts3ExprLHitGather(
151012  Fts3Expr *pExpr,
151013  MatchInfo *p
151014){
151015  assert( (pExpr->pLeft==0)==(pExpr->pRight==0) );
151016  if( pExpr->bEof==0 && pExpr->iDocid==p->pCursor->iPrevId ){
151017    if( pExpr->pLeft ){
151018      fts3ExprLHitGather(pExpr->pLeft, p);
151019      fts3ExprLHitGather(pExpr->pRight, p);
151020    }else{
151021      fts3ExprLHits(pExpr, p);
151022    }
151023  }
151024}
151025
151026/*
151027** fts3ExprIterate() callback used to collect the "global" matchinfo stats
151028** for a single query.
151029**
151030** fts3ExprIterate() callback to load the 'global' elements of a
151031** FTS3_MATCHINFO_HITS matchinfo array. The global stats are those elements
151032** of the matchinfo array that are constant for all rows returned by the
151033** current query.
151034**
151035** Argument pCtx is actually a pointer to a struct of type MatchInfo. This
151036** function populates Matchinfo.aMatchinfo[] as follows:
151037**
151038**   for(iCol=0; iCol<nCol; iCol++){
151039**     aMatchinfo[3*iPhrase*nCol + 3*iCol + 1] = X;
151040**     aMatchinfo[3*iPhrase*nCol + 3*iCol + 2] = Y;
151041**   }
151042**
151043** where X is the number of matches for phrase iPhrase is column iCol of all
151044** rows of the table. Y is the number of rows for which column iCol contains
151045** at least one instance of phrase iPhrase.
151046**
151047** If the phrase pExpr consists entirely of deferred tokens, then all X and
151048** Y values are set to nDoc, where nDoc is the number of documents in the
151049** file system. This is done because the full-text index doclist is required
151050** to calculate these values properly, and the full-text index doclist is
151051** not available for deferred tokens.
151052*/
151053static int fts3ExprGlobalHitsCb(
151054  Fts3Expr *pExpr,                /* Phrase expression node */
151055  int iPhrase,                    /* Phrase number (numbered from zero) */
151056  void *pCtx                      /* Pointer to MatchInfo structure */
151057){
151058  MatchInfo *p = (MatchInfo *)pCtx;
151059  return sqlite3Fts3EvalPhraseStats(
151060      p->pCursor, pExpr, &p->aMatchinfo[3*iPhrase*p->nCol]
151061  );
151062}
151063
151064/*
151065** fts3ExprIterate() callback used to collect the "local" part of the
151066** FTS3_MATCHINFO_HITS array. The local stats are those elements of the
151067** array that are different for each row returned by the query.
151068*/
151069static int fts3ExprLocalHitsCb(
151070  Fts3Expr *pExpr,                /* Phrase expression node */
151071  int iPhrase,                    /* Phrase number */
151072  void *pCtx                      /* Pointer to MatchInfo structure */
151073){
151074  int rc = SQLITE_OK;
151075  MatchInfo *p = (MatchInfo *)pCtx;
151076  int iStart = iPhrase * p->nCol * 3;
151077  int i;
151078
151079  for(i=0; i<p->nCol && rc==SQLITE_OK; i++){
151080    char *pCsr;
151081    rc = sqlite3Fts3EvalPhrasePoslist(p->pCursor, pExpr, i, &pCsr);
151082    if( pCsr ){
151083      p->aMatchinfo[iStart+i*3] = fts3ColumnlistCount(&pCsr);
151084    }else{
151085      p->aMatchinfo[iStart+i*3] = 0;
151086    }
151087  }
151088
151089  return rc;
151090}
151091
151092static int fts3MatchinfoCheck(
151093  Fts3Table *pTab,
151094  char cArg,
151095  char **pzErr
151096){
151097  if( (cArg==FTS3_MATCHINFO_NPHRASE)
151098   || (cArg==FTS3_MATCHINFO_NCOL)
151099   || (cArg==FTS3_MATCHINFO_NDOC && pTab->bFts4)
151100   || (cArg==FTS3_MATCHINFO_AVGLENGTH && pTab->bFts4)
151101   || (cArg==FTS3_MATCHINFO_LENGTH && pTab->bHasDocsize)
151102   || (cArg==FTS3_MATCHINFO_LCS)
151103   || (cArg==FTS3_MATCHINFO_HITS)
151104   || (cArg==FTS3_MATCHINFO_LHITS)
151105   || (cArg==FTS3_MATCHINFO_LHITS_BM)
151106  ){
151107    return SQLITE_OK;
151108  }
151109  sqlite3Fts3ErrMsg(pzErr, "unrecognized matchinfo request: %c", cArg);
151110  return SQLITE_ERROR;
151111}
151112
151113static int fts3MatchinfoSize(MatchInfo *pInfo, char cArg){
151114  int nVal;                       /* Number of integers output by cArg */
151115
151116  switch( cArg ){
151117    case FTS3_MATCHINFO_NDOC:
151118    case FTS3_MATCHINFO_NPHRASE:
151119    case FTS3_MATCHINFO_NCOL:
151120      nVal = 1;
151121      break;
151122
151123    case FTS3_MATCHINFO_AVGLENGTH:
151124    case FTS3_MATCHINFO_LENGTH:
151125    case FTS3_MATCHINFO_LCS:
151126      nVal = pInfo->nCol;
151127      break;
151128
151129    case FTS3_MATCHINFO_LHITS:
151130      nVal = pInfo->nCol * pInfo->nPhrase;
151131      break;
151132
151133    case FTS3_MATCHINFO_LHITS_BM:
151134      nVal = pInfo->nPhrase * ((pInfo->nCol + 31) / 32);
151135      break;
151136
151137    default:
151138      assert( cArg==FTS3_MATCHINFO_HITS );
151139      nVal = pInfo->nCol * pInfo->nPhrase * 3;
151140      break;
151141  }
151142
151143  return nVal;
151144}
151145
151146static int fts3MatchinfoSelectDoctotal(
151147  Fts3Table *pTab,
151148  sqlite3_stmt **ppStmt,
151149  sqlite3_int64 *pnDoc,
151150  const char **paLen
151151){
151152  sqlite3_stmt *pStmt;
151153  const char *a;
151154  sqlite3_int64 nDoc;
151155
151156  if( !*ppStmt ){
151157    int rc = sqlite3Fts3SelectDoctotal(pTab, ppStmt);
151158    if( rc!=SQLITE_OK ) return rc;
151159  }
151160  pStmt = *ppStmt;
151161  assert( sqlite3_data_count(pStmt)==1 );
151162
151163  a = sqlite3_column_blob(pStmt, 0);
151164  a += sqlite3Fts3GetVarint(a, &nDoc);
151165  if( nDoc==0 ) return FTS_CORRUPT_VTAB;
151166  *pnDoc = (u32)nDoc;
151167
151168  if( paLen ) *paLen = a;
151169  return SQLITE_OK;
151170}
151171
151172/*
151173** An instance of the following structure is used to store state while
151174** iterating through a multi-column position-list corresponding to the
151175** hits for a single phrase on a single row in order to calculate the
151176** values for a matchinfo() FTS3_MATCHINFO_LCS request.
151177*/
151178typedef struct LcsIterator LcsIterator;
151179struct LcsIterator {
151180  Fts3Expr *pExpr;                /* Pointer to phrase expression */
151181  int iPosOffset;                 /* Tokens count up to end of this phrase */
151182  char *pRead;                    /* Cursor used to iterate through aDoclist */
151183  int iPos;                       /* Current position */
151184};
151185
151186/*
151187** If LcsIterator.iCol is set to the following value, the iterator has
151188** finished iterating through all offsets for all columns.
151189*/
151190#define LCS_ITERATOR_FINISHED 0x7FFFFFFF;
151191
151192static int fts3MatchinfoLcsCb(
151193  Fts3Expr *pExpr,                /* Phrase expression node */
151194  int iPhrase,                    /* Phrase number (numbered from zero) */
151195  void *pCtx                      /* Pointer to MatchInfo structure */
151196){
151197  LcsIterator *aIter = (LcsIterator *)pCtx;
151198  aIter[iPhrase].pExpr = pExpr;
151199  return SQLITE_OK;
151200}
151201
151202/*
151203** Advance the iterator passed as an argument to the next position. Return
151204** 1 if the iterator is at EOF or if it now points to the start of the
151205** position list for the next column.
151206*/
151207static int fts3LcsIteratorAdvance(LcsIterator *pIter){
151208  char *pRead = pIter->pRead;
151209  sqlite3_int64 iRead;
151210  int rc = 0;
151211
151212  pRead += sqlite3Fts3GetVarint(pRead, &iRead);
151213  if( iRead==0 || iRead==1 ){
151214    pRead = 0;
151215    rc = 1;
151216  }else{
151217    pIter->iPos += (int)(iRead-2);
151218  }
151219
151220  pIter->pRead = pRead;
151221  return rc;
151222}
151223
151224/*
151225** This function implements the FTS3_MATCHINFO_LCS matchinfo() flag.
151226**
151227** If the call is successful, the longest-common-substring lengths for each
151228** column are written into the first nCol elements of the pInfo->aMatchinfo[]
151229** array before returning. SQLITE_OK is returned in this case.
151230**
151231** Otherwise, if an error occurs, an SQLite error code is returned and the
151232** data written to the first nCol elements of pInfo->aMatchinfo[] is
151233** undefined.
151234*/
151235static int fts3MatchinfoLcs(Fts3Cursor *pCsr, MatchInfo *pInfo){
151236  LcsIterator *aIter;
151237  int i;
151238  int iCol;
151239  int nToken = 0;
151240
151241  /* Allocate and populate the array of LcsIterator objects. The array
151242  ** contains one element for each matchable phrase in the query.
151243  **/
151244  aIter = sqlite3_malloc(sizeof(LcsIterator) * pCsr->nPhrase);
151245  if( !aIter ) return SQLITE_NOMEM;
151246  memset(aIter, 0, sizeof(LcsIterator) * pCsr->nPhrase);
151247  (void)fts3ExprIterate(pCsr->pExpr, fts3MatchinfoLcsCb, (void*)aIter);
151248
151249  for(i=0; i<pInfo->nPhrase; i++){
151250    LcsIterator *pIter = &aIter[i];
151251    nToken -= pIter->pExpr->pPhrase->nToken;
151252    pIter->iPosOffset = nToken;
151253  }
151254
151255  for(iCol=0; iCol<pInfo->nCol; iCol++){
151256    int nLcs = 0;                 /* LCS value for this column */
151257    int nLive = 0;                /* Number of iterators in aIter not at EOF */
151258
151259    for(i=0; i<pInfo->nPhrase; i++){
151260      int rc;
151261      LcsIterator *pIt = &aIter[i];
151262      rc = sqlite3Fts3EvalPhrasePoslist(pCsr, pIt->pExpr, iCol, &pIt->pRead);
151263      if( rc!=SQLITE_OK ) return rc;
151264      if( pIt->pRead ){
151265        pIt->iPos = pIt->iPosOffset;
151266        fts3LcsIteratorAdvance(&aIter[i]);
151267        nLive++;
151268      }
151269    }
151270
151271    while( nLive>0 ){
151272      LcsIterator *pAdv = 0;      /* The iterator to advance by one position */
151273      int nThisLcs = 0;           /* LCS for the current iterator positions */
151274
151275      for(i=0; i<pInfo->nPhrase; i++){
151276        LcsIterator *pIter = &aIter[i];
151277        if( pIter->pRead==0 ){
151278          /* This iterator is already at EOF for this column. */
151279          nThisLcs = 0;
151280        }else{
151281          if( pAdv==0 || pIter->iPos<pAdv->iPos ){
151282            pAdv = pIter;
151283          }
151284          if( nThisLcs==0 || pIter->iPos==pIter[-1].iPos ){
151285            nThisLcs++;
151286          }else{
151287            nThisLcs = 1;
151288          }
151289          if( nThisLcs>nLcs ) nLcs = nThisLcs;
151290        }
151291      }
151292      if( fts3LcsIteratorAdvance(pAdv) ) nLive--;
151293    }
151294
151295    pInfo->aMatchinfo[iCol] = nLcs;
151296  }
151297
151298  sqlite3_free(aIter);
151299  return SQLITE_OK;
151300}
151301
151302/*
151303** Populate the buffer pInfo->aMatchinfo[] with an array of integers to
151304** be returned by the matchinfo() function. Argument zArg contains the
151305** format string passed as the second argument to matchinfo (or the
151306** default value "pcx" if no second argument was specified). The format
151307** string has already been validated and the pInfo->aMatchinfo[] array
151308** is guaranteed to be large enough for the output.
151309**
151310** If bGlobal is true, then populate all fields of the matchinfo() output.
151311** If it is false, then assume that those fields that do not change between
151312** rows (i.e. FTS3_MATCHINFO_NPHRASE, NCOL, NDOC, AVGLENGTH and part of HITS)
151313** have already been populated.
151314**
151315** Return SQLITE_OK if successful, or an SQLite error code if an error
151316** occurs. If a value other than SQLITE_OK is returned, the state the
151317** pInfo->aMatchinfo[] buffer is left in is undefined.
151318*/
151319static int fts3MatchinfoValues(
151320  Fts3Cursor *pCsr,               /* FTS3 cursor object */
151321  int bGlobal,                    /* True to grab the global stats */
151322  MatchInfo *pInfo,               /* Matchinfo context object */
151323  const char *zArg                /* Matchinfo format string */
151324){
151325  int rc = SQLITE_OK;
151326  int i;
151327  Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
151328  sqlite3_stmt *pSelect = 0;
151329
151330  for(i=0; rc==SQLITE_OK && zArg[i]; i++){
151331    pInfo->flag = zArg[i];
151332    switch( zArg[i] ){
151333      case FTS3_MATCHINFO_NPHRASE:
151334        if( bGlobal ) pInfo->aMatchinfo[0] = pInfo->nPhrase;
151335        break;
151336
151337      case FTS3_MATCHINFO_NCOL:
151338        if( bGlobal ) pInfo->aMatchinfo[0] = pInfo->nCol;
151339        break;
151340
151341      case FTS3_MATCHINFO_NDOC:
151342        if( bGlobal ){
151343          sqlite3_int64 nDoc = 0;
151344          rc = fts3MatchinfoSelectDoctotal(pTab, &pSelect, &nDoc, 0);
151345          pInfo->aMatchinfo[0] = (u32)nDoc;
151346        }
151347        break;
151348
151349      case FTS3_MATCHINFO_AVGLENGTH:
151350        if( bGlobal ){
151351          sqlite3_int64 nDoc;     /* Number of rows in table */
151352          const char *a;          /* Aggregate column length array */
151353
151354          rc = fts3MatchinfoSelectDoctotal(pTab, &pSelect, &nDoc, &a);
151355          if( rc==SQLITE_OK ){
151356            int iCol;
151357            for(iCol=0; iCol<pInfo->nCol; iCol++){
151358              u32 iVal;
151359              sqlite3_int64 nToken;
151360              a += sqlite3Fts3GetVarint(a, &nToken);
151361              iVal = (u32)(((u32)(nToken&0xffffffff)+nDoc/2)/nDoc);
151362              pInfo->aMatchinfo[iCol] = iVal;
151363            }
151364          }
151365        }
151366        break;
151367
151368      case FTS3_MATCHINFO_LENGTH: {
151369        sqlite3_stmt *pSelectDocsize = 0;
151370        rc = sqlite3Fts3SelectDocsize(pTab, pCsr->iPrevId, &pSelectDocsize);
151371        if( rc==SQLITE_OK ){
151372          int iCol;
151373          const char *a = sqlite3_column_blob(pSelectDocsize, 0);
151374          for(iCol=0; iCol<pInfo->nCol; iCol++){
151375            sqlite3_int64 nToken;
151376            a += sqlite3Fts3GetVarint(a, &nToken);
151377            pInfo->aMatchinfo[iCol] = (u32)nToken;
151378          }
151379        }
151380        sqlite3_reset(pSelectDocsize);
151381        break;
151382      }
151383
151384      case FTS3_MATCHINFO_LCS:
151385        rc = fts3ExprLoadDoclists(pCsr, 0, 0);
151386        if( rc==SQLITE_OK ){
151387          rc = fts3MatchinfoLcs(pCsr, pInfo);
151388        }
151389        break;
151390
151391      case FTS3_MATCHINFO_LHITS_BM:
151392      case FTS3_MATCHINFO_LHITS: {
151393        int nZero = fts3MatchinfoSize(pInfo, zArg[i]) * sizeof(u32);
151394        memset(pInfo->aMatchinfo, 0, nZero);
151395        fts3ExprLHitGather(pCsr->pExpr, pInfo);
151396        break;
151397      }
151398
151399      default: {
151400        Fts3Expr *pExpr;
151401        assert( zArg[i]==FTS3_MATCHINFO_HITS );
151402        pExpr = pCsr->pExpr;
151403        rc = fts3ExprLoadDoclists(pCsr, 0, 0);
151404        if( rc!=SQLITE_OK ) break;
151405        if( bGlobal ){
151406          if( pCsr->pDeferred ){
151407            rc = fts3MatchinfoSelectDoctotal(pTab, &pSelect, &pInfo->nDoc, 0);
151408            if( rc!=SQLITE_OK ) break;
151409          }
151410          rc = fts3ExprIterate(pExpr, fts3ExprGlobalHitsCb,(void*)pInfo);
151411          sqlite3Fts3EvalTestDeferred(pCsr, &rc);
151412          if( rc!=SQLITE_OK ) break;
151413        }
151414        (void)fts3ExprIterate(pExpr, fts3ExprLocalHitsCb,(void*)pInfo);
151415        break;
151416      }
151417    }
151418
151419    pInfo->aMatchinfo += fts3MatchinfoSize(pInfo, zArg[i]);
151420  }
151421
151422  sqlite3_reset(pSelect);
151423  return rc;
151424}
151425
151426
151427/*
151428** Populate pCsr->aMatchinfo[] with data for the current row. The
151429** 'matchinfo' data is an array of 32-bit unsigned integers (C type u32).
151430*/
151431static void fts3GetMatchinfo(
151432  sqlite3_context *pCtx,        /* Return results here */
151433  Fts3Cursor *pCsr,               /* FTS3 Cursor object */
151434  const char *zArg                /* Second argument to matchinfo() function */
151435){
151436  MatchInfo sInfo;
151437  Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
151438  int rc = SQLITE_OK;
151439  int bGlobal = 0;                /* Collect 'global' stats as well as local */
151440
151441  u32 *aOut = 0;
151442  void (*xDestroyOut)(void*) = 0;
151443
151444  memset(&sInfo, 0, sizeof(MatchInfo));
151445  sInfo.pCursor = pCsr;
151446  sInfo.nCol = pTab->nColumn;
151447
151448  /* If there is cached matchinfo() data, but the format string for the
151449  ** cache does not match the format string for this request, discard
151450  ** the cached data. */
151451  if( pCsr->pMIBuffer && strcmp(pCsr->pMIBuffer->zMatchinfo, zArg) ){
151452    sqlite3Fts3MIBufferFree(pCsr->pMIBuffer);
151453    pCsr->pMIBuffer = 0;
151454  }
151455
151456  /* If Fts3Cursor.pMIBuffer is NULL, then this is the first time the
151457  ** matchinfo function has been called for this query. In this case
151458  ** allocate the array used to accumulate the matchinfo data and
151459  ** initialize those elements that are constant for every row.
151460  */
151461  if( pCsr->pMIBuffer==0 ){
151462    int nMatchinfo = 0;           /* Number of u32 elements in match-info */
151463    int i;                        /* Used to iterate through zArg */
151464
151465    /* Determine the number of phrases in the query */
151466    pCsr->nPhrase = fts3ExprPhraseCount(pCsr->pExpr);
151467    sInfo.nPhrase = pCsr->nPhrase;
151468
151469    /* Determine the number of integers in the buffer returned by this call. */
151470    for(i=0; zArg[i]; i++){
151471      char *zErr = 0;
151472      if( fts3MatchinfoCheck(pTab, zArg[i], &zErr) ){
151473        sqlite3_result_error(pCtx, zErr, -1);
151474        sqlite3_free(zErr);
151475        return;
151476      }
151477      nMatchinfo += fts3MatchinfoSize(&sInfo, zArg[i]);
151478    }
151479
151480    /* Allocate space for Fts3Cursor.aMatchinfo[] and Fts3Cursor.zMatchinfo. */
151481    pCsr->pMIBuffer = fts3MIBufferNew(nMatchinfo, zArg);
151482    if( !pCsr->pMIBuffer ) rc = SQLITE_NOMEM;
151483
151484    pCsr->isMatchinfoNeeded = 1;
151485    bGlobal = 1;
151486  }
151487
151488  if( rc==SQLITE_OK ){
151489    xDestroyOut = fts3MIBufferAlloc(pCsr->pMIBuffer, &aOut);
151490    if( xDestroyOut==0 ){
151491      rc = SQLITE_NOMEM;
151492    }
151493  }
151494
151495  if( rc==SQLITE_OK ){
151496    sInfo.aMatchinfo = aOut;
151497    sInfo.nPhrase = pCsr->nPhrase;
151498    rc = fts3MatchinfoValues(pCsr, bGlobal, &sInfo, zArg);
151499    if( bGlobal ){
151500      fts3MIBufferSetGlobal(pCsr->pMIBuffer);
151501    }
151502  }
151503
151504  if( rc!=SQLITE_OK ){
151505    sqlite3_result_error_code(pCtx, rc);
151506    if( xDestroyOut ) xDestroyOut(aOut);
151507  }else{
151508    int n = pCsr->pMIBuffer->nElem * sizeof(u32);
151509    sqlite3_result_blob(pCtx, aOut, n, xDestroyOut);
151510  }
151511}
151512
151513/*
151514** Implementation of snippet() function.
151515*/
151516SQLITE_PRIVATE void sqlite3Fts3Snippet(
151517  sqlite3_context *pCtx,          /* SQLite function call context */
151518  Fts3Cursor *pCsr,               /* Cursor object */
151519  const char *zStart,             /* Snippet start text - "<b>" */
151520  const char *zEnd,               /* Snippet end text - "</b>" */
151521  const char *zEllipsis,          /* Snippet ellipsis text - "<b>...</b>" */
151522  int iCol,                       /* Extract snippet from this column */
151523  int nToken                      /* Approximate number of tokens in snippet */
151524){
151525  Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
151526  int rc = SQLITE_OK;
151527  int i;
151528  StrBuffer res = {0, 0, 0};
151529
151530  /* The returned text includes up to four fragments of text extracted from
151531  ** the data in the current row. The first iteration of the for(...) loop
151532  ** below attempts to locate a single fragment of text nToken tokens in
151533  ** size that contains at least one instance of all phrases in the query
151534  ** expression that appear in the current row. If such a fragment of text
151535  ** cannot be found, the second iteration of the loop attempts to locate
151536  ** a pair of fragments, and so on.
151537  */
151538  int nSnippet = 0;               /* Number of fragments in this snippet */
151539  SnippetFragment aSnippet[4];    /* Maximum of 4 fragments per snippet */
151540  int nFToken = -1;               /* Number of tokens in each fragment */
151541
151542  if( !pCsr->pExpr ){
151543    sqlite3_result_text(pCtx, "", 0, SQLITE_STATIC);
151544    return;
151545  }
151546
151547  for(nSnippet=1; 1; nSnippet++){
151548
151549    int iSnip;                    /* Loop counter 0..nSnippet-1 */
151550    u64 mCovered = 0;             /* Bitmask of phrases covered by snippet */
151551    u64 mSeen = 0;                /* Bitmask of phrases seen by BestSnippet() */
151552
151553    if( nToken>=0 ){
151554      nFToken = (nToken+nSnippet-1) / nSnippet;
151555    }else{
151556      nFToken = -1 * nToken;
151557    }
151558
151559    for(iSnip=0; iSnip<nSnippet; iSnip++){
151560      int iBestScore = -1;        /* Best score of columns checked so far */
151561      int iRead;                  /* Used to iterate through columns */
151562      SnippetFragment *pFragment = &aSnippet[iSnip];
151563
151564      memset(pFragment, 0, sizeof(*pFragment));
151565
151566      /* Loop through all columns of the table being considered for snippets.
151567      ** If the iCol argument to this function was negative, this means all
151568      ** columns of the FTS3 table. Otherwise, only column iCol is considered.
151569      */
151570      for(iRead=0; iRead<pTab->nColumn; iRead++){
151571        SnippetFragment sF = {0, 0, 0, 0};
151572        int iS = 0;
151573        if( iCol>=0 && iRead!=iCol ) continue;
151574
151575        /* Find the best snippet of nFToken tokens in column iRead. */
151576        rc = fts3BestSnippet(nFToken, pCsr, iRead, mCovered, &mSeen, &sF, &iS);
151577        if( rc!=SQLITE_OK ){
151578          goto snippet_out;
151579        }
151580        if( iS>iBestScore ){
151581          *pFragment = sF;
151582          iBestScore = iS;
151583        }
151584      }
151585
151586      mCovered |= pFragment->covered;
151587    }
151588
151589    /* If all query phrases seen by fts3BestSnippet() are present in at least
151590    ** one of the nSnippet snippet fragments, break out of the loop.
151591    */
151592    assert( (mCovered&mSeen)==mCovered );
151593    if( mSeen==mCovered || nSnippet==SizeofArray(aSnippet) ) break;
151594  }
151595
151596  assert( nFToken>0 );
151597
151598  for(i=0; i<nSnippet && rc==SQLITE_OK; i++){
151599    rc = fts3SnippetText(pCsr, &aSnippet[i],
151600        i, (i==nSnippet-1), nFToken, zStart, zEnd, zEllipsis, &res
151601    );
151602  }
151603
151604 snippet_out:
151605  sqlite3Fts3SegmentsClose(pTab);
151606  if( rc!=SQLITE_OK ){
151607    sqlite3_result_error_code(pCtx, rc);
151608    sqlite3_free(res.z);
151609  }else{
151610    sqlite3_result_text(pCtx, res.z, -1, sqlite3_free);
151611  }
151612}
151613
151614
151615typedef struct TermOffset TermOffset;
151616typedef struct TermOffsetCtx TermOffsetCtx;
151617
151618struct TermOffset {
151619  char *pList;                    /* Position-list */
151620  int iPos;                       /* Position just read from pList */
151621  int iOff;                       /* Offset of this term from read positions */
151622};
151623
151624struct TermOffsetCtx {
151625  Fts3Cursor *pCsr;
151626  int iCol;                       /* Column of table to populate aTerm for */
151627  int iTerm;
151628  sqlite3_int64 iDocid;
151629  TermOffset *aTerm;
151630};
151631
151632/*
151633** This function is an fts3ExprIterate() callback used by sqlite3Fts3Offsets().
151634*/
151635static int fts3ExprTermOffsetInit(Fts3Expr *pExpr, int iPhrase, void *ctx){
151636  TermOffsetCtx *p = (TermOffsetCtx *)ctx;
151637  int nTerm;                      /* Number of tokens in phrase */
151638  int iTerm;                      /* For looping through nTerm phrase terms */
151639  char *pList;                    /* Pointer to position list for phrase */
151640  int iPos = 0;                   /* First position in position-list */
151641  int rc;
151642
151643  UNUSED_PARAMETER(iPhrase);
151644  rc = sqlite3Fts3EvalPhrasePoslist(p->pCsr, pExpr, p->iCol, &pList);
151645  nTerm = pExpr->pPhrase->nToken;
151646  if( pList ){
151647    fts3GetDeltaPosition(&pList, &iPos);
151648    assert( iPos>=0 );
151649  }
151650
151651  for(iTerm=0; iTerm<nTerm; iTerm++){
151652    TermOffset *pT = &p->aTerm[p->iTerm++];
151653    pT->iOff = nTerm-iTerm-1;
151654    pT->pList = pList;
151655    pT->iPos = iPos;
151656  }
151657
151658  return rc;
151659}
151660
151661/*
151662** Implementation of offsets() function.
151663*/
151664SQLITE_PRIVATE void sqlite3Fts3Offsets(
151665  sqlite3_context *pCtx,          /* SQLite function call context */
151666  Fts3Cursor *pCsr                /* Cursor object */
151667){
151668  Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
151669  sqlite3_tokenizer_module const *pMod = pTab->pTokenizer->pModule;
151670  int rc;                         /* Return Code */
151671  int nToken;                     /* Number of tokens in query */
151672  int iCol;                       /* Column currently being processed */
151673  StrBuffer res = {0, 0, 0};      /* Result string */
151674  TermOffsetCtx sCtx;             /* Context for fts3ExprTermOffsetInit() */
151675
151676  if( !pCsr->pExpr ){
151677    sqlite3_result_text(pCtx, "", 0, SQLITE_STATIC);
151678    return;
151679  }
151680
151681  memset(&sCtx, 0, sizeof(sCtx));
151682  assert( pCsr->isRequireSeek==0 );
151683
151684  /* Count the number of terms in the query */
151685  rc = fts3ExprLoadDoclists(pCsr, 0, &nToken);
151686  if( rc!=SQLITE_OK ) goto offsets_out;
151687
151688  /* Allocate the array of TermOffset iterators. */
151689  sCtx.aTerm = (TermOffset *)sqlite3_malloc(sizeof(TermOffset)*nToken);
151690  if( 0==sCtx.aTerm ){
151691    rc = SQLITE_NOMEM;
151692    goto offsets_out;
151693  }
151694  sCtx.iDocid = pCsr->iPrevId;
151695  sCtx.pCsr = pCsr;
151696
151697  /* Loop through the table columns, appending offset information to
151698  ** string-buffer res for each column.
151699  */
151700  for(iCol=0; iCol<pTab->nColumn; iCol++){
151701    sqlite3_tokenizer_cursor *pC; /* Tokenizer cursor */
151702    const char *ZDUMMY;           /* Dummy argument used with xNext() */
151703    int NDUMMY = 0;               /* Dummy argument used with xNext() */
151704    int iStart = 0;
151705    int iEnd = 0;
151706    int iCurrent = 0;
151707    const char *zDoc;
151708    int nDoc;
151709
151710    /* Initialize the contents of sCtx.aTerm[] for column iCol. There is
151711    ** no way that this operation can fail, so the return code from
151712    ** fts3ExprIterate() can be discarded.
151713    */
151714    sCtx.iCol = iCol;
151715    sCtx.iTerm = 0;
151716    (void)fts3ExprIterate(pCsr->pExpr, fts3ExprTermOffsetInit, (void*)&sCtx);
151717
151718    /* Retreive the text stored in column iCol. If an SQL NULL is stored
151719    ** in column iCol, jump immediately to the next iteration of the loop.
151720    ** If an OOM occurs while retrieving the data (this can happen if SQLite
151721    ** needs to transform the data from utf-16 to utf-8), return SQLITE_NOMEM
151722    ** to the caller.
151723    */
151724    zDoc = (const char *)sqlite3_column_text(pCsr->pStmt, iCol+1);
151725    nDoc = sqlite3_column_bytes(pCsr->pStmt, iCol+1);
151726    if( zDoc==0 ){
151727      if( sqlite3_column_type(pCsr->pStmt, iCol+1)==SQLITE_NULL ){
151728        continue;
151729      }
151730      rc = SQLITE_NOMEM;
151731      goto offsets_out;
151732    }
151733
151734    /* Initialize a tokenizer iterator to iterate through column iCol. */
151735    rc = sqlite3Fts3OpenTokenizer(pTab->pTokenizer, pCsr->iLangid,
151736        zDoc, nDoc, &pC
151737    );
151738    if( rc!=SQLITE_OK ) goto offsets_out;
151739
151740    rc = pMod->xNext(pC, &ZDUMMY, &NDUMMY, &iStart, &iEnd, &iCurrent);
151741    while( rc==SQLITE_OK ){
151742      int i;                      /* Used to loop through terms */
151743      int iMinPos = 0x7FFFFFFF;   /* Position of next token */
151744      TermOffset *pTerm = 0;      /* TermOffset associated with next token */
151745
151746      for(i=0; i<nToken; i++){
151747        TermOffset *pT = &sCtx.aTerm[i];
151748        if( pT->pList && (pT->iPos-pT->iOff)<iMinPos ){
151749          iMinPos = pT->iPos-pT->iOff;
151750          pTerm = pT;
151751        }
151752      }
151753
151754      if( !pTerm ){
151755        /* All offsets for this column have been gathered. */
151756        rc = SQLITE_DONE;
151757      }else{
151758        assert( iCurrent<=iMinPos );
151759        if( 0==(0xFE&*pTerm->pList) ){
151760          pTerm->pList = 0;
151761        }else{
151762          fts3GetDeltaPosition(&pTerm->pList, &pTerm->iPos);
151763        }
151764        while( rc==SQLITE_OK && iCurrent<iMinPos ){
151765          rc = pMod->xNext(pC, &ZDUMMY, &NDUMMY, &iStart, &iEnd, &iCurrent);
151766        }
151767        if( rc==SQLITE_OK ){
151768          char aBuffer[64];
151769          sqlite3_snprintf(sizeof(aBuffer), aBuffer,
151770              "%d %d %d %d ", iCol, pTerm-sCtx.aTerm, iStart, iEnd-iStart
151771          );
151772          rc = fts3StringAppend(&res, aBuffer, -1);
151773        }else if( rc==SQLITE_DONE && pTab->zContentTbl==0 ){
151774          rc = FTS_CORRUPT_VTAB;
151775        }
151776      }
151777    }
151778    if( rc==SQLITE_DONE ){
151779      rc = SQLITE_OK;
151780    }
151781
151782    pMod->xClose(pC);
151783    if( rc!=SQLITE_OK ) goto offsets_out;
151784  }
151785
151786 offsets_out:
151787  sqlite3_free(sCtx.aTerm);
151788  assert( rc!=SQLITE_DONE );
151789  sqlite3Fts3SegmentsClose(pTab);
151790  if( rc!=SQLITE_OK ){
151791    sqlite3_result_error_code(pCtx,  rc);
151792    sqlite3_free(res.z);
151793  }else{
151794    sqlite3_result_text(pCtx, res.z, res.n-1, sqlite3_free);
151795  }
151796  return;
151797}
151798
151799/*
151800** Implementation of matchinfo() function.
151801*/
151802SQLITE_PRIVATE void sqlite3Fts3Matchinfo(
151803  sqlite3_context *pContext,      /* Function call context */
151804  Fts3Cursor *pCsr,               /* FTS3 table cursor */
151805  const char *zArg                /* Second arg to matchinfo() function */
151806){
151807  Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
151808  const char *zFormat;
151809
151810  if( zArg ){
151811    zFormat = zArg;
151812  }else{
151813    zFormat = FTS3_MATCHINFO_DEFAULT;
151814  }
151815
151816  if( !pCsr->pExpr ){
151817    sqlite3_result_blob(pContext, "", 0, SQLITE_STATIC);
151818    return;
151819  }else{
151820    /* Retrieve matchinfo() data. */
151821    fts3GetMatchinfo(pContext, pCsr, zFormat);
151822    sqlite3Fts3SegmentsClose(pTab);
151823  }
151824}
151825
151826#endif
151827
151828/************** End of fts3_snippet.c ****************************************/
151829/************** Begin file fts3_unicode.c ************************************/
151830/*
151831** 2012 May 24
151832**
151833** The author disclaims copyright to this source code.  In place of
151834** a legal notice, here is a blessing:
151835**
151836**    May you do good and not evil.
151837**    May you find forgiveness for yourself and forgive others.
151838**    May you share freely, never taking more than you give.
151839**
151840******************************************************************************
151841**
151842** Implementation of the "unicode" full-text-search tokenizer.
151843*/
151844
151845#ifndef SQLITE_DISABLE_FTS3_UNICODE
151846
151847/* #include "fts3Int.h" */
151848#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
151849
151850/* #include <assert.h> */
151851/* #include <stdlib.h> */
151852/* #include <stdio.h> */
151853/* #include <string.h> */
151854
151855/* #include "fts3_tokenizer.h" */
151856
151857/*
151858** The following two macros - READ_UTF8 and WRITE_UTF8 - have been copied
151859** from the sqlite3 source file utf.c. If this file is compiled as part
151860** of the amalgamation, they are not required.
151861*/
151862#ifndef SQLITE_AMALGAMATION
151863
151864static const unsigned char sqlite3Utf8Trans1[] = {
151865  0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
151866  0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
151867  0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
151868  0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
151869  0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
151870  0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
151871  0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
151872  0x00, 0x01, 0x02, 0x03, 0x00, 0x01, 0x00, 0x00,
151873};
151874
151875#define READ_UTF8(zIn, zTerm, c)                           \
151876  c = *(zIn++);                                            \
151877  if( c>=0xc0 ){                                           \
151878    c = sqlite3Utf8Trans1[c-0xc0];                         \
151879    while( zIn!=zTerm && (*zIn & 0xc0)==0x80 ){            \
151880      c = (c<<6) + (0x3f & *(zIn++));                      \
151881    }                                                      \
151882    if( c<0x80                                             \
151883        || (c&0xFFFFF800)==0xD800                          \
151884        || (c&0xFFFFFFFE)==0xFFFE ){  c = 0xFFFD; }        \
151885  }
151886
151887#define WRITE_UTF8(zOut, c) {                          \
151888  if( c<0x00080 ){                                     \
151889    *zOut++ = (u8)(c&0xFF);                            \
151890  }                                                    \
151891  else if( c<0x00800 ){                                \
151892    *zOut++ = 0xC0 + (u8)((c>>6)&0x1F);                \
151893    *zOut++ = 0x80 + (u8)(c & 0x3F);                   \
151894  }                                                    \
151895  else if( c<0x10000 ){                                \
151896    *zOut++ = 0xE0 + (u8)((c>>12)&0x0F);               \
151897    *zOut++ = 0x80 + (u8)((c>>6) & 0x3F);              \
151898    *zOut++ = 0x80 + (u8)(c & 0x3F);                   \
151899  }else{                                               \
151900    *zOut++ = 0xF0 + (u8)((c>>18) & 0x07);             \
151901    *zOut++ = 0x80 + (u8)((c>>12) & 0x3F);             \
151902    *zOut++ = 0x80 + (u8)((c>>6) & 0x3F);              \
151903    *zOut++ = 0x80 + (u8)(c & 0x3F);                   \
151904  }                                                    \
151905}
151906
151907#endif /* ifndef SQLITE_AMALGAMATION */
151908
151909typedef struct unicode_tokenizer unicode_tokenizer;
151910typedef struct unicode_cursor unicode_cursor;
151911
151912struct unicode_tokenizer {
151913  sqlite3_tokenizer base;
151914  int bRemoveDiacritic;
151915  int nException;
151916  int *aiException;
151917};
151918
151919struct unicode_cursor {
151920  sqlite3_tokenizer_cursor base;
151921  const unsigned char *aInput;    /* Input text being tokenized */
151922  int nInput;                     /* Size of aInput[] in bytes */
151923  int iOff;                       /* Current offset within aInput[] */
151924  int iToken;                     /* Index of next token to be returned */
151925  char *zToken;                   /* storage for current token */
151926  int nAlloc;                     /* space allocated at zToken */
151927};
151928
151929
151930/*
151931** Destroy a tokenizer allocated by unicodeCreate().
151932*/
151933static int unicodeDestroy(sqlite3_tokenizer *pTokenizer){
151934  if( pTokenizer ){
151935    unicode_tokenizer *p = (unicode_tokenizer *)pTokenizer;
151936    sqlite3_free(p->aiException);
151937    sqlite3_free(p);
151938  }
151939  return SQLITE_OK;
151940}
151941
151942/*
151943** As part of a tokenchars= or separators= option, the CREATE VIRTUAL TABLE
151944** statement has specified that the tokenizer for this table shall consider
151945** all characters in string zIn/nIn to be separators (if bAlnum==0) or
151946** token characters (if bAlnum==1).
151947**
151948** For each codepoint in the zIn/nIn string, this function checks if the
151949** sqlite3FtsUnicodeIsalnum() function already returns the desired result.
151950** If so, no action is taken. Otherwise, the codepoint is added to the
151951** unicode_tokenizer.aiException[] array. For the purposes of tokenization,
151952** the return value of sqlite3FtsUnicodeIsalnum() is inverted for all
151953** codepoints in the aiException[] array.
151954**
151955** If a standalone diacritic mark (one that sqlite3FtsUnicodeIsdiacritic()
151956** identifies as a diacritic) occurs in the zIn/nIn string it is ignored.
151957** It is not possible to change the behavior of the tokenizer with respect
151958** to these codepoints.
151959*/
151960static int unicodeAddExceptions(
151961  unicode_tokenizer *p,           /* Tokenizer to add exceptions to */
151962  int bAlnum,                     /* Replace Isalnum() return value with this */
151963  const char *zIn,                /* Array of characters to make exceptions */
151964  int nIn                         /* Length of z in bytes */
151965){
151966  const unsigned char *z = (const unsigned char *)zIn;
151967  const unsigned char *zTerm = &z[nIn];
151968  int iCode;
151969  int nEntry = 0;
151970
151971  assert( bAlnum==0 || bAlnum==1 );
151972
151973  while( z<zTerm ){
151974    READ_UTF8(z, zTerm, iCode);
151975    assert( (sqlite3FtsUnicodeIsalnum(iCode) & 0xFFFFFFFE)==0 );
151976    if( sqlite3FtsUnicodeIsalnum(iCode)!=bAlnum
151977     && sqlite3FtsUnicodeIsdiacritic(iCode)==0
151978    ){
151979      nEntry++;
151980    }
151981  }
151982
151983  if( nEntry ){
151984    int *aNew;                    /* New aiException[] array */
151985    int nNew;                     /* Number of valid entries in array aNew[] */
151986
151987    aNew = sqlite3_realloc(p->aiException, (p->nException+nEntry)*sizeof(int));
151988    if( aNew==0 ) return SQLITE_NOMEM;
151989    nNew = p->nException;
151990
151991    z = (const unsigned char *)zIn;
151992    while( z<zTerm ){
151993      READ_UTF8(z, zTerm, iCode);
151994      if( sqlite3FtsUnicodeIsalnum(iCode)!=bAlnum
151995       && sqlite3FtsUnicodeIsdiacritic(iCode)==0
151996      ){
151997        int i, j;
151998        for(i=0; i<nNew && aNew[i]<iCode; i++);
151999        for(j=nNew; j>i; j--) aNew[j] = aNew[j-1];
152000        aNew[i] = iCode;
152001        nNew++;
152002      }
152003    }
152004    p->aiException = aNew;
152005    p->nException = nNew;
152006  }
152007
152008  return SQLITE_OK;
152009}
152010
152011/*
152012** Return true if the p->aiException[] array contains the value iCode.
152013*/
152014static int unicodeIsException(unicode_tokenizer *p, int iCode){
152015  if( p->nException>0 ){
152016    int *a = p->aiException;
152017    int iLo = 0;
152018    int iHi = p->nException-1;
152019
152020    while( iHi>=iLo ){
152021      int iTest = (iHi + iLo) / 2;
152022      if( iCode==a[iTest] ){
152023        return 1;
152024      }else if( iCode>a[iTest] ){
152025        iLo = iTest+1;
152026      }else{
152027        iHi = iTest-1;
152028      }
152029    }
152030  }
152031
152032  return 0;
152033}
152034
152035/*
152036** Return true if, for the purposes of tokenization, codepoint iCode is
152037** considered a token character (not a separator).
152038*/
152039static int unicodeIsAlnum(unicode_tokenizer *p, int iCode){
152040  assert( (sqlite3FtsUnicodeIsalnum(iCode) & 0xFFFFFFFE)==0 );
152041  return sqlite3FtsUnicodeIsalnum(iCode) ^ unicodeIsException(p, iCode);
152042}
152043
152044/*
152045** Create a new tokenizer instance.
152046*/
152047static int unicodeCreate(
152048  int nArg,                       /* Size of array argv[] */
152049  const char * const *azArg,      /* Tokenizer creation arguments */
152050  sqlite3_tokenizer **pp          /* OUT: New tokenizer handle */
152051){
152052  unicode_tokenizer *pNew;        /* New tokenizer object */
152053  int i;
152054  int rc = SQLITE_OK;
152055
152056  pNew = (unicode_tokenizer *) sqlite3_malloc(sizeof(unicode_tokenizer));
152057  if( pNew==NULL ) return SQLITE_NOMEM;
152058  memset(pNew, 0, sizeof(unicode_tokenizer));
152059  pNew->bRemoveDiacritic = 1;
152060
152061  for(i=0; rc==SQLITE_OK && i<nArg; i++){
152062    const char *z = azArg[i];
152063    int n = (int)strlen(z);
152064
152065    if( n==19 && memcmp("remove_diacritics=1", z, 19)==0 ){
152066      pNew->bRemoveDiacritic = 1;
152067    }
152068    else if( n==19 && memcmp("remove_diacritics=0", z, 19)==0 ){
152069      pNew->bRemoveDiacritic = 0;
152070    }
152071    else if( n>=11 && memcmp("tokenchars=", z, 11)==0 ){
152072      rc = unicodeAddExceptions(pNew, 1, &z[11], n-11);
152073    }
152074    else if( n>=11 && memcmp("separators=", z, 11)==0 ){
152075      rc = unicodeAddExceptions(pNew, 0, &z[11], n-11);
152076    }
152077    else{
152078      /* Unrecognized argument */
152079      rc  = SQLITE_ERROR;
152080    }
152081  }
152082
152083  if( rc!=SQLITE_OK ){
152084    unicodeDestroy((sqlite3_tokenizer *)pNew);
152085    pNew = 0;
152086  }
152087  *pp = (sqlite3_tokenizer *)pNew;
152088  return rc;
152089}
152090
152091/*
152092** Prepare to begin tokenizing a particular string.  The input
152093** string to be tokenized is pInput[0..nBytes-1].  A cursor
152094** used to incrementally tokenize this string is returned in
152095** *ppCursor.
152096*/
152097static int unicodeOpen(
152098  sqlite3_tokenizer *p,           /* The tokenizer */
152099  const char *aInput,             /* Input string */
152100  int nInput,                     /* Size of string aInput in bytes */
152101  sqlite3_tokenizer_cursor **pp   /* OUT: New cursor object */
152102){
152103  unicode_cursor *pCsr;
152104
152105  pCsr = (unicode_cursor *)sqlite3_malloc(sizeof(unicode_cursor));
152106  if( pCsr==0 ){
152107    return SQLITE_NOMEM;
152108  }
152109  memset(pCsr, 0, sizeof(unicode_cursor));
152110
152111  pCsr->aInput = (const unsigned char *)aInput;
152112  if( aInput==0 ){
152113    pCsr->nInput = 0;
152114  }else if( nInput<0 ){
152115    pCsr->nInput = (int)strlen(aInput);
152116  }else{
152117    pCsr->nInput = nInput;
152118  }
152119
152120  *pp = &pCsr->base;
152121  UNUSED_PARAMETER(p);
152122  return SQLITE_OK;
152123}
152124
152125/*
152126** Close a tokenization cursor previously opened by a call to
152127** simpleOpen() above.
152128*/
152129static int unicodeClose(sqlite3_tokenizer_cursor *pCursor){
152130  unicode_cursor *pCsr = (unicode_cursor *) pCursor;
152131  sqlite3_free(pCsr->zToken);
152132  sqlite3_free(pCsr);
152133  return SQLITE_OK;
152134}
152135
152136/*
152137** Extract the next token from a tokenization cursor.  The cursor must
152138** have been opened by a prior call to simpleOpen().
152139*/
152140static int unicodeNext(
152141  sqlite3_tokenizer_cursor *pC,   /* Cursor returned by simpleOpen */
152142  const char **paToken,           /* OUT: Token text */
152143  int *pnToken,                   /* OUT: Number of bytes at *paToken */
152144  int *piStart,                   /* OUT: Starting offset of token */
152145  int *piEnd,                     /* OUT: Ending offset of token */
152146  int *piPos                      /* OUT: Position integer of token */
152147){
152148  unicode_cursor *pCsr = (unicode_cursor *)pC;
152149  unicode_tokenizer *p = ((unicode_tokenizer *)pCsr->base.pTokenizer);
152150  int iCode = 0;
152151  char *zOut;
152152  const unsigned char *z = &pCsr->aInput[pCsr->iOff];
152153  const unsigned char *zStart = z;
152154  const unsigned char *zEnd;
152155  const unsigned char *zTerm = &pCsr->aInput[pCsr->nInput];
152156
152157  /* Scan past any delimiter characters before the start of the next token.
152158  ** Return SQLITE_DONE early if this takes us all the way to the end of
152159  ** the input.  */
152160  while( z<zTerm ){
152161    READ_UTF8(z, zTerm, iCode);
152162    if( unicodeIsAlnum(p, iCode) ) break;
152163    zStart = z;
152164  }
152165  if( zStart>=zTerm ) return SQLITE_DONE;
152166
152167  zOut = pCsr->zToken;
152168  do {
152169    int iOut;
152170
152171    /* Grow the output buffer if required. */
152172    if( (zOut-pCsr->zToken)>=(pCsr->nAlloc-4) ){
152173      char *zNew = sqlite3_realloc(pCsr->zToken, pCsr->nAlloc+64);
152174      if( !zNew ) return SQLITE_NOMEM;
152175      zOut = &zNew[zOut - pCsr->zToken];
152176      pCsr->zToken = zNew;
152177      pCsr->nAlloc += 64;
152178    }
152179
152180    /* Write the folded case of the last character read to the output */
152181    zEnd = z;
152182    iOut = sqlite3FtsUnicodeFold(iCode, p->bRemoveDiacritic);
152183    if( iOut ){
152184      WRITE_UTF8(zOut, iOut);
152185    }
152186
152187    /* If the cursor is not at EOF, read the next character */
152188    if( z>=zTerm ) break;
152189    READ_UTF8(z, zTerm, iCode);
152190  }while( unicodeIsAlnum(p, iCode)
152191       || sqlite3FtsUnicodeIsdiacritic(iCode)
152192  );
152193
152194  /* Set the output variables and return. */
152195  pCsr->iOff = (int)(z - pCsr->aInput);
152196  *paToken = pCsr->zToken;
152197  *pnToken = (int)(zOut - pCsr->zToken);
152198  *piStart = (int)(zStart - pCsr->aInput);
152199  *piEnd = (int)(zEnd - pCsr->aInput);
152200  *piPos = pCsr->iToken++;
152201  return SQLITE_OK;
152202}
152203
152204/*
152205** Set *ppModule to a pointer to the sqlite3_tokenizer_module
152206** structure for the unicode tokenizer.
152207*/
152208SQLITE_PRIVATE void sqlite3Fts3UnicodeTokenizer(sqlite3_tokenizer_module const **ppModule){
152209  static const sqlite3_tokenizer_module module = {
152210    0,
152211    unicodeCreate,
152212    unicodeDestroy,
152213    unicodeOpen,
152214    unicodeClose,
152215    unicodeNext,
152216    0,
152217  };
152218  *ppModule = &module;
152219}
152220
152221#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */
152222#endif /* ifndef SQLITE_DISABLE_FTS3_UNICODE */
152223
152224/************** End of fts3_unicode.c ****************************************/
152225/************** Begin file fts3_unicode2.c ***********************************/
152226/*
152227** 2012 May 25
152228**
152229** The author disclaims copyright to this source code.  In place of
152230** a legal notice, here is a blessing:
152231**
152232**    May you do good and not evil.
152233**    May you find forgiveness for yourself and forgive others.
152234**    May you share freely, never taking more than you give.
152235**
152236******************************************************************************
152237*/
152238
152239/*
152240** DO NOT EDIT THIS MACHINE GENERATED FILE.
152241*/
152242
152243#ifndef SQLITE_DISABLE_FTS3_UNICODE
152244#if defined(SQLITE_ENABLE_FTS3) || defined(SQLITE_ENABLE_FTS4)
152245
152246/* #include <assert.h> */
152247
152248/*
152249** Return true if the argument corresponds to a unicode codepoint
152250** classified as either a letter or a number. Otherwise false.
152251**
152252** The results are undefined if the value passed to this function
152253** is less than zero.
152254*/
152255SQLITE_PRIVATE int sqlite3FtsUnicodeIsalnum(int c){
152256  /* Each unsigned integer in the following array corresponds to a contiguous
152257  ** range of unicode codepoints that are not either letters or numbers (i.e.
152258  ** codepoints for which this function should return 0).
152259  **
152260  ** The most significant 22 bits in each 32-bit value contain the first
152261  ** codepoint in the range. The least significant 10 bits are used to store
152262  ** the size of the range (always at least 1). In other words, the value
152263  ** ((C<<22) + N) represents a range of N codepoints starting with codepoint
152264  ** C. It is not possible to represent a range larger than 1023 codepoints
152265  ** using this format.
152266  */
152267  static const unsigned int aEntry[] = {
152268    0x00000030, 0x0000E807, 0x00016C06, 0x0001EC2F, 0x0002AC07,
152269    0x0002D001, 0x0002D803, 0x0002EC01, 0x0002FC01, 0x00035C01,
152270    0x0003DC01, 0x000B0804, 0x000B480E, 0x000B9407, 0x000BB401,
152271    0x000BBC81, 0x000DD401, 0x000DF801, 0x000E1002, 0x000E1C01,
152272    0x000FD801, 0x00120808, 0x00156806, 0x00162402, 0x00163C01,
152273    0x00164437, 0x0017CC02, 0x00180005, 0x00181816, 0x00187802,
152274    0x00192C15, 0x0019A804, 0x0019C001, 0x001B5001, 0x001B580F,
152275    0x001B9C07, 0x001BF402, 0x001C000E, 0x001C3C01, 0x001C4401,
152276    0x001CC01B, 0x001E980B, 0x001FAC09, 0x001FD804, 0x00205804,
152277    0x00206C09, 0x00209403, 0x0020A405, 0x0020C00F, 0x00216403,
152278    0x00217801, 0x0023901B, 0x00240004, 0x0024E803, 0x0024F812,
152279    0x00254407, 0x00258804, 0x0025C001, 0x00260403, 0x0026F001,
152280    0x0026F807, 0x00271C02, 0x00272C03, 0x00275C01, 0x00278802,
152281    0x0027C802, 0x0027E802, 0x00280403, 0x0028F001, 0x0028F805,
152282    0x00291C02, 0x00292C03, 0x00294401, 0x0029C002, 0x0029D401,
152283    0x002A0403, 0x002AF001, 0x002AF808, 0x002B1C03, 0x002B2C03,
152284    0x002B8802, 0x002BC002, 0x002C0403, 0x002CF001, 0x002CF807,
152285    0x002D1C02, 0x002D2C03, 0x002D5802, 0x002D8802, 0x002DC001,
152286    0x002E0801, 0x002EF805, 0x002F1803, 0x002F2804, 0x002F5C01,
152287    0x002FCC08, 0x00300403, 0x0030F807, 0x00311803, 0x00312804,
152288    0x00315402, 0x00318802, 0x0031FC01, 0x00320802, 0x0032F001,
152289    0x0032F807, 0x00331803, 0x00332804, 0x00335402, 0x00338802,
152290    0x00340802, 0x0034F807, 0x00351803, 0x00352804, 0x00355C01,
152291    0x00358802, 0x0035E401, 0x00360802, 0x00372801, 0x00373C06,
152292    0x00375801, 0x00376008, 0x0037C803, 0x0038C401, 0x0038D007,
152293    0x0038FC01, 0x00391C09, 0x00396802, 0x003AC401, 0x003AD006,
152294    0x003AEC02, 0x003B2006, 0x003C041F, 0x003CD00C, 0x003DC417,
152295    0x003E340B, 0x003E6424, 0x003EF80F, 0x003F380D, 0x0040AC14,
152296    0x00412806, 0x00415804, 0x00417803, 0x00418803, 0x00419C07,
152297    0x0041C404, 0x0042080C, 0x00423C01, 0x00426806, 0x0043EC01,
152298    0x004D740C, 0x004E400A, 0x00500001, 0x0059B402, 0x005A0001,
152299    0x005A6C02, 0x005BAC03, 0x005C4803, 0x005CC805, 0x005D4802,
152300    0x005DC802, 0x005ED023, 0x005F6004, 0x005F7401, 0x0060000F,
152301    0x0062A401, 0x0064800C, 0x0064C00C, 0x00650001, 0x00651002,
152302    0x0066C011, 0x00672002, 0x00677822, 0x00685C05, 0x00687802,
152303    0x0069540A, 0x0069801D, 0x0069FC01, 0x006A8007, 0x006AA006,
152304    0x006C0005, 0x006CD011, 0x006D6823, 0x006E0003, 0x006E840D,
152305    0x006F980E, 0x006FF004, 0x00709014, 0x0070EC05, 0x0071F802,
152306    0x00730008, 0x00734019, 0x0073B401, 0x0073C803, 0x00770027,
152307    0x0077F004, 0x007EF401, 0x007EFC03, 0x007F3403, 0x007F7403,
152308    0x007FB403, 0x007FF402, 0x00800065, 0x0081A806, 0x0081E805,
152309    0x00822805, 0x0082801A, 0x00834021, 0x00840002, 0x00840C04,
152310    0x00842002, 0x00845001, 0x00845803, 0x00847806, 0x00849401,
152311    0x00849C01, 0x0084A401, 0x0084B801, 0x0084E802, 0x00850005,
152312    0x00852804, 0x00853C01, 0x00864264, 0x00900027, 0x0091000B,
152313    0x0092704E, 0x00940200, 0x009C0475, 0x009E53B9, 0x00AD400A,
152314    0x00B39406, 0x00B3BC03, 0x00B3E404, 0x00B3F802, 0x00B5C001,
152315    0x00B5FC01, 0x00B7804F, 0x00B8C00C, 0x00BA001A, 0x00BA6C59,
152316    0x00BC00D6, 0x00BFC00C, 0x00C00005, 0x00C02019, 0x00C0A807,
152317    0x00C0D802, 0x00C0F403, 0x00C26404, 0x00C28001, 0x00C3EC01,
152318    0x00C64002, 0x00C6580A, 0x00C70024, 0x00C8001F, 0x00C8A81E,
152319    0x00C94001, 0x00C98020, 0x00CA2827, 0x00CB003F, 0x00CC0100,
152320    0x01370040, 0x02924037, 0x0293F802, 0x02983403, 0x0299BC10,
152321    0x029A7C01, 0x029BC008, 0x029C0017, 0x029C8002, 0x029E2402,
152322    0x02A00801, 0x02A01801, 0x02A02C01, 0x02A08C09, 0x02A0D804,
152323    0x02A1D004, 0x02A20002, 0x02A2D011, 0x02A33802, 0x02A38012,
152324    0x02A3E003, 0x02A4980A, 0x02A51C0D, 0x02A57C01, 0x02A60004,
152325    0x02A6CC1B, 0x02A77802, 0x02A8A40E, 0x02A90C01, 0x02A93002,
152326    0x02A97004, 0x02A9DC03, 0x02A9EC01, 0x02AAC001, 0x02AAC803,
152327    0x02AADC02, 0x02AAF802, 0x02AB0401, 0x02AB7802, 0x02ABAC07,
152328    0x02ABD402, 0x02AF8C0B, 0x03600001, 0x036DFC02, 0x036FFC02,
152329    0x037FFC01, 0x03EC7801, 0x03ECA401, 0x03EEC810, 0x03F4F802,
152330    0x03F7F002, 0x03F8001A, 0x03F88007, 0x03F8C023, 0x03F95013,
152331    0x03F9A004, 0x03FBFC01, 0x03FC040F, 0x03FC6807, 0x03FCEC06,
152332    0x03FD6C0B, 0x03FF8007, 0x03FFA007, 0x03FFE405, 0x04040003,
152333    0x0404DC09, 0x0405E411, 0x0406400C, 0x0407402E, 0x040E7C01,
152334    0x040F4001, 0x04215C01, 0x04247C01, 0x0424FC01, 0x04280403,
152335    0x04281402, 0x04283004, 0x0428E003, 0x0428FC01, 0x04294009,
152336    0x0429FC01, 0x042CE407, 0x04400003, 0x0440E016, 0x04420003,
152337    0x0442C012, 0x04440003, 0x04449C0E, 0x04450004, 0x04460003,
152338    0x0446CC0E, 0x04471404, 0x045AAC0D, 0x0491C004, 0x05BD442E,
152339    0x05BE3C04, 0x074000F6, 0x07440027, 0x0744A4B5, 0x07480046,
152340    0x074C0057, 0x075B0401, 0x075B6C01, 0x075BEC01, 0x075C5401,
152341    0x075CD401, 0x075D3C01, 0x075DBC01, 0x075E2401, 0x075EA401,
152342    0x075F0C01, 0x07BBC002, 0x07C0002C, 0x07C0C064, 0x07C2800F,
152343    0x07C2C40E, 0x07C3040F, 0x07C3440F, 0x07C4401F, 0x07C4C03C,
152344    0x07C5C02B, 0x07C7981D, 0x07C8402B, 0x07C90009, 0x07C94002,
152345    0x07CC0021, 0x07CCC006, 0x07CCDC46, 0x07CE0014, 0x07CE8025,
152346    0x07CF1805, 0x07CF8011, 0x07D0003F, 0x07D10001, 0x07D108B6,
152347    0x07D3E404, 0x07D4003E, 0x07D50004, 0x07D54018, 0x07D7EC46,
152348    0x07D9140B, 0x07DA0046, 0x07DC0074, 0x38000401, 0x38008060,
152349    0x380400F0,
152350  };
152351  static const unsigned int aAscii[4] = {
152352    0xFFFFFFFF, 0xFC00FFFF, 0xF8000001, 0xF8000001,
152353  };
152354
152355  if( c<128 ){
152356    return ( (aAscii[c >> 5] & (1 << (c & 0x001F)))==0 );
152357  }else if( c<(1<<22) ){
152358    unsigned int key = (((unsigned int)c)<<10) | 0x000003FF;
152359    int iRes = 0;
152360    int iHi = sizeof(aEntry)/sizeof(aEntry[0]) - 1;
152361    int iLo = 0;
152362    while( iHi>=iLo ){
152363      int iTest = (iHi + iLo) / 2;
152364      if( key >= aEntry[iTest] ){
152365        iRes = iTest;
152366        iLo = iTest+1;
152367      }else{
152368        iHi = iTest-1;
152369      }
152370    }
152371    assert( aEntry[0]<key );
152372    assert( key>=aEntry[iRes] );
152373    return (((unsigned int)c) >= ((aEntry[iRes]>>10) + (aEntry[iRes]&0x3FF)));
152374  }
152375  return 1;
152376}
152377
152378
152379/*
152380** If the argument is a codepoint corresponding to a lowercase letter
152381** in the ASCII range with a diacritic added, return the codepoint
152382** of the ASCII letter only. For example, if passed 235 - "LATIN
152383** SMALL LETTER E WITH DIAERESIS" - return 65 ("LATIN SMALL LETTER
152384** E"). The resuls of passing a codepoint that corresponds to an
152385** uppercase letter are undefined.
152386*/
152387static int remove_diacritic(int c){
152388  unsigned short aDia[] = {
152389        0,  1797,  1848,  1859,  1891,  1928,  1940,  1995,
152390     2024,  2040,  2060,  2110,  2168,  2206,  2264,  2286,
152391     2344,  2383,  2472,  2488,  2516,  2596,  2668,  2732,
152392     2782,  2842,  2894,  2954,  2984,  3000,  3028,  3336,
152393     3456,  3696,  3712,  3728,  3744,  3896,  3912,  3928,
152394     3968,  4008,  4040,  4106,  4138,  4170,  4202,  4234,
152395     4266,  4296,  4312,  4344,  4408,  4424,  4472,  4504,
152396     6148,  6198,  6264,  6280,  6360,  6429,  6505,  6529,
152397    61448, 61468, 61534, 61592, 61642, 61688, 61704, 61726,
152398    61784, 61800, 61836, 61880, 61914, 61948, 61998, 62122,
152399    62154, 62200, 62218, 62302, 62364, 62442, 62478, 62536,
152400    62554, 62584, 62604, 62640, 62648, 62656, 62664, 62730,
152401    62924, 63050, 63082, 63274, 63390,
152402  };
152403  char aChar[] = {
152404    '\0', 'a',  'c',  'e',  'i',  'n',  'o',  'u',  'y',  'y',  'a',  'c',
152405    'd',  'e',  'e',  'g',  'h',  'i',  'j',  'k',  'l',  'n',  'o',  'r',
152406    's',  't',  'u',  'u',  'w',  'y',  'z',  'o',  'u',  'a',  'i',  'o',
152407    'u',  'g',  'k',  'o',  'j',  'g',  'n',  'a',  'e',  'i',  'o',  'r',
152408    'u',  's',  't',  'h',  'a',  'e',  'o',  'y',  '\0', '\0', '\0', '\0',
152409    '\0', '\0', '\0', '\0', 'a',  'b',  'd',  'd',  'e',  'f',  'g',  'h',
152410    'h',  'i',  'k',  'l',  'l',  'm',  'n',  'p',  'r',  'r',  's',  't',
152411    'u',  'v',  'w',  'w',  'x',  'y',  'z',  'h',  't',  'w',  'y',  'a',
152412    'e',  'i',  'o',  'u',  'y',
152413  };
152414
152415  unsigned int key = (((unsigned int)c)<<3) | 0x00000007;
152416  int iRes = 0;
152417  int iHi = sizeof(aDia)/sizeof(aDia[0]) - 1;
152418  int iLo = 0;
152419  while( iHi>=iLo ){
152420    int iTest = (iHi + iLo) / 2;
152421    if( key >= aDia[iTest] ){
152422      iRes = iTest;
152423      iLo = iTest+1;
152424    }else{
152425      iHi = iTest-1;
152426    }
152427  }
152428  assert( key>=aDia[iRes] );
152429  return ((c > (aDia[iRes]>>3) + (aDia[iRes]&0x07)) ? c : (int)aChar[iRes]);
152430}
152431
152432
152433/*
152434** Return true if the argument interpreted as a unicode codepoint
152435** is a diacritical modifier character.
152436*/
152437SQLITE_PRIVATE int sqlite3FtsUnicodeIsdiacritic(int c){
152438  unsigned int mask0 = 0x08029FDF;
152439  unsigned int mask1 = 0x000361F8;
152440  if( c<768 || c>817 ) return 0;
152441  return (c < 768+32) ?
152442      (mask0 & (1 << (c-768))) :
152443      (mask1 & (1 << (c-768-32)));
152444}
152445
152446
152447/*
152448** Interpret the argument as a unicode codepoint. If the codepoint
152449** is an upper case character that has a lower case equivalent,
152450** return the codepoint corresponding to the lower case version.
152451** Otherwise, return a copy of the argument.
152452**
152453** The results are undefined if the value passed to this function
152454** is less than zero.
152455*/
152456SQLITE_PRIVATE int sqlite3FtsUnicodeFold(int c, int bRemoveDiacritic){
152457  /* Each entry in the following array defines a rule for folding a range
152458  ** of codepoints to lower case. The rule applies to a range of nRange
152459  ** codepoints starting at codepoint iCode.
152460  **
152461  ** If the least significant bit in flags is clear, then the rule applies
152462  ** to all nRange codepoints (i.e. all nRange codepoints are upper case and
152463  ** need to be folded). Or, if it is set, then the rule only applies to
152464  ** every second codepoint in the range, starting with codepoint C.
152465  **
152466  ** The 7 most significant bits in flags are an index into the aiOff[]
152467  ** array. If a specific codepoint C does require folding, then its lower
152468  ** case equivalent is ((C + aiOff[flags>>1]) & 0xFFFF).
152469  **
152470  ** The contents of this array are generated by parsing the CaseFolding.txt
152471  ** file distributed as part of the "Unicode Character Database". See
152472  ** http://www.unicode.org for details.
152473  */
152474  static const struct TableEntry {
152475    unsigned short iCode;
152476    unsigned char flags;
152477    unsigned char nRange;
152478  } aEntry[] = {
152479    {65, 14, 26},          {181, 64, 1},          {192, 14, 23},
152480    {216, 14, 7},          {256, 1, 48},          {306, 1, 6},
152481    {313, 1, 16},          {330, 1, 46},          {376, 116, 1},
152482    {377, 1, 6},           {383, 104, 1},         {385, 50, 1},
152483    {386, 1, 4},           {390, 44, 1},          {391, 0, 1},
152484    {393, 42, 2},          {395, 0, 1},           {398, 32, 1},
152485    {399, 38, 1},          {400, 40, 1},          {401, 0, 1},
152486    {403, 42, 1},          {404, 46, 1},          {406, 52, 1},
152487    {407, 48, 1},          {408, 0, 1},           {412, 52, 1},
152488    {413, 54, 1},          {415, 56, 1},          {416, 1, 6},
152489    {422, 60, 1},          {423, 0, 1},           {425, 60, 1},
152490    {428, 0, 1},           {430, 60, 1},          {431, 0, 1},
152491    {433, 58, 2},          {435, 1, 4},           {439, 62, 1},
152492    {440, 0, 1},           {444, 0, 1},           {452, 2, 1},
152493    {453, 0, 1},           {455, 2, 1},           {456, 0, 1},
152494    {458, 2, 1},           {459, 1, 18},          {478, 1, 18},
152495    {497, 2, 1},           {498, 1, 4},           {502, 122, 1},
152496    {503, 134, 1},         {504, 1, 40},          {544, 110, 1},
152497    {546, 1, 18},          {570, 70, 1},          {571, 0, 1},
152498    {573, 108, 1},         {574, 68, 1},          {577, 0, 1},
152499    {579, 106, 1},         {580, 28, 1},          {581, 30, 1},
152500    {582, 1, 10},          {837, 36, 1},          {880, 1, 4},
152501    {886, 0, 1},           {902, 18, 1},          {904, 16, 3},
152502    {908, 26, 1},          {910, 24, 2},          {913, 14, 17},
152503    {931, 14, 9},          {962, 0, 1},           {975, 4, 1},
152504    {976, 140, 1},         {977, 142, 1},         {981, 146, 1},
152505    {982, 144, 1},         {984, 1, 24},          {1008, 136, 1},
152506    {1009, 138, 1},        {1012, 130, 1},        {1013, 128, 1},
152507    {1015, 0, 1},          {1017, 152, 1},        {1018, 0, 1},
152508    {1021, 110, 3},        {1024, 34, 16},        {1040, 14, 32},
152509    {1120, 1, 34},         {1162, 1, 54},         {1216, 6, 1},
152510    {1217, 1, 14},         {1232, 1, 88},         {1329, 22, 38},
152511    {4256, 66, 38},        {4295, 66, 1},         {4301, 66, 1},
152512    {7680, 1, 150},        {7835, 132, 1},        {7838, 96, 1},
152513    {7840, 1, 96},         {7944, 150, 8},        {7960, 150, 6},
152514    {7976, 150, 8},        {7992, 150, 8},        {8008, 150, 6},
152515    {8025, 151, 8},        {8040, 150, 8},        {8072, 150, 8},
152516    {8088, 150, 8},        {8104, 150, 8},        {8120, 150, 2},
152517    {8122, 126, 2},        {8124, 148, 1},        {8126, 100, 1},
152518    {8136, 124, 4},        {8140, 148, 1},        {8152, 150, 2},
152519    {8154, 120, 2},        {8168, 150, 2},        {8170, 118, 2},
152520    {8172, 152, 1},        {8184, 112, 2},        {8186, 114, 2},
152521    {8188, 148, 1},        {8486, 98, 1},         {8490, 92, 1},
152522    {8491, 94, 1},         {8498, 12, 1},         {8544, 8, 16},
152523    {8579, 0, 1},          {9398, 10, 26},        {11264, 22, 47},
152524    {11360, 0, 1},         {11362, 88, 1},        {11363, 102, 1},
152525    {11364, 90, 1},        {11367, 1, 6},         {11373, 84, 1},
152526    {11374, 86, 1},        {11375, 80, 1},        {11376, 82, 1},
152527    {11378, 0, 1},         {11381, 0, 1},         {11390, 78, 2},
152528    {11392, 1, 100},       {11499, 1, 4},         {11506, 0, 1},
152529    {42560, 1, 46},        {42624, 1, 24},        {42786, 1, 14},
152530    {42802, 1, 62},        {42873, 1, 4},         {42877, 76, 1},
152531    {42878, 1, 10},        {42891, 0, 1},         {42893, 74, 1},
152532    {42896, 1, 4},         {42912, 1, 10},        {42922, 72, 1},
152533    {65313, 14, 26},
152534  };
152535  static const unsigned short aiOff[] = {
152536   1,     2,     8,     15,    16,    26,    28,    32,
152537   37,    38,    40,    48,    63,    64,    69,    71,
152538   79,    80,    116,   202,   203,   205,   206,   207,
152539   209,   210,   211,   213,   214,   217,   218,   219,
152540   775,   7264,  10792, 10795, 23228, 23256, 30204, 54721,
152541   54753, 54754, 54756, 54787, 54793, 54809, 57153, 57274,
152542   57921, 58019, 58363, 61722, 65268, 65341, 65373, 65406,
152543   65408, 65410, 65415, 65424, 65436, 65439, 65450, 65462,
152544   65472, 65476, 65478, 65480, 65482, 65488, 65506, 65511,
152545   65514, 65521, 65527, 65528, 65529,
152546  };
152547
152548  int ret = c;
152549
152550  assert( c>=0 );
152551  assert( sizeof(unsigned short)==2 && sizeof(unsigned char)==1 );
152552
152553  if( c<128 ){
152554    if( c>='A' && c<='Z' ) ret = c + ('a' - 'A');
152555  }else if( c<65536 ){
152556    int iHi = sizeof(aEntry)/sizeof(aEntry[0]) - 1;
152557    int iLo = 0;
152558    int iRes = -1;
152559
152560    while( iHi>=iLo ){
152561      int iTest = (iHi + iLo) / 2;
152562      int cmp = (c - aEntry[iTest].iCode);
152563      if( cmp>=0 ){
152564        iRes = iTest;
152565        iLo = iTest+1;
152566      }else{
152567        iHi = iTest-1;
152568      }
152569    }
152570    assert( iRes<0 || c>=aEntry[iRes].iCode );
152571
152572    if( iRes>=0 ){
152573      const struct TableEntry *p = &aEntry[iRes];
152574      if( c<(p->iCode + p->nRange) && 0==(0x01 & p->flags & (p->iCode ^ c)) ){
152575        ret = (c + (aiOff[p->flags>>1])) & 0x0000FFFF;
152576        assert( ret>0 );
152577      }
152578    }
152579
152580    if( bRemoveDiacritic ) ret = remove_diacritic(ret);
152581  }
152582
152583  else if( c>=66560 && c<66600 ){
152584    ret = c + 40;
152585  }
152586
152587  return ret;
152588}
152589#endif /* defined(SQLITE_ENABLE_FTS3) || defined(SQLITE_ENABLE_FTS4) */
152590#endif /* !defined(SQLITE_DISABLE_FTS3_UNICODE) */
152591
152592/************** End of fts3_unicode2.c ***************************************/
152593/************** Begin file rtree.c *******************************************/
152594/*
152595** 2001 September 15
152596**
152597** The author disclaims copyright to this source code.  In place of
152598** a legal notice, here is a blessing:
152599**
152600**    May you do good and not evil.
152601**    May you find forgiveness for yourself and forgive others.
152602**    May you share freely, never taking more than you give.
152603**
152604*************************************************************************
152605** This file contains code for implementations of the r-tree and r*-tree
152606** algorithms packaged as an SQLite virtual table module.
152607*/
152608
152609/*
152610** Database Format of R-Tree Tables
152611** --------------------------------
152612**
152613** The data structure for a single virtual r-tree table is stored in three
152614** native SQLite tables declared as follows. In each case, the '%' character
152615** in the table name is replaced with the user-supplied name of the r-tree
152616** table.
152617**
152618**   CREATE TABLE %_node(nodeno INTEGER PRIMARY KEY, data BLOB)
152619**   CREATE TABLE %_parent(nodeno INTEGER PRIMARY KEY, parentnode INTEGER)
152620**   CREATE TABLE %_rowid(rowid INTEGER PRIMARY KEY, nodeno INTEGER)
152621**
152622** The data for each node of the r-tree structure is stored in the %_node
152623** table. For each node that is not the root node of the r-tree, there is
152624** an entry in the %_parent table associating the node with its parent.
152625** And for each row of data in the table, there is an entry in the %_rowid
152626** table that maps from the entries rowid to the id of the node that it
152627** is stored on.
152628**
152629** The root node of an r-tree always exists, even if the r-tree table is
152630** empty. The nodeno of the root node is always 1. All other nodes in the
152631** table must be the same size as the root node. The content of each node
152632** is formatted as follows:
152633**
152634**   1. If the node is the root node (node 1), then the first 2 bytes
152635**      of the node contain the tree depth as a big-endian integer.
152636**      For non-root nodes, the first 2 bytes are left unused.
152637**
152638**   2. The next 2 bytes contain the number of entries currently
152639**      stored in the node.
152640**
152641**   3. The remainder of the node contains the node entries. Each entry
152642**      consists of a single 8-byte integer followed by an even number
152643**      of 4-byte coordinates. For leaf nodes the integer is the rowid
152644**      of a record. For internal nodes it is the node number of a
152645**      child page.
152646*/
152647
152648#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_RTREE)
152649
152650#ifndef SQLITE_CORE
152651/*   #include "sqlite3ext.h" */
152652  SQLITE_EXTENSION_INIT1
152653#else
152654/*   #include "sqlite3.h" */
152655#endif
152656
152657/* #include <string.h> */
152658/* #include <assert.h> */
152659/* #include <stdio.h> */
152660
152661#ifndef SQLITE_AMALGAMATION
152662#include "sqlite3rtree.h"
152663typedef sqlite3_int64 i64;
152664typedef unsigned char u8;
152665typedef unsigned short u16;
152666typedef unsigned int u32;
152667#endif
152668
152669/*  The following macro is used to suppress compiler warnings.
152670*/
152671#ifndef UNUSED_PARAMETER
152672# define UNUSED_PARAMETER(x) (void)(x)
152673#endif
152674
152675typedef struct Rtree Rtree;
152676typedef struct RtreeCursor RtreeCursor;
152677typedef struct RtreeNode RtreeNode;
152678typedef struct RtreeCell RtreeCell;
152679typedef struct RtreeConstraint RtreeConstraint;
152680typedef struct RtreeMatchArg RtreeMatchArg;
152681typedef struct RtreeGeomCallback RtreeGeomCallback;
152682typedef union RtreeCoord RtreeCoord;
152683typedef struct RtreeSearchPoint RtreeSearchPoint;
152684
152685/* The rtree may have between 1 and RTREE_MAX_DIMENSIONS dimensions. */
152686#define RTREE_MAX_DIMENSIONS 5
152687
152688/* Size of hash table Rtree.aHash. This hash table is not expected to
152689** ever contain very many entries, so a fixed number of buckets is
152690** used.
152691*/
152692#define HASHSIZE 97
152693
152694/* The xBestIndex method of this virtual table requires an estimate of
152695** the number of rows in the virtual table to calculate the costs of
152696** various strategies. If possible, this estimate is loaded from the
152697** sqlite_stat1 table (with RTREE_MIN_ROWEST as a hard-coded minimum).
152698** Otherwise, if no sqlite_stat1 entry is available, use
152699** RTREE_DEFAULT_ROWEST.
152700*/
152701#define RTREE_DEFAULT_ROWEST 1048576
152702#define RTREE_MIN_ROWEST         100
152703
152704/*
152705** An rtree virtual-table object.
152706*/
152707struct Rtree {
152708  sqlite3_vtab base;          /* Base class.  Must be first */
152709  sqlite3 *db;                /* Host database connection */
152710  int iNodeSize;              /* Size in bytes of each node in the node table */
152711  u8 nDim;                    /* Number of dimensions */
152712  u8 eCoordType;              /* RTREE_COORD_REAL32 or RTREE_COORD_INT32 */
152713  u8 nBytesPerCell;           /* Bytes consumed per cell */
152714  int iDepth;                 /* Current depth of the r-tree structure */
152715  char *zDb;                  /* Name of database containing r-tree table */
152716  char *zName;                /* Name of r-tree table */
152717  int nBusy;                  /* Current number of users of this structure */
152718  i64 nRowEst;                /* Estimated number of rows in this table */
152719
152720  /* List of nodes removed during a CondenseTree operation. List is
152721  ** linked together via the pointer normally used for hash chains -
152722  ** RtreeNode.pNext. RtreeNode.iNode stores the depth of the sub-tree
152723  ** headed by the node (leaf nodes have RtreeNode.iNode==0).
152724  */
152725  RtreeNode *pDeleted;
152726  int iReinsertHeight;        /* Height of sub-trees Reinsert() has run on */
152727
152728  /* Statements to read/write/delete a record from xxx_node */
152729  sqlite3_stmt *pReadNode;
152730  sqlite3_stmt *pWriteNode;
152731  sqlite3_stmt *pDeleteNode;
152732
152733  /* Statements to read/write/delete a record from xxx_rowid */
152734  sqlite3_stmt *pReadRowid;
152735  sqlite3_stmt *pWriteRowid;
152736  sqlite3_stmt *pDeleteRowid;
152737
152738  /* Statements to read/write/delete a record from xxx_parent */
152739  sqlite3_stmt *pReadParent;
152740  sqlite3_stmt *pWriteParent;
152741  sqlite3_stmt *pDeleteParent;
152742
152743  RtreeNode *aHash[HASHSIZE]; /* Hash table of in-memory nodes. */
152744};
152745
152746/* Possible values for Rtree.eCoordType: */
152747#define RTREE_COORD_REAL32 0
152748#define RTREE_COORD_INT32  1
152749
152750/*
152751** If SQLITE_RTREE_INT_ONLY is defined, then this virtual table will
152752** only deal with integer coordinates.  No floating point operations
152753** will be done.
152754*/
152755#ifdef SQLITE_RTREE_INT_ONLY
152756  typedef sqlite3_int64 RtreeDValue;       /* High accuracy coordinate */
152757  typedef int RtreeValue;                  /* Low accuracy coordinate */
152758# define RTREE_ZERO 0
152759#else
152760  typedef double RtreeDValue;              /* High accuracy coordinate */
152761  typedef float RtreeValue;                /* Low accuracy coordinate */
152762# define RTREE_ZERO 0.0
152763#endif
152764
152765/*
152766** When doing a search of an r-tree, instances of the following structure
152767** record intermediate results from the tree walk.
152768**
152769** The id is always a node-id.  For iLevel>=1 the id is the node-id of
152770** the node that the RtreeSearchPoint represents.  When iLevel==0, however,
152771** the id is of the parent node and the cell that RtreeSearchPoint
152772** represents is the iCell-th entry in the parent node.
152773*/
152774struct RtreeSearchPoint {
152775  RtreeDValue rScore;    /* The score for this node.  Smallest goes first. */
152776  sqlite3_int64 id;      /* Node ID */
152777  u8 iLevel;             /* 0=entries.  1=leaf node.  2+ for higher */
152778  u8 eWithin;            /* PARTLY_WITHIN or FULLY_WITHIN */
152779  u8 iCell;              /* Cell index within the node */
152780};
152781
152782/*
152783** The minimum number of cells allowed for a node is a third of the
152784** maximum. In Gutman's notation:
152785**
152786**     m = M/3
152787**
152788** If an R*-tree "Reinsert" operation is required, the same number of
152789** cells are removed from the overfull node and reinserted into the tree.
152790*/
152791#define RTREE_MINCELLS(p) ((((p)->iNodeSize-4)/(p)->nBytesPerCell)/3)
152792#define RTREE_REINSERT(p) RTREE_MINCELLS(p)
152793#define RTREE_MAXCELLS 51
152794
152795/*
152796** The smallest possible node-size is (512-64)==448 bytes. And the largest
152797** supported cell size is 48 bytes (8 byte rowid + ten 4 byte coordinates).
152798** Therefore all non-root nodes must contain at least 3 entries. Since
152799** 2^40 is greater than 2^64, an r-tree structure always has a depth of
152800** 40 or less.
152801*/
152802#define RTREE_MAX_DEPTH 40
152803
152804
152805/*
152806** Number of entries in the cursor RtreeNode cache.  The first entry is
152807** used to cache the RtreeNode for RtreeCursor.sPoint.  The remaining
152808** entries cache the RtreeNode for the first elements of the priority queue.
152809*/
152810#define RTREE_CACHE_SZ  5
152811
152812/*
152813** An rtree cursor object.
152814*/
152815struct RtreeCursor {
152816  sqlite3_vtab_cursor base;         /* Base class.  Must be first */
152817  u8 atEOF;                         /* True if at end of search */
152818  u8 bPoint;                        /* True if sPoint is valid */
152819  int iStrategy;                    /* Copy of idxNum search parameter */
152820  int nConstraint;                  /* Number of entries in aConstraint */
152821  RtreeConstraint *aConstraint;     /* Search constraints. */
152822  int nPointAlloc;                  /* Number of slots allocated for aPoint[] */
152823  int nPoint;                       /* Number of slots used in aPoint[] */
152824  int mxLevel;                      /* iLevel value for root of the tree */
152825  RtreeSearchPoint *aPoint;         /* Priority queue for search points */
152826  RtreeSearchPoint sPoint;          /* Cached next search point */
152827  RtreeNode *aNode[RTREE_CACHE_SZ]; /* Rtree node cache */
152828  u32 anQueue[RTREE_MAX_DEPTH+1];   /* Number of queued entries by iLevel */
152829};
152830
152831/* Return the Rtree of a RtreeCursor */
152832#define RTREE_OF_CURSOR(X)   ((Rtree*)((X)->base.pVtab))
152833
152834/*
152835** A coordinate can be either a floating point number or a integer.  All
152836** coordinates within a single R-Tree are always of the same time.
152837*/
152838union RtreeCoord {
152839  RtreeValue f;      /* Floating point value */
152840  int i;             /* Integer value */
152841  u32 u;             /* Unsigned for byte-order conversions */
152842};
152843
152844/*
152845** The argument is an RtreeCoord. Return the value stored within the RtreeCoord
152846** formatted as a RtreeDValue (double or int64). This macro assumes that local
152847** variable pRtree points to the Rtree structure associated with the
152848** RtreeCoord.
152849*/
152850#ifdef SQLITE_RTREE_INT_ONLY
152851# define DCOORD(coord) ((RtreeDValue)coord.i)
152852#else
152853# define DCOORD(coord) (                           \
152854    (pRtree->eCoordType==RTREE_COORD_REAL32) ?      \
152855      ((double)coord.f) :                           \
152856      ((double)coord.i)                             \
152857  )
152858#endif
152859
152860/*
152861** A search constraint.
152862*/
152863struct RtreeConstraint {
152864  int iCoord;                     /* Index of constrained coordinate */
152865  int op;                         /* Constraining operation */
152866  union {
152867    RtreeDValue rValue;             /* Constraint value. */
152868    int (*xGeom)(sqlite3_rtree_geometry*,int,RtreeDValue*,int*);
152869    int (*xQueryFunc)(sqlite3_rtree_query_info*);
152870  } u;
152871  sqlite3_rtree_query_info *pInfo;  /* xGeom and xQueryFunc argument */
152872};
152873
152874/* Possible values for RtreeConstraint.op */
152875#define RTREE_EQ    0x41  /* A */
152876#define RTREE_LE    0x42  /* B */
152877#define RTREE_LT    0x43  /* C */
152878#define RTREE_GE    0x44  /* D */
152879#define RTREE_GT    0x45  /* E */
152880#define RTREE_MATCH 0x46  /* F: Old-style sqlite3_rtree_geometry_callback() */
152881#define RTREE_QUERY 0x47  /* G: New-style sqlite3_rtree_query_callback() */
152882
152883
152884/*
152885** An rtree structure node.
152886*/
152887struct RtreeNode {
152888  RtreeNode *pParent;         /* Parent node */
152889  i64 iNode;                  /* The node number */
152890  int nRef;                   /* Number of references to this node */
152891  int isDirty;                /* True if the node needs to be written to disk */
152892  u8 *zData;                  /* Content of the node, as should be on disk */
152893  RtreeNode *pNext;           /* Next node in this hash collision chain */
152894};
152895
152896/* Return the number of cells in a node  */
152897#define NCELL(pNode) readInt16(&(pNode)->zData[2])
152898
152899/*
152900** A single cell from a node, deserialized
152901*/
152902struct RtreeCell {
152903  i64 iRowid;                                 /* Node or entry ID */
152904  RtreeCoord aCoord[RTREE_MAX_DIMENSIONS*2];  /* Bounding box coordinates */
152905};
152906
152907
152908/*
152909** This object becomes the sqlite3_user_data() for the SQL functions
152910** that are created by sqlite3_rtree_geometry_callback() and
152911** sqlite3_rtree_query_callback() and which appear on the right of MATCH
152912** operators in order to constrain a search.
152913**
152914** xGeom and xQueryFunc are the callback functions.  Exactly one of
152915** xGeom and xQueryFunc fields is non-NULL, depending on whether the
152916** SQL function was created using sqlite3_rtree_geometry_callback() or
152917** sqlite3_rtree_query_callback().
152918**
152919** This object is deleted automatically by the destructor mechanism in
152920** sqlite3_create_function_v2().
152921*/
152922struct RtreeGeomCallback {
152923  int (*xGeom)(sqlite3_rtree_geometry*, int, RtreeDValue*, int*);
152924  int (*xQueryFunc)(sqlite3_rtree_query_info*);
152925  void (*xDestructor)(void*);
152926  void *pContext;
152927};
152928
152929
152930/*
152931** Value for the first field of every RtreeMatchArg object. The MATCH
152932** operator tests that the first field of a blob operand matches this
152933** value to avoid operating on invalid blobs (which could cause a segfault).
152934*/
152935#define RTREE_GEOMETRY_MAGIC 0x891245AB
152936
152937/*
152938** An instance of this structure (in the form of a BLOB) is returned by
152939** the SQL functions that sqlite3_rtree_geometry_callback() and
152940** sqlite3_rtree_query_callback() create, and is read as the right-hand
152941** operand to the MATCH operator of an R-Tree.
152942*/
152943struct RtreeMatchArg {
152944  u32 magic;                  /* Always RTREE_GEOMETRY_MAGIC */
152945  RtreeGeomCallback cb;       /* Info about the callback functions */
152946  int nParam;                 /* Number of parameters to the SQL function */
152947  sqlite3_value **apSqlParam; /* Original SQL parameter values */
152948  RtreeDValue aParam[1];      /* Values for parameters to the SQL function */
152949};
152950
152951#ifndef MAX
152952# define MAX(x,y) ((x) < (y) ? (y) : (x))
152953#endif
152954#ifndef MIN
152955# define MIN(x,y) ((x) > (y) ? (y) : (x))
152956#endif
152957
152958/*
152959** Functions to deserialize a 16 bit integer, 32 bit real number and
152960** 64 bit integer. The deserialized value is returned.
152961*/
152962static int readInt16(u8 *p){
152963  return (p[0]<<8) + p[1];
152964}
152965static void readCoord(u8 *p, RtreeCoord *pCoord){
152966  pCoord->u = (
152967    (((u32)p[0]) << 24) +
152968    (((u32)p[1]) << 16) +
152969    (((u32)p[2]) <<  8) +
152970    (((u32)p[3]) <<  0)
152971  );
152972}
152973static i64 readInt64(u8 *p){
152974  return (
152975    (((i64)p[0]) << 56) +
152976    (((i64)p[1]) << 48) +
152977    (((i64)p[2]) << 40) +
152978    (((i64)p[3]) << 32) +
152979    (((i64)p[4]) << 24) +
152980    (((i64)p[5]) << 16) +
152981    (((i64)p[6]) <<  8) +
152982    (((i64)p[7]) <<  0)
152983  );
152984}
152985
152986/*
152987** Functions to serialize a 16 bit integer, 32 bit real number and
152988** 64 bit integer. The value returned is the number of bytes written
152989** to the argument buffer (always 2, 4 and 8 respectively).
152990*/
152991static int writeInt16(u8 *p, int i){
152992  p[0] = (i>> 8)&0xFF;
152993  p[1] = (i>> 0)&0xFF;
152994  return 2;
152995}
152996static int writeCoord(u8 *p, RtreeCoord *pCoord){
152997  u32 i;
152998  assert( sizeof(RtreeCoord)==4 );
152999  assert( sizeof(u32)==4 );
153000  i = pCoord->u;
153001  p[0] = (i>>24)&0xFF;
153002  p[1] = (i>>16)&0xFF;
153003  p[2] = (i>> 8)&0xFF;
153004  p[3] = (i>> 0)&0xFF;
153005  return 4;
153006}
153007static int writeInt64(u8 *p, i64 i){
153008  p[0] = (i>>56)&0xFF;
153009  p[1] = (i>>48)&0xFF;
153010  p[2] = (i>>40)&0xFF;
153011  p[3] = (i>>32)&0xFF;
153012  p[4] = (i>>24)&0xFF;
153013  p[5] = (i>>16)&0xFF;
153014  p[6] = (i>> 8)&0xFF;
153015  p[7] = (i>> 0)&0xFF;
153016  return 8;
153017}
153018
153019/*
153020** Increment the reference count of node p.
153021*/
153022static void nodeReference(RtreeNode *p){
153023  if( p ){
153024    p->nRef++;
153025  }
153026}
153027
153028/*
153029** Clear the content of node p (set all bytes to 0x00).
153030*/
153031static void nodeZero(Rtree *pRtree, RtreeNode *p){
153032  memset(&p->zData[2], 0, pRtree->iNodeSize-2);
153033  p->isDirty = 1;
153034}
153035
153036/*
153037** Given a node number iNode, return the corresponding key to use
153038** in the Rtree.aHash table.
153039*/
153040static int nodeHash(i64 iNode){
153041  return iNode % HASHSIZE;
153042}
153043
153044/*
153045** Search the node hash table for node iNode. If found, return a pointer
153046** to it. Otherwise, return 0.
153047*/
153048static RtreeNode *nodeHashLookup(Rtree *pRtree, i64 iNode){
153049  RtreeNode *p;
153050  for(p=pRtree->aHash[nodeHash(iNode)]; p && p->iNode!=iNode; p=p->pNext);
153051  return p;
153052}
153053
153054/*
153055** Add node pNode to the node hash table.
153056*/
153057static void nodeHashInsert(Rtree *pRtree, RtreeNode *pNode){
153058  int iHash;
153059  assert( pNode->pNext==0 );
153060  iHash = nodeHash(pNode->iNode);
153061  pNode->pNext = pRtree->aHash[iHash];
153062  pRtree->aHash[iHash] = pNode;
153063}
153064
153065/*
153066** Remove node pNode from the node hash table.
153067*/
153068static void nodeHashDelete(Rtree *pRtree, RtreeNode *pNode){
153069  RtreeNode **pp;
153070  if( pNode->iNode!=0 ){
153071    pp = &pRtree->aHash[nodeHash(pNode->iNode)];
153072    for( ; (*pp)!=pNode; pp = &(*pp)->pNext){ assert(*pp); }
153073    *pp = pNode->pNext;
153074    pNode->pNext = 0;
153075  }
153076}
153077
153078/*
153079** Allocate and return new r-tree node. Initially, (RtreeNode.iNode==0),
153080** indicating that node has not yet been assigned a node number. It is
153081** assigned a node number when nodeWrite() is called to write the
153082** node contents out to the database.
153083*/
153084static RtreeNode *nodeNew(Rtree *pRtree, RtreeNode *pParent){
153085  RtreeNode *pNode;
153086  pNode = (RtreeNode *)sqlite3_malloc(sizeof(RtreeNode) + pRtree->iNodeSize);
153087  if( pNode ){
153088    memset(pNode, 0, sizeof(RtreeNode) + pRtree->iNodeSize);
153089    pNode->zData = (u8 *)&pNode[1];
153090    pNode->nRef = 1;
153091    pNode->pParent = pParent;
153092    pNode->isDirty = 1;
153093    nodeReference(pParent);
153094  }
153095  return pNode;
153096}
153097
153098/*
153099** Obtain a reference to an r-tree node.
153100*/
153101static int nodeAcquire(
153102  Rtree *pRtree,             /* R-tree structure */
153103  i64 iNode,                 /* Node number to load */
153104  RtreeNode *pParent,        /* Either the parent node or NULL */
153105  RtreeNode **ppNode         /* OUT: Acquired node */
153106){
153107  int rc;
153108  int rc2 = SQLITE_OK;
153109  RtreeNode *pNode;
153110
153111  /* Check if the requested node is already in the hash table. If so,
153112  ** increase its reference count and return it.
153113  */
153114  if( (pNode = nodeHashLookup(pRtree, iNode)) ){
153115    assert( !pParent || !pNode->pParent || pNode->pParent==pParent );
153116    if( pParent && !pNode->pParent ){
153117      nodeReference(pParent);
153118      pNode->pParent = pParent;
153119    }
153120    pNode->nRef++;
153121    *ppNode = pNode;
153122    return SQLITE_OK;
153123  }
153124
153125  sqlite3_bind_int64(pRtree->pReadNode, 1, iNode);
153126  rc = sqlite3_step(pRtree->pReadNode);
153127  if( rc==SQLITE_ROW ){
153128    const u8 *zBlob = sqlite3_column_blob(pRtree->pReadNode, 0);
153129    if( pRtree->iNodeSize==sqlite3_column_bytes(pRtree->pReadNode, 0) ){
153130      pNode = (RtreeNode *)sqlite3_malloc(sizeof(RtreeNode)+pRtree->iNodeSize);
153131      if( !pNode ){
153132        rc2 = SQLITE_NOMEM;
153133      }else{
153134        pNode->pParent = pParent;
153135        pNode->zData = (u8 *)&pNode[1];
153136        pNode->nRef = 1;
153137        pNode->iNode = iNode;
153138        pNode->isDirty = 0;
153139        pNode->pNext = 0;
153140        memcpy(pNode->zData, zBlob, pRtree->iNodeSize);
153141        nodeReference(pParent);
153142      }
153143    }
153144  }
153145  rc = sqlite3_reset(pRtree->pReadNode);
153146  if( rc==SQLITE_OK ) rc = rc2;
153147
153148  /* If the root node was just loaded, set pRtree->iDepth to the height
153149  ** of the r-tree structure. A height of zero means all data is stored on
153150  ** the root node. A height of one means the children of the root node
153151  ** are the leaves, and so on. If the depth as specified on the root node
153152  ** is greater than RTREE_MAX_DEPTH, the r-tree structure must be corrupt.
153153  */
153154  if( pNode && iNode==1 ){
153155    pRtree->iDepth = readInt16(pNode->zData);
153156    if( pRtree->iDepth>RTREE_MAX_DEPTH ){
153157      rc = SQLITE_CORRUPT_VTAB;
153158    }
153159  }
153160
153161  /* If no error has occurred so far, check if the "number of entries"
153162  ** field on the node is too large. If so, set the return code to
153163  ** SQLITE_CORRUPT_VTAB.
153164  */
153165  if( pNode && rc==SQLITE_OK ){
153166    if( NCELL(pNode)>((pRtree->iNodeSize-4)/pRtree->nBytesPerCell) ){
153167      rc = SQLITE_CORRUPT_VTAB;
153168    }
153169  }
153170
153171  if( rc==SQLITE_OK ){
153172    if( pNode!=0 ){
153173      nodeHashInsert(pRtree, pNode);
153174    }else{
153175      rc = SQLITE_CORRUPT_VTAB;
153176    }
153177    *ppNode = pNode;
153178  }else{
153179    sqlite3_free(pNode);
153180    *ppNode = 0;
153181  }
153182
153183  return rc;
153184}
153185
153186/*
153187** Overwrite cell iCell of node pNode with the contents of pCell.
153188*/
153189static void nodeOverwriteCell(
153190  Rtree *pRtree,             /* The overall R-Tree */
153191  RtreeNode *pNode,          /* The node into which the cell is to be written */
153192  RtreeCell *pCell,          /* The cell to write */
153193  int iCell                  /* Index into pNode into which pCell is written */
153194){
153195  int ii;
153196  u8 *p = &pNode->zData[4 + pRtree->nBytesPerCell*iCell];
153197  p += writeInt64(p, pCell->iRowid);
153198  for(ii=0; ii<(pRtree->nDim*2); ii++){
153199    p += writeCoord(p, &pCell->aCoord[ii]);
153200  }
153201  pNode->isDirty = 1;
153202}
153203
153204/*
153205** Remove the cell with index iCell from node pNode.
153206*/
153207static void nodeDeleteCell(Rtree *pRtree, RtreeNode *pNode, int iCell){
153208  u8 *pDst = &pNode->zData[4 + pRtree->nBytesPerCell*iCell];
153209  u8 *pSrc = &pDst[pRtree->nBytesPerCell];
153210  int nByte = (NCELL(pNode) - iCell - 1) * pRtree->nBytesPerCell;
153211  memmove(pDst, pSrc, nByte);
153212  writeInt16(&pNode->zData[2], NCELL(pNode)-1);
153213  pNode->isDirty = 1;
153214}
153215
153216/*
153217** Insert the contents of cell pCell into node pNode. If the insert
153218** is successful, return SQLITE_OK.
153219**
153220** If there is not enough free space in pNode, return SQLITE_FULL.
153221*/
153222static int nodeInsertCell(
153223  Rtree *pRtree,                /* The overall R-Tree */
153224  RtreeNode *pNode,             /* Write new cell into this node */
153225  RtreeCell *pCell              /* The cell to be inserted */
153226){
153227  int nCell;                    /* Current number of cells in pNode */
153228  int nMaxCell;                 /* Maximum number of cells for pNode */
153229
153230  nMaxCell = (pRtree->iNodeSize-4)/pRtree->nBytesPerCell;
153231  nCell = NCELL(pNode);
153232
153233  assert( nCell<=nMaxCell );
153234  if( nCell<nMaxCell ){
153235    nodeOverwriteCell(pRtree, pNode, pCell, nCell);
153236    writeInt16(&pNode->zData[2], nCell+1);
153237    pNode->isDirty = 1;
153238  }
153239
153240  return (nCell==nMaxCell);
153241}
153242
153243/*
153244** If the node is dirty, write it out to the database.
153245*/
153246static int nodeWrite(Rtree *pRtree, RtreeNode *pNode){
153247  int rc = SQLITE_OK;
153248  if( pNode->isDirty ){
153249    sqlite3_stmt *p = pRtree->pWriteNode;
153250    if( pNode->iNode ){
153251      sqlite3_bind_int64(p, 1, pNode->iNode);
153252    }else{
153253      sqlite3_bind_null(p, 1);
153254    }
153255    sqlite3_bind_blob(p, 2, pNode->zData, pRtree->iNodeSize, SQLITE_STATIC);
153256    sqlite3_step(p);
153257    pNode->isDirty = 0;
153258    rc = sqlite3_reset(p);
153259    if( pNode->iNode==0 && rc==SQLITE_OK ){
153260      pNode->iNode = sqlite3_last_insert_rowid(pRtree->db);
153261      nodeHashInsert(pRtree, pNode);
153262    }
153263  }
153264  return rc;
153265}
153266
153267/*
153268** Release a reference to a node. If the node is dirty and the reference
153269** count drops to zero, the node data is written to the database.
153270*/
153271static int nodeRelease(Rtree *pRtree, RtreeNode *pNode){
153272  int rc = SQLITE_OK;
153273  if( pNode ){
153274    assert( pNode->nRef>0 );
153275    pNode->nRef--;
153276    if( pNode->nRef==0 ){
153277      if( pNode->iNode==1 ){
153278        pRtree->iDepth = -1;
153279      }
153280      if( pNode->pParent ){
153281        rc = nodeRelease(pRtree, pNode->pParent);
153282      }
153283      if( rc==SQLITE_OK ){
153284        rc = nodeWrite(pRtree, pNode);
153285      }
153286      nodeHashDelete(pRtree, pNode);
153287      sqlite3_free(pNode);
153288    }
153289  }
153290  return rc;
153291}
153292
153293/*
153294** Return the 64-bit integer value associated with cell iCell of
153295** node pNode. If pNode is a leaf node, this is a rowid. If it is
153296** an internal node, then the 64-bit integer is a child page number.
153297*/
153298static i64 nodeGetRowid(
153299  Rtree *pRtree,       /* The overall R-Tree */
153300  RtreeNode *pNode,    /* The node from which to extract the ID */
153301  int iCell            /* The cell index from which to extract the ID */
153302){
153303  assert( iCell<NCELL(pNode) );
153304  return readInt64(&pNode->zData[4 + pRtree->nBytesPerCell*iCell]);
153305}
153306
153307/*
153308** Return coordinate iCoord from cell iCell in node pNode.
153309*/
153310static void nodeGetCoord(
153311  Rtree *pRtree,               /* The overall R-Tree */
153312  RtreeNode *pNode,            /* The node from which to extract a coordinate */
153313  int iCell,                   /* The index of the cell within the node */
153314  int iCoord,                  /* Which coordinate to extract */
153315  RtreeCoord *pCoord           /* OUT: Space to write result to */
153316){
153317  readCoord(&pNode->zData[12 + pRtree->nBytesPerCell*iCell + 4*iCoord], pCoord);
153318}
153319
153320/*
153321** Deserialize cell iCell of node pNode. Populate the structure pointed
153322** to by pCell with the results.
153323*/
153324static void nodeGetCell(
153325  Rtree *pRtree,               /* The overall R-Tree */
153326  RtreeNode *pNode,            /* The node containing the cell to be read */
153327  int iCell,                   /* Index of the cell within the node */
153328  RtreeCell *pCell             /* OUT: Write the cell contents here */
153329){
153330  u8 *pData;
153331  RtreeCoord *pCoord;
153332  int ii;
153333  pCell->iRowid = nodeGetRowid(pRtree, pNode, iCell);
153334  pData = pNode->zData + (12 + pRtree->nBytesPerCell*iCell);
153335  pCoord = pCell->aCoord;
153336  for(ii=0; ii<pRtree->nDim*2; ii++){
153337    readCoord(&pData[ii*4], &pCoord[ii]);
153338  }
153339}
153340
153341
153342/* Forward declaration for the function that does the work of
153343** the virtual table module xCreate() and xConnect() methods.
153344*/
153345static int rtreeInit(
153346  sqlite3 *, void *, int, const char *const*, sqlite3_vtab **, char **, int
153347);
153348
153349/*
153350** Rtree virtual table module xCreate method.
153351*/
153352static int rtreeCreate(
153353  sqlite3 *db,
153354  void *pAux,
153355  int argc, const char *const*argv,
153356  sqlite3_vtab **ppVtab,
153357  char **pzErr
153358){
153359  return rtreeInit(db, pAux, argc, argv, ppVtab, pzErr, 1);
153360}
153361
153362/*
153363** Rtree virtual table module xConnect method.
153364*/
153365static int rtreeConnect(
153366  sqlite3 *db,
153367  void *pAux,
153368  int argc, const char *const*argv,
153369  sqlite3_vtab **ppVtab,
153370  char **pzErr
153371){
153372  return rtreeInit(db, pAux, argc, argv, ppVtab, pzErr, 0);
153373}
153374
153375/*
153376** Increment the r-tree reference count.
153377*/
153378static void rtreeReference(Rtree *pRtree){
153379  pRtree->nBusy++;
153380}
153381
153382/*
153383** Decrement the r-tree reference count. When the reference count reaches
153384** zero the structure is deleted.
153385*/
153386static void rtreeRelease(Rtree *pRtree){
153387  pRtree->nBusy--;
153388  if( pRtree->nBusy==0 ){
153389    sqlite3_finalize(pRtree->pReadNode);
153390    sqlite3_finalize(pRtree->pWriteNode);
153391    sqlite3_finalize(pRtree->pDeleteNode);
153392    sqlite3_finalize(pRtree->pReadRowid);
153393    sqlite3_finalize(pRtree->pWriteRowid);
153394    sqlite3_finalize(pRtree->pDeleteRowid);
153395    sqlite3_finalize(pRtree->pReadParent);
153396    sqlite3_finalize(pRtree->pWriteParent);
153397    sqlite3_finalize(pRtree->pDeleteParent);
153398    sqlite3_free(pRtree);
153399  }
153400}
153401
153402/*
153403** Rtree virtual table module xDisconnect method.
153404*/
153405static int rtreeDisconnect(sqlite3_vtab *pVtab){
153406  rtreeRelease((Rtree *)pVtab);
153407  return SQLITE_OK;
153408}
153409
153410/*
153411** Rtree virtual table module xDestroy method.
153412*/
153413static int rtreeDestroy(sqlite3_vtab *pVtab){
153414  Rtree *pRtree = (Rtree *)pVtab;
153415  int rc;
153416  char *zCreate = sqlite3_mprintf(
153417    "DROP TABLE '%q'.'%q_node';"
153418    "DROP TABLE '%q'.'%q_rowid';"
153419    "DROP TABLE '%q'.'%q_parent';",
153420    pRtree->zDb, pRtree->zName,
153421    pRtree->zDb, pRtree->zName,
153422    pRtree->zDb, pRtree->zName
153423  );
153424  if( !zCreate ){
153425    rc = SQLITE_NOMEM;
153426  }else{
153427    rc = sqlite3_exec(pRtree->db, zCreate, 0, 0, 0);
153428    sqlite3_free(zCreate);
153429  }
153430  if( rc==SQLITE_OK ){
153431    rtreeRelease(pRtree);
153432  }
153433
153434  return rc;
153435}
153436
153437/*
153438** Rtree virtual table module xOpen method.
153439*/
153440static int rtreeOpen(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor){
153441  int rc = SQLITE_NOMEM;
153442  RtreeCursor *pCsr;
153443
153444  pCsr = (RtreeCursor *)sqlite3_malloc(sizeof(RtreeCursor));
153445  if( pCsr ){
153446    memset(pCsr, 0, sizeof(RtreeCursor));
153447    pCsr->base.pVtab = pVTab;
153448    rc = SQLITE_OK;
153449  }
153450  *ppCursor = (sqlite3_vtab_cursor *)pCsr;
153451
153452  return rc;
153453}
153454
153455
153456/*
153457** Free the RtreeCursor.aConstraint[] array and its contents.
153458*/
153459static void freeCursorConstraints(RtreeCursor *pCsr){
153460  if( pCsr->aConstraint ){
153461    int i;                        /* Used to iterate through constraint array */
153462    for(i=0; i<pCsr->nConstraint; i++){
153463      sqlite3_rtree_query_info *pInfo = pCsr->aConstraint[i].pInfo;
153464      if( pInfo ){
153465        if( pInfo->xDelUser ) pInfo->xDelUser(pInfo->pUser);
153466        sqlite3_free(pInfo);
153467      }
153468    }
153469    sqlite3_free(pCsr->aConstraint);
153470    pCsr->aConstraint = 0;
153471  }
153472}
153473
153474/*
153475** Rtree virtual table module xClose method.
153476*/
153477static int rtreeClose(sqlite3_vtab_cursor *cur){
153478  Rtree *pRtree = (Rtree *)(cur->pVtab);
153479  int ii;
153480  RtreeCursor *pCsr = (RtreeCursor *)cur;
153481  freeCursorConstraints(pCsr);
153482  sqlite3_free(pCsr->aPoint);
153483  for(ii=0; ii<RTREE_CACHE_SZ; ii++) nodeRelease(pRtree, pCsr->aNode[ii]);
153484  sqlite3_free(pCsr);
153485  return SQLITE_OK;
153486}
153487
153488/*
153489** Rtree virtual table module xEof method.
153490**
153491** Return non-zero if the cursor does not currently point to a valid
153492** record (i.e if the scan has finished), or zero otherwise.
153493*/
153494static int rtreeEof(sqlite3_vtab_cursor *cur){
153495  RtreeCursor *pCsr = (RtreeCursor *)cur;
153496  return pCsr->atEOF;
153497}
153498
153499/*
153500** Convert raw bits from the on-disk RTree record into a coordinate value.
153501** The on-disk format is big-endian and needs to be converted for little-
153502** endian platforms.  The on-disk record stores integer coordinates if
153503** eInt is true and it stores 32-bit floating point records if eInt is
153504** false.  a[] is the four bytes of the on-disk record to be decoded.
153505** Store the results in "r".
153506**
153507** There are three versions of this macro, one each for little-endian and
153508** big-endian processors and a third generic implementation.  The endian-
153509** specific implementations are much faster and are preferred if the
153510** processor endianness is known at compile-time.  The SQLITE_BYTEORDER
153511** macro is part of sqliteInt.h and hence the endian-specific
153512** implementation will only be used if this module is compiled as part
153513** of the amalgamation.
153514*/
153515#if defined(SQLITE_BYTEORDER) && SQLITE_BYTEORDER==1234
153516#define RTREE_DECODE_COORD(eInt, a, r) {                        \
153517    RtreeCoord c;    /* Coordinate decoded */                   \
153518    memcpy(&c.u,a,4);                                           \
153519    c.u = ((c.u>>24)&0xff)|((c.u>>8)&0xff00)|                   \
153520          ((c.u&0xff)<<24)|((c.u&0xff00)<<8);                   \
153521    r = eInt ? (sqlite3_rtree_dbl)c.i : (sqlite3_rtree_dbl)c.f; \
153522}
153523#elif defined(SQLITE_BYTEORDER) && SQLITE_BYTEORDER==4321
153524#define RTREE_DECODE_COORD(eInt, a, r) {                        \
153525    RtreeCoord c;    /* Coordinate decoded */                   \
153526    memcpy(&c.u,a,4);                                           \
153527    r = eInt ? (sqlite3_rtree_dbl)c.i : (sqlite3_rtree_dbl)c.f; \
153528}
153529#else
153530#define RTREE_DECODE_COORD(eInt, a, r) {                        \
153531    RtreeCoord c;    /* Coordinate decoded */                   \
153532    c.u = ((u32)a[0]<<24) + ((u32)a[1]<<16)                     \
153533           +((u32)a[2]<<8) + a[3];                              \
153534    r = eInt ? (sqlite3_rtree_dbl)c.i : (sqlite3_rtree_dbl)c.f; \
153535}
153536#endif
153537
153538/*
153539** Check the RTree node or entry given by pCellData and p against the MATCH
153540** constraint pConstraint.
153541*/
153542static int rtreeCallbackConstraint(
153543  RtreeConstraint *pConstraint,  /* The constraint to test */
153544  int eInt,                      /* True if RTree holding integer coordinates */
153545  u8 *pCellData,                 /* Raw cell content */
153546  RtreeSearchPoint *pSearch,     /* Container of this cell */
153547  sqlite3_rtree_dbl *prScore,    /* OUT: score for the cell */
153548  int *peWithin                  /* OUT: visibility of the cell */
153549){
153550  int i;                                                /* Loop counter */
153551  sqlite3_rtree_query_info *pInfo = pConstraint->pInfo; /* Callback info */
153552  int nCoord = pInfo->nCoord;                           /* No. of coordinates */
153553  int rc;                                             /* Callback return code */
153554  sqlite3_rtree_dbl aCoord[RTREE_MAX_DIMENSIONS*2];   /* Decoded coordinates */
153555
153556  assert( pConstraint->op==RTREE_MATCH || pConstraint->op==RTREE_QUERY );
153557  assert( nCoord==2 || nCoord==4 || nCoord==6 || nCoord==8 || nCoord==10 );
153558
153559  if( pConstraint->op==RTREE_QUERY && pSearch->iLevel==1 ){
153560    pInfo->iRowid = readInt64(pCellData);
153561  }
153562  pCellData += 8;
153563  for(i=0; i<nCoord; i++, pCellData += 4){
153564    RTREE_DECODE_COORD(eInt, pCellData, aCoord[i]);
153565  }
153566  if( pConstraint->op==RTREE_MATCH ){
153567    rc = pConstraint->u.xGeom((sqlite3_rtree_geometry*)pInfo,
153568                              nCoord, aCoord, &i);
153569    if( i==0 ) *peWithin = NOT_WITHIN;
153570    *prScore = RTREE_ZERO;
153571  }else{
153572    pInfo->aCoord = aCoord;
153573    pInfo->iLevel = pSearch->iLevel - 1;
153574    pInfo->rScore = pInfo->rParentScore = pSearch->rScore;
153575    pInfo->eWithin = pInfo->eParentWithin = pSearch->eWithin;
153576    rc = pConstraint->u.xQueryFunc(pInfo);
153577    if( pInfo->eWithin<*peWithin ) *peWithin = pInfo->eWithin;
153578    if( pInfo->rScore<*prScore || *prScore<RTREE_ZERO ){
153579      *prScore = pInfo->rScore;
153580    }
153581  }
153582  return rc;
153583}
153584
153585/*
153586** Check the internal RTree node given by pCellData against constraint p.
153587** If this constraint cannot be satisfied by any child within the node,
153588** set *peWithin to NOT_WITHIN.
153589*/
153590static void rtreeNonleafConstraint(
153591  RtreeConstraint *p,        /* The constraint to test */
153592  int eInt,                  /* True if RTree holds integer coordinates */
153593  u8 *pCellData,             /* Raw cell content as appears on disk */
153594  int *peWithin              /* Adjust downward, as appropriate */
153595){
153596  sqlite3_rtree_dbl val;     /* Coordinate value convert to a double */
153597
153598  /* p->iCoord might point to either a lower or upper bound coordinate
153599  ** in a coordinate pair.  But make pCellData point to the lower bound.
153600  */
153601  pCellData += 8 + 4*(p->iCoord&0xfe);
153602
153603  assert(p->op==RTREE_LE || p->op==RTREE_LT || p->op==RTREE_GE
153604      || p->op==RTREE_GT || p->op==RTREE_EQ );
153605  switch( p->op ){
153606    case RTREE_LE:
153607    case RTREE_LT:
153608    case RTREE_EQ:
153609      RTREE_DECODE_COORD(eInt, pCellData, val);
153610      /* val now holds the lower bound of the coordinate pair */
153611      if( p->u.rValue>=val ) return;
153612      if( p->op!=RTREE_EQ ) break;  /* RTREE_LE and RTREE_LT end here */
153613      /* Fall through for the RTREE_EQ case */
153614
153615    default: /* RTREE_GT or RTREE_GE,  or fallthrough of RTREE_EQ */
153616      pCellData += 4;
153617      RTREE_DECODE_COORD(eInt, pCellData, val);
153618      /* val now holds the upper bound of the coordinate pair */
153619      if( p->u.rValue<=val ) return;
153620  }
153621  *peWithin = NOT_WITHIN;
153622}
153623
153624/*
153625** Check the leaf RTree cell given by pCellData against constraint p.
153626** If this constraint is not satisfied, set *peWithin to NOT_WITHIN.
153627** If the constraint is satisfied, leave *peWithin unchanged.
153628**
153629** The constraint is of the form:  xN op $val
153630**
153631** The op is given by p->op.  The xN is p->iCoord-th coordinate in
153632** pCellData.  $val is given by p->u.rValue.
153633*/
153634static void rtreeLeafConstraint(
153635  RtreeConstraint *p,        /* The constraint to test */
153636  int eInt,                  /* True if RTree holds integer coordinates */
153637  u8 *pCellData,             /* Raw cell content as appears on disk */
153638  int *peWithin              /* Adjust downward, as appropriate */
153639){
153640  RtreeDValue xN;      /* Coordinate value converted to a double */
153641
153642  assert(p->op==RTREE_LE || p->op==RTREE_LT || p->op==RTREE_GE
153643      || p->op==RTREE_GT || p->op==RTREE_EQ );
153644  pCellData += 8 + p->iCoord*4;
153645  RTREE_DECODE_COORD(eInt, pCellData, xN);
153646  switch( p->op ){
153647    case RTREE_LE: if( xN <= p->u.rValue ) return;  break;
153648    case RTREE_LT: if( xN <  p->u.rValue ) return;  break;
153649    case RTREE_GE: if( xN >= p->u.rValue ) return;  break;
153650    case RTREE_GT: if( xN >  p->u.rValue ) return;  break;
153651    default:       if( xN == p->u.rValue ) return;  break;
153652  }
153653  *peWithin = NOT_WITHIN;
153654}
153655
153656/*
153657** One of the cells in node pNode is guaranteed to have a 64-bit
153658** integer value equal to iRowid. Return the index of this cell.
153659*/
153660static int nodeRowidIndex(
153661  Rtree *pRtree,
153662  RtreeNode *pNode,
153663  i64 iRowid,
153664  int *piIndex
153665){
153666  int ii;
153667  int nCell = NCELL(pNode);
153668  assert( nCell<200 );
153669  for(ii=0; ii<nCell; ii++){
153670    if( nodeGetRowid(pRtree, pNode, ii)==iRowid ){
153671      *piIndex = ii;
153672      return SQLITE_OK;
153673    }
153674  }
153675  return SQLITE_CORRUPT_VTAB;
153676}
153677
153678/*
153679** Return the index of the cell containing a pointer to node pNode
153680** in its parent. If pNode is the root node, return -1.
153681*/
153682static int nodeParentIndex(Rtree *pRtree, RtreeNode *pNode, int *piIndex){
153683  RtreeNode *pParent = pNode->pParent;
153684  if( pParent ){
153685    return nodeRowidIndex(pRtree, pParent, pNode->iNode, piIndex);
153686  }
153687  *piIndex = -1;
153688  return SQLITE_OK;
153689}
153690
153691/*
153692** Compare two search points.  Return negative, zero, or positive if the first
153693** is less than, equal to, or greater than the second.
153694**
153695** The rScore is the primary key.  Smaller rScore values come first.
153696** If the rScore is a tie, then use iLevel as the tie breaker with smaller
153697** iLevel values coming first.  In this way, if rScore is the same for all
153698** SearchPoints, then iLevel becomes the deciding factor and the result
153699** is a depth-first search, which is the desired default behavior.
153700*/
153701static int rtreeSearchPointCompare(
153702  const RtreeSearchPoint *pA,
153703  const RtreeSearchPoint *pB
153704){
153705  if( pA->rScore<pB->rScore ) return -1;
153706  if( pA->rScore>pB->rScore ) return +1;
153707  if( pA->iLevel<pB->iLevel ) return -1;
153708  if( pA->iLevel>pB->iLevel ) return +1;
153709  return 0;
153710}
153711
153712/*
153713** Interchange to search points in a cursor.
153714*/
153715static void rtreeSearchPointSwap(RtreeCursor *p, int i, int j){
153716  RtreeSearchPoint t = p->aPoint[i];
153717  assert( i<j );
153718  p->aPoint[i] = p->aPoint[j];
153719  p->aPoint[j] = t;
153720  i++; j++;
153721  if( i<RTREE_CACHE_SZ ){
153722    if( j>=RTREE_CACHE_SZ ){
153723      nodeRelease(RTREE_OF_CURSOR(p), p->aNode[i]);
153724      p->aNode[i] = 0;
153725    }else{
153726      RtreeNode *pTemp = p->aNode[i];
153727      p->aNode[i] = p->aNode[j];
153728      p->aNode[j] = pTemp;
153729    }
153730  }
153731}
153732
153733/*
153734** Return the search point with the lowest current score.
153735*/
153736static RtreeSearchPoint *rtreeSearchPointFirst(RtreeCursor *pCur){
153737  return pCur->bPoint ? &pCur->sPoint : pCur->nPoint ? pCur->aPoint : 0;
153738}
153739
153740/*
153741** Get the RtreeNode for the search point with the lowest score.
153742*/
153743static RtreeNode *rtreeNodeOfFirstSearchPoint(RtreeCursor *pCur, int *pRC){
153744  sqlite3_int64 id;
153745  int ii = 1 - pCur->bPoint;
153746  assert( ii==0 || ii==1 );
153747  assert( pCur->bPoint || pCur->nPoint );
153748  if( pCur->aNode[ii]==0 ){
153749    assert( pRC!=0 );
153750    id = ii ? pCur->aPoint[0].id : pCur->sPoint.id;
153751    *pRC = nodeAcquire(RTREE_OF_CURSOR(pCur), id, 0, &pCur->aNode[ii]);
153752  }
153753  return pCur->aNode[ii];
153754}
153755
153756/*
153757** Push a new element onto the priority queue
153758*/
153759static RtreeSearchPoint *rtreeEnqueue(
153760  RtreeCursor *pCur,    /* The cursor */
153761  RtreeDValue rScore,   /* Score for the new search point */
153762  u8 iLevel             /* Level for the new search point */
153763){
153764  int i, j;
153765  RtreeSearchPoint *pNew;
153766  if( pCur->nPoint>=pCur->nPointAlloc ){
153767    int nNew = pCur->nPointAlloc*2 + 8;
153768    pNew = sqlite3_realloc(pCur->aPoint, nNew*sizeof(pCur->aPoint[0]));
153769    if( pNew==0 ) return 0;
153770    pCur->aPoint = pNew;
153771    pCur->nPointAlloc = nNew;
153772  }
153773  i = pCur->nPoint++;
153774  pNew = pCur->aPoint + i;
153775  pNew->rScore = rScore;
153776  pNew->iLevel = iLevel;
153777  assert( iLevel<=RTREE_MAX_DEPTH );
153778  while( i>0 ){
153779    RtreeSearchPoint *pParent;
153780    j = (i-1)/2;
153781    pParent = pCur->aPoint + j;
153782    if( rtreeSearchPointCompare(pNew, pParent)>=0 ) break;
153783    rtreeSearchPointSwap(pCur, j, i);
153784    i = j;
153785    pNew = pParent;
153786  }
153787  return pNew;
153788}
153789
153790/*
153791** Allocate a new RtreeSearchPoint and return a pointer to it.  Return
153792** NULL if malloc fails.
153793*/
153794static RtreeSearchPoint *rtreeSearchPointNew(
153795  RtreeCursor *pCur,    /* The cursor */
153796  RtreeDValue rScore,   /* Score for the new search point */
153797  u8 iLevel             /* Level for the new search point */
153798){
153799  RtreeSearchPoint *pNew, *pFirst;
153800  pFirst = rtreeSearchPointFirst(pCur);
153801  pCur->anQueue[iLevel]++;
153802  if( pFirst==0
153803   || pFirst->rScore>rScore
153804   || (pFirst->rScore==rScore && pFirst->iLevel>iLevel)
153805  ){
153806    if( pCur->bPoint ){
153807      int ii;
153808      pNew = rtreeEnqueue(pCur, rScore, iLevel);
153809      if( pNew==0 ) return 0;
153810      ii = (int)(pNew - pCur->aPoint) + 1;
153811      if( ii<RTREE_CACHE_SZ ){
153812        assert( pCur->aNode[ii]==0 );
153813        pCur->aNode[ii] = pCur->aNode[0];
153814       }else{
153815        nodeRelease(RTREE_OF_CURSOR(pCur), pCur->aNode[0]);
153816      }
153817      pCur->aNode[0] = 0;
153818      *pNew = pCur->sPoint;
153819    }
153820    pCur->sPoint.rScore = rScore;
153821    pCur->sPoint.iLevel = iLevel;
153822    pCur->bPoint = 1;
153823    return &pCur->sPoint;
153824  }else{
153825    return rtreeEnqueue(pCur, rScore, iLevel);
153826  }
153827}
153828
153829#if 0
153830/* Tracing routines for the RtreeSearchPoint queue */
153831static void tracePoint(RtreeSearchPoint *p, int idx, RtreeCursor *pCur){
153832  if( idx<0 ){ printf(" s"); }else{ printf("%2d", idx); }
153833  printf(" %d.%05lld.%02d %g %d",
153834    p->iLevel, p->id, p->iCell, p->rScore, p->eWithin
153835  );
153836  idx++;
153837  if( idx<RTREE_CACHE_SZ ){
153838    printf(" %p\n", pCur->aNode[idx]);
153839  }else{
153840    printf("\n");
153841  }
153842}
153843static void traceQueue(RtreeCursor *pCur, const char *zPrefix){
153844  int ii;
153845  printf("=== %9s ", zPrefix);
153846  if( pCur->bPoint ){
153847    tracePoint(&pCur->sPoint, -1, pCur);
153848  }
153849  for(ii=0; ii<pCur->nPoint; ii++){
153850    if( ii>0 || pCur->bPoint ) printf("              ");
153851    tracePoint(&pCur->aPoint[ii], ii, pCur);
153852  }
153853}
153854# define RTREE_QUEUE_TRACE(A,B) traceQueue(A,B)
153855#else
153856# define RTREE_QUEUE_TRACE(A,B)   /* no-op */
153857#endif
153858
153859/* Remove the search point with the lowest current score.
153860*/
153861static void rtreeSearchPointPop(RtreeCursor *p){
153862  int i, j, k, n;
153863  i = 1 - p->bPoint;
153864  assert( i==0 || i==1 );
153865  if( p->aNode[i] ){
153866    nodeRelease(RTREE_OF_CURSOR(p), p->aNode[i]);
153867    p->aNode[i] = 0;
153868  }
153869  if( p->bPoint ){
153870    p->anQueue[p->sPoint.iLevel]--;
153871    p->bPoint = 0;
153872  }else if( p->nPoint ){
153873    p->anQueue[p->aPoint[0].iLevel]--;
153874    n = --p->nPoint;
153875    p->aPoint[0] = p->aPoint[n];
153876    if( n<RTREE_CACHE_SZ-1 ){
153877      p->aNode[1] = p->aNode[n+1];
153878      p->aNode[n+1] = 0;
153879    }
153880    i = 0;
153881    while( (j = i*2+1)<n ){
153882      k = j+1;
153883      if( k<n && rtreeSearchPointCompare(&p->aPoint[k], &p->aPoint[j])<0 ){
153884        if( rtreeSearchPointCompare(&p->aPoint[k], &p->aPoint[i])<0 ){
153885          rtreeSearchPointSwap(p, i, k);
153886          i = k;
153887        }else{
153888          break;
153889        }
153890      }else{
153891        if( rtreeSearchPointCompare(&p->aPoint[j], &p->aPoint[i])<0 ){
153892          rtreeSearchPointSwap(p, i, j);
153893          i = j;
153894        }else{
153895          break;
153896        }
153897      }
153898    }
153899  }
153900}
153901
153902
153903/*
153904** Continue the search on cursor pCur until the front of the queue
153905** contains an entry suitable for returning as a result-set row,
153906** or until the RtreeSearchPoint queue is empty, indicating that the
153907** query has completed.
153908*/
153909static int rtreeStepToLeaf(RtreeCursor *pCur){
153910  RtreeSearchPoint *p;
153911  Rtree *pRtree = RTREE_OF_CURSOR(pCur);
153912  RtreeNode *pNode;
153913  int eWithin;
153914  int rc = SQLITE_OK;
153915  int nCell;
153916  int nConstraint = pCur->nConstraint;
153917  int ii;
153918  int eInt;
153919  RtreeSearchPoint x;
153920
153921  eInt = pRtree->eCoordType==RTREE_COORD_INT32;
153922  while( (p = rtreeSearchPointFirst(pCur))!=0 && p->iLevel>0 ){
153923    pNode = rtreeNodeOfFirstSearchPoint(pCur, &rc);
153924    if( rc ) return rc;
153925    nCell = NCELL(pNode);
153926    assert( nCell<200 );
153927    while( p->iCell<nCell ){
153928      sqlite3_rtree_dbl rScore = (sqlite3_rtree_dbl)-1;
153929      u8 *pCellData = pNode->zData + (4+pRtree->nBytesPerCell*p->iCell);
153930      eWithin = FULLY_WITHIN;
153931      for(ii=0; ii<nConstraint; ii++){
153932        RtreeConstraint *pConstraint = pCur->aConstraint + ii;
153933        if( pConstraint->op>=RTREE_MATCH ){
153934          rc = rtreeCallbackConstraint(pConstraint, eInt, pCellData, p,
153935                                       &rScore, &eWithin);
153936          if( rc ) return rc;
153937        }else if( p->iLevel==1 ){
153938          rtreeLeafConstraint(pConstraint, eInt, pCellData, &eWithin);
153939        }else{
153940          rtreeNonleafConstraint(pConstraint, eInt, pCellData, &eWithin);
153941        }
153942        if( eWithin==NOT_WITHIN ) break;
153943      }
153944      p->iCell++;
153945      if( eWithin==NOT_WITHIN ) continue;
153946      x.iLevel = p->iLevel - 1;
153947      if( x.iLevel ){
153948        x.id = readInt64(pCellData);
153949        x.iCell = 0;
153950      }else{
153951        x.id = p->id;
153952        x.iCell = p->iCell - 1;
153953      }
153954      if( p->iCell>=nCell ){
153955        RTREE_QUEUE_TRACE(pCur, "POP-S:");
153956        rtreeSearchPointPop(pCur);
153957      }
153958      if( rScore<RTREE_ZERO ) rScore = RTREE_ZERO;
153959      p = rtreeSearchPointNew(pCur, rScore, x.iLevel);
153960      if( p==0 ) return SQLITE_NOMEM;
153961      p->eWithin = eWithin;
153962      p->id = x.id;
153963      p->iCell = x.iCell;
153964      RTREE_QUEUE_TRACE(pCur, "PUSH-S:");
153965      break;
153966    }
153967    if( p->iCell>=nCell ){
153968      RTREE_QUEUE_TRACE(pCur, "POP-Se:");
153969      rtreeSearchPointPop(pCur);
153970    }
153971  }
153972  pCur->atEOF = p==0;
153973  return SQLITE_OK;
153974}
153975
153976/*
153977** Rtree virtual table module xNext method.
153978*/
153979static int rtreeNext(sqlite3_vtab_cursor *pVtabCursor){
153980  RtreeCursor *pCsr = (RtreeCursor *)pVtabCursor;
153981  int rc = SQLITE_OK;
153982
153983  /* Move to the next entry that matches the configured constraints. */
153984  RTREE_QUEUE_TRACE(pCsr, "POP-Nx:");
153985  rtreeSearchPointPop(pCsr);
153986  rc = rtreeStepToLeaf(pCsr);
153987  return rc;
153988}
153989
153990/*
153991** Rtree virtual table module xRowid method.
153992*/
153993static int rtreeRowid(sqlite3_vtab_cursor *pVtabCursor, sqlite_int64 *pRowid){
153994  RtreeCursor *pCsr = (RtreeCursor *)pVtabCursor;
153995  RtreeSearchPoint *p = rtreeSearchPointFirst(pCsr);
153996  int rc = SQLITE_OK;
153997  RtreeNode *pNode = rtreeNodeOfFirstSearchPoint(pCsr, &rc);
153998  if( rc==SQLITE_OK && p ){
153999    *pRowid = nodeGetRowid(RTREE_OF_CURSOR(pCsr), pNode, p->iCell);
154000  }
154001  return rc;
154002}
154003
154004/*
154005** Rtree virtual table module xColumn method.
154006*/
154007static int rtreeColumn(sqlite3_vtab_cursor *cur, sqlite3_context *ctx, int i){
154008  Rtree *pRtree = (Rtree *)cur->pVtab;
154009  RtreeCursor *pCsr = (RtreeCursor *)cur;
154010  RtreeSearchPoint *p = rtreeSearchPointFirst(pCsr);
154011  RtreeCoord c;
154012  int rc = SQLITE_OK;
154013  RtreeNode *pNode = rtreeNodeOfFirstSearchPoint(pCsr, &rc);
154014
154015  if( rc ) return rc;
154016  if( p==0 ) return SQLITE_OK;
154017  if( i==0 ){
154018    sqlite3_result_int64(ctx, nodeGetRowid(pRtree, pNode, p->iCell));
154019  }else{
154020    if( rc ) return rc;
154021    nodeGetCoord(pRtree, pNode, p->iCell, i-1, &c);
154022#ifndef SQLITE_RTREE_INT_ONLY
154023    if( pRtree->eCoordType==RTREE_COORD_REAL32 ){
154024      sqlite3_result_double(ctx, c.f);
154025    }else
154026#endif
154027    {
154028      assert( pRtree->eCoordType==RTREE_COORD_INT32 );
154029      sqlite3_result_int(ctx, c.i);
154030    }
154031  }
154032  return SQLITE_OK;
154033}
154034
154035/*
154036** Use nodeAcquire() to obtain the leaf node containing the record with
154037** rowid iRowid. If successful, set *ppLeaf to point to the node and
154038** return SQLITE_OK. If there is no such record in the table, set
154039** *ppLeaf to 0 and return SQLITE_OK. If an error occurs, set *ppLeaf
154040** to zero and return an SQLite error code.
154041*/
154042static int findLeafNode(
154043  Rtree *pRtree,              /* RTree to search */
154044  i64 iRowid,                 /* The rowid searching for */
154045  RtreeNode **ppLeaf,         /* Write the node here */
154046  sqlite3_int64 *piNode       /* Write the node-id here */
154047){
154048  int rc;
154049  *ppLeaf = 0;
154050  sqlite3_bind_int64(pRtree->pReadRowid, 1, iRowid);
154051  if( sqlite3_step(pRtree->pReadRowid)==SQLITE_ROW ){
154052    i64 iNode = sqlite3_column_int64(pRtree->pReadRowid, 0);
154053    if( piNode ) *piNode = iNode;
154054    rc = nodeAcquire(pRtree, iNode, 0, ppLeaf);
154055    sqlite3_reset(pRtree->pReadRowid);
154056  }else{
154057    rc = sqlite3_reset(pRtree->pReadRowid);
154058  }
154059  return rc;
154060}
154061
154062/*
154063** This function is called to configure the RtreeConstraint object passed
154064** as the second argument for a MATCH constraint. The value passed as the
154065** first argument to this function is the right-hand operand to the MATCH
154066** operator.
154067*/
154068static int deserializeGeometry(sqlite3_value *pValue, RtreeConstraint *pCons){
154069  RtreeMatchArg *pBlob;              /* BLOB returned by geometry function */
154070  sqlite3_rtree_query_info *pInfo;   /* Callback information */
154071  int nBlob;                         /* Size of the geometry function blob */
154072  int nExpected;                     /* Expected size of the BLOB */
154073
154074  /* Check that value is actually a blob. */
154075  if( sqlite3_value_type(pValue)!=SQLITE_BLOB ) return SQLITE_ERROR;
154076
154077  /* Check that the blob is roughly the right size. */
154078  nBlob = sqlite3_value_bytes(pValue);
154079  if( nBlob<(int)sizeof(RtreeMatchArg) ){
154080    return SQLITE_ERROR;
154081  }
154082
154083  pInfo = (sqlite3_rtree_query_info*)sqlite3_malloc( sizeof(*pInfo)+nBlob );
154084  if( !pInfo ) return SQLITE_NOMEM;
154085  memset(pInfo, 0, sizeof(*pInfo));
154086  pBlob = (RtreeMatchArg*)&pInfo[1];
154087
154088  memcpy(pBlob, sqlite3_value_blob(pValue), nBlob);
154089  nExpected = (int)(sizeof(RtreeMatchArg) +
154090                    pBlob->nParam*sizeof(sqlite3_value*) +
154091                    (pBlob->nParam-1)*sizeof(RtreeDValue));
154092  if( pBlob->magic!=RTREE_GEOMETRY_MAGIC || nBlob!=nExpected ){
154093    sqlite3_free(pInfo);
154094    return SQLITE_ERROR;
154095  }
154096  pInfo->pContext = pBlob->cb.pContext;
154097  pInfo->nParam = pBlob->nParam;
154098  pInfo->aParam = pBlob->aParam;
154099  pInfo->apSqlParam = pBlob->apSqlParam;
154100
154101  if( pBlob->cb.xGeom ){
154102    pCons->u.xGeom = pBlob->cb.xGeom;
154103  }else{
154104    pCons->op = RTREE_QUERY;
154105    pCons->u.xQueryFunc = pBlob->cb.xQueryFunc;
154106  }
154107  pCons->pInfo = pInfo;
154108  return SQLITE_OK;
154109}
154110
154111/*
154112** Rtree virtual table module xFilter method.
154113*/
154114static int rtreeFilter(
154115  sqlite3_vtab_cursor *pVtabCursor,
154116  int idxNum, const char *idxStr,
154117  int argc, sqlite3_value **argv
154118){
154119  Rtree *pRtree = (Rtree *)pVtabCursor->pVtab;
154120  RtreeCursor *pCsr = (RtreeCursor *)pVtabCursor;
154121  RtreeNode *pRoot = 0;
154122  int ii;
154123  int rc = SQLITE_OK;
154124  int iCell = 0;
154125
154126  rtreeReference(pRtree);
154127
154128  /* Reset the cursor to the same state as rtreeOpen() leaves it in. */
154129  freeCursorConstraints(pCsr);
154130  sqlite3_free(pCsr->aPoint);
154131  memset(pCsr, 0, sizeof(RtreeCursor));
154132  pCsr->base.pVtab = (sqlite3_vtab*)pRtree;
154133
154134  pCsr->iStrategy = idxNum;
154135  if( idxNum==1 ){
154136    /* Special case - lookup by rowid. */
154137    RtreeNode *pLeaf;        /* Leaf on which the required cell resides */
154138    RtreeSearchPoint *p;     /* Search point for the the leaf */
154139    i64 iRowid = sqlite3_value_int64(argv[0]);
154140    i64 iNode = 0;
154141    rc = findLeafNode(pRtree, iRowid, &pLeaf, &iNode);
154142    if( rc==SQLITE_OK && pLeaf!=0 ){
154143      p = rtreeSearchPointNew(pCsr, RTREE_ZERO, 0);
154144      assert( p!=0 );  /* Always returns pCsr->sPoint */
154145      pCsr->aNode[0] = pLeaf;
154146      p->id = iNode;
154147      p->eWithin = PARTLY_WITHIN;
154148      rc = nodeRowidIndex(pRtree, pLeaf, iRowid, &iCell);
154149      p->iCell = iCell;
154150      RTREE_QUEUE_TRACE(pCsr, "PUSH-F1:");
154151    }else{
154152      pCsr->atEOF = 1;
154153    }
154154  }else{
154155    /* Normal case - r-tree scan. Set up the RtreeCursor.aConstraint array
154156    ** with the configured constraints.
154157    */
154158    rc = nodeAcquire(pRtree, 1, 0, &pRoot);
154159    if( rc==SQLITE_OK && argc>0 ){
154160      pCsr->aConstraint = sqlite3_malloc(sizeof(RtreeConstraint)*argc);
154161      pCsr->nConstraint = argc;
154162      if( !pCsr->aConstraint ){
154163        rc = SQLITE_NOMEM;
154164      }else{
154165        memset(pCsr->aConstraint, 0, sizeof(RtreeConstraint)*argc);
154166        memset(pCsr->anQueue, 0, sizeof(u32)*(pRtree->iDepth + 1));
154167        assert( (idxStr==0 && argc==0)
154168                || (idxStr && (int)strlen(idxStr)==argc*2) );
154169        for(ii=0; ii<argc; ii++){
154170          RtreeConstraint *p = &pCsr->aConstraint[ii];
154171          p->op = idxStr[ii*2];
154172          p->iCoord = idxStr[ii*2+1]-'0';
154173          if( p->op>=RTREE_MATCH ){
154174            /* A MATCH operator. The right-hand-side must be a blob that
154175            ** can be cast into an RtreeMatchArg object. One created using
154176            ** an sqlite3_rtree_geometry_callback() SQL user function.
154177            */
154178            rc = deserializeGeometry(argv[ii], p);
154179            if( rc!=SQLITE_OK ){
154180              break;
154181            }
154182            p->pInfo->nCoord = pRtree->nDim*2;
154183            p->pInfo->anQueue = pCsr->anQueue;
154184            p->pInfo->mxLevel = pRtree->iDepth + 1;
154185          }else{
154186#ifdef SQLITE_RTREE_INT_ONLY
154187            p->u.rValue = sqlite3_value_int64(argv[ii]);
154188#else
154189            p->u.rValue = sqlite3_value_double(argv[ii]);
154190#endif
154191          }
154192        }
154193      }
154194    }
154195    if( rc==SQLITE_OK ){
154196      RtreeSearchPoint *pNew;
154197      pNew = rtreeSearchPointNew(pCsr, RTREE_ZERO, pRtree->iDepth+1);
154198      if( pNew==0 ) return SQLITE_NOMEM;
154199      pNew->id = 1;
154200      pNew->iCell = 0;
154201      pNew->eWithin = PARTLY_WITHIN;
154202      assert( pCsr->bPoint==1 );
154203      pCsr->aNode[0] = pRoot;
154204      pRoot = 0;
154205      RTREE_QUEUE_TRACE(pCsr, "PUSH-Fm:");
154206      rc = rtreeStepToLeaf(pCsr);
154207    }
154208  }
154209
154210  nodeRelease(pRtree, pRoot);
154211  rtreeRelease(pRtree);
154212  return rc;
154213}
154214
154215/*
154216** Set the pIdxInfo->estimatedRows variable to nRow. Unless this
154217** extension is currently being used by a version of SQLite too old to
154218** support estimatedRows. In that case this function is a no-op.
154219*/
154220static void setEstimatedRows(sqlite3_index_info *pIdxInfo, i64 nRow){
154221#if SQLITE_VERSION_NUMBER>=3008002
154222  if( sqlite3_libversion_number()>=3008002 ){
154223    pIdxInfo->estimatedRows = nRow;
154224  }
154225#endif
154226}
154227
154228/*
154229** Rtree virtual table module xBestIndex method. There are three
154230** table scan strategies to choose from (in order from most to
154231** least desirable):
154232**
154233**   idxNum     idxStr        Strategy
154234**   ------------------------------------------------
154235**     1        Unused        Direct lookup by rowid.
154236**     2        See below     R-tree query or full-table scan.
154237**   ------------------------------------------------
154238**
154239** If strategy 1 is used, then idxStr is not meaningful. If strategy
154240** 2 is used, idxStr is formatted to contain 2 bytes for each
154241** constraint used. The first two bytes of idxStr correspond to
154242** the constraint in sqlite3_index_info.aConstraintUsage[] with
154243** (argvIndex==1) etc.
154244**
154245** The first of each pair of bytes in idxStr identifies the constraint
154246** operator as follows:
154247**
154248**   Operator    Byte Value
154249**   ----------------------
154250**      =        0x41 ('A')
154251**     <=        0x42 ('B')
154252**      <        0x43 ('C')
154253**     >=        0x44 ('D')
154254**      >        0x45 ('E')
154255**   MATCH       0x46 ('F')
154256**   ----------------------
154257**
154258** The second of each pair of bytes identifies the coordinate column
154259** to which the constraint applies. The leftmost coordinate column
154260** is 'a', the second from the left 'b' etc.
154261*/
154262static int rtreeBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdxInfo){
154263  Rtree *pRtree = (Rtree*)tab;
154264  int rc = SQLITE_OK;
154265  int ii;
154266  int bMatch = 0;                 /* True if there exists a MATCH constraint */
154267  i64 nRow;                       /* Estimated rows returned by this scan */
154268
154269  int iIdx = 0;
154270  char zIdxStr[RTREE_MAX_DIMENSIONS*8+1];
154271  memset(zIdxStr, 0, sizeof(zIdxStr));
154272
154273  /* Check if there exists a MATCH constraint - even an unusable one. If there
154274  ** is, do not consider the lookup-by-rowid plan as using such a plan would
154275  ** require the VDBE to evaluate the MATCH constraint, which is not currently
154276  ** possible. */
154277  for(ii=0; ii<pIdxInfo->nConstraint; ii++){
154278    if( pIdxInfo->aConstraint[ii].op==SQLITE_INDEX_CONSTRAINT_MATCH ){
154279      bMatch = 1;
154280    }
154281  }
154282
154283  assert( pIdxInfo->idxStr==0 );
154284  for(ii=0; ii<pIdxInfo->nConstraint && iIdx<(int)(sizeof(zIdxStr)-1); ii++){
154285    struct sqlite3_index_constraint *p = &pIdxInfo->aConstraint[ii];
154286
154287    if( bMatch==0 && p->usable
154288     && p->iColumn==0 && p->op==SQLITE_INDEX_CONSTRAINT_EQ
154289    ){
154290      /* We have an equality constraint on the rowid. Use strategy 1. */
154291      int jj;
154292      for(jj=0; jj<ii; jj++){
154293        pIdxInfo->aConstraintUsage[jj].argvIndex = 0;
154294        pIdxInfo->aConstraintUsage[jj].omit = 0;
154295      }
154296      pIdxInfo->idxNum = 1;
154297      pIdxInfo->aConstraintUsage[ii].argvIndex = 1;
154298      pIdxInfo->aConstraintUsage[jj].omit = 1;
154299
154300      /* This strategy involves a two rowid lookups on an B-Tree structures
154301      ** and then a linear search of an R-Tree node. This should be
154302      ** considered almost as quick as a direct rowid lookup (for which
154303      ** sqlite uses an internal cost of 0.0). It is expected to return
154304      ** a single row.
154305      */
154306      pIdxInfo->estimatedCost = 30.0;
154307      setEstimatedRows(pIdxInfo, 1);
154308      return SQLITE_OK;
154309    }
154310
154311    if( p->usable && (p->iColumn>0 || p->op==SQLITE_INDEX_CONSTRAINT_MATCH) ){
154312      u8 op;
154313      switch( p->op ){
154314        case SQLITE_INDEX_CONSTRAINT_EQ: op = RTREE_EQ; break;
154315        case SQLITE_INDEX_CONSTRAINT_GT: op = RTREE_GT; break;
154316        case SQLITE_INDEX_CONSTRAINT_LE: op = RTREE_LE; break;
154317        case SQLITE_INDEX_CONSTRAINT_LT: op = RTREE_LT; break;
154318        case SQLITE_INDEX_CONSTRAINT_GE: op = RTREE_GE; break;
154319        default:
154320          assert( p->op==SQLITE_INDEX_CONSTRAINT_MATCH );
154321          op = RTREE_MATCH;
154322          break;
154323      }
154324      zIdxStr[iIdx++] = op;
154325      zIdxStr[iIdx++] = p->iColumn - 1 + '0';
154326      pIdxInfo->aConstraintUsage[ii].argvIndex = (iIdx/2);
154327      pIdxInfo->aConstraintUsage[ii].omit = 1;
154328    }
154329  }
154330
154331  pIdxInfo->idxNum = 2;
154332  pIdxInfo->needToFreeIdxStr = 1;
154333  if( iIdx>0 && 0==(pIdxInfo->idxStr = sqlite3_mprintf("%s", zIdxStr)) ){
154334    return SQLITE_NOMEM;
154335  }
154336
154337  nRow = pRtree->nRowEst / (iIdx + 1);
154338  pIdxInfo->estimatedCost = (double)6.0 * (double)nRow;
154339  setEstimatedRows(pIdxInfo, nRow);
154340
154341  return rc;
154342}
154343
154344/*
154345** Return the N-dimensional volumn of the cell stored in *p.
154346*/
154347static RtreeDValue cellArea(Rtree *pRtree, RtreeCell *p){
154348  RtreeDValue area = (RtreeDValue)1;
154349  int ii;
154350  for(ii=0; ii<(pRtree->nDim*2); ii+=2){
154351    area = (area * (DCOORD(p->aCoord[ii+1]) - DCOORD(p->aCoord[ii])));
154352  }
154353  return area;
154354}
154355
154356/*
154357** Return the margin length of cell p. The margin length is the sum
154358** of the objects size in each dimension.
154359*/
154360static RtreeDValue cellMargin(Rtree *pRtree, RtreeCell *p){
154361  RtreeDValue margin = (RtreeDValue)0;
154362  int ii;
154363  for(ii=0; ii<(pRtree->nDim*2); ii+=2){
154364    margin += (DCOORD(p->aCoord[ii+1]) - DCOORD(p->aCoord[ii]));
154365  }
154366  return margin;
154367}
154368
154369/*
154370** Store the union of cells p1 and p2 in p1.
154371*/
154372static void cellUnion(Rtree *pRtree, RtreeCell *p1, RtreeCell *p2){
154373  int ii;
154374  if( pRtree->eCoordType==RTREE_COORD_REAL32 ){
154375    for(ii=0; ii<(pRtree->nDim*2); ii+=2){
154376      p1->aCoord[ii].f = MIN(p1->aCoord[ii].f, p2->aCoord[ii].f);
154377      p1->aCoord[ii+1].f = MAX(p1->aCoord[ii+1].f, p2->aCoord[ii+1].f);
154378    }
154379  }else{
154380    for(ii=0; ii<(pRtree->nDim*2); ii+=2){
154381      p1->aCoord[ii].i = MIN(p1->aCoord[ii].i, p2->aCoord[ii].i);
154382      p1->aCoord[ii+1].i = MAX(p1->aCoord[ii+1].i, p2->aCoord[ii+1].i);
154383    }
154384  }
154385}
154386
154387/*
154388** Return true if the area covered by p2 is a subset of the area covered
154389** by p1. False otherwise.
154390*/
154391static int cellContains(Rtree *pRtree, RtreeCell *p1, RtreeCell *p2){
154392  int ii;
154393  int isInt = (pRtree->eCoordType==RTREE_COORD_INT32);
154394  for(ii=0; ii<(pRtree->nDim*2); ii+=2){
154395    RtreeCoord *a1 = &p1->aCoord[ii];
154396    RtreeCoord *a2 = &p2->aCoord[ii];
154397    if( (!isInt && (a2[0].f<a1[0].f || a2[1].f>a1[1].f))
154398     || ( isInt && (a2[0].i<a1[0].i || a2[1].i>a1[1].i))
154399    ){
154400      return 0;
154401    }
154402  }
154403  return 1;
154404}
154405
154406/*
154407** Return the amount cell p would grow by if it were unioned with pCell.
154408*/
154409static RtreeDValue cellGrowth(Rtree *pRtree, RtreeCell *p, RtreeCell *pCell){
154410  RtreeDValue area;
154411  RtreeCell cell;
154412  memcpy(&cell, p, sizeof(RtreeCell));
154413  area = cellArea(pRtree, &cell);
154414  cellUnion(pRtree, &cell, pCell);
154415  return (cellArea(pRtree, &cell)-area);
154416}
154417
154418static RtreeDValue cellOverlap(
154419  Rtree *pRtree,
154420  RtreeCell *p,
154421  RtreeCell *aCell,
154422  int nCell
154423){
154424  int ii;
154425  RtreeDValue overlap = RTREE_ZERO;
154426  for(ii=0; ii<nCell; ii++){
154427    int jj;
154428    RtreeDValue o = (RtreeDValue)1;
154429    for(jj=0; jj<(pRtree->nDim*2); jj+=2){
154430      RtreeDValue x1, x2;
154431      x1 = MAX(DCOORD(p->aCoord[jj]), DCOORD(aCell[ii].aCoord[jj]));
154432      x2 = MIN(DCOORD(p->aCoord[jj+1]), DCOORD(aCell[ii].aCoord[jj+1]));
154433      if( x2<x1 ){
154434        o = (RtreeDValue)0;
154435        break;
154436      }else{
154437        o = o * (x2-x1);
154438      }
154439    }
154440    overlap += o;
154441  }
154442  return overlap;
154443}
154444
154445
154446/*
154447** This function implements the ChooseLeaf algorithm from Gutman[84].
154448** ChooseSubTree in r*tree terminology.
154449*/
154450static int ChooseLeaf(
154451  Rtree *pRtree,               /* Rtree table */
154452  RtreeCell *pCell,            /* Cell to insert into rtree */
154453  int iHeight,                 /* Height of sub-tree rooted at pCell */
154454  RtreeNode **ppLeaf           /* OUT: Selected leaf page */
154455){
154456  int rc;
154457  int ii;
154458  RtreeNode *pNode;
154459  rc = nodeAcquire(pRtree, 1, 0, &pNode);
154460
154461  for(ii=0; rc==SQLITE_OK && ii<(pRtree->iDepth-iHeight); ii++){
154462    int iCell;
154463    sqlite3_int64 iBest = 0;
154464
154465    RtreeDValue fMinGrowth = RTREE_ZERO;
154466    RtreeDValue fMinArea = RTREE_ZERO;
154467
154468    int nCell = NCELL(pNode);
154469    RtreeCell cell;
154470    RtreeNode *pChild;
154471
154472    RtreeCell *aCell = 0;
154473
154474    /* Select the child node which will be enlarged the least if pCell
154475    ** is inserted into it. Resolve ties by choosing the entry with
154476    ** the smallest area.
154477    */
154478    for(iCell=0; iCell<nCell; iCell++){
154479      int bBest = 0;
154480      RtreeDValue growth;
154481      RtreeDValue area;
154482      nodeGetCell(pRtree, pNode, iCell, &cell);
154483      growth = cellGrowth(pRtree, &cell, pCell);
154484      area = cellArea(pRtree, &cell);
154485      if( iCell==0||growth<fMinGrowth||(growth==fMinGrowth && area<fMinArea) ){
154486        bBest = 1;
154487      }
154488      if( bBest ){
154489        fMinGrowth = growth;
154490        fMinArea = area;
154491        iBest = cell.iRowid;
154492      }
154493    }
154494
154495    sqlite3_free(aCell);
154496    rc = nodeAcquire(pRtree, iBest, pNode, &pChild);
154497    nodeRelease(pRtree, pNode);
154498    pNode = pChild;
154499  }
154500
154501  *ppLeaf = pNode;
154502  return rc;
154503}
154504
154505/*
154506** A cell with the same content as pCell has just been inserted into
154507** the node pNode. This function updates the bounding box cells in
154508** all ancestor elements.
154509*/
154510static int AdjustTree(
154511  Rtree *pRtree,                    /* Rtree table */
154512  RtreeNode *pNode,                 /* Adjust ancestry of this node. */
154513  RtreeCell *pCell                  /* This cell was just inserted */
154514){
154515  RtreeNode *p = pNode;
154516  while( p->pParent ){
154517    RtreeNode *pParent = p->pParent;
154518    RtreeCell cell;
154519    int iCell;
154520
154521    if( nodeParentIndex(pRtree, p, &iCell) ){
154522      return SQLITE_CORRUPT_VTAB;
154523    }
154524
154525    nodeGetCell(pRtree, pParent, iCell, &cell);
154526    if( !cellContains(pRtree, &cell, pCell) ){
154527      cellUnion(pRtree, &cell, pCell);
154528      nodeOverwriteCell(pRtree, pParent, &cell, iCell);
154529    }
154530
154531    p = pParent;
154532  }
154533  return SQLITE_OK;
154534}
154535
154536/*
154537** Write mapping (iRowid->iNode) to the <rtree>_rowid table.
154538*/
154539static int rowidWrite(Rtree *pRtree, sqlite3_int64 iRowid, sqlite3_int64 iNode){
154540  sqlite3_bind_int64(pRtree->pWriteRowid, 1, iRowid);
154541  sqlite3_bind_int64(pRtree->pWriteRowid, 2, iNode);
154542  sqlite3_step(pRtree->pWriteRowid);
154543  return sqlite3_reset(pRtree->pWriteRowid);
154544}
154545
154546/*
154547** Write mapping (iNode->iPar) to the <rtree>_parent table.
154548*/
154549static int parentWrite(Rtree *pRtree, sqlite3_int64 iNode, sqlite3_int64 iPar){
154550  sqlite3_bind_int64(pRtree->pWriteParent, 1, iNode);
154551  sqlite3_bind_int64(pRtree->pWriteParent, 2, iPar);
154552  sqlite3_step(pRtree->pWriteParent);
154553  return sqlite3_reset(pRtree->pWriteParent);
154554}
154555
154556static int rtreeInsertCell(Rtree *, RtreeNode *, RtreeCell *, int);
154557
154558
154559/*
154560** Arguments aIdx, aDistance and aSpare all point to arrays of size
154561** nIdx. The aIdx array contains the set of integers from 0 to
154562** (nIdx-1) in no particular order. This function sorts the values
154563** in aIdx according to the indexed values in aDistance. For
154564** example, assuming the inputs:
154565**
154566**   aIdx      = { 0,   1,   2,   3 }
154567**   aDistance = { 5.0, 2.0, 7.0, 6.0 }
154568**
154569** this function sets the aIdx array to contain:
154570**
154571**   aIdx      = { 0,   1,   2,   3 }
154572**
154573** The aSpare array is used as temporary working space by the
154574** sorting algorithm.
154575*/
154576static void SortByDistance(
154577  int *aIdx,
154578  int nIdx,
154579  RtreeDValue *aDistance,
154580  int *aSpare
154581){
154582  if( nIdx>1 ){
154583    int iLeft = 0;
154584    int iRight = 0;
154585
154586    int nLeft = nIdx/2;
154587    int nRight = nIdx-nLeft;
154588    int *aLeft = aIdx;
154589    int *aRight = &aIdx[nLeft];
154590
154591    SortByDistance(aLeft, nLeft, aDistance, aSpare);
154592    SortByDistance(aRight, nRight, aDistance, aSpare);
154593
154594    memcpy(aSpare, aLeft, sizeof(int)*nLeft);
154595    aLeft = aSpare;
154596
154597    while( iLeft<nLeft || iRight<nRight ){
154598      if( iLeft==nLeft ){
154599        aIdx[iLeft+iRight] = aRight[iRight];
154600        iRight++;
154601      }else if( iRight==nRight ){
154602        aIdx[iLeft+iRight] = aLeft[iLeft];
154603        iLeft++;
154604      }else{
154605        RtreeDValue fLeft = aDistance[aLeft[iLeft]];
154606        RtreeDValue fRight = aDistance[aRight[iRight]];
154607        if( fLeft<fRight ){
154608          aIdx[iLeft+iRight] = aLeft[iLeft];
154609          iLeft++;
154610        }else{
154611          aIdx[iLeft+iRight] = aRight[iRight];
154612          iRight++;
154613        }
154614      }
154615    }
154616
154617#if 0
154618    /* Check that the sort worked */
154619    {
154620      int jj;
154621      for(jj=1; jj<nIdx; jj++){
154622        RtreeDValue left = aDistance[aIdx[jj-1]];
154623        RtreeDValue right = aDistance[aIdx[jj]];
154624        assert( left<=right );
154625      }
154626    }
154627#endif
154628  }
154629}
154630
154631/*
154632** Arguments aIdx, aCell and aSpare all point to arrays of size
154633** nIdx. The aIdx array contains the set of integers from 0 to
154634** (nIdx-1) in no particular order. This function sorts the values
154635** in aIdx according to dimension iDim of the cells in aCell. The
154636** minimum value of dimension iDim is considered first, the
154637** maximum used to break ties.
154638**
154639** The aSpare array is used as temporary working space by the
154640** sorting algorithm.
154641*/
154642static void SortByDimension(
154643  Rtree *pRtree,
154644  int *aIdx,
154645  int nIdx,
154646  int iDim,
154647  RtreeCell *aCell,
154648  int *aSpare
154649){
154650  if( nIdx>1 ){
154651
154652    int iLeft = 0;
154653    int iRight = 0;
154654
154655    int nLeft = nIdx/2;
154656    int nRight = nIdx-nLeft;
154657    int *aLeft = aIdx;
154658    int *aRight = &aIdx[nLeft];
154659
154660    SortByDimension(pRtree, aLeft, nLeft, iDim, aCell, aSpare);
154661    SortByDimension(pRtree, aRight, nRight, iDim, aCell, aSpare);
154662
154663    memcpy(aSpare, aLeft, sizeof(int)*nLeft);
154664    aLeft = aSpare;
154665    while( iLeft<nLeft || iRight<nRight ){
154666      RtreeDValue xleft1 = DCOORD(aCell[aLeft[iLeft]].aCoord[iDim*2]);
154667      RtreeDValue xleft2 = DCOORD(aCell[aLeft[iLeft]].aCoord[iDim*2+1]);
154668      RtreeDValue xright1 = DCOORD(aCell[aRight[iRight]].aCoord[iDim*2]);
154669      RtreeDValue xright2 = DCOORD(aCell[aRight[iRight]].aCoord[iDim*2+1]);
154670      if( (iLeft!=nLeft) && ((iRight==nRight)
154671       || (xleft1<xright1)
154672       || (xleft1==xright1 && xleft2<xright2)
154673      )){
154674        aIdx[iLeft+iRight] = aLeft[iLeft];
154675        iLeft++;
154676      }else{
154677        aIdx[iLeft+iRight] = aRight[iRight];
154678        iRight++;
154679      }
154680    }
154681
154682#if 0
154683    /* Check that the sort worked */
154684    {
154685      int jj;
154686      for(jj=1; jj<nIdx; jj++){
154687        RtreeDValue xleft1 = aCell[aIdx[jj-1]].aCoord[iDim*2];
154688        RtreeDValue xleft2 = aCell[aIdx[jj-1]].aCoord[iDim*2+1];
154689        RtreeDValue xright1 = aCell[aIdx[jj]].aCoord[iDim*2];
154690        RtreeDValue xright2 = aCell[aIdx[jj]].aCoord[iDim*2+1];
154691        assert( xleft1<=xright1 && (xleft1<xright1 || xleft2<=xright2) );
154692      }
154693    }
154694#endif
154695  }
154696}
154697
154698/*
154699** Implementation of the R*-tree variant of SplitNode from Beckman[1990].
154700*/
154701static int splitNodeStartree(
154702  Rtree *pRtree,
154703  RtreeCell *aCell,
154704  int nCell,
154705  RtreeNode *pLeft,
154706  RtreeNode *pRight,
154707  RtreeCell *pBboxLeft,
154708  RtreeCell *pBboxRight
154709){
154710  int **aaSorted;
154711  int *aSpare;
154712  int ii;
154713
154714  int iBestDim = 0;
154715  int iBestSplit = 0;
154716  RtreeDValue fBestMargin = RTREE_ZERO;
154717
154718  int nByte = (pRtree->nDim+1)*(sizeof(int*)+nCell*sizeof(int));
154719
154720  aaSorted = (int **)sqlite3_malloc(nByte);
154721  if( !aaSorted ){
154722    return SQLITE_NOMEM;
154723  }
154724
154725  aSpare = &((int *)&aaSorted[pRtree->nDim])[pRtree->nDim*nCell];
154726  memset(aaSorted, 0, nByte);
154727  for(ii=0; ii<pRtree->nDim; ii++){
154728    int jj;
154729    aaSorted[ii] = &((int *)&aaSorted[pRtree->nDim])[ii*nCell];
154730    for(jj=0; jj<nCell; jj++){
154731      aaSorted[ii][jj] = jj;
154732    }
154733    SortByDimension(pRtree, aaSorted[ii], nCell, ii, aCell, aSpare);
154734  }
154735
154736  for(ii=0; ii<pRtree->nDim; ii++){
154737    RtreeDValue margin = RTREE_ZERO;
154738    RtreeDValue fBestOverlap = RTREE_ZERO;
154739    RtreeDValue fBestArea = RTREE_ZERO;
154740    int iBestLeft = 0;
154741    int nLeft;
154742
154743    for(
154744      nLeft=RTREE_MINCELLS(pRtree);
154745      nLeft<=(nCell-RTREE_MINCELLS(pRtree));
154746      nLeft++
154747    ){
154748      RtreeCell left;
154749      RtreeCell right;
154750      int kk;
154751      RtreeDValue overlap;
154752      RtreeDValue area;
154753
154754      memcpy(&left, &aCell[aaSorted[ii][0]], sizeof(RtreeCell));
154755      memcpy(&right, &aCell[aaSorted[ii][nCell-1]], sizeof(RtreeCell));
154756      for(kk=1; kk<(nCell-1); kk++){
154757        if( kk<nLeft ){
154758          cellUnion(pRtree, &left, &aCell[aaSorted[ii][kk]]);
154759        }else{
154760          cellUnion(pRtree, &right, &aCell[aaSorted[ii][kk]]);
154761        }
154762      }
154763      margin += cellMargin(pRtree, &left);
154764      margin += cellMargin(pRtree, &right);
154765      overlap = cellOverlap(pRtree, &left, &right, 1);
154766      area = cellArea(pRtree, &left) + cellArea(pRtree, &right);
154767      if( (nLeft==RTREE_MINCELLS(pRtree))
154768       || (overlap<fBestOverlap)
154769       || (overlap==fBestOverlap && area<fBestArea)
154770      ){
154771        iBestLeft = nLeft;
154772        fBestOverlap = overlap;
154773        fBestArea = area;
154774      }
154775    }
154776
154777    if( ii==0 || margin<fBestMargin ){
154778      iBestDim = ii;
154779      fBestMargin = margin;
154780      iBestSplit = iBestLeft;
154781    }
154782  }
154783
154784  memcpy(pBboxLeft, &aCell[aaSorted[iBestDim][0]], sizeof(RtreeCell));
154785  memcpy(pBboxRight, &aCell[aaSorted[iBestDim][iBestSplit]], sizeof(RtreeCell));
154786  for(ii=0; ii<nCell; ii++){
154787    RtreeNode *pTarget = (ii<iBestSplit)?pLeft:pRight;
154788    RtreeCell *pBbox = (ii<iBestSplit)?pBboxLeft:pBboxRight;
154789    RtreeCell *pCell = &aCell[aaSorted[iBestDim][ii]];
154790    nodeInsertCell(pRtree, pTarget, pCell);
154791    cellUnion(pRtree, pBbox, pCell);
154792  }
154793
154794  sqlite3_free(aaSorted);
154795  return SQLITE_OK;
154796}
154797
154798
154799static int updateMapping(
154800  Rtree *pRtree,
154801  i64 iRowid,
154802  RtreeNode *pNode,
154803  int iHeight
154804){
154805  int (*xSetMapping)(Rtree *, sqlite3_int64, sqlite3_int64);
154806  xSetMapping = ((iHeight==0)?rowidWrite:parentWrite);
154807  if( iHeight>0 ){
154808    RtreeNode *pChild = nodeHashLookup(pRtree, iRowid);
154809    if( pChild ){
154810      nodeRelease(pRtree, pChild->pParent);
154811      nodeReference(pNode);
154812      pChild->pParent = pNode;
154813    }
154814  }
154815  return xSetMapping(pRtree, iRowid, pNode->iNode);
154816}
154817
154818static int SplitNode(
154819  Rtree *pRtree,
154820  RtreeNode *pNode,
154821  RtreeCell *pCell,
154822  int iHeight
154823){
154824  int i;
154825  int newCellIsRight = 0;
154826
154827  int rc = SQLITE_OK;
154828  int nCell = NCELL(pNode);
154829  RtreeCell *aCell;
154830  int *aiUsed;
154831
154832  RtreeNode *pLeft = 0;
154833  RtreeNode *pRight = 0;
154834
154835  RtreeCell leftbbox;
154836  RtreeCell rightbbox;
154837
154838  /* Allocate an array and populate it with a copy of pCell and
154839  ** all cells from node pLeft. Then zero the original node.
154840  */
154841  aCell = sqlite3_malloc((sizeof(RtreeCell)+sizeof(int))*(nCell+1));
154842  if( !aCell ){
154843    rc = SQLITE_NOMEM;
154844    goto splitnode_out;
154845  }
154846  aiUsed = (int *)&aCell[nCell+1];
154847  memset(aiUsed, 0, sizeof(int)*(nCell+1));
154848  for(i=0; i<nCell; i++){
154849    nodeGetCell(pRtree, pNode, i, &aCell[i]);
154850  }
154851  nodeZero(pRtree, pNode);
154852  memcpy(&aCell[nCell], pCell, sizeof(RtreeCell));
154853  nCell++;
154854
154855  if( pNode->iNode==1 ){
154856    pRight = nodeNew(pRtree, pNode);
154857    pLeft = nodeNew(pRtree, pNode);
154858    pRtree->iDepth++;
154859    pNode->isDirty = 1;
154860    writeInt16(pNode->zData, pRtree->iDepth);
154861  }else{
154862    pLeft = pNode;
154863    pRight = nodeNew(pRtree, pLeft->pParent);
154864    nodeReference(pLeft);
154865  }
154866
154867  if( !pLeft || !pRight ){
154868    rc = SQLITE_NOMEM;
154869    goto splitnode_out;
154870  }
154871
154872  memset(pLeft->zData, 0, pRtree->iNodeSize);
154873  memset(pRight->zData, 0, pRtree->iNodeSize);
154874
154875  rc = splitNodeStartree(pRtree, aCell, nCell, pLeft, pRight,
154876                         &leftbbox, &rightbbox);
154877  if( rc!=SQLITE_OK ){
154878    goto splitnode_out;
154879  }
154880
154881  /* Ensure both child nodes have node numbers assigned to them by calling
154882  ** nodeWrite(). Node pRight always needs a node number, as it was created
154883  ** by nodeNew() above. But node pLeft sometimes already has a node number.
154884  ** In this case avoid the all to nodeWrite().
154885  */
154886  if( SQLITE_OK!=(rc = nodeWrite(pRtree, pRight))
154887   || (0==pLeft->iNode && SQLITE_OK!=(rc = nodeWrite(pRtree, pLeft)))
154888  ){
154889    goto splitnode_out;
154890  }
154891
154892  rightbbox.iRowid = pRight->iNode;
154893  leftbbox.iRowid = pLeft->iNode;
154894
154895  if( pNode->iNode==1 ){
154896    rc = rtreeInsertCell(pRtree, pLeft->pParent, &leftbbox, iHeight+1);
154897    if( rc!=SQLITE_OK ){
154898      goto splitnode_out;
154899    }
154900  }else{
154901    RtreeNode *pParent = pLeft->pParent;
154902    int iCell;
154903    rc = nodeParentIndex(pRtree, pLeft, &iCell);
154904    if( rc==SQLITE_OK ){
154905      nodeOverwriteCell(pRtree, pParent, &leftbbox, iCell);
154906      rc = AdjustTree(pRtree, pParent, &leftbbox);
154907    }
154908    if( rc!=SQLITE_OK ){
154909      goto splitnode_out;
154910    }
154911  }
154912  if( (rc = rtreeInsertCell(pRtree, pRight->pParent, &rightbbox, iHeight+1)) ){
154913    goto splitnode_out;
154914  }
154915
154916  for(i=0; i<NCELL(pRight); i++){
154917    i64 iRowid = nodeGetRowid(pRtree, pRight, i);
154918    rc = updateMapping(pRtree, iRowid, pRight, iHeight);
154919    if( iRowid==pCell->iRowid ){
154920      newCellIsRight = 1;
154921    }
154922    if( rc!=SQLITE_OK ){
154923      goto splitnode_out;
154924    }
154925  }
154926  if( pNode->iNode==1 ){
154927    for(i=0; i<NCELL(pLeft); i++){
154928      i64 iRowid = nodeGetRowid(pRtree, pLeft, i);
154929      rc = updateMapping(pRtree, iRowid, pLeft, iHeight);
154930      if( rc!=SQLITE_OK ){
154931        goto splitnode_out;
154932      }
154933    }
154934  }else if( newCellIsRight==0 ){
154935    rc = updateMapping(pRtree, pCell->iRowid, pLeft, iHeight);
154936  }
154937
154938  if( rc==SQLITE_OK ){
154939    rc = nodeRelease(pRtree, pRight);
154940    pRight = 0;
154941  }
154942  if( rc==SQLITE_OK ){
154943    rc = nodeRelease(pRtree, pLeft);
154944    pLeft = 0;
154945  }
154946
154947splitnode_out:
154948  nodeRelease(pRtree, pRight);
154949  nodeRelease(pRtree, pLeft);
154950  sqlite3_free(aCell);
154951  return rc;
154952}
154953
154954/*
154955** If node pLeaf is not the root of the r-tree and its pParent pointer is
154956** still NULL, load all ancestor nodes of pLeaf into memory and populate
154957** the pLeaf->pParent chain all the way up to the root node.
154958**
154959** This operation is required when a row is deleted (or updated - an update
154960** is implemented as a delete followed by an insert). SQLite provides the
154961** rowid of the row to delete, which can be used to find the leaf on which
154962** the entry resides (argument pLeaf). Once the leaf is located, this
154963** function is called to determine its ancestry.
154964*/
154965static int fixLeafParent(Rtree *pRtree, RtreeNode *pLeaf){
154966  int rc = SQLITE_OK;
154967  RtreeNode *pChild = pLeaf;
154968  while( rc==SQLITE_OK && pChild->iNode!=1 && pChild->pParent==0 ){
154969    int rc2 = SQLITE_OK;          /* sqlite3_reset() return code */
154970    sqlite3_bind_int64(pRtree->pReadParent, 1, pChild->iNode);
154971    rc = sqlite3_step(pRtree->pReadParent);
154972    if( rc==SQLITE_ROW ){
154973      RtreeNode *pTest;           /* Used to test for reference loops */
154974      i64 iNode;                  /* Node number of parent node */
154975
154976      /* Before setting pChild->pParent, test that we are not creating a
154977      ** loop of references (as we would if, say, pChild==pParent). We don't
154978      ** want to do this as it leads to a memory leak when trying to delete
154979      ** the referenced counted node structures.
154980      */
154981      iNode = sqlite3_column_int64(pRtree->pReadParent, 0);
154982      for(pTest=pLeaf; pTest && pTest->iNode!=iNode; pTest=pTest->pParent);
154983      if( !pTest ){
154984        rc2 = nodeAcquire(pRtree, iNode, 0, &pChild->pParent);
154985      }
154986    }
154987    rc = sqlite3_reset(pRtree->pReadParent);
154988    if( rc==SQLITE_OK ) rc = rc2;
154989    if( rc==SQLITE_OK && !pChild->pParent ) rc = SQLITE_CORRUPT_VTAB;
154990    pChild = pChild->pParent;
154991  }
154992  return rc;
154993}
154994
154995static int deleteCell(Rtree *, RtreeNode *, int, int);
154996
154997static int removeNode(Rtree *pRtree, RtreeNode *pNode, int iHeight){
154998  int rc;
154999  int rc2;
155000  RtreeNode *pParent = 0;
155001  int iCell;
155002
155003  assert( pNode->nRef==1 );
155004
155005  /* Remove the entry in the parent cell. */
155006  rc = nodeParentIndex(pRtree, pNode, &iCell);
155007  if( rc==SQLITE_OK ){
155008    pParent = pNode->pParent;
155009    pNode->pParent = 0;
155010    rc = deleteCell(pRtree, pParent, iCell, iHeight+1);
155011  }
155012  rc2 = nodeRelease(pRtree, pParent);
155013  if( rc==SQLITE_OK ){
155014    rc = rc2;
155015  }
155016  if( rc!=SQLITE_OK ){
155017    return rc;
155018  }
155019
155020  /* Remove the xxx_node entry. */
155021  sqlite3_bind_int64(pRtree->pDeleteNode, 1, pNode->iNode);
155022  sqlite3_step(pRtree->pDeleteNode);
155023  if( SQLITE_OK!=(rc = sqlite3_reset(pRtree->pDeleteNode)) ){
155024    return rc;
155025  }
155026
155027  /* Remove the xxx_parent entry. */
155028  sqlite3_bind_int64(pRtree->pDeleteParent, 1, pNode->iNode);
155029  sqlite3_step(pRtree->pDeleteParent);
155030  if( SQLITE_OK!=(rc = sqlite3_reset(pRtree->pDeleteParent)) ){
155031    return rc;
155032  }
155033
155034  /* Remove the node from the in-memory hash table and link it into
155035  ** the Rtree.pDeleted list. Its contents will be re-inserted later on.
155036  */
155037  nodeHashDelete(pRtree, pNode);
155038  pNode->iNode = iHeight;
155039  pNode->pNext = pRtree->pDeleted;
155040  pNode->nRef++;
155041  pRtree->pDeleted = pNode;
155042
155043  return SQLITE_OK;
155044}
155045
155046static int fixBoundingBox(Rtree *pRtree, RtreeNode *pNode){
155047  RtreeNode *pParent = pNode->pParent;
155048  int rc = SQLITE_OK;
155049  if( pParent ){
155050    int ii;
155051    int nCell = NCELL(pNode);
155052    RtreeCell box;                            /* Bounding box for pNode */
155053    nodeGetCell(pRtree, pNode, 0, &box);
155054    for(ii=1; ii<nCell; ii++){
155055      RtreeCell cell;
155056      nodeGetCell(pRtree, pNode, ii, &cell);
155057      cellUnion(pRtree, &box, &cell);
155058    }
155059    box.iRowid = pNode->iNode;
155060    rc = nodeParentIndex(pRtree, pNode, &ii);
155061    if( rc==SQLITE_OK ){
155062      nodeOverwriteCell(pRtree, pParent, &box, ii);
155063      rc = fixBoundingBox(pRtree, pParent);
155064    }
155065  }
155066  return rc;
155067}
155068
155069/*
155070** Delete the cell at index iCell of node pNode. After removing the
155071** cell, adjust the r-tree data structure if required.
155072*/
155073static int deleteCell(Rtree *pRtree, RtreeNode *pNode, int iCell, int iHeight){
155074  RtreeNode *pParent;
155075  int rc;
155076
155077  if( SQLITE_OK!=(rc = fixLeafParent(pRtree, pNode)) ){
155078    return rc;
155079  }
155080
155081  /* Remove the cell from the node. This call just moves bytes around
155082  ** the in-memory node image, so it cannot fail.
155083  */
155084  nodeDeleteCell(pRtree, pNode, iCell);
155085
155086  /* If the node is not the tree root and now has less than the minimum
155087  ** number of cells, remove it from the tree. Otherwise, update the
155088  ** cell in the parent node so that it tightly contains the updated
155089  ** node.
155090  */
155091  pParent = pNode->pParent;
155092  assert( pParent || pNode->iNode==1 );
155093  if( pParent ){
155094    if( NCELL(pNode)<RTREE_MINCELLS(pRtree) ){
155095      rc = removeNode(pRtree, pNode, iHeight);
155096    }else{
155097      rc = fixBoundingBox(pRtree, pNode);
155098    }
155099  }
155100
155101  return rc;
155102}
155103
155104static int Reinsert(
155105  Rtree *pRtree,
155106  RtreeNode *pNode,
155107  RtreeCell *pCell,
155108  int iHeight
155109){
155110  int *aOrder;
155111  int *aSpare;
155112  RtreeCell *aCell;
155113  RtreeDValue *aDistance;
155114  int nCell;
155115  RtreeDValue aCenterCoord[RTREE_MAX_DIMENSIONS];
155116  int iDim;
155117  int ii;
155118  int rc = SQLITE_OK;
155119  int n;
155120
155121  memset(aCenterCoord, 0, sizeof(RtreeDValue)*RTREE_MAX_DIMENSIONS);
155122
155123  nCell = NCELL(pNode)+1;
155124  n = (nCell+1)&(~1);
155125
155126  /* Allocate the buffers used by this operation. The allocation is
155127  ** relinquished before this function returns.
155128  */
155129  aCell = (RtreeCell *)sqlite3_malloc(n * (
155130    sizeof(RtreeCell)     +         /* aCell array */
155131    sizeof(int)           +         /* aOrder array */
155132    sizeof(int)           +         /* aSpare array */
155133    sizeof(RtreeDValue)             /* aDistance array */
155134  ));
155135  if( !aCell ){
155136    return SQLITE_NOMEM;
155137  }
155138  aOrder    = (int *)&aCell[n];
155139  aSpare    = (int *)&aOrder[n];
155140  aDistance = (RtreeDValue *)&aSpare[n];
155141
155142  for(ii=0; ii<nCell; ii++){
155143    if( ii==(nCell-1) ){
155144      memcpy(&aCell[ii], pCell, sizeof(RtreeCell));
155145    }else{
155146      nodeGetCell(pRtree, pNode, ii, &aCell[ii]);
155147    }
155148    aOrder[ii] = ii;
155149    for(iDim=0; iDim<pRtree->nDim; iDim++){
155150      aCenterCoord[iDim] += DCOORD(aCell[ii].aCoord[iDim*2]);
155151      aCenterCoord[iDim] += DCOORD(aCell[ii].aCoord[iDim*2+1]);
155152    }
155153  }
155154  for(iDim=0; iDim<pRtree->nDim; iDim++){
155155    aCenterCoord[iDim] = (aCenterCoord[iDim]/(nCell*(RtreeDValue)2));
155156  }
155157
155158  for(ii=0; ii<nCell; ii++){
155159    aDistance[ii] = RTREE_ZERO;
155160    for(iDim=0; iDim<pRtree->nDim; iDim++){
155161      RtreeDValue coord = (DCOORD(aCell[ii].aCoord[iDim*2+1]) -
155162                               DCOORD(aCell[ii].aCoord[iDim*2]));
155163      aDistance[ii] += (coord-aCenterCoord[iDim])*(coord-aCenterCoord[iDim]);
155164    }
155165  }
155166
155167  SortByDistance(aOrder, nCell, aDistance, aSpare);
155168  nodeZero(pRtree, pNode);
155169
155170  for(ii=0; rc==SQLITE_OK && ii<(nCell-(RTREE_MINCELLS(pRtree)+1)); ii++){
155171    RtreeCell *p = &aCell[aOrder[ii]];
155172    nodeInsertCell(pRtree, pNode, p);
155173    if( p->iRowid==pCell->iRowid ){
155174      if( iHeight==0 ){
155175        rc = rowidWrite(pRtree, p->iRowid, pNode->iNode);
155176      }else{
155177        rc = parentWrite(pRtree, p->iRowid, pNode->iNode);
155178      }
155179    }
155180  }
155181  if( rc==SQLITE_OK ){
155182    rc = fixBoundingBox(pRtree, pNode);
155183  }
155184  for(; rc==SQLITE_OK && ii<nCell; ii++){
155185    /* Find a node to store this cell in. pNode->iNode currently contains
155186    ** the height of the sub-tree headed by the cell.
155187    */
155188    RtreeNode *pInsert;
155189    RtreeCell *p = &aCell[aOrder[ii]];
155190    rc = ChooseLeaf(pRtree, p, iHeight, &pInsert);
155191    if( rc==SQLITE_OK ){
155192      int rc2;
155193      rc = rtreeInsertCell(pRtree, pInsert, p, iHeight);
155194      rc2 = nodeRelease(pRtree, pInsert);
155195      if( rc==SQLITE_OK ){
155196        rc = rc2;
155197      }
155198    }
155199  }
155200
155201  sqlite3_free(aCell);
155202  return rc;
155203}
155204
155205/*
155206** Insert cell pCell into node pNode. Node pNode is the head of a
155207** subtree iHeight high (leaf nodes have iHeight==0).
155208*/
155209static int rtreeInsertCell(
155210  Rtree *pRtree,
155211  RtreeNode *pNode,
155212  RtreeCell *pCell,
155213  int iHeight
155214){
155215  int rc = SQLITE_OK;
155216  if( iHeight>0 ){
155217    RtreeNode *pChild = nodeHashLookup(pRtree, pCell->iRowid);
155218    if( pChild ){
155219      nodeRelease(pRtree, pChild->pParent);
155220      nodeReference(pNode);
155221      pChild->pParent = pNode;
155222    }
155223  }
155224  if( nodeInsertCell(pRtree, pNode, pCell) ){
155225    if( iHeight<=pRtree->iReinsertHeight || pNode->iNode==1){
155226      rc = SplitNode(pRtree, pNode, pCell, iHeight);
155227    }else{
155228      pRtree->iReinsertHeight = iHeight;
155229      rc = Reinsert(pRtree, pNode, pCell, iHeight);
155230    }
155231  }else{
155232    rc = AdjustTree(pRtree, pNode, pCell);
155233    if( rc==SQLITE_OK ){
155234      if( iHeight==0 ){
155235        rc = rowidWrite(pRtree, pCell->iRowid, pNode->iNode);
155236      }else{
155237        rc = parentWrite(pRtree, pCell->iRowid, pNode->iNode);
155238      }
155239    }
155240  }
155241  return rc;
155242}
155243
155244static int reinsertNodeContent(Rtree *pRtree, RtreeNode *pNode){
155245  int ii;
155246  int rc = SQLITE_OK;
155247  int nCell = NCELL(pNode);
155248
155249  for(ii=0; rc==SQLITE_OK && ii<nCell; ii++){
155250    RtreeNode *pInsert;
155251    RtreeCell cell;
155252    nodeGetCell(pRtree, pNode, ii, &cell);
155253
155254    /* Find a node to store this cell in. pNode->iNode currently contains
155255    ** the height of the sub-tree headed by the cell.
155256    */
155257    rc = ChooseLeaf(pRtree, &cell, (int)pNode->iNode, &pInsert);
155258    if( rc==SQLITE_OK ){
155259      int rc2;
155260      rc = rtreeInsertCell(pRtree, pInsert, &cell, (int)pNode->iNode);
155261      rc2 = nodeRelease(pRtree, pInsert);
155262      if( rc==SQLITE_OK ){
155263        rc = rc2;
155264      }
155265    }
155266  }
155267  return rc;
155268}
155269
155270/*
155271** Select a currently unused rowid for a new r-tree record.
155272*/
155273static int newRowid(Rtree *pRtree, i64 *piRowid){
155274  int rc;
155275  sqlite3_bind_null(pRtree->pWriteRowid, 1);
155276  sqlite3_bind_null(pRtree->pWriteRowid, 2);
155277  sqlite3_step(pRtree->pWriteRowid);
155278  rc = sqlite3_reset(pRtree->pWriteRowid);
155279  *piRowid = sqlite3_last_insert_rowid(pRtree->db);
155280  return rc;
155281}
155282
155283/*
155284** Remove the entry with rowid=iDelete from the r-tree structure.
155285*/
155286static int rtreeDeleteRowid(Rtree *pRtree, sqlite3_int64 iDelete){
155287  int rc;                         /* Return code */
155288  RtreeNode *pLeaf = 0;           /* Leaf node containing record iDelete */
155289  int iCell;                      /* Index of iDelete cell in pLeaf */
155290  RtreeNode *pRoot;               /* Root node of rtree structure */
155291
155292
155293  /* Obtain a reference to the root node to initialize Rtree.iDepth */
155294  rc = nodeAcquire(pRtree, 1, 0, &pRoot);
155295
155296  /* Obtain a reference to the leaf node that contains the entry
155297  ** about to be deleted.
155298  */
155299  if( rc==SQLITE_OK ){
155300    rc = findLeafNode(pRtree, iDelete, &pLeaf, 0);
155301  }
155302
155303  /* Delete the cell in question from the leaf node. */
155304  if( rc==SQLITE_OK ){
155305    int rc2;
155306    rc = nodeRowidIndex(pRtree, pLeaf, iDelete, &iCell);
155307    if( rc==SQLITE_OK ){
155308      rc = deleteCell(pRtree, pLeaf, iCell, 0);
155309    }
155310    rc2 = nodeRelease(pRtree, pLeaf);
155311    if( rc==SQLITE_OK ){
155312      rc = rc2;
155313    }
155314  }
155315
155316  /* Delete the corresponding entry in the <rtree>_rowid table. */
155317  if( rc==SQLITE_OK ){
155318    sqlite3_bind_int64(pRtree->pDeleteRowid, 1, iDelete);
155319    sqlite3_step(pRtree->pDeleteRowid);
155320    rc = sqlite3_reset(pRtree->pDeleteRowid);
155321  }
155322
155323  /* Check if the root node now has exactly one child. If so, remove
155324  ** it, schedule the contents of the child for reinsertion and
155325  ** reduce the tree height by one.
155326  **
155327  ** This is equivalent to copying the contents of the child into
155328  ** the root node (the operation that Gutman's paper says to perform
155329  ** in this scenario).
155330  */
155331  if( rc==SQLITE_OK && pRtree->iDepth>0 && NCELL(pRoot)==1 ){
155332    int rc2;
155333    RtreeNode *pChild;
155334    i64 iChild = nodeGetRowid(pRtree, pRoot, 0);
155335    rc = nodeAcquire(pRtree, iChild, pRoot, &pChild);
155336    if( rc==SQLITE_OK ){
155337      rc = removeNode(pRtree, pChild, pRtree->iDepth-1);
155338    }
155339    rc2 = nodeRelease(pRtree, pChild);
155340    if( rc==SQLITE_OK ) rc = rc2;
155341    if( rc==SQLITE_OK ){
155342      pRtree->iDepth--;
155343      writeInt16(pRoot->zData, pRtree->iDepth);
155344      pRoot->isDirty = 1;
155345    }
155346  }
155347
155348  /* Re-insert the contents of any underfull nodes removed from the tree. */
155349  for(pLeaf=pRtree->pDeleted; pLeaf; pLeaf=pRtree->pDeleted){
155350    if( rc==SQLITE_OK ){
155351      rc = reinsertNodeContent(pRtree, pLeaf);
155352    }
155353    pRtree->pDeleted = pLeaf->pNext;
155354    sqlite3_free(pLeaf);
155355  }
155356
155357  /* Release the reference to the root node. */
155358  if( rc==SQLITE_OK ){
155359    rc = nodeRelease(pRtree, pRoot);
155360  }else{
155361    nodeRelease(pRtree, pRoot);
155362  }
155363
155364  return rc;
155365}
155366
155367/*
155368** Rounding constants for float->double conversion.
155369*/
155370#define RNDTOWARDS  (1.0 - 1.0/8388608.0)  /* Round towards zero */
155371#define RNDAWAY     (1.0 + 1.0/8388608.0)  /* Round away from zero */
155372
155373#if !defined(SQLITE_RTREE_INT_ONLY)
155374/*
155375** Convert an sqlite3_value into an RtreeValue (presumably a float)
155376** while taking care to round toward negative or positive, respectively.
155377*/
155378static RtreeValue rtreeValueDown(sqlite3_value *v){
155379  double d = sqlite3_value_double(v);
155380  float f = (float)d;
155381  if( f>d ){
155382    f = (float)(d*(d<0 ? RNDAWAY : RNDTOWARDS));
155383  }
155384  return f;
155385}
155386static RtreeValue rtreeValueUp(sqlite3_value *v){
155387  double d = sqlite3_value_double(v);
155388  float f = (float)d;
155389  if( f<d ){
155390    f = (float)(d*(d<0 ? RNDTOWARDS : RNDAWAY));
155391  }
155392  return f;
155393}
155394#endif /* !defined(SQLITE_RTREE_INT_ONLY) */
155395
155396
155397/*
155398** The xUpdate method for rtree module virtual tables.
155399*/
155400static int rtreeUpdate(
155401  sqlite3_vtab *pVtab,
155402  int nData,
155403  sqlite3_value **azData,
155404  sqlite_int64 *pRowid
155405){
155406  Rtree *pRtree = (Rtree *)pVtab;
155407  int rc = SQLITE_OK;
155408  RtreeCell cell;                 /* New cell to insert if nData>1 */
155409  int bHaveRowid = 0;             /* Set to 1 after new rowid is determined */
155410
155411  rtreeReference(pRtree);
155412  assert(nData>=1);
155413
155414  cell.iRowid = 0;  /* Used only to suppress a compiler warning */
155415
155416  /* Constraint handling. A write operation on an r-tree table may return
155417  ** SQLITE_CONSTRAINT for two reasons:
155418  **
155419  **   1. A duplicate rowid value, or
155420  **   2. The supplied data violates the "x2>=x1" constraint.
155421  **
155422  ** In the first case, if the conflict-handling mode is REPLACE, then
155423  ** the conflicting row can be removed before proceeding. In the second
155424  ** case, SQLITE_CONSTRAINT must be returned regardless of the
155425  ** conflict-handling mode specified by the user.
155426  */
155427  if( nData>1 ){
155428    int ii;
155429
155430    /* Populate the cell.aCoord[] array. The first coordinate is azData[3].
155431    **
155432    ** NB: nData can only be less than nDim*2+3 if the rtree is mis-declared
155433    ** with "column" that are interpreted as table constraints.
155434    ** Example:  CREATE VIRTUAL TABLE bad USING rtree(x,y,CHECK(y>5));
155435    ** This problem was discovered after years of use, so we silently ignore
155436    ** these kinds of misdeclared tables to avoid breaking any legacy.
155437    */
155438    assert( nData<=(pRtree->nDim*2 + 3) );
155439
155440#ifndef SQLITE_RTREE_INT_ONLY
155441    if( pRtree->eCoordType==RTREE_COORD_REAL32 ){
155442      for(ii=0; ii<nData-4; ii+=2){
155443        cell.aCoord[ii].f = rtreeValueDown(azData[ii+3]);
155444        cell.aCoord[ii+1].f = rtreeValueUp(azData[ii+4]);
155445        if( cell.aCoord[ii].f>cell.aCoord[ii+1].f ){
155446          rc = SQLITE_CONSTRAINT;
155447          goto constraint;
155448        }
155449      }
155450    }else
155451#endif
155452    {
155453      for(ii=0; ii<nData-4; ii+=2){
155454        cell.aCoord[ii].i = sqlite3_value_int(azData[ii+3]);
155455        cell.aCoord[ii+1].i = sqlite3_value_int(azData[ii+4]);
155456        if( cell.aCoord[ii].i>cell.aCoord[ii+1].i ){
155457          rc = SQLITE_CONSTRAINT;
155458          goto constraint;
155459        }
155460      }
155461    }
155462
155463    /* If a rowid value was supplied, check if it is already present in
155464    ** the table. If so, the constraint has failed. */
155465    if( sqlite3_value_type(azData[2])!=SQLITE_NULL ){
155466      cell.iRowid = sqlite3_value_int64(azData[2]);
155467      if( sqlite3_value_type(azData[0])==SQLITE_NULL
155468       || sqlite3_value_int64(azData[0])!=cell.iRowid
155469      ){
155470        int steprc;
155471        sqlite3_bind_int64(pRtree->pReadRowid, 1, cell.iRowid);
155472        steprc = sqlite3_step(pRtree->pReadRowid);
155473        rc = sqlite3_reset(pRtree->pReadRowid);
155474        if( SQLITE_ROW==steprc ){
155475          if( sqlite3_vtab_on_conflict(pRtree->db)==SQLITE_REPLACE ){
155476            rc = rtreeDeleteRowid(pRtree, cell.iRowid);
155477          }else{
155478            rc = SQLITE_CONSTRAINT;
155479            goto constraint;
155480          }
155481        }
155482      }
155483      bHaveRowid = 1;
155484    }
155485  }
155486
155487  /* If azData[0] is not an SQL NULL value, it is the rowid of a
155488  ** record to delete from the r-tree table. The following block does
155489  ** just that.
155490  */
155491  if( sqlite3_value_type(azData[0])!=SQLITE_NULL ){
155492    rc = rtreeDeleteRowid(pRtree, sqlite3_value_int64(azData[0]));
155493  }
155494
155495  /* If the azData[] array contains more than one element, elements
155496  ** (azData[2]..azData[argc-1]) contain a new record to insert into
155497  ** the r-tree structure.
155498  */
155499  if( rc==SQLITE_OK && nData>1 ){
155500    /* Insert the new record into the r-tree */
155501    RtreeNode *pLeaf = 0;
155502
155503    /* Figure out the rowid of the new row. */
155504    if( bHaveRowid==0 ){
155505      rc = newRowid(pRtree, &cell.iRowid);
155506    }
155507    *pRowid = cell.iRowid;
155508
155509    if( rc==SQLITE_OK ){
155510      rc = ChooseLeaf(pRtree, &cell, 0, &pLeaf);
155511    }
155512    if( rc==SQLITE_OK ){
155513      int rc2;
155514      pRtree->iReinsertHeight = -1;
155515      rc = rtreeInsertCell(pRtree, pLeaf, &cell, 0);
155516      rc2 = nodeRelease(pRtree, pLeaf);
155517      if( rc==SQLITE_OK ){
155518        rc = rc2;
155519      }
155520    }
155521  }
155522
155523constraint:
155524  rtreeRelease(pRtree);
155525  return rc;
155526}
155527
155528/*
155529** The xRename method for rtree module virtual tables.
155530*/
155531static int rtreeRename(sqlite3_vtab *pVtab, const char *zNewName){
155532  Rtree *pRtree = (Rtree *)pVtab;
155533  int rc = SQLITE_NOMEM;
155534  char *zSql = sqlite3_mprintf(
155535    "ALTER TABLE %Q.'%q_node'   RENAME TO \"%w_node\";"
155536    "ALTER TABLE %Q.'%q_parent' RENAME TO \"%w_parent\";"
155537    "ALTER TABLE %Q.'%q_rowid'  RENAME TO \"%w_rowid\";"
155538    , pRtree->zDb, pRtree->zName, zNewName
155539    , pRtree->zDb, pRtree->zName, zNewName
155540    , pRtree->zDb, pRtree->zName, zNewName
155541  );
155542  if( zSql ){
155543    rc = sqlite3_exec(pRtree->db, zSql, 0, 0, 0);
155544    sqlite3_free(zSql);
155545  }
155546  return rc;
155547}
155548
155549/*
155550** This function populates the pRtree->nRowEst variable with an estimate
155551** of the number of rows in the virtual table. If possible, this is based
155552** on sqlite_stat1 data. Otherwise, use RTREE_DEFAULT_ROWEST.
155553*/
155554static int rtreeQueryStat1(sqlite3 *db, Rtree *pRtree){
155555  const char *zFmt = "SELECT stat FROM %Q.sqlite_stat1 WHERE tbl = '%q_rowid'";
155556  char *zSql;
155557  sqlite3_stmt *p;
155558  int rc;
155559  i64 nRow = 0;
155560
155561  zSql = sqlite3_mprintf(zFmt, pRtree->zDb, pRtree->zName);
155562  if( zSql==0 ){
155563    rc = SQLITE_NOMEM;
155564  }else{
155565    rc = sqlite3_prepare_v2(db, zSql, -1, &p, 0);
155566    if( rc==SQLITE_OK ){
155567      if( sqlite3_step(p)==SQLITE_ROW ) nRow = sqlite3_column_int64(p, 0);
155568      rc = sqlite3_finalize(p);
155569    }else if( rc!=SQLITE_NOMEM ){
155570      rc = SQLITE_OK;
155571    }
155572
155573    if( rc==SQLITE_OK ){
155574      if( nRow==0 ){
155575        pRtree->nRowEst = RTREE_DEFAULT_ROWEST;
155576      }else{
155577        pRtree->nRowEst = MAX(nRow, RTREE_MIN_ROWEST);
155578      }
155579    }
155580    sqlite3_free(zSql);
155581  }
155582
155583  return rc;
155584}
155585
155586static sqlite3_module rtreeModule = {
155587  0,                          /* iVersion */
155588  rtreeCreate,                /* xCreate - create a table */
155589  rtreeConnect,               /* xConnect - connect to an existing table */
155590  rtreeBestIndex,             /* xBestIndex - Determine search strategy */
155591  rtreeDisconnect,            /* xDisconnect - Disconnect from a table */
155592  rtreeDestroy,               /* xDestroy - Drop a table */
155593  rtreeOpen,                  /* xOpen - open a cursor */
155594  rtreeClose,                 /* xClose - close a cursor */
155595  rtreeFilter,                /* xFilter - configure scan constraints */
155596  rtreeNext,                  /* xNext - advance a cursor */
155597  rtreeEof,                   /* xEof */
155598  rtreeColumn,                /* xColumn - read data */
155599  rtreeRowid,                 /* xRowid - read data */
155600  rtreeUpdate,                /* xUpdate - write data */
155601  0,                          /* xBegin - begin transaction */
155602  0,                          /* xSync - sync transaction */
155603  0,                          /* xCommit - commit transaction */
155604  0,                          /* xRollback - rollback transaction */
155605  0,                          /* xFindFunction - function overloading */
155606  rtreeRename,                /* xRename - rename the table */
155607  0,                          /* xSavepoint */
155608  0,                          /* xRelease */
155609  0                           /* xRollbackTo */
155610};
155611
155612static int rtreeSqlInit(
155613  Rtree *pRtree,
155614  sqlite3 *db,
155615  const char *zDb,
155616  const char *zPrefix,
155617  int isCreate
155618){
155619  int rc = SQLITE_OK;
155620
155621  #define N_STATEMENT 9
155622  static const char *azSql[N_STATEMENT] = {
155623    /* Read and write the xxx_node table */
155624    "SELECT data FROM '%q'.'%q_node' WHERE nodeno = :1",
155625    "INSERT OR REPLACE INTO '%q'.'%q_node' VALUES(:1, :2)",
155626    "DELETE FROM '%q'.'%q_node' WHERE nodeno = :1",
155627
155628    /* Read and write the xxx_rowid table */
155629    "SELECT nodeno FROM '%q'.'%q_rowid' WHERE rowid = :1",
155630    "INSERT OR REPLACE INTO '%q'.'%q_rowid' VALUES(:1, :2)",
155631    "DELETE FROM '%q'.'%q_rowid' WHERE rowid = :1",
155632
155633    /* Read and write the xxx_parent table */
155634    "SELECT parentnode FROM '%q'.'%q_parent' WHERE nodeno = :1",
155635    "INSERT OR REPLACE INTO '%q'.'%q_parent' VALUES(:1, :2)",
155636    "DELETE FROM '%q'.'%q_parent' WHERE nodeno = :1"
155637  };
155638  sqlite3_stmt **appStmt[N_STATEMENT];
155639  int i;
155640
155641  pRtree->db = db;
155642
155643  if( isCreate ){
155644    char *zCreate = sqlite3_mprintf(
155645"CREATE TABLE \"%w\".\"%w_node\"(nodeno INTEGER PRIMARY KEY, data BLOB);"
155646"CREATE TABLE \"%w\".\"%w_rowid\"(rowid INTEGER PRIMARY KEY, nodeno INTEGER);"
155647"CREATE TABLE \"%w\".\"%w_parent\"(nodeno INTEGER PRIMARY KEY,"
155648                                  " parentnode INTEGER);"
155649"INSERT INTO '%q'.'%q_node' VALUES(1, zeroblob(%d))",
155650      zDb, zPrefix, zDb, zPrefix, zDb, zPrefix, zDb, zPrefix, pRtree->iNodeSize
155651    );
155652    if( !zCreate ){
155653      return SQLITE_NOMEM;
155654    }
155655    rc = sqlite3_exec(db, zCreate, 0, 0, 0);
155656    sqlite3_free(zCreate);
155657    if( rc!=SQLITE_OK ){
155658      return rc;
155659    }
155660  }
155661
155662  appStmt[0] = &pRtree->pReadNode;
155663  appStmt[1] = &pRtree->pWriteNode;
155664  appStmt[2] = &pRtree->pDeleteNode;
155665  appStmt[3] = &pRtree->pReadRowid;
155666  appStmt[4] = &pRtree->pWriteRowid;
155667  appStmt[5] = &pRtree->pDeleteRowid;
155668  appStmt[6] = &pRtree->pReadParent;
155669  appStmt[7] = &pRtree->pWriteParent;
155670  appStmt[8] = &pRtree->pDeleteParent;
155671
155672  rc = rtreeQueryStat1(db, pRtree);
155673  for(i=0; i<N_STATEMENT && rc==SQLITE_OK; i++){
155674    char *zSql = sqlite3_mprintf(azSql[i], zDb, zPrefix);
155675    if( zSql ){
155676      rc = sqlite3_prepare_v2(db, zSql, -1, appStmt[i], 0);
155677    }else{
155678      rc = SQLITE_NOMEM;
155679    }
155680    sqlite3_free(zSql);
155681  }
155682
155683  return rc;
155684}
155685
155686/*
155687** The second argument to this function contains the text of an SQL statement
155688** that returns a single integer value. The statement is compiled and executed
155689** using database connection db. If successful, the integer value returned
155690** is written to *piVal and SQLITE_OK returned. Otherwise, an SQLite error
155691** code is returned and the value of *piVal after returning is not defined.
155692*/
155693static int getIntFromStmt(sqlite3 *db, const char *zSql, int *piVal){
155694  int rc = SQLITE_NOMEM;
155695  if( zSql ){
155696    sqlite3_stmt *pStmt = 0;
155697    rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
155698    if( rc==SQLITE_OK ){
155699      if( SQLITE_ROW==sqlite3_step(pStmt) ){
155700        *piVal = sqlite3_column_int(pStmt, 0);
155701      }
155702      rc = sqlite3_finalize(pStmt);
155703    }
155704  }
155705  return rc;
155706}
155707
155708/*
155709** This function is called from within the xConnect() or xCreate() method to
155710** determine the node-size used by the rtree table being created or connected
155711** to. If successful, pRtree->iNodeSize is populated and SQLITE_OK returned.
155712** Otherwise, an SQLite error code is returned.
155713**
155714** If this function is being called as part of an xConnect(), then the rtree
155715** table already exists. In this case the node-size is determined by inspecting
155716** the root node of the tree.
155717**
155718** Otherwise, for an xCreate(), use 64 bytes less than the database page-size.
155719** This ensures that each node is stored on a single database page. If the
155720** database page-size is so large that more than RTREE_MAXCELLS entries
155721** would fit in a single node, use a smaller node-size.
155722*/
155723static int getNodeSize(
155724  sqlite3 *db,                    /* Database handle */
155725  Rtree *pRtree,                  /* Rtree handle */
155726  int isCreate,                   /* True for xCreate, false for xConnect */
155727  char **pzErr                    /* OUT: Error message, if any */
155728){
155729  int rc;
155730  char *zSql;
155731  if( isCreate ){
155732    int iPageSize = 0;
155733    zSql = sqlite3_mprintf("PRAGMA %Q.page_size", pRtree->zDb);
155734    rc = getIntFromStmt(db, zSql, &iPageSize);
155735    if( rc==SQLITE_OK ){
155736      pRtree->iNodeSize = iPageSize-64;
155737      if( (4+pRtree->nBytesPerCell*RTREE_MAXCELLS)<pRtree->iNodeSize ){
155738        pRtree->iNodeSize = 4+pRtree->nBytesPerCell*RTREE_MAXCELLS;
155739      }
155740    }else{
155741      *pzErr = sqlite3_mprintf("%s", sqlite3_errmsg(db));
155742    }
155743  }else{
155744    zSql = sqlite3_mprintf(
155745        "SELECT length(data) FROM '%q'.'%q_node' WHERE nodeno = 1",
155746        pRtree->zDb, pRtree->zName
155747    );
155748    rc = getIntFromStmt(db, zSql, &pRtree->iNodeSize);
155749    if( rc!=SQLITE_OK ){
155750      *pzErr = sqlite3_mprintf("%s", sqlite3_errmsg(db));
155751    }
155752  }
155753
155754  sqlite3_free(zSql);
155755  return rc;
155756}
155757
155758/*
155759** This function is the implementation of both the xConnect and xCreate
155760** methods of the r-tree virtual table.
155761**
155762**   argv[0]   -> module name
155763**   argv[1]   -> database name
155764**   argv[2]   -> table name
155765**   argv[...] -> column names...
155766*/
155767static int rtreeInit(
155768  sqlite3 *db,                        /* Database connection */
155769  void *pAux,                         /* One of the RTREE_COORD_* constants */
155770  int argc, const char *const*argv,   /* Parameters to CREATE TABLE statement */
155771  sqlite3_vtab **ppVtab,              /* OUT: New virtual table */
155772  char **pzErr,                       /* OUT: Error message, if any */
155773  int isCreate                        /* True for xCreate, false for xConnect */
155774){
155775  int rc = SQLITE_OK;
155776  Rtree *pRtree;
155777  int nDb;              /* Length of string argv[1] */
155778  int nName;            /* Length of string argv[2] */
155779  int eCoordType = (pAux ? RTREE_COORD_INT32 : RTREE_COORD_REAL32);
155780
155781  const char *aErrMsg[] = {
155782    0,                                                    /* 0 */
155783    "Wrong number of columns for an rtree table",         /* 1 */
155784    "Too few columns for an rtree table",                 /* 2 */
155785    "Too many columns for an rtree table"                 /* 3 */
155786  };
155787
155788  int iErr = (argc<6) ? 2 : argc>(RTREE_MAX_DIMENSIONS*2+4) ? 3 : argc%2;
155789  if( aErrMsg[iErr] ){
155790    *pzErr = sqlite3_mprintf("%s", aErrMsg[iErr]);
155791    return SQLITE_ERROR;
155792  }
155793
155794  sqlite3_vtab_config(db, SQLITE_VTAB_CONSTRAINT_SUPPORT, 1);
155795
155796  /* Allocate the sqlite3_vtab structure */
155797  nDb = (int)strlen(argv[1]);
155798  nName = (int)strlen(argv[2]);
155799  pRtree = (Rtree *)sqlite3_malloc(sizeof(Rtree)+nDb+nName+2);
155800  if( !pRtree ){
155801    return SQLITE_NOMEM;
155802  }
155803  memset(pRtree, 0, sizeof(Rtree)+nDb+nName+2);
155804  pRtree->nBusy = 1;
155805  pRtree->base.pModule = &rtreeModule;
155806  pRtree->zDb = (char *)&pRtree[1];
155807  pRtree->zName = &pRtree->zDb[nDb+1];
155808  pRtree->nDim = (argc-4)/2;
155809  pRtree->nBytesPerCell = 8 + pRtree->nDim*4*2;
155810  pRtree->eCoordType = eCoordType;
155811  memcpy(pRtree->zDb, argv[1], nDb);
155812  memcpy(pRtree->zName, argv[2], nName);
155813
155814  /* Figure out the node size to use. */
155815  rc = getNodeSize(db, pRtree, isCreate, pzErr);
155816
155817  /* Create/Connect to the underlying relational database schema. If
155818  ** that is successful, call sqlite3_declare_vtab() to configure
155819  ** the r-tree table schema.
155820  */
155821  if( rc==SQLITE_OK ){
155822    if( (rc = rtreeSqlInit(pRtree, db, argv[1], argv[2], isCreate)) ){
155823      *pzErr = sqlite3_mprintf("%s", sqlite3_errmsg(db));
155824    }else{
155825      char *zSql = sqlite3_mprintf("CREATE TABLE x(%s", argv[3]);
155826      char *zTmp;
155827      int ii;
155828      for(ii=4; zSql && ii<argc; ii++){
155829        zTmp = zSql;
155830        zSql = sqlite3_mprintf("%s, %s", zTmp, argv[ii]);
155831        sqlite3_free(zTmp);
155832      }
155833      if( zSql ){
155834        zTmp = zSql;
155835        zSql = sqlite3_mprintf("%s);", zTmp);
155836        sqlite3_free(zTmp);
155837      }
155838      if( !zSql ){
155839        rc = SQLITE_NOMEM;
155840      }else if( SQLITE_OK!=(rc = sqlite3_declare_vtab(db, zSql)) ){
155841        *pzErr = sqlite3_mprintf("%s", sqlite3_errmsg(db));
155842      }
155843      sqlite3_free(zSql);
155844    }
155845  }
155846
155847  if( rc==SQLITE_OK ){
155848    *ppVtab = (sqlite3_vtab *)pRtree;
155849  }else{
155850    assert( *ppVtab==0 );
155851    assert( pRtree->nBusy==1 );
155852    rtreeRelease(pRtree);
155853  }
155854  return rc;
155855}
155856
155857
155858/*
155859** Implementation of a scalar function that decodes r-tree nodes to
155860** human readable strings. This can be used for debugging and analysis.
155861**
155862** The scalar function takes two arguments: (1) the number of dimensions
155863** to the rtree (between 1 and 5, inclusive) and (2) a blob of data containing
155864** an r-tree node.  For a two-dimensional r-tree structure called "rt", to
155865** deserialize all nodes, a statement like:
155866**
155867**   SELECT rtreenode(2, data) FROM rt_node;
155868**
155869** The human readable string takes the form of a Tcl list with one
155870** entry for each cell in the r-tree node. Each entry is itself a
155871** list, containing the 8-byte rowid/pageno followed by the
155872** <num-dimension>*2 coordinates.
155873*/
155874static void rtreenode(sqlite3_context *ctx, int nArg, sqlite3_value **apArg){
155875  char *zText = 0;
155876  RtreeNode node;
155877  Rtree tree;
155878  int ii;
155879
155880  UNUSED_PARAMETER(nArg);
155881  memset(&node, 0, sizeof(RtreeNode));
155882  memset(&tree, 0, sizeof(Rtree));
155883  tree.nDim = sqlite3_value_int(apArg[0]);
155884  tree.nBytesPerCell = 8 + 8 * tree.nDim;
155885  node.zData = (u8 *)sqlite3_value_blob(apArg[1]);
155886
155887  for(ii=0; ii<NCELL(&node); ii++){
155888    char zCell[512];
155889    int nCell = 0;
155890    RtreeCell cell;
155891    int jj;
155892
155893    nodeGetCell(&tree, &node, ii, &cell);
155894    sqlite3_snprintf(512-nCell,&zCell[nCell],"%lld", cell.iRowid);
155895    nCell = (int)strlen(zCell);
155896    for(jj=0; jj<tree.nDim*2; jj++){
155897#ifndef SQLITE_RTREE_INT_ONLY
155898      sqlite3_snprintf(512-nCell,&zCell[nCell], " %g",
155899                       (double)cell.aCoord[jj].f);
155900#else
155901      sqlite3_snprintf(512-nCell,&zCell[nCell], " %d",
155902                       cell.aCoord[jj].i);
155903#endif
155904      nCell = (int)strlen(zCell);
155905    }
155906
155907    if( zText ){
155908      char *zTextNew = sqlite3_mprintf("%s {%s}", zText, zCell);
155909      sqlite3_free(zText);
155910      zText = zTextNew;
155911    }else{
155912      zText = sqlite3_mprintf("{%s}", zCell);
155913    }
155914  }
155915
155916  sqlite3_result_text(ctx, zText, -1, sqlite3_free);
155917}
155918
155919/* This routine implements an SQL function that returns the "depth" parameter
155920** from the front of a blob that is an r-tree node.  For example:
155921**
155922**     SELECT rtreedepth(data) FROM rt_node WHERE nodeno=1;
155923**
155924** The depth value is 0 for all nodes other than the root node, and the root
155925** node always has nodeno=1, so the example above is the primary use for this
155926** routine.  This routine is intended for testing and analysis only.
155927*/
155928static void rtreedepth(sqlite3_context *ctx, int nArg, sqlite3_value **apArg){
155929  UNUSED_PARAMETER(nArg);
155930  if( sqlite3_value_type(apArg[0])!=SQLITE_BLOB
155931   || sqlite3_value_bytes(apArg[0])<2
155932  ){
155933    sqlite3_result_error(ctx, "Invalid argument to rtreedepth()", -1);
155934  }else{
155935    u8 *zBlob = (u8 *)sqlite3_value_blob(apArg[0]);
155936    sqlite3_result_int(ctx, readInt16(zBlob));
155937  }
155938}
155939
155940/*
155941** Register the r-tree module with database handle db. This creates the
155942** virtual table module "rtree" and the debugging/analysis scalar
155943** function "rtreenode".
155944*/
155945SQLITE_PRIVATE int sqlite3RtreeInit(sqlite3 *db){
155946  const int utf8 = SQLITE_UTF8;
155947  int rc;
155948
155949  rc = sqlite3_create_function(db, "rtreenode", 2, utf8, 0, rtreenode, 0, 0);
155950  if( rc==SQLITE_OK ){
155951    rc = sqlite3_create_function(db, "rtreedepth", 1, utf8, 0,rtreedepth, 0, 0);
155952  }
155953  if( rc==SQLITE_OK ){
155954#ifdef SQLITE_RTREE_INT_ONLY
155955    void *c = (void *)RTREE_COORD_INT32;
155956#else
155957    void *c = (void *)RTREE_COORD_REAL32;
155958#endif
155959    rc = sqlite3_create_module_v2(db, "rtree", &rtreeModule, c, 0);
155960  }
155961  if( rc==SQLITE_OK ){
155962    void *c = (void *)RTREE_COORD_INT32;
155963    rc = sqlite3_create_module_v2(db, "rtree_i32", &rtreeModule, c, 0);
155964  }
155965
155966  return rc;
155967}
155968
155969/*
155970** This routine deletes the RtreeGeomCallback object that was attached
155971** one of the SQL functions create by sqlite3_rtree_geometry_callback()
155972** or sqlite3_rtree_query_callback().  In other words, this routine is the
155973** destructor for an RtreeGeomCallback objecct.  This routine is called when
155974** the corresponding SQL function is deleted.
155975*/
155976static void rtreeFreeCallback(void *p){
155977  RtreeGeomCallback *pInfo = (RtreeGeomCallback*)p;
155978  if( pInfo->xDestructor ) pInfo->xDestructor(pInfo->pContext);
155979  sqlite3_free(p);
155980}
155981
155982/*
155983** This routine frees the BLOB that is returned by geomCallback().
155984*/
155985static void rtreeMatchArgFree(void *pArg){
155986  int i;
155987  RtreeMatchArg *p = (RtreeMatchArg*)pArg;
155988  for(i=0; i<p->nParam; i++){
155989    sqlite3_value_free(p->apSqlParam[i]);
155990  }
155991  sqlite3_free(p);
155992}
155993
155994/*
155995** Each call to sqlite3_rtree_geometry_callback() or
155996** sqlite3_rtree_query_callback() creates an ordinary SQLite
155997** scalar function that is implemented by this routine.
155998**
155999** All this function does is construct an RtreeMatchArg object that
156000** contains the geometry-checking callback routines and a list of
156001** parameters to this function, then return that RtreeMatchArg object
156002** as a BLOB.
156003**
156004** The R-Tree MATCH operator will read the returned BLOB, deserialize
156005** the RtreeMatchArg object, and use the RtreeMatchArg object to figure
156006** out which elements of the R-Tree should be returned by the query.
156007*/
156008static void geomCallback(sqlite3_context *ctx, int nArg, sqlite3_value **aArg){
156009  RtreeGeomCallback *pGeomCtx = (RtreeGeomCallback *)sqlite3_user_data(ctx);
156010  RtreeMatchArg *pBlob;
156011  int nBlob;
156012  int memErr = 0;
156013
156014  nBlob = sizeof(RtreeMatchArg) + (nArg-1)*sizeof(RtreeDValue)
156015           + nArg*sizeof(sqlite3_value*);
156016  pBlob = (RtreeMatchArg *)sqlite3_malloc(nBlob);
156017  if( !pBlob ){
156018    sqlite3_result_error_nomem(ctx);
156019  }else{
156020    int i;
156021    pBlob->magic = RTREE_GEOMETRY_MAGIC;
156022    pBlob->cb = pGeomCtx[0];
156023    pBlob->apSqlParam = (sqlite3_value**)&pBlob->aParam[nArg];
156024    pBlob->nParam = nArg;
156025    for(i=0; i<nArg; i++){
156026      pBlob->apSqlParam[i] = sqlite3_value_dup(aArg[i]);
156027      if( pBlob->apSqlParam[i]==0 ) memErr = 1;
156028#ifdef SQLITE_RTREE_INT_ONLY
156029      pBlob->aParam[i] = sqlite3_value_int64(aArg[i]);
156030#else
156031      pBlob->aParam[i] = sqlite3_value_double(aArg[i]);
156032#endif
156033    }
156034    if( memErr ){
156035      sqlite3_result_error_nomem(ctx);
156036      rtreeMatchArgFree(pBlob);
156037    }else{
156038      sqlite3_result_blob(ctx, pBlob, nBlob, rtreeMatchArgFree);
156039    }
156040  }
156041}
156042
156043/*
156044** Register a new geometry function for use with the r-tree MATCH operator.
156045*/
156046SQLITE_API int SQLITE_STDCALL sqlite3_rtree_geometry_callback(
156047  sqlite3 *db,                  /* Register SQL function on this connection */
156048  const char *zGeom,            /* Name of the new SQL function */
156049  int (*xGeom)(sqlite3_rtree_geometry*,int,RtreeDValue*,int*), /* Callback */
156050  void *pContext                /* Extra data associated with the callback */
156051){
156052  RtreeGeomCallback *pGeomCtx;      /* Context object for new user-function */
156053
156054  /* Allocate and populate the context object. */
156055  pGeomCtx = (RtreeGeomCallback *)sqlite3_malloc(sizeof(RtreeGeomCallback));
156056  if( !pGeomCtx ) return SQLITE_NOMEM;
156057  pGeomCtx->xGeom = xGeom;
156058  pGeomCtx->xQueryFunc = 0;
156059  pGeomCtx->xDestructor = 0;
156060  pGeomCtx->pContext = pContext;
156061  return sqlite3_create_function_v2(db, zGeom, -1, SQLITE_ANY,
156062      (void *)pGeomCtx, geomCallback, 0, 0, rtreeFreeCallback
156063  );
156064}
156065
156066/*
156067** Register a new 2nd-generation geometry function for use with the
156068** r-tree MATCH operator.
156069*/
156070SQLITE_API int SQLITE_STDCALL sqlite3_rtree_query_callback(
156071  sqlite3 *db,                 /* Register SQL function on this connection */
156072  const char *zQueryFunc,      /* Name of new SQL function */
156073  int (*xQueryFunc)(sqlite3_rtree_query_info*), /* Callback */
156074  void *pContext,              /* Extra data passed into the callback */
156075  void (*xDestructor)(void*)   /* Destructor for the extra data */
156076){
156077  RtreeGeomCallback *pGeomCtx;      /* Context object for new user-function */
156078
156079  /* Allocate and populate the context object. */
156080  pGeomCtx = (RtreeGeomCallback *)sqlite3_malloc(sizeof(RtreeGeomCallback));
156081  if( !pGeomCtx ) return SQLITE_NOMEM;
156082  pGeomCtx->xGeom = 0;
156083  pGeomCtx->xQueryFunc = xQueryFunc;
156084  pGeomCtx->xDestructor = xDestructor;
156085  pGeomCtx->pContext = pContext;
156086  return sqlite3_create_function_v2(db, zQueryFunc, -1, SQLITE_ANY,
156087      (void *)pGeomCtx, geomCallback, 0, 0, rtreeFreeCallback
156088  );
156089}
156090
156091#if !SQLITE_CORE
156092#ifdef _WIN32
156093__declspec(dllexport)
156094#endif
156095SQLITE_API int SQLITE_STDCALL sqlite3_rtree_init(
156096  sqlite3 *db,
156097  char **pzErrMsg,
156098  const sqlite3_api_routines *pApi
156099){
156100  SQLITE_EXTENSION_INIT2(pApi)
156101  return sqlite3RtreeInit(db);
156102}
156103#endif
156104
156105#endif
156106
156107/************** End of rtree.c ***********************************************/
156108/************** Begin file icu.c *********************************************/
156109/*
156110** 2007 May 6
156111**
156112** The author disclaims copyright to this source code.  In place of
156113** a legal notice, here is a blessing:
156114**
156115**    May you do good and not evil.
156116**    May you find forgiveness for yourself and forgive others.
156117**    May you share freely, never taking more than you give.
156118**
156119*************************************************************************
156120** $Id: icu.c,v 1.7 2007/12/13 21:54:11 drh Exp $
156121**
156122** This file implements an integration between the ICU library
156123** ("International Components for Unicode", an open-source library
156124** for handling unicode data) and SQLite. The integration uses
156125** ICU to provide the following to SQLite:
156126**
156127**   * An implementation of the SQL regexp() function (and hence REGEXP
156128**     operator) using the ICU uregex_XX() APIs.
156129**
156130**   * Implementations of the SQL scalar upper() and lower() functions
156131**     for case mapping.
156132**
156133**   * Integration of ICU and SQLite collation sequences.
156134**
156135**   * An implementation of the LIKE operator that uses ICU to
156136**     provide case-independent matching.
156137*/
156138
156139#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_ICU)
156140
156141/* Include ICU headers */
156142#include <unicode/utypes.h>
156143#include <unicode/uregex.h>
156144#include <unicode/ustring.h>
156145#include <unicode/ucol.h>
156146
156147/* #include <assert.h> */
156148
156149#ifndef SQLITE_CORE
156150/*   #include "sqlite3ext.h" */
156151  SQLITE_EXTENSION_INIT1
156152#else
156153/*   #include "sqlite3.h" */
156154#endif
156155
156156/*
156157** Maximum length (in bytes) of the pattern in a LIKE or GLOB
156158** operator.
156159*/
156160#ifndef SQLITE_MAX_LIKE_PATTERN_LENGTH
156161# define SQLITE_MAX_LIKE_PATTERN_LENGTH 50000
156162#endif
156163
156164/*
156165** Version of sqlite3_free() that is always a function, never a macro.
156166*/
156167static void xFree(void *p){
156168  sqlite3_free(p);
156169}
156170
156171/*
156172** Compare two UTF-8 strings for equality where the first string is
156173** a "LIKE" expression. Return true (1) if they are the same and
156174** false (0) if they are different.
156175*/
156176static int icuLikeCompare(
156177  const uint8_t *zPattern,   /* LIKE pattern */
156178  const uint8_t *zString,    /* The UTF-8 string to compare against */
156179  const UChar32 uEsc         /* The escape character */
156180){
156181  static const int MATCH_ONE = (UChar32)'_';
156182  static const int MATCH_ALL = (UChar32)'%';
156183
156184  int iPattern = 0;       /* Current byte index in zPattern */
156185  int iString = 0;        /* Current byte index in zString */
156186
156187  int prevEscape = 0;     /* True if the previous character was uEsc */
156188
156189  while( zPattern[iPattern]!=0 ){
156190
156191    /* Read (and consume) the next character from the input pattern. */
156192    UChar32 uPattern;
156193    U8_NEXT_UNSAFE(zPattern, iPattern, uPattern);
156194
156195    /* There are now 4 possibilities:
156196    **
156197    **     1. uPattern is an unescaped match-all character "%",
156198    **     2. uPattern is an unescaped match-one character "_",
156199    **     3. uPattern is an unescaped escape character, or
156200    **     4. uPattern is to be handled as an ordinary character
156201    */
156202    if( !prevEscape && uPattern==MATCH_ALL ){
156203      /* Case 1. */
156204      uint8_t c;
156205
156206      /* Skip any MATCH_ALL or MATCH_ONE characters that follow a
156207      ** MATCH_ALL. For each MATCH_ONE, skip one character in the
156208      ** test string.
156209      */
156210      while( (c=zPattern[iPattern]) == MATCH_ALL || c == MATCH_ONE ){
156211        if( c==MATCH_ONE ){
156212          if( zString[iString]==0 ) return 0;
156213          U8_FWD_1_UNSAFE(zString, iString);
156214        }
156215        iPattern++;
156216      }
156217
156218      if( zPattern[iPattern]==0 ) return 1;
156219
156220      while( zString[iString] ){
156221        if( icuLikeCompare(&zPattern[iPattern], &zString[iString], uEsc) ){
156222          return 1;
156223        }
156224        U8_FWD_1_UNSAFE(zString, iString);
156225      }
156226      return 0;
156227
156228    }else if( !prevEscape && uPattern==MATCH_ONE ){
156229      /* Case 2. */
156230      if( zString[iString]==0 ) return 0;
156231      U8_FWD_1_UNSAFE(zString, iString);
156232
156233    }else if( !prevEscape && uPattern==uEsc){
156234      /* Case 3. */
156235      prevEscape = 1;
156236
156237    }else{
156238      /* Case 4. */
156239      UChar32 uString;
156240      U8_NEXT_UNSAFE(zString, iString, uString);
156241      uString = u_foldCase(uString, U_FOLD_CASE_DEFAULT);
156242      uPattern = u_foldCase(uPattern, U_FOLD_CASE_DEFAULT);
156243      if( uString!=uPattern ){
156244        return 0;
156245      }
156246      prevEscape = 0;
156247    }
156248  }
156249
156250  return zString[iString]==0;
156251}
156252
156253/*
156254** Implementation of the like() SQL function.  This function implements
156255** the build-in LIKE operator.  The first argument to the function is the
156256** pattern and the second argument is the string.  So, the SQL statements:
156257**
156258**       A LIKE B
156259**
156260** is implemented as like(B, A). If there is an escape character E,
156261**
156262**       A LIKE B ESCAPE E
156263**
156264** is mapped to like(B, A, E).
156265*/
156266static void icuLikeFunc(
156267  sqlite3_context *context,
156268  int argc,
156269  sqlite3_value **argv
156270){
156271  const unsigned char *zA = sqlite3_value_text(argv[0]);
156272  const unsigned char *zB = sqlite3_value_text(argv[1]);
156273  UChar32 uEsc = 0;
156274
156275  /* Limit the length of the LIKE or GLOB pattern to avoid problems
156276  ** of deep recursion and N*N behavior in patternCompare().
156277  */
156278  if( sqlite3_value_bytes(argv[0])>SQLITE_MAX_LIKE_PATTERN_LENGTH ){
156279    sqlite3_result_error(context, "LIKE or GLOB pattern too complex", -1);
156280    return;
156281  }
156282
156283
156284  if( argc==3 ){
156285    /* The escape character string must consist of a single UTF-8 character.
156286    ** Otherwise, return an error.
156287    */
156288    int nE= sqlite3_value_bytes(argv[2]);
156289    const unsigned char *zE = sqlite3_value_text(argv[2]);
156290    int i = 0;
156291    if( zE==0 ) return;
156292    U8_NEXT(zE, i, nE, uEsc);
156293    if( i!=nE){
156294      sqlite3_result_error(context,
156295          "ESCAPE expression must be a single character", -1);
156296      return;
156297    }
156298  }
156299
156300  if( zA && zB ){
156301    sqlite3_result_int(context, icuLikeCompare(zA, zB, uEsc));
156302  }
156303}
156304
156305/*
156306** This function is called when an ICU function called from within
156307** the implementation of an SQL scalar function returns an error.
156308**
156309** The scalar function context passed as the first argument is
156310** loaded with an error message based on the following two args.
156311*/
156312static void icuFunctionError(
156313  sqlite3_context *pCtx,       /* SQLite scalar function context */
156314  const char *zName,           /* Name of ICU function that failed */
156315  UErrorCode e                 /* Error code returned by ICU function */
156316){
156317  char zBuf[128];
156318  sqlite3_snprintf(128, zBuf, "ICU error: %s(): %s", zName, u_errorName(e));
156319  zBuf[127] = '\0';
156320  sqlite3_result_error(pCtx, zBuf, -1);
156321}
156322
156323/*
156324** Function to delete compiled regexp objects. Registered as
156325** a destructor function with sqlite3_set_auxdata().
156326*/
156327static void icuRegexpDelete(void *p){
156328  URegularExpression *pExpr = (URegularExpression *)p;
156329  uregex_close(pExpr);
156330}
156331
156332/*
156333** Implementation of SQLite REGEXP operator. This scalar function takes
156334** two arguments. The first is a regular expression pattern to compile
156335** the second is a string to match against that pattern. If either
156336** argument is an SQL NULL, then NULL Is returned. Otherwise, the result
156337** is 1 if the string matches the pattern, or 0 otherwise.
156338**
156339** SQLite maps the regexp() function to the regexp() operator such
156340** that the following two are equivalent:
156341**
156342**     zString REGEXP zPattern
156343**     regexp(zPattern, zString)
156344**
156345** Uses the following ICU regexp APIs:
156346**
156347**     uregex_open()
156348**     uregex_matches()
156349**     uregex_close()
156350*/
156351static void icuRegexpFunc(sqlite3_context *p, int nArg, sqlite3_value **apArg){
156352  UErrorCode status = U_ZERO_ERROR;
156353  URegularExpression *pExpr;
156354  UBool res;
156355  const UChar *zString = sqlite3_value_text16(apArg[1]);
156356
156357  (void)nArg;  /* Unused parameter */
156358
156359  /* If the left hand side of the regexp operator is NULL,
156360  ** then the result is also NULL.
156361  */
156362  if( !zString ){
156363    return;
156364  }
156365
156366  pExpr = sqlite3_get_auxdata(p, 0);
156367  if( !pExpr ){
156368    const UChar *zPattern = sqlite3_value_text16(apArg[0]);
156369    if( !zPattern ){
156370      return;
156371    }
156372    pExpr = uregex_open(zPattern, -1, 0, 0, &status);
156373
156374    if( U_SUCCESS(status) ){
156375      sqlite3_set_auxdata(p, 0, pExpr, icuRegexpDelete);
156376    }else{
156377      assert(!pExpr);
156378      icuFunctionError(p, "uregex_open", status);
156379      return;
156380    }
156381  }
156382
156383  /* Configure the text that the regular expression operates on. */
156384  uregex_setText(pExpr, zString, -1, &status);
156385  if( !U_SUCCESS(status) ){
156386    icuFunctionError(p, "uregex_setText", status);
156387    return;
156388  }
156389
156390  /* Attempt the match */
156391  res = uregex_matches(pExpr, 0, &status);
156392  if( !U_SUCCESS(status) ){
156393    icuFunctionError(p, "uregex_matches", status);
156394    return;
156395  }
156396
156397  /* Set the text that the regular expression operates on to a NULL
156398  ** pointer. This is not really necessary, but it is tidier than
156399  ** leaving the regular expression object configured with an invalid
156400  ** pointer after this function returns.
156401  */
156402  uregex_setText(pExpr, 0, 0, &status);
156403
156404  /* Return 1 or 0. */
156405  sqlite3_result_int(p, res ? 1 : 0);
156406}
156407
156408/*
156409** Implementations of scalar functions for case mapping - upper() and
156410** lower(). Function upper() converts its input to upper-case (ABC).
156411** Function lower() converts to lower-case (abc).
156412**
156413** ICU provides two types of case mapping, "general" case mapping and
156414** "language specific". Refer to ICU documentation for the differences
156415** between the two.
156416**
156417** To utilise "general" case mapping, the upper() or lower() scalar
156418** functions are invoked with one argument:
156419**
156420**     upper('ABC') -> 'abc'
156421**     lower('abc') -> 'ABC'
156422**
156423** To access ICU "language specific" case mapping, upper() or lower()
156424** should be invoked with two arguments. The second argument is the name
156425** of the locale to use. Passing an empty string ("") or SQL NULL value
156426** as the second argument is the same as invoking the 1 argument version
156427** of upper() or lower().
156428**
156429**     lower('I', 'en_us') -> 'i'
156430**     lower('I', 'tr_tr') -> '��' (small dotless i)
156431**
156432** http://www.icu-project.org/userguide/posix.html#case_mappings
156433*/
156434static void icuCaseFunc16(sqlite3_context *p, int nArg, sqlite3_value **apArg){
156435  const UChar *zInput;
156436  UChar *zOutput;
156437  int nInput;
156438  int nOutput;
156439
156440  UErrorCode status = U_ZERO_ERROR;
156441  const char *zLocale = 0;
156442
156443  assert(nArg==1 || nArg==2);
156444  if( nArg==2 ){
156445    zLocale = (const char *)sqlite3_value_text(apArg[1]);
156446  }
156447
156448  zInput = sqlite3_value_text16(apArg[0]);
156449  if( !zInput ){
156450    return;
156451  }
156452  nInput = sqlite3_value_bytes16(apArg[0]);
156453
156454  nOutput = nInput * 2 + 2;
156455  zOutput = sqlite3_malloc(nOutput);
156456  if( !zOutput ){
156457    return;
156458  }
156459
156460  if( sqlite3_user_data(p) ){
156461    u_strToUpper(zOutput, nOutput/2, zInput, nInput/2, zLocale, &status);
156462  }else{
156463    u_strToLower(zOutput, nOutput/2, zInput, nInput/2, zLocale, &status);
156464  }
156465
156466  if( !U_SUCCESS(status) ){
156467    icuFunctionError(p, "u_strToLower()/u_strToUpper", status);
156468    return;
156469  }
156470
156471  sqlite3_result_text16(p, zOutput, -1, xFree);
156472}
156473
156474/*
156475** Collation sequence destructor function. The pCtx argument points to
156476** a UCollator structure previously allocated using ucol_open().
156477*/
156478static void icuCollationDel(void *pCtx){
156479  UCollator *p = (UCollator *)pCtx;
156480  ucol_close(p);
156481}
156482
156483/*
156484** Collation sequence comparison function. The pCtx argument points to
156485** a UCollator structure previously allocated using ucol_open().
156486*/
156487static int icuCollationColl(
156488  void *pCtx,
156489  int nLeft,
156490  const void *zLeft,
156491  int nRight,
156492  const void *zRight
156493){
156494  UCollationResult res;
156495  UCollator *p = (UCollator *)pCtx;
156496  res = ucol_strcoll(p, (UChar *)zLeft, nLeft/2, (UChar *)zRight, nRight/2);
156497  switch( res ){
156498    case UCOL_LESS:    return -1;
156499    case UCOL_GREATER: return +1;
156500    case UCOL_EQUAL:   return 0;
156501  }
156502  assert(!"Unexpected return value from ucol_strcoll()");
156503  return 0;
156504}
156505
156506/*
156507** Implementation of the scalar function icu_load_collation().
156508**
156509** This scalar function is used to add ICU collation based collation
156510** types to an SQLite database connection. It is intended to be called
156511** as follows:
156512**
156513**     SELECT icu_load_collation(<locale>, <collation-name>);
156514**
156515** Where <locale> is a string containing an ICU locale identifier (i.e.
156516** "en_AU", "tr_TR" etc.) and <collation-name> is the name of the
156517** collation sequence to create.
156518*/
156519static void icuLoadCollation(
156520  sqlite3_context *p,
156521  int nArg,
156522  sqlite3_value **apArg
156523){
156524  sqlite3 *db = (sqlite3 *)sqlite3_user_data(p);
156525  UErrorCode status = U_ZERO_ERROR;
156526  const char *zLocale;      /* Locale identifier - (eg. "jp_JP") */
156527  const char *zName;        /* SQL Collation sequence name (eg. "japanese") */
156528  UCollator *pUCollator;    /* ICU library collation object */
156529  int rc;                   /* Return code from sqlite3_create_collation_x() */
156530
156531  assert(nArg==2);
156532  (void)nArg; /* Unused parameter */
156533  zLocale = (const char *)sqlite3_value_text(apArg[0]);
156534  zName = (const char *)sqlite3_value_text(apArg[1]);
156535
156536  if( !zLocale || !zName ){
156537    return;
156538  }
156539
156540  pUCollator = ucol_open(zLocale, &status);
156541  if( !U_SUCCESS(status) ){
156542    icuFunctionError(p, "ucol_open", status);
156543    return;
156544  }
156545  assert(p);
156546
156547  rc = sqlite3_create_collation_v2(db, zName, SQLITE_UTF16, (void *)pUCollator,
156548      icuCollationColl, icuCollationDel
156549  );
156550  if( rc!=SQLITE_OK ){
156551    ucol_close(pUCollator);
156552    sqlite3_result_error(p, "Error registering collation function", -1);
156553  }
156554}
156555
156556/*
156557** Register the ICU extension functions with database db.
156558*/
156559SQLITE_PRIVATE int sqlite3IcuInit(sqlite3 *db){
156560  struct IcuScalar {
156561    const char *zName;                        /* Function name */
156562    int nArg;                                 /* Number of arguments */
156563    int enc;                                  /* Optimal text encoding */
156564    void *pContext;                           /* sqlite3_user_data() context */
156565    void (*xFunc)(sqlite3_context*,int,sqlite3_value**);
156566  } scalars[] = {
156567    {"regexp", 2, SQLITE_ANY,          0, icuRegexpFunc},
156568
156569    {"lower",  1, SQLITE_UTF16,        0, icuCaseFunc16},
156570    {"lower",  2, SQLITE_UTF16,        0, icuCaseFunc16},
156571    {"upper",  1, SQLITE_UTF16, (void*)1, icuCaseFunc16},
156572    {"upper",  2, SQLITE_UTF16, (void*)1, icuCaseFunc16},
156573
156574    {"lower",  1, SQLITE_UTF8,         0, icuCaseFunc16},
156575    {"lower",  2, SQLITE_UTF8,         0, icuCaseFunc16},
156576    {"upper",  1, SQLITE_UTF8,  (void*)1, icuCaseFunc16},
156577    {"upper",  2, SQLITE_UTF8,  (void*)1, icuCaseFunc16},
156578
156579    {"like",   2, SQLITE_UTF8,         0, icuLikeFunc},
156580    {"like",   3, SQLITE_UTF8,         0, icuLikeFunc},
156581
156582    {"icu_load_collation",  2, SQLITE_UTF8, (void*)db, icuLoadCollation},
156583  };
156584
156585  int rc = SQLITE_OK;
156586  int i;
156587
156588  for(i=0; rc==SQLITE_OK && i<(int)(sizeof(scalars)/sizeof(scalars[0])); i++){
156589    struct IcuScalar *p = &scalars[i];
156590    rc = sqlite3_create_function(
156591        db, p->zName, p->nArg, p->enc, p->pContext, p->xFunc, 0, 0
156592    );
156593  }
156594
156595  return rc;
156596}
156597
156598#if !SQLITE_CORE
156599#ifdef _WIN32
156600__declspec(dllexport)
156601#endif
156602SQLITE_API int SQLITE_STDCALL sqlite3_icu_init(
156603  sqlite3 *db,
156604  char **pzErrMsg,
156605  const sqlite3_api_routines *pApi
156606){
156607  SQLITE_EXTENSION_INIT2(pApi)
156608  return sqlite3IcuInit(db);
156609}
156610#endif
156611
156612#endif
156613
156614/************** End of icu.c *************************************************/
156615/************** Begin file fts3_icu.c ****************************************/
156616/*
156617** 2007 June 22
156618**
156619** The author disclaims copyright to this source code.  In place of
156620** a legal notice, here is a blessing:
156621**
156622**    May you do good and not evil.
156623**    May you find forgiveness for yourself and forgive others.
156624**    May you share freely, never taking more than you give.
156625**
156626*************************************************************************
156627** This file implements a tokenizer for fts3 based on the ICU library.
156628*/
156629/* #include "fts3Int.h" */
156630#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
156631#ifdef SQLITE_ENABLE_ICU
156632
156633/* #include <assert.h> */
156634/* #include <string.h> */
156635/* #include "fts3_tokenizer.h" */
156636
156637#include <unicode/ubrk.h>
156638/* #include <unicode/ucol.h> */
156639/* #include <unicode/ustring.h> */
156640#include <unicode/utf16.h>
156641
156642typedef struct IcuTokenizer IcuTokenizer;
156643typedef struct IcuCursor IcuCursor;
156644
156645struct IcuTokenizer {
156646  sqlite3_tokenizer base;
156647  char *zLocale;
156648};
156649
156650struct IcuCursor {
156651  sqlite3_tokenizer_cursor base;
156652
156653  UBreakIterator *pIter;      /* ICU break-iterator object */
156654  int nChar;                  /* Number of UChar elements in pInput */
156655  UChar *aChar;               /* Copy of input using utf-16 encoding */
156656  int *aOffset;               /* Offsets of each character in utf-8 input */
156657
156658  int nBuffer;
156659  char *zBuffer;
156660
156661  int iToken;
156662};
156663
156664/*
156665** Create a new tokenizer instance.
156666*/
156667static int icuCreate(
156668  int argc,                            /* Number of entries in argv[] */
156669  const char * const *argv,            /* Tokenizer creation arguments */
156670  sqlite3_tokenizer **ppTokenizer      /* OUT: Created tokenizer */
156671){
156672  IcuTokenizer *p;
156673  int n = 0;
156674
156675  if( argc>0 ){
156676    n = strlen(argv[0])+1;
156677  }
156678  p = (IcuTokenizer *)sqlite3_malloc(sizeof(IcuTokenizer)+n);
156679  if( !p ){
156680    return SQLITE_NOMEM;
156681  }
156682  memset(p, 0, sizeof(IcuTokenizer));
156683
156684  if( n ){
156685    p->zLocale = (char *)&p[1];
156686    memcpy(p->zLocale, argv[0], n);
156687  }
156688
156689  *ppTokenizer = (sqlite3_tokenizer *)p;
156690
156691  return SQLITE_OK;
156692}
156693
156694/*
156695** Destroy a tokenizer
156696*/
156697static int icuDestroy(sqlite3_tokenizer *pTokenizer){
156698  IcuTokenizer *p = (IcuTokenizer *)pTokenizer;
156699  sqlite3_free(p);
156700  return SQLITE_OK;
156701}
156702
156703/*
156704** Prepare to begin tokenizing a particular string.  The input
156705** string to be tokenized is pInput[0..nBytes-1].  A cursor
156706** used to incrementally tokenize this string is returned in
156707** *ppCursor.
156708*/
156709static int icuOpen(
156710  sqlite3_tokenizer *pTokenizer,         /* The tokenizer */
156711  const char *zInput,                    /* Input string */
156712  int nInput,                            /* Length of zInput in bytes */
156713  sqlite3_tokenizer_cursor **ppCursor    /* OUT: Tokenization cursor */
156714){
156715  IcuTokenizer *p = (IcuTokenizer *)pTokenizer;
156716  IcuCursor *pCsr;
156717
156718  const int32_t opt = U_FOLD_CASE_DEFAULT;
156719  UErrorCode status = U_ZERO_ERROR;
156720  int nChar;
156721
156722  UChar32 c;
156723  int iInput = 0;
156724  int iOut = 0;
156725
156726  *ppCursor = 0;
156727
156728  if( zInput==0 ){
156729    nInput = 0;
156730    zInput = "";
156731  }else if( nInput<0 ){
156732    nInput = strlen(zInput);
156733  }
156734  nChar = nInput+1;
156735  pCsr = (IcuCursor *)sqlite3_malloc(
156736      sizeof(IcuCursor) +                /* IcuCursor */
156737      ((nChar+3)&~3) * sizeof(UChar) +   /* IcuCursor.aChar[] */
156738      (nChar+1) * sizeof(int)            /* IcuCursor.aOffset[] */
156739  );
156740  if( !pCsr ){
156741    return SQLITE_NOMEM;
156742  }
156743  memset(pCsr, 0, sizeof(IcuCursor));
156744  pCsr->aChar = (UChar *)&pCsr[1];
156745  pCsr->aOffset = (int *)&pCsr->aChar[(nChar+3)&~3];
156746
156747  pCsr->aOffset[iOut] = iInput;
156748  U8_NEXT(zInput, iInput, nInput, c);
156749  while( c>0 ){
156750    int isError = 0;
156751    c = u_foldCase(c, opt);
156752    U16_APPEND(pCsr->aChar, iOut, nChar, c, isError);
156753    if( isError ){
156754      sqlite3_free(pCsr);
156755      return SQLITE_ERROR;
156756    }
156757    pCsr->aOffset[iOut] = iInput;
156758
156759    if( iInput<nInput ){
156760      U8_NEXT(zInput, iInput, nInput, c);
156761    }else{
156762      c = 0;
156763    }
156764  }
156765
156766  pCsr->pIter = ubrk_open(UBRK_WORD, p->zLocale, pCsr->aChar, iOut, &status);
156767  if( !U_SUCCESS(status) ){
156768    sqlite3_free(pCsr);
156769    return SQLITE_ERROR;
156770  }
156771  pCsr->nChar = iOut;
156772
156773  ubrk_first(pCsr->pIter);
156774  *ppCursor = (sqlite3_tokenizer_cursor *)pCsr;
156775  return SQLITE_OK;
156776}
156777
156778/*
156779** Close a tokenization cursor previously opened by a call to icuOpen().
156780*/
156781static int icuClose(sqlite3_tokenizer_cursor *pCursor){
156782  IcuCursor *pCsr = (IcuCursor *)pCursor;
156783  ubrk_close(pCsr->pIter);
156784  sqlite3_free(pCsr->zBuffer);
156785  sqlite3_free(pCsr);
156786  return SQLITE_OK;
156787}
156788
156789/*
156790** Extract the next token from a tokenization cursor.
156791*/
156792static int icuNext(
156793  sqlite3_tokenizer_cursor *pCursor,  /* Cursor returned by simpleOpen */
156794  const char **ppToken,               /* OUT: *ppToken is the token text */
156795  int *pnBytes,                       /* OUT: Number of bytes in token */
156796  int *piStartOffset,                 /* OUT: Starting offset of token */
156797  int *piEndOffset,                   /* OUT: Ending offset of token */
156798  int *piPosition                     /* OUT: Position integer of token */
156799){
156800  IcuCursor *pCsr = (IcuCursor *)pCursor;
156801
156802  int iStart = 0;
156803  int iEnd = 0;
156804  int nByte = 0;
156805
156806  while( iStart==iEnd ){
156807    UChar32 c;
156808
156809    iStart = ubrk_current(pCsr->pIter);
156810    iEnd = ubrk_next(pCsr->pIter);
156811    if( iEnd==UBRK_DONE ){
156812      return SQLITE_DONE;
156813    }
156814
156815    while( iStart<iEnd ){
156816      int iWhite = iStart;
156817      U16_NEXT(pCsr->aChar, iWhite, pCsr->nChar, c);
156818      if( u_isspace(c) ){
156819        iStart = iWhite;
156820      }else{
156821        break;
156822      }
156823    }
156824    assert(iStart<=iEnd);
156825  }
156826
156827  do {
156828    UErrorCode status = U_ZERO_ERROR;
156829    if( nByte ){
156830      char *zNew = sqlite3_realloc(pCsr->zBuffer, nByte);
156831      if( !zNew ){
156832        return SQLITE_NOMEM;
156833      }
156834      pCsr->zBuffer = zNew;
156835      pCsr->nBuffer = nByte;
156836    }
156837
156838    u_strToUTF8(
156839        pCsr->zBuffer, pCsr->nBuffer, &nByte,    /* Output vars */
156840        &pCsr->aChar[iStart], iEnd-iStart,       /* Input vars */
156841        &status                                  /* Output success/failure */
156842    );
156843  } while( nByte>pCsr->nBuffer );
156844
156845  *ppToken = pCsr->zBuffer;
156846  *pnBytes = nByte;
156847  *piStartOffset = pCsr->aOffset[iStart];
156848  *piEndOffset = pCsr->aOffset[iEnd];
156849  *piPosition = pCsr->iToken++;
156850
156851  return SQLITE_OK;
156852}
156853
156854/*
156855** The set of routines that implement the simple tokenizer
156856*/
156857static const sqlite3_tokenizer_module icuTokenizerModule = {
156858  0,                           /* iVersion    */
156859  icuCreate,                   /* xCreate     */
156860  icuDestroy,                  /* xCreate     */
156861  icuOpen,                     /* xOpen       */
156862  icuClose,                    /* xClose      */
156863  icuNext,                     /* xNext       */
156864  0,                           /* xLanguageid */
156865};
156866
156867/*
156868** Set *ppModule to point at the implementation of the ICU tokenizer.
156869*/
156870SQLITE_PRIVATE void sqlite3Fts3IcuTokenizerModule(
156871  sqlite3_tokenizer_module const**ppModule
156872){
156873  *ppModule = &icuTokenizerModule;
156874}
156875
156876#endif /* defined(SQLITE_ENABLE_ICU) */
156877#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */
156878
156879/************** End of fts3_icu.c ********************************************/
156880/************** Begin file sqlite3rbu.c **************************************/
156881/*
156882** 2014 August 30
156883**
156884** The author disclaims copyright to this source code.  In place of
156885** a legal notice, here is a blessing:
156886**
156887**    May you do good and not evil.
156888**    May you find forgiveness for yourself and forgive others.
156889**    May you share freely, never taking more than you give.
156890**
156891*************************************************************************
156892**
156893**
156894** OVERVIEW
156895**
156896**  The RBU extension requires that the RBU update be packaged as an
156897**  SQLite database. The tables it expects to find are described in
156898**  sqlite3rbu.h.  Essentially, for each table xyz in the target database
156899**  that the user wishes to write to, a corresponding data_xyz table is
156900**  created in the RBU database and populated with one row for each row to
156901**  update, insert or delete from the target table.
156902**
156903**  The update proceeds in three stages:
156904**
156905**  1) The database is updated. The modified database pages are written
156906**     to a *-oal file. A *-oal file is just like a *-wal file, except
156907**     that it is named "<database>-oal" instead of "<database>-wal".
156908**     Because regular SQLite clients do not look for file named
156909**     "<database>-oal", they go on using the original database in
156910**     rollback mode while the *-oal file is being generated.
156911**
156912**     During this stage RBU does not update the database by writing
156913**     directly to the target tables. Instead it creates "imposter"
156914**     tables using the SQLITE_TESTCTRL_IMPOSTER interface that it uses
156915**     to update each b-tree individually. All updates required by each
156916**     b-tree are completed before moving on to the next, and all
156917**     updates are done in sorted key order.
156918**
156919**  2) The "<database>-oal" file is moved to the equivalent "<database>-wal"
156920**     location using a call to rename(2). Before doing this the RBU
156921**     module takes an EXCLUSIVE lock on the database file, ensuring
156922**     that there are no other active readers.
156923**
156924**     Once the EXCLUSIVE lock is released, any other database readers
156925**     detect the new *-wal file and read the database in wal mode. At
156926**     this point they see the new version of the database - including
156927**     the updates made as part of the RBU update.
156928**
156929**  3) The new *-wal file is checkpointed. This proceeds in the same way
156930**     as a regular database checkpoint, except that a single frame is
156931**     checkpointed each time sqlite3rbu_step() is called. If the RBU
156932**     handle is closed before the entire *-wal file is checkpointed,
156933**     the checkpoint progress is saved in the RBU database and the
156934**     checkpoint can be resumed by another RBU client at some point in
156935**     the future.
156936**
156937** POTENTIAL PROBLEMS
156938**
156939**  The rename() call might not be portable. And RBU is not currently
156940**  syncing the directory after renaming the file.
156941**
156942**  When state is saved, any commit to the *-oal file and the commit to
156943**  the RBU update database are not atomic. So if the power fails at the
156944**  wrong moment they might get out of sync. As the main database will be
156945**  committed before the RBU update database this will likely either just
156946**  pass unnoticed, or result in SQLITE_CONSTRAINT errors (due to UNIQUE
156947**  constraint violations).
156948**
156949**  If some client does modify the target database mid RBU update, or some
156950**  other error occurs, the RBU extension will keep throwing errors. It's
156951**  not really clear how to get out of this state. The system could just
156952**  by delete the RBU update database and *-oal file and have the device
156953**  download the update again and start over.
156954**
156955**  At present, for an UPDATE, both the new.* and old.* records are
156956**  collected in the rbu_xyz table. And for both UPDATEs and DELETEs all
156957**  fields are collected.  This means we're probably writing a lot more
156958**  data to disk when saving the state of an ongoing update to the RBU
156959**  update database than is strictly necessary.
156960**
156961*/
156962
156963/* #include <assert.h> */
156964/* #include <string.h> */
156965/* #include <stdio.h> */
156966
156967#if !defined(_WIN32)
156968/* #  include <unistd.h> */
156969#endif
156970
156971/* #include "sqlite3.h" */
156972
156973#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_RBU)
156974/************** Include sqlite3rbu.h in the middle of sqlite3rbu.c ***********/
156975/************** Begin file sqlite3rbu.h **************************************/
156976/*
156977** 2014 August 30
156978**
156979** The author disclaims copyright to this source code.  In place of
156980** a legal notice, here is a blessing:
156981**
156982**    May you do good and not evil.
156983**    May you find forgiveness for yourself and forgive others.
156984**    May you share freely, never taking more than you give.
156985**
156986*************************************************************************
156987**
156988** This file contains the public interface for the RBU extension.
156989*/
156990
156991/*
156992** SUMMARY
156993**
156994** Writing a transaction containing a large number of operations on
156995** b-tree indexes that are collectively larger than the available cache
156996** memory can be very inefficient.
156997**
156998** The problem is that in order to update a b-tree, the leaf page (at least)
156999** containing the entry being inserted or deleted must be modified. If the
157000** working set of leaves is larger than the available cache memory, then a
157001** single leaf that is modified more than once as part of the transaction
157002** may be loaded from or written to the persistent media multiple times.
157003** Additionally, because the index updates are likely to be applied in
157004** random order, access to pages within the database is also likely to be in
157005** random order, which is itself quite inefficient.
157006**
157007** One way to improve the situation is to sort the operations on each index
157008** by index key before applying them to the b-tree. This leads to an IO
157009** pattern that resembles a single linear scan through the index b-tree,
157010** and all but guarantees each modified leaf page is loaded and stored
157011** exactly once. SQLite uses this trick to improve the performance of
157012** CREATE INDEX commands. This extension allows it to be used to improve
157013** the performance of large transactions on existing databases.
157014**
157015** Additionally, this extension allows the work involved in writing the
157016** large transaction to be broken down into sub-transactions performed
157017** sequentially by separate processes. This is useful if the system cannot
157018** guarantee that a single update process will run for long enough to apply
157019** the entire update, for example because the update is being applied on a
157020** mobile device that is frequently rebooted. Even after the writer process
157021** has committed one or more sub-transactions, other database clients continue
157022** to read from the original database snapshot. In other words, partially
157023** applied transactions are not visible to other clients.
157024**
157025** "RBU" stands for "Resumable Bulk Update". As in a large database update
157026** transmitted via a wireless network to a mobile device. A transaction
157027** applied using this extension is hence refered to as an "RBU update".
157028**
157029**
157030** LIMITATIONS
157031**
157032** An "RBU update" transaction is subject to the following limitations:
157033**
157034**   * The transaction must consist of INSERT, UPDATE and DELETE operations
157035**     only.
157036**
157037**   * INSERT statements may not use any default values.
157038**
157039**   * UPDATE and DELETE statements must identify their target rows by
157040**     non-NULL PRIMARY KEY values. Rows with NULL values stored in PRIMARY
157041**     KEY fields may not be updated or deleted. If the table being written
157042**     has no PRIMARY KEY, affected rows must be identified by rowid.
157043**
157044**   * UPDATE statements may not modify PRIMARY KEY columns.
157045**
157046**   * No triggers will be fired.
157047**
157048**   * No foreign key violations are detected or reported.
157049**
157050**   * CHECK constraints are not enforced.
157051**
157052**   * No constraint handling mode except for "OR ROLLBACK" is supported.
157053**
157054**
157055** PREPARATION
157056**
157057** An "RBU update" is stored as a separate SQLite database. A database
157058** containing an RBU update is an "RBU database". For each table in the
157059** target database to be updated, the RBU database should contain a table
157060** named "data_<target name>" containing the same set of columns as the
157061** target table, and one more - "rbu_control". The data_% table should
157062** have no PRIMARY KEY or UNIQUE constraints, but each column should have
157063** the same type as the corresponding column in the target database.
157064** The "rbu_control" column should have no type at all. For example, if
157065** the target database contains:
157066**
157067**   CREATE TABLE t1(a INTEGER PRIMARY KEY, b TEXT, c UNIQUE);
157068**
157069** Then the RBU database should contain:
157070**
157071**   CREATE TABLE data_t1(a INTEGER, b TEXT, c, rbu_control);
157072**
157073** The order of the columns in the data_% table does not matter.
157074**
157075** If the target database table is a virtual table or a table that has no
157076** PRIMARY KEY declaration, the data_% table must also contain a column
157077** named "rbu_rowid". This column is mapped to the tables implicit primary
157078** key column - "rowid". Virtual tables for which the "rowid" column does
157079** not function like a primary key value cannot be updated using RBU. For
157080** example, if the target db contains either of the following:
157081**
157082**   CREATE VIRTUAL TABLE x1 USING fts3(a, b);
157083**   CREATE TABLE x1(a, b)
157084**
157085** then the RBU database should contain:
157086**
157087**   CREATE TABLE data_x1(a, b, rbu_rowid, rbu_control);
157088**
157089** All non-hidden columns (i.e. all columns matched by "SELECT *") of the
157090** target table must be present in the input table. For virtual tables,
157091** hidden columns are optional - they are updated by RBU if present in
157092** the input table, or not otherwise. For example, to write to an fts4
157093** table with a hidden languageid column such as:
157094**
157095**   CREATE VIRTUAL TABLE ft1 USING fts4(a, b, languageid='langid');
157096**
157097** Either of the following input table schemas may be used:
157098**
157099**   CREATE TABLE data_ft1(a, b, langid, rbu_rowid, rbu_control);
157100**   CREATE TABLE data_ft1(a, b, rbu_rowid, rbu_control);
157101**
157102** For each row to INSERT into the target database as part of the RBU
157103** update, the corresponding data_% table should contain a single record
157104** with the "rbu_control" column set to contain integer value 0. The
157105** other columns should be set to the values that make up the new record
157106** to insert.
157107**
157108** If the target database table has an INTEGER PRIMARY KEY, it is not
157109** possible to insert a NULL value into the IPK column. Attempting to
157110** do so results in an SQLITE_MISMATCH error.
157111**
157112** For each row to DELETE from the target database as part of the RBU
157113** update, the corresponding data_% table should contain a single record
157114** with the "rbu_control" column set to contain integer value 1. The
157115** real primary key values of the row to delete should be stored in the
157116** corresponding columns of the data_% table. The values stored in the
157117** other columns are not used.
157118**
157119** For each row to UPDATE from the target database as part of the RBU
157120** update, the corresponding data_% table should contain a single record
157121** with the "rbu_control" column set to contain a value of type text.
157122** The real primary key values identifying the row to update should be
157123** stored in the corresponding columns of the data_% table row, as should
157124** the new values of all columns being update. The text value in the
157125** "rbu_control" column must contain the same number of characters as
157126** there are columns in the target database table, and must consist entirely
157127** of 'x' and '.' characters (or in some special cases 'd' - see below). For
157128** each column that is being updated, the corresponding character is set to
157129** 'x'. For those that remain as they are, the corresponding character of the
157130** rbu_control value should be set to '.'. For example, given the tables
157131** above, the update statement:
157132**
157133**   UPDATE t1 SET c = 'usa' WHERE a = 4;
157134**
157135** is represented by the data_t1 row created by:
157136**
157137**   INSERT INTO data_t1(a, b, c, rbu_control) VALUES(4, NULL, 'usa', '..x');
157138**
157139** Instead of an 'x' character, characters of the rbu_control value specified
157140** for UPDATEs may also be set to 'd'. In this case, instead of updating the
157141** target table with the value stored in the corresponding data_% column, the
157142** user-defined SQL function "rbu_delta()" is invoked and the result stored in
157143** the target table column. rbu_delta() is invoked with two arguments - the
157144** original value currently stored in the target table column and the
157145** value specified in the data_xxx table.
157146**
157147** For example, this row:
157148**
157149**   INSERT INTO data_t1(a, b, c, rbu_control) VALUES(4, NULL, 'usa', '..d');
157150**
157151** is similar to an UPDATE statement such as:
157152**
157153**   UPDATE t1 SET c = rbu_delta(c, 'usa') WHERE a = 4;
157154**
157155** If the target database table is a virtual table or a table with no PRIMARY
157156** KEY, the rbu_control value should not include a character corresponding
157157** to the rbu_rowid value. For example, this:
157158**
157159**   INSERT INTO data_ft1(a, b, rbu_rowid, rbu_control)
157160**       VALUES(NULL, 'usa', 12, '.x');
157161**
157162** causes a result similar to:
157163**
157164**   UPDATE ft1 SET b = 'usa' WHERE rowid = 12;
157165**
157166** The data_xxx tables themselves should have no PRIMARY KEY declarations.
157167** However, RBU is more efficient if reading the rows in from each data_xxx
157168** table in "rowid" order is roughly the same as reading them sorted by
157169** the PRIMARY KEY of the corresponding target database table. In other
157170** words, rows should be sorted using the destination table PRIMARY KEY
157171** fields before they are inserted into the data_xxx tables.
157172**
157173** USAGE
157174**
157175** The API declared below allows an application to apply an RBU update
157176** stored on disk to an existing target database. Essentially, the
157177** application:
157178**
157179**     1) Opens an RBU handle using the sqlite3rbu_open() function.
157180**
157181**     2) Registers any required virtual table modules with the database
157182**        handle returned by sqlite3rbu_db(). Also, if required, register
157183**        the rbu_delta() implementation.
157184**
157185**     3) Calls the sqlite3rbu_step() function one or more times on
157186**        the new handle. Each call to sqlite3rbu_step() performs a single
157187**        b-tree operation, so thousands of calls may be required to apply
157188**        a complete update.
157189**
157190**     4) Calls sqlite3rbu_close() to close the RBU update handle. If
157191**        sqlite3rbu_step() has been called enough times to completely
157192**        apply the update to the target database, then the RBU database
157193**        is marked as fully applied. Otherwise, the state of the RBU
157194**        update application is saved in the RBU database for later
157195**        resumption.
157196**
157197** See comments below for more detail on APIs.
157198**
157199** If an update is only partially applied to the target database by the
157200** time sqlite3rbu_close() is called, various state information is saved
157201** within the RBU database. This allows subsequent processes to automatically
157202** resume the RBU update from where it left off.
157203**
157204** To remove all RBU extension state information, returning an RBU database
157205** to its original contents, it is sufficient to drop all tables that begin
157206** with the prefix "rbu_"
157207**
157208** DATABASE LOCKING
157209**
157210** An RBU update may not be applied to a database in WAL mode. Attempting
157211** to do so is an error (SQLITE_ERROR).
157212**
157213** While an RBU handle is open, a SHARED lock may be held on the target
157214** database file. This means it is possible for other clients to read the
157215** database, but not to write it.
157216**
157217** If an RBU update is started and then suspended before it is completed,
157218** then an external client writes to the database, then attempting to resume
157219** the suspended RBU update is also an error (SQLITE_BUSY).
157220*/
157221
157222#ifndef _SQLITE3RBU_H
157223#define _SQLITE3RBU_H
157224
157225/* #include "sqlite3.h"              ** Required for error code definitions ** */
157226
157227typedef struct sqlite3rbu sqlite3rbu;
157228
157229/*
157230** Open an RBU handle.
157231**
157232** Argument zTarget is the path to the target database. Argument zRbu is
157233** the path to the RBU database. Each call to this function must be matched
157234** by a call to sqlite3rbu_close(). When opening the databases, RBU passes
157235** the SQLITE_CONFIG_URI flag to sqlite3_open_v2(). So if either zTarget
157236** or zRbu begin with "file:", it will be interpreted as an SQLite
157237** database URI, not a regular file name.
157238**
157239** If the zState argument is passed a NULL value, the RBU extension stores
157240** the current state of the update (how many rows have been updated, which
157241** indexes are yet to be updated etc.) within the RBU database itself. This
157242** can be convenient, as it means that the RBU application does not need to
157243** organize removing a separate state file after the update is concluded.
157244** Or, if zState is non-NULL, it must be a path to a database file in which
157245** the RBU extension can store the state of the update.
157246**
157247** When resuming an RBU update, the zState argument must be passed the same
157248** value as when the RBU update was started.
157249**
157250** Once the RBU update is finished, the RBU extension does not
157251** automatically remove any zState database file, even if it created it.
157252**
157253** By default, RBU uses the default VFS to access the files on disk. To
157254** use a VFS other than the default, an SQLite "file:" URI containing a
157255** "vfs=..." option may be passed as the zTarget option.
157256**
157257** IMPORTANT NOTE FOR ZIPVFS USERS: The RBU extension works with all of
157258** SQLite's built-in VFSs, including the multiplexor VFS. However it does
157259** not work out of the box with zipvfs. Refer to the comment describing
157260** the zipvfs_create_vfs() API below for details on using RBU with zipvfs.
157261*/
157262SQLITE_API sqlite3rbu *SQLITE_STDCALL sqlite3rbu_open(
157263  const char *zTarget,
157264  const char *zRbu,
157265  const char *zState
157266);
157267
157268/*
157269** Internally, each RBU connection uses a separate SQLite database
157270** connection to access the target and rbu update databases. This
157271** API allows the application direct access to these database handles.
157272**
157273** The first argument passed to this function must be a valid, open, RBU
157274** handle. The second argument should be passed zero to access the target
157275** database handle, or non-zero to access the rbu update database handle.
157276** Accessing the underlying database handles may be useful in the
157277** following scenarios:
157278**
157279**   * If any target tables are virtual tables, it may be necessary to
157280**     call sqlite3_create_module() on the target database handle to
157281**     register the required virtual table implementations.
157282**
157283**   * If the data_xxx tables in the RBU source database are virtual
157284**     tables, the application may need to call sqlite3_create_module() on
157285**     the rbu update db handle to any required virtual table
157286**     implementations.
157287**
157288**   * If the application uses the "rbu_delta()" feature described above,
157289**     it must use sqlite3_create_function() or similar to register the
157290**     rbu_delta() implementation with the target database handle.
157291**
157292** If an error has occurred, either while opening or stepping the RBU object,
157293** this function may return NULL. The error code and message may be collected
157294** when sqlite3rbu_close() is called.
157295*/
157296SQLITE_API sqlite3 *SQLITE_STDCALL sqlite3rbu_db(sqlite3rbu*, int bRbu);
157297
157298/*
157299** Do some work towards applying the RBU update to the target db.
157300**
157301** Return SQLITE_DONE if the update has been completely applied, or
157302** SQLITE_OK if no error occurs but there remains work to do to apply
157303** the RBU update. If an error does occur, some other error code is
157304** returned.
157305**
157306** Once a call to sqlite3rbu_step() has returned a value other than
157307** SQLITE_OK, all subsequent calls on the same RBU handle are no-ops
157308** that immediately return the same value.
157309*/
157310SQLITE_API int SQLITE_STDCALL sqlite3rbu_step(sqlite3rbu *pRbu);
157311
157312/*
157313** Close an RBU handle.
157314**
157315** If the RBU update has been completely applied, mark the RBU database
157316** as fully applied. Otherwise, assuming no error has occurred, save the
157317** current state of the RBU update appliation to the RBU database.
157318**
157319** If an error has already occurred as part of an sqlite3rbu_step()
157320** or sqlite3rbu_open() call, or if one occurs within this function, an
157321** SQLite error code is returned. Additionally, *pzErrmsg may be set to
157322** point to a buffer containing a utf-8 formatted English language error
157323** message. It is the responsibility of the caller to eventually free any
157324** such buffer using sqlite3_free().
157325**
157326** Otherwise, if no error occurs, this function returns SQLITE_OK if the
157327** update has been partially applied, or SQLITE_DONE if it has been
157328** completely applied.
157329*/
157330SQLITE_API int SQLITE_STDCALL sqlite3rbu_close(sqlite3rbu *pRbu, char **pzErrmsg);
157331
157332/*
157333** Return the total number of key-value operations (inserts, deletes or
157334** updates) that have been performed on the target database since the
157335** current RBU update was started.
157336*/
157337SQLITE_API sqlite3_int64 SQLITE_STDCALL sqlite3rbu_progress(sqlite3rbu *pRbu);
157338
157339/*
157340** Create an RBU VFS named zName that accesses the underlying file-system
157341** via existing VFS zParent. Or, if the zParent parameter is passed NULL,
157342** then the new RBU VFS uses the default system VFS to access the file-system.
157343** The new object is registered as a non-default VFS with SQLite before
157344** returning.
157345**
157346** Part of the RBU implementation uses a custom VFS object. Usually, this
157347** object is created and deleted automatically by RBU.
157348**
157349** The exception is for applications that also use zipvfs. In this case,
157350** the custom VFS must be explicitly created by the user before the RBU
157351** handle is opened. The RBU VFS should be installed so that the zipvfs
157352** VFS uses the RBU VFS, which in turn uses any other VFS layers in use
157353** (for example multiplexor) to access the file-system. For example,
157354** to assemble an RBU enabled VFS stack that uses both zipvfs and
157355** multiplexor (error checking omitted):
157356**
157357**     // Create a VFS named "multiplex" (not the default).
157358**     sqlite3_multiplex_initialize(0, 0);
157359**
157360**     // Create an rbu VFS named "rbu" that uses multiplexor. If the
157361**     // second argument were replaced with NULL, the "rbu" VFS would
157362**     // access the file-system via the system default VFS, bypassing the
157363**     // multiplexor.
157364**     sqlite3rbu_create_vfs("rbu", "multiplex");
157365**
157366**     // Create a zipvfs VFS named "zipvfs" that uses rbu.
157367**     zipvfs_create_vfs_v3("zipvfs", "rbu", 0, xCompressorAlgorithmDetector);
157368**
157369**     // Make zipvfs the default VFS.
157370**     sqlite3_vfs_register(sqlite3_vfs_find("zipvfs"), 1);
157371**
157372** Because the default VFS created above includes a RBU functionality, it
157373** may be used by RBU clients. Attempting to use RBU with a zipvfs VFS stack
157374** that does not include the RBU layer results in an error.
157375**
157376** The overhead of adding the "rbu" VFS to the system is negligible for
157377** non-RBU users. There is no harm in an application accessing the
157378** file-system via "rbu" all the time, even if it only uses RBU functionality
157379** occasionally.
157380*/
157381SQLITE_API int SQLITE_STDCALL sqlite3rbu_create_vfs(const char *zName, const char *zParent);
157382
157383/*
157384** Deregister and destroy an RBU vfs created by an earlier call to
157385** sqlite3rbu_create_vfs().
157386**
157387** VFS objects are not reference counted. If a VFS object is destroyed
157388** before all database handles that use it have been closed, the results
157389** are undefined.
157390*/
157391SQLITE_API void SQLITE_STDCALL sqlite3rbu_destroy_vfs(const char *zName);
157392
157393#endif /* _SQLITE3RBU_H */
157394
157395/************** End of sqlite3rbu.h ******************************************/
157396/************** Continuing where we left off in sqlite3rbu.c *****************/
157397
157398/* Maximum number of prepared UPDATE statements held by this module */
157399#define SQLITE_RBU_UPDATE_CACHESIZE 16
157400
157401/*
157402** Swap two objects of type TYPE.
157403*/
157404#if !defined(SQLITE_AMALGAMATION)
157405# define SWAP(TYPE,A,B) {TYPE t=A; A=B; B=t;}
157406#endif
157407
157408/*
157409** The rbu_state table is used to save the state of a partially applied
157410** update so that it can be resumed later. The table consists of integer
157411** keys mapped to values as follows:
157412**
157413** RBU_STATE_STAGE:
157414**   May be set to integer values 1, 2, 4 or 5. As follows:
157415**       1: the *-rbu file is currently under construction.
157416**       2: the *-rbu file has been constructed, but not yet moved
157417**          to the *-wal path.
157418**       4: the checkpoint is underway.
157419**       5: the rbu update has been checkpointed.
157420**
157421** RBU_STATE_TBL:
157422**   Only valid if STAGE==1. The target database name of the table
157423**   currently being written.
157424**
157425** RBU_STATE_IDX:
157426**   Only valid if STAGE==1. The target database name of the index
157427**   currently being written, or NULL if the main table is currently being
157428**   updated.
157429**
157430** RBU_STATE_ROW:
157431**   Only valid if STAGE==1. Number of rows already processed for the current
157432**   table/index.
157433**
157434** RBU_STATE_PROGRESS:
157435**   Trbul number of sqlite3rbu_step() calls made so far as part of this
157436**   rbu update.
157437**
157438** RBU_STATE_CKPT:
157439**   Valid if STAGE==4. The 64-bit checksum associated with the wal-index
157440**   header created by recovering the *-wal file. This is used to detect
157441**   cases when another client appends frames to the *-wal file in the
157442**   middle of an incremental checkpoint (an incremental checkpoint cannot
157443**   be continued if this happens).
157444**
157445** RBU_STATE_COOKIE:
157446**   Valid if STAGE==1. The current change-counter cookie value in the
157447**   target db file.
157448**
157449** RBU_STATE_OALSZ:
157450**   Valid if STAGE==1. The size in bytes of the *-oal file.
157451*/
157452#define RBU_STATE_STAGE       1
157453#define RBU_STATE_TBL         2
157454#define RBU_STATE_IDX         3
157455#define RBU_STATE_ROW         4
157456#define RBU_STATE_PROGRESS    5
157457#define RBU_STATE_CKPT        6
157458#define RBU_STATE_COOKIE      7
157459#define RBU_STATE_OALSZ       8
157460
157461#define RBU_STAGE_OAL         1
157462#define RBU_STAGE_MOVE        2
157463#define RBU_STAGE_CAPTURE     3
157464#define RBU_STAGE_CKPT        4
157465#define RBU_STAGE_DONE        5
157466
157467
157468#define RBU_CREATE_STATE \
157469  "CREATE TABLE IF NOT EXISTS %s.rbu_state(k INTEGER PRIMARY KEY, v)"
157470
157471typedef struct RbuFrame RbuFrame;
157472typedef struct RbuObjIter RbuObjIter;
157473typedef struct RbuState RbuState;
157474typedef struct rbu_vfs rbu_vfs;
157475typedef struct rbu_file rbu_file;
157476typedef struct RbuUpdateStmt RbuUpdateStmt;
157477
157478#if !defined(SQLITE_AMALGAMATION)
157479typedef unsigned int u32;
157480typedef unsigned char u8;
157481typedef sqlite3_int64 i64;
157482#endif
157483
157484/*
157485** These values must match the values defined in wal.c for the equivalent
157486** locks. These are not magic numbers as they are part of the SQLite file
157487** format.
157488*/
157489#define WAL_LOCK_WRITE  0
157490#define WAL_LOCK_CKPT   1
157491#define WAL_LOCK_READ0  3
157492
157493/*
157494** A structure to store values read from the rbu_state table in memory.
157495*/
157496struct RbuState {
157497  int eStage;
157498  char *zTbl;
157499  char *zIdx;
157500  i64 iWalCksum;
157501  int nRow;
157502  i64 nProgress;
157503  u32 iCookie;
157504  i64 iOalSz;
157505};
157506
157507struct RbuUpdateStmt {
157508  char *zMask;                    /* Copy of update mask used with pUpdate */
157509  sqlite3_stmt *pUpdate;          /* Last update statement (or NULL) */
157510  RbuUpdateStmt *pNext;
157511};
157512
157513/*
157514** An iterator of this type is used to iterate through all objects in
157515** the target database that require updating. For each such table, the
157516** iterator visits, in order:
157517**
157518**     * the table itself,
157519**     * each index of the table (zero or more points to visit), and
157520**     * a special "cleanup table" state.
157521**
157522** abIndexed:
157523**   If the table has no indexes on it, abIndexed is set to NULL. Otherwise,
157524**   it points to an array of flags nTblCol elements in size. The flag is
157525**   set for each column that is either a part of the PK or a part of an
157526**   index. Or clear otherwise.
157527**
157528*/
157529struct RbuObjIter {
157530  sqlite3_stmt *pTblIter;         /* Iterate through tables */
157531  sqlite3_stmt *pIdxIter;         /* Index iterator */
157532  int nTblCol;                    /* Size of azTblCol[] array */
157533  char **azTblCol;                /* Array of unquoted target column names */
157534  char **azTblType;               /* Array of target column types */
157535  int *aiSrcOrder;                /* src table col -> target table col */
157536  u8 *abTblPk;                    /* Array of flags, set on target PK columns */
157537  u8 *abNotNull;                  /* Array of flags, set on NOT NULL columns */
157538  u8 *abIndexed;                  /* Array of flags, set on indexed & PK cols */
157539  int eType;                      /* Table type - an RBU_PK_XXX value */
157540
157541  /* Output variables. zTbl==0 implies EOF. */
157542  int bCleanup;                   /* True in "cleanup" state */
157543  const char *zTbl;               /* Name of target db table */
157544  const char *zIdx;               /* Name of target db index (or null) */
157545  int iTnum;                      /* Root page of current object */
157546  int iPkTnum;                    /* If eType==EXTERNAL, root of PK index */
157547  int bUnique;                    /* Current index is unique */
157548
157549  /* Statements created by rbuObjIterPrepareAll() */
157550  int nCol;                       /* Number of columns in current object */
157551  sqlite3_stmt *pSelect;          /* Source data */
157552  sqlite3_stmt *pInsert;          /* Statement for INSERT operations */
157553  sqlite3_stmt *pDelete;          /* Statement for DELETE ops */
157554  sqlite3_stmt *pTmpInsert;       /* Insert into rbu_tmp_$zTbl */
157555
157556  /* Last UPDATE used (for PK b-tree updates only), or NULL. */
157557  RbuUpdateStmt *pRbuUpdate;
157558};
157559
157560/*
157561** Values for RbuObjIter.eType
157562**
157563**     0: Table does not exist (error)
157564**     1: Table has an implicit rowid.
157565**     2: Table has an explicit IPK column.
157566**     3: Table has an external PK index.
157567**     4: Table is WITHOUT ROWID.
157568**     5: Table is a virtual table.
157569*/
157570#define RBU_PK_NOTABLE        0
157571#define RBU_PK_NONE           1
157572#define RBU_PK_IPK            2
157573#define RBU_PK_EXTERNAL       3
157574#define RBU_PK_WITHOUT_ROWID  4
157575#define RBU_PK_VTAB           5
157576
157577
157578/*
157579** Within the RBU_STAGE_OAL stage, each call to sqlite3rbu_step() performs
157580** one of the following operations.
157581*/
157582#define RBU_INSERT     1          /* Insert on a main table b-tree */
157583#define RBU_DELETE     2          /* Delete a row from a main table b-tree */
157584#define RBU_IDX_DELETE 3          /* Delete a row from an aux. index b-tree */
157585#define RBU_IDX_INSERT 4          /* Insert on an aux. index b-tree */
157586#define RBU_UPDATE     5          /* Update a row in a main table b-tree */
157587
157588
157589/*
157590** A single step of an incremental checkpoint - frame iWalFrame of the wal
157591** file should be copied to page iDbPage of the database file.
157592*/
157593struct RbuFrame {
157594  u32 iDbPage;
157595  u32 iWalFrame;
157596};
157597
157598/*
157599** RBU handle.
157600*/
157601struct sqlite3rbu {
157602  int eStage;                     /* Value of RBU_STATE_STAGE field */
157603  sqlite3 *dbMain;                /* target database handle */
157604  sqlite3 *dbRbu;                 /* rbu database handle */
157605  char *zTarget;                  /* Path to target db */
157606  char *zRbu;                     /* Path to rbu db */
157607  char *zState;                   /* Path to state db (or NULL if zRbu) */
157608  char zStateDb[5];               /* Db name for state ("stat" or "main") */
157609  int rc;                         /* Value returned by last rbu_step() call */
157610  char *zErrmsg;                  /* Error message if rc!=SQLITE_OK */
157611  int nStep;                      /* Rows processed for current object */
157612  int nProgress;                  /* Rows processed for all objects */
157613  RbuObjIter objiter;             /* Iterator for skipping through tbl/idx */
157614  const char *zVfsName;           /* Name of automatically created rbu vfs */
157615  rbu_file *pTargetFd;            /* File handle open on target db */
157616  i64 iOalSz;
157617
157618  /* The following state variables are used as part of the incremental
157619  ** checkpoint stage (eStage==RBU_STAGE_CKPT). See comments surrounding
157620  ** function rbuSetupCheckpoint() for details.  */
157621  u32 iMaxFrame;                  /* Largest iWalFrame value in aFrame[] */
157622  u32 mLock;
157623  int nFrame;                     /* Entries in aFrame[] array */
157624  int nFrameAlloc;                /* Allocated size of aFrame[] array */
157625  RbuFrame *aFrame;
157626  int pgsz;
157627  u8 *aBuf;
157628  i64 iWalCksum;
157629};
157630
157631/*
157632** An rbu VFS is implemented using an instance of this structure.
157633*/
157634struct rbu_vfs {
157635  sqlite3_vfs base;               /* rbu VFS shim methods */
157636  sqlite3_vfs *pRealVfs;          /* Underlying VFS */
157637  sqlite3_mutex *mutex;           /* Mutex to protect pMain */
157638  rbu_file *pMain;                /* Linked list of main db files */
157639};
157640
157641/*
157642** Each file opened by an rbu VFS is represented by an instance of
157643** the following structure.
157644*/
157645struct rbu_file {
157646  sqlite3_file base;              /* sqlite3_file methods */
157647  sqlite3_file *pReal;            /* Underlying file handle */
157648  rbu_vfs *pRbuVfs;               /* Pointer to the rbu_vfs object */
157649  sqlite3rbu *pRbu;               /* Pointer to rbu object (rbu target only) */
157650
157651  int openFlags;                  /* Flags this file was opened with */
157652  u32 iCookie;                    /* Cookie value for main db files */
157653  u8 iWriteVer;                   /* "write-version" value for main db files */
157654
157655  int nShm;                       /* Number of entries in apShm[] array */
157656  char **apShm;                   /* Array of mmap'd *-shm regions */
157657  char *zDel;                     /* Delete this when closing file */
157658
157659  const char *zWal;               /* Wal filename for this main db file */
157660  rbu_file *pWalFd;               /* Wal file descriptor for this main db */
157661  rbu_file *pMainNext;            /* Next MAIN_DB file */
157662};
157663
157664
157665/*
157666** Prepare the SQL statement in buffer zSql against database handle db.
157667** If successful, set *ppStmt to point to the new statement and return
157668** SQLITE_OK.
157669**
157670** Otherwise, if an error does occur, set *ppStmt to NULL and return
157671** an SQLite error code. Additionally, set output variable *pzErrmsg to
157672** point to a buffer containing an error message. It is the responsibility
157673** of the caller to (eventually) free this buffer using sqlite3_free().
157674*/
157675static int prepareAndCollectError(
157676  sqlite3 *db,
157677  sqlite3_stmt **ppStmt,
157678  char **pzErrmsg,
157679  const char *zSql
157680){
157681  int rc = sqlite3_prepare_v2(db, zSql, -1, ppStmt, 0);
157682  if( rc!=SQLITE_OK ){
157683    *pzErrmsg = sqlite3_mprintf("%s", sqlite3_errmsg(db));
157684    *ppStmt = 0;
157685  }
157686  return rc;
157687}
157688
157689/*
157690** Reset the SQL statement passed as the first argument. Return a copy
157691** of the value returned by sqlite3_reset().
157692**
157693** If an error has occurred, then set *pzErrmsg to point to a buffer
157694** containing an error message. It is the responsibility of the caller
157695** to eventually free this buffer using sqlite3_free().
157696*/
157697static int resetAndCollectError(sqlite3_stmt *pStmt, char **pzErrmsg){
157698  int rc = sqlite3_reset(pStmt);
157699  if( rc!=SQLITE_OK ){
157700    *pzErrmsg = sqlite3_mprintf("%s", sqlite3_errmsg(sqlite3_db_handle(pStmt)));
157701  }
157702  return rc;
157703}
157704
157705/*
157706** Unless it is NULL, argument zSql points to a buffer allocated using
157707** sqlite3_malloc containing an SQL statement. This function prepares the SQL
157708** statement against database db and frees the buffer. If statement
157709** compilation is successful, *ppStmt is set to point to the new statement
157710** handle and SQLITE_OK is returned.
157711**
157712** Otherwise, if an error occurs, *ppStmt is set to NULL and an error code
157713** returned. In this case, *pzErrmsg may also be set to point to an error
157714** message. It is the responsibility of the caller to free this error message
157715** buffer using sqlite3_free().
157716**
157717** If argument zSql is NULL, this function assumes that an OOM has occurred.
157718** In this case SQLITE_NOMEM is returned and *ppStmt set to NULL.
157719*/
157720static int prepareFreeAndCollectError(
157721  sqlite3 *db,
157722  sqlite3_stmt **ppStmt,
157723  char **pzErrmsg,
157724  char *zSql
157725){
157726  int rc;
157727  assert( *pzErrmsg==0 );
157728  if( zSql==0 ){
157729    rc = SQLITE_NOMEM;
157730    *ppStmt = 0;
157731  }else{
157732    rc = prepareAndCollectError(db, ppStmt, pzErrmsg, zSql);
157733    sqlite3_free(zSql);
157734  }
157735  return rc;
157736}
157737
157738/*
157739** Free the RbuObjIter.azTblCol[] and RbuObjIter.abTblPk[] arrays allocated
157740** by an earlier call to rbuObjIterCacheTableInfo().
157741*/
157742static void rbuObjIterFreeCols(RbuObjIter *pIter){
157743  int i;
157744  for(i=0; i<pIter->nTblCol; i++){
157745    sqlite3_free(pIter->azTblCol[i]);
157746    sqlite3_free(pIter->azTblType[i]);
157747  }
157748  sqlite3_free(pIter->azTblCol);
157749  pIter->azTblCol = 0;
157750  pIter->azTblType = 0;
157751  pIter->aiSrcOrder = 0;
157752  pIter->abTblPk = 0;
157753  pIter->abNotNull = 0;
157754  pIter->nTblCol = 0;
157755  pIter->eType = 0;               /* Invalid value */
157756}
157757
157758/*
157759** Finalize all statements and free all allocations that are specific to
157760** the current object (table/index pair).
157761*/
157762static void rbuObjIterClearStatements(RbuObjIter *pIter){
157763  RbuUpdateStmt *pUp;
157764
157765  sqlite3_finalize(pIter->pSelect);
157766  sqlite3_finalize(pIter->pInsert);
157767  sqlite3_finalize(pIter->pDelete);
157768  sqlite3_finalize(pIter->pTmpInsert);
157769  pUp = pIter->pRbuUpdate;
157770  while( pUp ){
157771    RbuUpdateStmt *pTmp = pUp->pNext;
157772    sqlite3_finalize(pUp->pUpdate);
157773    sqlite3_free(pUp);
157774    pUp = pTmp;
157775  }
157776
157777  pIter->pSelect = 0;
157778  pIter->pInsert = 0;
157779  pIter->pDelete = 0;
157780  pIter->pRbuUpdate = 0;
157781  pIter->pTmpInsert = 0;
157782  pIter->nCol = 0;
157783}
157784
157785/*
157786** Clean up any resources allocated as part of the iterator object passed
157787** as the only argument.
157788*/
157789static void rbuObjIterFinalize(RbuObjIter *pIter){
157790  rbuObjIterClearStatements(pIter);
157791  sqlite3_finalize(pIter->pTblIter);
157792  sqlite3_finalize(pIter->pIdxIter);
157793  rbuObjIterFreeCols(pIter);
157794  memset(pIter, 0, sizeof(RbuObjIter));
157795}
157796
157797/*
157798** Advance the iterator to the next position.
157799**
157800** If no error occurs, SQLITE_OK is returned and the iterator is left
157801** pointing to the next entry. Otherwise, an error code and message is
157802** left in the RBU handle passed as the first argument. A copy of the
157803** error code is returned.
157804*/
157805static int rbuObjIterNext(sqlite3rbu *p, RbuObjIter *pIter){
157806  int rc = p->rc;
157807  if( rc==SQLITE_OK ){
157808
157809    /* Free any SQLite statements used while processing the previous object */
157810    rbuObjIterClearStatements(pIter);
157811    if( pIter->zIdx==0 ){
157812      rc = sqlite3_exec(p->dbMain,
157813          "DROP TRIGGER IF EXISTS temp.rbu_insert_tr;"
157814          "DROP TRIGGER IF EXISTS temp.rbu_update1_tr;"
157815          "DROP TRIGGER IF EXISTS temp.rbu_update2_tr;"
157816          "DROP TRIGGER IF EXISTS temp.rbu_delete_tr;"
157817          , 0, 0, &p->zErrmsg
157818      );
157819    }
157820
157821    if( rc==SQLITE_OK ){
157822      if( pIter->bCleanup ){
157823        rbuObjIterFreeCols(pIter);
157824        pIter->bCleanup = 0;
157825        rc = sqlite3_step(pIter->pTblIter);
157826        if( rc!=SQLITE_ROW ){
157827          rc = resetAndCollectError(pIter->pTblIter, &p->zErrmsg);
157828          pIter->zTbl = 0;
157829        }else{
157830          pIter->zTbl = (const char*)sqlite3_column_text(pIter->pTblIter, 0);
157831          rc = pIter->zTbl ? SQLITE_OK : SQLITE_NOMEM;
157832        }
157833      }else{
157834        if( pIter->zIdx==0 ){
157835          sqlite3_stmt *pIdx = pIter->pIdxIter;
157836          rc = sqlite3_bind_text(pIdx, 1, pIter->zTbl, -1, SQLITE_STATIC);
157837        }
157838        if( rc==SQLITE_OK ){
157839          rc = sqlite3_step(pIter->pIdxIter);
157840          if( rc!=SQLITE_ROW ){
157841            rc = resetAndCollectError(pIter->pIdxIter, &p->zErrmsg);
157842            pIter->bCleanup = 1;
157843            pIter->zIdx = 0;
157844          }else{
157845            pIter->zIdx = (const char*)sqlite3_column_text(pIter->pIdxIter, 0);
157846            pIter->iTnum = sqlite3_column_int(pIter->pIdxIter, 1);
157847            pIter->bUnique = sqlite3_column_int(pIter->pIdxIter, 2);
157848            rc = pIter->zIdx ? SQLITE_OK : SQLITE_NOMEM;
157849          }
157850        }
157851      }
157852    }
157853  }
157854
157855  if( rc!=SQLITE_OK ){
157856    rbuObjIterFinalize(pIter);
157857    p->rc = rc;
157858  }
157859  return rc;
157860}
157861
157862/*
157863** Initialize the iterator structure passed as the second argument.
157864**
157865** If no error occurs, SQLITE_OK is returned and the iterator is left
157866** pointing to the first entry. Otherwise, an error code and message is
157867** left in the RBU handle passed as the first argument. A copy of the
157868** error code is returned.
157869*/
157870static int rbuObjIterFirst(sqlite3rbu *p, RbuObjIter *pIter){
157871  int rc;
157872  memset(pIter, 0, sizeof(RbuObjIter));
157873
157874  rc = prepareAndCollectError(p->dbRbu, &pIter->pTblIter, &p->zErrmsg,
157875      "SELECT substr(name, 6) FROM sqlite_master "
157876      "WHERE type IN ('table', 'view') AND name LIKE 'data_%'"
157877  );
157878
157879  if( rc==SQLITE_OK ){
157880    rc = prepareAndCollectError(p->dbMain, &pIter->pIdxIter, &p->zErrmsg,
157881        "SELECT name, rootpage, sql IS NULL OR substr(8, 6)=='UNIQUE' "
157882        "  FROM main.sqlite_master "
157883        "  WHERE type='index' AND tbl_name = ?"
157884    );
157885  }
157886
157887  pIter->bCleanup = 1;
157888  p->rc = rc;
157889  return rbuObjIterNext(p, pIter);
157890}
157891
157892/*
157893** This is a wrapper around "sqlite3_mprintf(zFmt, ...)". If an OOM occurs,
157894** an error code is stored in the RBU handle passed as the first argument.
157895**
157896** If an error has already occurred (p->rc is already set to something other
157897** than SQLITE_OK), then this function returns NULL without modifying the
157898** stored error code. In this case it still calls sqlite3_free() on any
157899** printf() parameters associated with %z conversions.
157900*/
157901static char *rbuMPrintf(sqlite3rbu *p, const char *zFmt, ...){
157902  char *zSql = 0;
157903  va_list ap;
157904  va_start(ap, zFmt);
157905  zSql = sqlite3_vmprintf(zFmt, ap);
157906  if( p->rc==SQLITE_OK ){
157907    if( zSql==0 ) p->rc = SQLITE_NOMEM;
157908  }else{
157909    sqlite3_free(zSql);
157910    zSql = 0;
157911  }
157912  va_end(ap);
157913  return zSql;
157914}
157915
157916/*
157917** Argument zFmt is a sqlite3_mprintf() style format string. The trailing
157918** arguments are the usual subsitution values. This function performs
157919** the printf() style substitutions and executes the result as an SQL
157920** statement on the RBU handles database.
157921**
157922** If an error occurs, an error code and error message is stored in the
157923** RBU handle. If an error has already occurred when this function is
157924** called, it is a no-op.
157925*/
157926static int rbuMPrintfExec(sqlite3rbu *p, sqlite3 *db, const char *zFmt, ...){
157927  va_list ap;
157928  char *zSql;
157929  va_start(ap, zFmt);
157930  zSql = sqlite3_vmprintf(zFmt, ap);
157931  if( p->rc==SQLITE_OK ){
157932    if( zSql==0 ){
157933      p->rc = SQLITE_NOMEM;
157934    }else{
157935      p->rc = sqlite3_exec(db, zSql, 0, 0, &p->zErrmsg);
157936    }
157937  }
157938  sqlite3_free(zSql);
157939  va_end(ap);
157940  return p->rc;
157941}
157942
157943/*
157944** Attempt to allocate and return a pointer to a zeroed block of nByte
157945** bytes.
157946**
157947** If an error (i.e. an OOM condition) occurs, return NULL and leave an
157948** error code in the rbu handle passed as the first argument. Or, if an
157949** error has already occurred when this function is called, return NULL
157950** immediately without attempting the allocation or modifying the stored
157951** error code.
157952*/
157953static void *rbuMalloc(sqlite3rbu *p, int nByte){
157954  void *pRet = 0;
157955  if( p->rc==SQLITE_OK ){
157956    assert( nByte>0 );
157957    pRet = sqlite3_malloc(nByte);
157958    if( pRet==0 ){
157959      p->rc = SQLITE_NOMEM;
157960    }else{
157961      memset(pRet, 0, nByte);
157962    }
157963  }
157964  return pRet;
157965}
157966
157967
157968/*
157969** Allocate and zero the pIter->azTblCol[] and abTblPk[] arrays so that
157970** there is room for at least nCol elements. If an OOM occurs, store an
157971** error code in the RBU handle passed as the first argument.
157972*/
157973static void rbuAllocateIterArrays(sqlite3rbu *p, RbuObjIter *pIter, int nCol){
157974  int nByte = (2*sizeof(char*) + sizeof(int) + 3*sizeof(u8)) * nCol;
157975  char **azNew;
157976
157977  azNew = (char**)rbuMalloc(p, nByte);
157978  if( azNew ){
157979    pIter->azTblCol = azNew;
157980    pIter->azTblType = &azNew[nCol];
157981    pIter->aiSrcOrder = (int*)&pIter->azTblType[nCol];
157982    pIter->abTblPk = (u8*)&pIter->aiSrcOrder[nCol];
157983    pIter->abNotNull = (u8*)&pIter->abTblPk[nCol];
157984    pIter->abIndexed = (u8*)&pIter->abNotNull[nCol];
157985  }
157986}
157987
157988/*
157989** The first argument must be a nul-terminated string. This function
157990** returns a copy of the string in memory obtained from sqlite3_malloc().
157991** It is the responsibility of the caller to eventually free this memory
157992** using sqlite3_free().
157993**
157994** If an OOM condition is encountered when attempting to allocate memory,
157995** output variable (*pRc) is set to SQLITE_NOMEM before returning. Otherwise,
157996** if the allocation succeeds, (*pRc) is left unchanged.
157997*/
157998static char *rbuStrndup(const char *zStr, int *pRc){
157999  char *zRet = 0;
158000
158001  assert( *pRc==SQLITE_OK );
158002  if( zStr ){
158003    int nCopy = strlen(zStr) + 1;
158004    zRet = (char*)sqlite3_malloc(nCopy);
158005    if( zRet ){
158006      memcpy(zRet, zStr, nCopy);
158007    }else{
158008      *pRc = SQLITE_NOMEM;
158009    }
158010  }
158011
158012  return zRet;
158013}
158014
158015/*
158016** Finalize the statement passed as the second argument.
158017**
158018** If the sqlite3_finalize() call indicates that an error occurs, and the
158019** rbu handle error code is not already set, set the error code and error
158020** message accordingly.
158021*/
158022static void rbuFinalize(sqlite3rbu *p, sqlite3_stmt *pStmt){
158023  sqlite3 *db = sqlite3_db_handle(pStmt);
158024  int rc = sqlite3_finalize(pStmt);
158025  if( p->rc==SQLITE_OK && rc!=SQLITE_OK ){
158026    p->rc = rc;
158027    p->zErrmsg = sqlite3_mprintf("%s", sqlite3_errmsg(db));
158028  }
158029}
158030
158031/* Determine the type of a table.
158032**
158033**   peType is of type (int*), a pointer to an output parameter of type
158034**   (int). This call sets the output parameter as follows, depending
158035**   on the type of the table specified by parameters dbName and zTbl.
158036**
158037**     RBU_PK_NOTABLE:       No such table.
158038**     RBU_PK_NONE:          Table has an implicit rowid.
158039**     RBU_PK_IPK:           Table has an explicit IPK column.
158040**     RBU_PK_EXTERNAL:      Table has an external PK index.
158041**     RBU_PK_WITHOUT_ROWID: Table is WITHOUT ROWID.
158042**     RBU_PK_VTAB:          Table is a virtual table.
158043**
158044**   Argument *piPk is also of type (int*), and also points to an output
158045**   parameter. Unless the table has an external primary key index
158046**   (i.e. unless *peType is set to 3), then *piPk is set to zero. Or,
158047**   if the table does have an external primary key index, then *piPk
158048**   is set to the root page number of the primary key index before
158049**   returning.
158050**
158051** ALGORITHM:
158052**
158053**   if( no entry exists in sqlite_master ){
158054**     return RBU_PK_NOTABLE
158055**   }else if( sql for the entry starts with "CREATE VIRTUAL" ){
158056**     return RBU_PK_VTAB
158057**   }else if( "PRAGMA index_list()" for the table contains a "pk" index ){
158058**     if( the index that is the pk exists in sqlite_master ){
158059**       *piPK = rootpage of that index.
158060**       return RBU_PK_EXTERNAL
158061**     }else{
158062**       return RBU_PK_WITHOUT_ROWID
158063**     }
158064**   }else if( "PRAGMA table_info()" lists one or more "pk" columns ){
158065**     return RBU_PK_IPK
158066**   }else{
158067**     return RBU_PK_NONE
158068**   }
158069*/
158070static void rbuTableType(
158071  sqlite3rbu *p,
158072  const char *zTab,
158073  int *peType,
158074  int *piTnum,
158075  int *piPk
158076){
158077  /*
158078  ** 0) SELECT count(*) FROM sqlite_master where name=%Q AND IsVirtual(%Q)
158079  ** 1) PRAGMA index_list = ?
158080  ** 2) SELECT count(*) FROM sqlite_master where name=%Q
158081  ** 3) PRAGMA table_info = ?
158082  */
158083  sqlite3_stmt *aStmt[4] = {0, 0, 0, 0};
158084
158085  *peType = RBU_PK_NOTABLE;
158086  *piPk = 0;
158087
158088  assert( p->rc==SQLITE_OK );
158089  p->rc = prepareFreeAndCollectError(p->dbMain, &aStmt[0], &p->zErrmsg,
158090    sqlite3_mprintf(
158091          "SELECT (sql LIKE 'create virtual%%'), rootpage"
158092          "  FROM sqlite_master"
158093          " WHERE name=%Q", zTab
158094  ));
158095  if( p->rc!=SQLITE_OK || sqlite3_step(aStmt[0])!=SQLITE_ROW ){
158096    /* Either an error, or no such table. */
158097    goto rbuTableType_end;
158098  }
158099  if( sqlite3_column_int(aStmt[0], 0) ){
158100    *peType = RBU_PK_VTAB;                     /* virtual table */
158101    goto rbuTableType_end;
158102  }
158103  *piTnum = sqlite3_column_int(aStmt[0], 1);
158104
158105  p->rc = prepareFreeAndCollectError(p->dbMain, &aStmt[1], &p->zErrmsg,
158106    sqlite3_mprintf("PRAGMA index_list=%Q",zTab)
158107  );
158108  if( p->rc ) goto rbuTableType_end;
158109  while( sqlite3_step(aStmt[1])==SQLITE_ROW ){
158110    const u8 *zOrig = sqlite3_column_text(aStmt[1], 3);
158111    const u8 *zIdx = sqlite3_column_text(aStmt[1], 1);
158112    if( zOrig && zIdx && zOrig[0]=='p' ){
158113      p->rc = prepareFreeAndCollectError(p->dbMain, &aStmt[2], &p->zErrmsg,
158114          sqlite3_mprintf(
158115            "SELECT rootpage FROM sqlite_master WHERE name = %Q", zIdx
158116      ));
158117      if( p->rc==SQLITE_OK ){
158118        if( sqlite3_step(aStmt[2])==SQLITE_ROW ){
158119          *piPk = sqlite3_column_int(aStmt[2], 0);
158120          *peType = RBU_PK_EXTERNAL;
158121        }else{
158122          *peType = RBU_PK_WITHOUT_ROWID;
158123        }
158124      }
158125      goto rbuTableType_end;
158126    }
158127  }
158128
158129  p->rc = prepareFreeAndCollectError(p->dbMain, &aStmt[3], &p->zErrmsg,
158130    sqlite3_mprintf("PRAGMA table_info=%Q",zTab)
158131  );
158132  if( p->rc==SQLITE_OK ){
158133    while( sqlite3_step(aStmt[3])==SQLITE_ROW ){
158134      if( sqlite3_column_int(aStmt[3],5)>0 ){
158135        *peType = RBU_PK_IPK;                /* explicit IPK column */
158136        goto rbuTableType_end;
158137      }
158138    }
158139    *peType = RBU_PK_NONE;
158140  }
158141
158142rbuTableType_end: {
158143    int i;
158144    for(i=0; i<sizeof(aStmt)/sizeof(aStmt[0]); i++){
158145      rbuFinalize(p, aStmt[i]);
158146    }
158147  }
158148}
158149
158150/*
158151** This is a helper function for rbuObjIterCacheTableInfo(). It populates
158152** the pIter->abIndexed[] array.
158153*/
158154static void rbuObjIterCacheIndexedCols(sqlite3rbu *p, RbuObjIter *pIter){
158155  sqlite3_stmt *pList = 0;
158156  int bIndex = 0;
158157
158158  if( p->rc==SQLITE_OK ){
158159    memcpy(pIter->abIndexed, pIter->abTblPk, sizeof(u8)*pIter->nTblCol);
158160    p->rc = prepareFreeAndCollectError(p->dbMain, &pList, &p->zErrmsg,
158161        sqlite3_mprintf("PRAGMA main.index_list = %Q", pIter->zTbl)
158162    );
158163  }
158164
158165  while( p->rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pList) ){
158166    const char *zIdx = (const char*)sqlite3_column_text(pList, 1);
158167    sqlite3_stmt *pXInfo = 0;
158168    if( zIdx==0 ) break;
158169    p->rc = prepareFreeAndCollectError(p->dbMain, &pXInfo, &p->zErrmsg,
158170        sqlite3_mprintf("PRAGMA main.index_xinfo = %Q", zIdx)
158171    );
158172    while( p->rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pXInfo) ){
158173      int iCid = sqlite3_column_int(pXInfo, 1);
158174      if( iCid>=0 ) pIter->abIndexed[iCid] = 1;
158175    }
158176    rbuFinalize(p, pXInfo);
158177    bIndex = 1;
158178  }
158179
158180  rbuFinalize(p, pList);
158181  if( bIndex==0 ) pIter->abIndexed = 0;
158182}
158183
158184
158185/*
158186** If they are not already populated, populate the pIter->azTblCol[],
158187** pIter->abTblPk[], pIter->nTblCol and pIter->bRowid variables according to
158188** the table (not index) that the iterator currently points to.
158189**
158190** Return SQLITE_OK if successful, or an SQLite error code otherwise. If
158191** an error does occur, an error code and error message are also left in
158192** the RBU handle.
158193*/
158194static int rbuObjIterCacheTableInfo(sqlite3rbu *p, RbuObjIter *pIter){
158195  if( pIter->azTblCol==0 ){
158196    sqlite3_stmt *pStmt = 0;
158197    int nCol = 0;
158198    int i;                        /* for() loop iterator variable */
158199    int bRbuRowid = 0;            /* If input table has column "rbu_rowid" */
158200    int iOrder = 0;
158201    int iTnum = 0;
158202
158203    /* Figure out the type of table this step will deal with. */
158204    assert( pIter->eType==0 );
158205    rbuTableType(p, pIter->zTbl, &pIter->eType, &iTnum, &pIter->iPkTnum);
158206    if( p->rc==SQLITE_OK && pIter->eType==RBU_PK_NOTABLE ){
158207      p->rc = SQLITE_ERROR;
158208      p->zErrmsg = sqlite3_mprintf("no such table: %s", pIter->zTbl);
158209    }
158210    if( p->rc ) return p->rc;
158211    if( pIter->zIdx==0 ) pIter->iTnum = iTnum;
158212
158213    assert( pIter->eType==RBU_PK_NONE || pIter->eType==RBU_PK_IPK
158214         || pIter->eType==RBU_PK_EXTERNAL || pIter->eType==RBU_PK_WITHOUT_ROWID
158215         || pIter->eType==RBU_PK_VTAB
158216    );
158217
158218    /* Populate the azTblCol[] and nTblCol variables based on the columns
158219    ** of the input table. Ignore any input table columns that begin with
158220    ** "rbu_".  */
158221    p->rc = prepareFreeAndCollectError(p->dbRbu, &pStmt, &p->zErrmsg,
158222        sqlite3_mprintf("SELECT * FROM 'data_%q'", pIter->zTbl)
158223    );
158224    if( p->rc==SQLITE_OK ){
158225      nCol = sqlite3_column_count(pStmt);
158226      rbuAllocateIterArrays(p, pIter, nCol);
158227    }
158228    for(i=0; p->rc==SQLITE_OK && i<nCol; i++){
158229      const char *zName = (const char*)sqlite3_column_name(pStmt, i);
158230      if( sqlite3_strnicmp("rbu_", zName, 4) ){
158231        char *zCopy = rbuStrndup(zName, &p->rc);
158232        pIter->aiSrcOrder[pIter->nTblCol] = pIter->nTblCol;
158233        pIter->azTblCol[pIter->nTblCol++] = zCopy;
158234      }
158235      else if( 0==sqlite3_stricmp("rbu_rowid", zName) ){
158236        bRbuRowid = 1;
158237      }
158238    }
158239    sqlite3_finalize(pStmt);
158240    pStmt = 0;
158241
158242    if( p->rc==SQLITE_OK
158243     && bRbuRowid!=(pIter->eType==RBU_PK_VTAB || pIter->eType==RBU_PK_NONE)
158244    ){
158245      p->rc = SQLITE_ERROR;
158246      p->zErrmsg = sqlite3_mprintf(
158247          "table data_%q %s rbu_rowid column", pIter->zTbl,
158248          (bRbuRowid ? "may not have" : "requires")
158249      );
158250    }
158251
158252    /* Check that all non-HIDDEN columns in the destination table are also
158253    ** present in the input table. Populate the abTblPk[], azTblType[] and
158254    ** aiTblOrder[] arrays at the same time.  */
158255    if( p->rc==SQLITE_OK ){
158256      p->rc = prepareFreeAndCollectError(p->dbMain, &pStmt, &p->zErrmsg,
158257          sqlite3_mprintf("PRAGMA table_info(%Q)", pIter->zTbl)
158258      );
158259    }
158260    while( p->rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
158261      const char *zName = (const char*)sqlite3_column_text(pStmt, 1);
158262      if( zName==0 ) break;  /* An OOM - finalize() below returns S_NOMEM */
158263      for(i=iOrder; i<pIter->nTblCol; i++){
158264        if( 0==strcmp(zName, pIter->azTblCol[i]) ) break;
158265      }
158266      if( i==pIter->nTblCol ){
158267        p->rc = SQLITE_ERROR;
158268        p->zErrmsg = sqlite3_mprintf("column missing from data_%q: %s",
158269            pIter->zTbl, zName
158270        );
158271      }else{
158272        int iPk = sqlite3_column_int(pStmt, 5);
158273        int bNotNull = sqlite3_column_int(pStmt, 3);
158274        const char *zType = (const char*)sqlite3_column_text(pStmt, 2);
158275
158276        if( i!=iOrder ){
158277          SWAP(int, pIter->aiSrcOrder[i], pIter->aiSrcOrder[iOrder]);
158278          SWAP(char*, pIter->azTblCol[i], pIter->azTblCol[iOrder]);
158279        }
158280
158281        pIter->azTblType[iOrder] = rbuStrndup(zType, &p->rc);
158282        pIter->abTblPk[iOrder] = (iPk!=0);
158283        pIter->abNotNull[iOrder] = (u8)bNotNull || (iPk!=0);
158284        iOrder++;
158285      }
158286    }
158287
158288    rbuFinalize(p, pStmt);
158289    rbuObjIterCacheIndexedCols(p, pIter);
158290    assert( pIter->eType!=RBU_PK_VTAB || pIter->abIndexed==0 );
158291  }
158292
158293  return p->rc;
158294}
158295
158296/*
158297** This function constructs and returns a pointer to a nul-terminated
158298** string containing some SQL clause or list based on one or more of the
158299** column names currently stored in the pIter->azTblCol[] array.
158300*/
158301static char *rbuObjIterGetCollist(
158302  sqlite3rbu *p,                  /* RBU object */
158303  RbuObjIter *pIter               /* Object iterator for column names */
158304){
158305  char *zList = 0;
158306  const char *zSep = "";
158307  int i;
158308  for(i=0; i<pIter->nTblCol; i++){
158309    const char *z = pIter->azTblCol[i];
158310    zList = rbuMPrintf(p, "%z%s\"%w\"", zList, zSep, z);
158311    zSep = ", ";
158312  }
158313  return zList;
158314}
158315
158316/*
158317** This function is used to create a SELECT list (the list of SQL
158318** expressions that follows a SELECT keyword) for a SELECT statement
158319** used to read from an data_xxx or rbu_tmp_xxx table while updating the
158320** index object currently indicated by the iterator object passed as the
158321** second argument. A "PRAGMA index_xinfo = <idxname>" statement is used
158322** to obtain the required information.
158323**
158324** If the index is of the following form:
158325**
158326**   CREATE INDEX i1 ON t1(c, b COLLATE nocase);
158327**
158328** and "t1" is a table with an explicit INTEGER PRIMARY KEY column
158329** "ipk", the returned string is:
158330**
158331**   "`c` COLLATE 'BINARY', `b` COLLATE 'NOCASE', `ipk` COLLATE 'BINARY'"
158332**
158333** As well as the returned string, three other malloc'd strings are
158334** returned via output parameters. As follows:
158335**
158336**   pzImposterCols: ...
158337**   pzImposterPk: ...
158338**   pzWhere: ...
158339*/
158340static char *rbuObjIterGetIndexCols(
158341  sqlite3rbu *p,                  /* RBU object */
158342  RbuObjIter *pIter,              /* Object iterator for column names */
158343  char **pzImposterCols,          /* OUT: Columns for imposter table */
158344  char **pzImposterPk,            /* OUT: Imposter PK clause */
158345  char **pzWhere,                 /* OUT: WHERE clause */
158346  int *pnBind                     /* OUT: Trbul number of columns */
158347){
158348  int rc = p->rc;                 /* Error code */
158349  int rc2;                        /* sqlite3_finalize() return code */
158350  char *zRet = 0;                 /* String to return */
158351  char *zImpCols = 0;             /* String to return via *pzImposterCols */
158352  char *zImpPK = 0;               /* String to return via *pzImposterPK */
158353  char *zWhere = 0;               /* String to return via *pzWhere */
158354  int nBind = 0;                  /* Value to return via *pnBind */
158355  const char *zCom = "";          /* Set to ", " later on */
158356  const char *zAnd = "";          /* Set to " AND " later on */
158357  sqlite3_stmt *pXInfo = 0;       /* PRAGMA index_xinfo = ? */
158358
158359  if( rc==SQLITE_OK ){
158360    assert( p->zErrmsg==0 );
158361    rc = prepareFreeAndCollectError(p->dbMain, &pXInfo, &p->zErrmsg,
158362        sqlite3_mprintf("PRAGMA main.index_xinfo = %Q", pIter->zIdx)
158363    );
158364  }
158365
158366  while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pXInfo) ){
158367    int iCid = sqlite3_column_int(pXInfo, 1);
158368    int bDesc = sqlite3_column_int(pXInfo, 3);
158369    const char *zCollate = (const char*)sqlite3_column_text(pXInfo, 4);
158370    const char *zCol;
158371    const char *zType;
158372
158373    if( iCid<0 ){
158374      /* An integer primary key. If the table has an explicit IPK, use
158375      ** its name. Otherwise, use "rbu_rowid".  */
158376      if( pIter->eType==RBU_PK_IPK ){
158377        int i;
158378        for(i=0; pIter->abTblPk[i]==0; i++);
158379        assert( i<pIter->nTblCol );
158380        zCol = pIter->azTblCol[i];
158381      }else{
158382        zCol = "rbu_rowid";
158383      }
158384      zType = "INTEGER";
158385    }else{
158386      zCol = pIter->azTblCol[iCid];
158387      zType = pIter->azTblType[iCid];
158388    }
158389
158390    zRet = sqlite3_mprintf("%z%s\"%w\" COLLATE %Q", zRet, zCom, zCol, zCollate);
158391    if( pIter->bUnique==0 || sqlite3_column_int(pXInfo, 5) ){
158392      const char *zOrder = (bDesc ? " DESC" : "");
158393      zImpPK = sqlite3_mprintf("%z%s\"rbu_imp_%d%w\"%s",
158394          zImpPK, zCom, nBind, zCol, zOrder
158395      );
158396    }
158397    zImpCols = sqlite3_mprintf("%z%s\"rbu_imp_%d%w\" %s COLLATE %Q",
158398        zImpCols, zCom, nBind, zCol, zType, zCollate
158399    );
158400    zWhere = sqlite3_mprintf(
158401        "%z%s\"rbu_imp_%d%w\" IS ?", zWhere, zAnd, nBind, zCol
158402    );
158403    if( zRet==0 || zImpPK==0 || zImpCols==0 || zWhere==0 ) rc = SQLITE_NOMEM;
158404    zCom = ", ";
158405    zAnd = " AND ";
158406    nBind++;
158407  }
158408
158409  rc2 = sqlite3_finalize(pXInfo);
158410  if( rc==SQLITE_OK ) rc = rc2;
158411
158412  if( rc!=SQLITE_OK ){
158413    sqlite3_free(zRet);
158414    sqlite3_free(zImpCols);
158415    sqlite3_free(zImpPK);
158416    sqlite3_free(zWhere);
158417    zRet = 0;
158418    zImpCols = 0;
158419    zImpPK = 0;
158420    zWhere = 0;
158421    p->rc = rc;
158422  }
158423
158424  *pzImposterCols = zImpCols;
158425  *pzImposterPk = zImpPK;
158426  *pzWhere = zWhere;
158427  *pnBind = nBind;
158428  return zRet;
158429}
158430
158431/*
158432** Assuming the current table columns are "a", "b" and "c", and the zObj
158433** paramter is passed "old", return a string of the form:
158434**
158435**     "old.a, old.b, old.b"
158436**
158437** With the column names escaped.
158438**
158439** For tables with implicit rowids - RBU_PK_EXTERNAL and RBU_PK_NONE, append
158440** the text ", old._rowid_" to the returned value.
158441*/
158442static char *rbuObjIterGetOldlist(
158443  sqlite3rbu *p,
158444  RbuObjIter *pIter,
158445  const char *zObj
158446){
158447  char *zList = 0;
158448  if( p->rc==SQLITE_OK && pIter->abIndexed ){
158449    const char *zS = "";
158450    int i;
158451    for(i=0; i<pIter->nTblCol; i++){
158452      if( pIter->abIndexed[i] ){
158453        const char *zCol = pIter->azTblCol[i];
158454        zList = sqlite3_mprintf("%z%s%s.\"%w\"", zList, zS, zObj, zCol);
158455      }else{
158456        zList = sqlite3_mprintf("%z%sNULL", zList, zS);
158457      }
158458      zS = ", ";
158459      if( zList==0 ){
158460        p->rc = SQLITE_NOMEM;
158461        break;
158462      }
158463    }
158464
158465    /* For a table with implicit rowids, append "old._rowid_" to the list. */
158466    if( pIter->eType==RBU_PK_EXTERNAL || pIter->eType==RBU_PK_NONE ){
158467      zList = rbuMPrintf(p, "%z, %s._rowid_", zList, zObj);
158468    }
158469  }
158470  return zList;
158471}
158472
158473/*
158474** Return an expression that can be used in a WHERE clause to match the
158475** primary key of the current table. For example, if the table is:
158476**
158477**   CREATE TABLE t1(a, b, c, PRIMARY KEY(b, c));
158478**
158479** Return the string:
158480**
158481**   "b = ?1 AND c = ?2"
158482*/
158483static char *rbuObjIterGetWhere(
158484  sqlite3rbu *p,
158485  RbuObjIter *pIter
158486){
158487  char *zList = 0;
158488  if( pIter->eType==RBU_PK_VTAB || pIter->eType==RBU_PK_NONE ){
158489    zList = rbuMPrintf(p, "_rowid_ = ?%d", pIter->nTblCol+1);
158490  }else if( pIter->eType==RBU_PK_EXTERNAL ){
158491    const char *zSep = "";
158492    int i;
158493    for(i=0; i<pIter->nTblCol; i++){
158494      if( pIter->abTblPk[i] ){
158495        zList = rbuMPrintf(p, "%z%sc%d=?%d", zList, zSep, i, i+1);
158496        zSep = " AND ";
158497      }
158498    }
158499    zList = rbuMPrintf(p,
158500        "_rowid_ = (SELECT id FROM rbu_imposter2 WHERE %z)", zList
158501    );
158502
158503  }else{
158504    const char *zSep = "";
158505    int i;
158506    for(i=0; i<pIter->nTblCol; i++){
158507      if( pIter->abTblPk[i] ){
158508        const char *zCol = pIter->azTblCol[i];
158509        zList = rbuMPrintf(p, "%z%s\"%w\"=?%d", zList, zSep, zCol, i+1);
158510        zSep = " AND ";
158511      }
158512    }
158513  }
158514  return zList;
158515}
158516
158517/*
158518** The SELECT statement iterating through the keys for the current object
158519** (p->objiter.pSelect) currently points to a valid row. However, there
158520** is something wrong with the rbu_control value in the rbu_control value
158521** stored in the (p->nCol+1)'th column. Set the error code and error message
158522** of the RBU handle to something reflecting this.
158523*/
158524static void rbuBadControlError(sqlite3rbu *p){
158525  p->rc = SQLITE_ERROR;
158526  p->zErrmsg = sqlite3_mprintf("invalid rbu_control value");
158527}
158528
158529
158530/*
158531** Return a nul-terminated string containing the comma separated list of
158532** assignments that should be included following the "SET" keyword of
158533** an UPDATE statement used to update the table object that the iterator
158534** passed as the second argument currently points to if the rbu_control
158535** column of the data_xxx table entry is set to zMask.
158536**
158537** The memory for the returned string is obtained from sqlite3_malloc().
158538** It is the responsibility of the caller to eventually free it using
158539** sqlite3_free().
158540**
158541** If an OOM error is encountered when allocating space for the new
158542** string, an error code is left in the rbu handle passed as the first
158543** argument and NULL is returned. Or, if an error has already occurred
158544** when this function is called, NULL is returned immediately, without
158545** attempting the allocation or modifying the stored error code.
158546*/
158547static char *rbuObjIterGetSetlist(
158548  sqlite3rbu *p,
158549  RbuObjIter *pIter,
158550  const char *zMask
158551){
158552  char *zList = 0;
158553  if( p->rc==SQLITE_OK ){
158554    int i;
158555
158556    if( strlen(zMask)!=pIter->nTblCol ){
158557      rbuBadControlError(p);
158558    }else{
158559      const char *zSep = "";
158560      for(i=0; i<pIter->nTblCol; i++){
158561        char c = zMask[pIter->aiSrcOrder[i]];
158562        if( c=='x' ){
158563          zList = rbuMPrintf(p, "%z%s\"%w\"=?%d",
158564              zList, zSep, pIter->azTblCol[i], i+1
158565          );
158566          zSep = ", ";
158567        }
158568        if( c=='d' ){
158569          zList = rbuMPrintf(p, "%z%s\"%w\"=rbu_delta(\"%w\", ?%d)",
158570              zList, zSep, pIter->azTblCol[i], pIter->azTblCol[i], i+1
158571          );
158572          zSep = ", ";
158573        }
158574      }
158575    }
158576  }
158577  return zList;
158578}
158579
158580/*
158581** Return a nul-terminated string consisting of nByte comma separated
158582** "?" expressions. For example, if nByte is 3, return a pointer to
158583** a buffer containing the string "?,?,?".
158584**
158585** The memory for the returned string is obtained from sqlite3_malloc().
158586** It is the responsibility of the caller to eventually free it using
158587** sqlite3_free().
158588**
158589** If an OOM error is encountered when allocating space for the new
158590** string, an error code is left in the rbu handle passed as the first
158591** argument and NULL is returned. Or, if an error has already occurred
158592** when this function is called, NULL is returned immediately, without
158593** attempting the allocation or modifying the stored error code.
158594*/
158595static char *rbuObjIterGetBindlist(sqlite3rbu *p, int nBind){
158596  char *zRet = 0;
158597  int nByte = nBind*2 + 1;
158598
158599  zRet = (char*)rbuMalloc(p, nByte);
158600  if( zRet ){
158601    int i;
158602    for(i=0; i<nBind; i++){
158603      zRet[i*2] = '?';
158604      zRet[i*2+1] = (i+1==nBind) ? '\0' : ',';
158605    }
158606  }
158607  return zRet;
158608}
158609
158610/*
158611** The iterator currently points to a table (not index) of type
158612** RBU_PK_WITHOUT_ROWID. This function creates the PRIMARY KEY
158613** declaration for the corresponding imposter table. For example,
158614** if the iterator points to a table created as:
158615**
158616**   CREATE TABLE t1(a, b, c, PRIMARY KEY(b, a DESC)) WITHOUT ROWID
158617**
158618** this function returns:
158619**
158620**   PRIMARY KEY("b", "a" DESC)
158621*/
158622static char *rbuWithoutRowidPK(sqlite3rbu *p, RbuObjIter *pIter){
158623  char *z = 0;
158624  assert( pIter->zIdx==0 );
158625  if( p->rc==SQLITE_OK ){
158626    const char *zSep = "PRIMARY KEY(";
158627    sqlite3_stmt *pXList = 0;     /* PRAGMA index_list = (pIter->zTbl) */
158628    sqlite3_stmt *pXInfo = 0;     /* PRAGMA index_xinfo = <pk-index> */
158629
158630    p->rc = prepareFreeAndCollectError(p->dbMain, &pXList, &p->zErrmsg,
158631        sqlite3_mprintf("PRAGMA main.index_list = %Q", pIter->zTbl)
158632    );
158633    while( p->rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pXList) ){
158634      const char *zOrig = (const char*)sqlite3_column_text(pXList,3);
158635      if( zOrig && strcmp(zOrig, "pk")==0 ){
158636        const char *zIdx = (const char*)sqlite3_column_text(pXList,1);
158637        if( zIdx ){
158638          p->rc = prepareFreeAndCollectError(p->dbMain, &pXInfo, &p->zErrmsg,
158639              sqlite3_mprintf("PRAGMA main.index_xinfo = %Q", zIdx)
158640          );
158641        }
158642        break;
158643      }
158644    }
158645    rbuFinalize(p, pXList);
158646
158647    while( p->rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pXInfo) ){
158648      if( sqlite3_column_int(pXInfo, 5) ){
158649        /* int iCid = sqlite3_column_int(pXInfo, 0); */
158650        const char *zCol = (const char*)sqlite3_column_text(pXInfo, 2);
158651        const char *zDesc = sqlite3_column_int(pXInfo, 3) ? " DESC" : "";
158652        z = rbuMPrintf(p, "%z%s\"%w\"%s", z, zSep, zCol, zDesc);
158653        zSep = ", ";
158654      }
158655    }
158656    z = rbuMPrintf(p, "%z)", z);
158657    rbuFinalize(p, pXInfo);
158658  }
158659  return z;
158660}
158661
158662/*
158663** This function creates the second imposter table used when writing to
158664** a table b-tree where the table has an external primary key. If the
158665** iterator passed as the second argument does not currently point to
158666** a table (not index) with an external primary key, this function is a
158667** no-op.
158668**
158669** Assuming the iterator does point to a table with an external PK, this
158670** function creates a WITHOUT ROWID imposter table named "rbu_imposter2"
158671** used to access that PK index. For example, if the target table is
158672** declared as follows:
158673**
158674**   CREATE TABLE t1(a, b TEXT, c REAL, PRIMARY KEY(b, c));
158675**
158676** then the imposter table schema is:
158677**
158678**   CREATE TABLE rbu_imposter2(c1 TEXT, c2 REAL, id INTEGER) WITHOUT ROWID;
158679**
158680*/
158681static void rbuCreateImposterTable2(sqlite3rbu *p, RbuObjIter *pIter){
158682  if( p->rc==SQLITE_OK && pIter->eType==RBU_PK_EXTERNAL ){
158683    int tnum = pIter->iPkTnum;    /* Root page of PK index */
158684    sqlite3_stmt *pQuery = 0;     /* SELECT name ... WHERE rootpage = $tnum */
158685    const char *zIdx = 0;         /* Name of PK index */
158686    sqlite3_stmt *pXInfo = 0;     /* PRAGMA main.index_xinfo = $zIdx */
158687    const char *zComma = "";
158688    char *zCols = 0;              /* Used to build up list of table cols */
158689    char *zPk = 0;                /* Used to build up table PK declaration */
158690
158691    /* Figure out the name of the primary key index for the current table.
158692    ** This is needed for the argument to "PRAGMA index_xinfo". Set
158693    ** zIdx to point to a nul-terminated string containing this name. */
158694    p->rc = prepareAndCollectError(p->dbMain, &pQuery, &p->zErrmsg,
158695        "SELECT name FROM sqlite_master WHERE rootpage = ?"
158696    );
158697    if( p->rc==SQLITE_OK ){
158698      sqlite3_bind_int(pQuery, 1, tnum);
158699      if( SQLITE_ROW==sqlite3_step(pQuery) ){
158700        zIdx = (const char*)sqlite3_column_text(pQuery, 0);
158701      }
158702    }
158703    if( zIdx ){
158704      p->rc = prepareFreeAndCollectError(p->dbMain, &pXInfo, &p->zErrmsg,
158705          sqlite3_mprintf("PRAGMA main.index_xinfo = %Q", zIdx)
158706      );
158707    }
158708    rbuFinalize(p, pQuery);
158709
158710    while( p->rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pXInfo) ){
158711      int bKey = sqlite3_column_int(pXInfo, 5);
158712      if( bKey ){
158713        int iCid = sqlite3_column_int(pXInfo, 1);
158714        int bDesc = sqlite3_column_int(pXInfo, 3);
158715        const char *zCollate = (const char*)sqlite3_column_text(pXInfo, 4);
158716        zCols = rbuMPrintf(p, "%z%sc%d %s COLLATE %s", zCols, zComma,
158717            iCid, pIter->azTblType[iCid], zCollate
158718        );
158719        zPk = rbuMPrintf(p, "%z%sc%d%s", zPk, zComma, iCid, bDesc?" DESC":"");
158720        zComma = ", ";
158721      }
158722    }
158723    zCols = rbuMPrintf(p, "%z, id INTEGER", zCols);
158724    rbuFinalize(p, pXInfo);
158725
158726    sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->dbMain, "main", 1, tnum);
158727    rbuMPrintfExec(p, p->dbMain,
158728        "CREATE TABLE rbu_imposter2(%z, PRIMARY KEY(%z)) WITHOUT ROWID",
158729        zCols, zPk
158730    );
158731    sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->dbMain, "main", 0, 0);
158732  }
158733}
158734
158735/*
158736** If an error has already occurred when this function is called, it
158737** immediately returns zero (without doing any work). Or, if an error
158738** occurs during the execution of this function, it sets the error code
158739** in the sqlite3rbu object indicated by the first argument and returns
158740** zero.
158741**
158742** The iterator passed as the second argument is guaranteed to point to
158743** a table (not an index) when this function is called. This function
158744** attempts to create any imposter table required to write to the main
158745** table b-tree of the table before returning. Non-zero is returned if
158746** an imposter table are created, or zero otherwise.
158747**
158748** An imposter table is required in all cases except RBU_PK_VTAB. Only
158749** virtual tables are written to directly. The imposter table has the
158750** same schema as the actual target table (less any UNIQUE constraints).
158751** More precisely, the "same schema" means the same columns, types,
158752** collation sequences. For tables that do not have an external PRIMARY
158753** KEY, it also means the same PRIMARY KEY declaration.
158754*/
158755static void rbuCreateImposterTable(sqlite3rbu *p, RbuObjIter *pIter){
158756  if( p->rc==SQLITE_OK && pIter->eType!=RBU_PK_VTAB ){
158757    int tnum = pIter->iTnum;
158758    const char *zComma = "";
158759    char *zSql = 0;
158760    int iCol;
158761    sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->dbMain, "main", 0, 1);
158762
158763    for(iCol=0; p->rc==SQLITE_OK && iCol<pIter->nTblCol; iCol++){
158764      const char *zPk = "";
158765      const char *zCol = pIter->azTblCol[iCol];
158766      const char *zColl = 0;
158767
158768      p->rc = sqlite3_table_column_metadata(
158769          p->dbMain, "main", pIter->zTbl, zCol, 0, &zColl, 0, 0, 0
158770      );
158771
158772      if( pIter->eType==RBU_PK_IPK && pIter->abTblPk[iCol] ){
158773        /* If the target table column is an "INTEGER PRIMARY KEY", add
158774        ** "PRIMARY KEY" to the imposter table column declaration. */
158775        zPk = "PRIMARY KEY ";
158776      }
158777      zSql = rbuMPrintf(p, "%z%s\"%w\" %s %sCOLLATE %s%s",
158778          zSql, zComma, zCol, pIter->azTblType[iCol], zPk, zColl,
158779          (pIter->abNotNull[iCol] ? " NOT NULL" : "")
158780      );
158781      zComma = ", ";
158782    }
158783
158784    if( pIter->eType==RBU_PK_WITHOUT_ROWID ){
158785      char *zPk = rbuWithoutRowidPK(p, pIter);
158786      if( zPk ){
158787        zSql = rbuMPrintf(p, "%z, %z", zSql, zPk);
158788      }
158789    }
158790
158791    sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->dbMain, "main", 1, tnum);
158792    rbuMPrintfExec(p, p->dbMain, "CREATE TABLE \"rbu_imp_%w\"(%z)%s",
158793        pIter->zTbl, zSql,
158794        (pIter->eType==RBU_PK_WITHOUT_ROWID ? " WITHOUT ROWID" : "")
158795    );
158796    sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->dbMain, "main", 0, 0);
158797  }
158798}
158799
158800/*
158801** Prepare a statement used to insert rows into the "rbu_tmp_xxx" table.
158802** Specifically a statement of the form:
158803**
158804**     INSERT INTO rbu_tmp_xxx VALUES(?, ?, ? ...);
158805**
158806** The number of bound variables is equal to the number of columns in
158807** the target table, plus one (for the rbu_control column), plus one more
158808** (for the rbu_rowid column) if the target table is an implicit IPK or
158809** virtual table.
158810*/
158811static void rbuObjIterPrepareTmpInsert(
158812  sqlite3rbu *p,
158813  RbuObjIter *pIter,
158814  const char *zCollist,
158815  const char *zRbuRowid
158816){
158817  int bRbuRowid = (pIter->eType==RBU_PK_EXTERNAL || pIter->eType==RBU_PK_NONE);
158818  char *zBind = rbuObjIterGetBindlist(p, pIter->nTblCol + 1 + bRbuRowid);
158819  if( zBind ){
158820    assert( pIter->pTmpInsert==0 );
158821    p->rc = prepareFreeAndCollectError(
158822        p->dbRbu, &pIter->pTmpInsert, &p->zErrmsg, sqlite3_mprintf(
158823          "INSERT INTO %s.'rbu_tmp_%q'(rbu_control,%s%s) VALUES(%z)",
158824          p->zStateDb, pIter->zTbl, zCollist, zRbuRowid, zBind
158825    ));
158826  }
158827}
158828
158829static void rbuTmpInsertFunc(
158830  sqlite3_context *pCtx,
158831  int nVal,
158832  sqlite3_value **apVal
158833){
158834  sqlite3rbu *p = sqlite3_user_data(pCtx);
158835  int rc = SQLITE_OK;
158836  int i;
158837
158838  for(i=0; rc==SQLITE_OK && i<nVal; i++){
158839    rc = sqlite3_bind_value(p->objiter.pTmpInsert, i+1, apVal[i]);
158840  }
158841  if( rc==SQLITE_OK ){
158842    sqlite3_step(p->objiter.pTmpInsert);
158843    rc = sqlite3_reset(p->objiter.pTmpInsert);
158844  }
158845
158846  if( rc!=SQLITE_OK ){
158847    sqlite3_result_error_code(pCtx, rc);
158848  }
158849}
158850
158851/*
158852** Ensure that the SQLite statement handles required to update the
158853** target database object currently indicated by the iterator passed
158854** as the second argument are available.
158855*/
158856static int rbuObjIterPrepareAll(
158857  sqlite3rbu *p,
158858  RbuObjIter *pIter,
158859  int nOffset                     /* Add "LIMIT -1 OFFSET $nOffset" to SELECT */
158860){
158861  assert( pIter->bCleanup==0 );
158862  if( pIter->pSelect==0 && rbuObjIterCacheTableInfo(p, pIter)==SQLITE_OK ){
158863    const int tnum = pIter->iTnum;
158864    char *zCollist = 0;           /* List of indexed columns */
158865    char **pz = &p->zErrmsg;
158866    const char *zIdx = pIter->zIdx;
158867    char *zLimit = 0;
158868
158869    if( nOffset ){
158870      zLimit = sqlite3_mprintf(" LIMIT -1 OFFSET %d", nOffset);
158871      if( !zLimit ) p->rc = SQLITE_NOMEM;
158872    }
158873
158874    if( zIdx ){
158875      const char *zTbl = pIter->zTbl;
158876      char *zImposterCols = 0;    /* Columns for imposter table */
158877      char *zImposterPK = 0;      /* Primary key declaration for imposter */
158878      char *zWhere = 0;           /* WHERE clause on PK columns */
158879      char *zBind = 0;
158880      int nBind = 0;
158881
158882      assert( pIter->eType!=RBU_PK_VTAB );
158883      zCollist = rbuObjIterGetIndexCols(
158884          p, pIter, &zImposterCols, &zImposterPK, &zWhere, &nBind
158885      );
158886      zBind = rbuObjIterGetBindlist(p, nBind);
158887
158888      /* Create the imposter table used to write to this index. */
158889      sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->dbMain, "main", 0, 1);
158890      sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->dbMain, "main", 1,tnum);
158891      rbuMPrintfExec(p, p->dbMain,
158892          "CREATE TABLE \"rbu_imp_%w\"( %s, PRIMARY KEY( %s ) ) WITHOUT ROWID",
158893          zTbl, zImposterCols, zImposterPK
158894      );
158895      sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->dbMain, "main", 0, 0);
158896
158897      /* Create the statement to insert index entries */
158898      pIter->nCol = nBind;
158899      if( p->rc==SQLITE_OK ){
158900        p->rc = prepareFreeAndCollectError(
158901            p->dbMain, &pIter->pInsert, &p->zErrmsg,
158902          sqlite3_mprintf("INSERT INTO \"rbu_imp_%w\" VALUES(%s)", zTbl, zBind)
158903        );
158904      }
158905
158906      /* And to delete index entries */
158907      if( p->rc==SQLITE_OK ){
158908        p->rc = prepareFreeAndCollectError(
158909            p->dbMain, &pIter->pDelete, &p->zErrmsg,
158910          sqlite3_mprintf("DELETE FROM \"rbu_imp_%w\" WHERE %s", zTbl, zWhere)
158911        );
158912      }
158913
158914      /* Create the SELECT statement to read keys in sorted order */
158915      if( p->rc==SQLITE_OK ){
158916        char *zSql;
158917        if( pIter->eType==RBU_PK_EXTERNAL || pIter->eType==RBU_PK_NONE ){
158918          zSql = sqlite3_mprintf(
158919              "SELECT %s, rbu_control FROM %s.'rbu_tmp_%q' ORDER BY %s%s",
158920              zCollist, p->zStateDb, pIter->zTbl,
158921              zCollist, zLimit
158922          );
158923        }else{
158924          zSql = sqlite3_mprintf(
158925              "SELECT %s, rbu_control FROM 'data_%q' "
158926              "WHERE typeof(rbu_control)='integer' AND rbu_control!=1 "
158927              "UNION ALL "
158928              "SELECT %s, rbu_control FROM %s.'rbu_tmp_%q' "
158929              "ORDER BY %s%s",
158930              zCollist, pIter->zTbl,
158931              zCollist, p->zStateDb, pIter->zTbl,
158932              zCollist, zLimit
158933          );
158934        }
158935        p->rc = prepareFreeAndCollectError(p->dbRbu, &pIter->pSelect, pz, zSql);
158936      }
158937
158938      sqlite3_free(zImposterCols);
158939      sqlite3_free(zImposterPK);
158940      sqlite3_free(zWhere);
158941      sqlite3_free(zBind);
158942    }else{
158943      int bRbuRowid = (pIter->eType==RBU_PK_VTAB || pIter->eType==RBU_PK_NONE);
158944      const char *zTbl = pIter->zTbl;       /* Table this step applies to */
158945      const char *zWrite;                   /* Imposter table name */
158946
158947      char *zBindings = rbuObjIterGetBindlist(p, pIter->nTblCol + bRbuRowid);
158948      char *zWhere = rbuObjIterGetWhere(p, pIter);
158949      char *zOldlist = rbuObjIterGetOldlist(p, pIter, "old");
158950      char *zNewlist = rbuObjIterGetOldlist(p, pIter, "new");
158951
158952      zCollist = rbuObjIterGetCollist(p, pIter);
158953      pIter->nCol = pIter->nTblCol;
158954
158955      /* Create the SELECT statement to read keys from data_xxx */
158956      if( p->rc==SQLITE_OK ){
158957        p->rc = prepareFreeAndCollectError(p->dbRbu, &pIter->pSelect, pz,
158958            sqlite3_mprintf(
158959              "SELECT %s, rbu_control%s FROM 'data_%q'%s",
158960              zCollist, (bRbuRowid ? ", rbu_rowid" : ""), zTbl, zLimit
158961            )
158962        );
158963      }
158964
158965      /* Create the imposter table or tables (if required). */
158966      rbuCreateImposterTable(p, pIter);
158967      rbuCreateImposterTable2(p, pIter);
158968      zWrite = (pIter->eType==RBU_PK_VTAB ? "" : "rbu_imp_");
158969
158970      /* Create the INSERT statement to write to the target PK b-tree */
158971      if( p->rc==SQLITE_OK ){
158972        p->rc = prepareFreeAndCollectError(p->dbMain, &pIter->pInsert, pz,
158973            sqlite3_mprintf(
158974              "INSERT INTO \"%s%w\"(%s%s) VALUES(%s)",
158975              zWrite, zTbl, zCollist, (bRbuRowid ? ", _rowid_" : ""), zBindings
158976            )
158977        );
158978      }
158979
158980      /* Create the DELETE statement to write to the target PK b-tree */
158981      if( p->rc==SQLITE_OK ){
158982        p->rc = prepareFreeAndCollectError(p->dbMain, &pIter->pDelete, pz,
158983            sqlite3_mprintf(
158984              "DELETE FROM \"%s%w\" WHERE %s", zWrite, zTbl, zWhere
158985            )
158986        );
158987      }
158988
158989      if( pIter->abIndexed ){
158990        const char *zRbuRowid = "";
158991        if( pIter->eType==RBU_PK_EXTERNAL || pIter->eType==RBU_PK_NONE ){
158992          zRbuRowid = ", rbu_rowid";
158993        }
158994
158995        /* Create the rbu_tmp_xxx table and the triggers to populate it. */
158996        rbuMPrintfExec(p, p->dbRbu,
158997            "CREATE TABLE IF NOT EXISTS %s.'rbu_tmp_%q' AS "
158998            "SELECT *%s FROM 'data_%q' WHERE 0;"
158999            , p->zStateDb
159000            , zTbl, (pIter->eType==RBU_PK_EXTERNAL ? ", 0 AS rbu_rowid" : "")
159001            , zTbl
159002        );
159003
159004        rbuMPrintfExec(p, p->dbMain,
159005            "CREATE TEMP TRIGGER rbu_delete_tr BEFORE DELETE ON \"%s%w\" "
159006            "BEGIN "
159007            "  SELECT rbu_tmp_insert(2, %s);"
159008            "END;"
159009
159010            "CREATE TEMP TRIGGER rbu_update1_tr BEFORE UPDATE ON \"%s%w\" "
159011            "BEGIN "
159012            "  SELECT rbu_tmp_insert(2, %s);"
159013            "END;"
159014
159015            "CREATE TEMP TRIGGER rbu_update2_tr AFTER UPDATE ON \"%s%w\" "
159016            "BEGIN "
159017            "  SELECT rbu_tmp_insert(3, %s);"
159018            "END;",
159019            zWrite, zTbl, zOldlist,
159020            zWrite, zTbl, zOldlist,
159021            zWrite, zTbl, zNewlist
159022        );
159023
159024        if( pIter->eType==RBU_PK_EXTERNAL || pIter->eType==RBU_PK_NONE ){
159025          rbuMPrintfExec(p, p->dbMain,
159026              "CREATE TEMP TRIGGER rbu_insert_tr AFTER INSERT ON \"%s%w\" "
159027              "BEGIN "
159028              "  SELECT rbu_tmp_insert(0, %s);"
159029              "END;",
159030              zWrite, zTbl, zNewlist
159031          );
159032        }
159033
159034        rbuObjIterPrepareTmpInsert(p, pIter, zCollist, zRbuRowid);
159035      }
159036
159037      sqlite3_free(zWhere);
159038      sqlite3_free(zOldlist);
159039      sqlite3_free(zNewlist);
159040      sqlite3_free(zBindings);
159041    }
159042    sqlite3_free(zCollist);
159043    sqlite3_free(zLimit);
159044  }
159045
159046  return p->rc;
159047}
159048
159049/*
159050** Set output variable *ppStmt to point to an UPDATE statement that may
159051** be used to update the imposter table for the main table b-tree of the
159052** table object that pIter currently points to, assuming that the
159053** rbu_control column of the data_xyz table contains zMask.
159054**
159055** If the zMask string does not specify any columns to update, then this
159056** is not an error. Output variable *ppStmt is set to NULL in this case.
159057*/
159058static int rbuGetUpdateStmt(
159059  sqlite3rbu *p,                  /* RBU handle */
159060  RbuObjIter *pIter,              /* Object iterator */
159061  const char *zMask,              /* rbu_control value ('x.x.') */
159062  sqlite3_stmt **ppStmt           /* OUT: UPDATE statement handle */
159063){
159064  RbuUpdateStmt **pp;
159065  RbuUpdateStmt *pUp = 0;
159066  int nUp = 0;
159067
159068  /* In case an error occurs */
159069  *ppStmt = 0;
159070
159071  /* Search for an existing statement. If one is found, shift it to the front
159072  ** of the LRU queue and return immediately. Otherwise, leave nUp pointing
159073  ** to the number of statements currently in the cache and pUp to the
159074  ** last object in the list.  */
159075  for(pp=&pIter->pRbuUpdate; *pp; pp=&((*pp)->pNext)){
159076    pUp = *pp;
159077    if( strcmp(pUp->zMask, zMask)==0 ){
159078      *pp = pUp->pNext;
159079      pUp->pNext = pIter->pRbuUpdate;
159080      pIter->pRbuUpdate = pUp;
159081      *ppStmt = pUp->pUpdate;
159082      return SQLITE_OK;
159083    }
159084    nUp++;
159085  }
159086  assert( pUp==0 || pUp->pNext==0 );
159087
159088  if( nUp>=SQLITE_RBU_UPDATE_CACHESIZE ){
159089    for(pp=&pIter->pRbuUpdate; *pp!=pUp; pp=&((*pp)->pNext));
159090    *pp = 0;
159091    sqlite3_finalize(pUp->pUpdate);
159092    pUp->pUpdate = 0;
159093  }else{
159094    pUp = (RbuUpdateStmt*)rbuMalloc(p, sizeof(RbuUpdateStmt)+pIter->nTblCol+1);
159095  }
159096
159097  if( pUp ){
159098    char *zWhere = rbuObjIterGetWhere(p, pIter);
159099    char *zSet = rbuObjIterGetSetlist(p, pIter, zMask);
159100    char *zUpdate = 0;
159101
159102    pUp->zMask = (char*)&pUp[1];
159103    memcpy(pUp->zMask, zMask, pIter->nTblCol);
159104    pUp->pNext = pIter->pRbuUpdate;
159105    pIter->pRbuUpdate = pUp;
159106
159107    if( zSet ){
159108      const char *zPrefix = "";
159109
159110      if( pIter->eType!=RBU_PK_VTAB ) zPrefix = "rbu_imp_";
159111      zUpdate = sqlite3_mprintf("UPDATE \"%s%w\" SET %s WHERE %s",
159112          zPrefix, pIter->zTbl, zSet, zWhere
159113      );
159114      p->rc = prepareFreeAndCollectError(
159115          p->dbMain, &pUp->pUpdate, &p->zErrmsg, zUpdate
159116      );
159117      *ppStmt = pUp->pUpdate;
159118    }
159119    sqlite3_free(zWhere);
159120    sqlite3_free(zSet);
159121  }
159122
159123  return p->rc;
159124}
159125
159126static sqlite3 *rbuOpenDbhandle(sqlite3rbu *p, const char *zName){
159127  sqlite3 *db = 0;
159128  if( p->rc==SQLITE_OK ){
159129    const int flags = SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|SQLITE_OPEN_URI;
159130    p->rc = sqlite3_open_v2(zName, &db, flags, p->zVfsName);
159131    if( p->rc ){
159132      p->zErrmsg = sqlite3_mprintf("%s", sqlite3_errmsg(db));
159133      sqlite3_close(db);
159134      db = 0;
159135    }
159136  }
159137  return db;
159138}
159139
159140/*
159141** Open the database handle and attach the RBU database as "rbu". If an
159142** error occurs, leave an error code and message in the RBU handle.
159143*/
159144static void rbuOpenDatabase(sqlite3rbu *p){
159145  assert( p->rc==SQLITE_OK );
159146  assert( p->dbMain==0 && p->dbRbu==0 );
159147
159148  p->eStage = 0;
159149  p->dbMain = rbuOpenDbhandle(p, p->zTarget);
159150  p->dbRbu = rbuOpenDbhandle(p, p->zRbu);
159151
159152  /* If using separate RBU and state databases, attach the state database to
159153  ** the RBU db handle now.  */
159154  if( p->zState ){
159155    rbuMPrintfExec(p, p->dbRbu, "ATTACH %Q AS stat", p->zState);
159156    memcpy(p->zStateDb, "stat", 4);
159157  }else{
159158    memcpy(p->zStateDb, "main", 4);
159159  }
159160
159161  if( p->rc==SQLITE_OK ){
159162    p->rc = sqlite3_create_function(p->dbMain,
159163        "rbu_tmp_insert", -1, SQLITE_UTF8, (void*)p, rbuTmpInsertFunc, 0, 0
159164    );
159165  }
159166
159167  if( p->rc==SQLITE_OK ){
159168    p->rc = sqlite3_file_control(p->dbMain, "main", SQLITE_FCNTL_RBU, (void*)p);
159169  }
159170  rbuMPrintfExec(p, p->dbMain, "SELECT * FROM sqlite_master");
159171
159172  /* Mark the database file just opened as an RBU target database. If
159173  ** this call returns SQLITE_NOTFOUND, then the RBU vfs is not in use.
159174  ** This is an error.  */
159175  if( p->rc==SQLITE_OK ){
159176    p->rc = sqlite3_file_control(p->dbMain, "main", SQLITE_FCNTL_RBU, (void*)p);
159177  }
159178
159179  if( p->rc==SQLITE_NOTFOUND ){
159180    p->rc = SQLITE_ERROR;
159181    p->zErrmsg = sqlite3_mprintf("rbu vfs not found");
159182  }
159183}
159184
159185/*
159186** This routine is a copy of the sqlite3FileSuffix3() routine from the core.
159187** It is a no-op unless SQLITE_ENABLE_8_3_NAMES is defined.
159188**
159189** If SQLITE_ENABLE_8_3_NAMES is set at compile-time and if the database
159190** filename in zBaseFilename is a URI with the "8_3_names=1" parameter and
159191** if filename in z[] has a suffix (a.k.a. "extension") that is longer than
159192** three characters, then shorten the suffix on z[] to be the last three
159193** characters of the original suffix.
159194**
159195** If SQLITE_ENABLE_8_3_NAMES is set to 2 at compile-time, then always
159196** do the suffix shortening regardless of URI parameter.
159197**
159198** Examples:
159199**
159200**     test.db-journal    =>   test.nal
159201**     test.db-wal        =>   test.wal
159202**     test.db-shm        =>   test.shm
159203**     test.db-mj7f3319fa =>   test.9fa
159204*/
159205static void rbuFileSuffix3(const char *zBase, char *z){
159206#ifdef SQLITE_ENABLE_8_3_NAMES
159207#if SQLITE_ENABLE_8_3_NAMES<2
159208  if( sqlite3_uri_boolean(zBase, "8_3_names", 0) )
159209#endif
159210  {
159211    int i, sz;
159212    sz = sqlite3Strlen30(z);
159213    for(i=sz-1; i>0 && z[i]!='/' && z[i]!='.'; i--){}
159214    if( z[i]=='.' && ALWAYS(sz>i+4) ) memmove(&z[i+1], &z[sz-3], 4);
159215  }
159216#endif
159217}
159218
159219/*
159220** Return the current wal-index header checksum for the target database
159221** as a 64-bit integer.
159222**
159223** The checksum is store in the first page of xShmMap memory as an 8-byte
159224** blob starting at byte offset 40.
159225*/
159226static i64 rbuShmChecksum(sqlite3rbu *p){
159227  i64 iRet = 0;
159228  if( p->rc==SQLITE_OK ){
159229    sqlite3_file *pDb = p->pTargetFd->pReal;
159230    u32 volatile *ptr;
159231    p->rc = pDb->pMethods->xShmMap(pDb, 0, 32*1024, 0, (void volatile**)&ptr);
159232    if( p->rc==SQLITE_OK ){
159233      iRet = ((i64)ptr[10] << 32) + ptr[11];
159234    }
159235  }
159236  return iRet;
159237}
159238
159239/*
159240** This function is called as part of initializing or reinitializing an
159241** incremental checkpoint.
159242**
159243** It populates the sqlite3rbu.aFrame[] array with the set of
159244** (wal frame -> db page) copy operations required to checkpoint the
159245** current wal file, and obtains the set of shm locks required to safely
159246** perform the copy operations directly on the file-system.
159247**
159248** If argument pState is not NULL, then the incremental checkpoint is
159249** being resumed. In this case, if the checksum of the wal-index-header
159250** following recovery is not the same as the checksum saved in the RbuState
159251** object, then the rbu handle is set to DONE state. This occurs if some
159252** other client appends a transaction to the wal file in the middle of
159253** an incremental checkpoint.
159254*/
159255static void rbuSetupCheckpoint(sqlite3rbu *p, RbuState *pState){
159256
159257  /* If pState is NULL, then the wal file may not have been opened and
159258  ** recovered. Running a read-statement here to ensure that doing so
159259  ** does not interfere with the "capture" process below.  */
159260  if( pState==0 ){
159261    p->eStage = 0;
159262    if( p->rc==SQLITE_OK ){
159263      p->rc = sqlite3_exec(p->dbMain, "SELECT * FROM sqlite_master", 0, 0, 0);
159264    }
159265  }
159266
159267  /* Assuming no error has occurred, run a "restart" checkpoint with the
159268  ** sqlite3rbu.eStage variable set to CAPTURE. This turns on the following
159269  ** special behaviour in the rbu VFS:
159270  **
159271  **   * If the exclusive shm WRITER or READ0 lock cannot be obtained,
159272  **     the checkpoint fails with SQLITE_BUSY (normally SQLite would
159273  **     proceed with running a passive checkpoint instead of failing).
159274  **
159275  **   * Attempts to read from the *-wal file or write to the database file
159276  **     do not perform any IO. Instead, the frame/page combinations that
159277  **     would be read/written are recorded in the sqlite3rbu.aFrame[]
159278  **     array.
159279  **
159280  **   * Calls to xShmLock(UNLOCK) to release the exclusive shm WRITER,
159281  **     READ0 and CHECKPOINT locks taken as part of the checkpoint are
159282  **     no-ops. These locks will not be released until the connection
159283  **     is closed.
159284  **
159285  **   * Attempting to xSync() the database file causes an SQLITE_INTERNAL
159286  **     error.
159287  **
159288  ** As a result, unless an error (i.e. OOM or SQLITE_BUSY) occurs, the
159289  ** checkpoint below fails with SQLITE_INTERNAL, and leaves the aFrame[]
159290  ** array populated with a set of (frame -> page) mappings. Because the
159291  ** WRITER, CHECKPOINT and READ0 locks are still held, it is safe to copy
159292  ** data from the wal file into the database file according to the
159293  ** contents of aFrame[].
159294  */
159295  if( p->rc==SQLITE_OK ){
159296    int rc2;
159297    p->eStage = RBU_STAGE_CAPTURE;
159298    rc2 = sqlite3_exec(p->dbMain, "PRAGMA main.wal_checkpoint=restart", 0, 0,0);
159299    if( rc2!=SQLITE_INTERNAL ) p->rc = rc2;
159300  }
159301
159302  if( p->rc==SQLITE_OK ){
159303    p->eStage = RBU_STAGE_CKPT;
159304    p->nStep = (pState ? pState->nRow : 0);
159305    p->aBuf = rbuMalloc(p, p->pgsz);
159306    p->iWalCksum = rbuShmChecksum(p);
159307  }
159308
159309  if( p->rc==SQLITE_OK && pState && pState->iWalCksum!=p->iWalCksum ){
159310    p->rc = SQLITE_DONE;
159311    p->eStage = RBU_STAGE_DONE;
159312  }
159313}
159314
159315/*
159316** Called when iAmt bytes are read from offset iOff of the wal file while
159317** the rbu object is in capture mode. Record the frame number of the frame
159318** being read in the aFrame[] array.
159319*/
159320static int rbuCaptureWalRead(sqlite3rbu *pRbu, i64 iOff, int iAmt){
159321  const u32 mReq = (1<<WAL_LOCK_WRITE)|(1<<WAL_LOCK_CKPT)|(1<<WAL_LOCK_READ0);
159322  u32 iFrame;
159323
159324  if( pRbu->mLock!=mReq ){
159325    pRbu->rc = SQLITE_BUSY;
159326    return SQLITE_INTERNAL;
159327  }
159328
159329  pRbu->pgsz = iAmt;
159330  if( pRbu->nFrame==pRbu->nFrameAlloc ){
159331    int nNew = (pRbu->nFrameAlloc ? pRbu->nFrameAlloc : 64) * 2;
159332    RbuFrame *aNew;
159333    aNew = (RbuFrame*)sqlite3_realloc(pRbu->aFrame, nNew * sizeof(RbuFrame));
159334    if( aNew==0 ) return SQLITE_NOMEM;
159335    pRbu->aFrame = aNew;
159336    pRbu->nFrameAlloc = nNew;
159337  }
159338
159339  iFrame = (u32)((iOff-32) / (i64)(iAmt+24)) + 1;
159340  if( pRbu->iMaxFrame<iFrame ) pRbu->iMaxFrame = iFrame;
159341  pRbu->aFrame[pRbu->nFrame].iWalFrame = iFrame;
159342  pRbu->aFrame[pRbu->nFrame].iDbPage = 0;
159343  pRbu->nFrame++;
159344  return SQLITE_OK;
159345}
159346
159347/*
159348** Called when a page of data is written to offset iOff of the database
159349** file while the rbu handle is in capture mode. Record the page number
159350** of the page being written in the aFrame[] array.
159351*/
159352static int rbuCaptureDbWrite(sqlite3rbu *pRbu, i64 iOff){
159353  pRbu->aFrame[pRbu->nFrame-1].iDbPage = (u32)(iOff / pRbu->pgsz) + 1;
159354  return SQLITE_OK;
159355}
159356
159357/*
159358** This is called as part of an incremental checkpoint operation. Copy
159359** a single frame of data from the wal file into the database file, as
159360** indicated by the RbuFrame object.
159361*/
159362static void rbuCheckpointFrame(sqlite3rbu *p, RbuFrame *pFrame){
159363  sqlite3_file *pWal = p->pTargetFd->pWalFd->pReal;
159364  sqlite3_file *pDb = p->pTargetFd->pReal;
159365  i64 iOff;
159366
159367  assert( p->rc==SQLITE_OK );
159368  iOff = (i64)(pFrame->iWalFrame-1) * (p->pgsz + 24) + 32 + 24;
159369  p->rc = pWal->pMethods->xRead(pWal, p->aBuf, p->pgsz, iOff);
159370  if( p->rc ) return;
159371
159372  iOff = (i64)(pFrame->iDbPage-1) * p->pgsz;
159373  p->rc = pDb->pMethods->xWrite(pDb, p->aBuf, p->pgsz, iOff);
159374}
159375
159376
159377/*
159378** Take an EXCLUSIVE lock on the database file.
159379*/
159380static void rbuLockDatabase(sqlite3rbu *p){
159381  sqlite3_file *pReal = p->pTargetFd->pReal;
159382  assert( p->rc==SQLITE_OK );
159383  p->rc = pReal->pMethods->xLock(pReal, SQLITE_LOCK_SHARED);
159384  if( p->rc==SQLITE_OK ){
159385    p->rc = pReal->pMethods->xLock(pReal, SQLITE_LOCK_EXCLUSIVE);
159386  }
159387}
159388
159389/*
159390** The RBU handle is currently in RBU_STAGE_OAL state, with a SHARED lock
159391** on the database file. This proc moves the *-oal file to the *-wal path,
159392** then reopens the database file (this time in vanilla, non-oal, WAL mode).
159393** If an error occurs, leave an error code and error message in the rbu
159394** handle.
159395*/
159396static void rbuMoveOalFile(sqlite3rbu *p){
159397  const char *zBase = sqlite3_db_filename(p->dbMain, "main");
159398
159399  char *zWal = sqlite3_mprintf("%s-wal", zBase);
159400  char *zOal = sqlite3_mprintf("%s-oal", zBase);
159401
159402  assert( p->eStage==RBU_STAGE_MOVE );
159403  assert( p->rc==SQLITE_OK && p->zErrmsg==0 );
159404  if( zWal==0 || zOal==0 ){
159405    p->rc = SQLITE_NOMEM;
159406  }else{
159407    /* Move the *-oal file to *-wal. At this point connection p->db is
159408    ** holding a SHARED lock on the target database file (because it is
159409    ** in WAL mode). So no other connection may be writing the db.
159410    **
159411    ** In order to ensure that there are no database readers, an EXCLUSIVE
159412    ** lock is obtained here before the *-oal is moved to *-wal.
159413    */
159414    rbuLockDatabase(p);
159415    if( p->rc==SQLITE_OK ){
159416      rbuFileSuffix3(zBase, zWal);
159417      rbuFileSuffix3(zBase, zOal);
159418
159419      /* Re-open the databases. */
159420      rbuObjIterFinalize(&p->objiter);
159421      sqlite3_close(p->dbMain);
159422      sqlite3_close(p->dbRbu);
159423      p->rc = rename(zOal, zWal) ? SQLITE_IOERR : SQLITE_OK;
159424      if( p->rc==SQLITE_OK ){
159425        p->dbMain = 0;
159426        p->dbRbu = 0;
159427        rbuOpenDatabase(p);
159428        rbuSetupCheckpoint(p, 0);
159429      }
159430    }
159431  }
159432
159433  sqlite3_free(zWal);
159434  sqlite3_free(zOal);
159435}
159436
159437/*
159438** The SELECT statement iterating through the keys for the current object
159439** (p->objiter.pSelect) currently points to a valid row. This function
159440** determines the type of operation requested by this row and returns
159441** one of the following values to indicate the result:
159442**
159443**     * RBU_INSERT
159444**     * RBU_DELETE
159445**     * RBU_IDX_DELETE
159446**     * RBU_UPDATE
159447**
159448** If RBU_UPDATE is returned, then output variable *pzMask is set to
159449** point to the text value indicating the columns to update.
159450**
159451** If the rbu_control field contains an invalid value, an error code and
159452** message are left in the RBU handle and zero returned.
159453*/
159454static int rbuStepType(sqlite3rbu *p, const char **pzMask){
159455  int iCol = p->objiter.nCol;     /* Index of rbu_control column */
159456  int res = 0;                    /* Return value */
159457
159458  switch( sqlite3_column_type(p->objiter.pSelect, iCol) ){
159459    case SQLITE_INTEGER: {
159460      int iVal = sqlite3_column_int(p->objiter.pSelect, iCol);
159461      if( iVal==0 ){
159462        res = RBU_INSERT;
159463      }else if( iVal==1 ){
159464        res = RBU_DELETE;
159465      }else if( iVal==2 ){
159466        res = RBU_IDX_DELETE;
159467      }else if( iVal==3 ){
159468        res = RBU_IDX_INSERT;
159469      }
159470      break;
159471    }
159472
159473    case SQLITE_TEXT: {
159474      const unsigned char *z = sqlite3_column_text(p->objiter.pSelect, iCol);
159475      if( z==0 ){
159476        p->rc = SQLITE_NOMEM;
159477      }else{
159478        *pzMask = (const char*)z;
159479      }
159480      res = RBU_UPDATE;
159481
159482      break;
159483    }
159484
159485    default:
159486      break;
159487  }
159488
159489  if( res==0 ){
159490    rbuBadControlError(p);
159491  }
159492  return res;
159493}
159494
159495#ifdef SQLITE_DEBUG
159496/*
159497** Assert that column iCol of statement pStmt is named zName.
159498*/
159499static void assertColumnName(sqlite3_stmt *pStmt, int iCol, const char *zName){
159500  const char *zCol = sqlite3_column_name(pStmt, iCol);
159501  assert( 0==sqlite3_stricmp(zName, zCol) );
159502}
159503#else
159504# define assertColumnName(x,y,z)
159505#endif
159506
159507/*
159508** This function does the work for an sqlite3rbu_step() call.
159509**
159510** The object-iterator (p->objiter) currently points to a valid object,
159511** and the input cursor (p->objiter.pSelect) currently points to a valid
159512** input row. Perform whatever processing is required and return.
159513**
159514** If no  error occurs, SQLITE_OK is returned. Otherwise, an error code
159515** and message is left in the RBU handle and a copy of the error code
159516** returned.
159517*/
159518static int rbuStep(sqlite3rbu *p){
159519  RbuObjIter *pIter = &p->objiter;
159520  const char *zMask = 0;
159521  int i;
159522  int eType = rbuStepType(p, &zMask);
159523
159524  if( eType ){
159525    assert( eType!=RBU_UPDATE || pIter->zIdx==0 );
159526
159527    if( pIter->zIdx==0 && eType==RBU_IDX_DELETE ){
159528      rbuBadControlError(p);
159529    }
159530    else if(
159531        eType==RBU_INSERT
159532     || eType==RBU_DELETE
159533     || eType==RBU_IDX_DELETE
159534     || eType==RBU_IDX_INSERT
159535    ){
159536      sqlite3_value *pVal;
159537      sqlite3_stmt *pWriter;
159538
159539      assert( eType!=RBU_UPDATE );
159540      assert( eType!=RBU_DELETE || pIter->zIdx==0 );
159541
159542      if( eType==RBU_IDX_DELETE || eType==RBU_DELETE ){
159543        pWriter = pIter->pDelete;
159544      }else{
159545        pWriter = pIter->pInsert;
159546      }
159547
159548      for(i=0; i<pIter->nCol; i++){
159549        /* If this is an INSERT into a table b-tree and the table has an
159550        ** explicit INTEGER PRIMARY KEY, check that this is not an attempt
159551        ** to write a NULL into the IPK column. That is not permitted.  */
159552        if( eType==RBU_INSERT
159553         && pIter->zIdx==0 && pIter->eType==RBU_PK_IPK && pIter->abTblPk[i]
159554         && sqlite3_column_type(pIter->pSelect, i)==SQLITE_NULL
159555        ){
159556          p->rc = SQLITE_MISMATCH;
159557          p->zErrmsg = sqlite3_mprintf("datatype mismatch");
159558          goto step_out;
159559        }
159560
159561        if( eType==RBU_DELETE && pIter->abTblPk[i]==0 ){
159562          continue;
159563        }
159564
159565        pVal = sqlite3_column_value(pIter->pSelect, i);
159566        p->rc = sqlite3_bind_value(pWriter, i+1, pVal);
159567        if( p->rc ) goto step_out;
159568      }
159569      if( pIter->zIdx==0
159570       && (pIter->eType==RBU_PK_VTAB || pIter->eType==RBU_PK_NONE)
159571      ){
159572        /* For a virtual table, or a table with no primary key, the
159573        ** SELECT statement is:
159574        **
159575        **   SELECT <cols>, rbu_control, rbu_rowid FROM ....
159576        **
159577        ** Hence column_value(pIter->nCol+1).
159578        */
159579        assertColumnName(pIter->pSelect, pIter->nCol+1, "rbu_rowid");
159580        pVal = sqlite3_column_value(pIter->pSelect, pIter->nCol+1);
159581        p->rc = sqlite3_bind_value(pWriter, pIter->nCol+1, pVal);
159582      }
159583      if( p->rc==SQLITE_OK ){
159584        sqlite3_step(pWriter);
159585        p->rc = resetAndCollectError(pWriter, &p->zErrmsg);
159586      }
159587    }else{
159588      sqlite3_value *pVal;
159589      sqlite3_stmt *pUpdate = 0;
159590      assert( eType==RBU_UPDATE );
159591      rbuGetUpdateStmt(p, pIter, zMask, &pUpdate);
159592      if( pUpdate ){
159593        for(i=0; p->rc==SQLITE_OK && i<pIter->nCol; i++){
159594          char c = zMask[pIter->aiSrcOrder[i]];
159595          pVal = sqlite3_column_value(pIter->pSelect, i);
159596          if( pIter->abTblPk[i] || c=='x' || c=='d' ){
159597            p->rc = sqlite3_bind_value(pUpdate, i+1, pVal);
159598          }
159599        }
159600        if( p->rc==SQLITE_OK
159601         && (pIter->eType==RBU_PK_VTAB || pIter->eType==RBU_PK_NONE)
159602        ){
159603          /* Bind the rbu_rowid value to column _rowid_ */
159604          assertColumnName(pIter->pSelect, pIter->nCol+1, "rbu_rowid");
159605          pVal = sqlite3_column_value(pIter->pSelect, pIter->nCol+1);
159606          p->rc = sqlite3_bind_value(pUpdate, pIter->nCol+1, pVal);
159607        }
159608        if( p->rc==SQLITE_OK ){
159609          sqlite3_step(pUpdate);
159610          p->rc = resetAndCollectError(pUpdate, &p->zErrmsg);
159611        }
159612      }
159613    }
159614  }
159615
159616 step_out:
159617  return p->rc;
159618}
159619
159620/*
159621** Increment the schema cookie of the main database opened by p->dbMain.
159622*/
159623static void rbuIncrSchemaCookie(sqlite3rbu *p){
159624  if( p->rc==SQLITE_OK ){
159625    int iCookie = 1000000;
159626    sqlite3_stmt *pStmt;
159627
159628    p->rc = prepareAndCollectError(p->dbMain, &pStmt, &p->zErrmsg,
159629        "PRAGMA schema_version"
159630    );
159631    if( p->rc==SQLITE_OK ){
159632      /* Coverage: it may be that this sqlite3_step() cannot fail. There
159633      ** is already a transaction open, so the prepared statement cannot
159634      ** throw an SQLITE_SCHEMA exception. The only database page the
159635      ** statement reads is page 1, which is guaranteed to be in the cache.
159636      ** And no memory allocations are required.  */
159637      if( SQLITE_ROW==sqlite3_step(pStmt) ){
159638        iCookie = sqlite3_column_int(pStmt, 0);
159639      }
159640      rbuFinalize(p, pStmt);
159641    }
159642    if( p->rc==SQLITE_OK ){
159643      rbuMPrintfExec(p, p->dbMain, "PRAGMA schema_version = %d", iCookie+1);
159644    }
159645  }
159646}
159647
159648/*
159649** Update the contents of the rbu_state table within the rbu database. The
159650** value stored in the RBU_STATE_STAGE column is eStage. All other values
159651** are determined by inspecting the rbu handle passed as the first argument.
159652*/
159653static void rbuSaveState(sqlite3rbu *p, int eStage){
159654  if( p->rc==SQLITE_OK || p->rc==SQLITE_DONE ){
159655    sqlite3_stmt *pInsert = 0;
159656    int rc;
159657
159658    assert( p->zErrmsg==0 );
159659    rc = prepareFreeAndCollectError(p->dbRbu, &pInsert, &p->zErrmsg,
159660        sqlite3_mprintf(
159661          "INSERT OR REPLACE INTO %s.rbu_state(k, v) VALUES "
159662          "(%d, %d), "
159663          "(%d, %Q), "
159664          "(%d, %Q), "
159665          "(%d, %d), "
159666          "(%d, %d), "
159667          "(%d, %lld), "
159668          "(%d, %lld), "
159669          "(%d, %lld) ",
159670          p->zStateDb,
159671          RBU_STATE_STAGE, eStage,
159672          RBU_STATE_TBL, p->objiter.zTbl,
159673          RBU_STATE_IDX, p->objiter.zIdx,
159674          RBU_STATE_ROW, p->nStep,
159675          RBU_STATE_PROGRESS, p->nProgress,
159676          RBU_STATE_CKPT, p->iWalCksum,
159677          RBU_STATE_COOKIE, (i64)p->pTargetFd->iCookie,
159678          RBU_STATE_OALSZ, p->iOalSz
159679      )
159680    );
159681    assert( pInsert==0 || rc==SQLITE_OK );
159682
159683    if( rc==SQLITE_OK ){
159684      sqlite3_step(pInsert);
159685      rc = sqlite3_finalize(pInsert);
159686    }
159687    if( rc!=SQLITE_OK ) p->rc = rc;
159688  }
159689}
159690
159691
159692/*
159693** Step the RBU object.
159694*/
159695SQLITE_API int SQLITE_STDCALL sqlite3rbu_step(sqlite3rbu *p){
159696  if( p ){
159697    switch( p->eStage ){
159698      case RBU_STAGE_OAL: {
159699        RbuObjIter *pIter = &p->objiter;
159700        while( p->rc==SQLITE_OK && pIter->zTbl ){
159701
159702          if( pIter->bCleanup ){
159703            /* Clean up the rbu_tmp_xxx table for the previous table. It
159704            ** cannot be dropped as there are currently active SQL statements.
159705            ** But the contents can be deleted.  */
159706            if( pIter->abIndexed ){
159707              rbuMPrintfExec(p, p->dbRbu,
159708                  "DELETE FROM %s.'rbu_tmp_%q'", p->zStateDb, pIter->zTbl
159709              );
159710            }
159711          }else{
159712            rbuObjIterPrepareAll(p, pIter, 0);
159713
159714            /* Advance to the next row to process. */
159715            if( p->rc==SQLITE_OK ){
159716              int rc = sqlite3_step(pIter->pSelect);
159717              if( rc==SQLITE_ROW ){
159718                p->nProgress++;
159719                p->nStep++;
159720                return rbuStep(p);
159721              }
159722              p->rc = sqlite3_reset(pIter->pSelect);
159723              p->nStep = 0;
159724            }
159725          }
159726
159727          rbuObjIterNext(p, pIter);
159728        }
159729
159730        if( p->rc==SQLITE_OK ){
159731          assert( pIter->zTbl==0 );
159732          rbuSaveState(p, RBU_STAGE_MOVE);
159733          rbuIncrSchemaCookie(p);
159734          if( p->rc==SQLITE_OK ){
159735            p->rc = sqlite3_exec(p->dbMain, "COMMIT", 0, 0, &p->zErrmsg);
159736          }
159737          if( p->rc==SQLITE_OK ){
159738            p->rc = sqlite3_exec(p->dbRbu, "COMMIT", 0, 0, &p->zErrmsg);
159739          }
159740          p->eStage = RBU_STAGE_MOVE;
159741        }
159742        break;
159743      }
159744
159745      case RBU_STAGE_MOVE: {
159746        if( p->rc==SQLITE_OK ){
159747          rbuMoveOalFile(p);
159748          p->nProgress++;
159749        }
159750        break;
159751      }
159752
159753      case RBU_STAGE_CKPT: {
159754        if( p->rc==SQLITE_OK ){
159755          if( p->nStep>=p->nFrame ){
159756            sqlite3_file *pDb = p->pTargetFd->pReal;
159757
159758            /* Sync the db file */
159759            p->rc = pDb->pMethods->xSync(pDb, SQLITE_SYNC_NORMAL);
159760
159761            /* Update nBackfill */
159762            if( p->rc==SQLITE_OK ){
159763              void volatile *ptr;
159764              p->rc = pDb->pMethods->xShmMap(pDb, 0, 32*1024, 0, &ptr);
159765              if( p->rc==SQLITE_OK ){
159766                ((u32 volatile*)ptr)[24] = p->iMaxFrame;
159767              }
159768            }
159769
159770            if( p->rc==SQLITE_OK ){
159771              p->eStage = RBU_STAGE_DONE;
159772              p->rc = SQLITE_DONE;
159773            }
159774          }else{
159775            RbuFrame *pFrame = &p->aFrame[p->nStep];
159776            rbuCheckpointFrame(p, pFrame);
159777            p->nStep++;
159778          }
159779          p->nProgress++;
159780        }
159781        break;
159782      }
159783
159784      default:
159785        break;
159786    }
159787    return p->rc;
159788  }else{
159789    return SQLITE_NOMEM;
159790  }
159791}
159792
159793/*
159794** Free an RbuState object allocated by rbuLoadState().
159795*/
159796static void rbuFreeState(RbuState *p){
159797  if( p ){
159798    sqlite3_free(p->zTbl);
159799    sqlite3_free(p->zIdx);
159800    sqlite3_free(p);
159801  }
159802}
159803
159804/*
159805** Allocate an RbuState object and load the contents of the rbu_state
159806** table into it. Return a pointer to the new object. It is the
159807** responsibility of the caller to eventually free the object using
159808** sqlite3_free().
159809**
159810** If an error occurs, leave an error code and message in the rbu handle
159811** and return NULL.
159812*/
159813static RbuState *rbuLoadState(sqlite3rbu *p){
159814  RbuState *pRet = 0;
159815  sqlite3_stmt *pStmt = 0;
159816  int rc;
159817  int rc2;
159818
159819  pRet = (RbuState*)rbuMalloc(p, sizeof(RbuState));
159820  if( pRet==0 ) return 0;
159821
159822  rc = prepareFreeAndCollectError(p->dbRbu, &pStmt, &p->zErrmsg,
159823      sqlite3_mprintf("SELECT k, v FROM %s.rbu_state", p->zStateDb)
159824  );
159825  while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
159826    switch( sqlite3_column_int(pStmt, 0) ){
159827      case RBU_STATE_STAGE:
159828        pRet->eStage = sqlite3_column_int(pStmt, 1);
159829        if( pRet->eStage!=RBU_STAGE_OAL
159830         && pRet->eStage!=RBU_STAGE_MOVE
159831         && pRet->eStage!=RBU_STAGE_CKPT
159832        ){
159833          p->rc = SQLITE_CORRUPT;
159834        }
159835        break;
159836
159837      case RBU_STATE_TBL:
159838        pRet->zTbl = rbuStrndup((char*)sqlite3_column_text(pStmt, 1), &rc);
159839        break;
159840
159841      case RBU_STATE_IDX:
159842        pRet->zIdx = rbuStrndup((char*)sqlite3_column_text(pStmt, 1), &rc);
159843        break;
159844
159845      case RBU_STATE_ROW:
159846        pRet->nRow = sqlite3_column_int(pStmt, 1);
159847        break;
159848
159849      case RBU_STATE_PROGRESS:
159850        pRet->nProgress = sqlite3_column_int64(pStmt, 1);
159851        break;
159852
159853      case RBU_STATE_CKPT:
159854        pRet->iWalCksum = sqlite3_column_int64(pStmt, 1);
159855        break;
159856
159857      case RBU_STATE_COOKIE:
159858        pRet->iCookie = (u32)sqlite3_column_int64(pStmt, 1);
159859        break;
159860
159861      case RBU_STATE_OALSZ:
159862        pRet->iOalSz = (u32)sqlite3_column_int64(pStmt, 1);
159863        break;
159864
159865      default:
159866        rc = SQLITE_CORRUPT;
159867        break;
159868    }
159869  }
159870  rc2 = sqlite3_finalize(pStmt);
159871  if( rc==SQLITE_OK ) rc = rc2;
159872
159873  p->rc = rc;
159874  return pRet;
159875}
159876
159877/*
159878** Compare strings z1 and z2, returning 0 if they are identical, or non-zero
159879** otherwise. Either or both argument may be NULL. Two NULL values are
159880** considered equal, and NULL is considered distinct from all other values.
159881*/
159882static int rbuStrCompare(const char *z1, const char *z2){
159883  if( z1==0 && z2==0 ) return 0;
159884  if( z1==0 || z2==0 ) return 1;
159885  return (sqlite3_stricmp(z1, z2)!=0);
159886}
159887
159888/*
159889** This function is called as part of sqlite3rbu_open() when initializing
159890** an rbu handle in OAL stage. If the rbu update has not started (i.e.
159891** the rbu_state table was empty) it is a no-op. Otherwise, it arranges
159892** things so that the next call to sqlite3rbu_step() continues on from
159893** where the previous rbu handle left off.
159894**
159895** If an error occurs, an error code and error message are left in the
159896** rbu handle passed as the first argument.
159897*/
159898static void rbuSetupOal(sqlite3rbu *p, RbuState *pState){
159899  assert( p->rc==SQLITE_OK );
159900  if( pState->zTbl ){
159901    RbuObjIter *pIter = &p->objiter;
159902    int rc = SQLITE_OK;
159903
159904    while( rc==SQLITE_OK && pIter->zTbl && (pIter->bCleanup
159905       || rbuStrCompare(pIter->zIdx, pState->zIdx)
159906       || rbuStrCompare(pIter->zTbl, pState->zTbl)
159907    )){
159908      rc = rbuObjIterNext(p, pIter);
159909    }
159910
159911    if( rc==SQLITE_OK && !pIter->zTbl ){
159912      rc = SQLITE_ERROR;
159913      p->zErrmsg = sqlite3_mprintf("rbu_state mismatch error");
159914    }
159915
159916    if( rc==SQLITE_OK ){
159917      p->nStep = pState->nRow;
159918      rc = rbuObjIterPrepareAll(p, &p->objiter, p->nStep);
159919    }
159920
159921    p->rc = rc;
159922  }
159923}
159924
159925/*
159926** If there is a "*-oal" file in the file-system corresponding to the
159927** target database in the file-system, delete it. If an error occurs,
159928** leave an error code and error message in the rbu handle.
159929*/
159930static void rbuDeleteOalFile(sqlite3rbu *p){
159931  char *zOal = sqlite3_mprintf("%s-oal", p->zTarget);
159932  assert( p->rc==SQLITE_OK && p->zErrmsg==0 );
159933  unlink(zOal);
159934  sqlite3_free(zOal);
159935}
159936
159937/*
159938** Allocate a private rbu VFS for the rbu handle passed as the only
159939** argument. This VFS will be used unless the call to sqlite3rbu_open()
159940** specified a URI with a vfs=? option in place of a target database
159941** file name.
159942*/
159943static void rbuCreateVfs(sqlite3rbu *p){
159944  int rnd;
159945  char zRnd[64];
159946
159947  assert( p->rc==SQLITE_OK );
159948  sqlite3_randomness(sizeof(int), (void*)&rnd);
159949  sqlite3_snprintf(sizeof(zRnd), zRnd, "rbu_vfs_%d", rnd);
159950  p->rc = sqlite3rbu_create_vfs(zRnd, 0);
159951  if( p->rc==SQLITE_OK ){
159952    sqlite3_vfs *pVfs = sqlite3_vfs_find(zRnd);
159953    assert( pVfs );
159954    p->zVfsName = pVfs->zName;
159955  }
159956}
159957
159958/*
159959** Destroy the private VFS created for the rbu handle passed as the only
159960** argument by an earlier call to rbuCreateVfs().
159961*/
159962static void rbuDeleteVfs(sqlite3rbu *p){
159963  if( p->zVfsName ){
159964    sqlite3rbu_destroy_vfs(p->zVfsName);
159965    p->zVfsName = 0;
159966  }
159967}
159968
159969/*
159970** Open and return a new RBU handle.
159971*/
159972SQLITE_API sqlite3rbu *SQLITE_STDCALL sqlite3rbu_open(
159973  const char *zTarget,
159974  const char *zRbu,
159975  const char *zState
159976){
159977  sqlite3rbu *p;
159978  int nTarget = strlen(zTarget);
159979  int nRbu = strlen(zRbu);
159980  int nState = zState ? strlen(zState) : 0;
159981
159982  p = (sqlite3rbu*)sqlite3_malloc(sizeof(sqlite3rbu)+nTarget+1+nRbu+1+nState+1);
159983  if( p ){
159984    RbuState *pState = 0;
159985
159986    /* Create the custom VFS. */
159987    memset(p, 0, sizeof(sqlite3rbu));
159988    rbuCreateVfs(p);
159989
159990    /* Open the target database */
159991    if( p->rc==SQLITE_OK ){
159992      p->zTarget = (char*)&p[1];
159993      memcpy(p->zTarget, zTarget, nTarget+1);
159994      p->zRbu = &p->zTarget[nTarget+1];
159995      memcpy(p->zRbu, zRbu, nRbu+1);
159996      if( zState ){
159997        p->zState = &p->zRbu[nRbu+1];
159998        memcpy(p->zState, zState, nState+1);
159999      }
160000      rbuOpenDatabase(p);
160001    }
160002
160003    /* If it has not already been created, create the rbu_state table */
160004    rbuMPrintfExec(p, p->dbRbu, RBU_CREATE_STATE, p->zStateDb);
160005
160006    if( p->rc==SQLITE_OK ){
160007      pState = rbuLoadState(p);
160008      assert( pState || p->rc!=SQLITE_OK );
160009      if( p->rc==SQLITE_OK ){
160010
160011        if( pState->eStage==0 ){
160012          rbuDeleteOalFile(p);
160013          p->eStage = RBU_STAGE_OAL;
160014        }else{
160015          p->eStage = pState->eStage;
160016        }
160017        p->nProgress = pState->nProgress;
160018        p->iOalSz = pState->iOalSz;
160019      }
160020    }
160021    assert( p->rc!=SQLITE_OK || p->eStage!=0 );
160022
160023    if( p->rc==SQLITE_OK && p->pTargetFd->pWalFd ){
160024      if( p->eStage==RBU_STAGE_OAL ){
160025        p->rc = SQLITE_ERROR;
160026        p->zErrmsg = sqlite3_mprintf("cannot update wal mode database");
160027      }else if( p->eStage==RBU_STAGE_MOVE ){
160028        p->eStage = RBU_STAGE_CKPT;
160029        p->nStep = 0;
160030      }
160031    }
160032
160033    if( p->rc==SQLITE_OK
160034     && (p->eStage==RBU_STAGE_OAL || p->eStage==RBU_STAGE_MOVE)
160035     && pState->eStage!=0 && p->pTargetFd->iCookie!=pState->iCookie
160036    ){
160037      /* At this point (pTargetFd->iCookie) contains the value of the
160038      ** change-counter cookie (the thing that gets incremented when a
160039      ** transaction is committed in rollback mode) currently stored on
160040      ** page 1 of the database file. */
160041      p->rc = SQLITE_BUSY;
160042      p->zErrmsg = sqlite3_mprintf("database modified during rbu update");
160043    }
160044
160045    if( p->rc==SQLITE_OK ){
160046      if( p->eStage==RBU_STAGE_OAL ){
160047
160048        /* Open transactions both databases. The *-oal file is opened or
160049        ** created at this point. */
160050        p->rc = sqlite3_exec(p->dbMain, "BEGIN IMMEDIATE", 0, 0, &p->zErrmsg);
160051        if( p->rc==SQLITE_OK ){
160052          p->rc = sqlite3_exec(p->dbRbu, "BEGIN IMMEDIATE", 0, 0, &p->zErrmsg);
160053        }
160054
160055        /* Point the object iterator at the first object */
160056        if( p->rc==SQLITE_OK ){
160057          p->rc = rbuObjIterFirst(p, &p->objiter);
160058        }
160059
160060        /* If the RBU database contains no data_xxx tables, declare the RBU
160061        ** update finished.  */
160062        if( p->rc==SQLITE_OK && p->objiter.zTbl==0 ){
160063          p->rc = SQLITE_DONE;
160064        }
160065
160066        if( p->rc==SQLITE_OK ){
160067          rbuSetupOal(p, pState);
160068        }
160069
160070      }else if( p->eStage==RBU_STAGE_MOVE ){
160071        /* no-op */
160072      }else if( p->eStage==RBU_STAGE_CKPT ){
160073        rbuSetupCheckpoint(p, pState);
160074      }else if( p->eStage==RBU_STAGE_DONE ){
160075        p->rc = SQLITE_DONE;
160076      }else{
160077        p->rc = SQLITE_CORRUPT;
160078      }
160079    }
160080
160081    rbuFreeState(pState);
160082  }
160083
160084  return p;
160085}
160086
160087
160088/*
160089** Return the database handle used by pRbu.
160090*/
160091SQLITE_API sqlite3 *SQLITE_STDCALL sqlite3rbu_db(sqlite3rbu *pRbu, int bRbu){
160092  sqlite3 *db = 0;
160093  if( pRbu ){
160094    db = (bRbu ? pRbu->dbRbu : pRbu->dbMain);
160095  }
160096  return db;
160097}
160098
160099
160100/*
160101** If the error code currently stored in the RBU handle is SQLITE_CONSTRAINT,
160102** then edit any error message string so as to remove all occurrences of
160103** the pattern "rbu_imp_[0-9]*".
160104*/
160105static void rbuEditErrmsg(sqlite3rbu *p){
160106  if( p->rc==SQLITE_CONSTRAINT && p->zErrmsg ){
160107    int i;
160108    int nErrmsg = strlen(p->zErrmsg);
160109    for(i=0; i<(nErrmsg-8); i++){
160110      if( memcmp(&p->zErrmsg[i], "rbu_imp_", 8)==0 ){
160111        int nDel = 8;
160112        while( p->zErrmsg[i+nDel]>='0' && p->zErrmsg[i+nDel]<='9' ) nDel++;
160113        memmove(&p->zErrmsg[i], &p->zErrmsg[i+nDel], nErrmsg + 1 - i - nDel);
160114        nErrmsg -= nDel;
160115      }
160116    }
160117  }
160118}
160119
160120/*
160121** Close the RBU handle.
160122*/
160123SQLITE_API int SQLITE_STDCALL sqlite3rbu_close(sqlite3rbu *p, char **pzErrmsg){
160124  int rc;
160125  if( p ){
160126
160127    /* Commit the transaction to the *-oal file. */
160128    if( p->rc==SQLITE_OK && p->eStage==RBU_STAGE_OAL ){
160129      p->rc = sqlite3_exec(p->dbMain, "COMMIT", 0, 0, &p->zErrmsg);
160130    }
160131
160132    rbuSaveState(p, p->eStage);
160133
160134    if( p->rc==SQLITE_OK && p->eStage==RBU_STAGE_OAL ){
160135      p->rc = sqlite3_exec(p->dbRbu, "COMMIT", 0, 0, &p->zErrmsg);
160136    }
160137
160138    /* Close any open statement handles. */
160139    rbuObjIterFinalize(&p->objiter);
160140
160141    /* Close the open database handle and VFS object. */
160142    sqlite3_close(p->dbMain);
160143    sqlite3_close(p->dbRbu);
160144    rbuDeleteVfs(p);
160145    sqlite3_free(p->aBuf);
160146    sqlite3_free(p->aFrame);
160147
160148    rbuEditErrmsg(p);
160149    rc = p->rc;
160150    *pzErrmsg = p->zErrmsg;
160151    sqlite3_free(p);
160152  }else{
160153    rc = SQLITE_NOMEM;
160154    *pzErrmsg = 0;
160155  }
160156  return rc;
160157}
160158
160159/*
160160** Return the total number of key-value operations (inserts, deletes or
160161** updates) that have been performed on the target database since the
160162** current RBU update was started.
160163*/
160164SQLITE_API sqlite3_int64 SQLITE_STDCALL sqlite3rbu_progress(sqlite3rbu *pRbu){
160165  return pRbu->nProgress;
160166}
160167
160168/**************************************************************************
160169** Beginning of RBU VFS shim methods. The VFS shim modifies the behaviour
160170** of a standard VFS in the following ways:
160171**
160172** 1. Whenever the first page of a main database file is read or
160173**    written, the value of the change-counter cookie is stored in
160174**    rbu_file.iCookie. Similarly, the value of the "write-version"
160175**    database header field is stored in rbu_file.iWriteVer. This ensures
160176**    that the values are always trustworthy within an open transaction.
160177**
160178** 2. Whenever an SQLITE_OPEN_WAL file is opened, the (rbu_file.pWalFd)
160179**    member variable of the associated database file descriptor is set
160180**    to point to the new file. A mutex protected linked list of all main
160181**    db fds opened using a particular RBU VFS is maintained at
160182**    rbu_vfs.pMain to facilitate this.
160183**
160184** 3. Using a new file-control "SQLITE_FCNTL_RBU", a main db rbu_file
160185**    object can be marked as the target database of an RBU update. This
160186**    turns on the following extra special behaviour:
160187**
160188** 3a. If xAccess() is called to check if there exists a *-wal file
160189**     associated with an RBU target database currently in RBU_STAGE_OAL
160190**     stage (preparing the *-oal file), the following special handling
160191**     applies:
160192**
160193**      * if the *-wal file does exist, return SQLITE_CANTOPEN. An RBU
160194**        target database may not be in wal mode already.
160195**
160196**      * if the *-wal file does not exist, set the output parameter to
160197**        non-zero (to tell SQLite that it does exist) anyway.
160198**
160199**     Then, when xOpen() is called to open the *-wal file associated with
160200**     the RBU target in RBU_STAGE_OAL stage, instead of opening the *-wal
160201**     file, the rbu vfs opens the corresponding *-oal file instead.
160202**
160203** 3b. The *-shm pages returned by xShmMap() for a target db file in
160204**     RBU_STAGE_OAL mode are actually stored in heap memory. This is to
160205**     avoid creating a *-shm file on disk. Additionally, xShmLock() calls
160206**     are no-ops on target database files in RBU_STAGE_OAL mode. This is
160207**     because assert() statements in some VFS implementations fail if
160208**     xShmLock() is called before xShmMap().
160209**
160210** 3c. If an EXCLUSIVE lock is attempted on a target database file in any
160211**     mode except RBU_STAGE_DONE (all work completed and checkpointed), it
160212**     fails with an SQLITE_BUSY error. This is to stop RBU connections
160213**     from automatically checkpointing a *-wal (or *-oal) file from within
160214**     sqlite3_close().
160215**
160216** 3d. In RBU_STAGE_CAPTURE mode, all xRead() calls on the wal file, and
160217**     all xWrite() calls on the target database file perform no IO.
160218**     Instead the frame and page numbers that would be read and written
160219**     are recorded. Additionally, successful attempts to obtain exclusive
160220**     xShmLock() WRITER, CHECKPOINTER and READ0 locks on the target
160221**     database file are recorded. xShmLock() calls to unlock the same
160222**     locks are no-ops (so that once obtained, these locks are never
160223**     relinquished). Finally, calls to xSync() on the target database
160224**     file fail with SQLITE_INTERNAL errors.
160225*/
160226
160227static void rbuUnlockShm(rbu_file *p){
160228  if( p->pRbu ){
160229    int (*xShmLock)(sqlite3_file*,int,int,int) = p->pReal->pMethods->xShmLock;
160230    int i;
160231    for(i=0; i<SQLITE_SHM_NLOCK;i++){
160232      if( (1<<i) & p->pRbu->mLock ){
160233        xShmLock(p->pReal, i, 1, SQLITE_SHM_UNLOCK|SQLITE_SHM_EXCLUSIVE);
160234      }
160235    }
160236    p->pRbu->mLock = 0;
160237  }
160238}
160239
160240/*
160241** Close an rbu file.
160242*/
160243static int rbuVfsClose(sqlite3_file *pFile){
160244  rbu_file *p = (rbu_file*)pFile;
160245  int rc;
160246  int i;
160247
160248  /* Free the contents of the apShm[] array. And the array itself. */
160249  for(i=0; i<p->nShm; i++){
160250    sqlite3_free(p->apShm[i]);
160251  }
160252  sqlite3_free(p->apShm);
160253  p->apShm = 0;
160254  sqlite3_free(p->zDel);
160255
160256  if( p->openFlags & SQLITE_OPEN_MAIN_DB ){
160257    rbu_file **pp;
160258    sqlite3_mutex_enter(p->pRbuVfs->mutex);
160259    for(pp=&p->pRbuVfs->pMain; *pp!=p; pp=&((*pp)->pMainNext));
160260    *pp = p->pMainNext;
160261    sqlite3_mutex_leave(p->pRbuVfs->mutex);
160262    rbuUnlockShm(p);
160263    p->pReal->pMethods->xShmUnmap(p->pReal, 0);
160264  }
160265
160266  /* Close the underlying file handle */
160267  rc = p->pReal->pMethods->xClose(p->pReal);
160268  return rc;
160269}
160270
160271
160272/*
160273** Read and return an unsigned 32-bit big-endian integer from the buffer
160274** passed as the only argument.
160275*/
160276static u32 rbuGetU32(u8 *aBuf){
160277  return ((u32)aBuf[0] << 24)
160278       + ((u32)aBuf[1] << 16)
160279       + ((u32)aBuf[2] <<  8)
160280       + ((u32)aBuf[3]);
160281}
160282
160283/*
160284** Read data from an rbuVfs-file.
160285*/
160286static int rbuVfsRead(
160287  sqlite3_file *pFile,
160288  void *zBuf,
160289  int iAmt,
160290  sqlite_int64 iOfst
160291){
160292  rbu_file *p = (rbu_file*)pFile;
160293  sqlite3rbu *pRbu = p->pRbu;
160294  int rc;
160295
160296  if( pRbu && pRbu->eStage==RBU_STAGE_CAPTURE ){
160297    assert( p->openFlags & SQLITE_OPEN_WAL );
160298    rc = rbuCaptureWalRead(p->pRbu, iOfst, iAmt);
160299  }else{
160300    if( pRbu && pRbu->eStage==RBU_STAGE_OAL
160301     && (p->openFlags & SQLITE_OPEN_WAL)
160302     && iOfst>=pRbu->iOalSz
160303    ){
160304      rc = SQLITE_OK;
160305      memset(zBuf, 0, iAmt);
160306    }else{
160307      rc = p->pReal->pMethods->xRead(p->pReal, zBuf, iAmt, iOfst);
160308    }
160309    if( rc==SQLITE_OK && iOfst==0 && (p->openFlags & SQLITE_OPEN_MAIN_DB) ){
160310      /* These look like magic numbers. But they are stable, as they are part
160311       ** of the definition of the SQLite file format, which may not change. */
160312      u8 *pBuf = (u8*)zBuf;
160313      p->iCookie = rbuGetU32(&pBuf[24]);
160314      p->iWriteVer = pBuf[19];
160315    }
160316  }
160317  return rc;
160318}
160319
160320/*
160321** Write data to an rbuVfs-file.
160322*/
160323static int rbuVfsWrite(
160324  sqlite3_file *pFile,
160325  const void *zBuf,
160326  int iAmt,
160327  sqlite_int64 iOfst
160328){
160329  rbu_file *p = (rbu_file*)pFile;
160330  sqlite3rbu *pRbu = p->pRbu;
160331  int rc;
160332
160333  if( pRbu && pRbu->eStage==RBU_STAGE_CAPTURE ){
160334    assert( p->openFlags & SQLITE_OPEN_MAIN_DB );
160335    rc = rbuCaptureDbWrite(p->pRbu, iOfst);
160336  }else{
160337    if( pRbu && pRbu->eStage==RBU_STAGE_OAL
160338     && (p->openFlags & SQLITE_OPEN_WAL)
160339     && iOfst>=pRbu->iOalSz
160340    ){
160341      pRbu->iOalSz = iAmt + iOfst;
160342    }
160343    rc = p->pReal->pMethods->xWrite(p->pReal, zBuf, iAmt, iOfst);
160344    if( rc==SQLITE_OK && iOfst==0 && (p->openFlags & SQLITE_OPEN_MAIN_DB) ){
160345      /* These look like magic numbers. But they are stable, as they are part
160346      ** of the definition of the SQLite file format, which may not change. */
160347      u8 *pBuf = (u8*)zBuf;
160348      p->iCookie = rbuGetU32(&pBuf[24]);
160349      p->iWriteVer = pBuf[19];
160350    }
160351  }
160352  return rc;
160353}
160354
160355/*
160356** Truncate an rbuVfs-file.
160357*/
160358static int rbuVfsTruncate(sqlite3_file *pFile, sqlite_int64 size){
160359  rbu_file *p = (rbu_file*)pFile;
160360  return p->pReal->pMethods->xTruncate(p->pReal, size);
160361}
160362
160363/*
160364** Sync an rbuVfs-file.
160365*/
160366static int rbuVfsSync(sqlite3_file *pFile, int flags){
160367  rbu_file *p = (rbu_file *)pFile;
160368  if( p->pRbu && p->pRbu->eStage==RBU_STAGE_CAPTURE ){
160369    if( p->openFlags & SQLITE_OPEN_MAIN_DB ){
160370      return SQLITE_INTERNAL;
160371    }
160372    return SQLITE_OK;
160373  }
160374  return p->pReal->pMethods->xSync(p->pReal, flags);
160375}
160376
160377/*
160378** Return the current file-size of an rbuVfs-file.
160379*/
160380static int rbuVfsFileSize(sqlite3_file *pFile, sqlite_int64 *pSize){
160381  rbu_file *p = (rbu_file *)pFile;
160382  return p->pReal->pMethods->xFileSize(p->pReal, pSize);
160383}
160384
160385/*
160386** Lock an rbuVfs-file.
160387*/
160388static int rbuVfsLock(sqlite3_file *pFile, int eLock){
160389  rbu_file *p = (rbu_file*)pFile;
160390  sqlite3rbu *pRbu = p->pRbu;
160391  int rc = SQLITE_OK;
160392
160393  assert( p->openFlags & (SQLITE_OPEN_MAIN_DB|SQLITE_OPEN_TEMP_DB) );
160394  if( pRbu && eLock==SQLITE_LOCK_EXCLUSIVE && pRbu->eStage!=RBU_STAGE_DONE ){
160395    /* Do not allow EXCLUSIVE locks. Preventing SQLite from taking this
160396    ** prevents it from checkpointing the database from sqlite3_close(). */
160397    rc = SQLITE_BUSY;
160398  }else{
160399    rc = p->pReal->pMethods->xLock(p->pReal, eLock);
160400  }
160401
160402  return rc;
160403}
160404
160405/*
160406** Unlock an rbuVfs-file.
160407*/
160408static int rbuVfsUnlock(sqlite3_file *pFile, int eLock){
160409  rbu_file *p = (rbu_file *)pFile;
160410  return p->pReal->pMethods->xUnlock(p->pReal, eLock);
160411}
160412
160413/*
160414** Check if another file-handle holds a RESERVED lock on an rbuVfs-file.
160415*/
160416static int rbuVfsCheckReservedLock(sqlite3_file *pFile, int *pResOut){
160417  rbu_file *p = (rbu_file *)pFile;
160418  return p->pReal->pMethods->xCheckReservedLock(p->pReal, pResOut);
160419}
160420
160421/*
160422** File control method. For custom operations on an rbuVfs-file.
160423*/
160424static int rbuVfsFileControl(sqlite3_file *pFile, int op, void *pArg){
160425  rbu_file *p = (rbu_file *)pFile;
160426  int (*xControl)(sqlite3_file*,int,void*) = p->pReal->pMethods->xFileControl;
160427  int rc;
160428
160429  assert( p->openFlags & (SQLITE_OPEN_MAIN_DB|SQLITE_OPEN_TEMP_DB)
160430       || p->openFlags & (SQLITE_OPEN_TRANSIENT_DB|SQLITE_OPEN_TEMP_JOURNAL)
160431  );
160432  if( op==SQLITE_FCNTL_RBU ){
160433    sqlite3rbu *pRbu = (sqlite3rbu*)pArg;
160434
160435    /* First try to find another RBU vfs lower down in the vfs stack. If
160436    ** one is found, this vfs will operate in pass-through mode. The lower
160437    ** level vfs will do the special RBU handling.  */
160438    rc = xControl(p->pReal, op, pArg);
160439
160440    if( rc==SQLITE_NOTFOUND ){
160441      /* Now search for a zipvfs instance lower down in the VFS stack. If
160442      ** one is found, this is an error.  */
160443      void *dummy = 0;
160444      rc = xControl(p->pReal, SQLITE_FCNTL_ZIPVFS, &dummy);
160445      if( rc==SQLITE_OK ){
160446        rc = SQLITE_ERROR;
160447        pRbu->zErrmsg = sqlite3_mprintf("rbu/zipvfs setup error");
160448      }else if( rc==SQLITE_NOTFOUND ){
160449        pRbu->pTargetFd = p;
160450        p->pRbu = pRbu;
160451        if( p->pWalFd ) p->pWalFd->pRbu = pRbu;
160452        rc = SQLITE_OK;
160453      }
160454    }
160455    return rc;
160456  }
160457
160458  rc = xControl(p->pReal, op, pArg);
160459  if( rc==SQLITE_OK && op==SQLITE_FCNTL_VFSNAME ){
160460    rbu_vfs *pRbuVfs = p->pRbuVfs;
160461    char *zIn = *(char**)pArg;
160462    char *zOut = sqlite3_mprintf("rbu(%s)/%z", pRbuVfs->base.zName, zIn);
160463    *(char**)pArg = zOut;
160464    if( zOut==0 ) rc = SQLITE_NOMEM;
160465  }
160466
160467  return rc;
160468}
160469
160470/*
160471** Return the sector-size in bytes for an rbuVfs-file.
160472*/
160473static int rbuVfsSectorSize(sqlite3_file *pFile){
160474  rbu_file *p = (rbu_file *)pFile;
160475  return p->pReal->pMethods->xSectorSize(p->pReal);
160476}
160477
160478/*
160479** Return the device characteristic flags supported by an rbuVfs-file.
160480*/
160481static int rbuVfsDeviceCharacteristics(sqlite3_file *pFile){
160482  rbu_file *p = (rbu_file *)pFile;
160483  return p->pReal->pMethods->xDeviceCharacteristics(p->pReal);
160484}
160485
160486/*
160487** Take or release a shared-memory lock.
160488*/
160489static int rbuVfsShmLock(sqlite3_file *pFile, int ofst, int n, int flags){
160490  rbu_file *p = (rbu_file*)pFile;
160491  sqlite3rbu *pRbu = p->pRbu;
160492  int rc = SQLITE_OK;
160493
160494#ifdef SQLITE_AMALGAMATION
160495    assert( WAL_CKPT_LOCK==1 );
160496#endif
160497
160498  assert( p->openFlags & (SQLITE_OPEN_MAIN_DB|SQLITE_OPEN_TEMP_DB) );
160499  if( pRbu && (pRbu->eStage==RBU_STAGE_OAL || pRbu->eStage==RBU_STAGE_MOVE) ){
160500    /* Magic number 1 is the WAL_CKPT_LOCK lock. Preventing SQLite from
160501    ** taking this lock also prevents any checkpoints from occurring.
160502    ** todo: really, it's not clear why this might occur, as
160503    ** wal_autocheckpoint ought to be turned off.  */
160504    if( ofst==WAL_LOCK_CKPT && n==1 ) rc = SQLITE_BUSY;
160505  }else{
160506    int bCapture = 0;
160507    if( n==1 && (flags & SQLITE_SHM_EXCLUSIVE)
160508     && pRbu && pRbu->eStage==RBU_STAGE_CAPTURE
160509     && (ofst==WAL_LOCK_WRITE || ofst==WAL_LOCK_CKPT || ofst==WAL_LOCK_READ0)
160510    ){
160511      bCapture = 1;
160512    }
160513
160514    if( bCapture==0 || 0==(flags & SQLITE_SHM_UNLOCK) ){
160515      rc = p->pReal->pMethods->xShmLock(p->pReal, ofst, n, flags);
160516      if( bCapture && rc==SQLITE_OK ){
160517        pRbu->mLock |= (1 << ofst);
160518      }
160519    }
160520  }
160521
160522  return rc;
160523}
160524
160525/*
160526** Obtain a pointer to a mapping of a single 32KiB page of the *-shm file.
160527*/
160528static int rbuVfsShmMap(
160529  sqlite3_file *pFile,
160530  int iRegion,
160531  int szRegion,
160532  int isWrite,
160533  void volatile **pp
160534){
160535  rbu_file *p = (rbu_file*)pFile;
160536  int rc = SQLITE_OK;
160537  int eStage = (p->pRbu ? p->pRbu->eStage : 0);
160538
160539  /* If not in RBU_STAGE_OAL, allow this call to pass through. Or, if this
160540  ** rbu is in the RBU_STAGE_OAL state, use heap memory for *-shm space
160541  ** instead of a file on disk.  */
160542  assert( p->openFlags & (SQLITE_OPEN_MAIN_DB|SQLITE_OPEN_TEMP_DB) );
160543  if( eStage==RBU_STAGE_OAL || eStage==RBU_STAGE_MOVE ){
160544    if( iRegion<=p->nShm ){
160545      int nByte = (iRegion+1) * sizeof(char*);
160546      char **apNew = (char**)sqlite3_realloc(p->apShm, nByte);
160547      if( apNew==0 ){
160548        rc = SQLITE_NOMEM;
160549      }else{
160550        memset(&apNew[p->nShm], 0, sizeof(char*) * (1 + iRegion - p->nShm));
160551        p->apShm = apNew;
160552        p->nShm = iRegion+1;
160553      }
160554    }
160555
160556    if( rc==SQLITE_OK && p->apShm[iRegion]==0 ){
160557      char *pNew = (char*)sqlite3_malloc(szRegion);
160558      if( pNew==0 ){
160559        rc = SQLITE_NOMEM;
160560      }else{
160561        memset(pNew, 0, szRegion);
160562        p->apShm[iRegion] = pNew;
160563      }
160564    }
160565
160566    if( rc==SQLITE_OK ){
160567      *pp = p->apShm[iRegion];
160568    }else{
160569      *pp = 0;
160570    }
160571  }else{
160572    assert( p->apShm==0 );
160573    rc = p->pReal->pMethods->xShmMap(p->pReal, iRegion, szRegion, isWrite, pp);
160574  }
160575
160576  return rc;
160577}
160578
160579/*
160580** Memory barrier.
160581*/
160582static void rbuVfsShmBarrier(sqlite3_file *pFile){
160583  rbu_file *p = (rbu_file *)pFile;
160584  p->pReal->pMethods->xShmBarrier(p->pReal);
160585}
160586
160587/*
160588** The xShmUnmap method.
160589*/
160590static int rbuVfsShmUnmap(sqlite3_file *pFile, int delFlag){
160591  rbu_file *p = (rbu_file*)pFile;
160592  int rc = SQLITE_OK;
160593  int eStage = (p->pRbu ? p->pRbu->eStage : 0);
160594
160595  assert( p->openFlags & (SQLITE_OPEN_MAIN_DB|SQLITE_OPEN_TEMP_DB) );
160596  if( eStage==RBU_STAGE_OAL || eStage==RBU_STAGE_MOVE ){
160597    /* no-op */
160598  }else{
160599    /* Release the checkpointer and writer locks */
160600    rbuUnlockShm(p);
160601    rc = p->pReal->pMethods->xShmUnmap(p->pReal, delFlag);
160602  }
160603  return rc;
160604}
160605
160606/*
160607** Given that zWal points to a buffer containing a wal file name passed to
160608** either the xOpen() or xAccess() VFS method, return a pointer to the
160609** file-handle opened by the same database connection on the corresponding
160610** database file.
160611*/
160612static rbu_file *rbuFindMaindb(rbu_vfs *pRbuVfs, const char *zWal){
160613  rbu_file *pDb;
160614  sqlite3_mutex_enter(pRbuVfs->mutex);
160615  for(pDb=pRbuVfs->pMain; pDb && pDb->zWal!=zWal; pDb=pDb->pMainNext);
160616  sqlite3_mutex_leave(pRbuVfs->mutex);
160617  return pDb;
160618}
160619
160620/*
160621** Open an rbu file handle.
160622*/
160623static int rbuVfsOpen(
160624  sqlite3_vfs *pVfs,
160625  const char *zName,
160626  sqlite3_file *pFile,
160627  int flags,
160628  int *pOutFlags
160629){
160630  static sqlite3_io_methods rbuvfs_io_methods = {
160631    2,                            /* iVersion */
160632    rbuVfsClose,                  /* xClose */
160633    rbuVfsRead,                   /* xRead */
160634    rbuVfsWrite,                  /* xWrite */
160635    rbuVfsTruncate,               /* xTruncate */
160636    rbuVfsSync,                   /* xSync */
160637    rbuVfsFileSize,               /* xFileSize */
160638    rbuVfsLock,                   /* xLock */
160639    rbuVfsUnlock,                 /* xUnlock */
160640    rbuVfsCheckReservedLock,      /* xCheckReservedLock */
160641    rbuVfsFileControl,            /* xFileControl */
160642    rbuVfsSectorSize,             /* xSectorSize */
160643    rbuVfsDeviceCharacteristics,  /* xDeviceCharacteristics */
160644    rbuVfsShmMap,                 /* xShmMap */
160645    rbuVfsShmLock,                /* xShmLock */
160646    rbuVfsShmBarrier,             /* xShmBarrier */
160647    rbuVfsShmUnmap                /* xShmUnmap */
160648  };
160649  rbu_vfs *pRbuVfs = (rbu_vfs*)pVfs;
160650  sqlite3_vfs *pRealVfs = pRbuVfs->pRealVfs;
160651  rbu_file *pFd = (rbu_file *)pFile;
160652  int rc = SQLITE_OK;
160653  const char *zOpen = zName;
160654
160655  memset(pFd, 0, sizeof(rbu_file));
160656  pFd->pReal = (sqlite3_file*)&pFd[1];
160657  pFd->pRbuVfs = pRbuVfs;
160658  pFd->openFlags = flags;
160659  if( zName ){
160660    if( flags & SQLITE_OPEN_MAIN_DB ){
160661      /* A main database has just been opened. The following block sets
160662      ** (pFd->zWal) to point to a buffer owned by SQLite that contains
160663      ** the name of the *-wal file this db connection will use. SQLite
160664      ** happens to pass a pointer to this buffer when using xAccess()
160665      ** or xOpen() to operate on the *-wal file.  */
160666      int n = strlen(zName);
160667      const char *z = &zName[n];
160668      if( flags & SQLITE_OPEN_URI ){
160669        int odd = 0;
160670        while( 1 ){
160671          if( z[0]==0 ){
160672            odd = 1 - odd;
160673            if( odd && z[1]==0 ) break;
160674          }
160675          z++;
160676        }
160677        z += 2;
160678      }else{
160679        while( *z==0 ) z++;
160680      }
160681      z += (n + 8 + 1);
160682      pFd->zWal = z;
160683    }
160684    else if( flags & SQLITE_OPEN_WAL ){
160685      rbu_file *pDb = rbuFindMaindb(pRbuVfs, zName);
160686      if( pDb ){
160687        if( pDb->pRbu && pDb->pRbu->eStage==RBU_STAGE_OAL ){
160688          /* This call is to open a *-wal file. Intead, open the *-oal. This
160689          ** code ensures that the string passed to xOpen() is terminated by a
160690          ** pair of '\0' bytes in case the VFS attempts to extract a URI
160691          ** parameter from it.  */
160692          int nCopy = strlen(zName);
160693          char *zCopy = sqlite3_malloc(nCopy+2);
160694          if( zCopy ){
160695            memcpy(zCopy, zName, nCopy);
160696            zCopy[nCopy-3] = 'o';
160697            zCopy[nCopy] = '\0';
160698            zCopy[nCopy+1] = '\0';
160699            zOpen = (const char*)(pFd->zDel = zCopy);
160700          }else{
160701            rc = SQLITE_NOMEM;
160702          }
160703          pFd->pRbu = pDb->pRbu;
160704        }
160705        pDb->pWalFd = pFd;
160706      }
160707    }
160708  }
160709
160710  if( rc==SQLITE_OK ){
160711    rc = pRealVfs->xOpen(pRealVfs, zOpen, pFd->pReal, flags, pOutFlags);
160712  }
160713  if( pFd->pReal->pMethods ){
160714    /* The xOpen() operation has succeeded. Set the sqlite3_file.pMethods
160715    ** pointer and, if the file is a main database file, link it into the
160716    ** mutex protected linked list of all such files.  */
160717    pFile->pMethods = &rbuvfs_io_methods;
160718    if( flags & SQLITE_OPEN_MAIN_DB ){
160719      sqlite3_mutex_enter(pRbuVfs->mutex);
160720      pFd->pMainNext = pRbuVfs->pMain;
160721      pRbuVfs->pMain = pFd;
160722      sqlite3_mutex_leave(pRbuVfs->mutex);
160723    }
160724  }else{
160725    sqlite3_free(pFd->zDel);
160726  }
160727
160728  return rc;
160729}
160730
160731/*
160732** Delete the file located at zPath.
160733*/
160734static int rbuVfsDelete(sqlite3_vfs *pVfs, const char *zPath, int dirSync){
160735  sqlite3_vfs *pRealVfs = ((rbu_vfs*)pVfs)->pRealVfs;
160736  return pRealVfs->xDelete(pRealVfs, zPath, dirSync);
160737}
160738
160739/*
160740** Test for access permissions. Return true if the requested permission
160741** is available, or false otherwise.
160742*/
160743static int rbuVfsAccess(
160744  sqlite3_vfs *pVfs,
160745  const char *zPath,
160746  int flags,
160747  int *pResOut
160748){
160749  rbu_vfs *pRbuVfs = (rbu_vfs*)pVfs;
160750  sqlite3_vfs *pRealVfs = pRbuVfs->pRealVfs;
160751  int rc;
160752
160753  rc = pRealVfs->xAccess(pRealVfs, zPath, flags, pResOut);
160754
160755  /* If this call is to check if a *-wal file associated with an RBU target
160756  ** database connection exists, and the RBU update is in RBU_STAGE_OAL,
160757  ** the following special handling is activated:
160758  **
160759  **   a) if the *-wal file does exist, return SQLITE_CANTOPEN. This
160760  **      ensures that the RBU extension never tries to update a database
160761  **      in wal mode, even if the first page of the database file has
160762  **      been damaged.
160763  **
160764  **   b) if the *-wal file does not exist, claim that it does anyway,
160765  **      causing SQLite to call xOpen() to open it. This call will also
160766  **      be intercepted (see the rbuVfsOpen() function) and the *-oal
160767  **      file opened instead.
160768  */
160769  if( rc==SQLITE_OK && flags==SQLITE_ACCESS_EXISTS ){
160770    rbu_file *pDb = rbuFindMaindb(pRbuVfs, zPath);
160771    if( pDb && pDb->pRbu && pDb->pRbu->eStage==RBU_STAGE_OAL ){
160772      if( *pResOut ){
160773        rc = SQLITE_CANTOPEN;
160774      }else{
160775        *pResOut = 1;
160776      }
160777    }
160778  }
160779
160780  return rc;
160781}
160782
160783/*
160784** Populate buffer zOut with the full canonical pathname corresponding
160785** to the pathname in zPath. zOut is guaranteed to point to a buffer
160786** of at least (DEVSYM_MAX_PATHNAME+1) bytes.
160787*/
160788static int rbuVfsFullPathname(
160789  sqlite3_vfs *pVfs,
160790  const char *zPath,
160791  int nOut,
160792  char *zOut
160793){
160794  sqlite3_vfs *pRealVfs = ((rbu_vfs*)pVfs)->pRealVfs;
160795  return pRealVfs->xFullPathname(pRealVfs, zPath, nOut, zOut);
160796}
160797
160798#ifndef SQLITE_OMIT_LOAD_EXTENSION
160799/*
160800** Open the dynamic library located at zPath and return a handle.
160801*/
160802static void *rbuVfsDlOpen(sqlite3_vfs *pVfs, const char *zPath){
160803  sqlite3_vfs *pRealVfs = ((rbu_vfs*)pVfs)->pRealVfs;
160804  return pRealVfs->xDlOpen(pRealVfs, zPath);
160805}
160806
160807/*
160808** Populate the buffer zErrMsg (size nByte bytes) with a human readable
160809** utf-8 string describing the most recent error encountered associated
160810** with dynamic libraries.
160811*/
160812static void rbuVfsDlError(sqlite3_vfs *pVfs, int nByte, char *zErrMsg){
160813  sqlite3_vfs *pRealVfs = ((rbu_vfs*)pVfs)->pRealVfs;
160814  pRealVfs->xDlError(pRealVfs, nByte, zErrMsg);
160815}
160816
160817/*
160818** Return a pointer to the symbol zSymbol in the dynamic library pHandle.
160819*/
160820static void (*rbuVfsDlSym(
160821  sqlite3_vfs *pVfs,
160822  void *pArg,
160823  const char *zSym
160824))(void){
160825  sqlite3_vfs *pRealVfs = ((rbu_vfs*)pVfs)->pRealVfs;
160826  return pRealVfs->xDlSym(pRealVfs, pArg, zSym);
160827}
160828
160829/*
160830** Close the dynamic library handle pHandle.
160831*/
160832static void rbuVfsDlClose(sqlite3_vfs *pVfs, void *pHandle){
160833  sqlite3_vfs *pRealVfs = ((rbu_vfs*)pVfs)->pRealVfs;
160834  pRealVfs->xDlClose(pRealVfs, pHandle);
160835}
160836#endif /* SQLITE_OMIT_LOAD_EXTENSION */
160837
160838/*
160839** Populate the buffer pointed to by zBufOut with nByte bytes of
160840** random data.
160841*/
160842static int rbuVfsRandomness(sqlite3_vfs *pVfs, int nByte, char *zBufOut){
160843  sqlite3_vfs *pRealVfs = ((rbu_vfs*)pVfs)->pRealVfs;
160844  return pRealVfs->xRandomness(pRealVfs, nByte, zBufOut);
160845}
160846
160847/*
160848** Sleep for nMicro microseconds. Return the number of microseconds
160849** actually slept.
160850*/
160851static int rbuVfsSleep(sqlite3_vfs *pVfs, int nMicro){
160852  sqlite3_vfs *pRealVfs = ((rbu_vfs*)pVfs)->pRealVfs;
160853  return pRealVfs->xSleep(pRealVfs, nMicro);
160854}
160855
160856/*
160857** Return the current time as a Julian Day number in *pTimeOut.
160858*/
160859static int rbuVfsCurrentTime(sqlite3_vfs *pVfs, double *pTimeOut){
160860  sqlite3_vfs *pRealVfs = ((rbu_vfs*)pVfs)->pRealVfs;
160861  return pRealVfs->xCurrentTime(pRealVfs, pTimeOut);
160862}
160863
160864/*
160865** No-op.
160866*/
160867static int rbuVfsGetLastError(sqlite3_vfs *pVfs, int a, char *b){
160868  return 0;
160869}
160870
160871/*
160872** Deregister and destroy an RBU vfs created by an earlier call to
160873** sqlite3rbu_create_vfs().
160874*/
160875SQLITE_API void SQLITE_STDCALL sqlite3rbu_destroy_vfs(const char *zName){
160876  sqlite3_vfs *pVfs = sqlite3_vfs_find(zName);
160877  if( pVfs && pVfs->xOpen==rbuVfsOpen ){
160878    sqlite3_mutex_free(((rbu_vfs*)pVfs)->mutex);
160879    sqlite3_vfs_unregister(pVfs);
160880    sqlite3_free(pVfs);
160881  }
160882}
160883
160884/*
160885** Create an RBU VFS named zName that accesses the underlying file-system
160886** via existing VFS zParent. The new object is registered as a non-default
160887** VFS with SQLite before returning.
160888*/
160889SQLITE_API int SQLITE_STDCALL sqlite3rbu_create_vfs(const char *zName, const char *zParent){
160890
160891  /* Template for VFS */
160892  static sqlite3_vfs vfs_template = {
160893    1,                            /* iVersion */
160894    0,                            /* szOsFile */
160895    0,                            /* mxPathname */
160896    0,                            /* pNext */
160897    0,                            /* zName */
160898    0,                            /* pAppData */
160899    rbuVfsOpen,                   /* xOpen */
160900    rbuVfsDelete,                 /* xDelete */
160901    rbuVfsAccess,                 /* xAccess */
160902    rbuVfsFullPathname,           /* xFullPathname */
160903
160904#ifndef SQLITE_OMIT_LOAD_EXTENSION
160905    rbuVfsDlOpen,                 /* xDlOpen */
160906    rbuVfsDlError,                /* xDlError */
160907    rbuVfsDlSym,                  /* xDlSym */
160908    rbuVfsDlClose,                /* xDlClose */
160909#else
160910    0, 0, 0, 0,
160911#endif
160912
160913    rbuVfsRandomness,             /* xRandomness */
160914    rbuVfsSleep,                  /* xSleep */
160915    rbuVfsCurrentTime,            /* xCurrentTime */
160916    rbuVfsGetLastError,           /* xGetLastError */
160917    0,                            /* xCurrentTimeInt64 (version 2) */
160918    0, 0, 0                       /* Unimplemented version 3 methods */
160919  };
160920
160921  rbu_vfs *pNew = 0;              /* Newly allocated VFS */
160922  int nName;
160923  int rc = SQLITE_OK;
160924
160925  int nByte;
160926  nName = strlen(zName);
160927  nByte = sizeof(rbu_vfs) + nName + 1;
160928  pNew = (rbu_vfs*)sqlite3_malloc(nByte);
160929  if( pNew==0 ){
160930    rc = SQLITE_NOMEM;
160931  }else{
160932    sqlite3_vfs *pParent;           /* Parent VFS */
160933    memset(pNew, 0, nByte);
160934    pParent = sqlite3_vfs_find(zParent);
160935    if( pParent==0 ){
160936      rc = SQLITE_NOTFOUND;
160937    }else{
160938      char *zSpace;
160939      memcpy(&pNew->base, &vfs_template, sizeof(sqlite3_vfs));
160940      pNew->base.mxPathname = pParent->mxPathname;
160941      pNew->base.szOsFile = sizeof(rbu_file) + pParent->szOsFile;
160942      pNew->pRealVfs = pParent;
160943      pNew->base.zName = (const char*)(zSpace = (char*)&pNew[1]);
160944      memcpy(zSpace, zName, nName);
160945
160946      /* Allocate the mutex and register the new VFS (not as the default) */
160947      pNew->mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_RECURSIVE);
160948      if( pNew->mutex==0 ){
160949        rc = SQLITE_NOMEM;
160950      }else{
160951        rc = sqlite3_vfs_register(&pNew->base, 0);
160952      }
160953    }
160954
160955    if( rc!=SQLITE_OK ){
160956      sqlite3_mutex_free(pNew->mutex);
160957      sqlite3_free(pNew);
160958    }
160959  }
160960
160961  return rc;
160962}
160963
160964
160965/**************************************************************************/
160966
160967#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_RBU) */
160968
160969/************** End of sqlite3rbu.c ******************************************/
160970/************** Begin file dbstat.c ******************************************/
160971/*
160972** 2010 July 12
160973**
160974** The author disclaims copyright to this source code.  In place of
160975** a legal notice, here is a blessing:
160976**
160977**    May you do good and not evil.
160978**    May you find forgiveness for yourself and forgive others.
160979**    May you share freely, never taking more than you give.
160980**
160981******************************************************************************
160982**
160983** This file contains an implementation of the "dbstat" virtual table.
160984**
160985** The dbstat virtual table is used to extract low-level formatting
160986** information from an SQLite database in order to implement the
160987** "sqlite3_analyzer" utility.  See the ../tool/spaceanal.tcl script
160988** for an example implementation.
160989*/
160990
160991/* #include "sqliteInt.h"   ** Requires access to internal data structures ** */
160992#if (defined(SQLITE_ENABLE_DBSTAT_VTAB) || defined(SQLITE_TEST)) \
160993    && !defined(SQLITE_OMIT_VIRTUALTABLE)
160994
160995/*
160996** Page paths:
160997**
160998**   The value of the 'path' column describes the path taken from the
160999**   root-node of the b-tree structure to each page. The value of the
161000**   root-node path is '/'.
161001**
161002**   The value of the path for the left-most child page of the root of
161003**   a b-tree is '/000/'. (Btrees store content ordered from left to right
161004**   so the pages to the left have smaller keys than the pages to the right.)
161005**   The next to left-most child of the root page is
161006**   '/001', and so on, each sibling page identified by a 3-digit hex
161007**   value. The children of the 451st left-most sibling have paths such
161008**   as '/1c2/000/, '/1c2/001/' etc.
161009**
161010**   Overflow pages are specified by appending a '+' character and a
161011**   six-digit hexadecimal value to the path to the cell they are linked
161012**   from. For example, the three overflow pages in a chain linked from
161013**   the left-most cell of the 450th child of the root page are identified
161014**   by the paths:
161015**
161016**      '/1c2/000+000000'         // First page in overflow chain
161017**      '/1c2/000+000001'         // Second page in overflow chain
161018**      '/1c2/000+000002'         // Third page in overflow chain
161019**
161020**   If the paths are sorted using the BINARY collation sequence, then
161021**   the overflow pages associated with a cell will appear earlier in the
161022**   sort-order than its child page:
161023**
161024**      '/1c2/000/'               // Left-most child of 451st child of root
161025*/
161026#define VTAB_SCHEMA                                                         \
161027  "CREATE TABLE xx( "                                                       \
161028  "  name       STRING,           /* Name of table or index */"             \
161029  "  path       INTEGER,          /* Path to page from root */"             \
161030  "  pageno     INTEGER,          /* Page number */"                        \
161031  "  pagetype   STRING,           /* 'internal', 'leaf' or 'overflow' */"   \
161032  "  ncell      INTEGER,          /* Cells on page (0 for overflow) */"     \
161033  "  payload    INTEGER,          /* Bytes of payload on this page */"      \
161034  "  unused     INTEGER,          /* Bytes of unused space on this page */" \
161035  "  mx_payload INTEGER,          /* Largest payload size of all cells */"  \
161036  "  pgoffset   INTEGER,          /* Offset of page in file */"             \
161037  "  pgsize     INTEGER           /* Size of the page */"                   \
161038  ");"
161039
161040
161041typedef struct StatTable StatTable;
161042typedef struct StatCursor StatCursor;
161043typedef struct StatPage StatPage;
161044typedef struct StatCell StatCell;
161045
161046struct StatCell {
161047  int nLocal;                     /* Bytes of local payload */
161048  u32 iChildPg;                   /* Child node (or 0 if this is a leaf) */
161049  int nOvfl;                      /* Entries in aOvfl[] */
161050  u32 *aOvfl;                     /* Array of overflow page numbers */
161051  int nLastOvfl;                  /* Bytes of payload on final overflow page */
161052  int iOvfl;                      /* Iterates through aOvfl[] */
161053};
161054
161055struct StatPage {
161056  u32 iPgno;
161057  DbPage *pPg;
161058  int iCell;
161059
161060  char *zPath;                    /* Path to this page */
161061
161062  /* Variables populated by statDecodePage(): */
161063  u8 flags;                       /* Copy of flags byte */
161064  int nCell;                      /* Number of cells on page */
161065  int nUnused;                    /* Number of unused bytes on page */
161066  StatCell *aCell;                /* Array of parsed cells */
161067  u32 iRightChildPg;              /* Right-child page number (or 0) */
161068  int nMxPayload;                 /* Largest payload of any cell on this page */
161069};
161070
161071struct StatCursor {
161072  sqlite3_vtab_cursor base;
161073  sqlite3_stmt *pStmt;            /* Iterates through set of root pages */
161074  int isEof;                      /* After pStmt has returned SQLITE_DONE */
161075
161076  StatPage aPage[32];
161077  int iPage;                      /* Current entry in aPage[] */
161078
161079  /* Values to return. */
161080  char *zName;                    /* Value of 'name' column */
161081  char *zPath;                    /* Value of 'path' column */
161082  u32 iPageno;                    /* Value of 'pageno' column */
161083  char *zPagetype;                /* Value of 'pagetype' column */
161084  int nCell;                      /* Value of 'ncell' column */
161085  int nPayload;                   /* Value of 'payload' column */
161086  int nUnused;                    /* Value of 'unused' column */
161087  int nMxPayload;                 /* Value of 'mx_payload' column */
161088  i64 iOffset;                    /* Value of 'pgOffset' column */
161089  int szPage;                     /* Value of 'pgSize' column */
161090};
161091
161092struct StatTable {
161093  sqlite3_vtab base;
161094  sqlite3 *db;
161095  int iDb;                        /* Index of database to analyze */
161096};
161097
161098#ifndef get2byte
161099# define get2byte(x)   ((x)[0]<<8 | (x)[1])
161100#endif
161101
161102/*
161103** Connect to or create a statvfs virtual table.
161104*/
161105static int statConnect(
161106  sqlite3 *db,
161107  void *pAux,
161108  int argc, const char *const*argv,
161109  sqlite3_vtab **ppVtab,
161110  char **pzErr
161111){
161112  StatTable *pTab = 0;
161113  int rc = SQLITE_OK;
161114  int iDb;
161115
161116  if( argc>=4 ){
161117    iDb = sqlite3FindDbName(db, argv[3]);
161118    if( iDb<0 ){
161119      *pzErr = sqlite3_mprintf("no such database: %s", argv[3]);
161120      return SQLITE_ERROR;
161121    }
161122  }else{
161123    iDb = 0;
161124  }
161125  rc = sqlite3_declare_vtab(db, VTAB_SCHEMA);
161126  if( rc==SQLITE_OK ){
161127    pTab = (StatTable *)sqlite3_malloc64(sizeof(StatTable));
161128    if( pTab==0 ) rc = SQLITE_NOMEM;
161129  }
161130
161131  assert( rc==SQLITE_OK || pTab==0 );
161132  if( rc==SQLITE_OK ){
161133    memset(pTab, 0, sizeof(StatTable));
161134    pTab->db = db;
161135    pTab->iDb = iDb;
161136  }
161137
161138  *ppVtab = (sqlite3_vtab*)pTab;
161139  return rc;
161140}
161141
161142/*
161143** Disconnect from or destroy a statvfs virtual table.
161144*/
161145static int statDisconnect(sqlite3_vtab *pVtab){
161146  sqlite3_free(pVtab);
161147  return SQLITE_OK;
161148}
161149
161150/*
161151** There is no "best-index". This virtual table always does a linear
161152** scan of the binary VFS log file.
161153*/
161154static int statBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdxInfo){
161155
161156  /* Records are always returned in ascending order of (name, path).
161157  ** If this will satisfy the client, set the orderByConsumed flag so that
161158  ** SQLite does not do an external sort.
161159  */
161160  if( ( pIdxInfo->nOrderBy==1
161161     && pIdxInfo->aOrderBy[0].iColumn==0
161162     && pIdxInfo->aOrderBy[0].desc==0
161163     ) ||
161164      ( pIdxInfo->nOrderBy==2
161165     && pIdxInfo->aOrderBy[0].iColumn==0
161166     && pIdxInfo->aOrderBy[0].desc==0
161167     && pIdxInfo->aOrderBy[1].iColumn==1
161168     && pIdxInfo->aOrderBy[1].desc==0
161169     )
161170  ){
161171    pIdxInfo->orderByConsumed = 1;
161172  }
161173
161174  pIdxInfo->estimatedCost = 10.0;
161175  return SQLITE_OK;
161176}
161177
161178/*
161179** Open a new statvfs cursor.
161180*/
161181static int statOpen(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor){
161182  StatTable *pTab = (StatTable *)pVTab;
161183  StatCursor *pCsr;
161184  int rc;
161185
161186  pCsr = (StatCursor *)sqlite3_malloc64(sizeof(StatCursor));
161187  if( pCsr==0 ){
161188    rc = SQLITE_NOMEM;
161189  }else{
161190    char *zSql;
161191    memset(pCsr, 0, sizeof(StatCursor));
161192    pCsr->base.pVtab = pVTab;
161193
161194    zSql = sqlite3_mprintf(
161195        "SELECT 'sqlite_master' AS name, 1 AS rootpage, 'table' AS type"
161196        "  UNION ALL  "
161197        "SELECT name, rootpage, type"
161198        "  FROM \"%w\".sqlite_master WHERE rootpage!=0"
161199        "  ORDER BY name", pTab->db->aDb[pTab->iDb].zName);
161200    if( zSql==0 ){
161201      rc = SQLITE_NOMEM;
161202    }else{
161203      rc = sqlite3_prepare_v2(pTab->db, zSql, -1, &pCsr->pStmt, 0);
161204      sqlite3_free(zSql);
161205    }
161206    if( rc!=SQLITE_OK ){
161207      sqlite3_free(pCsr);
161208      pCsr = 0;
161209    }
161210  }
161211
161212  *ppCursor = (sqlite3_vtab_cursor *)pCsr;
161213  return rc;
161214}
161215
161216static void statClearPage(StatPage *p){
161217  int i;
161218  if( p->aCell ){
161219    for(i=0; i<p->nCell; i++){
161220      sqlite3_free(p->aCell[i].aOvfl);
161221    }
161222    sqlite3_free(p->aCell);
161223  }
161224  sqlite3PagerUnref(p->pPg);
161225  sqlite3_free(p->zPath);
161226  memset(p, 0, sizeof(StatPage));
161227}
161228
161229static void statResetCsr(StatCursor *pCsr){
161230  int i;
161231  sqlite3_reset(pCsr->pStmt);
161232  for(i=0; i<ArraySize(pCsr->aPage); i++){
161233    statClearPage(&pCsr->aPage[i]);
161234  }
161235  pCsr->iPage = 0;
161236  sqlite3_free(pCsr->zPath);
161237  pCsr->zPath = 0;
161238}
161239
161240/*
161241** Close a statvfs cursor.
161242*/
161243static int statClose(sqlite3_vtab_cursor *pCursor){
161244  StatCursor *pCsr = (StatCursor *)pCursor;
161245  statResetCsr(pCsr);
161246  sqlite3_finalize(pCsr->pStmt);
161247  sqlite3_free(pCsr);
161248  return SQLITE_OK;
161249}
161250
161251static void getLocalPayload(
161252  int nUsable,                    /* Usable bytes per page */
161253  u8 flags,                       /* Page flags */
161254  int nTotal,                     /* Total record (payload) size */
161255  int *pnLocal                    /* OUT: Bytes stored locally */
161256){
161257  int nLocal;
161258  int nMinLocal;
161259  int nMaxLocal;
161260
161261  if( flags==0x0D ){              /* Table leaf node */
161262    nMinLocal = (nUsable - 12) * 32 / 255 - 23;
161263    nMaxLocal = nUsable - 35;
161264  }else{                          /* Index interior and leaf nodes */
161265    nMinLocal = (nUsable - 12) * 32 / 255 - 23;
161266    nMaxLocal = (nUsable - 12) * 64 / 255 - 23;
161267  }
161268
161269  nLocal = nMinLocal + (nTotal - nMinLocal) % (nUsable - 4);
161270  if( nLocal>nMaxLocal ) nLocal = nMinLocal;
161271  *pnLocal = nLocal;
161272}
161273
161274static int statDecodePage(Btree *pBt, StatPage *p){
161275  int nUnused;
161276  int iOff;
161277  int nHdr;
161278  int isLeaf;
161279  int szPage;
161280
161281  u8 *aData = sqlite3PagerGetData(p->pPg);
161282  u8 *aHdr = &aData[p->iPgno==1 ? 100 : 0];
161283
161284  p->flags = aHdr[0];
161285  p->nCell = get2byte(&aHdr[3]);
161286  p->nMxPayload = 0;
161287
161288  isLeaf = (p->flags==0x0A || p->flags==0x0D);
161289  nHdr = 12 - isLeaf*4 + (p->iPgno==1)*100;
161290
161291  nUnused = get2byte(&aHdr[5]) - nHdr - 2*p->nCell;
161292  nUnused += (int)aHdr[7];
161293  iOff = get2byte(&aHdr[1]);
161294  while( iOff ){
161295    nUnused += get2byte(&aData[iOff+2]);
161296    iOff = get2byte(&aData[iOff]);
161297  }
161298  p->nUnused = nUnused;
161299  p->iRightChildPg = isLeaf ? 0 : sqlite3Get4byte(&aHdr[8]);
161300  szPage = sqlite3BtreeGetPageSize(pBt);
161301
161302  if( p->nCell ){
161303    int i;                        /* Used to iterate through cells */
161304    int nUsable;                  /* Usable bytes per page */
161305
161306    sqlite3BtreeEnter(pBt);
161307    nUsable = szPage - sqlite3BtreeGetReserveNoMutex(pBt);
161308    sqlite3BtreeLeave(pBt);
161309    p->aCell = sqlite3_malloc64((p->nCell+1) * sizeof(StatCell));
161310    if( p->aCell==0 ) return SQLITE_NOMEM;
161311    memset(p->aCell, 0, (p->nCell+1) * sizeof(StatCell));
161312
161313    for(i=0; i<p->nCell; i++){
161314      StatCell *pCell = &p->aCell[i];
161315
161316      iOff = get2byte(&aData[nHdr+i*2]);
161317      if( !isLeaf ){
161318        pCell->iChildPg = sqlite3Get4byte(&aData[iOff]);
161319        iOff += 4;
161320      }
161321      if( p->flags==0x05 ){
161322        /* A table interior node. nPayload==0. */
161323      }else{
161324        u32 nPayload;             /* Bytes of payload total (local+overflow) */
161325        int nLocal;               /* Bytes of payload stored locally */
161326        iOff += getVarint32(&aData[iOff], nPayload);
161327        if( p->flags==0x0D ){
161328          u64 dummy;
161329          iOff += sqlite3GetVarint(&aData[iOff], &dummy);
161330        }
161331        if( nPayload>(u32)p->nMxPayload ) p->nMxPayload = nPayload;
161332        getLocalPayload(nUsable, p->flags, nPayload, &nLocal);
161333        pCell->nLocal = nLocal;
161334        assert( nLocal>=0 );
161335        assert( nPayload>=(u32)nLocal );
161336        assert( nLocal<=(nUsable-35) );
161337        if( nPayload>(u32)nLocal ){
161338          int j;
161339          int nOvfl = ((nPayload - nLocal) + nUsable-4 - 1) / (nUsable - 4);
161340          pCell->nLastOvfl = (nPayload-nLocal) - (nOvfl-1) * (nUsable-4);
161341          pCell->nOvfl = nOvfl;
161342          pCell->aOvfl = sqlite3_malloc64(sizeof(u32)*nOvfl);
161343          if( pCell->aOvfl==0 ) return SQLITE_NOMEM;
161344          pCell->aOvfl[0] = sqlite3Get4byte(&aData[iOff+nLocal]);
161345          for(j=1; j<nOvfl; j++){
161346            int rc;
161347            u32 iPrev = pCell->aOvfl[j-1];
161348            DbPage *pPg = 0;
161349            rc = sqlite3PagerGet(sqlite3BtreePager(pBt), iPrev, &pPg);
161350            if( rc!=SQLITE_OK ){
161351              assert( pPg==0 );
161352              return rc;
161353            }
161354            pCell->aOvfl[j] = sqlite3Get4byte(sqlite3PagerGetData(pPg));
161355            sqlite3PagerUnref(pPg);
161356          }
161357        }
161358      }
161359    }
161360  }
161361
161362  return SQLITE_OK;
161363}
161364
161365/*
161366** Populate the pCsr->iOffset and pCsr->szPage member variables. Based on
161367** the current value of pCsr->iPageno.
161368*/
161369static void statSizeAndOffset(StatCursor *pCsr){
161370  StatTable *pTab = (StatTable *)((sqlite3_vtab_cursor *)pCsr)->pVtab;
161371  Btree *pBt = pTab->db->aDb[pTab->iDb].pBt;
161372  Pager *pPager = sqlite3BtreePager(pBt);
161373  sqlite3_file *fd;
161374  sqlite3_int64 x[2];
161375
161376  /* The default page size and offset */
161377  pCsr->szPage = sqlite3BtreeGetPageSize(pBt);
161378  pCsr->iOffset = (i64)pCsr->szPage * (pCsr->iPageno - 1);
161379
161380  /* If connected to a ZIPVFS backend, override the page size and
161381  ** offset with actual values obtained from ZIPVFS.
161382  */
161383  fd = sqlite3PagerFile(pPager);
161384  x[0] = pCsr->iPageno;
161385  if( fd->pMethods!=0 && sqlite3OsFileControl(fd, 230440, &x)==SQLITE_OK ){
161386    pCsr->iOffset = x[0];
161387    pCsr->szPage = (int)x[1];
161388  }
161389}
161390
161391/*
161392** Move a statvfs cursor to the next entry in the file.
161393*/
161394static int statNext(sqlite3_vtab_cursor *pCursor){
161395  int rc;
161396  int nPayload;
161397  char *z;
161398  StatCursor *pCsr = (StatCursor *)pCursor;
161399  StatTable *pTab = (StatTable *)pCursor->pVtab;
161400  Btree *pBt = pTab->db->aDb[pTab->iDb].pBt;
161401  Pager *pPager = sqlite3BtreePager(pBt);
161402
161403  sqlite3_free(pCsr->zPath);
161404  pCsr->zPath = 0;
161405
161406statNextRestart:
161407  if( pCsr->aPage[0].pPg==0 ){
161408    rc = sqlite3_step(pCsr->pStmt);
161409    if( rc==SQLITE_ROW ){
161410      int nPage;
161411      u32 iRoot = (u32)sqlite3_column_int64(pCsr->pStmt, 1);
161412      sqlite3PagerPagecount(pPager, &nPage);
161413      if( nPage==0 ){
161414        pCsr->isEof = 1;
161415        return sqlite3_reset(pCsr->pStmt);
161416      }
161417      rc = sqlite3PagerGet(pPager, iRoot, &pCsr->aPage[0].pPg);
161418      pCsr->aPage[0].iPgno = iRoot;
161419      pCsr->aPage[0].iCell = 0;
161420      pCsr->aPage[0].zPath = z = sqlite3_mprintf("/");
161421      pCsr->iPage = 0;
161422      if( z==0 ) rc = SQLITE_NOMEM;
161423    }else{
161424      pCsr->isEof = 1;
161425      return sqlite3_reset(pCsr->pStmt);
161426    }
161427  }else{
161428
161429    /* Page p itself has already been visited. */
161430    StatPage *p = &pCsr->aPage[pCsr->iPage];
161431
161432    while( p->iCell<p->nCell ){
161433      StatCell *pCell = &p->aCell[p->iCell];
161434      if( pCell->iOvfl<pCell->nOvfl ){
161435        int nUsable;
161436        sqlite3BtreeEnter(pBt);
161437        nUsable = sqlite3BtreeGetPageSize(pBt) -
161438                        sqlite3BtreeGetReserveNoMutex(pBt);
161439        sqlite3BtreeLeave(pBt);
161440        pCsr->zName = (char *)sqlite3_column_text(pCsr->pStmt, 0);
161441        pCsr->iPageno = pCell->aOvfl[pCell->iOvfl];
161442        pCsr->zPagetype = "overflow";
161443        pCsr->nCell = 0;
161444        pCsr->nMxPayload = 0;
161445        pCsr->zPath = z = sqlite3_mprintf(
161446            "%s%.3x+%.6x", p->zPath, p->iCell, pCell->iOvfl
161447        );
161448        if( pCell->iOvfl<pCell->nOvfl-1 ){
161449          pCsr->nUnused = 0;
161450          pCsr->nPayload = nUsable - 4;
161451        }else{
161452          pCsr->nPayload = pCell->nLastOvfl;
161453          pCsr->nUnused = nUsable - 4 - pCsr->nPayload;
161454        }
161455        pCell->iOvfl++;
161456        statSizeAndOffset(pCsr);
161457        return z==0 ? SQLITE_NOMEM : SQLITE_OK;
161458      }
161459      if( p->iRightChildPg ) break;
161460      p->iCell++;
161461    }
161462
161463    if( !p->iRightChildPg || p->iCell>p->nCell ){
161464      statClearPage(p);
161465      if( pCsr->iPage==0 ) return statNext(pCursor);
161466      pCsr->iPage--;
161467      goto statNextRestart; /* Tail recursion */
161468    }
161469    pCsr->iPage++;
161470    assert( p==&pCsr->aPage[pCsr->iPage-1] );
161471
161472    if( p->iCell==p->nCell ){
161473      p[1].iPgno = p->iRightChildPg;
161474    }else{
161475      p[1].iPgno = p->aCell[p->iCell].iChildPg;
161476    }
161477    rc = sqlite3PagerGet(pPager, p[1].iPgno, &p[1].pPg);
161478    p[1].iCell = 0;
161479    p[1].zPath = z = sqlite3_mprintf("%s%.3x/", p->zPath, p->iCell);
161480    p->iCell++;
161481    if( z==0 ) rc = SQLITE_NOMEM;
161482  }
161483
161484
161485  /* Populate the StatCursor fields with the values to be returned
161486  ** by the xColumn() and xRowid() methods.
161487  */
161488  if( rc==SQLITE_OK ){
161489    int i;
161490    StatPage *p = &pCsr->aPage[pCsr->iPage];
161491    pCsr->zName = (char *)sqlite3_column_text(pCsr->pStmt, 0);
161492    pCsr->iPageno = p->iPgno;
161493
161494    rc = statDecodePage(pBt, p);
161495    if( rc==SQLITE_OK ){
161496      statSizeAndOffset(pCsr);
161497
161498      switch( p->flags ){
161499        case 0x05:             /* table internal */
161500        case 0x02:             /* index internal */
161501          pCsr->zPagetype = "internal";
161502          break;
161503        case 0x0D:             /* table leaf */
161504        case 0x0A:             /* index leaf */
161505          pCsr->zPagetype = "leaf";
161506          break;
161507        default:
161508          pCsr->zPagetype = "corrupted";
161509          break;
161510      }
161511      pCsr->nCell = p->nCell;
161512      pCsr->nUnused = p->nUnused;
161513      pCsr->nMxPayload = p->nMxPayload;
161514      pCsr->zPath = z = sqlite3_mprintf("%s", p->zPath);
161515      if( z==0 ) rc = SQLITE_NOMEM;
161516      nPayload = 0;
161517      for(i=0; i<p->nCell; i++){
161518        nPayload += p->aCell[i].nLocal;
161519      }
161520      pCsr->nPayload = nPayload;
161521    }
161522  }
161523
161524  return rc;
161525}
161526
161527static int statEof(sqlite3_vtab_cursor *pCursor){
161528  StatCursor *pCsr = (StatCursor *)pCursor;
161529  return pCsr->isEof;
161530}
161531
161532static int statFilter(
161533  sqlite3_vtab_cursor *pCursor,
161534  int idxNum, const char *idxStr,
161535  int argc, sqlite3_value **argv
161536){
161537  StatCursor *pCsr = (StatCursor *)pCursor;
161538
161539  statResetCsr(pCsr);
161540  return statNext(pCursor);
161541}
161542
161543static int statColumn(
161544  sqlite3_vtab_cursor *pCursor,
161545  sqlite3_context *ctx,
161546  int i
161547){
161548  StatCursor *pCsr = (StatCursor *)pCursor;
161549  switch( i ){
161550    case 0:            /* name */
161551      sqlite3_result_text(ctx, pCsr->zName, -1, SQLITE_TRANSIENT);
161552      break;
161553    case 1:            /* path */
161554      sqlite3_result_text(ctx, pCsr->zPath, -1, SQLITE_TRANSIENT);
161555      break;
161556    case 2:            /* pageno */
161557      sqlite3_result_int64(ctx, pCsr->iPageno);
161558      break;
161559    case 3:            /* pagetype */
161560      sqlite3_result_text(ctx, pCsr->zPagetype, -1, SQLITE_STATIC);
161561      break;
161562    case 4:            /* ncell */
161563      sqlite3_result_int(ctx, pCsr->nCell);
161564      break;
161565    case 5:            /* payload */
161566      sqlite3_result_int(ctx, pCsr->nPayload);
161567      break;
161568    case 6:            /* unused */
161569      sqlite3_result_int(ctx, pCsr->nUnused);
161570      break;
161571    case 7:            /* mx_payload */
161572      sqlite3_result_int(ctx, pCsr->nMxPayload);
161573      break;
161574    case 8:            /* pgoffset */
161575      sqlite3_result_int64(ctx, pCsr->iOffset);
161576      break;
161577    default:           /* pgsize */
161578      assert( i==9 );
161579      sqlite3_result_int(ctx, pCsr->szPage);
161580      break;
161581  }
161582  return SQLITE_OK;
161583}
161584
161585static int statRowid(sqlite3_vtab_cursor *pCursor, sqlite_int64 *pRowid){
161586  StatCursor *pCsr = (StatCursor *)pCursor;
161587  *pRowid = pCsr->iPageno;
161588  return SQLITE_OK;
161589}
161590
161591/*
161592** Invoke this routine to register the "dbstat" virtual table module
161593*/
161594SQLITE_PRIVATE int sqlite3DbstatRegister(sqlite3 *db){
161595  static sqlite3_module dbstat_module = {
161596    0,                            /* iVersion */
161597    statConnect,                  /* xCreate */
161598    statConnect,                  /* xConnect */
161599    statBestIndex,                /* xBestIndex */
161600    statDisconnect,               /* xDisconnect */
161601    statDisconnect,               /* xDestroy */
161602    statOpen,                     /* xOpen - open a cursor */
161603    statClose,                    /* xClose - close a cursor */
161604    statFilter,                   /* xFilter - configure scan constraints */
161605    statNext,                     /* xNext - advance a cursor */
161606    statEof,                      /* xEof - check for end of scan */
161607    statColumn,                   /* xColumn - read data */
161608    statRowid,                    /* xRowid - read data */
161609    0,                            /* xUpdate */
161610    0,                            /* xBegin */
161611    0,                            /* xSync */
161612    0,                            /* xCommit */
161613    0,                            /* xRollback */
161614    0,                            /* xFindMethod */
161615    0,                            /* xRename */
161616  };
161617  return sqlite3_create_module(db, "dbstat", &dbstat_module, 0);
161618}
161619#elif defined(SQLITE_ENABLE_DBSTAT_VTAB)
161620SQLITE_PRIVATE int sqlite3DbstatRegister(sqlite3 *db){ return SQLITE_OK; }
161621#endif /* SQLITE_ENABLE_DBSTAT_VTAB */
161622
161623/************** End of dbstat.c **********************************************/
161624